Linux-2.6.12-rc2
[linux-flexiantxendom0-natty.git] / drivers / acpi / executer / exmisc.c
1
2 /******************************************************************************
3  *
4  * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5  *
6  *****************************************************************************/
7
8 /*
9  * Copyright (C) 2000 - 2005, 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
46 #include <acpi/acpi.h>
47 #include <acpi/acinterp.h>
48 #include <acpi/amlcode.h>
49
50
51 #define _COMPONENT          ACPI_EXECUTER
52          ACPI_MODULE_NAME    ("exmisc")
53
54
55 /*******************************************************************************
56  *
57  * FUNCTION:    acpi_ex_get_object_reference
58  *
59  * PARAMETERS:  obj_desc            - Create a reference to this object
60  *              return_desc         - Where to store the reference
61  *              walk_state          - Current state
62  *
63  * RETURN:      Status
64  *
65  * DESCRIPTION: Obtain and return a "reference" to the target object
66  *              Common code for the ref_of_op and the cond_ref_of_op.
67  *
68  ******************************************************************************/
69
70 acpi_status
71 acpi_ex_get_object_reference (
72         union acpi_operand_object       *obj_desc,
73         union acpi_operand_object       **return_desc,
74         struct acpi_walk_state          *walk_state)
75 {
76         union acpi_operand_object       *reference_obj;
77         union acpi_operand_object       *referenced_obj;
78
79
80         ACPI_FUNCTION_TRACE_PTR ("ex_get_object_reference", obj_desc);
81
82
83         *return_desc = NULL;
84
85         switch (ACPI_GET_DESCRIPTOR_TYPE (obj_desc)) {
86         case ACPI_DESC_TYPE_OPERAND:
87
88                 if (ACPI_GET_OBJECT_TYPE (obj_desc) != ACPI_TYPE_LOCAL_REFERENCE) {
89                         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
90                 }
91
92                 /*
93                  * Must be a reference to a Local or Arg
94                  */
95                 switch (obj_desc->reference.opcode) {
96                 case AML_LOCAL_OP:
97                 case AML_ARG_OP:
98                 case AML_DEBUG_OP:
99
100                         /* The referenced object is the pseudo-node for the local/arg */
101
102                         referenced_obj = obj_desc->reference.object;
103                         break;
104
105                 default:
106
107                         ACPI_REPORT_ERROR (("Unknown Reference opcode in get_reference %X\n",
108                                 obj_desc->reference.opcode));
109                         return_ACPI_STATUS (AE_AML_INTERNAL);
110                 }
111                 break;
112
113
114         case ACPI_DESC_TYPE_NAMED:
115
116                 /*
117                  * A named reference that has already been resolved to a Node
118                  */
119                 referenced_obj = obj_desc;
120                 break;
121
122
123         default:
124
125                 ACPI_REPORT_ERROR (("Invalid descriptor type in get_reference: %X\n",
126                                 ACPI_GET_DESCRIPTOR_TYPE (obj_desc)));
127                 return_ACPI_STATUS (AE_TYPE);
128         }
129
130
131         /* Create a new reference object */
132
133         reference_obj = acpi_ut_create_internal_object (ACPI_TYPE_LOCAL_REFERENCE);
134         if (!reference_obj) {
135                 return_ACPI_STATUS (AE_NO_MEMORY);
136         }
137
138         reference_obj->reference.opcode = AML_REF_OF_OP;
139         reference_obj->reference.object = referenced_obj;
140         *return_desc = reference_obj;
141
142         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
143                         obj_desc, acpi_ut_get_object_type_name (obj_desc), *return_desc));
144
145         return_ACPI_STATUS (AE_OK);
146 }
147
148
149 /*******************************************************************************
150  *
151  * FUNCTION:    acpi_ex_concat_template
152  *
153  * PARAMETERS:  Operand0            - First source object
154  *              Operand1            - Second source object
155  *              actual_return_desc  - Where to place the return object
156  *              walk_state          - Current walk state
157  *
158  * RETURN:      Status
159  *
160  * DESCRIPTION: Concatenate two resource templates
161  *
162  ******************************************************************************/
163
164 acpi_status
165 acpi_ex_concat_template (
166         union acpi_operand_object       *operand0,
167         union acpi_operand_object       *operand1,
168         union acpi_operand_object       **actual_return_desc,
169         struct acpi_walk_state          *walk_state)
170 {
171         union acpi_operand_object       *return_desc;
172         u8                              *new_buf;
173         u8                              *end_tag1;
174         u8                              *end_tag2;
175         acpi_size                       length1;
176         acpi_size                       length2;
177
178
179         ACPI_FUNCTION_TRACE ("ex_concat_template");
180
181
182         /* Find the end_tags in each resource template */
183
184         end_tag1 = acpi_ut_get_resource_end_tag (operand0);
185         end_tag2 = acpi_ut_get_resource_end_tag (operand1);
186         if (!end_tag1 || !end_tag2) {
187                 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
188         }
189
190         /* Compute the length of each part */
191
192         length1 = ACPI_PTR_DIFF (end_tag1, operand0->buffer.pointer);
193         length2 = ACPI_PTR_DIFF (end_tag2, operand1->buffer.pointer) +
194                           2; /* Size of END_TAG */
195
196         /* Create a new buffer object for the result */
197
198         return_desc = acpi_ut_create_buffer_object (length1 + length2);
199         if (!return_desc) {
200                 return_ACPI_STATUS (AE_NO_MEMORY);
201         }
202
203         /* Copy the templates to the new descriptor */
204
205         new_buf = return_desc->buffer.pointer;
206         ACPI_MEMCPY (new_buf, operand0->buffer.pointer, length1);
207         ACPI_MEMCPY (new_buf + length1, operand1->buffer.pointer, length2);
208
209         /* Compute the new checksum */
210
211         new_buf[return_desc->buffer.length - 1] =
212                         acpi_ut_generate_checksum (return_desc->buffer.pointer,
213                                            (return_desc->buffer.length - 1));
214
215         /* Return the completed template descriptor */
216
217         *actual_return_desc = return_desc;
218         return_ACPI_STATUS (AE_OK);
219 }
220
221
222 /*******************************************************************************
223  *
224  * FUNCTION:    acpi_ex_do_concatenate
225  *
226  * PARAMETERS:  Operand0            - First source object
227  *              Operand1            - Second source object
228  *              actual_return_desc  - Where to place the return object
229  *              walk_state          - Current walk state
230  *
231  * RETURN:      Status
232  *
233  * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
234  *
235  ******************************************************************************/
236
237 acpi_status
238 acpi_ex_do_concatenate (
239         union acpi_operand_object       *operand0,
240         union acpi_operand_object       *operand1,
241         union acpi_operand_object       **actual_return_desc,
242         struct acpi_walk_state          *walk_state)
243 {
244         union acpi_operand_object       *local_operand1 = operand1;
245         union acpi_operand_object       *return_desc;
246         char                            *new_buf;
247         acpi_status                     status;
248         acpi_size                       new_length;
249
250
251         ACPI_FUNCTION_TRACE ("ex_do_concatenate");
252
253
254         /*
255          * Convert the second operand if necessary.  The first operand
256          * determines the type of the second operand, (See the Data Types
257          * section of the ACPI specification.)  Both object types are
258          * guaranteed to be either Integer/String/Buffer by the operand
259          * resolution mechanism.
260          */
261         switch (ACPI_GET_OBJECT_TYPE (operand0)) {
262         case ACPI_TYPE_INTEGER:
263                 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
264                 break;
265
266         case ACPI_TYPE_STRING:
267                 status = acpi_ex_convert_to_string (operand1, &local_operand1,
268                                  ACPI_IMPLICIT_CONVERT_HEX);
269                 break;
270
271         case ACPI_TYPE_BUFFER:
272                 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
273                 break;
274
275         default:
276                 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
277                                 ACPI_GET_OBJECT_TYPE (operand0)));
278                 status = AE_AML_INTERNAL;
279         }
280
281         if (ACPI_FAILURE (status)) {
282                 goto cleanup;
283         }
284
285         /*
286          * Both operands are now known to be the same object type
287          * (Both are Integer, String, or Buffer), and we can now perform the
288          * concatenation.
289          */
290
291         /*
292          * There are three cases to handle:
293          *
294          * 1) Two Integers concatenated to produce a new Buffer
295          * 2) Two Strings concatenated to produce a new String
296          * 3) Two Buffers concatenated to produce a new Buffer
297          */
298         switch (ACPI_GET_OBJECT_TYPE (operand0)) {
299         case ACPI_TYPE_INTEGER:
300
301                 /* Result of two Integers is a Buffer */
302                 /* Need enough buffer space for two integers */
303
304                 return_desc = acpi_ut_create_buffer_object (
305                                    ACPI_MUL_2 (acpi_gbl_integer_byte_width));
306                 if (!return_desc) {
307                         status = AE_NO_MEMORY;
308                         goto cleanup;
309                 }
310
311                 new_buf = (char *) return_desc->buffer.pointer;
312
313                 /* Copy the first integer, LSB first */
314
315                 ACPI_MEMCPY (new_buf,
316                                   &operand0->integer.value,
317                                   acpi_gbl_integer_byte_width);
318
319                 /* Copy the second integer (LSB first) after the first */
320
321                 ACPI_MEMCPY (new_buf + acpi_gbl_integer_byte_width,
322                                   &local_operand1->integer.value,
323                                   acpi_gbl_integer_byte_width);
324                 break;
325
326         case ACPI_TYPE_STRING:
327
328                 /* Result of two Strings is a String */
329
330                 new_length = (acpi_size) operand0->string.length +
331                                  (acpi_size) local_operand1->string.length;
332                 if (new_length > ACPI_MAX_STRING_CONVERSION) {
333                         status = AE_AML_STRING_LIMIT;
334                         goto cleanup;
335                 }
336
337                 return_desc = acpi_ut_create_string_object (new_length);
338                 if (!return_desc) {
339                         status = AE_NO_MEMORY;
340                         goto cleanup;
341                 }
342
343                 new_buf = return_desc->string.pointer;
344
345                 /* Concatenate the strings */
346
347                 ACPI_STRCPY (new_buf,
348                                   operand0->string.pointer);
349                 ACPI_STRCPY (new_buf + operand0->string.length,
350                                   local_operand1->string.pointer);
351                 break;
352
353         case ACPI_TYPE_BUFFER:
354
355                 /* Result of two Buffers is a Buffer */
356
357                 return_desc = acpi_ut_create_buffer_object (
358                                    (acpi_size) operand0->buffer.length +
359                                    (acpi_size) local_operand1->buffer.length);
360                 if (!return_desc) {
361                         status = AE_NO_MEMORY;
362                         goto cleanup;
363                 }
364
365                 new_buf = (char *) return_desc->buffer.pointer;
366
367                 /* Concatenate the buffers */
368
369                 ACPI_MEMCPY (new_buf,
370                                   operand0->buffer.pointer,
371                                   operand0->buffer.length);
372                 ACPI_MEMCPY (new_buf + operand0->buffer.length,
373                                   local_operand1->buffer.pointer,
374                                   local_operand1->buffer.length);
375                 break;
376
377         default:
378
379                 /* Invalid object type, should not happen here */
380
381                 ACPI_REPORT_ERROR (("Concatenate - Invalid object type: %X\n",
382                                 ACPI_GET_OBJECT_TYPE (operand0)));
383                 status =AE_AML_INTERNAL;
384                 goto cleanup;
385         }
386
387         *actual_return_desc = return_desc;
388
389 cleanup:
390         if (local_operand1 != operand1) {
391                 acpi_ut_remove_reference (local_operand1);
392         }
393         return_ACPI_STATUS (status);
394 }
395
396
397 /*******************************************************************************
398  *
399  * FUNCTION:    acpi_ex_do_math_op
400  *
401  * PARAMETERS:  Opcode              - AML opcode
402  *              Integer0            - Integer operand #0
403  *              Integer1            - Integer operand #1
404  *
405  * RETURN:      Integer result of the operation
406  *
407  * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
408  *              math functions here is to prevent a lot of pointer dereferencing
409  *              to obtain the operands.
410  *
411  ******************************************************************************/
412
413 acpi_integer
414 acpi_ex_do_math_op (
415         u16                             opcode,
416         acpi_integer                    integer0,
417         acpi_integer                    integer1)
418 {
419
420         ACPI_FUNCTION_ENTRY ();
421
422
423         switch (opcode) {
424         case AML_ADD_OP:                /* Add (Integer0, Integer1, Result) */
425
426                 return (integer0 + integer1);
427
428
429         case AML_BIT_AND_OP:            /* And (Integer0, Integer1, Result) */
430
431                 return (integer0 & integer1);
432
433
434         case AML_BIT_NAND_OP:           /* NAnd (Integer0, Integer1, Result) */
435
436                 return (~(integer0 & integer1));
437
438
439         case AML_BIT_OR_OP:             /* Or (Integer0, Integer1, Result) */
440
441                 return (integer0 | integer1);
442
443
444         case AML_BIT_NOR_OP:            /* NOr (Integer0, Integer1, Result) */
445
446                 return (~(integer0 | integer1));
447
448
449         case AML_BIT_XOR_OP:            /* XOr (Integer0, Integer1, Result) */
450
451                 return (integer0 ^ integer1);
452
453
454         case AML_MULTIPLY_OP:           /* Multiply (Integer0, Integer1, Result) */
455
456                 return (integer0 * integer1);
457
458
459         case AML_SHIFT_LEFT_OP:         /* shift_left (Operand, shift_count, Result) */
460
461                 return (integer0 << integer1);
462
463
464         case AML_SHIFT_RIGHT_OP:        /* shift_right (Operand, shift_count, Result) */
465
466                 return (integer0 >> integer1);
467
468
469         case AML_SUBTRACT_OP:           /* Subtract (Integer0, Integer1, Result) */
470
471                 return (integer0 - integer1);
472
473         default:
474
475                 return (0);
476         }
477 }
478
479
480 /*******************************************************************************
481  *
482  * FUNCTION:    acpi_ex_do_logical_numeric_op
483  *
484  * PARAMETERS:  Opcode              - AML opcode
485  *              Integer0            - Integer operand #0
486  *              Integer1            - Integer operand #1
487  *              logical_result      - TRUE/FALSE result of the operation
488  *
489  * RETURN:      Status
490  *
491  * DESCRIPTION: Execute a logical "Numeric" AML opcode. For these Numeric
492  *              operators (LAnd and LOr), both operands must be integers.
493  *
494  *              Note: cleanest machine code seems to be produced by the code
495  *              below, rather than using statements of the form:
496  *                  Result = (Integer0 && Integer1);
497  *
498  ******************************************************************************/
499
500 acpi_status
501 acpi_ex_do_logical_numeric_op (
502         u16                             opcode,
503         acpi_integer                    integer0,
504         acpi_integer                    integer1,
505         u8                              *logical_result)
506 {
507         acpi_status                     status = AE_OK;
508         u8                              local_result = FALSE;
509
510
511         ACPI_FUNCTION_TRACE ("ex_do_logical_numeric_op");
512
513
514         switch (opcode) {
515         case AML_LAND_OP:               /* LAnd (Integer0, Integer1) */
516
517                 if (integer0 && integer1) {
518                         local_result = TRUE;
519                 }
520                 break;
521
522         case AML_LOR_OP:                /* LOr (Integer0, Integer1) */
523
524                 if (integer0 || integer1) {
525                         local_result = TRUE;
526                 }
527                 break;
528
529         default:
530                 status = AE_AML_INTERNAL;
531                 break;
532         }
533
534         /* Return the logical result and status */
535
536         *logical_result = local_result;
537         return_ACPI_STATUS (status);
538 }
539
540
541 /*******************************************************************************
542  *
543  * FUNCTION:    acpi_ex_do_logical_op
544  *
545  * PARAMETERS:  Opcode              - AML opcode
546  *              Operand0            - operand #0
547  *              Operand1            - operand #1
548  *              logical_result      - TRUE/FALSE result of the operation
549  *
550  * RETURN:      Status
551  *
552  * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
553  *              functions here is to prevent a lot of pointer dereferencing
554  *              to obtain the operands and to simplify the generation of the
555  *              logical value. For the Numeric operators (LAnd and LOr), both
556  *              operands must be integers. For the other logical operators,
557  *              operands can be any combination of Integer/String/Buffer. The
558  *              first operand determines the type to which the second operand
559  *              will be converted.
560  *
561  *              Note: cleanest machine code seems to be produced by the code
562  *              below, rather than using statements of the form:
563  *                  Result = (Operand0 == Operand1);
564  *
565  ******************************************************************************/
566
567 acpi_status
568 acpi_ex_do_logical_op (
569         u16                             opcode,
570         union acpi_operand_object       *operand0,
571         union acpi_operand_object       *operand1,
572         u8                              *logical_result)
573 {
574         union acpi_operand_object       *local_operand1 = operand1;
575         acpi_integer                    integer0;
576         acpi_integer                    integer1;
577         u32                             length0;
578         u32                             length1;
579         acpi_status                     status = AE_OK;
580         u8                              local_result = FALSE;
581         int                             compare;
582
583
584         ACPI_FUNCTION_TRACE ("ex_do_logical_op");
585
586
587         /*
588          * Convert the second operand if necessary.  The first operand
589          * determines the type of the second operand, (See the Data Types
590          * section of the ACPI 3.0+ specification.)  Both object types are
591          * guaranteed to be either Integer/String/Buffer by the operand
592          * resolution mechanism.
593          */
594         switch (ACPI_GET_OBJECT_TYPE (operand0)) {
595         case ACPI_TYPE_INTEGER:
596                 status = acpi_ex_convert_to_integer (operand1, &local_operand1, 16);
597                 break;
598
599         case ACPI_TYPE_STRING:
600                 status = acpi_ex_convert_to_string (operand1, &local_operand1,
601                                  ACPI_IMPLICIT_CONVERT_HEX);
602                 break;
603
604         case ACPI_TYPE_BUFFER:
605                 status = acpi_ex_convert_to_buffer (operand1, &local_operand1);
606                 break;
607
608         default:
609                 status = AE_AML_INTERNAL;
610                 break;
611         }
612
613         if (ACPI_FAILURE (status)) {
614                 goto cleanup;
615         }
616
617         /*
618          * Two cases: 1) Both Integers, 2) Both Strings or Buffers
619          */
620         if (ACPI_GET_OBJECT_TYPE (operand0) == ACPI_TYPE_INTEGER) {
621                 /*
622                  * 1) Both operands are of type integer
623                  *    Note: local_operand1 may have changed above
624                  */
625                 integer0 = operand0->integer.value;
626                 integer1 = local_operand1->integer.value;
627
628                 switch (opcode) {
629                 case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
630
631                         if (integer0 == integer1) {
632                                 local_result = TRUE;
633                         }
634                         break;
635
636                 case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
637
638                         if (integer0 > integer1) {
639                                 local_result = TRUE;
640                         }
641                         break;
642
643                 case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
644
645                         if (integer0 < integer1) {
646                                 local_result = TRUE;
647                         }
648                         break;
649
650                 default:
651                         status = AE_AML_INTERNAL;
652                         break;
653                 }
654         }
655         else {
656                 /*
657                  * 2) Both operands are Strings or both are Buffers
658                  *    Note: Code below takes advantage of common Buffer/String
659                  *          object fields. local_operand1 may have changed above. Use
660                  *          memcmp to handle nulls in buffers.
661                  */
662                 length0 = operand0->buffer.length;
663                 length1 = local_operand1->buffer.length;
664
665                 /* Lexicographic compare: compare the data bytes */
666
667                 compare = ACPI_MEMCMP ((const char * ) operand0->buffer.pointer,
668                                  (const char * ) local_operand1->buffer.pointer,
669                                  (length0 > length1) ? length1 : length0);
670
671                 switch (opcode) {
672                 case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
673
674                         /* Length and all bytes must be equal */
675
676                         if ((length0 == length1) &&
677                                 (compare == 0)) {
678                                 /* Length and all bytes match ==> TRUE */
679
680                                 local_result = TRUE;
681                         }
682                         break;
683
684                 case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
685
686                         if (compare > 0) {
687                                 local_result = TRUE;
688                                 goto cleanup;   /* TRUE */
689                         }
690                         if (compare < 0) {
691                                 goto cleanup;   /* FALSE */
692                         }
693
694                         /* Bytes match (to shortest length), compare lengths */
695
696                         if (length0 > length1) {
697                                 local_result = TRUE;
698                         }
699                         break;
700
701                 case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
702
703                         if (compare > 0) {
704                                 goto cleanup;   /* FALSE */
705                         }
706                         if (compare < 0) {
707                                 local_result = TRUE;
708                                 goto cleanup;   /* TRUE */
709                         }
710
711                         /* Bytes match (to shortest length), compare lengths */
712
713                         if (length0 < length1) {
714                                 local_result = TRUE;
715                         }
716                         break;
717
718                 default:
719                         status = AE_AML_INTERNAL;
720                         break;
721                 }
722         }
723
724 cleanup:
725
726         /* New object was created if implicit conversion performed - delete */
727
728         if (local_operand1 != operand1) {
729                 acpi_ut_remove_reference (local_operand1);
730         }
731
732         /* Return the logical result and status */
733
734         *logical_result = local_result;
735         return_ACPI_STATUS (status);
736 }
737
738