2 /******************************************************************************
4 * Module Name: exmutex - ASL Mutex Acquire/Release functions
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/acevents.h>
52 #define _COMPONENT ACPI_EXECUTER
53 ACPI_MODULE_NAME ("exmutex")
55 /* Local prototypes */
59 ACPI_OPERAND_OBJECT *ObjDesc,
60 ACPI_THREAD_STATE *Thread);
63 /*******************************************************************************
65 * FUNCTION: AcpiExUnlinkMutex
67 * PARAMETERS: ObjDesc - The mutex to be unlinked
71 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
73 ******************************************************************************/
77 ACPI_OPERAND_OBJECT *ObjDesc)
79 ACPI_THREAD_STATE *Thread = ObjDesc->Mutex.OwnerThread;
87 /* Doubly linked list */
89 if (ObjDesc->Mutex.Next)
91 (ObjDesc->Mutex.Next)->Mutex.Prev = ObjDesc->Mutex.Prev;
94 if (ObjDesc->Mutex.Prev)
96 (ObjDesc->Mutex.Prev)->Mutex.Next = ObjDesc->Mutex.Next;
99 * Migrate the previous sync level associated with this mutex to
100 * the previous mutex on the list so that it may be preserved.
101 * This handles the case where several mutexes have been acquired
102 * at the same level, but are not released in opposite order.
104 (ObjDesc->Mutex.Prev)->Mutex.OriginalSyncLevel =
105 ObjDesc->Mutex.OriginalSyncLevel;
109 Thread->AcquiredMutexList = ObjDesc->Mutex.Next;
114 /*******************************************************************************
116 * FUNCTION: AcpiExLinkMutex
118 * PARAMETERS: ObjDesc - The mutex to be linked
119 * Thread - Current executing thread object
123 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
125 ******************************************************************************/
129 ACPI_OPERAND_OBJECT *ObjDesc,
130 ACPI_THREAD_STATE *Thread)
132 ACPI_OPERAND_OBJECT *ListHead;
135 ListHead = Thread->AcquiredMutexList;
137 /* This object will be the first object in the list */
139 ObjDesc->Mutex.Prev = NULL;
140 ObjDesc->Mutex.Next = ListHead;
142 /* Update old first object to point back to this object */
146 ListHead->Mutex.Prev = ObjDesc;
149 /* Update list head */
151 Thread->AcquiredMutexList = ObjDesc;
155 /*******************************************************************************
157 * FUNCTION: AcpiExAcquireMutexObject
159 * PARAMETERS: Timeout - Timeout in milliseconds
160 * ObjDesc - Mutex object
161 * ThreadId - Current thread state
165 * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
166 * path that supports multiple acquires by the same thread.
168 * MUTEX: Interpreter must be locked
170 * NOTE: This interface is called from three places:
171 * 1) From AcpiExAcquireMutex, via an AML Acquire() operator
172 * 2) From AcpiExAcquireGlobalLock when an AML Field access requires the
174 * 3) From the external interface, AcpiAcquireGlobalLock
176 ******************************************************************************/
179 AcpiExAcquireMutexObject (
181 ACPI_OPERAND_OBJECT *ObjDesc,
182 ACPI_THREAD_ID ThreadId)
187 ACPI_FUNCTION_TRACE_PTR (ExAcquireMutexObject, ObjDesc);
192 return_ACPI_STATUS (AE_BAD_PARAMETER);
195 /* Support for multiple acquires by the owning thread */
197 if (ObjDesc->Mutex.ThreadId == ThreadId)
200 * The mutex is already owned by this thread, just increment the
203 ObjDesc->Mutex.AcquisitionDepth++;
204 return_ACPI_STATUS (AE_OK);
207 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
209 if (ObjDesc == AcpiGbl_GlobalLockMutex)
211 Status = AcpiEvAcquireGlobalLock (Timeout);
215 Status = AcpiExSystemWaitMutex (ObjDesc->Mutex.OsMutex,
219 if (ACPI_FAILURE (Status))
221 /* Includes failure from a timeout on TimeDesc */
223 return_ACPI_STATUS (Status);
226 /* Acquired the mutex: update mutex object */
228 ObjDesc->Mutex.ThreadId = ThreadId;
229 ObjDesc->Mutex.AcquisitionDepth = 1;
230 ObjDesc->Mutex.OriginalSyncLevel = 0;
231 ObjDesc->Mutex.OwnerThread = NULL; /* Used only for AML Acquire() */
233 return_ACPI_STATUS (AE_OK);
237 /*******************************************************************************
239 * FUNCTION: AcpiExAcquireMutex
241 * PARAMETERS: TimeDesc - Timeout integer
242 * ObjDesc - Mutex object
243 * WalkState - Current method execution state
247 * DESCRIPTION: Acquire an AML mutex
249 ******************************************************************************/
253 ACPI_OPERAND_OBJECT *TimeDesc,
254 ACPI_OPERAND_OBJECT *ObjDesc,
255 ACPI_WALK_STATE *WalkState)
260 ACPI_FUNCTION_TRACE_PTR (ExAcquireMutex, ObjDesc);
265 return_ACPI_STATUS (AE_BAD_PARAMETER);
268 /* Must have a valid thread state struct */
270 if (!WalkState->Thread)
272 ACPI_ERROR ((AE_INFO,
273 "Cannot acquire Mutex [%4.4s], null thread info",
274 AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
275 return_ACPI_STATUS (AE_AML_INTERNAL);
279 * Current sync level must be less than or equal to the sync level of the
280 * mutex. This mechanism provides some deadlock prevention
282 if (WalkState->Thread->CurrentSyncLevel > ObjDesc->Mutex.SyncLevel)
284 ACPI_ERROR ((AE_INFO,
285 "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%u)",
286 AcpiUtGetNodeName (ObjDesc->Mutex.Node),
287 WalkState->Thread->CurrentSyncLevel));
288 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
291 Status = AcpiExAcquireMutexObject ((UINT16) TimeDesc->Integer.Value,
292 ObjDesc, WalkState->Thread->ThreadId);
293 if (ACPI_SUCCESS (Status) && ObjDesc->Mutex.AcquisitionDepth == 1)
295 /* Save Thread object, original/current sync levels */
297 ObjDesc->Mutex.OwnerThread = WalkState->Thread;
298 ObjDesc->Mutex.OriginalSyncLevel = WalkState->Thread->CurrentSyncLevel;
299 WalkState->Thread->CurrentSyncLevel = ObjDesc->Mutex.SyncLevel;
301 /* Link the mutex to the current thread for force-unlock at method exit */
303 AcpiExLinkMutex (ObjDesc, WalkState->Thread);
306 return_ACPI_STATUS (Status);
310 /*******************************************************************************
312 * FUNCTION: AcpiExReleaseMutexObject
314 * PARAMETERS: ObjDesc - The object descriptor for this op
318 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
319 * Provides a common path that supports multiple releases (after
320 * previous multiple acquires) by the same thread.
322 * MUTEX: Interpreter must be locked
324 * NOTE: This interface is called from three places:
325 * 1) From AcpiExReleaseMutex, via an AML Acquire() operator
326 * 2) From AcpiExReleaseGlobalLock when an AML Field access requires the
328 * 3) From the external interface, AcpiReleaseGlobalLock
330 ******************************************************************************/
333 AcpiExReleaseMutexObject (
334 ACPI_OPERAND_OBJECT *ObjDesc)
336 ACPI_STATUS Status = AE_OK;
339 ACPI_FUNCTION_TRACE (ExReleaseMutexObject);
342 if (ObjDesc->Mutex.AcquisitionDepth == 0)
344 return (AE_NOT_ACQUIRED);
347 /* Match multiple Acquires with multiple Releases */
349 ObjDesc->Mutex.AcquisitionDepth--;
350 if (ObjDesc->Mutex.AcquisitionDepth != 0)
352 /* Just decrement the depth and return */
354 return_ACPI_STATUS (AE_OK);
357 if (ObjDesc->Mutex.OwnerThread)
359 /* Unlink the mutex from the owner's list */
361 AcpiExUnlinkMutex (ObjDesc);
362 ObjDesc->Mutex.OwnerThread = NULL;
365 /* Release the mutex, special case for Global Lock */
367 if (ObjDesc == AcpiGbl_GlobalLockMutex)
369 Status = AcpiEvReleaseGlobalLock ();
373 AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
376 /* Clear mutex info */
378 ObjDesc->Mutex.ThreadId = 0;
379 return_ACPI_STATUS (Status);
383 /*******************************************************************************
385 * FUNCTION: AcpiExReleaseMutex
387 * PARAMETERS: ObjDesc - The object descriptor for this op
388 * WalkState - Current method execution state
392 * DESCRIPTION: Release a previously acquired Mutex.
394 ******************************************************************************/
398 ACPI_OPERAND_OBJECT *ObjDesc,
399 ACPI_WALK_STATE *WalkState)
401 ACPI_STATUS Status = AE_OK;
402 UINT8 PreviousSyncLevel;
403 ACPI_THREAD_STATE *OwnerThread;
406 ACPI_FUNCTION_TRACE (ExReleaseMutex);
411 return_ACPI_STATUS (AE_BAD_PARAMETER);
414 OwnerThread = ObjDesc->Mutex.OwnerThread;
416 /* The mutex must have been previously acquired in order to release it */
420 ACPI_ERROR ((AE_INFO,
421 "Cannot release Mutex [%4.4s], not acquired",
422 AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
423 return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED);
426 /* Must have a valid thread ID */
428 if (!WalkState->Thread)
430 ACPI_ERROR ((AE_INFO,
431 "Cannot release Mutex [%4.4s], null thread info",
432 AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
433 return_ACPI_STATUS (AE_AML_INTERNAL);
437 * The Mutex is owned, but this thread must be the owner.
438 * Special case for Global Lock, any thread can release
440 if ((OwnerThread->ThreadId != WalkState->Thread->ThreadId) &&
441 (ObjDesc != AcpiGbl_GlobalLockMutex))
443 ACPI_ERROR ((AE_INFO,
444 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
445 (UINT32) WalkState->Thread->ThreadId,
446 AcpiUtGetNodeName (ObjDesc->Mutex.Node),
447 (UINT32) OwnerThread->ThreadId));
448 return_ACPI_STATUS (AE_AML_NOT_OWNER);
452 * The sync level of the mutex must be equal to the current sync level. In
453 * other words, the current level means that at least one mutex at that
454 * level is currently being held. Attempting to release a mutex of a
455 * different level can only mean that the mutex ordering rule is being
456 * violated. This behavior is clarified in ACPI 4.0 specification.
458 if (ObjDesc->Mutex.SyncLevel != OwnerThread->CurrentSyncLevel)
460 ACPI_ERROR ((AE_INFO,
461 "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %u current %u",
462 AcpiUtGetNodeName (ObjDesc->Mutex.Node),
463 ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel));
464 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
468 * Get the previous SyncLevel from the head of the acquired mutex list.
469 * This handles the case where several mutexes at the same level have been
470 * acquired, but are not released in reverse order.
473 OwnerThread->AcquiredMutexList->Mutex.OriginalSyncLevel;
475 Status = AcpiExReleaseMutexObject (ObjDesc);
476 if (ACPI_FAILURE (Status))
478 return_ACPI_STATUS (Status);
481 if (ObjDesc->Mutex.AcquisitionDepth == 0)
483 /* Restore the previous SyncLevel */
485 OwnerThread->CurrentSyncLevel = PreviousSyncLevel;
488 return_ACPI_STATUS (Status);
492 /*******************************************************************************
494 * FUNCTION: AcpiExReleaseAllMutexes
496 * PARAMETERS: Thread - Current executing thread object
500 * DESCRIPTION: Release all mutexes held by this thread
502 * NOTE: This function is called as the thread is exiting the interpreter.
503 * Mutexes are not released when an individual control method is exited, but
504 * only when the parent thread actually exits the interpreter. This allows one
505 * method to acquire a mutex, and a different method to release it, as long as
506 * this is performed underneath a single parent control method.
508 ******************************************************************************/
511 AcpiExReleaseAllMutexes (
512 ACPI_THREAD_STATE *Thread)
514 ACPI_OPERAND_OBJECT *Next = Thread->AcquiredMutexList;
515 ACPI_OPERAND_OBJECT *ObjDesc;
518 ACPI_FUNCTION_ENTRY ();
521 /* Traverse the list of owned mutexes, releasing each one */
526 Next = ObjDesc->Mutex.Next;
528 ObjDesc->Mutex.Prev = NULL;
529 ObjDesc->Mutex.Next = NULL;
530 ObjDesc->Mutex.AcquisitionDepth = 0;
532 /* Release the mutex, special case for Global Lock */
534 if (ObjDesc == AcpiGbl_GlobalLockMutex)
538 (void) AcpiEvReleaseGlobalLock ();
542 AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
545 /* Mark mutex unowned */
547 ObjDesc->Mutex.OwnerThread = NULL;
548 ObjDesc->Mutex.ThreadId = 0;
550 /* Update Thread SyncLevel (Last mutex is the important one) */
552 Thread->CurrentSyncLevel = ObjDesc->Mutex.OriginalSyncLevel;