xref: /openbmc/linux/drivers/acpi/acpica/exoparg3.c (revision 1ac731c529cd4d6adbce134754b51ff7d822b145)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown  *
495b482a8SLen Brown  * Module Name: exoparg3 - AML execution - opcodes with 3 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("exoparg3")
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 /*******************************************************************************
4195b482a8SLen Brown  *
4295b482a8SLen Brown  * FUNCTION:    acpi_ex_opcode_3A_0T_0R
4395b482a8SLen Brown  *
4495b482a8SLen Brown  * PARAMETERS:  walk_state          - Current walk state
4595b482a8SLen Brown  *
4695b482a8SLen Brown  * RETURN:      Status
4795b482a8SLen Brown  *
4895b482a8SLen Brown  * DESCRIPTION: Execute Triadic operator (3 operands)
4995b482a8SLen Brown  *
5095b482a8SLen Brown  ******************************************************************************/
acpi_ex_opcode_3A_0T_0R(struct acpi_walk_state * walk_state)5195b482a8SLen Brown acpi_status acpi_ex_opcode_3A_0T_0R(struct acpi_walk_state *walk_state)
5295b482a8SLen Brown {
5395b482a8SLen Brown 	union acpi_operand_object **operand = &walk_state->operands[0];
5495b482a8SLen Brown 	struct acpi_signal_fatal_info *fatal;
5595b482a8SLen Brown 	acpi_status status = AE_OK;
5695b482a8SLen Brown 
5795b482a8SLen Brown 	ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_0T_0R,
5895b482a8SLen Brown 				acpi_ps_get_opcode_name(walk_state->opcode));
5995b482a8SLen Brown 
6095b482a8SLen Brown 	switch (walk_state->opcode) {
6195b482a8SLen Brown 	case AML_FATAL_OP:	/* Fatal (fatal_type fatal_code fatal_arg) */
6295b482a8SLen Brown 
6395b482a8SLen Brown 		ACPI_DEBUG_PRINT((ACPI_DB_INFO,
641fad8738SBob Moore 				  "FatalOp: Type %X Code %X Arg %X "
651fad8738SBob Moore 				  "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<\n",
6695b482a8SLen Brown 				  (u32)operand[0]->integer.value,
6795b482a8SLen Brown 				  (u32)operand[1]->integer.value,
6895b482a8SLen Brown 				  (u32)operand[2]->integer.value));
6995b482a8SLen Brown 
7095b482a8SLen Brown 		fatal = ACPI_ALLOCATE(sizeof(struct acpi_signal_fatal_info));
7195b482a8SLen Brown 		if (fatal) {
7295b482a8SLen Brown 			fatal->type = (u32) operand[0]->integer.value;
7395b482a8SLen Brown 			fatal->code = (u32) operand[1]->integer.value;
7495b482a8SLen Brown 			fatal->argument = (u32) operand[2]->integer.value;
7595b482a8SLen Brown 		}
7695b482a8SLen Brown 
7795b482a8SLen Brown 		/* Always signal the OS! */
7895b482a8SLen Brown 
7995b482a8SLen Brown 		status = acpi_os_signal(ACPI_SIGNAL_FATAL, fatal);
8095b482a8SLen Brown 
8195b482a8SLen Brown 		/* Might return while OS is shutting down, just continue */
8295b482a8SLen Brown 
8395b482a8SLen Brown 		ACPI_FREE(fatal);
8456a3d5e7SBob Moore 		goto cleanup;
8556a3d5e7SBob Moore 
8656a3d5e7SBob Moore 	case AML_EXTERNAL_OP:
8756a3d5e7SBob Moore 		/*
8856a3d5e7SBob Moore 		 * If the interpreter sees this opcode, just ignore it. The External
8956a3d5e7SBob Moore 		 * op is intended for use by disassemblers in order to properly
9056a3d5e7SBob Moore 		 * disassemble control method invocations. The opcode or group of
9156a3d5e7SBob Moore 		 * opcodes should be surrounded by an "if (0)" clause to ensure that
92b3aec972SDavid E. Box 		 * AML interpreters never see the opcode. Thus, something is
93b3aec972SDavid E. Box 		 * wrong if an external opcode ever gets here.
9456a3d5e7SBob Moore 		 */
95b3aec972SDavid E. Box 		ACPI_ERROR((AE_INFO, "Executed External Op"));
9656a3d5e7SBob Moore 		status = AE_OK;
9756a3d5e7SBob Moore 		goto cleanup;
9895b482a8SLen Brown 
9995b482a8SLen Brown 	default:
10095b482a8SLen Brown 
101f6a22b0bSBob Moore 		ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
10295b482a8SLen Brown 			    walk_state->opcode));
1031fad8738SBob Moore 
10495b482a8SLen Brown 		status = AE_AML_BAD_OPCODE;
10595b482a8SLen Brown 		goto cleanup;
10695b482a8SLen Brown 	}
10795b482a8SLen Brown 
10895b482a8SLen Brown cleanup:
10995b482a8SLen Brown 
11095b482a8SLen Brown 	return_ACPI_STATUS(status);
11195b482a8SLen Brown }
11295b482a8SLen Brown 
11395b482a8SLen Brown /*******************************************************************************
11495b482a8SLen Brown  *
11595b482a8SLen Brown  * FUNCTION:    acpi_ex_opcode_3A_1T_1R
11695b482a8SLen Brown  *
11795b482a8SLen Brown  * PARAMETERS:  walk_state          - Current walk state
11895b482a8SLen Brown  *
11995b482a8SLen Brown  * RETURN:      Status
12095b482a8SLen Brown  *
12195b482a8SLen Brown  * DESCRIPTION: Execute Triadic operator (3 operands)
12295b482a8SLen Brown  *
12395b482a8SLen Brown  ******************************************************************************/
12495b482a8SLen Brown 
acpi_ex_opcode_3A_1T_1R(struct acpi_walk_state * walk_state)12595b482a8SLen Brown acpi_status acpi_ex_opcode_3A_1T_1R(struct acpi_walk_state *walk_state)
12695b482a8SLen Brown {
12795b482a8SLen Brown 	union acpi_operand_object **operand = &walk_state->operands[0];
12895b482a8SLen Brown 	union acpi_operand_object *return_desc = NULL;
12995b482a8SLen Brown 	char *buffer = NULL;
13095b482a8SLen Brown 	acpi_status status = AE_OK;
1315df7e6cbSBob Moore 	u64 index;
13295b482a8SLen Brown 	acpi_size length;
13395b482a8SLen Brown 
13495b482a8SLen Brown 	ACPI_FUNCTION_TRACE_STR(ex_opcode_3A_1T_1R,
13595b482a8SLen Brown 				acpi_ps_get_opcode_name(walk_state->opcode));
13695b482a8SLen Brown 
13795b482a8SLen Brown 	switch (walk_state->opcode) {
13895b482a8SLen Brown 	case AML_MID_OP:	/* Mid (Source[0], Index[1], Length[2], Result[3]) */
13995b482a8SLen Brown 		/*
14095b482a8SLen Brown 		 * Create the return object. The Source operand is guaranteed to be
14195b482a8SLen Brown 		 * either a String or a Buffer, so just use its type.
14295b482a8SLen Brown 		 */
1433371c19cSBob Moore 		return_desc = acpi_ut_create_internal_object((operand[0])->
1443371c19cSBob Moore 							     common.type);
14595b482a8SLen Brown 		if (!return_desc) {
14695b482a8SLen Brown 			status = AE_NO_MEMORY;
14795b482a8SLen Brown 			goto cleanup;
14895b482a8SLen Brown 		}
14995b482a8SLen Brown 
15095b482a8SLen Brown 		/* Get the Integer values from the objects */
15195b482a8SLen Brown 
15295b482a8SLen Brown 		index = operand[1]->integer.value;
15395b482a8SLen Brown 		length = (acpi_size)operand[2]->integer.value;
15495b482a8SLen Brown 
15595b482a8SLen Brown 		/*
15695b482a8SLen Brown 		 * If the index is beyond the length of the String/Buffer, or if the
15795b482a8SLen Brown 		 * requested length is zero, return a zero-length String/Buffer
15895b482a8SLen Brown 		 */
15995b482a8SLen Brown 		if (index >= operand[0]->string.length) {
16095b482a8SLen Brown 			length = 0;
16195b482a8SLen Brown 		}
16295b482a8SLen Brown 
16395b482a8SLen Brown 		/* Truncate request if larger than the actual String/Buffer */
16495b482a8SLen Brown 
16595b482a8SLen Brown 		else if ((index + length) > operand[0]->string.length) {
1661fad8738SBob Moore 			length =
1671fad8738SBob Moore 			    (acpi_size)operand[0]->string.length -
16895b482a8SLen Brown 			    (acpi_size)index;
16995b482a8SLen Brown 		}
17095b482a8SLen Brown 
17195b482a8SLen Brown 		/* Strings always have a sub-pointer, not so for buffers */
17295b482a8SLen Brown 
1733371c19cSBob Moore 		switch ((operand[0])->common.type) {
17495b482a8SLen Brown 		case ACPI_TYPE_STRING:
17595b482a8SLen Brown 
17695b482a8SLen Brown 			/* Always allocate a new buffer for the String */
17795b482a8SLen Brown 
17895b482a8SLen Brown 			buffer = ACPI_ALLOCATE_ZEROED((acpi_size)length + 1);
17995b482a8SLen Brown 			if (!buffer) {
18095b482a8SLen Brown 				status = AE_NO_MEMORY;
18195b482a8SLen Brown 				goto cleanup;
18295b482a8SLen Brown 			}
18395b482a8SLen Brown 			break;
18495b482a8SLen Brown 
18595b482a8SLen Brown 		case ACPI_TYPE_BUFFER:
18695b482a8SLen Brown 
18795b482a8SLen Brown 			/* If the requested length is zero, don't allocate a buffer */
18895b482a8SLen Brown 
18995b482a8SLen Brown 			if (length > 0) {
19095b482a8SLen Brown 
19195b482a8SLen Brown 				/* Allocate a new buffer for the Buffer */
19295b482a8SLen Brown 
19395b482a8SLen Brown 				buffer = ACPI_ALLOCATE_ZEROED(length);
19495b482a8SLen Brown 				if (!buffer) {
19595b482a8SLen Brown 					status = AE_NO_MEMORY;
19695b482a8SLen Brown 					goto cleanup;
19795b482a8SLen Brown 				}
19895b482a8SLen Brown 			}
19995b482a8SLen Brown 			break;
20095b482a8SLen Brown 
20195b482a8SLen Brown 		default:	/* Should not happen */
20295b482a8SLen Brown 
20395b482a8SLen Brown 			status = AE_AML_OPERAND_TYPE;
20495b482a8SLen Brown 			goto cleanup;
20595b482a8SLen Brown 		}
20695b482a8SLen Brown 
20795b482a8SLen Brown 		if (buffer) {
20895b482a8SLen Brown 
20995b482a8SLen Brown 			/* We have a buffer, copy the portion requested */
21095b482a8SLen Brown 
2111fad8738SBob Moore 			memcpy(buffer,
2121fad8738SBob Moore 			       operand[0]->string.pointer + index, length);
21395b482a8SLen Brown 		}
21495b482a8SLen Brown 
21595b482a8SLen Brown 		/* Set the length of the new String/Buffer */
21695b482a8SLen Brown 
21795b482a8SLen Brown 		return_desc->string.pointer = buffer;
21895b482a8SLen Brown 		return_desc->string.length = (u32) length;
21995b482a8SLen Brown 
22095b482a8SLen Brown 		/* Mark buffer initialized */
22195b482a8SLen Brown 
22295b482a8SLen Brown 		return_desc->buffer.flags |= AOPOBJ_DATA_VALID;
22395b482a8SLen Brown 		break;
22495b482a8SLen Brown 
22595b482a8SLen Brown 	default:
22695b482a8SLen Brown 
227f6a22b0bSBob Moore 		ACPI_ERROR((AE_INFO, "Unknown AML opcode 0x%X",
22895b482a8SLen Brown 			    walk_state->opcode));
2291fad8738SBob Moore 
23095b482a8SLen Brown 		status = AE_AML_BAD_OPCODE;
23195b482a8SLen Brown 		goto cleanup;
23295b482a8SLen Brown 	}
23395b482a8SLen Brown 
23495b482a8SLen Brown 	/* Store the result in the target */
23595b482a8SLen Brown 
23695b482a8SLen Brown 	status = acpi_ex_store(return_desc, operand[3], walk_state);
23795b482a8SLen Brown 
23895b482a8SLen Brown cleanup:
23995b482a8SLen Brown 
24095b482a8SLen Brown 	/* Delete return object on error */
24195b482a8SLen Brown 
24295b482a8SLen Brown 	if (ACPI_FAILURE(status) || walk_state->result_obj) {
24395b482a8SLen Brown 		acpi_ut_remove_reference(return_desc);
24495b482a8SLen Brown 		walk_state->result_obj = NULL;
2451fad8738SBob Moore 	} else {
24695b482a8SLen Brown 		/* Set the return object and exit */
24795b482a8SLen Brown 
24895b482a8SLen Brown 		walk_state->result_obj = return_desc;
24995b482a8SLen Brown 	}
2501fad8738SBob Moore 
25195b482a8SLen Brown 	return_ACPI_STATUS(status);
25295b482a8SLen Brown }
253