blob: 36c1ca0b9adb71a9b2d19fddd711a4b545a45312 [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/*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * 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>
45#include <acpi/acparser.h>
46#include <acpi/amlcode.h>
47#include <acpi/acdispat.h>
48#include <acpi/acinterp.h>
49#include <acpi/acnamesp.h>
50
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#define _COMPONENT ACPI_DISPATCHER
Len Brown4be44fc2005-08-05 00:44:28 -040052ACPI_MODULE_NAME("dsmethod")
Linus Torvalds1da177e2005-04-16 15:20:36 -070053
54/*******************************************************************************
55 *
56 * FUNCTION: acpi_ds_parse_method
57 *
Robert Moore0c9938c2005-07-29 15:15:00 -070058 * PARAMETERS: Node - Method node
Linus Torvalds1da177e2005-04-16 15:20:36 -070059 *
60 * RETURN: Status
61 *
Robert Moore0c9938c2005-07-29 15:15:00 -070062 * DESCRIPTION: Parse the AML that is associated with the method.
Linus Torvalds1da177e2005-04-16 15:20:36 -070063 *
64 * MUTEX: Assumes parser is locked
65 *
66 ******************************************************************************/
Len Brown4be44fc2005-08-05 00:44:28 -040067acpi_status acpi_ds_parse_method(struct acpi_namespace_node *node)
Linus Torvalds1da177e2005-04-16 15:20:36 -070068{
Len Brown4be44fc2005-08-05 00:44:28 -040069 acpi_status status;
70 union acpi_operand_object *obj_desc;
71 union acpi_parse_object *op;
72 struct acpi_walk_state *walk_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -070073
Len Brown4be44fc2005-08-05 00:44:28 -040074 ACPI_FUNCTION_TRACE_PTR("ds_parse_method", node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 /* Parameter Validation */
77
Robert Moore0c9938c2005-07-29 15:15:00 -070078 if (!node) {
Len Brown4be44fc2005-08-05 00:44:28 -040079 return_ACPI_STATUS(AE_NULL_ENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 }
81
Len Brown4be44fc2005-08-05 00:44:28 -040082 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
83 "**** Parsing [%4.4s] **** named_obj=%p\n",
84 acpi_ut_get_node_name(node), node));
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86 /* Extract the method object from the method Node */
87
Len Brown4be44fc2005-08-05 00:44:28 -040088 obj_desc = acpi_ns_get_attached_object(node);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -040090 return_ACPI_STATUS(AE_NULL_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091 }
92
93 /* Create a mutex for the method if there is a concurrency limit */
94
95 if ((obj_desc->method.concurrency != ACPI_INFINITE_CONCURRENCY) &&
Len Brown4be44fc2005-08-05 00:44:28 -040096 (!obj_desc->method.semaphore)) {
97 status = acpi_os_create_semaphore(obj_desc->method.concurrency,
98 obj_desc->method.concurrency,
99 &obj_desc->method.semaphore);
100 if (ACPI_FAILURE(status)) {
101 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103 }
104
105 /*
106 * Allocate a new parser op to be the root of the parsed
107 * method tree
108 */
Len Brown4be44fc2005-08-05 00:44:28 -0400109 op = acpi_ps_alloc_op(AML_METHOD_OP);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110 if (!op) {
Len Brown4be44fc2005-08-05 00:44:28 -0400111 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
114 /* Init new op with the method name and pointer back to the Node */
115
Len Brown4be44fc2005-08-05 00:44:28 -0400116 acpi_ps_set_name(op, node->name.integer);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 op->common.node = node;
118
119 /*
120 * Get a new owner_id for objects created by this method. Namespace
121 * objects (such as Operation Regions) can be created during the
122 * first pass parse.
123 */
Len Brown4be44fc2005-08-05 00:44:28 -0400124 status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
125 if (ACPI_FAILURE(status)) {
Robert Mooref9f46012005-07-08 00:00:00 -0400126 goto cleanup;
127 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128
129 /* Create and initialize a new walk state */
130
Len Brown4be44fc2005-08-05 00:44:28 -0400131 walk_state =
132 acpi_ds_create_walk_state(obj_desc->method.owner_id, NULL, NULL,
133 NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 if (!walk_state) {
Robert Moore88ac00f2005-05-26 00:00:00 -0400135 status = AE_NO_MEMORY;
Robert Mooref9f46012005-07-08 00:00:00 -0400136 goto cleanup2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 }
138
Len Brown4be44fc2005-08-05 00:44:28 -0400139 status = acpi_ds_init_aml_walk(walk_state, op, node,
140 obj_desc->method.aml_start,
141 obj_desc->method.aml_length, NULL, 1);
142 if (ACPI_FAILURE(status)) {
143 acpi_ds_delete_walk_state(walk_state);
Robert Mooref9f46012005-07-08 00:00:00 -0400144 goto cleanup2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 }
146
147 /*
148 * Parse the method, first pass
149 *
Robert Moore44f6c012005-04-18 22:49:35 -0400150 * The first pass load is where newly declared named objects are added into
151 * the namespace. Actual evaluation of the named objects (what would be
152 * called a "second pass") happens during the actual execution of the
153 * method so that operands to the named objects can take on dynamic
154 * run-time values.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 */
Len Brown4be44fc2005-08-05 00:44:28 -0400156 status = acpi_ps_parse_aml(walk_state);
157 if (ACPI_FAILURE(status)) {
Robert Mooref9f46012005-07-08 00:00:00 -0400158 goto cleanup2;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 }
160
Len Brown4be44fc2005-08-05 00:44:28 -0400161 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
162 "**** [%4.4s] Parsed **** named_obj=%p Op=%p\n",
163 acpi_ut_get_node_name(node), node, op));
Robert Moore0c9938c2005-07-29 15:15:00 -0700164
165 /*
166 * Delete the parse tree. We simply re-parse the method for every
167 * execution since there isn't much overhead (compared to keeping lots
168 * of parse trees around)
169 */
Len Brown4be44fc2005-08-05 00:44:28 -0400170 acpi_ns_delete_namespace_subtree(node);
171 acpi_ns_delete_namespace_by_owner(obj_desc->method.owner_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Len Brown4be44fc2005-08-05 00:44:28 -0400173 cleanup2:
174 acpi_ut_release_owner_id(&obj_desc->method.owner_id);
Robert Mooref9f46012005-07-08 00:00:00 -0400175
Len Brown4be44fc2005-08-05 00:44:28 -0400176 cleanup:
177 acpi_ps_delete_parse_tree(op);
178 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179}
180
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181/*******************************************************************************
182 *
183 * FUNCTION: acpi_ds_begin_method_execution
184 *
185 * PARAMETERS: method_node - Node of the method
186 * obj_desc - The method object
187 * calling_method_node - Caller of this method (if non-null)
188 *
189 * RETURN: Status
190 *
191 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
192 * increments the thread count, and waits at the method semaphore
193 * for clearance to execute.
194 *
195 ******************************************************************************/
196
197acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400198acpi_ds_begin_method_execution(struct acpi_namespace_node *method_node,
199 union acpi_operand_object *obj_desc,
200 struct acpi_namespace_node *calling_method_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Len Brown4be44fc2005-08-05 00:44:28 -0400202 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203
Len Brown4be44fc2005-08-05 00:44:28 -0400204 ACPI_FUNCTION_TRACE_PTR("ds_begin_method_execution", method_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
206 if (!method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400207 return_ACPI_STATUS(AE_NULL_ENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208 }
209
Robert Mooreaff8c272005-09-02 17:24:17 -0400210 /* Prevent wraparound of thread count */
211
212 if (obj_desc->method.thread_count == ACPI_UINT8_MAX) {
213 ACPI_REPORT_ERROR(("Method reached maximum reentrancy limit (255)\n"));
214 return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
215 }
216
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217 /*
218 * If there is a concurrency limit on this method, we need to
219 * obtain a unit from the method semaphore.
220 */
221 if (obj_desc->method.semaphore) {
222 /*
223 * Allow recursive method calls, up to the reentrancy/concurrency
224 * limit imposed by the SERIALIZED rule and the sync_level method
225 * parameter.
226 *
227 * The point of this code is to avoid permanently blocking a
228 * thread that is making recursive method calls.
229 */
230 if (method_node == calling_method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400231 if (obj_desc->method.thread_count >=
232 obj_desc->method.concurrency) {
233 return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700234 }
235 }
236
237 /*
238 * Get a unit from the method semaphore. This releases the
239 * interpreter if we block
240 */
Len Brown4be44fc2005-08-05 00:44:28 -0400241 status =
242 acpi_ex_system_wait_semaphore(obj_desc->method.semaphore,
243 ACPI_WAIT_FOREVER);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700244 }
245
246 /*
Robert Mooreaff8c272005-09-02 17:24:17 -0400247 * Allocate an Owner ID for this method, only if this is the first thread
248 * to begin concurrent execution. We only need one owner_id, even if the
249 * method is invoked recursively.
250 */
251 if (!obj_desc->method.owner_id) {
252 status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
253 if (ACPI_FAILURE(status)) {
254 return_ACPI_STATUS(status);
255 }
256 }
257
258 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259 * Increment the method parse tree thread count since it has been
260 * reentered one more time (even if it is the same thread)
261 */
262 obj_desc->method.thread_count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400263 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264}
265
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266/*******************************************************************************
267 *
268 * FUNCTION: acpi_ds_call_control_method
269 *
270 * PARAMETERS: Thread - Info for this thread
271 * this_walk_state - Current walk state
272 * Op - Current Op to be walked
273 *
274 * RETURN: Status
275 *
276 * DESCRIPTION: Transfer execution to a called control method
277 *
278 ******************************************************************************/
279
280acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400281acpi_ds_call_control_method(struct acpi_thread_state *thread,
282 struct acpi_walk_state *this_walk_state,
283 union acpi_parse_object *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284{
Len Brown4be44fc2005-08-05 00:44:28 -0400285 acpi_status status;
286 struct acpi_namespace_node *method_node;
287 struct acpi_walk_state *next_walk_state = NULL;
288 union acpi_operand_object *obj_desc;
289 struct acpi_parameter_info info;
290 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291
Len Brown4be44fc2005-08-05 00:44:28 -0400292 ACPI_FUNCTION_TRACE_PTR("ds_call_control_method", this_walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700293
Len Brown4be44fc2005-08-05 00:44:28 -0400294 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
295 "Execute method %p, currentstate=%p\n",
296 this_walk_state->prev_op, this_walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297
298 /*
299 * Get the namespace entry for the control method we are about to call
300 */
301 method_node = this_walk_state->method_call_node;
302 if (!method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400303 return_ACPI_STATUS(AE_NULL_ENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Len Brown4be44fc2005-08-05 00:44:28 -0400306 obj_desc = acpi_ns_get_attached_object(method_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400308 return_ACPI_STATUS(AE_NULL_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700309 }
310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 /* Init for new method, wait on concurrency semaphore */
312
Len Brown4be44fc2005-08-05 00:44:28 -0400313 status = acpi_ds_begin_method_execution(method_node, obj_desc,
314 this_walk_state->method_node);
315 if (ACPI_FAILURE(status)) {
Robert Mooref9f46012005-07-08 00:00:00 -0400316 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700317 }
318
319 if (!(obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY)) {
320 /* 1) Parse: Create a new walk state for the preempting walk */
321
Len Brown4be44fc2005-08-05 00:44:28 -0400322 next_walk_state =
323 acpi_ds_create_walk_state(obj_desc->method.owner_id, op,
324 obj_desc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700325 if (!next_walk_state) {
Len Brown4be44fc2005-08-05 00:44:28 -0400326 return_ACPI_STATUS(AE_NO_MEMORY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700327 }
328
329 /* Create and init a Root Node */
330
Len Brown4be44fc2005-08-05 00:44:28 -0400331 op = acpi_ps_create_scope_op();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 if (!op) {
333 status = AE_NO_MEMORY;
334 goto cleanup;
335 }
336
Len Brown4be44fc2005-08-05 00:44:28 -0400337 status = acpi_ds_init_aml_walk(next_walk_state, op, method_node,
338 obj_desc->method.aml_start,
339 obj_desc->method.aml_length,
340 NULL, 1);
341 if (ACPI_FAILURE(status)) {
342 acpi_ds_delete_walk_state(next_walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 goto cleanup;
344 }
345
346 /* Begin AML parse */
347
Len Brown4be44fc2005-08-05 00:44:28 -0400348 status = acpi_ps_parse_aml(next_walk_state);
349 acpi_ps_delete_parse_tree(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700350 }
351
352 /* 2) Execute: Create a new state for the preempting walk */
353
Len Brown4be44fc2005-08-05 00:44:28 -0400354 next_walk_state = acpi_ds_create_walk_state(obj_desc->method.owner_id,
355 NULL, obj_desc, thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356 if (!next_walk_state) {
357 status = AE_NO_MEMORY;
358 goto cleanup;
359 }
360 /*
361 * The resolved arguments were put on the previous walk state's operand
Robert Mooreaff8c272005-09-02 17:24:17 -0400362 * stack. Operands on the previous walk state stack always
363 * start at index 0. Also, null terminate the list of arguments
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 */
Len Brown4be44fc2005-08-05 00:44:28 -0400365 this_walk_state->operands[this_walk_state->num_operands] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366
367 info.parameters = &this_walk_state->operands[0];
368 info.parameter_type = ACPI_PARAM_ARGS;
369
Len Brown4be44fc2005-08-05 00:44:28 -0400370 status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
371 obj_desc->method.aml_start,
372 obj_desc->method.aml_length, &info, 3);
373 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 goto cleanup;
375 }
376
377 /*
378 * Delete the operands on the previous walkstate operand stack
379 * (they were copied to new objects)
380 */
381 for (i = 0; i < obj_desc->method.param_count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400382 acpi_ut_remove_reference(this_walk_state->operands[i]);
383 this_walk_state->operands[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 }
385
386 /* Clear the operand stack */
387
388 this_walk_state->num_operands = 0;
389
Len Brown4be44fc2005-08-05 00:44:28 -0400390 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
391 "Starting nested execution, newstate=%p\n",
392 next_walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393
394 if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400395 status = obj_desc->method.implementation(next_walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396 }
397
Robert Mooreaff8c272005-09-02 17:24:17 -0400398 return_ACPI_STATUS(status);
399
400 cleanup:
401 /* Decrement the thread count on the method parse tree */
402
403 if (next_walk_state && (next_walk_state->method_desc)) {
404 next_walk_state->method_desc->method.thread_count--;
405 }
Len Browna94f1882005-09-03 00:09:12 -0400406
407 /* On error, we must delete the new walk state */
408
Robert Mooreaff8c272005-09-02 17:24:17 -0400409 acpi_ds_terminate_control_method(next_walk_state);
Len Browna94f1882005-09-03 00:09:12 -0400410 acpi_ds_delete_walk_state(next_walk_state);
Len Brown4be44fc2005-08-05 00:44:28 -0400411 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700412}
413
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414/*******************************************************************************
415 *
416 * FUNCTION: acpi_ds_restart_control_method
417 *
418 * PARAMETERS: walk_state - State for preempted method (caller)
419 * return_desc - Return value from the called method
420 *
421 * RETURN: Status
422 *
423 * DESCRIPTION: Restart a method that was preempted by another (nested) method
424 * invocation. Handle the return value (if any) from the callee.
425 *
426 ******************************************************************************/
427
428acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400429acpi_ds_restart_control_method(struct acpi_walk_state *walk_state,
430 union acpi_operand_object *return_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431{
Len Brown4be44fc2005-08-05 00:44:28 -0400432 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433
Len Brown4be44fc2005-08-05 00:44:28 -0400434 ACPI_FUNCTION_TRACE_PTR("ds_restart_control_method", walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435
Len Brown4be44fc2005-08-05 00:44:28 -0400436 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
437 "****Restart [%4.4s] Op %p return_value_from_callee %p\n",
438 (char *)&walk_state->method_node->name,
439 walk_state->method_call_op, return_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700440
Len Brown4be44fc2005-08-05 00:44:28 -0400441 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
442 " return_from_this_method_used?=%X res_stack %p Walk %p\n",
443 walk_state->return_used,
444 walk_state->results, walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445
446 /* Did the called method return a value? */
447
448 if (return_desc) {
449 /* Are we actually going to use the return value? */
450
451 if (walk_state->return_used) {
452 /* Save the return value from the previous method */
453
Len Brown4be44fc2005-08-05 00:44:28 -0400454 status = acpi_ds_result_push(return_desc, walk_state);
455 if (ACPI_FAILURE(status)) {
456 acpi_ut_remove_reference(return_desc);
457 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700458 }
459
460 /*
461 * Save as THIS method's return value in case it is returned
462 * immediately to yet another method
463 */
464 walk_state->return_desc = return_desc;
465 }
466
467 /*
468 * The following code is the
469 * optional support for a so-called "implicit return". Some AML code
470 * assumes that the last value of the method is "implicitly" returned
471 * to the caller. Just save the last result as the return value.
472 * NOTE: this is optional because the ASL language does not actually
473 * support this behavior.
474 */
Len Brown4be44fc2005-08-05 00:44:28 -0400475 else if (!acpi_ds_do_implicit_return
476 (return_desc, walk_state, FALSE)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477 /*
478 * Delete the return value if it will not be used by the
479 * calling method
480 */
Len Brown4be44fc2005-08-05 00:44:28 -0400481 acpi_ut_remove_reference(return_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700482 }
483 }
484
Len Brown4be44fc2005-08-05 00:44:28 -0400485 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486}
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/*******************************************************************************
489 *
490 * FUNCTION: acpi_ds_terminate_control_method
491 *
492 * PARAMETERS: walk_state - State of the method
493 *
Robert Mooreaff8c272005-09-02 17:24:17 -0400494 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700495 *
496 * DESCRIPTION: Terminate a control method. Delete everything that the method
497 * created, delete all locals and arguments, and delete the parse
498 * tree if requested.
499 *
500 ******************************************************************************/
501
Robert Mooreaff8c272005-09-02 17:24:17 -0400502void acpi_ds_terminate_control_method(struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503{
Len Brown4be44fc2005-08-05 00:44:28 -0400504 union acpi_operand_object *obj_desc;
505 struct acpi_namespace_node *method_node;
506 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Len Brown4be44fc2005-08-05 00:44:28 -0400508 ACPI_FUNCTION_TRACE_PTR("ds_terminate_control_method", walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
510 if (!walk_state) {
Robert Mooreaff8c272005-09-02 17:24:17 -0400511 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
513
514 /* The current method object was saved in the walk state */
515
516 obj_desc = walk_state->method_desc;
517 if (!obj_desc) {
Robert Mooreaff8c272005-09-02 17:24:17 -0400518 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 }
520
521 /* Delete all arguments and locals */
522
Len Brown4be44fc2005-08-05 00:44:28 -0400523 acpi_ds_method_data_delete_all(walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524
525 /*
526 * Lock the parser while we terminate this method.
527 * If this is the last thread executing the method,
528 * we have additional cleanup to perform
529 */
Len Brown4be44fc2005-08-05 00:44:28 -0400530 status = acpi_ut_acquire_mutex(ACPI_MTX_PARSER);
531 if (ACPI_FAILURE(status)) {
Robert Mooreaff8c272005-09-02 17:24:17 -0400532 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534
535 /* Signal completion of the execution of this method if necessary */
536
537 if (walk_state->method_desc->method.semaphore) {
Len Brown4be44fc2005-08-05 00:44:28 -0400538 status =
539 acpi_os_signal_semaphore(walk_state->method_desc->method.
540 semaphore, 1);
541 if (ACPI_FAILURE(status)) {
542 ACPI_REPORT_ERROR(("Could not signal method semaphore\n"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543
544 /* Ignore error and continue cleanup */
545 }
546 }
547
548 if (walk_state->method_desc->method.thread_count) {
Len Brown4be44fc2005-08-05 00:44:28 -0400549 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
550 "*** Not deleting method namespace, there are still %d threads\n",
551 walk_state->method_desc->method.
552 thread_count));
Robert Mooreaff8c272005-09-02 17:24:17 -0400553 } else { /* This is the last executing thread */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700554
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 /*
556 * Support to dynamically change a method from not_serialized to
557 * Serialized if it appears that the method is written foolishly and
558 * does not support multiple thread execution. The best example of this
559 * is if such a method creates namespace objects and blocks. A second
560 * thread will fail with an AE_ALREADY_EXISTS exception
561 *
562 * This code is here because we must wait until the last thread exits
563 * before creating the synchronization semaphore.
564 */
565 if ((walk_state->method_desc->method.concurrency == 1) &&
Len Brown4be44fc2005-08-05 00:44:28 -0400566 (!walk_state->method_desc->method.semaphore)) {
567 status = acpi_os_create_semaphore(1, 1,
568 &walk_state->
569 method_desc->method.
570 semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 }
572
573 /*
574 * There are no more threads executing this method. Perform
575 * additional cleanup.
576 *
577 * The method Node is stored in the walk state
578 */
579 method_node = walk_state->method_node;
580
581 /*
582 * Delete any namespace entries created immediately underneath
583 * the method
584 */
Len Brown4be44fc2005-08-05 00:44:28 -0400585 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
586 if (ACPI_FAILURE(status)) {
Robert Mooreaff8c272005-09-02 17:24:17 -0400587 goto exit;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
589
590 if (method_node->child) {
Len Brown4be44fc2005-08-05 00:44:28 -0400591 acpi_ns_delete_namespace_subtree(method_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 }
593
594 /*
595 * Delete any namespace entries created anywhere else within
596 * the namespace
597 */
Len Brown4be44fc2005-08-05 00:44:28 -0400598 acpi_ns_delete_namespace_by_owner(walk_state->method_desc->
599 method.owner_id);
600 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
601 acpi_ut_release_owner_id(&walk_state->method_desc->method.
602 owner_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 }
Len Browna94f1882005-09-03 00:09:12 -0400604
Robert Mooreaff8c272005-09-02 17:24:17 -0400605 exit:
606 (void)acpi_ut_release_mutex(ACPI_MTX_PARSER);
607 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608}