xref: /linux/drivers/acpi/acpica/exmutex.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown  *
495b482a8SLen Brown  * Module Name: exmutex - ASL Mutex Acquire/Release functions
595b482a8SLen Brown  *
6*612c2932SBob Moore  * Copyright (C) 2000 - 2023, Intel Corp.
795b482a8SLen Brown  *
895857638SErik Schmauss  *****************************************************************************/
995b482a8SLen Brown 
1095b482a8SLen Brown #include <acpi/acpi.h>
11e2f7a777SLen Brown #include "accommon.h"
12e2f7a777SLen Brown #include "acinterp.h"
13e2f7a777SLen Brown #include "acevents.h"
1495b482a8SLen Brown 
1595b482a8SLen Brown #define _COMPONENT          ACPI_EXECUTER
1695b482a8SLen Brown ACPI_MODULE_NAME("exmutex")
1795b482a8SLen Brown 
1895b482a8SLen Brown /* Local prototypes */
1995b482a8SLen Brown static void
2095b482a8SLen Brown acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
2195b482a8SLen Brown 		   struct acpi_thread_state *thread);
2295b482a8SLen Brown 
2395b482a8SLen Brown /*******************************************************************************
2495b482a8SLen Brown  *
2595b482a8SLen Brown  * FUNCTION:    acpi_ex_unlink_mutex
2695b482a8SLen Brown  *
2795b482a8SLen Brown  * PARAMETERS:  obj_desc            - The mutex to be unlinked
2895b482a8SLen Brown  *
2995b482a8SLen Brown  * RETURN:      None
3095b482a8SLen Brown  *
3195b482a8SLen Brown  * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
3295b482a8SLen Brown  *
3395b482a8SLen Brown  ******************************************************************************/
3495b482a8SLen Brown 
acpi_ex_unlink_mutex(union acpi_operand_object * obj_desc)3595b482a8SLen Brown void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
3695b482a8SLen Brown {
3795b482a8SLen Brown 	struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
3895b482a8SLen Brown 
3995b482a8SLen Brown 	if (!thread) {
4095b482a8SLen Brown 		return;
4195b482a8SLen Brown 	}
4295b482a8SLen Brown 
4395b482a8SLen Brown 	/* Doubly linked list */
4495b482a8SLen Brown 
4595b482a8SLen Brown 	if (obj_desc->mutex.next) {
4695b482a8SLen Brown 		(obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
4795b482a8SLen Brown 	}
4895b482a8SLen Brown 
4995b482a8SLen Brown 	if (obj_desc->mutex.prev) {
5095b482a8SLen Brown 		(obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
5110a3b461SBob Moore 
5210a3b461SBob Moore 		/*
53a7499bc8SBob Moore 		 * Migrate the previous sync level associated with this mutex to
54a7499bc8SBob Moore 		 * the previous mutex on the list so that it may be preserved.
55a7499bc8SBob Moore 		 * This handles the case where several mutexes have been acquired
56a7499bc8SBob Moore 		 * at the same level, but are not released in opposite order.
5710a3b461SBob Moore 		 */
5810a3b461SBob Moore 		(obj_desc->mutex.prev)->mutex.original_sync_level =
5910a3b461SBob Moore 		    obj_desc->mutex.original_sync_level;
6095b482a8SLen Brown 	} else {
6195b482a8SLen Brown 		thread->acquired_mutex_list = obj_desc->mutex.next;
6295b482a8SLen Brown 	}
6395b482a8SLen Brown }
6495b482a8SLen Brown 
6595b482a8SLen Brown /*******************************************************************************
6695b482a8SLen Brown  *
6795b482a8SLen Brown  * FUNCTION:    acpi_ex_link_mutex
6895b482a8SLen Brown  *
6995b482a8SLen Brown  * PARAMETERS:  obj_desc            - The mutex to be linked
70ba494beeSBob Moore  *              thread              - Current executing thread object
7195b482a8SLen Brown  *
7295b482a8SLen Brown  * RETURN:      None
7395b482a8SLen Brown  *
7495b482a8SLen Brown  * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
7595b482a8SLen Brown  *
7695b482a8SLen Brown  ******************************************************************************/
7795b482a8SLen Brown 
7895b482a8SLen Brown static void
acpi_ex_link_mutex(union acpi_operand_object * obj_desc,struct acpi_thread_state * thread)7995b482a8SLen Brown acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
8095b482a8SLen Brown 		   struct acpi_thread_state *thread)
8195b482a8SLen Brown {
8295b482a8SLen Brown 	union acpi_operand_object *list_head;
8395b482a8SLen Brown 
8495b482a8SLen Brown 	list_head = thread->acquired_mutex_list;
8595b482a8SLen Brown 
8695b482a8SLen Brown 	/* This object will be the first object in the list */
8795b482a8SLen Brown 
8895b482a8SLen Brown 	obj_desc->mutex.prev = NULL;
8995b482a8SLen Brown 	obj_desc->mutex.next = list_head;
9095b482a8SLen Brown 
9195b482a8SLen Brown 	/* Update old first object to point back to this object */
9295b482a8SLen Brown 
9395b482a8SLen Brown 	if (list_head) {
9495b482a8SLen Brown 		list_head->mutex.prev = obj_desc;
9595b482a8SLen Brown 	}
9695b482a8SLen Brown 
9795b482a8SLen Brown 	/* Update list head */
9895b482a8SLen Brown 
9995b482a8SLen Brown 	thread->acquired_mutex_list = obj_desc;
10095b482a8SLen Brown }
10195b482a8SLen Brown 
10295b482a8SLen Brown /*******************************************************************************
10395b482a8SLen Brown  *
10495b482a8SLen Brown  * FUNCTION:    acpi_ex_acquire_mutex_object
10595b482a8SLen Brown  *
106ba494beeSBob Moore  * PARAMETERS:  timeout             - Timeout in milliseconds
10795b482a8SLen Brown  *              obj_desc            - Mutex object
108a7499bc8SBob Moore  *              thread_id           - Current thread state
10995b482a8SLen Brown  *
11095b482a8SLen Brown  * RETURN:      Status
11195b482a8SLen Brown  *
11295b482a8SLen Brown  * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common
11395b482a8SLen Brown  *              path that supports multiple acquires by the same thread.
11495b482a8SLen Brown  *
11595b482a8SLen Brown  * MUTEX:       Interpreter must be locked
11695b482a8SLen Brown  *
11795b482a8SLen Brown  * NOTE: This interface is called from three places:
11895b482a8SLen Brown  * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator
11995b482a8SLen Brown  * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the
12095b482a8SLen Brown  *    global lock
12195b482a8SLen Brown  * 3) From the external interface, acpi_acquire_global_lock
12295b482a8SLen Brown  *
12395b482a8SLen Brown  ******************************************************************************/
12495b482a8SLen Brown 
12595b482a8SLen Brown acpi_status
acpi_ex_acquire_mutex_object(u16 timeout,union acpi_operand_object * obj_desc,acpi_thread_id thread_id)12695b482a8SLen Brown acpi_ex_acquire_mutex_object(u16 timeout,
12795b482a8SLen Brown 			     union acpi_operand_object *obj_desc,
12895b482a8SLen Brown 			     acpi_thread_id thread_id)
12995b482a8SLen Brown {
13095b482a8SLen Brown 	acpi_status status;
13195b482a8SLen Brown 
13295b482a8SLen Brown 	ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
13395b482a8SLen Brown 
13495b482a8SLen Brown 	if (!obj_desc) {
13595b482a8SLen Brown 		return_ACPI_STATUS(AE_BAD_PARAMETER);
13695b482a8SLen Brown 	}
13795b482a8SLen Brown 
13895b482a8SLen Brown 	/* Support for multiple acquires by the owning thread */
13995b482a8SLen Brown 
14095b482a8SLen Brown 	if (obj_desc->mutex.thread_id == thread_id) {
14195b482a8SLen Brown 		/*
14295b482a8SLen Brown 		 * The mutex is already owned by this thread, just increment the
14395b482a8SLen Brown 		 * acquisition depth
14495b482a8SLen Brown 		 */
14595b482a8SLen Brown 		obj_desc->mutex.acquisition_depth++;
14695b482a8SLen Brown 		return_ACPI_STATUS(AE_OK);
14795b482a8SLen Brown 	}
14895b482a8SLen Brown 
14995b482a8SLen Brown 	/* Acquire the mutex, wait if necessary. Special case for Global Lock */
15095b482a8SLen Brown 
15195b482a8SLen Brown 	if (obj_desc == acpi_gbl_global_lock_mutex) {
15295b482a8SLen Brown 		status = acpi_ev_acquire_global_lock(timeout);
15395b482a8SLen Brown 	} else {
154cd162b35SBob Moore 		status =
155cd162b35SBob Moore 		    acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
15695b482a8SLen Brown 					      timeout);
15795b482a8SLen Brown 	}
15895b482a8SLen Brown 
15995b482a8SLen Brown 	if (ACPI_FAILURE(status)) {
16095b482a8SLen Brown 
16195b482a8SLen Brown 		/* Includes failure from a timeout on time_desc */
16295b482a8SLen Brown 
16395b482a8SLen Brown 		return_ACPI_STATUS(status);
16495b482a8SLen Brown 	}
16595b482a8SLen Brown 
16695b482a8SLen Brown 	/* Acquired the mutex: update mutex object */
16795b482a8SLen Brown 
16895b482a8SLen Brown 	obj_desc->mutex.thread_id = thread_id;
16995b482a8SLen Brown 	obj_desc->mutex.acquisition_depth = 1;
17095b482a8SLen Brown 	obj_desc->mutex.original_sync_level = 0;
17195b482a8SLen Brown 	obj_desc->mutex.owner_thread = NULL;	/* Used only for AML Acquire() */
17295b482a8SLen Brown 
17395b482a8SLen Brown 	return_ACPI_STATUS(AE_OK);
17495b482a8SLen Brown }
17595b482a8SLen Brown 
17695b482a8SLen Brown /*******************************************************************************
17795b482a8SLen Brown  *
17895b482a8SLen Brown  * FUNCTION:    acpi_ex_acquire_mutex
17995b482a8SLen Brown  *
18095b482a8SLen Brown  * PARAMETERS:  time_desc           - Timeout integer
18195b482a8SLen Brown  *              obj_desc            - Mutex object
18295b482a8SLen Brown  *              walk_state          - Current method execution state
18395b482a8SLen Brown  *
18495b482a8SLen Brown  * RETURN:      Status
18595b482a8SLen Brown  *
18695b482a8SLen Brown  * DESCRIPTION: Acquire an AML mutex
18795b482a8SLen Brown  *
18895b482a8SLen Brown  ******************************************************************************/
18995b482a8SLen Brown 
19095b482a8SLen Brown acpi_status
acpi_ex_acquire_mutex(union acpi_operand_object * time_desc,union acpi_operand_object * obj_desc,struct acpi_walk_state * walk_state)19195b482a8SLen Brown acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
19295b482a8SLen Brown 		      union acpi_operand_object *obj_desc,
19395b482a8SLen Brown 		      struct acpi_walk_state *walk_state)
19495b482a8SLen Brown {
19595b482a8SLen Brown 	acpi_status status;
19695b482a8SLen Brown 
19795b482a8SLen Brown 	ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
19895b482a8SLen Brown 
19995b482a8SLen Brown 	if (!obj_desc) {
20095b482a8SLen Brown 		return_ACPI_STATUS(AE_BAD_PARAMETER);
20195b482a8SLen Brown 	}
20295b482a8SLen Brown 
203a7499bc8SBob Moore 	/* Must have a valid thread state struct */
20495b482a8SLen Brown 
20595b482a8SLen Brown 	if (!walk_state->thread) {
20695b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
20795b482a8SLen Brown 			    "Cannot acquire Mutex [%4.4s], null thread info",
20895b482a8SLen Brown 			    acpi_ut_get_node_name(obj_desc->mutex.node)));
20995b482a8SLen Brown 		return_ACPI_STATUS(AE_AML_INTERNAL);
21095b482a8SLen Brown 	}
21195b482a8SLen Brown 
21295b482a8SLen Brown 	/*
213cd162b35SBob Moore 	 * Current sync level must be less than or equal to the sync level
214cd162b35SBob Moore 	 * of the mutex. This mechanism provides some deadlock prevention.
21595b482a8SLen Brown 	 */
21695b482a8SLen Brown 	if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
21795b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
218cd162b35SBob Moore 			    "Cannot acquire Mutex [%4.4s], "
219cd162b35SBob Moore 			    "current SyncLevel is too large (%u)",
22095b482a8SLen Brown 			    acpi_ut_get_node_name(obj_desc->mutex.node),
22195b482a8SLen Brown 			    walk_state->thread->current_sync_level));
22295b482a8SLen Brown 		return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
22395b482a8SLen Brown 	}
22495b482a8SLen Brown 
225cd162b35SBob Moore 	ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
226cd162b35SBob Moore 			  "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
227cd162b35SBob Moore 			  "Depth %u TID %p\n",
228cd162b35SBob Moore 			  obj_desc->mutex.sync_level,
229cd162b35SBob Moore 			  walk_state->thread->current_sync_level,
230cd162b35SBob Moore 			  obj_desc->mutex.acquisition_depth,
231cd162b35SBob Moore 			  walk_state->thread));
232cd162b35SBob Moore 
23395b482a8SLen Brown 	status = acpi_ex_acquire_mutex_object((u16)time_desc->integer.value,
23495b482a8SLen Brown 					      obj_desc,
23595b482a8SLen Brown 					      walk_state->thread->thread_id);
236cd162b35SBob Moore 
23795b482a8SLen Brown 	if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
23895b482a8SLen Brown 
23995b482a8SLen Brown 		/* Save Thread object, original/current sync levels */
24095b482a8SLen Brown 
24195b482a8SLen Brown 		obj_desc->mutex.owner_thread = walk_state->thread;
24295b482a8SLen Brown 		obj_desc->mutex.original_sync_level =
24395b482a8SLen Brown 		    walk_state->thread->current_sync_level;
24495b482a8SLen Brown 		walk_state->thread->current_sync_level =
24595b482a8SLen Brown 		    obj_desc->mutex.sync_level;
24695b482a8SLen Brown 
24795b482a8SLen Brown 		/* Link the mutex to the current thread for force-unlock at method exit */
24895b482a8SLen Brown 
24995b482a8SLen Brown 		acpi_ex_link_mutex(obj_desc, walk_state->thread);
25095b482a8SLen Brown 	}
25195b482a8SLen Brown 
252cd162b35SBob Moore 	ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
253cd162b35SBob Moore 			  "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
254cd162b35SBob Moore 			  obj_desc->mutex.sync_level,
255cd162b35SBob Moore 			  walk_state->thread->current_sync_level,
256cd162b35SBob Moore 			  obj_desc->mutex.acquisition_depth));
257cd162b35SBob Moore 
25895b482a8SLen Brown 	return_ACPI_STATUS(status);
25995b482a8SLen Brown }
26095b482a8SLen Brown 
26195b482a8SLen Brown /*******************************************************************************
26295b482a8SLen Brown  *
26395b482a8SLen Brown  * FUNCTION:    acpi_ex_release_mutex_object
26495b482a8SLen Brown  *
26595b482a8SLen Brown  * PARAMETERS:  obj_desc            - The object descriptor for this op
26695b482a8SLen Brown  *
26795b482a8SLen Brown  * RETURN:      Status
26895b482a8SLen Brown  *
26995b482a8SLen Brown  * DESCRIPTION: Release a previously acquired Mutex, low level interface.
27095b482a8SLen Brown  *              Provides a common path that supports multiple releases (after
27195b482a8SLen Brown  *              previous multiple acquires) by the same thread.
27295b482a8SLen Brown  *
27395b482a8SLen Brown  * MUTEX:       Interpreter must be locked
27495b482a8SLen Brown  *
27595b482a8SLen Brown  * NOTE: This interface is called from three places:
27695b482a8SLen Brown  * 1) From acpi_ex_release_mutex, via an AML Acquire() operator
27795b482a8SLen Brown  * 2) From acpi_ex_release_global_lock when an AML Field access requires the
27895b482a8SLen Brown  *    global lock
27995b482a8SLen Brown  * 3) From the external interface, acpi_release_global_lock
28095b482a8SLen Brown  *
28195b482a8SLen Brown  ******************************************************************************/
28295b482a8SLen Brown 
acpi_ex_release_mutex_object(union acpi_operand_object * obj_desc)28395b482a8SLen Brown acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
28495b482a8SLen Brown {
28595b482a8SLen Brown 	acpi_status status = AE_OK;
28695b482a8SLen Brown 
28795b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ex_release_mutex_object);
28895b482a8SLen Brown 
28995b482a8SLen Brown 	if (obj_desc->mutex.acquisition_depth == 0) {
29068aafc35SBob Moore 		return_ACPI_STATUS(AE_NOT_ACQUIRED);
29195b482a8SLen Brown 	}
29295b482a8SLen Brown 
29395b482a8SLen Brown 	/* Match multiple Acquires with multiple Releases */
29495b482a8SLen Brown 
29595b482a8SLen Brown 	obj_desc->mutex.acquisition_depth--;
29695b482a8SLen Brown 	if (obj_desc->mutex.acquisition_depth != 0) {
29795b482a8SLen Brown 
29895b482a8SLen Brown 		/* Just decrement the depth and return */
29995b482a8SLen Brown 
30095b482a8SLen Brown 		return_ACPI_STATUS(AE_OK);
30195b482a8SLen Brown 	}
30295b482a8SLen Brown 
30395b482a8SLen Brown 	if (obj_desc->mutex.owner_thread) {
30495b482a8SLen Brown 
30595b482a8SLen Brown 		/* Unlink the mutex from the owner's list */
30695b482a8SLen Brown 
30795b482a8SLen Brown 		acpi_ex_unlink_mutex(obj_desc);
30895b482a8SLen Brown 		obj_desc->mutex.owner_thread = NULL;
30995b482a8SLen Brown 	}
31095b482a8SLen Brown 
31195b482a8SLen Brown 	/* Release the mutex, special case for Global Lock */
31295b482a8SLen Brown 
31395b482a8SLen Brown 	if (obj_desc == acpi_gbl_global_lock_mutex) {
31495b482a8SLen Brown 		status = acpi_ev_release_global_lock();
31595b482a8SLen Brown 	} else {
31695b482a8SLen Brown 		acpi_os_release_mutex(obj_desc->mutex.os_mutex);
31795b482a8SLen Brown 	}
31895b482a8SLen Brown 
31995b482a8SLen Brown 	/* Clear mutex info */
32095b482a8SLen Brown 
32128eb3fcfSLin Ming 	obj_desc->mutex.thread_id = 0;
32295b482a8SLen Brown 	return_ACPI_STATUS(status);
32395b482a8SLen Brown }
32495b482a8SLen Brown 
32595b482a8SLen Brown /*******************************************************************************
32695b482a8SLen Brown  *
32795b482a8SLen Brown  * FUNCTION:    acpi_ex_release_mutex
32895b482a8SLen Brown  *
32995b482a8SLen Brown  * PARAMETERS:  obj_desc            - The object descriptor for this op
33095b482a8SLen Brown  *              walk_state          - Current method execution state
33195b482a8SLen Brown  *
33295b482a8SLen Brown  * RETURN:      Status
33395b482a8SLen Brown  *
33495b482a8SLen Brown  * DESCRIPTION: Release a previously acquired Mutex.
33595b482a8SLen Brown  *
33695b482a8SLen Brown  ******************************************************************************/
33795b482a8SLen Brown 
33895b482a8SLen Brown acpi_status
acpi_ex_release_mutex(union acpi_operand_object * obj_desc,struct acpi_walk_state * walk_state)33995b482a8SLen Brown acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
34095b482a8SLen Brown 		      struct acpi_walk_state *walk_state)
34195b482a8SLen Brown {
34210a3b461SBob Moore 	u8 previous_sync_level;
343e0f40281SLin Ming 	struct acpi_thread_state *owner_thread;
344cd162b35SBob Moore 	acpi_status status = AE_OK;
34595b482a8SLen Brown 
34695b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ex_release_mutex);
34795b482a8SLen Brown 
34895b482a8SLen Brown 	if (!obj_desc) {
34995b482a8SLen Brown 		return_ACPI_STATUS(AE_BAD_PARAMETER);
35095b482a8SLen Brown 	}
35195b482a8SLen Brown 
352e0f40281SLin Ming 	owner_thread = obj_desc->mutex.owner_thread;
353e0f40281SLin Ming 
35495b482a8SLen Brown 	/* The mutex must have been previously acquired in order to release it */
35595b482a8SLen Brown 
356e0f40281SLin Ming 	if (!owner_thread) {
35795b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
35895b482a8SLen Brown 			    "Cannot release Mutex [%4.4s], not acquired",
35995b482a8SLen Brown 			    acpi_ut_get_node_name(obj_desc->mutex.node)));
36095b482a8SLen Brown 		return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
36195b482a8SLen Brown 	}
36295b482a8SLen Brown 
36375c8044fSLv Zheng 	/* Must have a valid thread ID */
3643e8214e5SLv Zheng 
365fbc3be2aSDan Carpenter 	if (!walk_state->thread) {
366fbc3be2aSDan Carpenter 		ACPI_ERROR((AE_INFO,
367fbc3be2aSDan Carpenter 			    "Cannot release Mutex [%4.4s], null thread info",
368fbc3be2aSDan Carpenter 			    acpi_ut_get_node_name(obj_desc->mutex.node)));
369fbc3be2aSDan Carpenter 		return_ACPI_STATUS(AE_AML_INTERNAL);
370fbc3be2aSDan Carpenter 	}
371fbc3be2aSDan Carpenter 
37295b482a8SLen Brown 	/*
37395b482a8SLen Brown 	 * The Mutex is owned, but this thread must be the owner.
37495b482a8SLen Brown 	 * Special case for Global Lock, any thread can release
37595b482a8SLen Brown 	 */
376e0f40281SLin Ming 	if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
377e0f40281SLin Ming 	    (obj_desc != acpi_gbl_global_lock_mutex)) {
37895b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
37928eb3fcfSLin Ming 			    "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
38028eb3fcfSLin Ming 			    (u32)walk_state->thread->thread_id,
38195b482a8SLen Brown 			    acpi_ut_get_node_name(obj_desc->mutex.node),
38228eb3fcfSLin Ming 			    (u32)owner_thread->thread_id));
38395b482a8SLen Brown 		return_ACPI_STATUS(AE_AML_NOT_OWNER);
38495b482a8SLen Brown 	}
38595b482a8SLen Brown 
38695b482a8SLen Brown 	/*
387315c7288SBob Moore 	 * The sync level of the mutex must be equal to the current sync level. In
388315c7288SBob Moore 	 * other words, the current level means that at least one mutex at that
389315c7288SBob Moore 	 * level is currently being held. Attempting to release a mutex of a
390315c7288SBob Moore 	 * different level can only mean that the mutex ordering rule is being
391315c7288SBob Moore 	 * violated. This behavior is clarified in ACPI 4.0 specification.
39295b482a8SLen Brown 	 */
393e0f40281SLin Ming 	if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
39495b482a8SLen Brown 		ACPI_ERROR((AE_INFO,
395cd162b35SBob Moore 			    "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
396cd162b35SBob Moore 			    "mutex %u current %u",
39795b482a8SLen Brown 			    acpi_ut_get_node_name(obj_desc->mutex.node),
39895b482a8SLen Brown 			    obj_desc->mutex.sync_level,
39995b482a8SLen Brown 			    walk_state->thread->current_sync_level));
40095b482a8SLen Brown 		return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
40195b482a8SLen Brown 	}
40295b482a8SLen Brown 
40310a3b461SBob Moore 	/*
40410a3b461SBob Moore 	 * Get the previous sync_level from the head of the acquired mutex list.
40510a3b461SBob Moore 	 * This handles the case where several mutexes at the same level have been
40610a3b461SBob Moore 	 * acquired, but are not released in reverse order.
40710a3b461SBob Moore 	 */
40810a3b461SBob Moore 	previous_sync_level =
409e0f40281SLin Ming 	    owner_thread->acquired_mutex_list->mutex.original_sync_level;
41010a3b461SBob Moore 
411cd162b35SBob Moore 	ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
412cd162b35SBob Moore 			  "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
413cd162b35SBob Moore 			  "Prev SyncLevel %u, Depth %u TID %p\n",
414cd162b35SBob Moore 			  obj_desc->mutex.sync_level,
415cd162b35SBob Moore 			  walk_state->thread->current_sync_level,
416cd162b35SBob Moore 			  previous_sync_level,
417cd162b35SBob Moore 			  obj_desc->mutex.acquisition_depth,
418cd162b35SBob Moore 			  walk_state->thread));
419cd162b35SBob Moore 
42095b482a8SLen Brown 	status = acpi_ex_release_mutex_object(obj_desc);
421315c7288SBob Moore 	if (ACPI_FAILURE(status)) {
422315c7288SBob Moore 		return_ACPI_STATUS(status);
423315c7288SBob Moore 	}
42495b482a8SLen Brown 
42595b482a8SLen Brown 	if (obj_desc->mutex.acquisition_depth == 0) {
42695b482a8SLen Brown 
427315c7288SBob Moore 		/* Restore the previous sync_level */
42895b482a8SLen Brown 
429e0f40281SLin Ming 		owner_thread->current_sync_level = previous_sync_level;
43095b482a8SLen Brown 	}
431a7499bc8SBob Moore 
432cd162b35SBob Moore 	ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
433cd162b35SBob Moore 			  "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
434cd162b35SBob Moore 			  "Prev SyncLevel %u, Depth %u\n",
435cd162b35SBob Moore 			  obj_desc->mutex.sync_level,
436cd162b35SBob Moore 			  walk_state->thread->current_sync_level,
437cd162b35SBob Moore 			  previous_sync_level,
438cd162b35SBob Moore 			  obj_desc->mutex.acquisition_depth));
439cd162b35SBob Moore 
44095b482a8SLen Brown 	return_ACPI_STATUS(status);
44195b482a8SLen Brown }
44295b482a8SLen Brown 
44395b482a8SLen Brown /*******************************************************************************
44495b482a8SLen Brown  *
44595b482a8SLen Brown  * FUNCTION:    acpi_ex_release_all_mutexes
44695b482a8SLen Brown  *
447ba494beeSBob Moore  * PARAMETERS:  thread              - Current executing thread object
44895b482a8SLen Brown  *
44995b482a8SLen Brown  * RETURN:      Status
45095b482a8SLen Brown  *
45195b482a8SLen Brown  * DESCRIPTION: Release all mutexes held by this thread
45295b482a8SLen Brown  *
45395b482a8SLen Brown  * NOTE: This function is called as the thread is exiting the interpreter.
45495b482a8SLen Brown  * Mutexes are not released when an individual control method is exited, but
45595b482a8SLen Brown  * only when the parent thread actually exits the interpreter. This allows one
45695b482a8SLen Brown  * method to acquire a mutex, and a different method to release it, as long as
45795b482a8SLen Brown  * this is performed underneath a single parent control method.
45895b482a8SLen Brown  *
45995b482a8SLen Brown  ******************************************************************************/
46095b482a8SLen Brown 
acpi_ex_release_all_mutexes(struct acpi_thread_state * thread)46195b482a8SLen Brown void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
46295b482a8SLen Brown {
46395b482a8SLen Brown 	union acpi_operand_object *next = thread->acquired_mutex_list;
46495b482a8SLen Brown 	union acpi_operand_object *obj_desc;
46595b482a8SLen Brown 
466cd162b35SBob Moore 	ACPI_FUNCTION_TRACE(ex_release_all_mutexes);
46795b482a8SLen Brown 
46895b482a8SLen Brown 	/* Traverse the list of owned mutexes, releasing each one */
46995b482a8SLen Brown 
47095b482a8SLen Brown 	while (next) {
47195b482a8SLen Brown 		obj_desc = next;
4722489ef01SBob Moore 		ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
473cd162b35SBob Moore 				  "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
474cd162b35SBob Moore 				  obj_desc->mutex.node->name.ascii,
475cd162b35SBob Moore 				  obj_desc->mutex.sync_level,
476cd162b35SBob Moore 				  obj_desc->mutex.acquisition_depth));
4772489ef01SBob Moore 
47895b482a8SLen Brown 		/* Release the mutex, special case for Global Lock */
47995b482a8SLen Brown 
48095b482a8SLen Brown 		if (obj_desc == acpi_gbl_global_lock_mutex) {
48195b482a8SLen Brown 
48295b482a8SLen Brown 			/* Ignore errors */
48395b482a8SLen Brown 
48495b482a8SLen Brown 			(void)acpi_ev_release_global_lock();
48595b482a8SLen Brown 		} else {
48695b482a8SLen Brown 			acpi_os_release_mutex(obj_desc->mutex.os_mutex);
48795b482a8SLen Brown 		}
48895b482a8SLen Brown 
48995b482a8SLen Brown 		/* Update Thread sync_level (Last mutex is the important one) */
49095b482a8SLen Brown 
49195b482a8SLen Brown 		thread->current_sync_level =
49295b482a8SLen Brown 		    obj_desc->mutex.original_sync_level;
493cd162b35SBob Moore 
494cd162b35SBob Moore 		/* Mark mutex unowned */
495cd162b35SBob Moore 
496cd162b35SBob Moore 		next = obj_desc->mutex.next;
497cd162b35SBob Moore 
498cd162b35SBob Moore 		obj_desc->mutex.prev = NULL;
499cd162b35SBob Moore 		obj_desc->mutex.next = NULL;
500cd162b35SBob Moore 		obj_desc->mutex.acquisition_depth = 0;
501cd162b35SBob Moore 		obj_desc->mutex.owner_thread = NULL;
502cd162b35SBob Moore 		obj_desc->mutex.thread_id = 0;
50395b482a8SLen Brown 	}
504cd162b35SBob Moore 
505cd162b35SBob Moore 	return_VOID;
50695b482a8SLen Brown }
507