1 /******************************************************************************
3 * Module Name: exmutex - ASL Mutex Acquire/Release functions
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2018, 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.
44 #include <acpi/acpi.h>
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME("exmutex")
52 /* Local prototypes */
54 acpi_ex_link_mutex(union acpi_operand_object
*obj_desc
,
55 struct acpi_thread_state
*thread
);
57 /*******************************************************************************
59 * FUNCTION: acpi_ex_unlink_mutex
61 * PARAMETERS: obj_desc - The mutex to be unlinked
65 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
67 ******************************************************************************/
69 void acpi_ex_unlink_mutex(union acpi_operand_object
*obj_desc
)
71 struct acpi_thread_state
*thread
= obj_desc
->mutex
.owner_thread
;
77 /* Doubly linked list */
79 if (obj_desc
->mutex
.next
) {
80 (obj_desc
->mutex
.next
)->mutex
.prev
= obj_desc
->mutex
.prev
;
83 if (obj_desc
->mutex
.prev
) {
84 (obj_desc
->mutex
.prev
)->mutex
.next
= obj_desc
->mutex
.next
;
87 * Migrate the previous sync level associated with this mutex to
88 * the previous mutex on the list so that it may be preserved.
89 * This handles the case where several mutexes have been acquired
90 * at the same level, but are not released in opposite order.
92 (obj_desc
->mutex
.prev
)->mutex
.original_sync_level
=
93 obj_desc
->mutex
.original_sync_level
;
95 thread
->acquired_mutex_list
= obj_desc
->mutex
.next
;
99 /*******************************************************************************
101 * FUNCTION: acpi_ex_link_mutex
103 * PARAMETERS: obj_desc - The mutex to be linked
104 * thread - Current executing thread object
108 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
110 ******************************************************************************/
113 acpi_ex_link_mutex(union acpi_operand_object
*obj_desc
,
114 struct acpi_thread_state
*thread
)
116 union acpi_operand_object
*list_head
;
118 list_head
= thread
->acquired_mutex_list
;
120 /* This object will be the first object in the list */
122 obj_desc
->mutex
.prev
= NULL
;
123 obj_desc
->mutex
.next
= list_head
;
125 /* Update old first object to point back to this object */
128 list_head
->mutex
.prev
= obj_desc
;
131 /* Update list head */
133 thread
->acquired_mutex_list
= obj_desc
;
136 /*******************************************************************************
138 * FUNCTION: acpi_ex_acquire_mutex_object
140 * PARAMETERS: timeout - Timeout in milliseconds
141 * obj_desc - Mutex object
142 * thread_id - Current thread state
146 * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
147 * path that supports multiple acquires by the same thread.
149 * MUTEX: Interpreter must be locked
151 * NOTE: This interface is called from three places:
152 * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
153 * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
155 * 3) From the external interface, acpi_acquire_global_lock
157 ******************************************************************************/
160 acpi_ex_acquire_mutex_object(u16 timeout
,
161 union acpi_operand_object
*obj_desc
,
162 acpi_thread_id thread_id
)
166 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object
, obj_desc
);
169 return_ACPI_STATUS(AE_BAD_PARAMETER
);
172 /* Support for multiple acquires by the owning thread */
174 if (obj_desc
->mutex
.thread_id
== thread_id
) {
176 * The mutex is already owned by this thread, just increment the
179 obj_desc
->mutex
.acquisition_depth
++;
180 return_ACPI_STATUS(AE_OK
);
183 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
185 if (obj_desc
== acpi_gbl_global_lock_mutex
) {
186 status
= acpi_ev_acquire_global_lock(timeout
);
189 acpi_ex_system_wait_mutex(obj_desc
->mutex
.os_mutex
,
193 if (ACPI_FAILURE(status
)) {
195 /* Includes failure from a timeout on time_desc */
197 return_ACPI_STATUS(status
);
200 /* Acquired the mutex: update mutex object */
202 obj_desc
->mutex
.thread_id
= thread_id
;
203 obj_desc
->mutex
.acquisition_depth
= 1;
204 obj_desc
->mutex
.original_sync_level
= 0;
205 obj_desc
->mutex
.owner_thread
= NULL
; /* Used only for AML Acquire() */
207 return_ACPI_STATUS(AE_OK
);
210 /*******************************************************************************
212 * FUNCTION: acpi_ex_acquire_mutex
214 * PARAMETERS: time_desc - Timeout integer
215 * obj_desc - Mutex object
216 * walk_state - Current method execution state
220 * DESCRIPTION: Acquire an AML mutex
222 ******************************************************************************/
225 acpi_ex_acquire_mutex(union acpi_operand_object
*time_desc
,
226 union acpi_operand_object
*obj_desc
,
227 struct acpi_walk_state
*walk_state
)
231 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex
, obj_desc
);
234 return_ACPI_STATUS(AE_BAD_PARAMETER
);
237 /* Must have a valid thread state struct */
239 if (!walk_state
->thread
) {
241 "Cannot acquire Mutex [%4.4s], null thread info",
242 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
243 return_ACPI_STATUS(AE_AML_INTERNAL
);
247 * Current sync level must be less than or equal to the sync level
248 * of the mutex. This mechanism provides some deadlock prevention.
250 if (walk_state
->thread
->current_sync_level
> obj_desc
->mutex
.sync_level
) {
252 "Cannot acquire Mutex [%4.4s], "
253 "current SyncLevel is too large (%u)",
254 acpi_ut_get_node_name(obj_desc
->mutex
.node
),
255 walk_state
->thread
->current_sync_level
));
256 return_ACPI_STATUS(AE_AML_MUTEX_ORDER
);
259 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
260 "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
262 obj_desc
->mutex
.sync_level
,
263 walk_state
->thread
->current_sync_level
,
264 obj_desc
->mutex
.acquisition_depth
,
265 walk_state
->thread
));
267 status
= acpi_ex_acquire_mutex_object((u16
)time_desc
->integer
.value
,
269 walk_state
->thread
->thread_id
);
271 if (ACPI_SUCCESS(status
) && obj_desc
->mutex
.acquisition_depth
== 1) {
273 /* Save Thread object, original/current sync levels */
275 obj_desc
->mutex
.owner_thread
= walk_state
->thread
;
276 obj_desc
->mutex
.original_sync_level
=
277 walk_state
->thread
->current_sync_level
;
278 walk_state
->thread
->current_sync_level
=
279 obj_desc
->mutex
.sync_level
;
281 /* Link the mutex to the current thread for force-unlock at method exit */
283 acpi_ex_link_mutex(obj_desc
, walk_state
->thread
);
286 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
287 "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
288 obj_desc
->mutex
.sync_level
,
289 walk_state
->thread
->current_sync_level
,
290 obj_desc
->mutex
.acquisition_depth
));
292 return_ACPI_STATUS(status
);
295 /*******************************************************************************
297 * FUNCTION: acpi_ex_release_mutex_object
299 * PARAMETERS: obj_desc - The object descriptor for this op
303 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
304 * Provides a common path that supports multiple releases (after
305 * previous multiple acquires) by the same thread.
307 * MUTEX: Interpreter must be locked
309 * NOTE: This interface is called from three places:
310 * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
311 * 2) From acpi_ex_release_global_lock when an AML Field access requires the
313 * 3) From the external interface, acpi_release_global_lock
315 ******************************************************************************/
317 acpi_status
acpi_ex_release_mutex_object(union acpi_operand_object
*obj_desc
)
319 acpi_status status
= AE_OK
;
321 ACPI_FUNCTION_TRACE(ex_release_mutex_object
);
323 if (obj_desc
->mutex
.acquisition_depth
== 0) {
324 return_ACPI_STATUS(AE_NOT_ACQUIRED
);
327 /* Match multiple Acquires with multiple Releases */
329 obj_desc
->mutex
.acquisition_depth
--;
330 if (obj_desc
->mutex
.acquisition_depth
!= 0) {
332 /* Just decrement the depth and return */
334 return_ACPI_STATUS(AE_OK
);
337 if (obj_desc
->mutex
.owner_thread
) {
339 /* Unlink the mutex from the owner's list */
341 acpi_ex_unlink_mutex(obj_desc
);
342 obj_desc
->mutex
.owner_thread
= NULL
;
345 /* Release the mutex, special case for Global Lock */
347 if (obj_desc
== acpi_gbl_global_lock_mutex
) {
348 status
= acpi_ev_release_global_lock();
350 acpi_os_release_mutex(obj_desc
->mutex
.os_mutex
);
353 /* Clear mutex info */
355 obj_desc
->mutex
.thread_id
= 0;
356 return_ACPI_STATUS(status
);
359 /*******************************************************************************
361 * FUNCTION: acpi_ex_release_mutex
363 * PARAMETERS: obj_desc - The object descriptor for this op
364 * walk_state - Current method execution state
368 * DESCRIPTION: Release a previously acquired Mutex.
370 ******************************************************************************/
373 acpi_ex_release_mutex(union acpi_operand_object
*obj_desc
,
374 struct acpi_walk_state
*walk_state
)
376 u8 previous_sync_level
;
377 struct acpi_thread_state
*owner_thread
;
378 acpi_status status
= AE_OK
;
380 ACPI_FUNCTION_TRACE(ex_release_mutex
);
383 return_ACPI_STATUS(AE_BAD_PARAMETER
);
386 owner_thread
= obj_desc
->mutex
.owner_thread
;
388 /* The mutex must have been previously acquired in order to release it */
392 "Cannot release Mutex [%4.4s], not acquired",
393 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
394 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED
);
397 /* Must have a valid thread ID */
399 if (!walk_state
->thread
) {
401 "Cannot release Mutex [%4.4s], null thread info",
402 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
403 return_ACPI_STATUS(AE_AML_INTERNAL
);
407 * The Mutex is owned, but this thread must be the owner.
408 * Special case for Global Lock, any thread can release
410 if ((owner_thread
->thread_id
!= walk_state
->thread
->thread_id
) &&
411 (obj_desc
!= acpi_gbl_global_lock_mutex
)) {
413 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
414 (u32
)walk_state
->thread
->thread_id
,
415 acpi_ut_get_node_name(obj_desc
->mutex
.node
),
416 (u32
)owner_thread
->thread_id
));
417 return_ACPI_STATUS(AE_AML_NOT_OWNER
);
421 * The sync level of the mutex must be equal to the current sync level. In
422 * other words, the current level means that at least one mutex at that
423 * level is currently being held. Attempting to release a mutex of a
424 * different level can only mean that the mutex ordering rule is being
425 * violated. This behavior is clarified in ACPI 4.0 specification.
427 if (obj_desc
->mutex
.sync_level
!= owner_thread
->current_sync_level
) {
429 "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
430 "mutex %u current %u",
431 acpi_ut_get_node_name(obj_desc
->mutex
.node
),
432 obj_desc
->mutex
.sync_level
,
433 walk_state
->thread
->current_sync_level
));
434 return_ACPI_STATUS(AE_AML_MUTEX_ORDER
);
438 * Get the previous sync_level from the head of the acquired mutex list.
439 * This handles the case where several mutexes at the same level have been
440 * acquired, but are not released in reverse order.
442 previous_sync_level
=
443 owner_thread
->acquired_mutex_list
->mutex
.original_sync_level
;
445 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
446 "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
447 "Prev SyncLevel %u, Depth %u TID %p\n",
448 obj_desc
->mutex
.sync_level
,
449 walk_state
->thread
->current_sync_level
,
451 obj_desc
->mutex
.acquisition_depth
,
452 walk_state
->thread
));
454 status
= acpi_ex_release_mutex_object(obj_desc
);
455 if (ACPI_FAILURE(status
)) {
456 return_ACPI_STATUS(status
);
459 if (obj_desc
->mutex
.acquisition_depth
== 0) {
461 /* Restore the previous sync_level */
463 owner_thread
->current_sync_level
= previous_sync_level
;
466 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
467 "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
468 "Prev SyncLevel %u, Depth %u\n",
469 obj_desc
->mutex
.sync_level
,
470 walk_state
->thread
->current_sync_level
,
472 obj_desc
->mutex
.acquisition_depth
));
474 return_ACPI_STATUS(status
);
477 /*******************************************************************************
479 * FUNCTION: acpi_ex_release_all_mutexes
481 * PARAMETERS: thread - Current executing thread object
485 * DESCRIPTION: Release all mutexes held by this thread
487 * NOTE: This function is called as the thread is exiting the interpreter.
488 * Mutexes are not released when an individual control method is exited, but
489 * only when the parent thread actually exits the interpreter. This allows one
490 * method to acquire a mutex, and a different method to release it, as long as
491 * this is performed underneath a single parent control method.
493 ******************************************************************************/
495 void acpi_ex_release_all_mutexes(struct acpi_thread_state
*thread
)
497 union acpi_operand_object
*next
= thread
->acquired_mutex_list
;
498 union acpi_operand_object
*obj_desc
;
500 ACPI_FUNCTION_TRACE(ex_release_all_mutexes
);
502 /* Traverse the list of owned mutexes, releasing each one */
506 ACPI_DEBUG_PRINT((ACPI_DB_EXEC
,
507 "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
508 obj_desc
->mutex
.node
->name
.ascii
,
509 obj_desc
->mutex
.sync_level
,
510 obj_desc
->mutex
.acquisition_depth
));
512 /* Release the mutex, special case for Global Lock */
514 if (obj_desc
== acpi_gbl_global_lock_mutex
) {
518 (void)acpi_ev_release_global_lock();
520 acpi_os_release_mutex(obj_desc
->mutex
.os_mutex
);
523 /* Update Thread sync_level (Last mutex is the important one) */
525 thread
->current_sync_level
=
526 obj_desc
->mutex
.original_sync_level
;
528 /* Mark mutex unowned */
530 next
= obj_desc
->mutex
.next
;
532 obj_desc
->mutex
.prev
= NULL
;
533 obj_desc
->mutex
.next
= NULL
;
534 obj_desc
->mutex
.acquisition_depth
= 0;
535 obj_desc
->mutex
.owner_thread
= NULL
;
536 obj_desc
->mutex
.thread_id
= 0;