PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / acpi / acpica / exmutex.c
blob7be0205ad06797a1037977c6757a879f28bb1810
1 /******************************************************************************
3 * Module Name: exmutex - ASL Mutex Acquire/Release functions
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acinterp.h"
47 #include "acevents.h"
49 #define _COMPONENT ACPI_EXECUTER
50 ACPI_MODULE_NAME("exmutex")
52 /* Local prototypes */
53 static void
54 acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
55 struct acpi_thread_state *thread);
57 /*******************************************************************************
59 * FUNCTION: acpi_ex_unlink_mutex
61 * PARAMETERS: obj_desc - The mutex to be unlinked
63 * RETURN: None
65 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
67 ******************************************************************************/
69 void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
71 struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
73 if (!thread) {
74 return;
77 /* Doubly linked list */
79 if (obj_desc->mutex.next) {
80 (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
83 if (obj_desc->mutex.prev) {
84 (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
87 * 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.
92 (obj_desc->mutex.prev)->mutex.original_sync_level =
93 obj_desc->mutex.original_sync_level;
94 } else {
95 thread->acquired_mutex_list = obj_desc->mutex.next;
99 /*******************************************************************************
101 * FUNCTION: acpi_ex_link_mutex
103 * PARAMETERS: obj_desc - The mutex to be linked
104 * thread - Current executing thread object
106 * RETURN: None
108 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
110 ******************************************************************************/
112 static void
113 acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
114 struct acpi_thread_state *thread)
116 union acpi_operand_object *list_head;
118 list_head = thread->acquired_mutex_list;
120 /* This object will be the first object in the list */
122 obj_desc->mutex.prev = NULL;
123 obj_desc->mutex.next = list_head;
125 /* Update old first object to point back to this object */
127 if (list_head) {
128 list_head->mutex.prev = obj_desc;
131 /* Update list head */
133 thread->acquired_mutex_list = obj_desc;
136 /*******************************************************************************
138 * FUNCTION: acpi_ex_acquire_mutex_object
140 * PARAMETERS: timeout - Timeout in milliseconds
141 * obj_desc - Mutex object
142 * thread_id - Current thread state
144 * RETURN: Status
146 * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
147 * path that supports multiple acquires by the same thread.
149 * MUTEX: Interpreter must be locked
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
157 ******************************************************************************/
159 acpi_status
160 acpi_ex_acquire_mutex_object(u16 timeout,
161 union acpi_operand_object *obj_desc,
162 acpi_thread_id thread_id)
164 acpi_status status;
166 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
168 if (!obj_desc) {
169 return_ACPI_STATUS(AE_BAD_PARAMETER);
172 /* Support for multiple acquires by the owning thread */
174 if (obj_desc->mutex.thread_id == thread_id) {
176 * The mutex is already owned by this thread, just increment the
177 * acquisition depth
179 obj_desc->mutex.acquisition_depth++;
180 return_ACPI_STATUS(AE_OK);
183 /* Acquire the mutex, wait if necessary. Special case for Global Lock */
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);
192 if (ACPI_FAILURE(status)) {
194 /* Includes failure from a timeout on time_desc */
196 return_ACPI_STATUS(status);
199 /* Acquired the mutex: update mutex object */
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() */
206 return_ACPI_STATUS(AE_OK);
209 /*******************************************************************************
211 * FUNCTION: acpi_ex_acquire_mutex
213 * PARAMETERS: time_desc - Timeout integer
214 * obj_desc - Mutex object
215 * walk_state - Current method execution state
217 * RETURN: Status
219 * DESCRIPTION: Acquire an AML mutex
221 ******************************************************************************/
223 acpi_status
224 acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
225 union acpi_operand_object *obj_desc,
226 struct acpi_walk_state *walk_state)
228 acpi_status status;
230 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
232 if (!obj_desc) {
233 return_ACPI_STATUS(AE_BAD_PARAMETER);
236 /* Must have a valid thread state struct */
238 if (!walk_state->thread) {
239 ACPI_ERROR((AE_INFO,
240 "Cannot acquire Mutex [%4.4s], null thread info",
241 acpi_ut_get_node_name(obj_desc->mutex.node)));
242 return_ACPI_STATUS(AE_AML_INTERNAL);
246 * Current sync level must be less than or equal to the sync level of the
247 * mutex. This mechanism provides some deadlock prevention
249 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
250 ACPI_ERROR((AE_INFO,
251 "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%u)",
252 acpi_ut_get_node_name(obj_desc->mutex.node),
253 walk_state->thread->current_sync_level));
254 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
257 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) {
262 /* Save Thread object, original/current sync levels */
264 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;
270 /* Link the mutex to the current thread for force-unlock at method exit */
272 acpi_ex_link_mutex(obj_desc, walk_state->thread);
275 return_ACPI_STATUS(status);
278 /*******************************************************************************
280 * FUNCTION: acpi_ex_release_mutex_object
282 * PARAMETERS: obj_desc - The object descriptor for this op
284 * RETURN: Status
286 * DESCRIPTION: Release a previously acquired Mutex, low level interface.
287 * Provides a common path that supports multiple releases (after
288 * previous multiple acquires) by the same thread.
290 * MUTEX: Interpreter must be locked
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
298 ******************************************************************************/
300 acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
302 acpi_status status = AE_OK;
304 ACPI_FUNCTION_TRACE(ex_release_mutex_object);
306 if (obj_desc->mutex.acquisition_depth == 0) {
307 return_ACPI_STATUS(AE_NOT_ACQUIRED);
310 /* Match multiple Acquires with multiple Releases */
312 obj_desc->mutex.acquisition_depth--;
313 if (obj_desc->mutex.acquisition_depth != 0) {
315 /* Just decrement the depth and return */
317 return_ACPI_STATUS(AE_OK);
320 if (obj_desc->mutex.owner_thread) {
322 /* Unlink the mutex from the owner's list */
324 acpi_ex_unlink_mutex(obj_desc);
325 obj_desc->mutex.owner_thread = NULL;
328 /* Release the mutex, special case for Global Lock */
330 if (obj_desc == acpi_gbl_global_lock_mutex) {
331 status = acpi_ev_release_global_lock();
332 } else {
333 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
336 /* Clear mutex info */
338 obj_desc->mutex.thread_id = 0;
339 return_ACPI_STATUS(status);
342 /*******************************************************************************
344 * FUNCTION: acpi_ex_release_mutex
346 * PARAMETERS: obj_desc - The object descriptor for this op
347 * walk_state - Current method execution state
349 * RETURN: Status
351 * DESCRIPTION: Release a previously acquired Mutex.
353 ******************************************************************************/
355 acpi_status
356 acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
357 struct acpi_walk_state *walk_state)
359 acpi_status status = AE_OK;
360 u8 previous_sync_level;
361 struct acpi_thread_state *owner_thread;
363 ACPI_FUNCTION_TRACE(ex_release_mutex);
365 if (!obj_desc) {
366 return_ACPI_STATUS(AE_BAD_PARAMETER);
369 owner_thread = obj_desc->mutex.owner_thread;
371 /* The mutex must have been previously acquired in order to release it */
373 if (!owner_thread) {
374 ACPI_ERROR((AE_INFO,
375 "Cannot release Mutex [%4.4s], not acquired",
376 acpi_ut_get_node_name(obj_desc->mutex.node)));
377 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
380 /* Must have a valid thread ID */
382 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);
390 * The Mutex is owned, but this thread must be the owner.
391 * Special case for Global Lock, any thread can release
393 if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
394 (obj_desc != acpi_gbl_global_lock_mutex)) {
395 ACPI_ERROR((AE_INFO,
396 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
397 (u32)walk_state->thread->thread_id,
398 acpi_ut_get_node_name(obj_desc->mutex.node),
399 (u32)owner_thread->thread_id));
400 return_ACPI_STATUS(AE_AML_NOT_OWNER);
404 * 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.
410 if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
411 ACPI_ERROR((AE_INFO,
412 "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %u current %u",
413 acpi_ut_get_node_name(obj_desc->mutex.node),
414 obj_desc->mutex.sync_level,
415 walk_state->thread->current_sync_level));
416 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
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.
424 previous_sync_level =
425 owner_thread->acquired_mutex_list->mutex.original_sync_level;
427 status = acpi_ex_release_mutex_object(obj_desc);
428 if (ACPI_FAILURE(status)) {
429 return_ACPI_STATUS(status);
432 if (obj_desc->mutex.acquisition_depth == 0) {
434 /* Restore the previous sync_level */
436 owner_thread->current_sync_level = previous_sync_level;
439 return_ACPI_STATUS(status);
442 /*******************************************************************************
444 * FUNCTION: acpi_ex_release_all_mutexes
446 * PARAMETERS: thread - Current executing thread object
448 * RETURN: Status
450 * DESCRIPTION: Release all mutexes held by this thread
452 * 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.
458 ******************************************************************************/
460 void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
462 union acpi_operand_object *next = thread->acquired_mutex_list;
463 union acpi_operand_object *obj_desc;
465 ACPI_FUNCTION_NAME(ex_release_all_mutexes);
467 /* Traverse the list of owned mutexes, releasing each one */
469 while (next) {
470 obj_desc = next;
471 next = obj_desc->mutex.next;
473 obj_desc->mutex.prev = NULL;
474 obj_desc->mutex.next = NULL;
475 obj_desc->mutex.acquisition_depth = 0;
477 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
478 "Force-releasing held mutex: %p\n",
479 obj_desc));
481 /* Release the mutex, special case for Global Lock */
483 if (obj_desc == acpi_gbl_global_lock_mutex) {
485 /* Ignore errors */
487 (void)acpi_ev_release_global_lock();
488 } else {
489 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
492 /* Mark mutex unowned */
494 obj_desc->mutex.owner_thread = NULL;
495 obj_desc->mutex.thread_id = 0;
497 /* Update Thread sync_level (Last mutex is the important one) */
499 thread->current_sync_level =
500 obj_desc->mutex.original_sync_level;