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