blob: 26faa91e930c42b013a8f6125c72dd745ec7cc1b [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 Moorec8100dc2016-01-15 08:17:03 +08008 * Copyright (C) 2000 - 2016, 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 {
Bob Moorecd162b32015-12-29 13:54:28 +0800188 status =
189 acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
190 timeout);
Bob Mooreba886cd2008-04-10 19:06:37 +0400191 }
192
193 if (ACPI_FAILURE(status)) {
194
195 /* Includes failure from a timeout on time_desc */
196
197 return_ACPI_STATUS(status);
198 }
199
Bob Moore91e38d12008-04-10 19:06:37 +0400200 /* Acquired the mutex: update mutex object */
Bob Mooreba886cd2008-04-10 19:06:37 +0400201
202 obj_desc->mutex.thread_id = thread_id;
203 obj_desc->mutex.acquisition_depth = 1;
204 obj_desc->mutex.original_sync_level = 0;
205 obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */
206
207 return_ACPI_STATUS(AE_OK);
208}
209
210/*******************************************************************************
211 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 * FUNCTION: acpi_ex_acquire_mutex
213 *
Robert Moore44f6c012005-04-18 22:49:35 -0400214 * PARAMETERS: time_desc - Timeout integer
215 * obj_desc - Mutex object
216 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 *
218 * RETURN: Status
219 *
220 * DESCRIPTION: Acquire an AML mutex
221 *
222 ******************************************************************************/
223
224acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400225acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
226 union acpi_operand_object *obj_desc,
227 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228{
Len Brown4be44fc2005-08-05 00:44:28 -0400229 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230
Bob Mooreb229cf92006-04-21 17:15:00 -0400231 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
233 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400234 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700235 }
236
Bob Moorea7499bc2010-04-01 11:04:54 +0800237 /* Must have a valid thread state struct */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
239 if (!walk_state->thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500240 ACPI_ERROR((AE_INFO,
241 "Cannot acquire Mutex [%4.4s], null thread info",
242 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400243 return_ACPI_STATUS(AE_AML_INTERNAL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /*
Bob Moorecd162b32015-12-29 13:54:28 +0800247 * Current sync level must be less than or equal to the sync level
248 * of the mutex. This mechanism provides some deadlock prevention.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 */
250 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500251 ACPI_ERROR((AE_INFO,
Bob Moorecd162b32015-12-29 13:54:28 +0800252 "Cannot acquire Mutex [%4.4s], "
253 "current SyncLevel is too large (%u)",
Bob Moore967440e32006-06-23 17:04:00 -0400254 acpi_ut_get_node_name(obj_desc->mutex.node),
255 walk_state->thread->current_sync_level));
Len Brown4be44fc2005-08-05 00:44:28 -0400256 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257 }
258
Bob Moorecd162b32015-12-29 13:54:28 +0800259 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
260 "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
261 "Depth %u TID %p\n",
262 obj_desc->mutex.sync_level,
263 walk_state->thread->current_sync_level,
264 obj_desc->mutex.acquisition_depth,
265 walk_state->thread));
266
Lv Zheng5431b652015-12-29 13:52:32 +0800267 status = acpi_ex_acquire_mutex_object((u16)time_desc->integer.value,
Bob Mooreba886cd2008-04-10 19:06:37 +0400268 obj_desc,
269 walk_state->thread->thread_id);
Bob Moorecd162b32015-12-29 13:54:28 +0800270
Bob Mooreba886cd2008-04-10 19:06:37 +0400271 if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
Bob Moore91e38d12008-04-10 19:06:37 +0400272
273 /* Save Thread object, original/current sync levels */
274
Bob Mooreba886cd2008-04-10 19:06:37 +0400275 obj_desc->mutex.owner_thread = walk_state->thread;
276 obj_desc->mutex.original_sync_level =
277 walk_state->thread->current_sync_level;
278 walk_state->thread->current_sync_level =
279 obj_desc->mutex.sync_level;
280
281 /* Link the mutex to the current thread for force-unlock at method exit */
282
283 acpi_ex_link_mutex(obj_desc, walk_state->thread);
284 }
285
Bob Moorecd162b32015-12-29 13:54:28 +0800286 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
287 "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
288 obj_desc->mutex.sync_level,
289 walk_state->thread->current_sync_level,
290 obj_desc->mutex.acquisition_depth));
291
Bob Mooreba886cd2008-04-10 19:06:37 +0400292 return_ACPI_STATUS(status);
293}
294
295/*******************************************************************************
296 *
297 * FUNCTION: acpi_ex_release_mutex_object
298 *
299 * PARAMETERS: obj_desc - The object descriptor for this op
300 *
301 * RETURN: Status
302 *
303 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
Bob Moore91e38d12008-04-10 19:06:37 +0400304 * Provides a common path that supports multiple releases (after
305 * previous multiple acquires) by the same thread.
306 *
307 * MUTEX: Interpreter must be locked
308 *
309 * NOTE: This interface is called from three places:
310 * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
311 * 2) From acpi_ex_release_global_lock when an AML Field access requires the
312 * global lock
313 * 3) From the external interface, acpi_release_global_lock
Bob Mooreba886cd2008-04-10 19:06:37 +0400314 *
315 ******************************************************************************/
316
317acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
318{
319 acpi_status status = AE_OK;
320
321 ACPI_FUNCTION_TRACE(ex_release_mutex_object);
322
Bob Mooref02e9fa2008-04-10 19:06:37 +0400323 if (obj_desc->mutex.acquisition_depth == 0) {
Bob Moore68aafc32012-10-31 02:26:01 +0000324 return_ACPI_STATUS(AE_NOT_ACQUIRED);
Bob Mooref02e9fa2008-04-10 19:06:37 +0400325 }
326
Bob Mooreba886cd2008-04-10 19:06:37 +0400327 /* Match multiple Acquires with multiple Releases */
328
329 obj_desc->mutex.acquisition_depth--;
330 if (obj_desc->mutex.acquisition_depth != 0) {
331
332 /* Just decrement the depth and return */
333
334 return_ACPI_STATUS(AE_OK);
335 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336
Len Brown262a7a22007-05-09 23:01:59 -0400337 if (obj_desc->mutex.owner_thread) {
Bob Mooreba886cd2008-04-10 19:06:37 +0400338
339 /* Unlink the mutex from the owner's list */
340
341 acpi_ex_unlink_mutex(obj_desc);
342 obj_desc->mutex.owner_thread = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
Bob Mooreba886cd2008-04-10 19:06:37 +0400345 /* Release the mutex, special case for Global Lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346
Bob Mooreba886cd2008-04-10 19:06:37 +0400347 if (obj_desc == acpi_gbl_global_lock_mutex) {
348 status = acpi_ev_release_global_lock();
Bob Moorec81da662007-02-02 19:48:18 +0300349 } else {
Bob Mooreba886cd2008-04-10 19:06:37 +0400350 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
Bob Moorec81da662007-02-02 19:48:18 +0300351 }
352
Bob Moore91e38d12008-04-10 19:06:37 +0400353 /* Clear mutex info */
354
Lin Ming28eb3fc2010-09-15 13:55:13 +0800355 obj_desc->mutex.thread_id = 0;
Bob Mooreba886cd2008-04-10 19:06:37 +0400356 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357}
358
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359/*******************************************************************************
360 *
361 * FUNCTION: acpi_ex_release_mutex
362 *
363 * PARAMETERS: obj_desc - The object descriptor for this op
Robert Moore44f6c012005-04-18 22:49:35 -0400364 * walk_state - Current method execution state
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 *
366 * RETURN: Status
367 *
368 * DESCRIPTION: Release a previously acquired Mutex.
369 *
370 ******************************************************************************/
371
372acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400373acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
374 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375{
Bob Moore10a3b462009-05-21 10:02:34 +0800376 u8 previous_sync_level;
Lin Minge0f40282010-03-05 17:59:54 +0800377 struct acpi_thread_state *owner_thread;
Bob Moorecd162b32015-12-29 13:54:28 +0800378 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379
Bob Mooreb229cf92006-04-21 17:15:00 -0400380 ACPI_FUNCTION_TRACE(ex_release_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381
382 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400383 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
Lin Minge0f40282010-03-05 17:59:54 +0800386 owner_thread = obj_desc->mutex.owner_thread;
387
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 /* The mutex must have been previously acquired in order to release it */
389
Lin Minge0f40282010-03-05 17:59:54 +0800390 if (!owner_thread) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500391 ACPI_ERROR((AE_INFO,
392 "Cannot release Mutex [%4.4s], not acquired",
393 acpi_ut_get_node_name(obj_desc->mutex.node)));
Len Brown4be44fc2005-08-05 00:44:28 -0400394 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700395 }
396
Lv Zheng75c80442012-12-19 05:36:49 +0000397 /* Must have a valid thread ID */
Lv Zheng3e8214e2012-12-19 05:37:15 +0000398
Dan Carpenterfbc3be22009-12-11 15:31:40 +0800399 if (!walk_state->thread) {
400 ACPI_ERROR((AE_INFO,
401 "Cannot release Mutex [%4.4s], null thread info",
402 acpi_ut_get_node_name(obj_desc->mutex.node)));
403 return_ACPI_STATUS(AE_AML_INTERNAL);
404 }
405
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * The Mutex is owned, but this thread must be the owner.
408 * Special case for Global Lock, any thread can release
409 */
Lin Minge0f40282010-03-05 17:59:54 +0800410 if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
411 (obj_desc != acpi_gbl_global_lock_mutex)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500412 ACPI_ERROR((AE_INFO,
Lin Ming28eb3fc2010-09-15 13:55:13 +0800413 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
414 (u32)walk_state->thread->thread_id,
Bob Mooreb8e4d892006-01-27 16:43:00 -0500415 acpi_ut_get_node_name(obj_desc->mutex.node),
Lin Ming28eb3fc2010-09-15 13:55:13 +0800416 (u32)owner_thread->thread_id));
Len Brown4be44fc2005-08-05 00:44:28 -0400417 return_ACPI_STATUS(AE_AML_NOT_OWNER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 }
419
420 /*
Bob Moore315c7282009-05-21 10:04:33 +0800421 * The sync level of the mutex must be equal to the current sync level. In
422 * other words, the current level means that at least one mutex at that
423 * level is currently being held. Attempting to release a mutex of a
424 * different level can only mean that the mutex ordering rule is being
425 * violated. This behavior is clarified in ACPI 4.0 specification.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 */
Lin Minge0f40282010-03-05 17:59:54 +0800427 if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500428 ACPI_ERROR((AE_INFO,
Bob Moorecd162b32015-12-29 13:54:28 +0800429 "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
430 "mutex %u current %u",
Bob Mooref02e9fa2008-04-10 19:06:37 +0400431 acpi_ut_get_node_name(obj_desc->mutex.node),
432 obj_desc->mutex.sync_level,
433 walk_state->thread->current_sync_level));
Len Brown4be44fc2005-08-05 00:44:28 -0400434 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435 }
436
Bob Moore10a3b462009-05-21 10:02:34 +0800437 /*
438 * Get the previous sync_level from the head of the acquired mutex list.
439 * This handles the case where several mutexes at the same level have been
440 * acquired, but are not released in reverse order.
441 */
442 previous_sync_level =
Lin Minge0f40282010-03-05 17:59:54 +0800443 owner_thread->acquired_mutex_list->mutex.original_sync_level;
Bob Moore10a3b462009-05-21 10:02:34 +0800444
Bob Moorecd162b32015-12-29 13:54:28 +0800445 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
446 "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
447 "Prev SyncLevel %u, Depth %u TID %p\n",
448 obj_desc->mutex.sync_level,
449 walk_state->thread->current_sync_level,
450 previous_sync_level,
451 obj_desc->mutex.acquisition_depth,
452 walk_state->thread));
453
Bob Mooreba886cd2008-04-10 19:06:37 +0400454 status = acpi_ex_release_mutex_object(obj_desc);
Bob Moore315c7282009-05-21 10:04:33 +0800455 if (ACPI_FAILURE(status)) {
456 return_ACPI_STATUS(status);
457 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458
Bob Mooref02e9fa2008-04-10 19:06:37 +0400459 if (obj_desc->mutex.acquisition_depth == 0) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400460
Bob Moore315c7282009-05-21 10:04:33 +0800461 /* Restore the previous sync_level */
Bob Mooref02e9fa2008-04-10 19:06:37 +0400462
Lin Minge0f40282010-03-05 17:59:54 +0800463 owner_thread->current_sync_level = previous_sync_level;
Bob Mooref02e9fa2008-04-10 19:06:37 +0400464 }
Bob Moorea7499bc2010-04-01 11:04:54 +0800465
Bob Moorecd162b32015-12-29 13:54:28 +0800466 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
467 "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
468 "Prev SyncLevel %u, Depth %u\n",
469 obj_desc->mutex.sync_level,
470 walk_state->thread->current_sync_level,
471 previous_sync_level,
472 obj_desc->mutex.acquisition_depth));
473
Len Brown4be44fc2005-08-05 00:44:28 -0400474 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700475}
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477/*******************************************************************************
478 *
479 * FUNCTION: acpi_ex_release_all_mutexes
480 *
Bob Mooreba494be2012-07-12 09:40:10 +0800481 * PARAMETERS: thread - Current executing thread object
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 *
483 * RETURN: Status
484 *
Robert Moore44f6c012005-04-18 22:49:35 -0400485 * DESCRIPTION: Release all mutexes held by this thread
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 *
Bob Mooref70a5e72007-02-02 19:48:21 +0300487 * NOTE: This function is called as the thread is exiting the interpreter.
488 * Mutexes are not released when an individual control method is exited, but
489 * only when the parent thread actually exits the interpreter. This allows one
490 * method to acquire a mutex, and a different method to release it, as long as
491 * this is performed underneath a single parent control method.
492 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 ******************************************************************************/
494
Len Brown4be44fc2005-08-05 00:44:28 -0400495void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496{
Len Brown4be44fc2005-08-05 00:44:28 -0400497 union acpi_operand_object *next = thread->acquired_mutex_list;
Bob Moorec81da662007-02-02 19:48:18 +0300498 union acpi_operand_object *obj_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Bob Moorecd162b32015-12-29 13:54:28 +0800500 ACPI_FUNCTION_TRACE(ex_release_all_mutexes);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501
502 /* Traverse the list of owned mutexes, releasing each one */
503
504 while (next) {
Bob Moorec81da662007-02-02 19:48:18 +0300505 obj_desc = next;
Bob Moore2489ef02012-10-31 02:27:24 +0000506 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
Bob Moorecd162b32015-12-29 13:54:28 +0800507 "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
508 obj_desc->mutex.node->name.ascii,
509 obj_desc->mutex.sync_level,
510 obj_desc->mutex.acquisition_depth));
Bob Moore2489ef02012-10-31 02:27:24 +0000511
Bob Moorec81da662007-02-02 19:48:18 +0300512 /* Release the mutex, special case for Global Lock */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700513
Bob Mooreba886cd2008-04-10 19:06:37 +0400514 if (obj_desc == acpi_gbl_global_lock_mutex) {
Bob Moorec81da662007-02-02 19:48:18 +0300515
516 /* Ignore errors */
517
518 (void)acpi_ev_release_global_lock();
519 } else {
520 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 }
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 /* Update Thread sync_level (Last mutex is the important one) */
524
Bob Moorec81da662007-02-02 19:48:18 +0300525 thread->current_sync_level =
526 obj_desc->mutex.original_sync_level;
Bob Moorecd162b32015-12-29 13:54:28 +0800527
528 /* Mark mutex unowned */
529
530 next = obj_desc->mutex.next;
531
532 obj_desc->mutex.prev = NULL;
533 obj_desc->mutex.next = NULL;
534 obj_desc->mutex.acquisition_depth = 0;
535 obj_desc->mutex.owner_thread = NULL;
536 obj_desc->mutex.thread_id = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Bob Moorecd162b32015-12-29 13:54:28 +0800538
539 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540}