1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Module Name: exmutex - ASL Mutex Acquire/Release functions
4433d6423SLionel Sambuc *
5433d6423SLionel Sambuc *****************************************************************************/
6433d6423SLionel Sambuc
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc * All rights reserved.
10433d6423SLionel Sambuc *
11*29492bb7SDavid van Moolenbroek * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek * are met:
14*29492bb7SDavid van Moolenbroek * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek * notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek * without modification.
17*29492bb7SDavid van Moolenbroek * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek * substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek * ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek * including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek * binary redistribution.
22*29492bb7SDavid van Moolenbroek * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek * of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek * from this software without specific prior written permission.
25433d6423SLionel Sambuc *
26*29492bb7SDavid van Moolenbroek * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek * Software Foundation.
29433d6423SLionel Sambuc *
30*29492bb7SDavid van Moolenbroek * NO WARRANTY
31*29492bb7SDavid van Moolenbroek * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek */
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc #include "acinterp.h"
47433d6423SLionel Sambuc #include "acevents.h"
48433d6423SLionel Sambuc
49433d6423SLionel Sambuc #define _COMPONENT ACPI_EXECUTER
50433d6423SLionel Sambuc ACPI_MODULE_NAME ("exmutex")
51433d6423SLionel Sambuc
52433d6423SLionel Sambuc /* Local prototypes */
53433d6423SLionel Sambuc
54433d6423SLionel Sambuc static void
55433d6423SLionel Sambuc AcpiExLinkMutex (
56433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc,
57433d6423SLionel Sambuc ACPI_THREAD_STATE *Thread);
58433d6423SLionel Sambuc
59433d6423SLionel Sambuc
60433d6423SLionel Sambuc /*******************************************************************************
61433d6423SLionel Sambuc *
62433d6423SLionel Sambuc * FUNCTION: AcpiExUnlinkMutex
63433d6423SLionel Sambuc *
64433d6423SLionel Sambuc * PARAMETERS: ObjDesc - The mutex to be unlinked
65433d6423SLionel Sambuc *
66433d6423SLionel Sambuc * RETURN: None
67433d6423SLionel Sambuc *
68433d6423SLionel Sambuc * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
69433d6423SLionel Sambuc *
70433d6423SLionel Sambuc ******************************************************************************/
71433d6423SLionel Sambuc
72433d6423SLionel Sambuc void
AcpiExUnlinkMutex(ACPI_OPERAND_OBJECT * ObjDesc)73433d6423SLionel Sambuc AcpiExUnlinkMutex (
74433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc)
75433d6423SLionel Sambuc {
76433d6423SLionel Sambuc ACPI_THREAD_STATE *Thread = ObjDesc->Mutex.OwnerThread;
77433d6423SLionel Sambuc
78433d6423SLionel Sambuc
79433d6423SLionel Sambuc if (!Thread)
80433d6423SLionel Sambuc {
81433d6423SLionel Sambuc return;
82433d6423SLionel Sambuc }
83433d6423SLionel Sambuc
84433d6423SLionel Sambuc /* Doubly linked list */
85433d6423SLionel Sambuc
86433d6423SLionel Sambuc if (ObjDesc->Mutex.Next)
87433d6423SLionel Sambuc {
88433d6423SLionel Sambuc (ObjDesc->Mutex.Next)->Mutex.Prev = ObjDesc->Mutex.Prev;
89433d6423SLionel Sambuc }
90433d6423SLionel Sambuc
91433d6423SLionel Sambuc if (ObjDesc->Mutex.Prev)
92433d6423SLionel Sambuc {
93433d6423SLionel Sambuc (ObjDesc->Mutex.Prev)->Mutex.Next = ObjDesc->Mutex.Next;
94433d6423SLionel Sambuc
95433d6423SLionel Sambuc /*
96433d6423SLionel Sambuc * Migrate the previous sync level associated with this mutex to
97433d6423SLionel Sambuc * the previous mutex on the list so that it may be preserved.
98433d6423SLionel Sambuc * This handles the case where several mutexes have been acquired
99433d6423SLionel Sambuc * at the same level, but are not released in opposite order.
100433d6423SLionel Sambuc */
101433d6423SLionel Sambuc (ObjDesc->Mutex.Prev)->Mutex.OriginalSyncLevel =
102433d6423SLionel Sambuc ObjDesc->Mutex.OriginalSyncLevel;
103433d6423SLionel Sambuc }
104433d6423SLionel Sambuc else
105433d6423SLionel Sambuc {
106433d6423SLionel Sambuc Thread->AcquiredMutexList = ObjDesc->Mutex.Next;
107433d6423SLionel Sambuc }
108433d6423SLionel Sambuc }
109433d6423SLionel Sambuc
110433d6423SLionel Sambuc
111433d6423SLionel Sambuc /*******************************************************************************
112433d6423SLionel Sambuc *
113433d6423SLionel Sambuc * FUNCTION: AcpiExLinkMutex
114433d6423SLionel Sambuc *
115433d6423SLionel Sambuc * PARAMETERS: ObjDesc - The mutex to be linked
116433d6423SLionel Sambuc * Thread - Current executing thread object
117433d6423SLionel Sambuc *
118433d6423SLionel Sambuc * RETURN: None
119433d6423SLionel Sambuc *
120433d6423SLionel Sambuc * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
121433d6423SLionel Sambuc *
122433d6423SLionel Sambuc ******************************************************************************/
123433d6423SLionel Sambuc
124433d6423SLionel Sambuc static void
AcpiExLinkMutex(ACPI_OPERAND_OBJECT * ObjDesc,ACPI_THREAD_STATE * Thread)125433d6423SLionel Sambuc AcpiExLinkMutex (
126433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc,
127433d6423SLionel Sambuc ACPI_THREAD_STATE *Thread)
128433d6423SLionel Sambuc {
129433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ListHead;
130433d6423SLionel Sambuc
131433d6423SLionel Sambuc
132433d6423SLionel Sambuc ListHead = Thread->AcquiredMutexList;
133433d6423SLionel Sambuc
134433d6423SLionel Sambuc /* This object will be the first object in the list */
135433d6423SLionel Sambuc
136433d6423SLionel Sambuc ObjDesc->Mutex.Prev = NULL;
137433d6423SLionel Sambuc ObjDesc->Mutex.Next = ListHead;
138433d6423SLionel Sambuc
139433d6423SLionel Sambuc /* Update old first object to point back to this object */
140433d6423SLionel Sambuc
141433d6423SLionel Sambuc if (ListHead)
142433d6423SLionel Sambuc {
143433d6423SLionel Sambuc ListHead->Mutex.Prev = ObjDesc;
144433d6423SLionel Sambuc }
145433d6423SLionel Sambuc
146433d6423SLionel Sambuc /* Update list head */
147433d6423SLionel Sambuc
148433d6423SLionel Sambuc Thread->AcquiredMutexList = ObjDesc;
149433d6423SLionel Sambuc }
150433d6423SLionel Sambuc
151433d6423SLionel Sambuc
152433d6423SLionel Sambuc /*******************************************************************************
153433d6423SLionel Sambuc *
154433d6423SLionel Sambuc * FUNCTION: AcpiExAcquireMutexObject
155433d6423SLionel Sambuc *
156433d6423SLionel Sambuc * PARAMETERS: Timeout - Timeout in milliseconds
157433d6423SLionel Sambuc * ObjDesc - Mutex object
158433d6423SLionel Sambuc * ThreadId - Current thread state
159433d6423SLionel Sambuc *
160433d6423SLionel Sambuc * RETURN: Status
161433d6423SLionel Sambuc *
162433d6423SLionel Sambuc * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
163433d6423SLionel Sambuc * path that supports multiple acquires by the same thread.
164433d6423SLionel Sambuc *
165433d6423SLionel Sambuc * MUTEX: Interpreter must be locked
166433d6423SLionel Sambuc *
167433d6423SLionel Sambuc * NOTE: This interface is called from three places:
168433d6423SLionel Sambuc * 1) From AcpiExAcquireMutex, via an AML Acquire() operator
169433d6423SLionel Sambuc * 2) From AcpiExAcquireGlobalLock when an AML Field access requires the
170433d6423SLionel Sambuc * global lock
171433d6423SLionel Sambuc * 3) From the external interface, AcpiAcquireGlobalLock
172433d6423SLionel Sambuc *
173433d6423SLionel Sambuc ******************************************************************************/
174433d6423SLionel Sambuc
175433d6423SLionel Sambuc ACPI_STATUS
AcpiExAcquireMutexObject(UINT16 Timeout,ACPI_OPERAND_OBJECT * ObjDesc,ACPI_THREAD_ID ThreadId)176433d6423SLionel Sambuc AcpiExAcquireMutexObject (
177433d6423SLionel Sambuc UINT16 Timeout,
178433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc,
179433d6423SLionel Sambuc ACPI_THREAD_ID ThreadId)
180433d6423SLionel Sambuc {
181433d6423SLionel Sambuc ACPI_STATUS Status;
182433d6423SLionel Sambuc
183433d6423SLionel Sambuc
184433d6423SLionel Sambuc ACPI_FUNCTION_TRACE_PTR (ExAcquireMutexObject, ObjDesc);
185433d6423SLionel Sambuc
186433d6423SLionel Sambuc
187433d6423SLionel Sambuc if (!ObjDesc)
188433d6423SLionel Sambuc {
189433d6423SLionel Sambuc return_ACPI_STATUS (AE_BAD_PARAMETER);
190433d6423SLionel Sambuc }
191433d6423SLionel Sambuc
192433d6423SLionel Sambuc /* Support for multiple acquires by the owning thread */
193433d6423SLionel Sambuc
194433d6423SLionel Sambuc if (ObjDesc->Mutex.ThreadId == ThreadId)
195433d6423SLionel Sambuc {
196433d6423SLionel Sambuc /*
197433d6423SLionel Sambuc * The mutex is already owned by this thread, just increment the
198433d6423SLionel Sambuc * acquisition depth
199433d6423SLionel Sambuc */
200433d6423SLionel Sambuc ObjDesc->Mutex.AcquisitionDepth++;
201433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
202433d6423SLionel Sambuc }
203433d6423SLionel Sambuc
204433d6423SLionel Sambuc /* Acquire the mutex, wait if necessary. Special case for Global Lock */
205433d6423SLionel Sambuc
206433d6423SLionel Sambuc if (ObjDesc == AcpiGbl_GlobalLockMutex)
207433d6423SLionel Sambuc {
208433d6423SLionel Sambuc Status = AcpiEvAcquireGlobalLock (Timeout);
209433d6423SLionel Sambuc }
210433d6423SLionel Sambuc else
211433d6423SLionel Sambuc {
212433d6423SLionel Sambuc Status = AcpiExSystemWaitMutex (ObjDesc->Mutex.OsMutex,
213433d6423SLionel Sambuc Timeout);
214433d6423SLionel Sambuc }
215433d6423SLionel Sambuc
216433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
217433d6423SLionel Sambuc {
218433d6423SLionel Sambuc /* Includes failure from a timeout on TimeDesc */
219433d6423SLionel Sambuc
220433d6423SLionel Sambuc return_ACPI_STATUS (Status);
221433d6423SLionel Sambuc }
222433d6423SLionel Sambuc
223433d6423SLionel Sambuc /* Acquired the mutex: update mutex object */
224433d6423SLionel Sambuc
225433d6423SLionel Sambuc ObjDesc->Mutex.ThreadId = ThreadId;
226433d6423SLionel Sambuc ObjDesc->Mutex.AcquisitionDepth = 1;
227433d6423SLionel Sambuc ObjDesc->Mutex.OriginalSyncLevel = 0;
228433d6423SLionel Sambuc ObjDesc->Mutex.OwnerThread = NULL; /* Used only for AML Acquire() */
229433d6423SLionel Sambuc
230433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
231433d6423SLionel Sambuc }
232433d6423SLionel Sambuc
233433d6423SLionel Sambuc
234433d6423SLionel Sambuc /*******************************************************************************
235433d6423SLionel Sambuc *
236433d6423SLionel Sambuc * FUNCTION: AcpiExAcquireMutex
237433d6423SLionel Sambuc *
238433d6423SLionel Sambuc * PARAMETERS: TimeDesc - Timeout integer
239433d6423SLionel Sambuc * ObjDesc - Mutex object
240433d6423SLionel Sambuc * WalkState - Current method execution state
241433d6423SLionel Sambuc *
242433d6423SLionel Sambuc * RETURN: Status
243433d6423SLionel Sambuc *
244433d6423SLionel Sambuc * DESCRIPTION: Acquire an AML mutex
245433d6423SLionel Sambuc *
246433d6423SLionel Sambuc ******************************************************************************/
247433d6423SLionel Sambuc
248433d6423SLionel Sambuc ACPI_STATUS
AcpiExAcquireMutex(ACPI_OPERAND_OBJECT * TimeDesc,ACPI_OPERAND_OBJECT * ObjDesc,ACPI_WALK_STATE * WalkState)249433d6423SLionel Sambuc AcpiExAcquireMutex (
250433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *TimeDesc,
251433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc,
252433d6423SLionel Sambuc ACPI_WALK_STATE *WalkState)
253433d6423SLionel Sambuc {
254433d6423SLionel Sambuc ACPI_STATUS Status;
255433d6423SLionel Sambuc
256433d6423SLionel Sambuc
257433d6423SLionel Sambuc ACPI_FUNCTION_TRACE_PTR (ExAcquireMutex, ObjDesc);
258433d6423SLionel Sambuc
259433d6423SLionel Sambuc
260433d6423SLionel Sambuc if (!ObjDesc)
261433d6423SLionel Sambuc {
262433d6423SLionel Sambuc return_ACPI_STATUS (AE_BAD_PARAMETER);
263433d6423SLionel Sambuc }
264433d6423SLionel Sambuc
265433d6423SLionel Sambuc /* Must have a valid thread state struct */
266433d6423SLionel Sambuc
267433d6423SLionel Sambuc if (!WalkState->Thread)
268433d6423SLionel Sambuc {
269433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO,
270433d6423SLionel Sambuc "Cannot acquire Mutex [%4.4s], null thread info",
271433d6423SLionel Sambuc AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
272433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_INTERNAL);
273433d6423SLionel Sambuc }
274433d6423SLionel Sambuc
275433d6423SLionel Sambuc /*
276433d6423SLionel Sambuc * Current sync level must be less than or equal to the sync level of the
277433d6423SLionel Sambuc * mutex. This mechanism provides some deadlock prevention
278433d6423SLionel Sambuc */
279433d6423SLionel Sambuc if (WalkState->Thread->CurrentSyncLevel > ObjDesc->Mutex.SyncLevel)
280433d6423SLionel Sambuc {
281433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO,
282433d6423SLionel Sambuc "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%u)",
283433d6423SLionel Sambuc AcpiUtGetNodeName (ObjDesc->Mutex.Node),
284433d6423SLionel Sambuc WalkState->Thread->CurrentSyncLevel));
285433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
286433d6423SLionel Sambuc }
287433d6423SLionel Sambuc
288433d6423SLionel Sambuc Status = AcpiExAcquireMutexObject ((UINT16) TimeDesc->Integer.Value,
289433d6423SLionel Sambuc ObjDesc, WalkState->Thread->ThreadId);
290433d6423SLionel Sambuc if (ACPI_SUCCESS (Status) && ObjDesc->Mutex.AcquisitionDepth == 1)
291433d6423SLionel Sambuc {
292433d6423SLionel Sambuc /* Save Thread object, original/current sync levels */
293433d6423SLionel Sambuc
294433d6423SLionel Sambuc ObjDesc->Mutex.OwnerThread = WalkState->Thread;
295433d6423SLionel Sambuc ObjDesc->Mutex.OriginalSyncLevel = WalkState->Thread->CurrentSyncLevel;
296433d6423SLionel Sambuc WalkState->Thread->CurrentSyncLevel = ObjDesc->Mutex.SyncLevel;
297433d6423SLionel Sambuc
298433d6423SLionel Sambuc /* Link the mutex to the current thread for force-unlock at method exit */
299433d6423SLionel Sambuc
300433d6423SLionel Sambuc AcpiExLinkMutex (ObjDesc, WalkState->Thread);
301433d6423SLionel Sambuc }
302433d6423SLionel Sambuc
303433d6423SLionel Sambuc return_ACPI_STATUS (Status);
304433d6423SLionel Sambuc }
305433d6423SLionel Sambuc
306433d6423SLionel Sambuc
307433d6423SLionel Sambuc /*******************************************************************************
308433d6423SLionel Sambuc *
309433d6423SLionel Sambuc * FUNCTION: AcpiExReleaseMutexObject
310433d6423SLionel Sambuc *
311433d6423SLionel Sambuc * PARAMETERS: ObjDesc - The object descriptor for this op
312433d6423SLionel Sambuc *
313433d6423SLionel Sambuc * RETURN: Status
314433d6423SLionel Sambuc *
315433d6423SLionel Sambuc * DESCRIPTION: Release a previously acquired Mutex, low level interface.
316433d6423SLionel Sambuc * Provides a common path that supports multiple releases (after
317433d6423SLionel Sambuc * previous multiple acquires) by the same thread.
318433d6423SLionel Sambuc *
319433d6423SLionel Sambuc * MUTEX: Interpreter must be locked
320433d6423SLionel Sambuc *
321433d6423SLionel Sambuc * NOTE: This interface is called from three places:
322433d6423SLionel Sambuc * 1) From AcpiExReleaseMutex, via an AML Acquire() operator
323433d6423SLionel Sambuc * 2) From AcpiExReleaseGlobalLock when an AML Field access requires the
324433d6423SLionel Sambuc * global lock
325433d6423SLionel Sambuc * 3) From the external interface, AcpiReleaseGlobalLock
326433d6423SLionel Sambuc *
327433d6423SLionel Sambuc ******************************************************************************/
328433d6423SLionel Sambuc
329433d6423SLionel Sambuc ACPI_STATUS
AcpiExReleaseMutexObject(ACPI_OPERAND_OBJECT * ObjDesc)330433d6423SLionel Sambuc AcpiExReleaseMutexObject (
331433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc)
332433d6423SLionel Sambuc {
333433d6423SLionel Sambuc ACPI_STATUS Status = AE_OK;
334433d6423SLionel Sambuc
335433d6423SLionel Sambuc
336433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (ExReleaseMutexObject);
337433d6423SLionel Sambuc
338433d6423SLionel Sambuc
339433d6423SLionel Sambuc if (ObjDesc->Mutex.AcquisitionDepth == 0)
340433d6423SLionel Sambuc {
341*29492bb7SDavid van Moolenbroek return_ACPI_STATUS (AE_NOT_ACQUIRED);
342433d6423SLionel Sambuc }
343433d6423SLionel Sambuc
344433d6423SLionel Sambuc /* Match multiple Acquires with multiple Releases */
345433d6423SLionel Sambuc
346433d6423SLionel Sambuc ObjDesc->Mutex.AcquisitionDepth--;
347433d6423SLionel Sambuc if (ObjDesc->Mutex.AcquisitionDepth != 0)
348433d6423SLionel Sambuc {
349433d6423SLionel Sambuc /* Just decrement the depth and return */
350433d6423SLionel Sambuc
351433d6423SLionel Sambuc return_ACPI_STATUS (AE_OK);
352433d6423SLionel Sambuc }
353433d6423SLionel Sambuc
354433d6423SLionel Sambuc if (ObjDesc->Mutex.OwnerThread)
355433d6423SLionel Sambuc {
356433d6423SLionel Sambuc /* Unlink the mutex from the owner's list */
357433d6423SLionel Sambuc
358433d6423SLionel Sambuc AcpiExUnlinkMutex (ObjDesc);
359433d6423SLionel Sambuc ObjDesc->Mutex.OwnerThread = NULL;
360433d6423SLionel Sambuc }
361433d6423SLionel Sambuc
362433d6423SLionel Sambuc /* Release the mutex, special case for Global Lock */
363433d6423SLionel Sambuc
364433d6423SLionel Sambuc if (ObjDesc == AcpiGbl_GlobalLockMutex)
365433d6423SLionel Sambuc {
366433d6423SLionel Sambuc Status = AcpiEvReleaseGlobalLock ();
367433d6423SLionel Sambuc }
368433d6423SLionel Sambuc else
369433d6423SLionel Sambuc {
370433d6423SLionel Sambuc AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
371433d6423SLionel Sambuc }
372433d6423SLionel Sambuc
373433d6423SLionel Sambuc /* Clear mutex info */
374433d6423SLionel Sambuc
375433d6423SLionel Sambuc ObjDesc->Mutex.ThreadId = 0;
376433d6423SLionel Sambuc return_ACPI_STATUS (Status);
377433d6423SLionel Sambuc }
378433d6423SLionel Sambuc
379433d6423SLionel Sambuc
380433d6423SLionel Sambuc /*******************************************************************************
381433d6423SLionel Sambuc *
382433d6423SLionel Sambuc * FUNCTION: AcpiExReleaseMutex
383433d6423SLionel Sambuc *
384433d6423SLionel Sambuc * PARAMETERS: ObjDesc - The object descriptor for this op
385433d6423SLionel Sambuc * WalkState - Current method execution state
386433d6423SLionel Sambuc *
387433d6423SLionel Sambuc * RETURN: Status
388433d6423SLionel Sambuc *
389433d6423SLionel Sambuc * DESCRIPTION: Release a previously acquired Mutex.
390433d6423SLionel Sambuc *
391433d6423SLionel Sambuc ******************************************************************************/
392433d6423SLionel Sambuc
393433d6423SLionel Sambuc ACPI_STATUS
AcpiExReleaseMutex(ACPI_OPERAND_OBJECT * ObjDesc,ACPI_WALK_STATE * WalkState)394433d6423SLionel Sambuc AcpiExReleaseMutex (
395433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc,
396433d6423SLionel Sambuc ACPI_WALK_STATE *WalkState)
397433d6423SLionel Sambuc {
398433d6423SLionel Sambuc ACPI_STATUS Status = AE_OK;
399433d6423SLionel Sambuc UINT8 PreviousSyncLevel;
400433d6423SLionel Sambuc ACPI_THREAD_STATE *OwnerThread;
401433d6423SLionel Sambuc
402433d6423SLionel Sambuc
403433d6423SLionel Sambuc ACPI_FUNCTION_TRACE (ExReleaseMutex);
404433d6423SLionel Sambuc
405433d6423SLionel Sambuc
406433d6423SLionel Sambuc if (!ObjDesc)
407433d6423SLionel Sambuc {
408433d6423SLionel Sambuc return_ACPI_STATUS (AE_BAD_PARAMETER);
409433d6423SLionel Sambuc }
410433d6423SLionel Sambuc
411433d6423SLionel Sambuc OwnerThread = ObjDesc->Mutex.OwnerThread;
412433d6423SLionel Sambuc
413433d6423SLionel Sambuc /* The mutex must have been previously acquired in order to release it */
414433d6423SLionel Sambuc
415433d6423SLionel Sambuc if (!OwnerThread)
416433d6423SLionel Sambuc {
417433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO,
418433d6423SLionel Sambuc "Cannot release Mutex [%4.4s], not acquired",
419433d6423SLionel Sambuc AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
420433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED);
421433d6423SLionel Sambuc }
422433d6423SLionel Sambuc
423433d6423SLionel Sambuc /* Must have a valid thread ID */
424433d6423SLionel Sambuc
425433d6423SLionel Sambuc if (!WalkState->Thread)
426433d6423SLionel Sambuc {
427433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO,
428433d6423SLionel Sambuc "Cannot release Mutex [%4.4s], null thread info",
429433d6423SLionel Sambuc AcpiUtGetNodeName (ObjDesc->Mutex.Node)));
430433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_INTERNAL);
431433d6423SLionel Sambuc }
432433d6423SLionel Sambuc
433433d6423SLionel Sambuc /*
434433d6423SLionel Sambuc * The Mutex is owned, but this thread must be the owner.
435433d6423SLionel Sambuc * Special case for Global Lock, any thread can release
436433d6423SLionel Sambuc */
437433d6423SLionel Sambuc if ((OwnerThread->ThreadId != WalkState->Thread->ThreadId) &&
438433d6423SLionel Sambuc (ObjDesc != AcpiGbl_GlobalLockMutex))
439433d6423SLionel Sambuc {
440433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO,
441*29492bb7SDavid van Moolenbroek "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
442*29492bb7SDavid van Moolenbroek (UINT32) WalkState->Thread->ThreadId,
443433d6423SLionel Sambuc AcpiUtGetNodeName (ObjDesc->Mutex.Node),
444*29492bb7SDavid van Moolenbroek (UINT32) OwnerThread->ThreadId));
445433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_NOT_OWNER);
446433d6423SLionel Sambuc }
447433d6423SLionel Sambuc
448433d6423SLionel Sambuc /*
449433d6423SLionel Sambuc * The sync level of the mutex must be equal to the current sync level. In
450433d6423SLionel Sambuc * other words, the current level means that at least one mutex at that
451433d6423SLionel Sambuc * level is currently being held. Attempting to release a mutex of a
452433d6423SLionel Sambuc * different level can only mean that the mutex ordering rule is being
453433d6423SLionel Sambuc * violated. This behavior is clarified in ACPI 4.0 specification.
454433d6423SLionel Sambuc */
455433d6423SLionel Sambuc if (ObjDesc->Mutex.SyncLevel != OwnerThread->CurrentSyncLevel)
456433d6423SLionel Sambuc {
457433d6423SLionel Sambuc ACPI_ERROR ((AE_INFO,
458433d6423SLionel Sambuc "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %u current %u",
459433d6423SLionel Sambuc AcpiUtGetNodeName (ObjDesc->Mutex.Node),
460433d6423SLionel Sambuc ObjDesc->Mutex.SyncLevel, WalkState->Thread->CurrentSyncLevel));
461433d6423SLionel Sambuc return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
462433d6423SLionel Sambuc }
463433d6423SLionel Sambuc
464433d6423SLionel Sambuc /*
465433d6423SLionel Sambuc * Get the previous SyncLevel from the head of the acquired mutex list.
466433d6423SLionel Sambuc * This handles the case where several mutexes at the same level have been
467433d6423SLionel Sambuc * acquired, but are not released in reverse order.
468433d6423SLionel Sambuc */
469433d6423SLionel Sambuc PreviousSyncLevel =
470433d6423SLionel Sambuc OwnerThread->AcquiredMutexList->Mutex.OriginalSyncLevel;
471433d6423SLionel Sambuc
472433d6423SLionel Sambuc Status = AcpiExReleaseMutexObject (ObjDesc);
473433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
474433d6423SLionel Sambuc {
475433d6423SLionel Sambuc return_ACPI_STATUS (Status);
476433d6423SLionel Sambuc }
477433d6423SLionel Sambuc
478433d6423SLionel Sambuc if (ObjDesc->Mutex.AcquisitionDepth == 0)
479433d6423SLionel Sambuc {
480433d6423SLionel Sambuc /* Restore the previous SyncLevel */
481433d6423SLionel Sambuc
482433d6423SLionel Sambuc OwnerThread->CurrentSyncLevel = PreviousSyncLevel;
483433d6423SLionel Sambuc }
484433d6423SLionel Sambuc
485433d6423SLionel Sambuc return_ACPI_STATUS (Status);
486433d6423SLionel Sambuc }
487433d6423SLionel Sambuc
488433d6423SLionel Sambuc
489433d6423SLionel Sambuc /*******************************************************************************
490433d6423SLionel Sambuc *
491433d6423SLionel Sambuc * FUNCTION: AcpiExReleaseAllMutexes
492433d6423SLionel Sambuc *
493433d6423SLionel Sambuc * PARAMETERS: Thread - Current executing thread object
494433d6423SLionel Sambuc *
495433d6423SLionel Sambuc * RETURN: Status
496433d6423SLionel Sambuc *
497433d6423SLionel Sambuc * DESCRIPTION: Release all mutexes held by this thread
498433d6423SLionel Sambuc *
499433d6423SLionel Sambuc * NOTE: This function is called as the thread is exiting the interpreter.
500433d6423SLionel Sambuc * Mutexes are not released when an individual control method is exited, but
501433d6423SLionel Sambuc * only when the parent thread actually exits the interpreter. This allows one
502433d6423SLionel Sambuc * method to acquire a mutex, and a different method to release it, as long as
503433d6423SLionel Sambuc * this is performed underneath a single parent control method.
504433d6423SLionel Sambuc *
505433d6423SLionel Sambuc ******************************************************************************/
506433d6423SLionel Sambuc
507433d6423SLionel Sambuc void
AcpiExReleaseAllMutexes(ACPI_THREAD_STATE * Thread)508433d6423SLionel Sambuc AcpiExReleaseAllMutexes (
509433d6423SLionel Sambuc ACPI_THREAD_STATE *Thread)
510433d6423SLionel Sambuc {
511433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *Next = Thread->AcquiredMutexList;
512433d6423SLionel Sambuc ACPI_OPERAND_OBJECT *ObjDesc;
513433d6423SLionel Sambuc
514433d6423SLionel Sambuc
515*29492bb7SDavid van Moolenbroek ACPI_FUNCTION_NAME (ExReleaseAllMutexes);
516433d6423SLionel Sambuc
517433d6423SLionel Sambuc
518433d6423SLionel Sambuc /* Traverse the list of owned mutexes, releasing each one */
519433d6423SLionel Sambuc
520433d6423SLionel Sambuc while (Next)
521433d6423SLionel Sambuc {
522433d6423SLionel Sambuc ObjDesc = Next;
523433d6423SLionel Sambuc Next = ObjDesc->Mutex.Next;
524433d6423SLionel Sambuc
525433d6423SLionel Sambuc ObjDesc->Mutex.Prev = NULL;
526433d6423SLionel Sambuc ObjDesc->Mutex.Next = NULL;
527433d6423SLionel Sambuc ObjDesc->Mutex.AcquisitionDepth = 0;
528433d6423SLionel Sambuc
529*29492bb7SDavid van Moolenbroek ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
530*29492bb7SDavid van Moolenbroek "Force-releasing held mutex: %p\n", ObjDesc));
531*29492bb7SDavid van Moolenbroek
532433d6423SLionel Sambuc /* Release the mutex, special case for Global Lock */
533433d6423SLionel Sambuc
534433d6423SLionel Sambuc if (ObjDesc == AcpiGbl_GlobalLockMutex)
535433d6423SLionel Sambuc {
536433d6423SLionel Sambuc /* Ignore errors */
537433d6423SLionel Sambuc
538433d6423SLionel Sambuc (void) AcpiEvReleaseGlobalLock ();
539433d6423SLionel Sambuc }
540433d6423SLionel Sambuc else
541433d6423SLionel Sambuc {
542433d6423SLionel Sambuc AcpiOsReleaseMutex (ObjDesc->Mutex.OsMutex);
543433d6423SLionel Sambuc }
544433d6423SLionel Sambuc
545433d6423SLionel Sambuc /* Mark mutex unowned */
546433d6423SLionel Sambuc
547433d6423SLionel Sambuc ObjDesc->Mutex.OwnerThread = NULL;
548433d6423SLionel Sambuc ObjDesc->Mutex.ThreadId = 0;
549433d6423SLionel Sambuc
550433d6423SLionel Sambuc /* Update Thread SyncLevel (Last mutex is the important one) */
551433d6423SLionel Sambuc
552433d6423SLionel Sambuc Thread->CurrentSyncLevel = ObjDesc->Mutex.OriginalSyncLevel;
553433d6423SLionel Sambuc }
554433d6423SLionel Sambuc }
555