]> rtime.felk.cvut.cz Git - linux-imx.git/blob - drivers/acpi/acpica/psobject.c
PM / QoS: Add pm_qos and dev_pm_qos to events-power.txt
[linux-imx.git] / drivers / acpi / acpica / psobject.c
1 /******************************************************************************
2  *
3  * Module Name: psobject - Support for parse objects
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2013, 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 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acparser.h"
47 #include "amlcode.h"
48
49 #define _COMPONENT          ACPI_PARSER
50 ACPI_MODULE_NAME("psobject")
51
52 /* Local prototypes */
53 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_ps_get_aml_opcode
58  *
59  * PARAMETERS:  walk_state          - Current state
60  *
61  * RETURN:      Status
62  *
63  * DESCRIPTION: Extract the next AML opcode from the input stream.
64  *
65  ******************************************************************************/
66
67 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
68 {
69
70         ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
71
72         walk_state->aml_offset =
73             (u32)ACPI_PTR_DIFF(walk_state->parser_state.aml,
74                                walk_state->parser_state.aml_start);
75         walk_state->opcode = acpi_ps_peek_opcode(&(walk_state->parser_state));
76
77         /*
78          * First cut to determine what we have found:
79          * 1) A valid AML opcode
80          * 2) A name string
81          * 3) An unknown/invalid opcode
82          */
83         walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
84
85         switch (walk_state->op_info->class) {
86         case AML_CLASS_ASCII:
87         case AML_CLASS_PREFIX:
88                 /*
89                  * Starts with a valid prefix or ASCII char, this is a name
90                  * string. Convert the bare name string to a namepath.
91                  */
92                 walk_state->opcode = AML_INT_NAMEPATH_OP;
93                 walk_state->arg_types = ARGP_NAMESTRING;
94                 break;
95
96         case AML_CLASS_UNKNOWN:
97
98                 /* The opcode is unrecognized. Complain and skip unknown opcodes */
99
100                 if (walk_state->pass_number == 2) {
101                         ACPI_ERROR((AE_INFO,
102                                     "Unknown opcode 0x%.2X at table offset 0x%.4X, ignoring",
103                                     walk_state->opcode,
104                                     (u32)(walk_state->aml_offset +
105                                           sizeof(struct acpi_table_header))));
106
107                         ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16),
108                                          48);
109
110 #ifdef ACPI_ASL_COMPILER
111                         /*
112                          * This is executed for the disassembler only. Output goes
113                          * to the disassembled ASL output file.
114                          */
115                         acpi_os_printf
116                             ("/*\nError: Unknown opcode 0x%.2X at table offset 0x%.4X, context:\n",
117                              walk_state->opcode,
118                              (u32)(walk_state->aml_offset +
119                                    sizeof(struct acpi_table_header)));
120
121                         /* Dump the context surrounding the invalid opcode */
122
123                         acpi_ut_dump_buffer(((u8 *)walk_state->parser_state.
124                                              aml - 16), 48, DB_BYTE_DISPLAY,
125                                             (walk_state->aml_offset +
126                                              sizeof(struct acpi_table_header) -
127                                              16));
128                         acpi_os_printf(" */\n");
129 #endif
130                 }
131
132                 /* Increment past one-byte or two-byte opcode */
133
134                 walk_state->parser_state.aml++;
135                 if (walk_state->opcode > 0xFF) {        /* Can only happen if first byte is 0x5B */
136                         walk_state->parser_state.aml++;
137                 }
138
139                 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
140
141         default:
142
143                 /* Found opcode info, this is a normal opcode */
144
145                 walk_state->parser_state.aml +=
146                     acpi_ps_get_opcode_size(walk_state->opcode);
147                 walk_state->arg_types = walk_state->op_info->parse_args;
148                 break;
149         }
150
151         return_ACPI_STATUS(AE_OK);
152 }
153
154 /*******************************************************************************
155  *
156  * FUNCTION:    acpi_ps_build_named_op
157  *
158  * PARAMETERS:  walk_state          - Current state
159  *              aml_op_start        - Begin of named Op in AML
160  *              unnamed_op          - Early Op (not a named Op)
161  *              op                  - Returned Op
162  *
163  * RETURN:      Status
164  *
165  * DESCRIPTION: Parse a named Op
166  *
167  ******************************************************************************/
168
169 acpi_status
170 acpi_ps_build_named_op(struct acpi_walk_state *walk_state,
171                        u8 *aml_op_start,
172                        union acpi_parse_object *unnamed_op,
173                        union acpi_parse_object **op)
174 {
175         acpi_status status = AE_OK;
176         union acpi_parse_object *arg = NULL;
177
178         ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
179
180         unnamed_op->common.value.arg = NULL;
181         unnamed_op->common.arg_list_length = 0;
182         unnamed_op->common.aml_opcode = walk_state->opcode;
183
184         /*
185          * Get and append arguments until we find the node that contains
186          * the name (the type ARGP_NAME).
187          */
188         while (GET_CURRENT_ARG_TYPE(walk_state->arg_types) &&
189                (GET_CURRENT_ARG_TYPE(walk_state->arg_types) != ARGP_NAME)) {
190                 status =
191                     acpi_ps_get_next_arg(walk_state,
192                                          &(walk_state->parser_state),
193                                          GET_CURRENT_ARG_TYPE(walk_state->
194                                                               arg_types), &arg);
195                 if (ACPI_FAILURE(status)) {
196                         return_ACPI_STATUS(status);
197                 }
198
199                 acpi_ps_append_arg(unnamed_op, arg);
200                 INCREMENT_ARG_LIST(walk_state->arg_types);
201         }
202
203         /*
204          * Make sure that we found a NAME and didn't run out of arguments
205          */
206         if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
207                 return_ACPI_STATUS(AE_AML_NO_OPERAND);
208         }
209
210         /* We know that this arg is a name, move to next arg */
211
212         INCREMENT_ARG_LIST(walk_state->arg_types);
213
214         /*
215          * Find the object. This will either insert the object into
216          * the namespace or simply look it up
217          */
218         walk_state->op = NULL;
219
220         status = walk_state->descending_callback(walk_state, op);
221         if (ACPI_FAILURE(status)) {
222                 ACPI_EXCEPTION((AE_INFO, status, "During name lookup/catalog"));
223                 return_ACPI_STATUS(status);
224         }
225
226         if (!*op) {
227                 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
228         }
229
230         status = acpi_ps_next_parse_state(walk_state, *op, status);
231         if (ACPI_FAILURE(status)) {
232                 if (status == AE_CTRL_PENDING) {
233                         return_ACPI_STATUS(AE_CTRL_PARSE_PENDING);
234                 }
235                 return_ACPI_STATUS(status);
236         }
237
238         acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
239
240         if ((*op)->common.aml_opcode == AML_REGION_OP ||
241             (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
242                 /*
243                  * Defer final parsing of an operation_region body, because we don't
244                  * have enough info in the first pass to parse it correctly (i.e.,
245                  * there may be method calls within the term_arg elements of the body.)
246                  *
247                  * However, we must continue parsing because the opregion is not a
248                  * standalone package -- we don't know where the end is at this point.
249                  *
250                  * (Length is unknown until parse of the body complete)
251                  */
252                 (*op)->named.data = aml_op_start;
253                 (*op)->named.length = 0;
254         }
255
256         return_ACPI_STATUS(AE_OK);
257 }
258
259 /*******************************************************************************
260  *
261  * FUNCTION:    acpi_ps_create_op
262  *
263  * PARAMETERS:  walk_state          - Current state
264  *              aml_op_start        - Op start in AML
265  *              new_op              - Returned Op
266  *
267  * RETURN:      Status
268  *
269  * DESCRIPTION: Get Op from AML
270  *
271  ******************************************************************************/
272
273 acpi_status
274 acpi_ps_create_op(struct acpi_walk_state *walk_state,
275                   u8 *aml_op_start, union acpi_parse_object **new_op)
276 {
277         acpi_status status = AE_OK;
278         union acpi_parse_object *op;
279         union acpi_parse_object *named_op = NULL;
280         union acpi_parse_object *parent_scope;
281         u8 argument_count;
282         const struct acpi_opcode_info *op_info;
283
284         ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
285
286         status = acpi_ps_get_aml_opcode(walk_state);
287         if (status == AE_CTRL_PARSE_CONTINUE) {
288                 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
289         }
290
291         /* Create Op structure and append to parent's argument list */
292
293         walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
294         op = acpi_ps_alloc_op(walk_state->opcode);
295         if (!op) {
296                 return_ACPI_STATUS(AE_NO_MEMORY);
297         }
298
299         if (walk_state->op_info->flags & AML_NAMED) {
300                 status =
301                     acpi_ps_build_named_op(walk_state, aml_op_start, op,
302                                            &named_op);
303                 acpi_ps_free_op(op);
304                 if (ACPI_FAILURE(status)) {
305                         return_ACPI_STATUS(status);
306                 }
307
308                 *new_op = named_op;
309                 return_ACPI_STATUS(AE_OK);
310         }
311
312         /* Not a named opcode, just allocate Op and append to parent */
313
314         if (walk_state->op_info->flags & AML_CREATE) {
315                 /*
316                  * Backup to beginning of create_XXXfield declaration
317                  * body_length is unknown until we parse the body
318                  */
319                 op->named.data = aml_op_start;
320                 op->named.length = 0;
321         }
322
323         if (walk_state->opcode == AML_BANK_FIELD_OP) {
324                 /*
325                  * Backup to beginning of bank_field declaration
326                  * body_length is unknown until we parse the body
327                  */
328                 op->named.data = aml_op_start;
329                 op->named.length = 0;
330         }
331
332         parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
333         acpi_ps_append_arg(parent_scope, op);
334
335         if (parent_scope) {
336                 op_info =
337                     acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
338                 if (op_info->flags & AML_HAS_TARGET) {
339                         argument_count =
340                             acpi_ps_get_argument_count(op_info->type);
341                         if (parent_scope->common.arg_list_length >
342                             argument_count) {
343                                 op->common.flags |= ACPI_PARSEOP_TARGET;
344                         }
345                 } else if (parent_scope->common.aml_opcode == AML_INCREMENT_OP) {
346                         op->common.flags |= ACPI_PARSEOP_TARGET;
347                 }
348         }
349
350         if (walk_state->descending_callback != NULL) {
351                 /*
352                  * Find the object. This will either insert the object into
353                  * the namespace or simply look it up
354                  */
355                 walk_state->op = *new_op = op;
356
357                 status = walk_state->descending_callback(walk_state, &op);
358                 status = acpi_ps_next_parse_state(walk_state, op, status);
359                 if (status == AE_CTRL_PENDING) {
360                         status = AE_CTRL_PARSE_PENDING;
361                 }
362         }
363
364         return_ACPI_STATUS(status);
365 }
366
367 /*******************************************************************************
368  *
369  * FUNCTION:    acpi_ps_complete_op
370  *
371  * PARAMETERS:  walk_state          - Current state
372  *              op                  - Returned Op
373  *              status              - Parse status before complete Op
374  *
375  * RETURN:      Status
376  *
377  * DESCRIPTION: Complete Op
378  *
379  ******************************************************************************/
380
381 acpi_status
382 acpi_ps_complete_op(struct acpi_walk_state *walk_state,
383                     union acpi_parse_object **op, acpi_status status)
384 {
385         acpi_status status2;
386
387         ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
388
389         /*
390          * Finished one argument of the containing scope
391          */
392         walk_state->parser_state.scope->parse_scope.arg_count--;
393
394         /* Close this Op (will result in parse subtree deletion) */
395
396         status2 = acpi_ps_complete_this_op(walk_state, *op);
397         if (ACPI_FAILURE(status2)) {
398                 return_ACPI_STATUS(status2);
399         }
400
401         *op = NULL;
402
403         switch (status) {
404         case AE_OK:
405                 break;
406
407         case AE_CTRL_TRANSFER:
408
409                 /* We are about to transfer to a called method */
410
411                 walk_state->prev_op = NULL;
412                 walk_state->prev_arg_types = walk_state->arg_types;
413                 return_ACPI_STATUS(status);
414
415         case AE_CTRL_END:
416
417                 acpi_ps_pop_scope(&(walk_state->parser_state), op,
418                                   &walk_state->arg_types,
419                                   &walk_state->arg_count);
420
421                 if (*op) {
422                         walk_state->op = *op;
423                         walk_state->op_info =
424                             acpi_ps_get_opcode_info((*op)->common.aml_opcode);
425                         walk_state->opcode = (*op)->common.aml_opcode;
426
427                         status = walk_state->ascending_callback(walk_state);
428                         status =
429                             acpi_ps_next_parse_state(walk_state, *op, status);
430
431                         status2 = acpi_ps_complete_this_op(walk_state, *op);
432                         if (ACPI_FAILURE(status2)) {
433                                 return_ACPI_STATUS(status2);
434                         }
435                 }
436
437                 status = AE_OK;
438                 break;
439
440         case AE_CTRL_BREAK:
441         case AE_CTRL_CONTINUE:
442
443                 /* Pop off scopes until we find the While */
444
445                 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
446                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
447                                           &walk_state->arg_types,
448                                           &walk_state->arg_count);
449                 }
450
451                 /* Close this iteration of the While loop */
452
453                 walk_state->op = *op;
454                 walk_state->op_info =
455                     acpi_ps_get_opcode_info((*op)->common.aml_opcode);
456                 walk_state->opcode = (*op)->common.aml_opcode;
457
458                 status = walk_state->ascending_callback(walk_state);
459                 status = acpi_ps_next_parse_state(walk_state, *op, status);
460
461                 status2 = acpi_ps_complete_this_op(walk_state, *op);
462                 if (ACPI_FAILURE(status2)) {
463                         return_ACPI_STATUS(status2);
464                 }
465
466                 status = AE_OK;
467                 break;
468
469         case AE_CTRL_TERMINATE:
470
471                 /* Clean up */
472                 do {
473                         if (*op) {
474                                 status2 =
475                                     acpi_ps_complete_this_op(walk_state, *op);
476                                 if (ACPI_FAILURE(status2)) {
477                                         return_ACPI_STATUS(status2);
478                                 }
479
480                                 acpi_ut_delete_generic_state
481                                     (acpi_ut_pop_generic_state
482                                      (&walk_state->control_state));
483                         }
484
485                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
486                                           &walk_state->arg_types,
487                                           &walk_state->arg_count);
488
489                 } while (*op);
490
491                 return_ACPI_STATUS(AE_OK);
492
493         default:                /* All other non-AE_OK status */
494
495                 do {
496                         if (*op) {
497                                 status2 =
498                                     acpi_ps_complete_this_op(walk_state, *op);
499                                 if (ACPI_FAILURE(status2)) {
500                                         return_ACPI_STATUS(status2);
501                                 }
502                         }
503
504                         acpi_ps_pop_scope(&(walk_state->parser_state), op,
505                                           &walk_state->arg_types,
506                                           &walk_state->arg_count);
507
508                 } while (*op);
509
510 #if 0
511                 /*
512                  * TBD: Cleanup parse ops on error
513                  */
514                 if (*op == NULL) {
515                         acpi_ps_pop_scope(parser_state, op,
516                                           &walk_state->arg_types,
517                                           &walk_state->arg_count);
518                 }
519 #endif
520                 walk_state->prev_op = NULL;
521                 walk_state->prev_arg_types = walk_state->arg_types;
522                 return_ACPI_STATUS(status);
523         }
524
525         /* This scope complete? */
526
527         if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
528                 acpi_ps_pop_scope(&(walk_state->parser_state), op,
529                                   &walk_state->arg_types,
530                                   &walk_state->arg_count);
531                 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
532         } else {
533                 *op = NULL;
534         }
535
536         return_ACPI_STATUS(AE_OK);
537 }
538
539 /*******************************************************************************
540  *
541  * FUNCTION:    acpi_ps_complete_final_op
542  *
543  * PARAMETERS:  walk_state          - Current state
544  *              op                  - Current Op
545  *              status              - Current parse status before complete last
546  *                                    Op
547  *
548  * RETURN:      Status
549  *
550  * DESCRIPTION: Complete last Op.
551  *
552  ******************************************************************************/
553
554 acpi_status
555 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
556                           union acpi_parse_object *op, acpi_status status)
557 {
558         acpi_status status2;
559
560         ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
561
562         /*
563          * Complete the last Op (if not completed), and clear the scope stack.
564          * It is easily possible to end an AML "package" with an unbounded number
565          * of open scopes (such as when several ASL blocks are closed with
566          * sequential closing braces). We want to terminate each one cleanly.
567          */
568         ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
569                           op));
570         do {
571                 if (op) {
572                         if (walk_state->ascending_callback != NULL) {
573                                 walk_state->op = op;
574                                 walk_state->op_info =
575                                     acpi_ps_get_opcode_info(op->common.
576                                                             aml_opcode);
577                                 walk_state->opcode = op->common.aml_opcode;
578
579                                 status =
580                                     walk_state->ascending_callback(walk_state);
581                                 status =
582                                     acpi_ps_next_parse_state(walk_state, op,
583                                                              status);
584                                 if (status == AE_CTRL_PENDING) {
585                                         status =
586                                             acpi_ps_complete_op(walk_state, &op,
587                                                                 AE_OK);
588                                         if (ACPI_FAILURE(status)) {
589                                                 return_ACPI_STATUS(status);
590                                         }
591                                 }
592
593                                 if (status == AE_CTRL_TERMINATE) {
594                                         status = AE_OK;
595
596                                         /* Clean up */
597                                         do {
598                                                 if (op) {
599                                                         status2 =
600                                                             acpi_ps_complete_this_op
601                                                             (walk_state, op);
602                                                         if (ACPI_FAILURE
603                                                             (status2)) {
604                                                                 return_ACPI_STATUS
605                                                                     (status2);
606                                                         }
607                                                 }
608
609                                                 acpi_ps_pop_scope(&
610                                                                   (walk_state->
611                                                                    parser_state),
612                                                                   &op,
613                                                                   &walk_state->
614                                                                   arg_types,
615                                                                   &walk_state->
616                                                                   arg_count);
617
618                                         } while (op);
619
620                                         return_ACPI_STATUS(status);
621                                 }
622
623                                 else if (ACPI_FAILURE(status)) {
624
625                                         /* First error is most important */
626
627                                         (void)
628                                             acpi_ps_complete_this_op(walk_state,
629                                                                      op);
630                                         return_ACPI_STATUS(status);
631                                 }
632                         }
633
634                         status2 = acpi_ps_complete_this_op(walk_state, op);
635                         if (ACPI_FAILURE(status2)) {
636                                 return_ACPI_STATUS(status2);
637                         }
638                 }
639
640                 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
641                                   &walk_state->arg_types,
642                                   &walk_state->arg_count);
643
644         } while (op);
645
646         return_ACPI_STATUS(status);
647 }