a3f29798d1d141670bed635ba4d9c312fac84d33
[pandora-kernel.git] / drivers / acpi / dispatcher / dsopcode.c
1 /******************************************************************************
2  *
3  * Module Name: dsopcode - Dispatcher Op Region support and handling of
4  *                         "control" opcodes
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2007, R. Byron Moore
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
15  * 1. Redistributions of source code must retain the above copyright
16  *    notice, this list of conditions, and the following disclaimer,
17  *    without modification.
18  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19  *    substantially similar to the "NO WARRANTY" disclaimer below
20  *    ("Disclaimer") and any redistribution must be conditioned upon
21  *    including a substantially similar Disclaimer requirement for further
22  *    binary redistribution.
23  * 3. Neither the names of the above-listed copyright holders nor the names
24  *    of any contributors may be used to endorse or promote products derived
25  *    from this software without specific prior written permission.
26  *
27  * Alternatively, this software may be distributed under the terms of the
28  * GNU General Public License ("GPL") version 2 as published by the Free
29  * Software Foundation.
30  *
31  * NO WARRANTY
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42  * POSSIBILITY OF SUCH DAMAGES.
43  */
44
45 #include <acpi/acpi.h>
46 #include <acpi/acparser.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acdispat.h>
49 #include <acpi/acinterp.h>
50 #include <acpi/acnamesp.h>
51 #include <acpi/acevents.h>
52 #include <acpi/actables.h>
53
54 #define _COMPONENT          ACPI_DISPATCHER
55 ACPI_MODULE_NAME("dsopcode")
56
57 /* Local prototypes */
58 static acpi_status
59 acpi_ds_execute_arguments(struct acpi_namespace_node *node,
60                           struct acpi_namespace_node *scope_node,
61                           u32 aml_length, u8 * aml_start);
62
63 static acpi_status
64 acpi_ds_init_buffer_field(u16 aml_opcode,
65                           union acpi_operand_object *obj_desc,
66                           union acpi_operand_object *buffer_desc,
67                           union acpi_operand_object *offset_desc,
68                           union acpi_operand_object *length_desc,
69                           union acpi_operand_object *result_desc);
70
71 /*******************************************************************************
72  *
73  * FUNCTION:    acpi_ds_execute_arguments
74  *
75  * PARAMETERS:  Node                - Object NS node
76  *              scope_node          - Parent NS node
77  *              aml_length          - Length of executable AML
78  *              aml_start           - Pointer to the AML
79  *
80  * RETURN:      Status.
81  *
82  * DESCRIPTION: Late (deferred) execution of region or field arguments
83  *
84  ******************************************************************************/
85
86 static acpi_status
87 acpi_ds_execute_arguments(struct acpi_namespace_node *node,
88                           struct acpi_namespace_node *scope_node,
89                           u32 aml_length, u8 * aml_start)
90 {
91         acpi_status status;
92         union acpi_parse_object *op;
93         struct acpi_walk_state *walk_state;
94
95         ACPI_FUNCTION_TRACE(ds_execute_arguments);
96
97         /*
98          * Allocate a new parser op to be the root of the parsed tree
99          */
100         op = acpi_ps_alloc_op(AML_INT_EVAL_SUBTREE_OP);
101         if (!op) {
102                 return_ACPI_STATUS(AE_NO_MEMORY);
103         }
104
105         /* Save the Node for use in acpi_ps_parse_aml */
106
107         op->common.node = scope_node;
108
109         /* Create and initialize a new parser state */
110
111         walk_state = acpi_ds_create_walk_state(0, NULL, NULL, NULL);
112         if (!walk_state) {
113                 status = AE_NO_MEMORY;
114                 goto cleanup;
115         }
116
117         status = acpi_ds_init_aml_walk(walk_state, op, NULL, aml_start,
118                                        aml_length, NULL, ACPI_IMODE_LOAD_PASS1);
119         if (ACPI_FAILURE(status)) {
120                 acpi_ds_delete_walk_state(walk_state);
121                 goto cleanup;
122         }
123
124         /* Mark this parse as a deferred opcode */
125
126         walk_state->parse_flags = ACPI_PARSE_DEFERRED_OP;
127         walk_state->deferred_node = node;
128
129         /* Pass1: Parse the entire declaration */
130
131         status = acpi_ps_parse_aml(walk_state);
132         if (ACPI_FAILURE(status)) {
133                 goto cleanup;
134         }
135
136         /* Get and init the Op created above */
137
138         op->common.node = node;
139         acpi_ps_delete_parse_tree(op);
140
141         /* Evaluate the deferred arguments */
142
143         op = acpi_ps_alloc_op(AML_INT_EVAL_SUBTREE_OP);
144         if (!op) {
145                 return_ACPI_STATUS(AE_NO_MEMORY);
146         }
147
148         op->common.node = scope_node;
149
150         /* Create and initialize a new parser state */
151
152         walk_state = acpi_ds_create_walk_state(0, NULL, NULL, NULL);
153         if (!walk_state) {
154                 status = AE_NO_MEMORY;
155                 goto cleanup;
156         }
157
158         /* Execute the opcode and arguments */
159
160         status = acpi_ds_init_aml_walk(walk_state, op, NULL, aml_start,
161                                        aml_length, NULL, ACPI_IMODE_EXECUTE);
162         if (ACPI_FAILURE(status)) {
163                 acpi_ds_delete_walk_state(walk_state);
164                 goto cleanup;
165         }
166
167         /* Mark this execution as a deferred opcode */
168
169         walk_state->deferred_node = node;
170         status = acpi_ps_parse_aml(walk_state);
171
172       cleanup:
173         acpi_ps_delete_parse_tree(op);
174         return_ACPI_STATUS(status);
175 }
176
177 /*******************************************************************************
178  *
179  * FUNCTION:    acpi_ds_get_buffer_field_arguments
180  *
181  * PARAMETERS:  obj_desc        - A valid buffer_field object
182  *
183  * RETURN:      Status.
184  *
185  * DESCRIPTION: Get buffer_field Buffer and Index. This implements the late
186  *              evaluation of these field attributes.
187  *
188  ******************************************************************************/
189
190 acpi_status
191 acpi_ds_get_buffer_field_arguments(union acpi_operand_object *obj_desc)
192 {
193         union acpi_operand_object *extra_desc;
194         struct acpi_namespace_node *node;
195         acpi_status status;
196
197         ACPI_FUNCTION_TRACE_PTR(ds_get_buffer_field_arguments, obj_desc);
198
199         if (obj_desc->common.flags & AOPOBJ_DATA_VALID) {
200                 return_ACPI_STATUS(AE_OK);
201         }
202
203         /* Get the AML pointer (method object) and buffer_field node */
204
205         extra_desc = acpi_ns_get_secondary_object(obj_desc);
206         node = obj_desc->buffer_field.node;
207
208         ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
209                         (ACPI_TYPE_BUFFER_FIELD, node, NULL));
210         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "[%4.4s] BufferField Arg Init\n",
211                           acpi_ut_get_node_name(node)));
212
213         /* Execute the AML code for the term_arg arguments */
214
215         status = acpi_ds_execute_arguments(node, acpi_ns_get_parent_node(node),
216                                            extra_desc->extra.aml_length,
217                                            extra_desc->extra.aml_start);
218         return_ACPI_STATUS(status);
219 }
220
221 /*******************************************************************************
222  *
223  * FUNCTION:    acpi_ds_get_buffer_arguments
224  *
225  * PARAMETERS:  obj_desc        - A valid Buffer object
226  *
227  * RETURN:      Status.
228  *
229  * DESCRIPTION: Get Buffer length and initializer byte list.  This implements
230  *              the late evaluation of these attributes.
231  *
232  ******************************************************************************/
233
234 acpi_status acpi_ds_get_buffer_arguments(union acpi_operand_object *obj_desc)
235 {
236         struct acpi_namespace_node *node;
237         acpi_status status;
238
239         ACPI_FUNCTION_TRACE_PTR(ds_get_buffer_arguments, obj_desc);
240
241         if (obj_desc->common.flags & AOPOBJ_DATA_VALID) {
242                 return_ACPI_STATUS(AE_OK);
243         }
244
245         /* Get the Buffer node */
246
247         node = obj_desc->buffer.node;
248         if (!node) {
249                 ACPI_ERROR((AE_INFO,
250                             "No pointer back to NS node in buffer obj %p",
251                             obj_desc));
252                 return_ACPI_STATUS(AE_AML_INTERNAL);
253         }
254
255         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Buffer Arg Init\n"));
256
257         /* Execute the AML code for the term_arg arguments */
258
259         status = acpi_ds_execute_arguments(node, node,
260                                            obj_desc->buffer.aml_length,
261                                            obj_desc->buffer.aml_start);
262         return_ACPI_STATUS(status);
263 }
264
265 /*******************************************************************************
266  *
267  * FUNCTION:    acpi_ds_get_package_arguments
268  *
269  * PARAMETERS:  obj_desc        - A valid Package object
270  *
271  * RETURN:      Status.
272  *
273  * DESCRIPTION: Get Package length and initializer byte list.  This implements
274  *              the late evaluation of these attributes.
275  *
276  ******************************************************************************/
277
278 acpi_status acpi_ds_get_package_arguments(union acpi_operand_object *obj_desc)
279 {
280         struct acpi_namespace_node *node;
281         acpi_status status;
282
283         ACPI_FUNCTION_TRACE_PTR(ds_get_package_arguments, obj_desc);
284
285         if (obj_desc->common.flags & AOPOBJ_DATA_VALID) {
286                 return_ACPI_STATUS(AE_OK);
287         }
288
289         /* Get the Package node */
290
291         node = obj_desc->package.node;
292         if (!node) {
293                 ACPI_ERROR((AE_INFO,
294                             "No pointer back to NS node in package %p",
295                             obj_desc));
296                 return_ACPI_STATUS(AE_AML_INTERNAL);
297         }
298
299         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "Package Arg Init\n"));
300
301         /* Execute the AML code for the term_arg arguments */
302
303         status = acpi_ds_execute_arguments(node, node,
304                                            obj_desc->package.aml_length,
305                                            obj_desc->package.aml_start);
306         return_ACPI_STATUS(status);
307 }
308
309 /*****************************************************************************
310  *
311  * FUNCTION:    acpi_ds_get_region_arguments
312  *
313  * PARAMETERS:  obj_desc        - A valid region object
314  *
315  * RETURN:      Status.
316  *
317  * DESCRIPTION: Get region address and length.  This implements the late
318  *              evaluation of these region attributes.
319  *
320  ****************************************************************************/
321
322 acpi_status acpi_ds_get_region_arguments(union acpi_operand_object *obj_desc)
323 {
324         struct acpi_namespace_node *node;
325         acpi_status status;
326         union acpi_operand_object *extra_desc;
327
328         ACPI_FUNCTION_TRACE_PTR(ds_get_region_arguments, obj_desc);
329
330         if (obj_desc->region.flags & AOPOBJ_DATA_VALID) {
331                 return_ACPI_STATUS(AE_OK);
332         }
333
334         extra_desc = acpi_ns_get_secondary_object(obj_desc);
335         if (!extra_desc) {
336                 return_ACPI_STATUS(AE_NOT_EXIST);
337         }
338
339         /* Get the Region node */
340
341         node = obj_desc->region.node;
342
343         ACPI_DEBUG_EXEC(acpi_ut_display_init_pathname
344                         (ACPI_TYPE_REGION, node, NULL));
345
346         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "[%4.4s] OpRegion Arg Init at AML %p\n",
347                           acpi_ut_get_node_name(node),
348                           extra_desc->extra.aml_start));
349
350         /* Execute the argument AML */
351
352         status = acpi_ds_execute_arguments(node, acpi_ns_get_parent_node(node),
353                                            extra_desc->extra.aml_length,
354                                            extra_desc->extra.aml_start);
355         if (ACPI_FAILURE(status)) {
356                 return_ACPI_STATUS(status);
357         }
358
359         /* Validate the region address/length via the host OS */
360
361         status = acpi_os_validate_address(obj_desc->region.space_id,
362                                           obj_desc->region.address,
363                                           (acpi_size) obj_desc->region.length,
364                                           acpi_ut_get_node_name(node));
365
366         if (ACPI_FAILURE(status)) {
367                 /*
368                  * Invalid address/length. We will emit an error message and mark
369                  * the region as invalid, so that it will cause an additional error if
370                  * it is ever used. Then return AE_OK.
371                  */
372                 ACPI_EXCEPTION((AE_INFO, status,
373                                 "During address validation of OpRegion [%4.4s]",
374                                 node->name.ascii));
375                 obj_desc->common.flags |= AOPOBJ_INVALID;
376                 status = AE_OK;
377         }
378
379         return_ACPI_STATUS(status);
380 }
381
382 /*******************************************************************************
383  *
384  * FUNCTION:    acpi_ds_initialize_region
385  *
386  * PARAMETERS:  obj_handle      - Region namespace node
387  *
388  * RETURN:      Status
389  *
390  * DESCRIPTION: Front end to ev_initialize_region
391  *
392  ******************************************************************************/
393
394 acpi_status acpi_ds_initialize_region(acpi_handle obj_handle)
395 {
396         union acpi_operand_object *obj_desc;
397         acpi_status status;
398
399         obj_desc = acpi_ns_get_attached_object(obj_handle);
400
401         /* Namespace is NOT locked */
402
403         status = acpi_ev_initialize_region(obj_desc, FALSE);
404         return (status);
405 }
406
407 /*******************************************************************************
408  *
409  * FUNCTION:    acpi_ds_init_buffer_field
410  *
411  * PARAMETERS:  aml_opcode      - create_xxx_field
412  *              obj_desc        - buffer_field object
413  *              buffer_desc     - Host Buffer
414  *              offset_desc     - Offset into buffer
415  *              length_desc     - Length of field (CREATE_FIELD_OP only)
416  *              result_desc     - Where to store the result
417  *
418  * RETURN:      Status
419  *
420  * DESCRIPTION: Perform actual initialization of a buffer field
421  *
422  ******************************************************************************/
423
424 static acpi_status
425 acpi_ds_init_buffer_field(u16 aml_opcode,
426                           union acpi_operand_object *obj_desc,
427                           union acpi_operand_object *buffer_desc,
428                           union acpi_operand_object *offset_desc,
429                           union acpi_operand_object *length_desc,
430                           union acpi_operand_object *result_desc)
431 {
432         u32 offset;
433         u32 bit_offset;
434         u32 bit_count;
435         u8 field_flags;
436         acpi_status status;
437
438         ACPI_FUNCTION_TRACE_PTR(ds_init_buffer_field, obj_desc);
439
440         /* Host object must be a Buffer */
441
442         if (ACPI_GET_OBJECT_TYPE(buffer_desc) != ACPI_TYPE_BUFFER) {
443                 ACPI_ERROR((AE_INFO,
444                             "Target of Create Field is not a Buffer object - %s",
445                             acpi_ut_get_object_type_name(buffer_desc)));
446
447                 status = AE_AML_OPERAND_TYPE;
448                 goto cleanup;
449         }
450
451         /*
452          * The last parameter to all of these opcodes (result_desc) started
453          * out as a name_string, and should therefore now be a NS node
454          * after resolution in acpi_ex_resolve_operands().
455          */
456         if (ACPI_GET_DESCRIPTOR_TYPE(result_desc) != ACPI_DESC_TYPE_NAMED) {
457                 ACPI_ERROR((AE_INFO,
458                             "(%s) destination not a NS Node [%s]",
459                             acpi_ps_get_opcode_name(aml_opcode),
460                             acpi_ut_get_descriptor_name(result_desc)));
461
462                 status = AE_AML_OPERAND_TYPE;
463                 goto cleanup;
464         }
465
466         offset = (u32) offset_desc->integer.value;
467
468         /*
469          * Setup the Bit offsets and counts, according to the opcode
470          */
471         switch (aml_opcode) {
472         case AML_CREATE_FIELD_OP:
473
474                 /* Offset is in bits, count is in bits */
475
476                 field_flags = AML_FIELD_ACCESS_BYTE;
477                 bit_offset = offset;
478                 bit_count = (u32) length_desc->integer.value;
479
480                 /* Must have a valid (>0) bit count */
481
482                 if (bit_count == 0) {
483                         ACPI_ERROR((AE_INFO,
484                                     "Attempt to CreateField of length zero"));
485                         status = AE_AML_OPERAND_VALUE;
486                         goto cleanup;
487                 }
488                 break;
489
490         case AML_CREATE_BIT_FIELD_OP:
491
492                 /* Offset is in bits, Field is one bit */
493
494                 bit_offset = offset;
495                 bit_count = 1;
496                 field_flags = AML_FIELD_ACCESS_BYTE;
497                 break;
498
499         case AML_CREATE_BYTE_FIELD_OP:
500
501                 /* Offset is in bytes, field is one byte */
502
503                 bit_offset = 8 * offset;
504                 bit_count = 8;
505                 field_flags = AML_FIELD_ACCESS_BYTE;
506                 break;
507
508         case AML_CREATE_WORD_FIELD_OP:
509
510                 /* Offset is in bytes, field is one word */
511
512                 bit_offset = 8 * offset;
513                 bit_count = 16;
514                 field_flags = AML_FIELD_ACCESS_WORD;
515                 break;
516
517         case AML_CREATE_DWORD_FIELD_OP:
518
519                 /* Offset is in bytes, field is one dword */
520
521                 bit_offset = 8 * offset;
522                 bit_count = 32;
523                 field_flags = AML_FIELD_ACCESS_DWORD;
524                 break;
525
526         case AML_CREATE_QWORD_FIELD_OP:
527
528                 /* Offset is in bytes, field is one qword */
529
530                 bit_offset = 8 * offset;
531                 bit_count = 64;
532                 field_flags = AML_FIELD_ACCESS_QWORD;
533                 break;
534
535         default:
536
537                 ACPI_ERROR((AE_INFO,
538                             "Unknown field creation opcode %02x", aml_opcode));
539                 status = AE_AML_BAD_OPCODE;
540                 goto cleanup;
541         }
542
543         /* Entire field must fit within the current length of the buffer */
544
545         if ((bit_offset + bit_count) > (8 * (u32) buffer_desc->buffer.length)) {
546                 ACPI_ERROR((AE_INFO,
547                             "Field [%4.4s] at %d exceeds Buffer [%4.4s] size %d (bits)",
548                             acpi_ut_get_node_name(result_desc),
549                             bit_offset + bit_count,
550                             acpi_ut_get_node_name(buffer_desc->buffer.node),
551                             8 * (u32) buffer_desc->buffer.length));
552                 status = AE_AML_BUFFER_LIMIT;
553                 goto cleanup;
554         }
555
556         /*
557          * Initialize areas of the field object that are common to all fields
558          * For field_flags, use LOCK_RULE = 0 (NO_LOCK),
559          * UPDATE_RULE = 0 (UPDATE_PRESERVE)
560          */
561         status = acpi_ex_prep_common_field_object(obj_desc, field_flags, 0,
562                                                   bit_offset, bit_count);
563         if (ACPI_FAILURE(status)) {
564                 goto cleanup;
565         }
566
567         obj_desc->buffer_field.buffer_obj = buffer_desc;
568
569         /* Reference count for buffer_desc inherits obj_desc count */
570
571         buffer_desc->common.reference_count = (u16)
572             (buffer_desc->common.reference_count +
573              obj_desc->common.reference_count);
574
575       cleanup:
576
577         /* Always delete the operands */
578
579         acpi_ut_remove_reference(offset_desc);
580         acpi_ut_remove_reference(buffer_desc);
581
582         if (aml_opcode == AML_CREATE_FIELD_OP) {
583                 acpi_ut_remove_reference(length_desc);
584         }
585
586         /* On failure, delete the result descriptor */
587
588         if (ACPI_FAILURE(status)) {
589                 acpi_ut_remove_reference(result_desc);  /* Result descriptor */
590         } else {
591                 /* Now the address and length are valid for this buffer_field */
592
593                 obj_desc->buffer_field.flags |= AOPOBJ_DATA_VALID;
594         }
595
596         return_ACPI_STATUS(status);
597 }
598
599 /*******************************************************************************
600  *
601  * FUNCTION:    acpi_ds_eval_buffer_field_operands
602  *
603  * PARAMETERS:  walk_state      - Current walk
604  *              Op              - A valid buffer_field Op object
605  *
606  * RETURN:      Status
607  *
608  * DESCRIPTION: Get buffer_field Buffer and Index
609  *              Called from acpi_ds_exec_end_op during buffer_field parse tree walk
610  *
611  ******************************************************************************/
612
613 acpi_status
614 acpi_ds_eval_buffer_field_operands(struct acpi_walk_state *walk_state,
615                                    union acpi_parse_object *op)
616 {
617         acpi_status status;
618         union acpi_operand_object *obj_desc;
619         struct acpi_namespace_node *node;
620         union acpi_parse_object *next_op;
621
622         ACPI_FUNCTION_TRACE_PTR(ds_eval_buffer_field_operands, op);
623
624         /*
625          * This is where we evaluate the address and length fields of the
626          * create_xxx_field declaration
627          */
628         node = op->common.node;
629
630         /* next_op points to the op that holds the Buffer */
631
632         next_op = op->common.value.arg;
633
634         /* Evaluate/create the address and length operands */
635
636         status = acpi_ds_create_operands(walk_state, next_op);
637         if (ACPI_FAILURE(status)) {
638                 return_ACPI_STATUS(status);
639         }
640
641         obj_desc = acpi_ns_get_attached_object(node);
642         if (!obj_desc) {
643                 return_ACPI_STATUS(AE_NOT_EXIST);
644         }
645
646         /* Resolve the operands */
647
648         status = acpi_ex_resolve_operands(op->common.aml_opcode,
649                                           ACPI_WALK_OPERANDS, walk_state);
650
651         ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE,
652                            acpi_ps_get_opcode_name(op->common.aml_opcode),
653                            walk_state->num_operands,
654                            "after AcpiExResolveOperands");
655
656         if (ACPI_FAILURE(status)) {
657                 ACPI_ERROR((AE_INFO, "(%s) bad operand(s) (%X)",
658                             acpi_ps_get_opcode_name(op->common.aml_opcode),
659                             status));
660
661                 return_ACPI_STATUS(status);
662         }
663
664         /* Initialize the Buffer Field */
665
666         if (op->common.aml_opcode == AML_CREATE_FIELD_OP) {
667
668                 /* NOTE: Slightly different operands for this opcode */
669
670                 status =
671                     acpi_ds_init_buffer_field(op->common.aml_opcode, obj_desc,
672                                               walk_state->operands[0],
673                                               walk_state->operands[1],
674                                               walk_state->operands[2],
675                                               walk_state->operands[3]);
676         } else {
677                 /* All other, create_xxx_field opcodes */
678
679                 status =
680                     acpi_ds_init_buffer_field(op->common.aml_opcode, obj_desc,
681                                               walk_state->operands[0],
682                                               walk_state->operands[1], NULL,
683                                               walk_state->operands[2]);
684         }
685
686         return_ACPI_STATUS(status);
687 }
688
689 /*******************************************************************************
690  *
691  * FUNCTION:    acpi_ds_eval_region_operands
692  *
693  * PARAMETERS:  walk_state      - Current walk
694  *              Op              - A valid region Op object
695  *
696  * RETURN:      Status
697  *
698  * DESCRIPTION: Get region address and length
699  *              Called from acpi_ds_exec_end_op during op_region parse tree walk
700  *
701  ******************************************************************************/
702
703 acpi_status
704 acpi_ds_eval_region_operands(struct acpi_walk_state *walk_state,
705                              union acpi_parse_object *op)
706 {
707         acpi_status status;
708         union acpi_operand_object *obj_desc;
709         union acpi_operand_object *operand_desc;
710         struct acpi_namespace_node *node;
711         union acpi_parse_object *next_op;
712
713         ACPI_FUNCTION_TRACE_PTR(ds_eval_region_operands, op);
714
715         /*
716          * This is where we evaluate the address and length fields of the
717          * op_region declaration
718          */
719         node = op->common.node;
720
721         /* next_op points to the op that holds the space_iD */
722
723         next_op = op->common.value.arg;
724
725         /* next_op points to address op */
726
727         next_op = next_op->common.next;
728
729         /* Evaluate/create the address and length operands */
730
731         status = acpi_ds_create_operands(walk_state, next_op);
732         if (ACPI_FAILURE(status)) {
733                 return_ACPI_STATUS(status);
734         }
735
736         /* Resolve the length and address operands to numbers */
737
738         status = acpi_ex_resolve_operands(op->common.aml_opcode,
739                                           ACPI_WALK_OPERANDS, walk_state);
740         if (ACPI_FAILURE(status)) {
741                 return_ACPI_STATUS(status);
742         }
743
744         ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE,
745                            acpi_ps_get_opcode_name(op->common.aml_opcode),
746                            1, "after AcpiExResolveOperands");
747
748         obj_desc = acpi_ns_get_attached_object(node);
749         if (!obj_desc) {
750                 return_ACPI_STATUS(AE_NOT_EXIST);
751         }
752
753         /*
754          * Get the length operand and save it
755          * (at Top of stack)
756          */
757         operand_desc = walk_state->operands[walk_state->num_operands - 1];
758
759         obj_desc->region.length = (u32) operand_desc->integer.value;
760         acpi_ut_remove_reference(operand_desc);
761
762         /*
763          * Get the address and save it
764          * (at top of stack - 1)
765          */
766         operand_desc = walk_state->operands[walk_state->num_operands - 2];
767
768         obj_desc->region.address = (acpi_physical_address)
769             operand_desc->integer.value;
770         acpi_ut_remove_reference(operand_desc);
771
772         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "RgnObj %p Addr %8.8X%8.8X Len %X\n",
773                           obj_desc,
774                           ACPI_FORMAT_NATIVE_UINT(obj_desc->region.address),
775                           obj_desc->region.length));
776
777         /* Now the address and length are valid for this opregion */
778
779         obj_desc->region.flags |= AOPOBJ_DATA_VALID;
780
781         return_ACPI_STATUS(status);
782 }
783
784 /*******************************************************************************
785  *
786  * FUNCTION:    acpi_ds_eval_table_region_operands
787  *
788  * PARAMETERS:  walk_state      - Current walk
789  *              Op              - A valid region Op object
790  *
791  * RETURN:      Status
792  *
793  * DESCRIPTION: Get region address and length
794  *              Called from acpi_ds_exec_end_op during data_table_region parse tree walk
795  *
796  ******************************************************************************/
797
798 acpi_status
799 acpi_ds_eval_table_region_operands(struct acpi_walk_state *walk_state,
800                                    union acpi_parse_object *op)
801 {
802         acpi_status status;
803         union acpi_operand_object *obj_desc;
804         union acpi_operand_object **operand;
805         struct acpi_namespace_node *node;
806         union acpi_parse_object *next_op;
807         acpi_native_uint table_index;
808         struct acpi_table_header *table;
809
810         ACPI_FUNCTION_TRACE_PTR(ds_eval_table_region_operands, op);
811
812         /*
813          * This is where we evaluate the signature_string and oem_iDString
814          * and oem_table_iDString of the data_table_region declaration
815          */
816         node = op->common.node;
817
818         /* next_op points to signature_string op */
819
820         next_op = op->common.value.arg;
821
822         /*
823          * Evaluate/create the signature_string and oem_iDString
824          * and oem_table_iDString operands
825          */
826         status = acpi_ds_create_operands(walk_state, next_op);
827         if (ACPI_FAILURE(status)) {
828                 return_ACPI_STATUS(status);
829         }
830
831         /*
832          * Resolve the signature_string and oem_iDString
833          * and oem_table_iDString operands
834          */
835         status = acpi_ex_resolve_operands(op->common.aml_opcode,
836                                           ACPI_WALK_OPERANDS, walk_state);
837         if (ACPI_FAILURE(status)) {
838                 return_ACPI_STATUS(status);
839         }
840
841         ACPI_DUMP_OPERANDS(ACPI_WALK_OPERANDS, ACPI_IMODE_EXECUTE,
842                            acpi_ps_get_opcode_name(op->common.aml_opcode),
843                            1, "after AcpiExResolveOperands");
844
845         operand = &walk_state->operands[0];
846
847         /* Find the ACPI table */
848
849         status = acpi_tb_find_table(operand[0]->string.pointer,
850                                     operand[1]->string.pointer,
851                                     operand[2]->string.pointer, &table_index);
852         if (ACPI_FAILURE(status)) {
853                 return_ACPI_STATUS(status);
854         }
855
856         acpi_ut_remove_reference(operand[0]);
857         acpi_ut_remove_reference(operand[1]);
858         acpi_ut_remove_reference(operand[2]);
859
860         status = acpi_get_table_by_index(table_index, &table);
861         if (ACPI_FAILURE(status)) {
862                 return_ACPI_STATUS(status);
863         }
864
865         obj_desc = acpi_ns_get_attached_object(node);
866         if (!obj_desc) {
867                 return_ACPI_STATUS(AE_NOT_EXIST);
868         }
869
870         obj_desc->region.address =
871             (acpi_physical_address) ACPI_TO_INTEGER(table);
872         obj_desc->region.length = table->length;
873
874         ACPI_DEBUG_PRINT((ACPI_DB_EXEC, "RgnObj %p Addr %8.8X%8.8X Len %X\n",
875                           obj_desc,
876                           ACPI_FORMAT_NATIVE_UINT(obj_desc->region.address),
877                           obj_desc->region.length));
878
879         /* Now the address and length are valid for this opregion */
880
881         obj_desc->region.flags |= AOPOBJ_DATA_VALID;
882
883         return_ACPI_STATUS(status);
884 }
885
886 /*******************************************************************************
887  *
888  * FUNCTION:    acpi_ds_eval_data_object_operands
889  *
890  * PARAMETERS:  walk_state      - Current walk
891  *              Op              - A valid data_object Op object
892  *              obj_desc        - data_object
893  *
894  * RETURN:      Status
895  *
896  * DESCRIPTION: Get the operands and complete the following data object types:
897  *              Buffer, Package.
898  *
899  ******************************************************************************/
900
901 acpi_status
902 acpi_ds_eval_data_object_operands(struct acpi_walk_state *walk_state,
903                                   union acpi_parse_object *op,
904                                   union acpi_operand_object *obj_desc)
905 {
906         acpi_status status;
907         union acpi_operand_object *arg_desc;
908         u32 length;
909
910         ACPI_FUNCTION_TRACE(ds_eval_data_object_operands);
911
912         /* The first operand (for all of these data objects) is the length */
913
914         /*
915          * Set proper index into operand stack for acpi_ds_obj_stack_push
916          * invoked inside acpi_ds_create_operand.
917          */
918         walk_state->operand_index = walk_state->num_operands;
919
920         status = acpi_ds_create_operand(walk_state, op->common.value.arg, 1);
921         if (ACPI_FAILURE(status)) {
922                 return_ACPI_STATUS(status);
923         }
924
925         status = acpi_ex_resolve_operands(walk_state->opcode,
926                                           &(walk_state->
927                                             operands[walk_state->num_operands -
928                                                      1]), walk_state);
929         if (ACPI_FAILURE(status)) {
930                 return_ACPI_STATUS(status);
931         }
932
933         /* Extract length operand */
934
935         arg_desc = walk_state->operands[walk_state->num_operands - 1];
936         length = (u32) arg_desc->integer.value;
937
938         /* Cleanup for length operand */
939
940         status = acpi_ds_obj_stack_pop(1, walk_state);
941         if (ACPI_FAILURE(status)) {
942                 return_ACPI_STATUS(status);
943         }
944
945         acpi_ut_remove_reference(arg_desc);
946
947         /*
948          * Create the actual data object
949          */
950         switch (op->common.aml_opcode) {
951         case AML_BUFFER_OP:
952
953                 status =
954                     acpi_ds_build_internal_buffer_obj(walk_state, op, length,
955                                                       &obj_desc);
956                 break;
957
958         case AML_PACKAGE_OP:
959         case AML_VAR_PACKAGE_OP:
960
961                 status =
962                     acpi_ds_build_internal_package_obj(walk_state, op, length,
963                                                        &obj_desc);
964                 break;
965
966         default:
967                 return_ACPI_STATUS(AE_AML_BAD_OPCODE);
968         }
969
970         if (ACPI_SUCCESS(status)) {
971                 /*
972                  * Return the object in the walk_state, unless the parent is a package -
973                  * in this case, the return object will be stored in the parse tree
974                  * for the package.
975                  */
976                 if ((!op->common.parent) ||
977                     ((op->common.parent->common.aml_opcode != AML_PACKAGE_OP) &&
978                      (op->common.parent->common.aml_opcode !=
979                       AML_VAR_PACKAGE_OP)
980                      && (op->common.parent->common.aml_opcode != AML_NAME_OP))) {
981                         walk_state->result_obj = obj_desc;
982                 }
983         }
984
985         return_ACPI_STATUS(status);
986 }
987
988 /*******************************************************************************
989  *
990  * FUNCTION:    acpi_ds_exec_begin_control_op
991  *
992  * PARAMETERS:  walk_list       - The list that owns the walk stack
993  *              Op              - The control Op
994  *
995  * RETURN:      Status
996  *
997  * DESCRIPTION: Handles all control ops encountered during control method
998  *              execution.
999  *
1000  ******************************************************************************/
1001
1002 acpi_status
1003 acpi_ds_exec_begin_control_op(struct acpi_walk_state *walk_state,
1004                               union acpi_parse_object *op)
1005 {
1006         acpi_status status = AE_OK;
1007         union acpi_generic_state *control_state;
1008
1009         ACPI_FUNCTION_NAME(ds_exec_begin_control_op);
1010
1011         ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "Op=%p Opcode=%2.2X State=%p\n", op,
1012                           op->common.aml_opcode, walk_state));
1013
1014         switch (op->common.aml_opcode) {
1015         case AML_IF_OP:
1016         case AML_WHILE_OP:
1017
1018                 /*
1019                  * IF/WHILE: Create a new control state to manage these
1020                  * constructs. We need to manage these as a stack, in order
1021                  * to handle nesting.
1022                  */
1023                 control_state = acpi_ut_create_control_state();
1024                 if (!control_state) {
1025                         status = AE_NO_MEMORY;
1026                         break;
1027                 }
1028                 /*
1029                  * Save a pointer to the predicate for multiple executions
1030                  * of a loop
1031                  */
1032                 control_state->control.aml_predicate_start =
1033                     walk_state->parser_state.aml - 1;
1034                 control_state->control.package_end =
1035                     walk_state->parser_state.pkg_end;
1036                 control_state->control.opcode = op->common.aml_opcode;
1037
1038                 /* Push the control state on this walk's control stack */
1039
1040                 acpi_ut_push_generic_state(&walk_state->control_state,
1041                                            control_state);
1042                 break;
1043
1044         case AML_ELSE_OP:
1045
1046                 /* Predicate is in the state object */
1047                 /* If predicate is true, the IF was executed, ignore ELSE part */
1048
1049                 if (walk_state->last_predicate) {
1050                         status = AE_CTRL_TRUE;
1051                 }
1052
1053                 break;
1054
1055         case AML_RETURN_OP:
1056
1057                 break;
1058
1059         default:
1060                 break;
1061         }
1062
1063         return (status);
1064 }
1065
1066 /*******************************************************************************
1067  *
1068  * FUNCTION:    acpi_ds_exec_end_control_op
1069  *
1070  * PARAMETERS:  walk_list       - The list that owns the walk stack
1071  *              Op              - The control Op
1072  *
1073  * RETURN:      Status
1074  *
1075  * DESCRIPTION: Handles all control ops encountered during control method
1076  *              execution.
1077  *
1078  ******************************************************************************/
1079
1080 acpi_status
1081 acpi_ds_exec_end_control_op(struct acpi_walk_state * walk_state,
1082                             union acpi_parse_object * op)
1083 {
1084         acpi_status status = AE_OK;
1085         union acpi_generic_state *control_state;
1086
1087         ACPI_FUNCTION_NAME(ds_exec_end_control_op);
1088
1089         switch (op->common.aml_opcode) {
1090         case AML_IF_OP:
1091
1092                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[IF_OP] Op=%p\n", op));
1093
1094                 /*
1095                  * Save the result of the predicate in case there is an
1096                  * ELSE to come
1097                  */
1098                 walk_state->last_predicate =
1099                     (u8) walk_state->control_state->common.value;
1100
1101                 /*
1102                  * Pop the control state that was created at the start
1103                  * of the IF and free it
1104                  */
1105                 control_state =
1106                     acpi_ut_pop_generic_state(&walk_state->control_state);
1107                 acpi_ut_delete_generic_state(control_state);
1108                 break;
1109
1110         case AML_ELSE_OP:
1111
1112                 break;
1113
1114         case AML_WHILE_OP:
1115
1116                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH, "[WHILE_OP] Op=%p\n", op));
1117
1118                 if (walk_state->control_state->common.value) {
1119
1120                         /* Predicate was true, go back and evaluate it again! */
1121
1122                         status = AE_CTRL_PENDING;
1123                 }
1124
1125                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
1126                                   "[WHILE_OP] termination! Op=%p\n", op));
1127
1128                 /* Pop this control state and free it */
1129
1130                 control_state =
1131                     acpi_ut_pop_generic_state(&walk_state->control_state);
1132
1133                 walk_state->aml_last_while =
1134                     control_state->control.aml_predicate_start;
1135                 acpi_ut_delete_generic_state(control_state);
1136                 break;
1137
1138         case AML_RETURN_OP:
1139
1140                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
1141                                   "[RETURN_OP] Op=%p Arg=%p\n", op,
1142                                   op->common.value.arg));
1143
1144                 /*
1145                  * One optional operand -- the return value
1146                  * It can be either an immediate operand or a result that
1147                  * has been bubbled up the tree
1148                  */
1149                 if (op->common.value.arg) {
1150
1151                         /* Since we have a real Return(), delete any implicit return */
1152
1153                         acpi_ds_clear_implicit_return(walk_state);
1154
1155                         /* Return statement has an immediate operand */
1156
1157                         status =
1158                             acpi_ds_create_operands(walk_state,
1159                                                     op->common.value.arg);
1160                         if (ACPI_FAILURE(status)) {
1161                                 return (status);
1162                         }
1163
1164                         /*
1165                          * If value being returned is a Reference (such as
1166                          * an arg or local), resolve it now because it may
1167                          * cease to exist at the end of the method.
1168                          */
1169                         status =
1170                             acpi_ex_resolve_to_value(&walk_state->operands[0],
1171                                                      walk_state);
1172                         if (ACPI_FAILURE(status)) {
1173                                 return (status);
1174                         }
1175
1176                         /*
1177                          * Get the return value and save as the last result
1178                          * value.  This is the only place where walk_state->return_desc
1179                          * is set to anything other than zero!
1180                          */
1181                         walk_state->return_desc = walk_state->operands[0];
1182                 } else if (walk_state->result_count) {
1183
1184                         /* Since we have a real Return(), delete any implicit return */
1185
1186                         acpi_ds_clear_implicit_return(walk_state);
1187
1188                         /*
1189                          * The return value has come from a previous calculation.
1190                          *
1191                          * If value being returned is a Reference (such as
1192                          * an arg or local), resolve it now because it may
1193                          * cease to exist at the end of the method.
1194                          *
1195                          * Allow references created by the Index operator to return unchanged.
1196                          */
1197                         if ((ACPI_GET_DESCRIPTOR_TYPE
1198                              (walk_state->results->results.obj_desc[0]) ==
1199                              ACPI_DESC_TYPE_OPERAND)
1200                             &&
1201                             (ACPI_GET_OBJECT_TYPE
1202                              (walk_state->results->results.obj_desc[0]) ==
1203                              ACPI_TYPE_LOCAL_REFERENCE)
1204                             && ((walk_state->results->results.obj_desc[0])->
1205                                 reference.opcode != AML_INDEX_OP)) {
1206                                 status =
1207                                     acpi_ex_resolve_to_value(&walk_state->
1208                                                              results->results.
1209                                                              obj_desc[0],
1210                                                              walk_state);
1211                                 if (ACPI_FAILURE(status)) {
1212                                         return (status);
1213                                 }
1214                         }
1215
1216                         walk_state->return_desc =
1217                             walk_state->results->results.obj_desc[0];
1218                 } else {
1219                         /* No return operand */
1220
1221                         if (walk_state->num_operands) {
1222                                 acpi_ut_remove_reference(walk_state->
1223                                                          operands[0]);
1224                         }
1225
1226                         walk_state->operands[0] = NULL;
1227                         walk_state->num_operands = 0;
1228                         walk_state->return_desc = NULL;
1229                 }
1230
1231                 ACPI_DEBUG_PRINT((ACPI_DB_DISPATCH,
1232                                   "Completed RETURN_OP State=%p, RetVal=%p\n",
1233                                   walk_state, walk_state->return_desc));
1234
1235                 /* End the control method execution right now */
1236
1237                 status = AE_CTRL_TERMINATE;
1238                 break;
1239
1240         case AML_NOOP_OP:
1241
1242                 /* Just do nothing! */
1243                 break;
1244
1245         case AML_BREAK_POINT_OP:
1246
1247                 /* Call up to the OS service layer to handle this */
1248
1249                 status =
1250                     acpi_os_signal(ACPI_SIGNAL_BREAKPOINT,
1251                                    "Executed AML Breakpoint opcode");
1252
1253                 /* If and when it returns, all done. */
1254
1255                 break;
1256
1257         case AML_BREAK_OP:
1258         case AML_CONTINUE_OP:   /* ACPI 2.0 */
1259
1260                 /* Pop and delete control states until we find a while */
1261
1262                 while (walk_state->control_state &&
1263                        (walk_state->control_state->control.opcode !=
1264                         AML_WHILE_OP)) {
1265                         control_state =
1266                             acpi_ut_pop_generic_state(&walk_state->
1267                                                       control_state);
1268                         acpi_ut_delete_generic_state(control_state);
1269                 }
1270
1271                 /* No while found? */
1272
1273                 if (!walk_state->control_state) {
1274                         return (AE_AML_NO_WHILE);
1275                 }
1276
1277                 /* Was: walk_state->aml_last_while = walk_state->control_state->Control.aml_predicate_start; */
1278
1279                 walk_state->aml_last_while =
1280                     walk_state->control_state->control.package_end;
1281
1282                 /* Return status depending on opcode */
1283
1284                 if (op->common.aml_opcode == AML_BREAK_OP) {
1285                         status = AE_CTRL_BREAK;
1286                 } else {
1287                         status = AE_CTRL_CONTINUE;
1288                 }
1289                 break;
1290
1291         default:
1292
1293                 ACPI_ERROR((AE_INFO, "Unknown control opcode=%X Op=%p",
1294                             op->common.aml_opcode, op));
1295
1296                 status = AE_AML_BAD_OPCODE;
1297                 break;
1298         }
1299
1300         return (status);
1301 }