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 * $FreeBSD$ 3015e32d5dSMike Smith */ 3115e32d5dSMike Smith 3215e32d5dSMike Smith #include "opt_acpi.h" 3315e32d5dSMike Smith #include <sys/param.h> 3415e32d5dSMike Smith #include <sys/kernel.h> 35fb919e4dSMark Murray #include <sys/proc.h> 36a89fcf28STakanori Watanabe #include <sys/fcntl.h> 3715e32d5dSMike Smith #include <sys/malloc.h> 38fe12f24bSPoul-Henning Kamp #include <sys/module.h> 3915e32d5dSMike Smith #include <sys/bus.h> 4015e32d5dSMike Smith #include <sys/conf.h> 4115e32d5dSMike Smith #include <sys/ioccom.h> 4215e32d5dSMike Smith #include <sys/reboot.h> 4315e32d5dSMike Smith #include <sys/sysctl.h> 4415e32d5dSMike Smith #include <sys/ctype.h> 451611ea87SMitsuru IWASAKI #include <sys/linker.h> 46f9390180SMitsuru IWASAKI #include <sys/power.h> 4754f6bca0SNate Lawson #include <sys/sbuf.h> 48eeb3a05fSNate Lawson #include <sys/smp.h> 4915e32d5dSMike Smith 5015e32d5dSMike Smith #include <machine/clock.h> 5115e32d5dSMike Smith #include <machine/resource.h> 52dc750869SNate Lawson #include <machine/bus.h> 53dc750869SNate Lawson #include <sys/rman.h> 54bc0f2195SMike Smith #include <isa/isavar.h> 55c796e27bSNate Lawson #include <isa/pnpvar.h> 56bc0f2195SMike Smith 5715e32d5dSMike Smith #include "acpi.h" 5815e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5915e32d5dSMike Smith #include <dev/acpica/acpiio.h> 609b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h> 6115e32d5dSMike Smith 6215e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 6315e32d5dSMike Smith 64be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 65cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 66b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 670ae55423SMike Smith 6815e32d5dSMike Smith static d_open_t acpiopen; 6915e32d5dSMike Smith static d_close_t acpiclose; 7015e32d5dSMike Smith static d_ioctl_t acpiioctl; 7115e32d5dSMike Smith 7215e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 73dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 747ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 757ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 767ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 777ac40f5fSPoul-Henning Kamp .d_name = "acpi", 7815e32d5dSMike Smith }; 7915e32d5dSMike Smith 80346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */ 81cb9b0d80SMike Smith struct mtx acpi_mutex; 82cb9b0d80SMike Smith 8389fb5af8SNate Lawson /* Bitmap of device quirks. */ 8489fb5af8SNate Lawson int acpi_quirks; 8589fb5af8SNate Lawson 866d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 8715e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 8815e32d5dSMike Smith static int acpi_probe(device_t dev); 8915e32d5dSMike Smith static int acpi_attach(device_t dev); 90169b539aSNate Lawson static int acpi_shutdown(device_t dev); 91be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 92be2b1797SNate Lawson int unit); 9315e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 94be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 95be2b1797SNate Lawson uintptr_t *result); 96be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 97be2b1797SNate Lawson uintptr_t value); 9891233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 99adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev); 100adad4744SNate Lawson static struct resource_list_entry *acpi_sysres_find(device_t dev, int type, 101adad4744SNate Lawson u_long addr); 102be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 103be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 104be2b1797SNate Lawson u_long count, u_int flags); 105be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 106be2b1797SNate Lawson int rid, struct resource *r); 1071e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1081e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 109ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 110ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 111ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 112ce619b2eSNate Lawson ACPI_BUFFER *ret); 113df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 114df8d2a32SNate Lawson void *context, void **retval); 115df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 116df8d2a32SNate Lawson int max_depth, acpi_scan_cb_t user_fn, void *arg); 117be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 118be2b1797SNate Lawson struct isa_pnp_id *ids); 11915e32d5dSMike Smith static void acpi_probe_children(device_t bus); 120b82ca9a9SNate Lawson static int acpi_probe_order(ACPI_HANDLE handle, int *order); 121be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 122be2b1797SNate Lawson void *context, void **status); 123ce619b2eSNate Lawson static BOOLEAN acpi_MatchHid(ACPI_HANDLE h, const char *hid); 12415e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 12513d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 126d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 127d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 128d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate); 12988a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 13088a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 13115e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 13215e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 133d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1341d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 135f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 136879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 137879d6c23STakanori Watanabe char *buf, size_t buflen); 138879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 139879d6c23STakanori Watanabe char *buf, size_t buflen); 140879d6c23STakanori Watanabe 14115e32d5dSMike Smith static device_method_t acpi_methods[] = { 14215e32d5dSMike Smith /* Device interface */ 14315e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 14415e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 14515e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 146169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 14756a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 14815e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 14915e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 15015e32d5dSMike Smith 15115e32d5dSMike Smith /* Bus interface */ 15215e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 15315e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 15415e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 15515e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 15691233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 15791233413SNate Lawson DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 15891233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 15915e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 16015e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 161879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 162879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 16315e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 16415e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 16515e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 16615e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 16715e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 16815e32d5dSMike Smith 169ce619b2eSNate Lawson /* ACPI bus */ 170ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 171ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 172df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 173ce619b2eSNate Lawson 174bc0f2195SMike Smith /* ISA emulation */ 175bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 176bc0f2195SMike Smith 17715e32d5dSMike Smith {0, 0} 17815e32d5dSMike Smith }; 17915e32d5dSMike Smith 18015e32d5dSMike Smith static driver_t acpi_driver = { 18115e32d5dSMike Smith "acpi", 18215e32d5dSMike Smith acpi_methods, 18315e32d5dSMike Smith sizeof(struct acpi_softc), 18415e32d5dSMike Smith }; 18515e32d5dSMike Smith 1863273b005SMike Smith static devclass_t acpi_devclass; 1876d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 188a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 18915e32d5dSMike Smith 19015e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 19115e2f34fSNate Lawson 192adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 193adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem; 194adad4744SNate Lawson 195bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5 196bee4aa4aSNate Lawson 197865b8d0bSNate Lawson static const char* sleep_state_names[] = { 198865b8d0bSNate Lawson "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 199865b8d0bSNate Lawson 2001d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 2011d7b121cSNate Lawson static char acpi_ca_version[12]; 2021d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2031d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 20415e32d5dSMike Smith 20515e32d5dSMike Smith /* 206413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 207c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 208c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 209c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 210c9b8d77dSNate Lawson * some IBMs use such code. 211413081d7SNate Lawson */ 212c9b8d77dSNate Lawson static int acpi_serialize_methods; 213413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 214413081d7SNate Lawson 215c9b8d77dSNate Lawson /* 2166d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2176d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 218be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 219be2b1797SNate Lawson * off it. 2206d63101aSMike Smith */ 2216d63101aSMike Smith static int 2226d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2236d63101aSMike Smith { 2246d63101aSMike Smith switch (event) { 2256d63101aSMike Smith case MOD_LOAD: 22687b45ed5SMitsuru IWASAKI if (!cold) { 22787b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2286d63101aSMike Smith return (EPERM); 22987b45ed5SMitsuru IWASAKI } 2306d63101aSMike Smith break; 2316d63101aSMike Smith case MOD_UNLOAD: 232f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2336d63101aSMike Smith return (EBUSY); 234f9390180SMitsuru IWASAKI break; 2356d63101aSMike Smith default: 2366d63101aSMike Smith break; 2376d63101aSMike Smith } 2386d63101aSMike Smith return (0); 2396d63101aSMike Smith } 2406d63101aSMike Smith 2416d63101aSMike Smith /* 242bbc2815cSJohn Baldwin * Perform early initialization. 24315e32d5dSMike Smith */ 244bbc2815cSJohn Baldwin ACPI_STATUS 245bbc2815cSJohn Baldwin acpi_Startup(void) 24615e32d5dSMike Smith { 24789fb5af8SNate Lawson static int started = 0; 24889fb5af8SNate Lawson int error, val; 24915e32d5dSMike Smith 2501b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2511b8c233dSPeter Pentchev 252bee4aa4aSNate Lawson /* Only run the startup code once. The MADT driver also calls this. */ 253bbc2815cSJohn Baldwin if (started) 25489fb5af8SNate Lawson return_VALUE (0); 255bbc2815cSJohn Baldwin started = 1; 25615e32d5dSMike Smith 257be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2586008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 259cb9b0d80SMike Smith 260413081d7SNate Lawson /* 261413081d7SNate Lawson * Set the globals from our tunables. This is needed because ACPI-CA 262f3fc4f8bSNate Lawson * uses UINT8 for some values and we have no tunable_byte. 263413081d7SNate Lawson */ 264f3fc4f8bSNate Lawson AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods; 265413081d7SNate Lawson 266be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 267b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 268bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 269bbc2815cSJohn Baldwin return_VALUE (error); 27015e32d5dSMike Smith } 2711611ea87SMitsuru IWASAKI 272b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 273bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 27489fb5af8SNate Lawson AcpiTerminate(); 275bbc2815cSJohn Baldwin return_VALUE (error); 27615e32d5dSMike Smith } 2773184cf5aSNate Lawson 27889fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 27989fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 28089fb5af8SNate Lawson 28189fb5af8SNate Lawson /* If the user manually set the disabled hint to 0, override any quirk. */ 28289fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 28389fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 28489fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 28589fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 28689fb5af8SNate Lawson AcpiTerminate(); 2874e376d58SNate Lawson return_VALUE (AE_ERROR); 28889fb5af8SNate Lawson } 2893184cf5aSNate Lawson 290bbc2815cSJohn Baldwin return_VALUE (AE_OK); 291bbc2815cSJohn Baldwin } 292bbc2815cSJohn Baldwin 293bbc2815cSJohn Baldwin /* 294bbc2815cSJohn Baldwin * Detect ACPI, perform early initialisation 295bbc2815cSJohn Baldwin */ 296bbc2815cSJohn Baldwin static void 297bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent) 298bbc2815cSJohn Baldwin { 299bbc2815cSJohn Baldwin device_t child; 300bbc2815cSJohn Baldwin 301bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 302bbc2815cSJohn Baldwin 303bbc2815cSJohn Baldwin if (!cold) 304bbc2815cSJohn Baldwin return_VOID; 305bbc2815cSJohn Baldwin 306bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 307bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 308bbc2815cSJohn Baldwin return_VOID; 309bbc2815cSJohn Baldwin 310bbc2815cSJohn Baldwin /* Make sure we're not being doubly invoked. */ 311bbc2815cSJohn Baldwin if (device_find_child(parent, "acpi", 0) != NULL) 312bbc2815cSJohn Baldwin return_VOID; 313bbc2815cSJohn Baldwin 314bbc2815cSJohn Baldwin /* Initialize ACPI-CA. */ 315bbc2815cSJohn Baldwin if (ACPI_FAILURE(acpi_Startup())) 316bbc2815cSJohn Baldwin return_VOID; 31715e32d5dSMike Smith 3184e376d58SNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION); 3194e376d58SNate Lawson 320be2b1797SNate Lawson /* Attach the actual ACPI device. */ 32115e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 322bee4aa4aSNate Lawson device_printf(parent, "device_identify failed\n"); 3230ae55423SMike Smith return_VOID; 32415e32d5dSMike Smith } 32515e32d5dSMike Smith } 32615e32d5dSMike Smith 32715e32d5dSMike Smith /* 328bee4aa4aSNate Lawson * Fetch some descriptive data from ACPI to put in our attach message. 32915e32d5dSMike Smith */ 33015e32d5dSMike Smith static int 33115e32d5dSMike Smith acpi_probe(device_t dev) 33215e32d5dSMike Smith { 33315e32d5dSMike Smith ACPI_TABLE_HEADER th; 33415e32d5dSMike Smith char buf[20]; 33515e32d5dSMike Smith int error; 3362ccd1cacSNate Lawson struct sbuf sb; 3372ccd1cacSNate Lawson ACPI_STATUS status; 33815e32d5dSMike Smith 339b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 340f9390180SMitsuru IWASAKI 341f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 342f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 343bee4aa4aSNate Lawson device_printf(dev, "probe failed, other PM system enabled.\n"); 344f9390180SMitsuru IWASAKI return_VALUE (ENXIO); 345f9390180SMitsuru IWASAKI } 346f9390180SMitsuru IWASAKI 347b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 348be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 349be2b1797SNate Lawson AcpiFormatException(status)); 350cb9b0d80SMike Smith error = ENXIO; 351cb9b0d80SMike Smith } else { 3522ccd1cacSNate Lawson sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 3532ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemId, 6); 3542ccd1cacSNate Lawson sbuf_trim(&sb); 3552ccd1cacSNate Lawson sbuf_putc(&sb, ' '); 3562ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemTableId, 8); 3572ccd1cacSNate Lawson sbuf_trim(&sb); 3582ccd1cacSNate Lawson sbuf_finish(&sb); 3592ccd1cacSNate Lawson device_set_desc_copy(dev, sbuf_data(&sb)); 3602ccd1cacSNate Lawson sbuf_delete(&sb); 361cb9b0d80SMike Smith error = 0; 362cb9b0d80SMike Smith } 363bee4aa4aSNate Lawson 364cb9b0d80SMike Smith return_VALUE (error); 36515e32d5dSMike Smith } 36615e32d5dSMike Smith 36715e32d5dSMike Smith static int 36815e32d5dSMike Smith acpi_attach(device_t dev) 36915e32d5dSMike Smith { 37015e32d5dSMike Smith struct acpi_softc *sc; 371cb9b0d80SMike Smith ACPI_STATUS status; 372ea27c63eSNate Lawson int error, state; 37332d18aa5SMike Smith UINT32 flags; 374ea27c63eSNate Lawson UINT8 TypeA, TypeB; 375498d464fSMitsuru IWASAKI char *env; 37615e32d5dSMike Smith 377b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 378bee4aa4aSNate Lawson 37915e32d5dSMike Smith sc = device_get_softc(dev); 38015e32d5dSMike Smith sc->acpi_dev = dev; 38115e32d5dSMike Smith 38291233413SNate Lawson /* Initialize resource manager. */ 38391233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 38491233413SNate Lawson acpi_rman_io.rm_start = 0; 38591233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 38691233413SNate Lawson acpi_rman_io.rm_descr = "I/O ports"; 38791233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 38891233413SNate Lawson panic("acpi rman_init IO ports failed"); 38991233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 39091233413SNate Lawson acpi_rman_mem.rm_start = 0; 39191233413SNate Lawson acpi_rman_mem.rm_end = ~0ul; 39291233413SNate Lawson acpi_rman_mem.rm_descr = "I/O memory addresses"; 39391233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 39491233413SNate Lawson panic("acpi rman_init memory failed"); 39591233413SNate Lawson 396be2b1797SNate Lawson /* Install the default address space handlers. */ 397cb9b0d80SMike Smith error = ENXIO; 398be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 399be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 400be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 401be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 402be2b1797SNate Lawson AcpiFormatException(status)); 403cb9b0d80SMike Smith goto out; 40415e32d5dSMike Smith } 405be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 406be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 407be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 408be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 409be2b1797SNate Lawson AcpiFormatException(status)); 410cb9b0d80SMike Smith goto out; 41115e32d5dSMike Smith } 412be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 413be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 414be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 415be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 416be2b1797SNate Lawson AcpiFormatException(status)); 417cb9b0d80SMike Smith goto out; 41815e32d5dSMike Smith } 41915e32d5dSMike Smith 42015e32d5dSMike Smith /* 421be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 422be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 423be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 424be2b1797SNate Lawson * object init pass. 42515e32d5dSMike Smith * 42632d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 42732d18aa5SMike Smith * 428be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 429be2b1797SNate Lawson * all our child devices, but on many systems it works here. 43015e32d5dSMike Smith */ 43132d18aa5SMike Smith flags = 0; 432d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 43332d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 434bee4aa4aSNate Lawson 435bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */ 436b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 437be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 438be2b1797SNate Lawson AcpiFormatException(status)); 439cb9b0d80SMike Smith goto out; 44015e32d5dSMike Smith } 44115e32d5dSMike Smith 442f8335e3aSNate Lawson /* 443f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 444f8335e3aSNate Lawson * the namespace has been evaluated. 445f8335e3aSNate Lawson */ 446f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 447f8335e3aSNate Lawson 448bee4aa4aSNate Lawson /* Bring device objects and regions online. */ 449b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 450be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 451be2b1797SNate Lawson AcpiFormatException(status)); 452b69ed3f4SMitsuru IWASAKI goto out; 453b69ed3f4SMitsuru IWASAKI } 454b69ed3f4SMitsuru IWASAKI 45515e32d5dSMike Smith /* 4561d073b1dSJohn Baldwin * Setup our sysctl tree. 4571d073b1dSJohn Baldwin * 4581d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4591d073b1dSJohn Baldwin */ 4601d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4611d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4621d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4631d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 4641d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 465d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 466d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 467d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4681d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4691d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4701d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4711d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4721d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4731d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4741d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4751d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 476f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 477f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 478f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 479f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 480f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 481f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4822d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 483ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 484ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 485ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4861611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 4871611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 4881611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4892d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 4902d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 491bf0c18ecSNate Lawson 492bf0c18ecSNate Lawson /* 4932b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 494bf0c18ecSNate Lawson * stabilize. 495bf0c18ecSNate Lawson */ 4962b9609abSNate Lawson sc->acpi_sleep_delay = 1; 4972d644607SMitsuru IWASAKI if (bootverbose) 4982d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 499498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 500498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 501498d464fSMitsuru IWASAKI freeenv(env); 502498d464fSMitsuru IWASAKI } 5031d073b1dSJohn Baldwin 5042d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5052d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 5062d610c46SNate Lawson sc->acpi_s4bios = 1; 5072d610c46SNate Lawson 5081d073b1dSJohn Baldwin /* 509ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 510ea27c63eSNate Lawson * to NONE by default to avoid surprising users. 51115e32d5dSMike Smith */ 512ea27c63eSNate Lawson sc->acpi_power_button_sx = ACPI_STATE_S5; 513ea27c63eSNate Lawson sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 514f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 515f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 51615e32d5dSMike Smith 517ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 518ea27c63eSNate Lawson sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 519ea27c63eSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++) 520ea27c63eSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 521ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 522ea27c63eSNate Lawson break; 523ea27c63eSNate Lawson } 524ea27c63eSNate Lawson 52513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 52615e32d5dSMike Smith 52715e32d5dSMike Smith /* 52815e32d5dSMike Smith * Scan the namespace and attach/initialise children. 52915e32d5dSMike Smith */ 53015e32d5dSMike Smith 53159e472e9SNate Lawson /* Register our shutdown handler. */ 532be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 533be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 53415e32d5dSMike Smith 53515e32d5dSMike Smith /* 53615e32d5dSMike Smith * Register our acpi event handlers. 53715e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 53815e32d5dSMike Smith */ 539be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 540be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 541be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 542be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 54315e32d5dSMike Smith 544be2b1797SNate Lawson /* Flag our initial states. */ 54515e32d5dSMike Smith sc->acpi_enabled = 1; 54615e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 547ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 54815e32d5dSMike Smith 549be2b1797SNate Lawson /* Create the control device */ 550a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 5512a4689e8SRobert Watson "acpi"); 55215e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 55315e32d5dSMike Smith 554be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 555c573e654SMitsuru IWASAKI goto out; 556c573e654SMitsuru IWASAKI 557be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 558f86214b6SMitsuru IWASAKI goto out; 559f86214b6SMitsuru IWASAKI 560f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 561f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 562f9390180SMitsuru IWASAKI 563eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 564eeb6dba2SJohn Baldwin acpi_probe_children(dev); 565eeb6dba2SJohn Baldwin 566cb9b0d80SMike Smith error = 0; 567cb9b0d80SMike Smith 568cb9b0d80SMike Smith out: 569cb9b0d80SMike Smith return_VALUE (error); 57015e32d5dSMike Smith } 57115e32d5dSMike Smith 572169b539aSNate Lawson static int 573169b539aSNate Lawson acpi_shutdown(device_t dev) 574169b539aSNate Lawson { 575169b539aSNate Lawson 5760e414868SNate Lawson /* Allow children to shutdown first. */ 5770e414868SNate Lawson bus_generic_shutdown(dev); 5780e414868SNate Lawson 579bee4aa4aSNate Lawson /* 580bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC). 581bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most). 582bee4aa4aSNate Lawson */ 583d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 584d4b9ff91SNate Lawson 585169b539aSNate Lawson return (0); 586169b539aSNate Lawson } 587169b539aSNate Lawson 58815e32d5dSMike Smith /* 58915e32d5dSMike Smith * Handle a new device being added 59015e32d5dSMike Smith */ 59115e32d5dSMike Smith static device_t 59215e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 59315e32d5dSMike Smith { 59415e32d5dSMike Smith struct acpi_device *ad; 59515e32d5dSMike Smith device_t child; 59615e32d5dSMike Smith 597be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 59815e32d5dSMike Smith return (NULL); 59915e32d5dSMike Smith 60015e32d5dSMike Smith resource_list_init(&ad->ad_rl); 60115e32d5dSMike Smith 60215e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 60315e32d5dSMike Smith if (child != NULL) 60415e32d5dSMike Smith device_set_ivars(child, ad); 60515e32d5dSMike Smith return (child); 60615e32d5dSMike Smith } 60715e32d5dSMike Smith 60815e32d5dSMike Smith static int 60915e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 61015e32d5dSMike Smith { 61115e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 61215e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 61315e32d5dSMike Smith int retval = 0; 61415e32d5dSMike Smith 61515e32d5dSMike Smith retval += bus_print_child_header(bus, child); 6169ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 6179ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 6189ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 6199ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 62015e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 62115e32d5dSMike Smith 62215e32d5dSMike Smith return (retval); 62315e32d5dSMike Smith } 62415e32d5dSMike Smith 62563600cc3SNate Lawson /* Location hint for devctl(8) */ 62663600cc3SNate Lawson static int 627247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 628247648afSTakanori Watanabe size_t buflen) 629247648afSTakanori Watanabe { 630247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 631247648afSTakanori Watanabe 632247648afSTakanori Watanabe if (dinfo->ad_handle) 633d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 634247648afSTakanori Watanabe else 635d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 636247648afSTakanori Watanabe return (0); 637247648afSTakanori Watanabe } 638247648afSTakanori Watanabe 63963600cc3SNate Lawson /* PnP information for devctl(8) */ 64063600cc3SNate Lawson static int 641247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 642247648afSTakanori Watanabe size_t buflen) 643247648afSTakanori Watanabe { 644247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 64563600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 64663600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 647247648afSTakanori Watanabe char *end; 648247648afSTakanori Watanabe int error; 649247648afSTakanori Watanabe 650247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 651247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 652247648afSTakanori Watanabe if (error) 653d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 654247648afSTakanori Watanabe else 655247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 656247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 657d40c0f53SNate Lawson adinfo->HardwareId.Value : "none", 65863600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 65963600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 660247648afSTakanori Watanabe if (adinfo) 661247648afSTakanori Watanabe AcpiOsFree(adinfo); 662247648afSTakanori Watanabe 663247648afSTakanori Watanabe return (0); 664247648afSTakanori Watanabe } 665247648afSTakanori Watanabe 666247648afSTakanori Watanabe /* 66715e32d5dSMike Smith * Handle per-device ivars 66815e32d5dSMike Smith */ 66915e32d5dSMike Smith static int 67015e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 67115e32d5dSMike Smith { 67215e32d5dSMike Smith struct acpi_device *ad; 67315e32d5dSMike Smith 67415e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 67515e32d5dSMike Smith printf("device has no ivars\n"); 67615e32d5dSMike Smith return (ENOENT); 67715e32d5dSMike Smith } 67815e32d5dSMike Smith 679be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 68015e32d5dSMike Smith switch(index) { 68115e32d5dSMike Smith case ACPI_IVAR_HANDLE: 68215e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 68315e32d5dSMike Smith break; 68415e32d5dSMike Smith case ACPI_IVAR_MAGIC: 68515e32d5dSMike Smith *(int *)result = ad->ad_magic; 68615e32d5dSMike Smith break; 68715e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 68815e32d5dSMike Smith *(void **)result = ad->ad_private; 68915e32d5dSMike Smith break; 690d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 691d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 692d4b9ff91SNate Lawson break; 69332d18aa5SMike Smith case ISA_IVAR_VENDORID: 69432d18aa5SMike Smith case ISA_IVAR_SERIAL: 69532d18aa5SMike Smith case ISA_IVAR_COMPATID: 69632d18aa5SMike Smith *(int *)result = -1; 69732d18aa5SMike Smith break; 69832d18aa5SMike Smith case ISA_IVAR_LOGICALID: 69932d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 70032d18aa5SMike Smith break; 70115e32d5dSMike Smith default: 70215e32d5dSMike Smith return (ENOENT); 70315e32d5dSMike Smith } 704be2b1797SNate Lawson 70515e32d5dSMike Smith return (0); 70615e32d5dSMike Smith } 70715e32d5dSMike Smith 70815e32d5dSMike Smith static int 70915e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 71015e32d5dSMike Smith { 71115e32d5dSMike Smith struct acpi_device *ad; 71215e32d5dSMike Smith 71315e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 71415e32d5dSMike Smith printf("device has no ivars\n"); 71515e32d5dSMike Smith return (ENOENT); 71615e32d5dSMike Smith } 71715e32d5dSMike Smith 71815e32d5dSMike Smith switch(index) { 71915e32d5dSMike Smith case ACPI_IVAR_HANDLE: 72015e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 72115e32d5dSMike Smith break; 72215e32d5dSMike Smith case ACPI_IVAR_MAGIC: 72315e32d5dSMike Smith ad->ad_magic = (int)value; 72415e32d5dSMike Smith break; 72515e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 72615e32d5dSMike Smith ad->ad_private = (void *)value; 72715e32d5dSMike Smith break; 728d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 729d4b9ff91SNate Lawson ad->ad_flags = (int)value; 730d4b9ff91SNate Lawson break; 73115e32d5dSMike Smith default: 7322ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 73315e32d5dSMike Smith return (ENOENT); 73415e32d5dSMike Smith } 735be2b1797SNate Lawson 73615e32d5dSMike Smith return (0); 73715e32d5dSMike Smith } 73815e32d5dSMike Smith 73915e32d5dSMike Smith /* 74015e32d5dSMike Smith * Handle child resource allocation/removal 74115e32d5dSMike Smith */ 74291233413SNate Lawson static struct resource_list * 74391233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 74415e32d5dSMike Smith { 74591233413SNate Lawson struct acpi_device *ad; 74615e32d5dSMike Smith 74791233413SNate Lawson ad = device_get_ivars(child); 74891233413SNate Lawson return (&ad->ad_rl); 74915e32d5dSMike Smith } 75015e32d5dSMike Smith 751adad4744SNate Lawson /* 752adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 753adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 754adad4744SNate Lawson */ 755adad4744SNate Lawson static int 756adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 757adad4744SNate Lawson { 758adad4744SNate Lawson struct resource *res; 759adad4744SNate Lawson struct resource_list *rl; 760adad4744SNate Lawson struct resource_list_entry *rle; 761adad4744SNate Lawson struct rman *rm; 762adad4744SNate Lawson 763adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 764adad4744SNate Lawson SLIST_FOREACH(rle, rl, link) { 765adad4744SNate Lawson if (rle->res != NULL) { 766adad4744SNate Lawson device_printf(dev, "duplicate resource for %lx\n", rle->start); 767adad4744SNate Lawson continue; 768adad4744SNate Lawson } 769adad4744SNate Lawson 770adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 771adad4744SNate Lawson switch (rle->type) { 772adad4744SNate Lawson case SYS_RES_IOPORT: 773adad4744SNate Lawson rm = &acpi_rman_io; 774adad4744SNate Lawson break; 775adad4744SNate Lawson case SYS_RES_MEMORY: 776adad4744SNate Lawson rm = &acpi_rman_mem; 777adad4744SNate Lawson break; 778adad4744SNate Lawson default: 779adad4744SNate Lawson continue; 780adad4744SNate Lawson } 781adad4744SNate Lawson 782adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 783adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 784adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 785adad4744SNate Lawson if (res != NULL) { 786adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 787adad4744SNate Lawson rle->res = res; 788adad4744SNate Lawson } else 789adad4744SNate Lawson device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 790adad4744SNate Lawson rle->start, rle->count, rle->type); 791adad4744SNate Lawson } 792adad4744SNate Lawson return (0); 793adad4744SNate Lawson } 794adad4744SNate Lawson 795adad4744SNate Lawson /* Find if we manage a given resource. */ 796adad4744SNate Lawson static struct resource_list_entry * 797adad4744SNate Lawson acpi_sysres_find(device_t dev, int type, u_long addr) 798adad4744SNate Lawson { 799adad4744SNate Lawson struct resource_list *rl; 800adad4744SNate Lawson struct resource_list_entry *rle; 801adad4744SNate Lawson 802adad4744SNate Lawson ACPI_SERIAL_ASSERT(acpi); 803adad4744SNate Lawson 804adad4744SNate Lawson /* We only consider IO and memory resources for our pool. */ 805adad4744SNate Lawson rle = NULL; 806adad4744SNate Lawson if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY) 807adad4744SNate Lawson goto out; 808adad4744SNate Lawson 809adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 810adad4744SNate Lawson SLIST_FOREACH(rle, rl, link) { 811adad4744SNate Lawson if (type == rle->type && addr >= rle->start && 812adad4744SNate Lawson addr < rle->start + rle->count) 813adad4744SNate Lawson break; 814adad4744SNate Lawson } 815adad4744SNate Lawson 816adad4744SNate Lawson out: 817adad4744SNate Lawson return (rle); 818adad4744SNate Lawson } 819adad4744SNate Lawson 82015e32d5dSMike Smith static struct resource * 82115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 82215e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 82315e32d5dSMike Smith { 82495957f62SJohn Baldwin ACPI_RESOURCE ares; 82515e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 82615e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 82791233413SNate Lawson struct resource_list_entry *rle; 82891233413SNate Lawson struct resource *res; 82991233413SNate Lawson struct rman *rm; 83015e32d5dSMike Smith 83115e2f34fSNate Lawson res = NULL; 83215e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 83315e2f34fSNate Lawson 83491233413SNate Lawson /* 83591233413SNate Lawson * If this is an allocation of the "default" range for a given RID, and 83691233413SNate Lawson * we know what the resources for this device are (i.e., they're on the 83791233413SNate Lawson * child's resource list), use those start/end values. 83891233413SNate Lawson */ 83991233413SNate Lawson if (start == 0UL && end == ~0UL) { 84091233413SNate Lawson rle = resource_list_find(rl, type, *rid); 84191233413SNate Lawson if (rle == NULL) 84215e2f34fSNate Lawson goto out; 84391233413SNate Lawson start = rle->start; 84491233413SNate Lawson end = rle->end; 84591233413SNate Lawson count = rle->count; 84691233413SNate Lawson } 84791233413SNate Lawson 84891233413SNate Lawson /* If we don't manage this address, pass the request up to the parent. */ 849adad4744SNate Lawson rle = acpi_sysres_find(bus, type, start); 85091233413SNate Lawson if (rle == NULL) { 85195957f62SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 85295957f62SJohn Baldwin start, end, count, flags); 85395957f62SJohn Baldwin } else { 85491233413SNate Lawson 85591233413SNate Lawson /* We only handle memory and IO resources through rman. */ 85691233413SNate Lawson switch (type) { 85791233413SNate Lawson case SYS_RES_IOPORT: 85891233413SNate Lawson rm = &acpi_rman_io; 85991233413SNate Lawson break; 86091233413SNate Lawson case SYS_RES_MEMORY: 86191233413SNate Lawson rm = &acpi_rman_mem; 86291233413SNate Lawson break; 86391233413SNate Lawson default: 86491233413SNate Lawson panic("acpi_alloc_resource: invalid res type %d", type); 86591233413SNate Lawson } 86691233413SNate Lawson 86791233413SNate Lawson /* If we do know it, allocate it from the local pool. */ 86895957f62SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 86995957f62SJohn Baldwin child); 87091233413SNate Lawson if (res == NULL) 87115e2f34fSNate Lawson goto out; 87291233413SNate Lawson 873a3236570SWarner Losh /* Copy the bus tag and handle from the pre-allocated resource. */ 87491233413SNate Lawson rman_set_bustag(res, rman_get_bustag(rle->res)); 875ed67d9deSWarner Losh rman_set_bushandle(res, rman_get_start(res)); 87691233413SNate Lawson 87791233413SNate Lawson /* If requested, activate the resource using the parent's method. */ 87895957f62SJohn Baldwin if (flags & RF_ACTIVE) 87991233413SNate Lawson if (bus_activate_resource(child, type, *rid, res) != 0) { 88091233413SNate Lawson rman_release_resource(res); 88115e2f34fSNate Lawson res = NULL; 88215e2f34fSNate Lawson goto out; 88391233413SNate Lawson } 88495957f62SJohn Baldwin } 88591233413SNate Lawson 88695957f62SJohn Baldwin if (res != NULL && device_get_parent(child) == bus) 88795957f62SJohn Baldwin switch (type) { 88895957f62SJohn Baldwin case SYS_RES_IRQ: 88995957f62SJohn Baldwin /* 89095957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 89195957f62SJohn Baldwin * configure the interrupt associated with a device when we 89295957f62SJohn Baldwin * parse the resources but have to defer it until a driver 89395957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 89495957f62SJohn Baldwin * 89595957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 89695957f62SJohn Baldwin */ 89795957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 89895957f62SJohn Baldwin acpi_config_intr(child, &ares); 89995957f62SJohn Baldwin break; 90095957f62SJohn Baldwin } 90115e2f34fSNate Lawson 90215e2f34fSNate Lawson out: 90315e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 90491233413SNate Lawson return (res); 90515e32d5dSMike Smith } 90615e32d5dSMike Smith 90715e32d5dSMike Smith static int 90891233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 90991233413SNate Lawson struct resource *r) 91015e32d5dSMike Smith { 91191233413SNate Lawson int ret; 91215e32d5dSMike Smith 91315e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 91415e2f34fSNate Lawson 91591233413SNate Lawson /* 91691233413SNate Lawson * If we know about this address, deactivate it and release it to the 91791233413SNate Lawson * local pool. If we don't, pass this request up to the parent. 91891233413SNate Lawson */ 919adad4744SNate Lawson if (acpi_sysres_find(bus, type, rman_get_start(r)) == NULL) { 92091233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 92191233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 92291233413SNate Lawson if (ret != 0) 92315e2f34fSNate Lawson goto out; 92491233413SNate Lawson } 92591233413SNate Lawson ret = rman_release_resource(r); 92691233413SNate Lawson } else 92791233413SNate Lawson ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 92891233413SNate Lawson 92915e2f34fSNate Lawson out: 93015e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 93191233413SNate Lawson return (ret); 93215e32d5dSMike Smith } 93315e32d5dSMike Smith 934dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 935dc750869SNate Lawson struct resource * 936dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 937dc750869SNate Lawson { 938dc750869SNate Lawson int type; 939dc750869SNate Lawson 940dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 941dc750869SNate Lawson gas->RegisterBitWidth < 8) 942dc750869SNate Lawson return (NULL); 943dc750869SNate Lawson 944dc750869SNate Lawson switch (gas->AddressSpaceId) { 945dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 946dc750869SNate Lawson type = SYS_RES_MEMORY; 947dc750869SNate Lawson break; 948dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 949dc750869SNate Lawson type = SYS_RES_IOPORT; 950dc750869SNate Lawson break; 951dc750869SNate Lawson default: 952dc750869SNate Lawson return (NULL); 953dc750869SNate Lawson } 954dc750869SNate Lawson 955dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 9565f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 957dc750869SNate Lawson } 958dc750869SNate Lawson 959c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 9601e4925e8SNate Lawson static uint32_t 96132d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 962bc0f2195SMike Smith { 9631e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9641e4925e8SNate Lawson ACPI_BUFFER buf; 965bc0f2195SMike Smith ACPI_HANDLE h; 966bc0f2195SMike Smith ACPI_STATUS error; 967bc0f2195SMike Smith u_int32_t pnpid; 96832d18aa5SMike Smith 969b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 97032d18aa5SMike Smith 97132d18aa5SMike Smith pnpid = 0; 972bd189fe7SNate Lawson buf.Pointer = NULL; 973bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 974bd189fe7SNate Lawson 9756fca9360SNate Lawson /* Fetch and validate the HID. */ 97632d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 977bd189fe7SNate Lawson goto out; 9786fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9796fca9360SNate Lawson if (ACPI_FAILURE(error)) 980bd189fe7SNate Lawson goto out; 9811e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 98232d18aa5SMike Smith 9831e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 9841e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 985be2b1797SNate Lawson 986bd189fe7SNate Lawson out: 987bd189fe7SNate Lawson if (buf.Pointer != NULL) 9881e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 98932d18aa5SMike Smith return_VALUE (pnpid); 99032d18aa5SMike Smith } 99132d18aa5SMike Smith 9921e4925e8SNate Lawson static int 9931e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 994b2566207STakanori Watanabe { 9951e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9961e4925e8SNate Lawson ACPI_BUFFER buf; 997b2566207STakanori Watanabe ACPI_HANDLE h; 998b2566207STakanori Watanabe ACPI_STATUS error; 9991e4925e8SNate Lawson uint32_t *pnpid; 10001e4925e8SNate Lawson int valid, i; 1001b2566207STakanori Watanabe 1002b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1003b2566207STakanori Watanabe 10041e4925e8SNate Lawson pnpid = cids; 10051e4925e8SNate Lawson valid = 0; 1006bd189fe7SNate Lawson buf.Pointer = NULL; 1007bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1008bd189fe7SNate Lawson 10091e4925e8SNate Lawson /* Fetch and validate the CID */ 1010b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 1011bd189fe7SNate Lawson goto out; 10121e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10131e4925e8SNate Lawson if (ACPI_FAILURE(error)) 1014bd189fe7SNate Lawson goto out; 10151e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 10161e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 1017b2566207STakanori Watanabe goto out; 1018b2566207STakanori Watanabe 10191e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 10201e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 10211e4925e8SNate Lawson for (i = 0; i < count; i++) { 10221e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 10231e4925e8SNate Lawson continue; 10241e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 10251e4925e8SNate Lawson valid++; 1026b2566207STakanori Watanabe } 1027b2566207STakanori Watanabe 10281e4925e8SNate Lawson out: 1029bd189fe7SNate Lawson if (buf.Pointer != NULL) 10301e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 10311e4925e8SNate Lawson return_VALUE (valid); 10321e4925e8SNate Lawson } 1033b2566207STakanori Watanabe 1034ce619b2eSNate Lawson static char * 1035ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1036ce619b2eSNate Lawson { 1037ce619b2eSNate Lawson ACPI_HANDLE h; 1038ce619b2eSNate Lawson int i; 1039ce619b2eSNate Lawson 1040ce619b2eSNate Lawson h = acpi_get_handle(dev); 1041ce619b2eSNate Lawson if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1042ce619b2eSNate Lawson return (NULL); 1043ce619b2eSNate Lawson 1044ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1045ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1046ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1047ce619b2eSNate Lawson return (ids[i]); 1048ce619b2eSNate Lawson } 1049ce619b2eSNate Lawson return (NULL); 1050ce619b2eSNate Lawson } 1051ce619b2eSNate Lawson 1052ce619b2eSNate Lawson static ACPI_STATUS 1053ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1054ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1055ce619b2eSNate Lawson { 1056ce619b2eSNate Lawson ACPI_HANDLE h; 1057ce619b2eSNate Lawson 1058df8d2a32SNate Lawson if (dev == NULL) 1059df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1060df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1061ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1062ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1063ce619b2eSNate Lawson } 1064ce619b2eSNate Lawson 1065df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1066df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1067df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1068df8d2a32SNate Lawson void *arg; 1069df8d2a32SNate Lawson ACPI_HANDLE parent; 1070df8d2a32SNate Lawson }; 1071df8d2a32SNate Lawson 1072ce619b2eSNate Lawson static ACPI_STATUS 1073df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1074df8d2a32SNate Lawson { 1075df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1076df8d2a32SNate Lawson device_t dev, old_dev; 1077df8d2a32SNate Lawson ACPI_STATUS status; 1078df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1079df8d2a32SNate Lawson 1080df8d2a32SNate Lawson /* 1081df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1082df8d2a32SNate Lawson * the parent where the scan began. 1083df8d2a32SNate Lawson */ 1084df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1085df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1086df8d2a32SNate Lawson return (AE_OK); 1087df8d2a32SNate Lawson 1088df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1089df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1090df8d2a32SNate Lawson return (AE_OK); 1091df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1092df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1093df8d2a32SNate Lawson return (AE_OK); 1094df8d2a32SNate Lawson 1095df8d2a32SNate Lawson /* 1096df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1097df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1098df8d2a32SNate Lawson */ 1099df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1100df8d2a32SNate Lawson dev = old_dev; 1101df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1102df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1103df8d2a32SNate Lawson return (status); 1104df8d2a32SNate Lawson 1105df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1106df8d2a32SNate Lawson if (old_dev != NULL) { 1107df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1108df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1109df8d2a32SNate Lawson } 1110df8d2a32SNate Lawson 1111df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1112df8d2a32SNate Lawson if (dev != NULL) 1113df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1114df8d2a32SNate Lawson 1115df8d2a32SNate Lawson return (AE_OK); 1116df8d2a32SNate Lawson } 1117df8d2a32SNate Lawson 1118df8d2a32SNate Lawson static ACPI_STATUS 1119df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1120df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1121ce619b2eSNate Lawson { 1122ce619b2eSNate Lawson ACPI_HANDLE h; 1123df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1124ce619b2eSNate Lawson 1125df8d2a32SNate Lawson if (acpi_disabled("children")) 1126df8d2a32SNate Lawson return (AE_OK); 1127df8d2a32SNate Lawson 1128df8d2a32SNate Lawson if (dev == NULL) 1129df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1130df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1131ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1132df8d2a32SNate Lawson ctx.user_fn = user_fn; 1133df8d2a32SNate Lawson ctx.arg = arg; 1134df8d2a32SNate Lawson ctx.parent = h; 1135df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 1136df8d2a32SNate Lawson acpi_device_scan_cb, &ctx, NULL)); 1137ce619b2eSNate Lawson } 1138ce619b2eSNate Lawson 113932d18aa5SMike Smith static int 114032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 114132d18aa5SMike Smith { 11421e4925e8SNate Lawson int result, cid_count, i; 11431e4925e8SNate Lawson uint32_t lid, cids[8]; 1144bc0f2195SMike Smith 1145b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1146bc0f2195SMike Smith 1147bc0f2195SMike Smith /* 1148bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1149bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1150bc0f2195SMike Smith * that to happen, so don't ever return it. 1151bc0f2195SMike Smith */ 1152bc0f2195SMike Smith result = ENXIO; 1153bc0f2195SMike Smith 1154be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1155b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 11561e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1157bc0f2195SMike Smith while (ids && ids->ip_id) { 11581e4925e8SNate Lawson if (lid == ids->ip_id) { 1159bc0f2195SMike Smith result = 0; 1160bc0f2195SMike Smith goto out; 1161bc0f2195SMike Smith } 11621e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 11631e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 11641e4925e8SNate Lawson result = 0; 11651e4925e8SNate Lawson goto out; 11661e4925e8SNate Lawson } 11671e4925e8SNate Lawson } 1168bc0f2195SMike Smith ids++; 1169bc0f2195SMike Smith } 1170be2b1797SNate Lawson 1171bc0f2195SMike Smith out: 1172bc0f2195SMike Smith return_VALUE (result); 1173bc0f2195SMike Smith } 1174bc0f2195SMike Smith 1175bc0f2195SMike Smith /* 117615e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 117715e32d5dSMike Smith * 1178be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1179be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 118015e32d5dSMike Smith */ 118115e32d5dSMike Smith static void 118215e32d5dSMike Smith acpi_probe_children(device_t bus) 118315e32d5dSMike Smith { 118415e32d5dSMike Smith ACPI_HANDLE parent; 1185be2b1797SNate Lawson ACPI_STATUS status; 118615e32d5dSMike Smith int i; 118791233413SNate Lawson static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 118815e32d5dSMike Smith 1189b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 11900ae55423SMike Smith 119115e32d5dSMike Smith /* 119215e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1193edc13633SNate Lawson * we find. We also probe/attach any early devices. 119415e32d5dSMike Smith * 119515e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1196be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1197be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1198be2b1797SNate Lawson * devices as they appear, which might be smarter.) 119915e32d5dSMike Smith */ 12004c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1201be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1202be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1203be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1204be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1205be2b1797SNate Lawson bus, NULL); 1206be2b1797SNate Lawson } 1207be2b1797SNate Lawson } 120815e32d5dSMike Smith 1209adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1210adad4744SNate Lawson acpi_sysres_alloc(bus); 1211adad4744SNate Lawson 1212edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1213edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1214edc13633SNate Lawson bus_generic_probe(bus); 1215edc13633SNate Lawson 1216edc13633SNate Lawson /* Probe/attach all children, created staticly and from the namespace. */ 12174c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 121815e32d5dSMike Smith bus_generic_attach(bus); 121915e32d5dSMike Smith 122015e32d5dSMike Smith /* 122115e32d5dSMike Smith * Some of these children may have attached others as part of their attach 122215e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 122315e32d5dSMike Smith */ 12244c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 122515e32d5dSMike Smith bus_generic_attach(bus); 12260ae55423SMike Smith 122788a79fc0SNate Lawson /* Attach wake sysctls. */ 12285c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 122988a79fc0SNate Lawson 1230bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 12310ae55423SMike Smith return_VOID; 123215e32d5dSMike Smith } 123315e32d5dSMike Smith 1234b82ca9a9SNate Lawson /* 1235b82ca9a9SNate Lawson * Determine the probe order for a given device and return non-zero if it 1236b82ca9a9SNate Lawson * should be attached immediately. 1237b82ca9a9SNate Lawson */ 123891233413SNate Lawson static int 1239b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 124091233413SNate Lawson { 124191233413SNate Lawson int ret; 124291233413SNate Lawson 1243b82ca9a9SNate Lawson /* 1244b82ca9a9SNate Lawson * 1. I/O port and memory system resource holders 1245b82ca9a9SNate Lawson * 2. Embedded controllers (to handle early accesses) 1246b82ca9a9SNate Lawson */ 124791233413SNate Lawson ret = 0; 124891233413SNate Lawson if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) { 124991233413SNate Lawson *order = 1; 125091233413SNate Lawson ret = 1; 1251b82ca9a9SNate Lawson } else if (acpi_MatchHid(handle, "PNP0C09")) { 125291233413SNate Lawson *order = 2; 125391233413SNate Lawson ret = 1; 125491233413SNate Lawson } 125591233413SNate Lawson 125691233413SNate Lawson return (ret); 125791233413SNate Lawson } 125891233413SNate Lawson 125915e32d5dSMike Smith /* 126015e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 126115e32d5dSMike Smith * it. 126215e32d5dSMike Smith */ 126315e32d5dSMike Smith static ACPI_STATUS 126415e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 126515e32d5dSMike Smith { 126615e32d5dSMike Smith ACPI_OBJECT_TYPE type; 126791233413SNate Lawson device_t child, bus; 126891233413SNate Lawson int order, probe_now; 126915e32d5dSMike Smith 1270b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 12710ae55423SMike Smith 1272be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 12730ae55423SMike Smith if (acpi_avoid(handle)) 12740ae55423SMike Smith return_ACPI_STATUS (AE_OK); 12750ae55423SMike Smith 127691233413SNate Lawson bus = (device_t)context; 1277b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 127815e32d5dSMike Smith switch (type) { 127915e32d5dSMike Smith case ACPI_TYPE_DEVICE: 128015e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 128115e32d5dSMike Smith case ACPI_TYPE_THERMAL: 128215e32d5dSMike Smith case ACPI_TYPE_POWER: 12830ae55423SMike Smith if (acpi_disabled("children")) 12840ae55423SMike Smith break; 1285be2b1797SNate Lawson 128615e32d5dSMike Smith /* 128715e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 1288b82ca9a9SNate Lawson * so that the probe/attach passes will run breadth-first. Orders 1289b82ca9a9SNate Lawson * less than 10 are reserved for special objects (i.e., system 1290b82ca9a9SNate Lawson * resources). Larger values are used for all other devices. 129115e32d5dSMike Smith */ 1292be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1293be2b1797SNate Lawson acpi_name(handle))); 1294b82ca9a9SNate Lawson order = (level + 1) * 10; 1295b82ca9a9SNate Lawson probe_now = acpi_probe_order(handle, &order); 129691233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1297bc0f2195SMike Smith if (child == NULL) 1298bc0f2195SMike Smith break; 129959a890e6SNate Lawson 130059a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 130115e32d5dSMike Smith acpi_set_handle(child, handle); 130259a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1303bc0f2195SMike Smith 1304bc0f2195SMike Smith /* 1305b519f9d6SMike Smith * Check that the device is present. If it's not present, 1306b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1307b519f9d6SMike Smith * the handle, but we don't probe it). 1308b519f9d6SMike Smith */ 1309be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1310b519f9d6SMike Smith device_disable(child); 1311b519f9d6SMike Smith break; 1312b519f9d6SMike Smith } 1313b519f9d6SMike Smith 1314b519f9d6SMike Smith /* 1315bc0f2195SMike Smith * Get the device's resource settings and attach them. 1316bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1317bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1318bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1319bc0f2195SMike Smith * device not to have any resources. 1320bc0f2195SMike Smith */ 132172ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1322bc0f2195SMike Smith 1323f504b607SNate Lawson /* If order was overridden, probe/attach now rather than later. */ 132491233413SNate Lawson if (probe_now) 132591233413SNate Lawson device_probe_and_attach(child); 13260ae55423SMike Smith break; 132715e32d5dSMike Smith } 132815e32d5dSMike Smith } 1329be2b1797SNate Lawson 13300ae55423SMike Smith return_ACPI_STATUS (AE_OK); 133115e32d5dSMike Smith } 133215e32d5dSMike Smith 133359a890e6SNate Lawson /* 133459a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 133559a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 133659a890e6SNate Lawson */ 133759a890e6SNate Lawson void 133859a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 133959a890e6SNate Lawson { 134059a890e6SNate Lawson } 134159a890e6SNate Lawson 134215e32d5dSMike Smith static void 134315e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 134415e32d5dSMike Smith { 1345d33f4987STakanori Watanabe ACPI_STATUS status; 1346eeb3a05fSNate Lawson 1347eeb3a05fSNate Lawson /* 134880f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 134980f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 135080f0e4c2SNate Lawson * an AP. 1351eeb3a05fSNate Lawson */ 1352eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1353904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1354904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1355904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1356904bf0c2SNate Lawson AcpiFormatException(status)); 1357904bf0c2SNate Lawson return; 1358904bf0c2SNate Lawson } 1359eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 136055398047SNate Lawson ACPI_DISABLE_IRQS(); 1361865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1362be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1363bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 136415e32d5dSMike Smith } else { 136515e32d5dSMike Smith DELAY(1000000); 136615e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 136715e32d5dSMike Smith } 136880f0e4c2SNate Lawson } else { 136980f0e4c2SNate Lawson printf("Shutting down ACPI\n"); 137080f0e4c2SNate Lawson AcpiTerminate(); 137180f0e4c2SNate Lawson } 137215e32d5dSMike Smith } 137315e32d5dSMike Smith 137413d4f7baSMitsuru IWASAKI static void 137513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 137613d4f7baSMitsuru IWASAKI { 137713d4f7baSMitsuru IWASAKI static int first_time = 1; 137813d4f7baSMitsuru IWASAKI 137913d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1380be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 138159ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 138213d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 138333febf93SNate Lawson acpi_event_power_button_sleep, sc); 1384be2b1797SNate Lawson if (first_time) 13856c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 138613d4f7baSMitsuru IWASAKI } 1387be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 138859ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 138913d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 139033febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1391be2b1797SNate Lawson if (first_time) 13926c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 139313d4f7baSMitsuru IWASAKI } 139413d4f7baSMitsuru IWASAKI 139513d4f7baSMitsuru IWASAKI first_time = 0; 139613d4f7baSMitsuru IWASAKI } 139713d4f7baSMitsuru IWASAKI 139815e32d5dSMike Smith /* 1399c5ba0be4SMike Smith * Returns true if the device is actually present and should 1400c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1401c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1402c5ba0be4SMike Smith */ 1403c5ba0be4SMike Smith BOOLEAN 1404c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1405c5ba0be4SMike Smith { 14061e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1407c5ba0be4SMike Smith ACPI_HANDLE h; 14081e4925e8SNate Lawson ACPI_BUFFER buf; 1409c5ba0be4SMike Smith ACPI_STATUS error; 14101e4925e8SNate Lawson int ret; 1411c5ba0be4SMike Smith 14121e4925e8SNate Lawson ret = FALSE; 1413c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1414c5ba0be4SMike Smith return (FALSE); 14151e4925e8SNate Lawson buf.Pointer = NULL; 14161e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14176fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14186fca9360SNate Lawson if (ACPI_FAILURE(error)) 1419c5ba0be4SMike Smith return (FALSE); 14201e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1421be2b1797SNate Lawson 1422be2b1797SNate Lawson /* If no _STA method, must be present */ 14231e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 14241e4925e8SNate Lawson ret = TRUE; 1425be2b1797SNate Lawson 1426be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 142717dbe0f7SNate Lawson if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus)) 14281e4925e8SNate Lawson ret = TRUE; 1429be2b1797SNate Lawson 14301e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14311e4925e8SNate Lawson return (ret); 1432c5ba0be4SMike Smith } 1433c5ba0be4SMike Smith 1434c5ba0be4SMike Smith /* 1435b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1436b53f2771SMike Smith */ 1437b53f2771SMike Smith BOOLEAN 1438b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1439b53f2771SMike Smith { 14401e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1441b53f2771SMike Smith ACPI_HANDLE h; 14421e4925e8SNate Lawson ACPI_BUFFER buf; 1443b53f2771SMike Smith ACPI_STATUS error; 14441e4925e8SNate Lawson int ret; 1445b53f2771SMike Smith 14461e4925e8SNate Lawson ret = FALSE; 1447b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1448b53f2771SMike Smith return (FALSE); 14491e4925e8SNate Lawson buf.Pointer = NULL; 14501e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14516fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14526fca9360SNate Lawson if (ACPI_FAILURE(error)) 1453b53f2771SMike Smith return (FALSE); 14541e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1455be2b1797SNate Lawson 1456be2b1797SNate Lawson /* If no _STA method, must be present */ 14571e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 14581e4925e8SNate Lawson ret = TRUE; 1459be2b1797SNate Lawson 146017dbe0f7SNate Lawson /* Return true for 'present', 'battery present', and 'functioning' */ 146117dbe0f7SNate Lawson if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus)) 14621e4925e8SNate Lawson ret = TRUE; 1463be2b1797SNate Lawson 14641e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14651e4925e8SNate Lawson return (ret); 1466b53f2771SMike Smith } 1467b53f2771SMike Smith 1468b53f2771SMike Smith /* 146991233413SNate Lawson * Match a HID string against a handle 147015e32d5dSMike Smith */ 1471ce619b2eSNate Lawson static BOOLEAN 1472ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 147315e32d5dSMike Smith { 14741e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 14751e4925e8SNate Lawson ACPI_BUFFER buf; 147615e32d5dSMike Smith ACPI_STATUS error; 14771e4925e8SNate Lawson int ret, i; 147815e32d5dSMike Smith 14791e4925e8SNate Lawson ret = FALSE; 148091233413SNate Lawson if (hid == NULL || h == NULL) 148191233413SNate Lawson return (ret); 14821e4925e8SNate Lawson buf.Pointer = NULL; 14831e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14846fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14856fca9360SNate Lawson if (ACPI_FAILURE(error)) 148691233413SNate Lawson return (ret); 14871e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1488be2b1797SNate Lawson 1489c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1490c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 14911e4925e8SNate Lawson ret = TRUE; 1492c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 14931e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 14941e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 14951e4925e8SNate Lawson ret = TRUE; 14961e4925e8SNate Lawson break; 14971e4925e8SNate Lawson } 14981e4925e8SNate Lawson } 14991e4925e8SNate Lawson } 1500be2b1797SNate Lawson 15011e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 15021e4925e8SNate Lawson return (ret); 150315e32d5dSMike Smith } 150415e32d5dSMike Smith 150515e32d5dSMike Smith /* 1506c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1507c5ba0be4SMike Smith * or one if its parents. 1508c5ba0be4SMike Smith */ 1509c5ba0be4SMike Smith ACPI_STATUS 1510c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1511c5ba0be4SMike Smith { 1512c5ba0be4SMike Smith ACPI_HANDLE r; 1513c5ba0be4SMike Smith ACPI_STATUS status; 1514c5ba0be4SMike Smith 1515be2b1797SNate Lawson /* Walk back up the tree to the root */ 1516c5ba0be4SMike Smith for (;;) { 1517be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1518be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1519c5ba0be4SMike Smith *result = r; 1520c5ba0be4SMike Smith return (AE_OK); 1521c5ba0be4SMike Smith } 1522bee4aa4aSNate Lawson /* XXX Return error here? */ 1523c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1524c5ba0be4SMike Smith return (AE_OK); 1525b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1526c5ba0be4SMike Smith return (AE_NOT_FOUND); 1527c5ba0be4SMike Smith parent = r; 1528c5ba0be4SMike Smith } 1529c5ba0be4SMike Smith } 1530c5ba0be4SMike Smith 1531eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1532eea17c34SNate Lawson uint32_t 1533eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1534eea17c34SNate Lawson { 1535eea17c34SNate Lawson uint32_t delta; 1536eea17c34SNate Lawson 1537eea17c34SNate Lawson if (end >= start) 1538eea17c34SNate Lawson delta = end - start; 1539eea17c34SNate Lawson else if (AcpiGbl_FADT->TmrValExt == 0) 15408d01ceefSNate Lawson delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1541eea17c34SNate Lawson else 1542eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 1543eea17c34SNate Lawson return (delta); 1544eea17c34SNate Lawson } 1545eea17c34SNate Lawson 1546c5ba0be4SMike Smith /* 1547c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1548c5ba0be4SMike Smith */ 1549c5ba0be4SMike Smith ACPI_BUFFER * 1550c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1551c5ba0be4SMike Smith { 1552c5ba0be4SMike Smith ACPI_BUFFER *buf; 1553c5ba0be4SMike Smith 1554c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1555c5ba0be4SMike Smith return (NULL); 1556c5ba0be4SMike Smith buf->Length = size; 1557c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1558c5ba0be4SMike Smith return (buf); 1559c5ba0be4SMike Smith } 1560c5ba0be4SMike Smith 1561c310653eSNate Lawson ACPI_STATUS 1562cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1563c310653eSNate Lawson { 1564c310653eSNate Lawson ACPI_OBJECT arg1; 1565c310653eSNate Lawson ACPI_OBJECT_LIST args; 1566c310653eSNate Lawson 1567c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1568c310653eSNate Lawson arg1.Integer.Value = number; 1569c310653eSNate Lawson args.Count = 1; 1570c310653eSNate Lawson args.Pointer = &arg1; 1571c310653eSNate Lawson 1572c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1573c310653eSNate Lawson } 1574c310653eSNate Lawson 1575c5ba0be4SMike Smith /* 1576c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1577c5ba0be4SMike Smith */ 1578c5ba0be4SMike Smith ACPI_STATUS 1579cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1580c5ba0be4SMike Smith { 1581be2b1797SNate Lawson ACPI_STATUS status; 1582c5ba0be4SMike Smith ACPI_BUFFER buf; 1583c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1584c5ba0be4SMike Smith 1585c5ba0be4SMike Smith if (handle == NULL) 1586c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 15870c7da7acSJohn Baldwin 15880c7da7acSJohn Baldwin /* 15890c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 15900c7da7acSJohn Baldwin * a method that will return an Integer. 15910c7da7acSJohn Baldwin */ 1592c5ba0be4SMike Smith buf.Pointer = ¶m; 1593c5ba0be4SMike Smith buf.Length = sizeof(param); 1594be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1595be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1596be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1597c5ba0be4SMike Smith *number = param.Integer.Value; 1598be2b1797SNate Lawson else 1599be2b1797SNate Lawson status = AE_TYPE; 1600c5ba0be4SMike Smith } 16010c7da7acSJohn Baldwin 16020c7da7acSJohn Baldwin /* 16030c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 16040c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 16050c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 16060c7da7acSJohn Baldwin * convert it into an Integer as best we can. 16070c7da7acSJohn Baldwin * 16080c7da7acSJohn Baldwin * This is a hack. 16090c7da7acSJohn Baldwin */ 1610be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1611b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1612be2b1797SNate Lawson status = AE_NO_MEMORY; 16130c7da7acSJohn Baldwin } else { 1614be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1615be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1616be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 16170c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 16180c7da7acSJohn Baldwin } 1619f97739daSNate Lawson } 1620be2b1797SNate Lawson return (status); 1621c5ba0be4SMike Smith } 1622c5ba0be4SMike Smith 1623c573e654SMitsuru IWASAKI ACPI_STATUS 1624cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1625c573e654SMitsuru IWASAKI { 1626c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1627dba55fa2SNate Lawson UINT8 *val; 1628c573e654SMitsuru IWASAKI int i; 1629c573e654SMitsuru IWASAKI 1630c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1631c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1632c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1633c573e654SMitsuru IWASAKI return (AE_OK); 1634c573e654SMitsuru IWASAKI } 1635c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1636c573e654SMitsuru IWASAKI return (AE_TYPE); 1637c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1638c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1639be2b1797SNate Lawson 1640c573e654SMitsuru IWASAKI *number = 0; 1641dba55fa2SNate Lawson val = p->Buffer.Pointer; 1642c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1643dba55fa2SNate Lawson *number += val[i] << (i * 8); 1644c573e654SMitsuru IWASAKI return (AE_OK); 1645c573e654SMitsuru IWASAKI } 1646c573e654SMitsuru IWASAKI 1647c5ba0be4SMike Smith /* 1648c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1649c5ba0be4SMike Smith * function for each element. 1650c5ba0be4SMike Smith * 1651c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1652c5ba0be4SMike Smith */ 1653c5ba0be4SMike Smith ACPI_STATUS 1654be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1655be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1656c5ba0be4SMike Smith { 1657c5ba0be4SMike Smith ACPI_OBJECT *comp; 1658c5ba0be4SMike Smith int i; 1659c5ba0be4SMike Smith 1660be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1661c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1662c5ba0be4SMike Smith 1663be2b1797SNate Lawson /* Iterate over components */ 1664be2b1797SNate Lawson i = 0; 1665be2b1797SNate Lawson comp = pkg->Package.Elements; 1666be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1667c5ba0be4SMike Smith func(comp, arg); 1668c5ba0be4SMike Smith 1669c5ba0be4SMike Smith return (AE_OK); 1670c5ba0be4SMike Smith } 1671c5ba0be4SMike Smith 16726f69255bSMike Smith /* 16736f69255bSMike Smith * Find the (index)th resource object in a set. 16746f69255bSMike Smith */ 16756f69255bSMike Smith ACPI_STATUS 16766d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 16776f69255bSMike Smith { 16786d63101aSMike Smith ACPI_RESOURCE *rp; 16796f69255bSMike Smith int i; 16806f69255bSMike Smith 16816d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16826f69255bSMike Smith i = index; 16836d63101aSMike Smith while (i-- > 0) { 1684be2b1797SNate Lawson /* Range check */ 16856d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16866f69255bSMike Smith return (AE_BAD_PARAMETER); 1687be2b1797SNate Lawson 1688be2b1797SNate Lawson /* Check for terminator */ 1689be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16906d63101aSMike Smith return (AE_NOT_FOUND); 1691abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16926f69255bSMike Smith } 16936f69255bSMike Smith if (resp != NULL) 16946d63101aSMike Smith *resp = rp; 1695be2b1797SNate Lawson 16966d63101aSMike Smith return (AE_OK); 16976d63101aSMike Smith } 16986d63101aSMike Smith 16996d63101aSMike Smith /* 17006d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 17016d63101aSMike Smith * 17026d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 17036d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 17046d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 17056d63101aSMike Smith * resources. 17066d63101aSMike Smith */ 17076d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 17086d63101aSMike Smith 17096d63101aSMike Smith ACPI_STATUS 17106d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 17116d63101aSMike Smith { 17126d63101aSMike Smith ACPI_RESOURCE *rp; 17136d63101aSMike Smith void *newp; 17146d63101aSMike Smith 1715be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 17166d63101aSMike Smith if (buf->Pointer == NULL) { 17176d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 17186d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 17196d63101aSMike Smith return (AE_NO_MEMORY); 17206d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 17216d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 17226d63101aSMike Smith rp->Length = 0; 17236d63101aSMike Smith } 17246d63101aSMike Smith if (res == NULL) 17256d63101aSMike Smith return (AE_OK); 17266d63101aSMike Smith 17276d63101aSMike Smith /* 17286d63101aSMike Smith * Scan the current buffer looking for the terminator. 17296d63101aSMike Smith * This will either find the terminator or hit the end 17306d63101aSMike Smith * of the buffer and return an error. 17316d63101aSMike Smith */ 17326d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 17336d63101aSMike Smith for (;;) { 1734be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 17356d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 17366d63101aSMike Smith return (AE_BAD_PARAMETER); 1737be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 17386d63101aSMike Smith break; 1739abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 17406d63101aSMike Smith } 17416d63101aSMike Smith 17426d63101aSMike Smith /* 17436d63101aSMike Smith * Check the size of the buffer and expand if required. 17446d63101aSMike Smith * 17456d63101aSMike Smith * Required size is: 17466d63101aSMike Smith * size of existing resources before terminator + 17476d63101aSMike Smith * size of new resource and header + 17486d63101aSMike Smith * size of terminator. 17496d63101aSMike Smith * 17506d63101aSMike Smith * Note that this loop should really only run once, unless 17516d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 17526d63101aSMike Smith */ 17536d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 17546d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 17556d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 17566d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 17576d63101aSMike Smith return (AE_NO_MEMORY); 17586d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1759a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1760a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 17616d63101aSMike Smith AcpiOsFree(buf->Pointer); 17626d63101aSMike Smith buf->Pointer = newp; 17636d63101aSMike Smith buf->Length += buf->Length; 17646d63101aSMike Smith } 17656d63101aSMike Smith 1766be2b1797SNate Lawson /* Insert the new resource. */ 17676d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 17686d63101aSMike Smith 1769be2b1797SNate Lawson /* And add the terminator. */ 1770abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 17716d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 17726d63101aSMike Smith rp->Length = 0; 17736d63101aSMike Smith 17746f69255bSMike Smith return (AE_OK); 17756f69255bSMike Smith } 1776c5ba0be4SMike Smith 1777da14ac9fSJohn Baldwin /* 1778da14ac9fSJohn Baldwin * Set interrupt model. 1779da14ac9fSJohn Baldwin */ 1780da14ac9fSJohn Baldwin ACPI_STATUS 1781da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1782da14ac9fSJohn Baldwin { 1783da14ac9fSJohn Baldwin 1784c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1785da14ac9fSJohn Baldwin } 1786da14ac9fSJohn Baldwin 1787ece50487SMitsuru IWASAKI static void 1788ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1789ece50487SMitsuru IWASAKI { 1790bee4aa4aSNate Lawson 1791ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1792ece50487SMitsuru IWASAKI } 1793c30382dfSMitsuru IWASAKI 179415e2f34fSNate Lawson enum acpi_sleep_state { 179515e2f34fSNate Lawson ACPI_SS_NONE, 179615e2f34fSNate Lawson ACPI_SS_GPE_SET, 179715e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 179815e2f34fSNate Lawson ACPI_SS_SLP_PREP, 179915e2f34fSNate Lawson ACPI_SS_SLEPT, 180015e2f34fSNate Lawson }; 180115e2f34fSNate Lawson 180215e32d5dSMike Smith /* 180315e32d5dSMike Smith * Set the system sleep state 180415e32d5dSMike Smith * 1805be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 180615e32d5dSMike Smith */ 180715e32d5dSMike Smith ACPI_STATUS 180815e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 180915e32d5dSMike Smith { 181015e2f34fSNate Lawson ACPI_STATUS status; 181156d8cb57SMitsuru IWASAKI UINT8 TypeA; 181256d8cb57SMitsuru IWASAKI UINT8 TypeB; 181315e2f34fSNate Lawson enum acpi_sleep_state slp_state; 181415e32d5dSMike Smith 1815b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 18160ae55423SMike Smith 181715e2f34fSNate Lawson status = AE_OK; 181815e2f34fSNate Lawson ACPI_LOCK(acpi); 181915e2f34fSNate Lawson if (sc->acpi_sleep_disabled) { 18206397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 182115e2f34fSNate Lawson status = AE_ERROR; 182215e2f34fSNate Lawson ACPI_UNLOCK(acpi); 182315e2f34fSNate Lawson printf("acpi: suspend request ignored (not ready yet)\n"); 182415e2f34fSNate Lawson return (status); 182515e2f34fSNate Lawson } 182615e2f34fSNate Lawson sc->acpi_sleep_disabled = 1; 182715e2f34fSNate Lawson ACPI_UNLOCK(acpi); 18286397624dSMitsuru IWASAKI 182915e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 183015e32d5dSMike Smith switch (state) { 183115e32d5dSMike Smith case ACPI_STATE_S1: 18326161544cSTakanori Watanabe case ACPI_STATE_S2: 18336161544cSTakanori Watanabe case ACPI_STATE_S3: 18346161544cSTakanori Watanabe case ACPI_STATE_S4: 1835adad4744SNate Lawson status = AcpiGetSleepTypeData(state, &TypeA, &TypeB); 183698175614SNate Lawson if (status == AE_NOT_FOUND) { 183798175614SNate Lawson device_printf(sc->acpi_dev, 183898175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 183998175614SNate Lawson break; 184098175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1841be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1842be2b1797SNate Lawson AcpiFormatException(status)); 184356d8cb57SMitsuru IWASAKI break; 184456d8cb57SMitsuru IWASAKI } 184556d8cb57SMitsuru IWASAKI 1846a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1847a1fccb47SMitsuru IWASAKI 1848d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 1849d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 185015e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 1851e8b4d56eSNate Lawson 185215e32d5dSMike Smith /* 1853c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 1854c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 185515e32d5dSMike Smith * 1856c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 1857c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 1858c7f88fc3SNate Lawson * bus interface does not provide for this. 185915e32d5dSMike Smith */ 186015e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 186115e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 186215e2f34fSNate Lawson break; 186315e2f34fSNate Lawson } 186415e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 1865b9c780d6SMitsuru IWASAKI 1866be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1867be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1868b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1869b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1870b9c780d6SMitsuru IWASAKI break; 1871b9c780d6SMitsuru IWASAKI } 187215e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 1873b9c780d6SMitsuru IWASAKI 1874be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1875ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1876ff01efb5SMitsuru IWASAKI 18776161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 18786161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 18796161544cSTakanori Watanabe 18806161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1881be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1882964679ceSMitsuru IWASAKI AcpiEnable(); 18836161544cSTakanori Watanabe } else { 1884c7f88fc3SNate Lawson ACPI_DISABLE_IRQS(); 1885adad4744SNate Lawson status = AcpiEnterSleepState(state); 1886be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1887be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1888be2b1797SNate Lawson AcpiFormatException(status)); 1889c30382dfSMitsuru IWASAKI break; 189015e32d5dSMike Smith } 18916161544cSTakanori Watanabe } 189215e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 189315e32d5dSMike Smith break; 189415e32d5dSMike Smith case ACPI_STATE_S5: 189515e32d5dSMike Smith /* 189615e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 189715e32d5dSMike Smith * shutdown handlers. 189815e32d5dSMike Smith */ 189915e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 190015e32d5dSMike Smith break; 190198175614SNate Lawson case ACPI_STATE_S0: 190215e32d5dSMike Smith default: 190315e32d5dSMike Smith status = AE_BAD_PARAMETER; 190415e32d5dSMike Smith break; 190515e32d5dSMike Smith } 1906ece50487SMitsuru IWASAKI 190715e2f34fSNate Lawson /* 190815e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 190915e2f34fSNate Lawson * process. This handles both the error and success cases. 191015e2f34fSNate Lawson */ 191115e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 191215e2f34fSNate Lawson acpi_wake_prep_walk(state); 191315e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 191415e2f34fSNate Lawson } 191515e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLP_PREP) 191615e2f34fSNate Lawson AcpiLeaveSleepState(state); 1917071339e2SNate Lawson if (slp_state >= ACPI_SS_DEV_SUSPEND) 1918071339e2SNate Lawson DEVICE_RESUME(root_bus); 191915e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLEPT) 192015e2f34fSNate Lawson acpi_enable_fixed_events(sc); 192115e2f34fSNate Lawson 192215e2f34fSNate Lawson /* Allow another sleep request after a while. */ 192315e2f34fSNate Lawson if (state != ACPI_STATE_S5) 1924ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1925ece50487SMitsuru IWASAKI 19260ae55423SMike Smith return_ACPI_STATUS (status); 192715e32d5dSMike Smith } 192815e32d5dSMike Smith 1929e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1930e8b4d56eSNate Lawson int 1931e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1932e8b4d56eSNate Lawson { 1933e8b4d56eSNate Lawson struct acpi_prw_data prw; 1934e8b4d56eSNate Lawson 1935e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1936e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1937e8b4d56eSNate Lawson return (ENXIO); 1938e8b4d56eSNate Lawson 1939e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1940e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1941e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1942e8b4d56eSNate Lawson return (ENXIO); 1943e8b4d56eSNate Lawson } 1944e8b4d56eSNate Lawson 1945e8b4d56eSNate Lawson return (0); 1946e8b4d56eSNate Lawson } 1947e8b4d56eSNate Lawson 1948e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1949e8b4d56eSNate Lawson int 1950e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1951e8b4d56eSNate Lawson { 1952e8b4d56eSNate Lawson struct acpi_prw_data prw; 1953e8b4d56eSNate Lawson ACPI_HANDLE handle; 1954e8b4d56eSNate Lawson ACPI_STATUS status; 1955e8b4d56eSNate Lawson int flags; 1956e8b4d56eSNate Lawson 1957d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 1958e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1959e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1960e8b4d56eSNate Lawson return (ENXIO); 1961e8b4d56eSNate Lawson 1962d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 1963e8b4d56eSNate Lawson if (enable) { 1964e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1965e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1966e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1967e8b4d56eSNate Lawson return (ENXIO); 1968e8b4d56eSNate Lawson } 1969d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1970e8b4d56eSNate Lawson } else { 1971e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1972e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1973e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1974e8b4d56eSNate Lawson return (ENXIO); 1975e8b4d56eSNate Lawson } 1976d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1977e8b4d56eSNate Lawson } 1978e8b4d56eSNate Lawson 1979e8b4d56eSNate Lawson return (0); 1980e8b4d56eSNate Lawson } 1981e8b4d56eSNate Lawson 1982d4b9ff91SNate Lawson static int 1983d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 1984e8b4d56eSNate Lawson { 1985e8b4d56eSNate Lawson struct acpi_prw_data prw; 1986d4b9ff91SNate Lawson device_t dev; 1987e8b4d56eSNate Lawson 1988d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 1989e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1990e8b4d56eSNate Lawson return (ENXIO); 1991d4b9ff91SNate Lawson dev = acpi_get_device(handle); 1992e8b4d56eSNate Lawson 1993e8b4d56eSNate Lawson /* 1994d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 1995d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 1996d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 1997d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 1998d4b9ff91SNate Lawson * and set _PSW. 1999e8b4d56eSNate Lawson */ 2000d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 2001cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2002e8b4d56eSNate Lawson if (bootverbose) 2003d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2004d4b9ff91SNate Lawson acpi_name(handle), sstate); 2005d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2006d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 2007d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 2008d4b9ff91SNate Lawson if (bootverbose) 2009d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2010d4b9ff91SNate Lawson acpi_name(handle), sstate); 2011d4b9ff91SNate Lawson } 2012cc85c78cSNate Lawson 2013cc85c78cSNate Lawson return (0); 2014e8b4d56eSNate Lawson } 2015e8b4d56eSNate Lawson 2016d4b9ff91SNate Lawson static int 2017d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2018cc85c78cSNate Lawson { 2019cc85c78cSNate Lawson struct acpi_prw_data prw; 2020d4b9ff91SNate Lawson device_t dev; 2021cc85c78cSNate Lawson 2022cc85c78cSNate Lawson /* 2023d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 2024d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 2025cc85c78cSNate Lawson */ 2026d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2027d4b9ff91SNate Lawson return (ENXIO); 2028d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2029d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2030d4b9ff91SNate Lawson return (0); 2031cc85c78cSNate Lawson 2032d4b9ff91SNate Lawson /* 2033d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 2034d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 2035d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 2036d4b9ff91SNate Lawson */ 2037d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 2038cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2039d4b9ff91SNate Lawson if (bootverbose) 2040d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2041d4b9ff91SNate Lawson } else { 2042d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 2043d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 2044d4b9ff91SNate Lawson if (bootverbose) 2045d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 2046d4b9ff91SNate Lawson acpi_name(handle)); 2047d4b9ff91SNate Lawson } 2048d4b9ff91SNate Lawson 2049e8b4d56eSNate Lawson return (0); 2050e8b4d56eSNate Lawson } 2051e8b4d56eSNate Lawson 2052e8b4d56eSNate Lawson static ACPI_STATUS 2053d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2054e8b4d56eSNate Lawson { 2055d4b9ff91SNate Lawson int sstate; 2056e8b4d56eSNate Lawson 2057d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 2058d4b9ff91SNate Lawson sstate = *(int *)context; 2059d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 2060d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 2061d4b9ff91SNate Lawson else 2062d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 2063e8b4d56eSNate Lawson return (AE_OK); 2064e8b4d56eSNate Lawson } 2065e8b4d56eSNate Lawson 2066d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2067e8b4d56eSNate Lawson static int 2068d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 2069e8b4d56eSNate Lawson { 2070e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 2071e8b4d56eSNate Lawson 2072e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2073d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 2074d4b9ff91SNate Lawson acpi_wake_prep, &sstate, NULL); 2075e8b4d56eSNate Lawson return (0); 2076e8b4d56eSNate Lawson } 2077e8b4d56eSNate Lawson 207888a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 207988a79fc0SNate Lawson static int 208088a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 208188a79fc0SNate Lawson { 208288a79fc0SNate Lawson int error, i, numdevs; 208388a79fc0SNate Lawson device_t *devlist; 208488a79fc0SNate Lawson device_t child; 2085d4b9ff91SNate Lawson ACPI_STATUS status; 208688a79fc0SNate Lawson 208788a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 208888a79fc0SNate Lawson if (error != 0 || numdevs == 0) 208988a79fc0SNate Lawson return (error); 209088a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 209188a79fc0SNate Lawson child = devlist[i]; 2092d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 209388a79fc0SNate Lawson if (!device_is_attached(child)) 209488a79fc0SNate Lawson continue; 2095d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2096d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 209788a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 209888a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 209988a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 210088a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 210188a79fc0SNate Lawson } 210288a79fc0SNate Lawson } 210388a79fc0SNate Lawson free(devlist, M_TEMP); 210488a79fc0SNate Lawson 210588a79fc0SNate Lawson return (0); 210688a79fc0SNate Lawson } 210788a79fc0SNate Lawson 210888a79fc0SNate Lawson /* Enable or disable wake from userland. */ 210988a79fc0SNate Lawson static int 211088a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 211188a79fc0SNate Lawson { 211288a79fc0SNate Lawson int enable, error; 211388a79fc0SNate Lawson device_t dev; 211488a79fc0SNate Lawson 211588a79fc0SNate Lawson dev = (device_t)arg1; 2116d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 211788a79fc0SNate Lawson 211888a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 211988a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 212088a79fc0SNate Lawson return (error); 212188a79fc0SNate Lawson if (enable != 0 && enable != 1) 212288a79fc0SNate Lawson return (EINVAL); 212388a79fc0SNate Lawson 212488a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 212588a79fc0SNate Lawson } 212688a79fc0SNate Lawson 2127e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2128d4b9ff91SNate Lawson int 2129e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2130e8b4d56eSNate Lawson { 2131e8b4d56eSNate Lawson ACPI_STATUS status; 2132e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2133e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2134d4b9ff91SNate Lawson int error, i, power_count; 2135e8b4d56eSNate Lawson 2136e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2137e8b4d56eSNate Lawson return (EINVAL); 2138e8b4d56eSNate Lawson 2139e8b4d56eSNate Lawson /* 2140e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2141e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2142e8b4d56eSNate Lawson */ 2143e8b4d56eSNate Lawson error = EINVAL; 2144e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2145e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2146e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2147e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2148e8b4d56eSNate Lawson return (ENOENT); 2149e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2150e8b4d56eSNate Lawson if (res == NULL) 2151e8b4d56eSNate Lawson return (ENOENT); 2152e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2153e8b4d56eSNate Lawson goto out; 2154e8b4d56eSNate Lawson 2155e8b4d56eSNate Lawson /* 2156e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2157e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2158e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2159e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2160e8b4d56eSNate Lawson */ 2161e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2162e8b4d56eSNate Lawson goto out; 2163e8b4d56eSNate Lawson 2164e8b4d56eSNate Lawson /* 2165e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2166e8b4d56eSNate Lawson */ 2167e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2168e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2169e8b4d56eSNate Lawson /* 2170e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2171e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2172e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2173e8b4d56eSNate Lawson * enabled for the wake event. 2174e8b4d56eSNate Lawson */ 2175e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2176e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2177e8b4d56eSNate Lawson error = 0; 2178e8b4d56eSNate Lawson break; 2179e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2180e8b4d56eSNate Lawson /* 2181e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2182e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2183e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2184e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2185e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2186e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2187e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2188e8b4d56eSNate Lawson * the wake event. 2189e8b4d56eSNate Lawson * 2190e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2191e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2192e8b4d56eSNate Lawson */ 2193e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2194e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2195e8b4d56eSNate Lawson goto out; 2196e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2197e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2198e8b4d56eSNate Lawson goto out; 2199e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2200e8b4d56eSNate Lawson goto out; 2201e8b4d56eSNate Lawson error = 0; 2202e8b4d56eSNate Lawson break; 2203e8b4d56eSNate Lawson default: 2204e8b4d56eSNate Lawson goto out; 2205e8b4d56eSNate Lawson } 2206e8b4d56eSNate Lawson 2207d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 2208d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 2209d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 2210d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2211d4b9ff91SNate Lawson power_count = 0; 2212d4b9ff91SNate Lawson } 2213d4b9ff91SNate Lawson prw->power_res_count = power_count; 2214d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 2215d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 2216e8b4d56eSNate Lawson 2217e8b4d56eSNate Lawson out: 2218e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2219e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2220e8b4d56eSNate Lawson return (error); 2221e8b4d56eSNate Lawson } 2222e8b4d56eSNate Lawson 222315e32d5dSMike Smith /* 222415e32d5dSMike Smith * ACPI Event Handlers 222515e32d5dSMike Smith */ 222615e32d5dSMike Smith 222715e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 222815e32d5dSMike Smith 222915e32d5dSMike Smith static void 223015e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 223115e32d5dSMike Smith { 2232bee4aa4aSNate Lawson 2233b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 223415e32d5dSMike Smith 2235a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 223615e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2237bee4aa4aSNate Lawson 22380ae55423SMike Smith return_VOID; 223915e32d5dSMike Smith } 224015e32d5dSMike Smith 224115e32d5dSMike Smith static void 224215e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 224315e32d5dSMike Smith { 2244bee4aa4aSNate Lawson 2245b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 22460ae55423SMike Smith 2247bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 2248cb9b0d80SMike Smith 22490ae55423SMike Smith return_VOID; 225015e32d5dSMike Smith } 225115e32d5dSMike Smith 225215e32d5dSMike Smith /* 225315e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 225415e32d5dSMike Smith */ 225515e32d5dSMike Smith UINT32 225633febf93SNate Lawson acpi_event_power_button_sleep(void *context) 225715e32d5dSMike Smith { 225815e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 225915e32d5dSMike Smith 2260b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22610ae55423SMike Smith 226215e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 22630ae55423SMike Smith 2264b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 226515e32d5dSMike Smith } 226615e32d5dSMike Smith 226715e32d5dSMike Smith UINT32 226833febf93SNate Lawson acpi_event_power_button_wake(void *context) 226915e32d5dSMike Smith { 227015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 227115e32d5dSMike Smith 2272b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22730ae55423SMike Smith 227415e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 22750ae55423SMike Smith 2276b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 227715e32d5dSMike Smith } 227815e32d5dSMike Smith 227915e32d5dSMike Smith UINT32 228033febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 228115e32d5dSMike Smith { 228215e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 228315e32d5dSMike Smith 2284b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22850ae55423SMike Smith 228615e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 22870ae55423SMike Smith 2288b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 228915e32d5dSMike Smith } 229015e32d5dSMike Smith 229115e32d5dSMike Smith UINT32 229233febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 229315e32d5dSMike Smith { 229415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 229515e32d5dSMike Smith 2296b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22970ae55423SMike Smith 229815e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 22990ae55423SMike Smith 2300b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 230115e32d5dSMike Smith } 230215e32d5dSMike Smith 230315e32d5dSMike Smith /* 2304bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 2305bee4aa4aSNate Lawson * use this for single-threaded callers. 230615e32d5dSMike Smith */ 230715e32d5dSMike Smith char * 230815e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 230915e32d5dSMike Smith { 2310bee4aa4aSNate Lawson ACPI_BUFFER buf; 2311bee4aa4aSNate Lawson static char data[256]; 231215e32d5dSMike Smith 2313bee4aa4aSNate Lawson buf.Length = sizeof(data); 2314bee4aa4aSNate Lawson buf.Pointer = data; 2315cb9b0d80SMike Smith 23160a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 2317bee4aa4aSNate Lawson return (data); 2318bee4aa4aSNate Lawson return ("(unknown)"); 231915e32d5dSMike Smith } 232015e32d5dSMike Smith 232115e32d5dSMike Smith /* 232215e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 232315e32d5dSMike Smith * parts of the namespace. 232415e32d5dSMike Smith */ 232515e32d5dSMike Smith int 232615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 232715e32d5dSMike Smith { 23283c600cfbSJohn Baldwin char *cp, *env, *np; 232915e32d5dSMike Smith int len; 233015e32d5dSMike Smith 233115e32d5dSMike Smith np = acpi_name(handle); 233215e32d5dSMike Smith if (*np == '\\') 233315e32d5dSMike Smith np++; 23343c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 233515e32d5dSMike Smith return (0); 233615e32d5dSMike Smith 2337be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 23383c600cfbSJohn Baldwin cp = env; 233915e32d5dSMike Smith for (;;) { 2340bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 234115e32d5dSMike Smith cp++; 234215e32d5dSMike Smith if (*cp == 0) 234315e32d5dSMike Smith break; 234415e32d5dSMike Smith len = 0; 2345bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 234615e32d5dSMike Smith len++; 2347d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 23483c600cfbSJohn Baldwin freeenv(env); 23490ae55423SMike Smith return(1); 2350d786139cSMaxime Henrion } 23510ae55423SMike Smith cp += len; 23520ae55423SMike Smith } 23533c600cfbSJohn Baldwin freeenv(env); 2354be2b1797SNate Lawson 23550ae55423SMike Smith return (0); 23560ae55423SMike Smith } 23570ae55423SMike Smith 23580ae55423SMike Smith /* 23590ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 23600ae55423SMike Smith */ 23610ae55423SMike Smith int 23620ae55423SMike Smith acpi_disabled(char *subsys) 23630ae55423SMike Smith { 236478689b15SMaxime Henrion char *cp, *env; 23650ae55423SMike Smith int len; 23660ae55423SMike Smith 23673184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 23680ae55423SMike Smith return (0); 23693184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 237078689b15SMaxime Henrion freeenv(env); 23710ae55423SMike Smith return (1); 2372d786139cSMaxime Henrion } 23730ae55423SMike Smith 23743184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 237578689b15SMaxime Henrion cp = env; 23760ae55423SMike Smith for (;;) { 23773184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 23780ae55423SMike Smith cp++; 23793184cf5aSNate Lawson if (*cp == '\0') 23800ae55423SMike Smith break; 23810ae55423SMike Smith len = 0; 23823184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 23830ae55423SMike Smith len++; 23843184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 238578689b15SMaxime Henrion freeenv(env); 238615e32d5dSMike Smith return (1); 2387d786139cSMaxime Henrion } 238815e32d5dSMike Smith cp += len; 238915e32d5dSMike Smith } 239078689b15SMaxime Henrion freeenv(env); 2391be2b1797SNate Lawson 239215e32d5dSMike Smith return (0); 239315e32d5dSMike Smith } 239415e32d5dSMike Smith 239515e32d5dSMike Smith /* 239615e32d5dSMike Smith * Control interface. 239715e32d5dSMike Smith * 23980ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2399be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2400be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 240115e32d5dSMike Smith */ 24020ae55423SMike Smith struct acpi_ioctl_hook 24030ae55423SMike Smith { 24040ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 24050ae55423SMike Smith u_long cmd; 2406be2b1797SNate Lawson acpi_ioctl_fn fn; 24070ae55423SMike Smith void *arg; 24080ae55423SMike Smith }; 24090ae55423SMike Smith 24100ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 24110ae55423SMike Smith static int acpi_ioctl_hooks_initted; 24120ae55423SMike Smith 24130ae55423SMike Smith int 2414be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 24150ae55423SMike Smith { 24160ae55423SMike Smith struct acpi_ioctl_hook *hp; 24170ae55423SMike Smith 24180ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 24190ae55423SMike Smith return (ENOMEM); 24200ae55423SMike Smith hp->cmd = cmd; 24210ae55423SMike Smith hp->fn = fn; 24220ae55423SMike Smith hp->arg = arg; 242315e2f34fSNate Lawson 242415e2f34fSNate Lawson ACPI_LOCK(acpi); 24250ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 24260ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 24270ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 24280ae55423SMike Smith } 24290ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 243015e2f34fSNate Lawson ACPI_UNLOCK(acpi); 243115e2f34fSNate Lawson 24320ae55423SMike Smith return (0); 24330ae55423SMike Smith } 24340ae55423SMike Smith 24350ae55423SMike Smith void 2436be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 24370ae55423SMike Smith { 24380ae55423SMike Smith struct acpi_ioctl_hook *hp; 24390ae55423SMike Smith 244015e2f34fSNate Lawson ACPI_LOCK(acpi); 24410ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 244215e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 24430ae55423SMike Smith break; 24440ae55423SMike Smith 24450ae55423SMike Smith if (hp != NULL) { 24460ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 24470ae55423SMike Smith free(hp, M_ACPIDEV); 24480ae55423SMike Smith } 244915e2f34fSNate Lawson ACPI_UNLOCK(acpi); 24500ae55423SMike Smith } 24510ae55423SMike Smith 245215e32d5dSMike Smith static int 245389c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 245415e32d5dSMike Smith { 245515e32d5dSMike Smith return (0); 245615e32d5dSMike Smith } 245715e32d5dSMike Smith 245815e32d5dSMike Smith static int 245989c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 246015e32d5dSMike Smith { 246115e32d5dSMike Smith return (0); 246215e32d5dSMike Smith } 246315e32d5dSMike Smith 246415e32d5dSMike Smith static int 246589c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 246615e32d5dSMike Smith { 246715e32d5dSMike Smith struct acpi_softc *sc; 24680ae55423SMike Smith struct acpi_ioctl_hook *hp; 2469bee4aa4aSNate Lawson int error, state; 247015e32d5dSMike Smith 2471a2e35898SDavid E. O'Brien error = 0; 2472a2e35898SDavid E. O'Brien hp = NULL; 247315e32d5dSMike Smith sc = dev->si_drv1; 247415e32d5dSMike Smith 24750ae55423SMike Smith /* 24760ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 24770ae55423SMike Smith */ 247815e2f34fSNate Lawson ACPI_LOCK(acpi); 2479bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 24800ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 2481bee4aa4aSNate Lawson if (hp->cmd == cmd) 2482bee4aa4aSNate Lawson break; 24830ae55423SMike Smith } 248415e2f34fSNate Lawson ACPI_UNLOCK(acpi); 2485bee4aa4aSNate Lawson if (hp) 2486bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 24870ae55423SMike Smith 24880ae55423SMike Smith /* 2489a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2490a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2491a89fcf28STakanori Watanabe * Not changing system behavior. 2492a89fcf28STakanori Watanabe */ 2493be2b1797SNate Lawson if ((flag & FWRITE) == 0) 2494be2b1797SNate Lawson return (EPERM); 2495a89fcf28STakanori Watanabe 2496be2b1797SNate Lawson /* Core system ioctls. */ 249715e32d5dSMike Smith switch (cmd) { 249815e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 2499bee4aa4aSNate Lawson error = EINVAL; 250015e32d5dSMike Smith state = *(int *)addr; 2501bee4aa4aSNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 2502bee4aa4aSNate Lawson if (ACPI_SUCCESS(acpi_SetSleepState(sc, state))) 2503bee4aa4aSNate Lawson error = 0; 250415e32d5dSMike Smith break; 250515e32d5dSMike Smith default: 2506bee4aa4aSNate Lawson error = ENXIO; 250715e32d5dSMike Smith break; 250815e32d5dSMike Smith } 2509917d44c8SMitsuru IWASAKI 251015e32d5dSMike Smith return (error); 251115e32d5dSMike Smith } 251215e32d5dSMike Smith 25131d073b1dSJohn Baldwin static int 2514d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2515d75de536SMitsuru IWASAKI { 2516d75de536SMitsuru IWASAKI int error; 2517bee4aa4aSNate Lawson struct sbuf sb; 2518d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2519d75de536SMitsuru IWASAKI 2520bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 2521bee4aa4aSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) 2522bee4aa4aSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 2523bee4aa4aSNate Lawson sbuf_printf(&sb, "S%d ", state); 2524bee4aa4aSNate Lawson sbuf_trim(&sb); 2525bee4aa4aSNate Lawson sbuf_finish(&sb); 2526bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 2527bee4aa4aSNate Lawson sbuf_delete(&sb); 2528d75de536SMitsuru IWASAKI return (error); 2529d75de536SMitsuru IWASAKI } 2530d75de536SMitsuru IWASAKI 2531d75de536SMitsuru IWASAKI static int 25321d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 25331d073b1dSJohn Baldwin { 25341d073b1dSJohn Baldwin char sleep_state[10]; 25351d073b1dSJohn Baldwin int error; 25361d073b1dSJohn Baldwin u_int new_state, old_state; 25371d073b1dSJohn Baldwin 25381d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2539bee4aa4aSNate Lawson if (old_state > ACPI_S_STATES_MAX + 1) 2540bee4aa4aSNate Lawson strlcpy(sleep_state, "unknown", sizeof(sleep_state)); 2541bee4aa4aSNate Lawson else 2542bee4aa4aSNate Lawson strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state)); 25431d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 25441d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2545be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2546bee4aa4aSNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) 2547bee4aa4aSNate Lawson if (strcmp(sleep_state, sleep_state_names[new_state]) == 0) 25481d073b1dSJohn Baldwin break; 2549931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2550be2b1797SNate Lawson if (new_state != old_state) 25511d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2552bee4aa4aSNate Lawson } else 25531d073b1dSJohn Baldwin error = EINVAL; 25541d073b1dSJohn Baldwin } 2555be2b1797SNate Lawson 25561d073b1dSJohn Baldwin return (error); 25571d073b1dSJohn Baldwin } 25581d073b1dSJohn Baldwin 25599b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 25609b937d48SNate Lawson void 25619b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 25629b937d48SNate Lawson { 25639b937d48SNate Lawson char notify_buf[16]; 25649b937d48SNate Lawson ACPI_BUFFER handle_buf; 25659b937d48SNate Lawson ACPI_STATUS status; 25669b937d48SNate Lawson 25679b937d48SNate Lawson if (subsystem == NULL) 25689b937d48SNate Lawson return; 25699b937d48SNate Lawson 25709b937d48SNate Lawson handle_buf.Pointer = NULL; 25719b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 25729b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 25739b937d48SNate Lawson if (ACPI_FAILURE(status)) 25749b937d48SNate Lawson return; 25759b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 25769b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 25779b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 25789b937d48SNate Lawson } 25799b937d48SNate Lawson 258015e32d5dSMike Smith #ifdef ACPI_DEBUG 25810ae55423SMike Smith /* 25820ae55423SMike Smith * Support for parsing debug options from the kernel environment. 25830ae55423SMike Smith * 25840ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 25850ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 25860ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 25870ae55423SMike Smith * prefixing the bit name with !. 25880ae55423SMike Smith */ 258915e32d5dSMike Smith struct debugtag 259015e32d5dSMike Smith { 259115e32d5dSMike Smith char *name; 259215e32d5dSMike Smith UINT32 value; 259315e32d5dSMike Smith }; 259415e32d5dSMike Smith 259515e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2596c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2597c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2598c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2599c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2600c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2601c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2602c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2603c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2604c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2605d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 26064c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2607d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2608297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 26094c1cdee6SMike Smith 2610c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2611c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 26123184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2613c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 26143184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 26153184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 26163184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 26174c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2618cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 26193184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2620b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 262115e32d5dSMike Smith {NULL, 0} 262215e32d5dSMike Smith }; 262315e32d5dSMike Smith 262415e32d5dSMike Smith static struct debugtag dbg_level[] = { 26254c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 26269501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 26279501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 26284c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 26299501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 26304c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2631d62ab2f4SMitsuru IWASAKI 2632d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2633297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 26344c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 26354c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2636d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 26374c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 26384c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 26394c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 26404c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 26414c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 26424c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 26434c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 26444c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 26454c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 26464c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2647d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2648d62ab2f4SMitsuru IWASAKI 2649d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2650d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2651d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2652d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2653d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 26544c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2655d62ab2f4SMitsuru IWASAKI 2656d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2657d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2658d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2659d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2660d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2661d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2662d62ab2f4SMitsuru IWASAKI 2663d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 266498479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 266598479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 266698479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 266798479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 266898479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 266915e32d5dSMike Smith {NULL, 0} 267015e32d5dSMike Smith }; 267115e32d5dSMike Smith 267215e32d5dSMike Smith static void 267315e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 267415e32d5dSMike Smith { 267515e32d5dSMike Smith char *ep; 267615e32d5dSMike Smith int i, l; 26770ae55423SMike Smith int set; 267815e32d5dSMike Smith 267915e32d5dSMike Smith while (*cp) { 268015e32d5dSMike Smith if (isspace(*cp)) { 268115e32d5dSMike Smith cp++; 268215e32d5dSMike Smith continue; 268315e32d5dSMike Smith } 268415e32d5dSMike Smith ep = cp; 268515e32d5dSMike Smith while (*ep && !isspace(*ep)) 268615e32d5dSMike Smith ep++; 26870ae55423SMike Smith if (*cp == '!') { 26880ae55423SMike Smith set = 0; 26890ae55423SMike Smith cp++; 26900ae55423SMike Smith if (cp == ep) 26910ae55423SMike Smith continue; 26920ae55423SMike Smith } else { 26930ae55423SMike Smith set = 1; 26940ae55423SMike Smith } 269515e32d5dSMike Smith l = ep - cp; 269615e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 269715e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2698be2b1797SNate Lawson if (set) 269915e32d5dSMike Smith *flag |= tag[i].value; 2700be2b1797SNate Lawson else 27010ae55423SMike Smith *flag &= ~tag[i].value; 270215e32d5dSMike Smith } 270315e32d5dSMike Smith } 270415e32d5dSMike Smith cp = ep; 270515e32d5dSMike Smith } 270615e32d5dSMike Smith } 270715e32d5dSMike Smith 270815e32d5dSMike Smith static void 27092a4ac806SMike Smith acpi_set_debugging(void *junk) 271015e32d5dSMike Smith { 27117e639165SNate Lawson char *layer, *level; 271215e32d5dSMike Smith 27131d7b121cSNate Lawson if (cold) { 27140ae55423SMike Smith AcpiDbgLayer = 0; 27150ae55423SMike Smith AcpiDbgLevel = 0; 27161d7b121cSNate Lawson } 27171d7b121cSNate Lawson 27187e639165SNate Lawson layer = getenv("debug.acpi.layer"); 27197e639165SNate Lawson level = getenv("debug.acpi.level"); 27207e639165SNate Lawson if (layer == NULL && level == NULL) 27217e639165SNate Lawson return; 27220ae55423SMike Smith 27237e639165SNate Lawson printf("ACPI set debug"); 27247e639165SNate Lawson if (layer != NULL) { 27257e639165SNate Lawson if (strcmp("NONE", layer) != 0) 27267e639165SNate Lawson printf(" layer '%s'", layer); 27277e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 27287e639165SNate Lawson freeenv(layer); 27291d7b121cSNate Lawson } 27307e639165SNate Lawson if (level != NULL) { 27317e639165SNate Lawson if (strcmp("NONE", level) != 0) 27327e639165SNate Lawson printf(" level '%s'", level); 27337e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 27347e639165SNate Lawson freeenv(level); 27357e639165SNate Lawson } 27367e639165SNate Lawson printf("\n"); 273715e32d5dSMike Smith } 2738bee4aa4aSNate Lawson 2739be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2740be2b1797SNate Lawson NULL); 27411d7b121cSNate Lawson 27421d7b121cSNate Lawson static int 27431d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 27441d7b121cSNate Lawson { 274554f6bca0SNate Lawson int error, *dbg; 27461d7b121cSNate Lawson struct debugtag *tag; 274754f6bca0SNate Lawson struct sbuf sb; 27481d7b121cSNate Lawson 274954f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 275054f6bca0SNate Lawson return (ENOMEM); 27511d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 27521d7b121cSNate Lawson tag = &dbg_layer[0]; 27531d7b121cSNate Lawson dbg = &AcpiDbgLayer; 27541d7b121cSNate Lawson } else { 27551d7b121cSNate Lawson tag = &dbg_level[0]; 27561d7b121cSNate Lawson dbg = &AcpiDbgLevel; 27571d7b121cSNate Lawson } 27581d7b121cSNate Lawson 27591d7b121cSNate Lawson /* Get old values if this is a get request. */ 276015e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 27611d7b121cSNate Lawson if (*dbg == 0) { 276254f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 27631d7b121cSNate Lawson } else if (req->newptr == NULL) { 27641d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 276554f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 276654f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 27671d7b121cSNate Lawson } 27681d7b121cSNate Lawson } 276954f6bca0SNate Lawson sbuf_trim(&sb); 277054f6bca0SNate Lawson sbuf_finish(&sb); 27711d7b121cSNate Lawson 27727e639165SNate Lawson /* Copy out the old values to the user. */ 27737e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 277454f6bca0SNate Lawson sbuf_delete(&sb); 27751d7b121cSNate Lawson 27761d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 27771d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 27781d7b121cSNate Lawson *dbg = 0; 27791d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 27801d7b121cSNate Lawson acpi_set_debugging(NULL); 27811d7b121cSNate Lawson } 278215e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 27831d7b121cSNate Lawson 27841d7b121cSNate Lawson return (error); 27851d7b121cSNate Lawson } 2786bee4aa4aSNate Lawson 27871d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 27881d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 27891d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 27901d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 2791bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 2792f9390180SMitsuru IWASAKI 2793f9390180SMitsuru IWASAKI static int 2794f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2795f9390180SMitsuru IWASAKI { 2796f9390180SMitsuru IWASAKI int state, acpi_state; 2797f9390180SMitsuru IWASAKI int error; 2798f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2799f9390180SMitsuru IWASAKI va_list ap; 2800f9390180SMitsuru IWASAKI 2801f9390180SMitsuru IWASAKI error = 0; 2802f9390180SMitsuru IWASAKI switch (cmd) { 2803f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2804f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2805f9390180SMitsuru IWASAKI if (sc == NULL) { 2806f9390180SMitsuru IWASAKI error = EINVAL; 2807f9390180SMitsuru IWASAKI goto out; 2808f9390180SMitsuru IWASAKI } 2809f9390180SMitsuru IWASAKI 2810f9390180SMitsuru IWASAKI va_start(ap, arg); 2811f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2812f9390180SMitsuru IWASAKI va_end(ap); 2813f9390180SMitsuru IWASAKI 2814f9390180SMitsuru IWASAKI switch (state) { 2815f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2816f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2817f9390180SMitsuru IWASAKI break; 2818f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2819f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2820f9390180SMitsuru IWASAKI break; 2821f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2822f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2823f9390180SMitsuru IWASAKI break; 2824f9390180SMitsuru IWASAKI default: 2825f9390180SMitsuru IWASAKI error = EINVAL; 2826f9390180SMitsuru IWASAKI goto out; 2827f9390180SMitsuru IWASAKI } 2828f9390180SMitsuru IWASAKI 2829f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2830f9390180SMitsuru IWASAKI break; 2831f9390180SMitsuru IWASAKI default: 2832f9390180SMitsuru IWASAKI error = EINVAL; 2833f9390180SMitsuru IWASAKI goto out; 2834f9390180SMitsuru IWASAKI } 2835f9390180SMitsuru IWASAKI 2836f9390180SMitsuru IWASAKI out: 2837f9390180SMitsuru IWASAKI return (error); 2838f9390180SMitsuru IWASAKI } 2839f9390180SMitsuru IWASAKI 2840f9390180SMitsuru IWASAKI static void 2841f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2842f9390180SMitsuru IWASAKI { 2843be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 28446c407052SMitsuru IWASAKI return; 2845f9390180SMitsuru IWASAKI 2846f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2847f9390180SMitsuru IWASAKI } 2848f9390180SMitsuru IWASAKI 2849f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2850