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> 36f34fa851SJohn Baldwin #include <sys/lock.h> 3715e32d5dSMike Smith #include <sys/malloc.h> 38b2c9c0daSMitsuru IWASAKI #include <sys/mutex.h> 3915e32d5dSMike Smith #include <sys/bus.h> 4015e32d5dSMike Smith #include <sys/conf.h> 4115e32d5dSMike Smith #include <sys/ioccom.h> 4215e32d5dSMike Smith #include <sys/reboot.h> 4315e32d5dSMike Smith #include <sys/sysctl.h> 4415e32d5dSMike Smith #include <sys/ctype.h> 451611ea87SMitsuru IWASAKI #include <sys/linker.h> 46f9390180SMitsuru IWASAKI #include <sys/power.h> 4715e32d5dSMike Smith 4815e32d5dSMike Smith #include <machine/clock.h> 4915e32d5dSMike Smith #include <machine/resource.h> 5015e32d5dSMike Smith 51bc0f2195SMike Smith #include <isa/isavar.h> 52bc0f2195SMike Smith 5315e32d5dSMike Smith #include "acpi.h" 5415e32d5dSMike Smith 551611ea87SMitsuru IWASAKI #include <dev/acpica/acpica_support.h> 561611ea87SMitsuru IWASAKI 5715e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5815e32d5dSMike Smith #include <dev/acpica/acpiio.h> 5915e32d5dSMike Smith 6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 6115e32d5dSMike Smith 6215e32d5dSMike Smith /* 630ae55423SMike Smith * Hooks for the ACPI CA debugging infrastructure 640ae55423SMike Smith */ 65cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 66b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 670ae55423SMike Smith 680ae55423SMike Smith /* 6915e32d5dSMike Smith * Character device 7015e32d5dSMike Smith */ 7115e32d5dSMike Smith 7215e32d5dSMike Smith static d_open_t acpiopen; 7315e32d5dSMike Smith static d_close_t acpiclose; 7415e32d5dSMike Smith static d_ioctl_t acpiioctl; 7515e32d5dSMike Smith 7615e32d5dSMike Smith #define CDEV_MAJOR 152 7715e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 7815e32d5dSMike Smith acpiopen, 7915e32d5dSMike Smith acpiclose, 8015e32d5dSMike Smith noread, 8115e32d5dSMike Smith nowrite, 8215e32d5dSMike Smith acpiioctl, 8315e32d5dSMike Smith nopoll, 8415e32d5dSMike Smith nommap, 8515e32d5dSMike Smith nostrategy, 8615e32d5dSMike Smith "acpi", 8715e32d5dSMike Smith CDEV_MAJOR, 8815e32d5dSMike Smith nodump, 8915e32d5dSMike Smith nopsize, 90606e1d68SJohn Baldwin 0 9115e32d5dSMike Smith }; 9215e32d5dSMike Smith 931d073b1dSJohn Baldwin static const char* sleep_state_names[] = { 94b8670bedSMitsuru IWASAKI "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 951d073b1dSJohn Baldwin 962a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */ 972a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5; 982a4ac806SMike Smith 99cb9b0d80SMike Smith struct mtx acpi_mutex; 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: 1886d63101aSMike Smith if (!cold) 1896d63101aSMike Smith return(EPERM); 1906d63101aSMike Smith break; 1916d63101aSMike Smith case MOD_UNLOAD: 192f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 1936d63101aSMike Smith return(EBUSY); 194f9390180SMitsuru IWASAKI break; 1956d63101aSMike Smith default: 1966d63101aSMike Smith break; 1976d63101aSMike Smith } 1986d63101aSMike Smith return(0); 1996d63101aSMike Smith } 2006d63101aSMike Smith 2016d63101aSMike Smith /* 20215e32d5dSMike Smith * Detect ACPI, perform early initialisation 20315e32d5dSMike Smith */ 20415e32d5dSMike Smith static void 20515e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent) 20615e32d5dSMike Smith { 20715e32d5dSMike Smith device_t child; 20815e32d5dSMike Smith int error; 209d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 210d786139cSMaxime Henrion char *debugpoint; 21115e32d5dSMike Smith #endif 21215e32d5dSMike Smith 213b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2140ae55423SMike Smith 215840f9b53STakanori Watanabe if(!cold){ 216840f9b53STakanori Watanabe printf("Don't load this driver from userland!!\n"); 217840f9b53STakanori Watanabe return ; 218840f9b53STakanori Watanabe } 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 233cb9b0d80SMike Smith /* initialise the ACPI mutex */ 2346008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 235cb9b0d80SMike Smith 23615e32d5dSMike Smith /* 2372a4ac806SMike Smith * Start up the ACPI CA subsystem. 23815e32d5dSMike Smith */ 239d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 240d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 241d786139cSMaxime Henrion if (debugpoint) { 242d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 24315e32d5dSMike Smith acpi_EnterDebugger(); 244d786139cSMaxime Henrion freeenv(debugpoint); 245d786139cSMaxime Henrion } 24615e32d5dSMike Smith #endif 247b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 248bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 2490ae55423SMike Smith return_VOID; 25015e32d5dSMike Smith } 251d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 252d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 253d786139cSMaxime Henrion if (debugpoint) { 254d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 25515e32d5dSMike Smith acpi_EnterDebugger(); 256d786139cSMaxime Henrion freeenv(debugpoint); 257d786139cSMaxime Henrion } 25815e32d5dSMike Smith #endif 2591611ea87SMitsuru IWASAKI 260b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 261bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 2620ae55423SMike Smith return_VOID; 26315e32d5dSMike Smith } 26415e32d5dSMike Smith 26515e32d5dSMike Smith /* 26615e32d5dSMike Smith * Attach the actual ACPI device. 26715e32d5dSMike Smith */ 26815e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 26915e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 2700ae55423SMike Smith return_VOID; 27115e32d5dSMike Smith } 27215e32d5dSMike Smith } 27315e32d5dSMike Smith 27415e32d5dSMike Smith /* 27515e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 27615e32d5dSMike Smith */ 27715e32d5dSMike Smith static int 27815e32d5dSMike Smith acpi_probe(device_t dev) 27915e32d5dSMike Smith { 28015e32d5dSMike Smith ACPI_TABLE_HEADER th; 28115e32d5dSMike Smith char buf[20]; 282cb9b0d80SMike Smith ACPI_STATUS status; 28315e32d5dSMike Smith int error; 28415e32d5dSMike Smith 285b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 286f9390180SMitsuru IWASAKI 287f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 288f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 289f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 290f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 291f9390180SMitsuru IWASAKI } 292f9390180SMitsuru IWASAKI 293cb9b0d80SMike Smith ACPI_LOCK; 2940ae55423SMike Smith 295b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 296bfae45aaSMike Smith device_printf(dev, "couldn't get XSDT header: %s\n", AcpiFormatException(status)); 297cb9b0d80SMike Smith error = ENXIO; 298cb9b0d80SMike Smith } else { 29915e32d5dSMike Smith sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 30015e32d5dSMike Smith device_set_desc_copy(dev, buf); 301cb9b0d80SMike Smith error = 0; 302cb9b0d80SMike Smith } 303cb9b0d80SMike Smith ACPI_UNLOCK; 304cb9b0d80SMike Smith return_VALUE(error); 30515e32d5dSMike Smith } 30615e32d5dSMike Smith 30715e32d5dSMike Smith static int 30815e32d5dSMike Smith acpi_attach(device_t dev) 30915e32d5dSMike Smith { 31015e32d5dSMike Smith struct acpi_softc *sc; 311cb9b0d80SMike Smith ACPI_STATUS status; 31215e32d5dSMike Smith int error; 31332d18aa5SMike Smith UINT32 flags; 31432d18aa5SMike Smith 315d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 316d786139cSMaxime Henrion char *debugpoint; 31715e32d5dSMike Smith #endif 31815e32d5dSMike Smith 319b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 320cb9b0d80SMike Smith ACPI_LOCK; 32115e32d5dSMike Smith sc = device_get_softc(dev); 32215e32d5dSMike Smith bzero(sc, sizeof(*sc)); 32315e32d5dSMike Smith sc->acpi_dev = dev; 32415e32d5dSMike Smith 325d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 326d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 327d786139cSMaxime Henrion if (debugpoint) { 328d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 32915e32d5dSMike Smith acpi_EnterDebugger(); 330d786139cSMaxime Henrion freeenv(debugpoint); 331d786139cSMaxime Henrion } 33215e32d5dSMike Smith #endif 33315e32d5dSMike Smith 33415e32d5dSMike Smith /* 33515e32d5dSMike Smith * Install the default address space handlers. 33615e32d5dSMike Smith */ 337cb9b0d80SMike Smith error = ENXIO; 338b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3392a4ac806SMike Smith ACPI_ADR_SPACE_SYSTEM_MEMORY, 34015e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 341b53f2771SMike Smith NULL, NULL))) { 342bfae45aaSMike Smith device_printf(dev, "could not initialise SystemMemory handler: %s\n", AcpiFormatException(status)); 343cb9b0d80SMike Smith goto out; 34415e32d5dSMike Smith } 345b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3462a4ac806SMike Smith ACPI_ADR_SPACE_SYSTEM_IO, 34715e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 348b53f2771SMike Smith NULL, NULL))) { 349bfae45aaSMike Smith device_printf(dev, "could not initialise SystemIO handler: %s\n", AcpiFormatException(status)); 350cb9b0d80SMike Smith goto out; 35115e32d5dSMike Smith } 352b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 3532a4ac806SMike Smith ACPI_ADR_SPACE_PCI_CONFIG, 35415e32d5dSMike Smith ACPI_DEFAULT_HANDLER, 355b53f2771SMike Smith NULL, NULL))) { 356bfae45aaSMike Smith device_printf(dev, "could not initialise PciConfig handler: %s\n", AcpiFormatException(status)); 357cb9b0d80SMike Smith goto out; 35815e32d5dSMike Smith } 35915e32d5dSMike Smith 36015e32d5dSMike Smith /* 36115e32d5dSMike Smith * Bring ACPI fully online. 36215e32d5dSMike Smith * 36332d18aa5SMike Smith * Note that some systems (specifically, those with namespace evaluation issues 36432d18aa5SMike Smith * that require the avoidance of parts of the namespace) must avoid running _INI 36532d18aa5SMike Smith * and _STA on everything, as well as dodging the final object init pass. 36615e32d5dSMike Smith * 36732d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 36832d18aa5SMike Smith * 36932d18aa5SMike Smith * XXX We should arrange for the object init pass after we have attached all our 37032d18aa5SMike Smith * child devices, but on many systems it works here. 37115e32d5dSMike Smith */ 372d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 373d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 374d786139cSMaxime Henrion if (debugpoint) { 375d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 37615e32d5dSMike Smith acpi_EnterDebugger(); 377d786139cSMaxime Henrion freeenv(debugpoint); 378d786139cSMaxime Henrion } 37915e32d5dSMike Smith #endif 38032d18aa5SMike Smith flags = 0; 381d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 38232d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 383b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 384bfae45aaSMike Smith device_printf(dev, "could not enable ACPI: %s\n", AcpiFormatException(status)); 385cb9b0d80SMike Smith goto out; 38615e32d5dSMike Smith } 38715e32d5dSMike Smith 388b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 389b69ed3f4SMitsuru IWASAKI device_printf(dev, "could not initialize ACPI objects: %s\n", AcpiFormatException(status)); 390b69ed3f4SMitsuru IWASAKI goto out; 391b69ed3f4SMitsuru IWASAKI } 392b69ed3f4SMitsuru IWASAKI 39315e32d5dSMike Smith /* 3941d073b1dSJohn Baldwin * Setup our sysctl tree. 3951d073b1dSJohn Baldwin * 3961d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 3971d073b1dSJohn Baldwin */ 3981d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 3991d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4001d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4011d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 4021d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4031d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4041d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4051d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4061d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4071d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4081d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4091d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4101d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 411f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 412f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 413f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 414f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 415f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 416f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4172d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 418ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 419ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 420ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4211611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 4221611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 4231611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4242d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 4252d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 426f3a6cbb6SMitsuru IWASAKI sc->acpi_sleep_delay = 0; 427931a10c9SMitsuru IWASAKI sc->acpi_s4bios = 1; 4282d644607SMitsuru IWASAKI if (bootverbose) 4292d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 4301d073b1dSJohn Baldwin 4311d073b1dSJohn Baldwin /* 43215e32d5dSMike Smith * Dispatch the default sleep state to devices. 43315e32d5dSMike Smith * TBD: should be configured from userland policy manager. 43415e32d5dSMike Smith */ 43515e32d5dSMike Smith sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 43615e32d5dSMike Smith sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 43715e32d5dSMike Smith sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 438f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 439f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 44015e32d5dSMike Smith 44113d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 44215e32d5dSMike Smith 44315e32d5dSMike Smith /* 44415e32d5dSMike Smith * Scan the namespace and attach/initialise children. 44515e32d5dSMike Smith */ 446d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 447d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 448d786139cSMaxime Henrion if (debugpoint) { 449d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 45015e32d5dSMike Smith acpi_EnterDebugger(); 451d786139cSMaxime Henrion freeenv(debugpoint); 452d786139cSMaxime Henrion } 45315e32d5dSMike Smith #endif 45415e32d5dSMike Smith 45515e32d5dSMike Smith /* 45615e32d5dSMike Smith * Register our shutdown handlers 45715e32d5dSMike Smith */ 45815e32d5dSMike Smith EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, SHUTDOWN_PRI_LAST); 45915e32d5dSMike Smith EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, SHUTDOWN_PRI_LAST); 46015e32d5dSMike Smith 46115e32d5dSMike Smith /* 46215e32d5dSMike Smith * Register our acpi event handlers. 46315e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 46415e32d5dSMike Smith */ 46515e32d5dSMike Smith EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, sc, ACPI_EVENT_PRI_LAST); 46615e32d5dSMike Smith EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, sc, ACPI_EVENT_PRI_LAST); 46715e32d5dSMike Smith 46815e32d5dSMike Smith /* 46915e32d5dSMike Smith * Flag our initial states. 47015e32d5dSMike Smith */ 47115e32d5dSMike Smith sc->acpi_enabled = 1; 47215e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 473ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 47415e32d5dSMike Smith 47515e32d5dSMike Smith /* 47615e32d5dSMike Smith * Create the control device 47715e32d5dSMike Smith */ 47815e32d5dSMike Smith sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, 0, 5, 0660, "acpi"); 47915e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 48015e32d5dSMike Smith 481d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 482d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 483d786139cSMaxime Henrion if (debugpoint) { 484d786139cSMaxime Henrion if (!strcmp(debugpoint, "running")) 48515e32d5dSMike Smith acpi_EnterDebugger(); 486d786139cSMaxime Henrion freeenv(debugpoint); 487d786139cSMaxime Henrion } 48815e32d5dSMike Smith #endif 489f86214b6SMitsuru IWASAKI 490c573e654SMitsuru IWASAKI #if defined(ACPI_MAX_THREADS) && ACPI_MAX_THREADS > 0 491c573e654SMitsuru IWASAKI if ((error = acpi_task_thread_init())) { 492c573e654SMitsuru IWASAKI goto out; 493c573e654SMitsuru IWASAKI } 494c573e654SMitsuru IWASAKI #endif 495c573e654SMitsuru IWASAKI 496f86214b6SMitsuru IWASAKI if ((error = acpi_machdep_init(dev))) { 497f86214b6SMitsuru IWASAKI goto out; 498f86214b6SMitsuru IWASAKI } 499f86214b6SMitsuru IWASAKI 500f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 501f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 502f9390180SMitsuru IWASAKI 503eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 504eeb6dba2SJohn Baldwin acpi_probe_children(dev); 505eeb6dba2SJohn Baldwin 506cb9b0d80SMike Smith error = 0; 507cb9b0d80SMike Smith 508cb9b0d80SMike Smith out: 509cb9b0d80SMike Smith ACPI_UNLOCK; 510cb9b0d80SMike Smith return_VALUE(error); 51115e32d5dSMike Smith } 51215e32d5dSMike Smith 51315e32d5dSMike Smith /* 51415e32d5dSMike Smith * Handle a new device being added 51515e32d5dSMike Smith */ 51615e32d5dSMike Smith static device_t 51715e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 51815e32d5dSMike Smith { 51915e32d5dSMike Smith struct acpi_device *ad; 52015e32d5dSMike Smith device_t child; 52115e32d5dSMike Smith 52215e32d5dSMike Smith if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT)) == NULL) 52315e32d5dSMike Smith return(NULL); 52415e32d5dSMike Smith bzero(ad, sizeof(*ad)); 52515e32d5dSMike Smith 52615e32d5dSMike Smith resource_list_init(&ad->ad_rl); 52715e32d5dSMike Smith 52815e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 52915e32d5dSMike Smith if (child != NULL) 53015e32d5dSMike Smith device_set_ivars(child, ad); 53115e32d5dSMike Smith return(child); 53215e32d5dSMike Smith } 53315e32d5dSMike Smith 53415e32d5dSMike Smith static int 53515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 53615e32d5dSMike Smith { 53715e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 53815e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 53915e32d5dSMike Smith int retval = 0; 54015e32d5dSMike Smith 54115e32d5dSMike Smith retval += bus_print_child_header(bus, child); 5429ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 5439ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 5449ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 5459ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 54615e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 54715e32d5dSMike Smith 54815e32d5dSMike Smith return(retval); 54915e32d5dSMike Smith } 55015e32d5dSMike Smith 55115e32d5dSMike Smith 55215e32d5dSMike Smith /* 55315e32d5dSMike Smith * Handle per-device ivars 55415e32d5dSMike Smith */ 55515e32d5dSMike Smith static int 55615e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 55715e32d5dSMike Smith { 55815e32d5dSMike Smith struct acpi_device *ad; 55915e32d5dSMike Smith 56015e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 56115e32d5dSMike Smith printf("device has no ivars\n"); 56215e32d5dSMike Smith return(ENOENT); 56315e32d5dSMike Smith } 56415e32d5dSMike Smith 56515e32d5dSMike Smith switch(index) { 56615e32d5dSMike Smith /* ACPI ivars */ 56715e32d5dSMike Smith case ACPI_IVAR_HANDLE: 56815e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 56915e32d5dSMike Smith break; 57015e32d5dSMike Smith case ACPI_IVAR_MAGIC: 57115e32d5dSMike Smith *(int *)result = ad->ad_magic; 57215e32d5dSMike Smith break; 57315e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 57415e32d5dSMike Smith *(void **)result = ad->ad_private; 57515e32d5dSMike Smith break; 57615e32d5dSMike Smith 57732d18aa5SMike Smith /* ISA compatibility */ 57832d18aa5SMike Smith case ISA_IVAR_VENDORID: 57932d18aa5SMike Smith case ISA_IVAR_SERIAL: 58032d18aa5SMike Smith case ISA_IVAR_COMPATID: 58132d18aa5SMike Smith *(int *)result = -1; 58232d18aa5SMike Smith break; 58332d18aa5SMike Smith 58432d18aa5SMike Smith case ISA_IVAR_LOGICALID: 58532d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 58632d18aa5SMike Smith break; 58732d18aa5SMike Smith 58815e32d5dSMike Smith default: 5892ab060eeSWarner Losh panic("bad ivar read request (%d)", index); 59015e32d5dSMike Smith return(ENOENT); 59115e32d5dSMike Smith } 59215e32d5dSMike Smith return(0); 59315e32d5dSMike Smith } 59415e32d5dSMike Smith 59515e32d5dSMike Smith static int 59615e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 59715e32d5dSMike Smith { 59815e32d5dSMike Smith struct acpi_device *ad; 59915e32d5dSMike Smith 60015e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 60115e32d5dSMike Smith printf("device has no ivars\n"); 60215e32d5dSMike Smith return(ENOENT); 60315e32d5dSMike Smith } 60415e32d5dSMike Smith 60515e32d5dSMike Smith switch(index) { 60615e32d5dSMike Smith /* ACPI ivars */ 60715e32d5dSMike Smith case ACPI_IVAR_HANDLE: 60815e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 60915e32d5dSMike Smith break; 61015e32d5dSMike Smith case ACPI_IVAR_MAGIC: 61115e32d5dSMike Smith ad->ad_magic = (int )value; 61215e32d5dSMike Smith break; 61315e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 61415e32d5dSMike Smith ad->ad_private = (void *)value; 61515e32d5dSMike Smith break; 61615e32d5dSMike Smith 61715e32d5dSMike Smith default: 6182ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 61915e32d5dSMike Smith return(ENOENT); 62015e32d5dSMike Smith } 62115e32d5dSMike Smith return(0); 62215e32d5dSMike Smith } 62315e32d5dSMike Smith 62415e32d5dSMike Smith /* 62515e32d5dSMike Smith * Handle child resource allocation/removal 62615e32d5dSMike Smith */ 62715e32d5dSMike Smith static int 62815e32d5dSMike Smith acpi_set_resource(device_t dev, device_t child, int type, int rid, u_long start, u_long count) 62915e32d5dSMike Smith { 63015e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 63115e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 63215e32d5dSMike Smith 63315e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 63415e32d5dSMike Smith 63515e32d5dSMike Smith return(0); 63615e32d5dSMike Smith } 63715e32d5dSMike Smith 63815e32d5dSMike Smith static int 63915e32d5dSMike Smith acpi_get_resource(device_t dev, device_t child, int type, int rid, u_long *startp, u_long *countp) 64015e32d5dSMike Smith { 64115e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 64215e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 64315e32d5dSMike Smith struct resource_list_entry *rle; 64415e32d5dSMike Smith 64515e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 64615e32d5dSMike Smith if (!rle) 64715e32d5dSMike Smith return(ENOENT); 64815e32d5dSMike Smith 64915e32d5dSMike Smith if (startp) 65015e32d5dSMike Smith *startp = rle->start; 65115e32d5dSMike Smith if (countp) 65215e32d5dSMike Smith *countp = rle->count; 65315e32d5dSMike Smith 65415e32d5dSMike Smith return(0); 65515e32d5dSMike Smith } 65615e32d5dSMike Smith 65715e32d5dSMike Smith static struct resource * 65815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 65915e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 66015e32d5dSMike Smith { 66115e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 66215e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 66315e32d5dSMike Smith 66415e32d5dSMike Smith return(resource_list_alloc(rl, bus, child, type, rid, start, end, count, flags)); 66515e32d5dSMike Smith } 66615e32d5dSMike Smith 66715e32d5dSMike Smith static int 66815e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 66915e32d5dSMike Smith { 67015e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 67115e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 67215e32d5dSMike Smith 67315e32d5dSMike Smith return(resource_list_release(rl, bus, child, type, rid, r)); 67415e32d5dSMike Smith } 67515e32d5dSMike Smith 67615e32d5dSMike Smith /* 677bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 678bc0f2195SMike Smith */ 679bc0f2195SMike Smith #define PNP_EISAID(s) \ 680bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 681bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 682bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 683bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 684bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 685bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 686bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 687bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 688bc0f2195SMike Smith 68932d18aa5SMike Smith static u_int32_t 69032d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 691bc0f2195SMike Smith { 692bc0f2195SMike Smith ACPI_HANDLE h; 693bc0f2195SMike Smith ACPI_DEVICE_INFO devinfo; 694bc0f2195SMike Smith ACPI_STATUS error; 695bc0f2195SMike Smith u_int32_t pnpid; 69632d18aa5SMike Smith 697b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 69832d18aa5SMike Smith 69932d18aa5SMike Smith pnpid = 0; 70032d18aa5SMike Smith ACPI_LOCK; 70132d18aa5SMike Smith 70232d18aa5SMike Smith /* fetch and validate the HID */ 70332d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 70432d18aa5SMike Smith goto out; 705b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 70632d18aa5SMike Smith goto out; 70732d18aa5SMike Smith if (!(devinfo.Valid & ACPI_VALID_HID)) 70832d18aa5SMike Smith goto out; 70932d18aa5SMike Smith 71032d18aa5SMike Smith pnpid = PNP_EISAID(devinfo.HardwareId); 71132d18aa5SMike Smith out: 71232d18aa5SMike Smith ACPI_UNLOCK; 71332d18aa5SMike Smith return_VALUE(pnpid); 71432d18aa5SMike Smith } 71532d18aa5SMike Smith 71632d18aa5SMike Smith static int 71732d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 71832d18aa5SMike Smith { 719bc0f2195SMike Smith int result; 72032d18aa5SMike Smith u_int32_t pnpid; 721bc0f2195SMike Smith 722b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 723bc0f2195SMike Smith 724bc0f2195SMike Smith /* 725bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 726bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 727bc0f2195SMike Smith * that to happen, so don't ever return it. 728bc0f2195SMike Smith */ 729bc0f2195SMike Smith result = ENXIO; 730bc0f2195SMike Smith 731bc0f2195SMike Smith /* scan the supplied IDs for a match */ 73232d18aa5SMike Smith pnpid = acpi_isa_get_logicalid(child); 733bc0f2195SMike Smith while (ids && ids->ip_id) { 734bc0f2195SMike Smith if (pnpid == ids->ip_id) { 735bc0f2195SMike Smith result = 0; 736bc0f2195SMike Smith goto out; 737bc0f2195SMike Smith } 738bc0f2195SMike Smith ids++; 739bc0f2195SMike Smith } 740bc0f2195SMike Smith out: 741bc0f2195SMike Smith return_VALUE(result); 742bc0f2195SMike Smith } 743bc0f2195SMike Smith 744bc0f2195SMike Smith /* 74515e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 74615e32d5dSMike Smith * 7477d3bcec9SMike Smith * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and \_SB_ scopes, 7487d3bcec9SMike Smith * and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 74915e32d5dSMike Smith */ 75015e32d5dSMike Smith static void 75115e32d5dSMike Smith acpi_probe_children(device_t bus) 75215e32d5dSMike Smith { 75315e32d5dSMike Smith ACPI_HANDLE parent; 7547d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 75515e32d5dSMike Smith int i; 75615e32d5dSMike Smith 757b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 758cb9b0d80SMike Smith ACPI_ASSERTLOCK; 7590ae55423SMike Smith 76015e32d5dSMike Smith /* 76115e32d5dSMike Smith * Create any static children by calling device identify methods. 76215e32d5dSMike Smith */ 7634c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 76415e32d5dSMike Smith bus_generic_probe(bus); 76515e32d5dSMike Smith 76615e32d5dSMike Smith /* 76715e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 76815e32d5dSMike Smith * we find. 76915e32d5dSMike Smith * 77015e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 77115e32d5dSMike Smith * we want to create nodes for all devices, not just those that are currently 77215e32d5dSMike Smith * present. (This assumes that we don't want to create/remove devices as they 77315e32d5dSMike Smith * appear, which might be smarter.) 77415e32d5dSMike Smith */ 7754c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 77615e32d5dSMike Smith for (i = 0; scopes[i] != NULL; i++) 777b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent))) 77815e32d5dSMike Smith AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, bus, NULL); 77915e32d5dSMike Smith 78015e32d5dSMike Smith /* 78115e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 78215e32d5dSMike Smith */ 7834c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 78415e32d5dSMike Smith bus_generic_attach(bus); 78515e32d5dSMike Smith 78615e32d5dSMike Smith /* 78715e32d5dSMike Smith * Some of these children may have attached others as part of their attach 78815e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 78915e32d5dSMike Smith */ 7904c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 79115e32d5dSMike Smith bus_generic_attach(bus); 7920ae55423SMike Smith 793bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 7940ae55423SMike Smith return_VOID; 79515e32d5dSMike Smith } 79615e32d5dSMike Smith 79715e32d5dSMike Smith /* 79815e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 79915e32d5dSMike Smith * it. 80015e32d5dSMike Smith */ 80115e32d5dSMike Smith static ACPI_STATUS 80215e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 80315e32d5dSMike Smith { 80415e32d5dSMike Smith ACPI_OBJECT_TYPE type; 80515e32d5dSMike Smith device_t child, bus = (device_t)context; 80615e32d5dSMike Smith 807b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 8080ae55423SMike Smith 8090ae55423SMike Smith /* 8100ae55423SMike Smith * Skip this device if we think we'll have trouble with it. 8110ae55423SMike Smith */ 8120ae55423SMike Smith if (acpi_avoid(handle)) 8130ae55423SMike Smith return_ACPI_STATUS(AE_OK); 8140ae55423SMike Smith 815b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 81615e32d5dSMike Smith switch(type) { 81715e32d5dSMike Smith case ACPI_TYPE_DEVICE: 81815e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 81915e32d5dSMike Smith case ACPI_TYPE_THERMAL: 82015e32d5dSMike Smith case ACPI_TYPE_POWER: 8210ae55423SMike Smith if (acpi_disabled("children")) 8220ae55423SMike Smith break; 82315e32d5dSMike Smith /* 82415e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 82515e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 82615e32d5dSMike Smith */ 8274c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", acpi_name(handle))); 82815e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 829bc0f2195SMike Smith if (child == NULL) 830bc0f2195SMike Smith break; 83115e32d5dSMike Smith acpi_set_handle(child, handle); 832bc0f2195SMike Smith 833bc0f2195SMike Smith /* 834b519f9d6SMike Smith * Check that the device is present. If it's not present, 835b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 836b519f9d6SMike Smith * the handle, but we don't probe it). 837b519f9d6SMike Smith */ 83823b4e251SMitsuru IWASAKI if ((type == ACPI_TYPE_DEVICE) && (!acpi_DeviceIsPresent(child))) { 839b519f9d6SMike Smith device_disable(child); 840b519f9d6SMike Smith break; 841b519f9d6SMike Smith } 842b519f9d6SMike Smith 843b519f9d6SMike Smith /* 844bc0f2195SMike Smith * Get the device's resource settings and attach them. 845bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 846bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 847bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 848bc0f2195SMike Smith * device not to have any resources. 849bc0f2195SMike Smith */ 850bc0f2195SMike Smith acpi_parse_resources(child, handle, &acpi_res_parse_set); 851bc0f2195SMike Smith 852bc0f2195SMike Smith /* if we're debugging, probe/attach now rather than later */ 853b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 8540ae55423SMike Smith break; 85515e32d5dSMike Smith } 85615e32d5dSMike Smith } 8570ae55423SMike Smith return_ACPI_STATUS(AE_OK); 85815e32d5dSMike Smith } 85915e32d5dSMike Smith 86015e32d5dSMike Smith static void 86115e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 86215e32d5dSMike Smith { 863cb9b0d80SMike Smith 864cb9b0d80SMike Smith ACPI_ASSERTLOCK; 865cb9b0d80SMike Smith 86615e32d5dSMike Smith /* 8670ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 86815e32d5dSMike Smith * will be turned on again on some laptops. 86915e32d5dSMike Smith * 87015e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 87115e32d5dSMike Smith * before powering down, since we may still need ACPI during the 87215e32d5dSMike Smith * shutdown process. 87315e32d5dSMike Smith */ 87415e32d5dSMike Smith acpi_Disable((struct acpi_softc *)arg); 87515e32d5dSMike Smith } 87615e32d5dSMike Smith 87715e32d5dSMike Smith static void 87815e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 87915e32d5dSMike Smith { 88015e32d5dSMike Smith ACPI_STATUS status; 88115e32d5dSMike Smith 882cb9b0d80SMike Smith ACPI_ASSERTLOCK; 883cb9b0d80SMike Smith 8845f3e4175SMitsuru IWASAKI if (howto & RB_POWEROFF) { 88515e32d5dSMike Smith printf("Power system off using ACPI...\n"); 886b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(acpi_off_state))) { 887b9c780d6SMitsuru IWASAKI printf("AcpiEnterSleepStatePrep failed - %s\n", 888b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 889b9c780d6SMitsuru IWASAKI return; 890b9c780d6SMitsuru IWASAKI } 891b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState(acpi_off_state))) { 892bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 89315e32d5dSMike Smith } else { 89415e32d5dSMike Smith DELAY(1000000); 89515e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 89615e32d5dSMike Smith } 897494ae560SMitsuru IWASAKI } else { 898494ae560SMitsuru IWASAKI printf("Terminate ACPI\n"); 899494ae560SMitsuru IWASAKI AcpiTerminate(); 90015e32d5dSMike Smith } 90115e32d5dSMike Smith } 90215e32d5dSMike Smith 90313d4f7baSMitsuru IWASAKI static void 90413d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 90513d4f7baSMitsuru IWASAKI { 90613d4f7baSMitsuru IWASAKI static int first_time = 1; 90713d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 90813d4f7baSMitsuru IWASAKI 909cb9b0d80SMike Smith ACPI_ASSERTLOCK; 910cb9b0d80SMike Smith 91113d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 912cb9b0d80SMike Smith if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->PwrButton == 0)) { 91343896e91SMike Smith AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED, 0); 91413d4f7baSMitsuru IWASAKI AcpiClearEvent(ACPI_EVENT_POWER_BUTTON, ACPI_EVENT_FIXED); 91513d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 91613d4f7baSMitsuru IWASAKI acpi_eventhandler_power_button_for_sleep, sc); 91713d4f7baSMitsuru IWASAKI if (first_time) { 91813d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "power"); 91913d4f7baSMitsuru IWASAKI } 92013d4f7baSMitsuru IWASAKI } 921cb9b0d80SMike Smith if ((AcpiGbl_FADT != NULL) && (AcpiGbl_FADT->SleepButton == 0)) { 92243896e91SMike Smith AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED, 0); 92313d4f7baSMitsuru IWASAKI AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON, ACPI_EVENT_FIXED); 92413d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 92513d4f7baSMitsuru IWASAKI acpi_eventhandler_sleep_button_for_sleep, sc); 92613d4f7baSMitsuru IWASAKI if (first_time) { 92713d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 92813d4f7baSMitsuru IWASAKI } 92913d4f7baSMitsuru IWASAKI } 93013d4f7baSMitsuru IWASAKI 93113d4f7baSMitsuru IWASAKI first_time = 0; 93213d4f7baSMitsuru IWASAKI } 93313d4f7baSMitsuru IWASAKI 93415e32d5dSMike Smith /* 935c5ba0be4SMike Smith * Returns true if the device is actually present and should 936c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 937c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 938c5ba0be4SMike Smith */ 939c5ba0be4SMike Smith BOOLEAN 940c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 941c5ba0be4SMike Smith { 942c5ba0be4SMike Smith ACPI_HANDLE h; 943c5ba0be4SMike Smith ACPI_DEVICE_INFO devinfo; 944c5ba0be4SMike Smith ACPI_STATUS error; 945c5ba0be4SMike Smith 946cb9b0d80SMike Smith ACPI_ASSERTLOCK; 947cb9b0d80SMike Smith 948c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 949c5ba0be4SMike Smith return(FALSE); 950b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 951c5ba0be4SMike Smith return(FALSE); 95243896e91SMike Smith /* if no _STA method, must be present */ 95343896e91SMike Smith if (!(devinfo.Valid & ACPI_VALID_STA)) 95443896e91SMike Smith return(TRUE); 95543896e91SMike Smith /* return true for 'present' and 'functioning' */ 95643896e91SMike Smith if ((devinfo.CurrentStatus & 0x9) == 0x9) 957c5ba0be4SMike Smith return(TRUE); 958c5ba0be4SMike Smith return(FALSE); 959c5ba0be4SMike Smith } 960c5ba0be4SMike Smith 961c5ba0be4SMike Smith /* 962b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 963b53f2771SMike Smith */ 964b53f2771SMike Smith BOOLEAN 965b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 966b53f2771SMike Smith { 967b53f2771SMike Smith ACPI_HANDLE h; 968b53f2771SMike Smith ACPI_DEVICE_INFO devinfo; 969b53f2771SMike Smith ACPI_STATUS error; 970b53f2771SMike Smith 971b53f2771SMike Smith ACPI_ASSERTLOCK; 972b53f2771SMike Smith 973b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 974b53f2771SMike Smith return(FALSE); 975b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 976b53f2771SMike Smith return(FALSE); 977b53f2771SMike Smith /* if no _STA method, must be present */ 978b53f2771SMike Smith if (!(devinfo.Valid & ACPI_VALID_STA)) 979b53f2771SMike Smith return(TRUE); 980b53f2771SMike Smith /* return true for 'present' and 'functioning' */ 981b53f2771SMike Smith if ((devinfo.CurrentStatus & 0x19) == 0x19) 982b53f2771SMike Smith return(TRUE); 983b53f2771SMike Smith return(FALSE); 984b53f2771SMike Smith } 985b53f2771SMike Smith 986b53f2771SMike Smith /* 98715e32d5dSMike Smith * Match a HID string against a device 98815e32d5dSMike Smith */ 98915e32d5dSMike Smith BOOLEAN 99015e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 99115e32d5dSMike Smith { 99215e32d5dSMike Smith ACPI_HANDLE h; 99315e32d5dSMike Smith ACPI_DEVICE_INFO devinfo; 99415e32d5dSMike Smith ACPI_STATUS error; 995ed136da6SDoug Rabson int cid; 99615e32d5dSMike Smith 997cb9b0d80SMike Smith ACPI_ASSERTLOCK; 998cb9b0d80SMike Smith 9994d332989SMike Smith if (hid == NULL) 100015e32d5dSMike Smith return(FALSE); 100115e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 100215e32d5dSMike Smith return(FALSE); 1003b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiGetObjectInfo(h, &devinfo))) 100415e32d5dSMike Smith return(FALSE); 10054d332989SMike Smith if ((devinfo.Valid & ACPI_VALID_HID) && !strcmp(hid, devinfo.HardwareId)) 100615e32d5dSMike Smith return(TRUE); 1007b53f2771SMike Smith if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1008ed136da6SDoug Rabson return(FALSE); 1009ed136da6SDoug Rabson if (cid == PNP_EISAID(hid)) 1010ed136da6SDoug Rabson return(TRUE); 101115e32d5dSMike Smith return(FALSE); 101215e32d5dSMike Smith } 101315e32d5dSMike Smith 101415e32d5dSMike Smith /* 1015c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1016c5ba0be4SMike Smith * or one if its parents. 1017c5ba0be4SMike Smith */ 1018c5ba0be4SMike Smith ACPI_STATUS 1019c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1020c5ba0be4SMike Smith { 1021c5ba0be4SMike Smith ACPI_HANDLE r; 1022c5ba0be4SMike Smith ACPI_STATUS status; 1023c5ba0be4SMike Smith 1024cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1025cb9b0d80SMike Smith 1026c5ba0be4SMike Smith /* walk back up the tree to the root */ 1027c5ba0be4SMike Smith for (;;) { 1028b53f2771SMike Smith if (ACPI_SUCCESS(status = AcpiGetHandle(parent, path, &r))) { 1029c5ba0be4SMike Smith *result = r; 1030c5ba0be4SMike Smith return(AE_OK); 1031c5ba0be4SMike Smith } 1032c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1033c5ba0be4SMike Smith return(AE_OK); 1034b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1035c5ba0be4SMike Smith return(AE_NOT_FOUND); 1036c5ba0be4SMike Smith parent = r; 1037c5ba0be4SMike Smith } 1038c5ba0be4SMike Smith } 1039c5ba0be4SMike Smith 1040c5ba0be4SMike Smith /* 1041c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1042c5ba0be4SMike Smith */ 1043c5ba0be4SMike Smith ACPI_BUFFER * 1044c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1045c5ba0be4SMike Smith { 1046c5ba0be4SMike Smith ACPI_BUFFER *buf; 1047c5ba0be4SMike Smith 1048c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1049c5ba0be4SMike Smith return(NULL); 1050c5ba0be4SMike Smith buf->Length = size; 1051c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1052c5ba0be4SMike Smith return(buf); 1053c5ba0be4SMike Smith } 1054c5ba0be4SMike Smith 1055c5ba0be4SMike Smith /* 1056c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1057c5ba0be4SMike Smith */ 1058c5ba0be4SMike Smith ACPI_STATUS 1059c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1060c5ba0be4SMike Smith { 1061c5ba0be4SMike Smith ACPI_STATUS error; 1062c5ba0be4SMike Smith ACPI_BUFFER buf; 1063c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1064c5ba0be4SMike Smith 1065cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1066cb9b0d80SMike Smith 1067c5ba0be4SMike Smith if (handle == NULL) 1068c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 10690c7da7acSJohn Baldwin 10700c7da7acSJohn Baldwin /* 10710c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 10720c7da7acSJohn Baldwin * a method that will return an Integer. 10730c7da7acSJohn Baldwin */ 1074c5ba0be4SMike Smith buf.Pointer = ¶m; 1075c5ba0be4SMike Smith buf.Length = sizeof(param); 1076b53f2771SMike Smith if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1077c5ba0be4SMike Smith if (param.Type == ACPI_TYPE_INTEGER) { 1078c5ba0be4SMike Smith *number = param.Integer.Value; 1079c5ba0be4SMike Smith } else { 1080c5ba0be4SMike Smith error = AE_TYPE; 1081c5ba0be4SMike Smith } 1082c5ba0be4SMike Smith } 10830c7da7acSJohn Baldwin 10840c7da7acSJohn Baldwin /* 10850c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 10860c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 10870c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 10880c7da7acSJohn Baldwin * convert it into an Integer as best we can. 10890c7da7acSJohn Baldwin * 10900c7da7acSJohn Baldwin * This is a hack. 10910c7da7acSJohn Baldwin */ 10920c7da7acSJohn Baldwin if (error == AE_BUFFER_OVERFLOW) { 1093b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 10940c7da7acSJohn Baldwin error = AE_NO_MEMORY; 10950c7da7acSJohn Baldwin } else { 1096b53f2771SMike Smith if (ACPI_SUCCESS(error = AcpiEvaluateObject(handle, path, NULL, &buf))) { 1097c573e654SMitsuru IWASAKI error = acpi_ConvertBufferToInteger(&buf, number); 10980c7da7acSJohn Baldwin } 10990c7da7acSJohn Baldwin } 11000c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 11010c7da7acSJohn Baldwin } 1102c5ba0be4SMike Smith return(error); 1103c5ba0be4SMike Smith } 1104c5ba0be4SMike Smith 1105c573e654SMitsuru IWASAKI ACPI_STATUS 1106c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1107c573e654SMitsuru IWASAKI { 1108c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1109c573e654SMitsuru IWASAKI int i; 1110c573e654SMitsuru IWASAKI 1111c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1112c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1113c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1114c573e654SMitsuru IWASAKI return(AE_OK); 1115c573e654SMitsuru IWASAKI } 1116c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1117c573e654SMitsuru IWASAKI return(AE_TYPE); 1118c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1119c573e654SMitsuru IWASAKI return(AE_BAD_DATA); 1120c573e654SMitsuru IWASAKI *number = 0; 1121c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1122c573e654SMitsuru IWASAKI *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1123c573e654SMitsuru IWASAKI return(AE_OK); 1124c573e654SMitsuru IWASAKI } 1125c573e654SMitsuru IWASAKI 1126c5ba0be4SMike Smith /* 1127c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1128c5ba0be4SMike Smith * function for each element. 1129c5ba0be4SMike Smith * 1130c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1131c5ba0be4SMike Smith */ 1132c5ba0be4SMike Smith ACPI_STATUS 1133c5ba0be4SMike Smith acpi_ForeachPackageObject(ACPI_OBJECT *pkg, void (* func)(ACPI_OBJECT *comp, void *arg), void *arg) 1134c5ba0be4SMike Smith { 1135c5ba0be4SMike Smith ACPI_OBJECT *comp; 1136c5ba0be4SMike Smith int i; 1137c5ba0be4SMike Smith 1138c5ba0be4SMike Smith if ((pkg == NULL) || (pkg->Type != ACPI_TYPE_PACKAGE)) 1139c5ba0be4SMike Smith return(AE_BAD_PARAMETER); 1140c5ba0be4SMike Smith 1141c5ba0be4SMike Smith /* iterate over components */ 1142c5ba0be4SMike Smith for (i = 0, comp = pkg->Package.Elements; i < pkg->Package.Count; i++, comp++) 1143c5ba0be4SMike Smith func(comp, arg); 1144c5ba0be4SMike Smith 1145c5ba0be4SMike Smith return(AE_OK); 1146c5ba0be4SMike Smith } 1147c5ba0be4SMike Smith 11486f69255bSMike Smith /* 11496f69255bSMike Smith * Find the (index)th resource object in a set. 11506f69255bSMike Smith */ 11516f69255bSMike Smith ACPI_STATUS 11526d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 11536f69255bSMike Smith { 11546d63101aSMike Smith ACPI_RESOURCE *rp; 11556f69255bSMike Smith int i; 11566f69255bSMike Smith 11576d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 11586f69255bSMike Smith i = index; 11596d63101aSMike Smith while (i-- > 0) { 11606f69255bSMike Smith /* range check */ 11616d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 11626f69255bSMike Smith return(AE_BAD_PARAMETER); 11636d63101aSMike Smith /* check for terminator */ 11646d63101aSMike Smith if ((rp->Id == ACPI_RSTYPE_END_TAG) || 11656d63101aSMike Smith (rp->Length == 0)) 11666d63101aSMike Smith return(AE_NOT_FOUND); 11676d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 11686f69255bSMike Smith } 11696f69255bSMike Smith if (resp != NULL) 11706d63101aSMike Smith *resp = rp; 11716d63101aSMike Smith return(AE_OK); 11726d63101aSMike Smith } 11736d63101aSMike Smith 11746d63101aSMike Smith /* 11756d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 11766d63101aSMike Smith * 11776d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 11786d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 11796d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 11806d63101aSMike Smith * resources. 11816d63101aSMike Smith */ 11826d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 11836d63101aSMike Smith 11846d63101aSMike Smith ACPI_STATUS 11856d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 11866d63101aSMike Smith { 11876d63101aSMike Smith ACPI_RESOURCE *rp; 11886d63101aSMike Smith void *newp; 11896d63101aSMike Smith 11906d63101aSMike Smith /* 11916d63101aSMike Smith * Initialise the buffer if necessary. 11926d63101aSMike Smith */ 11936d63101aSMike Smith if (buf->Pointer == NULL) { 11946d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 11956d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 11966d63101aSMike Smith return(AE_NO_MEMORY); 11976d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 11986d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 11996d63101aSMike Smith rp->Length = 0; 12006d63101aSMike Smith } 12016d63101aSMike Smith if (res == NULL) 12026d63101aSMike Smith return(AE_OK); 12036d63101aSMike Smith 12046d63101aSMike Smith /* 12056d63101aSMike Smith * Scan the current buffer looking for the terminator. 12066d63101aSMike Smith * This will either find the terminator or hit the end 12076d63101aSMike Smith * of the buffer and return an error. 12086d63101aSMike Smith */ 12096d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 12106d63101aSMike Smith for (;;) { 12116d63101aSMike Smith /* range check, don't go outside the buffer */ 12126d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 12136d63101aSMike Smith return(AE_BAD_PARAMETER); 12146d63101aSMike Smith if ((rp->Id == ACPI_RSTYPE_END_TAG) || 12156d63101aSMike Smith (rp->Length == 0)) { 12166d63101aSMike Smith break; 12176d63101aSMike Smith } 12186d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 12196d63101aSMike Smith } 12206d63101aSMike Smith 12216d63101aSMike Smith /* 12226d63101aSMike Smith * Check the size of the buffer and expand if required. 12236d63101aSMike Smith * 12246d63101aSMike Smith * Required size is: 12256d63101aSMike Smith * size of existing resources before terminator + 12266d63101aSMike Smith * size of new resource and header + 12276d63101aSMike Smith * size of terminator. 12286d63101aSMike Smith * 12296d63101aSMike Smith * Note that this loop should really only run once, unless 12306d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 12316d63101aSMike Smith */ 12326d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 12336d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 12346d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 12356d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 12366d63101aSMike Smith return(AE_NO_MEMORY); 12376d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1238a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1239a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 12406d63101aSMike Smith AcpiOsFree(buf->Pointer); 12416d63101aSMike Smith buf->Pointer = newp; 12426d63101aSMike Smith buf->Length += buf->Length; 12436d63101aSMike Smith } 12446d63101aSMike Smith 12456d63101aSMike Smith /* 12466d63101aSMike Smith * Insert the new resource. 12476d63101aSMike Smith */ 12486d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 12496d63101aSMike Smith 12506d63101aSMike Smith /* 12516d63101aSMike Smith * And add the terminator. 12526d63101aSMike Smith */ 12536d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 12546d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 12556d63101aSMike Smith rp->Length = 0; 12566d63101aSMike Smith 12576f69255bSMike Smith return(AE_OK); 12586f69255bSMike Smith } 1259c5ba0be4SMike Smith 1260da14ac9fSJohn Baldwin /* 1261da14ac9fSJohn Baldwin * Set interrupt model. 1262da14ac9fSJohn Baldwin */ 1263da14ac9fSJohn Baldwin ACPI_STATUS 1264da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1265da14ac9fSJohn Baldwin { 1266da14ac9fSJohn Baldwin ACPI_OBJECT_LIST ArgList; 1267da14ac9fSJohn Baldwin ACPI_OBJECT Arg; 1268da14ac9fSJohn Baldwin 1269da14ac9fSJohn Baldwin Arg.Type = ACPI_TYPE_INTEGER; 1270da14ac9fSJohn Baldwin Arg.Integer.Value = model; 1271da14ac9fSJohn Baldwin ArgList.Count = 1; 1272da14ac9fSJohn Baldwin ArgList.Pointer = &Arg; 1273da14ac9fSJohn Baldwin return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1274da14ac9fSJohn Baldwin } 1275da14ac9fSJohn Baldwin 1276ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1277ece50487SMitsuru IWASAKI 1278ece50487SMitsuru IWASAKI static void 1279ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1280ece50487SMitsuru IWASAKI { 1281ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1282ece50487SMitsuru IWASAKI } 1283c30382dfSMitsuru IWASAKI 128415e32d5dSMike Smith /* 128515e32d5dSMike Smith * Set the system sleep state 128615e32d5dSMike Smith * 128715e32d5dSMike Smith * Currently we only support S1 and S5 128815e32d5dSMike Smith */ 128915e32d5dSMike Smith ACPI_STATUS 129015e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 129115e32d5dSMike Smith { 129215e32d5dSMike Smith ACPI_STATUS status = AE_OK; 129356d8cb57SMitsuru IWASAKI UINT8 TypeA; 129456d8cb57SMitsuru IWASAKI UINT8 TypeB; 129515e32d5dSMike Smith 1296b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1297cb9b0d80SMike Smith ACPI_ASSERTLOCK; 12980ae55423SMike Smith 12996397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 13006397624dSMitsuru IWASAKI return_ACPI_STATUS(AE_BAD_PARAMETER); /* avoid reentry */ 13016397624dSMitsuru IWASAKI 1302ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1303ece50487SMitsuru IWASAKI return_ACPI_STATUS(AE_OK); 1304ece50487SMitsuru IWASAKI 130515e32d5dSMike Smith switch (state) { 130615e32d5dSMike Smith case ACPI_STATE_S0: /* XXX only for testing */ 1307b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1308bfae45aaSMike Smith device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 130915e32d5dSMike Smith } 131015e32d5dSMike Smith break; 131115e32d5dSMike Smith 131215e32d5dSMike Smith case ACPI_STATE_S1: 13136161544cSTakanori Watanabe case ACPI_STATE_S2: 13146161544cSTakanori Watanabe case ACPI_STATE_S3: 13156161544cSTakanori Watanabe case ACPI_STATE_S4: 131698479b04SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB))) { 131798479b04SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", AcpiFormatException(status)); 131856d8cb57SMitsuru IWASAKI break; 131956d8cb57SMitsuru IWASAKI } 132056d8cb57SMitsuru IWASAKI 1321a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1322a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1323a1fccb47SMitsuru IWASAKI 132415e32d5dSMike Smith /* 132515e32d5dSMike Smith * Inform all devices that we are going to sleep. 132615e32d5dSMike Smith */ 132715e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 132815e32d5dSMike Smith /* 132915e32d5dSMike Smith * Re-wake the system. 133015e32d5dSMike Smith * 133115e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 133215e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 133315e32d5dSMike Smith * current bus interface does not provide for this. 133415e32d5dSMike Smith */ 133515e32d5dSMike Smith DEVICE_RESUME(root_bus); 13360ae55423SMike Smith return_ACPI_STATUS(AE_ERROR); 133715e32d5dSMike Smith } 1338b9c780d6SMitsuru IWASAKI 1339b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepStatePrep(state))) { 1340b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1341b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1342b9c780d6SMitsuru IWASAKI break; 1343b9c780d6SMitsuru IWASAKI } 1344b9c780d6SMitsuru IWASAKI 1345ff01efb5SMitsuru IWASAKI if (sc->acpi_sleep_delay > 0) { 1346ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1347ff01efb5SMitsuru IWASAKI } 1348ff01efb5SMitsuru IWASAKI 13496161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 13506161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 13516161544cSTakanori Watanabe 1352a1fccb47SMitsuru IWASAKI /* AcpiEnterSleepState() maybe incompleted, unlock here if locked. */ 1353a1fccb47SMitsuru IWASAKI if (AcpiGbl_AcpiMutexInfo[ACPI_MTX_HARDWARE].OwnerId != ACPI_MUTEX_NOT_ACQUIRED) { 13546161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1355a1fccb47SMitsuru IWASAKI } 13566161544cSTakanori Watanabe 13576161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1358964679ceSMitsuru IWASAKI if (state == ACPI_STATE_S4) { 1359964679ceSMitsuru IWASAKI AcpiEnable(); 13606161544cSTakanori Watanabe } 13616161544cSTakanori Watanabe } else { 1362b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnterSleepState((UINT8)state))) { 1363bfae45aaSMike Smith device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", AcpiFormatException(status)); 1364c30382dfSMitsuru IWASAKI break; 136515e32d5dSMike Smith } 13666161544cSTakanori Watanabe } 1367ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 136815e32d5dSMike Smith DEVICE_RESUME(root_bus); 136915e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 137013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 137115e32d5dSMike Smith break; 137215e32d5dSMike Smith 137315e32d5dSMike Smith case ACPI_STATE_S5: 137415e32d5dSMike Smith /* 137515e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 137615e32d5dSMike Smith * shutdown handlers. 137715e32d5dSMike Smith */ 137815e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 137915e32d5dSMike Smith break; 138015e32d5dSMike Smith 138115e32d5dSMike Smith default: 138215e32d5dSMike Smith status = AE_BAD_PARAMETER; 138315e32d5dSMike Smith break; 138415e32d5dSMike Smith } 1385ece50487SMitsuru IWASAKI 1386ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1387ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1388ece50487SMitsuru IWASAKI 13890ae55423SMike Smith return_ACPI_STATUS(status); 139015e32d5dSMike Smith } 139115e32d5dSMike Smith 139215e32d5dSMike Smith /* 139315e32d5dSMike Smith * Enable/Disable ACPI 139415e32d5dSMike Smith */ 139515e32d5dSMike Smith ACPI_STATUS 139615e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 139715e32d5dSMike Smith { 139815e32d5dSMike Smith ACPI_STATUS status; 139915e32d5dSMike Smith u_int32_t flags; 140015e32d5dSMike Smith 1401b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1402cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14030ae55423SMike Smith 140415e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 140515e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 140615e32d5dSMike Smith if (!sc->acpi_enabled) { 140715e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 140815e32d5dSMike Smith } else { 140915e32d5dSMike Smith status = AE_OK; 141015e32d5dSMike Smith } 141115e32d5dSMike Smith if (status == AE_OK) 141215e32d5dSMike Smith sc->acpi_enabled = 1; 14130ae55423SMike Smith return_ACPI_STATUS(status); 141415e32d5dSMike Smith } 141515e32d5dSMike Smith 141615e32d5dSMike Smith ACPI_STATUS 141715e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 141815e32d5dSMike Smith { 141915e32d5dSMike Smith ACPI_STATUS status; 142015e32d5dSMike Smith 1421b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1422cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14230ae55423SMike Smith 142415e32d5dSMike Smith if (sc->acpi_enabled) { 142515e32d5dSMike Smith status = AcpiDisable(); 142615e32d5dSMike Smith } else { 142715e32d5dSMike Smith status = AE_OK; 142815e32d5dSMike Smith } 142915e32d5dSMike Smith if (status == AE_OK) 143015e32d5dSMike Smith sc->acpi_enabled = 0; 14310ae55423SMike Smith return_ACPI_STATUS(status); 143215e32d5dSMike Smith } 143315e32d5dSMike Smith 143415e32d5dSMike Smith /* 143515e32d5dSMike Smith * ACPI Event Handlers 143615e32d5dSMike Smith */ 143715e32d5dSMike Smith 143815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 143915e32d5dSMike Smith 144015e32d5dSMike Smith static void 144115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 144215e32d5dSMike Smith { 1443b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 144415e32d5dSMike Smith 1445cb9b0d80SMike Smith ACPI_LOCK; 1446a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 144715e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 1448cb9b0d80SMike Smith ACPI_UNLOCK; 14490ae55423SMike Smith return_VOID; 145015e32d5dSMike Smith } 145115e32d5dSMike Smith 145215e32d5dSMike Smith static void 145315e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 145415e32d5dSMike Smith { 1455b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 14560ae55423SMike Smith 145715e32d5dSMike Smith /* Well, what to do? :-) */ 14580ae55423SMike Smith 1459cb9b0d80SMike Smith ACPI_LOCK; 1460cb9b0d80SMike Smith ACPI_UNLOCK; 1461cb9b0d80SMike Smith 14620ae55423SMike Smith return_VOID; 146315e32d5dSMike Smith } 146415e32d5dSMike Smith 146515e32d5dSMike Smith /* 146615e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 146715e32d5dSMike Smith */ 146815e32d5dSMike Smith UINT32 146915e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context) 147015e32d5dSMike Smith { 147115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 147215e32d5dSMike Smith 1473b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 14740ae55423SMike Smith 147515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 14760ae55423SMike Smith 1477b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 147815e32d5dSMike Smith } 147915e32d5dSMike Smith 148015e32d5dSMike Smith UINT32 148115e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context) 148215e32d5dSMike Smith { 148315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 148415e32d5dSMike Smith 1485b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 14860ae55423SMike Smith 148715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 14880ae55423SMike Smith 1489b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 149015e32d5dSMike Smith } 149115e32d5dSMike Smith 149215e32d5dSMike Smith UINT32 149315e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context) 149415e32d5dSMike Smith { 149515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 149615e32d5dSMike Smith 1497b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 14980ae55423SMike Smith 149915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 15000ae55423SMike Smith 1501b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 150215e32d5dSMike Smith } 150315e32d5dSMike Smith 150415e32d5dSMike Smith UINT32 150515e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context) 150615e32d5dSMike Smith { 150715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 150815e32d5dSMike Smith 1509b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 15100ae55423SMike Smith 151115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 15120ae55423SMike Smith 1513b53f2771SMike Smith return_VALUE(ACPI_INTERRUPT_HANDLED); 151415e32d5dSMike Smith } 151515e32d5dSMike Smith 151615e32d5dSMike Smith /* 151715e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 151815e32d5dSMike Smith */ 151915e32d5dSMike Smith struct acpi_staticbuf { 152015e32d5dSMike Smith ACPI_BUFFER buffer; 152115e32d5dSMike Smith char data[512]; 152215e32d5dSMike Smith }; 152315e32d5dSMike Smith 152415e32d5dSMike Smith char * 152515e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 152615e32d5dSMike Smith { 152715e32d5dSMike Smith static struct acpi_staticbuf buf; 152815e32d5dSMike Smith 1529cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1530cb9b0d80SMike Smith 153115e32d5dSMike Smith buf.buffer.Length = 512; 153215e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 153315e32d5dSMike Smith 1534b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 153515e32d5dSMike Smith return(buf.buffer.Pointer); 153615e32d5dSMike Smith return("(unknown path)"); 153715e32d5dSMike Smith } 153815e32d5dSMike Smith 153915e32d5dSMike Smith /* 154015e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 154115e32d5dSMike Smith * parts of the namespace. 154215e32d5dSMike Smith */ 154315e32d5dSMike Smith int 154415e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 154515e32d5dSMike Smith { 15463c600cfbSJohn Baldwin char *cp, *env, *np; 154715e32d5dSMike Smith int len; 154815e32d5dSMike Smith 154915e32d5dSMike Smith np = acpi_name(handle); 155015e32d5dSMike Smith if (*np == '\\') 155115e32d5dSMike Smith np++; 15523c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 155315e32d5dSMike Smith return(0); 155415e32d5dSMike Smith 155515e32d5dSMike Smith /* scan the avoid list checking for a match */ 15563c600cfbSJohn Baldwin cp = env; 155715e32d5dSMike Smith for (;;) { 155815e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 155915e32d5dSMike Smith cp++; 156015e32d5dSMike Smith if (*cp == 0) 156115e32d5dSMike Smith break; 156215e32d5dSMike Smith len = 0; 156315e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 156415e32d5dSMike Smith len++; 1565d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 15663c600cfbSJohn Baldwin freeenv(env); 15670ae55423SMike Smith return(1); 1568d786139cSMaxime Henrion } 15690ae55423SMike Smith cp += len; 15700ae55423SMike Smith } 15713c600cfbSJohn Baldwin freeenv(env); 15720ae55423SMike Smith return(0); 15730ae55423SMike Smith } 15740ae55423SMike Smith 15750ae55423SMike Smith /* 15760ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 15770ae55423SMike Smith */ 15780ae55423SMike Smith int 15790ae55423SMike Smith acpi_disabled(char *subsys) 15800ae55423SMike Smith { 158178689b15SMaxime Henrion char *cp, *env; 15820ae55423SMike Smith int len; 15830ae55423SMike Smith 158478689b15SMaxime Henrion if ((env = getenv("debug.acpi.disable")) == NULL) 15850ae55423SMike Smith return(0); 158678689b15SMaxime Henrion if (!strcmp(env, "all")) { 158778689b15SMaxime Henrion freeenv(env); 15880ae55423SMike Smith return(1); 1589d786139cSMaxime Henrion } 15900ae55423SMike Smith 15910ae55423SMike Smith /* scan the disable list checking for a match */ 159278689b15SMaxime Henrion cp = env; 15930ae55423SMike Smith for (;;) { 15940ae55423SMike Smith while ((*cp != 0) && isspace(*cp)) 15950ae55423SMike Smith cp++; 15960ae55423SMike Smith if (*cp == 0) 15970ae55423SMike Smith break; 15980ae55423SMike Smith len = 0; 15990ae55423SMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 16000ae55423SMike Smith len++; 1601d786139cSMaxime Henrion if (!strncmp(cp, subsys, len)) { 160278689b15SMaxime Henrion freeenv(env); 160315e32d5dSMike Smith return(1); 1604d786139cSMaxime Henrion } 160515e32d5dSMike Smith cp += len; 160615e32d5dSMike Smith } 160778689b15SMaxime Henrion freeenv(env); 160815e32d5dSMike Smith return(0); 160915e32d5dSMike Smith } 161015e32d5dSMike Smith 161115e32d5dSMike Smith /* 1612a1fccb47SMitsuru IWASAKI * Device wake capability enable/disable. 1613a1fccb47SMitsuru IWASAKI */ 1614a1fccb47SMitsuru IWASAKI void 1615a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1616a1fccb47SMitsuru IWASAKI { 1617a1fccb47SMitsuru IWASAKI ACPI_OBJECT_LIST ArgList; 1618a1fccb47SMitsuru IWASAKI ACPI_OBJECT Arg; 1619a1fccb47SMitsuru IWASAKI 1620a1fccb47SMitsuru IWASAKI /* 1621a1fccb47SMitsuru IWASAKI * TBD: All Power Resources referenced by elements 2 through N 1622a1fccb47SMitsuru IWASAKI * of the _PRW object are put into the ON state. 1623a1fccb47SMitsuru IWASAKI */ 1624a1fccb47SMitsuru IWASAKI 1625a1fccb47SMitsuru IWASAKI /* 1626a1fccb47SMitsuru IWASAKI * enable/disable device wake function. 1627a1fccb47SMitsuru IWASAKI */ 1628a1fccb47SMitsuru IWASAKI 1629a1fccb47SMitsuru IWASAKI ArgList.Count = 1; 1630a1fccb47SMitsuru IWASAKI ArgList.Pointer = &Arg; 1631a1fccb47SMitsuru IWASAKI 1632a1fccb47SMitsuru IWASAKI Arg.Type = ACPI_TYPE_INTEGER; 1633a1fccb47SMitsuru IWASAKI Arg.Integer.Value = enable; 1634a1fccb47SMitsuru IWASAKI 1635a1fccb47SMitsuru IWASAKI (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1636a1fccb47SMitsuru IWASAKI } 1637a1fccb47SMitsuru IWASAKI 1638a1fccb47SMitsuru IWASAKI void 1639a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h) 1640a1fccb47SMitsuru IWASAKI { 1641a1fccb47SMitsuru IWASAKI struct acpi_softc *sc; 1642a1fccb47SMitsuru IWASAKI ACPI_STATUS status; 1643a1fccb47SMitsuru IWASAKI ACPI_BUFFER prw_buffer; 1644a1fccb47SMitsuru IWASAKI ACPI_OBJECT *res; 1645a1fccb47SMitsuru IWASAKI 1646a1fccb47SMitsuru IWASAKI ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1647a1fccb47SMitsuru IWASAKI 1648a1fccb47SMitsuru IWASAKI if ((sc = devclass_get_softc(acpi_devclass, 0)) == NULL) { 1649a1fccb47SMitsuru IWASAKI return; 1650a1fccb47SMitsuru IWASAKI } 1651a1fccb47SMitsuru IWASAKI 1652a1fccb47SMitsuru IWASAKI /* 1653a1fccb47SMitsuru IWASAKI * _PRW object is only required for devices that have the ability 1654a1fccb47SMitsuru IWASAKI * to wake the system from a system sleeping state. 1655a1fccb47SMitsuru IWASAKI */ 1656a1fccb47SMitsuru IWASAKI prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1657a1fccb47SMitsuru IWASAKI status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1658a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) { 1659a1fccb47SMitsuru IWASAKI return; 1660a1fccb47SMitsuru IWASAKI } 1661a1fccb47SMitsuru IWASAKI 1662a1fccb47SMitsuru IWASAKI res = (ACPI_OBJECT *)prw_buffer.Pointer; 1663a1fccb47SMitsuru IWASAKI if (res == NULL) { 1664a1fccb47SMitsuru IWASAKI return; 1665a1fccb47SMitsuru IWASAKI } 1666a1fccb47SMitsuru IWASAKI 1667a1fccb47SMitsuru IWASAKI if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1668a1fccb47SMitsuru IWASAKI goto out; 1669a1fccb47SMitsuru IWASAKI } 1670a1fccb47SMitsuru IWASAKI 1671a1fccb47SMitsuru IWASAKI /* 1672a1fccb47SMitsuru IWASAKI * The element 1 of the _PRW object: 1673a1fccb47SMitsuru IWASAKI * The lowest power system sleeping state that can be entered 1674a1fccb47SMitsuru IWASAKI * while still providing wake functionality. 1675a1fccb47SMitsuru IWASAKI * The sleeping state being entered must be greater or equal to 1676a1fccb47SMitsuru IWASAKI * the power state declared in element 1 of the _PRW object. 1677a1fccb47SMitsuru IWASAKI */ 1678a1fccb47SMitsuru IWASAKI if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) { 1679a1fccb47SMitsuru IWASAKI goto out; 1680a1fccb47SMitsuru IWASAKI } 1681a1fccb47SMitsuru IWASAKI 1682a1fccb47SMitsuru IWASAKI if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) { 1683a1fccb47SMitsuru IWASAKI goto out; 1684a1fccb47SMitsuru IWASAKI } 1685a1fccb47SMitsuru IWASAKI 1686a1fccb47SMitsuru IWASAKI /* 1687a1fccb47SMitsuru IWASAKI * The element 0 of the _PRW object: 1688a1fccb47SMitsuru IWASAKI */ 1689a1fccb47SMitsuru IWASAKI switch(res->Package.Elements[0].Type) { 1690a1fccb47SMitsuru IWASAKI case ACPI_TYPE_INTEGER: 1691a1fccb47SMitsuru IWASAKI /* 1692a1fccb47SMitsuru IWASAKI * If the data type of this package element is numeric, then this 1693a1fccb47SMitsuru IWASAKI * _PRW package element is the bit index in the GPEx_EN, in the 1694a1fccb47SMitsuru IWASAKI * GPE blocks described in the FADT, of the enable bit that is 1695a1fccb47SMitsuru IWASAKI * enabled for the wake event. 1696a1fccb47SMitsuru IWASAKI */ 1697a1fccb47SMitsuru IWASAKI 1698a1fccb47SMitsuru IWASAKI status = AcpiEnableEvent(res->Package.Elements[0].Integer.Value, 1699a1fccb47SMitsuru IWASAKI ACPI_EVENT_GPE, ACPI_EVENT_WAKE_ENABLE); 1700a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) 1701a1fccb47SMitsuru IWASAKI printf("%s: EnableEvent Failed\n", __func__); 1702a1fccb47SMitsuru IWASAKI break; 1703a1fccb47SMitsuru IWASAKI 1704a1fccb47SMitsuru IWASAKI case ACPI_TYPE_PACKAGE: 1705a1fccb47SMitsuru IWASAKI /* XXX TBD */ 1706a1fccb47SMitsuru IWASAKI 1707a1fccb47SMitsuru IWASAKI /* 1708a1fccb47SMitsuru IWASAKI * If the data type of this package element is a package, then this 1709a1fccb47SMitsuru IWASAKI * _PRW package element is itself a package containing two 1710a1fccb47SMitsuru IWASAKI * elements. The first is an object reference to the GPE Block 1711a1fccb47SMitsuru IWASAKI * device that contains the GPE that will be triggered by the wake 1712a1fccb47SMitsuru IWASAKI * event. The second element is numeric and it contains the bit 1713a1fccb47SMitsuru IWASAKI * index in the GPEx_EN, in the GPE Block referenced by the 1714a1fccb47SMitsuru IWASAKI * first element in the package, of the enable bit that is enabled for 1715a1fccb47SMitsuru IWASAKI * the wake event. 1716a1fccb47SMitsuru IWASAKI * For example, if this field is a package then it is of the form: 1717a1fccb47SMitsuru IWASAKI * Package() {\_SB.PCI0.ISA.GPE, 2} 1718a1fccb47SMitsuru IWASAKI */ 1719a1fccb47SMitsuru IWASAKI 1720a1fccb47SMitsuru IWASAKI break; 1721a1fccb47SMitsuru IWASAKI 1722a1fccb47SMitsuru IWASAKI default: 1723a1fccb47SMitsuru IWASAKI break; 1724a1fccb47SMitsuru IWASAKI } 1725a1fccb47SMitsuru IWASAKI 1726a1fccb47SMitsuru IWASAKI out: 1727a1fccb47SMitsuru IWASAKI if (prw_buffer.Pointer != NULL) 1728a1fccb47SMitsuru IWASAKI AcpiOsFree(prw_buffer.Pointer); 1729a1fccb47SMitsuru IWASAKI return; 1730a1fccb47SMitsuru IWASAKI } 1731a1fccb47SMitsuru IWASAKI 1732a1fccb47SMitsuru IWASAKI /* 173315e32d5dSMike Smith * Control interface. 173415e32d5dSMike Smith * 17350ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 17360ae55423SMike Smith * drivers wanting to be accessible via /dev/acpi should use the register/deregister 17370ae55423SMike Smith * interface to make their handlers visible. 173815e32d5dSMike Smith */ 17390ae55423SMike Smith struct acpi_ioctl_hook 17400ae55423SMike Smith { 17410ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 17420ae55423SMike Smith u_long cmd; 17430ae55423SMike Smith int (* fn)(u_long cmd, caddr_t addr, void *arg); 17440ae55423SMike Smith void *arg; 17450ae55423SMike Smith }; 17460ae55423SMike Smith 17470ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 17480ae55423SMike Smith static int acpi_ioctl_hooks_initted; 17490ae55423SMike Smith 17500ae55423SMike Smith /* 17510ae55423SMike Smith * Register an ioctl handler. 17520ae55423SMike Smith */ 17530ae55423SMike Smith int 17540ae55423SMike Smith acpi_register_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg), void *arg) 17550ae55423SMike Smith { 17560ae55423SMike Smith struct acpi_ioctl_hook *hp; 17570ae55423SMike Smith 17580ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 17590ae55423SMike Smith return(ENOMEM); 17600ae55423SMike Smith hp->cmd = cmd; 17610ae55423SMike Smith hp->fn = fn; 17620ae55423SMike Smith hp->arg = arg; 17630ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 17640ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 17650ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 17660ae55423SMike Smith } 17670ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 17680ae55423SMike Smith return(0); 17690ae55423SMike Smith } 17700ae55423SMike Smith 17710ae55423SMike Smith /* 17720ae55423SMike Smith * Deregister an ioctl handler. 17730ae55423SMike Smith */ 17740ae55423SMike Smith void 17750ae55423SMike Smith acpi_deregister_ioctl(u_long cmd, int (* fn)(u_long cmd, caddr_t addr, void *arg)) 17760ae55423SMike Smith { 17770ae55423SMike Smith struct acpi_ioctl_hook *hp; 17780ae55423SMike Smith 17790ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 17800ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 17810ae55423SMike Smith break; 17820ae55423SMike Smith 17830ae55423SMike Smith if (hp != NULL) { 17840ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 17850ae55423SMike Smith free(hp, M_ACPIDEV); 17860ae55423SMike Smith } 17870ae55423SMike Smith } 17880ae55423SMike Smith 178915e32d5dSMike Smith static int 1790b40ce416SJulian Elischer acpiopen(dev_t dev, int flag, int fmt, struct thread *td) 179115e32d5dSMike Smith { 179215e32d5dSMike Smith return(0); 179315e32d5dSMike Smith } 179415e32d5dSMike Smith 179515e32d5dSMike Smith static int 1796b40ce416SJulian Elischer acpiclose(dev_t dev, int flag, int fmt, struct thread *td) 179715e32d5dSMike Smith { 179815e32d5dSMike Smith return(0); 179915e32d5dSMike Smith } 180015e32d5dSMike Smith 180115e32d5dSMike Smith static int 1802b40ce416SJulian Elischer acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, struct thread *td) 180315e32d5dSMike Smith { 180415e32d5dSMike Smith struct acpi_softc *sc; 18050ae55423SMike Smith struct acpi_ioctl_hook *hp; 18060ae55423SMike Smith int error, xerror, state; 180715e32d5dSMike Smith 1808cb9b0d80SMike Smith ACPI_LOCK; 1809cb9b0d80SMike Smith 181015e32d5dSMike Smith error = state = 0; 181115e32d5dSMike Smith sc = dev->si_drv1; 181215e32d5dSMike Smith 18130ae55423SMike Smith /* 18140ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 18150ae55423SMike Smith */ 18160ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 18170ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 18180ae55423SMike Smith if (hp->cmd == cmd) { 18190ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 18200ae55423SMike Smith if (xerror != 0) 18210ae55423SMike Smith error = xerror; 1822917d44c8SMitsuru IWASAKI goto out; 18230ae55423SMike Smith } 18240ae55423SMike Smith } 18250ae55423SMike Smith } 18260ae55423SMike Smith 18270ae55423SMike Smith /* 18280ae55423SMike Smith * Core system ioctls. 18290ae55423SMike Smith */ 183015e32d5dSMike Smith switch (cmd) { 183115e32d5dSMike Smith case ACPIIO_ENABLE: 18320ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 183315e32d5dSMike Smith error = ENXIO; 183415e32d5dSMike Smith break; 183515e32d5dSMike Smith 183615e32d5dSMike Smith case ACPIIO_DISABLE: 18370ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 183815e32d5dSMike Smith error = ENXIO; 183915e32d5dSMike Smith break; 184015e32d5dSMike Smith 184115e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 184215e32d5dSMike Smith if (!sc->acpi_enabled) { 184315e32d5dSMike Smith error = ENXIO; 184415e32d5dSMike Smith break; 184515e32d5dSMike Smith } 184615e32d5dSMike Smith state = *(int *)addr; 1847a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 184815e32d5dSMike Smith acpi_SetSleepState(sc, state); 184915e32d5dSMike Smith } else { 185015e32d5dSMike Smith error = EINVAL; 185115e32d5dSMike Smith } 185215e32d5dSMike Smith break; 185315e32d5dSMike Smith 185415e32d5dSMike Smith default: 18550ae55423SMike Smith if (error == 0) 185615e32d5dSMike Smith error = EINVAL; 185715e32d5dSMike Smith break; 185815e32d5dSMike Smith } 1859917d44c8SMitsuru IWASAKI 1860917d44c8SMitsuru IWASAKI out: 1861cb9b0d80SMike Smith ACPI_UNLOCK; 186215e32d5dSMike Smith return(error); 186315e32d5dSMike Smith } 186415e32d5dSMike Smith 18651d073b1dSJohn Baldwin static int 18661d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 18671d073b1dSJohn Baldwin { 18681d073b1dSJohn Baldwin char sleep_state[10]; 18691d073b1dSJohn Baldwin int error; 18701d073b1dSJohn Baldwin u_int new_state, old_state; 18711d073b1dSJohn Baldwin 18721d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 1873b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX+1) { 18741d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 1875cb9b0d80SMike Smith } else { 1876b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 18771d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 18781d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 1879cb9b0d80SMike Smith } 18801d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 18811d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 1882b8670bedSMitsuru IWASAKI for (new_state = ACPI_STATE_S0; new_state <= ACPI_S_STATES_MAX+1; new_state++) { 18831d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 18841d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 18851d073b1dSJohn Baldwin break; 1886cb9b0d80SMike Smith } 1887931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX+1) { 1888931a10c9SMitsuru IWASAKI if (new_state != old_state) { 18891d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 1890931a10c9SMitsuru IWASAKI } 1891cb9b0d80SMike Smith } else { 18921d073b1dSJohn Baldwin error = EINVAL; 18931d073b1dSJohn Baldwin } 1894cb9b0d80SMike Smith } 18951d073b1dSJohn Baldwin return(error); 18961d073b1dSJohn Baldwin } 18971d073b1dSJohn Baldwin 189815e32d5dSMike Smith #ifdef ACPI_DEBUG 18990ae55423SMike Smith /* 19000ae55423SMike Smith * Support for parsing debug options from the kernel environment. 19010ae55423SMike Smith * 19020ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 19030ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 19040ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 19050ae55423SMike Smith * prefixing the bit name with !. 19060ae55423SMike Smith */ 190715e32d5dSMike Smith struct debugtag 190815e32d5dSMike Smith { 190915e32d5dSMike Smith char *name; 191015e32d5dSMike Smith UINT32 value; 191115e32d5dSMike Smith }; 191215e32d5dSMike Smith 191315e32d5dSMike Smith static struct debugtag dbg_layer[] = { 1914c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 1915c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 1916c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 1917c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 1918c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 1919c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 1920c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 1921c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 1922c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 1923d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 19244c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 1925d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 19264c1cdee6SMike Smith 1927cb9b0d80SMike Smith {"ACPI_BUS", ACPI_BUS}, 19284c1cdee6SMike Smith {"ACPI_SYSTEM", ACPI_SYSTEM}, 1929cb9b0d80SMike Smith {"ACPI_POWER", ACPI_POWER}, 1930cb9b0d80SMike Smith {"ACPI_EC", ACPI_EC}, 1931c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 1932c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 1933c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 19344c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 1935cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 19364c1cdee6SMike Smith {"ACPI_FAN", ACPI_FAN}, 19374c1cdee6SMike Smith 1938b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 1939c5ba0be4SMike Smith {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 194015e32d5dSMike Smith {NULL, 0} 194115e32d5dSMike Smith }; 194215e32d5dSMike Smith 194315e32d5dSMike Smith static struct debugtag dbg_level[] = { 19444c1cdee6SMike Smith {"ACPI_LV_OK", ACPI_LV_OK}, 19454c1cdee6SMike Smith {"ACPI_LV_INFO", ACPI_LV_INFO}, 19464c1cdee6SMike Smith {"ACPI_LV_WARN", ACPI_LV_WARN}, 19474c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 19484c1cdee6SMike Smith {"ACPI_LV_FATAL", ACPI_LV_FATAL}, 19494c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 19504c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 1951d62ab2f4SMitsuru IWASAKI 1952d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 19534c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 19544c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 1955d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 19564c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 19574c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 19584c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 19594c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 19604c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 19614c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 19624c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 19634c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 19644c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 19654c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 19664c1cdee6SMike Smith {"ACPI_LV_INIT", ACPI_LV_INIT}, 1967d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 1968d62ab2f4SMitsuru IWASAKI 1969d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 1970d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 1971d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 1972d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 1973d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 19744c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 1975d62ab2f4SMitsuru IWASAKI 1976d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 1977d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 1978d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 1979d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 1980d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 1981d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 1982d62ab2f4SMitsuru IWASAKI 1983d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 198498479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 198598479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 198698479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 198798479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 198898479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 198915e32d5dSMike Smith {NULL, 0} 199015e32d5dSMike Smith }; 199115e32d5dSMike Smith 199215e32d5dSMike Smith static void 199315e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 199415e32d5dSMike Smith { 199515e32d5dSMike Smith char *ep; 199615e32d5dSMike Smith int i, l; 19970ae55423SMike Smith int set; 199815e32d5dSMike Smith 199915e32d5dSMike Smith while (*cp) { 200015e32d5dSMike Smith if (isspace(*cp)) { 200115e32d5dSMike Smith cp++; 200215e32d5dSMike Smith continue; 200315e32d5dSMike Smith } 200415e32d5dSMike Smith ep = cp; 200515e32d5dSMike Smith while (*ep && !isspace(*ep)) 200615e32d5dSMike Smith ep++; 20070ae55423SMike Smith if (*cp == '!') { 20080ae55423SMike Smith set = 0; 20090ae55423SMike Smith cp++; 20100ae55423SMike Smith if (cp == ep) 20110ae55423SMike Smith continue; 20120ae55423SMike Smith } else { 20130ae55423SMike Smith set = 1; 20140ae55423SMike Smith } 201515e32d5dSMike Smith l = ep - cp; 201615e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 201715e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 20180ae55423SMike Smith if (set) { 201915e32d5dSMike Smith *flag |= tag[i].value; 20200ae55423SMike Smith } else { 20210ae55423SMike Smith *flag &= ~tag[i].value; 20220ae55423SMike Smith } 202315e32d5dSMike Smith printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 202415e32d5dSMike Smith } 202515e32d5dSMike Smith } 202615e32d5dSMike Smith cp = ep; 202715e32d5dSMike Smith } 202815e32d5dSMike Smith } 202915e32d5dSMike Smith 203015e32d5dSMike Smith static void 20312a4ac806SMike Smith acpi_set_debugging(void *junk) 203215e32d5dSMike Smith { 203394aae282SMike Barcroft char *cp; 203415e32d5dSMike Smith 20350ae55423SMike Smith AcpiDbgLayer = 0; 20360ae55423SMike Smith AcpiDbgLevel = 0; 203794aae282SMike Barcroft if ((cp = getenv("debug.acpi.layer")) != NULL) { 203815e32d5dSMike Smith acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 203994aae282SMike Barcroft freeenv(cp); 204094aae282SMike Barcroft } 204194aae282SMike Barcroft if ((cp = getenv("debug.acpi.level")) != NULL) { 204215e32d5dSMike Smith acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 204394aae282SMike Barcroft freeenv(cp); 204494aae282SMike Barcroft } 20450ae55423SMike Smith 20460ae55423SMike Smith printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, AcpiDbgLevel); 204715e32d5dSMike Smith } 20482a4ac806SMike Smith SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, NULL); 204915e32d5dSMike Smith #endif 2050f9390180SMitsuru IWASAKI 2051f9390180SMitsuru IWASAKI static int 2052f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2053f9390180SMitsuru IWASAKI { 2054f9390180SMitsuru IWASAKI int state, acpi_state; 2055f9390180SMitsuru IWASAKI int error; 2056f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2057f9390180SMitsuru IWASAKI va_list ap; 2058f9390180SMitsuru IWASAKI 2059f9390180SMitsuru IWASAKI error = 0; 2060f9390180SMitsuru IWASAKI switch (cmd) { 2061f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2062f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2063f9390180SMitsuru IWASAKI if (sc == NULL) { 2064f9390180SMitsuru IWASAKI error = EINVAL; 2065f9390180SMitsuru IWASAKI goto out; 2066f9390180SMitsuru IWASAKI } 2067f9390180SMitsuru IWASAKI 2068f9390180SMitsuru IWASAKI va_start(ap, arg); 2069f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2070f9390180SMitsuru IWASAKI va_end(ap); 2071f9390180SMitsuru IWASAKI 2072f9390180SMitsuru IWASAKI switch (state) { 2073f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2074f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2075f9390180SMitsuru IWASAKI break; 2076f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2077f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2078f9390180SMitsuru IWASAKI break; 2079f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2080f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2081f9390180SMitsuru IWASAKI break; 2082f9390180SMitsuru IWASAKI default: 2083f9390180SMitsuru IWASAKI error = EINVAL; 2084f9390180SMitsuru IWASAKI goto out; 2085f9390180SMitsuru IWASAKI } 2086f9390180SMitsuru IWASAKI 2087f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2088f9390180SMitsuru IWASAKI break; 2089f9390180SMitsuru IWASAKI 2090f9390180SMitsuru IWASAKI default: 2091f9390180SMitsuru IWASAKI error = EINVAL; 2092f9390180SMitsuru IWASAKI goto out; 2093f9390180SMitsuru IWASAKI } 2094f9390180SMitsuru IWASAKI 2095f9390180SMitsuru IWASAKI out: 2096f9390180SMitsuru IWASAKI return (error); 2097f9390180SMitsuru IWASAKI } 2098f9390180SMitsuru IWASAKI 2099f9390180SMitsuru IWASAKI static void 2100f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2101f9390180SMitsuru IWASAKI { 21026c407052SMitsuru IWASAKI int error; 21036c407052SMitsuru IWASAKI 21046c407052SMitsuru IWASAKI if (!resource_int_value("acpi", 0, "disabled", &error) && 21056c407052SMitsuru IWASAKI (error != 0)) 21066c407052SMitsuru IWASAKI return; 2107f9390180SMitsuru IWASAKI 2108f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2109f9390180SMitsuru IWASAKI } 2110f9390180SMitsuru IWASAKI 2111f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2112f9390180SMitsuru IWASAKI 2113