The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/dev/acpica/acpi_ec.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

    1 /*-
    2  * Copyright (c) 2003-2007 Nate Lawson
    3  * Copyright (c) 2000 Michael Smith
    4  * Copyright (c) 2000 BSDi
    5  * All rights reserved.
    6  *
    7  * Redistribution and use in source and binary forms, with or without
    8  * modification, are permitted provided that the following conditions
    9  * are met:
   10  * 1. Redistributions of source code must retain the above copyright
   11  *    notice, this list of conditions and the following disclaimer.
   12  * 2. Redistributions in binary form must reproduce the above copyright
   13  *    notice, this list of conditions and the following disclaimer in the
   14  *    documentation and/or other materials provided with the distribution.
   15  *
   16  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
   17  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   18  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   19  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
   20  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   21  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   22  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   23  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   24  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   25  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   26  * SUCH DAMAGE.
   27  */
   28 
   29 #include <sys/cdefs.h>
   30 __FBSDID("$FreeBSD: releng/7.3/sys/dev/acpica/acpi_ec.c 202793 2010-01-22 09:42:41Z avg $");
   31 
   32 #include "opt_acpi.h"
   33 #include <sys/param.h>
   34 #include <sys/kernel.h>
   35 #include <sys/bus.h>
   36 #include <sys/lock.h>
   37 #include <sys/malloc.h>
   38 #include <sys/module.h>
   39 #include <sys/sx.h>
   40 
   41 #include <machine/bus.h>
   42 #include <machine/resource.h>
   43 #include <sys/rman.h>
   44 
   45 #include <contrib/dev/acpica/acpi.h>
   46 #include <dev/acpica/acpivar.h>
   47 
   48 /* Hooks for the ACPI CA debugging infrastructure */
   49 #define _COMPONENT      ACPI_EC
   50 ACPI_MODULE_NAME("EC")
   51 
   52 /*
   53  * EC_COMMAND:
   54  * -----------
   55  */
   56 typedef UINT8                           EC_COMMAND;
   57 
   58 #define EC_COMMAND_UNKNOWN              ((EC_COMMAND) 0x00)
   59 #define EC_COMMAND_READ                 ((EC_COMMAND) 0x80)
   60 #define EC_COMMAND_WRITE                ((EC_COMMAND) 0x81)
   61 #define EC_COMMAND_BURST_ENABLE         ((EC_COMMAND) 0x82)
   62 #define EC_COMMAND_BURST_DISABLE        ((EC_COMMAND) 0x83)
   63 #define EC_COMMAND_QUERY                ((EC_COMMAND) 0x84)
   64 
   65 /*
   66  * EC_STATUS:
   67  * ----------
   68  * The encoding of the EC status register is illustrated below.
   69  * Note that a set bit (1) indicates the property is TRUE
   70  * (e.g. if bit 0 is set then the output buffer is full).
   71  * +-+-+-+-+-+-+-+-+
   72  * |7|6|5|4|3|2|1|0|
   73  * +-+-+-+-+-+-+-+-+
   74  *  | | | | | | | |
   75  *  | | | | | | | +- Output Buffer Full?
   76  *  | | | | | | +--- Input Buffer Full?
   77  *  | | | | | +----- <reserved>
   78  *  | | | | +------- Data Register is Command Byte?
   79  *  | | | +--------- Burst Mode Enabled?
   80  *  | | +----------- SCI Event?
   81  *  | +------------- SMI Event?
   82  *  +--------------- <reserved>
   83  *
   84  */
   85 typedef UINT8                           EC_STATUS;
   86 
   87 #define EC_FLAG_OUTPUT_BUFFER           ((EC_STATUS) 0x01)
   88 #define EC_FLAG_INPUT_BUFFER            ((EC_STATUS) 0x02)
   89 #define EC_FLAG_DATA_IS_CMD             ((EC_STATUS) 0x08)
   90 #define EC_FLAG_BURST_MODE              ((EC_STATUS) 0x10)
   91 
   92 /*
   93  * EC_EVENT:
   94  * ---------
   95  */
   96 typedef UINT8                           EC_EVENT;
   97 
   98 #define EC_EVENT_UNKNOWN                ((EC_EVENT) 0x00)
   99 #define EC_EVENT_OUTPUT_BUFFER_FULL     ((EC_EVENT) 0x01)
  100 #define EC_EVENT_INPUT_BUFFER_EMPTY     ((EC_EVENT) 0x02)
  101 #define EC_EVENT_SCI                    ((EC_EVENT) 0x20)
  102 #define EC_EVENT_SMI                    ((EC_EVENT) 0x40)
  103 
  104 /* Data byte returned after burst enable indicating it was successful. */
  105 #define EC_BURST_ACK                    0x90
  106 
  107 /*
  108  * Register access primitives
  109  */
  110 #define EC_GET_DATA(sc)                                                 \
  111         bus_space_read_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0)
  112 
  113 #define EC_SET_DATA(sc, v)                                              \
  114         bus_space_write_1((sc)->ec_data_tag, (sc)->ec_data_handle, 0, (v))
  115 
  116 #define EC_GET_CSR(sc)                                                  \
  117         bus_space_read_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0)
  118 
  119 #define EC_SET_CSR(sc, v)                                               \
  120         bus_space_write_1((sc)->ec_csr_tag, (sc)->ec_csr_handle, 0, (v))
  121 
  122 /* Additional params to pass from the probe routine */
  123 struct acpi_ec_params {
  124     int         glk;
  125     int         gpe_bit;
  126     ACPI_HANDLE gpe_handle;
  127     int         uid;
  128 };
  129 
  130 /*
  131  * Driver softc.
  132  */
  133 struct acpi_ec_softc {
  134     device_t            ec_dev;
  135     ACPI_HANDLE         ec_handle;
  136     int                 ec_uid;
  137     ACPI_HANDLE         ec_gpehandle;
  138     UINT8               ec_gpebit;
  139 
  140     int                 ec_data_rid;
  141     struct resource     *ec_data_res;
  142     bus_space_tag_t     ec_data_tag;
  143     bus_space_handle_t  ec_data_handle;
  144 
  145     int                 ec_csr_rid;
  146     struct resource     *ec_csr_res;
  147     bus_space_tag_t     ec_csr_tag;
  148     bus_space_handle_t  ec_csr_handle;
  149 
  150     int                 ec_glk;
  151     int                 ec_glkhandle;
  152     int                 ec_burstactive;
  153     int                 ec_sci_pend;
  154     u_int               ec_gencount;
  155     int                 ec_suspending;
  156 };
  157 
  158 /*
  159  * XXX njl
  160  * I couldn't find it in the spec but other implementations also use a
  161  * value of 1 ms for the time to acquire global lock.
  162  */
  163 #define EC_LOCK_TIMEOUT 1000
  164 
  165 /* Default delay in microseconds between each run of the status polling loop. */
  166 #define EC_POLL_DELAY   5
  167 
  168 /* Total time in ms spent waiting for a response from EC. */
  169 #define EC_TIMEOUT      750
  170 
  171 #define EVENT_READY(event, status)                      \
  172         (((event) == EC_EVENT_OUTPUT_BUFFER_FULL &&     \
  173          ((status) & EC_FLAG_OUTPUT_BUFFER) != 0) ||    \
  174          ((event) == EC_EVENT_INPUT_BUFFER_EMPTY &&     \
  175          ((status) & EC_FLAG_INPUT_BUFFER) == 0))
  176 
  177 ACPI_SERIAL_DECL(ec, "ACPI embedded controller");
  178 
  179 SYSCTL_DECL(_debug_acpi);
  180 SYSCTL_NODE(_debug_acpi, OID_AUTO, ec, CTLFLAG_RD, NULL, "EC debugging");
  181 
  182 static int      ec_burst_mode;
  183 TUNABLE_INT("debug.acpi.ec.burst", &ec_burst_mode);
  184 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, burst, CTLFLAG_RW, &ec_burst_mode, 0,
  185     "Enable use of burst mode (faster for nearly all systems)");
  186 static int      ec_polled_mode;
  187 TUNABLE_INT("debug.acpi.ec.polled", &ec_polled_mode);
  188 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, polled, CTLFLAG_RW, &ec_polled_mode, 0,
  189     "Force use of polled mode (only if interrupt mode doesn't work)");
  190 static int      ec_timeout = EC_TIMEOUT;
  191 TUNABLE_INT("debug.acpi.ec.timeout", &ec_timeout);
  192 SYSCTL_INT(_debug_acpi_ec, OID_AUTO, timeout, CTLFLAG_RW, &ec_timeout,
  193     EC_TIMEOUT, "Total time spent waiting for a response (poll+sleep)");
  194 
  195 static ACPI_STATUS
  196 EcLock(struct acpi_ec_softc *sc)
  197 {
  198     ACPI_STATUS status;
  199 
  200     /* If _GLK is non-zero, acquire the global lock. */
  201     status = AE_OK;
  202     if (sc->ec_glk) {
  203         status = AcpiAcquireGlobalLock(EC_LOCK_TIMEOUT, &sc->ec_glkhandle);
  204         if (ACPI_FAILURE(status))
  205             return (status);
  206     }
  207     ACPI_SERIAL_BEGIN(ec);
  208     return (status);
  209 }
  210 
  211 static void
  212 EcUnlock(struct acpi_ec_softc *sc)
  213 {
  214     ACPI_SERIAL_END(ec);
  215     if (sc->ec_glk)
  216         AcpiReleaseGlobalLock(sc->ec_glkhandle);
  217 }
  218 
  219 static uint32_t         EcGpeHandler(void *Context);
  220 static ACPI_STATUS      EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function,
  221                                 void *Context, void **return_Context);
  222 static ACPI_STATUS      EcSpaceHandler(UINT32 Function,
  223                                 ACPI_PHYSICAL_ADDRESS Address,
  224                                 UINT32 width, ACPI_INTEGER *Value,
  225                                 void *Context, void *RegionContext);
  226 static ACPI_STATUS      EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event,
  227                                 u_int gen_count);
  228 static ACPI_STATUS      EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd);
  229 static ACPI_STATUS      EcRead(struct acpi_ec_softc *sc, UINT8 Address,
  230                                 UINT8 *Data);
  231 static ACPI_STATUS      EcWrite(struct acpi_ec_softc *sc, UINT8 Address,
  232                                 UINT8 *Data);
  233 static int              acpi_ec_probe(device_t dev);
  234 static int              acpi_ec_attach(device_t dev);
  235 static int              acpi_ec_suspend(device_t dev);
  236 static int              acpi_ec_resume(device_t dev);
  237 static int              acpi_ec_shutdown(device_t dev);
  238 static int              acpi_ec_read_method(device_t dev, u_int addr,
  239                                 ACPI_INTEGER *val, int width);
  240 static int              acpi_ec_write_method(device_t dev, u_int addr,
  241                                 ACPI_INTEGER val, int width);
  242 
  243 static device_method_t acpi_ec_methods[] = {
  244     /* Device interface */
  245     DEVMETHOD(device_probe,     acpi_ec_probe),
  246     DEVMETHOD(device_attach,    acpi_ec_attach),
  247     DEVMETHOD(device_suspend,   acpi_ec_suspend),
  248     DEVMETHOD(device_resume,    acpi_ec_resume),
  249     DEVMETHOD(device_shutdown,  acpi_ec_shutdown),
  250 
  251     /* Embedded controller interface */
  252     DEVMETHOD(acpi_ec_read,     acpi_ec_read_method),
  253     DEVMETHOD(acpi_ec_write,    acpi_ec_write_method),
  254 
  255     {0, 0}
  256 };
  257 
  258 static driver_t acpi_ec_driver = {
  259     "acpi_ec",
  260     acpi_ec_methods,
  261     sizeof(struct acpi_ec_softc),
  262 };
  263 
  264 static devclass_t acpi_ec_devclass;
  265 DRIVER_MODULE(acpi_ec, acpi, acpi_ec_driver, acpi_ec_devclass, 0, 0);
  266 MODULE_DEPEND(acpi_ec, acpi, 1, 1, 1);
  267 
  268 /*
  269  * Look for an ECDT and if we find one, set up default GPE and
  270  * space handlers to catch attempts to access EC space before
  271  * we have a real driver instance in place.
  272  *
  273  * TODO: Some old Gateway laptops need us to fake up an ECDT or
  274  * otherwise attach early so that _REG methods can run.
  275  */
  276 void
  277 acpi_ec_ecdt_probe(device_t parent)
  278 {
  279     ACPI_TABLE_ECDT *ecdt;
  280     ACPI_STATUS      status;
  281     device_t         child;
  282     ACPI_HANDLE      h;
  283     struct acpi_ec_params *params;
  284 
  285     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  286 
  287     /* Find and validate the ECDT. */
  288     status = AcpiGetTable(ACPI_SIG_ECDT, 1, (ACPI_TABLE_HEADER **)&ecdt);
  289     if (ACPI_FAILURE(status) ||
  290         ecdt->Control.BitWidth != 8 ||
  291         ecdt->Data.BitWidth != 8) {
  292         return;
  293     }
  294 
  295     /* Create the child device with the given unit number. */
  296     child = BUS_ADD_CHILD(parent, 0, "acpi_ec", ecdt->Uid);
  297     if (child == NULL) {
  298         printf("%s: can't add child\n", __func__);
  299         return;
  300     }
  301 
  302     /* Find and save the ACPI handle for this device. */
  303     status = AcpiGetHandle(NULL, ecdt->Id, &h);
  304     if (ACPI_FAILURE(status)) {
  305         device_delete_child(parent, child);
  306         printf("%s: can't get handle\n", __func__);
  307         return;
  308     }
  309     acpi_set_handle(child, h);
  310 
  311     /* Set the data and CSR register addresses. */
  312     bus_set_resource(child, SYS_RES_IOPORT, 0, ecdt->Data.Address,
  313         /*count*/1);
  314     bus_set_resource(child, SYS_RES_IOPORT, 1, ecdt->Control.Address,
  315         /*count*/1);
  316 
  317     /*
  318      * Store values for the probe/attach routines to use.  Store the
  319      * ECDT GPE bit and set the global lock flag according to _GLK.
  320      * Note that it is not perfectly correct to be evaluating a method
  321      * before initializing devices, but in practice this function
  322      * should be safe to call at this point.
  323      */
  324     params = malloc(sizeof(struct acpi_ec_params), M_TEMP, M_WAITOK | M_ZERO);
  325     params->gpe_handle = NULL;
  326     params->gpe_bit = ecdt->Gpe;
  327     params->uid = ecdt->Uid;
  328     acpi_GetInteger(h, "_GLK", &params->glk);
  329     acpi_set_private(child, params);
  330 
  331     /* Finish the attach process. */
  332     if (device_probe_and_attach(child) != 0)
  333         device_delete_child(parent, child);
  334 }
  335 
  336 static int
  337 acpi_ec_probe(device_t dev)
  338 {
  339     ACPI_BUFFER buf;
  340     ACPI_HANDLE h;
  341     ACPI_OBJECT *obj;
  342     ACPI_STATUS status;
  343     device_t    peer;
  344     char        desc[64];
  345     int         ecdt;
  346     int         ret;
  347     struct acpi_ec_params *params;
  348     static char *ec_ids[] = { "PNP0C09", NULL };
  349 
  350     /* Check that this is a device and that EC is not disabled. */
  351     if (acpi_get_type(dev) != ACPI_TYPE_DEVICE || acpi_disabled("ec"))
  352         return (ENXIO);
  353 
  354     /*
  355      * If probed via ECDT, set description and continue.  Otherwise,
  356      * we can access the namespace and make sure this is not a
  357      * duplicate probe.
  358      */
  359     ret = ENXIO;
  360     ecdt = 0;
  361     buf.Pointer = NULL;
  362     buf.Length = ACPI_ALLOCATE_BUFFER;
  363     params = acpi_get_private(dev);
  364     if (params != NULL) {
  365         ecdt = 1;
  366         ret = 0;
  367     } else if (ACPI_ID_PROBE(device_get_parent(dev), dev, ec_ids)) {
  368         params = malloc(sizeof(struct acpi_ec_params), M_TEMP,
  369                         M_WAITOK | M_ZERO);
  370         h = acpi_get_handle(dev);
  371 
  372         /*
  373          * Read the unit ID to check for duplicate attach and the
  374          * global lock value to see if we should acquire it when
  375          * accessing the EC.
  376          */
  377         status = acpi_GetInteger(h, "_UID", &params->uid);
  378         if (ACPI_FAILURE(status))
  379             params->uid = 0;
  380         status = acpi_GetInteger(h, "_GLK", &params->glk);
  381         if (ACPI_FAILURE(status))
  382             params->glk = 0;
  383 
  384         /*
  385          * Evaluate the _GPE method to find the GPE bit used by the EC to
  386          * signal status (SCI).  If it's a package, it contains a reference
  387          * and GPE bit, similar to _PRW.
  388          */
  389         status = AcpiEvaluateObject(h, "_GPE", NULL, &buf);
  390         if (ACPI_FAILURE(status)) {
  391             device_printf(dev, "can't evaluate _GPE - %s\n",
  392                           AcpiFormatException(status));
  393             goto out;
  394         }
  395         obj = (ACPI_OBJECT *)buf.Pointer;
  396         if (obj == NULL)
  397             goto out;
  398 
  399         switch (obj->Type) {
  400         case ACPI_TYPE_INTEGER:
  401             params->gpe_handle = NULL;
  402             params->gpe_bit = obj->Integer.Value;
  403             break;
  404         case ACPI_TYPE_PACKAGE:
  405             if (!ACPI_PKG_VALID(obj, 2))
  406                 goto out;
  407             params->gpe_handle =
  408                 acpi_GetReference(NULL, &obj->Package.Elements[0]);
  409             if (params->gpe_handle == NULL ||
  410                 acpi_PkgInt32(obj, 1, &params->gpe_bit) != 0)
  411                 goto out;
  412             break;
  413         default:
  414             device_printf(dev, "_GPE has invalid type %d\n", obj->Type);
  415             goto out;
  416         }
  417 
  418         /* Store the values we got from the namespace for attach. */
  419         acpi_set_private(dev, params);
  420 
  421         /*
  422          * Check for a duplicate probe.  This can happen when a probe
  423          * via ECDT succeeded already.  If this is a duplicate, disable
  424          * this device.
  425          */
  426         peer = devclass_get_device(acpi_ec_devclass, params->uid);
  427         if (peer == NULL || !device_is_alive(peer))
  428             ret = 0;
  429         else
  430             device_disable(dev);
  431     }
  432 
  433 out:
  434     if (ret == 0) {
  435         snprintf(desc, sizeof(desc), "Embedded Controller: GPE %#x%s%s",
  436                  params->gpe_bit, (params->glk) ? ", GLK" : "",
  437                  ecdt ? ", ECDT" : "");
  438         device_set_desc_copy(dev, desc);
  439     }
  440 
  441     if (ret > 0 && params)
  442         free(params, M_TEMP);
  443     if (buf.Pointer)
  444         AcpiOsFree(buf.Pointer);
  445     return (ret);
  446 }
  447 
  448 static int
  449 acpi_ec_attach(device_t dev)
  450 {
  451     struct acpi_ec_softc        *sc;
  452     struct acpi_ec_params       *params;
  453     ACPI_STATUS                 Status;
  454 
  455     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  456 
  457     /* Fetch/initialize softc (assumes softc is pre-zeroed). */
  458     sc = device_get_softc(dev);
  459     params = acpi_get_private(dev);
  460     sc->ec_dev = dev;
  461     sc->ec_handle = acpi_get_handle(dev);
  462 
  463     /* Retrieve previously probed values via device ivars. */
  464     sc->ec_glk = params->glk;
  465     sc->ec_gpebit = params->gpe_bit;
  466     sc->ec_gpehandle = params->gpe_handle;
  467     sc->ec_uid = params->uid;
  468     sc->ec_suspending = FALSE;
  469     acpi_set_private(dev, NULL);
  470     free(params, M_TEMP);
  471 
  472     /* Attach bus resources for data and command/status ports. */
  473     sc->ec_data_rid = 0;
  474     sc->ec_data_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
  475                         &sc->ec_data_rid, RF_ACTIVE);
  476     if (sc->ec_data_res == NULL) {
  477         device_printf(dev, "can't allocate data port\n");
  478         goto error;
  479     }
  480     sc->ec_data_tag = rman_get_bustag(sc->ec_data_res);
  481     sc->ec_data_handle = rman_get_bushandle(sc->ec_data_res);
  482 
  483     sc->ec_csr_rid = 1;
  484     sc->ec_csr_res = bus_alloc_resource_any(sc->ec_dev, SYS_RES_IOPORT,
  485                         &sc->ec_csr_rid, RF_ACTIVE);
  486     if (sc->ec_csr_res == NULL) {
  487         device_printf(dev, "can't allocate command/status port\n");
  488         goto error;
  489     }
  490     sc->ec_csr_tag = rman_get_bustag(sc->ec_csr_res);
  491     sc->ec_csr_handle = rman_get_bushandle(sc->ec_csr_res);
  492 
  493     /*
  494      * Install a handler for this EC's GPE bit.  We want edge-triggered
  495      * behavior.
  496      */
  497     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching GPE handler\n"));
  498     Status = AcpiInstallGpeHandler(sc->ec_gpehandle, sc->ec_gpebit,
  499                 ACPI_GPE_EDGE_TRIGGERED, &EcGpeHandler, sc);
  500     if (ACPI_FAILURE(Status)) {
  501         device_printf(dev, "can't install GPE handler for %s - %s\n",
  502                       acpi_name(sc->ec_handle), AcpiFormatException(Status));
  503         goto error;
  504     }
  505 
  506     /*
  507      * Install address space handler
  508      */
  509     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "attaching address space handler\n"));
  510     Status = AcpiInstallAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
  511                 &EcSpaceHandler, &EcSpaceSetup, sc);
  512     if (ACPI_FAILURE(Status)) {
  513         device_printf(dev, "can't install address space handler for %s - %s\n",
  514                       acpi_name(sc->ec_handle), AcpiFormatException(Status));
  515         goto error;
  516     }
  517 
  518     /* Enable runtime GPEs for the handler. */
  519     Status = AcpiSetGpeType(sc->ec_gpehandle, sc->ec_gpebit,
  520                             ACPI_GPE_TYPE_RUNTIME);
  521     if (ACPI_FAILURE(Status)) {
  522         device_printf(dev, "AcpiSetGpeType failed: %s\n",
  523                       AcpiFormatException(Status));
  524         goto error;
  525     }
  526     Status = AcpiEnableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
  527     if (ACPI_FAILURE(Status)) {
  528         device_printf(dev, "AcpiEnableGpe failed: %s\n",
  529                       AcpiFormatException(Status));
  530         goto error;
  531     }
  532 
  533     ACPI_DEBUG_PRINT((ACPI_DB_RESOURCES, "acpi_ec_attach complete\n"));
  534     return (0);
  535 
  536 error:
  537     AcpiRemoveGpeHandler(sc->ec_gpehandle, sc->ec_gpebit, &EcGpeHandler);
  538     AcpiRemoveAddressSpaceHandler(sc->ec_handle, ACPI_ADR_SPACE_EC,
  539         EcSpaceHandler);
  540     if (sc->ec_csr_res)
  541         bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_csr_rid,
  542                              sc->ec_csr_res);
  543     if (sc->ec_data_res)
  544         bus_release_resource(sc->ec_dev, SYS_RES_IOPORT, sc->ec_data_rid,
  545                              sc->ec_data_res);
  546     return (ENXIO);
  547 }
  548 
  549 static int
  550 acpi_ec_suspend(device_t dev)
  551 {
  552     struct acpi_ec_softc        *sc;
  553 
  554     sc = device_get_softc(dev);
  555     sc->ec_suspending = TRUE;
  556     return (0);
  557 }
  558 
  559 static int
  560 acpi_ec_resume(device_t dev)
  561 {
  562     struct acpi_ec_softc        *sc;
  563 
  564     sc = device_get_softc(dev);
  565     sc->ec_suspending = FALSE;
  566     return (0);
  567 }
  568 
  569 static int
  570 acpi_ec_shutdown(device_t dev)
  571 {
  572     struct acpi_ec_softc        *sc;
  573 
  574     /* Disable the GPE so we don't get EC events during shutdown. */
  575     sc = device_get_softc(dev);
  576     AcpiDisableGpe(sc->ec_gpehandle, sc->ec_gpebit, ACPI_NOT_ISR);
  577     return (0);
  578 }
  579 
  580 /* Methods to allow other devices (e.g., smbat) to read/write EC space. */
  581 static int
  582 acpi_ec_read_method(device_t dev, u_int addr, ACPI_INTEGER *val, int width)
  583 {
  584     struct acpi_ec_softc *sc;
  585     ACPI_STATUS status;
  586 
  587     sc = device_get_softc(dev);
  588     status = EcSpaceHandler(ACPI_READ, addr, width * 8, val, sc, NULL);
  589     if (ACPI_FAILURE(status))
  590         return (ENXIO);
  591     return (0);
  592 }
  593 
  594 static int
  595 acpi_ec_write_method(device_t dev, u_int addr, ACPI_INTEGER val, int width)
  596 {
  597     struct acpi_ec_softc *sc;
  598     ACPI_STATUS status;
  599 
  600     sc = device_get_softc(dev);
  601     status = EcSpaceHandler(ACPI_WRITE, addr, width * 8, &val, sc, NULL);
  602     if (ACPI_FAILURE(status))
  603         return (ENXIO);
  604     return (0);
  605 }
  606 
  607 static void
  608 EcGpeQueryHandler(void *Context)
  609 {
  610     struct acpi_ec_softc        *sc = (struct acpi_ec_softc *)Context;
  611     UINT8                       Data;
  612     ACPI_STATUS                 Status;
  613     char                        qxx[5];
  614 
  615     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  616     KASSERT(Context != NULL, ("EcGpeQueryHandler called with NULL"));
  617 
  618     /* Serialize user access with EcSpaceHandler(). */
  619     Status = EcLock(sc);
  620     if (ACPI_FAILURE(Status)) {
  621         device_printf(sc->ec_dev, "GpeQuery lock error: %s\n",
  622             AcpiFormatException(Status));
  623         return;
  624     }
  625 
  626     /*
  627      * Send a query command to the EC to find out which _Qxx call it
  628      * wants to make.  This command clears the SCI bit and also the
  629      * interrupt source since we are edge-triggered.  To prevent the GPE
  630      * that may arise from running the query from causing another query
  631      * to be queued, we clear the pending flag only after running it.
  632      */
  633     Status = EcCommand(sc, EC_COMMAND_QUERY);
  634     sc->ec_sci_pend = FALSE;
  635     if (ACPI_FAILURE(Status)) {
  636         EcUnlock(sc);
  637         device_printf(sc->ec_dev, "GPE query failed: %s\n",
  638             AcpiFormatException(Status));
  639         return;
  640     }
  641     Data = EC_GET_DATA(sc);
  642 
  643     /*
  644      * We have to unlock before running the _Qxx method below since that
  645      * method may attempt to read/write from EC address space, causing
  646      * recursive acquisition of the lock.
  647      */
  648     EcUnlock(sc);
  649 
  650     /* Ignore the value for "no outstanding event". (13.3.5) */
  651     CTR2(KTR_ACPI, "ec query ok,%s running _Q%02X", Data ? "" : " not", Data);
  652     if (Data == 0)
  653         return;
  654 
  655     /* Evaluate _Qxx to respond to the controller. */
  656     snprintf(qxx, sizeof(qxx), "_Q%02X", Data);
  657     AcpiUtStrupr(qxx);
  658     Status = AcpiEvaluateObject(sc->ec_handle, qxx, NULL, NULL);
  659     if (ACPI_FAILURE(Status) && Status != AE_NOT_FOUND) {
  660         device_printf(sc->ec_dev, "evaluation of query method %s failed: %s\n",
  661             qxx, AcpiFormatException(Status));
  662     }
  663 }
  664 
  665 /*
  666  * The GPE handler is called when IBE/OBF or SCI events occur.  We are
  667  * called from an unknown lock context.
  668  */
  669 static uint32_t
  670 EcGpeHandler(void *Context)
  671 {
  672     struct acpi_ec_softc *sc = Context;
  673     ACPI_STATUS                Status;
  674     EC_STATUS                  EcStatus;
  675 
  676     KASSERT(Context != NULL, ("EcGpeHandler called with NULL"));
  677     CTR0(KTR_ACPI, "ec gpe handler start");
  678 
  679     /*
  680      * Notify EcWaitEvent() that the status register is now fresh.  If we
  681      * didn't do this, it wouldn't be possible to distinguish an old IBE
  682      * from a new one, for example when doing a write transaction (writing
  683      * address and then data values.)
  684      */
  685     atomic_add_int(&sc->ec_gencount, 1);
  686     wakeup(&sc->ec_gencount);
  687 
  688     /*
  689      * If the EC_SCI bit of the status register is set, queue a query handler.
  690      * It will run the query and _Qxx method later, under the lock.
  691      */
  692     EcStatus = EC_GET_CSR(sc);
  693     if ((EcStatus & EC_EVENT_SCI) && !sc->ec_sci_pend) {
  694         CTR0(KTR_ACPI, "ec gpe queueing query handler");
  695         Status = AcpiOsExecute(OSL_GPE_HANDLER, EcGpeQueryHandler, Context);
  696         if (ACPI_SUCCESS(Status))
  697             sc->ec_sci_pend = TRUE;
  698         else
  699             printf("EcGpeHandler: queuing GPE query handler failed\n");
  700     }
  701     return (0);
  702 }
  703 
  704 static ACPI_STATUS
  705 EcSpaceSetup(ACPI_HANDLE Region, UINT32 Function, void *Context,
  706              void **RegionContext)
  707 {
  708 
  709     ACPI_FUNCTION_TRACE((char *)(uintptr_t)__func__);
  710 
  711     /*
  712      * If deactivating a region, always set the output to NULL.  Otherwise,
  713      * just pass the context through.
  714      */
  715     if (Function == ACPI_REGION_DEACTIVATE)
  716         *RegionContext = NULL;
  717     else
  718         *RegionContext = Context;
  719 
  720     return_ACPI_STATUS (AE_OK);
  721 }
  722 
  723 static ACPI_STATUS
  724 EcSpaceHandler(UINT32 Function, ACPI_PHYSICAL_ADDRESS Address, UINT32 width,
  725                ACPI_INTEGER *Value, void *Context, void *RegionContext)
  726 {
  727     struct acpi_ec_softc        *sc = (struct acpi_ec_softc *)Context;
  728     ACPI_STATUS                 Status;
  729     UINT8                       EcAddr, EcData;
  730     int                         i;
  731 
  732     ACPI_FUNCTION_TRACE_U32((char *)(uintptr_t)__func__, (UINT32)Address);
  733 
  734     if (width % 8 != 0 || Value == NULL || Context == NULL)
  735         return_ACPI_STATUS (AE_BAD_PARAMETER);
  736     if (Address + (width / 8) - 1 > 0xFF)
  737         return_ACPI_STATUS (AE_BAD_ADDRESS);
  738 
  739     if (Function == ACPI_READ)
  740         *Value = 0;
  741     EcAddr = Address;
  742     Status = AE_ERROR;
  743 
  744     /*
  745      * If booting, check if we need to run the query handler.  If so, we
  746      * we call it directly here since our thread taskq is not active yet.
  747      */
  748     if (cold || rebooting) {
  749         if ((EC_GET_CSR(sc) & EC_EVENT_SCI)) {
  750             CTR0(KTR_ACPI, "ec running gpe handler directly");
  751             EcGpeQueryHandler(sc);
  752         }
  753     }
  754 
  755     /* Serialize with EcGpeQueryHandler() at transaction granularity. */
  756     Status = EcLock(sc);
  757     if (ACPI_FAILURE(Status))
  758         return_ACPI_STATUS (Status);
  759 
  760     /* Perform the transaction(s), based on width. */
  761     for (i = 0; i < width; i += 8, EcAddr++) {
  762         switch (Function) {
  763         case ACPI_READ:
  764             Status = EcRead(sc, EcAddr, &EcData);
  765             if (ACPI_SUCCESS(Status))
  766                 *Value |= ((ACPI_INTEGER)EcData) << i;
  767             break;
  768         case ACPI_WRITE:
  769             EcData = (UINT8)((*Value) >> i);
  770             Status = EcWrite(sc, EcAddr, &EcData);
  771             break;
  772         default:
  773             device_printf(sc->ec_dev, "invalid EcSpaceHandler function %d\n",
  774                           Function);
  775             Status = AE_BAD_PARAMETER;
  776             break;
  777         }
  778         if (ACPI_FAILURE(Status))
  779             break;
  780     }
  781 
  782     EcUnlock(sc);
  783     return_ACPI_STATUS (Status);
  784 }
  785 
  786 static ACPI_STATUS
  787 EcCheckStatus(struct acpi_ec_softc *sc, const char *msg, EC_EVENT event)
  788 {
  789     ACPI_STATUS status;
  790     EC_STATUS ec_status;
  791 
  792     status = AE_NO_HARDWARE_RESPONSE;
  793     ec_status = EC_GET_CSR(sc);
  794     if (sc->ec_burstactive && !(ec_status & EC_FLAG_BURST_MODE)) {
  795         CTR1(KTR_ACPI, "ec burst disabled in waitevent (%s)", msg);
  796         sc->ec_burstactive = FALSE;
  797     }
  798     if (EVENT_READY(event, ec_status)) {
  799         CTR2(KTR_ACPI, "ec %s wait ready, status %#x", msg, ec_status);
  800         status = AE_OK;
  801     }
  802     return (status);
  803 }
  804 
  805 static ACPI_STATUS
  806 EcWaitEvent(struct acpi_ec_softc *sc, EC_EVENT Event, u_int gen_count)
  807 {
  808     ACPI_STATUS Status;
  809     int         count, i, slp_ival;
  810 
  811     ACPI_SERIAL_ASSERT(ec);
  812     Status = AE_NO_HARDWARE_RESPONSE;
  813     int need_poll = cold || rebooting || ec_polled_mode || sc->ec_suspending;
  814     /*
  815      * The main CPU should be much faster than the EC.  So the status should
  816      * be "not ready" when we start waiting.  But if the main CPU is really
  817      * slow, it's possible we see the current "ready" response.  Since that
  818      * can't be distinguished from the previous response in polled mode,
  819      * this is a potential issue.  We really should have interrupts enabled
  820      * during boot so there is no ambiguity in polled mode.
  821      *
  822      * If this occurs, we add an additional delay before actually entering
  823      * the status checking loop, hopefully to allow the EC to go to work
  824      * and produce a non-stale status.
  825      */
  826     if (need_poll) {
  827         static int      once;
  828 
  829         if (EcCheckStatus(sc, "pre-check", Event) == AE_OK) {
  830             if (!once) {
  831                 device_printf(sc->ec_dev,
  832                     "warning: EC done before starting event wait\n");
  833                 once = 1;
  834             }
  835             AcpiOsStall(10);
  836         }
  837     }
  838 
  839     /* Wait for event by polling or GPE (interrupt). */
  840     if (need_poll) {
  841         count = (ec_timeout * 1000) / EC_POLL_DELAY;
  842         if (count == 0)
  843             count = 1;
  844         for (i = 0; i < count; i++) {
  845             Status = EcCheckStatus(sc, "poll", Event);
  846             if (Status == AE_OK)
  847                 break;
  848             AcpiOsStall(EC_POLL_DELAY);
  849         }
  850     } else {
  851         slp_ival = hz / 1000;
  852         if (slp_ival != 0) {
  853             count = ec_timeout;
  854         } else {
  855             /* hz has less than 1 ms resolution so scale timeout. */
  856             slp_ival = 1;
  857             count = ec_timeout / (1000 / hz);
  858         }
  859 
  860         /*
  861          * Wait for the GPE to signal the status changed, checking the
  862          * status register each time we get one.  It's possible to get a
  863          * GPE for an event we're not interested in here (i.e., SCI for
  864          * EC query).
  865          */
  866         for (i = 0; i < count; i++) {
  867             if (gen_count != sc->ec_gencount) {
  868                 /*
  869                  * Record new generation count.  It's possible the GPE was
  870                  * just to notify us that a query is needed and we need to
  871                  * wait for a second GPE to signal the completion of the
  872                  * event we are actually waiting for.
  873                  */
  874                 gen_count = sc->ec_gencount;
  875                 Status = EcCheckStatus(sc, "sleep", Event);
  876                 if (Status == AE_OK)
  877                     break;
  878             }
  879             tsleep(&sc->ec_gencount, PZERO, "ecgpe", slp_ival);
  880         }
  881 
  882         /*
  883          * We finished waiting for the GPE and it never arrived.  Try to
  884          * read the register once and trust whatever value we got.  This is
  885          * the best we can do at this point.  Then, force polled mode on
  886          * since this system doesn't appear to generate GPEs.
  887          */
  888         if (Status != AE_OK) {
  889             Status = EcCheckStatus(sc, "sleep_end", Event);
  890             device_printf(sc->ec_dev,
  891                 "wait timed out (%sresponse), forcing polled mode\n",
  892                 Status == AE_OK ? "" : "no ");
  893             ec_polled_mode = TRUE;
  894         }
  895     }
  896     if (Status != AE_OK)
  897             CTR0(KTR_ACPI, "error: ec wait timed out");
  898     return (Status);
  899 }
  900 
  901 static ACPI_STATUS
  902 EcCommand(struct acpi_ec_softc *sc, EC_COMMAND cmd)
  903 {
  904     ACPI_STATUS status;
  905     EC_EVENT    event;
  906     EC_STATUS   ec_status;
  907     u_int       gen_count;
  908 
  909     ACPI_SERIAL_ASSERT(ec);
  910 
  911     /* Don't use burst mode if user disabled it. */
  912     if (!ec_burst_mode && cmd == EC_COMMAND_BURST_ENABLE)
  913         return (AE_ERROR);
  914 
  915     /* Decide what to wait for based on command type. */
  916     switch (cmd) {
  917     case EC_COMMAND_READ:
  918     case EC_COMMAND_WRITE:
  919     case EC_COMMAND_BURST_DISABLE:
  920         event = EC_EVENT_INPUT_BUFFER_EMPTY;
  921         break;
  922     case EC_COMMAND_QUERY:
  923     case EC_COMMAND_BURST_ENABLE:
  924         event = EC_EVENT_OUTPUT_BUFFER_FULL;
  925         break;
  926     default:
  927         device_printf(sc->ec_dev, "EcCommand: invalid command %#x\n", cmd);
  928         return (AE_BAD_PARAMETER);
  929     }
  930 
  931     /* Run the command and wait for the chosen event. */
  932     CTR1(KTR_ACPI, "ec running command %#x", cmd);
  933     gen_count = sc->ec_gencount;
  934     EC_SET_CSR(sc, cmd);
  935     status = EcWaitEvent(sc, event, gen_count);
  936     if (ACPI_SUCCESS(status)) {
  937         /* If we succeeded, burst flag should now be present. */
  938         if (cmd == EC_COMMAND_BURST_ENABLE) {
  939             ec_status = EC_GET_CSR(sc);
  940             if ((ec_status & EC_FLAG_BURST_MODE) == 0)
  941                 status = AE_ERROR;
  942         }
  943     } else
  944         device_printf(sc->ec_dev, "EcCommand: no response to %#x\n", cmd);
  945     return (status);
  946 }
  947 
  948 static ACPI_STATUS
  949 EcRead(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
  950 {
  951     ACPI_STATUS status;
  952     UINT8 data;
  953     u_int gen_count;
  954 
  955     ACPI_SERIAL_ASSERT(ec);
  956     CTR1(KTR_ACPI, "ec read from %#x", Address);
  957 
  958     /* If we can't start burst mode, continue anyway. */
  959     status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
  960     if (status == AE_OK) {
  961         data = EC_GET_DATA(sc);
  962         if (data == EC_BURST_ACK) {
  963             CTR0(KTR_ACPI, "ec burst enabled");
  964             sc->ec_burstactive = TRUE;
  965         }
  966     }
  967 
  968     status = EcCommand(sc, EC_COMMAND_READ);
  969     if (ACPI_FAILURE(status))
  970         return (status);
  971 
  972     gen_count = sc->ec_gencount;
  973     EC_SET_DATA(sc, Address);
  974     status = EcWaitEvent(sc, EC_EVENT_OUTPUT_BUFFER_FULL, gen_count);
  975     if (ACPI_FAILURE(status)) {
  976         device_printf(sc->ec_dev, "EcRead: failed waiting to get data\n");
  977         return (status);
  978     }
  979     *Data = EC_GET_DATA(sc);
  980 
  981     if (sc->ec_burstactive) {
  982         sc->ec_burstactive = FALSE;
  983         status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
  984         if (ACPI_FAILURE(status))
  985             return (status);
  986         CTR0(KTR_ACPI, "ec disabled burst ok");
  987     }
  988 
  989     return (AE_OK);
  990 }
  991 
  992 static ACPI_STATUS
  993 EcWrite(struct acpi_ec_softc *sc, UINT8 Address, UINT8 *Data)
  994 {
  995     ACPI_STATUS status;
  996     UINT8 data;
  997     u_int gen_count;
  998 
  999     ACPI_SERIAL_ASSERT(ec);
 1000     CTR2(KTR_ACPI, "ec write to %#x, data %#x", Address, *Data);
 1001 
 1002     /* If we can't start burst mode, continue anyway. */
 1003     status = EcCommand(sc, EC_COMMAND_BURST_ENABLE);
 1004     if (status == AE_OK) {
 1005         data = EC_GET_DATA(sc);
 1006         if (data == EC_BURST_ACK) {
 1007             CTR0(KTR_ACPI, "ec burst enabled");
 1008             sc->ec_burstactive = TRUE;
 1009         }
 1010     }
 1011 
 1012     status = EcCommand(sc, EC_COMMAND_WRITE);
 1013     if (ACPI_FAILURE(status))
 1014         return (status);
 1015 
 1016     gen_count = sc->ec_gencount;
 1017     EC_SET_DATA(sc, Address);
 1018     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
 1019     if (ACPI_FAILURE(status)) {
 1020         device_printf(sc->ec_dev, "EcRead: failed waiting for sent address\n");
 1021         return (status);
 1022     }
 1023 
 1024     gen_count = sc->ec_gencount;
 1025     EC_SET_DATA(sc, *Data);
 1026     status = EcWaitEvent(sc, EC_EVENT_INPUT_BUFFER_EMPTY, gen_count);
 1027     if (ACPI_FAILURE(status)) {
 1028         device_printf(sc->ec_dev, "EcWrite: failed waiting for sent data\n");
 1029         return (status);
 1030     }
 1031 
 1032     if (sc->ec_burstactive) {
 1033         sc->ec_burstactive = FALSE;
 1034         status = EcCommand(sc, EC_COMMAND_BURST_DISABLE);
 1035         if (ACPI_FAILURE(status))
 1036             return (status);
 1037         CTR0(KTR_ACPI, "ec disabled burst ok");
 1038     }
 1039 
 1040     return (AE_OK);
 1041 }

Cache object: edaa1dcf9436fd02aa598f8c1df9fb36


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.