Home | History | Annotate | Line # | Download | only in events
evregion.c revision 1.1.1.5
      1 /******************************************************************************
      2  *
      3  * Module Name: evregion - Operation Region support
      4  *
      5  *****************************************************************************/
      6 
      7 /*
      8  * Copyright (C) 2000 - 2014, Intel Corp.
      9  * All rights reserved.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions, and the following disclaimer,
     16  *    without modification.
     17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  *    including a substantially similar Disclaimer requirement for further
     21  *    binary redistribution.
     22  * 3. Neither the names of the above-listed copyright holders nor the names
     23  *    of any contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * Alternatively, this software may be distributed under the terms of the
     27  * GNU General Public License ("GPL") version 2 as published by the Free
     28  * Software Foundation.
     29  *
     30  * NO WARRANTY
     31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  * POSSIBILITY OF SUCH DAMAGES.
     42  */
     43 
     44 #define __EVREGION_C__
     45 
     46 #include "acpi.h"
     47 #include "accommon.h"
     48 #include "acevents.h"
     49 #include "acnamesp.h"
     50 #include "acinterp.h"
     51 
     52 #define _COMPONENT          ACPI_EVENTS
     53         ACPI_MODULE_NAME    ("evregion")
     54 
     55 
     56 extern UINT8        AcpiGbl_DefaultAddressSpaces[];
     57 
     58 /* Local prototypes */
     59 
     60 static void
     61 AcpiEvOrphanEcRegMethod (
     62     ACPI_NAMESPACE_NODE     *EcDeviceNode);
     63 
     64 static ACPI_STATUS
     65 AcpiEvRegRun (
     66     ACPI_HANDLE             ObjHandle,
     67     UINT32                  Level,
     68     void                    *Context,
     69     void                    **ReturnValue);
     70 
     71 
     72 /*******************************************************************************
     73  *
     74  * FUNCTION:    AcpiEvInitializeOpRegions
     75  *
     76  * PARAMETERS:  None
     77  *
     78  * RETURN:      Status
     79  *
     80  * DESCRIPTION: Execute _REG methods for all Operation Regions that have
     81  *              an installed default region handler.
     82  *
     83  ******************************************************************************/
     84 
     85 ACPI_STATUS
     86 AcpiEvInitializeOpRegions (
     87     void)
     88 {
     89     ACPI_STATUS             Status;
     90     UINT32                  i;
     91 
     92 
     93     ACPI_FUNCTION_TRACE (EvInitializeOpRegions);
     94 
     95 
     96     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
     97     if (ACPI_FAILURE (Status))
     98     {
     99         return_ACPI_STATUS (Status);
    100     }
    101 
    102     /* Run the _REG methods for OpRegions in each default address space */
    103 
    104     for (i = 0; i < ACPI_NUM_DEFAULT_SPACES; i++)
    105     {
    106         /*
    107          * Make sure the installed handler is the DEFAULT handler. If not the
    108          * default, the _REG methods will have already been run (when the
    109          * handler was installed)
    110          */
    111         if (AcpiEvHasDefaultHandler (AcpiGbl_RootNode,
    112                AcpiGbl_DefaultAddressSpaces[i]))
    113         {
    114             Status = AcpiEvExecuteRegMethods (AcpiGbl_RootNode,
    115                         AcpiGbl_DefaultAddressSpaces[i]);
    116         }
    117     }
    118 
    119     AcpiGbl_RegMethodsExecuted = TRUE;
    120 
    121     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
    122     return_ACPI_STATUS (Status);
    123 }
    124 
    125 
    126 /*******************************************************************************
    127  *
    128  * FUNCTION:    AcpiEvAddressSpaceDispatch
    129  *
    130  * PARAMETERS:  RegionObj           - Internal region object
    131  *              FieldObj            - Corresponding field. Can be NULL.
    132  *              Function            - Read or Write operation
    133  *              RegionOffset        - Where in the region to read or write
    134  *              BitWidth            - Field width in bits (8, 16, 32, or 64)
    135  *              Value               - Pointer to in or out value, must be
    136  *                                    a full 64-bit integer
    137  *
    138  * RETURN:      Status
    139  *
    140  * DESCRIPTION: Dispatch an address space or operation region access to
    141  *              a previously installed handler.
    142  *
    143  ******************************************************************************/
    144 
    145 ACPI_STATUS
    146 AcpiEvAddressSpaceDispatch (
    147     ACPI_OPERAND_OBJECT     *RegionObj,
    148     ACPI_OPERAND_OBJECT     *FieldObj,
    149     UINT32                  Function,
    150     UINT32                  RegionOffset,
    151     UINT32                  BitWidth,
    152     UINT64                  *Value)
    153 {
    154     ACPI_STATUS             Status;
    155     ACPI_ADR_SPACE_HANDLER  Handler;
    156     ACPI_ADR_SPACE_SETUP    RegionSetup;
    157     ACPI_OPERAND_OBJECT     *HandlerDesc;
    158     ACPI_OPERAND_OBJECT     *RegionObj2;
    159     void                    *RegionContext = NULL;
    160     ACPI_CONNECTION_INFO    *Context;
    161     ACPI_PHYSICAL_ADDRESS   Address;
    162 
    163 
    164     ACPI_FUNCTION_TRACE (EvAddressSpaceDispatch);
    165 
    166 
    167     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
    168     if (!RegionObj2)
    169     {
    170         return_ACPI_STATUS (AE_NOT_EXIST);
    171     }
    172 
    173     /* Ensure that there is a handler associated with this region */
    174 
    175     HandlerDesc = RegionObj->Region.Handler;
    176     if (!HandlerDesc)
    177     {
    178         ACPI_ERROR ((AE_INFO,
    179             "No handler for Region [%4.4s] (%p) [%s]",
    180             AcpiUtGetNodeName (RegionObj->Region.Node),
    181             RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    182 
    183         return_ACPI_STATUS (AE_NOT_EXIST);
    184     }
    185 
    186     Context = HandlerDesc->AddressSpace.Context;
    187 
    188     /*
    189      * It may be the case that the region has never been initialized.
    190      * Some types of regions require special init code
    191      */
    192     if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
    193     {
    194         /* This region has not been initialized yet, do it */
    195 
    196         RegionSetup = HandlerDesc->AddressSpace.Setup;
    197         if (!RegionSetup)
    198         {
    199             /* No initialization routine, exit with error */
    200 
    201             ACPI_ERROR ((AE_INFO,
    202                 "No init routine for region(%p) [%s]",
    203                 RegionObj, AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    204             return_ACPI_STATUS (AE_NOT_EXIST);
    205         }
    206 
    207         /*
    208          * We must exit the interpreter because the region setup will
    209          * potentially execute control methods (for example, the _REG method
    210          * for this region)
    211          */
    212         AcpiExExitInterpreter ();
    213 
    214         Status = RegionSetup (RegionObj, ACPI_REGION_ACTIVATE,
    215                     Context, &RegionContext);
    216 
    217         /* Re-enter the interpreter */
    218 
    219         AcpiExEnterInterpreter ();
    220 
    221         /* Check for failure of the Region Setup */
    222 
    223         if (ACPI_FAILURE (Status))
    224         {
    225             ACPI_EXCEPTION ((AE_INFO, Status,
    226                 "During region initialization: [%s]",
    227                 AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    228             return_ACPI_STATUS (Status);
    229         }
    230 
    231         /* Region initialization may have been completed by RegionSetup */
    232 
    233         if (!(RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE))
    234         {
    235             RegionObj->Region.Flags |= AOPOBJ_SETUP_COMPLETE;
    236 
    237             /*
    238              * Save the returned context for use in all accesses to
    239              * the handler for this particular region
    240              */
    241             if (!(RegionObj2->Extra.RegionContext))
    242             {
    243                 RegionObj2->Extra.RegionContext = RegionContext;
    244             }
    245         }
    246     }
    247 
    248     /* We have everything we need, we can invoke the address space handler */
    249 
    250     Handler = HandlerDesc->AddressSpace.Handler;
    251     Address = (RegionObj->Region.Address + RegionOffset);
    252 
    253     /*
    254      * Special handling for GenericSerialBus and GeneralPurposeIo:
    255      * There are three extra parameters that must be passed to the
    256      * handler via the context:
    257      *   1) Connection buffer, a resource template from Connection() op
    258      *   2) Length of the above buffer
    259      *   3) Actual access length from the AccessAs() op
    260      *
    261      * In addition, for GeneralPurposeIo, the Address and BitWidth fields
    262      * are defined as follows:
    263      *   1) Address is the pin number index of the field (bit offset from
    264      *      the previous Connection)
    265      *   2) BitWidth is the actual bit length of the field (number of pins)
    266      */
    267     if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS) &&
    268         Context &&
    269         FieldObj)
    270     {
    271         /* Get the Connection (ResourceTemplate) buffer */
    272 
    273         Context->Connection = FieldObj->Field.ResourceBuffer;
    274         Context->Length = FieldObj->Field.ResourceLength;
    275         Context->AccessLength = FieldObj->Field.AccessLength;
    276     }
    277     if ((RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO) &&
    278         Context &&
    279         FieldObj)
    280     {
    281         /* Get the Connection (ResourceTemplate) buffer */
    282 
    283         Context->Connection = FieldObj->Field.ResourceBuffer;
    284         Context->Length = FieldObj->Field.ResourceLength;
    285         Context->AccessLength = FieldObj->Field.AccessLength;
    286         Address = FieldObj->Field.PinNumberIndex;
    287         BitWidth = FieldObj->Field.BitLength;
    288     }
    289 
    290     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
    291         "Handler %p (@%p) Address %8.8X%8.8X [%s]\n",
    292         &RegionObj->Region.Handler->AddressSpace, Handler,
    293         ACPI_FORMAT_NATIVE_UINT (Address),
    294         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    295 
    296     if (!(HandlerDesc->AddressSpace.HandlerFlags &
    297             ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
    298     {
    299         /*
    300          * For handlers other than the default (supplied) handlers, we must
    301          * exit the interpreter because the handler *might* block -- we don't
    302          * know what it will do, so we can't hold the lock on the intepreter.
    303          */
    304         AcpiExExitInterpreter();
    305     }
    306 
    307     /* Call the handler */
    308 
    309     Status = Handler (Function, Address, BitWidth, Value, Context,
    310         RegionObj2->Extra.RegionContext);
    311 
    312     if (ACPI_FAILURE (Status))
    313     {
    314         ACPI_EXCEPTION ((AE_INFO, Status, "Returned by Handler for [%s]",
    315             AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    316     }
    317 
    318     if (!(HandlerDesc->AddressSpace.HandlerFlags &
    319             ACPI_ADDR_HANDLER_DEFAULT_INSTALLED))
    320     {
    321         /*
    322          * We just returned from a non-default handler, we must re-enter the
    323          * interpreter
    324          */
    325        AcpiExEnterInterpreter ();
    326     }
    327 
    328     return_ACPI_STATUS (Status);
    329 }
    330 
    331 
    332 /*******************************************************************************
    333  *
    334  * FUNCTION:    AcpiEvDetachRegion
    335  *
    336  * PARAMETERS:  RegionObj           - Region Object
    337  *              AcpiNsIsLocked      - Namespace Region Already Locked?
    338  *
    339  * RETURN:      None
    340  *
    341  * DESCRIPTION: Break the association between the handler and the region
    342  *              this is a two way association.
    343  *
    344  ******************************************************************************/
    345 
    346 void
    347 AcpiEvDetachRegion(
    348     ACPI_OPERAND_OBJECT     *RegionObj,
    349     BOOLEAN                 AcpiNsIsLocked)
    350 {
    351     ACPI_OPERAND_OBJECT     *HandlerObj;
    352     ACPI_OPERAND_OBJECT     *ObjDesc;
    353     ACPI_OPERAND_OBJECT     *StartDesc;
    354     ACPI_OPERAND_OBJECT     **LastObjPtr;
    355     ACPI_ADR_SPACE_SETUP    RegionSetup;
    356     void                    **RegionContext;
    357     ACPI_OPERAND_OBJECT     *RegionObj2;
    358     ACPI_STATUS             Status;
    359 
    360 
    361     ACPI_FUNCTION_TRACE (EvDetachRegion);
    362 
    363 
    364     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
    365     if (!RegionObj2)
    366     {
    367         return_VOID;
    368     }
    369     RegionContext = &RegionObj2->Extra.RegionContext;
    370 
    371     /* Get the address handler from the region object */
    372 
    373     HandlerObj = RegionObj->Region.Handler;
    374     if (!HandlerObj)
    375     {
    376         /* This region has no handler, all done */
    377 
    378         return_VOID;
    379     }
    380 
    381     /* Find this region in the handler's list */
    382 
    383     ObjDesc = HandlerObj->AddressSpace.RegionList;
    384     StartDesc = ObjDesc;
    385     LastObjPtr = &HandlerObj->AddressSpace.RegionList;
    386 
    387     while (ObjDesc)
    388     {
    389         /* Is this the correct Region? */
    390 
    391         if (ObjDesc == RegionObj)
    392         {
    393             ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
    394                 "Removing Region %p from address handler %p\n",
    395                 RegionObj, HandlerObj));
    396 
    397             /* This is it, remove it from the handler's list */
    398 
    399             *LastObjPtr = ObjDesc->Region.Next;
    400             ObjDesc->Region.Next = NULL;        /* Must clear field */
    401 
    402             if (AcpiNsIsLocked)
    403             {
    404                 Status = AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
    405                 if (ACPI_FAILURE (Status))
    406                 {
    407                     return_VOID;
    408                 }
    409             }
    410 
    411             /* Now stop region accesses by executing the _REG method */
    412 
    413             Status = AcpiEvExecuteRegMethod (RegionObj, ACPI_REG_DISCONNECT);
    414             if (ACPI_FAILURE (Status))
    415             {
    416                 ACPI_EXCEPTION ((AE_INFO, Status, "from region _REG, [%s]",
    417                     AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    418             }
    419 
    420             if (AcpiNsIsLocked)
    421             {
    422                 Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
    423                 if (ACPI_FAILURE (Status))
    424                 {
    425                     return_VOID;
    426                 }
    427             }
    428 
    429             /*
    430              * If the region has been activated, call the setup handler with
    431              * the deactivate notification
    432              */
    433             if (RegionObj->Region.Flags & AOPOBJ_SETUP_COMPLETE)
    434             {
    435                 RegionSetup = HandlerObj->AddressSpace.Setup;
    436                 Status = RegionSetup (RegionObj, ACPI_REGION_DEACTIVATE,
    437                     HandlerObj->AddressSpace.Context, RegionContext);
    438 
    439                 /*
    440                  * RegionContext should have been released by the deactivate
    441                  * operation. We don't need access to it anymore here.
    442                  */
    443                 if (RegionContext)
    444                 {
    445                     *RegionContext = NULL;
    446                 }
    447 
    448                 /* Init routine may fail, Just ignore errors */
    449 
    450                 if (ACPI_FAILURE (Status))
    451                 {
    452                     ACPI_EXCEPTION ((AE_INFO, Status,
    453                         "from region handler - deactivate, [%s]",
    454                         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    455                 }
    456 
    457                 RegionObj->Region.Flags &= ~(AOPOBJ_SETUP_COMPLETE);
    458             }
    459 
    460             /*
    461              * Remove handler reference in the region
    462              *
    463              * NOTE: this doesn't mean that the region goes away, the region
    464              * is just inaccessible as indicated to the _REG method
    465              *
    466              * If the region is on the handler's list, this must be the
    467              * region's handler
    468              */
    469             RegionObj->Region.Handler = NULL;
    470             AcpiUtRemoveReference (HandlerObj);
    471 
    472             return_VOID;
    473         }
    474 
    475         /* Walk the linked list of handlers */
    476 
    477         LastObjPtr = &ObjDesc->Region.Next;
    478         ObjDesc = ObjDesc->Region.Next;
    479 
    480         /* Prevent infinite loop if list is corrupted */
    481 
    482         if (ObjDesc == StartDesc)
    483         {
    484             ACPI_ERROR ((AE_INFO,
    485                 "Circular handler list in region object %p",
    486                 RegionObj));
    487             return_VOID;
    488         }
    489     }
    490 
    491     /* If we get here, the region was not in the handler's region list */
    492 
    493     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
    494         "Cannot remove region %p from address handler %p\n",
    495         RegionObj, HandlerObj));
    496 
    497     return_VOID;
    498 }
    499 
    500 
    501 /*******************************************************************************
    502  *
    503  * FUNCTION:    AcpiEvAttachRegion
    504  *
    505  * PARAMETERS:  HandlerObj          - Handler Object
    506  *              RegionObj           - Region Object
    507  *              AcpiNsIsLocked      - Namespace Region Already Locked?
    508  *
    509  * RETURN:      None
    510  *
    511  * DESCRIPTION: Create the association between the handler and the region
    512  *              this is a two way association.
    513  *
    514  ******************************************************************************/
    515 
    516 ACPI_STATUS
    517 AcpiEvAttachRegion (
    518     ACPI_OPERAND_OBJECT     *HandlerObj,
    519     ACPI_OPERAND_OBJECT     *RegionObj,
    520     BOOLEAN                 AcpiNsIsLocked)
    521 {
    522 
    523     ACPI_FUNCTION_TRACE (EvAttachRegion);
    524 
    525 
    526     ACPI_DEBUG_PRINT ((ACPI_DB_OPREGION,
    527         "Adding Region [%4.4s] %p to address handler %p [%s]\n",
    528         AcpiUtGetNodeName (RegionObj->Region.Node),
    529         RegionObj, HandlerObj,
    530         AcpiUtGetRegionName (RegionObj->Region.SpaceId)));
    531 
    532     /* Link this region to the front of the handler's list */
    533 
    534     RegionObj->Region.Next = HandlerObj->AddressSpace.RegionList;
    535     HandlerObj->AddressSpace.RegionList = RegionObj;
    536 
    537     /* Install the region's handler */
    538 
    539     if (RegionObj->Region.Handler)
    540     {
    541         return_ACPI_STATUS (AE_ALREADY_EXISTS);
    542     }
    543 
    544     RegionObj->Region.Handler = HandlerObj;
    545     AcpiUtAddReference (HandlerObj);
    546 
    547     return_ACPI_STATUS (AE_OK);
    548 }
    549 
    550 
    551 /*******************************************************************************
    552  *
    553  * FUNCTION:    AcpiEvExecuteRegMethod
    554  *
    555  * PARAMETERS:  RegionObj           - Region object
    556  *              Function            - Passed to _REG: On (1) or Off (0)
    557  *
    558  * RETURN:      Status
    559  *
    560  * DESCRIPTION: Execute _REG method for a region
    561  *
    562  ******************************************************************************/
    563 
    564 ACPI_STATUS
    565 AcpiEvExecuteRegMethod (
    566     ACPI_OPERAND_OBJECT     *RegionObj,
    567     UINT32                  Function)
    568 {
    569     ACPI_EVALUATE_INFO      *Info;
    570     ACPI_OPERAND_OBJECT     *Args[3];
    571     ACPI_OPERAND_OBJECT     *RegionObj2;
    572     ACPI_STATUS             Status;
    573 
    574 
    575     ACPI_FUNCTION_TRACE (EvExecuteRegMethod);
    576 
    577 
    578     RegionObj2 = AcpiNsGetSecondaryObject (RegionObj);
    579     if (!RegionObj2)
    580     {
    581         return_ACPI_STATUS (AE_NOT_EXIST);
    582     }
    583 
    584     if (RegionObj2->Extra.Method_REG == NULL)
    585     {
    586         return_ACPI_STATUS (AE_OK);
    587     }
    588 
    589     /* Allocate and initialize the evaluation information block */
    590 
    591     Info = ACPI_ALLOCATE_ZEROED (sizeof (ACPI_EVALUATE_INFO));
    592     if (!Info)
    593     {
    594         return_ACPI_STATUS (AE_NO_MEMORY);
    595     }
    596 
    597     Info->PrefixNode = RegionObj2->Extra.Method_REG;
    598     Info->RelativePathname = NULL;
    599     Info->Parameters = Args;
    600     Info->Flags = ACPI_IGNORE_RETURN_VALUE;
    601 
    602     /*
    603      * The _REG method has two arguments:
    604      *
    605      * Arg0 - Integer:
    606      *  Operation region space ID Same value as RegionObj->Region.SpaceId
    607      *
    608      * Arg1 - Integer:
    609      *  connection status 1 for connecting the handler, 0 for disconnecting
    610      *  the handler (Passed as a parameter)
    611      */
    612     Args[0] = AcpiUtCreateIntegerObject ((UINT64) RegionObj->Region.SpaceId);
    613     if (!Args[0])
    614     {
    615         Status = AE_NO_MEMORY;
    616         goto Cleanup1;
    617     }
    618 
    619     Args[1] = AcpiUtCreateIntegerObject ((UINT64) Function);
    620     if (!Args[1])
    621     {
    622         Status = AE_NO_MEMORY;
    623         goto Cleanup2;
    624     }
    625 
    626     Args[2] = NULL; /* Terminate list */
    627 
    628     /* Execute the method, no return value */
    629 
    630     ACPI_DEBUG_EXEC (
    631         AcpiUtDisplayInitPathname (ACPI_TYPE_METHOD, Info->PrefixNode, NULL));
    632 
    633     Status = AcpiNsEvaluate (Info);
    634     AcpiUtRemoveReference (Args[1]);
    635 
    636 Cleanup2:
    637     AcpiUtRemoveReference (Args[0]);
    638 
    639 Cleanup1:
    640     ACPI_FREE (Info);
    641     return_ACPI_STATUS (Status);
    642 }
    643 
    644 
    645 /*******************************************************************************
    646  *
    647  * FUNCTION:    AcpiEvExecuteRegMethods
    648  *
    649  * PARAMETERS:  Node            - Namespace node for the device
    650  *              SpaceId         - The address space ID
    651  *
    652  * RETURN:      Status
    653  *
    654  * DESCRIPTION: Run all _REG methods for the input Space ID;
    655  *              Note: assumes namespace is locked, or system init time.
    656  *
    657  ******************************************************************************/
    658 
    659 ACPI_STATUS
    660 AcpiEvExecuteRegMethods (
    661     ACPI_NAMESPACE_NODE     *Node,
    662     ACPI_ADR_SPACE_TYPE     SpaceId)
    663 {
    664     ACPI_STATUS             Status;
    665 
    666 
    667     ACPI_FUNCTION_TRACE (EvExecuteRegMethods);
    668 
    669 
    670     /*
    671      * Run all _REG methods for all Operation Regions for this space ID. This
    672      * is a separate walk in order to handle any interdependencies between
    673      * regions and _REG methods. (i.e. handlers must be installed for all
    674      * regions of this Space ID before we can run any _REG methods)
    675      */
    676     Status = AcpiNsWalkNamespace (ACPI_TYPE_ANY, Node, ACPI_UINT32_MAX,
    677                 ACPI_NS_WALK_UNLOCK, AcpiEvRegRun, NULL,
    678                 &SpaceId, NULL);
    679 
    680     /* Special case for EC: handle "orphan" _REG methods with no region */
    681 
    682     if (SpaceId == ACPI_ADR_SPACE_EC)
    683     {
    684         AcpiEvOrphanEcRegMethod (Node);
    685     }
    686 
    687     return_ACPI_STATUS (Status);
    688 }
    689 
    690 
    691 /*******************************************************************************
    692  *
    693  * FUNCTION:    AcpiEvRegRun
    694  *
    695  * PARAMETERS:  WalkNamespace callback
    696  *
    697  * DESCRIPTION: Run _REG method for region objects of the requested spaceID
    698  *
    699  ******************************************************************************/
    700 
    701 static ACPI_STATUS
    702 AcpiEvRegRun (
    703     ACPI_HANDLE             ObjHandle,
    704     UINT32                  Level,
    705     void                    *Context,
    706     void                    **ReturnValue)
    707 {
    708     ACPI_OPERAND_OBJECT     *ObjDesc;
    709     ACPI_NAMESPACE_NODE     *Node;
    710     ACPI_ADR_SPACE_TYPE     SpaceId;
    711     ACPI_STATUS             Status;
    712 
    713 
    714     SpaceId = *ACPI_CAST_PTR (ACPI_ADR_SPACE_TYPE, Context);
    715 
    716     /* Convert and validate the device handle */
    717 
    718     Node = AcpiNsValidateHandle (ObjHandle);
    719     if (!Node)
    720     {
    721         return (AE_BAD_PARAMETER);
    722     }
    723 
    724     /*
    725      * We only care about regions.and objects that are allowed to have address
    726      * space handlers
    727      */
    728     if ((Node->Type != ACPI_TYPE_REGION) &&
    729         (Node != AcpiGbl_RootNode))
    730     {
    731         return (AE_OK);
    732     }
    733 
    734     /* Check for an existing internal object */
    735 
    736     ObjDesc = AcpiNsGetAttachedObject (Node);
    737     if (!ObjDesc)
    738     {
    739         /* No object, just exit */
    740 
    741         return (AE_OK);
    742     }
    743 
    744     /* Object is a Region */
    745 
    746     if (ObjDesc->Region.SpaceId != SpaceId)
    747     {
    748         /* This region is for a different address space, just ignore it */
    749 
    750         return (AE_OK);
    751     }
    752 
    753     Status = AcpiEvExecuteRegMethod (ObjDesc, ACPI_REG_CONNECT);
    754     return (Status);
    755 }
    756 
    757 
    758 /*******************************************************************************
    759  *
    760  * FUNCTION:    AcpiEvOrphanEcRegMethod
    761  *
    762  * PARAMETERS:  EcDeviceNode        - Namespace node for an EC device
    763  *
    764  * RETURN:      None
    765  *
    766  * DESCRIPTION: Execute an "orphan" _REG method that appears under the EC
    767  *              device. This is a _REG method that has no corresponding region
    768  *              within the EC device scope. The orphan _REG method appears to
    769  *              have been enabled by the description of the ECDT in the ACPI
    770  *              specification: "The availability of the region space can be
    771  *              detected by providing a _REG method object underneath the
    772  *              Embedded Controller device."
    773  *
    774  *              To quickly access the EC device, we use the EcDeviceNode used
    775  *              during EC handler installation. Otherwise, we would need to
    776  *              perform a time consuming namespace walk, executing _HID
    777  *              methods to find the EC device.
    778  *
    779  *  MUTEX:      Assumes the namespace is locked
    780  *
    781  ******************************************************************************/
    782 
    783 static void
    784 AcpiEvOrphanEcRegMethod (
    785     ACPI_NAMESPACE_NODE     *EcDeviceNode)
    786 {
    787     ACPI_HANDLE             RegMethod;
    788     ACPI_NAMESPACE_NODE     *NextNode;
    789     ACPI_STATUS             Status;
    790     ACPI_OBJECT_LIST        Args;
    791     ACPI_OBJECT             Objects[2];
    792 
    793 
    794     ACPI_FUNCTION_TRACE (EvOrphanEcRegMethod);
    795 
    796 
    797     if (!EcDeviceNode)
    798     {
    799         return_VOID;
    800     }
    801 
    802     /* Namespace is currently locked, must release */
    803 
    804     (void) AcpiUtReleaseMutex (ACPI_MTX_NAMESPACE);
    805 
    806     /* Get a handle to a _REG method immediately under the EC device */
    807 
    808     Status = AcpiGetHandle (EcDeviceNode, METHOD_NAME__REG, &RegMethod);
    809     if (ACPI_FAILURE (Status))
    810     {
    811         goto Exit; /* There is no _REG method present */
    812     }
    813 
    814     /*
    815      * Execute the _REG method only if there is no Operation Region in
    816      * this scope with the Embedded Controller space ID. Otherwise, it
    817      * will already have been executed. Note, this allows for Regions
    818      * with other space IDs to be present; but the code below will then
    819      * execute the _REG method with the EmbeddedControl SpaceID argument.
    820      */
    821     NextNode = AcpiNsGetNextNode (EcDeviceNode, NULL);
    822     while (NextNode)
    823     {
    824         if ((NextNode->Type == ACPI_TYPE_REGION) &&
    825             (NextNode->Object) &&
    826             (NextNode->Object->Region.SpaceId == ACPI_ADR_SPACE_EC))
    827         {
    828             goto Exit; /* Do not execute the _REG */
    829         }
    830 
    831         NextNode = AcpiNsGetNextNode (EcDeviceNode, NextNode);
    832     }
    833 
    834     /* Evaluate the _REG(EmbeddedControl,Connect) method */
    835 
    836     Args.Count = 2;
    837     Args.Pointer = Objects;
    838     Objects[0].Type = ACPI_TYPE_INTEGER;
    839     Objects[0].Integer.Value = ACPI_ADR_SPACE_EC;
    840     Objects[1].Type = ACPI_TYPE_INTEGER;
    841     Objects[1].Integer.Value = ACPI_REG_CONNECT;
    842 
    843     Status = AcpiEvaluateObject (RegMethod, NULL, &Args, NULL);
    844 
    845 Exit:
    846     /* We ignore all errors from above, don't care */
    847 
    848     Status = AcpiUtAcquireMutex (ACPI_MTX_NAMESPACE);
    849     return_VOID;
    850 }
    851