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
);
134 /* Debugger Support */
136 Status
= AcpiOsCreateMutex (&AcpiGbl_DbCommandReady
);
137 if (ACPI_FAILURE (Status
))
139 return_ACPI_STATUS (Status
);
142 Status
= AcpiOsCreateMutex (&AcpiGbl_DbCommandComplete
);
145 return_ACPI_STATUS (Status
);
149 /*******************************************************************************
151 * FUNCTION: AcpiUtMutexTerminate
157 * DESCRIPTION: Delete all of the system mutex objects. This includes mutexes,
158 * spin locks, and reader/writer locks.
160 ******************************************************************************/
163 AcpiUtMutexTerminate (
169 ACPI_FUNCTION_TRACE (UtMutexTerminate
);
172 /* Delete each predefined mutex object */
174 for (i
= 0; i
< ACPI_NUM_MUTEX
; i
++)
176 AcpiUtDeleteMutex (i
);
179 AcpiOsDeleteMutex (AcpiGbl_OsiMutex
);
181 /* Delete the spinlocks */
183 AcpiOsDeleteLock (AcpiGbl_GpeLock
);
184 AcpiOsDeleteLock (AcpiGbl_HardwareLock
);
185 AcpiOsDeleteLock (AcpiGbl_ReferenceCountLock
);
187 /* Delete the reader/writer lock */
189 AcpiUtDeleteRwLock (&AcpiGbl_NamespaceRwLock
);
192 AcpiOsDeleteMutex (AcpiGbl_DbCommandReady
);
193 AcpiOsDeleteMutex (AcpiGbl_DbCommandComplete
);
200 /*******************************************************************************
202 * FUNCTION: AcpiUtCreateMutex
204 * PARAMETERS: MutexID - ID of the mutex to be created
208 * DESCRIPTION: Create a mutex object.
210 ******************************************************************************/
214 ACPI_MUTEX_HANDLE MutexId
)
216 ACPI_STATUS Status
= AE_OK
;
219 ACPI_FUNCTION_TRACE_U32 (UtCreateMutex
, MutexId
);
222 if (!AcpiGbl_MutexInfo
[MutexId
].Mutex
)
224 Status
= AcpiOsCreateMutex (&AcpiGbl_MutexInfo
[MutexId
].Mutex
);
225 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
226 AcpiGbl_MutexInfo
[MutexId
].UseCount
= 0;
229 return_ACPI_STATUS (Status
);
233 /*******************************************************************************
235 * FUNCTION: AcpiUtDeleteMutex
237 * PARAMETERS: MutexID - ID of the mutex to be deleted
241 * DESCRIPTION: Delete a mutex object.
243 ******************************************************************************/
247 ACPI_MUTEX_HANDLE MutexId
)
250 ACPI_FUNCTION_TRACE_U32 (UtDeleteMutex
, MutexId
);
253 AcpiOsDeleteMutex (AcpiGbl_MutexInfo
[MutexId
].Mutex
);
255 AcpiGbl_MutexInfo
[MutexId
].Mutex
= NULL
;
256 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
262 /*******************************************************************************
264 * FUNCTION: AcpiUtAcquireMutex
266 * PARAMETERS: MutexID - ID of the mutex to be acquired
270 * DESCRIPTION: Acquire a mutex object.
272 ******************************************************************************/
276 ACPI_MUTEX_HANDLE MutexId
)
279 ACPI_THREAD_ID ThisThreadId
;
282 ACPI_FUNCTION_NAME (UtAcquireMutex
);
285 if (MutexId
> ACPI_MAX_MUTEX
)
287 return (AE_BAD_PARAMETER
);
290 ThisThreadId
= AcpiOsGetThreadId ();
292 #ifdef ACPI_MUTEX_DEBUG
296 * Mutex debug code, for internal debugging only.
298 * Deadlock prevention. Check if this thread owns any mutexes of value
299 * greater than or equal to this one. If so, the thread has violated
300 * the mutex ordering rule. This indicates a coding error somewhere in
301 * the ACPI subsystem code.
303 for (i
= MutexId
; i
< ACPI_NUM_MUTEX
; i
++)
305 if (AcpiGbl_MutexInfo
[i
].ThreadId
== ThisThreadId
)
309 ACPI_ERROR ((AE_INFO
,
310 "Mutex [%s] already acquired by this thread [%u]",
311 AcpiUtGetMutexName (MutexId
),
312 (UINT32
) ThisThreadId
));
314 return (AE_ALREADY_ACQUIRED
);
317 ACPI_ERROR ((AE_INFO
,
318 "Invalid acquire order: Thread %u owns [%s], wants [%s]",
319 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (i
),
320 AcpiUtGetMutexName (MutexId
)));
322 return (AE_ACQUIRE_DEADLOCK
);
328 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
,
329 "Thread %u attempting to acquire Mutex [%s]\n",
330 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (MutexId
)));
332 Status
= AcpiOsAcquireMutex (
333 AcpiGbl_MutexInfo
[MutexId
].Mutex
, ACPI_WAIT_FOREVER
);
334 if (ACPI_SUCCESS (Status
))
336 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
,
337 "Thread %u acquired Mutex [%s]\n",
338 (UINT32
) ThisThreadId
, AcpiUtGetMutexName (MutexId
)));
340 AcpiGbl_MutexInfo
[MutexId
].UseCount
++;
341 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ThisThreadId
;
345 ACPI_EXCEPTION ((AE_INFO
, Status
,
346 "Thread %u could not acquire Mutex [0x%X]",
347 (UINT32
) ThisThreadId
, MutexId
));
354 /*******************************************************************************
356 * FUNCTION: AcpiUtReleaseMutex
358 * PARAMETERS: MutexID - ID of the mutex to be released
362 * DESCRIPTION: Release a mutex object.
364 ******************************************************************************/
368 ACPI_MUTEX_HANDLE MutexId
)
370 ACPI_FUNCTION_NAME (UtReleaseMutex
);
373 ACPI_DEBUG_PRINT ((ACPI_DB_MUTEX
, "Thread %u releasing Mutex [%s]\n",
374 (UINT32
) AcpiOsGetThreadId (), AcpiUtGetMutexName (MutexId
)));
376 if (MutexId
> ACPI_MAX_MUTEX
)
378 return (AE_BAD_PARAMETER
);
382 * Mutex must be acquired in order to release it!
384 if (AcpiGbl_MutexInfo
[MutexId
].ThreadId
== ACPI_MUTEX_NOT_ACQUIRED
)
386 ACPI_ERROR ((AE_INFO
,
387 "Mutex [0x%X] is not acquired, cannot release", MutexId
));
389 return (AE_NOT_ACQUIRED
);
392 #ifdef ACPI_MUTEX_DEBUG
396 * Mutex debug code, for internal debugging only.
398 * Deadlock prevention. Check if this thread owns any mutexes of value
399 * greater than this one. If so, the thread has violated the mutex
400 * ordering rule. This indicates a coding error somewhere in
401 * the ACPI subsystem code.
403 for (i
= MutexId
; i
< ACPI_NUM_MUTEX
; i
++)
405 if (AcpiGbl_MutexInfo
[i
].ThreadId
== AcpiOsGetThreadId ())
412 ACPI_ERROR ((AE_INFO
,
413 "Invalid release order: owns [%s], releasing [%s]",
414 AcpiUtGetMutexName (i
), AcpiUtGetMutexName (MutexId
)));
416 return (AE_RELEASE_DEADLOCK
);
422 /* Mark unlocked FIRST */
424 AcpiGbl_MutexInfo
[MutexId
].ThreadId
= ACPI_MUTEX_NOT_ACQUIRED
;
426 AcpiOsReleaseMutex (AcpiGbl_MutexInfo
[MutexId
].Mutex
);