2 /******************************************************************************
4 * Module Name: exmutex - ASL Mutex Acquire/Release functions
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2005, R. Byron Moore
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.
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME ("exmutex")
53 /*******************************************************************************
55 * FUNCTION: acpi_ex_unlink_mutex
57 * PARAMETERS: obj_desc - The mutex to be unlinked
61 * DESCRIPTION: Remove a mutex from the "acquired_mutex" list
63 ******************************************************************************/
66 acpi_ex_unlink_mutex (
67 union acpi_operand_object *obj_desc)
69 struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
76 /* Doubly linked list */
78 if (obj_desc->mutex.next) {
79 (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
82 if (obj_desc->mutex.prev) {
83 (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
86 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 * list_head - head of the "acquired_mutex" list
100 * DESCRIPTION: Add a mutex to the "acquired_mutex" list for this walk
102 ******************************************************************************/
106 union acpi_operand_object *obj_desc,
107 struct acpi_thread_state *thread)
109 union acpi_operand_object *list_head;
112 list_head = thread->acquired_mutex_list;
114 /* This object will be the first object in the list */
116 obj_desc->mutex.prev = NULL;
117 obj_desc->mutex.next = list_head;
119 /* Update old first object to point back to this object */
122 list_head->mutex.prev = obj_desc;
125 /* Update list head */
127 thread->acquired_mutex_list = obj_desc;
131 /*******************************************************************************
133 * FUNCTION: acpi_ex_acquire_mutex
135 * PARAMETERS: time_desc - The 'time to delay' object descriptor
136 * obj_desc - The object descriptor for this op
140 * DESCRIPTION: Acquire an AML mutex
142 ******************************************************************************/
145 acpi_ex_acquire_mutex (
146 union acpi_operand_object *time_desc,
147 union acpi_operand_object *obj_desc,
148 struct acpi_walk_state *walk_state)
153 ACPI_FUNCTION_TRACE_PTR ("ex_acquire_mutex", obj_desc);
157 return_ACPI_STATUS (AE_BAD_PARAMETER);
160 /* Sanity check -- we must have a valid thread ID */
162 if (!walk_state->thread) {
163 ACPI_REPORT_ERROR (("Cannot acquire Mutex [%4.4s], null thread info\n",
164 acpi_ut_get_node_name (obj_desc->mutex.node)));
165 return_ACPI_STATUS (AE_AML_INTERNAL);
169 * Current Sync must be less than or equal to the sync level of the
170 * mutex. This mechanism provides some deadlock prevention
172 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
173 ACPI_REPORT_ERROR (("Cannot acquire Mutex [%4.4s], incorrect sync_level\n",
174 acpi_ut_get_node_name (obj_desc->mutex.node)));
175 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
178 /* Support for multiple acquires by the owning thread */
180 if (obj_desc->mutex.owner_thread) {
181 /* Special case for Global Lock, allow all threads */
183 if ((obj_desc->mutex.owner_thread->thread_id == walk_state->thread->thread_id) ||
184 (obj_desc->mutex.semaphore == acpi_gbl_global_lock_semaphore)) {
186 * The mutex is already owned by this thread,
187 * just increment the acquisition depth
189 obj_desc->mutex.acquisition_depth++;
190 return_ACPI_STATUS (AE_OK);
194 /* Acquire the mutex, wait if necessary */
196 status = acpi_ex_system_acquire_mutex (time_desc, obj_desc);
197 if (ACPI_FAILURE (status)) {
198 /* Includes failure from a timeout on time_desc */
200 return_ACPI_STATUS (status);
203 /* Have the mutex: update mutex and walk info and save the sync_level */
205 obj_desc->mutex.owner_thread = walk_state->thread;
206 obj_desc->mutex.acquisition_depth = 1;
207 obj_desc->mutex.original_sync_level = walk_state->thread->current_sync_level;
209 walk_state->thread->current_sync_level = obj_desc->mutex.sync_level;
211 /* Link the mutex to the current thread for force-unlock at method exit */
213 acpi_ex_link_mutex (obj_desc, walk_state->thread);
215 return_ACPI_STATUS (AE_OK);
219 /*******************************************************************************
221 * FUNCTION: acpi_ex_release_mutex
223 * PARAMETERS: obj_desc - The object descriptor for this op
227 * DESCRIPTION: Release a previously acquired Mutex.
229 ******************************************************************************/
232 acpi_ex_release_mutex (
233 union acpi_operand_object *obj_desc,
234 struct acpi_walk_state *walk_state)
239 ACPI_FUNCTION_TRACE ("ex_release_mutex");
243 return_ACPI_STATUS (AE_BAD_PARAMETER);
246 /* The mutex must have been previously acquired in order to release it */
248 if (!obj_desc->mutex.owner_thread) {
249 ACPI_REPORT_ERROR (("Cannot release Mutex [%4.4s], not acquired\n",
250 acpi_ut_get_node_name (obj_desc->mutex.node)));
251 return_ACPI_STATUS (AE_AML_MUTEX_NOT_ACQUIRED);
254 /* Sanity check -- we must have a valid thread ID */
256 if (!walk_state->thread) {
257 ACPI_REPORT_ERROR (("Cannot release Mutex [%4.4s], null thread info\n",
258 acpi_ut_get_node_name (obj_desc->mutex.node)));
259 return_ACPI_STATUS (AE_AML_INTERNAL);
263 * The Mutex is owned, but this thread must be the owner.
264 * Special case for Global Lock, any thread can release
266 if ((obj_desc->mutex.owner_thread->thread_id != walk_state->thread->thread_id) &&
267 (obj_desc->mutex.semaphore != acpi_gbl_global_lock_semaphore)) {
269 "Thread %X cannot release Mutex [%4.4s] acquired by thread %X\n",
270 walk_state->thread->thread_id,
271 acpi_ut_get_node_name (obj_desc->mutex.node),
272 obj_desc->mutex.owner_thread->thread_id));
273 return_ACPI_STATUS (AE_AML_NOT_OWNER);
277 * The sync level of the mutex must be less than or
278 * equal to the current sync level
280 if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
281 ACPI_REPORT_ERROR (("Cannot release Mutex [%4.4s], incorrect sync_level\n",
282 acpi_ut_get_node_name (obj_desc->mutex.node)));
283 return_ACPI_STATUS (AE_AML_MUTEX_ORDER);
286 /* Match multiple Acquires with multiple Releases */
288 obj_desc->mutex.acquisition_depth--;
289 if (obj_desc->mutex.acquisition_depth != 0) {
290 /* Just decrement the depth and return */
292 return_ACPI_STATUS (AE_OK);
295 /* Unlink the mutex from the owner's list */
297 acpi_ex_unlink_mutex (obj_desc);
299 /* Release the mutex */
301 status = acpi_ex_system_release_mutex (obj_desc);
303 /* Update the mutex and walk state, restore sync_level before acquire */
305 obj_desc->mutex.owner_thread = NULL;
306 walk_state->thread->current_sync_level = obj_desc->mutex.original_sync_level;
308 return_ACPI_STATUS (status);
312 /*******************************************************************************
314 * FUNCTION: acpi_ex_release_all_mutexes
316 * PARAMETERS: mutex_list - Head of the mutex list
320 * DESCRIPTION: Release all mutexes in the list
322 ******************************************************************************/
325 acpi_ex_release_all_mutexes (
326 struct acpi_thread_state *thread)
328 union acpi_operand_object *next = thread->acquired_mutex_list;
329 union acpi_operand_object *this;
333 ACPI_FUNCTION_ENTRY ();
336 /* Traverse the list of owned mutexes, releasing each one */
340 next = this->mutex.next;
342 this->mutex.acquisition_depth = 1;
343 this->mutex.prev = NULL;
344 this->mutex.next = NULL;
346 /* Release the mutex */
348 status = acpi_ex_system_release_mutex (this);
349 if (ACPI_FAILURE (status)) {
353 /* Mark mutex unowned */
355 this->mutex.owner_thread = NULL;
357 /* Update Thread sync_level (Last mutex is the important one) */
359 thread->current_sync_level = this->mutex.original_sync_level;