ACPICA: Dispatcher: Add trace support for interpreter
[firefly-linux-kernel-4.4.55.git] / drivers / acpi / acpica / psloop.c
1 /******************************************************************************
2  *
3  * Module Name: psloop - Main AML parse loop
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 /*
45  * Parse the AML and build an operation tree as most interpreters, (such as
46  * Perl) do. Parsing is done by hand rather than with a YACC generated parser
47  * to tightly constrain stack and dynamic memory usage. Parsing is kept
48  * flexible and the code fairly compact by parsing based on a list of AML
49  * opcode templates in aml_op_info[].
50  */
51
52 #include <acpi/acpi.h>
53 #include "accommon.h"
54 #include "acparser.h"
55 #include "acdispat.h"
56 #include "amlcode.h"
57
58 #define _COMPONENT          ACPI_PARSER
59 ACPI_MODULE_NAME("psloop")
60
61 /* Local prototypes */
62 static acpi_status
63 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
64                       u8 * aml_op_start, union acpi_parse_object *op);
65
66 static void
67 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
68                          u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
69
70 /*******************************************************************************
71  *
72  * FUNCTION:    acpi_ps_get_arguments
73  *
74  * PARAMETERS:  walk_state          - Current state
75  *              aml_op_start        - Op start in AML
76  *              op                  - Current Op
77  *
78  * RETURN:      Status
79  *
80  * DESCRIPTION: Get arguments for passed Op.
81  *
82  ******************************************************************************/
83
84 static acpi_status
85 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
86                       u8 * aml_op_start, union acpi_parse_object *op)
87 {
88         acpi_status status = AE_OK;
89         union acpi_parse_object *arg = NULL;
90         const struct acpi_opcode_info *op_info;
91
92         ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
93
94         switch (op->common.aml_opcode) {
95         case AML_BYTE_OP:       /* AML_BYTEDATA_ARG */
96         case AML_WORD_OP:       /* AML_WORDDATA_ARG */
97         case AML_DWORD_OP:      /* AML_DWORDATA_ARG */
98         case AML_QWORD_OP:      /* AML_QWORDATA_ARG */
99         case AML_STRING_OP:     /* AML_ASCIICHARLIST_ARG */
100
101                 /* Fill in constant or string argument directly */
102
103                 acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
104                                             GET_CURRENT_ARG_TYPE(walk_state->
105                                                                  arg_types),
106                                             op);
107                 break;
108
109         case AML_INT_NAMEPATH_OP:       /* AML_NAMESTRING_ARG */
110
111                 status =
112                     acpi_ps_get_next_namepath(walk_state,
113                                               &(walk_state->parser_state), op,
114                                               1);
115                 if (ACPI_FAILURE(status)) {
116                         return_ACPI_STATUS(status);
117                 }
118
119                 walk_state->arg_types = 0;
120                 break;
121
122         default:
123                 /*
124                  * Op is not a constant or string, append each argument to the Op
125                  */
126                 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types)
127                        && !walk_state->arg_count) {
128                         walk_state->aml = walk_state->parser_state.aml;
129
130                         status =
131                             acpi_ps_get_next_arg(walk_state,
132                                                  &(walk_state->parser_state),
133                                                  GET_CURRENT_ARG_TYPE
134                                                  (walk_state->arg_types), &arg);
135                         if (ACPI_FAILURE(status)) {
136                                 return_ACPI_STATUS(status);
137                         }
138
139                         if (arg) {
140                                 acpi_ps_append_arg(op, arg);
141                         }
142
143                         INCREMENT_ARG_LIST(walk_state->arg_types);
144                 }
145
146                 /*
147                  * Handle executable code at "module-level". This refers to
148                  * executable opcodes that appear outside of any control method.
149                  */
150                 if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
151                     ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
152                         /*
153                          * We want to skip If/Else/While constructs during Pass1 because we
154                          * want to actually conditionally execute the code during Pass2.
155                          *
156                          * Except for disassembly, where we always want to walk the
157                          * If/Else/While packages
158                          */
159                         switch (op->common.aml_opcode) {
160                         case AML_IF_OP:
161                         case AML_ELSE_OP:
162                         case AML_WHILE_OP:
163                                 /*
164                                  * Currently supported module-level opcodes are:
165                                  * IF/ELSE/WHILE. These appear to be the most common,
166                                  * and easiest to support since they open an AML
167                                  * package.
168                                  */
169                                 if (walk_state->pass_number ==
170                                     ACPI_IMODE_LOAD_PASS1) {
171                                         acpi_ps_link_module_code(op->common.
172                                                                  parent,
173                                                                  aml_op_start,
174                                                                  (u32)
175                                                                  (walk_state->
176                                                                  parser_state.
177                                                                  pkg_end -
178                                                                  aml_op_start),
179                                                                  walk_state->
180                                                                  owner_id);
181                                 }
182
183                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
184                                                   "Pass1: Skipping an If/Else/While body\n"));
185
186                                 /* Skip body of if/else/while in pass 1 */
187
188                                 walk_state->parser_state.aml =
189                                     walk_state->parser_state.pkg_end;
190                                 walk_state->arg_count = 0;
191                                 break;
192
193                         default:
194                                 /*
195                                  * Check for an unsupported executable opcode at module
196                                  * level. We must be in PASS1, the parent must be a SCOPE,
197                                  * The opcode class must be EXECUTE, and the opcode must
198                                  * not be an argument to another opcode.
199                                  */
200                                 if ((walk_state->pass_number ==
201                                      ACPI_IMODE_LOAD_PASS1)
202                                     && (op->common.parent->common.aml_opcode ==
203                                         AML_SCOPE_OP)) {
204                                         op_info =
205                                             acpi_ps_get_opcode_info(op->common.
206                                                                     aml_opcode);
207                                         if ((op_info->class ==
208                                              AML_CLASS_EXECUTE) && (!arg)) {
209                                                 ACPI_WARNING((AE_INFO,
210                                                               "Unsupported module-level executable opcode "
211                                                               "0x%.2X at table offset 0x%.4X",
212                                                               op->common.
213                                                               aml_opcode,
214                                                               (u32)
215                                                               (ACPI_PTR_DIFF
216                                                                (aml_op_start,
217                                                                 walk_state->
218                                                                 parser_state.
219                                                                 aml_start) +
220                                                                sizeof(struct
221                                                                       acpi_table_header))));
222                                         }
223                                 }
224                                 break;
225                         }
226                 }
227
228                 /* Special processing for certain opcodes */
229
230                 switch (op->common.aml_opcode) {
231                 case AML_METHOD_OP:
232                         /*
233                          * Skip parsing of control method because we don't have enough
234                          * info in the first pass to parse it correctly.
235                          *
236                          * Save the length and address of the body
237                          */
238                         op->named.data = walk_state->parser_state.aml;
239                         op->named.length = (u32)
240                             (walk_state->parser_state.pkg_end -
241                              walk_state->parser_state.aml);
242
243                         /* Skip body of method */
244
245                         walk_state->parser_state.aml =
246                             walk_state->parser_state.pkg_end;
247                         walk_state->arg_count = 0;
248                         break;
249
250                 case AML_BUFFER_OP:
251                 case AML_PACKAGE_OP:
252                 case AML_VAR_PACKAGE_OP:
253
254                         if ((op->common.parent) &&
255                             (op->common.parent->common.aml_opcode ==
256                              AML_NAME_OP)
257                             && (walk_state->pass_number <=
258                                 ACPI_IMODE_LOAD_PASS2)) {
259                                 /*
260                                  * Skip parsing of Buffers and Packages because we don't have
261                                  * enough info in the first pass to parse them correctly.
262                                  */
263                                 op->named.data = aml_op_start;
264                                 op->named.length = (u32)
265                                     (walk_state->parser_state.pkg_end -
266                                      aml_op_start);
267
268                                 /* Skip body */
269
270                                 walk_state->parser_state.aml =
271                                     walk_state->parser_state.pkg_end;
272                                 walk_state->arg_count = 0;
273                         }
274                         break;
275
276                 case AML_WHILE_OP:
277
278                         if (walk_state->control_state) {
279                                 walk_state->control_state->control.package_end =
280                                     walk_state->parser_state.pkg_end;
281                         }
282                         break;
283
284                 default:
285
286                         /* No action for all other opcodes */
287
288                         break;
289                 }
290
291                 break;
292         }
293
294         return_ACPI_STATUS(AE_OK);
295 }
296
297 /*******************************************************************************
298  *
299  * FUNCTION:    acpi_ps_link_module_code
300  *
301  * PARAMETERS:  parent_op           - Parent parser op
302  *              aml_start           - Pointer to the AML
303  *              aml_length          - Length of executable AML
304  *              owner_id            - owner_id of module level code
305  *
306  * RETURN:      None.
307  *
308  * DESCRIPTION: Wrap the module-level code with a method object and link the
309  *              object to the global list. Note, the mutex field of the method
310  *              object is used to link multiple module-level code objects.
311  *
312  ******************************************************************************/
313
314 static void
315 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
316                          u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
317 {
318         union acpi_operand_object *prev;
319         union acpi_operand_object *next;
320         union acpi_operand_object *method_obj;
321         struct acpi_namespace_node *parent_node;
322
323         /* Get the tail of the list */
324
325         prev = next = acpi_gbl_module_code_list;
326         while (next) {
327                 prev = next;
328                 next = next->method.mutex;
329         }
330
331         /*
332          * Insert the module level code into the list. Merge it if it is
333          * adjacent to the previous element.
334          */
335         if (!prev ||
336             ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
337
338                 /* Create, initialize, and link a new temporary method object */
339
340                 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
341                 if (!method_obj) {
342                         return;
343                 }
344
345                 if (parent_op->common.node) {
346                         parent_node = parent_op->common.node;
347                 } else {
348                         parent_node = acpi_gbl_root_node;
349                 }
350
351                 method_obj->method.aml_start = aml_start;
352                 method_obj->method.aml_length = aml_length;
353                 method_obj->method.owner_id = owner_id;
354                 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
355
356                 /*
357                  * Save the parent node in next_object. This is cheating, but we
358                  * don't want to expand the method object.
359                  */
360                 method_obj->method.next_object =
361                     ACPI_CAST_PTR(union acpi_operand_object, parent_node);
362
363                 if (!prev) {
364                         acpi_gbl_module_code_list = method_obj;
365                 } else {
366                         prev->method.mutex = method_obj;
367                 }
368         } else {
369                 prev->method.aml_length += aml_length;
370         }
371 }
372
373 /*******************************************************************************
374  *
375  * FUNCTION:    acpi_ps_parse_loop
376  *
377  * PARAMETERS:  walk_state          - Current state
378  *
379  * RETURN:      Status
380  *
381  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
382  *              a tree of ops.
383  *
384  ******************************************************************************/
385
386 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
387 {
388         acpi_status status = AE_OK;
389         union acpi_parse_object *op = NULL;     /* current op */
390         struct acpi_parse_state *parser_state;
391         u8 *aml_op_start = NULL;
392
393         ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
394
395         if (walk_state->descending_callback == NULL) {
396                 return_ACPI_STATUS(AE_BAD_PARAMETER);
397         }
398
399         parser_state = &walk_state->parser_state;
400         walk_state->arg_types = 0;
401
402 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
403
404         if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
405
406                 /* We are restarting a preempted control method */
407
408                 if (acpi_ps_has_completed_scope(parser_state)) {
409                         /*
410                          * We must check if a predicate to an IF or WHILE statement
411                          * was just completed
412                          */
413                         if ((parser_state->scope->parse_scope.op) &&
414                             ((parser_state->scope->parse_scope.op->common.
415                               aml_opcode == AML_IF_OP)
416                              || (parser_state->scope->parse_scope.op->common.
417                                  aml_opcode == AML_WHILE_OP))
418                             && (walk_state->control_state)
419                             && (walk_state->control_state->common.state ==
420                                 ACPI_CONTROL_PREDICATE_EXECUTING)) {
421                                 /*
422                                  * A predicate was just completed, get the value of the
423                                  * predicate and branch based on that value
424                                  */
425                                 walk_state->op = NULL;
426                                 status =
427                                     acpi_ds_get_predicate_value(walk_state,
428                                                                 ACPI_TO_POINTER
429                                                                 (TRUE));
430                                 if (ACPI_FAILURE(status)
431                                     && ((status & AE_CODE_MASK) !=
432                                         AE_CODE_CONTROL)) {
433                                         if (status == AE_AML_NO_RETURN_VALUE) {
434                                                 ACPI_EXCEPTION((AE_INFO, status,
435                                                                 "Invoked method did not return a value"));
436                                         }
437
438                                         ACPI_EXCEPTION((AE_INFO, status,
439                                                         "GetPredicate Failed"));
440                                         return_ACPI_STATUS(status);
441                                 }
442
443                                 status =
444                                     acpi_ps_next_parse_state(walk_state, op,
445                                                              status);
446                         }
447
448                         acpi_ps_pop_scope(parser_state, &op,
449                                           &walk_state->arg_types,
450                                           &walk_state->arg_count);
451                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
452                                           "Popped scope, Op=%p\n", op));
453                 } else if (walk_state->prev_op) {
454
455                         /* We were in the middle of an op */
456
457                         op = walk_state->prev_op;
458                         walk_state->arg_types = walk_state->prev_arg_types;
459                 }
460         }
461 #endif
462
463         /* Iterative parsing loop, while there is more AML to process: */
464
465         while ((parser_state->aml < parser_state->aml_end) || (op)) {
466                 aml_op_start = parser_state->aml;
467                 if (!op) {
468                         status =
469                             acpi_ps_create_op(walk_state, aml_op_start, &op);
470                         if (ACPI_FAILURE(status)) {
471                                 if (status == AE_CTRL_PARSE_CONTINUE) {
472                                         continue;
473                                 }
474
475                                 if (status == AE_CTRL_PARSE_PENDING) {
476                                         status = AE_OK;
477                                 }
478
479                                 if (status == AE_CTRL_TERMINATE) {
480                                         return_ACPI_STATUS(status);
481                                 }
482
483                                 status =
484                                     acpi_ps_complete_op(walk_state, &op,
485                                                         status);
486                                 if (ACPI_FAILURE(status)) {
487                                         return_ACPI_STATUS(status);
488                                 }
489
490                                 continue;
491                         }
492
493                         if (walk_state->op_info) {
494                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
495                                                   "Opcode %4.4X [%s] Op %p Aml %p\n",
496                                                   (u32)op->common.aml_opcode,
497                                                   walk_state->op_info->name, op,
498                                                   op->common.aml));
499                         }
500
501                         if (walk_state->op_info) {
502                                 ACPI_DEBUG_PRINT((ACPI_DB_TRACE_POINT,
503                                                   "Begin opcode: %s[0x%p] Class=0x%02x, Type=0x%02x, Flags=0x%04x.\n",
504                                                   op->common.aml_op_name,
505                                                   op->common.aml,
506                                                   walk_state->op_info->class,
507                                                   walk_state->op_info->type,
508                                                   walk_state->op_info->flags));
509                         } else {
510                                 ACPI_DEBUG_PRINT((ACPI_DB_TRACE_POINT,
511                                                   "Begin opcode: %s[0x%p].\n",
512                                                   op->common.aml_op_name,
513                                                   op->common.aml));
514                         }
515                 }
516
517                 /*
518                  * Start arg_count at zero because we don't know if there are
519                  * any args yet
520                  */
521                 walk_state->arg_count = 0;
522
523                 /* Are there any arguments that must be processed? */
524
525                 if (walk_state->arg_types) {
526
527                         /* Get arguments */
528
529                         status =
530                             acpi_ps_get_arguments(walk_state, aml_op_start, op);
531                         if (ACPI_FAILURE(status)) {
532                                 status =
533                                     acpi_ps_complete_op(walk_state, &op,
534                                                         status);
535                                 if (ACPI_FAILURE(status)) {
536                                         return_ACPI_STATUS(status);
537                                 }
538
539                                 continue;
540                         }
541                 }
542
543                 /* Check for arguments that need to be processed */
544
545                 if (walk_state->arg_count) {
546                         /*
547                          * There are arguments (complex ones), push Op and
548                          * prepare for argument
549                          */
550                         status = acpi_ps_push_scope(parser_state, op,
551                                                     walk_state->arg_types,
552                                                     walk_state->arg_count);
553                         if (ACPI_FAILURE(status)) {
554                                 status =
555                                     acpi_ps_complete_op(walk_state, &op,
556                                                         status);
557                                 if (ACPI_FAILURE(status)) {
558                                         return_ACPI_STATUS(status);
559                                 }
560
561                                 continue;
562                         }
563
564                         op = NULL;
565                         continue;
566                 }
567
568                 /*
569                  * All arguments have been processed -- Op is complete,
570                  * prepare for next
571                  */
572                 walk_state->op_info =
573                     acpi_ps_get_opcode_info(op->common.aml_opcode);
574                 if (walk_state->op_info->flags & AML_NAMED) {
575                         if (op->common.aml_opcode == AML_REGION_OP ||
576                             op->common.aml_opcode == AML_DATA_REGION_OP) {
577                                 /*
578                                  * Skip parsing of control method or opregion body,
579                                  * because we don't have enough info in the first pass
580                                  * to parse them correctly.
581                                  *
582                                  * Completed parsing an op_region declaration, we now
583                                  * know the length.
584                                  */
585                                 op->named.length =
586                                     (u32) (parser_state->aml - op->named.data);
587                         }
588                 }
589
590                 if (walk_state->op_info->flags & AML_CREATE) {
591                         /*
592                          * Backup to beginning of create_XXXfield declaration (1 for
593                          * Opcode)
594                          *
595                          * body_length is unknown until we parse the body
596                          */
597                         op->named.length =
598                             (u32) (parser_state->aml - op->named.data);
599                 }
600
601                 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
602                         /*
603                          * Backup to beginning of bank_field declaration
604                          *
605                          * body_length is unknown until we parse the body
606                          */
607                         op->named.length =
608                             (u32) (parser_state->aml - op->named.data);
609                 }
610
611                 /* This op complete, notify the dispatcher */
612
613                 if (walk_state->ascending_callback != NULL) {
614                         walk_state->op = op;
615                         walk_state->opcode = op->common.aml_opcode;
616
617                         status = walk_state->ascending_callback(walk_state);
618                         status =
619                             acpi_ps_next_parse_state(walk_state, op, status);
620                         if (status == AE_CTRL_PENDING) {
621                                 status = AE_OK;
622                         }
623                 }
624
625                 status = acpi_ps_complete_op(walk_state, &op, status);
626                 if (ACPI_FAILURE(status)) {
627                         return_ACPI_STATUS(status);
628                 }
629
630         }                       /* while parser_state->Aml */
631
632         status = acpi_ps_complete_final_op(walk_state, op, status);
633         return_ACPI_STATUS(status);
634 }