Home | History | Annotate | Line # | Download | only in executer
exmutex.c revision 1.1.1.6
      1      1.1    jruoho /******************************************************************************
      2      1.1    jruoho  *
      3      1.1    jruoho  * Module Name: exmutex - ASL Mutex Acquire/Release functions
      4      1.1    jruoho  *
      5      1.1    jruoho  *****************************************************************************/
      6      1.1    jruoho 
      7  1.1.1.2    jruoho /*
      8  1.1.1.6  christos  * Copyright (C) 2000 - 2016, Intel Corp.
      9      1.1    jruoho  * All rights reserved.
     10      1.1    jruoho  *
     11  1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12  1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13  1.1.1.2    jruoho  * are met:
     14  1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15  1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16  1.1.1.2    jruoho  *    without modification.
     17  1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21  1.1.1.2    jruoho  *    binary redistribution.
     22  1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23  1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24  1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25  1.1.1.2    jruoho  *
     26  1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27  1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28  1.1.1.2    jruoho  * Software Foundation.
     29  1.1.1.2    jruoho  *
     30  1.1.1.2    jruoho  * NO WARRANTY
     31  1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42  1.1.1.2    jruoho  */
     43      1.1    jruoho 
     44      1.1    jruoho #include "acpi.h"
     45      1.1    jruoho #include "accommon.h"
     46      1.1    jruoho #include "acinterp.h"
     47      1.1    jruoho #include "acevents.h"
     48      1.1    jruoho 
     49      1.1    jruoho #define _COMPONENT          ACPI_EXECUTER
     50      1.1    jruoho         ACPI_MODULE_NAME    ("exmutex")
     51      1.1    jruoho 
     52      1.1    jruoho /* Local prototypes */
     53      1.1    jruoho 
     54      1.1    jruoho static void
     55      1.1    jruoho AcpiExLinkMutex (
     56      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
     57      1.1    jruoho     ACPI_THREAD_STATE       *Thread);
     58      1.1    jruoho 
     59      1.1    jruoho 
     60      1.1    jruoho /*******************************************************************************
     61      1.1    jruoho  *
     62      1.1    jruoho  * FUNCTION:    AcpiExUnlinkMutex
     63      1.1    jruoho  *
     64      1.1    jruoho  * PARAMETERS:  ObjDesc             - The mutex to be unlinked
     65      1.1    jruoho  *
     66      1.1    jruoho  * RETURN:      None
     67      1.1    jruoho  *
     68      1.1    jruoho  * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
     69      1.1    jruoho  *
     70      1.1    jruoho  ******************************************************************************/
     71      1.1    jruoho 
     72      1.1    jruoho void
     73      1.1    jruoho AcpiExUnlinkMutex (
     74      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc)
     75      1.1    jruoho {
     76      1.1    jruoho     ACPI_THREAD_STATE       *Thread = ObjDesc->Mutex.OwnerThread;
     77      1.1    jruoho 
     78      1.1    jruoho 
     79      1.1    jruoho     if (!Thread)
     80      1.1    jruoho     {
     81      1.1    jruoho         return;
     82      1.1    jruoho     }
     83      1.1    jruoho 
     84      1.1    jruoho     /* Doubly linked list */
     85      1.1    jruoho 
     86      1.1    jruoho     if (ObjDesc->Mutex.Next)
     87      1.1    jruoho     {
     88      1.1    jruoho         (ObjDesc->Mutex.Next)->Mutex.Prev = ObjDesc->Mutex.Prev;
     89      1.1    jruoho     }
     90      1.1    jruoho 
     91      1.1    jruoho     if (ObjDesc->Mutex.Prev)
     92      1.1    jruoho     {
     93      1.1    jruoho         (ObjDesc->Mutex.Prev)->Mutex.Next = ObjDesc->Mutex.Next;
     94      1.1    jruoho 
     95      1.1    jruoho         /*
     96      1.1    jruoho          * Migrate the previous sync level associated with this mutex to
     97      1.1    jruoho          * the previous mutex on the list so that it may be preserved.
     98      1.1    jruoho          * This handles the case where several mutexes have been acquired
     99      1.1    jruoho          * at the same level, but are not released in opposite order.
    100      1.1    jruoho          */
    101      1.1    jruoho         (ObjDesc->Mutex.Prev)->Mutex.OriginalSyncLevel =
    102      1.1    jruoho             ObjDesc->Mutex.OriginalSyncLevel;
    103      1.1    jruoho     }
    104      1.1    jruoho     else
    105      1.1    jruoho     {
    106      1.1    jruoho         Thread->AcquiredMutexList = ObjDesc->Mutex.Next;
    107      1.1    jruoho     }
    108      1.1    jruoho }
    109      1.1    jruoho 
    110      1.1    jruoho 
    111      1.1    jruoho /*******************************************************************************
    112      1.1    jruoho  *
    113      1.1    jruoho  * FUNCTION:    AcpiExLinkMutex
    114      1.1    jruoho  *
    115      1.1    jruoho  * PARAMETERS:  ObjDesc             - The mutex to be linked
    116      1.1    jruoho  *              Thread              - Current executing thread object
    117      1.1    jruoho  *
    118      1.1    jruoho  * RETURN:      None
    119      1.1    jruoho  *
    120      1.1    jruoho  * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
    121      1.1    jruoho  *
    122      1.1    jruoho  ******************************************************************************/
    123      1.1    jruoho 
    124      1.1    jruoho static void
    125      1.1    jruoho AcpiExLinkMutex (
    126      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    127      1.1    jruoho     ACPI_THREAD_STATE       *Thread)
    128      1.1    jruoho {
    129      1.1    jruoho     ACPI_OPERAND_OBJECT     *ListHead;
    130      1.1    jruoho 
    131      1.1    jruoho 
    132      1.1    jruoho     ListHead = Thread->AcquiredMutexList;
    133      1.1    jruoho 
    134      1.1    jruoho     /* This object will be the first object in the list */
    135      1.1    jruoho 
    136      1.1    jruoho     ObjDesc->Mutex.Prev = NULL;
    137      1.1    jruoho     ObjDesc->Mutex.Next = ListHead;
    138      1.1    jruoho 
    139      1.1    jruoho     /* Update old first object to point back to this object */
    140      1.1    jruoho 
    141      1.1    jruoho     if (ListHead)
    142      1.1    jruoho     {
    143      1.1    jruoho         ListHead->Mutex.Prev = ObjDesc;
    144      1.1    jruoho     }
    145      1.1    jruoho 
    146      1.1    jruoho     /* Update list head */
    147      1.1    jruoho 
    148      1.1    jruoho     Thread->AcquiredMutexList = ObjDesc;
    149      1.1    jruoho }
    150      1.1    jruoho 
    151      1.1    jruoho 
    152      1.1    jruoho /*******************************************************************************
    153      1.1    jruoho  *
    154      1.1    jruoho  * FUNCTION:    AcpiExAcquireMutexObject
    155      1.1    jruoho  *
    156      1.1    jruoho  * PARAMETERS:  Timeout             - Timeout in milliseconds
    157      1.1    jruoho  *              ObjDesc             - Mutex object
    158      1.1    jruoho  *              ThreadId            - Current thread state
    159      1.1    jruoho  *
    160      1.1    jruoho  * RETURN:      Status
    161      1.1    jruoho  *
    162      1.1    jruoho  * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
    163      1.1    jruoho  *              path that supports multiple acquires by the same thread.
    164      1.1    jruoho  *
    165      1.1    jruoho  * MUTEX:       Interpreter must be locked
    166      1.1    jruoho  *
    167      1.1    jruoho  * NOTE: This interface is called from three places:
    168      1.1    jruoho  * 1) From AcpiExAcquireMutex, via an AML Acquire() operator
    169      1.1    jruoho  * 2) From AcpiExAcquireGlobalLock when an AML Field access requires the
    170      1.1    jruoho  *    global lock
    171      1.1    jruoho  * 3) From the external interface, AcpiAcquireGlobalLock
    172      1.1    jruoho  *
    173      1.1    jruoho  ******************************************************************************/
    174      1.1    jruoho 
    175      1.1    jruoho ACPI_STATUS
    176      1.1    jruoho AcpiExAcquireMutexObject (
    177      1.1    jruoho     UINT16                  Timeout,
    178      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    179      1.1    jruoho     ACPI_THREAD_ID          ThreadId)
    180      1.1    jruoho {
    181      1.1    jruoho     ACPI_STATUS             Status;
    182      1.1    jruoho 
    183      1.1    jruoho 
    184      1.1    jruoho     ACPI_FUNCTION_TRACE_PTR (ExAcquireMutexObject, ObjDesc);
    185      1.1    jruoho 
    186      1.1    jruoho 
    187      1.1    jruoho     if (!ObjDesc)
    188      1.1    jruoho     {
    189      1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    190      1.1    jruoho     }
    191      1.1    jruoho 
    192      1.1    jruoho     /* Support for multiple acquires by the owning thread */
    193      1.1    jruoho 
    194      1.1    jruoho     if (ObjDesc->Mutex.ThreadId == ThreadId)
    195      1.1    jruoho     {
    196      1.1    jruoho         /*
    197      1.1    jruoho          * The mutex is already owned by this thread, just increment the
    198      1.1    jruoho          * acquisition depth
    199      1.1    jruoho          */
    200      1.1    jruoho         ObjDesc->Mutex.AcquisitionDepth++;
    201      1.1    jruoho         return_ACPI_STATUS (AE_OK);
    202      1.1    jruoho     }
    203      1.1    jruoho 
    204      1.1    jruoho     /* Acquire the mutex, wait if necessary. Special case for Global Lock */
    205      1.1    jruoho 
    206      1.1    jruoho     if (ObjDesc == AcpiGbl_GlobalLockMutex)
    207      1.1    jruoho     {
    208      1.1    jruoho         Status = AcpiEvAcquireGlobalLock (Timeout);
    209      1.1    jruoho     }
    210      1.1    jruoho     else
    211      1.1    jruoho     {
    212  1.1.1.6  christos         Status = AcpiExSystemWaitMutex (ObjDesc->Mutex.OsMutex, Timeout);
    213      1.1    jruoho     }
    214      1.1    jruoho 
    215      1.1    jruoho     if (ACPI_FAILURE (Status))
    216      1.1    jruoho     {
    217      1.1    jruoho         /* Includes failure from a timeout on TimeDesc */
    218      1.1    jruoho 
    219      1.1    jruoho         return_ACPI_STATUS (Status);
    220      1.1    jruoho     }
    221      1.1    jruoho 
    222      1.1    jruoho     /* Acquired the mutex: update mutex object */
    223      1.1    jruoho 
    224      1.1    jruoho     ObjDesc->Mutex.ThreadId = ThreadId;
    225      1.1    jruoho     ObjDesc->Mutex.AcquisitionDepth = 1;
    226      1.1    jruoho     ObjDesc->Mutex.OriginalSyncLevel = 0;
    227      1.1    jruoho     ObjDesc->Mutex.OwnerThread = NULL;      /* Used only for AML Acquire() */
    228      1.1    jruoho 
    229      1.1    jruoho     return_ACPI_STATUS (AE_OK);
    230      1.1    jruoho }
    231      1.1    jruoho 
    232      1.1    jruoho 
    233      1.1    jruoho /*******************************************************************************
    234      1.1    jruoho  *
    235      1.1    jruoho  * FUNCTION:    AcpiExAcquireMutex
    236      1.1    jruoho  *
    237      1.1    jruoho  * PARAMETERS:  TimeDesc            - Timeout integer
    238      1.1    jruoho  *              ObjDesc             - Mutex object
    239      1.1    jruoho  *              WalkState           - Current method execution state
    240      1.1    jruoho  *
    241      1.1    jruoho  * RETURN:      Status
    242      1.1    jruoho  *
    243      1.1    jruoho  * DESCRIPTION: Acquire an AML mutex
    244      1.1    jruoho  *
    245      1.1    jruoho  ******************************************************************************/
    246      1.1    jruoho 
    247      1.1    jruoho ACPI_STATUS
    248      1.1    jruoho AcpiExAcquireMutex (
    249      1.1    jruoho     ACPI_OPERAND_OBJECT     *TimeDesc,
    250      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    251      1.1    jruoho     ACPI_WALK_STATE         *WalkState)
    252      1.1    jruoho {
    253      1.1    jruoho     ACPI_STATUS             Status;
    254      1.1    jruoho 
    255      1.1    jruoho 
    256      1.1    jruoho     ACPI_FUNCTION_TRACE_PTR (ExAcquireMutex, ObjDesc);
    257      1.1    jruoho 
    258      1.1    jruoho 
    259      1.1    jruoho     if (!ObjDesc)
    260      1.1    jruoho     {
    261      1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    262      1.1    jruoho     }
    263      1.1    jruoho 
    264      1.1    jruoho     /* Must have a valid thread state struct */
    265      1.1    jruoho 
    266      1.1    jruoho     if (!WalkState->Thread)
    267      1.1    jruoho     {
    268      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    269      1.1    jruoho             "Cannot acquire Mutex [%4.4s], null thread info",
    270      1.1    jruoho             AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
    271      1.1    jruoho         return_ACPI_STATUS (AE_AML_INTERNAL);
    272      1.1    jruoho     }
    273      1.1    jruoho 
    274      1.1    jruoho     /*
    275  1.1.1.6  christos      * Current sync level must be less than or equal to the sync level
    276  1.1.1.6  christos      * of the mutex. This mechanism provides some deadlock prevention.
    277      1.1    jruoho      */
    278      1.1    jruoho     if (WalkState->Thread->CurrentSyncLevel > ObjDesc->Mutex.SyncLevel)
    279      1.1    jruoho     {
    280      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    281  1.1.1.6  christos             "Cannot acquire Mutex [%4.4s], "
    282  1.1.1.6  christos             "current SyncLevel is too large (%u)",
    283      1.1    jruoho             AcpiUtGetNodeName (ObjDesc->Mutex.Node),
    284      1.1    jruoho             WalkState->Thread->CurrentSyncLevel));
    285      1.1    jruoho         return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
    286      1.1    jruoho     }
    287      1.1    jruoho 
    288  1.1.1.6  christos     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
    289  1.1.1.6  christos         "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
    290  1.1.1.6  christos         "Depth %u TID %p\n",
    291  1.1.1.6  christos         ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
    292  1.1.1.6  christos         ObjDesc->Mutex.AcquisitionDepth, WalkState->Thread));
    293  1.1.1.6  christos 
    294      1.1    jruoho     Status = AcpiExAcquireMutexObject ((UINT16) TimeDesc->Integer.Value,
    295  1.1.1.6  christos         ObjDesc, WalkState->Thread->ThreadId);
    296  1.1.1.6  christos 
    297      1.1    jruoho     if (ACPI_SUCCESS (Status) && ObjDesc->Mutex.AcquisitionDepth == 1)
    298      1.1    jruoho     {
    299      1.1    jruoho         /* Save Thread object, original/current sync levels */
    300      1.1    jruoho 
    301      1.1    jruoho         ObjDesc->Mutex.OwnerThread = WalkState->Thread;
    302  1.1.1.6  christos         ObjDesc->Mutex.OriginalSyncLevel =
    303  1.1.1.6  christos             WalkState->Thread->CurrentSyncLevel;
    304  1.1.1.6  christos         WalkState->Thread->CurrentSyncLevel =
    305  1.1.1.6  christos             ObjDesc->Mutex.SyncLevel;
    306      1.1    jruoho 
    307      1.1    jruoho         /* Link the mutex to the current thread for force-unlock at method exit */
    308      1.1    jruoho 
    309      1.1    jruoho         AcpiExLinkMutex (ObjDesc, WalkState->Thread);
    310      1.1    jruoho     }
    311      1.1    jruoho 
    312  1.1.1.6  christos     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
    313  1.1.1.6  christos         "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
    314  1.1.1.6  christos         ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
    315  1.1.1.6  christos         ObjDesc->Mutex.AcquisitionDepth));
    316  1.1.1.6  christos 
    317      1.1    jruoho     return_ACPI_STATUS (Status);
    318      1.1    jruoho }
    319      1.1    jruoho 
    320      1.1    jruoho 
    321      1.1    jruoho /*******************************************************************************
    322      1.1    jruoho  *
    323      1.1    jruoho  * FUNCTION:    AcpiExReleaseMutexObject
    324      1.1    jruoho  *
    325      1.1    jruoho  * PARAMETERS:  ObjDesc             - The object descriptor for this op
    326      1.1    jruoho  *
    327      1.1    jruoho  * RETURN:      Status
    328      1.1    jruoho  *
    329      1.1    jruoho  * DESCRIPTION: Release a previously acquired Mutex, low level interface.
    330      1.1    jruoho  *              Provides a common path that supports multiple releases (after
    331      1.1    jruoho  *              previous multiple acquires) by the same thread.
    332      1.1    jruoho  *
    333      1.1    jruoho  * MUTEX:       Interpreter must be locked
    334      1.1    jruoho  *
    335      1.1    jruoho  * NOTE: This interface is called from three places:
    336      1.1    jruoho  * 1) From AcpiExReleaseMutex, via an AML Acquire() operator
    337      1.1    jruoho  * 2) From AcpiExReleaseGlobalLock when an AML Field access requires the
    338      1.1    jruoho  *    global lock
    339      1.1    jruoho  * 3) From the external interface, AcpiReleaseGlobalLock
    340      1.1    jruoho  *
    341      1.1    jruoho  ******************************************************************************/
    342      1.1    jruoho 
    343      1.1    jruoho ACPI_STATUS
    344      1.1    jruoho AcpiExReleaseMutexObject (
    345      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc)
    346      1.1    jruoho {
    347      1.1    jruoho     ACPI_STATUS             Status = AE_OK;
    348      1.1    jruoho 
    349      1.1    jruoho 
    350      1.1    jruoho     ACPI_FUNCTION_TRACE (ExReleaseMutexObject);
    351      1.1    jruoho 
    352      1.1    jruoho 
    353      1.1    jruoho     if (ObjDesc->Mutex.AcquisitionDepth == 0)
    354      1.1    jruoho     {
    355  1.1.1.3  christos         return_ACPI_STATUS (AE_NOT_ACQUIRED);
    356      1.1    jruoho     }
    357      1.1    jruoho 
    358      1.1    jruoho     /* Match multiple Acquires with multiple Releases */
    359      1.1    jruoho 
    360      1.1    jruoho     ObjDesc->Mutex.AcquisitionDepth--;
    361      1.1    jruoho     if (ObjDesc->Mutex.AcquisitionDepth != 0)
    362      1.1    jruoho     {
    363      1.1    jruoho         /* Just decrement the depth and return */
    364      1.1    jruoho 
    365      1.1    jruoho         return_ACPI_STATUS (AE_OK);
    366      1.1    jruoho     }
    367      1.1    jruoho 
    368      1.1    jruoho     if (ObjDesc->Mutex.OwnerThread)
    369      1.1    jruoho     {
    370      1.1    jruoho         /* Unlink the mutex from the owner's list */
    371      1.1    jruoho 
    372      1.1    jruoho         AcpiExUnlinkMutex (ObjDesc);
    373      1.1    jruoho         ObjDesc->Mutex.OwnerThread = NULL;
    374      1.1    jruoho     }
    375      1.1    jruoho 
    376      1.1    jruoho     /* Release the mutex, special case for Global Lock */
    377      1.1    jruoho 
    378      1.1    jruoho     if (ObjDesc == AcpiGbl_GlobalLockMutex)
    379      1.1    jruoho     {
    380      1.1    jruoho         Status = AcpiEvReleaseGlobalLock ();
    381      1.1    jruoho     }
    382      1.1    jruoho     else
    383      1.1    jruoho     {
    384      1.1    jruoho         AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
    385      1.1    jruoho     }
    386      1.1    jruoho 
    387      1.1    jruoho     /* Clear mutex info */
    388      1.1    jruoho 
    389      1.1    jruoho     ObjDesc->Mutex.ThreadId = 0;
    390      1.1    jruoho     return_ACPI_STATUS (Status);
    391      1.1    jruoho }
    392      1.1    jruoho 
    393      1.1    jruoho 
    394      1.1    jruoho /*******************************************************************************
    395      1.1    jruoho  *
    396      1.1    jruoho  * FUNCTION:    AcpiExReleaseMutex
    397      1.1    jruoho  *
    398      1.1    jruoho  * PARAMETERS:  ObjDesc             - The object descriptor for this op
    399      1.1    jruoho  *              WalkState           - Current method execution state
    400      1.1    jruoho  *
    401      1.1    jruoho  * RETURN:      Status
    402      1.1    jruoho  *
    403      1.1    jruoho  * DESCRIPTION: Release a previously acquired Mutex.
    404      1.1    jruoho  *
    405      1.1    jruoho  ******************************************************************************/
    406      1.1    jruoho 
    407      1.1    jruoho ACPI_STATUS
    408      1.1    jruoho AcpiExReleaseMutex (
    409      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    410      1.1    jruoho     ACPI_WALK_STATE         *WalkState)
    411      1.1    jruoho {
    412      1.1    jruoho     UINT8                   PreviousSyncLevel;
    413      1.1    jruoho     ACPI_THREAD_STATE       *OwnerThread;
    414  1.1.1.6  christos     ACPI_STATUS             Status = AE_OK;
    415      1.1    jruoho 
    416      1.1    jruoho 
    417      1.1    jruoho     ACPI_FUNCTION_TRACE (ExReleaseMutex);
    418      1.1    jruoho 
    419      1.1    jruoho 
    420      1.1    jruoho     if (!ObjDesc)
    421      1.1    jruoho     {
    422      1.1    jruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
    423      1.1    jruoho     }
    424      1.1    jruoho 
    425      1.1    jruoho     OwnerThread = ObjDesc->Mutex.OwnerThread;
    426      1.1    jruoho 
    427      1.1    jruoho     /* The mutex must have been previously acquired in order to release it */
    428      1.1    jruoho 
    429      1.1    jruoho     if (!OwnerThread)
    430      1.1    jruoho     {
    431      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    432      1.1    jruoho             "Cannot release Mutex [%4.4s], not acquired",
    433      1.1    jruoho             AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
    434      1.1    jruoho         return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED);
    435      1.1    jruoho     }
    436      1.1    jruoho 
    437      1.1    jruoho     /* Must have a valid thread ID */
    438      1.1    jruoho 
    439      1.1    jruoho     if (!WalkState->Thread)
    440      1.1    jruoho     {
    441      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    442      1.1    jruoho             "Cannot release Mutex [%4.4s], null thread info",
    443      1.1    jruoho             AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
    444      1.1    jruoho         return_ACPI_STATUS (AE_AML_INTERNAL);
    445      1.1    jruoho     }
    446      1.1    jruoho 
    447      1.1    jruoho     /*
    448      1.1    jruoho      * The Mutex is owned, but this thread must be the owner.
    449      1.1    jruoho      * Special case for Global Lock, any thread can release
    450      1.1    jruoho      */
    451      1.1    jruoho     if ((OwnerThread->ThreadId != WalkState->Thread->ThreadId) &&
    452      1.1    jruoho         (ObjDesc != AcpiGbl_GlobalLockMutex))
    453      1.1    jruoho     {
    454      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    455  1.1.1.2    jruoho             "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
    456  1.1.1.2    jruoho             (UINT32) WalkState->Thread->ThreadId,
    457      1.1    jruoho             AcpiUtGetNodeName (ObjDesc->Mutex.Node),
    458  1.1.1.2    jruoho             (UINT32) OwnerThread->ThreadId));
    459      1.1    jruoho         return_ACPI_STATUS (AE_AML_NOT_OWNER);
    460      1.1    jruoho     }
    461      1.1    jruoho 
    462      1.1    jruoho     /*
    463      1.1    jruoho      * The sync level of the mutex must be equal to the current sync level. In
    464      1.1    jruoho      * other words, the current level means that at least one mutex at that
    465      1.1    jruoho      * level is currently being held. Attempting to release a mutex of a
    466      1.1    jruoho      * different level can only mean that the mutex ordering rule is being
    467      1.1    jruoho      * violated. This behavior is clarified in ACPI 4.0 specification.
    468      1.1    jruoho      */
    469      1.1    jruoho     if (ObjDesc->Mutex.SyncLevel != OwnerThread->CurrentSyncLevel)
    470      1.1    jruoho     {
    471      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    472  1.1.1.6  christos             "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
    473  1.1.1.6  christos             "mutex %u current %u",
    474      1.1    jruoho             AcpiUtGetNodeName (ObjDesc->Mutex.Node),
    475      1.1    jruoho             ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel));
    476      1.1    jruoho         return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
    477      1.1    jruoho     }
    478      1.1    jruoho 
    479      1.1    jruoho     /*
    480      1.1    jruoho      * Get the previous SyncLevel from the head of the acquired mutex list.
    481      1.1    jruoho      * This handles the case where several mutexes at the same level have been
    482      1.1    jruoho      * acquired, but are not released in reverse order.
    483      1.1    jruoho      */
    484      1.1    jruoho     PreviousSyncLevel =
    485      1.1    jruoho         OwnerThread->AcquiredMutexList->Mutex.OriginalSyncLevel;
    486      1.1    jruoho 
    487  1.1.1.6  christos     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
    488  1.1.1.6  christos         "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
    489  1.1.1.6  christos         "Prev SyncLevel %u, Depth %u TID %p\n",
    490  1.1.1.6  christos         ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
    491  1.1.1.6  christos         PreviousSyncLevel, ObjDesc->Mutex.AcquisitionDepth,
    492  1.1.1.6  christos         WalkState->Thread));
    493  1.1.1.6  christos 
    494      1.1    jruoho     Status = AcpiExReleaseMutexObject (ObjDesc);
    495      1.1    jruoho     if (ACPI_FAILURE (Status))
    496      1.1    jruoho     {
    497      1.1    jruoho         return_ACPI_STATUS (Status);
    498      1.1    jruoho     }
    499      1.1    jruoho 
    500      1.1    jruoho     if (ObjDesc->Mutex.AcquisitionDepth == 0)
    501      1.1    jruoho     {
    502      1.1    jruoho         /* Restore the previous SyncLevel */
    503      1.1    jruoho 
    504      1.1    jruoho         OwnerThread->CurrentSyncLevel = PreviousSyncLevel;
    505      1.1    jruoho     }
    506      1.1    jruoho 
    507  1.1.1.6  christos     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
    508  1.1.1.6  christos         "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
    509  1.1.1.6  christos         "Prev SyncLevel %u, Depth %u\n",
    510  1.1.1.6  christos         ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
    511  1.1.1.6  christos         PreviousSyncLevel, ObjDesc->Mutex.AcquisitionDepth));
    512  1.1.1.6  christos 
    513      1.1    jruoho     return_ACPI_STATUS (Status);
    514      1.1    jruoho }
    515      1.1    jruoho 
    516      1.1    jruoho 
    517      1.1    jruoho /*******************************************************************************
    518      1.1    jruoho  *
    519      1.1    jruoho  * FUNCTION:    AcpiExReleaseAllMutexes
    520      1.1    jruoho  *
    521      1.1    jruoho  * PARAMETERS:  Thread              - Current executing thread object
    522      1.1    jruoho  *
    523      1.1    jruoho  * RETURN:      Status
    524      1.1    jruoho  *
    525      1.1    jruoho  * DESCRIPTION: Release all mutexes held by this thread
    526      1.1    jruoho  *
    527      1.1    jruoho  * NOTE: This function is called as the thread is exiting the interpreter.
    528      1.1    jruoho  * Mutexes are not released when an individual control method is exited, but
    529      1.1    jruoho  * only when the parent thread actually exits the interpreter. This allows one
    530      1.1    jruoho  * method to acquire a mutex, and a different method to release it, as long as
    531      1.1    jruoho  * this is performed underneath a single parent control method.
    532      1.1    jruoho  *
    533      1.1    jruoho  ******************************************************************************/
    534      1.1    jruoho 
    535      1.1    jruoho void
    536      1.1    jruoho AcpiExReleaseAllMutexes (
    537      1.1    jruoho     ACPI_THREAD_STATE       *Thread)
    538      1.1    jruoho {
    539      1.1    jruoho     ACPI_OPERAND_OBJECT     *Next = Thread->AcquiredMutexList;
    540      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc;
    541      1.1    jruoho 
    542      1.1    jruoho 
    543  1.1.1.6  christos     ACPI_FUNCTION_TRACE (ExReleaseAllMutexes);
    544      1.1    jruoho 
    545      1.1    jruoho 
    546      1.1    jruoho     /* Traverse the list of owned mutexes, releasing each one */
    547      1.1    jruoho 
    548      1.1    jruoho     while (Next)
    549      1.1    jruoho     {
    550      1.1    jruoho         ObjDesc = Next;
    551  1.1.1.3  christos         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
    552  1.1.1.6  christos             "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
    553  1.1.1.6  christos             ObjDesc->Mutex.Node->Name.Ascii, ObjDesc->Mutex.SyncLevel,
    554  1.1.1.6  christos             ObjDesc->Mutex.AcquisitionDepth));
    555  1.1.1.3  christos 
    556      1.1    jruoho         /* Release the mutex, special case for Global Lock */
    557      1.1    jruoho 
    558      1.1    jruoho         if (ObjDesc == AcpiGbl_GlobalLockMutex)
    559      1.1    jruoho         {
    560      1.1    jruoho             /* Ignore errors */
    561      1.1    jruoho 
    562      1.1    jruoho             (void) AcpiEvReleaseGlobalLock ();
    563      1.1    jruoho         }
    564      1.1    jruoho         else
    565      1.1    jruoho         {
    566      1.1    jruoho             AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
    567      1.1    jruoho         }
    568      1.1    jruoho 
    569  1.1.1.6  christos         /* Update Thread SyncLevel (Last mutex is the important one) */
    570  1.1.1.6  christos 
    571  1.1.1.6  christos         Thread->CurrentSyncLevel = ObjDesc->Mutex.OriginalSyncLevel;
    572  1.1.1.6  christos 
    573      1.1    jruoho         /* Mark mutex unowned */
    574      1.1    jruoho 
    575  1.1.1.6  christos         Next = ObjDesc->Mutex.Next;
    576  1.1.1.6  christos 
    577  1.1.1.6  christos         ObjDesc->Mutex.Prev = NULL;
    578  1.1.1.6  christos         ObjDesc->Mutex.Next = NULL;
    579  1.1.1.6  christos         ObjDesc->Mutex.AcquisitionDepth = 0;
    580      1.1    jruoho         ObjDesc->Mutex.OwnerThread = NULL;
    581      1.1    jruoho         ObjDesc->Mutex.ThreadId = 0;
    582      1.1    jruoho     }
    583  1.1.1.6  christos 
    584  1.1.1.6  christos     return_VOID;
    585      1.1    jruoho }
    586