115e32d5dSMike Smith /*- 215e32d5dSMike Smith * Copyright (c) 2000 Takanori Watanabe <takawata@jp.freebsd.org> 315e32d5dSMike Smith * Copyright (c) 2000 Mitsuru IWASAKI <iwasaki@jp.freebsd.org> 4cb9b0d80SMike Smith * Copyright (c) 2000, 2001 Michael Smith 515e32d5dSMike Smith * Copyright (c) 2000 BSDi 615e32d5dSMike Smith * All rights reserved. 715e32d5dSMike Smith * 815e32d5dSMike Smith * Redistribution and use in source and binary forms, with or without 915e32d5dSMike Smith * modification, are permitted provided that the following conditions 1015e32d5dSMike Smith * are met: 1115e32d5dSMike Smith * 1. Redistributions of source code must retain the above copyright 1215e32d5dSMike Smith * notice, this list of conditions and the following disclaimer. 1315e32d5dSMike Smith * 2. Redistributions in binary form must reproduce the above copyright 1415e32d5dSMike Smith * notice, this list of conditions and the following disclaimer in the 1515e32d5dSMike Smith * documentation and/or other materials provided with the distribution. 1615e32d5dSMike Smith * 1715e32d5dSMike Smith * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1815e32d5dSMike Smith * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1915e32d5dSMike Smith * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 2015e32d5dSMike Smith * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 2115e32d5dSMike Smith * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2215e32d5dSMike Smith * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2315e32d5dSMike Smith * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2415e32d5dSMike Smith * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2515e32d5dSMike Smith * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2615e32d5dSMike Smith * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2715e32d5dSMike Smith * SUCH DAMAGE. 2815e32d5dSMike Smith * 2915e32d5dSMike Smith * $FreeBSD$ 3015e32d5dSMike Smith */ 3115e32d5dSMike Smith 3215e32d5dSMike Smith #include "opt_acpi.h" 3315e32d5dSMike Smith #include <sys/param.h> 3415e32d5dSMike Smith #include <sys/kernel.h> 35fb919e4dSMark Murray #include <sys/proc.h> 36a89fcf28STakanori Watanabe #include <sys/fcntl.h> 3715e32d5dSMike Smith #include <sys/malloc.h> 38fe12f24bSPoul-Henning Kamp #include <sys/module.h> 3915e32d5dSMike Smith #include <sys/bus.h> 4015e32d5dSMike Smith #include <sys/conf.h> 4115e32d5dSMike Smith #include <sys/ioccom.h> 4215e32d5dSMike Smith #include <sys/reboot.h> 4315e32d5dSMike Smith #include <sys/sysctl.h> 4415e32d5dSMike Smith #include <sys/ctype.h> 451611ea87SMitsuru IWASAKI #include <sys/linker.h> 46f9390180SMitsuru IWASAKI #include <sys/power.h> 4754f6bca0SNate Lawson #include <sys/sbuf.h> 48eeb3a05fSNate Lawson #include <sys/smp.h> 4915e32d5dSMike Smith 5015e32d5dSMike Smith #include <machine/clock.h> 5115e32d5dSMike Smith #include <machine/resource.h> 52dc750869SNate Lawson #include <machine/bus.h> 53dc750869SNate Lawson #include <sys/rman.h> 54bc0f2195SMike Smith #include <isa/isavar.h> 55c796e27bSNate Lawson #include <isa/pnpvar.h> 56bc0f2195SMike Smith 5715e32d5dSMike Smith #include "acpi.h" 5815e32d5dSMike Smith #include <dev/acpica/acpivar.h> 5915e32d5dSMike Smith #include <dev/acpica/acpiio.h> 609b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h> 6115e32d5dSMike Smith 6215e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 6315e32d5dSMike Smith 64be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 65cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 66b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 670ae55423SMike Smith 6815e32d5dSMike Smith static d_open_t acpiopen; 6915e32d5dSMike Smith static d_close_t acpiclose; 7015e32d5dSMike Smith static d_ioctl_t acpiioctl; 7115e32d5dSMike Smith 7215e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 73dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 74dc08ffecSPoul-Henning Kamp .d_flags = D_NEEDGIANT, 757ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 767ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 777ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 787ac40f5fSPoul-Henning Kamp .d_name = "acpi", 7915e32d5dSMike Smith }; 8015e32d5dSMike Smith 81fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 82cb9b0d80SMike Smith struct mtx acpi_mutex; 83fc0ea94aSJohn Baldwin #endif 84cb9b0d80SMike Smith 8591233413SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 8691233413SNate Lawson struct rman acpi_rman_io, acpi_rman_mem; 8791233413SNate Lawson 883184cf5aSNate Lawson struct acpi_quirks { 893184cf5aSNate Lawson char *OemId; 903184cf5aSNate Lawson uint32_t OemRevision; 913184cf5aSNate Lawson char *value; 923184cf5aSNate Lawson }; 933184cf5aSNate Lawson 943184cf5aSNate Lawson #define ACPI_OEM_REV_ANY 0 953184cf5aSNate Lawson 963184cf5aSNate Lawson static struct acpi_quirks acpi_quirks_table[] = { 973184cf5aSNate Lawson #ifdef notyet 983184cf5aSNate Lawson /* Bad PCI routing table. Used on some SuperMicro boards. */ 993184cf5aSNate Lawson { "PTLTD ", 0x06040000, "pci_link" }, 1003184cf5aSNate Lawson #endif 1013184cf5aSNate Lawson 1023184cf5aSNate Lawson { NULL, 0, NULL } 1033184cf5aSNate Lawson }; 1043184cf5aSNate Lawson 1056d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 10615e32d5dSMike Smith static void acpi_identify(driver_t *driver, device_t parent); 10715e32d5dSMike Smith static int acpi_probe(device_t dev); 10815e32d5dSMike Smith static int acpi_attach(device_t dev); 109169b539aSNate Lawson static int acpi_shutdown(device_t dev); 1103184cf5aSNate Lawson static void acpi_quirks_set(void); 111be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 112be2b1797SNate Lawson int unit); 11315e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 114be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 115be2b1797SNate Lawson uintptr_t *result); 116be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 117be2b1797SNate Lawson uintptr_t value); 11891233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 119be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 120be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 121be2b1797SNate Lawson u_long count, u_int flags); 122be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 123be2b1797SNate Lawson int rid, struct resource *r); 1241e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1251e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 126ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 127ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 128ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 129ce619b2eSNate Lawson ACPI_BUFFER *ret); 130ce619b2eSNate Lawson static ACPI_STATUS acpi_device_walk_ns(device_t bus, device_t dev, 131ce619b2eSNate Lawson ACPI_OBJECT_TYPE type, UINT32 max_depth, 132ce619b2eSNate Lawson ACPI_WALK_CALLBACK user_fn, void *context, void **ret); 133be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 134be2b1797SNate Lawson struct isa_pnp_id *ids); 13515e32d5dSMike Smith static void acpi_probe_children(device_t bus); 136b82ca9a9SNate Lawson static int acpi_probe_order(ACPI_HANDLE handle, int *order); 137be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 138be2b1797SNate Lawson void *context, void **status); 139ce619b2eSNate Lawson static BOOLEAN acpi_MatchHid(ACPI_HANDLE h, const char *hid); 14015e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 14113d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 142e8b4d56eSNate Lawson static int acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw); 143e8b4d56eSNate Lawson static ACPI_STATUS acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, 144e8b4d56eSNate Lawson void **status); 145e8b4d56eSNate Lawson static int acpi_wake_limit_walk(int sstate); 14688a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 14788a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 14815e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 14915e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 150d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1511d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 152f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 153879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 154879d6c23STakanori Watanabe char *buf, size_t buflen); 155879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 156879d6c23STakanori Watanabe char *buf, size_t buflen); 157879d6c23STakanori Watanabe 15815e32d5dSMike Smith static device_method_t acpi_methods[] = { 15915e32d5dSMike Smith /* Device interface */ 16015e32d5dSMike Smith DEVMETHOD(device_identify, acpi_identify), 16115e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 16215e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 163169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 16456a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 16515e32d5dSMike Smith DEVMETHOD(device_suspend, bus_generic_suspend), 16615e32d5dSMike Smith DEVMETHOD(device_resume, bus_generic_resume), 16715e32d5dSMike Smith 16815e32d5dSMike Smith /* Bus interface */ 16915e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 17015e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 17115e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 17215e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 17391233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 17491233413SNate Lawson DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 17591233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 17615e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 17715e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 178879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 179879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 18015e32d5dSMike Smith DEVMETHOD(bus_driver_added, bus_generic_driver_added), 18115e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 18215e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 18315e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 18415e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 18515e32d5dSMike Smith 186ce619b2eSNate Lawson /* ACPI bus */ 187ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 188ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 189ce619b2eSNate Lawson DEVMETHOD(acpi_walk_namespace, acpi_device_walk_ns), 190ce619b2eSNate Lawson 191bc0f2195SMike Smith /* ISA emulation */ 192bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 193bc0f2195SMike Smith 19415e32d5dSMike Smith {0, 0} 19515e32d5dSMike Smith }; 19615e32d5dSMike Smith 19715e32d5dSMike Smith static driver_t acpi_driver = { 19815e32d5dSMike Smith "acpi", 19915e32d5dSMike Smith acpi_methods, 20015e32d5dSMike Smith sizeof(struct acpi_softc), 20115e32d5dSMike Smith }; 20215e32d5dSMike Smith 2033273b005SMike Smith static devclass_t acpi_devclass; 2046d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 205a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 20615e32d5dSMike Smith 207865b8d0bSNate Lawson static const char* sleep_state_names[] = { 208865b8d0bSNate Lawson "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 209865b8d0bSNate Lawson 2101d7b121cSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RW, NULL, "ACPI debugging"); 2111d7b121cSNate Lawson static char acpi_ca_version[12]; 2121d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2131d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 21415e32d5dSMike Smith 21515e32d5dSMike Smith /* 216413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 217c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 218c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 219c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 220c9b8d77dSNate Lawson * some IBMs use such code. 221413081d7SNate Lawson */ 222c9b8d77dSNate Lawson static int acpi_serialize_methods; 223413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 224413081d7SNate Lawson 225c9b8d77dSNate Lawson /* 2266d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2276d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 228be2b1797SNate Lawson * It also cannot be unloaded, since the entire system bus heirarchy hangs 229be2b1797SNate Lawson * off it. 2306d63101aSMike Smith */ 2316d63101aSMike Smith static int 2326d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2336d63101aSMike Smith { 2346d63101aSMike Smith switch (event) { 2356d63101aSMike Smith case MOD_LOAD: 23687b45ed5SMitsuru IWASAKI if (!cold) { 23787b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2386d63101aSMike Smith return (EPERM); 23987b45ed5SMitsuru IWASAKI } 2406d63101aSMike Smith break; 2416d63101aSMike Smith case MOD_UNLOAD: 242f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2436d63101aSMike Smith return (EBUSY); 244f9390180SMitsuru IWASAKI break; 2456d63101aSMike Smith default: 2466d63101aSMike Smith break; 2476d63101aSMike Smith } 2486d63101aSMike Smith return (0); 2496d63101aSMike Smith } 2506d63101aSMike Smith 2516d63101aSMike Smith /* 252bbc2815cSJohn Baldwin * Perform early initialization. 25315e32d5dSMike Smith */ 254bbc2815cSJohn Baldwin ACPI_STATUS 255bbc2815cSJohn Baldwin acpi_Startup(void) 25615e32d5dSMike Smith { 257d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 258d786139cSMaxime Henrion char *debugpoint; 25915e32d5dSMike Smith #endif 260bbc2815cSJohn Baldwin static int error, started = 0; 26115e32d5dSMike Smith 2621b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2631b8c233dSPeter Pentchev 264bbc2815cSJohn Baldwin if (started) 265bbc2815cSJohn Baldwin return_VALUE (error); 266bbc2815cSJohn Baldwin started = 1; 26715e32d5dSMike Smith 268fc0ea94aSJohn Baldwin #if __FreeBSD_version >= 500000 269be2b1797SNate Lawson /* Initialise the ACPI mutex */ 2706008862bSJohn Baldwin mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 271fc0ea94aSJohn Baldwin #endif 272cb9b0d80SMike Smith 273413081d7SNate Lawson /* 274413081d7SNate Lawson * Set the globals from our tunables. This is needed because ACPI-CA 275f3fc4f8bSNate Lawson * uses UINT8 for some values and we have no tunable_byte. 276413081d7SNate Lawson */ 277f3fc4f8bSNate Lawson AcpiGbl_AllMethodsSerialized = (UINT8)acpi_serialize_methods; 278413081d7SNate Lawson 279be2b1797SNate Lawson /* Start up the ACPI CA subsystem. */ 280d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 281d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 282d786139cSMaxime Henrion if (debugpoint) { 283d786139cSMaxime Henrion if (!strcmp(debugpoint, "init")) 28415e32d5dSMike Smith acpi_EnterDebugger(); 285d786139cSMaxime Henrion freeenv(debugpoint); 286d786139cSMaxime Henrion } 28715e32d5dSMike Smith #endif 288b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiInitializeSubsystem())) { 289bfae45aaSMike Smith printf("ACPI: initialisation failed: %s\n", AcpiFormatException(error)); 290bbc2815cSJohn Baldwin return_VALUE (error); 29115e32d5dSMike Smith } 292d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 293d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 294d786139cSMaxime Henrion if (debugpoint) { 295d786139cSMaxime Henrion if (!strcmp(debugpoint, "tables")) 29615e32d5dSMike Smith acpi_EnterDebugger(); 297d786139cSMaxime Henrion freeenv(debugpoint); 298d786139cSMaxime Henrion } 29915e32d5dSMike Smith #endif 3001611ea87SMitsuru IWASAKI 301b53f2771SMike Smith if (ACPI_FAILURE(error = AcpiLoadTables())) { 302bfae45aaSMike Smith printf("ACPI: table load failed: %s\n", AcpiFormatException(error)); 303bbc2815cSJohn Baldwin return_VALUE(error); 30415e32d5dSMike Smith } 3053184cf5aSNate Lawson 3063184cf5aSNate Lawson /* Set up any quirks we have for this XSDT. */ 3073184cf5aSNate Lawson acpi_quirks_set(); 3084e376d58SNate Lawson if (acpi_disabled("acpi")) 3094e376d58SNate Lawson return_VALUE (AE_ERROR); 3103184cf5aSNate Lawson 311bbc2815cSJohn Baldwin return_VALUE (AE_OK); 312bbc2815cSJohn Baldwin } 313bbc2815cSJohn Baldwin 314bbc2815cSJohn Baldwin /* 315bbc2815cSJohn Baldwin * Detect ACPI, perform early initialisation 316bbc2815cSJohn Baldwin */ 317bbc2815cSJohn Baldwin static void 318bbc2815cSJohn Baldwin acpi_identify(driver_t *driver, device_t parent) 319bbc2815cSJohn Baldwin { 320bbc2815cSJohn Baldwin device_t child; 321bbc2815cSJohn Baldwin 322bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 323bbc2815cSJohn Baldwin 324bbc2815cSJohn Baldwin if (!cold) 325bbc2815cSJohn Baldwin return_VOID; 326bbc2815cSJohn Baldwin 327bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 328bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 329bbc2815cSJohn Baldwin return_VOID; 330bbc2815cSJohn Baldwin 331bbc2815cSJohn Baldwin /* Make sure we're not being doubly invoked. */ 332bbc2815cSJohn Baldwin if (device_find_child(parent, "acpi", 0) != NULL) 333bbc2815cSJohn Baldwin return_VOID; 334bbc2815cSJohn Baldwin 335bbc2815cSJohn Baldwin /* Initialize ACPI-CA. */ 336bbc2815cSJohn Baldwin if (ACPI_FAILURE(acpi_Startup())) 337bbc2815cSJohn Baldwin return_VOID; 33815e32d5dSMike Smith 3394e376d58SNate Lawson snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%#x", ACPI_CA_VERSION); 3404e376d58SNate Lawson 341be2b1797SNate Lawson /* Attach the actual ACPI device. */ 34215e32d5dSMike Smith if ((child = BUS_ADD_CHILD(parent, 0, "acpi", 0)) == NULL) { 34315e32d5dSMike Smith device_printf(parent, "ACPI: could not attach\n"); 3440ae55423SMike Smith return_VOID; 34515e32d5dSMike Smith } 34615e32d5dSMike Smith } 34715e32d5dSMike Smith 34815e32d5dSMike Smith /* 34915e32d5dSMike Smith * Fetch some descriptive data from ACPI to put in our attach message 35015e32d5dSMike Smith */ 35115e32d5dSMike Smith static int 35215e32d5dSMike Smith acpi_probe(device_t dev) 35315e32d5dSMike Smith { 35415e32d5dSMike Smith ACPI_TABLE_HEADER th; 35515e32d5dSMike Smith char buf[20]; 35615e32d5dSMike Smith int error; 3572ccd1cacSNate Lawson struct sbuf sb; 3582ccd1cacSNate Lawson ACPI_STATUS status; 359fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 36015e32d5dSMike Smith 361b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 362f9390180SMitsuru IWASAKI 363f9390180SMitsuru IWASAKI if (power_pm_get_type() != POWER_PM_TYPE_NONE && 364f9390180SMitsuru IWASAKI power_pm_get_type() != POWER_PM_TYPE_ACPI) { 365be2b1797SNate Lawson 366f9390180SMitsuru IWASAKI device_printf(dev, "Other PM system enabled.\n"); 367f9390180SMitsuru IWASAKI return_VALUE(ENXIO); 368f9390180SMitsuru IWASAKI } 369f9390180SMitsuru IWASAKI 370cb9b0d80SMike Smith ACPI_LOCK; 3710ae55423SMike Smith 372b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiGetTableHeader(ACPI_TABLE_XSDT, 1, &th))) { 373be2b1797SNate Lawson device_printf(dev, "couldn't get XSDT header: %s\n", 374be2b1797SNate Lawson AcpiFormatException(status)); 375cb9b0d80SMike Smith error = ENXIO; 376cb9b0d80SMike Smith } else { 3772ccd1cacSNate Lawson sbuf_new(&sb, buf, sizeof(buf), SBUF_FIXEDLEN); 3782ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemId, 6); 3792ccd1cacSNate Lawson sbuf_trim(&sb); 3802ccd1cacSNate Lawson sbuf_putc(&sb, ' '); 3812ccd1cacSNate Lawson sbuf_bcat(&sb, th.OemTableId, 8); 3822ccd1cacSNate Lawson sbuf_trim(&sb); 3832ccd1cacSNate Lawson sbuf_finish(&sb); 3842ccd1cacSNate Lawson device_set_desc_copy(dev, sbuf_data(&sb)); 3852ccd1cacSNate Lawson sbuf_delete(&sb); 386cb9b0d80SMike Smith error = 0; 387cb9b0d80SMike Smith } 388cb9b0d80SMike Smith ACPI_UNLOCK; 389cb9b0d80SMike Smith return_VALUE(error); 39015e32d5dSMike Smith } 39115e32d5dSMike Smith 39215e32d5dSMike Smith static int 39315e32d5dSMike Smith acpi_attach(device_t dev) 39415e32d5dSMike Smith { 39515e32d5dSMike Smith struct acpi_softc *sc; 396cb9b0d80SMike Smith ACPI_STATUS status; 397ea27c63eSNate Lawson int error, state; 39832d18aa5SMike Smith UINT32 flags; 399ea27c63eSNate Lawson UINT8 TypeA, TypeB; 400498d464fSMitsuru IWASAKI char *env; 401d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 402d786139cSMaxime Henrion char *debugpoint; 40315e32d5dSMike Smith #endif 404fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 40515e32d5dSMike Smith 406b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 407cb9b0d80SMike Smith ACPI_LOCK; 40815e32d5dSMike Smith sc = device_get_softc(dev); 40915e32d5dSMike Smith bzero(sc, sizeof(*sc)); 41015e32d5dSMike Smith sc->acpi_dev = dev; 41115e32d5dSMike Smith 41291233413SNate Lawson /* Initialize resource manager. */ 41391233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 41491233413SNate Lawson acpi_rman_io.rm_start = 0; 41591233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 41691233413SNate Lawson acpi_rman_io.rm_descr = "I/O ports"; 41791233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 41891233413SNate Lawson panic("acpi rman_init IO ports failed"); 41991233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 42091233413SNate Lawson acpi_rman_mem.rm_start = 0; 42191233413SNate Lawson acpi_rman_mem.rm_end = ~0ul; 42291233413SNate Lawson acpi_rman_mem.rm_descr = "I/O memory addresses"; 42391233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 42491233413SNate Lawson panic("acpi rman_init memory failed"); 42591233413SNate Lawson 426d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 427d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 428d786139cSMaxime Henrion if (debugpoint) { 429d786139cSMaxime Henrion if (!strcmp(debugpoint, "spaces")) 43015e32d5dSMike Smith acpi_EnterDebugger(); 431d786139cSMaxime Henrion freeenv(debugpoint); 432d786139cSMaxime Henrion } 43315e32d5dSMike Smith #endif 43415e32d5dSMike Smith 435be2b1797SNate Lawson /* Install the default address space handlers. */ 436cb9b0d80SMike Smith error = ENXIO; 437be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 438be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 439be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 440be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 441be2b1797SNate Lawson AcpiFormatException(status)); 442cb9b0d80SMike Smith goto out; 44315e32d5dSMike Smith } 444be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 445be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 446be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 447be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 448be2b1797SNate Lawson AcpiFormatException(status)); 449cb9b0d80SMike Smith goto out; 45015e32d5dSMike Smith } 451be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 452be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 453be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 454be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 455be2b1797SNate Lawson AcpiFormatException(status)); 456cb9b0d80SMike Smith goto out; 45715e32d5dSMike Smith } 45815e32d5dSMike Smith 45915e32d5dSMike Smith /* 46015e32d5dSMike Smith * Bring ACPI fully online. 46115e32d5dSMike Smith * 462be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 463be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 464be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 465be2b1797SNate Lawson * object init pass. 46615e32d5dSMike Smith * 46732d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 46832d18aa5SMike Smith * 469be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 470be2b1797SNate Lawson * all our child devices, but on many systems it works here. 47115e32d5dSMike Smith */ 472d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 473d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 474d786139cSMaxime Henrion if (debugpoint) { 475d786139cSMaxime Henrion if (!strcmp(debugpoint, "enable")) 47615e32d5dSMike Smith acpi_EnterDebugger(); 477d786139cSMaxime Henrion freeenv(debugpoint); 478d786139cSMaxime Henrion } 47915e32d5dSMike Smith #endif 48032d18aa5SMike Smith flags = 0; 481d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 48232d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 483b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 484be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 485be2b1797SNate Lawson AcpiFormatException(status)); 486cb9b0d80SMike Smith goto out; 48715e32d5dSMike Smith } 48815e32d5dSMike Smith 489f8335e3aSNate Lawson /* 490f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 491f8335e3aSNate Lawson * the namespace has been evaluated. 492f8335e3aSNate Lawson */ 493f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 494f8335e3aSNate Lawson 495b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 496be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 497be2b1797SNate Lawson AcpiFormatException(status)); 498b69ed3f4SMitsuru IWASAKI goto out; 499b69ed3f4SMitsuru IWASAKI } 500b69ed3f4SMitsuru IWASAKI 50115e32d5dSMike Smith /* 5021d073b1dSJohn Baldwin * Setup our sysctl tree. 5031d073b1dSJohn Baldwin * 5041d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 5051d073b1dSJohn Baldwin */ 5061d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 5071d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 5081d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5091d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5101d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 511d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 512d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 513d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5141d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5151d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5161d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5171d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5181d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5191d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5201d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5211d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 522f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 523f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 524f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 525f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 526f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 527f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5282d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 529ff01efb5SMitsuru IWASAKI OID_AUTO, "sleep_delay", CTLFLAG_RD | CTLFLAG_RW, 530ff01efb5SMitsuru IWASAKI &sc->acpi_sleep_delay, 0, "sleep delay"); 531ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5321611ea87SMitsuru IWASAKI OID_AUTO, "s4bios", CTLFLAG_RD | CTLFLAG_RW, 5331611ea87SMitsuru IWASAKI &sc->acpi_s4bios, 0, "S4BIOS mode"); 5341611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5352d644607SMitsuru IWASAKI OID_AUTO, "verbose", CTLFLAG_RD | CTLFLAG_RW, 5362d644607SMitsuru IWASAKI &sc->acpi_verbose, 0, "verbose mode"); 537bf0c18ecSNate Lawson 538bf0c18ecSNate Lawson /* 5392b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 540bf0c18ecSNate Lawson * stabilize. 541bf0c18ecSNate Lawson */ 5422b9609abSNate Lawson sc->acpi_sleep_delay = 1; 5432d644607SMitsuru IWASAKI if (bootverbose) 5442d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 545498d464fSMitsuru IWASAKI if ((env = getenv("hw.acpi.verbose")) && strcmp(env, "0")) { 546498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 547498d464fSMitsuru IWASAKI freeenv(env); 548498d464fSMitsuru IWASAKI } 5491d073b1dSJohn Baldwin 5502d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5512d610c46SNate Lawson if (AcpiGbl_FACS->S4Bios_f != 0) 5522d610c46SNate Lawson sc->acpi_s4bios = 1; 5532d610c46SNate Lawson 5541d073b1dSJohn Baldwin /* 555ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 556ea27c63eSNate Lawson * to NONE by default to avoid surprising users. 55715e32d5dSMike Smith */ 558ea27c63eSNate Lawson sc->acpi_power_button_sx = ACPI_STATE_S5; 559ea27c63eSNate Lawson sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 560f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 561f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 56215e32d5dSMike Smith 563ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 564ea27c63eSNate Lawson sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 565ea27c63eSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_STATE_S5; state++) 566ea27c63eSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 567ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 568ea27c63eSNate Lawson break; 569ea27c63eSNate Lawson } 570ea27c63eSNate Lawson 57113d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 57215e32d5dSMike Smith 57315e32d5dSMike Smith /* 57415e32d5dSMike Smith * Scan the namespace and attach/initialise children. 57515e32d5dSMike Smith */ 576d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 577d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 578d786139cSMaxime Henrion if (debugpoint) { 579d786139cSMaxime Henrion if (!strcmp(debugpoint, "probe")) 58015e32d5dSMike Smith acpi_EnterDebugger(); 581d786139cSMaxime Henrion freeenv(debugpoint); 582d786139cSMaxime Henrion } 58315e32d5dSMike Smith #endif 58415e32d5dSMike Smith 58559e472e9SNate Lawson /* Register our shutdown handler. */ 586be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 587be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 58815e32d5dSMike Smith 58915e32d5dSMike Smith /* 59015e32d5dSMike Smith * Register our acpi event handlers. 59115e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 59215e32d5dSMike Smith */ 593be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 594be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 595be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 596be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 59715e32d5dSMike Smith 598be2b1797SNate Lawson /* Flag our initial states. */ 59915e32d5dSMike Smith sc->acpi_enabled = 1; 60015e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 601ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 60215e32d5dSMike Smith 603be2b1797SNate Lawson /* Create the control device */ 604a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 6052a4689e8SRobert Watson "acpi"); 60615e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 60715e32d5dSMike Smith 608d62ab2f4SMitsuru IWASAKI #ifdef ACPI_DEBUGGER 609d786139cSMaxime Henrion debugpoint = getenv("debug.acpi.debugger"); 610d786139cSMaxime Henrion if (debugpoint) { 611be2b1797SNate Lawson if (strcmp(debugpoint, "running") == 0) 61215e32d5dSMike Smith acpi_EnterDebugger(); 613d786139cSMaxime Henrion freeenv(debugpoint); 614d786139cSMaxime Henrion } 61515e32d5dSMike Smith #endif 616f86214b6SMitsuru IWASAKI 61791da7c40SMitsuru IWASAKI #ifdef ACPI_USE_THREADS 618be2b1797SNate Lawson if ((error = acpi_task_thread_init())) 619c573e654SMitsuru IWASAKI goto out; 620c573e654SMitsuru IWASAKI #endif 621c573e654SMitsuru IWASAKI 622be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 623f86214b6SMitsuru IWASAKI goto out; 624f86214b6SMitsuru IWASAKI 625f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 626f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 627f9390180SMitsuru IWASAKI 628eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 629eeb6dba2SJohn Baldwin acpi_probe_children(dev); 630eeb6dba2SJohn Baldwin 631cb9b0d80SMike Smith error = 0; 632cb9b0d80SMike Smith 633cb9b0d80SMike Smith out: 634cb9b0d80SMike Smith ACPI_UNLOCK; 635cb9b0d80SMike Smith return_VALUE (error); 63615e32d5dSMike Smith } 63715e32d5dSMike Smith 638169b539aSNate Lawson static int 639169b539aSNate Lawson acpi_shutdown(device_t dev) 640169b539aSNate Lawson { 641169b539aSNate Lawson 6420e414868SNate Lawson /* Allow children to shutdown first. */ 6430e414868SNate Lawson bus_generic_shutdown(dev); 6440e414868SNate Lawson 645169b539aSNate Lawson /* Disable all wake GPEs not appropriate for reboot/poweroff. */ 646169b539aSNate Lawson acpi_wake_limit_walk(ACPI_STATE_S5); 647169b539aSNate Lawson return (0); 648169b539aSNate Lawson } 649169b539aSNate Lawson 6503184cf5aSNate Lawson static void 6513184cf5aSNate Lawson acpi_quirks_set() 6523184cf5aSNate Lawson { 6533184cf5aSNate Lawson XSDT_DESCRIPTOR *xsdt; 6543184cf5aSNate Lawson struct acpi_quirks *quirk; 6553184cf5aSNate Lawson char *env, *tmp; 6563184cf5aSNate Lawson int len; 6573184cf5aSNate Lawson 6584e376d58SNate Lawson /* 6594e376d58SNate Lawson * If the user loaded a custom table or disabled "quirks", leave 6604e376d58SNate Lawson * the settings alone. 6614e376d58SNate Lawson */ 6623184cf5aSNate Lawson len = 0; 6634e376d58SNate Lawson if ((env = getenv("acpi_dsdt_load")) != NULL) { 6644e376d58SNate Lawson /* XXX No strcasecmp but this is good enough. */ 6654e376d58SNate Lawson if (*env == 'Y' || *env == 'y') 6664e376d58SNate Lawson goto out; 6674e376d58SNate Lawson freeenv(env); 6684e376d58SNate Lawson } 6693184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) != NULL) { 6704e376d58SNate Lawson if (strstr("quirks", env) != NULL) 6713184cf5aSNate Lawson goto out; 6723184cf5aSNate Lawson len = strlen(env); 6733184cf5aSNate Lawson } 6743184cf5aSNate Lawson 6753184cf5aSNate Lawson /* 6763184cf5aSNate Lawson * Search through our quirk table and concatenate the disabled 6773184cf5aSNate Lawson * values with whatever we find. 6783184cf5aSNate Lawson */ 6793184cf5aSNate Lawson xsdt = AcpiGbl_XSDT; 6803184cf5aSNate Lawson for (quirk = acpi_quirks_table; quirk->OemId; quirk++) { 6813184cf5aSNate Lawson if (!strncmp(xsdt->OemId, quirk->OemId, strlen(quirk->OemId)) && 6823184cf5aSNate Lawson (xsdt->OemRevision == quirk->OemRevision || 6833184cf5aSNate Lawson quirk->OemRevision == ACPI_OEM_REV_ANY)) { 6843184cf5aSNate Lawson len += strlen(quirk->value) + 2; 6853184cf5aSNate Lawson if ((tmp = malloc(len, M_TEMP, M_NOWAIT)) == NULL) 6863184cf5aSNate Lawson goto out; 6873184cf5aSNate Lawson sprintf(tmp, "%s %s", env ? env : "", quirk->value); 6883184cf5aSNate Lawson setenv("debug.acpi.disabled", tmp); 6893184cf5aSNate Lawson free(tmp, M_TEMP); 6903184cf5aSNate Lawson break; 6913184cf5aSNate Lawson } 6923184cf5aSNate Lawson } 6933184cf5aSNate Lawson 6943184cf5aSNate Lawson out: 6953184cf5aSNate Lawson if (env) 6963184cf5aSNate Lawson freeenv(env); 6973184cf5aSNate Lawson } 6983184cf5aSNate Lawson 69915e32d5dSMike Smith /* 70015e32d5dSMike Smith * Handle a new device being added 70115e32d5dSMike Smith */ 70215e32d5dSMike Smith static device_t 70315e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 70415e32d5dSMike Smith { 70515e32d5dSMike Smith struct acpi_device *ad; 70615e32d5dSMike Smith device_t child; 70715e32d5dSMike Smith 708be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 70915e32d5dSMike Smith return (NULL); 71015e32d5dSMike Smith 71115e32d5dSMike Smith resource_list_init(&ad->ad_rl); 71215e32d5dSMike Smith 71315e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 71415e32d5dSMike Smith if (child != NULL) 71515e32d5dSMike Smith device_set_ivars(child, ad); 71615e32d5dSMike Smith return (child); 71715e32d5dSMike Smith } 71815e32d5dSMike Smith 71915e32d5dSMike Smith static int 72015e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 72115e32d5dSMike Smith { 72215e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 72315e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 72415e32d5dSMike Smith int retval = 0; 72515e32d5dSMike Smith 72615e32d5dSMike Smith retval += bus_print_child_header(bus, child); 7279ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 7289ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 7299ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 7309ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 73115e32d5dSMike Smith retval += bus_print_child_footer(bus, child); 73215e32d5dSMike Smith 73315e32d5dSMike Smith return (retval); 73415e32d5dSMike Smith } 73515e32d5dSMike Smith 73663600cc3SNate Lawson /* Location hint for devctl(8) */ 73763600cc3SNate Lawson static int 738247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 739247648afSTakanori Watanabe size_t buflen) 740247648afSTakanori Watanabe { 741247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 742247648afSTakanori Watanabe 743247648afSTakanori Watanabe if (dinfo->ad_handle) 744247648afSTakanori Watanabe snprintf(buf, buflen, "path=%s", acpi_name(dinfo->ad_handle)); 745247648afSTakanori Watanabe else 746247648afSTakanori Watanabe snprintf(buf, buflen, "magic=unknown"); 747247648afSTakanori Watanabe return (0); 748247648afSTakanori Watanabe } 749247648afSTakanori Watanabe 75063600cc3SNate Lawson /* PnP information for devctl(8) */ 75163600cc3SNate Lawson static int 752247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 753247648afSTakanori Watanabe size_t buflen) 754247648afSTakanori Watanabe { 755247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 75663600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 75763600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 758247648afSTakanori Watanabe char *end; 759247648afSTakanori Watanabe int error; 760247648afSTakanori Watanabe 761247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 762247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 763247648afSTakanori Watanabe 764247648afSTakanori Watanabe if (error) 765247648afSTakanori Watanabe snprintf(buf, buflen, "Unknown"); 766247648afSTakanori Watanabe else 767247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 768247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 769247648afSTakanori Watanabe adinfo->HardwareId.Value : "UNKNOWN", 77063600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 77163600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 772247648afSTakanori Watanabe 773247648afSTakanori Watanabe if (adinfo) 774247648afSTakanori Watanabe AcpiOsFree(adinfo); 775247648afSTakanori Watanabe 776247648afSTakanori Watanabe return (0); 777247648afSTakanori Watanabe } 778247648afSTakanori Watanabe 779247648afSTakanori Watanabe /* 78015e32d5dSMike Smith * Handle per-device ivars 78115e32d5dSMike Smith */ 78215e32d5dSMike Smith static int 78315e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 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 792be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 79315e32d5dSMike Smith switch(index) { 79415e32d5dSMike Smith case ACPI_IVAR_HANDLE: 79515e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 79615e32d5dSMike Smith break; 79715e32d5dSMike Smith case ACPI_IVAR_MAGIC: 79815e32d5dSMike Smith *(int *)result = ad->ad_magic; 79915e32d5dSMike Smith break; 80015e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 80115e32d5dSMike Smith *(void **)result = ad->ad_private; 80215e32d5dSMike Smith break; 80332d18aa5SMike Smith case ISA_IVAR_VENDORID: 80432d18aa5SMike Smith case ISA_IVAR_SERIAL: 80532d18aa5SMike Smith case ISA_IVAR_COMPATID: 80632d18aa5SMike Smith *(int *)result = -1; 80732d18aa5SMike Smith break; 80832d18aa5SMike Smith case ISA_IVAR_LOGICALID: 80932d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 81032d18aa5SMike Smith break; 81115e32d5dSMike Smith default: 81215e32d5dSMike Smith return (ENOENT); 81315e32d5dSMike Smith } 814be2b1797SNate Lawson 81515e32d5dSMike Smith return (0); 81615e32d5dSMike Smith } 81715e32d5dSMike Smith 81815e32d5dSMike Smith static int 81915e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 82015e32d5dSMike Smith { 82115e32d5dSMike Smith struct acpi_device *ad; 82215e32d5dSMike Smith 82315e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 82415e32d5dSMike Smith printf("device has no ivars\n"); 82515e32d5dSMike Smith return (ENOENT); 82615e32d5dSMike Smith } 82715e32d5dSMike Smith 82815e32d5dSMike Smith switch(index) { 82915e32d5dSMike Smith case ACPI_IVAR_HANDLE: 83015e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 83115e32d5dSMike Smith break; 83215e32d5dSMike Smith case ACPI_IVAR_MAGIC: 83315e32d5dSMike Smith ad->ad_magic = (int)value; 83415e32d5dSMike Smith break; 83515e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 83615e32d5dSMike Smith ad->ad_private = (void *)value; 83715e32d5dSMike Smith break; 83815e32d5dSMike Smith default: 8392ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 84015e32d5dSMike Smith return (ENOENT); 84115e32d5dSMike Smith } 842be2b1797SNate Lawson 84315e32d5dSMike Smith return (0); 84415e32d5dSMike Smith } 84515e32d5dSMike Smith 84615e32d5dSMike Smith /* 84715e32d5dSMike Smith * Handle child resource allocation/removal 84815e32d5dSMike Smith */ 84991233413SNate Lawson static struct resource_list * 85091233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 85115e32d5dSMike Smith { 85291233413SNate Lawson struct acpi_device *ad; 85315e32d5dSMike Smith 85491233413SNate Lawson ad = device_get_ivars(child); 85591233413SNate Lawson return (&ad->ad_rl); 85615e32d5dSMike Smith } 85715e32d5dSMike Smith 85815e32d5dSMike Smith static struct resource * 85915e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 86015e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 86115e32d5dSMike Smith { 86295957f62SJohn Baldwin ACPI_RESOURCE ares; 86315e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 86415e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 86591233413SNate Lawson struct resource_list_entry *rle; 86691233413SNate Lawson struct resource *res; 86791233413SNate Lawson struct rman *rm; 86815e32d5dSMike Smith 86991233413SNate Lawson /* 87091233413SNate Lawson * If this is an allocation of the "default" range for a given RID, and 87191233413SNate Lawson * we know what the resources for this device are (i.e., they're on the 87291233413SNate Lawson * child's resource list), use those start/end values. 87391233413SNate Lawson */ 87491233413SNate Lawson if (start == 0UL && end == ~0UL) { 87591233413SNate Lawson rle = resource_list_find(rl, type, *rid); 87691233413SNate Lawson if (rle == NULL) 87791233413SNate Lawson return (NULL); 87891233413SNate Lawson start = rle->start; 87991233413SNate Lawson end = rle->end; 88091233413SNate Lawson count = rle->count; 88191233413SNate Lawson } 88291233413SNate Lawson 88391233413SNate Lawson /* If we don't manage this address, pass the request up to the parent. */ 88491233413SNate Lawson rle = acpi_sysres_find(type, start); 88591233413SNate Lawson if (rle == NULL) { 88695957f62SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 88795957f62SJohn Baldwin start, end, count, flags); 88895957f62SJohn Baldwin } else { 88991233413SNate Lawson 89091233413SNate Lawson /* We only handle memory and IO resources through rman. */ 89191233413SNate Lawson switch (type) { 89291233413SNate Lawson case SYS_RES_IOPORT: 89391233413SNate Lawson rm = &acpi_rman_io; 89491233413SNate Lawson break; 89591233413SNate Lawson case SYS_RES_MEMORY: 89691233413SNate Lawson rm = &acpi_rman_mem; 89791233413SNate Lawson break; 89891233413SNate Lawson default: 89991233413SNate Lawson panic("acpi_alloc_resource: invalid res type %d", type); 90091233413SNate Lawson } 90191233413SNate Lawson 90291233413SNate Lawson /* If we do know it, allocate it from the local pool. */ 90395957f62SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 90495957f62SJohn Baldwin child); 90591233413SNate Lawson if (res == NULL) 90691233413SNate Lawson return (NULL); 90791233413SNate Lawson 908a3236570SWarner Losh /* Copy the bus tag and handle from the pre-allocated resource. */ 90991233413SNate Lawson rman_set_bustag(res, rman_get_bustag(rle->res)); 910ed67d9deSWarner Losh rman_set_bushandle(res, rman_get_start(res)); 91191233413SNate Lawson 91291233413SNate Lawson /* If requested, activate the resource using the parent's method. */ 91395957f62SJohn Baldwin if (flags & RF_ACTIVE) 91491233413SNate Lawson if (bus_activate_resource(child, type, *rid, res) != 0) { 91591233413SNate Lawson rman_release_resource(res); 91691233413SNate Lawson return (NULL); 91791233413SNate Lawson } 91895957f62SJohn Baldwin } 91991233413SNate Lawson 92095957f62SJohn Baldwin if (res != NULL && device_get_parent(child) == bus) 92195957f62SJohn Baldwin switch (type) { 92295957f62SJohn Baldwin case SYS_RES_IRQ: 92395957f62SJohn Baldwin /* 92495957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 92595957f62SJohn Baldwin * configure the interrupt associated with a device when we 92695957f62SJohn Baldwin * parse the resources but have to defer it until a driver 92795957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 92895957f62SJohn Baldwin * 92995957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 93095957f62SJohn Baldwin */ 93195957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 93295957f62SJohn Baldwin acpi_config_intr(child, &ares); 93395957f62SJohn Baldwin break; 93495957f62SJohn Baldwin } 93591233413SNate Lawson return (res); 93615e32d5dSMike Smith } 93715e32d5dSMike Smith 93815e32d5dSMike Smith static int 93991233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 94091233413SNate Lawson struct resource *r) 94115e32d5dSMike Smith { 94291233413SNate Lawson int ret; 94315e32d5dSMike Smith 94491233413SNate Lawson /* 94591233413SNate Lawson * If we know about this address, deactivate it and release it to the 94691233413SNate Lawson * local pool. If we don't, pass this request up to the parent. 94791233413SNate Lawson */ 94891233413SNate Lawson if (acpi_sysres_find(type, rman_get_start(r)) == NULL) { 94991233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 95091233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 95191233413SNate Lawson if (ret != 0) 95291233413SNate Lawson return (ret); 95391233413SNate Lawson } 95491233413SNate Lawson ret = rman_release_resource(r); 95591233413SNate Lawson } else 95691233413SNate Lawson ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 95791233413SNate Lawson 95891233413SNate Lawson return (ret); 95915e32d5dSMike Smith } 96015e32d5dSMike Smith 961dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 962dc750869SNate Lawson struct resource * 963dc750869SNate Lawson acpi_bus_alloc_gas(device_t dev, int *rid, ACPI_GENERIC_ADDRESS *gas) 964dc750869SNate Lawson { 965dc750869SNate Lawson int type; 966dc750869SNate Lawson 967dc750869SNate Lawson if (gas == NULL || !ACPI_VALID_ADDRESS(gas->Address) || 968dc750869SNate Lawson gas->RegisterBitWidth < 8) 969dc750869SNate Lawson return (NULL); 970dc750869SNate Lawson 971dc750869SNate Lawson switch (gas->AddressSpaceId) { 972dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 973dc750869SNate Lawson type = SYS_RES_MEMORY; 974dc750869SNate Lawson break; 975dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 976dc750869SNate Lawson type = SYS_RES_IOPORT; 977dc750869SNate Lawson break; 978dc750869SNate Lawson default: 979dc750869SNate Lawson return (NULL); 980dc750869SNate Lawson } 981dc750869SNate Lawson 982dc750869SNate Lawson bus_set_resource(dev, type, *rid, gas->Address, gas->RegisterBitWidth / 8); 9835f96beb9SNate Lawson return (bus_alloc_resource_any(dev, type, rid, RF_ACTIVE)); 984dc750869SNate Lawson } 985dc750869SNate Lawson 986c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 9871e4925e8SNate Lawson static uint32_t 98832d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 989bc0f2195SMike Smith { 9901e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 9911e4925e8SNate Lawson ACPI_BUFFER buf; 992bc0f2195SMike Smith ACPI_HANDLE h; 993bc0f2195SMike Smith ACPI_STATUS error; 994bc0f2195SMike Smith u_int32_t pnpid; 995fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 99632d18aa5SMike Smith 997b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 99832d18aa5SMike Smith 99932d18aa5SMike Smith pnpid = 0; 1000bd189fe7SNate Lawson buf.Pointer = NULL; 1001bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1002bd189fe7SNate Lawson 100332d18aa5SMike Smith ACPI_LOCK; 100432d18aa5SMike Smith 10056fca9360SNate Lawson /* Fetch and validate the HID. */ 100632d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1007bd189fe7SNate Lawson goto out; 10086fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10096fca9360SNate Lawson if (ACPI_FAILURE(error)) 1010bd189fe7SNate Lawson goto out; 10111e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 101232d18aa5SMike Smith 10131e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 10141e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 1015be2b1797SNate Lawson 1016bd189fe7SNate Lawson out: 1017bd189fe7SNate Lawson if (buf.Pointer != NULL) 10181e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 101932d18aa5SMike Smith ACPI_UNLOCK; 102032d18aa5SMike Smith return_VALUE (pnpid); 102132d18aa5SMike Smith } 102232d18aa5SMike Smith 10231e4925e8SNate Lawson static int 10241e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1025b2566207STakanori Watanabe { 10261e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 10271e4925e8SNate Lawson ACPI_BUFFER buf; 1028b2566207STakanori Watanabe ACPI_HANDLE h; 1029b2566207STakanori Watanabe ACPI_STATUS error; 10301e4925e8SNate Lawson uint32_t *pnpid; 10311e4925e8SNate Lawson int valid, i; 1032b2566207STakanori Watanabe ACPI_LOCK_DECL; 1033b2566207STakanori Watanabe 1034b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1035b2566207STakanori Watanabe 10361e4925e8SNate Lawson pnpid = cids; 10371e4925e8SNate Lawson valid = 0; 1038bd189fe7SNate Lawson buf.Pointer = NULL; 1039bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1040bd189fe7SNate Lawson 1041b2566207STakanori Watanabe ACPI_LOCK; 1042b2566207STakanori Watanabe 10431e4925e8SNate Lawson /* Fetch and validate the CID */ 1044b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 1045bd189fe7SNate Lawson goto out; 10461e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 10471e4925e8SNate Lawson if (ACPI_FAILURE(error)) 1048bd189fe7SNate Lawson goto out; 10491e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 10501e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 1051b2566207STakanori Watanabe goto out; 1052b2566207STakanori Watanabe 10531e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 10541e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 10551e4925e8SNate Lawson for (i = 0; i < count; i++) { 10561e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 10571e4925e8SNate Lawson continue; 10581e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 10591e4925e8SNate Lawson valid++; 1060b2566207STakanori Watanabe } 1061b2566207STakanori Watanabe 10621e4925e8SNate Lawson out: 1063bd189fe7SNate Lawson if (buf.Pointer != NULL) 10641e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 10651e4925e8SNate Lawson ACPI_UNLOCK; 10661e4925e8SNate Lawson return_VALUE (valid); 10671e4925e8SNate Lawson } 1068b2566207STakanori Watanabe 1069ce619b2eSNate Lawson static char * 1070ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1071ce619b2eSNate Lawson { 1072ce619b2eSNate Lawson ACPI_HANDLE h; 1073ce619b2eSNate Lawson int i; 1074ce619b2eSNate Lawson 1075ce619b2eSNate Lawson h = acpi_get_handle(dev); 1076ce619b2eSNate Lawson if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1077ce619b2eSNate Lawson return (NULL); 1078ce619b2eSNate Lawson 1079ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1080ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1081ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1082ce619b2eSNate Lawson return (ids[i]); 1083ce619b2eSNate Lawson } 1084ce619b2eSNate Lawson return (NULL); 1085ce619b2eSNate Lawson } 1086ce619b2eSNate Lawson 1087ce619b2eSNate Lawson static ACPI_STATUS 1088ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1089ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1090ce619b2eSNate Lawson { 1091ce619b2eSNate Lawson ACPI_HANDLE h; 1092ce619b2eSNate Lawson 1093ce619b2eSNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 1094ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1095ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1096ce619b2eSNate Lawson } 1097ce619b2eSNate Lawson 1098ce619b2eSNate Lawson static ACPI_STATUS 1099ce619b2eSNate Lawson acpi_device_walk_ns(device_t bus, device_t dev, ACPI_OBJECT_TYPE type, 1100ce619b2eSNate Lawson UINT32 max_depth, ACPI_WALK_CALLBACK user_fn, void *context, void **ret) 1101ce619b2eSNate Lawson { 1102ce619b2eSNate Lawson ACPI_HANDLE h; 1103ce619b2eSNate Lawson 1104ce619b2eSNate Lawson if ((h = acpi_get_handle(dev)) == NULL) 1105ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1106ce619b2eSNate Lawson return (AcpiWalkNamespace(type, h, max_depth, user_fn, context, ret)); 1107ce619b2eSNate Lawson } 1108ce619b2eSNate Lawson 110932d18aa5SMike Smith static int 111032d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 111132d18aa5SMike Smith { 11121e4925e8SNate Lawson int result, cid_count, i; 11131e4925e8SNate Lawson uint32_t lid, cids[8]; 1114bc0f2195SMike Smith 1115b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1116bc0f2195SMike Smith 1117bc0f2195SMike Smith /* 1118bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1119bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1120bc0f2195SMike Smith * that to happen, so don't ever return it. 1121bc0f2195SMike Smith */ 1122bc0f2195SMike Smith result = ENXIO; 1123bc0f2195SMike Smith 1124be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1125b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 11261e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1127bc0f2195SMike Smith while (ids && ids->ip_id) { 11281e4925e8SNate Lawson if (lid == ids->ip_id) { 1129bc0f2195SMike Smith result = 0; 1130bc0f2195SMike Smith goto out; 1131bc0f2195SMike Smith } 11321e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 11331e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 11341e4925e8SNate Lawson result = 0; 11351e4925e8SNate Lawson goto out; 11361e4925e8SNate Lawson } 11371e4925e8SNate Lawson } 1138bc0f2195SMike Smith ids++; 1139bc0f2195SMike Smith } 1140be2b1797SNate Lawson 1141bc0f2195SMike Smith out: 1142bc0f2195SMike Smith return_VALUE (result); 1143bc0f2195SMike Smith } 1144bc0f2195SMike Smith 1145bc0f2195SMike Smith /* 114615e32d5dSMike Smith * Scan relevant portions of the ACPI namespace and attach child devices. 114715e32d5dSMike Smith * 1148be2b1797SNate Lawson * Note that we only expect to find devices in the \_PR_, \_TZ_, \_SI_ and 1149be2b1797SNate Lawson * \_SB_ scopes, and \_PR_ and \_TZ_ become obsolete in the ACPI 2.0 spec. 115015e32d5dSMike Smith */ 115115e32d5dSMike Smith static void 115215e32d5dSMike Smith acpi_probe_children(device_t bus) 115315e32d5dSMike Smith { 115415e32d5dSMike Smith ACPI_HANDLE parent; 1155be2b1797SNate Lawson ACPI_STATUS status; 115615e32d5dSMike Smith int i; 115791233413SNate Lawson static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI", "\\_SB_", NULL}; 115815e32d5dSMike Smith 1159b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1160cb9b0d80SMike Smith ACPI_ASSERTLOCK; 11610ae55423SMike Smith 1162be2b1797SNate Lawson /* Create any static children by calling device identify methods. */ 11634c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 116415e32d5dSMike Smith bus_generic_probe(bus); 116515e32d5dSMike Smith 116615e32d5dSMike Smith /* 116715e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 116815e32d5dSMike Smith * we find. 116915e32d5dSMike Smith * 117015e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1171be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1172be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1173be2b1797SNate Lawson * devices as they appear, which might be smarter.) 117415e32d5dSMike Smith */ 11754c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 1176be2b1797SNate Lawson for (i = 0; scopes[i] != NULL; i++) { 1177be2b1797SNate Lawson status = AcpiGetHandle(ACPI_ROOT_OBJECT, scopes[i], &parent); 1178be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1179be2b1797SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, parent, 100, acpi_probe_child, 1180be2b1797SNate Lawson bus, NULL); 1181be2b1797SNate Lawson } 1182be2b1797SNate Lawson } 118315e32d5dSMike Smith 118415e32d5dSMike Smith /* 118515e32d5dSMike Smith * Scan all of the child devices we have created and let them probe/attach. 118615e32d5dSMike Smith */ 11874c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 118815e32d5dSMike Smith bus_generic_attach(bus); 118915e32d5dSMike Smith 119015e32d5dSMike Smith /* 119115e32d5dSMike Smith * Some of these children may have attached others as part of their attach 119215e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 119315e32d5dSMike Smith */ 11944c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 119515e32d5dSMike Smith bus_generic_attach(bus); 11960ae55423SMike Smith 119788a79fc0SNate Lawson /* Attach wake sysctls. */ 11985c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 119988a79fc0SNate Lawson 1200bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 12010ae55423SMike Smith return_VOID; 120215e32d5dSMike Smith } 120315e32d5dSMike Smith 1204b82ca9a9SNate Lawson /* 1205b82ca9a9SNate Lawson * Determine the probe order for a given device and return non-zero if it 1206b82ca9a9SNate Lawson * should be attached immediately. 1207b82ca9a9SNate Lawson */ 120891233413SNate Lawson static int 1209b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 121091233413SNate Lawson { 121191233413SNate Lawson int ret; 121291233413SNate Lawson 1213b82ca9a9SNate Lawson /* 1214b82ca9a9SNate Lawson * 1. I/O port and memory system resource holders 1215b82ca9a9SNate Lawson * 2. Embedded controllers (to handle early accesses) 1216b82ca9a9SNate Lawson */ 121791233413SNate Lawson ret = 0; 121891233413SNate Lawson if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) { 121991233413SNate Lawson *order = 1; 122091233413SNate Lawson ret = 1; 1221b82ca9a9SNate Lawson } else if (acpi_MatchHid(handle, "PNP0C09")) { 122291233413SNate Lawson *order = 2; 122391233413SNate Lawson ret = 1; 122491233413SNate Lawson } 122591233413SNate Lawson 1226b82ca9a9SNate Lawson /* Always probe/attach immediately if we're debugging. */ 1227b82ca9a9SNate Lawson ACPI_DEBUG_EXEC(ret = 1); 1228b82ca9a9SNate Lawson 122991233413SNate Lawson return (ret); 123091233413SNate Lawson } 123191233413SNate Lawson 123215e32d5dSMike Smith /* 123315e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 123415e32d5dSMike Smith * it. 123515e32d5dSMike Smith */ 123615e32d5dSMike Smith static ACPI_STATUS 123715e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 123815e32d5dSMike Smith { 123915e32d5dSMike Smith ACPI_OBJECT_TYPE type; 124091233413SNate Lawson device_t child, bus; 124191233413SNate Lawson int order, probe_now; 124215e32d5dSMike Smith 1243b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 12440ae55423SMike Smith 1245be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 12460ae55423SMike Smith if (acpi_avoid(handle)) 12470ae55423SMike Smith return_ACPI_STATUS (AE_OK); 12480ae55423SMike Smith 124991233413SNate Lawson bus = (device_t)context; 1250b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 125115e32d5dSMike Smith switch (type) { 125215e32d5dSMike Smith case ACPI_TYPE_DEVICE: 125315e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 125415e32d5dSMike Smith case ACPI_TYPE_THERMAL: 125515e32d5dSMike Smith case ACPI_TYPE_POWER: 12560ae55423SMike Smith if (acpi_disabled("children")) 12570ae55423SMike Smith break; 1258be2b1797SNate Lawson 125915e32d5dSMike Smith /* 126015e32d5dSMike Smith * Create a placeholder device for this node. Sort the placeholder 1261b82ca9a9SNate Lawson * so that the probe/attach passes will run breadth-first. Orders 1262b82ca9a9SNate Lawson * less than 10 are reserved for special objects (i.e., system 1263b82ca9a9SNate Lawson * resources). Larger values are used for all other devices. 126415e32d5dSMike Smith */ 1265be2b1797SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", 1266be2b1797SNate Lawson acpi_name(handle))); 1267b82ca9a9SNate Lawson order = (level + 1) * 10; 1268b82ca9a9SNate Lawson probe_now = acpi_probe_order(handle, &order); 126991233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1270bc0f2195SMike Smith if (child == NULL) 1271bc0f2195SMike Smith break; 127259a890e6SNate Lawson 127359a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 127415e32d5dSMike Smith acpi_set_handle(child, handle); 127559a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1276bc0f2195SMike Smith 1277e8b4d56eSNate Lawson /* Check if the device can generate wake events. */ 1278e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiEvaluateObject(handle, "_PRW", NULL, NULL))) 1279e8b4d56eSNate Lawson device_set_flags(child, ACPI_FLAG_WAKE_CAPABLE); 1280e8b4d56eSNate Lawson 1281bc0f2195SMike Smith /* 1282b519f9d6SMike Smith * Check that the device is present. If it's not present, 1283b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1284b519f9d6SMike Smith * the handle, but we don't probe it). 1285b519f9d6SMike Smith */ 1286be2b1797SNate Lawson if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1287b519f9d6SMike Smith device_disable(child); 1288b519f9d6SMike Smith break; 1289b519f9d6SMike Smith } 1290b519f9d6SMike Smith 1291b519f9d6SMike Smith /* 1292bc0f2195SMike Smith * Get the device's resource settings and attach them. 1293bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1294bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1295bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1296bc0f2195SMike Smith * device not to have any resources. 1297bc0f2195SMike Smith */ 129872ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 1299bc0f2195SMike Smith 1300f504b607SNate Lawson /* If order was overridden, probe/attach now rather than later. */ 130191233413SNate Lawson if (probe_now) 130291233413SNate Lawson device_probe_and_attach(child); 13030ae55423SMike Smith break; 130415e32d5dSMike Smith } 130515e32d5dSMike Smith } 1306be2b1797SNate Lawson 13070ae55423SMike Smith return_ACPI_STATUS (AE_OK); 130815e32d5dSMike Smith } 130915e32d5dSMike Smith 131059a890e6SNate Lawson /* 131159a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 131259a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 131359a890e6SNate Lawson */ 131459a890e6SNate Lawson void 131559a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 131659a890e6SNate Lawson { 131759a890e6SNate Lawson } 131859a890e6SNate Lawson 131915e32d5dSMike Smith static void 132015e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 132115e32d5dSMike Smith { 1322d33f4987STakanori Watanabe ACPI_STATUS status; 1323eeb3a05fSNate Lawson 1324eeb3a05fSNate Lawson /* 132580f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 132680f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 132780f0e4c2SNate Lawson * an AP. 1328eeb3a05fSNate Lawson */ 1329eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1330904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1331904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1332904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1333904bf0c2SNate Lawson AcpiFormatException(status)); 1334904bf0c2SNate Lawson return; 1335904bf0c2SNate Lawson } 1336eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 133755398047SNate Lawson ACPI_DISABLE_IRQS(); 1338865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1339be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1340bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 134115e32d5dSMike Smith } else { 134215e32d5dSMike Smith DELAY(1000000); 134315e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 134415e32d5dSMike Smith } 134580f0e4c2SNate Lawson } else { 134680f0e4c2SNate Lawson printf("Shutting down ACPI\n"); 134780f0e4c2SNate Lawson AcpiTerminate(); 134880f0e4c2SNate Lawson } 134915e32d5dSMike Smith } 135015e32d5dSMike Smith 135113d4f7baSMitsuru IWASAKI static void 135213d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 135313d4f7baSMitsuru IWASAKI { 135413d4f7baSMitsuru IWASAKI static int first_time = 1; 135513d4f7baSMitsuru IWASAKI 1356cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1357cb9b0d80SMike Smith 135813d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 1359be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->PwrButton == 0) { 136059ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 136113d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 136233febf93SNate Lawson acpi_event_power_button_sleep, sc); 1363be2b1797SNate Lawson if (first_time) 13646c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 136513d4f7baSMitsuru IWASAKI } 1366be2b1797SNate Lawson if (AcpiGbl_FADT != NULL && AcpiGbl_FADT->SleepButton == 0) { 136759ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 136813d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 136933febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1370be2b1797SNate Lawson if (first_time) 13716c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 137213d4f7baSMitsuru IWASAKI } 137313d4f7baSMitsuru IWASAKI 137413d4f7baSMitsuru IWASAKI first_time = 0; 137513d4f7baSMitsuru IWASAKI } 137613d4f7baSMitsuru IWASAKI 137715e32d5dSMike Smith /* 1378c5ba0be4SMike Smith * Returns true if the device is actually present and should 1379c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1380c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1381c5ba0be4SMike Smith */ 1382c5ba0be4SMike Smith BOOLEAN 1383c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1384c5ba0be4SMike Smith { 13851e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1386c5ba0be4SMike Smith ACPI_HANDLE h; 13871e4925e8SNate Lawson ACPI_BUFFER buf; 1388c5ba0be4SMike Smith ACPI_STATUS error; 13891e4925e8SNate Lawson int ret; 1390c5ba0be4SMike Smith 1391cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1392cb9b0d80SMike Smith 13931e4925e8SNate Lawson ret = FALSE; 1394c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1395c5ba0be4SMike Smith return (FALSE); 13961e4925e8SNate Lawson buf.Pointer = NULL; 13971e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 13986fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13996fca9360SNate Lawson if (ACPI_FAILURE(error)) 1400c5ba0be4SMike Smith return (FALSE); 14011e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1402be2b1797SNate Lawson 1403be2b1797SNate Lawson /* If no _STA method, must be present */ 14041e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 14051e4925e8SNate Lawson ret = TRUE; 1406be2b1797SNate Lawson 1407be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 14081e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x9) == 0x9) 14091e4925e8SNate Lawson ret = TRUE; 1410be2b1797SNate Lawson 14111e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14121e4925e8SNate Lawson return (ret); 1413c5ba0be4SMike Smith } 1414c5ba0be4SMike Smith 1415c5ba0be4SMike Smith /* 1416b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1417b53f2771SMike Smith */ 1418b53f2771SMike Smith BOOLEAN 1419b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1420b53f2771SMike Smith { 14211e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1422b53f2771SMike Smith ACPI_HANDLE h; 14231e4925e8SNate Lawson ACPI_BUFFER buf; 1424b53f2771SMike Smith ACPI_STATUS error; 14251e4925e8SNate Lawson int ret; 1426b53f2771SMike Smith 1427b53f2771SMike Smith ACPI_ASSERTLOCK; 1428b53f2771SMike Smith 14291e4925e8SNate Lawson ret = FALSE; 1430b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1431b53f2771SMike Smith return (FALSE); 14321e4925e8SNate Lawson buf.Pointer = NULL; 14331e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14346fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14356fca9360SNate Lawson if (ACPI_FAILURE(error)) 1436b53f2771SMike Smith return (FALSE); 14371e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1438be2b1797SNate Lawson 1439be2b1797SNate Lawson /* If no _STA method, must be present */ 14401e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 14411e4925e8SNate Lawson ret = TRUE; 1442be2b1797SNate Lawson 1443be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 14441e4925e8SNate Lawson if ((devinfo->CurrentStatus & 0x19) == 0x19) 14451e4925e8SNate Lawson ret = TRUE; 1446be2b1797SNate Lawson 14471e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14481e4925e8SNate Lawson return (ret); 1449b53f2771SMike Smith } 1450b53f2771SMike Smith 1451b53f2771SMike Smith /* 145291233413SNate Lawson * Match a HID string against a handle 145315e32d5dSMike Smith */ 1454ce619b2eSNate Lawson static BOOLEAN 1455ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 145615e32d5dSMike Smith { 14571e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 14581e4925e8SNate Lawson ACPI_BUFFER buf; 145915e32d5dSMike Smith ACPI_STATUS error; 14601e4925e8SNate Lawson int ret, i; 146115e32d5dSMike Smith 1462cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1463cb9b0d80SMike Smith 14641e4925e8SNate Lawson ret = FALSE; 146591233413SNate Lawson if (hid == NULL || h == NULL) 146691233413SNate Lawson return (ret); 14671e4925e8SNate Lawson buf.Pointer = NULL; 14681e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 14696fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 14706fca9360SNate Lawson if (ACPI_FAILURE(error)) 147191233413SNate Lawson return (ret); 14721e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1473be2b1797SNate Lawson 1474c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1475c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 14761e4925e8SNate Lawson ret = TRUE; 1477c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 14781e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 14791e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 14801e4925e8SNate Lawson ret = TRUE; 14811e4925e8SNate Lawson break; 14821e4925e8SNate Lawson } 14831e4925e8SNate Lawson } 14841e4925e8SNate Lawson } 1485be2b1797SNate Lawson 14861e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 14871e4925e8SNate Lawson return (ret); 148815e32d5dSMike Smith } 148915e32d5dSMike Smith 149015e32d5dSMike Smith /* 1491c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1492c5ba0be4SMike Smith * or one if its parents. 1493c5ba0be4SMike Smith */ 1494c5ba0be4SMike Smith ACPI_STATUS 1495c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1496c5ba0be4SMike Smith { 1497c5ba0be4SMike Smith ACPI_HANDLE r; 1498c5ba0be4SMike Smith ACPI_STATUS status; 1499c5ba0be4SMike Smith 1500cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1501cb9b0d80SMike Smith 1502be2b1797SNate Lawson /* Walk back up the tree to the root */ 1503c5ba0be4SMike Smith for (;;) { 1504be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1505be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1506c5ba0be4SMike Smith *result = r; 1507c5ba0be4SMike Smith return (AE_OK); 1508c5ba0be4SMike Smith } 1509c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1510c5ba0be4SMike Smith return (AE_OK); 1511b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1512c5ba0be4SMike Smith return (AE_NOT_FOUND); 1513c5ba0be4SMike Smith parent = r; 1514c5ba0be4SMike Smith } 1515c5ba0be4SMike Smith } 1516c5ba0be4SMike Smith 1517eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 1518eea17c34SNate Lawson uint32_t 1519eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 1520eea17c34SNate Lawson { 1521eea17c34SNate Lawson uint32_t delta; 1522eea17c34SNate Lawson 1523eea17c34SNate Lawson if (end >= start) 1524eea17c34SNate Lawson delta = end - start; 1525eea17c34SNate Lawson else if (AcpiGbl_FADT->TmrValExt == 0) 15268d01ceefSNate Lawson delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 1527eea17c34SNate Lawson else 1528eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 1529eea17c34SNate Lawson return (delta); 1530eea17c34SNate Lawson } 1531eea17c34SNate Lawson 1532c5ba0be4SMike Smith /* 1533c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 1534c5ba0be4SMike Smith */ 1535c5ba0be4SMike Smith ACPI_BUFFER * 1536c5ba0be4SMike Smith acpi_AllocBuffer(int size) 1537c5ba0be4SMike Smith { 1538c5ba0be4SMike Smith ACPI_BUFFER *buf; 1539c5ba0be4SMike Smith 1540c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 1541c5ba0be4SMike Smith return (NULL); 1542c5ba0be4SMike Smith buf->Length = size; 1543c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 1544c5ba0be4SMike Smith return (buf); 1545c5ba0be4SMike Smith } 1546c5ba0be4SMike Smith 1547c310653eSNate Lawson ACPI_STATUS 1548cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 1549c310653eSNate Lawson { 1550c310653eSNate Lawson ACPI_OBJECT arg1; 1551c310653eSNate Lawson ACPI_OBJECT_LIST args; 1552c310653eSNate Lawson 1553c310653eSNate Lawson ACPI_ASSERTLOCK; 1554c310653eSNate Lawson 1555c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 1556c310653eSNate Lawson arg1.Integer.Value = number; 1557c310653eSNate Lawson args.Count = 1; 1558c310653eSNate Lawson args.Pointer = &arg1; 1559c310653eSNate Lawson 1560c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 1561c310653eSNate Lawson } 1562c310653eSNate Lawson 1563c5ba0be4SMike Smith /* 1564c5ba0be4SMike Smith * Evaluate a path that should return an integer. 1565c5ba0be4SMike Smith */ 1566c5ba0be4SMike Smith ACPI_STATUS 1567cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 1568c5ba0be4SMike Smith { 1569be2b1797SNate Lawson ACPI_STATUS status; 1570c5ba0be4SMike Smith ACPI_BUFFER buf; 1571c573e654SMitsuru IWASAKI ACPI_OBJECT param; 1572c5ba0be4SMike Smith 1573cb9b0d80SMike Smith ACPI_ASSERTLOCK; 1574cb9b0d80SMike Smith 1575c5ba0be4SMike Smith if (handle == NULL) 1576c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 15770c7da7acSJohn Baldwin 15780c7da7acSJohn Baldwin /* 15790c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 15800c7da7acSJohn Baldwin * a method that will return an Integer. 15810c7da7acSJohn Baldwin */ 1582c5ba0be4SMike Smith buf.Pointer = ¶m; 1583c5ba0be4SMike Smith buf.Length = sizeof(param); 1584be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1585be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1586be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 1587c5ba0be4SMike Smith *number = param.Integer.Value; 1588be2b1797SNate Lawson else 1589be2b1797SNate Lawson status = AE_TYPE; 1590c5ba0be4SMike Smith } 15910c7da7acSJohn Baldwin 15920c7da7acSJohn Baldwin /* 15930c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 15940c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 15950c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 15960c7da7acSJohn Baldwin * convert it into an Integer as best we can. 15970c7da7acSJohn Baldwin * 15980c7da7acSJohn Baldwin * This is a hack. 15990c7da7acSJohn Baldwin */ 1600be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 1601b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 1602be2b1797SNate Lawson status = AE_NO_MEMORY; 16030c7da7acSJohn Baldwin } else { 1604be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 1605be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 1606be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 16070c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 16080c7da7acSJohn Baldwin } 1609f97739daSNate Lawson } 1610be2b1797SNate Lawson return (status); 1611c5ba0be4SMike Smith } 1612c5ba0be4SMike Smith 1613c573e654SMitsuru IWASAKI ACPI_STATUS 1614cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 1615c573e654SMitsuru IWASAKI { 1616c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 1617dba55fa2SNate Lawson UINT8 *val; 1618c573e654SMitsuru IWASAKI int i; 1619c573e654SMitsuru IWASAKI 1620c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 1621c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 1622c573e654SMitsuru IWASAKI *number = p->Integer.Value; 1623c573e654SMitsuru IWASAKI return (AE_OK); 1624c573e654SMitsuru IWASAKI } 1625c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 1626c573e654SMitsuru IWASAKI return (AE_TYPE); 1627c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 1628c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 1629be2b1797SNate Lawson 1630c573e654SMitsuru IWASAKI *number = 0; 1631dba55fa2SNate Lawson val = p->Buffer.Pointer; 1632c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 1633dba55fa2SNate Lawson *number += val[i] << (i * 8); 1634c573e654SMitsuru IWASAKI return (AE_OK); 1635c573e654SMitsuru IWASAKI } 1636c573e654SMitsuru IWASAKI 1637c5ba0be4SMike Smith /* 1638c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 1639c5ba0be4SMike Smith * function for each element. 1640c5ba0be4SMike Smith * 1641c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 1642c5ba0be4SMike Smith */ 1643c5ba0be4SMike Smith ACPI_STATUS 1644be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 1645be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 1646c5ba0be4SMike Smith { 1647c5ba0be4SMike Smith ACPI_OBJECT *comp; 1648c5ba0be4SMike Smith int i; 1649c5ba0be4SMike Smith 1650be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 1651c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 1652c5ba0be4SMike Smith 1653be2b1797SNate Lawson /* Iterate over components */ 1654be2b1797SNate Lawson i = 0; 1655be2b1797SNate Lawson comp = pkg->Package.Elements; 1656be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 1657c5ba0be4SMike Smith func(comp, arg); 1658c5ba0be4SMike Smith 1659c5ba0be4SMike Smith return (AE_OK); 1660c5ba0be4SMike Smith } 1661c5ba0be4SMike Smith 16626f69255bSMike Smith /* 16636f69255bSMike Smith * Find the (index)th resource object in a set. 16646f69255bSMike Smith */ 16656f69255bSMike Smith ACPI_STATUS 16666d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 16676f69255bSMike Smith { 16686d63101aSMike Smith ACPI_RESOURCE *rp; 16696f69255bSMike Smith int i; 16706f69255bSMike Smith 16716d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 16726f69255bSMike Smith i = index; 16736d63101aSMike Smith while (i-- > 0) { 1674be2b1797SNate Lawson /* Range check */ 16756d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 16766f69255bSMike Smith return (AE_BAD_PARAMETER); 1677be2b1797SNate Lawson 1678be2b1797SNate Lawson /* Check for terminator */ 1679be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 16806d63101aSMike Smith return (AE_NOT_FOUND); 1681abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 16826f69255bSMike Smith } 16836f69255bSMike Smith if (resp != NULL) 16846d63101aSMike Smith *resp = rp; 1685be2b1797SNate Lawson 16866d63101aSMike Smith return (AE_OK); 16876d63101aSMike Smith } 16886d63101aSMike Smith 16896d63101aSMike Smith /* 16906d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 16916d63101aSMike Smith * 16926d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 16936d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 16946d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 16956d63101aSMike Smith * resources. 16966d63101aSMike Smith */ 16976d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 16986d63101aSMike Smith 16996d63101aSMike Smith ACPI_STATUS 17006d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 17016d63101aSMike Smith { 17026d63101aSMike Smith ACPI_RESOURCE *rp; 17036d63101aSMike Smith void *newp; 17046d63101aSMike Smith 1705be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 17066d63101aSMike Smith if (buf->Pointer == NULL) { 17076d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 17086d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 17096d63101aSMike Smith return (AE_NO_MEMORY); 17106d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 17116d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 17126d63101aSMike Smith rp->Length = 0; 17136d63101aSMike Smith } 17146d63101aSMike Smith if (res == NULL) 17156d63101aSMike Smith return (AE_OK); 17166d63101aSMike Smith 17176d63101aSMike Smith /* 17186d63101aSMike Smith * Scan the current buffer looking for the terminator. 17196d63101aSMike Smith * This will either find the terminator or hit the end 17206d63101aSMike Smith * of the buffer and return an error. 17216d63101aSMike Smith */ 17226d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 17236d63101aSMike Smith for (;;) { 1724be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 17256d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 17266d63101aSMike Smith return (AE_BAD_PARAMETER); 1727be2b1797SNate Lawson if (rp->Id == ACPI_RSTYPE_END_TAG || rp->Length == 0) 17286d63101aSMike Smith break; 1729abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 17306d63101aSMike Smith } 17316d63101aSMike Smith 17326d63101aSMike Smith /* 17336d63101aSMike Smith * Check the size of the buffer and expand if required. 17346d63101aSMike Smith * 17356d63101aSMike Smith * Required size is: 17366d63101aSMike Smith * size of existing resources before terminator + 17376d63101aSMike Smith * size of new resource and header + 17386d63101aSMike Smith * size of terminator. 17396d63101aSMike Smith * 17406d63101aSMike Smith * Note that this loop should really only run once, unless 17416d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 17426d63101aSMike Smith */ 17436d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 17446d63101aSMike Smith res->Length + ACPI_RESOURCE_LENGTH_NO_DATA + 17456d63101aSMike Smith ACPI_RESOURCE_LENGTH) >= buf->Length) { 17466d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 17476d63101aSMike Smith return (AE_NO_MEMORY); 17486d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 1749a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 1750a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 17516d63101aSMike Smith AcpiOsFree(buf->Pointer); 17526d63101aSMike Smith buf->Pointer = newp; 17536d63101aSMike Smith buf->Length += buf->Length; 17546d63101aSMike Smith } 17556d63101aSMike Smith 1756be2b1797SNate Lawson /* Insert the new resource. */ 17576d63101aSMike Smith bcopy(res, rp, res->Length + ACPI_RESOURCE_LENGTH_NO_DATA); 17586d63101aSMike Smith 1759be2b1797SNate Lawson /* And add the terminator. */ 1760abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 17616d63101aSMike Smith rp->Id = ACPI_RSTYPE_END_TAG; 17626d63101aSMike Smith rp->Length = 0; 17636d63101aSMike Smith 17646f69255bSMike Smith return (AE_OK); 17656f69255bSMike Smith } 1766c5ba0be4SMike Smith 1767da14ac9fSJohn Baldwin /* 1768da14ac9fSJohn Baldwin * Set interrupt model. 1769da14ac9fSJohn Baldwin */ 1770da14ac9fSJohn Baldwin ACPI_STATUS 1771da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 1772da14ac9fSJohn Baldwin { 1773da14ac9fSJohn Baldwin 1774c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 1775da14ac9fSJohn Baldwin } 1776da14ac9fSJohn Baldwin 1777ece50487SMitsuru IWASAKI #define ACPI_MINIMUM_AWAKETIME 5 1778ece50487SMitsuru IWASAKI 1779ece50487SMitsuru IWASAKI static void 1780ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 1781ece50487SMitsuru IWASAKI { 1782ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 1783ece50487SMitsuru IWASAKI } 1784c30382dfSMitsuru IWASAKI 178515e32d5dSMike Smith /* 178615e32d5dSMike Smith * Set the system sleep state 178715e32d5dSMike Smith * 1788be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 178915e32d5dSMike Smith */ 179015e32d5dSMike Smith ACPI_STATUS 179115e32d5dSMike Smith acpi_SetSleepState(struct acpi_softc *sc, int state) 179215e32d5dSMike Smith { 179315e32d5dSMike Smith ACPI_STATUS status = AE_OK; 179456d8cb57SMitsuru IWASAKI UINT8 TypeA; 179556d8cb57SMitsuru IWASAKI UINT8 TypeB; 179615e32d5dSMike Smith 1797b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 1798cb9b0d80SMike Smith ACPI_ASSERTLOCK; 17990ae55423SMike Smith 180098175614SNate Lawson /* Avoid reentry if already attempting to suspend. */ 18016397624dSMitsuru IWASAKI if (sc->acpi_sstate != ACPI_STATE_S0) 180298175614SNate Lawson return_ACPI_STATUS (AE_BAD_PARAMETER); 18036397624dSMitsuru IWASAKI 180498175614SNate Lawson /* We recently woke up so don't suspend again for a while. */ 1805ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1806ece50487SMitsuru IWASAKI return_ACPI_STATUS (AE_OK); 1807ece50487SMitsuru IWASAKI 180815e32d5dSMike Smith switch (state) { 180915e32d5dSMike Smith case ACPI_STATE_S1: 18106161544cSTakanori Watanabe case ACPI_STATE_S2: 18116161544cSTakanori Watanabe case ACPI_STATE_S3: 18126161544cSTakanori Watanabe case ACPI_STATE_S4: 1813be2b1797SNate Lawson status = AcpiGetSleepTypeData((UINT8)state, &TypeA, &TypeB); 181498175614SNate Lawson if (status == AE_NOT_FOUND) { 181598175614SNate Lawson device_printf(sc->acpi_dev, 181698175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 181798175614SNate Lawson break; 181898175614SNate Lawson } else if (ACPI_FAILURE(status)) { 1819be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 1820be2b1797SNate Lawson AcpiFormatException(status)); 182156d8cb57SMitsuru IWASAKI break; 182256d8cb57SMitsuru IWASAKI } 182356d8cb57SMitsuru IWASAKI 1824a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 1825a1fccb47SMitsuru IWASAKI sc->acpi_sleep_disabled = 1; 1826a1fccb47SMitsuru IWASAKI 1827e8b4d56eSNate Lawson /* Disable all wake GPEs not appropriate for this state. */ 1828e8b4d56eSNate Lawson acpi_wake_limit_walk(state); 1829e8b4d56eSNate Lawson 1830be2b1797SNate Lawson /* Inform all devices that we are going to sleep. */ 183115e32d5dSMike Smith if (DEVICE_SUSPEND(root_bus) != 0) { 183215e32d5dSMike Smith /* 183315e32d5dSMike Smith * Re-wake the system. 183415e32d5dSMike Smith * 183515e32d5dSMike Smith * XXX note that a better two-pass approach with a 'veto' pass 183615e32d5dSMike Smith * followed by a "real thing" pass would be better, but the 183715e32d5dSMike Smith * current bus interface does not provide for this. 183815e32d5dSMike Smith */ 183915e32d5dSMike Smith DEVICE_RESUME(root_bus); 18400ae55423SMike Smith return_ACPI_STATUS (AE_ERROR); 184115e32d5dSMike Smith } 1842b9c780d6SMitsuru IWASAKI 1843be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 1844be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1845b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 1846b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 1847b9c780d6SMitsuru IWASAKI break; 1848b9c780d6SMitsuru IWASAKI } 1849b9c780d6SMitsuru IWASAKI 1850be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 1851ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 1852ff01efb5SMitsuru IWASAKI 18536161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 18546161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 18556161544cSTakanori Watanabe 1856be2b1797SNate Lawson /* AcpiEnterSleepState() may be incomplete, unlock if locked. */ 185759ddeb18SNate Lawson if (AcpiGbl_MutexInfo[ACPI_MTX_HARDWARE].OwnerId != 185859ddeb18SNate Lawson ACPI_MUTEX_NOT_ACQUIRED) { 185959ddeb18SNate Lawson 18606161544cSTakanori Watanabe AcpiUtReleaseMutex(ACPI_MTX_HARDWARE); 1861a1fccb47SMitsuru IWASAKI } 18626161544cSTakanori Watanabe 18636161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 1864be2b1797SNate Lawson if (state == ACPI_STATE_S4) 1865964679ceSMitsuru IWASAKI AcpiEnable(); 18666161544cSTakanori Watanabe } else { 1867be2b1797SNate Lawson status = AcpiEnterSleepState((UINT8)state); 1868be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1869be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 1870be2b1797SNate Lawson AcpiFormatException(status)); 1871c30382dfSMitsuru IWASAKI break; 187215e32d5dSMike Smith } 18736161544cSTakanori Watanabe } 1874ff741befSTakanori Watanabe AcpiLeaveSleepState((UINT8)state); 187515e32d5dSMike Smith DEVICE_RESUME(root_bus); 187615e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 187713d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 187815e32d5dSMike Smith break; 187915e32d5dSMike Smith case ACPI_STATE_S5: 188015e32d5dSMike Smith /* 188115e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 188215e32d5dSMike Smith * shutdown handlers. 188315e32d5dSMike Smith */ 188415e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 188515e32d5dSMike Smith break; 188698175614SNate Lawson case ACPI_STATE_S0: 188715e32d5dSMike Smith default: 188815e32d5dSMike Smith status = AE_BAD_PARAMETER; 188915e32d5dSMike Smith break; 189015e32d5dSMike Smith } 1891ece50487SMitsuru IWASAKI 189298175614SNate Lawson /* Disable a second sleep request for a short period */ 1893ece50487SMitsuru IWASAKI if (sc->acpi_sleep_disabled) 1894ece50487SMitsuru IWASAKI timeout(acpi_sleep_enable, (caddr_t)sc, hz * ACPI_MINIMUM_AWAKETIME); 1895ece50487SMitsuru IWASAKI 18960ae55423SMike Smith return_ACPI_STATUS (status); 189715e32d5dSMike Smith } 189815e32d5dSMike Smith 1899e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 1900e8b4d56eSNate Lawson int 1901e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 1902e8b4d56eSNate Lawson { 1903e8b4d56eSNate Lawson struct acpi_prw_data prw; 1904e8b4d56eSNate Lawson 1905e8b4d56eSNate Lawson /* Check that the device can wake the system. */ 1906e8b4d56eSNate Lawson if ((device_get_flags(dev) & ACPI_FLAG_WAKE_CAPABLE) == 0) 1907e8b4d56eSNate Lawson return (ENXIO); 1908e8b4d56eSNate Lawson 1909e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1910e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 1911e8b4d56eSNate Lawson return (ENXIO); 1912e8b4d56eSNate Lawson 1913e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 1914e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 1915e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 1916e8b4d56eSNate Lawson return (ENXIO); 1917e8b4d56eSNate Lawson } 1918e8b4d56eSNate Lawson 1919e8b4d56eSNate Lawson return (0); 1920e8b4d56eSNate Lawson } 1921e8b4d56eSNate Lawson 1922e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 1923e8b4d56eSNate Lawson int 1924e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 1925e8b4d56eSNate Lawson { 1926e8b4d56eSNate Lawson struct acpi_prw_data prw; 1927e8b4d56eSNate Lawson ACPI_HANDLE handle; 1928e8b4d56eSNate Lawson ACPI_STATUS status; 1929e8b4d56eSNate Lawson int flags; 1930e8b4d56eSNate Lawson 1931e8b4d56eSNate Lawson /* Make sure the device supports waking the system. */ 1932e8b4d56eSNate Lawson flags = device_get_flags(dev); 1933e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1934e8b4d56eSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1935e8b4d56eSNate Lawson return (ENXIO); 1936e8b4d56eSNate Lawson 1937e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 1938e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1939e8b4d56eSNate Lawson return (ENXIO); 1940e8b4d56eSNate Lawson 1941e8b4d56eSNate Lawson if (enable) { 1942e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1943e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1944e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 1945e8b4d56eSNate Lawson return (ENXIO); 1946e8b4d56eSNate Lawson } 1947e8b4d56eSNate Lawson device_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 1948e8b4d56eSNate Lawson } else { 1949e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1950e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 1951e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 1952e8b4d56eSNate Lawson return (ENXIO); 1953e8b4d56eSNate Lawson } 1954e8b4d56eSNate Lawson device_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 1955e8b4d56eSNate Lawson } 1956e8b4d56eSNate Lawson 1957e8b4d56eSNate Lawson return (0); 1958e8b4d56eSNate Lawson } 1959e8b4d56eSNate Lawson 1960e8b4d56eSNate Lawson /* Configure a device's GPE appropriately for the new sleep state. */ 1961e8b4d56eSNate Lawson int 1962e8b4d56eSNate Lawson acpi_wake_sleep_prep(device_t dev, int sstate) 1963e8b4d56eSNate Lawson { 1964e8b4d56eSNate Lawson struct acpi_prw_data prw; 1965e8b4d56eSNate Lawson ACPI_HANDLE handle; 1966cc85c78cSNate Lawson int flags; 1967e8b4d56eSNate Lawson 1968e8b4d56eSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 1969cc85c78cSNate Lawson flags = device_get_flags(dev); 1970e8b4d56eSNate Lawson handle = acpi_get_handle(dev); 1971cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 1972e8b4d56eSNate Lawson return (ENXIO); 1973cc85c78cSNate Lawson 1974cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 1975e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 1976e8b4d56eSNate Lawson return (ENXIO); 1977e8b4d56eSNate Lawson 1978e8b4d56eSNate Lawson /* 1979cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 1980cc85c78cSNate Lawson * of the _PRW object are put into the ON state. 1981e8b4d56eSNate Lawson */ 1982cc85c78cSNate Lawson 1983cc85c78cSNate Lawson /* 1984cc85c78cSNate Lawson * If the user requested that this device wake the system and the next 1985cc85c78cSNate Lawson * sleep state is valid for this GPE, enable it and the device's wake 1986cc85c78cSNate Lawson * capability. The sleep state must be less than (i.e., higher power) 1987cc85c78cSNate Lawson * or equal to the value specified by _PRW. Return early, leaving 1988cc85c78cSNate Lawson * the appropriate power resources enabled. 1989cc85c78cSNate Lawson */ 1990cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0 && 1991cc85c78cSNate Lawson sstate <= prw.lowest_wake) { 1992e8b4d56eSNate Lawson if (bootverbose) 1993cc85c78cSNate Lawson device_printf(dev, "wake_prep enabled gpe %#x for state %d\n", 1994e8b4d56eSNate Lawson prw.gpe_bit, sstate); 1995cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 1996cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 1); 1997e8b4d56eSNate Lawson return (0); 1998e8b4d56eSNate Lawson } 1999e8b4d56eSNate Lawson 2000e8b4d56eSNate Lawson /* 2001cc85c78cSNate Lawson * If the device wake was disabled or this sleep state is too low for 2002cc85c78cSNate Lawson * this device, disable its wake capability and GPE. 2003e8b4d56eSNate Lawson */ 2004cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2005cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 2006e8b4d56eSNate Lawson if (bootverbose) 2007cc85c78cSNate Lawson device_printf(dev, "wake_prep disabled gpe %#x for state %d\n", 2008cc85c78cSNate Lawson prw.gpe_bit, sstate); 2009cc85c78cSNate Lawson 2010cc85c78cSNate Lawson /* 2011cc85c78cSNate Lawson * TBD: All Power Resources referenced by elements 2 through N 2012cc85c78cSNate Lawson * of the _PRW object are put into the OFF state. 2013cc85c78cSNate Lawson */ 2014cc85c78cSNate Lawson 2015cc85c78cSNate Lawson return (0); 2016e8b4d56eSNate Lawson } 2017e8b4d56eSNate Lawson 2018cc85c78cSNate Lawson /* Re-enable GPEs after wake. */ 2019cc85c78cSNate Lawson int 2020cc85c78cSNate Lawson acpi_wake_run_prep(device_t dev) 2021cc85c78cSNate Lawson { 2022cc85c78cSNate Lawson struct acpi_prw_data prw; 2023cc85c78cSNate Lawson ACPI_HANDLE handle; 2024cc85c78cSNate Lawson int flags; 2025cc85c78cSNate Lawson 2026cc85c78cSNate Lawson /* Check that this is an ACPI device and get its GPE. */ 2027cc85c78cSNate Lawson flags = device_get_flags(dev); 2028cc85c78cSNate Lawson handle = acpi_get_handle(dev); 2029cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_CAPABLE) == 0 || handle == NULL) 2030cc85c78cSNate Lawson return (ENXIO); 2031cc85c78cSNate Lawson 2032cc85c78cSNate Lawson /* Evaluate _PRW to find the GPE. */ 2033cc85c78cSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2034cc85c78cSNate Lawson return (ENXIO); 2035cc85c78cSNate Lawson 2036cc85c78cSNate Lawson /* 2037cc85c78cSNate Lawson * TBD: Be sure all Power Resources referenced by elements 2 through N 2038cc85c78cSNate Lawson * of the _PRW object are in the ON state. 2039cc85c78cSNate Lawson */ 2040cc85c78cSNate Lawson 2041cc85c78cSNate Lawson /* Disable wake capability and if the user requested, enable the GPE. */ 2042cc85c78cSNate Lawson acpi_SetInteger(handle, "_PSW", 0); 2043cc85c78cSNate Lawson if ((flags & ACPI_FLAG_WAKE_ENABLED) != 0) 2044cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2045e8b4d56eSNate Lawson return (0); 2046e8b4d56eSNate Lawson } 2047e8b4d56eSNate Lawson 2048e8b4d56eSNate Lawson static ACPI_STATUS 2049e8b4d56eSNate Lawson acpi_wake_limit(ACPI_HANDLE h, UINT32 level, void *context, void **status) 2050e8b4d56eSNate Lawson { 2051e8b4d56eSNate Lawson struct acpi_prw_data prw; 205244b8ae71SNate Lawson int *sstate; 2053e8b4d56eSNate Lawson 2054e8b4d56eSNate Lawson /* It's ok not to have _PRW if the device can't wake the system. */ 2055e8b4d56eSNate Lawson if (acpi_parse_prw(h, &prw) != 0) 2056e8b4d56eSNate Lawson return (AE_OK); 2057e8b4d56eSNate Lawson 205844b8ae71SNate Lawson sstate = (int *)context; 205944b8ae71SNate Lawson if (*sstate > prw.lowest_wake) 2060e8b4d56eSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2061e8b4d56eSNate Lawson 2062e8b4d56eSNate Lawson return (AE_OK); 2063e8b4d56eSNate Lawson } 2064e8b4d56eSNate Lawson 2065e8b4d56eSNate Lawson /* Walk all system devices, disabling them if necessary for sstate. */ 2066e8b4d56eSNate Lawson static int 2067e8b4d56eSNate Lawson acpi_wake_limit_walk(int sstate) 2068e8b4d56eSNate Lawson { 2069e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 2070e8b4d56eSNate Lawson 2071e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2072e8b4d56eSNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, sb_handle, 100, 207344b8ae71SNate Lawson acpi_wake_limit, &sstate, NULL); 2074e8b4d56eSNate Lawson return (0); 2075e8b4d56eSNate Lawson } 2076e8b4d56eSNate Lawson 207788a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 207888a79fc0SNate Lawson static int 207988a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 208088a79fc0SNate Lawson { 208188a79fc0SNate Lawson int error, i, numdevs; 208288a79fc0SNate Lawson device_t *devlist; 208388a79fc0SNate Lawson device_t child; 208488a79fc0SNate Lawson 208588a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 208688a79fc0SNate Lawson if (error != 0 || numdevs == 0) 208788a79fc0SNate Lawson return (error); 208888a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 208988a79fc0SNate Lawson child = devlist[i]; 209088a79fc0SNate Lawson if (!device_is_attached(child)) 209188a79fc0SNate Lawson continue; 209288a79fc0SNate Lawson if (device_get_flags(child) & ACPI_FLAG_WAKE_CAPABLE) { 209388a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 209488a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 209588a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 209688a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 209788a79fc0SNate Lawson } 209888a79fc0SNate Lawson acpi_wake_sysctl_walk(child); 209988a79fc0SNate Lawson } 210088a79fc0SNate Lawson free(devlist, M_TEMP); 210188a79fc0SNate Lawson 210288a79fc0SNate Lawson return (0); 210388a79fc0SNate Lawson } 210488a79fc0SNate Lawson 210588a79fc0SNate Lawson /* Enable or disable wake from userland. */ 210688a79fc0SNate Lawson static int 210788a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 210888a79fc0SNate Lawson { 210988a79fc0SNate Lawson int enable, error; 211088a79fc0SNate Lawson device_t dev; 211188a79fc0SNate Lawson 211288a79fc0SNate Lawson dev = (device_t)arg1; 211388a79fc0SNate Lawson enable = (device_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 211488a79fc0SNate Lawson 211588a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 211688a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 211788a79fc0SNate Lawson return (error); 211888a79fc0SNate Lawson if (enable != 0 && enable != 1) 211988a79fc0SNate Lawson return (EINVAL); 212088a79fc0SNate Lawson 212188a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 212288a79fc0SNate Lawson } 212388a79fc0SNate Lawson 2124e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2125e8b4d56eSNate Lawson static int 2126e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2127e8b4d56eSNate Lawson { 2128e8b4d56eSNate Lawson ACPI_STATUS status; 2129e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2130e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2131e8b4d56eSNate Lawson int error; 2132e8b4d56eSNate Lawson 2133e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2134e8b4d56eSNate Lawson return (EINVAL); 2135e8b4d56eSNate Lawson 2136e8b4d56eSNate Lawson /* 2137e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2138e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2139e8b4d56eSNate Lawson */ 2140e8b4d56eSNate Lawson error = EINVAL; 2141e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2142e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2143e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2144e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2145e8b4d56eSNate Lawson return (ENOENT); 2146e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2147e8b4d56eSNate Lawson if (res == NULL) 2148e8b4d56eSNate Lawson return (ENOENT); 2149e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2150e8b4d56eSNate Lawson goto out; 2151e8b4d56eSNate Lawson 2152e8b4d56eSNate Lawson /* 2153e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2154e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2155e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2156e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2157e8b4d56eSNate Lawson */ 2158e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2159e8b4d56eSNate Lawson goto out; 2160e8b4d56eSNate Lawson 2161e8b4d56eSNate Lawson /* 2162e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2163e8b4d56eSNate Lawson */ 2164e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2165e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2166e8b4d56eSNate Lawson /* 2167e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2168e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2169e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2170e8b4d56eSNate Lawson * enabled for the wake event. 2171e8b4d56eSNate Lawson */ 2172e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2173e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2174e8b4d56eSNate Lawson error = 0; 2175e8b4d56eSNate Lawson break; 2176e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2177e8b4d56eSNate Lawson /* 2178e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2179e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2180e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2181e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2182e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2183e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2184e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2185e8b4d56eSNate Lawson * the wake event. 2186e8b4d56eSNate Lawson * 2187e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2188e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2189e8b4d56eSNate Lawson */ 2190e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2191e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2192e8b4d56eSNate Lawson goto out; 2193e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2194e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2195e8b4d56eSNate Lawson goto out; 2196e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2197e8b4d56eSNate Lawson goto out; 2198e8b4d56eSNate Lawson error = 0; 2199e8b4d56eSNate Lawson break; 2200e8b4d56eSNate Lawson default: 2201e8b4d56eSNate Lawson goto out; 2202e8b4d56eSNate Lawson } 2203e8b4d56eSNate Lawson 2204e8b4d56eSNate Lawson /* XXX No power resource handling yet. */ 2205e8b4d56eSNate Lawson prw->power_res = NULL; 2206e8b4d56eSNate Lawson 2207e8b4d56eSNate Lawson out: 2208e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2209e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2210e8b4d56eSNate Lawson return (error); 2211e8b4d56eSNate Lawson } 2212e8b4d56eSNate Lawson 221315e32d5dSMike Smith /* 221415e32d5dSMike Smith * Enable/Disable ACPI 221515e32d5dSMike Smith */ 221615e32d5dSMike Smith ACPI_STATUS 221715e32d5dSMike Smith acpi_Enable(struct acpi_softc *sc) 221815e32d5dSMike Smith { 221915e32d5dSMike Smith ACPI_STATUS status; 222015e32d5dSMike Smith u_int32_t flags; 222115e32d5dSMike Smith 2222b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2223cb9b0d80SMike Smith ACPI_ASSERTLOCK; 22240ae55423SMike Smith 222515e32d5dSMike Smith flags = ACPI_NO_ADDRESS_SPACE_INIT | ACPI_NO_HARDWARE_INIT | 222615e32d5dSMike Smith ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 2227be2b1797SNate Lawson if (!sc->acpi_enabled) 222815e32d5dSMike Smith status = AcpiEnableSubsystem(flags); 2229be2b1797SNate Lawson else 223015e32d5dSMike Smith status = AE_OK; 2231be2b1797SNate Lawson 223215e32d5dSMike Smith if (status == AE_OK) 223315e32d5dSMike Smith sc->acpi_enabled = 1; 2234be2b1797SNate Lawson 22350ae55423SMike Smith return_ACPI_STATUS (status); 223615e32d5dSMike Smith } 223715e32d5dSMike Smith 223815e32d5dSMike Smith ACPI_STATUS 223915e32d5dSMike Smith acpi_Disable(struct acpi_softc *sc) 224015e32d5dSMike Smith { 224115e32d5dSMike Smith ACPI_STATUS status; 224215e32d5dSMike Smith 2243b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2244cb9b0d80SMike Smith ACPI_ASSERTLOCK; 22450ae55423SMike Smith 2246be2b1797SNate Lawson if (sc->acpi_enabled) 224715e32d5dSMike Smith status = AcpiDisable(); 2248be2b1797SNate Lawson else 224915e32d5dSMike Smith status = AE_OK; 2250be2b1797SNate Lawson 225115e32d5dSMike Smith if (status == AE_OK) 225215e32d5dSMike Smith sc->acpi_enabled = 0; 2253be2b1797SNate Lawson 22540ae55423SMike Smith return_ACPI_STATUS (status); 225515e32d5dSMike Smith } 225615e32d5dSMike Smith 225715e32d5dSMike Smith /* 225815e32d5dSMike Smith * ACPI Event Handlers 225915e32d5dSMike Smith */ 226015e32d5dSMike Smith 226115e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 226215e32d5dSMike Smith 226315e32d5dSMike Smith static void 226415e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 226515e32d5dSMike Smith { 2266fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2267b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 226815e32d5dSMike Smith 2269cb9b0d80SMike Smith ACPI_LOCK; 2270a5d1879bSMitsuru IWASAKI if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 227115e32d5dSMike Smith acpi_SetSleepState((struct acpi_softc *)arg, state); 2272cb9b0d80SMike Smith ACPI_UNLOCK; 22730ae55423SMike Smith return_VOID; 227415e32d5dSMike Smith } 227515e32d5dSMike Smith 227615e32d5dSMike Smith static void 227715e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 227815e32d5dSMike Smith { 2279fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 2280b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 22810ae55423SMike Smith 228215e32d5dSMike Smith /* Well, what to do? :-) */ 22830ae55423SMike Smith 2284cb9b0d80SMike Smith ACPI_LOCK; 2285cb9b0d80SMike Smith ACPI_UNLOCK; 2286cb9b0d80SMike Smith 22870ae55423SMike Smith return_VOID; 228815e32d5dSMike Smith } 228915e32d5dSMike Smith 229015e32d5dSMike Smith /* 229115e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 229215e32d5dSMike Smith */ 229315e32d5dSMike Smith UINT32 229433febf93SNate Lawson acpi_event_power_button_sleep(void *context) 229515e32d5dSMike Smith { 229615e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 229715e32d5dSMike Smith 2298b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 22990ae55423SMike Smith 230015e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 23010ae55423SMike Smith 2302b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 230315e32d5dSMike Smith } 230415e32d5dSMike Smith 230515e32d5dSMike Smith UINT32 230633febf93SNate Lawson acpi_event_power_button_wake(void *context) 230715e32d5dSMike Smith { 230815e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 230915e32d5dSMike Smith 2310b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 23110ae55423SMike Smith 231215e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 23130ae55423SMike Smith 2314b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 231515e32d5dSMike Smith } 231615e32d5dSMike Smith 231715e32d5dSMike Smith UINT32 231833febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 231915e32d5dSMike Smith { 232015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 232115e32d5dSMike Smith 2322b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 23230ae55423SMike Smith 232415e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 23250ae55423SMike Smith 2326b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 232715e32d5dSMike Smith } 232815e32d5dSMike Smith 232915e32d5dSMike Smith UINT32 233033febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 233115e32d5dSMike Smith { 233215e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 233315e32d5dSMike Smith 2334b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 23350ae55423SMike Smith 233615e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 23370ae55423SMike Smith 2338b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 233915e32d5dSMike Smith } 234015e32d5dSMike Smith 234115e32d5dSMike Smith /* 234215e32d5dSMike Smith * XXX This is kinda ugly, and should not be here. 234315e32d5dSMike Smith */ 234415e32d5dSMike Smith struct acpi_staticbuf { 234515e32d5dSMike Smith ACPI_BUFFER buffer; 234615e32d5dSMike Smith char data[512]; 234715e32d5dSMike Smith }; 234815e32d5dSMike Smith 234915e32d5dSMike Smith char * 235015e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 235115e32d5dSMike Smith { 235215e32d5dSMike Smith static struct acpi_staticbuf buf; 235315e32d5dSMike Smith 2354cb9b0d80SMike Smith ACPI_ASSERTLOCK; 2355cb9b0d80SMike Smith 235615e32d5dSMike Smith buf.buffer.Length = 512; 235715e32d5dSMike Smith buf.buffer.Pointer = &buf.data[0]; 235815e32d5dSMike Smith 2359b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf.buffer))) 236015e32d5dSMike Smith return (buf.buffer.Pointer); 2361be2b1797SNate Lawson 236215e32d5dSMike Smith return ("(unknown path)"); 236315e32d5dSMike Smith } 236415e32d5dSMike Smith 236515e32d5dSMike Smith /* 236615e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 236715e32d5dSMike Smith * parts of the namespace. 236815e32d5dSMike Smith */ 236915e32d5dSMike Smith int 237015e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 237115e32d5dSMike Smith { 23723c600cfbSJohn Baldwin char *cp, *env, *np; 237315e32d5dSMike Smith int len; 237415e32d5dSMike Smith 237515e32d5dSMike Smith np = acpi_name(handle); 237615e32d5dSMike Smith if (*np == '\\') 237715e32d5dSMike Smith np++; 23783c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 237915e32d5dSMike Smith return (0); 238015e32d5dSMike Smith 2381be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 23823c600cfbSJohn Baldwin cp = env; 238315e32d5dSMike Smith for (;;) { 238415e32d5dSMike Smith while ((*cp != 0) && isspace(*cp)) 238515e32d5dSMike Smith cp++; 238615e32d5dSMike Smith if (*cp == 0) 238715e32d5dSMike Smith break; 238815e32d5dSMike Smith len = 0; 238915e32d5dSMike Smith while ((cp[len] != 0) && !isspace(cp[len])) 239015e32d5dSMike Smith len++; 2391d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 23923c600cfbSJohn Baldwin freeenv(env); 23930ae55423SMike Smith return(1); 2394d786139cSMaxime Henrion } 23950ae55423SMike Smith cp += len; 23960ae55423SMike Smith } 23973c600cfbSJohn Baldwin freeenv(env); 2398be2b1797SNate Lawson 23990ae55423SMike Smith return (0); 24000ae55423SMike Smith } 24010ae55423SMike Smith 24020ae55423SMike Smith /* 24030ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 24040ae55423SMike Smith */ 24050ae55423SMike Smith int 24060ae55423SMike Smith acpi_disabled(char *subsys) 24070ae55423SMike Smith { 240878689b15SMaxime Henrion char *cp, *env; 24090ae55423SMike Smith int len; 24100ae55423SMike Smith 24113184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 24120ae55423SMike Smith return (0); 24133184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 241478689b15SMaxime Henrion freeenv(env); 24150ae55423SMike Smith return (1); 2416d786139cSMaxime Henrion } 24170ae55423SMike Smith 24183184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 241978689b15SMaxime Henrion cp = env; 24200ae55423SMike Smith for (;;) { 24213184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 24220ae55423SMike Smith cp++; 24233184cf5aSNate Lawson if (*cp == '\0') 24240ae55423SMike Smith break; 24250ae55423SMike Smith len = 0; 24263184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 24270ae55423SMike Smith len++; 24283184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 242978689b15SMaxime Henrion freeenv(env); 243015e32d5dSMike Smith return (1); 2431d786139cSMaxime Henrion } 243215e32d5dSMike Smith cp += len; 243315e32d5dSMike Smith } 243478689b15SMaxime Henrion freeenv(env); 2435be2b1797SNate Lawson 243615e32d5dSMike Smith return (0); 243715e32d5dSMike Smith } 243815e32d5dSMike Smith 243915e32d5dSMike Smith /* 244015e32d5dSMike Smith * Control interface. 244115e32d5dSMike Smith * 24420ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 2443be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 2444be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 244515e32d5dSMike Smith */ 24460ae55423SMike Smith struct acpi_ioctl_hook 24470ae55423SMike Smith { 24480ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 24490ae55423SMike Smith u_long cmd; 2450be2b1797SNate Lawson acpi_ioctl_fn fn; 24510ae55423SMike Smith void *arg; 24520ae55423SMike Smith }; 24530ae55423SMike Smith 24540ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 24550ae55423SMike Smith static int acpi_ioctl_hooks_initted; 24560ae55423SMike Smith 24570ae55423SMike Smith /* 24580ae55423SMike Smith * Register an ioctl handler. 24590ae55423SMike Smith */ 24600ae55423SMike Smith int 2461be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 24620ae55423SMike Smith { 24630ae55423SMike Smith struct acpi_ioctl_hook *hp; 24640ae55423SMike Smith 24650ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 24660ae55423SMike Smith return (ENOMEM); 24670ae55423SMike Smith hp->cmd = cmd; 24680ae55423SMike Smith hp->fn = fn; 24690ae55423SMike Smith hp->arg = arg; 24700ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 24710ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 24720ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 24730ae55423SMike Smith } 24740ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 24750ae55423SMike Smith return (0); 24760ae55423SMike Smith } 24770ae55423SMike Smith 24780ae55423SMike Smith /* 24790ae55423SMike Smith * Deregister an ioctl handler. 24800ae55423SMike Smith */ 24810ae55423SMike Smith void 2482be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 24830ae55423SMike Smith { 24840ae55423SMike Smith struct acpi_ioctl_hook *hp; 24850ae55423SMike Smith 24860ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 24870ae55423SMike Smith if ((hp->cmd == cmd) && (hp->fn == fn)) 24880ae55423SMike Smith break; 24890ae55423SMike Smith 24900ae55423SMike Smith if (hp != NULL) { 24910ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 24920ae55423SMike Smith free(hp, M_ACPIDEV); 24930ae55423SMike Smith } 24940ae55423SMike Smith } 24950ae55423SMike Smith 249615e32d5dSMike Smith static int 249789c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 249815e32d5dSMike Smith { 249915e32d5dSMike Smith return (0); 250015e32d5dSMike Smith } 250115e32d5dSMike Smith 250215e32d5dSMike Smith static int 250389c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 250415e32d5dSMike Smith { 250515e32d5dSMike Smith return (0); 250615e32d5dSMike Smith } 250715e32d5dSMike Smith 250815e32d5dSMike Smith static int 250989c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 251015e32d5dSMike Smith { 251115e32d5dSMike Smith struct acpi_softc *sc; 25120ae55423SMike Smith struct acpi_ioctl_hook *hp; 25130ae55423SMike Smith int error, xerror, state; 2514fc0ea94aSJohn Baldwin ACPI_LOCK_DECL; 251515e32d5dSMike Smith 2516cb9b0d80SMike Smith ACPI_LOCK; 2517cb9b0d80SMike Smith 251815e32d5dSMike Smith error = state = 0; 251915e32d5dSMike Smith sc = dev->si_drv1; 252015e32d5dSMike Smith 25210ae55423SMike Smith /* 25220ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 25230ae55423SMike Smith */ 25240ae55423SMike Smith if (acpi_ioctl_hooks_initted) { 25250ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 25260ae55423SMike Smith if (hp->cmd == cmd) { 25270ae55423SMike Smith xerror = hp->fn(cmd, addr, hp->arg); 25280ae55423SMike Smith if (xerror != 0) 25290ae55423SMike Smith error = xerror; 2530917d44c8SMitsuru IWASAKI goto out; 25310ae55423SMike Smith } 25320ae55423SMike Smith } 25330ae55423SMike Smith } 25340ae55423SMike Smith 25350ae55423SMike Smith /* 2536a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 2537a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 2538a89fcf28STakanori Watanabe * Not changing system behavior. 2539a89fcf28STakanori Watanabe */ 2540be2b1797SNate Lawson if ((flag & FWRITE) == 0) 2541be2b1797SNate Lawson return (EPERM); 2542a89fcf28STakanori Watanabe 2543be2b1797SNate Lawson /* Core system ioctls. */ 254415e32d5dSMike Smith switch (cmd) { 254515e32d5dSMike Smith case ACPIIO_ENABLE: 25460ae55423SMike Smith if (ACPI_FAILURE(acpi_Enable(sc))) 254715e32d5dSMike Smith error = ENXIO; 254815e32d5dSMike Smith break; 254915e32d5dSMike Smith case ACPIIO_DISABLE: 25500ae55423SMike Smith if (ACPI_FAILURE(acpi_Disable(sc))) 255115e32d5dSMike Smith error = ENXIO; 255215e32d5dSMike Smith break; 255315e32d5dSMike Smith case ACPIIO_SETSLPSTATE: 255415e32d5dSMike Smith if (!sc->acpi_enabled) { 255515e32d5dSMike Smith error = ENXIO; 255615e32d5dSMike Smith break; 255715e32d5dSMike Smith } 255815e32d5dSMike Smith state = *(int *)addr; 25592d610c46SNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) { 25602d610c46SNate Lawson if (ACPI_FAILURE(acpi_SetSleepState(sc, state))) 256115e32d5dSMike Smith error = EINVAL; 25622d610c46SNate Lawson } else { 25632d610c46SNate Lawson error = EINVAL; 25642d610c46SNate Lawson } 256515e32d5dSMike Smith break; 256615e32d5dSMike Smith default: 25670ae55423SMike Smith if (error == 0) 256815e32d5dSMike Smith error = EINVAL; 256915e32d5dSMike Smith break; 257015e32d5dSMike Smith } 2571917d44c8SMitsuru IWASAKI 2572917d44c8SMitsuru IWASAKI out: 2573cb9b0d80SMike Smith ACPI_UNLOCK; 257415e32d5dSMike Smith return (error); 257515e32d5dSMike Smith } 257615e32d5dSMike Smith 25771d073b1dSJohn Baldwin static int 2578d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 2579d75de536SMitsuru IWASAKI { 2580d75de536SMitsuru IWASAKI char sleep_state[4]; 2581d75de536SMitsuru IWASAKI char buf[16]; 2582d75de536SMitsuru IWASAKI int error; 2583d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 2584d75de536SMitsuru IWASAKI 2585d75de536SMitsuru IWASAKI buf[0] = '\0'; 2586d75de536SMitsuru IWASAKI for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) { 2587d75de536SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 2588d75de536SMitsuru IWASAKI sprintf(sleep_state, "S%d ", state); 2589d75de536SMitsuru IWASAKI strcat(buf, sleep_state); 2590d75de536SMitsuru IWASAKI } 2591d75de536SMitsuru IWASAKI } 2592d75de536SMitsuru IWASAKI error = sysctl_handle_string(oidp, buf, sizeof(buf), req); 2593d75de536SMitsuru IWASAKI return (error); 2594d75de536SMitsuru IWASAKI } 2595d75de536SMitsuru IWASAKI 2596d75de536SMitsuru IWASAKI static int 25971d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 25981d073b1dSJohn Baldwin { 25991d073b1dSJohn Baldwin char sleep_state[10]; 26001d073b1dSJohn Baldwin int error; 26011d073b1dSJohn Baldwin u_int new_state, old_state; 26021d073b1dSJohn Baldwin 26031d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 2604b8670bedSMitsuru IWASAKI if (old_state > ACPI_S_STATES_MAX + 1) { 26051d073b1dSJohn Baldwin strcpy(sleep_state, "unknown"); 2606cb9b0d80SMike Smith } else { 2607b8670bedSMitsuru IWASAKI bzero(sleep_state, sizeof(sleep_state)); 26081d073b1dSJohn Baldwin strncpy(sleep_state, sleep_state_names[old_state], 26091d073b1dSJohn Baldwin sizeof(sleep_state_names[old_state])); 2610cb9b0d80SMike Smith } 26111d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 26121d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 2613be2b1797SNate Lawson new_state = ACPI_STATE_S0; 2614be2b1797SNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) { 26151d073b1dSJohn Baldwin if (strncmp(sleep_state, sleep_state_names[new_state], 26161d073b1dSJohn Baldwin sizeof(sleep_state)) == 0) 26171d073b1dSJohn Baldwin break; 2618cb9b0d80SMike Smith } 2619931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 2620be2b1797SNate Lawson if (new_state != old_state) 26211d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 2622cb9b0d80SMike Smith } else { 26231d073b1dSJohn Baldwin error = EINVAL; 26241d073b1dSJohn Baldwin } 2625cb9b0d80SMike Smith } 2626be2b1797SNate Lawson 26271d073b1dSJohn Baldwin return (error); 26281d073b1dSJohn Baldwin } 26291d073b1dSJohn Baldwin 26309b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 26319b937d48SNate Lawson void 26329b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 26339b937d48SNate Lawson { 26349b937d48SNate Lawson char notify_buf[16]; 26359b937d48SNate Lawson ACPI_BUFFER handle_buf; 26369b937d48SNate Lawson ACPI_STATUS status; 26379b937d48SNate Lawson 26389b937d48SNate Lawson if (subsystem == NULL) 26399b937d48SNate Lawson return; 26409b937d48SNate Lawson 26419b937d48SNate Lawson handle_buf.Pointer = NULL; 26429b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 26439b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 26449b937d48SNate Lawson if (ACPI_FAILURE(status)) 26459b937d48SNate Lawson return; 26469b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 26479b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 26489b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 26499b937d48SNate Lawson } 26509b937d48SNate Lawson 265115e32d5dSMike Smith #ifdef ACPI_DEBUG 26520ae55423SMike Smith /* 26530ae55423SMike Smith * Support for parsing debug options from the kernel environment. 26540ae55423SMike Smith * 26550ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 26560ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 26570ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 26580ae55423SMike Smith * prefixing the bit name with !. 26590ae55423SMike Smith */ 266015e32d5dSMike Smith struct debugtag 266115e32d5dSMike Smith { 266215e32d5dSMike Smith char *name; 266315e32d5dSMike Smith UINT32 value; 266415e32d5dSMike Smith }; 266515e32d5dSMike Smith 266615e32d5dSMike Smith static struct debugtag dbg_layer[] = { 2667c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 2668c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 2669c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 2670c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 2671c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 2672c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 2673c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 2674c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 2675c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 2676d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 26774c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 2678d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 2679297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 26804c1cdee6SMike Smith 2681c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 2682c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 26833184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 2684c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 26853184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 26863184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 26873184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 26884c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 2689cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 26903184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 2691b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 269215e32d5dSMike Smith {NULL, 0} 269315e32d5dSMike Smith }; 269415e32d5dSMike Smith 269515e32d5dSMike Smith static struct debugtag dbg_level[] = { 26964c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 26979501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 26989501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 26994c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 27009501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 27014c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 2702d62ab2f4SMitsuru IWASAKI 2703d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 2704297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 27054c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 27064c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 2707d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 27084c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 27094c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 27104c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 27114c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 27124c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 27134c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 27144c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 27154c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 27164c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 27174c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 2718d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 2719d62ab2f4SMitsuru IWASAKI 2720d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 2721d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 2722d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 2723d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 2724d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 27254c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 2726d62ab2f4SMitsuru IWASAKI 2727d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 2728d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 2729d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 2730d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 2731d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 2732d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 2733d62ab2f4SMitsuru IWASAKI 2734d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 273598479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 273698479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 273798479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 273898479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 273998479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 274015e32d5dSMike Smith {NULL, 0} 274115e32d5dSMike Smith }; 274215e32d5dSMike Smith 274315e32d5dSMike Smith static void 274415e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 274515e32d5dSMike Smith { 274615e32d5dSMike Smith char *ep; 274715e32d5dSMike Smith int i, l; 27480ae55423SMike Smith int set; 274915e32d5dSMike Smith 275015e32d5dSMike Smith while (*cp) { 275115e32d5dSMike Smith if (isspace(*cp)) { 275215e32d5dSMike Smith cp++; 275315e32d5dSMike Smith continue; 275415e32d5dSMike Smith } 275515e32d5dSMike Smith ep = cp; 275615e32d5dSMike Smith while (*ep && !isspace(*ep)) 275715e32d5dSMike Smith ep++; 27580ae55423SMike Smith if (*cp == '!') { 27590ae55423SMike Smith set = 0; 27600ae55423SMike Smith cp++; 27610ae55423SMike Smith if (cp == ep) 27620ae55423SMike Smith continue; 27630ae55423SMike Smith } else { 27640ae55423SMike Smith set = 1; 27650ae55423SMike Smith } 276615e32d5dSMike Smith l = ep - cp; 276715e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 276815e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 2769be2b1797SNate Lawson if (set) 277015e32d5dSMike Smith *flag |= tag[i].value; 2771be2b1797SNate Lawson else 27720ae55423SMike Smith *flag &= ~tag[i].value; 277315e32d5dSMike Smith } 277415e32d5dSMike Smith } 277515e32d5dSMike Smith cp = ep; 277615e32d5dSMike Smith } 277715e32d5dSMike Smith } 277815e32d5dSMike Smith 277915e32d5dSMike Smith static void 27802a4ac806SMike Smith acpi_set_debugging(void *junk) 278115e32d5dSMike Smith { 27827e639165SNate Lawson char *layer, *level; 278315e32d5dSMike Smith 27841d7b121cSNate Lawson if (cold) { 27850ae55423SMike Smith AcpiDbgLayer = 0; 27860ae55423SMike Smith AcpiDbgLevel = 0; 27871d7b121cSNate Lawson } 27881d7b121cSNate Lawson 27897e639165SNate Lawson layer = getenv("debug.acpi.layer"); 27907e639165SNate Lawson level = getenv("debug.acpi.level"); 27917e639165SNate Lawson if (layer == NULL && level == NULL) 27927e639165SNate Lawson return; 27930ae55423SMike Smith 27947e639165SNate Lawson printf("ACPI set debug"); 27957e639165SNate Lawson if (layer != NULL) { 27967e639165SNate Lawson if (strcmp("NONE", layer) != 0) 27977e639165SNate Lawson printf(" layer '%s'", layer); 27987e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 27997e639165SNate Lawson freeenv(layer); 28001d7b121cSNate Lawson } 28017e639165SNate Lawson if (level != NULL) { 28027e639165SNate Lawson if (strcmp("NONE", level) != 0) 28037e639165SNate Lawson printf(" level '%s'", level); 28047e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 28057e639165SNate Lawson freeenv(level); 28067e639165SNate Lawson } 28077e639165SNate Lawson printf("\n"); 280815e32d5dSMike Smith } 2809be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 2810be2b1797SNate Lawson NULL); 28111d7b121cSNate Lawson 28121d7b121cSNate Lawson static int 28131d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 28141d7b121cSNate Lawson { 281554f6bca0SNate Lawson int error, *dbg; 28161d7b121cSNate Lawson struct debugtag *tag; 281754f6bca0SNate Lawson struct sbuf sb; 28181d7b121cSNate Lawson 281954f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 282054f6bca0SNate Lawson return (ENOMEM); 28211d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 28221d7b121cSNate Lawson tag = &dbg_layer[0]; 28231d7b121cSNate Lawson dbg = &AcpiDbgLayer; 28241d7b121cSNate Lawson } else { 28251d7b121cSNate Lawson tag = &dbg_level[0]; 28261d7b121cSNate Lawson dbg = &AcpiDbgLevel; 28271d7b121cSNate Lawson } 28281d7b121cSNate Lawson 28291d7b121cSNate Lawson /* Get old values if this is a get request. */ 28301d7b121cSNate Lawson if (*dbg == 0) { 283154f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 28321d7b121cSNate Lawson } else if (req->newptr == NULL) { 28331d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 283454f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 283554f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 28361d7b121cSNate Lawson } 28371d7b121cSNate Lawson } 283854f6bca0SNate Lawson sbuf_trim(&sb); 283954f6bca0SNate Lawson sbuf_finish(&sb); 28401d7b121cSNate Lawson 28417e639165SNate Lawson /* Copy out the old values to the user. */ 28427e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 284354f6bca0SNate Lawson sbuf_delete(&sb); 28441d7b121cSNate Lawson 28451d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 28461d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 28471d7b121cSNate Lawson *dbg = 0; 28481d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 28491d7b121cSNate Lawson acpi_set_debugging(NULL); 28501d7b121cSNate Lawson } 28511d7b121cSNate Lawson 28521d7b121cSNate Lawson return (error); 28531d7b121cSNate Lawson } 28541d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 28551d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 28561d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 28571d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 285815e32d5dSMike Smith #endif 2859f9390180SMitsuru IWASAKI 2860f9390180SMitsuru IWASAKI static int 2861f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 2862f9390180SMitsuru IWASAKI { 2863f9390180SMitsuru IWASAKI int state, acpi_state; 2864f9390180SMitsuru IWASAKI int error; 2865f9390180SMitsuru IWASAKI struct acpi_softc *sc; 2866f9390180SMitsuru IWASAKI va_list ap; 2867f9390180SMitsuru IWASAKI 2868f9390180SMitsuru IWASAKI error = 0; 2869f9390180SMitsuru IWASAKI switch (cmd) { 2870f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 2871f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 2872f9390180SMitsuru IWASAKI if (sc == NULL) { 2873f9390180SMitsuru IWASAKI error = EINVAL; 2874f9390180SMitsuru IWASAKI goto out; 2875f9390180SMitsuru IWASAKI } 2876f9390180SMitsuru IWASAKI 2877f9390180SMitsuru IWASAKI va_start(ap, arg); 2878f9390180SMitsuru IWASAKI state = va_arg(ap, int); 2879f9390180SMitsuru IWASAKI va_end(ap); 2880f9390180SMitsuru IWASAKI 2881f9390180SMitsuru IWASAKI switch (state) { 2882f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 2883f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 2884f9390180SMitsuru IWASAKI break; 2885f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 2886f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 2887f9390180SMitsuru IWASAKI break; 2888f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 2889f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 2890f9390180SMitsuru IWASAKI break; 2891f9390180SMitsuru IWASAKI default: 2892f9390180SMitsuru IWASAKI error = EINVAL; 2893f9390180SMitsuru IWASAKI goto out; 2894f9390180SMitsuru IWASAKI } 2895f9390180SMitsuru IWASAKI 2896f9390180SMitsuru IWASAKI acpi_SetSleepState(sc, acpi_state); 2897f9390180SMitsuru IWASAKI break; 2898f9390180SMitsuru IWASAKI default: 2899f9390180SMitsuru IWASAKI error = EINVAL; 2900f9390180SMitsuru IWASAKI goto out; 2901f9390180SMitsuru IWASAKI } 2902f9390180SMitsuru IWASAKI 2903f9390180SMitsuru IWASAKI out: 2904f9390180SMitsuru IWASAKI return (error); 2905f9390180SMitsuru IWASAKI } 2906f9390180SMitsuru IWASAKI 2907f9390180SMitsuru IWASAKI static void 2908f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 2909f9390180SMitsuru IWASAKI { 2910be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 29116c407052SMitsuru IWASAKI return; 2912f9390180SMitsuru IWASAKI 2913f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 2914f9390180SMitsuru IWASAKI } 2915f9390180SMitsuru IWASAKI 2916f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 2917