1 /*******************************************************************************
3 * Module Name: utmutex - local mutex support
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2013, 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.
50 #define _COMPONENT ACPI_UTILITIES
51 ACPI_MODULE_NAME ("utmutex")
53 /* Local prototypes */
57 ACPI_MUTEX_HANDLE MutexId
);
61 ACPI_MUTEX_HANDLE MutexId
);
64 /*******************************************************************************
66 * FUNCTION: AcpiUtMutexInitialize
72 * DESCRIPTION: Create the system mutex objects. This includes mutexes,
73 * spin locks, and reader/writer locks.
75 ******************************************************************************/
78 AcpiUtMutexInitialize (
85 ACPI_FUNCTION_TRACE (UtMutexInitialize
);
88 /* Create each of the predefined mutex objects */
90 for (i
= 0; i
< ACPI_NUM_MUTEX
; i
++)
92 Status
= AcpiUtCreateMutex (i
);
93 if (ACPI_FAILURE (Status
))
95 return_ACPI_STATUS (Status
);
99 /* Create the spinlocks for use at interrupt level or for speed */
101 Status
= AcpiOsCreateLock (&AcpiGbl_GpeLock
);
102 if (ACPI_FAILURE (Status
))
104 return_ACPI_STATUS (Status
);
107 Status
= AcpiOsCreateLock (&AcpiGbl_HardwareLock
);
108 if (ACPI_FAILURE (Status
))
110 return_ACPI_STATUS (Status
);
113 Status
= AcpiOsCreateLock (&AcpiGbl_ReferenceCountLock
);
114 if (ACPI_FAILURE (Status
))
116 return_ACPI_STATUS (Status
);
119 /* Mutex for _OSI support */
121 Status
= AcpiOsCreateMutex (&AcpiGbl_OsiMutex
);
122 if (ACPI_FAILURE (Status
))
124 return_ACPI_STATUS (Status
);
127 /* Create the reader/writer lock for namespace access */
129 Status
= AcpiUtCreateRwLock (&AcpiGbl_NamespaceRwLock
);
130 return_ACPI_STATUS (Status
);
134 /*******************************************************************************
136 * FUNCTION: AcpiUtMutexTerminate
142 * DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
143 * spin locks, and reader/writer locks.
145 ******************************************************************************/
148 AcpiUtMutexTerminate (
154 ACPI_FUNCTION_TRACE (UtMutexTerminate
);
157 /* Delete each predefined mutex object */
159 for (i
= 0; i
< ACPI_NUM_MUTEX
; i
++)
161 AcpiUtDeleteMutex (i
);
164 AcpiOsDeleteMutex (AcpiGbl_OsiMutex
);
166 /* Delete the spinlocks */
168 AcpiOsDeleteLock (AcpiGbl_GpeLock
);
169 AcpiOsDeleteLock (AcpiGbl_HardwareLock
);
170 AcpiOsDeleteLock (AcpiGbl_ReferenceCountLock
);
172 /* Delete the reader/writer lock */
174 AcpiUtDeleteRwLock (&AcpiGbl_NamespaceRwLock
);
179 /*******************************************************************************
181 * FUNCTION: AcpiUtCreateMutex
183 * PARAMETERS: MutexID - ID of the mutex to be created
187 * DESCRIPTION: Create a mutex object.
189 ******************************************************************************/
193 ACPI_MUTEX_HANDLE MutexId
)
195 ACPI_STATUS Status
= AE_OK
;
198 ACPI_FUNCTION_TRACE_U32 (UtCreateMutex
, MutexId
);
201 if (!AcpiGbl_MutexInfo
[MutexId
].Mutex
)
203 Status
= AcpiOsCreateMutex (&AcpiGbl_MutexInfo
[MutexId
].Mutex
);
204 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
205 AcpiGbl_MutexInfo
[MutexId
].UseCount
= 0;
208 return_ACPI_STATUS (Status
);
212 /*******************************************************************************
214 * FUNCTION: AcpiUtDeleteMutex
216 * PARAMETERS: MutexID - ID of the mutex to be deleted
220 * DESCRIPTION: Delete a mutex object.
222 ******************************************************************************/
226 ACPI_MUTEX_HANDLE MutexId
)
229 ACPI_FUNCTION_TRACE_U32 (UtDeleteMutex
, MutexId
);
232 AcpiOsDeleteMutex (AcpiGbl_MutexInfo
[MutexId
].Mutex
);
234 AcpiGbl_MutexInfo
[MutexId
].Mutex
= NULL
;
235 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
241 /*******************************************************************************
243 * FUNCTION: AcpiUtAcquireMutex
245 * PARAMETERS: MutexID - ID of the mutex to be acquired
249 * DESCRIPTION: Acquire a mutex object.
251 ******************************************************************************/
255 ACPI_MUTEX_HANDLE MutexId
)
258 ACPI_THREAD_ID ThisThreadId
;
261 ACPI_FUNCTION_NAME (UtAcquireMutex
);
264 if (MutexId
> ACPI_MAX_MUTEX
)
266 return (AE_BAD_PARAMETER
);
269 ThisThreadId
= AcpiOsGetThreadId ();
271 #ifdef ACPI_MUTEX_DEBUG
275 * Mutex debug code, for internal debugging only.
277 * Deadlock prevention. Check if this thread owns any mutexes of value
278 * greater than or equal to this one. If so, the thread has violated
279 * the mutex ordering rule. This indicates a coding error somewhere in
280 * the ACPI subsystem code.
282 for (i
= MutexId
; i
< ACPI_NUM_MUTEX
; i
++)
284 if (AcpiGbl_MutexInfo
[i
].ThreadId
== ThisThreadId
)
288 ACPI_ERROR ((AE_INFO
,
289 "Mutex [%s] already acquired by this thread [%u]",
290 AcpiUtGetMutexName (MutexId
),
291 (UINT32
) ThisThreadId
));
293 return (AE_ALREADY_ACQUIRED
);
296 ACPI_ERROR ((AE_INFO
,
297 "Invalid acquire order: Thread %u owns [%s], wants [%s]",
298 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (i
),
299 AcpiUtGetMutexName (MutexId
)));
301 return (AE_ACQUIRE_DEADLOCK
);
307 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
,
308 "Thread %u attempting to acquire Mutex [%s]\n",
309 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (MutexId
)));
311 Status
= AcpiOsAcquireMutex (AcpiGbl_MutexInfo
[MutexId
].Mutex
,
313 if (ACPI_SUCCESS (Status
))
315 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Thread %u acquired Mutex [%s]\n",
316 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (MutexId
)));
318 AcpiGbl_MutexInfo
[MutexId
].UseCount
++;
319 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ThisThreadId
;
323 ACPI_EXCEPTION ((AE_INFO
, Status
,
324 "Thread %u could not acquire Mutex [0x%X]",
325 (UINT32
) ThisThreadId
, MutexId
));
332 /*******************************************************************************
334 * FUNCTION: AcpiUtReleaseMutex
336 * PARAMETERS: MutexID - ID of the mutex to be released
340 * DESCRIPTION: Release a mutex object.
342 ******************************************************************************/
346 ACPI_MUTEX_HANDLE MutexId
)
348 ACPI_FUNCTION_NAME (UtReleaseMutex
);
351 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Thread %u releasing Mutex [%s]\n",
352 (UINT32
) AcpiOsGetThreadId (), AcpiUtGetMutexName (MutexId
)));
354 if (MutexId
> ACPI_MAX_MUTEX
)
356 return (AE_BAD_PARAMETER
);
360 * Mutex must be acquired in order to release it!
362 if (AcpiGbl_MutexInfo
[MutexId
].ThreadId
== ACPI_MUTEX_NOT_ACQUIRED
)
364 ACPI_ERROR ((AE_INFO
,
365 "Mutex [0x%X] is not acquired, cannot release", MutexId
));
367 return (AE_NOT_ACQUIRED
);
370 #ifdef ACPI_MUTEX_DEBUG
374 * Mutex debug code, for internal debugging only.
376 * Deadlock prevention. Check if this thread owns any mutexes of value
377 * greater than this one. If so, the thread has violated the mutex
378 * ordering rule. This indicates a coding error somewhere in
379 * the ACPI subsystem code.
381 for (i
= MutexId
; i
< ACPI_NUM_MUTEX
; i
++)
383 if (AcpiGbl_MutexInfo
[i
].ThreadId
== AcpiOsGetThreadId ())
390 ACPI_ERROR ((AE_INFO
,
391 "Invalid release order: owns [%s], releasing [%s]",
392 AcpiUtGetMutexName (i
), AcpiUtGetMutexName (MutexId
)));
394 return (AE_RELEASE_DEADLOCK
);
400 /* Mark unlocked FIRST */
402 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
404 AcpiOsReleaseMutex (AcpiGbl_MutexInfo
[MutexId
].Mutex
);