Erik Schmauss | 9585763 | 2018-03-14 16:13:07 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /****************************************************************************** |
| 3 | * |
| 4 | * Module Name: exmutex - ASL Mutex Acquire/Release functions |
| 5 | * |
Bob Moore | 840c02c | 2019-01-14 09:55:25 -0800 | [diff] [blame] | 6 | * Copyright (C) 2000 - 2019, Intel Corp. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 7 | * |
Erik Schmauss | 9585763 | 2018-03-14 16:13:07 -0700 | [diff] [blame] | 8 | *****************************************************************************/ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 9 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include <acpi/acpi.h> |
Len Brown | e2f7a77 | 2009-01-09 00:30:03 -0500 | [diff] [blame] | 11 | #include "accommon.h" |
| 12 | #include "acinterp.h" |
| 13 | #include "acevents.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | |
| 15 | #define _COMPONENT ACPI_EXECUTER |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 16 | ACPI_MODULE_NAME("exmutex") |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 18 | /* Local prototypes */ |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 19 | static void |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 20 | acpi_ex_link_mutex(union acpi_operand_object *obj_desc, |
| 21 | struct acpi_thread_state *thread); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 22 | |
| 23 | /******************************************************************************* |
| 24 | * |
| 25 | * FUNCTION: acpi_ex_unlink_mutex |
| 26 | * |
| 27 | * PARAMETERS: obj_desc - The mutex to be unlinked |
| 28 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 29 | * RETURN: None |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | * |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 31 | * DESCRIPTION: Remove a mutex from the "AcquiredMutex" list |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | * |
| 33 | ******************************************************************************/ |
| 34 | |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 35 | void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 36 | { |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 37 | struct acpi_thread_state *thread = obj_desc->mutex.owner_thread; |
| 38 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | if (!thread) { |
| 40 | return; |
| 41 | } |
| 42 | |
| 43 | /* Doubly linked list */ |
| 44 | |
| 45 | if (obj_desc->mutex.next) { |
| 46 | (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev; |
| 47 | } |
| 48 | |
| 49 | if (obj_desc->mutex.prev) { |
| 50 | (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next; |
Bob Moore | 10a3b46 | 2009-05-21 10:02:34 +0800 | [diff] [blame] | 51 | |
| 52 | /* |
Bob Moore | a7499bc | 2010-04-01 11:04:54 +0800 | [diff] [blame] | 53 | * Migrate the previous sync level associated with this mutex to |
| 54 | * the previous mutex on the list so that it may be preserved. |
| 55 | * This handles the case where several mutexes have been acquired |
| 56 | * at the same level, but are not released in opposite order. |
Bob Moore | 10a3b46 | 2009-05-21 10:02:34 +0800 | [diff] [blame] | 57 | */ |
| 58 | (obj_desc->mutex.prev)->mutex.original_sync_level = |
| 59 | obj_desc->mutex.original_sync_level; |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 60 | } else { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 61 | thread->acquired_mutex_list = obj_desc->mutex.next; |
| 62 | } |
| 63 | } |
| 64 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 65 | /******************************************************************************* |
| 66 | * |
| 67 | * FUNCTION: acpi_ex_link_mutex |
| 68 | * |
Bob Moore | a7499bc | 2010-04-01 11:04:54 +0800 | [diff] [blame] | 69 | * PARAMETERS: obj_desc - The mutex to be linked |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 70 | * thread - Current executing thread object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 71 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 72 | * RETURN: None |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 73 | * |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 74 | * DESCRIPTION: Add a mutex to the "AcquiredMutex" list for this walk |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 75 | * |
| 76 | ******************************************************************************/ |
| 77 | |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 78 | static void |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 79 | acpi_ex_link_mutex(union acpi_operand_object *obj_desc, |
| 80 | struct acpi_thread_state *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 81 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 82 | union acpi_operand_object *list_head; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 83 | |
| 84 | list_head = thread->acquired_mutex_list; |
| 85 | |
| 86 | /* This object will be the first object in the list */ |
| 87 | |
| 88 | obj_desc->mutex.prev = NULL; |
| 89 | obj_desc->mutex.next = list_head; |
| 90 | |
| 91 | /* Update old first object to point back to this object */ |
| 92 | |
| 93 | if (list_head) { |
| 94 | list_head->mutex.prev = obj_desc; |
| 95 | } |
| 96 | |
| 97 | /* Update list head */ |
| 98 | |
| 99 | thread->acquired_mutex_list = obj_desc; |
| 100 | } |
| 101 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 102 | /******************************************************************************* |
| 103 | * |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 104 | * FUNCTION: acpi_ex_acquire_mutex_object |
| 105 | * |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 106 | * PARAMETERS: timeout - Timeout in milliseconds |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 107 | * obj_desc - Mutex object |
Bob Moore | a7499bc | 2010-04-01 11:04:54 +0800 | [diff] [blame] | 108 | * thread_id - Current thread state |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 109 | * |
| 110 | * RETURN: Status |
| 111 | * |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 112 | * DESCRIPTION: Acquire an AML mutex, low-level interface. Provides a common |
| 113 | * path that supports multiple acquires by the same thread. |
| 114 | * |
| 115 | * MUTEX: Interpreter must be locked |
| 116 | * |
| 117 | * NOTE: This interface is called from three places: |
| 118 | * 1) From acpi_ex_acquire_mutex, via an AML Acquire() operator |
| 119 | * 2) From acpi_ex_acquire_global_lock when an AML Field access requires the |
| 120 | * global lock |
| 121 | * 3) From the external interface, acpi_acquire_global_lock |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 122 | * |
| 123 | ******************************************************************************/ |
| 124 | |
| 125 | acpi_status |
| 126 | acpi_ex_acquire_mutex_object(u16 timeout, |
| 127 | union acpi_operand_object *obj_desc, |
| 128 | acpi_thread_id thread_id) |
| 129 | { |
| 130 | acpi_status status; |
| 131 | |
| 132 | ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc); |
| 133 | |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 134 | if (!obj_desc) { |
| 135 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
| 136 | } |
| 137 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 138 | /* Support for multiple acquires by the owning thread */ |
| 139 | |
| 140 | if (obj_desc->mutex.thread_id == thread_id) { |
| 141 | /* |
| 142 | * The mutex is already owned by this thread, just increment the |
| 143 | * acquisition depth |
| 144 | */ |
| 145 | obj_desc->mutex.acquisition_depth++; |
| 146 | return_ACPI_STATUS(AE_OK); |
| 147 | } |
| 148 | |
| 149 | /* Acquire the mutex, wait if necessary. Special case for Global Lock */ |
| 150 | |
| 151 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
| 152 | status = acpi_ev_acquire_global_lock(timeout); |
| 153 | } else { |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 154 | status = |
| 155 | acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex, |
| 156 | timeout); |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | if (ACPI_FAILURE(status)) { |
| 160 | |
| 161 | /* Includes failure from a timeout on time_desc */ |
| 162 | |
| 163 | return_ACPI_STATUS(status); |
| 164 | } |
| 165 | |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 166 | /* Acquired the mutex: update mutex object */ |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 167 | |
| 168 | obj_desc->mutex.thread_id = thread_id; |
| 169 | obj_desc->mutex.acquisition_depth = 1; |
| 170 | obj_desc->mutex.original_sync_level = 0; |
| 171 | obj_desc->mutex.owner_thread = NULL; /* Used only for AML Acquire() */ |
| 172 | |
| 173 | return_ACPI_STATUS(AE_OK); |
| 174 | } |
| 175 | |
| 176 | /******************************************************************************* |
| 177 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 178 | * FUNCTION: acpi_ex_acquire_mutex |
| 179 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 180 | * PARAMETERS: time_desc - Timeout integer |
| 181 | * obj_desc - Mutex object |
| 182 | * walk_state - Current method execution state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 183 | * |
| 184 | * RETURN: Status |
| 185 | * |
| 186 | * DESCRIPTION: Acquire an AML mutex |
| 187 | * |
| 188 | ******************************************************************************/ |
| 189 | |
| 190 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 191 | acpi_ex_acquire_mutex(union acpi_operand_object *time_desc, |
| 192 | union acpi_operand_object *obj_desc, |
| 193 | struct acpi_walk_state *walk_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 194 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 195 | acpi_status status; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 196 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 197 | ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 198 | |
| 199 | if (!obj_desc) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 200 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 201 | } |
| 202 | |
Bob Moore | a7499bc | 2010-04-01 11:04:54 +0800 | [diff] [blame] | 203 | /* Must have a valid thread state struct */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | |
| 205 | if (!walk_state->thread) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 206 | ACPI_ERROR((AE_INFO, |
| 207 | "Cannot acquire Mutex [%4.4s], null thread info", |
| 208 | acpi_ut_get_node_name(obj_desc->mutex.node))); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 209 | return_ACPI_STATUS(AE_AML_INTERNAL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | /* |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 213 | * Current sync level must be less than or equal to the sync level |
| 214 | * of the mutex. This mechanism provides some deadlock prevention. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 215 | */ |
| 216 | if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 217 | ACPI_ERROR((AE_INFO, |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 218 | "Cannot acquire Mutex [%4.4s], " |
| 219 | "current SyncLevel is too large (%u)", |
Bob Moore | 967440e3 | 2006-06-23 17:04:00 -0400 | [diff] [blame] | 220 | acpi_ut_get_node_name(obj_desc->mutex.node), |
| 221 | walk_state->thread->current_sync_level)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 222 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 223 | } |
| 224 | |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 225 | ACPI_DEBUG_PRINT((ACPI_DB_EXEC, |
| 226 | "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, " |
| 227 | "Depth %u TID %p\n", |
| 228 | obj_desc->mutex.sync_level, |
| 229 | walk_state->thread->current_sync_level, |
| 230 | obj_desc->mutex.acquisition_depth, |
| 231 | walk_state->thread)); |
| 232 | |
Lv Zheng | 5431b65 | 2015-12-29 13:52:32 +0800 | [diff] [blame] | 233 | status = acpi_ex_acquire_mutex_object((u16)time_desc->integer.value, |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 234 | obj_desc, |
| 235 | walk_state->thread->thread_id); |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 236 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 237 | if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) { |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 238 | |
| 239 | /* Save Thread object, original/current sync levels */ |
| 240 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 241 | obj_desc->mutex.owner_thread = walk_state->thread; |
| 242 | obj_desc->mutex.original_sync_level = |
| 243 | walk_state->thread->current_sync_level; |
| 244 | walk_state->thread->current_sync_level = |
| 245 | obj_desc->mutex.sync_level; |
| 246 | |
| 247 | /* Link the mutex to the current thread for force-unlock at method exit */ |
| 248 | |
| 249 | acpi_ex_link_mutex(obj_desc, walk_state->thread); |
| 250 | } |
| 251 | |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 252 | ACPI_DEBUG_PRINT((ACPI_DB_EXEC, |
| 253 | "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n", |
| 254 | obj_desc->mutex.sync_level, |
| 255 | walk_state->thread->current_sync_level, |
| 256 | obj_desc->mutex.acquisition_depth)); |
| 257 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 258 | return_ACPI_STATUS(status); |
| 259 | } |
| 260 | |
| 261 | /******************************************************************************* |
| 262 | * |
| 263 | * FUNCTION: acpi_ex_release_mutex_object |
| 264 | * |
| 265 | * PARAMETERS: obj_desc - The object descriptor for this op |
| 266 | * |
| 267 | * RETURN: Status |
| 268 | * |
| 269 | * DESCRIPTION: Release a previously acquired Mutex, low level interface. |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 270 | * Provides a common path that supports multiple releases (after |
| 271 | * previous multiple acquires) by the same thread. |
| 272 | * |
| 273 | * MUTEX: Interpreter must be locked |
| 274 | * |
| 275 | * NOTE: This interface is called from three places: |
| 276 | * 1) From acpi_ex_release_mutex, via an AML Acquire() operator |
| 277 | * 2) From acpi_ex_release_global_lock when an AML Field access requires the |
| 278 | * global lock |
| 279 | * 3) From the external interface, acpi_release_global_lock |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 280 | * |
| 281 | ******************************************************************************/ |
| 282 | |
| 283 | acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc) |
| 284 | { |
| 285 | acpi_status status = AE_OK; |
| 286 | |
| 287 | ACPI_FUNCTION_TRACE(ex_release_mutex_object); |
| 288 | |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 289 | if (obj_desc->mutex.acquisition_depth == 0) { |
Bob Moore | 68aafc3 | 2012-10-31 02:26:01 +0000 | [diff] [blame] | 290 | return_ACPI_STATUS(AE_NOT_ACQUIRED); |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 291 | } |
| 292 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 293 | /* Match multiple Acquires with multiple Releases */ |
| 294 | |
| 295 | obj_desc->mutex.acquisition_depth--; |
| 296 | if (obj_desc->mutex.acquisition_depth != 0) { |
| 297 | |
| 298 | /* Just decrement the depth and return */ |
| 299 | |
| 300 | return_ACPI_STATUS(AE_OK); |
| 301 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 302 | |
Len Brown | 262a7a2 | 2007-05-09 23:01:59 -0400 | [diff] [blame] | 303 | if (obj_desc->mutex.owner_thread) { |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 304 | |
| 305 | /* Unlink the mutex from the owner's list */ |
| 306 | |
| 307 | acpi_ex_unlink_mutex(obj_desc); |
| 308 | obj_desc->mutex.owner_thread = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 309 | } |
| 310 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 311 | /* Release the mutex, special case for Global Lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 312 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 313 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
| 314 | status = acpi_ev_release_global_lock(); |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 315 | } else { |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 316 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 317 | } |
| 318 | |
Bob Moore | 91e38d1 | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 319 | /* Clear mutex info */ |
| 320 | |
Lin Ming | 28eb3fc | 2010-09-15 13:55:13 +0800 | [diff] [blame] | 321 | obj_desc->mutex.thread_id = 0; |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 322 | return_ACPI_STATUS(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 323 | } |
| 324 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 325 | /******************************************************************************* |
| 326 | * |
| 327 | * FUNCTION: acpi_ex_release_mutex |
| 328 | * |
| 329 | * PARAMETERS: obj_desc - The object descriptor for this op |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 330 | * walk_state - Current method execution state |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 331 | * |
| 332 | * RETURN: Status |
| 333 | * |
| 334 | * DESCRIPTION: Release a previously acquired Mutex. |
| 335 | * |
| 336 | ******************************************************************************/ |
| 337 | |
| 338 | acpi_status |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 339 | acpi_ex_release_mutex(union acpi_operand_object *obj_desc, |
| 340 | struct acpi_walk_state *walk_state) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 341 | { |
Bob Moore | 10a3b46 | 2009-05-21 10:02:34 +0800 | [diff] [blame] | 342 | u8 previous_sync_level; |
Lin Ming | e0f4028 | 2010-03-05 17:59:54 +0800 | [diff] [blame] | 343 | struct acpi_thread_state *owner_thread; |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 344 | acpi_status status = AE_OK; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 345 | |
Bob Moore | b229cf9 | 2006-04-21 17:15:00 -0400 | [diff] [blame] | 346 | ACPI_FUNCTION_TRACE(ex_release_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 347 | |
| 348 | if (!obj_desc) { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 349 | return_ACPI_STATUS(AE_BAD_PARAMETER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 350 | } |
| 351 | |
Lin Ming | e0f4028 | 2010-03-05 17:59:54 +0800 | [diff] [blame] | 352 | owner_thread = obj_desc->mutex.owner_thread; |
| 353 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 354 | /* The mutex must have been previously acquired in order to release it */ |
| 355 | |
Lin Ming | e0f4028 | 2010-03-05 17:59:54 +0800 | [diff] [blame] | 356 | if (!owner_thread) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 357 | ACPI_ERROR((AE_INFO, |
| 358 | "Cannot release Mutex [%4.4s], not acquired", |
| 359 | acpi_ut_get_node_name(obj_desc->mutex.node))); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 360 | return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 361 | } |
| 362 | |
Lv Zheng | 75c8044 | 2012-12-19 05:36:49 +0000 | [diff] [blame] | 363 | /* Must have a valid thread ID */ |
Lv Zheng | 3e8214e | 2012-12-19 05:37:15 +0000 | [diff] [blame] | 364 | |
Dan Carpenter | fbc3be2 | 2009-12-11 15:31:40 +0800 | [diff] [blame] | 365 | if (!walk_state->thread) { |
| 366 | ACPI_ERROR((AE_INFO, |
| 367 | "Cannot release Mutex [%4.4s], null thread info", |
| 368 | acpi_ut_get_node_name(obj_desc->mutex.node))); |
| 369 | return_ACPI_STATUS(AE_AML_INTERNAL); |
| 370 | } |
| 371 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 372 | /* |
| 373 | * The Mutex is owned, but this thread must be the owner. |
| 374 | * Special case for Global Lock, any thread can release |
| 375 | */ |
Lin Ming | e0f4028 | 2010-03-05 17:59:54 +0800 | [diff] [blame] | 376 | if ((owner_thread->thread_id != walk_state->thread->thread_id) && |
| 377 | (obj_desc != acpi_gbl_global_lock_mutex)) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 378 | ACPI_ERROR((AE_INFO, |
Lin Ming | 28eb3fc | 2010-09-15 13:55:13 +0800 | [diff] [blame] | 379 | "Thread %u cannot release Mutex [%4.4s] acquired by thread %u", |
| 380 | (u32)walk_state->thread->thread_id, |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 381 | acpi_ut_get_node_name(obj_desc->mutex.node), |
Lin Ming | 28eb3fc | 2010-09-15 13:55:13 +0800 | [diff] [blame] | 382 | (u32)owner_thread->thread_id)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 383 | return_ACPI_STATUS(AE_AML_NOT_OWNER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 384 | } |
| 385 | |
| 386 | /* |
Bob Moore | 315c728 | 2009-05-21 10:04:33 +0800 | [diff] [blame] | 387 | * The sync level of the mutex must be equal to the current sync level. In |
| 388 | * other words, the current level means that at least one mutex at that |
| 389 | * level is currently being held. Attempting to release a mutex of a |
| 390 | * different level can only mean that the mutex ordering rule is being |
| 391 | * violated. This behavior is clarified in ACPI 4.0 specification. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 392 | */ |
Lin Ming | e0f4028 | 2010-03-05 17:59:54 +0800 | [diff] [blame] | 393 | if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) { |
Bob Moore | b8e4d89 | 2006-01-27 16:43:00 -0500 | [diff] [blame] | 394 | ACPI_ERROR((AE_INFO, |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 395 | "Cannot release Mutex [%4.4s], SyncLevel mismatch: " |
| 396 | "mutex %u current %u", |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 397 | acpi_ut_get_node_name(obj_desc->mutex.node), |
| 398 | obj_desc->mutex.sync_level, |
| 399 | walk_state->thread->current_sync_level)); |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 400 | return_ACPI_STATUS(AE_AML_MUTEX_ORDER); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 401 | } |
| 402 | |
Bob Moore | 10a3b46 | 2009-05-21 10:02:34 +0800 | [diff] [blame] | 403 | /* |
| 404 | * Get the previous sync_level from the head of the acquired mutex list. |
| 405 | * This handles the case where several mutexes at the same level have been |
| 406 | * acquired, but are not released in reverse order. |
| 407 | */ |
| 408 | previous_sync_level = |
Lin Ming | e0f4028 | 2010-03-05 17:59:54 +0800 | [diff] [blame] | 409 | owner_thread->acquired_mutex_list->mutex.original_sync_level; |
Bob Moore | 10a3b46 | 2009-05-21 10:02:34 +0800 | [diff] [blame] | 410 | |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 411 | ACPI_DEBUG_PRINT((ACPI_DB_EXEC, |
| 412 | "Releasing: Object SyncLevel %u, Thread SyncLevel %u, " |
| 413 | "Prev SyncLevel %u, Depth %u TID %p\n", |
| 414 | obj_desc->mutex.sync_level, |
| 415 | walk_state->thread->current_sync_level, |
| 416 | previous_sync_level, |
| 417 | obj_desc->mutex.acquisition_depth, |
| 418 | walk_state->thread)); |
| 419 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 420 | status = acpi_ex_release_mutex_object(obj_desc); |
Bob Moore | 315c728 | 2009-05-21 10:04:33 +0800 | [diff] [blame] | 421 | if (ACPI_FAILURE(status)) { |
| 422 | return_ACPI_STATUS(status); |
| 423 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 424 | |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 425 | if (obj_desc->mutex.acquisition_depth == 0) { |
Bob Moore | 52fc0b0 | 2006-10-02 00:00:00 -0400 | [diff] [blame] | 426 | |
Bob Moore | 315c728 | 2009-05-21 10:04:33 +0800 | [diff] [blame] | 427 | /* Restore the previous sync_level */ |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 428 | |
Lin Ming | e0f4028 | 2010-03-05 17:59:54 +0800 | [diff] [blame] | 429 | owner_thread->current_sync_level = previous_sync_level; |
Bob Moore | f02e9fa | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 430 | } |
Bob Moore | a7499bc | 2010-04-01 11:04:54 +0800 | [diff] [blame] | 431 | |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 432 | ACPI_DEBUG_PRINT((ACPI_DB_EXEC, |
| 433 | "Released: Object SyncLevel %u, Thread SyncLevel, %u, " |
| 434 | "Prev SyncLevel %u, Depth %u\n", |
| 435 | obj_desc->mutex.sync_level, |
| 436 | walk_state->thread->current_sync_level, |
| 437 | previous_sync_level, |
| 438 | obj_desc->mutex.acquisition_depth)); |
| 439 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 440 | return_ACPI_STATUS(status); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 441 | } |
| 442 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 443 | /******************************************************************************* |
| 444 | * |
| 445 | * FUNCTION: acpi_ex_release_all_mutexes |
| 446 | * |
Bob Moore | ba494be | 2012-07-12 09:40:10 +0800 | [diff] [blame] | 447 | * PARAMETERS: thread - Current executing thread object |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 448 | * |
| 449 | * RETURN: Status |
| 450 | * |
Robert Moore | 44f6c01 | 2005-04-18 22:49:35 -0400 | [diff] [blame] | 451 | * DESCRIPTION: Release all mutexes held by this thread |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 452 | * |
Bob Moore | f70a5e7 | 2007-02-02 19:48:21 +0300 | [diff] [blame] | 453 | * NOTE: This function is called as the thread is exiting the interpreter. |
| 454 | * Mutexes are not released when an individual control method is exited, but |
| 455 | * only when the parent thread actually exits the interpreter. This allows one |
| 456 | * method to acquire a mutex, and a different method to release it, as long as |
| 457 | * this is performed underneath a single parent control method. |
| 458 | * |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 459 | ******************************************************************************/ |
| 460 | |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 461 | void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 462 | { |
Len Brown | 4be44fc | 2005-08-05 00:44:28 -0400 | [diff] [blame] | 463 | union acpi_operand_object *next = thread->acquired_mutex_list; |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 464 | union acpi_operand_object *obj_desc; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 465 | |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 466 | ACPI_FUNCTION_TRACE(ex_release_all_mutexes); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 467 | |
| 468 | /* Traverse the list of owned mutexes, releasing each one */ |
| 469 | |
| 470 | while (next) { |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 471 | obj_desc = next; |
Bob Moore | 2489ef0 | 2012-10-31 02:27:24 +0000 | [diff] [blame] | 472 | ACPI_DEBUG_PRINT((ACPI_DB_EXEC, |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 473 | "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n", |
| 474 | obj_desc->mutex.node->name.ascii, |
| 475 | obj_desc->mutex.sync_level, |
| 476 | obj_desc->mutex.acquisition_depth)); |
Bob Moore | 2489ef0 | 2012-10-31 02:27:24 +0000 | [diff] [blame] | 477 | |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 478 | /* Release the mutex, special case for Global Lock */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 479 | |
Bob Moore | ba886cd | 2008-04-10 19:06:37 +0400 | [diff] [blame] | 480 | if (obj_desc == acpi_gbl_global_lock_mutex) { |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 481 | |
| 482 | /* Ignore errors */ |
| 483 | |
| 484 | (void)acpi_ev_release_global_lock(); |
| 485 | } else { |
| 486 | acpi_os_release_mutex(obj_desc->mutex.os_mutex); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 489 | /* Update Thread sync_level (Last mutex is the important one) */ |
| 490 | |
Bob Moore | c81da66 | 2007-02-02 19:48:18 +0300 | [diff] [blame] | 491 | thread->current_sync_level = |
| 492 | obj_desc->mutex.original_sync_level; |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 493 | |
| 494 | /* Mark mutex unowned */ |
| 495 | |
| 496 | next = obj_desc->mutex.next; |
| 497 | |
| 498 | obj_desc->mutex.prev = NULL; |
| 499 | obj_desc->mutex.next = NULL; |
| 500 | obj_desc->mutex.acquisition_depth = 0; |
| 501 | obj_desc->mutex.owner_thread = NULL; |
| 502 | obj_desc->mutex.thread_id = 0; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 503 | } |
Bob Moore | cd162b3 | 2015-12-29 13:54:28 +0800 | [diff] [blame] | 504 | |
| 505 | return_VOID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 506 | } |