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> 3815e32d5dSMike Smith #include <sys/bus.h> 3915e32d5dSMike Smith #include <sys/conf.h> 4015e32d5dSMike Smith #include <sys/ioccom.h> 4115e32d5dSMike Smith #include <sys/reboot.h> 4215e32d5dSMike Smith #include <sys/sysctl.h> 4315e32d5dSMike Smith #include <sys/ctype.h> 441611ea87SMitsuru IWASAKI #include <sys/linker.h> 45f9390180SMitsuru IWASAKI #include <sys/power.h> 4615e32d5dSMike Smith 4715e32d5dSMike Smith #include <machine/clock.h> 4815e32d5dSMike Smith #include <machine/resource.h> 4915e32d5dSMike Smith 50bc0f2195SMike Smith #include <isa/isavar.h> 51bc0f2195SMike Smith 5215e32d5dSMike Smith #include "acpi.h" 5315e32d5dSMike Smith 541611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h> 551611ea87SMitsuru IWASAKI 5615e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5715e32d5dSMike Smith #include <dev/acpica/acpiio.h> 5815e32d5dSMike Smith 5915e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 6015e32d5dSMike Smith 6115e32d5dSMike Smith /* 620ae55423SMike Smith * Hooks for the ACPI CA debugging infrastructure 630ae55423SMike Smith */ 64cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 65b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 660ae55423SMike Smith 670ae55423SMike Smith /* 6815e32d5dSMike Smith * Character device 6915e32d5dSMike Smith */ 7015e32d5dSMike Smith 7115e32d5dSMike Smith static d_open_t acpiopen; 7215e32d5dSMike Smith static d_close_t acpiclose; 7315e32d5dSMike Smith static d_ioctl_t acpiioctl; 7415e32d5dSMike Smith 7515e32d5dSMike Smith #define CDEV_MAJOR 152 7615e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 7715e32d5dSMike Smith acpiopen, 7815e32d5dSMike Smith acpiclose, 7915e32d5dSMike Smith noread, 8015e32d5dSMike Smith nowrite, 8115e32d5dSMike Smith acpiioctl, 8215e32d5dSMike Smith nopoll, 8315e32d5dSMike Smith nommap, 8415e32d5dSMike Smith nostrategy, 8515e32d5dSMike Smith "acpi", 8615e32d5dSMike Smith CDEV_MAJOR, 8715e32d5dSMike Smith nodump, 8815e32d5dSMike Smith nopsize, 89606e1d68SJohn Baldwin 0 9015e32d5dSMike Smith }; 9115e32d5dSMike Smith 921d073b1dSJohn Baldwin static const char* sleep_state_names[] = { 93b8670bedSMitsuru IWASAKI "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 941d073b1dSJohn Baldwin 952a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */ 962a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5; 972a4ac806SMike Smith 98fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 99cb9b0d80SMike Smith struct mtx acpi_mutex; 100fc0ea94aSJohn Baldwin #endif 101cb9b0d80SMike Smith 1026d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10315e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 10415e32d5dSMike Smith static int acpi_probe(device_t dev); 10515e32d5dSMike Smith static int acpi_attach(device_t dev); 10615e32d5dSMike Smith static device_t acpi_add_child(device_t bus, int order, const char *name, int unit); 10715e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 10815e32d5dSMike Smith static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result); 10915e32d5dSMike Smith static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value); 11015e32d5dSMike Smith static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, 11115e32d5dSMike Smith u_long count); 11215e32d5dSMike Smith static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, 11315e32d5dSMike Smith u_long *countp); 11415e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 11515e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags); 11615e32d5dSMike Smith static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r); 11732d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev); 118bc0f2195SMike Smith static int acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids); 11915e32d5dSMike Smith 12015e32d5dSMike Smith static void acpi_probe_children(device_t bus); 12115e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status); 12215e32d5dSMike Smith 12315e32d5dSMike Smith static void acpi_shutdown_pre_sync(void *arg, int howto); 12415e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 12515e32d5dSMike Smith 12613d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 12713d4f7baSMitsuru IWASAKI 12815e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 12915e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 1301d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 13115e32d5dSMike Smith 132f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 133f9390180SMitsuru IWASAKI 13415e32d5dSMike Smith static device_method_t acpi_methods[] = { 13515e32d5dSMike Smith /* Device interface */ 13615e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 13715e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 13815e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 13915e32d5dSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 14015e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 14115e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 14215e32d5dSMike Smith 14315e32d5dSMike Smith /* Bus interface */ 14415e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 14515e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 14615e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 14715e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 14815e32d5dSMike Smith DEVMETHOD(bus_set_resource, acpi_set_resource), 14915e32d5dSMike Smith DEVMETHOD(bus_get_resource, acpi_get_resource), 15015e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 15115e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 15215e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 15315e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 15415e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 15515e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 15615e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 15715e32d5dSMike Smith 158bc0f2195SMike Smith /* ISA emulation */ 159bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 160bc0f2195SMike Smith 16115e32d5dSMike Smith {0, 0} 16215e32d5dSMike Smith }; 16315e32d5dSMike Smith 16415e32d5dSMike Smith static driver_t acpi_driver = { 16515e32d5dSMike Smith "acpi", 16615e32d5dSMike Smith acpi_methods, 16715e32d5dSMike Smith sizeof(struct acpi_softc), 16815e32d5dSMike Smith }; 16915e32d5dSMike Smith 1703273b005SMike Smith static devclass_t acpi_devclass; 1716d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 172dde24897SMike Smith MODULE_VERSION(acpi, 100); 17315e32d5dSMike Smith 17415e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, ""); 17515e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, ""); 176dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION; 177dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, ""); 17815e32d5dSMike Smith 17915e32d5dSMike Smith /* 1806d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 1816d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 1826d63101aSMike Smith * It also cannot be unloaded, since the entire system bus heirarchy hangs off it. 1836d63101aSMike Smith */ 1846d63101aSMike Smith static int 1856d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 1866d63101aSMike Smith { 1876d63101aSMike Smith switch(event) { 1886d63101aSMike Smith case MOD_LOAD: 18987b45ed5SMitsuru IWASAKI if (!cold) { 19087b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 1916d63101aSMike Smith return(EPERM); 19287b45ed5SMitsuru IWASAKI } 1936d63101aSMike Smith break; 1946d63101aSMike Smith case MOD_UNLOAD: 195f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 1966d63101aSMike Smith return(EBUSY); 197f9390180SMitsuru IWASAKI break; 1986d63101aSMike Smith default: 1996d63101aSMike Smith break; 2006d63101aSMike Smith } 2016d63101aSMike Smith return(0); 2026d63101aSMike Smith } 2036d63101aSMike Smith 2046d63101aSMike Smith /* 20515e32d5dSMike Smith * Detect ACPI, perform early initialisation 20615e32d5dSMike Smith */ 20715e32d5dSMike Smith static void 20815e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent) 20915e32d5dSMike Smith { 21015e32d5dSMike Smith device_t child; 21115e32d5dSMike Smith int error; 212d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 213d786139cSMaxime Henrion char *debugpoint; 21415e32d5dSMike Smith #endif 21515e32d5dSMike Smith 216b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2170ae55423SMike Smith 21887b45ed5SMitsuru IWASAKI if (!cold) 21987b45ed5SMitsuru IWASAKI return_VOID; 220840f9b53STakanori Watanabe 22115e32d5dSMike Smith /* 22232d18aa5SMike Smith * Check that we haven't been disabled with a hint. 22332d18aa5SMike Smith */ 22432d18aa5SMike Smith if (!resource_int_value("acpi", 0, "disabled", &error) && 22532d18aa5SMike Smith (error != 0)) 22632d18aa5SMike Smith return_VOID; 22732d18aa5SMike Smith 22832d18aa5SMike Smith /* 22915e32d5dSMike Smith * Make sure we're not being doubly invoked. 23015e32d5dSMike Smith */ 23115e32d5dSMike Smith if (device_find_child(parent, "acpi", 0) != NULL) 2320ae55423SMike Smith return_VOID; 23315e32d5dSMike Smith 234fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 235cb9b0d80SMike Smith /* initialise the ACPI mutex */ 2366008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 237fc0ea94aSJohn Baldwin #endif 238cb9b0d80SMike Smith 23915e32d5dSMike Smith /* 2402a4ac806SMike Smith * Start up the ACPI CA subsystem. 24115e32d5dSMike Smith */ 242d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 243d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 244d786139cSMaxime Henrion if (debugpoint) { 245d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 24615e32d5dSMike Smith acpi_EnterDebugger(); 247d786139cSMaxime Henrion freeenv(debugpoint); 248d786139cSMaxime Henrion } 24915e32d5dSMike Smith #endif 250b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 251bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 2520ae55423SMike Smith return_VOID; 25315e32d5dSMike Smith } 254d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 255d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 256d786139cSMaxime Henrion if (debugpoint) { 257d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 25815e32d5dSMike Smith acpi_EnterDebugger(); 259d786139cSMaxime Henrion freeenv(debugpoint); 260d786139cSMaxime Henrion } 26115e32d5dSMike Smith #endif 2621611ea87SMitsuru IWASAKI 263b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 264bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 2650ae55423SMike Smith return_VOID; 26615e32d5dSMike Smith } 26715e32d5dSMike Smith 26815e32d5dSMike Smith /* 26915e32d5dSMike Smith * Attach the actual ACPI device. 27015e32d5dSMike Smith */ 27115e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 27215e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 2730ae55423SMike Smith return_VOID; 27415e32d5dSMike Smith } 27515e32d5dSMike Smith } 27615e32d5dSMike Smith 27715e32d5dSMike Smith /* 27815e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 27915e32d5dSMike Smith */ 28015e32d5dSMike Smith static int 28115e32d5dSMike Smith acpi_probe(device_t dev) 28215e32d5dSMike Smith { 28315e32d5dSMike Smith ACPI_TABLE_HEADER th; 28415e32d5dSMike Smith char buf[20]; 285cb9b0d80SMike Smith ACPI_STATUS status; 28615e32d5dSMike Smith int error; 287fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 28815e32d5dSMike Smith 289b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 290f9390180SMitsuru IWASAKI 291f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 292f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 293f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 294f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 295f9390180SMitsuru IWASAKI } 296f9390180SMitsuru IWASAKI 297cb9b0d80SMike Smith ACPI_LOCK; 2980ae55423SMike Smith 299b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 300bfae45aaSMike Smith device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status)); 301cb9b0d80SMike Smith error = ENXIO; 302cb9b0d80SMike Smith } else { 30315e32d5dSMike Smith sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 30415e32d5dSMike Smith device_set_desc_copy(dev, buf); 305cb9b0d80SMike Smith error = 0; 306cb9b0d80SMike Smith } 307cb9b0d80SMike Smith ACPI_UNLOCK; 308cb9b0d80SMike Smith return_VALUE(error); 30915e32d5dSMike Smith } 31015e32d5dSMike Smith 31115e32d5dSMike Smith static int 31215e32d5dSMike Smith acpi_attach(device_t dev) 31315e32d5dSMike Smith { 31415e32d5dSMike Smith struct acpi_softc *sc; 315cb9b0d80SMike Smith ACPI_STATUS status; 31615e32d5dSMike Smith int error; 31732d18aa5SMike Smith UINT32 flags; 318498d464fSMitsuru IWASAKI char *env; 319d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 320d786139cSMaxime Henrion char *debugpoint; 32115e32d5dSMike Smith #endif 322fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 32315e32d5dSMike Smith 324b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 325cb9b0d80SMike Smith ACPI_LOCK; 32615e32d5dSMike Smith sc = device_get_softc(dev); 32715e32d5dSMike Smith bzero(sc, sizeof(*sc)); 32815e32d5dSMike Smith sc->acpi_dev = dev; 32915e32d5dSMike Smith 330d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 331d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 332d786139cSMaxime Henrion if (debugpoint) { 333d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 33415e32d5dSMike Smith acpi_EnterDebugger(); 335d786139cSMaxime Henrion freeenv(debugpoint); 336d786139cSMaxime Henrion } 33715e32d5dSMike Smith #endif 33815e32d5dSMike Smith 33915e32d5dSMike Smith /* 34015e32d5dSMike Smith * Install the default address space handlers. 34115e32d5dSMike Smith */ 342cb9b0d80SMike Smith error = ENXIO; 343b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3442a4ac806SMike Smith ACPI_ADR_SPACE_SYSTEM_MEMORY, 34515e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 346b53f2771SMike Smith NULL, NULL))) { 347bfae45aaSMike Smith device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status)); 348cb9b0d80SMike Smith goto out; 34915e32d5dSMike Smith } 350b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3512a4ac806SMike Smith ACPI_ADR_SPACE_SYSTEM_IO, 35215e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 353b53f2771SMike Smith NULL, NULL))) { 354bfae45aaSMike Smith device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status)); 355cb9b0d80SMike Smith goto out; 35615e32d5dSMike Smith } 357b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3582a4ac806SMike Smith ACPI_ADR_SPACE_PCI_CONFIG, 35915e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 360b53f2771SMike Smith NULL, NULL))) { 361bfae45aaSMike Smith device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status)); 362cb9b0d80SMike Smith goto out; 36315e32d5dSMike Smith } 36415e32d5dSMike Smith 36515e32d5dSMike Smith /* 36615e32d5dSMike Smith * Bring ACPI fully online. 36715e32d5dSMike Smith * 36832d18aa5SMike Smith * Note that some systems (specifically, those with namespace evaluation issues 36932d18aa5SMike Smith * that require the avoidance of parts of the namespace) must avoid running _INI 37032d18aa5SMike Smith * and _STA on everything, as well as dodging the final object init pass. 37115e32d5dSMike Smith * 37232d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 37332d18aa5SMike Smith * 37432d18aa5SMike Smith * XXX We should arrange for the object init pass after we have attached all our 37532d18aa5SMike Smith * child devices, but on many systems it works here. 37615e32d5dSMike Smith */ 377d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 378d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 379d786139cSMaxime Henrion if (debugpoint) { 380d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 38115e32d5dSMike Smith acpi_EnterDebugger(); 382d786139cSMaxime Henrion freeenv(debugpoint); 383d786139cSMaxime Henrion } 38415e32d5dSMike Smith #endif 38532d18aa5SMike Smith flags = 0; 386d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 38732d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 388b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 389bfae45aaSMike Smith device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status)); 390cb9b0d80SMike Smith goto out; 39115e32d5dSMike Smith } 39215e32d5dSMike Smith 393b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 394b69ed3f4SMitsuru IWASAKI device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status)); 395b69ed3f4SMitsuru IWASAKI goto out; 396b69ed3f4SMitsuru IWASAKI } 397b69ed3f4SMitsuru IWASAKI 39815e32d5dSMike Smith /* 3991d073b1dSJohn Baldwin * Setup our sysctl tree. 4001d073b1dSJohn Baldwin * 4011d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4021d073b1dSJohn Baldwin */ 4031d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4041d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4051d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4061d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 4071d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4081d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4091d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4101d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4111d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4121d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4131d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4141d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4151d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 416f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 417f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 418f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 419f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 420f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 421f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4222d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 423ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 424ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 425ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4261611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 4271611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 4281611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4292d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 4302d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 431c6a78e98STakanori Watanabe SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 432c6a78e98STakanori Watanabe OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 433c6a78e98STakanori Watanabe &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 434c6a78e98STakanori Watanabe sc->acpi_disable_on_poweroff = 1; 435f3a6cbb6SMitsuru IWASAKI sc->acpi_sleep_delay = 0; 436931a10c9SMitsuru IWASAKI sc->acpi_s4bios = 1; 4372d644607SMitsuru IWASAKI if (bootverbose) 4382d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 439498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 440498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 441498d464fSMitsuru IWASAKI freeenv(env); 442498d464fSMitsuru IWASAKI } 4431d073b1dSJohn Baldwin 4441d073b1dSJohn Baldwin /* 44515e32d5dSMike Smith * Dispatch the default sleep state to devices. 44615e32d5dSMike Smith * TBD: should be configured from userland policy manager. 44715e32d5dSMike Smith */ 44815e32d5dSMike Smith sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 44915e32d5dSMike Smith sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 45015e32d5dSMike Smith sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 451f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 452f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 45315e32d5dSMike Smith 45413d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 45515e32d5dSMike Smith 45615e32d5dSMike Smith /* 45715e32d5dSMike Smith * Scan the namespace and attach/initialise children. 45815e32d5dSMike Smith */ 459d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 460d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 461d786139cSMaxime Henrion if (debugpoint) { 462d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 46315e32d5dSMike Smith acpi_EnterDebugger(); 464d786139cSMaxime Henrion freeenv(debugpoint); 465d786139cSMaxime Henrion } 46615e32d5dSMike Smith #endif 46715e32d5dSMike Smith 46815e32d5dSMike Smith /* 46915e32d5dSMike Smith * Register our shutdown handlers 47015e32d5dSMike Smith */ 47115e32d5dSMike Smith EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 47215e32d5dSMike Smith EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 47315e32d5dSMike Smith 47415e32d5dSMike Smith /* 47515e32d5dSMike Smith * Register our acpi event handlers. 47615e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 47715e32d5dSMike Smith */ 47815e32d5dSMike Smith EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 47915e32d5dSMike Smith EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 48015e32d5dSMike Smith 48115e32d5dSMike Smith /* 48215e32d5dSMike Smith * Flag our initial states. 48315e32d5dSMike Smith */ 48415e32d5dSMike Smith sc->acpi_enabled = 1; 48515e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 486ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 48715e32d5dSMike Smith 48815e32d5dSMike Smith /* 48915e32d5dSMike Smith * Create the control device 49015e32d5dSMike Smith */ 491a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 4922a4689e8SRobert Watson "acpi"); 49315e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 49415e32d5dSMike Smith 495d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 496d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 497d786139cSMaxime Henrion if (debugpoint) { 498d786139cSMaxime Henrion if (!strcmp(debugpoint, "running")) 49915e32d5dSMike Smith acpi_EnterDebugger(); 500d786139cSMaxime Henrion freeenv(debugpoint); 501d786139cSMaxime Henrion } 50215e32d5dSMike Smith #endif 503f86214b6SMitsuru IWASAKI 50491da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 505c573e654SMitsuru IWASAKI if ((error = acpi_task_thread_init())) { 506c573e654SMitsuru IWASAKI goto out; 507c573e654SMitsuru IWASAKI } 508c573e654SMitsuru IWASAKI #endif 509c573e654SMitsuru IWASAKI 510f86214b6SMitsuru IWASAKI if ((error = acpi_machdep_init(dev))) { 511f86214b6SMitsuru IWASAKI goto out; 512f86214b6SMitsuru IWASAKI } 513f86214b6SMitsuru IWASAKI 514f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 515f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 516f9390180SMitsuru IWASAKI 517eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 518eeb6dba2SJohn Baldwin acpi_probe_children(dev); 519eeb6dba2SJohn Baldwin 520cb9b0d80SMike Smith error = 0; 521cb9b0d80SMike Smith 522cb9b0d80SMike Smith out: 523cb9b0d80SMike Smith ACPI_UNLOCK; 524cb9b0d80SMike Smith return_VALUE(error); 52515e32d5dSMike Smith } 52615e32d5dSMike Smith 52715e32d5dSMike Smith /* 52815e32d5dSMike Smith * Handle a new device being added 52915e32d5dSMike Smith */ 53015e32d5dSMike Smith static device_t 53115e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 53215e32d5dSMike Smith { 53315e32d5dSMike Smith struct acpi_device *ad; 53415e32d5dSMike Smith device_t child; 53515e32d5dSMike Smith 53615e32d5dSMike Smith if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 53715e32d5dSMike Smith return(NULL); 53815e32d5dSMike Smith bzero(ad, sizeof(*ad)); 53915e32d5dSMike Smith 54015e32d5dSMike Smith resource_list_init(&ad->ad_rl); 54115e32d5dSMike Smith 54215e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 54315e32d5dSMike Smith if (child != NULL) 54415e32d5dSMike Smith device_set_ivars(child, ad); 54515e32d5dSMike Smith return(child); 54615e32d5dSMike Smith } 54715e32d5dSMike Smith 54815e32d5dSMike Smith static int 54915e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 55015e32d5dSMike Smith { 55115e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 55215e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 55315e32d5dSMike Smith int retval = 0; 55415e32d5dSMike Smith 55515e32d5dSMike Smith retval += bus_print_child_header(bus, child); 5569ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 5579ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 5589ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 5599ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 56015e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 56115e32d5dSMike Smith 56215e32d5dSMike Smith return(retval); 56315e32d5dSMike Smith } 56415e32d5dSMike Smith 56515e32d5dSMike Smith 56615e32d5dSMike Smith /* 56715e32d5dSMike Smith * Handle per-device ivars 56815e32d5dSMike Smith */ 56915e32d5dSMike Smith static int 57015e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 57115e32d5dSMike Smith { 57215e32d5dSMike Smith struct acpi_device *ad; 57315e32d5dSMike Smith 57415e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 57515e32d5dSMike Smith printf("device has no ivars\n"); 57615e32d5dSMike Smith return(ENOENT); 57715e32d5dSMike Smith } 57815e32d5dSMike Smith 57915e32d5dSMike Smith switch(index) { 58015e32d5dSMike Smith /* ACPI ivars */ 58115e32d5dSMike Smith case ACPI_IVAR_HANDLE: 58215e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 58315e32d5dSMike Smith break; 58415e32d5dSMike Smith case ACPI_IVAR_MAGIC: 58515e32d5dSMike Smith *(int *)result = ad->ad_magic; 58615e32d5dSMike Smith break; 58715e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 58815e32d5dSMike Smith *(void **)result = ad->ad_private; 58915e32d5dSMike Smith break; 59015e32d5dSMike Smith 59132d18aa5SMike Smith /* ISA compatibility */ 59232d18aa5SMike Smith case ISA_IVAR_VENDORID: 59332d18aa5SMike Smith case ISA_IVAR_SERIAL: 59432d18aa5SMike Smith case ISA_IVAR_COMPATID: 59532d18aa5SMike Smith *(int *)result = -1; 59632d18aa5SMike Smith break; 59732d18aa5SMike Smith 59832d18aa5SMike Smith case ISA_IVAR_LOGICALID: 59932d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 60032d18aa5SMike Smith break; 60132d18aa5SMike Smith 60215e32d5dSMike Smith default: 60315e32d5dSMike Smith return(ENOENT); 60415e32d5dSMike Smith } 60515e32d5dSMike Smith return(0); 60615e32d5dSMike Smith } 60715e32d5dSMike Smith 60815e32d5dSMike Smith static int 60915e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 61015e32d5dSMike Smith { 61115e32d5dSMike Smith struct acpi_device *ad; 61215e32d5dSMike Smith 61315e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 61415e32d5dSMike Smith printf("device has no ivars\n"); 61515e32d5dSMike Smith return(ENOENT); 61615e32d5dSMike Smith } 61715e32d5dSMike Smith 61815e32d5dSMike Smith switch(index) { 61915e32d5dSMike Smith /* ACPI ivars */ 62015e32d5dSMike Smith case ACPI_IVAR_HANDLE: 62115e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 62215e32d5dSMike Smith break; 62315e32d5dSMike Smith case ACPI_IVAR_MAGIC: 62415e32d5dSMike Smith ad->ad_magic = (int )value; 62515e32d5dSMike Smith break; 62615e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 62715e32d5dSMike Smith ad->ad_private = (void *)value; 62815e32d5dSMike Smith break; 62915e32d5dSMike Smith 63015e32d5dSMike Smith default: 6312ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 63215e32d5dSMike Smith return(ENOENT); 63315e32d5dSMike Smith } 63415e32d5dSMike Smith return(0); 63515e32d5dSMike Smith } 63615e32d5dSMike Smith 63715e32d5dSMike Smith /* 63815e32d5dSMike Smith * Handle child resource allocation/removal 63915e32d5dSMike Smith */ 64015e32d5dSMike Smith static int 64115e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 64215e32d5dSMike Smith { 64315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 64415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 64515e32d5dSMike Smith 64615e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 64715e32d5dSMike Smith 64815e32d5dSMike Smith return(0); 64915e32d5dSMike Smith } 65015e32d5dSMike Smith 65115e32d5dSMike Smith static int 65215e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 65315e32d5dSMike Smith { 65415e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 65515e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 65615e32d5dSMike Smith struct resource_list_entry *rle; 65715e32d5dSMike Smith 65815e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 65915e32d5dSMike Smith if (!rle) 66015e32d5dSMike Smith return(ENOENT); 66115e32d5dSMike Smith 66215e32d5dSMike Smith if (startp) 66315e32d5dSMike Smith *startp = rle->start; 66415e32d5dSMike Smith if (countp) 66515e32d5dSMike Smith *countp = rle->count; 66615e32d5dSMike Smith 66715e32d5dSMike Smith return(0); 66815e32d5dSMike Smith } 66915e32d5dSMike Smith 67015e32d5dSMike Smith static struct resource * 67115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 67215e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 67315e32d5dSMike Smith { 67415e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 67515e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 67615e32d5dSMike Smith 67715e32d5dSMike Smith return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 67815e32d5dSMike Smith } 67915e32d5dSMike Smith 68015e32d5dSMike Smith static int 68115e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 68215e32d5dSMike Smith { 68315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 68415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 68515e32d5dSMike Smith 68615e32d5dSMike Smith return(resource_list_release(rl, bus, child, type, rid, r)); 68715e32d5dSMike Smith } 68815e32d5dSMike Smith 68915e32d5dSMike Smith /* 690bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 691bc0f2195SMike Smith */ 692bc0f2195SMike Smith #define PNP_EISAID(s) \ 693bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 694bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 695bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 696bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 697bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 698bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 699bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 700bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 701bc0f2195SMike Smith 70232d18aa5SMike Smith static u_int32_t 70332d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 704bc0f2195SMike Smith { 705bc0f2195SMike Smith ACPI_HANDLE h; 706bc0f2195SMike Smith ACPI_DEVICE_INFO devinfo; 707bc0f2195SMike Smith ACPI_STATUS error; 708bc0f2195SMike Smith u_int32_t pnpid; 709fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 71032d18aa5SMike Smith 711b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 71232d18aa5SMike Smith 71332d18aa5SMike Smith pnpid = 0; 71432d18aa5SMike Smith ACPI_LOCK; 71532d18aa5SMike Smith 71632d18aa5SMike Smith /* fetch and validate the HID */ 71732d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 71832d18aa5SMike Smith goto out; 719b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 72032d18aa5SMike Smith goto out; 72132d18aa5SMike Smith if (!(devinfo.Valid & ACPI_VALID_HID)) 72232d18aa5SMike Smith goto out; 72332d18aa5SMike Smith 72432d18aa5SMike Smith pnpid = PNP_EISAID(devinfo.HardwareId); 72532d18aa5SMike Smith out: 72632d18aa5SMike Smith ACPI_UNLOCK; 72732d18aa5SMike Smith return_VALUE(pnpid); 72832d18aa5SMike Smith } 72932d18aa5SMike Smith 73032d18aa5SMike Smith static int 73132d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 73232d18aa5SMike Smith { 733bc0f2195SMike Smith int result; 73432d18aa5SMike Smith u_int32_t pnpid; 735bc0f2195SMike Smith 736b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 737bc0f2195SMike Smith 738bc0f2195SMike Smith /* 739bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 740bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 741bc0f2195SMike Smith * that to happen, so don't ever return it. 742bc0f2195SMike Smith */ 743bc0f2195SMike Smith result = ENXIO; 744bc0f2195SMike Smith 745bc0f2195SMike Smith /* scan the supplied IDs for a match */ 74632d18aa5SMike Smith pnpid = acpi_isa_get_logicalid(child); 747bc0f2195SMike Smith while (ids && ids->ip_id) { 748bc0f2195SMike Smith if (pnpid == ids->ip_id) { 749bc0f2195SMike Smith result = 0; 750bc0f2195SMike Smith goto out; 751bc0f2195SMike Smith } 752bc0f2195SMike Smith ids++; 753bc0f2195SMike Smith } 754bc0f2195SMike Smith out: 755bc0f2195SMike Smith return_VALUE(result); 756bc0f2195SMike Smith } 757bc0f2195SMike Smith 758bc0f2195SMike Smith /* 75915e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 76015e32d5dSMike Smith * 7617d3bcec9SMike Smith * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 7627d3bcec9SMike Smith * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 76315e32d5dSMike Smith */ 76415e32d5dSMike Smith static void 76515e32d5dSMike Smith acpi_probe_children(device_t bus) 76615e32d5dSMike Smith { 76715e32d5dSMike Smith ACPI_HANDLE parent; 7687d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 76915e32d5dSMike Smith int i; 77015e32d5dSMike Smith 771b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 772cb9b0d80SMike Smith ACPI_ASSERTLOCK; 7730ae55423SMike Smith 77415e32d5dSMike Smith /* 77515e32d5dSMike Smith * Create any static children by calling device identify methods. 77615e32d5dSMike Smith */ 7774c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 77815e32d5dSMike Smith bus_generic_probe(bus); 77915e32d5dSMike Smith 78015e32d5dSMike Smith /* 78115e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 78215e32d5dSMike Smith * we find. 78315e32d5dSMike Smith * 78415e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 78515e32d5dSMike Smith * we want to create nodes for all devices, not just those that are currently 78615e32d5dSMike Smith * present. (This assumes that we don't want to create/remove devices as they 78715e32d5dSMike Smith * appear, which might be smarter.) 78815e32d5dSMike Smith */ 7894c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 79015e32d5dSMike Smith for (i = 0; scopes[i] != NULL; i++) 791b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent))) 79215e32d5dSMike Smith AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 79315e32d5dSMike Smith 79415e32d5dSMike Smith /* 79515e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 79615e32d5dSMike Smith */ 7974c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 79815e32d5dSMike Smith bus_generic_attach(bus); 79915e32d5dSMike Smith 80015e32d5dSMike Smith /* 80115e32d5dSMike Smith * Some of these children may have attached others as part of their attach 80215e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 80315e32d5dSMike Smith */ 8044c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 80515e32d5dSMike Smith bus_generic_attach(bus); 8060ae55423SMike Smith 807bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 8080ae55423SMike Smith return_VOID; 80915e32d5dSMike Smith } 81015e32d5dSMike Smith 81115e32d5dSMike Smith /* 81215e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 81315e32d5dSMike Smith * it. 81415e32d5dSMike Smith */ 81515e32d5dSMike Smith static ACPI_STATUS 81615e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 81715e32d5dSMike Smith { 81815e32d5dSMike Smith ACPI_OBJECT_TYPE type; 81915e32d5dSMike Smith device_t child, bus = (device_t)context; 82015e32d5dSMike Smith 821b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 8220ae55423SMike Smith 8230ae55423SMike Smith /* 8240ae55423SMike Smith * Skip this device if we think we'll have trouble with it. 8250ae55423SMike Smith */ 8260ae55423SMike Smith if (acpi_avoid(handle)) 8270ae55423SMike Smith return_ACPI_STATUS(AE_OK); 8280ae55423SMike Smith 829b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 83015e32d5dSMike Smith switch(type) { 83115e32d5dSMike Smith case ACPI_TYPE_DEVICE: 83215e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 83315e32d5dSMike Smith case ACPI_TYPE_THERMAL: 83415e32d5dSMike Smith case ACPI_TYPE_POWER: 8350ae55423SMike Smith if (acpi_disabled("children")) 8360ae55423SMike Smith break; 83715e32d5dSMike Smith /* 83815e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 83915e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 84015e32d5dSMike Smith */ 8414c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle))); 84215e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 843bc0f2195SMike Smith if (child == NULL) 844bc0f2195SMike Smith break; 84515e32d5dSMike Smith acpi_set_handle(child, handle); 846bc0f2195SMike Smith 847bc0f2195SMike Smith /* 848b519f9d6SMike Smith * Check that the device is present. If it's not present, 849b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 850b519f9d6SMike Smith * the handle, but we don't probe it). 851b519f9d6SMike Smith */ 85223b4e251SMitsuru IWASAKI if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) { 853b519f9d6SMike Smith device_disable(child); 854b519f9d6SMike Smith break; 855b519f9d6SMike Smith } 856b519f9d6SMike Smith 857b519f9d6SMike Smith /* 858bc0f2195SMike Smith * Get the device's resource settings and attach them. 859bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 860bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 861bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 862bc0f2195SMike Smith * device not to have any resources. 863bc0f2195SMike Smith */ 864bc0f2195SMike Smith acpi_parse_resources(child, handle, &acpi_res_parse_set); 865bc0f2195SMike Smith 866bc0f2195SMike Smith /* if we're debugging, probe/attach now rather than later */ 867b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 8680ae55423SMike Smith break; 86915e32d5dSMike Smith } 87015e32d5dSMike Smith } 8710ae55423SMike Smith return_ACPI_STATUS(AE_OK); 87215e32d5dSMike Smith } 87315e32d5dSMike Smith 87415e32d5dSMike Smith static void 87515e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 87615e32d5dSMike Smith { 877cb9b0d80SMike Smith 878c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 879c6a78e98STakanori Watanabe 880cb9b0d80SMike Smith ACPI_ASSERTLOCK; 881cb9b0d80SMike Smith 88215e32d5dSMike Smith /* 8830ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 88415e32d5dSMike Smith * will be turned on again on some laptops. 88515e32d5dSMike Smith * 88615e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 88715e32d5dSMike Smith * before powering down, since we may still need ACPI during the 88815e32d5dSMike Smith * shutdown process. 88915e32d5dSMike Smith */ 890c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 891c6a78e98STakanori Watanabe acpi_Disable(sc); 89215e32d5dSMike Smith } 89315e32d5dSMike Smith 89415e32d5dSMike Smith static void 89515e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 89615e32d5dSMike Smith { 89715e32d5dSMike Smith ACPI_STATUS status; 89815e32d5dSMike Smith 899cb9b0d80SMike Smith ACPI_ASSERTLOCK; 900cb9b0d80SMike Smith 9015f3e4175SMitsuru IWASAKI if (howto & RB_POWEROFF) { 90215e32d5dSMike Smith printf("Power system off using ACPI...\n"); 903b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) { 904b9c780d6SMitsuru IWASAKI printf("AcpiEnterSleepStatePrep failed - %s\n", 905b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 906b9c780d6SMitsuru IWASAKI return; 907b9c780d6SMitsuru IWASAKI } 908b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) { 909bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 91015e32d5dSMike Smith } else { 91115e32d5dSMike Smith DELAY(1000000); 91215e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 91315e32d5dSMike Smith } 914494ae560SMitsuru IWASAKI } else { 915494ae560SMitsuru IWASAKI printf("Terminate ACPI\n"); 916494ae560SMitsuru IWASAKI AcpiTerminate(); 91715e32d5dSMike Smith } 91815e32d5dSMike Smith } 91915e32d5dSMike Smith 92013d4f7baSMitsuru IWASAKI static void 92113d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 92213d4f7baSMitsuru IWASAKI { 92313d4f7baSMitsuru IWASAKI static int first_time = 1; 92413d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 92513d4f7baSMitsuru IWASAKI 926cb9b0d80SMike Smith ACPI_ASSERTLOCK; 927cb9b0d80SMike Smith 92813d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 929cb9b0d80SMike Smith if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 93043896e91SMike Smith AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0); 93113d4f7baSMitsuru IWASAKI AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 93213d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 93313d4f7baSMitsuru IWASAKI acpi_eventhandler_power_button_for_sleep, sc); 93413d4f7baSMitsuru IWASAKI if (first_time) { 93513d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "power"); 93613d4f7baSMitsuru IWASAKI } 93713d4f7baSMitsuru IWASAKI } 938cb9b0d80SMike Smith if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 93943896e91SMike Smith AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0); 94013d4f7baSMitsuru IWASAKI AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 94113d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 94213d4f7baSMitsuru IWASAKI acpi_eventhandler_sleep_button_for_sleep, sc); 94313d4f7baSMitsuru IWASAKI if (first_time) { 94413d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 94513d4f7baSMitsuru IWASAKI } 94613d4f7baSMitsuru IWASAKI } 94713d4f7baSMitsuru IWASAKI 94813d4f7baSMitsuru IWASAKI first_time = 0; 94913d4f7baSMitsuru IWASAKI } 95013d4f7baSMitsuru IWASAKI 95115e32d5dSMike Smith /* 952c5ba0be4SMike Smith * Returns true if the device is actually present and should 953c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 954c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 955c5ba0be4SMike Smith */ 956c5ba0be4SMike Smith BOOLEAN 957c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 958c5ba0be4SMike Smith { 959c5ba0be4SMike Smith ACPI_HANDLE h; 960c5ba0be4SMike Smith ACPI_DEVICE_INFO devinfo; 961c5ba0be4SMike Smith ACPI_STATUS error; 962c5ba0be4SMike Smith 963cb9b0d80SMike Smith ACPI_ASSERTLOCK; 964cb9b0d80SMike Smith 965c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 966c5ba0be4SMike Smith return(FALSE); 967b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 968c5ba0be4SMike Smith return(FALSE); 96943896e91SMike Smith /* if no _STA method, must be present */ 97043896e91SMike Smith if (!(devinfo.Valid & ACPI_VALID_STA)) 97143896e91SMike Smith return(TRUE); 97243896e91SMike Smith /* return true for 'present' and 'functioning' */ 97343896e91SMike Smith if ((devinfo.CurrentStatus & 0x9) == 0x9) 974c5ba0be4SMike Smith return(TRUE); 975c5ba0be4SMike Smith return(FALSE); 976c5ba0be4SMike Smith } 977c5ba0be4SMike Smith 978c5ba0be4SMike Smith /* 979b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 980b53f2771SMike Smith */ 981b53f2771SMike Smith BOOLEAN 982b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 983b53f2771SMike Smith { 984b53f2771SMike Smith ACPI_HANDLE h; 985b53f2771SMike Smith ACPI_DEVICE_INFO devinfo; 986b53f2771SMike Smith ACPI_STATUS error; 987b53f2771SMike Smith 988b53f2771SMike Smith ACPI_ASSERTLOCK; 989b53f2771SMike Smith 990b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 991b53f2771SMike Smith return(FALSE); 992b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 993b53f2771SMike Smith return(FALSE); 994b53f2771SMike Smith /* if no _STA method, must be present */ 995b53f2771SMike Smith if (!(devinfo.Valid & ACPI_VALID_STA)) 996b53f2771SMike Smith return(TRUE); 997b53f2771SMike Smith /* return true for 'present' and 'functioning' */ 998b53f2771SMike Smith if ((devinfo.CurrentStatus & 0x19) == 0x19) 999b53f2771SMike Smith return(TRUE); 1000b53f2771SMike Smith return(FALSE); 1001b53f2771SMike Smith } 1002b53f2771SMike Smith 1003b53f2771SMike Smith /* 100415e32d5dSMike Smith * Match a HID string against a device 100515e32d5dSMike Smith */ 100615e32d5dSMike Smith BOOLEAN 100715e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 100815e32d5dSMike Smith { 100915e32d5dSMike Smith ACPI_HANDLE h; 101015e32d5dSMike Smith ACPI_DEVICE_INFO devinfo; 101115e32d5dSMike Smith ACPI_STATUS error; 1012ed136da6SDoug Rabson int cid; 101315e32d5dSMike Smith 1014cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1015cb9b0d80SMike Smith 10164d332989SMike Smith if (hid == NULL) 101715e32d5dSMike Smith return(FALSE); 101815e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 101915e32d5dSMike Smith return(FALSE); 1020b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 102115e32d5dSMike Smith return(FALSE); 10224d332989SMike Smith if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 102315e32d5dSMike Smith return(TRUE); 1024b53f2771SMike Smith if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1025ed136da6SDoug Rabson return(FALSE); 1026ed136da6SDoug Rabson if (cid == PNP_EISAID(hid)) 1027ed136da6SDoug Rabson return(TRUE); 102815e32d5dSMike Smith return(FALSE); 102915e32d5dSMike Smith } 103015e32d5dSMike Smith 103115e32d5dSMike Smith /* 1032c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1033c5ba0be4SMike Smith * or one if its parents. 1034c5ba0be4SMike Smith */ 1035c5ba0be4SMike Smith ACPI_STATUS 1036c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1037c5ba0be4SMike Smith { 1038c5ba0be4SMike Smith ACPI_HANDLE r; 1039c5ba0be4SMike Smith ACPI_STATUS status; 1040c5ba0be4SMike Smith 1041cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1042cb9b0d80SMike Smith 1043c5ba0be4SMike Smith /* walk back up the tree to the root */ 1044c5ba0be4SMike Smith for (;;) { 1045b53f2771SMike Smith if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) { 1046c5ba0be4SMike Smith *result = r; 1047c5ba0be4SMike Smith return(AE_OK); 1048c5ba0be4SMike Smith } 1049c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1050c5ba0be4SMike Smith return(AE_OK); 1051b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1052c5ba0be4SMike Smith return(AE_NOT_FOUND); 1053c5ba0be4SMike Smith parent = r; 1054c5ba0be4SMike Smith } 1055c5ba0be4SMike Smith } 1056c5ba0be4SMike Smith 1057c5ba0be4SMike Smith /* 1058c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1059c5ba0be4SMike Smith */ 1060c5ba0be4SMike Smith ACPI_BUFFER * 1061c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1062c5ba0be4SMike Smith { 1063c5ba0be4SMike Smith ACPI_BUFFER *buf; 1064c5ba0be4SMike Smith 1065c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1066c5ba0be4SMike Smith return(NULL); 1067c5ba0be4SMike Smith buf->Length = size; 1068c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1069c5ba0be4SMike Smith return(buf); 1070c5ba0be4SMike Smith } 1071c5ba0be4SMike Smith 1072c5ba0be4SMike Smith /* 1073c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1074c5ba0be4SMike Smith */ 1075c5ba0be4SMike Smith ACPI_STATUS 1076c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1077c5ba0be4SMike Smith { 1078c5ba0be4SMike Smith ACPI_STATUS error; 1079c5ba0be4SMike Smith ACPI_BUFFER buf; 1080c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1081c5ba0be4SMike Smith 1082cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1083cb9b0d80SMike Smith 1084c5ba0be4SMike Smith if (handle == NULL) 1085c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 10860c7da7acSJohn Baldwin 10870c7da7acSJohn Baldwin /* 10880c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 10890c7da7acSJohn Baldwin * a method that will return an Integer. 10900c7da7acSJohn Baldwin */ 1091c5ba0be4SMike Smith buf.Pointer = ¶m; 1092c5ba0be4SMike Smith buf.Length = sizeof(param); 1093b53f2771SMike Smith if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1094c5ba0be4SMike Smith if (param.Type == ACPI_TYPE_INTEGER) { 1095c5ba0be4SMike Smith *number = param.Integer.Value; 1096c5ba0be4SMike Smith } else { 1097c5ba0be4SMike Smith error = AE_TYPE; 1098c5ba0be4SMike Smith } 1099c5ba0be4SMike Smith } 11000c7da7acSJohn Baldwin 11010c7da7acSJohn Baldwin /* 11020c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 11030c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 11040c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 11050c7da7acSJohn Baldwin * convert it into an Integer as best we can. 11060c7da7acSJohn Baldwin * 11070c7da7acSJohn Baldwin * This is a hack. 11080c7da7acSJohn Baldwin */ 11090c7da7acSJohn Baldwin if (error == AE_BUFFER_OVERFLOW) { 1110b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 11110c7da7acSJohn Baldwin error = AE_NO_MEMORY; 11120c7da7acSJohn Baldwin } else { 1113b53f2771SMike Smith if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1114c573e654SMitsuru IWASAKI error = acpi_ConvertBufferToInteger(&buf, number); 11150c7da7acSJohn Baldwin } 11160c7da7acSJohn Baldwin } 11170c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 11180c7da7acSJohn Baldwin } 1119c5ba0be4SMike Smith return(error); 1120c5ba0be4SMike Smith } 1121c5ba0be4SMike Smith 1122c573e654SMitsuru IWASAKI ACPI_STATUS 1123c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1124c573e654SMitsuru IWASAKI { 1125c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1126c573e654SMitsuru IWASAKI int i; 1127c573e654SMitsuru IWASAKI 1128c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1129c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1130c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1131c573e654SMitsuru IWASAKI return(AE_OK); 1132c573e654SMitsuru IWASAKI } 1133c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1134c573e654SMitsuru IWASAKI return(AE_TYPE); 1135c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1136c573e654SMitsuru IWASAKI return(AE_BAD_DATA); 1137c573e654SMitsuru IWASAKI *number = 0; 1138c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1139c573e654SMitsuru IWASAKI *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1140c573e654SMitsuru IWASAKI return(AE_OK); 1141c573e654SMitsuru IWASAKI } 1142c573e654SMitsuru IWASAKI 1143c5ba0be4SMike Smith /* 1144c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1145c5ba0be4SMike Smith * function for each element. 1146c5ba0be4SMike Smith * 1147c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1148c5ba0be4SMike Smith */ 1149c5ba0be4SMike Smith ACPI_STATUS 1150c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 1151c5ba0be4SMike Smith { 1152c5ba0be4SMike Smith ACPI_OBJECT *comp; 1153c5ba0be4SMike Smith int i; 1154c5ba0be4SMike Smith 1155c5ba0be4SMike Smith if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 1156c5ba0be4SMike Smith return(AE_BAD_PARAMETER); 1157c5ba0be4SMike Smith 1158c5ba0be4SMike Smith /* iterate over components */ 1159c5ba0be4SMike Smith for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 1160c5ba0be4SMike Smith func(comp, arg); 1161c5ba0be4SMike Smith 1162c5ba0be4SMike Smith return(AE_OK); 1163c5ba0be4SMike Smith } 1164c5ba0be4SMike Smith 11656f69255bSMike Smith /* 11666f69255bSMike Smith * Find the (index)th resource object in a set. 11676f69255bSMike Smith */ 11686f69255bSMike Smith ACPI_STATUS 11696d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 11706f69255bSMike Smith { 11716d63101aSMike Smith ACPI_RESOURCE *rp; 11726f69255bSMike Smith int i; 11736f69255bSMike Smith 11746d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 11756f69255bSMike Smith i = index; 11766d63101aSMike Smith while (i-- > 0) { 11776f69255bSMike Smith /* range check */ 11786d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 11796f69255bSMike Smith return(AE_BAD_PARAMETER); 11806d63101aSMike Smith /* check for terminator */ 11816d63101aSMike Smith if ((rp->Id == ACPI_RSTYPE_END_TAG) || 11826d63101aSMike Smith (rp->Length == 0)) 11836d63101aSMike Smith return(AE_NOT_FOUND); 11846d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 11856f69255bSMike Smith } 11866f69255bSMike Smith if (resp != NULL) 11876d63101aSMike Smith *resp = rp; 11886d63101aSMike Smith return(AE_OK); 11896d63101aSMike Smith } 11906d63101aSMike Smith 11916d63101aSMike Smith /* 11926d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 11936d63101aSMike Smith * 11946d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 11956d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 11966d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 11976d63101aSMike Smith * resources. 11986d63101aSMike Smith */ 11996d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 12006d63101aSMike Smith 12016d63101aSMike Smith ACPI_STATUS 12026d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 12036d63101aSMike Smith { 12046d63101aSMike Smith ACPI_RESOURCE *rp; 12056d63101aSMike Smith void *newp; 12066d63101aSMike Smith 12076d63101aSMike Smith /* 12086d63101aSMike Smith * Initialise the buffer if necessary. 12096d63101aSMike Smith */ 12106d63101aSMike Smith if (buf->Pointer == NULL) { 12116d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 12126d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 12136d63101aSMike Smith return(AE_NO_MEMORY); 12146d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 12156d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 12166d63101aSMike Smith rp->Length = 0; 12176d63101aSMike Smith } 12186d63101aSMike Smith if (res == NULL) 12196d63101aSMike Smith return(AE_OK); 12206d63101aSMike Smith 12216d63101aSMike Smith /* 12226d63101aSMike Smith * Scan the current buffer looking for the terminator. 12236d63101aSMike Smith * This will either find the terminator or hit the end 12246d63101aSMike Smith * of the buffer and return an error. 12256d63101aSMike Smith */ 12266d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 12276d63101aSMike Smith for (;;) { 12286d63101aSMike Smith /* range check, don't go outside the buffer */ 12296d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 12306d63101aSMike Smith return(AE_BAD_PARAMETER); 12316d63101aSMike Smith if ((rp->Id == ACPI_RSTYPE_END_TAG) || 12326d63101aSMike Smith (rp->Length == 0)) { 12336d63101aSMike Smith break; 12346d63101aSMike Smith } 12356d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 12366d63101aSMike Smith } 12376d63101aSMike Smith 12386d63101aSMike Smith /* 12396d63101aSMike Smith * Check the size of the buffer and expand if required. 12406d63101aSMike Smith * 12416d63101aSMike Smith * Required size is: 12426d63101aSMike Smith * size of existing resources before terminator + 12436d63101aSMike Smith * size of new resource and header + 12446d63101aSMike Smith * size of terminator. 12456d63101aSMike Smith * 12466d63101aSMike Smith * Note that this loop should really only run once, unless 12476d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 12486d63101aSMike Smith */ 12496d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 12506d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 12516d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 12526d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 12536d63101aSMike Smith return(AE_NO_MEMORY); 12546d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1255a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1256a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 12576d63101aSMike Smith AcpiOsFree(buf->Pointer); 12586d63101aSMike Smith buf->Pointer = newp; 12596d63101aSMike Smith buf->Length += buf->Length; 12606d63101aSMike Smith } 12616d63101aSMike Smith 12626d63101aSMike Smith /* 12636d63101aSMike Smith * Insert the new resource. 12646d63101aSMike Smith */ 12656d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 12666d63101aSMike Smith 12676d63101aSMike Smith /* 12686d63101aSMike Smith * And add the terminator. 12696d63101aSMike Smith */ 12706d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 12716d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 12726d63101aSMike Smith rp->Length = 0; 12736d63101aSMike Smith 12746f69255bSMike Smith return(AE_OK); 12756f69255bSMike Smith } 1276c5ba0be4SMike Smith 1277da14ac9fSJohn Baldwin /* 1278da14ac9fSJohn Baldwin * Set interrupt model. 1279da14ac9fSJohn Baldwin */ 1280da14ac9fSJohn Baldwin ACPI_STATUS 1281da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1282da14ac9fSJohn Baldwin { 1283da14ac9fSJohn Baldwin ACPI_OBJECT_LIST ArgList; 1284da14ac9fSJohn Baldwin ACPI_OBJECT Arg; 1285da14ac9fSJohn Baldwin 1286da14ac9fSJohn Baldwin Arg.Type = ACPI_TYPE_INTEGER; 1287da14ac9fSJohn Baldwin Arg.Integer.Value = model; 1288da14ac9fSJohn Baldwin ArgList.Count = 1; 1289da14ac9fSJohn Baldwin ArgList.Pointer = &Arg; 1290da14ac9fSJohn Baldwin return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1291da14ac9fSJohn Baldwin } 1292da14ac9fSJohn Baldwin 1293ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1294ece50487SMitsuru IWASAKI 1295ece50487SMitsuru IWASAKI static void 1296ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1297ece50487SMitsuru IWASAKI { 1298ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1299ece50487SMitsuru IWASAKI } 1300c30382dfSMitsuru IWASAKI 130115e32d5dSMike Smith /* 130215e32d5dSMike Smith * Set the system sleep state 130315e32d5dSMike Smith * 130415e32d5dSMike Smith * Currently we only support S1 and S5 130515e32d5dSMike Smith */ 130615e32d5dSMike Smith ACPI_STATUS 130715e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 130815e32d5dSMike Smith { 130915e32d5dSMike Smith ACPI_STATUS status = AE_OK; 131056d8cb57SMitsuru IWASAKI UINT8 TypeA; 131156d8cb57SMitsuru IWASAKI UINT8 TypeB; 131215e32d5dSMike Smith 1313b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1314cb9b0d80SMike Smith ACPI_ASSERTLOCK; 13150ae55423SMike Smith 13166397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 13176397624dSMitsuru IWASAKI return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */ 13186397624dSMitsuru IWASAKI 1319ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1320ece50487SMitsuru IWASAKI return_ACPI_STATUS(AE_OK); 1321ece50487SMitsuru IWASAKI 132215e32d5dSMike Smith switch (state) { 132315e32d5dSMike Smith case ACPI_STATE_S0: /* XXX only for testing */ 1324b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1325bfae45aaSMike Smith device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 132615e32d5dSMike Smith } 132715e32d5dSMike Smith break; 132815e32d5dSMike Smith 132915e32d5dSMike Smith case ACPI_STATE_S1: 13306161544cSTakanori Watanabe case ACPI_STATE_S2: 13316161544cSTakanori Watanabe case ACPI_STATE_S3: 13326161544cSTakanori Watanabe case ACPI_STATE_S4: 133398479b04SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) { 133498479b04SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status)); 133556d8cb57SMitsuru IWASAKI break; 133656d8cb57SMitsuru IWASAKI } 133756d8cb57SMitsuru IWASAKI 1338a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1339a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1340a1fccb47SMitsuru IWASAKI 134115e32d5dSMike Smith /* 134215e32d5dSMike Smith * Inform all devices that we are going to sleep. 134315e32d5dSMike Smith */ 134415e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 134515e32d5dSMike Smith /* 134615e32d5dSMike Smith * Re-wake the system. 134715e32d5dSMike Smith * 134815e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 134915e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 135015e32d5dSMike Smith * current bus interface does not provide for this. 135115e32d5dSMike Smith */ 135215e32d5dSMike Smith DEVICE_RESUME(root_bus); 13530ae55423SMike Smith return_ACPI_STATUS(AE_ERROR); 135415e32d5dSMike Smith } 1355b9c780d6SMitsuru IWASAKI 1356b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) { 1357b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1358b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1359b9c780d6SMitsuru IWASAKI break; 1360b9c780d6SMitsuru IWASAKI } 1361b9c780d6SMitsuru IWASAKI 1362ff01efb5SMitsuru IWASAKI if (sc->acpi_sleep_delay > 0) { 1363ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1364ff01efb5SMitsuru IWASAKI } 1365ff01efb5SMitsuru IWASAKI 13666161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 13676161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 13686161544cSTakanori Watanabe 1369a1fccb47SMitsuru IWASAKI /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */ 1370a1fccb47SMitsuru IWASAKI if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) { 13716161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1372a1fccb47SMitsuru IWASAKI } 13736161544cSTakanori Watanabe 13746161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1375964679ceSMitsuru IWASAKI if (state == ACPI_STATE_S4) { 1376964679ceSMitsuru IWASAKI AcpiEnable(); 13776161544cSTakanori Watanabe } 13786161544cSTakanori Watanabe } else { 1379b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1380bfae45aaSMike Smith device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1381c30382dfSMitsuru IWASAKI break; 138215e32d5dSMike Smith } 13836161544cSTakanori Watanabe } 1384ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 138515e32d5dSMike Smith DEVICE_RESUME(root_bus); 138615e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 138713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 138815e32d5dSMike Smith break; 138915e32d5dSMike Smith 139015e32d5dSMike Smith case ACPI_STATE_S5: 139115e32d5dSMike Smith /* 139215e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 139315e32d5dSMike Smith * shutdown handlers. 139415e32d5dSMike Smith */ 139515e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 139615e32d5dSMike Smith break; 139715e32d5dSMike Smith 139815e32d5dSMike Smith default: 139915e32d5dSMike Smith status = AE_BAD_PARAMETER; 140015e32d5dSMike Smith break; 140115e32d5dSMike Smith } 1402ece50487SMitsuru IWASAKI 1403ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1404ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1405ece50487SMitsuru IWASAKI 14060ae55423SMike Smith return_ACPI_STATUS(status); 140715e32d5dSMike Smith } 140815e32d5dSMike Smith 140915e32d5dSMike Smith /* 141015e32d5dSMike Smith * Enable/Disable ACPI 141115e32d5dSMike Smith */ 141215e32d5dSMike Smith ACPI_STATUS 141315e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 141415e32d5dSMike Smith { 141515e32d5dSMike Smith ACPI_STATUS status; 141615e32d5dSMike Smith u_int32_t flags; 141715e32d5dSMike Smith 1418b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1419cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14200ae55423SMike Smith 142115e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 142215e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 142315e32d5dSMike Smith if (!sc->acpi_enabled) { 142415e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 142515e32d5dSMike Smith } else { 142615e32d5dSMike Smith status = AE_OK; 142715e32d5dSMike Smith } 142815e32d5dSMike Smith if (status == AE_OK) 142915e32d5dSMike Smith sc->acpi_enabled = 1; 14300ae55423SMike Smith return_ACPI_STATUS(status); 143115e32d5dSMike Smith } 143215e32d5dSMike Smith 143315e32d5dSMike Smith ACPI_STATUS 143415e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 143515e32d5dSMike Smith { 143615e32d5dSMike Smith ACPI_STATUS status; 143715e32d5dSMike Smith 1438b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1439cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14400ae55423SMike Smith 144115e32d5dSMike Smith if (sc->acpi_enabled) { 144215e32d5dSMike Smith status = AcpiDisable(); 144315e32d5dSMike Smith } else { 144415e32d5dSMike Smith status = AE_OK; 144515e32d5dSMike Smith } 144615e32d5dSMike Smith if (status == AE_OK) 144715e32d5dSMike Smith sc->acpi_enabled = 0; 14480ae55423SMike Smith return_ACPI_STATUS(status); 144915e32d5dSMike Smith } 145015e32d5dSMike Smith 145115e32d5dSMike Smith /* 145215e32d5dSMike Smith * ACPI Event Handlers 145315e32d5dSMike Smith */ 145415e32d5dSMike Smith 145515e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 145615e32d5dSMike Smith 145715e32d5dSMike Smith static void 145815e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 145915e32d5dSMike Smith { 1460fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1461b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 146215e32d5dSMike Smith 1463cb9b0d80SMike Smith ACPI_LOCK; 1464a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 146515e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 1466cb9b0d80SMike Smith ACPI_UNLOCK; 14670ae55423SMike Smith return_VOID; 146815e32d5dSMike Smith } 146915e32d5dSMike Smith 147015e32d5dSMike Smith static void 147115e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 147215e32d5dSMike Smith { 1473fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1474b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 14750ae55423SMike Smith 147615e32d5dSMike Smith /* Well, what to do? :-) */ 14770ae55423SMike Smith 1478cb9b0d80SMike Smith ACPI_LOCK; 1479cb9b0d80SMike Smith ACPI_UNLOCK; 1480cb9b0d80SMike Smith 14810ae55423SMike Smith return_VOID; 148215e32d5dSMike Smith } 148315e32d5dSMike Smith 148415e32d5dSMike Smith /* 148515e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 148615e32d5dSMike Smith */ 148715e32d5dSMike Smith UINT32 148815e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context) 148915e32d5dSMike Smith { 149015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 149115e32d5dSMike Smith 1492b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 14930ae55423SMike Smith 149415e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 14950ae55423SMike Smith 1496b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 149715e32d5dSMike Smith } 149815e32d5dSMike Smith 149915e32d5dSMike Smith UINT32 150015e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context) 150115e32d5dSMike Smith { 150215e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 150315e32d5dSMike Smith 1504b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15050ae55423SMike Smith 150615e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 15070ae55423SMike Smith 1508b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 150915e32d5dSMike Smith } 151015e32d5dSMike Smith 151115e32d5dSMike Smith UINT32 151215e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context) 151315e32d5dSMike Smith { 151415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 151515e32d5dSMike Smith 1516b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15170ae55423SMike Smith 151815e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 15190ae55423SMike Smith 1520b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 152115e32d5dSMike Smith } 152215e32d5dSMike Smith 152315e32d5dSMike Smith UINT32 152415e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context) 152515e32d5dSMike Smith { 152615e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 152715e32d5dSMike Smith 1528b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15290ae55423SMike Smith 153015e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 15310ae55423SMike Smith 1532b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 153315e32d5dSMike Smith } 153415e32d5dSMike Smith 153515e32d5dSMike Smith /* 153615e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 153715e32d5dSMike Smith */ 153815e32d5dSMike Smith struct acpi_staticbuf { 153915e32d5dSMike Smith ACPI_BUFFER buffer; 154015e32d5dSMike Smith char data[512]; 154115e32d5dSMike Smith }; 154215e32d5dSMike Smith 154315e32d5dSMike Smith char * 154415e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 154515e32d5dSMike Smith { 154615e32d5dSMike Smith static struct acpi_staticbuf buf; 154715e32d5dSMike Smith 1548cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1549cb9b0d80SMike Smith 155015e32d5dSMike Smith buf.buffer.Length = 512; 155115e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 155215e32d5dSMike Smith 1553b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 155415e32d5dSMike Smith return(buf.buffer.Pointer); 155515e32d5dSMike Smith return("(unknown path)"); 155615e32d5dSMike Smith } 155715e32d5dSMike Smith 155815e32d5dSMike Smith /* 155915e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 156015e32d5dSMike Smith * parts of the namespace. 156115e32d5dSMike Smith */ 156215e32d5dSMike Smith int 156315e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 156415e32d5dSMike Smith { 15653c600cfbSJohn Baldwin char *cp, *env, *np; 156615e32d5dSMike Smith int len; 156715e32d5dSMike Smith 156815e32d5dSMike Smith np = acpi_name(handle); 156915e32d5dSMike Smith if (*np == '\\') 157015e32d5dSMike Smith np++; 15713c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 157215e32d5dSMike Smith return(0); 157315e32d5dSMike Smith 157415e32d5dSMike Smith /* scan the avoid list checking for a match */ 15753c600cfbSJohn Baldwin cp = env; 157615e32d5dSMike Smith for (;;) { 157715e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 157815e32d5dSMike Smith cp++; 157915e32d5dSMike Smith if (*cp == 0) 158015e32d5dSMike Smith break; 158115e32d5dSMike Smith len = 0; 158215e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 158315e32d5dSMike Smith len++; 1584d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 15853c600cfbSJohn Baldwin freeenv(env); 15860ae55423SMike Smith return(1); 1587d786139cSMaxime Henrion } 15880ae55423SMike Smith cp += len; 15890ae55423SMike Smith } 15903c600cfbSJohn Baldwin freeenv(env); 15910ae55423SMike Smith return(0); 15920ae55423SMike Smith } 15930ae55423SMike Smith 15940ae55423SMike Smith /* 15950ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 15960ae55423SMike Smith */ 15970ae55423SMike Smith int 15980ae55423SMike Smith acpi_disabled(char *subsys) 15990ae55423SMike Smith { 160078689b15SMaxime Henrion char *cp, *env; 16010ae55423SMike Smith int len; 16020ae55423SMike Smith 160378689b15SMaxime Henrion if ((env = getenv("debug.acpi.disable")) == NULL) 16040ae55423SMike Smith return(0); 160578689b15SMaxime Henrion if (!strcmp(env, "all")) { 160678689b15SMaxime Henrion freeenv(env); 16070ae55423SMike Smith return(1); 1608d786139cSMaxime Henrion } 16090ae55423SMike Smith 16100ae55423SMike Smith /* scan the disable list checking for a match */ 161178689b15SMaxime Henrion cp = env; 16120ae55423SMike Smith for (;;) { 16130ae55423SMike Smith while ((*cp != 0) && isspace(*cp)) 16140ae55423SMike Smith cp++; 16150ae55423SMike Smith if (*cp == 0) 16160ae55423SMike Smith break; 16170ae55423SMike Smith len = 0; 16180ae55423SMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 16190ae55423SMike Smith len++; 1620d786139cSMaxime Henrion if (!strncmp(cp, subsys, len)) { 162178689b15SMaxime Henrion freeenv(env); 162215e32d5dSMike Smith return(1); 1623d786139cSMaxime Henrion } 162415e32d5dSMike Smith cp += len; 162515e32d5dSMike Smith } 162678689b15SMaxime Henrion freeenv(env); 162715e32d5dSMike Smith return(0); 162815e32d5dSMike Smith } 162915e32d5dSMike Smith 163015e32d5dSMike Smith /* 1631a1fccb47SMitsuru IWASAKI * Device wake capability enable/disable. 1632a1fccb47SMitsuru IWASAKI */ 1633a1fccb47SMitsuru IWASAKI void 1634a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1635a1fccb47SMitsuru IWASAKI { 1636a1fccb47SMitsuru IWASAKI ACPI_OBJECT_LIST ArgList; 1637a1fccb47SMitsuru IWASAKI ACPI_OBJECT Arg; 1638a1fccb47SMitsuru IWASAKI 1639a1fccb47SMitsuru IWASAKI /* 1640a1fccb47SMitsuru IWASAKI * TBD: All Power Resources referenced by elements 2 through N 1641a1fccb47SMitsuru IWASAKI * of the _PRW object are put into the ON state. 1642a1fccb47SMitsuru IWASAKI */ 1643a1fccb47SMitsuru IWASAKI 1644a1fccb47SMitsuru IWASAKI /* 1645a1fccb47SMitsuru IWASAKI * enable/disable device wake function. 1646a1fccb47SMitsuru IWASAKI */ 1647a1fccb47SMitsuru IWASAKI 1648a1fccb47SMitsuru IWASAKI ArgList.Count = 1; 1649a1fccb47SMitsuru IWASAKI ArgList.Pointer = &Arg; 1650a1fccb47SMitsuru IWASAKI 1651a1fccb47SMitsuru IWASAKI Arg.Type = ACPI_TYPE_INTEGER; 1652a1fccb47SMitsuru IWASAKI Arg.Integer.Value = enable; 1653a1fccb47SMitsuru IWASAKI 1654a1fccb47SMitsuru IWASAKI (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1655a1fccb47SMitsuru IWASAKI } 1656a1fccb47SMitsuru IWASAKI 1657a1fccb47SMitsuru IWASAKI void 1658a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h) 1659a1fccb47SMitsuru IWASAKI { 1660a1fccb47SMitsuru IWASAKI struct acpi_softc *sc; 1661a1fccb47SMitsuru IWASAKI ACPI_STATUS status; 1662a1fccb47SMitsuru IWASAKI ACPI_BUFFER prw_buffer; 1663a1fccb47SMitsuru IWASAKI ACPI_OBJECT *res; 1664a1fccb47SMitsuru IWASAKI 1665a1fccb47SMitsuru IWASAKI ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1666a1fccb47SMitsuru IWASAKI 1667a1fccb47SMitsuru IWASAKI if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) { 1668a1fccb47SMitsuru IWASAKI return; 1669a1fccb47SMitsuru IWASAKI } 1670a1fccb47SMitsuru IWASAKI 1671a1fccb47SMitsuru IWASAKI /* 1672a1fccb47SMitsuru IWASAKI * _PRW object is only required for devices that have the ability 1673a1fccb47SMitsuru IWASAKI * to wake the system from a system sleeping state. 1674a1fccb47SMitsuru IWASAKI */ 1675a1fccb47SMitsuru IWASAKI prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1676a1fccb47SMitsuru IWASAKI status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1677a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) { 1678a1fccb47SMitsuru IWASAKI return; 1679a1fccb47SMitsuru IWASAKI } 1680a1fccb47SMitsuru IWASAKI 1681a1fccb47SMitsuru IWASAKI res = (ACPI_OBJECT *)prw_buffer.Pointer; 1682a1fccb47SMitsuru IWASAKI if (res == NULL) { 1683a1fccb47SMitsuru IWASAKI return; 1684a1fccb47SMitsuru IWASAKI } 1685a1fccb47SMitsuru IWASAKI 1686a1fccb47SMitsuru IWASAKI if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1687a1fccb47SMitsuru IWASAKI goto out; 1688a1fccb47SMitsuru IWASAKI } 1689a1fccb47SMitsuru IWASAKI 1690a1fccb47SMitsuru IWASAKI /* 1691a1fccb47SMitsuru IWASAKI * The element 1 of the _PRW object: 1692a1fccb47SMitsuru IWASAKI * The lowest power system sleeping state that can be entered 1693a1fccb47SMitsuru IWASAKI * while still providing wake functionality. 1694a1fccb47SMitsuru IWASAKI * The sleeping state being entered must be greater or equal to 1695a1fccb47SMitsuru IWASAKI * the power state declared in element 1 of the _PRW object. 1696a1fccb47SMitsuru IWASAKI */ 1697a1fccb47SMitsuru IWASAKI if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) { 1698a1fccb47SMitsuru IWASAKI goto out; 1699a1fccb47SMitsuru IWASAKI } 1700a1fccb47SMitsuru IWASAKI 1701a1fccb47SMitsuru IWASAKI if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) { 1702a1fccb47SMitsuru IWASAKI goto out; 1703a1fccb47SMitsuru IWASAKI } 1704a1fccb47SMitsuru IWASAKI 1705a1fccb47SMitsuru IWASAKI /* 1706a1fccb47SMitsuru IWASAKI * The element 0 of the _PRW object: 1707a1fccb47SMitsuru IWASAKI */ 1708a1fccb47SMitsuru IWASAKI switch(res->Package.Elements[0].Type) { 1709a1fccb47SMitsuru IWASAKI case ACPI_TYPE_INTEGER: 1710a1fccb47SMitsuru IWASAKI /* 1711a1fccb47SMitsuru IWASAKI * If the data type of this package element is numeric, then this 1712a1fccb47SMitsuru IWASAKI * _PRW package element is the bit index in the GPEx_EN, in the 1713a1fccb47SMitsuru IWASAKI * GPE blocks described in the FADT, of the enable bit that is 1714a1fccb47SMitsuru IWASAKI * enabled for the wake event. 1715a1fccb47SMitsuru IWASAKI */ 1716a1fccb47SMitsuru IWASAKI 1717a1fccb47SMitsuru IWASAKI status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value, 1718a1fccb47SMitsuru IWASAKI ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE); 1719a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) 1720a1fccb47SMitsuru IWASAKI printf("%s: EnableEvent Failed\n", __func__); 1721a1fccb47SMitsuru IWASAKI break; 1722a1fccb47SMitsuru IWASAKI 1723a1fccb47SMitsuru IWASAKI case ACPI_TYPE_PACKAGE: 1724a1fccb47SMitsuru IWASAKI /* XXX TBD */ 1725a1fccb47SMitsuru IWASAKI 1726a1fccb47SMitsuru IWASAKI /* 1727a1fccb47SMitsuru IWASAKI * If the data type of this package element is a package, then this 1728a1fccb47SMitsuru IWASAKI * _PRW package element is itself a package containing two 1729a1fccb47SMitsuru IWASAKI * elements. The first is an object reference to the GPE Block 1730a1fccb47SMitsuru IWASAKI * device that contains the GPE that will be triggered by the wake 1731a1fccb47SMitsuru IWASAKI * event. The second element is numeric and it contains the bit 1732a1fccb47SMitsuru IWASAKI * index in the GPEx_EN, in the GPE Block referenced by the 1733a1fccb47SMitsuru IWASAKI * first element in the package, of the enable bit that is enabled for 1734a1fccb47SMitsuru IWASAKI * the wake event. 1735a1fccb47SMitsuru IWASAKI * For example, if this field is a package then it is of the form: 1736a1fccb47SMitsuru IWASAKI * Package() {\_SB.PCI0.ISA.GPE, 2} 1737a1fccb47SMitsuru IWASAKI */ 1738a1fccb47SMitsuru IWASAKI 1739a1fccb47SMitsuru IWASAKI break; 1740a1fccb47SMitsuru IWASAKI 1741a1fccb47SMitsuru IWASAKI default: 1742a1fccb47SMitsuru IWASAKI break; 1743a1fccb47SMitsuru IWASAKI } 1744a1fccb47SMitsuru IWASAKI 1745a1fccb47SMitsuru IWASAKI out: 1746a1fccb47SMitsuru IWASAKI if (prw_buffer.Pointer != NULL) 1747a1fccb47SMitsuru IWASAKI AcpiOsFree(prw_buffer.Pointer); 1748a1fccb47SMitsuru IWASAKI return; 1749a1fccb47SMitsuru IWASAKI } 1750a1fccb47SMitsuru IWASAKI 1751a1fccb47SMitsuru IWASAKI /* 175215e32d5dSMike Smith * Control interface. 175315e32d5dSMike Smith * 17540ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 17550ae55423SMike Smith * drivers wanting to be accessible via /dev/acpi should use the register/deregister 17560ae55423SMike Smith * interface to make their handlers visible. 175715e32d5dSMike Smith */ 17580ae55423SMike Smith struct acpi_ioctl_hook 17590ae55423SMike Smith { 17600ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 17610ae55423SMike Smith u_long cmd; 17620ae55423SMike Smith int (* fn)(u_long cmd, caddr_t addr, void *arg); 17630ae55423SMike Smith void *arg; 17640ae55423SMike Smith }; 17650ae55423SMike Smith 17660ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 17670ae55423SMike Smith static int acpi_ioctl_hooks_initted; 17680ae55423SMike Smith 17690ae55423SMike Smith /* 17700ae55423SMike Smith * Register an ioctl handler. 17710ae55423SMike Smith */ 17720ae55423SMike Smith int 17730ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 17740ae55423SMike Smith { 17750ae55423SMike Smith struct acpi_ioctl_hook *hp; 17760ae55423SMike Smith 17770ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 17780ae55423SMike Smith return(ENOMEM); 17790ae55423SMike Smith hp->cmd = cmd; 17800ae55423SMike Smith hp->fn = fn; 17810ae55423SMike Smith hp->arg = arg; 17820ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 17830ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 17840ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 17850ae55423SMike Smith } 17860ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 17870ae55423SMike Smith return(0); 17880ae55423SMike Smith } 17890ae55423SMike Smith 17900ae55423SMike Smith /* 17910ae55423SMike Smith * Deregister an ioctl handler. 17920ae55423SMike Smith */ 17930ae55423SMike Smith void 17940ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 17950ae55423SMike Smith { 17960ae55423SMike Smith struct acpi_ioctl_hook *hp; 17970ae55423SMike Smith 17980ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 17990ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 18000ae55423SMike Smith break; 18010ae55423SMike Smith 18020ae55423SMike Smith if (hp != NULL) { 18030ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 18040ae55423SMike Smith free(hp, M_ACPIDEV); 18050ae55423SMike Smith } 18060ae55423SMike Smith } 18070ae55423SMike Smith 180815e32d5dSMike Smith static int 18096b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 181015e32d5dSMike Smith { 181115e32d5dSMike Smith return(0); 181215e32d5dSMike Smith } 181315e32d5dSMike Smith 181415e32d5dSMike Smith static int 18156b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 181615e32d5dSMike Smith { 181715e32d5dSMike Smith return(0); 181815e32d5dSMike Smith } 181915e32d5dSMike Smith 182015e32d5dSMike Smith static int 18216b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 182215e32d5dSMike Smith { 182315e32d5dSMike Smith struct acpi_softc *sc; 18240ae55423SMike Smith struct acpi_ioctl_hook *hp; 18250ae55423SMike Smith int error, xerror, state; 1826fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 182715e32d5dSMike Smith 1828cb9b0d80SMike Smith ACPI_LOCK; 1829cb9b0d80SMike Smith 183015e32d5dSMike Smith error = state = 0; 183115e32d5dSMike Smith sc = dev->si_drv1; 183215e32d5dSMike Smith 18330ae55423SMike Smith /* 18340ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 18350ae55423SMike Smith */ 18360ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 18370ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 18380ae55423SMike Smith if (hp->cmd == cmd) { 18390ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 18400ae55423SMike Smith if (xerror != 0) 18410ae55423SMike Smith error = xerror; 1842917d44c8SMitsuru IWASAKI goto out; 18430ae55423SMike Smith } 18440ae55423SMike Smith } 18450ae55423SMike Smith } 18460ae55423SMike Smith 18470ae55423SMike Smith /* 1848a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 1849a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 1850a89fcf28STakanori Watanabe * Not changing system behavior. 1851a89fcf28STakanori Watanabe */ 1852a89fcf28STakanori Watanabe if(!(flag & FWRITE)){ 1853a89fcf28STakanori Watanabe return EPERM; 1854a89fcf28STakanori Watanabe } 1855a89fcf28STakanori Watanabe 1856a89fcf28STakanori Watanabe /* 18570ae55423SMike Smith * Core system ioctls. 18580ae55423SMike Smith */ 185915e32d5dSMike Smith switch (cmd) { 186015e32d5dSMike Smith case ACPIIO_ENABLE: 18610ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 186215e32d5dSMike Smith error = ENXIO; 186315e32d5dSMike Smith break; 186415e32d5dSMike Smith 186515e32d5dSMike Smith case ACPIIO_DISABLE: 18660ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 186715e32d5dSMike Smith error = ENXIO; 186815e32d5dSMike Smith break; 186915e32d5dSMike Smith 187015e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 187115e32d5dSMike Smith if (!sc->acpi_enabled) { 187215e32d5dSMike Smith error = ENXIO; 187315e32d5dSMike Smith break; 187415e32d5dSMike Smith } 187515e32d5dSMike Smith state = *(int *)addr; 1876a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 187715e32d5dSMike Smith acpi_SetSleepState(sc, state); 187815e32d5dSMike Smith } else { 187915e32d5dSMike Smith error = EINVAL; 188015e32d5dSMike Smith } 188115e32d5dSMike Smith break; 188215e32d5dSMike Smith 188315e32d5dSMike Smith default: 18840ae55423SMike Smith if (error == 0) 188515e32d5dSMike Smith error = EINVAL; 188615e32d5dSMike Smith break; 188715e32d5dSMike Smith } 1888917d44c8SMitsuru IWASAKI 1889917d44c8SMitsuru IWASAKI out: 1890cb9b0d80SMike Smith ACPI_UNLOCK; 189115e32d5dSMike Smith return(error); 189215e32d5dSMike Smith } 189315e32d5dSMike Smith 18941d073b1dSJohn Baldwin static int 18951d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 18961d073b1dSJohn Baldwin { 18971d073b1dSJohn Baldwin char sleep_state[10]; 18981d073b1dSJohn Baldwin int error; 18991d073b1dSJohn Baldwin u_int new_state, old_state; 19001d073b1dSJohn Baldwin 19011d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 1902b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX+1) { 19031d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 1904cb9b0d80SMike Smith } else { 1905b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 19061d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 19071d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 1908cb9b0d80SMike Smith } 19091d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 19101d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 1911b8670bedSMitsuru IWASAKI for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) { 19121d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 19131d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 19141d073b1dSJohn Baldwin break; 1915cb9b0d80SMike Smith } 1916931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX+1) { 1917931a10c9SMitsuru IWASAKI if (new_state != old_state) { 19181d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 1919931a10c9SMitsuru IWASAKI } 1920cb9b0d80SMike Smith } else { 19211d073b1dSJohn Baldwin error = EINVAL; 19221d073b1dSJohn Baldwin } 1923cb9b0d80SMike Smith } 19241d073b1dSJohn Baldwin return(error); 19251d073b1dSJohn Baldwin } 19261d073b1dSJohn Baldwin 192715e32d5dSMike Smith #ifdef ACPI_DEBUG 19280ae55423SMike Smith /* 19290ae55423SMike Smith * Support for parsing debug options from the kernel environment. 19300ae55423SMike Smith * 19310ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 19320ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 19330ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 19340ae55423SMike Smith * prefixing the bit name with !. 19350ae55423SMike Smith */ 193615e32d5dSMike Smith struct debugtag 193715e32d5dSMike Smith { 193815e32d5dSMike Smith char *name; 193915e32d5dSMike Smith UINT32 value; 194015e32d5dSMike Smith }; 194115e32d5dSMike Smith 194215e32d5dSMike Smith static struct debugtag dbg_layer[] = { 1943c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 1944c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 1945c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 1946c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 1947c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1948c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 1949c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1950c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 1951c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 1952d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 19534c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1954d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 19554c1cdee6SMike Smith 1956cb9b0d80SMike Smith {"ACPI_BUS", ACPI_BUS}, 19574c1cdee6SMike Smith {"ACPI_SYSTEM", ACPI_SYSTEM}, 1958cb9b0d80SMike Smith {"ACPI_POWER", ACPI_POWER}, 1959cb9b0d80SMike Smith {"ACPI_EC", ACPI_EC}, 1960c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 1961c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 1962c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 19634c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 1964cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 19654c1cdee6SMike Smith {"ACPI_FAN", ACPI_FAN}, 19664c1cdee6SMike Smith 1967b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 1968c5ba0be4SMike Smith {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 196915e32d5dSMike Smith {NULL, 0} 197015e32d5dSMike Smith }; 197115e32d5dSMike Smith 197215e32d5dSMike Smith static struct debugtag dbg_level[] = { 19734c1cdee6SMike Smith {"ACPI_LV_OK", ACPI_LV_OK}, 19744c1cdee6SMike Smith {"ACPI_LV_INFO", ACPI_LV_INFO}, 19754c1cdee6SMike Smith {"ACPI_LV_WARN", ACPI_LV_WARN}, 19764c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 19774c1cdee6SMike Smith {"ACPI_LV_FATAL", ACPI_LV_FATAL}, 19784c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 19794c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 1980d62ab2f4SMitsuru IWASAKI 1981d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 19824c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 19834c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 1984d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 19854c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 19864c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 19874c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 19884c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 19894c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 19904c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 19914c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 19924c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 19934c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 19944c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 19954c1cdee6SMike Smith {"ACPI_LV_INIT", ACPI_LV_INIT}, 1996d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 1997d62ab2f4SMitsuru IWASAKI 1998d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 1999d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2000d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2001d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2002d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 20034c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2004d62ab2f4SMitsuru IWASAKI 2005d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2006d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2007d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2008d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2009d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2010d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2011d62ab2f4SMitsuru IWASAKI 2012d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 201398479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 201498479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 201598479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 201698479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 201798479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 201815e32d5dSMike Smith {NULL, 0} 201915e32d5dSMike Smith }; 202015e32d5dSMike Smith 202115e32d5dSMike Smith static void 202215e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 202315e32d5dSMike Smith { 202415e32d5dSMike Smith char *ep; 202515e32d5dSMike Smith int i, l; 20260ae55423SMike Smith int set; 202715e32d5dSMike Smith 202815e32d5dSMike Smith while (*cp) { 202915e32d5dSMike Smith if (isspace(*cp)) { 203015e32d5dSMike Smith cp++; 203115e32d5dSMike Smith continue; 203215e32d5dSMike Smith } 203315e32d5dSMike Smith ep = cp; 203415e32d5dSMike Smith while (*ep && !isspace(*ep)) 203515e32d5dSMike Smith ep++; 20360ae55423SMike Smith if (*cp == '!') { 20370ae55423SMike Smith set = 0; 20380ae55423SMike Smith cp++; 20390ae55423SMike Smith if (cp == ep) 20400ae55423SMike Smith continue; 20410ae55423SMike Smith } else { 20420ae55423SMike Smith set = 1; 20430ae55423SMike Smith } 204415e32d5dSMike Smith l = ep - cp; 204515e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 204615e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 20470ae55423SMike Smith if (set) { 204815e32d5dSMike Smith *flag |= tag[i].value; 20490ae55423SMike Smith } else { 20500ae55423SMike Smith *flag &= ~tag[i].value; 20510ae55423SMike Smith } 205215e32d5dSMike Smith printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 205315e32d5dSMike Smith } 205415e32d5dSMike Smith } 205515e32d5dSMike Smith cp = ep; 205615e32d5dSMike Smith } 205715e32d5dSMike Smith } 205815e32d5dSMike Smith 205915e32d5dSMike Smith static void 20602a4ac806SMike Smith acpi_set_debugging(void *junk) 206115e32d5dSMike Smith { 206294aae282SMike Barcroft char *cp; 206315e32d5dSMike Smith 206487b45ed5SMitsuru IWASAKI if (!cold) 206587b45ed5SMitsuru IWASAKI return; 206687b45ed5SMitsuru IWASAKI 20670ae55423SMike Smith AcpiDbgLayer = 0; 20680ae55423SMike Smith AcpiDbgLevel = 0; 206994aae282SMike Barcroft if ((cp = getenv("debug.acpi.layer")) != NULL) { 207015e32d5dSMike Smith acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 207194aae282SMike Barcroft freeenv(cp); 207294aae282SMike Barcroft } 207394aae282SMike Barcroft if ((cp = getenv("debug.acpi.level")) != NULL) { 207415e32d5dSMike Smith acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 207594aae282SMike Barcroft freeenv(cp); 207694aae282SMike Barcroft } 20770ae55423SMike Smith 20780ae55423SMike Smith printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 207915e32d5dSMike Smith } 20802a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 208115e32d5dSMike Smith #endif 2082f9390180SMitsuru IWASAKI 2083f9390180SMitsuru IWASAKI static int 2084f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2085f9390180SMitsuru IWASAKI { 2086f9390180SMitsuru IWASAKI int state, acpi_state; 2087f9390180SMitsuru IWASAKI int error; 2088f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2089f9390180SMitsuru IWASAKI va_list ap; 2090f9390180SMitsuru IWASAKI 2091f9390180SMitsuru IWASAKI error = 0; 2092f9390180SMitsuru IWASAKI switch (cmd) { 2093f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2094f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2095f9390180SMitsuru IWASAKI if (sc == NULL) { 2096f9390180SMitsuru IWASAKI error = EINVAL; 2097f9390180SMitsuru IWASAKI goto out; 2098f9390180SMitsuru IWASAKI } 2099f9390180SMitsuru IWASAKI 2100f9390180SMitsuru IWASAKI va_start(ap, arg); 2101f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2102f9390180SMitsuru IWASAKI va_end(ap); 2103f9390180SMitsuru IWASAKI 2104f9390180SMitsuru IWASAKI switch (state) { 2105f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2106f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2107f9390180SMitsuru IWASAKI break; 2108f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2109f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2110f9390180SMitsuru IWASAKI break; 2111f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2112f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2113f9390180SMitsuru IWASAKI break; 2114f9390180SMitsuru IWASAKI default: 2115f9390180SMitsuru IWASAKI error = EINVAL; 2116f9390180SMitsuru IWASAKI goto out; 2117f9390180SMitsuru IWASAKI } 2118f9390180SMitsuru IWASAKI 2119f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2120f9390180SMitsuru IWASAKI break; 2121f9390180SMitsuru IWASAKI 2122f9390180SMitsuru IWASAKI default: 2123f9390180SMitsuru IWASAKI error = EINVAL; 2124f9390180SMitsuru IWASAKI goto out; 2125f9390180SMitsuru IWASAKI } 2126f9390180SMitsuru IWASAKI 2127f9390180SMitsuru IWASAKI out: 2128f9390180SMitsuru IWASAKI return (error); 2129f9390180SMitsuru IWASAKI } 2130f9390180SMitsuru IWASAKI 2131f9390180SMitsuru IWASAKI static void 2132f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2133f9390180SMitsuru IWASAKI { 21346c407052SMitsuru IWASAKI int error; 21356c407052SMitsuru IWASAKI 213687b45ed5SMitsuru IWASAKI if (!cold) 213787b45ed5SMitsuru IWASAKI return; 213887b45ed5SMitsuru IWASAKI 21396c407052SMitsuru IWASAKI if (!resource_int_value("acpi", 0, "disabled", &error) && 21406c407052SMitsuru IWASAKI (error != 0)) 21416c407052SMitsuru IWASAKI return; 2142f9390180SMitsuru IWASAKI 2143f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2144f9390180SMitsuru IWASAKI } 2145f9390180SMitsuru IWASAKI 2146f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2147f9390180SMitsuru IWASAKI 2148