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