blob: b49ea2a95f4f99f5afde46ee9a1394d16078d675 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: exmutex - ASL Mutex Acquire/Release functions
4 *
5 *****************************************************************************/
6
7/*
Bob Moorefbb7a2d2014-02-08 09:42:25 +08008 * Copyright (C) 2000 - 2014, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acinterp.h"
47#include "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;
Bob Moore10a3b462009-05-21 10:02:34 +080085
86 /*
Bob Moorea7499bc2010-04-01 11:04:54 +080087 * Migrate the previous sync level associated with this mutex to
88 * the previous mutex on the list so that it may be preserved.
89 * This handles the case where several mutexes have been acquired
90 * at the same level, but are not released in opposite order.
Bob Moore10a3b462009-05-21 10:02:34 +080091 */
92 (obj_desc->mutex.prev)->mutex.original_sync_level =
93 obj_desc->mutex.original_sync_level;
Len Brown4be44fc2005-08-05 00:44:28 -040094 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -070095 thread->acquired_mutex_list = obj_desc->mutex.next;
96 }
97}
98
Linus Torvalds1da177e2005-04-16 15:20:36 -070099/*******************************************************************************
100 *
101 * FUNCTION: acpi_ex_link_mutex
102 *
Bob Moorea7499bc2010-04-01 11:04:54 +0800103 * PARAMETERS: obj_desc - The mutex to be linked
Bob Mooreba494be2012-07-12 09:40:10 +0800104 * thread - Current executing thread object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 *
Robert Moore44f6c012005-04-18 22:49:35 -0400106 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 *
Bob Mooreb229cf92006-04-21 17:15:00 -0400108 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
Linus Torvalds1da177e2005-04-16 15:20:36 -0700109 *
110 ******************************************************************************/
111
Robert Moore44f6c012005-04-18 22:49:35 -0400112static void
Len Brown4be44fc2005-08-05 00:44:28 -0400113acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
114 struct acpi_thread_state *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115{
Len Brown4be44fc2005-08-05 00:44:28 -0400116 union acpi_operand_object *list_head;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117
118 list_head = thread->acquired_mutex_list;
119
120 /* This object will be the first object in the list */
121
122 obj_desc->mutex.prev = NULL;
123 obj_desc->mutex.next = list_head;
124
125 /* Update old first object to point back to this object */
126
127 if (list_head) {
128 list_head->mutex.prev = obj_desc;
129 }
130
131 /* Update list head */
132
133 thread->acquired_mutex_list = obj_desc;
134}
135
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136/*******************************************************************************
137 *
Bob Mooreba886cd2008-04-10 19:06:37 +0400138 * FUNCTION: acpi_ex_acquire_mutex_object
139 *
Bob Mooreba494be2012-07-12 09:40:10 +0800140 * PARAMETERS: timeout - Timeout in milliseconds
Bob Mooreba886cd2008-04-10 19:06:37 +0400141 * obj_desc - Mutex object
Bob Moorea7499bc2010-04-01 11:04:54 +0800142 * thread_id - Current thread state
Bob Mooreba886cd2008-04-10 19:06:37 +0400143 *
144 * RETURN: Status
145 *
Bob Moore91e38d12008-04-10 19:06:37 +0400146 * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
147 * path that supports multiple acquires by the same thread.
148 *
149 * MUTEX: Interpreter must be locked
150 *
151 * NOTE: This interface is called from three places:
152 * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
153 * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
154 * global lock
155 * 3) From the external interface, acpi_acquire_global_lock
Bob Mooreba886cd2008-04-10 19:06:37 +0400156 *
157 ******************************************************************************/
158
159acpi_status
160acpi_ex_acquire_mutex_object(u16 timeout,
161 union acpi_operand_object *obj_desc,
162 acpi_thread_id thread_id)
163{
164 acpi_status status;
165
166 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
167
Bob Mooref02e9fa2008-04-10 19:06:37 +0400168 if (!obj_desc) {
169 return_ACPI_STATUS(AE_BAD_PARAMETER);
170 }
171
Bob Mooreba886cd2008-04-10 19:06:37 +0400172 /* Support for multiple acquires by the owning thread */
173
174 if (obj_desc->mutex.thread_id == thread_id) {
175 /*
176 * The mutex is already owned by this thread, just increment the
177 * acquisition depth
178 */
179 obj_desc->mutex.acquisition_depth++;
180 return_ACPI_STATUS(AE_OK);
181 }
182
183 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
184
185 if (obj_desc == acpi_gbl_global_lock_mutex) {
186 status = acpi_ev_acquire_global_lock(timeout);
187 } else {
188 status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
189 timeout);
190 }
191
192 if (ACPI_FAILURE(status)) {
193
194 /* Includes failure from a timeout on time_desc */
195
196 return_ACPI_STATUS(status);
197 }
198
Bob Moore91e38d12008-04-10 19:06:37 +0400199 /* Acquired the mutex: update mutex object */
Bob Mooreba886cd2008-04-10 19:06:37 +0400200
201 obj_desc->mutex.thread_id = thread_id;
202 obj_desc->mutex.acquisition_depth = 1;
203 obj_desc->mutex.original_sync_level = 0;
204 obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */
205
206 return_ACPI_STATUS(AE_OK);
207}
208
209/*******************************************************************************
210 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211 * FUNCTION: acpi_ex_acquire_mutex
212 *
Robert Moore44f6c012005-04-18 22:49:35 -0400213 * PARAMETERS: time_desc - Timeout integer
214 * obj_desc - Mutex object
215 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 *
217 * RETURN: Status
218 *
219 * DESCRIPTION: Acquire an AML mutex
220 *
221 ******************************************************************************/
222
223acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400224acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
225 union acpi_operand_object *obj_desc,
226 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227{
Len Brown4be44fc2005-08-05 00:44:28 -0400228 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229
Bob Mooreb229cf92006-04-21 17:15:00 -0400230 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700231
232 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400233 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235
Bob Moorea7499bc2010-04-01 11:04:54 +0800236 /* Must have a valid thread state struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237
238 if (!walk_state->thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500239 ACPI_ERROR((AE_INFO,
240 "Cannot acquire Mutex [%4.4s], null thread info",
241 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400242 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
245 /*
Bob Moore91e38d12008-04-10 19:06:37 +0400246 * Current sync level must be less than or equal to the sync level of the
Bob Moore967440e32006-06-23 17:04:00 -0400247 * mutex. This mechanism provides some deadlock prevention
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 */
249 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500250 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800251 "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%u)",
Bob Moore967440e32006-06-23 17:04:00 -0400252 acpi_ut_get_node_name(obj_desc->mutex.node),
253 walk_state->thread->current_sync_level));
Len Brown4be44fc2005-08-05 00:44:28 -0400254 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700255 }
256
Bob Mooreba886cd2008-04-10 19:06:37 +0400257 status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value,
258 obj_desc,
259 walk_state->thread->thread_id);
260 if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
Bob Moore91e38d12008-04-10 19:06:37 +0400261
262 /* Save Thread object, original/current sync levels */
263
Bob Mooreba886cd2008-04-10 19:06:37 +0400264 obj_desc->mutex.owner_thread = walk_state->thread;
265 obj_desc->mutex.original_sync_level =
266 walk_state->thread->current_sync_level;
267 walk_state->thread->current_sync_level =
268 obj_desc->mutex.sync_level;
269
270 /* Link the mutex to the current thread for force-unlock at method exit */
271
272 acpi_ex_link_mutex(obj_desc, walk_state->thread);
273 }
274
275 return_ACPI_STATUS(status);
276}
277
278/*******************************************************************************
279 *
280 * FUNCTION: acpi_ex_release_mutex_object
281 *
282 * PARAMETERS: obj_desc - The object descriptor for this op
283 *
284 * RETURN: Status
285 *
286 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
Bob Moore91e38d12008-04-10 19:06:37 +0400287 * Provides a common path that supports multiple releases (after
288 * previous multiple acquires) by the same thread.
289 *
290 * MUTEX: Interpreter must be locked
291 *
292 * NOTE: This interface is called from three places:
293 * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
294 * 2) From acpi_ex_release_global_lock when an AML Field access requires the
295 * global lock
296 * 3) From the external interface, acpi_release_global_lock
Bob Mooreba886cd2008-04-10 19:06:37 +0400297 *
298 ******************************************************************************/
299
300acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
301{
302 acpi_status status = AE_OK;
303
304 ACPI_FUNCTION_TRACE(ex_release_mutex_object);
305
Bob Mooref02e9fa2008-04-10 19:06:37 +0400306 if (obj_desc->mutex.acquisition_depth == 0) {
Bob Moore68aafc32012-10-31 02:26:01 +0000307 return_ACPI_STATUS(AE_NOT_ACQUIRED);
Bob Mooref02e9fa2008-04-10 19:06:37 +0400308 }
309
Bob Mooreba886cd2008-04-10 19:06:37 +0400310 /* Match multiple Acquires with multiple Releases */
311
312 obj_desc->mutex.acquisition_depth--;
313 if (obj_desc->mutex.acquisition_depth != 0) {
314
315 /* Just decrement the depth and return */
316
317 return_ACPI_STATUS(AE_OK);
318 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319
Len Brown262a7a22007-05-09 23:01:59 -0400320 if (obj_desc->mutex.owner_thread) {
Bob Mooreba886cd2008-04-10 19:06:37 +0400321
322 /* Unlink the mutex from the owner's list */
323
324 acpi_ex_unlink_mutex(obj_desc);
325 obj_desc->mutex.owner_thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 }
327
Bob Mooreba886cd2008-04-10 19:06:37 +0400328 /* Release the mutex, special case for Global Lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Bob Mooreba886cd2008-04-10 19:06:37 +0400330 if (obj_desc == acpi_gbl_global_lock_mutex) {
331 status = acpi_ev_release_global_lock();
Bob Moorec81da662007-02-02 19:48:18 +0300332 } else {
Bob Mooreba886cd2008-04-10 19:06:37 +0400333 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
Bob Moorec81da662007-02-02 19:48:18 +0300334 }
335
Bob Moore91e38d12008-04-10 19:06:37 +0400336 /* Clear mutex info */
337
Lin Ming28eb3fc2010-09-15 13:55:13 +0800338 obj_desc->mutex.thread_id = 0;
Bob Mooreba886cd2008-04-10 19:06:37 +0400339 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340}
341
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342/*******************************************************************************
343 *
344 * FUNCTION: acpi_ex_release_mutex
345 *
346 * PARAMETERS: obj_desc - The object descriptor for this op
Robert Moore44f6c012005-04-18 22:49:35 -0400347 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 *
349 * RETURN: Status
350 *
351 * DESCRIPTION: Release a previously acquired Mutex.
352 *
353 ******************************************************************************/
354
355acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400356acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
357 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358{
Bob Moorec81da662007-02-02 19:48:18 +0300359 acpi_status status = AE_OK;
Bob Moore10a3b462009-05-21 10:02:34 +0800360 u8 previous_sync_level;
Lin Minge0f40282010-03-05 17:59:54 +0800361 struct acpi_thread_state *owner_thread;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700362
Bob Mooreb229cf92006-04-21 17:15:00 -0400363 ACPI_FUNCTION_TRACE(ex_release_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364
365 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400366 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 }
368
Lin Minge0f40282010-03-05 17:59:54 +0800369 owner_thread = obj_desc->mutex.owner_thread;
370
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371 /* The mutex must have been previously acquired in order to release it */
372
Lin Minge0f40282010-03-05 17:59:54 +0800373 if (!owner_thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500374 ACPI_ERROR((AE_INFO,
375 "Cannot release Mutex [%4.4s], not acquired",
376 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400377 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 }
379
Lv Zheng75c80442012-12-19 05:36:49 +0000380 /* Must have a valid thread ID */
Lv Zheng3e8214e2012-12-19 05:37:15 +0000381
Dan Carpenterfbc3be22009-12-11 15:31:40 +0800382 if (!walk_state->thread) {
383 ACPI_ERROR((AE_INFO,
384 "Cannot release Mutex [%4.4s], null thread info",
385 acpi_ut_get_node_name(obj_desc->mutex.node)));
386 return_ACPI_STATUS(AE_AML_INTERNAL);
387 }
388
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 /*
390 * The Mutex is owned, but this thread must be the owner.
391 * Special case for Global Lock, any thread can release
392 */
Lin Minge0f40282010-03-05 17:59:54 +0800393 if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
394 (obj_desc != acpi_gbl_global_lock_mutex)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500395 ACPI_ERROR((AE_INFO,
Lin Ming28eb3fc2010-09-15 13:55:13 +0800396 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
397 (u32)walk_state->thread->thread_id,
Bob Mooreb8e4d892006-01-27 16:43:00 -0500398 acpi_ut_get_node_name(obj_desc->mutex.node),
Lin Ming28eb3fc2010-09-15 13:55:13 +0800399 (u32)owner_thread->thread_id));
Len Brown4be44fc2005-08-05 00:44:28 -0400400 return_ACPI_STATUS(AE_AML_NOT_OWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401 }
402
403 /*
Bob Moore315c7282009-05-21 10:04:33 +0800404 * The sync level of the mutex must be equal to the current sync level. In
405 * other words, the current level means that at least one mutex at that
406 * level is currently being held. Attempting to release a mutex of a
407 * different level can only mean that the mutex ordering rule is being
408 * violated. This behavior is clarified in ACPI 4.0 specification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700409 */
Lin Minge0f40282010-03-05 17:59:54 +0800410 if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500411 ACPI_ERROR((AE_INFO,
Bob Mooref6a22b02010-03-05 17:56:40 +0800412 "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %u current %u",
Bob Mooref02e9fa2008-04-10 19:06:37 +0400413 acpi_ut_get_node_name(obj_desc->mutex.node),
414 obj_desc->mutex.sync_level,
415 walk_state->thread->current_sync_level));
Len Brown4be44fc2005-08-05 00:44:28 -0400416 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
Bob Moore10a3b462009-05-21 10:02:34 +0800419 /*
420 * Get the previous sync_level from the head of the acquired mutex list.
421 * This handles the case where several mutexes at the same level have been
422 * acquired, but are not released in reverse order.
423 */
424 previous_sync_level =
Lin Minge0f40282010-03-05 17:59:54 +0800425 owner_thread->acquired_mutex_list->mutex.original_sync_level;
Bob Moore10a3b462009-05-21 10:02:34 +0800426
Bob Mooreba886cd2008-04-10 19:06:37 +0400427 status = acpi_ex_release_mutex_object(obj_desc);
Bob Moore315c7282009-05-21 10:04:33 +0800428 if (ACPI_FAILURE(status)) {
429 return_ACPI_STATUS(status);
430 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431
Bob Mooref02e9fa2008-04-10 19:06:37 +0400432 if (obj_desc->mutex.acquisition_depth == 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400433
Bob Moore315c7282009-05-21 10:04:33 +0800434 /* Restore the previous sync_level */
Bob Mooref02e9fa2008-04-10 19:06:37 +0400435
Lin Minge0f40282010-03-05 17:59:54 +0800436 owner_thread->current_sync_level = previous_sync_level;
Bob Mooref02e9fa2008-04-10 19:06:37 +0400437 }
Bob Moorea7499bc2010-04-01 11:04:54 +0800438
Len Brown4be44fc2005-08-05 00:44:28 -0400439 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440}
441
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442/*******************************************************************************
443 *
444 * FUNCTION: acpi_ex_release_all_mutexes
445 *
Bob Mooreba494be2012-07-12 09:40:10 +0800446 * PARAMETERS: thread - Current executing thread object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 *
448 * RETURN: Status
449 *
Robert Moore44f6c012005-04-18 22:49:35 -0400450 * DESCRIPTION: Release all mutexes held by this thread
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 *
Bob Mooref70a5e72007-02-02 19:48:21 +0300452 * NOTE: This function is called as the thread is exiting the interpreter.
453 * Mutexes are not released when an individual control method is exited, but
454 * only when the parent thread actually exits the interpreter. This allows one
455 * method to acquire a mutex, and a different method to release it, as long as
456 * this is performed underneath a single parent control method.
457 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 ******************************************************************************/
459
Len Brown4be44fc2005-08-05 00:44:28 -0400460void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461{
Len Brown4be44fc2005-08-05 00:44:28 -0400462 union acpi_operand_object *next = thread->acquired_mutex_list;
Bob Moorec81da662007-02-02 19:48:18 +0300463 union acpi_operand_object *obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Bob Moore2489ef02012-10-31 02:27:24 +0000465 ACPI_FUNCTION_NAME(ex_release_all_mutexes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
467 /* Traverse the list of owned mutexes, releasing each one */
468
469 while (next) {
Bob Moorec81da662007-02-02 19:48:18 +0300470 obj_desc = next;
471 next = obj_desc->mutex.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472
Bob Moorec81da662007-02-02 19:48:18 +0300473 obj_desc->mutex.prev = NULL;
474 obj_desc->mutex.next = NULL;
Bob Mooref70a5e72007-02-02 19:48:21 +0300475 obj_desc->mutex.acquisition_depth = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700476
Bob Moore2489ef02012-10-31 02:27:24 +0000477 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
478 "Force-releasing held mutex: %p\n",
479 obj_desc));
480
Bob Moorec81da662007-02-02 19:48:18 +0300481 /* Release the mutex, special case for Global Lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482
Bob Mooreba886cd2008-04-10 19:06:37 +0400483 if (obj_desc == acpi_gbl_global_lock_mutex) {
Bob Moorec81da662007-02-02 19:48:18 +0300484
485 /* Ignore errors */
486
487 (void)acpi_ev_release_global_lock();
488 } else {
489 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490 }
491
492 /* Mark mutex unowned */
493
Len Brown262a7a22007-05-09 23:01:59 -0400494 obj_desc->mutex.owner_thread = NULL;
Lin Ming28eb3fc2010-09-15 13:55:13 +0800495 obj_desc->mutex.thread_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496
497 /* Update Thread sync_level (Last mutex is the important one) */
498
Bob Moorec81da662007-02-02 19:48:18 +0300499 thread->current_sync_level =
500 obj_desc->mutex.original_sync_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502}