Import acpica-unix-20061109 from Intel.
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / interpreter / executer / exprep.c
1
2 /******************************************************************************
3  *
4  * Module Name: exprep - ACPI AML (p-code) execution - field prep utilities
5  *              $Revision: 1.141 $
6  *
7  *****************************************************************************/
8
9 /******************************************************************************
10  *
11  * 1. Copyright Notice
12  *
13  * Some or all of this work - Copyright (c) 1999 - 2006, 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  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
89  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90  * PARTICULAR PURPOSE.
91  *
92  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
93  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
94  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
95  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
96  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
97  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
98  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99  * LIMITED REMEDY.
100  *
101  * 4.3. Licensee shall not export, either directly or indirectly, any of this
102  * software or system incorporating such software without first obtaining any
103  * required license or other approval from the U. S. Department of Commerce or
104  * any other agency or department of the United States Government.  In the
105  * event Licensee exports any such software from the United States or
106  * re-exports any such software from a foreign destination, Licensee shall
107  * ensure that the distribution and export/re-export of the software is in
108  * compliance with all laws, regulations, orders, or other restrictions of the
109  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
110  * any of its subsidiaries will export/re-export any technical data, process,
111  * software, or service, directly or indirectly, to any country for which the
112  * United States government or any agency thereof requires an export license,
113  * other governmental approval, or letter of assurance, without first obtaining
114  * such license, approval or letter.
115  *
116  *****************************************************************************/
117
118 #define __EXPREP_C__
119
120 #include "acpi.h"
121 #include "acinterp.h"
122 #include "amlcode.h"
123 #include "acnamesp.h"
124
125
126 #define _COMPONENT          ACPI_EXECUTER
127         ACPI_MODULE_NAME    ("exprep")
128
129 /* Local prototypes */
130
131 static UINT32
132 AcpiExDecodeFieldAccess (
133     ACPI_OPERAND_OBJECT     *ObjDesc,
134     UINT8                   FieldFlags,
135     UINT32                  *ReturnByteAlignment);
136
137
138 #ifdef ACPI_UNDER_DEVELOPMENT
139
140 static UINT32
141 AcpiExGenerateAccess (
142     UINT32                  FieldBitOffset,
143     UINT32                  FieldBitLength,
144     UINT32                  RegionLength);
145
146 /*******************************************************************************
147  *
148  * FUNCTION:    AcpiExGenerateAccess
149  *
150  * PARAMETERS:  FieldBitOffset      - Start of field within parent region/buffer
151  *              FieldBitLength      - Length of field in bits
152  *              RegionLength        - Length of parent in bytes
153  *
154  * RETURN:      Field granularity (8, 16, 32 or 64) and
155  *              ByteAlignment (1, 2, 3, or 4)
156  *
157  * DESCRIPTION: Generate an optimal access width for fields defined with the
158  *              AnyAcc keyword.
159  *
160  * NOTE: Need to have the RegionLength in order to check for boundary
161  *       conditions (end-of-region).  However, the RegionLength is a deferred
162  *       operation.  Therefore, to complete this implementation, the generation
163  *       of this access width must be deferred until the region length has
164  *       been evaluated.
165  *
166  ******************************************************************************/
167
168 static UINT32
169 AcpiExGenerateAccess (
170     UINT32                  FieldBitOffset,
171     UINT32                  FieldBitLength,
172     UINT32                  RegionLength)
173 {
174     UINT32                  FieldByteLength;
175     UINT32                  FieldByteOffset;
176     UINT32                  FieldByteEndOffset;
177     UINT32                  AccessByteWidth;
178     UINT32                  FieldStartOffset;
179     UINT32                  FieldEndOffset;
180     UINT32                  MinimumAccessWidth = 0xFFFFFFFF;
181     UINT32                  MinimumAccesses = 0xFFFFFFFF;
182     UINT32                  Accesses;
183
184
185     ACPI_FUNCTION_TRACE (ExGenerateAccess);
186
187
188     /* Round Field start offset and length to "minimal" byte boundaries */
189
190     FieldByteOffset    = ACPI_DIV_8 (ACPI_ROUND_DOWN (FieldBitOffset, 8));
191     FieldByteEndOffset = ACPI_DIV_8 (ACPI_ROUND_UP   (FieldBitLength +
192                                                       FieldBitOffset, 8));
193     FieldByteLength    = FieldByteEndOffset - FieldByteOffset;
194
195     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
196             "Bit length %d, Bit offset %d\n",
197             FieldBitLength, FieldBitOffset));
198
199     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
200             "Byte Length %d, Byte Offset %d, End Offset %d\n",
201             FieldByteLength, FieldByteOffset, FieldByteEndOffset));
202
203     /*
204      * Iterative search for the maximum access width that is both aligned
205      * and does not go beyond the end of the region
206      *
207      * Start at ByteAcc and work upwards to QwordAcc max. (1,2,4,8 bytes)
208      */
209     for (AccessByteWidth = 1; AccessByteWidth <= 8; AccessByteWidth <<= 1)
210     {
211         /*
212          * 1) Round end offset up to next access boundary and make sure that
213          *    this does not go beyond the end of the parent region.
214          * 2) When the Access width is greater than the FieldByteLength, we
215          *    are done. (This does not optimize for the perfectly aligned
216          *    case yet).
217          */
218         if (ACPI_ROUND_UP (FieldByteEndOffset, AccessByteWidth) <= RegionLength)
219         {
220             FieldStartOffset =
221                 ACPI_ROUND_DOWN (FieldByteOffset, AccessByteWidth) /
222                 AccessByteWidth;
223
224             FieldEndOffset =
225                 ACPI_ROUND_UP ((FieldByteLength + FieldByteOffset),
226                     AccessByteWidth) / AccessByteWidth;
227
228             Accesses = FieldEndOffset - FieldStartOffset;
229
230             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
231                     "AccessWidth %d end is within region\n", AccessByteWidth));
232
233             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
234                     "Field Start %d, Field End %d -- requires %d accesses\n",
235                     FieldStartOffset, FieldEndOffset, Accesses));
236
237             /* Single access is optimal */
238
239             if (Accesses <= 1)
240             {
241                 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
242                     "Entire field can be accessed with one operation of size %d\n",
243                     AccessByteWidth));
244                 return_VALUE (AccessByteWidth);
245             }
246
247             /*
248              * Fits in the region, but requires more than one read/write.
249              * try the next wider access on next iteration
250              */
251             if (Accesses < MinimumAccesses)
252             {
253                 MinimumAccesses    = Accesses;
254                 MinimumAccessWidth = AccessByteWidth;
255             }
256         }
257         else
258         {
259             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
260                 "AccessWidth %d end is NOT within region\n", AccessByteWidth));
261             if (AccessByteWidth == 1)
262             {
263                 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
264                         "Field goes beyond end-of-region!\n"));
265
266                 /* Field does not fit in the region at all */
267
268                 return_VALUE (0);
269             }
270
271             /*
272              * This width goes beyond the end-of-region, back off to
273              * previous access
274              */
275             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
276                     "Backing off to previous optimal access width of %d\n",
277                     MinimumAccessWidth));
278             return_VALUE (MinimumAccessWidth);
279         }
280     }
281
282     /*
283      * Could not read/write field with one operation,
284      * just use max access width
285      */
286     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
287             "Cannot access field in one operation, using width 8\n"));
288     return_VALUE (8);
289 }
290 #endif /* ACPI_UNDER_DEVELOPMENT */
291
292
293 /*******************************************************************************
294  *
295  * FUNCTION:    AcpiExDecodeFieldAccess
296  *
297  * PARAMETERS:  ObjDesc             - Field object
298  *              FieldFlags          - Encoded fieldflags (contains access bits)
299  *              ReturnByteAlignment - Where the byte alignment is returned
300  *
301  * RETURN:      Field granularity (8, 16, 32 or 64) and
302  *              ByteAlignment (1, 2, 3, or 4)
303  *
304  * DESCRIPTION: Decode the AccessType bits of a field definition.
305  *
306  ******************************************************************************/
307
308 static UINT32
309 AcpiExDecodeFieldAccess (
310     ACPI_OPERAND_OBJECT     *ObjDesc,
311     UINT8                   FieldFlags,
312     UINT32                  *ReturnByteAlignment)
313 {
314     UINT32                  Access;
315     UINT32                  ByteAlignment;
316     UINT32                  BitLength;
317
318
319     ACPI_FUNCTION_TRACE (ExDecodeFieldAccess);
320
321
322     Access = (FieldFlags & AML_FIELD_ACCESS_TYPE_MASK);
323
324     switch (Access)
325     {
326     case AML_FIELD_ACCESS_ANY:
327
328 #ifdef ACPI_UNDER_DEVELOPMENT
329         ByteAlignment =
330             AcpiExGenerateAccess (ObjDesc->CommonField.StartFieldBitOffset,
331                 ObjDesc->CommonField.BitLength,
332                 0xFFFFFFFF /* Temp until we pass RegionLength as parameter */);
333         BitLength = ByteAlignment * 8;
334 #endif
335
336         ByteAlignment = 1;
337         BitLength = 8;
338         break;
339
340     case AML_FIELD_ACCESS_BYTE:
341     case AML_FIELD_ACCESS_BUFFER:   /* ACPI 2.0 (SMBus Buffer) */
342         ByteAlignment = 1;
343         BitLength     = 8;
344         break;
345
346     case AML_FIELD_ACCESS_WORD:
347         ByteAlignment = 2;
348         BitLength     = 16;
349         break;
350
351     case AML_FIELD_ACCESS_DWORD:
352         ByteAlignment = 4;
353         BitLength     = 32;
354         break;
355
356     case AML_FIELD_ACCESS_QWORD:    /* ACPI 2.0 */
357         ByteAlignment = 8;
358         BitLength     = 64;
359         break;
360
361     default:
362         /* Invalid field access type */
363
364         ACPI_ERROR ((AE_INFO,
365             "Unknown field access type %X",
366             Access));
367         return_UINT32 (0);
368     }
369
370     if (ACPI_GET_OBJECT_TYPE (ObjDesc) == ACPI_TYPE_BUFFER_FIELD)
371     {
372         /*
373          * BufferField access can be on any byte boundary, so the
374          * ByteAlignment is always 1 byte -- regardless of any ByteAlignment
375          * implied by the field access type.
376          */
377         ByteAlignment = 1;
378     }
379
380     *ReturnByteAlignment = ByteAlignment;
381     return_UINT32 (BitLength);
382 }
383
384
385 /*******************************************************************************
386  *
387  * FUNCTION:    AcpiExPrepCommonFieldObject
388  *
389  * PARAMETERS:  ObjDesc             - The field object
390  *              FieldFlags          - Access, LockRule, and UpdateRule.
391  *                                    The format of a FieldFlag is described
392  *                                    in the ACPI specification
393  *              FieldAttribute      - Special attributes (not used)
394  *              FieldBitPosition    - Field start position
395  *              FieldBitLength      - Field length in number of bits
396  *
397  * RETURN:      Status
398  *
399  * DESCRIPTION: Initialize the areas of the field object that are common
400  *              to the various types of fields.  Note: This is very "sensitive"
401  *              code because we are solving the general case for field
402  *              alignment.
403  *
404  ******************************************************************************/
405
406 ACPI_STATUS
407 AcpiExPrepCommonFieldObject (
408     ACPI_OPERAND_OBJECT     *ObjDesc,
409     UINT8                   FieldFlags,
410     UINT8                   FieldAttribute,
411     UINT32                  FieldBitPosition,
412     UINT32                  FieldBitLength)
413 {
414     UINT32                  AccessBitWidth;
415     UINT32                  ByteAlignment;
416     UINT32                  NearestByteAddress;
417
418
419     ACPI_FUNCTION_TRACE (ExPrepCommonFieldObject);
420
421
422     /*
423      * Note: the structure being initialized is the
424      * ACPI_COMMON_FIELD_INFO;  No structure fields outside of the common
425      * area are initialized by this procedure.
426      */
427     ObjDesc->CommonField.FieldFlags = FieldFlags;
428     ObjDesc->CommonField.Attribute  = FieldAttribute;
429     ObjDesc->CommonField.BitLength  = FieldBitLength;
430
431     /*
432      * Decode the access type so we can compute offsets.  The access type gives
433      * two pieces of information - the width of each field access and the
434      * necessary ByteAlignment (address granularity) of the access.
435      *
436      * For AnyAcc, the AccessBitWidth is the largest width that is both
437      * necessary and possible in an attempt to access the whole field in one
438      * I/O operation.  However, for AnyAcc, the ByteAlignment is always one
439      * byte.
440      *
441      * For all Buffer Fields, the ByteAlignment is always one byte.
442      *
443      * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is
444      * the same (equivalent) as the ByteAlignment.
445      */
446     AccessBitWidth = AcpiExDecodeFieldAccess (ObjDesc, FieldFlags,
447                                 &ByteAlignment);
448     if (!AccessBitWidth)
449     {
450         return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
451     }
452
453     /* Setup width (access granularity) fields */
454
455     ObjDesc->CommonField.AccessByteWidth = (UINT8)
456             ACPI_DIV_8 (AccessBitWidth);            /* 1,  2,  4,  8 */
457
458     ObjDesc->CommonField.AccessBitWidth = (UINT8) AccessBitWidth;
459
460     /*
461      * BaseByteOffset is the address of the start of the field within the
462      * region.  It is the byte address of the first *datum* (field-width data
463      * unit) of the field. (i.e., the first datum that contains at least the
464      * first *bit* of the field.)
465      *
466      * Note: ByteAlignment is always either equal to the AccessBitWidth or 8
467      * (Byte access), and it defines the addressing granularity of the parent
468      * region or buffer.
469      */
470     NearestByteAddress =
471             ACPI_ROUND_BITS_DOWN_TO_BYTES (FieldBitPosition);
472     ObjDesc->CommonField.BaseByteOffset = (UINT32)
473             ACPI_ROUND_DOWN (NearestByteAddress, ByteAlignment);
474
475     /*
476      * StartFieldBitOffset is the offset of the first bit of the field within
477      * a field datum.
478      */
479     ObjDesc->CommonField.StartFieldBitOffset = (UINT8)
480         (FieldBitPosition - ACPI_MUL_8 (ObjDesc->CommonField.BaseByteOffset));
481
482     /*
483      * Does the entire field fit within a single field access element? (datum)
484      * (i.e., without crossing a datum boundary)
485      */
486     if ((ObjDesc->CommonField.StartFieldBitOffset + FieldBitLength) <=
487             (UINT16) AccessBitWidth)
488     {
489         ObjDesc->Common.Flags |= AOPOBJ_SINGLE_DATUM;
490     }
491
492     return_ACPI_STATUS (AE_OK);
493 }
494
495
496 /*******************************************************************************
497  *
498  * FUNCTION:    AcpiExPrepFieldValue
499  *
500  * PARAMETERS:  Info    - Contains all field creation info
501  *
502  * RETURN:      Status
503  *
504  * DESCRIPTION: Construct an ACPI_OPERAND_OBJECT of type DefField and
505  *              connect it to the parent Node.
506  *
507  ******************************************************************************/
508
509 ACPI_STATUS
510 AcpiExPrepFieldValue (
511     ACPI_CREATE_FIELD_INFO  *Info)
512 {
513     ACPI_OPERAND_OBJECT     *ObjDesc;
514     UINT32                  Type;
515     ACPI_STATUS             Status;
516
517
518     ACPI_FUNCTION_TRACE (ExPrepFieldValue);
519
520
521     /* Parameter validation */
522
523     if (Info->FieldType != ACPI_TYPE_LOCAL_INDEX_FIELD)
524     {
525         if (!Info->RegionNode)
526         {
527             ACPI_ERROR ((AE_INFO, "Null RegionNode"));
528             return_ACPI_STATUS (AE_AML_NO_OPERAND);
529         }
530
531         Type = AcpiNsGetType (Info->RegionNode);
532         if (Type != ACPI_TYPE_REGION)
533         {
534             ACPI_ERROR ((AE_INFO,
535                 "Needed Region, found type %X (%s)",
536                 Type, AcpiUtGetTypeName (Type)));
537
538             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
539         }
540     }
541
542     /* Allocate a new field object */
543
544     ObjDesc = AcpiUtCreateInternalObject (Info->FieldType);
545     if (!ObjDesc)
546     {
547         return_ACPI_STATUS (AE_NO_MEMORY);
548     }
549
550     /* Initialize areas of the object that are common to all fields */
551
552     ObjDesc->CommonField.Node = Info->FieldNode;
553     Status = AcpiExPrepCommonFieldObject (ObjDesc, Info->FieldFlags,
554                 Info->Attribute, Info->FieldBitPosition, Info->FieldBitLength);
555     if (ACPI_FAILURE (Status))
556     {
557         AcpiUtDeleteObjectDesc (ObjDesc);
558         return_ACPI_STATUS (Status);
559     }
560
561     /* Initialize areas of the object that are specific to the field type */
562
563     switch (Info->FieldType)
564     {
565     case ACPI_TYPE_LOCAL_REGION_FIELD:
566
567         ObjDesc->Field.RegionObj = AcpiNsGetAttachedObject (Info->RegionNode);
568
569         /* An additional reference for the container */
570
571         AcpiUtAddReference (ObjDesc->Field.RegionObj);
572
573         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
574             "RegionField: BitOff %X, Off %X, Gran %X, Region %p\n",
575             ObjDesc->Field.StartFieldBitOffset, ObjDesc->Field.BaseByteOffset,
576             ObjDesc->Field.AccessByteWidth, ObjDesc->Field.RegionObj));
577         break;
578
579
580     case ACPI_TYPE_LOCAL_BANK_FIELD:
581
582         ObjDesc->BankField.Value     = Info->BankValue;
583         ObjDesc->BankField.RegionObj = AcpiNsGetAttachedObject (
584                                             Info->RegionNode);
585         ObjDesc->BankField.BankObj   = AcpiNsGetAttachedObject (
586                                             Info->RegisterNode);
587
588         /* An additional reference for the attached objects */
589
590         AcpiUtAddReference (ObjDesc->BankField.RegionObj);
591         AcpiUtAddReference (ObjDesc->BankField.BankObj);
592
593         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
594             "Bank Field: BitOff %X, Off %X, Gran %X, Region %p, BankReg %p\n",
595             ObjDesc->BankField.StartFieldBitOffset,
596             ObjDesc->BankField.BaseByteOffset,
597             ObjDesc->Field.AccessByteWidth,
598             ObjDesc->BankField.RegionObj,
599             ObjDesc->BankField.BankObj));
600         break;
601
602
603     case ACPI_TYPE_LOCAL_INDEX_FIELD:
604
605         /* Get the Index and Data registers */
606
607         ObjDesc->IndexField.IndexObj = AcpiNsGetAttachedObject (
608                                             Info->RegisterNode);
609         ObjDesc->IndexField.DataObj  = AcpiNsGetAttachedObject (
610                                             Info->DataRegisterNode);
611
612         if (!ObjDesc->IndexField.DataObj || !ObjDesc->IndexField.IndexObj)
613         {
614             ACPI_ERROR ((AE_INFO, "Null Index Object during field prep"));
615             AcpiUtDeleteObjectDesc (ObjDesc);
616             return_ACPI_STATUS (AE_AML_INTERNAL);
617         }
618
619         /* An additional reference for the attached objects */
620
621         AcpiUtAddReference (ObjDesc->IndexField.DataObj);
622         AcpiUtAddReference (ObjDesc->IndexField.IndexObj);
623
624         /*
625          * April 2006: Changed to match MS behavior
626          *
627          * The value written to the Index register is the byte offset of the
628          * target field in units of the granularity of the IndexField
629          *
630          * Previously, the value was calculated as an index in terms of the
631          * width of the Data register, as below:
632          *
633          *      ObjDesc->IndexField.Value = (UINT32)
634          *          (Info->FieldBitPosition / ACPI_MUL_8 (
635          *              ObjDesc->Field.AccessByteWidth));
636          *
637          * February 2006: Tried value as a byte offset:
638          *      ObjDesc->IndexField.Value = (UINT32)
639          *          ACPI_DIV_8 (Info->FieldBitPosition);
640          */
641         ObjDesc->IndexField.Value = (UINT32) ACPI_ROUND_DOWN (
642             ACPI_DIV_8 (Info->FieldBitPosition),
643             ObjDesc->IndexField.AccessByteWidth);
644
645         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
646             "IndexField: BitOff %X, Off %X, Value %X, Gran %X, Index %p, Data %p\n",
647             ObjDesc->IndexField.StartFieldBitOffset,
648             ObjDesc->IndexField.BaseByteOffset,
649             ObjDesc->IndexField.Value,
650             ObjDesc->Field.AccessByteWidth,
651             ObjDesc->IndexField.IndexObj,
652             ObjDesc->IndexField.DataObj));
653         break;
654
655     default:
656         /* No other types should get here */
657         break;
658     }
659
660     /*
661      * Store the constructed descriptor (ObjDesc) into the parent Node,
662      * preserving the current type of that NamedObj.
663      */
664     Status = AcpiNsAttachObject (Info->FieldNode, ObjDesc,
665                     AcpiNsGetType (Info->FieldNode));
666
667     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Set NamedObj %p [%4.4s], ObjDesc %p\n",
668             Info->FieldNode, AcpiUtGetNodeName (Info->FieldNode), ObjDesc));
669
670     /* Remove local reference to the object */
671
672     AcpiUtRemoveReference (ObjDesc);
673     return_ACPI_STATUS (Status);
674 }
675