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); 126e8b4d56eSNate Lawson static int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw); 127e8b4d56eSNate Lawson static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, 128e8b4d56eSNate Lawson void **status); 129e8b4d56eSNate Lawson static int acpi_wake_limit_walk(int sstate); 13088a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 13188a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 13215e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 13315e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 134d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1351d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 136f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 137879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 138879d6c23STakanori Watanabe char *buf, size_t buflen); 139879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 140879d6c23STakanori Watanabe char *buf, size_t buflen); 141879d6c23STakanori Watanabe 14215e32d5dSMike Smith static device_method_t acpi_methods[] = { 14315e32d5dSMike Smith /* Device interface */ 14415e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 14515e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 14615e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 147169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 14856a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 14915e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 15015e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 15115e32d5dSMike Smith 15215e32d5dSMike Smith /* Bus interface */ 15315e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 15415e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 15515e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 15615e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 15791233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 15891233413SNate Lawson DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 15991233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 16015e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 16115e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 162879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 163879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 16415e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 16515e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 16615e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 16715e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 16815e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 16915e32d5dSMike Smith 170ce619b2eSNate Lawson /* ACPI bus */ 171ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 172ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 173ce619b2eSNate Lawson DEVMETHOD(acpi_walk_namespace, acpi_device_walk_ns), 174ce619b2eSNate Lawson 175bc0f2195SMike Smith /* ISA emulation */ 176bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 177bc0f2195SMike Smith 17815e32d5dSMike Smith {0, 0} 17915e32d5dSMike Smith }; 18015e32d5dSMike Smith 18115e32d5dSMike Smith static driver_t acpi_driver = { 18215e32d5dSMike Smith "acpi", 18315e32d5dSMike Smith acpi_methods, 18415e32d5dSMike Smith sizeof(struct acpi_softc), 18515e32d5dSMike Smith }; 18615e32d5dSMike Smith 1873273b005SMike Smith static devclass_t acpi_devclass; 1886d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 189a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 19015e32d5dSMike Smith 191865b8d0bSNate Lawson static const char* sleep_state_names[] = { 192865b8d0bSNate Lawson "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 193865b8d0bSNate Lawson 1941d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 1951d7b121cSNate Lawson static char acpi_ca_version[12]; 1961d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 1971d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 19815e32d5dSMike Smith 19915e32d5dSMike Smith /* 200413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 201c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 202c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 203c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 204c9b8d77dSNate Lawson * some IBMs use such code. 205413081d7SNate Lawson */ 206c9b8d77dSNate Lawson static int acpi_serialize_methods; 207413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 208413081d7SNate Lawson 209c9b8d77dSNate Lawson /* 2106d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2116d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 212be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 213be2b1797SNate Lawson * off it. 2146d63101aSMike Smith */ 2156d63101aSMike Smith static int 2166d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2176d63101aSMike Smith { 2186d63101aSMike Smith switch (event) { 2196d63101aSMike Smith case MOD_LOAD: 22087b45ed5SMitsuru IWASAKI if (!cold) { 22187b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2226d63101aSMike Smith return (EPERM); 22387b45ed5SMitsuru IWASAKI } 2246d63101aSMike Smith break; 2256d63101aSMike Smith case MOD_UNLOAD: 226f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2276d63101aSMike Smith return (EBUSY); 228f9390180SMitsuru IWASAKI break; 2296d63101aSMike Smith default: 2306d63101aSMike Smith break; 2316d63101aSMike Smith } 2326d63101aSMike Smith return (0); 2336d63101aSMike Smith } 2346d63101aSMike Smith 2356d63101aSMike Smith /* 236bbc2815cSJohn Baldwin * Perform early initialization. 23715e32d5dSMike Smith */ 238bbc2815cSJohn Baldwin ACPI_STATUS 239bbc2815cSJohn Baldwin acpi_Startup(void) 24015e32d5dSMike Smith { 241d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 242d786139cSMaxime Henrion char *debugpoint; 24315e32d5dSMike Smith #endif 24489fb5af8SNate Lawson static int started = 0; 24589fb5af8SNate Lawson int error, val; 24615e32d5dSMike Smith 2471b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2481b8c233dSPeter Pentchev 249bbc2815cSJohn Baldwin if (started) 25089fb5af8SNate Lawson return_VALUE (0); 251bbc2815cSJohn Baldwin started = 1; 25215e32d5dSMike Smith 253be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2546008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 255cb9b0d80SMike Smith 256413081d7SNate Lawson /* 257413081d7SNate Lawson * Set the globals from our tunables. This is needed because ACPI-CA 258f3fc4f8bSNate Lawson * uses UINT8 for some values and we have no tunable_byte. 259413081d7SNate Lawson */ 260f3fc4f8bSNate Lawson AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods; 261413081d7SNate Lawson 262be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 263d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 264d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 265d786139cSMaxime Henrion if (debugpoint) { 266d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 26715e32d5dSMike Smith acpi_EnterDebugger(); 268d786139cSMaxime Henrion freeenv(debugpoint); 269d786139cSMaxime Henrion } 27015e32d5dSMike Smith #endif 271b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 272bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 273bbc2815cSJohn Baldwin return_VALUE (error); 27415e32d5dSMike Smith } 275d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 276d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 277d786139cSMaxime Henrion if (debugpoint) { 278d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 27915e32d5dSMike Smith acpi_EnterDebugger(); 280d786139cSMaxime Henrion freeenv(debugpoint); 281d786139cSMaxime Henrion } 28215e32d5dSMike Smith #endif 2831611ea87SMitsuru IWASAKI 284b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 285bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 28689fb5af8SNate Lawson AcpiTerminate(); 287bbc2815cSJohn Baldwin return_VALUE (error); 28815e32d5dSMike Smith } 2893184cf5aSNate Lawson 29089fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 29189fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 29289fb5af8SNate Lawson 29389fb5af8SNate Lawson /* If the user manually set the disabled hint to 0, override any quirk. */ 29489fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 29589fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 29689fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 29789fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 29889fb5af8SNate Lawson AcpiTerminate(); 2994e376d58SNate Lawson return_VALUE (AE_ERROR); 30089fb5af8SNate Lawson } 3013184cf5aSNate Lawson 302bbc2815cSJohn Baldwin return_VALUE (AE_OK); 303bbc2815cSJohn Baldwin } 304bbc2815cSJohn Baldwin 305bbc2815cSJohn Baldwin /* 306bbc2815cSJohn Baldwin * Detect ACPI, perform early initialisation 307bbc2815cSJohn Baldwin */ 308bbc2815cSJohn Baldwin static void 309bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent) 310bbc2815cSJohn Baldwin { 311bbc2815cSJohn Baldwin device_t child; 312bbc2815cSJohn Baldwin 313bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 314bbc2815cSJohn Baldwin 315bbc2815cSJohn Baldwin if (!cold) 316bbc2815cSJohn Baldwin return_VOID; 317bbc2815cSJohn Baldwin 318bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 319bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 320bbc2815cSJohn Baldwin return_VOID; 321bbc2815cSJohn Baldwin 322bbc2815cSJohn Baldwin /* Make sure we're not being doubly invoked. */ 323bbc2815cSJohn Baldwin if (device_find_child(parent, "acpi", 0) != NULL) 324bbc2815cSJohn Baldwin return_VOID; 325bbc2815cSJohn Baldwin 326bbc2815cSJohn Baldwin /* Initialize ACPI-CA. */ 327bbc2815cSJohn Baldwin if (ACPI_FAILURE(acpi_Startup())) 328bbc2815cSJohn Baldwin return_VOID; 32915e32d5dSMike Smith 3304e376d58SNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION); 3314e376d58SNate Lawson 332be2b1797SNate Lawson /* Attach the actual ACPI device. */ 33315e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 33415e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 3350ae55423SMike Smith return_VOID; 33615e32d5dSMike Smith } 33715e32d5dSMike Smith } 33815e32d5dSMike Smith 33915e32d5dSMike Smith /* 34015e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 34115e32d5dSMike Smith */ 34215e32d5dSMike Smith static int 34315e32d5dSMike Smith acpi_probe(device_t dev) 34415e32d5dSMike Smith { 34515e32d5dSMike Smith ACPI_TABLE_HEADER th; 34615e32d5dSMike Smith char buf[20]; 34715e32d5dSMike Smith int error; 3482ccd1cacSNate Lawson struct sbuf sb; 3492ccd1cacSNate Lawson ACPI_STATUS status; 350fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 35115e32d5dSMike Smith 352b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 353f9390180SMitsuru IWASAKI 354f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 355f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 356be2b1797SNate Lawson 357f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 358f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 359f9390180SMitsuru IWASAKI } 360f9390180SMitsuru IWASAKI 361cb9b0d80SMike Smith ACPI_LOCK; 3620ae55423SMike Smith 363b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 364be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 365be2b1797SNate Lawson AcpiFormatException(status)); 366cb9b0d80SMike Smith error = ENXIO; 367cb9b0d80SMike Smith } else { 3682ccd1cacSNate Lawson sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 3692ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemId, 6); 3702ccd1cacSNate Lawson sbuf_trim(&sb); 3712ccd1cacSNate Lawson sbuf_putc(&sb, ' '); 3722ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemTableId, 8); 3732ccd1cacSNate Lawson sbuf_trim(&sb); 3742ccd1cacSNate Lawson sbuf_finish(&sb); 3752ccd1cacSNate Lawson device_set_desc_copy(dev, sbuf_data(&sb)); 3762ccd1cacSNate Lawson sbuf_delete(&sb); 377cb9b0d80SMike Smith error = 0; 378cb9b0d80SMike Smith } 379cb9b0d80SMike Smith ACPI_UNLOCK; 380cb9b0d80SMike Smith return_VALUE(error); 38115e32d5dSMike Smith } 38215e32d5dSMike Smith 38315e32d5dSMike Smith static int 38415e32d5dSMike Smith acpi_attach(device_t dev) 38515e32d5dSMike Smith { 38615e32d5dSMike Smith struct acpi_softc *sc; 387cb9b0d80SMike Smith ACPI_STATUS status; 388ea27c63eSNate Lawson int error, state; 38932d18aa5SMike Smith UINT32 flags; 390ea27c63eSNate Lawson UINT8 TypeA, TypeB; 391498d464fSMitsuru IWASAKI char *env; 392d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 393d786139cSMaxime Henrion char *debugpoint; 39415e32d5dSMike Smith #endif 395fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 39615e32d5dSMike Smith 397b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 398cb9b0d80SMike Smith ACPI_LOCK; 39915e32d5dSMike Smith sc = device_get_softc(dev); 40015e32d5dSMike Smith bzero(sc, sizeof(*sc)); 40115e32d5dSMike Smith sc->acpi_dev = dev; 40215e32d5dSMike Smith 40391233413SNate Lawson /* Initialize resource manager. */ 40491233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 40591233413SNate Lawson acpi_rman_io.rm_start = 0; 40691233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 40791233413SNate Lawson acpi_rman_io.rm_descr = "I/O ports"; 40891233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 40991233413SNate Lawson panic("acpi rman_init IO ports failed"); 41091233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 41191233413SNate Lawson acpi_rman_mem.rm_start = 0; 41291233413SNate Lawson acpi_rman_mem.rm_end = ~0ul; 41391233413SNate Lawson acpi_rman_mem.rm_descr = "I/O memory addresses"; 41491233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 41591233413SNate Lawson panic("acpi rman_init memory failed"); 41691233413SNate Lawson 417d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 418d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 419d786139cSMaxime Henrion if (debugpoint) { 420d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 42115e32d5dSMike Smith acpi_EnterDebugger(); 422d786139cSMaxime Henrion freeenv(debugpoint); 423d786139cSMaxime Henrion } 42415e32d5dSMike Smith #endif 42515e32d5dSMike Smith 426be2b1797SNate Lawson /* Install the default address space handlers. */ 427cb9b0d80SMike Smith error = ENXIO; 428be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 429be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 430be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 431be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 432be2b1797SNate Lawson AcpiFormatException(status)); 433cb9b0d80SMike Smith goto out; 43415e32d5dSMike Smith } 435be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 436be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 437be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 438be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 439be2b1797SNate Lawson AcpiFormatException(status)); 440cb9b0d80SMike Smith goto out; 44115e32d5dSMike Smith } 442be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 443be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 444be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 445be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 446be2b1797SNate Lawson AcpiFormatException(status)); 447cb9b0d80SMike Smith goto out; 44815e32d5dSMike Smith } 44915e32d5dSMike Smith 45015e32d5dSMike Smith /* 45115e32d5dSMike Smith * Bring ACPI fully online. 45215e32d5dSMike Smith * 453be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 454be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 455be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 456be2b1797SNate Lawson * object init pass. 45715e32d5dSMike Smith * 45832d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 45932d18aa5SMike Smith * 460be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 461be2b1797SNate Lawson * all our child devices, but on many systems it works here. 46215e32d5dSMike Smith */ 463d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 464d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 465d786139cSMaxime Henrion if (debugpoint) { 466d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 46715e32d5dSMike Smith acpi_EnterDebugger(); 468d786139cSMaxime Henrion freeenv(debugpoint); 469d786139cSMaxime Henrion } 47015e32d5dSMike Smith #endif 47132d18aa5SMike Smith flags = 0; 472d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 47332d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 474b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 475be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 476be2b1797SNate Lawson AcpiFormatException(status)); 477cb9b0d80SMike Smith goto out; 47815e32d5dSMike Smith } 47915e32d5dSMike Smith 480f8335e3aSNate Lawson /* 481f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 482f8335e3aSNate Lawson * the namespace has been evaluated. 483f8335e3aSNate Lawson */ 484f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 485f8335e3aSNate Lawson 486b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 487be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 488be2b1797SNate Lawson AcpiFormatException(status)); 489b69ed3f4SMitsuru IWASAKI goto out; 490b69ed3f4SMitsuru IWASAKI } 491b69ed3f4SMitsuru IWASAKI 49215e32d5dSMike Smith /* 4931d073b1dSJohn Baldwin * Setup our sysctl tree. 4941d073b1dSJohn Baldwin * 4951d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4961d073b1dSJohn Baldwin */ 4971d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4981d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4991d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5001d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5011d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 502d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 503d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 504d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5051d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5061d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5071d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5081d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5091d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5101d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5111d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5121d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 513f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 514f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 515f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 516f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 517f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 518f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5192d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 520ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 521ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 522ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5231611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 5241611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 5251611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5262d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 5272d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 528bf0c18ecSNate Lawson 529bf0c18ecSNate Lawson /* 5302b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 531bf0c18ecSNate Lawson * stabilize. 532bf0c18ecSNate Lawson */ 5332b9609abSNate Lawson sc->acpi_sleep_delay = 1; 5342d644607SMitsuru IWASAKI if (bootverbose) 5352d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 536498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 537498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 538498d464fSMitsuru IWASAKI freeenv(env); 539498d464fSMitsuru IWASAKI } 5401d073b1dSJohn Baldwin 5412d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5422d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 5432d610c46SNate Lawson sc->acpi_s4bios = 1; 5442d610c46SNate Lawson 5451d073b1dSJohn Baldwin /* 546ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 547ea27c63eSNate Lawson * to NONE by default to avoid surprising users. 54815e32d5dSMike Smith */ 549ea27c63eSNate Lawson sc->acpi_power_button_sx = ACPI_STATE_S5; 550ea27c63eSNate Lawson sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 551f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 552f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 55315e32d5dSMike Smith 554ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 555ea27c63eSNate Lawson sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 556ea27c63eSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++) 557ea27c63eSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 558ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 559ea27c63eSNate Lawson break; 560ea27c63eSNate Lawson } 561ea27c63eSNate Lawson 56213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 56315e32d5dSMike Smith 56415e32d5dSMike Smith /* 56515e32d5dSMike Smith * Scan the namespace and attach/initialise children. 56615e32d5dSMike Smith */ 567d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 568d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 569d786139cSMaxime Henrion if (debugpoint) { 570d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 57115e32d5dSMike Smith acpi_EnterDebugger(); 572d786139cSMaxime Henrion freeenv(debugpoint); 573d786139cSMaxime Henrion } 57415e32d5dSMike Smith #endif 57515e32d5dSMike Smith 57659e472e9SNate Lawson /* Register our shutdown handler. */ 577be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 578be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 57915e32d5dSMike Smith 58015e32d5dSMike Smith /* 58115e32d5dSMike Smith * Register our acpi event handlers. 58215e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 58315e32d5dSMike Smith */ 584be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 585be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 586be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 587be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 58815e32d5dSMike Smith 589be2b1797SNate Lawson /* Flag our initial states. */ 59015e32d5dSMike Smith sc->acpi_enabled = 1; 59115e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 592ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 59315e32d5dSMike Smith 594be2b1797SNate Lawson /* Create the control device */ 595a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 5962a4689e8SRobert Watson "acpi"); 59715e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 59815e32d5dSMike Smith 599d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 600d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 601d786139cSMaxime Henrion if (debugpoint) { 602be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 60315e32d5dSMike Smith acpi_EnterDebugger(); 604d786139cSMaxime Henrion freeenv(debugpoint); 605d786139cSMaxime Henrion } 60615e32d5dSMike Smith #endif 607f86214b6SMitsuru IWASAKI 60891da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 609be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 610c573e654SMitsuru IWASAKI goto out; 611c573e654SMitsuru IWASAKI #endif 612c573e654SMitsuru IWASAKI 613be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 614f86214b6SMitsuru IWASAKI goto out; 615f86214b6SMitsuru IWASAKI 616f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 617f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 618f9390180SMitsuru IWASAKI 619eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 620eeb6dba2SJohn Baldwin acpi_probe_children(dev); 621eeb6dba2SJohn Baldwin 622cb9b0d80SMike Smith error = 0; 623cb9b0d80SMike Smith 624cb9b0d80SMike Smith out: 625cb9b0d80SMike Smith ACPI_UNLOCK; 626cb9b0d80SMike Smith return_VALUE (error); 62715e32d5dSMike Smith } 62815e32d5dSMike Smith 629169b539aSNate Lawson static int 630169b539aSNate Lawson acpi_shutdown(device_t dev) 631169b539aSNate Lawson { 632169b539aSNate Lawson 6330e414868SNate Lawson /* Allow children to shutdown first. */ 6340e414868SNate Lawson bus_generic_shutdown(dev); 6350e414868SNate Lawson 636169b539aSNate Lawson /* Disable all wake GPEs not appropriate for reboot/poweroff. */ 637169b539aSNate Lawson acpi_wake_limit_walk(ACPI_STATE_S5); 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; 74532d18aa5SMike Smith case ISA_IVAR_VENDORID: 74632d18aa5SMike Smith case ISA_IVAR_SERIAL: 74732d18aa5SMike Smith case ISA_IVAR_COMPATID: 74832d18aa5SMike Smith *(int *)result = -1; 74932d18aa5SMike Smith break; 75032d18aa5SMike Smith case ISA_IVAR_LOGICALID: 75132d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 75232d18aa5SMike Smith break; 75315e32d5dSMike Smith default: 75415e32d5dSMike Smith return (ENOENT); 75515e32d5dSMike Smith } 756be2b1797SNate Lawson 75715e32d5dSMike Smith return (0); 75815e32d5dSMike Smith } 75915e32d5dSMike Smith 76015e32d5dSMike Smith static int 76115e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 76215e32d5dSMike Smith { 76315e32d5dSMike Smith struct acpi_device *ad; 76415e32d5dSMike Smith 76515e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 76615e32d5dSMike Smith printf("device has no ivars\n"); 76715e32d5dSMike Smith return (ENOENT); 76815e32d5dSMike Smith } 76915e32d5dSMike Smith 77015e32d5dSMike Smith switch(index) { 77115e32d5dSMike Smith case ACPI_IVAR_HANDLE: 77215e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 77315e32d5dSMike Smith break; 77415e32d5dSMike Smith case ACPI_IVAR_MAGIC: 77515e32d5dSMike Smith ad->ad_magic = (int)value; 77615e32d5dSMike Smith break; 77715e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 77815e32d5dSMike Smith ad->ad_private = (void *)value; 77915e32d5dSMike Smith break; 78015e32d5dSMike Smith default: 7812ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 78215e32d5dSMike Smith return (ENOENT); 78315e32d5dSMike Smith } 784be2b1797SNate Lawson 78515e32d5dSMike Smith return (0); 78615e32d5dSMike Smith } 78715e32d5dSMike Smith 78815e32d5dSMike Smith /* 78915e32d5dSMike Smith * Handle child resource allocation/removal 79015e32d5dSMike Smith */ 79191233413SNate Lawson static struct resource_list * 79291233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 79315e32d5dSMike Smith { 79491233413SNate Lawson struct acpi_device *ad; 79515e32d5dSMike Smith 79691233413SNate Lawson ad = device_get_ivars(child); 79791233413SNate Lawson return (&ad->ad_rl); 79815e32d5dSMike Smith } 79915e32d5dSMike Smith 80015e32d5dSMike Smith static struct resource * 80115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 80215e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 80315e32d5dSMike Smith { 80495957f62SJohn Baldwin ACPI_RESOURCE ares; 80515e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 80615e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 80791233413SNate Lawson struct resource_list_entry *rle; 80891233413SNate Lawson struct resource *res; 80991233413SNate Lawson struct rman *rm; 81015e32d5dSMike Smith 81191233413SNate Lawson /* 81291233413SNate Lawson * If this is an allocation of the "default" range for a given RID, and 81391233413SNate Lawson * we know what the resources for this device are (i.e., they're on the 81491233413SNate Lawson * child's resource list), use those start/end values. 81591233413SNate Lawson */ 81691233413SNate Lawson if (start == 0UL && end == ~0UL) { 81791233413SNate Lawson rle = resource_list_find(rl, type, *rid); 81891233413SNate Lawson if (rle == NULL) 81991233413SNate Lawson return (NULL); 82091233413SNate Lawson start = rle->start; 82191233413SNate Lawson end = rle->end; 82291233413SNate Lawson count = rle->count; 82391233413SNate Lawson } 82491233413SNate Lawson 82591233413SNate Lawson /* If we don't manage this address, pass the request up to the parent. */ 82691233413SNate Lawson rle = acpi_sysres_find(type, start); 82791233413SNate Lawson if (rle == NULL) { 82895957f62SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 82995957f62SJohn Baldwin start, end, count, flags); 83095957f62SJohn Baldwin } else { 83191233413SNate Lawson 83291233413SNate Lawson /* We only handle memory and IO resources through rman. */ 83391233413SNate Lawson switch (type) { 83491233413SNate Lawson case SYS_RES_IOPORT: 83591233413SNate Lawson rm = &acpi_rman_io; 83691233413SNate Lawson break; 83791233413SNate Lawson case SYS_RES_MEMORY: 83891233413SNate Lawson rm = &acpi_rman_mem; 83991233413SNate Lawson break; 84091233413SNate Lawson default: 84191233413SNate Lawson panic("acpi_alloc_resource: invalid res type %d", type); 84291233413SNate Lawson } 84391233413SNate Lawson 84491233413SNate Lawson /* If we do know it, allocate it from the local pool. */ 84595957f62SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 84695957f62SJohn Baldwin child); 84791233413SNate Lawson if (res == NULL) 84891233413SNate Lawson return (NULL); 84991233413SNate Lawson 850a3236570SWarner Losh /* Copy the bus tag and handle from the pre-allocated resource. */ 85191233413SNate Lawson rman_set_bustag(res, rman_get_bustag(rle->res)); 852ed67d9deSWarner Losh rman_set_bushandle(res, rman_get_start(res)); 85391233413SNate Lawson 85491233413SNate Lawson /* If requested, activate the resource using the parent's method. */ 85595957f62SJohn Baldwin if (flags & RF_ACTIVE) 85691233413SNate Lawson if (bus_activate_resource(child, type, *rid, res) != 0) { 85791233413SNate Lawson rman_release_resource(res); 85891233413SNate Lawson return (NULL); 85991233413SNate Lawson } 86095957f62SJohn Baldwin } 86191233413SNate Lawson 86295957f62SJohn Baldwin if (res != NULL && device_get_parent(child) == bus) 86395957f62SJohn Baldwin switch (type) { 86495957f62SJohn Baldwin case SYS_RES_IRQ: 86595957f62SJohn Baldwin /* 86695957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 86795957f62SJohn Baldwin * configure the interrupt associated with a device when we 86895957f62SJohn Baldwin * parse the resources but have to defer it until a driver 86995957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 87095957f62SJohn Baldwin * 87195957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 87295957f62SJohn Baldwin */ 87395957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 87495957f62SJohn Baldwin acpi_config_intr(child, &ares); 87595957f62SJohn Baldwin break; 87695957f62SJohn Baldwin } 87791233413SNate Lawson return (res); 87815e32d5dSMike Smith } 87915e32d5dSMike Smith 88015e32d5dSMike Smith static int 88191233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 88291233413SNate Lawson struct resource *r) 88315e32d5dSMike Smith { 88491233413SNate Lawson int ret; 88515e32d5dSMike Smith 88691233413SNate Lawson /* 88791233413SNate Lawson * If we know about this address, deactivate it and release it to the 88891233413SNate Lawson * local pool. If we don't, pass this request up to the parent. 88991233413SNate Lawson */ 89091233413SNate Lawson if (acpi_sysres_find(type, rman_get_start(r)) == NULL) { 89191233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 89291233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 89391233413SNate Lawson if (ret != 0) 89491233413SNate Lawson return (ret); 89591233413SNate Lawson } 89691233413SNate Lawson ret = rman_release_resource(r); 89791233413SNate Lawson } else 89891233413SNate Lawson ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 89991233413SNate Lawson 90091233413SNate Lawson return (ret); 90115e32d5dSMike Smith } 90215e32d5dSMike Smith 903dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 904dc750869SNate Lawson struct resource * 905dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 906dc750869SNate Lawson { 907dc750869SNate Lawson int type; 908dc750869SNate Lawson 909dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 910dc750869SNate Lawson gas->RegisterBitWidth < 8) 911dc750869SNate Lawson return (NULL); 912dc750869SNate Lawson 913dc750869SNate Lawson switch (gas->AddressSpaceId) { 914dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 915dc750869SNate Lawson type = SYS_RES_MEMORY; 916dc750869SNate Lawson break; 917dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 918dc750869SNate Lawson type = SYS_RES_IOPORT; 919dc750869SNate Lawson break; 920dc750869SNate Lawson default: 921dc750869SNate Lawson return (NULL); 922dc750869SNate Lawson } 923dc750869SNate Lawson 924dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 9255f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 926dc750869SNate Lawson } 927dc750869SNate Lawson 928c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 9291e4925e8SNate Lawson static uint32_t 93032d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 931bc0f2195SMike Smith { 9321e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9331e4925e8SNate Lawson ACPI_BUFFER buf; 934bc0f2195SMike Smith ACPI_HANDLE h; 935bc0f2195SMike Smith ACPI_STATUS error; 936bc0f2195SMike Smith u_int32_t pnpid; 937fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 93832d18aa5SMike Smith 939b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 94032d18aa5SMike Smith 94132d18aa5SMike Smith pnpid = 0; 942bd189fe7SNate Lawson buf.Pointer = NULL; 943bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 944bd189fe7SNate Lawson 94532d18aa5SMike Smith ACPI_LOCK; 94632d18aa5SMike Smith 9476fca9360SNate Lawson /* Fetch and validate the HID. */ 94832d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 949bd189fe7SNate Lawson goto out; 9506fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9516fca9360SNate Lawson if (ACPI_FAILURE(error)) 952bd189fe7SNate Lawson goto out; 9531e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 95432d18aa5SMike Smith 9551e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 9561e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 957be2b1797SNate Lawson 958bd189fe7SNate Lawson out: 959bd189fe7SNate Lawson if (buf.Pointer != NULL) 9601e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 96132d18aa5SMike Smith ACPI_UNLOCK; 96232d18aa5SMike Smith return_VALUE (pnpid); 96332d18aa5SMike Smith } 96432d18aa5SMike Smith 9651e4925e8SNate Lawson static int 9661e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 967b2566207STakanori Watanabe { 9681e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9691e4925e8SNate Lawson ACPI_BUFFER buf; 970b2566207STakanori Watanabe ACPI_HANDLE h; 971b2566207STakanori Watanabe ACPI_STATUS error; 9721e4925e8SNate Lawson uint32_t *pnpid; 9731e4925e8SNate Lawson int valid, i; 974b2566207STakanori Watanabe ACPI_LOCK_DECL; 975b2566207STakanori Watanabe 976b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 977b2566207STakanori Watanabe 9781e4925e8SNate Lawson pnpid = cids; 9791e4925e8SNate Lawson valid = 0; 980bd189fe7SNate Lawson buf.Pointer = NULL; 981bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 982bd189fe7SNate Lawson 983b2566207STakanori Watanabe ACPI_LOCK; 984b2566207STakanori Watanabe 9851e4925e8SNate Lawson /* Fetch and validate the CID */ 986b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 987bd189fe7SNate Lawson goto out; 9881e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9891e4925e8SNate Lawson if (ACPI_FAILURE(error)) 990bd189fe7SNate Lawson goto out; 9911e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 9921e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 993b2566207STakanori Watanabe goto out; 994b2566207STakanori Watanabe 9951e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 9961e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 9971e4925e8SNate Lawson for (i = 0; i < count; i++) { 9981e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 9991e4925e8SNate Lawson continue; 10001e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 10011e4925e8SNate Lawson valid++; 1002b2566207STakanori Watanabe } 1003b2566207STakanori Watanabe 10041e4925e8SNate Lawson out: 1005bd189fe7SNate Lawson if (buf.Pointer != NULL) 10061e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 10071e4925e8SNate Lawson ACPI_UNLOCK; 10081e4925e8SNate Lawson return_VALUE (valid); 10091e4925e8SNate Lawson } 1010b2566207STakanori Watanabe 1011ce619b2eSNate Lawson static char * 1012ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1013ce619b2eSNate Lawson { 1014ce619b2eSNate Lawson ACPI_HANDLE h; 1015ce619b2eSNate Lawson int i; 1016ce619b2eSNate Lawson 1017ce619b2eSNate Lawson h = acpi_get_handle(dev); 1018ce619b2eSNate Lawson if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1019ce619b2eSNate Lawson return (NULL); 1020ce619b2eSNate Lawson 1021ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1022ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1023ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1024ce619b2eSNate Lawson return (ids[i]); 1025ce619b2eSNate Lawson } 1026ce619b2eSNate Lawson return (NULL); 1027ce619b2eSNate Lawson } 1028ce619b2eSNate Lawson 1029ce619b2eSNate Lawson static ACPI_STATUS 1030ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1031ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1032ce619b2eSNate Lawson { 1033ce619b2eSNate Lawson ACPI_HANDLE h; 1034ce619b2eSNate Lawson 1035ce619b2eSNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 1036ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1037ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1038ce619b2eSNate Lawson } 1039ce619b2eSNate Lawson 1040ce619b2eSNate Lawson static ACPI_STATUS 1041ce619b2eSNate Lawson acpi_device_walk_ns(device_t bus, device_t dev, ACPI_OBJECT_TYPE type, 1042ce619b2eSNate Lawson UINT32 max_depth, ACPI_WALK_CALLBACK user_fn, void *context, void **ret) 1043ce619b2eSNate Lawson { 1044ce619b2eSNate Lawson ACPI_HANDLE h; 1045ce619b2eSNate Lawson 1046ce619b2eSNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 1047ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1048ce619b2eSNate Lawson return (AcpiWalkNamespace(type, h, max_depth, user_fn, context, ret)); 1049ce619b2eSNate Lawson } 1050ce619b2eSNate Lawson 105132d18aa5SMike Smith static int 105232d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 105332d18aa5SMike Smith { 10541e4925e8SNate Lawson int result, cid_count, i; 10551e4925e8SNate Lawson uint32_t lid, cids[8]; 1056bc0f2195SMike Smith 1057b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1058bc0f2195SMike Smith 1059bc0f2195SMike Smith /* 1060bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1061bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1062bc0f2195SMike Smith * that to happen, so don't ever return it. 1063bc0f2195SMike Smith */ 1064bc0f2195SMike Smith result = ENXIO; 1065bc0f2195SMike Smith 1066be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1067b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 10681e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1069bc0f2195SMike Smith while (ids && ids->ip_id) { 10701e4925e8SNate Lawson if (lid == ids->ip_id) { 1071bc0f2195SMike Smith result = 0; 1072bc0f2195SMike Smith goto out; 1073bc0f2195SMike Smith } 10741e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 10751e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 10761e4925e8SNate Lawson result = 0; 10771e4925e8SNate Lawson goto out; 10781e4925e8SNate Lawson } 10791e4925e8SNate Lawson } 1080bc0f2195SMike Smith ids++; 1081bc0f2195SMike Smith } 1082be2b1797SNate Lawson 1083bc0f2195SMike Smith out: 1084bc0f2195SMike Smith return_VALUE (result); 1085bc0f2195SMike Smith } 1086bc0f2195SMike Smith 1087bc0f2195SMike Smith /* 108815e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 108915e32d5dSMike Smith * 1090be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1091be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 109215e32d5dSMike Smith */ 109315e32d5dSMike Smith static void 109415e32d5dSMike Smith acpi_probe_children(device_t bus) 109515e32d5dSMike Smith { 109615e32d5dSMike Smith ACPI_HANDLE parent; 1097be2b1797SNate Lawson ACPI_STATUS status; 109815e32d5dSMike Smith int i; 109991233413SNate Lawson static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 110015e32d5dSMike Smith 1101b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1102cb9b0d80SMike Smith ACPI_ASSERTLOCK; 11030ae55423SMike Smith 1104be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 11054c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 110615e32d5dSMike Smith bus_generic_probe(bus); 110715e32d5dSMike Smith 110815e32d5dSMike Smith /* 110915e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 111015e32d5dSMike Smith * we find. 111115e32d5dSMike Smith * 111215e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1113be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1114be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1115be2b1797SNate Lawson * devices as they appear, which might be smarter.) 111615e32d5dSMike Smith */ 11174c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1118be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1119be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1120be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1121be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1122be2b1797SNate Lawson bus, NULL); 1123be2b1797SNate Lawson } 1124be2b1797SNate Lawson } 112515e32d5dSMike Smith 112615e32d5dSMike Smith /* 112715e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 112815e32d5dSMike Smith */ 11294c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 113015e32d5dSMike Smith bus_generic_attach(bus); 113115e32d5dSMike Smith 113215e32d5dSMike Smith /* 113315e32d5dSMike Smith * Some of these children may have attached others as part of their attach 113415e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 113515e32d5dSMike Smith */ 11364c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 113715e32d5dSMike Smith bus_generic_attach(bus); 11380ae55423SMike Smith 113988a79fc0SNate Lawson /* Attach wake sysctls. */ 11405c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 114188a79fc0SNate Lawson 1142bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 11430ae55423SMike Smith return_VOID; 114415e32d5dSMike Smith } 114515e32d5dSMike Smith 1146b82ca9a9SNate Lawson /* 1147b82ca9a9SNate Lawson * Determine the probe order for a given device and return non-zero if it 1148b82ca9a9SNate Lawson * should be attached immediately. 1149b82ca9a9SNate Lawson */ 115091233413SNate Lawson static int 1151b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 115291233413SNate Lawson { 115391233413SNate Lawson int ret; 115491233413SNate Lawson 1155b82ca9a9SNate Lawson /* 1156b82ca9a9SNate Lawson * 1. I/O port and memory system resource holders 1157b82ca9a9SNate Lawson * 2. Embedded controllers (to handle early accesses) 1158b82ca9a9SNate Lawson */ 115991233413SNate Lawson ret = 0; 116091233413SNate Lawson if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) { 116191233413SNate Lawson *order = 1; 116291233413SNate Lawson ret = 1; 1163b82ca9a9SNate Lawson } else if (acpi_MatchHid(handle, "PNP0C09")) { 116491233413SNate Lawson *order = 2; 116591233413SNate Lawson ret = 1; 116691233413SNate Lawson } 116791233413SNate Lawson 1168b82ca9a9SNate Lawson /* Always probe/attach immediately if we're debugging. */ 1169b82ca9a9SNate Lawson ACPI_DEBUG_EXEC(ret = 1); 1170b82ca9a9SNate Lawson 117191233413SNate Lawson return (ret); 117291233413SNate Lawson } 117391233413SNate Lawson 117415e32d5dSMike Smith /* 117515e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 117615e32d5dSMike Smith * it. 117715e32d5dSMike Smith */ 117815e32d5dSMike Smith static ACPI_STATUS 117915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 118015e32d5dSMike Smith { 118115e32d5dSMike Smith ACPI_OBJECT_TYPE type; 118291233413SNate Lawson device_t child, bus; 118391233413SNate Lawson int order, probe_now; 118415e32d5dSMike Smith 1185b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 11860ae55423SMike Smith 1187be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 11880ae55423SMike Smith if (acpi_avoid(handle)) 11890ae55423SMike Smith return_ACPI_STATUS (AE_OK); 11900ae55423SMike Smith 119191233413SNate Lawson bus = (device_t)context; 1192b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 119315e32d5dSMike Smith switch (type) { 119415e32d5dSMike Smith case ACPI_TYPE_DEVICE: 119515e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 119615e32d5dSMike Smith case ACPI_TYPE_THERMAL: 119715e32d5dSMike Smith case ACPI_TYPE_POWER: 11980ae55423SMike Smith if (acpi_disabled("children")) 11990ae55423SMike Smith break; 1200be2b1797SNate Lawson 120115e32d5dSMike Smith /* 120215e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 1203b82ca9a9SNate Lawson * so that the probe/attach passes will run breadth-first. Orders 1204b82ca9a9SNate Lawson * less than 10 are reserved for special objects (i.e., system 1205b82ca9a9SNate Lawson * resources). Larger values are used for all other devices. 120615e32d5dSMike Smith */ 1207be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1208be2b1797SNate Lawson acpi_name(handle))); 1209b82ca9a9SNate Lawson order = (level + 1) * 10; 1210b82ca9a9SNate Lawson probe_now = acpi_probe_order(handle, &order); 121191233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1212bc0f2195SMike Smith if (child == NULL) 1213bc0f2195SMike Smith break; 121459a890e6SNate Lawson 121559a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 121615e32d5dSMike Smith acpi_set_handle(child, handle); 121759a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1218bc0f2195SMike Smith 1219e8b4d56eSNate Lawson /* Check if the device can generate wake events. */ 1220e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL))) 1221e8b4d56eSNate Lawson device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE); 1222e8b4d56eSNate Lawson 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 1769e8b4d56eSNate Lawson /* Disable all wake GPEs not appropriate for this state. */ 1770e8b4d56eSNate Lawson acpi_wake_limit_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 } 1816ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 181715e32d5dSMike Smith DEVICE_RESUME(root_bus); 181815e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 181913d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 182015e32d5dSMike Smith break; 182115e32d5dSMike Smith case ACPI_STATE_S5: 182215e32d5dSMike Smith /* 182315e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 182415e32d5dSMike Smith * shutdown handlers. 182515e32d5dSMike Smith */ 182615e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 182715e32d5dSMike Smith break; 182898175614SNate Lawson case ACPI_STATE_S0: 182915e32d5dSMike Smith default: 183015e32d5dSMike Smith status = AE_BAD_PARAMETER; 183115e32d5dSMike Smith break; 183215e32d5dSMike Smith } 1833ece50487SMitsuru IWASAKI 183498175614SNate Lawson /* Disable a second sleep request for a short period */ 1835ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1836ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1837ece50487SMitsuru IWASAKI 18380ae55423SMike Smith return_ACPI_STATUS (status); 183915e32d5dSMike Smith } 184015e32d5dSMike Smith 1841e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1842e8b4d56eSNate Lawson int 1843e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1844e8b4d56eSNate Lawson { 1845e8b4d56eSNate Lawson struct acpi_prw_data prw; 1846e8b4d56eSNate Lawson 1847e8b4d56eSNate Lawson /* Check that the device can wake the system. */ 1848e8b4d56eSNate Lawson if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0) 1849e8b4d56eSNate Lawson return (ENXIO); 1850e8b4d56eSNate Lawson 1851e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1852e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1853e8b4d56eSNate Lawson return (ENXIO); 1854e8b4d56eSNate Lawson 1855e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1856e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1857e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1858e8b4d56eSNate Lawson return (ENXIO); 1859e8b4d56eSNate Lawson } 1860e8b4d56eSNate Lawson 1861e8b4d56eSNate Lawson return (0); 1862e8b4d56eSNate Lawson } 1863e8b4d56eSNate Lawson 1864e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1865e8b4d56eSNate Lawson int 1866e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1867e8b4d56eSNate Lawson { 1868e8b4d56eSNate Lawson struct acpi_prw_data prw; 1869e8b4d56eSNate Lawson ACPI_HANDLE handle; 1870e8b4d56eSNate Lawson ACPI_STATUS status; 1871e8b4d56eSNate Lawson int flags; 1872e8b4d56eSNate Lawson 1873e8b4d56eSNate Lawson /* Make sure the device supports waking the system. */ 1874e8b4d56eSNate Lawson flags = device_get_flags(dev); 1875e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1876e8b4d56eSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1877e8b4d56eSNate Lawson return (ENXIO); 1878e8b4d56eSNate Lawson 1879e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1880e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1881e8b4d56eSNate Lawson return (ENXIO); 1882e8b4d56eSNate Lawson 1883e8b4d56eSNate Lawson if (enable) { 1884e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1885e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1886e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1887e8b4d56eSNate Lawson return (ENXIO); 1888e8b4d56eSNate Lawson } 1889e8b4d56eSNate Lawson device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1890e8b4d56eSNate Lawson } else { 1891e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1892e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1893e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1894e8b4d56eSNate Lawson return (ENXIO); 1895e8b4d56eSNate Lawson } 1896e8b4d56eSNate Lawson device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1897e8b4d56eSNate Lawson } 1898e8b4d56eSNate Lawson 1899e8b4d56eSNate Lawson return (0); 1900e8b4d56eSNate Lawson } 1901e8b4d56eSNate Lawson 1902e8b4d56eSNate Lawson /* Configure a device's GPE appropriately for the new sleep state. */ 1903e8b4d56eSNate Lawson int 1904e8b4d56eSNate Lawson acpi_wake_sleep_prep(device_t dev, int sstate) 1905e8b4d56eSNate Lawson { 1906e8b4d56eSNate Lawson struct acpi_prw_data prw; 1907e8b4d56eSNate Lawson ACPI_HANDLE handle; 1908cc85c78cSNate Lawson int flags; 1909e8b4d56eSNate Lawson 1910e8b4d56eSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1911cc85c78cSNate Lawson flags = device_get_flags(dev); 1912e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1913cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1914e8b4d56eSNate Lawson return (ENXIO); 1915cc85c78cSNate Lawson 1916cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1917e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1918e8b4d56eSNate Lawson return (ENXIO); 1919e8b4d56eSNate Lawson 1920e8b4d56eSNate Lawson /* 1921cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1922cc85c78cSNate Lawson * of the _PRW object are put into the ON state. 1923e8b4d56eSNate Lawson */ 1924cc85c78cSNate Lawson 1925cc85c78cSNate Lawson /* 1926cc85c78cSNate Lawson * If the user requested that this device wake the system and the next 1927cc85c78cSNate Lawson * sleep state is valid for this GPE, enable it and the device's wake 1928cc85c78cSNate Lawson * capability. The sleep state must be less than (i.e., higher power) 1929cc85c78cSNate Lawson * or equal to the value specified by _PRW. Return early, leaving 1930cc85c78cSNate Lawson * the appropriate power resources enabled. 1931cc85c78cSNate Lawson */ 1932cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 && 1933cc85c78cSNate Lawson sstate <= prw.lowest_wake) { 1934e8b4d56eSNate Lawson if (bootverbose) 1935cc85c78cSNate Lawson device_printf(dev, "wake_prep enabled gpe %#x for state %d\n", 1936e8b4d56eSNate Lawson prw.gpe_bit, sstate); 1937cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1938cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 1); 1939e8b4d56eSNate Lawson return (0); 1940e8b4d56eSNate Lawson } 1941e8b4d56eSNate Lawson 1942e8b4d56eSNate Lawson /* 1943cc85c78cSNate Lawson * If the device wake was disabled or this sleep state is too low for 1944cc85c78cSNate Lawson * this device, disable its wake capability and GPE. 1945e8b4d56eSNate Lawson */ 1946cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1947cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1948e8b4d56eSNate Lawson if (bootverbose) 1949cc85c78cSNate Lawson device_printf(dev, "wake_prep disabled gpe %#x for state %d\n", 1950cc85c78cSNate Lawson prw.gpe_bit, sstate); 1951cc85c78cSNate Lawson 1952cc85c78cSNate Lawson /* 1953cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1954cc85c78cSNate Lawson * of the _PRW object are put into the OFF state. 1955cc85c78cSNate Lawson */ 1956cc85c78cSNate Lawson 1957cc85c78cSNate Lawson return (0); 1958e8b4d56eSNate Lawson } 1959e8b4d56eSNate Lawson 1960cc85c78cSNate Lawson /* Re-enable GPEs after wake. */ 1961cc85c78cSNate Lawson int 1962cc85c78cSNate Lawson acpi_wake_run_prep(device_t dev) 1963cc85c78cSNate Lawson { 1964cc85c78cSNate Lawson struct acpi_prw_data prw; 1965cc85c78cSNate Lawson ACPI_HANDLE handle; 1966cc85c78cSNate Lawson int flags; 1967cc85c78cSNate Lawson 1968cc85c78cSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1969cc85c78cSNate Lawson flags = device_get_flags(dev); 1970cc85c78cSNate Lawson handle = acpi_get_handle(dev); 1971cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1972cc85c78cSNate Lawson return (ENXIO); 1973cc85c78cSNate Lawson 1974cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1975cc85c78cSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1976cc85c78cSNate Lawson return (ENXIO); 1977cc85c78cSNate Lawson 1978cc85c78cSNate Lawson /* 1979cc85c78cSNate Lawson * TBD: Be sure all Power Resources referenced by elements 2 through N 1980cc85c78cSNate Lawson * of the _PRW object are in the ON state. 1981cc85c78cSNate Lawson */ 1982cc85c78cSNate Lawson 1983cc85c78cSNate Lawson /* Disable wake capability and if the user requested, enable the GPE. */ 1984cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1985cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0) 1986cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1987e8b4d56eSNate Lawson return (0); 1988e8b4d56eSNate Lawson } 1989e8b4d56eSNate Lawson 1990e8b4d56eSNate Lawson static ACPI_STATUS 1991e8b4d56eSNate Lawson acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status) 1992e8b4d56eSNate Lawson { 1993e8b4d56eSNate Lawson struct acpi_prw_data prw; 199444b8ae71SNate Lawson int *sstate; 1995e8b4d56eSNate Lawson 1996e8b4d56eSNate Lawson /* It's ok not to have _PRW if the device can't wake the system. */ 1997e8b4d56eSNate Lawson if (acpi_parse_prw(h, &prw) != 0) 1998e8b4d56eSNate Lawson return (AE_OK); 1999e8b4d56eSNate Lawson 200044b8ae71SNate Lawson sstate = (int *)context; 200144b8ae71SNate Lawson if (*sstate > prw.lowest_wake) 2002e8b4d56eSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2003e8b4d56eSNate Lawson 2004e8b4d56eSNate Lawson return (AE_OK); 2005e8b4d56eSNate Lawson } 2006e8b4d56eSNate Lawson 2007e8b4d56eSNate Lawson /* Walk all system devices, disabling them if necessary for sstate. */ 2008e8b4d56eSNate Lawson static int 2009e8b4d56eSNate Lawson acpi_wake_limit_walk(int sstate) 2010e8b4d56eSNate Lawson { 2011e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 2012e8b4d56eSNate Lawson 2013e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2014e8b4d56eSNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100, 201544b8ae71SNate Lawson acpi_wake_limit, &sstate, NULL); 2016e8b4d56eSNate Lawson return (0); 2017e8b4d56eSNate Lawson } 2018e8b4d56eSNate Lawson 201988a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 202088a79fc0SNate Lawson static int 202188a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 202288a79fc0SNate Lawson { 202388a79fc0SNate Lawson int error, i, numdevs; 202488a79fc0SNate Lawson device_t *devlist; 202588a79fc0SNate Lawson device_t child; 202688a79fc0SNate Lawson 202788a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 202888a79fc0SNate Lawson if (error != 0 || numdevs == 0) 202988a79fc0SNate Lawson return (error); 203088a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 203188a79fc0SNate Lawson child = devlist[i]; 203288a79fc0SNate Lawson if (!device_is_attached(child)) 203388a79fc0SNate Lawson continue; 203488a79fc0SNate Lawson if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) { 203588a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 203688a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 203788a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 203888a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 203988a79fc0SNate Lawson } 204088a79fc0SNate Lawson acpi_wake_sysctl_walk(child); 204188a79fc0SNate Lawson } 204288a79fc0SNate Lawson free(devlist, M_TEMP); 204388a79fc0SNate Lawson 204488a79fc0SNate Lawson return (0); 204588a79fc0SNate Lawson } 204688a79fc0SNate Lawson 204788a79fc0SNate Lawson /* Enable or disable wake from userland. */ 204888a79fc0SNate Lawson static int 204988a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 205088a79fc0SNate Lawson { 205188a79fc0SNate Lawson int enable, error; 205288a79fc0SNate Lawson device_t dev; 205388a79fc0SNate Lawson 205488a79fc0SNate Lawson dev = (device_t)arg1; 205588a79fc0SNate Lawson enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 205688a79fc0SNate Lawson 205788a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 205888a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 205988a79fc0SNate Lawson return (error); 206088a79fc0SNate Lawson if (enable != 0 && enable != 1) 206188a79fc0SNate Lawson return (EINVAL); 206288a79fc0SNate Lawson 206388a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 206488a79fc0SNate Lawson } 206588a79fc0SNate Lawson 2066e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2067e8b4d56eSNate Lawson static int 2068e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2069e8b4d56eSNate Lawson { 2070e8b4d56eSNate Lawson ACPI_STATUS status; 2071e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2072e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2073e8b4d56eSNate Lawson int error; 2074e8b4d56eSNate Lawson 2075e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2076e8b4d56eSNate Lawson return (EINVAL); 2077e8b4d56eSNate Lawson 2078e8b4d56eSNate Lawson /* 2079e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2080e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2081e8b4d56eSNate Lawson */ 2082e8b4d56eSNate Lawson error = EINVAL; 2083e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2084e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2085e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2086e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2087e8b4d56eSNate Lawson return (ENOENT); 2088e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2089e8b4d56eSNate Lawson if (res == NULL) 2090e8b4d56eSNate Lawson return (ENOENT); 2091e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2092e8b4d56eSNate Lawson goto out; 2093e8b4d56eSNate Lawson 2094e8b4d56eSNate Lawson /* 2095e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2096e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2097e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2098e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2099e8b4d56eSNate Lawson */ 2100e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2101e8b4d56eSNate Lawson goto out; 2102e8b4d56eSNate Lawson 2103e8b4d56eSNate Lawson /* 2104e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2105e8b4d56eSNate Lawson */ 2106e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2107e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2108e8b4d56eSNate Lawson /* 2109e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2110e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2111e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2112e8b4d56eSNate Lawson * enabled for the wake event. 2113e8b4d56eSNate Lawson */ 2114e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2115e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2116e8b4d56eSNate Lawson error = 0; 2117e8b4d56eSNate Lawson break; 2118e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2119e8b4d56eSNate Lawson /* 2120e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2121e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2122e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2123e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2124e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2125e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2126e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2127e8b4d56eSNate Lawson * the wake event. 2128e8b4d56eSNate Lawson * 2129e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2130e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2131e8b4d56eSNate Lawson */ 2132e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2133e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2134e8b4d56eSNate Lawson goto out; 2135e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2136e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2137e8b4d56eSNate Lawson goto out; 2138e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2139e8b4d56eSNate Lawson goto out; 2140e8b4d56eSNate Lawson error = 0; 2141e8b4d56eSNate Lawson break; 2142e8b4d56eSNate Lawson default: 2143e8b4d56eSNate Lawson goto out; 2144e8b4d56eSNate Lawson } 2145e8b4d56eSNate Lawson 2146e8b4d56eSNate Lawson /* XXX No power resource handling yet. */ 2147e8b4d56eSNate Lawson prw->power_res = NULL; 2148e8b4d56eSNate Lawson 2149e8b4d56eSNate Lawson out: 2150e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2151e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2152e8b4d56eSNate Lawson return (error); 2153e8b4d56eSNate Lawson } 2154e8b4d56eSNate Lawson 215515e32d5dSMike Smith /* 215615e32d5dSMike Smith * Enable/Disable ACPI 215715e32d5dSMike Smith */ 215815e32d5dSMike Smith ACPI_STATUS 215915e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 216015e32d5dSMike Smith { 216115e32d5dSMike Smith ACPI_STATUS status; 216215e32d5dSMike Smith u_int32_t flags; 216315e32d5dSMike Smith 2164b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2165cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21660ae55423SMike Smith 216715e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 216815e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 2169be2b1797SNate Lawson if (!sc->acpi_enabled) 217015e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 2171be2b1797SNate Lawson else 217215e32d5dSMike Smith status = AE_OK; 2173be2b1797SNate Lawson 217415e32d5dSMike Smith if (status == AE_OK) 217515e32d5dSMike Smith sc->acpi_enabled = 1; 2176be2b1797SNate Lawson 21770ae55423SMike Smith return_ACPI_STATUS (status); 217815e32d5dSMike Smith } 217915e32d5dSMike Smith 218015e32d5dSMike Smith ACPI_STATUS 218115e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 218215e32d5dSMike Smith { 218315e32d5dSMike Smith ACPI_STATUS status; 218415e32d5dSMike Smith 2185b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2186cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21870ae55423SMike Smith 2188be2b1797SNate Lawson if (sc->acpi_enabled) 218915e32d5dSMike Smith status = AcpiDisable(); 2190be2b1797SNate Lawson else 219115e32d5dSMike Smith status = AE_OK; 2192be2b1797SNate Lawson 219315e32d5dSMike Smith if (status == AE_OK) 219415e32d5dSMike Smith sc->acpi_enabled = 0; 2195be2b1797SNate Lawson 21960ae55423SMike Smith return_ACPI_STATUS (status); 219715e32d5dSMike Smith } 219815e32d5dSMike Smith 219915e32d5dSMike Smith /* 220015e32d5dSMike Smith * ACPI Event Handlers 220115e32d5dSMike Smith */ 220215e32d5dSMike Smith 220315e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 220415e32d5dSMike Smith 220515e32d5dSMike Smith static void 220615e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 220715e32d5dSMike Smith { 2208fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2209b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 221015e32d5dSMike Smith 2211cb9b0d80SMike Smith ACPI_LOCK; 2212a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 221315e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2214cb9b0d80SMike Smith ACPI_UNLOCK; 22150ae55423SMike Smith return_VOID; 221615e32d5dSMike Smith } 221715e32d5dSMike Smith 221815e32d5dSMike Smith static void 221915e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 222015e32d5dSMike Smith { 2221fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2222b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 22230ae55423SMike Smith 222415e32d5dSMike Smith /* Well, what to do? :-) */ 22250ae55423SMike Smith 2226cb9b0d80SMike Smith ACPI_LOCK; 2227cb9b0d80SMike Smith ACPI_UNLOCK; 2228cb9b0d80SMike Smith 22290ae55423SMike Smith return_VOID; 223015e32d5dSMike Smith } 223115e32d5dSMike Smith 223215e32d5dSMike Smith /* 223315e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 223415e32d5dSMike Smith */ 223515e32d5dSMike Smith UINT32 223633febf93SNate Lawson acpi_event_power_button_sleep(void *context) 223715e32d5dSMike Smith { 223815e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 223915e32d5dSMike Smith 2240b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22410ae55423SMike Smith 224215e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 22430ae55423SMike Smith 2244b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 224515e32d5dSMike Smith } 224615e32d5dSMike Smith 224715e32d5dSMike Smith UINT32 224833febf93SNate Lawson acpi_event_power_button_wake(void *context) 224915e32d5dSMike Smith { 225015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 225115e32d5dSMike Smith 2252b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22530ae55423SMike Smith 225415e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 22550ae55423SMike Smith 2256b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 225715e32d5dSMike Smith } 225815e32d5dSMike Smith 225915e32d5dSMike Smith UINT32 226033febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 226115e32d5dSMike Smith { 226215e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 226315e32d5dSMike Smith 2264b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22650ae55423SMike Smith 226615e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 22670ae55423SMike Smith 2268b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 226915e32d5dSMike Smith } 227015e32d5dSMike Smith 227115e32d5dSMike Smith UINT32 227233febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 227315e32d5dSMike Smith { 227415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 227515e32d5dSMike Smith 2276b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22770ae55423SMike Smith 227815e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 22790ae55423SMike Smith 2280b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 228115e32d5dSMike Smith } 228215e32d5dSMike Smith 228315e32d5dSMike Smith /* 228415e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 228515e32d5dSMike Smith */ 228615e32d5dSMike Smith struct acpi_staticbuf { 228715e32d5dSMike Smith ACPI_BUFFER buffer; 228815e32d5dSMike Smith char data[512]; 228915e32d5dSMike Smith }; 229015e32d5dSMike Smith 229115e32d5dSMike Smith char * 229215e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 229315e32d5dSMike Smith { 229415e32d5dSMike Smith static struct acpi_staticbuf buf; 229515e32d5dSMike Smith 2296cb9b0d80SMike Smith ACPI_ASSERTLOCK; 2297cb9b0d80SMike Smith 229815e32d5dSMike Smith buf.buffer.Length = 512; 229915e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 230015e32d5dSMike Smith 2301b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 230215e32d5dSMike Smith return (buf.buffer.Pointer); 2303be2b1797SNate Lawson 230415e32d5dSMike Smith return ("(unknown path)"); 230515e32d5dSMike Smith } 230615e32d5dSMike Smith 230715e32d5dSMike Smith /* 230815e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 230915e32d5dSMike Smith * parts of the namespace. 231015e32d5dSMike Smith */ 231115e32d5dSMike Smith int 231215e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 231315e32d5dSMike Smith { 23143c600cfbSJohn Baldwin char *cp, *env, *np; 231515e32d5dSMike Smith int len; 231615e32d5dSMike Smith 231715e32d5dSMike Smith np = acpi_name(handle); 231815e32d5dSMike Smith if (*np == '\\') 231915e32d5dSMike Smith np++; 23203c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 232115e32d5dSMike Smith return (0); 232215e32d5dSMike Smith 2323be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 23243c600cfbSJohn Baldwin cp = env; 232515e32d5dSMike Smith for (;;) { 232615e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 232715e32d5dSMike Smith cp++; 232815e32d5dSMike Smith if (*cp == 0) 232915e32d5dSMike Smith break; 233015e32d5dSMike Smith len = 0; 233115e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 233215e32d5dSMike Smith len++; 2333d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 23343c600cfbSJohn Baldwin freeenv(env); 23350ae55423SMike Smith return(1); 2336d786139cSMaxime Henrion } 23370ae55423SMike Smith cp += len; 23380ae55423SMike Smith } 23393c600cfbSJohn Baldwin freeenv(env); 2340be2b1797SNate Lawson 23410ae55423SMike Smith return (0); 23420ae55423SMike Smith } 23430ae55423SMike Smith 23440ae55423SMike Smith /* 23450ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 23460ae55423SMike Smith */ 23470ae55423SMike Smith int 23480ae55423SMike Smith acpi_disabled(char *subsys) 23490ae55423SMike Smith { 235078689b15SMaxime Henrion char *cp, *env; 23510ae55423SMike Smith int len; 23520ae55423SMike Smith 23533184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 23540ae55423SMike Smith return (0); 23553184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 235678689b15SMaxime Henrion freeenv(env); 23570ae55423SMike Smith return (1); 2358d786139cSMaxime Henrion } 23590ae55423SMike Smith 23603184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 236178689b15SMaxime Henrion cp = env; 23620ae55423SMike Smith for (;;) { 23633184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 23640ae55423SMike Smith cp++; 23653184cf5aSNate Lawson if (*cp == '\0') 23660ae55423SMike Smith break; 23670ae55423SMike Smith len = 0; 23683184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 23690ae55423SMike Smith len++; 23703184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 237178689b15SMaxime Henrion freeenv(env); 237215e32d5dSMike Smith return (1); 2373d786139cSMaxime Henrion } 237415e32d5dSMike Smith cp += len; 237515e32d5dSMike Smith } 237678689b15SMaxime Henrion freeenv(env); 2377be2b1797SNate Lawson 237815e32d5dSMike Smith return (0); 237915e32d5dSMike Smith } 238015e32d5dSMike Smith 238115e32d5dSMike Smith /* 238215e32d5dSMike Smith * Control interface. 238315e32d5dSMike Smith * 23840ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2385be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2386be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 238715e32d5dSMike Smith */ 23880ae55423SMike Smith struct acpi_ioctl_hook 23890ae55423SMike Smith { 23900ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 23910ae55423SMike Smith u_long cmd; 2392be2b1797SNate Lawson acpi_ioctl_fn fn; 23930ae55423SMike Smith void *arg; 23940ae55423SMike Smith }; 23950ae55423SMike Smith 23960ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 23970ae55423SMike Smith static int acpi_ioctl_hooks_initted; 23980ae55423SMike Smith 23990ae55423SMike Smith /* 24000ae55423SMike Smith * Register an ioctl handler. 24010ae55423SMike Smith */ 24020ae55423SMike Smith int 2403be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 24040ae55423SMike Smith { 24050ae55423SMike Smith struct acpi_ioctl_hook *hp; 24060ae55423SMike Smith 24070ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 24080ae55423SMike Smith return (ENOMEM); 24090ae55423SMike Smith hp->cmd = cmd; 24100ae55423SMike Smith hp->fn = fn; 24110ae55423SMike Smith hp->arg = arg; 24120ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 24130ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 24140ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 24150ae55423SMike Smith } 24160ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 24170ae55423SMike Smith return (0); 24180ae55423SMike Smith } 24190ae55423SMike Smith 24200ae55423SMike Smith /* 24210ae55423SMike Smith * Deregister an ioctl handler. 24220ae55423SMike Smith */ 24230ae55423SMike Smith void 2424be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 24250ae55423SMike Smith { 24260ae55423SMike Smith struct acpi_ioctl_hook *hp; 24270ae55423SMike Smith 24280ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 24290ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 24300ae55423SMike Smith break; 24310ae55423SMike Smith 24320ae55423SMike Smith if (hp != NULL) { 24330ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 24340ae55423SMike Smith free(hp, M_ACPIDEV); 24350ae55423SMike Smith } 24360ae55423SMike Smith } 24370ae55423SMike Smith 243815e32d5dSMike Smith static int 243989c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 244015e32d5dSMike Smith { 244115e32d5dSMike Smith return (0); 244215e32d5dSMike Smith } 244315e32d5dSMike Smith 244415e32d5dSMike Smith static int 244589c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 244615e32d5dSMike Smith { 244715e32d5dSMike Smith return (0); 244815e32d5dSMike Smith } 244915e32d5dSMike Smith 245015e32d5dSMike Smith static int 245189c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 245215e32d5dSMike Smith { 245315e32d5dSMike Smith struct acpi_softc *sc; 24540ae55423SMike Smith struct acpi_ioctl_hook *hp; 24550ae55423SMike Smith int error, xerror, state; 2456fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 245715e32d5dSMike Smith 2458cb9b0d80SMike Smith ACPI_LOCK; 2459cb9b0d80SMike Smith 246015e32d5dSMike Smith error = state = 0; 246115e32d5dSMike Smith sc = dev->si_drv1; 246215e32d5dSMike Smith 24630ae55423SMike Smith /* 24640ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 24650ae55423SMike Smith */ 24660ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 24670ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 24680ae55423SMike Smith if (hp->cmd == cmd) { 24690ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 24700ae55423SMike Smith if (xerror != 0) 24710ae55423SMike Smith error = xerror; 2472917d44c8SMitsuru IWASAKI goto out; 24730ae55423SMike Smith } 24740ae55423SMike Smith } 24750ae55423SMike Smith } 24760ae55423SMike Smith 24770ae55423SMike Smith /* 2478a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2479a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2480a89fcf28STakanori Watanabe * Not changing system behavior. 2481a89fcf28STakanori Watanabe */ 2482be2b1797SNate Lawson if ((flag & FWRITE) == 0) 2483be2b1797SNate Lawson return (EPERM); 2484a89fcf28STakanori Watanabe 2485be2b1797SNate Lawson /* Core system ioctls. */ 248615e32d5dSMike Smith switch (cmd) { 248715e32d5dSMike Smith case ACPIIO_ENABLE: 24880ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 248915e32d5dSMike Smith error = ENXIO; 249015e32d5dSMike Smith break; 249115e32d5dSMike Smith case ACPIIO_DISABLE: 24920ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 249315e32d5dSMike Smith error = ENXIO; 249415e32d5dSMike Smith break; 249515e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 249615e32d5dSMike Smith if (!sc->acpi_enabled) { 249715e32d5dSMike Smith error = ENXIO; 249815e32d5dSMike Smith break; 249915e32d5dSMike Smith } 250015e32d5dSMike Smith state = *(int *)addr; 25012d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 25022d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 250315e32d5dSMike Smith error = EINVAL; 25042d610c46SNate Lawson } else { 25052d610c46SNate Lawson error = EINVAL; 25062d610c46SNate Lawson } 250715e32d5dSMike Smith break; 250815e32d5dSMike Smith default: 25090ae55423SMike Smith if (error == 0) 251015e32d5dSMike Smith error = EINVAL; 251115e32d5dSMike Smith break; 251215e32d5dSMike Smith } 2513917d44c8SMitsuru IWASAKI 2514917d44c8SMitsuru IWASAKI out: 2515cb9b0d80SMike Smith ACPI_UNLOCK; 251615e32d5dSMike Smith return (error); 251715e32d5dSMike Smith } 251815e32d5dSMike Smith 25191d073b1dSJohn Baldwin static int 2520d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2521d75de536SMitsuru IWASAKI { 2522d75de536SMitsuru IWASAKI char sleep_state[4]; 2523d75de536SMitsuru IWASAKI char buf[16]; 2524d75de536SMitsuru IWASAKI int error; 2525d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2526d75de536SMitsuru IWASAKI 2527d75de536SMitsuru IWASAKI buf[0] = '\0'; 2528d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) { 2529d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2530d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2531d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2532d75de536SMitsuru IWASAKI } 2533d75de536SMitsuru IWASAKI } 2534d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2535d75de536SMitsuru IWASAKI return (error); 2536d75de536SMitsuru IWASAKI } 2537d75de536SMitsuru IWASAKI 2538d75de536SMitsuru IWASAKI static int 25391d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 25401d073b1dSJohn Baldwin { 25411d073b1dSJohn Baldwin char sleep_state[10]; 25421d073b1dSJohn Baldwin int error; 25431d073b1dSJohn Baldwin u_int new_state, old_state; 25441d073b1dSJohn Baldwin 25451d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2546b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX + 1) { 25471d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2548cb9b0d80SMike Smith } else { 2549b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 25501d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 25511d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2552cb9b0d80SMike Smith } 25531d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 25541d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2555be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2556be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 25571d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 25581d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 25591d073b1dSJohn Baldwin break; 2560cb9b0d80SMike Smith } 2561931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2562be2b1797SNate Lawson if (new_state != old_state) 25631d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2564cb9b0d80SMike Smith } else { 25651d073b1dSJohn Baldwin error = EINVAL; 25661d073b1dSJohn Baldwin } 2567cb9b0d80SMike Smith } 2568be2b1797SNate Lawson 25691d073b1dSJohn Baldwin return (error); 25701d073b1dSJohn Baldwin } 25711d073b1dSJohn Baldwin 25729b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 25739b937d48SNate Lawson void 25749b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 25759b937d48SNate Lawson { 25769b937d48SNate Lawson char notify_buf[16]; 25779b937d48SNate Lawson ACPI_BUFFER handle_buf; 25789b937d48SNate Lawson ACPI_STATUS status; 25799b937d48SNate Lawson 25809b937d48SNate Lawson if (subsystem == NULL) 25819b937d48SNate Lawson return; 25829b937d48SNate Lawson 25839b937d48SNate Lawson handle_buf.Pointer = NULL; 25849b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 25859b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 25869b937d48SNate Lawson if (ACPI_FAILURE(status)) 25879b937d48SNate Lawson return; 25889b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 25899b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 25909b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 25919b937d48SNate Lawson } 25929b937d48SNate Lawson 259315e32d5dSMike Smith #ifdef ACPI_DEBUG 25940ae55423SMike Smith /* 25950ae55423SMike Smith * Support for parsing debug options from the kernel environment. 25960ae55423SMike Smith * 25970ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 25980ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 25990ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 26000ae55423SMike Smith * prefixing the bit name with !. 26010ae55423SMike Smith */ 260215e32d5dSMike Smith struct debugtag 260315e32d5dSMike Smith { 260415e32d5dSMike Smith char *name; 260515e32d5dSMike Smith UINT32 value; 260615e32d5dSMike Smith }; 260715e32d5dSMike Smith 260815e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2609c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2610c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2611c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2612c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2613c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2614c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2615c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2616c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2617c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2618d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 26194c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2620d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2621297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 26224c1cdee6SMike Smith 2623c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2624c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 26253184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2626c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 26273184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 26283184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 26293184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 26304c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2631cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 26323184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2633b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 263415e32d5dSMike Smith {NULL, 0} 263515e32d5dSMike Smith }; 263615e32d5dSMike Smith 263715e32d5dSMike Smith static struct debugtag dbg_level[] = { 26384c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 26399501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 26409501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 26414c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 26429501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 26434c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2644d62ab2f4SMitsuru IWASAKI 2645d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2646297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 26474c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 26484c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2649d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 26504c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 26514c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 26524c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 26534c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 26544c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 26554c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 26564c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 26574c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 26584c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 26594c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2660d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2661d62ab2f4SMitsuru IWASAKI 2662d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2663d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2664d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2665d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2666d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 26674c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2668d62ab2f4SMitsuru IWASAKI 2669d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2670d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2671d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2672d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2673d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2674d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2675d62ab2f4SMitsuru IWASAKI 2676d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 267798479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 267898479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 267998479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 268098479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 268198479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 268215e32d5dSMike Smith {NULL, 0} 268315e32d5dSMike Smith }; 268415e32d5dSMike Smith 268515e32d5dSMike Smith static void 268615e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 268715e32d5dSMike Smith { 268815e32d5dSMike Smith char *ep; 268915e32d5dSMike Smith int i, l; 26900ae55423SMike Smith int set; 269115e32d5dSMike Smith 269215e32d5dSMike Smith while (*cp) { 269315e32d5dSMike Smith if (isspace(*cp)) { 269415e32d5dSMike Smith cp++; 269515e32d5dSMike Smith continue; 269615e32d5dSMike Smith } 269715e32d5dSMike Smith ep = cp; 269815e32d5dSMike Smith while (*ep && !isspace(*ep)) 269915e32d5dSMike Smith ep++; 27000ae55423SMike Smith if (*cp == '!') { 27010ae55423SMike Smith set = 0; 27020ae55423SMike Smith cp++; 27030ae55423SMike Smith if (cp == ep) 27040ae55423SMike Smith continue; 27050ae55423SMike Smith } else { 27060ae55423SMike Smith set = 1; 27070ae55423SMike Smith } 270815e32d5dSMike Smith l = ep - cp; 270915e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 271015e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2711be2b1797SNate Lawson if (set) 271215e32d5dSMike Smith *flag |= tag[i].value; 2713be2b1797SNate Lawson else 27140ae55423SMike Smith *flag &= ~tag[i].value; 271515e32d5dSMike Smith } 271615e32d5dSMike Smith } 271715e32d5dSMike Smith cp = ep; 271815e32d5dSMike Smith } 271915e32d5dSMike Smith } 272015e32d5dSMike Smith 272115e32d5dSMike Smith static void 27222a4ac806SMike Smith acpi_set_debugging(void *junk) 272315e32d5dSMike Smith { 27247e639165SNate Lawson char *layer, *level; 272515e32d5dSMike Smith 27261d7b121cSNate Lawson if (cold) { 27270ae55423SMike Smith AcpiDbgLayer = 0; 27280ae55423SMike Smith AcpiDbgLevel = 0; 27291d7b121cSNate Lawson } 27301d7b121cSNate Lawson 27317e639165SNate Lawson layer = getenv("debug.acpi.layer"); 27327e639165SNate Lawson level = getenv("debug.acpi.level"); 27337e639165SNate Lawson if (layer == NULL && level == NULL) 27347e639165SNate Lawson return; 27350ae55423SMike Smith 27367e639165SNate Lawson printf("ACPI set debug"); 27377e639165SNate Lawson if (layer != NULL) { 27387e639165SNate Lawson if (strcmp("NONE", layer) != 0) 27397e639165SNate Lawson printf(" layer '%s'", layer); 27407e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 27417e639165SNate Lawson freeenv(layer); 27421d7b121cSNate Lawson } 27437e639165SNate Lawson if (level != NULL) { 27447e639165SNate Lawson if (strcmp("NONE", level) != 0) 27457e639165SNate Lawson printf(" level '%s'", level); 27467e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 27477e639165SNate Lawson freeenv(level); 27487e639165SNate Lawson } 27497e639165SNate Lawson printf("\n"); 275015e32d5dSMike Smith } 2751be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2752be2b1797SNate Lawson NULL); 27531d7b121cSNate Lawson 27541d7b121cSNate Lawson static int 27551d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 27561d7b121cSNate Lawson { 275754f6bca0SNate Lawson int error, *dbg; 27581d7b121cSNate Lawson struct debugtag *tag; 275954f6bca0SNate Lawson struct sbuf sb; 27601d7b121cSNate Lawson 276154f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 276254f6bca0SNate Lawson return (ENOMEM); 27631d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 27641d7b121cSNate Lawson tag = &dbg_layer[0]; 27651d7b121cSNate Lawson dbg = &AcpiDbgLayer; 27661d7b121cSNate Lawson } else { 27671d7b121cSNate Lawson tag = &dbg_level[0]; 27681d7b121cSNate Lawson dbg = &AcpiDbgLevel; 27691d7b121cSNate Lawson } 27701d7b121cSNate Lawson 27711d7b121cSNate Lawson /* Get old values if this is a get request. */ 27721d7b121cSNate Lawson if (*dbg == 0) { 277354f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 27741d7b121cSNate Lawson } else if (req->newptr == NULL) { 27751d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 277654f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 277754f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 27781d7b121cSNate Lawson } 27791d7b121cSNate Lawson } 278054f6bca0SNate Lawson sbuf_trim(&sb); 278154f6bca0SNate Lawson sbuf_finish(&sb); 27821d7b121cSNate Lawson 27837e639165SNate Lawson /* Copy out the old values to the user. */ 27847e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 278554f6bca0SNate Lawson sbuf_delete(&sb); 27861d7b121cSNate Lawson 27871d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 27881d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 27891d7b121cSNate Lawson *dbg = 0; 27901d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 27911d7b121cSNate Lawson acpi_set_debugging(NULL); 27921d7b121cSNate Lawson } 27931d7b121cSNate Lawson 27941d7b121cSNate Lawson return (error); 27951d7b121cSNate Lawson } 27961d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 27971d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 27981d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 27991d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 280015e32d5dSMike Smith #endif 2801f9390180SMitsuru IWASAKI 2802f9390180SMitsuru IWASAKI static int 2803f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2804f9390180SMitsuru IWASAKI { 2805f9390180SMitsuru IWASAKI int state, acpi_state; 2806f9390180SMitsuru IWASAKI int error; 2807f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2808f9390180SMitsuru IWASAKI va_list ap; 2809f9390180SMitsuru IWASAKI 2810f9390180SMitsuru IWASAKI error = 0; 2811f9390180SMitsuru IWASAKI switch (cmd) { 2812f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2813f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2814f9390180SMitsuru IWASAKI if (sc == NULL) { 2815f9390180SMitsuru IWASAKI error = EINVAL; 2816f9390180SMitsuru IWASAKI goto out; 2817f9390180SMitsuru IWASAKI } 2818f9390180SMitsuru IWASAKI 2819f9390180SMitsuru IWASAKI va_start(ap, arg); 2820f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2821f9390180SMitsuru IWASAKI va_end(ap); 2822f9390180SMitsuru IWASAKI 2823f9390180SMitsuru IWASAKI switch (state) { 2824f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2825f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2826f9390180SMitsuru IWASAKI break; 2827f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2828f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2829f9390180SMitsuru IWASAKI break; 2830f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2831f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2832f9390180SMitsuru IWASAKI break; 2833f9390180SMitsuru IWASAKI default: 2834f9390180SMitsuru IWASAKI error = EINVAL; 2835f9390180SMitsuru IWASAKI goto out; 2836f9390180SMitsuru IWASAKI } 2837f9390180SMitsuru IWASAKI 2838f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2839f9390180SMitsuru IWASAKI break; 2840f9390180SMitsuru IWASAKI default: 2841f9390180SMitsuru IWASAKI error = EINVAL; 2842f9390180SMitsuru IWASAKI goto out; 2843f9390180SMitsuru IWASAKI } 2844f9390180SMitsuru IWASAKI 2845f9390180SMitsuru IWASAKI out: 2846f9390180SMitsuru IWASAKI return (error); 2847f9390180SMitsuru IWASAKI } 2848f9390180SMitsuru IWASAKI 2849f9390180SMitsuru IWASAKI static void 2850f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2851f9390180SMitsuru IWASAKI { 2852be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 28536c407052SMitsuru IWASAKI return; 2854f9390180SMitsuru IWASAKI 2855f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2856f9390180SMitsuru IWASAKI } 2857f9390180SMitsuru IWASAKI 2858f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2859