xref: /openbmc/linux/drivers/acpi/acpica/dswload.c (revision 612c2932)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown  *
49ad19ac4SBob Moore  * Module Name: dswload - Dispatcher first pass namespace load callbacks
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 "acparser.h"
13e2f7a777SLen Brown #include "amlcode.h"
14e2f7a777SLen Brown #include "acdispat.h"
15e2f7a777SLen Brown #include "acinterp.h"
16e2f7a777SLen Brown #include "acnamesp.h"
1795b482a8SLen Brown #ifdef ACPI_ASL_COMPILER
1856324c10SLv Zheng #include "acdisasm.h"
1995b482a8SLen Brown #endif
2095b482a8SLen Brown 
2195b482a8SLen Brown #define _COMPONENT          ACPI_DISPATCHER
2295b482a8SLen Brown ACPI_MODULE_NAME("dswload")
2395b482a8SLen Brown 
2495b482a8SLen Brown /*******************************************************************************
2595b482a8SLen Brown  *
2695b482a8SLen Brown  * FUNCTION:    acpi_ds_init_callbacks
2795b482a8SLen Brown  *
2895b482a8SLen Brown  * PARAMETERS:  walk_state      - Current state of the parse tree walk
2995b482a8SLen Brown  *              pass_number     - 1, 2, or 3
3095b482a8SLen Brown  *
3195b482a8SLen Brown  * RETURN:      Status
3295b482a8SLen Brown  *
3395b482a8SLen Brown  * DESCRIPTION: Init walk state callbacks
3495b482a8SLen Brown  *
3595b482a8SLen Brown  ******************************************************************************/
3695b482a8SLen Brown acpi_status
acpi_ds_init_callbacks(struct acpi_walk_state * walk_state,u32 pass_number)3795b482a8SLen Brown acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
3895b482a8SLen Brown {
3995b482a8SLen Brown 
4095b482a8SLen Brown 	switch (pass_number) {
4122b5afceSBob Moore 	case 0:
4222b5afceSBob Moore 
4322b5afceSBob Moore 		/* Parse only - caller will setup callbacks */
4422b5afceSBob Moore 
4522b5afceSBob Moore 		walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
4622b5afceSBob Moore 		    ACPI_PARSE_DELETE_TREE | ACPI_PARSE_DISASSEMBLE;
4722b5afceSBob Moore 		walk_state->descending_callback = NULL;
4822b5afceSBob Moore 		walk_state->ascending_callback = NULL;
4922b5afceSBob Moore 		break;
5022b5afceSBob Moore 
5195b482a8SLen Brown 	case 1:
521d1ea1b7SChao Guan 
5322b5afceSBob Moore 		/* Load pass 1 */
5422b5afceSBob Moore 
5595b482a8SLen Brown 		walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
5695b482a8SLen Brown 		    ACPI_PARSE_DELETE_TREE;
5795b482a8SLen Brown 		walk_state->descending_callback = acpi_ds_load1_begin_op;
5895b482a8SLen Brown 		walk_state->ascending_callback = acpi_ds_load1_end_op;
5995b482a8SLen Brown 		break;
6095b482a8SLen Brown 
6195b482a8SLen Brown 	case 2:
621d1ea1b7SChao Guan 
6322b5afceSBob Moore 		/* Load pass 2 */
6422b5afceSBob Moore 
6595b482a8SLen Brown 		walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
6695b482a8SLen Brown 		    ACPI_PARSE_DELETE_TREE;
6795b482a8SLen Brown 		walk_state->descending_callback = acpi_ds_load2_begin_op;
6895b482a8SLen Brown 		walk_state->ascending_callback = acpi_ds_load2_end_op;
6995b482a8SLen Brown 		break;
7095b482a8SLen Brown 
7195b482a8SLen Brown 	case 3:
721d1ea1b7SChao Guan 
7322b5afceSBob Moore 		/* Execution pass */
7422b5afceSBob Moore 
7595b482a8SLen Brown 		walk_state->parse_flags |= ACPI_PARSE_EXECUTE |
7695b482a8SLen Brown 		    ACPI_PARSE_DELETE_TREE;
7795b482a8SLen Brown 		walk_state->descending_callback = acpi_ds_exec_begin_op;
7895b482a8SLen Brown 		walk_state->ascending_callback = acpi_ds_exec_end_op;
7995b482a8SLen Brown 		break;
8095b482a8SLen Brown 
8195b482a8SLen Brown 	default:
821d1ea1b7SChao Guan 
8395b482a8SLen Brown 		return (AE_BAD_PARAMETER);
8495b482a8SLen Brown 	}
8595b482a8SLen Brown 
8695b482a8SLen Brown 	return (AE_OK);
8795b482a8SLen Brown }
8895b482a8SLen Brown 
8995b482a8SLen Brown /*******************************************************************************
9095b482a8SLen Brown  *
9195b482a8SLen Brown  * FUNCTION:    acpi_ds_load1_begin_op
9295b482a8SLen Brown  *
9395b482a8SLen Brown  * PARAMETERS:  walk_state      - Current state of the parse tree walk
9495b482a8SLen Brown  *              out_op          - Where to return op if a new one is created
9595b482a8SLen Brown  *
9695b482a8SLen Brown  * RETURN:      Status
9795b482a8SLen Brown  *
9895b482a8SLen Brown  * DESCRIPTION: Descending callback used during the loading of ACPI tables.
9995b482a8SLen Brown  *
10095b482a8SLen Brown  ******************************************************************************/
10195b482a8SLen Brown 
10295b482a8SLen Brown acpi_status
acpi_ds_load1_begin_op(struct acpi_walk_state * walk_state,union acpi_parse_object ** out_op)10395b482a8SLen Brown acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
10495b482a8SLen Brown 		       union acpi_parse_object **out_op)
10595b482a8SLen Brown {
10695b482a8SLen Brown 	union acpi_parse_object *op;
10795b482a8SLen Brown 	struct acpi_namespace_node *node;
10895b482a8SLen Brown 	acpi_status status;
10995b482a8SLen Brown 	acpi_object_type object_type;
11095b482a8SLen Brown 	char *path;
11195b482a8SLen Brown 	u32 flags;
11295b482a8SLen Brown 
1131ef63231SBob Moore 	ACPI_FUNCTION_TRACE_PTR(ds_load1_begin_op, walk_state->op);
11495b482a8SLen Brown 
11595b482a8SLen Brown 	op = walk_state->op;
11695b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
11795b482a8SLen Brown 			  walk_state));
11895b482a8SLen Brown 
11995b482a8SLen Brown 	/* We are only interested in opcodes that have an associated name */
12095b482a8SLen Brown 
12195b482a8SLen Brown 	if (op) {
12295b482a8SLen Brown 		if (!(walk_state->op_info->flags & AML_NAMED)) {
12395b482a8SLen Brown 			*out_op = op;
12495b482a8SLen Brown 			return_ACPI_STATUS(AE_OK);
12595b482a8SLen Brown 		}
12695b482a8SLen Brown 
12795b482a8SLen Brown 		/* Check if this object has already been installed in the namespace */
12895b482a8SLen Brown 
12995b482a8SLen Brown 		if (op->common.node) {
13095b482a8SLen Brown 			*out_op = op;
13195b482a8SLen Brown 			return_ACPI_STATUS(AE_OK);
13295b482a8SLen Brown 		}
13395b482a8SLen Brown 	}
13495b482a8SLen Brown 
13595b482a8SLen Brown 	path = acpi_ps_get_next_namestring(&walk_state->parser_state);
13695b482a8SLen Brown 
13795b482a8SLen Brown 	/* Map the raw opcode into an internal object type */
13895b482a8SLen Brown 
13995b482a8SLen Brown 	object_type = walk_state->op_info->object_type;
14095b482a8SLen Brown 
14195b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
14295b482a8SLen Brown 			  "State=%p Op=%p [%s]\n", walk_state, op,
14395b482a8SLen Brown 			  acpi_ut_get_type_name(object_type)));
14495b482a8SLen Brown 
14595b482a8SLen Brown 	switch (walk_state->opcode) {
14695b482a8SLen Brown 	case AML_SCOPE_OP:
14795b482a8SLen Brown 		/*
14895b482a8SLen Brown 		 * The target name of the Scope() operator must exist at this point so
14995b482a8SLen Brown 		 * that we can actually open the scope to enter new names underneath it.
15095b482a8SLen Brown 		 * Allow search-to-root for single namesegs.
15195b482a8SLen Brown 		 */
15295b482a8SLen Brown 		status =
15395b482a8SLen Brown 		    acpi_ns_lookup(walk_state->scope_info, path, object_type,
15495b482a8SLen Brown 				   ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
15595b482a8SLen Brown 				   walk_state, &(node));
15695b482a8SLen Brown #ifdef ACPI_ASL_COMPILER
15795b482a8SLen Brown 		if (status == AE_NOT_FOUND) {
15895b482a8SLen Brown 			/*
15995b482a8SLen Brown 			 * Table disassembly:
16095b482a8SLen Brown 			 * Target of Scope() not found. Generate an External for it, and
16195b482a8SLen Brown 			 * insert the name into the namespace.
16295b482a8SLen Brown 			 */
1635af2b635SBob Moore 			acpi_dm_add_op_to_external_list(op, path,
1645af2b635SBob Moore 							ACPI_TYPE_DEVICE, 0, 0);
16595b482a8SLen Brown 			status =
16695b482a8SLen Brown 			    acpi_ns_lookup(walk_state->scope_info, path,
16795b482a8SLen Brown 					   object_type, ACPI_IMODE_LOAD_PASS1,
16895b482a8SLen Brown 					   ACPI_NS_SEARCH_PARENT, walk_state,
16995b482a8SLen Brown 					   &node);
17095b482a8SLen Brown 		}
17195b482a8SLen Brown #endif
17295b482a8SLen Brown 		if (ACPI_FAILURE(status)) {
17316ccf829SBob Moore 			ACPI_ERROR_NAMESPACE(walk_state->scope_info, path,
17416ccf829SBob Moore 					     status);
17595b482a8SLen Brown 			return_ACPI_STATUS(status);
17695b482a8SLen Brown 		}
17795b482a8SLen Brown 
17895b482a8SLen Brown 		/*
17995b482a8SLen Brown 		 * Check to make sure that the target is
18095b482a8SLen Brown 		 * one of the opcodes that actually opens a scope
18195b482a8SLen Brown 		 */
18295b482a8SLen Brown 		switch (node->type) {
18395b482a8SLen Brown 		case ACPI_TYPE_ANY:
18495b482a8SLen Brown 		case ACPI_TYPE_LOCAL_SCOPE:	/* Scope  */
18595b482a8SLen Brown 		case ACPI_TYPE_DEVICE:
18695b482a8SLen Brown 		case ACPI_TYPE_POWER:
18795b482a8SLen Brown 		case ACPI_TYPE_PROCESSOR:
18895b482a8SLen Brown 		case ACPI_TYPE_THERMAL:
18995b482a8SLen Brown 
19095b482a8SLen Brown 			/* These are acceptable types */
19195b482a8SLen Brown 			break;
19295b482a8SLen Brown 
19395b482a8SLen Brown 		case ACPI_TYPE_INTEGER:
19495b482a8SLen Brown 		case ACPI_TYPE_STRING:
19595b482a8SLen Brown 		case ACPI_TYPE_BUFFER:
19695b482a8SLen Brown 			/*
197f2cb1251SBob Moore 			 * These types we will allow, but we will change the type.
198f2cb1251SBob Moore 			 * This enables some existing code of the form:
19995b482a8SLen Brown 			 *
20095b482a8SLen Brown 			 *  Name (DEB, 0)
20195b482a8SLen Brown 			 *  Scope (DEB) { ... }
20295b482a8SLen Brown 			 *
203f2cb1251SBob Moore 			 * Note: silently change the type here. On the second pass,
204f2cb1251SBob Moore 			 * we will report a warning
20595b482a8SLen Brown 			 */
20695b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_INFO,
207f2cb1251SBob Moore 					  "Type override - [%4.4s] had invalid type (%s) "
208f2cb1251SBob Moore 					  "for Scope operator, changed to type ANY\n",
209f2cb1251SBob Moore 					  acpi_ut_get_node_name(node),
21095b482a8SLen Brown 					  acpi_ut_get_type_name(node->type)));
21195b482a8SLen Brown 
21295b482a8SLen Brown 			node->type = ACPI_TYPE_ANY;
21395b482a8SLen Brown 			walk_state->scope_info->common.value = ACPI_TYPE_ANY;
21495b482a8SLen Brown 			break;
21595b482a8SLen Brown 
21628f538b5SBob Moore 		case ACPI_TYPE_METHOD:
21728f538b5SBob Moore 			/*
21828f538b5SBob Moore 			 * Allow scope change to root during execution of module-level
21928f538b5SBob Moore 			 * code. Root is typed METHOD during this time.
22028f538b5SBob Moore 			 */
22128f538b5SBob Moore 			if ((node == acpi_gbl_root_node) &&
22228f538b5SBob Moore 			    (walk_state->
22328f538b5SBob Moore 			     parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
22428f538b5SBob Moore 				break;
22528f538b5SBob Moore 			}
22628f538b5SBob Moore 
227c1a7c2ceSNick Desaulniers 			ACPI_FALLTHROUGH;
22828f538b5SBob Moore 
22995b482a8SLen Brown 		default:
23095b482a8SLen Brown 
23195b482a8SLen Brown 			/* All other types are an error */
23295b482a8SLen Brown 
23395b482a8SLen Brown 			ACPI_ERROR((AE_INFO,
234f2cb1251SBob Moore 				    "Invalid type (%s) for target of "
235f2cb1251SBob Moore 				    "Scope operator [%4.4s] (Cannot override)",
236f2cb1251SBob Moore 				    acpi_ut_get_type_name(node->type),
237f2cb1251SBob Moore 				    acpi_ut_get_node_name(node)));
23895b482a8SLen Brown 
23995b482a8SLen Brown 			return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
24095b482a8SLen Brown 		}
24195b482a8SLen Brown 		break;
24295b482a8SLen Brown 
24395b482a8SLen Brown 	default:
24495b482a8SLen Brown 		/*
24595b482a8SLen Brown 		 * For all other named opcodes, we will enter the name into
24695b482a8SLen Brown 		 * the namespace.
24795b482a8SLen Brown 		 *
24895b482a8SLen Brown 		 * Setup the search flags.
24995b482a8SLen Brown 		 * Since we are entering a name into the namespace, we do not want to
25095b482a8SLen Brown 		 * enable the search-to-root upsearch.
25195b482a8SLen Brown 		 *
25295b482a8SLen Brown 		 * There are only two conditions where it is acceptable that the name
25395b482a8SLen Brown 		 * already exists:
25495b482a8SLen Brown 		 *    1) the Scope() operator can reopen a scoping object that was
25595b482a8SLen Brown 		 *       previously defined (Scope, Method, Device, etc.)
25695b482a8SLen Brown 		 *    2) Whenever we are parsing a deferred opcode (op_region, Buffer,
25795b482a8SLen Brown 		 *       buffer_field, or Package), the name of the object is already
25895b482a8SLen Brown 		 *       in the namespace.
25995b482a8SLen Brown 		 */
26095b482a8SLen Brown 		if (walk_state->deferred_node) {
26195b482a8SLen Brown 
26295b482a8SLen Brown 			/* This name is already in the namespace, get the node */
26395b482a8SLen Brown 
26495b482a8SLen Brown 			node = walk_state->deferred_node;
26595b482a8SLen Brown 			status = AE_OK;
26695b482a8SLen Brown 			break;
26795b482a8SLen Brown 		}
26895b482a8SLen Brown 
26995b482a8SLen Brown 		/*
27095b482a8SLen Brown 		 * If we are executing a method, do not create any namespace objects
27195b482a8SLen Brown 		 * during the load phase, only during execution.
27295b482a8SLen Brown 		 */
27395b482a8SLen Brown 		if (walk_state->method_node) {
27495b482a8SLen Brown 			node = NULL;
27595b482a8SLen Brown 			status = AE_OK;
27695b482a8SLen Brown 			break;
27795b482a8SLen Brown 		}
27895b482a8SLen Brown 
27995b482a8SLen Brown 		flags = ACPI_NS_NO_UPSEARCH;
28095b482a8SLen Brown 		if ((walk_state->opcode != AML_SCOPE_OP) &&
28195b482a8SLen Brown 		    (!(walk_state->parse_flags & ACPI_PARSE_DEFERRED_OP))) {
2828f6f0361SLv Zheng 			if (walk_state->namespace_override) {
2838f6f0361SLv Zheng 				flags |= ACPI_NS_OVERRIDE_IF_FOUND;
2848f6f0361SLv Zheng 				ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
2858f6f0361SLv Zheng 						  "[%s] Override allowed\n",
2868f6f0361SLv Zheng 						  acpi_ut_get_type_name
2878f6f0361SLv Zheng 						  (object_type)));
2888f6f0361SLv Zheng 			} else {
28995b482a8SLen Brown 				flags |= ACPI_NS_ERROR_IF_FOUND;
29095b482a8SLen Brown 				ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
29195b482a8SLen Brown 						  "[%s] Cannot already exist\n",
2928f6f0361SLv Zheng 						  acpi_ut_get_type_name
2938f6f0361SLv Zheng 						  (object_type)));
2948f6f0361SLv Zheng 			}
29595b482a8SLen Brown 		} else {
29695b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
29795b482a8SLen Brown 					  "[%s] Both Find or Create allowed\n",
29895b482a8SLen Brown 					  acpi_ut_get_type_name(object_type)));
29995b482a8SLen Brown 		}
30095b482a8SLen Brown 
30195b482a8SLen Brown 		/*
30295b482a8SLen Brown 		 * Enter the named type into the internal namespace. We enter the name
30395b482a8SLen Brown 		 * as we go downward in the parse tree. Any necessary subobjects that
30495b482a8SLen Brown 		 * involve arguments to the opcode must be created as we go back up the
30595b482a8SLen Brown 		 * parse tree later.
30695b482a8SLen Brown 		 */
30795b482a8SLen Brown 		status =
30895b482a8SLen Brown 		    acpi_ns_lookup(walk_state->scope_info, path, object_type,
30995b482a8SLen Brown 				   ACPI_IMODE_LOAD_PASS1, flags, walk_state,
31095b482a8SLen Brown 				   &node);
31195b482a8SLen Brown 		if (ACPI_FAILURE(status)) {
31295b482a8SLen Brown 			if (status == AE_ALREADY_EXISTS) {
31395b482a8SLen Brown 
31495b482a8SLen Brown 				/* The name already exists in this scope */
31595b482a8SLen Brown 
31695b482a8SLen Brown 				if (node->flags & ANOBJ_IS_EXTERNAL) {
31795b482a8SLen Brown 					/*
31895b482a8SLen Brown 					 * Allow one create on an object or segment that was
31995b482a8SLen Brown 					 * previously declared External
32095b482a8SLen Brown 					 */
32195b482a8SLen Brown 					node->flags &= ~ANOBJ_IS_EXTERNAL;
32295b482a8SLen Brown 					node->type = (u8) object_type;
32395b482a8SLen Brown 
32495b482a8SLen Brown 					/* Just retyped a node, probably will need to open a scope */
32595b482a8SLen Brown 
32695b482a8SLen Brown 					if (acpi_ns_opens_scope(object_type)) {
32795b482a8SLen Brown 						status =
32895b482a8SLen Brown 						    acpi_ds_scope_stack_push
32995b482a8SLen Brown 						    (node, object_type,
33095b482a8SLen Brown 						     walk_state);
33195b482a8SLen Brown 						if (ACPI_FAILURE(status)) {
33295b482a8SLen Brown 							return_ACPI_STATUS
33395b482a8SLen Brown 							    (status);
33495b482a8SLen Brown 						}
33595b482a8SLen Brown 					}
33695b482a8SLen Brown 
33795b482a8SLen Brown 					status = AE_OK;
33895b482a8SLen Brown 				}
33995b482a8SLen Brown 			}
34095b482a8SLen Brown 
34195b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
34216ccf829SBob Moore 				ACPI_ERROR_NAMESPACE(walk_state->scope_info,
34316ccf829SBob Moore 						     path, status);
34495b482a8SLen Brown 				return_ACPI_STATUS(status);
34595b482a8SLen Brown 			}
34695b482a8SLen Brown 		}
34795b482a8SLen Brown 		break;
34895b482a8SLen Brown 	}
34995b482a8SLen Brown 
35095b482a8SLen Brown 	/* Common exit */
35195b482a8SLen Brown 
35295b482a8SLen Brown 	if (!op) {
35395b482a8SLen Brown 
35495b482a8SLen Brown 		/* Create a new op */
35595b482a8SLen Brown 
35662eb935bSLv Zheng 		op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
35795b482a8SLen Brown 		if (!op) {
35895b482a8SLen Brown 			return_ACPI_STATUS(AE_NO_MEMORY);
35995b482a8SLen Brown 		}
36095b482a8SLen Brown 	}
36195b482a8SLen Brown 
36295b482a8SLen Brown 	/* Initialize the op */
36395b482a8SLen Brown 
364178a0f63SErik Schmauss #ifdef ACPI_CONSTANT_EVAL_ONLY
36505c3507cSErik Schmauss 	op->named.path = path;
36695b482a8SLen Brown #endif
36795b482a8SLen Brown 
36895b482a8SLen Brown 	if (node) {
36995b482a8SLen Brown 		/*
37095b482a8SLen Brown 		 * Put the Node in the "op" object that the parser uses, so we
37195b482a8SLen Brown 		 * can get it again quickly when this scope is closed
37295b482a8SLen Brown 		 */
37395b482a8SLen Brown 		op->common.node = node;
37495b482a8SLen Brown 		op->named.name = node->name.integer;
37595b482a8SLen Brown 	}
37695b482a8SLen Brown 
37795b482a8SLen Brown 	acpi_ps_append_arg(acpi_ps_get_parent_scope(&walk_state->parser_state),
37895b482a8SLen Brown 			   op);
37995b482a8SLen Brown 	*out_op = op;
38095b482a8SLen Brown 	return_ACPI_STATUS(status);
38195b482a8SLen Brown }
38295b482a8SLen Brown 
38395b482a8SLen Brown /*******************************************************************************
38495b482a8SLen Brown  *
38595b482a8SLen Brown  * FUNCTION:    acpi_ds_load1_end_op
38695b482a8SLen Brown  *
38795b482a8SLen Brown  * PARAMETERS:  walk_state      - Current state of the parse tree walk
38895b482a8SLen Brown  *
38995b482a8SLen Brown  * RETURN:      Status
39095b482a8SLen Brown  *
39195b482a8SLen Brown  * DESCRIPTION: Ascending callback used during the loading of the namespace,
39295b482a8SLen Brown  *              both control methods and everything else.
39395b482a8SLen Brown  *
39495b482a8SLen Brown  ******************************************************************************/
39595b482a8SLen Brown 
acpi_ds_load1_end_op(struct acpi_walk_state * walk_state)39695b482a8SLen Brown acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
39795b482a8SLen Brown {
39895b482a8SLen Brown 	union acpi_parse_object *op;
39995b482a8SLen Brown 	acpi_object_type object_type;
40095b482a8SLen Brown 	acpi_status status = AE_OK;
401bdcf4cdbSErik Schmauss #ifdef ACPI_ASL_COMPILER
402bdcf4cdbSErik Schmauss 	u8 param_count;
403bdcf4cdbSErik Schmauss #endif
404bdcf4cdbSErik Schmauss 
40595b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ds_load1_end_op);
40695b482a8SLen Brown 
40795b482a8SLen Brown 	op = walk_state->op;
40895b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
40995b482a8SLen Brown 			  walk_state));
41095b482a8SLen Brown 
4115ddbd771SErik Kaneda 	/*
4125ddbd771SErik Kaneda 	 * Disassembler: handle create field operators here.
4135ddbd771SErik Kaneda 	 *
4145ddbd771SErik Kaneda 	 * create_buffer_field is a deferred op that is typically processed in load
4155ddbd771SErik Kaneda 	 * pass 2. However, disassembly of control method contents walk the parse
4165ddbd771SErik Kaneda 	 * tree with ACPI_PARSE_LOAD_PASS1 and AML_CREATE operators are processed
4175ddbd771SErik Kaneda 	 * in a later walk. This is a problem when there is a control method that
4185ddbd771SErik Kaneda 	 * has the same name as the AML_CREATE object. In this case, any use of the
4195ddbd771SErik Kaneda 	 * name segment will be detected as a method call rather than a reference
4205ddbd771SErik Kaneda 	 * to a buffer field.
4215ddbd771SErik Kaneda 	 *
4225ddbd771SErik Kaneda 	 * This earlier creation during disassembly solves this issue by inserting
4235ddbd771SErik Kaneda 	 * the named object in the ACPI namespace so that references to this name
4245ddbd771SErik Kaneda 	 * would be a name string rather than a method call.
4255ddbd771SErik Kaneda 	 */
4265ddbd771SErik Kaneda 	if ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) &&
4275ddbd771SErik Kaneda 	    (walk_state->op_info->flags & AML_CREATE)) {
4285ddbd771SErik Kaneda 		status = acpi_ds_create_buffer_field(op, walk_state);
4295ddbd771SErik Kaneda 		return_ACPI_STATUS(status);
4305ddbd771SErik Kaneda 	}
4315ddbd771SErik Kaneda 
43295b482a8SLen Brown 	/* We are only interested in opcodes that have an associated name */
43395b482a8SLen Brown 
43495b482a8SLen Brown 	if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
43595b482a8SLen Brown 		return_ACPI_STATUS(AE_OK);
43695b482a8SLen Brown 	}
43795b482a8SLen Brown 
43895b482a8SLen Brown 	/* Get the object type to determine if we should pop the scope */
43995b482a8SLen Brown 
44095b482a8SLen Brown 	object_type = walk_state->op_info->object_type;
44195b482a8SLen Brown 
44295b482a8SLen Brown 	if (walk_state->op_info->flags & AML_FIELD) {
44395b482a8SLen Brown 		/*
44495b482a8SLen Brown 		 * If we are executing a method, do not create any namespace objects
44595b482a8SLen Brown 		 * during the load phase, only during execution.
44695b482a8SLen Brown 		 */
44795b482a8SLen Brown 		if (!walk_state->method_node) {
44895b482a8SLen Brown 			if (walk_state->opcode == AML_FIELD_OP ||
44995b482a8SLen Brown 			    walk_state->opcode == AML_BANK_FIELD_OP ||
45095b482a8SLen Brown 			    walk_state->opcode == AML_INDEX_FIELD_OP) {
45195b482a8SLen Brown 				status =
45295b482a8SLen Brown 				    acpi_ds_init_field_objects(op, walk_state);
45395b482a8SLen Brown 			}
45495b482a8SLen Brown 		}
45595b482a8SLen Brown 		return_ACPI_STATUS(status);
45695b482a8SLen Brown 	}
45795b482a8SLen Brown 
45895b482a8SLen Brown 	/*
45995b482a8SLen Brown 	 * If we are executing a method, do not create any namespace objects
46095b482a8SLen Brown 	 * during the load phase, only during execution.
46195b482a8SLen Brown 	 */
46295b482a8SLen Brown 	if (!walk_state->method_node) {
46395b482a8SLen Brown 		if (op->common.aml_opcode == AML_REGION_OP) {
46495b482a8SLen Brown 			status =
46595b482a8SLen Brown 			    acpi_ex_create_region(op->named.data,
46695b482a8SLen Brown 						  op->named.length,
4671fad8738SBob Moore 						  (acpi_adr_space_type)
4681fad8738SBob Moore 						  ((op->common.value.arg)->
4691fad8738SBob Moore 						   common.value.integer),
47095b482a8SLen Brown 						  walk_state);
47195b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
47295b482a8SLen Brown 				return_ACPI_STATUS(status);
47395b482a8SLen Brown 			}
47495b482a8SLen Brown 		} else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
47595b482a8SLen Brown 			status =
47695b482a8SLen Brown 			    acpi_ex_create_region(op->named.data,
47795b482a8SLen Brown 						  op->named.length,
47882a1b7cbSBob Moore 						  ACPI_ADR_SPACE_DATA_TABLE,
47995b482a8SLen Brown 						  walk_state);
48095b482a8SLen Brown 			if (ACPI_FAILURE(status)) {
48195b482a8SLen Brown 				return_ACPI_STATUS(status);
48295b482a8SLen Brown 			}
48395b482a8SLen Brown 		}
48495b482a8SLen Brown 	}
48595b482a8SLen Brown 
48695b482a8SLen Brown 	if (op->common.aml_opcode == AML_NAME_OP) {
48795b482a8SLen Brown 
48895b482a8SLen Brown 		/* For Name opcode, get the object type from the argument */
48995b482a8SLen Brown 
49095b482a8SLen Brown 		if (op->common.value.arg) {
49195b482a8SLen Brown 			object_type = (acpi_ps_get_opcode_info((op->common.
49295b482a8SLen Brown 								value.arg)->
49395b482a8SLen Brown 							       common.
49495b482a8SLen Brown 							       aml_opcode))->
49595b482a8SLen Brown 			    object_type;
49695b482a8SLen Brown 
49795b482a8SLen Brown 			/* Set node type if we have a namespace node */
49895b482a8SLen Brown 
49995b482a8SLen Brown 			if (op->common.node) {
50095b482a8SLen Brown 				op->common.node->type = (u8) object_type;
50195b482a8SLen Brown 			}
50295b482a8SLen Brown 		}
50395b482a8SLen Brown 	}
504bdcf4cdbSErik Schmauss #ifdef ACPI_ASL_COMPILER
505bdcf4cdbSErik Schmauss 	/*
506bdcf4cdbSErik Schmauss 	 * For external opcode, get the object type from the argument and
507bdcf4cdbSErik Schmauss 	 * get the parameter count from the argument's next.
508bdcf4cdbSErik Schmauss 	 */
509bdcf4cdbSErik Schmauss 	if (acpi_gbl_disasm_flag &&
510bdcf4cdbSErik Schmauss 	    op->common.node && op->common.aml_opcode == AML_EXTERNAL_OP) {
511bdcf4cdbSErik Schmauss 		/*
512bdcf4cdbSErik Schmauss 		 * Note, if this external is not a method
513bdcf4cdbSErik Schmauss 		 * Op->Common.Value.Arg->Common.Next->Common.Value.Integer == 0
514bdcf4cdbSErik Schmauss 		 * Therefore, param_count will be 0.
515bdcf4cdbSErik Schmauss 		 */
516bdcf4cdbSErik Schmauss 		param_count =
517bdcf4cdbSErik Schmauss 		    (u8)op->common.value.arg->common.next->common.value.integer;
518bdcf4cdbSErik Schmauss 		object_type = (u8)op->common.value.arg->common.value.integer;
519bdcf4cdbSErik Schmauss 		op->common.node->flags |= ANOBJ_IS_EXTERNAL;
520bdcf4cdbSErik Schmauss 		op->common.node->type = (u8)object_type;
521bdcf4cdbSErik Schmauss 
522bdcf4cdbSErik Schmauss 		acpi_dm_create_subobject_for_external((u8)object_type,
523bdcf4cdbSErik Schmauss 						      &op->common.node,
524bdcf4cdbSErik Schmauss 						      param_count);
525bdcf4cdbSErik Schmauss 
526bdcf4cdbSErik Schmauss 		/*
527bdcf4cdbSErik Schmauss 		 * Add the external to the external list because we may be
528bdcf4cdbSErik Schmauss 		 * emitting code based off of the items within the external list.
529bdcf4cdbSErik Schmauss 		 */
530bdcf4cdbSErik Schmauss 		acpi_dm_add_op_to_external_list(op, op->named.path,
531bdcf4cdbSErik Schmauss 						(u8)object_type, param_count,
532bdcf4cdbSErik Schmauss 						ACPI_EXT_ORIGIN_FROM_OPCODE |
533bdcf4cdbSErik Schmauss 						ACPI_EXT_RESOLVED_REFERENCE);
534bdcf4cdbSErik Schmauss 	}
535bdcf4cdbSErik Schmauss #endif
53695b482a8SLen Brown 
53795b482a8SLen Brown 	/*
53895b482a8SLen Brown 	 * If we are executing a method, do not create any namespace objects
53995b482a8SLen Brown 	 * during the load phase, only during execution.
54095b482a8SLen Brown 	 */
54195b482a8SLen Brown 	if (!walk_state->method_node) {
54295b482a8SLen Brown 		if (op->common.aml_opcode == AML_METHOD_OP) {
54395b482a8SLen Brown 			/*
54495b482a8SLen Brown 			 * method_op pkg_length name_string method_flags term_list
54595b482a8SLen Brown 			 *
54695b482a8SLen Brown 			 * Note: We must create the method node/object pair as soon as we
54795b482a8SLen Brown 			 * see the method declaration. This allows later pass1 parsing
54895b482a8SLen Brown 			 * of invocations of the method (need to know the number of
54995b482a8SLen Brown 			 * arguments.)
55095b482a8SLen Brown 			 */
55195b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
55295b482a8SLen Brown 					  "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
55395b482a8SLen Brown 					  walk_state, op, op->named.node));
55495b482a8SLen Brown 
55595b482a8SLen Brown 			if (!acpi_ns_get_attached_object(op->named.node)) {
55695b482a8SLen Brown 				walk_state->operands[0] =
55795b482a8SLen Brown 				    ACPI_CAST_PTR(void, op->named.node);
55895b482a8SLen Brown 				walk_state->num_operands = 1;
55995b482a8SLen Brown 
56095b482a8SLen Brown 				status =
56195b482a8SLen Brown 				    acpi_ds_create_operands(walk_state,
56295b482a8SLen Brown 							    op->common.value.
56395b482a8SLen Brown 							    arg);
56495b482a8SLen Brown 				if (ACPI_SUCCESS(status)) {
56595b482a8SLen Brown 					status =
56695b482a8SLen Brown 					    acpi_ex_create_method(op->named.
56795b482a8SLen Brown 								  data,
56895b482a8SLen Brown 								  op->named.
56995b482a8SLen Brown 								  length,
57095b482a8SLen Brown 								  walk_state);
57195b482a8SLen Brown 				}
57295b482a8SLen Brown 
57395b482a8SLen Brown 				walk_state->operands[0] = NULL;
57495b482a8SLen Brown 				walk_state->num_operands = 0;
57595b482a8SLen Brown 
57695b482a8SLen Brown 				if (ACPI_FAILURE(status)) {
57795b482a8SLen Brown 					return_ACPI_STATUS(status);
57895b482a8SLen Brown 				}
57995b482a8SLen Brown 			}
58095b482a8SLen Brown 		}
58195b482a8SLen Brown 	}
58295b482a8SLen Brown 
58395b482a8SLen Brown 	/* Pop the scope stack (only if loading a table) */
58495b482a8SLen Brown 
585bdcf4cdbSErik Schmauss 	if (!walk_state->method_node &&
586bdcf4cdbSErik Schmauss 	    op->common.aml_opcode != AML_EXTERNAL_OP &&
587bdcf4cdbSErik Schmauss 	    acpi_ns_opens_scope(object_type)) {
58895b482a8SLen Brown 		ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
58995b482a8SLen Brown 				  "(%s): Popping scope for Op %p\n",
59095b482a8SLen Brown 				  acpi_ut_get_type_name(object_type), op));
59195b482a8SLen Brown 
59295b482a8SLen Brown 		status = acpi_ds_scope_stack_pop(walk_state);
59395b482a8SLen Brown 	}
59495b482a8SLen Brown 
59595b482a8SLen Brown 	return_ACPI_STATUS(status);
59695b482a8SLen Brown }
597