Linux-2.6.12-rc2
[linux-2.6/kvm.git] / drivers / acpi / dispatcher / dsmethod.c
blob9f0456cb9bb55c7d771c77dd487e6f6b7ebf3ce8
1 /******************************************************************************
3 * Module Name: dsmethod - Parser/Interpreter interface - control method parsing
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2005, R. Byron Moore
9 * All rights reserved.
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.
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.
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.
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acdispat.h>
49 #include <acpi/acinterp.h>
50 #include <acpi/acnamesp.h>
53 #define _COMPONENT ACPI_DISPATCHER
54 ACPI_MODULE_NAME ("dsmethod")
57 /*******************************************************************************
59 * FUNCTION: acpi_ds_parse_method
61 * PARAMETERS: obj_handle - Method node
63 * RETURN: Status
65 * DESCRIPTION: Call the parser and parse the AML that is associated with the
66 * method.
68 * MUTEX: Assumes parser is locked
70 ******************************************************************************/
72 acpi_status
73 acpi_ds_parse_method (
74 acpi_handle obj_handle)
76 acpi_status status;
77 union acpi_operand_object *obj_desc;
78 union acpi_parse_object *op;
79 struct acpi_namespace_node *node;
80 acpi_owner_id owner_id;
81 struct acpi_walk_state *walk_state;
84 ACPI_FUNCTION_TRACE_PTR ("ds_parse_method", obj_handle);
87 /* Parameter Validation */
89 if (!obj_handle) {
90 return_ACPI_STATUS (AE_NULL_ENTRY);
93 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE, "**** Parsing [%4.4s] **** named_obj=%p\n",
94 acpi_ut_get_node_name (obj_handle), obj_handle));
96 /* Extract the method object from the method Node */
98 node = (struct acpi_namespace_node *) obj_handle;
99 obj_desc = acpi_ns_get_attached_object (node);
100 if (!obj_desc) {
101 return_ACPI_STATUS (AE_NULL_OBJECT);
104 /* Create a mutex for the method if there is a concurrency limit */
106 if ((obj_desc->method.concurrency != ACPI_INFINITE_CONCURRENCY) &&
107 (!obj_desc->method.semaphore)) {
108 status = acpi_os_create_semaphore (obj_desc->method.concurrency,
109 obj_desc->method.concurrency,
110 &obj_desc->method.semaphore);
111 if (ACPI_FAILURE (status)) {
112 return_ACPI_STATUS (status);
117 * Allocate a new parser op to be the root of the parsed
118 * method tree
120 op = acpi_ps_alloc_op (AML_METHOD_OP);
121 if (!op) {
122 return_ACPI_STATUS (AE_NO_MEMORY);
125 /* Init new op with the method name and pointer back to the Node */
127 acpi_ps_set_name (op, node->name.integer);
128 op->common.node = node;
131 * Get a new owner_id for objects created by this method. Namespace
132 * objects (such as Operation Regions) can be created during the
133 * first pass parse.
135 owner_id = acpi_ut_allocate_owner_id (ACPI_OWNER_TYPE_METHOD);
136 obj_desc->method.owning_id = owner_id;
138 /* Create and initialize a new walk state */
140 walk_state = acpi_ds_create_walk_state (owner_id, NULL, NULL, NULL);
141 if (!walk_state) {
142 return_ACPI_STATUS (AE_NO_MEMORY);
145 status = acpi_ds_init_aml_walk (walk_state, op, node,
146 obj_desc->method.aml_start,
147 obj_desc->method.aml_length, NULL, 1);
148 if (ACPI_FAILURE (status)) {
149 acpi_ds_delete_walk_state (walk_state);
150 return_ACPI_STATUS (status);
154 * Parse the method, first pass
156 * The first pass load is where newly declared named objects are
157 * added into the namespace. Actual evaluation of
158 * the named objects (what would be called a "second
159 * pass") happens during the actual execution of the
160 * method so that operands to the named objects can
161 * take on dynamic run-time values.
163 status = acpi_ps_parse_aml (walk_state);
164 if (ACPI_FAILURE (status)) {
165 return_ACPI_STATUS (status);
168 ACPI_DEBUG_PRINT ((ACPI_DB_PARSE,
169 "**** [%4.4s] Parsed **** named_obj=%p Op=%p\n",
170 acpi_ut_get_node_name (obj_handle), obj_handle, op));
172 acpi_ps_delete_parse_tree (op);
173 return_ACPI_STATUS (status);
177 /*******************************************************************************
179 * FUNCTION: acpi_ds_begin_method_execution
181 * PARAMETERS: method_node - Node of the method
182 * obj_desc - The method object
183 * calling_method_node - Caller of this method (if non-null)
185 * RETURN: Status
187 * DESCRIPTION: Prepare a method for execution. Parses the method if necessary,
188 * increments the thread count, and waits at the method semaphore
189 * for clearance to execute.
191 ******************************************************************************/
193 acpi_status
194 acpi_ds_begin_method_execution (
195 struct acpi_namespace_node *method_node,
196 union acpi_operand_object *obj_desc,
197 struct acpi_namespace_node *calling_method_node)
199 acpi_status status = AE_OK;
202 ACPI_FUNCTION_TRACE_PTR ("ds_begin_method_execution", method_node);
205 if (!method_node) {
206 return_ACPI_STATUS (AE_NULL_ENTRY);
210 * If there is a concurrency limit on this method, we need to
211 * obtain a unit from the method semaphore.
213 if (obj_desc->method.semaphore) {
215 * Allow recursive method calls, up to the reentrancy/concurrency
216 * limit imposed by the SERIALIZED rule and the sync_level method
217 * parameter.
219 * The point of this code is to avoid permanently blocking a
220 * thread that is making recursive method calls.
222 if (method_node == calling_method_node) {
223 if (obj_desc->method.thread_count >= obj_desc->method.concurrency) {
224 return_ACPI_STATUS (AE_AML_METHOD_LIMIT);
229 * Get a unit from the method semaphore. This releases the
230 * interpreter if we block
232 status = acpi_ex_system_wait_semaphore (obj_desc->method.semaphore,
233 ACPI_WAIT_FOREVER);
237 * Increment the method parse tree thread count since it has been
238 * reentered one more time (even if it is the same thread)
240 obj_desc->method.thread_count++;
241 return_ACPI_STATUS (status);
245 /*******************************************************************************
247 * FUNCTION: acpi_ds_call_control_method
249 * PARAMETERS: Thread - Info for this thread
250 * this_walk_state - Current walk state
251 * Op - Current Op to be walked
253 * RETURN: Status
255 * DESCRIPTION: Transfer execution to a called control method
257 ******************************************************************************/
259 acpi_status
260 acpi_ds_call_control_method (
261 struct acpi_thread_state *thread,
262 struct acpi_walk_state *this_walk_state,
263 union acpi_parse_object *op)
265 acpi_status status;
266 struct acpi_namespace_node *method_node;
267 struct acpi_walk_state *next_walk_state;
268 union acpi_operand_object *obj_desc;
269 struct acpi_parameter_info info;
270 u32 i;
273 ACPI_FUNCTION_TRACE_PTR ("ds_call_control_method", this_walk_state);
275 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Execute method %p, currentstate=%p\n",
276 this_walk_state->prev_op, this_walk_state));
279 * Get the namespace entry for the control method we are about to call
281 method_node = this_walk_state->method_call_node;
282 if (!method_node) {
283 return_ACPI_STATUS (AE_NULL_ENTRY);
286 obj_desc = acpi_ns_get_attached_object (method_node);
287 if (!obj_desc) {
288 return_ACPI_STATUS (AE_NULL_OBJECT);
291 obj_desc->method.owning_id = acpi_ut_allocate_owner_id (ACPI_OWNER_TYPE_METHOD);
293 /* Init for new method, wait on concurrency semaphore */
295 status = acpi_ds_begin_method_execution (method_node, obj_desc,
296 this_walk_state->method_node);
297 if (ACPI_FAILURE (status)) {
298 return_ACPI_STATUS (status);
301 if (!(obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY)) {
302 /* 1) Parse: Create a new walk state for the preempting walk */
304 next_walk_state = acpi_ds_create_walk_state (obj_desc->method.owning_id,
305 op, obj_desc, NULL);
306 if (!next_walk_state) {
307 return_ACPI_STATUS (AE_NO_MEMORY);
310 /* Create and init a Root Node */
312 op = acpi_ps_create_scope_op ();
313 if (!op) {
314 status = AE_NO_MEMORY;
315 goto cleanup;
318 status = acpi_ds_init_aml_walk (next_walk_state, op, method_node,
319 obj_desc->method.aml_start, obj_desc->method.aml_length,
320 NULL, 1);
321 if (ACPI_FAILURE (status)) {
322 acpi_ds_delete_walk_state (next_walk_state);
323 goto cleanup;
326 /* Begin AML parse */
328 status = acpi_ps_parse_aml (next_walk_state);
329 acpi_ps_delete_parse_tree (op);
332 /* 2) Execute: Create a new state for the preempting walk */
334 next_walk_state = acpi_ds_create_walk_state (obj_desc->method.owning_id,
335 NULL, obj_desc, thread);
336 if (!next_walk_state) {
337 status = AE_NO_MEMORY;
338 goto cleanup;
341 * The resolved arguments were put on the previous walk state's operand
342 * stack. Operands on the previous walk state stack always
343 * start at index 0.
344 * Null terminate the list of arguments
346 this_walk_state->operands [this_walk_state->num_operands] = NULL;
348 info.parameters = &this_walk_state->operands[0];
349 info.parameter_type = ACPI_PARAM_ARGS;
351 status = acpi_ds_init_aml_walk (next_walk_state, NULL, method_node,
352 obj_desc->method.aml_start, obj_desc->method.aml_length,
353 &info, 3);
354 if (ACPI_FAILURE (status)) {
355 goto cleanup;
359 * Delete the operands on the previous walkstate operand stack
360 * (they were copied to new objects)
362 for (i = 0; i < obj_desc->method.param_count; i++) {
363 acpi_ut_remove_reference (this_walk_state->operands [i]);
364 this_walk_state->operands [i] = NULL;
367 /* Clear the operand stack */
369 this_walk_state->num_operands = 0;
371 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
372 "Starting nested execution, newstate=%p\n", next_walk_state));
374 if (obj_desc->method.method_flags & AML_METHOD_INTERNAL_ONLY) {
375 status = obj_desc->method.implementation (next_walk_state);
376 return_ACPI_STATUS (status);
379 return_ACPI_STATUS (AE_OK);
382 /* On error, we must delete the new walk state */
384 cleanup:
385 if (next_walk_state && (next_walk_state->method_desc)) {
386 /* Decrement the thread count on the method parse tree */
388 next_walk_state->method_desc->method.thread_count--;
390 (void) acpi_ds_terminate_control_method (next_walk_state);
391 acpi_ds_delete_walk_state (next_walk_state);
392 return_ACPI_STATUS (status);
396 /*******************************************************************************
398 * FUNCTION: acpi_ds_restart_control_method
400 * PARAMETERS: walk_state - State for preempted method (caller)
401 * return_desc - Return value from the called method
403 * RETURN: Status
405 * DESCRIPTION: Restart a method that was preempted by another (nested) method
406 * invocation. Handle the return value (if any) from the callee.
408 ******************************************************************************/
410 acpi_status
411 acpi_ds_restart_control_method (
412 struct acpi_walk_state *walk_state,
413 union acpi_operand_object *return_desc)
415 acpi_status status;
418 ACPI_FUNCTION_TRACE_PTR ("ds_restart_control_method", walk_state);
421 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
422 "****Restart [%4.4s] Op %p return_value_from_callee %p\n",
423 (char *) &walk_state->method_node->name, walk_state->method_call_op,
424 return_desc));
426 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
427 " return_from_this_method_used?=%X res_stack %p Walk %p\n",
428 walk_state->return_used,
429 walk_state->results, walk_state));
431 /* Did the called method return a value? */
433 if (return_desc) {
434 /* Are we actually going to use the return value? */
436 if (walk_state->return_used) {
437 /* Save the return value from the previous method */
439 status = acpi_ds_result_push (return_desc, walk_state);
440 if (ACPI_FAILURE (status)) {
441 acpi_ut_remove_reference (return_desc);
442 return_ACPI_STATUS (status);
446 * Save as THIS method's return value in case it is returned
447 * immediately to yet another method
449 walk_state->return_desc = return_desc;
453 * The following code is the
454 * optional support for a so-called "implicit return". Some AML code
455 * assumes that the last value of the method is "implicitly" returned
456 * to the caller. Just save the last result as the return value.
457 * NOTE: this is optional because the ASL language does not actually
458 * support this behavior.
460 else if (!acpi_ds_do_implicit_return (return_desc, walk_state, FALSE)) {
462 * Delete the return value if it will not be used by the
463 * calling method
465 acpi_ut_remove_reference (return_desc);
469 return_ACPI_STATUS (AE_OK);
473 /*******************************************************************************
475 * FUNCTION: acpi_ds_terminate_control_method
477 * PARAMETERS: walk_state - State of the method
479 * RETURN: Status
481 * DESCRIPTION: Terminate a control method. Delete everything that the method
482 * created, delete all locals and arguments, and delete the parse
483 * tree if requested.
485 ******************************************************************************/
487 acpi_status
488 acpi_ds_terminate_control_method (
489 struct acpi_walk_state *walk_state)
491 union acpi_operand_object *obj_desc;
492 struct acpi_namespace_node *method_node;
493 acpi_status status;
496 ACPI_FUNCTION_TRACE_PTR ("ds_terminate_control_method", walk_state);
499 if (!walk_state) {
500 return (AE_BAD_PARAMETER);
503 /* The current method object was saved in the walk state */
505 obj_desc = walk_state->method_desc;
506 if (!obj_desc) {
507 return_ACPI_STATUS (AE_OK);
510 /* Delete all arguments and locals */
512 acpi_ds_method_data_delete_all (walk_state);
515 * Lock the parser while we terminate this method.
516 * If this is the last thread executing the method,
517 * we have additional cleanup to perform
519 status = acpi_ut_acquire_mutex (ACPI_MTX_PARSER);
520 if (ACPI_FAILURE (status)) {
521 return_ACPI_STATUS (status);
524 /* Signal completion of the execution of this method if necessary */
526 if (walk_state->method_desc->method.semaphore) {
527 status = acpi_os_signal_semaphore (
528 walk_state->method_desc->method.semaphore, 1);
529 if (ACPI_FAILURE (status)) {
530 ACPI_REPORT_ERROR (("Could not signal method semaphore\n"));
531 status = AE_OK;
533 /* Ignore error and continue cleanup */
537 if (walk_state->method_desc->method.thread_count) {
538 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
539 "*** Not deleting method namespace, there are still %d threads\n",
540 walk_state->method_desc->method.thread_count));
543 if (!walk_state->method_desc->method.thread_count) {
545 * Support to dynamically change a method from not_serialized to
546 * Serialized if it appears that the method is written foolishly and
547 * does not support multiple thread execution. The best example of this
548 * is if such a method creates namespace objects and blocks. A second
549 * thread will fail with an AE_ALREADY_EXISTS exception
551 * This code is here because we must wait until the last thread exits
552 * before creating the synchronization semaphore.
554 if ((walk_state->method_desc->method.concurrency == 1) &&
555 (!walk_state->method_desc->method.semaphore)) {
556 status = acpi_os_create_semaphore (1,
558 &walk_state->method_desc->method.semaphore);
562 * There are no more threads executing this method. Perform
563 * additional cleanup.
565 * The method Node is stored in the walk state
567 method_node = walk_state->method_node;
570 * Delete any namespace entries created immediately underneath
571 * the method
573 status = acpi_ut_acquire_mutex (ACPI_MTX_NAMESPACE);
574 if (ACPI_FAILURE (status)) {
575 return_ACPI_STATUS (status);
578 if (method_node->child) {
579 acpi_ns_delete_namespace_subtree (method_node);
583 * Delete any namespace entries created anywhere else within
584 * the namespace
586 acpi_ns_delete_namespace_by_owner (walk_state->method_desc->method.owning_id);
587 status = acpi_ut_release_mutex (ACPI_MTX_NAMESPACE);
588 if (ACPI_FAILURE (status)) {
589 return_ACPI_STATUS (status);
593 status = acpi_ut_release_mutex (ACPI_MTX_PARSER);
594 return_ACPI_STATUS (status);