1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
51 #include <contrib/dev/acpica/include/acevents.h>
52 #include <contrib/dev/acpica/include/acdispat.h>
55 #define _COMPONENT ACPI_EXECUTER
56 ACPI_MODULE_NAME ("exfldio")
58 /* Local prototypes */
62 ACPI_OPERAND_OBJECT *ObjDesc,
63 UINT32 FieldDatumByteOffset,
68 AcpiExRegisterOverflow (
69 ACPI_OPERAND_OBJECT *ObjDesc,
74 ACPI_OPERAND_OBJECT *ObjDesc,
75 UINT32 FieldDatumByteOffset);
78 /*******************************************************************************
80 * FUNCTION: AcpiExSetupRegion
82 * PARAMETERS: ObjDesc - Field to be read or written
83 * FieldDatumByteOffset - Byte offset of this datum within the
88 * DESCRIPTION: Common processing for AcpiExExtractFromField and
89 * AcpiExInsertIntoField. Initialize the Region if necessary and
90 * validate the request.
92 ******************************************************************************/
96 ACPI_OPERAND_OBJECT *ObjDesc,
97 UINT32 FieldDatumByteOffset)
99 ACPI_STATUS Status = AE_OK;
100 ACPI_OPERAND_OBJECT *RgnDesc;
104 ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
107 RgnDesc = ObjDesc->CommonField.RegionObj;
109 /* We must have a valid region */
111 if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
113 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
114 RgnDesc->Common.Type,
115 AcpiUtGetObjectTypeName (RgnDesc)));
117 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
120 SpaceId = RgnDesc->Region.SpaceId;
122 /* Validate the Space ID */
124 if (!AcpiIsValidSpaceId (SpaceId))
126 ACPI_ERROR ((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
127 return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
131 * If the Region Address and Length have not been previously evaluated,
132 * evaluate them now and save the results.
134 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
136 Status = AcpiDsGetRegionArguments (RgnDesc);
137 if (ACPI_FAILURE (Status))
139 return_ACPI_STATUS (Status);
144 * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
145 * address space and the request cannot be directly validated
147 if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
148 SpaceId == ACPI_ADR_SPACE_GSBUS ||
149 SpaceId == ACPI_ADR_SPACE_IPMI)
151 /* SMBus or IPMI has a non-linear address space */
153 return_ACPI_STATUS (AE_OK);
156 #ifdef ACPI_UNDER_DEVELOPMENT
158 * If the Field access is AnyAcc, we can now compute the optimal
159 * access (because we know know the length of the parent region)
161 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
163 if (ACPI_FAILURE (Status))
165 return_ACPI_STATUS (Status);
171 * Validate the request. The entire request from the byte offset for a
172 * length of one field datum (access width) must fit within the region.
173 * (Region length is specified in bytes)
175 if (RgnDesc->Region.Length <
176 (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
177 ObjDesc->CommonField.AccessByteWidth))
179 if (AcpiGbl_EnableInterpreterSlack)
182 * Slack mode only: We will go ahead and allow access to this
183 * field if it is within the region length rounded up to the next
184 * access width boundary. ACPI_SIZE cast for 64-bit compile.
186 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
187 ObjDesc->CommonField.AccessByteWidth) >=
188 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
189 ObjDesc->CommonField.AccessByteWidth +
190 FieldDatumByteOffset))
192 return_ACPI_STATUS (AE_OK);
196 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
199 * This is the case where the AccessType (AccWord, etc.) is wider
200 * than the region itself. For example, a region of length one
201 * byte, and a field with Dword access specified.
203 ACPI_ERROR ((AE_INFO,
204 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
205 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
206 ObjDesc->CommonField.AccessByteWidth,
207 AcpiUtGetNodeName (RgnDesc->Region.Node),
208 RgnDesc->Region.Length));
212 * Offset rounded up to next multiple of field width
213 * exceeds region length, indicate an error
215 ACPI_ERROR ((AE_INFO,
216 "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
217 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
218 ObjDesc->CommonField.BaseByteOffset,
219 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
220 AcpiUtGetNodeName (RgnDesc->Region.Node),
221 RgnDesc->Region.Length));
223 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
226 return_ACPI_STATUS (AE_OK);
230 /*******************************************************************************
232 * FUNCTION: AcpiExAccessRegion
234 * PARAMETERS: ObjDesc - Field to be read
235 * FieldDatumByteOffset - Byte offset of this datum within the
237 * Value - Where to store value (must at least
239 * Function - Read or Write flag plus other region-
244 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
246 ******************************************************************************/
250 ACPI_OPERAND_OBJECT *ObjDesc,
251 UINT32 FieldDatumByteOffset,
256 ACPI_OPERAND_OBJECT *RgnDesc;
260 ACPI_FUNCTION_TRACE (ExAccessRegion);
264 * Ensure that the region operands are fully evaluated and verify
265 * the validity of the request
267 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
268 if (ACPI_FAILURE (Status))
270 return_ACPI_STATUS (Status);
274 * The physical address of this field datum is:
276 * 1) The base of the region, plus
277 * 2) The base offset of the field, plus
278 * 3) The current offset into the field
280 RgnDesc = ObjDesc->CommonField.RegionObj;
282 ObjDesc->CommonField.BaseByteOffset +
283 FieldDatumByteOffset;
285 if ((Function & ACPI_IO_MASK) == ACPI_READ)
287 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
291 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
294 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
295 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
296 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
297 RgnDesc->Region.SpaceId,
298 ObjDesc->CommonField.AccessByteWidth,
299 ObjDesc->CommonField.BaseByteOffset,
300 FieldDatumByteOffset,
301 ACPI_CAST_PTR (void, (RgnDesc->Region.Address + RegionOffset))));
303 /* Invoke the appropriate AddressSpace/OpRegion handler */
305 Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
306 Function, RegionOffset,
307 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
309 if (ACPI_FAILURE (Status))
311 if (Status == AE_NOT_IMPLEMENTED)
313 ACPI_ERROR ((AE_INFO,
314 "Region %s (ID=%u) not implemented",
315 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
316 RgnDesc->Region.SpaceId));
318 else if (Status == AE_NOT_EXIST)
320 ACPI_ERROR ((AE_INFO,
321 "Region %s (ID=%u) has no handler",
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,
353 ACPI_FUNCTION_NAME (ExRegisterOverflow);
356 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
359 * The field is large enough to hold the maximum integer, so we can
365 if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
368 * The Value is larger than the maximum value that can fit into
371 ACPI_ERROR ((AE_INFO,
372 "Index value 0x%8.8X%8.8X overflows field width 0x%X",
373 ACPI_FORMAT_UINT64 (Value),
374 ObjDesc->CommonField.BitLength));
379 /* The Value will fit into the field with no truncation */
385 /*******************************************************************************
387 * FUNCTION: AcpiExFieldDatumIo
389 * PARAMETERS: ObjDesc - Field to be read
390 * FieldDatumByteOffset - Byte offset of this datum within the
392 * Value - Where to store value (must be 64 bits)
393 * ReadWrite - Read or Write flag
397 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
398 * demultiplexed here to handle the different types of fields
399 * (BufferField, RegionField, IndexField, BankField)
401 ******************************************************************************/
405 ACPI_OPERAND_OBJECT *ObjDesc,
406 UINT32 FieldDatumByteOffset,
414 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
417 if (ReadWrite == ACPI_READ)
423 /* To support reads without saving return value */
427 /* Clear the entire return buffer first, [Very Important!] */
433 * The four types of fields are:
435 * BufferField - Read/write from/to a Buffer
436 * RegionField - Read/write from/to a Operation Region.
437 * BankField - Write to a Bank Register, then read/write from/to an
439 * IndexField - Write to an Index Register, then read/write from/to a
442 switch (ObjDesc->Common.Type)
444 case ACPI_TYPE_BUFFER_FIELD:
446 * If the BufferField arguments have not been previously evaluated,
447 * evaluate them now and save the results.
449 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
451 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
452 if (ACPI_FAILURE (Status))
454 return_ACPI_STATUS (Status);
458 if (ReadWrite == ACPI_READ)
461 * Copy the data from the source buffer.
462 * Length is the field width in bytes.
465 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
466 ObjDesc->BufferField.BaseByteOffset +
467 FieldDatumByteOffset,
468 ObjDesc->CommonField.AccessByteWidth);
473 * Copy the data to the target buffer.
474 * Length is the field width in bytes.
476 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
477 ObjDesc->BufferField.BaseByteOffset +
478 FieldDatumByteOffset,
479 Value, ObjDesc->CommonField.AccessByteWidth);
486 case ACPI_TYPE_LOCAL_BANK_FIELD:
489 * Ensure that the BankValue is not beyond the capacity of
492 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
493 (UINT64) ObjDesc->BankField.Value))
495 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
499 * For BankFields, we must write the BankValue to the BankRegister
500 * (itself a RegionField) before we can access the data.
502 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
503 &ObjDesc->BankField.Value,
504 sizeof (ObjDesc->BankField.Value));
505 if (ACPI_FAILURE (Status))
507 return_ACPI_STATUS (Status);
511 * Now that the Bank has been selected, fall through to the
512 * RegionField case and write the datum to the Operation Region
515 /*lint -fallthrough */
518 case ACPI_TYPE_LOCAL_REGION_FIELD:
520 * For simple RegionFields, we just directly access the owning
523 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
528 case ACPI_TYPE_LOCAL_INDEX_FIELD:
532 * Ensure that the IndexValue is not beyond the capacity of
535 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
536 (UINT64) ObjDesc->IndexField.Value))
538 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
541 /* Write the index value to the IndexRegister (itself a RegionField) */
543 FieldDatumByteOffset += ObjDesc->IndexField.Value;
545 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
546 "Write to Index Register: Value %8.8X\n",
547 FieldDatumByteOffset));
549 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
550 &FieldDatumByteOffset,
551 sizeof (FieldDatumByteOffset));
552 if (ACPI_FAILURE (Status))
554 return_ACPI_STATUS (Status);
557 if (ReadWrite == ACPI_READ)
559 /* Read the datum from the DataRegister */
561 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
562 "Read from Data Register\n"));
564 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
565 Value, sizeof (UINT64));
569 /* Write the datum to the DataRegister */
571 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
572 "Write to Data Register: Value %8.8X%8.8X\n",
573 ACPI_FORMAT_UINT64 (*Value)));
575 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
576 Value, sizeof (UINT64));
583 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
584 ObjDesc->Common.Type));
585 Status = AE_AML_INTERNAL;
589 if (ACPI_SUCCESS (Status))
591 if (ReadWrite == ACPI_READ)
593 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
594 "Value Read %8.8X%8.8X, Width %u\n",
595 ACPI_FORMAT_UINT64 (*Value),
596 ObjDesc->CommonField.AccessByteWidth));
600 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
601 "Value Written %8.8X%8.8X, Width %u\n",
602 ACPI_FORMAT_UINT64 (*Value),
603 ObjDesc->CommonField.AccessByteWidth));
607 return_ACPI_STATUS (Status);
611 /*******************************************************************************
613 * FUNCTION: AcpiExWriteWithUpdateRule
615 * PARAMETERS: ObjDesc - Field to be written
616 * Mask - bitmask within field datum
617 * FieldValue - Value to write
618 * FieldDatumByteOffset - Offset of datum within field
622 * DESCRIPTION: Apply the field update rule to a field write
624 ******************************************************************************/
627 AcpiExWriteWithUpdateRule (
628 ACPI_OPERAND_OBJECT *ObjDesc,
631 UINT32 FieldDatumByteOffset)
633 ACPI_STATUS Status = AE_OK;
638 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
641 /* Start with the new bits */
643 MergedValue = FieldValue;
645 /* If the mask is all ones, we don't need to worry about the update rule */
647 if (Mask != ACPI_UINT64_MAX)
649 /* Decode the update rule */
651 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
653 case AML_FIELD_UPDATE_PRESERVE:
655 * Check if update rule needs to be applied (not if mask is all
656 * ones) The left shift drops the bits we want to ignore.
658 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
659 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
662 * Read the current contents of the byte/word/dword containing
663 * the field, and merge with the new field value.
665 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
666 &CurrentValue, ACPI_READ);
667 if (ACPI_FAILURE (Status))
669 return_ACPI_STATUS (Status);
672 MergedValue |= (CurrentValue & ~Mask);
676 case AML_FIELD_UPDATE_WRITE_AS_ONES:
678 /* Set positions outside the field to all ones */
680 MergedValue |= ~Mask;
683 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
685 /* Set positions outside the field to all zeros */
692 ACPI_ERROR ((AE_INFO,
693 "Unknown UpdateRule value: 0x%X",
694 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
695 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
699 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
700 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
701 ACPI_FORMAT_UINT64 (Mask),
702 FieldDatumByteOffset,
703 ObjDesc->CommonField.AccessByteWidth,
704 ACPI_FORMAT_UINT64 (FieldValue),
705 ACPI_FORMAT_UINT64 (MergedValue)));
707 /* Write the merged value */
709 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
710 &MergedValue, ACPI_WRITE);
712 return_ACPI_STATUS (Status);
716 /*******************************************************************************
718 * FUNCTION: AcpiExExtractFromField
720 * PARAMETERS: ObjDesc - Field to be read
721 * Buffer - Where to store the field data
722 * BufferLength - Length of Buffer
726 * DESCRIPTION: Retrieve the current value of the given field
728 ******************************************************************************/
731 AcpiExExtractFromField (
732 ACPI_OPERAND_OBJECT *ObjDesc,
739 UINT32 FieldOffset = 0;
740 UINT32 BufferOffset = 0;
741 UINT32 BufferTailBits;
743 UINT32 FieldDatumCount;
744 UINT32 AccessBitWidth;
748 ACPI_FUNCTION_TRACE (ExExtractFromField);
751 /* Validate target buffer and clear it */
754 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
756 ACPI_ERROR ((AE_INFO,
757 "Field size %u (bits) is too large for buffer (%u)",
758 ObjDesc->CommonField.BitLength, BufferLength));
760 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
763 ACPI_MEMSET (Buffer, 0, BufferLength);
764 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
766 /* Handle the simple case here */
768 if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
769 (ObjDesc->CommonField.BitLength == AccessBitWidth))
771 Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
772 return_ACPI_STATUS (Status);
775 /* TBD: Move to common setup code */
777 /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
779 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
781 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
782 AccessBitWidth = sizeof (UINT64) * 8;
785 /* Compute the number of datums (access width data items) */
787 DatumCount = ACPI_ROUND_UP_TO (
788 ObjDesc->CommonField.BitLength, AccessBitWidth);
790 FieldDatumCount = ACPI_ROUND_UP_TO (
791 ObjDesc->CommonField.BitLength +
792 ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
794 /* Priming read from the field */
796 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
797 if (ACPI_FAILURE (Status))
799 return_ACPI_STATUS (Status);
801 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
803 /* Read the rest of the field */
805 for (i = 1; i < FieldDatumCount; i++)
807 /* Get next input datum from the field */
809 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
810 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
811 &RawDatum, ACPI_READ);
812 if (ACPI_FAILURE (Status))
814 return_ACPI_STATUS (Status);
818 * Merge with previous datum if necessary.
820 * Note: Before the shift, check if the shift value will be larger than
821 * the integer size. If so, there is no need to perform the operation.
822 * This avoids the differences in behavior between different compilers
823 * concerning shift values larger than the target data width.
825 if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
826 ACPI_INTEGER_BIT_SIZE)
828 MergedDatum |= RawDatum <<
829 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
837 /* Write merged datum to target buffer */
839 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
840 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
841 BufferLength - BufferOffset));
843 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
844 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
847 /* Mask off any extra bits in the last datum */
849 BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
852 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
855 /* Write the last datum to the buffer */
857 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
858 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
859 BufferLength - BufferOffset));
861 return_ACPI_STATUS (AE_OK);
865 /*******************************************************************************
867 * FUNCTION: AcpiExInsertIntoField
869 * PARAMETERS: ObjDesc - Field to be written
870 * Buffer - Data to be written
871 * BufferLength - Length of Buffer
875 * DESCRIPTION: Store the Buffer contents into the given field
877 ******************************************************************************/
880 AcpiExInsertIntoField (
881 ACPI_OPERAND_OBJECT *ObjDesc,
891 UINT32 FieldOffset = 0;
892 UINT32 BufferOffset = 0;
893 UINT32 BufferTailBits;
895 UINT32 FieldDatumCount;
896 UINT32 AccessBitWidth;
897 UINT32 RequiredLength;
901 ACPI_FUNCTION_TRACE (ExInsertIntoField);
904 /* Validate input buffer */
907 RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
908 ObjDesc->CommonField.BitLength);
910 * We must have a buffer that is at least as long as the field
911 * we are writing to. This is because individual fields are
912 * indivisible and partial writes are not supported -- as per
913 * the ACPI specification.
915 if (BufferLength < RequiredLength)
917 /* We need to create a new buffer */
919 NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
922 return_ACPI_STATUS (AE_NO_MEMORY);
926 * Copy the original data to the new buffer, starting
927 * at Byte zero. All unused (upper) bytes of the
930 ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
932 BufferLength = RequiredLength;
935 /* TBD: Move to common setup code */
937 /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
938 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
940 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
943 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
946 * Create the bitmasks used for bit insertion.
947 * Note: This if/else is used to bypass compiler differences with the
950 if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
952 WidthMask = ACPI_UINT64_MAX;
956 WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
960 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
962 /* Compute the number of datums (access width data items) */
964 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
967 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
968 ObjDesc->CommonField.StartFieldBitOffset,
971 /* Get initial Datum from the input buffer */
973 ACPI_MEMCPY (&RawDatum, Buffer,
974 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
975 BufferLength - BufferOffset));
977 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
979 /* Write the entire field */
981 for (i = 1; i < FieldDatumCount; i++)
983 /* Write merged datum to the target field */
986 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
987 MergedDatum, FieldOffset);
988 if (ACPI_FAILURE (Status))
993 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
996 * Start new output datum by merging with previous input datum
999 * Note: Before the shift, check if the shift value will be larger than
1000 * the integer size. If so, there is no need to perform the operation.
1001 * This avoids the differences in behavior between different compilers
1002 * concerning shift values larger than the target data width.
1004 if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1005 ACPI_INTEGER_BIT_SIZE)
1007 MergedDatum = RawDatum >>
1008 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1017 if (i == DatumCount)
1022 /* Get the next input datum from the buffer */
1024 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1025 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1026 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1027 BufferLength - BufferOffset));
1029 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1032 /* Mask off any extra bits in the last datum */
1034 BufferTailBits = (ObjDesc->CommonField.BitLength +
1035 ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1038 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1041 /* Write the last datum to the field */
1043 MergedDatum &= Mask;
1044 Status = AcpiExWriteWithUpdateRule (ObjDesc,
1045 Mask, MergedDatum, FieldOffset);
1048 /* Free temporary buffer if we used one */
1052 ACPI_FREE (NewBuffer);
1054 return_ACPI_STATUS (Status);