1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
52 #define _COMPONENT ACPI_EXECUTER
53 ACPI_MODULE_NAME ("exfldio")
55 /* Local prototypes */
59 ACPI_OPERAND_OBJECT *ObjDesc,
60 UINT32 FieldDatumByteOffset,
65 AcpiExRegisterOverflow (
66 ACPI_OPERAND_OBJECT *ObjDesc,
71 ACPI_OPERAND_OBJECT *ObjDesc,
72 UINT32 FieldDatumByteOffset);
75 /*******************************************************************************
77 * FUNCTION: AcpiExSetupRegion
79 * PARAMETERS: ObjDesc - Field to be read or written
80 * FieldDatumByteOffset - Byte offset of this datum within the
85 * DESCRIPTION: Common processing for AcpiExExtractFromField and
86 * AcpiExInsertIntoField. Initialize the Region if necessary and
87 * validate the request.
89 ******************************************************************************/
93 ACPI_OPERAND_OBJECT *ObjDesc,
94 UINT32 FieldDatumByteOffset)
96 ACPI_STATUS Status = AE_OK;
97 ACPI_OPERAND_OBJECT *RgnDesc;
101 ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
104 RgnDesc = ObjDesc->CommonField.RegionObj;
106 /* We must have a valid region */
108 if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
110 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
111 RgnDesc->Common.Type,
112 AcpiUtGetObjectTypeName (RgnDesc)));
114 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
117 SpaceId = RgnDesc->Region.SpaceId;
119 /* Validate the Space ID */
121 if (!AcpiIsValidSpaceId (SpaceId))
123 ACPI_ERROR ((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
124 return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
128 * If the Region Address and Length have not been previously evaluated,
129 * evaluate them now and save the results.
131 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
133 Status = AcpiDsGetRegionArguments (RgnDesc);
134 if (ACPI_FAILURE (Status))
136 return_ACPI_STATUS (Status);
141 * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
142 * address space and the request cannot be directly validated
144 if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
145 SpaceId == ACPI_ADR_SPACE_GSBUS ||
146 SpaceId == ACPI_ADR_SPACE_IPMI)
148 /* SMBus or IPMI has a non-linear address space */
150 return_ACPI_STATUS (AE_OK);
153 #ifdef ACPI_UNDER_DEVELOPMENT
155 * If the Field access is AnyAcc, we can now compute the optimal
156 * access (because we know know the length of the parent region)
158 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
160 if (ACPI_FAILURE (Status))
162 return_ACPI_STATUS (Status);
168 * Validate the request. The entire request from the byte offset for a
169 * length of one field datum (access width) must fit within the region.
170 * (Region length is specified in bytes)
172 if (RgnDesc->Region.Length <
173 (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
174 ObjDesc->CommonField.AccessByteWidth))
176 if (AcpiGbl_EnableInterpreterSlack)
179 * Slack mode only: We will go ahead and allow access to this
180 * field if it is within the region length rounded up to the next
181 * access width boundary. ACPI_SIZE cast for 64-bit compile.
183 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
184 ObjDesc->CommonField.AccessByteWidth) >=
185 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
186 ObjDesc->CommonField.AccessByteWidth +
187 FieldDatumByteOffset))
189 return_ACPI_STATUS (AE_OK);
193 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
196 * This is the case where the AccessType (AccWord, etc.) is wider
197 * than the region itself. For example, a region of length one
198 * byte, and a field with Dword access specified.
200 ACPI_ERROR ((AE_INFO,
201 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
202 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
203 ObjDesc->CommonField.AccessByteWidth,
204 AcpiUtGetNodeName (RgnDesc->Region.Node),
205 RgnDesc->Region.Length));
209 * Offset rounded up to next multiple of field width
210 * exceeds region length, indicate an error
212 ACPI_ERROR ((AE_INFO,
213 "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
214 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
215 ObjDesc->CommonField.BaseByteOffset,
216 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
217 AcpiUtGetNodeName (RgnDesc->Region.Node),
218 RgnDesc->Region.Length));
220 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
223 return_ACPI_STATUS (AE_OK);
227 /*******************************************************************************
229 * FUNCTION: AcpiExAccessRegion
231 * PARAMETERS: ObjDesc - Field to be read
232 * FieldDatumByteOffset - Byte offset of this datum within the
234 * Value - Where to store value (must at least
236 * Function - Read or Write flag plus other region-
241 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
243 ******************************************************************************/
247 ACPI_OPERAND_OBJECT *ObjDesc,
248 UINT32 FieldDatumByteOffset,
253 ACPI_OPERAND_OBJECT *RgnDesc;
257 ACPI_FUNCTION_TRACE (ExAccessRegion);
261 * Ensure that the region operands are fully evaluated and verify
262 * the validity of the request
264 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
265 if (ACPI_FAILURE (Status))
267 return_ACPI_STATUS (Status);
271 * The physical address of this field datum is:
273 * 1) The base of the region, plus
274 * 2) The base offset of the field, plus
275 * 3) The current offset into the field
277 RgnDesc = ObjDesc->CommonField.RegionObj;
279 ObjDesc->CommonField.BaseByteOffset +
280 FieldDatumByteOffset;
282 if ((Function & ACPI_IO_MASK) == ACPI_READ)
284 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
288 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
291 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
292 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
293 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
294 RgnDesc->Region.SpaceId,
295 ObjDesc->CommonField.AccessByteWidth,
296 ObjDesc->CommonField.BaseByteOffset,
297 FieldDatumByteOffset,
298 ACPI_CAST_PTR (void, (RgnDesc->Region.Address + RegionOffset))));
300 /* Invoke the appropriate AddressSpace/OpRegion handler */
302 Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
303 Function, RegionOffset,
304 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
306 if (ACPI_FAILURE (Status))
308 if (Status == AE_NOT_IMPLEMENTED)
310 ACPI_ERROR ((AE_INFO,
311 "Region %s (ID=%u) not implemented",
312 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
313 RgnDesc->Region.SpaceId));
315 else if (Status == AE_NOT_EXIST)
317 ACPI_ERROR ((AE_INFO,
318 "Region %s (ID=%u) has no handler",
319 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
320 RgnDesc->Region.SpaceId));
324 return_ACPI_STATUS (Status);
328 /*******************************************************************************
330 * FUNCTION: AcpiExRegisterOverflow
332 * PARAMETERS: ObjDesc - Register(Field) to be written
333 * Value - Value to be stored
335 * RETURN: TRUE if value overflows the field, FALSE otherwise
337 * DESCRIPTION: Check if a value is out of range of the field being written.
338 * Used to check if the values written to Index and Bank registers
339 * are out of range. Normally, the value is simply truncated
340 * to fit the field, but this case is most likely a serious
341 * coding error in the ASL.
343 ******************************************************************************/
346 AcpiExRegisterOverflow (
347 ACPI_OPERAND_OBJECT *ObjDesc,
351 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
354 * The field is large enough to hold the maximum integer, so we can
360 if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
363 * The Value is larger than the maximum value that can fit into
366 ACPI_ERROR ((AE_INFO,
367 "Index value 0x%8.8X%8.8X overflows field width 0x%X",
368 ACPI_FORMAT_UINT64 (Value),
369 ObjDesc->CommonField.BitLength));
374 /* The Value will fit into the field with no truncation */
380 /*******************************************************************************
382 * FUNCTION: AcpiExFieldDatumIo
384 * PARAMETERS: ObjDesc - Field to be read
385 * FieldDatumByteOffset - Byte offset of this datum within the
387 * Value - Where to store value (must be 64 bits)
388 * ReadWrite - Read or Write flag
392 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
393 * demultiplexed here to handle the different types of fields
394 * (BufferField, RegionField, IndexField, BankField)
396 ******************************************************************************/
400 ACPI_OPERAND_OBJECT *ObjDesc,
401 UINT32 FieldDatumByteOffset,
409 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
412 if (ReadWrite == ACPI_READ)
418 /* To support reads without saving return value */
422 /* Clear the entire return buffer first, [Very Important!] */
428 * The four types of fields are:
430 * BufferField - Read/write from/to a Buffer
431 * RegionField - Read/write from/to a Operation Region.
432 * BankField - Write to a Bank Register, then read/write from/to an
434 * IndexField - Write to an Index Register, then read/write from/to a
437 switch (ObjDesc->Common.Type)
439 case ACPI_TYPE_BUFFER_FIELD:
441 * If the BufferField arguments have not been previously evaluated,
442 * evaluate them now and save the results.
444 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
446 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
447 if (ACPI_FAILURE (Status))
449 return_ACPI_STATUS (Status);
453 if (ReadWrite == ACPI_READ)
456 * Copy the data from the source buffer.
457 * Length is the field width in bytes.
460 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
461 ObjDesc->BufferField.BaseByteOffset +
462 FieldDatumByteOffset,
463 ObjDesc->CommonField.AccessByteWidth);
468 * Copy the data to the target buffer.
469 * Length is the field width in bytes.
471 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
472 ObjDesc->BufferField.BaseByteOffset +
473 FieldDatumByteOffset,
474 Value, ObjDesc->CommonField.AccessByteWidth);
480 case ACPI_TYPE_LOCAL_BANK_FIELD:
482 * Ensure that the BankValue is not beyond the capacity of
485 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
486 (UINT64) ObjDesc->BankField.Value))
488 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
492 * For BankFields, we must write the BankValue to the BankRegister
493 * (itself a RegionField) before we can access the data.
495 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
496 &ObjDesc->BankField.Value,
497 sizeof (ObjDesc->BankField.Value));
498 if (ACPI_FAILURE (Status))
500 return_ACPI_STATUS (Status);
504 * Now that the Bank has been selected, fall through to the
505 * RegionField case and write the datum to the Operation Region
508 /*lint -fallthrough */
510 case ACPI_TYPE_LOCAL_REGION_FIELD:
512 * For simple RegionFields, we just directly access the owning
515 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
519 case ACPI_TYPE_LOCAL_INDEX_FIELD:
521 * Ensure that the IndexValue is not beyond the capacity of
524 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
525 (UINT64) ObjDesc->IndexField.Value))
527 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
530 /* Write the index value to the IndexRegister (itself a RegionField) */
532 FieldDatumByteOffset += ObjDesc->IndexField.Value;
534 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
535 "Write to Index Register: Value %8.8X\n",
536 FieldDatumByteOffset));
538 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
539 &FieldDatumByteOffset,
540 sizeof (FieldDatumByteOffset));
541 if (ACPI_FAILURE (Status))
543 return_ACPI_STATUS (Status);
546 if (ReadWrite == ACPI_READ)
548 /* Read the datum from the DataRegister */
550 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
551 "Read from Data Register\n"));
553 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
554 Value, sizeof (UINT64));
558 /* Write the datum to the DataRegister */
560 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
561 "Write to Data Register: Value %8.8X%8.8X\n",
562 ACPI_FORMAT_UINT64 (*Value)));
564 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
565 Value, sizeof (UINT64));
571 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
572 ObjDesc->Common.Type));
573 Status = AE_AML_INTERNAL;
577 if (ACPI_SUCCESS (Status))
579 if (ReadWrite == ACPI_READ)
581 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
582 "Value Read %8.8X%8.8X, Width %u\n",
583 ACPI_FORMAT_UINT64 (*Value),
584 ObjDesc->CommonField.AccessByteWidth));
588 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
589 "Value Written %8.8X%8.8X, Width %u\n",
590 ACPI_FORMAT_UINT64 (*Value),
591 ObjDesc->CommonField.AccessByteWidth));
595 return_ACPI_STATUS (Status);
599 /*******************************************************************************
601 * FUNCTION: AcpiExWriteWithUpdateRule
603 * PARAMETERS: ObjDesc - Field to be written
604 * Mask - bitmask within field datum
605 * FieldValue - Value to write
606 * FieldDatumByteOffset - Offset of datum within field
610 * DESCRIPTION: Apply the field update rule to a field write
612 ******************************************************************************/
615 AcpiExWriteWithUpdateRule (
616 ACPI_OPERAND_OBJECT *ObjDesc,
619 UINT32 FieldDatumByteOffset)
621 ACPI_STATUS Status = AE_OK;
626 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
629 /* Start with the new bits */
631 MergedValue = FieldValue;
633 /* If the mask is all ones, we don't need to worry about the update rule */
635 if (Mask != ACPI_UINT64_MAX)
637 /* Decode the update rule */
639 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
641 case AML_FIELD_UPDATE_PRESERVE:
643 * Check if update rule needs to be applied (not if mask is all
644 * ones) The left shift drops the bits we want to ignore.
646 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
647 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
650 * Read the current contents of the byte/word/dword containing
651 * the field, and merge with the new field value.
653 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
654 &CurrentValue, ACPI_READ);
655 if (ACPI_FAILURE (Status))
657 return_ACPI_STATUS (Status);
660 MergedValue |= (CurrentValue & ~Mask);
664 case AML_FIELD_UPDATE_WRITE_AS_ONES:
666 /* Set positions outside the field to all ones */
668 MergedValue |= ~Mask;
671 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
673 /* Set positions outside the field to all zeros */
680 ACPI_ERROR ((AE_INFO,
681 "Unknown UpdateRule value: 0x%X",
682 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
683 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
687 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
688 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
689 ACPI_FORMAT_UINT64 (Mask),
690 FieldDatumByteOffset,
691 ObjDesc->CommonField.AccessByteWidth,
692 ACPI_FORMAT_UINT64 (FieldValue),
693 ACPI_FORMAT_UINT64 (MergedValue)));
695 /* Write the merged value */
697 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
698 &MergedValue, ACPI_WRITE);
700 return_ACPI_STATUS (Status);
704 /*******************************************************************************
706 * FUNCTION: AcpiExExtractFromField
708 * PARAMETERS: ObjDesc - Field to be read
709 * Buffer - Where to store the field data
710 * BufferLength - Length of Buffer
714 * DESCRIPTION: Retrieve the current value of the given field
716 ******************************************************************************/
719 AcpiExExtractFromField (
720 ACPI_OPERAND_OBJECT *ObjDesc,
727 UINT32 FieldOffset = 0;
728 UINT32 BufferOffset = 0;
729 UINT32 BufferTailBits;
731 UINT32 FieldDatumCount;
732 UINT32 AccessBitWidth;
736 ACPI_FUNCTION_TRACE (ExExtractFromField);
739 /* Validate target buffer and clear it */
742 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
744 ACPI_ERROR ((AE_INFO,
745 "Field size %u (bits) is too large for buffer (%u)",
746 ObjDesc->CommonField.BitLength, BufferLength));
748 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
751 ACPI_MEMSET (Buffer, 0, BufferLength);
752 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
754 /* Handle the simple case here */
756 if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
757 (ObjDesc->CommonField.BitLength == AccessBitWidth))
759 if (BufferLength >= sizeof (UINT64))
761 Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
765 /* Use RawDatum (UINT64) to handle buffers < 64 bits */
767 Status = AcpiExFieldDatumIo (ObjDesc, 0, &RawDatum, ACPI_READ);
768 ACPI_MEMCPY (Buffer, &RawDatum, BufferLength);
771 return_ACPI_STATUS (Status);
774 /* TBD: Move to common setup code */
776 /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
778 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
780 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
781 AccessBitWidth = sizeof (UINT64) * 8;
784 /* Compute the number of datums (access width data items) */
786 DatumCount = ACPI_ROUND_UP_TO (
787 ObjDesc->CommonField.BitLength, AccessBitWidth);
789 FieldDatumCount = ACPI_ROUND_UP_TO (
790 ObjDesc->CommonField.BitLength +
791 ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
793 /* Priming read from the field */
795 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
796 if (ACPI_FAILURE (Status))
798 return_ACPI_STATUS (Status);
800 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
802 /* Read the rest of the field */
804 for (i = 1; i < FieldDatumCount; i++)
806 /* Get next input datum from the field */
808 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
809 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
810 &RawDatum, ACPI_READ);
811 if (ACPI_FAILURE (Status))
813 return_ACPI_STATUS (Status);
817 * Merge with previous datum if necessary.
819 * Note: Before the shift, check if the shift value will be larger than
820 * the integer size. If so, there is no need to perform the operation.
821 * This avoids the differences in behavior between different compilers
822 * concerning shift values larger than the target data width.
824 if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
825 ACPI_INTEGER_BIT_SIZE)
827 MergedDatum |= RawDatum <<
828 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
836 /* Write merged datum to target buffer */
838 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
839 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
840 BufferLength - BufferOffset));
842 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
843 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
846 /* Mask off any extra bits in the last datum */
848 BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
851 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
854 /* Write the last datum to the buffer */
856 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
857 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
858 BufferLength - BufferOffset));
860 return_ACPI_STATUS (AE_OK);
864 /*******************************************************************************
866 * FUNCTION: AcpiExInsertIntoField
868 * PARAMETERS: ObjDesc - Field to be written
869 * Buffer - Data to be written
870 * BufferLength - Length of Buffer
874 * DESCRIPTION: Store the Buffer contents into the given field
876 ******************************************************************************/
879 AcpiExInsertIntoField (
880 ACPI_OPERAND_OBJECT *ObjDesc,
890 UINT32 FieldOffset = 0;
891 UINT32 BufferOffset = 0;
892 UINT32 BufferTailBits;
894 UINT32 FieldDatumCount;
895 UINT32 AccessBitWidth;
896 UINT32 RequiredLength;
900 ACPI_FUNCTION_TRACE (ExInsertIntoField);
903 /* Validate input buffer */
906 RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
907 ObjDesc->CommonField.BitLength);
909 * We must have a buffer that is at least as long as the field
910 * we are writing to. This is because individual fields are
911 * indivisible and partial writes are not supported -- as per
912 * the ACPI specification.
914 if (BufferLength < RequiredLength)
916 /* We need to create a new buffer */
918 NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
921 return_ACPI_STATUS (AE_NO_MEMORY);
925 * Copy the original data to the new buffer, starting
926 * at Byte zero. All unused (upper) bytes of the
929 ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
931 BufferLength = RequiredLength;
934 /* TBD: Move to common setup code */
936 /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
937 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
939 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
942 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
945 * Create the bitmasks used for bit insertion.
946 * Note: This if/else is used to bypass compiler differences with the
949 if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
951 WidthMask = ACPI_UINT64_MAX;
955 WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
959 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
961 /* Compute the number of datums (access width data items) */
963 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
966 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
967 ObjDesc->CommonField.StartFieldBitOffset,
970 /* Get initial Datum from the input buffer */
972 ACPI_MEMCPY (&RawDatum, Buffer,
973 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
974 BufferLength - BufferOffset));
976 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
978 /* Write the entire field */
980 for (i = 1; i < FieldDatumCount; i++)
982 /* Write merged datum to the target field */
985 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
986 MergedDatum, FieldOffset);
987 if (ACPI_FAILURE (Status))
992 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
995 * Start new output datum by merging with previous input datum
998 * Note: Before the shift, check if the shift value will be larger than
999 * the integer size. If so, there is no need to perform the operation.
1000 * This avoids the differences in behavior between different compilers
1001 * concerning shift values larger than the target data width.
1003 if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1004 ACPI_INTEGER_BIT_SIZE)
1006 MergedDatum = RawDatum >>
1007 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1016 if (i == DatumCount)
1021 /* Get the next input datum from the buffer */
1023 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1024 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1025 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1026 BufferLength - BufferOffset));
1028 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1031 /* Mask off any extra bits in the last datum */
1033 BufferTailBits = (ObjDesc->CommonField.BitLength +
1034 ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1037 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1040 /* Write the last datum to the field */
1042 MergedDatum &= Mask;
1043 Status = AcpiExWriteWithUpdateRule (ObjDesc,
1044 Mask, MergedDatum, FieldOffset);
1047 /* Free temporary buffer if we used one */
1051 ACPI_FREE (NewBuffer);
1053 return_ACPI_STATUS (Status);