]>
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 | ||
c0d127b5 AS |
69 | void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc, |
70 | struct acpi_thread_state *thread) | |
1da177e4 | 71 | { |
1da177e4 LT |
72 | if (!thread) { |
73 | return; | |
74 | } | |
75 | ||
76 | /* Doubly linked list */ | |
77 | ||
78 | if (obj_desc->mutex.next) { | |
79 | (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev; | |
80 | } | |
81 | ||
82 | if (obj_desc->mutex.prev) { | |
83 | (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next; | |
4be44fcd | 84 | } else { |
1da177e4 LT |
85 | thread->acquired_mutex_list = obj_desc->mutex.next; |
86 | } | |
87 | } | |
88 | ||
1da177e4 LT |
89 | /******************************************************************************* |
90 | * | |
91 | * FUNCTION: acpi_ex_link_mutex | |
92 | * | |
44f6c012 RM |
93 | * PARAMETERS: obj_desc - The mutex to be linked |
94 | * Thread - Current executing thread object | |
1da177e4 | 95 | * |
44f6c012 | 96 | * RETURN: None |
1da177e4 | 97 | * |
b229cf92 | 98 | * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk |
1da177e4 LT |
99 | * |
100 | ******************************************************************************/ | |
101 | ||
44f6c012 | 102 | static void |
4be44fcd LB |
103 | acpi_ex_link_mutex(union acpi_operand_object *obj_desc, |
104 | struct acpi_thread_state *thread) | |
1da177e4 | 105 | { |
4be44fcd | 106 | union acpi_operand_object *list_head; |
1da177e4 LT |
107 | |
108 | list_head = thread->acquired_mutex_list; | |
109 | ||
110 | /* This object will be the first object in the list */ | |
111 | ||
112 | obj_desc->mutex.prev = NULL; | |
113 | obj_desc->mutex.next = list_head; | |
114 | ||
115 | /* Update old first object to point back to this object */ | |
116 | ||
117 | if (list_head) { | |
118 | list_head->mutex.prev = obj_desc; | |
119 | } | |
120 | ||
121 | /* Update list head */ | |
122 | ||
123 | thread->acquired_mutex_list = obj_desc; | |
124 | } | |
125 | ||
1da177e4 LT |
126 | /******************************************************************************* |
127 | * | |
128 | * FUNCTION: acpi_ex_acquire_mutex | |
129 | * | |
44f6c012 RM |
130 | * PARAMETERS: time_desc - Timeout integer |
131 | * obj_desc - Mutex object | |
132 | * walk_state - Current method execution state | |
1da177e4 LT |
133 | * |
134 | * RETURN: Status | |
135 | * | |
136 | * DESCRIPTION: Acquire an AML mutex | |
137 | * | |
138 | ******************************************************************************/ | |
139 | ||
140 | acpi_status | |
4be44fcd LB |
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) | |
1da177e4 | 144 | { |
4be44fcd | 145 | acpi_status status; |
1da177e4 | 146 | |
b229cf92 | 147 | ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc); |
1da177e4 LT |
148 | |
149 | if (!obj_desc) { | |
4be44fcd | 150 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
151 | } |
152 | ||
c81da666 | 153 | /* Sanity check: we must have a valid thread ID */ |
1da177e4 LT |
154 | |
155 | if (!walk_state->thread) { | |
b8e4d893 BM |
156 | ACPI_ERROR((AE_INFO, |
157 | "Cannot acquire Mutex [%4.4s], null thread info", | |
158 | acpi_ut_get_node_name(obj_desc->mutex.node))); | |
4be44fcd | 159 | return_ACPI_STATUS(AE_AML_INTERNAL); |
1da177e4 LT |
160 | } |
161 | ||
162 | /* | |
163 | * Current Sync must be less than or equal to the sync level of the | |
967440e3 | 164 | * mutex. This mechanism provides some deadlock prevention |
1da177e4 LT |
165 | */ |
166 | if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) { | |
b8e4d893 | 167 | ACPI_ERROR((AE_INFO, |
967440e3 BM |
168 | "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)", |
169 | acpi_ut_get_node_name(obj_desc->mutex.node), | |
170 | walk_state->thread->current_sync_level)); | |
4be44fcd | 171 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
1da177e4 LT |
172 | } |
173 | ||
174 | /* Support for multiple acquires by the owning thread */ | |
175 | ||
c0d127b5 AS |
176 | if (obj_desc->mutex.owner_thread_id == acpi_os_get_thread_id()) { |
177 | /* | |
178 | * The mutex is already owned by this thread, just increment the | |
179 | * acquisition depth | |
180 | */ | |
181 | obj_desc->mutex.acquisition_depth++; | |
182 | return_ACPI_STATUS(AE_OK); | |
1da177e4 LT |
183 | } |
184 | ||
c81da666 BM |
185 | /* Acquire the mutex, wait if necessary. Special case for Global Lock */ |
186 | ||
187 | if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) { | |
188 | status = | |
189 | acpi_ev_acquire_global_lock((u16) time_desc->integer.value); | |
190 | } else { | |
191 | status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex, | |
192 | (u16) time_desc->integer. | |
193 | value); | |
194 | } | |
1da177e4 | 195 | |
4be44fcd | 196 | if (ACPI_FAILURE(status)) { |
52fc0b02 | 197 | |
1da177e4 LT |
198 | /* Includes failure from a timeout on time_desc */ |
199 | ||
4be44fcd | 200 | return_ACPI_STATUS(status); |
1da177e4 LT |
201 | } |
202 | ||
203 | /* Have the mutex: update mutex and walk info and save the sync_level */ | |
204 | ||
c0d127b5 | 205 | obj_desc->mutex.owner_thread_id = acpi_os_get_thread_id(); |
1da177e4 | 206 | obj_desc->mutex.acquisition_depth = 1; |
4be44fcd LB |
207 | obj_desc->mutex.original_sync_level = |
208 | walk_state->thread->current_sync_level; | |
1da177e4 LT |
209 | |
210 | walk_state->thread->current_sync_level = obj_desc->mutex.sync_level; | |
211 | ||
212 | /* Link the mutex to the current thread for force-unlock at method exit */ | |
213 | ||
4be44fcd | 214 | acpi_ex_link_mutex(obj_desc, walk_state->thread); |
4be44fcd | 215 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
216 | } |
217 | ||
1da177e4 LT |
218 | /******************************************************************************* |
219 | * | |
220 | * FUNCTION: acpi_ex_release_mutex | |
221 | * | |
222 | * PARAMETERS: obj_desc - The object descriptor for this op | |
44f6c012 | 223 | * walk_state - Current method execution state |
1da177e4 LT |
224 | * |
225 | * RETURN: Status | |
226 | * | |
227 | * DESCRIPTION: Release a previously acquired Mutex. | |
228 | * | |
229 | ******************************************************************************/ | |
230 | ||
231 | acpi_status | |
4be44fcd LB |
232 | acpi_ex_release_mutex(union acpi_operand_object *obj_desc, |
233 | struct acpi_walk_state *walk_state) | |
1da177e4 | 234 | { |
c81da666 | 235 | acpi_status status = AE_OK; |
1da177e4 | 236 | |
b229cf92 | 237 | ACPI_FUNCTION_TRACE(ex_release_mutex); |
1da177e4 LT |
238 | |
239 | if (!obj_desc) { | |
4be44fcd | 240 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
1da177e4 LT |
241 | } |
242 | ||
243 | /* The mutex must have been previously acquired in order to release it */ | |
244 | ||
c0d127b5 | 245 | if (!obj_desc->mutex.owner_thread_id) { |
b8e4d893 BM |
246 | ACPI_ERROR((AE_INFO, |
247 | "Cannot release Mutex [%4.4s], not acquired", | |
248 | acpi_ut_get_node_name(obj_desc->mutex.node))); | |
4be44fcd | 249 | return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED); |
1da177e4 LT |
250 | } |
251 | ||
c81da666 | 252 | /* Sanity check: we must have a valid thread ID */ |
1da177e4 LT |
253 | |
254 | if (!walk_state->thread) { | |
b8e4d893 BM |
255 | ACPI_ERROR((AE_INFO, |
256 | "Cannot release Mutex [%4.4s], null thread info", | |
257 | acpi_ut_get_node_name(obj_desc->mutex.node))); | |
4be44fcd | 258 | return_ACPI_STATUS(AE_AML_INTERNAL); |
1da177e4 LT |
259 | } |
260 | ||
261 | /* | |
262 | * The Mutex is owned, but this thread must be the owner. | |
263 | * Special case for Global Lock, any thread can release | |
264 | */ | |
c0d127b5 | 265 | if ((obj_desc->mutex.owner_thread_id != |
4be44fcd | 266 | walk_state->thread->thread_id) |
c81da666 | 267 | && (obj_desc->mutex.os_mutex != acpi_gbl_global_lock_mutex)) { |
b8e4d893 | 268 | ACPI_ERROR((AE_INFO, |
965a3d44 MB |
269 | "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX", |
270 | (unsigned long)walk_state->thread->thread_id, | |
b8e4d893 | 271 | acpi_ut_get_node_name(obj_desc->mutex.node), |
c0d127b5 | 272 | (unsigned long)obj_desc->mutex.owner_thread_id)); |
4be44fcd | 273 | return_ACPI_STATUS(AE_AML_NOT_OWNER); |
1da177e4 LT |
274 | } |
275 | ||
276 | /* | |
c81da666 BM |
277 | * The sync level of the mutex must be less than or equal to the current |
278 | * sync level | |
1da177e4 LT |
279 | */ |
280 | if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) { | |
b8e4d893 | 281 | ACPI_ERROR((AE_INFO, |
b229cf92 | 282 | "Cannot release Mutex [%4.4s], incorrect SyncLevel", |
b8e4d893 | 283 | acpi_ut_get_node_name(obj_desc->mutex.node))); |
4be44fcd | 284 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
1da177e4 LT |
285 | } |
286 | ||
287 | /* Match multiple Acquires with multiple Releases */ | |
288 | ||
289 | obj_desc->mutex.acquisition_depth--; | |
290 | if (obj_desc->mutex.acquisition_depth != 0) { | |
52fc0b02 | 291 | |
1da177e4 LT |
292 | /* Just decrement the depth and return */ |
293 | ||
4be44fcd | 294 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
295 | } |
296 | ||
297 | /* Unlink the mutex from the owner's list */ | |
298 | ||
c0d127b5 | 299 | acpi_ex_unlink_mutex(obj_desc, walk_state->thread); |
1da177e4 | 300 | |
c81da666 | 301 | /* Release the mutex, special case for Global Lock */ |
1da177e4 | 302 | |
c81da666 BM |
303 | if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) { |
304 | status = acpi_ev_release_global_lock(); | |
305 | } else { | |
306 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); | |
307 | } | |
1da177e4 | 308 | |
c81da666 | 309 | /* Update the mutex and restore sync_level */ |
1da177e4 | 310 | |
c0d127b5 | 311 | obj_desc->mutex.owner_thread_id = ACPI_MUTEX_NOT_ACQUIRED; |
4be44fcd LB |
312 | walk_state->thread->current_sync_level = |
313 | obj_desc->mutex.original_sync_level; | |
1da177e4 | 314 | |
4be44fcd | 315 | return_ACPI_STATUS(status); |
1da177e4 LT |
316 | } |
317 | ||
1da177e4 LT |
318 | /******************************************************************************* |
319 | * | |
320 | * FUNCTION: acpi_ex_release_all_mutexes | |
321 | * | |
44f6c012 | 322 | * PARAMETERS: Thread - Current executing thread object |
1da177e4 LT |
323 | * |
324 | * RETURN: Status | |
325 | * | |
44f6c012 | 326 | * DESCRIPTION: Release all mutexes held by this thread |
1da177e4 | 327 | * |
f70a5e7b BM |
328 | * NOTE: This function is called as the thread is exiting the interpreter. |
329 | * Mutexes are not released when an individual control method is exited, but | |
330 | * only when the parent thread actually exits the interpreter. This allows one | |
331 | * method to acquire a mutex, and a different method to release it, as long as | |
332 | * this is performed underneath a single parent control method. | |
333 | * | |
1da177e4 LT |
334 | ******************************************************************************/ |
335 | ||
4be44fcd | 336 | void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread) |
1da177e4 | 337 | { |
4be44fcd | 338 | union acpi_operand_object *next = thread->acquired_mutex_list; |
c81da666 | 339 | union acpi_operand_object *obj_desc; |
1da177e4 | 340 | |
4be44fcd | 341 | ACPI_FUNCTION_ENTRY(); |
1da177e4 LT |
342 | |
343 | /* Traverse the list of owned mutexes, releasing each one */ | |
344 | ||
345 | while (next) { | |
c81da666 BM |
346 | obj_desc = next; |
347 | next = obj_desc->mutex.next; | |
348 | ||
349 | obj_desc->mutex.prev = NULL; | |
350 | obj_desc->mutex.next = NULL; | |
f70a5e7b | 351 | obj_desc->mutex.acquisition_depth = 0; |
c81da666 BM |
352 | |
353 | /* Release the mutex, special case for Global Lock */ | |
1da177e4 | 354 | |
c81da666 | 355 | if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) { |
1da177e4 | 356 | |
c81da666 | 357 | /* Ignore errors */ |
1da177e4 | 358 | |
c81da666 BM |
359 | (void)acpi_ev_release_global_lock(); |
360 | } else { | |
361 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); | |
1da177e4 LT |
362 | } |
363 | ||
364 | /* Mark mutex unowned */ | |
365 | ||
c0d127b5 | 366 | obj_desc->mutex.owner_thread_id = ACPI_MUTEX_NOT_ACQUIRED; |
1da177e4 LT |
367 | |
368 | /* Update Thread sync_level (Last mutex is the important one) */ | |
369 | ||
c81da666 BM |
370 | thread->current_sync_level = |
371 | obj_desc->mutex.original_sync_level; | |
1da177e4 LT |
372 | } |
373 | } |