115e32d5dSMike Smith /*- 215e32d5dSMike Smith * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 315e32d5dSMike Smith * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4cb9b0d80SMike Smith * Copyright (c) 2000, 2001 Michael Smith 515e32d5dSMike Smith * Copyright (c) 2000 BSDi 615e32d5dSMike Smith * All rights reserved. 715e32d5dSMike Smith * 815e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without 915e32d5dSMike Smith * modification, are permitted provided that the following conditions 1015e32d5dSMike Smith * are met: 1115e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright 1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer. 1315e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1415e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the 1515e32d5dSMike Smith * documentation and/or other materials provided with the distribution. 1615e32d5dSMike Smith * 1715e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1815e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1915e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2015e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2115e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2215e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2315e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2415e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2515e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2615e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2715e32d5dSMike Smith * SUCH DAMAGE. 2815e32d5dSMike Smith * 2915e32d5dSMike Smith * $FreeBSD$ 3015e32d5dSMike Smith */ 3115e32d5dSMike Smith 3215e32d5dSMike Smith #include "opt_acpi.h" 3315e32d5dSMike Smith #include <sys/param.h> 3415e32d5dSMike Smith #include <sys/kernel.h> 35fb919e4dSMark Murray #include <sys/proc.h> 36a89fcf28STakanori Watanabe #include <sys/fcntl.h> 3715e32d5dSMike Smith #include <sys/malloc.h> 3815e32d5dSMike Smith #include <sys/bus.h> 3915e32d5dSMike Smith #include <sys/conf.h> 4015e32d5dSMike Smith #include <sys/ioccom.h> 4115e32d5dSMike Smith #include <sys/reboot.h> 4215e32d5dSMike Smith #include <sys/sysctl.h> 4315e32d5dSMike Smith #include <sys/ctype.h> 441611ea87SMitsuru IWASAKI #include <sys/linker.h> 45f9390180SMitsuru IWASAKI #include <sys/power.h> 4615e32d5dSMike Smith 4715e32d5dSMike Smith #include <machine/clock.h> 4815e32d5dSMike Smith #include <machine/resource.h> 49bc0f2195SMike Smith #include <isa/isavar.h> 50bc0f2195SMike Smith 5115e32d5dSMike Smith #include "acpi.h" 5215e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5315e32d5dSMike Smith #include <dev/acpica/acpiio.h> 5415e32d5dSMike Smith 5515e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 5615e32d5dSMike Smith 57be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 58cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 59b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 600ae55423SMike Smith 6115e32d5dSMike Smith static d_open_t acpiopen; 6215e32d5dSMike Smith static d_close_t acpiclose; 6315e32d5dSMike Smith static d_ioctl_t acpiioctl; 6415e32d5dSMike Smith 6515e32d5dSMike Smith #define CDEV_MAJOR 152 6615e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 677ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 687ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 697ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 707ac40f5fSPoul-Henning Kamp .d_name = "acpi", 717ac40f5fSPoul-Henning Kamp .d_maj = CDEV_MAJOR, 7215e32d5dSMike Smith }; 7315e32d5dSMike Smith 741d073b1dSJohn Baldwin static const char* sleep_state_names[] = { 75b8670bedSMitsuru IWASAKI "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 761d073b1dSJohn Baldwin 772a4ac806SMike Smith /* this has to be static, as the softc is gone when we need it */ 782a4ac806SMike Smith static int acpi_off_state = ACPI_STATE_S5; 792a4ac806SMike Smith 80fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 81cb9b0d80SMike Smith struct mtx acpi_mutex; 82fc0ea94aSJohn Baldwin #endif 83cb9b0d80SMike Smith 846d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 8515e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 8615e32d5dSMike Smith static int acpi_probe(device_t dev); 8715e32d5dSMike Smith static int acpi_attach(device_t dev); 88be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 89be2b1797SNate Lawson int unit); 9015e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 91be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 92be2b1797SNate Lawson uintptr_t *result); 93be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 94be2b1797SNate Lawson uintptr_t value); 95be2b1797SNate Lawson static int acpi_set_resource(device_t dev, device_t child, int type, 96be2b1797SNate Lawson int rid, u_long start, u_long count); 97be2b1797SNate Lawson static int acpi_get_resource(device_t dev, device_t child, int type, 98be2b1797SNate Lawson int rid, u_long *startp, u_long *countp); 99be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 100be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 101be2b1797SNate Lawson u_long count, u_int flags); 102be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 103be2b1797SNate Lawson int rid, struct resource *r); 10432d18aa5SMike Smith static u_int32_t acpi_isa_get_logicalid(device_t dev); 105b2566207STakanori Watanabe static u_int32_t acpi_isa_get_compatid(device_t dev); 106be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 107be2b1797SNate Lawson struct isa_pnp_id *ids); 10815e32d5dSMike Smith static void acpi_probe_children(device_t bus); 109be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 110be2b1797SNate Lawson void *context, void **status); 11115e32d5dSMike Smith static void acpi_shutdown_pre_sync(void *arg, int howto); 11215e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 11313d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 11415e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 11515e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 116d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1171d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 118f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 119f9390180SMitsuru IWASAKI 12015e32d5dSMike Smith static device_method_t acpi_methods[] = { 12115e32d5dSMike Smith /* Device interface */ 12215e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 12315e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 12415e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 12515e32d5dSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 12615e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 12715e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 12815e32d5dSMike Smith 12915e32d5dSMike Smith /* Bus interface */ 13015e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 13115e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 13215e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 13315e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 13415e32d5dSMike Smith DEVMETHOD(bus_set_resource, acpi_set_resource), 13515e32d5dSMike Smith DEVMETHOD(bus_get_resource, acpi_get_resource), 13615e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 13715e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 13815e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 13915e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 14015e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 14115e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 14215e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 14315e32d5dSMike Smith 144bc0f2195SMike Smith /* ISA emulation */ 145bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 146bc0f2195SMike Smith 14715e32d5dSMike Smith {0, 0} 14815e32d5dSMike Smith }; 14915e32d5dSMike Smith 15015e32d5dSMike Smith static driver_t acpi_driver = { 15115e32d5dSMike Smith "acpi", 15215e32d5dSMike Smith acpi_methods, 15315e32d5dSMike Smith sizeof(struct acpi_softc), 15415e32d5dSMike Smith }; 15515e32d5dSMike Smith 1563273b005SMike Smith static devclass_t acpi_devclass; 1576d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 158dde24897SMike Smith MODULE_VERSION(acpi, 100); 15915e32d5dSMike Smith 160be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_debug_layer, CTLFLAG_RW, &AcpiDbgLayer, 0, 161be2b1797SNate Lawson ""); 162be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_debug_level, CTLFLAG_RW, &AcpiDbgLevel, 0, 163be2b1797SNate Lawson ""); 164dde24897SMike Smith static int acpi_ca_version = ACPI_CA_VERSION; 165be2b1797SNate Lawson SYSCTL_INT(_debug, OID_AUTO, acpi_ca_version, CTLFLAG_RD, &acpi_ca_version, 0, 166be2b1797SNate Lawson ""); 16715e32d5dSMike Smith 16815e32d5dSMike Smith /* 1696d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 1706d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 171be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 172be2b1797SNate Lawson * off it. 1736d63101aSMike Smith */ 1746d63101aSMike Smith static int 1756d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 1766d63101aSMike Smith { 1776d63101aSMike Smith switch(event) { 1786d63101aSMike Smith case MOD_LOAD: 17987b45ed5SMitsuru IWASAKI if (!cold) { 18087b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 1816d63101aSMike Smith return (EPERM); 18287b45ed5SMitsuru IWASAKI } 1836d63101aSMike Smith break; 1846d63101aSMike Smith case MOD_UNLOAD: 185f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 1866d63101aSMike Smith return (EBUSY); 187f9390180SMitsuru IWASAKI break; 1886d63101aSMike Smith default: 1896d63101aSMike Smith break; 1906d63101aSMike Smith } 1916d63101aSMike Smith return (0); 1926d63101aSMike Smith } 1936d63101aSMike Smith 1946d63101aSMike Smith /* 19515e32d5dSMike Smith * Detect ACPI, perform early initialisation 19615e32d5dSMike Smith */ 19715e32d5dSMike Smith static void 19815e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent) 19915e32d5dSMike Smith { 20015e32d5dSMike Smith device_t child; 20115e32d5dSMike Smith int error; 202d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 203d786139cSMaxime Henrion char *debugpoint; 20415e32d5dSMike Smith #endif 20515e32d5dSMike Smith 206b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2070ae55423SMike Smith 20887b45ed5SMitsuru IWASAKI if (!cold) 20987b45ed5SMitsuru IWASAKI return_VOID; 210840f9b53STakanori Watanabe 211be2b1797SNate Lawson /* Check that we haven't been disabled with a hint. */ 2128a9bc9c0SJohn Baldwin if (resource_disabled("acpi", 0)) 21332d18aa5SMike Smith return_VOID; 21432d18aa5SMike Smith 215be2b1797SNate Lawson /* Make sure we're not being doubly invoked. */ 21615e32d5dSMike Smith if (device_find_child(parent, "acpi", 0) != NULL) 2170ae55423SMike Smith return_VOID; 21815e32d5dSMike Smith 219fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 220be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2216008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 222fc0ea94aSJohn Baldwin #endif 223cb9b0d80SMike Smith 224be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 225d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 226d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 227d786139cSMaxime Henrion if (debugpoint) { 228d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 22915e32d5dSMike Smith acpi_EnterDebugger(); 230d786139cSMaxime Henrion freeenv(debugpoint); 231d786139cSMaxime Henrion } 23215e32d5dSMike Smith #endif 233b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 234bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 2350ae55423SMike Smith return_VOID; 23615e32d5dSMike Smith } 237d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 238d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 239d786139cSMaxime Henrion if (debugpoint) { 240d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 24115e32d5dSMike Smith acpi_EnterDebugger(); 242d786139cSMaxime Henrion freeenv(debugpoint); 243d786139cSMaxime Henrion } 24415e32d5dSMike Smith #endif 2451611ea87SMitsuru IWASAKI 246b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 247bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 2480ae55423SMike Smith return_VOID; 24915e32d5dSMike Smith } 25015e32d5dSMike Smith 251be2b1797SNate Lawson /* Attach the actual ACPI device. */ 25215e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 25315e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 2540ae55423SMike Smith return_VOID; 25515e32d5dSMike Smith } 25615e32d5dSMike Smith } 25715e32d5dSMike Smith 25815e32d5dSMike Smith /* 25915e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 26015e32d5dSMike Smith */ 26115e32d5dSMike Smith static int 26215e32d5dSMike Smith acpi_probe(device_t dev) 26315e32d5dSMike Smith { 26415e32d5dSMike Smith ACPI_TABLE_HEADER th; 26515e32d5dSMike Smith char buf[20]; 266cb9b0d80SMike Smith ACPI_STATUS status; 26715e32d5dSMike Smith int error; 268fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 26915e32d5dSMike Smith 270b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 271f9390180SMitsuru IWASAKI 272f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 273f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 274be2b1797SNate Lawson 275f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 276f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 277f9390180SMitsuru IWASAKI } 278f9390180SMitsuru IWASAKI 279cb9b0d80SMike Smith ACPI_LOCK; 2800ae55423SMike Smith 281b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 282be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 283be2b1797SNate Lawson AcpiFormatException(status)); 284cb9b0d80SMike Smith error = ENXIO; 285cb9b0d80SMike Smith } else { 28615e32d5dSMike Smith sprintf(buf, "%.6s %.8s", th.OemId, th.OemTableId); 28715e32d5dSMike Smith device_set_desc_copy(dev, buf); 288cb9b0d80SMike Smith error = 0; 289cb9b0d80SMike Smith } 290cb9b0d80SMike Smith ACPI_UNLOCK; 291cb9b0d80SMike Smith return_VALUE(error); 29215e32d5dSMike Smith } 29315e32d5dSMike Smith 29415e32d5dSMike Smith static int 29515e32d5dSMike Smith acpi_attach(device_t dev) 29615e32d5dSMike Smith { 29715e32d5dSMike Smith struct acpi_softc *sc; 298cb9b0d80SMike Smith ACPI_STATUS status; 29915e32d5dSMike Smith int error; 30032d18aa5SMike Smith UINT32 flags; 301498d464fSMitsuru IWASAKI char *env; 302d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 303d786139cSMaxime Henrion char *debugpoint; 30415e32d5dSMike Smith #endif 305fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 30615e32d5dSMike Smith 307b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 308cb9b0d80SMike Smith ACPI_LOCK; 30915e32d5dSMike Smith sc = device_get_softc(dev); 31015e32d5dSMike Smith bzero(sc, sizeof(*sc)); 31115e32d5dSMike Smith sc->acpi_dev = dev; 31215e32d5dSMike Smith 313d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 314d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 315d786139cSMaxime Henrion if (debugpoint) { 316d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 31715e32d5dSMike Smith acpi_EnterDebugger(); 318d786139cSMaxime Henrion freeenv(debugpoint); 319d786139cSMaxime Henrion } 32015e32d5dSMike Smith #endif 32115e32d5dSMike Smith 322be2b1797SNate Lawson /* Install the default address space handlers. */ 323cb9b0d80SMike Smith error = ENXIO; 324be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 325be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 326be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 327be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 328be2b1797SNate Lawson AcpiFormatException(status)); 329cb9b0d80SMike Smith goto out; 33015e32d5dSMike Smith } 331be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 332be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 333be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 334be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 335be2b1797SNate Lawson AcpiFormatException(status)); 336cb9b0d80SMike Smith goto out; 33715e32d5dSMike Smith } 338be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 339be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 340be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 341be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 342be2b1797SNate Lawson AcpiFormatException(status)); 343cb9b0d80SMike Smith goto out; 34415e32d5dSMike Smith } 34515e32d5dSMike Smith 34615e32d5dSMike Smith /* 34715e32d5dSMike Smith * Bring ACPI fully online. 34815e32d5dSMike Smith * 349be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 350be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 351be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 352be2b1797SNate Lawson * object init pass. 35315e32d5dSMike Smith * 35432d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 35532d18aa5SMike Smith * 356be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 357be2b1797SNate Lawson * all our child devices, but on many systems it works here. 35815e32d5dSMike Smith */ 359d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 360d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 361d786139cSMaxime Henrion if (debugpoint) { 362d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 36315e32d5dSMike Smith acpi_EnterDebugger(); 364d786139cSMaxime Henrion freeenv(debugpoint); 365d786139cSMaxime Henrion } 36615e32d5dSMike Smith #endif 36732d18aa5SMike Smith flags = 0; 368d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 36932d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 370b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 371be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 372be2b1797SNate Lawson AcpiFormatException(status)); 373cb9b0d80SMike Smith goto out; 37415e32d5dSMike Smith } 37515e32d5dSMike Smith 376f8335e3aSNate Lawson /* 377f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 378f8335e3aSNate Lawson * the namespace has been evaluated. 379f8335e3aSNate Lawson */ 380f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 381f8335e3aSNate Lawson 382b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 383be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 384be2b1797SNate Lawson AcpiFormatException(status)); 385b69ed3f4SMitsuru IWASAKI goto out; 386b69ed3f4SMitsuru IWASAKI } 387b69ed3f4SMitsuru IWASAKI 38815e32d5dSMike Smith /* 3891d073b1dSJohn Baldwin * Setup our sysctl tree. 3901d073b1dSJohn Baldwin * 3911d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 3921d073b1dSJohn Baldwin */ 3931d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 3941d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 3951d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 3961d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 3971d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 398d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 399d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 400d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4011d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4021d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4031d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4041d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4051d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4061d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4071d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4081d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 409f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 410f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 411f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 412f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 413f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 414f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4152d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 416ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 417ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 418ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4191611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 4201611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 4211611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4222d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 4232d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 424c6a78e98STakanori Watanabe SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 425c6a78e98STakanori Watanabe OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 426c6a78e98STakanori Watanabe &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 427bf0c18ecSNate Lawson 428bf0c18ecSNate Lawson /* 429bf0c18ecSNate Lawson * Default to 5 seconds before sleeping to give some machines time to 430bf0c18ecSNate Lawson * stabilize. 431bf0c18ecSNate Lawson */ 432bf0c18ecSNate Lawson sc->acpi_sleep_delay = 5; 433c6a78e98STakanori Watanabe sc->acpi_disable_on_poweroff = 1; 4342d644607SMitsuru IWASAKI if (bootverbose) 4352d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 436498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 437498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 438498d464fSMitsuru IWASAKI freeenv(env); 439498d464fSMitsuru IWASAKI } 4401d073b1dSJohn Baldwin 4412d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 4422d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 4432d610c46SNate Lawson sc->acpi_s4bios = 1; 4442d610c46SNate Lawson 4451d073b1dSJohn Baldwin /* 44615e32d5dSMike Smith * Dispatch the default sleep state to devices. 44715e32d5dSMike Smith * TBD: should be configured from userland policy manager. 44815e32d5dSMike Smith */ 44915e32d5dSMike Smith sc->acpi_power_button_sx = ACPI_POWER_BUTTON_DEFAULT_SX; 45015e32d5dSMike Smith sc->acpi_sleep_button_sx = ACPI_SLEEP_BUTTON_DEFAULT_SX; 45115e32d5dSMike Smith sc->acpi_lid_switch_sx = ACPI_LID_SWITCH_DEFAULT_SX; 452f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 453f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 45415e32d5dSMike Smith 45513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 45615e32d5dSMike Smith 45715e32d5dSMike Smith /* 45815e32d5dSMike Smith * Scan the namespace and attach/initialise children. 45915e32d5dSMike Smith */ 460d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 461d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 462d786139cSMaxime Henrion if (debugpoint) { 463d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 46415e32d5dSMike Smith acpi_EnterDebugger(); 465d786139cSMaxime Henrion freeenv(debugpoint); 466d786139cSMaxime Henrion } 46715e32d5dSMike Smith #endif 46815e32d5dSMike Smith 469be2b1797SNate Lawson /* Register our shutdown handlers */ 470be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, 471be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 472be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 473be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 47415e32d5dSMike Smith 47515e32d5dSMike Smith /* 47615e32d5dSMike Smith * Register our acpi event handlers. 47715e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 47815e32d5dSMike Smith */ 479be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 480be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 481be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 482be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 48315e32d5dSMike Smith 484be2b1797SNate Lawson /* Flag our initial states. */ 48515e32d5dSMike Smith sc->acpi_enabled = 1; 48615e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 487ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 48815e32d5dSMike Smith 489be2b1797SNate Lawson /* Create the control device */ 490a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 4912a4689e8SRobert Watson "acpi"); 49215e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 49315e32d5dSMike Smith 494d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 495d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 496d786139cSMaxime Henrion if (debugpoint) { 497be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 49815e32d5dSMike Smith acpi_EnterDebugger(); 499d786139cSMaxime Henrion freeenv(debugpoint); 500d786139cSMaxime Henrion } 50115e32d5dSMike Smith #endif 502f86214b6SMitsuru IWASAKI 50391da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 504be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 505c573e654SMitsuru IWASAKI goto out; 506c573e654SMitsuru IWASAKI #endif 507c573e654SMitsuru IWASAKI 508be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 509f86214b6SMitsuru IWASAKI goto out; 510f86214b6SMitsuru IWASAKI 511f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 512f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 513f9390180SMitsuru IWASAKI 514eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 515eeb6dba2SJohn Baldwin acpi_probe_children(dev); 516eeb6dba2SJohn Baldwin 517cb9b0d80SMike Smith error = 0; 518cb9b0d80SMike Smith 519cb9b0d80SMike Smith out: 520cb9b0d80SMike Smith ACPI_UNLOCK; 521cb9b0d80SMike Smith return_VALUE (error); 52215e32d5dSMike Smith } 52315e32d5dSMike Smith 52415e32d5dSMike Smith /* 52515e32d5dSMike Smith * Handle a new device being added 52615e32d5dSMike Smith */ 52715e32d5dSMike Smith static device_t 52815e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 52915e32d5dSMike Smith { 53015e32d5dSMike Smith struct acpi_device *ad; 53115e32d5dSMike Smith device_t child; 53215e32d5dSMike Smith 533be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 53415e32d5dSMike Smith return (NULL); 53515e32d5dSMike Smith 53615e32d5dSMike Smith resource_list_init(&ad->ad_rl); 53715e32d5dSMike Smith 53815e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 53915e32d5dSMike Smith if (child != NULL) 54015e32d5dSMike Smith device_set_ivars(child, ad); 54115e32d5dSMike Smith return (child); 54215e32d5dSMike Smith } 54315e32d5dSMike Smith 54415e32d5dSMike Smith static int 54515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 54615e32d5dSMike Smith { 54715e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 54815e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 54915e32d5dSMike Smith int retval = 0; 55015e32d5dSMike Smith 55115e32d5dSMike Smith retval += bus_print_child_header(bus, child); 5529ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 5539ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 5549ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 5559ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 55615e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 55715e32d5dSMike Smith 55815e32d5dSMike Smith return (retval); 55915e32d5dSMike Smith } 56015e32d5dSMike Smith 56115e32d5dSMike Smith 56215e32d5dSMike Smith /* 56315e32d5dSMike Smith * Handle per-device ivars 56415e32d5dSMike Smith */ 56515e32d5dSMike Smith static int 56615e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 56715e32d5dSMike Smith { 56815e32d5dSMike Smith struct acpi_device *ad; 56915e32d5dSMike Smith 57015e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 57115e32d5dSMike Smith printf("device has no ivars\n"); 57215e32d5dSMike Smith return (ENOENT); 57315e32d5dSMike Smith } 57415e32d5dSMike Smith 575be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 57615e32d5dSMike Smith switch(index) { 57715e32d5dSMike Smith case ACPI_IVAR_HANDLE: 57815e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 57915e32d5dSMike Smith break; 58015e32d5dSMike Smith case ACPI_IVAR_MAGIC: 58115e32d5dSMike Smith *(int *)result = ad->ad_magic; 58215e32d5dSMike Smith break; 58315e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 58415e32d5dSMike Smith *(void **)result = ad->ad_private; 58515e32d5dSMike Smith break; 58632d18aa5SMike Smith case ISA_IVAR_VENDORID: 58732d18aa5SMike Smith case ISA_IVAR_SERIAL: 58832d18aa5SMike Smith case ISA_IVAR_COMPATID: 58932d18aa5SMike Smith *(int *)result = -1; 59032d18aa5SMike Smith break; 59132d18aa5SMike Smith case ISA_IVAR_LOGICALID: 59232d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 59332d18aa5SMike Smith break; 59415e32d5dSMike Smith default: 59515e32d5dSMike Smith return (ENOENT); 59615e32d5dSMike Smith } 597be2b1797SNate Lawson 59815e32d5dSMike Smith return (0); 59915e32d5dSMike Smith } 60015e32d5dSMike Smith 60115e32d5dSMike Smith static int 60215e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 60315e32d5dSMike Smith { 60415e32d5dSMike Smith struct acpi_device *ad; 60515e32d5dSMike Smith 60615e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 60715e32d5dSMike Smith printf("device has no ivars\n"); 60815e32d5dSMike Smith return (ENOENT); 60915e32d5dSMike Smith } 61015e32d5dSMike Smith 61115e32d5dSMike Smith switch(index) { 61215e32d5dSMike Smith case ACPI_IVAR_HANDLE: 61315e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 61415e32d5dSMike Smith break; 61515e32d5dSMike Smith case ACPI_IVAR_MAGIC: 61615e32d5dSMike Smith ad->ad_magic = (int)value; 61715e32d5dSMike Smith break; 61815e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 61915e32d5dSMike Smith ad->ad_private = (void *)value; 62015e32d5dSMike Smith break; 62115e32d5dSMike Smith default: 6222ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 62315e32d5dSMike Smith return (ENOENT); 62415e32d5dSMike Smith } 625be2b1797SNate Lawson 62615e32d5dSMike Smith return (0); 62715e32d5dSMike Smith } 62815e32d5dSMike Smith 629be2b1797SNate Lawson ACPI_HANDLE 630be2b1797SNate Lawson acpi_get_handle(device_t dev) 631be2b1797SNate Lawson { 632be2b1797SNate Lawson uintptr_t up; 633be2b1797SNate Lawson ACPI_HANDLE h; 634be2b1797SNate Lawson 635be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, &up)) 636be2b1797SNate Lawson return(NULL); 637be2b1797SNate Lawson h = (ACPI_HANDLE)up; 638be2b1797SNate Lawson return (h); 639be2b1797SNate Lawson } 640be2b1797SNate Lawson 641be2b1797SNate Lawson int 642be2b1797SNate Lawson acpi_set_handle(device_t dev, ACPI_HANDLE h) 643be2b1797SNate Lawson { 644be2b1797SNate Lawson uintptr_t up; 645be2b1797SNate Lawson 646be2b1797SNate Lawson up = (uintptr_t)h; 647be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_HANDLE, up)); 648be2b1797SNate Lawson } 649be2b1797SNate Lawson 650be2b1797SNate Lawson int 651be2b1797SNate Lawson acpi_get_magic(device_t dev) 652be2b1797SNate Lawson { 653be2b1797SNate Lawson uintptr_t up; 654be2b1797SNate Lawson int m; 655be2b1797SNate Lawson 656be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, &up)) 657be2b1797SNate Lawson return(0); 658be2b1797SNate Lawson m = (int)up; 659be2b1797SNate Lawson return (m); 660be2b1797SNate Lawson } 661be2b1797SNate Lawson 662be2b1797SNate Lawson int 663be2b1797SNate Lawson acpi_set_magic(device_t dev, int m) 664be2b1797SNate Lawson { 665be2b1797SNate Lawson uintptr_t up; 666be2b1797SNate Lawson 667be2b1797SNate Lawson up = (uintptr_t)m; 668be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_MAGIC, up)); 669be2b1797SNate Lawson } 670be2b1797SNate Lawson 671be2b1797SNate Lawson void * 672be2b1797SNate Lawson acpi_get_private(device_t dev) 673be2b1797SNate Lawson { 674be2b1797SNate Lawson uintptr_t up; 675be2b1797SNate Lawson void *p; 676be2b1797SNate Lawson 677be2b1797SNate Lawson if (BUS_READ_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, &up)) 678be2b1797SNate Lawson return (NULL); 679be2b1797SNate Lawson p = (void *)up; 680be2b1797SNate Lawson return (p); 681be2b1797SNate Lawson } 682be2b1797SNate Lawson 683be2b1797SNate Lawson int 684be2b1797SNate Lawson acpi_set_private(device_t dev, void *p) 685be2b1797SNate Lawson { 686be2b1797SNate Lawson uintptr_t up; 687be2b1797SNate Lawson 688be2b1797SNate Lawson up = (uintptr_t)p; 689be2b1797SNate Lawson return (BUS_WRITE_IVAR(device_get_parent(dev), dev, ACPI_IVAR_PRIVATE, up)); 690be2b1797SNate Lawson } 691be2b1797SNate Lawson 692be2b1797SNate Lawson ACPI_OBJECT_TYPE 693be2b1797SNate Lawson acpi_get_type(device_t dev) 694be2b1797SNate Lawson { 695be2b1797SNate Lawson ACPI_HANDLE h; 696be2b1797SNate Lawson ACPI_OBJECT_TYPE t; 697be2b1797SNate Lawson 698be2b1797SNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 699be2b1797SNate Lawson return (ACPI_TYPE_NOT_FOUND); 700be2b1797SNate Lawson if (AcpiGetType(h, &t) != AE_OK) 701be2b1797SNate Lawson return (ACPI_TYPE_NOT_FOUND); 702be2b1797SNate Lawson return (t); 703be2b1797SNate Lawson } 704be2b1797SNate Lawson 70515e32d5dSMike Smith /* 70615e32d5dSMike Smith * Handle child resource allocation/removal 70715e32d5dSMike Smith */ 70815e32d5dSMike Smith static int 709be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid, 710be2b1797SNate Lawson u_long start, u_long count) 71115e32d5dSMike Smith { 71215e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 71315e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 71415e32d5dSMike Smith 71515e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 71615e32d5dSMike Smith 71715e32d5dSMike Smith return(0); 71815e32d5dSMike Smith } 71915e32d5dSMike Smith 72015e32d5dSMike Smith static int 721be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid, 722be2b1797SNate Lawson u_long *startp, u_long *countp) 72315e32d5dSMike Smith { 72415e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 72515e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 72615e32d5dSMike Smith struct resource_list_entry *rle; 72715e32d5dSMike Smith 72815e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 72915e32d5dSMike Smith if (!rle) 73015e32d5dSMike Smith return(ENOENT); 73115e32d5dSMike Smith 73215e32d5dSMike Smith if (startp) 73315e32d5dSMike Smith *startp = rle->start; 73415e32d5dSMike Smith if (countp) 73515e32d5dSMike Smith *countp = rle->count; 73615e32d5dSMike Smith 73715e32d5dSMike Smith return (0); 73815e32d5dSMike Smith } 73915e32d5dSMike Smith 74015e32d5dSMike Smith static struct resource * 74115e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 74215e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 74315e32d5dSMike Smith { 74415e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 74515e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 74615e32d5dSMike Smith 747be2b1797SNate Lawson return (resource_list_alloc(rl, bus, child, type, rid, start, end, count, 748be2b1797SNate Lawson flags)); 74915e32d5dSMike Smith } 75015e32d5dSMike Smith 75115e32d5dSMike Smith static int 75215e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 75315e32d5dSMike Smith { 75415e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 75515e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 75615e32d5dSMike Smith 75715e32d5dSMike Smith return(resource_list_release(rl, bus, child, type, rid, r)); 75815e32d5dSMike Smith } 75915e32d5dSMike Smith 76015e32d5dSMike Smith /* 761bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 762bc0f2195SMike Smith */ 763bc0f2195SMike Smith #define PNP_EISAID(s) \ 764bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 765bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 766bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 767bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 768bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 769bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 770bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 771bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 772bc0f2195SMike Smith 77332d18aa5SMike Smith static u_int32_t 77432d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 775bc0f2195SMike Smith { 776bc0f2195SMike Smith ACPI_HANDLE h; 777bc0f2195SMike Smith ACPI_DEVICE_INFO devinfo; 7786fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 779bc0f2195SMike Smith ACPI_STATUS error; 780bc0f2195SMike Smith u_int32_t pnpid; 781fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 78232d18aa5SMike Smith 783b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 78432d18aa5SMike Smith 78532d18aa5SMike Smith pnpid = 0; 78632d18aa5SMike Smith ACPI_LOCK; 78732d18aa5SMike Smith 7886fca9360SNate Lawson /* Fetch and validate the HID. */ 78932d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 79032d18aa5SMike Smith goto out; 7916fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 7926fca9360SNate Lawson if (ACPI_FAILURE(error)) 79332d18aa5SMike Smith goto out; 7946fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_HID) == 0) 79532d18aa5SMike Smith goto out; 79632d18aa5SMike Smith 7976fca9360SNate Lawson pnpid = PNP_EISAID(devinfo.HardwareId.Value); 798be2b1797SNate Lawson 79932d18aa5SMike Smith out: 80032d18aa5SMike Smith ACPI_UNLOCK; 80132d18aa5SMike Smith return_VALUE (pnpid); 80232d18aa5SMike Smith } 80332d18aa5SMike Smith 804b2566207STakanori Watanabe static u_int32_t 805b2566207STakanori Watanabe acpi_isa_get_compatid(device_t dev) 806b2566207STakanori Watanabe { 807b2566207STakanori Watanabe ACPI_HANDLE h; 808b2566207STakanori Watanabe ACPI_STATUS error; 809b2566207STakanori Watanabe u_int32_t pnpid; 810b2566207STakanori Watanabe ACPI_LOCK_DECL; 811b2566207STakanori Watanabe 812b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 813b2566207STakanori Watanabe 814b2566207STakanori Watanabe pnpid = 0; 815b2566207STakanori Watanabe ACPI_LOCK; 816b2566207STakanori Watanabe 817be2b1797SNate Lawson /* Fetch and validate the HID */ 818b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 819b2566207STakanori Watanabe goto out; 820b2566207STakanori Watanabe if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &pnpid))) 821b2566207STakanori Watanabe goto out; 822b2566207STakanori Watanabe 823b2566207STakanori Watanabe out: 824b2566207STakanori Watanabe ACPI_UNLOCK; 825b2566207STakanori Watanabe return_VALUE (pnpid); 826b2566207STakanori Watanabe } 827b2566207STakanori Watanabe 828b2566207STakanori Watanabe 82932d18aa5SMike Smith static int 83032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 83132d18aa5SMike Smith { 832bc0f2195SMike Smith int result; 833b2566207STakanori Watanabe u_int32_t lid, cid; 834bc0f2195SMike Smith 835b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 836bc0f2195SMike Smith 837bc0f2195SMike Smith /* 838bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 839bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 840bc0f2195SMike Smith * that to happen, so don't ever return it. 841bc0f2195SMike Smith */ 842bc0f2195SMike Smith result = ENXIO; 843bc0f2195SMike Smith 844be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 845b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 846b2566207STakanori Watanabe cid = acpi_isa_get_compatid(child); 847bc0f2195SMike Smith while (ids && ids->ip_id) { 848b2566207STakanori Watanabe if (lid == ids->ip_id || cid == ids->ip_id) { 849bc0f2195SMike Smith result = 0; 850bc0f2195SMike Smith goto out; 851bc0f2195SMike Smith } 852bc0f2195SMike Smith ids++; 853bc0f2195SMike Smith } 854be2b1797SNate Lawson 855bc0f2195SMike Smith out: 856bc0f2195SMike Smith return_VALUE(result); 857bc0f2195SMike Smith } 858bc0f2195SMike Smith 859bc0f2195SMike Smith /* 86015e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 86115e32d5dSMike Smith * 862be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 863be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 86415e32d5dSMike Smith */ 86515e32d5dSMike Smith static void 86615e32d5dSMike Smith acpi_probe_children(device_t bus) 86715e32d5dSMike Smith { 86815e32d5dSMike Smith ACPI_HANDLE parent; 869be2b1797SNate Lawson ACPI_STATUS status; 8707d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 87115e32d5dSMike Smith int i; 87215e32d5dSMike Smith 873b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 874cb9b0d80SMike Smith ACPI_ASSERTLOCK; 8750ae55423SMike Smith 876be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 8774c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 87815e32d5dSMike Smith bus_generic_probe(bus); 87915e32d5dSMike Smith 88015e32d5dSMike Smith /* 88115e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 88215e32d5dSMike Smith * we find. 88315e32d5dSMike Smith * 88415e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 885be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 886be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 887be2b1797SNate Lawson * devices as they appear, which might be smarter.) 88815e32d5dSMike Smith */ 8894c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 890be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 891be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 892be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 893be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 894be2b1797SNate Lawson bus, NULL); 895be2b1797SNate Lawson } 896be2b1797SNate Lawson } 89715e32d5dSMike Smith 89815e32d5dSMike Smith /* 89915e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 90015e32d5dSMike Smith */ 9014c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 90215e32d5dSMike Smith bus_generic_attach(bus); 90315e32d5dSMike Smith 90415e32d5dSMike Smith /* 90515e32d5dSMike Smith * Some of these children may have attached others as part of their attach 90615e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 90715e32d5dSMike Smith */ 9084c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 90915e32d5dSMike Smith bus_generic_attach(bus); 9100ae55423SMike Smith 911bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 9120ae55423SMike Smith return_VOID; 91315e32d5dSMike Smith } 91415e32d5dSMike Smith 91515e32d5dSMike Smith /* 91615e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 91715e32d5dSMike Smith * it. 91815e32d5dSMike Smith */ 91915e32d5dSMike Smith static ACPI_STATUS 92015e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 92115e32d5dSMike Smith { 92215e32d5dSMike Smith ACPI_OBJECT_TYPE type; 92315e32d5dSMike Smith device_t child, bus = (device_t)context; 92415e32d5dSMike Smith 925b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 9260ae55423SMike Smith 927be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 9280ae55423SMike Smith if (acpi_avoid(handle)) 9290ae55423SMike Smith return_ACPI_STATUS (AE_OK); 9300ae55423SMike Smith 931b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 93215e32d5dSMike Smith switch(type) { 93315e32d5dSMike Smith case ACPI_TYPE_DEVICE: 93415e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 93515e32d5dSMike Smith case ACPI_TYPE_THERMAL: 93615e32d5dSMike Smith case ACPI_TYPE_POWER: 9370ae55423SMike Smith if (acpi_disabled("children")) 9380ae55423SMike Smith break; 939be2b1797SNate Lawson 94015e32d5dSMike Smith /* 94115e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 94215e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 94315e32d5dSMike Smith */ 944be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 945be2b1797SNate Lawson acpi_name(handle))); 94615e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 947bc0f2195SMike Smith if (child == NULL) 948bc0f2195SMike Smith break; 94915e32d5dSMike Smith acpi_set_handle(child, handle); 950bc0f2195SMike Smith 951bc0f2195SMike Smith /* 952b519f9d6SMike Smith * Check that the device is present. If it's not present, 953b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 954b519f9d6SMike Smith * the handle, but we don't probe it). 955b519f9d6SMike Smith */ 956be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 957b519f9d6SMike Smith device_disable(child); 958b519f9d6SMike Smith break; 959b519f9d6SMike Smith } 960b519f9d6SMike Smith 961b519f9d6SMike Smith /* 962bc0f2195SMike Smith * Get the device's resource settings and attach them. 963bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 964bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 965bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 966bc0f2195SMike Smith * device not to have any resources. 967bc0f2195SMike Smith */ 968bc0f2195SMike Smith acpi_parse_resources(child, handle, &acpi_res_parse_set); 969bc0f2195SMike Smith 970be2b1797SNate Lawson /* If we're debugging, probe/attach now rather than later */ 971b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 9720ae55423SMike Smith break; 97315e32d5dSMike Smith } 97415e32d5dSMike Smith } 975be2b1797SNate Lawson 9760ae55423SMike Smith return_ACPI_STATUS (AE_OK); 97715e32d5dSMike Smith } 97815e32d5dSMike Smith 97915e32d5dSMike Smith static void 98015e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 98115e32d5dSMike Smith { 982c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 983c6a78e98STakanori Watanabe 984cb9b0d80SMike Smith ACPI_ASSERTLOCK; 985cb9b0d80SMike Smith 98615e32d5dSMike Smith /* 9870ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 98815e32d5dSMike Smith * will be turned on again on some laptops. 98915e32d5dSMike Smith * 99015e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 99115e32d5dSMike Smith * before powering down, since we may still need ACPI during the 99215e32d5dSMike Smith * shutdown process. 99315e32d5dSMike Smith */ 994c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 995c6a78e98STakanori Watanabe acpi_Disable(sc); 99615e32d5dSMike Smith } 99715e32d5dSMike Smith 99815e32d5dSMike Smith static void 99915e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 100015e32d5dSMike Smith { 100115e32d5dSMike Smith ACPI_STATUS status; 100215e32d5dSMike Smith 1003cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1004cb9b0d80SMike Smith 1005be2b1797SNate Lawson if ((howto & RB_POWEROFF) != 0) { 1006e1a90ae1SNate Lawson printf("Powering system off using ACPI\n"); 1007be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(acpi_off_state); 1008be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1009b9c780d6SMitsuru IWASAKI printf("AcpiEnterSleepStatePrep failed - %s\n", 1010b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1011b9c780d6SMitsuru IWASAKI return; 1012b9c780d6SMitsuru IWASAKI } 1013be2b1797SNate Lawson status = AcpiEnterSleepState(acpi_off_state); 1014be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1015bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 101615e32d5dSMike Smith } else { 101715e32d5dSMike Smith DELAY(1000000); 101815e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 101915e32d5dSMike Smith } 1020494ae560SMitsuru IWASAKI } else { 1021e1a90ae1SNate Lawson printf("Shutting down ACPI\n"); 1022494ae560SMitsuru IWASAKI AcpiTerminate(); 102315e32d5dSMike Smith } 102415e32d5dSMike Smith } 102515e32d5dSMike Smith 102613d4f7baSMitsuru IWASAKI static void 102713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 102813d4f7baSMitsuru IWASAKI { 102913d4f7baSMitsuru IWASAKI static int first_time = 1; 103013d4f7baSMitsuru IWASAKI #define MSGFORMAT "%s button is handled as a fixed feature programming model.\n" 103113d4f7baSMitsuru IWASAKI 1032cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1033cb9b0d80SMike Smith 103413d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1035be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 103659ddeb18SNate Lawson AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0); 103759ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 103813d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1039be2b1797SNate Lawson acpi_eventhandler_power_button_for_sleep, 1040be2b1797SNate Lawson sc); 1041be2b1797SNate Lawson if (first_time) 104213d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "power"); 104313d4f7baSMitsuru IWASAKI } 1044be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 104559ddeb18SNate Lawson AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, 0); 104659ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 104713d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 1048be2b1797SNate Lawson acpi_eventhandler_sleep_button_for_sleep, 1049be2b1797SNate Lawson sc); 1050be2b1797SNate Lawson if (first_time) 105113d4f7baSMitsuru IWASAKI device_printf(sc->acpi_dev, MSGFORMAT, "sleep"); 105213d4f7baSMitsuru IWASAKI } 105313d4f7baSMitsuru IWASAKI 105413d4f7baSMitsuru IWASAKI first_time = 0; 105513d4f7baSMitsuru IWASAKI } 105613d4f7baSMitsuru IWASAKI 105715e32d5dSMike Smith /* 1058c5ba0be4SMike Smith * Returns true if the device is actually present and should 1059c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1060c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1061c5ba0be4SMike Smith */ 1062c5ba0be4SMike Smith BOOLEAN 1063c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1064c5ba0be4SMike Smith { 1065c5ba0be4SMike Smith ACPI_HANDLE h; 1066c5ba0be4SMike Smith ACPI_DEVICE_INFO devinfo; 10676fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 1068c5ba0be4SMike Smith ACPI_STATUS error; 1069c5ba0be4SMike Smith 1070cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1071cb9b0d80SMike Smith 1072c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1073c5ba0be4SMike Smith return (FALSE); 10746fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10756fca9360SNate Lawson if (ACPI_FAILURE(error)) 1076c5ba0be4SMike Smith return (FALSE); 1077be2b1797SNate Lawson 1078be2b1797SNate Lawson /* If no _STA method, must be present */ 10796fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_STA) == 0) 108043896e91SMike Smith return (TRUE); 1081be2b1797SNate Lawson 1082be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 108343896e91SMike Smith if ((devinfo.CurrentStatus & 0x9) == 0x9) 1084c5ba0be4SMike Smith return (TRUE); 1085be2b1797SNate Lawson 1086c5ba0be4SMike Smith return (FALSE); 1087c5ba0be4SMike Smith } 1088c5ba0be4SMike Smith 1089c5ba0be4SMike Smith /* 1090b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1091b53f2771SMike Smith */ 1092b53f2771SMike Smith BOOLEAN 1093b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1094b53f2771SMike Smith { 1095b53f2771SMike Smith ACPI_HANDLE h; 1096b53f2771SMike Smith ACPI_DEVICE_INFO devinfo; 10976fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 1098b53f2771SMike Smith ACPI_STATUS error; 1099b53f2771SMike Smith 1100b53f2771SMike Smith ACPI_ASSERTLOCK; 1101b53f2771SMike Smith 1102b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1103b53f2771SMike Smith return (FALSE); 11046fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 11056fca9360SNate Lawson if (ACPI_FAILURE(error)) 1106b53f2771SMike Smith return (FALSE); 1107be2b1797SNate Lawson 1108be2b1797SNate Lawson /* If no _STA method, must be present */ 11096fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_STA) == 0) 1110b53f2771SMike Smith return (TRUE); 1111be2b1797SNate Lawson 1112be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 1113b53f2771SMike Smith if ((devinfo.CurrentStatus & 0x19) == 0x19) 1114b53f2771SMike Smith return (TRUE); 1115be2b1797SNate Lawson 1116b53f2771SMike Smith return (FALSE); 1117b53f2771SMike Smith } 1118b53f2771SMike Smith 1119b53f2771SMike Smith /* 112015e32d5dSMike Smith * Match a HID string against a device 112115e32d5dSMike Smith */ 112215e32d5dSMike Smith BOOLEAN 112315e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 112415e32d5dSMike Smith { 112515e32d5dSMike Smith ACPI_HANDLE h; 112615e32d5dSMike Smith ACPI_DEVICE_INFO devinfo; 11276fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 112815e32d5dSMike Smith ACPI_STATUS error; 1129ed136da6SDoug Rabson int cid; 113015e32d5dSMike Smith 1131cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1132cb9b0d80SMike Smith 11334d332989SMike Smith if (hid == NULL) 113415e32d5dSMike Smith return (FALSE); 113515e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 113615e32d5dSMike Smith return (FALSE); 11376fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 11386fca9360SNate Lawson if (ACPI_FAILURE(error)) 113915e32d5dSMike Smith return (FALSE); 11406fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_HID) != 0 && 11416fca9360SNate Lawson strcmp(hid, devinfo.HardwareId.Value) == 0) 114215e32d5dSMike Smith return (TRUE); 1143be2b1797SNate Lawson 1144b53f2771SMike Smith if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1145ed136da6SDoug Rabson return (FALSE); 1146ed136da6SDoug Rabson if (cid == PNP_EISAID(hid)) 1147ed136da6SDoug Rabson return (TRUE); 1148be2b1797SNate Lawson 114915e32d5dSMike Smith return (FALSE); 115015e32d5dSMike Smith } 115115e32d5dSMike Smith 115215e32d5dSMike Smith /* 1153c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1154c5ba0be4SMike Smith * or one if its parents. 1155c5ba0be4SMike Smith */ 1156c5ba0be4SMike Smith ACPI_STATUS 1157c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1158c5ba0be4SMike Smith { 1159c5ba0be4SMike Smith ACPI_HANDLE r; 1160c5ba0be4SMike Smith ACPI_STATUS status; 1161c5ba0be4SMike Smith 1162cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1163cb9b0d80SMike Smith 1164be2b1797SNate Lawson /* Walk back up the tree to the root */ 1165c5ba0be4SMike Smith for (;;) { 1166be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1167be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1168c5ba0be4SMike Smith *result = r; 1169c5ba0be4SMike Smith return (AE_OK); 1170c5ba0be4SMike Smith } 1171c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1172c5ba0be4SMike Smith return (AE_OK); 1173b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1174c5ba0be4SMike Smith return (AE_NOT_FOUND); 1175c5ba0be4SMike Smith parent = r; 1176c5ba0be4SMike Smith } 1177c5ba0be4SMike Smith } 1178c5ba0be4SMike Smith 1179c5ba0be4SMike Smith /* 1180c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1181c5ba0be4SMike Smith */ 1182c5ba0be4SMike Smith ACPI_BUFFER * 1183c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1184c5ba0be4SMike Smith { 1185c5ba0be4SMike Smith ACPI_BUFFER *buf; 1186c5ba0be4SMike Smith 1187c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1188c5ba0be4SMike Smith return (NULL); 1189c5ba0be4SMike Smith buf->Length = size; 1190c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1191c5ba0be4SMike Smith return (buf); 1192c5ba0be4SMike Smith } 1193c5ba0be4SMike Smith 1194c5ba0be4SMike Smith /* 1195c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1196c5ba0be4SMike Smith */ 1197c5ba0be4SMike Smith ACPI_STATUS 1198c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1199c5ba0be4SMike Smith { 1200be2b1797SNate Lawson ACPI_STATUS status; 1201c5ba0be4SMike Smith ACPI_BUFFER buf; 1202c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1203c5ba0be4SMike Smith 1204cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1205cb9b0d80SMike Smith 1206c5ba0be4SMike Smith if (handle == NULL) 1207c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 12080c7da7acSJohn Baldwin 12090c7da7acSJohn Baldwin /* 12100c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 12110c7da7acSJohn Baldwin * a method that will return an Integer. 12120c7da7acSJohn Baldwin */ 1213c5ba0be4SMike Smith buf.Pointer = ¶m; 1214c5ba0be4SMike Smith buf.Length = sizeof(param); 1215be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1216be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1217be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1218c5ba0be4SMike Smith *number = param.Integer.Value; 1219be2b1797SNate Lawson else 1220be2b1797SNate Lawson status = AE_TYPE; 1221c5ba0be4SMike Smith } 12220c7da7acSJohn Baldwin 12230c7da7acSJohn Baldwin /* 12240c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 12250c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 12260c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 12270c7da7acSJohn Baldwin * convert it into an Integer as best we can. 12280c7da7acSJohn Baldwin * 12290c7da7acSJohn Baldwin * This is a hack. 12300c7da7acSJohn Baldwin */ 1231be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1232b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1233be2b1797SNate Lawson status = AE_NO_MEMORY; 12340c7da7acSJohn Baldwin } else { 1235be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1236be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1237be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 12380c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 12390c7da7acSJohn Baldwin } 1240f97739daSNate Lawson } 1241be2b1797SNate Lawson return (status); 1242c5ba0be4SMike Smith } 1243c5ba0be4SMike Smith 1244c573e654SMitsuru IWASAKI ACPI_STATUS 1245c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1246c573e654SMitsuru IWASAKI { 1247c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1248c573e654SMitsuru IWASAKI int i; 1249c573e654SMitsuru IWASAKI 1250c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1251c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1252c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1253c573e654SMitsuru IWASAKI return (AE_OK); 1254c573e654SMitsuru IWASAKI } 1255c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1256c573e654SMitsuru IWASAKI return (AE_TYPE); 1257c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1258c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1259be2b1797SNate Lawson 1260c573e654SMitsuru IWASAKI *number = 0; 1261c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1262c573e654SMitsuru IWASAKI *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1263c573e654SMitsuru IWASAKI return (AE_OK); 1264c573e654SMitsuru IWASAKI } 1265c573e654SMitsuru IWASAKI 1266c5ba0be4SMike Smith /* 1267c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1268c5ba0be4SMike Smith * function for each element. 1269c5ba0be4SMike Smith * 1270c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1271c5ba0be4SMike Smith */ 1272c5ba0be4SMike Smith ACPI_STATUS 1273be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1274be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1275c5ba0be4SMike Smith { 1276c5ba0be4SMike Smith ACPI_OBJECT *comp; 1277c5ba0be4SMike Smith int i; 1278c5ba0be4SMike Smith 1279be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1280c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1281c5ba0be4SMike Smith 1282be2b1797SNate Lawson /* Iterate over components */ 1283be2b1797SNate Lawson i = 0; 1284be2b1797SNate Lawson comp = pkg->Package.Elements; 1285be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1286c5ba0be4SMike Smith func(comp, arg); 1287c5ba0be4SMike Smith 1288c5ba0be4SMike Smith return (AE_OK); 1289c5ba0be4SMike Smith } 1290c5ba0be4SMike Smith 12916f69255bSMike Smith /* 12926f69255bSMike Smith * Find the (index)th resource object in a set. 12936f69255bSMike Smith */ 12946f69255bSMike Smith ACPI_STATUS 12956d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 12966f69255bSMike Smith { 12976d63101aSMike Smith ACPI_RESOURCE *rp; 12986f69255bSMike Smith int i; 12996f69255bSMike Smith 13006d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13016f69255bSMike Smith i = index; 13026d63101aSMike Smith while (i-- > 0) { 1303be2b1797SNate Lawson /* Range check */ 13046d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 13056f69255bSMike Smith return (AE_BAD_PARAMETER); 1306be2b1797SNate Lawson 1307be2b1797SNate Lawson /* Check for terminator */ 1308be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 13096d63101aSMike Smith return (AE_NOT_FOUND); 13106d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13116f69255bSMike Smith } 13126f69255bSMike Smith if (resp != NULL) 13136d63101aSMike Smith *resp = rp; 1314be2b1797SNate Lawson 13156d63101aSMike Smith return (AE_OK); 13166d63101aSMike Smith } 13176d63101aSMike Smith 13186d63101aSMike Smith /* 13196d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 13206d63101aSMike Smith * 13216d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 13226d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 13236d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 13246d63101aSMike Smith * resources. 13256d63101aSMike Smith */ 13266d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 13276d63101aSMike Smith 13286d63101aSMike Smith ACPI_STATUS 13296d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 13306d63101aSMike Smith { 13316d63101aSMike Smith ACPI_RESOURCE *rp; 13326d63101aSMike Smith void *newp; 13336d63101aSMike Smith 1334be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 13356d63101aSMike Smith if (buf->Pointer == NULL) { 13366d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 13376d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 13386d63101aSMike Smith return (AE_NO_MEMORY); 13396d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13406d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 13416d63101aSMike Smith rp->Length = 0; 13426d63101aSMike Smith } 13436d63101aSMike Smith if (res == NULL) 13446d63101aSMike Smith return (AE_OK); 13456d63101aSMike Smith 13466d63101aSMike Smith /* 13476d63101aSMike Smith * Scan the current buffer looking for the terminator. 13486d63101aSMike Smith * This will either find the terminator or hit the end 13496d63101aSMike Smith * of the buffer and return an error. 13506d63101aSMike Smith */ 13516d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13526d63101aSMike Smith for (;;) { 1353be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 13546d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 13556d63101aSMike Smith return (AE_BAD_PARAMETER); 1356be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 13576d63101aSMike Smith break; 13586d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13596d63101aSMike Smith } 13606d63101aSMike Smith 13616d63101aSMike Smith /* 13626d63101aSMike Smith * Check the size of the buffer and expand if required. 13636d63101aSMike Smith * 13646d63101aSMike Smith * Required size is: 13656d63101aSMike Smith * size of existing resources before terminator + 13666d63101aSMike Smith * size of new resource and header + 13676d63101aSMike Smith * size of terminator. 13686d63101aSMike Smith * 13696d63101aSMike Smith * Note that this loop should really only run once, unless 13706d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 13716d63101aSMike Smith */ 13726d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 13736d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 13746d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 13756d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 13766d63101aSMike Smith return (AE_NO_MEMORY); 13776d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1378a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1379a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 13806d63101aSMike Smith AcpiOsFree(buf->Pointer); 13816d63101aSMike Smith buf->Pointer = newp; 13826d63101aSMike Smith buf->Length += buf->Length; 13836d63101aSMike Smith } 13846d63101aSMike Smith 1385be2b1797SNate Lawson /* Insert the new resource. */ 13866d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 13876d63101aSMike Smith 1388be2b1797SNate Lawson /* And add the terminator. */ 13896d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13906d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 13916d63101aSMike Smith rp->Length = 0; 13926d63101aSMike Smith 13936f69255bSMike Smith return (AE_OK); 13946f69255bSMike Smith } 1395c5ba0be4SMike Smith 1396da14ac9fSJohn Baldwin /* 1397da14ac9fSJohn Baldwin * Set interrupt model. 1398da14ac9fSJohn Baldwin */ 1399da14ac9fSJohn Baldwin ACPI_STATUS 1400da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1401da14ac9fSJohn Baldwin { 1402da14ac9fSJohn Baldwin ACPI_OBJECT_LIST ArgList; 1403da14ac9fSJohn Baldwin ACPI_OBJECT Arg; 1404da14ac9fSJohn Baldwin 1405da14ac9fSJohn Baldwin Arg.Type = ACPI_TYPE_INTEGER; 1406da14ac9fSJohn Baldwin Arg.Integer.Value = model; 1407da14ac9fSJohn Baldwin ArgList.Count = 1; 1408da14ac9fSJohn Baldwin ArgList.Pointer = &Arg; 1409da14ac9fSJohn Baldwin return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1410da14ac9fSJohn Baldwin } 1411da14ac9fSJohn Baldwin 1412ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1413ece50487SMitsuru IWASAKI 1414ece50487SMitsuru IWASAKI static void 1415ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1416ece50487SMitsuru IWASAKI { 1417ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1418ece50487SMitsuru IWASAKI } 1419c30382dfSMitsuru IWASAKI 142015e32d5dSMike Smith /* 142115e32d5dSMike Smith * Set the system sleep state 142215e32d5dSMike Smith * 1423be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 142415e32d5dSMike Smith */ 142515e32d5dSMike Smith ACPI_STATUS 142615e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 142715e32d5dSMike Smith { 142815e32d5dSMike Smith ACPI_STATUS status = AE_OK; 142956d8cb57SMitsuru IWASAKI UINT8 TypeA; 143056d8cb57SMitsuru IWASAKI UINT8 TypeB; 143115e32d5dSMike Smith 1432b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1433cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14340ae55423SMike Smith 143598175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 14366397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 143798175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 14386397624dSMitsuru IWASAKI 143998175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1440ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1441ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1442ece50487SMitsuru IWASAKI 144315e32d5dSMike Smith switch (state) { 144415e32d5dSMike Smith case ACPI_STATE_S1: 14456161544cSTakanori Watanabe case ACPI_STATE_S2: 14466161544cSTakanori Watanabe case ACPI_STATE_S3: 14476161544cSTakanori Watanabe case ACPI_STATE_S4: 1448be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 144998175614SNate Lawson if (status == AE_NOT_FOUND) { 145098175614SNate Lawson device_printf(sc->acpi_dev, 145198175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 145298175614SNate Lawson break; 145398175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1454be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1455be2b1797SNate Lawson AcpiFormatException(status)); 145656d8cb57SMitsuru IWASAKI break; 145756d8cb57SMitsuru IWASAKI } 145856d8cb57SMitsuru IWASAKI 1459a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1460a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1461a1fccb47SMitsuru IWASAKI 1462be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 146315e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 146415e32d5dSMike Smith /* 146515e32d5dSMike Smith * Re-wake the system. 146615e32d5dSMike Smith * 146715e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 146815e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 146915e32d5dSMike Smith * current bus interface does not provide for this. 147015e32d5dSMike Smith */ 147115e32d5dSMike Smith DEVICE_RESUME(root_bus); 14720ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 147315e32d5dSMike Smith } 1474b9c780d6SMitsuru IWASAKI 1475be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1476be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1477b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1478b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1479b9c780d6SMitsuru IWASAKI break; 1480b9c780d6SMitsuru IWASAKI } 1481b9c780d6SMitsuru IWASAKI 1482be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1483ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1484ff01efb5SMitsuru IWASAKI 14856161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 14866161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 14876161544cSTakanori Watanabe 1488be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 148959ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 149059ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 149159ddeb18SNate Lawson 14926161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1493a1fccb47SMitsuru IWASAKI } 14946161544cSTakanori Watanabe 14956161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1496be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1497964679ceSMitsuru IWASAKI AcpiEnable(); 14986161544cSTakanori Watanabe } else { 1499be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1500be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1501be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1502be2b1797SNate Lawson AcpiFormatException(status)); 1503c30382dfSMitsuru IWASAKI break; 150415e32d5dSMike Smith } 15056161544cSTakanori Watanabe } 1506ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 150715e32d5dSMike Smith DEVICE_RESUME(root_bus); 150815e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 150913d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 151015e32d5dSMike Smith break; 151115e32d5dSMike Smith case ACPI_STATE_S5: 151215e32d5dSMike Smith /* 151315e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 151415e32d5dSMike Smith * shutdown handlers. 151515e32d5dSMike Smith */ 151615e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 151715e32d5dSMike Smith break; 151898175614SNate Lawson case ACPI_STATE_S0: 151915e32d5dSMike Smith default: 152015e32d5dSMike Smith status = AE_BAD_PARAMETER; 152115e32d5dSMike Smith break; 152215e32d5dSMike Smith } 1523ece50487SMitsuru IWASAKI 152498175614SNate Lawson /* Disable a second sleep request for a short period */ 1525ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1526ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1527ece50487SMitsuru IWASAKI 15280ae55423SMike Smith return_ACPI_STATUS (status); 152915e32d5dSMike Smith } 153015e32d5dSMike Smith 153115e32d5dSMike Smith /* 153215e32d5dSMike Smith * Enable/Disable ACPI 153315e32d5dSMike Smith */ 153415e32d5dSMike Smith ACPI_STATUS 153515e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 153615e32d5dSMike Smith { 153715e32d5dSMike Smith ACPI_STATUS status; 153815e32d5dSMike Smith u_int32_t flags; 153915e32d5dSMike Smith 1540b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1541cb9b0d80SMike Smith ACPI_ASSERTLOCK; 15420ae55423SMike Smith 154315e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 154415e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1545be2b1797SNate Lawson if (!sc->acpi_enabled) 154615e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 1547be2b1797SNate Lawson else 154815e32d5dSMike Smith status = AE_OK; 1549be2b1797SNate Lawson 155015e32d5dSMike Smith if (status == AE_OK) 155115e32d5dSMike Smith sc->acpi_enabled = 1; 1552be2b1797SNate Lawson 15530ae55423SMike Smith return_ACPI_STATUS (status); 155415e32d5dSMike Smith } 155515e32d5dSMike Smith 155615e32d5dSMike Smith ACPI_STATUS 155715e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 155815e32d5dSMike Smith { 155915e32d5dSMike Smith ACPI_STATUS status; 156015e32d5dSMike Smith 1561b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1562cb9b0d80SMike Smith ACPI_ASSERTLOCK; 15630ae55423SMike Smith 1564be2b1797SNate Lawson if (sc->acpi_enabled) 156515e32d5dSMike Smith status = AcpiDisable(); 1566be2b1797SNate Lawson else 156715e32d5dSMike Smith status = AE_OK; 1568be2b1797SNate Lawson 156915e32d5dSMike Smith if (status == AE_OK) 157015e32d5dSMike Smith sc->acpi_enabled = 0; 1571be2b1797SNate Lawson 15720ae55423SMike Smith return_ACPI_STATUS (status); 157315e32d5dSMike Smith } 157415e32d5dSMike Smith 157515e32d5dSMike Smith /* 157615e32d5dSMike Smith * ACPI Event Handlers 157715e32d5dSMike Smith */ 157815e32d5dSMike Smith 157915e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 158015e32d5dSMike Smith 158115e32d5dSMike Smith static void 158215e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 158315e32d5dSMike Smith { 1584fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1585b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 158615e32d5dSMike Smith 1587cb9b0d80SMike Smith ACPI_LOCK; 1588a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 158915e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 1590cb9b0d80SMike Smith ACPI_UNLOCK; 15910ae55423SMike Smith return_VOID; 159215e32d5dSMike Smith } 159315e32d5dSMike Smith 159415e32d5dSMike Smith static void 159515e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 159615e32d5dSMike Smith { 1597fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1598b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 15990ae55423SMike Smith 160015e32d5dSMike Smith /* Well, what to do? :-) */ 16010ae55423SMike Smith 1602cb9b0d80SMike Smith ACPI_LOCK; 1603cb9b0d80SMike Smith ACPI_UNLOCK; 1604cb9b0d80SMike Smith 16050ae55423SMike Smith return_VOID; 160615e32d5dSMike Smith } 160715e32d5dSMike Smith 160815e32d5dSMike Smith /* 160915e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 161015e32d5dSMike Smith */ 161115e32d5dSMike Smith UINT32 161215e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context) 161315e32d5dSMike Smith { 161415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 161515e32d5dSMike Smith 1616b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16170ae55423SMike Smith 161815e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 16190ae55423SMike Smith 1620b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 162115e32d5dSMike Smith } 162215e32d5dSMike Smith 162315e32d5dSMike Smith UINT32 162415e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context) 162515e32d5dSMike Smith { 162615e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 162715e32d5dSMike Smith 1628b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16290ae55423SMike Smith 163015e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 16310ae55423SMike Smith 1632b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 163315e32d5dSMike Smith } 163415e32d5dSMike Smith 163515e32d5dSMike Smith UINT32 163615e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context) 163715e32d5dSMike Smith { 163815e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 163915e32d5dSMike Smith 1640b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16410ae55423SMike Smith 164215e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 16430ae55423SMike Smith 1644b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 164515e32d5dSMike Smith } 164615e32d5dSMike Smith 164715e32d5dSMike Smith UINT32 164815e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context) 164915e32d5dSMike Smith { 165015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 165115e32d5dSMike Smith 1652b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16530ae55423SMike Smith 165415e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 16550ae55423SMike Smith 1656b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 165715e32d5dSMike Smith } 165815e32d5dSMike Smith 165915e32d5dSMike Smith /* 166015e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 166115e32d5dSMike Smith */ 166215e32d5dSMike Smith struct acpi_staticbuf { 166315e32d5dSMike Smith ACPI_BUFFER buffer; 166415e32d5dSMike Smith char data[512]; 166515e32d5dSMike Smith }; 166615e32d5dSMike Smith 166715e32d5dSMike Smith char * 166815e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 166915e32d5dSMike Smith { 167015e32d5dSMike Smith static struct acpi_staticbuf buf; 167115e32d5dSMike Smith 1672cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1673cb9b0d80SMike Smith 167415e32d5dSMike Smith buf.buffer.Length = 512; 167515e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 167615e32d5dSMike Smith 1677b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 167815e32d5dSMike Smith return (buf.buffer.Pointer); 1679be2b1797SNate Lawson 168015e32d5dSMike Smith return ("(unknown path)"); 168115e32d5dSMike Smith } 168215e32d5dSMike Smith 168315e32d5dSMike Smith /* 168415e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 168515e32d5dSMike Smith * parts of the namespace. 168615e32d5dSMike Smith */ 168715e32d5dSMike Smith int 168815e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 168915e32d5dSMike Smith { 16903c600cfbSJohn Baldwin char *cp, *env, *np; 169115e32d5dSMike Smith int len; 169215e32d5dSMike Smith 169315e32d5dSMike Smith np = acpi_name(handle); 169415e32d5dSMike Smith if (*np == '\\') 169515e32d5dSMike Smith np++; 16963c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 169715e32d5dSMike Smith return (0); 169815e32d5dSMike Smith 1699be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 17003c600cfbSJohn Baldwin cp = env; 170115e32d5dSMike Smith for (;;) { 170215e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 170315e32d5dSMike Smith cp++; 170415e32d5dSMike Smith if (*cp == 0) 170515e32d5dSMike Smith break; 170615e32d5dSMike Smith len = 0; 170715e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 170815e32d5dSMike Smith len++; 1709d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 17103c600cfbSJohn Baldwin freeenv(env); 17110ae55423SMike Smith return(1); 1712d786139cSMaxime Henrion } 17130ae55423SMike Smith cp += len; 17140ae55423SMike Smith } 17153c600cfbSJohn Baldwin freeenv(env); 1716be2b1797SNate Lawson 17170ae55423SMike Smith return (0); 17180ae55423SMike Smith } 17190ae55423SMike Smith 17200ae55423SMike Smith /* 17210ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 17220ae55423SMike Smith */ 17230ae55423SMike Smith int 17240ae55423SMike Smith acpi_disabled(char *subsys) 17250ae55423SMike Smith { 172678689b15SMaxime Henrion char *cp, *env; 17270ae55423SMike Smith int len; 17280ae55423SMike Smith 172978689b15SMaxime Henrion if ((env = getenv("debug.acpi.disable")) == NULL) 17300ae55423SMike Smith return (0); 173178689b15SMaxime Henrion if (!strcmp(env, "all")) { 173278689b15SMaxime Henrion freeenv(env); 17330ae55423SMike Smith return (1); 1734d786139cSMaxime Henrion } 17350ae55423SMike Smith 17360ae55423SMike Smith /* scan the disable list checking for a match */ 173778689b15SMaxime Henrion cp = env; 17380ae55423SMike Smith for (;;) { 17390ae55423SMike Smith while ((*cp != 0) && isspace(*cp)) 17400ae55423SMike Smith cp++; 17410ae55423SMike Smith if (*cp == 0) 17420ae55423SMike Smith break; 17430ae55423SMike Smith len = 0; 17440ae55423SMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 17450ae55423SMike Smith len++; 1746d786139cSMaxime Henrion if (!strncmp(cp, subsys, len)) { 174778689b15SMaxime Henrion freeenv(env); 174815e32d5dSMike Smith return (1); 1749d786139cSMaxime Henrion } 175015e32d5dSMike Smith cp += len; 175115e32d5dSMike Smith } 175278689b15SMaxime Henrion freeenv(env); 1753be2b1797SNate Lawson 175415e32d5dSMike Smith return (0); 175515e32d5dSMike Smith } 175615e32d5dSMike Smith 175715e32d5dSMike Smith /* 1758a1fccb47SMitsuru IWASAKI * Device wake capability enable/disable. 1759a1fccb47SMitsuru IWASAKI */ 1760a1fccb47SMitsuru IWASAKI void 1761a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1762a1fccb47SMitsuru IWASAKI { 1763a1fccb47SMitsuru IWASAKI ACPI_OBJECT_LIST ArgList; 1764a1fccb47SMitsuru IWASAKI ACPI_OBJECT Arg; 1765a1fccb47SMitsuru IWASAKI 1766a1fccb47SMitsuru IWASAKI /* 1767a1fccb47SMitsuru IWASAKI * TBD: All Power Resources referenced by elements 2 through N 1768a1fccb47SMitsuru IWASAKI * of the _PRW object are put into the ON state. 1769a1fccb47SMitsuru IWASAKI */ 1770a1fccb47SMitsuru IWASAKI 1771a1fccb47SMitsuru IWASAKI ArgList.Count = 1; 1772a1fccb47SMitsuru IWASAKI ArgList.Pointer = &Arg; 1773a1fccb47SMitsuru IWASAKI 1774a1fccb47SMitsuru IWASAKI Arg.Type = ACPI_TYPE_INTEGER; 1775a1fccb47SMitsuru IWASAKI Arg.Integer.Value = enable; 1776a1fccb47SMitsuru IWASAKI 1777a1fccb47SMitsuru IWASAKI (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1778a1fccb47SMitsuru IWASAKI } 1779a1fccb47SMitsuru IWASAKI 1780a1fccb47SMitsuru IWASAKI void 1781a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h) 1782a1fccb47SMitsuru IWASAKI { 1783a1fccb47SMitsuru IWASAKI struct acpi_softc *sc; 1784a1fccb47SMitsuru IWASAKI ACPI_STATUS status; 1785a1fccb47SMitsuru IWASAKI ACPI_BUFFER prw_buffer; 1786a1fccb47SMitsuru IWASAKI ACPI_OBJECT *res; 1787a1fccb47SMitsuru IWASAKI 1788a1fccb47SMitsuru IWASAKI ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1789a1fccb47SMitsuru IWASAKI 1790be2b1797SNate Lawson sc = devclass_get_softc(acpi_devclass, 0); 1791be2b1797SNate Lawson if (sc == NULL) 1792a1fccb47SMitsuru IWASAKI return; 1793a1fccb47SMitsuru IWASAKI 1794a1fccb47SMitsuru IWASAKI /* 1795a1fccb47SMitsuru IWASAKI * _PRW object is only required for devices that have the ability 1796a1fccb47SMitsuru IWASAKI * to wake the system from a system sleeping state. 1797a1fccb47SMitsuru IWASAKI */ 1798a1fccb47SMitsuru IWASAKI prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1799a1fccb47SMitsuru IWASAKI status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1800be2b1797SNate Lawson if (ACPI_FAILURE(status)) 1801a1fccb47SMitsuru IWASAKI return; 1802a1fccb47SMitsuru IWASAKI 1803a1fccb47SMitsuru IWASAKI res = (ACPI_OBJECT *)prw_buffer.Pointer; 1804be2b1797SNate Lawson if (res == NULL) 1805a1fccb47SMitsuru IWASAKI return; 1806a1fccb47SMitsuru IWASAKI 1807a1fccb47SMitsuru IWASAKI if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1808a1fccb47SMitsuru IWASAKI goto out; 1809a1fccb47SMitsuru IWASAKI } 1810a1fccb47SMitsuru IWASAKI 1811a1fccb47SMitsuru IWASAKI /* 1812a1fccb47SMitsuru IWASAKI * The element 1 of the _PRW object: 1813a1fccb47SMitsuru IWASAKI * The lowest power system sleeping state that can be entered 1814a1fccb47SMitsuru IWASAKI * while still providing wake functionality. 1815a1fccb47SMitsuru IWASAKI * The sleeping state being entered must be greater or equal to 1816a1fccb47SMitsuru IWASAKI * the power state declared in element 1 of the _PRW object. 1817a1fccb47SMitsuru IWASAKI */ 1818be2b1797SNate Lawson if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) 1819a1fccb47SMitsuru IWASAKI goto out; 1820a1fccb47SMitsuru IWASAKI 1821be2b1797SNate Lawson if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) 1822a1fccb47SMitsuru IWASAKI goto out; 1823a1fccb47SMitsuru IWASAKI 1824a1fccb47SMitsuru IWASAKI /* 1825a1fccb47SMitsuru IWASAKI * The element 0 of the _PRW object: 1826a1fccb47SMitsuru IWASAKI */ 1827a1fccb47SMitsuru IWASAKI switch(res->Package.Elements[0].Type) { 1828a1fccb47SMitsuru IWASAKI case ACPI_TYPE_INTEGER: 1829a1fccb47SMitsuru IWASAKI /* 1830a1fccb47SMitsuru IWASAKI * If the data type of this package element is numeric, then this 1831a1fccb47SMitsuru IWASAKI * _PRW package element is the bit index in the GPEx_EN, in the 1832a1fccb47SMitsuru IWASAKI * GPE blocks described in the FADT, of the enable bit that is 1833a1fccb47SMitsuru IWASAKI * enabled for the wake event. 1834a1fccb47SMitsuru IWASAKI */ 1835a1fccb47SMitsuru IWASAKI 18366fca9360SNate Lawson status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value, 18376fca9360SNate Lawson ACPI_EVENT_WAKE_ENABLE); 1838a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) 1839a1fccb47SMitsuru IWASAKI printf("%s: EnableEvent Failed\n", __func__); 1840a1fccb47SMitsuru IWASAKI break; 1841a1fccb47SMitsuru IWASAKI case ACPI_TYPE_PACKAGE: 1842a1fccb47SMitsuru IWASAKI /* 1843be2b1797SNate Lawson * XXX TBD 1844be2b1797SNate Lawson * 1845a1fccb47SMitsuru IWASAKI * If the data type of this package element is a package, then this 1846a1fccb47SMitsuru IWASAKI * _PRW package element is itself a package containing two 1847a1fccb47SMitsuru IWASAKI * elements. The first is an object reference to the GPE Block 1848a1fccb47SMitsuru IWASAKI * device that contains the GPE that will be triggered by the wake 1849a1fccb47SMitsuru IWASAKI * event. The second element is numeric and it contains the bit 1850a1fccb47SMitsuru IWASAKI * index in the GPEx_EN, in the GPE Block referenced by the 1851a1fccb47SMitsuru IWASAKI * first element in the package, of the enable bit that is enabled for 1852a1fccb47SMitsuru IWASAKI * the wake event. 1853a1fccb47SMitsuru IWASAKI * For example, if this field is a package then it is of the form: 1854a1fccb47SMitsuru IWASAKI * Package() {\_SB.PCI0.ISA.GPE, 2} 1855a1fccb47SMitsuru IWASAKI */ 1856a1fccb47SMitsuru IWASAKI break; 1857a1fccb47SMitsuru IWASAKI default: 1858a1fccb47SMitsuru IWASAKI break; 1859a1fccb47SMitsuru IWASAKI } 1860a1fccb47SMitsuru IWASAKI 1861a1fccb47SMitsuru IWASAKI out: 1862a1fccb47SMitsuru IWASAKI if (prw_buffer.Pointer != NULL) 1863a1fccb47SMitsuru IWASAKI AcpiOsFree(prw_buffer.Pointer); 1864a1fccb47SMitsuru IWASAKI } 1865a1fccb47SMitsuru IWASAKI 1866a1fccb47SMitsuru IWASAKI /* 186715e32d5dSMike Smith * Control interface. 186815e32d5dSMike Smith * 18690ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 1870be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 1871be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 187215e32d5dSMike Smith */ 18730ae55423SMike Smith struct acpi_ioctl_hook 18740ae55423SMike Smith { 18750ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 18760ae55423SMike Smith u_long cmd; 1877be2b1797SNate Lawson acpi_ioctl_fn fn; 18780ae55423SMike Smith void *arg; 18790ae55423SMike Smith }; 18800ae55423SMike Smith 18810ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 18820ae55423SMike Smith static int acpi_ioctl_hooks_initted; 18830ae55423SMike Smith 18840ae55423SMike Smith /* 18850ae55423SMike Smith * Register an ioctl handler. 18860ae55423SMike Smith */ 18870ae55423SMike Smith int 1888be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 18890ae55423SMike Smith { 18900ae55423SMike Smith struct acpi_ioctl_hook *hp; 18910ae55423SMike Smith 18920ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 18930ae55423SMike Smith return (ENOMEM); 18940ae55423SMike Smith hp->cmd = cmd; 18950ae55423SMike Smith hp->fn = fn; 18960ae55423SMike Smith hp->arg = arg; 18970ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 18980ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 18990ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 19000ae55423SMike Smith } 19010ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 19020ae55423SMike Smith return (0); 19030ae55423SMike Smith } 19040ae55423SMike Smith 19050ae55423SMike Smith /* 19060ae55423SMike Smith * Deregister an ioctl handler. 19070ae55423SMike Smith */ 19080ae55423SMike Smith void 1909be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 19100ae55423SMike Smith { 19110ae55423SMike Smith struct acpi_ioctl_hook *hp; 19120ae55423SMike Smith 19130ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 19140ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 19150ae55423SMike Smith break; 19160ae55423SMike Smith 19170ae55423SMike Smith if (hp != NULL) { 19180ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 19190ae55423SMike Smith free(hp, M_ACPIDEV); 19200ae55423SMike Smith } 19210ae55423SMike Smith } 19220ae55423SMike Smith 192315e32d5dSMike Smith static int 19246b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 192515e32d5dSMike Smith { 192615e32d5dSMike Smith return (0); 192715e32d5dSMike Smith } 192815e32d5dSMike Smith 192915e32d5dSMike Smith static int 19306b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 193115e32d5dSMike Smith { 193215e32d5dSMike Smith return (0); 193315e32d5dSMike Smith } 193415e32d5dSMike Smith 193515e32d5dSMike Smith static int 19366b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 193715e32d5dSMike Smith { 193815e32d5dSMike Smith struct acpi_softc *sc; 19390ae55423SMike Smith struct acpi_ioctl_hook *hp; 19400ae55423SMike Smith int error, xerror, state; 1941fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 194215e32d5dSMike Smith 1943cb9b0d80SMike Smith ACPI_LOCK; 1944cb9b0d80SMike Smith 194515e32d5dSMike Smith error = state = 0; 194615e32d5dSMike Smith sc = dev->si_drv1; 194715e32d5dSMike Smith 19480ae55423SMike Smith /* 19490ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 19500ae55423SMike Smith */ 19510ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 19520ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 19530ae55423SMike Smith if (hp->cmd == cmd) { 19540ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 19550ae55423SMike Smith if (xerror != 0) 19560ae55423SMike Smith error = xerror; 1957917d44c8SMitsuru IWASAKI goto out; 19580ae55423SMike Smith } 19590ae55423SMike Smith } 19600ae55423SMike Smith } 19610ae55423SMike Smith 19620ae55423SMike Smith /* 1963a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 1964a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 1965a89fcf28STakanori Watanabe * Not changing system behavior. 1966a89fcf28STakanori Watanabe */ 1967be2b1797SNate Lawson if((flag & FWRITE) == 0) 1968be2b1797SNate Lawson return (EPERM); 1969a89fcf28STakanori Watanabe 1970be2b1797SNate Lawson /* Core system ioctls. */ 197115e32d5dSMike Smith switch (cmd) { 197215e32d5dSMike Smith case ACPIIO_ENABLE: 19730ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 197415e32d5dSMike Smith error = ENXIO; 197515e32d5dSMike Smith break; 197615e32d5dSMike Smith case ACPIIO_DISABLE: 19770ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 197815e32d5dSMike Smith error = ENXIO; 197915e32d5dSMike Smith break; 198015e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 198115e32d5dSMike Smith if (!sc->acpi_enabled) { 198215e32d5dSMike Smith error = ENXIO; 198315e32d5dSMike Smith break; 198415e32d5dSMike Smith } 198515e32d5dSMike Smith state = *(int *)addr; 19862d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 19872d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 198815e32d5dSMike Smith error = EINVAL; 19892d610c46SNate Lawson } else { 19902d610c46SNate Lawson error = EINVAL; 19912d610c46SNate Lawson } 199215e32d5dSMike Smith break; 199315e32d5dSMike Smith default: 19940ae55423SMike Smith if (error == 0) 199515e32d5dSMike Smith error = EINVAL; 199615e32d5dSMike Smith break; 199715e32d5dSMike Smith } 1998917d44c8SMitsuru IWASAKI 1999917d44c8SMitsuru IWASAKI out: 2000cb9b0d80SMike Smith ACPI_UNLOCK; 200115e32d5dSMike Smith return (error); 200215e32d5dSMike Smith } 200315e32d5dSMike Smith 20041d073b1dSJohn Baldwin static int 2005d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2006d75de536SMitsuru IWASAKI { 2007d75de536SMitsuru IWASAKI char sleep_state[4]; 2008d75de536SMitsuru IWASAKI char buf[16]; 2009d75de536SMitsuru IWASAKI int error; 2010d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2011d75de536SMitsuru IWASAKI 2012d75de536SMitsuru IWASAKI buf[0] = '\0'; 2013d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) { 2014d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2015d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2016d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2017d75de536SMitsuru IWASAKI } 2018d75de536SMitsuru IWASAKI } 2019d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2020d75de536SMitsuru IWASAKI return (error); 2021d75de536SMitsuru IWASAKI } 2022d75de536SMitsuru IWASAKI 2023d75de536SMitsuru IWASAKI static int 20241d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 20251d073b1dSJohn Baldwin { 20261d073b1dSJohn Baldwin char sleep_state[10]; 20271d073b1dSJohn Baldwin int error; 20281d073b1dSJohn Baldwin u_int new_state, old_state; 20291d073b1dSJohn Baldwin 20301d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2031b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX+1) { 20321d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2033cb9b0d80SMike Smith } else { 2034b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 20351d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 20361d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2037cb9b0d80SMike Smith } 20381d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 20391d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2040be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2041be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 20421d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 20431d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 20441d073b1dSJohn Baldwin break; 2045cb9b0d80SMike Smith } 2046931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2047be2b1797SNate Lawson if (new_state != old_state) 20481d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2049cb9b0d80SMike Smith } else { 20501d073b1dSJohn Baldwin error = EINVAL; 20511d073b1dSJohn Baldwin } 2052cb9b0d80SMike Smith } 2053be2b1797SNate Lawson 20541d073b1dSJohn Baldwin return (error); 20551d073b1dSJohn Baldwin } 20561d073b1dSJohn Baldwin 205715e32d5dSMike Smith #ifdef ACPI_DEBUG 20580ae55423SMike Smith /* 20590ae55423SMike Smith * Support for parsing debug options from the kernel environment. 20600ae55423SMike Smith * 20610ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 20620ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 20630ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 20640ae55423SMike Smith * prefixing the bit name with !. 20650ae55423SMike Smith */ 206615e32d5dSMike Smith struct debugtag 206715e32d5dSMike Smith { 206815e32d5dSMike Smith char *name; 206915e32d5dSMike Smith UINT32 value; 207015e32d5dSMike Smith }; 207115e32d5dSMike Smith 207215e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2073c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2074c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2075c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2076c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2077c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2078c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2079c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2080c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2081c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2082d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 20834c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2084d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 20854c1cdee6SMike Smith 2086cb9b0d80SMike Smith {"ACPI_BUS", ACPI_BUS}, 20874c1cdee6SMike Smith {"ACPI_SYSTEM", ACPI_SYSTEM}, 2088cb9b0d80SMike Smith {"ACPI_POWER", ACPI_POWER}, 2089cb9b0d80SMike Smith {"ACPI_EC", ACPI_EC}, 2090c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2091c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 2092c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 20934c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2094cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 20954c1cdee6SMike Smith {"ACPI_FAN", ACPI_FAN}, 20964c1cdee6SMike Smith 2097b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 2098c5ba0be4SMike Smith {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 209915e32d5dSMike Smith {NULL, 0} 210015e32d5dSMike Smith }; 210115e32d5dSMike Smith 210215e32d5dSMike Smith static struct debugtag dbg_level[] = { 21034c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 21049501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 21059501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 21064c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 21079501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 21084c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2109d62ab2f4SMitsuru IWASAKI 2110d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 21114c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 21124c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2113d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 21144c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 21154c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 21164c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 21174c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 21184c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 21194c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 21204c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 21214c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 21224c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 21234c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 21249501b603SJohn Baldwin {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 2125d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2126d62ab2f4SMitsuru IWASAKI 2127d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2128d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2129d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2130d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2131d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 21324c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2133d62ab2f4SMitsuru IWASAKI 2134d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2135d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2136d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2137d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2138d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2139d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2140d62ab2f4SMitsuru IWASAKI 2141d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 214298479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 214398479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 214498479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 214598479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 214698479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 214715e32d5dSMike Smith {NULL, 0} 214815e32d5dSMike Smith }; 214915e32d5dSMike Smith 215015e32d5dSMike Smith static void 215115e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 215215e32d5dSMike Smith { 215315e32d5dSMike Smith char *ep; 215415e32d5dSMike Smith int i, l; 21550ae55423SMike Smith int set; 215615e32d5dSMike Smith 215715e32d5dSMike Smith while (*cp) { 215815e32d5dSMike Smith if (isspace(*cp)) { 215915e32d5dSMike Smith cp++; 216015e32d5dSMike Smith continue; 216115e32d5dSMike Smith } 216215e32d5dSMike Smith ep = cp; 216315e32d5dSMike Smith while (*ep && !isspace(*ep)) 216415e32d5dSMike Smith ep++; 21650ae55423SMike Smith if (*cp == '!') { 21660ae55423SMike Smith set = 0; 21670ae55423SMike Smith cp++; 21680ae55423SMike Smith if (cp == ep) 21690ae55423SMike Smith continue; 21700ae55423SMike Smith } else { 21710ae55423SMike Smith set = 1; 21720ae55423SMike Smith } 217315e32d5dSMike Smith l = ep - cp; 217415e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 217515e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2176be2b1797SNate Lawson if (set) 217715e32d5dSMike Smith *flag |= tag[i].value; 2178be2b1797SNate Lawson else 21790ae55423SMike Smith *flag &= ~tag[i].value; 218015e32d5dSMike Smith printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 218115e32d5dSMike Smith } 218215e32d5dSMike Smith } 218315e32d5dSMike Smith cp = ep; 218415e32d5dSMike Smith } 218515e32d5dSMike Smith } 218615e32d5dSMike Smith 218715e32d5dSMike Smith static void 21882a4ac806SMike Smith acpi_set_debugging(void *junk) 218915e32d5dSMike Smith { 219094aae282SMike Barcroft char *cp; 219115e32d5dSMike Smith 219287b45ed5SMitsuru IWASAKI if (!cold) 219387b45ed5SMitsuru IWASAKI return; 219487b45ed5SMitsuru IWASAKI 21950ae55423SMike Smith AcpiDbgLayer = 0; 21960ae55423SMike Smith AcpiDbgLevel = 0; 219794aae282SMike Barcroft if ((cp = getenv("debug.acpi.layer")) != NULL) { 219815e32d5dSMike Smith acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 219994aae282SMike Barcroft freeenv(cp); 220094aae282SMike Barcroft } 220194aae282SMike Barcroft if ((cp = getenv("debug.acpi.level")) != NULL) { 220215e32d5dSMike Smith acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 220394aae282SMike Barcroft freeenv(cp); 220494aae282SMike Barcroft } 22050ae55423SMike Smith 2206be2b1797SNate Lawson printf("ACPI debug layer 0x%x debug level 0x%x\n", AcpiDbgLayer, 2207be2b1797SNate Lawson AcpiDbgLevel); 220815e32d5dSMike Smith } 2209be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2210be2b1797SNate Lawson NULL); 221115e32d5dSMike Smith #endif 2212f9390180SMitsuru IWASAKI 2213f9390180SMitsuru IWASAKI static int 2214f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2215f9390180SMitsuru IWASAKI { 2216f9390180SMitsuru IWASAKI int state, acpi_state; 2217f9390180SMitsuru IWASAKI int error; 2218f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2219f9390180SMitsuru IWASAKI va_list ap; 2220f9390180SMitsuru IWASAKI 2221f9390180SMitsuru IWASAKI error = 0; 2222f9390180SMitsuru IWASAKI switch (cmd) { 2223f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2224f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2225f9390180SMitsuru IWASAKI if (sc == NULL) { 2226f9390180SMitsuru IWASAKI error = EINVAL; 2227f9390180SMitsuru IWASAKI goto out; 2228f9390180SMitsuru IWASAKI } 2229f9390180SMitsuru IWASAKI 2230f9390180SMitsuru IWASAKI va_start(ap, arg); 2231f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2232f9390180SMitsuru IWASAKI va_end(ap); 2233f9390180SMitsuru IWASAKI 2234f9390180SMitsuru IWASAKI switch (state) { 2235f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2236f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2237f9390180SMitsuru IWASAKI break; 2238f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2239f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2240f9390180SMitsuru IWASAKI break; 2241f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2242f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2243f9390180SMitsuru IWASAKI break; 2244f9390180SMitsuru IWASAKI default: 2245f9390180SMitsuru IWASAKI error = EINVAL; 2246f9390180SMitsuru IWASAKI goto out; 2247f9390180SMitsuru IWASAKI } 2248f9390180SMitsuru IWASAKI 2249f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2250f9390180SMitsuru IWASAKI break; 2251f9390180SMitsuru IWASAKI default: 2252f9390180SMitsuru IWASAKI error = EINVAL; 2253f9390180SMitsuru IWASAKI goto out; 2254f9390180SMitsuru IWASAKI } 2255f9390180SMitsuru IWASAKI 2256f9390180SMitsuru IWASAKI out: 2257f9390180SMitsuru IWASAKI return (error); 2258f9390180SMitsuru IWASAKI } 2259f9390180SMitsuru IWASAKI 2260f9390180SMitsuru IWASAKI static void 2261f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2262f9390180SMitsuru IWASAKI { 2263be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 22646c407052SMitsuru IWASAKI return; 2265f9390180SMitsuru IWASAKI 2266f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2267f9390180SMitsuru IWASAKI } 2268f9390180SMitsuru IWASAKI 2269f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2270