Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | |
| 2 | /****************************************************************************** |
| 3 | * |
| 4 | * Module Name: exmutex - ASL Mutex Acquire/Release functions |
| 5 | * |
| 6 | *****************************************************************************/ |
| 7 | |
| 8 | /* |
Bob Moore | 6c9deb7 | 2007-02-02 19:48:24 +0300 | [diff] [blame] | 9 | * Copyright (C) 2000 - 2007, R. Byron Moore |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | * All rights reserved. |
| 11 | * |
| 12 | * Redistribution and use in source and binary forms, with or without |
| 13 | * modification, are permitted provided that the following conditions |
| 14 | * are met: |
| 15 | * 1. Redistributions of source code must retain the above copyright |
| 16 | * notice, this list of conditions, and the following disclaimer, |
| 17 | * without modification. |
| 18 | * 2. Redistributions in binary form must reproduce at minimum a disclaimer |
| 19 | * substantially similar to the "NO WARRANTY" disclaimer below |
| 20 | * ("Disclaimer") and any redistribution must be conditioned upon |
| 21 | * including a substantially similar Disclaimer requirement for further |
| 22 | * binary redistribution. |
| 23 | * 3. Neither the names of the above-listed copyright holders nor the names |
| 24 | * of any contributors may be used to endorse or promote products derived |
| 25 | * from this software without specific prior written permission. |
| 26 | * |
| 27 | * Alternatively, this software may be distributed under the terms of the |
| 28 | * GNU General Public License ("GPL") version 2 as published by the Free |
| 29 | * Software Foundation. |
| 30 | * |
| 31 | * NO WARRANTY |
| 32 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 33 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 34 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR |
| 35 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 36 | * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 37 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS |
| 38 | * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 39 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 40 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING |
| 41 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 42 | * POSSIBILITY OF SUCH DAMAGES. |
| 43 | */ |
| 44 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 45 | #include <acpi/acpi.h> |
| 46 | #include <acpi/acinterp.h> |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 47 | #include <acpi/acevents.h> |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 48 | |
| 49 | #define _COMPONENT ACPI_EXECUTER |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 50 | ACPI_MODULE_NAME("exmutex") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 52 | /* Local prototypes */ |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 53 | static void |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 54 | acpi_ex_link_mutex(union acpi_operand_object *obj_desc, |
| 55 | struct acpi_thread_state *thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 56 | |
| 57 | /******************************************************************************* |
| 58 | * |
| 59 | * FUNCTION: acpi_ex_unlink_mutex |
| 60 | * |
| 61 | * PARAMETERS: obj_desc - The mutex to be unlinked |
| 62 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 63 | * RETURN: None |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 64 | * |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 65 | * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 66 | * |
| 67 | ******************************************************************************/ |
| 68 | |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 69 | void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | { |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 71 | struct acpi_thread_state *thread = obj_desc->mutex.owner_thread; |
| 72 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | 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; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 85 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 86 | thread->acquired_mutex_list = obj_desc->mutex.next; |
| 87 | } |
Lin Ming | 941f48b | 2008-04-10 19:06:41 +0400 | [diff] [blame^] | 88 | return; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 91 | /******************************************************************************* |
| 92 | * |
| 93 | * FUNCTION: acpi_ex_link_mutex |
| 94 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 95 | * PARAMETERS: obj_desc - The mutex to be linked |
| 96 | * Thread - Current executing thread object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 97 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 98 | * RETURN: None |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 99 | * |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 100 | * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | * |
| 102 | ******************************************************************************/ |
| 103 | |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 104 | static void |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 105 | acpi_ex_link_mutex(union acpi_operand_object *obj_desc, |
| 106 | struct acpi_thread_state *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 107 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 108 | union acpi_operand_object *list_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 109 | |
| 110 | list_head = thread->acquired_mutex_list; |
| 111 | |
| 112 | /* This object will be the first object in the list */ |
| 113 | |
| 114 | obj_desc->mutex.prev = NULL; |
| 115 | obj_desc->mutex.next = list_head; |
| 116 | |
| 117 | /* Update old first object to point back to this object */ |
| 118 | |
| 119 | if (list_head) { |
| 120 | list_head->mutex.prev = obj_desc; |
| 121 | } |
| 122 | |
| 123 | /* Update list head */ |
| 124 | |
| 125 | thread->acquired_mutex_list = obj_desc; |
| 126 | } |
| 127 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 128 | /******************************************************************************* |
| 129 | * |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 130 | * FUNCTION: acpi_ex_acquire_mutex_object |
| 131 | * |
| 132 | * PARAMETERS: time_desc - Timeout in milliseconds |
| 133 | * obj_desc - Mutex object |
| 134 | * Thread - Current thread state |
| 135 | * |
| 136 | * RETURN: Status |
| 137 | * |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 138 | * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common |
| 139 | * path that supports multiple acquires by the same thread. |
| 140 | * |
| 141 | * MUTEX: Interpreter must be locked |
| 142 | * |
| 143 | * NOTE: This interface is called from three places: |
| 144 | * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator |
| 145 | * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the |
| 146 | * global lock |
| 147 | * 3) From the external interface, acpi_acquire_global_lock |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 148 | * |
| 149 | ******************************************************************************/ |
| 150 | |
| 151 | acpi_status |
| 152 | acpi_ex_acquire_mutex_object(u16 timeout, |
| 153 | union acpi_operand_object *obj_desc, |
| 154 | acpi_thread_id thread_id) |
| 155 | { |
| 156 | acpi_status status; |
| 157 | |
| 158 | ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc); |
| 159 | |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 160 | if (!obj_desc) { |
| 161 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
| 162 | } |
| 163 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 164 | /* Support for multiple acquires by the owning thread */ |
| 165 | |
| 166 | if (obj_desc->mutex.thread_id == thread_id) { |
| 167 | /* |
| 168 | * The mutex is already owned by this thread, just increment the |
| 169 | * acquisition depth |
| 170 | */ |
| 171 | obj_desc->mutex.acquisition_depth++; |
| 172 | return_ACPI_STATUS(AE_OK); |
| 173 | } |
| 174 | |
| 175 | /* Acquire the mutex, wait if necessary. Special case for Global Lock */ |
| 176 | |
| 177 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
| 178 | status = acpi_ev_acquire_global_lock(timeout); |
| 179 | } else { |
| 180 | status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex, |
| 181 | timeout); |
| 182 | } |
| 183 | |
| 184 | if (ACPI_FAILURE(status)) { |
| 185 | |
| 186 | /* Includes failure from a timeout on time_desc */ |
| 187 | |
| 188 | return_ACPI_STATUS(status); |
| 189 | } |
| 190 | |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 191 | /* Acquired the mutex: update mutex object */ |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 192 | |
| 193 | obj_desc->mutex.thread_id = thread_id; |
| 194 | obj_desc->mutex.acquisition_depth = 1; |
| 195 | obj_desc->mutex.original_sync_level = 0; |
| 196 | obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */ |
| 197 | |
| 198 | return_ACPI_STATUS(AE_OK); |
| 199 | } |
| 200 | |
| 201 | /******************************************************************************* |
| 202 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 203 | * FUNCTION: acpi_ex_acquire_mutex |
| 204 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 205 | * PARAMETERS: time_desc - Timeout integer |
| 206 | * obj_desc - Mutex object |
| 207 | * walk_state - Current method execution state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | * |
| 209 | * RETURN: Status |
| 210 | * |
| 211 | * DESCRIPTION: Acquire an AML mutex |
| 212 | * |
| 213 | ******************************************************************************/ |
| 214 | |
| 215 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 216 | acpi_ex_acquire_mutex(union acpi_operand_object *time_desc, |
| 217 | union acpi_operand_object *obj_desc, |
| 218 | struct acpi_walk_state *walk_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 219 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 220 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 221 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 222 | ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | |
| 224 | if (!obj_desc) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 225 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 228 | /* Must have a valid thread ID */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 229 | |
| 230 | if (!walk_state->thread) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 231 | ACPI_ERROR((AE_INFO, |
| 232 | "Cannot acquire Mutex [%4.4s], null thread info", |
| 233 | acpi_ut_get_node_name(obj_desc->mutex.node))); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 234 | return_ACPI_STATUS(AE_AML_INTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | /* |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 238 | * Current sync level must be less than or equal to the sync level of the |
Bob Moore | 967440e | 2006-06-23 17:04:00 -0400 | [diff] [blame] | 239 | * mutex. This mechanism provides some deadlock prevention |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 240 | */ |
| 241 | if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 242 | ACPI_ERROR((AE_INFO, |
Bob Moore | 967440e | 2006-06-23 17:04:00 -0400 | [diff] [blame] | 243 | "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)", |
| 244 | acpi_ut_get_node_name(obj_desc->mutex.node), |
| 245 | walk_state->thread->current_sync_level)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 246 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 247 | } |
| 248 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 249 | status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value, |
| 250 | obj_desc, |
| 251 | walk_state->thread->thread_id); |
| 252 | if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) { |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 253 | |
| 254 | /* Save Thread object, original/current sync levels */ |
| 255 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 256 | obj_desc->mutex.owner_thread = walk_state->thread; |
| 257 | obj_desc->mutex.original_sync_level = |
| 258 | walk_state->thread->current_sync_level; |
| 259 | walk_state->thread->current_sync_level = |
| 260 | obj_desc->mutex.sync_level; |
| 261 | |
| 262 | /* Link the mutex to the current thread for force-unlock at method exit */ |
| 263 | |
| 264 | acpi_ex_link_mutex(obj_desc, walk_state->thread); |
| 265 | } |
| 266 | |
| 267 | return_ACPI_STATUS(status); |
| 268 | } |
| 269 | |
| 270 | /******************************************************************************* |
| 271 | * |
| 272 | * FUNCTION: acpi_ex_release_mutex_object |
| 273 | * |
| 274 | * PARAMETERS: obj_desc - The object descriptor for this op |
| 275 | * |
| 276 | * RETURN: Status |
| 277 | * |
| 278 | * DESCRIPTION: Release a previously acquired Mutex, low level interface. |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 279 | * Provides a common path that supports multiple releases (after |
| 280 | * previous multiple acquires) by the same thread. |
| 281 | * |
| 282 | * MUTEX: Interpreter must be locked |
| 283 | * |
| 284 | * NOTE: This interface is called from three places: |
| 285 | * 1) From acpi_ex_release_mutex, via an AML Acquire() operator |
| 286 | * 2) From acpi_ex_release_global_lock when an AML Field access requires the |
| 287 | * global lock |
| 288 | * 3) From the external interface, acpi_release_global_lock |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 289 | * |
| 290 | ******************************************************************************/ |
| 291 | |
| 292 | acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc) |
| 293 | { |
| 294 | acpi_status status = AE_OK; |
| 295 | |
| 296 | ACPI_FUNCTION_TRACE(ex_release_mutex_object); |
| 297 | |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 298 | if (obj_desc->mutex.acquisition_depth == 0) { |
| 299 | return (AE_NOT_ACQUIRED); |
| 300 | } |
| 301 | |
Lin Ming | 941f48b | 2008-04-10 19:06:41 +0400 | [diff] [blame^] | 302 | /* No obj_desc->Mutex.owner_thread for Global Lock */ |
| 303 | |
| 304 | /* |
| 305 | * Mutex to be released must be at the head of acquired list to prevent |
| 306 | * deadlock. (The head of the list is the last mutex acquired.) |
| 307 | */ |
| 308 | if (obj_desc->mutex.owner_thread && |
| 309 | (obj_desc != obj_desc->mutex.owner_thread->acquired_mutex_list)) { |
| 310 | return (AE_AML_MUTEX_ORDER); |
| 311 | } |
| 312 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 313 | /* Match multiple Acquires with multiple Releases */ |
| 314 | |
| 315 | obj_desc->mutex.acquisition_depth--; |
| 316 | if (obj_desc->mutex.acquisition_depth != 0) { |
| 317 | |
| 318 | /* Just decrement the depth and return */ |
| 319 | |
| 320 | return_ACPI_STATUS(AE_OK); |
| 321 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 322 | |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 323 | if (obj_desc->mutex.owner_thread) { |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 324 | |
| 325 | /* Unlink the mutex from the owner's list */ |
| 326 | |
| 327 | acpi_ex_unlink_mutex(obj_desc); |
| 328 | obj_desc->mutex.owner_thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 329 | } |
| 330 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 331 | /* Release the mutex, special case for Global Lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 332 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 333 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
| 334 | status = acpi_ev_release_global_lock(); |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 335 | } else { |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 336 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 337 | } |
| 338 | |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 339 | /* Clear mutex info */ |
| 340 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 341 | obj_desc->mutex.thread_id = 0; |
| 342 | return_ACPI_STATUS(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 343 | } |
| 344 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | /******************************************************************************* |
| 346 | * |
| 347 | * FUNCTION: acpi_ex_release_mutex |
| 348 | * |
| 349 | * PARAMETERS: obj_desc - The object descriptor for this op |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 350 | * walk_state - Current method execution state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 351 | * |
| 352 | * RETURN: Status |
| 353 | * |
| 354 | * DESCRIPTION: Release a previously acquired Mutex. |
| 355 | * |
| 356 | ******************************************************************************/ |
| 357 | |
| 358 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 359 | acpi_ex_release_mutex(union acpi_operand_object *obj_desc, |
| 360 | struct acpi_walk_state *walk_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | { |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 362 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 363 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 364 | ACPI_FUNCTION_TRACE(ex_release_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 365 | |
| 366 | if (!obj_desc) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 367 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /* The mutex must have been previously acquired in order to release it */ |
| 371 | |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 372 | if (!obj_desc->mutex.owner_thread) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 373 | ACPI_ERROR((AE_INFO, |
| 374 | "Cannot release Mutex [%4.4s], not acquired", |
| 375 | acpi_ut_get_node_name(obj_desc->mutex.node))); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 376 | return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 377 | } |
| 378 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 379 | /* |
| 380 | * The Mutex is owned, but this thread must be the owner. |
| 381 | * Special case for Global Lock, any thread can release |
| 382 | */ |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 383 | if ((obj_desc->mutex.owner_thread->thread_id != |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 384 | walk_state->thread->thread_id) |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 385 | && (obj_desc != acpi_gbl_global_lock_mutex)) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 386 | ACPI_ERROR((AE_INFO, |
Martin Bligh | 965a3d4 | 2006-10-20 14:30:26 -0700 | [diff] [blame] | 387 | "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX", |
| 388 | (unsigned long)walk_state->thread->thread_id, |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 389 | acpi_ut_get_node_name(obj_desc->mutex.node), |
Len Brown | fd35094 | 2007-05-09 23:34:35 -0400 | [diff] [blame] | 390 | (unsigned long)obj_desc->mutex.owner_thread-> |
| 391 | thread_id)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 392 | return_ACPI_STATUS(AE_AML_NOT_OWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 393 | } |
| 394 | |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 395 | /* Must have a valid thread ID */ |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 396 | |
| 397 | if (!walk_state->thread) { |
| 398 | ACPI_ERROR((AE_INFO, |
| 399 | "Cannot release Mutex [%4.4s], null thread info", |
| 400 | acpi_ut_get_node_name(obj_desc->mutex.node))); |
| 401 | return_ACPI_STATUS(AE_AML_INTERNAL); |
| 402 | } |
| 403 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 404 | /* |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 405 | * The sync level of the mutex must be less than or equal to the current |
| 406 | * sync level |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 407 | */ |
| 408 | if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 409 | ACPI_ERROR((AE_INFO, |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 410 | "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %d current %d", |
| 411 | acpi_ut_get_node_name(obj_desc->mutex.node), |
| 412 | obj_desc->mutex.sync_level, |
| 413 | walk_state->thread->current_sync_level)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 414 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 417 | status = acpi_ex_release_mutex_object(obj_desc); |
Lin Ming | 941f48b | 2008-04-10 19:06:41 +0400 | [diff] [blame^] | 418 | if (ACPI_FAILURE(status)) { |
| 419 | return_ACPI_STATUS(status); |
| 420 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 421 | |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 422 | if (obj_desc->mutex.acquisition_depth == 0) { |
Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 423 | |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 424 | /* Restore the original sync_level */ |
| 425 | |
| 426 | walk_state->thread->current_sync_level = |
| 427 | obj_desc->mutex.original_sync_level; |
| 428 | } |
Lin Ming | 941f48b | 2008-04-10 19:06:41 +0400 | [diff] [blame^] | 429 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 430 | return_ACPI_STATUS(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 431 | } |
| 432 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 433 | /******************************************************************************* |
| 434 | * |
| 435 | * FUNCTION: acpi_ex_release_all_mutexes |
| 436 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 437 | * PARAMETERS: Thread - Current executing thread object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 438 | * |
| 439 | * RETURN: Status |
| 440 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 441 | * DESCRIPTION: Release all mutexes held by this thread |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 442 | * |
Bob Moore | f70a5e7 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 443 | * NOTE: This function is called as the thread is exiting the interpreter. |
| 444 | * Mutexes are not released when an individual control method is exited, but |
| 445 | * only when the parent thread actually exits the interpreter. This allows one |
| 446 | * method to acquire a mutex, and a different method to release it, as long as |
| 447 | * this is performed underneath a single parent control method. |
| 448 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 449 | ******************************************************************************/ |
| 450 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 451 | void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 453 | union acpi_operand_object *next = thread->acquired_mutex_list; |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 454 | union acpi_operand_object *obj_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 455 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 456 | ACPI_FUNCTION_ENTRY(); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 457 | |
| 458 | /* Traverse the list of owned mutexes, releasing each one */ |
| 459 | |
| 460 | while (next) { |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 461 | obj_desc = next; |
| 462 | next = obj_desc->mutex.next; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 463 | |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 464 | obj_desc->mutex.prev = NULL; |
| 465 | obj_desc->mutex.next = NULL; |
Bob Moore | f70a5e7 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 466 | obj_desc->mutex.acquisition_depth = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 468 | /* Release the mutex, special case for Global Lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 469 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 470 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 471 | |
| 472 | /* Ignore errors */ |
| 473 | |
| 474 | (void)acpi_ev_release_global_lock(); |
| 475 | } else { |
| 476 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | /* Mark mutex unowned */ |
| 480 | |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 481 | obj_desc->mutex.owner_thread = NULL; |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 482 | obj_desc->mutex.thread_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 483 | |
| 484 | /* Update Thread sync_level (Last mutex is the important one) */ |
| 485 | |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 486 | thread->current_sync_level = |
| 487 | obj_desc->mutex.original_sync_level; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 488 | } |
| 489 | } |