1 /*******************************************************************************
3 * Module Name: utmutex - local mutex support
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2015, 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.
47 #define _COMPONENT ACPI_UTILITIES
48 ACPI_MODULE_NAME ("utmutex")
50 /* Local prototypes */
54 ACPI_MUTEX_HANDLE MutexId);
58 ACPI_MUTEX_HANDLE MutexId);
61 /*******************************************************************************
63 * FUNCTION: AcpiUtMutexInitialize
69 * DESCRIPTION: Create the system mutex objects. This includes mutexes,
70 * spin locks, and reader/writer locks.
72 ******************************************************************************/
75 AcpiUtMutexInitialize (
82 ACPI_FUNCTION_TRACE (UtMutexInitialize);
85 /* Create each of the predefined mutex objects */
87 for (i = 0; i < ACPI_NUM_MUTEX; i++)
89 Status = AcpiUtCreateMutex (i);
90 if (ACPI_FAILURE (Status))
92 return_ACPI_STATUS (Status);
96 /* Create the spinlocks for use at interrupt level or for speed */
98 Status = AcpiOsCreateLock (&AcpiGbl_GpeLock);
99 if (ACPI_FAILURE (Status))
101 return_ACPI_STATUS (Status);
104 Status = AcpiOsCreateLock (&AcpiGbl_HardwareLock);
105 if (ACPI_FAILURE (Status))
107 return_ACPI_STATUS (Status);
110 Status = AcpiOsCreateLock (&AcpiGbl_ReferenceCountLock);
111 if (ACPI_FAILURE (Status))
113 return_ACPI_STATUS (Status);
116 /* Mutex for _OSI support */
118 Status = AcpiOsCreateMutex (&AcpiGbl_OsiMutex);
119 if (ACPI_FAILURE (Status))
121 return_ACPI_STATUS (Status);
124 /* Create the reader/writer lock for namespace access */
126 Status = AcpiUtCreateRwLock (&AcpiGbl_NamespaceRwLock);
127 return_ACPI_STATUS (Status);
131 /*******************************************************************************
133 * FUNCTION: AcpiUtMutexTerminate
139 * DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
140 * spin locks, and reader/writer locks.
142 ******************************************************************************/
145 AcpiUtMutexTerminate (
151 ACPI_FUNCTION_TRACE (UtMutexTerminate);
154 /* Delete each predefined mutex object */
156 for (i = 0; i < ACPI_NUM_MUTEX; i++)
158 AcpiUtDeleteMutex (i);
161 AcpiOsDeleteMutex (AcpiGbl_OsiMutex);
163 /* Delete the spinlocks */
165 AcpiOsDeleteLock (AcpiGbl_GpeLock);
166 AcpiOsDeleteLock (AcpiGbl_HardwareLock);
167 AcpiOsDeleteLock (AcpiGbl_ReferenceCountLock);
169 /* Delete the reader/writer lock */
171 AcpiUtDeleteRwLock (&AcpiGbl_NamespaceRwLock);
176 /*******************************************************************************
178 * FUNCTION: AcpiUtCreateMutex
180 * PARAMETERS: MutexID - ID of the mutex to be created
184 * DESCRIPTION: Create a mutex object.
186 ******************************************************************************/
190 ACPI_MUTEX_HANDLE MutexId)
192 ACPI_STATUS Status = AE_OK;
195 ACPI_FUNCTION_TRACE_U32 (UtCreateMutex, MutexId);
198 if (!AcpiGbl_MutexInfo[MutexId].Mutex)
200 Status = AcpiOsCreateMutex (&AcpiGbl_MutexInfo[MutexId].Mutex);
201 AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
202 AcpiGbl_MutexInfo[MutexId].UseCount = 0;
205 return_ACPI_STATUS (Status);
209 /*******************************************************************************
211 * FUNCTION: AcpiUtDeleteMutex
213 * PARAMETERS: MutexID - ID of the mutex to be deleted
217 * DESCRIPTION: Delete a mutex object.
219 ******************************************************************************/
223 ACPI_MUTEX_HANDLE MutexId)
226 ACPI_FUNCTION_TRACE_U32 (UtDeleteMutex, MutexId);
229 AcpiOsDeleteMutex (AcpiGbl_MutexInfo[MutexId].Mutex);
231 AcpiGbl_MutexInfo[MutexId].Mutex = NULL;
232 AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
238 /*******************************************************************************
240 * FUNCTION: AcpiUtAcquireMutex
242 * PARAMETERS: MutexID - ID of the mutex to be acquired
246 * DESCRIPTION: Acquire a mutex object.
248 ******************************************************************************/
252 ACPI_MUTEX_HANDLE MutexId)
255 ACPI_THREAD_ID ThisThreadId;
258 ACPI_FUNCTION_NAME (UtAcquireMutex);
261 if (MutexId > ACPI_MAX_MUTEX)
263 return (AE_BAD_PARAMETER);
266 ThisThreadId = AcpiOsGetThreadId ();
268 #ifdef ACPI_MUTEX_DEBUG
272 * Mutex debug code, for internal debugging only.
274 * Deadlock prevention. Check if this thread owns any mutexes of value
275 * greater than or equal to this one. If so, the thread has violated
276 * the mutex ordering rule. This indicates a coding error somewhere in
277 * the ACPI subsystem code.
279 for (i = MutexId; i < ACPI_NUM_MUTEX; i++)
281 if (AcpiGbl_MutexInfo[i].ThreadId == ThisThreadId)
285 ACPI_ERROR ((AE_INFO,
286 "Mutex [%s] already acquired by this thread [%u]",
287 AcpiUtGetMutexName (MutexId),
288 (UINT32) ThisThreadId));
290 return (AE_ALREADY_ACQUIRED);
293 ACPI_ERROR ((AE_INFO,
294 "Invalid acquire order: Thread %u owns [%s], wants [%s]",
295 (UINT32) ThisThreadId, AcpiUtGetMutexName (i),
296 AcpiUtGetMutexName (MutexId)));
298 return (AE_ACQUIRE_DEADLOCK);
304 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX,
305 "Thread %u attempting to acquire Mutex [%s]\n",
306 (UINT32) ThisThreadId, AcpiUtGetMutexName (MutexId)));
308 Status = AcpiOsAcquireMutex (AcpiGbl_MutexInfo[MutexId].Mutex,
310 if (ACPI_SUCCESS (Status))
312 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %u acquired Mutex [%s]\n",
313 (UINT32) ThisThreadId, AcpiUtGetMutexName (MutexId)));
315 AcpiGbl_MutexInfo[MutexId].UseCount++;
316 AcpiGbl_MutexInfo[MutexId].ThreadId = ThisThreadId;
320 ACPI_EXCEPTION ((AE_INFO, Status,
321 "Thread %u could not acquire Mutex [0x%X]",
322 (UINT32) ThisThreadId, MutexId));
329 /*******************************************************************************
331 * FUNCTION: AcpiUtReleaseMutex
333 * PARAMETERS: MutexID - ID of the mutex to be released
337 * DESCRIPTION: Release a mutex object.
339 ******************************************************************************/
343 ACPI_MUTEX_HANDLE MutexId)
345 ACPI_FUNCTION_NAME (UtReleaseMutex);
348 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX, "Thread %u releasing Mutex [%s]\n",
349 (UINT32) AcpiOsGetThreadId (), AcpiUtGetMutexName (MutexId)));
351 if (MutexId > ACPI_MAX_MUTEX)
353 return (AE_BAD_PARAMETER);
357 * Mutex must be acquired in order to release it!
359 if (AcpiGbl_MutexInfo[MutexId].ThreadId == ACPI_MUTEX_NOT_ACQUIRED)
361 ACPI_ERROR ((AE_INFO,
362 "Mutex [0x%X] is not acquired, cannot release", MutexId));
364 return (AE_NOT_ACQUIRED);
367 #ifdef ACPI_MUTEX_DEBUG
371 * Mutex debug code, for internal debugging only.
373 * Deadlock prevention. Check if this thread owns any mutexes of value
374 * greater than this one. If so, the thread has violated the mutex
375 * ordering rule. This indicates a coding error somewhere in
376 * the ACPI subsystem code.
378 for (i = MutexId; i < ACPI_NUM_MUTEX; i++)
380 if (AcpiGbl_MutexInfo[i].ThreadId == AcpiOsGetThreadId ())
387 ACPI_ERROR ((AE_INFO,
388 "Invalid release order: owns [%s], releasing [%s]",
389 AcpiUtGetMutexName (i), AcpiUtGetMutexName (MutexId)));
391 return (AE_RELEASE_DEADLOCK);
397 /* Mark unlocked FIRST */
399 AcpiGbl_MutexInfo[MutexId].ThreadId = ACPI_MUTEX_NOT_ACQUIRED;
401 AcpiOsReleaseMutex (AcpiGbl_MutexInfo[MutexId].Mutex);