ACPICA: Update all ACPICA copyrights and signons to 2010
[linux-3.10.git] / drivers / acpi / acpica / psloop.c
1 /******************************************************************************
2  *
3  * Module Name: psloop - Main AML parse loop
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2010, 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 static u32 acpi_gbl_depth = 0;
62
63 /* Local prototypes */
64
65 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
66
67 static acpi_status
68 acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
69                        u8 * aml_op_start,
70                        union acpi_parse_object *unnamed_op,
71                        union acpi_parse_object **op);
72
73 static acpi_status
74 acpi_ps_create_op(struct acpi_walk_state *walk_state,
75                   u8 * aml_op_start, union acpi_parse_object **new_op);
76
77 static acpi_status
78 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
79                       u8 * aml_op_start, union acpi_parse_object *op);
80
81 static acpi_status
82 acpi_ps_complete_op(struct acpi_walk_state *walk_state,
83                     union acpi_parse_object **op, acpi_status status);
84
85 static acpi_status
86 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
87                           union acpi_parse_object *op, acpi_status status);
88
89 static void
90 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
91                          u8 *aml_start, u32 aml_length, acpi_owner_id owner_id);
92
93 /*******************************************************************************
94  *
95  * FUNCTION:    acpi_ps_get_aml_opcode
96  *
97  * PARAMETERS:  walk_state          - Current state
98  *
99  * RETURN:      Status
100  *
101  * DESCRIPTION: Extract the next AML opcode from the input stream.
102  *
103  ******************************************************************************/
104
105 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
106 {
107
108         ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
109
110         walk_state->aml_offset =
111             (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
112                                 walk_state->parser_state.aml_start);
113         walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
114
115         /*
116          * First cut to determine what we have found:
117          * 1) A valid AML opcode
118          * 2) A name string
119          * 3) An unknown/invalid opcode
120          */
121         walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
122
123         switch (walk_state->op_info->class) {
124         case AML_CLASS_ASCII:
125         case AML_CLASS_PREFIX:
126                 /*
127                  * Starts with a valid prefix or ASCII char, this is a name
128                  * string. Convert the bare name string to a namepath.
129                  */
130                 walk_state->opcode = AML_INT_NAMEPATH_OP;
131                 walk_state->arg_types = ARGP_NAMESTRING;
132                 break;
133
134         case AML_CLASS_UNKNOWN:
135
136                 /* The opcode is unrecognized. Just skip unknown opcodes */
137
138                 ACPI_ERROR((AE_INFO,
139                             "Found unknown opcode %X at AML address %p offset %X, ignoring",
140                             walk_state->opcode, walk_state->parser_state.aml,
141                             walk_state->aml_offset));
142
143                 ACPI_DUMP_BUFFER(walk_state->parser_state.aml, 128);
144
145                 /* Assume one-byte bad opcode */
146
147                 walk_state->parser_state.aml++;
148                 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
149
150         default:
151
152                 /* Found opcode info, this is a normal opcode */
153
154                 walk_state->parser_state.aml +=
155                     acpi_ps_get_opcode_size(walk_state->opcode);
156                 walk_state->arg_types = walk_state->op_info->parse_args;
157                 break;
158         }
159
160         return_ACPI_STATUS(AE_OK);
161 }
162
163 /*******************************************************************************
164  *
165  * FUNCTION:    acpi_ps_build_named_op
166  *
167  * PARAMETERS:  walk_state          - Current state
168  *              aml_op_start        - Begin of named Op in AML
169  *              unnamed_op          - Early Op (not a named Op)
170  *              Op                  - Returned Op
171  *
172  * RETURN:      Status
173  *
174  * DESCRIPTION: Parse a named Op
175  *
176  ******************************************************************************/
177
178 static acpi_status
179 acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
180                        u8 * aml_op_start,
181                        union acpi_parse_object *unnamed_op,
182                        union acpi_parse_object **op)
183 {
184         acpi_status status = AE_OK;
185         union acpi_parse_object *arg = NULL;
186
187         ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
188
189         unnamed_op->common.value.arg = NULL;
190         unnamed_op->common.arg_list_length = 0;
191         unnamed_op->common.aml_opcode = walk_state->opcode;
192
193         /*
194          * Get and append arguments until we find the node that contains
195          * the name (the type ARGP_NAME).
196          */
197         while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
198                (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
199                 status =
200                     acpi_ps_get_next_arg(walk_state,
201                                          &(walk_state->parser_state),
202                                          GET_CURRENT_ARG_TYPE(walk_state->
203                                                               arg_types), &arg);
204                 if (ACPI_FAILURE(status)) {
205                         return_ACPI_STATUS(status);
206                 }
207
208                 acpi_ps_append_arg(unnamed_op, arg);
209                 INCREMENT_ARG_LIST(walk_state->arg_types);
210         }
211
212         /*
213          * Make sure that we found a NAME and didn't run out of arguments
214          */
215         if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
216                 return_ACPI_STATUS(AE_AML_NO_OPERAND);
217         }
218
219         /* We know that this arg is a name, move to next arg */
220
221         INCREMENT_ARG_LIST(walk_state->arg_types);
222
223         /*
224          * Find the object. This will either insert the object into
225          * the namespace or simply look it up
226          */
227         walk_state->op = NULL;
228
229         status = walk_state->descending_callback(walk_state, op);
230         if (ACPI_FAILURE(status)) {
231                 ACPI_EXCEPTION((AE_INFO, status, "During name lookup/catalog"));
232                 return_ACPI_STATUS(status);
233         }
234
235         if (!*op) {
236                 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
237         }
238
239         status = acpi_ps_next_parse_state(walk_state, *op, status);
240         if (ACPI_FAILURE(status)) {
241                 if (status == AE_CTRL_PENDING) {
242                         return_ACPI_STATUS(AE_CTRL_PARSE_PENDING);
243                 }
244                 return_ACPI_STATUS(status);
245         }
246
247         acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
248         acpi_gbl_depth++;
249
250         if ((*op)->common.aml_opcode == AML_REGION_OP ||
251             (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
252                 /*
253                  * Defer final parsing of an operation_region body, because we don't
254                  * have enough info in the first pass to parse it correctly (i.e.,
255                  * there may be method calls within the term_arg elements of the body.)
256                  *
257                  * However, we must continue parsing because the opregion is not a
258                  * standalone package -- we don't know where the end is at this point.
259                  *
260                  * (Length is unknown until parse of the body complete)
261                  */
262                 (*op)->named.data = aml_op_start;
263                 (*op)->named.length = 0;
264         }
265
266         return_ACPI_STATUS(AE_OK);
267 }
268
269 /*******************************************************************************
270  *
271  * FUNCTION:    acpi_ps_create_op
272  *
273  * PARAMETERS:  walk_state          - Current state
274  *              aml_op_start        - Op start in AML
275  *              new_op              - Returned Op
276  *
277  * RETURN:      Status
278  *
279  * DESCRIPTION: Get Op from AML
280  *
281  ******************************************************************************/
282
283 static acpi_status
284 acpi_ps_create_op(struct acpi_walk_state *walk_state,
285                   u8 * aml_op_start, union acpi_parse_object **new_op)
286 {
287         acpi_status status = AE_OK;
288         union acpi_parse_object *op;
289         union acpi_parse_object *named_op = NULL;
290         union acpi_parse_object *parent_scope;
291         u8 argument_count;
292         const struct acpi_opcode_info *op_info;
293
294         ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
295
296         status = acpi_ps_get_aml_opcode(walk_state);
297         if (status == AE_CTRL_PARSE_CONTINUE) {
298                 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
299         }
300
301         /* Create Op structure and append to parent's argument list */
302
303         walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
304         op = acpi_ps_alloc_op(walk_state->opcode);
305         if (!op) {
306                 return_ACPI_STATUS(AE_NO_MEMORY);
307         }
308
309         if (walk_state->op_info->flags & AML_NAMED) {
310                 status =
311                     acpi_ps_build_named_op(walk_state, aml_op_start, op,
312                                            &named_op);
313                 acpi_ps_free_op(op);
314                 if (ACPI_FAILURE(status)) {
315                         return_ACPI_STATUS(status);
316                 }
317
318                 *new_op = named_op;
319                 return_ACPI_STATUS(AE_OK);
320         }
321
322         /* Not a named opcode, just allocate Op and append to parent */
323
324         if (walk_state->op_info->flags & AML_CREATE) {
325                 /*
326                  * Backup to beginning of create_xXXfield declaration
327                  * body_length is unknown until we parse the body
328                  */
329                 op->named.data = aml_op_start;
330                 op->named.length = 0;
331         }
332
333         if (walk_state->opcode == AML_BANK_FIELD_OP) {
334                 /*
335                  * Backup to beginning of bank_field declaration
336                  * body_length is unknown until we parse the body
337                  */
338                 op->named.data = aml_op_start;
339                 op->named.length = 0;
340         }
341
342         parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
343         acpi_ps_append_arg(parent_scope, op);
344
345         if (parent_scope) {
346                 op_info =
347                     acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
348                 if (op_info->flags & AML_HAS_TARGET) {
349                         argument_count =
350                             acpi_ps_get_argument_count(op_info->type);
351                         if (parent_scope->common.arg_list_length >
352                             argument_count) {
353                                 op->common.flags |= ACPI_PARSEOP_TARGET;
354                         }
355                 } else if (parent_scope->common.aml_opcode == AML_INCREMENT_OP) {
356                         op->common.flags |= ACPI_PARSEOP_TARGET;
357                 }
358         }
359
360         if (walk_state->descending_callback != NULL) {
361                 /*
362                  * Find the object. This will either insert the object into
363                  * the namespace or simply look it up
364                  */
365                 walk_state->op = *new_op = op;
366
367                 status = walk_state->descending_callback(walk_state, &op);
368                 status = acpi_ps_next_parse_state(walk_state, op, status);
369                 if (status == AE_CTRL_PENDING) {
370                         status = AE_CTRL_PARSE_PENDING;
371                 }
372         }
373
374         return_ACPI_STATUS(status);
375 }
376
377 /*******************************************************************************
378  *
379  * FUNCTION:    acpi_ps_get_arguments
380  *
381  * PARAMETERS:  walk_state          - Current state
382  *              aml_op_start        - Op start in AML
383  *              Op                  - Current Op
384  *
385  * RETURN:      Status
386  *
387  * DESCRIPTION: Get arguments for passed Op.
388  *
389  ******************************************************************************/
390
391 static acpi_status
392 acpi_ps_get_arguments(struct acpi_walk_state *walk_state,
393                       u8 * aml_op_start, union acpi_parse_object *op)
394 {
395         acpi_status status = AE_OK;
396         union acpi_parse_object *arg = NULL;
397         const struct acpi_opcode_info *op_info;
398
399         ACPI_FUNCTION_TRACE_PTR(ps_get_arguments, walk_state);
400
401         switch (op->common.aml_opcode) {
402         case AML_BYTE_OP:       /* AML_BYTEDATA_ARG */
403         case AML_WORD_OP:       /* AML_WORDDATA_ARG */
404         case AML_DWORD_OP:      /* AML_DWORDATA_ARG */
405         case AML_QWORD_OP:      /* AML_QWORDATA_ARG */
406         case AML_STRING_OP:     /* AML_ASCIICHARLIST_ARG */
407
408                 /* Fill in constant or string argument directly */
409
410                 acpi_ps_get_next_simple_arg(&(walk_state->parser_state),
411                                             GET_CURRENT_ARG_TYPE(walk_state->
412                                                                  arg_types),
413                                             op);
414                 break;
415
416         case AML_INT_NAMEPATH_OP:       /* AML_NAMESTRING_ARG */
417
418                 status =
419                     acpi_ps_get_next_namepath(walk_state,
420                                               &(walk_state->parser_state), op,
421                                               1);
422                 if (ACPI_FAILURE(status)) {
423                         return_ACPI_STATUS(status);
424                 }
425
426                 walk_state->arg_types = 0;
427                 break;
428
429         default:
430                 /*
431                  * Op is not a constant or string, append each argument to the Op
432                  */
433                 while (GET_CURRENT_ARG_TYPE(walk_state->arg_types)
434                        && !walk_state->arg_count) {
435                         walk_state->aml_offset =
436                             (u32) ACPI_PTR_DIFF(walk_state->parser_state.aml,
437                                                 walk_state->parser_state.
438                                                 aml_start);
439
440                         status =
441                             acpi_ps_get_next_arg(walk_state,
442                                                  &(walk_state->parser_state),
443                                                  GET_CURRENT_ARG_TYPE
444                                                  (walk_state->arg_types), &arg);
445                         if (ACPI_FAILURE(status)) {
446                                 return_ACPI_STATUS(status);
447                         }
448
449                         if (arg) {
450                                 arg->common.aml_offset = walk_state->aml_offset;
451                                 acpi_ps_append_arg(op, arg);
452                         }
453
454                         INCREMENT_ARG_LIST(walk_state->arg_types);
455                 }
456
457                 /*
458                  * Handle executable code at "module-level". This refers to
459                  * executable opcodes that appear outside of any control method.
460                  */
461                 if ((walk_state->pass_number <= ACPI_IMODE_LOAD_PASS2) &&
462                     ((walk_state->parse_flags & ACPI_PARSE_DISASSEMBLE) == 0)) {
463                         /*
464                          * We want to skip If/Else/While constructs during Pass1 because we
465                          * want to actually conditionally execute the code during Pass2.
466                          *
467                          * Except for disassembly, where we always want to walk the
468                          * If/Else/While packages
469                          */
470                         switch (op->common.aml_opcode) {
471                         case AML_IF_OP:
472                         case AML_ELSE_OP:
473                         case AML_WHILE_OP:
474
475                                 /*
476                                  * Currently supported module-level opcodes are:
477                                  * IF/ELSE/WHILE. These appear to be the most common,
478                                  * and easiest to support since they open an AML
479                                  * package.
480                                  */
481                                 if (walk_state->pass_number ==
482                                     ACPI_IMODE_LOAD_PASS1) {
483                                         acpi_ps_link_module_code(op->common.
484                                                                  parent,
485                                                                  aml_op_start,
486                                                                  (u32)
487                                                                  (walk_state->
488                                                                  parser_state.
489                                                                  pkg_end -
490                                                                  aml_op_start),
491                                                                  walk_state->
492                                                                  owner_id);
493                                 }
494
495                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
496                                                   "Pass1: Skipping an If/Else/While body\n"));
497
498                                 /* Skip body of if/else/while in pass 1 */
499
500                                 walk_state->parser_state.aml =
501                                     walk_state->parser_state.pkg_end;
502                                 walk_state->arg_count = 0;
503                                 break;
504
505                         default:
506                                 /*
507                                  * Check for an unsupported executable opcode at module
508                                  * level. We must be in PASS1, the parent must be a SCOPE,
509                                  * The opcode class must be EXECUTE, and the opcode must
510                                  * not be an argument to another opcode.
511                                  */
512                                 if ((walk_state->pass_number ==
513                                      ACPI_IMODE_LOAD_PASS1)
514                                     && (op->common.parent->common.aml_opcode ==
515                                         AML_SCOPE_OP)) {
516                                         op_info =
517                                             acpi_ps_get_opcode_info(op->common.
518                                                                     aml_opcode);
519                                         if ((op_info->class ==
520                                              AML_CLASS_EXECUTE) && (!arg)) {
521                                                 ACPI_WARNING((AE_INFO,
522                                                               "Detected an unsupported executable opcode "
523                                                               "at module-level: [0x%.4X] at table offset 0x%.4X",
524                                                               op->common.aml_opcode,
525                                                               (u32)((aml_op_start - walk_state->parser_state.aml_start)
526                                                                 + sizeof(struct acpi_table_header))));
527                                         }
528                                 }
529                                 break;
530                         }
531                 }
532
533                 /* Special processing for certain opcodes */
534
535                 switch (op->common.aml_opcode) {
536                 case AML_METHOD_OP:
537                         /*
538                          * Skip parsing of control method because we don't have enough
539                          * info in the first pass to parse it correctly.
540                          *
541                          * Save the length and address of the body
542                          */
543                         op->named.data = walk_state->parser_state.aml;
544                         op->named.length = (u32)
545                             (walk_state->parser_state.pkg_end -
546                              walk_state->parser_state.aml);
547
548                         /* Skip body of method */
549
550                         walk_state->parser_state.aml =
551                             walk_state->parser_state.pkg_end;
552                         walk_state->arg_count = 0;
553                         break;
554
555                 case AML_BUFFER_OP:
556                 case AML_PACKAGE_OP:
557                 case AML_VAR_PACKAGE_OP:
558
559                         if ((op->common.parent) &&
560                             (op->common.parent->common.aml_opcode ==
561                              AML_NAME_OP)
562                             && (walk_state->pass_number <=
563                                 ACPI_IMODE_LOAD_PASS2)) {
564                                 /*
565                                  * Skip parsing of Buffers and Packages because we don't have
566                                  * enough info in the first pass to parse them correctly.
567                                  */
568                                 op->named.data = aml_op_start;
569                                 op->named.length = (u32)
570                                     (walk_state->parser_state.pkg_end -
571                                      aml_op_start);
572
573                                 /* Skip body */
574
575                                 walk_state->parser_state.aml =
576                                     walk_state->parser_state.pkg_end;
577                                 walk_state->arg_count = 0;
578                         }
579                         break;
580
581                 case AML_WHILE_OP:
582
583                         if (walk_state->control_state) {
584                                 walk_state->control_state->control.package_end =
585                                     walk_state->parser_state.pkg_end;
586                         }
587                         break;
588
589                 default:
590
591                         /* No action for all other opcodes */
592                         break;
593                 }
594
595                 break;
596         }
597
598         return_ACPI_STATUS(AE_OK);
599 }
600
601 /*******************************************************************************
602  *
603  * FUNCTION:    acpi_ps_link_module_code
604  *
605  * PARAMETERS:  parent_op           - Parent parser op
606  *              aml_start           - Pointer to the AML
607  *              aml_length          - Length of executable AML
608  *              owner_id            - owner_id of module level code
609  *
610  * RETURN:      None.
611  *
612  * DESCRIPTION: Wrap the module-level code with a method object and link the
613  *              object to the global list. Note, the mutex field of the method
614  *              object is used to link multiple module-level code objects.
615  *
616  ******************************************************************************/
617
618 static void
619 acpi_ps_link_module_code(union acpi_parse_object *parent_op,
620                          u8 *aml_start, u32 aml_length, acpi_owner_id owner_id)
621 {
622         union acpi_operand_object *prev;
623         union acpi_operand_object *next;
624         union acpi_operand_object *method_obj;
625         struct acpi_namespace_node *parent_node;
626
627         /* Get the tail of the list */
628
629         prev = next = acpi_gbl_module_code_list;
630         while (next) {
631                 prev = next;
632                 next = next->method.mutex;
633         }
634
635         /*
636          * Insert the module level code into the list. Merge it if it is
637          * adjacent to the previous element.
638          */
639         if (!prev ||
640             ((prev->method.aml_start + prev->method.aml_length) != aml_start)) {
641
642                 /* Create, initialize, and link a new temporary method object */
643
644                 method_obj = acpi_ut_create_internal_object(ACPI_TYPE_METHOD);
645                 if (!method_obj) {
646                         return;
647                 }
648
649                 if (parent_op->common.node) {
650                         parent_node = parent_op->common.node;
651                 } else {
652                         parent_node = acpi_gbl_root_node;
653                 }
654
655                 method_obj->method.aml_start = aml_start;
656                 method_obj->method.aml_length = aml_length;
657                 method_obj->method.owner_id = owner_id;
658                 method_obj->method.flags |= AOPOBJ_MODULE_LEVEL;
659
660                 /*
661                  * Save the parent node in next_object. This is cheating, but we
662                  * don't want to expand the method object.
663                  */
664                 method_obj->method.next_object =
665                     ACPI_CAST_PTR(union acpi_operand_object, parent_node);
666
667                 if (!prev) {
668                         acpi_gbl_module_code_list = method_obj;
669                 } else {
670                         prev->method.mutex = method_obj;
671                 }
672         } else {
673                 prev->method.aml_length += aml_length;
674         }
675 }
676
677 /*******************************************************************************
678  *
679  * FUNCTION:    acpi_ps_complete_op
680  *
681  * PARAMETERS:  walk_state          - Current state
682  *              Op                  - Returned Op
683  *              Status              - Parse status before complete Op
684  *
685  * RETURN:      Status
686  *
687  * DESCRIPTION: Complete Op
688  *
689  ******************************************************************************/
690
691 static acpi_status
692 acpi_ps_complete_op(struct acpi_walk_state *walk_state,
693                     union acpi_parse_object **op, acpi_status status)
694 {
695         acpi_status status2;
696
697         ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
698
699         /*
700          * Finished one argument of the containing scope
701          */
702         walk_state->parser_state.scope->parse_scope.arg_count--;
703
704         /* Close this Op (will result in parse subtree deletion) */
705
706         status2 = acpi_ps_complete_this_op(walk_state, *op);
707         if (ACPI_FAILURE(status2)) {
708                 return_ACPI_STATUS(status2);
709         }
710
711         *op = NULL;
712
713         switch (status) {
714         case AE_OK:
715                 break;
716
717         case AE_CTRL_TRANSFER:
718
719                 /* We are about to transfer to a called method */
720
721                 walk_state->prev_op = NULL;
722                 walk_state->prev_arg_types = walk_state->arg_types;
723                 return_ACPI_STATUS(status);
724
725         case AE_CTRL_END:
726
727                 acpi_ps_pop_scope(&(walk_state->parser_state), op,
728                                   &walk_state->arg_types,
729                                   &walk_state->arg_count);
730
731                 if (*op) {
732                         walk_state->op = *op;
733                         walk_state->op_info =
734                             acpi_ps_get_opcode_info((*op)->common.aml_opcode);
735                         walk_state->opcode = (*op)->common.aml_opcode;
736
737                         status = walk_state->ascending_callback(walk_state);
738                         status =
739                             acpi_ps_next_parse_state(walk_state, *op, status);
740
741                         status2 = acpi_ps_complete_this_op(walk_state, *op);
742                         if (ACPI_FAILURE(status2)) {
743                                 return_ACPI_STATUS(status2);
744                         }
745                 }
746
747                 status = AE_OK;
748                 break;
749
750         case AE_CTRL_BREAK:
751         case AE_CTRL_CONTINUE:
752
753                 /* Pop off scopes until we find the While */
754
755                 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
756                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
757                                           &walk_state->arg_types,
758                                           &walk_state->arg_count);
759                 }
760
761                 /* Close this iteration of the While loop */
762
763                 walk_state->op = *op;
764                 walk_state->op_info =
765                     acpi_ps_get_opcode_info((*op)->common.aml_opcode);
766                 walk_state->opcode = (*op)->common.aml_opcode;
767
768                 status = walk_state->ascending_callback(walk_state);
769                 status = acpi_ps_next_parse_state(walk_state, *op, status);
770
771                 status2 = acpi_ps_complete_this_op(walk_state, *op);
772                 if (ACPI_FAILURE(status2)) {
773                         return_ACPI_STATUS(status2);
774                 }
775
776                 status = AE_OK;
777                 break;
778
779         case AE_CTRL_TERMINATE:
780
781                 /* Clean up */
782                 do {
783                         if (*op) {
784                                 status2 =
785                                     acpi_ps_complete_this_op(walk_state, *op);
786                                 if (ACPI_FAILURE(status2)) {
787                                         return_ACPI_STATUS(status2);
788                                 }
789
790                                 acpi_ut_delete_generic_state
791                                     (acpi_ut_pop_generic_state
792                                      (&walk_state->control_state));
793                         }
794
795                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
796                                           &walk_state->arg_types,
797                                           &walk_state->arg_count);
798
799                 } while (*op);
800
801                 return_ACPI_STATUS(AE_OK);
802
803         default:                /* All other non-AE_OK status */
804
805                 do {
806                         if (*op) {
807                                 status2 =
808                                     acpi_ps_complete_this_op(walk_state, *op);
809                                 if (ACPI_FAILURE(status2)) {
810                                         return_ACPI_STATUS(status2);
811                                 }
812                         }
813
814                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
815                                           &walk_state->arg_types,
816                                           &walk_state->arg_count);
817
818                 } while (*op);
819
820 #if 0
821                 /*
822                  * TBD: Cleanup parse ops on error
823                  */
824                 if (*op == NULL) {
825                         acpi_ps_pop_scope(parser_state, op,
826                                           &walk_state->arg_types,
827                                           &walk_state->arg_count);
828                 }
829 #endif
830                 walk_state->prev_op = NULL;
831                 walk_state->prev_arg_types = walk_state->arg_types;
832                 return_ACPI_STATUS(status);
833         }
834
835         /* This scope complete? */
836
837         if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
838                 acpi_ps_pop_scope(&(walk_state->parser_state), op,
839                                   &walk_state->arg_types,
840                                   &walk_state->arg_count);
841                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
842         } else {
843                 *op = NULL;
844         }
845
846         ACPI_PREEMPTION_POINT();
847
848         return_ACPI_STATUS(AE_OK);
849 }
850
851 /*******************************************************************************
852  *
853  * FUNCTION:    acpi_ps_complete_final_op
854  *
855  * PARAMETERS:  walk_state          - Current state
856  *              Op                  - Current Op
857  *              Status              - Current parse status before complete last
858  *                                    Op
859  *
860  * RETURN:      Status
861  *
862  * DESCRIPTION: Complete last Op.
863  *
864  ******************************************************************************/
865
866 static acpi_status
867 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
868                           union acpi_parse_object *op, acpi_status status)
869 {
870         acpi_status status2;
871
872         ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
873
874         /*
875          * Complete the last Op (if not completed), and clear the scope stack.
876          * It is easily possible to end an AML "package" with an unbounded number
877          * of open scopes (such as when several ASL blocks are closed with
878          * sequential closing braces). We want to terminate each one cleanly.
879          */
880         ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
881                           op));
882         do {
883                 if (op) {
884                         if (walk_state->ascending_callback != NULL) {
885                                 walk_state->op = op;
886                                 walk_state->op_info =
887                                     acpi_ps_get_opcode_info(op->common.
888                                                             aml_opcode);
889                                 walk_state->opcode = op->common.aml_opcode;
890
891                                 status =
892                                     walk_state->ascending_callback(walk_state);
893                                 status =
894                                     acpi_ps_next_parse_state(walk_state, op,
895                                                              status);
896                                 if (status == AE_CTRL_PENDING) {
897                                         status =
898                                             acpi_ps_complete_op(walk_state, &op,
899                                                                 AE_OK);
900                                         if (ACPI_FAILURE(status)) {
901                                                 return_ACPI_STATUS(status);
902                                         }
903                                 }
904
905                                 if (status == AE_CTRL_TERMINATE) {
906                                         status = AE_OK;
907
908                                         /* Clean up */
909                                         do {
910                                                 if (op) {
911                                                         status2 =
912                                                             acpi_ps_complete_this_op
913                                                             (walk_state, op);
914                                                         if (ACPI_FAILURE
915                                                             (status2)) {
916                                                                 return_ACPI_STATUS
917                                                                     (status2);
918                                                         }
919                                                 }
920
921                                                 acpi_ps_pop_scope(&
922                                                                   (walk_state->
923                                                                    parser_state),
924                                                                   &op,
925                                                                   &walk_state->
926                                                                   arg_types,
927                                                                   &walk_state->
928                                                                   arg_count);
929
930                                         } while (op);
931
932                                         return_ACPI_STATUS(status);
933                                 }
934
935                                 else if (ACPI_FAILURE(status)) {
936
937                                         /* First error is most important */
938
939                                         (void)
940                                             acpi_ps_complete_this_op(walk_state,
941                                                                      op);
942                                         return_ACPI_STATUS(status);
943                                 }
944                         }
945
946                         status2 = acpi_ps_complete_this_op(walk_state, op);
947                         if (ACPI_FAILURE(status2)) {
948                                 return_ACPI_STATUS(status2);
949                         }
950                 }
951
952                 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
953                                   &walk_state->arg_types,
954                                   &walk_state->arg_count);
955
956         } while (op);
957
958         return_ACPI_STATUS(status);
959 }
960
961 /*******************************************************************************
962  *
963  * FUNCTION:    acpi_ps_parse_loop
964  *
965  * PARAMETERS:  walk_state          - Current state
966  *
967  * RETURN:      Status
968  *
969  * DESCRIPTION: Parse AML (pointed to by the current parser state) and return
970  *              a tree of ops.
971  *
972  ******************************************************************************/
973
974 acpi_status acpi_ps_parse_loop(struct acpi_walk_state *walk_state)
975 {
976         acpi_status status = AE_OK;
977         union acpi_parse_object *op = NULL;     /* current op */
978         struct acpi_parse_state *parser_state;
979         u8 *aml_op_start = NULL;
980
981         ACPI_FUNCTION_TRACE_PTR(ps_parse_loop, walk_state);
982
983         if (walk_state->descending_callback == NULL) {
984                 return_ACPI_STATUS(AE_BAD_PARAMETER);
985         }
986
987         parser_state = &walk_state->parser_state;
988         walk_state->arg_types = 0;
989
990 #if (!defined (ACPI_NO_METHOD_EXECUTION) && !defined (ACPI_CONSTANT_EVAL_ONLY))
991
992         if (walk_state->walk_type & ACPI_WALK_METHOD_RESTART) {
993
994                 /* We are restarting a preempted control method */
995
996                 if (acpi_ps_has_completed_scope(parser_state)) {
997                         /*
998                          * We must check if a predicate to an IF or WHILE statement
999                          * was just completed
1000                          */
1001                         if ((parser_state->scope->parse_scope.op) &&
1002                             ((parser_state->scope->parse_scope.op->common.
1003                               aml_opcode == AML_IF_OP)
1004                              || (parser_state->scope->parse_scope.op->common.
1005                                  aml_opcode == AML_WHILE_OP))
1006                             && (walk_state->control_state)
1007                             && (walk_state->control_state->common.state ==
1008                                 ACPI_CONTROL_PREDICATE_EXECUTING)) {
1009                                 /*
1010                                  * A predicate was just completed, get the value of the
1011                                  * predicate and branch based on that value
1012                                  */
1013                                 walk_state->op = NULL;
1014                                 status =
1015                                     acpi_ds_get_predicate_value(walk_state,
1016                                                                 ACPI_TO_POINTER
1017                                                                 (TRUE));
1018                                 if (ACPI_FAILURE(status)
1019                                     && ((status & AE_CODE_MASK) !=
1020                                         AE_CODE_CONTROL)) {
1021                                         if (status == AE_AML_NO_RETURN_VALUE) {
1022                                                 ACPI_EXCEPTION((AE_INFO, status,
1023                                                                 "Invoked method did not return a value"));
1024
1025                                         }
1026
1027                                         ACPI_EXCEPTION((AE_INFO, status,
1028                                                         "GetPredicate Failed"));
1029                                         return_ACPI_STATUS(status);
1030                                 }
1031
1032                                 status =
1033                                     acpi_ps_next_parse_state(walk_state, op,
1034                                                              status);
1035                         }
1036
1037                         acpi_ps_pop_scope(parser_state, &op,
1038                                           &walk_state->arg_types,
1039                                           &walk_state->arg_count);
1040                         ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
1041                                           "Popped scope, Op=%p\n", op));
1042                 } else if (walk_state->prev_op) {
1043
1044                         /* We were in the middle of an op */
1045
1046                         op = walk_state->prev_op;
1047                         walk_state->arg_types = walk_state->prev_arg_types;
1048                 }
1049         }
1050 #endif
1051
1052         /* Iterative parsing loop, while there is more AML to process: */
1053
1054         while ((parser_state->aml < parser_state->aml_end) || (op)) {
1055                 aml_op_start = parser_state->aml;
1056                 if (!op) {
1057                         status =
1058                             acpi_ps_create_op(walk_state, aml_op_start, &op);
1059                         if (ACPI_FAILURE(status)) {
1060                                 if (status == AE_CTRL_PARSE_CONTINUE) {
1061                                         continue;
1062                                 }
1063
1064                                 if (status == AE_CTRL_PARSE_PENDING) {
1065                                         status = AE_OK;
1066                                 }
1067
1068                                 status =
1069                                     acpi_ps_complete_op(walk_state, &op,
1070                                                         status);
1071                                 if (ACPI_FAILURE(status)) {
1072                                         return_ACPI_STATUS(status);
1073                                 }
1074
1075                                 continue;
1076                         }
1077
1078                         op->common.aml_offset = walk_state->aml_offset;
1079
1080                         if (walk_state->op_info) {
1081                                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE,
1082                                                   "Opcode %4.4X [%s] Op %p Aml %p AmlOffset %5.5X\n",
1083                                                   (u32) op->common.aml_opcode,
1084                                                   walk_state->op_info->name, op,
1085                                                   parser_state->aml,
1086                                                   op->common.aml_offset));
1087                         }
1088                 }
1089
1090                 /*
1091                  * Start arg_count at zero because we don't know if there are
1092                  * any args yet
1093                  */
1094                 walk_state->arg_count = 0;
1095
1096                 /* Are there any arguments that must be processed? */
1097
1098                 if (walk_state->arg_types) {
1099
1100                         /* Get arguments */
1101
1102                         status =
1103                             acpi_ps_get_arguments(walk_state, aml_op_start, op);
1104                         if (ACPI_FAILURE(status)) {
1105                                 status =
1106                                     acpi_ps_complete_op(walk_state, &op,
1107                                                         status);
1108                                 if (ACPI_FAILURE(status)) {
1109                                         return_ACPI_STATUS(status);
1110                                 }
1111
1112                                 continue;
1113                         }
1114                 }
1115
1116                 /* Check for arguments that need to be processed */
1117
1118                 if (walk_state->arg_count) {
1119                         /*
1120                          * There are arguments (complex ones), push Op and
1121                          * prepare for argument
1122                          */
1123                         status = acpi_ps_push_scope(parser_state, op,
1124                                                     walk_state->arg_types,
1125                                                     walk_state->arg_count);
1126                         if (ACPI_FAILURE(status)) {
1127                                 status =
1128                                     acpi_ps_complete_op(walk_state, &op,
1129                                                         status);
1130                                 if (ACPI_FAILURE(status)) {
1131                                         return_ACPI_STATUS(status);
1132                                 }
1133
1134                                 continue;
1135                         }
1136
1137                         op = NULL;
1138                         continue;
1139                 }
1140
1141                 /*
1142                  * All arguments have been processed -- Op is complete,
1143                  * prepare for next
1144                  */
1145                 walk_state->op_info =
1146                     acpi_ps_get_opcode_info(op->common.aml_opcode);
1147                 if (walk_state->op_info->flags & AML_NAMED) {
1148                         if (acpi_gbl_depth) {
1149                                 acpi_gbl_depth--;
1150                         }
1151
1152                         if (op->common.aml_opcode == AML_REGION_OP ||
1153                             op->common.aml_opcode == AML_DATA_REGION_OP) {
1154                                 /*
1155                                  * Skip parsing of control method or opregion body,
1156                                  * because we don't have enough info in the first pass
1157                                  * to parse them correctly.
1158                                  *
1159                                  * Completed parsing an op_region declaration, we now
1160                                  * know the length.
1161                                  */
1162                                 op->named.length =
1163                                     (u32) (parser_state->aml - op->named.data);
1164                         }
1165                 }
1166
1167                 if (walk_state->op_info->flags & AML_CREATE) {
1168                         /*
1169                          * Backup to beginning of create_xXXfield declaration (1 for
1170                          * Opcode)
1171                          *
1172                          * body_length is unknown until we parse the body
1173                          */
1174                         op->named.length =
1175                             (u32) (parser_state->aml - op->named.data);
1176                 }
1177
1178                 if (op->common.aml_opcode == AML_BANK_FIELD_OP) {
1179                         /*
1180                          * Backup to beginning of bank_field declaration
1181                          *
1182                          * body_length is unknown until we parse the body
1183                          */
1184                         op->named.length =
1185                             (u32) (parser_state->aml - op->named.data);
1186                 }
1187
1188                 /* This op complete, notify the dispatcher */
1189
1190                 if (walk_state->ascending_callback != NULL) {
1191                         walk_state->op = op;
1192                         walk_state->opcode = op->common.aml_opcode;
1193
1194                         status = walk_state->ascending_callback(walk_state);
1195                         status =
1196                             acpi_ps_next_parse_state(walk_state, op, status);
1197                         if (status == AE_CTRL_PENDING) {
1198                                 status = AE_OK;
1199                         }
1200                 }
1201
1202                 status = acpi_ps_complete_op(walk_state, &op, status);
1203                 if (ACPI_FAILURE(status)) {
1204                         return_ACPI_STATUS(status);
1205                 }
1206
1207         }                       /* while parser_state->Aml */
1208
1209         status = acpi_ps_complete_final_op(walk_state, op, status);
1210         return_ACPI_STATUS(status);
1211 }