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, 74dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 757ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 767ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 777ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 787ac40f5fSPoul-Henning Kamp .d_name = "acpi", 7915e32d5dSMike Smith }; 8015e32d5dSMike Smith 81346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */ 82cb9b0d80SMike Smith struct mtx acpi_mutex; 83cb9b0d80SMike Smith 8489fb5af8SNate Lawson /* Bitmap of device quirks. */ 8589fb5af8SNate Lawson int acpi_quirks; 8689fb5af8SNate Lawson 8791233413SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 8891233413SNate Lawson struct rman acpi_rman_io, acpi_rman_mem; 8991233413SNate Lawson 906d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 9115e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 9215e32d5dSMike Smith static int acpi_probe(device_t dev); 9315e32d5dSMike Smith static int acpi_attach(device_t dev); 94169b539aSNate Lawson static int acpi_shutdown(device_t dev); 95be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 96be2b1797SNate Lawson int unit); 9715e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 98be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 99be2b1797SNate Lawson uintptr_t *result); 100be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 101be2b1797SNate Lawson uintptr_t value); 10291233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 103be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 104be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 105be2b1797SNate Lawson u_long count, u_int flags); 106be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 107be2b1797SNate Lawson int rid, struct resource *r); 1081e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1091e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 110ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 111ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 112ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 113ce619b2eSNate Lawson ACPI_BUFFER *ret); 114ce619b2eSNate Lawson static ACPI_STATUS acpi_device_walk_ns(device_t bus, device_t dev, 115ce619b2eSNate Lawson ACPI_OBJECT_TYPE type, UINT32 max_depth, 116ce619b2eSNate Lawson ACPI_WALK_CALLBACK user_fn, void *context, void **ret); 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), 172ce619b2eSNate Lawson DEVMETHOD(acpi_walk_namespace, acpi_device_walk_ns), 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 190865b8d0bSNate Lawson static const char* sleep_state_names[] = { 191865b8d0bSNate Lawson "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 192865b8d0bSNate Lawson 1931d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 1941d7b121cSNate Lawson static char acpi_ca_version[12]; 1951d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 1961d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 19715e32d5dSMike Smith 19815e32d5dSMike Smith /* 199413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 200c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 201c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 202c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 203c9b8d77dSNate Lawson * some IBMs use such code. 204413081d7SNate Lawson */ 205c9b8d77dSNate Lawson static int acpi_serialize_methods; 206413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 207413081d7SNate Lawson 208c9b8d77dSNate Lawson /* 2096d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2106d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 211be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 212be2b1797SNate Lawson * off it. 2136d63101aSMike Smith */ 2146d63101aSMike Smith static int 2156d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2166d63101aSMike Smith { 2176d63101aSMike Smith switch (event) { 2186d63101aSMike Smith case MOD_LOAD: 21987b45ed5SMitsuru IWASAKI if (!cold) { 22087b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2216d63101aSMike Smith return (EPERM); 22287b45ed5SMitsuru IWASAKI } 2236d63101aSMike Smith break; 2246d63101aSMike Smith case MOD_UNLOAD: 225f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2266d63101aSMike Smith return (EBUSY); 227f9390180SMitsuru IWASAKI break; 2286d63101aSMike Smith default: 2296d63101aSMike Smith break; 2306d63101aSMike Smith } 2316d63101aSMike Smith return (0); 2326d63101aSMike Smith } 2336d63101aSMike Smith 2346d63101aSMike Smith /* 235bbc2815cSJohn Baldwin * Perform early initialization. 23615e32d5dSMike Smith */ 237bbc2815cSJohn Baldwin ACPI_STATUS 238bbc2815cSJohn Baldwin acpi_Startup(void) 23915e32d5dSMike Smith { 240d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 241d786139cSMaxime Henrion char *debugpoint; 24215e32d5dSMike Smith #endif 24389fb5af8SNate Lawson static int started = 0; 24489fb5af8SNate Lawson int error, val; 24515e32d5dSMike Smith 2461b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2471b8c233dSPeter Pentchev 248bbc2815cSJohn Baldwin if (started) 24989fb5af8SNate Lawson return_VALUE (0); 250bbc2815cSJohn Baldwin started = 1; 25115e32d5dSMike Smith 252be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2536008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 254cb9b0d80SMike Smith 255413081d7SNate Lawson /* 256413081d7SNate Lawson * Set the globals from our tunables. This is needed because ACPI-CA 257f3fc4f8bSNate Lawson * uses UINT8 for some values and we have no tunable_byte. 258413081d7SNate Lawson */ 259f3fc4f8bSNate Lawson AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods; 260413081d7SNate Lawson 261be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 262d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 263d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 264d786139cSMaxime Henrion if (debugpoint) { 265d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 26615e32d5dSMike Smith acpi_EnterDebugger(); 267d786139cSMaxime Henrion freeenv(debugpoint); 268d786139cSMaxime Henrion } 26915e32d5dSMike Smith #endif 270b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 271bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 272bbc2815cSJohn Baldwin return_VALUE (error); 27315e32d5dSMike Smith } 274d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 275d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 276d786139cSMaxime Henrion if (debugpoint) { 277d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 27815e32d5dSMike Smith acpi_EnterDebugger(); 279d786139cSMaxime Henrion freeenv(debugpoint); 280d786139cSMaxime Henrion } 28115e32d5dSMike Smith #endif 2821611ea87SMitsuru IWASAKI 283b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 284bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 28589fb5af8SNate Lawson AcpiTerminate(); 286bbc2815cSJohn Baldwin return_VALUE (error); 28715e32d5dSMike Smith } 2883184cf5aSNate Lawson 28989fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 29089fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 29189fb5af8SNate Lawson 29289fb5af8SNate Lawson /* If the user manually set the disabled hint to 0, override any quirk. */ 29389fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 29489fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 29589fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 29689fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 29789fb5af8SNate Lawson AcpiTerminate(); 2984e376d58SNate Lawson return_VALUE (AE_ERROR); 29989fb5af8SNate Lawson } 3003184cf5aSNate Lawson 301bbc2815cSJohn Baldwin return_VALUE (AE_OK); 302bbc2815cSJohn Baldwin } 303bbc2815cSJohn Baldwin 304bbc2815cSJohn Baldwin /* 305bbc2815cSJohn Baldwin * Detect ACPI, perform early initialisation 306bbc2815cSJohn Baldwin */ 307bbc2815cSJohn Baldwin static void 308bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent) 309bbc2815cSJohn Baldwin { 310bbc2815cSJohn Baldwin device_t child; 311bbc2815cSJohn Baldwin 312bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 313bbc2815cSJohn Baldwin 314bbc2815cSJohn Baldwin if (!cold) 315bbc2815cSJohn Baldwin return_VOID; 316bbc2815cSJohn Baldwin 317bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 318bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 319bbc2815cSJohn Baldwin return_VOID; 320bbc2815cSJohn Baldwin 321bbc2815cSJohn Baldwin /* Make sure we're not being doubly invoked. */ 322bbc2815cSJohn Baldwin if (device_find_child(parent, "acpi", 0) != NULL) 323bbc2815cSJohn Baldwin return_VOID; 324bbc2815cSJohn Baldwin 325bbc2815cSJohn Baldwin /* Initialize ACPI-CA. */ 326bbc2815cSJohn Baldwin if (ACPI_FAILURE(acpi_Startup())) 327bbc2815cSJohn Baldwin return_VOID; 32815e32d5dSMike Smith 3294e376d58SNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION); 3304e376d58SNate Lawson 331be2b1797SNate Lawson /* Attach the actual ACPI device. */ 33215e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 33315e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 3340ae55423SMike Smith return_VOID; 33515e32d5dSMike Smith } 33615e32d5dSMike Smith } 33715e32d5dSMike Smith 33815e32d5dSMike Smith /* 33915e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 34015e32d5dSMike Smith */ 34115e32d5dSMike Smith static int 34215e32d5dSMike Smith acpi_probe(device_t dev) 34315e32d5dSMike Smith { 34415e32d5dSMike Smith ACPI_TABLE_HEADER th; 34515e32d5dSMike Smith char buf[20]; 34615e32d5dSMike Smith int error; 3472ccd1cacSNate Lawson struct sbuf sb; 3482ccd1cacSNate Lawson ACPI_STATUS status; 349fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 35015e32d5dSMike Smith 351b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 352f9390180SMitsuru IWASAKI 353f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 354f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 355be2b1797SNate Lawson 356f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 357f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 358f9390180SMitsuru IWASAKI } 359f9390180SMitsuru IWASAKI 360cb9b0d80SMike Smith ACPI_LOCK; 3610ae55423SMike Smith 362b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 363be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 364be2b1797SNate Lawson AcpiFormatException(status)); 365cb9b0d80SMike Smith error = ENXIO; 366cb9b0d80SMike Smith } else { 3672ccd1cacSNate Lawson sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 3682ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemId, 6); 3692ccd1cacSNate Lawson sbuf_trim(&sb); 3702ccd1cacSNate Lawson sbuf_putc(&sb, ' '); 3712ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemTableId, 8); 3722ccd1cacSNate Lawson sbuf_trim(&sb); 3732ccd1cacSNate Lawson sbuf_finish(&sb); 3742ccd1cacSNate Lawson device_set_desc_copy(dev, sbuf_data(&sb)); 3752ccd1cacSNate Lawson sbuf_delete(&sb); 376cb9b0d80SMike Smith error = 0; 377cb9b0d80SMike Smith } 378cb9b0d80SMike Smith ACPI_UNLOCK; 379cb9b0d80SMike Smith return_VALUE(error); 38015e32d5dSMike Smith } 38115e32d5dSMike Smith 38215e32d5dSMike Smith static int 38315e32d5dSMike Smith acpi_attach(device_t dev) 38415e32d5dSMike Smith { 38515e32d5dSMike Smith struct acpi_softc *sc; 386cb9b0d80SMike Smith ACPI_STATUS status; 387ea27c63eSNate Lawson int error, state; 38832d18aa5SMike Smith UINT32 flags; 389ea27c63eSNate Lawson UINT8 TypeA, TypeB; 390498d464fSMitsuru IWASAKI char *env; 391d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 392d786139cSMaxime Henrion char *debugpoint; 39315e32d5dSMike Smith #endif 394fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 39515e32d5dSMike Smith 396b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 397cb9b0d80SMike Smith ACPI_LOCK; 39815e32d5dSMike Smith sc = device_get_softc(dev); 39915e32d5dSMike Smith bzero(sc, sizeof(*sc)); 40015e32d5dSMike Smith sc->acpi_dev = dev; 40115e32d5dSMike Smith 40291233413SNate Lawson /* Initialize resource manager. */ 40391233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 40491233413SNate Lawson acpi_rman_io.rm_start = 0; 40591233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 40691233413SNate Lawson acpi_rman_io.rm_descr = "I/O ports"; 40791233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 40891233413SNate Lawson panic("acpi rman_init IO ports failed"); 40991233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 41091233413SNate Lawson acpi_rman_mem.rm_start = 0; 41191233413SNate Lawson acpi_rman_mem.rm_end = ~0ul; 41291233413SNate Lawson acpi_rman_mem.rm_descr = "I/O memory addresses"; 41391233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 41491233413SNate Lawson panic("acpi rman_init memory failed"); 41591233413SNate Lawson 416d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 417d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 418d786139cSMaxime Henrion if (debugpoint) { 419d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 42015e32d5dSMike Smith acpi_EnterDebugger(); 421d786139cSMaxime Henrion freeenv(debugpoint); 422d786139cSMaxime Henrion } 42315e32d5dSMike Smith #endif 42415e32d5dSMike Smith 425be2b1797SNate Lawson /* Install the default address space handlers. */ 426cb9b0d80SMike Smith error = ENXIO; 427be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 428be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 429be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 430be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 431be2b1797SNate Lawson AcpiFormatException(status)); 432cb9b0d80SMike Smith goto out; 43315e32d5dSMike Smith } 434be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 435be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 436be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 437be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 438be2b1797SNate Lawson AcpiFormatException(status)); 439cb9b0d80SMike Smith goto out; 44015e32d5dSMike Smith } 441be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 442be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 443be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 444be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 445be2b1797SNate Lawson AcpiFormatException(status)); 446cb9b0d80SMike Smith goto out; 44715e32d5dSMike Smith } 44815e32d5dSMike Smith 44915e32d5dSMike Smith /* 45015e32d5dSMike Smith * Bring ACPI fully online. 45115e32d5dSMike Smith * 452be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 453be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 454be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 455be2b1797SNate Lawson * object init pass. 45615e32d5dSMike Smith * 45732d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 45832d18aa5SMike Smith * 459be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 460be2b1797SNate Lawson * all our child devices, but on many systems it works here. 46115e32d5dSMike Smith */ 462d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 463d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 464d786139cSMaxime Henrion if (debugpoint) { 465d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 46615e32d5dSMike Smith acpi_EnterDebugger(); 467d786139cSMaxime Henrion freeenv(debugpoint); 468d786139cSMaxime Henrion } 46915e32d5dSMike Smith #endif 47032d18aa5SMike Smith flags = 0; 471d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 47232d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 473b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 474be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 475be2b1797SNate Lawson AcpiFormatException(status)); 476cb9b0d80SMike Smith goto out; 47715e32d5dSMike Smith } 47815e32d5dSMike Smith 479f8335e3aSNate Lawson /* 480f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 481f8335e3aSNate Lawson * the namespace has been evaluated. 482f8335e3aSNate Lawson */ 483f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 484f8335e3aSNate Lawson 485b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 486be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 487be2b1797SNate Lawson AcpiFormatException(status)); 488b69ed3f4SMitsuru IWASAKI goto out; 489b69ed3f4SMitsuru IWASAKI } 490b69ed3f4SMitsuru IWASAKI 49115e32d5dSMike Smith /* 4921d073b1dSJohn Baldwin * Setup our sysctl tree. 4931d073b1dSJohn Baldwin * 4941d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4951d073b1dSJohn Baldwin */ 4961d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4971d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4981d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4991d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5001d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 501d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 502d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 503d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5041d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5051d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5061d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5071d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5081d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5091d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5101d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5111d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 512f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 513f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 514f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 515f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 516f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 517f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5182d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 519ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 520ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 521ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5221611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 5231611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 5241611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5252d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 5262d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 527bf0c18ecSNate Lawson 528bf0c18ecSNate Lawson /* 5292b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 530bf0c18ecSNate Lawson * stabilize. 531bf0c18ecSNate Lawson */ 5322b9609abSNate Lawson sc->acpi_sleep_delay = 1; 5332d644607SMitsuru IWASAKI if (bootverbose) 5342d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 535498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 536498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 537498d464fSMitsuru IWASAKI freeenv(env); 538498d464fSMitsuru IWASAKI } 5391d073b1dSJohn Baldwin 5402d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5412d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 5422d610c46SNate Lawson sc->acpi_s4bios = 1; 5432d610c46SNate Lawson 5441d073b1dSJohn Baldwin /* 545ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 546ea27c63eSNate Lawson * to NONE by default to avoid surprising users. 54715e32d5dSMike Smith */ 548ea27c63eSNate Lawson sc->acpi_power_button_sx = ACPI_STATE_S5; 549ea27c63eSNate Lawson sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 550f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 551f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 55215e32d5dSMike Smith 553ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 554ea27c63eSNate Lawson sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 555ea27c63eSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++) 556ea27c63eSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 557ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 558ea27c63eSNate Lawson break; 559ea27c63eSNate Lawson } 560ea27c63eSNate Lawson 56113d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 56215e32d5dSMike Smith 56315e32d5dSMike Smith /* 56415e32d5dSMike Smith * Scan the namespace and attach/initialise children. 56515e32d5dSMike Smith */ 566d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 567d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 568d786139cSMaxime Henrion if (debugpoint) { 569d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 57015e32d5dSMike Smith acpi_EnterDebugger(); 571d786139cSMaxime Henrion freeenv(debugpoint); 572d786139cSMaxime Henrion } 57315e32d5dSMike Smith #endif 57415e32d5dSMike Smith 57559e472e9SNate Lawson /* Register our shutdown handler. */ 576be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 577be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 57815e32d5dSMike Smith 57915e32d5dSMike Smith /* 58015e32d5dSMike Smith * Register our acpi event handlers. 58115e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 58215e32d5dSMike Smith */ 583be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 584be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 585be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 586be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 58715e32d5dSMike Smith 588be2b1797SNate Lawson /* Flag our initial states. */ 58915e32d5dSMike Smith sc->acpi_enabled = 1; 59015e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 591ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 59215e32d5dSMike Smith 593be2b1797SNate Lawson /* Create the control device */ 594a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 5952a4689e8SRobert Watson "acpi"); 59615e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 59715e32d5dSMike Smith 598d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 599d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 600d786139cSMaxime Henrion if (debugpoint) { 601be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 60215e32d5dSMike Smith acpi_EnterDebugger(); 603d786139cSMaxime Henrion freeenv(debugpoint); 604d786139cSMaxime Henrion } 60515e32d5dSMike Smith #endif 606f86214b6SMitsuru IWASAKI 60791da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 608be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 609c573e654SMitsuru IWASAKI goto out; 610c573e654SMitsuru IWASAKI #endif 611c573e654SMitsuru IWASAKI 612be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 613f86214b6SMitsuru IWASAKI goto out; 614f86214b6SMitsuru IWASAKI 615f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 616f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 617f9390180SMitsuru IWASAKI 618eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 619eeb6dba2SJohn Baldwin acpi_probe_children(dev); 620eeb6dba2SJohn Baldwin 621cb9b0d80SMike Smith error = 0; 622cb9b0d80SMike Smith 623cb9b0d80SMike Smith out: 624cb9b0d80SMike Smith ACPI_UNLOCK; 625cb9b0d80SMike Smith return_VALUE (error); 62615e32d5dSMike Smith } 62715e32d5dSMike Smith 628169b539aSNate Lawson static int 629169b539aSNate Lawson acpi_shutdown(device_t dev) 630169b539aSNate Lawson { 631169b539aSNate Lawson 6320e414868SNate Lawson /* Allow children to shutdown first. */ 6330e414868SNate Lawson bus_generic_shutdown(dev); 6340e414868SNate Lawson 635d4b9ff91SNate Lawson /* Enable any GPEs that are able to power-on the system (i.e., RTC). */ 636d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 637d4b9ff91SNate Lawson 638169b539aSNate Lawson return (0); 639169b539aSNate Lawson } 640169b539aSNate Lawson 64115e32d5dSMike Smith /* 64215e32d5dSMike Smith * Handle a new device being added 64315e32d5dSMike Smith */ 64415e32d5dSMike Smith static device_t 64515e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 64615e32d5dSMike Smith { 64715e32d5dSMike Smith struct acpi_device *ad; 64815e32d5dSMike Smith device_t child; 64915e32d5dSMike Smith 650be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 65115e32d5dSMike Smith return (NULL); 65215e32d5dSMike Smith 65315e32d5dSMike Smith resource_list_init(&ad->ad_rl); 65415e32d5dSMike Smith 65515e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 65615e32d5dSMike Smith if (child != NULL) 65715e32d5dSMike Smith device_set_ivars(child, ad); 65815e32d5dSMike Smith return (child); 65915e32d5dSMike Smith } 66015e32d5dSMike Smith 66115e32d5dSMike Smith static int 66215e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 66315e32d5dSMike Smith { 66415e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 66515e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 66615e32d5dSMike Smith int retval = 0; 66715e32d5dSMike Smith 66815e32d5dSMike Smith retval += bus_print_child_header(bus, child); 6699ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 6709ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 6719ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 6729ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 67315e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 67415e32d5dSMike Smith 67515e32d5dSMike Smith return (retval); 67615e32d5dSMike Smith } 67715e32d5dSMike Smith 67863600cc3SNate Lawson /* Location hint for devctl(8) */ 67963600cc3SNate Lawson static int 680247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 681247648afSTakanori Watanabe size_t buflen) 682247648afSTakanori Watanabe { 683247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 684247648afSTakanori Watanabe 685247648afSTakanori Watanabe if (dinfo->ad_handle) 686247648afSTakanori Watanabe snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle)); 687247648afSTakanori Watanabe else 688247648afSTakanori Watanabe snprintf(buf, buflen, "magic=unknown"); 689247648afSTakanori Watanabe return (0); 690247648afSTakanori Watanabe } 691247648afSTakanori Watanabe 69263600cc3SNate Lawson /* PnP information for devctl(8) */ 69363600cc3SNate Lawson static int 694247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 695247648afSTakanori Watanabe size_t buflen) 696247648afSTakanori Watanabe { 697247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 69863600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 69963600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 700247648afSTakanori Watanabe char *end; 701247648afSTakanori Watanabe int error; 702247648afSTakanori Watanabe 703247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 704247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 705247648afSTakanori Watanabe 706247648afSTakanori Watanabe if (error) 707247648afSTakanori Watanabe snprintf(buf, buflen, "Unknown"); 708247648afSTakanori Watanabe else 709247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 710247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 711247648afSTakanori Watanabe adinfo->HardwareId.Value : "UNKNOWN", 71263600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 71363600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 714247648afSTakanori Watanabe 715247648afSTakanori Watanabe if (adinfo) 716247648afSTakanori Watanabe AcpiOsFree(adinfo); 717247648afSTakanori Watanabe 718247648afSTakanori Watanabe return (0); 719247648afSTakanori Watanabe } 720247648afSTakanori Watanabe 721247648afSTakanori Watanabe /* 72215e32d5dSMike Smith * Handle per-device ivars 72315e32d5dSMike Smith */ 72415e32d5dSMike Smith static int 72515e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 72615e32d5dSMike Smith { 72715e32d5dSMike Smith struct acpi_device *ad; 72815e32d5dSMike Smith 72915e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 73015e32d5dSMike Smith printf("device has no ivars\n"); 73115e32d5dSMike Smith return (ENOENT); 73215e32d5dSMike Smith } 73315e32d5dSMike Smith 734be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 73515e32d5dSMike Smith switch(index) { 73615e32d5dSMike Smith case ACPI_IVAR_HANDLE: 73715e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 73815e32d5dSMike Smith break; 73915e32d5dSMike Smith case ACPI_IVAR_MAGIC: 74015e32d5dSMike Smith *(int *)result = ad->ad_magic; 74115e32d5dSMike Smith break; 74215e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 74315e32d5dSMike Smith *(void **)result = ad->ad_private; 74415e32d5dSMike Smith break; 745d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 746d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 747d4b9ff91SNate Lawson break; 74832d18aa5SMike Smith case ISA_IVAR_VENDORID: 74932d18aa5SMike Smith case ISA_IVAR_SERIAL: 75032d18aa5SMike Smith case ISA_IVAR_COMPATID: 75132d18aa5SMike Smith *(int *)result = -1; 75232d18aa5SMike Smith break; 75332d18aa5SMike Smith case ISA_IVAR_LOGICALID: 75432d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 75532d18aa5SMike Smith break; 75615e32d5dSMike Smith default: 75715e32d5dSMike Smith return (ENOENT); 75815e32d5dSMike Smith } 759be2b1797SNate Lawson 76015e32d5dSMike Smith return (0); 76115e32d5dSMike Smith } 76215e32d5dSMike Smith 76315e32d5dSMike Smith static int 76415e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 76515e32d5dSMike Smith { 76615e32d5dSMike Smith struct acpi_device *ad; 76715e32d5dSMike Smith 76815e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 76915e32d5dSMike Smith printf("device has no ivars\n"); 77015e32d5dSMike Smith return (ENOENT); 77115e32d5dSMike Smith } 77215e32d5dSMike Smith 77315e32d5dSMike Smith switch(index) { 77415e32d5dSMike Smith case ACPI_IVAR_HANDLE: 77515e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 77615e32d5dSMike Smith break; 77715e32d5dSMike Smith case ACPI_IVAR_MAGIC: 77815e32d5dSMike Smith ad->ad_magic = (int)value; 77915e32d5dSMike Smith break; 78015e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 78115e32d5dSMike Smith ad->ad_private = (void *)value; 78215e32d5dSMike Smith break; 783d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 784d4b9ff91SNate Lawson ad->ad_flags = (int)value; 785d4b9ff91SNate Lawson break; 78615e32d5dSMike Smith default: 7872ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 78815e32d5dSMike Smith return (ENOENT); 78915e32d5dSMike Smith } 790be2b1797SNate Lawson 79115e32d5dSMike Smith return (0); 79215e32d5dSMike Smith } 79315e32d5dSMike Smith 79415e32d5dSMike Smith /* 79515e32d5dSMike Smith * Handle child resource allocation/removal 79615e32d5dSMike Smith */ 79791233413SNate Lawson static struct resource_list * 79891233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 79915e32d5dSMike Smith { 80091233413SNate Lawson struct acpi_device *ad; 80115e32d5dSMike Smith 80291233413SNate Lawson ad = device_get_ivars(child); 80391233413SNate Lawson return (&ad->ad_rl); 80415e32d5dSMike Smith } 80515e32d5dSMike Smith 80615e32d5dSMike Smith static struct resource * 80715e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 80815e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 80915e32d5dSMike Smith { 81095957f62SJohn Baldwin ACPI_RESOURCE ares; 81115e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 81215e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 81391233413SNate Lawson struct resource_list_entry *rle; 81491233413SNate Lawson struct resource *res; 81591233413SNate Lawson struct rman *rm; 81615e32d5dSMike Smith 81791233413SNate Lawson /* 81891233413SNate Lawson * If this is an allocation of the "default" range for a given RID, and 81991233413SNate Lawson * we know what the resources for this device are (i.e., they're on the 82091233413SNate Lawson * child's resource list), use those start/end values. 82191233413SNate Lawson */ 82291233413SNate Lawson if (start == 0UL && end == ~0UL) { 82391233413SNate Lawson rle = resource_list_find(rl, type, *rid); 82491233413SNate Lawson if (rle == NULL) 82591233413SNate Lawson return (NULL); 82691233413SNate Lawson start = rle->start; 82791233413SNate Lawson end = rle->end; 82891233413SNate Lawson count = rle->count; 82991233413SNate Lawson } 83091233413SNate Lawson 83191233413SNate Lawson /* If we don't manage this address, pass the request up to the parent. */ 83291233413SNate Lawson rle = acpi_sysres_find(type, start); 83391233413SNate Lawson if (rle == NULL) { 83495957f62SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 83595957f62SJohn Baldwin start, end, count, flags); 83695957f62SJohn Baldwin } else { 83791233413SNate Lawson 83891233413SNate Lawson /* We only handle memory and IO resources through rman. */ 83991233413SNate Lawson switch (type) { 84091233413SNate Lawson case SYS_RES_IOPORT: 84191233413SNate Lawson rm = &acpi_rman_io; 84291233413SNate Lawson break; 84391233413SNate Lawson case SYS_RES_MEMORY: 84491233413SNate Lawson rm = &acpi_rman_mem; 84591233413SNate Lawson break; 84691233413SNate Lawson default: 84791233413SNate Lawson panic("acpi_alloc_resource: invalid res type %d", type); 84891233413SNate Lawson } 84991233413SNate Lawson 85091233413SNate Lawson /* If we do know it, allocate it from the local pool. */ 85195957f62SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 85295957f62SJohn Baldwin child); 85391233413SNate Lawson if (res == NULL) 85491233413SNate Lawson return (NULL); 85591233413SNate Lawson 856a3236570SWarner Losh /* Copy the bus tag and handle from the pre-allocated resource. */ 85791233413SNate Lawson rman_set_bustag(res, rman_get_bustag(rle->res)); 858ed67d9deSWarner Losh rman_set_bushandle(res, rman_get_start(res)); 85991233413SNate Lawson 86091233413SNate Lawson /* If requested, activate the resource using the parent's method. */ 86195957f62SJohn Baldwin if (flags & RF_ACTIVE) 86291233413SNate Lawson if (bus_activate_resource(child, type, *rid, res) != 0) { 86391233413SNate Lawson rman_release_resource(res); 86491233413SNate Lawson return (NULL); 86591233413SNate Lawson } 86695957f62SJohn Baldwin } 86791233413SNate Lawson 86895957f62SJohn Baldwin if (res != NULL && device_get_parent(child) == bus) 86995957f62SJohn Baldwin switch (type) { 87095957f62SJohn Baldwin case SYS_RES_IRQ: 87195957f62SJohn Baldwin /* 87295957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 87395957f62SJohn Baldwin * configure the interrupt associated with a device when we 87495957f62SJohn Baldwin * parse the resources but have to defer it until a driver 87595957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 87695957f62SJohn Baldwin * 87795957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 87895957f62SJohn Baldwin */ 87995957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 88095957f62SJohn Baldwin acpi_config_intr(child, &ares); 88195957f62SJohn Baldwin break; 88295957f62SJohn Baldwin } 88391233413SNate Lawson return (res); 88415e32d5dSMike Smith } 88515e32d5dSMike Smith 88615e32d5dSMike Smith static int 88791233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 88891233413SNate Lawson struct resource *r) 88915e32d5dSMike Smith { 89091233413SNate Lawson int ret; 89115e32d5dSMike Smith 89291233413SNate Lawson /* 89391233413SNate Lawson * If we know about this address, deactivate it and release it to the 89491233413SNate Lawson * local pool. If we don't, pass this request up to the parent. 89591233413SNate Lawson */ 89691233413SNate Lawson if (acpi_sysres_find(type, rman_get_start(r)) == NULL) { 89791233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 89891233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 89991233413SNate Lawson if (ret != 0) 90091233413SNate Lawson return (ret); 90191233413SNate Lawson } 90291233413SNate Lawson ret = rman_release_resource(r); 90391233413SNate Lawson } else 90491233413SNate Lawson ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 90591233413SNate Lawson 90691233413SNate Lawson return (ret); 90715e32d5dSMike Smith } 90815e32d5dSMike Smith 909dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 910dc750869SNate Lawson struct resource * 911dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 912dc750869SNate Lawson { 913dc750869SNate Lawson int type; 914dc750869SNate Lawson 915dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 916dc750869SNate Lawson gas->RegisterBitWidth < 8) 917dc750869SNate Lawson return (NULL); 918dc750869SNate Lawson 919dc750869SNate Lawson switch (gas->AddressSpaceId) { 920dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 921dc750869SNate Lawson type = SYS_RES_MEMORY; 922dc750869SNate Lawson break; 923dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 924dc750869SNate Lawson type = SYS_RES_IOPORT; 925dc750869SNate Lawson break; 926dc750869SNate Lawson default: 927dc750869SNate Lawson return (NULL); 928dc750869SNate Lawson } 929dc750869SNate Lawson 930dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 9315f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 932dc750869SNate Lawson } 933dc750869SNate Lawson 934c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 9351e4925e8SNate Lawson static uint32_t 93632d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 937bc0f2195SMike Smith { 9381e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9391e4925e8SNate Lawson ACPI_BUFFER buf; 940bc0f2195SMike Smith ACPI_HANDLE h; 941bc0f2195SMike Smith ACPI_STATUS error; 942bc0f2195SMike Smith u_int32_t pnpid; 943fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 94432d18aa5SMike Smith 945b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 94632d18aa5SMike Smith 94732d18aa5SMike Smith pnpid = 0; 948bd189fe7SNate Lawson buf.Pointer = NULL; 949bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 950bd189fe7SNate Lawson 95132d18aa5SMike Smith ACPI_LOCK; 95232d18aa5SMike Smith 9536fca9360SNate Lawson /* Fetch and validate the HID. */ 95432d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 955bd189fe7SNate Lawson goto out; 9566fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9576fca9360SNate Lawson if (ACPI_FAILURE(error)) 958bd189fe7SNate Lawson goto out; 9591e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 96032d18aa5SMike Smith 9611e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 9621e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 963be2b1797SNate Lawson 964bd189fe7SNate Lawson out: 965bd189fe7SNate Lawson if (buf.Pointer != NULL) 9661e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 96732d18aa5SMike Smith ACPI_UNLOCK; 96832d18aa5SMike Smith return_VALUE (pnpid); 96932d18aa5SMike Smith } 97032d18aa5SMike Smith 9711e4925e8SNate Lawson static int 9721e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 973b2566207STakanori Watanabe { 9741e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9751e4925e8SNate Lawson ACPI_BUFFER buf; 976b2566207STakanori Watanabe ACPI_HANDLE h; 977b2566207STakanori Watanabe ACPI_STATUS error; 9781e4925e8SNate Lawson uint32_t *pnpid; 9791e4925e8SNate Lawson int valid, i; 980b2566207STakanori Watanabe ACPI_LOCK_DECL; 981b2566207STakanori Watanabe 982b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 983b2566207STakanori Watanabe 9841e4925e8SNate Lawson pnpid = cids; 9851e4925e8SNate Lawson valid = 0; 986bd189fe7SNate Lawson buf.Pointer = NULL; 987bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 988bd189fe7SNate Lawson 989b2566207STakanori Watanabe ACPI_LOCK; 990b2566207STakanori Watanabe 9911e4925e8SNate Lawson /* Fetch and validate the CID */ 992b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 993bd189fe7SNate Lawson goto out; 9941e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9951e4925e8SNate Lawson if (ACPI_FAILURE(error)) 996bd189fe7SNate Lawson goto out; 9971e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 9981e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 999b2566207STakanori Watanabe goto out; 1000b2566207STakanori Watanabe 10011e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 10021e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 10031e4925e8SNate Lawson for (i = 0; i < count; i++) { 10041e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 10051e4925e8SNate Lawson continue; 10061e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 10071e4925e8SNate Lawson valid++; 1008b2566207STakanori Watanabe } 1009b2566207STakanori Watanabe 10101e4925e8SNate Lawson out: 1011bd189fe7SNate Lawson if (buf.Pointer != NULL) 10121e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 10131e4925e8SNate Lawson ACPI_UNLOCK; 10141e4925e8SNate Lawson return_VALUE (valid); 10151e4925e8SNate Lawson } 1016b2566207STakanori Watanabe 1017ce619b2eSNate Lawson static char * 1018ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1019ce619b2eSNate Lawson { 1020ce619b2eSNate Lawson ACPI_HANDLE h; 1021ce619b2eSNate Lawson int i; 1022ce619b2eSNate Lawson 1023ce619b2eSNate Lawson h = acpi_get_handle(dev); 1024ce619b2eSNate Lawson if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1025ce619b2eSNate Lawson return (NULL); 1026ce619b2eSNate Lawson 1027ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1028ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1029ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1030ce619b2eSNate Lawson return (ids[i]); 1031ce619b2eSNate Lawson } 1032ce619b2eSNate Lawson return (NULL); 1033ce619b2eSNate Lawson } 1034ce619b2eSNate Lawson 1035ce619b2eSNate Lawson static ACPI_STATUS 1036ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1037ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1038ce619b2eSNate Lawson { 1039ce619b2eSNate Lawson ACPI_HANDLE h; 1040ce619b2eSNate Lawson 1041ce619b2eSNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 1042ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1043ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1044ce619b2eSNate Lawson } 1045ce619b2eSNate Lawson 1046ce619b2eSNate Lawson static ACPI_STATUS 1047ce619b2eSNate Lawson acpi_device_walk_ns(device_t bus, device_t dev, ACPI_OBJECT_TYPE type, 1048ce619b2eSNate Lawson UINT32 max_depth, ACPI_WALK_CALLBACK user_fn, void *context, void **ret) 1049ce619b2eSNate Lawson { 1050ce619b2eSNate Lawson ACPI_HANDLE h; 1051ce619b2eSNate Lawson 1052ce619b2eSNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 1053ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1054ce619b2eSNate Lawson return (AcpiWalkNamespace(type, h, max_depth, user_fn, context, ret)); 1055ce619b2eSNate Lawson } 1056ce619b2eSNate Lawson 105732d18aa5SMike Smith static int 105832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 105932d18aa5SMike Smith { 10601e4925e8SNate Lawson int result, cid_count, i; 10611e4925e8SNate Lawson uint32_t lid, cids[8]; 1062bc0f2195SMike Smith 1063b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1064bc0f2195SMike Smith 1065bc0f2195SMike Smith /* 1066bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1067bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1068bc0f2195SMike Smith * that to happen, so don't ever return it. 1069bc0f2195SMike Smith */ 1070bc0f2195SMike Smith result = ENXIO; 1071bc0f2195SMike Smith 1072be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1073b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 10741e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1075bc0f2195SMike Smith while (ids && ids->ip_id) { 10761e4925e8SNate Lawson if (lid == ids->ip_id) { 1077bc0f2195SMike Smith result = 0; 1078bc0f2195SMike Smith goto out; 1079bc0f2195SMike Smith } 10801e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 10811e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 10821e4925e8SNate Lawson result = 0; 10831e4925e8SNate Lawson goto out; 10841e4925e8SNate Lawson } 10851e4925e8SNate Lawson } 1086bc0f2195SMike Smith ids++; 1087bc0f2195SMike Smith } 1088be2b1797SNate Lawson 1089bc0f2195SMike Smith out: 1090bc0f2195SMike Smith return_VALUE (result); 1091bc0f2195SMike Smith } 1092bc0f2195SMike Smith 1093bc0f2195SMike Smith /* 109415e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 109515e32d5dSMike Smith * 1096be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1097be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 109815e32d5dSMike Smith */ 109915e32d5dSMike Smith static void 110015e32d5dSMike Smith acpi_probe_children(device_t bus) 110115e32d5dSMike Smith { 110215e32d5dSMike Smith ACPI_HANDLE parent; 1103be2b1797SNate Lawson ACPI_STATUS status; 110415e32d5dSMike Smith int i; 110591233413SNate Lawson static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 110615e32d5dSMike Smith 1107b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1108cb9b0d80SMike Smith ACPI_ASSERTLOCK; 11090ae55423SMike Smith 111015e32d5dSMike Smith /* 111115e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1112edc13633SNate Lawson * we find. We also probe/attach any early devices. 111315e32d5dSMike Smith * 111415e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1115be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1116be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1117be2b1797SNate Lawson * devices as they appear, which might be smarter.) 111815e32d5dSMike Smith */ 11194c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1120be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1121be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1122be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1123be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1124be2b1797SNate Lawson bus, NULL); 1125be2b1797SNate Lawson } 1126be2b1797SNate Lawson } 112715e32d5dSMike Smith 1128edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1129edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1130edc13633SNate Lawson bus_generic_probe(bus); 1131edc13633SNate Lawson 1132edc13633SNate Lawson /* Probe/attach all children, created staticly and from the namespace. */ 11334c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 113415e32d5dSMike Smith bus_generic_attach(bus); 113515e32d5dSMike Smith 113615e32d5dSMike Smith /* 113715e32d5dSMike Smith * Some of these children may have attached others as part of their attach 113815e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 113915e32d5dSMike Smith */ 11404c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 114115e32d5dSMike Smith bus_generic_attach(bus); 11420ae55423SMike Smith 114388a79fc0SNate Lawson /* Attach wake sysctls. */ 11445c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 114588a79fc0SNate Lawson 1146bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 11470ae55423SMike Smith return_VOID; 114815e32d5dSMike Smith } 114915e32d5dSMike Smith 1150b82ca9a9SNate Lawson /* 1151b82ca9a9SNate Lawson * Determine the probe order for a given device and return non-zero if it 1152b82ca9a9SNate Lawson * should be attached immediately. 1153b82ca9a9SNate Lawson */ 115491233413SNate Lawson static int 1155b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 115691233413SNate Lawson { 115791233413SNate Lawson int ret; 115891233413SNate Lawson 1159b82ca9a9SNate Lawson /* 1160b82ca9a9SNate Lawson * 1. I/O port and memory system resource holders 1161b82ca9a9SNate Lawson * 2. Embedded controllers (to handle early accesses) 1162b82ca9a9SNate Lawson */ 116391233413SNate Lawson ret = 0; 116491233413SNate Lawson if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) { 116591233413SNate Lawson *order = 1; 116691233413SNate Lawson ret = 1; 1167b82ca9a9SNate Lawson } else if (acpi_MatchHid(handle, "PNP0C09")) { 116891233413SNate Lawson *order = 2; 116991233413SNate Lawson ret = 1; 117091233413SNate Lawson } 117191233413SNate Lawson 1172b82ca9a9SNate Lawson /* Always probe/attach immediately if we're debugging. */ 1173b82ca9a9SNate Lawson ACPI_DEBUG_EXEC(ret = 1); 1174b82ca9a9SNate Lawson 117591233413SNate Lawson return (ret); 117691233413SNate Lawson } 117791233413SNate Lawson 117815e32d5dSMike Smith /* 117915e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 118015e32d5dSMike Smith * it. 118115e32d5dSMike Smith */ 118215e32d5dSMike Smith static ACPI_STATUS 118315e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 118415e32d5dSMike Smith { 118515e32d5dSMike Smith ACPI_OBJECT_TYPE type; 118691233413SNate Lawson device_t child, bus; 118791233413SNate Lawson int order, probe_now; 118815e32d5dSMike Smith 1189b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 11900ae55423SMike Smith 1191be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 11920ae55423SMike Smith if (acpi_avoid(handle)) 11930ae55423SMike Smith return_ACPI_STATUS (AE_OK); 11940ae55423SMike Smith 119591233413SNate Lawson bus = (device_t)context; 1196b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 119715e32d5dSMike Smith switch (type) { 119815e32d5dSMike Smith case ACPI_TYPE_DEVICE: 119915e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 120015e32d5dSMike Smith case ACPI_TYPE_THERMAL: 120115e32d5dSMike Smith case ACPI_TYPE_POWER: 12020ae55423SMike Smith if (acpi_disabled("children")) 12030ae55423SMike Smith break; 1204be2b1797SNate Lawson 120515e32d5dSMike Smith /* 120615e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 1207b82ca9a9SNate Lawson * so that the probe/attach passes will run breadth-first. Orders 1208b82ca9a9SNate Lawson * less than 10 are reserved for special objects (i.e., system 1209b82ca9a9SNate Lawson * resources). Larger values are used for all other devices. 121015e32d5dSMike Smith */ 1211be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1212be2b1797SNate Lawson acpi_name(handle))); 1213b82ca9a9SNate Lawson order = (level + 1) * 10; 1214b82ca9a9SNate Lawson probe_now = acpi_probe_order(handle, &order); 121591233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1216bc0f2195SMike Smith if (child == NULL) 1217bc0f2195SMike Smith break; 121859a890e6SNate Lawson 121959a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 122015e32d5dSMike Smith acpi_set_handle(child, handle); 122159a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1222bc0f2195SMike Smith 1223bc0f2195SMike Smith /* 1224b519f9d6SMike Smith * Check that the device is present. If it's not present, 1225b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1226b519f9d6SMike Smith * the handle, but we don't probe it). 1227b519f9d6SMike Smith */ 1228be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1229b519f9d6SMike Smith device_disable(child); 1230b519f9d6SMike Smith break; 1231b519f9d6SMike Smith } 1232b519f9d6SMike Smith 1233b519f9d6SMike Smith /* 1234bc0f2195SMike Smith * Get the device's resource settings and attach them. 1235bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1236bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1237bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1238bc0f2195SMike Smith * device not to have any resources. 1239bc0f2195SMike Smith */ 124072ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1241bc0f2195SMike Smith 1242f504b607SNate Lawson /* If order was overridden, probe/attach now rather than later. */ 124391233413SNate Lawson if (probe_now) 124491233413SNate Lawson device_probe_and_attach(child); 12450ae55423SMike Smith break; 124615e32d5dSMike Smith } 124715e32d5dSMike Smith } 1248be2b1797SNate Lawson 12490ae55423SMike Smith return_ACPI_STATUS (AE_OK); 125015e32d5dSMike Smith } 125115e32d5dSMike Smith 125259a890e6SNate Lawson /* 125359a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 125459a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 125559a890e6SNate Lawson */ 125659a890e6SNate Lawson void 125759a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 125859a890e6SNate Lawson { 125959a890e6SNate Lawson } 126059a890e6SNate Lawson 126115e32d5dSMike Smith static void 126215e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 126315e32d5dSMike Smith { 1264d33f4987STakanori Watanabe ACPI_STATUS status; 1265eeb3a05fSNate Lawson 1266eeb3a05fSNate Lawson /* 126780f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 126880f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 126980f0e4c2SNate Lawson * an AP. 1270eeb3a05fSNate Lawson */ 1271eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1272904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1273904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1274904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1275904bf0c2SNate Lawson AcpiFormatException(status)); 1276904bf0c2SNate Lawson return; 1277904bf0c2SNate Lawson } 1278eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 127955398047SNate Lawson ACPI_DISABLE_IRQS(); 1280865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1281be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1282bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 128315e32d5dSMike Smith } else { 128415e32d5dSMike Smith DELAY(1000000); 128515e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 128615e32d5dSMike Smith } 128780f0e4c2SNate Lawson } else { 128880f0e4c2SNate Lawson printf("Shutting down ACPI\n"); 128980f0e4c2SNate Lawson AcpiTerminate(); 129080f0e4c2SNate Lawson } 129115e32d5dSMike Smith } 129215e32d5dSMike Smith 129313d4f7baSMitsuru IWASAKI static void 129413d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 129513d4f7baSMitsuru IWASAKI { 129613d4f7baSMitsuru IWASAKI static int first_time = 1; 129713d4f7baSMitsuru IWASAKI 1298cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1299cb9b0d80SMike Smith 130013d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1301be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 130259ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 130313d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 130433febf93SNate Lawson acpi_event_power_button_sleep, sc); 1305be2b1797SNate Lawson if (first_time) 13066c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 130713d4f7baSMitsuru IWASAKI } 1308be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 130959ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 131013d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 131133febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1312be2b1797SNate Lawson if (first_time) 13136c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 131413d4f7baSMitsuru IWASAKI } 131513d4f7baSMitsuru IWASAKI 131613d4f7baSMitsuru IWASAKI first_time = 0; 131713d4f7baSMitsuru IWASAKI } 131813d4f7baSMitsuru IWASAKI 131915e32d5dSMike Smith /* 1320c5ba0be4SMike Smith * Returns true if the device is actually present and should 1321c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1322c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1323c5ba0be4SMike Smith */ 1324c5ba0be4SMike Smith BOOLEAN 1325c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1326c5ba0be4SMike Smith { 13271e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1328c5ba0be4SMike Smith ACPI_HANDLE h; 13291e4925e8SNate Lawson ACPI_BUFFER buf; 1330c5ba0be4SMike Smith ACPI_STATUS error; 13311e4925e8SNate Lawson int ret; 1332c5ba0be4SMike Smith 1333cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1334cb9b0d80SMike Smith 13351e4925e8SNate Lawson ret = FALSE; 1336c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1337c5ba0be4SMike Smith return (FALSE); 13381e4925e8SNate Lawson buf.Pointer = NULL; 13391e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13406fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13416fca9360SNate Lawson if (ACPI_FAILURE(error)) 1342c5ba0be4SMike Smith return (FALSE); 13431e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1344be2b1797SNate Lawson 1345be2b1797SNate Lawson /* If no _STA method, must be present */ 13461e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 13471e4925e8SNate Lawson ret = TRUE; 1348be2b1797SNate Lawson 1349be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13501e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x9) == 0x9) 13511e4925e8SNate Lawson ret = TRUE; 1352be2b1797SNate Lawson 13531e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13541e4925e8SNate Lawson return (ret); 1355c5ba0be4SMike Smith } 1356c5ba0be4SMike Smith 1357c5ba0be4SMike Smith /* 1358b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1359b53f2771SMike Smith */ 1360b53f2771SMike Smith BOOLEAN 1361b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1362b53f2771SMike Smith { 13631e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1364b53f2771SMike Smith ACPI_HANDLE h; 13651e4925e8SNate Lawson ACPI_BUFFER buf; 1366b53f2771SMike Smith ACPI_STATUS error; 13671e4925e8SNate Lawson int ret; 1368b53f2771SMike Smith 1369b53f2771SMike Smith ACPI_ASSERTLOCK; 1370b53f2771SMike Smith 13711e4925e8SNate Lawson ret = FALSE; 1372b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1373b53f2771SMike Smith return (FALSE); 13741e4925e8SNate Lawson buf.Pointer = NULL; 13751e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13766fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13776fca9360SNate Lawson if (ACPI_FAILURE(error)) 1378b53f2771SMike Smith return (FALSE); 13791e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1380be2b1797SNate Lawson 1381be2b1797SNate Lawson /* If no _STA method, must be present */ 13821e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 13831e4925e8SNate Lawson ret = TRUE; 1384be2b1797SNate Lawson 1385be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13861e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x19) == 0x19) 13871e4925e8SNate Lawson ret = TRUE; 1388be2b1797SNate Lawson 13891e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13901e4925e8SNate Lawson return (ret); 1391b53f2771SMike Smith } 1392b53f2771SMike Smith 1393b53f2771SMike Smith /* 139491233413SNate Lawson * Match a HID string against a handle 139515e32d5dSMike Smith */ 1396ce619b2eSNate Lawson static BOOLEAN 1397ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 139815e32d5dSMike Smith { 13991e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 14001e4925e8SNate Lawson ACPI_BUFFER buf; 140115e32d5dSMike Smith ACPI_STATUS error; 14021e4925e8SNate Lawson int ret, i; 140315e32d5dSMike Smith 1404cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1405cb9b0d80SMike Smith 14061e4925e8SNate Lawson ret = FALSE; 140791233413SNate Lawson if (hid == NULL || h == NULL) 140891233413SNate Lawson return (ret); 14091e4925e8SNate Lawson buf.Pointer = NULL; 14101e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14116fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14126fca9360SNate Lawson if (ACPI_FAILURE(error)) 141391233413SNate Lawson return (ret); 14141e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1415be2b1797SNate Lawson 1416c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1417c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 14181e4925e8SNate Lawson ret = TRUE; 1419c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 14201e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 14211e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 14221e4925e8SNate Lawson ret = TRUE; 14231e4925e8SNate Lawson break; 14241e4925e8SNate Lawson } 14251e4925e8SNate Lawson } 14261e4925e8SNate Lawson } 1427be2b1797SNate Lawson 14281e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14291e4925e8SNate Lawson return (ret); 143015e32d5dSMike Smith } 143115e32d5dSMike Smith 143215e32d5dSMike Smith /* 1433c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1434c5ba0be4SMike Smith * or one if its parents. 1435c5ba0be4SMike Smith */ 1436c5ba0be4SMike Smith ACPI_STATUS 1437c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1438c5ba0be4SMike Smith { 1439c5ba0be4SMike Smith ACPI_HANDLE r; 1440c5ba0be4SMike Smith ACPI_STATUS status; 1441c5ba0be4SMike Smith 1442cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1443cb9b0d80SMike Smith 1444be2b1797SNate Lawson /* Walk back up the tree to the root */ 1445c5ba0be4SMike Smith for (;;) { 1446be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1447be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1448c5ba0be4SMike Smith *result = r; 1449c5ba0be4SMike Smith return (AE_OK); 1450c5ba0be4SMike Smith } 1451c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1452c5ba0be4SMike Smith return (AE_OK); 1453b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1454c5ba0be4SMike Smith return (AE_NOT_FOUND); 1455c5ba0be4SMike Smith parent = r; 1456c5ba0be4SMike Smith } 1457c5ba0be4SMike Smith } 1458c5ba0be4SMike Smith 1459eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1460eea17c34SNate Lawson uint32_t 1461eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1462eea17c34SNate Lawson { 1463eea17c34SNate Lawson uint32_t delta; 1464eea17c34SNate Lawson 1465eea17c34SNate Lawson if (end >= start) 1466eea17c34SNate Lawson delta = end - start; 1467eea17c34SNate Lawson else if (AcpiGbl_FADT->TmrValExt == 0) 14688d01ceefSNate Lawson delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1469eea17c34SNate Lawson else 1470eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 1471eea17c34SNate Lawson return (delta); 1472eea17c34SNate Lawson } 1473eea17c34SNate Lawson 1474c5ba0be4SMike Smith /* 1475c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1476c5ba0be4SMike Smith */ 1477c5ba0be4SMike Smith ACPI_BUFFER * 1478c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1479c5ba0be4SMike Smith { 1480c5ba0be4SMike Smith ACPI_BUFFER *buf; 1481c5ba0be4SMike Smith 1482c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1483c5ba0be4SMike Smith return (NULL); 1484c5ba0be4SMike Smith buf->Length = size; 1485c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1486c5ba0be4SMike Smith return (buf); 1487c5ba0be4SMike Smith } 1488c5ba0be4SMike Smith 1489c310653eSNate Lawson ACPI_STATUS 1490cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1491c310653eSNate Lawson { 1492c310653eSNate Lawson ACPI_OBJECT arg1; 1493c310653eSNate Lawson ACPI_OBJECT_LIST args; 1494c310653eSNate Lawson 1495c310653eSNate Lawson ACPI_ASSERTLOCK; 1496c310653eSNate Lawson 1497c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1498c310653eSNate Lawson arg1.Integer.Value = number; 1499c310653eSNate Lawson args.Count = 1; 1500c310653eSNate Lawson args.Pointer = &arg1; 1501c310653eSNate Lawson 1502c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1503c310653eSNate Lawson } 1504c310653eSNate Lawson 1505c5ba0be4SMike Smith /* 1506c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1507c5ba0be4SMike Smith */ 1508c5ba0be4SMike Smith ACPI_STATUS 1509cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1510c5ba0be4SMike Smith { 1511be2b1797SNate Lawson ACPI_STATUS status; 1512c5ba0be4SMike Smith ACPI_BUFFER buf; 1513c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1514c5ba0be4SMike Smith 1515cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1516cb9b0d80SMike Smith 1517c5ba0be4SMike Smith if (handle == NULL) 1518c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 15190c7da7acSJohn Baldwin 15200c7da7acSJohn Baldwin /* 15210c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 15220c7da7acSJohn Baldwin * a method that will return an Integer. 15230c7da7acSJohn Baldwin */ 1524c5ba0be4SMike Smith buf.Pointer = ¶m; 1525c5ba0be4SMike Smith buf.Length = sizeof(param); 1526be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1527be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1528be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1529c5ba0be4SMike Smith *number = param.Integer.Value; 1530be2b1797SNate Lawson else 1531be2b1797SNate Lawson status = AE_TYPE; 1532c5ba0be4SMike Smith } 15330c7da7acSJohn Baldwin 15340c7da7acSJohn Baldwin /* 15350c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 15360c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 15370c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 15380c7da7acSJohn Baldwin * convert it into an Integer as best we can. 15390c7da7acSJohn Baldwin * 15400c7da7acSJohn Baldwin * This is a hack. 15410c7da7acSJohn Baldwin */ 1542be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1543b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1544be2b1797SNate Lawson status = AE_NO_MEMORY; 15450c7da7acSJohn Baldwin } else { 1546be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1547be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1548be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 15490c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 15500c7da7acSJohn Baldwin } 1551f97739daSNate Lawson } 1552be2b1797SNate Lawson return (status); 1553c5ba0be4SMike Smith } 1554c5ba0be4SMike Smith 1555c573e654SMitsuru IWASAKI ACPI_STATUS 1556cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1557c573e654SMitsuru IWASAKI { 1558c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1559dba55fa2SNate Lawson UINT8 *val; 1560c573e654SMitsuru IWASAKI int i; 1561c573e654SMitsuru IWASAKI 1562c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1563c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1564c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1565c573e654SMitsuru IWASAKI return (AE_OK); 1566c573e654SMitsuru IWASAKI } 1567c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1568c573e654SMitsuru IWASAKI return (AE_TYPE); 1569c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1570c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1571be2b1797SNate Lawson 1572c573e654SMitsuru IWASAKI *number = 0; 1573dba55fa2SNate Lawson val = p->Buffer.Pointer; 1574c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1575dba55fa2SNate Lawson *number += val[i] << (i * 8); 1576c573e654SMitsuru IWASAKI return (AE_OK); 1577c573e654SMitsuru IWASAKI } 1578c573e654SMitsuru IWASAKI 1579c5ba0be4SMike Smith /* 1580c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1581c5ba0be4SMike Smith * function for each element. 1582c5ba0be4SMike Smith * 1583c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1584c5ba0be4SMike Smith */ 1585c5ba0be4SMike Smith ACPI_STATUS 1586be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1587be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1588c5ba0be4SMike Smith { 1589c5ba0be4SMike Smith ACPI_OBJECT *comp; 1590c5ba0be4SMike Smith int i; 1591c5ba0be4SMike Smith 1592be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1593c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1594c5ba0be4SMike Smith 1595be2b1797SNate Lawson /* Iterate over components */ 1596be2b1797SNate Lawson i = 0; 1597be2b1797SNate Lawson comp = pkg->Package.Elements; 1598be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1599c5ba0be4SMike Smith func(comp, arg); 1600c5ba0be4SMike Smith 1601c5ba0be4SMike Smith return (AE_OK); 1602c5ba0be4SMike Smith } 1603c5ba0be4SMike Smith 16046f69255bSMike Smith /* 16056f69255bSMike Smith * Find the (index)th resource object in a set. 16066f69255bSMike Smith */ 16076f69255bSMike Smith ACPI_STATUS 16086d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 16096f69255bSMike Smith { 16106d63101aSMike Smith ACPI_RESOURCE *rp; 16116f69255bSMike Smith int i; 16126f69255bSMike Smith 16136d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16146f69255bSMike Smith i = index; 16156d63101aSMike Smith while (i-- > 0) { 1616be2b1797SNate Lawson /* Range check */ 16176d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16186f69255bSMike Smith return (AE_BAD_PARAMETER); 1619be2b1797SNate Lawson 1620be2b1797SNate Lawson /* Check for terminator */ 1621be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16226d63101aSMike Smith return (AE_NOT_FOUND); 1623abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16246f69255bSMike Smith } 16256f69255bSMike Smith if (resp != NULL) 16266d63101aSMike Smith *resp = rp; 1627be2b1797SNate Lawson 16286d63101aSMike Smith return (AE_OK); 16296d63101aSMike Smith } 16306d63101aSMike Smith 16316d63101aSMike Smith /* 16326d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 16336d63101aSMike Smith * 16346d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 16356d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 16366d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 16376d63101aSMike Smith * resources. 16386d63101aSMike Smith */ 16396d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 16406d63101aSMike Smith 16416d63101aSMike Smith ACPI_STATUS 16426d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 16436d63101aSMike Smith { 16446d63101aSMike Smith ACPI_RESOURCE *rp; 16456d63101aSMike Smith void *newp; 16466d63101aSMike Smith 1647be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 16486d63101aSMike Smith if (buf->Pointer == NULL) { 16496d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 16506d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 16516d63101aSMike Smith return (AE_NO_MEMORY); 16526d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16536d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 16546d63101aSMike Smith rp->Length = 0; 16556d63101aSMike Smith } 16566d63101aSMike Smith if (res == NULL) 16576d63101aSMike Smith return (AE_OK); 16586d63101aSMike Smith 16596d63101aSMike Smith /* 16606d63101aSMike Smith * Scan the current buffer looking for the terminator. 16616d63101aSMike Smith * This will either find the terminator or hit the end 16626d63101aSMike Smith * of the buffer and return an error. 16636d63101aSMike Smith */ 16646d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16656d63101aSMike Smith for (;;) { 1666be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 16676d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16686d63101aSMike Smith return (AE_BAD_PARAMETER); 1669be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16706d63101aSMike Smith break; 1671abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16726d63101aSMike Smith } 16736d63101aSMike Smith 16746d63101aSMike Smith /* 16756d63101aSMike Smith * Check the size of the buffer and expand if required. 16766d63101aSMike Smith * 16776d63101aSMike Smith * Required size is: 16786d63101aSMike Smith * size of existing resources before terminator + 16796d63101aSMike Smith * size of new resource and header + 16806d63101aSMike Smith * size of terminator. 16816d63101aSMike Smith * 16826d63101aSMike Smith * Note that this loop should really only run once, unless 16836d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 16846d63101aSMike Smith */ 16856d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 16866d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 16876d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 16886d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 16896d63101aSMike Smith return (AE_NO_MEMORY); 16906d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1691a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1692a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 16936d63101aSMike Smith AcpiOsFree(buf->Pointer); 16946d63101aSMike Smith buf->Pointer = newp; 16956d63101aSMike Smith buf->Length += buf->Length; 16966d63101aSMike Smith } 16976d63101aSMike Smith 1698be2b1797SNate Lawson /* Insert the new resource. */ 16996d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 17006d63101aSMike Smith 1701be2b1797SNate Lawson /* And add the terminator. */ 1702abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 17036d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 17046d63101aSMike Smith rp->Length = 0; 17056d63101aSMike Smith 17066f69255bSMike Smith return (AE_OK); 17076f69255bSMike Smith } 1708c5ba0be4SMike Smith 1709da14ac9fSJohn Baldwin /* 1710da14ac9fSJohn Baldwin * Set interrupt model. 1711da14ac9fSJohn Baldwin */ 1712da14ac9fSJohn Baldwin ACPI_STATUS 1713da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1714da14ac9fSJohn Baldwin { 1715da14ac9fSJohn Baldwin 1716c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1717da14ac9fSJohn Baldwin } 1718da14ac9fSJohn Baldwin 1719ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1720ece50487SMitsuru IWASAKI 1721ece50487SMitsuru IWASAKI static void 1722ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1723ece50487SMitsuru IWASAKI { 1724ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1725ece50487SMitsuru IWASAKI } 1726c30382dfSMitsuru IWASAKI 172715e32d5dSMike Smith /* 172815e32d5dSMike Smith * Set the system sleep state 172915e32d5dSMike Smith * 1730be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 173115e32d5dSMike Smith */ 173215e32d5dSMike Smith ACPI_STATUS 173315e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 173415e32d5dSMike Smith { 173515e32d5dSMike Smith ACPI_STATUS status = AE_OK; 173656d8cb57SMitsuru IWASAKI UINT8 TypeA; 173756d8cb57SMitsuru IWASAKI UINT8 TypeB; 173815e32d5dSMike Smith 1739b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1740cb9b0d80SMike Smith ACPI_ASSERTLOCK; 17410ae55423SMike Smith 174298175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 17436397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 174498175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 17456397624dSMitsuru IWASAKI 174698175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1747ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1748ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1749ece50487SMitsuru IWASAKI 175015e32d5dSMike Smith switch (state) { 175115e32d5dSMike Smith case ACPI_STATE_S1: 17526161544cSTakanori Watanabe case ACPI_STATE_S2: 17536161544cSTakanori Watanabe case ACPI_STATE_S3: 17546161544cSTakanori Watanabe case ACPI_STATE_S4: 1755be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 175698175614SNate Lawson if (status == AE_NOT_FOUND) { 175798175614SNate Lawson device_printf(sc->acpi_dev, 175898175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 175998175614SNate Lawson break; 176098175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1761be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1762be2b1797SNate Lawson AcpiFormatException(status)); 176356d8cb57SMitsuru IWASAKI break; 176456d8cb57SMitsuru IWASAKI } 176556d8cb57SMitsuru IWASAKI 1766a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1767a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1768a1fccb47SMitsuru IWASAKI 1769d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 1770d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 1771e8b4d56eSNate Lawson 1772be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 177315e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 177415e32d5dSMike Smith /* 177515e32d5dSMike Smith * Re-wake the system. 177615e32d5dSMike Smith * 177715e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 177815e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 177915e32d5dSMike Smith * current bus interface does not provide for this. 178015e32d5dSMike Smith */ 178115e32d5dSMike Smith DEVICE_RESUME(root_bus); 17820ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 178315e32d5dSMike Smith } 1784b9c780d6SMitsuru IWASAKI 1785be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1786be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1787b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1788b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1789b9c780d6SMitsuru IWASAKI break; 1790b9c780d6SMitsuru IWASAKI } 1791b9c780d6SMitsuru IWASAKI 1792be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1793ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1794ff01efb5SMitsuru IWASAKI 17956161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 17966161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 17976161544cSTakanori Watanabe 1798be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 179959ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 180059ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 180159ddeb18SNate Lawson 18026161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1803a1fccb47SMitsuru IWASAKI } 18046161544cSTakanori Watanabe 18056161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1806be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1807964679ceSMitsuru IWASAKI AcpiEnable(); 18086161544cSTakanori Watanabe } else { 1809be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1810be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1811be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1812be2b1797SNate Lawson AcpiFormatException(status)); 1813c30382dfSMitsuru IWASAKI break; 181415e32d5dSMike Smith } 18156161544cSTakanori Watanabe } 1816d4b9ff91SNate Lawson 1817d4b9ff91SNate Lawson /* Resume devices, re-enable GPEs and fixed events. */ 1818d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 1819ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 182015e32d5dSMike Smith DEVICE_RESUME(root_bus); 182115e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 182213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 182315e32d5dSMike Smith break; 182415e32d5dSMike Smith case ACPI_STATE_S5: 182515e32d5dSMike Smith /* 182615e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 182715e32d5dSMike Smith * shutdown handlers. 182815e32d5dSMike Smith */ 182915e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 183015e32d5dSMike Smith break; 183198175614SNate Lawson case ACPI_STATE_S0: 183215e32d5dSMike Smith default: 183315e32d5dSMike Smith status = AE_BAD_PARAMETER; 183415e32d5dSMike Smith break; 183515e32d5dSMike Smith } 1836ece50487SMitsuru IWASAKI 183798175614SNate Lawson /* Disable a second sleep request for a short period */ 1838ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1839ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1840ece50487SMitsuru IWASAKI 18410ae55423SMike Smith return_ACPI_STATUS (status); 184215e32d5dSMike Smith } 184315e32d5dSMike Smith 1844e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1845e8b4d56eSNate Lawson int 1846e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1847e8b4d56eSNate Lawson { 1848e8b4d56eSNate Lawson struct acpi_prw_data prw; 1849e8b4d56eSNate Lawson 1850e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1851e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1852e8b4d56eSNate Lawson return (ENXIO); 1853e8b4d56eSNate Lawson 1854e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1855e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1856e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1857e8b4d56eSNate Lawson return (ENXIO); 1858e8b4d56eSNate Lawson } 1859e8b4d56eSNate Lawson 1860e8b4d56eSNate Lawson return (0); 1861e8b4d56eSNate Lawson } 1862e8b4d56eSNate Lawson 1863e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1864e8b4d56eSNate Lawson int 1865e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1866e8b4d56eSNate Lawson { 1867e8b4d56eSNate Lawson struct acpi_prw_data prw; 1868e8b4d56eSNate Lawson ACPI_HANDLE handle; 1869e8b4d56eSNate Lawson ACPI_STATUS status; 1870e8b4d56eSNate Lawson int flags; 1871e8b4d56eSNate Lawson 1872d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 1873e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1874e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1875e8b4d56eSNate Lawson return (ENXIO); 1876e8b4d56eSNate Lawson 1877d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 1878e8b4d56eSNate Lawson if (enable) { 1879e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1880e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1881e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1882e8b4d56eSNate Lawson return (ENXIO); 1883e8b4d56eSNate Lawson } 1884d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1885e8b4d56eSNate Lawson } else { 1886e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1887e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1888e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1889e8b4d56eSNate Lawson return (ENXIO); 1890e8b4d56eSNate Lawson } 1891d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1892e8b4d56eSNate Lawson } 1893e8b4d56eSNate Lawson 1894e8b4d56eSNate Lawson return (0); 1895e8b4d56eSNate Lawson } 1896e8b4d56eSNate Lawson 1897d4b9ff91SNate Lawson static int 1898d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 1899e8b4d56eSNate Lawson { 1900e8b4d56eSNate Lawson struct acpi_prw_data prw; 1901d4b9ff91SNate Lawson device_t dev; 1902e8b4d56eSNate Lawson 1903d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 1904e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1905e8b4d56eSNate Lawson return (ENXIO); 1906d4b9ff91SNate Lawson dev = acpi_get_device(handle); 1907e8b4d56eSNate Lawson 1908e8b4d56eSNate Lawson /* 1909d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 1910d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 1911d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 1912d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 1913d4b9ff91SNate Lawson * and set _PSW. 1914e8b4d56eSNate Lawson */ 1915d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 1916cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1917e8b4d56eSNate Lawson if (bootverbose) 1918d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 1919d4b9ff91SNate Lawson acpi_name(handle), sstate); 1920d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 1921d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 1922d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 1923d4b9ff91SNate Lawson if (bootverbose) 1924d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 1925d4b9ff91SNate Lawson acpi_name(handle), sstate); 1926d4b9ff91SNate Lawson } 1927cc85c78cSNate Lawson 1928cc85c78cSNate Lawson return (0); 1929e8b4d56eSNate Lawson } 1930e8b4d56eSNate Lawson 1931d4b9ff91SNate Lawson static int 1932d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 1933cc85c78cSNate Lawson { 1934cc85c78cSNate Lawson struct acpi_prw_data prw; 1935d4b9ff91SNate Lawson device_t dev; 1936cc85c78cSNate Lawson 1937cc85c78cSNate Lawson /* 1938d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 1939d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 1940cc85c78cSNate Lawson */ 1941d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1942d4b9ff91SNate Lawson return (ENXIO); 1943d4b9ff91SNate Lawson dev = acpi_get_device(handle); 1944d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 1945d4b9ff91SNate Lawson return (0); 1946cc85c78cSNate Lawson 1947d4b9ff91SNate Lawson /* 1948d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 1949d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 1950d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 1951d4b9ff91SNate Lawson */ 1952d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 1953cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1954d4b9ff91SNate Lawson if (bootverbose) 1955d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 1956d4b9ff91SNate Lawson } else { 1957d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1958d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 1959d4b9ff91SNate Lawson if (bootverbose) 1960d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 1961d4b9ff91SNate Lawson acpi_name(handle)); 1962d4b9ff91SNate Lawson } 1963d4b9ff91SNate Lawson 1964e8b4d56eSNate Lawson return (0); 1965e8b4d56eSNate Lawson } 1966e8b4d56eSNate Lawson 1967e8b4d56eSNate Lawson static ACPI_STATUS 1968d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 1969e8b4d56eSNate Lawson { 1970d4b9ff91SNate Lawson int sstate; 1971e8b4d56eSNate Lawson 1972d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 1973d4b9ff91SNate Lawson sstate = *(int *)context; 1974d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 1975d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 1976d4b9ff91SNate Lawson else 1977d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 1978e8b4d56eSNate Lawson return (AE_OK); 1979e8b4d56eSNate Lawson } 1980e8b4d56eSNate Lawson 1981d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 1982e8b4d56eSNate Lawson static int 1983d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 1984e8b4d56eSNate Lawson { 1985e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 1986e8b4d56eSNate Lawson 1987e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 1988d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 1989d4b9ff91SNate Lawson acpi_wake_prep, &sstate, NULL); 1990e8b4d56eSNate Lawson return (0); 1991e8b4d56eSNate Lawson } 1992e8b4d56eSNate Lawson 199388a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 199488a79fc0SNate Lawson static int 199588a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 199688a79fc0SNate Lawson { 199788a79fc0SNate Lawson int error, i, numdevs; 199888a79fc0SNate Lawson device_t *devlist; 199988a79fc0SNate Lawson device_t child; 2000d4b9ff91SNate Lawson ACPI_STATUS status; 200188a79fc0SNate Lawson 200288a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 200388a79fc0SNate Lawson if (error != 0 || numdevs == 0) 200488a79fc0SNate Lawson return (error); 200588a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 200688a79fc0SNate Lawson child = devlist[i]; 2007d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 200888a79fc0SNate Lawson if (!device_is_attached(child)) 200988a79fc0SNate Lawson continue; 2010d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2011d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 201288a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 201388a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 201488a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 201588a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 201688a79fc0SNate Lawson } 201788a79fc0SNate Lawson } 201888a79fc0SNate Lawson free(devlist, M_TEMP); 201988a79fc0SNate Lawson 202088a79fc0SNate Lawson return (0); 202188a79fc0SNate Lawson } 202288a79fc0SNate Lawson 202388a79fc0SNate Lawson /* Enable or disable wake from userland. */ 202488a79fc0SNate Lawson static int 202588a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 202688a79fc0SNate Lawson { 202788a79fc0SNate Lawson int enable, error; 202888a79fc0SNate Lawson device_t dev; 202988a79fc0SNate Lawson 203088a79fc0SNate Lawson dev = (device_t)arg1; 2031d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 203288a79fc0SNate Lawson 203388a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 203488a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 203588a79fc0SNate Lawson return (error); 203688a79fc0SNate Lawson if (enable != 0 && enable != 1) 203788a79fc0SNate Lawson return (EINVAL); 203888a79fc0SNate Lawson 203988a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 204088a79fc0SNate Lawson } 204188a79fc0SNate Lawson 2042e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2043d4b9ff91SNate Lawson int 2044e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2045e8b4d56eSNate Lawson { 2046e8b4d56eSNate Lawson ACPI_STATUS status; 2047e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2048e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2049d4b9ff91SNate Lawson int error, i, power_count; 2050e8b4d56eSNate Lawson 2051e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2052e8b4d56eSNate Lawson return (EINVAL); 2053e8b4d56eSNate Lawson 2054e8b4d56eSNate Lawson /* 2055e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2056e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2057e8b4d56eSNate Lawson */ 2058e8b4d56eSNate Lawson error = EINVAL; 2059e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2060e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2061e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2062e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2063e8b4d56eSNate Lawson return (ENOENT); 2064e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2065e8b4d56eSNate Lawson if (res == NULL) 2066e8b4d56eSNate Lawson return (ENOENT); 2067e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2068e8b4d56eSNate Lawson goto out; 2069e8b4d56eSNate Lawson 2070e8b4d56eSNate Lawson /* 2071e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2072e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2073e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2074e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2075e8b4d56eSNate Lawson */ 2076e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2077e8b4d56eSNate Lawson goto out; 2078e8b4d56eSNate Lawson 2079e8b4d56eSNate Lawson /* 2080e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2081e8b4d56eSNate Lawson */ 2082e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2083e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2084e8b4d56eSNate Lawson /* 2085e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2086e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2087e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2088e8b4d56eSNate Lawson * enabled for the wake event. 2089e8b4d56eSNate Lawson */ 2090e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2091e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2092e8b4d56eSNate Lawson error = 0; 2093e8b4d56eSNate Lawson break; 2094e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2095e8b4d56eSNate Lawson /* 2096e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2097e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2098e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2099e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2100e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2101e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2102e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2103e8b4d56eSNate Lawson * the wake event. 2104e8b4d56eSNate Lawson * 2105e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2106e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2107e8b4d56eSNate Lawson */ 2108e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2109e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2110e8b4d56eSNate Lawson goto out; 2111e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2112e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2113e8b4d56eSNate Lawson goto out; 2114e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2115e8b4d56eSNate Lawson goto out; 2116e8b4d56eSNate Lawson error = 0; 2117e8b4d56eSNate Lawson break; 2118e8b4d56eSNate Lawson default: 2119e8b4d56eSNate Lawson goto out; 2120e8b4d56eSNate Lawson } 2121e8b4d56eSNate Lawson 2122d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 2123d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 2124d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 2125d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2126d4b9ff91SNate Lawson power_count = 0; 2127d4b9ff91SNate Lawson } 2128d4b9ff91SNate Lawson prw->power_res_count = power_count; 2129d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 2130d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 2131e8b4d56eSNate Lawson 2132e8b4d56eSNate Lawson out: 2133e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2134e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2135e8b4d56eSNate Lawson return (error); 2136e8b4d56eSNate Lawson } 2137e8b4d56eSNate Lawson 213815e32d5dSMike Smith /* 213915e32d5dSMike Smith * Enable/Disable ACPI 214015e32d5dSMike Smith */ 214115e32d5dSMike Smith ACPI_STATUS 214215e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 214315e32d5dSMike Smith { 214415e32d5dSMike Smith ACPI_STATUS status; 214515e32d5dSMike Smith u_int32_t flags; 214615e32d5dSMike Smith 2147b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2148cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21490ae55423SMike Smith 215015e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 215115e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 2152be2b1797SNate Lawson if (!sc->acpi_enabled) 215315e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 2154be2b1797SNate Lawson else 215515e32d5dSMike Smith status = AE_OK; 2156be2b1797SNate Lawson 215715e32d5dSMike Smith if (status == AE_OK) 215815e32d5dSMike Smith sc->acpi_enabled = 1; 2159be2b1797SNate Lawson 21600ae55423SMike Smith return_ACPI_STATUS (status); 216115e32d5dSMike Smith } 216215e32d5dSMike Smith 216315e32d5dSMike Smith ACPI_STATUS 216415e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 216515e32d5dSMike Smith { 216615e32d5dSMike Smith ACPI_STATUS status; 216715e32d5dSMike Smith 2168b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2169cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21700ae55423SMike Smith 2171be2b1797SNate Lawson if (sc->acpi_enabled) 217215e32d5dSMike Smith status = AcpiDisable(); 2173be2b1797SNate Lawson else 217415e32d5dSMike Smith status = AE_OK; 2175be2b1797SNate Lawson 217615e32d5dSMike Smith if (status == AE_OK) 217715e32d5dSMike Smith sc->acpi_enabled = 0; 2178be2b1797SNate Lawson 21790ae55423SMike Smith return_ACPI_STATUS (status); 218015e32d5dSMike Smith } 218115e32d5dSMike Smith 218215e32d5dSMike Smith /* 218315e32d5dSMike Smith * ACPI Event Handlers 218415e32d5dSMike Smith */ 218515e32d5dSMike Smith 218615e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 218715e32d5dSMike Smith 218815e32d5dSMike Smith static void 218915e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 219015e32d5dSMike Smith { 2191fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2192b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 219315e32d5dSMike Smith 2194cb9b0d80SMike Smith ACPI_LOCK; 2195a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 219615e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2197cb9b0d80SMike Smith ACPI_UNLOCK; 21980ae55423SMike Smith return_VOID; 219915e32d5dSMike Smith } 220015e32d5dSMike Smith 220115e32d5dSMike Smith static void 220215e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 220315e32d5dSMike Smith { 2204fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2205b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 22060ae55423SMike Smith 220715e32d5dSMike Smith /* Well, what to do? :-) */ 22080ae55423SMike Smith 2209cb9b0d80SMike Smith ACPI_LOCK; 2210cb9b0d80SMike Smith ACPI_UNLOCK; 2211cb9b0d80SMike Smith 22120ae55423SMike Smith return_VOID; 221315e32d5dSMike Smith } 221415e32d5dSMike Smith 221515e32d5dSMike Smith /* 221615e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 221715e32d5dSMike Smith */ 221815e32d5dSMike Smith UINT32 221933febf93SNate Lawson acpi_event_power_button_sleep(void *context) 222015e32d5dSMike Smith { 222115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 222215e32d5dSMike Smith 2223b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22240ae55423SMike Smith 222515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 22260ae55423SMike Smith 2227b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 222815e32d5dSMike Smith } 222915e32d5dSMike Smith 223015e32d5dSMike Smith UINT32 223133febf93SNate Lawson acpi_event_power_button_wake(void *context) 223215e32d5dSMike Smith { 223315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 223415e32d5dSMike Smith 2235b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22360ae55423SMike Smith 223715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 22380ae55423SMike Smith 2239b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 224015e32d5dSMike Smith } 224115e32d5dSMike Smith 224215e32d5dSMike Smith UINT32 224333febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 224415e32d5dSMike Smith { 224515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 224615e32d5dSMike Smith 2247b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22480ae55423SMike Smith 224915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 22500ae55423SMike Smith 2251b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 225215e32d5dSMike Smith } 225315e32d5dSMike Smith 225415e32d5dSMike Smith UINT32 225533febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 225615e32d5dSMike Smith { 225715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 225815e32d5dSMike Smith 2259b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22600ae55423SMike Smith 226115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 22620ae55423SMike Smith 2263b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 226415e32d5dSMike Smith } 226515e32d5dSMike Smith 226615e32d5dSMike Smith /* 226715e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 226815e32d5dSMike Smith */ 226915e32d5dSMike Smith struct acpi_staticbuf { 227015e32d5dSMike Smith ACPI_BUFFER buffer; 227115e32d5dSMike Smith char data[512]; 227215e32d5dSMike Smith }; 227315e32d5dSMike Smith 227415e32d5dSMike Smith char * 227515e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 227615e32d5dSMike Smith { 227715e32d5dSMike Smith static struct acpi_staticbuf buf; 227815e32d5dSMike Smith 2279cb9b0d80SMike Smith ACPI_ASSERTLOCK; 2280cb9b0d80SMike Smith 228115e32d5dSMike Smith buf.buffer.Length = 512; 228215e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 228315e32d5dSMike Smith 2284b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 228515e32d5dSMike Smith return (buf.buffer.Pointer); 2286be2b1797SNate Lawson 228715e32d5dSMike Smith return ("(unknown path)"); 228815e32d5dSMike Smith } 228915e32d5dSMike Smith 229015e32d5dSMike Smith /* 229115e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 229215e32d5dSMike Smith * parts of the namespace. 229315e32d5dSMike Smith */ 229415e32d5dSMike Smith int 229515e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 229615e32d5dSMike Smith { 22973c600cfbSJohn Baldwin char *cp, *env, *np; 229815e32d5dSMike Smith int len; 229915e32d5dSMike Smith 230015e32d5dSMike Smith np = acpi_name(handle); 230115e32d5dSMike Smith if (*np == '\\') 230215e32d5dSMike Smith np++; 23033c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 230415e32d5dSMike Smith return (0); 230515e32d5dSMike Smith 2306be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 23073c600cfbSJohn Baldwin cp = env; 230815e32d5dSMike Smith for (;;) { 230915e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 231015e32d5dSMike Smith cp++; 231115e32d5dSMike Smith if (*cp == 0) 231215e32d5dSMike Smith break; 231315e32d5dSMike Smith len = 0; 231415e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 231515e32d5dSMike Smith len++; 2316d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 23173c600cfbSJohn Baldwin freeenv(env); 23180ae55423SMike Smith return(1); 2319d786139cSMaxime Henrion } 23200ae55423SMike Smith cp += len; 23210ae55423SMike Smith } 23223c600cfbSJohn Baldwin freeenv(env); 2323be2b1797SNate Lawson 23240ae55423SMike Smith return (0); 23250ae55423SMike Smith } 23260ae55423SMike Smith 23270ae55423SMike Smith /* 23280ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 23290ae55423SMike Smith */ 23300ae55423SMike Smith int 23310ae55423SMike Smith acpi_disabled(char *subsys) 23320ae55423SMike Smith { 233378689b15SMaxime Henrion char *cp, *env; 23340ae55423SMike Smith int len; 23350ae55423SMike Smith 23363184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 23370ae55423SMike Smith return (0); 23383184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 233978689b15SMaxime Henrion freeenv(env); 23400ae55423SMike Smith return (1); 2341d786139cSMaxime Henrion } 23420ae55423SMike Smith 23433184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 234478689b15SMaxime Henrion cp = env; 23450ae55423SMike Smith for (;;) { 23463184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 23470ae55423SMike Smith cp++; 23483184cf5aSNate Lawson if (*cp == '\0') 23490ae55423SMike Smith break; 23500ae55423SMike Smith len = 0; 23513184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 23520ae55423SMike Smith len++; 23533184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 235478689b15SMaxime Henrion freeenv(env); 235515e32d5dSMike Smith return (1); 2356d786139cSMaxime Henrion } 235715e32d5dSMike Smith cp += len; 235815e32d5dSMike Smith } 235978689b15SMaxime Henrion freeenv(env); 2360be2b1797SNate Lawson 236115e32d5dSMike Smith return (0); 236215e32d5dSMike Smith } 236315e32d5dSMike Smith 236415e32d5dSMike Smith /* 236515e32d5dSMike Smith * Control interface. 236615e32d5dSMike Smith * 23670ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2368be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2369be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 237015e32d5dSMike Smith */ 23710ae55423SMike Smith struct acpi_ioctl_hook 23720ae55423SMike Smith { 23730ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 23740ae55423SMike Smith u_long cmd; 2375be2b1797SNate Lawson acpi_ioctl_fn fn; 23760ae55423SMike Smith void *arg; 23770ae55423SMike Smith }; 23780ae55423SMike Smith 23790ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 23800ae55423SMike Smith static int acpi_ioctl_hooks_initted; 23810ae55423SMike Smith 23820ae55423SMike Smith /* 23830ae55423SMike Smith * Register an ioctl handler. 23840ae55423SMike Smith */ 23850ae55423SMike Smith int 2386be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 23870ae55423SMike Smith { 23880ae55423SMike Smith struct acpi_ioctl_hook *hp; 23890ae55423SMike Smith 23900ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 23910ae55423SMike Smith return (ENOMEM); 23920ae55423SMike Smith hp->cmd = cmd; 23930ae55423SMike Smith hp->fn = fn; 23940ae55423SMike Smith hp->arg = arg; 23950ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 23960ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 23970ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 23980ae55423SMike Smith } 23990ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 24000ae55423SMike Smith return (0); 24010ae55423SMike Smith } 24020ae55423SMike Smith 24030ae55423SMike Smith /* 24040ae55423SMike Smith * Deregister an ioctl handler. 24050ae55423SMike Smith */ 24060ae55423SMike Smith void 2407be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 24080ae55423SMike Smith { 24090ae55423SMike Smith struct acpi_ioctl_hook *hp; 24100ae55423SMike Smith 24110ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 24120ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 24130ae55423SMike Smith break; 24140ae55423SMike Smith 24150ae55423SMike Smith if (hp != NULL) { 24160ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 24170ae55423SMike Smith free(hp, M_ACPIDEV); 24180ae55423SMike Smith } 24190ae55423SMike Smith } 24200ae55423SMike Smith 242115e32d5dSMike Smith static int 242289c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 242315e32d5dSMike Smith { 242415e32d5dSMike Smith return (0); 242515e32d5dSMike Smith } 242615e32d5dSMike Smith 242715e32d5dSMike Smith static int 242889c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 242915e32d5dSMike Smith { 243015e32d5dSMike Smith return (0); 243115e32d5dSMike Smith } 243215e32d5dSMike Smith 243315e32d5dSMike Smith static int 243489c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 243515e32d5dSMike Smith { 243615e32d5dSMike Smith struct acpi_softc *sc; 24370ae55423SMike Smith struct acpi_ioctl_hook *hp; 24380ae55423SMike Smith int error, xerror, state; 2439fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 244015e32d5dSMike Smith 2441cb9b0d80SMike Smith ACPI_LOCK; 2442cb9b0d80SMike Smith 244315e32d5dSMike Smith error = state = 0; 244415e32d5dSMike Smith sc = dev->si_drv1; 244515e32d5dSMike Smith 24460ae55423SMike Smith /* 24470ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 24480ae55423SMike Smith */ 24490ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 24500ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 24510ae55423SMike Smith if (hp->cmd == cmd) { 24520ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 24530ae55423SMike Smith if (xerror != 0) 24540ae55423SMike Smith error = xerror; 2455917d44c8SMitsuru IWASAKI goto out; 24560ae55423SMike Smith } 24570ae55423SMike Smith } 24580ae55423SMike Smith } 24590ae55423SMike Smith 24600ae55423SMike Smith /* 2461a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2462a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2463a89fcf28STakanori Watanabe * Not changing system behavior. 2464a89fcf28STakanori Watanabe */ 2465be2b1797SNate Lawson if ((flag & FWRITE) == 0) 2466be2b1797SNate Lawson return (EPERM); 2467a89fcf28STakanori Watanabe 2468be2b1797SNate Lawson /* Core system ioctls. */ 246915e32d5dSMike Smith switch (cmd) { 247015e32d5dSMike Smith case ACPIIO_ENABLE: 24710ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 247215e32d5dSMike Smith error = ENXIO; 247315e32d5dSMike Smith break; 247415e32d5dSMike Smith case ACPIIO_DISABLE: 24750ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 247615e32d5dSMike Smith error = ENXIO; 247715e32d5dSMike Smith break; 247815e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 247915e32d5dSMike Smith if (!sc->acpi_enabled) { 248015e32d5dSMike Smith error = ENXIO; 248115e32d5dSMike Smith break; 248215e32d5dSMike Smith } 248315e32d5dSMike Smith state = *(int *)addr; 24842d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 24852d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 248615e32d5dSMike Smith error = EINVAL; 24872d610c46SNate Lawson } else { 24882d610c46SNate Lawson error = EINVAL; 24892d610c46SNate Lawson } 249015e32d5dSMike Smith break; 249115e32d5dSMike Smith default: 24920ae55423SMike Smith if (error == 0) 249315e32d5dSMike Smith error = EINVAL; 249415e32d5dSMike Smith break; 249515e32d5dSMike Smith } 2496917d44c8SMitsuru IWASAKI 2497917d44c8SMitsuru IWASAKI out: 2498cb9b0d80SMike Smith ACPI_UNLOCK; 249915e32d5dSMike Smith return (error); 250015e32d5dSMike Smith } 250115e32d5dSMike Smith 25021d073b1dSJohn Baldwin static int 2503d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2504d75de536SMitsuru IWASAKI { 2505d75de536SMitsuru IWASAKI char sleep_state[4]; 2506d75de536SMitsuru IWASAKI char buf[16]; 2507d75de536SMitsuru IWASAKI int error; 2508d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2509d75de536SMitsuru IWASAKI 2510d75de536SMitsuru IWASAKI buf[0] = '\0'; 2511d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) { 2512d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2513d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2514d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2515d75de536SMitsuru IWASAKI } 2516d75de536SMitsuru IWASAKI } 2517d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2518d75de536SMitsuru IWASAKI return (error); 2519d75de536SMitsuru IWASAKI } 2520d75de536SMitsuru IWASAKI 2521d75de536SMitsuru IWASAKI static int 25221d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 25231d073b1dSJohn Baldwin { 25241d073b1dSJohn Baldwin char sleep_state[10]; 25251d073b1dSJohn Baldwin int error; 25261d073b1dSJohn Baldwin u_int new_state, old_state; 25271d073b1dSJohn Baldwin 25281d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2529b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX + 1) { 25301d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2531cb9b0d80SMike Smith } else { 2532b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 25331d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 25341d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2535cb9b0d80SMike Smith } 25361d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 25371d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2538be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2539be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 25401d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 25411d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 25421d073b1dSJohn Baldwin break; 2543cb9b0d80SMike Smith } 2544931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2545be2b1797SNate Lawson if (new_state != old_state) 25461d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2547cb9b0d80SMike Smith } else { 25481d073b1dSJohn Baldwin error = EINVAL; 25491d073b1dSJohn Baldwin } 2550cb9b0d80SMike Smith } 2551be2b1797SNate Lawson 25521d073b1dSJohn Baldwin return (error); 25531d073b1dSJohn Baldwin } 25541d073b1dSJohn Baldwin 25559b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 25569b937d48SNate Lawson void 25579b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 25589b937d48SNate Lawson { 25599b937d48SNate Lawson char notify_buf[16]; 25609b937d48SNate Lawson ACPI_BUFFER handle_buf; 25619b937d48SNate Lawson ACPI_STATUS status; 25629b937d48SNate Lawson 25639b937d48SNate Lawson if (subsystem == NULL) 25649b937d48SNate Lawson return; 25659b937d48SNate Lawson 25669b937d48SNate Lawson handle_buf.Pointer = NULL; 25679b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 25689b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 25699b937d48SNate Lawson if (ACPI_FAILURE(status)) 25709b937d48SNate Lawson return; 25719b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 25729b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 25739b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 25749b937d48SNate Lawson } 25759b937d48SNate Lawson 257615e32d5dSMike Smith #ifdef ACPI_DEBUG 25770ae55423SMike Smith /* 25780ae55423SMike Smith * Support for parsing debug options from the kernel environment. 25790ae55423SMike Smith * 25800ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 25810ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 25820ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 25830ae55423SMike Smith * prefixing the bit name with !. 25840ae55423SMike Smith */ 258515e32d5dSMike Smith struct debugtag 258615e32d5dSMike Smith { 258715e32d5dSMike Smith char *name; 258815e32d5dSMike Smith UINT32 value; 258915e32d5dSMike Smith }; 259015e32d5dSMike Smith 259115e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2592c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2593c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2594c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2595c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2596c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2597c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2598c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2599c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2600c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2601d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 26024c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2603d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2604297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 26054c1cdee6SMike Smith 2606c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2607c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 26083184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2609c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 26103184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 26113184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 26123184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 26134c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2614cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 26153184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2616b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 261715e32d5dSMike Smith {NULL, 0} 261815e32d5dSMike Smith }; 261915e32d5dSMike Smith 262015e32d5dSMike Smith static struct debugtag dbg_level[] = { 26214c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 26229501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 26239501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 26244c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 26259501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 26264c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2627d62ab2f4SMitsuru IWASAKI 2628d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2629297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 26304c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 26314c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2632d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 26334c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 26344c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 26354c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 26364c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 26374c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 26384c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 26394c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 26404c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 26414c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 26424c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2643d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2644d62ab2f4SMitsuru IWASAKI 2645d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2646d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2647d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2648d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2649d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 26504c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2651d62ab2f4SMitsuru IWASAKI 2652d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2653d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2654d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2655d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2656d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2657d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2658d62ab2f4SMitsuru IWASAKI 2659d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 266098479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 266198479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 266298479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 266398479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 266498479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 266515e32d5dSMike Smith {NULL, 0} 266615e32d5dSMike Smith }; 266715e32d5dSMike Smith 266815e32d5dSMike Smith static void 266915e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 267015e32d5dSMike Smith { 267115e32d5dSMike Smith char *ep; 267215e32d5dSMike Smith int i, l; 26730ae55423SMike Smith int set; 267415e32d5dSMike Smith 267515e32d5dSMike Smith while (*cp) { 267615e32d5dSMike Smith if (isspace(*cp)) { 267715e32d5dSMike Smith cp++; 267815e32d5dSMike Smith continue; 267915e32d5dSMike Smith } 268015e32d5dSMike Smith ep = cp; 268115e32d5dSMike Smith while (*ep && !isspace(*ep)) 268215e32d5dSMike Smith ep++; 26830ae55423SMike Smith if (*cp == '!') { 26840ae55423SMike Smith set = 0; 26850ae55423SMike Smith cp++; 26860ae55423SMike Smith if (cp == ep) 26870ae55423SMike Smith continue; 26880ae55423SMike Smith } else { 26890ae55423SMike Smith set = 1; 26900ae55423SMike Smith } 269115e32d5dSMike Smith l = ep - cp; 269215e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 269315e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2694be2b1797SNate Lawson if (set) 269515e32d5dSMike Smith *flag |= tag[i].value; 2696be2b1797SNate Lawson else 26970ae55423SMike Smith *flag &= ~tag[i].value; 269815e32d5dSMike Smith } 269915e32d5dSMike Smith } 270015e32d5dSMike Smith cp = ep; 270115e32d5dSMike Smith } 270215e32d5dSMike Smith } 270315e32d5dSMike Smith 270415e32d5dSMike Smith static void 27052a4ac806SMike Smith acpi_set_debugging(void *junk) 270615e32d5dSMike Smith { 27077e639165SNate Lawson char *layer, *level; 270815e32d5dSMike Smith 27091d7b121cSNate Lawson if (cold) { 27100ae55423SMike Smith AcpiDbgLayer = 0; 27110ae55423SMike Smith AcpiDbgLevel = 0; 27121d7b121cSNate Lawson } 27131d7b121cSNate Lawson 27147e639165SNate Lawson layer = getenv("debug.acpi.layer"); 27157e639165SNate Lawson level = getenv("debug.acpi.level"); 27167e639165SNate Lawson if (layer == NULL && level == NULL) 27177e639165SNate Lawson return; 27180ae55423SMike Smith 27197e639165SNate Lawson printf("ACPI set debug"); 27207e639165SNate Lawson if (layer != NULL) { 27217e639165SNate Lawson if (strcmp("NONE", layer) != 0) 27227e639165SNate Lawson printf(" layer '%s'", layer); 27237e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 27247e639165SNate Lawson freeenv(layer); 27251d7b121cSNate Lawson } 27267e639165SNate Lawson if (level != NULL) { 27277e639165SNate Lawson if (strcmp("NONE", level) != 0) 27287e639165SNate Lawson printf(" level '%s'", level); 27297e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 27307e639165SNate Lawson freeenv(level); 27317e639165SNate Lawson } 27327e639165SNate Lawson printf("\n"); 273315e32d5dSMike Smith } 2734be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2735be2b1797SNate Lawson NULL); 27361d7b121cSNate Lawson 27371d7b121cSNate Lawson static int 27381d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 27391d7b121cSNate Lawson { 274054f6bca0SNate Lawson int error, *dbg; 27411d7b121cSNate Lawson struct debugtag *tag; 274254f6bca0SNate Lawson struct sbuf sb; 27431d7b121cSNate Lawson 274454f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 274554f6bca0SNate Lawson return (ENOMEM); 27461d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 27471d7b121cSNate Lawson tag = &dbg_layer[0]; 27481d7b121cSNate Lawson dbg = &AcpiDbgLayer; 27491d7b121cSNate Lawson } else { 27501d7b121cSNate Lawson tag = &dbg_level[0]; 27511d7b121cSNate Lawson dbg = &AcpiDbgLevel; 27521d7b121cSNate Lawson } 27531d7b121cSNate Lawson 27541d7b121cSNate Lawson /* Get old values if this is a get request. */ 27551d7b121cSNate Lawson if (*dbg == 0) { 275654f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 27571d7b121cSNate Lawson } else if (req->newptr == NULL) { 27581d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 275954f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 276054f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 27611d7b121cSNate Lawson } 27621d7b121cSNate Lawson } 276354f6bca0SNate Lawson sbuf_trim(&sb); 276454f6bca0SNate Lawson sbuf_finish(&sb); 27651d7b121cSNate Lawson 27667e639165SNate Lawson /* Copy out the old values to the user. */ 27677e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 276854f6bca0SNate Lawson sbuf_delete(&sb); 27691d7b121cSNate Lawson 27701d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 27711d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 27721d7b121cSNate Lawson *dbg = 0; 27731d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 27741d7b121cSNate Lawson acpi_set_debugging(NULL); 27751d7b121cSNate Lawson } 27761d7b121cSNate Lawson 27771d7b121cSNate Lawson return (error); 27781d7b121cSNate Lawson } 27791d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 27801d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 27811d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 27821d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 278315e32d5dSMike Smith #endif 2784f9390180SMitsuru IWASAKI 2785f9390180SMitsuru IWASAKI static int 2786f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2787f9390180SMitsuru IWASAKI { 2788f9390180SMitsuru IWASAKI int state, acpi_state; 2789f9390180SMitsuru IWASAKI int error; 2790f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2791f9390180SMitsuru IWASAKI va_list ap; 2792f9390180SMitsuru IWASAKI 2793f9390180SMitsuru IWASAKI error = 0; 2794f9390180SMitsuru IWASAKI switch (cmd) { 2795f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2796f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2797f9390180SMitsuru IWASAKI if (sc == NULL) { 2798f9390180SMitsuru IWASAKI error = EINVAL; 2799f9390180SMitsuru IWASAKI goto out; 2800f9390180SMitsuru IWASAKI } 2801f9390180SMitsuru IWASAKI 2802f9390180SMitsuru IWASAKI va_start(ap, arg); 2803f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2804f9390180SMitsuru IWASAKI va_end(ap); 2805f9390180SMitsuru IWASAKI 2806f9390180SMitsuru IWASAKI switch (state) { 2807f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2808f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2809f9390180SMitsuru IWASAKI break; 2810f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2811f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2812f9390180SMitsuru IWASAKI break; 2813f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2814f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2815f9390180SMitsuru IWASAKI break; 2816f9390180SMitsuru IWASAKI default: 2817f9390180SMitsuru IWASAKI error = EINVAL; 2818f9390180SMitsuru IWASAKI goto out; 2819f9390180SMitsuru IWASAKI } 2820f9390180SMitsuru IWASAKI 2821f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2822f9390180SMitsuru IWASAKI break; 2823f9390180SMitsuru IWASAKI default: 2824f9390180SMitsuru IWASAKI error = EINVAL; 2825f9390180SMitsuru IWASAKI goto out; 2826f9390180SMitsuru IWASAKI } 2827f9390180SMitsuru IWASAKI 2828f9390180SMitsuru IWASAKI out: 2829f9390180SMitsuru IWASAKI return (error); 2830f9390180SMitsuru IWASAKI } 2831f9390180SMitsuru IWASAKI 2832f9390180SMitsuru IWASAKI static void 2833f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2834f9390180SMitsuru IWASAKI { 2835be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 28366c407052SMitsuru IWASAKI return; 2837f9390180SMitsuru IWASAKI 2838f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2839f9390180SMitsuru IWASAKI } 2840f9390180SMitsuru IWASAKI 2841f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2842