1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2003, Intel Corp.
13 * All rights reserved.
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
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
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;
38 * The above copyright and patent license is granted only if the following
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.
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
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
73 * 3.4. Intel retains all right, title, and interest in and to the Original
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.
81 * 4. Disclaimer and Export Compliance
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
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
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.
115 *****************************************************************************/
116 /* $DragonFly: src/sys/contrib/dev/acpica/Attic/exfldio.c,v 1.1 2003/09/24 03:32:16 drhodus Exp $ */
119 #define __EXFLDIO_C__
122 #include "acinterp.h"
124 #include "acevents.h"
125 #include "acdispat.h"
128 #define _COMPONENT ACPI_EXECUTER
129 ACPI_MODULE_NAME ("exfldio")
132 /*******************************************************************************
134 * FUNCTION: AcpiExSetupRegion
136 * PARAMETERS: *ObjDesc - Field to be read or written
137 * FieldDatumByteOffset - Byte offset of this datum within the
142 * DESCRIPTION: Common processing for AcpiExExtractFromField and
143 * AcpiExInsertIntoField. Initialize the
145 ******************************************************************************/
149 ACPI_OPERAND_OBJECT *ObjDesc,
150 UINT32 FieldDatumByteOffset)
152 ACPI_STATUS Status = AE_OK;
153 ACPI_OPERAND_OBJECT *RgnDesc;
156 ACPI_FUNCTION_TRACE_U32 ("ExSetupRegion", FieldDatumByteOffset);
159 RgnDesc = ObjDesc->CommonField.RegionObj;
161 /* We must have a valid region */
163 if (ACPI_GET_OBJECT_TYPE (RgnDesc) != ACPI_TYPE_REGION)
165 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %X (%s)\n",
166 ACPI_GET_OBJECT_TYPE (RgnDesc),
167 AcpiUtGetObjectTypeName (RgnDesc)));
169 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
173 * If the Region Address and Length have not been previously evaluated,
174 * evaluate them now and save the results.
176 if (!(RgnDesc->Region.Flags & AOPOBJ_DATA_VALID))
178 Status = AcpiDsGetRegionArguments (RgnDesc);
179 if (ACPI_FAILURE (Status))
181 return_ACPI_STATUS (Status);
185 if (RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
187 /* SMBus has a non-linear address space */
189 return_ACPI_STATUS (AE_OK);
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)
197 if (RgnDesc->Region.Length < (ObjDesc->CommonField.BaseByteOffset
198 + FieldDatumByteOffset
199 + ObjDesc->CommonField.AccessByteWidth))
201 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
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.
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));
215 * Offset rounded up to next multiple of field width
216 * exceeds region length, indicate an error
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));
224 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
227 return_ACPI_STATUS (AE_OK);
231 /*******************************************************************************
233 * FUNCTION: AcpiExAccessRegion
235 * PARAMETERS: *ObjDesc - Field to be read
236 * FieldDatumByteOffset - Byte offset of this datum within the
238 * *Value - Where to store value (must at least
239 * the size of ACPI_INTEGER)
240 * Function - Read or Write flag plus other region-
245 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
247 ******************************************************************************/
251 ACPI_OPERAND_OBJECT *ObjDesc,
252 UINT32 FieldDatumByteOffset,
257 ACPI_OPERAND_OBJECT *RgnDesc;
258 ACPI_PHYSICAL_ADDRESS Address;
261 ACPI_FUNCTION_TRACE ("ExAccessRegion");
265 * Ensure that the region operands are fully evaluated and verify
266 * the validity of the request
268 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
269 if (ACPI_FAILURE (Status))
271 return_ACPI_STATUS (Status);
275 * The physical address of this field datum is:
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
281 RgnDesc = ObjDesc->CommonField.RegionObj;
282 Address = RgnDesc->Region.Address
283 + ObjDesc->CommonField.BaseByteOffset
284 + FieldDatumByteOffset;
286 if ((Function & ACPI_IO_MASK) == ACPI_READ)
288 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
292 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
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)));
304 /* Invoke the appropriate AddressSpace/OpRegion handler */
306 Status = AcpiEvAddressSpaceDispatch (RgnDesc, Function,
307 Address, ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
309 if (ACPI_FAILURE (Status))
311 if (Status == AE_NOT_IMPLEMENTED)
313 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
314 "Region %s(%X) not implemented\n",
315 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
316 RgnDesc->Region.SpaceId));
318 else if (Status == AE_NOT_EXIST)
320 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
321 "Region %s(%X) has no handler\n",
322 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
323 RgnDesc->Region.SpaceId));
327 return_ACPI_STATUS (Status);
331 /*******************************************************************************
333 * FUNCTION: AcpiExRegisterOverflow
335 * PARAMETERS: *ObjDesc - Register(Field) to be written
336 * Value - Value to be stored
338 * RETURN: TRUE if value overflows the field, FALSE otherwise
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.
346 ******************************************************************************/
349 AcpiExRegisterOverflow (
350 ACPI_OPERAND_OBJECT *ObjDesc,
354 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
357 * The field is large enough to hold the maximum integer, so we can
363 if (Value >= ((ACPI_INTEGER) 1 << ObjDesc->CommonField.BitLength))
366 * The Value is larger than the maximum value that can fit into
372 /* The Value will fit into the field with no truncation */
378 /*******************************************************************************
380 * FUNCTION: AcpiExFieldDatumIo
382 * PARAMETERS: *ObjDesc - Field to be read
383 * FieldDatumByteOffset - Byte offset of this datum within the
385 * *Value - Where to store value (must be 64 bits)
386 * ReadWrite - Read or Write flag
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)
394 ******************************************************************************/
398 ACPI_OPERAND_OBJECT *ObjDesc,
399 UINT32 FieldDatumByteOffset,
404 ACPI_INTEGER LocalValue;
407 ACPI_FUNCTION_TRACE_U32 ("ExFieldDatumIo", FieldDatumByteOffset);
410 if (ReadWrite == ACPI_READ)
415 Value = &LocalValue; /* To support reads without saving return value */
418 /* Clear the entire return buffer first, [Very Important!] */
424 * The four types of fields are:
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
431 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
433 case ACPI_TYPE_BUFFER_FIELD:
435 * If the BufferField arguments have not been previously evaluated,
436 * evaluate them now and save the results.
438 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
440 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
441 if (ACPI_FAILURE (Status))
443 return_ACPI_STATUS (Status);
447 if (ReadWrite == ACPI_READ)
450 * Copy the data from the source buffer.
451 * Length is the field width in bytes.
453 ACPI_MEMCPY (Value, (ObjDesc->BufferField.BufferObj)->Buffer.Pointer
454 + ObjDesc->BufferField.BaseByteOffset
455 + FieldDatumByteOffset,
456 ObjDesc->CommonField.AccessByteWidth);
461 * Copy the data to the target buffer.
462 * Length is the field width in bytes.
464 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer
465 + ObjDesc->BufferField.BaseByteOffset
466 + FieldDatumByteOffset,
467 Value, ObjDesc->CommonField.AccessByteWidth);
474 case ACPI_TYPE_LOCAL_BANK_FIELD:
476 /* Ensure that the BankValue is not beyond the capacity of the register */
478 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
479 (ACPI_INTEGER) ObjDesc->BankField.Value))
481 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
485 * For BankFields, we must write the BankValue to the BankRegister
486 * (itself a RegionField) before we can access the data.
488 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
489 &ObjDesc->BankField.Value,
490 sizeof (ObjDesc->BankField.Value));
491 if (ACPI_FAILURE (Status))
493 return_ACPI_STATUS (Status);
497 * Now that the Bank has been selected, fall through to the
498 * RegionField case and write the datum to the Operation Region
501 /*lint -fallthrough */
504 case ACPI_TYPE_LOCAL_REGION_FIELD:
506 * For simple RegionFields, we just directly access the owning
509 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
514 case ACPI_TYPE_LOCAL_INDEX_FIELD:
517 /* Ensure that the IndexValue is not beyond the capacity of the register */
519 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
520 (ACPI_INTEGER) ObjDesc->IndexField.Value))
522 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
525 /* Write the index value to the IndexRegister (itself a RegionField) */
527 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
528 &ObjDesc->IndexField.Value,
529 sizeof (ObjDesc->IndexField.Value));
530 if (ACPI_FAILURE (Status))
532 return_ACPI_STATUS (Status);
535 if (ReadWrite == ACPI_READ)
537 /* Read the datum from the DataRegister */
539 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
540 Value, ObjDesc->CommonField.AccessByteWidth);
544 /* Write the datum to the Data register */
546 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
547 Value, ObjDesc->CommonField.AccessByteWidth);
554 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "%p, Wrong object type - %s\n",
555 ObjDesc, AcpiUtGetObjectTypeName (ObjDesc)));
556 Status = AE_AML_INTERNAL;
560 if (ACPI_SUCCESS (Status))
562 if (ReadWrite == ACPI_READ)
564 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Read=%8.8X%8.8X\n",
565 ACPI_HIDWORD (*Value), ACPI_LODWORD (*Value)));
569 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Written=%8.8X%8.8X\n",
570 ACPI_HIDWORD (*Value), ACPI_LODWORD (*Value)));
574 return_ACPI_STATUS (Status);
578 /*******************************************************************************
580 * FUNCTION: AcpiExWriteWithUpdateRule
582 * PARAMETERS: *ObjDesc - Field to be set
583 * Value - Value to store
587 * DESCRIPTION: Apply the field update rule to a field write
589 ******************************************************************************/
592 AcpiExWriteWithUpdateRule (
593 ACPI_OPERAND_OBJECT *ObjDesc,
595 ACPI_INTEGER FieldValue,
596 UINT32 FieldDatumByteOffset)
598 ACPI_STATUS Status = AE_OK;
599 ACPI_INTEGER MergedValue;
600 ACPI_INTEGER CurrentValue;
603 ACPI_FUNCTION_TRACE_U32 ("ExWriteWithUpdateRule", Mask);
606 /* Start with the new bits */
608 MergedValue = FieldValue;
610 /* If the mask is all ones, we don't need to worry about the update rule */
612 if (Mask != ACPI_INTEGER_MAX)
614 /* Decode the update rule */
616 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
618 case AML_FIELD_UPDATE_PRESERVE:
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.
623 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
624 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
627 * Read the current contents of the byte/word/dword containing
628 * the field, and merge with the new field value.
630 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
631 &CurrentValue, ACPI_READ);
632 MergedValue |= (CurrentValue & ~Mask);
636 case AML_FIELD_UPDATE_WRITE_AS_ONES:
638 /* Set positions outside the field to all ones */
640 MergedValue |= ~Mask;
643 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
645 /* Set positions outside the field to all zeros */
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);
658 /* Write the merged value */
660 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
661 &MergedValue, ACPI_WRITE);
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)));
670 return_ACPI_STATUS (Status);
674 /*******************************************************************************
676 * FUNCTION: AcpiExGetBufferDatum
678 * PARAMETERS: Datum - Where the Datum is returned
679 * Buffer - Raw field buffer
680 * ByteGranularity - 1/2/4/8 Granularity of the field
682 * Offset - Datum offset into the buffer
686 * DESCRIPTION: Get a datum from the buffer according to the buffer field
689 ******************************************************************************/
692 AcpiExGetBufferDatum(
695 UINT32 ByteGranularity,
699 ACPI_FUNCTION_ENTRY ();
702 switch (ByteGranularity)
704 case ACPI_FIELD_BYTE_GRANULARITY:
706 *Datum = ((UINT8 *) Buffer) [Offset];
709 case ACPI_FIELD_WORD_GRANULARITY:
711 ACPI_MOVE_UNALIGNED16_TO_32 (Datum, &(((UINT16 *) Buffer) [Offset]));
714 case ACPI_FIELD_DWORD_GRANULARITY:
716 ACPI_MOVE_UNALIGNED32_TO_32 (Datum, &(((UINT32 *) Buffer) [Offset]));
719 case ACPI_FIELD_QWORD_GRANULARITY:
721 ACPI_MOVE_UNALIGNED64_TO_64 (Datum, &(((UINT64 *) Buffer) [Offset]));
725 /* Should not get here */
731 /*******************************************************************************
733 * FUNCTION: AcpiExSetBufferDatum
735 * PARAMETERS: MergedDatum - Value to store
736 * Buffer - Receiving buffer
737 * ByteGranularity - 1/2/4/8 Granularity of the field
739 * Offset - Datum offset into the buffer
743 * DESCRIPTION: Store the merged datum to the buffer according to the
746 ******************************************************************************/
749 AcpiExSetBufferDatum (
750 ACPI_INTEGER MergedDatum,
752 UINT32 ByteGranularity,
756 ACPI_FUNCTION_ENTRY ();
759 switch (ByteGranularity)
761 case ACPI_FIELD_BYTE_GRANULARITY:
763 ((UINT8 *) Buffer) [Offset] = (UINT8) MergedDatum;
766 case ACPI_FIELD_WORD_GRANULARITY:
768 ACPI_MOVE_UNALIGNED16_TO_16 (&(((UINT16 *) Buffer)[Offset]), &MergedDatum);
771 case ACPI_FIELD_DWORD_GRANULARITY:
773 ACPI_MOVE_UNALIGNED32_TO_32 (&(((UINT32 *) Buffer)[Offset]), &MergedDatum);
776 case ACPI_FIELD_QWORD_GRANULARITY:
778 ACPI_MOVE_UNALIGNED64_TO_64 (&(((UINT64 *) Buffer)[Offset]), &MergedDatum);
782 /* Should not get here */
788 /*******************************************************************************
790 * FUNCTION: AcpiExExtractFromField
792 * PARAMETERS: *ObjDesc - Field to be read
793 * *Value - Where to store value
797 * DESCRIPTION: Retrieve the value of the given field
799 ******************************************************************************/
802 AcpiExExtractFromField (
803 ACPI_OPERAND_OBJECT *ObjDesc,
808 UINT32 FieldDatumByteOffset;
810 ACPI_INTEGER PreviousRawDatum;
811 ACPI_INTEGER ThisRawDatum = 0;
812 ACPI_INTEGER MergedDatum = 0;
813 UINT32 ByteFieldLength;
817 ACPI_FUNCTION_TRACE ("ExExtractFromField");
821 * The field must fit within the caller's buffer
823 ByteFieldLength = ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
824 if (ByteFieldLength > BufferLength)
826 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
827 "Field size %X (bytes) too large for buffer (%X)\n",
828 ByteFieldLength, BufferLength));
830 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
833 /* Convert field byte count to datum count, round up if necessary */
835 DatumCount = ACPI_ROUND_UP_TO (ByteFieldLength,
836 ObjDesc->CommonField.AccessByteWidth);
838 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
839 "ByteLen=%X, DatumLen=%X, ByteGran=%X\n",
840 ByteFieldLength, DatumCount,ObjDesc->CommonField.AccessByteWidth));
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).
847 ACPI_MEMSET (Buffer, 0, BufferLength);
849 /* Read the first raw datum to prime the loop */
851 FieldDatumByteOffset = 0;
854 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
855 &PreviousRawDatum, ACPI_READ);
856 if (ACPI_FAILURE (Status))
858 return_ACPI_STATUS (Status);
862 /* We might actually be done if the request fits in one datum */
864 if ((DatumCount == 1) &&
865 (ObjDesc->CommonField.Flags & AOPOBJ_SINGLE_DATUM))
867 /* 1) Shift the valid data bits down to start at bit 0 */
869 MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
871 /* 2) Mask off any upper unused bits (bits not part of the field) */
873 if (ObjDesc->CommonField.EndBufferValidBits)
875 MergedDatum &= ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
878 /* Store the datum to the caller buffer */
880 AcpiExSetBufferDatum (MergedDatum, Buffer, ObjDesc->CommonField.AccessByteWidth,
883 return_ACPI_STATUS (AE_OK);
887 /* We need to get more raw data to complete one or more field data */
889 while (DatumOffset < DatumCount)
891 FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
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.
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.
901 if ((ObjDesc->CommonField.StartFieldBitOffset != 0) ||
902 ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
903 (DatumOffset < (DatumCount -1))))
906 * Get the next raw datum, it contains some or all bits
907 * of the current field datum
909 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
910 &ThisRawDatum, ACPI_READ);
911 if (ACPI_FAILURE (Status))
913 return_ACPI_STATUS (Status);
918 * Create the (possibly) merged datum to be stored to the caller buffer
920 if (ObjDesc->CommonField.StartFieldBitOffset == 0)
922 /* Field is not skewed and we can just copy the datum */
924 MergedDatum = PreviousRawDatum;
929 * Put together the appropriate bits of the two raw data to make a
930 * single complete field datum
932 * 1) Normalize the first datum down to bit 0
934 MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
936 /* 2) Insert the second datum "above" the first datum */
938 MergedDatum |= (ThisRawDatum << ObjDesc->CommonField.DatumValidBits);
940 if ((DatumOffset >= (DatumCount -1)))
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.
948 if (ObjDesc->CommonField.EndBufferValidBits)
951 ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
957 * Store the merged field datum in the caller's buffer, according to
958 * the granularity of the field (size of each datum).
960 AcpiExSetBufferDatum (MergedDatum, Buffer,
961 ObjDesc->CommonField.AccessByteWidth, DatumOffset);
964 * Save the raw datum that was just acquired since it may contain bits
965 * of the *next* field datum. Update offsets
967 PreviousRawDatum = ThisRawDatum;
971 return_ACPI_STATUS (AE_OK);
975 /*******************************************************************************
977 * FUNCTION: AcpiExInsertIntoField
979 * PARAMETERS: *ObjDesc - Field to be set
980 * Buffer - Value to store
984 * DESCRIPTION: Store the value into the given field
986 ******************************************************************************/
989 AcpiExInsertIntoField (
990 ACPI_OPERAND_OBJECT *ObjDesc,
995 UINT32 FieldDatumByteOffset;
998 ACPI_INTEGER MergedDatum;
999 ACPI_INTEGER PreviousRawDatum;
1000 ACPI_INTEGER ThisRawDatum;
1001 UINT32 ByteFieldLength;
1005 ACPI_FUNCTION_TRACE ("ExInsertIntoField");
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.
1014 ByteFieldLength = ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
1015 if (BufferLength < ByteFieldLength)
1017 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Buffer length %X too small for field %X\n",
1018 BufferLength, ByteFieldLength));
1020 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
1023 /* Convert byte count to datum count, round up if necessary */
1025 DatumCount = ACPI_ROUND_UP_TO (ByteFieldLength, ObjDesc->CommonField.AccessByteWidth);
1027 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
1028 "ByteLen=%X, DatumLen=%X, ByteGran=%X\n",
1029 ByteFieldLength, DatumCount, ObjDesc->CommonField.AccessByteWidth));
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
1037 FieldDatumByteOffset = 0;
1040 /* Get a single datum from the caller's buffer */
1042 AcpiExGetBufferDatum (&PreviousRawDatum, Buffer,
1043 ObjDesc->CommonField.AccessByteWidth, DatumOffset);
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
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)
1053 * Mask off bits that are "below" the field (if any)
1055 Mask = ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
1057 /* If the field fits in one datum, may need to mask upper bits */
1059 if ((ObjDesc->CommonField.Flags & AOPOBJ_SINGLE_DATUM) &&
1060 ObjDesc->CommonField.EndFieldValidBits)
1062 /* There are bits above the field, mask them off also */
1064 Mask &= ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
1067 /* Shift and mask the value into the field position */
1069 MergedDatum = (PreviousRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
1070 MergedDatum &= Mask;
1072 /* Apply the update rule (if necessary) and write the datum to the field */
1074 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask, MergedDatum,
1075 FieldDatumByteOffset);
1076 if (ACPI_FAILURE (Status))
1078 return_ACPI_STATUS (Status);
1081 /* If the entire field fits within one datum, we are done. */
1083 if ((DatumCount == 1) &&
1084 (ObjDesc->CommonField.Flags & AOPOBJ_SINGLE_DATUM))
1086 return_ACPI_STATUS (AE_OK);
1091 * Write the aligned data.
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.
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.
1100 while (DatumOffset < DatumCount)
1103 FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
1106 * Get the next raw buffer datum. It may contain bits of the previous
1109 AcpiExGetBufferDatum (&ThisRawDatum, Buffer,
1110 ObjDesc->CommonField.AccessByteWidth, DatumOffset);
1112 /* Create the field datum based on the field alignment */
1114 if (ObjDesc->CommonField.StartFieldBitOffset != 0)
1117 * Put together appropriate bits of the two raw buffer data to make
1118 * a single complete field datum
1121 (PreviousRawDatum >> ObjDesc->CommonField.DatumValidBits) |
1122 (ThisRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
1126 /* Field began aligned on datum boundary */
1128 MergedDatum = ThisRawDatum;
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
1136 if (DatumOffset == DatumCount)
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
1142 if (ObjDesc->CommonField.EndFieldValidBits)
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.
1149 * Mask off the unused bits above (after) the end-of-field
1151 Mask = ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
1152 MergedDatum &= Mask;
1154 /* Write the last datum with the update rule */
1156 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask, MergedDatum,
1157 FieldDatumByteOffset);
1158 if (ACPI_FAILURE (Status))
1160 return_ACPI_STATUS (Status);
1166 /* Normal case -- write the completed datum */
1168 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
1169 &MergedDatum, ACPI_WRITE);
1170 if (ACPI_FAILURE (Status))
1172 return_ACPI_STATUS (Status);
1177 * Save the most recent datum since it may contain bits of the *next*
1178 * field datum. Update current byte offset.
1180 PreviousRawDatum = ThisRawDatum;
1183 return_ACPI_STATUS (Status);