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> 3615e32d5dSMike Smith #include <sys/malloc.h> 3715e32d5dSMike Smith #include <sys/bus.h> 3815e32d5dSMike Smith #include <sys/conf.h> 3915e32d5dSMike Smith #include <sys/ioccom.h> 4015e32d5dSMike Smith #include <sys/reboot.h> 4115e32d5dSMike Smith #include <sys/sysctl.h> 4215e32d5dSMike Smith #include <sys/ctype.h> 431611ea87SMitsuru IWASAKI #include <sys/linker.h> 44f9390180SMitsuru IWASAKI #include <sys/power.h> 4515e32d5dSMike Smith 4615e32d5dSMike Smith #include <machine/clock.h> 4715e32d5dSMike Smith #include <machine/resource.h> 4815e32d5dSMike Smith 49bc0f2195SMike Smith #include <isa/isavar.h> 50bc0f2195SMike Smith 5115e32d5dSMike Smith #include "acpi.h" 5215e32d5dSMike Smith 531611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h> 541611ea87SMitsuru IWASAKI 5515e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5615e32d5dSMike Smith #include <dev/acpica/acpiio.h> 5715e32d5dSMike Smith 5815e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 5915e32d5dSMike Smith 6015e32d5dSMike Smith /* 610ae55423SMike Smith * Hooks for the ACPI CA debugging infrastructure 620ae55423SMike Smith */ 63cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 64b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 650ae55423SMike Smith 660ae55423SMike Smith /* 6715e32d5dSMike Smith * Character device 6815e32d5dSMike Smith */ 6915e32d5dSMike Smith 7015e32d5dSMike Smith static d_open_t acpiopen; 7115e32d5dSMike Smith static d_close_t acpiclose; 7215e32d5dSMike Smith static d_ioctl_t acpiioctl; 7315e32d5dSMike Smith 7415e32d5dSMike Smith #define CDEV_MAJOR 152 7515e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 7615e32d5dSMike Smith acpiopen, 7715e32d5dSMike Smith acpiclose, 7815e32d5dSMike Smith noread, 7915e32d5dSMike Smith nowrite, 8015e32d5dSMike Smith acpiioctl, 8115e32d5dSMike Smith nopoll, 8215e32d5dSMike Smith nommap, 8315e32d5dSMike Smith nostrategy, 8415e32d5dSMike Smith "acpi", 8515e32d5dSMike Smith CDEV_MAJOR, 8615e32d5dSMike Smith nodump, 8715e32d5dSMike Smith nopsize, 88606e1d68SJohn Baldwin 0 8915e32d5dSMike Smith }; 9015e32d5dSMike Smith 911d073b1dSJohn Baldwin static const char* sleep_state_names[] = { 92b8670bedSMitsuru IWASAKI "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 931d073b1dSJohn Baldwin 942a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */ 952a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5; 962a4ac806SMike Smith 97fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 98cb9b0d80SMike Smith struct mtx acpi_mutex; 99fc0ea94aSJohn Baldwin #endif 100cb9b0d80SMike Smith 1016d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10215e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 10315e32d5dSMike Smith static int acpi_probe(device_t dev); 10415e32d5dSMike Smith static int acpi_attach(device_t dev); 10515e32d5dSMike Smith static device_t acpi_add_child(device_t bus, int order, const char *name, int unit); 10615e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 10715e32d5dSMike Smith static int acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result); 10815e32d5dSMike Smith static int acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value); 10915e32d5dSMike Smith static int acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, 11015e32d5dSMike Smith u_long count); 11115e32d5dSMike Smith static int acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, 11215e32d5dSMike Smith u_long *countp); 11315e32d5dSMike Smith static struct resource *acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 11415e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags); 11515e32d5dSMike Smith static int acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r); 11632d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev); 117bc0f2195SMike Smith static int acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids); 11815e32d5dSMike Smith 11915e32d5dSMike Smith static void acpi_probe_children(device_t bus); 12015e32d5dSMike Smith static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status); 12115e32d5dSMike Smith 12215e32d5dSMike Smith static void acpi_shutdown_pre_sync(void *arg, int howto); 12315e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 12415e32d5dSMike Smith 12513d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 12613d4f7baSMitsuru IWASAKI 12715e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 12815e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 1291d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 13015e32d5dSMike Smith 131f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 132f9390180SMitsuru IWASAKI 13315e32d5dSMike Smith static device_method_t acpi_methods[] = { 13415e32d5dSMike Smith /* Device interface */ 13515e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 13615e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 13715e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 13815e32d5dSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 13915e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 14015e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 14115e32d5dSMike Smith 14215e32d5dSMike Smith /* Bus interface */ 14315e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 14415e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 14515e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 14615e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 14715e32d5dSMike Smith DEVMETHOD(bus_set_resource, acpi_set_resource), 14815e32d5dSMike Smith DEVMETHOD(bus_get_resource, acpi_get_resource), 14915e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 15015e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 15115e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 15215e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 15315e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 15415e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 15515e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 15615e32d5dSMike Smith 157bc0f2195SMike Smith /* ISA emulation */ 158bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 159bc0f2195SMike Smith 16015e32d5dSMike Smith {0, 0} 16115e32d5dSMike Smith }; 16215e32d5dSMike Smith 16315e32d5dSMike Smith static driver_t acpi_driver = { 16415e32d5dSMike Smith "acpi", 16515e32d5dSMike Smith acpi_methods, 16615e32d5dSMike Smith sizeof(struct acpi_softc), 16715e32d5dSMike Smith }; 16815e32d5dSMike Smith 1693273b005SMike Smith static devclass_t acpi_devclass; 1706d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 171dde24897SMike Smith MODULE_VERSION(acpi, 100); 17215e32d5dSMike Smith 17315e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, ""); 17415e32d5dSMike Smith SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, ""); 175dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION; 176dd081ed5SMitsuru IWASAKI SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, ""); 17715e32d5dSMike Smith 17815e32d5dSMike Smith /* 1796d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 1806d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 1816d63101aSMike Smith * It also cannot be unloaded, since the entire system bus heirarchy hangs off it. 1826d63101aSMike Smith */ 1836d63101aSMike Smith static int 1846d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 1856d63101aSMike Smith { 1866d63101aSMike Smith switch(event) { 1876d63101aSMike Smith case MOD_LOAD: 18887b45ed5SMitsuru IWASAKI if (!cold) { 18987b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 1906d63101aSMike Smith return(EPERM); 19187b45ed5SMitsuru IWASAKI } 1926d63101aSMike Smith break; 1936d63101aSMike Smith case MOD_UNLOAD: 194f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 1956d63101aSMike Smith return(EBUSY); 196f9390180SMitsuru IWASAKI break; 1976d63101aSMike Smith default: 1986d63101aSMike Smith break; 1996d63101aSMike Smith } 2006d63101aSMike Smith return(0); 2016d63101aSMike Smith } 2026d63101aSMike Smith 2036d63101aSMike Smith /* 20415e32d5dSMike Smith * Detect ACPI, perform early initialisation 20515e32d5dSMike Smith */ 20615e32d5dSMike Smith static void 20715e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent) 20815e32d5dSMike Smith { 20915e32d5dSMike Smith device_t child; 21015e32d5dSMike Smith int error; 211d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 212d786139cSMaxime Henrion char *debugpoint; 21315e32d5dSMike Smith #endif 21415e32d5dSMike Smith 215b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2160ae55423SMike Smith 21787b45ed5SMitsuru IWASAKI if (!cold) 21887b45ed5SMitsuru IWASAKI return_VOID; 219840f9b53STakanori Watanabe 22015e32d5dSMike Smith /* 22132d18aa5SMike Smith * Check that we haven't been disabled with a hint. 22232d18aa5SMike Smith */ 22332d18aa5SMike Smith if (!resource_int_value("acpi", 0, "disabled", &error) && 22432d18aa5SMike Smith (error != 0)) 22532d18aa5SMike Smith return_VOID; 22632d18aa5SMike Smith 22732d18aa5SMike Smith /* 22815e32d5dSMike Smith * Make sure we're not being doubly invoked. 22915e32d5dSMike Smith */ 23015e32d5dSMike Smith if (device_find_child(parent, "acpi", 0) != NULL) 2310ae55423SMike Smith return_VOID; 23215e32d5dSMike Smith 233fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 234cb9b0d80SMike Smith /* initialise the ACPI mutex */ 2356008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 236fc0ea94aSJohn Baldwin #endif 237cb9b0d80SMike Smith 23815e32d5dSMike Smith /* 2392a4ac806SMike Smith * Start up the ACPI CA subsystem. 24015e32d5dSMike Smith */ 241d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 242d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 243d786139cSMaxime Henrion if (debugpoint) { 244d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 24515e32d5dSMike Smith acpi_EnterDebugger(); 246d786139cSMaxime Henrion freeenv(debugpoint); 247d786139cSMaxime Henrion } 24815e32d5dSMike Smith #endif 249b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 250bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 2510ae55423SMike Smith return_VOID; 25215e32d5dSMike Smith } 253d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 254d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 255d786139cSMaxime Henrion if (debugpoint) { 256d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 25715e32d5dSMike Smith acpi_EnterDebugger(); 258d786139cSMaxime Henrion freeenv(debugpoint); 259d786139cSMaxime Henrion } 26015e32d5dSMike Smith #endif 2611611ea87SMitsuru IWASAKI 262b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 263bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 2640ae55423SMike Smith return_VOID; 26515e32d5dSMike Smith } 26615e32d5dSMike Smith 26715e32d5dSMike Smith /* 26815e32d5dSMike Smith * Attach the actual ACPI device. 26915e32d5dSMike Smith */ 27015e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 27115e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 2720ae55423SMike Smith return_VOID; 27315e32d5dSMike Smith } 27415e32d5dSMike Smith } 27515e32d5dSMike Smith 27615e32d5dSMike Smith /* 27715e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 27815e32d5dSMike Smith */ 27915e32d5dSMike Smith static int 28015e32d5dSMike Smith acpi_probe(device_t dev) 28115e32d5dSMike Smith { 28215e32d5dSMike Smith ACPI_TABLE_HEADER th; 28315e32d5dSMike Smith char buf[20]; 284cb9b0d80SMike Smith ACPI_STATUS status; 28515e32d5dSMike Smith int error; 286fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 28715e32d5dSMike Smith 288b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 289f9390180SMitsuru IWASAKI 290f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 291f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 292f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 293f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 294f9390180SMitsuru IWASAKI } 295f9390180SMitsuru IWASAKI 296cb9b0d80SMike Smith ACPI_LOCK; 2970ae55423SMike Smith 298b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 299bfae45aaSMike Smith device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status)); 300cb9b0d80SMike Smith error = ENXIO; 301cb9b0d80SMike Smith } else { 30215e32d5dSMike Smith sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 30315e32d5dSMike Smith device_set_desc_copy(dev, buf); 304cb9b0d80SMike Smith error = 0; 305cb9b0d80SMike Smith } 306cb9b0d80SMike Smith ACPI_UNLOCK; 307cb9b0d80SMike Smith return_VALUE(error); 30815e32d5dSMike Smith } 30915e32d5dSMike Smith 31015e32d5dSMike Smith static int 31115e32d5dSMike Smith acpi_attach(device_t dev) 31215e32d5dSMike Smith { 31315e32d5dSMike Smith struct acpi_softc *sc; 314cb9b0d80SMike Smith ACPI_STATUS status; 31515e32d5dSMike Smith int error; 31632d18aa5SMike Smith UINT32 flags; 317498d464fSMitsuru IWASAKI char *env; 318d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 319d786139cSMaxime Henrion char *debugpoint; 32015e32d5dSMike Smith #endif 321fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 32215e32d5dSMike Smith 323b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 324cb9b0d80SMike Smith ACPI_LOCK; 32515e32d5dSMike Smith sc = device_get_softc(dev); 32615e32d5dSMike Smith bzero(sc, sizeof(*sc)); 32715e32d5dSMike Smith sc->acpi_dev = dev; 32815e32d5dSMike Smith 329d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 330d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 331d786139cSMaxime Henrion if (debugpoint) { 332d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 33315e32d5dSMike Smith acpi_EnterDebugger(); 334d786139cSMaxime Henrion freeenv(debugpoint); 335d786139cSMaxime Henrion } 33615e32d5dSMike Smith #endif 33715e32d5dSMike Smith 33815e32d5dSMike Smith /* 33915e32d5dSMike Smith * Install the default address space handlers. 34015e32d5dSMike Smith */ 341cb9b0d80SMike Smith error = ENXIO; 342b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3432a4ac806SMike Smith ACPI_ADR_SPACE_SYSTEM_MEMORY, 34415e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 345b53f2771SMike Smith NULL, NULL))) { 346bfae45aaSMike Smith device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status)); 347cb9b0d80SMike Smith goto out; 34815e32d5dSMike Smith } 349b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3502a4ac806SMike Smith ACPI_ADR_SPACE_SYSTEM_IO, 35115e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 352b53f2771SMike Smith NULL, NULL))) { 353bfae45aaSMike Smith device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status)); 354cb9b0d80SMike Smith goto out; 35515e32d5dSMike Smith } 356b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3572a4ac806SMike Smith ACPI_ADR_SPACE_PCI_CONFIG, 35815e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 359b53f2771SMike Smith NULL, NULL))) { 360bfae45aaSMike Smith device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status)); 361cb9b0d80SMike Smith goto out; 36215e32d5dSMike Smith } 36315e32d5dSMike Smith 36415e32d5dSMike Smith /* 36515e32d5dSMike Smith * Bring ACPI fully online. 36615e32d5dSMike Smith * 36732d18aa5SMike Smith * Note that some systems (specifically, those with namespace evaluation issues 36832d18aa5SMike Smith * that require the avoidance of parts of the namespace) must avoid running _INI 36932d18aa5SMike Smith * and _STA on everything, as well as dodging the final object init pass. 37015e32d5dSMike Smith * 37132d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 37232d18aa5SMike Smith * 37332d18aa5SMike Smith * XXX We should arrange for the object init pass after we have attached all our 37432d18aa5SMike Smith * child devices, but on many systems it works here. 37515e32d5dSMike Smith */ 376d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 377d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 378d786139cSMaxime Henrion if (debugpoint) { 379d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 38015e32d5dSMike Smith acpi_EnterDebugger(); 381d786139cSMaxime Henrion freeenv(debugpoint); 382d786139cSMaxime Henrion } 38315e32d5dSMike Smith #endif 38432d18aa5SMike Smith flags = 0; 385d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 38632d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 387b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 388bfae45aaSMike Smith device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status)); 389cb9b0d80SMike Smith goto out; 39015e32d5dSMike Smith } 39115e32d5dSMike Smith 392b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 393b69ed3f4SMitsuru IWASAKI device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status)); 394b69ed3f4SMitsuru IWASAKI goto out; 395b69ed3f4SMitsuru IWASAKI } 396b69ed3f4SMitsuru IWASAKI 39715e32d5dSMike Smith /* 3981d073b1dSJohn Baldwin * Setup our sysctl tree. 3991d073b1dSJohn Baldwin * 4001d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4011d073b1dSJohn Baldwin */ 4021d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4031d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4041d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4051d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 4061d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4071d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4081d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4091d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4101d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4111d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4121d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4131d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4141d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 415f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 416f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 417f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 418f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 419f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 420f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4212d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 422ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 423ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 424ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4251611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 4261611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 4271611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4282d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 4292d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 430c6a78e98STakanori Watanabe SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 431c6a78e98STakanori Watanabe OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 432c6a78e98STakanori Watanabe &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 433c6a78e98STakanori Watanabe sc->acpi_disable_on_poweroff = 1; 434f3a6cbb6SMitsuru IWASAKI sc->acpi_sleep_delay = 0; 435931a10c9SMitsuru IWASAKI sc->acpi_s4bios = 1; 4362d644607SMitsuru IWASAKI if (bootverbose) 4372d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 438498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 439498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 440498d464fSMitsuru IWASAKI freeenv(env); 441498d464fSMitsuru IWASAKI } 4421d073b1dSJohn Baldwin 4431d073b1dSJohn Baldwin /* 44415e32d5dSMike Smith * Dispatch the default sleep state to devices. 44515e32d5dSMike Smith * TBD: should be configured from userland policy manager. 44615e32d5dSMike Smith */ 44715e32d5dSMike Smith sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 44815e32d5dSMike Smith sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 44915e32d5dSMike Smith sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 450f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 451f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 45215e32d5dSMike Smith 45313d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 45415e32d5dSMike Smith 45515e32d5dSMike Smith /* 45615e32d5dSMike Smith * Scan the namespace and attach/initialise children. 45715e32d5dSMike Smith */ 458d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 459d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 460d786139cSMaxime Henrion if (debugpoint) { 461d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 46215e32d5dSMike Smith acpi_EnterDebugger(); 463d786139cSMaxime Henrion freeenv(debugpoint); 464d786139cSMaxime Henrion } 46515e32d5dSMike Smith #endif 46615e32d5dSMike Smith 46715e32d5dSMike Smith /* 46815e32d5dSMike Smith * Register our shutdown handlers 46915e32d5dSMike Smith */ 47015e32d5dSMike Smith EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 47115e32d5dSMike Smith EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 47215e32d5dSMike Smith 47315e32d5dSMike Smith /* 47415e32d5dSMike Smith * Register our acpi event handlers. 47515e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 47615e32d5dSMike Smith */ 47715e32d5dSMike Smith EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 47815e32d5dSMike Smith EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 47915e32d5dSMike Smith 48015e32d5dSMike Smith /* 48115e32d5dSMike Smith * Flag our initial states. 48215e32d5dSMike Smith */ 48315e32d5dSMike Smith sc->acpi_enabled = 1; 48415e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 485ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 48615e32d5dSMike Smith 48715e32d5dSMike Smith /* 48815e32d5dSMike Smith * Create the control device 48915e32d5dSMike Smith */ 4902a4689e8SRobert Watson sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0600, 4912a4689e8SRobert Watson "acpi"); 49215e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 49315e32d5dSMike Smith 494d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 495d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 496d786139cSMaxime Henrion if (debugpoint) { 497d786139cSMaxime Henrion if (!strcmp(debugpoint, "running")) 49815e32d5dSMike Smith acpi_EnterDebugger(); 499d786139cSMaxime Henrion freeenv(debugpoint); 500d786139cSMaxime Henrion } 50115e32d5dSMike Smith #endif 502f86214b6SMitsuru IWASAKI 50391da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 504c573e654SMitsuru IWASAKI if ((error = acpi_task_thread_init())) { 505c573e654SMitsuru IWASAKI goto out; 506c573e654SMitsuru IWASAKI } 507c573e654SMitsuru IWASAKI #endif 508c573e654SMitsuru IWASAKI 509f86214b6SMitsuru IWASAKI if ((error = acpi_machdep_init(dev))) { 510f86214b6SMitsuru IWASAKI goto out; 511f86214b6SMitsuru IWASAKI } 512f86214b6SMitsuru IWASAKI 513f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 514f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 515f9390180SMitsuru IWASAKI 516eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 517eeb6dba2SJohn Baldwin acpi_probe_children(dev); 518eeb6dba2SJohn Baldwin 519cb9b0d80SMike Smith error = 0; 520cb9b0d80SMike Smith 521cb9b0d80SMike Smith out: 522cb9b0d80SMike Smith ACPI_UNLOCK; 523cb9b0d80SMike Smith return_VALUE(error); 52415e32d5dSMike Smith } 52515e32d5dSMike Smith 52615e32d5dSMike Smith /* 52715e32d5dSMike Smith * Handle a new device being added 52815e32d5dSMike Smith */ 52915e32d5dSMike Smith static device_t 53015e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 53115e32d5dSMike Smith { 53215e32d5dSMike Smith struct acpi_device *ad; 53315e32d5dSMike Smith device_t child; 53415e32d5dSMike Smith 53515e32d5dSMike Smith if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 53615e32d5dSMike Smith return(NULL); 53715e32d5dSMike Smith bzero(ad, sizeof(*ad)); 53815e32d5dSMike Smith 53915e32d5dSMike Smith resource_list_init(&ad->ad_rl); 54015e32d5dSMike Smith 54115e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 54215e32d5dSMike Smith if (child != NULL) 54315e32d5dSMike Smith device_set_ivars(child, ad); 54415e32d5dSMike Smith return(child); 54515e32d5dSMike Smith } 54615e32d5dSMike Smith 54715e32d5dSMike Smith static int 54815e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 54915e32d5dSMike Smith { 55015e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 55115e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 55215e32d5dSMike Smith int retval = 0; 55315e32d5dSMike Smith 55415e32d5dSMike Smith retval += bus_print_child_header(bus, child); 5559ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 5569ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 5579ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 5589ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 55915e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 56015e32d5dSMike Smith 56115e32d5dSMike Smith return(retval); 56215e32d5dSMike Smith } 56315e32d5dSMike Smith 56415e32d5dSMike Smith 56515e32d5dSMike Smith /* 56615e32d5dSMike Smith * Handle per-device ivars 56715e32d5dSMike Smith */ 56815e32d5dSMike Smith static int 56915e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 57015e32d5dSMike Smith { 57115e32d5dSMike Smith struct acpi_device *ad; 57215e32d5dSMike Smith 57315e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 57415e32d5dSMike Smith printf("device has no ivars\n"); 57515e32d5dSMike Smith return(ENOENT); 57615e32d5dSMike Smith } 57715e32d5dSMike Smith 57815e32d5dSMike Smith switch(index) { 57915e32d5dSMike Smith /* ACPI ivars */ 58015e32d5dSMike Smith case ACPI_IVAR_HANDLE: 58115e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 58215e32d5dSMike Smith break; 58315e32d5dSMike Smith case ACPI_IVAR_MAGIC: 58415e32d5dSMike Smith *(int *)result = ad->ad_magic; 58515e32d5dSMike Smith break; 58615e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 58715e32d5dSMike Smith *(void **)result = ad->ad_private; 58815e32d5dSMike Smith break; 58915e32d5dSMike Smith 59032d18aa5SMike Smith /* ISA compatibility */ 59132d18aa5SMike Smith case ISA_IVAR_VENDORID: 59232d18aa5SMike Smith case ISA_IVAR_SERIAL: 59332d18aa5SMike Smith case ISA_IVAR_COMPATID: 59432d18aa5SMike Smith *(int *)result = -1; 59532d18aa5SMike Smith break; 59632d18aa5SMike Smith 59732d18aa5SMike Smith case ISA_IVAR_LOGICALID: 59832d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 59932d18aa5SMike Smith break; 60032d18aa5SMike Smith 60115e32d5dSMike Smith default: 60215e32d5dSMike Smith return(ENOENT); 60315e32d5dSMike Smith } 60415e32d5dSMike Smith return(0); 60515e32d5dSMike Smith } 60615e32d5dSMike Smith 60715e32d5dSMike Smith static int 60815e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 60915e32d5dSMike Smith { 61015e32d5dSMike Smith struct acpi_device *ad; 61115e32d5dSMike Smith 61215e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 61315e32d5dSMike Smith printf("device has no ivars\n"); 61415e32d5dSMike Smith return(ENOENT); 61515e32d5dSMike Smith } 61615e32d5dSMike Smith 61715e32d5dSMike Smith switch(index) { 61815e32d5dSMike Smith /* ACPI ivars */ 61915e32d5dSMike Smith case ACPI_IVAR_HANDLE: 62015e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 62115e32d5dSMike Smith break; 62215e32d5dSMike Smith case ACPI_IVAR_MAGIC: 62315e32d5dSMike Smith ad->ad_magic = (int )value; 62415e32d5dSMike Smith break; 62515e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 62615e32d5dSMike Smith ad->ad_private = (void *)value; 62715e32d5dSMike Smith break; 62815e32d5dSMike Smith 62915e32d5dSMike Smith default: 6302ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 63115e32d5dSMike Smith return(ENOENT); 63215e32d5dSMike Smith } 63315e32d5dSMike Smith return(0); 63415e32d5dSMike Smith } 63515e32d5dSMike Smith 63615e32d5dSMike Smith /* 63715e32d5dSMike Smith * Handle child resource allocation/removal 63815e32d5dSMike Smith */ 63915e32d5dSMike Smith static int 64015e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 64115e32d5dSMike Smith { 64215e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 64315e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 64415e32d5dSMike Smith 64515e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 64615e32d5dSMike Smith 64715e32d5dSMike Smith return(0); 64815e32d5dSMike Smith } 64915e32d5dSMike Smith 65015e32d5dSMike Smith static int 65115e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 65215e32d5dSMike Smith { 65315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 65415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 65515e32d5dSMike Smith struct resource_list_entry *rle; 65615e32d5dSMike Smith 65715e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 65815e32d5dSMike Smith if (!rle) 65915e32d5dSMike Smith return(ENOENT); 66015e32d5dSMike Smith 66115e32d5dSMike Smith if (startp) 66215e32d5dSMike Smith *startp = rle->start; 66315e32d5dSMike Smith if (countp) 66415e32d5dSMike Smith *countp = rle->count; 66515e32d5dSMike Smith 66615e32d5dSMike Smith return(0); 66715e32d5dSMike Smith } 66815e32d5dSMike Smith 66915e32d5dSMike Smith static struct resource * 67015e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 67115e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 67215e32d5dSMike Smith { 67315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 67415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 67515e32d5dSMike Smith 67615e32d5dSMike Smith return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 67715e32d5dSMike Smith } 67815e32d5dSMike Smith 67915e32d5dSMike Smith static int 68015e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 68115e32d5dSMike Smith { 68215e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 68315e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 68415e32d5dSMike Smith 68515e32d5dSMike Smith return(resource_list_release(rl, bus, child, type, rid, r)); 68615e32d5dSMike Smith } 68715e32d5dSMike Smith 68815e32d5dSMike Smith /* 689bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 690bc0f2195SMike Smith */ 691bc0f2195SMike Smith #define PNP_EISAID(s) \ 692bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 693bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 694bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 695bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 696bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 697bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 698bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 699bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 700bc0f2195SMike Smith 70132d18aa5SMike Smith static u_int32_t 70232d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 703bc0f2195SMike Smith { 704bc0f2195SMike Smith ACPI_HANDLE h; 705bc0f2195SMike Smith ACPI_DEVICE_INFO devinfo; 706bc0f2195SMike Smith ACPI_STATUS error; 707bc0f2195SMike Smith u_int32_t pnpid; 708fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 70932d18aa5SMike Smith 710b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 71132d18aa5SMike Smith 71232d18aa5SMike Smith pnpid = 0; 71332d18aa5SMike Smith ACPI_LOCK; 71432d18aa5SMike Smith 71532d18aa5SMike Smith /* fetch and validate the HID */ 71632d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 71732d18aa5SMike Smith goto out; 718b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 71932d18aa5SMike Smith goto out; 72032d18aa5SMike Smith if (!(devinfo.Valid & ACPI_VALID_HID)) 72132d18aa5SMike Smith goto out; 72232d18aa5SMike Smith 72332d18aa5SMike Smith pnpid = PNP_EISAID(devinfo.HardwareId); 72432d18aa5SMike Smith out: 72532d18aa5SMike Smith ACPI_UNLOCK; 72632d18aa5SMike Smith return_VALUE(pnpid); 72732d18aa5SMike Smith } 72832d18aa5SMike Smith 72932d18aa5SMike Smith static int 73032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 73132d18aa5SMike Smith { 732bc0f2195SMike Smith int result; 73332d18aa5SMike Smith u_int32_t pnpid; 734bc0f2195SMike Smith 735b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 736bc0f2195SMike Smith 737bc0f2195SMike Smith /* 738bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 739bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 740bc0f2195SMike Smith * that to happen, so don't ever return it. 741bc0f2195SMike Smith */ 742bc0f2195SMike Smith result = ENXIO; 743bc0f2195SMike Smith 744bc0f2195SMike Smith /* scan the supplied IDs for a match */ 74532d18aa5SMike Smith pnpid = acpi_isa_get_logicalid(child); 746bc0f2195SMike Smith while (ids && ids->ip_id) { 747bc0f2195SMike Smith if (pnpid == ids->ip_id) { 748bc0f2195SMike Smith result = 0; 749bc0f2195SMike Smith goto out; 750bc0f2195SMike Smith } 751bc0f2195SMike Smith ids++; 752bc0f2195SMike Smith } 753bc0f2195SMike Smith out: 754bc0f2195SMike Smith return_VALUE(result); 755bc0f2195SMike Smith } 756bc0f2195SMike Smith 757bc0f2195SMike Smith /* 75815e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 75915e32d5dSMike Smith * 7607d3bcec9SMike Smith * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 7617d3bcec9SMike Smith * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 76215e32d5dSMike Smith */ 76315e32d5dSMike Smith static void 76415e32d5dSMike Smith acpi_probe_children(device_t bus) 76515e32d5dSMike Smith { 76615e32d5dSMike Smith ACPI_HANDLE parent; 7677d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 76815e32d5dSMike Smith int i; 76915e32d5dSMike Smith 770b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 771cb9b0d80SMike Smith ACPI_ASSERTLOCK; 7720ae55423SMike Smith 77315e32d5dSMike Smith /* 77415e32d5dSMike Smith * Create any static children by calling device identify methods. 77515e32d5dSMike Smith */ 7764c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 77715e32d5dSMike Smith bus_generic_probe(bus); 77815e32d5dSMike Smith 77915e32d5dSMike Smith /* 78015e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 78115e32d5dSMike Smith * we find. 78215e32d5dSMike Smith * 78315e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 78415e32d5dSMike Smith * we want to create nodes for all devices, not just those that are currently 78515e32d5dSMike Smith * present. (This assumes that we don't want to create/remove devices as they 78615e32d5dSMike Smith * appear, which might be smarter.) 78715e32d5dSMike Smith */ 7884c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 78915e32d5dSMike Smith for (i = 0; scopes[i] != NULL; i++) 790b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent))) 79115e32d5dSMike Smith AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 79215e32d5dSMike Smith 79315e32d5dSMike Smith /* 79415e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 79515e32d5dSMike Smith */ 7964c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 79715e32d5dSMike Smith bus_generic_attach(bus); 79815e32d5dSMike Smith 79915e32d5dSMike Smith /* 80015e32d5dSMike Smith * Some of these children may have attached others as part of their attach 80115e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 80215e32d5dSMike Smith */ 8034c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 80415e32d5dSMike Smith bus_generic_attach(bus); 8050ae55423SMike Smith 806bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 8070ae55423SMike Smith return_VOID; 80815e32d5dSMike Smith } 80915e32d5dSMike Smith 81015e32d5dSMike Smith /* 81115e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 81215e32d5dSMike Smith * it. 81315e32d5dSMike Smith */ 81415e32d5dSMike Smith static ACPI_STATUS 81515e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 81615e32d5dSMike Smith { 81715e32d5dSMike Smith ACPI_OBJECT_TYPE type; 81815e32d5dSMike Smith device_t child, bus = (device_t)context; 81915e32d5dSMike Smith 820b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 8210ae55423SMike Smith 8220ae55423SMike Smith /* 8230ae55423SMike Smith * Skip this device if we think we'll have trouble with it. 8240ae55423SMike Smith */ 8250ae55423SMike Smith if (acpi_avoid(handle)) 8260ae55423SMike Smith return_ACPI_STATUS(AE_OK); 8270ae55423SMike Smith 828b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 82915e32d5dSMike Smith switch(type) { 83015e32d5dSMike Smith case ACPI_TYPE_DEVICE: 83115e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 83215e32d5dSMike Smith case ACPI_TYPE_THERMAL: 83315e32d5dSMike Smith case ACPI_TYPE_POWER: 8340ae55423SMike Smith if (acpi_disabled("children")) 8350ae55423SMike Smith break; 83615e32d5dSMike Smith /* 83715e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 83815e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 83915e32d5dSMike Smith */ 8404c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle))); 84115e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 842bc0f2195SMike Smith if (child == NULL) 843bc0f2195SMike Smith break; 84415e32d5dSMike Smith acpi_set_handle(child, handle); 845bc0f2195SMike Smith 846bc0f2195SMike Smith /* 847b519f9d6SMike Smith * Check that the device is present. If it's not present, 848b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 849b519f9d6SMike Smith * the handle, but we don't probe it). 850b519f9d6SMike Smith */ 85123b4e251SMitsuru IWASAKI if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) { 852b519f9d6SMike Smith device_disable(child); 853b519f9d6SMike Smith break; 854b519f9d6SMike Smith } 855b519f9d6SMike Smith 856b519f9d6SMike Smith /* 857bc0f2195SMike Smith * Get the device's resource settings and attach them. 858bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 859bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 860bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 861bc0f2195SMike Smith * device not to have any resources. 862bc0f2195SMike Smith */ 863bc0f2195SMike Smith acpi_parse_resources(child, handle, &acpi_res_parse_set); 864bc0f2195SMike Smith 865bc0f2195SMike Smith /* if we're debugging, probe/attach now rather than later */ 866b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 8670ae55423SMike Smith break; 86815e32d5dSMike Smith } 86915e32d5dSMike Smith } 8700ae55423SMike Smith return_ACPI_STATUS(AE_OK); 87115e32d5dSMike Smith } 87215e32d5dSMike Smith 87315e32d5dSMike Smith static void 87415e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 87515e32d5dSMike Smith { 876cb9b0d80SMike Smith 877c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 878c6a78e98STakanori Watanabe 879cb9b0d80SMike Smith ACPI_ASSERTLOCK; 880cb9b0d80SMike Smith 88115e32d5dSMike Smith /* 8820ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 88315e32d5dSMike Smith * will be turned on again on some laptops. 88415e32d5dSMike Smith * 88515e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 88615e32d5dSMike Smith * before powering down, since we may still need ACPI during the 88715e32d5dSMike Smith * shutdown process. 88815e32d5dSMike Smith */ 889c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 890c6a78e98STakanori Watanabe acpi_Disable(sc); 89115e32d5dSMike Smith } 89215e32d5dSMike Smith 89315e32d5dSMike Smith static void 89415e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 89515e32d5dSMike Smith { 89615e32d5dSMike Smith ACPI_STATUS status; 89715e32d5dSMike Smith 898cb9b0d80SMike Smith ACPI_ASSERTLOCK; 899cb9b0d80SMike Smith 9005f3e4175SMitsuru IWASAKI if (howto & RB_POWEROFF) { 90115e32d5dSMike Smith printf("Power system off using ACPI...\n"); 902b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) { 903b9c780d6SMitsuru IWASAKI printf("AcpiEnterSleepStatePrep failed - %s\n", 904b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 905b9c780d6SMitsuru IWASAKI return; 906b9c780d6SMitsuru IWASAKI } 907b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) { 908bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 90915e32d5dSMike Smith } else { 91015e32d5dSMike Smith DELAY(1000000); 91115e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 91215e32d5dSMike Smith } 913494ae560SMitsuru IWASAKI } else { 914494ae560SMitsuru IWASAKI printf("Terminate ACPI\n"); 915494ae560SMitsuru IWASAKI AcpiTerminate(); 91615e32d5dSMike Smith } 91715e32d5dSMike Smith } 91815e32d5dSMike Smith 91913d4f7baSMitsuru IWASAKI static void 92013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 92113d4f7baSMitsuru IWASAKI { 92213d4f7baSMitsuru IWASAKI static int first_time = 1; 92313d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 92413d4f7baSMitsuru IWASAKI 925cb9b0d80SMike Smith ACPI_ASSERTLOCK; 926cb9b0d80SMike Smith 92713d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 928cb9b0d80SMike Smith if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 92943896e91SMike Smith AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0); 93013d4f7baSMitsuru IWASAKI AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 93113d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 93213d4f7baSMitsuru IWASAKI acpi_eventhandler_power_button_for_sleep, sc); 93313d4f7baSMitsuru IWASAKI if (first_time) { 93413d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "power"); 93513d4f7baSMitsuru IWASAKI } 93613d4f7baSMitsuru IWASAKI } 937cb9b0d80SMike Smith if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 93843896e91SMike Smith AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0); 93913d4f7baSMitsuru IWASAKI AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 94013d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 94113d4f7baSMitsuru IWASAKI acpi_eventhandler_sleep_button_for_sleep, sc); 94213d4f7baSMitsuru IWASAKI if (first_time) { 94313d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 94413d4f7baSMitsuru IWASAKI } 94513d4f7baSMitsuru IWASAKI } 94613d4f7baSMitsuru IWASAKI 94713d4f7baSMitsuru IWASAKI first_time = 0; 94813d4f7baSMitsuru IWASAKI } 94913d4f7baSMitsuru IWASAKI 95015e32d5dSMike Smith /* 951c5ba0be4SMike Smith * Returns true if the device is actually present and should 952c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 953c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 954c5ba0be4SMike Smith */ 955c5ba0be4SMike Smith BOOLEAN 956c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 957c5ba0be4SMike Smith { 958c5ba0be4SMike Smith ACPI_HANDLE h; 959c5ba0be4SMike Smith ACPI_DEVICE_INFO devinfo; 960c5ba0be4SMike Smith ACPI_STATUS error; 961c5ba0be4SMike Smith 962cb9b0d80SMike Smith ACPI_ASSERTLOCK; 963cb9b0d80SMike Smith 964c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 965c5ba0be4SMike Smith return(FALSE); 966b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 967c5ba0be4SMike Smith return(FALSE); 96843896e91SMike Smith /* if no _STA method, must be present */ 96943896e91SMike Smith if (!(devinfo.Valid & ACPI_VALID_STA)) 97043896e91SMike Smith return(TRUE); 97143896e91SMike Smith /* return true for 'present' and 'functioning' */ 97243896e91SMike Smith if ((devinfo.CurrentStatus & 0x9) == 0x9) 973c5ba0be4SMike Smith return(TRUE); 974c5ba0be4SMike Smith return(FALSE); 975c5ba0be4SMike Smith } 976c5ba0be4SMike Smith 977c5ba0be4SMike Smith /* 978b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 979b53f2771SMike Smith */ 980b53f2771SMike Smith BOOLEAN 981b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 982b53f2771SMike Smith { 983b53f2771SMike Smith ACPI_HANDLE h; 984b53f2771SMike Smith ACPI_DEVICE_INFO devinfo; 985b53f2771SMike Smith ACPI_STATUS error; 986b53f2771SMike Smith 987b53f2771SMike Smith ACPI_ASSERTLOCK; 988b53f2771SMike Smith 989b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 990b53f2771SMike Smith return(FALSE); 991b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 992b53f2771SMike Smith return(FALSE); 993b53f2771SMike Smith /* if no _STA method, must be present */ 994b53f2771SMike Smith if (!(devinfo.Valid & ACPI_VALID_STA)) 995b53f2771SMike Smith return(TRUE); 996b53f2771SMike Smith /* return true for 'present' and 'functioning' */ 997b53f2771SMike Smith if ((devinfo.CurrentStatus & 0x19) == 0x19) 998b53f2771SMike Smith return(TRUE); 999b53f2771SMike Smith return(FALSE); 1000b53f2771SMike Smith } 1001b53f2771SMike Smith 1002b53f2771SMike Smith /* 100315e32d5dSMike Smith * Match a HID string against a device 100415e32d5dSMike Smith */ 100515e32d5dSMike Smith BOOLEAN 100615e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 100715e32d5dSMike Smith { 100815e32d5dSMike Smith ACPI_HANDLE h; 100915e32d5dSMike Smith ACPI_DEVICE_INFO devinfo; 101015e32d5dSMike Smith ACPI_STATUS error; 1011ed136da6SDoug Rabson int cid; 101215e32d5dSMike Smith 1013cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1014cb9b0d80SMike Smith 10154d332989SMike Smith if (hid == NULL) 101615e32d5dSMike Smith return(FALSE); 101715e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 101815e32d5dSMike Smith return(FALSE); 1019b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 102015e32d5dSMike Smith return(FALSE); 10214d332989SMike Smith if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 102215e32d5dSMike Smith return(TRUE); 1023b53f2771SMike Smith if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1024ed136da6SDoug Rabson return(FALSE); 1025ed136da6SDoug Rabson if (cid == PNP_EISAID(hid)) 1026ed136da6SDoug Rabson return(TRUE); 102715e32d5dSMike Smith return(FALSE); 102815e32d5dSMike Smith } 102915e32d5dSMike Smith 103015e32d5dSMike Smith /* 1031c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1032c5ba0be4SMike Smith * or one if its parents. 1033c5ba0be4SMike Smith */ 1034c5ba0be4SMike Smith ACPI_STATUS 1035c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1036c5ba0be4SMike Smith { 1037c5ba0be4SMike Smith ACPI_HANDLE r; 1038c5ba0be4SMike Smith ACPI_STATUS status; 1039c5ba0be4SMike Smith 1040cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1041cb9b0d80SMike Smith 1042c5ba0be4SMike Smith /* walk back up the tree to the root */ 1043c5ba0be4SMike Smith for (;;) { 1044b53f2771SMike Smith if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) { 1045c5ba0be4SMike Smith *result = r; 1046c5ba0be4SMike Smith return(AE_OK); 1047c5ba0be4SMike Smith } 1048c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1049c5ba0be4SMike Smith return(AE_OK); 1050b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1051c5ba0be4SMike Smith return(AE_NOT_FOUND); 1052c5ba0be4SMike Smith parent = r; 1053c5ba0be4SMike Smith } 1054c5ba0be4SMike Smith } 1055c5ba0be4SMike Smith 1056c5ba0be4SMike Smith /* 1057c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1058c5ba0be4SMike Smith */ 1059c5ba0be4SMike Smith ACPI_BUFFER * 1060c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1061c5ba0be4SMike Smith { 1062c5ba0be4SMike Smith ACPI_BUFFER *buf; 1063c5ba0be4SMike Smith 1064c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1065c5ba0be4SMike Smith return(NULL); 1066c5ba0be4SMike Smith buf->Length = size; 1067c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1068c5ba0be4SMike Smith return(buf); 1069c5ba0be4SMike Smith } 1070c5ba0be4SMike Smith 1071c5ba0be4SMike Smith /* 1072c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1073c5ba0be4SMike Smith */ 1074c5ba0be4SMike Smith ACPI_STATUS 1075c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1076c5ba0be4SMike Smith { 1077c5ba0be4SMike Smith ACPI_STATUS error; 1078c5ba0be4SMike Smith ACPI_BUFFER buf; 1079c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1080c5ba0be4SMike Smith 1081cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1082cb9b0d80SMike Smith 1083c5ba0be4SMike Smith if (handle == NULL) 1084c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 10850c7da7acSJohn Baldwin 10860c7da7acSJohn Baldwin /* 10870c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 10880c7da7acSJohn Baldwin * a method that will return an Integer. 10890c7da7acSJohn Baldwin */ 1090c5ba0be4SMike Smith buf.Pointer = ¶m; 1091c5ba0be4SMike Smith buf.Length = sizeof(param); 1092b53f2771SMike Smith if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1093c5ba0be4SMike Smith if (param.Type == ACPI_TYPE_INTEGER) { 1094c5ba0be4SMike Smith *number = param.Integer.Value; 1095c5ba0be4SMike Smith } else { 1096c5ba0be4SMike Smith error = AE_TYPE; 1097c5ba0be4SMike Smith } 1098c5ba0be4SMike Smith } 10990c7da7acSJohn Baldwin 11000c7da7acSJohn Baldwin /* 11010c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 11020c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 11030c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 11040c7da7acSJohn Baldwin * convert it into an Integer as best we can. 11050c7da7acSJohn Baldwin * 11060c7da7acSJohn Baldwin * This is a hack. 11070c7da7acSJohn Baldwin */ 11080c7da7acSJohn Baldwin if (error == AE_BUFFER_OVERFLOW) { 1109b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 11100c7da7acSJohn Baldwin error = AE_NO_MEMORY; 11110c7da7acSJohn Baldwin } else { 1112b53f2771SMike Smith if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1113c573e654SMitsuru IWASAKI error = acpi_ConvertBufferToInteger(&buf, number); 11140c7da7acSJohn Baldwin } 11150c7da7acSJohn Baldwin } 11160c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 11170c7da7acSJohn Baldwin } 1118c5ba0be4SMike Smith return(error); 1119c5ba0be4SMike Smith } 1120c5ba0be4SMike Smith 1121c573e654SMitsuru IWASAKI ACPI_STATUS 1122c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1123c573e654SMitsuru IWASAKI { 1124c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1125c573e654SMitsuru IWASAKI int i; 1126c573e654SMitsuru IWASAKI 1127c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1128c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1129c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1130c573e654SMitsuru IWASAKI return(AE_OK); 1131c573e654SMitsuru IWASAKI } 1132c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1133c573e654SMitsuru IWASAKI return(AE_TYPE); 1134c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1135c573e654SMitsuru IWASAKI return(AE_BAD_DATA); 1136c573e654SMitsuru IWASAKI *number = 0; 1137c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1138c573e654SMitsuru IWASAKI *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1139c573e654SMitsuru IWASAKI return(AE_OK); 1140c573e654SMitsuru IWASAKI } 1141c573e654SMitsuru IWASAKI 1142c5ba0be4SMike Smith /* 1143c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1144c5ba0be4SMike Smith * function for each element. 1145c5ba0be4SMike Smith * 1146c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1147c5ba0be4SMike Smith */ 1148c5ba0be4SMike Smith ACPI_STATUS 1149c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 1150c5ba0be4SMike Smith { 1151c5ba0be4SMike Smith ACPI_OBJECT *comp; 1152c5ba0be4SMike Smith int i; 1153c5ba0be4SMike Smith 1154c5ba0be4SMike Smith if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 1155c5ba0be4SMike Smith return(AE_BAD_PARAMETER); 1156c5ba0be4SMike Smith 1157c5ba0be4SMike Smith /* iterate over components */ 1158c5ba0be4SMike Smith for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 1159c5ba0be4SMike Smith func(comp, arg); 1160c5ba0be4SMike Smith 1161c5ba0be4SMike Smith return(AE_OK); 1162c5ba0be4SMike Smith } 1163c5ba0be4SMike Smith 11646f69255bSMike Smith /* 11656f69255bSMike Smith * Find the (index)th resource object in a set. 11666f69255bSMike Smith */ 11676f69255bSMike Smith ACPI_STATUS 11686d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 11696f69255bSMike Smith { 11706d63101aSMike Smith ACPI_RESOURCE *rp; 11716f69255bSMike Smith int i; 11726f69255bSMike Smith 11736d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 11746f69255bSMike Smith i = index; 11756d63101aSMike Smith while (i-- > 0) { 11766f69255bSMike Smith /* range check */ 11776d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 11786f69255bSMike Smith return(AE_BAD_PARAMETER); 11796d63101aSMike Smith /* check for terminator */ 11806d63101aSMike Smith if ((rp->Id == ACPI_RSTYPE_END_TAG) || 11816d63101aSMike Smith (rp->Length == 0)) 11826d63101aSMike Smith return(AE_NOT_FOUND); 11836d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 11846f69255bSMike Smith } 11856f69255bSMike Smith if (resp != NULL) 11866d63101aSMike Smith *resp = rp; 11876d63101aSMike Smith return(AE_OK); 11886d63101aSMike Smith } 11896d63101aSMike Smith 11906d63101aSMike Smith /* 11916d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 11926d63101aSMike Smith * 11936d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 11946d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 11956d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 11966d63101aSMike Smith * resources. 11976d63101aSMike Smith */ 11986d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 11996d63101aSMike Smith 12006d63101aSMike Smith ACPI_STATUS 12016d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 12026d63101aSMike Smith { 12036d63101aSMike Smith ACPI_RESOURCE *rp; 12046d63101aSMike Smith void *newp; 12056d63101aSMike Smith 12066d63101aSMike Smith /* 12076d63101aSMike Smith * Initialise the buffer if necessary. 12086d63101aSMike Smith */ 12096d63101aSMike Smith if (buf->Pointer == NULL) { 12106d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 12116d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 12126d63101aSMike Smith return(AE_NO_MEMORY); 12136d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 12146d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 12156d63101aSMike Smith rp->Length = 0; 12166d63101aSMike Smith } 12176d63101aSMike Smith if (res == NULL) 12186d63101aSMike Smith return(AE_OK); 12196d63101aSMike Smith 12206d63101aSMike Smith /* 12216d63101aSMike Smith * Scan the current buffer looking for the terminator. 12226d63101aSMike Smith * This will either find the terminator or hit the end 12236d63101aSMike Smith * of the buffer and return an error. 12246d63101aSMike Smith */ 12256d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 12266d63101aSMike Smith for (;;) { 12276d63101aSMike Smith /* range check, don't go outside the buffer */ 12286d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 12296d63101aSMike Smith return(AE_BAD_PARAMETER); 12306d63101aSMike Smith if ((rp->Id == ACPI_RSTYPE_END_TAG) || 12316d63101aSMike Smith (rp->Length == 0)) { 12326d63101aSMike Smith break; 12336d63101aSMike Smith } 12346d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 12356d63101aSMike Smith } 12366d63101aSMike Smith 12376d63101aSMike Smith /* 12386d63101aSMike Smith * Check the size of the buffer and expand if required. 12396d63101aSMike Smith * 12406d63101aSMike Smith * Required size is: 12416d63101aSMike Smith * size of existing resources before terminator + 12426d63101aSMike Smith * size of new resource and header + 12436d63101aSMike Smith * size of terminator. 12446d63101aSMike Smith * 12456d63101aSMike Smith * Note that this loop should really only run once, unless 12466d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 12476d63101aSMike Smith */ 12486d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 12496d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 12506d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 12516d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 12526d63101aSMike Smith return(AE_NO_MEMORY); 12536d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1254a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1255a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 12566d63101aSMike Smith AcpiOsFree(buf->Pointer); 12576d63101aSMike Smith buf->Pointer = newp; 12586d63101aSMike Smith buf->Length += buf->Length; 12596d63101aSMike Smith } 12606d63101aSMike Smith 12616d63101aSMike Smith /* 12626d63101aSMike Smith * Insert the new resource. 12636d63101aSMike Smith */ 12646d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 12656d63101aSMike Smith 12666d63101aSMike Smith /* 12676d63101aSMike Smith * And add the terminator. 12686d63101aSMike Smith */ 12696d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 12706d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 12716d63101aSMike Smith rp->Length = 0; 12726d63101aSMike Smith 12736f69255bSMike Smith return(AE_OK); 12746f69255bSMike Smith } 1275c5ba0be4SMike Smith 1276da14ac9fSJohn Baldwin /* 1277da14ac9fSJohn Baldwin * Set interrupt model. 1278da14ac9fSJohn Baldwin */ 1279da14ac9fSJohn Baldwin ACPI_STATUS 1280da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1281da14ac9fSJohn Baldwin { 1282da14ac9fSJohn Baldwin ACPI_OBJECT_LIST ArgList; 1283da14ac9fSJohn Baldwin ACPI_OBJECT Arg; 1284da14ac9fSJohn Baldwin 1285da14ac9fSJohn Baldwin Arg.Type = ACPI_TYPE_INTEGER; 1286da14ac9fSJohn Baldwin Arg.Integer.Value = model; 1287da14ac9fSJohn Baldwin ArgList.Count = 1; 1288da14ac9fSJohn Baldwin ArgList.Pointer = &Arg; 1289da14ac9fSJohn Baldwin return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1290da14ac9fSJohn Baldwin } 1291da14ac9fSJohn Baldwin 1292ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1293ece50487SMitsuru IWASAKI 1294ece50487SMitsuru IWASAKI static void 1295ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1296ece50487SMitsuru IWASAKI { 1297ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1298ece50487SMitsuru IWASAKI } 1299c30382dfSMitsuru IWASAKI 130015e32d5dSMike Smith /* 130115e32d5dSMike Smith * Set the system sleep state 130215e32d5dSMike Smith * 130315e32d5dSMike Smith * Currently we only support S1 and S5 130415e32d5dSMike Smith */ 130515e32d5dSMike Smith ACPI_STATUS 130615e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 130715e32d5dSMike Smith { 130815e32d5dSMike Smith ACPI_STATUS status = AE_OK; 130956d8cb57SMitsuru IWASAKI UINT8 TypeA; 131056d8cb57SMitsuru IWASAKI UINT8 TypeB; 131115e32d5dSMike Smith 1312b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1313cb9b0d80SMike Smith ACPI_ASSERTLOCK; 13140ae55423SMike Smith 13156397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 13166397624dSMitsuru IWASAKI return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */ 13176397624dSMitsuru IWASAKI 1318ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1319ece50487SMitsuru IWASAKI return_ACPI_STATUS(AE_OK); 1320ece50487SMitsuru IWASAKI 132115e32d5dSMike Smith switch (state) { 132215e32d5dSMike Smith case ACPI_STATE_S0: /* XXX only for testing */ 1323b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1324bfae45aaSMike Smith device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 132515e32d5dSMike Smith } 132615e32d5dSMike Smith break; 132715e32d5dSMike Smith 132815e32d5dSMike Smith case ACPI_STATE_S1: 13296161544cSTakanori Watanabe case ACPI_STATE_S2: 13306161544cSTakanori Watanabe case ACPI_STATE_S3: 13316161544cSTakanori Watanabe case ACPI_STATE_S4: 133298479b04SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) { 133398479b04SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status)); 133456d8cb57SMitsuru IWASAKI break; 133556d8cb57SMitsuru IWASAKI } 133656d8cb57SMitsuru IWASAKI 1337a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1338a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1339a1fccb47SMitsuru IWASAKI 134015e32d5dSMike Smith /* 134115e32d5dSMike Smith * Inform all devices that we are going to sleep. 134215e32d5dSMike Smith */ 134315e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 134415e32d5dSMike Smith /* 134515e32d5dSMike Smith * Re-wake the system. 134615e32d5dSMike Smith * 134715e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 134815e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 134915e32d5dSMike Smith * current bus interface does not provide for this. 135015e32d5dSMike Smith */ 135115e32d5dSMike Smith DEVICE_RESUME(root_bus); 13520ae55423SMike Smith return_ACPI_STATUS(AE_ERROR); 135315e32d5dSMike Smith } 1354b9c780d6SMitsuru IWASAKI 1355b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) { 1356b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1357b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1358b9c780d6SMitsuru IWASAKI break; 1359b9c780d6SMitsuru IWASAKI } 1360b9c780d6SMitsuru IWASAKI 1361ff01efb5SMitsuru IWASAKI if (sc->acpi_sleep_delay > 0) { 1362ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1363ff01efb5SMitsuru IWASAKI } 1364ff01efb5SMitsuru IWASAKI 13656161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 13666161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 13676161544cSTakanori Watanabe 1368a1fccb47SMitsuru IWASAKI /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */ 1369a1fccb47SMitsuru IWASAKI if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) { 13706161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1371a1fccb47SMitsuru IWASAKI } 13726161544cSTakanori Watanabe 13736161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1374964679ceSMitsuru IWASAKI if (state == ACPI_STATE_S4) { 1375964679ceSMitsuru IWASAKI AcpiEnable(); 13766161544cSTakanori Watanabe } 13776161544cSTakanori Watanabe } else { 1378b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1379bfae45aaSMike Smith device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1380c30382dfSMitsuru IWASAKI break; 138115e32d5dSMike Smith } 13826161544cSTakanori Watanabe } 1383ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 138415e32d5dSMike Smith DEVICE_RESUME(root_bus); 138515e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 138613d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 138715e32d5dSMike Smith break; 138815e32d5dSMike Smith 138915e32d5dSMike Smith case ACPI_STATE_S5: 139015e32d5dSMike Smith /* 139115e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 139215e32d5dSMike Smith * shutdown handlers. 139315e32d5dSMike Smith */ 139415e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 139515e32d5dSMike Smith break; 139615e32d5dSMike Smith 139715e32d5dSMike Smith default: 139815e32d5dSMike Smith status = AE_BAD_PARAMETER; 139915e32d5dSMike Smith break; 140015e32d5dSMike Smith } 1401ece50487SMitsuru IWASAKI 1402ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1403ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1404ece50487SMitsuru IWASAKI 14050ae55423SMike Smith return_ACPI_STATUS(status); 140615e32d5dSMike Smith } 140715e32d5dSMike Smith 140815e32d5dSMike Smith /* 140915e32d5dSMike Smith * Enable/Disable ACPI 141015e32d5dSMike Smith */ 141115e32d5dSMike Smith ACPI_STATUS 141215e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 141315e32d5dSMike Smith { 141415e32d5dSMike Smith ACPI_STATUS status; 141515e32d5dSMike Smith u_int32_t flags; 141615e32d5dSMike Smith 1417b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1418cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14190ae55423SMike Smith 142015e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 142115e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 142215e32d5dSMike Smith if (!sc->acpi_enabled) { 142315e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 142415e32d5dSMike Smith } else { 142515e32d5dSMike Smith status = AE_OK; 142615e32d5dSMike Smith } 142715e32d5dSMike Smith if (status == AE_OK) 142815e32d5dSMike Smith sc->acpi_enabled = 1; 14290ae55423SMike Smith return_ACPI_STATUS(status); 143015e32d5dSMike Smith } 143115e32d5dSMike Smith 143215e32d5dSMike Smith ACPI_STATUS 143315e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 143415e32d5dSMike Smith { 143515e32d5dSMike Smith ACPI_STATUS status; 143615e32d5dSMike Smith 1437b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1438cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14390ae55423SMike Smith 144015e32d5dSMike Smith if (sc->acpi_enabled) { 144115e32d5dSMike Smith status = AcpiDisable(); 144215e32d5dSMike Smith } else { 144315e32d5dSMike Smith status = AE_OK; 144415e32d5dSMike Smith } 144515e32d5dSMike Smith if (status == AE_OK) 144615e32d5dSMike Smith sc->acpi_enabled = 0; 14470ae55423SMike Smith return_ACPI_STATUS(status); 144815e32d5dSMike Smith } 144915e32d5dSMike Smith 145015e32d5dSMike Smith /* 145115e32d5dSMike Smith * ACPI Event Handlers 145215e32d5dSMike Smith */ 145315e32d5dSMike Smith 145415e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 145515e32d5dSMike Smith 145615e32d5dSMike Smith static void 145715e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 145815e32d5dSMike Smith { 1459fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1460b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 146115e32d5dSMike Smith 1462cb9b0d80SMike Smith ACPI_LOCK; 1463a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 146415e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 1465cb9b0d80SMike Smith ACPI_UNLOCK; 14660ae55423SMike Smith return_VOID; 146715e32d5dSMike Smith } 146815e32d5dSMike Smith 146915e32d5dSMike Smith static void 147015e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 147115e32d5dSMike Smith { 1472fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1473b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 14740ae55423SMike Smith 147515e32d5dSMike Smith /* Well, what to do? :-) */ 14760ae55423SMike Smith 1477cb9b0d80SMike Smith ACPI_LOCK; 1478cb9b0d80SMike Smith ACPI_UNLOCK; 1479cb9b0d80SMike Smith 14800ae55423SMike Smith return_VOID; 148115e32d5dSMike Smith } 148215e32d5dSMike Smith 148315e32d5dSMike Smith /* 148415e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 148515e32d5dSMike Smith */ 148615e32d5dSMike Smith UINT32 148715e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context) 148815e32d5dSMike Smith { 148915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 149015e32d5dSMike Smith 1491b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 14920ae55423SMike Smith 149315e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 14940ae55423SMike Smith 1495b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 149615e32d5dSMike Smith } 149715e32d5dSMike Smith 149815e32d5dSMike Smith UINT32 149915e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context) 150015e32d5dSMike Smith { 150115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 150215e32d5dSMike Smith 1503b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15040ae55423SMike Smith 150515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 15060ae55423SMike Smith 1507b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 150815e32d5dSMike Smith } 150915e32d5dSMike Smith 151015e32d5dSMike Smith UINT32 151115e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context) 151215e32d5dSMike Smith { 151315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 151415e32d5dSMike Smith 1515b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15160ae55423SMike Smith 151715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 15180ae55423SMike Smith 1519b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 152015e32d5dSMike Smith } 152115e32d5dSMike Smith 152215e32d5dSMike Smith UINT32 152315e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context) 152415e32d5dSMike Smith { 152515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 152615e32d5dSMike Smith 1527b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15280ae55423SMike Smith 152915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 15300ae55423SMike Smith 1531b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 153215e32d5dSMike Smith } 153315e32d5dSMike Smith 153415e32d5dSMike Smith /* 153515e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 153615e32d5dSMike Smith */ 153715e32d5dSMike Smith struct acpi_staticbuf { 153815e32d5dSMike Smith ACPI_BUFFER buffer; 153915e32d5dSMike Smith char data[512]; 154015e32d5dSMike Smith }; 154115e32d5dSMike Smith 154215e32d5dSMike Smith char * 154315e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 154415e32d5dSMike Smith { 154515e32d5dSMike Smith static struct acpi_staticbuf buf; 154615e32d5dSMike Smith 1547cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1548cb9b0d80SMike Smith 154915e32d5dSMike Smith buf.buffer.Length = 512; 155015e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 155115e32d5dSMike Smith 1552b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 155315e32d5dSMike Smith return(buf.buffer.Pointer); 155415e32d5dSMike Smith return("(unknown path)"); 155515e32d5dSMike Smith } 155615e32d5dSMike Smith 155715e32d5dSMike Smith /* 155815e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 155915e32d5dSMike Smith * parts of the namespace. 156015e32d5dSMike Smith */ 156115e32d5dSMike Smith int 156215e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 156315e32d5dSMike Smith { 15643c600cfbSJohn Baldwin char *cp, *env, *np; 156515e32d5dSMike Smith int len; 156615e32d5dSMike Smith 156715e32d5dSMike Smith np = acpi_name(handle); 156815e32d5dSMike Smith if (*np == '\\') 156915e32d5dSMike Smith np++; 15703c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 157115e32d5dSMike Smith return(0); 157215e32d5dSMike Smith 157315e32d5dSMike Smith /* scan the avoid list checking for a match */ 15743c600cfbSJohn Baldwin cp = env; 157515e32d5dSMike Smith for (;;) { 157615e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 157715e32d5dSMike Smith cp++; 157815e32d5dSMike Smith if (*cp == 0) 157915e32d5dSMike Smith break; 158015e32d5dSMike Smith len = 0; 158115e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 158215e32d5dSMike Smith len++; 1583d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 15843c600cfbSJohn Baldwin freeenv(env); 15850ae55423SMike Smith return(1); 1586d786139cSMaxime Henrion } 15870ae55423SMike Smith cp += len; 15880ae55423SMike Smith } 15893c600cfbSJohn Baldwin freeenv(env); 15900ae55423SMike Smith return(0); 15910ae55423SMike Smith } 15920ae55423SMike Smith 15930ae55423SMike Smith /* 15940ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 15950ae55423SMike Smith */ 15960ae55423SMike Smith int 15970ae55423SMike Smith acpi_disabled(char *subsys) 15980ae55423SMike Smith { 159978689b15SMaxime Henrion char *cp, *env; 16000ae55423SMike Smith int len; 16010ae55423SMike Smith 160278689b15SMaxime Henrion if ((env = getenv("debug.acpi.disable")) == NULL) 16030ae55423SMike Smith return(0); 160478689b15SMaxime Henrion if (!strcmp(env, "all")) { 160578689b15SMaxime Henrion freeenv(env); 16060ae55423SMike Smith return(1); 1607d786139cSMaxime Henrion } 16080ae55423SMike Smith 16090ae55423SMike Smith /* scan the disable list checking for a match */ 161078689b15SMaxime Henrion cp = env; 16110ae55423SMike Smith for (;;) { 16120ae55423SMike Smith while ((*cp != 0) && isspace(*cp)) 16130ae55423SMike Smith cp++; 16140ae55423SMike Smith if (*cp == 0) 16150ae55423SMike Smith break; 16160ae55423SMike Smith len = 0; 16170ae55423SMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 16180ae55423SMike Smith len++; 1619d786139cSMaxime Henrion if (!strncmp(cp, subsys, len)) { 162078689b15SMaxime Henrion freeenv(env); 162115e32d5dSMike Smith return(1); 1622d786139cSMaxime Henrion } 162315e32d5dSMike Smith cp += len; 162415e32d5dSMike Smith } 162578689b15SMaxime Henrion freeenv(env); 162615e32d5dSMike Smith return(0); 162715e32d5dSMike Smith } 162815e32d5dSMike Smith 162915e32d5dSMike Smith /* 1630a1fccb47SMitsuru IWASAKI * Device wake capability enable/disable. 1631a1fccb47SMitsuru IWASAKI */ 1632a1fccb47SMitsuru IWASAKI void 1633a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1634a1fccb47SMitsuru IWASAKI { 1635a1fccb47SMitsuru IWASAKI ACPI_OBJECT_LIST ArgList; 1636a1fccb47SMitsuru IWASAKI ACPI_OBJECT Arg; 1637a1fccb47SMitsuru IWASAKI 1638a1fccb47SMitsuru IWASAKI /* 1639a1fccb47SMitsuru IWASAKI * TBD: All Power Resources referenced by elements 2 through N 1640a1fccb47SMitsuru IWASAKI * of the _PRW object are put into the ON state. 1641a1fccb47SMitsuru IWASAKI */ 1642a1fccb47SMitsuru IWASAKI 1643a1fccb47SMitsuru IWASAKI /* 1644a1fccb47SMitsuru IWASAKI * enable/disable device wake function. 1645a1fccb47SMitsuru IWASAKI */ 1646a1fccb47SMitsuru IWASAKI 1647a1fccb47SMitsuru IWASAKI ArgList.Count = 1; 1648a1fccb47SMitsuru IWASAKI ArgList.Pointer = &Arg; 1649a1fccb47SMitsuru IWASAKI 1650a1fccb47SMitsuru IWASAKI Arg.Type = ACPI_TYPE_INTEGER; 1651a1fccb47SMitsuru IWASAKI Arg.Integer.Value = enable; 1652a1fccb47SMitsuru IWASAKI 1653a1fccb47SMitsuru IWASAKI (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1654a1fccb47SMitsuru IWASAKI } 1655a1fccb47SMitsuru IWASAKI 1656a1fccb47SMitsuru IWASAKI void 1657a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h) 1658a1fccb47SMitsuru IWASAKI { 1659a1fccb47SMitsuru IWASAKI struct acpi_softc *sc; 1660a1fccb47SMitsuru IWASAKI ACPI_STATUS status; 1661a1fccb47SMitsuru IWASAKI ACPI_BUFFER prw_buffer; 1662a1fccb47SMitsuru IWASAKI ACPI_OBJECT *res; 1663a1fccb47SMitsuru IWASAKI 1664a1fccb47SMitsuru IWASAKI ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1665a1fccb47SMitsuru IWASAKI 1666a1fccb47SMitsuru IWASAKI if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) { 1667a1fccb47SMitsuru IWASAKI return; 1668a1fccb47SMitsuru IWASAKI } 1669a1fccb47SMitsuru IWASAKI 1670a1fccb47SMitsuru IWASAKI /* 1671a1fccb47SMitsuru IWASAKI * _PRW object is only required for devices that have the ability 1672a1fccb47SMitsuru IWASAKI * to wake the system from a system sleeping state. 1673a1fccb47SMitsuru IWASAKI */ 1674a1fccb47SMitsuru IWASAKI prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1675a1fccb47SMitsuru IWASAKI status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1676a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) { 1677a1fccb47SMitsuru IWASAKI return; 1678a1fccb47SMitsuru IWASAKI } 1679a1fccb47SMitsuru IWASAKI 1680a1fccb47SMitsuru IWASAKI res = (ACPI_OBJECT *)prw_buffer.Pointer; 1681a1fccb47SMitsuru IWASAKI if (res == NULL) { 1682a1fccb47SMitsuru IWASAKI return; 1683a1fccb47SMitsuru IWASAKI } 1684a1fccb47SMitsuru IWASAKI 1685a1fccb47SMitsuru IWASAKI if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1686a1fccb47SMitsuru IWASAKI goto out; 1687a1fccb47SMitsuru IWASAKI } 1688a1fccb47SMitsuru IWASAKI 1689a1fccb47SMitsuru IWASAKI /* 1690a1fccb47SMitsuru IWASAKI * The element 1 of the _PRW object: 1691a1fccb47SMitsuru IWASAKI * The lowest power system sleeping state that can be entered 1692a1fccb47SMitsuru IWASAKI * while still providing wake functionality. 1693a1fccb47SMitsuru IWASAKI * The sleeping state being entered must be greater or equal to 1694a1fccb47SMitsuru IWASAKI * the power state declared in element 1 of the _PRW object. 1695a1fccb47SMitsuru IWASAKI */ 1696a1fccb47SMitsuru IWASAKI if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) { 1697a1fccb47SMitsuru IWASAKI goto out; 1698a1fccb47SMitsuru IWASAKI } 1699a1fccb47SMitsuru IWASAKI 1700a1fccb47SMitsuru IWASAKI if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) { 1701a1fccb47SMitsuru IWASAKI goto out; 1702a1fccb47SMitsuru IWASAKI } 1703a1fccb47SMitsuru IWASAKI 1704a1fccb47SMitsuru IWASAKI /* 1705a1fccb47SMitsuru IWASAKI * The element 0 of the _PRW object: 1706a1fccb47SMitsuru IWASAKI */ 1707a1fccb47SMitsuru IWASAKI switch(res->Package.Elements[0].Type) { 1708a1fccb47SMitsuru IWASAKI case ACPI_TYPE_INTEGER: 1709a1fccb47SMitsuru IWASAKI /* 1710a1fccb47SMitsuru IWASAKI * If the data type of this package element is numeric, then this 1711a1fccb47SMitsuru IWASAKI * _PRW package element is the bit index in the GPEx_EN, in the 1712a1fccb47SMitsuru IWASAKI * GPE blocks described in the FADT, of the enable bit that is 1713a1fccb47SMitsuru IWASAKI * enabled for the wake event. 1714a1fccb47SMitsuru IWASAKI */ 1715a1fccb47SMitsuru IWASAKI 1716a1fccb47SMitsuru IWASAKI status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value, 1717a1fccb47SMitsuru IWASAKI ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE); 1718a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) 1719a1fccb47SMitsuru IWASAKI printf("%s: EnableEvent Failed\n", __func__); 1720a1fccb47SMitsuru IWASAKI break; 1721a1fccb47SMitsuru IWASAKI 1722a1fccb47SMitsuru IWASAKI case ACPI_TYPE_PACKAGE: 1723a1fccb47SMitsuru IWASAKI /* XXX TBD */ 1724a1fccb47SMitsuru IWASAKI 1725a1fccb47SMitsuru IWASAKI /* 1726a1fccb47SMitsuru IWASAKI * If the data type of this package element is a package, then this 1727a1fccb47SMitsuru IWASAKI * _PRW package element is itself a package containing two 1728a1fccb47SMitsuru IWASAKI * elements. The first is an object reference to the GPE Block 1729a1fccb47SMitsuru IWASAKI * device that contains the GPE that will be triggered by the wake 1730a1fccb47SMitsuru IWASAKI * event. The second element is numeric and it contains the bit 1731a1fccb47SMitsuru IWASAKI * index in the GPEx_EN, in the GPE Block referenced by the 1732a1fccb47SMitsuru IWASAKI * first element in the package, of the enable bit that is enabled for 1733a1fccb47SMitsuru IWASAKI * the wake event. 1734a1fccb47SMitsuru IWASAKI * For example, if this field is a package then it is of the form: 1735a1fccb47SMitsuru IWASAKI * Package() {\_SB.PCI0.ISA.GPE, 2} 1736a1fccb47SMitsuru IWASAKI */ 1737a1fccb47SMitsuru IWASAKI 1738a1fccb47SMitsuru IWASAKI break; 1739a1fccb47SMitsuru IWASAKI 1740a1fccb47SMitsuru IWASAKI default: 1741a1fccb47SMitsuru IWASAKI break; 1742a1fccb47SMitsuru IWASAKI } 1743a1fccb47SMitsuru IWASAKI 1744a1fccb47SMitsuru IWASAKI out: 1745a1fccb47SMitsuru IWASAKI if (prw_buffer.Pointer != NULL) 1746a1fccb47SMitsuru IWASAKI AcpiOsFree(prw_buffer.Pointer); 1747a1fccb47SMitsuru IWASAKI return; 1748a1fccb47SMitsuru IWASAKI } 1749a1fccb47SMitsuru IWASAKI 1750a1fccb47SMitsuru IWASAKI /* 175115e32d5dSMike Smith * Control interface. 175215e32d5dSMike Smith * 17530ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 17540ae55423SMike Smith * drivers wanting to be accessible via /dev/acpi should use the register/deregister 17550ae55423SMike Smith * interface to make their handlers visible. 175615e32d5dSMike Smith */ 17570ae55423SMike Smith struct acpi_ioctl_hook 17580ae55423SMike Smith { 17590ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 17600ae55423SMike Smith u_long cmd; 17610ae55423SMike Smith int (* fn)(u_long cmd, caddr_t addr, void *arg); 17620ae55423SMike Smith void *arg; 17630ae55423SMike Smith }; 17640ae55423SMike Smith 17650ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 17660ae55423SMike Smith static int acpi_ioctl_hooks_initted; 17670ae55423SMike Smith 17680ae55423SMike Smith /* 17690ae55423SMike Smith * Register an ioctl handler. 17700ae55423SMike Smith */ 17710ae55423SMike Smith int 17720ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 17730ae55423SMike Smith { 17740ae55423SMike Smith struct acpi_ioctl_hook *hp; 17750ae55423SMike Smith 17760ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 17770ae55423SMike Smith return(ENOMEM); 17780ae55423SMike Smith hp->cmd = cmd; 17790ae55423SMike Smith hp->fn = fn; 17800ae55423SMike Smith hp->arg = arg; 17810ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 17820ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 17830ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 17840ae55423SMike Smith } 17850ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 17860ae55423SMike Smith return(0); 17870ae55423SMike Smith } 17880ae55423SMike Smith 17890ae55423SMike Smith /* 17900ae55423SMike Smith * Deregister an ioctl handler. 17910ae55423SMike Smith */ 17920ae55423SMike Smith void 17930ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 17940ae55423SMike Smith { 17950ae55423SMike Smith struct acpi_ioctl_hook *hp; 17960ae55423SMike Smith 17970ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 17980ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 17990ae55423SMike Smith break; 18000ae55423SMike Smith 18010ae55423SMike Smith if (hp != NULL) { 18020ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 18030ae55423SMike Smith free(hp, M_ACPIDEV); 18040ae55423SMike Smith } 18050ae55423SMike Smith } 18060ae55423SMike Smith 180715e32d5dSMike Smith static int 18086b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 180915e32d5dSMike Smith { 181015e32d5dSMike Smith return(0); 181115e32d5dSMike Smith } 181215e32d5dSMike Smith 181315e32d5dSMike Smith static int 18146b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 181515e32d5dSMike Smith { 181615e32d5dSMike Smith return(0); 181715e32d5dSMike Smith } 181815e32d5dSMike Smith 181915e32d5dSMike Smith static int 18206b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 182115e32d5dSMike Smith { 182215e32d5dSMike Smith struct acpi_softc *sc; 18230ae55423SMike Smith struct acpi_ioctl_hook *hp; 18240ae55423SMike Smith int error, xerror, state; 1825fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 182615e32d5dSMike Smith 1827cb9b0d80SMike Smith ACPI_LOCK; 1828cb9b0d80SMike Smith 182915e32d5dSMike Smith error = state = 0; 183015e32d5dSMike Smith sc = dev->si_drv1; 183115e32d5dSMike Smith 18320ae55423SMike Smith /* 18330ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 18340ae55423SMike Smith */ 18350ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 18360ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 18370ae55423SMike Smith if (hp->cmd == cmd) { 18380ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 18390ae55423SMike Smith if (xerror != 0) 18400ae55423SMike Smith error = xerror; 1841917d44c8SMitsuru IWASAKI goto out; 18420ae55423SMike Smith } 18430ae55423SMike Smith } 18440ae55423SMike Smith } 18450ae55423SMike Smith 18460ae55423SMike Smith /* 18470ae55423SMike Smith * Core system ioctls. 18480ae55423SMike Smith */ 184915e32d5dSMike Smith switch (cmd) { 185015e32d5dSMike Smith case ACPIIO_ENABLE: 18510ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 185215e32d5dSMike Smith error = ENXIO; 185315e32d5dSMike Smith break; 185415e32d5dSMike Smith 185515e32d5dSMike Smith case ACPIIO_DISABLE: 18560ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 185715e32d5dSMike Smith error = ENXIO; 185815e32d5dSMike Smith break; 185915e32d5dSMike Smith 186015e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 186115e32d5dSMike Smith if (!sc->acpi_enabled) { 186215e32d5dSMike Smith error = ENXIO; 186315e32d5dSMike Smith break; 186415e32d5dSMike Smith } 186515e32d5dSMike Smith state = *(int *)addr; 1866a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 186715e32d5dSMike Smith acpi_SetSleepState(sc, state); 186815e32d5dSMike Smith } else { 186915e32d5dSMike Smith error = EINVAL; 187015e32d5dSMike Smith } 187115e32d5dSMike Smith break; 187215e32d5dSMike Smith 187315e32d5dSMike Smith default: 18740ae55423SMike Smith if (error == 0) 187515e32d5dSMike Smith error = EINVAL; 187615e32d5dSMike Smith break; 187715e32d5dSMike Smith } 1878917d44c8SMitsuru IWASAKI 1879917d44c8SMitsuru IWASAKI out: 1880cb9b0d80SMike Smith ACPI_UNLOCK; 188115e32d5dSMike Smith return(error); 188215e32d5dSMike Smith } 188315e32d5dSMike Smith 18841d073b1dSJohn Baldwin static int 18851d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 18861d073b1dSJohn Baldwin { 18871d073b1dSJohn Baldwin char sleep_state[10]; 18881d073b1dSJohn Baldwin int error; 18891d073b1dSJohn Baldwin u_int new_state, old_state; 18901d073b1dSJohn Baldwin 18911d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 1892b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX+1) { 18931d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 1894cb9b0d80SMike Smith } else { 1895b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 18961d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 18971d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 1898cb9b0d80SMike Smith } 18991d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 19001d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 1901b8670bedSMitsuru IWASAKI for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) { 19021d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 19031d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 19041d073b1dSJohn Baldwin break; 1905cb9b0d80SMike Smith } 1906931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX+1) { 1907931a10c9SMitsuru IWASAKI if (new_state != old_state) { 19081d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 1909931a10c9SMitsuru IWASAKI } 1910cb9b0d80SMike Smith } else { 19111d073b1dSJohn Baldwin error = EINVAL; 19121d073b1dSJohn Baldwin } 1913cb9b0d80SMike Smith } 19141d073b1dSJohn Baldwin return(error); 19151d073b1dSJohn Baldwin } 19161d073b1dSJohn Baldwin 191715e32d5dSMike Smith #ifdef ACPI_DEBUG 19180ae55423SMike Smith /* 19190ae55423SMike Smith * Support for parsing debug options from the kernel environment. 19200ae55423SMike Smith * 19210ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 19220ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 19230ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 19240ae55423SMike Smith * prefixing the bit name with !. 19250ae55423SMike Smith */ 192615e32d5dSMike Smith struct debugtag 192715e32d5dSMike Smith { 192815e32d5dSMike Smith char *name; 192915e32d5dSMike Smith UINT32 value; 193015e32d5dSMike Smith }; 193115e32d5dSMike Smith 193215e32d5dSMike Smith static struct debugtag dbg_layer[] = { 1933c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 1934c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 1935c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 1936c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 1937c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1938c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 1939c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1940c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 1941c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 1942d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 19434c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1944d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 19454c1cdee6SMike Smith 1946cb9b0d80SMike Smith {"ACPI_BUS", ACPI_BUS}, 19474c1cdee6SMike Smith {"ACPI_SYSTEM", ACPI_SYSTEM}, 1948cb9b0d80SMike Smith {"ACPI_POWER", ACPI_POWER}, 1949cb9b0d80SMike Smith {"ACPI_EC", ACPI_EC}, 1950c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 1951c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 1952c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 19534c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 1954cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 19554c1cdee6SMike Smith {"ACPI_FAN", ACPI_FAN}, 19564c1cdee6SMike Smith 1957b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 1958c5ba0be4SMike Smith {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 195915e32d5dSMike Smith {NULL, 0} 196015e32d5dSMike Smith }; 196115e32d5dSMike Smith 196215e32d5dSMike Smith static struct debugtag dbg_level[] = { 19634c1cdee6SMike Smith {"ACPI_LV_OK", ACPI_LV_OK}, 19644c1cdee6SMike Smith {"ACPI_LV_INFO", ACPI_LV_INFO}, 19654c1cdee6SMike Smith {"ACPI_LV_WARN", ACPI_LV_WARN}, 19664c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 19674c1cdee6SMike Smith {"ACPI_LV_FATAL", ACPI_LV_FATAL}, 19684c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 19694c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 1970d62ab2f4SMitsuru IWASAKI 1971d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 19724c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 19734c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 1974d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 19754c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 19764c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 19774c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 19784c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 19794c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 19804c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 19814c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 19824c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 19834c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 19844c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 19854c1cdee6SMike Smith {"ACPI_LV_INIT", ACPI_LV_INIT}, 1986d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 1987d62ab2f4SMitsuru IWASAKI 1988d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 1989d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 1990d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 1991d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 1992d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 19934c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 1994d62ab2f4SMitsuru IWASAKI 1995d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 1996d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 1997d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 1998d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 1999d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2000d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2001d62ab2f4SMitsuru IWASAKI 2002d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 200398479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 200498479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 200598479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 200698479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 200798479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 200815e32d5dSMike Smith {NULL, 0} 200915e32d5dSMike Smith }; 201015e32d5dSMike Smith 201115e32d5dSMike Smith static void 201215e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 201315e32d5dSMike Smith { 201415e32d5dSMike Smith char *ep; 201515e32d5dSMike Smith int i, l; 20160ae55423SMike Smith int set; 201715e32d5dSMike Smith 201815e32d5dSMike Smith while (*cp) { 201915e32d5dSMike Smith if (isspace(*cp)) { 202015e32d5dSMike Smith cp++; 202115e32d5dSMike Smith continue; 202215e32d5dSMike Smith } 202315e32d5dSMike Smith ep = cp; 202415e32d5dSMike Smith while (*ep && !isspace(*ep)) 202515e32d5dSMike Smith ep++; 20260ae55423SMike Smith if (*cp == '!') { 20270ae55423SMike Smith set = 0; 20280ae55423SMike Smith cp++; 20290ae55423SMike Smith if (cp == ep) 20300ae55423SMike Smith continue; 20310ae55423SMike Smith } else { 20320ae55423SMike Smith set = 1; 20330ae55423SMike Smith } 203415e32d5dSMike Smith l = ep - cp; 203515e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 203615e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 20370ae55423SMike Smith if (set) { 203815e32d5dSMike Smith *flag |= tag[i].value; 20390ae55423SMike Smith } else { 20400ae55423SMike Smith *flag &= ~tag[i].value; 20410ae55423SMike Smith } 204215e32d5dSMike Smith printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 204315e32d5dSMike Smith } 204415e32d5dSMike Smith } 204515e32d5dSMike Smith cp = ep; 204615e32d5dSMike Smith } 204715e32d5dSMike Smith } 204815e32d5dSMike Smith 204915e32d5dSMike Smith static void 20502a4ac806SMike Smith acpi_set_debugging(void *junk) 205115e32d5dSMike Smith { 205294aae282SMike Barcroft char *cp; 205315e32d5dSMike Smith 205487b45ed5SMitsuru IWASAKI if (!cold) 205587b45ed5SMitsuru IWASAKI return; 205687b45ed5SMitsuru IWASAKI 20570ae55423SMike Smith AcpiDbgLayer = 0; 20580ae55423SMike Smith AcpiDbgLevel = 0; 205994aae282SMike Barcroft if ((cp = getenv("debug.acpi.layer")) != NULL) { 206015e32d5dSMike Smith acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 206194aae282SMike Barcroft freeenv(cp); 206294aae282SMike Barcroft } 206394aae282SMike Barcroft if ((cp = getenv("debug.acpi.level")) != NULL) { 206415e32d5dSMike Smith acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 206594aae282SMike Barcroft freeenv(cp); 206694aae282SMike Barcroft } 20670ae55423SMike Smith 20680ae55423SMike Smith printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 206915e32d5dSMike Smith } 20702a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 207115e32d5dSMike Smith #endif 2072f9390180SMitsuru IWASAKI 2073f9390180SMitsuru IWASAKI static int 2074f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2075f9390180SMitsuru IWASAKI { 2076f9390180SMitsuru IWASAKI int state, acpi_state; 2077f9390180SMitsuru IWASAKI int error; 2078f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2079f9390180SMitsuru IWASAKI va_list ap; 2080f9390180SMitsuru IWASAKI 2081f9390180SMitsuru IWASAKI error = 0; 2082f9390180SMitsuru IWASAKI switch (cmd) { 2083f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2084f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2085f9390180SMitsuru IWASAKI if (sc == NULL) { 2086f9390180SMitsuru IWASAKI error = EINVAL; 2087f9390180SMitsuru IWASAKI goto out; 2088f9390180SMitsuru IWASAKI } 2089f9390180SMitsuru IWASAKI 2090f9390180SMitsuru IWASAKI va_start(ap, arg); 2091f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2092f9390180SMitsuru IWASAKI va_end(ap); 2093f9390180SMitsuru IWASAKI 2094f9390180SMitsuru IWASAKI switch (state) { 2095f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2096f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2097f9390180SMitsuru IWASAKI break; 2098f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2099f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2100f9390180SMitsuru IWASAKI break; 2101f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2102f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2103f9390180SMitsuru IWASAKI break; 2104f9390180SMitsuru IWASAKI default: 2105f9390180SMitsuru IWASAKI error = EINVAL; 2106f9390180SMitsuru IWASAKI goto out; 2107f9390180SMitsuru IWASAKI } 2108f9390180SMitsuru IWASAKI 2109f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2110f9390180SMitsuru IWASAKI break; 2111f9390180SMitsuru IWASAKI 2112f9390180SMitsuru IWASAKI default: 2113f9390180SMitsuru IWASAKI error = EINVAL; 2114f9390180SMitsuru IWASAKI goto out; 2115f9390180SMitsuru IWASAKI } 2116f9390180SMitsuru IWASAKI 2117f9390180SMitsuru IWASAKI out: 2118f9390180SMitsuru IWASAKI return (error); 2119f9390180SMitsuru IWASAKI } 2120f9390180SMitsuru IWASAKI 2121f9390180SMitsuru IWASAKI static void 2122f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2123f9390180SMitsuru IWASAKI { 21246c407052SMitsuru IWASAKI int error; 21256c407052SMitsuru IWASAKI 212687b45ed5SMitsuru IWASAKI if (!cold) 212787b45ed5SMitsuru IWASAKI return; 212887b45ed5SMitsuru IWASAKI 21296c407052SMitsuru IWASAKI if (!resource_int_value("acpi", 0, "disabled", &error) && 21306c407052SMitsuru IWASAKI (error != 0)) 21316c407052SMitsuru IWASAKI return; 2132f9390180SMitsuru IWASAKI 2133f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2134f9390180SMitsuru IWASAKI } 2135f9390180SMitsuru IWASAKI 2136f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2137f9390180SMitsuru IWASAKI 2138