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