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 620169b539aSNate Lawson /* Disable all wake GPEs not appropriate for reboot/poweroff. */ 621169b539aSNate Lawson acpi_wake_limit_walk(ACPI_STATE_S5); 622169b539aSNate Lawson return (0); 623169b539aSNate Lawson } 624169b539aSNate Lawson 6253184cf5aSNate Lawson static void 6263184cf5aSNate Lawson acpi_quirks_set() 6273184cf5aSNate Lawson { 6283184cf5aSNate Lawson XSDT_DESCRIPTOR *xsdt; 6293184cf5aSNate Lawson struct acpi_quirks *quirk; 6303184cf5aSNate Lawson char *env, *tmp; 6313184cf5aSNate Lawson int len; 6323184cf5aSNate Lawson 6334e376d58SNate Lawson /* 6344e376d58SNate Lawson * If the user loaded a custom table or disabled "quirks", leave 6354e376d58SNate Lawson * the settings alone. 6364e376d58SNate Lawson */ 6373184cf5aSNate Lawson len = 0; 6384e376d58SNate Lawson if ((env = getenv("acpi_dsdt_load")) != NULL) { 6394e376d58SNate Lawson /* XXX No strcasecmp but this is good enough. */ 6404e376d58SNate Lawson if (*env == 'Y' || *env == 'y') 6414e376d58SNate Lawson goto out; 6424e376d58SNate Lawson freeenv(env); 6434e376d58SNate Lawson } 6443184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) != NULL) { 6454e376d58SNate Lawson if (strstr("quirks", env) != NULL) 6463184cf5aSNate Lawson goto out; 6473184cf5aSNate Lawson len = strlen(env); 6483184cf5aSNate Lawson } 6493184cf5aSNate Lawson 6503184cf5aSNate Lawson /* 6513184cf5aSNate Lawson * Search through our quirk table and concatenate the disabled 6523184cf5aSNate Lawson * values with whatever we find. 6533184cf5aSNate Lawson */ 6543184cf5aSNate Lawson xsdt = AcpiGbl_XSDT; 6553184cf5aSNate Lawson for (quirk = acpi_quirks_table; quirk->OemId; quirk++) { 6563184cf5aSNate Lawson if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) && 6573184cf5aSNate Lawson (xsdt->OemRevision == quirk->OemRevision || 6583184cf5aSNate Lawson quirk->OemRevision == ACPI_OEM_REV_ANY)) { 6593184cf5aSNate Lawson len += strlen(quirk->value) + 2; 6603184cf5aSNate Lawson if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL) 6613184cf5aSNate Lawson goto out; 6623184cf5aSNate Lawson sprintf(tmp, "%s %s", env ? env : "", quirk->value); 6633184cf5aSNate Lawson setenv("debug.acpi.disabled", tmp); 6643184cf5aSNate Lawson free(tmp, M_TEMP); 6653184cf5aSNate Lawson break; 6663184cf5aSNate Lawson } 6673184cf5aSNate Lawson } 6683184cf5aSNate Lawson 6693184cf5aSNate Lawson out: 6703184cf5aSNate Lawson if (env) 6713184cf5aSNate Lawson freeenv(env); 6723184cf5aSNate Lawson } 6733184cf5aSNate Lawson 67415e32d5dSMike Smith /* 67515e32d5dSMike Smith * Handle a new device being added 67615e32d5dSMike Smith */ 67715e32d5dSMike Smith static device_t 67815e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 67915e32d5dSMike Smith { 68015e32d5dSMike Smith struct acpi_device *ad; 68115e32d5dSMike Smith device_t child; 68215e32d5dSMike Smith 683be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 68415e32d5dSMike Smith return (NULL); 68515e32d5dSMike Smith 68615e32d5dSMike Smith resource_list_init(&ad->ad_rl); 68715e32d5dSMike Smith 68815e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 68915e32d5dSMike Smith if (child != NULL) 69015e32d5dSMike Smith device_set_ivars(child, ad); 69115e32d5dSMike Smith return (child); 69215e32d5dSMike Smith } 69315e32d5dSMike Smith 69415e32d5dSMike Smith static int 69515e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 69615e32d5dSMike Smith { 69715e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 69815e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 69915e32d5dSMike Smith int retval = 0; 70015e32d5dSMike Smith 70115e32d5dSMike Smith retval += bus_print_child_header(bus, child); 7029ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 7039ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 7049ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 7059ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 70615e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 70715e32d5dSMike Smith 70815e32d5dSMike Smith return (retval); 70915e32d5dSMike Smith } 71015e32d5dSMike Smith 71163600cc3SNate Lawson /* Location hint for devctl(8) */ 71263600cc3SNate Lawson static int 713247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 714247648afSTakanori Watanabe size_t buflen) 715247648afSTakanori Watanabe { 716247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 717247648afSTakanori Watanabe 718247648afSTakanori Watanabe if (dinfo->ad_handle) 719247648afSTakanori Watanabe snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle)); 720247648afSTakanori Watanabe else 721247648afSTakanori Watanabe snprintf(buf, buflen, "magic=unknown"); 722247648afSTakanori Watanabe return (0); 723247648afSTakanori Watanabe } 724247648afSTakanori Watanabe 72563600cc3SNate Lawson /* PnP information for devctl(8) */ 72663600cc3SNate Lawson static int 727247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 728247648afSTakanori Watanabe size_t buflen) 729247648afSTakanori Watanabe { 730247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 73163600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 73263600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 733247648afSTakanori Watanabe char *end; 734247648afSTakanori Watanabe int error; 735247648afSTakanori Watanabe 736247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 737247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 738247648afSTakanori Watanabe 739247648afSTakanori Watanabe if (error) 740247648afSTakanori Watanabe snprintf(buf, buflen, "Unknown"); 741247648afSTakanori Watanabe else 742247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 743247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 744247648afSTakanori Watanabe adinfo->HardwareId.Value : "UNKNOWN", 74563600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 74663600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 747247648afSTakanori Watanabe 748247648afSTakanori Watanabe if (adinfo) 749247648afSTakanori Watanabe AcpiOsFree(adinfo); 750247648afSTakanori Watanabe 751247648afSTakanori Watanabe return (0); 752247648afSTakanori Watanabe } 753247648afSTakanori Watanabe 754247648afSTakanori Watanabe /* 75515e32d5dSMike Smith * Handle per-device ivars 75615e32d5dSMike Smith */ 75715e32d5dSMike Smith static int 75815e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 75915e32d5dSMike Smith { 76015e32d5dSMike Smith struct acpi_device *ad; 76115e32d5dSMike Smith 76215e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 76315e32d5dSMike Smith printf("device has no ivars\n"); 76415e32d5dSMike Smith return (ENOENT); 76515e32d5dSMike Smith } 76615e32d5dSMike Smith 767be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 76815e32d5dSMike Smith switch(index) { 76915e32d5dSMike Smith case ACPI_IVAR_HANDLE: 77015e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 77115e32d5dSMike Smith break; 77215e32d5dSMike Smith case ACPI_IVAR_MAGIC: 77315e32d5dSMike Smith *(int *)result = ad->ad_magic; 77415e32d5dSMike Smith break; 77515e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 77615e32d5dSMike Smith *(void **)result = ad->ad_private; 77715e32d5dSMike Smith break; 77832d18aa5SMike Smith case ISA_IVAR_VENDORID: 77932d18aa5SMike Smith case ISA_IVAR_SERIAL: 78032d18aa5SMike Smith case ISA_IVAR_COMPATID: 78132d18aa5SMike Smith *(int *)result = -1; 78232d18aa5SMike Smith break; 78332d18aa5SMike Smith case ISA_IVAR_LOGICALID: 78432d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 78532d18aa5SMike Smith break; 78615e32d5dSMike Smith default: 78715e32d5dSMike Smith return (ENOENT); 78815e32d5dSMike Smith } 789be2b1797SNate Lawson 79015e32d5dSMike Smith return (0); 79115e32d5dSMike Smith } 79215e32d5dSMike Smith 79315e32d5dSMike Smith static int 79415e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 79515e32d5dSMike Smith { 79615e32d5dSMike Smith struct acpi_device *ad; 79715e32d5dSMike Smith 79815e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 79915e32d5dSMike Smith printf("device has no ivars\n"); 80015e32d5dSMike Smith return (ENOENT); 80115e32d5dSMike Smith } 80215e32d5dSMike Smith 80315e32d5dSMike Smith switch(index) { 80415e32d5dSMike Smith case ACPI_IVAR_HANDLE: 80515e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 80615e32d5dSMike Smith break; 80715e32d5dSMike Smith case ACPI_IVAR_MAGIC: 80815e32d5dSMike Smith ad->ad_magic = (int)value; 80915e32d5dSMike Smith break; 81015e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 81115e32d5dSMike Smith ad->ad_private = (void *)value; 81215e32d5dSMike Smith break; 81315e32d5dSMike Smith default: 8142ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 81515e32d5dSMike Smith return (ENOENT); 81615e32d5dSMike Smith } 817be2b1797SNate Lawson 81815e32d5dSMike Smith return (0); 81915e32d5dSMike Smith } 82015e32d5dSMike Smith 82115e32d5dSMike Smith /* 82215e32d5dSMike Smith * Handle child resource allocation/removal 82315e32d5dSMike Smith */ 82415e32d5dSMike Smith static int 825be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid, 826be2b1797SNate Lawson u_long start, u_long count) 82715e32d5dSMike Smith { 82815e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 82915e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 83015e32d5dSMike Smith 83115e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 83215e32d5dSMike Smith 83315e32d5dSMike Smith return(0); 83415e32d5dSMike Smith } 83515e32d5dSMike Smith 83615e32d5dSMike Smith static int 837be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid, 838be2b1797SNate Lawson u_long *startp, u_long *countp) 83915e32d5dSMike Smith { 84015e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 84115e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 84215e32d5dSMike Smith struct resource_list_entry *rle; 84315e32d5dSMike Smith 84415e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 84515e32d5dSMike Smith if (!rle) 84615e32d5dSMike Smith return(ENOENT); 84715e32d5dSMike Smith 84815e32d5dSMike Smith if (startp) 84915e32d5dSMike Smith *startp = rle->start; 85015e32d5dSMike Smith if (countp) 85115e32d5dSMike Smith *countp = rle->count; 85215e32d5dSMike Smith 85315e32d5dSMike Smith return (0); 85415e32d5dSMike Smith } 85515e32d5dSMike Smith 85615e32d5dSMike Smith static struct resource * 85715e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 85815e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 85915e32d5dSMike Smith { 86015e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 86115e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 86215e32d5dSMike Smith 863be2b1797SNate Lawson return (resource_list_alloc(rl, bus, child, type, rid, start, end, count, 864be2b1797SNate Lawson flags)); 86515e32d5dSMike Smith } 86615e32d5dSMike Smith 86715e32d5dSMike Smith static int 86815e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 86915e32d5dSMike Smith { 87015e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 87115e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 87215e32d5dSMike Smith 87315e32d5dSMike Smith return (resource_list_release(rl, bus, child, type, rid, r)); 87415e32d5dSMike Smith } 87515e32d5dSMike Smith 876dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 877dc750869SNate Lawson struct resource * 878dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 879dc750869SNate Lawson { 880dc750869SNate Lawson int type; 881dc750869SNate Lawson 882dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 883dc750869SNate Lawson gas->RegisterBitWidth < 8) 884dc750869SNate Lawson return (NULL); 885dc750869SNate Lawson 886dc750869SNate Lawson switch (gas->AddressSpaceId) { 887dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 888dc750869SNate Lawson type = SYS_RES_MEMORY; 889dc750869SNate Lawson break; 890dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 891dc750869SNate Lawson type = SYS_RES_IOPORT; 892dc750869SNate Lawson break; 893dc750869SNate Lawson default: 894dc750869SNate Lawson return (NULL); 895dc750869SNate Lawson } 896dc750869SNate Lawson 897dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 8985f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 899dc750869SNate Lawson } 900dc750869SNate Lawson 90115e32d5dSMike Smith /* 902bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 903bc0f2195SMike Smith */ 904bc0f2195SMike Smith #define PNP_EISAID(s) \ 905bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 906bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 907bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 908bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 909bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 910bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 911bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 912bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 913bc0f2195SMike Smith 9141e4925e8SNate Lawson static uint32_t 91532d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 916bc0f2195SMike Smith { 9171e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9181e4925e8SNate Lawson ACPI_BUFFER buf; 919bc0f2195SMike Smith ACPI_HANDLE h; 920bc0f2195SMike Smith ACPI_STATUS error; 921bc0f2195SMike Smith u_int32_t pnpid; 922fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 92332d18aa5SMike Smith 924b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 92532d18aa5SMike Smith 92632d18aa5SMike Smith pnpid = 0; 927bd189fe7SNate Lawson buf.Pointer = NULL; 928bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 929bd189fe7SNate Lawson 93032d18aa5SMike Smith ACPI_LOCK; 93132d18aa5SMike Smith 9326fca9360SNate Lawson /* Fetch and validate the HID. */ 93332d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 934bd189fe7SNate Lawson goto out; 9356fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9366fca9360SNate Lawson if (ACPI_FAILURE(error)) 937bd189fe7SNate Lawson goto out; 9381e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 93932d18aa5SMike Smith 9401e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 9411e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 942be2b1797SNate Lawson 943bd189fe7SNate Lawson out: 944bd189fe7SNate Lawson if (buf.Pointer != NULL) 9451e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 94632d18aa5SMike Smith ACPI_UNLOCK; 94732d18aa5SMike Smith return_VALUE (pnpid); 94832d18aa5SMike Smith } 94932d18aa5SMike Smith 9501e4925e8SNate Lawson static int 9511e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 952b2566207STakanori Watanabe { 9531e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9541e4925e8SNate Lawson ACPI_BUFFER buf; 955b2566207STakanori Watanabe ACPI_HANDLE h; 956b2566207STakanori Watanabe ACPI_STATUS error; 9571e4925e8SNate Lawson uint32_t *pnpid; 9581e4925e8SNate Lawson int valid, i; 959b2566207STakanori Watanabe ACPI_LOCK_DECL; 960b2566207STakanori Watanabe 961b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 962b2566207STakanori Watanabe 9631e4925e8SNate Lawson pnpid = cids; 9641e4925e8SNate Lawson valid = 0; 965bd189fe7SNate Lawson buf.Pointer = NULL; 966bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 967bd189fe7SNate Lawson 968b2566207STakanori Watanabe ACPI_LOCK; 969b2566207STakanori Watanabe 9701e4925e8SNate Lawson /* Fetch and validate the CID */ 971b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 972bd189fe7SNate Lawson goto out; 9731e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9741e4925e8SNate Lawson if (ACPI_FAILURE(error)) 975bd189fe7SNate Lawson goto out; 9761e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 9771e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 978b2566207STakanori Watanabe goto out; 979b2566207STakanori Watanabe 9801e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 9811e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 9821e4925e8SNate Lawson for (i = 0; i < count; i++) { 9831e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 9841e4925e8SNate Lawson continue; 9851e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 9861e4925e8SNate Lawson valid++; 987b2566207STakanori Watanabe } 988b2566207STakanori Watanabe 9891e4925e8SNate Lawson out: 990bd189fe7SNate Lawson if (buf.Pointer != NULL) 9911e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 9921e4925e8SNate Lawson ACPI_UNLOCK; 9931e4925e8SNate Lawson return_VALUE (valid); 9941e4925e8SNate Lawson } 995b2566207STakanori Watanabe 99632d18aa5SMike Smith static int 99732d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 99832d18aa5SMike Smith { 9991e4925e8SNate Lawson int result, cid_count, i; 10001e4925e8SNate Lawson uint32_t lid, cids[8]; 1001bc0f2195SMike Smith 1002b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1003bc0f2195SMike Smith 1004bc0f2195SMike Smith /* 1005bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1006bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1007bc0f2195SMike Smith * that to happen, so don't ever return it. 1008bc0f2195SMike Smith */ 1009bc0f2195SMike Smith result = ENXIO; 1010bc0f2195SMike Smith 1011be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1012b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 10131e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1014bc0f2195SMike Smith while (ids && ids->ip_id) { 10151e4925e8SNate Lawson if (lid == ids->ip_id) { 1016bc0f2195SMike Smith result = 0; 1017bc0f2195SMike Smith goto out; 1018bc0f2195SMike Smith } 10191e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 10201e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 10211e4925e8SNate Lawson result = 0; 10221e4925e8SNate Lawson goto out; 10231e4925e8SNate Lawson } 10241e4925e8SNate Lawson } 1025bc0f2195SMike Smith ids++; 1026bc0f2195SMike Smith } 1027be2b1797SNate Lawson 1028bc0f2195SMike Smith out: 1029bc0f2195SMike Smith return_VALUE (result); 1030bc0f2195SMike Smith } 1031bc0f2195SMike Smith 1032bc0f2195SMike Smith /* 103315e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 103415e32d5dSMike Smith * 1035be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1036be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 103715e32d5dSMike Smith */ 103815e32d5dSMike Smith static void 103915e32d5dSMike Smith acpi_probe_children(device_t bus) 104015e32d5dSMike Smith { 104115e32d5dSMike Smith ACPI_HANDLE parent; 1042be2b1797SNate Lawson ACPI_STATUS status; 10437d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 104415e32d5dSMike Smith int i; 104515e32d5dSMike Smith 1046b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1047cb9b0d80SMike Smith ACPI_ASSERTLOCK; 10480ae55423SMike Smith 1049be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 10504c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 105115e32d5dSMike Smith bus_generic_probe(bus); 105215e32d5dSMike Smith 105315e32d5dSMike Smith /* 105415e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 105515e32d5dSMike Smith * we find. 105615e32d5dSMike Smith * 105715e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1058be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1059be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1060be2b1797SNate Lawson * devices as they appear, which might be smarter.) 106115e32d5dSMike Smith */ 10624c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1063be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1064be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1065be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1066be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1067be2b1797SNate Lawson bus, NULL); 1068be2b1797SNate Lawson } 1069be2b1797SNate Lawson } 107015e32d5dSMike Smith 107115e32d5dSMike Smith /* 107215e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 107315e32d5dSMike Smith */ 10744c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 107515e32d5dSMike Smith bus_generic_attach(bus); 107615e32d5dSMike Smith 107715e32d5dSMike Smith /* 107815e32d5dSMike Smith * Some of these children may have attached others as part of their attach 107915e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 108015e32d5dSMike Smith */ 10814c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 108215e32d5dSMike Smith bus_generic_attach(bus); 10830ae55423SMike Smith 108488a79fc0SNate Lawson /* Attach wake sysctls. */ 10855c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 108688a79fc0SNate Lawson 1087bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 10880ae55423SMike Smith return_VOID; 108915e32d5dSMike Smith } 109015e32d5dSMike Smith 109115e32d5dSMike Smith /* 109215e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 109315e32d5dSMike Smith * it. 109415e32d5dSMike Smith */ 109515e32d5dSMike Smith static ACPI_STATUS 109615e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 109715e32d5dSMike Smith { 109815e32d5dSMike Smith ACPI_OBJECT_TYPE type; 109915e32d5dSMike Smith device_t child, bus = (device_t)context; 110015e32d5dSMike Smith 1101b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 11020ae55423SMike Smith 1103be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 11040ae55423SMike Smith if (acpi_avoid(handle)) 11050ae55423SMike Smith return_ACPI_STATUS (AE_OK); 11060ae55423SMike Smith 1107b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 110815e32d5dSMike Smith switch(type) { 110915e32d5dSMike Smith case ACPI_TYPE_DEVICE: 111015e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 111115e32d5dSMike Smith case ACPI_TYPE_THERMAL: 111215e32d5dSMike Smith case ACPI_TYPE_POWER: 11130ae55423SMike Smith if (acpi_disabled("children")) 11140ae55423SMike Smith break; 1115be2b1797SNate Lawson 111615e32d5dSMike Smith /* 111715e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 111815e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 111915e32d5dSMike Smith */ 1120be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1121be2b1797SNate Lawson acpi_name(handle))); 112215e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 1123bc0f2195SMike Smith if (child == NULL) 1124bc0f2195SMike Smith break; 112515e32d5dSMike Smith acpi_set_handle(child, handle); 1126bc0f2195SMike Smith 1127e8b4d56eSNate Lawson /* Check if the device can generate wake events. */ 1128e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL))) 1129e8b4d56eSNate Lawson device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE); 1130e8b4d56eSNate Lawson 1131bc0f2195SMike Smith /* 1132b519f9d6SMike Smith * Check that the device is present. If it's not present, 1133b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1134b519f9d6SMike Smith * the handle, but we don't probe it). 1135b519f9d6SMike Smith */ 1136be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1137b519f9d6SMike Smith device_disable(child); 1138b519f9d6SMike Smith break; 1139b519f9d6SMike Smith } 1140b519f9d6SMike Smith 1141b519f9d6SMike Smith /* 1142bc0f2195SMike Smith * Get the device's resource settings and attach them. 1143bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1144bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1145bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1146bc0f2195SMike Smith * device not to have any resources. 1147bc0f2195SMike Smith */ 114872ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1149bc0f2195SMike Smith 1150be2b1797SNate Lawson /* If we're debugging, probe/attach now rather than later */ 1151b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 11520ae55423SMike Smith break; 115315e32d5dSMike Smith } 115415e32d5dSMike Smith } 1155be2b1797SNate Lawson 11560ae55423SMike Smith return_ACPI_STATUS (AE_OK); 115715e32d5dSMike Smith } 115815e32d5dSMike Smith 115915e32d5dSMike Smith static void 116015e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 116115e32d5dSMike Smith { 1162c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 1163c6a78e98STakanori Watanabe 1164cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1165cb9b0d80SMike Smith 116615e32d5dSMike Smith /* 11670ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 116815e32d5dSMike Smith * will be turned on again on some laptops. 116915e32d5dSMike Smith * 117015e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 117115e32d5dSMike Smith * before powering down, since we may still need ACPI during the 117215e32d5dSMike Smith * shutdown process. 117315e32d5dSMike Smith */ 1174c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 1175c6a78e98STakanori Watanabe acpi_Disable(sc); 117615e32d5dSMike Smith } 117715e32d5dSMike Smith 117815e32d5dSMike Smith static void 117915e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 118015e32d5dSMike Smith { 1181d33f4987STakanori Watanabe ACPI_STATUS status; 1182eeb3a05fSNate Lawson ACPI_ASSERTLOCK; 1183eeb3a05fSNate Lawson 1184eeb3a05fSNate Lawson /* 1185eeb3a05fSNate Lawson * If powering off, run the actual shutdown code on each processor. 1186eeb3a05fSNate Lawson * It will only perform the shutdown on the BSP. Some chipsets do 1187eeb3a05fSNate Lawson * not power off the system correctly if called from an AP. 1188eeb3a05fSNate Lawson */ 1189eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1190904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1191904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1192904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1193904bf0c2SNate Lawson AcpiFormatException(status)); 1194904bf0c2SNate Lawson return; 1195904bf0c2SNate Lawson } 1196eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 1197eeb3a05fSNate Lawson smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL); 1198eeb3a05fSNate Lawson } else { 1199eeb3a05fSNate Lawson printf("Shutting down ACPI\n"); 1200eeb3a05fSNate Lawson AcpiTerminate(); 1201eeb3a05fSNate Lawson } 1202eeb3a05fSNate Lawson } 1203eeb3a05fSNate Lawson 1204904bf0c2SNate Lawson /* 1205904bf0c2SNate Lawson * Since this function may be called with locks held or in an unknown 1206904bf0c2SNate Lawson * context, it cannot allocate memory, acquire locks, sleep, etc. 1207904bf0c2SNate Lawson */ 1208eeb3a05fSNate Lawson static void 1209eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg) 1210eeb3a05fSNate Lawson { 121115e32d5dSMike Smith ACPI_STATUS status; 121215e32d5dSMike Smith 1213cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1214cb9b0d80SMike Smith 1215eeb3a05fSNate Lawson /* Only attempt to power off if this is the BSP (cpuid 0). */ 1216eeb3a05fSNate Lawson if (PCPU_GET(cpuid) != 0) 1217eeb3a05fSNate Lawson return; 1218eeb3a05fSNate Lawson 121955398047SNate Lawson ACPI_DISABLE_IRQS(); 1220865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1221be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1222bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 122315e32d5dSMike Smith } else { 122415e32d5dSMike Smith DELAY(1000000); 122515e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 122615e32d5dSMike Smith } 122715e32d5dSMike Smith } 122815e32d5dSMike Smith 122913d4f7baSMitsuru IWASAKI static void 123013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 123113d4f7baSMitsuru IWASAKI { 123213d4f7baSMitsuru IWASAKI static int first_time = 1; 123313d4f7baSMitsuru IWASAKI 1234cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1235cb9b0d80SMike Smith 123613d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1237be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 123859ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 123913d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 124033febf93SNate Lawson acpi_event_power_button_sleep, sc); 1241be2b1797SNate Lawson if (first_time) 12426c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 124313d4f7baSMitsuru IWASAKI } 1244be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 124559ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 124613d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 124733febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1248be2b1797SNate Lawson if (first_time) 12496c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 125013d4f7baSMitsuru IWASAKI } 125113d4f7baSMitsuru IWASAKI 125213d4f7baSMitsuru IWASAKI first_time = 0; 125313d4f7baSMitsuru IWASAKI } 125413d4f7baSMitsuru IWASAKI 125515e32d5dSMike Smith /* 1256c5ba0be4SMike Smith * Returns true if the device is actually present and should 1257c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1258c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1259c5ba0be4SMike Smith */ 1260c5ba0be4SMike Smith BOOLEAN 1261c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1262c5ba0be4SMike Smith { 12631e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1264c5ba0be4SMike Smith ACPI_HANDLE h; 12651e4925e8SNate Lawson ACPI_BUFFER buf; 1266c5ba0be4SMike Smith ACPI_STATUS error; 12671e4925e8SNate Lawson int ret; 1268c5ba0be4SMike Smith 1269cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1270cb9b0d80SMike Smith 12711e4925e8SNate Lawson ret = FALSE; 1272c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1273c5ba0be4SMike Smith return (FALSE); 12741e4925e8SNate Lawson buf.Pointer = NULL; 12751e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 12766fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 12776fca9360SNate Lawson if (ACPI_FAILURE(error)) 1278c5ba0be4SMike Smith return (FALSE); 12791e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1280be2b1797SNate Lawson 1281be2b1797SNate Lawson /* If no _STA method, must be present */ 12821e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 12831e4925e8SNate Lawson ret = TRUE; 1284be2b1797SNate Lawson 1285be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 12861e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x9) == 0x9) 12871e4925e8SNate Lawson ret = TRUE; 1288be2b1797SNate Lawson 12891e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 12901e4925e8SNate Lawson return (ret); 1291c5ba0be4SMike Smith } 1292c5ba0be4SMike Smith 1293c5ba0be4SMike Smith /* 1294b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1295b53f2771SMike Smith */ 1296b53f2771SMike Smith BOOLEAN 1297b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1298b53f2771SMike Smith { 12991e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1300b53f2771SMike Smith ACPI_HANDLE h; 13011e4925e8SNate Lawson ACPI_BUFFER buf; 1302b53f2771SMike Smith ACPI_STATUS error; 13031e4925e8SNate Lawson int ret; 1304b53f2771SMike Smith 1305b53f2771SMike Smith ACPI_ASSERTLOCK; 1306b53f2771SMike Smith 13071e4925e8SNate Lawson ret = FALSE; 1308b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1309b53f2771SMike Smith return (FALSE); 13101e4925e8SNate Lawson buf.Pointer = NULL; 13111e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13126fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13136fca9360SNate Lawson if (ACPI_FAILURE(error)) 1314b53f2771SMike Smith return (FALSE); 13151e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1316be2b1797SNate Lawson 1317be2b1797SNate Lawson /* If no _STA method, must be present */ 13181e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 13191e4925e8SNate Lawson ret = TRUE; 1320be2b1797SNate Lawson 1321be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13221e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x19) == 0x19) 13231e4925e8SNate Lawson ret = TRUE; 1324be2b1797SNate Lawson 13251e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13261e4925e8SNate Lawson return (ret); 1327b53f2771SMike Smith } 1328b53f2771SMike Smith 1329b53f2771SMike Smith /* 133015e32d5dSMike Smith * Match a HID string against a device 133115e32d5dSMike Smith */ 133215e32d5dSMike Smith BOOLEAN 133315e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 133415e32d5dSMike Smith { 13351e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 133615e32d5dSMike Smith ACPI_HANDLE h; 13371e4925e8SNate Lawson ACPI_BUFFER buf; 133815e32d5dSMike Smith ACPI_STATUS error; 13391e4925e8SNate Lawson int ret, i; 134015e32d5dSMike Smith 1341cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1342cb9b0d80SMike Smith 13431e4925e8SNate Lawson ret = FALSE; 13444d332989SMike Smith if (hid == NULL) 134515e32d5dSMike Smith return (FALSE); 134615e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 134715e32d5dSMike Smith return (FALSE); 13481e4925e8SNate Lawson buf.Pointer = NULL; 13491e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13506fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13516fca9360SNate Lawson if (ACPI_FAILURE(error)) 135215e32d5dSMike Smith return (FALSE); 13531e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1354be2b1797SNate Lawson 1355c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1356c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 13571e4925e8SNate Lawson ret = TRUE; 1358c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 13591e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 13601e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 13611e4925e8SNate Lawson ret = TRUE; 13621e4925e8SNate Lawson break; 13631e4925e8SNate Lawson } 13641e4925e8SNate Lawson } 13651e4925e8SNate Lawson } 1366be2b1797SNate Lawson 13671e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13681e4925e8SNate Lawson return (ret); 136915e32d5dSMike Smith } 137015e32d5dSMike Smith 137115e32d5dSMike Smith /* 1372c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1373c5ba0be4SMike Smith * or one if its parents. 1374c5ba0be4SMike Smith */ 1375c5ba0be4SMike Smith ACPI_STATUS 1376c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1377c5ba0be4SMike Smith { 1378c5ba0be4SMike Smith ACPI_HANDLE r; 1379c5ba0be4SMike Smith ACPI_STATUS status; 1380c5ba0be4SMike Smith 1381cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1382cb9b0d80SMike Smith 1383be2b1797SNate Lawson /* Walk back up the tree to the root */ 1384c5ba0be4SMike Smith for (;;) { 1385be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1386be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1387c5ba0be4SMike Smith *result = r; 1388c5ba0be4SMike Smith return (AE_OK); 1389c5ba0be4SMike Smith } 1390c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1391c5ba0be4SMike Smith return (AE_OK); 1392b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1393c5ba0be4SMike Smith return (AE_NOT_FOUND); 1394c5ba0be4SMike Smith parent = r; 1395c5ba0be4SMike Smith } 1396c5ba0be4SMike Smith } 1397c5ba0be4SMike Smith 1398eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1399eea17c34SNate Lawson uint32_t 1400eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1401eea17c34SNate Lawson { 1402eea17c34SNate Lawson uint32_t delta; 1403eea17c34SNate Lawson 1404eea17c34SNate Lawson if (end >= start) 1405eea17c34SNate Lawson delta = end - start; 1406eea17c34SNate Lawson else if (AcpiGbl_FADT->TmrValExt == 0) 14078d01ceefSNate Lawson delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1408eea17c34SNate Lawson else 1409eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 1410eea17c34SNate Lawson return (delta); 1411eea17c34SNate Lawson } 1412eea17c34SNate Lawson 1413c5ba0be4SMike Smith /* 1414c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1415c5ba0be4SMike Smith */ 1416c5ba0be4SMike Smith ACPI_BUFFER * 1417c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1418c5ba0be4SMike Smith { 1419c5ba0be4SMike Smith ACPI_BUFFER *buf; 1420c5ba0be4SMike Smith 1421c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1422c5ba0be4SMike Smith return (NULL); 1423c5ba0be4SMike Smith buf->Length = size; 1424c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1425c5ba0be4SMike Smith return (buf); 1426c5ba0be4SMike Smith } 1427c5ba0be4SMike Smith 1428c310653eSNate Lawson ACPI_STATUS 1429cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1430c310653eSNate Lawson { 1431c310653eSNate Lawson ACPI_OBJECT arg1; 1432c310653eSNate Lawson ACPI_OBJECT_LIST args; 1433c310653eSNate Lawson 1434c310653eSNate Lawson ACPI_ASSERTLOCK; 1435c310653eSNate Lawson 1436c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1437c310653eSNate Lawson arg1.Integer.Value = number; 1438c310653eSNate Lawson args.Count = 1; 1439c310653eSNate Lawson args.Pointer = &arg1; 1440c310653eSNate Lawson 1441c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1442c310653eSNate Lawson } 1443c310653eSNate Lawson 1444c5ba0be4SMike Smith /* 1445c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1446c5ba0be4SMike Smith */ 1447c5ba0be4SMike Smith ACPI_STATUS 1448cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1449c5ba0be4SMike Smith { 1450be2b1797SNate Lawson ACPI_STATUS status; 1451c5ba0be4SMike Smith ACPI_BUFFER buf; 1452c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1453c5ba0be4SMike Smith 1454cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1455cb9b0d80SMike Smith 1456c5ba0be4SMike Smith if (handle == NULL) 1457c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 14580c7da7acSJohn Baldwin 14590c7da7acSJohn Baldwin /* 14600c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 14610c7da7acSJohn Baldwin * a method that will return an Integer. 14620c7da7acSJohn Baldwin */ 1463c5ba0be4SMike Smith buf.Pointer = ¶m; 1464c5ba0be4SMike Smith buf.Length = sizeof(param); 1465be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1466be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1467be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1468c5ba0be4SMike Smith *number = param.Integer.Value; 1469be2b1797SNate Lawson else 1470be2b1797SNate Lawson status = AE_TYPE; 1471c5ba0be4SMike Smith } 14720c7da7acSJohn Baldwin 14730c7da7acSJohn Baldwin /* 14740c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 14750c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 14760c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 14770c7da7acSJohn Baldwin * convert it into an Integer as best we can. 14780c7da7acSJohn Baldwin * 14790c7da7acSJohn Baldwin * This is a hack. 14800c7da7acSJohn Baldwin */ 1481be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1482b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1483be2b1797SNate Lawson status = AE_NO_MEMORY; 14840c7da7acSJohn Baldwin } else { 1485be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1486be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1487be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 14880c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 14890c7da7acSJohn Baldwin } 1490f97739daSNate Lawson } 1491be2b1797SNate Lawson return (status); 1492c5ba0be4SMike Smith } 1493c5ba0be4SMike Smith 1494c573e654SMitsuru IWASAKI ACPI_STATUS 1495cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1496c573e654SMitsuru IWASAKI { 1497c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1498dba55fa2SNate Lawson UINT8 *val; 1499c573e654SMitsuru IWASAKI int i; 1500c573e654SMitsuru IWASAKI 1501c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1502c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1503c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1504c573e654SMitsuru IWASAKI return (AE_OK); 1505c573e654SMitsuru IWASAKI } 1506c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1507c573e654SMitsuru IWASAKI return (AE_TYPE); 1508c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1509c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1510be2b1797SNate Lawson 1511c573e654SMitsuru IWASAKI *number = 0; 1512dba55fa2SNate Lawson val = p->Buffer.Pointer; 1513c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1514dba55fa2SNate Lawson *number += val[i] << (i * 8); 1515c573e654SMitsuru IWASAKI return (AE_OK); 1516c573e654SMitsuru IWASAKI } 1517c573e654SMitsuru IWASAKI 1518c5ba0be4SMike Smith /* 1519c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1520c5ba0be4SMike Smith * function for each element. 1521c5ba0be4SMike Smith * 1522c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1523c5ba0be4SMike Smith */ 1524c5ba0be4SMike Smith ACPI_STATUS 1525be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1526be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1527c5ba0be4SMike Smith { 1528c5ba0be4SMike Smith ACPI_OBJECT *comp; 1529c5ba0be4SMike Smith int i; 1530c5ba0be4SMike Smith 1531be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1532c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1533c5ba0be4SMike Smith 1534be2b1797SNate Lawson /* Iterate over components */ 1535be2b1797SNate Lawson i = 0; 1536be2b1797SNate Lawson comp = pkg->Package.Elements; 1537be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1538c5ba0be4SMike Smith func(comp, arg); 1539c5ba0be4SMike Smith 1540c5ba0be4SMike Smith return (AE_OK); 1541c5ba0be4SMike Smith } 1542c5ba0be4SMike Smith 15436f69255bSMike Smith /* 15446f69255bSMike Smith * Find the (index)th resource object in a set. 15456f69255bSMike Smith */ 15466f69255bSMike Smith ACPI_STATUS 15476d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 15486f69255bSMike Smith { 15496d63101aSMike Smith ACPI_RESOURCE *rp; 15506f69255bSMike Smith int i; 15516f69255bSMike Smith 15526d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 15536f69255bSMike Smith i = index; 15546d63101aSMike Smith while (i-- > 0) { 1555be2b1797SNate Lawson /* Range check */ 15566d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 15576f69255bSMike Smith return (AE_BAD_PARAMETER); 1558be2b1797SNate Lawson 1559be2b1797SNate Lawson /* Check for terminator */ 1560be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 15616d63101aSMike Smith return (AE_NOT_FOUND); 1562abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 15636f69255bSMike Smith } 15646f69255bSMike Smith if (resp != NULL) 15656d63101aSMike Smith *resp = rp; 1566be2b1797SNate Lawson 15676d63101aSMike Smith return (AE_OK); 15686d63101aSMike Smith } 15696d63101aSMike Smith 15706d63101aSMike Smith /* 15716d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 15726d63101aSMike Smith * 15736d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 15746d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 15756d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 15766d63101aSMike Smith * resources. 15776d63101aSMike Smith */ 15786d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 15796d63101aSMike Smith 15806d63101aSMike Smith ACPI_STATUS 15816d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 15826d63101aSMike Smith { 15836d63101aSMike Smith ACPI_RESOURCE *rp; 15846d63101aSMike Smith void *newp; 15856d63101aSMike Smith 1586be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 15876d63101aSMike Smith if (buf->Pointer == NULL) { 15886d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 15896d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 15906d63101aSMike Smith return (AE_NO_MEMORY); 15916d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 15926d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 15936d63101aSMike Smith rp->Length = 0; 15946d63101aSMike Smith } 15956d63101aSMike Smith if (res == NULL) 15966d63101aSMike Smith return (AE_OK); 15976d63101aSMike Smith 15986d63101aSMike Smith /* 15996d63101aSMike Smith * Scan the current buffer looking for the terminator. 16006d63101aSMike Smith * This will either find the terminator or hit the end 16016d63101aSMike Smith * of the buffer and return an error. 16026d63101aSMike Smith */ 16036d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16046d63101aSMike Smith for (;;) { 1605be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 16066d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16076d63101aSMike Smith return (AE_BAD_PARAMETER); 1608be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16096d63101aSMike Smith break; 1610abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16116d63101aSMike Smith } 16126d63101aSMike Smith 16136d63101aSMike Smith /* 16146d63101aSMike Smith * Check the size of the buffer and expand if required. 16156d63101aSMike Smith * 16166d63101aSMike Smith * Required size is: 16176d63101aSMike Smith * size of existing resources before terminator + 16186d63101aSMike Smith * size of new resource and header + 16196d63101aSMike Smith * size of terminator. 16206d63101aSMike Smith * 16216d63101aSMike Smith * Note that this loop should really only run once, unless 16226d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 16236d63101aSMike Smith */ 16246d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 16256d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 16266d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 16276d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 16286d63101aSMike Smith return (AE_NO_MEMORY); 16296d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1630a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1631a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 16326d63101aSMike Smith AcpiOsFree(buf->Pointer); 16336d63101aSMike Smith buf->Pointer = newp; 16346d63101aSMike Smith buf->Length += buf->Length; 16356d63101aSMike Smith } 16366d63101aSMike Smith 1637be2b1797SNate Lawson /* Insert the new resource. */ 16386d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 16396d63101aSMike Smith 1640be2b1797SNate Lawson /* And add the terminator. */ 1641abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16426d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 16436d63101aSMike Smith rp->Length = 0; 16446d63101aSMike Smith 16456f69255bSMike Smith return (AE_OK); 16466f69255bSMike Smith } 1647c5ba0be4SMike Smith 1648da14ac9fSJohn Baldwin /* 1649da14ac9fSJohn Baldwin * Set interrupt model. 1650da14ac9fSJohn Baldwin */ 1651da14ac9fSJohn Baldwin ACPI_STATUS 1652da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1653da14ac9fSJohn Baldwin { 1654da14ac9fSJohn Baldwin 1655c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1656da14ac9fSJohn Baldwin } 1657da14ac9fSJohn Baldwin 1658ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1659ece50487SMitsuru IWASAKI 1660ece50487SMitsuru IWASAKI static void 1661ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1662ece50487SMitsuru IWASAKI { 1663ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1664ece50487SMitsuru IWASAKI } 1665c30382dfSMitsuru IWASAKI 166615e32d5dSMike Smith /* 166715e32d5dSMike Smith * Set the system sleep state 166815e32d5dSMike Smith * 1669be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 167015e32d5dSMike Smith */ 167115e32d5dSMike Smith ACPI_STATUS 167215e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 167315e32d5dSMike Smith { 167415e32d5dSMike Smith ACPI_STATUS status = AE_OK; 167556d8cb57SMitsuru IWASAKI UINT8 TypeA; 167656d8cb57SMitsuru IWASAKI UINT8 TypeB; 167715e32d5dSMike Smith 1678b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1679cb9b0d80SMike Smith ACPI_ASSERTLOCK; 16800ae55423SMike Smith 168198175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 16826397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 168398175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 16846397624dSMitsuru IWASAKI 168598175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1686ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1687ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1688ece50487SMitsuru IWASAKI 168915e32d5dSMike Smith switch (state) { 169015e32d5dSMike Smith case ACPI_STATE_S1: 16916161544cSTakanori Watanabe case ACPI_STATE_S2: 16926161544cSTakanori Watanabe case ACPI_STATE_S3: 16936161544cSTakanori Watanabe case ACPI_STATE_S4: 1694be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 169598175614SNate Lawson if (status == AE_NOT_FOUND) { 169698175614SNate Lawson device_printf(sc->acpi_dev, 169798175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 169898175614SNate Lawson break; 169998175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1700be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1701be2b1797SNate Lawson AcpiFormatException(status)); 170256d8cb57SMitsuru IWASAKI break; 170356d8cb57SMitsuru IWASAKI } 170456d8cb57SMitsuru IWASAKI 1705a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1706a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1707a1fccb47SMitsuru IWASAKI 1708e8b4d56eSNate Lawson /* Disable all wake GPEs not appropriate for this state. */ 1709e8b4d56eSNate Lawson acpi_wake_limit_walk(state); 1710e8b4d56eSNate Lawson 1711be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 171215e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 171315e32d5dSMike Smith /* 171415e32d5dSMike Smith * Re-wake the system. 171515e32d5dSMike Smith * 171615e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 171715e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 171815e32d5dSMike Smith * current bus interface does not provide for this. 171915e32d5dSMike Smith */ 172015e32d5dSMike Smith DEVICE_RESUME(root_bus); 17210ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 172215e32d5dSMike Smith } 1723b9c780d6SMitsuru IWASAKI 1724be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1725be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1726b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1727b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1728b9c780d6SMitsuru IWASAKI break; 1729b9c780d6SMitsuru IWASAKI } 1730b9c780d6SMitsuru IWASAKI 1731be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1732ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1733ff01efb5SMitsuru IWASAKI 17346161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 17356161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 17366161544cSTakanori Watanabe 1737be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 173859ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 173959ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 174059ddeb18SNate Lawson 17416161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1742a1fccb47SMitsuru IWASAKI } 17436161544cSTakanori Watanabe 17446161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1745be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1746964679ceSMitsuru IWASAKI AcpiEnable(); 17476161544cSTakanori Watanabe } else { 1748be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1749be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1750be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1751be2b1797SNate Lawson AcpiFormatException(status)); 1752c30382dfSMitsuru IWASAKI break; 175315e32d5dSMike Smith } 17546161544cSTakanori Watanabe } 1755ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 175615e32d5dSMike Smith DEVICE_RESUME(root_bus); 175715e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 175813d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 175915e32d5dSMike Smith break; 176015e32d5dSMike Smith case ACPI_STATE_S5: 176115e32d5dSMike Smith /* 176215e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 176315e32d5dSMike Smith * shutdown handlers. 176415e32d5dSMike Smith */ 176515e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 176615e32d5dSMike Smith break; 176798175614SNate Lawson case ACPI_STATE_S0: 176815e32d5dSMike Smith default: 176915e32d5dSMike Smith status = AE_BAD_PARAMETER; 177015e32d5dSMike Smith break; 177115e32d5dSMike Smith } 1772ece50487SMitsuru IWASAKI 177398175614SNate Lawson /* Disable a second sleep request for a short period */ 1774ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1775ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1776ece50487SMitsuru IWASAKI 17770ae55423SMike Smith return_ACPI_STATUS (status); 177815e32d5dSMike Smith } 177915e32d5dSMike Smith 1780e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1781e8b4d56eSNate Lawson int 1782e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1783e8b4d56eSNate Lawson { 1784e8b4d56eSNate Lawson struct acpi_prw_data prw; 1785e8b4d56eSNate Lawson 1786e8b4d56eSNate Lawson /* Check that the device can wake the system. */ 1787e8b4d56eSNate Lawson if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0) 1788e8b4d56eSNate Lawson return (ENXIO); 1789e8b4d56eSNate Lawson 1790e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1791e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1792e8b4d56eSNate Lawson return (ENXIO); 1793e8b4d56eSNate Lawson 1794e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1795e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1796e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1797e8b4d56eSNate Lawson return (ENXIO); 1798e8b4d56eSNate Lawson } 1799e8b4d56eSNate Lawson 1800e8b4d56eSNate Lawson return (0); 1801e8b4d56eSNate Lawson } 1802e8b4d56eSNate Lawson 1803e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1804e8b4d56eSNate Lawson int 1805e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1806e8b4d56eSNate Lawson { 1807e8b4d56eSNate Lawson struct acpi_prw_data prw; 1808e8b4d56eSNate Lawson ACPI_HANDLE handle; 1809e8b4d56eSNate Lawson ACPI_STATUS status; 1810e8b4d56eSNate Lawson int flags; 1811e8b4d56eSNate Lawson 1812e8b4d56eSNate Lawson /* Make sure the device supports waking the system. */ 1813e8b4d56eSNate Lawson flags = device_get_flags(dev); 1814e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1815e8b4d56eSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1816e8b4d56eSNate Lawson return (ENXIO); 1817e8b4d56eSNate Lawson 1818e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1819e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1820e8b4d56eSNate Lawson return (ENXIO); 1821e8b4d56eSNate Lawson 1822e8b4d56eSNate Lawson if (enable) { 1823e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1824e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1825e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1826e8b4d56eSNate Lawson return (ENXIO); 1827e8b4d56eSNate Lawson } 1828e8b4d56eSNate Lawson device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1829e8b4d56eSNate Lawson } else { 1830e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1831e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1832e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1833e8b4d56eSNate Lawson return (ENXIO); 1834e8b4d56eSNate Lawson } 1835e8b4d56eSNate Lawson device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1836e8b4d56eSNate Lawson } 1837e8b4d56eSNate Lawson 1838e8b4d56eSNate Lawson return (0); 1839e8b4d56eSNate Lawson } 1840e8b4d56eSNate Lawson 1841e8b4d56eSNate Lawson /* Configure a device's GPE appropriately for the new sleep state. */ 1842e8b4d56eSNate Lawson int 1843e8b4d56eSNate Lawson acpi_wake_sleep_prep(device_t dev, int sstate) 1844e8b4d56eSNate Lawson { 1845e8b4d56eSNate Lawson struct acpi_prw_data prw; 1846e8b4d56eSNate Lawson ACPI_HANDLE handle; 1847cc85c78cSNate Lawson int flags; 1848e8b4d56eSNate Lawson 1849e8b4d56eSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1850cc85c78cSNate Lawson flags = device_get_flags(dev); 1851e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1852cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1853e8b4d56eSNate Lawson return (ENXIO); 1854cc85c78cSNate Lawson 1855cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1856e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1857e8b4d56eSNate Lawson return (ENXIO); 1858e8b4d56eSNate Lawson 1859e8b4d56eSNate Lawson /* 1860cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1861cc85c78cSNate Lawson * of the _PRW object are put into the ON state. 1862e8b4d56eSNate Lawson */ 1863cc85c78cSNate Lawson 1864cc85c78cSNate Lawson /* 1865cc85c78cSNate Lawson * If the user requested that this device wake the system and the next 1866cc85c78cSNate Lawson * sleep state is valid for this GPE, enable it and the device's wake 1867cc85c78cSNate Lawson * capability. The sleep state must be less than (i.e., higher power) 1868cc85c78cSNate Lawson * or equal to the value specified by _PRW. Return early, leaving 1869cc85c78cSNate Lawson * the appropriate power resources enabled. 1870cc85c78cSNate Lawson */ 1871cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 && 1872cc85c78cSNate Lawson sstate <= prw.lowest_wake) { 1873e8b4d56eSNate Lawson if (bootverbose) 1874cc85c78cSNate Lawson device_printf(dev, "wake_prep enabled gpe %#x for state %d\n", 1875e8b4d56eSNate Lawson prw.gpe_bit, sstate); 1876cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1877cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 1); 1878e8b4d56eSNate Lawson return (0); 1879e8b4d56eSNate Lawson } 1880e8b4d56eSNate Lawson 1881e8b4d56eSNate Lawson /* 1882cc85c78cSNate Lawson * If the device wake was disabled or this sleep state is too low for 1883cc85c78cSNate Lawson * this device, disable its wake capability and GPE. 1884e8b4d56eSNate Lawson */ 1885cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1886cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1887e8b4d56eSNate Lawson if (bootverbose) 1888cc85c78cSNate Lawson device_printf(dev, "wake_prep disabled gpe %#x for state %d\n", 1889cc85c78cSNate Lawson prw.gpe_bit, sstate); 1890cc85c78cSNate Lawson 1891cc85c78cSNate Lawson /* 1892cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1893cc85c78cSNate Lawson * of the _PRW object are put into the OFF state. 1894cc85c78cSNate Lawson */ 1895cc85c78cSNate Lawson 1896cc85c78cSNate Lawson return (0); 1897e8b4d56eSNate Lawson } 1898e8b4d56eSNate Lawson 1899cc85c78cSNate Lawson /* Re-enable GPEs after wake. */ 1900cc85c78cSNate Lawson int 1901cc85c78cSNate Lawson acpi_wake_run_prep(device_t dev) 1902cc85c78cSNate Lawson { 1903cc85c78cSNate Lawson struct acpi_prw_data prw; 1904cc85c78cSNate Lawson ACPI_HANDLE handle; 1905cc85c78cSNate Lawson int flags; 1906cc85c78cSNate Lawson 1907cc85c78cSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1908cc85c78cSNate Lawson flags = device_get_flags(dev); 1909cc85c78cSNate Lawson handle = acpi_get_handle(dev); 1910cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1911cc85c78cSNate Lawson return (ENXIO); 1912cc85c78cSNate Lawson 1913cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1914cc85c78cSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1915cc85c78cSNate Lawson return (ENXIO); 1916cc85c78cSNate Lawson 1917cc85c78cSNate Lawson /* 1918cc85c78cSNate Lawson * TBD: Be sure all Power Resources referenced by elements 2 through N 1919cc85c78cSNate Lawson * of the _PRW object are in the ON state. 1920cc85c78cSNate Lawson */ 1921cc85c78cSNate Lawson 1922cc85c78cSNate Lawson /* Disable wake capability and if the user requested, enable the GPE. */ 1923cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1924cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0) 1925cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1926e8b4d56eSNate Lawson return (0); 1927e8b4d56eSNate Lawson } 1928e8b4d56eSNate Lawson 1929e8b4d56eSNate Lawson static ACPI_STATUS 1930e8b4d56eSNate Lawson acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status) 1931e8b4d56eSNate Lawson { 1932e8b4d56eSNate Lawson struct acpi_prw_data prw; 193344b8ae71SNate Lawson int *sstate; 1934e8b4d56eSNate Lawson 1935e8b4d56eSNate Lawson /* It's ok not to have _PRW if the device can't wake the system. */ 1936e8b4d56eSNate Lawson if (acpi_parse_prw(h, &prw) != 0) 1937e8b4d56eSNate Lawson return (AE_OK); 1938e8b4d56eSNate Lawson 193944b8ae71SNate Lawson sstate = (int *)context; 194044b8ae71SNate Lawson if (*sstate > prw.lowest_wake) 1941e8b4d56eSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1942e8b4d56eSNate Lawson 1943e8b4d56eSNate Lawson return (AE_OK); 1944e8b4d56eSNate Lawson } 1945e8b4d56eSNate Lawson 1946e8b4d56eSNate Lawson /* Walk all system devices, disabling them if necessary for sstate. */ 1947e8b4d56eSNate Lawson static int 1948e8b4d56eSNate Lawson acpi_wake_limit_walk(int sstate) 1949e8b4d56eSNate Lawson { 1950e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 1951e8b4d56eSNate Lawson 1952e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 1953e8b4d56eSNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100, 195444b8ae71SNate Lawson acpi_wake_limit, &sstate, NULL); 1955e8b4d56eSNate Lawson return (0); 1956e8b4d56eSNate Lawson } 1957e8b4d56eSNate Lawson 195888a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 195988a79fc0SNate Lawson static int 196088a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 196188a79fc0SNate Lawson { 196288a79fc0SNate Lawson int error, i, numdevs; 196388a79fc0SNate Lawson device_t *devlist; 196488a79fc0SNate Lawson device_t child; 196588a79fc0SNate Lawson 196688a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 196788a79fc0SNate Lawson if (error != 0 || numdevs == 0) 196888a79fc0SNate Lawson return (error); 196988a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 197088a79fc0SNate Lawson child = devlist[i]; 197188a79fc0SNate Lawson if (!device_is_attached(child)) 197288a79fc0SNate Lawson continue; 197388a79fc0SNate Lawson if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) { 197488a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 197588a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 197688a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 197788a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 197888a79fc0SNate Lawson } 197988a79fc0SNate Lawson acpi_wake_sysctl_walk(child); 198088a79fc0SNate Lawson } 198188a79fc0SNate Lawson free(devlist, M_TEMP); 198288a79fc0SNate Lawson 198388a79fc0SNate Lawson return (0); 198488a79fc0SNate Lawson } 198588a79fc0SNate Lawson 198688a79fc0SNate Lawson /* Enable or disable wake from userland. */ 198788a79fc0SNate Lawson static int 198888a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 198988a79fc0SNate Lawson { 199088a79fc0SNate Lawson int enable, error; 199188a79fc0SNate Lawson device_t dev; 199288a79fc0SNate Lawson 199388a79fc0SNate Lawson dev = (device_t)arg1; 199488a79fc0SNate Lawson enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 199588a79fc0SNate Lawson 199688a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 199788a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 199888a79fc0SNate Lawson return (error); 199988a79fc0SNate Lawson if (enable != 0 && enable != 1) 200088a79fc0SNate Lawson return (EINVAL); 200188a79fc0SNate Lawson 200288a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 200388a79fc0SNate Lawson } 200488a79fc0SNate Lawson 2005e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2006e8b4d56eSNate Lawson static int 2007e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2008e8b4d56eSNate Lawson { 2009e8b4d56eSNate Lawson ACPI_STATUS status; 2010e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2011e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2012e8b4d56eSNate Lawson int error; 2013e8b4d56eSNate Lawson 2014e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2015e8b4d56eSNate Lawson return (EINVAL); 2016e8b4d56eSNate Lawson 2017e8b4d56eSNate Lawson /* 2018e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2019e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2020e8b4d56eSNate Lawson */ 2021e8b4d56eSNate Lawson error = EINVAL; 2022e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2023e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2024e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2025e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2026e8b4d56eSNate Lawson return (ENOENT); 2027e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2028e8b4d56eSNate Lawson if (res == NULL) 2029e8b4d56eSNate Lawson return (ENOENT); 2030e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2031e8b4d56eSNate Lawson goto out; 2032e8b4d56eSNate Lawson 2033e8b4d56eSNate Lawson /* 2034e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2035e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2036e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2037e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2038e8b4d56eSNate Lawson */ 2039e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2040e8b4d56eSNate Lawson goto out; 2041e8b4d56eSNate Lawson 2042e8b4d56eSNate Lawson /* 2043e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2044e8b4d56eSNate Lawson */ 2045e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2046e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2047e8b4d56eSNate Lawson /* 2048e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2049e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2050e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2051e8b4d56eSNate Lawson * enabled for the wake event. 2052e8b4d56eSNate Lawson */ 2053e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2054e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2055e8b4d56eSNate Lawson error = 0; 2056e8b4d56eSNate Lawson break; 2057e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2058e8b4d56eSNate Lawson /* 2059e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2060e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2061e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2062e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2063e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2064e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2065e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2066e8b4d56eSNate Lawson * the wake event. 2067e8b4d56eSNate Lawson * 2068e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2069e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2070e8b4d56eSNate Lawson */ 2071e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2072e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2073e8b4d56eSNate Lawson goto out; 2074e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2075e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2076e8b4d56eSNate Lawson goto out; 2077e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2078e8b4d56eSNate Lawson goto out; 2079e8b4d56eSNate Lawson error = 0; 2080e8b4d56eSNate Lawson break; 2081e8b4d56eSNate Lawson default: 2082e8b4d56eSNate Lawson goto out; 2083e8b4d56eSNate Lawson } 2084e8b4d56eSNate Lawson 2085e8b4d56eSNate Lawson /* XXX No power resource handling yet. */ 2086e8b4d56eSNate Lawson prw->power_res = NULL; 2087e8b4d56eSNate Lawson 2088e8b4d56eSNate Lawson out: 2089e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2090e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2091e8b4d56eSNate Lawson return (error); 2092e8b4d56eSNate Lawson } 2093e8b4d56eSNate Lawson 209415e32d5dSMike Smith /* 209515e32d5dSMike Smith * Enable/Disable ACPI 209615e32d5dSMike Smith */ 209715e32d5dSMike Smith ACPI_STATUS 209815e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 209915e32d5dSMike Smith { 210015e32d5dSMike Smith ACPI_STATUS status; 210115e32d5dSMike Smith u_int32_t flags; 210215e32d5dSMike Smith 2103b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2104cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21050ae55423SMike Smith 210615e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 210715e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 2108be2b1797SNate Lawson if (!sc->acpi_enabled) 210915e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 2110be2b1797SNate Lawson else 211115e32d5dSMike Smith status = AE_OK; 2112be2b1797SNate Lawson 211315e32d5dSMike Smith if (status == AE_OK) 211415e32d5dSMike Smith sc->acpi_enabled = 1; 2115be2b1797SNate Lawson 21160ae55423SMike Smith return_ACPI_STATUS (status); 211715e32d5dSMike Smith } 211815e32d5dSMike Smith 211915e32d5dSMike Smith ACPI_STATUS 212015e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 212115e32d5dSMike Smith { 212215e32d5dSMike Smith ACPI_STATUS status; 212315e32d5dSMike Smith 2124b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2125cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21260ae55423SMike Smith 2127be2b1797SNate Lawson if (sc->acpi_enabled) 212815e32d5dSMike Smith status = AcpiDisable(); 2129be2b1797SNate Lawson else 213015e32d5dSMike Smith status = AE_OK; 2131be2b1797SNate Lawson 213215e32d5dSMike Smith if (status == AE_OK) 213315e32d5dSMike Smith sc->acpi_enabled = 0; 2134be2b1797SNate Lawson 21350ae55423SMike Smith return_ACPI_STATUS (status); 213615e32d5dSMike Smith } 213715e32d5dSMike Smith 213815e32d5dSMike Smith /* 213915e32d5dSMike Smith * ACPI Event Handlers 214015e32d5dSMike Smith */ 214115e32d5dSMike Smith 214215e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 214315e32d5dSMike Smith 214415e32d5dSMike Smith static void 214515e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 214615e32d5dSMike Smith { 2147fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2148b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 214915e32d5dSMike Smith 2150cb9b0d80SMike Smith ACPI_LOCK; 2151a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 215215e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2153cb9b0d80SMike Smith ACPI_UNLOCK; 21540ae55423SMike Smith return_VOID; 215515e32d5dSMike Smith } 215615e32d5dSMike Smith 215715e32d5dSMike Smith static void 215815e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 215915e32d5dSMike Smith { 2160fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2161b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 21620ae55423SMike Smith 216315e32d5dSMike Smith /* Well, what to do? :-) */ 21640ae55423SMike Smith 2165cb9b0d80SMike Smith ACPI_LOCK; 2166cb9b0d80SMike Smith ACPI_UNLOCK; 2167cb9b0d80SMike Smith 21680ae55423SMike Smith return_VOID; 216915e32d5dSMike Smith } 217015e32d5dSMike Smith 217115e32d5dSMike Smith /* 217215e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 217315e32d5dSMike Smith */ 217415e32d5dSMike Smith UINT32 217533febf93SNate Lawson acpi_event_power_button_sleep(void *context) 217615e32d5dSMike Smith { 217715e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 217815e32d5dSMike Smith 2179b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 21800ae55423SMike Smith 218115e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 21820ae55423SMike Smith 2183b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 218415e32d5dSMike Smith } 218515e32d5dSMike Smith 218615e32d5dSMike Smith UINT32 218733febf93SNate Lawson acpi_event_power_button_wake(void *context) 218815e32d5dSMike Smith { 218915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 219015e32d5dSMike Smith 2191b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 21920ae55423SMike Smith 219315e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 21940ae55423SMike Smith 2195b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 219615e32d5dSMike Smith } 219715e32d5dSMike Smith 219815e32d5dSMike Smith UINT32 219933febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 220015e32d5dSMike Smith { 220115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 220215e32d5dSMike Smith 2203b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22040ae55423SMike Smith 220515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 22060ae55423SMike Smith 2207b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 220815e32d5dSMike Smith } 220915e32d5dSMike Smith 221015e32d5dSMike Smith UINT32 221133febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 221215e32d5dSMike Smith { 221315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 221415e32d5dSMike Smith 2215b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22160ae55423SMike Smith 221715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 22180ae55423SMike Smith 2219b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 222015e32d5dSMike Smith } 222115e32d5dSMike Smith 222215e32d5dSMike Smith /* 222315e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 222415e32d5dSMike Smith */ 222515e32d5dSMike Smith struct acpi_staticbuf { 222615e32d5dSMike Smith ACPI_BUFFER buffer; 222715e32d5dSMike Smith char data[512]; 222815e32d5dSMike Smith }; 222915e32d5dSMike Smith 223015e32d5dSMike Smith char * 223115e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 223215e32d5dSMike Smith { 223315e32d5dSMike Smith static struct acpi_staticbuf buf; 223415e32d5dSMike Smith 2235cb9b0d80SMike Smith ACPI_ASSERTLOCK; 2236cb9b0d80SMike Smith 223715e32d5dSMike Smith buf.buffer.Length = 512; 223815e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 223915e32d5dSMike Smith 2240b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 224115e32d5dSMike Smith return (buf.buffer.Pointer); 2242be2b1797SNate Lawson 224315e32d5dSMike Smith return ("(unknown path)"); 224415e32d5dSMike Smith } 224515e32d5dSMike Smith 224615e32d5dSMike Smith /* 224715e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 224815e32d5dSMike Smith * parts of the namespace. 224915e32d5dSMike Smith */ 225015e32d5dSMike Smith int 225115e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 225215e32d5dSMike Smith { 22533c600cfbSJohn Baldwin char *cp, *env, *np; 225415e32d5dSMike Smith int len; 225515e32d5dSMike Smith 225615e32d5dSMike Smith np = acpi_name(handle); 225715e32d5dSMike Smith if (*np == '\\') 225815e32d5dSMike Smith np++; 22593c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 226015e32d5dSMike Smith return (0); 226115e32d5dSMike Smith 2262be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 22633c600cfbSJohn Baldwin cp = env; 226415e32d5dSMike Smith for (;;) { 226515e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 226615e32d5dSMike Smith cp++; 226715e32d5dSMike Smith if (*cp == 0) 226815e32d5dSMike Smith break; 226915e32d5dSMike Smith len = 0; 227015e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 227115e32d5dSMike Smith len++; 2272d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 22733c600cfbSJohn Baldwin freeenv(env); 22740ae55423SMike Smith return(1); 2275d786139cSMaxime Henrion } 22760ae55423SMike Smith cp += len; 22770ae55423SMike Smith } 22783c600cfbSJohn Baldwin freeenv(env); 2279be2b1797SNate Lawson 22800ae55423SMike Smith return (0); 22810ae55423SMike Smith } 22820ae55423SMike Smith 22830ae55423SMike Smith /* 22840ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 22850ae55423SMike Smith */ 22860ae55423SMike Smith int 22870ae55423SMike Smith acpi_disabled(char *subsys) 22880ae55423SMike Smith { 228978689b15SMaxime Henrion char *cp, *env; 22900ae55423SMike Smith int len; 22910ae55423SMike Smith 22923184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 22930ae55423SMike Smith return (0); 22943184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 229578689b15SMaxime Henrion freeenv(env); 22960ae55423SMike Smith return (1); 2297d786139cSMaxime Henrion } 22980ae55423SMike Smith 22993184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 230078689b15SMaxime Henrion cp = env; 23010ae55423SMike Smith for (;;) { 23023184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 23030ae55423SMike Smith cp++; 23043184cf5aSNate Lawson if (*cp == '\0') 23050ae55423SMike Smith break; 23060ae55423SMike Smith len = 0; 23073184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 23080ae55423SMike Smith len++; 23093184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 231078689b15SMaxime Henrion freeenv(env); 231115e32d5dSMike Smith return (1); 2312d786139cSMaxime Henrion } 231315e32d5dSMike Smith cp += len; 231415e32d5dSMike Smith } 231578689b15SMaxime Henrion freeenv(env); 2316be2b1797SNate Lawson 231715e32d5dSMike Smith return (0); 231815e32d5dSMike Smith } 231915e32d5dSMike Smith 232015e32d5dSMike Smith /* 232115e32d5dSMike Smith * Control interface. 232215e32d5dSMike Smith * 23230ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2324be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2325be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 232615e32d5dSMike Smith */ 23270ae55423SMike Smith struct acpi_ioctl_hook 23280ae55423SMike Smith { 23290ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 23300ae55423SMike Smith u_long cmd; 2331be2b1797SNate Lawson acpi_ioctl_fn fn; 23320ae55423SMike Smith void *arg; 23330ae55423SMike Smith }; 23340ae55423SMike Smith 23350ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 23360ae55423SMike Smith static int acpi_ioctl_hooks_initted; 23370ae55423SMike Smith 23380ae55423SMike Smith /* 23390ae55423SMike Smith * Register an ioctl handler. 23400ae55423SMike Smith */ 23410ae55423SMike Smith int 2342be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 23430ae55423SMike Smith { 23440ae55423SMike Smith struct acpi_ioctl_hook *hp; 23450ae55423SMike Smith 23460ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 23470ae55423SMike Smith return (ENOMEM); 23480ae55423SMike Smith hp->cmd = cmd; 23490ae55423SMike Smith hp->fn = fn; 23500ae55423SMike Smith hp->arg = arg; 23510ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 23520ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 23530ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 23540ae55423SMike Smith } 23550ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 23560ae55423SMike Smith return (0); 23570ae55423SMike Smith } 23580ae55423SMike Smith 23590ae55423SMike Smith /* 23600ae55423SMike Smith * Deregister an ioctl handler. 23610ae55423SMike Smith */ 23620ae55423SMike Smith void 2363be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 23640ae55423SMike Smith { 23650ae55423SMike Smith struct acpi_ioctl_hook *hp; 23660ae55423SMike Smith 23670ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 23680ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 23690ae55423SMike Smith break; 23700ae55423SMike Smith 23710ae55423SMike Smith if (hp != NULL) { 23720ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 23730ae55423SMike Smith free(hp, M_ACPIDEV); 23740ae55423SMike Smith } 23750ae55423SMike Smith } 23760ae55423SMike Smith 237715e32d5dSMike Smith static int 23786b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 237915e32d5dSMike Smith { 238015e32d5dSMike Smith return (0); 238115e32d5dSMike Smith } 238215e32d5dSMike Smith 238315e32d5dSMike Smith static int 23846b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 238515e32d5dSMike Smith { 238615e32d5dSMike Smith return (0); 238715e32d5dSMike Smith } 238815e32d5dSMike Smith 238915e32d5dSMike Smith static int 23906b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 239115e32d5dSMike Smith { 239215e32d5dSMike Smith struct acpi_softc *sc; 23930ae55423SMike Smith struct acpi_ioctl_hook *hp; 23940ae55423SMike Smith int error, xerror, state; 2395fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 239615e32d5dSMike Smith 2397cb9b0d80SMike Smith ACPI_LOCK; 2398cb9b0d80SMike Smith 239915e32d5dSMike Smith error = state = 0; 240015e32d5dSMike Smith sc = dev->si_drv1; 240115e32d5dSMike Smith 24020ae55423SMike Smith /* 24030ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 24040ae55423SMike Smith */ 24050ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 24060ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 24070ae55423SMike Smith if (hp->cmd == cmd) { 24080ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 24090ae55423SMike Smith if (xerror != 0) 24100ae55423SMike Smith error = xerror; 2411917d44c8SMitsuru IWASAKI goto out; 24120ae55423SMike Smith } 24130ae55423SMike Smith } 24140ae55423SMike Smith } 24150ae55423SMike Smith 24160ae55423SMike Smith /* 2417a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2418a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2419a89fcf28STakanori Watanabe * Not changing system behavior. 2420a89fcf28STakanori Watanabe */ 2421be2b1797SNate Lawson if((flag & FWRITE) == 0) 2422be2b1797SNate Lawson return (EPERM); 2423a89fcf28STakanori Watanabe 2424be2b1797SNate Lawson /* Core system ioctls. */ 242515e32d5dSMike Smith switch (cmd) { 242615e32d5dSMike Smith case ACPIIO_ENABLE: 24270ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 242815e32d5dSMike Smith error = ENXIO; 242915e32d5dSMike Smith break; 243015e32d5dSMike Smith case ACPIIO_DISABLE: 24310ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 243215e32d5dSMike Smith error = ENXIO; 243315e32d5dSMike Smith break; 243415e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 243515e32d5dSMike Smith if (!sc->acpi_enabled) { 243615e32d5dSMike Smith error = ENXIO; 243715e32d5dSMike Smith break; 243815e32d5dSMike Smith } 243915e32d5dSMike Smith state = *(int *)addr; 24402d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 24412d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 244215e32d5dSMike Smith error = EINVAL; 24432d610c46SNate Lawson } else { 24442d610c46SNate Lawson error = EINVAL; 24452d610c46SNate Lawson } 244615e32d5dSMike Smith break; 244715e32d5dSMike Smith default: 24480ae55423SMike Smith if (error == 0) 244915e32d5dSMike Smith error = EINVAL; 245015e32d5dSMike Smith break; 245115e32d5dSMike Smith } 2452917d44c8SMitsuru IWASAKI 2453917d44c8SMitsuru IWASAKI out: 2454cb9b0d80SMike Smith ACPI_UNLOCK; 245515e32d5dSMike Smith return (error); 245615e32d5dSMike Smith } 245715e32d5dSMike Smith 24581d073b1dSJohn Baldwin static int 2459d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2460d75de536SMitsuru IWASAKI { 2461d75de536SMitsuru IWASAKI char sleep_state[4]; 2462d75de536SMitsuru IWASAKI char buf[16]; 2463d75de536SMitsuru IWASAKI int error; 2464d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2465d75de536SMitsuru IWASAKI 2466d75de536SMitsuru IWASAKI buf[0] = '\0'; 2467d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) { 2468d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2469d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2470d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2471d75de536SMitsuru IWASAKI } 2472d75de536SMitsuru IWASAKI } 2473d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2474d75de536SMitsuru IWASAKI return (error); 2475d75de536SMitsuru IWASAKI } 2476d75de536SMitsuru IWASAKI 2477d75de536SMitsuru IWASAKI static int 24781d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 24791d073b1dSJohn Baldwin { 24801d073b1dSJohn Baldwin char sleep_state[10]; 24811d073b1dSJohn Baldwin int error; 24821d073b1dSJohn Baldwin u_int new_state, old_state; 24831d073b1dSJohn Baldwin 24841d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2485b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX + 1) { 24861d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2487cb9b0d80SMike Smith } else { 2488b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 24891d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 24901d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2491cb9b0d80SMike Smith } 24921d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 24931d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2494be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2495be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 24961d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 24971d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 24981d073b1dSJohn Baldwin break; 2499cb9b0d80SMike Smith } 2500931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2501be2b1797SNate Lawson if (new_state != old_state) 25021d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2503cb9b0d80SMike Smith } else { 25041d073b1dSJohn Baldwin error = EINVAL; 25051d073b1dSJohn Baldwin } 2506cb9b0d80SMike Smith } 2507be2b1797SNate Lawson 25081d073b1dSJohn Baldwin return (error); 25091d073b1dSJohn Baldwin } 25101d073b1dSJohn Baldwin 25119b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 25129b937d48SNate Lawson void 25139b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 25149b937d48SNate Lawson { 25159b937d48SNate Lawson char notify_buf[16]; 25169b937d48SNate Lawson ACPI_BUFFER handle_buf; 25179b937d48SNate Lawson ACPI_STATUS status; 25189b937d48SNate Lawson 25199b937d48SNate Lawson if (subsystem == NULL) 25209b937d48SNate Lawson return; 25219b937d48SNate Lawson 25229b937d48SNate Lawson handle_buf.Pointer = NULL; 25239b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 25249b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 25259b937d48SNate Lawson if (ACPI_FAILURE(status)) 25269b937d48SNate Lawson return; 25279b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 25289b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 25299b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 25309b937d48SNate Lawson } 25319b937d48SNate Lawson 253215e32d5dSMike Smith #ifdef ACPI_DEBUG 25330ae55423SMike Smith /* 25340ae55423SMike Smith * Support for parsing debug options from the kernel environment. 25350ae55423SMike Smith * 25360ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 25370ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 25380ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 25390ae55423SMike Smith * prefixing the bit name with !. 25400ae55423SMike Smith */ 254115e32d5dSMike Smith struct debugtag 254215e32d5dSMike Smith { 254315e32d5dSMike Smith char *name; 254415e32d5dSMike Smith UINT32 value; 254515e32d5dSMike Smith }; 254615e32d5dSMike Smith 254715e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2548c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2549c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2550c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2551c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2552c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2553c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2554c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2555c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2556c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2557d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 25584c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2559d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2560297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 25614c1cdee6SMike Smith 2562c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2563c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 25643184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2565c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 25663184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 25673184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 25683184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 25694c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2570cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 25713184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2572b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 257315e32d5dSMike Smith {NULL, 0} 257415e32d5dSMike Smith }; 257515e32d5dSMike Smith 257615e32d5dSMike Smith static struct debugtag dbg_level[] = { 25774c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 25789501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 25799501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 25804c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 25819501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 25824c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2583d62ab2f4SMitsuru IWASAKI 2584d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2585297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 25864c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 25874c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2588d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 25894c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 25904c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 25914c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 25924c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 25934c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 25944c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 25954c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 25964c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 25974c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 25984c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2599d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2600d62ab2f4SMitsuru IWASAKI 2601d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2602d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2603d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2604d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2605d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 26064c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2607d62ab2f4SMitsuru IWASAKI 2608d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2609d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2610d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2611d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2612d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2613d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2614d62ab2f4SMitsuru IWASAKI 2615d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 261698479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 261798479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 261898479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 261998479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 262098479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 262115e32d5dSMike Smith {NULL, 0} 262215e32d5dSMike Smith }; 262315e32d5dSMike Smith 262415e32d5dSMike Smith static void 262515e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 262615e32d5dSMike Smith { 262715e32d5dSMike Smith char *ep; 262815e32d5dSMike Smith int i, l; 26290ae55423SMike Smith int set; 263015e32d5dSMike Smith 263115e32d5dSMike Smith while (*cp) { 263215e32d5dSMike Smith if (isspace(*cp)) { 263315e32d5dSMike Smith cp++; 263415e32d5dSMike Smith continue; 263515e32d5dSMike Smith } 263615e32d5dSMike Smith ep = cp; 263715e32d5dSMike Smith while (*ep && !isspace(*ep)) 263815e32d5dSMike Smith ep++; 26390ae55423SMike Smith if (*cp == '!') { 26400ae55423SMike Smith set = 0; 26410ae55423SMike Smith cp++; 26420ae55423SMike Smith if (cp == ep) 26430ae55423SMike Smith continue; 26440ae55423SMike Smith } else { 26450ae55423SMike Smith set = 1; 26460ae55423SMike Smith } 264715e32d5dSMike Smith l = ep - cp; 264815e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 264915e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2650be2b1797SNate Lawson if (set) 265115e32d5dSMike Smith *flag |= tag[i].value; 2652be2b1797SNate Lawson else 26530ae55423SMike Smith *flag &= ~tag[i].value; 265415e32d5dSMike Smith } 265515e32d5dSMike Smith } 265615e32d5dSMike Smith cp = ep; 265715e32d5dSMike Smith } 265815e32d5dSMike Smith } 265915e32d5dSMike Smith 266015e32d5dSMike Smith static void 26612a4ac806SMike Smith acpi_set_debugging(void *junk) 266215e32d5dSMike Smith { 26637e639165SNate Lawson char *layer, *level; 266415e32d5dSMike Smith 26651d7b121cSNate Lawson if (cold) { 26660ae55423SMike Smith AcpiDbgLayer = 0; 26670ae55423SMike Smith AcpiDbgLevel = 0; 26681d7b121cSNate Lawson } 26691d7b121cSNate Lawson 26707e639165SNate Lawson layer = getenv("debug.acpi.layer"); 26717e639165SNate Lawson level = getenv("debug.acpi.level"); 26727e639165SNate Lawson if (layer == NULL && level == NULL) 26737e639165SNate Lawson return; 26740ae55423SMike Smith 26757e639165SNate Lawson printf("ACPI set debug"); 26767e639165SNate Lawson if (layer != NULL) { 26777e639165SNate Lawson if (strcmp("NONE", layer) != 0) 26787e639165SNate Lawson printf(" layer '%s'", layer); 26797e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 26807e639165SNate Lawson freeenv(layer); 26811d7b121cSNate Lawson } 26827e639165SNate Lawson if (level != NULL) { 26837e639165SNate Lawson if (strcmp("NONE", level) != 0) 26847e639165SNate Lawson printf(" level '%s'", level); 26857e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 26867e639165SNate Lawson freeenv(level); 26877e639165SNate Lawson } 26887e639165SNate Lawson printf("\n"); 268915e32d5dSMike Smith } 2690be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2691be2b1797SNate Lawson NULL); 26921d7b121cSNate Lawson 26931d7b121cSNate Lawson static int 26941d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 26951d7b121cSNate Lawson { 269654f6bca0SNate Lawson int error, *dbg; 26971d7b121cSNate Lawson struct debugtag *tag; 269854f6bca0SNate Lawson struct sbuf sb; 26991d7b121cSNate Lawson 270054f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 270154f6bca0SNate Lawson return (ENOMEM); 27021d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 27031d7b121cSNate Lawson tag = &dbg_layer[0]; 27041d7b121cSNate Lawson dbg = &AcpiDbgLayer; 27051d7b121cSNate Lawson } else { 27061d7b121cSNate Lawson tag = &dbg_level[0]; 27071d7b121cSNate Lawson dbg = &AcpiDbgLevel; 27081d7b121cSNate Lawson } 27091d7b121cSNate Lawson 27101d7b121cSNate Lawson /* Get old values if this is a get request. */ 27111d7b121cSNate Lawson if (*dbg == 0) { 271254f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 27131d7b121cSNate Lawson } else if (req->newptr == NULL) { 27141d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 271554f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 271654f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 27171d7b121cSNate Lawson } 27181d7b121cSNate Lawson } 271954f6bca0SNate Lawson sbuf_trim(&sb); 272054f6bca0SNate Lawson sbuf_finish(&sb); 27211d7b121cSNate Lawson 27227e639165SNate Lawson /* Copy out the old values to the user. */ 27237e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 272454f6bca0SNate Lawson sbuf_delete(&sb); 27251d7b121cSNate Lawson 27261d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 27271d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 27281d7b121cSNate Lawson *dbg = 0; 27291d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 27301d7b121cSNate Lawson acpi_set_debugging(NULL); 27311d7b121cSNate Lawson } 27321d7b121cSNate Lawson 27331d7b121cSNate Lawson return (error); 27341d7b121cSNate Lawson } 27351d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 27361d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 27371d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 27381d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 273915e32d5dSMike Smith #endif 2740f9390180SMitsuru IWASAKI 2741f9390180SMitsuru IWASAKI static int 2742f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2743f9390180SMitsuru IWASAKI { 2744f9390180SMitsuru IWASAKI int state, acpi_state; 2745f9390180SMitsuru IWASAKI int error; 2746f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2747f9390180SMitsuru IWASAKI va_list ap; 2748f9390180SMitsuru IWASAKI 2749f9390180SMitsuru IWASAKI error = 0; 2750f9390180SMitsuru IWASAKI switch (cmd) { 2751f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2752f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2753f9390180SMitsuru IWASAKI if (sc == NULL) { 2754f9390180SMitsuru IWASAKI error = EINVAL; 2755f9390180SMitsuru IWASAKI goto out; 2756f9390180SMitsuru IWASAKI } 2757f9390180SMitsuru IWASAKI 2758f9390180SMitsuru IWASAKI va_start(ap, arg); 2759f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2760f9390180SMitsuru IWASAKI va_end(ap); 2761f9390180SMitsuru IWASAKI 2762f9390180SMitsuru IWASAKI switch (state) { 2763f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2764f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2765f9390180SMitsuru IWASAKI break; 2766f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2767f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2768f9390180SMitsuru IWASAKI break; 2769f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2770f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2771f9390180SMitsuru IWASAKI break; 2772f9390180SMitsuru IWASAKI default: 2773f9390180SMitsuru IWASAKI error = EINVAL; 2774f9390180SMitsuru IWASAKI goto out; 2775f9390180SMitsuru IWASAKI } 2776f9390180SMitsuru IWASAKI 2777f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2778f9390180SMitsuru IWASAKI break; 2779f9390180SMitsuru IWASAKI default: 2780f9390180SMitsuru IWASAKI error = EINVAL; 2781f9390180SMitsuru IWASAKI goto out; 2782f9390180SMitsuru IWASAKI } 2783f9390180SMitsuru IWASAKI 2784f9390180SMitsuru IWASAKI out: 2785f9390180SMitsuru IWASAKI return (error); 2786f9390180SMitsuru IWASAKI } 2787f9390180SMitsuru IWASAKI 2788f9390180SMitsuru IWASAKI static void 2789f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2790f9390180SMitsuru IWASAKI { 2791be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 27926c407052SMitsuru IWASAKI return; 2793f9390180SMitsuru IWASAKI 2794f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2795f9390180SMitsuru IWASAKI } 2796f9390180SMitsuru IWASAKI 2797f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2798