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