vserver 1.9.3
[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 - 2004, 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:  *obj_desc           - Object to be converted.  Must be an
153  *                                    Integer, Buffer, or String
154  *              walk_state          - Current walk state
155  *
156  * RETURN:      Status
157  *
158  * DESCRIPTION: Concatenate two resource templates
159  *
160  ******************************************************************************/
161
162 acpi_status
163 acpi_ex_concat_template (
164         union acpi_operand_object       *obj_desc1,
165         union acpi_operand_object       *obj_desc2,
166         union acpi_operand_object       **actual_return_desc,
167         struct acpi_walk_state          *walk_state)
168 {
169         union acpi_operand_object       *return_desc;
170         u8                              *new_buf;
171         u8                              *end_tag1;
172         u8                              *end_tag2;
173         acpi_size                       length1;
174         acpi_size                       length2;
175
176
177         ACPI_FUNCTION_TRACE ("ex_concat_template");
178
179
180         /* Find the end_tags in each resource template */
181
182         end_tag1 = acpi_ut_get_resource_end_tag (obj_desc1);
183         end_tag2 = acpi_ut_get_resource_end_tag (obj_desc2);
184         if (!end_tag1 || !end_tag2) {
185                 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
186         }
187
188         /* Compute the length of each part */
189
190         length1 = ACPI_PTR_DIFF (end_tag1, obj_desc1->buffer.pointer);
191         length2 = ACPI_PTR_DIFF (end_tag2, obj_desc2->buffer.pointer) +
192                           2; /* Size of END_TAG */
193
194         /* Create a new buffer object for the result */
195
196         return_desc = acpi_ut_create_buffer_object (length1 + length2);
197         if (!return_desc) {
198                 return_ACPI_STATUS (AE_NO_MEMORY);
199         }
200
201         /* Copy the templates to the new descriptor */
202
203         new_buf = return_desc->buffer.pointer;
204         ACPI_MEMCPY (new_buf, obj_desc1->buffer.pointer, length1);
205         ACPI_MEMCPY (new_buf + length1, obj_desc2->buffer.pointer, length2);
206
207         /* Compute the new checksum */
208
209         new_buf[return_desc->buffer.length - 1] =
210                         acpi_ut_generate_checksum (return_desc->buffer.pointer,
211                                            (return_desc->buffer.length - 1));
212
213         /* Return the completed template descriptor */
214
215         *actual_return_desc = return_desc;
216         return_ACPI_STATUS (AE_OK);
217 }
218
219
220 /*******************************************************************************
221  *
222  * FUNCTION:    acpi_ex_do_concatenate
223  *
224  * PARAMETERS:  obj_desc1           - First source object
225  *              obj_desc2           - Second source object
226  *              actual_return_desc  - Where to place the return object
227  *              walk_state          - Current walk state
228  *
229  * RETURN:      Status
230  *
231  * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
232  *
233  ******************************************************************************/
234
235 acpi_status
236 acpi_ex_do_concatenate (
237         union acpi_operand_object       *obj_desc1,
238         union acpi_operand_object       *obj_desc2,
239         union acpi_operand_object       **actual_return_desc,
240         struct acpi_walk_state          *walk_state)
241 {
242         acpi_status                     status;
243         u32                             i;
244         acpi_integer                    this_integer;
245         union acpi_operand_object       *return_desc;
246         char                            *new_buf;
247
248
249         ACPI_FUNCTION_ENTRY ();
250
251
252         /*
253          * There are three cases to handle:
254          *
255          * 1) Two Integers concatenated to produce a new Buffer
256          * 2) Two Strings concatenated to produce a new String
257          * 3) Two Buffers concatenated to produce a new Buffer
258          */
259         switch (ACPI_GET_OBJECT_TYPE (obj_desc1)) {
260         case ACPI_TYPE_INTEGER:
261
262                 /* Result of two Integers is a Buffer */
263                 /* Need enough buffer space for two integers */
264
265                 return_desc = acpi_ut_create_buffer_object (acpi_gbl_integer_byte_width * 2);
266                 if (!return_desc) {
267                         return (AE_NO_MEMORY);
268                 }
269
270                 new_buf = (char *) return_desc->buffer.pointer;
271
272                 /* Convert the first integer */
273
274                 this_integer = obj_desc1->integer.value;
275                 for (i = 0; i < acpi_gbl_integer_byte_width; i++) {
276                         new_buf[i] = (char) this_integer;
277                         this_integer >>= 8;
278                 }
279
280                 /* Convert the second integer */
281
282                 this_integer = obj_desc2->integer.value;
283                 for (; i < (ACPI_MUL_2 (acpi_gbl_integer_byte_width)); i++) {
284                         new_buf[i] = (char) this_integer;
285                         this_integer >>= 8;
286                 }
287
288                 break;
289
290
291         case ACPI_TYPE_STRING:
292
293                 /* Result of two Strings is a String */
294
295                 return_desc = acpi_ut_create_internal_object (ACPI_TYPE_STRING);
296                 if (!return_desc) {
297                         return (AE_NO_MEMORY);
298                 }
299
300                 /* Operand0 is string  */
301
302                 new_buf = ACPI_MEM_CALLOCATE ((acpi_size) obj_desc1->string.length +
303                                    (acpi_size) obj_desc2->string.length + 1);
304                 if (!new_buf) {
305                         ACPI_REPORT_ERROR
306                                 (("ex_do_concatenate: String allocation failure\n"));
307                         status = AE_NO_MEMORY;
308                         goto cleanup;
309                 }
310
311                 /* Concatenate the strings */
312
313                 ACPI_STRCPY (new_buf, obj_desc1->string.pointer);
314                 ACPI_STRCPY (new_buf + obj_desc1->string.length,
315                                   obj_desc2->string.pointer);
316
317                 /* Complete the String object initialization */
318
319                 return_desc->string.pointer = new_buf;
320                 return_desc->string.length = obj_desc1->string.length +
321                                    obj_desc2->string.length;
322                 break;
323
324
325         case ACPI_TYPE_BUFFER:
326
327                 /* Result of two Buffers is a Buffer */
328
329                 return_desc = acpi_ut_create_buffer_object (
330                                    (acpi_size) obj_desc1->buffer.length +
331                                    (acpi_size) obj_desc2->buffer.length);
332                 if (!return_desc) {
333                         return (AE_NO_MEMORY);
334                 }
335
336                 new_buf = (char *) return_desc->buffer.pointer;
337
338                 /* Concatenate the buffers */
339
340                 ACPI_MEMCPY (new_buf, obj_desc1->buffer.pointer,
341                                   obj_desc1->buffer.length);
342                 ACPI_MEMCPY (new_buf + obj_desc1->buffer.length, obj_desc2->buffer.pointer,
343                                    obj_desc2->buffer.length);
344
345                 break;
346
347
348         default:
349
350                 /* Invalid object type, should not happen here */
351
352                 ACPI_REPORT_ERROR (("Concat - invalid obj type: %X\n",
353                                 ACPI_GET_OBJECT_TYPE (obj_desc1)));
354                 status = AE_AML_INTERNAL;
355                 return_desc = NULL;
356         }
357
358         *actual_return_desc = return_desc;
359         return (AE_OK);
360
361
362 cleanup:
363
364         acpi_ut_remove_reference (return_desc);
365         return (status);
366 }
367
368
369 /*******************************************************************************
370  *
371  * FUNCTION:    acpi_ex_do_math_op
372  *
373  * PARAMETERS:  Opcode              - AML opcode
374  *              Operand0            - Integer operand #0
375  *              Operand1            - Integer operand #1
376  *
377  * RETURN:      Integer result of the operation
378  *
379  * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
380  *              math functions here is to prevent a lot of pointer dereferencing
381  *              to obtain the operands.
382  *
383  ******************************************************************************/
384
385 acpi_integer
386 acpi_ex_do_math_op (
387         u16                             opcode,
388         acpi_integer                    operand0,
389         acpi_integer                    operand1)
390 {
391
392         ACPI_FUNCTION_ENTRY ();
393
394
395         switch (opcode) {
396         case AML_ADD_OP:                /* Add (Operand0, Operand1, Result) */
397
398                 return (operand0 + operand1);
399
400
401         case AML_BIT_AND_OP:            /* And (Operand0, Operand1, Result) */
402
403                 return (operand0 & operand1);
404
405
406         case AML_BIT_NAND_OP:           /* NAnd (Operand0, Operand1, Result) */
407
408                 return (~(operand0 & operand1));
409
410
411         case AML_BIT_OR_OP:             /* Or (Operand0, Operand1, Result) */
412
413                 return (operand0 | operand1);
414
415
416         case AML_BIT_NOR_OP:            /* NOr (Operand0, Operand1, Result) */
417
418                 return (~(operand0 | operand1));
419
420
421         case AML_BIT_XOR_OP:            /* XOr (Operand0, Operand1, Result) */
422
423                 return (operand0 ^ operand1);
424
425
426         case AML_MULTIPLY_OP:           /* Multiply (Operand0, Operand1, Result) */
427
428                 return (operand0 * operand1);
429
430
431         case AML_SHIFT_LEFT_OP:         /* shift_left (Operand, shift_count, Result) */
432
433                 return (operand0 << operand1);
434
435
436         case AML_SHIFT_RIGHT_OP:        /* shift_right (Operand, shift_count, Result) */
437
438                 return (operand0 >> operand1);
439
440
441         case AML_SUBTRACT_OP:           /* Subtract (Operand0, Operand1, Result) */
442
443                 return (operand0 - operand1);
444
445         default:
446
447                 return (0);
448         }
449 }
450
451
452 /*******************************************************************************
453  *
454  * FUNCTION:    acpi_ex_do_logical_op
455  *
456  * PARAMETERS:  Opcode              - AML opcode
457  *              obj_desc0           - operand #0
458  *              obj_desc1           - operand #1
459  *
460  * RETURN:      TRUE/FALSE result of the operation
461  *
462  * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
463  *              functions here is to prevent a lot of pointer dereferencing
464  *              to obtain the operands and to simplify the generation of the
465  *              logical value.  Both operands must already be validated as
466  *              1) Both the same type, and
467  *              2) Either Integer, Buffer, or String type.
468  *
469  *              Note: cleanest machine code seems to be produced by the code
470  *              below, rather than using statements of the form:
471  *                  Result = (Operand0 == Operand1);
472  *
473  ******************************************************************************/
474
475 u8
476 acpi_ex_do_logical_op (
477         u16                             opcode,
478         union acpi_operand_object       *obj_desc0,
479         union acpi_operand_object       *obj_desc1)
480 {
481         acpi_integer                    operand0;
482         acpi_integer                    operand1;
483         u8                              *ptr0;
484         u8                              *ptr1;
485         u32                             length0;
486         u32                             length1;
487         u32                             i;
488
489
490         ACPI_FUNCTION_ENTRY ();
491
492
493         if (ACPI_GET_OBJECT_TYPE (obj_desc0) == ACPI_TYPE_INTEGER) {
494                 /* Both operands are of type integer */
495
496                 operand0 = obj_desc0->integer.value;
497                 operand1 = obj_desc1->integer.value;
498
499                 switch (opcode) {
500                 case AML_LAND_OP:               /* LAnd (Operand0, Operand1) */
501
502                         if (operand0 && operand1) {
503                                 return (TRUE);
504                         }
505                         break;
506
507                 case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
508
509                         if (operand0 == operand1) {
510                                 return (TRUE);
511                         }
512                         break;
513
514                 case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
515
516                         if (operand0 > operand1) {
517                                 return (TRUE);
518                         }
519                         break;
520
521                 case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
522
523                         if (operand0 < operand1) {
524                                 return (TRUE);
525                         }
526                         break;
527
528                 case AML_LOR_OP:                 /* LOr (Operand0, Operand1) */
529
530                         if (operand0 || operand1) {
531                                 return (TRUE);
532                         }
533                         break;
534
535                 default:
536                         break;
537                 }
538         }
539         else {
540                 /*
541                  * Case for Buffer/String objects.
542                  * NOTE: takes advantage of common Buffer/String object fields
543                  */
544                 length0 = obj_desc0->buffer.length;
545                 ptr0    = obj_desc0->buffer.pointer;
546
547                 length1 = obj_desc1->buffer.length;
548                 ptr1    = obj_desc1->buffer.pointer;
549
550                 switch (opcode) {
551                 case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
552
553                         /* Length and all bytes must be equal */
554
555                         if (length0 != length1) {
556                                 return (FALSE);
557                         }
558
559                         for (i = 0; i < length0; i++) {
560                                 if (ptr0[i] != ptr1[i]) {
561                                         return (FALSE);
562                                 }
563                         }
564                         return (TRUE);
565
566                 case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
567
568                         /* Lexicographic compare:  Scan the 1-to-1 data */
569
570                         for (i = 0; (i < length0) && (i < length1); i++) {
571                                 if (ptr0[i] > ptr1[i]) {
572                                         return (TRUE);
573                                 }
574                         }
575
576                         /* Bytes match, now check lengths */
577
578                         if (length0 > length1) {
579                                 return (TRUE);
580                         }
581
582                         /* Length0 <= Length1 */
583
584                         return (FALSE);
585
586                 case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
587
588                         /* Lexicographic compare:  Scan the 1-to-1 data */
589
590                         for (i = 0; (i < length0) && (i < length1); i++) {
591                                 if (ptr0[i] < ptr1[i]) {
592                                         return (TRUE);
593                                 }
594                         }
595
596                         /* Bytes match, now check lengths */
597
598                         if (length0 < length1) {
599                                 return (TRUE);
600                         }
601
602                         /* Length0 >= Length1 */
603
604                         return (FALSE);
605
606                 default:
607                         break;
608                 }
609         }
610
611         return (FALSE);
612 }
613
614