115e32d5dSMike Smith /*- 215e32d5dSMike Smith * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 315e32d5dSMike Smith * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4cb9b0d80SMike Smith * Copyright (c) 2000, 2001 Michael Smith 515e32d5dSMike Smith * Copyright (c) 2000 BSDi 615e32d5dSMike Smith * All rights reserved. 715e32d5dSMike Smith * 815e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without 915e32d5dSMike Smith * modification, are permitted provided that the following conditions 1015e32d5dSMike Smith * are met: 1115e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright 1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer. 1315e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1415e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the 1515e32d5dSMike Smith * documentation and/or other materials provided with the distribution. 1615e32d5dSMike Smith * 1715e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1815e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1915e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2015e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2115e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2215e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2315e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2415e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2515e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2615e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2715e32d5dSMike Smith * SUCH DAMAGE. 2815e32d5dSMike Smith * 2915e32d5dSMike Smith * $FreeBSD$ 3015e32d5dSMike Smith */ 3115e32d5dSMike Smith 3215e32d5dSMike Smith #include "opt_acpi.h" 3315e32d5dSMike Smith #include <sys/param.h> 3415e32d5dSMike Smith #include <sys/kernel.h> 35fb919e4dSMark Murray #include <sys/proc.h> 36a89fcf28STakanori Watanabe #include <sys/fcntl.h> 3715e32d5dSMike Smith #include <sys/malloc.h> 3815e32d5dSMike Smith #include <sys/bus.h> 3915e32d5dSMike Smith #include <sys/conf.h> 4015e32d5dSMike Smith #include <sys/ioccom.h> 4115e32d5dSMike Smith #include <sys/reboot.h> 4215e32d5dSMike Smith #include <sys/sysctl.h> 4315e32d5dSMike Smith #include <sys/ctype.h> 441611ea87SMitsuru IWASAKI #include <sys/linker.h> 45f9390180SMitsuru IWASAKI #include <sys/power.h> 4654f6bca0SNate Lawson #include <sys/sbuf.h> 47eeb3a05fSNate Lawson #include <sys/smp.h> 4815e32d5dSMike Smith 4915e32d5dSMike Smith #include <machine/clock.h> 5015e32d5dSMike Smith #include <machine/resource.h> 51dc750869SNate Lawson #include <machine/bus.h> 52dc750869SNate Lawson #include <sys/rman.h> 53bc0f2195SMike Smith #include <isa/isavar.h> 54bc0f2195SMike Smith 5515e32d5dSMike Smith #include "acpi.h" 5615e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5715e32d5dSMike Smith #include <dev/acpica/acpiio.h> 589b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h> 5915e32d5dSMike Smith 6015e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 6115e32d5dSMike Smith 62be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 63cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 64b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 650ae55423SMike Smith 6615e32d5dSMike Smith static d_open_t acpiopen; 6715e32d5dSMike Smith static d_close_t acpiclose; 6815e32d5dSMike Smith static d_ioctl_t acpiioctl; 6915e32d5dSMike Smith 7015e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 71dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 72dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 737ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 747ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 757ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 767ac40f5fSPoul-Henning Kamp .d_name = "acpi", 7715e32d5dSMike Smith }; 7815e32d5dSMike Smith 79fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 80cb9b0d80SMike Smith struct mtx acpi_mutex; 81fc0ea94aSJohn Baldwin #endif 82cb9b0d80SMike Smith 833184cf5aSNate Lawson struct acpi_quirks { 843184cf5aSNate Lawson char *OemId; 853184cf5aSNate Lawson uint32_t OemRevision; 863184cf5aSNate Lawson char *value; 873184cf5aSNate Lawson }; 883184cf5aSNate Lawson 893184cf5aSNate Lawson #define ACPI_OEM_REV_ANY 0 903184cf5aSNate Lawson 913184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = { 923184cf5aSNate Lawson #ifdef notyet 933184cf5aSNate Lawson /* Bad PCI routing table. Used on some SuperMicro boards. */ 943184cf5aSNate Lawson { "PTLTD ", 0x06040000, "pci_link" }, 953184cf5aSNate Lawson #endif 963184cf5aSNate Lawson 973184cf5aSNate Lawson { NULL, 0, NULL } 983184cf5aSNate Lawson }; 993184cf5aSNate Lawson 1006d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10115e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 10215e32d5dSMike Smith static int acpi_probe(device_t dev); 10315e32d5dSMike Smith static int acpi_attach(device_t dev); 1043184cf5aSNate Lawson static void acpi_quirks_set(void); 105be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 106be2b1797SNate Lawson int unit); 10715e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 108be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 109be2b1797SNate Lawson uintptr_t *result); 110be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 111be2b1797SNate Lawson uintptr_t value); 112be2b1797SNate Lawson static int acpi_set_resource(device_t dev, device_t child, int type, 113be2b1797SNate Lawson int rid, u_long start, u_long count); 114be2b1797SNate Lawson static int acpi_get_resource(device_t dev, device_t child, int type, 115be2b1797SNate Lawson int rid, u_long *startp, u_long *countp); 116be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 117be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 118be2b1797SNate Lawson u_long count, u_int flags); 119be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 120be2b1797SNate Lawson int rid, struct resource *r); 1211e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1221e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 123be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 124be2b1797SNate Lawson struct isa_pnp_id *ids); 12515e32d5dSMike Smith static void acpi_probe_children(device_t bus); 126be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 127be2b1797SNate Lawson void *context, void **status); 12815e32d5dSMike Smith static void acpi_shutdown_pre_sync(void *arg, int howto); 12915e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 130eeb3a05fSNate Lawson static void acpi_shutdown_poweroff(void *arg); 13113d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 132e8b4d56eSNate Lawson static int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw); 133e8b4d56eSNate Lawson static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, 134e8b4d56eSNate Lawson void **status); 135e8b4d56eSNate Lawson static int acpi_wake_limit_walk(int sstate); 13688a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 13788a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 13815e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 13915e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 140d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1411d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 142f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 143879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 144879d6c23STakanori Watanabe char *buf, size_t buflen); 145879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 146879d6c23STakanori Watanabe char *buf, size_t buflen); 147879d6c23STakanori Watanabe 14815e32d5dSMike Smith static device_method_t acpi_methods[] = { 14915e32d5dSMike Smith /* Device interface */ 15015e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 15115e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 15215e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 15356a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 15415e32d5dSMike Smith DEVMETHOD(device_shutdown, bus_generic_shutdown), 15515e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 15615e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 15715e32d5dSMike Smith 15815e32d5dSMike Smith /* Bus interface */ 15915e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 16015e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 16115e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 16215e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 16315e32d5dSMike Smith DEVMETHOD(bus_set_resource, acpi_set_resource), 16415e32d5dSMike Smith DEVMETHOD(bus_get_resource, acpi_get_resource), 16515e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 16615e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 167879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 168879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 16915e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 17015e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 17115e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 17215e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 17315e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 17415e32d5dSMike Smith 175bc0f2195SMike Smith /* ISA emulation */ 176bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 177bc0f2195SMike Smith 17815e32d5dSMike Smith {0, 0} 17915e32d5dSMike Smith }; 18015e32d5dSMike Smith 18115e32d5dSMike Smith static driver_t acpi_driver = { 18215e32d5dSMike Smith "acpi", 18315e32d5dSMike Smith acpi_methods, 18415e32d5dSMike Smith sizeof(struct acpi_softc), 18515e32d5dSMike Smith }; 18615e32d5dSMike Smith 1873273b005SMike Smith static devclass_t acpi_devclass; 1886d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 189a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 19015e32d5dSMike Smith 191865b8d0bSNate Lawson static const char* sleep_state_names[] = { 192865b8d0bSNate Lawson "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 193865b8d0bSNate Lawson 1941d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 1951d7b121cSNate Lawson static char acpi_ca_version[12]; 1961d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 1971d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 19815e32d5dSMike Smith 19915e32d5dSMike Smith /* 200413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 201c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 202c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 203c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 204c9b8d77dSNate Lawson * some IBMs use such code. 205413081d7SNate Lawson */ 206c9b8d77dSNate Lawson static int acpi_serialize_methods; 207413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 208413081d7SNate Lawson 209c9b8d77dSNate Lawson /* 2106d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2116d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 212be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 213be2b1797SNate Lawson * off it. 2146d63101aSMike Smith */ 2156d63101aSMike Smith static int 2166d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2176d63101aSMike Smith { 2186d63101aSMike Smith switch(event) { 2196d63101aSMike Smith case MOD_LOAD: 22087b45ed5SMitsuru IWASAKI if (!cold) { 22187b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2226d63101aSMike Smith return (EPERM); 22387b45ed5SMitsuru IWASAKI } 2246d63101aSMike Smith break; 2256d63101aSMike Smith case MOD_UNLOAD: 226f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2276d63101aSMike Smith return (EBUSY); 228f9390180SMitsuru IWASAKI break; 2296d63101aSMike Smith default: 2306d63101aSMike Smith break; 2316d63101aSMike Smith } 2326d63101aSMike Smith return (0); 2336d63101aSMike Smith } 2346d63101aSMike Smith 2356d63101aSMike Smith /* 236bbc2815cSJohn Baldwin * Perform early initialization. 23715e32d5dSMike Smith */ 238bbc2815cSJohn Baldwin ACPI_STATUS 239bbc2815cSJohn Baldwin acpi_Startup(void) 24015e32d5dSMike Smith { 241d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 242d786139cSMaxime Henrion char *debugpoint; 24315e32d5dSMike Smith #endif 244bbc2815cSJohn Baldwin static int error, started = 0; 24515e32d5dSMike Smith 2461b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2471b8c233dSPeter Pentchev 248bbc2815cSJohn Baldwin if (started) 249bbc2815cSJohn Baldwin return_VALUE (error); 250bbc2815cSJohn Baldwin started = 1; 25115e32d5dSMike Smith 252fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 253be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2546008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 255fc0ea94aSJohn Baldwin #endif 256cb9b0d80SMike Smith 257413081d7SNate Lawson /* 258413081d7SNate Lawson * Set the globals from our tunables. This is needed because ACPI-CA 259f3fc4f8bSNate Lawson * uses UINT8 for some values and we have no tunable_byte. 260413081d7SNate Lawson */ 261f3fc4f8bSNate Lawson AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods; 262413081d7SNate Lawson 263be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 264d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 265d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 266d786139cSMaxime Henrion if (debugpoint) { 267d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 26815e32d5dSMike Smith acpi_EnterDebugger(); 269d786139cSMaxime Henrion freeenv(debugpoint); 270d786139cSMaxime Henrion } 27115e32d5dSMike Smith #endif 272b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 273bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 274bbc2815cSJohn Baldwin return_VALUE (error); 27515e32d5dSMike Smith } 276d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 277d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 278d786139cSMaxime Henrion if (debugpoint) { 279d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 28015e32d5dSMike Smith acpi_EnterDebugger(); 281d786139cSMaxime Henrion freeenv(debugpoint); 282d786139cSMaxime Henrion } 28315e32d5dSMike Smith #endif 2841611ea87SMitsuru IWASAKI 285b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 286bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 287bbc2815cSJohn Baldwin return_VALUE(error); 28815e32d5dSMike Smith } 2893184cf5aSNate Lawson 2903184cf5aSNate Lawson /* Set up any quirks we have for this XSDT. */ 2913184cf5aSNate Lawson acpi_quirks_set(); 2924e376d58SNate Lawson if (acpi_disabled("acpi")) 2934e376d58SNate Lawson return_VALUE (AE_ERROR); 2943184cf5aSNate Lawson 295bbc2815cSJohn Baldwin return_VALUE (AE_OK); 296bbc2815cSJohn Baldwin } 297bbc2815cSJohn Baldwin 298bbc2815cSJohn Baldwin /* 299bbc2815cSJohn Baldwin * Detect ACPI, perform early initialisation 300bbc2815cSJohn Baldwin */ 301bbc2815cSJohn Baldwin static void 302bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent) 303bbc2815cSJohn Baldwin { 304bbc2815cSJohn Baldwin device_t child; 305bbc2815cSJohn Baldwin 306bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 307bbc2815cSJohn Baldwin 308bbc2815cSJohn Baldwin if (!cold) 309bbc2815cSJohn Baldwin return_VOID; 310bbc2815cSJohn Baldwin 311bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 312bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 313bbc2815cSJohn Baldwin return_VOID; 314bbc2815cSJohn Baldwin 315bbc2815cSJohn Baldwin /* Make sure we're not being doubly invoked. */ 316bbc2815cSJohn Baldwin if (device_find_child(parent, "acpi", 0) != NULL) 317bbc2815cSJohn Baldwin return_VOID; 318bbc2815cSJohn Baldwin 319bbc2815cSJohn Baldwin /* Initialize ACPI-CA. */ 320bbc2815cSJohn Baldwin if (ACPI_FAILURE(acpi_Startup())) 321bbc2815cSJohn Baldwin return_VOID; 32215e32d5dSMike Smith 3234e376d58SNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION); 3244e376d58SNate Lawson 325be2b1797SNate Lawson /* Attach the actual ACPI device. */ 32615e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 32715e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 3280ae55423SMike Smith return_VOID; 32915e32d5dSMike Smith } 33015e32d5dSMike Smith } 33115e32d5dSMike Smith 33215e32d5dSMike Smith /* 33315e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 33415e32d5dSMike Smith */ 33515e32d5dSMike Smith static int 33615e32d5dSMike Smith acpi_probe(device_t dev) 33715e32d5dSMike Smith { 33815e32d5dSMike Smith ACPI_TABLE_HEADER th; 33915e32d5dSMike Smith char buf[20]; 34015e32d5dSMike Smith int error; 3412ccd1cacSNate Lawson struct sbuf sb; 3422ccd1cacSNate Lawson ACPI_STATUS status; 343fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 34415e32d5dSMike Smith 345b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 346f9390180SMitsuru IWASAKI 347f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 348f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 349be2b1797SNate Lawson 350f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 351f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 352f9390180SMitsuru IWASAKI } 353f9390180SMitsuru IWASAKI 354cb9b0d80SMike Smith ACPI_LOCK; 3550ae55423SMike Smith 356b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 357be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 358be2b1797SNate Lawson AcpiFormatException(status)); 359cb9b0d80SMike Smith error = ENXIO; 360cb9b0d80SMike Smith } else { 3612ccd1cacSNate Lawson sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 3622ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemId, 6); 3632ccd1cacSNate Lawson sbuf_trim(&sb); 3642ccd1cacSNate Lawson sbuf_putc(&sb, ' '); 3652ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemTableId, 8); 3662ccd1cacSNate Lawson sbuf_trim(&sb); 3672ccd1cacSNate Lawson sbuf_finish(&sb); 3682ccd1cacSNate Lawson device_set_desc_copy(dev, sbuf_data(&sb)); 3692ccd1cacSNate Lawson sbuf_delete(&sb); 370cb9b0d80SMike Smith error = 0; 371cb9b0d80SMike Smith } 372cb9b0d80SMike Smith ACPI_UNLOCK; 373cb9b0d80SMike Smith return_VALUE(error); 37415e32d5dSMike Smith } 37515e32d5dSMike Smith 37615e32d5dSMike Smith static int 37715e32d5dSMike Smith acpi_attach(device_t dev) 37815e32d5dSMike Smith { 37915e32d5dSMike Smith struct acpi_softc *sc; 380cb9b0d80SMike Smith ACPI_STATUS status; 381ea27c63eSNate Lawson int error, state; 38232d18aa5SMike Smith UINT32 flags; 383ea27c63eSNate Lawson UINT8 TypeA, TypeB; 384498d464fSMitsuru IWASAKI char *env; 385d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 386d786139cSMaxime Henrion char *debugpoint; 38715e32d5dSMike Smith #endif 388fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 38915e32d5dSMike Smith 390b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 391cb9b0d80SMike Smith ACPI_LOCK; 39215e32d5dSMike Smith sc = device_get_softc(dev); 39315e32d5dSMike Smith bzero(sc, sizeof(*sc)); 39415e32d5dSMike Smith sc->acpi_dev = dev; 39515e32d5dSMike Smith 396d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 397d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 398d786139cSMaxime Henrion if (debugpoint) { 399d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 40015e32d5dSMike Smith acpi_EnterDebugger(); 401d786139cSMaxime Henrion freeenv(debugpoint); 402d786139cSMaxime Henrion } 40315e32d5dSMike Smith #endif 40415e32d5dSMike Smith 405be2b1797SNate Lawson /* Install the default address space handlers. */ 406cb9b0d80SMike Smith error = ENXIO; 407be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 408be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 409be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 410be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 411be2b1797SNate Lawson AcpiFormatException(status)); 412cb9b0d80SMike Smith goto out; 41315e32d5dSMike Smith } 414be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 415be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 416be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 417be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 418be2b1797SNate Lawson AcpiFormatException(status)); 419cb9b0d80SMike Smith goto out; 42015e32d5dSMike Smith } 421be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 422be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 423be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 424be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 425be2b1797SNate Lawson AcpiFormatException(status)); 426cb9b0d80SMike Smith goto out; 42715e32d5dSMike Smith } 42815e32d5dSMike Smith 42915e32d5dSMike Smith /* 43015e32d5dSMike Smith * Bring ACPI fully online. 43115e32d5dSMike Smith * 432be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 433be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 434be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 435be2b1797SNate Lawson * object init pass. 43615e32d5dSMike Smith * 43732d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 43832d18aa5SMike Smith * 439be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 440be2b1797SNate Lawson * all our child devices, but on many systems it works here. 44115e32d5dSMike Smith */ 442d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 443d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 444d786139cSMaxime Henrion if (debugpoint) { 445d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 44615e32d5dSMike Smith acpi_EnterDebugger(); 447d786139cSMaxime Henrion freeenv(debugpoint); 448d786139cSMaxime Henrion } 44915e32d5dSMike Smith #endif 45032d18aa5SMike Smith flags = 0; 451d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 45232d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 453b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 454be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 455be2b1797SNate Lawson AcpiFormatException(status)); 456cb9b0d80SMike Smith goto out; 45715e32d5dSMike Smith } 45815e32d5dSMike Smith 459f8335e3aSNate Lawson /* 460f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 461f8335e3aSNate Lawson * the namespace has been evaluated. 462f8335e3aSNate Lawson */ 463f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 464f8335e3aSNate Lawson 465b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 466be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 467be2b1797SNate Lawson AcpiFormatException(status)); 468b69ed3f4SMitsuru IWASAKI goto out; 469b69ed3f4SMitsuru IWASAKI } 470b69ed3f4SMitsuru IWASAKI 47115e32d5dSMike Smith /* 4721d073b1dSJohn Baldwin * Setup our sysctl tree. 4731d073b1dSJohn Baldwin * 4741d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 4751d073b1dSJohn Baldwin */ 4761d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 4771d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 4781d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 4791d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 4801d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 481d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 482d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 483d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4841d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4851d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4861d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4871d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 4881d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4891d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 4901d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 4911d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 492f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 493f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 494f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 495f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 496f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 497f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 4982d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 499ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 500ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 501ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5021611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 5031611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 5041611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5052d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 5062d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 507c6a78e98STakanori Watanabe SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 508c6a78e98STakanori Watanabe OID_AUTO, "disable_on_poweroff", CTLFLAG_RD | CTLFLAG_RW, 509c6a78e98STakanori Watanabe &sc->acpi_disable_on_poweroff, 0, "ACPI subsystem disable on poweroff"); 510bf0c18ecSNate Lawson 511bf0c18ecSNate Lawson /* 512bf0c18ecSNate Lawson * Default to 5 seconds before sleeping to give some machines time to 513bf0c18ecSNate Lawson * stabilize. 514bf0c18ecSNate Lawson */ 515bf0c18ecSNate Lawson sc->acpi_sleep_delay = 5; 516c6a78e98STakanori Watanabe sc->acpi_disable_on_poweroff = 1; 5172d644607SMitsuru IWASAKI if (bootverbose) 5182d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 519498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 520498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 521498d464fSMitsuru IWASAKI freeenv(env); 522498d464fSMitsuru IWASAKI } 5231d073b1dSJohn Baldwin 5242d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5252d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 5262d610c46SNate Lawson sc->acpi_s4bios = 1; 5272d610c46SNate Lawson 5281d073b1dSJohn Baldwin /* 529ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 530ea27c63eSNate Lawson * to NONE by default to avoid surprising users. 53115e32d5dSMike Smith */ 532ea27c63eSNate Lawson sc->acpi_power_button_sx = ACPI_STATE_S5; 533ea27c63eSNate Lawson sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 534f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 535f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 53615e32d5dSMike Smith 537ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 538ea27c63eSNate Lawson sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 539ea27c63eSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++) 540ea27c63eSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 541ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 542ea27c63eSNate Lawson break; 543ea27c63eSNate Lawson } 544ea27c63eSNate Lawson 54513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 54615e32d5dSMike Smith 54715e32d5dSMike Smith /* 54815e32d5dSMike Smith * Scan the namespace and attach/initialise children. 54915e32d5dSMike Smith */ 550d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 551d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 552d786139cSMaxime Henrion if (debugpoint) { 553d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 55415e32d5dSMike Smith acpi_EnterDebugger(); 555d786139cSMaxime Henrion freeenv(debugpoint); 556d786139cSMaxime Henrion } 55715e32d5dSMike Smith #endif 55815e32d5dSMike Smith 559be2b1797SNate Lawson /* Register our shutdown handlers */ 560be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_pre_sync, acpi_shutdown_pre_sync, sc, 561be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 562be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 563be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 56415e32d5dSMike Smith 56515e32d5dSMike Smith /* 56615e32d5dSMike Smith * Register our acpi event handlers. 56715e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 56815e32d5dSMike Smith */ 569be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 570be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 571be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 572be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 57315e32d5dSMike Smith 574be2b1797SNate Lawson /* Flag our initial states. */ 57515e32d5dSMike Smith sc->acpi_enabled = 1; 57615e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 577ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 57815e32d5dSMike Smith 579be2b1797SNate Lawson /* Create the control device */ 580a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 5812a4689e8SRobert Watson "acpi"); 58215e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 58315e32d5dSMike Smith 584d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 585d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 586d786139cSMaxime Henrion if (debugpoint) { 587be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 58815e32d5dSMike Smith acpi_EnterDebugger(); 589d786139cSMaxime Henrion freeenv(debugpoint); 590d786139cSMaxime Henrion } 59115e32d5dSMike Smith #endif 592f86214b6SMitsuru IWASAKI 59391da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 594be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 595c573e654SMitsuru IWASAKI goto out; 596c573e654SMitsuru IWASAKI #endif 597c573e654SMitsuru IWASAKI 598be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 599f86214b6SMitsuru IWASAKI goto out; 600f86214b6SMitsuru IWASAKI 601f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 602f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 603f9390180SMitsuru IWASAKI 604eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 605eeb6dba2SJohn Baldwin acpi_probe_children(dev); 606eeb6dba2SJohn Baldwin 607cb9b0d80SMike Smith error = 0; 608cb9b0d80SMike Smith 609cb9b0d80SMike Smith out: 610cb9b0d80SMike Smith ACPI_UNLOCK; 611cb9b0d80SMike Smith return_VALUE (error); 61215e32d5dSMike Smith } 61315e32d5dSMike Smith 6143184cf5aSNate Lawson static void 6153184cf5aSNate Lawson acpi_quirks_set() 6163184cf5aSNate Lawson { 6173184cf5aSNate Lawson XSDT_DESCRIPTOR *xsdt; 6183184cf5aSNate Lawson struct acpi_quirks *quirk; 6193184cf5aSNate Lawson char *env, *tmp; 6203184cf5aSNate Lawson int len; 6213184cf5aSNate Lawson 6224e376d58SNate Lawson /* 6234e376d58SNate Lawson * If the user loaded a custom table or disabled "quirks", leave 6244e376d58SNate Lawson * the settings alone. 6254e376d58SNate Lawson */ 6263184cf5aSNate Lawson len = 0; 6274e376d58SNate Lawson if ((env = getenv("acpi_dsdt_load")) != NULL) { 6284e376d58SNate Lawson /* XXX No strcasecmp but this is good enough. */ 6294e376d58SNate Lawson if (*env == 'Y' || *env == 'y') 6304e376d58SNate Lawson goto out; 6314e376d58SNate Lawson freeenv(env); 6324e376d58SNate Lawson } 6333184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) != NULL) { 6344e376d58SNate Lawson if (strstr("quirks", env) != NULL) 6353184cf5aSNate Lawson goto out; 6363184cf5aSNate Lawson len = strlen(env); 6373184cf5aSNate Lawson } 6383184cf5aSNate Lawson 6393184cf5aSNate Lawson /* 6403184cf5aSNate Lawson * Search through our quirk table and concatenate the disabled 6413184cf5aSNate Lawson * values with whatever we find. 6423184cf5aSNate Lawson */ 6433184cf5aSNate Lawson xsdt = AcpiGbl_XSDT; 6443184cf5aSNate Lawson for (quirk = acpi_quirks_table; quirk->OemId; quirk++) { 6453184cf5aSNate Lawson if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) && 6463184cf5aSNate Lawson (xsdt->OemRevision == quirk->OemRevision || 6473184cf5aSNate Lawson quirk->OemRevision == ACPI_OEM_REV_ANY)) { 6483184cf5aSNate Lawson len += strlen(quirk->value) + 2; 6493184cf5aSNate Lawson if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL) 6503184cf5aSNate Lawson goto out; 6513184cf5aSNate Lawson sprintf(tmp, "%s %s", env ? env : "", quirk->value); 6523184cf5aSNate Lawson setenv("debug.acpi.disabled", tmp); 6533184cf5aSNate Lawson free(tmp, M_TEMP); 6543184cf5aSNate Lawson break; 6553184cf5aSNate Lawson } 6563184cf5aSNate Lawson } 6573184cf5aSNate Lawson 6583184cf5aSNate Lawson out: 6593184cf5aSNate Lawson if (env) 6603184cf5aSNate Lawson freeenv(env); 6613184cf5aSNate Lawson } 6623184cf5aSNate Lawson 66315e32d5dSMike Smith /* 66415e32d5dSMike Smith * Handle a new device being added 66515e32d5dSMike Smith */ 66615e32d5dSMike Smith static device_t 66715e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 66815e32d5dSMike Smith { 66915e32d5dSMike Smith struct acpi_device *ad; 67015e32d5dSMike Smith device_t child; 67115e32d5dSMike Smith 672be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 67315e32d5dSMike Smith return (NULL); 67415e32d5dSMike Smith 67515e32d5dSMike Smith resource_list_init(&ad->ad_rl); 67615e32d5dSMike Smith 67715e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 67815e32d5dSMike Smith if (child != NULL) 67915e32d5dSMike Smith device_set_ivars(child, ad); 68015e32d5dSMike Smith return (child); 68115e32d5dSMike Smith } 68215e32d5dSMike Smith 68315e32d5dSMike Smith static int 68415e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 68515e32d5dSMike Smith { 68615e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 68715e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 68815e32d5dSMike Smith int retval = 0; 68915e32d5dSMike Smith 69015e32d5dSMike Smith retval += bus_print_child_header(bus, child); 6919ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 6929ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 6939ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 6949ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 69515e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 69615e32d5dSMike Smith 69715e32d5dSMike Smith return (retval); 69815e32d5dSMike Smith } 69915e32d5dSMike Smith 70063600cc3SNate Lawson /* Location hint for devctl(8) */ 70163600cc3SNate Lawson static int 702247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 703247648afSTakanori Watanabe size_t buflen) 704247648afSTakanori Watanabe { 705247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 706247648afSTakanori Watanabe 707247648afSTakanori Watanabe if (dinfo->ad_handle) 708247648afSTakanori Watanabe snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle)); 709247648afSTakanori Watanabe else 710247648afSTakanori Watanabe snprintf(buf, buflen, "magic=unknown"); 711247648afSTakanori Watanabe return (0); 712247648afSTakanori Watanabe } 713247648afSTakanori Watanabe 71463600cc3SNate Lawson /* PnP information for devctl(8) */ 71563600cc3SNate Lawson static int 716247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 717247648afSTakanori Watanabe size_t buflen) 718247648afSTakanori Watanabe { 719247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 72063600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 72163600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 722247648afSTakanori Watanabe char *end; 723247648afSTakanori Watanabe int error; 724247648afSTakanori Watanabe 725247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 726247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 727247648afSTakanori Watanabe 728247648afSTakanori Watanabe if (error) 729247648afSTakanori Watanabe snprintf(buf, buflen, "Unknown"); 730247648afSTakanori Watanabe else 731247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 732247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 733247648afSTakanori Watanabe adinfo->HardwareId.Value : "UNKNOWN", 73463600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 73563600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 736247648afSTakanori Watanabe 737247648afSTakanori Watanabe if (adinfo) 738247648afSTakanori Watanabe AcpiOsFree(adinfo); 739247648afSTakanori Watanabe 740247648afSTakanori Watanabe return (0); 741247648afSTakanori Watanabe } 742247648afSTakanori Watanabe 743247648afSTakanori Watanabe /* 74415e32d5dSMike Smith * Handle per-device ivars 74515e32d5dSMike Smith */ 74615e32d5dSMike Smith static int 74715e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 74815e32d5dSMike Smith { 74915e32d5dSMike Smith struct acpi_device *ad; 75015e32d5dSMike Smith 75115e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 75215e32d5dSMike Smith printf("device has no ivars\n"); 75315e32d5dSMike Smith return (ENOENT); 75415e32d5dSMike Smith } 75515e32d5dSMike Smith 756be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 75715e32d5dSMike Smith switch(index) { 75815e32d5dSMike Smith case ACPI_IVAR_HANDLE: 75915e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 76015e32d5dSMike Smith break; 76115e32d5dSMike Smith case ACPI_IVAR_MAGIC: 76215e32d5dSMike Smith *(int *)result = ad->ad_magic; 76315e32d5dSMike Smith break; 76415e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 76515e32d5dSMike Smith *(void **)result = ad->ad_private; 76615e32d5dSMike Smith break; 76732d18aa5SMike Smith case ISA_IVAR_VENDORID: 76832d18aa5SMike Smith case ISA_IVAR_SERIAL: 76932d18aa5SMike Smith case ISA_IVAR_COMPATID: 77032d18aa5SMike Smith *(int *)result = -1; 77132d18aa5SMike Smith break; 77232d18aa5SMike Smith case ISA_IVAR_LOGICALID: 77332d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 77432d18aa5SMike Smith break; 77515e32d5dSMike Smith default: 77615e32d5dSMike Smith return (ENOENT); 77715e32d5dSMike Smith } 778be2b1797SNate Lawson 77915e32d5dSMike Smith return (0); 78015e32d5dSMike Smith } 78115e32d5dSMike Smith 78215e32d5dSMike Smith static int 78315e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 78415e32d5dSMike Smith { 78515e32d5dSMike Smith struct acpi_device *ad; 78615e32d5dSMike Smith 78715e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 78815e32d5dSMike Smith printf("device has no ivars\n"); 78915e32d5dSMike Smith return (ENOENT); 79015e32d5dSMike Smith } 79115e32d5dSMike Smith 79215e32d5dSMike Smith switch(index) { 79315e32d5dSMike Smith case ACPI_IVAR_HANDLE: 79415e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 79515e32d5dSMike Smith break; 79615e32d5dSMike Smith case ACPI_IVAR_MAGIC: 79715e32d5dSMike Smith ad->ad_magic = (int)value; 79815e32d5dSMike Smith break; 79915e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 80015e32d5dSMike Smith ad->ad_private = (void *)value; 80115e32d5dSMike Smith break; 80215e32d5dSMike Smith default: 8032ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 80415e32d5dSMike Smith return (ENOENT); 80515e32d5dSMike Smith } 806be2b1797SNate Lawson 80715e32d5dSMike Smith return (0); 80815e32d5dSMike Smith } 80915e32d5dSMike Smith 81015e32d5dSMike Smith /* 81115e32d5dSMike Smith * Handle child resource allocation/removal 81215e32d5dSMike Smith */ 81315e32d5dSMike Smith static int 814be2b1797SNate Lawson acpi_set_resource(device_t dev, device_t child, int type, int rid, 815be2b1797SNate Lawson u_long start, u_long count) 81615e32d5dSMike Smith { 81715e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 81815e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 81915e32d5dSMike Smith 82015e32d5dSMike Smith resource_list_add(rl, type, rid, start, start + count -1, count); 82115e32d5dSMike Smith 82215e32d5dSMike Smith return(0); 82315e32d5dSMike Smith } 82415e32d5dSMike Smith 82515e32d5dSMike Smith static int 826be2b1797SNate Lawson acpi_get_resource(device_t dev, device_t child, int type, int rid, 827be2b1797SNate Lawson u_long *startp, u_long *countp) 82815e32d5dSMike Smith { 82915e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 83015e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 83115e32d5dSMike Smith struct resource_list_entry *rle; 83215e32d5dSMike Smith 83315e32d5dSMike Smith rle = resource_list_find(rl, type, rid); 83415e32d5dSMike Smith if (!rle) 83515e32d5dSMike Smith return(ENOENT); 83615e32d5dSMike Smith 83715e32d5dSMike Smith if (startp) 83815e32d5dSMike Smith *startp = rle->start; 83915e32d5dSMike Smith if (countp) 84015e32d5dSMike Smith *countp = rle->count; 84115e32d5dSMike Smith 84215e32d5dSMike Smith return (0); 84315e32d5dSMike Smith } 84415e32d5dSMike Smith 84515e32d5dSMike Smith static struct resource * 84615e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 84715e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 84815e32d5dSMike Smith { 84915e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 85015e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 85115e32d5dSMike Smith 852be2b1797SNate Lawson return (resource_list_alloc(rl, bus, child, type, rid, start, end, count, 853be2b1797SNate Lawson flags)); 85415e32d5dSMike Smith } 85515e32d5dSMike Smith 85615e32d5dSMike Smith static int 85715e32d5dSMike Smith acpi_release_resource(device_t bus, device_t child, int type, int rid, struct resource *r) 85815e32d5dSMike Smith { 85915e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 86015e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 86115e32d5dSMike Smith 86215e32d5dSMike Smith return (resource_list_release(rl, bus, child, type, rid, r)); 86315e32d5dSMike Smith } 86415e32d5dSMike Smith 865dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 866dc750869SNate Lawson struct resource * 867dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 868dc750869SNate Lawson { 869dc750869SNate Lawson int type; 870dc750869SNate Lawson 871dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 872dc750869SNate Lawson gas->RegisterBitWidth < 8) 873dc750869SNate Lawson return (NULL); 874dc750869SNate Lawson 875dc750869SNate Lawson switch (gas->AddressSpaceId) { 876dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 877dc750869SNate Lawson type = SYS_RES_MEMORY; 878dc750869SNate Lawson break; 879dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 880dc750869SNate Lawson type = SYS_RES_IOPORT; 881dc750869SNate Lawson break; 882dc750869SNate Lawson default: 883dc750869SNate Lawson return (NULL); 884dc750869SNate Lawson } 885dc750869SNate Lawson 886dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 8875f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 888dc750869SNate Lawson } 889dc750869SNate Lawson 89015e32d5dSMike Smith /* 891bc0f2195SMike Smith * Handle ISA-like devices probing for a PnP ID to match. 892bc0f2195SMike Smith */ 893bc0f2195SMike Smith #define PNP_EISAID(s) \ 894bc0f2195SMike Smith ((((s[0] - '@') & 0x1f) << 2) \ 895bc0f2195SMike Smith | (((s[1] - '@') & 0x18) >> 3) \ 896bc0f2195SMike Smith | (((s[1] - '@') & 0x07) << 13) \ 897bc0f2195SMike Smith | (((s[2] - '@') & 0x1f) << 8) \ 898bc0f2195SMike Smith | (PNP_HEXTONUM(s[4]) << 16) \ 899bc0f2195SMike Smith | (PNP_HEXTONUM(s[3]) << 20) \ 900bc0f2195SMike Smith | (PNP_HEXTONUM(s[6]) << 24) \ 901bc0f2195SMike Smith | (PNP_HEXTONUM(s[5]) << 28)) 902bc0f2195SMike Smith 9031e4925e8SNate Lawson static uint32_t 90432d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 905bc0f2195SMike Smith { 9061e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9071e4925e8SNate Lawson ACPI_BUFFER buf; 908bc0f2195SMike Smith ACPI_HANDLE h; 909bc0f2195SMike Smith ACPI_STATUS error; 910bc0f2195SMike Smith u_int32_t pnpid; 911fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 91232d18aa5SMike Smith 913b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 91432d18aa5SMike Smith 91532d18aa5SMike Smith pnpid = 0; 916bd189fe7SNate Lawson buf.Pointer = NULL; 917bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 918bd189fe7SNate Lawson 91932d18aa5SMike Smith ACPI_LOCK; 92032d18aa5SMike Smith 9216fca9360SNate Lawson /* Fetch and validate the HID. */ 92232d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 923bd189fe7SNate Lawson goto out; 9246fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9256fca9360SNate Lawson if (ACPI_FAILURE(error)) 926bd189fe7SNate Lawson goto out; 9271e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 92832d18aa5SMike Smith 9291e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 9301e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 931be2b1797SNate Lawson 932bd189fe7SNate Lawson out: 933bd189fe7SNate Lawson if (buf.Pointer != NULL) 9341e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 93532d18aa5SMike Smith ACPI_UNLOCK; 93632d18aa5SMike Smith return_VALUE (pnpid); 93732d18aa5SMike Smith } 93832d18aa5SMike Smith 9391e4925e8SNate Lawson static int 9401e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 941b2566207STakanori Watanabe { 9421e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9431e4925e8SNate Lawson ACPI_BUFFER buf; 944b2566207STakanori Watanabe ACPI_HANDLE h; 945b2566207STakanori Watanabe ACPI_STATUS error; 9461e4925e8SNate Lawson uint32_t *pnpid; 9471e4925e8SNate Lawson int valid, i; 948b2566207STakanori Watanabe ACPI_LOCK_DECL; 949b2566207STakanori Watanabe 950b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 951b2566207STakanori Watanabe 9521e4925e8SNate Lawson pnpid = cids; 9531e4925e8SNate Lawson valid = 0; 954bd189fe7SNate Lawson buf.Pointer = NULL; 955bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 956bd189fe7SNate Lawson 957b2566207STakanori Watanabe ACPI_LOCK; 958b2566207STakanori Watanabe 9591e4925e8SNate Lawson /* Fetch and validate the CID */ 960b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 961bd189fe7SNate Lawson goto out; 9621e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 9631e4925e8SNate Lawson if (ACPI_FAILURE(error)) 964bd189fe7SNate Lawson goto out; 9651e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 9661e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 967b2566207STakanori Watanabe goto out; 968b2566207STakanori Watanabe 9691e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 9701e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 9711e4925e8SNate Lawson for (i = 0; i < count; i++) { 9721e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 9731e4925e8SNate Lawson continue; 9741e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 9751e4925e8SNate Lawson valid++; 976b2566207STakanori Watanabe } 977b2566207STakanori Watanabe 9781e4925e8SNate Lawson out: 979bd189fe7SNate Lawson if (buf.Pointer != NULL) 9801e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 9811e4925e8SNate Lawson ACPI_UNLOCK; 9821e4925e8SNate Lawson return_VALUE (valid); 9831e4925e8SNate Lawson } 984b2566207STakanori Watanabe 98532d18aa5SMike Smith static int 98632d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 98732d18aa5SMike Smith { 9881e4925e8SNate Lawson int result, cid_count, i; 9891e4925e8SNate Lawson uint32_t lid, cids[8]; 990bc0f2195SMike Smith 991b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 992bc0f2195SMike Smith 993bc0f2195SMike Smith /* 994bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 995bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 996bc0f2195SMike Smith * that to happen, so don't ever return it. 997bc0f2195SMike Smith */ 998bc0f2195SMike Smith result = ENXIO; 999bc0f2195SMike Smith 1000be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1001b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 10021e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1003bc0f2195SMike Smith while (ids && ids->ip_id) { 10041e4925e8SNate Lawson if (lid == ids->ip_id) { 1005bc0f2195SMike Smith result = 0; 1006bc0f2195SMike Smith goto out; 1007bc0f2195SMike Smith } 10081e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 10091e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 10101e4925e8SNate Lawson result = 0; 10111e4925e8SNate Lawson goto out; 10121e4925e8SNate Lawson } 10131e4925e8SNate Lawson } 1014bc0f2195SMike Smith ids++; 1015bc0f2195SMike Smith } 1016be2b1797SNate Lawson 1017bc0f2195SMike Smith out: 1018bc0f2195SMike Smith return_VALUE (result); 1019bc0f2195SMike Smith } 1020bc0f2195SMike Smith 1021bc0f2195SMike Smith /* 102215e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 102315e32d5dSMike Smith * 1024be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1025be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 102615e32d5dSMike Smith */ 102715e32d5dSMike Smith static void 102815e32d5dSMike Smith acpi_probe_children(device_t bus) 102915e32d5dSMike Smith { 103015e32d5dSMike Smith ACPI_HANDLE parent; 1031be2b1797SNate Lawson ACPI_STATUS status; 10327d3bcec9SMike Smith static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 103315e32d5dSMike Smith int i; 103415e32d5dSMike Smith 1035b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1036cb9b0d80SMike Smith ACPI_ASSERTLOCK; 10370ae55423SMike Smith 1038be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 10394c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 104015e32d5dSMike Smith bus_generic_probe(bus); 104115e32d5dSMike Smith 104215e32d5dSMike Smith /* 104315e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 104415e32d5dSMike Smith * we find. 104515e32d5dSMike Smith * 104615e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1047be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1048be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1049be2b1797SNate Lawson * devices as they appear, which might be smarter.) 105015e32d5dSMike Smith */ 10514c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1052be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1053be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1054be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1055be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1056be2b1797SNate Lawson bus, NULL); 1057be2b1797SNate Lawson } 1058be2b1797SNate Lawson } 105915e32d5dSMike Smith 106015e32d5dSMike Smith /* 106115e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 106215e32d5dSMike Smith */ 10634c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 106415e32d5dSMike Smith bus_generic_attach(bus); 106515e32d5dSMike Smith 106615e32d5dSMike Smith /* 106715e32d5dSMike Smith * Some of these children may have attached others as part of their attach 106815e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 106915e32d5dSMike Smith */ 10704c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 107115e32d5dSMike Smith bus_generic_attach(bus); 10720ae55423SMike Smith 107388a79fc0SNate Lawson /* Attach wake sysctls. */ 10745c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 107588a79fc0SNate Lawson 1076bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 10770ae55423SMike Smith return_VOID; 107815e32d5dSMike Smith } 107915e32d5dSMike Smith 108015e32d5dSMike Smith /* 108115e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 108215e32d5dSMike Smith * it. 108315e32d5dSMike Smith */ 108415e32d5dSMike Smith static ACPI_STATUS 108515e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 108615e32d5dSMike Smith { 108715e32d5dSMike Smith ACPI_OBJECT_TYPE type; 108815e32d5dSMike Smith device_t child, bus = (device_t)context; 108915e32d5dSMike Smith 1090b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 10910ae55423SMike Smith 1092be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 10930ae55423SMike Smith if (acpi_avoid(handle)) 10940ae55423SMike Smith return_ACPI_STATUS (AE_OK); 10950ae55423SMike Smith 1096b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 109715e32d5dSMike Smith switch(type) { 109815e32d5dSMike Smith case ACPI_TYPE_DEVICE: 109915e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 110015e32d5dSMike Smith case ACPI_TYPE_THERMAL: 110115e32d5dSMike Smith case ACPI_TYPE_POWER: 11020ae55423SMike Smith if (acpi_disabled("children")) 11030ae55423SMike Smith break; 1104be2b1797SNate Lawson 110515e32d5dSMike Smith /* 110615e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 110715e32d5dSMike Smith * so that the probe/attach passes will run breadth-first. 110815e32d5dSMike Smith */ 1109be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1110be2b1797SNate Lawson acpi_name(handle))); 111115e32d5dSMike Smith child = BUS_ADD_CHILD(bus, level * 10, NULL, -1); 1112bc0f2195SMike Smith if (child == NULL) 1113bc0f2195SMike Smith break; 111415e32d5dSMike Smith acpi_set_handle(child, handle); 1115bc0f2195SMike Smith 1116e8b4d56eSNate Lawson /* Check if the device can generate wake events. */ 1117e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL))) 1118e8b4d56eSNate Lawson device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE); 1119e8b4d56eSNate Lawson 1120bc0f2195SMike Smith /* 1121b519f9d6SMike Smith * Check that the device is present. If it's not present, 1122b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1123b519f9d6SMike Smith * the handle, but we don't probe it). 1124b519f9d6SMike Smith */ 1125be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1126b519f9d6SMike Smith device_disable(child); 1127b519f9d6SMike Smith break; 1128b519f9d6SMike Smith } 1129b519f9d6SMike Smith 1130b519f9d6SMike Smith /* 1131bc0f2195SMike Smith * Get the device's resource settings and attach them. 1132bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1133bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1134bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1135bc0f2195SMike Smith * device not to have any resources. 1136bc0f2195SMike Smith */ 113772ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1138bc0f2195SMike Smith 1139be2b1797SNate Lawson /* If we're debugging, probe/attach now rather than later */ 1140b53f2771SMike Smith ACPI_DEBUG_EXEC(device_probe_and_attach(child)); 11410ae55423SMike Smith break; 114215e32d5dSMike Smith } 114315e32d5dSMike Smith } 1144be2b1797SNate Lawson 11450ae55423SMike Smith return_ACPI_STATUS (AE_OK); 114615e32d5dSMike Smith } 114715e32d5dSMike Smith 114815e32d5dSMike Smith static void 114915e32d5dSMike Smith acpi_shutdown_pre_sync(void *arg, int howto) 115015e32d5dSMike Smith { 1151c6a78e98STakanori Watanabe struct acpi_softc *sc = arg; 1152c6a78e98STakanori Watanabe 1153cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1154cb9b0d80SMike Smith 1155e8b4d56eSNate Lawson /* Disable all wake GPEs not appropriate for this state. */ 1156e8b4d56eSNate Lawson acpi_wake_limit_walk(ACPI_STATE_S5); 1157e8b4d56eSNate Lawson 115815e32d5dSMike Smith /* 11590ae55423SMike Smith * Disable all ACPI events before soft off, otherwise the system 116015e32d5dSMike Smith * will be turned on again on some laptops. 116115e32d5dSMike Smith * 116215e32d5dSMike Smith * XXX this should probably be restricted to masking some events just 116315e32d5dSMike Smith * before powering down, since we may still need ACPI during the 116415e32d5dSMike Smith * shutdown process. 116515e32d5dSMike Smith */ 1166c6a78e98STakanori Watanabe if (sc->acpi_disable_on_poweroff) 1167c6a78e98STakanori Watanabe acpi_Disable(sc); 116815e32d5dSMike Smith } 116915e32d5dSMike Smith 117015e32d5dSMike Smith static void 117115e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 117215e32d5dSMike Smith { 1173d33f4987STakanori Watanabe ACPI_STATUS status; 1174eeb3a05fSNate Lawson ACPI_ASSERTLOCK; 1175eeb3a05fSNate Lawson 1176eeb3a05fSNate Lawson /* 1177eeb3a05fSNate Lawson * If powering off, run the actual shutdown code on each processor. 1178eeb3a05fSNate Lawson * It will only perform the shutdown on the BSP. Some chipsets do 1179eeb3a05fSNate Lawson * not power off the system correctly if called from an AP. 1180eeb3a05fSNate Lawson */ 1181eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1182904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1183904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1184904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1185904bf0c2SNate Lawson AcpiFormatException(status)); 1186904bf0c2SNate Lawson return; 1187904bf0c2SNate Lawson } 1188eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 1189eeb3a05fSNate Lawson smp_rendezvous(NULL, acpi_shutdown_poweroff, NULL, NULL); 1190eeb3a05fSNate Lawson } else { 1191eeb3a05fSNate Lawson printf("Shutting down ACPI\n"); 1192eeb3a05fSNate Lawson AcpiTerminate(); 1193eeb3a05fSNate Lawson } 1194eeb3a05fSNate Lawson } 1195eeb3a05fSNate Lawson 1196904bf0c2SNate Lawson /* 1197904bf0c2SNate Lawson * Since this function may be called with locks held or in an unknown 1198904bf0c2SNate Lawson * context, it cannot allocate memory, acquire locks, sleep, etc. 1199904bf0c2SNate Lawson */ 1200eeb3a05fSNate Lawson static void 1201eeb3a05fSNate Lawson acpi_shutdown_poweroff(void *arg) 1202eeb3a05fSNate Lawson { 120315e32d5dSMike Smith ACPI_STATUS status; 120415e32d5dSMike Smith 1205cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1206cb9b0d80SMike Smith 1207eeb3a05fSNate Lawson /* Only attempt to power off if this is the BSP (cpuid 0). */ 1208eeb3a05fSNate Lawson if (PCPU_GET(cpuid) != 0) 1209eeb3a05fSNate Lawson return; 1210eeb3a05fSNate Lawson 121155398047SNate Lawson ACPI_DISABLE_IRQS(); 1212865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1213be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1214bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 121515e32d5dSMike Smith } else { 121615e32d5dSMike Smith DELAY(1000000); 121715e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 121815e32d5dSMike Smith } 121915e32d5dSMike Smith } 122015e32d5dSMike Smith 122113d4f7baSMitsuru IWASAKI static void 122213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 122313d4f7baSMitsuru IWASAKI { 122413d4f7baSMitsuru IWASAKI static int first_time = 1; 122513d4f7baSMitsuru IWASAKI 1226cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1227cb9b0d80SMike Smith 122813d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1229be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 123059ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 123113d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 123233febf93SNate Lawson acpi_event_power_button_sleep, sc); 1233be2b1797SNate Lawson if (first_time) 12346c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 123513d4f7baSMitsuru IWASAKI } 1236be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 123759ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 123813d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 123933febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1240be2b1797SNate Lawson if (first_time) 12416c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 124213d4f7baSMitsuru IWASAKI } 124313d4f7baSMitsuru IWASAKI 124413d4f7baSMitsuru IWASAKI first_time = 0; 124513d4f7baSMitsuru IWASAKI } 124613d4f7baSMitsuru IWASAKI 124715e32d5dSMike Smith /* 1248c5ba0be4SMike Smith * Returns true if the device is actually present and should 1249c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1250c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1251c5ba0be4SMike Smith */ 1252c5ba0be4SMike Smith BOOLEAN 1253c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1254c5ba0be4SMike Smith { 12551e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1256c5ba0be4SMike Smith ACPI_HANDLE h; 12571e4925e8SNate Lawson ACPI_BUFFER buf; 1258c5ba0be4SMike Smith ACPI_STATUS error; 12591e4925e8SNate Lawson int ret; 1260c5ba0be4SMike Smith 1261cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1262cb9b0d80SMike Smith 12631e4925e8SNate Lawson ret = FALSE; 1264c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1265c5ba0be4SMike Smith return (FALSE); 12661e4925e8SNate Lawson buf.Pointer = NULL; 12671e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 12686fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 12696fca9360SNate Lawson if (ACPI_FAILURE(error)) 1270c5ba0be4SMike Smith return (FALSE); 12711e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1272be2b1797SNate Lawson 1273be2b1797SNate Lawson /* If no _STA method, must be present */ 12741e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 12751e4925e8SNate Lawson ret = TRUE; 1276be2b1797SNate Lawson 1277be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 12781e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x9) == 0x9) 12791e4925e8SNate Lawson ret = TRUE; 1280be2b1797SNate Lawson 12811e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 12821e4925e8SNate Lawson return (ret); 1283c5ba0be4SMike Smith } 1284c5ba0be4SMike Smith 1285c5ba0be4SMike Smith /* 1286b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1287b53f2771SMike Smith */ 1288b53f2771SMike Smith BOOLEAN 1289b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1290b53f2771SMike Smith { 12911e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1292b53f2771SMike Smith ACPI_HANDLE h; 12931e4925e8SNate Lawson ACPI_BUFFER buf; 1294b53f2771SMike Smith ACPI_STATUS error; 12951e4925e8SNate Lawson int ret; 1296b53f2771SMike Smith 1297b53f2771SMike Smith ACPI_ASSERTLOCK; 1298b53f2771SMike Smith 12991e4925e8SNate Lawson ret = FALSE; 1300b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1301b53f2771SMike Smith return (FALSE); 13021e4925e8SNate Lawson buf.Pointer = NULL; 13031e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13046fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13056fca9360SNate Lawson if (ACPI_FAILURE(error)) 1306b53f2771SMike Smith return (FALSE); 13071e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1308be2b1797SNate Lawson 1309be2b1797SNate Lawson /* If no _STA method, must be present */ 13101e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 13111e4925e8SNate Lawson ret = TRUE; 1312be2b1797SNate Lawson 1313be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 13141e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x19) == 0x19) 13151e4925e8SNate Lawson ret = TRUE; 1316be2b1797SNate Lawson 13171e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13181e4925e8SNate Lawson return (ret); 1319b53f2771SMike Smith } 1320b53f2771SMike Smith 1321b53f2771SMike Smith /* 132215e32d5dSMike Smith * Match a HID string against a device 132315e32d5dSMike Smith */ 132415e32d5dSMike Smith BOOLEAN 132515e32d5dSMike Smith acpi_MatchHid(device_t dev, char *hid) 132615e32d5dSMike Smith { 13271e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 132815e32d5dSMike Smith ACPI_HANDLE h; 13291e4925e8SNate Lawson ACPI_BUFFER buf; 133015e32d5dSMike Smith ACPI_STATUS error; 13311e4925e8SNate Lawson int ret, i; 133215e32d5dSMike Smith 1333cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1334cb9b0d80SMike Smith 13351e4925e8SNate Lawson ret = FALSE; 13364d332989SMike Smith if (hid == NULL) 133715e32d5dSMike Smith return (FALSE); 133815e32d5dSMike Smith if ((h = acpi_get_handle(dev)) == NULL) 133915e32d5dSMike Smith return (FALSE); 13401e4925e8SNate Lawson buf.Pointer = NULL; 13411e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13426fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13436fca9360SNate Lawson if (ACPI_FAILURE(error)) 134415e32d5dSMike Smith return (FALSE); 13451e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1346be2b1797SNate Lawson 1347c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1348c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 13491e4925e8SNate Lawson ret = TRUE; 1350c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 13511e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 13521e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 13531e4925e8SNate Lawson ret = TRUE; 13541e4925e8SNate Lawson break; 13551e4925e8SNate Lawson } 13561e4925e8SNate Lawson } 13571e4925e8SNate Lawson } 1358be2b1797SNate Lawson 13591e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13601e4925e8SNate Lawson return (ret); 136115e32d5dSMike Smith } 136215e32d5dSMike Smith 136315e32d5dSMike Smith /* 1364c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1365c5ba0be4SMike Smith * or one if its parents. 1366c5ba0be4SMike Smith */ 1367c5ba0be4SMike Smith ACPI_STATUS 1368c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1369c5ba0be4SMike Smith { 1370c5ba0be4SMike Smith ACPI_HANDLE r; 1371c5ba0be4SMike Smith ACPI_STATUS status; 1372c5ba0be4SMike Smith 1373cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1374cb9b0d80SMike Smith 1375be2b1797SNate Lawson /* Walk back up the tree to the root */ 1376c5ba0be4SMike Smith for (;;) { 1377be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1378be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1379c5ba0be4SMike Smith *result = r; 1380c5ba0be4SMike Smith return (AE_OK); 1381c5ba0be4SMike Smith } 1382c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1383c5ba0be4SMike Smith return (AE_OK); 1384b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1385c5ba0be4SMike Smith return (AE_NOT_FOUND); 1386c5ba0be4SMike Smith parent = r; 1387c5ba0be4SMike Smith } 1388c5ba0be4SMike Smith } 1389c5ba0be4SMike Smith 1390eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1391eea17c34SNate Lawson uint32_t 1392eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1393eea17c34SNate Lawson { 1394eea17c34SNate Lawson uint32_t delta; 1395eea17c34SNate Lawson 1396eea17c34SNate Lawson if (end >= start) 1397eea17c34SNate Lawson delta = end - start; 1398eea17c34SNate Lawson else if (AcpiGbl_FADT->TmrValExt == 0) 13998d01ceefSNate Lawson delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1400eea17c34SNate Lawson else 1401eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 1402eea17c34SNate Lawson return (delta); 1403eea17c34SNate Lawson } 1404eea17c34SNate Lawson 1405c5ba0be4SMike Smith /* 1406c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1407c5ba0be4SMike Smith */ 1408c5ba0be4SMike Smith ACPI_BUFFER * 1409c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1410c5ba0be4SMike Smith { 1411c5ba0be4SMike Smith ACPI_BUFFER *buf; 1412c5ba0be4SMike Smith 1413c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1414c5ba0be4SMike Smith return (NULL); 1415c5ba0be4SMike Smith buf->Length = size; 1416c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1417c5ba0be4SMike Smith return (buf); 1418c5ba0be4SMike Smith } 1419c5ba0be4SMike Smith 1420c310653eSNate Lawson ACPI_STATUS 1421cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1422c310653eSNate Lawson { 1423c310653eSNate Lawson ACPI_OBJECT arg1; 1424c310653eSNate Lawson ACPI_OBJECT_LIST args; 1425c310653eSNate Lawson 1426c310653eSNate Lawson ACPI_ASSERTLOCK; 1427c310653eSNate Lawson 1428c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1429c310653eSNate Lawson arg1.Integer.Value = number; 1430c310653eSNate Lawson args.Count = 1; 1431c310653eSNate Lawson args.Pointer = &arg1; 1432c310653eSNate Lawson 1433c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1434c310653eSNate Lawson } 1435c310653eSNate Lawson 1436c5ba0be4SMike Smith /* 1437c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1438c5ba0be4SMike Smith */ 1439c5ba0be4SMike Smith ACPI_STATUS 1440cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1441c5ba0be4SMike Smith { 1442be2b1797SNate Lawson ACPI_STATUS status; 1443c5ba0be4SMike Smith ACPI_BUFFER buf; 1444c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1445c5ba0be4SMike Smith 1446cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1447cb9b0d80SMike Smith 1448c5ba0be4SMike Smith if (handle == NULL) 1449c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 14500c7da7acSJohn Baldwin 14510c7da7acSJohn Baldwin /* 14520c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 14530c7da7acSJohn Baldwin * a method that will return an Integer. 14540c7da7acSJohn Baldwin */ 1455c5ba0be4SMike Smith buf.Pointer = ¶m; 1456c5ba0be4SMike Smith buf.Length = sizeof(param); 1457be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1458be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1459be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1460c5ba0be4SMike Smith *number = param.Integer.Value; 1461be2b1797SNate Lawson else 1462be2b1797SNate Lawson status = AE_TYPE; 1463c5ba0be4SMike Smith } 14640c7da7acSJohn Baldwin 14650c7da7acSJohn Baldwin /* 14660c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 14670c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 14680c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 14690c7da7acSJohn Baldwin * convert it into an Integer as best we can. 14700c7da7acSJohn Baldwin * 14710c7da7acSJohn Baldwin * This is a hack. 14720c7da7acSJohn Baldwin */ 1473be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1474b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1475be2b1797SNate Lawson status = AE_NO_MEMORY; 14760c7da7acSJohn Baldwin } else { 1477be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1478be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1479be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 14800c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 14810c7da7acSJohn Baldwin } 1482f97739daSNate Lawson } 1483be2b1797SNate Lawson return (status); 1484c5ba0be4SMike Smith } 1485c5ba0be4SMike Smith 1486c573e654SMitsuru IWASAKI ACPI_STATUS 1487cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1488c573e654SMitsuru IWASAKI { 1489c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1490dba55fa2SNate Lawson UINT8 *val; 1491c573e654SMitsuru IWASAKI int i; 1492c573e654SMitsuru IWASAKI 1493c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1494c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1495c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1496c573e654SMitsuru IWASAKI return (AE_OK); 1497c573e654SMitsuru IWASAKI } 1498c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1499c573e654SMitsuru IWASAKI return (AE_TYPE); 1500c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1501c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1502be2b1797SNate Lawson 1503c573e654SMitsuru IWASAKI *number = 0; 1504dba55fa2SNate Lawson val = p->Buffer.Pointer; 1505c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1506dba55fa2SNate Lawson *number += val[i] << (i * 8); 1507c573e654SMitsuru IWASAKI return (AE_OK); 1508c573e654SMitsuru IWASAKI } 1509c573e654SMitsuru IWASAKI 1510c5ba0be4SMike Smith /* 1511c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1512c5ba0be4SMike Smith * function for each element. 1513c5ba0be4SMike Smith * 1514c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1515c5ba0be4SMike Smith */ 1516c5ba0be4SMike Smith ACPI_STATUS 1517be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1518be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1519c5ba0be4SMike Smith { 1520c5ba0be4SMike Smith ACPI_OBJECT *comp; 1521c5ba0be4SMike Smith int i; 1522c5ba0be4SMike Smith 1523be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1524c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1525c5ba0be4SMike Smith 1526be2b1797SNate Lawson /* Iterate over components */ 1527be2b1797SNate Lawson i = 0; 1528be2b1797SNate Lawson comp = pkg->Package.Elements; 1529be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1530c5ba0be4SMike Smith func(comp, arg); 1531c5ba0be4SMike Smith 1532c5ba0be4SMike Smith return (AE_OK); 1533c5ba0be4SMike Smith } 1534c5ba0be4SMike Smith 15356f69255bSMike Smith /* 15366f69255bSMike Smith * Find the (index)th resource object in a set. 15376f69255bSMike Smith */ 15386f69255bSMike Smith ACPI_STATUS 15396d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 15406f69255bSMike Smith { 15416d63101aSMike Smith ACPI_RESOURCE *rp; 15426f69255bSMike Smith int i; 15436f69255bSMike Smith 15446d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 15456f69255bSMike Smith i = index; 15466d63101aSMike Smith while (i-- > 0) { 1547be2b1797SNate Lawson /* Range check */ 15486d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 15496f69255bSMike Smith return (AE_BAD_PARAMETER); 1550be2b1797SNate Lawson 1551be2b1797SNate Lawson /* Check for terminator */ 1552be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 15536d63101aSMike Smith return (AE_NOT_FOUND); 1554abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 15556f69255bSMike Smith } 15566f69255bSMike Smith if (resp != NULL) 15576d63101aSMike Smith *resp = rp; 1558be2b1797SNate Lawson 15596d63101aSMike Smith return (AE_OK); 15606d63101aSMike Smith } 15616d63101aSMike Smith 15626d63101aSMike Smith /* 15636d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 15646d63101aSMike Smith * 15656d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 15666d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 15676d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 15686d63101aSMike Smith * resources. 15696d63101aSMike Smith */ 15706d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 15716d63101aSMike Smith 15726d63101aSMike Smith ACPI_STATUS 15736d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 15746d63101aSMike Smith { 15756d63101aSMike Smith ACPI_RESOURCE *rp; 15766d63101aSMike Smith void *newp; 15776d63101aSMike Smith 1578be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 15796d63101aSMike Smith if (buf->Pointer == NULL) { 15806d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 15816d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 15826d63101aSMike Smith return (AE_NO_MEMORY); 15836d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 15846d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 15856d63101aSMike Smith rp->Length = 0; 15866d63101aSMike Smith } 15876d63101aSMike Smith if (res == NULL) 15886d63101aSMike Smith return (AE_OK); 15896d63101aSMike Smith 15906d63101aSMike Smith /* 15916d63101aSMike Smith * Scan the current buffer looking for the terminator. 15926d63101aSMike Smith * This will either find the terminator or hit the end 15936d63101aSMike Smith * of the buffer and return an error. 15946d63101aSMike Smith */ 15956d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 15966d63101aSMike Smith for (;;) { 1597be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 15986d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 15996d63101aSMike Smith return (AE_BAD_PARAMETER); 1600be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16016d63101aSMike Smith break; 1602abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16036d63101aSMike Smith } 16046d63101aSMike Smith 16056d63101aSMike Smith /* 16066d63101aSMike Smith * Check the size of the buffer and expand if required. 16076d63101aSMike Smith * 16086d63101aSMike Smith * Required size is: 16096d63101aSMike Smith * size of existing resources before terminator + 16106d63101aSMike Smith * size of new resource and header + 16116d63101aSMike Smith * size of terminator. 16126d63101aSMike Smith * 16136d63101aSMike Smith * Note that this loop should really only run once, unless 16146d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 16156d63101aSMike Smith */ 16166d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 16176d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 16186d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 16196d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 16206d63101aSMike Smith return (AE_NO_MEMORY); 16216d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1622a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1623a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 16246d63101aSMike Smith AcpiOsFree(buf->Pointer); 16256d63101aSMike Smith buf->Pointer = newp; 16266d63101aSMike Smith buf->Length += buf->Length; 16276d63101aSMike Smith } 16286d63101aSMike Smith 1629be2b1797SNate Lawson /* Insert the new resource. */ 16306d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 16316d63101aSMike Smith 1632be2b1797SNate Lawson /* And add the terminator. */ 1633abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16346d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 16356d63101aSMike Smith rp->Length = 0; 16366d63101aSMike Smith 16376f69255bSMike Smith return (AE_OK); 16386f69255bSMike Smith } 1639c5ba0be4SMike Smith 1640da14ac9fSJohn Baldwin /* 1641da14ac9fSJohn Baldwin * Set interrupt model. 1642da14ac9fSJohn Baldwin */ 1643da14ac9fSJohn Baldwin ACPI_STATUS 1644da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1645da14ac9fSJohn Baldwin { 1646da14ac9fSJohn Baldwin 1647c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1648da14ac9fSJohn Baldwin } 1649da14ac9fSJohn Baldwin 1650ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1651ece50487SMitsuru IWASAKI 1652ece50487SMitsuru IWASAKI static void 1653ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1654ece50487SMitsuru IWASAKI { 1655ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1656ece50487SMitsuru IWASAKI } 1657c30382dfSMitsuru IWASAKI 165815e32d5dSMike Smith /* 165915e32d5dSMike Smith * Set the system sleep state 166015e32d5dSMike Smith * 1661be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 166215e32d5dSMike Smith */ 166315e32d5dSMike Smith ACPI_STATUS 166415e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 166515e32d5dSMike Smith { 166615e32d5dSMike Smith ACPI_STATUS status = AE_OK; 166756d8cb57SMitsuru IWASAKI UINT8 TypeA; 166856d8cb57SMitsuru IWASAKI UINT8 TypeB; 166915e32d5dSMike Smith 1670b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1671cb9b0d80SMike Smith ACPI_ASSERTLOCK; 16720ae55423SMike Smith 167398175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 16746397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 167598175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 16766397624dSMitsuru IWASAKI 167798175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1678ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1679ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1680ece50487SMitsuru IWASAKI 168115e32d5dSMike Smith switch (state) { 168215e32d5dSMike Smith case ACPI_STATE_S1: 16836161544cSTakanori Watanabe case ACPI_STATE_S2: 16846161544cSTakanori Watanabe case ACPI_STATE_S3: 16856161544cSTakanori Watanabe case ACPI_STATE_S4: 1686be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 168798175614SNate Lawson if (status == AE_NOT_FOUND) { 168898175614SNate Lawson device_printf(sc->acpi_dev, 168998175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 169098175614SNate Lawson break; 169198175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1692be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1693be2b1797SNate Lawson AcpiFormatException(status)); 169456d8cb57SMitsuru IWASAKI break; 169556d8cb57SMitsuru IWASAKI } 169656d8cb57SMitsuru IWASAKI 1697a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1698a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1699a1fccb47SMitsuru IWASAKI 1700e8b4d56eSNate Lawson /* Disable all wake GPEs not appropriate for this state. */ 1701e8b4d56eSNate Lawson acpi_wake_limit_walk(state); 1702e8b4d56eSNate Lawson 1703be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 170415e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 170515e32d5dSMike Smith /* 170615e32d5dSMike Smith * Re-wake the system. 170715e32d5dSMike Smith * 170815e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 170915e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 171015e32d5dSMike Smith * current bus interface does not provide for this. 171115e32d5dSMike Smith */ 171215e32d5dSMike Smith DEVICE_RESUME(root_bus); 17130ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 171415e32d5dSMike Smith } 1715b9c780d6SMitsuru IWASAKI 1716be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1717be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1718b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1719b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1720b9c780d6SMitsuru IWASAKI break; 1721b9c780d6SMitsuru IWASAKI } 1722b9c780d6SMitsuru IWASAKI 1723be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1724ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1725ff01efb5SMitsuru IWASAKI 17266161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 17276161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 17286161544cSTakanori Watanabe 1729be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 173059ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 173159ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 173259ddeb18SNate Lawson 17336161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1734a1fccb47SMitsuru IWASAKI } 17356161544cSTakanori Watanabe 17366161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1737be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1738964679ceSMitsuru IWASAKI AcpiEnable(); 17396161544cSTakanori Watanabe } else { 1740be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1741be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1742be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1743be2b1797SNate Lawson AcpiFormatException(status)); 1744c30382dfSMitsuru IWASAKI break; 174515e32d5dSMike Smith } 17466161544cSTakanori Watanabe } 1747ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 174815e32d5dSMike Smith DEVICE_RESUME(root_bus); 174915e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 175013d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 175115e32d5dSMike Smith break; 175215e32d5dSMike Smith case ACPI_STATE_S5: 175315e32d5dSMike Smith /* 175415e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 175515e32d5dSMike Smith * shutdown handlers. 175615e32d5dSMike Smith */ 175715e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 175815e32d5dSMike Smith break; 175998175614SNate Lawson case ACPI_STATE_S0: 176015e32d5dSMike Smith default: 176115e32d5dSMike Smith status = AE_BAD_PARAMETER; 176215e32d5dSMike Smith break; 176315e32d5dSMike Smith } 1764ece50487SMitsuru IWASAKI 176598175614SNate Lawson /* Disable a second sleep request for a short period */ 1766ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1767ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1768ece50487SMitsuru IWASAKI 17690ae55423SMike Smith return_ACPI_STATUS (status); 177015e32d5dSMike Smith } 177115e32d5dSMike Smith 1772e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1773e8b4d56eSNate Lawson int 1774e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1775e8b4d56eSNate Lawson { 1776e8b4d56eSNate Lawson struct acpi_prw_data prw; 1777e8b4d56eSNate Lawson 1778e8b4d56eSNate Lawson /* Check that the device can wake the system. */ 1779e8b4d56eSNate Lawson if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0) 1780e8b4d56eSNate Lawson return (ENXIO); 1781e8b4d56eSNate Lawson 1782e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1783e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1784e8b4d56eSNate Lawson return (ENXIO); 1785e8b4d56eSNate Lawson 1786e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1787e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1788e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1789e8b4d56eSNate Lawson return (ENXIO); 1790e8b4d56eSNate Lawson } 1791e8b4d56eSNate Lawson 1792e8b4d56eSNate Lawson return (0); 1793e8b4d56eSNate Lawson } 1794e8b4d56eSNate Lawson 1795e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1796e8b4d56eSNate Lawson int 1797e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1798e8b4d56eSNate Lawson { 1799e8b4d56eSNate Lawson struct acpi_prw_data prw; 1800e8b4d56eSNate Lawson ACPI_HANDLE handle; 1801e8b4d56eSNate Lawson ACPI_STATUS status; 1802e8b4d56eSNate Lawson int flags; 1803e8b4d56eSNate Lawson 1804e8b4d56eSNate Lawson /* Make sure the device supports waking the system. */ 1805e8b4d56eSNate Lawson flags = device_get_flags(dev); 1806e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1807e8b4d56eSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1808e8b4d56eSNate Lawson return (ENXIO); 1809e8b4d56eSNate Lawson 1810e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1811e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1812e8b4d56eSNate Lawson return (ENXIO); 1813e8b4d56eSNate Lawson 1814e8b4d56eSNate Lawson if (enable) { 1815e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1816e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1817e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1818e8b4d56eSNate Lawson return (ENXIO); 1819e8b4d56eSNate Lawson } 1820e8b4d56eSNate Lawson device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1821e8b4d56eSNate Lawson } else { 1822e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1823e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1824e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1825e8b4d56eSNate Lawson return (ENXIO); 1826e8b4d56eSNate Lawson } 1827e8b4d56eSNate Lawson device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1828e8b4d56eSNate Lawson } 1829e8b4d56eSNate Lawson 1830e8b4d56eSNate Lawson return (0); 1831e8b4d56eSNate Lawson } 1832e8b4d56eSNate Lawson 1833e8b4d56eSNate Lawson /* Configure a device's GPE appropriately for the new sleep state. */ 1834e8b4d56eSNate Lawson int 1835e8b4d56eSNate Lawson acpi_wake_sleep_prep(device_t dev, int sstate) 1836e8b4d56eSNate Lawson { 1837e8b4d56eSNate Lawson struct acpi_prw_data prw; 1838e8b4d56eSNate Lawson ACPI_HANDLE handle; 1839cc85c78cSNate Lawson int flags; 1840e8b4d56eSNate Lawson 1841e8b4d56eSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1842cc85c78cSNate Lawson flags = device_get_flags(dev); 1843e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1844cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1845e8b4d56eSNate Lawson return (ENXIO); 1846cc85c78cSNate Lawson 1847cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1848e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1849e8b4d56eSNate Lawson return (ENXIO); 1850e8b4d56eSNate Lawson 1851e8b4d56eSNate Lawson /* 1852cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1853cc85c78cSNate Lawson * of the _PRW object are put into the ON state. 1854e8b4d56eSNate Lawson */ 1855cc85c78cSNate Lawson 1856cc85c78cSNate Lawson /* 1857cc85c78cSNate Lawson * If the user requested that this device wake the system and the next 1858cc85c78cSNate Lawson * sleep state is valid for this GPE, enable it and the device's wake 1859cc85c78cSNate Lawson * capability. The sleep state must be less than (i.e., higher power) 1860cc85c78cSNate Lawson * or equal to the value specified by _PRW. Return early, leaving 1861cc85c78cSNate Lawson * the appropriate power resources enabled. 1862cc85c78cSNate Lawson */ 1863cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 && 1864cc85c78cSNate Lawson sstate <= prw.lowest_wake) { 1865e8b4d56eSNate Lawson if (bootverbose) 1866cc85c78cSNate Lawson device_printf(dev, "wake_prep enabled gpe %#x for state %d\n", 1867e8b4d56eSNate Lawson prw.gpe_bit, sstate); 1868cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1869cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 1); 1870e8b4d56eSNate Lawson return (0); 1871e8b4d56eSNate Lawson } 1872e8b4d56eSNate Lawson 1873e8b4d56eSNate Lawson /* 1874cc85c78cSNate Lawson * If the device wake was disabled or this sleep state is too low for 1875cc85c78cSNate Lawson * this device, disable its wake capability and GPE. 1876e8b4d56eSNate Lawson */ 1877cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1878cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1879e8b4d56eSNate Lawson if (bootverbose) 1880cc85c78cSNate Lawson device_printf(dev, "wake_prep disabled gpe %#x for state %d\n", 1881cc85c78cSNate Lawson prw.gpe_bit, sstate); 1882cc85c78cSNate Lawson 1883cc85c78cSNate Lawson /* 1884cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1885cc85c78cSNate Lawson * of the _PRW object are put into the OFF state. 1886cc85c78cSNate Lawson */ 1887cc85c78cSNate Lawson 1888cc85c78cSNate Lawson return (0); 1889e8b4d56eSNate Lawson } 1890e8b4d56eSNate Lawson 1891cc85c78cSNate Lawson /* Re-enable GPEs after wake. */ 1892cc85c78cSNate Lawson int 1893cc85c78cSNate Lawson acpi_wake_run_prep(device_t dev) 1894cc85c78cSNate Lawson { 1895cc85c78cSNate Lawson struct acpi_prw_data prw; 1896cc85c78cSNate Lawson ACPI_HANDLE handle; 1897cc85c78cSNate Lawson int flags; 1898cc85c78cSNate Lawson 1899cc85c78cSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1900cc85c78cSNate Lawson flags = device_get_flags(dev); 1901cc85c78cSNate Lawson handle = acpi_get_handle(dev); 1902cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1903cc85c78cSNate Lawson return (ENXIO); 1904cc85c78cSNate Lawson 1905cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1906cc85c78cSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1907cc85c78cSNate Lawson return (ENXIO); 1908cc85c78cSNate Lawson 1909cc85c78cSNate Lawson /* 1910cc85c78cSNate Lawson * TBD: Be sure all Power Resources referenced by elements 2 through N 1911cc85c78cSNate Lawson * of the _PRW object are in the ON state. 1912cc85c78cSNate Lawson */ 1913cc85c78cSNate Lawson 1914cc85c78cSNate Lawson /* Disable wake capability and if the user requested, enable the GPE. */ 1915cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 1916cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0) 1917cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1918e8b4d56eSNate Lawson return (0); 1919e8b4d56eSNate Lawson } 1920e8b4d56eSNate Lawson 1921e8b4d56eSNate Lawson static ACPI_STATUS 1922e8b4d56eSNate Lawson acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status) 1923e8b4d56eSNate Lawson { 1924e8b4d56eSNate Lawson struct acpi_prw_data prw; 192544b8ae71SNate Lawson int *sstate; 1926e8b4d56eSNate Lawson 1927e8b4d56eSNate Lawson /* It's ok not to have _PRW if the device can't wake the system. */ 1928e8b4d56eSNate Lawson if (acpi_parse_prw(h, &prw) != 0) 1929e8b4d56eSNate Lawson return (AE_OK); 1930e8b4d56eSNate Lawson 193144b8ae71SNate Lawson sstate = (int *)context; 193244b8ae71SNate Lawson if (*sstate > prw.lowest_wake) 1933e8b4d56eSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1934e8b4d56eSNate Lawson 1935e8b4d56eSNate Lawson return (AE_OK); 1936e8b4d56eSNate Lawson } 1937e8b4d56eSNate Lawson 1938e8b4d56eSNate Lawson /* Walk all system devices, disabling them if necessary for sstate. */ 1939e8b4d56eSNate Lawson static int 1940e8b4d56eSNate Lawson acpi_wake_limit_walk(int sstate) 1941e8b4d56eSNate Lawson { 1942e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 1943e8b4d56eSNate Lawson 1944e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 1945e8b4d56eSNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100, 194644b8ae71SNate Lawson acpi_wake_limit, &sstate, NULL); 1947e8b4d56eSNate Lawson return (0); 1948e8b4d56eSNate Lawson } 1949e8b4d56eSNate Lawson 195088a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 195188a79fc0SNate Lawson static int 195288a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 195388a79fc0SNate Lawson { 195488a79fc0SNate Lawson int error, i, numdevs; 195588a79fc0SNate Lawson device_t *devlist; 195688a79fc0SNate Lawson device_t child; 195788a79fc0SNate Lawson 195888a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 195988a79fc0SNate Lawson if (error != 0 || numdevs == 0) 196088a79fc0SNate Lawson return (error); 196188a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 196288a79fc0SNate Lawson child = devlist[i]; 196388a79fc0SNate Lawson if (!device_is_attached(child)) 196488a79fc0SNate Lawson continue; 196588a79fc0SNate Lawson if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) { 196688a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 196788a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 196888a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 196988a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 197088a79fc0SNate Lawson } 197188a79fc0SNate Lawson acpi_wake_sysctl_walk(child); 197288a79fc0SNate Lawson } 197388a79fc0SNate Lawson free(devlist, M_TEMP); 197488a79fc0SNate Lawson 197588a79fc0SNate Lawson return (0); 197688a79fc0SNate Lawson } 197788a79fc0SNate Lawson 197888a79fc0SNate Lawson /* Enable or disable wake from userland. */ 197988a79fc0SNate Lawson static int 198088a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 198188a79fc0SNate Lawson { 198288a79fc0SNate Lawson int enable, error; 198388a79fc0SNate Lawson device_t dev; 198488a79fc0SNate Lawson 198588a79fc0SNate Lawson dev = (device_t)arg1; 198688a79fc0SNate Lawson enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 198788a79fc0SNate Lawson 198888a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 198988a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 199088a79fc0SNate Lawson return (error); 199188a79fc0SNate Lawson if (enable != 0 && enable != 1) 199288a79fc0SNate Lawson return (EINVAL); 199388a79fc0SNate Lawson 199488a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 199588a79fc0SNate Lawson } 199688a79fc0SNate Lawson 1997e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 1998e8b4d56eSNate Lawson static int 1999e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2000e8b4d56eSNate Lawson { 2001e8b4d56eSNate Lawson ACPI_STATUS status; 2002e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2003e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2004e8b4d56eSNate Lawson int error; 2005e8b4d56eSNate Lawson 2006e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2007e8b4d56eSNate Lawson return (EINVAL); 2008e8b4d56eSNate Lawson 2009e8b4d56eSNate Lawson /* 2010e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2011e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2012e8b4d56eSNate Lawson */ 2013e8b4d56eSNate Lawson error = EINVAL; 2014e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2015e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2016e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2017e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2018e8b4d56eSNate Lawson return (ENOENT); 2019e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2020e8b4d56eSNate Lawson if (res == NULL) 2021e8b4d56eSNate Lawson return (ENOENT); 2022e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2023e8b4d56eSNate Lawson goto out; 2024e8b4d56eSNate Lawson 2025e8b4d56eSNate Lawson /* 2026e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2027e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2028e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2029e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2030e8b4d56eSNate Lawson */ 2031e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2032e8b4d56eSNate Lawson goto out; 2033e8b4d56eSNate Lawson 2034e8b4d56eSNate Lawson /* 2035e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2036e8b4d56eSNate Lawson */ 2037e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2038e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2039e8b4d56eSNate Lawson /* 2040e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2041e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2042e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2043e8b4d56eSNate Lawson * enabled for the wake event. 2044e8b4d56eSNate Lawson */ 2045e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2046e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2047e8b4d56eSNate Lawson error = 0; 2048e8b4d56eSNate Lawson break; 2049e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2050e8b4d56eSNate Lawson /* 2051e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2052e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2053e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2054e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2055e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2056e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2057e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2058e8b4d56eSNate Lawson * the wake event. 2059e8b4d56eSNate Lawson * 2060e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2061e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2062e8b4d56eSNate Lawson */ 2063e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2064e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2065e8b4d56eSNate Lawson goto out; 2066e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2067e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2068e8b4d56eSNate Lawson goto out; 2069e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2070e8b4d56eSNate Lawson goto out; 2071e8b4d56eSNate Lawson error = 0; 2072e8b4d56eSNate Lawson break; 2073e8b4d56eSNate Lawson default: 2074e8b4d56eSNate Lawson goto out; 2075e8b4d56eSNate Lawson } 2076e8b4d56eSNate Lawson 2077e8b4d56eSNate Lawson /* XXX No power resource handling yet. */ 2078e8b4d56eSNate Lawson prw->power_res = NULL; 2079e8b4d56eSNate Lawson 2080e8b4d56eSNate Lawson out: 2081e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2082e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2083e8b4d56eSNate Lawson return (error); 2084e8b4d56eSNate Lawson } 2085e8b4d56eSNate Lawson 208615e32d5dSMike Smith /* 208715e32d5dSMike Smith * Enable/Disable ACPI 208815e32d5dSMike Smith */ 208915e32d5dSMike Smith ACPI_STATUS 209015e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 209115e32d5dSMike Smith { 209215e32d5dSMike Smith ACPI_STATUS status; 209315e32d5dSMike Smith u_int32_t flags; 209415e32d5dSMike Smith 2095b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2096cb9b0d80SMike Smith ACPI_ASSERTLOCK; 20970ae55423SMike Smith 209815e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 209915e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 2100be2b1797SNate Lawson if (!sc->acpi_enabled) 210115e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 2102be2b1797SNate Lawson else 210315e32d5dSMike Smith status = AE_OK; 2104be2b1797SNate Lawson 210515e32d5dSMike Smith if (status == AE_OK) 210615e32d5dSMike Smith sc->acpi_enabled = 1; 2107be2b1797SNate Lawson 21080ae55423SMike Smith return_ACPI_STATUS (status); 210915e32d5dSMike Smith } 211015e32d5dSMike Smith 211115e32d5dSMike Smith ACPI_STATUS 211215e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 211315e32d5dSMike Smith { 211415e32d5dSMike Smith ACPI_STATUS status; 211515e32d5dSMike Smith 2116b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2117cb9b0d80SMike Smith ACPI_ASSERTLOCK; 21180ae55423SMike Smith 2119be2b1797SNate Lawson if (sc->acpi_enabled) 212015e32d5dSMike Smith status = AcpiDisable(); 2121be2b1797SNate Lawson else 212215e32d5dSMike Smith status = AE_OK; 2123be2b1797SNate Lawson 212415e32d5dSMike Smith if (status == AE_OK) 212515e32d5dSMike Smith sc->acpi_enabled = 0; 2126be2b1797SNate Lawson 21270ae55423SMike Smith return_ACPI_STATUS (status); 212815e32d5dSMike Smith } 212915e32d5dSMike Smith 213015e32d5dSMike Smith /* 213115e32d5dSMike Smith * ACPI Event Handlers 213215e32d5dSMike Smith */ 213315e32d5dSMike Smith 213415e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 213515e32d5dSMike Smith 213615e32d5dSMike Smith static void 213715e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 213815e32d5dSMike Smith { 2139fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2140b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 214115e32d5dSMike Smith 2142cb9b0d80SMike Smith ACPI_LOCK; 2143a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 214415e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2145cb9b0d80SMike Smith ACPI_UNLOCK; 21460ae55423SMike Smith return_VOID; 214715e32d5dSMike Smith } 214815e32d5dSMike Smith 214915e32d5dSMike Smith static void 215015e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 215115e32d5dSMike Smith { 2152fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2153b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 21540ae55423SMike Smith 215515e32d5dSMike Smith /* Well, what to do? :-) */ 21560ae55423SMike Smith 2157cb9b0d80SMike Smith ACPI_LOCK; 2158cb9b0d80SMike Smith ACPI_UNLOCK; 2159cb9b0d80SMike Smith 21600ae55423SMike Smith return_VOID; 216115e32d5dSMike Smith } 216215e32d5dSMike Smith 216315e32d5dSMike Smith /* 216415e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 216515e32d5dSMike Smith */ 216615e32d5dSMike Smith UINT32 216733febf93SNate Lawson acpi_event_power_button_sleep(void *context) 216815e32d5dSMike Smith { 216915e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 217015e32d5dSMike Smith 2171b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 21720ae55423SMike Smith 217315e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 21740ae55423SMike Smith 2175b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 217615e32d5dSMike Smith } 217715e32d5dSMike Smith 217815e32d5dSMike Smith UINT32 217933febf93SNate Lawson acpi_event_power_button_wake(void *context) 218015e32d5dSMike Smith { 218115e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 218215e32d5dSMike Smith 2183b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 21840ae55423SMike Smith 218515e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 21860ae55423SMike Smith 2187b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 218815e32d5dSMike Smith } 218915e32d5dSMike Smith 219015e32d5dSMike Smith UINT32 219133febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 219215e32d5dSMike Smith { 219315e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 219415e32d5dSMike Smith 2195b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 21960ae55423SMike Smith 219715e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 21980ae55423SMike Smith 2199b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 220015e32d5dSMike Smith } 220115e32d5dSMike Smith 220215e32d5dSMike Smith UINT32 220333febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 220415e32d5dSMike Smith { 220515e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 220615e32d5dSMike Smith 2207b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22080ae55423SMike Smith 220915e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 22100ae55423SMike Smith 2211b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 221215e32d5dSMike Smith } 221315e32d5dSMike Smith 221415e32d5dSMike Smith /* 221515e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 221615e32d5dSMike Smith */ 221715e32d5dSMike Smith struct acpi_staticbuf { 221815e32d5dSMike Smith ACPI_BUFFER buffer; 221915e32d5dSMike Smith char data[512]; 222015e32d5dSMike Smith }; 222115e32d5dSMike Smith 222215e32d5dSMike Smith char * 222315e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 222415e32d5dSMike Smith { 222515e32d5dSMike Smith static struct acpi_staticbuf buf; 222615e32d5dSMike Smith 2227cb9b0d80SMike Smith ACPI_ASSERTLOCK; 2228cb9b0d80SMike Smith 222915e32d5dSMike Smith buf.buffer.Length = 512; 223015e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 223115e32d5dSMike Smith 2232b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 223315e32d5dSMike Smith return (buf.buffer.Pointer); 2234be2b1797SNate Lawson 223515e32d5dSMike Smith return ("(unknown path)"); 223615e32d5dSMike Smith } 223715e32d5dSMike Smith 223815e32d5dSMike Smith /* 223915e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 224015e32d5dSMike Smith * parts of the namespace. 224115e32d5dSMike Smith */ 224215e32d5dSMike Smith int 224315e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 224415e32d5dSMike Smith { 22453c600cfbSJohn Baldwin char *cp, *env, *np; 224615e32d5dSMike Smith int len; 224715e32d5dSMike Smith 224815e32d5dSMike Smith np = acpi_name(handle); 224915e32d5dSMike Smith if (*np == '\\') 225015e32d5dSMike Smith np++; 22513c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 225215e32d5dSMike Smith return (0); 225315e32d5dSMike Smith 2254be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 22553c600cfbSJohn Baldwin cp = env; 225615e32d5dSMike Smith for (;;) { 225715e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 225815e32d5dSMike Smith cp++; 225915e32d5dSMike Smith if (*cp == 0) 226015e32d5dSMike Smith break; 226115e32d5dSMike Smith len = 0; 226215e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 226315e32d5dSMike Smith len++; 2264d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 22653c600cfbSJohn Baldwin freeenv(env); 22660ae55423SMike Smith return(1); 2267d786139cSMaxime Henrion } 22680ae55423SMike Smith cp += len; 22690ae55423SMike Smith } 22703c600cfbSJohn Baldwin freeenv(env); 2271be2b1797SNate Lawson 22720ae55423SMike Smith return (0); 22730ae55423SMike Smith } 22740ae55423SMike Smith 22750ae55423SMike Smith /* 22760ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 22770ae55423SMike Smith */ 22780ae55423SMike Smith int 22790ae55423SMike Smith acpi_disabled(char *subsys) 22800ae55423SMike Smith { 228178689b15SMaxime Henrion char *cp, *env; 22820ae55423SMike Smith int len; 22830ae55423SMike Smith 22843184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 22850ae55423SMike Smith return (0); 22863184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 228778689b15SMaxime Henrion freeenv(env); 22880ae55423SMike Smith return (1); 2289d786139cSMaxime Henrion } 22900ae55423SMike Smith 22913184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 229278689b15SMaxime Henrion cp = env; 22930ae55423SMike Smith for (;;) { 22943184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 22950ae55423SMike Smith cp++; 22963184cf5aSNate Lawson if (*cp == '\0') 22970ae55423SMike Smith break; 22980ae55423SMike Smith len = 0; 22993184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 23000ae55423SMike Smith len++; 23013184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 230278689b15SMaxime Henrion freeenv(env); 230315e32d5dSMike Smith return (1); 2304d786139cSMaxime Henrion } 230515e32d5dSMike Smith cp += len; 230615e32d5dSMike Smith } 230778689b15SMaxime Henrion freeenv(env); 2308be2b1797SNate Lawson 230915e32d5dSMike Smith return (0); 231015e32d5dSMike Smith } 231115e32d5dSMike Smith 231215e32d5dSMike Smith /* 231315e32d5dSMike Smith * Control interface. 231415e32d5dSMike Smith * 23150ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2316be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2317be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 231815e32d5dSMike Smith */ 23190ae55423SMike Smith struct acpi_ioctl_hook 23200ae55423SMike Smith { 23210ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 23220ae55423SMike Smith u_long cmd; 2323be2b1797SNate Lawson acpi_ioctl_fn fn; 23240ae55423SMike Smith void *arg; 23250ae55423SMike Smith }; 23260ae55423SMike Smith 23270ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 23280ae55423SMike Smith static int acpi_ioctl_hooks_initted; 23290ae55423SMike Smith 23300ae55423SMike Smith /* 23310ae55423SMike Smith * Register an ioctl handler. 23320ae55423SMike Smith */ 23330ae55423SMike Smith int 2334be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 23350ae55423SMike Smith { 23360ae55423SMike Smith struct acpi_ioctl_hook *hp; 23370ae55423SMike Smith 23380ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 23390ae55423SMike Smith return (ENOMEM); 23400ae55423SMike Smith hp->cmd = cmd; 23410ae55423SMike Smith hp->fn = fn; 23420ae55423SMike Smith hp->arg = arg; 23430ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 23440ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 23450ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 23460ae55423SMike Smith } 23470ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 23480ae55423SMike Smith return (0); 23490ae55423SMike Smith } 23500ae55423SMike Smith 23510ae55423SMike Smith /* 23520ae55423SMike Smith * Deregister an ioctl handler. 23530ae55423SMike Smith */ 23540ae55423SMike Smith void 2355be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 23560ae55423SMike Smith { 23570ae55423SMike Smith struct acpi_ioctl_hook *hp; 23580ae55423SMike Smith 23590ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 23600ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 23610ae55423SMike Smith break; 23620ae55423SMike Smith 23630ae55423SMike Smith if (hp != NULL) { 23640ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 23650ae55423SMike Smith free(hp, M_ACPIDEV); 23660ae55423SMike Smith } 23670ae55423SMike Smith } 23680ae55423SMike Smith 236915e32d5dSMike Smith static int 23706b4d1b08SJohn Baldwin acpiopen(dev_t dev, int flag, int fmt, d_thread_t *td) 237115e32d5dSMike Smith { 237215e32d5dSMike Smith return (0); 237315e32d5dSMike Smith } 237415e32d5dSMike Smith 237515e32d5dSMike Smith static int 23766b4d1b08SJohn Baldwin acpiclose(dev_t dev, int flag, int fmt, d_thread_t *td) 237715e32d5dSMike Smith { 237815e32d5dSMike Smith return (0); 237915e32d5dSMike Smith } 238015e32d5dSMike Smith 238115e32d5dSMike Smith static int 23826b4d1b08SJohn Baldwin acpiioctl(dev_t dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 238315e32d5dSMike Smith { 238415e32d5dSMike Smith struct acpi_softc *sc; 23850ae55423SMike Smith struct acpi_ioctl_hook *hp; 23860ae55423SMike Smith int error, xerror, state; 2387fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 238815e32d5dSMike Smith 2389cb9b0d80SMike Smith ACPI_LOCK; 2390cb9b0d80SMike Smith 239115e32d5dSMike Smith error = state = 0; 239215e32d5dSMike Smith sc = dev->si_drv1; 239315e32d5dSMike Smith 23940ae55423SMike Smith /* 23950ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 23960ae55423SMike Smith */ 23970ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 23980ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 23990ae55423SMike Smith if (hp->cmd == cmd) { 24000ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 24010ae55423SMike Smith if (xerror != 0) 24020ae55423SMike Smith error = xerror; 2403917d44c8SMitsuru IWASAKI goto out; 24040ae55423SMike Smith } 24050ae55423SMike Smith } 24060ae55423SMike Smith } 24070ae55423SMike Smith 24080ae55423SMike Smith /* 2409a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2410a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2411a89fcf28STakanori Watanabe * Not changing system behavior. 2412a89fcf28STakanori Watanabe */ 2413be2b1797SNate Lawson if((flag & FWRITE) == 0) 2414be2b1797SNate Lawson return (EPERM); 2415a89fcf28STakanori Watanabe 2416be2b1797SNate Lawson /* Core system ioctls. */ 241715e32d5dSMike Smith switch (cmd) { 241815e32d5dSMike Smith case ACPIIO_ENABLE: 24190ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 242015e32d5dSMike Smith error = ENXIO; 242115e32d5dSMike Smith break; 242215e32d5dSMike Smith case ACPIIO_DISABLE: 24230ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 242415e32d5dSMike Smith error = ENXIO; 242515e32d5dSMike Smith break; 242615e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 242715e32d5dSMike Smith if (!sc->acpi_enabled) { 242815e32d5dSMike Smith error = ENXIO; 242915e32d5dSMike Smith break; 243015e32d5dSMike Smith } 243115e32d5dSMike Smith state = *(int *)addr; 24322d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 24332d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 243415e32d5dSMike Smith error = EINVAL; 24352d610c46SNate Lawson } else { 24362d610c46SNate Lawson error = EINVAL; 24372d610c46SNate Lawson } 243815e32d5dSMike Smith break; 243915e32d5dSMike Smith default: 24400ae55423SMike Smith if (error == 0) 244115e32d5dSMike Smith error = EINVAL; 244215e32d5dSMike Smith break; 244315e32d5dSMike Smith } 2444917d44c8SMitsuru IWASAKI 2445917d44c8SMitsuru IWASAKI out: 2446cb9b0d80SMike Smith ACPI_UNLOCK; 244715e32d5dSMike Smith return (error); 244815e32d5dSMike Smith } 244915e32d5dSMike Smith 24501d073b1dSJohn Baldwin static int 2451d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2452d75de536SMitsuru IWASAKI { 2453d75de536SMitsuru IWASAKI char sleep_state[4]; 2454d75de536SMitsuru IWASAKI char buf[16]; 2455d75de536SMitsuru IWASAKI int error; 2456d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2457d75de536SMitsuru IWASAKI 2458d75de536SMitsuru IWASAKI buf[0] = '\0'; 2459d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) { 2460d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2461d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2462d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2463d75de536SMitsuru IWASAKI } 2464d75de536SMitsuru IWASAKI } 2465d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2466d75de536SMitsuru IWASAKI return (error); 2467d75de536SMitsuru IWASAKI } 2468d75de536SMitsuru IWASAKI 2469d75de536SMitsuru IWASAKI static int 24701d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 24711d073b1dSJohn Baldwin { 24721d073b1dSJohn Baldwin char sleep_state[10]; 24731d073b1dSJohn Baldwin int error; 24741d073b1dSJohn Baldwin u_int new_state, old_state; 24751d073b1dSJohn Baldwin 24761d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2477b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX + 1) { 24781d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2479cb9b0d80SMike Smith } else { 2480b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 24811d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 24821d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2483cb9b0d80SMike Smith } 24841d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 24851d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2486be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2487be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 24881d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 24891d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 24901d073b1dSJohn Baldwin break; 2491cb9b0d80SMike Smith } 2492931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2493be2b1797SNate Lawson if (new_state != old_state) 24941d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2495cb9b0d80SMike Smith } else { 24961d073b1dSJohn Baldwin error = EINVAL; 24971d073b1dSJohn Baldwin } 2498cb9b0d80SMike Smith } 2499be2b1797SNate Lawson 25001d073b1dSJohn Baldwin return (error); 25011d073b1dSJohn Baldwin } 25021d073b1dSJohn Baldwin 25039b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 25049b937d48SNate Lawson void 25059b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 25069b937d48SNate Lawson { 25079b937d48SNate Lawson char notify_buf[16]; 25089b937d48SNate Lawson ACPI_BUFFER handle_buf; 25099b937d48SNate Lawson ACPI_STATUS status; 25109b937d48SNate Lawson 25119b937d48SNate Lawson if (subsystem == NULL) 25129b937d48SNate Lawson return; 25139b937d48SNate Lawson 25149b937d48SNate Lawson handle_buf.Pointer = NULL; 25159b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 25169b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 25179b937d48SNate Lawson if (ACPI_FAILURE(status)) 25189b937d48SNate Lawson return; 25199b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 25209b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 25219b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 25229b937d48SNate Lawson } 25239b937d48SNate Lawson 252415e32d5dSMike Smith #ifdef ACPI_DEBUG 25250ae55423SMike Smith /* 25260ae55423SMike Smith * Support for parsing debug options from the kernel environment. 25270ae55423SMike Smith * 25280ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 25290ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 25300ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 25310ae55423SMike Smith * prefixing the bit name with !. 25320ae55423SMike Smith */ 253315e32d5dSMike Smith struct debugtag 253415e32d5dSMike Smith { 253515e32d5dSMike Smith char *name; 253615e32d5dSMike Smith UINT32 value; 253715e32d5dSMike Smith }; 253815e32d5dSMike Smith 253915e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2540c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2541c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2542c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2543c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2544c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2545c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2546c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2547c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2548c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2549d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 25504c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2551d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2552297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 25534c1cdee6SMike Smith 2554c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2555c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 25563184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2557c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 25583184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 25593184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 25603184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 25614c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2562cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 25633184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2564b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 256515e32d5dSMike Smith {NULL, 0} 256615e32d5dSMike Smith }; 256715e32d5dSMike Smith 256815e32d5dSMike Smith static struct debugtag dbg_level[] = { 25694c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 25709501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 25719501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 25724c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 25739501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 25744c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2575d62ab2f4SMitsuru IWASAKI 2576d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2577297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 25784c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 25794c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2580d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 25814c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 25824c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 25834c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 25844c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 25854c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 25864c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 25874c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 25884c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 25894c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 25904c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2591d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2592d62ab2f4SMitsuru IWASAKI 2593d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2594d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2595d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2596d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2597d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 25984c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2599d62ab2f4SMitsuru IWASAKI 2600d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2601d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2602d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2603d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2604d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2605d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2606d62ab2f4SMitsuru IWASAKI 2607d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 260898479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 260998479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 261098479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 261198479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 261298479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 261315e32d5dSMike Smith {NULL, 0} 261415e32d5dSMike Smith }; 261515e32d5dSMike Smith 261615e32d5dSMike Smith static void 261715e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 261815e32d5dSMike Smith { 261915e32d5dSMike Smith char *ep; 262015e32d5dSMike Smith int i, l; 26210ae55423SMike Smith int set; 262215e32d5dSMike Smith 262315e32d5dSMike Smith while (*cp) { 262415e32d5dSMike Smith if (isspace(*cp)) { 262515e32d5dSMike Smith cp++; 262615e32d5dSMike Smith continue; 262715e32d5dSMike Smith } 262815e32d5dSMike Smith ep = cp; 262915e32d5dSMike Smith while (*ep && !isspace(*ep)) 263015e32d5dSMike Smith ep++; 26310ae55423SMike Smith if (*cp == '!') { 26320ae55423SMike Smith set = 0; 26330ae55423SMike Smith cp++; 26340ae55423SMike Smith if (cp == ep) 26350ae55423SMike Smith continue; 26360ae55423SMike Smith } else { 26370ae55423SMike Smith set = 1; 26380ae55423SMike Smith } 263915e32d5dSMike Smith l = ep - cp; 264015e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 264115e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2642be2b1797SNate Lawson if (set) 264315e32d5dSMike Smith *flag |= tag[i].value; 2644be2b1797SNate Lawson else 26450ae55423SMike Smith *flag &= ~tag[i].value; 264615e32d5dSMike Smith } 264715e32d5dSMike Smith } 264815e32d5dSMike Smith cp = ep; 264915e32d5dSMike Smith } 265015e32d5dSMike Smith } 265115e32d5dSMike Smith 265215e32d5dSMike Smith static void 26532a4ac806SMike Smith acpi_set_debugging(void *junk) 265415e32d5dSMike Smith { 26557e639165SNate Lawson char *layer, *level; 265615e32d5dSMike Smith 26571d7b121cSNate Lawson if (cold) { 26580ae55423SMike Smith AcpiDbgLayer = 0; 26590ae55423SMike Smith AcpiDbgLevel = 0; 26601d7b121cSNate Lawson } 26611d7b121cSNate Lawson 26627e639165SNate Lawson layer = getenv("debug.acpi.layer"); 26637e639165SNate Lawson level = getenv("debug.acpi.level"); 26647e639165SNate Lawson if (layer == NULL && level == NULL) 26657e639165SNate Lawson return; 26660ae55423SMike Smith 26677e639165SNate Lawson printf("ACPI set debug"); 26687e639165SNate Lawson if (layer != NULL) { 26697e639165SNate Lawson if (strcmp("NONE", layer) != 0) 26707e639165SNate Lawson printf(" layer '%s'", layer); 26717e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 26727e639165SNate Lawson freeenv(layer); 26731d7b121cSNate Lawson } 26747e639165SNate Lawson if (level != NULL) { 26757e639165SNate Lawson if (strcmp("NONE", level) != 0) 26767e639165SNate Lawson printf(" level '%s'", level); 26777e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 26787e639165SNate Lawson freeenv(level); 26797e639165SNate Lawson } 26807e639165SNate Lawson printf("\n"); 268115e32d5dSMike Smith } 2682be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2683be2b1797SNate Lawson NULL); 26841d7b121cSNate Lawson 26851d7b121cSNate Lawson static int 26861d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 26871d7b121cSNate Lawson { 268854f6bca0SNate Lawson int error, *dbg; 26891d7b121cSNate Lawson struct debugtag *tag; 269054f6bca0SNate Lawson struct sbuf sb; 26911d7b121cSNate Lawson 269254f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 269354f6bca0SNate Lawson return (ENOMEM); 26941d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 26951d7b121cSNate Lawson tag = &dbg_layer[0]; 26961d7b121cSNate Lawson dbg = &AcpiDbgLayer; 26971d7b121cSNate Lawson } else { 26981d7b121cSNate Lawson tag = &dbg_level[0]; 26991d7b121cSNate Lawson dbg = &AcpiDbgLevel; 27001d7b121cSNate Lawson } 27011d7b121cSNate Lawson 27021d7b121cSNate Lawson /* Get old values if this is a get request. */ 27031d7b121cSNate Lawson if (*dbg == 0) { 270454f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 27051d7b121cSNate Lawson } else if (req->newptr == NULL) { 27061d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 270754f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 270854f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 27091d7b121cSNate Lawson } 27101d7b121cSNate Lawson } 271154f6bca0SNate Lawson sbuf_trim(&sb); 271254f6bca0SNate Lawson sbuf_finish(&sb); 27131d7b121cSNate Lawson 27147e639165SNate Lawson /* Copy out the old values to the user. */ 27157e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 271654f6bca0SNate Lawson sbuf_delete(&sb); 27171d7b121cSNate Lawson 27181d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 27191d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 27201d7b121cSNate Lawson *dbg = 0; 27211d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 27221d7b121cSNate Lawson acpi_set_debugging(NULL); 27231d7b121cSNate Lawson } 27241d7b121cSNate Lawson 27251d7b121cSNate Lawson return (error); 27261d7b121cSNate Lawson } 27271d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 27281d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 27291d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 27301d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 273115e32d5dSMike Smith #endif 2732f9390180SMitsuru IWASAKI 2733f9390180SMitsuru IWASAKI static int 2734f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2735f9390180SMitsuru IWASAKI { 2736f9390180SMitsuru IWASAKI int state, acpi_state; 2737f9390180SMitsuru IWASAKI int error; 2738f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2739f9390180SMitsuru IWASAKI va_list ap; 2740f9390180SMitsuru IWASAKI 2741f9390180SMitsuru IWASAKI error = 0; 2742f9390180SMitsuru IWASAKI switch (cmd) { 2743f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2744f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2745f9390180SMitsuru IWASAKI if (sc == NULL) { 2746f9390180SMitsuru IWASAKI error = EINVAL; 2747f9390180SMitsuru IWASAKI goto out; 2748f9390180SMitsuru IWASAKI } 2749f9390180SMitsuru IWASAKI 2750f9390180SMitsuru IWASAKI va_start(ap, arg); 2751f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2752f9390180SMitsuru IWASAKI va_end(ap); 2753f9390180SMitsuru IWASAKI 2754f9390180SMitsuru IWASAKI switch (state) { 2755f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2756f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2757f9390180SMitsuru IWASAKI break; 2758f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2759f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2760f9390180SMitsuru IWASAKI break; 2761f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2762f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2763f9390180SMitsuru IWASAKI break; 2764f9390180SMitsuru IWASAKI default: 2765f9390180SMitsuru IWASAKI error = EINVAL; 2766f9390180SMitsuru IWASAKI goto out; 2767f9390180SMitsuru IWASAKI } 2768f9390180SMitsuru IWASAKI 2769f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2770f9390180SMitsuru IWASAKI break; 2771f9390180SMitsuru IWASAKI default: 2772f9390180SMitsuru IWASAKI error = EINVAL; 2773f9390180SMitsuru IWASAKI goto out; 2774f9390180SMitsuru IWASAKI } 2775f9390180SMitsuru IWASAKI 2776f9390180SMitsuru IWASAKI out: 2777f9390180SMitsuru IWASAKI return (error); 2778f9390180SMitsuru IWASAKI } 2779f9390180SMitsuru IWASAKI 2780f9390180SMitsuru IWASAKI static void 2781f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2782f9390180SMitsuru IWASAKI { 2783be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 27846c407052SMitsuru IWASAKI return; 2785f9390180SMitsuru IWASAKI 2786f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2787f9390180SMitsuru IWASAKI } 2788f9390180SMitsuru IWASAKI 2789f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2790