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> 38fe12f24bSPoul-Henning Kamp #include <sys/module.h> 3915e32d5dSMike Smith #include <sys/bus.h> 4015e32d5dSMike Smith #include <sys/conf.h> 4115e32d5dSMike Smith #include <sys/ioccom.h> 4215e32d5dSMike Smith #include <sys/reboot.h> 4315e32d5dSMike Smith #include <sys/sysctl.h> 4415e32d5dSMike Smith #include <sys/ctype.h> 451611ea87SMitsuru IWASAKI #include <sys/linker.h> 46f9390180SMitsuru IWASAKI #include <sys/power.h> 4754f6bca0SNate Lawson #include <sys/sbuf.h> 48eeb3a05fSNate Lawson #include <sys/smp.h> 4915e32d5dSMike Smith 5015e32d5dSMike Smith #include <machine/clock.h> 5115e32d5dSMike Smith #include <machine/resource.h> 52dc750869SNate Lawson #include <machine/bus.h> 53dc750869SNate Lawson #include <sys/rman.h> 54bc0f2195SMike Smith #include <isa/isavar.h> 55bc0f2195SMike Smith 5615e32d5dSMike Smith #include "acpi.h" 5715e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5815e32d5dSMike Smith #include <dev/acpica/acpiio.h> 599b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h> 6015e32d5dSMike Smith 6115e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 6215e32d5dSMike Smith 63be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 64cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 65b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 660ae55423SMike Smith 6715e32d5dSMike Smith static d_open_t acpiopen; 6815e32d5dSMike Smith static d_close_t acpiclose; 6915e32d5dSMike Smith static d_ioctl_t acpiioctl; 7015e32d5dSMike Smith 7115e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 72dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 73dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 747ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 757ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 767ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 777ac40f5fSPoul-Henning Kamp .d_name = "acpi", 7815e32d5dSMike Smith }; 7915e32d5dSMike Smith 80fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 81cb9b0d80SMike Smith struct mtx acpi_mutex; 82fc0ea94aSJohn Baldwin #endif 83cb9b0d80SMike Smith 843184cf5aSNate Lawson struct acpi_quirks { 853184cf5aSNate Lawson char *OemId; 863184cf5aSNate Lawson uint32_t OemRevision; 873184cf5aSNate Lawson char *value; 883184cf5aSNate Lawson }; 893184cf5aSNate Lawson 903184cf5aSNate Lawson #define ACPI_OEM_REV_ANY 0 913184cf5aSNate Lawson 923184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = { 933184cf5aSNate Lawson #ifdef notyet 943184cf5aSNate Lawson /* Bad PCI routing table. Used on some SuperMicro boards. */ 953184cf5aSNate Lawson { "PTLTD ", 0x06040000, "pci_link" }, 963184cf5aSNate Lawson #endif 973184cf5aSNate Lawson 983184cf5aSNate Lawson { NULL, 0, NULL } 993184cf5aSNate Lawson }; 1003184cf5aSNate Lawson 1016d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10215e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 10315e32d5dSMike Smith static int acpi_probe(device_t dev); 10415e32d5dSMike Smith static int acpi_attach(device_t dev); 105169b539aSNate Lawson static int acpi_shutdown(device_t dev); 1063184cf5aSNate Lawson static void acpi_quirks_set(void); 107be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 108be2b1797SNate Lawson int unit); 10915e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 110be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 111be2b1797SNate Lawson uintptr_t *result); 112be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 113be2b1797SNate Lawson uintptr_t value); 114be2b1797SNate Lawson static int acpi_set_resource(device_t dev, device_t child, int type, 115be2b1797SNate Lawson int rid, u_long start, u_long count); 116be2b1797SNate Lawson static int acpi_get_resource(device_t dev, device_t child, int type, 117be2b1797SNate Lawson int rid, u_long *startp, u_long *countp); 118be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 119be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 120be2b1797SNate Lawson u_long count, u_int flags); 121be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 122be2b1797SNate Lawson int rid, struct resource *r); 1231e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1241e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 125be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 126be2b1797SNate Lawson struct isa_pnp_id *ids); 12715e32d5dSMike Smith static void acpi_probe_children(device_t bus); 128be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 129be2b1797SNate Lawson void *context, void **status); 13015e32d5dSMike Smith static void acpi_shutdown_pre_sync(void *arg, int howto); 13115e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 132eeb3a05fSNate Lawson static void acpi_shutdown_poweroff(void *arg); 13313d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 134e8b4d56eSNate Lawson static int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw); 135e8b4d56eSNate Lawson static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, 136e8b4d56eSNate Lawson void **status); 137e8b4d56eSNate Lawson static int acpi_wake_limit_walk(int sstate); 13888a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 13988a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 14015e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 14115e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 142d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1431d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 144f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 145879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 146879d6c23STakanori Watanabe char *buf, size_t buflen); 147879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 148879d6c23STakanori Watanabe char *buf, size_t buflen); 149879d6c23STakanori Watanabe 15015e32d5dSMike Smith static device_method_t acpi_methods[] = { 15115e32d5dSMike Smith /* Device interface */ 15215e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 15315e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 15415e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 155169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 15656a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 15715e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 15815e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 15915e32d5dSMike Smith 16015e32d5dSMike Smith /* Bus interface */ 16115e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 16215e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 16315e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 16415e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 16515e32d5dSMike Smith DEVMETHOD(bus_set_resource, acpi_set_resource), 16615e32d5dSMike Smith DEVMETHOD(bus_get_resource, acpi_get_resource), 16715e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 16815e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 169879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 170879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 17115e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 17215e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 17315e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 17415e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 17515e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 17615e32d5dSMike Smith 177bc0f2195SMike Smith /* ISA emulation */ 178bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 179bc0f2195SMike Smith 18015e32d5dSMike Smith {0, 0} 18115e32d5dSMike Smith }; 18215e32d5dSMike Smith 18315e32d5dSMike Smith static driver_t acpi_driver = { 18415e32d5dSMike Smith "acpi", 18515e32d5dSMike Smith acpi_methods, 18615e32d5dSMike Smith sizeof(struct acpi_softc), 18715e32d5dSMike Smith }; 18815e32d5dSMike Smith 1893273b005SMike Smith static devclass_t acpi_devclass; 1906d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 191a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 19215e32d5dSMike Smith 193865b8d0bSNate Lawson static const char* sleep_state_names[] = { 194865b8d0bSNate Lawson "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 195865b8d0bSNate Lawson 1961d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 1971d7b121cSNate Lawson static char acpi_ca_version[12]; 1981d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 1991d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 20015e32d5dSMike Smith 20115e32d5dSMike Smith /* 202413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 203c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 204c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 205c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 206c9b8d77dSNate Lawson * some IBMs use such code. 207413081d7SNate Lawson */ 208c9b8d77dSNate Lawson static int acpi_serialize_methods; 209413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 210413081d7SNate Lawson 211c9b8d77dSNate Lawson /* 2126d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2136d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 214be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 215be2b1797SNate Lawson * off it. 2166d63101aSMike Smith */ 2176d63101aSMike Smith static int 2186d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2196d63101aSMike Smith { 2206d63101aSMike Smith switch(event) { 2216d63101aSMike Smith case MOD_LOAD: 22287b45ed5SMitsuru IWASAKI if (!cold) { 22387b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2246d63101aSMike Smith return (EPERM); 22587b45ed5SMitsuru IWASAKI } 2266d63101aSMike Smith break; 2276d63101aSMike Smith case MOD_UNLOAD: 228f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2296d63101aSMike Smith return (EBUSY); 230f9390180SMitsuru IWASAKI break; 2316d63101aSMike Smith default: 2326d63101aSMike Smith break; 2336d63101aSMike Smith } 2346d63101aSMike Smith return (0); 2356d63101aSMike Smith } 2366d63101aSMike Smith 2376d63101aSMike Smith /* 238bbc2815cSJohn Baldwin * Perform early initialization. 23915e32d5dSMike Smith */ 240bbc2815cSJohn Baldwin ACPI_STATUS 241bbc2815cSJohn Baldwin acpi_Startup(void) 24215e32d5dSMike Smith { 243d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 244d786139cSMaxime Henrion char *debugpoint; 24515e32d5dSMike Smith #endif 246bbc2815cSJohn Baldwin static int error, started = 0; 24715e32d5dSMike Smith 2481b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2491b8c233dSPeter Pentchev 250bbc2815cSJohn Baldwin if (started) 251bbc2815cSJohn Baldwin return_VALUE (error); 252bbc2815cSJohn Baldwin started = 1; 25315e32d5dSMike Smith 254fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 255be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2566008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 257fc0ea94aSJohn Baldwin #endif 258cb9b0d80SMike Smith 259413081d7SNate Lawson /* 260413081d7SNate Lawson * Set the globals from our tunables. This is needed because ACPI-CA 261f3fc4f8bSNate Lawson * uses UINT8 for some values and we have no tunable_byte. 262413081d7SNate Lawson */ 263f3fc4f8bSNate Lawson AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods; 264413081d7SNate Lawson 265be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 266d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 267d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 268d786139cSMaxime Henrion if (debugpoint) { 269d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 27015e32d5dSMike Smith acpi_EnterDebugger(); 271d786139cSMaxime Henrion freeenv(debugpoint); 272d786139cSMaxime Henrion } 27315e32d5dSMike Smith #endif 274b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 275bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 276bbc2815cSJohn Baldwin return_VALUE (error); 27715e32d5dSMike Smith } 278d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 279d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 280d786139cSMaxime Henrion if (debugpoint) { 281d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 28215e32d5dSMike Smith acpi_EnterDebugger(); 283d786139cSMaxime Henrion freeenv(debugpoint); 284d786139cSMaxime Henrion } 28515e32d5dSMike Smith #endif 2861611ea87SMitsuru IWASAKI 287b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 288bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 289bbc2815cSJohn Baldwin return_VALUE(error); 29015e32d5dSMike Smith } 2913184cf5aSNate Lawson 2923184cf5aSNate Lawson /* Set up any quirks we have for this XSDT. */ 2933184cf5aSNate Lawson acpi_quirks_set(); 2944e376d58SNate Lawson if (acpi_disabled("acpi")) 2954e376d58SNate Lawson return_VALUE (AE_ERROR); 2963184cf5aSNate Lawson 297bbc2815cSJohn Baldwin return_VALUE (AE_OK); 298bbc2815cSJohn Baldwin } 299bbc2815cSJohn Baldwin 300bbc2815cSJohn Baldwin /* 301bbc2815cSJohn Baldwin * Detect ACPI, perform early initialisation 302bbc2815cSJohn Baldwin */ 303bbc2815cSJohn Baldwin static void 304bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent) 305bbc2815cSJohn Baldwin { 306bbc2815cSJohn Baldwin device_t child; 307bbc2815cSJohn Baldwin 308bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 309bbc2815cSJohn Baldwin 310bbc2815cSJohn Baldwin if (!cold) 311bbc2815cSJohn Baldwin return_VOID; 312bbc2815cSJohn Baldwin 313bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 314bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 315bbc2815cSJohn Baldwin return_VOID; 316bbc2815cSJohn Baldwin 317bbc2815cSJohn Baldwin /* Make sure we're not being doubly invoked. */ 318bbc2815cSJohn Baldwin if (device_find_child(parent, "acpi", 0) != NULL) 319bbc2815cSJohn Baldwin return_VOID; 320bbc2815cSJohn Baldwin 321bbc2815cSJohn Baldwin /* Initialize ACPI-CA. */ 322bbc2815cSJohn Baldwin if (ACPI_FAILURE(acpi_Startup())) 323bbc2815cSJohn Baldwin return_VOID; 32415e32d5dSMike Smith 3254e376d58SNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION); 3264e376d58SNate Lawson 327be2b1797SNate Lawson /* Attach the actual ACPI device. */ 32815e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 32915e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 3300ae55423SMike Smith return_VOID; 33115e32d5dSMike Smith } 33215e32d5dSMike Smith } 33315e32d5dSMike Smith 33415e32d5dSMike Smith /* 33515e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 33615e32d5dSMike Smith */ 33715e32d5dSMike Smith static int 33815e32d5dSMike Smith acpi_probe(device_t dev) 33915e32d5dSMike Smith { 34015e32d5dSMike Smith ACPI_TABLE_HEADER th; 34115e32d5dSMike Smith char buf[20]; 34215e32d5dSMike Smith int error; 3432ccd1cacSNate Lawson struct sbuf sb; 3442ccd1cacSNate Lawson ACPI_STATUS status; 345fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 34615e32d5dSMike Smith 347b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 348f9390180SMitsuru IWASAKI 349f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 350f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 351be2b1797SNate Lawson 352f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 353f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 354f9390180SMitsuru IWASAKI } 355f9390180SMitsuru IWASAKI 356cb9b0d80SMike Smith ACPI_LOCK; 3570ae55423SMike Smith 358b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 359be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 360be2b1797SNate Lawson AcpiFormatException(status)); 361cb9b0d80SMike Smith error = ENXIO; 362cb9b0d80SMike Smith } else { 3632ccd1cacSNate Lawson sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 3642ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemId, 6); 3652ccd1cacSNate Lawson sbuf_trim(&sb); 3662ccd1cacSNate Lawson sbuf_putc(&sb, ' '); 3672ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemTableId, 8); 3682ccd1cacSNate Lawson sbuf_trim(&sb); 3692ccd1cacSNate Lawson sbuf_finish(&sb); 3702ccd1cacSNate Lawson device_set_desc_copy(dev, sbuf_data(&sb)); 3712ccd1cacSNate Lawson sbuf_delete(&sb); 372cb9b0d80SMike Smith error = 0; 373cb9b0d80SMike Smith } 374cb9b0d80SMike Smith ACPI_UNLOCK; 375cb9b0d80SMike Smith return_VALUE(error); 37615e32d5dSMike Smith } 37715e32d5dSMike Smith 37815e32d5dSMike Smith static int 37915e32d5dSMike Smith acpi_attach(device_t dev) 38015e32d5dSMike Smith { 38115e32d5dSMike Smith struct acpi_softc *sc; 382cb9b0d80SMike Smith ACPI_STATUS status; 383ea27c63eSNate Lawson int error, state; 38432d18aa5SMike Smith UINT32 flags; 385ea27c63eSNate Lawson UINT8 TypeA, TypeB; 386498d464fSMitsuru IWASAKI char *env; 387d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 388d786139cSMaxime Henrion char *debugpoint; 38915e32d5dSMike Smith #endif 390fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 39115e32d5dSMike Smith 392b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 393cb9b0d80SMike Smith ACPI_LOCK; 39415e32d5dSMike Smith sc = device_get_softc(dev); 39515e32d5dSMike Smith bzero(sc, sizeof(*sc)); 39615e32d5dSMike Smith sc->acpi_dev = dev; 39715e32d5dSMike Smith 398d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 399d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 400d786139cSMaxime Henrion if (debugpoint) { 401d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 40215e32d5dSMike Smith acpi_EnterDebugger(); 403d786139cSMaxime Henrion freeenv(debugpoint); 404d786139cSMaxime Henrion } 40515e32d5dSMike Smith #endif 40615e32d5dSMike Smith 407be2b1797SNate Lawson /* Install the default address space handlers. */ 408cb9b0d80SMike Smith error = ENXIO; 409be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 410be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 411be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 412be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 413be2b1797SNate Lawson AcpiFormatException(status)); 414cb9b0d80SMike Smith goto out; 41515e32d5dSMike Smith } 416be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 417be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 418be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 419be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 420be2b1797SNate Lawson AcpiFormatException(status)); 421cb9b0d80SMike Smith goto out; 42215e32d5dSMike Smith } 423be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 424be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 425be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 426be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 427be2b1797SNate Lawson AcpiFormatException(status)); 428cb9b0d80SMike Smith goto out; 42915e32d5dSMike Smith } 43015e32d5dSMike Smith 43115e32d5dSMike Smith /* 43215e32d5dSMike Smith * Bring ACPI fully online. 43315e32d5dSMike Smith * 434be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 435be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 436be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 437be2b1797SNate Lawson * object init pass. 43815e32d5dSMike Smith * 43932d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 44032d18aa5SMike Smith * 441be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 442be2b1797SNate Lawson * all our child devices, but on many systems it works here. 44315e32d5dSMike Smith */ 444d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 445d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 446d786139cSMaxime Henrion if (debugpoint) { 447d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 44815e32d5dSMike Smith acpi_EnterDebugger(); 449d786139cSMaxime Henrion freeenv(debugpoint); 450d786139cSMaxime Henrion } 45115e32d5dSMike Smith #endif 45232d18aa5SMike Smith flags = 0; 453d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 45432d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 455b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 456be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 457be2b1797SNate Lawson AcpiFormatException(status)); 458cb9b0d80SMike Smith goto out; 45915e32d5dSMike Smith } 46015e32d5dSMike Smith 461f8335e3aSNate Lawson /* 462f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 463f8335e3aSNate Lawson * the namespace has been evaluated. 464f8335e3aSNate Lawson */ 465f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 466f8335e3aSNate Lawson 467b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 468be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 469be2b1797SNate Lawson AcpiFormatException(status)); 470b69ed3f4SMitsuru IWASAKI goto out; 471b69ed3f4SMitsuru IWASAKI } 472b69ed3f4SMitsuru IWASAKI 47315e32d5dSMike Smith /* 4741d073b1dSJohn Baldwin * Setup our sysctl tree. 4751d073b1dSJohn Baldwin * 4761d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4771d073b1dSJohn Baldwin */ 4781d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4791d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4801d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4811d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 4821d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 483d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 484d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 485d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4861d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4871d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4881d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4891d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4901d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4911d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4921d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4931d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 494f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 495f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 496f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 497f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 498f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 499f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5002d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 501ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 502ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 503ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5041611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 5051611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 5061611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5072d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 5082d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 509c6a78e98STakanori Watanabe SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 510c6a78e98STakanori Watanabe OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 511c6a78e98STakanori Watanabe &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 512bf0c18ecSNate Lawson 513bf0c18ecSNate Lawson /* 5142b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 515bf0c18ecSNate Lawson * stabilize. 516bf0c18ecSNate Lawson */ 5172b9609abSNate Lawson sc->acpi_sleep_delay = 1; 518043498dfSNate Lawson sc->acpi_disable_on_poweroff = 0; 5192d644607SMitsuru IWASAKI if (bootverbose) 5202d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 521498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 522498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 523498d464fSMitsuru IWASAKI freeenv(env); 524498d464fSMitsuru IWASAKI } 5251d073b1dSJohn Baldwin 5262d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5272d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 5282d610c46SNate Lawson sc->acpi_s4bios = 1; 5292d610c46SNate Lawson 5301d073b1dSJohn Baldwin /* 531ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 532ea27c63eSNate Lawson * to NONE by default to avoid surprising users. 53315e32d5dSMike Smith */ 534ea27c63eSNate Lawson sc->acpi_power_button_sx = ACPI_STATE_S5; 535ea27c63eSNate Lawson sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 536f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 537f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 53815e32d5dSMike Smith 539ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 540ea27c63eSNate Lawson sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 541ea27c63eSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++) 542ea27c63eSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 543ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 544ea27c63eSNate Lawson break; 545ea27c63eSNate Lawson } 546ea27c63eSNate Lawson 54713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 54815e32d5dSMike Smith 54915e32d5dSMike Smith /* 55015e32d5dSMike Smith * Scan the namespace and attach/initialise children. 55115e32d5dSMike Smith */ 552d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 553d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 554d786139cSMaxime Henrion if (debugpoint) { 555d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 55615e32d5dSMike Smith acpi_EnterDebugger(); 557d786139cSMaxime Henrion freeenv(debugpoint); 558d786139cSMaxime Henrion } 55915e32d5dSMike Smith #endif 56015e32d5dSMike Smith 561be2b1797SNate Lawson /* Register our shutdown handlers */ 562be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, 563be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 564be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 565be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 56615e32d5dSMike Smith 56715e32d5dSMike Smith /* 56815e32d5dSMike Smith * Register our acpi event handlers. 56915e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 57015e32d5dSMike Smith */ 571be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 572be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 573be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 574be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 57515e32d5dSMike Smith 576be2b1797SNate Lawson /* Flag our initial states. */ 57715e32d5dSMike Smith sc->acpi_enabled = 1; 57815e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 579ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 58015e32d5dSMike Smith 581be2b1797SNate Lawson /* Create the control device */ 582a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 5832a4689e8SRobert Watson "acpi"); 58415e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 58515e32d5dSMike Smith 586d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 587d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 588d786139cSMaxime Henrion if (debugpoint) { 589be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 59015e32d5dSMike Smith acpi_EnterDebugger(); 591d786139cSMaxime Henrion freeenv(debugpoint); 592d786139cSMaxime Henrion } 59315e32d5dSMike Smith #endif 594f86214b6SMitsuru IWASAKI 59591da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 596be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 597c573e654SMitsuru IWASAKI goto out; 598c573e654SMitsuru IWASAKI #endif 599c573e654SMitsuru IWASAKI 600be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 601f86214b6SMitsuru IWASAKI goto out; 602f86214b6SMitsuru IWASAKI 603f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 604f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 605f9390180SMitsuru IWASAKI 606eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 607eeb6dba2SJohn Baldwin acpi_probe_children(dev); 608eeb6dba2SJohn Baldwin 609cb9b0d80SMike Smith error = 0; 610cb9b0d80SMike Smith 611cb9b0d80SMike Smith out: 612cb9b0d80SMike Smith ACPI_UNLOCK; 613cb9b0d80SMike Smith return_VALUE (error); 61415e32d5dSMike Smith } 61515e32d5dSMike Smith 616169b539aSNate Lawson static int 617169b539aSNate Lawson acpi_shutdown(device_t dev) 618169b539aSNate Lawson { 619169b539aSNate Lawson 6200e414868SNate Lawson /* Allow children to shutdown first. */ 6210e414868SNate Lawson bus_generic_shutdown(dev); 6220e414868SNate Lawson 623169b539aSNate Lawson /* Disable all wake GPEs not appropriate for reboot/poweroff. */ 624169b539aSNate Lawson acpi_wake_limit_walk(ACPI_STATE_S5); 625169b539aSNate Lawson return (0); 626169b539aSNate Lawson } 627169b539aSNate Lawson 6283184cf5aSNate Lawson static void 6293184cf5aSNate Lawson acpi_quirks_set() 6303184cf5aSNate Lawson { 6313184cf5aSNate Lawson XSDT_DESCRIPTOR *xsdt; 6323184cf5aSNate Lawson struct acpi_quirks *quirk; 6333184cf5aSNate Lawson char *env, *tmp; 6343184cf5aSNate Lawson int len; 6353184cf5aSNate Lawson 6364e376d58SNate Lawson /* 6374e376d58SNate Lawson * If the user loaded a custom table or disabled "quirks", leave 6384e376d58SNate Lawson * the settings alone. 6394e376d58SNate Lawson */ 6403184cf5aSNate Lawson len = 0; 6414e376d58SNate Lawson if ((env = getenv("acpi_dsdt_load")) != NULL) { 6424e376d58SNate Lawson /* XXX No strcasecmp but this is good enough. */ 6434e376d58SNate Lawson if (*env == 'Y' || *env == 'y') 6444e376d58SNate Lawson goto out; 6454e376d58SNate Lawson freeenv(env); 6464e376d58SNate Lawson } 6473184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) != NULL) { 6484e376d58SNate Lawson if (strstr("quirks", env) != NULL) 6493184cf5aSNate Lawson goto out; 6503184cf5aSNate Lawson len = strlen(env); 6513184cf5aSNate Lawson } 6523184cf5aSNate Lawson 6533184cf5aSNate Lawson /* 6543184cf5aSNate Lawson * Search through our quirk table and concatenate the disabled 6553184cf5aSNate Lawson * values with whatever we find. 6563184cf5aSNate Lawson */ 6573184cf5aSNate Lawson xsdt = AcpiGbl_XSDT; 6583184cf5aSNate Lawson for (quirk = acpi_quirks_table; quirk->OemId; quirk++) { 6593184cf5aSNate Lawson if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) && 6603184cf5aSNate Lawson (xsdt->OemRevision == quirk->OemRevision || 6613184cf5aSNate Lawson quirk->OemRevision == ACPI_OEM_REV_ANY)) { 6623184cf5aSNate Lawson len += strlen(quirk->value) + 2; 6633184cf5aSNate Lawson if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL) 6643184cf5aSNate Lawson goto out; 6653184cf5aSNate Lawson sprintf(tmp, "%s %s", env ? env : "", quirk->value); 6663184cf5aSNate Lawson setenv("debug.acpi.disabled", tmp); 6673184cf5aSNate Lawson free(tmp, M_TEMP); 6683184cf5aSNate Lawson break; 6693184cf5aSNate Lawson } 6703184cf5aSNate Lawson } 6713184cf5aSNate Lawson 6723184cf5aSNate Lawson out: 6733184cf5aSNate Lawson if (env) 6743184cf5aSNate Lawson freeenv(env); 6753184cf5aSNate Lawson } 6763184cf5aSNate Lawson 67715e32d5dSMike Smith /* 67815e32d5dSMike Smith * Handle a new device being added 67915e32d5dSMike Smith */ 68015e32d5dSMike Smith static device_t 68115e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 68215e32d5dSMike Smith { 68315e32d5dSMike Smith struct acpi_device *ad; 68415e32d5dSMike Smith device_t child; 68515e32d5dSMike Smith 686be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 68715e32d5dSMike Smith return (NULL); 68815e32d5dSMike Smith 68915e32d5dSMike Smith resource_list_init(&ad->ad_rl); 69015e32d5dSMike Smith 69115e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 69215e32d5dSMike Smith if (child != NULL) 69315e32d5dSMike Smith device_set_ivars(child, ad); 69415e32d5dSMike Smith return (child); 69515e32d5dSMike Smith } 69615e32d5dSMike Smith 69715e32d5dSMike Smith static int 69815e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 69915e32d5dSMike Smith { 70015e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 70115e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 70215e32d5dSMike Smith int retval = 0; 70315e32d5dSMike Smith 70415e32d5dSMike Smith retval += bus_print_child_header(bus, child); 7059ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 7069ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 7079ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 7089ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 70915e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 71015e32d5dSMike Smith 71115e32d5dSMike Smith return (retval); 71215e32d5dSMike Smith } 71315e32d5dSMike Smith 71463600cc3SNate Lawson /* Location hint for devctl(8) */ 71563600cc3SNate Lawson static int 716247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 717247648afSTakanori Watanabe size_t buflen) 718247648afSTakanori Watanabe { 719247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 720247648afSTakanori Watanabe 721247648afSTakanori Watanabe if (dinfo->ad_handle) 722247648afSTakanori Watanabe snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle)); 723247648afSTakanori Watanabe else 724247648afSTakanori Watanabe snprintf(buf, buflen, "magic=unknown"); 725247648afSTakanori Watanabe return (0); 726247648afSTakanori Watanabe } 727247648afSTakanori Watanabe 72863600cc3SNate Lawson /* PnP information for devctl(8) */ 72963600cc3SNate Lawson static int 730247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 731247648afSTakanori Watanabe size_t buflen) 732247648afSTakanori Watanabe { 733247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 73463600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 73563600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 736247648afSTakanori Watanabe char *end; 737247648afSTakanori Watanabe int error; 738247648afSTakanori Watanabe 739247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 740247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 741247648afSTakanori Watanabe 742247648afSTakanori Watanabe if (error) 743247648afSTakanori Watanabe snprintf(buf, buflen, "Unknown"); 744247648afSTakanori Watanabe else 745247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 746247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 747247648afSTakanori Watanabe adinfo->HardwareId.Value : "UNKNOWN", 74863600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 74963600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 750247648afSTakanori Watanabe 751247648afSTakanori Watanabe if (adinfo) 752247648afSTakanori Watanabe AcpiOsFree(adinfo); 753247648afSTakanori Watanabe 754247648afSTakanori Watanabe return (0); 755247648afSTakanori Watanabe } 756247648afSTakanori Watanabe 757247648afSTakanori Watanabe /* 75815e32d5dSMike Smith * Handle per-device ivars 75915e32d5dSMike Smith */ 76015e32d5dSMike Smith static int 76115e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 76215e32d5dSMike Smith { 76315e32d5dSMike Smith struct acpi_device *ad; 76415e32d5dSMike Smith 76515e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 76615e32d5dSMike Smith printf("device has no ivars\n"); 76715e32d5dSMike Smith return (ENOENT); 76815e32d5dSMike Smith } 76915e32d5dSMike Smith 770be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 77115e32d5dSMike Smith switch(index) { 77215e32d5dSMike Smith case ACPI_IVAR_HANDLE: 77315e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 77415e32d5dSMike Smith break; 77515e32d5dSMike Smith case ACPI_IVAR_MAGIC: 77615e32d5dSMike Smith *(int *)result = ad->ad_magic; 77715e32d5dSMike Smith break; 77815e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 77915e32d5dSMike Smith *(void **)result = ad->ad_private; 78015e32d5dSMike Smith break; 78132d18aa5SMike Smith case ISA_IVAR_VENDORID: 78232d18aa5SMike Smith case ISA_IVAR_SERIAL: 78332d18aa5SMike Smith case ISA_IVAR_COMPATID: 78432d18aa5SMike Smith *(int *)result = -1; 78532d18aa5SMike Smith break; 78632d18aa5SMike Smith case ISA_IVAR_LOGICALID: 78732d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 78832d18aa5SMike Smith break; 78915e32d5dSMike Smith default: 79015e32d5dSMike Smith return (ENOENT); 79115e32d5dSMike Smith } 792be2b1797SNate Lawson 79315e32d5dSMike Smith return (0); 79415e32d5dSMike Smith } 79515e32d5dSMike Smith 79615e32d5dSMike Smith static int 79715e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 79815e32d5dSMike Smith { 79915e32d5dSMike Smith struct acpi_device *ad; 80015e32d5dSMike Smith 80115e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 80215e32d5dSMike Smith printf("device has no ivars\n"); 80315e32d5dSMike Smith return (ENOENT); 80415e32d5dSMike Smith } 80515e32d5dSMike Smith 80615e32d5dSMike Smith switch(index) { 80715e32d5dSMike Smith case ACPI_IVAR_HANDLE: 80815e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 80915e32d5dSMike Smith break; 81015e32d5dSMike Smith case ACPI_IVAR_MAGIC: 81115e32d5dSMike Smith ad->ad_magic = (int)value; 81215e32d5dSMike Smith break; 81315e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 81415e32d5dSMike Smith ad->ad_private = (void *)value; 81515e32d5dSMike Smith break; 81615e32d5dSMike Smith default: 8172ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 81815e32d5dSMike Smith return (ENOENT); 81915e32d5dSMike Smith } 820be2b1797SNate Lawson 82115e32d5dSMike Smith return (0); 82215e32d5dSMike Smith } 82315e32d5dSMike Smith 82415e32d5dSMike Smith /* 82515e32d5dSMike Smith * Handle child resource allocation/removal 82615e32d5dSMike Smith */ 82715e32d5dSMike Smith static int 828be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid, 829be2b1797SNate Lawson u_long start, u_long count) 83015e32d5dSMike Smith { 83115e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 83215e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 83315e32d5dSMike Smith 83415e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 83515e32d5dSMike Smith 83615e32d5dSMike Smith return(0); 83715e32d5dSMike Smith } 83815e32d5dSMike Smith 83915e32d5dSMike Smith static int 840be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid, 841be2b1797SNate Lawson u_long *startp, u_long *countp) 84215e32d5dSMike Smith { 84315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 84415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 84515e32d5dSMike Smith struct resource_list_entry *rle; 84615e32d5dSMike Smith 84715e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 84815e32d5dSMike Smith if (!rle) 84915e32d5dSMike Smith return(ENOENT); 85015e32d5dSMike Smith 85115e32d5dSMike Smith if (startp) 85215e32d5dSMike Smith *startp = rle->start; 85315e32d5dSMike Smith if (countp) 85415e32d5dSMike Smith *countp = rle->count; 85515e32d5dSMike Smith 85615e32d5dSMike Smith return (0); 85715e32d5dSMike Smith } 85815e32d5dSMike Smith 85915e32d5dSMike Smith static struct resource * 86015e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 86115e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 86215e32d5dSMike Smith { 86315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 86415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 86515e32d5dSMike Smith 866be2b1797SNate Lawson return (resource_list_alloc(rl, bus, child, type, rid, start, end, count, 867be2b1797SNate Lawson flags)); 86815e32d5dSMike Smith } 86915e32d5dSMike Smith 87015e32d5dSMike Smith static int 87115e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 87215e32d5dSMike Smith { 87315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 87415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 87515e32d5dSMike Smith 87615e32d5dSMike Smith return (resource_list_release(rl, bus, child, type, rid, r)); 87715e32d5dSMike Smith } 87815e32d5dSMike Smith 879dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 880dc750869SNate Lawson struct resource * 881dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 882dc750869SNate Lawson { 883dc750869SNate Lawson int type; 884dc750869SNate Lawson 885dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 886dc750869SNate Lawson gas->RegisterBitWidth < 8) 887dc750869SNate Lawson return (NULL); 888dc750869SNate Lawson 889dc750869SNate Lawson switch (gas->AddressSpaceId) { 890dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 891dc750869SNate Lawson type = SYS_RES_MEMORY; 892dc750869SNate Lawson break; 893dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 894dc750869SNate Lawson type = SYS_RES_IOPORT; 895dc750869SNate Lawson break; 896dc750869SNate Lawson default: 897dc750869SNate Lawson return (NULL); 898dc750869SNate Lawson } 899dc750869SNate Lawson 900dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 9015f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 902dc750869SNate Lawson } 903dc750869SNate Lawson 90415e32d5dSMike Smith /* 905bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 906bc0f2195SMike Smith */ 907bc0f2195SMike Smith #define PNP_EISAID(s) \ 908bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 909bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 910bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 911bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 912bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 913bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 914bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 915bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 916bc0f2195SMike Smith 9171e4925e8SNate Lawson static uint32_t 91832d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 919bc0f2195SMike Smith { 9201e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9211e4925e8SNate Lawson ACPI_BUFFER buf; 922bc0f2195SMike Smith ACPI_HANDLE h; 923bc0f2195SMike Smith ACPI_STATUS error; 924bc0f2195SMike Smith u_int32_t pnpid; 925fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 92632d18aa5SMike Smith 927b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 92832d18aa5SMike Smith 92932d18aa5SMike Smith pnpid = 0; 930bd189fe7SNate Lawson buf.Pointer = NULL; 931bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 932bd189fe7SNate Lawson 93332d18aa5SMike Smith ACPI_LOCK; 93432d18aa5SMike Smith 9356fca9360SNate Lawson /* Fetch and validate the HID. */ 93632d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 937bd189fe7SNate Lawson goto out; 9386fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9396fca9360SNate Lawson if (ACPI_FAILURE(error)) 940bd189fe7SNate Lawson goto out; 9411e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 94232d18aa5SMike Smith 9431e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 9441e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 945be2b1797SNate Lawson 946bd189fe7SNate Lawson out: 947bd189fe7SNate Lawson if (buf.Pointer != NULL) 9481e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 94932d18aa5SMike Smith ACPI_UNLOCK; 95032d18aa5SMike Smith return_VALUE (pnpid); 95132d18aa5SMike Smith } 95232d18aa5SMike Smith 9531e4925e8SNate Lawson static int 9541e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 955b2566207STakanori Watanabe { 9561e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9571e4925e8SNate Lawson ACPI_BUFFER buf; 958b2566207STakanori Watanabe ACPI_HANDLE h; 959b2566207STakanori Watanabe ACPI_STATUS error; 9601e4925e8SNate Lawson uint32_t *pnpid; 9611e4925e8SNate Lawson int valid, i; 962b2566207STakanori Watanabe ACPI_LOCK_DECL; 963b2566207STakanori Watanabe 964b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 965b2566207STakanori Watanabe 9661e4925e8SNate Lawson pnpid = cids; 9671e4925e8SNate Lawson valid = 0; 968bd189fe7SNate Lawson buf.Pointer = NULL; 969bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 970bd189fe7SNate Lawson 971b2566207STakanori Watanabe ACPI_LOCK; 972b2566207STakanori Watanabe 9731e4925e8SNate Lawson /* Fetch and validate the CID */ 974b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 975bd189fe7SNate Lawson goto out; 9761e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9771e4925e8SNate Lawson if (ACPI_FAILURE(error)) 978bd189fe7SNate Lawson goto out; 9791e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 9801e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 981b2566207STakanori Watanabe goto out; 982b2566207STakanori Watanabe 9831e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 9841e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 9851e4925e8SNate Lawson for (i = 0; i < count; i++) { 9861e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 9871e4925e8SNate Lawson continue; 9881e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 9891e4925e8SNate Lawson valid++; 990b2566207STakanori Watanabe } 991b2566207STakanori Watanabe 9921e4925e8SNate Lawson out: 993bd189fe7SNate Lawson if (buf.Pointer != NULL) 9941e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 9951e4925e8SNate Lawson ACPI_UNLOCK; 9961e4925e8SNate Lawson return_VALUE (valid); 9971e4925e8SNate Lawson } 998b2566207STakanori Watanabe 99932d18aa5SMike Smith static int 100032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 100132d18aa5SMike Smith { 10021e4925e8SNate Lawson int result, cid_count, i; 10031e4925e8SNate Lawson uint32_t lid, cids[8]; 1004bc0f2195SMike Smith 1005b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1006bc0f2195SMike Smith 1007bc0f2195SMike Smith /* 1008bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1009bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1010bc0f2195SMike Smith * that to happen, so don't ever return it. 1011bc0f2195SMike Smith */ 1012bc0f2195SMike Smith result = ENXIO; 1013bc0f2195SMike Smith 1014be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1015b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 10161e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1017bc0f2195SMike Smith while (ids && ids->ip_id) { 10181e4925e8SNate Lawson if (lid == ids->ip_id) { 1019bc0f2195SMike Smith result = 0; 1020bc0f2195SMike Smith goto out; 1021bc0f2195SMike Smith } 10221e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 10231e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 10241e4925e8SNate Lawson result = 0; 10251e4925e8SNate Lawson goto out; 10261e4925e8SNate Lawson } 10271e4925e8SNate Lawson } 1028bc0f2195SMike Smith ids++; 1029bc0f2195SMike Smith } 1030be2b1797SNate Lawson 1031bc0f2195SMike Smith out: 1032bc0f2195SMike Smith return_VALUE (result); 1033bc0f2195SMike Smith } 1034bc0f2195SMike Smith 1035bc0f2195SMike Smith /* 103615e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 103715e32d5dSMike Smith * 1038be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1039be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 104015e32d5dSMike Smith */ 104115e32d5dSMike Smith static void 104215e32d5dSMike Smith acpi_probe_children(device_t bus) 104315e32d5dSMike Smith { 104415e32d5dSMike Smith ACPI_HANDLE parent; 1045be2b1797SNate Lawson ACPI_STATUS status; 10467d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 104715e32d5dSMike Smith int i; 104815e32d5dSMike Smith 1049b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1050cb9b0d80SMike Smith ACPI_ASSERTLOCK; 10510ae55423SMike Smith 1052be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 10534c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 105415e32d5dSMike Smith bus_generic_probe(bus); 105515e32d5dSMike Smith 105615e32d5dSMike Smith /* 105715e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 105815e32d5dSMike Smith * we find. 105915e32d5dSMike Smith * 106015e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1061be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1062be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1063be2b1797SNate Lawson * devices as they appear, which might be smarter.) 106415e32d5dSMike Smith */ 10654c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1066be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1067be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1068be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1069be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1070be2b1797SNate Lawson bus, NULL); 1071be2b1797SNate Lawson } 1072be2b1797SNate Lawson } 107315e32d5dSMike Smith 107415e32d5dSMike Smith /* 107515e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 107615e32d5dSMike Smith */ 10774c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 107815e32d5dSMike Smith bus_generic_attach(bus); 107915e32d5dSMike Smith 108015e32d5dSMike Smith /* 108115e32d5dSMike Smith * Some of these children may have attached others as part of their attach 108215e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 108315e32d5dSMike Smith */ 10844c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 108515e32d5dSMike Smith bus_generic_attach(bus); 10860ae55423SMike Smith 108788a79fc0SNate Lawson /* Attach wake sysctls. */ 10885c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 108988a79fc0SNate Lawson 1090bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 10910ae55423SMike Smith return_VOID; 109215e32d5dSMike Smith } 109315e32d5dSMike Smith 109415e32d5dSMike Smith /* 109515e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 109615e32d5dSMike Smith * it. 109715e32d5dSMike Smith */ 109815e32d5dSMike Smith static ACPI_STATUS 109915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 110015e32d5dSMike Smith { 110115e32d5dSMike Smith ACPI_OBJECT_TYPE type; 110215e32d5dSMike Smith device_t child, bus = (device_t)context; 110315e32d5dSMike Smith 1104b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 11050ae55423SMike Smith 1106be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 11070ae55423SMike Smith if (acpi_avoid(handle)) 11080ae55423SMike Smith return_ACPI_STATUS (AE_OK); 11090ae55423SMike Smith 1110b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 111115e32d5dSMike Smith switch(type) { 111215e32d5dSMike Smith case ACPI_TYPE_DEVICE: 111315e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 111415e32d5dSMike Smith case ACPI_TYPE_THERMAL: 111515e32d5dSMike Smith case ACPI_TYPE_POWER: 11160ae55423SMike Smith if (acpi_disabled("children")) 11170ae55423SMike Smith break; 1118be2b1797SNate Lawson 111915e32d5dSMike Smith /* 112015e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 112115e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 112215e32d5dSMike Smith */ 1123be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1124be2b1797SNate Lawson acpi_name(handle))); 112515e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 1126bc0f2195SMike Smith if (child == NULL) 1127bc0f2195SMike Smith break; 112859a890e6SNate Lawson 112959a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 113015e32d5dSMike Smith acpi_set_handle(child, handle); 113159a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1132bc0f2195SMike Smith 1133e8b4d56eSNate Lawson /* Check if the device can generate wake events. */ 1134e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL))) 1135e8b4d56eSNate Lawson device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE); 1136e8b4d56eSNate Lawson 1137bc0f2195SMike Smith /* 1138b519f9d6SMike Smith * Check that the device is present. If it's not present, 1139b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1140b519f9d6SMike Smith * the handle, but we don't probe it). 1141b519f9d6SMike Smith */ 1142be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1143b519f9d6SMike Smith device_disable(child); 1144b519f9d6SMike Smith break; 1145b519f9d6SMike Smith } 1146b519f9d6SMike Smith 1147b519f9d6SMike Smith /* 1148bc0f2195SMike Smith * Get the device's resource settings and attach them. 1149bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1150bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1151bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1152bc0f2195SMike Smith * device not to have any resources. 1153bc0f2195SMike Smith */ 115472ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1155bc0f2195SMike Smith 1156be2b1797SNate Lawson /* If we're debugging, probe/attach now rather than later */ 1157b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 11580ae55423SMike Smith break; 115915e32d5dSMike Smith } 116015e32d5dSMike Smith } 1161be2b1797SNate Lawson 11620ae55423SMike Smith return_ACPI_STATUS (AE_OK); 116315e32d5dSMike Smith } 116415e32d5dSMike Smith 116559a890e6SNate Lawson /* 116659a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 116759a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 116859a890e6SNate Lawson */ 116959a890e6SNate Lawson void 117059a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 117159a890e6SNate Lawson { 117259a890e6SNate Lawson } 117359a890e6SNate Lawson 117415e32d5dSMike Smith static void 117515e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 117615e32d5dSMike Smith { 1177c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 1178c6a78e98STakanori Watanabe 1179cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1180cb9b0d80SMike Smith 118115e32d5dSMike Smith /* 11820ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 118315e32d5dSMike Smith * will be turned on again on some laptops. 118415e32d5dSMike Smith * 118515e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 118615e32d5dSMike Smith * before powering down, since we may still need ACPI during the 118715e32d5dSMike Smith * shutdown process. 118815e32d5dSMike Smith */ 1189c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 1190c6a78e98STakanori Watanabe acpi_Disable(sc); 119115e32d5dSMike Smith } 119215e32d5dSMike Smith 119315e32d5dSMike Smith static void 119415e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 119515e32d5dSMike Smith { 1196d33f4987STakanori Watanabe ACPI_STATUS status; 1197eeb3a05fSNate Lawson ACPI_ASSERTLOCK; 1198eeb3a05fSNate Lawson 1199eeb3a05fSNate Lawson /* 1200eeb3a05fSNate Lawson * If powering off, run the actual shutdown code on each processor. 1201eeb3a05fSNate Lawson * It will only perform the shutdown on the BSP. Some chipsets do 1202eeb3a05fSNate Lawson * not power off the system correctly if called from an AP. 1203eeb3a05fSNate Lawson */ 1204eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1205904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1206904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1207904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1208904bf0c2SNate Lawson AcpiFormatException(status)); 1209904bf0c2SNate Lawson return; 1210904bf0c2SNate Lawson } 1211eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 1212eeb3a05fSNate Lawson smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL); 1213eeb3a05fSNate Lawson } else { 1214eeb3a05fSNate Lawson printf("Shutting down ACPI\n"); 1215eeb3a05fSNate Lawson AcpiTerminate(); 1216eeb3a05fSNate Lawson } 1217eeb3a05fSNate Lawson } 1218eeb3a05fSNate Lawson 1219904bf0c2SNate Lawson /* 1220904bf0c2SNate Lawson * Since this function may be called with locks held or in an unknown 1221904bf0c2SNate Lawson * context, it cannot allocate memory, acquire locks, sleep, etc. 1222904bf0c2SNate Lawson */ 1223eeb3a05fSNate Lawson static void 1224eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg) 1225eeb3a05fSNate Lawson { 122615e32d5dSMike Smith ACPI_STATUS status; 122715e32d5dSMike Smith 1228cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1229cb9b0d80SMike Smith 1230eeb3a05fSNate Lawson /* Only attempt to power off if this is the BSP (cpuid 0). */ 1231eeb3a05fSNate Lawson if (PCPU_GET(cpuid) != 0) 1232eeb3a05fSNate Lawson return; 1233eeb3a05fSNate Lawson 123455398047SNate Lawson ACPI_DISABLE_IRQS(); 1235865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1236be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1237bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 123815e32d5dSMike Smith } else { 123915e32d5dSMike Smith DELAY(1000000); 124015e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 124115e32d5dSMike Smith } 124215e32d5dSMike Smith } 124315e32d5dSMike Smith 124413d4f7baSMitsuru IWASAKI static void 124513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 124613d4f7baSMitsuru IWASAKI { 124713d4f7baSMitsuru IWASAKI static int first_time = 1; 124813d4f7baSMitsuru IWASAKI 1249cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1250cb9b0d80SMike Smith 125113d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1252be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 125359ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 125413d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 125533febf93SNate Lawson acpi_event_power_button_sleep, sc); 1256be2b1797SNate Lawson if (first_time) 12576c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 125813d4f7baSMitsuru IWASAKI } 1259be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 126059ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 126113d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 126233febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1263be2b1797SNate Lawson if (first_time) 12646c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 126513d4f7baSMitsuru IWASAKI } 126613d4f7baSMitsuru IWASAKI 126713d4f7baSMitsuru IWASAKI first_time = 0; 126813d4f7baSMitsuru IWASAKI } 126913d4f7baSMitsuru IWASAKI 127015e32d5dSMike Smith /* 1271c5ba0be4SMike Smith * Returns true if the device is actually present and should 1272c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1273c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1274c5ba0be4SMike Smith */ 1275c5ba0be4SMike Smith BOOLEAN 1276c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1277c5ba0be4SMike Smith { 12781e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1279c5ba0be4SMike Smith ACPI_HANDLE h; 12801e4925e8SNate Lawson ACPI_BUFFER buf; 1281c5ba0be4SMike Smith ACPI_STATUS error; 12821e4925e8SNate Lawson int ret; 1283c5ba0be4SMike Smith 1284cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1285cb9b0d80SMike Smith 12861e4925e8SNate Lawson ret = FALSE; 1287c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1288c5ba0be4SMike Smith return (FALSE); 12891e4925e8SNate Lawson buf.Pointer = NULL; 12901e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 12916fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 12926fca9360SNate Lawson if (ACPI_FAILURE(error)) 1293c5ba0be4SMike Smith return (FALSE); 12941e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1295be2b1797SNate Lawson 1296be2b1797SNate Lawson /* If no _STA method, must be present */ 12971e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 12981e4925e8SNate Lawson ret = TRUE; 1299be2b1797SNate Lawson 1300be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13011e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x9) == 0x9) 13021e4925e8SNate Lawson ret = TRUE; 1303be2b1797SNate Lawson 13041e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13051e4925e8SNate Lawson return (ret); 1306c5ba0be4SMike Smith } 1307c5ba0be4SMike Smith 1308c5ba0be4SMike Smith /* 1309b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1310b53f2771SMike Smith */ 1311b53f2771SMike Smith BOOLEAN 1312b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1313b53f2771SMike Smith { 13141e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1315b53f2771SMike Smith ACPI_HANDLE h; 13161e4925e8SNate Lawson ACPI_BUFFER buf; 1317b53f2771SMike Smith ACPI_STATUS error; 13181e4925e8SNate Lawson int ret; 1319b53f2771SMike Smith 1320b53f2771SMike Smith ACPI_ASSERTLOCK; 1321b53f2771SMike Smith 13221e4925e8SNate Lawson ret = FALSE; 1323b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1324b53f2771SMike Smith return (FALSE); 13251e4925e8SNate Lawson buf.Pointer = NULL; 13261e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13276fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13286fca9360SNate Lawson if (ACPI_FAILURE(error)) 1329b53f2771SMike Smith return (FALSE); 13301e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1331be2b1797SNate Lawson 1332be2b1797SNate Lawson /* If no _STA method, must be present */ 13331e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 13341e4925e8SNate Lawson ret = TRUE; 1335be2b1797SNate Lawson 1336be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13371e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x19) == 0x19) 13381e4925e8SNate Lawson ret = TRUE; 1339be2b1797SNate Lawson 13401e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13411e4925e8SNate Lawson return (ret); 1342b53f2771SMike Smith } 1343b53f2771SMike Smith 1344b53f2771SMike Smith /* 134515e32d5dSMike Smith * Match a HID string against a device 134615e32d5dSMike Smith */ 134715e32d5dSMike Smith BOOLEAN 134815e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 134915e32d5dSMike Smith { 13501e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 135115e32d5dSMike Smith ACPI_HANDLE h; 13521e4925e8SNate Lawson ACPI_BUFFER buf; 135315e32d5dSMike Smith ACPI_STATUS error; 13541e4925e8SNate Lawson int ret, i; 135515e32d5dSMike Smith 1356cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1357cb9b0d80SMike Smith 13581e4925e8SNate Lawson ret = FALSE; 13594d332989SMike Smith if (hid == NULL) 136015e32d5dSMike Smith return (FALSE); 136115e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 136215e32d5dSMike Smith return (FALSE); 13631e4925e8SNate Lawson buf.Pointer = NULL; 13641e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13656fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13666fca9360SNate Lawson if (ACPI_FAILURE(error)) 136715e32d5dSMike Smith return (FALSE); 13681e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1369be2b1797SNate Lawson 1370c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1371c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 13721e4925e8SNate Lawson ret = TRUE; 1373c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 13741e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 13751e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 13761e4925e8SNate Lawson ret = TRUE; 13771e4925e8SNate Lawson break; 13781e4925e8SNate Lawson } 13791e4925e8SNate Lawson } 13801e4925e8SNate Lawson } 1381be2b1797SNate Lawson 13821e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13831e4925e8SNate Lawson return (ret); 138415e32d5dSMike Smith } 138515e32d5dSMike Smith 138615e32d5dSMike Smith /* 1387c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1388c5ba0be4SMike Smith * or one if its parents. 1389c5ba0be4SMike Smith */ 1390c5ba0be4SMike Smith ACPI_STATUS 1391c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1392c5ba0be4SMike Smith { 1393c5ba0be4SMike Smith ACPI_HANDLE r; 1394c5ba0be4SMike Smith ACPI_STATUS status; 1395c5ba0be4SMike Smith 1396cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1397cb9b0d80SMike Smith 1398be2b1797SNate Lawson /* Walk back up the tree to the root */ 1399c5ba0be4SMike Smith for (;;) { 1400be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1401be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1402c5ba0be4SMike Smith *result = r; 1403c5ba0be4SMike Smith return (AE_OK); 1404c5ba0be4SMike Smith } 1405c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1406c5ba0be4SMike Smith return (AE_OK); 1407b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1408c5ba0be4SMike Smith return (AE_NOT_FOUND); 1409c5ba0be4SMike Smith parent = r; 1410c5ba0be4SMike Smith } 1411c5ba0be4SMike Smith } 1412c5ba0be4SMike Smith 1413eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1414eea17c34SNate Lawson uint32_t 1415eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1416eea17c34SNate Lawson { 1417eea17c34SNate Lawson uint32_t delta; 1418eea17c34SNate Lawson 1419eea17c34SNate Lawson if (end >= start) 1420eea17c34SNate Lawson delta = end - start; 1421eea17c34SNate Lawson else if (AcpiGbl_FADT->TmrValExt == 0) 14228d01ceefSNate Lawson delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1423eea17c34SNate Lawson else 1424eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 1425eea17c34SNate Lawson return (delta); 1426eea17c34SNate Lawson } 1427eea17c34SNate Lawson 1428c5ba0be4SMike Smith /* 1429c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1430c5ba0be4SMike Smith */ 1431c5ba0be4SMike Smith ACPI_BUFFER * 1432c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1433c5ba0be4SMike Smith { 1434c5ba0be4SMike Smith ACPI_BUFFER *buf; 1435c5ba0be4SMike Smith 1436c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1437c5ba0be4SMike Smith return (NULL); 1438c5ba0be4SMike Smith buf->Length = size; 1439c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1440c5ba0be4SMike Smith return (buf); 1441c5ba0be4SMike Smith } 1442c5ba0be4SMike Smith 1443c310653eSNate Lawson ACPI_STATUS 1444cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1445c310653eSNate Lawson { 1446c310653eSNate Lawson ACPI_OBJECT arg1; 1447c310653eSNate Lawson ACPI_OBJECT_LIST args; 1448c310653eSNate Lawson 1449c310653eSNate Lawson ACPI_ASSERTLOCK; 1450c310653eSNate Lawson 1451c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1452c310653eSNate Lawson arg1.Integer.Value = number; 1453c310653eSNate Lawson args.Count = 1; 1454c310653eSNate Lawson args.Pointer = &arg1; 1455c310653eSNate Lawson 1456c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1457c310653eSNate Lawson } 1458c310653eSNate Lawson 1459c5ba0be4SMike Smith /* 1460c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1461c5ba0be4SMike Smith */ 1462c5ba0be4SMike Smith ACPI_STATUS 1463cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1464c5ba0be4SMike Smith { 1465be2b1797SNate Lawson ACPI_STATUS status; 1466c5ba0be4SMike Smith ACPI_BUFFER buf; 1467c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1468c5ba0be4SMike Smith 1469cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1470cb9b0d80SMike Smith 1471c5ba0be4SMike Smith if (handle == NULL) 1472c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 14730c7da7acSJohn Baldwin 14740c7da7acSJohn Baldwin /* 14750c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 14760c7da7acSJohn Baldwin * a method that will return an Integer. 14770c7da7acSJohn Baldwin */ 1478c5ba0be4SMike Smith buf.Pointer = ¶m; 1479c5ba0be4SMike Smith buf.Length = sizeof(param); 1480be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1481be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1482be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1483c5ba0be4SMike Smith *number = param.Integer.Value; 1484be2b1797SNate Lawson else 1485be2b1797SNate Lawson status = AE_TYPE; 1486c5ba0be4SMike Smith } 14870c7da7acSJohn Baldwin 14880c7da7acSJohn Baldwin /* 14890c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 14900c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 14910c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 14920c7da7acSJohn Baldwin * convert it into an Integer as best we can. 14930c7da7acSJohn Baldwin * 14940c7da7acSJohn Baldwin * This is a hack. 14950c7da7acSJohn Baldwin */ 1496be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1497b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1498be2b1797SNate Lawson status = AE_NO_MEMORY; 14990c7da7acSJohn Baldwin } else { 1500be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1501be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1502be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 15030c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 15040c7da7acSJohn Baldwin } 1505f97739daSNate Lawson } 1506be2b1797SNate Lawson return (status); 1507c5ba0be4SMike Smith } 1508c5ba0be4SMike Smith 1509c573e654SMitsuru IWASAKI ACPI_STATUS 1510cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1511c573e654SMitsuru IWASAKI { 1512c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1513dba55fa2SNate Lawson UINT8 *val; 1514c573e654SMitsuru IWASAKI int i; 1515c573e654SMitsuru IWASAKI 1516c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1517c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1518c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1519c573e654SMitsuru IWASAKI return (AE_OK); 1520c573e654SMitsuru IWASAKI } 1521c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1522c573e654SMitsuru IWASAKI return (AE_TYPE); 1523c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1524c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1525be2b1797SNate Lawson 1526c573e654SMitsuru IWASAKI *number = 0; 1527dba55fa2SNate Lawson val = p->Buffer.Pointer; 1528c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1529dba55fa2SNate Lawson *number += val[i] << (i * 8); 1530c573e654SMitsuru IWASAKI return (AE_OK); 1531c573e654SMitsuru IWASAKI } 1532c573e654SMitsuru IWASAKI 1533c5ba0be4SMike Smith /* 1534c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1535c5ba0be4SMike Smith * function for each element. 1536c5ba0be4SMike Smith * 1537c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1538c5ba0be4SMike Smith */ 1539c5ba0be4SMike Smith ACPI_STATUS 1540be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1541be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1542c5ba0be4SMike Smith { 1543c5ba0be4SMike Smith ACPI_OBJECT *comp; 1544c5ba0be4SMike Smith int i; 1545c5ba0be4SMike Smith 1546be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1547c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1548c5ba0be4SMike Smith 1549be2b1797SNate Lawson /* Iterate over components */ 1550be2b1797SNate Lawson i = 0; 1551be2b1797SNate Lawson comp = pkg->Package.Elements; 1552be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1553c5ba0be4SMike Smith func(comp, arg); 1554c5ba0be4SMike Smith 1555c5ba0be4SMike Smith return (AE_OK); 1556c5ba0be4SMike Smith } 1557c5ba0be4SMike Smith 15586f69255bSMike Smith /* 15596f69255bSMike Smith * Find the (index)th resource object in a set. 15606f69255bSMike Smith */ 15616f69255bSMike Smith ACPI_STATUS 15626d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 15636f69255bSMike Smith { 15646d63101aSMike Smith ACPI_RESOURCE *rp; 15656f69255bSMike Smith int i; 15666f69255bSMike Smith 15676d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 15686f69255bSMike Smith i = index; 15696d63101aSMike Smith while (i-- > 0) { 1570be2b1797SNate Lawson /* Range check */ 15716d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 15726f69255bSMike Smith return (AE_BAD_PARAMETER); 1573be2b1797SNate Lawson 1574be2b1797SNate Lawson /* Check for terminator */ 1575be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 15766d63101aSMike Smith return (AE_NOT_FOUND); 1577abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 15786f69255bSMike Smith } 15796f69255bSMike Smith if (resp != NULL) 15806d63101aSMike Smith *resp = rp; 1581be2b1797SNate Lawson 15826d63101aSMike Smith return (AE_OK); 15836d63101aSMike Smith } 15846d63101aSMike Smith 15856d63101aSMike Smith /* 15866d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 15876d63101aSMike Smith * 15886d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 15896d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 15906d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 15916d63101aSMike Smith * resources. 15926d63101aSMike Smith */ 15936d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 15946d63101aSMike Smith 15956d63101aSMike Smith ACPI_STATUS 15966d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 15976d63101aSMike Smith { 15986d63101aSMike Smith ACPI_RESOURCE *rp; 15996d63101aSMike Smith void *newp; 16006d63101aSMike Smith 1601be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 16026d63101aSMike Smith if (buf->Pointer == NULL) { 16036d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 16046d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 16056d63101aSMike Smith return (AE_NO_MEMORY); 16066d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16076d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 16086d63101aSMike Smith rp->Length = 0; 16096d63101aSMike Smith } 16106d63101aSMike Smith if (res == NULL) 16116d63101aSMike Smith return (AE_OK); 16126d63101aSMike Smith 16136d63101aSMike Smith /* 16146d63101aSMike Smith * Scan the current buffer looking for the terminator. 16156d63101aSMike Smith * This will either find the terminator or hit the end 16166d63101aSMike Smith * of the buffer and return an error. 16176d63101aSMike Smith */ 16186d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16196d63101aSMike Smith for (;;) { 1620be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 16216d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16226d63101aSMike Smith return (AE_BAD_PARAMETER); 1623be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16246d63101aSMike Smith break; 1625abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16266d63101aSMike Smith } 16276d63101aSMike Smith 16286d63101aSMike Smith /* 16296d63101aSMike Smith * Check the size of the buffer and expand if required. 16306d63101aSMike Smith * 16316d63101aSMike Smith * Required size is: 16326d63101aSMike Smith * size of existing resources before terminator + 16336d63101aSMike Smith * size of new resource and header + 16346d63101aSMike Smith * size of terminator. 16356d63101aSMike Smith * 16366d63101aSMike Smith * Note that this loop should really only run once, unless 16376d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 16386d63101aSMike Smith */ 16396d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 16406d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 16416d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 16426d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 16436d63101aSMike Smith return (AE_NO_MEMORY); 16446d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1645a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1646a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 16476d63101aSMike Smith AcpiOsFree(buf->Pointer); 16486d63101aSMike Smith buf->Pointer = newp; 16496d63101aSMike Smith buf->Length += buf->Length; 16506d63101aSMike Smith } 16516d63101aSMike Smith 1652be2b1797SNate Lawson /* Insert the new resource. */ 16536d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 16546d63101aSMike Smith 1655be2b1797SNate Lawson /* And add the terminator. */ 1656abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16576d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 16586d63101aSMike Smith rp->Length = 0; 16596d63101aSMike Smith 16606f69255bSMike Smith return (AE_OK); 16616f69255bSMike Smith } 1662c5ba0be4SMike Smith 1663da14ac9fSJohn Baldwin /* 1664da14ac9fSJohn Baldwin * Set interrupt model. 1665da14ac9fSJohn Baldwin */ 1666da14ac9fSJohn Baldwin ACPI_STATUS 1667da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1668da14ac9fSJohn Baldwin { 1669da14ac9fSJohn Baldwin 1670c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1671da14ac9fSJohn Baldwin } 1672da14ac9fSJohn Baldwin 1673ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1674ece50487SMitsuru IWASAKI 1675ece50487SMitsuru IWASAKI static void 1676ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1677ece50487SMitsuru IWASAKI { 1678ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1679ece50487SMitsuru IWASAKI } 1680c30382dfSMitsuru IWASAKI 168115e32d5dSMike Smith /* 168215e32d5dSMike Smith * Set the system sleep state 168315e32d5dSMike Smith * 1684be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 168515e32d5dSMike Smith */ 168615e32d5dSMike Smith ACPI_STATUS 168715e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 168815e32d5dSMike Smith { 168915e32d5dSMike Smith ACPI_STATUS status = AE_OK; 169056d8cb57SMitsuru IWASAKI UINT8 TypeA; 169156d8cb57SMitsuru IWASAKI UINT8 TypeB; 169215e32d5dSMike Smith 1693b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1694cb9b0d80SMike Smith ACPI_ASSERTLOCK; 16950ae55423SMike Smith 169698175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 16976397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 169898175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 16996397624dSMitsuru IWASAKI 170098175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1701ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1702ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1703ece50487SMitsuru IWASAKI 170415e32d5dSMike Smith switch (state) { 170515e32d5dSMike Smith case ACPI_STATE_S1: 17066161544cSTakanori Watanabe case ACPI_STATE_S2: 17076161544cSTakanori Watanabe case ACPI_STATE_S3: 17086161544cSTakanori Watanabe case ACPI_STATE_S4: 1709be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 171098175614SNate Lawson if (status == AE_NOT_FOUND) { 171198175614SNate Lawson device_printf(sc->acpi_dev, 171298175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 171398175614SNate Lawson break; 171498175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1715be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1716be2b1797SNate Lawson AcpiFormatException(status)); 171756d8cb57SMitsuru IWASAKI break; 171856d8cb57SMitsuru IWASAKI } 171956d8cb57SMitsuru IWASAKI 1720a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1721a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1722a1fccb47SMitsuru IWASAKI 1723e8b4d56eSNate Lawson /* Disable all wake GPEs not appropriate for this state. */ 1724e8b4d56eSNate Lawson acpi_wake_limit_walk(state); 1725e8b4d56eSNate Lawson 1726be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 172715e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 172815e32d5dSMike Smith /* 172915e32d5dSMike Smith * Re-wake the system. 173015e32d5dSMike Smith * 173115e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 173215e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 173315e32d5dSMike Smith * current bus interface does not provide for this. 173415e32d5dSMike Smith */ 173515e32d5dSMike Smith DEVICE_RESUME(root_bus); 17360ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 173715e32d5dSMike Smith } 1738b9c780d6SMitsuru IWASAKI 1739be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1740be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1741b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1742b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1743b9c780d6SMitsuru IWASAKI break; 1744b9c780d6SMitsuru IWASAKI } 1745b9c780d6SMitsuru IWASAKI 1746be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1747ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1748ff01efb5SMitsuru IWASAKI 17496161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 17506161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 17516161544cSTakanori Watanabe 1752be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 175359ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 175459ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 175559ddeb18SNate Lawson 17566161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1757a1fccb47SMitsuru IWASAKI } 17586161544cSTakanori Watanabe 17596161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1760be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1761964679ceSMitsuru IWASAKI AcpiEnable(); 17626161544cSTakanori Watanabe } else { 1763be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1764be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1765be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1766be2b1797SNate Lawson AcpiFormatException(status)); 1767c30382dfSMitsuru IWASAKI break; 176815e32d5dSMike Smith } 17696161544cSTakanori Watanabe } 1770ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 177115e32d5dSMike Smith DEVICE_RESUME(root_bus); 177215e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 177313d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 177415e32d5dSMike Smith break; 177515e32d5dSMike Smith case ACPI_STATE_S5: 177615e32d5dSMike Smith /* 177715e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 177815e32d5dSMike Smith * shutdown handlers. 177915e32d5dSMike Smith */ 178015e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 178115e32d5dSMike Smith break; 178298175614SNate Lawson case ACPI_STATE_S0: 178315e32d5dSMike Smith default: 178415e32d5dSMike Smith status = AE_BAD_PARAMETER; 178515e32d5dSMike Smith break; 178615e32d5dSMike Smith } 1787ece50487SMitsuru IWASAKI 178898175614SNate Lawson /* Disable a second sleep request for a short period */ 1789ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1790ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1791ece50487SMitsuru IWASAKI 17920ae55423SMike Smith return_ACPI_STATUS (status); 179315e32d5dSMike Smith } 179415e32d5dSMike Smith 1795e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1796e8b4d56eSNate Lawson int 1797e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1798e8b4d56eSNate Lawson { 1799e8b4d56eSNate Lawson struct acpi_prw_data prw; 1800e8b4d56eSNate Lawson 1801e8b4d56eSNate Lawson /* Check that the device can wake the system. */ 1802e8b4d56eSNate Lawson if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0) 1803e8b4d56eSNate Lawson return (ENXIO); 1804e8b4d56eSNate Lawson 1805e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1806e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1807e8b4d56eSNate Lawson return (ENXIO); 1808e8b4d56eSNate Lawson 1809e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1810e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1811e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1812e8b4d56eSNate Lawson return (ENXIO); 1813e8b4d56eSNate Lawson } 1814e8b4d56eSNate Lawson 1815e8b4d56eSNate Lawson return (0); 1816e8b4d56eSNate Lawson } 1817e8b4d56eSNate Lawson 1818e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1819e8b4d56eSNate Lawson int 1820e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1821e8b4d56eSNate Lawson { 1822e8b4d56eSNate Lawson struct acpi_prw_data prw; 1823e8b4d56eSNate Lawson ACPI_HANDLE handle; 1824e8b4d56eSNate Lawson ACPI_STATUS status; 1825e8b4d56eSNate Lawson int flags; 1826e8b4d56eSNate Lawson 1827e8b4d56eSNate Lawson /* Make sure the device supports waking the system. */ 1828e8b4d56eSNate Lawson flags = device_get_flags(dev); 1829e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1830e8b4d56eSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1831e8b4d56eSNate Lawson return (ENXIO); 1832e8b4d56eSNate Lawson 1833e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1834e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1835e8b4d56eSNate Lawson return (ENXIO); 1836e8b4d56eSNate Lawson 1837e8b4d56eSNate Lawson if (enable) { 1838e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1839e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1840e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1841e8b4d56eSNate Lawson return (ENXIO); 1842e8b4d56eSNate Lawson } 1843e8b4d56eSNate Lawson device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1844e8b4d56eSNate Lawson } else { 1845e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1846e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1847e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1848e8b4d56eSNate Lawson return (ENXIO); 1849e8b4d56eSNate Lawson } 1850e8b4d56eSNate Lawson device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1851e8b4d56eSNate Lawson } 1852e8b4d56eSNate Lawson 1853e8b4d56eSNate Lawson return (0); 1854e8b4d56eSNate Lawson } 1855e8b4d56eSNate Lawson 1856e8b4d56eSNate Lawson /* Configure a device's GPE appropriately for the new sleep state. */ 1857e8b4d56eSNate Lawson int 1858e8b4d56eSNate Lawson acpi_wake_sleep_prep(device_t dev, int sstate) 1859e8b4d56eSNate Lawson { 1860e8b4d56eSNate Lawson struct acpi_prw_data prw; 1861e8b4d56eSNate Lawson ACPI_HANDLE handle; 1862cc85c78cSNate Lawson int flags; 1863e8b4d56eSNate Lawson 1864e8b4d56eSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1865cc85c78cSNate Lawson flags = device_get_flags(dev); 1866e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1867cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1868e8b4d56eSNate Lawson return (ENXIO); 1869cc85c78cSNate Lawson 1870cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1871e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1872e8b4d56eSNate Lawson return (ENXIO); 1873e8b4d56eSNate Lawson 1874e8b4d56eSNate Lawson /* 1875cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1876cc85c78cSNate Lawson * of the _PRW object are put into the ON state. 1877e8b4d56eSNate Lawson */ 1878cc85c78cSNate Lawson 1879cc85c78cSNate Lawson /* 1880cc85c78cSNate Lawson * If the user requested that this device wake the system and the next 1881cc85c78cSNate Lawson * sleep state is valid for this GPE, enable it and the device's wake 1882cc85c78cSNate Lawson * capability. The sleep state must be less than (i.e., higher power) 1883cc85c78cSNate Lawson * or equal to the value specified by _PRW. Return early, leaving 1884cc85c78cSNate Lawson * the appropriate power resources enabled. 1885cc85c78cSNate Lawson */ 1886cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 && 1887cc85c78cSNate Lawson sstate <= prw.lowest_wake) { 1888e8b4d56eSNate Lawson if (bootverbose) 1889cc85c78cSNate Lawson device_printf(dev, "wake_prep enabled gpe %#x for state %d\n", 1890e8b4d56eSNate Lawson prw.gpe_bit, sstate); 1891cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1892cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 1); 1893e8b4d56eSNate Lawson return (0); 1894e8b4d56eSNate Lawson } 1895e8b4d56eSNate Lawson 1896e8b4d56eSNate Lawson /* 1897cc85c78cSNate Lawson * If the device wake was disabled or this sleep state is too low for 1898cc85c78cSNate Lawson * this device, disable its wake capability and GPE. 1899e8b4d56eSNate Lawson */ 1900cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1901cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1902e8b4d56eSNate Lawson if (bootverbose) 1903cc85c78cSNate Lawson device_printf(dev, "wake_prep disabled gpe %#x for state %d\n", 1904cc85c78cSNate Lawson prw.gpe_bit, sstate); 1905cc85c78cSNate Lawson 1906cc85c78cSNate Lawson /* 1907cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1908cc85c78cSNate Lawson * of the _PRW object are put into the OFF state. 1909cc85c78cSNate Lawson */ 1910cc85c78cSNate Lawson 1911cc85c78cSNate Lawson return (0); 1912e8b4d56eSNate Lawson } 1913e8b4d56eSNate Lawson 1914cc85c78cSNate Lawson /* Re-enable GPEs after wake. */ 1915cc85c78cSNate Lawson int 1916cc85c78cSNate Lawson acpi_wake_run_prep(device_t dev) 1917cc85c78cSNate Lawson { 1918cc85c78cSNate Lawson struct acpi_prw_data prw; 1919cc85c78cSNate Lawson ACPI_HANDLE handle; 1920cc85c78cSNate Lawson int flags; 1921cc85c78cSNate Lawson 1922cc85c78cSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1923cc85c78cSNate Lawson flags = device_get_flags(dev); 1924cc85c78cSNate Lawson handle = acpi_get_handle(dev); 1925cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1926cc85c78cSNate Lawson return (ENXIO); 1927cc85c78cSNate Lawson 1928cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1929cc85c78cSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1930cc85c78cSNate Lawson return (ENXIO); 1931cc85c78cSNate Lawson 1932cc85c78cSNate Lawson /* 1933cc85c78cSNate Lawson * TBD: Be sure all Power Resources referenced by elements 2 through N 1934cc85c78cSNate Lawson * of the _PRW object are in the ON state. 1935cc85c78cSNate Lawson */ 1936cc85c78cSNate Lawson 1937cc85c78cSNate Lawson /* Disable wake capability and if the user requested, enable the GPE. */ 1938cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1939cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0) 1940cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1941e8b4d56eSNate Lawson return (0); 1942e8b4d56eSNate Lawson } 1943e8b4d56eSNate Lawson 1944e8b4d56eSNate Lawson static ACPI_STATUS 1945e8b4d56eSNate Lawson acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status) 1946e8b4d56eSNate Lawson { 1947e8b4d56eSNate Lawson struct acpi_prw_data prw; 194844b8ae71SNate Lawson int *sstate; 1949e8b4d56eSNate Lawson 1950e8b4d56eSNate Lawson /* It's ok not to have _PRW if the device can't wake the system. */ 1951e8b4d56eSNate Lawson if (acpi_parse_prw(h, &prw) != 0) 1952e8b4d56eSNate Lawson return (AE_OK); 1953e8b4d56eSNate Lawson 195444b8ae71SNate Lawson sstate = (int *)context; 195544b8ae71SNate Lawson if (*sstate > prw.lowest_wake) 1956e8b4d56eSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1957e8b4d56eSNate Lawson 1958e8b4d56eSNate Lawson return (AE_OK); 1959e8b4d56eSNate Lawson } 1960e8b4d56eSNate Lawson 1961e8b4d56eSNate Lawson /* Walk all system devices, disabling them if necessary for sstate. */ 1962e8b4d56eSNate Lawson static int 1963e8b4d56eSNate Lawson acpi_wake_limit_walk(int sstate) 1964e8b4d56eSNate Lawson { 1965e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 1966e8b4d56eSNate Lawson 1967e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 1968e8b4d56eSNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100, 196944b8ae71SNate Lawson acpi_wake_limit, &sstate, NULL); 1970e8b4d56eSNate Lawson return (0); 1971e8b4d56eSNate Lawson } 1972e8b4d56eSNate Lawson 197388a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 197488a79fc0SNate Lawson static int 197588a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 197688a79fc0SNate Lawson { 197788a79fc0SNate Lawson int error, i, numdevs; 197888a79fc0SNate Lawson device_t *devlist; 197988a79fc0SNate Lawson device_t child; 198088a79fc0SNate Lawson 198188a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 198288a79fc0SNate Lawson if (error != 0 || numdevs == 0) 198388a79fc0SNate Lawson return (error); 198488a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 198588a79fc0SNate Lawson child = devlist[i]; 198688a79fc0SNate Lawson if (!device_is_attached(child)) 198788a79fc0SNate Lawson continue; 198888a79fc0SNate Lawson if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) { 198988a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 199088a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 199188a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 199288a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 199388a79fc0SNate Lawson } 199488a79fc0SNate Lawson acpi_wake_sysctl_walk(child); 199588a79fc0SNate Lawson } 199688a79fc0SNate Lawson free(devlist, M_TEMP); 199788a79fc0SNate Lawson 199888a79fc0SNate Lawson return (0); 199988a79fc0SNate Lawson } 200088a79fc0SNate Lawson 200188a79fc0SNate Lawson /* Enable or disable wake from userland. */ 200288a79fc0SNate Lawson static int 200388a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 200488a79fc0SNate Lawson { 200588a79fc0SNate Lawson int enable, error; 200688a79fc0SNate Lawson device_t dev; 200788a79fc0SNate Lawson 200888a79fc0SNate Lawson dev = (device_t)arg1; 200988a79fc0SNate Lawson enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 201088a79fc0SNate Lawson 201188a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 201288a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 201388a79fc0SNate Lawson return (error); 201488a79fc0SNate Lawson if (enable != 0 && enable != 1) 201588a79fc0SNate Lawson return (EINVAL); 201688a79fc0SNate Lawson 201788a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 201888a79fc0SNate Lawson } 201988a79fc0SNate Lawson 2020e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2021e8b4d56eSNate Lawson static int 2022e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2023e8b4d56eSNate Lawson { 2024e8b4d56eSNate Lawson ACPI_STATUS status; 2025e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2026e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2027e8b4d56eSNate Lawson int error; 2028e8b4d56eSNate Lawson 2029e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2030e8b4d56eSNate Lawson return (EINVAL); 2031e8b4d56eSNate Lawson 2032e8b4d56eSNate Lawson /* 2033e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2034e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2035e8b4d56eSNate Lawson */ 2036e8b4d56eSNate Lawson error = EINVAL; 2037e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2038e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2039e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2040e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2041e8b4d56eSNate Lawson return (ENOENT); 2042e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2043e8b4d56eSNate Lawson if (res == NULL) 2044e8b4d56eSNate Lawson return (ENOENT); 2045e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2046e8b4d56eSNate Lawson goto out; 2047e8b4d56eSNate Lawson 2048e8b4d56eSNate Lawson /* 2049e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2050e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2051e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2052e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2053e8b4d56eSNate Lawson */ 2054e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2055e8b4d56eSNate Lawson goto out; 2056e8b4d56eSNate Lawson 2057e8b4d56eSNate Lawson /* 2058e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2059e8b4d56eSNate Lawson */ 2060e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2061e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2062e8b4d56eSNate Lawson /* 2063e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2064e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2065e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2066e8b4d56eSNate Lawson * enabled for the wake event. 2067e8b4d56eSNate Lawson */ 2068e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2069e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2070e8b4d56eSNate Lawson error = 0; 2071e8b4d56eSNate Lawson break; 2072e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2073e8b4d56eSNate Lawson /* 2074e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2075e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2076e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2077e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2078e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2079e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2080e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2081e8b4d56eSNate Lawson * the wake event. 2082e8b4d56eSNate Lawson * 2083e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2084e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2085e8b4d56eSNate Lawson */ 2086e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2087e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2088e8b4d56eSNate Lawson goto out; 2089e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2090e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2091e8b4d56eSNate Lawson goto out; 2092e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2093e8b4d56eSNate Lawson goto out; 2094e8b4d56eSNate Lawson error = 0; 2095e8b4d56eSNate Lawson break; 2096e8b4d56eSNate Lawson default: 2097e8b4d56eSNate Lawson goto out; 2098e8b4d56eSNate Lawson } 2099e8b4d56eSNate Lawson 2100e8b4d56eSNate Lawson /* XXX No power resource handling yet. */ 2101e8b4d56eSNate Lawson prw->power_res = NULL; 2102e8b4d56eSNate Lawson 2103e8b4d56eSNate Lawson out: 2104e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2105e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2106e8b4d56eSNate Lawson return (error); 2107e8b4d56eSNate Lawson } 2108e8b4d56eSNate Lawson 210915e32d5dSMike Smith /* 211015e32d5dSMike Smith * Enable/Disable ACPI 211115e32d5dSMike Smith */ 211215e32d5dSMike Smith ACPI_STATUS 211315e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 211415e32d5dSMike Smith { 211515e32d5dSMike Smith ACPI_STATUS status; 211615e32d5dSMike Smith u_int32_t flags; 211715e32d5dSMike Smith 2118b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2119cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21200ae55423SMike Smith 212115e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 212215e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 2123be2b1797SNate Lawson if (!sc->acpi_enabled) 212415e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 2125be2b1797SNate Lawson else 212615e32d5dSMike Smith status = AE_OK; 2127be2b1797SNate Lawson 212815e32d5dSMike Smith if (status == AE_OK) 212915e32d5dSMike Smith sc->acpi_enabled = 1; 2130be2b1797SNate Lawson 21310ae55423SMike Smith return_ACPI_STATUS (status); 213215e32d5dSMike Smith } 213315e32d5dSMike Smith 213415e32d5dSMike Smith ACPI_STATUS 213515e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 213615e32d5dSMike Smith { 213715e32d5dSMike Smith ACPI_STATUS status; 213815e32d5dSMike Smith 2139b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2140cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21410ae55423SMike Smith 2142be2b1797SNate Lawson if (sc->acpi_enabled) 214315e32d5dSMike Smith status = AcpiDisable(); 2144be2b1797SNate Lawson else 214515e32d5dSMike Smith status = AE_OK; 2146be2b1797SNate Lawson 214715e32d5dSMike Smith if (status == AE_OK) 214815e32d5dSMike Smith sc->acpi_enabled = 0; 2149be2b1797SNate Lawson 21500ae55423SMike Smith return_ACPI_STATUS (status); 215115e32d5dSMike Smith } 215215e32d5dSMike Smith 215315e32d5dSMike Smith /* 215415e32d5dSMike Smith * ACPI Event Handlers 215515e32d5dSMike Smith */ 215615e32d5dSMike Smith 215715e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 215815e32d5dSMike Smith 215915e32d5dSMike Smith static void 216015e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 216115e32d5dSMike Smith { 2162fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2163b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 216415e32d5dSMike Smith 2165cb9b0d80SMike Smith ACPI_LOCK; 2166a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 216715e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2168cb9b0d80SMike Smith ACPI_UNLOCK; 21690ae55423SMike Smith return_VOID; 217015e32d5dSMike Smith } 217115e32d5dSMike Smith 217215e32d5dSMike Smith static void 217315e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 217415e32d5dSMike Smith { 2175fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2176b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 21770ae55423SMike Smith 217815e32d5dSMike Smith /* Well, what to do? :-) */ 21790ae55423SMike Smith 2180cb9b0d80SMike Smith ACPI_LOCK; 2181cb9b0d80SMike Smith ACPI_UNLOCK; 2182cb9b0d80SMike Smith 21830ae55423SMike Smith return_VOID; 218415e32d5dSMike Smith } 218515e32d5dSMike Smith 218615e32d5dSMike Smith /* 218715e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 218815e32d5dSMike Smith */ 218915e32d5dSMike Smith UINT32 219033febf93SNate Lawson acpi_event_power_button_sleep(void *context) 219115e32d5dSMike Smith { 219215e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 219315e32d5dSMike Smith 2194b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 21950ae55423SMike Smith 219615e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 21970ae55423SMike Smith 2198b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 219915e32d5dSMike Smith } 220015e32d5dSMike Smith 220115e32d5dSMike Smith UINT32 220233febf93SNate Lawson acpi_event_power_button_wake(void *context) 220315e32d5dSMike Smith { 220415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 220515e32d5dSMike Smith 2206b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22070ae55423SMike Smith 220815e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 22090ae55423SMike Smith 2210b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 221115e32d5dSMike Smith } 221215e32d5dSMike Smith 221315e32d5dSMike Smith UINT32 221433febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 221515e32d5dSMike Smith { 221615e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 221715e32d5dSMike Smith 2218b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22190ae55423SMike Smith 222015e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 22210ae55423SMike Smith 2222b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 222315e32d5dSMike Smith } 222415e32d5dSMike Smith 222515e32d5dSMike Smith UINT32 222633febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 222715e32d5dSMike Smith { 222815e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 222915e32d5dSMike Smith 2230b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22310ae55423SMike Smith 223215e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 22330ae55423SMike Smith 2234b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 223515e32d5dSMike Smith } 223615e32d5dSMike Smith 223715e32d5dSMike Smith /* 223815e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 223915e32d5dSMike Smith */ 224015e32d5dSMike Smith struct acpi_staticbuf { 224115e32d5dSMike Smith ACPI_BUFFER buffer; 224215e32d5dSMike Smith char data[512]; 224315e32d5dSMike Smith }; 224415e32d5dSMike Smith 224515e32d5dSMike Smith char * 224615e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 224715e32d5dSMike Smith { 224815e32d5dSMike Smith static struct acpi_staticbuf buf; 224915e32d5dSMike Smith 2250cb9b0d80SMike Smith ACPI_ASSERTLOCK; 2251cb9b0d80SMike Smith 225215e32d5dSMike Smith buf.buffer.Length = 512; 225315e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 225415e32d5dSMike Smith 2255b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 225615e32d5dSMike Smith return (buf.buffer.Pointer); 2257be2b1797SNate Lawson 225815e32d5dSMike Smith return ("(unknown path)"); 225915e32d5dSMike Smith } 226015e32d5dSMike Smith 226115e32d5dSMike Smith /* 226215e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 226315e32d5dSMike Smith * parts of the namespace. 226415e32d5dSMike Smith */ 226515e32d5dSMike Smith int 226615e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 226715e32d5dSMike Smith { 22683c600cfbSJohn Baldwin char *cp, *env, *np; 226915e32d5dSMike Smith int len; 227015e32d5dSMike Smith 227115e32d5dSMike Smith np = acpi_name(handle); 227215e32d5dSMike Smith if (*np == '\\') 227315e32d5dSMike Smith np++; 22743c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 227515e32d5dSMike Smith return (0); 227615e32d5dSMike Smith 2277be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 22783c600cfbSJohn Baldwin cp = env; 227915e32d5dSMike Smith for (;;) { 228015e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 228115e32d5dSMike Smith cp++; 228215e32d5dSMike Smith if (*cp == 0) 228315e32d5dSMike Smith break; 228415e32d5dSMike Smith len = 0; 228515e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 228615e32d5dSMike Smith len++; 2287d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 22883c600cfbSJohn Baldwin freeenv(env); 22890ae55423SMike Smith return(1); 2290d786139cSMaxime Henrion } 22910ae55423SMike Smith cp += len; 22920ae55423SMike Smith } 22933c600cfbSJohn Baldwin freeenv(env); 2294be2b1797SNate Lawson 22950ae55423SMike Smith return (0); 22960ae55423SMike Smith } 22970ae55423SMike Smith 22980ae55423SMike Smith /* 22990ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 23000ae55423SMike Smith */ 23010ae55423SMike Smith int 23020ae55423SMike Smith acpi_disabled(char *subsys) 23030ae55423SMike Smith { 230478689b15SMaxime Henrion char *cp, *env; 23050ae55423SMike Smith int len; 23060ae55423SMike Smith 23073184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 23080ae55423SMike Smith return (0); 23093184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 231078689b15SMaxime Henrion freeenv(env); 23110ae55423SMike Smith return (1); 2312d786139cSMaxime Henrion } 23130ae55423SMike Smith 23143184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 231578689b15SMaxime Henrion cp = env; 23160ae55423SMike Smith for (;;) { 23173184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 23180ae55423SMike Smith cp++; 23193184cf5aSNate Lawson if (*cp == '\0') 23200ae55423SMike Smith break; 23210ae55423SMike Smith len = 0; 23223184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 23230ae55423SMike Smith len++; 23243184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 232578689b15SMaxime Henrion freeenv(env); 232615e32d5dSMike Smith return (1); 2327d786139cSMaxime Henrion } 232815e32d5dSMike Smith cp += len; 232915e32d5dSMike Smith } 233078689b15SMaxime Henrion freeenv(env); 2331be2b1797SNate Lawson 233215e32d5dSMike Smith return (0); 233315e32d5dSMike Smith } 233415e32d5dSMike Smith 233515e32d5dSMike Smith /* 233615e32d5dSMike Smith * Control interface. 233715e32d5dSMike Smith * 23380ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2339be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2340be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 234115e32d5dSMike Smith */ 23420ae55423SMike Smith struct acpi_ioctl_hook 23430ae55423SMike Smith { 23440ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 23450ae55423SMike Smith u_long cmd; 2346be2b1797SNate Lawson acpi_ioctl_fn fn; 23470ae55423SMike Smith void *arg; 23480ae55423SMike Smith }; 23490ae55423SMike Smith 23500ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 23510ae55423SMike Smith static int acpi_ioctl_hooks_initted; 23520ae55423SMike Smith 23530ae55423SMike Smith /* 23540ae55423SMike Smith * Register an ioctl handler. 23550ae55423SMike Smith */ 23560ae55423SMike Smith int 2357be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 23580ae55423SMike Smith { 23590ae55423SMike Smith struct acpi_ioctl_hook *hp; 23600ae55423SMike Smith 23610ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 23620ae55423SMike Smith return (ENOMEM); 23630ae55423SMike Smith hp->cmd = cmd; 23640ae55423SMike Smith hp->fn = fn; 23650ae55423SMike Smith hp->arg = arg; 23660ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 23670ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 23680ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 23690ae55423SMike Smith } 23700ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 23710ae55423SMike Smith return (0); 23720ae55423SMike Smith } 23730ae55423SMike Smith 23740ae55423SMike Smith /* 23750ae55423SMike Smith * Deregister an ioctl handler. 23760ae55423SMike Smith */ 23770ae55423SMike Smith void 2378be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 23790ae55423SMike Smith { 23800ae55423SMike Smith struct acpi_ioctl_hook *hp; 23810ae55423SMike Smith 23820ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 23830ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 23840ae55423SMike Smith break; 23850ae55423SMike Smith 23860ae55423SMike Smith if (hp != NULL) { 23870ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 23880ae55423SMike Smith free(hp, M_ACPIDEV); 23890ae55423SMike Smith } 23900ae55423SMike Smith } 23910ae55423SMike Smith 239215e32d5dSMike Smith static int 23936b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 239415e32d5dSMike Smith { 239515e32d5dSMike Smith return (0); 239615e32d5dSMike Smith } 239715e32d5dSMike Smith 239815e32d5dSMike Smith static int 23996b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 240015e32d5dSMike Smith { 240115e32d5dSMike Smith return (0); 240215e32d5dSMike Smith } 240315e32d5dSMike Smith 240415e32d5dSMike Smith static int 24056b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 240615e32d5dSMike Smith { 240715e32d5dSMike Smith struct acpi_softc *sc; 24080ae55423SMike Smith struct acpi_ioctl_hook *hp; 24090ae55423SMike Smith int error, xerror, state; 2410fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 241115e32d5dSMike Smith 2412cb9b0d80SMike Smith ACPI_LOCK; 2413cb9b0d80SMike Smith 241415e32d5dSMike Smith error = state = 0; 241515e32d5dSMike Smith sc = dev->si_drv1; 241615e32d5dSMike Smith 24170ae55423SMike Smith /* 24180ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 24190ae55423SMike Smith */ 24200ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 24210ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 24220ae55423SMike Smith if (hp->cmd == cmd) { 24230ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 24240ae55423SMike Smith if (xerror != 0) 24250ae55423SMike Smith error = xerror; 2426917d44c8SMitsuru IWASAKI goto out; 24270ae55423SMike Smith } 24280ae55423SMike Smith } 24290ae55423SMike Smith } 24300ae55423SMike Smith 24310ae55423SMike Smith /* 2432a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2433a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2434a89fcf28STakanori Watanabe * Not changing system behavior. 2435a89fcf28STakanori Watanabe */ 2436be2b1797SNate Lawson if((flag & FWRITE) == 0) 2437be2b1797SNate Lawson return (EPERM); 2438a89fcf28STakanori Watanabe 2439be2b1797SNate Lawson /* Core system ioctls. */ 244015e32d5dSMike Smith switch (cmd) { 244115e32d5dSMike Smith case ACPIIO_ENABLE: 24420ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 244315e32d5dSMike Smith error = ENXIO; 244415e32d5dSMike Smith break; 244515e32d5dSMike Smith case ACPIIO_DISABLE: 24460ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 244715e32d5dSMike Smith error = ENXIO; 244815e32d5dSMike Smith break; 244915e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 245015e32d5dSMike Smith if (!sc->acpi_enabled) { 245115e32d5dSMike Smith error = ENXIO; 245215e32d5dSMike Smith break; 245315e32d5dSMike Smith } 245415e32d5dSMike Smith state = *(int *)addr; 24552d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 24562d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 245715e32d5dSMike Smith error = EINVAL; 24582d610c46SNate Lawson } else { 24592d610c46SNate Lawson error = EINVAL; 24602d610c46SNate Lawson } 246115e32d5dSMike Smith break; 246215e32d5dSMike Smith default: 24630ae55423SMike Smith if (error == 0) 246415e32d5dSMike Smith error = EINVAL; 246515e32d5dSMike Smith break; 246615e32d5dSMike Smith } 2467917d44c8SMitsuru IWASAKI 2468917d44c8SMitsuru IWASAKI out: 2469cb9b0d80SMike Smith ACPI_UNLOCK; 247015e32d5dSMike Smith return (error); 247115e32d5dSMike Smith } 247215e32d5dSMike Smith 24731d073b1dSJohn Baldwin static int 2474d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2475d75de536SMitsuru IWASAKI { 2476d75de536SMitsuru IWASAKI char sleep_state[4]; 2477d75de536SMitsuru IWASAKI char buf[16]; 2478d75de536SMitsuru IWASAKI int error; 2479d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2480d75de536SMitsuru IWASAKI 2481d75de536SMitsuru IWASAKI buf[0] = '\0'; 2482d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) { 2483d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2484d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2485d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2486d75de536SMitsuru IWASAKI } 2487d75de536SMitsuru IWASAKI } 2488d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2489d75de536SMitsuru IWASAKI return (error); 2490d75de536SMitsuru IWASAKI } 2491d75de536SMitsuru IWASAKI 2492d75de536SMitsuru IWASAKI static int 24931d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 24941d073b1dSJohn Baldwin { 24951d073b1dSJohn Baldwin char sleep_state[10]; 24961d073b1dSJohn Baldwin int error; 24971d073b1dSJohn Baldwin u_int new_state, old_state; 24981d073b1dSJohn Baldwin 24991d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2500b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX + 1) { 25011d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2502cb9b0d80SMike Smith } else { 2503b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 25041d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 25051d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2506cb9b0d80SMike Smith } 25071d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 25081d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2509be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2510be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 25111d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 25121d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 25131d073b1dSJohn Baldwin break; 2514cb9b0d80SMike Smith } 2515931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2516be2b1797SNate Lawson if (new_state != old_state) 25171d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2518cb9b0d80SMike Smith } else { 25191d073b1dSJohn Baldwin error = EINVAL; 25201d073b1dSJohn Baldwin } 2521cb9b0d80SMike Smith } 2522be2b1797SNate Lawson 25231d073b1dSJohn Baldwin return (error); 25241d073b1dSJohn Baldwin } 25251d073b1dSJohn Baldwin 25269b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 25279b937d48SNate Lawson void 25289b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 25299b937d48SNate Lawson { 25309b937d48SNate Lawson char notify_buf[16]; 25319b937d48SNate Lawson ACPI_BUFFER handle_buf; 25329b937d48SNate Lawson ACPI_STATUS status; 25339b937d48SNate Lawson 25349b937d48SNate Lawson if (subsystem == NULL) 25359b937d48SNate Lawson return; 25369b937d48SNate Lawson 25379b937d48SNate Lawson handle_buf.Pointer = NULL; 25389b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 25399b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 25409b937d48SNate Lawson if (ACPI_FAILURE(status)) 25419b937d48SNate Lawson return; 25429b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 25439b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 25449b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 25459b937d48SNate Lawson } 25469b937d48SNate Lawson 254715e32d5dSMike Smith #ifdef ACPI_DEBUG 25480ae55423SMike Smith /* 25490ae55423SMike Smith * Support for parsing debug options from the kernel environment. 25500ae55423SMike Smith * 25510ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 25520ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 25530ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 25540ae55423SMike Smith * prefixing the bit name with !. 25550ae55423SMike Smith */ 255615e32d5dSMike Smith struct debugtag 255715e32d5dSMike Smith { 255815e32d5dSMike Smith char *name; 255915e32d5dSMike Smith UINT32 value; 256015e32d5dSMike Smith }; 256115e32d5dSMike Smith 256215e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2563c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2564c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2565c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2566c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2567c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2568c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2569c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2570c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2571c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2572d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 25734c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2574d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2575297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 25764c1cdee6SMike Smith 2577c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2578c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 25793184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2580c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 25813184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 25823184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 25833184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 25844c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2585cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 25863184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2587b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 258815e32d5dSMike Smith {NULL, 0} 258915e32d5dSMike Smith }; 259015e32d5dSMike Smith 259115e32d5dSMike Smith static struct debugtag dbg_level[] = { 25924c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 25939501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 25949501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 25954c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 25969501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 25974c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2598d62ab2f4SMitsuru IWASAKI 2599d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2600297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 26014c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 26024c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2603d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 26044c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 26054c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 26064c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 26074c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 26084c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 26094c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 26104c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 26114c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 26124c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 26134c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2614d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2615d62ab2f4SMitsuru IWASAKI 2616d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2617d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2618d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2619d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2620d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 26214c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2622d62ab2f4SMitsuru IWASAKI 2623d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2624d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2625d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2626d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2627d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2628d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2629d62ab2f4SMitsuru IWASAKI 2630d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 263198479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 263298479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 263398479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 263498479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 263598479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 263615e32d5dSMike Smith {NULL, 0} 263715e32d5dSMike Smith }; 263815e32d5dSMike Smith 263915e32d5dSMike Smith static void 264015e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 264115e32d5dSMike Smith { 264215e32d5dSMike Smith char *ep; 264315e32d5dSMike Smith int i, l; 26440ae55423SMike Smith int set; 264515e32d5dSMike Smith 264615e32d5dSMike Smith while (*cp) { 264715e32d5dSMike Smith if (isspace(*cp)) { 264815e32d5dSMike Smith cp++; 264915e32d5dSMike Smith continue; 265015e32d5dSMike Smith } 265115e32d5dSMike Smith ep = cp; 265215e32d5dSMike Smith while (*ep && !isspace(*ep)) 265315e32d5dSMike Smith ep++; 26540ae55423SMike Smith if (*cp == '!') { 26550ae55423SMike Smith set = 0; 26560ae55423SMike Smith cp++; 26570ae55423SMike Smith if (cp == ep) 26580ae55423SMike Smith continue; 26590ae55423SMike Smith } else { 26600ae55423SMike Smith set = 1; 26610ae55423SMike Smith } 266215e32d5dSMike Smith l = ep - cp; 266315e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 266415e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2665be2b1797SNate Lawson if (set) 266615e32d5dSMike Smith *flag |= tag[i].value; 2667be2b1797SNate Lawson else 26680ae55423SMike Smith *flag &= ~tag[i].value; 266915e32d5dSMike Smith } 267015e32d5dSMike Smith } 267115e32d5dSMike Smith cp = ep; 267215e32d5dSMike Smith } 267315e32d5dSMike Smith } 267415e32d5dSMike Smith 267515e32d5dSMike Smith static void 26762a4ac806SMike Smith acpi_set_debugging(void *junk) 267715e32d5dSMike Smith { 26787e639165SNate Lawson char *layer, *level; 267915e32d5dSMike Smith 26801d7b121cSNate Lawson if (cold) { 26810ae55423SMike Smith AcpiDbgLayer = 0; 26820ae55423SMike Smith AcpiDbgLevel = 0; 26831d7b121cSNate Lawson } 26841d7b121cSNate Lawson 26857e639165SNate Lawson layer = getenv("debug.acpi.layer"); 26867e639165SNate Lawson level = getenv("debug.acpi.level"); 26877e639165SNate Lawson if (layer == NULL && level == NULL) 26887e639165SNate Lawson return; 26890ae55423SMike Smith 26907e639165SNate Lawson printf("ACPI set debug"); 26917e639165SNate Lawson if (layer != NULL) { 26927e639165SNate Lawson if (strcmp("NONE", layer) != 0) 26937e639165SNate Lawson printf(" layer '%s'", layer); 26947e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 26957e639165SNate Lawson freeenv(layer); 26961d7b121cSNate Lawson } 26977e639165SNate Lawson if (level != NULL) { 26987e639165SNate Lawson if (strcmp("NONE", level) != 0) 26997e639165SNate Lawson printf(" level '%s'", level); 27007e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 27017e639165SNate Lawson freeenv(level); 27027e639165SNate Lawson } 27037e639165SNate Lawson printf("\n"); 270415e32d5dSMike Smith } 2705be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2706be2b1797SNate Lawson NULL); 27071d7b121cSNate Lawson 27081d7b121cSNate Lawson static int 27091d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 27101d7b121cSNate Lawson { 271154f6bca0SNate Lawson int error, *dbg; 27121d7b121cSNate Lawson struct debugtag *tag; 271354f6bca0SNate Lawson struct sbuf sb; 27141d7b121cSNate Lawson 271554f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 271654f6bca0SNate Lawson return (ENOMEM); 27171d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 27181d7b121cSNate Lawson tag = &dbg_layer[0]; 27191d7b121cSNate Lawson dbg = &AcpiDbgLayer; 27201d7b121cSNate Lawson } else { 27211d7b121cSNate Lawson tag = &dbg_level[0]; 27221d7b121cSNate Lawson dbg = &AcpiDbgLevel; 27231d7b121cSNate Lawson } 27241d7b121cSNate Lawson 27251d7b121cSNate Lawson /* Get old values if this is a get request. */ 27261d7b121cSNate Lawson if (*dbg == 0) { 272754f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 27281d7b121cSNate Lawson } else if (req->newptr == NULL) { 27291d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 273054f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 273154f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 27321d7b121cSNate Lawson } 27331d7b121cSNate Lawson } 273454f6bca0SNate Lawson sbuf_trim(&sb); 273554f6bca0SNate Lawson sbuf_finish(&sb); 27361d7b121cSNate Lawson 27377e639165SNate Lawson /* Copy out the old values to the user. */ 27387e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 273954f6bca0SNate Lawson sbuf_delete(&sb); 27401d7b121cSNate Lawson 27411d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 27421d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 27431d7b121cSNate Lawson *dbg = 0; 27441d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 27451d7b121cSNate Lawson acpi_set_debugging(NULL); 27461d7b121cSNate Lawson } 27471d7b121cSNate Lawson 27481d7b121cSNate Lawson return (error); 27491d7b121cSNate Lawson } 27501d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 27511d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 27521d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 27531d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 275415e32d5dSMike Smith #endif 2755f9390180SMitsuru IWASAKI 2756f9390180SMitsuru IWASAKI static int 2757f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2758f9390180SMitsuru IWASAKI { 2759f9390180SMitsuru IWASAKI int state, acpi_state; 2760f9390180SMitsuru IWASAKI int error; 2761f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2762f9390180SMitsuru IWASAKI va_list ap; 2763f9390180SMitsuru IWASAKI 2764f9390180SMitsuru IWASAKI error = 0; 2765f9390180SMitsuru IWASAKI switch (cmd) { 2766f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2767f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2768f9390180SMitsuru IWASAKI if (sc == NULL) { 2769f9390180SMitsuru IWASAKI error = EINVAL; 2770f9390180SMitsuru IWASAKI goto out; 2771f9390180SMitsuru IWASAKI } 2772f9390180SMitsuru IWASAKI 2773f9390180SMitsuru IWASAKI va_start(ap, arg); 2774f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2775f9390180SMitsuru IWASAKI va_end(ap); 2776f9390180SMitsuru IWASAKI 2777f9390180SMitsuru IWASAKI switch (state) { 2778f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2779f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2780f9390180SMitsuru IWASAKI break; 2781f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2782f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2783f9390180SMitsuru IWASAKI break; 2784f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2785f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2786f9390180SMitsuru IWASAKI break; 2787f9390180SMitsuru IWASAKI default: 2788f9390180SMitsuru IWASAKI error = EINVAL; 2789f9390180SMitsuru IWASAKI goto out; 2790f9390180SMitsuru IWASAKI } 2791f9390180SMitsuru IWASAKI 2792f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2793f9390180SMitsuru IWASAKI break; 2794f9390180SMitsuru IWASAKI default: 2795f9390180SMitsuru IWASAKI error = EINVAL; 2796f9390180SMitsuru IWASAKI goto out; 2797f9390180SMitsuru IWASAKI } 2798f9390180SMitsuru IWASAKI 2799f9390180SMitsuru IWASAKI out: 2800f9390180SMitsuru IWASAKI return (error); 2801f9390180SMitsuru IWASAKI } 2802f9390180SMitsuru IWASAKI 2803f9390180SMitsuru IWASAKI static void 2804f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2805f9390180SMitsuru IWASAKI { 2806be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 28076c407052SMitsuru IWASAKI return; 2808f9390180SMitsuru IWASAKI 2809f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2810f9390180SMitsuru IWASAKI } 2811f9390180SMitsuru IWASAKI 2812f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2813