1 /*******************************************************************************
3 * Module Name: utmutex - local mutex support
5 ******************************************************************************/
8 * Copyright (C) 2000 - 2016, 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 if (ACPI_FAILURE (Status
))
129 return_ACPI_STATUS (Status
);
132 return_ACPI_STATUS (Status
);
136 /*******************************************************************************
138 * FUNCTION: AcpiUtMutexTerminate
144 * DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
145 * spin locks, and reader/writer locks.
147 ******************************************************************************/
150 AcpiUtMutexTerminate (
156 ACPI_FUNCTION_TRACE (UtMutexTerminate
);
159 /* Delete each predefined mutex object */
161 for (i
= 0; i
< ACPI_NUM_MUTEX
; i
++)
163 AcpiUtDeleteMutex (i
);
166 AcpiOsDeleteMutex (AcpiGbl_OsiMutex
);
168 /* Delete the spinlocks */
170 AcpiOsDeleteLock (AcpiGbl_GpeLock
);
171 AcpiOsDeleteLock (AcpiGbl_HardwareLock
);
172 AcpiOsDeleteLock (AcpiGbl_ReferenceCountLock
);
174 /* Delete the reader/writer lock */
176 AcpiUtDeleteRwLock (&AcpiGbl_NamespaceRwLock
);
181 /*******************************************************************************
183 * FUNCTION: AcpiUtCreateMutex
185 * PARAMETERS: MutexID - ID of the mutex to be created
189 * DESCRIPTION: Create a mutex object.
191 ******************************************************************************/
195 ACPI_MUTEX_HANDLE MutexId
)
197 ACPI_STATUS Status
= AE_OK
;
200 ACPI_FUNCTION_TRACE_U32 (UtCreateMutex
, MutexId
);
203 if (!AcpiGbl_MutexInfo
[MutexId
].Mutex
)
205 Status
= AcpiOsCreateMutex (&AcpiGbl_MutexInfo
[MutexId
].Mutex
);
206 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
207 AcpiGbl_MutexInfo
[MutexId
].UseCount
= 0;
210 return_ACPI_STATUS (Status
);
214 /*******************************************************************************
216 * FUNCTION: AcpiUtDeleteMutex
218 * PARAMETERS: MutexID - ID of the mutex to be deleted
222 * DESCRIPTION: Delete a mutex object.
224 ******************************************************************************/
228 ACPI_MUTEX_HANDLE MutexId
)
231 ACPI_FUNCTION_TRACE_U32 (UtDeleteMutex
, MutexId
);
234 AcpiOsDeleteMutex (AcpiGbl_MutexInfo
[MutexId
].Mutex
);
236 AcpiGbl_MutexInfo
[MutexId
].Mutex
= NULL
;
237 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
243 /*******************************************************************************
245 * FUNCTION: AcpiUtAcquireMutex
247 * PARAMETERS: MutexID - ID of the mutex to be acquired
251 * DESCRIPTION: Acquire a mutex object.
253 ******************************************************************************/
257 ACPI_MUTEX_HANDLE MutexId
)
260 ACPI_THREAD_ID ThisThreadId
;
263 ACPI_FUNCTION_NAME (UtAcquireMutex
);
266 if (MutexId
> ACPI_MAX_MUTEX
)
268 return (AE_BAD_PARAMETER
);
271 ThisThreadId
= AcpiOsGetThreadId ();
273 #ifdef ACPI_MUTEX_DEBUG
277 * Mutex debug code, for internal debugging only.
279 * Deadlock prevention. Check if this thread owns any mutexes of value
280 * greater than or equal to this one. If so, the thread has violated
281 * the mutex ordering rule. This indicates a coding error somewhere in
282 * the ACPI subsystem code.
284 for (i
= MutexId
; i
< ACPI_NUM_MUTEX
; i
++)
286 if (AcpiGbl_MutexInfo
[i
].ThreadId
== ThisThreadId
)
290 ACPI_ERROR ((AE_INFO
,
291 "Mutex [%s] already acquired by this thread [%u]",
292 AcpiUtGetMutexName (MutexId
),
293 (UINT32
) ThisThreadId
));
295 return (AE_ALREADY_ACQUIRED
);
298 ACPI_ERROR ((AE_INFO
,
299 "Invalid acquire order: Thread %u owns [%s], wants [%s]",
300 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (i
),
301 AcpiUtGetMutexName (MutexId
)));
303 return (AE_ACQUIRE_DEADLOCK
);
309 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
,
310 "Thread %u attempting to acquire Mutex [%s]\n",
311 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (MutexId
)));
313 Status
= AcpiOsAcquireMutex (
314 AcpiGbl_MutexInfo
[MutexId
].Mutex
, ACPI_WAIT_FOREVER
);
315 if (ACPI_SUCCESS (Status
))
317 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
,
318 "Thread %u acquired Mutex [%s]\n",
319 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (MutexId
)));
321 AcpiGbl_MutexInfo
[MutexId
].UseCount
++;
322 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ThisThreadId
;
326 ACPI_EXCEPTION ((AE_INFO
, Status
,
327 "Thread %u could not acquire Mutex [0x%X]",
328 (UINT32
) ThisThreadId
, MutexId
));
335 /*******************************************************************************
337 * FUNCTION: AcpiUtReleaseMutex
339 * PARAMETERS: MutexID - ID of the mutex to be released
343 * DESCRIPTION: Release a mutex object.
345 ******************************************************************************/
349 ACPI_MUTEX_HANDLE MutexId
)
351 ACPI_FUNCTION_NAME (UtReleaseMutex
);
354 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Thread %u releasing Mutex [%s]\n",
355 (UINT32
) AcpiOsGetThreadId (), AcpiUtGetMutexName (MutexId
)));
357 if (MutexId
> ACPI_MAX_MUTEX
)
359 return (AE_BAD_PARAMETER
);
363 * Mutex must be acquired in order to release it!
365 if (AcpiGbl_MutexInfo
[MutexId
].ThreadId
== ACPI_MUTEX_NOT_ACQUIRED
)
367 ACPI_ERROR ((AE_INFO
,
368 "Mutex [0x%X] is not acquired, cannot release", MutexId
));
370 return (AE_NOT_ACQUIRED
);
373 #ifdef ACPI_MUTEX_DEBUG
377 * Mutex debug code, for internal debugging only.
379 * Deadlock prevention. Check if this thread owns any mutexes of value
380 * greater than this one. If so, the thread has violated the mutex
381 * ordering rule. This indicates a coding error somewhere in
382 * the ACPI subsystem code.
384 for (i
= MutexId
; i
< ACPI_NUM_MUTEX
; i
++)
386 if (AcpiGbl_MutexInfo
[i
].ThreadId
== AcpiOsGetThreadId ())
393 ACPI_ERROR ((AE_INFO
,
394 "Invalid release order: owns [%s], releasing [%s]",
395 AcpiUtGetMutexName (i
), AcpiUtGetMutexName (MutexId
)));
397 return (AE_RELEASE_DEADLOCK
);
403 /* Mark unlocked FIRST */
405 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
407 AcpiOsReleaseMutex (AcpiGbl_MutexInfo
[MutexId
].Mutex
);