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 1601d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 1611d7b121cSNate Lawson static char acpi_ca_version[12]; 1621d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 1631d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 16415e32d5dSMike Smith 16515e32d5dSMike Smith /* 1666d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 1676d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 168be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 169be2b1797SNate Lawson * off it. 1706d63101aSMike Smith */ 1716d63101aSMike Smith static int 1726d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 1736d63101aSMike Smith { 1746d63101aSMike Smith switch(event) { 1756d63101aSMike Smith case MOD_LOAD: 17687b45ed5SMitsuru IWASAKI if (!cold) { 17787b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 1786d63101aSMike Smith return (EPERM); 17987b45ed5SMitsuru IWASAKI } 1806d63101aSMike Smith break; 1816d63101aSMike Smith case MOD_UNLOAD: 182f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 1836d63101aSMike Smith return (EBUSY); 184f9390180SMitsuru IWASAKI break; 1856d63101aSMike Smith default: 1866d63101aSMike Smith break; 1876d63101aSMike Smith } 1886d63101aSMike Smith return (0); 1896d63101aSMike Smith } 1906d63101aSMike Smith 1916d63101aSMike Smith /* 19215e32d5dSMike Smith * Detect ACPI, perform early initialisation 19315e32d5dSMike Smith */ 19415e32d5dSMike Smith static void 19515e32d5dSMike Smith acpi_identify(driver_t *driver, device_t parent) 19615e32d5dSMike Smith { 19715e32d5dSMike Smith device_t child; 19815e32d5dSMike Smith int error; 199d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 200d786139cSMaxime Henrion char *debugpoint; 20115e32d5dSMike Smith #endif 20215e32d5dSMike Smith 203b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2040ae55423SMike Smith 20587b45ed5SMitsuru IWASAKI if (!cold) 20687b45ed5SMitsuru IWASAKI return_VOID; 207840f9b53STakanori Watanabe 208be2b1797SNate Lawson /* Check that we haven't been disabled with a hint. */ 2098a9bc9c0SJohn Baldwin if (resource_disabled("acpi", 0)) 21032d18aa5SMike Smith return_VOID; 21132d18aa5SMike Smith 2121d7b121cSNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "0x%x", 2131d7b121cSNate Lawson ACPI_CA_VERSION); 2141d7b121cSNate Lawson 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 1031cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1032cb9b0d80SMike Smith 103313d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1034be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 103559ddeb18SNate Lawson AcpiEnableEvent(ACPI_EVENT_POWER_BUTTON, 0); 103659ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 103713d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 1038be2b1797SNate Lawson acpi_eventhandler_power_button_for_sleep, 1039be2b1797SNate Lawson sc); 1040be2b1797SNate Lawson if (first_time) 10416c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 104213d4f7baSMitsuru IWASAKI } 1043be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 104459ddeb18SNate Lawson AcpiEnableEvent(ACPI_EVENT_SLEEP_BUTTON, 0); 104559ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 104613d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 1047be2b1797SNate Lawson acpi_eventhandler_sleep_button_for_sleep, 1048be2b1797SNate Lawson sc); 1049be2b1797SNate Lawson if (first_time) 10506c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 105113d4f7baSMitsuru IWASAKI } 105213d4f7baSMitsuru IWASAKI 105313d4f7baSMitsuru IWASAKI first_time = 0; 105413d4f7baSMitsuru IWASAKI } 105513d4f7baSMitsuru IWASAKI 105615e32d5dSMike Smith /* 1057c5ba0be4SMike Smith * Returns true if the device is actually present and should 1058c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1059c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1060c5ba0be4SMike Smith */ 1061c5ba0be4SMike Smith BOOLEAN 1062c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1063c5ba0be4SMike Smith { 1064c5ba0be4SMike Smith ACPI_HANDLE h; 1065c5ba0be4SMike Smith ACPI_DEVICE_INFO devinfo; 10666fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 1067c5ba0be4SMike Smith ACPI_STATUS error; 1068c5ba0be4SMike Smith 1069cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1070cb9b0d80SMike Smith 1071c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1072c5ba0be4SMike Smith return (FALSE); 10736fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10746fca9360SNate Lawson if (ACPI_FAILURE(error)) 1075c5ba0be4SMike Smith return (FALSE); 1076be2b1797SNate Lawson 1077be2b1797SNate Lawson /* If no _STA method, must be present */ 10786fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_STA) == 0) 107943896e91SMike Smith return (TRUE); 1080be2b1797SNate Lawson 1081be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 108243896e91SMike Smith if ((devinfo.CurrentStatus & 0x9) == 0x9) 1083c5ba0be4SMike Smith return (TRUE); 1084be2b1797SNate Lawson 1085c5ba0be4SMike Smith return (FALSE); 1086c5ba0be4SMike Smith } 1087c5ba0be4SMike Smith 1088c5ba0be4SMike Smith /* 1089b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1090b53f2771SMike Smith */ 1091b53f2771SMike Smith BOOLEAN 1092b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1093b53f2771SMike Smith { 1094b53f2771SMike Smith ACPI_HANDLE h; 1095b53f2771SMike Smith ACPI_DEVICE_INFO devinfo; 10966fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 1097b53f2771SMike Smith ACPI_STATUS error; 1098b53f2771SMike Smith 1099b53f2771SMike Smith ACPI_ASSERTLOCK; 1100b53f2771SMike Smith 1101b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1102b53f2771SMike Smith return (FALSE); 11036fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 11046fca9360SNate Lawson if (ACPI_FAILURE(error)) 1105b53f2771SMike Smith return (FALSE); 1106be2b1797SNate Lawson 1107be2b1797SNate Lawson /* If no _STA method, must be present */ 11086fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_STA) == 0) 1109b53f2771SMike Smith return (TRUE); 1110be2b1797SNate Lawson 1111be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 1112b53f2771SMike Smith if ((devinfo.CurrentStatus & 0x19) == 0x19) 1113b53f2771SMike Smith return (TRUE); 1114be2b1797SNate Lawson 1115b53f2771SMike Smith return (FALSE); 1116b53f2771SMike Smith } 1117b53f2771SMike Smith 1118b53f2771SMike Smith /* 111915e32d5dSMike Smith * Match a HID string against a device 112015e32d5dSMike Smith */ 112115e32d5dSMike Smith BOOLEAN 112215e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 112315e32d5dSMike Smith { 112415e32d5dSMike Smith ACPI_HANDLE h; 112515e32d5dSMike Smith ACPI_DEVICE_INFO devinfo; 11266fca9360SNate Lawson ACPI_BUFFER buf = {sizeof(devinfo), &devinfo}; 112715e32d5dSMike Smith ACPI_STATUS error; 1128ed136da6SDoug Rabson int cid; 112915e32d5dSMike Smith 1130cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1131cb9b0d80SMike Smith 11324d332989SMike Smith if (hid == NULL) 113315e32d5dSMike Smith return (FALSE); 113415e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 113515e32d5dSMike Smith return (FALSE); 11366fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 11376fca9360SNate Lawson if (ACPI_FAILURE(error)) 113815e32d5dSMike Smith return (FALSE); 11396fca9360SNate Lawson if ((devinfo.Valid & ACPI_VALID_HID) != 0 && 11406fca9360SNate Lawson strcmp(hid, devinfo.HardwareId.Value) == 0) 114115e32d5dSMike Smith return (TRUE); 1142be2b1797SNate Lawson 1143b53f2771SMike Smith if (ACPI_FAILURE(error = acpi_EvaluateInteger(h, "_CID", &cid))) 1144ed136da6SDoug Rabson return (FALSE); 1145ed136da6SDoug Rabson if (cid == PNP_EISAID(hid)) 1146ed136da6SDoug Rabson return (TRUE); 1147be2b1797SNate Lawson 114815e32d5dSMike Smith return (FALSE); 114915e32d5dSMike Smith } 115015e32d5dSMike Smith 115115e32d5dSMike Smith /* 1152c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1153c5ba0be4SMike Smith * or one if its parents. 1154c5ba0be4SMike Smith */ 1155c5ba0be4SMike Smith ACPI_STATUS 1156c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1157c5ba0be4SMike Smith { 1158c5ba0be4SMike Smith ACPI_HANDLE r; 1159c5ba0be4SMike Smith ACPI_STATUS status; 1160c5ba0be4SMike Smith 1161cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1162cb9b0d80SMike Smith 1163be2b1797SNate Lawson /* Walk back up the tree to the root */ 1164c5ba0be4SMike Smith for (;;) { 1165be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1166be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1167c5ba0be4SMike Smith *result = r; 1168c5ba0be4SMike Smith return (AE_OK); 1169c5ba0be4SMike Smith } 1170c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1171c5ba0be4SMike Smith return (AE_OK); 1172b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1173c5ba0be4SMike Smith return (AE_NOT_FOUND); 1174c5ba0be4SMike Smith parent = r; 1175c5ba0be4SMike Smith } 1176c5ba0be4SMike Smith } 1177c5ba0be4SMike Smith 1178c5ba0be4SMike Smith /* 1179c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1180c5ba0be4SMike Smith */ 1181c5ba0be4SMike Smith ACPI_BUFFER * 1182c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1183c5ba0be4SMike Smith { 1184c5ba0be4SMike Smith ACPI_BUFFER *buf; 1185c5ba0be4SMike Smith 1186c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1187c5ba0be4SMike Smith return (NULL); 1188c5ba0be4SMike Smith buf->Length = size; 1189c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1190c5ba0be4SMike Smith return (buf); 1191c5ba0be4SMike Smith } 1192c5ba0be4SMike Smith 1193c5ba0be4SMike Smith /* 1194c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1195c5ba0be4SMike Smith */ 1196c5ba0be4SMike Smith ACPI_STATUS 1197c5ba0be4SMike Smith acpi_EvaluateInteger(ACPI_HANDLE handle, char *path, int *number) 1198c5ba0be4SMike Smith { 1199be2b1797SNate Lawson ACPI_STATUS status; 1200c5ba0be4SMike Smith ACPI_BUFFER buf; 1201c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1202c5ba0be4SMike Smith 1203cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1204cb9b0d80SMike Smith 1205c5ba0be4SMike Smith if (handle == NULL) 1206c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 12070c7da7acSJohn Baldwin 12080c7da7acSJohn Baldwin /* 12090c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 12100c7da7acSJohn Baldwin * a method that will return an Integer. 12110c7da7acSJohn Baldwin */ 1212c5ba0be4SMike Smith buf.Pointer = ¶m; 1213c5ba0be4SMike Smith buf.Length = sizeof(param); 1214be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1215be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1216be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1217c5ba0be4SMike Smith *number = param.Integer.Value; 1218be2b1797SNate Lawson else 1219be2b1797SNate Lawson status = AE_TYPE; 1220c5ba0be4SMike Smith } 12210c7da7acSJohn Baldwin 12220c7da7acSJohn Baldwin /* 12230c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 12240c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 12250c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 12260c7da7acSJohn Baldwin * convert it into an Integer as best we can. 12270c7da7acSJohn Baldwin * 12280c7da7acSJohn Baldwin * This is a hack. 12290c7da7acSJohn Baldwin */ 1230be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1231b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1232be2b1797SNate Lawson status = AE_NO_MEMORY; 12330c7da7acSJohn Baldwin } else { 1234be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1235be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1236be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 12370c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 12380c7da7acSJohn Baldwin } 1239f97739daSNate Lawson } 1240be2b1797SNate Lawson return (status); 1241c5ba0be4SMike Smith } 1242c5ba0be4SMike Smith 1243c573e654SMitsuru IWASAKI ACPI_STATUS 1244c573e654SMitsuru IWASAKI acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, int *number) 1245c573e654SMitsuru IWASAKI { 1246c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1247c573e654SMitsuru IWASAKI int i; 1248c573e654SMitsuru IWASAKI 1249c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1250c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1251c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1252c573e654SMitsuru IWASAKI return (AE_OK); 1253c573e654SMitsuru IWASAKI } 1254c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1255c573e654SMitsuru IWASAKI return (AE_TYPE); 1256c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1257c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1258be2b1797SNate Lawson 1259c573e654SMitsuru IWASAKI *number = 0; 1260c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1261c573e654SMitsuru IWASAKI *number += (*(p->Buffer.Pointer + i) << (i * 8)); 1262c573e654SMitsuru IWASAKI return (AE_OK); 1263c573e654SMitsuru IWASAKI } 1264c573e654SMitsuru IWASAKI 1265c5ba0be4SMike Smith /* 1266c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1267c5ba0be4SMike Smith * function for each element. 1268c5ba0be4SMike Smith * 1269c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1270c5ba0be4SMike Smith */ 1271c5ba0be4SMike Smith ACPI_STATUS 1272be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1273be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1274c5ba0be4SMike Smith { 1275c5ba0be4SMike Smith ACPI_OBJECT *comp; 1276c5ba0be4SMike Smith int i; 1277c5ba0be4SMike Smith 1278be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1279c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1280c5ba0be4SMike Smith 1281be2b1797SNate Lawson /* Iterate over components */ 1282be2b1797SNate Lawson i = 0; 1283be2b1797SNate Lawson comp = pkg->Package.Elements; 1284be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1285c5ba0be4SMike Smith func(comp, arg); 1286c5ba0be4SMike Smith 1287c5ba0be4SMike Smith return (AE_OK); 1288c5ba0be4SMike Smith } 1289c5ba0be4SMike Smith 12906f69255bSMike Smith /* 12916f69255bSMike Smith * Find the (index)th resource object in a set. 12926f69255bSMike Smith */ 12936f69255bSMike Smith ACPI_STATUS 12946d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 12956f69255bSMike Smith { 12966d63101aSMike Smith ACPI_RESOURCE *rp; 12976f69255bSMike Smith int i; 12986f69255bSMike Smith 12996d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13006f69255bSMike Smith i = index; 13016d63101aSMike Smith while (i-- > 0) { 1302be2b1797SNate Lawson /* Range check */ 13036d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 13046f69255bSMike Smith return (AE_BAD_PARAMETER); 1305be2b1797SNate Lawson 1306be2b1797SNate Lawson /* Check for terminator */ 1307be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 13086d63101aSMike Smith return (AE_NOT_FOUND); 13096d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13106f69255bSMike Smith } 13116f69255bSMike Smith if (resp != NULL) 13126d63101aSMike Smith *resp = rp; 1313be2b1797SNate Lawson 13146d63101aSMike Smith return (AE_OK); 13156d63101aSMike Smith } 13166d63101aSMike Smith 13176d63101aSMike Smith /* 13186d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 13196d63101aSMike Smith * 13206d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 13216d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 13226d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 13236d63101aSMike Smith * resources. 13246d63101aSMike Smith */ 13256d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 13266d63101aSMike Smith 13276d63101aSMike Smith ACPI_STATUS 13286d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 13296d63101aSMike Smith { 13306d63101aSMike Smith ACPI_RESOURCE *rp; 13316d63101aSMike Smith void *newp; 13326d63101aSMike Smith 1333be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 13346d63101aSMike Smith if (buf->Pointer == NULL) { 13356d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 13366d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 13376d63101aSMike Smith return (AE_NO_MEMORY); 13386d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13396d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 13406d63101aSMike Smith rp->Length = 0; 13416d63101aSMike Smith } 13426d63101aSMike Smith if (res == NULL) 13436d63101aSMike Smith return (AE_OK); 13446d63101aSMike Smith 13456d63101aSMike Smith /* 13466d63101aSMike Smith * Scan the current buffer looking for the terminator. 13476d63101aSMike Smith * This will either find the terminator or hit the end 13486d63101aSMike Smith * of the buffer and return an error. 13496d63101aSMike Smith */ 13506d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 13516d63101aSMike Smith for (;;) { 1352be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 13536d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 13546d63101aSMike Smith return (AE_BAD_PARAMETER); 1355be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 13566d63101aSMike Smith break; 13576d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13586d63101aSMike Smith } 13596d63101aSMike Smith 13606d63101aSMike Smith /* 13616d63101aSMike Smith * Check the size of the buffer and expand if required. 13626d63101aSMike Smith * 13636d63101aSMike Smith * Required size is: 13646d63101aSMike Smith * size of existing resources before terminator + 13656d63101aSMike Smith * size of new resource and header + 13666d63101aSMike Smith * size of terminator. 13676d63101aSMike Smith * 13686d63101aSMike Smith * Note that this loop should really only run once, unless 13696d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 13706d63101aSMike Smith */ 13716d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 13726d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 13736d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 13746d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 13756d63101aSMike Smith return (AE_NO_MEMORY); 13766d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1377a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1378a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 13796d63101aSMike Smith AcpiOsFree(buf->Pointer); 13806d63101aSMike Smith buf->Pointer = newp; 13816d63101aSMike Smith buf->Length += buf->Length; 13826d63101aSMike Smith } 13836d63101aSMike Smith 1384be2b1797SNate Lawson /* Insert the new resource. */ 13856d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 13866d63101aSMike Smith 1387be2b1797SNate Lawson /* And add the terminator. */ 13886d63101aSMike Smith rp = ACPI_RESOURCE_NEXT(rp); 13896d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 13906d63101aSMike Smith rp->Length = 0; 13916d63101aSMike Smith 13926f69255bSMike Smith return (AE_OK); 13936f69255bSMike Smith } 1394c5ba0be4SMike Smith 1395da14ac9fSJohn Baldwin /* 1396da14ac9fSJohn Baldwin * Set interrupt model. 1397da14ac9fSJohn Baldwin */ 1398da14ac9fSJohn Baldwin ACPI_STATUS 1399da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1400da14ac9fSJohn Baldwin { 1401da14ac9fSJohn Baldwin ACPI_OBJECT_LIST ArgList; 1402da14ac9fSJohn Baldwin ACPI_OBJECT Arg; 1403da14ac9fSJohn Baldwin 1404da14ac9fSJohn Baldwin Arg.Type = ACPI_TYPE_INTEGER; 1405da14ac9fSJohn Baldwin Arg.Integer.Value = model; 1406da14ac9fSJohn Baldwin ArgList.Count = 1; 1407da14ac9fSJohn Baldwin ArgList.Pointer = &Arg; 1408da14ac9fSJohn Baldwin return (AcpiEvaluateObject(ACPI_ROOT_OBJECT, "_PIC", &ArgList, NULL)); 1409da14ac9fSJohn Baldwin } 1410da14ac9fSJohn Baldwin 1411ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1412ece50487SMitsuru IWASAKI 1413ece50487SMitsuru IWASAKI static void 1414ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1415ece50487SMitsuru IWASAKI { 1416ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1417ece50487SMitsuru IWASAKI } 1418c30382dfSMitsuru IWASAKI 141915e32d5dSMike Smith /* 142015e32d5dSMike Smith * Set the system sleep state 142115e32d5dSMike Smith * 1422be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 142315e32d5dSMike Smith */ 142415e32d5dSMike Smith ACPI_STATUS 142515e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 142615e32d5dSMike Smith { 142715e32d5dSMike Smith ACPI_STATUS status = AE_OK; 142856d8cb57SMitsuru IWASAKI UINT8 TypeA; 142956d8cb57SMitsuru IWASAKI UINT8 TypeB; 143015e32d5dSMike Smith 1431b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1432cb9b0d80SMike Smith ACPI_ASSERTLOCK; 14330ae55423SMike Smith 143498175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 14356397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 143698175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 14376397624dSMitsuru IWASAKI 143898175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1439ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1440ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1441ece50487SMitsuru IWASAKI 144215e32d5dSMike Smith switch (state) { 144315e32d5dSMike Smith case ACPI_STATE_S1: 14446161544cSTakanori Watanabe case ACPI_STATE_S2: 14456161544cSTakanori Watanabe case ACPI_STATE_S3: 14466161544cSTakanori Watanabe case ACPI_STATE_S4: 1447be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 144898175614SNate Lawson if (status == AE_NOT_FOUND) { 144998175614SNate Lawson device_printf(sc->acpi_dev, 145098175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 145198175614SNate Lawson break; 145298175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1453be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1454be2b1797SNate Lawson AcpiFormatException(status)); 145556d8cb57SMitsuru IWASAKI break; 145656d8cb57SMitsuru IWASAKI } 145756d8cb57SMitsuru IWASAKI 1458a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1459a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1460a1fccb47SMitsuru IWASAKI 1461be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 146215e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 146315e32d5dSMike Smith /* 146415e32d5dSMike Smith * Re-wake the system. 146515e32d5dSMike Smith * 146615e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 146715e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 146815e32d5dSMike Smith * current bus interface does not provide for this. 146915e32d5dSMike Smith */ 147015e32d5dSMike Smith DEVICE_RESUME(root_bus); 14710ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 147215e32d5dSMike Smith } 1473b9c780d6SMitsuru IWASAKI 1474be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1475be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1476b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1477b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1478b9c780d6SMitsuru IWASAKI break; 1479b9c780d6SMitsuru IWASAKI } 1480b9c780d6SMitsuru IWASAKI 1481be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1482ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1483ff01efb5SMitsuru IWASAKI 14846161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 14856161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 14866161544cSTakanori Watanabe 1487be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 148859ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 148959ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 149059ddeb18SNate Lawson 14916161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1492a1fccb47SMitsuru IWASAKI } 14936161544cSTakanori Watanabe 14946161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1495be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1496964679ceSMitsuru IWASAKI AcpiEnable(); 14976161544cSTakanori Watanabe } else { 1498be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1499be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1500be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1501be2b1797SNate Lawson AcpiFormatException(status)); 1502c30382dfSMitsuru IWASAKI break; 150315e32d5dSMike Smith } 15046161544cSTakanori Watanabe } 1505ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 150615e32d5dSMike Smith DEVICE_RESUME(root_bus); 150715e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 150813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 150915e32d5dSMike Smith break; 151015e32d5dSMike Smith case ACPI_STATE_S5: 151115e32d5dSMike Smith /* 151215e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 151315e32d5dSMike Smith * shutdown handlers. 151415e32d5dSMike Smith */ 151515e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 151615e32d5dSMike Smith break; 151798175614SNate Lawson case ACPI_STATE_S0: 151815e32d5dSMike Smith default: 151915e32d5dSMike Smith status = AE_BAD_PARAMETER; 152015e32d5dSMike Smith break; 152115e32d5dSMike Smith } 1522ece50487SMitsuru IWASAKI 152398175614SNate Lawson /* Disable a second sleep request for a short period */ 1524ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1525ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1526ece50487SMitsuru IWASAKI 15270ae55423SMike Smith return_ACPI_STATUS (status); 152815e32d5dSMike Smith } 152915e32d5dSMike Smith 153015e32d5dSMike Smith /* 153115e32d5dSMike Smith * Enable/Disable ACPI 153215e32d5dSMike Smith */ 153315e32d5dSMike Smith ACPI_STATUS 153415e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 153515e32d5dSMike Smith { 153615e32d5dSMike Smith ACPI_STATUS status; 153715e32d5dSMike Smith u_int32_t flags; 153815e32d5dSMike Smith 1539b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1540cb9b0d80SMike Smith ACPI_ASSERTLOCK; 15410ae55423SMike Smith 154215e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 154315e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 1544be2b1797SNate Lawson if (!sc->acpi_enabled) 154515e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 1546be2b1797SNate Lawson else 154715e32d5dSMike Smith status = AE_OK; 1548be2b1797SNate Lawson 154915e32d5dSMike Smith if (status == AE_OK) 155015e32d5dSMike Smith sc->acpi_enabled = 1; 1551be2b1797SNate Lawson 15520ae55423SMike Smith return_ACPI_STATUS (status); 155315e32d5dSMike Smith } 155415e32d5dSMike Smith 155515e32d5dSMike Smith ACPI_STATUS 155615e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 155715e32d5dSMike Smith { 155815e32d5dSMike Smith ACPI_STATUS status; 155915e32d5dSMike Smith 1560b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1561cb9b0d80SMike Smith ACPI_ASSERTLOCK; 15620ae55423SMike Smith 1563be2b1797SNate Lawson if (sc->acpi_enabled) 156415e32d5dSMike Smith status = AcpiDisable(); 1565be2b1797SNate Lawson else 156615e32d5dSMike Smith status = AE_OK; 1567be2b1797SNate Lawson 156815e32d5dSMike Smith if (status == AE_OK) 156915e32d5dSMike Smith sc->acpi_enabled = 0; 1570be2b1797SNate Lawson 15710ae55423SMike Smith return_ACPI_STATUS (status); 157215e32d5dSMike Smith } 157315e32d5dSMike Smith 157415e32d5dSMike Smith /* 157515e32d5dSMike Smith * ACPI Event Handlers 157615e32d5dSMike Smith */ 157715e32d5dSMike Smith 157815e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 157915e32d5dSMike Smith 158015e32d5dSMike Smith static void 158115e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 158215e32d5dSMike Smith { 1583fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1584b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 158515e32d5dSMike Smith 1586cb9b0d80SMike Smith ACPI_LOCK; 1587a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 158815e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 1589cb9b0d80SMike Smith ACPI_UNLOCK; 15900ae55423SMike Smith return_VOID; 159115e32d5dSMike Smith } 159215e32d5dSMike Smith 159315e32d5dSMike Smith static void 159415e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 159515e32d5dSMike Smith { 1596fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 1597b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 15980ae55423SMike Smith 159915e32d5dSMike Smith /* Well, what to do? :-) */ 16000ae55423SMike Smith 1601cb9b0d80SMike Smith ACPI_LOCK; 1602cb9b0d80SMike Smith ACPI_UNLOCK; 1603cb9b0d80SMike Smith 16040ae55423SMike Smith return_VOID; 160515e32d5dSMike Smith } 160615e32d5dSMike Smith 160715e32d5dSMike Smith /* 160815e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 160915e32d5dSMike Smith */ 161015e32d5dSMike Smith UINT32 161115e32d5dSMike Smith acpi_eventhandler_power_button_for_sleep(void *context) 161215e32d5dSMike Smith { 161315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 161415e32d5dSMike Smith 1615b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16160ae55423SMike Smith 161715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 16180ae55423SMike Smith 1619b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 162015e32d5dSMike Smith } 162115e32d5dSMike Smith 162215e32d5dSMike Smith UINT32 162315e32d5dSMike Smith acpi_eventhandler_power_button_for_wakeup(void *context) 162415e32d5dSMike Smith { 162515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 162615e32d5dSMike Smith 1627b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16280ae55423SMike Smith 162915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 16300ae55423SMike Smith 1631b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 163215e32d5dSMike Smith } 163315e32d5dSMike Smith 163415e32d5dSMike Smith UINT32 163515e32d5dSMike Smith acpi_eventhandler_sleep_button_for_sleep(void *context) 163615e32d5dSMike Smith { 163715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 163815e32d5dSMike Smith 1639b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16400ae55423SMike Smith 164115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 16420ae55423SMike Smith 1643b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 164415e32d5dSMike Smith } 164515e32d5dSMike Smith 164615e32d5dSMike Smith UINT32 164715e32d5dSMike Smith acpi_eventhandler_sleep_button_for_wakeup(void *context) 164815e32d5dSMike Smith { 164915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 165015e32d5dSMike Smith 1651b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16520ae55423SMike Smith 165315e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 16540ae55423SMike Smith 1655b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 165615e32d5dSMike Smith } 165715e32d5dSMike Smith 165815e32d5dSMike Smith /* 165915e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 166015e32d5dSMike Smith */ 166115e32d5dSMike Smith struct acpi_staticbuf { 166215e32d5dSMike Smith ACPI_BUFFER buffer; 166315e32d5dSMike Smith char data[512]; 166415e32d5dSMike Smith }; 166515e32d5dSMike Smith 166615e32d5dSMike Smith char * 166715e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 166815e32d5dSMike Smith { 166915e32d5dSMike Smith static struct acpi_staticbuf buf; 167015e32d5dSMike Smith 1671cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1672cb9b0d80SMike Smith 167315e32d5dSMike Smith buf.buffer.Length = 512; 167415e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 167515e32d5dSMike Smith 1676b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 167715e32d5dSMike Smith return (buf.buffer.Pointer); 1678be2b1797SNate Lawson 167915e32d5dSMike Smith return ("(unknown path)"); 168015e32d5dSMike Smith } 168115e32d5dSMike Smith 168215e32d5dSMike Smith /* 168315e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 168415e32d5dSMike Smith * parts of the namespace. 168515e32d5dSMike Smith */ 168615e32d5dSMike Smith int 168715e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 168815e32d5dSMike Smith { 16893c600cfbSJohn Baldwin char *cp, *env, *np; 169015e32d5dSMike Smith int len; 169115e32d5dSMike Smith 169215e32d5dSMike Smith np = acpi_name(handle); 169315e32d5dSMike Smith if (*np == '\\') 169415e32d5dSMike Smith np++; 16953c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 169615e32d5dSMike Smith return (0); 169715e32d5dSMike Smith 1698be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 16993c600cfbSJohn Baldwin cp = env; 170015e32d5dSMike Smith for (;;) { 170115e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 170215e32d5dSMike Smith cp++; 170315e32d5dSMike Smith if (*cp == 0) 170415e32d5dSMike Smith break; 170515e32d5dSMike Smith len = 0; 170615e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 170715e32d5dSMike Smith len++; 1708d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 17093c600cfbSJohn Baldwin freeenv(env); 17100ae55423SMike Smith return(1); 1711d786139cSMaxime Henrion } 17120ae55423SMike Smith cp += len; 17130ae55423SMike Smith } 17143c600cfbSJohn Baldwin freeenv(env); 1715be2b1797SNate Lawson 17160ae55423SMike Smith return (0); 17170ae55423SMike Smith } 17180ae55423SMike Smith 17190ae55423SMike Smith /* 17200ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 17210ae55423SMike Smith */ 17220ae55423SMike Smith int 17230ae55423SMike Smith acpi_disabled(char *subsys) 17240ae55423SMike Smith { 172578689b15SMaxime Henrion char *cp, *env; 17260ae55423SMike Smith int len; 17270ae55423SMike Smith 172878689b15SMaxime Henrion if ((env = getenv("debug.acpi.disable")) == NULL) 17290ae55423SMike Smith return (0); 173078689b15SMaxime Henrion if (!strcmp(env, "all")) { 173178689b15SMaxime Henrion freeenv(env); 17320ae55423SMike Smith return (1); 1733d786139cSMaxime Henrion } 17340ae55423SMike Smith 17350ae55423SMike Smith /* scan the disable list checking for a match */ 173678689b15SMaxime Henrion cp = env; 17370ae55423SMike Smith for (;;) { 17380ae55423SMike Smith while ((*cp != 0) && isspace(*cp)) 17390ae55423SMike Smith cp++; 17400ae55423SMike Smith if (*cp == 0) 17410ae55423SMike Smith break; 17420ae55423SMike Smith len = 0; 17430ae55423SMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 17440ae55423SMike Smith len++; 1745d786139cSMaxime Henrion if (!strncmp(cp, subsys, len)) { 174678689b15SMaxime Henrion freeenv(env); 174715e32d5dSMike Smith return (1); 1748d786139cSMaxime Henrion } 174915e32d5dSMike Smith cp += len; 175015e32d5dSMike Smith } 175178689b15SMaxime Henrion freeenv(env); 1752be2b1797SNate Lawson 175315e32d5dSMike Smith return (0); 175415e32d5dSMike Smith } 175515e32d5dSMike Smith 175615e32d5dSMike Smith /* 1757a1fccb47SMitsuru IWASAKI * Device wake capability enable/disable. 1758a1fccb47SMitsuru IWASAKI */ 1759a1fccb47SMitsuru IWASAKI void 1760a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_capability(ACPI_HANDLE h, int enable) 1761a1fccb47SMitsuru IWASAKI { 1762a1fccb47SMitsuru IWASAKI ACPI_OBJECT_LIST ArgList; 1763a1fccb47SMitsuru IWASAKI ACPI_OBJECT Arg; 1764a1fccb47SMitsuru IWASAKI 1765a1fccb47SMitsuru IWASAKI /* 1766a1fccb47SMitsuru IWASAKI * TBD: All Power Resources referenced by elements 2 through N 1767a1fccb47SMitsuru IWASAKI * of the _PRW object are put into the ON state. 1768a1fccb47SMitsuru IWASAKI */ 1769a1fccb47SMitsuru IWASAKI 1770a1fccb47SMitsuru IWASAKI ArgList.Count = 1; 1771a1fccb47SMitsuru IWASAKI ArgList.Pointer = &Arg; 1772a1fccb47SMitsuru IWASAKI 1773a1fccb47SMitsuru IWASAKI Arg.Type = ACPI_TYPE_INTEGER; 1774a1fccb47SMitsuru IWASAKI Arg.Integer.Value = enable; 1775a1fccb47SMitsuru IWASAKI 1776a1fccb47SMitsuru IWASAKI (void)AcpiEvaluateObject(h, "_PSW", &ArgList, NULL); 1777a1fccb47SMitsuru IWASAKI } 1778a1fccb47SMitsuru IWASAKI 1779a1fccb47SMitsuru IWASAKI void 1780a1fccb47SMitsuru IWASAKI acpi_device_enable_wake_event(ACPI_HANDLE h) 1781a1fccb47SMitsuru IWASAKI { 1782a1fccb47SMitsuru IWASAKI struct acpi_softc *sc; 1783a1fccb47SMitsuru IWASAKI ACPI_STATUS status; 1784a1fccb47SMitsuru IWASAKI ACPI_BUFFER prw_buffer; 1785a1fccb47SMitsuru IWASAKI ACPI_OBJECT *res; 1786a1fccb47SMitsuru IWASAKI 1787a1fccb47SMitsuru IWASAKI ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1788a1fccb47SMitsuru IWASAKI 1789be2b1797SNate Lawson sc = devclass_get_softc(acpi_devclass, 0); 1790be2b1797SNate Lawson if (sc == NULL) 1791a1fccb47SMitsuru IWASAKI return; 1792a1fccb47SMitsuru IWASAKI 1793a1fccb47SMitsuru IWASAKI /* 1794a1fccb47SMitsuru IWASAKI * _PRW object is only required for devices that have the ability 1795a1fccb47SMitsuru IWASAKI * to wake the system from a system sleeping state. 1796a1fccb47SMitsuru IWASAKI */ 1797a1fccb47SMitsuru IWASAKI prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 1798a1fccb47SMitsuru IWASAKI status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 1799be2b1797SNate Lawson if (ACPI_FAILURE(status)) 1800a1fccb47SMitsuru IWASAKI return; 1801a1fccb47SMitsuru IWASAKI 1802a1fccb47SMitsuru IWASAKI res = (ACPI_OBJECT *)prw_buffer.Pointer; 1803be2b1797SNate Lawson if (res == NULL) 1804a1fccb47SMitsuru IWASAKI return; 1805a1fccb47SMitsuru IWASAKI 1806a1fccb47SMitsuru IWASAKI if ((res->Type != ACPI_TYPE_PACKAGE) || (res->Package.Count < 2)) { 1807a1fccb47SMitsuru IWASAKI goto out; 1808a1fccb47SMitsuru IWASAKI } 1809a1fccb47SMitsuru IWASAKI 1810a1fccb47SMitsuru IWASAKI /* 1811a1fccb47SMitsuru IWASAKI * The element 1 of the _PRW object: 1812a1fccb47SMitsuru IWASAKI * The lowest power system sleeping state that can be entered 1813a1fccb47SMitsuru IWASAKI * while still providing wake functionality. 1814a1fccb47SMitsuru IWASAKI * The sleeping state being entered must be greater or equal to 1815a1fccb47SMitsuru IWASAKI * the power state declared in element 1 of the _PRW object. 1816a1fccb47SMitsuru IWASAKI */ 1817be2b1797SNate Lawson if (res->Package.Elements[1].Type != ACPI_TYPE_INTEGER) 1818a1fccb47SMitsuru IWASAKI goto out; 1819a1fccb47SMitsuru IWASAKI 1820be2b1797SNate Lawson if (sc->acpi_sstate > res->Package.Elements[1].Integer.Value) 1821a1fccb47SMitsuru IWASAKI goto out; 1822a1fccb47SMitsuru IWASAKI 1823a1fccb47SMitsuru IWASAKI /* 1824a1fccb47SMitsuru IWASAKI * The element 0 of the _PRW object: 1825a1fccb47SMitsuru IWASAKI */ 1826a1fccb47SMitsuru IWASAKI switch(res->Package.Elements[0].Type) { 1827a1fccb47SMitsuru IWASAKI case ACPI_TYPE_INTEGER: 1828a1fccb47SMitsuru IWASAKI /* 1829a1fccb47SMitsuru IWASAKI * If the data type of this package element is numeric, then this 1830a1fccb47SMitsuru IWASAKI * _PRW package element is the bit index in the GPEx_EN, in the 1831a1fccb47SMitsuru IWASAKI * GPE blocks described in the FADT, of the enable bit that is 1832a1fccb47SMitsuru IWASAKI * enabled for the wake event. 1833a1fccb47SMitsuru IWASAKI */ 1834a1fccb47SMitsuru IWASAKI 18356fca9360SNate Lawson status = AcpiEnableGpe(NULL, res->Package.Elements[0].Integer.Value, 18366fca9360SNate Lawson ACPI_EVENT_WAKE_ENABLE); 1837a1fccb47SMitsuru IWASAKI if (ACPI_FAILURE(status)) 1838a1fccb47SMitsuru IWASAKI printf("%s: EnableEvent Failed\n", __func__); 1839a1fccb47SMitsuru IWASAKI break; 1840a1fccb47SMitsuru IWASAKI case ACPI_TYPE_PACKAGE: 1841a1fccb47SMitsuru IWASAKI /* 1842be2b1797SNate Lawson * XXX TBD 1843be2b1797SNate Lawson * 1844a1fccb47SMitsuru IWASAKI * If the data type of this package element is a package, then this 1845a1fccb47SMitsuru IWASAKI * _PRW package element is itself a package containing two 1846a1fccb47SMitsuru IWASAKI * elements. The first is an object reference to the GPE Block 1847a1fccb47SMitsuru IWASAKI * device that contains the GPE that will be triggered by the wake 1848a1fccb47SMitsuru IWASAKI * event. The second element is numeric and it contains the bit 1849a1fccb47SMitsuru IWASAKI * index in the GPEx_EN, in the GPE Block referenced by the 1850a1fccb47SMitsuru IWASAKI * first element in the package, of the enable bit that is enabled for 1851a1fccb47SMitsuru IWASAKI * the wake event. 1852a1fccb47SMitsuru IWASAKI * For example, if this field is a package then it is of the form: 1853a1fccb47SMitsuru IWASAKI * Package() {\_SB.PCI0.ISA.GPE, 2} 1854a1fccb47SMitsuru IWASAKI */ 1855a1fccb47SMitsuru IWASAKI break; 1856a1fccb47SMitsuru IWASAKI default: 1857a1fccb47SMitsuru IWASAKI break; 1858a1fccb47SMitsuru IWASAKI } 1859a1fccb47SMitsuru IWASAKI 1860a1fccb47SMitsuru IWASAKI out: 1861a1fccb47SMitsuru IWASAKI if (prw_buffer.Pointer != NULL) 1862a1fccb47SMitsuru IWASAKI AcpiOsFree(prw_buffer.Pointer); 1863a1fccb47SMitsuru IWASAKI } 1864a1fccb47SMitsuru IWASAKI 1865a1fccb47SMitsuru IWASAKI /* 186615e32d5dSMike Smith * Control interface. 186715e32d5dSMike Smith * 18680ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 1869be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 1870be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 187115e32d5dSMike Smith */ 18720ae55423SMike Smith struct acpi_ioctl_hook 18730ae55423SMike Smith { 18740ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 18750ae55423SMike Smith u_long cmd; 1876be2b1797SNate Lawson acpi_ioctl_fn fn; 18770ae55423SMike Smith void *arg; 18780ae55423SMike Smith }; 18790ae55423SMike Smith 18800ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 18810ae55423SMike Smith static int acpi_ioctl_hooks_initted; 18820ae55423SMike Smith 18830ae55423SMike Smith /* 18840ae55423SMike Smith * Register an ioctl handler. 18850ae55423SMike Smith */ 18860ae55423SMike Smith int 1887be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 18880ae55423SMike Smith { 18890ae55423SMike Smith struct acpi_ioctl_hook *hp; 18900ae55423SMike Smith 18910ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 18920ae55423SMike Smith return (ENOMEM); 18930ae55423SMike Smith hp->cmd = cmd; 18940ae55423SMike Smith hp->fn = fn; 18950ae55423SMike Smith hp->arg = arg; 18960ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 18970ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 18980ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 18990ae55423SMike Smith } 19000ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 19010ae55423SMike Smith return (0); 19020ae55423SMike Smith } 19030ae55423SMike Smith 19040ae55423SMike Smith /* 19050ae55423SMike Smith * Deregister an ioctl handler. 19060ae55423SMike Smith */ 19070ae55423SMike Smith void 1908be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 19090ae55423SMike Smith { 19100ae55423SMike Smith struct acpi_ioctl_hook *hp; 19110ae55423SMike Smith 19120ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 19130ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 19140ae55423SMike Smith break; 19150ae55423SMike Smith 19160ae55423SMike Smith if (hp != NULL) { 19170ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 19180ae55423SMike Smith free(hp, M_ACPIDEV); 19190ae55423SMike Smith } 19200ae55423SMike Smith } 19210ae55423SMike Smith 192215e32d5dSMike Smith static int 19236b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 192415e32d5dSMike Smith { 192515e32d5dSMike Smith return (0); 192615e32d5dSMike Smith } 192715e32d5dSMike Smith 192815e32d5dSMike Smith static int 19296b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 193015e32d5dSMike Smith { 193115e32d5dSMike Smith return (0); 193215e32d5dSMike Smith } 193315e32d5dSMike Smith 193415e32d5dSMike Smith static int 19356b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 193615e32d5dSMike Smith { 193715e32d5dSMike Smith struct acpi_softc *sc; 19380ae55423SMike Smith struct acpi_ioctl_hook *hp; 19390ae55423SMike Smith int error, xerror, state; 1940fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 194115e32d5dSMike Smith 1942cb9b0d80SMike Smith ACPI_LOCK; 1943cb9b0d80SMike Smith 194415e32d5dSMike Smith error = state = 0; 194515e32d5dSMike Smith sc = dev->si_drv1; 194615e32d5dSMike Smith 19470ae55423SMike Smith /* 19480ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 19490ae55423SMike Smith */ 19500ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 19510ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 19520ae55423SMike Smith if (hp->cmd == cmd) { 19530ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 19540ae55423SMike Smith if (xerror != 0) 19550ae55423SMike Smith error = xerror; 1956917d44c8SMitsuru IWASAKI goto out; 19570ae55423SMike Smith } 19580ae55423SMike Smith } 19590ae55423SMike Smith } 19600ae55423SMike Smith 19610ae55423SMike Smith /* 1962a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 1963a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 1964a89fcf28STakanori Watanabe * Not changing system behavior. 1965a89fcf28STakanori Watanabe */ 1966be2b1797SNate Lawson if((flag & FWRITE) == 0) 1967be2b1797SNate Lawson return (EPERM); 1968a89fcf28STakanori Watanabe 1969be2b1797SNate Lawson /* Core system ioctls. */ 197015e32d5dSMike Smith switch (cmd) { 197115e32d5dSMike Smith case ACPIIO_ENABLE: 19720ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 197315e32d5dSMike Smith error = ENXIO; 197415e32d5dSMike Smith break; 197515e32d5dSMike Smith case ACPIIO_DISABLE: 19760ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 197715e32d5dSMike Smith error = ENXIO; 197815e32d5dSMike Smith break; 197915e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 198015e32d5dSMike Smith if (!sc->acpi_enabled) { 198115e32d5dSMike Smith error = ENXIO; 198215e32d5dSMike Smith break; 198315e32d5dSMike Smith } 198415e32d5dSMike Smith state = *(int *)addr; 19852d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 19862d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 198715e32d5dSMike Smith error = EINVAL; 19882d610c46SNate Lawson } else { 19892d610c46SNate Lawson error = EINVAL; 19902d610c46SNate Lawson } 199115e32d5dSMike Smith break; 199215e32d5dSMike Smith default: 19930ae55423SMike Smith if (error == 0) 199415e32d5dSMike Smith error = EINVAL; 199515e32d5dSMike Smith break; 199615e32d5dSMike Smith } 1997917d44c8SMitsuru IWASAKI 1998917d44c8SMitsuru IWASAKI out: 1999cb9b0d80SMike Smith ACPI_UNLOCK; 200015e32d5dSMike Smith return (error); 200115e32d5dSMike Smith } 200215e32d5dSMike Smith 20031d073b1dSJohn Baldwin static int 2004d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2005d75de536SMitsuru IWASAKI { 2006d75de536SMitsuru IWASAKI char sleep_state[4]; 2007d75de536SMitsuru IWASAKI char buf[16]; 2008d75de536SMitsuru IWASAKI int error; 2009d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2010d75de536SMitsuru IWASAKI 2011d75de536SMitsuru IWASAKI buf[0] = '\0'; 2012d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX+1; state++) { 2013d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2014d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2015d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2016d75de536SMitsuru IWASAKI } 2017d75de536SMitsuru IWASAKI } 2018d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2019d75de536SMitsuru IWASAKI return (error); 2020d75de536SMitsuru IWASAKI } 2021d75de536SMitsuru IWASAKI 2022d75de536SMitsuru IWASAKI static int 20231d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 20241d073b1dSJohn Baldwin { 20251d073b1dSJohn Baldwin char sleep_state[10]; 20261d073b1dSJohn Baldwin int error; 20271d073b1dSJohn Baldwin u_int new_state, old_state; 20281d073b1dSJohn Baldwin 20291d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2030b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX+1) { 20311d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2032cb9b0d80SMike Smith } else { 2033b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 20341d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 20351d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2036cb9b0d80SMike Smith } 20371d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 20381d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2039be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2040be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 20411d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 20421d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 20431d073b1dSJohn Baldwin break; 2044cb9b0d80SMike Smith } 2045931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2046be2b1797SNate Lawson if (new_state != old_state) 20471d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2048cb9b0d80SMike Smith } else { 20491d073b1dSJohn Baldwin error = EINVAL; 20501d073b1dSJohn Baldwin } 2051cb9b0d80SMike Smith } 2052be2b1797SNate Lawson 20531d073b1dSJohn Baldwin return (error); 20541d073b1dSJohn Baldwin } 20551d073b1dSJohn Baldwin 205615e32d5dSMike Smith #ifdef ACPI_DEBUG 20570ae55423SMike Smith /* 20580ae55423SMike Smith * Support for parsing debug options from the kernel environment. 20590ae55423SMike Smith * 20600ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 20610ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 20620ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 20630ae55423SMike Smith * prefixing the bit name with !. 20640ae55423SMike Smith */ 206515e32d5dSMike Smith struct debugtag 206615e32d5dSMike Smith { 206715e32d5dSMike Smith char *name; 206815e32d5dSMike Smith UINT32 value; 206915e32d5dSMike Smith }; 207015e32d5dSMike Smith 207115e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2072c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2073c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2074c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2075c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2076c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2077c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2078c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2079c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2080c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2081d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 20824c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2083d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2084297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 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}, 2096b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 209715e32d5dSMike Smith {NULL, 0} 209815e32d5dSMike Smith }; 209915e32d5dSMike Smith 210015e32d5dSMike Smith static struct debugtag dbg_level[] = { 21014c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 21029501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 21039501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 21044c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 21059501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 21064c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2107d62ab2f4SMitsuru IWASAKI 2108d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2109297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 21104c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 21114c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2112d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 21134c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 21144c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 21154c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 21164c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 21174c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 21184c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 21194c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 21204c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 21214c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 21224c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2123d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2124d62ab2f4SMitsuru IWASAKI 2125d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2126d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2127d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2128d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2129d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 21304c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2131d62ab2f4SMitsuru IWASAKI 2132d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2133d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2134d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2135d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2136d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2137d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2138d62ab2f4SMitsuru IWASAKI 2139d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 214098479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 214198479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 214298479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 214398479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 214498479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 214515e32d5dSMike Smith {NULL, 0} 214615e32d5dSMike Smith }; 214715e32d5dSMike Smith 214815e32d5dSMike Smith static void 214915e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 215015e32d5dSMike Smith { 215115e32d5dSMike Smith char *ep; 215215e32d5dSMike Smith int i, l; 21530ae55423SMike Smith int set; 215415e32d5dSMike Smith 215515e32d5dSMike Smith while (*cp) { 215615e32d5dSMike Smith if (isspace(*cp)) { 215715e32d5dSMike Smith cp++; 215815e32d5dSMike Smith continue; 215915e32d5dSMike Smith } 216015e32d5dSMike Smith ep = cp; 216115e32d5dSMike Smith while (*ep && !isspace(*ep)) 216215e32d5dSMike Smith ep++; 21630ae55423SMike Smith if (*cp == '!') { 21640ae55423SMike Smith set = 0; 21650ae55423SMike Smith cp++; 21660ae55423SMike Smith if (cp == ep) 21670ae55423SMike Smith continue; 21680ae55423SMike Smith } else { 21690ae55423SMike Smith set = 1; 21700ae55423SMike Smith } 217115e32d5dSMike Smith l = ep - cp; 217215e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 217315e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2174be2b1797SNate Lawson if (set) 217515e32d5dSMike Smith *flag |= tag[i].value; 2176be2b1797SNate Lawson else 21770ae55423SMike Smith *flag &= ~tag[i].value; 217815e32d5dSMike Smith printf("ACPI_DEBUG: set '%s'\n", tag[i].name); 217915e32d5dSMike Smith } 218015e32d5dSMike Smith } 218115e32d5dSMike Smith cp = ep; 218215e32d5dSMike Smith } 218315e32d5dSMike Smith } 218415e32d5dSMike Smith 218515e32d5dSMike Smith static void 21862a4ac806SMike Smith acpi_set_debugging(void *junk) 218715e32d5dSMike Smith { 218894aae282SMike Barcroft char *cp; 218915e32d5dSMike Smith 21901d7b121cSNate Lawson if (cold) { 21910ae55423SMike Smith AcpiDbgLayer = 0; 21920ae55423SMike Smith AcpiDbgLevel = 0; 21931d7b121cSNate Lawson } 21941d7b121cSNate Lawson 219594aae282SMike Barcroft if ((cp = getenv("debug.acpi.layer")) != NULL) { 219615e32d5dSMike Smith acpi_parse_debug(cp, &dbg_layer[0], &AcpiDbgLayer); 219794aae282SMike Barcroft freeenv(cp); 219894aae282SMike Barcroft } 219994aae282SMike Barcroft if ((cp = getenv("debug.acpi.level")) != NULL) { 220015e32d5dSMike Smith acpi_parse_debug(cp, &dbg_level[0], &AcpiDbgLevel); 220194aae282SMike Barcroft freeenv(cp); 220294aae282SMike Barcroft } 22030ae55423SMike Smith 22041d7b121cSNate Lawson if (cold) { 22051d7b121cSNate Lawson printf("ACPI debug layer 0x%x debug level 0x%x\n", 22061d7b121cSNate Lawson AcpiDbgLayer, AcpiDbgLevel); 22071d7b121cSNate Lawson } 220815e32d5dSMike Smith } 2209be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2210be2b1797SNate Lawson NULL); 22111d7b121cSNate Lawson 22121d7b121cSNate Lawson static int 22131d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 22141d7b121cSNate Lawson { 22151d7b121cSNate Lawson char *options; 22161d7b121cSNate Lawson int error, len, *dbg; 22171d7b121cSNate Lawson struct debugtag *tag; 22181d7b121cSNate Lawson 22191d7b121cSNate Lawson len = 512; 22201d7b121cSNate Lawson MALLOC(options, char *, len, M_TEMP, M_WAITOK); 22211d7b121cSNate Lawson options[0] = '\0'; 22221d7b121cSNate Lawson 22231d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 22241d7b121cSNate Lawson tag = &dbg_layer[0]; 22251d7b121cSNate Lawson dbg = &AcpiDbgLayer; 22261d7b121cSNate Lawson } else { 22271d7b121cSNate Lawson tag = &dbg_level[0]; 22281d7b121cSNate Lawson dbg = &AcpiDbgLevel; 22291d7b121cSNate Lawson } 22301d7b121cSNate Lawson 22311d7b121cSNate Lawson /* Get old values if this is a get request. */ 22321d7b121cSNate Lawson if (*dbg == 0) { 22331d7b121cSNate Lawson strlcpy(options, "NONE", sizeof(options)); 22341d7b121cSNate Lawson } else if (req->newptr == NULL) { 22351d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 22361d7b121cSNate Lawson if ((*dbg & tag->value) == tag->value) { 22371d7b121cSNate Lawson strlcat(options, tag->name, len); 22381d7b121cSNate Lawson strlcat(options, " ", len); /* XXX */ 22391d7b121cSNate Lawson } 22401d7b121cSNate Lawson } 22411d7b121cSNate Lawson } 22421d7b121cSNate Lawson 22431d7b121cSNate Lawson error = sysctl_handle_string(oidp, options, len, req); 22441d7b121cSNate Lawson 22451d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 22461d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 22471d7b121cSNate Lawson *dbg = 0; 22481d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 22491d7b121cSNate Lawson acpi_set_debugging(NULL); 22501d7b121cSNate Lawson } 22511d7b121cSNate Lawson FREE(options, M_TEMP); 22521d7b121cSNate Lawson 22531d7b121cSNate Lawson return (error); 22541d7b121cSNate Lawson } 22551d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 22561d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 22571d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 22581d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 225915e32d5dSMike Smith #endif 2260f9390180SMitsuru IWASAKI 2261f9390180SMitsuru IWASAKI static int 2262f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2263f9390180SMitsuru IWASAKI { 2264f9390180SMitsuru IWASAKI int state, acpi_state; 2265f9390180SMitsuru IWASAKI int error; 2266f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2267f9390180SMitsuru IWASAKI va_list ap; 2268f9390180SMitsuru IWASAKI 2269f9390180SMitsuru IWASAKI error = 0; 2270f9390180SMitsuru IWASAKI switch (cmd) { 2271f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2272f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2273f9390180SMitsuru IWASAKI if (sc == NULL) { 2274f9390180SMitsuru IWASAKI error = EINVAL; 2275f9390180SMitsuru IWASAKI goto out; 2276f9390180SMitsuru IWASAKI } 2277f9390180SMitsuru IWASAKI 2278f9390180SMitsuru IWASAKI va_start(ap, arg); 2279f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2280f9390180SMitsuru IWASAKI va_end(ap); 2281f9390180SMitsuru IWASAKI 2282f9390180SMitsuru IWASAKI switch (state) { 2283f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2284f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2285f9390180SMitsuru IWASAKI break; 2286f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2287f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2288f9390180SMitsuru IWASAKI break; 2289f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2290f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2291f9390180SMitsuru IWASAKI break; 2292f9390180SMitsuru IWASAKI default: 2293f9390180SMitsuru IWASAKI error = EINVAL; 2294f9390180SMitsuru IWASAKI goto out; 2295f9390180SMitsuru IWASAKI } 2296f9390180SMitsuru IWASAKI 2297f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2298f9390180SMitsuru IWASAKI break; 2299f9390180SMitsuru IWASAKI default: 2300f9390180SMitsuru IWASAKI error = EINVAL; 2301f9390180SMitsuru IWASAKI goto out; 2302f9390180SMitsuru IWASAKI } 2303f9390180SMitsuru IWASAKI 2304f9390180SMitsuru IWASAKI out: 2305f9390180SMitsuru IWASAKI return (error); 2306f9390180SMitsuru IWASAKI } 2307f9390180SMitsuru IWASAKI 2308f9390180SMitsuru IWASAKI static void 2309f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2310f9390180SMitsuru IWASAKI { 2311be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 23126c407052SMitsuru IWASAKI return; 2313f9390180SMitsuru IWASAKI 2314f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2315f9390180SMitsuru IWASAKI } 2316f9390180SMitsuru IWASAKI 2317f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2318