2 /******************************************************************************
4 * Module Name: exmutex - ASL Mutex Acquire/Release functions
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2012, 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.
45 #include <acpi/acpi.h>
50 #define _COMPONENT ACPI_EXECUTER
51 ACPI_MODULE_NAME("exmutex")
53 /* Local prototypes */
55 acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
56 struct acpi_thread_state *thread);
58 /*******************************************************************************
60 * FUNCTION: acpi_ex_unlink_mutex
62 * PARAMETERS: obj_desc - The mutex to be unlinked
66 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
68 ******************************************************************************/
70 void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
72 struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
78 /* Doubly linked list */
80 if (obj_desc->mutex.next) {
81 (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
84 if (obj_desc->mutex.prev) {
85 (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
88 * Migrate the previous sync level associated with this mutex to
89 * the previous mutex on the list so that it may be preserved.
90 * This handles the case where several mutexes have been acquired
91 * at the same level, but are not released in opposite order.
93 (obj_desc->mutex.prev)->mutex.original_sync_level =
94 obj_desc->mutex.original_sync_level;
96 thread->acquired_mutex_list = obj_desc->mutex.next;
100 /*******************************************************************************
102 * FUNCTION: acpi_ex_link_mutex
104 * PARAMETERS: obj_desc - The mutex to be linked
105 * thread - Current executing thread object
109 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
111 ******************************************************************************/
114 acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
115 struct acpi_thread_state *thread)
117 union acpi_operand_object *list_head;
119 list_head = thread->acquired_mutex_list;
121 /* This object will be the first object in the list */
123 obj_desc->mutex.prev = NULL;
124 obj_desc->mutex.next = list_head;
126 /* Update old first object to point back to this object */
129 list_head->mutex.prev = obj_desc;
132 /* Update list head */
134 thread->acquired_mutex_list = obj_desc;
137 /*******************************************************************************
139 * FUNCTION: acpi_ex_acquire_mutex_object
141 * PARAMETERS: timeout - Timeout in milliseconds
142 * obj_desc - Mutex object
143 * thread_id - Current thread state
147 * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
148 * path that supports multiple acquires by the same thread.
150 * MUTEX: Interpreter must be locked
152 * NOTE: This interface is called from three places:
153 * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
154 * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
156 * 3) From the external interface, acpi_acquire_global_lock
158 ******************************************************************************/
161 acpi_ex_acquire_mutex_object(u16 timeout,
162 union acpi_operand_object *obj_desc,
163 acpi_thread_id thread_id)
167 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
170 return_ACPI_STATUS(AE_BAD_PARAMETER);
173 /* Support for multiple acquires by the owning thread */
175 if (obj_desc->mutex.thread_id == thread_id) {
177 * The mutex is already owned by this thread, just increment the
180 obj_desc->mutex.acquisition_depth++;
181 return_ACPI_STATUS(AE_OK);
184 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
186 if (obj_desc == acpi_gbl_global_lock_mutex) {
187 status = acpi_ev_acquire_global_lock(timeout);
189 status = 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 of the
248 * 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], current SyncLevel is too large (%u)",
253 acpi_ut_get_node_name(obj_desc->mutex.node),
254 walk_state->thread->current_sync_level));
255 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
258 status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value,
260 walk_state->thread->thread_id);
261 if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
263 /* Save Thread object, original/current sync levels */
265 obj_desc->mutex.owner_thread = walk_state->thread;
266 obj_desc->mutex.original_sync_level =
267 walk_state->thread->current_sync_level;
268 walk_state->thread->current_sync_level =
269 obj_desc->mutex.sync_level;
271 /* Link the mutex to the current thread for force-unlock at method exit */
273 acpi_ex_link_mutex(obj_desc, walk_state->thread);
276 return_ACPI_STATUS(status);
279 /*******************************************************************************
281 * FUNCTION: acpi_ex_release_mutex_object
283 * PARAMETERS: obj_desc - The object descriptor for this op
287 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
288 * Provides a common path that supports multiple releases (after
289 * previous multiple acquires) by the same thread.
291 * MUTEX: Interpreter must be locked
293 * NOTE: This interface is called from three places:
294 * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
295 * 2) From acpi_ex_release_global_lock when an AML Field access requires the
297 * 3) From the external interface, acpi_release_global_lock
299 ******************************************************************************/
301 acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
303 acpi_status status = AE_OK;
305 ACPI_FUNCTION_TRACE(ex_release_mutex_object);
307 if (obj_desc->mutex.acquisition_depth == 0) {
308 return (AE_NOT_ACQUIRED);
311 /* Match multiple Acquires with multiple Releases */
313 obj_desc->mutex.acquisition_depth--;
314 if (obj_desc->mutex.acquisition_depth != 0) {
316 /* Just decrement the depth and return */
318 return_ACPI_STATUS(AE_OK);
321 if (obj_desc->mutex.owner_thread) {
323 /* Unlink the mutex from the owner's list */
325 acpi_ex_unlink_mutex(obj_desc);
326 obj_desc->mutex.owner_thread = NULL;
329 /* Release the mutex, special case for Global Lock */
331 if (obj_desc == acpi_gbl_global_lock_mutex) {
332 status = acpi_ev_release_global_lock();
334 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
337 /* Clear mutex info */
339 obj_desc->mutex.thread_id = 0;
340 return_ACPI_STATUS(status);
343 /*******************************************************************************
345 * FUNCTION: acpi_ex_release_mutex
347 * PARAMETERS: obj_desc - The object descriptor for this op
348 * walk_state - Current method execution state
352 * DESCRIPTION: Release a previously acquired Mutex.
354 ******************************************************************************/
357 acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
358 struct acpi_walk_state *walk_state)
360 acpi_status status = AE_OK;
361 u8 previous_sync_level;
362 struct acpi_thread_state *owner_thread;
364 ACPI_FUNCTION_TRACE(ex_release_mutex);
367 return_ACPI_STATUS(AE_BAD_PARAMETER);
370 owner_thread = obj_desc->mutex.owner_thread;
372 /* The mutex must have been previously acquired in order to release it */
376 "Cannot release Mutex [%4.4s], not acquired",
377 acpi_ut_get_node_name(obj_desc->mutex.node)));
378 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
381 /* Must have a valid thread. */
382 if (!walk_state->thread) {
384 "Cannot release Mutex [%4.4s], null thread info",
385 acpi_ut_get_node_name(obj_desc->mutex.node)));
386 return_ACPI_STATUS(AE_AML_INTERNAL);
390 * The Mutex is owned, but this thread must be the owner.
391 * Special case for Global Lock, any thread can release
393 if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
394 (obj_desc != acpi_gbl_global_lock_mutex)) {
396 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
397 (u32)walk_state->thread->thread_id,
398 acpi_ut_get_node_name(obj_desc->mutex.node),
399 (u32)owner_thread->thread_id));
400 return_ACPI_STATUS(AE_AML_NOT_OWNER);
404 * The sync level of the mutex must be equal to the current sync level. In
405 * other words, the current level means that at least one mutex at that
406 * level is currently being held. Attempting to release a mutex of a
407 * different level can only mean that the mutex ordering rule is being
408 * violated. This behavior is clarified in ACPI 4.0 specification.
410 if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
412 "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %u current %u",
413 acpi_ut_get_node_name(obj_desc->mutex.node),
414 obj_desc->mutex.sync_level,
415 walk_state->thread->current_sync_level));
416 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
420 * Get the previous sync_level from the head of the acquired mutex list.
421 * This handles the case where several mutexes at the same level have been
422 * acquired, but are not released in reverse order.
424 previous_sync_level =
425 owner_thread->acquired_mutex_list->mutex.original_sync_level;
427 status = acpi_ex_release_mutex_object(obj_desc);
428 if (ACPI_FAILURE(status)) {
429 return_ACPI_STATUS(status);
432 if (obj_desc->mutex.acquisition_depth == 0) {
434 /* Restore the previous sync_level */
436 owner_thread->current_sync_level = previous_sync_level;
439 return_ACPI_STATUS(status);
442 /*******************************************************************************
444 * FUNCTION: acpi_ex_release_all_mutexes
446 * PARAMETERS: thread - Current executing thread object
450 * DESCRIPTION: Release all mutexes held by this thread
452 * NOTE: This function is called as the thread is exiting the interpreter.
453 * Mutexes are not released when an individual control method is exited, but
454 * only when the parent thread actually exits the interpreter. This allows one
455 * method to acquire a mutex, and a different method to release it, as long as
456 * this is performed underneath a single parent control method.
458 ******************************************************************************/
460 void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
462 union acpi_operand_object *next = thread->acquired_mutex_list;
463 union acpi_operand_object *obj_desc;
465 ACPI_FUNCTION_ENTRY();
467 /* Traverse the list of owned mutexes, releasing each one */
471 next = obj_desc->mutex.next;
473 obj_desc->mutex.prev = NULL;
474 obj_desc->mutex.next = NULL;
475 obj_desc->mutex.acquisition_depth = 0;
477 /* Release the mutex, special case for Global Lock */
479 if (obj_desc == acpi_gbl_global_lock_mutex) {
483 (void)acpi_ev_release_global_lock();
485 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
488 /* Mark mutex unowned */
490 obj_desc->mutex.owner_thread = NULL;
491 obj_desc->mutex.thread_id = 0;
493 /* Update Thread sync_level (Last mutex is the important one) */
495 thread->current_sync_level =
496 obj_desc->mutex.original_sync_level;