* Intel ACPI 20030228 distribution with local DragonFly changes.
[dragonfly.git] / sys / contrib / dev / acpica / exmisc.c
1
2 /******************************************************************************
3  *
4  * Module Name: exmisc - ACPI AML (p-code) execution - specific opcodes
5  *              $Revision: 115 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
14  * All rights reserved.
15  *
16  * 2. License
17  *
18  * 2.1. This is your license from Intel Corp. under its intellectual property
19  * rights.  You may have additional license terms from the party that provided
20  * you this software, covering your right to use that party's intellectual
21  * property rights.
22  *
23  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
24  * copy of the source code appearing in this file ("Covered Code") an
25  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
26  * base code distributed originally by Intel ("Original Intel Code") to copy,
27  * make derivatives, distribute, use and display any portion of the Covered
28  * Code in any form, with the right to sublicense such rights; and
29  *
30  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
31  * license (with the right to sublicense), under only those claims of Intel
32  * patents that are infringed by the Original Intel Code, to make, use, sell,
33  * offer to sell, and import the Covered Code and derivative works thereof
34  * solely to the minimum extent necessary to exercise the above copyright
35  * license, and in no event shall the patent license extend to any additions
36  * to or modifications of the Original Intel Code.  No other license or right
37  * is granted directly or by implication, estoppel or otherwise;
38  *
39  * The above copyright and patent license is granted only if the following
40  * conditions are met:
41  *
42  * 3. Conditions
43  *
44  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
45  * Redistribution of source code of any substantial portion of the Covered
46  * Code or modification with rights to further distribute source must include
47  * the above Copyright Notice, the above License, this list of Conditions,
48  * and the following Disclaimer and Export Compliance provision.  In addition,
49  * Licensee must cause all Covered Code to which Licensee contributes to
50  * contain a file documenting the changes Licensee made to create that Covered
51  * Code and the date of any change.  Licensee must include in that file the
52  * documentation of any changes made by any predecessor Licensee.  Licensee
53  * must include a prominent statement that the modification is derived,
54  * directly or indirectly, from Original Intel Code.
55  *
56  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
57  * Redistribution of source code of any substantial portion of the Covered
58  * Code or modification without rights to further distribute source must
59  * include the following Disclaimer and Export Compliance provision in the
60  * documentation and/or other materials provided with distribution.  In
61  * addition, Licensee may not authorize further sublicense of source of any
62  * portion of the Covered Code, and must include terms to the effect that the
63  * license from Licensee to its licensee is limited to the intellectual
64  * property embodied in the software Licensee provides to its licensee, and
65  * not to intellectual property embodied in modifications its licensee may
66  * make.
67  *
68  * 3.3. Redistribution of Executable. Redistribution in executable form of any
69  * substantial portion of the Covered Code or modification must reproduce the
70  * above Copyright Notice, and the following Disclaimer and Export Compliance
71  * provision in the documentation and/or other materials provided with the
72  * distribution.
73  *
74  * 3.4. Intel retains all right, title, and interest in and to the Original
75  * Intel Code.
76  *
77  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
78  * Intel shall be used in advertising or otherwise to promote the sale, use or
79  * other dealings in products derived from or relating to the Covered Code
80  * without prior written authorization from Intel.
81  *
82  * 4. Disclaimer and Export Compliance
83  *
84  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
85  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
86  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
87  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
88
89  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
90  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91  * PARTICULAR PURPOSE.
92  *
93  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
94  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
95  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
96  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
97  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
98  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
99  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100  * LIMITED REMEDY.
101  *
102  * 4.3. Licensee shall not export, either directly or indirectly, any of this
103  * software or system incorporating such software without first obtaining any
104  * required license or other approval from the U. S. Department of Commerce or
105  * any other agency or department of the United States Government.  In the
106  * event Licensee exports any such software from the United States or
107  * re-exports any such software from a foreign destination, Licensee shall
108  * ensure that the distribution and export/re-export of the software is in
109  * compliance with all laws, regulations, orders, or other restrictions of the
110  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
111  * any of its subsidiaries will export/re-export any technical data, process,
112  * software, or service, directly or indirectly, to any country for which the
113  * United States government or any agency thereof requires an export license,
114  * other governmental approval, or letter of assurance, without first obtaining
115  * such license, approval or letter.
116  *
117  *****************************************************************************/
118 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/exmisc.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $                                                               */
119
120 #define __EXMISC_C__
121
122 #include "acpi.h"
123 #include "acinterp.h"
124 #include "amlcode.h"
125
126
127 #define _COMPONENT          ACPI_EXECUTER
128         ACPI_MODULE_NAME    ("exmisc")
129
130
131 /*******************************************************************************
132  *
133  * FUNCTION:    AcpiExGetObjectReference
134  *
135  * PARAMETERS:  ObjDesc             - Create a reference to this object
136  *              ReturnDesc          - Where to store the reference
137  *              WalkState           - Current state
138  *
139  * RETURN:      Status
140  *
141  * DESCRIPTION: Obtain and return a "reference" to the target object
142  *              Common code for the RefOfOp and the CondRefOfOp.
143  *
144  ******************************************************************************/
145
146 ACPI_STATUS
147 AcpiExGetObjectReference (
148     ACPI_OPERAND_OBJECT     *ObjDesc,
149     ACPI_OPERAND_OBJECT     **ReturnDesc,
150     ACPI_WALK_STATE         *WalkState)
151 {
152     ACPI_OPERAND_OBJECT     *ReferenceObj;
153     ACPI_OPERAND_OBJECT     *ReferencedObj;
154
155
156     ACPI_FUNCTION_TRACE_PTR ("ExGetObjectReference", ObjDesc);
157
158
159     *ReturnDesc = NULL;
160
161     switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
162     {
163     case ACPI_DESC_TYPE_OPERAND:
164
165         if (ACPI_GET_OBJECT_TYPE (ObjDesc) != ACPI_TYPE_LOCAL_REFERENCE)
166         {
167             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
168         }
169
170         /*
171          * Must be a reference to a Local or Arg
172          */
173         switch (ObjDesc->Reference.Opcode)
174         {
175         case AML_LOCAL_OP:
176         case AML_ARG_OP:
177
178             /* The referenced object is the pseudo-node for the local/arg */
179
180             ReferencedObj = ObjDesc->Reference.Object;
181             break;
182
183         default:
184
185             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Unknown Reference subtype %X\n",
186                 ObjDesc->Reference.Opcode));
187             return_ACPI_STATUS (AE_AML_INTERNAL);
188         }
189         break;
190
191
192     case ACPI_DESC_TYPE_NAMED:
193
194         /*
195          * A named reference that has already been resolved to a Node
196          */
197         ReferencedObj = ObjDesc;
198         break;
199
200
201     default:
202
203         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Invalid descriptor type %X in %p\n",
204             ACPI_GET_DESCRIPTOR_TYPE (ObjDesc), ObjDesc));
205         return_ACPI_STATUS (AE_TYPE);
206     }
207
208
209     /* Create a new reference object */
210
211     ReferenceObj = AcpiUtCreateInternalObject (ACPI_TYPE_LOCAL_REFERENCE);
212     if (!ReferenceObj)
213     {
214         return_ACPI_STATUS (AE_NO_MEMORY);
215     }
216
217     ReferenceObj->Reference.Opcode = AML_REF_OF_OP;
218     ReferenceObj->Reference.Object = ReferencedObj;
219     *ReturnDesc = ReferenceObj;
220
221     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Object %p Type [%s], returning Reference %p\n",
222         ObjDesc, AcpiUtGetObjectTypeName (ObjDesc), *ReturnDesc));
223
224     return_ACPI_STATUS (AE_OK);
225 }
226
227
228 /*******************************************************************************
229  *
230  * FUNCTION:    AcpiExConcatTemplate
231  *
232  * PARAMETERS:  *ObjDesc            - Object to be converted.  Must be an
233  *                                    Integer, Buffer, or String
234  *              WalkState           - Current walk state
235  *
236  * RETURN:      Status
237  *
238  * DESCRIPTION: Concatenate two resource templates
239  *
240  ******************************************************************************/
241
242 ACPI_STATUS
243 AcpiExConcatTemplate (
244     ACPI_OPERAND_OBJECT     *ObjDesc1,
245     ACPI_OPERAND_OBJECT     *ObjDesc2,
246     ACPI_OPERAND_OBJECT     **ActualReturnDesc,
247     ACPI_WALK_STATE         *WalkState)
248 {
249     ACPI_OPERAND_OBJECT     *ReturnDesc;
250     UINT8                   *NewBuf;
251     UINT8                   *EndTag1;
252     UINT8                   *EndTag2;
253     ACPI_SIZE               Length1;
254     ACPI_SIZE               Length2;
255
256
257     ACPI_FUNCTION_TRACE ("ExConcatTemplate");
258
259
260     /* Find the EndTags in each resource template */
261
262     EndTag1 = AcpiUtGetResourceEndTag (ObjDesc1);
263     EndTag2 = AcpiUtGetResourceEndTag (ObjDesc2);
264     if (!EndTag1 || !EndTag2)
265     {
266         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
267     }
268
269     /* Compute the length of each part */
270
271     Length1 = ACPI_PTR_DIFF (EndTag1, ObjDesc1->Buffer.Pointer);
272     Length2 = ACPI_PTR_DIFF (EndTag2, ObjDesc2->Buffer.Pointer) +
273                              2; /* Size of END_TAG */
274
275     /* Create a new buffer object for the result */
276
277     ReturnDesc = AcpiUtCreateBufferObject (Length1 + Length2);
278     if (!ReturnDesc)
279     {
280         return_ACPI_STATUS (AE_NO_MEMORY);
281     }
282
283     /* Copy the templates to the new descriptor */
284
285     NewBuf = ReturnDesc->Buffer.Pointer;
286     ACPI_MEMCPY (NewBuf, ObjDesc1->Buffer.Pointer, Length1);
287     ACPI_MEMCPY (NewBuf + Length1, ObjDesc2->Buffer.Pointer, Length2);
288
289     /* Compute the new checksum */
290
291     NewBuf[ReturnDesc->Buffer.Length - 1] =
292             AcpiUtGenerateChecksum (ReturnDesc->Buffer.Pointer,
293                                    (ReturnDesc->Buffer.Length - 1));
294
295     /* Return the completed template descriptor */
296
297     *ActualReturnDesc = ReturnDesc;
298     return_ACPI_STATUS (AE_OK);
299 }
300
301
302 /*******************************************************************************
303  *
304  * FUNCTION:    AcpiExDoConcatenate
305  *
306  * PARAMETERS:  ObjDesc1            - First source object
307  *              ObjDesc2            - Second source object
308  *              ActualReturnDesc    - Where to place the return object
309  *              WalkState           - Current walk state
310  *
311  * RETURN:      Status
312  *
313  * DESCRIPTION: Concatenate two objects OF THE SAME TYPE.
314  *
315  ******************************************************************************/
316
317 ACPI_STATUS
318 AcpiExDoConcatenate (
319     ACPI_OPERAND_OBJECT     *ObjDesc1,
320     ACPI_OPERAND_OBJECT     *ObjDesc2,
321     ACPI_OPERAND_OBJECT     **ActualReturnDesc,
322     ACPI_WALK_STATE         *WalkState)
323 {
324     ACPI_STATUS             Status;
325     UINT32                  i;
326     ACPI_INTEGER            ThisInteger;
327     ACPI_OPERAND_OBJECT     *ReturnDesc;
328     char                    *NewBuf;
329
330
331     ACPI_FUNCTION_ENTRY ();
332
333
334     /*
335      * There are three cases to handle:
336      *
337      * 1) Two Integers concatenated to produce a new Buffer
338      * 2) Two Strings concatenated to produce a new String
339      * 3) Two Buffers concatenated to produce a new Buffer
340      */
341     switch (ACPI_GET_OBJECT_TYPE (ObjDesc1))
342     {
343     case ACPI_TYPE_INTEGER:
344
345         /* Result of two Integers is a Buffer */
346         /* Need enough buffer space for two integers */
347
348         ReturnDesc = AcpiUtCreateBufferObject (AcpiGbl_IntegerByteWidth * 2);
349         if (!ReturnDesc)
350         {
351             return (AE_NO_MEMORY);
352         }
353
354         NewBuf = (char *) ReturnDesc->Buffer.Pointer;
355
356         /* Convert the first integer */
357
358         ThisInteger = ObjDesc1->Integer.Value;
359         for (i = 0; i < AcpiGbl_IntegerByteWidth; i++)
360         {
361             NewBuf[i] = (char) ThisInteger;
362             ThisInteger >>= 8;
363         }
364
365         /* Convert the second integer */
366
367         ThisInteger = ObjDesc2->Integer.Value;
368         for (; i < (ACPI_MUL_2 (AcpiGbl_IntegerByteWidth)); i++)
369         {
370             NewBuf[i] = (char) ThisInteger;
371             ThisInteger >>= 8;
372         }
373
374         break;
375
376
377     case ACPI_TYPE_STRING:
378
379         /* Result of two Strings is a String */
380
381         ReturnDesc = AcpiUtCreateInternalObject (ACPI_TYPE_STRING);
382         if (!ReturnDesc)
383         {
384             return (AE_NO_MEMORY);
385         }
386
387         /* Operand0 is string  */
388
389         NewBuf = ACPI_MEM_CALLOCATE ((ACPI_SIZE) ObjDesc1->String.Length +
390                                      (ACPI_SIZE) ObjDesc2->String.Length + 1);
391         if (!NewBuf)
392         {
393             ACPI_REPORT_ERROR
394                 (("ExDoConcatenate: String allocation failure\n"));
395             Status = AE_NO_MEMORY;
396             goto Cleanup;
397         }
398
399         /* Concatenate the strings */
400
401         ACPI_STRCPY (NewBuf, ObjDesc1->String.Pointer);
402         ACPI_STRCPY (NewBuf + ObjDesc1->String.Length,
403                               ObjDesc2->String.Pointer);
404
405         /* Complete the String object initialization */
406
407         ReturnDesc->String.Pointer = NewBuf;
408         ReturnDesc->String.Length  = ObjDesc1->String.Length +
409                                      ObjDesc2->String.Length;
410         break;
411
412
413     case ACPI_TYPE_BUFFER:
414
415         /* Result of two Buffers is a Buffer */
416
417         ReturnDesc = AcpiUtCreateBufferObject (
418                             (ACPI_SIZE) ObjDesc1->Buffer.Length +
419                             (ACPI_SIZE) ObjDesc2->Buffer.Length);
420         if (!ReturnDesc)
421         {
422             return (AE_NO_MEMORY);
423         }
424
425         NewBuf = (char *) ReturnDesc->Buffer.Pointer;
426
427         /* Concatenate the buffers */
428
429         ACPI_MEMCPY (NewBuf, ObjDesc1->Buffer.Pointer,
430                         ObjDesc1->Buffer.Length);
431         ACPI_MEMCPY (NewBuf + ObjDesc1->Buffer.Length, ObjDesc2->Buffer.Pointer,
432                          ObjDesc2->Buffer.Length);
433
434         break;
435
436
437     default:
438
439         /* Invalid object type, should not happen here */
440
441         Status = AE_AML_INTERNAL;
442         ReturnDesc = NULL;
443     }
444
445     *ActualReturnDesc = ReturnDesc;
446     return (AE_OK);
447
448
449 Cleanup:
450
451     AcpiUtRemoveReference (ReturnDesc);
452     return (Status);
453 }
454
455
456 /*******************************************************************************
457  *
458  * FUNCTION:    AcpiExDoMathOp
459  *
460  * PARAMETERS:  Opcode              - AML opcode
461  *              Operand0            - Integer operand #0
462  *              Operand1            - Integer operand #1
463  *
464  * RETURN:      Integer result of the operation
465  *
466  * DESCRIPTION: Execute a math AML opcode. The purpose of having all of the
467  *              math functions here is to prevent a lot of pointer dereferencing
468  *              to obtain the operands.
469  *
470  ******************************************************************************/
471
472 ACPI_INTEGER
473 AcpiExDoMathOp (
474     UINT16                  Opcode,
475     ACPI_INTEGER            Operand0,
476     ACPI_INTEGER            Operand1)
477 {
478
479
480     switch (Opcode)
481     {
482     case AML_ADD_OP:                /* Add (Operand0, Operand1, Result) */
483
484         return (Operand0 + Operand1);
485
486
487     case AML_BIT_AND_OP:            /* And (Operand0, Operand1, Result) */
488
489         return (Operand0 & Operand1);
490
491
492     case AML_BIT_NAND_OP:           /* NAnd (Operand0, Operand1, Result) */
493
494         return (~(Operand0 & Operand1));
495
496
497     case AML_BIT_OR_OP:             /* Or (Operand0, Operand1, Result) */
498
499         return (Operand0 | Operand1);
500
501
502     case AML_BIT_NOR_OP:            /* NOr (Operand0, Operand1, Result) */
503
504         return (~(Operand0 | Operand1));
505
506
507     case AML_BIT_XOR_OP:            /* XOr (Operand0, Operand1, Result) */
508
509         return (Operand0 ^ Operand1);
510
511
512     case AML_MULTIPLY_OP:           /* Multiply (Operand0, Operand1, Result) */
513
514         return (Operand0 * Operand1);
515
516
517     case AML_SHIFT_LEFT_OP:         /* ShiftLeft (Operand, ShiftCount, Result) */
518
519         return (Operand0 << Operand1);
520
521
522     case AML_SHIFT_RIGHT_OP:        /* ShiftRight (Operand, ShiftCount, Result) */
523
524         return (Operand0 >> Operand1);
525
526
527     case AML_SUBTRACT_OP:           /* Subtract (Operand0, Operand1, Result) */
528
529         return (Operand0 - Operand1);
530
531     default:
532
533         return (0);
534     }
535 }
536
537
538 /*******************************************************************************
539  *
540  * FUNCTION:    AcpiExDoLogicalOp
541  *
542  * PARAMETERS:  Opcode              - AML opcode
543  *              Operand0            - Integer operand #0
544  *              Operand1            - Integer operand #1
545  *
546  * RETURN:      TRUE/FALSE result of the operation
547  *
548  * DESCRIPTION: Execute a logical AML opcode. The purpose of having all of the
549  *              functions here is to prevent a lot of pointer dereferencing
550  *              to obtain the operands and to simplify the generation of the
551  *              logical value.
552  *
553  *              Note: cleanest machine code seems to be produced by the code
554  *              below, rather than using statements of the form:
555  *                  Result = (Operand0 == Operand1);
556  *
557  ******************************************************************************/
558
559 BOOLEAN
560 AcpiExDoLogicalOp (
561     UINT16                  Opcode,
562     ACPI_INTEGER            Operand0,
563     ACPI_INTEGER            Operand1)
564 {
565
566
567     switch (Opcode)
568     {
569
570     case AML_LAND_OP:               /* LAnd (Operand0, Operand1) */
571
572         if (Operand0 && Operand1)
573         {
574             return (TRUE);
575         }
576         break;
577
578
579     case AML_LEQUAL_OP:             /* LEqual (Operand0, Operand1) */
580
581         if (Operand0 == Operand1)
582         {
583             return (TRUE);
584         }
585         break;
586
587
588     case AML_LGREATER_OP:           /* LGreater (Operand0, Operand1) */
589
590         if (Operand0 > Operand1)
591         {
592             return (TRUE);
593         }
594         break;
595
596
597     case AML_LLESS_OP:              /* LLess (Operand0, Operand1) */
598
599         if (Operand0 < Operand1)
600         {
601             return (TRUE);
602         }
603         break;
604
605
606     case AML_LOR_OP:                 /* LOr (Operand0, Operand1) */
607
608         if (Operand0 || Operand1)
609         {
610             return (TRUE);
611         }
612         break;
613
614     default:
615         break;
616     }
617
618     return (FALSE);
619 }
620
621