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"); 620a91c5fa8SNate Lawson if (device_get_flags(child)) 621a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child)); 6222c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child); 62315e32d5dSMike Smith 62415e32d5dSMike Smith return (retval); 62515e32d5dSMike Smith } 62615e32d5dSMike Smith 62763600cc3SNate Lawson /* Location hint for devctl(8) */ 62863600cc3SNate Lawson static int 629247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 630247648afSTakanori Watanabe size_t buflen) 631247648afSTakanori Watanabe { 632247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 633247648afSTakanori Watanabe 634247648afSTakanori Watanabe if (dinfo->ad_handle) 635d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 636247648afSTakanori Watanabe else 637d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 638247648afSTakanori Watanabe return (0); 639247648afSTakanori Watanabe } 640247648afSTakanori Watanabe 64163600cc3SNate Lawson /* PnP information for devctl(8) */ 64263600cc3SNate Lawson static int 643247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 644247648afSTakanori Watanabe size_t buflen) 645247648afSTakanori Watanabe { 646247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 64763600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 64863600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 649247648afSTakanori Watanabe char *end; 650247648afSTakanori Watanabe int error; 651247648afSTakanori Watanabe 652247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 653247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 654247648afSTakanori Watanabe if (error) 655d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 656247648afSTakanori Watanabe else 657247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 658247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 659d40c0f53SNate Lawson adinfo->HardwareId.Value : "none", 66063600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 66163600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 662247648afSTakanori Watanabe if (adinfo) 663247648afSTakanori Watanabe AcpiOsFree(adinfo); 664247648afSTakanori Watanabe 665247648afSTakanori Watanabe return (0); 666247648afSTakanori Watanabe } 667247648afSTakanori Watanabe 668247648afSTakanori Watanabe /* 66915e32d5dSMike Smith * Handle per-device ivars 67015e32d5dSMike Smith */ 67115e32d5dSMike Smith static int 67215e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 67315e32d5dSMike Smith { 67415e32d5dSMike Smith struct acpi_device *ad; 67515e32d5dSMike Smith 67615e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 67715e32d5dSMike Smith printf("device has no ivars\n"); 67815e32d5dSMike Smith return (ENOENT); 67915e32d5dSMike Smith } 68015e32d5dSMike Smith 681be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 68215e32d5dSMike Smith switch(index) { 68315e32d5dSMike Smith case ACPI_IVAR_HANDLE: 68415e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 68515e32d5dSMike Smith break; 68615e32d5dSMike Smith case ACPI_IVAR_MAGIC: 68715e32d5dSMike Smith *(int *)result = ad->ad_magic; 68815e32d5dSMike Smith break; 68915e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 69015e32d5dSMike Smith *(void **)result = ad->ad_private; 69115e32d5dSMike Smith break; 692d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 693d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 694d4b9ff91SNate Lawson break; 69532d18aa5SMike Smith case ISA_IVAR_VENDORID: 69632d18aa5SMike Smith case ISA_IVAR_SERIAL: 69732d18aa5SMike Smith case ISA_IVAR_COMPATID: 69832d18aa5SMike Smith *(int *)result = -1; 69932d18aa5SMike Smith break; 70032d18aa5SMike Smith case ISA_IVAR_LOGICALID: 70132d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 70232d18aa5SMike Smith break; 70315e32d5dSMike Smith default: 70415e32d5dSMike Smith return (ENOENT); 70515e32d5dSMike Smith } 706be2b1797SNate Lawson 70715e32d5dSMike Smith return (0); 70815e32d5dSMike Smith } 70915e32d5dSMike Smith 71015e32d5dSMike Smith static int 71115e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 71215e32d5dSMike Smith { 71315e32d5dSMike Smith struct acpi_device *ad; 71415e32d5dSMike Smith 71515e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 71615e32d5dSMike Smith printf("device has no ivars\n"); 71715e32d5dSMike Smith return (ENOENT); 71815e32d5dSMike Smith } 71915e32d5dSMike Smith 72015e32d5dSMike Smith switch(index) { 72115e32d5dSMike Smith case ACPI_IVAR_HANDLE: 72215e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 72315e32d5dSMike Smith break; 72415e32d5dSMike Smith case ACPI_IVAR_MAGIC: 72515e32d5dSMike Smith ad->ad_magic = (int)value; 72615e32d5dSMike Smith break; 72715e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 72815e32d5dSMike Smith ad->ad_private = (void *)value; 72915e32d5dSMike Smith break; 730d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 731d4b9ff91SNate Lawson ad->ad_flags = (int)value; 732d4b9ff91SNate Lawson break; 73315e32d5dSMike Smith default: 7342ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 73515e32d5dSMike Smith return (ENOENT); 73615e32d5dSMike Smith } 737be2b1797SNate Lawson 73815e32d5dSMike Smith return (0); 73915e32d5dSMike Smith } 74015e32d5dSMike Smith 74115e32d5dSMike Smith /* 74215e32d5dSMike Smith * Handle child resource allocation/removal 74315e32d5dSMike Smith */ 74491233413SNate Lawson static struct resource_list * 74591233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 74615e32d5dSMike Smith { 74791233413SNate Lawson struct acpi_device *ad; 74815e32d5dSMike Smith 74991233413SNate Lawson ad = device_get_ivars(child); 75091233413SNate Lawson return (&ad->ad_rl); 75115e32d5dSMike Smith } 75215e32d5dSMike Smith 753adad4744SNate Lawson /* 754adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 755adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 756adad4744SNate Lawson */ 757adad4744SNate Lawson static int 758adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 759adad4744SNate Lawson { 760adad4744SNate Lawson struct resource *res; 761adad4744SNate Lawson struct resource_list *rl; 762adad4744SNate Lawson struct resource_list_entry *rle; 763adad4744SNate Lawson struct rman *rm; 764adad4744SNate Lawson 765adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 766adad4744SNate Lawson SLIST_FOREACH(rle, rl, link) { 767adad4744SNate Lawson if (rle->res != NULL) { 768adad4744SNate Lawson device_printf(dev, "duplicate resource for %lx\n", rle->start); 769adad4744SNate Lawson continue; 770adad4744SNate Lawson } 771adad4744SNate Lawson 772adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 773adad4744SNate Lawson switch (rle->type) { 774adad4744SNate Lawson case SYS_RES_IOPORT: 775adad4744SNate Lawson rm = &acpi_rman_io; 776adad4744SNate Lawson break; 777adad4744SNate Lawson case SYS_RES_MEMORY: 778adad4744SNate Lawson rm = &acpi_rman_mem; 779adad4744SNate Lawson break; 780adad4744SNate Lawson default: 781adad4744SNate Lawson continue; 782adad4744SNate Lawson } 783adad4744SNate Lawson 784adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 785adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 786adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 787adad4744SNate Lawson if (res != NULL) { 788adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 789adad4744SNate Lawson rle->res = res; 790adad4744SNate Lawson } else 791adad4744SNate Lawson device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 792adad4744SNate Lawson rle->start, rle->count, rle->type); 793adad4744SNate Lawson } 794adad4744SNate Lawson return (0); 795adad4744SNate Lawson } 796adad4744SNate Lawson 797adad4744SNate Lawson /* Find if we manage a given resource. */ 798adad4744SNate Lawson static struct resource_list_entry * 799adad4744SNate Lawson acpi_sysres_find(device_t dev, int type, u_long addr) 800adad4744SNate Lawson { 801adad4744SNate Lawson struct resource_list *rl; 802adad4744SNate Lawson struct resource_list_entry *rle; 803adad4744SNate Lawson 804adad4744SNate Lawson ACPI_SERIAL_ASSERT(acpi); 805adad4744SNate Lawson 806adad4744SNate Lawson /* We only consider IO and memory resources for our pool. */ 807adad4744SNate Lawson rle = NULL; 808adad4744SNate Lawson if (type != SYS_RES_IOPORT && type != SYS_RES_MEMORY) 809adad4744SNate Lawson goto out; 810adad4744SNate Lawson 811adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 812adad4744SNate Lawson SLIST_FOREACH(rle, rl, link) { 813adad4744SNate Lawson if (type == rle->type && addr >= rle->start && 814adad4744SNate Lawson addr < rle->start + rle->count) 815adad4744SNate Lawson break; 816adad4744SNate Lawson } 817adad4744SNate Lawson 818adad4744SNate Lawson out: 819adad4744SNate Lawson return (rle); 820adad4744SNate Lawson } 821adad4744SNate Lawson 82215e32d5dSMike Smith static struct resource * 82315e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 82415e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 82515e32d5dSMike Smith { 82695957f62SJohn Baldwin ACPI_RESOURCE ares; 82715e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 82815e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 82991233413SNate Lawson struct resource_list_entry *rle; 83091233413SNate Lawson struct resource *res; 83191233413SNate Lawson struct rman *rm; 83215e32d5dSMike Smith 83315e2f34fSNate Lawson res = NULL; 83415e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 83515e2f34fSNate Lawson 83691233413SNate Lawson /* 83791233413SNate Lawson * If this is an allocation of the "default" range for a given RID, and 83891233413SNate Lawson * we know what the resources for this device are (i.e., they're on the 83991233413SNate Lawson * child's resource list), use those start/end values. 84091233413SNate Lawson */ 84191233413SNate Lawson if (start == 0UL && end == ~0UL) { 84291233413SNate Lawson rle = resource_list_find(rl, type, *rid); 84391233413SNate Lawson if (rle == NULL) 84415e2f34fSNate Lawson goto out; 84591233413SNate Lawson start = rle->start; 84691233413SNate Lawson end = rle->end; 84791233413SNate Lawson count = rle->count; 84891233413SNate Lawson } 84991233413SNate Lawson 85091233413SNate Lawson /* If we don't manage this address, pass the request up to the parent. */ 851adad4744SNate Lawson rle = acpi_sysres_find(bus, type, start); 85291233413SNate Lawson if (rle == NULL) { 85395957f62SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 85495957f62SJohn Baldwin start, end, count, flags); 85595957f62SJohn Baldwin } else { 85691233413SNate Lawson 85791233413SNate Lawson /* We only handle memory and IO resources through rman. */ 85891233413SNate Lawson switch (type) { 85991233413SNate Lawson case SYS_RES_IOPORT: 86091233413SNate Lawson rm = &acpi_rman_io; 86191233413SNate Lawson break; 86291233413SNate Lawson case SYS_RES_MEMORY: 86391233413SNate Lawson rm = &acpi_rman_mem; 86491233413SNate Lawson break; 86591233413SNate Lawson default: 86691233413SNate Lawson panic("acpi_alloc_resource: invalid res type %d", type); 86791233413SNate Lawson } 86891233413SNate Lawson 86991233413SNate Lawson /* If we do know it, allocate it from the local pool. */ 87095957f62SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 87195957f62SJohn Baldwin child); 87291233413SNate Lawson if (res == NULL) 87315e2f34fSNate Lawson goto out; 87491233413SNate Lawson 875a3236570SWarner Losh /* Copy the bus tag and handle from the pre-allocated resource. */ 87691233413SNate Lawson rman_set_bustag(res, rman_get_bustag(rle->res)); 877ed67d9deSWarner Losh rman_set_bushandle(res, rman_get_start(res)); 87891233413SNate Lawson 87991233413SNate Lawson /* If requested, activate the resource using the parent's method. */ 88095957f62SJohn Baldwin if (flags & RF_ACTIVE) 88191233413SNate Lawson if (bus_activate_resource(child, type, *rid, res) != 0) { 88291233413SNate Lawson rman_release_resource(res); 88315e2f34fSNate Lawson res = NULL; 88415e2f34fSNate Lawson goto out; 88591233413SNate Lawson } 88695957f62SJohn Baldwin } 88791233413SNate Lawson 88895957f62SJohn Baldwin if (res != NULL && device_get_parent(child) == bus) 88995957f62SJohn Baldwin switch (type) { 89095957f62SJohn Baldwin case SYS_RES_IRQ: 89195957f62SJohn Baldwin /* 89295957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 89395957f62SJohn Baldwin * configure the interrupt associated with a device when we 89495957f62SJohn Baldwin * parse the resources but have to defer it until a driver 89595957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 89695957f62SJohn Baldwin * 89795957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 89895957f62SJohn Baldwin */ 89995957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 90095957f62SJohn Baldwin acpi_config_intr(child, &ares); 90195957f62SJohn Baldwin break; 90295957f62SJohn Baldwin } 90315e2f34fSNate Lawson 90415e2f34fSNate Lawson out: 90515e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 90691233413SNate Lawson return (res); 90715e32d5dSMike Smith } 90815e32d5dSMike Smith 90915e32d5dSMike Smith static int 91091233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 91191233413SNate Lawson struct resource *r) 91215e32d5dSMike Smith { 91391233413SNate Lawson int ret; 91415e32d5dSMike Smith 91515e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 91615e2f34fSNate Lawson 91791233413SNate Lawson /* 91891233413SNate Lawson * If we know about this address, deactivate it and release it to the 91991233413SNate Lawson * local pool. If we don't, pass this request up to the parent. 92091233413SNate Lawson */ 921adad4744SNate Lawson if (acpi_sysres_find(bus, type, rman_get_start(r)) == NULL) { 92291233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 92391233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 92491233413SNate Lawson if (ret != 0) 92515e2f34fSNate Lawson goto out; 92691233413SNate Lawson } 92791233413SNate Lawson ret = rman_release_resource(r); 92891233413SNate Lawson } else 92991233413SNate Lawson ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 93091233413SNate Lawson 93115e2f34fSNate Lawson out: 93215e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 93391233413SNate Lawson return (ret); 93415e32d5dSMike Smith } 93515e32d5dSMike Smith 936dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 937dc750869SNate Lawson struct resource * 938dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 939dc750869SNate Lawson { 940dc750869SNate Lawson int type; 941dc750869SNate Lawson 942dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 943dc750869SNate Lawson gas->RegisterBitWidth < 8) 944dc750869SNate Lawson return (NULL); 945dc750869SNate Lawson 946dc750869SNate Lawson switch (gas->AddressSpaceId) { 947dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 948dc750869SNate Lawson type = SYS_RES_MEMORY; 949dc750869SNate Lawson break; 950dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 951dc750869SNate Lawson type = SYS_RES_IOPORT; 952dc750869SNate Lawson break; 953dc750869SNate Lawson default: 954dc750869SNate Lawson return (NULL); 955dc750869SNate Lawson } 956dc750869SNate Lawson 957dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 9585f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 959dc750869SNate Lawson } 960dc750869SNate Lawson 961c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 9621e4925e8SNate Lawson static uint32_t 96332d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 964bc0f2195SMike Smith { 9651e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9661e4925e8SNate Lawson ACPI_BUFFER buf; 967bc0f2195SMike Smith ACPI_HANDLE h; 968bc0f2195SMike Smith ACPI_STATUS error; 969bc0f2195SMike Smith u_int32_t pnpid; 97032d18aa5SMike Smith 971b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 97232d18aa5SMike Smith 97332d18aa5SMike Smith pnpid = 0; 974bd189fe7SNate Lawson buf.Pointer = NULL; 975bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 976bd189fe7SNate Lawson 9776fca9360SNate Lawson /* Fetch and validate the HID. */ 97832d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 979bd189fe7SNate Lawson goto out; 9806fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9816fca9360SNate Lawson if (ACPI_FAILURE(error)) 982bd189fe7SNate Lawson goto out; 9831e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 98432d18aa5SMike Smith 9851e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 9861e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 987be2b1797SNate Lawson 988bd189fe7SNate Lawson out: 989bd189fe7SNate Lawson if (buf.Pointer != NULL) 9901e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 99132d18aa5SMike Smith return_VALUE (pnpid); 99232d18aa5SMike Smith } 99332d18aa5SMike Smith 9941e4925e8SNate Lawson static int 9951e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 996b2566207STakanori Watanabe { 9971e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9981e4925e8SNate Lawson ACPI_BUFFER buf; 999b2566207STakanori Watanabe ACPI_HANDLE h; 1000b2566207STakanori Watanabe ACPI_STATUS error; 10011e4925e8SNate Lawson uint32_t *pnpid; 10021e4925e8SNate Lawson int valid, i; 1003b2566207STakanori Watanabe 1004b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1005b2566207STakanori Watanabe 10061e4925e8SNate Lawson pnpid = cids; 10071e4925e8SNate Lawson valid = 0; 1008bd189fe7SNate Lawson buf.Pointer = NULL; 1009bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1010bd189fe7SNate Lawson 10111e4925e8SNate Lawson /* Fetch and validate the CID */ 1012b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 1013bd189fe7SNate Lawson goto out; 10141e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10151e4925e8SNate Lawson if (ACPI_FAILURE(error)) 1016bd189fe7SNate Lawson goto out; 10171e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 10181e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 1019b2566207STakanori Watanabe goto out; 1020b2566207STakanori Watanabe 10211e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 10221e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 10231e4925e8SNate Lawson for (i = 0; i < count; i++) { 10241e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 10251e4925e8SNate Lawson continue; 10261e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 10271e4925e8SNate Lawson valid++; 1028b2566207STakanori Watanabe } 1029b2566207STakanori Watanabe 10301e4925e8SNate Lawson out: 1031bd189fe7SNate Lawson if (buf.Pointer != NULL) 10321e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 10331e4925e8SNate Lawson return_VALUE (valid); 10341e4925e8SNate Lawson } 1035b2566207STakanori Watanabe 1036ce619b2eSNate Lawson static char * 1037ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1038ce619b2eSNate Lawson { 1039ce619b2eSNate Lawson ACPI_HANDLE h; 1040ce619b2eSNate Lawson int i; 1041ce619b2eSNate Lawson 1042ce619b2eSNate Lawson h = acpi_get_handle(dev); 1043ce619b2eSNate Lawson if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1044ce619b2eSNate Lawson return (NULL); 1045ce619b2eSNate Lawson 1046ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1047ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1048ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1049ce619b2eSNate Lawson return (ids[i]); 1050ce619b2eSNate Lawson } 1051ce619b2eSNate Lawson return (NULL); 1052ce619b2eSNate Lawson } 1053ce619b2eSNate Lawson 1054ce619b2eSNate Lawson static ACPI_STATUS 1055ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1056ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1057ce619b2eSNate Lawson { 1058ce619b2eSNate Lawson ACPI_HANDLE h; 1059ce619b2eSNate Lawson 1060df8d2a32SNate Lawson if (dev == NULL) 1061df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1062df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1063ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1064ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1065ce619b2eSNate Lawson } 1066ce619b2eSNate Lawson 1067df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1068df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1069df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1070df8d2a32SNate Lawson void *arg; 1071df8d2a32SNate Lawson ACPI_HANDLE parent; 1072df8d2a32SNate Lawson }; 1073df8d2a32SNate Lawson 1074ce619b2eSNate Lawson static ACPI_STATUS 1075df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1076df8d2a32SNate Lawson { 1077df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1078df8d2a32SNate Lawson device_t dev, old_dev; 1079df8d2a32SNate Lawson ACPI_STATUS status; 1080df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1081df8d2a32SNate Lawson 1082df8d2a32SNate Lawson /* 1083df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1084df8d2a32SNate Lawson * the parent where the scan began. 1085df8d2a32SNate Lawson */ 1086df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1087df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1088df8d2a32SNate Lawson return (AE_OK); 1089df8d2a32SNate Lawson 1090df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1091df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1092df8d2a32SNate Lawson return (AE_OK); 1093df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1094df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1095df8d2a32SNate Lawson return (AE_OK); 1096df8d2a32SNate Lawson 1097df8d2a32SNate Lawson /* 1098df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1099df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1100df8d2a32SNate Lawson */ 1101df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1102df8d2a32SNate Lawson dev = old_dev; 1103df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1104df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1105df8d2a32SNate Lawson return (status); 1106df8d2a32SNate Lawson 1107df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1108df8d2a32SNate Lawson if (old_dev != NULL) { 1109df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1110df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1111df8d2a32SNate Lawson } 1112df8d2a32SNate Lawson 1113df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1114df8d2a32SNate Lawson if (dev != NULL) 1115df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1116df8d2a32SNate Lawson 1117df8d2a32SNate Lawson return (AE_OK); 1118df8d2a32SNate Lawson } 1119df8d2a32SNate Lawson 1120df8d2a32SNate Lawson static ACPI_STATUS 1121df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1122df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1123ce619b2eSNate Lawson { 1124ce619b2eSNate Lawson ACPI_HANDLE h; 1125df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1126ce619b2eSNate Lawson 1127df8d2a32SNate Lawson if (acpi_disabled("children")) 1128df8d2a32SNate Lawson return (AE_OK); 1129df8d2a32SNate Lawson 1130df8d2a32SNate Lawson if (dev == NULL) 1131df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1132df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1133ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1134df8d2a32SNate Lawson ctx.user_fn = user_fn; 1135df8d2a32SNate Lawson ctx.arg = arg; 1136df8d2a32SNate Lawson ctx.parent = h; 1137df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 1138df8d2a32SNate Lawson acpi_device_scan_cb, &ctx, NULL)); 1139ce619b2eSNate Lawson } 1140ce619b2eSNate Lawson 114132d18aa5SMike Smith static int 114232d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 114332d18aa5SMike Smith { 11441e4925e8SNate Lawson int result, cid_count, i; 11451e4925e8SNate Lawson uint32_t lid, cids[8]; 1146bc0f2195SMike Smith 1147b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1148bc0f2195SMike Smith 1149bc0f2195SMike Smith /* 1150bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1151bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1152bc0f2195SMike Smith * that to happen, so don't ever return it. 1153bc0f2195SMike Smith */ 1154bc0f2195SMike Smith result = ENXIO; 1155bc0f2195SMike Smith 1156be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1157b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 11581e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1159bc0f2195SMike Smith while (ids && ids->ip_id) { 11601e4925e8SNate Lawson if (lid == ids->ip_id) { 1161bc0f2195SMike Smith result = 0; 1162bc0f2195SMike Smith goto out; 1163bc0f2195SMike Smith } 11641e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 11651e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 11661e4925e8SNate Lawson result = 0; 11671e4925e8SNate Lawson goto out; 11681e4925e8SNate Lawson } 11691e4925e8SNate Lawson } 1170bc0f2195SMike Smith ids++; 1171bc0f2195SMike Smith } 1172be2b1797SNate Lawson 1173bc0f2195SMike Smith out: 11749e0dd54fSNate Lawson if (result == 0 && ids->ip_desc) 11759e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc); 11769e0dd54fSNate Lawson 1177bc0f2195SMike Smith return_VALUE (result); 1178bc0f2195SMike Smith } 1179bc0f2195SMike Smith 1180bc0f2195SMike Smith /* 118115e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 118215e32d5dSMike Smith * 1183be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1184be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 118515e32d5dSMike Smith */ 118615e32d5dSMike Smith static void 118715e32d5dSMike Smith acpi_probe_children(device_t bus) 118815e32d5dSMike Smith { 118915e32d5dSMike Smith ACPI_HANDLE parent; 1190be2b1797SNate Lawson ACPI_STATUS status; 119115e32d5dSMike Smith int i; 119291233413SNate Lawson static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 119315e32d5dSMike Smith 1194b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 11950ae55423SMike Smith 119615e32d5dSMike Smith /* 119715e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1198edc13633SNate Lawson * we find. We also probe/attach any early devices. 119915e32d5dSMike Smith * 120015e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1201be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1202be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1203be2b1797SNate Lawson * devices as they appear, which might be smarter.) 120415e32d5dSMike Smith */ 12054c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1206be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1207be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1208be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1209be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1210be2b1797SNate Lawson bus, NULL); 1211be2b1797SNate Lawson } 1212be2b1797SNate Lawson } 121315e32d5dSMike Smith 1214adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1215adad4744SNate Lawson acpi_sysres_alloc(bus); 1216adad4744SNate Lawson 1217edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1218edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1219edc13633SNate Lawson bus_generic_probe(bus); 1220edc13633SNate Lawson 1221edc13633SNate Lawson /* Probe/attach all children, created staticly and from the namespace. */ 12224c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 122315e32d5dSMike Smith bus_generic_attach(bus); 122415e32d5dSMike Smith 122515e32d5dSMike Smith /* 122615e32d5dSMike Smith * Some of these children may have attached others as part of their attach 122715e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 122815e32d5dSMike Smith */ 12294c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 123015e32d5dSMike Smith bus_generic_attach(bus); 12310ae55423SMike Smith 123288a79fc0SNate Lawson /* Attach wake sysctls. */ 12335c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 123488a79fc0SNate Lawson 1235bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 12360ae55423SMike Smith return_VOID; 123715e32d5dSMike Smith } 123815e32d5dSMike Smith 1239b82ca9a9SNate Lawson /* 1240b82ca9a9SNate Lawson * Determine the probe order for a given device and return non-zero if it 1241b82ca9a9SNate Lawson * should be attached immediately. 1242b82ca9a9SNate Lawson */ 124391233413SNate Lawson static int 1244b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 124591233413SNate Lawson { 124691233413SNate Lawson int ret; 124791233413SNate Lawson 1248b82ca9a9SNate Lawson /* 1249b82ca9a9SNate Lawson * 1. I/O port and memory system resource holders 1250b82ca9a9SNate Lawson * 2. Embedded controllers (to handle early accesses) 1251b82ca9a9SNate Lawson */ 125291233413SNate Lawson ret = 0; 125391233413SNate Lawson if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) { 125491233413SNate Lawson *order = 1; 125591233413SNate Lawson ret = 1; 1256b82ca9a9SNate Lawson } else if (acpi_MatchHid(handle, "PNP0C09")) { 125791233413SNate Lawson *order = 2; 125891233413SNate Lawson ret = 1; 125991233413SNate Lawson } 126091233413SNate Lawson 126191233413SNate Lawson return (ret); 126291233413SNate Lawson } 126391233413SNate Lawson 126415e32d5dSMike Smith /* 126515e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 126615e32d5dSMike Smith * it. 126715e32d5dSMike Smith */ 126815e32d5dSMike Smith static ACPI_STATUS 126915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 127015e32d5dSMike Smith { 127115e32d5dSMike Smith ACPI_OBJECT_TYPE type; 127291233413SNate Lawson device_t child, bus; 127391233413SNate Lawson int order, probe_now; 127415e32d5dSMike Smith 1275b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 12760ae55423SMike Smith 1277be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 12780ae55423SMike Smith if (acpi_avoid(handle)) 12790ae55423SMike Smith return_ACPI_STATUS (AE_OK); 12800ae55423SMike Smith 128191233413SNate Lawson bus = (device_t)context; 1282b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 128315e32d5dSMike Smith switch (type) { 128415e32d5dSMike Smith case ACPI_TYPE_DEVICE: 128515e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 128615e32d5dSMike Smith case ACPI_TYPE_THERMAL: 128715e32d5dSMike Smith case ACPI_TYPE_POWER: 12880ae55423SMike Smith if (acpi_disabled("children")) 12890ae55423SMike Smith break; 1290be2b1797SNate Lawson 129115e32d5dSMike Smith /* 129215e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 1293b82ca9a9SNate Lawson * so that the probe/attach passes will run breadth-first. Orders 1294b82ca9a9SNate Lawson * less than 10 are reserved for special objects (i.e., system 1295b82ca9a9SNate Lawson * resources). Larger values are used for all other devices. 129615e32d5dSMike Smith */ 1297be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1298be2b1797SNate Lawson acpi_name(handle))); 1299b82ca9a9SNate Lawson order = (level + 1) * 10; 1300b82ca9a9SNate Lawson probe_now = acpi_probe_order(handle, &order); 130191233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1302bc0f2195SMike Smith if (child == NULL) 1303bc0f2195SMike Smith break; 130459a890e6SNate Lawson 130559a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 130615e32d5dSMike Smith acpi_set_handle(child, handle); 130759a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1308bc0f2195SMike Smith 1309bc0f2195SMike Smith /* 1310b519f9d6SMike Smith * Check that the device is present. If it's not present, 1311b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1312b519f9d6SMike Smith * the handle, but we don't probe it). 1313b519f9d6SMike Smith */ 1314be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1315b519f9d6SMike Smith device_disable(child); 1316b519f9d6SMike Smith break; 1317b519f9d6SMike Smith } 1318b519f9d6SMike Smith 1319b519f9d6SMike Smith /* 1320bc0f2195SMike Smith * Get the device's resource settings and attach them. 1321bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1322bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1323bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1324bc0f2195SMike Smith * device not to have any resources. 1325bc0f2195SMike Smith */ 132672ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1327bc0f2195SMike Smith 1328f504b607SNate Lawson /* If order was overridden, probe/attach now rather than later. */ 132991233413SNate Lawson if (probe_now) 133091233413SNate Lawson device_probe_and_attach(child); 13310ae55423SMike Smith break; 133215e32d5dSMike Smith } 133315e32d5dSMike Smith } 1334be2b1797SNate Lawson 13350ae55423SMike Smith return_ACPI_STATUS (AE_OK); 133615e32d5dSMike Smith } 133715e32d5dSMike Smith 133859a890e6SNate Lawson /* 133959a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 134059a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 134159a890e6SNate Lawson */ 134259a890e6SNate Lawson void 134359a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 134459a890e6SNate Lawson { 134559a890e6SNate Lawson } 134659a890e6SNate Lawson 134715e32d5dSMike Smith static void 134815e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 134915e32d5dSMike Smith { 1350d33f4987STakanori Watanabe ACPI_STATUS status; 1351eeb3a05fSNate Lawson 1352eeb3a05fSNate Lawson /* 135380f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 135480f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 135580f0e4c2SNate Lawson * an AP. 1356eeb3a05fSNate Lawson */ 1357eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1358904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1359904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1360904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1361904bf0c2SNate Lawson AcpiFormatException(status)); 1362904bf0c2SNate Lawson return; 1363904bf0c2SNate Lawson } 1364eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 136555398047SNate Lawson ACPI_DISABLE_IRQS(); 1366865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1367be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1368bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 136915e32d5dSMike Smith } else { 137015e32d5dSMike Smith DELAY(1000000); 137115e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 137215e32d5dSMike Smith } 1373ba36768bSNate Lawson } else if (panicstr == NULL) { 137480f0e4c2SNate Lawson printf("Shutting down ACPI\n"); 137580f0e4c2SNate Lawson AcpiTerminate(); 137680f0e4c2SNate Lawson } 137715e32d5dSMike Smith } 137815e32d5dSMike Smith 137913d4f7baSMitsuru IWASAKI static void 138013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 138113d4f7baSMitsuru IWASAKI { 138213d4f7baSMitsuru IWASAKI static int first_time = 1; 138313d4f7baSMitsuru IWASAKI 138413d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1385be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 138659ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 138713d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 138833febf93SNate Lawson acpi_event_power_button_sleep, sc); 1389be2b1797SNate Lawson if (first_time) 13906c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 139113d4f7baSMitsuru IWASAKI } 1392be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 139359ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 139413d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 139533febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1396be2b1797SNate Lawson if (first_time) 13976c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 139813d4f7baSMitsuru IWASAKI } 139913d4f7baSMitsuru IWASAKI 140013d4f7baSMitsuru IWASAKI first_time = 0; 140113d4f7baSMitsuru IWASAKI } 140213d4f7baSMitsuru IWASAKI 140315e32d5dSMike Smith /* 1404c5ba0be4SMike Smith * Returns true if the device is actually present and should 1405c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1406c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1407c5ba0be4SMike Smith */ 1408c5ba0be4SMike Smith BOOLEAN 1409c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1410c5ba0be4SMike Smith { 14111e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1412c5ba0be4SMike Smith ACPI_HANDLE h; 14131e4925e8SNate Lawson ACPI_BUFFER buf; 1414c5ba0be4SMike Smith ACPI_STATUS error; 14151e4925e8SNate Lawson int ret; 1416c5ba0be4SMike Smith 14171e4925e8SNate Lawson ret = FALSE; 1418c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1419c5ba0be4SMike Smith return (FALSE); 14201e4925e8SNate Lawson buf.Pointer = NULL; 14211e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14226fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14236fca9360SNate Lawson if (ACPI_FAILURE(error)) 1424c5ba0be4SMike Smith return (FALSE); 14251e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1426be2b1797SNate Lawson 1427be2b1797SNate Lawson /* If no _STA method, must be present */ 14281e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 14291e4925e8SNate Lawson ret = TRUE; 1430be2b1797SNate Lawson 1431be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 143217dbe0f7SNate Lawson if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus)) 14331e4925e8SNate Lawson ret = TRUE; 1434be2b1797SNate Lawson 14351e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14361e4925e8SNate Lawson return (ret); 1437c5ba0be4SMike Smith } 1438c5ba0be4SMike Smith 1439c5ba0be4SMike Smith /* 1440b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1441b53f2771SMike Smith */ 1442b53f2771SMike Smith BOOLEAN 1443b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1444b53f2771SMike Smith { 14451e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1446b53f2771SMike Smith ACPI_HANDLE h; 14471e4925e8SNate Lawson ACPI_BUFFER buf; 1448b53f2771SMike Smith ACPI_STATUS error; 14491e4925e8SNate Lawson int ret; 1450b53f2771SMike Smith 14511e4925e8SNate Lawson ret = FALSE; 1452b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1453b53f2771SMike Smith return (FALSE); 14541e4925e8SNate Lawson buf.Pointer = NULL; 14551e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14566fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14576fca9360SNate Lawson if (ACPI_FAILURE(error)) 1458b53f2771SMike Smith return (FALSE); 14591e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1460be2b1797SNate Lawson 1461be2b1797SNate Lawson /* If no _STA method, must be present */ 14621e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 14631e4925e8SNate Lawson ret = TRUE; 1464be2b1797SNate Lawson 146517dbe0f7SNate Lawson /* Return true for 'present', 'battery present', and 'functioning' */ 146617dbe0f7SNate Lawson if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus)) 14671e4925e8SNate Lawson ret = TRUE; 1468be2b1797SNate Lawson 14691e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14701e4925e8SNate Lawson return (ret); 1471b53f2771SMike Smith } 1472b53f2771SMike Smith 1473b53f2771SMike Smith /* 147491233413SNate Lawson * Match a HID string against a handle 147515e32d5dSMike Smith */ 1476ce619b2eSNate Lawson static BOOLEAN 1477ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 147815e32d5dSMike Smith { 14791e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 14801e4925e8SNate Lawson ACPI_BUFFER buf; 148115e32d5dSMike Smith ACPI_STATUS error; 14821e4925e8SNate Lawson int ret, i; 148315e32d5dSMike Smith 14841e4925e8SNate Lawson ret = FALSE; 148591233413SNate Lawson if (hid == NULL || h == NULL) 148691233413SNate Lawson return (ret); 14871e4925e8SNate Lawson buf.Pointer = NULL; 14881e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14896fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14906fca9360SNate Lawson if (ACPI_FAILURE(error)) 149191233413SNate Lawson return (ret); 14921e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1493be2b1797SNate Lawson 1494c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1495c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 14961e4925e8SNate Lawson ret = TRUE; 1497c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 14981e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 14991e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 15001e4925e8SNate Lawson ret = TRUE; 15011e4925e8SNate Lawson break; 15021e4925e8SNate Lawson } 15031e4925e8SNate Lawson } 15041e4925e8SNate Lawson } 1505be2b1797SNate Lawson 15061e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 15071e4925e8SNate Lawson return (ret); 150815e32d5dSMike Smith } 150915e32d5dSMike Smith 151015e32d5dSMike Smith /* 1511c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1512c5ba0be4SMike Smith * or one if its parents. 1513c5ba0be4SMike Smith */ 1514c5ba0be4SMike Smith ACPI_STATUS 1515c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1516c5ba0be4SMike Smith { 1517c5ba0be4SMike Smith ACPI_HANDLE r; 1518c5ba0be4SMike Smith ACPI_STATUS status; 1519c5ba0be4SMike Smith 1520be2b1797SNate Lawson /* Walk back up the tree to the root */ 1521c5ba0be4SMike Smith for (;;) { 1522be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1523be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1524c5ba0be4SMike Smith *result = r; 1525c5ba0be4SMike Smith return (AE_OK); 1526c5ba0be4SMike Smith } 1527bee4aa4aSNate Lawson /* XXX Return error here? */ 1528c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1529c5ba0be4SMike Smith return (AE_OK); 1530b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1531c5ba0be4SMike Smith return (AE_NOT_FOUND); 1532c5ba0be4SMike Smith parent = r; 1533c5ba0be4SMike Smith } 1534c5ba0be4SMike Smith } 1535c5ba0be4SMike Smith 1536eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1537eea17c34SNate Lawson uint32_t 1538eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1539eea17c34SNate Lawson { 1540eea17c34SNate Lawson uint32_t delta; 1541eea17c34SNate Lawson 1542eea17c34SNate Lawson if (end >= start) 1543eea17c34SNate Lawson delta = end - start; 1544eea17c34SNate Lawson else if (AcpiGbl_FADT->TmrValExt == 0) 15458d01ceefSNate Lawson delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1546eea17c34SNate Lawson else 1547eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 1548eea17c34SNate Lawson return (delta); 1549eea17c34SNate Lawson } 1550eea17c34SNate Lawson 1551c5ba0be4SMike Smith /* 1552c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1553c5ba0be4SMike Smith */ 1554c5ba0be4SMike Smith ACPI_BUFFER * 1555c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1556c5ba0be4SMike Smith { 1557c5ba0be4SMike Smith ACPI_BUFFER *buf; 1558c5ba0be4SMike Smith 1559c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1560c5ba0be4SMike Smith return (NULL); 1561c5ba0be4SMike Smith buf->Length = size; 1562c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1563c5ba0be4SMike Smith return (buf); 1564c5ba0be4SMike Smith } 1565c5ba0be4SMike Smith 1566c310653eSNate Lawson ACPI_STATUS 1567cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1568c310653eSNate Lawson { 1569c310653eSNate Lawson ACPI_OBJECT arg1; 1570c310653eSNate Lawson ACPI_OBJECT_LIST args; 1571c310653eSNate Lawson 1572c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1573c310653eSNate Lawson arg1.Integer.Value = number; 1574c310653eSNate Lawson args.Count = 1; 1575c310653eSNate Lawson args.Pointer = &arg1; 1576c310653eSNate Lawson 1577c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1578c310653eSNate Lawson } 1579c310653eSNate Lawson 1580c5ba0be4SMike Smith /* 1581c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1582c5ba0be4SMike Smith */ 1583c5ba0be4SMike Smith ACPI_STATUS 1584cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1585c5ba0be4SMike Smith { 1586be2b1797SNate Lawson ACPI_STATUS status; 1587c5ba0be4SMike Smith ACPI_BUFFER buf; 1588c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1589c5ba0be4SMike Smith 1590c5ba0be4SMike Smith if (handle == NULL) 1591c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 15920c7da7acSJohn Baldwin 15930c7da7acSJohn Baldwin /* 15940c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 15950c7da7acSJohn Baldwin * a method that will return an Integer. 15960c7da7acSJohn Baldwin */ 1597c5ba0be4SMike Smith buf.Pointer = ¶m; 1598c5ba0be4SMike Smith buf.Length = sizeof(param); 1599be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1600be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1601be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1602c5ba0be4SMike Smith *number = param.Integer.Value; 1603be2b1797SNate Lawson else 1604be2b1797SNate Lawson status = AE_TYPE; 1605c5ba0be4SMike Smith } 16060c7da7acSJohn Baldwin 16070c7da7acSJohn Baldwin /* 16080c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 16090c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 16100c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 16110c7da7acSJohn Baldwin * convert it into an Integer as best we can. 16120c7da7acSJohn Baldwin * 16130c7da7acSJohn Baldwin * This is a hack. 16140c7da7acSJohn Baldwin */ 1615be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1616b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1617be2b1797SNate Lawson status = AE_NO_MEMORY; 16180c7da7acSJohn Baldwin } else { 1619be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1620be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1621be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 16220c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 16230c7da7acSJohn Baldwin } 1624f97739daSNate Lawson } 1625be2b1797SNate Lawson return (status); 1626c5ba0be4SMike Smith } 1627c5ba0be4SMike Smith 1628c573e654SMitsuru IWASAKI ACPI_STATUS 1629cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1630c573e654SMitsuru IWASAKI { 1631c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1632dba55fa2SNate Lawson UINT8 *val; 1633c573e654SMitsuru IWASAKI int i; 1634c573e654SMitsuru IWASAKI 1635c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1636c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1637c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1638c573e654SMitsuru IWASAKI return (AE_OK); 1639c573e654SMitsuru IWASAKI } 1640c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1641c573e654SMitsuru IWASAKI return (AE_TYPE); 1642c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1643c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1644be2b1797SNate Lawson 1645c573e654SMitsuru IWASAKI *number = 0; 1646dba55fa2SNate Lawson val = p->Buffer.Pointer; 1647c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1648dba55fa2SNate Lawson *number += val[i] << (i * 8); 1649c573e654SMitsuru IWASAKI return (AE_OK); 1650c573e654SMitsuru IWASAKI } 1651c573e654SMitsuru IWASAKI 1652c5ba0be4SMike Smith /* 1653c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1654c5ba0be4SMike Smith * function for each element. 1655c5ba0be4SMike Smith * 1656c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1657c5ba0be4SMike Smith */ 1658c5ba0be4SMike Smith ACPI_STATUS 1659be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1660be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1661c5ba0be4SMike Smith { 1662c5ba0be4SMike Smith ACPI_OBJECT *comp; 1663c5ba0be4SMike Smith int i; 1664c5ba0be4SMike Smith 1665be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1666c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1667c5ba0be4SMike Smith 1668be2b1797SNate Lawson /* Iterate over components */ 1669be2b1797SNate Lawson i = 0; 1670be2b1797SNate Lawson comp = pkg->Package.Elements; 1671be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1672c5ba0be4SMike Smith func(comp, arg); 1673c5ba0be4SMike Smith 1674c5ba0be4SMike Smith return (AE_OK); 1675c5ba0be4SMike Smith } 1676c5ba0be4SMike Smith 16776f69255bSMike Smith /* 16786f69255bSMike Smith * Find the (index)th resource object in a set. 16796f69255bSMike Smith */ 16806f69255bSMike Smith ACPI_STATUS 16816d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 16826f69255bSMike Smith { 16836d63101aSMike Smith ACPI_RESOURCE *rp; 16846f69255bSMike Smith int i; 16856f69255bSMike Smith 16866d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16876f69255bSMike Smith i = index; 16886d63101aSMike Smith while (i-- > 0) { 1689be2b1797SNate Lawson /* Range check */ 16906d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16916f69255bSMike Smith return (AE_BAD_PARAMETER); 1692be2b1797SNate Lawson 1693be2b1797SNate Lawson /* Check for terminator */ 1694be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16956d63101aSMike Smith return (AE_NOT_FOUND); 1696abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16976f69255bSMike Smith } 16986f69255bSMike Smith if (resp != NULL) 16996d63101aSMike Smith *resp = rp; 1700be2b1797SNate Lawson 17016d63101aSMike Smith return (AE_OK); 17026d63101aSMike Smith } 17036d63101aSMike Smith 17046d63101aSMike Smith /* 17056d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 17066d63101aSMike Smith * 17076d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 17086d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 17096d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 17106d63101aSMike Smith * resources. 17116d63101aSMike Smith */ 17126d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 17136d63101aSMike Smith 17146d63101aSMike Smith ACPI_STATUS 17156d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 17166d63101aSMike Smith { 17176d63101aSMike Smith ACPI_RESOURCE *rp; 17186d63101aSMike Smith void *newp; 17196d63101aSMike Smith 1720be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 17216d63101aSMike Smith if (buf->Pointer == NULL) { 17226d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 17236d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 17246d63101aSMike Smith return (AE_NO_MEMORY); 17256d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 17266d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 17276d63101aSMike Smith rp->Length = 0; 17286d63101aSMike Smith } 17296d63101aSMike Smith if (res == NULL) 17306d63101aSMike Smith return (AE_OK); 17316d63101aSMike Smith 17326d63101aSMike Smith /* 17336d63101aSMike Smith * Scan the current buffer looking for the terminator. 17346d63101aSMike Smith * This will either find the terminator or hit the end 17356d63101aSMike Smith * of the buffer and return an error. 17366d63101aSMike Smith */ 17376d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 17386d63101aSMike Smith for (;;) { 1739be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 17406d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 17416d63101aSMike Smith return (AE_BAD_PARAMETER); 1742be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 17436d63101aSMike Smith break; 1744abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 17456d63101aSMike Smith } 17466d63101aSMike Smith 17476d63101aSMike Smith /* 17486d63101aSMike Smith * Check the size of the buffer and expand if required. 17496d63101aSMike Smith * 17506d63101aSMike Smith * Required size is: 17516d63101aSMike Smith * size of existing resources before terminator + 17526d63101aSMike Smith * size of new resource and header + 17536d63101aSMike Smith * size of terminator. 17546d63101aSMike Smith * 17556d63101aSMike Smith * Note that this loop should really only run once, unless 17566d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 17576d63101aSMike Smith */ 17586d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 17596d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 17606d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 17616d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 17626d63101aSMike Smith return (AE_NO_MEMORY); 17636d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1764a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1765a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 17666d63101aSMike Smith AcpiOsFree(buf->Pointer); 17676d63101aSMike Smith buf->Pointer = newp; 17686d63101aSMike Smith buf->Length += buf->Length; 17696d63101aSMike Smith } 17706d63101aSMike Smith 1771be2b1797SNate Lawson /* Insert the new resource. */ 17726d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 17736d63101aSMike Smith 1774be2b1797SNate Lawson /* And add the terminator. */ 1775abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 17766d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 17776d63101aSMike Smith rp->Length = 0; 17786d63101aSMike Smith 17796f69255bSMike Smith return (AE_OK); 17806f69255bSMike Smith } 1781c5ba0be4SMike Smith 1782da14ac9fSJohn Baldwin /* 1783da14ac9fSJohn Baldwin * Set interrupt model. 1784da14ac9fSJohn Baldwin */ 1785da14ac9fSJohn Baldwin ACPI_STATUS 1786da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1787da14ac9fSJohn Baldwin { 1788da14ac9fSJohn Baldwin 1789c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1790da14ac9fSJohn Baldwin } 1791da14ac9fSJohn Baldwin 1792ece50487SMitsuru IWASAKI static void 1793ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1794ece50487SMitsuru IWASAKI { 1795bee4aa4aSNate Lawson 1796ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1797ece50487SMitsuru IWASAKI } 1798c30382dfSMitsuru IWASAKI 179915e2f34fSNate Lawson enum acpi_sleep_state { 180015e2f34fSNate Lawson ACPI_SS_NONE, 180115e2f34fSNate Lawson ACPI_SS_GPE_SET, 180215e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 180315e2f34fSNate Lawson ACPI_SS_SLP_PREP, 180415e2f34fSNate Lawson ACPI_SS_SLEPT, 180515e2f34fSNate Lawson }; 180615e2f34fSNate Lawson 180715e32d5dSMike Smith /* 180815e32d5dSMike Smith * Set the system sleep state 180915e32d5dSMike Smith * 1810be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 181115e32d5dSMike Smith */ 181215e32d5dSMike Smith ACPI_STATUS 181315e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 181415e32d5dSMike Smith { 181515e2f34fSNate Lawson ACPI_STATUS status; 181656d8cb57SMitsuru IWASAKI UINT8 TypeA; 181756d8cb57SMitsuru IWASAKI UINT8 TypeB; 181815e2f34fSNate Lawson enum acpi_sleep_state slp_state; 181915e32d5dSMike Smith 1820b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 18210ae55423SMike Smith 182215e2f34fSNate Lawson status = AE_OK; 182315e2f34fSNate Lawson ACPI_LOCK(acpi); 182415e2f34fSNate Lawson if (sc->acpi_sleep_disabled) { 18256397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 182615e2f34fSNate Lawson status = AE_ERROR; 182715e2f34fSNate Lawson ACPI_UNLOCK(acpi); 182815e2f34fSNate Lawson printf("acpi: suspend request ignored (not ready yet)\n"); 182915e2f34fSNate Lawson return (status); 183015e2f34fSNate Lawson } 183115e2f34fSNate Lawson sc->acpi_sleep_disabled = 1; 183215e2f34fSNate Lawson ACPI_UNLOCK(acpi); 18336397624dSMitsuru IWASAKI 183415e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 183515e32d5dSMike Smith switch (state) { 183615e32d5dSMike Smith case ACPI_STATE_S1: 18376161544cSTakanori Watanabe case ACPI_STATE_S2: 18386161544cSTakanori Watanabe case ACPI_STATE_S3: 18396161544cSTakanori Watanabe case ACPI_STATE_S4: 1840adad4744SNate Lawson status = AcpiGetSleepTypeData(state, &TypeA, &TypeB); 184198175614SNate Lawson if (status == AE_NOT_FOUND) { 184298175614SNate Lawson device_printf(sc->acpi_dev, 184398175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 184498175614SNate Lawson break; 184598175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1846be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1847be2b1797SNate Lawson AcpiFormatException(status)); 184856d8cb57SMitsuru IWASAKI break; 184956d8cb57SMitsuru IWASAKI } 185056d8cb57SMitsuru IWASAKI 1851a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1852a1fccb47SMitsuru IWASAKI 1853d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 1854d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 185515e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 1856e8b4d56eSNate Lawson 185715e32d5dSMike Smith /* 1858c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 1859c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 186015e32d5dSMike Smith * 1861c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 1862c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 1863c7f88fc3SNate Lawson * bus interface does not provide for this. 186415e32d5dSMike Smith */ 186515e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 186615e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 186715e2f34fSNate Lawson break; 186815e2f34fSNate Lawson } 186915e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 1870b9c780d6SMitsuru IWASAKI 1871be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1872be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1873b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1874b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1875b9c780d6SMitsuru IWASAKI break; 1876b9c780d6SMitsuru IWASAKI } 187715e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 1878b9c780d6SMitsuru IWASAKI 1879be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1880ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1881ff01efb5SMitsuru IWASAKI 18826161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 18836161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 18846161544cSTakanori Watanabe 18856161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1886be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1887964679ceSMitsuru IWASAKI AcpiEnable(); 18886161544cSTakanori Watanabe } else { 1889c7f88fc3SNate Lawson ACPI_DISABLE_IRQS(); 1890adad4744SNate Lawson status = AcpiEnterSleepState(state); 1891be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1892be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1893be2b1797SNate Lawson AcpiFormatException(status)); 1894c30382dfSMitsuru IWASAKI break; 189515e32d5dSMike Smith } 18966161544cSTakanori Watanabe } 189715e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 189815e32d5dSMike Smith break; 189915e32d5dSMike Smith case ACPI_STATE_S5: 190015e32d5dSMike Smith /* 190115e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 190215e32d5dSMike Smith * shutdown handlers. 190315e32d5dSMike Smith */ 190415e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 190515e32d5dSMike Smith break; 190698175614SNate Lawson case ACPI_STATE_S0: 190715e32d5dSMike Smith default: 190815e32d5dSMike Smith status = AE_BAD_PARAMETER; 190915e32d5dSMike Smith break; 191015e32d5dSMike Smith } 1911ece50487SMitsuru IWASAKI 191215e2f34fSNate Lawson /* 191315e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 191415e2f34fSNate Lawson * process. This handles both the error and success cases. 191515e2f34fSNate Lawson */ 191615e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 191715e2f34fSNate Lawson acpi_wake_prep_walk(state); 191815e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 191915e2f34fSNate Lawson } 192015e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLP_PREP) 192115e2f34fSNate Lawson AcpiLeaveSleepState(state); 1922071339e2SNate Lawson if (slp_state >= ACPI_SS_DEV_SUSPEND) 1923071339e2SNate Lawson DEVICE_RESUME(root_bus); 192415e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLEPT) 192515e2f34fSNate Lawson acpi_enable_fixed_events(sc); 192615e2f34fSNate Lawson 192715e2f34fSNate Lawson /* Allow another sleep request after a while. */ 192815e2f34fSNate Lawson if (state != ACPI_STATE_S5) 1929ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1930ece50487SMitsuru IWASAKI 19310ae55423SMike Smith return_ACPI_STATUS (status); 193215e32d5dSMike Smith } 193315e32d5dSMike Smith 1934e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1935e8b4d56eSNate Lawson int 1936e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1937e8b4d56eSNate Lawson { 1938e8b4d56eSNate Lawson struct acpi_prw_data prw; 1939e8b4d56eSNate Lawson 1940e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1941e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1942e8b4d56eSNate Lawson return (ENXIO); 1943e8b4d56eSNate Lawson 1944e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1945e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1946e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1947e8b4d56eSNate Lawson return (ENXIO); 1948e8b4d56eSNate Lawson } 1949e8b4d56eSNate Lawson 1950e8b4d56eSNate Lawson return (0); 1951e8b4d56eSNate Lawson } 1952e8b4d56eSNate Lawson 1953e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1954e8b4d56eSNate Lawson int 1955e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1956e8b4d56eSNate Lawson { 1957e8b4d56eSNate Lawson struct acpi_prw_data prw; 1958e8b4d56eSNate Lawson ACPI_HANDLE handle; 1959e8b4d56eSNate Lawson ACPI_STATUS status; 1960e8b4d56eSNate Lawson int flags; 1961e8b4d56eSNate Lawson 1962d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 1963e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1964e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1965e8b4d56eSNate Lawson return (ENXIO); 1966e8b4d56eSNate Lawson 1967d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 1968e8b4d56eSNate Lawson if (enable) { 1969e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1970e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1971e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1972e8b4d56eSNate Lawson return (ENXIO); 1973e8b4d56eSNate Lawson } 1974d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1975e8b4d56eSNate Lawson } else { 1976e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1977e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1978e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1979e8b4d56eSNate Lawson return (ENXIO); 1980e8b4d56eSNate Lawson } 1981d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1982e8b4d56eSNate Lawson } 1983e8b4d56eSNate Lawson 1984e8b4d56eSNate Lawson return (0); 1985e8b4d56eSNate Lawson } 1986e8b4d56eSNate Lawson 1987d4b9ff91SNate Lawson static int 1988d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 1989e8b4d56eSNate Lawson { 1990e8b4d56eSNate Lawson struct acpi_prw_data prw; 1991d4b9ff91SNate Lawson device_t dev; 1992e8b4d56eSNate Lawson 1993d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 1994e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1995e8b4d56eSNate Lawson return (ENXIO); 1996d4b9ff91SNate Lawson dev = acpi_get_device(handle); 1997e8b4d56eSNate Lawson 1998e8b4d56eSNate Lawson /* 1999d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 2000d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 2001d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 2002d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 2003d4b9ff91SNate Lawson * and set _PSW. 2004e8b4d56eSNate Lawson */ 2005d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 2006cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2007e8b4d56eSNate Lawson if (bootverbose) 2008d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2009d4b9ff91SNate Lawson acpi_name(handle), sstate); 2010d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2011d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 2012d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 2013d4b9ff91SNate Lawson if (bootverbose) 2014d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2015d4b9ff91SNate Lawson acpi_name(handle), sstate); 2016d4b9ff91SNate Lawson } 2017cc85c78cSNate Lawson 2018cc85c78cSNate Lawson return (0); 2019e8b4d56eSNate Lawson } 2020e8b4d56eSNate Lawson 2021d4b9ff91SNate Lawson static int 2022d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2023cc85c78cSNate Lawson { 2024cc85c78cSNate Lawson struct acpi_prw_data prw; 2025d4b9ff91SNate Lawson device_t dev; 2026cc85c78cSNate Lawson 2027cc85c78cSNate Lawson /* 2028d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 2029d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 2030cc85c78cSNate Lawson */ 2031d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2032d4b9ff91SNate Lawson return (ENXIO); 2033d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2034d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2035d4b9ff91SNate Lawson return (0); 2036cc85c78cSNate Lawson 2037d4b9ff91SNate Lawson /* 2038d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 2039d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 2040d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 2041d4b9ff91SNate Lawson */ 2042d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 2043cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2044d4b9ff91SNate Lawson if (bootverbose) 2045d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2046d4b9ff91SNate Lawson } else { 2047d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 2048d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 2049d4b9ff91SNate Lawson if (bootverbose) 2050d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 2051d4b9ff91SNate Lawson acpi_name(handle)); 2052d4b9ff91SNate Lawson } 2053d4b9ff91SNate Lawson 2054e8b4d56eSNate Lawson return (0); 2055e8b4d56eSNate Lawson } 2056e8b4d56eSNate Lawson 2057e8b4d56eSNate Lawson static ACPI_STATUS 2058d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2059e8b4d56eSNate Lawson { 2060d4b9ff91SNate Lawson int sstate; 2061e8b4d56eSNate Lawson 2062d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 2063d4b9ff91SNate Lawson sstate = *(int *)context; 2064d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 2065d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 2066d4b9ff91SNate Lawson else 2067d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 2068e8b4d56eSNate Lawson return (AE_OK); 2069e8b4d56eSNate Lawson } 2070e8b4d56eSNate Lawson 2071d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2072e8b4d56eSNate Lawson static int 2073d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 2074e8b4d56eSNate Lawson { 2075e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 2076e8b4d56eSNate Lawson 2077e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2078d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 2079d4b9ff91SNate Lawson acpi_wake_prep, &sstate, NULL); 2080e8b4d56eSNate Lawson return (0); 2081e8b4d56eSNate Lawson } 2082e8b4d56eSNate Lawson 208388a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 208488a79fc0SNate Lawson static int 208588a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 208688a79fc0SNate Lawson { 208788a79fc0SNate Lawson int error, i, numdevs; 208888a79fc0SNate Lawson device_t *devlist; 208988a79fc0SNate Lawson device_t child; 2090d4b9ff91SNate Lawson ACPI_STATUS status; 209188a79fc0SNate Lawson 209288a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 209388a79fc0SNate Lawson if (error != 0 || numdevs == 0) 209488a79fc0SNate Lawson return (error); 209588a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 209688a79fc0SNate Lawson child = devlist[i]; 2097d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 209888a79fc0SNate Lawson if (!device_is_attached(child)) 209988a79fc0SNate Lawson continue; 2100d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2101d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 210288a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 210388a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 210488a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 210588a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 210688a79fc0SNate Lawson } 210788a79fc0SNate Lawson } 210888a79fc0SNate Lawson free(devlist, M_TEMP); 210988a79fc0SNate Lawson 211088a79fc0SNate Lawson return (0); 211188a79fc0SNate Lawson } 211288a79fc0SNate Lawson 211388a79fc0SNate Lawson /* Enable or disable wake from userland. */ 211488a79fc0SNate Lawson static int 211588a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 211688a79fc0SNate Lawson { 211788a79fc0SNate Lawson int enable, error; 211888a79fc0SNate Lawson device_t dev; 211988a79fc0SNate Lawson 212088a79fc0SNate Lawson dev = (device_t)arg1; 2121d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 212288a79fc0SNate Lawson 212388a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 212488a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 212588a79fc0SNate Lawson return (error); 212688a79fc0SNate Lawson if (enable != 0 && enable != 1) 212788a79fc0SNate Lawson return (EINVAL); 212888a79fc0SNate Lawson 212988a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 213088a79fc0SNate Lawson } 213188a79fc0SNate Lawson 2132e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2133d4b9ff91SNate Lawson int 2134e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2135e8b4d56eSNate Lawson { 2136e8b4d56eSNate Lawson ACPI_STATUS status; 2137e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2138e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2139d4b9ff91SNate Lawson int error, i, power_count; 2140e8b4d56eSNate Lawson 2141e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2142e8b4d56eSNate Lawson return (EINVAL); 2143e8b4d56eSNate Lawson 2144e8b4d56eSNate Lawson /* 2145e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2146e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2147e8b4d56eSNate Lawson */ 2148e8b4d56eSNate Lawson error = EINVAL; 2149e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2150e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2151e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2152e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2153e8b4d56eSNate Lawson return (ENOENT); 2154e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2155e8b4d56eSNate Lawson if (res == NULL) 2156e8b4d56eSNate Lawson return (ENOENT); 2157e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2158e8b4d56eSNate Lawson goto out; 2159e8b4d56eSNate Lawson 2160e8b4d56eSNate Lawson /* 2161e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2162e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2163e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2164e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2165e8b4d56eSNate Lawson */ 2166e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2167e8b4d56eSNate Lawson goto out; 2168e8b4d56eSNate Lawson 2169e8b4d56eSNate Lawson /* 2170e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2171e8b4d56eSNate Lawson */ 2172e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2173e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2174e8b4d56eSNate Lawson /* 2175e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2176e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2177e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2178e8b4d56eSNate Lawson * enabled for the wake event. 2179e8b4d56eSNate Lawson */ 2180e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2181e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2182e8b4d56eSNate Lawson error = 0; 2183e8b4d56eSNate Lawson break; 2184e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2185e8b4d56eSNate Lawson /* 2186e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2187e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2188e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2189e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2190e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2191e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2192e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2193e8b4d56eSNate Lawson * the wake event. 2194e8b4d56eSNate Lawson * 2195e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2196e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2197e8b4d56eSNate Lawson */ 2198e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2199e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2200e8b4d56eSNate Lawson goto out; 2201e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2202e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2203e8b4d56eSNate Lawson goto out; 2204e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2205e8b4d56eSNate Lawson goto out; 2206e8b4d56eSNate Lawson error = 0; 2207e8b4d56eSNate Lawson break; 2208e8b4d56eSNate Lawson default: 2209e8b4d56eSNate Lawson goto out; 2210e8b4d56eSNate Lawson } 2211e8b4d56eSNate Lawson 2212d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 2213d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 2214d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 2215d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2216d4b9ff91SNate Lawson power_count = 0; 2217d4b9ff91SNate Lawson } 2218d4b9ff91SNate Lawson prw->power_res_count = power_count; 2219d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 2220d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 2221e8b4d56eSNate Lawson 2222e8b4d56eSNate Lawson out: 2223e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2224e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2225e8b4d56eSNate Lawson return (error); 2226e8b4d56eSNate Lawson } 2227e8b4d56eSNate Lawson 222815e32d5dSMike Smith /* 222915e32d5dSMike Smith * ACPI Event Handlers 223015e32d5dSMike Smith */ 223115e32d5dSMike Smith 223215e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 223315e32d5dSMike Smith 223415e32d5dSMike Smith static void 223515e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 223615e32d5dSMike Smith { 2237bee4aa4aSNate Lawson 2238b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 223915e32d5dSMike Smith 2240a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 224115e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2242bee4aa4aSNate Lawson 22430ae55423SMike Smith return_VOID; 224415e32d5dSMike Smith } 224515e32d5dSMike Smith 224615e32d5dSMike Smith static void 224715e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 224815e32d5dSMike Smith { 2249bee4aa4aSNate Lawson 2250b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 22510ae55423SMike Smith 2252bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 2253cb9b0d80SMike Smith 22540ae55423SMike Smith return_VOID; 225515e32d5dSMike Smith } 225615e32d5dSMike Smith 225715e32d5dSMike Smith /* 225815e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 225915e32d5dSMike Smith */ 226015e32d5dSMike Smith UINT32 226133febf93SNate Lawson acpi_event_power_button_sleep(void *context) 226215e32d5dSMike Smith { 226315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 226415e32d5dSMike Smith 2265b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22660ae55423SMike Smith 226715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 22680ae55423SMike Smith 2269b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 227015e32d5dSMike Smith } 227115e32d5dSMike Smith 227215e32d5dSMike Smith UINT32 227333febf93SNate Lawson acpi_event_power_button_wake(void *context) 227415e32d5dSMike Smith { 227515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 227615e32d5dSMike Smith 2277b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22780ae55423SMike Smith 227915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 22800ae55423SMike Smith 2281b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 228215e32d5dSMike Smith } 228315e32d5dSMike Smith 228415e32d5dSMike Smith UINT32 228533febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 228615e32d5dSMike Smith { 228715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 228815e32d5dSMike Smith 2289b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22900ae55423SMike Smith 229115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 22920ae55423SMike Smith 2293b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 229415e32d5dSMike Smith } 229515e32d5dSMike Smith 229615e32d5dSMike Smith UINT32 229733febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 229815e32d5dSMike Smith { 229915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 230015e32d5dSMike Smith 2301b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 23020ae55423SMike Smith 230315e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 23040ae55423SMike Smith 2305b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 230615e32d5dSMike Smith } 230715e32d5dSMike Smith 230815e32d5dSMike Smith /* 2309bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 2310bee4aa4aSNate Lawson * use this for single-threaded callers. 231115e32d5dSMike Smith */ 231215e32d5dSMike Smith char * 231315e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 231415e32d5dSMike Smith { 2315bee4aa4aSNate Lawson ACPI_BUFFER buf; 2316bee4aa4aSNate Lawson static char data[256]; 231715e32d5dSMike Smith 2318bee4aa4aSNate Lawson buf.Length = sizeof(data); 2319bee4aa4aSNate Lawson buf.Pointer = data; 2320cb9b0d80SMike Smith 23210a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 2322bee4aa4aSNate Lawson return (data); 2323bee4aa4aSNate Lawson return ("(unknown)"); 232415e32d5dSMike Smith } 232515e32d5dSMike Smith 232615e32d5dSMike Smith /* 232715e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 232815e32d5dSMike Smith * parts of the namespace. 232915e32d5dSMike Smith */ 233015e32d5dSMike Smith int 233115e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 233215e32d5dSMike Smith { 23333c600cfbSJohn Baldwin char *cp, *env, *np; 233415e32d5dSMike Smith int len; 233515e32d5dSMike Smith 233615e32d5dSMike Smith np = acpi_name(handle); 233715e32d5dSMike Smith if (*np == '\\') 233815e32d5dSMike Smith np++; 23393c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 234015e32d5dSMike Smith return (0); 234115e32d5dSMike Smith 2342be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 23433c600cfbSJohn Baldwin cp = env; 234415e32d5dSMike Smith for (;;) { 2345bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 234615e32d5dSMike Smith cp++; 234715e32d5dSMike Smith if (*cp == 0) 234815e32d5dSMike Smith break; 234915e32d5dSMike Smith len = 0; 2350bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 235115e32d5dSMike Smith len++; 2352d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 23533c600cfbSJohn Baldwin freeenv(env); 23540ae55423SMike Smith return(1); 2355d786139cSMaxime Henrion } 23560ae55423SMike Smith cp += len; 23570ae55423SMike Smith } 23583c600cfbSJohn Baldwin freeenv(env); 2359be2b1797SNate Lawson 23600ae55423SMike Smith return (0); 23610ae55423SMike Smith } 23620ae55423SMike Smith 23630ae55423SMike Smith /* 23640ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 23650ae55423SMike Smith */ 23660ae55423SMike Smith int 23670ae55423SMike Smith acpi_disabled(char *subsys) 23680ae55423SMike Smith { 236978689b15SMaxime Henrion char *cp, *env; 23700ae55423SMike Smith int len; 23710ae55423SMike Smith 23723184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 23730ae55423SMike Smith return (0); 23743184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 237578689b15SMaxime Henrion freeenv(env); 23760ae55423SMike Smith return (1); 2377d786139cSMaxime Henrion } 23780ae55423SMike Smith 23793184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 238078689b15SMaxime Henrion cp = env; 23810ae55423SMike Smith for (;;) { 23823184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 23830ae55423SMike Smith cp++; 23843184cf5aSNate Lawson if (*cp == '\0') 23850ae55423SMike Smith break; 23860ae55423SMike Smith len = 0; 23873184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 23880ae55423SMike Smith len++; 23893184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 239078689b15SMaxime Henrion freeenv(env); 239115e32d5dSMike Smith return (1); 2392d786139cSMaxime Henrion } 239315e32d5dSMike Smith cp += len; 239415e32d5dSMike Smith } 239578689b15SMaxime Henrion freeenv(env); 2396be2b1797SNate Lawson 239715e32d5dSMike Smith return (0); 239815e32d5dSMike Smith } 239915e32d5dSMike Smith 240015e32d5dSMike Smith /* 240115e32d5dSMike Smith * Control interface. 240215e32d5dSMike Smith * 24030ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2404be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2405be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 240615e32d5dSMike Smith */ 24070ae55423SMike Smith struct acpi_ioctl_hook 24080ae55423SMike Smith { 24090ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 24100ae55423SMike Smith u_long cmd; 2411be2b1797SNate Lawson acpi_ioctl_fn fn; 24120ae55423SMike Smith void *arg; 24130ae55423SMike Smith }; 24140ae55423SMike Smith 24150ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 24160ae55423SMike Smith static int acpi_ioctl_hooks_initted; 24170ae55423SMike Smith 24180ae55423SMike Smith int 2419be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 24200ae55423SMike Smith { 24210ae55423SMike Smith struct acpi_ioctl_hook *hp; 24220ae55423SMike Smith 24230ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 24240ae55423SMike Smith return (ENOMEM); 24250ae55423SMike Smith hp->cmd = cmd; 24260ae55423SMike Smith hp->fn = fn; 24270ae55423SMike Smith hp->arg = arg; 242815e2f34fSNate Lawson 242915e2f34fSNate Lawson ACPI_LOCK(acpi); 24300ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 24310ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 24320ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 24330ae55423SMike Smith } 24340ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 243515e2f34fSNate Lawson ACPI_UNLOCK(acpi); 243615e2f34fSNate Lawson 24370ae55423SMike Smith return (0); 24380ae55423SMike Smith } 24390ae55423SMike Smith 24400ae55423SMike Smith void 2441be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 24420ae55423SMike Smith { 24430ae55423SMike Smith struct acpi_ioctl_hook *hp; 24440ae55423SMike Smith 244515e2f34fSNate Lawson ACPI_LOCK(acpi); 24460ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 244715e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 24480ae55423SMike Smith break; 24490ae55423SMike Smith 24500ae55423SMike Smith if (hp != NULL) { 24510ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 24520ae55423SMike Smith free(hp, M_ACPIDEV); 24530ae55423SMike Smith } 245415e2f34fSNate Lawson ACPI_UNLOCK(acpi); 24550ae55423SMike Smith } 24560ae55423SMike Smith 245715e32d5dSMike Smith static int 245889c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 245915e32d5dSMike Smith { 246015e32d5dSMike Smith return (0); 246115e32d5dSMike Smith } 246215e32d5dSMike Smith 246315e32d5dSMike Smith static int 246489c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 246515e32d5dSMike Smith { 246615e32d5dSMike Smith return (0); 246715e32d5dSMike Smith } 246815e32d5dSMike Smith 246915e32d5dSMike Smith static int 247089c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 247115e32d5dSMike Smith { 247215e32d5dSMike Smith struct acpi_softc *sc; 24730ae55423SMike Smith struct acpi_ioctl_hook *hp; 2474bee4aa4aSNate Lawson int error, state; 247515e32d5dSMike Smith 2476a2e35898SDavid E. O'Brien error = 0; 2477a2e35898SDavid E. O'Brien hp = NULL; 247815e32d5dSMike Smith sc = dev->si_drv1; 247915e32d5dSMike Smith 24800ae55423SMike Smith /* 24810ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 24820ae55423SMike Smith */ 248315e2f34fSNate Lawson ACPI_LOCK(acpi); 2484bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 24850ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 2486bee4aa4aSNate Lawson if (hp->cmd == cmd) 2487bee4aa4aSNate Lawson break; 24880ae55423SMike Smith } 248915e2f34fSNate Lawson ACPI_UNLOCK(acpi); 2490bee4aa4aSNate Lawson if (hp) 2491bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 24920ae55423SMike Smith 24930ae55423SMike Smith /* 2494a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2495a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2496a89fcf28STakanori Watanabe * Not changing system behavior. 2497a89fcf28STakanori Watanabe */ 2498be2b1797SNate Lawson if ((flag & FWRITE) == 0) 2499be2b1797SNate Lawson return (EPERM); 2500a89fcf28STakanori Watanabe 2501be2b1797SNate Lawson /* Core system ioctls. */ 250215e32d5dSMike Smith switch (cmd) { 250315e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 2504bee4aa4aSNate Lawson error = EINVAL; 250515e32d5dSMike Smith state = *(int *)addr; 2506bee4aa4aSNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 2507bee4aa4aSNate Lawson if (ACPI_SUCCESS(acpi_SetSleepState(sc, state))) 2508bee4aa4aSNate Lawson error = 0; 250915e32d5dSMike Smith break; 251015e32d5dSMike Smith default: 2511bee4aa4aSNate Lawson error = ENXIO; 251215e32d5dSMike Smith break; 251315e32d5dSMike Smith } 2514917d44c8SMitsuru IWASAKI 251515e32d5dSMike Smith return (error); 251615e32d5dSMike Smith } 251715e32d5dSMike Smith 25181d073b1dSJohn Baldwin static int 2519d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2520d75de536SMitsuru IWASAKI { 2521d75de536SMitsuru IWASAKI int error; 2522bee4aa4aSNate Lawson struct sbuf sb; 2523d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2524d75de536SMitsuru IWASAKI 2525bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 2526bee4aa4aSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) 2527bee4aa4aSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 2528bee4aa4aSNate Lawson sbuf_printf(&sb, "S%d ", state); 2529bee4aa4aSNate Lawson sbuf_trim(&sb); 2530bee4aa4aSNate Lawson sbuf_finish(&sb); 2531bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 2532bee4aa4aSNate Lawson sbuf_delete(&sb); 2533d75de536SMitsuru IWASAKI return (error); 2534d75de536SMitsuru IWASAKI } 2535d75de536SMitsuru IWASAKI 2536d75de536SMitsuru IWASAKI static int 25371d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 25381d073b1dSJohn Baldwin { 25391d073b1dSJohn Baldwin char sleep_state[10]; 25401d073b1dSJohn Baldwin int error; 25411d073b1dSJohn Baldwin u_int new_state, old_state; 25421d073b1dSJohn Baldwin 25431d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2544bee4aa4aSNate Lawson if (old_state > ACPI_S_STATES_MAX + 1) 2545bee4aa4aSNate Lawson strlcpy(sleep_state, "unknown", sizeof(sleep_state)); 2546bee4aa4aSNate Lawson else 2547bee4aa4aSNate Lawson strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state)); 25481d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 25491d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2550be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2551bee4aa4aSNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) 2552bee4aa4aSNate Lawson if (strcmp(sleep_state, sleep_state_names[new_state]) == 0) 25531d073b1dSJohn Baldwin break; 2554931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2555be2b1797SNate Lawson if (new_state != old_state) 25561d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2557bee4aa4aSNate Lawson } else 25581d073b1dSJohn Baldwin error = EINVAL; 25591d073b1dSJohn Baldwin } 2560be2b1797SNate Lawson 25611d073b1dSJohn Baldwin return (error); 25621d073b1dSJohn Baldwin } 25631d073b1dSJohn Baldwin 25649b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 25659b937d48SNate Lawson void 25669b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 25679b937d48SNate Lawson { 25689b937d48SNate Lawson char notify_buf[16]; 25699b937d48SNate Lawson ACPI_BUFFER handle_buf; 25709b937d48SNate Lawson ACPI_STATUS status; 25719b937d48SNate Lawson 25729b937d48SNate Lawson if (subsystem == NULL) 25739b937d48SNate Lawson return; 25749b937d48SNate Lawson 25759b937d48SNate Lawson handle_buf.Pointer = NULL; 25769b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 25779b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 25789b937d48SNate Lawson if (ACPI_FAILURE(status)) 25799b937d48SNate Lawson return; 25809b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 25819b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 25829b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 25839b937d48SNate Lawson } 25849b937d48SNate Lawson 258515e32d5dSMike Smith #ifdef ACPI_DEBUG 25860ae55423SMike Smith /* 25870ae55423SMike Smith * Support for parsing debug options from the kernel environment. 25880ae55423SMike Smith * 25890ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 25900ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 25910ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 25920ae55423SMike Smith * prefixing the bit name with !. 25930ae55423SMike Smith */ 259415e32d5dSMike Smith struct debugtag 259515e32d5dSMike Smith { 259615e32d5dSMike Smith char *name; 259715e32d5dSMike Smith UINT32 value; 259815e32d5dSMike Smith }; 259915e32d5dSMike Smith 260015e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2601c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2602c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2603c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2604c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2605c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2606c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2607c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2608c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2609c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2610d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 26114c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2612d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2613297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 26144c1cdee6SMike Smith 2615c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2616c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 26173184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2618c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 26193184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 26203184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 26213184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 26224c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2623cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 26243184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2625b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 262615e32d5dSMike Smith {NULL, 0} 262715e32d5dSMike Smith }; 262815e32d5dSMike Smith 262915e32d5dSMike Smith static struct debugtag dbg_level[] = { 26304c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 26319501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 26329501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 26334c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 26349501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 26354c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2636d62ab2f4SMitsuru IWASAKI 2637d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2638297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 26394c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 26404c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2641d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 26424c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 26434c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 26444c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 26454c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 26464c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 26474c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 26484c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 26494c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 26504c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 26514c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2652d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2653d62ab2f4SMitsuru IWASAKI 2654d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2655d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2656d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2657d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2658d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 26594c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2660d62ab2f4SMitsuru IWASAKI 2661d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2662d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2663d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2664d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2665d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2666d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2667d62ab2f4SMitsuru IWASAKI 2668d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 266998479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 267098479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 267198479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 267298479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 267398479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 267415e32d5dSMike Smith {NULL, 0} 267515e32d5dSMike Smith }; 267615e32d5dSMike Smith 267715e32d5dSMike Smith static void 267815e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 267915e32d5dSMike Smith { 268015e32d5dSMike Smith char *ep; 268115e32d5dSMike Smith int i, l; 26820ae55423SMike Smith int set; 268315e32d5dSMike Smith 268415e32d5dSMike Smith while (*cp) { 268515e32d5dSMike Smith if (isspace(*cp)) { 268615e32d5dSMike Smith cp++; 268715e32d5dSMike Smith continue; 268815e32d5dSMike Smith } 268915e32d5dSMike Smith ep = cp; 269015e32d5dSMike Smith while (*ep && !isspace(*ep)) 269115e32d5dSMike Smith ep++; 26920ae55423SMike Smith if (*cp == '!') { 26930ae55423SMike Smith set = 0; 26940ae55423SMike Smith cp++; 26950ae55423SMike Smith if (cp == ep) 26960ae55423SMike Smith continue; 26970ae55423SMike Smith } else { 26980ae55423SMike Smith set = 1; 26990ae55423SMike Smith } 270015e32d5dSMike Smith l = ep - cp; 270115e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 270215e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2703be2b1797SNate Lawson if (set) 270415e32d5dSMike Smith *flag |= tag[i].value; 2705be2b1797SNate Lawson else 27060ae55423SMike Smith *flag &= ~tag[i].value; 270715e32d5dSMike Smith } 270815e32d5dSMike Smith } 270915e32d5dSMike Smith cp = ep; 271015e32d5dSMike Smith } 271115e32d5dSMike Smith } 271215e32d5dSMike Smith 271315e32d5dSMike Smith static void 27142a4ac806SMike Smith acpi_set_debugging(void *junk) 271515e32d5dSMike Smith { 27167e639165SNate Lawson char *layer, *level; 271715e32d5dSMike Smith 27181d7b121cSNate Lawson if (cold) { 27190ae55423SMike Smith AcpiDbgLayer = 0; 27200ae55423SMike Smith AcpiDbgLevel = 0; 27211d7b121cSNate Lawson } 27221d7b121cSNate Lawson 27237e639165SNate Lawson layer = getenv("debug.acpi.layer"); 27247e639165SNate Lawson level = getenv("debug.acpi.level"); 27257e639165SNate Lawson if (layer == NULL && level == NULL) 27267e639165SNate Lawson return; 27270ae55423SMike Smith 27287e639165SNate Lawson printf("ACPI set debug"); 27297e639165SNate Lawson if (layer != NULL) { 27307e639165SNate Lawson if (strcmp("NONE", layer) != 0) 27317e639165SNate Lawson printf(" layer '%s'", layer); 27327e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 27337e639165SNate Lawson freeenv(layer); 27341d7b121cSNate Lawson } 27357e639165SNate Lawson if (level != NULL) { 27367e639165SNate Lawson if (strcmp("NONE", level) != 0) 27377e639165SNate Lawson printf(" level '%s'", level); 27387e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 27397e639165SNate Lawson freeenv(level); 27407e639165SNate Lawson } 27417e639165SNate Lawson printf("\n"); 274215e32d5dSMike Smith } 2743bee4aa4aSNate Lawson 2744be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2745be2b1797SNate Lawson NULL); 27461d7b121cSNate Lawson 27471d7b121cSNate Lawson static int 27481d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 27491d7b121cSNate Lawson { 275054f6bca0SNate Lawson int error, *dbg; 27511d7b121cSNate Lawson struct debugtag *tag; 275254f6bca0SNate Lawson struct sbuf sb; 27531d7b121cSNate Lawson 275454f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 275554f6bca0SNate Lawson return (ENOMEM); 27561d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 27571d7b121cSNate Lawson tag = &dbg_layer[0]; 27581d7b121cSNate Lawson dbg = &AcpiDbgLayer; 27591d7b121cSNate Lawson } else { 27601d7b121cSNate Lawson tag = &dbg_level[0]; 27611d7b121cSNate Lawson dbg = &AcpiDbgLevel; 27621d7b121cSNate Lawson } 27631d7b121cSNate Lawson 27641d7b121cSNate Lawson /* Get old values if this is a get request. */ 276515e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 27661d7b121cSNate Lawson if (*dbg == 0) { 276754f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 27681d7b121cSNate Lawson } else if (req->newptr == NULL) { 27691d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 277054f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 277154f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 27721d7b121cSNate Lawson } 27731d7b121cSNate Lawson } 277454f6bca0SNate Lawson sbuf_trim(&sb); 277554f6bca0SNate Lawson sbuf_finish(&sb); 27761d7b121cSNate Lawson 27777e639165SNate Lawson /* Copy out the old values to the user. */ 27787e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 277954f6bca0SNate Lawson sbuf_delete(&sb); 27801d7b121cSNate Lawson 27811d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 27821d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 27831d7b121cSNate Lawson *dbg = 0; 27841d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 27851d7b121cSNate Lawson acpi_set_debugging(NULL); 27861d7b121cSNate Lawson } 278715e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 27881d7b121cSNate Lawson 27891d7b121cSNate Lawson return (error); 27901d7b121cSNate Lawson } 2791bee4aa4aSNate Lawson 27921d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 27931d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 27941d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 27951d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 2796bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 2797f9390180SMitsuru IWASAKI 2798f9390180SMitsuru IWASAKI static int 2799f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2800f9390180SMitsuru IWASAKI { 2801f9390180SMitsuru IWASAKI int state, acpi_state; 2802f9390180SMitsuru IWASAKI int error; 2803f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2804f9390180SMitsuru IWASAKI va_list ap; 2805f9390180SMitsuru IWASAKI 2806f9390180SMitsuru IWASAKI error = 0; 2807f9390180SMitsuru IWASAKI switch (cmd) { 2808f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2809f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2810f9390180SMitsuru IWASAKI if (sc == NULL) { 2811f9390180SMitsuru IWASAKI error = EINVAL; 2812f9390180SMitsuru IWASAKI goto out; 2813f9390180SMitsuru IWASAKI } 2814f9390180SMitsuru IWASAKI 2815f9390180SMitsuru IWASAKI va_start(ap, arg); 2816f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2817f9390180SMitsuru IWASAKI va_end(ap); 2818f9390180SMitsuru IWASAKI 2819f9390180SMitsuru IWASAKI switch (state) { 2820f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2821f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2822f9390180SMitsuru IWASAKI break; 2823f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2824f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2825f9390180SMitsuru IWASAKI break; 2826f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2827f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2828f9390180SMitsuru IWASAKI break; 2829f9390180SMitsuru IWASAKI default: 2830f9390180SMitsuru IWASAKI error = EINVAL; 2831f9390180SMitsuru IWASAKI goto out; 2832f9390180SMitsuru IWASAKI } 2833f9390180SMitsuru IWASAKI 2834f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2835f9390180SMitsuru IWASAKI break; 2836f9390180SMitsuru IWASAKI default: 2837f9390180SMitsuru IWASAKI error = EINVAL; 2838f9390180SMitsuru IWASAKI goto out; 2839f9390180SMitsuru IWASAKI } 2840f9390180SMitsuru IWASAKI 2841f9390180SMitsuru IWASAKI out: 2842f9390180SMitsuru IWASAKI return (error); 2843f9390180SMitsuru IWASAKI } 2844f9390180SMitsuru IWASAKI 2845f9390180SMitsuru IWASAKI static void 2846f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2847f9390180SMitsuru IWASAKI { 2848be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 28496c407052SMitsuru IWASAKI return; 2850f9390180SMitsuru IWASAKI 2851f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2852f9390180SMitsuru IWASAKI } 2853f9390180SMitsuru IWASAKI 2854f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2855