1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2005, 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 *****************************************************************************/
118 #define __EXFLDIO_C__
120 #include <contrib/dev/acpica/acpi.h>
121 #include <contrib/dev/acpica/acinterp.h>
122 #include <contrib/dev/acpica/amlcode.h>
123 #include <contrib/dev/acpica/acevents.h>
124 #include <contrib/dev/acpica/acdispat.h>
127 #define _COMPONENT ACPI_EXECUTER
128 ACPI_MODULE_NAME ("exfldio")
130 /* Local prototypes */
134 ACPI_OPERAND_OBJECT *ObjDesc,
135 UINT32 FieldDatumByteOffset,
140 AcpiExRegisterOverflow (
141 ACPI_OPERAND_OBJECT *ObjDesc,
146 ACPI_OPERAND_OBJECT *ObjDesc,
147 UINT32 FieldDatumByteOffset);
150 /*******************************************************************************
152 * FUNCTION: AcpiExSetupRegion
154 * PARAMETERS: ObjDesc - Field to be read or written
155 * FieldDatumByteOffset - Byte offset of this datum within the
160 * DESCRIPTION: Common processing for AcpiExExtractFromField and
161 * AcpiExInsertIntoField. Initialize the Region if necessary and
162 * validate the request.
164 ******************************************************************************/
168 ACPI_OPERAND_OBJECT *ObjDesc,
169 UINT32 FieldDatumByteOffset)
171 ACPI_STATUS Status = AE_OK;
172 ACPI_OPERAND_OBJECT *RgnDesc;
175 ACPI_FUNCTION_TRACE_U32 ("ExSetupRegion", FieldDatumByteOffset);
178 RgnDesc = ObjDesc->CommonField.RegionObj;
180 /* We must have a valid region */
182 if (ACPI_GET_OBJECT_TYPE (RgnDesc) != ACPI_TYPE_REGION)
184 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %X (%s)\n",
185 ACPI_GET_OBJECT_TYPE (RgnDesc),
186 AcpiUtGetObjectTypeName (RgnDesc)));
188 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
192 * If the Region Address and Length have not been previously evaluated,
193 * evaluate them now and save the results.
195 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
197 Status = AcpiDsGetRegionArguments (RgnDesc);
198 if (ACPI_FAILURE (Status))
200 return_ACPI_STATUS (Status);
204 if (RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
206 /* SMBus has a non-linear address space */
208 return_ACPI_STATUS (AE_OK);
211 #ifdef ACPI_UNDER_DEVELOPMENT
213 * If the Field access is AnyAcc, we can now compute the optimal
214 * access (because we know know the length of the parent region)
216 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
218 if (ACPI_FAILURE (Status))
220 return_ACPI_STATUS (Status);
226 * Validate the request. The entire request from the byte offset for a
227 * length of one field datum (access width) must fit within the region.
228 * (Region length is specified in bytes)
230 if (RgnDesc->Region.Length < (ObjDesc->CommonField.BaseByteOffset +
231 FieldDatumByteOffset +
232 ObjDesc->CommonField.AccessByteWidth))
234 if (AcpiGbl_EnableInterpreterSlack)
237 * Slack mode only: We will go ahead and allow access to this
238 * field if it is within the region length rounded up to the next
239 * access width boundary.
241 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
242 ObjDesc->CommonField.AccessByteWidth) >=
243 (ObjDesc->CommonField.BaseByteOffset +
244 (ACPI_NATIVE_UINT) ObjDesc->CommonField.AccessByteWidth +
245 FieldDatumByteOffset))
247 return_ACPI_STATUS (AE_OK);
251 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
254 * This is the case where the AccessType (AccWord, etc.) is wider
255 * than the region itself. For example, a region of length one
256 * byte, and a field with Dword access specified.
258 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
259 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n",
260 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
261 ObjDesc->CommonField.AccessByteWidth,
262 AcpiUtGetNodeName (RgnDesc->Region.Node),
263 RgnDesc->Region.Length));
267 * Offset rounded up to next multiple of field width
268 * exceeds region length, indicate an error
270 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
271 "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n",
272 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
273 ObjDesc->CommonField.BaseByteOffset,
274 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
275 AcpiUtGetNodeName (RgnDesc->Region.Node),
276 RgnDesc->Region.Length));
278 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
281 return_ACPI_STATUS (AE_OK);
285 /*******************************************************************************
287 * FUNCTION: AcpiExAccessRegion
289 * PARAMETERS: ObjDesc - Field to be read
290 * FieldDatumByteOffset - Byte offset of this datum within the
292 * Value - Where to store value (must at least
293 * the size of ACPI_INTEGER)
294 * Function - Read or Write flag plus other region-
299 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
301 ******************************************************************************/
305 ACPI_OPERAND_OBJECT *ObjDesc,
306 UINT32 FieldDatumByteOffset,
311 ACPI_OPERAND_OBJECT *RgnDesc;
312 ACPI_PHYSICAL_ADDRESS Address;
315 ACPI_FUNCTION_TRACE ("ExAccessRegion");
319 * Ensure that the region operands are fully evaluated and verify
320 * the validity of the request
322 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
323 if (ACPI_FAILURE (Status))
325 return_ACPI_STATUS (Status);
329 * The physical address of this field datum is:
331 * 1) The base of the region, plus
332 * 2) The base offset of the field, plus
333 * 3) The current offset into the field
335 RgnDesc = ObjDesc->CommonField.RegionObj;
336 Address = RgnDesc->Region.Address +
337 ObjDesc->CommonField.BaseByteOffset +
338 FieldDatumByteOffset;
340 if ((Function & ACPI_IO_MASK) == ACPI_READ)
342 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
346 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
349 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
350 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %8.8X%8.8X\n",
351 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
352 RgnDesc->Region.SpaceId,
353 ObjDesc->CommonField.AccessByteWidth,
354 ObjDesc->CommonField.BaseByteOffset,
355 FieldDatumByteOffset,
356 ACPI_FORMAT_UINT64 (Address)));
358 /* Invoke the appropriate AddressSpace/OpRegion handler */
360 Status = AcpiEvAddressSpaceDispatch (RgnDesc, Function,
362 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
364 if (ACPI_FAILURE (Status))
366 if (Status == AE_NOT_IMPLEMENTED)
368 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
369 "Region %s(%X) not implemented\n",
370 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
371 RgnDesc->Region.SpaceId));
373 else if (Status == AE_NOT_EXIST)
376 "Region %s(%X) has no handler\n",
377 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
378 RgnDesc->Region.SpaceId));
382 return_ACPI_STATUS (Status);
386 /*******************************************************************************
388 * FUNCTION: AcpiExRegisterOverflow
390 * PARAMETERS: ObjDesc - Register(Field) to be written
391 * Value - Value to be stored
393 * RETURN: TRUE if value overflows the field, FALSE otherwise
395 * DESCRIPTION: Check if a value is out of range of the field being written.
396 * Used to check if the values written to Index and Bank registers
397 * are out of range. Normally, the value is simply truncated
398 * to fit the field, but this case is most likely a serious
399 * coding error in the ASL.
401 ******************************************************************************/
404 AcpiExRegisterOverflow (
405 ACPI_OPERAND_OBJECT *ObjDesc,
409 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
412 * The field is large enough to hold the maximum integer, so we can
418 if (Value >= ((ACPI_INTEGER) 1 << ObjDesc->CommonField.BitLength))
421 * The Value is larger than the maximum value that can fit into
427 /* The Value will fit into the field with no truncation */
433 /*******************************************************************************
435 * FUNCTION: AcpiExFieldDatumIo
437 * PARAMETERS: ObjDesc - Field to be read
438 * FieldDatumByteOffset - Byte offset of this datum within the
440 * Value - Where to store value (must be 64 bits)
441 * ReadWrite - Read or Write flag
445 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
446 * demultiplexed here to handle the different types of fields
447 * (BufferField, RegionField, IndexField, BankField)
449 ******************************************************************************/
453 ACPI_OPERAND_OBJECT *ObjDesc,
454 UINT32 FieldDatumByteOffset,
459 ACPI_INTEGER LocalValue;
462 ACPI_FUNCTION_TRACE_U32 ("ExFieldDatumIo", FieldDatumByteOffset);
465 if (ReadWrite == ACPI_READ)
471 /* To support reads without saving return value */
475 /* Clear the entire return buffer first, [Very Important!] */
481 * The four types of fields are:
483 * BufferField - Read/write from/to a Buffer
484 * RegionField - Read/write from/to a Operation Region.
485 * BankField - Write to a Bank Register, then read/write from/to an
487 * IndexField - Write to an Index Register, then read/write from/to a
490 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
492 case ACPI_TYPE_BUFFER_FIELD:
494 * If the BufferField arguments have not been previously evaluated,
495 * evaluate them now and save the results.
497 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
499 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
500 if (ACPI_FAILURE (Status))
502 return_ACPI_STATUS (Status);
506 if (ReadWrite == ACPI_READ)
509 * Copy the data from the source buffer.
510 * Length is the field width in bytes.
513 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
514 ObjDesc->BufferField.BaseByteOffset +
515 FieldDatumByteOffset,
516 ObjDesc->CommonField.AccessByteWidth);
521 * Copy the data to the target buffer.
522 * Length is the field width in bytes.
524 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
525 ObjDesc->BufferField.BaseByteOffset +
526 FieldDatumByteOffset,
527 Value, ObjDesc->CommonField.AccessByteWidth);
534 case ACPI_TYPE_LOCAL_BANK_FIELD:
537 * Ensure that the BankValue is not beyond the capacity of
540 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
541 (ACPI_INTEGER) ObjDesc->BankField.Value))
543 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
547 * For BankFields, we must write the BankValue to the BankRegister
548 * (itself a RegionField) before we can access the data.
550 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
551 &ObjDesc->BankField.Value,
552 sizeof (ObjDesc->BankField.Value));
553 if (ACPI_FAILURE (Status))
555 return_ACPI_STATUS (Status);
559 * Now that the Bank has been selected, fall through to the
560 * RegionField case and write the datum to the Operation Region
563 /*lint -fallthrough */
566 case ACPI_TYPE_LOCAL_REGION_FIELD:
568 * For simple RegionFields, we just directly access the owning
571 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
576 case ACPI_TYPE_LOCAL_INDEX_FIELD:
580 * Ensure that the IndexValue is not beyond the capacity of
583 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
584 (ACPI_INTEGER) ObjDesc->IndexField.Value))
586 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
589 /* Write the index value to the IndexRegister (itself a RegionField) */
591 FieldDatumByteOffset += ObjDesc->IndexField.Value;
593 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
594 "Write to Index Register: Value %8.8X\n",
595 FieldDatumByteOffset));
597 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
598 &FieldDatumByteOffset,
599 sizeof (FieldDatumByteOffset));
600 if (ACPI_FAILURE (Status))
602 return_ACPI_STATUS (Status);
605 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
606 "I/O to Data Register: ValuePtr %p\n",
609 if (ReadWrite == ACPI_READ)
611 /* Read the datum from the DataRegister */
613 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
614 Value, sizeof (ACPI_INTEGER));
618 /* Write the datum to the DataRegister */
620 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
621 Value, sizeof (ACPI_INTEGER));
628 ACPI_REPORT_ERROR (("Wrong object type in field I/O %X\n",
629 ACPI_GET_OBJECT_TYPE (ObjDesc)));
630 Status = AE_AML_INTERNAL;
634 if (ACPI_SUCCESS (Status))
636 if (ReadWrite == ACPI_READ)
638 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
639 "Value Read %8.8X%8.8X, Width %d\n",
640 ACPI_FORMAT_UINT64 (*Value),
641 ObjDesc->CommonField.AccessByteWidth));
645 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
646 "Value Written %8.8X%8.8X, Width %d\n",
647 ACPI_FORMAT_UINT64 (*Value),
648 ObjDesc->CommonField.AccessByteWidth));
652 return_ACPI_STATUS (Status);
656 /*******************************************************************************
658 * FUNCTION: AcpiExWriteWithUpdateRule
660 * PARAMETERS: ObjDesc - Field to be written
661 * Mask - bitmask within field datum
662 * FieldValue - Value to write
663 * FieldDatumByteOffset - Offset of datum within field
667 * DESCRIPTION: Apply the field update rule to a field write
669 ******************************************************************************/
672 AcpiExWriteWithUpdateRule (
673 ACPI_OPERAND_OBJECT *ObjDesc,
675 ACPI_INTEGER FieldValue,
676 UINT32 FieldDatumByteOffset)
678 ACPI_STATUS Status = AE_OK;
679 ACPI_INTEGER MergedValue;
680 ACPI_INTEGER CurrentValue;
683 ACPI_FUNCTION_TRACE_U32 ("ExWriteWithUpdateRule", Mask);
686 /* Start with the new bits */
688 MergedValue = FieldValue;
690 /* If the mask is all ones, we don't need to worry about the update rule */
692 if (Mask != ACPI_INTEGER_MAX)
694 /* Decode the update rule */
696 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
698 case AML_FIELD_UPDATE_PRESERVE:
700 * Check if update rule needs to be applied (not if mask is all
701 * ones) The left shift drops the bits we want to ignore.
703 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
704 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
707 * Read the current contents of the byte/word/dword containing
708 * the field, and merge with the new field value.
710 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
711 &CurrentValue, ACPI_READ);
712 if (ACPI_FAILURE (Status))
714 return_ACPI_STATUS (Status);
717 MergedValue |= (CurrentValue & ~Mask);
721 case AML_FIELD_UPDATE_WRITE_AS_ONES:
723 /* Set positions outside the field to all ones */
725 MergedValue |= ~Mask;
728 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
730 /* Set positions outside the field to all zeros */
737 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
738 "WriteWithUpdateRule: Unknown UpdateRule setting: %X\n",
739 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
740 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
744 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
745 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
746 ACPI_FORMAT_UINT64 (Mask),
747 FieldDatumByteOffset,
748 ObjDesc->CommonField.AccessByteWidth,
749 ACPI_FORMAT_UINT64 (FieldValue),
750 ACPI_FORMAT_UINT64 (MergedValue)));
752 /* Write the merged value */
754 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
755 &MergedValue, ACPI_WRITE);
757 return_ACPI_STATUS (Status);
761 /*******************************************************************************
763 * FUNCTION: AcpiExExtractFromField
765 * PARAMETERS: ObjDesc - Field to be read
766 * Buffer - Where to store the field data
767 * BufferLength - Length of Buffer
771 * DESCRIPTION: Retrieve the current value of the given field
773 ******************************************************************************/
776 AcpiExExtractFromField (
777 ACPI_OPERAND_OBJECT *ObjDesc,
782 ACPI_INTEGER RawDatum;
783 ACPI_INTEGER MergedDatum;
784 UINT32 FieldOffset = 0;
785 UINT32 BufferOffset = 0;
786 UINT32 BufferTailBits;
788 UINT32 FieldDatumCount;
792 ACPI_FUNCTION_TRACE ("ExExtractFromField");
795 /* Validate target buffer and clear it */
797 if (BufferLength < ACPI_ROUND_BITS_UP_TO_BYTES (
798 ObjDesc->CommonField.BitLength))
800 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
801 "Field size %X (bits) is too large for buffer (%X)\n",
802 ObjDesc->CommonField.BitLength, BufferLength));
804 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
806 ACPI_MEMSET (Buffer, 0, BufferLength);
808 /* Compute the number of datums (access width data items) */
810 DatumCount = ACPI_ROUND_UP_TO (
811 ObjDesc->CommonField.BitLength,
812 ObjDesc->CommonField.AccessBitWidth);
813 FieldDatumCount = ACPI_ROUND_UP_TO (
814 ObjDesc->CommonField.BitLength +
815 ObjDesc->CommonField.StartFieldBitOffset,
816 ObjDesc->CommonField.AccessBitWidth);
818 /* Priming read from the field */
820 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
821 if (ACPI_FAILURE (Status))
823 return_ACPI_STATUS (Status);
825 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
827 /* Read the rest of the field */
829 for (i = 1; i < FieldDatumCount; i++)
831 /* Get next input datum from the field */
833 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
834 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
835 &RawDatum, ACPI_READ);
836 if (ACPI_FAILURE (Status))
838 return_ACPI_STATUS (Status);
841 /* Merge with previous datum if necessary */
843 MergedDatum |= RawDatum <<
844 (ObjDesc->CommonField.AccessBitWidth -
845 ObjDesc->CommonField.StartFieldBitOffset);
852 /* Write merged datum to target buffer */
854 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
855 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
856 BufferLength - BufferOffset));
858 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
859 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
862 /* Mask off any extra bits in the last datum */
864 BufferTailBits = ObjDesc->CommonField.BitLength %
865 ObjDesc->CommonField.AccessBitWidth;
868 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
871 /* Write the last datum to the buffer */
873 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
874 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
875 BufferLength - BufferOffset));
877 return_ACPI_STATUS (AE_OK);
881 /*******************************************************************************
883 * FUNCTION: AcpiExInsertIntoField
885 * PARAMETERS: ObjDesc - Field to be written
886 * Buffer - Data to be written
887 * BufferLength - Length of Buffer
891 * DESCRIPTION: Store the Buffer contents into the given field
893 ******************************************************************************/
896 AcpiExInsertIntoField (
897 ACPI_OPERAND_OBJECT *ObjDesc,
903 ACPI_INTEGER MergedDatum;
904 ACPI_INTEGER RawDatum = 0;
905 UINT32 FieldOffset = 0;
906 UINT32 BufferOffset = 0;
907 UINT32 BufferTailBits;
909 UINT32 FieldDatumCount;
913 ACPI_FUNCTION_TRACE ("ExInsertIntoField");
916 /* Validate input buffer */
918 if (BufferLength < ACPI_ROUND_BITS_UP_TO_BYTES (
919 ObjDesc->CommonField.BitLength))
921 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
922 "Field size %X (bits) is too large for buffer (%X)\n",
923 ObjDesc->CommonField.BitLength, BufferLength));
925 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
928 /* Compute the number of datums (access width data items) */
930 Mask = ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
931 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
932 ObjDesc->CommonField.AccessBitWidth);
933 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
934 ObjDesc->CommonField.StartFieldBitOffset,
935 ObjDesc->CommonField.AccessBitWidth);
937 /* Get initial Datum from the input buffer */
939 ACPI_MEMCPY (&RawDatum, Buffer,
940 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
941 BufferLength - BufferOffset));
943 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
945 /* Write the entire field */
947 for (i = 1; i < FieldDatumCount; i++)
949 /* Write merged datum to the target field */
952 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
953 MergedDatum, FieldOffset);
954 if (ACPI_FAILURE (Status))
956 return_ACPI_STATUS (Status);
959 /* Start new output datum by merging with previous input datum */
961 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
962 MergedDatum = RawDatum >>
963 (ObjDesc->CommonField.AccessBitWidth -
964 ObjDesc->CommonField.StartFieldBitOffset);
965 Mask = ACPI_INTEGER_MAX;
972 /* Get the next input datum from the buffer */
974 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
975 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
976 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
977 BufferLength - BufferOffset));
978 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
981 /* Mask off any extra bits in the last datum */
983 BufferTailBits = (ObjDesc->CommonField.BitLength +
984 ObjDesc->CommonField.StartFieldBitOffset) %
985 ObjDesc->CommonField.AccessBitWidth;
988 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
991 /* Write the last datum to the field */
994 Status = AcpiExWriteWithUpdateRule (ObjDesc,
995 Mask, MergedDatum, FieldOffset);
997 return_ACPI_STATUS (Status);