1 /*******************************************************************************
3 * Module Name: utmutex - local mutex support
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2014, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
49 #define _COMPONENT ACPI_UTILITIES
50 ACPI_MODULE_NAME ("utmutex")
52 /* Local prototypes */
56 ACPI_MUTEX_HANDLE MutexId);
60 ACPI_MUTEX_HANDLE MutexId);
63 /*******************************************************************************
65 * FUNCTION: AcpiUtMutexInitialize
71 * DESCRIPTION: Create the system mutex objects. This includes mutexes,
72 * spin locks, and reader/writer locks.
74 ******************************************************************************/
77 AcpiUtMutexInitialize (
84 ACPI_FUNCTION_TRACE (UtMutexInitialize);
87 /* Create each of the predefined mutex objects */
89 for (i = 0; i < ACPI_NUM_MUTEX; i++)
91 Status = AcpiUtCreateMutex (i);
92 if (ACPI_FAILURE (Status))
94 return_ACPI_STATUS (Status);
98 /* Create the spinlocks for use at interrupt level or for speed */
100 Status = AcpiOsCreateLock (&AcpiGbl_GpeLock);
101 if (ACPI_FAILURE (Status))
103 return_ACPI_STATUS (Status);
106 Status = AcpiOsCreateLock (&AcpiGbl_HardwareLock);
107 if (ACPI_FAILURE (Status))
109 return_ACPI_STATUS (Status);
112 Status = AcpiOsCreateLock (&AcpiGbl_ReferenceCountLock);
113 if (ACPI_FAILURE (Status))
115 return_ACPI_STATUS (Status);
118 /* Mutex for _OSI support */
120 Status = AcpiOsCreateMutex (&AcpiGbl_OsiMutex);
121 if (ACPI_FAILURE (Status))
123 return_ACPI_STATUS (Status);
126 /* Create the reader/writer lock for namespace access */
128 Status = AcpiUtCreateRwLock (&AcpiGbl_NamespaceRwLock);
129 return_ACPI_STATUS (Status);
133 /*******************************************************************************
135 * FUNCTION: AcpiUtMutexTerminate
141 * DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
142 * spin locks, and reader/writer locks.
144 ******************************************************************************/
147 AcpiUtMutexTerminate (
153 ACPI_FUNCTION_TRACE (UtMutexTerminate);
156 /* Delete each predefined mutex object */
158 for (i = 0; i < ACPI_NUM_MUTEX; i++)
160 AcpiUtDeleteMutex (i);
163 AcpiOsDeleteMutex (AcpiGbl_OsiMutex);
165 /* Delete the spinlocks */
167 AcpiOsDeleteLock (AcpiGbl_GpeLock);
168 AcpiOsDeleteLock (AcpiGbl_HardwareLock);
169 AcpiOsDeleteLock (AcpiGbl_ReferenceCountLock);
171 /* Delete the reader/writer lock */
173 AcpiUtDeleteRwLock (&AcpiGbl_NamespaceRwLock);
178 /*******************************************************************************
180 * FUNCTION: AcpiUtCreateMutex
182 * PARAMETERS: MutexID - ID of the mutex to be created
186 * DESCRIPTION: Create a mutex object.
188 ******************************************************************************/
192 ACPI_MUTEX_HANDLE MutexId)
194 ACPI_STATUS Status = AE_OK;
197 ACPI_FUNCTION_TRACE_U32 (UtCreateMutex, MutexId);
200 if (!AcpiGbl_MutexInfo[MutexId].Mutex)
202 Status = AcpiOsCreateMutex (&AcpiGbl_MutexInfo[MutexId].Mutex);
203 AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
204 AcpiGbl_MutexInfo[MutexId].UseCount = 0;
207 return_ACPI_STATUS (Status);
211 /*******************************************************************************
213 * FUNCTION: AcpiUtDeleteMutex
215 * PARAMETERS: MutexID - ID of the mutex to be deleted
219 * DESCRIPTION: Delete a mutex object.
221 ******************************************************************************/
225 ACPI_MUTEX_HANDLE MutexId)
228 ACPI_FUNCTION_TRACE_U32 (UtDeleteMutex, MutexId);
231 AcpiOsDeleteMutex (AcpiGbl_MutexInfo[MutexId].Mutex);
233 AcpiGbl_MutexInfo[MutexId].Mutex = NULL;
234 AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
240 /*******************************************************************************
242 * FUNCTION: AcpiUtAcquireMutex
244 * PARAMETERS: MutexID - ID of the mutex to be acquired
248 * DESCRIPTION: Acquire a mutex object.
250 ******************************************************************************/
254 ACPI_MUTEX_HANDLE MutexId)
257 ACPI_THREAD_ID ThisThreadId;
260 ACPI_FUNCTION_NAME (UtAcquireMutex);
263 if (MutexId > ACPI_MAX_MUTEX)
265 return (AE_BAD_PARAMETER);
268 ThisThreadId = AcpiOsGetThreadId ();
270 #ifdef ACPI_MUTEX_DEBUG
274 * Mutex debug code, for internal debugging only.
276 * Deadlock prevention. Check if this thread owns any mutexes of value
277 * greater than or equal to this one. If so, the thread has violated
278 * the mutex ordering rule. This indicates a coding error somewhere in
279 * the ACPI subsystem code.
281 for (i = MutexId; i < ACPI_NUM_MUTEX; i++)
283 if (AcpiGbl_MutexInfo[i].ThreadId == ThisThreadId)
287 ACPI_ERROR ((AE_INFO,
288 "Mutex [%s] already acquired by this thread [%u]",
289 AcpiUtGetMutexName (MutexId),
290 (UINT32) ThisThreadId));
292 return (AE_ALREADY_ACQUIRED);
295 ACPI_ERROR ((AE_INFO,
296 "Invalid acquire order: Thread %u owns [%s], wants [%s]",
297 (UINT32) ThisThreadId, AcpiUtGetMutexName (i),
298 AcpiUtGetMutexName (MutexId)));
300 return (AE_ACQUIRE_DEADLOCK);
306 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
307 "Thread %u attempting to acquire Mutex [%s]\n",
308 (UINT32) ThisThreadId, AcpiUtGetMutexName (MutexId)));
310 Status = AcpiOsAcquireMutex (AcpiGbl_MutexInfo[MutexId].Mutex,
312 if (ACPI_SUCCESS (Status))
314 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %u acquired Mutex [%s]\n",
315 (UINT32) ThisThreadId, AcpiUtGetMutexName (MutexId)));
317 AcpiGbl_MutexInfo[MutexId].UseCount++;
318 AcpiGbl_MutexInfo[MutexId].ThreadId = ThisThreadId;
322 ACPI_EXCEPTION ((AE_INFO, Status,
323 "Thread %u could not acquire Mutex [0x%X]",
324 (UINT32) ThisThreadId, MutexId));
331 /*******************************************************************************
333 * FUNCTION: AcpiUtReleaseMutex
335 * PARAMETERS: MutexID - ID of the mutex to be released
339 * DESCRIPTION: Release a mutex object.
341 ******************************************************************************/
345 ACPI_MUTEX_HANDLE MutexId)
347 ACPI_FUNCTION_NAME (UtReleaseMutex);
350 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %u releasing Mutex [%s]\n",
351 (UINT32) AcpiOsGetThreadId (), AcpiUtGetMutexName (MutexId)));
353 if (MutexId > ACPI_MAX_MUTEX)
355 return (AE_BAD_PARAMETER);
359 * Mutex must be acquired in order to release it!
361 if (AcpiGbl_MutexInfo[MutexId].ThreadId == ACPI_MUTEX_NOT_ACQUIRED)
363 ACPI_ERROR ((AE_INFO,
364 "Mutex [0x%X] is not acquired, cannot release", MutexId));
366 return (AE_NOT_ACQUIRED);
369 #ifdef ACPI_MUTEX_DEBUG
373 * Mutex debug code, for internal debugging only.
375 * Deadlock prevention. Check if this thread owns any mutexes of value
376 * greater than this one. If so, the thread has violated the mutex
377 * ordering rule. This indicates a coding error somewhere in
378 * the ACPI subsystem code.
380 for (i = MutexId; i < ACPI_NUM_MUTEX; i++)
382 if (AcpiGbl_MutexInfo[i].ThreadId == AcpiOsGetThreadId ())
389 ACPI_ERROR ((AE_INFO,
390 "Invalid release order: owns [%s], releasing [%s]",
391 AcpiUtGetMutexName (i), AcpiUtGetMutexName (MutexId)));
393 return (AE_RELEASE_DEADLOCK);
399 /* Mark unlocked FIRST */
401 AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
403 AcpiOsReleaseMutex (AcpiGbl_MutexInfo[MutexId].Mutex);