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
45#include <acpi/acpi.h>
46#include <acpi/acinterp.h>
47#include <acpi/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 } else {
86 thread->acquired_mutex_list = obj_desc->mutex.next;
87 }
88}
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103static void
104acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
105 struct acpi_thread_state *thread)
106{
107 union acpi_operand_object *list_head;
108
109 list_head = thread->acquired_mutex_list;
110
111
112
113 obj_desc->mutex.prev = NULL;
114 obj_desc->mutex.next = list_head;
115
116
117
118 if (list_head) {
119 list_head->mutex.prev = obj_desc;
120 }
121
122
123
124 thread->acquired_mutex_list = obj_desc;
125}
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150acpi_status
151acpi_ex_acquire_mutex_object(u16 timeout,
152 union acpi_operand_object *obj_desc,
153 acpi_thread_id thread_id)
154{
155 acpi_status status;
156
157 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex_object, obj_desc);
158
159 if (!obj_desc) {
160 return_ACPI_STATUS(AE_BAD_PARAMETER);
161 }
162
163
164
165 if (obj_desc->mutex.thread_id == thread_id) {
166
167
168
169
170 obj_desc->mutex.acquisition_depth++;
171 return_ACPI_STATUS(AE_OK);
172 }
173
174
175
176 if (obj_desc == acpi_gbl_global_lock_mutex) {
177 status = acpi_ev_acquire_global_lock(timeout);
178 } else {
179 status = acpi_ex_system_wait_mutex(obj_desc->mutex.os_mutex,
180 timeout);
181 }
182
183 if (ACPI_FAILURE(status)) {
184
185
186
187 return_ACPI_STATUS(status);
188 }
189
190
191
192 obj_desc->mutex.thread_id = thread_id;
193 obj_desc->mutex.acquisition_depth = 1;
194 obj_desc->mutex.original_sync_level = 0;
195 obj_desc->mutex.owner_thread = NULL;
196
197 return_ACPI_STATUS(AE_OK);
198}
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214acpi_status
215acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
216 union acpi_operand_object *obj_desc,
217 struct acpi_walk_state *walk_state)
218{
219 acpi_status status;
220
221 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
222
223 if (!obj_desc) {
224 return_ACPI_STATUS(AE_BAD_PARAMETER);
225 }
226
227
228
229 if (!walk_state->thread) {
230 ACPI_ERROR((AE_INFO,
231 "Cannot acquire Mutex [%4.4s], null thread info",
232 acpi_ut_get_node_name(obj_desc->mutex.node)));
233 return_ACPI_STATUS(AE_AML_INTERNAL);
234 }
235
236
237
238
239
240 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
241 ACPI_ERROR((AE_INFO,
242 "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)",
243 acpi_ut_get_node_name(obj_desc->mutex.node),
244 walk_state->thread->current_sync_level));
245 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
246 }
247
248 status = acpi_ex_acquire_mutex_object((u16) time_desc->integer.value,
249 obj_desc,
250 walk_state->thread->thread_id);
251 if (ACPI_SUCCESS(status) && obj_desc->mutex.acquisition_depth == 1) {
252
253
254
255 obj_desc->mutex.owner_thread = walk_state->thread;
256 obj_desc->mutex.original_sync_level =
257 walk_state->thread->current_sync_level;
258 walk_state->thread->current_sync_level =
259 obj_desc->mutex.sync_level;
260
261
262
263 acpi_ex_link_mutex(obj_desc, walk_state->thread);
264 }
265
266 return_ACPI_STATUS(status);
267}
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291acpi_status acpi_ex_release_mutex_object(union acpi_operand_object *obj_desc)
292{
293 acpi_status status = AE_OK;
294
295 ACPI_FUNCTION_TRACE(ex_release_mutex_object);
296
297 if (obj_desc->mutex.acquisition_depth == 0) {
298 return (AE_NOT_ACQUIRED);
299 }
300
301
302
303 obj_desc->mutex.acquisition_depth--;
304 if (obj_desc->mutex.acquisition_depth != 0) {
305
306
307
308 return_ACPI_STATUS(AE_OK);
309 }
310
311 if (obj_desc->mutex.owner_thread) {
312
313
314
315 acpi_ex_unlink_mutex(obj_desc);
316 obj_desc->mutex.owner_thread = NULL;
317 }
318
319
320
321 if (obj_desc == acpi_gbl_global_lock_mutex) {
322 status = acpi_ev_release_global_lock();
323 } else {
324 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
325 }
326
327
328
329 obj_desc->mutex.thread_id = NULL;
330 return_ACPI_STATUS(status);
331}
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346acpi_status
347acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
348 struct acpi_walk_state *walk_state)
349{
350 acpi_status status = AE_OK;
351
352 ACPI_FUNCTION_TRACE(ex_release_mutex);
353
354 if (!obj_desc) {
355 return_ACPI_STATUS(AE_BAD_PARAMETER);
356 }
357
358
359
360 if (!obj_desc->mutex.owner_thread) {
361 ACPI_ERROR((AE_INFO,
362 "Cannot release Mutex [%4.4s], not acquired",
363 acpi_ut_get_node_name(obj_desc->mutex.node)));
364 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
365 }
366
367
368
369
370
371 if ((obj_desc->mutex.owner_thread->thread_id !=
372 walk_state->thread->thread_id)
373 && (obj_desc != acpi_gbl_global_lock_mutex)) {
374 ACPI_ERROR((AE_INFO,
375 "Thread %lX cannot release Mutex [%4.4s] acquired by thread %lX",
376 (unsigned long)walk_state->thread->thread_id,
377 acpi_ut_get_node_name(obj_desc->mutex.node),
378 (unsigned long)obj_desc->mutex.owner_thread->
379 thread_id));
380 return_ACPI_STATUS(AE_AML_NOT_OWNER);
381 }
382
383
384
385 if (!walk_state->thread) {
386 ACPI_ERROR((AE_INFO,
387 "Cannot release Mutex [%4.4s], null thread info",
388 acpi_ut_get_node_name(obj_desc->mutex.node)));
389 return_ACPI_STATUS(AE_AML_INTERNAL);
390 }
391
392
393
394
395
396 if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
397 ACPI_ERROR((AE_INFO,
398 "Cannot release Mutex [%4.4s], SyncLevel mismatch: mutex %d current %d",
399 acpi_ut_get_node_name(obj_desc->mutex.node),
400 obj_desc->mutex.sync_level,
401 walk_state->thread->current_sync_level));
402 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
403 }
404
405 status = acpi_ex_release_mutex_object(obj_desc);
406
407 if (obj_desc->mutex.acquisition_depth == 0) {
408
409
410
411 walk_state->thread->current_sync_level =
412 obj_desc->mutex.original_sync_level;
413 }
414 return_ACPI_STATUS(status);
415}
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
436{
437 union acpi_operand_object *next = thread->acquired_mutex_list;
438 union acpi_operand_object *obj_desc;
439
440 ACPI_FUNCTION_ENTRY();
441
442
443
444 while (next) {
445 obj_desc = next;
446 next = obj_desc->mutex.next;
447
448 obj_desc->mutex.prev = NULL;
449 obj_desc->mutex.next = NULL;
450 obj_desc->mutex.acquisition_depth = 0;
451
452
453
454 if (obj_desc == acpi_gbl_global_lock_mutex) {
455
456
457
458 (void)acpi_ev_release_global_lock();
459 } else {
460 acpi_os_release_mutex(obj_desc->mutex.os_mutex);
461 }
462
463
464
465 obj_desc->mutex.owner_thread = NULL;
466 obj_desc->mutex.thread_id = NULL;
467
468
469
470 thread->current_sync_level =
471 obj_desc->mutex.original_sync_level;
472 }
473}
474