2 /******************************************************************************
4 * Module Name: exmutex - ASL Mutex Acquire/Release functions
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2008, 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;
87 thread->acquired_mutex_list = obj_desc->mutex.next;
91 /*******************************************************************************
93 * FUNCTION: acpi_ex_link_mutex
95 * PARAMETERS: obj_desc - The mutex to be linked
96 * Thread - Current executing thread object
100 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
102 ******************************************************************************/
105 acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
106 struct acpi_thread_state *thread)
108 union acpi_operand_object *list_head;
110 list_head = thread->acquired_mutex_list;
112 /* This object will be the first object in the list */
114 obj_desc->mutex.prev = NULL;
115 obj_desc->mutex.next = list_head;
117 /* Update old first object to point back to this object */
120 list_head->mutex.prev = obj_desc;
123 /* Update list head */
125 thread->acquired_mutex_list = obj_desc;
128 /*******************************************************************************
130 * FUNCTION: acpi_ex_acquire_mutex_object
132 * PARAMETERS: time_desc - Timeout in milliseconds
133 * obj_desc - Mutex object
134 * Thread - Current thread state
138 * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
139 * path that supports multiple acquires by the same thread.
141 * MUTEX: Interpreter must be locked
143 * NOTE: This interface is called from three places:
144 * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
145 * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
147 * 3) From the external interface, acpi_acquire_global_lock
149 ******************************************************************************/
152 acpi_ex_acquire_mutex_object(u16 timeout,
153 union acpi_operand_object *obj_desc,
154 acpi_thread_id thread_id)
158 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
161 return_ACPI_STATUS(AE_BAD_PARAMETER);
164 /* Support for multiple acquires by the owning thread */
166 if (obj_desc->mutex.thread_id == thread_id) {
168 * The mutex is already owned by this thread, just increment the
171 obj_desc->mutex.acquisition_depth++;
172 return_ACPI_STATUS(AE_OK);
175 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
177 if (obj_desc == acpi_gbl_global_lock_mutex) {
178 status = acpi_ev_acquire_global_lock(timeout);
180 status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
184 if (ACPI_FAILURE(status)) {
186 /* Includes failure from a timeout on time_desc */
188 return_ACPI_STATUS(status);
191 /* Acquired the mutex: update mutex object */
193 obj_desc->mutex.thread_id = thread_id;
194 obj_desc->mutex.acquisition_depth = 1;
195 obj_desc->mutex.original_sync_level = 0;
196 obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */
198 return_ACPI_STATUS(AE_OK);
201 /*******************************************************************************
203 * FUNCTION: acpi_ex_acquire_mutex
205 * PARAMETERS: time_desc - Timeout integer
206 * obj_desc - Mutex object
207 * walk_state - Current method execution state
211 * DESCRIPTION: Acquire an AML mutex
213 ******************************************************************************/
216 acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
217 union acpi_operand_object *obj_desc,
218 struct acpi_walk_state *walk_state)
222 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
225 return_ACPI_STATUS(AE_BAD_PARAMETER);
228 /* Must have a valid thread ID */
230 if (!walk_state->thread) {
232 "Cannot acquire Mutex [%4.4s], null thread info",
233 acpi_ut_get_node_name(obj_desc->mutex.node)));
234 return_ACPI_STATUS(AE_AML_INTERNAL);
238 * Current sync level must be less than or equal to the sync level of the
239 * mutex. This mechanism provides some deadlock prevention
241 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
243 "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)",
244 acpi_ut_get_node_name(obj_desc->mutex.node),
245 walk_state->thread->current_sync_level));
246 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
249 status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value,
251 walk_state->thread->thread_id);
252 if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
254 /* Save Thread object, original/current sync levels */
256 obj_desc->mutex.owner_thread = walk_state->thread;
257 obj_desc->mutex.original_sync_level =
258 walk_state->thread->current_sync_level;
259 walk_state->thread->current_sync_level =
260 obj_desc->mutex.sync_level;
262 /* Link the mutex to the current thread for force-unlock at method exit */
264 acpi_ex_link_mutex(obj_desc, walk_state->thread);
267 return_ACPI_STATUS(status);
270 /*******************************************************************************
272 * FUNCTION: acpi_ex_release_mutex_object
274 * PARAMETERS: obj_desc - The object descriptor for this op
278 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
279 * Provides a common path that supports multiple releases (after
280 * previous multiple acquires) by the same thread.
282 * MUTEX: Interpreter must be locked
284 * NOTE: This interface is called from three places:
285 * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
286 * 2) From acpi_ex_release_global_lock when an AML Field access requires the
288 * 3) From the external interface, acpi_release_global_lock
290 ******************************************************************************/
292 acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
294 acpi_status status = AE_OK;
296 ACPI_FUNCTION_TRACE(ex_release_mutex_object);
298 if (obj_desc->mutex.acquisition_depth == 0) {
299 return (AE_NOT_ACQUIRED);
302 /* Match multiple Acquires with multiple Releases */
304 obj_desc->mutex.acquisition_depth--;
305 if (obj_desc->mutex.acquisition_depth != 0) {
307 /* Just decrement the depth and return */
309 return_ACPI_STATUS(AE_OK);
312 if (obj_desc->mutex.owner_thread) {
314 /* Unlink the mutex from the owner's list */
316 acpi_ex_unlink_mutex(obj_desc);
317 obj_desc->mutex.owner_thread = NULL;
320 /* Release the mutex, special case for Global Lock */
322 if (obj_desc == acpi_gbl_global_lock_mutex) {
323 status = acpi_ev_release_global_lock();
325 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
328 /* Clear mutex info */
330 obj_desc->mutex.thread_id = NULL;
331 return_ACPI_STATUS(status);
334 /*******************************************************************************
336 * FUNCTION: acpi_ex_release_mutex
338 * PARAMETERS: obj_desc - The object descriptor for this op
339 * walk_state - Current method execution state
343 * DESCRIPTION: Release a previously acquired Mutex.
345 ******************************************************************************/
348 acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
349 struct acpi_walk_state *walk_state)
351 acpi_status status = AE_OK;
353 ACPI_FUNCTION_TRACE(ex_release_mutex);
356 return_ACPI_STATUS(AE_BAD_PARAMETER);
359 /* The mutex must have been previously acquired in order to release it */
361 if (!obj_desc->mutex.owner_thread) {
363 "Cannot release Mutex [%4.4s], not acquired",
364 acpi_ut_get_node_name(obj_desc->mutex.node)));
365 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
369 * The Mutex is owned, but this thread must be the owner.
370 * Special case for Global Lock, any thread can release
372 if ((obj_desc->mutex.owner_thread->thread_id !=
373 walk_state->thread->thread_id)
374 && (obj_desc != acpi_gbl_global_lock_mutex)) {
376 "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX",
377 (unsigned long)walk_state->thread->thread_id,
378 acpi_ut_get_node_name(obj_desc->mutex.node),
379 (unsigned long)obj_desc->mutex.owner_thread->
381 return_ACPI_STATUS(AE_AML_NOT_OWNER);
384 /* Must have a valid thread ID */
386 if (!walk_state->thread) {
388 "Cannot release Mutex [%4.4s], null thread info",
389 acpi_ut_get_node_name(obj_desc->mutex.node)));
390 return_ACPI_STATUS(AE_AML_INTERNAL);
394 * The sync level of the mutex must be less than or equal to the current
397 if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
399 "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %d current %d",
400 acpi_ut_get_node_name(obj_desc->mutex.node),
401 obj_desc->mutex.sync_level,
402 walk_state->thread->current_sync_level));
403 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
406 status = acpi_ex_release_mutex_object(obj_desc);
408 if (obj_desc->mutex.acquisition_depth == 0) {
410 /* Restore the original sync_level */
412 walk_state->thread->current_sync_level =
413 obj_desc->mutex.original_sync_level;
415 return_ACPI_STATUS(status);
418 /*******************************************************************************
420 * FUNCTION: acpi_ex_release_all_mutexes
422 * PARAMETERS: Thread - Current executing thread object
426 * DESCRIPTION: Release all mutexes held by this thread
428 * NOTE: This function is called as the thread is exiting the interpreter.
429 * Mutexes are not released when an individual control method is exited, but
430 * only when the parent thread actually exits the interpreter. This allows one
431 * method to acquire a mutex, and a different method to release it, as long as
432 * this is performed underneath a single parent control method.
434 ******************************************************************************/
436 void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
438 union acpi_operand_object *next = thread->acquired_mutex_list;
439 union acpi_operand_object *obj_desc;
441 ACPI_FUNCTION_ENTRY();
443 /* Traverse the list of owned mutexes, releasing each one */
447 next = obj_desc->mutex.next;
449 obj_desc->mutex.prev = NULL;
450 obj_desc->mutex.next = NULL;
451 obj_desc->mutex.acquisition_depth = 0;
453 /* Release the mutex, special case for Global Lock */
455 if (obj_desc == acpi_gbl_global_lock_mutex) {
459 (void)acpi_ev_release_global_lock();
461 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
464 /* Mark mutex unowned */
466 obj_desc->mutex.owner_thread = NULL;
467 obj_desc->mutex.thread_id = NULL;
469 /* Update Thread sync_level (Last mutex is the important one) */
471 thread->current_sync_level =
472 obj_desc->mutex.original_sync_level;