machine/stdarg provides __va* macros, not va* macros, use <stdarg.h> instead
[dragonfly.git] / sys / contrib / dev / acpica / exfldio.c
1 /******************************************************************************
2  *
3  * Module Name: exfldio - Aml Field I/O
4  *              $Revision: 92 $
5  *
6  *****************************************************************************/
7
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights.  You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code.  No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision.  In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change.  Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee.  Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution.  In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/exfldio.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $                                                               */
117
118
119 #define __EXFLDIO_C__
120
121 #include "acpi.h"
122 #include "acinterp.h"
123 #include "amlcode.h"
124 #include "acevents.h"
125 #include "acdispat.h"
126
127
128 #define _COMPONENT          ACPI_EXECUTER
129         ACPI_MODULE_NAME    ("exfldio")
130
131
132 /*******************************************************************************
133  *
134  * FUNCTION:    AcpiExSetupRegion
135  *
136  * PARAMETERS:  *ObjDesc                - Field to be read or written
137  *              FieldDatumByteOffset    - Byte offset of this datum within the
138  *                                        parent field
139  *
140  * RETURN:      Status
141  *
142  * DESCRIPTION: Common processing for AcpiExExtractFromField and
143  *              AcpiExInsertIntoField.  Initialize the
144  *
145  ******************************************************************************/
146
147 ACPI_STATUS
148 AcpiExSetupRegion (
149     ACPI_OPERAND_OBJECT     *ObjDesc,
150     UINT32                  FieldDatumByteOffset)
151 {
152     ACPI_STATUS             Status = AE_OK;
153     ACPI_OPERAND_OBJECT     *RgnDesc;
154
155
156     ACPI_FUNCTION_TRACE_U32 ("ExSetupRegion", FieldDatumByteOffset);
157
158
159     RgnDesc = ObjDesc->CommonField.RegionObj;
160
161     /* We must have a valid region */
162
163     if (ACPI_GET_OBJECT_TYPE (RgnDesc) != ACPI_TYPE_REGION)
164     {
165         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %X (%s)\n",
166             ACPI_GET_OBJECT_TYPE (RgnDesc),
167             AcpiUtGetObjectTypeName (RgnDesc)));
168
169         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
170     }
171
172     /*
173      * If the Region Address and Length have not been previously evaluated,
174      * evaluate them now and save the results.
175      */
176     if (!(RgnDesc->Region.Flags & AOPOBJ_DATA_VALID))
177     {
178         Status = AcpiDsGetRegionArguments (RgnDesc);
179         if (ACPI_FAILURE (Status))
180         {
181             return_ACPI_STATUS (Status);
182         }
183     }
184
185     if (RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
186     {
187         /* SMBus has a non-linear address space */
188
189         return_ACPI_STATUS (AE_OK);
190     }
191
192     /*
193      * Validate the request.  The entire request from the byte offset for a
194      * length of one field datum (access width) must fit within the region.
195      * (Region length is specified in bytes)
196      */
197     if (RgnDesc->Region.Length < (ObjDesc->CommonField.BaseByteOffset
198                                     + FieldDatumByteOffset
199                                     + ObjDesc->CommonField.AccessByteWidth))
200     {
201         if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
202         {
203             /*
204              * This is the case where the AccessType (AccWord, etc.) is wider
205              * than the region itself.  For example, a region of length one
206              * byte, and a field with Dword access specified.
207              */
208             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
209                 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n",
210                 ObjDesc->CommonField.Node->Name.Ascii, ObjDesc->CommonField.AccessByteWidth,
211                 RgnDesc->Region.Node->Name.Ascii, RgnDesc->Region.Length));
212         }
213
214         /*
215          * Offset rounded up to next multiple of field width
216          * exceeds region length, indicate an error
217          */
218         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
219             "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n",
220             ObjDesc->CommonField.Node->Name.Ascii, ObjDesc->CommonField.BaseByteOffset,
221             FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
222             RgnDesc->Region.Node->Name.Ascii, RgnDesc->Region.Length));
223
224         return_ACPI_STATUS (AE_AML_REGION_LIMIT);
225     }
226
227     return_ACPI_STATUS (AE_OK);
228 }
229
230
231 /*******************************************************************************
232  *
233  * FUNCTION:    AcpiExAccessRegion
234  *
235  * PARAMETERS:  *ObjDesc                - Field to be read
236  *              FieldDatumByteOffset    - Byte offset of this datum within the
237  *                                        parent field
238  *              *Value                  - Where to store value (must at least
239  *                                        the size of ACPI_INTEGER)
240  *              Function                - Read or Write flag plus other region-
241  *                                        dependent flags
242  *
243  * RETURN:      Status
244  *
245  * DESCRIPTION: Read or Write a single field datum to an Operation Region.
246  *
247  ******************************************************************************/
248
249 ACPI_STATUS
250 AcpiExAccessRegion (
251     ACPI_OPERAND_OBJECT     *ObjDesc,
252     UINT32                  FieldDatumByteOffset,
253     ACPI_INTEGER            *Value,
254     UINT32                  Function)
255 {
256     ACPI_STATUS             Status;
257     ACPI_OPERAND_OBJECT     *RgnDesc;
258     ACPI_PHYSICAL_ADDRESS   Address;
259
260
261     ACPI_FUNCTION_TRACE ("ExAccessRegion");
262
263
264     /*
265      * Ensure that the region operands are fully evaluated and verify
266      * the validity of the request
267      */
268     Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
269     if (ACPI_FAILURE (Status))
270     {
271         return_ACPI_STATUS (Status);
272     }
273
274     /*
275      * The physical address of this field datum is:
276      *
277      * 1) The base of the region, plus
278      * 2) The base offset of the field, plus
279      * 3) The current offset into the field
280      */
281     RgnDesc = ObjDesc->CommonField.RegionObj;
282     Address = RgnDesc->Region.Address
283                 + ObjDesc->CommonField.BaseByteOffset
284                 + FieldDatumByteOffset;
285
286     if ((Function & ACPI_IO_MASK) == ACPI_READ)
287     {
288         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
289     }
290     else
291     {
292         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
293     }
294
295     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
296         " Region[%s-%X] Access %X Base:Off %X:%X at %8.8X%8.8X\n",
297         AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
298         RgnDesc->Region.SpaceId,
299         ObjDesc->CommonField.AccessByteWidth,
300         ObjDesc->CommonField.BaseByteOffset,
301         FieldDatumByteOffset,
302         ACPI_HIDWORD (Address), ACPI_LODWORD (Address)));
303
304     /* Invoke the appropriate AddressSpace/OpRegion handler */
305
306     Status = AcpiEvAddressSpaceDispatch (RgnDesc, Function,
307                     Address, ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
308
309     if (ACPI_FAILURE (Status))
310     {
311         if (Status == AE_NOT_IMPLEMENTED)
312         {
313             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
314                 "Region %s(%X) not implemented\n",
315                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
316                 RgnDesc->Region.SpaceId));
317         }
318         else if (Status == AE_NOT_EXIST)
319         {
320             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
321                 "Region %s(%X) has no handler\n",
322                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
323                 RgnDesc->Region.SpaceId));
324         }
325     }
326
327     return_ACPI_STATUS (Status);
328 }
329
330
331 /*******************************************************************************
332  *
333  * FUNCTION:    AcpiExRegisterOverflow
334  *
335  * PARAMETERS:  *ObjDesc                - Register(Field) to be written
336  *              Value                   - Value to be stored
337  *
338  * RETURN:      TRUE if value overflows the field, FALSE otherwise
339  *
340  * DESCRIPTION: Check if a value is out of range of the field being written.
341  *              Used to check if the values written to Index and Bank registers
342  *              are out of range.  Normally, the value is simply truncated
343  *              to fit the field, but this case is most likely a serious
344  *              coding error in the ASL.
345  *
346  ******************************************************************************/
347
348 BOOLEAN
349 AcpiExRegisterOverflow (
350     ACPI_OPERAND_OBJECT     *ObjDesc,
351     ACPI_INTEGER            Value)
352 {
353
354     if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
355     {
356         /*
357          * The field is large enough to hold the maximum integer, so we can
358          * never overflow it.
359          */
360         return (FALSE);
361     }
362
363     if (Value >= ((ACPI_INTEGER) 1 << ObjDesc->CommonField.BitLength))
364     {
365         /*
366          * The Value is larger than the maximum value that can fit into
367          * the register.
368          */
369         return (TRUE);
370     }
371
372     /* The Value will fit into the field with no truncation */
373
374     return (FALSE);
375 }
376
377
378 /*******************************************************************************
379  *
380  * FUNCTION:    AcpiExFieldDatumIo
381  *
382  * PARAMETERS:  *ObjDesc                - Field to be read
383  *              FieldDatumByteOffset    - Byte offset of this datum within the
384  *                                        parent field
385  *              *Value                  - Where to store value (must be 64 bits)
386  *              ReadWrite               - Read or Write flag
387  *
388  * RETURN:      Status
389  *
390  * DESCRIPTION: Read or Write a single datum of a field.  The FieldType is
391  *              demultiplexed here to handle the different types of fields
392  *              (BufferField, RegionField, IndexField, BankField)
393  *
394  ******************************************************************************/
395
396 ACPI_STATUS
397 AcpiExFieldDatumIo (
398     ACPI_OPERAND_OBJECT     *ObjDesc,
399     UINT32                  FieldDatumByteOffset,
400     ACPI_INTEGER            *Value,
401     UINT32                  ReadWrite)
402 {
403     ACPI_STATUS             Status;
404     ACPI_INTEGER            LocalValue;
405
406
407     ACPI_FUNCTION_TRACE_U32 ("ExFieldDatumIo", FieldDatumByteOffset);
408
409
410     if (ReadWrite == ACPI_READ)
411     {
412         if (!Value)
413         {
414             LocalValue = 0;
415             Value = &LocalValue;  /* To support reads without saving return value */
416         }
417
418         /* Clear the entire return buffer first, [Very Important!] */
419
420         *Value = 0;
421     }
422
423     /*
424      * The four types of fields are:
425      *
426      * BufferFields - Read/write from/to a Buffer
427      * RegionFields - Read/write from/to a Operation Region.
428      * BankFields   - Write to a Bank Register, then read/write from/to an OpRegion
429      * IndexFields  - Write to an Index Register, then read/write from/to a Data Register
430      */
431     switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
432     {
433     case ACPI_TYPE_BUFFER_FIELD:
434         /*
435          * If the BufferField arguments have not been previously evaluated,
436          * evaluate them now and save the results.
437          */
438         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
439         {
440             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
441             if (ACPI_FAILURE (Status))
442             {
443                 return_ACPI_STATUS (Status);
444             }
445         }
446
447         if (ReadWrite == ACPI_READ)
448         {
449             /*
450              * Copy the data from the source buffer.
451              * Length is the field width in bytes.
452              */
453             ACPI_MEMCPY (Value, (ObjDesc->BufferField.BufferObj)->Buffer.Pointer
454                             + ObjDesc->BufferField.BaseByteOffset
455                             + FieldDatumByteOffset,
456                             ObjDesc->CommonField.AccessByteWidth);
457         }
458         else
459         {
460             /*
461              * Copy the data to the target buffer.
462              * Length is the field width in bytes.
463              */
464             ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer
465                     + ObjDesc->BufferField.BaseByteOffset
466                     + FieldDatumByteOffset,
467                     Value, ObjDesc->CommonField.AccessByteWidth);
468         }
469
470         Status = AE_OK;
471         break;
472
473
474     case ACPI_TYPE_LOCAL_BANK_FIELD:
475
476         /* Ensure that the BankValue is not beyond the capacity of the register */
477
478         if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
479                                     (ACPI_INTEGER) ObjDesc->BankField.Value))
480         {
481             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
482         }
483
484         /*
485          * For BankFields, we must write the BankValue to the BankRegister
486          * (itself a RegionField) before we can access the data.
487          */
488         Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
489                                 &ObjDesc->BankField.Value,
490                                 sizeof (ObjDesc->BankField.Value));
491         if (ACPI_FAILURE (Status))
492         {
493             return_ACPI_STATUS (Status);
494         }
495
496         /*
497          * Now that the Bank has been selected, fall through to the
498          * RegionField case and write the datum to the Operation Region
499          */
500
501         /*lint -fallthrough */
502
503
504     case ACPI_TYPE_LOCAL_REGION_FIELD:
505         /*
506          * For simple RegionFields, we just directly access the owning
507          * Operation Region.
508          */
509         Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
510                         ReadWrite);
511         break;
512
513
514     case ACPI_TYPE_LOCAL_INDEX_FIELD:
515
516
517         /* Ensure that the IndexValue is not beyond the capacity of the register */
518
519         if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
520                                     (ACPI_INTEGER) ObjDesc->IndexField.Value))
521         {
522             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
523         }
524
525         /* Write the index value to the IndexRegister (itself a RegionField) */
526
527         Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
528                                 &ObjDesc->IndexField.Value,
529                                 sizeof (ObjDesc->IndexField.Value));
530         if (ACPI_FAILURE (Status))
531         {
532             return_ACPI_STATUS (Status);
533         }
534
535         if (ReadWrite == ACPI_READ)
536         {
537             /* Read the datum from the DataRegister */
538
539             Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
540                             Value, ObjDesc->CommonField.AccessByteWidth);
541         }
542         else
543         {
544             /* Write the datum to the Data register */
545
546             Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
547                             Value, ObjDesc->CommonField.AccessByteWidth);
548         }
549         break;
550
551
552     default:
553
554         ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "%p, Wrong object type - %s\n",
555             ObjDesc, AcpiUtGetObjectTypeName (ObjDesc)));
556         Status = AE_AML_INTERNAL;
557         break;
558     }
559
560     if (ACPI_SUCCESS (Status))
561     {
562         if (ReadWrite == ACPI_READ)
563         {
564             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Read=%8.8X%8.8X\n",
565                                 ACPI_HIDWORD (*Value), ACPI_LODWORD (*Value)));
566         }
567         else
568         {
569             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Written=%8.8X%8.8X\n",
570                                 ACPI_HIDWORD (*Value), ACPI_LODWORD (*Value)));
571         }
572     }
573
574     return_ACPI_STATUS (Status);
575 }
576
577
578 /*******************************************************************************
579  *
580  * FUNCTION:    AcpiExWriteWithUpdateRule
581  *
582  * PARAMETERS:  *ObjDesc            - Field to be set
583  *              Value               - Value to store
584  *
585  * RETURN:      Status
586  *
587  * DESCRIPTION: Apply the field update rule to a field write
588  *
589  ******************************************************************************/
590
591 ACPI_STATUS
592 AcpiExWriteWithUpdateRule (
593     ACPI_OPERAND_OBJECT     *ObjDesc,
594     ACPI_INTEGER            Mask,
595     ACPI_INTEGER            FieldValue,
596     UINT32                  FieldDatumByteOffset)
597 {
598     ACPI_STATUS             Status = AE_OK;
599     ACPI_INTEGER            MergedValue;
600     ACPI_INTEGER            CurrentValue;
601
602
603     ACPI_FUNCTION_TRACE_U32 ("ExWriteWithUpdateRule", Mask);
604
605
606     /* Start with the new bits  */
607
608     MergedValue = FieldValue;
609
610     /* If the mask is all ones, we don't need to worry about the update rule */
611
612     if (Mask != ACPI_INTEGER_MAX)
613     {
614         /* Decode the update rule */
615
616         switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
617         {
618         case AML_FIELD_UPDATE_PRESERVE:
619             /*
620              * Check if update rule needs to be applied (not if mask is all
621              * ones)  The left shift drops the bits we want to ignore.
622              */
623             if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
624                            ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
625             {
626                 /*
627                  * Read the current contents of the byte/word/dword containing
628                  * the field, and merge with the new field value.
629                  */
630                 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
631                                 &CurrentValue, ACPI_READ);
632                 MergedValue |= (CurrentValue & ~Mask);
633             }
634             break;
635
636         case AML_FIELD_UPDATE_WRITE_AS_ONES:
637
638             /* Set positions outside the field to all ones */
639
640             MergedValue |= ~Mask;
641             break;
642
643         case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
644
645             /* Set positions outside the field to all zeros */
646
647             MergedValue &= Mask;
648             break;
649
650         default:
651             ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
652                 "WriteWithUpdateRule: Unknown UpdateRule setting: %X\n",
653                 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
654             return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
655         }
656     }
657
658     /* Write the merged value */
659
660     Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
661                     &MergedValue, ACPI_WRITE);
662
663     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
664         "Mask %8.8X%8.8X DatumOffset %X Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
665         ACPI_HIDWORD (Mask), ACPI_LODWORD (Mask),
666         FieldDatumByteOffset,
667         ACPI_HIDWORD (FieldValue), ACPI_LODWORD (FieldValue),
668         ACPI_HIDWORD (MergedValue),ACPI_LODWORD (MergedValue)));
669
670     return_ACPI_STATUS (Status);
671 }
672
673
674 /*******************************************************************************
675  *
676  * FUNCTION:    AcpiExGetBufferDatum
677  *
678  * PARAMETERS:  Datum               - Where the Datum is returned
679  *              Buffer              - Raw field buffer
680  *              ByteGranularity     - 1/2/4/8 Granularity of the field
681  *                                    (aka Datum Size)
682  *              Offset              - Datum offset into the buffer
683  *
684  * RETURN:      none
685  *
686  * DESCRIPTION: Get a datum from the buffer according to the buffer field
687  *              byte granularity
688  *
689  ******************************************************************************/
690
691 void
692 AcpiExGetBufferDatum(
693     ACPI_INTEGER            *Datum,
694     void                    *Buffer,
695     UINT32                  ByteGranularity,
696     UINT32                  Offset)
697 {
698
699     ACPI_FUNCTION_ENTRY ();
700
701
702     switch (ByteGranularity)
703     {
704     case ACPI_FIELD_BYTE_GRANULARITY:
705
706         *Datum = ((UINT8 *) Buffer) [Offset];
707         break;
708
709     case ACPI_FIELD_WORD_GRANULARITY:
710
711         ACPI_MOVE_UNALIGNED16_TO_32 (Datum, &(((UINT16 *) Buffer) [Offset]));
712         break;
713
714     case ACPI_FIELD_DWORD_GRANULARITY:
715
716         ACPI_MOVE_UNALIGNED32_TO_32 (Datum, &(((UINT32 *) Buffer) [Offset]));
717         break;
718
719     case ACPI_FIELD_QWORD_GRANULARITY:
720
721         ACPI_MOVE_UNALIGNED64_TO_64 (Datum, &(((UINT64 *) Buffer) [Offset]));
722         break;
723
724     default:
725         /* Should not get here */
726         break;
727     }
728 }
729
730
731 /*******************************************************************************
732  *
733  * FUNCTION:    AcpiExSetBufferDatum
734  *
735  * PARAMETERS:  MergedDatum         - Value to store
736  *              Buffer              - Receiving buffer
737  *              ByteGranularity     - 1/2/4/8 Granularity of the field
738  *                                    (aka Datum Size)
739  *              Offset              - Datum offset into the buffer
740  *
741  * RETURN:      none
742  *
743  * DESCRIPTION: Store the merged datum to the buffer according to the
744  *              byte granularity
745  *
746  ******************************************************************************/
747
748 void
749 AcpiExSetBufferDatum (
750     ACPI_INTEGER            MergedDatum,
751     void                    *Buffer,
752     UINT32                  ByteGranularity,
753     UINT32                  Offset)
754 {
755
756     ACPI_FUNCTION_ENTRY ();
757
758
759     switch (ByteGranularity)
760     {
761     case ACPI_FIELD_BYTE_GRANULARITY:
762
763         ((UINT8 *) Buffer) [Offset] = (UINT8) MergedDatum;
764         break;
765
766     case ACPI_FIELD_WORD_GRANULARITY:
767
768         ACPI_MOVE_UNALIGNED16_TO_16 (&(((UINT16 *) Buffer)[Offset]), &MergedDatum);
769         break;
770
771     case ACPI_FIELD_DWORD_GRANULARITY:
772
773         ACPI_MOVE_UNALIGNED32_TO_32 (&(((UINT32 *) Buffer)[Offset]), &MergedDatum);
774         break;
775
776     case ACPI_FIELD_QWORD_GRANULARITY:
777
778         ACPI_MOVE_UNALIGNED64_TO_64 (&(((UINT64 *) Buffer)[Offset]), &MergedDatum);
779         break;
780
781     default:
782         /* Should not get here */
783         break;
784     }
785 }
786
787
788 /*******************************************************************************
789  *
790  * FUNCTION:    AcpiExExtractFromField
791  *
792  * PARAMETERS:  *ObjDesc            - Field to be read
793  *              *Value              - Where to store value
794  *
795  * RETURN:      Status
796  *
797  * DESCRIPTION: Retrieve the value of the given field
798  *
799  ******************************************************************************/
800
801 ACPI_STATUS
802 AcpiExExtractFromField (
803     ACPI_OPERAND_OBJECT     *ObjDesc,
804     void                    *Buffer,
805     UINT32                  BufferLength)
806 {
807     ACPI_STATUS             Status;
808     UINT32                  FieldDatumByteOffset;
809     UINT32                  DatumOffset;
810     ACPI_INTEGER            PreviousRawDatum;
811     ACPI_INTEGER            ThisRawDatum = 0;
812     ACPI_INTEGER            MergedDatum = 0;
813     UINT32                  ByteFieldLength;
814     UINT32                  DatumCount;
815
816
817     ACPI_FUNCTION_TRACE ("ExExtractFromField");
818
819
820     /*
821      * The field must fit within the caller's buffer
822      */
823     ByteFieldLength = ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
824     if (ByteFieldLength > BufferLength)
825     {
826         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
827             "Field size %X (bytes) too large for buffer (%X)\n",
828             ByteFieldLength, BufferLength));
829
830         return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
831     }
832
833     /* Convert field byte count to datum count, round up if necessary */
834
835     DatumCount = ACPI_ROUND_UP_TO (ByteFieldLength,
836                               ObjDesc->CommonField.AccessByteWidth);
837
838     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
839         "ByteLen=%X, DatumLen=%X, ByteGran=%X\n",
840         ByteFieldLength, DatumCount,ObjDesc->CommonField.AccessByteWidth));
841
842     /*
843      * Clear the caller's buffer (the whole buffer length as given)
844      * This is very important, especially in the cases where a byte is read,
845      * but the buffer is really a UINT32 (4 bytes).
846      */
847     ACPI_MEMSET (Buffer, 0, BufferLength);
848
849     /* Read the first raw datum to prime the loop */
850
851     FieldDatumByteOffset = 0;
852     DatumOffset= 0;
853
854     Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
855                     &PreviousRawDatum, ACPI_READ);
856     if (ACPI_FAILURE (Status))
857     {
858         return_ACPI_STATUS (Status);
859     }
860
861
862     /* We might actually be done if the request fits in one datum */
863
864     if ((DatumCount == 1) &&
865         (ObjDesc->CommonField.Flags & AOPOBJ_SINGLE_DATUM))
866     {
867         /* 1) Shift the valid data bits down to start at bit 0 */
868
869         MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
870
871         /* 2) Mask off any upper unused bits (bits not part of the field) */
872
873         if (ObjDesc->CommonField.EndBufferValidBits)
874         {
875             MergedDatum &= ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
876         }
877
878         /* Store the datum to the caller buffer */
879
880         AcpiExSetBufferDatum (MergedDatum, Buffer, ObjDesc->CommonField.AccessByteWidth,
881                 DatumOffset);
882
883         return_ACPI_STATUS (AE_OK);
884     }
885
886
887     /* We need to get more raw data to complete one or more field data */
888
889     while (DatumOffset < DatumCount)
890     {
891         FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
892
893         /*
894          * If the field is aligned on a byte boundary, we don't want
895          * to perform a final read, since this would potentially read
896          * past the end of the region.
897          *
898          * We could just split the aligned and non-aligned cases since the
899          * aligned case is so very simple, but this would require more code.
900          */
901         if ((ObjDesc->CommonField.StartFieldBitOffset != 0)  ||
902             ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
903             (DatumOffset < (DatumCount -1))))
904         {
905             /*
906              * Get the next raw datum, it contains some or all bits
907              * of the current field datum
908              */
909             Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
910                             &ThisRawDatum, ACPI_READ);
911             if (ACPI_FAILURE (Status))
912             {
913                 return_ACPI_STATUS (Status);
914             }
915         }
916
917         /*
918          * Create the (possibly) merged datum to be stored to the caller buffer
919          */
920         if (ObjDesc->CommonField.StartFieldBitOffset == 0)
921         {
922             /* Field is not skewed and we can just copy the datum */
923
924             MergedDatum = PreviousRawDatum;
925         }
926         else
927         {
928             /*
929              * Put together the appropriate bits of the two raw data to make a
930              * single complete field datum
931              *
932              * 1) Normalize the first datum down to bit 0
933              */
934             MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
935
936             /* 2) Insert the second datum "above" the first datum */
937
938             MergedDatum |= (ThisRawDatum << ObjDesc->CommonField.DatumValidBits);
939
940             if ((DatumOffset >= (DatumCount -1)))
941             {
942                 /*
943                  * This is the last iteration of the loop.  We need to clear
944                  * any unused bits (bits that are not part of this field) that
945                  * came from the last raw datum before we store the final
946                  * merged datum into the caller buffer.
947                  */
948                 if (ObjDesc->CommonField.EndBufferValidBits)
949                 {
950                     MergedDatum &=
951                         ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
952                 }
953             }
954         }
955
956         /*
957          * Store the merged field datum in the caller's buffer, according to
958          * the granularity of the field (size of each datum).
959          */
960         AcpiExSetBufferDatum (MergedDatum, Buffer,
961                 ObjDesc->CommonField.AccessByteWidth, DatumOffset);
962
963         /*
964          * Save the raw datum that was just acquired since it may contain bits
965          * of the *next* field datum.  Update offsets
966          */
967         PreviousRawDatum = ThisRawDatum;
968         DatumOffset++;
969     }
970
971     return_ACPI_STATUS (AE_OK);
972 }
973
974
975 /*******************************************************************************
976  *
977  * FUNCTION:    AcpiExInsertIntoField
978  *
979  * PARAMETERS:  *ObjDesc            - Field to be set
980  *              Buffer              - Value to store
981  *
982  * RETURN:      Status
983  *
984  * DESCRIPTION: Store the value into the given field
985  *
986  ******************************************************************************/
987
988 ACPI_STATUS
989 AcpiExInsertIntoField (
990     ACPI_OPERAND_OBJECT     *ObjDesc,
991     void                    *Buffer,
992     UINT32                  BufferLength)
993 {
994     ACPI_STATUS             Status;
995     UINT32                  FieldDatumByteOffset;
996     UINT32                  DatumOffset;
997     ACPI_INTEGER            Mask;
998     ACPI_INTEGER            MergedDatum;
999     ACPI_INTEGER            PreviousRawDatum;
1000     ACPI_INTEGER            ThisRawDatum;
1001     UINT32                  ByteFieldLength;
1002     UINT32                  DatumCount;
1003
1004
1005     ACPI_FUNCTION_TRACE ("ExInsertIntoField");
1006
1007
1008     /*
1009      * Incoming buffer must be at least as long as the field, we do not
1010      * allow "partial" field writes.  We do not care if the buffer is
1011      * larger than the field, this typically happens when an integer is
1012      * written to a field that is actually smaller than an integer.
1013      */
1014     ByteFieldLength = ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
1015     if (BufferLength < ByteFieldLength)
1016     {
1017         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Buffer length %X too small for field %X\n",
1018             BufferLength, ByteFieldLength));
1019
1020         return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
1021     }
1022
1023     /* Convert byte count to datum count, round up if necessary */
1024
1025     DatumCount = ACPI_ROUND_UP_TO (ByteFieldLength, ObjDesc->CommonField.AccessByteWidth);
1026
1027     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
1028         "ByteLen=%X, DatumLen=%X, ByteGran=%X\n",
1029         ByteFieldLength, DatumCount, ObjDesc->CommonField.AccessByteWidth));
1030
1031     /*
1032      * Break the request into up to three parts (similar to an I/O request):
1033      * 1) non-aligned part at start
1034      * 2) aligned part in middle
1035      * 3) non-aligned part at the end
1036      */
1037     FieldDatumByteOffset = 0;
1038     DatumOffset= 0;
1039
1040     /* Get a single datum from the caller's buffer */
1041
1042     AcpiExGetBufferDatum (&PreviousRawDatum, Buffer,
1043             ObjDesc->CommonField.AccessByteWidth, DatumOffset);
1044
1045     /*
1046      * Part1:
1047      * Write a partial field datum if field does not begin on a datum boundary
1048      * Note: The code in this section also handles the aligned case
1049      *
1050      * Construct Mask with 1 bits where the field is, 0 bits elsewhere
1051      * (Only the bottom 5 bits of BitLength are valid for a shift operation)
1052      *
1053      * Mask off bits that are "below" the field (if any)
1054      */
1055     Mask = ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
1056
1057     /* If the field fits in one datum, may need to mask upper bits */
1058
1059     if ((ObjDesc->CommonField.Flags & AOPOBJ_SINGLE_DATUM) &&
1060          ObjDesc->CommonField.EndFieldValidBits)
1061     {
1062         /* There are bits above the field, mask them off also */
1063
1064         Mask &= ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
1065     }
1066
1067     /* Shift and mask the value into the field position */
1068
1069     MergedDatum = (PreviousRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
1070     MergedDatum &= Mask;
1071
1072     /* Apply the update rule (if necessary) and write the datum to the field */
1073
1074     Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask, MergedDatum,
1075                         FieldDatumByteOffset);
1076     if (ACPI_FAILURE (Status))
1077     {
1078         return_ACPI_STATUS (Status);
1079     }
1080
1081     /* If the entire field fits within one datum, we are done. */
1082
1083     if ((DatumCount == 1) &&
1084        (ObjDesc->CommonField.Flags & AOPOBJ_SINGLE_DATUM))
1085     {
1086         return_ACPI_STATUS (AE_OK);
1087     }
1088
1089     /*
1090      * Part2:
1091      * Write the aligned data.
1092      *
1093      * We don't need to worry about the update rule for these data, because
1094      * all of the bits in each datum are part of the field.
1095      *
1096      * The last datum must be special cased because it might contain bits
1097      * that are not part of the field -- therefore the "update rule" must be
1098      * applied in Part3 below.
1099      */
1100     while (DatumOffset < DatumCount)
1101     {
1102         DatumOffset++;
1103         FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
1104
1105         /*
1106          * Get the next raw buffer datum.  It may contain bits of the previous
1107          * field datum
1108          */
1109         AcpiExGetBufferDatum (&ThisRawDatum, Buffer,
1110                 ObjDesc->CommonField.AccessByteWidth, DatumOffset);
1111
1112         /* Create the field datum based on the field alignment */
1113
1114         if (ObjDesc->CommonField.StartFieldBitOffset != 0)
1115         {
1116             /*
1117              * Put together appropriate bits of the two raw buffer data to make
1118              * a single complete field datum
1119              */
1120             MergedDatum =
1121                 (PreviousRawDatum >> ObjDesc->CommonField.DatumValidBits) |
1122                 (ThisRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
1123         }
1124         else
1125         {
1126             /* Field began aligned on datum boundary */
1127
1128             MergedDatum = ThisRawDatum;
1129         }
1130
1131         /*
1132          * Special handling for the last datum if the field does NOT end on
1133          * a datum boundary.  Update Rule must be applied to the bits outside
1134          * the field.
1135          */
1136         if (DatumOffset == DatumCount)
1137         {
1138             /*
1139              * If there are dangling non-aligned bits, perform one more merged write
1140              * Else - field is aligned at the end, no need for any more writes
1141              */
1142             if (ObjDesc->CommonField.EndFieldValidBits)
1143             {
1144                 /*
1145                  * Part3:
1146                  * This is the last datum and the field does not end on a datum boundary.
1147                  * Build the partial datum and write with the update rule.
1148                  *
1149                  * Mask off the unused bits above (after) the end-of-field
1150                  */
1151                 Mask = ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
1152                 MergedDatum &= Mask;
1153
1154                 /* Write the last datum with the update rule */
1155
1156                 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask, MergedDatum,
1157                                     FieldDatumByteOffset);
1158                 if (ACPI_FAILURE (Status))
1159                 {
1160                     return_ACPI_STATUS (Status);
1161                 }
1162             }
1163         }
1164         else
1165         {
1166             /* Normal case -- write the completed datum */
1167
1168             Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
1169                             &MergedDatum, ACPI_WRITE);
1170             if (ACPI_FAILURE (Status))
1171             {
1172                 return_ACPI_STATUS (Status);
1173             }
1174         }
1175
1176         /*
1177          * Save the most recent datum since it may contain bits of the *next*
1178          * field datum.  Update current byte offset.
1179          */
1180         PreviousRawDatum = ThisRawDatum;
1181     }
1182
1183     return_ACPI_STATUS (Status);
1184 }
1185
1186