GNU Linux-libre 4.19.207-gnu1
[releases.git] / drivers / acpi / acpica / dswload.c
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
3  *
4  * Module Name: dswload - Dispatcher first pass namespace load callbacks
5  *
6  * Copyright (C) 2000 - 2018, Intel Corp.
7  *
8  *****************************************************************************/
9
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"
17
18 #ifdef ACPI_ASL_COMPILER
19 #include "acdisasm.h"
20 #endif
21
22 #define _COMPONENT          ACPI_DISPATCHER
23 ACPI_MODULE_NAME("dswload")
24
25 /*******************************************************************************
26  *
27  * FUNCTION:    acpi_ds_init_callbacks
28  *
29  * PARAMETERS:  walk_state      - Current state of the parse tree walk
30  *              pass_number     - 1, 2, or 3
31  *
32  * RETURN:      Status
33  *
34  * DESCRIPTION: Init walk state callbacks
35  *
36  ******************************************************************************/
37 acpi_status
38 acpi_ds_init_callbacks(struct acpi_walk_state *walk_state, u32 pass_number)
39 {
40
41         switch (pass_number) {
42         case 0:
43
44                 /* Parse only - caller will setup callbacks */
45
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;
51
52         case 1:
53
54                 /* Load pass 1 */
55
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;
61
62         case 2:
63
64                 /* Load pass 2 */
65
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;
71
72         case 3:
73
74                 /* Execution pass */
75
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;
83
84         default:
85
86                 return (AE_BAD_PARAMETER);
87         }
88
89         return (AE_OK);
90 }
91
92 /*******************************************************************************
93  *
94  * FUNCTION:    acpi_ds_load1_begin_op
95  *
96  * PARAMETERS:  walk_state      - Current state of the parse tree walk
97  *              out_op          - Where to return op if a new one is created
98  *
99  * RETURN:      Status
100  *
101  * DESCRIPTION: Descending callback used during the loading of ACPI tables.
102  *
103  ******************************************************************************/
104
105 acpi_status
106 acpi_ds_load1_begin_op(struct acpi_walk_state *walk_state,
107                        union acpi_parse_object **out_op)
108 {
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;
115
116         ACPI_FUNCTION_TRACE_PTR(ds_load1_begin_op, walk_state->op);
117
118         op = walk_state->op;
119         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
120                           walk_state));
121
122         /* We are only interested in opcodes that have an associated name */
123
124         if (op) {
125                 if (!(walk_state->op_info->flags & AML_NAMED)) {
126                         *out_op = op;
127                         return_ACPI_STATUS(AE_OK);
128                 }
129
130                 /* Check if this object has already been installed in the namespace */
131
132                 if (op->common.node) {
133                         *out_op = op;
134                         return_ACPI_STATUS(AE_OK);
135                 }
136         }
137
138         path = acpi_ps_get_next_namestring(&walk_state->parser_state);
139
140         /* Map the raw opcode into an internal object type */
141
142         object_type = walk_state->op_info->object_type;
143
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)));
147
148         switch (walk_state->opcode) {
149         case AML_SCOPE_OP:
150                 /*
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.
154                  */
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) {
161                         /*
162                          * Table disassembly:
163                          * Target of Scope() not found. Generate an External for it, and
164                          * insert the name into the namespace.
165                          */
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);
173                 }
174 #endif
175                 if (ACPI_FAILURE(status)) {
176                         ACPI_ERROR_NAMESPACE(walk_state->scope_info, path,
177                                              status);
178                         return_ACPI_STATUS(status);
179                 }
180
181                 /*
182                  * Check to make sure that the target is
183                  * one of the opcodes that actually opens a scope
184                  */
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:
192
193                         /* These are acceptable types */
194                         break;
195
196                 case ACPI_TYPE_INTEGER:
197                 case ACPI_TYPE_STRING:
198                 case ACPI_TYPE_BUFFER:
199                         /*
200                          * These types we will allow, but we will change the type.
201                          * This enables some existing code of the form:
202                          *
203                          *  Name (DEB, 0)
204                          *  Scope (DEB) { ... }
205                          *
206                          * Note: silently change the type here. On the second pass,
207                          * we will report a warning
208                          */
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)));
214
215                         node->type = ACPI_TYPE_ANY;
216                         walk_state->scope_info->common.value = ACPI_TYPE_ANY;
217                         break;
218
219                 case ACPI_TYPE_METHOD:
220                         /*
221                          * Allow scope change to root during execution of module-level
222                          * code. Root is typed METHOD during this time.
223                          */
224                         if ((node == acpi_gbl_root_node) &&
225                             (walk_state->
226                              parse_flags & ACPI_PARSE_MODULE_LEVEL)) {
227                                 break;
228                         }
229
230                         /*lint -fallthrough */
231
232                 default:
233
234                         /* All other types are an error */
235
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)));
241
242                         return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
243                 }
244                 break;
245
246         default:
247                 /*
248                  * For all other named opcodes, we will enter the name into
249                  * the namespace.
250                  *
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.
254                  *
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.
262                  */
263                 if (walk_state->deferred_node) {
264
265                         /* This name is already in the namespace, get the node */
266
267                         node = walk_state->deferred_node;
268                         status = AE_OK;
269                         break;
270                 }
271
272                 /*
273                  * If we are executing a method, do not create any namespace objects
274                  * during the load phase, only during execution.
275                  */
276                 if (walk_state->method_node) {
277                         node = NULL;
278                         status = AE_OK;
279                         break;
280                 }
281
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)));
297                         }
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)));
302                 }
303
304                 /*
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.
309                  */
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) {
316
317                                 /* The name already exists in this scope */
318
319                                 if (node->flags & ANOBJ_IS_EXTERNAL) {
320                                         /*
321                                          * Allow one create on an object or segment that was
322                                          * previously declared External
323                                          */
324                                         node->flags &= ~ANOBJ_IS_EXTERNAL;
325                                         node->type = (u8) object_type;
326
327                                         /* Just retyped a node, probably will need to open a scope */
328
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);
337                                                 }
338                                         }
339
340                                         status = AE_OK;
341                                 }
342                         }
343
344                         if (ACPI_FAILURE(status)) {
345                                 ACPI_ERROR_NAMESPACE(walk_state->scope_info,
346                                                      path, status);
347                                 return_ACPI_STATUS(status);
348                         }
349                 }
350                 break;
351         }
352
353         /* Common exit */
354
355         if (!op) {
356
357                 /* Create a new op */
358
359                 op = acpi_ps_alloc_op(walk_state->opcode, walk_state->aml);
360                 if (!op) {
361                         return_ACPI_STATUS(AE_NO_MEMORY);
362                 }
363         }
364
365         /* Initialize the op */
366
367 #if (defined (ACPI_NO_METHOD_EXECUTION) || defined (ACPI_CONSTANT_EVAL_ONLY))
368         op->named.path = path;
369 #endif
370
371         if (node) {
372                 /*
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
375                  */
376                 op->common.node = node;
377                 op->named.name = node->name.integer;
378         }
379
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);
384 }
385
386 /*******************************************************************************
387  *
388  * FUNCTION:    acpi_ds_load1_end_op
389  *
390  * PARAMETERS:  walk_state      - Current state of the parse tree walk
391  *
392  * RETURN:      Status
393  *
394  * DESCRIPTION: Ascending callback used during the loading of the namespace,
395  *              both control methods and everything else.
396  *
397  ******************************************************************************/
398
399 acpi_status acpi_ds_load1_end_op(struct acpi_walk_state *walk_state)
400 {
401         union acpi_parse_object *op;
402         acpi_object_type object_type;
403         acpi_status status = AE_OK;
404
405 #ifdef ACPI_ASL_COMPILER
406         u8 param_count;
407 #endif
408
409         ACPI_FUNCTION_TRACE(ds_load1_end_op);
410
411         op = walk_state->op;
412         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p State=%p\n", op,
413                           walk_state));
414
415         /*
416          * Disassembler: handle create field operators here.
417          *
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.
425          *
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.
429          */
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);
434         }
435
436         /* We are only interested in opcodes that have an associated name */
437
438         if (!(walk_state->op_info->flags & (AML_NAMED | AML_FIELD))) {
439                 return_ACPI_STATUS(AE_OK);
440         }
441
442         /* Get the object type to determine if we should pop the scope */
443
444         object_type = walk_state->op_info->object_type;
445
446 #ifndef ACPI_NO_METHOD_EXECUTION
447         if (walk_state->op_info->flags & AML_FIELD) {
448                 /*
449                  * If we are executing a method, do not create any namespace objects
450                  * during the load phase, only during execution.
451                  */
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);
458                         }
459                 }
460                 return_ACPI_STATUS(status);
461         }
462
463         /*
464          * If we are executing a method, do not create any namespace objects
465          * during the load phase, only during execution.
466          */
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);
478                         }
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);
487                         }
488                 }
489         }
490 #endif
491
492         if (op->common.aml_opcode == AML_NAME_OP) {
493
494                 /* For Name opcode, get the object type from the argument */
495
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;
502
503                         /* Set node type if we have a namespace node */
504
505                         if (op->common.node) {
506                                 op->common.node->type = (u8) object_type;
507                         }
508                 }
509         }
510 #ifdef ACPI_ASL_COMPILER
511         /*
512          * For external opcode, get the object type from the argument and
513          * get the parameter count from the argument's next.
514          */
515         if (acpi_gbl_disasm_flag &&
516             op->common.node && op->common.aml_opcode == AML_EXTERNAL_OP) {
517                 /*
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.
521                  */
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;
527
528                 acpi_dm_create_subobject_for_external((u8)object_type,
529                                                       &op->common.node,
530                                                       param_count);
531
532                 /*
533                  * Add the external to the external list because we may be
534                  * emitting code based off of the items within the external list.
535                  */
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);
540         }
541 #endif
542
543         /*
544          * If we are executing a method, do not create any namespace objects
545          * during the load phase, only during execution.
546          */
547         if (!walk_state->method_node) {
548                 if (op->common.aml_opcode == AML_METHOD_OP) {
549                         /*
550                          * method_op pkg_length name_string method_flags term_list
551                          *
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.)
556                          */
557                         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
558                                           "LOADING-Method: State=%p Op=%p NamedObj=%p\n",
559                                           walk_state, op, op->named.node));
560
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;
565
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);
577                                 }
578
579                                 walk_state->operands[0] = NULL;
580                                 walk_state->num_operands = 0;
581
582                                 if (ACPI_FAILURE(status)) {
583                                         return_ACPI_STATUS(status);
584                                 }
585                         }
586                 }
587         }
588
589         /* Pop the scope stack (only if loading a table) */
590
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));
597
598                 status = acpi_ds_scope_stack_pop(walk_state);
599         }
600
601         return_ACPI_STATUS(status);
602 }