1 /******************************************************************************
3 * Module Name: exmutex - ASL Mutex Acquire/Release functions
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.
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME ("exmutex")
52 /* Local prototypes */
56 ACPI_OPERAND_OBJECT *ObjDesc,
57 ACPI_THREAD_STATE *Thread);
60 /*******************************************************************************
62 * FUNCTION: AcpiExUnlinkMutex
64 * PARAMETERS: ObjDesc - The mutex to be unlinked
68 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
70 ******************************************************************************/
74 ACPI_OPERAND_OBJECT *ObjDesc)
76 ACPI_THREAD_STATE *Thread = ObjDesc->Mutex.OwnerThread;
84 /* Doubly linked list */
86 if (ObjDesc->Mutex.Next)
88 (ObjDesc->Mutex.Next)->Mutex.Prev = ObjDesc->Mutex.Prev;
91 if (ObjDesc->Mutex.Prev)
93 (ObjDesc->Mutex.Prev)->Mutex.Next = ObjDesc->Mutex.Next;
96 * Migrate the previous sync level associated with this mutex to
97 * the previous mutex on the list so that it may be preserved.
98 * This handles the case where several mutexes have been acquired
99 * at the same level, but are not released in opposite order.
101 (ObjDesc->Mutex.Prev)->Mutex.OriginalSyncLevel =
102 ObjDesc->Mutex.OriginalSyncLevel;
106 Thread->AcquiredMutexList = ObjDesc->Mutex.Next;
111 /*******************************************************************************
113 * FUNCTION: AcpiExLinkMutex
115 * PARAMETERS: ObjDesc - The mutex to be linked
116 * Thread - Current executing thread object
120 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
122 ******************************************************************************/
126 ACPI_OPERAND_OBJECT *ObjDesc,
127 ACPI_THREAD_STATE *Thread)
129 ACPI_OPERAND_OBJECT *ListHead;
132 ListHead = Thread->AcquiredMutexList;
134 /* This object will be the first object in the list */
136 ObjDesc->Mutex.Prev = NULL;
137 ObjDesc->Mutex.Next = ListHead;
139 /* Update old first object to point back to this object */
143 ListHead->Mutex.Prev = ObjDesc;
146 /* Update list head */
148 Thread->AcquiredMutexList = ObjDesc;
152 /*******************************************************************************
154 * FUNCTION: AcpiExAcquireMutexObject
156 * PARAMETERS: Timeout - Timeout in milliseconds
157 * ObjDesc - Mutex object
158 * ThreadId - Current thread state
162 * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
163 * path that supports multiple acquires by the same thread.
165 * MUTEX: Interpreter must be locked
167 * NOTE: This interface is called from three places:
168 * 1) From AcpiExAcquireMutex, via an AML Acquire() operator
169 * 2) From AcpiExAcquireGlobalLock when an AML Field access requires the
171 * 3) From the external interface, AcpiAcquireGlobalLock
173 ******************************************************************************/
176 AcpiExAcquireMutexObject (
178 ACPI_OPERAND_OBJECT *ObjDesc,
179 ACPI_THREAD_ID ThreadId)
184 ACPI_FUNCTION_TRACE_PTR (ExAcquireMutexObject, ObjDesc);
189 return_ACPI_STATUS (AE_BAD_PARAMETER);
192 /* Support for multiple acquires by the owning thread */
194 if (ObjDesc->Mutex.ThreadId == ThreadId)
197 * The mutex is already owned by this thread, just increment the
200 ObjDesc->Mutex.AcquisitionDepth++;
201 return_ACPI_STATUS (AE_OK);
204 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
206 if (ObjDesc == AcpiGbl_GlobalLockMutex)
208 Status = AcpiEvAcquireGlobalLock (Timeout);
212 Status = AcpiExSystemWaitMutex (ObjDesc->Mutex.OsMutex, Timeout);
215 if (ACPI_FAILURE (Status))
217 /* Includes failure from a timeout on TimeDesc */
219 return_ACPI_STATUS (Status);
222 /* Acquired the mutex: update mutex object */
224 ObjDesc->Mutex.ThreadId = ThreadId;
225 ObjDesc->Mutex.AcquisitionDepth = 1;
226 ObjDesc->Mutex.OriginalSyncLevel = 0;
227 ObjDesc->Mutex.OwnerThread = NULL; /* Used only for AML Acquire() */
229 return_ACPI_STATUS (AE_OK);
233 /*******************************************************************************
235 * FUNCTION: AcpiExAcquireMutex
237 * PARAMETERS: TimeDesc - Timeout integer
238 * ObjDesc - Mutex object
239 * WalkState - Current method execution state
243 * DESCRIPTION: Acquire an AML mutex
245 ******************************************************************************/
249 ACPI_OPERAND_OBJECT *TimeDesc,
250 ACPI_OPERAND_OBJECT *ObjDesc,
251 ACPI_WALK_STATE *WalkState)
256 ACPI_FUNCTION_TRACE_PTR (ExAcquireMutex, ObjDesc);
261 return_ACPI_STATUS (AE_BAD_PARAMETER);
264 /* Must have a valid thread state struct */
266 if (!WalkState->Thread)
268 ACPI_ERROR ((AE_INFO,
269 "Cannot acquire Mutex [%4.4s], null thread info",
270 AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
271 return_ACPI_STATUS (AE_AML_INTERNAL);
275 * Current sync level must be less than or equal to the sync level
276 * of the mutex. This mechanism provides some deadlock prevention.
278 if (WalkState->Thread->CurrentSyncLevel > ObjDesc->Mutex.SyncLevel)
280 ACPI_ERROR ((AE_INFO,
281 "Cannot acquire Mutex [%4.4s], "
282 "current SyncLevel is too large (%u)",
283 AcpiUtGetNodeName (ObjDesc->Mutex.Node),
284 WalkState->Thread->CurrentSyncLevel));
285 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
288 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
289 "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
291 ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
292 ObjDesc->Mutex.AcquisitionDepth, WalkState->Thread));
294 Status = AcpiExAcquireMutexObject ((UINT16) TimeDesc->Integer.Value,
295 ObjDesc, WalkState->Thread->ThreadId);
297 if (ACPI_SUCCESS (Status) && ObjDesc->Mutex.AcquisitionDepth == 1)
299 /* Save Thread object, original/current sync levels */
301 ObjDesc->Mutex.OwnerThread = WalkState->Thread;
302 ObjDesc->Mutex.OriginalSyncLevel =
303 WalkState->Thread->CurrentSyncLevel;
304 WalkState->Thread->CurrentSyncLevel =
305 ObjDesc->Mutex.SyncLevel;
307 /* Link the mutex to the current thread for force-unlock at method exit */
309 AcpiExLinkMutex (ObjDesc, WalkState->Thread);
312 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
313 "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
314 ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
315 ObjDesc->Mutex.AcquisitionDepth));
317 return_ACPI_STATUS (Status);
321 /*******************************************************************************
323 * FUNCTION: AcpiExReleaseMutexObject
325 * PARAMETERS: ObjDesc - The object descriptor for this op
329 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
330 * Provides a common path that supports multiple releases (after
331 * previous multiple acquires) by the same thread.
333 * MUTEX: Interpreter must be locked
335 * NOTE: This interface is called from three places:
336 * 1) From AcpiExReleaseMutex, via an AML Acquire() operator
337 * 2) From AcpiExReleaseGlobalLock when an AML Field access requires the
339 * 3) From the external interface, AcpiReleaseGlobalLock
341 ******************************************************************************/
344 AcpiExReleaseMutexObject (
345 ACPI_OPERAND_OBJECT *ObjDesc)
347 ACPI_STATUS Status = AE_OK;
350 ACPI_FUNCTION_TRACE (ExReleaseMutexObject);
353 if (ObjDesc->Mutex.AcquisitionDepth == 0)
355 return_ACPI_STATUS (AE_NOT_ACQUIRED);
358 /* Match multiple Acquires with multiple Releases */
360 ObjDesc->Mutex.AcquisitionDepth--;
361 if (ObjDesc->Mutex.AcquisitionDepth != 0)
363 /* Just decrement the depth and return */
365 return_ACPI_STATUS (AE_OK);
368 if (ObjDesc->Mutex.OwnerThread)
370 /* Unlink the mutex from the owner's list */
372 AcpiExUnlinkMutex (ObjDesc);
373 ObjDesc->Mutex.OwnerThread = NULL;
376 /* Release the mutex, special case for Global Lock */
378 if (ObjDesc == AcpiGbl_GlobalLockMutex)
380 Status = AcpiEvReleaseGlobalLock ();
384 AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
387 /* Clear mutex info */
389 ObjDesc->Mutex.ThreadId = 0;
390 return_ACPI_STATUS (Status);
394 /*******************************************************************************
396 * FUNCTION: AcpiExReleaseMutex
398 * PARAMETERS: ObjDesc - The object descriptor for this op
399 * WalkState - Current method execution state
403 * DESCRIPTION: Release a previously acquired Mutex.
405 ******************************************************************************/
409 ACPI_OPERAND_OBJECT *ObjDesc,
410 ACPI_WALK_STATE *WalkState)
412 UINT8 PreviousSyncLevel;
413 ACPI_THREAD_STATE *OwnerThread;
414 ACPI_STATUS Status = AE_OK;
417 ACPI_FUNCTION_TRACE (ExReleaseMutex);
422 return_ACPI_STATUS (AE_BAD_PARAMETER);
425 OwnerThread = ObjDesc->Mutex.OwnerThread;
427 /* The mutex must have been previously acquired in order to release it */
431 ACPI_ERROR ((AE_INFO,
432 "Cannot release Mutex [%4.4s], not acquired",
433 AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
434 return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED);
437 /* Must have a valid thread ID */
439 if (!WalkState->Thread)
441 ACPI_ERROR ((AE_INFO,
442 "Cannot release Mutex [%4.4s], null thread info",
443 AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
444 return_ACPI_STATUS (AE_AML_INTERNAL);
448 * The Mutex is owned, but this thread must be the owner.
449 * Special case for Global Lock, any thread can release
451 if ((OwnerThread->ThreadId != WalkState->Thread->ThreadId) &&
452 (ObjDesc != AcpiGbl_GlobalLockMutex))
454 ACPI_ERROR ((AE_INFO,
455 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
456 (UINT32) WalkState->Thread->ThreadId,
457 AcpiUtGetNodeName (ObjDesc->Mutex.Node),
458 (UINT32) OwnerThread->ThreadId));
459 return_ACPI_STATUS (AE_AML_NOT_OWNER);
463 * The sync level of the mutex must be equal to the current sync level. In
464 * other words, the current level means that at least one mutex at that
465 * level is currently being held. Attempting to release a mutex of a
466 * different level can only mean that the mutex ordering rule is being
467 * violated. This behavior is clarified in ACPI 4.0 specification.
469 if (ObjDesc->Mutex.SyncLevel != OwnerThread->CurrentSyncLevel)
471 ACPI_ERROR ((AE_INFO,
472 "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
473 "mutex %u current %u",
474 AcpiUtGetNodeName (ObjDesc->Mutex.Node),
475 ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel));
476 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
480 * Get the previous SyncLevel from the head of the acquired mutex list.
481 * This handles the case where several mutexes at the same level have been
482 * acquired, but are not released in reverse order.
485 OwnerThread->AcquiredMutexList->Mutex.OriginalSyncLevel;
487 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
488 "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
489 "Prev SyncLevel %u, Depth %u TID %p\n",
490 ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
491 PreviousSyncLevel, ObjDesc->Mutex.AcquisitionDepth,
494 Status = AcpiExReleaseMutexObject (ObjDesc);
495 if (ACPI_FAILURE (Status))
497 return_ACPI_STATUS (Status);
500 if (ObjDesc->Mutex.AcquisitionDepth == 0)
502 /* Restore the previous SyncLevel */
504 OwnerThread->CurrentSyncLevel = PreviousSyncLevel;
507 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
508 "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
509 "Prev SyncLevel %u, Depth %u\n",
510 ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel,
511 PreviousSyncLevel, ObjDesc->Mutex.AcquisitionDepth));
513 return_ACPI_STATUS (Status);
517 /*******************************************************************************
519 * FUNCTION: AcpiExReleaseAllMutexes
521 * PARAMETERS: Thread - Current executing thread object
525 * DESCRIPTION: Release all mutexes held by this thread
527 * NOTE: This function is called as the thread is exiting the interpreter.
528 * Mutexes are not released when an individual control method is exited, but
529 * only when the parent thread actually exits the interpreter. This allows one
530 * method to acquire a mutex, and a different method to release it, as long as
531 * this is performed underneath a single parent control method.
533 ******************************************************************************/
536 AcpiExReleaseAllMutexes (
537 ACPI_THREAD_STATE *Thread)
539 ACPI_OPERAND_OBJECT *Next = Thread->AcquiredMutexList;
540 ACPI_OPERAND_OBJECT *ObjDesc;
543 ACPI_FUNCTION_TRACE (ExReleaseAllMutexes);
546 /* Traverse the list of owned mutexes, releasing each one */
551 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
552 "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
553 ObjDesc->Mutex.Node->Name.Ascii, ObjDesc->Mutex.SyncLevel,
554 ObjDesc->Mutex.AcquisitionDepth));
556 /* Release the mutex, special case for Global Lock */
558 if (ObjDesc == AcpiGbl_GlobalLockMutex)
562 (void) AcpiEvReleaseGlobalLock ();
566 AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
569 /* Update Thread SyncLevel (Last mutex is the important one) */
571 Thread->CurrentSyncLevel = ObjDesc->Mutex.OriginalSyncLevel;
573 /* Mark mutex unowned */
575 Next = ObjDesc->Mutex.Next;
577 ObjDesc->Mutex.Prev = NULL;
578 ObjDesc->Mutex.Next = NULL;
579 ObjDesc->Mutex.AcquisitionDepth = 0;
580 ObjDesc->Mutex.OwnerThread = NULL;
581 ObjDesc->Mutex.ThreadId = 0;