blob: 64ebccb6e3821a18ab45b681b9d198372c55297d [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
4 *
5 *****************************************************************************/
6
7/*
Len Brown75a44ce2008-04-23 23:00:13 -04008 * Copyright (C) 2000 - 2008, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Bob Moore50df4d82008-12-31 03:01:23 +080045#include <acpi/accommon.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070046#include <acpi/amlcode.h>
47#include <acpi/acdispat.h>
48#include <acpi/acinterp.h>
49#include <acpi/acnamesp.h>
Bob Mooredefba1d2005-12-16 17:05:00 -050050#include <acpi/acdisasm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051
Linus Torvalds1da177e2005-04-16 15:20:36 -070052#define _COMPONENT ACPI_DISPATCHER
Len Brown4be44fc2005-08-05 00:44:28 -040053ACPI_MODULE_NAME("dsmethod")
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
Bob Moore967440e2006-06-23 17:04:00 -040055/* Local prototypes */
56static acpi_status
57acpi_ds_create_method_mutex(union acpi_operand_object *method_desc);
58
Linus Torvalds1da177e2005-04-16 15:20:36 -070059/*******************************************************************************
60 *
Bob Mooredefba1d2005-12-16 17:05:00 -050061 * FUNCTION: acpi_ds_method_error
62 *
63 * PARAMETERS: Status - Execution status
64 * walk_state - Current state
65 *
66 * RETURN: Status
67 *
68 * DESCRIPTION: Called on method error. Invoke the global exception handler if
69 * present, dump the method data if the disassembler is configured
70 *
71 * Note: Allows the exception handler to change the status code
72 *
73 ******************************************************************************/
Bob Moore967440e2006-06-23 17:04:00 -040074
Bob Mooredefba1d2005-12-16 17:05:00 -050075acpi_status
76acpi_ds_method_error(acpi_status status, struct acpi_walk_state *walk_state)
77{
78 ACPI_FUNCTION_ENTRY();
79
80 /* Ignore AE_OK and control exception codes */
81
82 if (ACPI_SUCCESS(status) || (status & AE_CODE_CONTROL)) {
83 return (status);
84 }
85
86 /* Invoke the global exception handler */
87
88 if (acpi_gbl_exception_handler) {
Bob Moore52fc0b02006-10-02 00:00:00 -040089
Bob Mooredefba1d2005-12-16 17:05:00 -050090 /* Exit the interpreter, allow handler to execute methods */
91
92 acpi_ex_exit_interpreter();
93
94 /*
95 * Handler can map the exception code to anything it wants, including
96 * AE_OK, in which case the executing method will not be aborted.
97 */
98 status = acpi_gbl_exception_handler(status,
99 walk_state->method_node ?
100 walk_state->method_node->
101 name.integer : 0,
102 walk_state->opcode,
103 walk_state->aml_offset,
104 NULL);
Bob Mooreb7f9f042008-04-10 19:06:40 +0400105 acpi_ex_enter_interpreter();
Bob Mooredefba1d2005-12-16 17:05:00 -0500106 }
Lin Mingd8a0ec92008-09-27 11:50:24 +0800107
108 acpi_ds_clear_implicit_return(walk_state);
109
Bob Mooredefba1d2005-12-16 17:05:00 -0500110#ifdef ACPI_DISASSEMBLER
111 if (ACPI_FAILURE(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400112
Bob Mooredefba1d2005-12-16 17:05:00 -0500113 /* Display method locals/args if disassembler is present */
114
115 acpi_dm_dump_method_info(status, walk_state, walk_state->op);
116 }
117#endif
118
119 return (status);
120}
121
122/*******************************************************************************
123 *
Bob Moore967440e2006-06-23 17:04:00 -0400124 * FUNCTION: acpi_ds_create_method_mutex
125 *
126 * PARAMETERS: obj_desc - The method object
127 *
128 * RETURN: Status
129 *
130 * DESCRIPTION: Create a mutex object for a serialized control method
131 *
132 ******************************************************************************/
133
134static acpi_status
135acpi_ds_create_method_mutex(union acpi_operand_object *method_desc)
136{
137 union acpi_operand_object *mutex_desc;
138 acpi_status status;
139
Bob Mooref6dd9222006-07-07 20:44:38 -0400140 ACPI_FUNCTION_TRACE(ds_create_method_mutex);
Bob Moore967440e2006-06-23 17:04:00 -0400141
142 /* Create the new mutex object */
143
144 mutex_desc = acpi_ut_create_internal_object(ACPI_TYPE_MUTEX);
145 if (!mutex_desc) {
146 return_ACPI_STATUS(AE_NO_MEMORY);
147 }
148
149 /* Create the actual OS Mutex */
150
151 status = acpi_os_create_mutex(&mutex_desc->mutex.os_mutex);
152 if (ACPI_FAILURE(status)) {
153 return_ACPI_STATUS(status);
154 }
155
156 mutex_desc->mutex.sync_level = method_desc->method.sync_level;
157 method_desc->method.mutex = mutex_desc;
158 return_ACPI_STATUS(AE_OK);
159}
160
161/*******************************************************************************
162 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 * FUNCTION: acpi_ds_begin_method_execution
164 *
165 * PARAMETERS: method_node - Node of the method
166 * obj_desc - The method object
Bob Moore967440e2006-06-23 17:04:00 -0400167 * walk_state - current state, NULL if not yet executing
168 * a method.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 *
170 * RETURN: Status
171 *
172 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
173 * increments the thread count, and waits at the method semaphore
174 * for clearance to execute.
175 *
176 ******************************************************************************/
Bob Mooredefba1d2005-12-16 17:05:00 -0500177
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178acpi_status
Bob Moore967440e2006-06-23 17:04:00 -0400179acpi_ds_begin_method_execution(struct acpi_namespace_node *method_node,
180 union acpi_operand_object *obj_desc,
181 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182{
Len Brown4be44fc2005-08-05 00:44:28 -0400183 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184
Bob Mooreb229cf92006-04-21 17:15:00 -0400185 ACPI_FUNCTION_TRACE_PTR(ds_begin_method_execution, method_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
187 if (!method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400188 return_ACPI_STATUS(AE_NULL_ENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
Robert Mooreaff8c272005-09-02 17:24:17 -0400191 /* Prevent wraparound of thread count */
192
193 if (obj_desc->method.thread_count == ACPI_UINT8_MAX) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500194 ACPI_ERROR((AE_INFO,
195 "Method reached maximum reentrancy limit (255)"));
Robert Mooreaff8c272005-09-02 17:24:17 -0400196 return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
197 }
198
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 /*
Bob Moore967440e2006-06-23 17:04:00 -0400200 * If this method is serialized, we need to acquire the method mutex.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 */
Bob Moore967440e2006-06-23 17:04:00 -0400202 if (obj_desc->method.method_flags & AML_METHOD_SERIALIZED) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 /*
Bob Moore967440e2006-06-23 17:04:00 -0400204 * Create a mutex for the method if it is defined to be Serialized
205 * and a mutex has not already been created. We defer the mutex creation
206 * until a method is actually executed, to minimize the object count
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 */
Bob Moore967440e2006-06-23 17:04:00 -0400208 if (!obj_desc->method.mutex) {
209 status = acpi_ds_create_method_mutex(obj_desc);
210 if (ACPI_FAILURE(status)) {
211 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212 }
213 }
214
215 /*
Bob Moore967440e2006-06-23 17:04:00 -0400216 * The current_sync_level (per-thread) must be less than or equal to
217 * the sync level of the method. This mechanism provides some
218 * deadlock prevention
219 *
220 * Top-level method invocation has no walk state at this point
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 */
Bob Moore967440e2006-06-23 17:04:00 -0400222 if (walk_state &&
223 (walk_state->thread->current_sync_level >
224 obj_desc->method.mutex->mutex.sync_level)) {
225 ACPI_ERROR((AE_INFO,
226 "Cannot acquire Mutex for method [%4.4s], current SyncLevel is too large (%d)",
227 acpi_ut_get_node_name(method_node),
228 walk_state->thread->current_sync_level));
229
230 return_ACPI_STATUS(AE_AML_MUTEX_ORDER);
Bob Mooreb229cf92006-04-21 17:15:00 -0400231 }
Bob Moore967440e2006-06-23 17:04:00 -0400232
233 /*
234 * Obtain the method mutex if necessary. Do not acquire mutex for a
235 * recursive call.
236 */
Len Brown262a7a22007-05-09 23:01:59 -0400237 if (!walk_state ||
Bob Mooreba886cd2008-04-10 19:06:37 +0400238 !obj_desc->method.mutex->mutex.thread_id ||
239 (walk_state->thread->thread_id !=
240 obj_desc->method.mutex->mutex.thread_id)) {
Bob Moore967440e2006-06-23 17:04:00 -0400241 /*
242 * Acquire the method mutex. This releases the interpreter if we
243 * block (and reacquires it before it returns)
244 */
245 status =
246 acpi_ex_system_wait_mutex(obj_desc->method.mutex->
247 mutex.os_mutex,
248 ACPI_WAIT_FOREVER);
249 if (ACPI_FAILURE(status)) {
250 return_ACPI_STATUS(status);
251 }
252
253 /* Update the mutex and walk info and save the original sync_level */
254
255 if (walk_state) {
256 obj_desc->method.mutex->mutex.
257 original_sync_level =
258 walk_state->thread->current_sync_level;
259
Bob Mooreba886cd2008-04-10 19:06:37 +0400260 obj_desc->method.mutex->mutex.thread_id =
261 walk_state->thread->thread_id;
Bob Moore967440e2006-06-23 17:04:00 -0400262 walk_state->thread->current_sync_level =
263 obj_desc->method.sync_level;
264 } else {
265 obj_desc->method.mutex->mutex.
266 original_sync_level =
267 obj_desc->method.mutex->mutex.sync_level;
268 }
269 }
270
271 /* Always increase acquisition depth */
272
273 obj_desc->method.mutex->mutex.acquisition_depth++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700274 }
275
276 /*
Robert Mooreaff8c272005-09-02 17:24:17 -0400277 * Allocate an Owner ID for this method, only if this is the first thread
278 * to begin concurrent execution. We only need one owner_id, even if the
279 * method is invoked recursively.
280 */
281 if (!obj_desc->method.owner_id) {
282 status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
283 if (ACPI_FAILURE(status)) {
Bob Mooreb229cf92006-04-21 17:15:00 -0400284 goto cleanup;
Robert Mooreaff8c272005-09-02 17:24:17 -0400285 }
286 }
287
288 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700289 * Increment the method parse tree thread count since it has been
290 * reentered one more time (even if it is the same thread)
291 */
292 obj_desc->method.thread_count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400293 return_ACPI_STATUS(status);
Bob Mooreb229cf92006-04-21 17:15:00 -0400294
295 cleanup:
Bob Moore967440e2006-06-23 17:04:00 -0400296 /* On error, must release the method mutex (if present) */
Bob Mooreb229cf92006-04-21 17:15:00 -0400297
Bob Moore967440e2006-06-23 17:04:00 -0400298 if (obj_desc->method.mutex) {
299 acpi_os_release_mutex(obj_desc->method.mutex->mutex.os_mutex);
Bob Mooreb229cf92006-04-21 17:15:00 -0400300 }
301 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302}
303
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304/*******************************************************************************
305 *
306 * FUNCTION: acpi_ds_call_control_method
307 *
308 * PARAMETERS: Thread - Info for this thread
309 * this_walk_state - Current walk state
310 * Op - Current Op to be walked
311 *
312 * RETURN: Status
313 *
314 * DESCRIPTION: Transfer execution to a called control method
315 *
316 ******************************************************************************/
317
318acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400319acpi_ds_call_control_method(struct acpi_thread_state *thread,
320 struct acpi_walk_state *this_walk_state,
321 union acpi_parse_object *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322{
Len Brown4be44fc2005-08-05 00:44:28 -0400323 acpi_status status;
324 struct acpi_namespace_node *method_node;
325 struct acpi_walk_state *next_walk_state = NULL;
326 union acpi_operand_object *obj_desc;
Bob Moore41195322006-05-26 16:36:00 -0400327 struct acpi_evaluate_info *info;
Len Brown4be44fc2005-08-05 00:44:28 -0400328 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329
Bob Mooreb229cf92006-04-21 17:15:00 -0400330 ACPI_FUNCTION_TRACE_PTR(ds_call_control_method, this_walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
Len Brown4be44fc2005-08-05 00:44:28 -0400332 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Valery Podrezov9bc75cf2007-02-02 19:48:21 +0300333 "Calling method %p, currentstate=%p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400334 this_walk_state->prev_op, this_walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335
336 /*
337 * Get the namespace entry for the control method we are about to call
338 */
339 method_node = this_walk_state->method_call_node;
340 if (!method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400341 return_ACPI_STATUS(AE_NULL_ENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342 }
343
Len Brown4be44fc2005-08-05 00:44:28 -0400344 obj_desc = acpi_ns_get_attached_object(method_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400346 return_ACPI_STATUS(AE_NULL_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 }
348
Bob Moore967440e2006-06-23 17:04:00 -0400349 /* Init for new method, possibly wait on method mutex */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350
Len Brown4be44fc2005-08-05 00:44:28 -0400351 status = acpi_ds_begin_method_execution(method_node, obj_desc,
Bob Moore967440e2006-06-23 17:04:00 -0400352 this_walk_state);
Len Brown4be44fc2005-08-05 00:44:28 -0400353 if (ACPI_FAILURE(status)) {
Bob Mooreb229cf92006-04-21 17:15:00 -0400354 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 }
356
Valery Podrezov9bc75cf2007-02-02 19:48:21 +0300357 /* Begin method parse/execution. Create a new walk state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358
Len Brown4be44fc2005-08-05 00:44:28 -0400359 next_walk_state = acpi_ds_create_walk_state(obj_desc->method.owner_id,
360 NULL, obj_desc, thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 if (!next_walk_state) {
362 status = AE_NO_MEMORY;
363 goto cleanup;
364 }
Bob Mooreb229cf92006-04-21 17:15:00 -0400365
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 /*
367 * The resolved arguments were put on the previous walk state's operand
Robert Mooreaff8c272005-09-02 17:24:17 -0400368 * stack. Operands on the previous walk state stack always
369 * start at index 0. Also, null terminate the list of arguments
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370 */
Len Brown4be44fc2005-08-05 00:44:28 -0400371 this_walk_state->operands[this_walk_state->num_operands] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372
Bob Moore41195322006-05-26 16:36:00 -0400373 /*
374 * Allocate and initialize the evaluation information block
375 * TBD: this is somewhat inefficient, should change interface to
376 * ds_init_aml_walk. For now, keeps this struct off the CPU stack
377 */
378 info = ACPI_ALLOCATE_ZEROED(sizeof(struct acpi_evaluate_info));
379 if (!info) {
380 return_ACPI_STATUS(AE_NO_MEMORY);
381 }
382
383 info->parameters = &this_walk_state->operands[0];
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384
Len Brown4be44fc2005-08-05 00:44:28 -0400385 status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
386 obj_desc->method.aml_start,
Bob Mooreec3153f2007-02-02 19:48:21 +0300387 obj_desc->method.aml_length, info,
388 ACPI_IMODE_EXECUTE);
Bob Moore41195322006-05-26 16:36:00 -0400389
390 ACPI_FREE(info);
Len Brown4be44fc2005-08-05 00:44:28 -0400391 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 goto cleanup;
393 }
394
395 /*
396 * Delete the operands on the previous walkstate operand stack
397 * (they were copied to new objects)
398 */
399 for (i = 0; i < obj_desc->method.param_count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400400 acpi_ut_remove_reference(this_walk_state->operands[i]);
401 this_walk_state->operands[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 }
403
404 /* Clear the operand stack */
405
406 this_walk_state->num_operands = 0;
407
Len Brown4be44fc2005-08-05 00:44:28 -0400408 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Valery Podrezov9bc75cf2007-02-02 19:48:21 +0300409 "**** Begin nested execution of [%4.4s] **** WalkState=%p\n",
410 method_node->name.ascii, next_walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411
Bob Mooreb229cf92006-04-21 17:15:00 -0400412 /* Invoke an internal method if necessary */
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400415 status = obj_desc->method.implementation(next_walk_state);
Bob Moorea8fadc92008-11-13 09:45:35 +0800416 if (status == AE_OK) {
417 status = AE_CTRL_TERMINATE;
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 }
420
Robert Mooreaff8c272005-09-02 17:24:17 -0400421 return_ACPI_STATUS(status);
422
423 cleanup:
Robert Mooreaff8c272005-09-02 17:24:17 -0400424
Bob Mooreb229cf92006-04-21 17:15:00 -0400425 /* On error, we must terminate the method properly */
426
427 acpi_ds_terminate_control_method(obj_desc, next_walk_state);
428 if (next_walk_state) {
429 acpi_ds_delete_walk_state(next_walk_state);
Robert Mooreaff8c272005-09-02 17:24:17 -0400430 }
Len Browna94f1882005-09-03 00:09:12 -0400431
Len Brown4be44fc2005-08-05 00:44:28 -0400432 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433}
434
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435/*******************************************************************************
436 *
437 * FUNCTION: acpi_ds_restart_control_method
438 *
439 * PARAMETERS: walk_state - State for preempted method (caller)
440 * return_desc - Return value from the called method
441 *
442 * RETURN: Status
443 *
444 * DESCRIPTION: Restart a method that was preempted by another (nested) method
445 * invocation. Handle the return value (if any) from the callee.
446 *
447 ******************************************************************************/
448
449acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400450acpi_ds_restart_control_method(struct acpi_walk_state *walk_state,
451 union acpi_operand_object *return_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452{
Len Brown4be44fc2005-08-05 00:44:28 -0400453 acpi_status status;
Bob Moore958dd242006-05-12 17:12:00 -0400454 int same_as_implicit_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Bob Mooreb229cf92006-04-21 17:15:00 -0400456 ACPI_FUNCTION_TRACE_PTR(ds_restart_control_method, walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457
Len Brown4be44fc2005-08-05 00:44:28 -0400458 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Bob Mooreb229cf92006-04-21 17:15:00 -0400459 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
Bob Mooref6dd9222006-07-07 20:44:38 -0400460 acpi_ut_get_node_name(walk_state->method_node),
Len Brown4be44fc2005-08-05 00:44:28 -0400461 walk_state->method_call_op, return_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Len Brown4be44fc2005-08-05 00:44:28 -0400463 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Bob Mooreb229cf92006-04-21 17:15:00 -0400464 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400465 walk_state->return_used,
466 walk_state->results, walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
468 /* Did the called method return a value? */
469
470 if (return_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400471
Bob Moore958dd242006-05-12 17:12:00 -0400472 /* Is the implicit return object the same as the return desc? */
473
474 same_as_implicit_return =
475 (walk_state->implicit_return_obj == return_desc);
476
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /* Are we actually going to use the return value? */
478
479 if (walk_state->return_used) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400480
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 /* Save the return value from the previous method */
482
Len Brown4be44fc2005-08-05 00:44:28 -0400483 status = acpi_ds_result_push(return_desc, walk_state);
484 if (ACPI_FAILURE(status)) {
485 acpi_ut_remove_reference(return_desc);
486 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
489 /*
490 * Save as THIS method's return value in case it is returned
491 * immediately to yet another method
492 */
493 walk_state->return_desc = return_desc;
494 }
495
496 /*
Bob Moore958dd242006-05-12 17:12:00 -0400497 * The following code is the optional support for the so-called
498 * "implicit return". Some AML code assumes that the last value of the
499 * method is "implicitly" returned to the caller, in the absence of an
500 * explicit return value.
501 *
502 * Just save the last result of the method as the return value.
503 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 * NOTE: this is optional because the ASL language does not actually
505 * support this behavior.
506 */
Len Brown4be44fc2005-08-05 00:44:28 -0400507 else if (!acpi_ds_do_implicit_return
Bob Moore958dd242006-05-12 17:12:00 -0400508 (return_desc, walk_state, FALSE)
509 || same_as_implicit_return) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 /*
511 * Delete the return value if it will not be used by the
Bob Moore958dd242006-05-12 17:12:00 -0400512 * calling method or remove one reference if the explicit return
513 * is the same as the implicit return value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 */
Len Brown4be44fc2005-08-05 00:44:28 -0400515 acpi_ut_remove_reference(return_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 }
517 }
518
Len Brown4be44fc2005-08-05 00:44:28 -0400519 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520}
521
Linus Torvalds1da177e2005-04-16 15:20:36 -0700522/*******************************************************************************
523 *
524 * FUNCTION: acpi_ds_terminate_control_method
525 *
Bob Mooreb229cf92006-04-21 17:15:00 -0400526 * PARAMETERS: method_desc - Method object
527 * walk_state - State associated with the method
Linus Torvalds1da177e2005-04-16 15:20:36 -0700528 *
Robert Mooreaff8c272005-09-02 17:24:17 -0400529 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 *
531 * DESCRIPTION: Terminate a control method. Delete everything that the method
532 * created, delete all locals and arguments, and delete the parse
533 * tree if requested.
534 *
Bob Moore967440e2006-06-23 17:04:00 -0400535 * MUTEX: Interpreter is locked
536 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 ******************************************************************************/
538
Bob Mooreb229cf92006-04-21 17:15:00 -0400539void
540acpi_ds_terminate_control_method(union acpi_operand_object *method_desc,
541 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
Bob Mooreb229cf92006-04-21 17:15:00 -0400544 ACPI_FUNCTION_TRACE_PTR(ds_terminate_control_method, walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545
Bob Mooreb229cf92006-04-21 17:15:00 -0400546 /* method_desc is required, walk_state is optional */
547
548 if (!method_desc) {
Robert Mooreaff8c272005-09-02 17:24:17 -0400549 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 }
551
Bob Mooreb229cf92006-04-21 17:15:00 -0400552 if (walk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
Bob Mooreb229cf92006-04-21 17:15:00 -0400554 /* Delete all arguments and locals */
555
556 acpi_ds_method_data_delete_all(walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Bob Mooreb7f9f042008-04-10 19:06:40 +0400558 /*
559 * If method is serialized, release the mutex and restore the
560 * current sync level for this thread
561 */
562 if (method_desc->method.mutex) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Bob Mooreb7f9f042008-04-10 19:06:40 +0400564 /* Acquisition Depth handles recursive calls */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
Bob Mooreb7f9f042008-04-10 19:06:40 +0400566 method_desc->method.mutex->mutex.acquisition_depth--;
567 if (!method_desc->method.mutex->mutex.acquisition_depth) {
568 walk_state->thread->current_sync_level =
569 method_desc->method.mutex->mutex.
570 original_sync_level;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571
Bob Mooreb7f9f042008-04-10 19:06:40 +0400572 acpi_os_release_mutex(method_desc->method.
573 mutex->mutex.os_mutex);
Harvey Harrisonb62151d2008-05-22 15:45:06 -0700574 method_desc->method.mutex->mutex.thread_id = NULL;
Bob Mooreb7f9f042008-04-10 19:06:40 +0400575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577
Bob Mooreb229cf92006-04-21 17:15:00 -0400578 /*
Bob Mooref6dd9222006-07-07 20:44:38 -0400579 * Delete any namespace objects created anywhere within
Bob Mooreb229cf92006-04-21 17:15:00 -0400580 * the namespace by the execution of this method
581 */
582 acpi_ns_delete_namespace_by_owner(method_desc->method.owner_id);
Bob Moore28f55eb2005-12-02 18:27:00 -0500583 }
584
Bob Mooreb229cf92006-04-21 17:15:00 -0400585 /* Decrement the thread count on the method */
Bob Moore28f55eb2005-12-02 18:27:00 -0500586
Bob Mooreb229cf92006-04-21 17:15:00 -0400587 if (method_desc->method.thread_count) {
588 method_desc->method.thread_count--;
589 } else {
590 ACPI_ERROR((AE_INFO, "Invalid zero thread count in method"));
591 }
Bob Moore28f55eb2005-12-02 18:27:00 -0500592
593 /* Are there any other threads currently executing this method? */
594
Bob Mooreb229cf92006-04-21 17:15:00 -0400595 if (method_desc->method.thread_count) {
Bob Moore28f55eb2005-12-02 18:27:00 -0500596 /*
597 * Additional threads. Do not release the owner_id in this case,
598 * we immediately reuse it for the next thread executing this method
599 */
Len Brown4be44fc2005-08-05 00:44:28 -0400600 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Bob Moore28f55eb2005-12-02 18:27:00 -0500601 "*** Completed execution of one thread, %d threads remaining\n",
Bob Mooreb229cf92006-04-21 17:15:00 -0400602 method_desc->method.thread_count));
Bob Moore28f55eb2005-12-02 18:27:00 -0500603 } else {
604 /* This is the only executing thread for this method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 /*
607 * Support to dynamically change a method from not_serialized to
Bob Moore28f55eb2005-12-02 18:27:00 -0500608 * Serialized if it appears that the method is incorrectly written and
Bob Moore967440e2006-06-23 17:04:00 -0400609 * does not support multiple thread execution. The best example of this
610 * is if such a method creates namespace objects and blocks. A second
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611 * thread will fail with an AE_ALREADY_EXISTS exception
612 *
613 * This code is here because we must wait until the last thread exits
614 * before creating the synchronization semaphore.
615 */
Bob Moore967440e2006-06-23 17:04:00 -0400616 if ((method_desc->method.method_flags & AML_METHOD_SERIALIZED)
617 && (!method_desc->method.mutex)) {
Bob Mooreb7f9f042008-04-10 19:06:40 +0400618 (void)acpi_ds_create_method_mutex(method_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700619 }
620
Bob Moore28f55eb2005-12-02 18:27:00 -0500621 /* No more threads, we can free the owner_id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Bob Mooreb229cf92006-04-21 17:15:00 -0400623 acpi_ut_release_owner_id(&method_desc->method.owner_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624 }
Len Browna94f1882005-09-03 00:09:12 -0400625
Robert Mooreaff8c272005-09-02 17:24:17 -0400626 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627}