Linux 4.1.16
[linux/fpc-iii.git] / drivers / acpi / acpica / psobject.c
blob2f5ddd806c58bac51c88f16c53705c6213108c47
1 /******************************************************************************
3 * Module Name: psobject - Support for parse objects
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2015, Intel Corp.
9 * All rights reserved.
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.
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.
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.
44 #include <acpi/acpi.h>
45 #include "accommon.h"
46 #include "acparser.h"
47 #include "amlcode.h"
49 #define _COMPONENT ACPI_PARSER
50 ACPI_MODULE_NAME("psobject")
52 /* Local prototypes */
53 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state);
55 /*******************************************************************************
57 * FUNCTION: acpi_ps_get_aml_opcode
59 * PARAMETERS: walk_state - Current state
61 * RETURN: Status
63 * DESCRIPTION: Extract the next AML opcode from the input stream.
65 ******************************************************************************/
67 static acpi_status acpi_ps_get_aml_opcode(struct acpi_walk_state *walk_state)
70 ACPI_FUNCTION_TRACE_PTR(ps_get_aml_opcode, walk_state);
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));
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
83 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
85 switch (walk_state->op_info->class) {
86 case AML_CLASS_ASCII:
87 case AML_CLASS_PREFIX:
89 * Starts with a valid prefix or ASCII char, this is a name
90 * string. Convert the bare name string to a namepath.
92 walk_state->opcode = AML_INT_NAMEPATH_OP;
93 walk_state->arg_types = ARGP_NAMESTRING;
94 break;
96 case AML_CLASS_UNKNOWN:
98 /* The opcode is unrecognized. Complain and skip unknown opcodes */
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))));
107 ACPI_DUMP_BUFFER((walk_state->parser_state.aml - 16),
108 48);
110 #ifdef ACPI_ASL_COMPILER
112 * This is executed for the disassembler only. Output goes
113 * to the disassembled ASL output file.
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)));
121 /* Dump the context surrounding the invalid opcode */
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
132 /* Increment past one-byte or two-byte opcode */
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++;
139 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
141 default:
143 /* Found opcode info, this is a normal opcode */
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;
151 return_ACPI_STATUS(AE_OK);
154 /*******************************************************************************
156 * FUNCTION: acpi_ps_build_named_op
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
163 * RETURN: Status
165 * DESCRIPTION: Parse a named Op
167 ******************************************************************************/
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)
175 acpi_status status = AE_OK;
176 union acpi_parse_object *arg = NULL;
178 ACPI_FUNCTION_TRACE_PTR(ps_build_named_op, walk_state);
180 unnamed_op->common.value.arg = NULL;
181 unnamed_op->common.arg_list_length = 0;
182 unnamed_op->common.aml_opcode = walk_state->opcode;
185 * Get and append arguments until we find the node that contains
186 * the name (the type ARGP_NAME).
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);
199 acpi_ps_append_arg(unnamed_op, arg);
200 INCREMENT_ARG_LIST(walk_state->arg_types);
204 * Make sure that we found a NAME and didn't run out of arguments
206 if (!GET_CURRENT_ARG_TYPE(walk_state->arg_types)) {
207 return_ACPI_STATUS(AE_AML_NO_OPERAND);
210 /* We know that this arg is a name, move to next arg */
212 INCREMENT_ARG_LIST(walk_state->arg_types);
215 * Find the object. This will either insert the object into
216 * the namespace or simply look it up
218 walk_state->op = NULL;
220 status = walk_state->descending_callback(walk_state, op);
221 if (ACPI_FAILURE(status)) {
222 if (status != AE_CTRL_TERMINATE) {
223 ACPI_EXCEPTION((AE_INFO, status,
224 "During name lookup/catalog"));
226 return_ACPI_STATUS(status);
229 if (!*op) {
230 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
233 status = acpi_ps_next_parse_state(walk_state, *op, status);
234 if (ACPI_FAILURE(status)) {
235 if (status == AE_CTRL_PENDING) {
236 status = AE_CTRL_PARSE_PENDING;
238 return_ACPI_STATUS(status);
241 acpi_ps_append_arg(*op, unnamed_op->common.value.arg);
243 if ((*op)->common.aml_opcode == AML_REGION_OP ||
244 (*op)->common.aml_opcode == AML_DATA_REGION_OP) {
246 * Defer final parsing of an operation_region body, because we don't
247 * have enough info in the first pass to parse it correctly (i.e.,
248 * there may be method calls within the term_arg elements of the body.)
250 * However, we must continue parsing because the opregion is not a
251 * standalone package -- we don't know where the end is at this point.
253 * (Length is unknown until parse of the body complete)
255 (*op)->named.data = aml_op_start;
256 (*op)->named.length = 0;
259 return_ACPI_STATUS(AE_OK);
262 /*******************************************************************************
264 * FUNCTION: acpi_ps_create_op
266 * PARAMETERS: walk_state - Current state
267 * aml_op_start - Op start in AML
268 * new_op - Returned Op
270 * RETURN: Status
272 * DESCRIPTION: Get Op from AML
274 ******************************************************************************/
276 acpi_status
277 acpi_ps_create_op(struct acpi_walk_state *walk_state,
278 u8 *aml_op_start, union acpi_parse_object **new_op)
280 acpi_status status = AE_OK;
281 union acpi_parse_object *op;
282 union acpi_parse_object *named_op = NULL;
283 union acpi_parse_object *parent_scope;
284 u8 argument_count;
285 const struct acpi_opcode_info *op_info;
287 ACPI_FUNCTION_TRACE_PTR(ps_create_op, walk_state);
289 status = acpi_ps_get_aml_opcode(walk_state);
290 if (status == AE_CTRL_PARSE_CONTINUE) {
291 return_ACPI_STATUS(AE_CTRL_PARSE_CONTINUE);
294 /* Create Op structure and append to parent's argument list */
296 walk_state->op_info = acpi_ps_get_opcode_info(walk_state->opcode);
297 op = acpi_ps_alloc_op(walk_state->opcode);
298 if (!op) {
299 return_ACPI_STATUS(AE_NO_MEMORY);
302 if (walk_state->op_info->flags & AML_NAMED) {
303 status =
304 acpi_ps_build_named_op(walk_state, aml_op_start, op,
305 &named_op);
306 acpi_ps_free_op(op);
307 if (ACPI_FAILURE(status)) {
308 return_ACPI_STATUS(status);
311 *new_op = named_op;
312 return_ACPI_STATUS(AE_OK);
315 /* Not a named opcode, just allocate Op and append to parent */
317 if (walk_state->op_info->flags & AML_CREATE) {
319 * Backup to beginning of create_XXXfield declaration
320 * body_length is unknown until we parse the body
322 op->named.data = aml_op_start;
323 op->named.length = 0;
326 if (walk_state->opcode == AML_BANK_FIELD_OP) {
328 * Backup to beginning of bank_field declaration
329 * body_length is unknown until we parse the body
331 op->named.data = aml_op_start;
332 op->named.length = 0;
335 parent_scope = acpi_ps_get_parent_scope(&(walk_state->parser_state));
336 acpi_ps_append_arg(parent_scope, op);
338 if (parent_scope) {
339 op_info =
340 acpi_ps_get_opcode_info(parent_scope->common.aml_opcode);
341 if (op_info->flags & AML_HAS_TARGET) {
342 argument_count =
343 acpi_ps_get_argument_count(op_info->type);
344 if (parent_scope->common.arg_list_length >
345 argument_count) {
346 op->common.flags |= ACPI_PARSEOP_TARGET;
348 } else if (parent_scope->common.aml_opcode == AML_INCREMENT_OP) {
349 op->common.flags |= ACPI_PARSEOP_TARGET;
353 if (walk_state->descending_callback != NULL) {
355 * Find the object. This will either insert the object into
356 * the namespace or simply look it up
358 walk_state->op = *new_op = op;
360 status = walk_state->descending_callback(walk_state, &op);
361 status = acpi_ps_next_parse_state(walk_state, op, status);
362 if (status == AE_CTRL_PENDING) {
363 status = AE_CTRL_PARSE_PENDING;
367 return_ACPI_STATUS(status);
370 /*******************************************************************************
372 * FUNCTION: acpi_ps_complete_op
374 * PARAMETERS: walk_state - Current state
375 * op - Returned Op
376 * status - Parse status before complete Op
378 * RETURN: Status
380 * DESCRIPTION: Complete Op
382 ******************************************************************************/
384 acpi_status
385 acpi_ps_complete_op(struct acpi_walk_state *walk_state,
386 union acpi_parse_object **op, acpi_status status)
388 acpi_status status2;
390 ACPI_FUNCTION_TRACE_PTR(ps_complete_op, walk_state);
393 * Finished one argument of the containing scope
395 walk_state->parser_state.scope->parse_scope.arg_count--;
397 /* Close this Op (will result in parse subtree deletion) */
399 status2 = acpi_ps_complete_this_op(walk_state, *op);
400 if (ACPI_FAILURE(status2)) {
401 return_ACPI_STATUS(status2);
404 *op = NULL;
406 switch (status) {
407 case AE_OK:
409 break;
411 case AE_CTRL_TRANSFER:
413 /* We are about to transfer to a called method */
415 walk_state->prev_op = NULL;
416 walk_state->prev_arg_types = walk_state->arg_types;
417 return_ACPI_STATUS(status);
419 case AE_CTRL_END:
421 acpi_ps_pop_scope(&(walk_state->parser_state), op,
422 &walk_state->arg_types,
423 &walk_state->arg_count);
425 if (*op) {
426 walk_state->op = *op;
427 walk_state->op_info =
428 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
429 walk_state->opcode = (*op)->common.aml_opcode;
431 status = walk_state->ascending_callback(walk_state);
432 status =
433 acpi_ps_next_parse_state(walk_state, *op, status);
435 status2 = acpi_ps_complete_this_op(walk_state, *op);
436 if (ACPI_FAILURE(status2)) {
437 return_ACPI_STATUS(status2);
441 status = AE_OK;
442 break;
444 case AE_CTRL_BREAK:
445 case AE_CTRL_CONTINUE:
447 /* Pop off scopes until we find the While */
449 while (!(*op) || ((*op)->common.aml_opcode != AML_WHILE_OP)) {
450 acpi_ps_pop_scope(&(walk_state->parser_state), op,
451 &walk_state->arg_types,
452 &walk_state->arg_count);
455 /* Close this iteration of the While loop */
457 walk_state->op = *op;
458 walk_state->op_info =
459 acpi_ps_get_opcode_info((*op)->common.aml_opcode);
460 walk_state->opcode = (*op)->common.aml_opcode;
462 status = walk_state->ascending_callback(walk_state);
463 status = acpi_ps_next_parse_state(walk_state, *op, status);
465 status2 = acpi_ps_complete_this_op(walk_state, *op);
466 if (ACPI_FAILURE(status2)) {
467 return_ACPI_STATUS(status2);
470 status = AE_OK;
471 break;
473 case AE_CTRL_TERMINATE:
475 /* Clean up */
476 do {
477 if (*op) {
478 status2 =
479 acpi_ps_complete_this_op(walk_state, *op);
480 if (ACPI_FAILURE(status2)) {
481 return_ACPI_STATUS(status2);
484 acpi_ut_delete_generic_state
485 (acpi_ut_pop_generic_state
486 (&walk_state->control_state));
489 acpi_ps_pop_scope(&(walk_state->parser_state), op,
490 &walk_state->arg_types,
491 &walk_state->arg_count);
493 } while (*op);
495 return_ACPI_STATUS(AE_OK);
497 default: /* All other non-AE_OK status */
499 do {
500 if (*op) {
501 status2 =
502 acpi_ps_complete_this_op(walk_state, *op);
503 if (ACPI_FAILURE(status2)) {
504 return_ACPI_STATUS(status2);
508 acpi_ps_pop_scope(&(walk_state->parser_state), op,
509 &walk_state->arg_types,
510 &walk_state->arg_count);
512 } while (*op);
514 #if 0
516 * TBD: Cleanup parse ops on error
518 if (*op == NULL) {
519 acpi_ps_pop_scope(parser_state, op,
520 &walk_state->arg_types,
521 &walk_state->arg_count);
523 #endif
524 walk_state->prev_op = NULL;
525 walk_state->prev_arg_types = walk_state->arg_types;
526 return_ACPI_STATUS(status);
529 /* This scope complete? */
531 if (acpi_ps_has_completed_scope(&(walk_state->parser_state))) {
532 acpi_ps_pop_scope(&(walk_state->parser_state), op,
533 &walk_state->arg_types,
534 &walk_state->arg_count);
535 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "Popped scope, Op=%p\n", *op));
536 } else {
537 *op = NULL;
540 return_ACPI_STATUS(AE_OK);
543 /*******************************************************************************
545 * FUNCTION: acpi_ps_complete_final_op
547 * PARAMETERS: walk_state - Current state
548 * op - Current Op
549 * status - Current parse status before complete last
550 * Op
552 * RETURN: Status
554 * DESCRIPTION: Complete last Op.
556 ******************************************************************************/
558 acpi_status
559 acpi_ps_complete_final_op(struct acpi_walk_state *walk_state,
560 union acpi_parse_object *op, acpi_status status)
562 acpi_status status2;
564 ACPI_FUNCTION_TRACE_PTR(ps_complete_final_op, walk_state);
567 * Complete the last Op (if not completed), and clear the scope stack.
568 * It is easily possible to end an AML "package" with an unbounded number
569 * of open scopes (such as when several ASL blocks are closed with
570 * sequential closing braces). We want to terminate each one cleanly.
572 ACPI_DEBUG_PRINT((ACPI_DB_PARSE, "AML package complete at Op %p\n",
573 op));
574 do {
575 if (op) {
576 if (walk_state->ascending_callback != NULL) {
577 walk_state->op = op;
578 walk_state->op_info =
579 acpi_ps_get_opcode_info(op->common.
580 aml_opcode);
581 walk_state->opcode = op->common.aml_opcode;
583 status =
584 walk_state->ascending_callback(walk_state);
585 status =
586 acpi_ps_next_parse_state(walk_state, op,
587 status);
588 if (status == AE_CTRL_PENDING) {
589 status =
590 acpi_ps_complete_op(walk_state, &op,
591 AE_OK);
592 if (ACPI_FAILURE(status)) {
593 return_ACPI_STATUS(status);
597 if (status == AE_CTRL_TERMINATE) {
598 status = AE_OK;
600 /* Clean up */
601 do {
602 if (op) {
603 status2 =
604 acpi_ps_complete_this_op
605 (walk_state, op);
606 if (ACPI_FAILURE
607 (status2)) {
608 return_ACPI_STATUS
609 (status2);
613 acpi_ps_pop_scope(&
614 (walk_state->
615 parser_state),
616 &op,
617 &walk_state->
618 arg_types,
619 &walk_state->
620 arg_count);
622 } while (op);
624 return_ACPI_STATUS(status);
627 else if (ACPI_FAILURE(status)) {
629 /* First error is most important */
631 (void)
632 acpi_ps_complete_this_op(walk_state,
633 op);
634 return_ACPI_STATUS(status);
638 status2 = acpi_ps_complete_this_op(walk_state, op);
639 if (ACPI_FAILURE(status2)) {
640 return_ACPI_STATUS(status2);
644 acpi_ps_pop_scope(&(walk_state->parser_state), &op,
645 &walk_state->arg_types,
646 &walk_state->arg_count);
648 } while (op);
650 return_ACPI_STATUS(status);