exmutex.c 10.4 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8

/******************************************************************************
 *
 * Module Name: exmutex - ASL Mutex Acquire/Release functions
 *
 *****************************************************************************/

/*
B
Bob Moore 已提交
9
 * Copyright (C) 2000 - 2006, R. Byron Moore
L
Linus Torvalds 已提交
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions, and the following disclaimer,
 *    without modification.
 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
 *    substantially similar to the "NO WARRANTY" disclaimer below
 *    ("Disclaimer") and any redistribution must be conditioned upon
 *    including a substantially similar Disclaimer requirement for further
 *    binary redistribution.
 * 3. Neither the names of the above-listed copyright holders nor the names
 *    of any contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * NO WARRANTY
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGES.
 */

#include <acpi/acpi.h>
#include <acpi/acinterp.h>

#define _COMPONENT          ACPI_EXECUTER
L
Len Brown 已提交
49
ACPI_MODULE_NAME("exmutex")
L
Linus Torvalds 已提交
50

R
Robert Moore 已提交
51 52
/* Local prototypes */
static void
L
Len Brown 已提交
53 54
acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
		   struct acpi_thread_state *thread);
L
Linus Torvalds 已提交
55 56 57 58 59 60 61

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_unlink_mutex
 *
 * PARAMETERS:  obj_desc            - The mutex to be unlinked
 *
R
Robert Moore 已提交
62
 * RETURN:      None
L
Linus Torvalds 已提交
63
 *
B
Bob Moore 已提交
64
 * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list
L
Linus Torvalds 已提交
65 66 67
 *
 ******************************************************************************/

L
Len Brown 已提交
68
void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
L
Linus Torvalds 已提交
69
{
L
Len Brown 已提交
70
	struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
L
Linus Torvalds 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83

	if (!thread) {
		return;
	}

	/* Doubly linked list */

	if (obj_desc->mutex.next) {
		(obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
	}

	if (obj_desc->mutex.prev) {
		(obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
L
Len Brown 已提交
84
	} else {
L
Linus Torvalds 已提交
85 86 87 88 89 90 91 92
		thread->acquired_mutex_list = obj_desc->mutex.next;
	}
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_link_mutex
 *
R
Robert Moore 已提交
93 94
 * PARAMETERS:  obj_desc        - The mutex to be linked
 *              Thread          - Current executing thread object
L
Linus Torvalds 已提交
95
 *
R
Robert Moore 已提交
96
 * RETURN:      None
L
Linus Torvalds 已提交
97
 *
B
Bob Moore 已提交
98
 * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk
L
Linus Torvalds 已提交
99 100 101
 *
 ******************************************************************************/

R
Robert Moore 已提交
102
static void
L
Len Brown 已提交
103 104
acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
		   struct acpi_thread_state *thread)
L
Linus Torvalds 已提交
105
{
L
Len Brown 已提交
106
	union acpi_operand_object *list_head;
L
Linus Torvalds 已提交
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129

	list_head = thread->acquired_mutex_list;

	/* This object will be the first object in the list */

	obj_desc->mutex.prev = NULL;
	obj_desc->mutex.next = list_head;

	/* Update old first object to point back to this object */

	if (list_head) {
		list_head->mutex.prev = obj_desc;
	}

	/* Update list head */

	thread->acquired_mutex_list = obj_desc;
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_acquire_mutex
 *
R
Robert Moore 已提交
130 131 132
 * PARAMETERS:  time_desc           - Timeout integer
 *              obj_desc            - Mutex object
 *              walk_state          - Current method execution state
L
Linus Torvalds 已提交
133 134 135 136 137 138 139 140
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Acquire an AML mutex
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
141 142 143
acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
		      union acpi_operand_object *obj_desc,
		      struct acpi_walk_state *walk_state)
L
Linus Torvalds 已提交
144
{
L
Len Brown 已提交
145
	acpi_status status;
L
Linus Torvalds 已提交
146

B
Bob Moore 已提交
147
	ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
L
Linus Torvalds 已提交
148 149

	if (!obj_desc) {
L
Len Brown 已提交
150
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
151 152 153 154 155
	}

	/* Sanity check -- we must have a valid thread ID */

	if (!walk_state->thread) {
B
Bob Moore 已提交
156 157 158
		ACPI_ERROR((AE_INFO,
			    "Cannot acquire Mutex [%4.4s], null thread info",
			    acpi_ut_get_node_name(obj_desc->mutex.node)));
L
Len Brown 已提交
159
		return_ACPI_STATUS(AE_AML_INTERNAL);
L
Linus Torvalds 已提交
160 161 162 163 164 165 166
	}

	/*
	 * Current Sync must be less than or equal to the sync level of the
	 * mutex.  This mechanism provides some deadlock prevention
	 */
	if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
B
Bob Moore 已提交
167
		ACPI_ERROR((AE_INFO,
B
Bob Moore 已提交
168
			    "Cannot acquire Mutex [%4.4s], incorrect SyncLevel",
B
Bob Moore 已提交
169
			    acpi_ut_get_node_name(obj_desc->mutex.node)));
L
Len Brown 已提交
170
		return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
L
Linus Torvalds 已提交
171 172 173 174 175
	}

	/* Support for multiple acquires by the owning thread */

	if (obj_desc->mutex.owner_thread) {
B
Bob Moore 已提交
176

L
Linus Torvalds 已提交
177 178
		/* Special case for Global Lock, allow all threads */

R
Robert Moore 已提交
179
		if ((obj_desc->mutex.owner_thread->thread_id ==
L
Len Brown 已提交
180 181 182
		     walk_state->thread->thread_id) ||
		    (obj_desc->mutex.semaphore ==
		     acpi_gbl_global_lock_semaphore)) {
L
Linus Torvalds 已提交
183 184 185 186 187
			/*
			 * The mutex is already owned by this thread,
			 * just increment the acquisition depth
			 */
			obj_desc->mutex.acquisition_depth++;
L
Len Brown 已提交
188
			return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
189 190 191 192 193
		}
	}

	/* Acquire the mutex, wait if necessary */

L
Len Brown 已提交
194 195
	status = acpi_ex_system_acquire_mutex(time_desc, obj_desc);
	if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
196

L
Linus Torvalds 已提交
197 198
		/* Includes failure from a timeout on time_desc */

L
Len Brown 已提交
199
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
200 201 202 203
	}

	/* Have the mutex: update mutex and walk info and save the sync_level */

L
Len Brown 已提交
204
	obj_desc->mutex.owner_thread = walk_state->thread;
L
Linus Torvalds 已提交
205
	obj_desc->mutex.acquisition_depth = 1;
L
Len Brown 已提交
206 207
	obj_desc->mutex.original_sync_level =
	    walk_state->thread->current_sync_level;
L
Linus Torvalds 已提交
208 209 210 211 212

	walk_state->thread->current_sync_level = obj_desc->mutex.sync_level;

	/* Link the mutex to the current thread for force-unlock at method exit */

L
Len Brown 已提交
213
	acpi_ex_link_mutex(obj_desc, walk_state->thread);
L
Linus Torvalds 已提交
214

L
Len Brown 已提交
215
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
216 217 218 219 220 221 222
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_release_mutex
 *
 * PARAMETERS:  obj_desc            - The object descriptor for this op
R
Robert Moore 已提交
223
 *              walk_state          - Current method execution state
L
Linus Torvalds 已提交
224 225 226 227 228 229 230 231
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Release a previously acquired Mutex.
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
232 233
acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
		      struct acpi_walk_state *walk_state)
L
Linus Torvalds 已提交
234
{
L
Len Brown 已提交
235
	acpi_status status;
L
Linus Torvalds 已提交
236

B
Bob Moore 已提交
237
	ACPI_FUNCTION_TRACE(ex_release_mutex);
L
Linus Torvalds 已提交
238 239

	if (!obj_desc) {
L
Len Brown 已提交
240
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
241 242 243 244 245
	}

	/* The mutex must have been previously acquired in order to release it */

	if (!obj_desc->mutex.owner_thread) {
B
Bob Moore 已提交
246 247 248
		ACPI_ERROR((AE_INFO,
			    "Cannot release Mutex [%4.4s], not acquired",
			    acpi_ut_get_node_name(obj_desc->mutex.node)));
L
Len Brown 已提交
249
		return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
L
Linus Torvalds 已提交
250 251 252 253 254
	}

	/* Sanity check -- we must have a valid thread ID */

	if (!walk_state->thread) {
B
Bob Moore 已提交
255 256 257
		ACPI_ERROR((AE_INFO,
			    "Cannot release Mutex [%4.4s], null thread info",
			    acpi_ut_get_node_name(obj_desc->mutex.node)));
L
Len Brown 已提交
258
		return_ACPI_STATUS(AE_AML_INTERNAL);
L
Linus Torvalds 已提交
259 260 261 262 263 264
	}

	/*
	 * The Mutex is owned, but this thread must be the owner.
	 * Special case for Global Lock, any thread can release
	 */
L
Len Brown 已提交
265 266 267
	if ((obj_desc->mutex.owner_thread->thread_id !=
	     walk_state->thread->thread_id)
	    && (obj_desc->mutex.semaphore != acpi_gbl_global_lock_semaphore)) {
B
Bob Moore 已提交
268 269 270 271 272
		ACPI_ERROR((AE_INFO,
			    "Thread %X cannot release Mutex [%4.4s] acquired by thread %X",
			    walk_state->thread->thread_id,
			    acpi_ut_get_node_name(obj_desc->mutex.node),
			    obj_desc->mutex.owner_thread->thread_id));
L
Len Brown 已提交
273
		return_ACPI_STATUS(AE_AML_NOT_OWNER);
L
Linus Torvalds 已提交
274 275 276 277 278 279 280
	}

	/*
	 * The sync level of the mutex must be less than or
	 * equal to the current sync level
	 */
	if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
B
Bob Moore 已提交
281
		ACPI_ERROR((AE_INFO,
B
Bob Moore 已提交
282
			    "Cannot release Mutex [%4.4s], incorrect SyncLevel",
B
Bob Moore 已提交
283
			    acpi_ut_get_node_name(obj_desc->mutex.node)));
L
Len Brown 已提交
284
		return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
L
Linus Torvalds 已提交
285 286 287 288 289 290
	}

	/* Match multiple Acquires with multiple Releases */

	obj_desc->mutex.acquisition_depth--;
	if (obj_desc->mutex.acquisition_depth != 0) {
B
Bob Moore 已提交
291

L
Linus Torvalds 已提交
292 293
		/* Just decrement the depth and return */

L
Len Brown 已提交
294
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
295 296 297 298
	}

	/* Unlink the mutex from the owner's list */

L
Len Brown 已提交
299
	acpi_ex_unlink_mutex(obj_desc);
L
Linus Torvalds 已提交
300 301 302

	/* Release the mutex */

L
Len Brown 已提交
303
	status = acpi_ex_system_release_mutex(obj_desc);
L
Linus Torvalds 已提交
304 305 306 307

	/* Update the mutex and walk state, restore sync_level before acquire */

	obj_desc->mutex.owner_thread = NULL;
L
Len Brown 已提交
308 309
	walk_state->thread->current_sync_level =
	    obj_desc->mutex.original_sync_level;
L
Linus Torvalds 已提交
310

L
Len Brown 已提交
311
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
312 313 314 315 316 317
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_release_all_mutexes
 *
R
Robert Moore 已提交
318
 * PARAMETERS:  Thread          - Current executing thread object
L
Linus Torvalds 已提交
319 320 321
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
322
 * DESCRIPTION: Release all mutexes held by this thread
L
Linus Torvalds 已提交
323 324 325
 *
 ******************************************************************************/

L
Len Brown 已提交
326
void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
L
Linus Torvalds 已提交
327
{
L
Len Brown 已提交
328 329 330
	union acpi_operand_object *next = thread->acquired_mutex_list;
	union acpi_operand_object *this;
	acpi_status status;
L
Linus Torvalds 已提交
331

L
Len Brown 已提交
332
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
333 334 335 336 337 338 339 340

	/* Traverse the list of owned mutexes, releasing each one */

	while (next) {
		this = next;
		next = this->mutex.next;

		this->mutex.acquisition_depth = 1;
L
Len Brown 已提交
341 342
		this->mutex.prev = NULL;
		this->mutex.next = NULL;
L
Linus Torvalds 已提交
343

L
Len Brown 已提交
344
		/* Release the mutex */
L
Linus Torvalds 已提交
345

L
Len Brown 已提交
346 347
		status = acpi_ex_system_release_mutex(this);
		if (ACPI_FAILURE(status)) {
L
Linus Torvalds 已提交
348 349 350 351 352 353 354 355 356 357 358 359
			continue;
		}

		/* Mark mutex unowned */

		this->mutex.owner_thread = NULL;

		/* Update Thread sync_level (Last mutex is the important one) */

		thread->current_sync_level = this->mutex.original_sync_level;
	}
}