xref: /OK3568_Linux_fs/kernel/drivers/acpi/acpica/exoparg6.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /******************************************************************************
3*4882a593Smuzhiyun  *
4*4882a593Smuzhiyun  * Module Name: exoparg6 - AML execution - opcodes with 6 arguments
5*4882a593Smuzhiyun  *
6*4882a593Smuzhiyun  * Copyright (C) 2000 - 2020, Intel Corp.
7*4882a593Smuzhiyun  *
8*4882a593Smuzhiyun  *****************************************************************************/
9*4882a593Smuzhiyun 
10*4882a593Smuzhiyun #include <acpi/acpi.h>
11*4882a593Smuzhiyun #include "accommon.h"
12*4882a593Smuzhiyun #include "acinterp.h"
13*4882a593Smuzhiyun #include "acparser.h"
14*4882a593Smuzhiyun #include "amlcode.h"
15*4882a593Smuzhiyun 
16*4882a593Smuzhiyun #define _COMPONENT          ACPI_EXECUTER
17*4882a593Smuzhiyun ACPI_MODULE_NAME("exoparg6")
18*4882a593Smuzhiyun 
19*4882a593Smuzhiyun /*!
20*4882a593Smuzhiyun  * Naming convention for AML interpreter execution routines.
21*4882a593Smuzhiyun  *
22*4882a593Smuzhiyun  * The routines that begin execution of AML opcodes are named with a common
23*4882a593Smuzhiyun  * convention based upon the number of arguments, the number of target operands,
24*4882a593Smuzhiyun  * and whether or not a value is returned:
25*4882a593Smuzhiyun  *
26*4882a593Smuzhiyun  *      AcpiExOpcode_xA_yT_zR
27*4882a593Smuzhiyun  *
28*4882a593Smuzhiyun  * Where:
29*4882a593Smuzhiyun  *
30*4882a593Smuzhiyun  * xA - ARGUMENTS:    The number of arguments (input operands) that are
31*4882a593Smuzhiyun  *                    required for this opcode type (1 through 6 args).
32*4882a593Smuzhiyun  * yT - TARGETS:      The number of targets (output operands) that are required
33*4882a593Smuzhiyun  *                    for this opcode type (0, 1, or 2 targets).
34*4882a593Smuzhiyun  * zR - RETURN VALUE: Indicates whether this opcode type returns a value
35*4882a593Smuzhiyun  *                    as the function return (0 or 1).
36*4882a593Smuzhiyun  *
37*4882a593Smuzhiyun  * The AcpiExOpcode* functions are called via the Dispatcher component with
38*4882a593Smuzhiyun  * fully resolved operands.
39*4882a593Smuzhiyun !*/
40*4882a593Smuzhiyun /* Local prototypes */
41*4882a593Smuzhiyun static u8
42*4882a593Smuzhiyun acpi_ex_do_match(u32 match_op,
43*4882a593Smuzhiyun 		 union acpi_operand_object *package_obj,
44*4882a593Smuzhiyun 		 union acpi_operand_object *match_obj);
45*4882a593Smuzhiyun 
46*4882a593Smuzhiyun /*******************************************************************************
47*4882a593Smuzhiyun  *
48*4882a593Smuzhiyun  * FUNCTION:    acpi_ex_do_match
49*4882a593Smuzhiyun  *
50*4882a593Smuzhiyun  * PARAMETERS:  match_op        - The AML match operand
51*4882a593Smuzhiyun  *              package_obj     - Object from the target package
52*4882a593Smuzhiyun  *              match_obj       - Object to be matched
53*4882a593Smuzhiyun  *
54*4882a593Smuzhiyun  * RETURN:      TRUE if the match is successful, FALSE otherwise
55*4882a593Smuzhiyun  *
56*4882a593Smuzhiyun  * DESCRIPTION: Implements the low-level match for the ASL Match operator.
57*4882a593Smuzhiyun  *              Package elements will be implicitly converted to the type of
58*4882a593Smuzhiyun  *              the match object (Integer/Buffer/String).
59*4882a593Smuzhiyun  *
60*4882a593Smuzhiyun  ******************************************************************************/
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun static u8
acpi_ex_do_match(u32 match_op,union acpi_operand_object * package_obj,union acpi_operand_object * match_obj)63*4882a593Smuzhiyun acpi_ex_do_match(u32 match_op,
64*4882a593Smuzhiyun 		 union acpi_operand_object *package_obj,
65*4882a593Smuzhiyun 		 union acpi_operand_object *match_obj)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun 	u8 logical_result = TRUE;
68*4882a593Smuzhiyun 	acpi_status status;
69*4882a593Smuzhiyun 
70*4882a593Smuzhiyun 	/*
71*4882a593Smuzhiyun 	 * Note: Since the package_obj/match_obj ordering is opposite to that of
72*4882a593Smuzhiyun 	 * the standard logical operators, we have to reverse them when we call
73*4882a593Smuzhiyun 	 * do_logical_op in order to make the implicit conversion rules work
74*4882a593Smuzhiyun 	 * correctly. However, this means we have to flip the entire equation
75*4882a593Smuzhiyun 	 * also. A bit ugly perhaps, but overall, better than fussing the
76*4882a593Smuzhiyun 	 * parameters around at runtime, over and over again.
77*4882a593Smuzhiyun 	 *
78*4882a593Smuzhiyun 	 * Below, P[i] refers to the package element, M refers to the Match object.
79*4882a593Smuzhiyun 	 */
80*4882a593Smuzhiyun 	switch (match_op) {
81*4882a593Smuzhiyun 	case MATCH_MTR:
82*4882a593Smuzhiyun 
83*4882a593Smuzhiyun 		/* Always true */
84*4882a593Smuzhiyun 
85*4882a593Smuzhiyun 		break;
86*4882a593Smuzhiyun 
87*4882a593Smuzhiyun 	case MATCH_MEQ:
88*4882a593Smuzhiyun 		/*
89*4882a593Smuzhiyun 		 * True if equal: (P[i] == M)
90*4882a593Smuzhiyun 		 * Change to:     (M == P[i])
91*4882a593Smuzhiyun 		 */
92*4882a593Smuzhiyun 		status =
93*4882a593Smuzhiyun 		    acpi_ex_do_logical_op(AML_LOGICAL_EQUAL_OP, match_obj,
94*4882a593Smuzhiyun 					  package_obj, &logical_result);
95*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
96*4882a593Smuzhiyun 			return (FALSE);
97*4882a593Smuzhiyun 		}
98*4882a593Smuzhiyun 		break;
99*4882a593Smuzhiyun 
100*4882a593Smuzhiyun 	case MATCH_MLE:
101*4882a593Smuzhiyun 		/*
102*4882a593Smuzhiyun 		 * True if less than or equal: (P[i] <= M) (P[i] not_greater than M)
103*4882a593Smuzhiyun 		 * Change to:                  (M >= P[i]) (M not_less than P[i])
104*4882a593Smuzhiyun 		 */
105*4882a593Smuzhiyun 		status =
106*4882a593Smuzhiyun 		    acpi_ex_do_logical_op(AML_LOGICAL_LESS_OP, match_obj,
107*4882a593Smuzhiyun 					  package_obj, &logical_result);
108*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
109*4882a593Smuzhiyun 			return (FALSE);
110*4882a593Smuzhiyun 		}
111*4882a593Smuzhiyun 		logical_result = (u8) ! logical_result;
112*4882a593Smuzhiyun 		break;
113*4882a593Smuzhiyun 
114*4882a593Smuzhiyun 	case MATCH_MLT:
115*4882a593Smuzhiyun 		/*
116*4882a593Smuzhiyun 		 * True if less than: (P[i] < M)
117*4882a593Smuzhiyun 		 * Change to:         (M > P[i])
118*4882a593Smuzhiyun 		 */
119*4882a593Smuzhiyun 		status =
120*4882a593Smuzhiyun 		    acpi_ex_do_logical_op(AML_LOGICAL_GREATER_OP, match_obj,
121*4882a593Smuzhiyun 					  package_obj, &logical_result);
122*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
123*4882a593Smuzhiyun 			return (FALSE);
124*4882a593Smuzhiyun 		}
125*4882a593Smuzhiyun 		break;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	case MATCH_MGE:
128*4882a593Smuzhiyun 		/*
129*4882a593Smuzhiyun 		 * True if greater than or equal: (P[i] >= M) (P[i] not_less than M)
130*4882a593Smuzhiyun 		 * Change to:                     (M <= P[i]) (M not_greater than P[i])
131*4882a593Smuzhiyun 		 */
132*4882a593Smuzhiyun 		status =
133*4882a593Smuzhiyun 		    acpi_ex_do_logical_op(AML_LOGICAL_GREATER_OP, match_obj,
134*4882a593Smuzhiyun 					  package_obj, &logical_result);
135*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
136*4882a593Smuzhiyun 			return (FALSE);
137*4882a593Smuzhiyun 		}
138*4882a593Smuzhiyun 		logical_result = (u8) ! logical_result;
139*4882a593Smuzhiyun 		break;
140*4882a593Smuzhiyun 
141*4882a593Smuzhiyun 	case MATCH_MGT:
142*4882a593Smuzhiyun 		/*
143*4882a593Smuzhiyun 		 * True if greater than: (P[i] > M)
144*4882a593Smuzhiyun 		 * Change to:            (M < P[i])
145*4882a593Smuzhiyun 		 */
146*4882a593Smuzhiyun 		status =
147*4882a593Smuzhiyun 		    acpi_ex_do_logical_op(AML_LOGICAL_LESS_OP, match_obj,
148*4882a593Smuzhiyun 					  package_obj, &logical_result);
149*4882a593Smuzhiyun 		if (ACPI_FAILURE(status)) {
150*4882a593Smuzhiyun 			return (FALSE);
151*4882a593Smuzhiyun 		}
152*4882a593Smuzhiyun 		break;
153*4882a593Smuzhiyun 
154*4882a593Smuzhiyun 	default:
155*4882a593Smuzhiyun 
156*4882a593Smuzhiyun 		/* Undefined */
157*4882a593Smuzhiyun 
158*4882a593Smuzhiyun 		return (FALSE);
159*4882a593Smuzhiyun 	}
160*4882a593Smuzhiyun 
161*4882a593Smuzhiyun 	return (logical_result);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun 
164*4882a593Smuzhiyun /*******************************************************************************
165*4882a593Smuzhiyun  *
166*4882a593Smuzhiyun  * FUNCTION:    acpi_ex_opcode_6A_0T_1R
167*4882a593Smuzhiyun  *
168*4882a593Smuzhiyun  * PARAMETERS:  walk_state          - Current walk state
169*4882a593Smuzhiyun  *
170*4882a593Smuzhiyun  * RETURN:      Status
171*4882a593Smuzhiyun  *
172*4882a593Smuzhiyun  * DESCRIPTION: Execute opcode with 6 arguments, no target, and a return value
173*4882a593Smuzhiyun  *
174*4882a593Smuzhiyun  ******************************************************************************/
175*4882a593Smuzhiyun 
acpi_ex_opcode_6A_0T_1R(struct acpi_walk_state * walk_state)176*4882a593Smuzhiyun acpi_status acpi_ex_opcode_6A_0T_1R(struct acpi_walk_state *walk_state)
177*4882a593Smuzhiyun {
178*4882a593Smuzhiyun 	union acpi_operand_object **operand = &walk_state->operands[0];
179*4882a593Smuzhiyun 	union acpi_operand_object *return_desc = NULL;
180*4882a593Smuzhiyun 	acpi_status status = AE_OK;
181*4882a593Smuzhiyun 	u64 index;
182*4882a593Smuzhiyun 	union acpi_operand_object *this_element;
183*4882a593Smuzhiyun 
184*4882a593Smuzhiyun 	ACPI_FUNCTION_TRACE_STR(ex_opcode_6A_0T_1R,
185*4882a593Smuzhiyun 				acpi_ps_get_opcode_name(walk_state->opcode));
186*4882a593Smuzhiyun 
187*4882a593Smuzhiyun 	switch (walk_state->opcode) {
188*4882a593Smuzhiyun 	case AML_MATCH_OP:
189*4882a593Smuzhiyun 		/*
190*4882a593Smuzhiyun 		 * Match (search_pkg[0], match_op1[1], match_obj1[2],
191*4882a593Smuzhiyun 		 *                      match_op2[3], match_obj2[4], start_index[5])
192*4882a593Smuzhiyun 		 */
193*4882a593Smuzhiyun 
194*4882a593Smuzhiyun 		/* Validate both Match Term Operators (MTR, MEQ, etc.) */
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		if ((operand[1]->integer.value > MAX_MATCH_OPERATOR) ||
197*4882a593Smuzhiyun 		    (operand[3]->integer.value > MAX_MATCH_OPERATOR)) {
198*4882a593Smuzhiyun 			ACPI_ERROR((AE_INFO, "Match operator out of range"));
199*4882a593Smuzhiyun 			status = AE_AML_OPERAND_VALUE;
200*4882a593Smuzhiyun 			goto cleanup;
201*4882a593Smuzhiyun 		}
202*4882a593Smuzhiyun 
203*4882a593Smuzhiyun 		/* Get the package start_index, validate against the package length */
204*4882a593Smuzhiyun 
205*4882a593Smuzhiyun 		index = operand[5]->integer.value;
206*4882a593Smuzhiyun 		if (index >= operand[0]->package.count) {
207*4882a593Smuzhiyun 			ACPI_ERROR((AE_INFO,
208*4882a593Smuzhiyun 				    "Index (0x%8.8X%8.8X) beyond package end (0x%X)",
209*4882a593Smuzhiyun 				    ACPI_FORMAT_UINT64(index),
210*4882a593Smuzhiyun 				    operand[0]->package.count));
211*4882a593Smuzhiyun 			status = AE_AML_PACKAGE_LIMIT;
212*4882a593Smuzhiyun 			goto cleanup;
213*4882a593Smuzhiyun 		}
214*4882a593Smuzhiyun 
215*4882a593Smuzhiyun 		/* Create an integer for the return value */
216*4882a593Smuzhiyun 		/* Default return value is ACPI_UINT64_MAX if no match found */
217*4882a593Smuzhiyun 
218*4882a593Smuzhiyun 		return_desc = acpi_ut_create_integer_object(ACPI_UINT64_MAX);
219*4882a593Smuzhiyun 		if (!return_desc) {
220*4882a593Smuzhiyun 			status = AE_NO_MEMORY;
221*4882a593Smuzhiyun 			goto cleanup;
222*4882a593Smuzhiyun 
223*4882a593Smuzhiyun 		}
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 		/*
226*4882a593Smuzhiyun 		 * Examine each element until a match is found. Both match conditions
227*4882a593Smuzhiyun 		 * must be satisfied for a match to occur. Within the loop,
228*4882a593Smuzhiyun 		 * "continue" signifies that the current element does not match
229*4882a593Smuzhiyun 		 * and the next should be examined.
230*4882a593Smuzhiyun 		 *
231*4882a593Smuzhiyun 		 * Upon finding a match, the loop will terminate via "break" at
232*4882a593Smuzhiyun 		 * the bottom. If it terminates "normally", match_value will be
233*4882a593Smuzhiyun 		 * ACPI_UINT64_MAX (Ones) (its initial value) indicating that no
234*4882a593Smuzhiyun 		 * match was found.
235*4882a593Smuzhiyun 		 */
236*4882a593Smuzhiyun 		for (; index < operand[0]->package.count; index++) {
237*4882a593Smuzhiyun 
238*4882a593Smuzhiyun 			/* Get the current package element */
239*4882a593Smuzhiyun 
240*4882a593Smuzhiyun 			this_element = operand[0]->package.elements[index];
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 			/* Treat any uninitialized (NULL) elements as non-matching */
243*4882a593Smuzhiyun 
244*4882a593Smuzhiyun 			if (!this_element) {
245*4882a593Smuzhiyun 				continue;
246*4882a593Smuzhiyun 			}
247*4882a593Smuzhiyun 
248*4882a593Smuzhiyun 			/*
249*4882a593Smuzhiyun 			 * Both match conditions must be satisfied. Execution of a continue
250*4882a593Smuzhiyun 			 * (proceed to next iteration of enclosing for loop) signifies a
251*4882a593Smuzhiyun 			 * non-match.
252*4882a593Smuzhiyun 			 */
253*4882a593Smuzhiyun 			if (!acpi_ex_do_match((u32) operand[1]->integer.value,
254*4882a593Smuzhiyun 					      this_element, operand[2])) {
255*4882a593Smuzhiyun 				continue;
256*4882a593Smuzhiyun 			}
257*4882a593Smuzhiyun 
258*4882a593Smuzhiyun 			if (!acpi_ex_do_match((u32) operand[3]->integer.value,
259*4882a593Smuzhiyun 					      this_element, operand[4])) {
260*4882a593Smuzhiyun 				continue;
261*4882a593Smuzhiyun 			}
262*4882a593Smuzhiyun 
263*4882a593Smuzhiyun 			/* Match found: Index is the return value */
264*4882a593Smuzhiyun 
265*4882a593Smuzhiyun 			return_desc->integer.value = index;
266*4882a593Smuzhiyun 			break;
267*4882a593Smuzhiyun 		}
268*4882a593Smuzhiyun 		break;
269*4882a593Smuzhiyun 
270*4882a593Smuzhiyun 	case AML_LOAD_TABLE_OP:
271*4882a593Smuzhiyun 
272*4882a593Smuzhiyun 		status = acpi_ex_load_table_op(walk_state, &return_desc);
273*4882a593Smuzhiyun 		break;
274*4882a593Smuzhiyun 
275*4882a593Smuzhiyun 	default:
276*4882a593Smuzhiyun 
277*4882a593Smuzhiyun 		ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
278*4882a593Smuzhiyun 			    walk_state->opcode));
279*4882a593Smuzhiyun 
280*4882a593Smuzhiyun 		status = AE_AML_BAD_OPCODE;
281*4882a593Smuzhiyun 		goto cleanup;
282*4882a593Smuzhiyun 	}
283*4882a593Smuzhiyun 
284*4882a593Smuzhiyun cleanup:
285*4882a593Smuzhiyun 
286*4882a593Smuzhiyun 	/* Delete return object on error */
287*4882a593Smuzhiyun 
288*4882a593Smuzhiyun 	if (ACPI_FAILURE(status)) {
289*4882a593Smuzhiyun 		acpi_ut_remove_reference(return_desc);
290*4882a593Smuzhiyun 	}
291*4882a593Smuzhiyun 
292*4882a593Smuzhiyun 	/* Save return object on success */
293*4882a593Smuzhiyun 
294*4882a593Smuzhiyun 	else {
295*4882a593Smuzhiyun 		walk_state->result_obj = return_desc;
296*4882a593Smuzhiyun 	}
297*4882a593Smuzhiyun 
298*4882a593Smuzhiyun 	return_ACPI_STATUS(status);
299*4882a593Smuzhiyun }
300