Linux 4.19.133
[linux/fpc-iii.git] / drivers / acpi / acpica / dswload.c
blobba53662f121799a5c1d12945341dcc4eb1357184
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 - 2018, 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 #ifndef ACPI_NO_METHOD_EXECUTION
77 walk_state->parse_flags |= ACPI_PARSE_EXECUTE |
78 ACPI_PARSE_DELETE_TREE;
79 walk_state->descending_callback = acpi_ds_exec_begin_op;
80 walk_state->ascending_callback = acpi_ds_exec_end_op;
81 #endif
82 break;
84 default:
86 return (AE_BAD_PARAMETER);
89 return (AE_OK);
92 /*******************************************************************************
94 * FUNCTION: acpi_ds_load1_begin_op
96 * PARAMETERS: walk_state - Current state of the parse tree walk
97 * out_op - Where to return op if a new one is created
99 * RETURN: Status
101 * DESCRIPTION: Descending callback used during the loading of ACPI tables.
103 ******************************************************************************/
105 acpi_status
106 acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
107 union acpi_parse_object **out_op)
109 union acpi_parse_object *op;
110 struct acpi_namespace_node *node;
111 acpi_status status;
112 acpi_object_type object_type;
113 char *path;
114 u32 flags;
116 ACPI_FUNCTION_TRACE_PTR(ds_load1_begin_op, walk_state->op);
118 op = walk_state->op;
119 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
120 walk_state));
122 /* We are only interested in opcodes that have an associated name */
124 if (op) {
125 if (!(walk_state->op_info->flags & AML_NAMED)) {
126 *out_op = op;
127 return_ACPI_STATUS(AE_OK);
130 /* Check if this object has already been installed in the namespace */
132 if (op->common.node) {
133 *out_op = op;
134 return_ACPI_STATUS(AE_OK);
138 path = acpi_ps_get_next_namestring(&walk_state->parser_state);
140 /* Map the raw opcode into an internal object type */
142 object_type = walk_state->op_info->object_type;
144 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
145 "State=%p Op=%p [%s]\n", walk_state, op,
146 acpi_ut_get_type_name(object_type)));
148 switch (walk_state->opcode) {
149 case AML_SCOPE_OP:
151 * The target name of the Scope() operator must exist at this point so
152 * that we can actually open the scope to enter new names underneath it.
153 * Allow search-to-root for single namesegs.
155 status =
156 acpi_ns_lookup(walk_state->scope_info, path, object_type,
157 ACPI_IMODE_EXECUTE, ACPI_NS_SEARCH_PARENT,
158 walk_state, &(node));
159 #ifdef ACPI_ASL_COMPILER
160 if (status == AE_NOT_FOUND) {
162 * Table disassembly:
163 * Target of Scope() not found. Generate an External for it, and
164 * insert the name into the namespace.
166 acpi_dm_add_op_to_external_list(op, path,
167 ACPI_TYPE_DEVICE, 0, 0);
168 status =
169 acpi_ns_lookup(walk_state->scope_info, path,
170 object_type, ACPI_IMODE_LOAD_PASS1,
171 ACPI_NS_SEARCH_PARENT, walk_state,
172 &node);
174 #endif
175 if (ACPI_FAILURE(status)) {
176 ACPI_ERROR_NAMESPACE(walk_state->scope_info, path,
177 status);
178 return_ACPI_STATUS(status);
182 * Check to make sure that the target is
183 * one of the opcodes that actually opens a scope
185 switch (node->type) {
186 case ACPI_TYPE_ANY:
187 case ACPI_TYPE_LOCAL_SCOPE: /* Scope */
188 case ACPI_TYPE_DEVICE:
189 case ACPI_TYPE_POWER:
190 case ACPI_TYPE_PROCESSOR:
191 case ACPI_TYPE_THERMAL:
193 /* These are acceptable types */
194 break;
196 case ACPI_TYPE_INTEGER:
197 case ACPI_TYPE_STRING:
198 case ACPI_TYPE_BUFFER:
200 * These types we will allow, but we will change the type.
201 * This enables some existing code of the form:
203 * Name (DEB, 0)
204 * Scope (DEB) { ... }
206 * Note: silently change the type here. On the second pass,
207 * we will report a warning
209 ACPI_DEBUG_PRINT((ACPI_DB_INFO,
210 "Type override - [%4.4s] had invalid type (%s) "
211 "for Scope operator, changed to type ANY\n",
212 acpi_ut_get_node_name(node),
213 acpi_ut_get_type_name(node->type)));
215 node->type = ACPI_TYPE_ANY;
216 walk_state->scope_info->common.value = ACPI_TYPE_ANY;
217 break;
219 case ACPI_TYPE_METHOD:
221 * Allow scope change to root during execution of module-level
222 * code. Root is typed METHOD during this time.
224 if ((node == acpi_gbl_root_node) &&
225 (walk_state->
226 parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
227 break;
230 /*lint -fallthrough */
232 default:
234 /* All other types are an error */
236 ACPI_ERROR((AE_INFO,
237 "Invalid type (%s) for target of "
238 "Scope operator [%4.4s] (Cannot override)",
239 acpi_ut_get_type_name(node->type),
240 acpi_ut_get_node_name(node)));
242 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
244 break;
246 default:
248 * For all other named opcodes, we will enter the name into
249 * the namespace.
251 * Setup the search flags.
252 * Since we are entering a name into the namespace, we do not want to
253 * enable the search-to-root upsearch.
255 * There are only two conditions where it is acceptable that the name
256 * already exists:
257 * 1) the Scope() operator can reopen a scoping object that was
258 * previously defined (Scope, Method, Device, etc.)
259 * 2) Whenever we are parsing a deferred opcode (op_region, Buffer,
260 * buffer_field, or Package), the name of the object is already
261 * in the namespace.
263 if (walk_state->deferred_node) {
265 /* This name is already in the namespace, get the node */
267 node = walk_state->deferred_node;
268 status = AE_OK;
269 break;
273 * If we are executing a method, do not create any namespace objects
274 * during the load phase, only during execution.
276 if (walk_state->method_node) {
277 node = NULL;
278 status = AE_OK;
279 break;
282 flags = ACPI_NS_NO_UPSEARCH;
283 if ((walk_state->opcode != AML_SCOPE_OP) &&
284 (!(walk_state->parse_flags & ACPI_PARSE_DEFERRED_OP))) {
285 if (walk_state->namespace_override) {
286 flags |= ACPI_NS_OVERRIDE_IF_FOUND;
287 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
288 "[%s] Override allowed\n",
289 acpi_ut_get_type_name
290 (object_type)));
291 } else {
292 flags |= ACPI_NS_ERROR_IF_FOUND;
293 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
294 "[%s] Cannot already exist\n",
295 acpi_ut_get_type_name
296 (object_type)));
298 } else {
299 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
300 "[%s] Both Find or Create allowed\n",
301 acpi_ut_get_type_name(object_type)));
305 * Enter the named type into the internal namespace. We enter the name
306 * as we go downward in the parse tree. Any necessary subobjects that
307 * involve arguments to the opcode must be created as we go back up the
308 * parse tree later.
310 status =
311 acpi_ns_lookup(walk_state->scope_info, path, object_type,
312 ACPI_IMODE_LOAD_PASS1, flags, walk_state,
313 &node);
314 if (ACPI_FAILURE(status)) {
315 if (status == AE_ALREADY_EXISTS) {
317 /* The name already exists in this scope */
319 if (node->flags & ANOBJ_IS_EXTERNAL) {
321 * Allow one create on an object or segment that was
322 * previously declared External
324 node->flags &= ~ANOBJ_IS_EXTERNAL;
325 node->type = (u8) object_type;
327 /* Just retyped a node, probably will need to open a scope */
329 if (acpi_ns_opens_scope(object_type)) {
330 status =
331 acpi_ds_scope_stack_push
332 (node, object_type,
333 walk_state);
334 if (ACPI_FAILURE(status)) {
335 return_ACPI_STATUS
336 (status);
340 status = AE_OK;
344 if (ACPI_FAILURE(status)) {
345 ACPI_ERROR_NAMESPACE(walk_state->scope_info,
346 path, status);
347 return_ACPI_STATUS(status);
350 break;
353 /* Common exit */
355 if (!op) {
357 /* Create a new op */
359 op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
360 if (!op) {
361 return_ACPI_STATUS(AE_NO_MEMORY);
365 /* Initialize the op */
367 #if (defined (ACPI_NO_METHOD_EXECUTION) || defined (ACPI_CONSTANT_EVAL_ONLY))
368 op->named.path = path;
369 #endif
371 if (node) {
373 * Put the Node in the "op" object that the parser uses, so we
374 * can get it again quickly when this scope is closed
376 op->common.node = node;
377 op->named.name = node->name.integer;
380 acpi_ps_append_arg(acpi_ps_get_parent_scope(&walk_state->parser_state),
381 op);
382 *out_op = op;
383 return_ACPI_STATUS(status);
386 /*******************************************************************************
388 * FUNCTION: acpi_ds_load1_end_op
390 * PARAMETERS: walk_state - Current state of the parse tree walk
392 * RETURN: Status
394 * DESCRIPTION: Ascending callback used during the loading of the namespace,
395 * both control methods and everything else.
397 ******************************************************************************/
399 acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
401 union acpi_parse_object *op;
402 acpi_object_type object_type;
403 acpi_status status = AE_OK;
405 #ifdef ACPI_ASL_COMPILER
406 u8 param_count;
407 #endif
409 ACPI_FUNCTION_TRACE(ds_load1_end_op);
411 op = walk_state->op;
412 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
413 walk_state));
416 * Disassembler: handle create field operators here.
418 * create_buffer_field is a deferred op that is typically processed in load
419 * pass 2. However, disassembly of control method contents walk the parse
420 * tree with ACPI_PARSE_LOAD_PASS1 and AML_CREATE operators are processed
421 * in a later walk. This is a problem when there is a control method that
422 * has the same name as the AML_CREATE object. In this case, any use of the
423 * name segment will be detected as a method call rather than a reference
424 * to a buffer field.
426 * This earlier creation during disassembly solves this issue by inserting
427 * the named object in the ACPI namespace so that references to this name
428 * would be a name string rather than a method call.
430 if ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) &&
431 (walk_state->op_info->flags & AML_CREATE)) {
432 status = acpi_ds_create_buffer_field(op, walk_state);
433 return_ACPI_STATUS(status);
436 /* We are only interested in opcodes that have an associated name */
438 if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
439 return_ACPI_STATUS(AE_OK);
442 /* Get the object type to determine if we should pop the scope */
444 object_type = walk_state->op_info->object_type;
446 #ifndef ACPI_NO_METHOD_EXECUTION
447 if (walk_state->op_info->flags & AML_FIELD) {
449 * If we are executing a method, do not create any namespace objects
450 * during the load phase, only during execution.
452 if (!walk_state->method_node) {
453 if (walk_state->opcode == AML_FIELD_OP ||
454 walk_state->opcode == AML_BANK_FIELD_OP ||
455 walk_state->opcode == AML_INDEX_FIELD_OP) {
456 status =
457 acpi_ds_init_field_objects(op, walk_state);
460 return_ACPI_STATUS(status);
464 * If we are executing a method, do not create any namespace objects
465 * during the load phase, only during execution.
467 if (!walk_state->method_node) {
468 if (op->common.aml_opcode == AML_REGION_OP) {
469 status =
470 acpi_ex_create_region(op->named.data,
471 op->named.length,
472 (acpi_adr_space_type)
473 ((op->common.value.arg)->
474 common.value.integer),
475 walk_state);
476 if (ACPI_FAILURE(status)) {
477 return_ACPI_STATUS(status);
479 } else if (op->common.aml_opcode == AML_DATA_REGION_OP) {
480 status =
481 acpi_ex_create_region(op->named.data,
482 op->named.length,
483 ACPI_ADR_SPACE_DATA_TABLE,
484 walk_state);
485 if (ACPI_FAILURE(status)) {
486 return_ACPI_STATUS(status);
490 #endif
492 if (op->common.aml_opcode == AML_NAME_OP) {
494 /* For Name opcode, get the object type from the argument */
496 if (op->common.value.arg) {
497 object_type = (acpi_ps_get_opcode_info((op->common.
498 value.arg)->
499 common.
500 aml_opcode))->
501 object_type;
503 /* Set node type if we have a namespace node */
505 if (op->common.node) {
506 op->common.node->type = (u8) object_type;
510 #ifdef ACPI_ASL_COMPILER
512 * For external opcode, get the object type from the argument and
513 * get the parameter count from the argument's next.
515 if (acpi_gbl_disasm_flag &&
516 op->common.node && op->common.aml_opcode == AML_EXTERNAL_OP) {
518 * Note, if this external is not a method
519 * Op->Common.Value.Arg->Common.Next->Common.Value.Integer == 0
520 * Therefore, param_count will be 0.
522 param_count =
523 (u8)op->common.value.arg->common.next->common.value.integer;
524 object_type = (u8)op->common.value.arg->common.value.integer;
525 op->common.node->flags |= ANOBJ_IS_EXTERNAL;
526 op->common.node->type = (u8)object_type;
528 acpi_dm_create_subobject_for_external((u8)object_type,
529 &op->common.node,
530 param_count);
533 * Add the external to the external list because we may be
534 * emitting code based off of the items within the external list.
536 acpi_dm_add_op_to_external_list(op, op->named.path,
537 (u8)object_type, param_count,
538 ACPI_EXT_ORIGIN_FROM_OPCODE |
539 ACPI_EXT_RESOLVED_REFERENCE);
541 #endif
544 * If we are executing a method, do not create any namespace objects
545 * during the load phase, only during execution.
547 if (!walk_state->method_node) {
548 if (op->common.aml_opcode == AML_METHOD_OP) {
550 * method_op pkg_length name_string method_flags term_list
552 * Note: We must create the method node/object pair as soon as we
553 * see the method declaration. This allows later pass1 parsing
554 * of invocations of the method (need to know the number of
555 * arguments.)
557 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
558 "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
559 walk_state, op, op->named.node));
561 if (!acpi_ns_get_attached_object(op->named.node)) {
562 walk_state->operands[0] =
563 ACPI_CAST_PTR(void, op->named.node);
564 walk_state->num_operands = 1;
566 status =
567 acpi_ds_create_operands(walk_state,
568 op->common.value.
569 arg);
570 if (ACPI_SUCCESS(status)) {
571 status =
572 acpi_ex_create_method(op->named.
573 data,
574 op->named.
575 length,
576 walk_state);
579 walk_state->operands[0] = NULL;
580 walk_state->num_operands = 0;
582 if (ACPI_FAILURE(status)) {
583 return_ACPI_STATUS(status);
589 /* Pop the scope stack (only if loading a table) */
591 if (!walk_state->method_node &&
592 op->common.aml_opcode != AML_EXTERNAL_OP &&
593 acpi_ns_opens_scope(object_type)) {
594 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
595 "(%s): Popping scope for Op %p\n",
596 acpi_ut_get_type_name(object_type), op));
598 status = acpi_ds_scope_stack_pop(walk_state);
601 return_ACPI_STATUS(status);