blob: 3a39c2e8e104a001c22f3cc18adab681b598497d [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 Moore4a90c7e2006-01-13 16:22:00 -05009 * Copyright (C) 2000 - 2006, 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>
47
48#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040049ACPI_MODULE_NAME("exmutex")
Linus Torvalds1da177e2005-04-16 15:20:36 -070050
Robert Moore44f6c012005-04-18 22:49:35 -040051/* Local prototypes */
Robert Moore44f6c012005-04-18 22:49:35 -040052static void
Len Brown4be44fc2005-08-05 00:44:28 -040053acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
54 struct acpi_thread_state *thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -070055
56/*******************************************************************************
57 *
58 * FUNCTION: acpi_ex_unlink_mutex
59 *
60 * PARAMETERS: obj_desc - The mutex to be unlinked
61 *
Robert Moore44f6c012005-04-18 22:49:35 -040062 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
Bob Mooreb229cf92006-04-21 17:15:00 -040064 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 *
66 ******************************************************************************/
67
Len Brown4be44fc2005-08-05 00:44:28 -040068void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -070069{
Len Brown4be44fc2005-08-05 00:44:28 -040070 struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
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;
Len Brown4be44fc2005-08-05 00:44:28 -040084 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070085 thread->acquired_mutex_list = obj_desc->mutex.next;
86 }
87}
88
Linus Torvalds1da177e2005-04-16 15:20:36 -070089/*******************************************************************************
90 *
91 * FUNCTION: acpi_ex_link_mutex
92 *
Robert Moore44f6c012005-04-18 22:49:35 -040093 * PARAMETERS: obj_desc - The mutex to be linked
94 * Thread - Current executing thread object
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 *
Robert Moore44f6c012005-04-18 22:49:35 -040096 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -070097 *
Bob Mooreb229cf92006-04-21 17:15:00 -040098 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
Linus Torvalds1da177e2005-04-16 15:20:36 -070099 *
100 ******************************************************************************/
101
Robert Moore44f6c012005-04-18 22:49:35 -0400102static void
Len Brown4be44fc2005-08-05 00:44:28 -0400103acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
104 struct acpi_thread_state *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
Len Brown4be44fc2005-08-05 00:44:28 -0400106 union acpi_operand_object *list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
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
Linus Torvalds1da177e2005-04-16 15:20:36 -0700126/*******************************************************************************
127 *
128 * FUNCTION: acpi_ex_acquire_mutex
129 *
Robert Moore44f6c012005-04-18 22:49:35 -0400130 * PARAMETERS: time_desc - Timeout integer
131 * obj_desc - Mutex object
132 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
134 * RETURN: Status
135 *
136 * DESCRIPTION: Acquire an AML mutex
137 *
138 ******************************************************************************/
139
140acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400141acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
142 union acpi_operand_object *obj_desc,
143 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144{
Len Brown4be44fc2005-08-05 00:44:28 -0400145 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146
Bob Mooreb229cf92006-04-21 17:15:00 -0400147 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148
149 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400150 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 }
152
153 /* Sanity check -- we must have a valid thread ID */
154
155 if (!walk_state->thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500156 ACPI_ERROR((AE_INFO,
157 "Cannot acquire Mutex [%4.4s], null thread info",
158 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400159 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 }
161
162 /*
163 * Current Sync must be less than or equal to the sync level of the
Bob Moore967440e2006-06-23 17:04:00 -0400164 * mutex. This mechanism provides some deadlock prevention
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 */
166 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500167 ACPI_ERROR((AE_INFO,
Bob Moore967440e2006-06-23 17:04:00 -0400168 "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));
Len Brown4be44fc2005-08-05 00:44:28 -0400171 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 /* Support for multiple acquires by the owning thread */
175
176 if (obj_desc->mutex.owner_thread) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 /* Special case for Global Lock, allow all threads */
179
Robert Moore44f6c012005-04-18 22:49:35 -0400180 if ((obj_desc->mutex.owner_thread->thread_id ==
Len Brown4be44fc2005-08-05 00:44:28 -0400181 walk_state->thread->thread_id) ||
Bob Moore967440e2006-06-23 17:04:00 -0400182 (obj_desc->mutex.os_mutex == ACPI_GLOBAL_LOCK)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 /*
184 * The mutex is already owned by this thread,
185 * just increment the acquisition depth
186 */
187 obj_desc->mutex.acquisition_depth++;
Len Brown4be44fc2005-08-05 00:44:28 -0400188 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190 }
191
192 /* Acquire the mutex, wait if necessary */
193
Len Brown4be44fc2005-08-05 00:44:28 -0400194 status = acpi_ex_system_acquire_mutex(time_desc, obj_desc);
195 if (ACPI_FAILURE(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400196
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 /* Includes failure from a timeout on time_desc */
198
Len Brown4be44fc2005-08-05 00:44:28 -0400199 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700200 }
201
202 /* Have the mutex: update mutex and walk info and save the sync_level */
203
Len Brown4be44fc2005-08-05 00:44:28 -0400204 obj_desc->mutex.owner_thread = walk_state->thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205 obj_desc->mutex.acquisition_depth = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400206 obj_desc->mutex.original_sync_level =
207 walk_state->thread->current_sync_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
209 walk_state->thread->current_sync_level = obj_desc->mutex.sync_level;
210
211 /* Link the mutex to the current thread for force-unlock at method exit */
212
Len Brown4be44fc2005-08-05 00:44:28 -0400213 acpi_ex_link_mutex(obj_desc, walk_state->thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
Len Brown4be44fc2005-08-05 00:44:28 -0400215 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216}
217
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218/*******************************************************************************
219 *
220 * FUNCTION: acpi_ex_release_mutex
221 *
222 * PARAMETERS: obj_desc - The object descriptor for this op
Robert Moore44f6c012005-04-18 22:49:35 -0400223 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224 *
225 * RETURN: Status
226 *
227 * DESCRIPTION: Release a previously acquired Mutex.
228 *
229 ******************************************************************************/
230
231acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400232acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
233 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234{
Len Brown4be44fc2005-08-05 00:44:28 -0400235 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Bob Mooreb229cf92006-04-21 17:15:00 -0400237 ACPI_FUNCTION_TRACE(ex_release_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400240 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 }
242
243 /* The mutex must have been previously acquired in order to release it */
244
245 if (!obj_desc->mutex.owner_thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500246 ACPI_ERROR((AE_INFO,
247 "Cannot release Mutex [%4.4s], not acquired",
248 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400249 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700250 }
251
252 /* Sanity check -- we must have a valid thread ID */
253
254 if (!walk_state->thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500255 ACPI_ERROR((AE_INFO,
256 "Cannot release Mutex [%4.4s], null thread info",
257 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400258 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 }
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 */
Len Brown4be44fc2005-08-05 00:44:28 -0400265 if ((obj_desc->mutex.owner_thread->thread_id !=
266 walk_state->thread->thread_id)
Bob Moore967440e2006-06-23 17:04:00 -0400267 && (obj_desc->mutex.os_mutex != ACPI_GLOBAL_LOCK)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500268 ACPI_ERROR((AE_INFO,
269 "Thread %X cannot release Mutex [%4.4s] acquired by thread %X",
Len Brownab8aa062006-07-07 20:11:07 -0400270 (u32) walk_state->thread->thread_id,
Bob Mooreb8e4d892006-01-27 16:43:00 -0500271 acpi_ut_get_node_name(obj_desc->mutex.node),
Len Brownab8aa062006-07-07 20:11:07 -0400272 (u32) obj_desc->mutex.owner_thread->thread_id));
Len Brown4be44fc2005-08-05 00:44:28 -0400273 return_ACPI_STATUS(AE_AML_NOT_OWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
276 /*
277 * The sync level of the mutex must be less than or
278 * equal to the current sync level
279 */
280 if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500281 ACPI_ERROR((AE_INFO,
Bob Mooreb229cf92006-04-21 17:15:00 -0400282 "Cannot release Mutex [%4.4s], incorrect SyncLevel",
Bob Mooreb8e4d892006-01-27 16:43:00 -0500283 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400284 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 }
286
287 /* Match multiple Acquires with multiple Releases */
288
289 obj_desc->mutex.acquisition_depth--;
290 if (obj_desc->mutex.acquisition_depth != 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400291
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 /* Just decrement the depth and return */
293
Len Brown4be44fc2005-08-05 00:44:28 -0400294 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 }
296
297 /* Unlink the mutex from the owner's list */
298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 acpi_ex_unlink_mutex(obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700300
301 /* Release the mutex */
302
Len Brown4be44fc2005-08-05 00:44:28 -0400303 status = acpi_ex_system_release_mutex(obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304
305 /* Update the mutex and walk state, restore sync_level before acquire */
306
307 obj_desc->mutex.owner_thread = NULL;
Len Brown4be44fc2005-08-05 00:44:28 -0400308 walk_state->thread->current_sync_level =
309 obj_desc->mutex.original_sync_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Len Brown4be44fc2005-08-05 00:44:28 -0400311 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312}
313
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314/*******************************************************************************
315 *
316 * FUNCTION: acpi_ex_release_all_mutexes
317 *
Robert Moore44f6c012005-04-18 22:49:35 -0400318 * PARAMETERS: Thread - Current executing thread object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 *
320 * RETURN: Status
321 *
Robert Moore44f6c012005-04-18 22:49:35 -0400322 * DESCRIPTION: Release all mutexes held by this thread
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 *
324 ******************************************************************************/
325
Len Brown4be44fc2005-08-05 00:44:28 -0400326void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327{
Len Brown4be44fc2005-08-05 00:44:28 -0400328 union acpi_operand_object *next = thread->acquired_mutex_list;
329 union acpi_operand_object *this;
330 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Len Brown4be44fc2005-08-05 00:44:28 -0400332 ACPI_FUNCTION_ENTRY();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333
334 /* Traverse the list of owned mutexes, releasing each one */
335
336 while (next) {
337 this = next;
338 next = this->mutex.next;
339
340 this->mutex.acquisition_depth = 1;
Len Brown4be44fc2005-08-05 00:44:28 -0400341 this->mutex.prev = NULL;
342 this->mutex.next = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
Len Brown4be44fc2005-08-05 00:44:28 -0400344 /* Release the mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345
Len Brown4be44fc2005-08-05 00:44:28 -0400346 status = acpi_ex_system_release_mutex(this);
347 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 continue;
349 }
350
351 /* Mark mutex unowned */
352
353 this->mutex.owner_thread = NULL;
354
355 /* Update Thread sync_level (Last mutex is the important one) */
356
357 thread->current_sync_level = this->mutex.original_sync_level;
358 }
359}