13446Smrj /*******************************************************************************
23446Smrj  *
33446Smrj  * Module Name: utmutex - local mutex support
4*7851SDana.Myers@Sun.COM  *              $Revision: 1.13 $
53446Smrj  *
63446Smrj  ******************************************************************************/
73446Smrj 
83446Smrj /******************************************************************************
93446Smrj  *
103446Smrj  * 1. Copyright Notice
113446Smrj  *
12*7851SDana.Myers@Sun.COM  * Some or all of this work - Copyright (c) 1999 - 2008, Intel Corp.
133446Smrj  * All rights reserved.
143446Smrj  *
153446Smrj  * 2. License
163446Smrj  *
173446Smrj  * 2.1. This is your license from Intel Corp. under its intellectual property
183446Smrj  * rights.  You may have additional license terms from the party that provided
193446Smrj  * you this software, covering your right to use that party's intellectual
203446Smrj  * property rights.
213446Smrj  *
223446Smrj  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
233446Smrj  * copy of the source code appearing in this file ("Covered Code") an
243446Smrj  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
253446Smrj  * base code distributed originally by Intel ("Original Intel Code") to copy,
263446Smrj  * make derivatives, distribute, use and display any portion of the Covered
273446Smrj  * Code in any form, with the right to sublicense such rights; and
283446Smrj  *
293446Smrj  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
303446Smrj  * license (with the right to sublicense), under only those claims of Intel
313446Smrj  * patents that are infringed by the Original Intel Code, to make, use, sell,
323446Smrj  * offer to sell, and import the Covered Code and derivative works thereof
333446Smrj  * solely to the minimum extent necessary to exercise the above copyright
343446Smrj  * license, and in no event shall the patent license extend to any additions
353446Smrj  * to or modifications of the Original Intel Code.  No other license or right
363446Smrj  * is granted directly or by implication, estoppel or otherwise;
373446Smrj  *
383446Smrj  * The above copyright and patent license is granted only if the following
393446Smrj  * conditions are met:
403446Smrj  *
413446Smrj  * 3. Conditions
423446Smrj  *
433446Smrj  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
443446Smrj  * Redistribution of source code of any substantial portion of the Covered
453446Smrj  * Code or modification with rights to further distribute source must include
463446Smrj  * the above Copyright Notice, the above License, this list of Conditions,
473446Smrj  * and the following Disclaimer and Export Compliance provision.  In addition,
483446Smrj  * Licensee must cause all Covered Code to which Licensee contributes to
493446Smrj  * contain a file documenting the changes Licensee made to create that Covered
503446Smrj  * Code and the date of any change.  Licensee must include in that file the
513446Smrj  * documentation of any changes made by any predecessor Licensee.  Licensee
523446Smrj  * must include a prominent statement that the modification is derived,
533446Smrj  * directly or indirectly, from Original Intel Code.
543446Smrj  *
553446Smrj  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
563446Smrj  * Redistribution of source code of any substantial portion of the Covered
573446Smrj  * Code or modification without rights to further distribute source must
583446Smrj  * include the following Disclaimer and Export Compliance provision in the
593446Smrj  * documentation and/or other materials provided with distribution.  In
603446Smrj  * addition, Licensee may not authorize further sublicense of source of any
613446Smrj  * portion of the Covered Code, and must include terms to the effect that the
623446Smrj  * license from Licensee to its licensee is limited to the intellectual
633446Smrj  * property embodied in the software Licensee provides to its licensee, and
643446Smrj  * not to intellectual property embodied in modifications its licensee may
653446Smrj  * make.
663446Smrj  *
673446Smrj  * 3.3. Redistribution of Executable. Redistribution in executable form of any
683446Smrj  * substantial portion of the Covered Code or modification must reproduce the
693446Smrj  * above Copyright Notice, and the following Disclaimer and Export Compliance
703446Smrj  * provision in the documentation and/or other materials provided with the
713446Smrj  * distribution.
723446Smrj  *
733446Smrj  * 3.4. Intel retains all right, title, and interest in and to the Original
743446Smrj  * Intel Code.
753446Smrj  *
763446Smrj  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
773446Smrj  * Intel shall be used in advertising or otherwise to promote the sale, use or
783446Smrj  * other dealings in products derived from or relating to the Covered Code
793446Smrj  * without prior written authorization from Intel.
803446Smrj  *
813446Smrj  * 4. Disclaimer and Export Compliance
823446Smrj  *
833446Smrj  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
843446Smrj  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
853446Smrj  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
863446Smrj  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
873446Smrj  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
883446Smrj  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
893446Smrj  * PARTICULAR PURPOSE.
903446Smrj  *
913446Smrj  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
923446Smrj  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
933446Smrj  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
943446Smrj  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
953446Smrj  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
963446Smrj  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
973446Smrj  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
983446Smrj  * LIMITED REMEDY.
993446Smrj  *
1003446Smrj  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1013446Smrj  * software or system incorporating such software without first obtaining any
1023446Smrj  * required license or other approval from the U. S. Department of Commerce or
1033446Smrj  * any other agency or department of the United States Government.  In the
1043446Smrj  * event Licensee exports any such software from the United States or
1053446Smrj  * re-exports any such software from a foreign destination, Licensee shall
1063446Smrj  * ensure that the distribution and export/re-export of the software is in
1073446Smrj  * compliance with all laws, regulations, orders, or other restrictions of the
1083446Smrj  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1093446Smrj  * any of its subsidiaries will export/re-export any technical data, process,
1103446Smrj  * software, or service, directly or indirectly, to any country for which the
1113446Smrj  * United States government or any agency thereof requires an export license,
1123446Smrj  * other governmental approval, or letter of assurance, without first obtaining
1133446Smrj  * such license, approval or letter.
1143446Smrj  *
1153446Smrj  *****************************************************************************/
1163446Smrj 
1173446Smrj 
1183446Smrj #define __UTMUTEX_C__
1193446Smrj 
1203446Smrj #include "acpi.h"
1213446Smrj 
1223446Smrj #define _COMPONENT          ACPI_UTILITIES
1233446Smrj         ACPI_MODULE_NAME    ("utmutex")
1243446Smrj 
1253446Smrj /* Local prototypes */
1263446Smrj 
1273446Smrj static ACPI_STATUS
1283446Smrj AcpiUtCreateMutex (
1293446Smrj     ACPI_MUTEX_HANDLE       MutexId);
1303446Smrj 
1313446Smrj static ACPI_STATUS
1323446Smrj AcpiUtDeleteMutex (
1333446Smrj     ACPI_MUTEX_HANDLE       MutexId);
1343446Smrj 
1353446Smrj 
1363446Smrj /*******************************************************************************
1373446Smrj  *
1383446Smrj  * FUNCTION:    AcpiUtMutexInitialize
1393446Smrj  *
1403446Smrj  * PARAMETERS:  None.
1413446Smrj  *
1423446Smrj  * RETURN:      Status
1433446Smrj  *
1443446Smrj  * DESCRIPTION: Create the system mutex objects.
1453446Smrj  *
1463446Smrj  ******************************************************************************/
1473446Smrj 
1483446Smrj ACPI_STATUS
1493446Smrj AcpiUtMutexInitialize (
1503446Smrj     void)
1513446Smrj {
1523446Smrj     UINT32                  i;
1533446Smrj     ACPI_STATUS             Status;
1543446Smrj 
1553446Smrj 
1563446Smrj     ACPI_FUNCTION_TRACE (UtMutexInitialize);
1573446Smrj 
1583446Smrj 
1593446Smrj     /*
1603446Smrj      * Create each of the predefined mutex objects
1613446Smrj      */
1623446Smrj     for (i = 0; i < ACPI_NUM_MUTEX; i++)
1633446Smrj     {
1643446Smrj         Status = AcpiUtCreateMutex (i);
1653446Smrj         if (ACPI_FAILURE (Status))
1663446Smrj         {
1673446Smrj             return_ACPI_STATUS (Status);
1683446Smrj         }
1693446Smrj     }
1703446Smrj 
1713446Smrj     /* Create the spinlocks for use at interrupt level */
1723446Smrj 
1733446Smrj     Status = AcpiOsCreateLock (&AcpiGbl_GpeLock);
1743446Smrj     if (ACPI_FAILURE (Status))
1753446Smrj     {
1763446Smrj         return_ACPI_STATUS (Status);
1773446Smrj     }
1783446Smrj 
1793446Smrj     Status = AcpiOsCreateLock (&AcpiGbl_HardwareLock);
1803446Smrj     return_ACPI_STATUS (Status);
1813446Smrj }
1823446Smrj 
1833446Smrj 
1843446Smrj /*******************************************************************************
1853446Smrj  *
1863446Smrj  * FUNCTION:    AcpiUtMutexTerminate
1873446Smrj  *
1883446Smrj  * PARAMETERS:  None.
1893446Smrj  *
1903446Smrj  * RETURN:      None.
1913446Smrj  *
1923446Smrj  * DESCRIPTION: Delete all of the system mutex objects.
1933446Smrj  *
1943446Smrj  ******************************************************************************/
1953446Smrj 
1963446Smrj void
1973446Smrj AcpiUtMutexTerminate (
1983446Smrj     void)
1993446Smrj {
2003446Smrj     UINT32                  i;
2013446Smrj 
2023446Smrj 
2033446Smrj     ACPI_FUNCTION_TRACE (UtMutexTerminate);
2043446Smrj 
2053446Smrj 
2063446Smrj     /*
2073446Smrj      * Delete each predefined mutex object
2083446Smrj      */
2093446Smrj     for (i = 0; i < ACPI_NUM_MUTEX; i++)
2103446Smrj     {
2113446Smrj         (void) AcpiUtDeleteMutex (i);
2123446Smrj     }
2133446Smrj 
2143446Smrj     /* Delete the spinlocks */
2153446Smrj 
2163446Smrj     AcpiOsDeleteLock (AcpiGbl_GpeLock);
2173446Smrj     AcpiOsDeleteLock (AcpiGbl_HardwareLock);
2183446Smrj     return_VOID;
2193446Smrj }
2203446Smrj 
2213446Smrj 
2223446Smrj /*******************************************************************************
2233446Smrj  *
2243446Smrj  * FUNCTION:    AcpiUtCreateMutex
2253446Smrj  *
2263446Smrj  * PARAMETERS:  MutexID         - ID of the mutex to be created
2273446Smrj  *
2283446Smrj  * RETURN:      Status
2293446Smrj  *
2303446Smrj  * DESCRIPTION: Create a mutex object.
2313446Smrj  *
2323446Smrj  ******************************************************************************/
2333446Smrj 
2343446Smrj static ACPI_STATUS
2353446Smrj AcpiUtCreateMutex (
2363446Smrj     ACPI_MUTEX_HANDLE       MutexId)
2373446Smrj {
2383446Smrj     ACPI_STATUS             Status = AE_OK;
2393446Smrj 
2403446Smrj 
2413446Smrj     ACPI_FUNCTION_TRACE_U32 (UtCreateMutex, MutexId);
2423446Smrj 
2433446Smrj 
2443446Smrj     if (MutexId > ACPI_MAX_MUTEX)
2453446Smrj     {
2463446Smrj         return_ACPI_STATUS (AE_BAD_PARAMETER);
2473446Smrj     }
2483446Smrj 
2493446Smrj     if (!AcpiGbl_MutexInfo[MutexId].Mutex)
2503446Smrj     {
2513446Smrj         Status = AcpiOsCreateMutex (&AcpiGbl_MutexInfo[MutexId].Mutex);
2523446Smrj         AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
2533446Smrj         AcpiGbl_MutexInfo[MutexId].UseCount = 0;
2543446Smrj     }
2553446Smrj 
2563446Smrj     return_ACPI_STATUS (Status);
2573446Smrj }
2583446Smrj 
2593446Smrj 
2603446Smrj /*******************************************************************************
2613446Smrj  *
2623446Smrj  * FUNCTION:    AcpiUtDeleteMutex
2633446Smrj  *
2643446Smrj  * PARAMETERS:  MutexID         - ID of the mutex to be deleted
2653446Smrj  *
2663446Smrj  * RETURN:      Status
2673446Smrj  *
2683446Smrj  * DESCRIPTION: Delete a mutex object.
2693446Smrj  *
2703446Smrj  ******************************************************************************/
2713446Smrj 
2723446Smrj static ACPI_STATUS
2733446Smrj AcpiUtDeleteMutex (
2743446Smrj     ACPI_MUTEX_HANDLE       MutexId)
2753446Smrj {
2763446Smrj 
2773446Smrj     ACPI_FUNCTION_TRACE_U32 (UtDeleteMutex, MutexId);
2783446Smrj 
2793446Smrj 
2803446Smrj     if (MutexId > ACPI_MAX_MUTEX)
2813446Smrj     {
2823446Smrj         return_ACPI_STATUS (AE_BAD_PARAMETER);
2833446Smrj     }
2843446Smrj 
2853446Smrj     AcpiOsDeleteMutex (AcpiGbl_MutexInfo[MutexId].Mutex);
2863446Smrj 
2873446Smrj     AcpiGbl_MutexInfo[MutexId].Mutex = NULL;
2883446Smrj     AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
2893446Smrj 
2903446Smrj     return_ACPI_STATUS (AE_OK);
2913446Smrj }
2923446Smrj 
2933446Smrj 
2943446Smrj /*******************************************************************************
2953446Smrj  *
2963446Smrj  * FUNCTION:    AcpiUtAcquireMutex
2973446Smrj  *
2983446Smrj  * PARAMETERS:  MutexID         - ID of the mutex to be acquired
2993446Smrj  *
3003446Smrj  * RETURN:      Status
3013446Smrj  *
3023446Smrj  * DESCRIPTION: Acquire a mutex object.
3033446Smrj  *
3043446Smrj  ******************************************************************************/
3053446Smrj 
3063446Smrj ACPI_STATUS
3073446Smrj AcpiUtAcquireMutex (
3083446Smrj     ACPI_MUTEX_HANDLE       MutexId)
3093446Smrj {
3103446Smrj     ACPI_STATUS             Status;
3113446Smrj     ACPI_THREAD_ID          ThisThreadId;
3123446Smrj 
3133446Smrj 
3143446Smrj     ACPI_FUNCTION_NAME (UtAcquireMutex);
3153446Smrj 
3163446Smrj 
3173446Smrj     if (MutexId > ACPI_MAX_MUTEX)
3183446Smrj     {
3193446Smrj         return (AE_BAD_PARAMETER);
3203446Smrj     }
3213446Smrj 
3223446Smrj     ThisThreadId = AcpiOsGetThreadId ();
3233446Smrj 
3243446Smrj #ifdef ACPI_MUTEX_DEBUG
3253446Smrj     {
3263446Smrj         UINT32                  i;
3273446Smrj         /*
3283446Smrj          * Mutex debug code, for internal debugging only.
3293446Smrj          *
3303446Smrj          * Deadlock prevention.  Check if this thread owns any mutexes of value
3313446Smrj          * greater than or equal to this one.  If so, the thread has violated
3323446Smrj          * the mutex ordering rule.  This indicates a coding error somewhere in
3333446Smrj          * the ACPI subsystem code.
3343446Smrj          */
335*7851SDana.Myers@Sun.COM         for (i = MutexId; i < ACPI_NUM_MUTEX; i++)
3363446Smrj         {
3373446Smrj             if (AcpiGbl_MutexInfo[i].ThreadId == ThisThreadId)
3383446Smrj             {
3393446Smrj                 if (i == MutexId)
3403446Smrj                 {
3413446Smrj                     ACPI_ERROR ((AE_INFO,
3423446Smrj                         "Mutex [%s] already acquired by this thread [%X]",
3433446Smrj                         AcpiUtGetMutexName (MutexId), ThisThreadId));
3443446Smrj 
3453446Smrj                     return (AE_ALREADY_ACQUIRED);
3463446Smrj                 }
3473446Smrj 
3483446Smrj                 ACPI_ERROR ((AE_INFO,
3493446Smrj                     "Invalid acquire order: Thread %X owns [%s], wants [%s]",
3503446Smrj                     ThisThreadId, AcpiUtGetMutexName (i),
3513446Smrj                     AcpiUtGetMutexName (MutexId)));
3523446Smrj 
3533446Smrj                 return (AE_ACQUIRE_DEADLOCK);
3543446Smrj             }
3553446Smrj         }
3563446Smrj     }
3573446Smrj #endif
3583446Smrj 
3593446Smrj     ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
3603446Smrj         "Thread %X attempting to acquire Mutex [%s]\n",
3613446Smrj         ThisThreadId, AcpiUtGetMutexName (MutexId)));
3623446Smrj 
3633446Smrj     Status = AcpiOsAcquireMutex (AcpiGbl_MutexInfo[MutexId].Mutex,
3643446Smrj                 ACPI_WAIT_FOREVER);
3653446Smrj     if (ACPI_SUCCESS (Status))
3663446Smrj     {
3673446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %X acquired Mutex [%s]\n",
3683446Smrj             ThisThreadId, AcpiUtGetMutexName (MutexId)));
3693446Smrj 
3703446Smrj         AcpiGbl_MutexInfo[MutexId].UseCount++;
3713446Smrj         AcpiGbl_MutexInfo[MutexId].ThreadId = ThisThreadId;
3723446Smrj     }
3733446Smrj     else
3743446Smrj     {
3753446Smrj         ACPI_EXCEPTION ((AE_INFO, Status,
3763446Smrj             "Thread %X could not acquire Mutex [%X]", ThisThreadId, MutexId));
3773446Smrj     }
3783446Smrj 
3793446Smrj     return (Status);
3803446Smrj }
3813446Smrj 
3823446Smrj 
3833446Smrj /*******************************************************************************
3843446Smrj  *
3853446Smrj  * FUNCTION:    AcpiUtReleaseMutex
3863446Smrj  *
3873446Smrj  * PARAMETERS:  MutexID         - ID of the mutex to be released
3883446Smrj  *
3893446Smrj  * RETURN:      Status
3903446Smrj  *
3913446Smrj  * DESCRIPTION: Release a mutex object.
3923446Smrj  *
3933446Smrj  ******************************************************************************/
3943446Smrj 
3953446Smrj ACPI_STATUS
3963446Smrj AcpiUtReleaseMutex (
3973446Smrj     ACPI_MUTEX_HANDLE       MutexId)
3983446Smrj {
3993446Smrj     ACPI_THREAD_ID          ThisThreadId;
4003446Smrj 
4013446Smrj 
4023446Smrj     ACPI_FUNCTION_NAME (UtReleaseMutex);
4033446Smrj 
4043446Smrj 
4053446Smrj     ThisThreadId = AcpiOsGetThreadId ();
4063446Smrj     ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
4073446Smrj         "Thread %X releasing Mutex [%s]\n", ThisThreadId,
4083446Smrj         AcpiUtGetMutexName (MutexId)));
4093446Smrj 
4103446Smrj     if (MutexId > ACPI_MAX_MUTEX)
4113446Smrj     {
4123446Smrj         return (AE_BAD_PARAMETER);
4133446Smrj     }
4143446Smrj 
4153446Smrj     /*
4163446Smrj      * Mutex must be acquired in order to release it!
4173446Smrj      */
4183446Smrj     if (AcpiGbl_MutexInfo[MutexId].ThreadId == ACPI_MUTEX_NOT_ACQUIRED)
4193446Smrj     {
4203446Smrj         ACPI_ERROR ((AE_INFO,
4213446Smrj             "Mutex [%X] is not acquired, cannot release", MutexId));
4223446Smrj 
4233446Smrj         return (AE_NOT_ACQUIRED);
4243446Smrj     }
4253446Smrj 
4263446Smrj #ifdef ACPI_MUTEX_DEBUG
4273446Smrj     {
4283446Smrj         UINT32                  i;
4293446Smrj         /*
4303446Smrj          * Mutex debug code, for internal debugging only.
4313446Smrj          *
4323446Smrj          * Deadlock prevention.  Check if this thread owns any mutexes of value
4333446Smrj          * greater than this one.  If so, the thread has violated the mutex
4343446Smrj          * ordering rule.  This indicates a coding error somewhere in
4353446Smrj          * the ACPI subsystem code.
4363446Smrj          */
437*7851SDana.Myers@Sun.COM         for (i = MutexId; i < ACPI_NUM_MUTEX; i++)
4383446Smrj         {
4393446Smrj             if (AcpiGbl_MutexInfo[i].ThreadId == ThisThreadId)
4403446Smrj             {
4413446Smrj                 if (i == MutexId)
4423446Smrj                 {
4433446Smrj                     continue;
4443446Smrj                 }
4453446Smrj 
4463446Smrj                 ACPI_ERROR ((AE_INFO,
4473446Smrj                     "Invalid release order: owns [%s], releasing [%s]",
4483446Smrj                     AcpiUtGetMutexName (i), AcpiUtGetMutexName (MutexId)));
4493446Smrj 
4503446Smrj                 return (AE_RELEASE_DEADLOCK);
4513446Smrj             }
4523446Smrj         }
4533446Smrj     }
4543446Smrj #endif
4553446Smrj 
4563446Smrj     /* Mark unlocked FIRST */
4573446Smrj 
4583446Smrj     AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
4593446Smrj 
4603446Smrj     AcpiOsReleaseMutex (AcpiGbl_MutexInfo[MutexId].Mutex);
4613446Smrj     return (AE_OK);
4623446Smrj }
4633446Smrj 
4643446Smrj 
465