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 30dad97feeSDavid E. O'Brien #include <sys/cdefs.h> 31dad97feeSDavid E. O'Brien __FBSDID("$FreeBSD$"); 32dad97feeSDavid E. O'Brien 3315e32d5dSMike Smith #include "opt_acpi.h" 3415e32d5dSMike Smith #include <sys/param.h> 3515e32d5dSMike Smith #include <sys/kernel.h> 36fb919e4dSMark Murray #include <sys/proc.h> 37a89fcf28STakanori Watanabe #include <sys/fcntl.h> 3815e32d5dSMike Smith #include <sys/malloc.h> 39fe12f24bSPoul-Henning Kamp #include <sys/module.h> 4015e32d5dSMike Smith #include <sys/bus.h> 4115e32d5dSMike Smith #include <sys/conf.h> 4215e32d5dSMike Smith #include <sys/ioccom.h> 4315e32d5dSMike Smith #include <sys/reboot.h> 4415e32d5dSMike Smith #include <sys/sysctl.h> 4515e32d5dSMike Smith #include <sys/ctype.h> 461611ea87SMitsuru IWASAKI #include <sys/linker.h> 47f9390180SMitsuru IWASAKI #include <sys/power.h> 4854f6bca0SNate Lawson #include <sys/sbuf.h> 49eeb3a05fSNate Lawson #include <sys/smp.h> 5015e32d5dSMike Smith 51d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 52d320e05cSJohn Baldwin #include <machine/pci_cfgreg.h> 53d320e05cSJohn Baldwin #endif 5415e32d5dSMike Smith #include <machine/resource.h> 55dc750869SNate Lawson #include <machine/bus.h> 56dc750869SNate Lawson #include <sys/rman.h> 57bc0f2195SMike Smith #include <isa/isavar.h> 58c796e27bSNate Lawson #include <isa/pnpvar.h> 59bc0f2195SMike Smith 602a191126SDavid E. O'Brien #include <contrib/dev/acpica/acpi.h> 6115e32d5dSMike Smith #include <dev/acpica/acpivar.h> 6215e32d5dSMike Smith #include <dev/acpica/acpiio.h> 6387a500cdSNate Lawson #include <contrib/dev/acpica/achware.h> 649b937d48SNate Lawson #include <contrib/dev/acpica/acnamesp.h> 6515e32d5dSMike Smith 6610ce62b9SNate Lawson #include "pci_if.h" 6710ce62b9SNate Lawson #include <dev/pci/pcivar.h> 6810ce62b9SNate Lawson #include <dev/pci/pci_private.h> 6910ce62b9SNate Lawson 702be4e471SJung-uk Kim #include <vm/vm_param.h> 712be4e471SJung-uk Kim 7215e32d5dSMike Smith MALLOC_DEFINE(M_ACPIDEV, "acpidev", "ACPI devices"); 7315e32d5dSMike Smith 74be2b1797SNate Lawson /* Hooks for the ACPI CA debugging infrastructure */ 75cb9b0d80SMike Smith #define _COMPONENT ACPI_BUS 76b53f2771SMike Smith ACPI_MODULE_NAME("ACPI") 770ae55423SMike Smith 7815e32d5dSMike Smith static d_open_t acpiopen; 7915e32d5dSMike Smith static d_close_t acpiclose; 8015e32d5dSMike Smith static d_ioctl_t acpiioctl; 8115e32d5dSMike Smith 8215e32d5dSMike Smith static struct cdevsw acpi_cdevsw = { 83dc08ffecSPoul-Henning Kamp .d_version = D_VERSION, 847ac40f5fSPoul-Henning Kamp .d_open = acpiopen, 857ac40f5fSPoul-Henning Kamp .d_close = acpiclose, 867ac40f5fSPoul-Henning Kamp .d_ioctl = acpiioctl, 877ac40f5fSPoul-Henning Kamp .d_name = "acpi", 8815e32d5dSMike Smith }; 8915e32d5dSMike Smith 90346eda4fSNate Lawson /* Global mutex for locking access to the ACPI subsystem. */ 91cb9b0d80SMike Smith struct mtx acpi_mutex; 92cb9b0d80SMike Smith 9389fb5af8SNate Lawson /* Bitmap of device quirks. */ 9489fb5af8SNate Lawson int acpi_quirks; 9589fb5af8SNate Lawson 966d63101aSMike Smith static int acpi_modevent(struct module *mod, int event, void *junk); 9715e32d5dSMike Smith static int acpi_probe(device_t dev); 9815e32d5dSMike Smith static int acpi_attach(device_t dev); 9910ce62b9SNate Lawson static int acpi_suspend(device_t dev); 10010ce62b9SNate Lawson static int acpi_resume(device_t dev); 101169b539aSNate Lawson static int acpi_shutdown(device_t dev); 102be2b1797SNate Lawson static device_t acpi_add_child(device_t bus, int order, const char *name, 103be2b1797SNate Lawson int unit); 10415e32d5dSMike Smith static int acpi_print_child(device_t bus, device_t child); 10510ce62b9SNate Lawson static void acpi_probe_nomatch(device_t bus, device_t child); 10610ce62b9SNate Lawson static void acpi_driver_added(device_t dev, driver_t *driver); 107be2b1797SNate Lawson static int acpi_read_ivar(device_t dev, device_t child, int index, 108be2b1797SNate Lawson uintptr_t *result); 109be2b1797SNate Lawson static int acpi_write_ivar(device_t dev, device_t child, int index, 110be2b1797SNate Lawson uintptr_t value); 11191233413SNate Lawson static struct resource_list *acpi_get_rlist(device_t dev, device_t child); 112adad4744SNate Lawson static int acpi_sysres_alloc(device_t dev); 113be2b1797SNate Lawson static struct resource *acpi_alloc_resource(device_t bus, device_t child, 114be2b1797SNate Lawson int type, int *rid, u_long start, u_long end, 115be2b1797SNate Lawson u_long count, u_int flags); 116be2b1797SNate Lawson static int acpi_release_resource(device_t bus, device_t child, int type, 117be2b1797SNate Lawson int rid, struct resource *r); 1188b28b622SNate Lawson static void acpi_delete_resource(device_t bus, device_t child, int type, 1198b28b622SNate Lawson int rid); 1201e4925e8SNate Lawson static uint32_t acpi_isa_get_logicalid(device_t dev); 1211e4925e8SNate Lawson static int acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count); 122ce619b2eSNate Lawson static char *acpi_device_id_probe(device_t bus, device_t dev, char **ids); 123ce619b2eSNate Lawson static ACPI_STATUS acpi_device_eval_obj(device_t bus, device_t dev, 124ce619b2eSNate Lawson ACPI_STRING pathname, ACPI_OBJECT_LIST *parameters, 125ce619b2eSNate Lawson ACPI_BUFFER *ret); 12610ce62b9SNate Lawson static int acpi_device_pwr_for_sleep(device_t bus, device_t dev, 12710ce62b9SNate Lawson int *dstate); 128df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, 129df8d2a32SNate Lawson void *context, void **retval); 130df8d2a32SNate Lawson static ACPI_STATUS acpi_device_scan_children(device_t bus, device_t dev, 131df8d2a32SNate Lawson int max_depth, acpi_scan_cb_t user_fn, void *arg); 13210ce62b9SNate Lawson static int acpi_set_powerstate_method(device_t bus, device_t child, 13310ce62b9SNate Lawson int state); 134be2b1797SNate Lawson static int acpi_isa_pnp_probe(device_t bus, device_t child, 135be2b1797SNate Lawson struct isa_pnp_id *ids); 13615e32d5dSMike Smith static void acpi_probe_children(device_t bus); 137d227204dSJohn Baldwin static void acpi_probe_order(ACPI_HANDLE handle, int *order); 138be2b1797SNate Lawson static ACPI_STATUS acpi_probe_child(ACPI_HANDLE handle, UINT32 level, 139be2b1797SNate Lawson void *context, void **status); 140ce619b2eSNate Lawson static BOOLEAN acpi_MatchHid(ACPI_HANDLE h, const char *hid); 14100a30448SNate Lawson static ACPI_STATUS acpi_EnterSleepState(struct acpi_softc *sc, int state); 14215e32d5dSMike Smith static void acpi_shutdown_final(void *arg, int howto); 14313d4f7baSMitsuru IWASAKI static void acpi_enable_fixed_events(struct acpi_softc *sc); 144d4b9ff91SNate Lawson static int acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate); 145d4b9ff91SNate Lawson static int acpi_wake_run_prep(ACPI_HANDLE handle, int sstate); 146d4b9ff91SNate Lawson static int acpi_wake_prep_walk(int sstate); 14788a79fc0SNate Lawson static int acpi_wake_sysctl_walk(device_t dev); 14888a79fc0SNate Lawson static int acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS); 14915e32d5dSMike Smith static void acpi_system_eventhandler_sleep(void *arg, int state); 15015e32d5dSMike Smith static void acpi_system_eventhandler_wakeup(void *arg, int state); 151d75de536SMitsuru IWASAKI static int acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 1521d073b1dSJohn Baldwin static int acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS); 153f9390180SMitsuru IWASAKI static int acpi_pm_func(u_long cmd, void *arg, ...); 154879d6c23STakanori Watanabe static int acpi_child_location_str_method(device_t acdev, device_t child, 155879d6c23STakanori Watanabe char *buf, size_t buflen); 156879d6c23STakanori Watanabe static int acpi_child_pnpinfo_str_method(device_t acdev, device_t child, 157879d6c23STakanori Watanabe char *buf, size_t buflen); 158d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 159d320e05cSJohn Baldwin static void acpi_enable_pcie(void); 160d320e05cSJohn Baldwin #endif 1610d484d24SJohn Baldwin static void acpi_hint_device_unit(device_t acdev, device_t child, 1620d484d24SJohn Baldwin const char *name, int *unitp); 163879d6c23STakanori Watanabe 16415e32d5dSMike Smith static device_method_t acpi_methods[] = { 16515e32d5dSMike Smith /* Device interface */ 16615e32d5dSMike Smith DEVMETHOD(device_probe, acpi_probe), 16715e32d5dSMike Smith DEVMETHOD(device_attach, acpi_attach), 168169b539aSNate Lawson DEVMETHOD(device_shutdown, acpi_shutdown), 16956a70eadSNate Lawson DEVMETHOD(device_detach, bus_generic_detach), 17010ce62b9SNate Lawson DEVMETHOD(device_suspend, acpi_suspend), 17110ce62b9SNate Lawson DEVMETHOD(device_resume, acpi_resume), 17215e32d5dSMike Smith 17315e32d5dSMike Smith /* Bus interface */ 17415e32d5dSMike Smith DEVMETHOD(bus_add_child, acpi_add_child), 17515e32d5dSMike Smith DEVMETHOD(bus_print_child, acpi_print_child), 17610ce62b9SNate Lawson DEVMETHOD(bus_probe_nomatch, acpi_probe_nomatch), 17710ce62b9SNate Lawson DEVMETHOD(bus_driver_added, acpi_driver_added), 17815e32d5dSMike Smith DEVMETHOD(bus_read_ivar, acpi_read_ivar), 17915e32d5dSMike Smith DEVMETHOD(bus_write_ivar, acpi_write_ivar), 18091233413SNate Lawson DEVMETHOD(bus_get_resource_list, acpi_get_rlist), 18191233413SNate Lawson DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 18291233413SNate Lawson DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 18315e32d5dSMike Smith DEVMETHOD(bus_alloc_resource, acpi_alloc_resource), 18415e32d5dSMike Smith DEVMETHOD(bus_release_resource, acpi_release_resource), 1858b28b622SNate Lawson DEVMETHOD(bus_delete_resource, acpi_delete_resource), 186879d6c23STakanori Watanabe DEVMETHOD(bus_child_pnpinfo_str, acpi_child_pnpinfo_str_method), 187879d6c23STakanori Watanabe DEVMETHOD(bus_child_location_str, acpi_child_location_str_method), 18815e32d5dSMike Smith DEVMETHOD(bus_activate_resource, bus_generic_activate_resource), 18915e32d5dSMike Smith DEVMETHOD(bus_deactivate_resource, bus_generic_deactivate_resource), 19015e32d5dSMike Smith DEVMETHOD(bus_setup_intr, bus_generic_setup_intr), 19115e32d5dSMike Smith DEVMETHOD(bus_teardown_intr, bus_generic_teardown_intr), 1920d484d24SJohn Baldwin DEVMETHOD(bus_hint_device_unit, acpi_hint_device_unit), 19315e32d5dSMike Smith 194ce619b2eSNate Lawson /* ACPI bus */ 195ce619b2eSNate Lawson DEVMETHOD(acpi_id_probe, acpi_device_id_probe), 196ce619b2eSNate Lawson DEVMETHOD(acpi_evaluate_object, acpi_device_eval_obj), 19710ce62b9SNate Lawson DEVMETHOD(acpi_pwr_for_sleep, acpi_device_pwr_for_sleep), 198df8d2a32SNate Lawson DEVMETHOD(acpi_scan_children, acpi_device_scan_children), 199ce619b2eSNate Lawson 20010ce62b9SNate Lawson /* PCI emulation */ 20110ce62b9SNate Lawson DEVMETHOD(pci_set_powerstate, acpi_set_powerstate_method), 20210ce62b9SNate Lawson 203bc0f2195SMike Smith /* ISA emulation */ 204bc0f2195SMike Smith DEVMETHOD(isa_pnp_probe, acpi_isa_pnp_probe), 205bc0f2195SMike Smith 20615e32d5dSMike Smith {0, 0} 20715e32d5dSMike Smith }; 20815e32d5dSMike Smith 20915e32d5dSMike Smith static driver_t acpi_driver = { 21015e32d5dSMike Smith "acpi", 21115e32d5dSMike Smith acpi_methods, 21215e32d5dSMike Smith sizeof(struct acpi_softc), 21315e32d5dSMike Smith }; 21415e32d5dSMike Smith 2153273b005SMike Smith static devclass_t acpi_devclass; 2166d63101aSMike Smith DRIVER_MODULE(acpi, nexus, acpi_driver, acpi_devclass, acpi_modevent, 0); 217a4ecd543SNate Lawson MODULE_VERSION(acpi, 1); 21815e32d5dSMike Smith 21915e2f34fSNate Lawson ACPI_SERIAL_DECL(acpi, "ACPI root bus"); 22015e2f34fSNate Lawson 221adad4744SNate Lawson /* Local pools for managing system resources for ACPI child devices. */ 222adad4744SNate Lawson static struct rman acpi_rman_io, acpi_rman_mem; 223adad4744SNate Lawson 224bee4aa4aSNate Lawson #define ACPI_MINIMUM_AWAKETIME 5 225bee4aa4aSNate Lawson 226865b8d0bSNate Lawson static const char* sleep_state_names[] = { 227865b8d0bSNate Lawson "S0", "S1", "S2", "S3", "S4", "S5", "NONE"}; 228865b8d0bSNate Lawson 2295217af30SJohn Baldwin /* Holds the description of the acpi0 device. */ 2305217af30SJohn Baldwin static char acpi_desc[ACPI_OEM_ID_SIZE + ACPI_OEM_TABLE_ID_SIZE + 2]; 2315217af30SJohn Baldwin 232197b4dccSNate Lawson SYSCTL_NODE(_debug, OID_AUTO, acpi, CTLFLAG_RD, NULL, "ACPI debugging"); 2331d7b121cSNate Lawson static char acpi_ca_version[12]; 2341d7b121cSNate Lawson SYSCTL_STRING(_debug_acpi, OID_AUTO, acpi_ca_version, CTLFLAG_RD, 2351d7b121cSNate Lawson acpi_ca_version, 0, "Version of Intel ACPI-CA"); 23615e32d5dSMike Smith 23715e32d5dSMike Smith /* 238413081d7SNate Lawson * Allow override of whether methods execute in parallel or not. 239c9b8d77dSNate Lawson * Enable this for serial behavior, which fixes "AE_ALREADY_EXISTS" 240c9b8d77dSNate Lawson * errors for AML that really can't handle parallel method execution. 241c9b8d77dSNate Lawson * It is off by default since this breaks recursive methods and 242c9b8d77dSNate Lawson * some IBMs use such code. 243413081d7SNate Lawson */ 244c9b8d77dSNate Lawson static int acpi_serialize_methods; 245413081d7SNate Lawson TUNABLE_INT("hw.acpi.serialize_methods", &acpi_serialize_methods); 246413081d7SNate Lawson 24710ce62b9SNate Lawson /* Power devices off and on in suspend and resume. XXX Remove once tested. */ 24810ce62b9SNate Lawson static int acpi_do_powerstate = 1; 24910ce62b9SNate Lawson TUNABLE_INT("debug.acpi.do_powerstate", &acpi_do_powerstate); 25010ce62b9SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, do_powerstate, CTLFLAG_RW, 25110ce62b9SNate Lawson &acpi_do_powerstate, 1, "Turn off devices when suspending."); 25210ce62b9SNate Lawson 25339da3eb3SNate Lawson /* Allow users to override quirks. */ 25439da3eb3SNate Lawson TUNABLE_INT("debug.acpi.quirks", &acpi_quirks); 25539da3eb3SNate Lawson 25693bfd059SNate Lawson static int acpi_susp_bounce; 25793bfd059SNate Lawson SYSCTL_INT(_debug_acpi, OID_AUTO, suspend_bounce, CTLFLAG_RW, 25893bfd059SNate Lawson &acpi_susp_bounce, 0, "Don't actually suspend, just test devices."); 25993bfd059SNate Lawson 260c9b8d77dSNate Lawson /* 2616d63101aSMike Smith * ACPI can only be loaded as a module by the loader; activating it after 2626d63101aSMike Smith * system bootstrap time is not useful, and can be fatal to the system. 2638c0879b6SJohn Baldwin * It also cannot be unloaded, since the entire system bus hierarchy hangs 264be2b1797SNate Lawson * off it. 2656d63101aSMike Smith */ 2666d63101aSMike Smith static int 2676d63101aSMike Smith acpi_modevent(struct module *mod, int event, void *junk) 2686d63101aSMike Smith { 2696d63101aSMike Smith switch (event) { 2706d63101aSMike Smith case MOD_LOAD: 27187b45ed5SMitsuru IWASAKI if (!cold) { 27287b45ed5SMitsuru IWASAKI printf("The ACPI driver cannot be loaded after boot.\n"); 2736d63101aSMike Smith return (EPERM); 27487b45ed5SMitsuru IWASAKI } 2756d63101aSMike Smith break; 2766d63101aSMike Smith case MOD_UNLOAD: 277f9390180SMitsuru IWASAKI if (!cold && power_pm_get_type() == POWER_PM_TYPE_ACPI) 2786d63101aSMike Smith return (EBUSY); 279f9390180SMitsuru IWASAKI break; 2806d63101aSMike Smith default: 2816d63101aSMike Smith break; 2826d63101aSMike Smith } 2836d63101aSMike Smith return (0); 2846d63101aSMike Smith } 2856d63101aSMike Smith 2866d63101aSMike Smith /* 287bbc2815cSJohn Baldwin * Perform early initialization. 28815e32d5dSMike Smith */ 289bbc2815cSJohn Baldwin ACPI_STATUS 290bbc2815cSJohn Baldwin acpi_Startup(void) 29115e32d5dSMike Smith { 29289fb5af8SNate Lawson static int started = 0; 2932be4e471SJung-uk Kim ACPI_STATUS status; 2942be4e471SJung-uk Kim int val; 29515e32d5dSMike Smith 2961b8c233dSPeter Pentchev ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 2971b8c233dSPeter Pentchev 298bee4aa4aSNate Lawson /* Only run the startup code once. The MADT driver also calls this. */ 299bbc2815cSJohn Baldwin if (started) 3002be4e471SJung-uk Kim return_VALUE (AE_OK); 301bbc2815cSJohn Baldwin started = 1; 30215e32d5dSMike Smith 303413081d7SNate Lawson /* 3042be4e471SJung-uk Kim * Pre-allocate space for RSDT/XSDT and DSDT tables and allow resizing 3052be4e471SJung-uk Kim * if more tables exist. 306413081d7SNate Lawson */ 3072be4e471SJung-uk Kim if (ACPI_FAILURE(status = AcpiInitializeTables(NULL, 2, TRUE))) { 3082be4e471SJung-uk Kim printf("ACPI: Table initialisation failed: %s\n", 3092be4e471SJung-uk Kim AcpiFormatException(status)); 3102be4e471SJung-uk Kim return_VALUE (status); 31115e32d5dSMike Smith } 3123184cf5aSNate Lawson 31389fb5af8SNate Lawson /* Set up any quirks we have for this system. */ 3142be4e471SJung-uk Kim if (acpi_quirks == ACPI_Q_OK) 31589fb5af8SNate Lawson acpi_table_quirks(&acpi_quirks); 31689fb5af8SNate Lawson 31739da3eb3SNate Lawson /* If the user manually set the disabled hint to 0, force-enable ACPI. */ 31889fb5af8SNate Lawson if (resource_int_value("acpi", 0, "disabled", &val) == 0 && val == 0) 31989fb5af8SNate Lawson acpi_quirks &= ~ACPI_Q_BROKEN; 32089fb5af8SNate Lawson if (acpi_quirks & ACPI_Q_BROKEN) { 32189fb5af8SNate Lawson printf("ACPI disabled by blacklist. Contact your BIOS vendor.\n"); 3222be4e471SJung-uk Kim status = AE_SUPPORT; 32389fb5af8SNate Lawson } 3243184cf5aSNate Lawson 3252be4e471SJung-uk Kim return_VALUE (status); 326bbc2815cSJohn Baldwin } 327bbc2815cSJohn Baldwin 328bbc2815cSJohn Baldwin /* 3295217af30SJohn Baldwin * Detect ACPI and perform early initialisation. 330bbc2815cSJohn Baldwin */ 3315217af30SJohn Baldwin int 3325217af30SJohn Baldwin acpi_identify(void) 333bbc2815cSJohn Baldwin { 3345217af30SJohn Baldwin ACPI_TABLE_RSDP *rsdp; 3355217af30SJohn Baldwin ACPI_TABLE_HEADER *rsdt; 3365217af30SJohn Baldwin ACPI_PHYSICAL_ADDRESS paddr; 3375217af30SJohn Baldwin struct sbuf sb; 338bbc2815cSJohn Baldwin 339bbc2815cSJohn Baldwin ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 340bbc2815cSJohn Baldwin 341bbc2815cSJohn Baldwin if (!cold) 3425217af30SJohn Baldwin return (ENXIO); 343bbc2815cSJohn Baldwin 344bbc2815cSJohn Baldwin /* Check that we haven't been disabled with a hint. */ 345bbc2815cSJohn Baldwin if (resource_disabled("acpi", 0)) 3465217af30SJohn Baldwin return (ENXIO); 347bbc2815cSJohn Baldwin 3485217af30SJohn Baldwin /* Check for other PM systems. */ 3495217af30SJohn Baldwin if (power_pm_get_type() != POWER_PM_TYPE_NONE && 3505217af30SJohn Baldwin power_pm_get_type() != POWER_PM_TYPE_ACPI) { 3515217af30SJohn Baldwin printf("ACPI identify failed, other PM system enabled.\n"); 3525217af30SJohn Baldwin return (ENXIO); 3535217af30SJohn Baldwin } 3544b1ff978SMark Santcroos 3552be4e471SJung-uk Kim /* Initialize root tables. */ 3562be4e471SJung-uk Kim if (ACPI_FAILURE(acpi_Startup())) { 3572be4e471SJung-uk Kim printf("ACPI: Try disabling either ACPI or apic support.\n"); 3585217af30SJohn Baldwin return (ENXIO); 3592be4e471SJung-uk Kim } 36015e32d5dSMike Smith 3615217af30SJohn Baldwin if ((paddr = AcpiOsGetRootPointer()) == 0 || 3625217af30SJohn Baldwin (rsdp = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_RSDP))) == NULL) 3635217af30SJohn Baldwin return (ENXIO); 3645217af30SJohn Baldwin if (rsdp->Revision > 1 && rsdp->XsdtPhysicalAddress != 0) 3655217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->XsdtPhysicalAddress; 3665217af30SJohn Baldwin else 3675217af30SJohn Baldwin paddr = (ACPI_PHYSICAL_ADDRESS)rsdp->RsdtPhysicalAddress; 3685217af30SJohn Baldwin AcpiOsUnmapMemory(rsdp, sizeof(ACPI_TABLE_RSDP)); 3695217af30SJohn Baldwin 3705217af30SJohn Baldwin if ((rsdt = AcpiOsMapMemory(paddr, sizeof(ACPI_TABLE_HEADER))) == NULL) 3715217af30SJohn Baldwin return (ENXIO); 3725217af30SJohn Baldwin sbuf_new(&sb, acpi_desc, sizeof(acpi_desc), SBUF_FIXEDLEN); 3735217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemId, ACPI_OEM_ID_SIZE); 3745217af30SJohn Baldwin sbuf_trim(&sb); 3755217af30SJohn Baldwin sbuf_putc(&sb, ' '); 3765217af30SJohn Baldwin sbuf_bcat(&sb, rsdt->OemTableId, ACPI_OEM_TABLE_ID_SIZE); 3775217af30SJohn Baldwin sbuf_trim(&sb); 3785217af30SJohn Baldwin sbuf_finish(&sb); 3795217af30SJohn Baldwin sbuf_delete(&sb); 3805217af30SJohn Baldwin AcpiOsUnmapMemory(rsdt, sizeof(ACPI_TABLE_HEADER)); 3815217af30SJohn Baldwin 3825217af30SJohn Baldwin snprintf(acpi_ca_version, sizeof(acpi_ca_version), "%x", ACPI_CA_VERSION); 3835217af30SJohn Baldwin 3845217af30SJohn Baldwin return (0); 38515e32d5dSMike Smith } 38615e32d5dSMike Smith 38715e32d5dSMike Smith /* 388bee4aa4aSNate Lawson * Fetch some descriptive data from ACPI to put in our attach message. 38915e32d5dSMike Smith */ 39015e32d5dSMike Smith static int 39115e32d5dSMike Smith acpi_probe(device_t dev) 39215e32d5dSMike Smith { 39315e32d5dSMike Smith 394b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 395f9390180SMitsuru IWASAKI 3965217af30SJohn Baldwin device_set_desc(dev, acpi_desc); 397bee4aa4aSNate Lawson 3982be4e471SJung-uk Kim return_VALUE (0); 39915e32d5dSMike Smith } 40015e32d5dSMike Smith 40115e32d5dSMike Smith static int 40215e32d5dSMike Smith acpi_attach(device_t dev) 40315e32d5dSMike Smith { 40415e32d5dSMike Smith struct acpi_softc *sc; 4052be4e471SJung-uk Kim ACPI_TABLE_FACS *facs; 406cb9b0d80SMike Smith ACPI_STATUS status; 407ea27c63eSNate Lawson int error, state; 40832d18aa5SMike Smith UINT32 flags; 409ea27c63eSNate Lawson UINT8 TypeA, TypeB; 410498d464fSMitsuru IWASAKI char *env; 41115e32d5dSMike Smith 412b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 413bee4aa4aSNate Lawson 41415e32d5dSMike Smith sc = device_get_softc(dev); 41515e32d5dSMike Smith sc->acpi_dev = dev; 41600a30448SNate Lawson callout_init(&sc->susp_force_to, TRUE); 41715e32d5dSMike Smith 4182be4e471SJung-uk Kim error = ENXIO; 4192be4e471SJung-uk Kim 42091233413SNate Lawson /* Initialize resource manager. */ 42191233413SNate Lawson acpi_rman_io.rm_type = RMAN_ARRAY; 42291233413SNate Lawson acpi_rman_io.rm_start = 0; 42391233413SNate Lawson acpi_rman_io.rm_end = 0xffff; 4240bba6acfSJohn Baldwin acpi_rman_io.rm_descr = "ACPI I/O ports"; 42591233413SNate Lawson if (rman_init(&acpi_rman_io) != 0) 42691233413SNate Lawson panic("acpi rman_init IO ports failed"); 42791233413SNate Lawson acpi_rman_mem.rm_type = RMAN_ARRAY; 42891233413SNate Lawson acpi_rman_mem.rm_start = 0; 42991233413SNate Lawson acpi_rman_mem.rm_end = ~0ul; 4300bba6acfSJohn Baldwin acpi_rman_mem.rm_descr = "ACPI I/O memory addresses"; 43191233413SNate Lawson if (rman_init(&acpi_rman_mem) != 0) 43291233413SNate Lawson panic("acpi rman_init memory failed"); 43391233413SNate Lawson 4342be4e471SJung-uk Kim /* Initialise the ACPI mutex */ 4352be4e471SJung-uk Kim mtx_init(&acpi_mutex, "ACPI global lock", NULL, MTX_DEF); 4362be4e471SJung-uk Kim 4372be4e471SJung-uk Kim /* 4382be4e471SJung-uk Kim * Set the globals from our tunables. This is needed because ACPI-CA 4392be4e471SJung-uk Kim * uses UINT8 for some values and we have no tunable_byte. 4402be4e471SJung-uk Kim */ 4412be4e471SJung-uk Kim AcpiGbl_AllMethodsSerialized = acpi_serialize_methods; 4422be4e471SJung-uk Kim AcpiGbl_EnableInterpreterSlack = TRUE; 4432be4e471SJung-uk Kim 4442be4e471SJung-uk Kim /* Start up the ACPI CA subsystem. */ 4452be4e471SJung-uk Kim status = AcpiInitializeSubsystem(); 4462be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4472be4e471SJung-uk Kim device_printf(dev, "Could not initialize Subsystem: %s\n", 4482be4e471SJung-uk Kim AcpiFormatException(status)); 4492be4e471SJung-uk Kim goto out; 4502be4e471SJung-uk Kim } 4512be4e471SJung-uk Kim 4522be4e471SJung-uk Kim /* Load ACPI name space. */ 4532be4e471SJung-uk Kim status = AcpiLoadTables(); 4542be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 4552be4e471SJung-uk Kim device_printf(dev, "Could not load Namespace: %s\n", 4562be4e471SJung-uk Kim AcpiFormatException(status)); 4572be4e471SJung-uk Kim goto out; 4582be4e471SJung-uk Kim } 4592be4e471SJung-uk Kim 460d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 461d320e05cSJohn Baldwin /* Handle MCFG table if present. */ 462d320e05cSJohn Baldwin acpi_enable_pcie(); 463d320e05cSJohn Baldwin #endif 464d320e05cSJohn Baldwin 465be2b1797SNate Lawson /* Install the default address space handlers. */ 466be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 467be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_MEMORY, ACPI_DEFAULT_HANDLER, NULL, NULL); 468be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 469be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemMemory handler: %s\n", 470be2b1797SNate Lawson AcpiFormatException(status)); 471cb9b0d80SMike Smith goto out; 47215e32d5dSMike Smith } 473be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 474be2b1797SNate Lawson ACPI_ADR_SPACE_SYSTEM_IO, ACPI_DEFAULT_HANDLER, NULL, NULL); 475be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 476be2b1797SNate Lawson device_printf(dev, "Could not initialise SystemIO handler: %s\n", 477be2b1797SNate Lawson AcpiFormatException(status)); 478cb9b0d80SMike Smith goto out; 47915e32d5dSMike Smith } 480be2b1797SNate Lawson status = AcpiInstallAddressSpaceHandler(ACPI_ROOT_OBJECT, 481be2b1797SNate Lawson ACPI_ADR_SPACE_PCI_CONFIG, ACPI_DEFAULT_HANDLER, NULL, NULL); 482be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 483be2b1797SNate Lawson device_printf(dev, "could not initialise PciConfig handler: %s\n", 484be2b1797SNate Lawson AcpiFormatException(status)); 485cb9b0d80SMike Smith goto out; 48615e32d5dSMike Smith } 48715e32d5dSMike Smith 48815e32d5dSMike Smith /* 489be2b1797SNate Lawson * Note that some systems (specifically, those with namespace evaluation 490be2b1797SNate Lawson * issues that require the avoidance of parts of the namespace) must 491be2b1797SNate Lawson * avoid running _INI and _STA on everything, as well as dodging the final 492be2b1797SNate Lawson * object init pass. 49315e32d5dSMike Smith * 49432d18aa5SMike Smith * For these devices, we set ACPI_NO_DEVICE_INIT and ACPI_NO_OBJECT_INIT). 49532d18aa5SMike Smith * 496be2b1797SNate Lawson * XXX We should arrange for the object init pass after we have attached 497be2b1797SNate Lawson * all our child devices, but on many systems it works here. 49815e32d5dSMike Smith */ 49932d18aa5SMike Smith flags = 0; 500d786139cSMaxime Henrion if (testenv("debug.acpi.avoid")) 50132d18aa5SMike Smith flags = ACPI_NO_DEVICE_INIT | ACPI_NO_OBJECT_INIT; 502bee4aa4aSNate Lawson 503bee4aa4aSNate Lawson /* Bring the hardware and basic handlers online. */ 504b53f2771SMike Smith if (ACPI_FAILURE(status = AcpiEnableSubsystem(flags))) { 505be2b1797SNate Lawson device_printf(dev, "Could not enable ACPI: %s\n", 506be2b1797SNate Lawson AcpiFormatException(status)); 507cb9b0d80SMike Smith goto out; 50815e32d5dSMike Smith } 50915e32d5dSMike Smith 510f8335e3aSNate Lawson /* 511f8335e3aSNate Lawson * Call the ECDT probe function to provide EC functionality before 512f8335e3aSNate Lawson * the namespace has been evaluated. 513da72d149SNate Lawson * 514da72d149SNate Lawson * XXX This happens before the sysresource devices have been probed and 515da72d149SNate Lawson * attached so its resources come from nexus0. In practice, this isn't 516da72d149SNate Lawson * a problem but should be addressed eventually. 517f8335e3aSNate Lawson */ 518f8335e3aSNate Lawson acpi_ec_ecdt_probe(dev); 519f8335e3aSNate Lawson 520bee4aa4aSNate Lawson /* Bring device objects and regions online. */ 521b69ed3f4SMitsuru IWASAKI if (ACPI_FAILURE(status = AcpiInitializeObjects(flags))) { 522be2b1797SNate Lawson device_printf(dev, "Could not initialize ACPI objects: %s\n", 523be2b1797SNate Lawson AcpiFormatException(status)); 524b69ed3f4SMitsuru IWASAKI goto out; 525b69ed3f4SMitsuru IWASAKI } 526b69ed3f4SMitsuru IWASAKI 52715e32d5dSMike Smith /* 5281d073b1dSJohn Baldwin * Setup our sysctl tree. 5291d073b1dSJohn Baldwin * 5301d073b1dSJohn Baldwin * XXX: This doesn't check to make sure that none of these fail. 5311d073b1dSJohn Baldwin */ 5321d073b1dSJohn Baldwin sysctl_ctx_init(&sc->acpi_sysctl_ctx); 5331d073b1dSJohn Baldwin sc->acpi_sysctl_tree = SYSCTL_ADD_NODE(&sc->acpi_sysctl_ctx, 5341d073b1dSJohn Baldwin SYSCTL_STATIC_CHILDREN(_hw), OID_AUTO, 5351d073b1dSJohn Baldwin device_get_name(dev), CTLFLAG_RD, 0, ""); 5361d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 537d75de536SMitsuru IWASAKI OID_AUTO, "supported_sleep_state", CTLTYPE_STRING | CTLFLAG_RD, 538d75de536SMitsuru IWASAKI 0, 0, acpi_supported_sleep_state_sysctl, "A", ""); 539d75de536SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5401d073b1dSJohn Baldwin OID_AUTO, "power_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5411d073b1dSJohn Baldwin &sc->acpi_power_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5421d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5431d073b1dSJohn Baldwin OID_AUTO, "sleep_button_state", CTLTYPE_STRING | CTLFLAG_RW, 5441d073b1dSJohn Baldwin &sc->acpi_sleep_button_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5451d073b1dSJohn Baldwin SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 5461d073b1dSJohn Baldwin OID_AUTO, "lid_switch_state", CTLTYPE_STRING | CTLFLAG_RW, 5471d073b1dSJohn Baldwin &sc->acpi_lid_switch_sx, 0, acpi_sleep_state_sysctl, "A", ""); 548f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 549f86214b6SMitsuru IWASAKI OID_AUTO, "standby_state", CTLTYPE_STRING | CTLFLAG_RW, 550f86214b6SMitsuru IWASAKI &sc->acpi_standby_sx, 0, acpi_sleep_state_sysctl, "A", ""); 551f86214b6SMitsuru IWASAKI SYSCTL_ADD_PROC(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 552f86214b6SMitsuru IWASAKI OID_AUTO, "suspend_state", CTLTYPE_STRING | CTLFLAG_RW, 553f86214b6SMitsuru IWASAKI &sc->acpi_suspend_sx, 0, acpi_sleep_state_sysctl, "A", ""); 5542d644607SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 555197b4dccSNate Lawson OID_AUTO, "sleep_delay", CTLFLAG_RW, &sc->acpi_sleep_delay, 0, 556197b4dccSNate Lawson "sleep delay"); 557ff01efb5SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 558197b4dccSNate Lawson OID_AUTO, "s4bios", CTLFLAG_RW, &sc->acpi_s4bios, 0, "S4BIOS mode"); 5591611ea87SMitsuru IWASAKI SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 560197b4dccSNate Lawson OID_AUTO, "verbose", CTLFLAG_RW, &sc->acpi_verbose, 0, "verbose mode"); 561d85e6785SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 562d85e6785SNate Lawson OID_AUTO, "disable_on_reboot", CTLFLAG_RW, 563d85e6785SNate Lawson &sc->acpi_do_disable, 0, "Disable ACPI when rebooting/halting system"); 564d1b16e18SNate Lawson SYSCTL_ADD_INT(&sc->acpi_sysctl_ctx, SYSCTL_CHILDREN(sc->acpi_sysctl_tree), 565d1b16e18SNate Lawson OID_AUTO, "handle_reboot", CTLFLAG_RW, 566d1b16e18SNate Lawson &sc->acpi_handle_reboot, 0, "Use ACPI Reset Register to reboot"); 567bf0c18ecSNate Lawson 568bf0c18ecSNate Lawson /* 5692b9609abSNate Lawson * Default to 1 second before sleeping to give some machines time to 570bf0c18ecSNate Lawson * stabilize. 571bf0c18ecSNate Lawson */ 5722b9609abSNate Lawson sc->acpi_sleep_delay = 1; 5732d644607SMitsuru IWASAKI if (bootverbose) 5742d644607SMitsuru IWASAKI sc->acpi_verbose = 1; 575965a34fbSNate Lawson if ((env = getenv("hw.acpi.verbose")) != NULL) { 576965a34fbSNate Lawson if (strcmp(env, "0") != 0) 577498d464fSMitsuru IWASAKI sc->acpi_verbose = 1; 578498d464fSMitsuru IWASAKI freeenv(env); 579498d464fSMitsuru IWASAKI } 5801d073b1dSJohn Baldwin 5812d610c46SNate Lawson /* Only enable S4BIOS by default if the FACS says it is available. */ 5822be4e471SJung-uk Kim status = AcpiGetTable(ACPI_SIG_FACS, 0, (ACPI_TABLE_HEADER **)&facs); 5832be4e471SJung-uk Kim if (ACPI_FAILURE(status)) { 5842be4e471SJung-uk Kim device_printf(dev, "couldn't get FACS: %s\n", 5852be4e471SJung-uk Kim AcpiFormatException(status)); 5862be4e471SJung-uk Kim error = ENXIO; 5872be4e471SJung-uk Kim goto out; 5882be4e471SJung-uk Kim } 5892be4e471SJung-uk Kim if (facs->Flags & ACPI_FACS_S4_BIOS_PRESENT) 5902d610c46SNate Lawson sc->acpi_s4bios = 1; 5912d610c46SNate Lawson 5921d073b1dSJohn Baldwin /* 593ea27c63eSNate Lawson * Dispatch the default sleep state to devices. The lid switch is set 594ea27c63eSNate Lawson * to NONE by default to avoid surprising users. 59515e32d5dSMike Smith */ 596ea27c63eSNate Lawson sc->acpi_power_button_sx = ACPI_STATE_S5; 597ea27c63eSNate Lawson sc->acpi_lid_switch_sx = ACPI_S_STATES_MAX + 1; 598f86214b6SMitsuru IWASAKI sc->acpi_standby_sx = ACPI_STATE_S1; 599f86214b6SMitsuru IWASAKI sc->acpi_suspend_sx = ACPI_STATE_S3; 60015e32d5dSMike Smith 601ea27c63eSNate Lawson /* Pick the first valid sleep state for the sleep button default. */ 602ea27c63eSNate Lawson sc->acpi_sleep_button_sx = ACPI_S_STATES_MAX + 1; 60300a30448SNate Lawson for (state = ACPI_STATE_S1; state <= ACPI_STATE_S4; state++) 604ea27c63eSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) { 605ea27c63eSNate Lawson sc->acpi_sleep_button_sx = state; 606ea27c63eSNate Lawson break; 607ea27c63eSNate Lawson } 608ea27c63eSNate Lawson 60913d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(sc); 61015e32d5dSMike Smith 61115e32d5dSMike Smith /* 61215e32d5dSMike Smith * Scan the namespace and attach/initialise children. 61315e32d5dSMike Smith */ 61415e32d5dSMike Smith 61559e472e9SNate Lawson /* Register our shutdown handler. */ 616be2b1797SNate Lawson EVENTHANDLER_REGISTER(shutdown_final, acpi_shutdown_final, sc, 617be2b1797SNate Lawson SHUTDOWN_PRI_LAST); 61815e32d5dSMike Smith 61915e32d5dSMike Smith /* 62015e32d5dSMike Smith * Register our acpi event handlers. 62115e32d5dSMike Smith * XXX should be configurable eg. via userland policy manager. 62215e32d5dSMike Smith */ 623be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_sleep_event, acpi_system_eventhandler_sleep, 624be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 625be2b1797SNate Lawson EVENTHANDLER_REGISTER(acpi_wakeup_event, acpi_system_eventhandler_wakeup, 626be2b1797SNate Lawson sc, ACPI_EVENT_PRI_LAST); 62715e32d5dSMike Smith 628be2b1797SNate Lawson /* Flag our initial states. */ 62915e32d5dSMike Smith sc->acpi_enabled = 1; 63015e32d5dSMike Smith sc->acpi_sstate = ACPI_STATE_S0; 631ece50487SMitsuru IWASAKI sc->acpi_sleep_disabled = 0; 63215e32d5dSMike Smith 633be2b1797SNate Lawson /* Create the control device */ 634a89fcf28STakanori Watanabe sc->acpi_dev_t = make_dev(&acpi_cdevsw, 0, UID_ROOT, GID_WHEEL, 0644, 6352a4689e8SRobert Watson "acpi"); 63615e32d5dSMike Smith sc->acpi_dev_t->si_drv1 = sc; 63715e32d5dSMike Smith 638be2b1797SNate Lawson if ((error = acpi_machdep_init(dev))) 639f86214b6SMitsuru IWASAKI goto out; 640f86214b6SMitsuru IWASAKI 641f9390180SMitsuru IWASAKI /* Register ACPI again to pass the correct argument of pm_func. */ 642f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, sc); 643f9390180SMitsuru IWASAKI 644eeb6dba2SJohn Baldwin if (!acpi_disabled("bus")) 645eeb6dba2SJohn Baldwin acpi_probe_children(dev); 646eeb6dba2SJohn Baldwin 647cb9b0d80SMike Smith error = 0; 648cb9b0d80SMike Smith 649cb9b0d80SMike Smith out: 650cb9b0d80SMike Smith return_VALUE (error); 65115e32d5dSMike Smith } 65215e32d5dSMike Smith 653169b539aSNate Lawson static int 65410ce62b9SNate Lawson acpi_suspend(device_t dev) 65510ce62b9SNate Lawson { 65610ce62b9SNate Lawson device_t child, *devlist; 65710ce62b9SNate Lawson int error, i, numdevs, pstate; 65810ce62b9SNate Lawson 6595d3d03f1SNate Lawson GIANT_REQUIRED; 6605d3d03f1SNate Lawson 66110ce62b9SNate Lawson /* First give child devices a chance to suspend. */ 66210ce62b9SNate Lawson error = bus_generic_suspend(dev); 66310ce62b9SNate Lawson if (error) 66410ce62b9SNate Lawson return (error); 66510ce62b9SNate Lawson 66610ce62b9SNate Lawson /* 66710ce62b9SNate Lawson * Now, set them into the appropriate power state, usually D3. If the 66810ce62b9SNate Lawson * device has an _SxD method for the next sleep state, use that power 66910ce62b9SNate Lawson * state instead. 67010ce62b9SNate Lawson */ 671099ea4b5SWarner Losh error = device_get_children(dev, &devlist, &numdevs); 672099ea4b5SWarner Losh if (error) 673099ea4b5SWarner Losh return (error); 67410ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 67510ce62b9SNate Lawson /* If the device is not attached, we've powered it down elsewhere. */ 67610ce62b9SNate Lawson child = devlist[i]; 67710ce62b9SNate Lawson if (!device_is_attached(child)) 67810ce62b9SNate Lawson continue; 67910ce62b9SNate Lawson 68010ce62b9SNate Lawson /* 68110ce62b9SNate Lawson * Default to D3 for all sleep states. The _SxD method is optional 68210ce62b9SNate Lawson * so set the powerstate even if it's absent. 68310ce62b9SNate Lawson */ 68410ce62b9SNate Lawson pstate = PCI_POWERSTATE_D3; 68510ce62b9SNate Lawson error = acpi_device_pwr_for_sleep(device_get_parent(child), 68610ce62b9SNate Lawson child, &pstate); 68710ce62b9SNate Lawson if ((error == 0 || error == ESRCH) && acpi_do_powerstate) 68810ce62b9SNate Lawson pci_set_powerstate(child, pstate); 68910ce62b9SNate Lawson } 69010ce62b9SNate Lawson free(devlist, M_TEMP); 69110ce62b9SNate Lawson error = 0; 69210ce62b9SNate Lawson 69310ce62b9SNate Lawson return (error); 69410ce62b9SNate Lawson } 69510ce62b9SNate Lawson 69610ce62b9SNate Lawson static int 69710ce62b9SNate Lawson acpi_resume(device_t dev) 69810ce62b9SNate Lawson { 69910ce62b9SNate Lawson ACPI_HANDLE handle; 700099ea4b5SWarner Losh int i, numdevs, error; 70110ce62b9SNate Lawson device_t child, *devlist; 70210ce62b9SNate Lawson 7035d3d03f1SNate Lawson GIANT_REQUIRED; 7045d3d03f1SNate Lawson 70510ce62b9SNate Lawson /* 70610ce62b9SNate Lawson * Put all devices in D0 before resuming them. Call _S0D on each one 70710ce62b9SNate Lawson * since some systems expect this. 70810ce62b9SNate Lawson */ 709099ea4b5SWarner Losh error = device_get_children(dev, &devlist, &numdevs); 710099ea4b5SWarner Losh if (error) 711099ea4b5SWarner Losh return (error); 71210ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 71310ce62b9SNate Lawson child = devlist[i]; 71410ce62b9SNate Lawson handle = acpi_get_handle(child); 71510ce62b9SNate Lawson if (handle) 71610ce62b9SNate Lawson AcpiEvaluateObject(handle, "_S0D", NULL, NULL); 71710ce62b9SNate Lawson if (device_is_attached(child) && acpi_do_powerstate) 71810ce62b9SNate Lawson pci_set_powerstate(child, PCI_POWERSTATE_D0); 71910ce62b9SNate Lawson } 72010ce62b9SNate Lawson free(devlist, M_TEMP); 72110ce62b9SNate Lawson 72210ce62b9SNate Lawson return (bus_generic_resume(dev)); 72310ce62b9SNate Lawson } 72410ce62b9SNate Lawson 72510ce62b9SNate Lawson static int 726169b539aSNate Lawson acpi_shutdown(device_t dev) 727169b539aSNate Lawson { 728169b539aSNate Lawson 7295d3d03f1SNate Lawson GIANT_REQUIRED; 7305d3d03f1SNate Lawson 7310e414868SNate Lawson /* Allow children to shutdown first. */ 7320e414868SNate Lawson bus_generic_shutdown(dev); 7330e414868SNate Lawson 734bee4aa4aSNate Lawson /* 735bee4aa4aSNate Lawson * Enable any GPEs that are able to power-on the system (i.e., RTC). 736bee4aa4aSNate Lawson * Also, disable any that are not valid for this state (most). 737bee4aa4aSNate Lawson */ 738d4b9ff91SNate Lawson acpi_wake_prep_walk(ACPI_STATE_S5); 739d4b9ff91SNate Lawson 740169b539aSNate Lawson return (0); 741169b539aSNate Lawson } 742169b539aSNate Lawson 74315e32d5dSMike Smith /* 74415e32d5dSMike Smith * Handle a new device being added 74515e32d5dSMike Smith */ 74615e32d5dSMike Smith static device_t 74715e32d5dSMike Smith acpi_add_child(device_t bus, int order, const char *name, int unit) 74815e32d5dSMike Smith { 74915e32d5dSMike Smith struct acpi_device *ad; 75015e32d5dSMike Smith device_t child; 75115e32d5dSMike Smith 752be2b1797SNate Lawson if ((ad = malloc(sizeof(*ad), M_ACPIDEV, M_NOWAIT | M_ZERO)) == NULL) 75315e32d5dSMike Smith return (NULL); 75415e32d5dSMike Smith 75515e32d5dSMike Smith resource_list_init(&ad->ad_rl); 75615e32d5dSMike Smith 75715e32d5dSMike Smith child = device_add_child_ordered(bus, order, name, unit); 75815e32d5dSMike Smith if (child != NULL) 75915e32d5dSMike Smith device_set_ivars(child, ad); 76043ce1c77SNate Lawson else 76143ce1c77SNate Lawson free(ad, M_ACPIDEV); 76215e32d5dSMike Smith return (child); 76315e32d5dSMike Smith } 76415e32d5dSMike Smith 76515e32d5dSMike Smith static int 76615e32d5dSMike Smith acpi_print_child(device_t bus, device_t child) 76715e32d5dSMike Smith { 76815e32d5dSMike Smith struct acpi_device *adev = device_get_ivars(child); 76915e32d5dSMike Smith struct resource_list *rl = &adev->ad_rl; 77015e32d5dSMike Smith int retval = 0; 77115e32d5dSMike Smith 77215e32d5dSMike Smith retval += bus_print_child_header(bus, child); 7739ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "port", SYS_RES_IOPORT, "%#lx"); 7749ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "iomem", SYS_RES_MEMORY, "%#lx"); 7759ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "irq", SYS_RES_IRQ, "%ld"); 7769ed79ecaSJohn Baldwin retval += resource_list_print_type(rl, "drq", SYS_RES_DRQ, "%ld"); 777a91c5fa8SNate Lawson if (device_get_flags(child)) 778a91c5fa8SNate Lawson retval += printf(" flags %#x", device_get_flags(child)); 7792c8d3b23SNate Lawson retval += bus_print_child_footer(bus, child); 78015e32d5dSMike Smith 78115e32d5dSMike Smith return (retval); 78215e32d5dSMike Smith } 78315e32d5dSMike Smith 78410ce62b9SNate Lawson /* 78510ce62b9SNate Lawson * If this device is an ACPI child but no one claimed it, attempt 78610ce62b9SNate Lawson * to power it off. We'll power it back up when a driver is added. 78710ce62b9SNate Lawson * 78810ce62b9SNate Lawson * XXX Disabled for now since many necessary devices (like fdc and 78910ce62b9SNate Lawson * ATA) don't claim the devices we created for them but still expect 79010ce62b9SNate Lawson * them to be powered up. 79110ce62b9SNate Lawson */ 79210ce62b9SNate Lawson static void 79310ce62b9SNate Lawson acpi_probe_nomatch(device_t bus, device_t child) 79410ce62b9SNate Lawson { 79596cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 79696cf0b6dSWarner Losh pci_set_powerstate(child, PCI_POWERSTATE_D3); 79796cf0b6dSWarner Losh #endif 79810ce62b9SNate Lawson } 79910ce62b9SNate Lawson 80010ce62b9SNate Lawson /* 80110ce62b9SNate Lawson * If a new driver has a chance to probe a child, first power it up. 80210ce62b9SNate Lawson * 80310ce62b9SNate Lawson * XXX Disabled for now (see acpi_probe_nomatch for details). 80410ce62b9SNate Lawson */ 80510ce62b9SNate Lawson static void 80610ce62b9SNate Lawson acpi_driver_added(device_t dev, driver_t *driver) 80710ce62b9SNate Lawson { 80810ce62b9SNate Lawson device_t child, *devlist; 80910ce62b9SNate Lawson int i, numdevs; 81010ce62b9SNate Lawson 81110ce62b9SNate Lawson DEVICE_IDENTIFY(driver, dev); 812099ea4b5SWarner Losh if (device_get_children(dev, &devlist, &numdevs)) 813099ea4b5SWarner Losh return; 81410ce62b9SNate Lawson for (i = 0; i < numdevs; i++) { 81510ce62b9SNate Lawson child = devlist[i]; 81610ce62b9SNate Lawson if (device_get_state(child) == DS_NOTPRESENT) { 81796cf0b6dSWarner Losh #ifdef ACPI_ENABLE_POWERDOWN_NODRIVER 81896cf0b6dSWarner Losh pci_set_powerstate(child, PCI_POWERSTATE_D0); 81910ce62b9SNate Lawson if (device_probe_and_attach(child) != 0) 82096cf0b6dSWarner Losh pci_set_powerstate(child, PCI_POWERSTATE_D3); 82196cf0b6dSWarner Losh #else 82296cf0b6dSWarner Losh device_probe_and_attach(child); 82396cf0b6dSWarner Losh #endif 82410ce62b9SNate Lawson } 82510ce62b9SNate Lawson } 82610ce62b9SNate Lawson free(devlist, M_TEMP); 82710ce62b9SNate Lawson } 82810ce62b9SNate Lawson 82963600cc3SNate Lawson /* Location hint for devctl(8) */ 83063600cc3SNate Lawson static int 831247648afSTakanori Watanabe acpi_child_location_str_method(device_t cbdev, device_t child, char *buf, 832247648afSTakanori Watanabe size_t buflen) 833247648afSTakanori Watanabe { 834247648afSTakanori Watanabe struct acpi_device *dinfo = device_get_ivars(child); 835247648afSTakanori Watanabe 836247648afSTakanori Watanabe if (dinfo->ad_handle) 837d40c0f53SNate Lawson snprintf(buf, buflen, "handle=%s", acpi_name(dinfo->ad_handle)); 838247648afSTakanori Watanabe else 839d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 840247648afSTakanori Watanabe return (0); 841247648afSTakanori Watanabe } 842247648afSTakanori Watanabe 84363600cc3SNate Lawson /* PnP information for devctl(8) */ 84463600cc3SNate Lawson static int 845247648afSTakanori Watanabe acpi_child_pnpinfo_str_method(device_t cbdev, device_t child, char *buf, 846247648afSTakanori Watanabe size_t buflen) 847247648afSTakanori Watanabe { 848247648afSTakanori Watanabe ACPI_BUFFER adbuf = {ACPI_ALLOCATE_BUFFER, NULL}; 84963600cc3SNate Lawson ACPI_DEVICE_INFO *adinfo; 85063600cc3SNate Lawson struct acpi_device *dinfo = device_get_ivars(child); 851247648afSTakanori Watanabe char *end; 852247648afSTakanori Watanabe int error; 853247648afSTakanori Watanabe 854247648afSTakanori Watanabe error = AcpiGetObjectInfo(dinfo->ad_handle, &adbuf); 855247648afSTakanori Watanabe adinfo = (ACPI_DEVICE_INFO *) adbuf.Pointer; 856247648afSTakanori Watanabe if (error) 857d40c0f53SNate Lawson snprintf(buf, buflen, "unknown"); 858247648afSTakanori Watanabe else 859247648afSTakanori Watanabe snprintf(buf, buflen, "_HID=%s _UID=%lu", 860247648afSTakanori Watanabe (adinfo->Valid & ACPI_VALID_HID) ? 861d40c0f53SNate Lawson adinfo->HardwareId.Value : "none", 86263600cc3SNate Lawson (adinfo->Valid & ACPI_VALID_UID) ? 86363600cc3SNate Lawson strtoul(adinfo->UniqueId.Value, &end, 10) : 0); 864247648afSTakanori Watanabe if (adinfo) 865247648afSTakanori Watanabe AcpiOsFree(adinfo); 866247648afSTakanori Watanabe 867247648afSTakanori Watanabe return (0); 868247648afSTakanori Watanabe } 869247648afSTakanori Watanabe 870247648afSTakanori Watanabe /* 87115e32d5dSMike Smith * Handle per-device ivars 87215e32d5dSMike Smith */ 87315e32d5dSMike Smith static int 87415e32d5dSMike Smith acpi_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 87515e32d5dSMike Smith { 87615e32d5dSMike Smith struct acpi_device *ad; 87715e32d5dSMike Smith 87815e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 87915e32d5dSMike Smith printf("device has no ivars\n"); 88015e32d5dSMike Smith return (ENOENT); 88115e32d5dSMike Smith } 88215e32d5dSMike Smith 883be2b1797SNate Lawson /* ACPI and ISA compatibility ivars */ 88415e32d5dSMike Smith switch(index) { 88515e32d5dSMike Smith case ACPI_IVAR_HANDLE: 88615e32d5dSMike Smith *(ACPI_HANDLE *)result = ad->ad_handle; 88715e32d5dSMike Smith break; 88815e32d5dSMike Smith case ACPI_IVAR_MAGIC: 88970fa7bc0SNate Lawson *(uintptr_t *)result = ad->ad_magic; 89015e32d5dSMike Smith break; 89115e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 89215e32d5dSMike Smith *(void **)result = ad->ad_private; 89315e32d5dSMike Smith break; 894d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 895d4b9ff91SNate Lawson *(int *)result = ad->ad_flags; 896d4b9ff91SNate Lawson break; 89732d18aa5SMike Smith case ISA_IVAR_VENDORID: 89832d18aa5SMike Smith case ISA_IVAR_SERIAL: 89932d18aa5SMike Smith case ISA_IVAR_COMPATID: 90032d18aa5SMike Smith *(int *)result = -1; 90132d18aa5SMike Smith break; 90232d18aa5SMike Smith case ISA_IVAR_LOGICALID: 90332d18aa5SMike Smith *(int *)result = acpi_isa_get_logicalid(child); 90432d18aa5SMike Smith break; 90515e32d5dSMike Smith default: 90615e32d5dSMike Smith return (ENOENT); 90715e32d5dSMike Smith } 908be2b1797SNate Lawson 90915e32d5dSMike Smith return (0); 91015e32d5dSMike Smith } 91115e32d5dSMike Smith 91215e32d5dSMike Smith static int 91315e32d5dSMike Smith acpi_write_ivar(device_t dev, device_t child, int index, uintptr_t value) 91415e32d5dSMike Smith { 91515e32d5dSMike Smith struct acpi_device *ad; 91615e32d5dSMike Smith 91715e32d5dSMike Smith if ((ad = device_get_ivars(child)) == NULL) { 91815e32d5dSMike Smith printf("device has no ivars\n"); 91915e32d5dSMike Smith return (ENOENT); 92015e32d5dSMike Smith } 92115e32d5dSMike Smith 92215e32d5dSMike Smith switch(index) { 92315e32d5dSMike Smith case ACPI_IVAR_HANDLE: 92415e32d5dSMike Smith ad->ad_handle = (ACPI_HANDLE)value; 92515e32d5dSMike Smith break; 92615e32d5dSMike Smith case ACPI_IVAR_MAGIC: 92770fa7bc0SNate Lawson ad->ad_magic = (uintptr_t)value; 92815e32d5dSMike Smith break; 92915e32d5dSMike Smith case ACPI_IVAR_PRIVATE: 93015e32d5dSMike Smith ad->ad_private = (void *)value; 93115e32d5dSMike Smith break; 932d4b9ff91SNate Lawson case ACPI_IVAR_FLAGS: 933d4b9ff91SNate Lawson ad->ad_flags = (int)value; 934d4b9ff91SNate Lawson break; 93515e32d5dSMike Smith default: 9362ab060eeSWarner Losh panic("bad ivar write request (%d)", index); 93715e32d5dSMike Smith return (ENOENT); 93815e32d5dSMike Smith } 939be2b1797SNate Lawson 94015e32d5dSMike Smith return (0); 94115e32d5dSMike Smith } 94215e32d5dSMike Smith 94315e32d5dSMike Smith /* 94415e32d5dSMike Smith * Handle child resource allocation/removal 94515e32d5dSMike Smith */ 94691233413SNate Lawson static struct resource_list * 94791233413SNate Lawson acpi_get_rlist(device_t dev, device_t child) 94815e32d5dSMike Smith { 94991233413SNate Lawson struct acpi_device *ad; 95015e32d5dSMike Smith 95191233413SNate Lawson ad = device_get_ivars(child); 95291233413SNate Lawson return (&ad->ad_rl); 95315e32d5dSMike Smith } 95415e32d5dSMike Smith 9550d484d24SJohn Baldwin static int 9560d484d24SJohn Baldwin acpi_match_resource_hint(device_t dev, int type, long value) 9570d484d24SJohn Baldwin { 9580d484d24SJohn Baldwin struct acpi_device *ad = device_get_ivars(dev); 9590d484d24SJohn Baldwin struct resource_list *rl = &ad->ad_rl; 9600d484d24SJohn Baldwin struct resource_list_entry *rle; 9610d484d24SJohn Baldwin 9620d484d24SJohn Baldwin STAILQ_FOREACH(rle, rl, link) { 9630d484d24SJohn Baldwin if (rle->type != type) 9640d484d24SJohn Baldwin continue; 9650d484d24SJohn Baldwin if (rle->start <= value && rle->end >= value) 9660d484d24SJohn Baldwin return (1); 9670d484d24SJohn Baldwin } 9680d484d24SJohn Baldwin return (0); 9690d484d24SJohn Baldwin } 9700d484d24SJohn Baldwin 9710d484d24SJohn Baldwin /* 9720d484d24SJohn Baldwin * Wire device unit numbers based on resource matches in hints. 9730d484d24SJohn Baldwin */ 9740d484d24SJohn Baldwin static void 9750d484d24SJohn Baldwin acpi_hint_device_unit(device_t acdev, device_t child, const char *name, 9760d484d24SJohn Baldwin int *unitp) 9770d484d24SJohn Baldwin { 9780d484d24SJohn Baldwin const char *s; 9790d484d24SJohn Baldwin long value; 9800d484d24SJohn Baldwin int line, matches, unit; 9810d484d24SJohn Baldwin 9820d484d24SJohn Baldwin /* 9830d484d24SJohn Baldwin * Iterate over all the hints for the devices with the specified 9840d484d24SJohn Baldwin * name to see if one's resources are a subset of this device. 9850d484d24SJohn Baldwin */ 9860d484d24SJohn Baldwin line = 0; 9870d484d24SJohn Baldwin for (;;) { 9880d484d24SJohn Baldwin if (resource_find_dev(&line, name, &unit, "at", NULL) != 0) 9890d484d24SJohn Baldwin break; 9900d484d24SJohn Baldwin 9910d484d24SJohn Baldwin /* Must have an "at" for acpi or isa. */ 9920d484d24SJohn Baldwin resource_string_value(name, unit, "at", &s); 9930d484d24SJohn Baldwin if (!(strcmp(s, "acpi0") == 0 || strcmp(s, "acpi") == 0 || 9940d484d24SJohn Baldwin strcmp(s, "isa0") == 0 || strcmp(s, "isa") == 0)) 9950d484d24SJohn Baldwin continue; 9960d484d24SJohn Baldwin 9970d484d24SJohn Baldwin /* 9980d484d24SJohn Baldwin * Check for matching resources. We must have at least one, 9990d484d24SJohn Baldwin * and all resources specified have to match. 10000d484d24SJohn Baldwin * 10010d484d24SJohn Baldwin * XXX: We may want to revisit this to be more lenient and wire 10020d484d24SJohn Baldwin * as long as it gets one match. 10030d484d24SJohn Baldwin */ 10040d484d24SJohn Baldwin matches = 0; 10050d484d24SJohn Baldwin if (resource_long_value(name, unit, "port", &value) == 0) { 10060d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IOPORT, value)) 10070d484d24SJohn Baldwin matches++; 10080d484d24SJohn Baldwin else 10090d484d24SJohn Baldwin continue; 10100d484d24SJohn Baldwin } 10110d484d24SJohn Baldwin if (resource_long_value(name, unit, "maddr", &value) == 0) { 10120d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_MEMORY, value)) 10130d484d24SJohn Baldwin matches++; 10140d484d24SJohn Baldwin else 10150d484d24SJohn Baldwin continue; 10160d484d24SJohn Baldwin } 10170d484d24SJohn Baldwin if (resource_long_value(name, unit, "irq", &value) == 0) { 10180d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_IRQ, value)) 10190d484d24SJohn Baldwin matches++; 10200d484d24SJohn Baldwin else 10210d484d24SJohn Baldwin continue; 10220d484d24SJohn Baldwin } 10230d484d24SJohn Baldwin if (resource_long_value(name, unit, "drq", &value) == 0) { 10240d484d24SJohn Baldwin if (acpi_match_resource_hint(child, SYS_RES_DRQ, value)) 10250d484d24SJohn Baldwin matches++; 10260d484d24SJohn Baldwin else 10270d484d24SJohn Baldwin continue; 10280d484d24SJohn Baldwin } 10290d484d24SJohn Baldwin 10300d484d24SJohn Baldwin if (matches > 0) { 10310d484d24SJohn Baldwin /* We have a winner! */ 10320d484d24SJohn Baldwin *unitp = unit; 10330d484d24SJohn Baldwin break; 10340d484d24SJohn Baldwin } 10350d484d24SJohn Baldwin } 10360d484d24SJohn Baldwin } 10370d484d24SJohn Baldwin 1038adad4744SNate Lawson /* 1039adad4744SNate Lawson * Pre-allocate/manage all memory and IO resources. Since rman can't handle 1040adad4744SNate Lawson * duplicates, we merge any in the sysresource attach routine. 1041adad4744SNate Lawson */ 1042adad4744SNate Lawson static int 1043adad4744SNate Lawson acpi_sysres_alloc(device_t dev) 1044adad4744SNate Lawson { 1045adad4744SNate Lawson struct resource *res; 1046adad4744SNate Lawson struct resource_list *rl; 1047adad4744SNate Lawson struct resource_list_entry *rle; 1048adad4744SNate Lawson struct rman *rm; 1049da72d149SNate Lawson char *sysres_ids[] = { "PNP0C01", "PNP0C02", NULL }; 1050da72d149SNate Lawson device_t *children; 1051da72d149SNate Lawson int child_count, i; 1052da72d149SNate Lawson 1053da72d149SNate Lawson /* 1054da72d149SNate Lawson * Probe/attach any sysresource devices. This would be unnecessary if we 1055da72d149SNate Lawson * had multi-pass probe/attach. 1056da72d149SNate Lawson */ 1057da72d149SNate Lawson if (device_get_children(dev, &children, &child_count) != 0) 1058da72d149SNate Lawson return (ENXIO); 1059da72d149SNate Lawson for (i = 0; i < child_count; i++) { 1060da72d149SNate Lawson if (ACPI_ID_PROBE(dev, children[i], sysres_ids) != NULL) 1061da72d149SNate Lawson device_probe_and_attach(children[i]); 1062da72d149SNate Lawson } 1063da72d149SNate Lawson free(children, M_TEMP); 1064adad4744SNate Lawson 1065adad4744SNate Lawson rl = BUS_GET_RESOURCE_LIST(device_get_parent(dev), dev); 1066be1bf4d2SPoul-Henning Kamp STAILQ_FOREACH(rle, rl, link) { 1067adad4744SNate Lawson if (rle->res != NULL) { 1068adad4744SNate Lawson device_printf(dev, "duplicate resource for %lx\n", rle->start); 1069adad4744SNate Lawson continue; 1070adad4744SNate Lawson } 1071adad4744SNate Lawson 1072adad4744SNate Lawson /* Only memory and IO resources are valid here. */ 1073adad4744SNate Lawson switch (rle->type) { 1074adad4744SNate Lawson case SYS_RES_IOPORT: 1075adad4744SNate Lawson rm = &acpi_rman_io; 1076adad4744SNate Lawson break; 1077adad4744SNate Lawson case SYS_RES_MEMORY: 1078adad4744SNate Lawson rm = &acpi_rman_mem; 1079adad4744SNate Lawson break; 1080adad4744SNate Lawson default: 1081adad4744SNate Lawson continue; 1082adad4744SNate Lawson } 1083adad4744SNate Lawson 1084adad4744SNate Lawson /* Pre-allocate resource and add to our rman pool. */ 1085adad4744SNate Lawson res = BUS_ALLOC_RESOURCE(device_get_parent(dev), dev, rle->type, 1086adad4744SNate Lawson &rle->rid, rle->start, rle->start + rle->count - 1, rle->count, 0); 1087adad4744SNate Lawson if (res != NULL) { 1088adad4744SNate Lawson rman_manage_region(rm, rman_get_start(res), rman_get_end(res)); 1089adad4744SNate Lawson rle->res = res; 1090adad4744SNate Lawson } else 1091adad4744SNate Lawson device_printf(dev, "reservation of %lx, %lx (%d) failed\n", 1092adad4744SNate Lawson rle->start, rle->count, rle->type); 1093adad4744SNate Lawson } 1094adad4744SNate Lawson return (0); 1095adad4744SNate Lawson } 1096adad4744SNate Lawson 109715e32d5dSMike Smith static struct resource * 109815e32d5dSMike Smith acpi_alloc_resource(device_t bus, device_t child, int type, int *rid, 109915e32d5dSMike Smith u_long start, u_long end, u_long count, u_int flags) 110015e32d5dSMike Smith { 110195957f62SJohn Baldwin ACPI_RESOURCE ares; 110215e32d5dSMike Smith struct acpi_device *ad = device_get_ivars(child); 110315e32d5dSMike Smith struct resource_list *rl = &ad->ad_rl; 110491233413SNate Lawson struct resource_list_entry *rle; 110591233413SNate Lawson struct resource *res; 110691233413SNate Lawson struct rman *rm; 110715e32d5dSMike Smith 110815e2f34fSNate Lawson res = NULL; 1109397c30a8SJohn Baldwin 1110397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1111397c30a8SJohn Baldwin switch (type) { 1112397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1113397c30a8SJohn Baldwin rm = &acpi_rman_io; 1114397c30a8SJohn Baldwin break; 1115397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1116397c30a8SJohn Baldwin rm = &acpi_rman_mem; 1117397c30a8SJohn Baldwin break; 1118397c30a8SJohn Baldwin default: 1119397c30a8SJohn Baldwin rm = NULL; 1120397c30a8SJohn Baldwin } 1121397c30a8SJohn Baldwin 112215e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 112315e2f34fSNate Lawson 112491233413SNate Lawson /* 112591233413SNate Lawson * If this is an allocation of the "default" range for a given RID, and 112691233413SNate Lawson * we know what the resources for this device are (i.e., they're on the 112791233413SNate Lawson * child's resource list), use those start/end values. 112891233413SNate Lawson */ 1129f09aa88cSWarner Losh if (bus == device_get_parent(child) && start == 0UL && end == ~0UL) { 113091233413SNate Lawson rle = resource_list_find(rl, type, *rid); 113191233413SNate Lawson if (rle == NULL) 113215e2f34fSNate Lawson goto out; 113391233413SNate Lawson start = rle->start; 113491233413SNate Lawson end = rle->end; 113591233413SNate Lawson count = rle->count; 113691233413SNate Lawson } 113791233413SNate Lawson 1138397c30a8SJohn Baldwin /* 1139397c30a8SJohn Baldwin * If this is an allocation of a specific range, see if we can satisfy 1140397c30a8SJohn Baldwin * the request from our system resource regions. If we can't, pass the 1141397c30a8SJohn Baldwin * request up to the parent. 1142397c30a8SJohn Baldwin */ 1143147c0ad0SJohn Baldwin if (start + count - 1 == end && rm != NULL) 1144397c30a8SJohn Baldwin res = rman_reserve_resource(rm, start, end, count, flags & ~RF_ACTIVE, 1145397c30a8SJohn Baldwin child); 1146397c30a8SJohn Baldwin if (res == NULL) { 114795957f62SJohn Baldwin res = BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, rid, 114895957f62SJohn Baldwin start, end, count, flags); 114995957f62SJohn Baldwin } else { 1150c3f861f4SWarner Losh rman_set_rid(res, *rid); 115191233413SNate Lawson 115291233413SNate Lawson /* If requested, activate the resource using the parent's method. */ 115395957f62SJohn Baldwin if (flags & RF_ACTIVE) 115491233413SNate Lawson if (bus_activate_resource(child, type, *rid, res) != 0) { 115591233413SNate Lawson rman_release_resource(res); 115615e2f34fSNate Lawson res = NULL; 115715e2f34fSNate Lawson goto out; 115891233413SNate Lawson } 115995957f62SJohn Baldwin } 116091233413SNate Lawson 116195957f62SJohn Baldwin if (res != NULL && device_get_parent(child) == bus) 116295957f62SJohn Baldwin switch (type) { 116395957f62SJohn Baldwin case SYS_RES_IRQ: 116495957f62SJohn Baldwin /* 116595957f62SJohn Baldwin * Since bus_config_intr() takes immediate effect, we cannot 116695957f62SJohn Baldwin * configure the interrupt associated with a device when we 116795957f62SJohn Baldwin * parse the resources but have to defer it until a driver 116895957f62SJohn Baldwin * actually allocates the interrupt via bus_alloc_resource(). 116995957f62SJohn Baldwin * 117095957f62SJohn Baldwin * XXX: Should we handle the lookup failing? 117195957f62SJohn Baldwin */ 117295957f62SJohn Baldwin if (ACPI_SUCCESS(acpi_lookup_irq_resource(child, *rid, res, &ares))) 117395957f62SJohn Baldwin acpi_config_intr(child, &ares); 117495957f62SJohn Baldwin break; 117595957f62SJohn Baldwin } 117615e2f34fSNate Lawson 117715e2f34fSNate Lawson out: 117815e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 117991233413SNate Lawson return (res); 118015e32d5dSMike Smith } 118115e32d5dSMike Smith 118215e32d5dSMike Smith static int 118391233413SNate Lawson acpi_release_resource(device_t bus, device_t child, int type, int rid, 118491233413SNate Lawson struct resource *r) 118515e32d5dSMike Smith { 1186397c30a8SJohn Baldwin struct rman *rm; 118791233413SNate Lawson int ret; 118815e32d5dSMike Smith 1189397c30a8SJohn Baldwin /* We only handle memory and IO resources through rman. */ 1190397c30a8SJohn Baldwin switch (type) { 1191397c30a8SJohn Baldwin case SYS_RES_IOPORT: 1192397c30a8SJohn Baldwin rm = &acpi_rman_io; 1193397c30a8SJohn Baldwin break; 1194397c30a8SJohn Baldwin case SYS_RES_MEMORY: 1195397c30a8SJohn Baldwin rm = &acpi_rman_mem; 1196397c30a8SJohn Baldwin break; 1197397c30a8SJohn Baldwin default: 1198397c30a8SJohn Baldwin rm = NULL; 1199397c30a8SJohn Baldwin } 1200397c30a8SJohn Baldwin 120115e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 120215e2f34fSNate Lawson 120391233413SNate Lawson /* 1204397c30a8SJohn Baldwin * If this resource belongs to one of our internal managers, 1205397c30a8SJohn Baldwin * deactivate it and release it to the local pool. If it doesn't, 1206397c30a8SJohn Baldwin * pass this request up to the parent. 120791233413SNate Lawson */ 1208397c30a8SJohn Baldwin if (rm != NULL && rman_is_region_manager(r, rm)) { 120991233413SNate Lawson if (rman_get_flags(r) & RF_ACTIVE) { 121091233413SNate Lawson ret = bus_deactivate_resource(child, type, rid, r); 121191233413SNate Lawson if (ret != 0) 121215e2f34fSNate Lawson goto out; 121391233413SNate Lawson } 121491233413SNate Lawson ret = rman_release_resource(r); 121591233413SNate Lawson } else 121691233413SNate Lawson ret = BUS_RELEASE_RESOURCE(device_get_parent(bus), child, type, rid, r); 121791233413SNate Lawson 121815e2f34fSNate Lawson out: 121915e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 122091233413SNate Lawson return (ret); 122115e32d5dSMike Smith } 122215e32d5dSMike Smith 12238b28b622SNate Lawson static void 12248b28b622SNate Lawson acpi_delete_resource(device_t bus, device_t child, int type, int rid) 12258b28b622SNate Lawson { 12268b28b622SNate Lawson struct resource_list *rl; 12278b28b622SNate Lawson 12288b28b622SNate Lawson rl = acpi_get_rlist(bus, child); 12298b28b622SNate Lawson resource_list_delete(rl, type, rid); 12308b28b622SNate Lawson } 12318b28b622SNate Lawson 1232dc750869SNate Lawson /* Allocate an IO port or memory resource, given its GAS. */ 1233e1c4bf3fSNate Lawson int 1234e1c4bf3fSNate Lawson acpi_bus_alloc_gas(device_t dev, int *type, int *rid, ACPI_GENERIC_ADDRESS *gas, 1235907b6777SNate Lawson struct resource **res, u_int flags) 1236dc750869SNate Lawson { 1237e1c4bf3fSNate Lawson int error, res_type; 1238dc750869SNate Lawson 1239e1c4bf3fSNate Lawson error = ENOMEM; 12408f118e25SNate Lawson if (type == NULL || rid == NULL || gas == NULL || res == NULL) 1241e1c4bf3fSNate Lawson return (EINVAL); 1242dc750869SNate Lawson 12438f118e25SNate Lawson /* We only support memory and IO spaces. */ 12442be4e471SJung-uk Kim switch (gas->SpaceId) { 1245dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_MEMORY: 1246e1c4bf3fSNate Lawson res_type = SYS_RES_MEMORY; 1247dc750869SNate Lawson break; 1248dc750869SNate Lawson case ACPI_ADR_SPACE_SYSTEM_IO: 1249e1c4bf3fSNate Lawson res_type = SYS_RES_IOPORT; 1250dc750869SNate Lawson break; 1251dc750869SNate Lawson default: 1252e1c4bf3fSNate Lawson return (EOPNOTSUPP); 1253dc750869SNate Lawson } 1254dc750869SNate Lawson 12552fe912dfSNate Lawson /* 1256f45fc848SNate Lawson * If the register width is less than 8, assume the BIOS author means 1257f45fc848SNate Lawson * it is a bit field and just allocate a byte. 12582fe912dfSNate Lawson */ 12592be4e471SJung-uk Kim if (gas->BitWidth && gas->BitWidth < 8) 12602be4e471SJung-uk Kim gas->BitWidth = 8; 12612fe912dfSNate Lawson 12628f118e25SNate Lawson /* Validate the address after we're sure we support the space. */ 12632be4e471SJung-uk Kim if (gas->Address == 0 || gas->BitWidth == 0) 12648f118e25SNate Lawson return (EINVAL); 12658f118e25SNate Lawson 1266e1c4bf3fSNate Lawson bus_set_resource(dev, res_type, *rid, gas->Address, 12672be4e471SJung-uk Kim gas->BitWidth / 8); 1268907b6777SNate Lawson *res = bus_alloc_resource_any(dev, res_type, rid, RF_ACTIVE | flags); 1269e1c4bf3fSNate Lawson if (*res != NULL) { 1270e1c4bf3fSNate Lawson *type = res_type; 1271e1c4bf3fSNate Lawson error = 0; 1272ca2c69c8SNate Lawson } else 1273ca2c69c8SNate Lawson bus_delete_resource(dev, res_type, *rid); 1274ca2c69c8SNate Lawson 1275e1c4bf3fSNate Lawson return (error); 1276dc750869SNate Lawson } 1277dc750869SNate Lawson 1278c796e27bSNate Lawson /* Probe _HID and _CID for compatible ISA PNP ids. */ 12791e4925e8SNate Lawson static uint32_t 128032d18aa5SMike Smith acpi_isa_get_logicalid(device_t dev) 1281bc0f2195SMike Smith { 12821e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 12831e4925e8SNate Lawson ACPI_BUFFER buf; 1284bc0f2195SMike Smith ACPI_HANDLE h; 1285bc0f2195SMike Smith ACPI_STATUS error; 1286bc0f2195SMike Smith u_int32_t pnpid; 128732d18aa5SMike Smith 1288b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 128932d18aa5SMike Smith 129032d18aa5SMike Smith pnpid = 0; 1291bd189fe7SNate Lawson buf.Pointer = NULL; 1292bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1293bd189fe7SNate Lawson 12946fca9360SNate Lawson /* Fetch and validate the HID. */ 129532d18aa5SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1296bd189fe7SNate Lawson goto out; 12976fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 12986fca9360SNate Lawson if (ACPI_FAILURE(error)) 1299bd189fe7SNate Lawson goto out; 13001e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 130132d18aa5SMike Smith 13021e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_HID) != 0) 13031e4925e8SNate Lawson pnpid = PNP_EISAID(devinfo->HardwareId.Value); 1304be2b1797SNate Lawson 1305bd189fe7SNate Lawson out: 1306bd189fe7SNate Lawson if (buf.Pointer != NULL) 13071e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 130832d18aa5SMike Smith return_VALUE (pnpid); 130932d18aa5SMike Smith } 131032d18aa5SMike Smith 13111e4925e8SNate Lawson static int 13121e4925e8SNate Lawson acpi_isa_get_compatid(device_t dev, uint32_t *cids, int count) 1313b2566207STakanori Watanabe { 13141e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 13151e4925e8SNate Lawson ACPI_BUFFER buf; 1316b2566207STakanori Watanabe ACPI_HANDLE h; 1317b2566207STakanori Watanabe ACPI_STATUS error; 13181e4925e8SNate Lawson uint32_t *pnpid; 13191e4925e8SNate Lawson int valid, i; 1320b2566207STakanori Watanabe 1321b2566207STakanori Watanabe ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1322b2566207STakanori Watanabe 13231e4925e8SNate Lawson pnpid = cids; 13241e4925e8SNate Lawson valid = 0; 1325bd189fe7SNate Lawson buf.Pointer = NULL; 1326bd189fe7SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 1327bd189fe7SNate Lawson 13281e4925e8SNate Lawson /* Fetch and validate the CID */ 1329b2566207STakanori Watanabe if ((h = acpi_get_handle(dev)) == NULL) 1330bd189fe7SNate Lawson goto out; 13311e4925e8SNate Lawson error = AcpiGetObjectInfo(h, &buf); 13321e4925e8SNate Lawson if (ACPI_FAILURE(error)) 1333bd189fe7SNate Lawson goto out; 13341e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 13351e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_CID) == 0) 1336b2566207STakanori Watanabe goto out; 1337b2566207STakanori Watanabe 13381e4925e8SNate Lawson if (devinfo->CompatibilityId.Count < count) 13391e4925e8SNate Lawson count = devinfo->CompatibilityId.Count; 13401e4925e8SNate Lawson for (i = 0; i < count; i++) { 13411e4925e8SNate Lawson if (strncmp(devinfo->CompatibilityId.Id[i].Value, "PNP", 3) != 0) 13421e4925e8SNate Lawson continue; 13431e4925e8SNate Lawson *pnpid++ = PNP_EISAID(devinfo->CompatibilityId.Id[i].Value); 13441e4925e8SNate Lawson valid++; 1345b2566207STakanori Watanabe } 1346b2566207STakanori Watanabe 13471e4925e8SNate Lawson out: 1348bd189fe7SNate Lawson if (buf.Pointer != NULL) 13491e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 13501e4925e8SNate Lawson return_VALUE (valid); 13511e4925e8SNate Lawson } 1352b2566207STakanori Watanabe 1353ce619b2eSNate Lawson static char * 1354ce619b2eSNate Lawson acpi_device_id_probe(device_t bus, device_t dev, char **ids) 1355ce619b2eSNate Lawson { 1356ce619b2eSNate Lawson ACPI_HANDLE h; 1357ce619b2eSNate Lawson int i; 1358ce619b2eSNate Lawson 1359ce619b2eSNate Lawson h = acpi_get_handle(dev); 1360ce619b2eSNate Lawson if (ids == NULL || h == NULL || acpi_get_type(dev) != ACPI_TYPE_DEVICE) 1361ce619b2eSNate Lawson return (NULL); 1362ce619b2eSNate Lawson 1363ce619b2eSNate Lawson /* Try to match one of the array of IDs with a HID or CID. */ 1364ce619b2eSNate Lawson for (i = 0; ids[i] != NULL; i++) { 1365ce619b2eSNate Lawson if (acpi_MatchHid(h, ids[i])) 1366ce619b2eSNate Lawson return (ids[i]); 1367ce619b2eSNate Lawson } 1368ce619b2eSNate Lawson return (NULL); 1369ce619b2eSNate Lawson } 1370ce619b2eSNate Lawson 1371ce619b2eSNate Lawson static ACPI_STATUS 1372ce619b2eSNate Lawson acpi_device_eval_obj(device_t bus, device_t dev, ACPI_STRING pathname, 1373ce619b2eSNate Lawson ACPI_OBJECT_LIST *parameters, ACPI_BUFFER *ret) 1374ce619b2eSNate Lawson { 1375ce619b2eSNate Lawson ACPI_HANDLE h; 1376ce619b2eSNate Lawson 1377df8d2a32SNate Lawson if (dev == NULL) 1378df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1379df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1380ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1381ce619b2eSNate Lawson return (AcpiEvaluateObject(h, pathname, parameters, ret)); 1382ce619b2eSNate Lawson } 1383ce619b2eSNate Lawson 138410ce62b9SNate Lawson static int 138510ce62b9SNate Lawson acpi_device_pwr_for_sleep(device_t bus, device_t dev, int *dstate) 138610ce62b9SNate Lawson { 138710ce62b9SNate Lawson struct acpi_softc *sc; 138810ce62b9SNate Lawson ACPI_HANDLE handle; 138910ce62b9SNate Lawson ACPI_STATUS status; 139010ce62b9SNate Lawson char sxd[8]; 139110ce62b9SNate Lawson int error; 139210ce62b9SNate Lawson 139310ce62b9SNate Lawson sc = device_get_softc(bus); 139410ce62b9SNate Lawson handle = acpi_get_handle(dev); 139510ce62b9SNate Lawson 139610ce62b9SNate Lawson /* 139710ce62b9SNate Lawson * XXX If we find these devices, don't try to power them down. 139810ce62b9SNate Lawson * The serial and IRDA ports on my T23 hang the system when 139910ce62b9SNate Lawson * set to D3 and it appears that such legacy devices may 140010ce62b9SNate Lawson * need special handling in their drivers. 140110ce62b9SNate Lawson */ 140210ce62b9SNate Lawson if (handle == NULL || 140310ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0500") || 140410ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0501") || 140510ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0502") || 140610ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0510") || 140710ce62b9SNate Lawson acpi_MatchHid(handle, "PNP0511")) 140810ce62b9SNate Lawson return (ENXIO); 140910ce62b9SNate Lawson 141010ce62b9SNate Lawson /* 141110ce62b9SNate Lawson * Override next state with the value from _SxD, if present. If no 141210ce62b9SNate Lawson * dstate argument was provided, don't fetch the return value. 141310ce62b9SNate Lawson */ 141410ce62b9SNate Lawson snprintf(sxd, sizeof(sxd), "_S%dD", sc->acpi_sstate); 141510ce62b9SNate Lawson if (dstate) 141610ce62b9SNate Lawson status = acpi_GetInteger(handle, sxd, dstate); 141710ce62b9SNate Lawson else 141810ce62b9SNate Lawson status = AcpiEvaluateObject(handle, sxd, NULL, NULL); 141910ce62b9SNate Lawson 142010ce62b9SNate Lawson switch (status) { 142110ce62b9SNate Lawson case AE_OK: 142210ce62b9SNate Lawson error = 0; 142310ce62b9SNate Lawson break; 142410ce62b9SNate Lawson case AE_NOT_FOUND: 142510ce62b9SNate Lawson error = ESRCH; 142610ce62b9SNate Lawson break; 142710ce62b9SNate Lawson default: 142810ce62b9SNate Lawson error = ENXIO; 142910ce62b9SNate Lawson break; 143010ce62b9SNate Lawson } 143110ce62b9SNate Lawson 143210ce62b9SNate Lawson return (error); 143310ce62b9SNate Lawson } 143410ce62b9SNate Lawson 1435df8d2a32SNate Lawson /* Callback arg for our implementation of walking the namespace. */ 1436df8d2a32SNate Lawson struct acpi_device_scan_ctx { 1437df8d2a32SNate Lawson acpi_scan_cb_t user_fn; 1438df8d2a32SNate Lawson void *arg; 1439df8d2a32SNate Lawson ACPI_HANDLE parent; 1440df8d2a32SNate Lawson }; 1441df8d2a32SNate Lawson 1442ce619b2eSNate Lawson static ACPI_STATUS 1443df8d2a32SNate Lawson acpi_device_scan_cb(ACPI_HANDLE h, UINT32 level, void *arg, void **retval) 1444df8d2a32SNate Lawson { 1445df8d2a32SNate Lawson struct acpi_device_scan_ctx *ctx; 1446df8d2a32SNate Lawson device_t dev, old_dev; 1447df8d2a32SNate Lawson ACPI_STATUS status; 1448df8d2a32SNate Lawson ACPI_OBJECT_TYPE type; 1449df8d2a32SNate Lawson 1450df8d2a32SNate Lawson /* 1451df8d2a32SNate Lawson * Skip this device if we think we'll have trouble with it or it is 1452df8d2a32SNate Lawson * the parent where the scan began. 1453df8d2a32SNate Lawson */ 1454df8d2a32SNate Lawson ctx = (struct acpi_device_scan_ctx *)arg; 1455df8d2a32SNate Lawson if (acpi_avoid(h) || h == ctx->parent) 1456df8d2a32SNate Lawson return (AE_OK); 1457df8d2a32SNate Lawson 1458df8d2a32SNate Lawson /* If this is not a valid device type (e.g., a method), skip it. */ 1459df8d2a32SNate Lawson if (ACPI_FAILURE(AcpiGetType(h, &type))) 1460df8d2a32SNate Lawson return (AE_OK); 1461df8d2a32SNate Lawson if (type != ACPI_TYPE_DEVICE && type != ACPI_TYPE_PROCESSOR && 1462df8d2a32SNate Lawson type != ACPI_TYPE_THERMAL && type != ACPI_TYPE_POWER) 1463df8d2a32SNate Lawson return (AE_OK); 1464df8d2a32SNate Lawson 1465df8d2a32SNate Lawson /* 1466df8d2a32SNate Lawson * Call the user function with the current device. If it is unchanged 1467df8d2a32SNate Lawson * afterwards, return. Otherwise, we update the handle to the new dev. 1468df8d2a32SNate Lawson */ 1469df8d2a32SNate Lawson old_dev = acpi_get_device(h); 1470df8d2a32SNate Lawson dev = old_dev; 1471df8d2a32SNate Lawson status = ctx->user_fn(h, &dev, level, ctx->arg); 1472df8d2a32SNate Lawson if (ACPI_FAILURE(status) || old_dev == dev) 1473df8d2a32SNate Lawson return (status); 1474df8d2a32SNate Lawson 1475df8d2a32SNate Lawson /* Remove the old child and its connection to the handle. */ 1476df8d2a32SNate Lawson if (old_dev != NULL) { 1477df8d2a32SNate Lawson device_delete_child(device_get_parent(old_dev), old_dev); 1478df8d2a32SNate Lawson AcpiDetachData(h, acpi_fake_objhandler); 1479df8d2a32SNate Lawson } 1480df8d2a32SNate Lawson 1481df8d2a32SNate Lawson /* Recreate the handle association if the user created a device. */ 1482df8d2a32SNate Lawson if (dev != NULL) 1483df8d2a32SNate Lawson AcpiAttachData(h, acpi_fake_objhandler, dev); 1484df8d2a32SNate Lawson 1485df8d2a32SNate Lawson return (AE_OK); 1486df8d2a32SNate Lawson } 1487df8d2a32SNate Lawson 1488df8d2a32SNate Lawson static ACPI_STATUS 1489df8d2a32SNate Lawson acpi_device_scan_children(device_t bus, device_t dev, int max_depth, 1490df8d2a32SNate Lawson acpi_scan_cb_t user_fn, void *arg) 1491ce619b2eSNate Lawson { 1492ce619b2eSNate Lawson ACPI_HANDLE h; 1493df8d2a32SNate Lawson struct acpi_device_scan_ctx ctx; 1494ce619b2eSNate Lawson 1495df8d2a32SNate Lawson if (acpi_disabled("children")) 1496df8d2a32SNate Lawson return (AE_OK); 1497df8d2a32SNate Lawson 1498df8d2a32SNate Lawson if (dev == NULL) 1499df8d2a32SNate Lawson h = ACPI_ROOT_OBJECT; 1500df8d2a32SNate Lawson else if ((h = acpi_get_handle(dev)) == NULL) 1501ce619b2eSNate Lawson return (AE_BAD_PARAMETER); 1502df8d2a32SNate Lawson ctx.user_fn = user_fn; 1503df8d2a32SNate Lawson ctx.arg = arg; 1504df8d2a32SNate Lawson ctx.parent = h; 1505df8d2a32SNate Lawson return (AcpiWalkNamespace(ACPI_TYPE_ANY, h, max_depth, 1506df8d2a32SNate Lawson acpi_device_scan_cb, &ctx, NULL)); 1507ce619b2eSNate Lawson } 1508ce619b2eSNate Lawson 150910ce62b9SNate Lawson /* 151010ce62b9SNate Lawson * Even though ACPI devices are not PCI, we use the PCI approach for setting 151110ce62b9SNate Lawson * device power states since it's close enough to ACPI. 151210ce62b9SNate Lawson */ 151310ce62b9SNate Lawson static int 151410ce62b9SNate Lawson acpi_set_powerstate_method(device_t bus, device_t child, int state) 151510ce62b9SNate Lawson { 151610ce62b9SNate Lawson ACPI_HANDLE h; 151710ce62b9SNate Lawson ACPI_STATUS status; 151810ce62b9SNate Lawson int error; 151910ce62b9SNate Lawson 152010ce62b9SNate Lawson error = 0; 152110ce62b9SNate Lawson h = acpi_get_handle(child); 152210ce62b9SNate Lawson if (state < ACPI_STATE_D0 || state > ACPI_STATE_D3) 152310ce62b9SNate Lawson return (EINVAL); 152410ce62b9SNate Lawson if (h == NULL) 152510ce62b9SNate Lawson return (0); 152610ce62b9SNate Lawson 152710ce62b9SNate Lawson /* Ignore errors if the power methods aren't present. */ 152810ce62b9SNate Lawson status = acpi_pwr_switch_consumer(h, state); 152910ce62b9SNate Lawson if (ACPI_FAILURE(status) && status != AE_NOT_FOUND 153010ce62b9SNate Lawson && status != AE_BAD_PARAMETER) 153110ce62b9SNate Lawson device_printf(bus, "failed to set ACPI power state D%d on %s: %s\n", 153210ce62b9SNate Lawson state, acpi_name(h), AcpiFormatException(status)); 153310ce62b9SNate Lawson 153410ce62b9SNate Lawson return (error); 153510ce62b9SNate Lawson } 153610ce62b9SNate Lawson 153732d18aa5SMike Smith static int 153832d18aa5SMike Smith acpi_isa_pnp_probe(device_t bus, device_t child, struct isa_pnp_id *ids) 153932d18aa5SMike Smith { 15401e4925e8SNate Lawson int result, cid_count, i; 15411e4925e8SNate Lawson uint32_t lid, cids[8]; 1542bc0f2195SMike Smith 1543b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 1544bc0f2195SMike Smith 1545bc0f2195SMike Smith /* 1546bc0f2195SMike Smith * ISA-style drivers attached to ACPI may persist and 1547bc0f2195SMike Smith * probe manually if we return ENOENT. We never want 1548bc0f2195SMike Smith * that to happen, so don't ever return it. 1549bc0f2195SMike Smith */ 1550bc0f2195SMike Smith result = ENXIO; 1551bc0f2195SMike Smith 1552be2b1797SNate Lawson /* Scan the supplied IDs for a match */ 1553b2566207STakanori Watanabe lid = acpi_isa_get_logicalid(child); 15541e4925e8SNate Lawson cid_count = acpi_isa_get_compatid(child, cids, 8); 1555bc0f2195SMike Smith while (ids && ids->ip_id) { 15561e4925e8SNate Lawson if (lid == ids->ip_id) { 1557bc0f2195SMike Smith result = 0; 1558bc0f2195SMike Smith goto out; 1559bc0f2195SMike Smith } 15601e4925e8SNate Lawson for (i = 0; i < cid_count; i++) { 15611e4925e8SNate Lawson if (cids[i] == ids->ip_id) { 15621e4925e8SNate Lawson result = 0; 15631e4925e8SNate Lawson goto out; 15641e4925e8SNate Lawson } 15651e4925e8SNate Lawson } 1566bc0f2195SMike Smith ids++; 1567bc0f2195SMike Smith } 1568be2b1797SNate Lawson 1569bc0f2195SMike Smith out: 15709e0dd54fSNate Lawson if (result == 0 && ids->ip_desc) 15719e0dd54fSNate Lawson device_set_desc(child, ids->ip_desc); 15729e0dd54fSNate Lawson 1573bc0f2195SMike Smith return_VALUE (result); 1574bc0f2195SMike Smith } 1575bc0f2195SMike Smith 1576d320e05cSJohn Baldwin #if defined(__i386__) || defined(__amd64__) 1577d320e05cSJohn Baldwin /* 1578d320e05cSJohn Baldwin * Look for a MCFG table. If it is present, use the settings for 1579d320e05cSJohn Baldwin * domain (segment) 0 to setup PCI config space access via the memory 1580d320e05cSJohn Baldwin * map. 1581d320e05cSJohn Baldwin */ 1582d320e05cSJohn Baldwin static void 1583d320e05cSJohn Baldwin acpi_enable_pcie(void) 1584d320e05cSJohn Baldwin { 1585d320e05cSJohn Baldwin ACPI_TABLE_HEADER *hdr; 1586d320e05cSJohn Baldwin ACPI_MCFG_ALLOCATION *alloc, *end; 1587d320e05cSJohn Baldwin ACPI_STATUS status; 1588d320e05cSJohn Baldwin 1589d320e05cSJohn Baldwin status = AcpiGetTable(ACPI_SIG_MCFG, 1, &hdr); 1590d320e05cSJohn Baldwin if (ACPI_FAILURE(status)) 1591d320e05cSJohn Baldwin return; 1592d320e05cSJohn Baldwin 1593d320e05cSJohn Baldwin end = (ACPI_MCFG_ALLOCATION *)((char *)hdr + hdr->Length); 1594d320e05cSJohn Baldwin alloc = (ACPI_MCFG_ALLOCATION *)((ACPI_TABLE_MCFG *)hdr + 1); 1595d320e05cSJohn Baldwin while (alloc < end) { 1596d320e05cSJohn Baldwin if (alloc->PciSegment == 0) { 1597d320e05cSJohn Baldwin pcie_cfgregopen(alloc->Address, alloc->StartBusNumber, 1598d320e05cSJohn Baldwin alloc->EndBusNumber); 1599d320e05cSJohn Baldwin return; 1600d320e05cSJohn Baldwin } 1601d320e05cSJohn Baldwin alloc++; 1602d320e05cSJohn Baldwin } 1603d320e05cSJohn Baldwin } 1604d320e05cSJohn Baldwin #endif 1605d320e05cSJohn Baldwin 1606bc0f2195SMike Smith /* 160733332dc2SNate Lawson * Scan all of the ACPI namespace and attach child devices. 160815e32d5dSMike Smith * 160933332dc2SNate Lawson * We should only expect to find devices in the \_PR, \_TZ, \_SI, and 161033332dc2SNate Lawson * \_SB scopes, and \_PR and \_TZ became obsolete in the ACPI 2.0 spec. 161133332dc2SNate Lawson * However, in violation of the spec, some systems place their PCI link 161233332dc2SNate Lawson * devices in \, so we have to walk the whole namespace. We check the 161333332dc2SNate Lawson * type of namespace nodes, so this should be ok. 161415e32d5dSMike Smith */ 161515e32d5dSMike Smith static void 161615e32d5dSMike Smith acpi_probe_children(device_t bus) 161715e32d5dSMike Smith { 161815e32d5dSMike Smith 1619b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16200ae55423SMike Smith 162115e32d5dSMike Smith /* 162215e32d5dSMike Smith * Scan the namespace and insert placeholders for all the devices that 1623edc13633SNate Lawson * we find. We also probe/attach any early devices. 162415e32d5dSMike Smith * 162515e32d5dSMike Smith * Note that we use AcpiWalkNamespace rather than AcpiGetDevices because 1626be2b1797SNate Lawson * we want to create nodes for all devices, not just those that are 1627be2b1797SNate Lawson * currently present. (This assumes that we don't want to create/remove 1628be2b1797SNate Lawson * devices as they appear, which might be smarter.) 162915e32d5dSMike Smith */ 16304c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "namespace scan\n")); 163133332dc2SNate Lawson AcpiWalkNamespace(ACPI_TYPE_ANY, ACPI_ROOT_OBJECT, 100, acpi_probe_child, 1632be2b1797SNate Lawson bus, NULL); 163315e32d5dSMike Smith 1634adad4744SNate Lawson /* Pre-allocate resources for our rman from any sysresource devices. */ 1635adad4744SNate Lawson acpi_sysres_alloc(bus); 1636adad4744SNate Lawson 1637edc13633SNate Lawson /* Create any static children by calling device identify methods. */ 1638edc13633SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "device identify routines\n")); 1639edc13633SNate Lawson bus_generic_probe(bus); 1640edc13633SNate Lawson 1641edc13633SNate Lawson /* Probe/attach all children, created staticly and from the namespace. */ 16424c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "first bus_generic_attach\n")); 164315e32d5dSMike Smith bus_generic_attach(bus); 164415e32d5dSMike Smith 164515e32d5dSMike Smith /* 164615e32d5dSMike Smith * Some of these children may have attached others as part of their attach 164715e32d5dSMike Smith * process (eg. the root PCI bus driver), so rescan. 164815e32d5dSMike Smith */ 16494c1cdee6SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "second bus_generic_attach\n")); 165015e32d5dSMike Smith bus_generic_attach(bus); 16510ae55423SMike Smith 165288a79fc0SNate Lawson /* Attach wake sysctls. */ 16535c9ea25eSNate Lawson acpi_wake_sysctl_walk(bus); 165488a79fc0SNate Lawson 1655bc0f2195SMike Smith ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "done attaching children\n")); 16560ae55423SMike Smith return_VOID; 165715e32d5dSMike Smith } 165815e32d5dSMike Smith 1659b82ca9a9SNate Lawson /* 1660d227204dSJohn Baldwin * Determine the probe order for a given device. 1661b82ca9a9SNate Lawson */ 1662d227204dSJohn Baldwin static void 1663b82ca9a9SNate Lawson acpi_probe_order(ACPI_HANDLE handle, int *order) 166491233413SNate Lawson { 1665463e0f91SJohn Baldwin ACPI_OBJECT_TYPE type; 166691233413SNate Lawson 1667b82ca9a9SNate Lawson /* 1668b82ca9a9SNate Lawson * 1. I/O port and memory system resource holders 1669b82ca9a9SNate Lawson * 2. Embedded controllers (to handle early accesses) 1670b460c6f8SJohn Baldwin * 3. PCI Link Devices 1671b6618687SJohn Baldwin * 100000. CPUs 1672b82ca9a9SNate Lawson */ 1673463e0f91SJohn Baldwin AcpiGetType(handle, &type); 1674da72d149SNate Lawson if (acpi_MatchHid(handle, "PNP0C01") || acpi_MatchHid(handle, "PNP0C02")) 167591233413SNate Lawson *order = 1; 1676da72d149SNate Lawson else if (acpi_MatchHid(handle, "PNP0C09")) 167791233413SNate Lawson *order = 2; 1678da72d149SNate Lawson else if (acpi_MatchHid(handle, "PNP0C0F")) 1679b460c6f8SJohn Baldwin *order = 3; 1680d227204dSJohn Baldwin else if (type == ACPI_TYPE_PROCESSOR) 1681b6618687SJohn Baldwin *order = 100000; 168291233413SNate Lawson } 168391233413SNate Lawson 168415e32d5dSMike Smith /* 168515e32d5dSMike Smith * Evaluate a child device and determine whether we might attach a device to 168615e32d5dSMike Smith * it. 168715e32d5dSMike Smith */ 168815e32d5dSMike Smith static ACPI_STATUS 168915e32d5dSMike Smith acpi_probe_child(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 169015e32d5dSMike Smith { 169115e32d5dSMike Smith ACPI_OBJECT_TYPE type; 1692858a52f4SMitsuru IWASAKI ACPI_HANDLE h; 169333332dc2SNate Lawson device_t bus, child; 1694da72d149SNate Lawson int order; 169533332dc2SNate Lawson char *handle_str, **search; 169633332dc2SNate Lawson static char *scopes[] = {"\\_PR_", "\\_TZ_", "\\_SI_", "\\_SB_", NULL}; 169715e32d5dSMike Smith 1698b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 16990ae55423SMike Smith 1700be2b1797SNate Lawson /* Skip this device if we think we'll have trouble with it. */ 17010ae55423SMike Smith if (acpi_avoid(handle)) 17020ae55423SMike Smith return_ACPI_STATUS (AE_OK); 17030ae55423SMike Smith 170491233413SNate Lawson bus = (device_t)context; 1705b53f2771SMike Smith if (ACPI_SUCCESS(AcpiGetType(handle, &type))) { 170615e32d5dSMike Smith switch (type) { 170715e32d5dSMike Smith case ACPI_TYPE_DEVICE: 170815e32d5dSMike Smith case ACPI_TYPE_PROCESSOR: 170915e32d5dSMike Smith case ACPI_TYPE_THERMAL: 171015e32d5dSMike Smith case ACPI_TYPE_POWER: 17110ae55423SMike Smith if (acpi_disabled("children")) 17120ae55423SMike Smith break; 1713be2b1797SNate Lawson 171415e32d5dSMike Smith /* 171533332dc2SNate Lawson * Since we scan from \, be sure to skip system scope objects. 171633332dc2SNate Lawson * At least \_SB and \_TZ are detected as devices (ACPI-CA bug?) 171733332dc2SNate Lawson */ 171833332dc2SNate Lawson handle_str = acpi_name(handle); 171933332dc2SNate Lawson for (search = scopes; *search != NULL; search++) { 172033332dc2SNate Lawson if (strcmp(handle_str, *search) == 0) 172133332dc2SNate Lawson break; 172233332dc2SNate Lawson } 172333332dc2SNate Lawson if (*search != NULL) 172433332dc2SNate Lawson break; 172533332dc2SNate Lawson 172633332dc2SNate Lawson /* 1727463e0f91SJohn Baldwin * Create a placeholder device for this node. Sort the 1728463e0f91SJohn Baldwin * placeholder so that the probe/attach passes will run 1729463e0f91SJohn Baldwin * breadth-first. Orders less than ACPI_DEV_BASE_ORDER 1730463e0f91SJohn Baldwin * are reserved for special objects (i.e., system 1731b6618687SJohn Baldwin * resources). CPU devices have a very high order to 1732b6618687SJohn Baldwin * ensure they are probed after other devices. 173315e32d5dSMike Smith */ 173433332dc2SNate Lawson ACPI_DEBUG_PRINT((ACPI_DB_OBJECTS, "scanning '%s'\n", handle_str)); 1735d227204dSJohn Baldwin order = level * 10 + 100; 1736da72d149SNate Lawson acpi_probe_order(handle, &order); 173791233413SNate Lawson child = BUS_ADD_CHILD(bus, order, NULL, -1); 1738bc0f2195SMike Smith if (child == NULL) 1739bc0f2195SMike Smith break; 174059a890e6SNate Lawson 174159a890e6SNate Lawson /* Associate the handle with the device_t and vice versa. */ 174215e32d5dSMike Smith acpi_set_handle(child, handle); 174359a890e6SNate Lawson AcpiAttachData(handle, acpi_fake_objhandler, child); 1744bc0f2195SMike Smith 1745bc0f2195SMike Smith /* 1746b519f9d6SMike Smith * Check that the device is present. If it's not present, 1747b519f9d6SMike Smith * leave it disabled (so that we have a device_t attached to 1748b519f9d6SMike Smith * the handle, but we don't probe it). 1749893f750aSNate Lawson * 1750893f750aSNate Lawson * XXX PCI link devices sometimes report "present" but not 1751893f750aSNate Lawson * "functional" (i.e. if disabled). Go ahead and probe them 1752893f750aSNate Lawson * anyway since we may enable them later. 1753b519f9d6SMike Smith */ 1754858a52f4SMitsuru IWASAKI if (type == ACPI_TYPE_DEVICE && !acpi_DeviceIsPresent(child)) { 1755858a52f4SMitsuru IWASAKI /* Never disable PCI link devices. */ 1756858a52f4SMitsuru IWASAKI if (acpi_MatchHid(handle, "PNP0C0F")) 1757858a52f4SMitsuru IWASAKI break; 1758858a52f4SMitsuru IWASAKI /* 1759858a52f4SMitsuru IWASAKI * Docking stations should remain enabled since the system 1760858a52f4SMitsuru IWASAKI * may be undocked at boot. 1761858a52f4SMitsuru IWASAKI */ 1762858a52f4SMitsuru IWASAKI if (ACPI_SUCCESS(AcpiGetHandle(handle, "_DCK", &h))) 1763858a52f4SMitsuru IWASAKI break; 1764858a52f4SMitsuru IWASAKI 1765b519f9d6SMike Smith device_disable(child); 1766b519f9d6SMike Smith break; 1767b519f9d6SMike Smith } 1768b519f9d6SMike Smith 1769b519f9d6SMike Smith /* 1770bc0f2195SMike Smith * Get the device's resource settings and attach them. 1771bc0f2195SMike Smith * Note that if the device has _PRS but no _CRS, we need 1772bc0f2195SMike Smith * to decide when it's appropriate to try to configure the 1773bc0f2195SMike Smith * device. Ignore the return value here; it's OK for the 1774bc0f2195SMike Smith * device not to have any resources. 1775bc0f2195SMike Smith */ 177672ad60adSNate Lawson acpi_parse_resources(child, handle, &acpi_res_parse_set, NULL); 17770ae55423SMike Smith break; 177815e32d5dSMike Smith } 177915e32d5dSMike Smith } 1780be2b1797SNate Lawson 17810ae55423SMike Smith return_ACPI_STATUS (AE_OK); 178215e32d5dSMike Smith } 178315e32d5dSMike Smith 178459a890e6SNate Lawson /* 178559a890e6SNate Lawson * AcpiAttachData() requires an object handler but never uses it. This is a 178659a890e6SNate Lawson * placeholder object handler so we can store a device_t in an ACPI_HANDLE. 178759a890e6SNate Lawson */ 178859a890e6SNate Lawson void 178959a890e6SNate Lawson acpi_fake_objhandler(ACPI_HANDLE h, UINT32 fn, void *data) 179059a890e6SNate Lawson { 179159a890e6SNate Lawson } 179259a890e6SNate Lawson 179315e32d5dSMike Smith static void 179415e32d5dSMike Smith acpi_shutdown_final(void *arg, int howto) 179515e32d5dSMike Smith { 1796d85e6785SNate Lawson struct acpi_softc *sc; 1797d33f4987STakanori Watanabe ACPI_STATUS status; 1798eeb3a05fSNate Lawson 1799eeb3a05fSNate Lawson /* 180080f0e4c2SNate Lawson * XXX Shutdown code should only run on the BSP (cpuid 0). 180180f0e4c2SNate Lawson * Some chipsets do not power off the system correctly if called from 180280f0e4c2SNate Lawson * an AP. 1803eeb3a05fSNate Lawson */ 1804d85e6785SNate Lawson sc = arg; 1805eeb3a05fSNate Lawson if ((howto & RB_POWEROFF) != 0) { 1806904bf0c2SNate Lawson status = AcpiEnterSleepStatePrep(ACPI_STATE_S5); 1807904bf0c2SNate Lawson if (ACPI_FAILURE(status)) { 1808904bf0c2SNate Lawson printf("AcpiEnterSleepStatePrep failed - %s\n", 1809904bf0c2SNate Lawson AcpiFormatException(status)); 1810904bf0c2SNate Lawson return; 1811904bf0c2SNate Lawson } 1812eeb3a05fSNate Lawson printf("Powering system off using ACPI\n"); 181355398047SNate Lawson ACPI_DISABLE_IRQS(); 1814865b8d0bSNate Lawson status = AcpiEnterSleepState(ACPI_STATE_S5); 1815be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 1816bfae45aaSMike Smith printf("ACPI power-off failed - %s\n", AcpiFormatException(status)); 181715e32d5dSMike Smith } else { 181815e32d5dSMike Smith DELAY(1000000); 181915e32d5dSMike Smith printf("ACPI power-off failed - timeout\n"); 182015e32d5dSMike Smith } 18212be4e471SJung-uk Kim } else if ((howto & RB_HALT) == 0 && 18222be4e471SJung-uk Kim (AcpiGbl_FADT.Flags & ACPI_FADT_RESET_REGISTER) && 1823d1b16e18SNate Lawson sc->acpi_handle_reboot) { 1824d85e6785SNate Lawson /* Reboot using the reset register. */ 182587a500cdSNate Lawson status = AcpiHwLowLevelWrite( 18262be4e471SJung-uk Kim AcpiGbl_FADT.ResetRegister.BitWidth, 18272be4e471SJung-uk Kim AcpiGbl_FADT.ResetValue, &AcpiGbl_FADT.ResetRegister); 182887a500cdSNate Lawson if (ACPI_FAILURE(status)) { 182987a500cdSNate Lawson printf("ACPI reset failed - %s\n", AcpiFormatException(status)); 183087a500cdSNate Lawson } else { 183187a500cdSNate Lawson DELAY(1000000); 183287a500cdSNate Lawson printf("ACPI reset failed - timeout\n"); 183387a500cdSNate Lawson } 1834d85e6785SNate Lawson } else if (sc->acpi_do_disable && panicstr == NULL) { 1835d85e6785SNate Lawson /* 1836d85e6785SNate Lawson * Only disable ACPI if the user requested. On some systems, writing 1837d85e6785SNate Lawson * the disable value to SMI_CMD hangs the system. 1838d85e6785SNate Lawson */ 183980f0e4c2SNate Lawson printf("Shutting down ACPI\n"); 184080f0e4c2SNate Lawson AcpiTerminate(); 184180f0e4c2SNate Lawson } 184215e32d5dSMike Smith } 184315e32d5dSMike Smith 184413d4f7baSMitsuru IWASAKI static void 184513d4f7baSMitsuru IWASAKI acpi_enable_fixed_events(struct acpi_softc *sc) 184613d4f7baSMitsuru IWASAKI { 184713d4f7baSMitsuru IWASAKI static int first_time = 1; 184813d4f7baSMitsuru IWASAKI 184913d4f7baSMitsuru IWASAKI /* Enable and clear fixed events and install handlers. */ 18502be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_POWER_BUTTON) == 0) { 185159ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_POWER_BUTTON); 185213d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_POWER_BUTTON, 185333febf93SNate Lawson acpi_event_power_button_sleep, sc); 1854be2b1797SNate Lawson if (first_time) 18556c0e8467SNate Lawson device_printf(sc->acpi_dev, "Power Button (fixed)\n"); 185613d4f7baSMitsuru IWASAKI } 18572be4e471SJung-uk Kim if ((AcpiGbl_FADT.Flags & ACPI_FADT_SLEEP_BUTTON) == 0) { 185859ddeb18SNate Lawson AcpiClearEvent(ACPI_EVENT_SLEEP_BUTTON); 185913d4f7baSMitsuru IWASAKI AcpiInstallFixedEventHandler(ACPI_EVENT_SLEEP_BUTTON, 186033febf93SNate Lawson acpi_event_sleep_button_sleep, sc); 1861be2b1797SNate Lawson if (first_time) 18626c0e8467SNate Lawson device_printf(sc->acpi_dev, "Sleep Button (fixed)\n"); 186313d4f7baSMitsuru IWASAKI } 186413d4f7baSMitsuru IWASAKI 186513d4f7baSMitsuru IWASAKI first_time = 0; 186613d4f7baSMitsuru IWASAKI } 186713d4f7baSMitsuru IWASAKI 186815e32d5dSMike Smith /* 1869c5ba0be4SMike Smith * Returns true if the device is actually present and should 1870c5ba0be4SMike Smith * be attached to. This requires the present, enabled, UI-visible 1871c5ba0be4SMike Smith * and diagnostics-passed bits to be set. 1872c5ba0be4SMike Smith */ 1873c5ba0be4SMike Smith BOOLEAN 1874c5ba0be4SMike Smith acpi_DeviceIsPresent(device_t dev) 1875c5ba0be4SMike Smith { 18761e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1877c5ba0be4SMike Smith ACPI_HANDLE h; 18781e4925e8SNate Lawson ACPI_BUFFER buf; 1879c5ba0be4SMike Smith ACPI_STATUS error; 18801e4925e8SNate Lawson int ret; 1881c5ba0be4SMike Smith 18821e4925e8SNate Lawson ret = FALSE; 1883c5ba0be4SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1884c5ba0be4SMike Smith return (FALSE); 18851e4925e8SNate Lawson buf.Pointer = NULL; 18861e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 18876fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 18886fca9360SNate Lawson if (ACPI_FAILURE(error)) 1889c5ba0be4SMike Smith return (FALSE); 18901e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1891be2b1797SNate Lawson 1892be2b1797SNate Lawson /* If no _STA method, must be present */ 18931e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 18941e4925e8SNate Lawson ret = TRUE; 1895be2b1797SNate Lawson 1896be2b1797SNate Lawson /* Return true for 'present' and 'functioning' */ 189717dbe0f7SNate Lawson if (ACPI_DEVICE_PRESENT(devinfo->CurrentStatus)) 18981e4925e8SNate Lawson ret = TRUE; 1899be2b1797SNate Lawson 19001e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 19011e4925e8SNate Lawson return (ret); 1902c5ba0be4SMike Smith } 1903c5ba0be4SMike Smith 1904c5ba0be4SMike Smith /* 1905b53f2771SMike Smith * Returns true if the battery is actually present and inserted. 1906b53f2771SMike Smith */ 1907b53f2771SMike Smith BOOLEAN 1908b53f2771SMike Smith acpi_BatteryIsPresent(device_t dev) 1909b53f2771SMike Smith { 19101e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 1911b53f2771SMike Smith ACPI_HANDLE h; 19121e4925e8SNate Lawson ACPI_BUFFER buf; 1913b53f2771SMike Smith ACPI_STATUS error; 19141e4925e8SNate Lawson int ret; 1915b53f2771SMike Smith 19161e4925e8SNate Lawson ret = FALSE; 1917b53f2771SMike Smith if ((h = acpi_get_handle(dev)) == NULL) 1918b53f2771SMike Smith return (FALSE); 19191e4925e8SNate Lawson buf.Pointer = NULL; 19201e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 19216fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 19226fca9360SNate Lawson if (ACPI_FAILURE(error)) 1923b53f2771SMike Smith return (FALSE); 19241e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1925be2b1797SNate Lawson 1926be2b1797SNate Lawson /* If no _STA method, must be present */ 19271e4925e8SNate Lawson if ((devinfo->Valid & ACPI_VALID_STA) == 0) 19281e4925e8SNate Lawson ret = TRUE; 1929be2b1797SNate Lawson 193017dbe0f7SNate Lawson /* Return true for 'present', 'battery present', and 'functioning' */ 193117dbe0f7SNate Lawson if (ACPI_BATTERY_PRESENT(devinfo->CurrentStatus)) 19321e4925e8SNate Lawson ret = TRUE; 1933be2b1797SNate Lawson 19341e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 19351e4925e8SNate Lawson return (ret); 1936b53f2771SMike Smith } 1937b53f2771SMike Smith 1938b53f2771SMike Smith /* 193991233413SNate Lawson * Match a HID string against a handle 194015e32d5dSMike Smith */ 1941ce619b2eSNate Lawson static BOOLEAN 1942ce619b2eSNate Lawson acpi_MatchHid(ACPI_HANDLE h, const char *hid) 194315e32d5dSMike Smith { 19441e4925e8SNate Lawson ACPI_DEVICE_INFO *devinfo; 19451e4925e8SNate Lawson ACPI_BUFFER buf; 194615e32d5dSMike Smith ACPI_STATUS error; 19471e4925e8SNate Lawson int ret, i; 194815e32d5dSMike Smith 19491e4925e8SNate Lawson ret = FALSE; 195091233413SNate Lawson if (hid == NULL || h == NULL) 195191233413SNate Lawson return (ret); 19521e4925e8SNate Lawson buf.Pointer = NULL; 19531e4925e8SNate Lawson buf.Length = ACPI_ALLOCATE_BUFFER; 19546fca9360SNate Lawson error = AcpiGetObjectInfo(h, &buf); 19556fca9360SNate Lawson if (ACPI_FAILURE(error)) 195691233413SNate Lawson return (ret); 19571e4925e8SNate Lawson devinfo = (ACPI_DEVICE_INFO *)buf.Pointer; 1958be2b1797SNate Lawson 1959c59c9a8eSJohn Baldwin if ((devinfo->Valid & ACPI_VALID_HID) != 0 && 1960c59c9a8eSJohn Baldwin strcmp(hid, devinfo->HardwareId.Value) == 0) 19611e4925e8SNate Lawson ret = TRUE; 1962c59c9a8eSJohn Baldwin else if ((devinfo->Valid & ACPI_VALID_CID) != 0) { 19631e4925e8SNate Lawson for (i = 0; i < devinfo->CompatibilityId.Count; i++) { 19641e4925e8SNate Lawson if (strcmp(hid, devinfo->CompatibilityId.Id[i].Value) == 0) { 19651e4925e8SNate Lawson ret = TRUE; 19661e4925e8SNate Lawson break; 19671e4925e8SNate Lawson } 19681e4925e8SNate Lawson } 19691e4925e8SNate Lawson } 1970be2b1797SNate Lawson 19711e4925e8SNate Lawson AcpiOsFree(buf.Pointer); 19721e4925e8SNate Lawson return (ret); 197315e32d5dSMike Smith } 197415e32d5dSMike Smith 197515e32d5dSMike Smith /* 1976c5ba0be4SMike Smith * Return the handle of a named object within our scope, ie. that of (parent) 1977c5ba0be4SMike Smith * or one if its parents. 1978c5ba0be4SMike Smith */ 1979c5ba0be4SMike Smith ACPI_STATUS 1980c5ba0be4SMike Smith acpi_GetHandleInScope(ACPI_HANDLE parent, char *path, ACPI_HANDLE *result) 1981c5ba0be4SMike Smith { 1982c5ba0be4SMike Smith ACPI_HANDLE r; 1983c5ba0be4SMike Smith ACPI_STATUS status; 1984c5ba0be4SMike Smith 1985be2b1797SNate Lawson /* Walk back up the tree to the root */ 1986c5ba0be4SMike Smith for (;;) { 1987be2b1797SNate Lawson status = AcpiGetHandle(parent, path, &r); 1988be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 1989c5ba0be4SMike Smith *result = r; 1990c5ba0be4SMike Smith return (AE_OK); 1991c5ba0be4SMike Smith } 1992bee4aa4aSNate Lawson /* XXX Return error here? */ 1993c5ba0be4SMike Smith if (status != AE_NOT_FOUND) 1994c5ba0be4SMike Smith return (AE_OK); 1995b53f2771SMike Smith if (ACPI_FAILURE(AcpiGetParent(parent, &r))) 1996c5ba0be4SMike Smith return (AE_NOT_FOUND); 1997c5ba0be4SMike Smith parent = r; 1998c5ba0be4SMike Smith } 1999c5ba0be4SMike Smith } 2000c5ba0be4SMike Smith 2001eea17c34SNate Lawson /* Find the difference between two PM tick counts. */ 2002eea17c34SNate Lawson uint32_t 2003eea17c34SNate Lawson acpi_TimerDelta(uint32_t end, uint32_t start) 2004eea17c34SNate Lawson { 2005eea17c34SNate Lawson uint32_t delta; 2006eea17c34SNate Lawson 2007eea17c34SNate Lawson if (end >= start) 2008eea17c34SNate Lawson delta = end - start; 20092be4e471SJung-uk Kim else if (AcpiGbl_FADT.Flags & ACPI_FADT_32BIT_TIMER) 2010eea17c34SNate Lawson delta = ((0xFFFFFFFF - start) + end + 1); 20112be4e471SJung-uk Kim else 20122be4e471SJung-uk Kim delta = ((0x00FFFFFF - start) + end + 1) & 0x00FFFFFF; 2013eea17c34SNate Lawson return (delta); 2014eea17c34SNate Lawson } 2015eea17c34SNate Lawson 2016c5ba0be4SMike Smith /* 2017c5ba0be4SMike Smith * Allocate a buffer with a preset data size. 2018c5ba0be4SMike Smith */ 2019c5ba0be4SMike Smith ACPI_BUFFER * 2020c5ba0be4SMike Smith acpi_AllocBuffer(int size) 2021c5ba0be4SMike Smith { 2022c5ba0be4SMike Smith ACPI_BUFFER *buf; 2023c5ba0be4SMike Smith 2024c5ba0be4SMike Smith if ((buf = malloc(size + sizeof(*buf), M_ACPIDEV, M_NOWAIT)) == NULL) 2025c5ba0be4SMike Smith return (NULL); 2026c5ba0be4SMike Smith buf->Length = size; 2027c5ba0be4SMike Smith buf->Pointer = (void *)(buf + 1); 2028c5ba0be4SMike Smith return (buf); 2029c5ba0be4SMike Smith } 2030c5ba0be4SMike Smith 2031c310653eSNate Lawson ACPI_STATUS 2032cc58e4eeSNate Lawson acpi_SetInteger(ACPI_HANDLE handle, char *path, UINT32 number) 2033c310653eSNate Lawson { 2034c310653eSNate Lawson ACPI_OBJECT arg1; 2035c310653eSNate Lawson ACPI_OBJECT_LIST args; 2036c310653eSNate Lawson 2037c310653eSNate Lawson arg1.Type = ACPI_TYPE_INTEGER; 2038c310653eSNate Lawson arg1.Integer.Value = number; 2039c310653eSNate Lawson args.Count = 1; 2040c310653eSNate Lawson args.Pointer = &arg1; 2041c310653eSNate Lawson 2042c310653eSNate Lawson return (AcpiEvaluateObject(handle, path, &args, NULL)); 2043c310653eSNate Lawson } 2044c310653eSNate Lawson 2045c5ba0be4SMike Smith /* 2046c5ba0be4SMike Smith * Evaluate a path that should return an integer. 2047c5ba0be4SMike Smith */ 2048c5ba0be4SMike Smith ACPI_STATUS 2049cc58e4eeSNate Lawson acpi_GetInteger(ACPI_HANDLE handle, char *path, UINT32 *number) 2050c5ba0be4SMike Smith { 2051be2b1797SNate Lawson ACPI_STATUS status; 2052c5ba0be4SMike Smith ACPI_BUFFER buf; 2053c573e654SMitsuru IWASAKI ACPI_OBJECT param; 2054c5ba0be4SMike Smith 2055c5ba0be4SMike Smith if (handle == NULL) 2056c5ba0be4SMike Smith handle = ACPI_ROOT_OBJECT; 20570c7da7acSJohn Baldwin 20580c7da7acSJohn Baldwin /* 20590c7da7acSJohn Baldwin * Assume that what we've been pointed at is an Integer object, or 20600c7da7acSJohn Baldwin * a method that will return an Integer. 20610c7da7acSJohn Baldwin */ 2062c5ba0be4SMike Smith buf.Pointer = ¶m; 2063c5ba0be4SMike Smith buf.Length = sizeof(param); 2064be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2065be2b1797SNate Lawson if (ACPI_SUCCESS(status)) { 2066be2b1797SNate Lawson if (param.Type == ACPI_TYPE_INTEGER) 2067c5ba0be4SMike Smith *number = param.Integer.Value; 2068be2b1797SNate Lawson else 2069be2b1797SNate Lawson status = AE_TYPE; 2070c5ba0be4SMike Smith } 20710c7da7acSJohn Baldwin 20720c7da7acSJohn Baldwin /* 20730c7da7acSJohn Baldwin * In some applications, a method that's expected to return an Integer 20740c7da7acSJohn Baldwin * may instead return a Buffer (probably to simplify some internal 20750c7da7acSJohn Baldwin * arithmetic). We'll try to fetch whatever it is, and if it's a Buffer, 20760c7da7acSJohn Baldwin * convert it into an Integer as best we can. 20770c7da7acSJohn Baldwin * 20780c7da7acSJohn Baldwin * This is a hack. 20790c7da7acSJohn Baldwin */ 2080be2b1797SNate Lawson if (status == AE_BUFFER_OVERFLOW) { 2081b53f2771SMike Smith if ((buf.Pointer = AcpiOsAllocate(buf.Length)) == NULL) { 2082be2b1797SNate Lawson status = AE_NO_MEMORY; 20830c7da7acSJohn Baldwin } else { 2084be2b1797SNate Lawson status = AcpiEvaluateObject(handle, path, NULL, &buf); 2085be2b1797SNate Lawson if (ACPI_SUCCESS(status)) 2086be2b1797SNate Lawson status = acpi_ConvertBufferToInteger(&buf, number); 20870c7da7acSJohn Baldwin AcpiOsFree(buf.Pointer); 20880c7da7acSJohn Baldwin } 2089f97739daSNate Lawson } 2090be2b1797SNate Lawson return (status); 2091c5ba0be4SMike Smith } 2092c5ba0be4SMike Smith 2093c573e654SMitsuru IWASAKI ACPI_STATUS 2094cc58e4eeSNate Lawson acpi_ConvertBufferToInteger(ACPI_BUFFER *bufp, UINT32 *number) 2095c573e654SMitsuru IWASAKI { 2096c573e654SMitsuru IWASAKI ACPI_OBJECT *p; 2097dba55fa2SNate Lawson UINT8 *val; 2098c573e654SMitsuru IWASAKI int i; 2099c573e654SMitsuru IWASAKI 2100c573e654SMitsuru IWASAKI p = (ACPI_OBJECT *)bufp->Pointer; 2101c573e654SMitsuru IWASAKI if (p->Type == ACPI_TYPE_INTEGER) { 2102c573e654SMitsuru IWASAKI *number = p->Integer.Value; 2103c573e654SMitsuru IWASAKI return (AE_OK); 2104c573e654SMitsuru IWASAKI } 2105c573e654SMitsuru IWASAKI if (p->Type != ACPI_TYPE_BUFFER) 2106c573e654SMitsuru IWASAKI return (AE_TYPE); 2107c573e654SMitsuru IWASAKI if (p->Buffer.Length > sizeof(int)) 2108c573e654SMitsuru IWASAKI return (AE_BAD_DATA); 2109be2b1797SNate Lawson 2110c573e654SMitsuru IWASAKI *number = 0; 2111dba55fa2SNate Lawson val = p->Buffer.Pointer; 2112c573e654SMitsuru IWASAKI for (i = 0; i < p->Buffer.Length; i++) 2113dba55fa2SNate Lawson *number += val[i] << (i * 8); 2114c573e654SMitsuru IWASAKI return (AE_OK); 2115c573e654SMitsuru IWASAKI } 2116c573e654SMitsuru IWASAKI 2117c5ba0be4SMike Smith /* 2118c5ba0be4SMike Smith * Iterate over the elements of an a package object, calling the supplied 2119c5ba0be4SMike Smith * function for each element. 2120c5ba0be4SMike Smith * 2121c5ba0be4SMike Smith * XXX possible enhancement might be to abort traversal on error. 2122c5ba0be4SMike Smith */ 2123c5ba0be4SMike Smith ACPI_STATUS 2124be2b1797SNate Lawson acpi_ForeachPackageObject(ACPI_OBJECT *pkg, 2125be2b1797SNate Lawson void (*func)(ACPI_OBJECT *comp, void *arg), void *arg) 2126c5ba0be4SMike Smith { 2127c5ba0be4SMike Smith ACPI_OBJECT *comp; 2128c5ba0be4SMike Smith int i; 2129c5ba0be4SMike Smith 2130be2b1797SNate Lawson if (pkg == NULL || pkg->Type != ACPI_TYPE_PACKAGE) 2131c5ba0be4SMike Smith return (AE_BAD_PARAMETER); 2132c5ba0be4SMike Smith 2133be2b1797SNate Lawson /* Iterate over components */ 2134be2b1797SNate Lawson i = 0; 2135be2b1797SNate Lawson comp = pkg->Package.Elements; 2136be2b1797SNate Lawson for (; i < pkg->Package.Count; i++, comp++) 2137c5ba0be4SMike Smith func(comp, arg); 2138c5ba0be4SMike Smith 2139c5ba0be4SMike Smith return (AE_OK); 2140c5ba0be4SMike Smith } 2141c5ba0be4SMike Smith 21426f69255bSMike Smith /* 21436f69255bSMike Smith * Find the (index)th resource object in a set. 21446f69255bSMike Smith */ 21456f69255bSMike Smith ACPI_STATUS 21466d63101aSMike Smith acpi_FindIndexedResource(ACPI_BUFFER *buf, int index, ACPI_RESOURCE **resp) 21476f69255bSMike Smith { 21486d63101aSMike Smith ACPI_RESOURCE *rp; 21496f69255bSMike Smith int i; 21506f69255bSMike Smith 21516d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 21526f69255bSMike Smith i = index; 21536d63101aSMike Smith while (i-- > 0) { 2154be2b1797SNate Lawson /* Range check */ 21556d63101aSMike Smith if (rp > (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 21566f69255bSMike Smith return (AE_BAD_PARAMETER); 2157be2b1797SNate Lawson 2158be2b1797SNate Lawson /* Check for terminator */ 2159e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 21606d63101aSMike Smith return (AE_NOT_FOUND); 2161abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 21626f69255bSMike Smith } 21636f69255bSMike Smith if (resp != NULL) 21646d63101aSMike Smith *resp = rp; 2165be2b1797SNate Lawson 21666d63101aSMike Smith return (AE_OK); 21676d63101aSMike Smith } 21686d63101aSMike Smith 21696d63101aSMike Smith /* 21706d63101aSMike Smith * Append an ACPI_RESOURCE to an ACPI_BUFFER. 21716d63101aSMike Smith * 21726d63101aSMike Smith * Given a pointer to an ACPI_RESOURCE structure, expand the ACPI_BUFFER 21736d63101aSMike Smith * provided to contain it. If the ACPI_BUFFER is empty, allocate a sensible 21746d63101aSMike Smith * backing block. If the ACPI_RESOURCE is NULL, return an empty set of 21756d63101aSMike Smith * resources. 21766d63101aSMike Smith */ 21776d63101aSMike Smith #define ACPI_INITIAL_RESOURCE_BUFFER_SIZE 512 21786d63101aSMike Smith 21796d63101aSMike Smith ACPI_STATUS 21806d63101aSMike Smith acpi_AppendBufferResource(ACPI_BUFFER *buf, ACPI_RESOURCE *res) 21816d63101aSMike Smith { 21826d63101aSMike Smith ACPI_RESOURCE *rp; 21836d63101aSMike Smith void *newp; 21846d63101aSMike Smith 2185be2b1797SNate Lawson /* Initialise the buffer if necessary. */ 21866d63101aSMike Smith if (buf->Pointer == NULL) { 21876d63101aSMike Smith buf->Length = ACPI_INITIAL_RESOURCE_BUFFER_SIZE; 21886d63101aSMike Smith if ((buf->Pointer = AcpiOsAllocate(buf->Length)) == NULL) 21896d63101aSMike Smith return (AE_NO_MEMORY); 21906d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 2191e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 21926d63101aSMike Smith rp->Length = 0; 21936d63101aSMike Smith } 21946d63101aSMike Smith if (res == NULL) 21956d63101aSMike Smith return (AE_OK); 21966d63101aSMike Smith 21976d63101aSMike Smith /* 21986d63101aSMike Smith * Scan the current buffer looking for the terminator. 21996d63101aSMike Smith * This will either find the terminator or hit the end 22006d63101aSMike Smith * of the buffer and return an error. 22016d63101aSMike Smith */ 22026d63101aSMike Smith rp = (ACPI_RESOURCE *)buf->Pointer; 22036d63101aSMike Smith for (;;) { 2204be2b1797SNate Lawson /* Range check, don't go outside the buffer */ 22056d63101aSMike Smith if (rp >= (ACPI_RESOURCE *)((u_int8_t *)buf->Pointer + buf->Length)) 22066d63101aSMike Smith return (AE_BAD_PARAMETER); 2207e8d472a7SJung-uk Kim if (rp->Type == ACPI_RESOURCE_TYPE_END_TAG || rp->Length == 0) 22086d63101aSMike Smith break; 2209abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 22106d63101aSMike Smith } 22116d63101aSMike Smith 22126d63101aSMike Smith /* 22136d63101aSMike Smith * Check the size of the buffer and expand if required. 22146d63101aSMike Smith * 22156d63101aSMike Smith * Required size is: 22166d63101aSMike Smith * size of existing resources before terminator + 22176d63101aSMike Smith * size of new resource and header + 22186d63101aSMike Smith * size of terminator. 22196d63101aSMike Smith * 22206d63101aSMike Smith * Note that this loop should really only run once, unless 22216d63101aSMike Smith * for some reason we are stuffing a *really* huge resource. 22226d63101aSMike Smith */ 22236d63101aSMike Smith while ((((u_int8_t *)rp - (u_int8_t *)buf->Pointer) + 2224e8d472a7SJung-uk Kim res->Length + ACPI_RS_SIZE_NO_DATA + 2225e8d472a7SJung-uk Kim ACPI_RS_SIZE_MIN) >= buf->Length) { 22266d63101aSMike Smith if ((newp = AcpiOsAllocate(buf->Length * 2)) == NULL) 22276d63101aSMike Smith return (AE_NO_MEMORY); 22286d63101aSMike Smith bcopy(buf->Pointer, newp, buf->Length); 2229a692219dSMike Smith rp = (ACPI_RESOURCE *)((u_int8_t *)newp + 2230a692219dSMike Smith ((u_int8_t *)rp - (u_int8_t *)buf->Pointer)); 22316d63101aSMike Smith AcpiOsFree(buf->Pointer); 22326d63101aSMike Smith buf->Pointer = newp; 22336d63101aSMike Smith buf->Length += buf->Length; 22346d63101aSMike Smith } 22356d63101aSMike Smith 2236be2b1797SNate Lawson /* Insert the new resource. */ 2237e8d472a7SJung-uk Kim bcopy(res, rp, res->Length + ACPI_RS_SIZE_NO_DATA); 22386d63101aSMike Smith 2239be2b1797SNate Lawson /* And add the terminator. */ 2240abcbc5bcSNate Lawson rp = ACPI_NEXT_RESOURCE(rp); 2241e8d472a7SJung-uk Kim rp->Type = ACPI_RESOURCE_TYPE_END_TAG; 22426d63101aSMike Smith rp->Length = 0; 22436d63101aSMike Smith 22446f69255bSMike Smith return (AE_OK); 22456f69255bSMike Smith } 2246c5ba0be4SMike Smith 2247da14ac9fSJohn Baldwin /* 2248da14ac9fSJohn Baldwin * Set interrupt model. 2249da14ac9fSJohn Baldwin */ 2250da14ac9fSJohn Baldwin ACPI_STATUS 2251da14ac9fSJohn Baldwin acpi_SetIntrModel(int model) 2252da14ac9fSJohn Baldwin { 2253da14ac9fSJohn Baldwin 2254c310653eSNate Lawson return (acpi_SetInteger(ACPI_ROOT_OBJECT, "_PIC", model)); 2255da14ac9fSJohn Baldwin } 2256da14ac9fSJohn Baldwin 225700a30448SNate Lawson /* 225800a30448SNate Lawson * DEPRECATED. This interface has serious deficiencies and will be 225900a30448SNate Lawson * removed. 226000a30448SNate Lawson * 226100a30448SNate Lawson * Immediately enter the sleep state. In the old model, acpiconf(8) ran 226200a30448SNate Lawson * rc.suspend and rc.resume so we don't have to notify devd(8) to do this. 226300a30448SNate Lawson */ 226400a30448SNate Lawson ACPI_STATUS 226500a30448SNate Lawson acpi_SetSleepState(struct acpi_softc *sc, int state) 226600a30448SNate Lawson { 226700a30448SNate Lawson static int once; 226800a30448SNate Lawson 226900a30448SNate Lawson if (!once) { 227000a30448SNate Lawson printf( 227100a30448SNate Lawson "warning: acpi_SetSleepState() deprecated, need to update your software\n"); 227200a30448SNate Lawson once = 1; 227300a30448SNate Lawson } 227400a30448SNate Lawson return (acpi_EnterSleepState(sc, state)); 227500a30448SNate Lawson } 227600a30448SNate Lawson 227700a30448SNate Lawson static void 227800a30448SNate Lawson acpi_sleep_force(void *arg) 227900a30448SNate Lawson { 228000a30448SNate Lawson struct acpi_softc *sc; 228100a30448SNate Lawson 228200a30448SNate Lawson printf("acpi: suspend request timed out, forcing sleep now\n"); 228300a30448SNate Lawson sc = arg; 228400a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 228500a30448SNate Lawson printf("acpi: force sleep state S%d failed\n", sc->acpi_next_sstate); 228600a30448SNate Lawson } 228700a30448SNate Lawson 228800a30448SNate Lawson /* 228900a30448SNate Lawson * Request that the system enter the given suspend state. All /dev/apm 229000a30448SNate Lawson * devices and devd(8) will be notified. Userland then has a chance to 229100a30448SNate Lawson * save state and acknowledge the request. The system sleeps once all 229200a30448SNate Lawson * acks are in. 229300a30448SNate Lawson */ 229400a30448SNate Lawson int 229500a30448SNate Lawson acpi_ReqSleepState(struct acpi_softc *sc, int state) 229600a30448SNate Lawson { 229700a30448SNate Lawson struct apm_clone_data *clone; 229800a30448SNate Lawson 229900a30448SNate Lawson if (state < ACPI_STATE_S1 || state > ACPI_STATE_S5) 230000a30448SNate Lawson return (EINVAL); 230100a30448SNate Lawson 230200a30448SNate Lawson /* S5 (soft-off) should be entered directly with no waiting. */ 230300a30448SNate Lawson if (state == ACPI_STATE_S5) { 230400a30448SNate Lawson if (ACPI_SUCCESS(acpi_EnterSleepState(sc, state))) 230500a30448SNate Lawson return (0); 230600a30448SNate Lawson else 230700a30448SNate Lawson return (ENXIO); 230800a30448SNate Lawson } 230900a30448SNate Lawson 2310b6648efdSNate Lawson #if !defined(__i386__) 2311b6648efdSNate Lawson /* This platform does not support acpi suspend/resume. */ 2312b6648efdSNate Lawson return (EOPNOTSUPP); 2313b6648efdSNate Lawson #endif 2314b6648efdSNate Lawson 231500a30448SNate Lawson /* If a suspend request is already in progress, just return. */ 231600a30448SNate Lawson ACPI_LOCK(acpi); 231700a30448SNate Lawson if (sc->acpi_next_sstate != 0) { 231800a30448SNate Lawson ACPI_UNLOCK(acpi); 231900a30448SNate Lawson return (0); 232000a30448SNate Lawson } 232100a30448SNate Lawson 232200a30448SNate Lawson /* Record the pending state and notify all apm devices. */ 232300a30448SNate Lawson sc->acpi_next_sstate = state; 232400a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 232500a30448SNate Lawson clone->notify_status = APM_EV_NONE; 232600a30448SNate Lawson if ((clone->flags & ACPI_EVF_DEVD) == 0) { 232700a30448SNate Lawson selwakeuppri(&clone->sel_read, PZERO); 232800a30448SNate Lawson KNOTE_UNLOCKED(&clone->sel_read.si_note, 0); 232900a30448SNate Lawson } 233000a30448SNate Lawson } 233100a30448SNate Lawson 23320c26519eSMitsuru IWASAKI /* If devd(8) is not running, immediately enter the sleep state. */ 23330c26519eSMitsuru IWASAKI if (devctl_process_running() == FALSE) { 23340c26519eSMitsuru IWASAKI ACPI_UNLOCK(acpi); 23357572a9c7SMitsuru IWASAKI if (ACPI_SUCCESS(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) { 23367572a9c7SMitsuru IWASAKI return (0); 23377572a9c7SMitsuru IWASAKI } else { 23387572a9c7SMitsuru IWASAKI return (ENXIO); 23397572a9c7SMitsuru IWASAKI } 23400c26519eSMitsuru IWASAKI } 23410c26519eSMitsuru IWASAKI 234200a30448SNate Lawson /* Now notify devd(8) also. */ 234300a30448SNate Lawson acpi_UserNotify("Suspend", ACPI_ROOT_OBJECT, state); 234400a30448SNate Lawson 234500a30448SNate Lawson /* 234600a30448SNate Lawson * Set a timeout to fire if userland doesn't ack the suspend request 234700a30448SNate Lawson * in time. This way we still eventually go to sleep if we were 234800a30448SNate Lawson * overheating or running low on battery, even if userland is hung. 234900a30448SNate Lawson * We cancel this timeout once all userland acks are in or the 235000a30448SNate Lawson * suspend request is aborted. 235100a30448SNate Lawson */ 235200a30448SNate Lawson callout_reset(&sc->susp_force_to, 10 * hz, acpi_sleep_force, sc); 235300a30448SNate Lawson ACPI_UNLOCK(acpi); 235400a30448SNate Lawson return (0); 235500a30448SNate Lawson } 235600a30448SNate Lawson 235700a30448SNate Lawson /* 235800a30448SNate Lawson * Acknowledge (or reject) a pending sleep state. The caller has 235900a30448SNate Lawson * prepared for suspend and is now ready for it to proceed. If the 236000a30448SNate Lawson * error argument is non-zero, it indicates suspend should be cancelled 236100a30448SNate Lawson * and gives an errno value describing why. Once all votes are in, 236200a30448SNate Lawson * we suspend the system. 236300a30448SNate Lawson */ 236400a30448SNate Lawson int 236500a30448SNate Lawson acpi_AckSleepState(struct apm_clone_data *clone, int error) 236600a30448SNate Lawson { 236700a30448SNate Lawson struct acpi_softc *sc; 236800a30448SNate Lawson int ret, sleeping; 236900a30448SNate Lawson 2370b6648efdSNate Lawson #if !defined(__i386__) 2371b6648efdSNate Lawson /* This platform does not support acpi suspend/resume. */ 2372b6648efdSNate Lawson return (EOPNOTSUPP); 2373b6648efdSNate Lawson #endif 2374b6648efdSNate Lawson 237500a30448SNate Lawson /* If no pending sleep state, return an error. */ 237600a30448SNate Lawson ACPI_LOCK(acpi); 237700a30448SNate Lawson sc = clone->acpi_sc; 237800a30448SNate Lawson if (sc->acpi_next_sstate == 0) { 237900a30448SNate Lawson ACPI_UNLOCK(acpi); 238000a30448SNate Lawson return (ENXIO); 238100a30448SNate Lawson } 238200a30448SNate Lawson 238300a30448SNate Lawson /* Caller wants to abort suspend process. */ 238400a30448SNate Lawson if (error) { 238500a30448SNate Lawson sc->acpi_next_sstate = 0; 238600a30448SNate Lawson callout_stop(&sc->susp_force_to); 238700a30448SNate Lawson printf("acpi: listener on %s cancelled the pending suspend\n", 238800a30448SNate Lawson devtoname(clone->cdev)); 238900a30448SNate Lawson ACPI_UNLOCK(acpi); 239000a30448SNate Lawson return (0); 239100a30448SNate Lawson } 239200a30448SNate Lawson 239300a30448SNate Lawson /* 239400a30448SNate Lawson * Mark this device as acking the suspend request. Then, walk through 239500a30448SNate Lawson * all devices, seeing if they agree yet. We only count devices that 239600a30448SNate Lawson * are writable since read-only devices couldn't ack the request. 239700a30448SNate Lawson */ 239800a30448SNate Lawson clone->notify_status = APM_EV_ACKED; 239900a30448SNate Lawson sleeping = TRUE; 240000a30448SNate Lawson STAILQ_FOREACH(clone, &sc->apm_cdevs, entries) { 240100a30448SNate Lawson if ((clone->flags & ACPI_EVF_WRITE) != 0 && 240200a30448SNate Lawson clone->notify_status != APM_EV_ACKED) { 240300a30448SNate Lawson sleeping = FALSE; 240400a30448SNate Lawson break; 240500a30448SNate Lawson } 240600a30448SNate Lawson } 240700a30448SNate Lawson 240800a30448SNate Lawson /* If all devices have voted "yes", we will suspend now. */ 240900a30448SNate Lawson if (sleeping) 241000a30448SNate Lawson callout_stop(&sc->susp_force_to); 241100a30448SNate Lawson ACPI_UNLOCK(acpi); 241200a30448SNate Lawson ret = 0; 241300a30448SNate Lawson if (sleeping) { 241400a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, sc->acpi_next_sstate))) 241500a30448SNate Lawson ret = ENODEV; 241600a30448SNate Lawson } 241700a30448SNate Lawson 241800a30448SNate Lawson return (ret); 241900a30448SNate Lawson } 242000a30448SNate Lawson 2421ece50487SMitsuru IWASAKI static void 2422ece50487SMitsuru IWASAKI acpi_sleep_enable(void *arg) 2423ece50487SMitsuru IWASAKI { 2424bee4aa4aSNate Lawson 2425ece50487SMitsuru IWASAKI ((struct acpi_softc *)arg)->acpi_sleep_disabled = 0; 2426ece50487SMitsuru IWASAKI } 2427c30382dfSMitsuru IWASAKI 242815e2f34fSNate Lawson enum acpi_sleep_state { 242915e2f34fSNate Lawson ACPI_SS_NONE, 243015e2f34fSNate Lawson ACPI_SS_GPE_SET, 243115e2f34fSNate Lawson ACPI_SS_DEV_SUSPEND, 243215e2f34fSNate Lawson ACPI_SS_SLP_PREP, 243315e2f34fSNate Lawson ACPI_SS_SLEPT, 243415e2f34fSNate Lawson }; 243515e2f34fSNate Lawson 243615e32d5dSMike Smith /* 243700a30448SNate Lawson * Enter the desired system sleep state. 243815e32d5dSMike Smith * 2439be2b1797SNate Lawson * Currently we support S1-S5 but S4 is only S4BIOS 244015e32d5dSMike Smith */ 244100a30448SNate Lawson static ACPI_STATUS 244200a30448SNate Lawson acpi_EnterSleepState(struct acpi_softc *sc, int state) 244315e32d5dSMike Smith { 244415e2f34fSNate Lawson ACPI_STATUS status; 244556d8cb57SMitsuru IWASAKI UINT8 TypeA; 244656d8cb57SMitsuru IWASAKI UINT8 TypeB; 244715e2f34fSNate Lawson enum acpi_sleep_state slp_state; 244815e32d5dSMike Smith 2449b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 24500ae55423SMike Smith 245100a30448SNate Lawson /* Re-entry once we're suspending is not allowed. */ 245215e2f34fSNate Lawson status = AE_OK; 245315e2f34fSNate Lawson ACPI_LOCK(acpi); 245415e2f34fSNate Lawson if (sc->acpi_sleep_disabled) { 245515e2f34fSNate Lawson ACPI_UNLOCK(acpi); 245615e2f34fSNate Lawson printf("acpi: suspend request ignored (not ready yet)\n"); 245700a30448SNate Lawson return (AE_ERROR); 245815e2f34fSNate Lawson } 245915e2f34fSNate Lawson sc->acpi_sleep_disabled = 1; 246015e2f34fSNate Lawson ACPI_UNLOCK(acpi); 24616397624dSMitsuru IWASAKI 24625d3d03f1SNate Lawson /* 24635d3d03f1SNate Lawson * Be sure to hold Giant across DEVICE_SUSPEND/RESUME since non-MPSAFE 24645d3d03f1SNate Lawson * drivers need this. 24655d3d03f1SNate Lawson */ 24665d3d03f1SNate Lawson mtx_lock(&Giant); 246715e2f34fSNate Lawson slp_state = ACPI_SS_NONE; 246815e32d5dSMike Smith switch (state) { 246915e32d5dSMike Smith case ACPI_STATE_S1: 24706161544cSTakanori Watanabe case ACPI_STATE_S2: 24716161544cSTakanori Watanabe case ACPI_STATE_S3: 24726161544cSTakanori Watanabe case ACPI_STATE_S4: 2473adad4744SNate Lawson status = AcpiGetSleepTypeData(state, &TypeA, &TypeB); 247498175614SNate Lawson if (status == AE_NOT_FOUND) { 247598175614SNate Lawson device_printf(sc->acpi_dev, 247698175614SNate Lawson "Sleep state S%d not supported by BIOS\n", state); 247798175614SNate Lawson break; 247898175614SNate Lawson } else if (ACPI_FAILURE(status)) { 2479be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiGetSleepTypeData failed - %s\n", 2480be2b1797SNate Lawson AcpiFormatException(status)); 248156d8cb57SMitsuru IWASAKI break; 248256d8cb57SMitsuru IWASAKI } 248356d8cb57SMitsuru IWASAKI 2484a1fccb47SMitsuru IWASAKI sc->acpi_sstate = state; 2485a1fccb47SMitsuru IWASAKI 2486d4b9ff91SNate Lawson /* Enable any GPEs as appropriate and requested by the user. */ 2487d4b9ff91SNate Lawson acpi_wake_prep_walk(state); 248815e2f34fSNate Lawson slp_state = ACPI_SS_GPE_SET; 2489e8b4d56eSNate Lawson 249015e32d5dSMike Smith /* 2491c7f88fc3SNate Lawson * Inform all devices that we are going to sleep. If at least one 2492c7f88fc3SNate Lawson * device fails, DEVICE_SUSPEND() automatically resumes the tree. 249315e32d5dSMike Smith * 2494c7f88fc3SNate Lawson * XXX Note that a better two-pass approach with a 'veto' pass 2495c7f88fc3SNate Lawson * followed by a "real thing" pass would be better, but the current 2496c7f88fc3SNate Lawson * bus interface does not provide for this. 249715e32d5dSMike Smith */ 249815e2f34fSNate Lawson if (DEVICE_SUSPEND(root_bus) != 0) { 249915e2f34fSNate Lawson device_printf(sc->acpi_dev, "device_suspend failed\n"); 250015e2f34fSNate Lawson break; 250115e2f34fSNate Lawson } 250215e2f34fSNate Lawson slp_state = ACPI_SS_DEV_SUSPEND; 2503b9c780d6SMitsuru IWASAKI 250493bfd059SNate Lawson /* If testing device suspend only, back out of everything here. */ 250593bfd059SNate Lawson if (acpi_susp_bounce) 250693bfd059SNate Lawson break; 250793bfd059SNate Lawson 2508be2b1797SNate Lawson status = AcpiEnterSleepStatePrep(state); 2509be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2510b9c780d6SMitsuru IWASAKI device_printf(sc->acpi_dev, "AcpiEnterSleepStatePrep failed - %s\n", 2511b9c780d6SMitsuru IWASAKI AcpiFormatException(status)); 2512b9c780d6SMitsuru IWASAKI break; 2513b9c780d6SMitsuru IWASAKI } 251415e2f34fSNate Lawson slp_state = ACPI_SS_SLP_PREP; 2515b9c780d6SMitsuru IWASAKI 2516be2b1797SNate Lawson if (sc->acpi_sleep_delay > 0) 2517ff01efb5SMitsuru IWASAKI DELAY(sc->acpi_sleep_delay * 1000000); 2518ff01efb5SMitsuru IWASAKI 25196161544cSTakanori Watanabe if (state != ACPI_STATE_S1) { 25206161544cSTakanori Watanabe acpi_sleep_machdep(sc, state); 25216161544cSTakanori Watanabe 25226161544cSTakanori Watanabe /* Re-enable ACPI hardware on wakeup from sleep state 4. */ 2523be2b1797SNate Lawson if (state == ACPI_STATE_S4) 2524964679ceSMitsuru IWASAKI AcpiEnable(); 25256161544cSTakanori Watanabe } else { 2526c7f88fc3SNate Lawson ACPI_DISABLE_IRQS(); 2527adad4744SNate Lawson status = AcpiEnterSleepState(state); 2528be2b1797SNate Lawson if (ACPI_FAILURE(status)) { 2529be2b1797SNate Lawson device_printf(sc->acpi_dev, "AcpiEnterSleepState failed - %s\n", 2530be2b1797SNate Lawson AcpiFormatException(status)); 2531c30382dfSMitsuru IWASAKI break; 253215e32d5dSMike Smith } 25336161544cSTakanori Watanabe } 253415e2f34fSNate Lawson slp_state = ACPI_SS_SLEPT; 253515e32d5dSMike Smith break; 253615e32d5dSMike Smith case ACPI_STATE_S5: 253715e32d5dSMike Smith /* 253815e32d5dSMike Smith * Shut down cleanly and power off. This will call us back through the 253915e32d5dSMike Smith * shutdown handlers. 254015e32d5dSMike Smith */ 254115e32d5dSMike Smith shutdown_nice(RB_POWEROFF); 254215e32d5dSMike Smith break; 254398175614SNate Lawson case ACPI_STATE_S0: 254415e32d5dSMike Smith default: 254515e32d5dSMike Smith status = AE_BAD_PARAMETER; 254615e32d5dSMike Smith break; 254715e32d5dSMike Smith } 2548ece50487SMitsuru IWASAKI 254915e2f34fSNate Lawson /* 255015e2f34fSNate Lawson * Back out state according to how far along we got in the suspend 255115e2f34fSNate Lawson * process. This handles both the error and success cases. 255215e2f34fSNate Lawson */ 255300a30448SNate Lawson sc->acpi_next_sstate = 0; 255415e2f34fSNate Lawson if (slp_state >= ACPI_SS_GPE_SET) { 255515e2f34fSNate Lawson acpi_wake_prep_walk(state); 255615e2f34fSNate Lawson sc->acpi_sstate = ACPI_STATE_S0; 255715e2f34fSNate Lawson } 255815e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLP_PREP) 255915e2f34fSNate Lawson AcpiLeaveSleepState(state); 2560071339e2SNate Lawson if (slp_state >= ACPI_SS_DEV_SUSPEND) 2561071339e2SNate Lawson DEVICE_RESUME(root_bus); 256215e2f34fSNate Lawson if (slp_state >= ACPI_SS_SLEPT) 256315e2f34fSNate Lawson acpi_enable_fixed_events(sc); 256415e2f34fSNate Lawson 256515e2f34fSNate Lawson /* Allow another sleep request after a while. */ 256615e2f34fSNate Lawson if (state != ACPI_STATE_S5) 256700a30448SNate Lawson timeout(acpi_sleep_enable, sc, hz * ACPI_MINIMUM_AWAKETIME); 256800a30448SNate Lawson 256900a30448SNate Lawson /* Run /etc/rc.resume after we are back. */ 257000a30448SNate Lawson acpi_UserNotify("Resume", ACPI_ROOT_OBJECT, state); 2571ece50487SMitsuru IWASAKI 25725d3d03f1SNate Lawson mtx_unlock(&Giant); 25730ae55423SMike Smith return_ACPI_STATUS (status); 257415e32d5dSMike Smith } 257515e32d5dSMike Smith 2576e8b4d56eSNate Lawson /* Initialize a device's wake GPE. */ 2577e8b4d56eSNate Lawson int 2578e8b4d56eSNate Lawson acpi_wake_init(device_t dev, int type) 2579e8b4d56eSNate Lawson { 2580e8b4d56eSNate Lawson struct acpi_prw_data prw; 2581e8b4d56eSNate Lawson 2582e8b4d56eSNate Lawson /* Evaluate _PRW to find the GPE. */ 2583e8b4d56eSNate Lawson if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2584e8b4d56eSNate Lawson return (ENXIO); 2585e8b4d56eSNate Lawson 2586e8b4d56eSNate Lawson /* Set the requested type for the GPE (runtime, wake, or both). */ 2587e8b4d56eSNate Lawson if (ACPI_FAILURE(AcpiSetGpeType(prw.gpe_handle, prw.gpe_bit, type))) { 2588e8b4d56eSNate Lawson device_printf(dev, "set GPE type failed\n"); 2589e8b4d56eSNate Lawson return (ENXIO); 2590e8b4d56eSNate Lawson } 2591e8b4d56eSNate Lawson 2592e8b4d56eSNate Lawson return (0); 2593e8b4d56eSNate Lawson } 2594e8b4d56eSNate Lawson 2595e8b4d56eSNate Lawson /* Enable or disable the device's wake GPE. */ 2596e8b4d56eSNate Lawson int 2597e8b4d56eSNate Lawson acpi_wake_set_enable(device_t dev, int enable) 2598e8b4d56eSNate Lawson { 2599e8b4d56eSNate Lawson struct acpi_prw_data prw; 2600e8b4d56eSNate Lawson ACPI_STATUS status; 2601e8b4d56eSNate Lawson int flags; 2602e8b4d56eSNate Lawson 2603d4b9ff91SNate Lawson /* Make sure the device supports waking the system and get the GPE. */ 26042be4e471SJung-uk Kim if (acpi_parse_prw(acpi_get_handle(dev), &prw) != 0) 2605e8b4d56eSNate Lawson return (ENXIO); 2606e8b4d56eSNate Lawson 2607d4b9ff91SNate Lawson flags = acpi_get_flags(dev); 2608e8b4d56eSNate Lawson if (enable) { 2609e8b4d56eSNate Lawson status = AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2610e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2611e8b4d56eSNate Lawson device_printf(dev, "enable wake failed\n"); 2612e8b4d56eSNate Lawson return (ENXIO); 2613e8b4d56eSNate Lawson } 2614d4b9ff91SNate Lawson acpi_set_flags(dev, flags | ACPI_FLAG_WAKE_ENABLED); 2615e8b4d56eSNate Lawson } else { 2616e8b4d56eSNate Lawson status = AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2617e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) { 2618e8b4d56eSNate Lawson device_printf(dev, "disable wake failed\n"); 2619e8b4d56eSNate Lawson return (ENXIO); 2620e8b4d56eSNate Lawson } 2621d4b9ff91SNate Lawson acpi_set_flags(dev, flags & ~ACPI_FLAG_WAKE_ENABLED); 2622e8b4d56eSNate Lawson } 2623e8b4d56eSNate Lawson 2624e8b4d56eSNate Lawson return (0); 2625e8b4d56eSNate Lawson } 2626e8b4d56eSNate Lawson 2627d4b9ff91SNate Lawson static int 2628d4b9ff91SNate Lawson acpi_wake_sleep_prep(ACPI_HANDLE handle, int sstate) 2629e8b4d56eSNate Lawson { 2630e8b4d56eSNate Lawson struct acpi_prw_data prw; 2631d4b9ff91SNate Lawson device_t dev; 2632e8b4d56eSNate Lawson 2633d4b9ff91SNate Lawson /* Check that this is a wake-capable device and get its GPE. */ 2634e8b4d56eSNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2635e8b4d56eSNate Lawson return (ENXIO); 2636d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2637e8b4d56eSNate Lawson 2638e8b4d56eSNate Lawson /* 2639d4b9ff91SNate Lawson * The destination sleep state must be less than (i.e., higher power) 2640d4b9ff91SNate Lawson * or equal to the value specified by _PRW. If this GPE cannot be 2641d4b9ff91SNate Lawson * enabled for the next sleep state, then disable it. If it can and 2642d4b9ff91SNate Lawson * the user requested it be enabled, turn on any required power resources 2643d4b9ff91SNate Lawson * and set _PSW. 2644e8b4d56eSNate Lawson */ 2645d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 2646cc85c78cSNate Lawson AcpiDisableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2647e8b4d56eSNate Lawson if (bootverbose) 2648d4b9ff91SNate Lawson device_printf(dev, "wake_prep disabled wake for %s (S%d)\n", 2649d4b9ff91SNate Lawson acpi_name(handle), sstate); 2650d4b9ff91SNate Lawson } else if (dev && (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) != 0) { 2651d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 1); 2652d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 1); 2653d4b9ff91SNate Lawson if (bootverbose) 2654d4b9ff91SNate Lawson device_printf(dev, "wake_prep enabled for %s (S%d)\n", 2655d4b9ff91SNate Lawson acpi_name(handle), sstate); 2656d4b9ff91SNate Lawson } 2657cc85c78cSNate Lawson 2658cc85c78cSNate Lawson return (0); 2659e8b4d56eSNate Lawson } 2660e8b4d56eSNate Lawson 2661d4b9ff91SNate Lawson static int 2662d4b9ff91SNate Lawson acpi_wake_run_prep(ACPI_HANDLE handle, int sstate) 2663cc85c78cSNate Lawson { 2664cc85c78cSNate Lawson struct acpi_prw_data prw; 2665d4b9ff91SNate Lawson device_t dev; 2666cc85c78cSNate Lawson 2667cc85c78cSNate Lawson /* 2668d4b9ff91SNate Lawson * Check that this is a wake-capable device and get its GPE. Return 2669d4b9ff91SNate Lawson * now if the user didn't enable this device for wake. 2670cc85c78cSNate Lawson */ 2671d4b9ff91SNate Lawson if (acpi_parse_prw(handle, &prw) != 0) 2672d4b9ff91SNate Lawson return (ENXIO); 2673d4b9ff91SNate Lawson dev = acpi_get_device(handle); 2674d4b9ff91SNate Lawson if (dev == NULL || (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) == 0) 2675d4b9ff91SNate Lawson return (0); 2676cc85c78cSNate Lawson 2677d4b9ff91SNate Lawson /* 2678d4b9ff91SNate Lawson * If this GPE couldn't be enabled for the previous sleep state, it was 2679d4b9ff91SNate Lawson * disabled before going to sleep so re-enable it. If it was enabled, 2680d4b9ff91SNate Lawson * clear _PSW and turn off any power resources it used. 2681d4b9ff91SNate Lawson */ 2682d4b9ff91SNate Lawson if (sstate > prw.lowest_wake) { 2683cc85c78cSNate Lawson AcpiEnableGpe(prw.gpe_handle, prw.gpe_bit, ACPI_NOT_ISR); 2684d4b9ff91SNate Lawson if (bootverbose) 2685d4b9ff91SNate Lawson device_printf(dev, "run_prep re-enabled %s\n", acpi_name(handle)); 2686d4b9ff91SNate Lawson } else { 2687d4b9ff91SNate Lawson acpi_SetInteger(handle, "_PSW", 0); 2688d4b9ff91SNate Lawson acpi_pwr_wake_enable(handle, 0); 2689d4b9ff91SNate Lawson if (bootverbose) 2690d4b9ff91SNate Lawson device_printf(dev, "run_prep cleaned up for %s\n", 2691d4b9ff91SNate Lawson acpi_name(handle)); 2692d4b9ff91SNate Lawson } 2693d4b9ff91SNate Lawson 2694e8b4d56eSNate Lawson return (0); 2695e8b4d56eSNate Lawson } 2696e8b4d56eSNate Lawson 2697e8b4d56eSNate Lawson static ACPI_STATUS 2698d4b9ff91SNate Lawson acpi_wake_prep(ACPI_HANDLE handle, UINT32 level, void *context, void **status) 2699e8b4d56eSNate Lawson { 2700d4b9ff91SNate Lawson int sstate; 2701e8b4d56eSNate Lawson 2702d4b9ff91SNate Lawson /* If suspending, run the sleep prep function, otherwise wake. */ 2703d4b9ff91SNate Lawson sstate = *(int *)context; 2704d4b9ff91SNate Lawson if (AcpiGbl_SystemAwakeAndRunning) 2705d4b9ff91SNate Lawson acpi_wake_sleep_prep(handle, sstate); 2706d4b9ff91SNate Lawson else 2707d4b9ff91SNate Lawson acpi_wake_run_prep(handle, sstate); 2708e8b4d56eSNate Lawson return (AE_OK); 2709e8b4d56eSNate Lawson } 2710e8b4d56eSNate Lawson 2711d4b9ff91SNate Lawson /* Walk the tree rooted at acpi0 to prep devices for suspend/resume. */ 2712e8b4d56eSNate Lawson static int 2713d4b9ff91SNate Lawson acpi_wake_prep_walk(int sstate) 2714e8b4d56eSNate Lawson { 2715e8b4d56eSNate Lawson ACPI_HANDLE sb_handle; 2716e8b4d56eSNate Lawson 2717e8b4d56eSNate Lawson if (ACPI_SUCCESS(AcpiGetHandle(ACPI_ROOT_OBJECT, "\\_SB_", &sb_handle))) 2718d4b9ff91SNate Lawson AcpiWalkNamespace(ACPI_TYPE_DEVICE, sb_handle, 100, 2719d4b9ff91SNate Lawson acpi_wake_prep, &sstate, NULL); 2720e8b4d56eSNate Lawson return (0); 2721e8b4d56eSNate Lawson } 2722e8b4d56eSNate Lawson 272388a79fc0SNate Lawson /* Walk the tree rooted at acpi0 to attach per-device wake sysctls. */ 272488a79fc0SNate Lawson static int 272588a79fc0SNate Lawson acpi_wake_sysctl_walk(device_t dev) 272688a79fc0SNate Lawson { 272788a79fc0SNate Lawson int error, i, numdevs; 272888a79fc0SNate Lawson device_t *devlist; 272988a79fc0SNate Lawson device_t child; 2730d4b9ff91SNate Lawson ACPI_STATUS status; 273188a79fc0SNate Lawson 273288a79fc0SNate Lawson error = device_get_children(dev, &devlist, &numdevs); 27331c9ec538SNate Lawson if (error != 0 || numdevs == 0) { 27341c9ec538SNate Lawson if (numdevs == 0) 27351c9ec538SNate Lawson free(devlist, M_TEMP); 273688a79fc0SNate Lawson return (error); 27371c9ec538SNate Lawson } 273888a79fc0SNate Lawson for (i = 0; i < numdevs; i++) { 273988a79fc0SNate Lawson child = devlist[i]; 2740d4b9ff91SNate Lawson acpi_wake_sysctl_walk(child); 274188a79fc0SNate Lawson if (!device_is_attached(child)) 274288a79fc0SNate Lawson continue; 2743d4b9ff91SNate Lawson status = AcpiEvaluateObject(acpi_get_handle(child), "_PRW", NULL, NULL); 2744d4b9ff91SNate Lawson if (ACPI_SUCCESS(status)) { 274588a79fc0SNate Lawson SYSCTL_ADD_PROC(device_get_sysctl_ctx(child), 274688a79fc0SNate Lawson SYSCTL_CHILDREN(device_get_sysctl_tree(child)), OID_AUTO, 274788a79fc0SNate Lawson "wake", CTLTYPE_INT | CTLFLAG_RW, child, 0, 274888a79fc0SNate Lawson acpi_wake_set_sysctl, "I", "Device set to wake the system"); 274988a79fc0SNate Lawson } 275088a79fc0SNate Lawson } 275188a79fc0SNate Lawson free(devlist, M_TEMP); 275288a79fc0SNate Lawson 275388a79fc0SNate Lawson return (0); 275488a79fc0SNate Lawson } 275588a79fc0SNate Lawson 275688a79fc0SNate Lawson /* Enable or disable wake from userland. */ 275788a79fc0SNate Lawson static int 275888a79fc0SNate Lawson acpi_wake_set_sysctl(SYSCTL_HANDLER_ARGS) 275988a79fc0SNate Lawson { 276088a79fc0SNate Lawson int enable, error; 276188a79fc0SNate Lawson device_t dev; 276288a79fc0SNate Lawson 276388a79fc0SNate Lawson dev = (device_t)arg1; 2764d4b9ff91SNate Lawson enable = (acpi_get_flags(dev) & ACPI_FLAG_WAKE_ENABLED) ? 1 : 0; 276588a79fc0SNate Lawson 276688a79fc0SNate Lawson error = sysctl_handle_int(oidp, &enable, 0, req); 276788a79fc0SNate Lawson if (error != 0 || req->newptr == NULL) 276888a79fc0SNate Lawson return (error); 276988a79fc0SNate Lawson if (enable != 0 && enable != 1) 277088a79fc0SNate Lawson return (EINVAL); 277188a79fc0SNate Lawson 277288a79fc0SNate Lawson return (acpi_wake_set_enable(dev, enable)); 277388a79fc0SNate Lawson } 277488a79fc0SNate Lawson 2775e8b4d56eSNate Lawson /* Parse a device's _PRW into a structure. */ 2776d4b9ff91SNate Lawson int 2777e8b4d56eSNate Lawson acpi_parse_prw(ACPI_HANDLE h, struct acpi_prw_data *prw) 2778e8b4d56eSNate Lawson { 2779e8b4d56eSNate Lawson ACPI_STATUS status; 2780e8b4d56eSNate Lawson ACPI_BUFFER prw_buffer; 2781e8b4d56eSNate Lawson ACPI_OBJECT *res, *res2; 2782d4b9ff91SNate Lawson int error, i, power_count; 2783e8b4d56eSNate Lawson 2784e8b4d56eSNate Lawson if (h == NULL || prw == NULL) 2785e8b4d56eSNate Lawson return (EINVAL); 2786e8b4d56eSNate Lawson 2787e8b4d56eSNate Lawson /* 2788e8b4d56eSNate Lawson * The _PRW object (7.2.9) is only required for devices that have the 2789e8b4d56eSNate Lawson * ability to wake the system from a sleeping state. 2790e8b4d56eSNate Lawson */ 2791e8b4d56eSNate Lawson error = EINVAL; 2792e8b4d56eSNate Lawson prw_buffer.Pointer = NULL; 2793e8b4d56eSNate Lawson prw_buffer.Length = ACPI_ALLOCATE_BUFFER; 2794e8b4d56eSNate Lawson status = AcpiEvaluateObject(h, "_PRW", NULL, &prw_buffer); 2795e8b4d56eSNate Lawson if (ACPI_FAILURE(status)) 2796e8b4d56eSNate Lawson return (ENOENT); 2797e8b4d56eSNate Lawson res = (ACPI_OBJECT *)prw_buffer.Pointer; 2798e8b4d56eSNate Lawson if (res == NULL) 2799e8b4d56eSNate Lawson return (ENOENT); 2800e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res, 2)) 2801e8b4d56eSNate Lawson goto out; 2802e8b4d56eSNate Lawson 2803e8b4d56eSNate Lawson /* 2804e8b4d56eSNate Lawson * Element 1 of the _PRW object: 2805e8b4d56eSNate Lawson * The lowest power system sleeping state that can be entered while still 2806e8b4d56eSNate Lawson * providing wake functionality. The sleeping state being entered must 2807e8b4d56eSNate Lawson * be less than (i.e., higher power) or equal to this value. 2808e8b4d56eSNate Lawson */ 2809e8b4d56eSNate Lawson if (acpi_PkgInt32(res, 1, &prw->lowest_wake) != 0) 2810e8b4d56eSNate Lawson goto out; 2811e8b4d56eSNate Lawson 2812e8b4d56eSNate Lawson /* 2813e8b4d56eSNate Lawson * Element 0 of the _PRW object: 2814e8b4d56eSNate Lawson */ 2815e8b4d56eSNate Lawson switch (res->Package.Elements[0].Type) { 2816e8b4d56eSNate Lawson case ACPI_TYPE_INTEGER: 2817e8b4d56eSNate Lawson /* 2818e8b4d56eSNate Lawson * If the data type of this package element is numeric, then this 2819e8b4d56eSNate Lawson * _PRW package element is the bit index in the GPEx_EN, in the 2820e8b4d56eSNate Lawson * GPE blocks described in the FADT, of the enable bit that is 2821e8b4d56eSNate Lawson * enabled for the wake event. 2822e8b4d56eSNate Lawson */ 2823e8b4d56eSNate Lawson prw->gpe_handle = NULL; 2824e8b4d56eSNate Lawson prw->gpe_bit = res->Package.Elements[0].Integer.Value; 2825e8b4d56eSNate Lawson error = 0; 2826e8b4d56eSNate Lawson break; 2827e8b4d56eSNate Lawson case ACPI_TYPE_PACKAGE: 2828e8b4d56eSNate Lawson /* 2829e8b4d56eSNate Lawson * If the data type of this package element is a package, then this 2830e8b4d56eSNate Lawson * _PRW package element is itself a package containing two 2831e8b4d56eSNate Lawson * elements. The first is an object reference to the GPE Block 2832e8b4d56eSNate Lawson * device that contains the GPE that will be triggered by the wake 2833e8b4d56eSNate Lawson * event. The second element is numeric and it contains the bit 2834e8b4d56eSNate Lawson * index in the GPEx_EN, in the GPE Block referenced by the 2835e8b4d56eSNate Lawson * first element in the package, of the enable bit that is enabled for 2836e8b4d56eSNate Lawson * the wake event. 2837e8b4d56eSNate Lawson * 2838e8b4d56eSNate Lawson * For example, if this field is a package then it is of the form: 2839e8b4d56eSNate Lawson * Package() {\_SB.PCI0.ISA.GPE, 2} 2840e8b4d56eSNate Lawson */ 2841e8b4d56eSNate Lawson res2 = &res->Package.Elements[0]; 2842e8b4d56eSNate Lawson if (!ACPI_PKG_VALID(res2, 2)) 2843e8b4d56eSNate Lawson goto out; 2844e8b4d56eSNate Lawson prw->gpe_handle = acpi_GetReference(NULL, &res2->Package.Elements[0]); 2845e8b4d56eSNate Lawson if (prw->gpe_handle == NULL) 2846e8b4d56eSNate Lawson goto out; 2847e8b4d56eSNate Lawson if (acpi_PkgInt32(res2, 1, &prw->gpe_bit) != 0) 2848e8b4d56eSNate Lawson goto out; 2849e8b4d56eSNate Lawson error = 0; 2850e8b4d56eSNate Lawson break; 2851e8b4d56eSNate Lawson default: 2852e8b4d56eSNate Lawson goto out; 2853e8b4d56eSNate Lawson } 2854e8b4d56eSNate Lawson 2855d4b9ff91SNate Lawson /* Elements 2 to N of the _PRW object are power resources. */ 2856d4b9ff91SNate Lawson power_count = res->Package.Count - 2; 2857d4b9ff91SNate Lawson if (power_count > ACPI_PRW_MAX_POWERRES) { 2858d4b9ff91SNate Lawson printf("ACPI device %s has too many power resources\n", acpi_name(h)); 2859d4b9ff91SNate Lawson power_count = 0; 2860d4b9ff91SNate Lawson } 2861d4b9ff91SNate Lawson prw->power_res_count = power_count; 2862d4b9ff91SNate Lawson for (i = 0; i < power_count; i++) 2863d4b9ff91SNate Lawson prw->power_res[i] = res->Package.Elements[i]; 2864e8b4d56eSNate Lawson 2865e8b4d56eSNate Lawson out: 2866e8b4d56eSNate Lawson if (prw_buffer.Pointer != NULL) 2867e8b4d56eSNate Lawson AcpiOsFree(prw_buffer.Pointer); 2868e8b4d56eSNate Lawson return (error); 2869e8b4d56eSNate Lawson } 2870e8b4d56eSNate Lawson 287115e32d5dSMike Smith /* 287215e32d5dSMike Smith * ACPI Event Handlers 287315e32d5dSMike Smith */ 287415e32d5dSMike Smith 287515e32d5dSMike Smith /* System Event Handlers (registered by EVENTHANDLER_REGISTER) */ 287615e32d5dSMike Smith 287715e32d5dSMike Smith static void 287815e32d5dSMike Smith acpi_system_eventhandler_sleep(void *arg, int state) 287915e32d5dSMike Smith { 288000a30448SNate Lawson int ret; 2881bee4aa4aSNate Lawson 2882b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 288315e32d5dSMike Smith 2884813d6dcaSNate Lawson /* Check if button action is disabled. */ 2885813d6dcaSNate Lawson if (state == ACPI_S_STATES_MAX + 1) 2886813d6dcaSNate Lawson return; 2887813d6dcaSNate Lawson 288800a30448SNate Lawson /* Request that the system prepare to enter the given suspend state. */ 288900a30448SNate Lawson ret = acpi_ReqSleepState((struct acpi_softc *)arg, state); 289000a30448SNate Lawson if (ret != 0) 289100a30448SNate Lawson printf("acpi: request to enter state S%d failed (err %d)\n", 289200a30448SNate Lawson state, ret); 2893bee4aa4aSNate Lawson 28940ae55423SMike Smith return_VOID; 289515e32d5dSMike Smith } 289615e32d5dSMike Smith 289715e32d5dSMike Smith static void 289815e32d5dSMike Smith acpi_system_eventhandler_wakeup(void *arg, int state) 289915e32d5dSMike Smith { 2900bee4aa4aSNate Lawson 2901b4a05238SPeter Wemm ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, state); 29020ae55423SMike Smith 2903bee4aa4aSNate Lawson /* Currently, nothing to do for wakeup. */ 2904cb9b0d80SMike Smith 29050ae55423SMike Smith return_VOID; 290615e32d5dSMike Smith } 290715e32d5dSMike Smith 290815e32d5dSMike Smith /* 290915e32d5dSMike Smith * ACPICA Event Handlers (FixedEvent, also called from button notify handler) 291015e32d5dSMike Smith */ 291115e32d5dSMike Smith UINT32 291233febf93SNate Lawson acpi_event_power_button_sleep(void *context) 291315e32d5dSMike Smith { 291415e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 291515e32d5dSMike Smith 2916b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29170ae55423SMike Smith 291815e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_power_button_sx); 29190ae55423SMike Smith 2920b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 292115e32d5dSMike Smith } 292215e32d5dSMike Smith 292315e32d5dSMike Smith UINT32 292433febf93SNate Lawson acpi_event_power_button_wake(void *context) 292515e32d5dSMike Smith { 292615e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 292715e32d5dSMike Smith 2928b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29290ae55423SMike Smith 293015e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_power_button_sx); 29310ae55423SMike Smith 2932b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 293315e32d5dSMike Smith } 293415e32d5dSMike Smith 293515e32d5dSMike Smith UINT32 293633febf93SNate Lawson acpi_event_sleep_button_sleep(void *context) 293715e32d5dSMike Smith { 293815e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 293915e32d5dSMike Smith 2940b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29410ae55423SMike Smith 294215e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_sleep_event, sc->acpi_sleep_button_sx); 29430ae55423SMike Smith 2944b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 294515e32d5dSMike Smith } 294615e32d5dSMike Smith 294715e32d5dSMike Smith UINT32 294833febf93SNate Lawson acpi_event_sleep_button_wake(void *context) 294915e32d5dSMike Smith { 295015e32d5dSMike Smith struct acpi_softc *sc = (struct acpi_softc *)context; 295115e32d5dSMike Smith 2952b4a05238SPeter Wemm ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__); 29530ae55423SMike Smith 295415e32d5dSMike Smith EVENTHANDLER_INVOKE(acpi_wakeup_event, sc->acpi_sleep_button_sx); 29550ae55423SMike Smith 2956b53f2771SMike Smith return_VALUE (ACPI_INTERRUPT_HANDLED); 295715e32d5dSMike Smith } 295815e32d5dSMike Smith 295915e32d5dSMike Smith /* 2960bee4aa4aSNate Lawson * XXX This static buffer is suboptimal. There is no locking so only 2961bee4aa4aSNate Lawson * use this for single-threaded callers. 296215e32d5dSMike Smith */ 296315e32d5dSMike Smith char * 296415e32d5dSMike Smith acpi_name(ACPI_HANDLE handle) 296515e32d5dSMike Smith { 2966bee4aa4aSNate Lawson ACPI_BUFFER buf; 2967bee4aa4aSNate Lawson static char data[256]; 296815e32d5dSMike Smith 2969bee4aa4aSNate Lawson buf.Length = sizeof(data); 2970bee4aa4aSNate Lawson buf.Pointer = data; 2971cb9b0d80SMike Smith 29720a9a1f44SNate Lawson if (handle && ACPI_SUCCESS(AcpiGetName(handle, ACPI_FULL_PATHNAME, &buf))) 2973bee4aa4aSNate Lawson return (data); 2974bee4aa4aSNate Lawson return ("(unknown)"); 297515e32d5dSMike Smith } 297615e32d5dSMike Smith 297715e32d5dSMike Smith /* 297815e32d5dSMike Smith * Debugging/bug-avoidance. Avoid trying to fetch info on various 297915e32d5dSMike Smith * parts of the namespace. 298015e32d5dSMike Smith */ 298115e32d5dSMike Smith int 298215e32d5dSMike Smith acpi_avoid(ACPI_HANDLE handle) 298315e32d5dSMike Smith { 29843c600cfbSJohn Baldwin char *cp, *env, *np; 298515e32d5dSMike Smith int len; 298615e32d5dSMike Smith 298715e32d5dSMike Smith np = acpi_name(handle); 298815e32d5dSMike Smith if (*np == '\\') 298915e32d5dSMike Smith np++; 29903c600cfbSJohn Baldwin if ((env = getenv("debug.acpi.avoid")) == NULL) 299115e32d5dSMike Smith return (0); 299215e32d5dSMike Smith 2993be2b1797SNate Lawson /* Scan the avoid list checking for a match */ 29943c600cfbSJohn Baldwin cp = env; 299515e32d5dSMike Smith for (;;) { 2996bee4aa4aSNate Lawson while (*cp != 0 && isspace(*cp)) 299715e32d5dSMike Smith cp++; 299815e32d5dSMike Smith if (*cp == 0) 299915e32d5dSMike Smith break; 300015e32d5dSMike Smith len = 0; 3001bee4aa4aSNate Lawson while (cp[len] != 0 && !isspace(cp[len])) 300215e32d5dSMike Smith len++; 3003d786139cSMaxime Henrion if (!strncmp(cp, np, len)) { 30043c600cfbSJohn Baldwin freeenv(env); 30050ae55423SMike Smith return(1); 3006d786139cSMaxime Henrion } 30070ae55423SMike Smith cp += len; 30080ae55423SMike Smith } 30093c600cfbSJohn Baldwin freeenv(env); 3010be2b1797SNate Lawson 30110ae55423SMike Smith return (0); 30120ae55423SMike Smith } 30130ae55423SMike Smith 30140ae55423SMike Smith /* 30150ae55423SMike Smith * Debugging/bug-avoidance. Disable ACPI subsystem components. 30160ae55423SMike Smith */ 30170ae55423SMike Smith int 30180ae55423SMike Smith acpi_disabled(char *subsys) 30190ae55423SMike Smith { 302078689b15SMaxime Henrion char *cp, *env; 30210ae55423SMike Smith int len; 30220ae55423SMike Smith 30233184cf5aSNate Lawson if ((env = getenv("debug.acpi.disabled")) == NULL) 30240ae55423SMike Smith return (0); 30253184cf5aSNate Lawson if (strcmp(env, "all") == 0) { 302678689b15SMaxime Henrion freeenv(env); 30270ae55423SMike Smith return (1); 3028d786139cSMaxime Henrion } 30290ae55423SMike Smith 30303184cf5aSNate Lawson /* Scan the disable list, checking for a match. */ 303178689b15SMaxime Henrion cp = env; 30320ae55423SMike Smith for (;;) { 30333184cf5aSNate Lawson while (*cp != '\0' && isspace(*cp)) 30340ae55423SMike Smith cp++; 30353184cf5aSNate Lawson if (*cp == '\0') 30360ae55423SMike Smith break; 30370ae55423SMike Smith len = 0; 30383184cf5aSNate Lawson while (cp[len] != '\0' && !isspace(cp[len])) 30390ae55423SMike Smith len++; 30403184cf5aSNate Lawson if (strncmp(cp, subsys, len) == 0) { 304178689b15SMaxime Henrion freeenv(env); 304215e32d5dSMike Smith return (1); 3043d786139cSMaxime Henrion } 304415e32d5dSMike Smith cp += len; 304515e32d5dSMike Smith } 304678689b15SMaxime Henrion freeenv(env); 3047be2b1797SNate Lawson 304815e32d5dSMike Smith return (0); 304915e32d5dSMike Smith } 305015e32d5dSMike Smith 305115e32d5dSMike Smith /* 305215e32d5dSMike Smith * Control interface. 305315e32d5dSMike Smith * 30540ae55423SMike Smith * We multiplex ioctls for all participating ACPI devices here. Individual 3055be2b1797SNate Lawson * drivers wanting to be accessible via /dev/acpi should use the 3056be2b1797SNate Lawson * register/deregister interface to make their handlers visible. 305715e32d5dSMike Smith */ 30580ae55423SMike Smith struct acpi_ioctl_hook 30590ae55423SMike Smith { 30600ae55423SMike Smith TAILQ_ENTRY(acpi_ioctl_hook) link; 30610ae55423SMike Smith u_long cmd; 3062be2b1797SNate Lawson acpi_ioctl_fn fn; 30630ae55423SMike Smith void *arg; 30640ae55423SMike Smith }; 30650ae55423SMike Smith 30660ae55423SMike Smith static TAILQ_HEAD(,acpi_ioctl_hook) acpi_ioctl_hooks; 30670ae55423SMike Smith static int acpi_ioctl_hooks_initted; 30680ae55423SMike Smith 30690ae55423SMike Smith int 3070be2b1797SNate Lawson acpi_register_ioctl(u_long cmd, acpi_ioctl_fn fn, void *arg) 30710ae55423SMike Smith { 30720ae55423SMike Smith struct acpi_ioctl_hook *hp; 30730ae55423SMike Smith 30740ae55423SMike Smith if ((hp = malloc(sizeof(*hp), M_ACPIDEV, M_NOWAIT)) == NULL) 30750ae55423SMike Smith return (ENOMEM); 30760ae55423SMike Smith hp->cmd = cmd; 30770ae55423SMike Smith hp->fn = fn; 30780ae55423SMike Smith hp->arg = arg; 307915e2f34fSNate Lawson 308015e2f34fSNate Lawson ACPI_LOCK(acpi); 30810ae55423SMike Smith if (acpi_ioctl_hooks_initted == 0) { 30820ae55423SMike Smith TAILQ_INIT(&acpi_ioctl_hooks); 30830ae55423SMike Smith acpi_ioctl_hooks_initted = 1; 30840ae55423SMike Smith } 30850ae55423SMike Smith TAILQ_INSERT_TAIL(&acpi_ioctl_hooks, hp, link); 308615e2f34fSNate Lawson ACPI_UNLOCK(acpi); 308715e2f34fSNate Lawson 30880ae55423SMike Smith return (0); 30890ae55423SMike Smith } 30900ae55423SMike Smith 30910ae55423SMike Smith void 3092be2b1797SNate Lawson acpi_deregister_ioctl(u_long cmd, acpi_ioctl_fn fn) 30930ae55423SMike Smith { 30940ae55423SMike Smith struct acpi_ioctl_hook *hp; 30950ae55423SMike Smith 309615e2f34fSNate Lawson ACPI_LOCK(acpi); 30970ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) 309815e2f34fSNate Lawson if (hp->cmd == cmd && hp->fn == fn) 30990ae55423SMike Smith break; 31000ae55423SMike Smith 31010ae55423SMike Smith if (hp != NULL) { 31020ae55423SMike Smith TAILQ_REMOVE(&acpi_ioctl_hooks, hp, link); 31030ae55423SMike Smith free(hp, M_ACPIDEV); 31040ae55423SMike Smith } 310515e2f34fSNate Lawson ACPI_UNLOCK(acpi); 31060ae55423SMike Smith } 31070ae55423SMike Smith 310815e32d5dSMike Smith static int 310989c9c53dSPoul-Henning Kamp acpiopen(struct cdev *dev, int flag, int fmt, d_thread_t *td) 311015e32d5dSMike Smith { 311115e32d5dSMike Smith return (0); 311215e32d5dSMike Smith } 311315e32d5dSMike Smith 311415e32d5dSMike Smith static int 311589c9c53dSPoul-Henning Kamp acpiclose(struct cdev *dev, int flag, int fmt, d_thread_t *td) 311615e32d5dSMike Smith { 311715e32d5dSMike Smith return (0); 311815e32d5dSMike Smith } 311915e32d5dSMike Smith 312015e32d5dSMike Smith static int 312189c9c53dSPoul-Henning Kamp acpiioctl(struct cdev *dev, u_long cmd, caddr_t addr, int flag, d_thread_t *td) 312215e32d5dSMike Smith { 312315e32d5dSMike Smith struct acpi_softc *sc; 31240ae55423SMike Smith struct acpi_ioctl_hook *hp; 3125bee4aa4aSNate Lawson int error, state; 312615e32d5dSMike Smith 3127a2e35898SDavid E. O'Brien error = 0; 3128a2e35898SDavid E. O'Brien hp = NULL; 312915e32d5dSMike Smith sc = dev->si_drv1; 313015e32d5dSMike Smith 31310ae55423SMike Smith /* 31320ae55423SMike Smith * Scan the list of registered ioctls, looking for handlers. 31330ae55423SMike Smith */ 313415e2f34fSNate Lawson ACPI_LOCK(acpi); 3135bee4aa4aSNate Lawson if (acpi_ioctl_hooks_initted) 31360ae55423SMike Smith TAILQ_FOREACH(hp, &acpi_ioctl_hooks, link) { 3137bee4aa4aSNate Lawson if (hp->cmd == cmd) 3138bee4aa4aSNate Lawson break; 31390ae55423SMike Smith } 314015e2f34fSNate Lawson ACPI_UNLOCK(acpi); 3141bee4aa4aSNate Lawson if (hp) 3142bee4aa4aSNate Lawson return (hp->fn(cmd, addr, hp->arg)); 31430ae55423SMike Smith 31440ae55423SMike Smith /* 3145a89fcf28STakanori Watanabe * Core ioctls are not permitted for non-writable user. 3146a89fcf28STakanori Watanabe * Currently, other ioctls just fetch information. 3147a89fcf28STakanori Watanabe * Not changing system behavior. 3148a89fcf28STakanori Watanabe */ 3149be2b1797SNate Lawson if ((flag & FWRITE) == 0) 3150be2b1797SNate Lawson return (EPERM); 3151a89fcf28STakanori Watanabe 3152be2b1797SNate Lawson /* Core system ioctls. */ 315315e32d5dSMike Smith switch (cmd) { 315400a30448SNate Lawson case ACPIIO_REQSLPSTATE: 315500a30448SNate Lawson state = *(int *)addr; 315600a30448SNate Lawson if (state != ACPI_STATE_S5) 315700a30448SNate Lawson error = acpi_ReqSleepState(sc, state); 315800a30448SNate Lawson else { 315900a30448SNate Lawson printf("power off via acpi ioctl not supported\n"); 316000a30448SNate Lawson error = ENXIO; 316100a30448SNate Lawson } 316200a30448SNate Lawson break; 316300a30448SNate Lawson case ACPIIO_ACKSLPSTATE: 316400a30448SNate Lawson error = *(int *)addr; 316500a30448SNate Lawson error = acpi_AckSleepState(sc->acpi_clone, error); 316600a30448SNate Lawson break; 316700a30448SNate Lawson case ACPIIO_SETSLPSTATE: /* DEPRECATED */ 3168bee4aa4aSNate Lawson error = EINVAL; 316915e32d5dSMike Smith state = *(int *)addr; 3170bee4aa4aSNate Lawson if (state >= ACPI_STATE_S0 && state <= ACPI_S_STATES_MAX) 3171bee4aa4aSNate Lawson if (ACPI_SUCCESS(acpi_SetSleepState(sc, state))) 3172bee4aa4aSNate Lawson error = 0; 317315e32d5dSMike Smith break; 317415e32d5dSMike Smith default: 3175bee4aa4aSNate Lawson error = ENXIO; 317615e32d5dSMike Smith break; 317715e32d5dSMike Smith } 3178917d44c8SMitsuru IWASAKI 317915e32d5dSMike Smith return (error); 318015e32d5dSMike Smith } 318115e32d5dSMike Smith 31821d073b1dSJohn Baldwin static int 3183d75de536SMitsuru IWASAKI acpi_supported_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 3184d75de536SMitsuru IWASAKI { 3185d75de536SMitsuru IWASAKI int error; 3186bee4aa4aSNate Lawson struct sbuf sb; 3187d75de536SMitsuru IWASAKI UINT8 state, TypeA, TypeB; 3188d75de536SMitsuru IWASAKI 3189bee4aa4aSNate Lawson sbuf_new(&sb, NULL, 32, SBUF_AUTOEXTEND); 3190bee4aa4aSNate Lawson for (state = ACPI_STATE_S1; state < ACPI_S_STATES_MAX + 1; state++) 3191bee4aa4aSNate Lawson if (ACPI_SUCCESS(AcpiGetSleepTypeData(state, &TypeA, &TypeB))) 3192bee4aa4aSNate Lawson sbuf_printf(&sb, "S%d ", state); 3193bee4aa4aSNate Lawson sbuf_trim(&sb); 3194bee4aa4aSNate Lawson sbuf_finish(&sb); 3195bee4aa4aSNate Lawson error = sysctl_handle_string(oidp, sbuf_data(&sb), sbuf_len(&sb), req); 3196bee4aa4aSNate Lawson sbuf_delete(&sb); 3197d75de536SMitsuru IWASAKI return (error); 3198d75de536SMitsuru IWASAKI } 3199d75de536SMitsuru IWASAKI 3200d75de536SMitsuru IWASAKI static int 32011d073b1dSJohn Baldwin acpi_sleep_state_sysctl(SYSCTL_HANDLER_ARGS) 32021d073b1dSJohn Baldwin { 32031d073b1dSJohn Baldwin char sleep_state[10]; 32041d073b1dSJohn Baldwin int error; 32051d073b1dSJohn Baldwin u_int new_state, old_state; 32061d073b1dSJohn Baldwin 32071d073b1dSJohn Baldwin old_state = *(u_int *)oidp->oid_arg1; 3208bee4aa4aSNate Lawson if (old_state > ACPI_S_STATES_MAX + 1) 3209bee4aa4aSNate Lawson strlcpy(sleep_state, "unknown", sizeof(sleep_state)); 3210bee4aa4aSNate Lawson else 3211bee4aa4aSNate Lawson strlcpy(sleep_state, sleep_state_names[old_state], sizeof(sleep_state)); 32121d073b1dSJohn Baldwin error = sysctl_handle_string(oidp, sleep_state, sizeof(sleep_state), req); 32131d073b1dSJohn Baldwin if (error == 0 && req->newptr != NULL) { 3214be2b1797SNate Lawson new_state = ACPI_STATE_S0; 3215bee4aa4aSNate Lawson for (; new_state <= ACPI_S_STATES_MAX + 1; new_state++) 3216bee4aa4aSNate Lawson if (strcmp(sleep_state, sleep_state_names[new_state]) == 0) 32171d073b1dSJohn Baldwin break; 3218931a10c9SMitsuru IWASAKI if (new_state <= ACPI_S_STATES_MAX + 1) { 3219be2b1797SNate Lawson if (new_state != old_state) 32201d073b1dSJohn Baldwin *(u_int *)oidp->oid_arg1 = new_state; 3221bee4aa4aSNate Lawson } else 32221d073b1dSJohn Baldwin error = EINVAL; 32231d073b1dSJohn Baldwin } 3224be2b1797SNate Lawson 32251d073b1dSJohn Baldwin return (error); 32261d073b1dSJohn Baldwin } 32271d073b1dSJohn Baldwin 32289b937d48SNate Lawson /* Inform devctl(4) when we receive a Notify. */ 32299b937d48SNate Lawson void 32309b937d48SNate Lawson acpi_UserNotify(const char *subsystem, ACPI_HANDLE h, uint8_t notify) 32319b937d48SNate Lawson { 32329b937d48SNate Lawson char notify_buf[16]; 32339b937d48SNate Lawson ACPI_BUFFER handle_buf; 32349b937d48SNate Lawson ACPI_STATUS status; 32359b937d48SNate Lawson 32369b937d48SNate Lawson if (subsystem == NULL) 32379b937d48SNate Lawson return; 32389b937d48SNate Lawson 32399b937d48SNate Lawson handle_buf.Pointer = NULL; 32409b937d48SNate Lawson handle_buf.Length = ACPI_ALLOCATE_BUFFER; 32419b937d48SNate Lawson status = AcpiNsHandleToPathname(h, &handle_buf); 32429b937d48SNate Lawson if (ACPI_FAILURE(status)) 32439b937d48SNate Lawson return; 32449b937d48SNate Lawson snprintf(notify_buf, sizeof(notify_buf), "notify=0x%02x", notify); 32459b937d48SNate Lawson devctl_notify("ACPI", subsystem, handle_buf.Pointer, notify_buf); 32469b937d48SNate Lawson AcpiOsFree(handle_buf.Pointer); 32479b937d48SNate Lawson } 32489b937d48SNate Lawson 324915e32d5dSMike Smith #ifdef ACPI_DEBUG 32500ae55423SMike Smith /* 32510ae55423SMike Smith * Support for parsing debug options from the kernel environment. 32520ae55423SMike Smith * 32530ae55423SMike Smith * Bits may be set in the AcpiDbgLayer and AcpiDbgLevel debug registers 32540ae55423SMike Smith * by specifying the names of the bits in the debug.acpi.layer and 32550ae55423SMike Smith * debug.acpi.level environment variables. Bits may be unset by 32560ae55423SMike Smith * prefixing the bit name with !. 32570ae55423SMike Smith */ 325815e32d5dSMike Smith struct debugtag 325915e32d5dSMike Smith { 326015e32d5dSMike Smith char *name; 326115e32d5dSMike Smith UINT32 value; 326215e32d5dSMike Smith }; 326315e32d5dSMike Smith 326415e32d5dSMike Smith static struct debugtag dbg_layer[] = { 3265c5ba0be4SMike Smith {"ACPI_UTILITIES", ACPI_UTILITIES}, 3266c5ba0be4SMike Smith {"ACPI_HARDWARE", ACPI_HARDWARE}, 3267c5ba0be4SMike Smith {"ACPI_EVENTS", ACPI_EVENTS}, 3268c5ba0be4SMike Smith {"ACPI_TABLES", ACPI_TABLES}, 3269c5ba0be4SMike Smith {"ACPI_NAMESPACE", ACPI_NAMESPACE}, 3270c5ba0be4SMike Smith {"ACPI_PARSER", ACPI_PARSER}, 3271c5ba0be4SMike Smith {"ACPI_DISPATCHER", ACPI_DISPATCHER}, 3272c5ba0be4SMike Smith {"ACPI_EXECUTER", ACPI_EXECUTER}, 3273c5ba0be4SMike Smith {"ACPI_RESOURCES", ACPI_RESOURCES}, 3274d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DEBUGGER", ACPI_CA_DEBUGGER}, 32754c1cdee6SMike Smith {"ACPI_OS_SERVICES", ACPI_OS_SERVICES}, 3276d62ab2f4SMitsuru IWASAKI {"ACPI_CA_DISASSEMBLER", ACPI_CA_DISASSEMBLER}, 3277297835bcSNate Lawson {"ACPI_ALL_COMPONENTS", ACPI_ALL_COMPONENTS}, 32784c1cdee6SMike Smith 3279c5ba0be4SMike Smith {"ACPI_AC_ADAPTER", ACPI_AC_ADAPTER}, 3280c5ba0be4SMike Smith {"ACPI_BATTERY", ACPI_BATTERY}, 32813184cf5aSNate Lawson {"ACPI_BUS", ACPI_BUS}, 3282c5ba0be4SMike Smith {"ACPI_BUTTON", ACPI_BUTTON}, 32833184cf5aSNate Lawson {"ACPI_EC", ACPI_EC}, 32843184cf5aSNate Lawson {"ACPI_FAN", ACPI_FAN}, 32853184cf5aSNate Lawson {"ACPI_POWERRES", ACPI_POWERRES}, 32864c1cdee6SMike Smith {"ACPI_PROCESSOR", ACPI_PROCESSOR}, 3287cb9b0d80SMike Smith {"ACPI_THERMAL", ACPI_THERMAL}, 32883184cf5aSNate Lawson {"ACPI_TIMER", ACPI_TIMER}, 3289b53f2771SMike Smith {"ACPI_ALL_DRIVERS", ACPI_ALL_DRIVERS}, 329015e32d5dSMike Smith {NULL, 0} 329115e32d5dSMike Smith }; 329215e32d5dSMike Smith 329315e32d5dSMike Smith static struct debugtag dbg_level[] = { 32944c1cdee6SMike Smith {"ACPI_LV_ERROR", ACPI_LV_ERROR}, 32959501b603SJohn Baldwin {"ACPI_LV_WARN", ACPI_LV_WARN}, 32969501b603SJohn Baldwin {"ACPI_LV_INIT", ACPI_LV_INIT}, 32974c1cdee6SMike Smith {"ACPI_LV_DEBUG_OBJECT", ACPI_LV_DEBUG_OBJECT}, 32989501b603SJohn Baldwin {"ACPI_LV_INFO", ACPI_LV_INFO}, 32994c1cdee6SMike Smith {"ACPI_LV_ALL_EXCEPTIONS", ACPI_LV_ALL_EXCEPTIONS}, 3300d62ab2f4SMitsuru IWASAKI 3301d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 1 [Standard Trace Level] */ 3302297835bcSNate Lawson {"ACPI_LV_INIT_NAMES", ACPI_LV_INIT_NAMES}, 33034c1cdee6SMike Smith {"ACPI_LV_PARSE", ACPI_LV_PARSE}, 33044c1cdee6SMike Smith {"ACPI_LV_LOAD", ACPI_LV_LOAD}, 3305d62ab2f4SMitsuru IWASAKI {"ACPI_LV_DISPATCH", ACPI_LV_DISPATCH}, 33064c1cdee6SMike Smith {"ACPI_LV_EXEC", ACPI_LV_EXEC}, 33074c1cdee6SMike Smith {"ACPI_LV_NAMES", ACPI_LV_NAMES}, 33084c1cdee6SMike Smith {"ACPI_LV_OPREGION", ACPI_LV_OPREGION}, 33094c1cdee6SMike Smith {"ACPI_LV_BFIELD", ACPI_LV_BFIELD}, 33104c1cdee6SMike Smith {"ACPI_LV_TABLES", ACPI_LV_TABLES}, 33114c1cdee6SMike Smith {"ACPI_LV_VALUES", ACPI_LV_VALUES}, 33124c1cdee6SMike Smith {"ACPI_LV_OBJECTS", ACPI_LV_OBJECTS}, 33134c1cdee6SMike Smith {"ACPI_LV_RESOURCES", ACPI_LV_RESOURCES}, 33144c1cdee6SMike Smith {"ACPI_LV_USER_REQUESTS", ACPI_LV_USER_REQUESTS}, 33154c1cdee6SMike Smith {"ACPI_LV_PACKAGE", ACPI_LV_PACKAGE}, 3316d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY1", ACPI_LV_VERBOSITY1}, 3317d62ab2f4SMitsuru IWASAKI 3318d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 2 [Function tracing and memory allocation] */ 3319d62ab2f4SMitsuru IWASAKI {"ACPI_LV_ALLOCATIONS", ACPI_LV_ALLOCATIONS}, 3320d62ab2f4SMitsuru IWASAKI {"ACPI_LV_FUNCTIONS", ACPI_LV_FUNCTIONS}, 3321d62ab2f4SMitsuru IWASAKI {"ACPI_LV_OPTIMIZATIONS", ACPI_LV_OPTIMIZATIONS}, 3322d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY2", ACPI_LV_VERBOSITY2}, 33234c1cdee6SMike Smith {"ACPI_LV_ALL", ACPI_LV_ALL}, 3324d62ab2f4SMitsuru IWASAKI 3325d62ab2f4SMitsuru IWASAKI /* Trace verbosity level 3 [Threading, I/O, and Interrupts] */ 3326d62ab2f4SMitsuru IWASAKI {"ACPI_LV_MUTEX", ACPI_LV_MUTEX}, 3327d62ab2f4SMitsuru IWASAKI {"ACPI_LV_THREADS", ACPI_LV_THREADS}, 3328d62ab2f4SMitsuru IWASAKI {"ACPI_LV_IO", ACPI_LV_IO}, 3329d62ab2f4SMitsuru IWASAKI {"ACPI_LV_INTERRUPTS", ACPI_LV_INTERRUPTS}, 3330d62ab2f4SMitsuru IWASAKI {"ACPI_LV_VERBOSITY3", ACPI_LV_VERBOSITY3}, 3331d62ab2f4SMitsuru IWASAKI 3332d62ab2f4SMitsuru IWASAKI /* Exceptionally verbose output -- also used in the global "DebugLevel" */ 333398479b04SMitsuru IWASAKI {"ACPI_LV_AML_DISASSEMBLE", ACPI_LV_AML_DISASSEMBLE}, 333498479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE_INFO", ACPI_LV_VERBOSE_INFO}, 333598479b04SMitsuru IWASAKI {"ACPI_LV_FULL_TABLES", ACPI_LV_FULL_TABLES}, 333698479b04SMitsuru IWASAKI {"ACPI_LV_EVENTS", ACPI_LV_EVENTS}, 333798479b04SMitsuru IWASAKI {"ACPI_LV_VERBOSE", ACPI_LV_VERBOSE}, 333815e32d5dSMike Smith {NULL, 0} 333915e32d5dSMike Smith }; 334015e32d5dSMike Smith 334115e32d5dSMike Smith static void 334215e32d5dSMike Smith acpi_parse_debug(char *cp, struct debugtag *tag, UINT32 *flag) 334315e32d5dSMike Smith { 334415e32d5dSMike Smith char *ep; 334515e32d5dSMike Smith int i, l; 33460ae55423SMike Smith int set; 334715e32d5dSMike Smith 334815e32d5dSMike Smith while (*cp) { 334915e32d5dSMike Smith if (isspace(*cp)) { 335015e32d5dSMike Smith cp++; 335115e32d5dSMike Smith continue; 335215e32d5dSMike Smith } 335315e32d5dSMike Smith ep = cp; 335415e32d5dSMike Smith while (*ep && !isspace(*ep)) 335515e32d5dSMike Smith ep++; 33560ae55423SMike Smith if (*cp == '!') { 33570ae55423SMike Smith set = 0; 33580ae55423SMike Smith cp++; 33590ae55423SMike Smith if (cp == ep) 33600ae55423SMike Smith continue; 33610ae55423SMike Smith } else { 33620ae55423SMike Smith set = 1; 33630ae55423SMike Smith } 336415e32d5dSMike Smith l = ep - cp; 336515e32d5dSMike Smith for (i = 0; tag[i].name != NULL; i++) { 336615e32d5dSMike Smith if (!strncmp(cp, tag[i].name, l)) { 3367be2b1797SNate Lawson if (set) 336815e32d5dSMike Smith *flag |= tag[i].value; 3369be2b1797SNate Lawson else 33700ae55423SMike Smith *flag &= ~tag[i].value; 337115e32d5dSMike Smith } 337215e32d5dSMike Smith } 337315e32d5dSMike Smith cp = ep; 337415e32d5dSMike Smith } 337515e32d5dSMike Smith } 337615e32d5dSMike Smith 337715e32d5dSMike Smith static void 33782a4ac806SMike Smith acpi_set_debugging(void *junk) 337915e32d5dSMike Smith { 33807e639165SNate Lawson char *layer, *level; 338115e32d5dSMike Smith 33821d7b121cSNate Lawson if (cold) { 33830ae55423SMike Smith AcpiDbgLayer = 0; 33840ae55423SMike Smith AcpiDbgLevel = 0; 33851d7b121cSNate Lawson } 33861d7b121cSNate Lawson 33877e639165SNate Lawson layer = getenv("debug.acpi.layer"); 33887e639165SNate Lawson level = getenv("debug.acpi.level"); 33897e639165SNate Lawson if (layer == NULL && level == NULL) 33907e639165SNate Lawson return; 33910ae55423SMike Smith 33927e639165SNate Lawson printf("ACPI set debug"); 33937e639165SNate Lawson if (layer != NULL) { 33947e639165SNate Lawson if (strcmp("NONE", layer) != 0) 33957e639165SNate Lawson printf(" layer '%s'", layer); 33967e639165SNate Lawson acpi_parse_debug(layer, &dbg_layer[0], &AcpiDbgLayer); 33977e639165SNate Lawson freeenv(layer); 33981d7b121cSNate Lawson } 33997e639165SNate Lawson if (level != NULL) { 34007e639165SNate Lawson if (strcmp("NONE", level) != 0) 34017e639165SNate Lawson printf(" level '%s'", level); 34027e639165SNate Lawson acpi_parse_debug(level, &dbg_level[0], &AcpiDbgLevel); 34037e639165SNate Lawson freeenv(level); 34047e639165SNate Lawson } 34057e639165SNate Lawson printf("\n"); 340615e32d5dSMike Smith } 3407bee4aa4aSNate Lawson 3408be2b1797SNate Lawson SYSINIT(acpi_debugging, SI_SUB_TUNABLES, SI_ORDER_ANY, acpi_set_debugging, 3409be2b1797SNate Lawson NULL); 34101d7b121cSNate Lawson 34111d7b121cSNate Lawson static int 34121d7b121cSNate Lawson acpi_debug_sysctl(SYSCTL_HANDLER_ARGS) 34131d7b121cSNate Lawson { 341454f6bca0SNate Lawson int error, *dbg; 34151d7b121cSNate Lawson struct debugtag *tag; 341654f6bca0SNate Lawson struct sbuf sb; 34171d7b121cSNate Lawson 341854f6bca0SNate Lawson if (sbuf_new(&sb, NULL, 128, SBUF_AUTOEXTEND) == NULL) 341954f6bca0SNate Lawson return (ENOMEM); 34201d7b121cSNate Lawson if (strcmp(oidp->oid_arg1, "debug.acpi.layer") == 0) { 34211d7b121cSNate Lawson tag = &dbg_layer[0]; 34221d7b121cSNate Lawson dbg = &AcpiDbgLayer; 34231d7b121cSNate Lawson } else { 34241d7b121cSNate Lawson tag = &dbg_level[0]; 34251d7b121cSNate Lawson dbg = &AcpiDbgLevel; 34261d7b121cSNate Lawson } 34271d7b121cSNate Lawson 34281d7b121cSNate Lawson /* Get old values if this is a get request. */ 342915e2f34fSNate Lawson ACPI_SERIAL_BEGIN(acpi); 34301d7b121cSNate Lawson if (*dbg == 0) { 343154f6bca0SNate Lawson sbuf_cpy(&sb, "NONE"); 34321d7b121cSNate Lawson } else if (req->newptr == NULL) { 34331d7b121cSNate Lawson for (; tag->name != NULL; tag++) { 343454f6bca0SNate Lawson if ((*dbg & tag->value) == tag->value) 343554f6bca0SNate Lawson sbuf_printf(&sb, "%s ", tag->name); 34361d7b121cSNate Lawson } 34371d7b121cSNate Lawson } 343854f6bca0SNate Lawson sbuf_trim(&sb); 343954f6bca0SNate Lawson sbuf_finish(&sb); 34401d7b121cSNate Lawson 34417e639165SNate Lawson /* Copy out the old values to the user. */ 34427e639165SNate Lawson error = SYSCTL_OUT(req, sbuf_data(&sb), sbuf_len(&sb)); 344354f6bca0SNate Lawson sbuf_delete(&sb); 34441d7b121cSNate Lawson 34451d7b121cSNate Lawson /* If the user is setting a string, parse it. */ 34461d7b121cSNate Lawson if (error == 0 && req->newptr != NULL) { 34471d7b121cSNate Lawson *dbg = 0; 34481d7b121cSNate Lawson setenv((char *)oidp->oid_arg1, (char *)req->newptr); 34491d7b121cSNate Lawson acpi_set_debugging(NULL); 34501d7b121cSNate Lawson } 345115e2f34fSNate Lawson ACPI_SERIAL_END(acpi); 34521d7b121cSNate Lawson 34531d7b121cSNate Lawson return (error); 34541d7b121cSNate Lawson } 3455bee4aa4aSNate Lawson 34561d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, layer, CTLFLAG_RW | CTLTYPE_STRING, 34571d7b121cSNate Lawson "debug.acpi.layer", 0, acpi_debug_sysctl, "A", ""); 34581d7b121cSNate Lawson SYSCTL_PROC(_debug_acpi, OID_AUTO, level, CTLFLAG_RW | CTLTYPE_STRING, 34591d7b121cSNate Lawson "debug.acpi.level", 0, acpi_debug_sysctl, "A", ""); 3460bee4aa4aSNate Lawson #endif /* ACPI_DEBUG */ 3461f9390180SMitsuru IWASAKI 3462f9390180SMitsuru IWASAKI static int 3463f9390180SMitsuru IWASAKI acpi_pm_func(u_long cmd, void *arg, ...) 3464f9390180SMitsuru IWASAKI { 3465f9390180SMitsuru IWASAKI int state, acpi_state; 3466f9390180SMitsuru IWASAKI int error; 3467f9390180SMitsuru IWASAKI struct acpi_softc *sc; 3468f9390180SMitsuru IWASAKI va_list ap; 3469f9390180SMitsuru IWASAKI 3470f9390180SMitsuru IWASAKI error = 0; 3471f9390180SMitsuru IWASAKI switch (cmd) { 3472f9390180SMitsuru IWASAKI case POWER_CMD_SUSPEND: 3473f9390180SMitsuru IWASAKI sc = (struct acpi_softc *)arg; 3474f9390180SMitsuru IWASAKI if (sc == NULL) { 3475f9390180SMitsuru IWASAKI error = EINVAL; 3476f9390180SMitsuru IWASAKI goto out; 3477f9390180SMitsuru IWASAKI } 3478f9390180SMitsuru IWASAKI 3479f9390180SMitsuru IWASAKI va_start(ap, arg); 3480f9390180SMitsuru IWASAKI state = va_arg(ap, int); 3481f9390180SMitsuru IWASAKI va_end(ap); 3482f9390180SMitsuru IWASAKI 3483f9390180SMitsuru IWASAKI switch (state) { 3484f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_STANDBY: 3485f9390180SMitsuru IWASAKI acpi_state = sc->acpi_standby_sx; 3486f9390180SMitsuru IWASAKI break; 3487f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_SUSPEND: 3488f9390180SMitsuru IWASAKI acpi_state = sc->acpi_suspend_sx; 3489f9390180SMitsuru IWASAKI break; 3490f9390180SMitsuru IWASAKI case POWER_SLEEP_STATE_HIBERNATE: 3491f9390180SMitsuru IWASAKI acpi_state = ACPI_STATE_S4; 3492f9390180SMitsuru IWASAKI break; 3493f9390180SMitsuru IWASAKI default: 3494f9390180SMitsuru IWASAKI error = EINVAL; 3495f9390180SMitsuru IWASAKI goto out; 3496f9390180SMitsuru IWASAKI } 3497f9390180SMitsuru IWASAKI 349800a30448SNate Lawson if (ACPI_FAILURE(acpi_EnterSleepState(sc, acpi_state))) 349900a30448SNate Lawson error = ENXIO; 3500f9390180SMitsuru IWASAKI break; 3501f9390180SMitsuru IWASAKI default: 3502f9390180SMitsuru IWASAKI error = EINVAL; 3503f9390180SMitsuru IWASAKI goto out; 3504f9390180SMitsuru IWASAKI } 3505f9390180SMitsuru IWASAKI 3506f9390180SMitsuru IWASAKI out: 3507f9390180SMitsuru IWASAKI return (error); 3508f9390180SMitsuru IWASAKI } 3509f9390180SMitsuru IWASAKI 3510f9390180SMitsuru IWASAKI static void 3511f9390180SMitsuru IWASAKI acpi_pm_register(void *arg) 3512f9390180SMitsuru IWASAKI { 3513be2b1797SNate Lawson if (!cold || resource_disabled("acpi", 0)) 35146c407052SMitsuru IWASAKI return; 3515f9390180SMitsuru IWASAKI 3516f9390180SMitsuru IWASAKI power_pm_register(POWER_PM_TYPE_ACPI, acpi_pm_func, NULL); 3517f9390180SMitsuru IWASAKI } 3518f9390180SMitsuru IWASAKI 3519f9390180SMitsuru IWASAKI SYSINIT(power, SI_SUB_KLD, SI_ORDER_ANY, acpi_pm_register, 0); 3520