Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | |
2 | /****************************************************************************** | |
3 | * | |
4 | * Module Name: exmutex - ASL Mutex Acquire/Release functions | |
5 | * | |
6 | *****************************************************************************/ | |
7 | ||
8 | /* | |
75a44ce0 | 9 | * Copyright (C) 2000 - 2008, Intel Corp. |
1da177e4 LT |
10 | * All rights reserved. |
11 | * | |
12 | * Redistribution and use in source and binary forms, with or without | |
13 | * modification, are permitted provided that the following conditions | |
14 | * are met: | |
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. | |
26 | * | |
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. | |
30 | * | |
31 | * NO WARRANTY | |
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. | |
43 | */ | |
44 | ||
1da177e4 | 45 | #include <acpi/acpi.h> |
e2f7a777 LB |
46 | #include "accommon.h" |
47 | #include "acinterp.h" | |
48 | #include "acevents.h" | |
1da177e4 LT |
49 | |
50 | #define _COMPONENT ACPI_EXECUTER | |
4be44fcd | 51 | ACPI_MODULE_NAME("exmutex") |
1da177e4 | 52 | |
44f6c012 | 53 | /* Local prototypes */ |
44f6c012 | 54 | static void |
4be44fcd LB |
55 | acpi_ex_link_mutex(union acpi_operand_object *obj_desc, |
56 | struct acpi_thread_state *thread); | |
1da177e4 LT |
57 | |
58 | /******************************************************************************* | |
59 | * | |
60 | * FUNCTION: acpi_ex_unlink_mutex | |
61 | * | |
62 | * PARAMETERS: obj_desc - The mutex to be unlinked | |
63 | * | |
44f6c012 | 64 | * RETURN: None |
1da177e4 | 65 | * |
b229cf92 | 66 | * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list |
1da177e4 LT |
67 | * |
68 | ******************************************************************************/ | |
69 | ||
262a7a28 | 70 | void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc) |
1da177e4 | 71 | { |
262a7a28 LB |
72 | struct acpi_thread_state *thread = obj_desc->mutex.owner_thread; |
73 | ||
1da177e4 LT |
74 | if (!thread) { |
75 | return; | |
76 | } | |
77 | ||
78 | /* Doubly linked list */ | |
79 | ||
80 | if (obj_desc->mutex.next) { | |
81 | (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev; | |
82 | } | |
83 | ||
84 | if (obj_desc->mutex.prev) { | |
85 | (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next; | |
4be44fcd | 86 | } else { |
1da177e4 LT |
87 | thread->acquired_mutex_list = obj_desc->mutex.next; |
88 | } | |
89 | } | |
90 | ||
1da177e4 LT |
91 | /******************************************************************************* |
92 | * | |
93 | * FUNCTION: acpi_ex_link_mutex | |
94 | * | |
44f6c012 RM |
95 | * PARAMETERS: obj_desc - The mutex to be linked |
96 | * Thread - Current executing thread object | |
1da177e4 | 97 | * |
44f6c012 | 98 | * RETURN: None |
1da177e4 | 99 | * |
b229cf92 | 100 | * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk |
1da177e4 LT |
101 | * |
102 | ******************************************************************************/ | |
103 | ||
44f6c012 | 104 | static void |
4be44fcd LB |
105 | acpi_ex_link_mutex(union acpi_operand_object *obj_desc, |
106 | struct acpi_thread_state *thread) | |
1da177e4 | 107 | { |
4be44fcd | 108 | union acpi_operand_object *list_head; |
1da177e4 LT |
109 | |
110 | list_head = thread->acquired_mutex_list; | |
111 | ||
112 | /* This object will be the first object in the list */ | |
113 | ||
114 | obj_desc->mutex.prev = NULL; | |
115 | obj_desc->mutex.next = list_head; | |
116 | ||
117 | /* Update old first object to point back to this object */ | |
118 | ||
119 | if (list_head) { | |
120 | list_head->mutex.prev = obj_desc; | |
121 | } | |
122 | ||
123 | /* Update list head */ | |
124 | ||
125 | thread->acquired_mutex_list = obj_desc; | |
126 | } | |
127 | ||
ba886cd4 BM |
128 | /******************************************************************************* |
129 | * | |
130 | * FUNCTION: acpi_ex_acquire_mutex_object | |
131 | * | |
132 | * PARAMETERS: time_desc - Timeout in milliseconds | |
133 | * obj_desc - Mutex object | |
134 | * Thread - Current thread state | |
135 | * | |
136 | * RETURN: Status | |
137 | * | |
91e38d10 BM |
138 | * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common |
139 | * path that supports multiple acquires by the same thread. | |
140 | * | |
141 | * MUTEX: Interpreter must be locked | |
142 | * | |
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 | |
146 | * global lock | |
147 | * 3) From the external interface, acpi_acquire_global_lock | |
ba886cd4 BM |
148 | * |
149 | ******************************************************************************/ | |
150 | ||
151 | acpi_status | |
152 | acpi_ex_acquire_mutex_object(u16 timeout, | |
153 | union acpi_operand_object *obj_desc, | |
154 | acpi_thread_id thread_id) | |
155 | { | |
156 | acpi_status status; | |
157 | ||
158 | ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc); | |
159 | ||
f02e9fa1 BM |
160 | if (!obj_desc) { |
161 | return_ACPI_STATUS(AE_BAD_PARAMETER); | |
162 | } | |
163 | ||
ba886cd4 BM |
164 | /* Support for multiple acquires by the owning thread */ |
165 | ||
166 | if (obj_desc->mutex.thread_id == thread_id) { | |
167 | /* | |
168 | * The mutex is already owned by this thread, just increment the | |
169 | * acquisition depth | |
170 | */ | |
171 | obj_desc->mutex.acquisition_depth++; | |
172 | return_ACPI_STATUS(AE_OK); | |
173 | } | |
174 | ||
175 | /* Acquire the mutex, wait if necessary. Special case for Global Lock */ | |
176 | ||
177 | if (obj_desc == acpi_gbl_global_lock_mutex) { | |
178 | status = acpi_ev_acquire_global_lock(timeout); | |
179 | } else { | |
180 | status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex, | |
181 | timeout); | |
182 | } | |
183 | ||
184 | if (ACPI_FAILURE(status)) { | |
185 | ||
186 | /* Includes failure from a timeout on time_desc */ | |
187 | ||
188 | return_ACPI_STATUS(status); | |
189 | } | |
190 | ||
91e38d10 | 191 | /* Acquired the mutex: update mutex object */ |
ba886cd4 BM |
192 | |
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() */ | |
197 | ||
198 | return_ACPI_STATUS(AE_OK); | |
199 | } | |
200 | ||
1da177e4 LT |
201 | /******************************************************************************* |
202 | * | |
203 | * FUNCTION: acpi_ex_acquire_mutex | |
204 | * | |
44f6c012 RM |
205 | * PARAMETERS: time_desc - Timeout integer |
206 | * obj_desc - Mutex object | |
207 | * walk_state - Current method execution state | |
1da177e4 LT |
208 | * |
209 | * RETURN: Status | |
210 | * | |
211 | * DESCRIPTION: Acquire an AML mutex | |
212 | * | |
213 | ******************************************************************************/ | |
214 | ||
215 | acpi_status | |
4be44fcd LB |
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) | |
1da177e4 | 219 | { |
4be44fcd | 220 | acpi_status status; |
1da177e4 | 221 | |
b229cf92 | 222 | ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc); |
1da177e4 LT |
223 | |
224 | if (!obj_desc) { | |
4be44fcd | 225 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
226 | } |
227 | ||
91e38d10 | 228 | /* Must have a valid thread ID */ |
1da177e4 LT |
229 | |
230 | if (!walk_state->thread) { | |
b8e4d893 BM |
231 | ACPI_ERROR((AE_INFO, |
232 | "Cannot acquire Mutex [%4.4s], null thread info", | |
233 | acpi_ut_get_node_name(obj_desc->mutex.node))); | |
4be44fcd | 234 | return_ACPI_STATUS(AE_AML_INTERNAL); |
1da177e4 LT |
235 | } |
236 | ||
237 | /* | |
91e38d10 | 238 | * Current sync level must be less than or equal to the sync level of the |
967440e3 | 239 | * mutex. This mechanism provides some deadlock prevention |
1da177e4 LT |
240 | */ |
241 | if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) { | |
b8e4d893 | 242 | ACPI_ERROR((AE_INFO, |
967440e3 BM |
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)); | |
4be44fcd | 246 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
1da177e4 LT |
247 | } |
248 | ||
ba886cd4 BM |
249 | status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value, |
250 | obj_desc, | |
251 | walk_state->thread->thread_id); | |
252 | if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) { | |
91e38d10 BM |
253 | |
254 | /* Save Thread object, original/current sync levels */ | |
255 | ||
ba886cd4 BM |
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; | |
1da177e4 | 261 | |
ba886cd4 BM |
262 | /* Link the mutex to the current thread for force-unlock at method exit */ |
263 | ||
264 | acpi_ex_link_mutex(obj_desc, walk_state->thread); | |
1da177e4 LT |
265 | } |
266 | ||
ba886cd4 BM |
267 | return_ACPI_STATUS(status); |
268 | } | |
c81da666 | 269 | |
ba886cd4 BM |
270 | /******************************************************************************* |
271 | * | |
272 | * FUNCTION: acpi_ex_release_mutex_object | |
273 | * | |
274 | * PARAMETERS: obj_desc - The object descriptor for this op | |
275 | * | |
276 | * RETURN: Status | |
277 | * | |
278 | * DESCRIPTION: Release a previously acquired Mutex, low level interface. | |
91e38d10 BM |
279 | * Provides a common path that supports multiple releases (after |
280 | * previous multiple acquires) by the same thread. | |
281 | * | |
282 | * MUTEX: Interpreter must be locked | |
283 | * | |
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 | |
287 | * global lock | |
288 | * 3) From the external interface, acpi_release_global_lock | |
ba886cd4 BM |
289 | * |
290 | ******************************************************************************/ | |
1da177e4 | 291 | |
ba886cd4 BM |
292 | acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc) |
293 | { | |
294 | acpi_status status = AE_OK; | |
52fc0b02 | 295 | |
ba886cd4 | 296 | ACPI_FUNCTION_TRACE(ex_release_mutex_object); |
1da177e4 | 297 | |
f02e9fa1 BM |
298 | if (obj_desc->mutex.acquisition_depth == 0) { |
299 | return (AE_NOT_ACQUIRED); | |
300 | } | |
301 | ||
ba886cd4 BM |
302 | /* Match multiple Acquires with multiple Releases */ |
303 | ||
304 | obj_desc->mutex.acquisition_depth--; | |
305 | if (obj_desc->mutex.acquisition_depth != 0) { | |
306 | ||
307 | /* Just decrement the depth and return */ | |
308 | ||
309 | return_ACPI_STATUS(AE_OK); | |
1da177e4 LT |
310 | } |
311 | ||
ba886cd4 | 312 | if (obj_desc->mutex.owner_thread) { |
1da177e4 | 313 | |
ba886cd4 BM |
314 | /* Unlink the mutex from the owner's list */ |
315 | ||
316 | acpi_ex_unlink_mutex(obj_desc); | |
317 | obj_desc->mutex.owner_thread = NULL; | |
318 | } | |
1da177e4 | 319 | |
ba886cd4 | 320 | /* Release the mutex, special case for Global Lock */ |
1da177e4 | 321 | |
ba886cd4 BM |
322 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
323 | status = acpi_ev_release_global_lock(); | |
324 | } else { | |
325 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); | |
326 | } | |
1da177e4 | 327 | |
91e38d10 BM |
328 | /* Clear mutex info */ |
329 | ||
b62151de | 330 | obj_desc->mutex.thread_id = NULL; |
ba886cd4 | 331 | return_ACPI_STATUS(status); |
1da177e4 LT |
332 | } |
333 | ||
1da177e4 LT |
334 | /******************************************************************************* |
335 | * | |
336 | * FUNCTION: acpi_ex_release_mutex | |
337 | * | |
338 | * PARAMETERS: obj_desc - The object descriptor for this op | |
44f6c012 | 339 | * walk_state - Current method execution state |
1da177e4 LT |
340 | * |
341 | * RETURN: Status | |
342 | * | |
343 | * DESCRIPTION: Release a previously acquired Mutex. | |
344 | * | |
345 | ******************************************************************************/ | |
346 | ||
347 | acpi_status | |
4be44fcd LB |
348 | acpi_ex_release_mutex(union acpi_operand_object *obj_desc, |
349 | struct acpi_walk_state *walk_state) | |
1da177e4 | 350 | { |
c81da666 | 351 | acpi_status status = AE_OK; |
1da177e4 | 352 | |
b229cf92 | 353 | ACPI_FUNCTION_TRACE(ex_release_mutex); |
1da177e4 LT |
354 | |
355 | if (!obj_desc) { | |
4be44fcd | 356 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
357 | } |
358 | ||
359 | /* The mutex must have been previously acquired in order to release it */ | |
360 | ||
262a7a28 | 361 | if (!obj_desc->mutex.owner_thread) { |
b8e4d893 BM |
362 | ACPI_ERROR((AE_INFO, |
363 | "Cannot release Mutex [%4.4s], not acquired", | |
364 | acpi_ut_get_node_name(obj_desc->mutex.node))); | |
4be44fcd | 365 | return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED); |
1da177e4 LT |
366 | } |
367 | ||
1da177e4 LT |
368 | /* |
369 | * The Mutex is owned, but this thread must be the owner. | |
370 | * Special case for Global Lock, any thread can release | |
371 | */ | |
262a7a28 | 372 | if ((obj_desc->mutex.owner_thread->thread_id != |
4be44fcd | 373 | walk_state->thread->thread_id) |
ba886cd4 | 374 | && (obj_desc != acpi_gbl_global_lock_mutex)) { |
b8e4d893 | 375 | ACPI_ERROR((AE_INFO, |
965a3d44 MB |
376 | "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX", |
377 | (unsigned long)walk_state->thread->thread_id, | |
b8e4d893 | 378 | acpi_ut_get_node_name(obj_desc->mutex.node), |
fd350943 LB |
379 | (unsigned long)obj_desc->mutex.owner_thread-> |
380 | thread_id)); | |
4be44fcd | 381 | return_ACPI_STATUS(AE_AML_NOT_OWNER); |
1da177e4 LT |
382 | } |
383 | ||
91e38d10 | 384 | /* Must have a valid thread ID */ |
ba886cd4 BM |
385 | |
386 | if (!walk_state->thread) { | |
387 | ACPI_ERROR((AE_INFO, | |
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); | |
391 | } | |
392 | ||
1da177e4 | 393 | /* |
c81da666 BM |
394 | * The sync level of the mutex must be less than or equal to the current |
395 | * sync level | |
1da177e4 LT |
396 | */ |
397 | if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) { | |
b8e4d893 | 398 | ACPI_ERROR((AE_INFO, |
f02e9fa1 BM |
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)); | |
4be44fcd | 403 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
1da177e4 LT |
404 | } |
405 | ||
ba886cd4 | 406 | status = acpi_ex_release_mutex_object(obj_desc); |
1da177e4 | 407 | |
f02e9fa1 | 408 | if (obj_desc->mutex.acquisition_depth == 0) { |
1da177e4 | 409 | |
f02e9fa1 BM |
410 | /* Restore the original sync_level */ |
411 | ||
412 | walk_state->thread->current_sync_level = | |
413 | obj_desc->mutex.original_sync_level; | |
414 | } | |
4be44fcd | 415 | return_ACPI_STATUS(status); |
1da177e4 LT |
416 | } |
417 | ||
1da177e4 LT |
418 | /******************************************************************************* |
419 | * | |
420 | * FUNCTION: acpi_ex_release_all_mutexes | |
421 | * | |
44f6c012 | 422 | * PARAMETERS: Thread - Current executing thread object |
1da177e4 LT |
423 | * |
424 | * RETURN: Status | |
425 | * | |
44f6c012 | 426 | * DESCRIPTION: Release all mutexes held by this thread |
1da177e4 | 427 | * |
f70a5e7b BM |
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. | |
433 | * | |
1da177e4 LT |
434 | ******************************************************************************/ |
435 | ||
4be44fcd | 436 | void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread) |
1da177e4 | 437 | { |
4be44fcd | 438 | union acpi_operand_object *next = thread->acquired_mutex_list; |
c81da666 | 439 | union acpi_operand_object *obj_desc; |
1da177e4 | 440 | |
4be44fcd | 441 | ACPI_FUNCTION_ENTRY(); |
1da177e4 LT |
442 | |
443 | /* Traverse the list of owned mutexes, releasing each one */ | |
444 | ||
445 | while (next) { | |
c81da666 BM |
446 | obj_desc = next; |
447 | next = obj_desc->mutex.next; | |
448 | ||
449 | obj_desc->mutex.prev = NULL; | |
450 | obj_desc->mutex.next = NULL; | |
f70a5e7b | 451 | obj_desc->mutex.acquisition_depth = 0; |
c81da666 BM |
452 | |
453 | /* Release the mutex, special case for Global Lock */ | |
1da177e4 | 454 | |
ba886cd4 | 455 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
1da177e4 | 456 | |
c81da666 | 457 | /* Ignore errors */ |
1da177e4 | 458 | |
c81da666 BM |
459 | (void)acpi_ev_release_global_lock(); |
460 | } else { | |
461 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); | |
1da177e4 LT |
462 | } |
463 | ||
464 | /* Mark mutex unowned */ | |
465 | ||
262a7a28 | 466 | obj_desc->mutex.owner_thread = NULL; |
b62151de | 467 | obj_desc->mutex.thread_id = NULL; |
1da177e4 LT |
468 | |
469 | /* Update Thread sync_level (Last mutex is the important one) */ | |
470 | ||
c81da666 BM |
471 | thread->current_sync_level = |
472 | obj_desc->mutex.original_sync_level; | |
1da177e4 LT |
473 | } |
474 | } |