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