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
48#define _COMPONENT ACPI_EXECUTER
49ACPI_MODULE_NAME("exmutex")
50
51
52static void
53acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
54 struct acpi_thread_state *thread);
55
56
57
58
59
60
61
62
63
64
65
66
67
68void acpi_ex_unlink_mutex(union acpi_operand_object *obj_desc)
69{
70 struct acpi_thread_state *thread = obj_desc->mutex.owner_thread;
71
72 if (!thread) {
73 return;
74 }
75
76
77
78 if (obj_desc->mutex.next) {
79 (obj_desc->mutex.next)->mutex.prev = obj_desc->mutex.prev;
80 }
81
82 if (obj_desc->mutex.prev) {
83 (obj_desc->mutex.prev)->mutex.next = obj_desc->mutex.next;
84 } else {
85 thread->acquired_mutex_list = obj_desc->mutex.next;
86 }
87}
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102static void
103acpi_ex_link_mutex(union acpi_operand_object *obj_desc,
104 struct acpi_thread_state *thread)
105{
106 union acpi_operand_object *list_head;
107
108 list_head = thread->acquired_mutex_list;
109
110
111
112 obj_desc->mutex.prev = NULL;
113 obj_desc->mutex.next = list_head;
114
115
116
117 if (list_head) {
118 list_head->mutex.prev = obj_desc;
119 }
120
121
122
123 thread->acquired_mutex_list = obj_desc;
124}
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140acpi_status
141acpi_ex_acquire_mutex(union acpi_operand_object *time_desc,
142 union acpi_operand_object *obj_desc,
143 struct acpi_walk_state *walk_state)
144{
145 acpi_status status;
146
147 ACPI_FUNCTION_TRACE_PTR(ex_acquire_mutex, obj_desc);
148
149 if (!obj_desc) {
150 return_ACPI_STATUS(AE_BAD_PARAMETER);
151 }
152
153
154
155 if (!walk_state->thread) {
156 ACPI_ERROR((AE_INFO,
157 "Cannot acquire Mutex [%4.4s], null thread info",
158 acpi_ut_get_node_name(obj_desc->mutex.node)));
159 return_ACPI_STATUS(AE_AML_INTERNAL);
160 }
161
162
163
164
165
166 if (walk_state->thread->current_sync_level > obj_desc->mutex.sync_level) {
167 ACPI_ERROR((AE_INFO,
168 "Cannot acquire Mutex [%4.4s], current SyncLevel is too large (%d)",
169 acpi_ut_get_node_name(obj_desc->mutex.node),
170 walk_state->thread->current_sync_level));
171 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
172 }
173
174
175
176 if (obj_desc->mutex.owner_thread) {
177
178
179
180 if ((obj_desc->mutex.owner_thread->thread_id ==
181 walk_state->thread->thread_id) ||
182 (obj_desc->mutex.os_mutex == ACPI_GLOBAL_LOCK)) {
183
184
185
186
187 obj_desc->mutex.acquisition_depth++;
188 return_ACPI_STATUS(AE_OK);
189 }
190 }
191
192
193
194 status = acpi_ex_system_acquire_mutex(time_desc, obj_desc);
195 if (ACPI_FAILURE(status)) {
196
197
198
199 return_ACPI_STATUS(status);
200 }
201
202
203
204 obj_desc->mutex.owner_thread = walk_state->thread;
205 obj_desc->mutex.acquisition_depth = 1;
206 obj_desc->mutex.original_sync_level =
207 walk_state->thread->current_sync_level;
208
209 walk_state->thread->current_sync_level = obj_desc->mutex.sync_level;
210
211
212
213 acpi_ex_link_mutex(obj_desc, walk_state->thread);
214
215 return_ACPI_STATUS(AE_OK);
216}
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231acpi_status
232acpi_ex_release_mutex(union acpi_operand_object *obj_desc,
233 struct acpi_walk_state *walk_state)
234{
235 acpi_status status;
236
237 ACPI_FUNCTION_TRACE(ex_release_mutex);
238
239 if (!obj_desc) {
240 return_ACPI_STATUS(AE_BAD_PARAMETER);
241 }
242
243
244
245 if (!obj_desc->mutex.owner_thread) {
246 ACPI_ERROR((AE_INFO,
247 "Cannot release Mutex [%4.4s], not acquired",
248 acpi_ut_get_node_name(obj_desc->mutex.node)));
249 return_ACPI_STATUS(AE_AML_MUTEX_NOT_ACQUIRED);
250 }
251
252
253
254 if (!walk_state->thread) {
255 ACPI_ERROR((AE_INFO,
256 "Cannot release Mutex [%4.4s], null thread info",
257 acpi_ut_get_node_name(obj_desc->mutex.node)));
258 return_ACPI_STATUS(AE_AML_INTERNAL);
259 }
260
261
262
263
264
265 if ((obj_desc->mutex.owner_thread->thread_id !=
266 walk_state->thread->thread_id)
267 && (obj_desc->mutex.os_mutex != ACPI_GLOBAL_LOCK)) {
268 ACPI_ERROR((AE_INFO,
269 "Thread %X cannot release Mutex [%4.4s] acquired by thread %X",
270 (u32) walk_state->thread->thread_id,
271 acpi_ut_get_node_name(obj_desc->mutex.node),
272 (u32) obj_desc->mutex.owner_thread->thread_id));
273 return_ACPI_STATUS(AE_AML_NOT_OWNER);
274 }
275
276
277
278
279
280 if (obj_desc->mutex.sync_level > walk_state->thread->current_sync_level) {
281 ACPI_ERROR((AE_INFO,
282 "Cannot release Mutex [%4.4s], incorrect SyncLevel",
283 acpi_ut_get_node_name(obj_desc->mutex.node)));
284 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
285 }
286
287
288
289 obj_desc->mutex.acquisition_depth--;
290 if (obj_desc->mutex.acquisition_depth != 0) {
291
292
293
294 return_ACPI_STATUS(AE_OK);
295 }
296
297
298
299 acpi_ex_unlink_mutex(obj_desc);
300
301
302
303 status = acpi_ex_system_release_mutex(obj_desc);
304
305
306
307 obj_desc->mutex.owner_thread = NULL;
308 walk_state->thread->current_sync_level =
309 obj_desc->mutex.original_sync_level;
310
311 return_ACPI_STATUS(status);
312}
313
314
315
316
317
318
319
320
321
322
323
324
325
326void acpi_ex_release_all_mutexes(struct acpi_thread_state *thread)
327{
328 union acpi_operand_object *next = thread->acquired_mutex_list;
329 union acpi_operand_object *this;
330 acpi_status status;
331
332 ACPI_FUNCTION_ENTRY();
333
334
335
336 while (next) {
337 this = next;
338 next = this->mutex.next;
339
340 this->mutex.acquisition_depth = 1;
341 this->mutex.prev = NULL;
342 this->mutex.next = NULL;
343
344
345
346 status = acpi_ex_system_release_mutex(this);
347 if (ACPI_FAILURE(status)) {
348 continue;
349 }
350
351
352
353 this->mutex.owner_thread = NULL;
354
355
356
357 thread->current_sync_level = this->mutex.original_sync_level;
358 }
359}
360