2 /******************************************************************************
4 * Module Name: exmutex - ASL Mutex Acquire/Release functions
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2006, 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.
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
48 #define _COMPONENT ACPI_EXECUTER
49 ACPI_MODULE_NAME("exmutex")
51 /* Local prototypes */
53 acpi_ex_link_mutex(union acpi_operand_object
*obj_desc
,
54 struct acpi_thread_state
*thread
);
56 /*******************************************************************************
58 * FUNCTION: acpi_ex_unlink_mutex
60 * PARAMETERS: obj_desc - The mutex to be unlinked
64 * DESCRIPTION: Remove a mutex from the "acquired_mutex" list
66 ******************************************************************************/
68 void acpi_ex_unlink_mutex(union acpi_operand_object
*obj_desc
)
70 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
;
85 thread
->acquired_mutex_list
= obj_desc
->mutex
.next
;
89 /*******************************************************************************
91 * FUNCTION: acpi_ex_link_mutex
93 * PARAMETERS: obj_desc - The mutex to be linked
94 * Thread - Current executing thread object
98 * DESCRIPTION: Add a mutex to the "acquired_mutex" list for this walk
100 ******************************************************************************/
103 acpi_ex_link_mutex(union acpi_operand_object
*obj_desc
,
104 struct acpi_thread_state
*thread
)
106 union acpi_operand_object
*list_head
;
108 list_head
= thread
->acquired_mutex_list
;
110 /* This object will be the first object in the list */
112 obj_desc
->mutex
.prev
= NULL
;
113 obj_desc
->mutex
.next
= list_head
;
115 /* Update old first object to point back to this object */
118 list_head
->mutex
.prev
= obj_desc
;
121 /* Update list head */
123 thread
->acquired_mutex_list
= obj_desc
;
126 /*******************************************************************************
128 * FUNCTION: acpi_ex_acquire_mutex
130 * PARAMETERS: time_desc - Timeout integer
131 * obj_desc - Mutex object
132 * walk_state - Current method execution state
136 * DESCRIPTION: Acquire an AML mutex
138 ******************************************************************************/
141 acpi_ex_acquire_mutex(union acpi_operand_object
*time_desc
,
142 union acpi_operand_object
*obj_desc
,
143 struct acpi_walk_state
*walk_state
)
147 ACPI_FUNCTION_TRACE_PTR("ex_acquire_mutex", obj_desc
);
150 return_ACPI_STATUS(AE_BAD_PARAMETER
);
153 /* Sanity check -- we must have a valid thread ID */
155 if (!walk_state
->thread
) {
157 "Cannot acquire Mutex [%4.4s], null thread info",
158 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
159 return_ACPI_STATUS(AE_AML_INTERNAL
);
163 * Current Sync must be less than or equal to the sync level of the
164 * mutex. This mechanism provides some deadlock prevention
166 if (walk_state
->thread
->current_sync_level
> obj_desc
->mutex
.sync_level
) {
168 "Cannot acquire Mutex [%4.4s], incorrect sync_level",
169 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
170 return_ACPI_STATUS(AE_AML_MUTEX_ORDER
);
173 /* Support for multiple acquires by the owning thread */
175 if (obj_desc
->mutex
.owner_thread
) {
176 /* Special case for Global Lock, allow all threads */
178 if ((obj_desc
->mutex
.owner_thread
->thread_id
==
179 walk_state
->thread
->thread_id
) ||
180 (obj_desc
->mutex
.semaphore
==
181 acpi_gbl_global_lock_semaphore
)) {
183 * The mutex is already owned by this thread,
184 * just increment the acquisition depth
186 obj_desc
->mutex
.acquisition_depth
++;
187 return_ACPI_STATUS(AE_OK
);
191 /* Acquire the mutex, wait if necessary */
193 status
= acpi_ex_system_acquire_mutex(time_desc
, obj_desc
);
194 if (ACPI_FAILURE(status
)) {
195 /* Includes failure from a timeout on time_desc */
197 return_ACPI_STATUS(status
);
200 /* Have the mutex: update mutex and walk info and save the sync_level */
202 obj_desc
->mutex
.owner_thread
= walk_state
->thread
;
203 obj_desc
->mutex
.acquisition_depth
= 1;
204 obj_desc
->mutex
.original_sync_level
=
205 walk_state
->thread
->current_sync_level
;
207 walk_state
->thread
->current_sync_level
= obj_desc
->mutex
.sync_level
;
209 /* Link the mutex to the current thread for force-unlock at method exit */
211 acpi_ex_link_mutex(obj_desc
, walk_state
->thread
);
213 return_ACPI_STATUS(AE_OK
);
216 /*******************************************************************************
218 * FUNCTION: acpi_ex_release_mutex
220 * PARAMETERS: obj_desc - The object descriptor for this op
221 * walk_state - Current method execution state
225 * DESCRIPTION: Release a previously acquired Mutex.
227 ******************************************************************************/
230 acpi_ex_release_mutex(union acpi_operand_object
*obj_desc
,
231 struct acpi_walk_state
*walk_state
)
235 ACPI_FUNCTION_TRACE("ex_release_mutex");
238 return_ACPI_STATUS(AE_BAD_PARAMETER
);
241 /* The mutex must have been previously acquired in order to release it */
243 if (!obj_desc
->mutex
.owner_thread
) {
245 "Cannot release Mutex [%4.4s], not acquired",
246 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
247 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED
);
250 /* Sanity check -- we must have a valid thread ID */
252 if (!walk_state
->thread
) {
254 "Cannot release Mutex [%4.4s], null thread info",
255 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
256 return_ACPI_STATUS(AE_AML_INTERNAL
);
260 * The Mutex is owned, but this thread must be the owner.
261 * Special case for Global Lock, any thread can release
263 if ((obj_desc
->mutex
.owner_thread
->thread_id
!=
264 walk_state
->thread
->thread_id
)
265 && (obj_desc
->mutex
.semaphore
!= acpi_gbl_global_lock_semaphore
)) {
267 "Thread %X cannot release Mutex [%4.4s] acquired by thread %X",
268 walk_state
->thread
->thread_id
,
269 acpi_ut_get_node_name(obj_desc
->mutex
.node
),
270 obj_desc
->mutex
.owner_thread
->thread_id
));
271 return_ACPI_STATUS(AE_AML_NOT_OWNER
);
275 * The sync level of the mutex must be less than or
276 * equal to the current sync level
278 if (obj_desc
->mutex
.sync_level
> walk_state
->thread
->current_sync_level
) {
280 "Cannot release Mutex [%4.4s], incorrect sync_level",
281 acpi_ut_get_node_name(obj_desc
->mutex
.node
)));
282 return_ACPI_STATUS(AE_AML_MUTEX_ORDER
);
285 /* Match multiple Acquires with multiple Releases */
287 obj_desc
->mutex
.acquisition_depth
--;
288 if (obj_desc
->mutex
.acquisition_depth
!= 0) {
289 /* Just decrement the depth and return */
291 return_ACPI_STATUS(AE_OK
);
294 /* Unlink the mutex from the owner's list */
296 acpi_ex_unlink_mutex(obj_desc
);
298 /* Release the mutex */
300 status
= acpi_ex_system_release_mutex(obj_desc
);
302 /* Update the mutex and walk state, restore sync_level before acquire */
304 obj_desc
->mutex
.owner_thread
= NULL
;
305 walk_state
->thread
->current_sync_level
=
306 obj_desc
->mutex
.original_sync_level
;
308 return_ACPI_STATUS(status
);
311 /*******************************************************************************
313 * FUNCTION: acpi_ex_release_all_mutexes
315 * PARAMETERS: Thread - Current executing thread object
319 * DESCRIPTION: Release all mutexes held by this thread
321 ******************************************************************************/
323 void acpi_ex_release_all_mutexes(struct acpi_thread_state
*thread
)
325 union acpi_operand_object
*next
= thread
->acquired_mutex_list
;
326 union acpi_operand_object
*this;
329 ACPI_FUNCTION_ENTRY();
331 /* Traverse the list of owned mutexes, releasing each one */
335 next
= this->mutex
.next
;
337 this->mutex
.acquisition_depth
= 1;
338 this->mutex
.prev
= NULL
;
339 this->mutex
.next
= NULL
;
341 /* Release the mutex */
343 status
= acpi_ex_system_release_mutex(this);
344 if (ACPI_FAILURE(status
)) {
348 /* Mark mutex unowned */
350 this->mutex
.owner_thread
= NULL
;
352 /* Update Thread sync_level (Last mutex is the important one) */
354 thread
->current_sync_level
= this->mutex
.original_sync_level
;