blob: 5101bad5baf8b3074d29156b8a47444b286e90f8 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001
2/******************************************************************************
3 *
4 * Module Name: exmutex - ASL Mutex Acquire/Release functions
5 *
6 *****************************************************************************/
7
8/*
Bob Moore6c9deb72007-02-02 19:48:24 +03009 * Copyright (C) 2000 - 2007, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -070010 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <acpi/acpi.h>
46#include <acpi/acinterp.h>
Bob Moorec81da662007-02-02 19:48:18 +030047#include <acpi/acevents.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070048
49#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040050ACPI_MODULE_NAME("exmutex")
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Robert Moore44f6c012005-04-18 22:49:35 -040052/* Local prototypes */
Robert Moore44f6c012005-04-18 22:49:35 -040053static void
Len Brown4be44fc2005-08-05 00:44:28 -040054acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
55 struct acpi_thread_state *thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -070056
57/*******************************************************************************
58 *
59 * FUNCTION: acpi_ex_unlink_mutex
60 *
61 * PARAMETERS: obj_desc - The mutex to be unlinked
62 *
Robert Moore44f6c012005-04-18 22:49:35 -040063 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -070064 *
Bob Mooreb229cf92006-04-21 17:15:00 -040065 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
Linus Torvalds1da177e2005-04-16 15:20:36 -070066 *
67 ******************************************************************************/
68
Len Brown262a7a22007-05-09 23:01:59 -040069void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070070{
Len Brown262a7a22007-05-09 23:01:59 -040071 struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 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;
Len Brown4be44fc2005-08-05 00:44:28 -040085 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070086 thread->acquired_mutex_list = obj_desc->mutex.next;
87 }
88}
89
Linus Torvalds1da177e2005-04-16 15:20:36 -070090/*******************************************************************************
91 *
92 * FUNCTION: acpi_ex_link_mutex
93 *
Robert Moore44f6c012005-04-18 22:49:35 -040094 * PARAMETERS: obj_desc - The mutex to be linked
95 * Thread - Current executing thread object
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *
Robert Moore44f6c012005-04-18 22:49:35 -040097 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -070098 *
Bob Mooreb229cf92006-04-21 17:15:00 -040099 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 *
101 ******************************************************************************/
102
Robert Moore44f6c012005-04-18 22:49:35 -0400103static void
Len Brown4be44fc2005-08-05 00:44:28 -0400104acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
105 struct acpi_thread_state *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
Len Brown4be44fc2005-08-05 00:44:28 -0400107 union acpi_operand_object *list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127/*******************************************************************************
128 *
129 * FUNCTION: acpi_ex_acquire_mutex
130 *
Robert Moore44f6c012005-04-18 22:49:35 -0400131 * PARAMETERS: time_desc - Timeout integer
132 * obj_desc - Mutex object
133 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 *
135 * RETURN: Status
136 *
137 * DESCRIPTION: Acquire an AML mutex
138 *
139 ******************************************************************************/
140
141acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400142acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
143 union acpi_operand_object *obj_desc,
144 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145{
Len Brown4be44fc2005-08-05 00:44:28 -0400146 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147
Bob Mooreb229cf92006-04-21 17:15:00 -0400148 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149
150 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400151 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 }
153
Bob Moorec81da662007-02-02 19:48:18 +0300154 /* Sanity check: we must have a valid thread ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155
156 if (!walk_state->thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500157 ACPI_ERROR((AE_INFO,
158 "Cannot acquire Mutex [%4.4s], null thread info",
159 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400160 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 }
162
163 /*
164 * Current Sync must be less than or equal to the sync level of the
Bob Moore967440e32006-06-23 17:04:00 -0400165 * mutex. This mechanism provides some deadlock prevention
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166 */
167 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500168 ACPI_ERROR((AE_INFO,
Bob Moore967440e32006-06-23 17:04:00 -0400169 "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));
Len Brown4be44fc2005-08-05 00:44:28 -0400172 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
175 /* Support for multiple acquires by the owning thread */
176
Len Brown262a7a22007-05-09 23:01:59 -0400177 if (obj_desc->mutex.owner_thread) {
178 if (obj_desc->mutex.owner_thread->thread_id ==
179 walk_state->thread->thread_id) {
180 /*
181 * The mutex is already owned by this thread, just increment the
182 * acquisition depth
183 */
184 obj_desc->mutex.acquisition_depth++;
185 return_ACPI_STATUS(AE_OK);
186 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188
Bob Moorec81da662007-02-02 19:48:18 +0300189 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190
Bob Moorec81da662007-02-02 19:48:18 +0300191 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 }
199
Len Brown4be44fc2005-08-05 00:44:28 -0400200 if (ACPI_FAILURE(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400201
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 /* Includes failure from a timeout on time_desc */
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 }
206
207 /* Have the mutex: update mutex and walk info and save the sync_level */
208
Len Brown262a7a22007-05-09 23:01:59 -0400209 obj_desc->mutex.owner_thread = walk_state->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 obj_desc->mutex.acquisition_depth = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400211 obj_desc->mutex.original_sync_level =
212 walk_state->thread->current_sync_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213
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
Len Brown4be44fc2005-08-05 00:44:28 -0400218 acpi_ex_link_mutex(obj_desc, walk_state->thread);
Len Brown4be44fc2005-08-05 00:44:28 -0400219 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222/*******************************************************************************
223 *
224 * FUNCTION: acpi_ex_release_mutex
225 *
226 * PARAMETERS: obj_desc - The object descriptor for this op
Robert Moore44f6c012005-04-18 22:49:35 -0400227 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228 *
229 * RETURN: Status
230 *
231 * DESCRIPTION: Release a previously acquired Mutex.
232 *
233 ******************************************************************************/
234
235acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400236acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
237 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238{
Bob Moorec81da662007-02-02 19:48:18 +0300239 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700240
Bob Mooreb229cf92006-04-21 17:15:00 -0400241 ACPI_FUNCTION_TRACE(ex_release_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400244 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246
247 /* The mutex must have been previously acquired in order to release it */
248
Len Brown262a7a22007-05-09 23:01:59 -0400249 if (!obj_desc->mutex.owner_thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500250 ACPI_ERROR((AE_INFO,
251 "Cannot release Mutex [%4.4s], not acquired",
252 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400253 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
Bob Moorec81da662007-02-02 19:48:18 +0300256 /* Sanity check: we must have a valid thread ID */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 if (!walk_state->thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500259 ACPI_ERROR((AE_INFO,
260 "Cannot release Mutex [%4.4s], null thread info",
261 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400262 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263 }
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 */
Len Brown262a7a22007-05-09 23:01:59 -0400269 if ((obj_desc->mutex.owner_thread->thread_id !=
Len Brown4be44fc2005-08-05 00:44:28 -0400270 walk_state->thread->thread_id)
Bob Moorec81da662007-02-02 19:48:18 +0300271 && (obj_desc->mutex.os_mutex != acpi_gbl_global_lock_mutex)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500272 ACPI_ERROR((AE_INFO,
Martin Bligh965a3d42006-10-20 14:30:26 -0700273 "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX",
274 (unsigned long)walk_state->thread->thread_id,
Bob Mooreb8e4d892006-01-27 16:43:00 -0500275 acpi_ut_get_node_name(obj_desc->mutex.node),
Len Brown262a7a22007-05-09 23:01:59 -0400276 (unsigned long)obj_desc->mutex.owner_thread->thread_id));
Len Brown4be44fc2005-08-05 00:44:28 -0400277 return_ACPI_STATUS(AE_AML_NOT_OWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
280 /*
Bob Moorec81da662007-02-02 19:48:18 +0300281 * The sync level of the mutex must be less than or equal to the current
282 * sync level
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 */
284 if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500285 ACPI_ERROR((AE_INFO,
Bob Mooreb229cf92006-04-21 17:15:00 -0400286 "Cannot release Mutex [%4.4s], incorrect SyncLevel",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500287 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400288 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 }
290
291 /* Match multiple Acquires with multiple Releases */
292
293 obj_desc->mutex.acquisition_depth--;
294 if (obj_desc->mutex.acquisition_depth != 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296 /* Just decrement the depth and return */
297
Len Brown4be44fc2005-08-05 00:44:28 -0400298 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700299 }
300
301 /* Unlink the mutex from the owner's list */
302
Len Brown262a7a22007-05-09 23:01:59 -0400303 acpi_ex_unlink_mutex(obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
Bob Moorec81da662007-02-02 19:48:18 +0300305 /* Release the mutex, special case for Global Lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306
Bob Moorec81da662007-02-02 19:48:18 +0300307 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 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312
Bob Moorec81da662007-02-02 19:48:18 +0300313 /* Update the mutex and restore sync_level */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314
Len Brown262a7a22007-05-09 23:01:59 -0400315 obj_desc->mutex.owner_thread = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400316 walk_state->thread->current_sync_level =
317 obj_desc->mutex.original_sync_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700318
Len Brown4be44fc2005-08-05 00:44:28 -0400319 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320}
321
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322/*******************************************************************************
323 *
324 * FUNCTION: acpi_ex_release_all_mutexes
325 *
Robert Moore44f6c012005-04-18 22:49:35 -0400326 * PARAMETERS: Thread - Current executing thread object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 *
328 * RETURN: Status
329 *
Robert Moore44f6c012005-04-18 22:49:35 -0400330 * DESCRIPTION: Release all mutexes held by this thread
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 *
Bob Mooref70a5e72007-02-02 19:48:21 +0300332 * NOTE: This function is called as the thread is exiting the interpreter.
333 * Mutexes are not released when an individual control method is exited, but
334 * only when the parent thread actually exits the interpreter. This allows one
335 * method to acquire a mutex, and a different method to release it, as long as
336 * this is performed underneath a single parent control method.
337 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 ******************************************************************************/
339
Len Brown4be44fc2005-08-05 00:44:28 -0400340void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700341{
Len Brown4be44fc2005-08-05 00:44:28 -0400342 union acpi_operand_object *next = thread->acquired_mutex_list;
Bob Moorec81da662007-02-02 19:48:18 +0300343 union acpi_operand_object *obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344
Len Brown4be44fc2005-08-05 00:44:28 -0400345 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
347 /* Traverse the list of owned mutexes, releasing each one */
348
349 while (next) {
Bob Moorec81da662007-02-02 19:48:18 +0300350 obj_desc = next;
351 next = obj_desc->mutex.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352
Bob Moorec81da662007-02-02 19:48:18 +0300353 obj_desc->mutex.prev = NULL;
354 obj_desc->mutex.next = NULL;
Bob Mooref70a5e72007-02-02 19:48:21 +0300355 obj_desc->mutex.acquisition_depth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
Bob Moorec81da662007-02-02 19:48:18 +0300357 /* Release the mutex, special case for Global Lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Bob Moorec81da662007-02-02 19:48:18 +0300359 if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
360
361 /* Ignore errors */
362
363 (void)acpi_ev_release_global_lock();
364 } else {
365 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
367
368 /* Mark mutex unowned */
369
Len Brown262a7a22007-05-09 23:01:59 -0400370 obj_desc->mutex.owner_thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372 /* Update Thread sync_level (Last mutex is the important one) */
373
Bob Moorec81da662007-02-02 19:48:18 +0300374 thread->current_sync_level =
375 obj_desc->mutex.original_sync_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377}