xref: /openbmc/linux/drivers/acpi/acpica/psloop.c (revision 927a6abf023b1a8c6940880802fc17026be6a351)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown  *
495b482a8SLen Brown  * Module Name: psloop - Main AML parse loop
595b482a8SLen Brown  *
6da6f8320SBob Moore  * Copyright (C) 2000 - 2018, Intel Corp.
795b482a8SLen Brown  *
895857638SErik Schmauss  *****************************************************************************/
995b482a8SLen Brown 
1095b482a8SLen Brown /*
1195b482a8SLen Brown  * Parse the AML and build an operation tree as most interpreters, (such as
1295b482a8SLen Brown  * Perl) do. Parsing is done by hand rather than with a YACC generated parser
1395b482a8SLen Brown  * to tightly constrain stack and dynamic memory usage. Parsing is kept
1495b482a8SLen Brown  * flexible and the code fairly compact by parsing based on a list of AML
1595b482a8SLen Brown  * opcode templates in aml_op_info[].
1695b482a8SLen Brown  */
1795b482a8SLen Brown 
1895b482a8SLen Brown #include <acpi/acpi.h>
19e2f7a777SLen Brown #include "accommon.h"
20ab6c5733SLv Zheng #include "acinterp.h"
21e2f7a777SLen Brown #include "acparser.h"
22e2f7a777SLen Brown #include "acdispat.h"
23e2f7a777SLen Brown #include "amlcode.h"
249cf7adecSBob Moore #include "acconvert.h"
254a7c94c7SErik Schmauss #include "acnamesp.h"
2695b482a8SLen Brown 
2795b482a8SLen Brown #define _COMPONENT          ACPI_PARSER
2895b482a8SLen Brown ACPI_MODULE_NAME("psloop")
2995b482a8SLen Brown 
3095b482a8SLen Brown /* Local prototypes */
3195b482a8SLen Brown static acpi_status
3295b482a8SLen Brown acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
3395b482a8SLen Brown 		      u8 * aml_op_start, union acpi_parse_object *op);
3495b482a8SLen Brown 
357f0c826aSLin Ming static void
369a884ab6SLin Ming acpi_ps_link_module_code(union acpi_parse_object *parent_op,
379a884ab6SLin Ming 			 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
387f0c826aSLin Ming 
3995b482a8SLen Brown /*******************************************************************************
4095b482a8SLen Brown  *
4195b482a8SLen Brown  * FUNCTION:    acpi_ps_get_arguments
4295b482a8SLen Brown  *
4395b482a8SLen Brown  * PARAMETERS:  walk_state          - Current state
4495b482a8SLen Brown  *              aml_op_start        - Op start in AML
45ba494beeSBob Moore  *              op                  - Current Op
4695b482a8SLen Brown  *
4795b482a8SLen Brown  * RETURN:      Status
4895b482a8SLen Brown  *
4995b482a8SLen Brown  * DESCRIPTION: Get arguments for passed Op.
5095b482a8SLen Brown  *
5195b482a8SLen Brown  ******************************************************************************/
5295b482a8SLen Brown 
5395b482a8SLen Brown static acpi_status
5495b482a8SLen Brown acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
5595b482a8SLen Brown 		      u8 * aml_op_start, union acpi_parse_object *op)
5695b482a8SLen Brown {
5795b482a8SLen Brown 	acpi_status status = AE_OK;
5895b482a8SLen Brown 	union acpi_parse_object *arg = NULL;
597f0c826aSLin Ming 	const struct acpi_opcode_info *op_info;
6095b482a8SLen Brown 
6195b482a8SLen Brown 	ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
6295b482a8SLen Brown 
63ce87e09dSBob Moore 	ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
64ce87e09dSBob Moore 			  "Get arguments for opcode [%s]\n",
65ce87e09dSBob Moore 			  op->common.aml_op_name));
66ce87e09dSBob Moore 
6795b482a8SLen Brown 	switch (op->common.aml_opcode) {
6895b482a8SLen Brown 	case AML_BYTE_OP:	/* AML_BYTEDATA_ARG */
6995b482a8SLen Brown 	case AML_WORD_OP:	/* AML_WORDDATA_ARG */
7095b482a8SLen Brown 	case AML_DWORD_OP:	/* AML_DWORDATA_ARG */
7195b482a8SLen Brown 	case AML_QWORD_OP:	/* AML_QWORDATA_ARG */
7295b482a8SLen Brown 	case AML_STRING_OP:	/* AML_ASCIICHARLIST_ARG */
7395b482a8SLen Brown 
7495b482a8SLen Brown 		/* Fill in constant or string argument directly */
7595b482a8SLen Brown 
7695b482a8SLen Brown 		acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
7795b482a8SLen Brown 					    GET_CURRENT_ARG_TYPE(walk_state->
7895b482a8SLen Brown 								 arg_types),
7995b482a8SLen Brown 					    op);
8095b482a8SLen Brown 		break;
8195b482a8SLen Brown 
8295b482a8SLen Brown 	case AML_INT_NAMEPATH_OP:	/* AML_NAMESTRING_ARG */
8395b482a8SLen Brown 
8489438f96SBob Moore 		status = acpi_ps_get_next_namepath(walk_state,
8589438f96SBob Moore 						   &(walk_state->parser_state),
8689438f96SBob Moore 						   op,
8789438f96SBob Moore 						   ACPI_POSSIBLE_METHOD_CALL);
8895b482a8SLen Brown 		if (ACPI_FAILURE(status)) {
8995b482a8SLen Brown 			return_ACPI_STATUS(status);
9095b482a8SLen Brown 		}
9195b482a8SLen Brown 
9295b482a8SLen Brown 		walk_state->arg_types = 0;
9395b482a8SLen Brown 		break;
9495b482a8SLen Brown 
9595b482a8SLen Brown 	default:
9695b482a8SLen Brown 		/*
9795b482a8SLen Brown 		 * Op is not a constant or string, append each argument to the Op
9895b482a8SLen Brown 		 */
991fad8738SBob Moore 		while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
1001fad8738SBob Moore 		       !walk_state->arg_count) {
10183482f75SLv Zheng 			walk_state->aml = walk_state->parser_state.aml;
10295b482a8SLen Brown 
1039cf7adecSBob Moore 			switch (op->common.aml_opcode) {
1049cf7adecSBob Moore 			case AML_METHOD_OP:
1059cf7adecSBob Moore 			case AML_BUFFER_OP:
1069cf7adecSBob Moore 			case AML_PACKAGE_OP:
1079cf7adecSBob Moore 			case AML_VARIABLE_PACKAGE_OP:
1089cf7adecSBob Moore 			case AML_WHILE_OP:
1099cf7adecSBob Moore 
1109cf7adecSBob Moore 				break;
1119cf7adecSBob Moore 
1129cf7adecSBob Moore 			default:
1139cf7adecSBob Moore 
1149cf7adecSBob Moore 				ASL_CV_CAPTURE_COMMENTS(walk_state);
1159cf7adecSBob Moore 				break;
1169cf7adecSBob Moore 			}
1179cf7adecSBob Moore 
11895b482a8SLen Brown 			status =
11995b482a8SLen Brown 			    acpi_ps_get_next_arg(walk_state,
12095b482a8SLen Brown 						 &(walk_state->parser_state),
12195b482a8SLen Brown 						 GET_CURRENT_ARG_TYPE
12295b482a8SLen Brown 						 (walk_state->arg_types), &arg);
12395b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
12495b482a8SLen Brown 				return_ACPI_STATUS(status);
12595b482a8SLen Brown 			}
12695b482a8SLen Brown 
12795b482a8SLen Brown 			if (arg) {
12895b482a8SLen Brown 				acpi_ps_append_arg(op, arg);
12995b482a8SLen Brown 			}
13095b482a8SLen Brown 
13195b482a8SLen Brown 			INCREMENT_ARG_LIST(walk_state->arg_types);
13295b482a8SLen Brown 		}
13395b482a8SLen Brown 
134a62a7117SBob Moore 		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
1351ef63231SBob Moore 				  "Final argument count: %8.8X pass %u\n",
136a62a7117SBob Moore 				  walk_state->arg_count,
137a62a7117SBob Moore 				  walk_state->pass_number));
138a62a7117SBob Moore 
1397f0c826aSLin Ming 		/*
140a406dea8SBob Moore 		 * This case handles the legacy option that groups all module-level
141a406dea8SBob Moore 		 * code blocks together and defers execution until all of the tables
142a406dea8SBob Moore 		 * are loaded. Execute all of these blocks at this time.
143a406dea8SBob Moore 		 * Execute any module-level code that was detected during the table
144a406dea8SBob Moore 		 * load phase.
145a406dea8SBob Moore 		 *
146a406dea8SBob Moore 		 * Note: this option is deprecated and will be eliminated in the
147a406dea8SBob Moore 		 * future. Use of this option can cause problems with AML code that
148a406dea8SBob Moore 		 * depends upon in-order immediate execution of module-level code.
1497f0c826aSLin Ming 		 */
15008930d56SErik Schmauss 		if (!acpi_gbl_execute_tables_as_methods &&
151a406dea8SBob Moore 		    (walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
15295b482a8SLen Brown 		    ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
15395b482a8SLen Brown 			/*
15495b482a8SLen Brown 			 * We want to skip If/Else/While constructs during Pass1 because we
15595b482a8SLen Brown 			 * want to actually conditionally execute the code during Pass2.
15695b482a8SLen Brown 			 *
15795b482a8SLen Brown 			 * Except for disassembly, where we always want to walk the
15895b482a8SLen Brown 			 * If/Else/While packages
15995b482a8SLen Brown 			 */
16095b482a8SLen Brown 			switch (op->common.aml_opcode) {
16195b482a8SLen Brown 			case AML_IF_OP:
16295b482a8SLen Brown 			case AML_ELSE_OP:
16395b482a8SLen Brown 			case AML_WHILE_OP:
1647f0c826aSLin Ming 				/*
1657f0c826aSLin Ming 				 * Currently supported module-level opcodes are:
1667f0c826aSLin Ming 				 * IF/ELSE/WHILE. These appear to be the most common,
1677f0c826aSLin Ming 				 * and easiest to support since they open an AML
1687f0c826aSLin Ming 				 * package.
1697f0c826aSLin Ming 				 */
1707f0c826aSLin Ming 				if (walk_state->pass_number ==
1717f0c826aSLin Ming 				    ACPI_IMODE_LOAD_PASS1) {
1729a884ab6SLin Ming 					acpi_ps_link_module_code(op->common.
1739a884ab6SLin Ming 								 parent,
1749a884ab6SLin Ming 								 aml_op_start,
1759a884ab6SLin Ming 								 (u32)
1769a884ab6SLin Ming 								 (walk_state->
1777f0c826aSLin Ming 								 parser_state.
1787f0c826aSLin Ming 								 pkg_end -
1799a884ab6SLin Ming 								 aml_op_start),
1807f0c826aSLin Ming 								 walk_state->
1817f0c826aSLin Ming 								 owner_id);
1827f0c826aSLin Ming 				}
1837f0c826aSLin Ming 
18495b482a8SLen Brown 				ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
18595b482a8SLen Brown 						  "Pass1: Skipping an If/Else/While body\n"));
18695b482a8SLen Brown 
18795b482a8SLen Brown 				/* Skip body of if/else/while in pass 1 */
18895b482a8SLen Brown 
18995b482a8SLen Brown 				walk_state->parser_state.aml =
19095b482a8SLen Brown 				    walk_state->parser_state.pkg_end;
19195b482a8SLen Brown 				walk_state->arg_count = 0;
19295b482a8SLen Brown 				break;
19395b482a8SLen Brown 
19495b482a8SLen Brown 			default:
1957f0c826aSLin Ming 				/*
1967f0c826aSLin Ming 				 * Check for an unsupported executable opcode at module
1977f0c826aSLin Ming 				 * level. We must be in PASS1, the parent must be a SCOPE,
1987f0c826aSLin Ming 				 * The opcode class must be EXECUTE, and the opcode must
1997f0c826aSLin Ming 				 * not be an argument to another opcode.
2007f0c826aSLin Ming 				 */
2017f0c826aSLin Ming 				if ((walk_state->pass_number ==
2027f0c826aSLin Ming 				     ACPI_IMODE_LOAD_PASS1)
2037f0c826aSLin Ming 				    && (op->common.parent->common.aml_opcode ==
2047f0c826aSLin Ming 					AML_SCOPE_OP)) {
2057f0c826aSLin Ming 					op_info =
2067f0c826aSLin Ming 					    acpi_ps_get_opcode_info(op->common.
2077f0c826aSLin Ming 								    aml_opcode);
2087f0c826aSLin Ming 					if ((op_info->class ==
2097f0c826aSLin Ming 					     AML_CLASS_EXECUTE) && (!arg)) {
2107f0c826aSLin Ming 						ACPI_WARNING((AE_INFO,
21100eb3255SBob Moore 							      "Unsupported module-level executable opcode "
21200eb3255SBob Moore 							      "0x%.2X at table offset 0x%.4X",
21300eb3255SBob Moore 							      op->common.
21400eb3255SBob Moore 							      aml_opcode,
21500eb3255SBob Moore 							      (u32)
21600eb3255SBob Moore 							      (ACPI_PTR_DIFF
21700eb3255SBob Moore 							       (aml_op_start,
21800eb3255SBob Moore 								walk_state->
21900eb3255SBob Moore 								parser_state.
22000eb3255SBob Moore 								aml_start) +
22100eb3255SBob Moore 							       sizeof(struct
22200eb3255SBob Moore 								      acpi_table_header))));
2237f0c826aSLin Ming 					}
2247f0c826aSLin Ming 				}
22595b482a8SLen Brown 				break;
22695b482a8SLen Brown 			}
22795b482a8SLen Brown 		}
2287f0c826aSLin Ming 
2297f0c826aSLin Ming 		/* Special processing for certain opcodes */
23095b482a8SLen Brown 
23195b482a8SLen Brown 		switch (op->common.aml_opcode) {
23295b482a8SLen Brown 		case AML_METHOD_OP:
23395b482a8SLen Brown 			/*
23495b482a8SLen Brown 			 * Skip parsing of control method because we don't have enough
23595b482a8SLen Brown 			 * info in the first pass to parse it correctly.
23695b482a8SLen Brown 			 *
23795b482a8SLen Brown 			 * Save the length and address of the body
23895b482a8SLen Brown 			 */
23995b482a8SLen Brown 			op->named.data = walk_state->parser_state.aml;
24095b482a8SLen Brown 			op->named.length = (u32)
24195b482a8SLen Brown 			    (walk_state->parser_state.pkg_end -
24295b482a8SLen Brown 			     walk_state->parser_state.aml);
24395b482a8SLen Brown 
24495b482a8SLen Brown 			/* Skip body of method */
24595b482a8SLen Brown 
24695b482a8SLen Brown 			walk_state->parser_state.aml =
24795b482a8SLen Brown 			    walk_state->parser_state.pkg_end;
24895b482a8SLen Brown 			walk_state->arg_count = 0;
24995b482a8SLen Brown 			break;
25095b482a8SLen Brown 
25195b482a8SLen Brown 		case AML_BUFFER_OP:
25295b482a8SLen Brown 		case AML_PACKAGE_OP:
2539ff5a21aSBob Moore 		case AML_VARIABLE_PACKAGE_OP:
25495b482a8SLen Brown 
25595b482a8SLen Brown 			if ((op->common.parent) &&
25695b482a8SLen Brown 			    (op->common.parent->common.aml_opcode ==
25795b482a8SLen Brown 			     AML_NAME_OP)
25895b482a8SLen Brown 			    && (walk_state->pass_number <=
25995b482a8SLen Brown 				ACPI_IMODE_LOAD_PASS2)) {
260a62a7117SBob Moore 				ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
261a62a7117SBob Moore 						  "Setup Package/Buffer: Pass %u, AML Ptr: %p\n",
262a62a7117SBob Moore 						  walk_state->pass_number,
263a62a7117SBob Moore 						  aml_op_start));
264a62a7117SBob Moore 
26595b482a8SLen Brown 				/*
26695b482a8SLen Brown 				 * Skip parsing of Buffers and Packages because we don't have
26795b482a8SLen Brown 				 * enough info in the first pass to parse them correctly.
26895b482a8SLen Brown 				 */
26995b482a8SLen Brown 				op->named.data = aml_op_start;
27095b482a8SLen Brown 				op->named.length = (u32)
27195b482a8SLen Brown 				    (walk_state->parser_state.pkg_end -
27295b482a8SLen Brown 				     aml_op_start);
27395b482a8SLen Brown 
27495b482a8SLen Brown 				/* Skip body */
27595b482a8SLen Brown 
27695b482a8SLen Brown 				walk_state->parser_state.aml =
27795b482a8SLen Brown 				    walk_state->parser_state.pkg_end;
27895b482a8SLen Brown 				walk_state->arg_count = 0;
27995b482a8SLen Brown 			}
28095b482a8SLen Brown 			break;
28195b482a8SLen Brown 
28295b482a8SLen Brown 		case AML_WHILE_OP:
28395b482a8SLen Brown 
28495b482a8SLen Brown 			if (walk_state->control_state) {
28595b482a8SLen Brown 				walk_state->control_state->control.package_end =
28695b482a8SLen Brown 				    walk_state->parser_state.pkg_end;
28795b482a8SLen Brown 			}
28895b482a8SLen Brown 			break;
28995b482a8SLen Brown 
29095b482a8SLen Brown 		default:
29195b482a8SLen Brown 
29295b482a8SLen Brown 			/* No action for all other opcodes */
2931d1ea1b7SChao Guan 
29495b482a8SLen Brown 			break;
29595b482a8SLen Brown 		}
29695b482a8SLen Brown 
29795b482a8SLen Brown 		break;
29895b482a8SLen Brown 	}
29995b482a8SLen Brown 
30095b482a8SLen Brown 	return_ACPI_STATUS(AE_OK);
30195b482a8SLen Brown }
30295b482a8SLen Brown 
30395b482a8SLen Brown /*******************************************************************************
30495b482a8SLen Brown  *
3057f0c826aSLin Ming  * FUNCTION:    acpi_ps_link_module_code
3067f0c826aSLin Ming  *
3079a884ab6SLin Ming  * PARAMETERS:  parent_op           - Parent parser op
3089a884ab6SLin Ming  *              aml_start           - Pointer to the AML
3097f0c826aSLin Ming  *              aml_length          - Length of executable AML
3107f0c826aSLin Ming  *              owner_id            - owner_id of module level code
3117f0c826aSLin Ming  *
3127f0c826aSLin Ming  * RETURN:      None.
3137f0c826aSLin Ming  *
3147f0c826aSLin Ming  * DESCRIPTION: Wrap the module-level code with a method object and link the
3157f0c826aSLin Ming  *              object to the global list. Note, the mutex field of the method
3167f0c826aSLin Ming  *              object is used to link multiple module-level code objects.
3177f0c826aSLin Ming  *
318a406dea8SBob Moore  * NOTE: In this legacy option, each block of detected executable AML
319a406dea8SBob Moore  * code that is outside of any control method is wrapped with a temporary
320a406dea8SBob Moore  * control method object and placed on a global list below.
321a406dea8SBob Moore  *
322a406dea8SBob Moore  * This function executes the module-level code for all tables only after
323a406dea8SBob Moore  * all of the tables have been loaded. It is a legacy option and is
324a406dea8SBob Moore  * not compatible with other ACPI implementations. See acpi_ns_load_table.
325a406dea8SBob Moore  *
326a406dea8SBob Moore  * This function will be removed when the legacy option is removed.
327a406dea8SBob Moore  *
3287f0c826aSLin Ming  ******************************************************************************/
3297f0c826aSLin Ming 
3307f0c826aSLin Ming static void
3319a884ab6SLin Ming acpi_ps_link_module_code(union acpi_parse_object *parent_op,
3329a884ab6SLin Ming 			 u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
3337f0c826aSLin Ming {
3347f0c826aSLin Ming 	union acpi_operand_object *prev;
3357f0c826aSLin Ming 	union acpi_operand_object *next;
3367f0c826aSLin Ming 	union acpi_operand_object *method_obj;
3379a884ab6SLin Ming 	struct acpi_namespace_node *parent_node;
3387f0c826aSLin Ming 
33925823e78SBob Moore 	ACPI_FUNCTION_TRACE(ps_link_module_code);
34025823e78SBob Moore 
3417f0c826aSLin Ming 	/* Get the tail of the list */
3427f0c826aSLin Ming 
3437f0c826aSLin Ming 	prev = next = acpi_gbl_module_code_list;
3447f0c826aSLin Ming 	while (next) {
3457f0c826aSLin Ming 		prev = next;
3467f0c826aSLin Ming 		next = next->method.mutex;
3477f0c826aSLin Ming 	}
3487f0c826aSLin Ming 
3497f0c826aSLin Ming 	/*
3507f0c826aSLin Ming 	 * Insert the module level code into the list. Merge it if it is
3517f0c826aSLin Ming 	 * adjacent to the previous element.
3527f0c826aSLin Ming 	 */
3537f0c826aSLin Ming 	if (!prev ||
3547f0c826aSLin Ming 	    ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
3557f0c826aSLin Ming 
3567f0c826aSLin Ming 		/* Create, initialize, and link a new temporary method object */
3577f0c826aSLin Ming 
3587f0c826aSLin Ming 		method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
3597f0c826aSLin Ming 		if (!method_obj) {
36025823e78SBob Moore 			return_VOID;
3617f0c826aSLin Ming 		}
3627f0c826aSLin Ming 
36325823e78SBob Moore 		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
36425823e78SBob Moore 				  "Create/Link new code block: %p\n",
36525823e78SBob Moore 				  method_obj));
36625823e78SBob Moore 
3679a884ab6SLin Ming 		if (parent_op->common.node) {
3689a884ab6SLin Ming 			parent_node = parent_op->common.node;
3699a884ab6SLin Ming 		} else {
3709a884ab6SLin Ming 			parent_node = acpi_gbl_root_node;
3719a884ab6SLin Ming 		}
3729a884ab6SLin Ming 
3737f0c826aSLin Ming 		method_obj->method.aml_start = aml_start;
3747f0c826aSLin Ming 		method_obj->method.aml_length = aml_length;
3757f0c826aSLin Ming 		method_obj->method.owner_id = owner_id;
37626294842SLin Ming 		method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
3777f0c826aSLin Ming 
3789a884ab6SLin Ming 		/*
3799a884ab6SLin Ming 		 * Save the parent node in next_object. This is cheating, but we
3809a884ab6SLin Ming 		 * don't want to expand the method object.
3819a884ab6SLin Ming 		 */
3829a884ab6SLin Ming 		method_obj->method.next_object =
3839a884ab6SLin Ming 		    ACPI_CAST_PTR(union acpi_operand_object, parent_node);
3849a884ab6SLin Ming 
3857f0c826aSLin Ming 		if (!prev) {
3867f0c826aSLin Ming 			acpi_gbl_module_code_list = method_obj;
3877f0c826aSLin Ming 		} else {
3887f0c826aSLin Ming 			prev->method.mutex = method_obj;
3897f0c826aSLin Ming 		}
3907f0c826aSLin Ming 	} else {
39125823e78SBob Moore 		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
39225823e78SBob Moore 				  "Appending to existing code block: %p\n",
39325823e78SBob Moore 				  prev));
39425823e78SBob Moore 
3957f0c826aSLin Ming 		prev->method.aml_length += aml_length;
3967f0c826aSLin Ming 	}
39725823e78SBob Moore 
39825823e78SBob Moore 	return_VOID;
3997f0c826aSLin Ming }
4007f0c826aSLin Ming 
4017f0c826aSLin Ming /*******************************************************************************
4027f0c826aSLin Ming  *
40395b482a8SLen Brown  * FUNCTION:    acpi_ps_parse_loop
40495b482a8SLen Brown  *
40595b482a8SLen Brown  * PARAMETERS:  walk_state          - Current state
40695b482a8SLen Brown  *
40795b482a8SLen Brown  * RETURN:      Status
40895b482a8SLen Brown  *
40995b482a8SLen Brown  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
41095b482a8SLen Brown  *              a tree of ops.
41195b482a8SLen Brown  *
41295b482a8SLen Brown  ******************************************************************************/
41395b482a8SLen Brown 
41495b482a8SLen Brown acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
41595b482a8SLen Brown {
41695b482a8SLen Brown 	acpi_status status = AE_OK;
41795b482a8SLen Brown 	union acpi_parse_object *op = NULL;	/* current op */
41895b482a8SLen Brown 	struct acpi_parse_state *parser_state;
41995b482a8SLen Brown 	u8 *aml_op_start = NULL;
420c64baa3aSErik Schmauss 	u8 opcode_length;
42195b482a8SLen Brown 
42295b482a8SLen Brown 	ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
42395b482a8SLen Brown 
42495b482a8SLen Brown 	if (walk_state->descending_callback == NULL) {
42595b482a8SLen Brown 		return_ACPI_STATUS(AE_BAD_PARAMETER);
42695b482a8SLen Brown 	}
42795b482a8SLen Brown 
42895b482a8SLen Brown 	parser_state = &walk_state->parser_state;
42995b482a8SLen Brown 	walk_state->arg_types = 0;
43095b482a8SLen Brown 
43195b482a8SLen Brown #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
43295b482a8SLen Brown 
43395b482a8SLen Brown 	if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
43495b482a8SLen Brown 
43595b482a8SLen Brown 		/* We are restarting a preempted control method */
43695b482a8SLen Brown 
43795b482a8SLen Brown 		if (acpi_ps_has_completed_scope(parser_state)) {
43895b482a8SLen Brown 			/*
43995b482a8SLen Brown 			 * We must check if a predicate to an IF or WHILE statement
44095b482a8SLen Brown 			 * was just completed
44195b482a8SLen Brown 			 */
44295b482a8SLen Brown 			if ((parser_state->scope->parse_scope.op) &&
44395b482a8SLen Brown 			    ((parser_state->scope->parse_scope.op->common.
44495b482a8SLen Brown 			      aml_opcode == AML_IF_OP)
44595b482a8SLen Brown 			     || (parser_state->scope->parse_scope.op->common.
44695b482a8SLen Brown 				 aml_opcode == AML_WHILE_OP))
44795b482a8SLen Brown 			    && (walk_state->control_state)
44895b482a8SLen Brown 			    && (walk_state->control_state->common.state ==
44995b482a8SLen Brown 				ACPI_CONTROL_PREDICATE_EXECUTING)) {
45095b482a8SLen Brown 				/*
45195b482a8SLen Brown 				 * A predicate was just completed, get the value of the
45295b482a8SLen Brown 				 * predicate and branch based on that value
45395b482a8SLen Brown 				 */
45495b482a8SLen Brown 				walk_state->op = NULL;
45595b482a8SLen Brown 				status =
45695b482a8SLen Brown 				    acpi_ds_get_predicate_value(walk_state,
45795b482a8SLen Brown 								ACPI_TO_POINTER
45895b482a8SLen Brown 								(TRUE));
45995b482a8SLen Brown 				if (ACPI_FAILURE(status)
46095b482a8SLen Brown 				    && ((status & AE_CODE_MASK) !=
46195b482a8SLen Brown 					AE_CODE_CONTROL)) {
46295b482a8SLen Brown 					if (status == AE_AML_NO_RETURN_VALUE) {
46395b482a8SLen Brown 						ACPI_EXCEPTION((AE_INFO, status,
46495b482a8SLen Brown 								"Invoked method did not return a value"));
46595b482a8SLen Brown 					}
46695b482a8SLen Brown 
46795b482a8SLen Brown 					ACPI_EXCEPTION((AE_INFO, status,
46895b482a8SLen Brown 							"GetPredicate Failed"));
46995b482a8SLen Brown 					return_ACPI_STATUS(status);
47095b482a8SLen Brown 				}
47195b482a8SLen Brown 
47295b482a8SLen Brown 				status =
47395b482a8SLen Brown 				    acpi_ps_next_parse_state(walk_state, op,
47495b482a8SLen Brown 							     status);
47595b482a8SLen Brown 			}
47695b482a8SLen Brown 
47795b482a8SLen Brown 			acpi_ps_pop_scope(parser_state, &op,
47895b482a8SLen Brown 					  &walk_state->arg_types,
47995b482a8SLen Brown 					  &walk_state->arg_count);
48095b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
48195b482a8SLen Brown 					  "Popped scope, Op=%p\n", op));
48295b482a8SLen Brown 		} else if (walk_state->prev_op) {
48395b482a8SLen Brown 
48495b482a8SLen Brown 			/* We were in the middle of an op */
48595b482a8SLen Brown 
48695b482a8SLen Brown 			op = walk_state->prev_op;
48795b482a8SLen Brown 			walk_state->arg_types = walk_state->prev_arg_types;
48895b482a8SLen Brown 		}
48995b482a8SLen Brown 	}
49095b482a8SLen Brown #endif
49195b482a8SLen Brown 
49295b482a8SLen Brown 	/* Iterative parsing loop, while there is more AML to process: */
49395b482a8SLen Brown 
49495b482a8SLen Brown 	while ((parser_state->aml < parser_state->aml_end) || (op)) {
4959cf7adecSBob Moore 		ASL_CV_CAPTURE_COMMENTS(walk_state);
4969cf7adecSBob Moore 
49795b482a8SLen Brown 		aml_op_start = parser_state->aml;
49895b482a8SLen Brown 		if (!op) {
49995b482a8SLen Brown 			status =
50095b482a8SLen Brown 			    acpi_ps_create_op(walk_state, aml_op_start, &op);
50195b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
50273c2a01cSSchmauss, Erik 				/*
50373c2a01cSSchmauss, Erik 				 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
50473c2a01cSSchmauss, Erik 				 * executing it as a control method. However, if we encounter
50573c2a01cSSchmauss, Erik 				 * an error while loading the table, we need to keep trying to
50673c2a01cSSchmauss, Erik 				 * load the table rather than aborting the table load. Set the
50773c2a01cSSchmauss, Erik 				 * status to AE_OK to proceed with the table load.
50873c2a01cSSchmauss, Erik 				 */
50973c2a01cSSchmauss, Erik 				if ((walk_state->
51073c2a01cSSchmauss, Erik 				     parse_flags & ACPI_PARSE_MODULE_LEVEL)
511*927a6abfSBob Moore 				    && ((status == AE_ALREADY_EXISTS)
512*927a6abfSBob Moore 					|| (status == AE_NOT_FOUND))) {
51373c2a01cSSchmauss, Erik 					status = AE_OK;
51473c2a01cSSchmauss, Erik 				}
51595b482a8SLen Brown 				if (status == AE_CTRL_PARSE_CONTINUE) {
51695b482a8SLen Brown 					continue;
51795b482a8SLen Brown 				}
51895b482a8SLen Brown 
51995b482a8SLen Brown 				if (status == AE_CTRL_PARSE_PENDING) {
52095b482a8SLen Brown 					status = AE_OK;
52195b482a8SLen Brown 				}
52295b482a8SLen Brown 
52322b5afceSBob Moore 				if (status == AE_CTRL_TERMINATE) {
52422b5afceSBob Moore 					return_ACPI_STATUS(status);
52522b5afceSBob Moore 				}
52622b5afceSBob Moore 
52795b482a8SLen Brown 				status =
52895b482a8SLen Brown 				    acpi_ps_complete_op(walk_state, &op,
52995b482a8SLen Brown 							status);
53095b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
53195b482a8SLen Brown 					return_ACPI_STATUS(status);
53295b482a8SLen Brown 				}
5334a7c94c7SErik Schmauss 				if (acpi_ns_opens_scope
5344a7c94c7SErik Schmauss 				    (acpi_ps_get_opcode_info
5354a7c94c7SErik Schmauss 				     (walk_state->opcode)->object_type)) {
5365088814aSErik Schmauss 					/*
5374a7c94c7SErik Schmauss 					 * If the scope/device op fails to parse, skip the body of
5384a7c94c7SErik Schmauss 					 * the scope op because the parse failure indicates that
5394a7c94c7SErik Schmauss 					 * the device may not exist.
5405088814aSErik Schmauss 					 */
541*927a6abfSBob Moore 					ACPI_INFO(("Skipping parse of AML opcode: %s (0x%4.4X)", acpi_ps_get_opcode_name(walk_state->opcode), walk_state->opcode));
542c64baa3aSErik Schmauss 
543c64baa3aSErik Schmauss 					/*
544c64baa3aSErik Schmauss 					 * Determine the opcode length before skipping the opcode.
545c64baa3aSErik Schmauss 					 * An opcode can be 1 byte or 2 bytes in length.
546c64baa3aSErik Schmauss 					 */
547c64baa3aSErik Schmauss 					opcode_length = 1;
548c64baa3aSErik Schmauss 					if ((walk_state->opcode & 0xFF00) ==
549c64baa3aSErik Schmauss 					    AML_EXTENDED_OPCODE) {
550c64baa3aSErik Schmauss 						opcode_length = 2;
551c64baa3aSErik Schmauss 					}
5525088814aSErik Schmauss 					walk_state->parser_state.aml =
553c64baa3aSErik Schmauss 					    walk_state->aml + opcode_length;
554c64baa3aSErik Schmauss 
5555088814aSErik Schmauss 					walk_state->parser_state.aml =
5565088814aSErik Schmauss 					    acpi_ps_get_next_package_end
5575088814aSErik Schmauss 					    (&walk_state->parser_state);
5585088814aSErik Schmauss 					walk_state->aml =
5595088814aSErik Schmauss 					    walk_state->parser_state.aml;
5605088814aSErik Schmauss 				}
56195b482a8SLen Brown 
56295b482a8SLen Brown 				continue;
56395b482a8SLen Brown 			}
56495b482a8SLen Brown 
565ab6c5733SLv Zheng 			acpi_ex_start_trace_opcode(op, walk_state);
56695b482a8SLen Brown 		}
56795b482a8SLen Brown 
56895b482a8SLen Brown 		/*
56995b482a8SLen Brown 		 * Start arg_count at zero because we don't know if there are
57095b482a8SLen Brown 		 * any args yet
57195b482a8SLen Brown 		 */
57295b482a8SLen Brown 		walk_state->arg_count = 0;
57395b482a8SLen Brown 
5749cf7adecSBob Moore 		switch (op->common.aml_opcode) {
5759cf7adecSBob Moore 		case AML_BYTE_OP:
5769cf7adecSBob Moore 		case AML_WORD_OP:
5779cf7adecSBob Moore 		case AML_DWORD_OP:
5789cf7adecSBob Moore 		case AML_QWORD_OP:
5799cf7adecSBob Moore 
5809cf7adecSBob Moore 			break;
5819cf7adecSBob Moore 
5829cf7adecSBob Moore 		default:
5839cf7adecSBob Moore 
5849cf7adecSBob Moore 			ASL_CV_CAPTURE_COMMENTS(walk_state);
5859cf7adecSBob Moore 			break;
5869cf7adecSBob Moore 		}
5879cf7adecSBob Moore 
58895b482a8SLen Brown 		/* Are there any arguments that must be processed? */
58995b482a8SLen Brown 
59095b482a8SLen Brown 		if (walk_state->arg_types) {
59195b482a8SLen Brown 
59295b482a8SLen Brown 			/* Get arguments */
59395b482a8SLen Brown 
59495b482a8SLen Brown 			status =
59595b482a8SLen Brown 			    acpi_ps_get_arguments(walk_state, aml_op_start, op);
59695b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
59795b482a8SLen Brown 				status =
59895b482a8SLen Brown 				    acpi_ps_complete_op(walk_state, &op,
59995b482a8SLen Brown 							status);
60095b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
60195b482a8SLen Brown 					return_ACPI_STATUS(status);
60295b482a8SLen Brown 				}
6035088814aSErik Schmauss 				if ((walk_state->control_state) &&
6045088814aSErik Schmauss 				    ((walk_state->control_state->control.
6055088814aSErik Schmauss 				      opcode == AML_IF_OP)
6065088814aSErik Schmauss 				     || (walk_state->control_state->control.
6075088814aSErik Schmauss 					 opcode == AML_WHILE_OP))) {
6085088814aSErik Schmauss 					/*
6095088814aSErik Schmauss 					 * If the if/while op fails to parse, we will skip parsing
6105088814aSErik Schmauss 					 * the body of the op.
6115088814aSErik Schmauss 					 */
6125088814aSErik Schmauss 					parser_state->aml =
6135088814aSErik Schmauss 					    walk_state->control_state->control.
6145088814aSErik Schmauss 					    aml_predicate_start + 1;
6155088814aSErik Schmauss 					parser_state->aml =
6165088814aSErik Schmauss 					    acpi_ps_get_next_package_end
6175088814aSErik Schmauss 					    (parser_state);
6185088814aSErik Schmauss 					walk_state->aml = parser_state->aml;
61995b482a8SLen Brown 
6205088814aSErik Schmauss 					ACPI_ERROR((AE_INFO,
6215088814aSErik Schmauss 						    "Skipping While/If block"));
6225088814aSErik Schmauss 					if (*walk_state->aml == AML_ELSE_OP) {
6235088814aSErik Schmauss 						ACPI_ERROR((AE_INFO,
6245088814aSErik Schmauss 							    "Skipping Else block"));
6255088814aSErik Schmauss 						walk_state->parser_state.aml =
6265088814aSErik Schmauss 						    walk_state->aml + 1;
6275088814aSErik Schmauss 						walk_state->parser_state.aml =
6285088814aSErik Schmauss 						    acpi_ps_get_next_package_end
6295088814aSErik Schmauss 						    (parser_state);
6305088814aSErik Schmauss 						walk_state->aml =
6315088814aSErik Schmauss 						    parser_state->aml;
6325088814aSErik Schmauss 					}
6335088814aSErik Schmauss 					ACPI_FREE(acpi_ut_pop_generic_state
6345088814aSErik Schmauss 						  (&walk_state->control_state));
6355088814aSErik Schmauss 				}
6365088814aSErik Schmauss 				op = NULL;
63795b482a8SLen Brown 				continue;
63895b482a8SLen Brown 			}
63995b482a8SLen Brown 		}
64095b482a8SLen Brown 
64195b482a8SLen Brown 		/* Check for arguments that need to be processed */
64295b482a8SLen Brown 
643a62a7117SBob Moore 		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
6441ef63231SBob Moore 				  "Parseloop: argument count: %8.8X\n",
645a62a7117SBob Moore 				  walk_state->arg_count));
646a62a7117SBob Moore 
64795b482a8SLen Brown 		if (walk_state->arg_count) {
64895b482a8SLen Brown 			/*
64995b482a8SLen Brown 			 * There are arguments (complex ones), push Op and
65095b482a8SLen Brown 			 * prepare for argument
65195b482a8SLen Brown 			 */
65295b482a8SLen Brown 			status = acpi_ps_push_scope(parser_state, op,
65395b482a8SLen Brown 						    walk_state->arg_types,
65495b482a8SLen Brown 						    walk_state->arg_count);
65595b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
65695b482a8SLen Brown 				status =
65795b482a8SLen Brown 				    acpi_ps_complete_op(walk_state, &op,
65895b482a8SLen Brown 							status);
65995b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
66095b482a8SLen Brown 					return_ACPI_STATUS(status);
66195b482a8SLen Brown 				}
66295b482a8SLen Brown 
66395b482a8SLen Brown 				continue;
66495b482a8SLen Brown 			}
66595b482a8SLen Brown 
66695b482a8SLen Brown 			op = NULL;
66795b482a8SLen Brown 			continue;
66895b482a8SLen Brown 		}
66995b482a8SLen Brown 
67095b482a8SLen Brown 		/*
67195b482a8SLen Brown 		 * All arguments have been processed -- Op is complete,
67295b482a8SLen Brown 		 * prepare for next
67395b482a8SLen Brown 		 */
67495b482a8SLen Brown 		walk_state->op_info =
67595b482a8SLen Brown 		    acpi_ps_get_opcode_info(op->common.aml_opcode);
67695b482a8SLen Brown 		if (walk_state->op_info->flags & AML_NAMED) {
67795b482a8SLen Brown 			if (op->common.aml_opcode == AML_REGION_OP ||
67895b482a8SLen Brown 			    op->common.aml_opcode == AML_DATA_REGION_OP) {
67995b482a8SLen Brown 				/*
68095b482a8SLen Brown 				 * Skip parsing of control method or opregion body,
68195b482a8SLen Brown 				 * because we don't have enough info in the first pass
68295b482a8SLen Brown 				 * to parse them correctly.
68395b482a8SLen Brown 				 *
68495b482a8SLen Brown 				 * Completed parsing an op_region declaration, we now
68595b482a8SLen Brown 				 * know the length.
68695b482a8SLen Brown 				 */
68795b482a8SLen Brown 				op->named.length =
68895b482a8SLen Brown 				    (u32) (parser_state->aml - op->named.data);
68995b482a8SLen Brown 			}
69095b482a8SLen Brown 		}
69195b482a8SLen Brown 
69295b482a8SLen Brown 		if (walk_state->op_info->flags & AML_CREATE) {
69395b482a8SLen Brown 			/*
694ba494beeSBob Moore 			 * Backup to beginning of create_XXXfield declaration (1 for
69595b482a8SLen Brown 			 * Opcode)
69695b482a8SLen Brown 			 *
69795b482a8SLen Brown 			 * body_length is unknown until we parse the body
69895b482a8SLen Brown 			 */
69995b482a8SLen Brown 			op->named.length =
70095b482a8SLen Brown 			    (u32) (parser_state->aml - op->named.data);
70195b482a8SLen Brown 		}
70295b482a8SLen Brown 
70395b482a8SLen Brown 		if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
70495b482a8SLen Brown 			/*
70595b482a8SLen Brown 			 * Backup to beginning of bank_field declaration
70695b482a8SLen Brown 			 *
70795b482a8SLen Brown 			 * body_length is unknown until we parse the body
70895b482a8SLen Brown 			 */
70995b482a8SLen Brown 			op->named.length =
71095b482a8SLen Brown 			    (u32) (parser_state->aml - op->named.data);
71195b482a8SLen Brown 		}
71295b482a8SLen Brown 
71395b482a8SLen Brown 		/* This op complete, notify the dispatcher */
71495b482a8SLen Brown 
71595b482a8SLen Brown 		if (walk_state->ascending_callback != NULL) {
71695b482a8SLen Brown 			walk_state->op = op;
71795b482a8SLen Brown 			walk_state->opcode = op->common.aml_opcode;
71895b482a8SLen Brown 
71995b482a8SLen Brown 			status = walk_state->ascending_callback(walk_state);
72095b482a8SLen Brown 			status =
72195b482a8SLen Brown 			    acpi_ps_next_parse_state(walk_state, op, status);
72295b482a8SLen Brown 			if (status == AE_CTRL_PENDING) {
72395b482a8SLen Brown 				status = AE_OK;
72473c2a01cSSchmauss, Erik 			} else
72573c2a01cSSchmauss, Erik 			    if ((walk_state->
72673c2a01cSSchmauss, Erik 				 parse_flags & ACPI_PARSE_MODULE_LEVEL)
727d46b6537SErik Schmauss 				&& (ACPI_AML_EXCEPTION(status)
728d46b6537SErik Schmauss 				    || status == AE_ALREADY_EXISTS
729d46b6537SErik Schmauss 				    || status == AE_NOT_FOUND)) {
73073c2a01cSSchmauss, Erik 				/*
731d46b6537SErik Schmauss 				 * ACPI_PARSE_MODULE_LEVEL flag means that we
732d46b6537SErik Schmauss 				 * are currently loading a table by executing
733d46b6537SErik Schmauss 				 * it as a control method. However, if we
734d46b6537SErik Schmauss 				 * encounter an error while loading the table,
735d46b6537SErik Schmauss 				 * we need to keep trying to load the table
736d46b6537SErik Schmauss 				 * rather than aborting the table load (setting
737d46b6537SErik Schmauss 				 * the status to AE_OK continues the table
738d46b6537SErik Schmauss 				 * load). If we get a failure at this point, it
739d46b6537SErik Schmauss 				 * means that the dispatcher got an error while
740d46b6537SErik Schmauss 				 * trying to execute the Op.
74173c2a01cSSchmauss, Erik 				 */
74273c2a01cSSchmauss, Erik 				status = AE_OK;
74395b482a8SLen Brown 			}
74495b482a8SLen Brown 		}
74595b482a8SLen Brown 
74695b482a8SLen Brown 		status = acpi_ps_complete_op(walk_state, &op, status);
74795b482a8SLen Brown 		if (ACPI_FAILURE(status)) {
74895b482a8SLen Brown 			return_ACPI_STATUS(status);
74995b482a8SLen Brown 		}
75095b482a8SLen Brown 
75195b482a8SLen Brown 	}			/* while parser_state->Aml */
75295b482a8SLen Brown 
75395b482a8SLen Brown 	status = acpi_ps_complete_final_op(walk_state, op, status);
75495b482a8SLen Brown 	return_ACPI_STATUS(status);
75595b482a8SLen Brown }
756