treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / acpi / acpica / dswload.c
blob697974e37edfbc1fda6b031d111828dd8c2774bf
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 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
11 #include "accommon.h"
12 #include "acparser.h"
13 #include "amlcode.h"
14 #include "acdispat.h"
15 #include "acinterp.h"
16 #include "acnamesp.h"
18 #ifdef ACPI_ASL_COMPILER
19 #include "acdisasm.h"
20 #endif
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
32 * RETURN: Status
34 * DESCRIPTION: Init walk state callbacks
36 ******************************************************************************/
37 acpi_status
38 acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
41 switch (pass_number) {
42 case 0:
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;
50 break;
52 case 1:
54 /* Load pass 1 */
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;
60 break;
62 case 2:
64 /* Load pass 2 */
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;
70 break;
72 case 3:
74 /* Execution pass */
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;
80 break;
82 default:
84 return (AE_BAD_PARAMETER);
87 return (AE_OK);
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
97 * RETURN: Status
99 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
101 ******************************************************************************/
103 acpi_status
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;
109 acpi_status status;
110 acpi_object_type object_type;
111 char *path;
112 u32 flags;
114 ACPI_FUNCTION_TRACE_PTR(ds_load1_begin_op, walk_state->op);
116 op = walk_state->op;
117 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
118 walk_state));
120 /* We are only interested in opcodes that have an associated name */
122 if (op) {
123 if (!(walk_state->op_info->flags & AML_NAMED)) {
124 *out_op = op;
125 return_ACPI_STATUS(AE_OK);
128 /* Check if this object has already been installed in the namespace */
130 if (op->common.node) {
131 *out_op = op;
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) {
147 case AML_SCOPE_OP:
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.
153 status =
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) {
160 * Table disassembly:
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);
166 status =
167 acpi_ns_lookup(walk_state->scope_info, path,
168 object_type, ACPI_IMODE_LOAD_PASS1,
169 ACPI_NS_SEARCH_PARENT, walk_state,
170 &node);
172 #endif
173 if (ACPI_FAILURE(status)) {
174 ACPI_ERROR_NAMESPACE(walk_state->scope_info, path,
175 status);
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) {
184 case ACPI_TYPE_ANY:
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 */
192 break;
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:
201 * Name (DEB, 0)
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;
215 break;
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) &&
223 (walk_state->
224 parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
225 break;
228 /*lint -fallthrough */
230 default:
232 /* All other types are an error */
234 ACPI_ERROR((AE_INFO,
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);
242 break;
244 default:
246 * For all other named opcodes, we will enter the name into
247 * the namespace.
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
254 * already exists:
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
259 * in the namespace.
261 if (walk_state->deferred_node) {
263 /* This name is already in the namespace, get the node */
265 node = walk_state->deferred_node;
266 status = AE_OK;
267 break;
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) {
275 node = NULL;
276 status = AE_OK;
277 break;
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
288 (object_type)));
289 } else {
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
294 (object_type)));
296 } else {
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
306 * parse tree later.
308 status =
309 acpi_ns_lookup(walk_state->scope_info, path, object_type,
310 ACPI_IMODE_LOAD_PASS1, flags, walk_state,
311 &node);
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)) {
328 status =
329 acpi_ds_scope_stack_push
330 (node, object_type,
331 walk_state);
332 if (ACPI_FAILURE(status)) {
333 return_ACPI_STATUS
334 (status);
338 status = AE_OK;
342 if (ACPI_FAILURE(status)) {
343 ACPI_ERROR_NAMESPACE(walk_state->scope_info,
344 path, status);
345 return_ACPI_STATUS(status);
348 break;
351 /* Common exit */
353 if (!op) {
355 /* Create a new op */
357 op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
358 if (!op) {
359 return_ACPI_STATUS(AE_NO_MEMORY);
363 /* Initialize the op */
365 #ifdef ACPI_CONSTANT_EVAL_ONLY
366 op->named.path = path;
367 #endif
369 if (node) {
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),
379 op);
380 *out_op = op;
381 return_ACPI_STATUS(status);
384 /*******************************************************************************
386 * FUNCTION: acpi_ds_load1_end_op
388 * PARAMETERS: walk_state - Current state of the parse tree walk
390 * RETURN: Status
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
404 u8 param_count;
405 #endif
407 ACPI_FUNCTION_TRACE(ds_load1_end_op);
409 op = walk_state->op;
410 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
411 walk_state));
414 * Disassembler: handle create field operators here.
416 * create_buffer_field is a deferred op that is typically processed in load
417 * pass 2. However, disassembly of control method contents walk the parse
418 * tree with ACPI_PARSE_LOAD_PASS1 and AML_CREATE operators are processed
419 * in a later walk. This is a problem when there is a control method that
420 * has the same name as the AML_CREATE object. In this case, any use of the
421 * name segment will be detected as a method call rather than a reference
422 * to a buffer field.
424 * This earlier creation during disassembly solves this issue by inserting
425 * the named object in the ACPI namespace so that references to this name
426 * would be a name string rather than a method call.
428 if ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) &&
429 (walk_state->op_info->flags & AML_CREATE)) {
430 status = acpi_ds_create_buffer_field(op, walk_state);
431 return_ACPI_STATUS(status);
434 /* We are only interested in opcodes that have an associated name */
436 if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
437 return_ACPI_STATUS(AE_OK);
440 /* Get the object type to determine if we should pop the scope */
442 object_type = walk_state->op_info->object_type;
444 if (walk_state->op_info->flags & AML_FIELD) {
446 * If we are executing a method, do not create any namespace objects
447 * during the load phase, only during execution.
449 if (!walk_state->method_node) {
450 if (walk_state->opcode == AML_FIELD_OP ||
451 walk_state->opcode == AML_BANK_FIELD_OP ||
452 walk_state->opcode == AML_INDEX_FIELD_OP) {
453 status =
454 acpi_ds_init_field_objects(op, walk_state);
457 return_ACPI_STATUS(status);
461 * If we are executing a method, do not create any namespace objects
462 * during the load phase, only during execution.
464 if (!walk_state->method_node) {
465 if (op->common.aml_opcode == AML_REGION_OP) {
466 status =
467 acpi_ex_create_region(op->named.data,
468 op->named.length,
469 (acpi_adr_space_type)
470 ((op->common.value.arg)->
471 common.value.integer),
472 walk_state);
473 if (ACPI_FAILURE(status)) {
474 return_ACPI_STATUS(status);
476 } else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
477 status =
478 acpi_ex_create_region(op->named.data,
479 op->named.length,
480 ACPI_ADR_SPACE_DATA_TABLE,
481 walk_state);
482 if (ACPI_FAILURE(status)) {
483 return_ACPI_STATUS(status);
488 if (op->common.aml_opcode == AML_NAME_OP) {
490 /* For Name opcode, get the object type from the argument */
492 if (op->common.value.arg) {
493 object_type = (acpi_ps_get_opcode_info((op->common.
494 value.arg)->
495 common.
496 aml_opcode))->
497 object_type;
499 /* Set node type if we have a namespace node */
501 if (op->common.node) {
502 op->common.node->type = (u8) object_type;
506 #ifdef ACPI_ASL_COMPILER
508 * For external opcode, get the object type from the argument and
509 * get the parameter count from the argument's next.
511 if (acpi_gbl_disasm_flag &&
512 op->common.node && op->common.aml_opcode == AML_EXTERNAL_OP) {
514 * Note, if this external is not a method
515 * Op->Common.Value.Arg->Common.Next->Common.Value.Integer == 0
516 * Therefore, param_count will be 0.
518 param_count =
519 (u8)op->common.value.arg->common.next->common.value.integer;
520 object_type = (u8)op->common.value.arg->common.value.integer;
521 op->common.node->flags |= ANOBJ_IS_EXTERNAL;
522 op->common.node->type = (u8)object_type;
524 acpi_dm_create_subobject_for_external((u8)object_type,
525 &op->common.node,
526 param_count);
529 * Add the external to the external list because we may be
530 * emitting code based off of the items within the external list.
532 acpi_dm_add_op_to_external_list(op, op->named.path,
533 (u8)object_type, param_count,
534 ACPI_EXT_ORIGIN_FROM_OPCODE |
535 ACPI_EXT_RESOLVED_REFERENCE);
537 #endif
540 * If we are executing a method, do not create any namespace objects
541 * during the load phase, only during execution.
543 if (!walk_state->method_node) {
544 if (op->common.aml_opcode == AML_METHOD_OP) {
546 * method_op pkg_length name_string method_flags term_list
548 * Note: We must create the method node/object pair as soon as we
549 * see the method declaration. This allows later pass1 parsing
550 * of invocations of the method (need to know the number of
551 * arguments.)
553 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
554 "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
555 walk_state, op, op->named.node));
557 if (!acpi_ns_get_attached_object(op->named.node)) {
558 walk_state->operands[0] =
559 ACPI_CAST_PTR(void, op->named.node);
560 walk_state->num_operands = 1;
562 status =
563 acpi_ds_create_operands(walk_state,
564 op->common.value.
565 arg);
566 if (ACPI_SUCCESS(status)) {
567 status =
568 acpi_ex_create_method(op->named.
569 data,
570 op->named.
571 length,
572 walk_state);
575 walk_state->operands[0] = NULL;
576 walk_state->num_operands = 0;
578 if (ACPI_FAILURE(status)) {
579 return_ACPI_STATUS(status);
585 /* Pop the scope stack (only if loading a table) */
587 if (!walk_state->method_node &&
588 op->common.aml_opcode != AML_EXTERNAL_OP &&
589 acpi_ns_opens_scope(object_type)) {
590 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
591 "(%s): Popping scope for Op %p\n",
592 acpi_ut_get_type_name(object_type), op));
594 status = acpi_ds_scope_stack_pop(walk_state);
597 return_ACPI_STATUS(status);