1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Module Name: exmutex - ASL Mutex Acquire/Release functions
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun *
8*4882a593Smuzhiyun *****************************************************************************/
9*4882a593Smuzhiyun
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "acinterp.h"
13*4882a593Smuzhiyun #include "acevents.h"
14*4882a593Smuzhiyun
15*4882a593Smuzhiyun #define _COMPONENT ACPI_EXECUTER
16*4882a593Smuzhiyun ACPI_MODULE_NAME("exmutex")
17*4882a593Smuzhiyun
18*4882a593Smuzhiyun /* Local prototypes */
19*4882a593Smuzhiyun static void
20*4882a593Smuzhiyun acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
21*4882a593Smuzhiyun struct acpi_thread_state *thread);
22*4882a593Smuzhiyun
23*4882a593Smuzhiyun /*******************************************************************************
24*4882a593Smuzhiyun *
25*4882a593Smuzhiyun * FUNCTION: acpi_ex_unlink_mutex
26*4882a593Smuzhiyun *
27*4882a593Smuzhiyun * PARAMETERS: obj_desc - The mutex to be unlinked
28*4882a593Smuzhiyun *
29*4882a593Smuzhiyun * RETURN: None
30*4882a593Smuzhiyun *
31*4882a593Smuzhiyun * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
32*4882a593Smuzhiyun *
33*4882a593Smuzhiyun ******************************************************************************/
34*4882a593Smuzhiyun
acpi_ex_unlink_mutex(union acpi_operand_object * obj_desc)35*4882a593Smuzhiyun void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
36*4882a593Smuzhiyun {
37*4882a593Smuzhiyun struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
38*4882a593Smuzhiyun
39*4882a593Smuzhiyun if (!thread) {
40*4882a593Smuzhiyun return;
41*4882a593Smuzhiyun }
42*4882a593Smuzhiyun
43*4882a593Smuzhiyun /* Doubly linked list */
44*4882a593Smuzhiyun
45*4882a593Smuzhiyun if (obj_desc->mutex.next) {
46*4882a593Smuzhiyun (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
47*4882a593Smuzhiyun }
48*4882a593Smuzhiyun
49*4882a593Smuzhiyun if (obj_desc->mutex.prev) {
50*4882a593Smuzhiyun (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun /*
53*4882a593Smuzhiyun * Migrate the previous sync level associated with this mutex to
54*4882a593Smuzhiyun * the previous mutex on the list so that it may be preserved.
55*4882a593Smuzhiyun * This handles the case where several mutexes have been acquired
56*4882a593Smuzhiyun * at the same level, but are not released in opposite order.
57*4882a593Smuzhiyun */
58*4882a593Smuzhiyun (obj_desc->mutex.prev)->mutex.original_sync_level =
59*4882a593Smuzhiyun obj_desc->mutex.original_sync_level;
60*4882a593Smuzhiyun } else {
61*4882a593Smuzhiyun thread->acquired_mutex_list = obj_desc->mutex.next;
62*4882a593Smuzhiyun }
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
65*4882a593Smuzhiyun /*******************************************************************************
66*4882a593Smuzhiyun *
67*4882a593Smuzhiyun * FUNCTION: acpi_ex_link_mutex
68*4882a593Smuzhiyun *
69*4882a593Smuzhiyun * PARAMETERS: obj_desc - The mutex to be linked
70*4882a593Smuzhiyun * thread - Current executing thread object
71*4882a593Smuzhiyun *
72*4882a593Smuzhiyun * RETURN: None
73*4882a593Smuzhiyun *
74*4882a593Smuzhiyun * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
75*4882a593Smuzhiyun *
76*4882a593Smuzhiyun ******************************************************************************/
77*4882a593Smuzhiyun
78*4882a593Smuzhiyun static void
acpi_ex_link_mutex(union acpi_operand_object * obj_desc,struct acpi_thread_state * thread)79*4882a593Smuzhiyun acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
80*4882a593Smuzhiyun struct acpi_thread_state *thread)
81*4882a593Smuzhiyun {
82*4882a593Smuzhiyun union acpi_operand_object *list_head;
83*4882a593Smuzhiyun
84*4882a593Smuzhiyun list_head = thread->acquired_mutex_list;
85*4882a593Smuzhiyun
86*4882a593Smuzhiyun /* This object will be the first object in the list */
87*4882a593Smuzhiyun
88*4882a593Smuzhiyun obj_desc->mutex.prev = NULL;
89*4882a593Smuzhiyun obj_desc->mutex.next = list_head;
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun /* Update old first object to point back to this object */
92*4882a593Smuzhiyun
93*4882a593Smuzhiyun if (list_head) {
94*4882a593Smuzhiyun list_head->mutex.prev = obj_desc;
95*4882a593Smuzhiyun }
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* Update list head */
98*4882a593Smuzhiyun
99*4882a593Smuzhiyun thread->acquired_mutex_list = obj_desc;
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*******************************************************************************
103*4882a593Smuzhiyun *
104*4882a593Smuzhiyun * FUNCTION: acpi_ex_acquire_mutex_object
105*4882a593Smuzhiyun *
106*4882a593Smuzhiyun * PARAMETERS: timeout - Timeout in milliseconds
107*4882a593Smuzhiyun * obj_desc - Mutex object
108*4882a593Smuzhiyun * thread_id - Current thread state
109*4882a593Smuzhiyun *
110*4882a593Smuzhiyun * RETURN: Status
111*4882a593Smuzhiyun *
112*4882a593Smuzhiyun * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
113*4882a593Smuzhiyun * path that supports multiple acquires by the same thread.
114*4882a593Smuzhiyun *
115*4882a593Smuzhiyun * MUTEX: Interpreter must be locked
116*4882a593Smuzhiyun *
117*4882a593Smuzhiyun * NOTE: This interface is called from three places:
118*4882a593Smuzhiyun * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
119*4882a593Smuzhiyun * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
120*4882a593Smuzhiyun * global lock
121*4882a593Smuzhiyun * 3) From the external interface, acpi_acquire_global_lock
122*4882a593Smuzhiyun *
123*4882a593Smuzhiyun ******************************************************************************/
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun acpi_status
acpi_ex_acquire_mutex_object(u16 timeout,union acpi_operand_object * obj_desc,acpi_thread_id thread_id)126*4882a593Smuzhiyun acpi_ex_acquire_mutex_object(u16 timeout,
127*4882a593Smuzhiyun union acpi_operand_object *obj_desc,
128*4882a593Smuzhiyun acpi_thread_id thread_id)
129*4882a593Smuzhiyun {
130*4882a593Smuzhiyun acpi_status status;
131*4882a593Smuzhiyun
132*4882a593Smuzhiyun ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
133*4882a593Smuzhiyun
134*4882a593Smuzhiyun if (!obj_desc) {
135*4882a593Smuzhiyun return_ACPI_STATUS(AE_BAD_PARAMETER);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun
138*4882a593Smuzhiyun /* Support for multiple acquires by the owning thread */
139*4882a593Smuzhiyun
140*4882a593Smuzhiyun if (obj_desc->mutex.thread_id == thread_id) {
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * The mutex is already owned by this thread, just increment the
143*4882a593Smuzhiyun * acquisition depth
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth++;
146*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK);
147*4882a593Smuzhiyun }
148*4882a593Smuzhiyun
149*4882a593Smuzhiyun /* Acquire the mutex, wait if necessary. Special case for Global Lock */
150*4882a593Smuzhiyun
151*4882a593Smuzhiyun if (obj_desc == acpi_gbl_global_lock_mutex) {
152*4882a593Smuzhiyun status = acpi_ev_acquire_global_lock(timeout);
153*4882a593Smuzhiyun } else {
154*4882a593Smuzhiyun status =
155*4882a593Smuzhiyun acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
156*4882a593Smuzhiyun timeout);
157*4882a593Smuzhiyun }
158*4882a593Smuzhiyun
159*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
160*4882a593Smuzhiyun
161*4882a593Smuzhiyun /* Includes failure from a timeout on time_desc */
162*4882a593Smuzhiyun
163*4882a593Smuzhiyun return_ACPI_STATUS(status);
164*4882a593Smuzhiyun }
165*4882a593Smuzhiyun
166*4882a593Smuzhiyun /* Acquired the mutex: update mutex object */
167*4882a593Smuzhiyun
168*4882a593Smuzhiyun obj_desc->mutex.thread_id = thread_id;
169*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth = 1;
170*4882a593Smuzhiyun obj_desc->mutex.original_sync_level = 0;
171*4882a593Smuzhiyun obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK);
174*4882a593Smuzhiyun }
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun /*******************************************************************************
177*4882a593Smuzhiyun *
178*4882a593Smuzhiyun * FUNCTION: acpi_ex_acquire_mutex
179*4882a593Smuzhiyun *
180*4882a593Smuzhiyun * PARAMETERS: time_desc - Timeout integer
181*4882a593Smuzhiyun * obj_desc - Mutex object
182*4882a593Smuzhiyun * walk_state - Current method execution state
183*4882a593Smuzhiyun *
184*4882a593Smuzhiyun * RETURN: Status
185*4882a593Smuzhiyun *
186*4882a593Smuzhiyun * DESCRIPTION: Acquire an AML mutex
187*4882a593Smuzhiyun *
188*4882a593Smuzhiyun ******************************************************************************/
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun acpi_status
acpi_ex_acquire_mutex(union acpi_operand_object * time_desc,union acpi_operand_object * obj_desc,struct acpi_walk_state * walk_state)191*4882a593Smuzhiyun acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
192*4882a593Smuzhiyun union acpi_operand_object *obj_desc,
193*4882a593Smuzhiyun struct acpi_walk_state *walk_state)
194*4882a593Smuzhiyun {
195*4882a593Smuzhiyun acpi_status status;
196*4882a593Smuzhiyun
197*4882a593Smuzhiyun ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
198*4882a593Smuzhiyun
199*4882a593Smuzhiyun if (!obj_desc) {
200*4882a593Smuzhiyun return_ACPI_STATUS(AE_BAD_PARAMETER);
201*4882a593Smuzhiyun }
202*4882a593Smuzhiyun
203*4882a593Smuzhiyun /* Must have a valid thread state struct */
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun if (!walk_state->thread) {
206*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
207*4882a593Smuzhiyun "Cannot acquire Mutex [%4.4s], null thread info",
208*4882a593Smuzhiyun acpi_ut_get_node_name(obj_desc->mutex.node)));
209*4882a593Smuzhiyun return_ACPI_STATUS(AE_AML_INTERNAL);
210*4882a593Smuzhiyun }
211*4882a593Smuzhiyun
212*4882a593Smuzhiyun /*
213*4882a593Smuzhiyun * Current sync level must be less than or equal to the sync level
214*4882a593Smuzhiyun * of the mutex. This mechanism provides some deadlock prevention.
215*4882a593Smuzhiyun */
216*4882a593Smuzhiyun if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
217*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
218*4882a593Smuzhiyun "Cannot acquire Mutex [%4.4s], "
219*4882a593Smuzhiyun "current SyncLevel is too large (%u)",
220*4882a593Smuzhiyun acpi_ut_get_node_name(obj_desc->mutex.node),
221*4882a593Smuzhiyun walk_state->thread->current_sync_level));
222*4882a593Smuzhiyun return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
223*4882a593Smuzhiyun }
224*4882a593Smuzhiyun
225*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
226*4882a593Smuzhiyun "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
227*4882a593Smuzhiyun "Depth %u TID %p\n",
228*4882a593Smuzhiyun obj_desc->mutex.sync_level,
229*4882a593Smuzhiyun walk_state->thread->current_sync_level,
230*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth,
231*4882a593Smuzhiyun walk_state->thread));
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun status = acpi_ex_acquire_mutex_object((u16)time_desc->integer.value,
234*4882a593Smuzhiyun obj_desc,
235*4882a593Smuzhiyun walk_state->thread->thread_id);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
238*4882a593Smuzhiyun
239*4882a593Smuzhiyun /* Save Thread object, original/current sync levels */
240*4882a593Smuzhiyun
241*4882a593Smuzhiyun obj_desc->mutex.owner_thread = walk_state->thread;
242*4882a593Smuzhiyun obj_desc->mutex.original_sync_level =
243*4882a593Smuzhiyun walk_state->thread->current_sync_level;
244*4882a593Smuzhiyun walk_state->thread->current_sync_level =
245*4882a593Smuzhiyun obj_desc->mutex.sync_level;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun /* Link the mutex to the current thread for force-unlock at method exit */
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun acpi_ex_link_mutex(obj_desc, walk_state->thread);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun
252*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
253*4882a593Smuzhiyun "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
254*4882a593Smuzhiyun obj_desc->mutex.sync_level,
255*4882a593Smuzhiyun walk_state->thread->current_sync_level,
256*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth));
257*4882a593Smuzhiyun
258*4882a593Smuzhiyun return_ACPI_STATUS(status);
259*4882a593Smuzhiyun }
260*4882a593Smuzhiyun
261*4882a593Smuzhiyun /*******************************************************************************
262*4882a593Smuzhiyun *
263*4882a593Smuzhiyun * FUNCTION: acpi_ex_release_mutex_object
264*4882a593Smuzhiyun *
265*4882a593Smuzhiyun * PARAMETERS: obj_desc - The object descriptor for this op
266*4882a593Smuzhiyun *
267*4882a593Smuzhiyun * RETURN: Status
268*4882a593Smuzhiyun *
269*4882a593Smuzhiyun * DESCRIPTION: Release a previously acquired Mutex, low level interface.
270*4882a593Smuzhiyun * Provides a common path that supports multiple releases (after
271*4882a593Smuzhiyun * previous multiple acquires) by the same thread.
272*4882a593Smuzhiyun *
273*4882a593Smuzhiyun * MUTEX: Interpreter must be locked
274*4882a593Smuzhiyun *
275*4882a593Smuzhiyun * NOTE: This interface is called from three places:
276*4882a593Smuzhiyun * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
277*4882a593Smuzhiyun * 2) From acpi_ex_release_global_lock when an AML Field access requires the
278*4882a593Smuzhiyun * global lock
279*4882a593Smuzhiyun * 3) From the external interface, acpi_release_global_lock
280*4882a593Smuzhiyun *
281*4882a593Smuzhiyun ******************************************************************************/
282*4882a593Smuzhiyun
acpi_ex_release_mutex_object(union acpi_operand_object * obj_desc)283*4882a593Smuzhiyun acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
284*4882a593Smuzhiyun {
285*4882a593Smuzhiyun acpi_status status = AE_OK;
286*4882a593Smuzhiyun
287*4882a593Smuzhiyun ACPI_FUNCTION_TRACE(ex_release_mutex_object);
288*4882a593Smuzhiyun
289*4882a593Smuzhiyun if (obj_desc->mutex.acquisition_depth == 0) {
290*4882a593Smuzhiyun return_ACPI_STATUS(AE_NOT_ACQUIRED);
291*4882a593Smuzhiyun }
292*4882a593Smuzhiyun
293*4882a593Smuzhiyun /* Match multiple Acquires with multiple Releases */
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth--;
296*4882a593Smuzhiyun if (obj_desc->mutex.acquisition_depth != 0) {
297*4882a593Smuzhiyun
298*4882a593Smuzhiyun /* Just decrement the depth and return */
299*4882a593Smuzhiyun
300*4882a593Smuzhiyun return_ACPI_STATUS(AE_OK);
301*4882a593Smuzhiyun }
302*4882a593Smuzhiyun
303*4882a593Smuzhiyun if (obj_desc->mutex.owner_thread) {
304*4882a593Smuzhiyun
305*4882a593Smuzhiyun /* Unlink the mutex from the owner's list */
306*4882a593Smuzhiyun
307*4882a593Smuzhiyun acpi_ex_unlink_mutex(obj_desc);
308*4882a593Smuzhiyun obj_desc->mutex.owner_thread = NULL;
309*4882a593Smuzhiyun }
310*4882a593Smuzhiyun
311*4882a593Smuzhiyun /* Release the mutex, special case for Global Lock */
312*4882a593Smuzhiyun
313*4882a593Smuzhiyun if (obj_desc == acpi_gbl_global_lock_mutex) {
314*4882a593Smuzhiyun status = acpi_ev_release_global_lock();
315*4882a593Smuzhiyun } else {
316*4882a593Smuzhiyun acpi_os_release_mutex(obj_desc->mutex.os_mutex);
317*4882a593Smuzhiyun }
318*4882a593Smuzhiyun
319*4882a593Smuzhiyun /* Clear mutex info */
320*4882a593Smuzhiyun
321*4882a593Smuzhiyun obj_desc->mutex.thread_id = 0;
322*4882a593Smuzhiyun return_ACPI_STATUS(status);
323*4882a593Smuzhiyun }
324*4882a593Smuzhiyun
325*4882a593Smuzhiyun /*******************************************************************************
326*4882a593Smuzhiyun *
327*4882a593Smuzhiyun * FUNCTION: acpi_ex_release_mutex
328*4882a593Smuzhiyun *
329*4882a593Smuzhiyun * PARAMETERS: obj_desc - The object descriptor for this op
330*4882a593Smuzhiyun * walk_state - Current method execution state
331*4882a593Smuzhiyun *
332*4882a593Smuzhiyun * RETURN: Status
333*4882a593Smuzhiyun *
334*4882a593Smuzhiyun * DESCRIPTION: Release a previously acquired Mutex.
335*4882a593Smuzhiyun *
336*4882a593Smuzhiyun ******************************************************************************/
337*4882a593Smuzhiyun
338*4882a593Smuzhiyun acpi_status
acpi_ex_release_mutex(union acpi_operand_object * obj_desc,struct acpi_walk_state * walk_state)339*4882a593Smuzhiyun acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
340*4882a593Smuzhiyun struct acpi_walk_state *walk_state)
341*4882a593Smuzhiyun {
342*4882a593Smuzhiyun u8 previous_sync_level;
343*4882a593Smuzhiyun struct acpi_thread_state *owner_thread;
344*4882a593Smuzhiyun acpi_status status = AE_OK;
345*4882a593Smuzhiyun
346*4882a593Smuzhiyun ACPI_FUNCTION_TRACE(ex_release_mutex);
347*4882a593Smuzhiyun
348*4882a593Smuzhiyun if (!obj_desc) {
349*4882a593Smuzhiyun return_ACPI_STATUS(AE_BAD_PARAMETER);
350*4882a593Smuzhiyun }
351*4882a593Smuzhiyun
352*4882a593Smuzhiyun owner_thread = obj_desc->mutex.owner_thread;
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun /* The mutex must have been previously acquired in order to release it */
355*4882a593Smuzhiyun
356*4882a593Smuzhiyun if (!owner_thread) {
357*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
358*4882a593Smuzhiyun "Cannot release Mutex [%4.4s], not acquired",
359*4882a593Smuzhiyun acpi_ut_get_node_name(obj_desc->mutex.node)));
360*4882a593Smuzhiyun return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
361*4882a593Smuzhiyun }
362*4882a593Smuzhiyun
363*4882a593Smuzhiyun /* Must have a valid thread ID */
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun if (!walk_state->thread) {
366*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
367*4882a593Smuzhiyun "Cannot release Mutex [%4.4s], null thread info",
368*4882a593Smuzhiyun acpi_ut_get_node_name(obj_desc->mutex.node)));
369*4882a593Smuzhiyun return_ACPI_STATUS(AE_AML_INTERNAL);
370*4882a593Smuzhiyun }
371*4882a593Smuzhiyun
372*4882a593Smuzhiyun /*
373*4882a593Smuzhiyun * The Mutex is owned, but this thread must be the owner.
374*4882a593Smuzhiyun * Special case for Global Lock, any thread can release
375*4882a593Smuzhiyun */
376*4882a593Smuzhiyun if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
377*4882a593Smuzhiyun (obj_desc != acpi_gbl_global_lock_mutex)) {
378*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
379*4882a593Smuzhiyun "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
380*4882a593Smuzhiyun (u32)walk_state->thread->thread_id,
381*4882a593Smuzhiyun acpi_ut_get_node_name(obj_desc->mutex.node),
382*4882a593Smuzhiyun (u32)owner_thread->thread_id));
383*4882a593Smuzhiyun return_ACPI_STATUS(AE_AML_NOT_OWNER);
384*4882a593Smuzhiyun }
385*4882a593Smuzhiyun
386*4882a593Smuzhiyun /*
387*4882a593Smuzhiyun * The sync level of the mutex must be equal to the current sync level. In
388*4882a593Smuzhiyun * other words, the current level means that at least one mutex at that
389*4882a593Smuzhiyun * level is currently being held. Attempting to release a mutex of a
390*4882a593Smuzhiyun * different level can only mean that the mutex ordering rule is being
391*4882a593Smuzhiyun * violated. This behavior is clarified in ACPI 4.0 specification.
392*4882a593Smuzhiyun */
393*4882a593Smuzhiyun if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
394*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
395*4882a593Smuzhiyun "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
396*4882a593Smuzhiyun "mutex %u current %u",
397*4882a593Smuzhiyun acpi_ut_get_node_name(obj_desc->mutex.node),
398*4882a593Smuzhiyun obj_desc->mutex.sync_level,
399*4882a593Smuzhiyun walk_state->thread->current_sync_level));
400*4882a593Smuzhiyun return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun
403*4882a593Smuzhiyun /*
404*4882a593Smuzhiyun * Get the previous sync_level from the head of the acquired mutex list.
405*4882a593Smuzhiyun * This handles the case where several mutexes at the same level have been
406*4882a593Smuzhiyun * acquired, but are not released in reverse order.
407*4882a593Smuzhiyun */
408*4882a593Smuzhiyun previous_sync_level =
409*4882a593Smuzhiyun owner_thread->acquired_mutex_list->mutex.original_sync_level;
410*4882a593Smuzhiyun
411*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
412*4882a593Smuzhiyun "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
413*4882a593Smuzhiyun "Prev SyncLevel %u, Depth %u TID %p\n",
414*4882a593Smuzhiyun obj_desc->mutex.sync_level,
415*4882a593Smuzhiyun walk_state->thread->current_sync_level,
416*4882a593Smuzhiyun previous_sync_level,
417*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth,
418*4882a593Smuzhiyun walk_state->thread));
419*4882a593Smuzhiyun
420*4882a593Smuzhiyun status = acpi_ex_release_mutex_object(obj_desc);
421*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
422*4882a593Smuzhiyun return_ACPI_STATUS(status);
423*4882a593Smuzhiyun }
424*4882a593Smuzhiyun
425*4882a593Smuzhiyun if (obj_desc->mutex.acquisition_depth == 0) {
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun /* Restore the previous sync_level */
428*4882a593Smuzhiyun
429*4882a593Smuzhiyun owner_thread->current_sync_level = previous_sync_level;
430*4882a593Smuzhiyun }
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
433*4882a593Smuzhiyun "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
434*4882a593Smuzhiyun "Prev SyncLevel %u, Depth %u\n",
435*4882a593Smuzhiyun obj_desc->mutex.sync_level,
436*4882a593Smuzhiyun walk_state->thread->current_sync_level,
437*4882a593Smuzhiyun previous_sync_level,
438*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth));
439*4882a593Smuzhiyun
440*4882a593Smuzhiyun return_ACPI_STATUS(status);
441*4882a593Smuzhiyun }
442*4882a593Smuzhiyun
443*4882a593Smuzhiyun /*******************************************************************************
444*4882a593Smuzhiyun *
445*4882a593Smuzhiyun * FUNCTION: acpi_ex_release_all_mutexes
446*4882a593Smuzhiyun *
447*4882a593Smuzhiyun * PARAMETERS: thread - Current executing thread object
448*4882a593Smuzhiyun *
449*4882a593Smuzhiyun * RETURN: Status
450*4882a593Smuzhiyun *
451*4882a593Smuzhiyun * DESCRIPTION: Release all mutexes held by this thread
452*4882a593Smuzhiyun *
453*4882a593Smuzhiyun * NOTE: This function is called as the thread is exiting the interpreter.
454*4882a593Smuzhiyun * Mutexes are not released when an individual control method is exited, but
455*4882a593Smuzhiyun * only when the parent thread actually exits the interpreter. This allows one
456*4882a593Smuzhiyun * method to acquire a mutex, and a different method to release it, as long as
457*4882a593Smuzhiyun * this is performed underneath a single parent control method.
458*4882a593Smuzhiyun *
459*4882a593Smuzhiyun ******************************************************************************/
460*4882a593Smuzhiyun
acpi_ex_release_all_mutexes(struct acpi_thread_state * thread)461*4882a593Smuzhiyun void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun union acpi_operand_object *next = thread->acquired_mutex_list;
464*4882a593Smuzhiyun union acpi_operand_object *obj_desc;
465*4882a593Smuzhiyun
466*4882a593Smuzhiyun ACPI_FUNCTION_TRACE(ex_release_all_mutexes);
467*4882a593Smuzhiyun
468*4882a593Smuzhiyun /* Traverse the list of owned mutexes, releasing each one */
469*4882a593Smuzhiyun
470*4882a593Smuzhiyun while (next) {
471*4882a593Smuzhiyun obj_desc = next;
472*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
473*4882a593Smuzhiyun "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
474*4882a593Smuzhiyun obj_desc->mutex.node->name.ascii,
475*4882a593Smuzhiyun obj_desc->mutex.sync_level,
476*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth));
477*4882a593Smuzhiyun
478*4882a593Smuzhiyun /* Release the mutex, special case for Global Lock */
479*4882a593Smuzhiyun
480*4882a593Smuzhiyun if (obj_desc == acpi_gbl_global_lock_mutex) {
481*4882a593Smuzhiyun
482*4882a593Smuzhiyun /* Ignore errors */
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun (void)acpi_ev_release_global_lock();
485*4882a593Smuzhiyun } else {
486*4882a593Smuzhiyun acpi_os_release_mutex(obj_desc->mutex.os_mutex);
487*4882a593Smuzhiyun }
488*4882a593Smuzhiyun
489*4882a593Smuzhiyun /* Update Thread sync_level (Last mutex is the important one) */
490*4882a593Smuzhiyun
491*4882a593Smuzhiyun thread->current_sync_level =
492*4882a593Smuzhiyun obj_desc->mutex.original_sync_level;
493*4882a593Smuzhiyun
494*4882a593Smuzhiyun /* Mark mutex unowned */
495*4882a593Smuzhiyun
496*4882a593Smuzhiyun next = obj_desc->mutex.next;
497*4882a593Smuzhiyun
498*4882a593Smuzhiyun obj_desc->mutex.prev = NULL;
499*4882a593Smuzhiyun obj_desc->mutex.next = NULL;
500*4882a593Smuzhiyun obj_desc->mutex.acquisition_depth = 0;
501*4882a593Smuzhiyun obj_desc->mutex.owner_thread = NULL;
502*4882a593Smuzhiyun obj_desc->mutex.thread_id = 0;
503*4882a593Smuzhiyun }
504*4882a593Smuzhiyun
505*4882a593Smuzhiyun return_VOID;
506*4882a593Smuzhiyun }
507