xref: /openbmc/linux/drivers/acpi/acpica/psloop.c (revision 4a7c94c721074eafb27298d93dbcc339aa28e745)
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"
25*4a7c94c7SErik 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 		 */
150a406dea8SBob Moore 		if (acpi_gbl_group_module_level_code &&
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;
42095b482a8SLen Brown 
42195b482a8SLen Brown 	ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
42295b482a8SLen Brown 
42395b482a8SLen Brown 	if (walk_state->descending_callback == NULL) {
42495b482a8SLen Brown 		return_ACPI_STATUS(AE_BAD_PARAMETER);
42595b482a8SLen Brown 	}
42695b482a8SLen Brown 
42795b482a8SLen Brown 	parser_state = &walk_state->parser_state;
42895b482a8SLen Brown 	walk_state->arg_types = 0;
42995b482a8SLen Brown 
43095b482a8SLen Brown #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
43195b482a8SLen Brown 
43295b482a8SLen Brown 	if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
43395b482a8SLen Brown 
43495b482a8SLen Brown 		/* We are restarting a preempted control method */
43595b482a8SLen Brown 
43695b482a8SLen Brown 		if (acpi_ps_has_completed_scope(parser_state)) {
43795b482a8SLen Brown 			/*
43895b482a8SLen Brown 			 * We must check if a predicate to an IF or WHILE statement
43995b482a8SLen Brown 			 * was just completed
44095b482a8SLen Brown 			 */
44195b482a8SLen Brown 			if ((parser_state->scope->parse_scope.op) &&
44295b482a8SLen Brown 			    ((parser_state->scope->parse_scope.op->common.
44395b482a8SLen Brown 			      aml_opcode == AML_IF_OP)
44495b482a8SLen Brown 			     || (parser_state->scope->parse_scope.op->common.
44595b482a8SLen Brown 				 aml_opcode == AML_WHILE_OP))
44695b482a8SLen Brown 			    && (walk_state->control_state)
44795b482a8SLen Brown 			    && (walk_state->control_state->common.state ==
44895b482a8SLen Brown 				ACPI_CONTROL_PREDICATE_EXECUTING)) {
44995b482a8SLen Brown 				/*
45095b482a8SLen Brown 				 * A predicate was just completed, get the value of the
45195b482a8SLen Brown 				 * predicate and branch based on that value
45295b482a8SLen Brown 				 */
45395b482a8SLen Brown 				walk_state->op = NULL;
45495b482a8SLen Brown 				status =
45595b482a8SLen Brown 				    acpi_ds_get_predicate_value(walk_state,
45695b482a8SLen Brown 								ACPI_TO_POINTER
45795b482a8SLen Brown 								(TRUE));
45895b482a8SLen Brown 				if (ACPI_FAILURE(status)
45995b482a8SLen Brown 				    && ((status & AE_CODE_MASK) !=
46095b482a8SLen Brown 					AE_CODE_CONTROL)) {
46195b482a8SLen Brown 					if (status == AE_AML_NO_RETURN_VALUE) {
46295b482a8SLen Brown 						ACPI_EXCEPTION((AE_INFO, status,
46395b482a8SLen Brown 								"Invoked method did not return a value"));
46495b482a8SLen Brown 					}
46595b482a8SLen Brown 
46695b482a8SLen Brown 					ACPI_EXCEPTION((AE_INFO, status,
46795b482a8SLen Brown 							"GetPredicate Failed"));
46895b482a8SLen Brown 					return_ACPI_STATUS(status);
46995b482a8SLen Brown 				}
47095b482a8SLen Brown 
47195b482a8SLen Brown 				status =
47295b482a8SLen Brown 				    acpi_ps_next_parse_state(walk_state, op,
47395b482a8SLen Brown 							     status);
47495b482a8SLen Brown 			}
47595b482a8SLen Brown 
47695b482a8SLen Brown 			acpi_ps_pop_scope(parser_state, &op,
47795b482a8SLen Brown 					  &walk_state->arg_types,
47895b482a8SLen Brown 					  &walk_state->arg_count);
47995b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
48095b482a8SLen Brown 					  "Popped scope, Op=%p\n", op));
48195b482a8SLen Brown 		} else if (walk_state->prev_op) {
48295b482a8SLen Brown 
48395b482a8SLen Brown 			/* We were in the middle of an op */
48495b482a8SLen Brown 
48595b482a8SLen Brown 			op = walk_state->prev_op;
48695b482a8SLen Brown 			walk_state->arg_types = walk_state->prev_arg_types;
48795b482a8SLen Brown 		}
48895b482a8SLen Brown 	}
48995b482a8SLen Brown #endif
49095b482a8SLen Brown 
49195b482a8SLen Brown 	/* Iterative parsing loop, while there is more AML to process: */
49295b482a8SLen Brown 
49395b482a8SLen Brown 	while ((parser_state->aml < parser_state->aml_end) || (op)) {
4949cf7adecSBob Moore 		ASL_CV_CAPTURE_COMMENTS(walk_state);
4959cf7adecSBob Moore 
49695b482a8SLen Brown 		aml_op_start = parser_state->aml;
49795b482a8SLen Brown 		if (!op) {
49895b482a8SLen Brown 			status =
49995b482a8SLen Brown 			    acpi_ps_create_op(walk_state, aml_op_start, &op);
50095b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
50173c2a01cSSchmauss, Erik 				/*
50273c2a01cSSchmauss, Erik 				 * ACPI_PARSE_MODULE_LEVEL means that we are loading a table by
50373c2a01cSSchmauss, Erik 				 * executing it as a control method. However, if we encounter
50473c2a01cSSchmauss, Erik 				 * an error while loading the table, we need to keep trying to
50573c2a01cSSchmauss, Erik 				 * load the table rather than aborting the table load. Set the
50673c2a01cSSchmauss, Erik 				 * status to AE_OK to proceed with the table load.
50773c2a01cSSchmauss, Erik 				 */
50873c2a01cSSchmauss, Erik 				if ((walk_state->
50973c2a01cSSchmauss, Erik 				     parse_flags & ACPI_PARSE_MODULE_LEVEL)
51073c2a01cSSchmauss, Erik 				    && status == AE_ALREADY_EXISTS) {
51173c2a01cSSchmauss, Erik 					status = AE_OK;
51273c2a01cSSchmauss, Erik 				}
51395b482a8SLen Brown 				if (status == AE_CTRL_PARSE_CONTINUE) {
51495b482a8SLen Brown 					continue;
51595b482a8SLen Brown 				}
51695b482a8SLen Brown 
51795b482a8SLen Brown 				if (status == AE_CTRL_PARSE_PENDING) {
51895b482a8SLen Brown 					status = AE_OK;
51995b482a8SLen Brown 				}
52095b482a8SLen Brown 
52122b5afceSBob Moore 				if (status == AE_CTRL_TERMINATE) {
52222b5afceSBob Moore 					return_ACPI_STATUS(status);
52322b5afceSBob Moore 				}
52422b5afceSBob Moore 
52595b482a8SLen Brown 				status =
52695b482a8SLen Brown 				    acpi_ps_complete_op(walk_state, &op,
52795b482a8SLen Brown 							status);
52895b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
52995b482a8SLen Brown 					return_ACPI_STATUS(status);
53095b482a8SLen Brown 				}
531*4a7c94c7SErik Schmauss 				if (acpi_ns_opens_scope
532*4a7c94c7SErik Schmauss 				    (acpi_ps_get_opcode_info
533*4a7c94c7SErik Schmauss 				     (walk_state->opcode)->object_type)) {
5345088814aSErik Schmauss 					/*
535*4a7c94c7SErik Schmauss 					 * If the scope/device op fails to parse, skip the body of
536*4a7c94c7SErik Schmauss 					 * the scope op because the parse failure indicates that
537*4a7c94c7SErik Schmauss 					 * the device may not exist.
5385088814aSErik Schmauss 					 */
539*4a7c94c7SErik Schmauss 					ACPI_ERROR((AE_INFO,
540*4a7c94c7SErik Schmauss 						    "Skip parsing opcode %s",
541*4a7c94c7SErik Schmauss 						    acpi_ps_get_opcode_name
542*4a7c94c7SErik Schmauss 						    (walk_state->opcode)));
5435088814aSErik Schmauss 					walk_state->parser_state.aml =
5445088814aSErik Schmauss 					    walk_state->aml + 1;
5455088814aSErik Schmauss 					walk_state->parser_state.aml =
5465088814aSErik Schmauss 					    acpi_ps_get_next_package_end
5475088814aSErik Schmauss 					    (&walk_state->parser_state);
5485088814aSErik Schmauss 					walk_state->aml =
5495088814aSErik Schmauss 					    walk_state->parser_state.aml;
5505088814aSErik Schmauss 				}
55195b482a8SLen Brown 
55295b482a8SLen Brown 				continue;
55395b482a8SLen Brown 			}
55495b482a8SLen Brown 
555ab6c5733SLv Zheng 			acpi_ex_start_trace_opcode(op, walk_state);
55695b482a8SLen Brown 		}
55795b482a8SLen Brown 
55895b482a8SLen Brown 		/*
55995b482a8SLen Brown 		 * Start arg_count at zero because we don't know if there are
56095b482a8SLen Brown 		 * any args yet
56195b482a8SLen Brown 		 */
56295b482a8SLen Brown 		walk_state->arg_count = 0;
56395b482a8SLen Brown 
5649cf7adecSBob Moore 		switch (op->common.aml_opcode) {
5659cf7adecSBob Moore 		case AML_BYTE_OP:
5669cf7adecSBob Moore 		case AML_WORD_OP:
5679cf7adecSBob Moore 		case AML_DWORD_OP:
5689cf7adecSBob Moore 		case AML_QWORD_OP:
5699cf7adecSBob Moore 
5709cf7adecSBob Moore 			break;
5719cf7adecSBob Moore 
5729cf7adecSBob Moore 		default:
5739cf7adecSBob Moore 
5749cf7adecSBob Moore 			ASL_CV_CAPTURE_COMMENTS(walk_state);
5759cf7adecSBob Moore 			break;
5769cf7adecSBob Moore 		}
5779cf7adecSBob Moore 
57895b482a8SLen Brown 		/* Are there any arguments that must be processed? */
57995b482a8SLen Brown 
58095b482a8SLen Brown 		if (walk_state->arg_types) {
58195b482a8SLen Brown 
58295b482a8SLen Brown 			/* Get arguments */
58395b482a8SLen Brown 
58495b482a8SLen Brown 			status =
58595b482a8SLen Brown 			    acpi_ps_get_arguments(walk_state, aml_op_start, op);
58695b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
58795b482a8SLen Brown 				status =
58895b482a8SLen Brown 				    acpi_ps_complete_op(walk_state, &op,
58995b482a8SLen Brown 							status);
59095b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
59195b482a8SLen Brown 					return_ACPI_STATUS(status);
59295b482a8SLen Brown 				}
5935088814aSErik Schmauss 				if ((walk_state->control_state) &&
5945088814aSErik Schmauss 				    ((walk_state->control_state->control.
5955088814aSErik Schmauss 				      opcode == AML_IF_OP)
5965088814aSErik Schmauss 				     || (walk_state->control_state->control.
5975088814aSErik Schmauss 					 opcode == AML_WHILE_OP))) {
5985088814aSErik Schmauss 					/*
5995088814aSErik Schmauss 					 * If the if/while op fails to parse, we will skip parsing
6005088814aSErik Schmauss 					 * the body of the op.
6015088814aSErik Schmauss 					 */
6025088814aSErik Schmauss 					parser_state->aml =
6035088814aSErik Schmauss 					    walk_state->control_state->control.
6045088814aSErik Schmauss 					    aml_predicate_start + 1;
6055088814aSErik Schmauss 					parser_state->aml =
6065088814aSErik Schmauss 					    acpi_ps_get_next_package_end
6075088814aSErik Schmauss 					    (parser_state);
6085088814aSErik Schmauss 					walk_state->aml = parser_state->aml;
60995b482a8SLen Brown 
6105088814aSErik Schmauss 					ACPI_ERROR((AE_INFO,
6115088814aSErik Schmauss 						    "Skipping While/If block"));
6125088814aSErik Schmauss 					if (*walk_state->aml == AML_ELSE_OP) {
6135088814aSErik Schmauss 						ACPI_ERROR((AE_INFO,
6145088814aSErik Schmauss 							    "Skipping Else block"));
6155088814aSErik Schmauss 						walk_state->parser_state.aml =
6165088814aSErik Schmauss 						    walk_state->aml + 1;
6175088814aSErik Schmauss 						walk_state->parser_state.aml =
6185088814aSErik Schmauss 						    acpi_ps_get_next_package_end
6195088814aSErik Schmauss 						    (parser_state);
6205088814aSErik Schmauss 						walk_state->aml =
6215088814aSErik Schmauss 						    parser_state->aml;
6225088814aSErik Schmauss 					}
6235088814aSErik Schmauss 					ACPI_FREE(acpi_ut_pop_generic_state
6245088814aSErik Schmauss 						  (&walk_state->control_state));
6255088814aSErik Schmauss 				}
6265088814aSErik Schmauss 				op = NULL;
62795b482a8SLen Brown 				continue;
62895b482a8SLen Brown 			}
62995b482a8SLen Brown 		}
63095b482a8SLen Brown 
63195b482a8SLen Brown 		/* Check for arguments that need to be processed */
63295b482a8SLen Brown 
633a62a7117SBob Moore 		ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
6341ef63231SBob Moore 				  "Parseloop: argument count: %8.8X\n",
635a62a7117SBob Moore 				  walk_state->arg_count));
636a62a7117SBob Moore 
63795b482a8SLen Brown 		if (walk_state->arg_count) {
63895b482a8SLen Brown 			/*
63995b482a8SLen Brown 			 * There are arguments (complex ones), push Op and
64095b482a8SLen Brown 			 * prepare for argument
64195b482a8SLen Brown 			 */
64295b482a8SLen Brown 			status = acpi_ps_push_scope(parser_state, op,
64395b482a8SLen Brown 						    walk_state->arg_types,
64495b482a8SLen Brown 						    walk_state->arg_count);
64595b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
64695b482a8SLen Brown 				status =
64795b482a8SLen Brown 				    acpi_ps_complete_op(walk_state, &op,
64895b482a8SLen Brown 							status);
64995b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
65095b482a8SLen Brown 					return_ACPI_STATUS(status);
65195b482a8SLen Brown 				}
65295b482a8SLen Brown 
65395b482a8SLen Brown 				continue;
65495b482a8SLen Brown 			}
65595b482a8SLen Brown 
65695b482a8SLen Brown 			op = NULL;
65795b482a8SLen Brown 			continue;
65895b482a8SLen Brown 		}
65995b482a8SLen Brown 
66095b482a8SLen Brown 		/*
66195b482a8SLen Brown 		 * All arguments have been processed -- Op is complete,
66295b482a8SLen Brown 		 * prepare for next
66395b482a8SLen Brown 		 */
66495b482a8SLen Brown 		walk_state->op_info =
66595b482a8SLen Brown 		    acpi_ps_get_opcode_info(op->common.aml_opcode);
66695b482a8SLen Brown 		if (walk_state->op_info->flags & AML_NAMED) {
66795b482a8SLen Brown 			if (op->common.aml_opcode == AML_REGION_OP ||
66895b482a8SLen Brown 			    op->common.aml_opcode == AML_DATA_REGION_OP) {
66995b482a8SLen Brown 				/*
67095b482a8SLen Brown 				 * Skip parsing of control method or opregion body,
67195b482a8SLen Brown 				 * because we don't have enough info in the first pass
67295b482a8SLen Brown 				 * to parse them correctly.
67395b482a8SLen Brown 				 *
67495b482a8SLen Brown 				 * Completed parsing an op_region declaration, we now
67595b482a8SLen Brown 				 * know the length.
67695b482a8SLen Brown 				 */
67795b482a8SLen Brown 				op->named.length =
67895b482a8SLen Brown 				    (u32) (parser_state->aml - op->named.data);
67995b482a8SLen Brown 			}
68095b482a8SLen Brown 		}
68195b482a8SLen Brown 
68295b482a8SLen Brown 		if (walk_state->op_info->flags & AML_CREATE) {
68395b482a8SLen Brown 			/*
684ba494beeSBob Moore 			 * Backup to beginning of create_XXXfield declaration (1 for
68595b482a8SLen Brown 			 * Opcode)
68695b482a8SLen Brown 			 *
68795b482a8SLen Brown 			 * body_length is unknown until we parse the body
68895b482a8SLen Brown 			 */
68995b482a8SLen Brown 			op->named.length =
69095b482a8SLen Brown 			    (u32) (parser_state->aml - op->named.data);
69195b482a8SLen Brown 		}
69295b482a8SLen Brown 
69395b482a8SLen Brown 		if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
69495b482a8SLen Brown 			/*
69595b482a8SLen Brown 			 * Backup to beginning of bank_field declaration
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 		/* This op complete, notify the dispatcher */
70495b482a8SLen Brown 
70595b482a8SLen Brown 		if (walk_state->ascending_callback != NULL) {
70695b482a8SLen Brown 			walk_state->op = op;
70795b482a8SLen Brown 			walk_state->opcode = op->common.aml_opcode;
70895b482a8SLen Brown 
70995b482a8SLen Brown 			status = walk_state->ascending_callback(walk_state);
71095b482a8SLen Brown 			status =
71195b482a8SLen Brown 			    acpi_ps_next_parse_state(walk_state, op, status);
71295b482a8SLen Brown 			if (status == AE_CTRL_PENDING) {
71395b482a8SLen Brown 				status = AE_OK;
71473c2a01cSSchmauss, Erik 			} else
71573c2a01cSSchmauss, Erik 			    if ((walk_state->
71673c2a01cSSchmauss, Erik 				 parse_flags & ACPI_PARSE_MODULE_LEVEL)
717d46b6537SErik Schmauss 				&& (ACPI_AML_EXCEPTION(status)
718d46b6537SErik Schmauss 				    || status == AE_ALREADY_EXISTS
719d46b6537SErik Schmauss 				    || status == AE_NOT_FOUND)) {
72073c2a01cSSchmauss, Erik 				/*
721d46b6537SErik Schmauss 				 * ACPI_PARSE_MODULE_LEVEL flag means that we
722d46b6537SErik Schmauss 				 * are currently loading a table by executing
723d46b6537SErik Schmauss 				 * it as a control method. However, if we
724d46b6537SErik Schmauss 				 * encounter an error while loading the table,
725d46b6537SErik Schmauss 				 * we need to keep trying to load the table
726d46b6537SErik Schmauss 				 * rather than aborting the table load (setting
727d46b6537SErik Schmauss 				 * the status to AE_OK continues the table
728d46b6537SErik Schmauss 				 * load). If we get a failure at this point, it
729d46b6537SErik Schmauss 				 * means that the dispatcher got an error while
730d46b6537SErik Schmauss 				 * trying to execute the Op.
73173c2a01cSSchmauss, Erik 				 */
73273c2a01cSSchmauss, Erik 				status = AE_OK;
73395b482a8SLen Brown 			}
73495b482a8SLen Brown 		}
73595b482a8SLen Brown 
73695b482a8SLen Brown 		status = acpi_ps_complete_op(walk_state, &op, status);
73795b482a8SLen Brown 		if (ACPI_FAILURE(status)) {
73895b482a8SLen Brown 			return_ACPI_STATUS(status);
73995b482a8SLen Brown 		}
74095b482a8SLen Brown 
74195b482a8SLen Brown 	}			/* while parser_state->Aml */
74295b482a8SLen Brown 
74395b482a8SLen Brown 	status = acpi_ps_complete_final_op(walk_state, op, status);
74495b482a8SLen Brown 	return_ACPI_STATUS(status);
74595b482a8SLen Brown }
746