exmutex.c 11.2 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 - 2007, 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
 * 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>
47
#include <acpi/acevents.h>
L
Linus Torvalds 已提交
48 49

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

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

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

69
void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
L
Linus Torvalds 已提交
70
{
71 72
	struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;

L
Linus Torvalds 已提交
73 74 75 76 77 78 79 80 81 82 83 84
	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 已提交
85
	} else {
L
Linus Torvalds 已提交
86 87 88 89 90 91 92 93
		thread->acquired_mutex_list = obj_desc->mutex.next;
	}
}

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

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

	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 已提交
131 132 133
 * PARAMETERS:  time_desc           - Timeout integer
 *              obj_desc            - Mutex object
 *              walk_state          - Current method execution state
L
Linus Torvalds 已提交
134 135 136 137 138 139 140 141
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Acquire an AML mutex
 *
 ******************************************************************************/

acpi_status
L
Len Brown 已提交
142 143 144
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 已提交
145
{
L
Len Brown 已提交
146
	acpi_status status;
L
Linus Torvalds 已提交
147

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

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

154
	/* Sanity check: we must have a valid thread ID */
L
Linus Torvalds 已提交
155 156

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

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

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

177 178 179 180 181 182 183 184 185 186
	if (obj_desc->mutex.owner_thread) {
		if (obj_desc->mutex.owner_thread->thread_id ==
		    walk_state->thread->thread_id) {
			/*
			 * The mutex is already owned by this thread, just increment the
			 * acquisition depth
			 */
			obj_desc->mutex.acquisition_depth++;
			return_ACPI_STATUS(AE_OK);
		}
L
Linus Torvalds 已提交
187 188
	}

189 190 191 192 193 194 195 196 197 198
	/* Acquire the mutex, wait if necessary. Special case for Global Lock */

	if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
		status =
		    acpi_ev_acquire_global_lock((u16) time_desc->integer.value);
	} else {
		status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
						   (u16) time_desc->integer.
						   value);
	}
L
Linus Torvalds 已提交
199

L
Len Brown 已提交
200
	if (ACPI_FAILURE(status)) {
B
Bob Moore 已提交
201

L
Linus Torvalds 已提交
202 203
		/* Includes failure from a timeout on time_desc */

L
Len Brown 已提交
204
		return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
205 206 207 208
	}

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

209
	obj_desc->mutex.owner_thread = walk_state->thread;
L
Linus Torvalds 已提交
210
	obj_desc->mutex.acquisition_depth = 1;
L
Len Brown 已提交
211 212
	obj_desc->mutex.original_sync_level =
	    walk_state->thread->current_sync_level;
L
Linus Torvalds 已提交
213 214 215 216 217

	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 已提交
218 219
	acpi_ex_link_mutex(obj_desc, walk_state->thread);
	return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
220 221 222 223 224 225 226
}

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

acpi_status
L
Len Brown 已提交
236 237
acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
		      struct acpi_walk_state *walk_state)
L
Linus Torvalds 已提交
238
{
239
	acpi_status status = AE_OK;
L
Linus Torvalds 已提交
240

B
Bob Moore 已提交
241
	ACPI_FUNCTION_TRACE(ex_release_mutex);
L
Linus Torvalds 已提交
242 243

	if (!obj_desc) {
L
Len Brown 已提交
244
		return_ACPI_STATUS(AE_BAD_PARAMETER);
L
Linus Torvalds 已提交
245 246 247 248
	}

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

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

256
	/* Sanity check: we must have a valid thread ID */
L
Linus Torvalds 已提交
257 258

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

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

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

	/* Match multiple Acquires with multiple Releases */

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

L
Linus Torvalds 已提交
297 298
		/* Just decrement the depth and return */

L
Len Brown 已提交
299
		return_ACPI_STATUS(AE_OK);
L
Linus Torvalds 已提交
300 301 302 303
	}

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

304
	acpi_ex_unlink_mutex(obj_desc);
L
Linus Torvalds 已提交
305

306
	/* Release the mutex, special case for Global Lock */
L
Linus Torvalds 已提交
307

308 309 310 311 312
	if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
		status = acpi_ev_release_global_lock();
	} else {
		acpi_os_release_mutex(obj_desc->mutex.os_mutex);
	}
L
Linus Torvalds 已提交
313

314
	/* Update the mutex and restore sync_level */
L
Linus Torvalds 已提交
315

316
	obj_desc->mutex.owner_thread = NULL;
L
Len Brown 已提交
317 318
	walk_state->thread->current_sync_level =
	    obj_desc->mutex.original_sync_level;
L
Linus Torvalds 已提交
319

L
Len Brown 已提交
320
	return_ACPI_STATUS(status);
L
Linus Torvalds 已提交
321 322 323 324 325 326
}

/*******************************************************************************
 *
 * FUNCTION:    acpi_ex_release_all_mutexes
 *
R
Robert Moore 已提交
327
 * PARAMETERS:  Thread          - Current executing thread object
L
Linus Torvalds 已提交
328 329 330
 *
 * RETURN:      Status
 *
R
Robert Moore 已提交
331
 * DESCRIPTION: Release all mutexes held by this thread
L
Linus Torvalds 已提交
332
 *
333 334 335 336 337 338
 * NOTE: This function is called as the thread is exiting the interpreter.
 * Mutexes are not released when an individual control method is exited, but
 * only when the parent thread actually exits the interpreter. This allows one
 * method to acquire a mutex, and a different method to release it, as long as
 * this is performed underneath a single parent control method.
 *
L
Linus Torvalds 已提交
339 340
 ******************************************************************************/

L
Len Brown 已提交
341
void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
L
Linus Torvalds 已提交
342
{
L
Len Brown 已提交
343
	union acpi_operand_object *next = thread->acquired_mutex_list;
344
	union acpi_operand_object *obj_desc;
L
Linus Torvalds 已提交
345

L
Len Brown 已提交
346
	ACPI_FUNCTION_ENTRY();
L
Linus Torvalds 已提交
347 348 349 350

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

	while (next) {
351 352 353 354 355
		obj_desc = next;
		next = obj_desc->mutex.next;

		obj_desc->mutex.prev = NULL;
		obj_desc->mutex.next = NULL;
356
		obj_desc->mutex.acquisition_depth = 0;
357 358

		/* Release the mutex, special case for Global Lock */
L
Linus Torvalds 已提交
359

360
		if (obj_desc->mutex.os_mutex == acpi_gbl_global_lock_mutex) {
L
Linus Torvalds 已提交
361

362
			/* Ignore errors */
L
Linus Torvalds 已提交
363

364 365 366
			(void)acpi_ev_release_global_lock();
		} else {
			acpi_os_release_mutex(obj_desc->mutex.os_mutex);
L
Linus Torvalds 已提交
367 368 369 370
		}

		/* Mark mutex unowned */

371
		obj_desc->mutex.owner_thread = NULL;
L
Linus Torvalds 已提交
372 373 374

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

375 376
		thread->current_sync_level =
		    obj_desc->mutex.original_sync_level;
L
Linus Torvalds 已提交
377 378
	}
}