1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: dswload - Dispatcher first pass namespace load callbacks
6 * Copyright (C) 2000 - 2019, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
18 #ifdef ACPI_ASL_COMPILER
22 #define _COMPONENT ACPI_DISPATCHER
23 ACPI_MODULE_NAME("dswload")
25 /*******************************************************************************
27 * FUNCTION: acpi_ds_init_callbacks
29 * PARAMETERS: walk_state - Current state of the parse tree walk
30 * pass_number - 1, 2, or 3
34 * DESCRIPTION: Init walk state callbacks
36 ******************************************************************************/
38 acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
41 switch (pass_number) {
44 /* Parse only - caller will setup callbacks */
46 walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
47 ACPI_PARSE_DELETE_TREE | ACPI_PARSE_DISASSEMBLE;
48 walk_state->descending_callback = NULL;
49 walk_state->ascending_callback = NULL;
56 walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
57 ACPI_PARSE_DELETE_TREE;
58 walk_state->descending_callback = acpi_ds_load1_begin_op;
59 walk_state->ascending_callback = acpi_ds_load1_end_op;
66 walk_state->parse_flags = ACPI_PARSE_LOAD_PASS1 |
67 ACPI_PARSE_DELETE_TREE;
68 walk_state->descending_callback = acpi_ds_load2_begin_op;
69 walk_state->ascending_callback = acpi_ds_load2_end_op;
76 walk_state->parse_flags |= ACPI_PARSE_EXECUTE |
77 ACPI_PARSE_DELETE_TREE;
78 walk_state->descending_callback = acpi_ds_exec_begin_op;
79 walk_state->ascending_callback = acpi_ds_exec_end_op;
84 return (AE_BAD_PARAMETER);
90 /*******************************************************************************
92 * FUNCTION: acpi_ds_load1_begin_op
94 * PARAMETERS: walk_state - Current state of the parse tree walk
95 * out_op - Where to return op if a new one is created
99 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
101 ******************************************************************************/
104 acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
105 union acpi_parse_object **out_op)
107 union acpi_parse_object *op;
108 struct acpi_namespace_node *node;
110 acpi_object_type object_type;
114 ACPI_FUNCTION_TRACE_PTR(ds_load1_begin_op, walk_state->op);
117 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
120 /* We are only interested in opcodes that have an associated name */
123 if (!(walk_state->op_info->flags & AML_NAMED)) {
125 return_ACPI_STATUS(AE_OK);
128 /* Check if this object has already been installed in the namespace */
130 if (op->common.node) {
132 return_ACPI_STATUS(AE_OK);
136 path = acpi_ps_get_next_namestring(&walk_state->parser_state);
138 /* Map the raw opcode into an internal object type */
140 object_type = walk_state->op_info->object_type;
142 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
143 "State=%p Op=%p [%s]\n", walk_state, op,
144 acpi_ut_get_type_name(object_type)));
146 switch (walk_state->opcode) {
149 * The target name of the Scope() operator must exist at this point so
150 * that we can actually open the scope to enter new names underneath it.
151 * Allow search-to-root for single namesegs.
154 acpi_ns_lookup(walk_state->scope_info, path, object_type,
155 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
156 walk_state, &(node));
157 #ifdef ACPI_ASL_COMPILER
158 if (status == AE_NOT_FOUND) {
161 * Target of Scope() not found. Generate an External for it, and
162 * insert the name into the namespace.
164 acpi_dm_add_op_to_external_list(op, path,
165 ACPI_TYPE_DEVICE, 0, 0);
167 acpi_ns_lookup(walk_state->scope_info, path,
168 object_type, ACPI_IMODE_LOAD_PASS1,
169 ACPI_NS_SEARCH_PARENT, walk_state,
173 if (ACPI_FAILURE(status)) {
174 ACPI_ERROR_NAMESPACE(walk_state->scope_info, path,
176 return_ACPI_STATUS(status);
180 * Check to make sure that the target is
181 * one of the opcodes that actually opens a scope
183 switch (node->type) {
185 case ACPI_TYPE_LOCAL_SCOPE: /* Scope */
186 case ACPI_TYPE_DEVICE:
187 case ACPI_TYPE_POWER:
188 case ACPI_TYPE_PROCESSOR:
189 case ACPI_TYPE_THERMAL:
191 /* These are acceptable types */
194 case ACPI_TYPE_INTEGER:
195 case ACPI_TYPE_STRING:
196 case ACPI_TYPE_BUFFER:
198 * These types we will allow, but we will change the type.
199 * This enables some existing code of the form:
202 * Scope (DEB) { ... }
204 * Note: silently change the type here. On the second pass,
205 * we will report a warning
207 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
208 "Type override - [%4.4s] had invalid type (%s) "
209 "for Scope operator, changed to type ANY\n",
210 acpi_ut_get_node_name(node),
211 acpi_ut_get_type_name(node->type)));
213 node->type = ACPI_TYPE_ANY;
214 walk_state->scope_info->common.value = ACPI_TYPE_ANY;
217 case ACPI_TYPE_METHOD:
219 * Allow scope change to root during execution of module-level
220 * code. Root is typed METHOD during this time.
222 if ((node == acpi_gbl_root_node) &&
224 parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
228 /*lint -fallthrough */
232 /* All other types are an error */
235 "Invalid type (%s) for target of "
236 "Scope operator [%4.4s] (Cannot override)",
237 acpi_ut_get_type_name(node->type),
238 acpi_ut_get_node_name(node)));
240 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
246 * For all other named opcodes, we will enter the name into
249 * Setup the search flags.
250 * Since we are entering a name into the namespace, we do not want to
251 * enable the search-to-root upsearch.
253 * There are only two conditions where it is acceptable that the name
255 * 1) the Scope() operator can reopen a scoping object that was
256 * previously defined (Scope, Method, Device, etc.)
257 * 2) Whenever we are parsing a deferred opcode (op_region, Buffer,
258 * buffer_field, or Package), the name of the object is already
261 if (walk_state->deferred_node) {
263 /* This name is already in the namespace, get the node */
265 node = walk_state->deferred_node;
271 * If we are executing a method, do not create any namespace objects
272 * during the load phase, only during execution.
274 if (walk_state->method_node) {
280 flags = ACPI_NS_NO_UPSEARCH;
281 if ((walk_state->opcode != AML_SCOPE_OP) &&
282 (!(walk_state->parse_flags & ACPI_PARSE_DEFERRED_OP))) {
283 if (walk_state->namespace_override) {
284 flags |= ACPI_NS_OVERRIDE_IF_FOUND;
285 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
286 "[%s] Override allowed\n",
287 acpi_ut_get_type_name
290 flags |= ACPI_NS_ERROR_IF_FOUND;
291 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
292 "[%s] Cannot already exist\n",
293 acpi_ut_get_type_name
297 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
298 "[%s] Both Find or Create allowed\n",
299 acpi_ut_get_type_name(object_type)));
303 * Enter the named type into the internal namespace. We enter the name
304 * as we go downward in the parse tree. Any necessary subobjects that
305 * involve arguments to the opcode must be created as we go back up the
309 acpi_ns_lookup(walk_state->scope_info, path, object_type,
310 ACPI_IMODE_LOAD_PASS1, flags, walk_state,
312 if (ACPI_FAILURE(status)) {
313 if (status == AE_ALREADY_EXISTS) {
315 /* The name already exists in this scope */
317 if (node->flags & ANOBJ_IS_EXTERNAL) {
319 * Allow one create on an object or segment that was
320 * previously declared External
322 node->flags &= ~ANOBJ_IS_EXTERNAL;
323 node->type = (u8) object_type;
325 /* Just retyped a node, probably will need to open a scope */
327 if (acpi_ns_opens_scope(object_type)) {
329 acpi_ds_scope_stack_push
332 if (ACPI_FAILURE(status)) {
342 if (ACPI_FAILURE(status)) {
343 ACPI_ERROR_NAMESPACE(walk_state->scope_info,
345 return_ACPI_STATUS(status);
355 /* Create a new op */
357 op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
359 return_ACPI_STATUS(AE_NO_MEMORY);
363 /* Initialize the op */
365 #ifdef ACPI_CONSTANT_EVAL_ONLY
366 op->named.path = path;
371 * Put the Node in the "op" object that the parser uses, so we
372 * can get it again quickly when this scope is closed
374 op->common.node = node;
375 op->named.name = node->name.integer;
378 acpi_ps_append_arg(acpi_ps_get_parent_scope(&walk_state->parser_state),
381 return_ACPI_STATUS(status);
384 /*******************************************************************************
386 * FUNCTION: acpi_ds_load1_end_op
388 * PARAMETERS: walk_state - Current state of the parse tree walk
392 * DESCRIPTION: Ascending callback used during the loading of the namespace,
393 * both control methods and everything else.
395 ******************************************************************************/
397 acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
399 union acpi_parse_object *op;
400 acpi_object_type object_type;
401 acpi_status status = AE_OK;
403 #ifdef ACPI_ASL_COMPILER
407 ACPI_FUNCTION_TRACE(ds_load1_end_op);
410 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
413 /* We are only interested in opcodes that have an associated name */
415 if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
416 return_ACPI_STATUS(AE_OK);
419 /* Get the object type to determine if we should pop the scope */
421 object_type = walk_state->op_info->object_type;
423 if (walk_state->op_info->flags & AML_FIELD) {
425 * If we are executing a method, do not create any namespace objects
426 * during the load phase, only during execution.
428 if (!walk_state->method_node) {
429 if (walk_state->opcode == AML_FIELD_OP ||
430 walk_state->opcode == AML_BANK_FIELD_OP ||
431 walk_state->opcode == AML_INDEX_FIELD_OP) {
433 acpi_ds_init_field_objects(op, walk_state);
436 return_ACPI_STATUS(status);
440 * If we are executing a method, do not create any namespace objects
441 * during the load phase, only during execution.
443 if (!walk_state->method_node) {
444 if (op->common.aml_opcode == AML_REGION_OP) {
446 acpi_ex_create_region(op->named.data,
448 (acpi_adr_space_type)
449 ((op->common.value.arg)->
450 common.value.integer),
452 if (ACPI_FAILURE(status)) {
453 return_ACPI_STATUS(status);
455 } else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
457 acpi_ex_create_region(op->named.data,
459 ACPI_ADR_SPACE_DATA_TABLE,
461 if (ACPI_FAILURE(status)) {
462 return_ACPI_STATUS(status);
467 if (op->common.aml_opcode == AML_NAME_OP) {
469 /* For Name opcode, get the object type from the argument */
471 if (op->common.value.arg) {
472 object_type = (acpi_ps_get_opcode_info((op->common.
478 /* Set node type if we have a namespace node */
480 if (op->common.node) {
481 op->common.node->type = (u8) object_type;
485 #ifdef ACPI_ASL_COMPILER
487 * For external opcode, get the object type from the argument and
488 * get the parameter count from the argument's next.
490 if (acpi_gbl_disasm_flag &&
491 op->common.node && op->common.aml_opcode == AML_EXTERNAL_OP) {
493 * Note, if this external is not a method
494 * Op->Common.Value.Arg->Common.Next->Common.Value.Integer == 0
495 * Therefore, param_count will be 0.
498 (u8)op->common.value.arg->common.next->common.value.integer;
499 object_type = (u8)op->common.value.arg->common.value.integer;
500 op->common.node->flags |= ANOBJ_IS_EXTERNAL;
501 op->common.node->type = (u8)object_type;
503 acpi_dm_create_subobject_for_external((u8)object_type,
508 * Add the external to the external list because we may be
509 * emitting code based off of the items within the external list.
511 acpi_dm_add_op_to_external_list(op, op->named.path,
512 (u8)object_type, param_count,
513 ACPI_EXT_ORIGIN_FROM_OPCODE |
514 ACPI_EXT_RESOLVED_REFERENCE);
519 * If we are executing a method, do not create any namespace objects
520 * during the load phase, only during execution.
522 if (!walk_state->method_node) {
523 if (op->common.aml_opcode == AML_METHOD_OP) {
525 * method_op pkg_length name_string method_flags term_list
527 * Note: We must create the method node/object pair as soon as we
528 * see the method declaration. This allows later pass1 parsing
529 * of invocations of the method (need to know the number of
532 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
533 "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
534 walk_state, op, op->named.node));
536 if (!acpi_ns_get_attached_object(op->named.node)) {
537 walk_state->operands[0] =
538 ACPI_CAST_PTR(void, op->named.node);
539 walk_state->num_operands = 1;
542 acpi_ds_create_operands(walk_state,
545 if (ACPI_SUCCESS(status)) {
547 acpi_ex_create_method(op->named.
554 walk_state->operands[0] = NULL;
555 walk_state->num_operands = 0;
557 if (ACPI_FAILURE(status)) {
558 return_ACPI_STATUS(status);
564 /* Pop the scope stack (only if loading a table) */
566 if (!walk_state->method_node &&
567 op->common.aml_opcode != AML_EXTERNAL_OP &&
568 acpi_ns_opens_scope(object_type)) {
569 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
570 "(%s): Popping scope for Op %p\n",
571 acpi_ut_get_type_name(object_type), op));
573 status = acpi_ds_scope_stack_pop(walk_state);
576 return_ACPI_STATUS(status);