1
2
3
4
5
6
7
8
9
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#include <acpi/acpi.h>
45#include "accommon.h"
46#include "acinterp.h"
47#include "acevents.h"
48
49#define _COMPONENT ACPI_EXECUTER
50ACPI_MODULE_NAME("exmutex")
51
52
53static void
54acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
55 struct acpi_thread_state *thread);
56
57
58
59
60
61
62
63
64
65
66
67
68
69void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
70{
71 struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
72
73 if (!thread) {
74 return;
75 }
76
77
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;
85
86
87
88
89
90
91
92 (obj_desc->mutex.prev)->mutex.original_sync_level =
93 obj_desc->mutex.original_sync_level;
94 } else {
95 thread->acquired_mutex_list = obj_desc->mutex.next;
96 }
97}
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112static void
113acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
114 struct acpi_thread_state *thread)
115{
116 union acpi_operand_object *list_head;
117
118 list_head = thread->acquired_mutex_list;
119
120
121
122 obj_desc->mutex.prev = NULL;
123 obj_desc->mutex.next = list_head;
124
125
126
127 if (list_head) {
128 list_head->mutex.prev = obj_desc;
129 }
130
131
132
133 thread->acquired_mutex_list = obj_desc;
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159acpi_status
160acpi_ex_acquire_mutex_object(u16 timeout,
161 union acpi_operand_object *obj_desc,
162 acpi_thread_id thread_id)
163{
164 acpi_status status;
165
166 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
167
168 if (!obj_desc) {
169 return_ACPI_STATUS(AE_BAD_PARAMETER);
170 }
171
172
173
174 if (obj_desc->mutex.thread_id == thread_id) {
175
176
177
178
179 obj_desc->mutex.acquisition_depth++;
180 return_ACPI_STATUS(AE_OK);
181 }
182
183
184
185 if (obj_desc == acpi_gbl_global_lock_mutex) {
186 status = acpi_ev_acquire_global_lock(timeout);
187 } else {
188 status =
189 acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
190 timeout);
191 }
192
193 if (ACPI_FAILURE(status)) {
194
195
196
197 return_ACPI_STATUS(status);
198 }
199
200
201
202 obj_desc->mutex.thread_id = thread_id;
203 obj_desc->mutex.acquisition_depth = 1;
204 obj_desc->mutex.original_sync_level = 0;
205 obj_desc->mutex.owner_thread = NULL;
206
207 return_ACPI_STATUS(AE_OK);
208}
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224acpi_status
225acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
226 union acpi_operand_object *obj_desc,
227 struct acpi_walk_state *walk_state)
228{
229 acpi_status status;
230
231 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
232
233 if (!obj_desc) {
234 return_ACPI_STATUS(AE_BAD_PARAMETER);
235 }
236
237
238
239 if (!walk_state->thread) {
240 ACPI_ERROR((AE_INFO,
241 "Cannot acquire Mutex [%4.4s], null thread info",
242 acpi_ut_get_node_name(obj_desc->mutex.node)));
243 return_ACPI_STATUS(AE_AML_INTERNAL);
244 }
245
246
247
248
249
250 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
251 ACPI_ERROR((AE_INFO,
252 "Cannot acquire Mutex [%4.4s], "
253 "current SyncLevel is too large (%u)",
254 acpi_ut_get_node_name(obj_desc->mutex.node),
255 walk_state->thread->current_sync_level));
256 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
257 }
258
259 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
260 "Acquiring: Mutex SyncLevel %u, Thread SyncLevel %u, "
261 "Depth %u TID %p\n",
262 obj_desc->mutex.sync_level,
263 walk_state->thread->current_sync_level,
264 obj_desc->mutex.acquisition_depth,
265 walk_state->thread));
266
267 status = acpi_ex_acquire_mutex_object((u16)time_desc->integer.value,
268 obj_desc,
269 walk_state->thread->thread_id);
270
271 if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
272
273
274
275 obj_desc->mutex.owner_thread = walk_state->thread;
276 obj_desc->mutex.original_sync_level =
277 walk_state->thread->current_sync_level;
278 walk_state->thread->current_sync_level =
279 obj_desc->mutex.sync_level;
280
281
282
283 acpi_ex_link_mutex(obj_desc, walk_state->thread);
284 }
285
286 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
287 "Acquired: Mutex SyncLevel %u, Thread SyncLevel %u, Depth %u\n",
288 obj_desc->mutex.sync_level,
289 walk_state->thread->current_sync_level,
290 obj_desc->mutex.acquisition_depth));
291
292 return_ACPI_STATUS(status);
293}
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
318{
319 acpi_status status = AE_OK;
320
321 ACPI_FUNCTION_TRACE(ex_release_mutex_object);
322
323 if (obj_desc->mutex.acquisition_depth == 0) {
324 return_ACPI_STATUS(AE_NOT_ACQUIRED);
325 }
326
327
328
329 obj_desc->mutex.acquisition_depth--;
330 if (obj_desc->mutex.acquisition_depth != 0) {
331
332
333
334 return_ACPI_STATUS(AE_OK);
335 }
336
337 if (obj_desc->mutex.owner_thread) {
338
339
340
341 acpi_ex_unlink_mutex(obj_desc);
342 obj_desc->mutex.owner_thread = NULL;
343 }
344
345
346
347 if (obj_desc == acpi_gbl_global_lock_mutex) {
348 status = acpi_ev_release_global_lock();
349 } else {
350 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
351 }
352
353
354
355 obj_desc->mutex.thread_id = 0;
356 return_ACPI_STATUS(status);
357}
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372acpi_status
373acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
374 struct acpi_walk_state *walk_state)
375{
376 u8 previous_sync_level;
377 struct acpi_thread_state *owner_thread;
378 acpi_status status = AE_OK;
379
380 ACPI_FUNCTION_TRACE(ex_release_mutex);
381
382 if (!obj_desc) {
383 return_ACPI_STATUS(AE_BAD_PARAMETER);
384 }
385
386 owner_thread = obj_desc->mutex.owner_thread;
387
388
389
390 if (!owner_thread) {
391 ACPI_ERROR((AE_INFO,
392 "Cannot release Mutex [%4.4s], not acquired",
393 acpi_ut_get_node_name(obj_desc->mutex.node)));
394 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
395 }
396
397
398
399 if (!walk_state->thread) {
400 ACPI_ERROR((AE_INFO,
401 "Cannot release Mutex [%4.4s], null thread info",
402 acpi_ut_get_node_name(obj_desc->mutex.node)));
403 return_ACPI_STATUS(AE_AML_INTERNAL);
404 }
405
406
407
408
409
410 if ((owner_thread->thread_id != walk_state->thread->thread_id) &&
411 (obj_desc != acpi_gbl_global_lock_mutex)) {
412 ACPI_ERROR((AE_INFO,
413 "Thread %u cannot release Mutex [%4.4s] acquired by thread %u",
414 (u32)walk_state->thread->thread_id,
415 acpi_ut_get_node_name(obj_desc->mutex.node),
416 (u32)owner_thread->thread_id));
417 return_ACPI_STATUS(AE_AML_NOT_OWNER);
418 }
419
420
421
422
423
424
425
426
427 if (obj_desc->mutex.sync_level != owner_thread->current_sync_level) {
428 ACPI_ERROR((AE_INFO,
429 "Cannot release Mutex [%4.4s], SyncLevel mismatch: "
430 "mutex %u current %u",
431 acpi_ut_get_node_name(obj_desc->mutex.node),
432 obj_desc->mutex.sync_level,
433 walk_state->thread->current_sync_level));
434 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
435 }
436
437
438
439
440
441
442 previous_sync_level =
443 owner_thread->acquired_mutex_list->mutex.original_sync_level;
444
445 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
446 "Releasing: Object SyncLevel %u, Thread SyncLevel %u, "
447 "Prev SyncLevel %u, Depth %u TID %p\n",
448 obj_desc->mutex.sync_level,
449 walk_state->thread->current_sync_level,
450 previous_sync_level,
451 obj_desc->mutex.acquisition_depth,
452 walk_state->thread));
453
454 status = acpi_ex_release_mutex_object(obj_desc);
455 if (ACPI_FAILURE(status)) {
456 return_ACPI_STATUS(status);
457 }
458
459 if (obj_desc->mutex.acquisition_depth == 0) {
460
461
462
463 owner_thread->current_sync_level = previous_sync_level;
464 }
465
466 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
467 "Released: Object SyncLevel %u, Thread SyncLevel, %u, "
468 "Prev SyncLevel %u, Depth %u\n",
469 obj_desc->mutex.sync_level,
470 walk_state->thread->current_sync_level,
471 previous_sync_level,
472 obj_desc->mutex.acquisition_depth));
473
474 return_ACPI_STATUS(status);
475}
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
496{
497 union acpi_operand_object *next = thread->acquired_mutex_list;
498 union acpi_operand_object *obj_desc;
499
500 ACPI_FUNCTION_TRACE(ex_release_all_mutexes);
501
502
503
504 while (next) {
505 obj_desc = next;
506 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
507 "Mutex [%4.4s] force-release, SyncLevel %u Depth %u\n",
508 obj_desc->mutex.node->name.ascii,
509 obj_desc->mutex.sync_level,
510 obj_desc->mutex.acquisition_depth));
511
512
513
514 if (obj_desc == acpi_gbl_global_lock_mutex) {
515
516
517
518 (void)acpi_ev_release_global_lock();
519 } else {
520 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
521 }
522
523
524
525 thread->current_sync_level =
526 obj_desc->mutex.original_sync_level;
527
528
529
530 next = obj_desc->mutex.next;
531
532 obj_desc->mutex.prev = NULL;
533 obj_desc->mutex.next = NULL;
534 obj_desc->mutex.acquisition_depth = 0;
535 obj_desc->mutex.owner_thread = NULL;
536 obj_desc->mutex.thread_id = 0;
537 }
538
539 return_VOID;
540}
541