195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown *
495b482a8SLen Brown * Module Name: exoparg6 - AML execution - opcodes with 6 arguments
595b482a8SLen Brown *
6*612c2932SBob Moore * Copyright (C) 2000 - 2023, Intel Corp.
795b482a8SLen Brown *
895857638SErik Schmauss *****************************************************************************/
995b482a8SLen Brown
1095b482a8SLen Brown #include <acpi/acpi.h>
11e2f7a777SLen Brown #include "accommon.h"
12e2f7a777SLen Brown #include "acinterp.h"
13e2f7a777SLen Brown #include "acparser.h"
14e2f7a777SLen Brown #include "amlcode.h"
1595b482a8SLen Brown
1695b482a8SLen Brown #define _COMPONENT ACPI_EXECUTER
1795b482a8SLen Brown ACPI_MODULE_NAME("exoparg6")
1895b482a8SLen Brown
1995b482a8SLen Brown /*!
2095b482a8SLen Brown * Naming convention for AML interpreter execution routines.
2195b482a8SLen Brown *
2295b482a8SLen Brown * The routines that begin execution of AML opcodes are named with a common
2395b482a8SLen Brown * convention based upon the number of arguments, the number of target operands,
2495b482a8SLen Brown * and whether or not a value is returned:
2595b482a8SLen Brown *
2695b482a8SLen Brown * AcpiExOpcode_xA_yT_zR
2795b482a8SLen Brown *
2895b482a8SLen Brown * Where:
2995b482a8SLen Brown *
3095b482a8SLen Brown * xA - ARGUMENTS: The number of arguments (input operands) that are
3195b482a8SLen Brown * required for this opcode type (1 through 6 args).
3295b482a8SLen Brown * yT - TARGETS: The number of targets (output operands) that are required
3395b482a8SLen Brown * for this opcode type (0, 1, or 2 targets).
3495b482a8SLen Brown * zR - RETURN VALUE: Indicates whether this opcode type returns a value
3595b482a8SLen Brown * as the function return (0 or 1).
3695b482a8SLen Brown *
3795b482a8SLen Brown * The AcpiExOpcode* functions are called via the Dispatcher component with
3895b482a8SLen Brown * fully resolved operands.
3995b482a8SLen Brown !*/
4095b482a8SLen Brown /* Local prototypes */
4195b482a8SLen Brown static u8
4295b482a8SLen Brown acpi_ex_do_match(u32 match_op,
4395b482a8SLen Brown union acpi_operand_object *package_obj,
4495b482a8SLen Brown union acpi_operand_object *match_obj);
4595b482a8SLen Brown
4695b482a8SLen Brown /*******************************************************************************
4795b482a8SLen Brown *
4895b482a8SLen Brown * FUNCTION: acpi_ex_do_match
4995b482a8SLen Brown *
5095b482a8SLen Brown * PARAMETERS: match_op - The AML match operand
5195b482a8SLen Brown * package_obj - Object from the target package
5295b482a8SLen Brown * match_obj - Object to be matched
5395b482a8SLen Brown *
5495b482a8SLen Brown * RETURN: TRUE if the match is successful, FALSE otherwise
5595b482a8SLen Brown *
5695b482a8SLen Brown * DESCRIPTION: Implements the low-level match for the ASL Match operator.
5795b482a8SLen Brown * Package elements will be implicitly converted to the type of
5895b482a8SLen Brown * the match object (Integer/Buffer/String).
5995b482a8SLen Brown *
6095b482a8SLen Brown ******************************************************************************/
6195b482a8SLen Brown
6295b482a8SLen Brown static u8
acpi_ex_do_match(u32 match_op,union acpi_operand_object * package_obj,union acpi_operand_object * match_obj)6395b482a8SLen Brown acpi_ex_do_match(u32 match_op,
6495b482a8SLen Brown union acpi_operand_object *package_obj,
6595b482a8SLen Brown union acpi_operand_object *match_obj)
6695b482a8SLen Brown {
6795b482a8SLen Brown u8 logical_result = TRUE;
6895b482a8SLen Brown acpi_status status;
6995b482a8SLen Brown
7095b482a8SLen Brown /*
7195b482a8SLen Brown * Note: Since the package_obj/match_obj ordering is opposite to that of
7295b482a8SLen Brown * the standard logical operators, we have to reverse them when we call
7395b482a8SLen Brown * do_logical_op in order to make the implicit conversion rules work
7495b482a8SLen Brown * correctly. However, this means we have to flip the entire equation
7595b482a8SLen Brown * also. A bit ugly perhaps, but overall, better than fussing the
7695b482a8SLen Brown * parameters around at runtime, over and over again.
7795b482a8SLen Brown *
7895b482a8SLen Brown * Below, P[i] refers to the package element, M refers to the Match object.
7995b482a8SLen Brown */
8095b482a8SLen Brown switch (match_op) {
8195b482a8SLen Brown case MATCH_MTR:
8295b482a8SLen Brown
8395b482a8SLen Brown /* Always true */
8495b482a8SLen Brown
8595b482a8SLen Brown break;
8695b482a8SLen Brown
8795b482a8SLen Brown case MATCH_MEQ:
8895b482a8SLen Brown /*
8995b482a8SLen Brown * True if equal: (P[i] == M)
9095b482a8SLen Brown * Change to: (M == P[i])
9195b482a8SLen Brown */
9295b482a8SLen Brown status =
939ff5a21aSBob Moore acpi_ex_do_logical_op(AML_LOGICAL_EQUAL_OP, match_obj,
949ff5a21aSBob Moore package_obj, &logical_result);
9595b482a8SLen Brown if (ACPI_FAILURE(status)) {
9695b482a8SLen Brown return (FALSE);
9795b482a8SLen Brown }
9895b482a8SLen Brown break;
9995b482a8SLen Brown
10095b482a8SLen Brown case MATCH_MLE:
10195b482a8SLen Brown /*
10295b482a8SLen Brown * True if less than or equal: (P[i] <= M) (P[i] not_greater than M)
10395b482a8SLen Brown * Change to: (M >= P[i]) (M not_less than P[i])
10495b482a8SLen Brown */
10595b482a8SLen Brown status =
1069ff5a21aSBob Moore acpi_ex_do_logical_op(AML_LOGICAL_LESS_OP, match_obj,
1079ff5a21aSBob Moore package_obj, &logical_result);
10895b482a8SLen Brown if (ACPI_FAILURE(status)) {
10995b482a8SLen Brown return (FALSE);
11095b482a8SLen Brown }
11195b482a8SLen Brown logical_result = (u8) ! logical_result;
11295b482a8SLen Brown break;
11395b482a8SLen Brown
11495b482a8SLen Brown case MATCH_MLT:
11595b482a8SLen Brown /*
11695b482a8SLen Brown * True if less than: (P[i] < M)
11795b482a8SLen Brown * Change to: (M > P[i])
11895b482a8SLen Brown */
11995b482a8SLen Brown status =
1209ff5a21aSBob Moore acpi_ex_do_logical_op(AML_LOGICAL_GREATER_OP, match_obj,
12195b482a8SLen Brown package_obj, &logical_result);
12295b482a8SLen Brown if (ACPI_FAILURE(status)) {
12395b482a8SLen Brown return (FALSE);
12495b482a8SLen Brown }
12595b482a8SLen Brown break;
12695b482a8SLen Brown
12795b482a8SLen Brown case MATCH_MGE:
12895b482a8SLen Brown /*
12995b482a8SLen Brown * True if greater than or equal: (P[i] >= M) (P[i] not_less than M)
13095b482a8SLen Brown * Change to: (M <= P[i]) (M not_greater than P[i])
13195b482a8SLen Brown */
13295b482a8SLen Brown status =
1339ff5a21aSBob Moore acpi_ex_do_logical_op(AML_LOGICAL_GREATER_OP, match_obj,
13495b482a8SLen Brown package_obj, &logical_result);
13595b482a8SLen Brown if (ACPI_FAILURE(status)) {
13695b482a8SLen Brown return (FALSE);
13795b482a8SLen Brown }
13895b482a8SLen Brown logical_result = (u8) ! logical_result;
13995b482a8SLen Brown break;
14095b482a8SLen Brown
14195b482a8SLen Brown case MATCH_MGT:
14295b482a8SLen Brown /*
14395b482a8SLen Brown * True if greater than: (P[i] > M)
14495b482a8SLen Brown * Change to: (M < P[i])
14595b482a8SLen Brown */
14695b482a8SLen Brown status =
1479ff5a21aSBob Moore acpi_ex_do_logical_op(AML_LOGICAL_LESS_OP, match_obj,
1489ff5a21aSBob Moore package_obj, &logical_result);
14995b482a8SLen Brown if (ACPI_FAILURE(status)) {
15095b482a8SLen Brown return (FALSE);
15195b482a8SLen Brown }
15295b482a8SLen Brown break;
15395b482a8SLen Brown
15495b482a8SLen Brown default:
15595b482a8SLen Brown
15695b482a8SLen Brown /* Undefined */
15795b482a8SLen Brown
15895b482a8SLen Brown return (FALSE);
15995b482a8SLen Brown }
16095b482a8SLen Brown
16168aafc35SBob Moore return (logical_result);
16295b482a8SLen Brown }
16395b482a8SLen Brown
16495b482a8SLen Brown /*******************************************************************************
16595b482a8SLen Brown *
16695b482a8SLen Brown * FUNCTION: acpi_ex_opcode_6A_0T_1R
16795b482a8SLen Brown *
16895b482a8SLen Brown * PARAMETERS: walk_state - Current walk state
16995b482a8SLen Brown *
17095b482a8SLen Brown * RETURN: Status
17195b482a8SLen Brown *
17295b482a8SLen Brown * DESCRIPTION: Execute opcode with 6 arguments, no target, and a return value
17395b482a8SLen Brown *
17495b482a8SLen Brown ******************************************************************************/
17595b482a8SLen Brown
acpi_ex_opcode_6A_0T_1R(struct acpi_walk_state * walk_state)17695b482a8SLen Brown acpi_status acpi_ex_opcode_6A_0T_1R(struct acpi_walk_state *walk_state)
17795b482a8SLen Brown {
17895b482a8SLen Brown union acpi_operand_object **operand = &walk_state->operands[0];
17995b482a8SLen Brown union acpi_operand_object *return_desc = NULL;
18095b482a8SLen Brown acpi_status status = AE_OK;
1815df7e6cbSBob Moore u64 index;
18295b482a8SLen Brown union acpi_operand_object *this_element;
18395b482a8SLen Brown
18495b482a8SLen Brown ACPI_FUNCTION_TRACE_STR(ex_opcode_6A_0T_1R,
18595b482a8SLen Brown acpi_ps_get_opcode_name(walk_state->opcode));
18695b482a8SLen Brown
18795b482a8SLen Brown switch (walk_state->opcode) {
18895b482a8SLen Brown case AML_MATCH_OP:
18995b482a8SLen Brown /*
19095b482a8SLen Brown * Match (search_pkg[0], match_op1[1], match_obj1[2],
19195b482a8SLen Brown * match_op2[3], match_obj2[4], start_index[5])
19295b482a8SLen Brown */
19395b482a8SLen Brown
19495b482a8SLen Brown /* Validate both Match Term Operators (MTR, MEQ, etc.) */
19595b482a8SLen Brown
19695b482a8SLen Brown if ((operand[1]->integer.value > MAX_MATCH_OPERATOR) ||
19795b482a8SLen Brown (operand[3]->integer.value > MAX_MATCH_OPERATOR)) {
19895b482a8SLen Brown ACPI_ERROR((AE_INFO, "Match operator out of range"));
19995b482a8SLen Brown status = AE_AML_OPERAND_VALUE;
20095b482a8SLen Brown goto cleanup;
20195b482a8SLen Brown }
20295b482a8SLen Brown
20395b482a8SLen Brown /* Get the package start_index, validate against the package length */
20495b482a8SLen Brown
20595b482a8SLen Brown index = operand[5]->integer.value;
20695b482a8SLen Brown if (index >= operand[0]->package.count) {
20795b482a8SLen Brown ACPI_ERROR((AE_INFO,
208f6a22b0bSBob Moore "Index (0x%8.8X%8.8X) beyond package end (0x%X)",
20995b482a8SLen Brown ACPI_FORMAT_UINT64(index),
21095b482a8SLen Brown operand[0]->package.count));
21195b482a8SLen Brown status = AE_AML_PACKAGE_LIMIT;
21295b482a8SLen Brown goto cleanup;
21395b482a8SLen Brown }
21495b482a8SLen Brown
21595b482a8SLen Brown /* Create an integer for the return value */
2165df7e6cbSBob Moore /* Default return value is ACPI_UINT64_MAX if no match found */
21795b482a8SLen Brown
2185df7e6cbSBob Moore return_desc = acpi_ut_create_integer_object(ACPI_UINT64_MAX);
21995b482a8SLen Brown if (!return_desc) {
22095b482a8SLen Brown status = AE_NO_MEMORY;
22195b482a8SLen Brown goto cleanup;
22295b482a8SLen Brown
22395b482a8SLen Brown }
22495b482a8SLen Brown
22595b482a8SLen Brown /*
22695b482a8SLen Brown * Examine each element until a match is found. Both match conditions
22795b482a8SLen Brown * must be satisfied for a match to occur. Within the loop,
22895b482a8SLen Brown * "continue" signifies that the current element does not match
22995b482a8SLen Brown * and the next should be examined.
23095b482a8SLen Brown *
23195b482a8SLen Brown * Upon finding a match, the loop will terminate via "break" at
23295b482a8SLen Brown * the bottom. If it terminates "normally", match_value will be
2335df7e6cbSBob Moore * ACPI_UINT64_MAX (Ones) (its initial value) indicating that no
23495b482a8SLen Brown * match was found.
23595b482a8SLen Brown */
23695b482a8SLen Brown for (; index < operand[0]->package.count; index++) {
23795b482a8SLen Brown
23895b482a8SLen Brown /* Get the current package element */
23995b482a8SLen Brown
24095b482a8SLen Brown this_element = operand[0]->package.elements[index];
24195b482a8SLen Brown
24295b482a8SLen Brown /* Treat any uninitialized (NULL) elements as non-matching */
24395b482a8SLen Brown
24495b482a8SLen Brown if (!this_element) {
24595b482a8SLen Brown continue;
24695b482a8SLen Brown }
24795b482a8SLen Brown
24895b482a8SLen Brown /*
24995b482a8SLen Brown * Both match conditions must be satisfied. Execution of a continue
25095b482a8SLen Brown * (proceed to next iteration of enclosing for loop) signifies a
25195b482a8SLen Brown * non-match.
25295b482a8SLen Brown */
25395b482a8SLen Brown if (!acpi_ex_do_match((u32) operand[1]->integer.value,
25495b482a8SLen Brown this_element, operand[2])) {
25595b482a8SLen Brown continue;
25695b482a8SLen Brown }
25795b482a8SLen Brown
25895b482a8SLen Brown if (!acpi_ex_do_match((u32) operand[3]->integer.value,
25995b482a8SLen Brown this_element, operand[4])) {
26095b482a8SLen Brown continue;
26195b482a8SLen Brown }
26295b482a8SLen Brown
26395b482a8SLen Brown /* Match found: Index is the return value */
26495b482a8SLen Brown
26595b482a8SLen Brown return_desc->integer.value = index;
26695b482a8SLen Brown break;
26795b482a8SLen Brown }
26895b482a8SLen Brown break;
26995b482a8SLen Brown
27095b482a8SLen Brown case AML_LOAD_TABLE_OP:
27195b482a8SLen Brown
27295b482a8SLen Brown status = acpi_ex_load_table_op(walk_state, &return_desc);
27395b482a8SLen Brown break;
27495b482a8SLen Brown
27595b482a8SLen Brown default:
27695b482a8SLen Brown
277f6a22b0bSBob Moore ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
27895b482a8SLen Brown walk_state->opcode));
2791fad8738SBob Moore
28095b482a8SLen Brown status = AE_AML_BAD_OPCODE;
28195b482a8SLen Brown goto cleanup;
28295b482a8SLen Brown }
28395b482a8SLen Brown
28495b482a8SLen Brown cleanup:
28595b482a8SLen Brown
28695b482a8SLen Brown /* Delete return object on error */
28795b482a8SLen Brown
28895b482a8SLen Brown if (ACPI_FAILURE(status)) {
28995b482a8SLen Brown acpi_ut_remove_reference(return_desc);
29095b482a8SLen Brown }
29195b482a8SLen Brown
29295b482a8SLen Brown /* Save return object on success */
29395b482a8SLen Brown
29495b482a8SLen Brown else {
29595b482a8SLen Brown walk_state->result_obj = return_desc;
29695b482a8SLen Brown }
29795b482a8SLen Brown
29895b482a8SLen Brown return_ACPI_STATUS(status);
29995b482a8SLen Brown }
300