d5843830fef809d9ec58d499828bf01633cc4cd5
[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                                 arg->common.aml = walk_state->aml;
141                                 acpi_ps_append_arg(op, arg);
142                         }
143
144                         INCREMENT_ARG_LIST(walk_state->arg_types);
145                 }
146
147                 /*
148                  * Handle executable code at "module-level". This refers to
149                  * executable opcodes that appear outside of any control method.
150                  */
151                 if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
152                     ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
153                         /*
154                          * We want to skip If/Else/While constructs during Pass1 because we
155                          * want to actually conditionally execute the code during Pass2.
156                          *
157                          * Except for disassembly, where we always want to walk the
158                          * If/Else/While packages
159                          */
160                         switch (op->common.aml_opcode) {
161                         case AML_IF_OP:
162                         case AML_ELSE_OP:
163                         case AML_WHILE_OP:
164                                 /*
165                                  * Currently supported module-level opcodes are:
166                                  * IF/ELSE/WHILE. These appear to be the most common,
167                                  * and easiest to support since they open an AML
168                                  * package.
169                                  */
170                                 if (walk_state->pass_number ==
171                                     ACPI_IMODE_LOAD_PASS1) {
172                                         acpi_ps_link_module_code(op->common.
173                                                                  parent,
174                                                                  aml_op_start,
175                                                                  (u32)
176                                                                  (walk_state->
177                                                                  parser_state.
178                                                                  pkg_end -
179                                                                  aml_op_start),
180                                                                  walk_state->
181                                                                  owner_id);
182                                 }
183
184                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
185                                                   "Pass1: Skipping an If/Else/While body\n"));
186
187                                 /* Skip body of if/else/while in pass 1 */
188
189                                 walk_state->parser_state.aml =
190                                     walk_state->parser_state.pkg_end;
191                                 walk_state->arg_count = 0;
192                                 break;
193
194                         default:
195                                 /*
196                                  * Check for an unsupported executable opcode at module
197                                  * level. We must be in PASS1, the parent must be a SCOPE,
198                                  * The opcode class must be EXECUTE, and the opcode must
199                                  * not be an argument to another opcode.
200                                  */
201                                 if ((walk_state->pass_number ==
202                                      ACPI_IMODE_LOAD_PASS1)
203                                     && (op->common.parent->common.aml_opcode ==
204                                         AML_SCOPE_OP)) {
205                                         op_info =
206                                             acpi_ps_get_opcode_info(op->common.
207                                                                     aml_opcode);
208                                         if ((op_info->class ==
209                                              AML_CLASS_EXECUTE) && (!arg)) {
210                                                 ACPI_WARNING((AE_INFO,
211                                                               "Unsupported module-level executable opcode "
212                                                               "0x%.2X at table offset 0x%.4X",
213                                                               op->common.
214                                                               aml_opcode,
215                                                               (u32)
216                                                               (ACPI_PTR_DIFF
217                                                                (aml_op_start,
218                                                                 walk_state->
219                                                                 parser_state.
220                                                                 aml_start) +
221                                                                sizeof(struct
222                                                                       acpi_table_header))));
223                                         }
224                                 }
225                                 break;
226                         }
227                 }
228
229                 /* Special processing for certain opcodes */
230
231                 switch (op->common.aml_opcode) {
232                 case AML_METHOD_OP:
233                         /*
234                          * Skip parsing of control method because we don't have enough
235                          * info in the first pass to parse it correctly.
236                          *
237                          * Save the length and address of the body
238                          */
239                         op->named.data = walk_state->parser_state.aml;
240                         op->named.length = (u32)
241                             (walk_state->parser_state.pkg_end -
242                              walk_state->parser_state.aml);
243
244                         /* Skip body of method */
245
246                         walk_state->parser_state.aml =
247                             walk_state->parser_state.pkg_end;
248                         walk_state->arg_count = 0;
249                         break;
250
251                 case AML_BUFFER_OP:
252                 case AML_PACKAGE_OP:
253                 case AML_VAR_PACKAGE_OP:
254
255                         if ((op->common.parent) &&
256                             (op->common.parent->common.aml_opcode ==
257                              AML_NAME_OP)
258                             && (walk_state->pass_number <=
259                                 ACPI_IMODE_LOAD_PASS2)) {
260                                 /*
261                                  * Skip parsing of Buffers and Packages because we don't have
262                                  * enough info in the first pass to parse them correctly.
263                                  */
264                                 op->named.data = aml_op_start;
265                                 op->named.length = (u32)
266                                     (walk_state->parser_state.pkg_end -
267                                      aml_op_start);
268
269                                 /* Skip body */
270
271                                 walk_state->parser_state.aml =
272                                     walk_state->parser_state.pkg_end;
273                                 walk_state->arg_count = 0;
274                         }
275                         break;
276
277                 case AML_WHILE_OP:
278
279                         if (walk_state->control_state) {
280                                 walk_state->control_state->control.package_end =
281                                     walk_state->parser_state.pkg_end;
282                         }
283                         break;
284
285                 default:
286
287                         /* No action for all other opcodes */
288
289                         break;
290                 }
291
292                 break;
293         }
294
295         return_ACPI_STATUS(AE_OK);
296 }
297
298 /*******************************************************************************
299  *
300  * FUNCTION:    acpi_ps_link_module_code
301  *
302  * PARAMETERS:  parent_op           - Parent parser op
303  *              aml_start           - Pointer to the AML
304  *              aml_length          - Length of executable AML
305  *              owner_id            - owner_id of module level code
306  *
307  * RETURN:      None.
308  *
309  * DESCRIPTION: Wrap the module-level code with a method object and link the
310  *              object to the global list. Note, the mutex field of the method
311  *              object is used to link multiple module-level code objects.
312  *
313  ******************************************************************************/
314
315 static void
316 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
317                          u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
318 {
319         union acpi_operand_object *prev;
320         union acpi_operand_object *next;
321         union acpi_operand_object *method_obj;
322         struct acpi_namespace_node *parent_node;
323
324         /* Get the tail of the list */
325
326         prev = next = acpi_gbl_module_code_list;
327         while (next) {
328                 prev = next;
329                 next = next->method.mutex;
330         }
331
332         /*
333          * Insert the module level code into the list. Merge it if it is
334          * adjacent to the previous element.
335          */
336         if (!prev ||
337             ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
338
339                 /* Create, initialize, and link a new temporary method object */
340
341                 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
342                 if (!method_obj) {
343                         return;
344                 }
345
346                 if (parent_op->common.node) {
347                         parent_node = parent_op->common.node;
348                 } else {
349                         parent_node = acpi_gbl_root_node;
350                 }
351
352                 method_obj->method.aml_start = aml_start;
353                 method_obj->method.aml_length = aml_length;
354                 method_obj->method.owner_id = owner_id;
355                 method_obj->method.info_flags |= ACPI_METHOD_MODULE_LEVEL;
356
357                 /*
358                  * Save the parent node in next_object. This is cheating, but we
359                  * don't want to expand the method object.
360                  */
361                 method_obj->method.next_object =
362                     ACPI_CAST_PTR(union acpi_operand_object, parent_node);
363
364                 if (!prev) {
365                         acpi_gbl_module_code_list = method_obj;
366                 } else {
367                         prev->method.mutex = method_obj;
368                 }
369         } else {
370                 prev->method.aml_length += aml_length;
371         }
372 }
373
374 /*******************************************************************************
375  *
376  * FUNCTION:    acpi_ps_parse_loop
377  *
378  * PARAMETERS:  walk_state          - Current state
379  *
380  * RETURN:      Status
381  *
382  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
383  *              a tree of ops.
384  *
385  ******************************************************************************/
386
387 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
388 {
389         acpi_status status = AE_OK;
390         union acpi_parse_object *op = NULL;     /* current op */
391         struct acpi_parse_state *parser_state;
392         u8 *aml_op_start = NULL;
393
394         ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
395
396         if (walk_state->descending_callback == NULL) {
397                 return_ACPI_STATUS(AE_BAD_PARAMETER);
398         }
399
400         parser_state = &walk_state->parser_state;
401         walk_state->arg_types = 0;
402
403 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
404
405         if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
406
407                 /* We are restarting a preempted control method */
408
409                 if (acpi_ps_has_completed_scope(parser_state)) {
410                         /*
411                          * We must check if a predicate to an IF or WHILE statement
412                          * was just completed
413                          */
414                         if ((parser_state->scope->parse_scope.op) &&
415                             ((parser_state->scope->parse_scope.op->common.
416                               aml_opcode == AML_IF_OP)
417                              || (parser_state->scope->parse_scope.op->common.
418                                  aml_opcode == AML_WHILE_OP))
419                             && (walk_state->control_state)
420                             && (walk_state->control_state->common.state ==
421                                 ACPI_CONTROL_PREDICATE_EXECUTING)) {
422                                 /*
423                                  * A predicate was just completed, get the value of the
424                                  * predicate and branch based on that value
425                                  */
426                                 walk_state->op = NULL;
427                                 status =
428                                     acpi_ds_get_predicate_value(walk_state,
429                                                                 ACPI_TO_POINTER
430                                                                 (TRUE));
431                                 if (ACPI_FAILURE(status)
432                                     && ((status & AE_CODE_MASK) !=
433                                         AE_CODE_CONTROL)) {
434                                         if (status == AE_AML_NO_RETURN_VALUE) {
435                                                 ACPI_EXCEPTION((AE_INFO, status,
436                                                                 "Invoked method did not return a value"));
437                                         }
438
439                                         ACPI_EXCEPTION((AE_INFO, status,
440                                                         "GetPredicate Failed"));
441                                         return_ACPI_STATUS(status);
442                                 }
443
444                                 status =
445                                     acpi_ps_next_parse_state(walk_state, op,
446                                                              status);
447                         }
448
449                         acpi_ps_pop_scope(parser_state, &op,
450                                           &walk_state->arg_types,
451                                           &walk_state->arg_count);
452                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
453                                           "Popped scope, Op=%p\n", op));
454                 } else if (walk_state->prev_op) {
455
456                         /* We were in the middle of an op */
457
458                         op = walk_state->prev_op;
459                         walk_state->arg_types = walk_state->prev_arg_types;
460                 }
461         }
462 #endif
463
464         /* Iterative parsing loop, while there is more AML to process: */
465
466         while ((parser_state->aml < parser_state->aml_end) || (op)) {
467                 aml_op_start = parser_state->aml;
468                 if (!op) {
469                         status =
470                             acpi_ps_create_op(walk_state, aml_op_start, &op);
471                         if (ACPI_FAILURE(status)) {
472                                 if (status == AE_CTRL_PARSE_CONTINUE) {
473                                         continue;
474                                 }
475
476                                 if (status == AE_CTRL_PARSE_PENDING) {
477                                         status = AE_OK;
478                                 }
479
480                                 if (status == AE_CTRL_TERMINATE) {
481                                         return_ACPI_STATUS(status);
482                                 }
483
484                                 status =
485                                     acpi_ps_complete_op(walk_state, &op,
486                                                         status);
487                                 if (ACPI_FAILURE(status)) {
488                                         return_ACPI_STATUS(status);
489                                 }
490
491                                 continue;
492                         }
493
494                         op->common.aml = walk_state->aml;
495
496                         if (walk_state->op_info) {
497                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
498                                                   "Opcode %4.4X [%s] Op %p Aml %p\n",
499                                                   (u32)op->common.aml_opcode,
500                                                   walk_state->op_info->name, op,
501                                                   op->common.aml));
502                         }
503                 }
504
505                 /*
506                  * Start arg_count at zero because we don't know if there are
507                  * any args yet
508                  */
509                 walk_state->arg_count = 0;
510
511                 /* Are there any arguments that must be processed? */
512
513                 if (walk_state->arg_types) {
514
515                         /* Get arguments */
516
517                         status =
518                             acpi_ps_get_arguments(walk_state, aml_op_start, op);
519                         if (ACPI_FAILURE(status)) {
520                                 status =
521                                     acpi_ps_complete_op(walk_state, &op,
522                                                         status);
523                                 if (ACPI_FAILURE(status)) {
524                                         return_ACPI_STATUS(status);
525                                 }
526
527                                 continue;
528                         }
529                 }
530
531                 /* Check for arguments that need to be processed */
532
533                 if (walk_state->arg_count) {
534                         /*
535                          * There are arguments (complex ones), push Op and
536                          * prepare for argument
537                          */
538                         status = acpi_ps_push_scope(parser_state, op,
539                                                     walk_state->arg_types,
540                                                     walk_state->arg_count);
541                         if (ACPI_FAILURE(status)) {
542                                 status =
543                                     acpi_ps_complete_op(walk_state, &op,
544                                                         status);
545                                 if (ACPI_FAILURE(status)) {
546                                         return_ACPI_STATUS(status);
547                                 }
548
549                                 continue;
550                         }
551
552                         op = NULL;
553                         continue;
554                 }
555
556                 /*
557                  * All arguments have been processed -- Op is complete,
558                  * prepare for next
559                  */
560                 walk_state->op_info =
561                     acpi_ps_get_opcode_info(op->common.aml_opcode);
562                 if (walk_state->op_info->flags & AML_NAMED) {
563                         if (op->common.aml_opcode == AML_REGION_OP ||
564                             op->common.aml_opcode == AML_DATA_REGION_OP) {
565                                 /*
566                                  * Skip parsing of control method or opregion body,
567                                  * because we don't have enough info in the first pass
568                                  * to parse them correctly.
569                                  *
570                                  * Completed parsing an op_region declaration, we now
571                                  * know the length.
572                                  */
573                                 op->named.length =
574                                     (u32) (parser_state->aml - op->named.data);
575                         }
576                 }
577
578                 if (walk_state->op_info->flags & AML_CREATE) {
579                         /*
580                          * Backup to beginning of create_XXXfield declaration (1 for
581                          * Opcode)
582                          *
583                          * body_length is unknown until we parse the body
584                          */
585                         op->named.length =
586                             (u32) (parser_state->aml - op->named.data);
587                 }
588
589                 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
590                         /*
591                          * Backup to beginning of bank_field declaration
592                          *
593                          * body_length is unknown until we parse the body
594                          */
595                         op->named.length =
596                             (u32) (parser_state->aml - op->named.data);
597                 }
598
599                 /* This op complete, notify the dispatcher */
600
601                 if (walk_state->ascending_callback != NULL) {
602                         walk_state->op = op;
603                         walk_state->opcode = op->common.aml_opcode;
604
605                         status = walk_state->ascending_callback(walk_state);
606                         status =
607                             acpi_ps_next_parse_state(walk_state, op, status);
608                         if (status == AE_CTRL_PENDING) {
609                                 status = AE_OK;
610                         }
611                 }
612
613                 status = acpi_ps_complete_op(walk_state, &op, status);
614                 if (ACPI_FAILURE(status)) {
615                         return_ACPI_STATUS(status);
616                 }
617
618         }                       /* while parser_state->Aml */
619
620         status = acpi_ps_complete_final_op(walk_state, op, status);
621         return_ACPI_STATUS(status);
622 }