vserver 1.9.3
[linux-2.6.git] / drivers / acpi / dispatcher / dsutils.c
1 /*******************************************************************************
2  *
3  * Module Name: dsutils - Dispatcher utilities
4  *
5  ******************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2004, 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
44
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>
51 #include <acpi/acdebug.h>
52
53 #define _COMPONENT          ACPI_DISPATCHER
54          ACPI_MODULE_NAME    ("dsutils")
55
56
57 #ifndef ACPI_NO_METHOD_EXECUTION
58
59 /*******************************************************************************
60  *
61  * FUNCTION:    acpi_ds_is_result_used
62  *
63  * PARAMETERS:  Op                  - Current Op
64  *              walk_state          - Current State
65  *
66  * RETURN:      TRUE if result is used, FALSE otherwise
67  *
68  * DESCRIPTION: Check if a result object will be used by the parent
69  *
70  ******************************************************************************/
71
72 u8
73 acpi_ds_is_result_used (
74         union acpi_parse_object         *op,
75         struct acpi_walk_state          *walk_state)
76 {
77         const struct acpi_opcode_info   *parent_info;
78
79
80         ACPI_FUNCTION_TRACE_PTR ("ds_is_result_used", op);
81
82
83         /* Must have both an Op and a Result Object */
84
85         if (!op) {
86                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
87                 return_VALUE (TRUE);
88         }
89
90         /*
91          * If there is no parent, we are executing at the method level.
92          * An executing method typically has no parent, since each method
93          * is parsed separately.
94          */
95         if (!op->common.parent) {
96                 /*
97                  * If this is the last statement in the method, we know it is not a
98                  * Return() operator (would not come here.) The following code is the
99                  * optional support for a so-called "implicit return". Some AML code
100                  * assumes that the last value of the method is "implicitly" returned
101                  * to the caller. Just save the last result as the return value.
102                  * NOTE: this is optional because the ASL language does not actually
103                  * support this behavior.
104                  */
105                 if ((acpi_gbl_enable_interpreter_slack) &&
106                         (walk_state->parser_state.aml >= walk_state->parser_state.aml_end)) {
107                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
108                                         "Result of [%s] will be implicitly returned\n",
109                                         acpi_ps_get_opcode_name (op->common.aml_opcode)));
110
111                         /* Use the top of the result stack as the implicit return value */
112
113                         walk_state->return_desc = walk_state->results->results.obj_desc[0];
114                         return_VALUE (TRUE);
115                 }
116
117                 /* No parent, the return value cannot possibly be used */
118
119                 return_VALUE (FALSE);
120         }
121
122         /* Get info on the parent. The root_op is AML_SCOPE */
123
124         parent_info = acpi_ps_get_opcode_info (op->common.parent->common.aml_opcode);
125         if (parent_info->class == AML_CLASS_UNKNOWN) {
126                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown parent opcode. Op=%p\n", op));
127                 return_VALUE (FALSE);
128         }
129
130         /*
131          * Decide what to do with the result based on the parent.  If
132          * the parent opcode will not use the result, delete the object.
133          * Otherwise leave it as is, it will be deleted when it is used
134          * as an operand later.
135          */
136         switch (parent_info->class) {
137         case AML_CLASS_CONTROL:
138
139                 switch (op->common.parent->common.aml_opcode) {
140                 case AML_RETURN_OP:
141
142                         /* Never delete the return value associated with a return opcode */
143
144                         goto result_used;
145
146                 case AML_IF_OP:
147                 case AML_WHILE_OP:
148
149                         /*
150                          * If we are executing the predicate AND this is the predicate op,
151                          * we will use the return value
152                          */
153                         if ((walk_state->control_state->common.state == ACPI_CONTROL_PREDICATE_EXECUTING) &&
154                                 (walk_state->control_state->control.predicate_op == op)) {
155                                 goto result_used;
156                         }
157                         break;
158
159                 default:
160                         /* Ignore other control opcodes */
161                         break;
162                 }
163
164                 /* The general control opcode returns no result */
165
166                 goto result_not_used;
167
168
169         case AML_CLASS_CREATE:
170
171                 /*
172                  * These opcodes allow term_arg(s) as operands and therefore
173                  * the operands can be method calls.  The result is used.
174                  */
175                 goto result_used;
176
177
178         case AML_CLASS_NAMED_OBJECT:
179
180                 if ((op->common.parent->common.aml_opcode == AML_REGION_OP)      ||
181                         (op->common.parent->common.aml_opcode == AML_DATA_REGION_OP) ||
182                         (op->common.parent->common.aml_opcode == AML_PACKAGE_OP)     ||
183                         (op->common.parent->common.aml_opcode == AML_VAR_PACKAGE_OP) ||
184                         (op->common.parent->common.aml_opcode == AML_BUFFER_OP)      ||
185                         (op->common.parent->common.aml_opcode == AML_INT_EVAL_SUBTREE_OP)) {
186                         /*
187                          * These opcodes allow term_arg(s) as operands and therefore
188                          * the operands can be method calls.  The result is used.
189                          */
190                         goto result_used;
191                 }
192
193                 goto result_not_used;
194
195
196         default:
197
198                 /*
199                  * In all other cases. the parent will actually use the return
200                  * object, so keep it.
201                  */
202                 goto result_used;
203         }
204
205
206 result_used:
207         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] used by Parent [%s] Op=%p\n",
208                         acpi_ps_get_opcode_name (op->common.aml_opcode),
209                         acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
210
211         return_VALUE (TRUE);
212
213
214 result_not_used:
215         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Result of [%s] not used by Parent [%s] Op=%p\n",
216                         acpi_ps_get_opcode_name (op->common.aml_opcode),
217                         acpi_ps_get_opcode_name (op->common.parent->common.aml_opcode), op));
218
219         return_VALUE (FALSE);
220 }
221
222
223 /*******************************************************************************
224  *
225  * FUNCTION:    acpi_ds_delete_result_if_not_used
226  *
227  * PARAMETERS:  Op              - Current parse Op
228  *              result_obj      - Result of the operation
229  *              walk_state      - Current state
230  *
231  * RETURN:      Status
232  *
233  * DESCRIPTION: Used after interpretation of an opcode.  If there is an internal
234  *              result descriptor, check if the parent opcode will actually use
235  *              this result.  If not, delete the result now so that it will
236  *              not become orphaned.
237  *
238  ******************************************************************************/
239
240 void
241 acpi_ds_delete_result_if_not_used (
242         union acpi_parse_object         *op,
243         union acpi_operand_object       *result_obj,
244         struct acpi_walk_state          *walk_state)
245 {
246         union acpi_operand_object       *obj_desc;
247         acpi_status                     status;
248
249
250         ACPI_FUNCTION_TRACE_PTR ("ds_delete_result_if_not_used", result_obj);
251
252
253         if (!op) {
254                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Null Op\n"));
255                 return_VOID;
256         }
257
258         if (!result_obj) {
259                 return_VOID;
260         }
261
262         if (!acpi_ds_is_result_used (op, walk_state)) {
263                 /*
264                  * Must pop the result stack (obj_desc should be equal to result_obj)
265                  */
266                 status = acpi_ds_result_pop (&obj_desc, walk_state);
267                 if (ACPI_SUCCESS (status)) {
268                         acpi_ut_remove_reference (result_obj);
269                 }
270         }
271
272         return_VOID;
273 }
274
275
276 /*******************************************************************************
277  *
278  * FUNCTION:    acpi_ds_resolve_operands
279  *
280  * PARAMETERS:  walk_state          - Current walk state with operands on stack
281  *
282  * RETURN:      Status
283  *
284  * DESCRIPTION: Resolve all operands to their values.  Used to prepare
285  *              arguments to a control method invocation (a call from one
286  *              method to another.)
287  *
288  ******************************************************************************/
289
290 acpi_status
291 acpi_ds_resolve_operands (
292         struct acpi_walk_state          *walk_state)
293 {
294         u32                             i;
295         acpi_status                     status = AE_OK;
296
297
298         ACPI_FUNCTION_TRACE_PTR ("ds_resolve_operands", walk_state);
299
300
301         /*
302          * Attempt to resolve each of the valid operands
303          * Method arguments are passed by reference, not by value.  This means
304          * that the actual objects are passed, not copies of the objects.
305          */
306         for (i = 0; i < walk_state->num_operands; i++) {
307                 status = acpi_ex_resolve_to_value (&walk_state->operands[i], walk_state);
308                 if (ACPI_FAILURE (status)) {
309                         break;
310                 }
311         }
312
313         return_ACPI_STATUS (status);
314 }
315
316
317 /*******************************************************************************
318  *
319  * FUNCTION:    acpi_ds_clear_operands
320  *
321  * PARAMETERS:  walk_state          - Current walk state with operands on stack
322  *
323  * RETURN:      None
324  *
325  * DESCRIPTION: Clear all operands on the current walk state operand stack.
326  *
327  ******************************************************************************/
328
329 void
330 acpi_ds_clear_operands (
331         struct acpi_walk_state          *walk_state)
332 {
333         u32                             i;
334
335
336         ACPI_FUNCTION_TRACE_PTR ("acpi_ds_clear_operands", walk_state);
337
338
339         /*
340          * Remove a reference on each operand on the stack
341          */
342         for (i = 0; i < walk_state->num_operands; i++) {
343                 /*
344                  * Remove a reference to all operands, including both
345                  * "Arguments" and "Targets".
346                  */
347                 acpi_ut_remove_reference (walk_state->operands[i]);
348                 walk_state->operands[i] = NULL;
349         }
350
351         walk_state->num_operands = 0;
352         return_VOID;
353 }
354 #endif
355
356
357 /*******************************************************************************
358  *
359  * FUNCTION:    acpi_ds_create_operand
360  *
361  * PARAMETERS:  walk_state      - Current walk state
362  *              Arg             - Parse object for the argument
363  *              arg_index       - Which argument (zero based)
364  *
365  * RETURN:      Status
366  *
367  * DESCRIPTION: Translate a parse tree object that is an argument to an AML
368  *              opcode to the equivalent interpreter object.  This may include
369  *              looking up a name or entering a new name into the internal
370  *              namespace.
371  *
372  ******************************************************************************/
373
374 acpi_status
375 acpi_ds_create_operand (
376         struct acpi_walk_state          *walk_state,
377         union acpi_parse_object         *arg,
378         u32                             arg_index)
379 {
380         acpi_status                     status = AE_OK;
381         char                            *name_string;
382         u32                             name_length;
383         union acpi_operand_object       *obj_desc;
384         union acpi_parse_object         *parent_op;
385         u16                             opcode;
386         acpi_interpreter_mode           interpreter_mode;
387         const struct acpi_opcode_info   *op_info;
388
389
390         ACPI_FUNCTION_TRACE_PTR ("ds_create_operand", arg);
391
392
393         /* A valid name must be looked up in the namespace */
394
395         if ((arg->common.aml_opcode == AML_INT_NAMEPATH_OP) &&
396                 (arg->common.value.string)) {
397                 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Getting a name: Arg=%p\n", arg));
398
399                 /* Get the entire name string from the AML stream */
400
401                 status = acpi_ex_get_name_string (ACPI_TYPE_ANY, arg->common.value.buffer,
402                                   &name_string, &name_length);
403
404                 if (ACPI_FAILURE (status)) {
405                         return_ACPI_STATUS (status);
406                 }
407
408                 /*
409                  * All prefixes have been handled, and the name is
410                  * in name_string
411                  */
412
413
414                 /*
415                  * Special handling for buffer_field declarations. This is a deferred
416                  * opcode that unfortunately defines the field name as the last
417                  * parameter instead of the first.  We get here when we are performing
418                  * the deferred execution, so the actual name of the field is already
419                  * in the namespace.  We don't want to attempt to look it up again
420                  * because we may be executing in a different scope than where the
421                  * actual opcode exists.
422                  */
423                 if ((walk_state->deferred_node) &&
424                         (walk_state->deferred_node->type == ACPI_TYPE_BUFFER_FIELD) &&
425                         (arg_index != 0)) {
426                         obj_desc = ACPI_CAST_PTR (union acpi_operand_object, walk_state->deferred_node);
427                         status = AE_OK;
428                 }
429                 else    /* All other opcodes */ {
430                         /*
431                          * Differentiate between a namespace "create" operation
432                          * versus a "lookup" operation (IMODE_LOAD_PASS2 vs.
433                          * IMODE_EXECUTE) in order to support the creation of
434                          * namespace objects during the execution of control methods.
435                          */
436                         parent_op = arg->common.parent;
437                         op_info = acpi_ps_get_opcode_info (parent_op->common.aml_opcode);
438                         if ((op_info->flags & AML_NSNODE) &&
439                                 (parent_op->common.aml_opcode != AML_INT_METHODCALL_OP) &&
440                                 (parent_op->common.aml_opcode != AML_REGION_OP) &&
441                                 (parent_op->common.aml_opcode != AML_INT_NAMEPATH_OP)) {
442                                 /* Enter name into namespace if not found */
443
444                                 interpreter_mode = ACPI_IMODE_LOAD_PASS2;
445                         }
446                         else {
447                                 /* Return a failure if name not found */
448
449                                 interpreter_mode = ACPI_IMODE_EXECUTE;
450                         }
451
452                         status = acpi_ns_lookup (walk_state->scope_info, name_string,
453                                          ACPI_TYPE_ANY, interpreter_mode,
454                                          ACPI_NS_SEARCH_PARENT | ACPI_NS_DONT_OPEN_SCOPE,
455                                          walk_state,
456                                          ACPI_CAST_INDIRECT_PTR (struct acpi_namespace_node, &obj_desc));
457                         /*
458                          * The only case where we pass through (ignore) a NOT_FOUND
459                          * error is for the cond_ref_of opcode.
460                          */
461                         if (status == AE_NOT_FOUND) {
462                                 if (parent_op->common.aml_opcode == AML_COND_REF_OF_OP) {
463                                         /*
464                                          * For the Conditional Reference op, it's OK if
465                                          * the name is not found;  We just need a way to
466                                          * indicate this to the interpreter, set the
467                                          * object to the root
468                                          */
469                                         obj_desc = ACPI_CAST_PTR (union acpi_operand_object, acpi_gbl_root_node);
470                                         status = AE_OK;
471                                 }
472                                 else {
473                                         /*
474                                          * We just plain didn't find it -- which is a
475                                          * very serious error at this point
476                                          */
477                                         status = AE_AML_NAME_NOT_FOUND;
478                                 }
479                         }
480
481                         if (ACPI_FAILURE (status)) {
482                                 ACPI_REPORT_NSERROR (name_string, status);
483                         }
484                 }
485
486                 /* Free the namestring created above */
487
488                 ACPI_MEM_FREE (name_string);
489
490                 /* Check status from the lookup */
491
492                 if (ACPI_FAILURE (status)) {
493                         return_ACPI_STATUS (status);
494                 }
495
496                 /* Put the resulting object onto the current object stack */
497
498                 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
499                 if (ACPI_FAILURE (status)) {
500                         return_ACPI_STATUS (status);
501                 }
502                 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
503         }
504         else {
505                 /* Check for null name case */
506
507                 if (arg->common.aml_opcode == AML_INT_NAMEPATH_OP) {
508                         /*
509                          * If the name is null, this means that this is an
510                          * optional result parameter that was not specified
511                          * in the original ASL.  Create a Zero Constant for a
512                          * placeholder.  (Store to a constant is a Noop.)
513                          */
514                         opcode = AML_ZERO_OP;       /* Has no arguments! */
515
516                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Null namepath: Arg=%p\n", arg));
517                 }
518                 else {
519                         opcode = arg->common.aml_opcode;
520                 }
521
522                 /* Get the object type of the argument */
523
524                 op_info = acpi_ps_get_opcode_info (opcode);
525                 if (op_info->object_type == ACPI_TYPE_INVALID) {
526                         return_ACPI_STATUS (AE_NOT_IMPLEMENTED);
527                 }
528
529                 if (op_info->flags & AML_HAS_RETVAL) {
530                         ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH,
531                                 "Argument previously created, already stacked \n"));
532
533                         ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (
534                                 walk_state->operands [walk_state->num_operands - 1], walk_state));
535
536                         /*
537                          * Use value that was already previously returned
538                          * by the evaluation of this argument
539                          */
540                         status = acpi_ds_result_pop_from_bottom (&obj_desc, walk_state);
541                         if (ACPI_FAILURE (status)) {
542                                 /*
543                                  * Only error is underflow, and this indicates
544                                  * a missing or null operand!
545                                  */
546                                 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Missing or null operand, %s\n",
547                                         acpi_format_exception (status)));
548                                 return_ACPI_STATUS (status);
549                         }
550                 }
551                 else {
552                         /* Create an ACPI_INTERNAL_OBJECT for the argument */
553
554                         obj_desc = acpi_ut_create_internal_object (op_info->object_type);
555                         if (!obj_desc) {
556                                 return_ACPI_STATUS (AE_NO_MEMORY);
557                         }
558
559                         /* Initialize the new object */
560
561                         status = acpi_ds_init_object_from_op (walk_state, arg,
562                                          opcode, &obj_desc);
563                         if (ACPI_FAILURE (status)) {
564                                 acpi_ut_delete_object_desc (obj_desc);
565                                 return_ACPI_STATUS (status);
566                         }
567                 }
568
569                 /* Put the operand object on the object stack */
570
571                 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
572                 if (ACPI_FAILURE (status)) {
573                         return_ACPI_STATUS (status);
574                 }
575
576                 ACPI_DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
577         }
578
579         return_ACPI_STATUS (AE_OK);
580 }
581
582
583 /*******************************************************************************
584  *
585  * FUNCTION:    acpi_ds_create_operands
586  *
587  * PARAMETERS:  first_arg           - First argument of a parser argument tree
588  *
589  * RETURN:      Status
590  *
591  * DESCRIPTION: Convert an operator's arguments from a parse tree format to
592  *              namespace objects and place those argument object on the object
593  *              stack in preparation for evaluation by the interpreter.
594  *
595  ******************************************************************************/
596
597 acpi_status
598 acpi_ds_create_operands (
599         struct acpi_walk_state          *walk_state,
600         union acpi_parse_object         *first_arg)
601 {
602         acpi_status                     status = AE_OK;
603         union acpi_parse_object         *arg;
604         u32                             arg_count = 0;
605
606
607         ACPI_FUNCTION_TRACE_PTR ("ds_create_operands", first_arg);
608
609
610         /* For all arguments in the list... */
611
612         arg = first_arg;
613         while (arg) {
614                 status = acpi_ds_create_operand (walk_state, arg, arg_count);
615                 if (ACPI_FAILURE (status)) {
616                         goto cleanup;
617                 }
618
619                 ACPI_DEBUG_PRINT ((ACPI_DB_DISPATCH, "Arg #%d (%p) done, Arg1=%p\n",
620                         arg_count, arg, first_arg));
621
622                 /* Move on to next argument, if any */
623
624                 arg = arg->common.next;
625                 arg_count++;
626         }
627
628         return_ACPI_STATUS (status);
629
630
631 cleanup:
632         /*
633          * We must undo everything done above; meaning that we must
634          * pop everything off of the operand stack and delete those
635          * objects
636          */
637         (void) acpi_ds_obj_stack_pop_and_delete (arg_count, walk_state);
638
639         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "While creating Arg %d - %s\n",
640                 (arg_count + 1), acpi_format_exception (status)));
641         return_ACPI_STATUS (status);
642 }
643
644