blob: e348db0e541e42066b309ee734ae2df90e742f8f [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/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05008 * Copyright (C) 2000 - 2006, R. Byron Moore
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>
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>
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
55/*******************************************************************************
56 *
Bob Mooredefba1d2005-12-16 17:05:00 -050057 * FUNCTION: acpi_ds_method_error
58 *
59 * PARAMETERS: Status - Execution status
60 * walk_state - Current state
61 *
62 * RETURN: Status
63 *
64 * DESCRIPTION: Called on method error. Invoke the global exception handler if
65 * present, dump the method data if the disassembler is configured
66 *
67 * Note: Allows the exception handler to change the status code
68 *
69 ******************************************************************************/
70acpi_status
71acpi_ds_method_error(acpi_status status, struct acpi_walk_state *walk_state)
72{
73 ACPI_FUNCTION_ENTRY();
74
75 /* Ignore AE_OK and control exception codes */
76
77 if (ACPI_SUCCESS(status) || (status & AE_CODE_CONTROL)) {
78 return (status);
79 }
80
81 /* Invoke the global exception handler */
82
83 if (acpi_gbl_exception_handler) {
Bob Moore52fc0b02006-10-02 00:00:00 -040084
Bob Mooredefba1d2005-12-16 17:05:00 -050085 /* Exit the interpreter, allow handler to execute methods */
86
87 acpi_ex_exit_interpreter();
88
89 /*
90 * Handler can map the exception code to anything it wants, including
91 * AE_OK, in which case the executing method will not be aborted.
92 */
93 status = acpi_gbl_exception_handler(status,
94 walk_state->method_node ?
95 walk_state->method_node->
96 name.integer : 0,
97 walk_state->opcode,
98 walk_state->aml_offset,
99 NULL);
100 (void)acpi_ex_enter_interpreter();
101 }
102#ifdef ACPI_DISASSEMBLER
103 if (ACPI_FAILURE(status)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400104
Bob Mooredefba1d2005-12-16 17:05:00 -0500105 /* Display method locals/args if disassembler is present */
106
107 acpi_dm_dump_method_info(status, walk_state, walk_state->op);
108 }
109#endif
110
111 return (status);
112}
113
114/*******************************************************************************
115 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700116 * FUNCTION: acpi_ds_begin_method_execution
117 *
118 * PARAMETERS: method_node - Node of the method
119 * obj_desc - The method object
120 * calling_method_node - Caller of this method (if non-null)
121 *
122 * RETURN: Status
123 *
124 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
125 * increments the thread count, and waits at the method semaphore
126 * for clearance to execute.
127 *
128 ******************************************************************************/
Bob Mooredefba1d2005-12-16 17:05:00 -0500129
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130acpi_status
Bob Mooredefba1d2005-12-16 17:05:00 -0500131acpi_ds_begin_method_execution(struct acpi_namespace_node * method_node,
132 union acpi_operand_object * obj_desc,
133 struct acpi_namespace_node * calling_method_node)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134{
Len Brown4be44fc2005-08-05 00:44:28 -0400135 acpi_status status = AE_OK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136
Bob Mooreb229cf92006-04-21 17:15:00 -0400137 ACPI_FUNCTION_TRACE_PTR(ds_begin_method_execution, method_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138
139 if (!method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400140 return_ACPI_STATUS(AE_NULL_ENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 }
142
Robert Mooreaff8c272005-09-02 17:24:17 -0400143 /* Prevent wraparound of thread count */
144
145 if (obj_desc->method.thread_count == ACPI_UINT8_MAX) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500146 ACPI_ERROR((AE_INFO,
147 "Method reached maximum reentrancy limit (255)"));
Robert Mooreaff8c272005-09-02 17:24:17 -0400148 return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
149 }
150
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 /*
152 * If there is a concurrency limit on this method, we need to
153 * obtain a unit from the method semaphore.
154 */
155 if (obj_desc->method.semaphore) {
156 /*
157 * Allow recursive method calls, up to the reentrancy/concurrency
158 * limit imposed by the SERIALIZED rule and the sync_level method
159 * parameter.
160 *
161 * The point of this code is to avoid permanently blocking a
162 * thread that is making recursive method calls.
163 */
164 if (method_node == calling_method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400165 if (obj_desc->method.thread_count >=
166 obj_desc->method.concurrency) {
167 return_ACPI_STATUS(AE_AML_METHOD_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 }
169 }
170
171 /*
172 * Get a unit from the method semaphore. This releases the
Bob Mooreb229cf92006-04-21 17:15:00 -0400173 * interpreter if we block (then reacquires it)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 */
Len Brown4be44fc2005-08-05 00:44:28 -0400175 status =
176 acpi_ex_system_wait_semaphore(obj_desc->method.semaphore,
177 ACPI_WAIT_FOREVER);
Bob Mooreb229cf92006-04-21 17:15:00 -0400178 if (ACPI_FAILURE(status)) {
179 return_ACPI_STATUS(status);
180 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700181 }
182
183 /*
Robert Mooreaff8c272005-09-02 17:24:17 -0400184 * Allocate an Owner ID for this method, only if this is the first thread
185 * to begin concurrent execution. We only need one owner_id, even if the
186 * method is invoked recursively.
187 */
188 if (!obj_desc->method.owner_id) {
189 status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
190 if (ACPI_FAILURE(status)) {
Bob Mooreb229cf92006-04-21 17:15:00 -0400191 goto cleanup;
Robert Mooreaff8c272005-09-02 17:24:17 -0400192 }
193 }
194
195 /*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 * Increment the method parse tree thread count since it has been
197 * reentered one more time (even if it is the same thread)
198 */
199 obj_desc->method.thread_count++;
Len Brown4be44fc2005-08-05 00:44:28 -0400200 return_ACPI_STATUS(status);
Bob Mooreb229cf92006-04-21 17:15:00 -0400201
202 cleanup:
203 /* On error, must signal the method semaphore if present */
204
205 if (obj_desc->method.semaphore) {
206 (void)acpi_os_signal_semaphore(obj_desc->method.semaphore, 1);
207 }
208 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211/*******************************************************************************
212 *
213 * FUNCTION: acpi_ds_call_control_method
214 *
215 * PARAMETERS: Thread - Info for this thread
216 * this_walk_state - Current walk state
217 * Op - Current Op to be walked
218 *
219 * RETURN: Status
220 *
221 * DESCRIPTION: Transfer execution to a called control method
222 *
223 ******************************************************************************/
224
225acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400226acpi_ds_call_control_method(struct acpi_thread_state *thread,
227 struct acpi_walk_state *this_walk_state,
228 union acpi_parse_object *op)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229{
Len Brown4be44fc2005-08-05 00:44:28 -0400230 acpi_status status;
231 struct acpi_namespace_node *method_node;
232 struct acpi_walk_state *next_walk_state = NULL;
233 union acpi_operand_object *obj_desc;
234 struct acpi_parameter_info info;
235 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700236
Bob Mooreb229cf92006-04-21 17:15:00 -0400237 ACPI_FUNCTION_TRACE_PTR(ds_call_control_method, this_walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238
Len Brown4be44fc2005-08-05 00:44:28 -0400239 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
240 "Execute method %p, currentstate=%p\n",
241 this_walk_state->prev_op, this_walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 /*
244 * Get the namespace entry for the control method we are about to call
245 */
246 method_node = this_walk_state->method_call_node;
247 if (!method_node) {
Len Brown4be44fc2005-08-05 00:44:28 -0400248 return_ACPI_STATUS(AE_NULL_ENTRY);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249 }
250
Len Brown4be44fc2005-08-05 00:44:28 -0400251 obj_desc = acpi_ns_get_attached_object(method_node);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 if (!obj_desc) {
Len Brown4be44fc2005-08-05 00:44:28 -0400253 return_ACPI_STATUS(AE_NULL_OBJECT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 }
255
Bob Mooreb229cf92006-04-21 17:15:00 -0400256 /* Init for new method, possibly wait on concurrency semaphore */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
Len Brown4be44fc2005-08-05 00:44:28 -0400258 status = acpi_ds_begin_method_execution(method_node, obj_desc,
259 this_walk_state->method_node);
260 if (ACPI_FAILURE(status)) {
Bob Mooreb229cf92006-04-21 17:15:00 -0400261 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262 }
263
Bob Mooreb229cf92006-04-21 17:15:00 -0400264 /*
265 * 1) Parse the method. All "normal" methods are parsed for each execution.
266 * Internal methods (_OSI, etc.) do not require parsing.
267 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (!(obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY)) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400269
Bob Mooreb229cf92006-04-21 17:15:00 -0400270 /* Create a new walk state for the parse */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700271
Len Brown4be44fc2005-08-05 00:44:28 -0400272 next_walk_state =
273 acpi_ds_create_walk_state(obj_desc->method.owner_id, op,
274 obj_desc, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275 if (!next_walk_state) {
Bob Mooreb229cf92006-04-21 17:15:00 -0400276 status = AE_NO_MEMORY;
277 goto cleanup;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 }
279
Bob Mooreb229cf92006-04-21 17:15:00 -0400280 /* Create and init a parse tree root */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281
Len Brown4be44fc2005-08-05 00:44:28 -0400282 op = acpi_ps_create_scope_op();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (!op) {
284 status = AE_NO_MEMORY;
285 goto cleanup;
286 }
287
Len Brown4be44fc2005-08-05 00:44:28 -0400288 status = acpi_ds_init_aml_walk(next_walk_state, op, method_node,
289 obj_desc->method.aml_start,
290 obj_desc->method.aml_length,
291 NULL, 1);
292 if (ACPI_FAILURE(status)) {
Bob Mooreb229cf92006-04-21 17:15:00 -0400293 acpi_ps_delete_parse_tree(op);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294 goto cleanup;
295 }
296
Bob Mooreb229cf92006-04-21 17:15:00 -0400297 /* Begin AML parse (deletes next_walk_state) */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700298
Len Brown4be44fc2005-08-05 00:44:28 -0400299 status = acpi_ps_parse_aml(next_walk_state);
300 acpi_ps_delete_parse_tree(op);
Bob Mooreb229cf92006-04-21 17:15:00 -0400301 if (ACPI_FAILURE(status)) {
302 goto cleanup;
303 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700304 }
305
Bob Mooreb229cf92006-04-21 17:15:00 -0400306 /* 2) Begin method execution. Create a new walk state */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307
Len Brown4be44fc2005-08-05 00:44:28 -0400308 next_walk_state = acpi_ds_create_walk_state(obj_desc->method.owner_id,
309 NULL, obj_desc, thread);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310 if (!next_walk_state) {
311 status = AE_NO_MEMORY;
312 goto cleanup;
313 }
Bob Mooreb229cf92006-04-21 17:15:00 -0400314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 /*
316 * The resolved arguments were put on the previous walk state's operand
Robert Mooreaff8c272005-09-02 17:24:17 -0400317 * stack. Operands on the previous walk state stack always
318 * start at index 0. Also, null terminate the list of arguments
Linus Torvalds1da177e2005-04-16 15:20:36 -0700319 */
Len Brown4be44fc2005-08-05 00:44:28 -0400320 this_walk_state->operands[this_walk_state->num_operands] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321
322 info.parameters = &this_walk_state->operands[0];
323 info.parameter_type = ACPI_PARAM_ARGS;
324
Len Brown4be44fc2005-08-05 00:44:28 -0400325 status = acpi_ds_init_aml_walk(next_walk_state, NULL, method_node,
326 obj_desc->method.aml_start,
327 obj_desc->method.aml_length, &info, 3);
328 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329 goto cleanup;
330 }
331
332 /*
333 * Delete the operands on the previous walkstate operand stack
334 * (they were copied to new objects)
335 */
336 for (i = 0; i < obj_desc->method.param_count; i++) {
Len Brown4be44fc2005-08-05 00:44:28 -0400337 acpi_ut_remove_reference(this_walk_state->operands[i]);
338 this_walk_state->operands[i] = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700339 }
340
341 /* Clear the operand stack */
342
343 this_walk_state->num_operands = 0;
344
Len Brown4be44fc2005-08-05 00:44:28 -0400345 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
346 "Starting nested execution, newstate=%p\n",
347 next_walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348
Bob Mooreb229cf92006-04-21 17:15:00 -0400349 /* Invoke an internal method if necessary */
350
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351 if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
Len Brown4be44fc2005-08-05 00:44:28 -0400352 status = obj_desc->method.implementation(next_walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 }
354
Robert Mooreaff8c272005-09-02 17:24:17 -0400355 return_ACPI_STATUS(status);
356
357 cleanup:
Robert Mooreaff8c272005-09-02 17:24:17 -0400358
Bob Mooreb229cf92006-04-21 17:15:00 -0400359 /* On error, we must terminate the method properly */
360
361 acpi_ds_terminate_control_method(obj_desc, next_walk_state);
362 if (next_walk_state) {
363 acpi_ds_delete_walk_state(next_walk_state);
Robert Mooreaff8c272005-09-02 17:24:17 -0400364 }
Len Browna94f1882005-09-03 00:09:12 -0400365
Len Brown4be44fc2005-08-05 00:44:28 -0400366 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367}
368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369/*******************************************************************************
370 *
371 * FUNCTION: acpi_ds_restart_control_method
372 *
373 * PARAMETERS: walk_state - State for preempted method (caller)
374 * return_desc - Return value from the called method
375 *
376 * RETURN: Status
377 *
378 * DESCRIPTION: Restart a method that was preempted by another (nested) method
379 * invocation. Handle the return value (if any) from the callee.
380 *
381 ******************************************************************************/
382
383acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400384acpi_ds_restart_control_method(struct acpi_walk_state *walk_state,
385 union acpi_operand_object *return_desc)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386{
Len Brown4be44fc2005-08-05 00:44:28 -0400387 acpi_status status;
Bob Moore958dd242006-05-12 17:12:00 -0400388 int same_as_implicit_return;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389
Bob Mooreb229cf92006-04-21 17:15:00 -0400390 ACPI_FUNCTION_TRACE_PTR(ds_restart_control_method, walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391
Len Brown4be44fc2005-08-05 00:44:28 -0400392 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Bob Mooreb229cf92006-04-21 17:15:00 -0400393 "****Restart [%4.4s] Op %p ReturnValueFromCallee %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400394 (char *)&walk_state->method_node->name,
395 walk_state->method_call_op, return_desc));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700396
Len Brown4be44fc2005-08-05 00:44:28 -0400397 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Bob Mooreb229cf92006-04-21 17:15:00 -0400398 " ReturnFromThisMethodUsed?=%X ResStack %p Walk %p\n",
Len Brown4be44fc2005-08-05 00:44:28 -0400399 walk_state->return_used,
400 walk_state->results, walk_state));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402 /* Did the called method return a value? */
403
404 if (return_desc) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400405
Bob Moore958dd242006-05-12 17:12:00 -0400406 /* Is the implicit return object the same as the return desc? */
407
408 same_as_implicit_return =
409 (walk_state->implicit_return_obj == return_desc);
410
Linus Torvalds1da177e2005-04-16 15:20:36 -0700411 /* Are we actually going to use the return value? */
412
413 if (walk_state->return_used) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400414
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 /* Save the return value from the previous method */
416
Len Brown4be44fc2005-08-05 00:44:28 -0400417 status = acpi_ds_result_push(return_desc, walk_state);
418 if (ACPI_FAILURE(status)) {
419 acpi_ut_remove_reference(return_desc);
420 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 }
422
423 /*
424 * Save as THIS method's return value in case it is returned
425 * immediately to yet another method
426 */
427 walk_state->return_desc = return_desc;
428 }
429
430 /*
Bob Moore958dd242006-05-12 17:12:00 -0400431 * The following code is the optional support for the so-called
432 * "implicit return". Some AML code assumes that the last value of the
433 * method is "implicitly" returned to the caller, in the absence of an
434 * explicit return value.
435 *
436 * Just save the last result of the method as the return value.
437 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 * NOTE: this is optional because the ASL language does not actually
439 * support this behavior.
440 */
Len Brown4be44fc2005-08-05 00:44:28 -0400441 else if (!acpi_ds_do_implicit_return
Bob Moore958dd242006-05-12 17:12:00 -0400442 (return_desc, walk_state, FALSE)
443 || same_as_implicit_return) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444 /*
445 * Delete the return value if it will not be used by the
Bob Moore958dd242006-05-12 17:12:00 -0400446 * calling method or remove one reference if the explicit return
447 * is the same as the implicit return value.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448 */
Len Brown4be44fc2005-08-05 00:44:28 -0400449 acpi_ut_remove_reference(return_desc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450 }
451 }
452
Len Brown4be44fc2005-08-05 00:44:28 -0400453 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454}
455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456/*******************************************************************************
457 *
458 * FUNCTION: acpi_ds_terminate_control_method
459 *
Bob Mooreb229cf92006-04-21 17:15:00 -0400460 * PARAMETERS: method_desc - Method object
461 * walk_state - State associated with the method
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 *
Robert Mooreaff8c272005-09-02 17:24:17 -0400463 * RETURN: None
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464 *
465 * DESCRIPTION: Terminate a control method. Delete everything that the method
466 * created, delete all locals and arguments, and delete the parse
467 * tree if requested.
468 *
469 ******************************************************************************/
470
Bob Mooreb229cf92006-04-21 17:15:00 -0400471void
472acpi_ds_terminate_control_method(union acpi_operand_object *method_desc,
473 struct acpi_walk_state *walk_state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474{
Len Brown4be44fc2005-08-05 00:44:28 -0400475 struct acpi_namespace_node *method_node;
476 acpi_status status;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700477
Bob Mooreb229cf92006-04-21 17:15:00 -0400478 ACPI_FUNCTION_TRACE_PTR(ds_terminate_control_method, walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700479
Bob Mooreb229cf92006-04-21 17:15:00 -0400480 /* method_desc is required, walk_state is optional */
481
482 if (!method_desc) {
Robert Mooreaff8c272005-09-02 17:24:17 -0400483 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485
Bob Mooreb229cf92006-04-21 17:15:00 -0400486 if (walk_state) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487
Bob Mooreb229cf92006-04-21 17:15:00 -0400488 /* Delete all arguments and locals */
489
490 acpi_ds_method_data_delete_all(walk_state);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700491 }
492
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 /*
494 * Lock the parser while we terminate this method.
495 * If this is the last thread executing the method,
496 * we have additional cleanup to perform
497 */
Len Brown4be44fc2005-08-05 00:44:28 -0400498 status = acpi_ut_acquire_mutex(ACPI_MTX_PARSER);
499 if (ACPI_FAILURE(status)) {
Robert Mooreaff8c272005-09-02 17:24:17 -0400500 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 }
502
503 /* Signal completion of the execution of this method if necessary */
504
Bob Mooreb229cf92006-04-21 17:15:00 -0400505 if (method_desc->method.semaphore) {
Len Brown4be44fc2005-08-05 00:44:28 -0400506 status =
Bob Mooreb229cf92006-04-21 17:15:00 -0400507 acpi_os_signal_semaphore(method_desc->method.semaphore, 1);
Len Brown4be44fc2005-08-05 00:44:28 -0400508 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509
Bob Mooreb229cf92006-04-21 17:15:00 -0400510 /* Ignore error and continue */
511
512 ACPI_EXCEPTION((AE_INFO, status,
513 "Could not signal method semaphore"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 }
515 }
516
Bob Mooreb229cf92006-04-21 17:15:00 -0400517 if (walk_state) {
518 /*
519 * Delete any objects created by this method during execution.
520 * The method Node is stored in the walk state
521 */
522 method_node = walk_state->method_node;
Bob Moore28f55eb2005-12-02 18:27:00 -0500523
Bob Mooreb229cf92006-04-21 17:15:00 -0400524 /* Lock namespace for possible update */
Bob Moore28f55eb2005-12-02 18:27:00 -0500525
Bob Mooreb229cf92006-04-21 17:15:00 -0400526 status = acpi_ut_acquire_mutex(ACPI_MTX_NAMESPACE);
527 if (ACPI_FAILURE(status)) {
528 goto exit;
529 }
530
531 /*
532 * Delete any namespace entries created immediately underneath
533 * the method
534 */
535 if (method_node && method_node->child) {
536 acpi_ns_delete_namespace_subtree(method_node);
537 }
538
539 /*
540 * Delete any namespace entries created anywhere else within
541 * the namespace by the execution of this method
542 */
543 acpi_ns_delete_namespace_by_owner(method_desc->method.owner_id);
544 status = acpi_ut_release_mutex(ACPI_MTX_NAMESPACE);
Bob Moore28f55eb2005-12-02 18:27:00 -0500545 }
546
Bob Mooreb229cf92006-04-21 17:15:00 -0400547 /* Decrement the thread count on the method */
Bob Moore28f55eb2005-12-02 18:27:00 -0500548
Bob Mooreb229cf92006-04-21 17:15:00 -0400549 if (method_desc->method.thread_count) {
550 method_desc->method.thread_count--;
551 } else {
552 ACPI_ERROR((AE_INFO, "Invalid zero thread count in method"));
553 }
Bob Moore28f55eb2005-12-02 18:27:00 -0500554
555 /* Are there any other threads currently executing this method? */
556
Bob Mooreb229cf92006-04-21 17:15:00 -0400557 if (method_desc->method.thread_count) {
Bob Moore28f55eb2005-12-02 18:27:00 -0500558 /*
559 * Additional threads. Do not release the owner_id in this case,
560 * we immediately reuse it for the next thread executing this method
561 */
Len Brown4be44fc2005-08-05 00:44:28 -0400562 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
Bob Moore28f55eb2005-12-02 18:27:00 -0500563 "*** Completed execution of one thread, %d threads remaining\n",
Bob Mooreb229cf92006-04-21 17:15:00 -0400564 method_desc->method.thread_count));
Bob Moore28f55eb2005-12-02 18:27:00 -0500565 } else {
566 /* This is the only executing thread for this method */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 /*
569 * Support to dynamically change a method from not_serialized to
Bob Moore28f55eb2005-12-02 18:27:00 -0500570 * Serialized if it appears that the method is incorrectly written and
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571 * does not support multiple thread execution. The best example of this
572 * is if such a method creates namespace objects and blocks. A second
573 * thread will fail with an AE_ALREADY_EXISTS exception
574 *
575 * This code is here because we must wait until the last thread exits
576 * before creating the synchronization semaphore.
577 */
Bob Mooreb229cf92006-04-21 17:15:00 -0400578 if ((method_desc->method.concurrency == 1) &&
579 (!method_desc->method.semaphore)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400580 status = acpi_os_create_semaphore(1, 1,
Bob Mooreb229cf92006-04-21 17:15:00 -0400581 &method_desc->method.
Len Brown4be44fc2005-08-05 00:44:28 -0400582 semaphore);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 }
584
Bob Moore28f55eb2005-12-02 18:27:00 -0500585 /* No more threads, we can free the owner_id */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586
Bob Mooreb229cf92006-04-21 17:15:00 -0400587 acpi_ut_release_owner_id(&method_desc->method.owner_id);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700588 }
Len Browna94f1882005-09-03 00:09:12 -0400589
Robert Mooreaff8c272005-09-02 17:24:17 -0400590 exit:
591 (void)acpi_ut_release_mutex(ACPI_MTX_PARSER);
592 return_VOID;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593}
Bob Moore28f55eb2005-12-02 18:27:00 -0500594
595#ifdef ACPI_INIT_PARSE_METHODS
596 /*
597 * Note 11/2005: Removed this code to parse all methods during table
598 * load because it causes problems if there are any errors during the
599 * parse. Also, it seems like overkill and we probably don't want to
600 * abort a table load because of an issue with a single method.
601 */
602
603/*******************************************************************************
604 *
605 * FUNCTION: acpi_ds_parse_method
606 *
607 * PARAMETERS: Node - Method node
608 *
609 * RETURN: Status
610 *
611 * DESCRIPTION: Parse the AML that is associated with the method.
612 *
613 * MUTEX: Assumes parser is locked
614 *
615 ******************************************************************************/
616
617acpi_status acpi_ds_parse_method(struct acpi_namespace_node *node)
618{
619 acpi_status status;
620 union acpi_operand_object *obj_desc;
621 union acpi_parse_object *op;
622 struct acpi_walk_state *walk_state;
623
Bob Mooreb229cf92006-04-21 17:15:00 -0400624 ACPI_FUNCTION_TRACE_PTR(ds_parse_method, node);
Bob Moore28f55eb2005-12-02 18:27:00 -0500625
626 /* Parameter Validation */
627
628 if (!node) {
629 return_ACPI_STATUS(AE_NULL_ENTRY);
630 }
631
632 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
Bob Mooreb229cf92006-04-21 17:15:00 -0400633 "**** Parsing [%4.4s] **** NamedObj=%p\n",
Bob Moore28f55eb2005-12-02 18:27:00 -0500634 acpi_ut_get_node_name(node), node));
635
636 /* Extract the method object from the method Node */
637
638 obj_desc = acpi_ns_get_attached_object(node);
639 if (!obj_desc) {
640 return_ACPI_STATUS(AE_NULL_OBJECT);
641 }
642
643 /* Create a mutex for the method if there is a concurrency limit */
644
645 if ((obj_desc->method.concurrency != ACPI_INFINITE_CONCURRENCY) &&
646 (!obj_desc->method.semaphore)) {
647 status = acpi_os_create_semaphore(obj_desc->method.concurrency,
648 obj_desc->method.concurrency,
649 &obj_desc->method.semaphore);
650 if (ACPI_FAILURE(status)) {
651 return_ACPI_STATUS(status);
652 }
653 }
654
655 /*
656 * Allocate a new parser op to be the root of the parsed
657 * method tree
658 */
659 op = acpi_ps_alloc_op(AML_METHOD_OP);
660 if (!op) {
661 return_ACPI_STATUS(AE_NO_MEMORY);
662 }
663
664 /* Init new op with the method name and pointer back to the Node */
665
666 acpi_ps_set_name(op, node->name.integer);
667 op->common.node = node;
668
669 /*
670 * Get a new owner_id for objects created by this method. Namespace
671 * objects (such as Operation Regions) can be created during the
672 * first pass parse.
673 */
674 status = acpi_ut_allocate_owner_id(&obj_desc->method.owner_id);
675 if (ACPI_FAILURE(status)) {
676 goto cleanup;
677 }
678
679 /* Create and initialize a new walk state */
680
681 walk_state =
682 acpi_ds_create_walk_state(obj_desc->method.owner_id, NULL, NULL,
683 NULL);
684 if (!walk_state) {
685 status = AE_NO_MEMORY;
686 goto cleanup2;
687 }
688
689 status = acpi_ds_init_aml_walk(walk_state, op, node,
690 obj_desc->method.aml_start,
691 obj_desc->method.aml_length, NULL, 1);
692 if (ACPI_FAILURE(status)) {
693 acpi_ds_delete_walk_state(walk_state);
694 goto cleanup2;
695 }
696
697 /*
698 * Parse the method, first pass
699 *
700 * The first pass load is where newly declared named objects are added into
701 * the namespace. Actual evaluation of the named objects (what would be
702 * called a "second pass") happens during the actual execution of the
703 * method so that operands to the named objects can take on dynamic
704 * run-time values.
705 */
706 status = acpi_ps_parse_aml(walk_state);
707 if (ACPI_FAILURE(status)) {
708 goto cleanup2;
709 }
710
711 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
Bob Mooreb229cf92006-04-21 17:15:00 -0400712 "**** [%4.4s] Parsed **** NamedObj=%p Op=%p\n",
Bob Moore28f55eb2005-12-02 18:27:00 -0500713 acpi_ut_get_node_name(node), node, op));
714
715 /*
716 * Delete the parse tree. We simply re-parse the method for every
717 * execution since there isn't much overhead (compared to keeping lots
718 * of parse trees around)
719 */
720 acpi_ns_delete_namespace_subtree(node);
721 acpi_ns_delete_namespace_by_owner(obj_desc->method.owner_id);
722
723 cleanup2:
724 acpi_ut_release_owner_id(&obj_desc->method.owner_id);
725
726 cleanup:
727 acpi_ps_delete_parse_tree(op);
728 return_ACPI_STATUS(status);
729}
730#endif