- Kai Germaschewski: ISDN update (including Makefiles)
[davej-history.git] / drivers / acpi / dispatcher / dsutils.c
blob2efa43e5164bc7a0f7f00e728f645f6ab1c5f92f
1 /*******************************************************************************
3 * Module Name: dsutils - Dispatcher utilities
4 * $Revision: 50 $
6 ******************************************************************************/
8 /*
9 * Copyright (C) 2000 R. Byron Moore
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include "acpi.h"
28 #include "acparser.h"
29 #include "amlcode.h"
30 #include "acdispat.h"
31 #include "acinterp.h"
32 #include "acnamesp.h"
33 #include "acdebug.h"
35 #define _COMPONENT PARSER
36 MODULE_NAME ("dsutils")
39 /*******************************************************************************
41 * FUNCTION: Acpi_ds_is_result_used
43 * PARAMETERS: Op
44 * Result_obj
45 * Walk_state
47 * RETURN: Status
49 * DESCRIPTION: Check if a result object will be used by the parent
51 ******************************************************************************/
54 acpi_ds_is_result_used (
55 ACPI_PARSE_OBJECT *op,
56 ACPI_WALK_STATE *walk_state)
58 ACPI_OPCODE_INFO *parent_info;
61 /* Must have both an Op and a Result Object */
63 if (!op) {
64 return (TRUE);
69 * If there is no parent, the result can't possibly be used!
70 * (An executing method typically has no parent, since each
71 * method is parsed separately) However, a method that is
72 * invoked from another method has a parent.
74 if (!op->parent) {
75 return (FALSE);
80 * Get info on the parent. The root Op is AML_SCOPE
83 parent_info = acpi_ps_get_opcode_info (op->parent->opcode);
84 if (ACPI_GET_OP_TYPE (parent_info) != ACPI_OP_TYPE_OPCODE) {
85 return (FALSE);
90 * Decide what to do with the result based on the parent. If
91 * the parent opcode will not use the result, delete the object.
92 * Otherwise leave it as is, it will be deleted when it is used
93 * as an operand later.
96 switch (ACPI_GET_OP_CLASS (parent_info))
99 * In these cases, the parent will never use the return object
101 case OPTYPE_CONTROL: /* IF, ELSE, WHILE only */
103 switch (op->parent->opcode)
105 case AML_RETURN_OP:
107 /* Never delete the return value associated with a return opcode */
109 return (TRUE);
110 break;
112 case AML_IF_OP:
113 case AML_WHILE_OP:
116 * If we are executing the predicate AND this is the predicate op,
117 * we will use the return value!
120 if ((walk_state->control_state->common.state == CONTROL_PREDICATE_EXECUTING) &&
121 (walk_state->control_state->control.predicate_op == op))
123 return (TRUE);
126 break;
130 /* Fall through to not used case below */
133 case OPTYPE_NAMED_OBJECT: /* Scope, method, etc. */
136 * These opcodes allow Term_arg(s) as operands and therefore
137 * method calls. The result is used.
139 if ((op->parent->opcode == AML_REGION_OP) ||
140 (op->parent->opcode == AML_CREATE_FIELD_OP) ||
141 (op->parent->opcode == AML_BIT_FIELD_OP) ||
142 (op->parent->opcode == AML_BYTE_FIELD_OP) ||
143 (op->parent->opcode == AML_WORD_FIELD_OP) ||
144 (op->parent->opcode == AML_DWORD_FIELD_OP) ||
145 (op->parent->opcode == AML_QWORD_FIELD_OP))
147 return (TRUE);
150 return (FALSE);
151 break;
154 * In all other cases. the parent will actually use the return
155 * object, so keep it.
157 default:
158 break;
161 return (TRUE);
165 /*******************************************************************************
167 * FUNCTION: Acpi_ds_delete_result_if_not_used
169 * PARAMETERS: Op
170 * Result_obj
171 * Walk_state
173 * RETURN: Status
175 * DESCRIPTION: Used after interpretation of an opcode. If there is an internal
176 * result descriptor, check if the parent opcode will actually use
177 * this result. If not, delete the result now so that it will
178 * not become orphaned.
180 ******************************************************************************/
182 void
183 acpi_ds_delete_result_if_not_used (
184 ACPI_PARSE_OBJECT *op,
185 ACPI_OPERAND_OBJECT *result_obj,
186 ACPI_WALK_STATE *walk_state)
188 ACPI_OPERAND_OBJECT *obj_desc;
189 ACPI_STATUS status;
192 if (!op) {
193 return;
196 if (!result_obj) {
197 return;
201 if (!acpi_ds_is_result_used (op, walk_state)) {
203 * Must pop the result stack (Obj_desc should be equal
204 * to Result_obj)
207 status = acpi_ds_result_pop (&obj_desc, walk_state);
208 if (ACPI_SUCCESS (status)) {
209 acpi_cm_remove_reference (result_obj);
213 return;
217 /*******************************************************************************
219 * FUNCTION: Acpi_ds_create_operand
221 * PARAMETERS: Walk_state
222 * Arg
224 * RETURN: Status
226 * DESCRIPTION: Translate a parse tree object that is an argument to an AML
227 * opcode to the equivalent interpreter object. This may include
228 * looking up a name or entering a new name into the internal
229 * namespace.
231 ******************************************************************************/
233 ACPI_STATUS
234 acpi_ds_create_operand (
235 ACPI_WALK_STATE *walk_state,
236 ACPI_PARSE_OBJECT *arg,
237 u32 arg_index)
239 ACPI_STATUS status = AE_OK;
240 NATIVE_CHAR *name_string;
241 u32 name_length;
242 OBJECT_TYPE_INTERNAL data_type;
243 ACPI_OPERAND_OBJECT *obj_desc;
244 ACPI_PARSE_OBJECT *parent_op;
245 u16 opcode;
246 u32 flags;
247 OPERATING_MODE interpreter_mode;
250 /* A valid name must be looked up in the namespace */
252 if ((arg->opcode == AML_NAMEPATH_OP) &&
253 (arg->value.string))
255 /* Get the entire name string from the AML stream */
257 status = acpi_aml_get_name_string (ACPI_TYPE_ANY,
258 arg->value.buffer,
259 &name_string,
260 &name_length);
262 if (ACPI_FAILURE (status)) {
263 return (status);
267 * All prefixes have been handled, and the name is
268 * in Name_string
272 * Differentiate between a namespace "create" operation
273 * versus a "lookup" operation (IMODE_LOAD_PASS2 vs.
274 * IMODE_EXECUTE) in order to support the creation of
275 * namespace objects during the execution of control methods.
278 parent_op = arg->parent;
279 if ((acpi_ps_is_node_op (parent_op->opcode)) &&
280 (parent_op->opcode != AML_METHODCALL_OP) &&
281 (parent_op->opcode != AML_REGION_OP) &&
282 (parent_op->opcode != AML_NAMEPATH_OP))
284 /* Enter name into namespace if not found */
286 interpreter_mode = IMODE_LOAD_PASS2;
289 else {
290 /* Return a failure if name not found */
292 interpreter_mode = IMODE_EXECUTE;
295 status = acpi_ns_lookup (walk_state->scope_info, name_string,
296 ACPI_TYPE_ANY, interpreter_mode,
297 NS_SEARCH_PARENT | NS_DONT_OPEN_SCOPE,
298 walk_state,
299 (ACPI_NAMESPACE_NODE **) &obj_desc);
301 /* Free the namestring created above */
303 acpi_cm_free (name_string);
306 * The only case where we pass through (ignore) a NOT_FOUND
307 * error is for the Cond_ref_of opcode.
310 if (status == AE_NOT_FOUND) {
311 if (parent_op->opcode == AML_COND_REF_OF_OP) {
313 * For the Conditional Reference op, it's OK if
314 * the name is not found; We just need a way to
315 * indicate this to the interpreter, set the
316 * object to the root
318 obj_desc = (ACPI_OPERAND_OBJECT *) acpi_gbl_root_node;
319 status = AE_OK;
322 else {
324 * We just plain didn't find it -- which is a
325 * very serious error at this point
327 status = AE_AML_NAME_NOT_FOUND;
331 /* Check status from the lookup */
333 if (ACPI_FAILURE (status)) {
334 return (status);
337 /* Put the resulting object onto the current object stack */
339 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
340 if (ACPI_FAILURE (status)) {
341 return (status);
343 DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
347 else {
348 /* Check for null name case */
350 if (arg->opcode == AML_NAMEPATH_OP) {
352 * If the name is null, this means that this is an
353 * optional result parameter that was not specified
354 * in the original ASL. Create an Reference for a
355 * placeholder
357 opcode = AML_ZERO_OP; /* Has no arguments! */
360 * TBD: [Investigate] anything else needed for the
361 * zero op lvalue?
365 else {
366 opcode = arg->opcode;
370 /* Get the data type of the argument */
372 data_type = acpi_ds_map_opcode_to_data_type (opcode, &flags);
373 if (data_type == INTERNAL_TYPE_INVALID) {
374 return (AE_NOT_IMPLEMENTED);
377 if (flags & OP_HAS_RETURN_VALUE) {
378 DEBUGGER_EXEC (acpi_db_display_argument_object (walk_state->operands [walk_state->num_operands - 1], walk_state));
381 * Use value that was already previously returned
382 * by the evaluation of this argument
385 status = acpi_ds_result_pop_from_bottom (&obj_desc, walk_state);
386 if (ACPI_FAILURE (status)) {
388 * Only error is underflow, and this indicates
389 * a missing or null operand!
391 return (status);
396 else {
397 /* Create an ACPI_INTERNAL_OBJECT for the argument */
399 obj_desc = acpi_cm_create_internal_object (data_type);
400 if (!obj_desc) {
401 return (AE_NO_MEMORY);
404 /* Initialize the new object */
406 status = acpi_ds_init_object_from_op (walk_state, arg,
407 opcode, &obj_desc);
408 if (ACPI_FAILURE (status)) {
409 acpi_cm_delete_object_desc (obj_desc);
410 return (status);
414 /* Put the operand object on the object stack */
416 status = acpi_ds_obj_stack_push (obj_desc, walk_state);
417 if (ACPI_FAILURE (status)) {
418 return (status);
421 DEBUGGER_EXEC (acpi_db_display_argument_object (obj_desc, walk_state));
424 return (AE_OK);
428 /*******************************************************************************
430 * FUNCTION: Acpi_ds_create_operands
432 * PARAMETERS: First_arg - First argument of a parser argument tree
434 * RETURN: Status
436 * DESCRIPTION: Convert an operator's arguments from a parse tree format to
437 * namespace objects and place those argument object on the object
438 * stack in preparation for evaluation by the interpreter.
440 ******************************************************************************/
442 ACPI_STATUS
443 acpi_ds_create_operands (
444 ACPI_WALK_STATE *walk_state,
445 ACPI_PARSE_OBJECT *first_arg)
447 ACPI_STATUS status = AE_OK;
448 ACPI_PARSE_OBJECT *arg;
449 u32 arg_count = 0;
452 /* For all arguments in the list... */
454 arg = first_arg;
455 while (arg) {
456 status = acpi_ds_create_operand (walk_state, arg, arg_count);
457 if (ACPI_FAILURE (status)) {
458 goto cleanup;
461 /* Move on to next argument, if any */
463 arg = arg->next;
464 arg_count++;
467 return (status);
470 cleanup:
472 * We must undo everything done above; meaning that we must
473 * pop everything off of the operand stack and delete those
474 * objects
477 acpi_ds_obj_stack_pop_and_delete (arg_count, walk_state);
479 return (status);
483 /*******************************************************************************
485 * FUNCTION: Acpi_ds_resolve_operands
487 * PARAMETERS: Walk_state - Current walk state with operands on stack
489 * RETURN: Status
491 * DESCRIPTION: Resolve all operands to their values. Used to prepare
492 * arguments to a control method invocation (a call from one
493 * method to another.)
495 ******************************************************************************/
497 ACPI_STATUS
498 acpi_ds_resolve_operands (
499 ACPI_WALK_STATE *walk_state)
501 u32 i;
502 ACPI_STATUS status = AE_OK;
506 * Attempt to resolve each of the valid operands
507 * Method arguments are passed by value, not by reference
511 * TBD: [Investigate] Note from previous parser:
512 * Ref_of problem with Acpi_aml_resolve_to_value() conversion.
515 for (i = 0; i < walk_state->num_operands; i++) {
516 status = acpi_aml_resolve_to_value (&walk_state->operands[i], walk_state);
517 if (ACPI_FAILURE (status)) {
518 break;
522 return (status);
526 /*******************************************************************************
528 * FUNCTION: Acpi_ds_map_opcode_to_data_type
530 * PARAMETERS: Opcode - AML opcode to map
531 * Out_flags - Additional info about the opcode
533 * RETURN: The ACPI type associated with the opcode
535 * DESCRIPTION: Convert a raw AML opcode to the associated ACPI data type,
536 * if any. If the opcode returns a value as part of the
537 * intepreter execution, a flag is returned in Out_flags.
539 ******************************************************************************/
541 OBJECT_TYPE_INTERNAL
542 acpi_ds_map_opcode_to_data_type (
543 u16 opcode,
544 u32 *out_flags)
546 OBJECT_TYPE_INTERNAL data_type = INTERNAL_TYPE_INVALID;
547 ACPI_OPCODE_INFO *op_info;
548 u32 flags = 0;
551 op_info = acpi_ps_get_opcode_info (opcode);
552 if (ACPI_GET_OP_TYPE (op_info) != ACPI_OP_TYPE_OPCODE) {
553 /* Unknown opcode */
555 return (data_type);
558 switch (ACPI_GET_OP_CLASS (op_info))
561 case OPTYPE_LITERAL:
563 switch (opcode)
565 case AML_BYTE_OP:
566 case AML_WORD_OP:
567 case AML_DWORD_OP:
569 data_type = ACPI_TYPE_NUMBER;
570 break;
573 case AML_STRING_OP:
575 data_type = ACPI_TYPE_STRING;
576 break;
578 case AML_NAMEPATH_OP:
579 data_type = INTERNAL_TYPE_REFERENCE;
580 break;
582 default:
583 break;
585 break;
588 case OPTYPE_DATA_TERM:
590 switch (opcode)
592 case AML_BUFFER_OP:
594 data_type = ACPI_TYPE_BUFFER;
595 break;
597 case AML_PACKAGE_OP:
599 data_type = ACPI_TYPE_PACKAGE;
600 break;
602 default:
603 break;
605 break;
608 case OPTYPE_CONSTANT:
609 case OPTYPE_METHOD_ARGUMENT:
610 case OPTYPE_LOCAL_VARIABLE:
612 data_type = INTERNAL_TYPE_REFERENCE;
613 break;
616 case OPTYPE_MONADIC2:
617 case OPTYPE_MONADIC2_r:
618 case OPTYPE_DYADIC2:
619 case OPTYPE_DYADIC2_r:
620 case OPTYPE_DYADIC2_s:
621 case OPTYPE_INDEX:
622 case OPTYPE_MATCH:
623 case OPTYPE_RETURN:
625 flags = OP_HAS_RETURN_VALUE;
626 data_type = ACPI_TYPE_ANY;
627 break;
629 case OPTYPE_METHOD_CALL:
631 flags = OP_HAS_RETURN_VALUE;
632 data_type = ACPI_TYPE_METHOD;
633 break;
636 case OPTYPE_NAMED_OBJECT:
638 data_type = acpi_ds_map_named_opcode_to_data_type (opcode);
639 break;
642 case OPTYPE_DYADIC1:
643 case OPTYPE_CONTROL:
645 /* No mapping needed at this time */
647 break;
650 default:
652 break;
655 /* Return flags to caller if requested */
657 if (out_flags) {
658 *out_flags = flags;
661 return (data_type);
665 /*******************************************************************************
667 * FUNCTION: Acpi_ds_map_named_opcode_to_data_type
669 * PARAMETERS: Opcode - The Named AML opcode to map
671 * RETURN: The ACPI type associated with the named opcode
673 * DESCRIPTION: Convert a raw Named AML opcode to the associated data type.
674 * Named opcodes are a subsystem of the AML opcodes.
676 ******************************************************************************/
678 OBJECT_TYPE_INTERNAL
679 acpi_ds_map_named_opcode_to_data_type (
680 u16 opcode)
682 OBJECT_TYPE_INTERNAL data_type;
685 /* Decode Opcode */
687 switch (opcode)
689 case AML_SCOPE_OP:
690 data_type = INTERNAL_TYPE_SCOPE;
691 break;
693 case AML_DEVICE_OP:
694 data_type = ACPI_TYPE_DEVICE;
695 break;
697 case AML_THERMAL_ZONE_OP:
698 data_type = ACPI_TYPE_THERMAL;
699 break;
701 case AML_METHOD_OP:
702 data_type = ACPI_TYPE_METHOD;
703 break;
705 case AML_POWER_RES_OP:
706 data_type = ACPI_TYPE_POWER;
707 break;
709 case AML_PROCESSOR_OP:
710 data_type = ACPI_TYPE_PROCESSOR;
711 break;
713 case AML_DEF_FIELD_OP: /* Def_field_op */
714 data_type = INTERNAL_TYPE_DEF_FIELD_DEFN;
715 break;
717 case AML_INDEX_FIELD_OP: /* Index_field_op */
718 data_type = INTERNAL_TYPE_INDEX_FIELD_DEFN;
719 break;
721 case AML_BANK_FIELD_OP: /* Bank_field_op */
722 data_type = INTERNAL_TYPE_BANK_FIELD_DEFN;
723 break;
725 case AML_NAMEDFIELD_OP: /* NO CASE IN ORIGINAL */
726 data_type = ACPI_TYPE_ANY;
727 break;
729 case AML_NAME_OP: /* Name_op - special code in original */
730 case AML_NAMEPATH_OP:
731 data_type = ACPI_TYPE_ANY;
732 break;
734 case AML_ALIAS_OP:
735 data_type = INTERNAL_TYPE_ALIAS;
736 break;
738 case AML_MUTEX_OP:
739 data_type = ACPI_TYPE_MUTEX;
740 break;
742 case AML_EVENT_OP:
743 data_type = ACPI_TYPE_EVENT;
744 break;
746 case AML_REGION_OP:
747 data_type = ACPI_TYPE_REGION;
748 break;
751 default:
752 data_type = ACPI_TYPE_ANY;
753 break;
757 return (data_type);