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.
54 #define _COMPONENT ACPI_EXECUTER
55 ACPI_MODULE_NAME ("exfldio")
57 /* Local prototypes */
61 ACPI_OPERAND_OBJECT *ObjDesc,
62 UINT32 FieldDatumByteOffset,
67 AcpiExRegisterOverflow (
68 ACPI_OPERAND_OBJECT *ObjDesc,
73 ACPI_OPERAND_OBJECT *ObjDesc,
74 UINT32 FieldDatumByteOffset);
77 /*******************************************************************************
79 * FUNCTION: AcpiExSetupRegion
81 * PARAMETERS: ObjDesc - Field to be read or written
82 * FieldDatumByteOffset - Byte offset of this datum within the
87 * DESCRIPTION: Common processing for AcpiExExtractFromField and
88 * AcpiExInsertIntoField. Initialize the Region if necessary and
89 * validate the request.
91 ******************************************************************************/
95 ACPI_OPERAND_OBJECT *ObjDesc,
96 UINT32 FieldDatumByteOffset)
98 ACPI_STATUS Status = AE_OK;
99 ACPI_OPERAND_OBJECT *RgnDesc;
103 ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
106 RgnDesc = ObjDesc->CommonField.RegionObj;
108 /* We must have a valid region */
110 if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
112 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
113 RgnDesc->Common.Type,
114 AcpiUtGetObjectTypeName (RgnDesc)));
116 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
119 SpaceId = RgnDesc->Region.SpaceId;
121 /* Validate the Space ID */
123 if (!AcpiIsValidSpaceId (SpaceId))
125 ACPI_ERROR ((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
126 return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
130 * If the Region Address and Length have not been previously evaluated,
131 * evaluate them now and save the results.
133 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
135 Status = AcpiDsGetRegionArguments (RgnDesc);
136 if (ACPI_FAILURE (Status))
138 return_ACPI_STATUS (Status);
143 * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
144 * address space and the request cannot be directly validated
146 if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
147 SpaceId == ACPI_ADR_SPACE_GSBUS ||
148 SpaceId == ACPI_ADR_SPACE_IPMI)
150 /* SMBus or IPMI has a non-linear address space */
152 return_ACPI_STATUS (AE_OK);
155 #ifdef ACPI_UNDER_DEVELOPMENT
157 * If the Field access is AnyAcc, we can now compute the optimal
158 * access (because we know know the length of the parent region)
160 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
162 if (ACPI_FAILURE (Status))
164 return_ACPI_STATUS (Status);
170 * Validate the request. The entire request from the byte offset for a
171 * length of one field datum (access width) must fit within the region.
172 * (Region length is specified in bytes)
174 if (RgnDesc->Region.Length <
175 (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
176 ObjDesc->CommonField.AccessByteWidth))
178 if (AcpiGbl_EnableInterpreterSlack)
181 * Slack mode only: We will go ahead and allow access to this
182 * field if it is within the region length rounded up to the next
183 * access width boundary. ACPI_SIZE cast for 64-bit compile.
185 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
186 ObjDesc->CommonField.AccessByteWidth) >=
187 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
188 ObjDesc->CommonField.AccessByteWidth +
189 FieldDatumByteOffset))
191 return_ACPI_STATUS (AE_OK);
195 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
198 * This is the case where the AccessType (AccWord, etc.) is wider
199 * than the region itself. For example, a region of length one
200 * byte, and a field with Dword access specified.
202 ACPI_ERROR ((AE_INFO,
203 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
204 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
205 ObjDesc->CommonField.AccessByteWidth,
206 AcpiUtGetNodeName (RgnDesc->Region.Node),
207 RgnDesc->Region.Length));
211 * Offset rounded up to next multiple of field width
212 * exceeds region length, indicate an error
214 ACPI_ERROR ((AE_INFO,
215 "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
216 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
217 ObjDesc->CommonField.BaseByteOffset,
218 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
219 AcpiUtGetNodeName (RgnDesc->Region.Node),
220 RgnDesc->Region.Length));
222 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
225 return_ACPI_STATUS (AE_OK);
229 /*******************************************************************************
231 * FUNCTION: AcpiExAccessRegion
233 * PARAMETERS: ObjDesc - Field to be read
234 * FieldDatumByteOffset - Byte offset of this datum within the
236 * Value - Where to store value (must at least
238 * Function - Read or Write flag plus other region-
243 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
245 ******************************************************************************/
249 ACPI_OPERAND_OBJECT *ObjDesc,
250 UINT32 FieldDatumByteOffset,
255 ACPI_OPERAND_OBJECT *RgnDesc;
259 ACPI_FUNCTION_TRACE (ExAccessRegion);
263 * Ensure that the region operands are fully evaluated and verify
264 * the validity of the request
266 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
267 if (ACPI_FAILURE (Status))
269 return_ACPI_STATUS (Status);
273 * The physical address of this field datum is:
275 * 1) The base of the region, plus
276 * 2) The base offset of the field, plus
277 * 3) The current offset into the field
279 RgnDesc = ObjDesc->CommonField.RegionObj;
281 ObjDesc->CommonField.BaseByteOffset +
282 FieldDatumByteOffset;
284 if ((Function & ACPI_IO_MASK) == ACPI_READ)
286 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
290 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
293 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
294 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
295 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
296 RgnDesc->Region.SpaceId,
297 ObjDesc->CommonField.AccessByteWidth,
298 ObjDesc->CommonField.BaseByteOffset,
299 FieldDatumByteOffset,
300 ACPI_CAST_PTR (void, (RgnDesc->Region.Address + RegionOffset))));
302 /* Invoke the appropriate AddressSpace/OpRegion handler */
304 Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
305 Function, RegionOffset,
306 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
308 if (ACPI_FAILURE (Status))
310 if (Status == AE_NOT_IMPLEMENTED)
312 ACPI_ERROR ((AE_INFO,
313 "Region %s (ID=%u) not implemented",
314 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
315 RgnDesc->Region.SpaceId));
317 else if (Status == AE_NOT_EXIST)
319 ACPI_ERROR ((AE_INFO,
320 "Region %s (ID=%u) has no handler",
321 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
322 RgnDesc->Region.SpaceId));
326 return_ACPI_STATUS (Status);
330 /*******************************************************************************
332 * FUNCTION: AcpiExRegisterOverflow
334 * PARAMETERS: ObjDesc - Register(Field) to be written
335 * Value - Value to be stored
337 * RETURN: TRUE if value overflows the field, FALSE otherwise
339 * DESCRIPTION: Check if a value is out of range of the field being written.
340 * Used to check if the values written to Index and Bank registers
341 * are out of range. Normally, the value is simply truncated
342 * to fit the field, but this case is most likely a serious
343 * coding error in the ASL.
345 ******************************************************************************/
348 AcpiExRegisterOverflow (
349 ACPI_OPERAND_OBJECT *ObjDesc,
353 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
356 * The field is large enough to hold the maximum integer, so we can
362 if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
365 * The Value is larger than the maximum value that can fit into
368 ACPI_ERROR ((AE_INFO,
369 "Index value 0x%8.8X%8.8X overflows field width 0x%X",
370 ACPI_FORMAT_UINT64 (Value),
371 ObjDesc->CommonField.BitLength));
376 /* The Value will fit into the field with no truncation */
382 /*******************************************************************************
384 * FUNCTION: AcpiExFieldDatumIo
386 * PARAMETERS: ObjDesc - Field to be read
387 * FieldDatumByteOffset - Byte offset of this datum within the
389 * Value - Where to store value (must be 64 bits)
390 * ReadWrite - Read or Write flag
394 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
395 * demultiplexed here to handle the different types of fields
396 * (BufferField, RegionField, IndexField, BankField)
398 ******************************************************************************/
402 ACPI_OPERAND_OBJECT *ObjDesc,
403 UINT32 FieldDatumByteOffset,
411 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
414 if (ReadWrite == ACPI_READ)
420 /* To support reads without saving return value */
424 /* Clear the entire return buffer first, [Very Important!] */
430 * The four types of fields are:
432 * BufferField - Read/write from/to a Buffer
433 * RegionField - Read/write from/to a Operation Region.
434 * BankField - Write to a Bank Register, then read/write from/to an
436 * IndexField - Write to an Index Register, then read/write from/to a
439 switch (ObjDesc->Common.Type)
441 case ACPI_TYPE_BUFFER_FIELD:
443 * If the BufferField arguments have not been previously evaluated,
444 * evaluate them now and save the results.
446 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
448 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
449 if (ACPI_FAILURE (Status))
451 return_ACPI_STATUS (Status);
455 if (ReadWrite == ACPI_READ)
458 * Copy the data from the source buffer.
459 * Length is the field width in bytes.
462 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
463 ObjDesc->BufferField.BaseByteOffset +
464 FieldDatumByteOffset,
465 ObjDesc->CommonField.AccessByteWidth);
470 * Copy the data to the target buffer.
471 * Length is the field width in bytes.
473 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
474 ObjDesc->BufferField.BaseByteOffset +
475 FieldDatumByteOffset,
476 Value, ObjDesc->CommonField.AccessByteWidth);
482 case ACPI_TYPE_LOCAL_BANK_FIELD:
484 * Ensure that the BankValue is not beyond the capacity of
487 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
488 (UINT64) ObjDesc->BankField.Value))
490 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
494 * For BankFields, we must write the BankValue to the BankRegister
495 * (itself a RegionField) before we can access the data.
497 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
498 &ObjDesc->BankField.Value,
499 sizeof (ObjDesc->BankField.Value));
500 if (ACPI_FAILURE (Status))
502 return_ACPI_STATUS (Status);
506 * Now that the Bank has been selected, fall through to the
507 * RegionField case and write the datum to the Operation Region
510 /*lint -fallthrough */
512 case ACPI_TYPE_LOCAL_REGION_FIELD:
514 * For simple RegionFields, we just directly access the owning
517 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
521 case ACPI_TYPE_LOCAL_INDEX_FIELD:
523 * Ensure that the IndexValue is not beyond the capacity of
526 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
527 (UINT64) ObjDesc->IndexField.Value))
529 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
532 /* Write the index value to the IndexRegister (itself a RegionField) */
534 FieldDatumByteOffset += ObjDesc->IndexField.Value;
536 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
537 "Write to Index Register: Value %8.8X\n",
538 FieldDatumByteOffset));
540 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
541 &FieldDatumByteOffset,
542 sizeof (FieldDatumByteOffset));
543 if (ACPI_FAILURE (Status))
545 return_ACPI_STATUS (Status);
548 if (ReadWrite == ACPI_READ)
550 /* Read the datum from the DataRegister */
552 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
553 "Read from Data Register\n"));
555 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
556 Value, sizeof (UINT64));
560 /* Write the datum to the DataRegister */
562 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
563 "Write to Data Register: Value %8.8X%8.8X\n",
564 ACPI_FORMAT_UINT64 (*Value)));
566 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
567 Value, sizeof (UINT64));
573 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
574 ObjDesc->Common.Type));
575 Status = AE_AML_INTERNAL;
579 if (ACPI_SUCCESS (Status))
581 if (ReadWrite == ACPI_READ)
583 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
584 "Value Read %8.8X%8.8X, Width %u\n",
585 ACPI_FORMAT_UINT64 (*Value),
586 ObjDesc->CommonField.AccessByteWidth));
590 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
591 "Value Written %8.8X%8.8X, Width %u\n",
592 ACPI_FORMAT_UINT64 (*Value),
593 ObjDesc->CommonField.AccessByteWidth));
597 return_ACPI_STATUS (Status);
601 /*******************************************************************************
603 * FUNCTION: AcpiExWriteWithUpdateRule
605 * PARAMETERS: ObjDesc - Field to be written
606 * Mask - bitmask within field datum
607 * FieldValue - Value to write
608 * FieldDatumByteOffset - Offset of datum within field
612 * DESCRIPTION: Apply the field update rule to a field write
614 ******************************************************************************/
617 AcpiExWriteWithUpdateRule (
618 ACPI_OPERAND_OBJECT *ObjDesc,
621 UINT32 FieldDatumByteOffset)
623 ACPI_STATUS Status = AE_OK;
628 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
631 /* Start with the new bits */
633 MergedValue = FieldValue;
635 /* If the mask is all ones, we don't need to worry about the update rule */
637 if (Mask != ACPI_UINT64_MAX)
639 /* Decode the update rule */
641 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
643 case AML_FIELD_UPDATE_PRESERVE:
645 * Check if update rule needs to be applied (not if mask is all
646 * ones) The left shift drops the bits we want to ignore.
648 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
649 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
652 * Read the current contents of the byte/word/dword containing
653 * the field, and merge with the new field value.
655 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
656 &CurrentValue, ACPI_READ);
657 if (ACPI_FAILURE (Status))
659 return_ACPI_STATUS (Status);
662 MergedValue |= (CurrentValue & ~Mask);
666 case AML_FIELD_UPDATE_WRITE_AS_ONES:
668 /* Set positions outside the field to all ones */
670 MergedValue |= ~Mask;
673 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
675 /* Set positions outside the field to all zeros */
682 ACPI_ERROR ((AE_INFO,
683 "Unknown UpdateRule value: 0x%X",
684 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
685 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
689 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
690 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
691 ACPI_FORMAT_UINT64 (Mask),
692 FieldDatumByteOffset,
693 ObjDesc->CommonField.AccessByteWidth,
694 ACPI_FORMAT_UINT64 (FieldValue),
695 ACPI_FORMAT_UINT64 (MergedValue)));
697 /* Write the merged value */
699 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
700 &MergedValue, ACPI_WRITE);
702 return_ACPI_STATUS (Status);
706 /*******************************************************************************
708 * FUNCTION: AcpiExExtractFromField
710 * PARAMETERS: ObjDesc - Field to be read
711 * Buffer - Where to store the field data
712 * BufferLength - Length of Buffer
716 * DESCRIPTION: Retrieve the current value of the given field
718 ******************************************************************************/
721 AcpiExExtractFromField (
722 ACPI_OPERAND_OBJECT *ObjDesc,
729 UINT32 FieldOffset = 0;
730 UINT32 BufferOffset = 0;
731 UINT32 BufferTailBits;
733 UINT32 FieldDatumCount;
734 UINT32 AccessBitWidth;
738 ACPI_FUNCTION_TRACE (ExExtractFromField);
741 /* Validate target buffer and clear it */
744 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
746 ACPI_ERROR ((AE_INFO,
747 "Field size %u (bits) is too large for buffer (%u)",
748 ObjDesc->CommonField.BitLength, BufferLength));
750 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
753 ACPI_MEMSET (Buffer, 0, BufferLength);
754 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
756 /* Handle the simple case here */
758 if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
759 (ObjDesc->CommonField.BitLength == AccessBitWidth))
761 if (BufferLength >= sizeof (UINT64))
763 Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
767 /* Use RawDatum (UINT64) to handle buffers < 64 bits */
769 Status = AcpiExFieldDatumIo (ObjDesc, 0, &RawDatum, ACPI_READ);
770 ACPI_MEMCPY (Buffer, &RawDatum, BufferLength);
773 return_ACPI_STATUS (Status);
776 /* TBD: Move to common setup code */
778 /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
780 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
782 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
783 AccessBitWidth = sizeof (UINT64) * 8;
786 /* Compute the number of datums (access width data items) */
788 DatumCount = ACPI_ROUND_UP_TO (
789 ObjDesc->CommonField.BitLength, AccessBitWidth);
791 FieldDatumCount = ACPI_ROUND_UP_TO (
792 ObjDesc->CommonField.BitLength +
793 ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
795 /* Priming read from the field */
797 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
798 if (ACPI_FAILURE (Status))
800 return_ACPI_STATUS (Status);
802 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
804 /* Read the rest of the field */
806 for (i = 1; i < FieldDatumCount; i++)
808 /* Get next input datum from the field */
810 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
811 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
812 &RawDatum, ACPI_READ);
813 if (ACPI_FAILURE (Status))
815 return_ACPI_STATUS (Status);
819 * Merge with previous datum if necessary.
821 * Note: Before the shift, check if the shift value will be larger than
822 * the integer size. If so, there is no need to perform the operation.
823 * This avoids the differences in behavior between different compilers
824 * concerning shift values larger than the target data width.
826 if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
827 ACPI_INTEGER_BIT_SIZE)
829 MergedDatum |= RawDatum <<
830 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
838 /* Write merged datum to target buffer */
840 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
841 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
842 BufferLength - BufferOffset));
844 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
845 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
848 /* Mask off any extra bits in the last datum */
850 BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
853 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
856 /* Write the last datum to the buffer */
858 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
859 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
860 BufferLength - BufferOffset));
862 return_ACPI_STATUS (AE_OK);
866 /*******************************************************************************
868 * FUNCTION: AcpiExInsertIntoField
870 * PARAMETERS: ObjDesc - Field to be written
871 * Buffer - Data to be written
872 * BufferLength - Length of Buffer
876 * DESCRIPTION: Store the Buffer contents into the given field
878 ******************************************************************************/
881 AcpiExInsertIntoField (
882 ACPI_OPERAND_OBJECT *ObjDesc,
892 UINT32 FieldOffset = 0;
893 UINT32 BufferOffset = 0;
894 UINT32 BufferTailBits;
896 UINT32 FieldDatumCount;
897 UINT32 AccessBitWidth;
898 UINT32 RequiredLength;
902 ACPI_FUNCTION_TRACE (ExInsertIntoField);
905 /* Validate input buffer */
908 RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
909 ObjDesc->CommonField.BitLength);
911 * We must have a buffer that is at least as long as the field
912 * we are writing to. This is because individual fields are
913 * indivisible and partial writes are not supported -- as per
914 * the ACPI specification.
916 if (BufferLength < RequiredLength)
918 /* We need to create a new buffer */
920 NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
923 return_ACPI_STATUS (AE_NO_MEMORY);
927 * Copy the original data to the new buffer, starting
928 * at Byte zero. All unused (upper) bytes of the
931 ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
933 BufferLength = RequiredLength;
936 /* TBD: Move to common setup code */
938 /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
939 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
941 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
944 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
947 * Create the bitmasks used for bit insertion.
948 * Note: This if/else is used to bypass compiler differences with the
951 if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
953 WidthMask = ACPI_UINT64_MAX;
957 WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
961 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
963 /* Compute the number of datums (access width data items) */
965 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
968 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
969 ObjDesc->CommonField.StartFieldBitOffset,
972 /* Get initial Datum from the input buffer */
974 ACPI_MEMCPY (&RawDatum, Buffer,
975 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
976 BufferLength - BufferOffset));
978 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
980 /* Write the entire field */
982 for (i = 1; i < FieldDatumCount; i++)
984 /* Write merged datum to the target field */
987 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
988 MergedDatum, FieldOffset);
989 if (ACPI_FAILURE (Status))
994 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
997 * Start new output datum by merging with previous input datum
1000 * Note: Before the shift, check if the shift value will be larger than
1001 * the integer size. If so, there is no need to perform the operation.
1002 * This avoids the differences in behavior between different compilers
1003 * concerning shift values larger than the target data width.
1005 if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1006 ACPI_INTEGER_BIT_SIZE)
1008 MergedDatum = RawDatum >>
1009 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1018 if (i == DatumCount)
1023 /* Get the next input datum from the buffer */
1025 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1026 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1027 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1028 BufferLength - BufferOffset));
1030 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1033 /* Mask off any extra bits in the last datum */
1035 BufferTailBits = (ObjDesc->CommonField.BitLength +
1036 ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1039 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1042 /* Write the last datum to the field */
1044 MergedDatum &= Mask;
1045 Status = AcpiExWriteWithUpdateRule (ObjDesc,
1046 Mask, MergedDatum, FieldOffset);
1049 /* Free temporary buffer if we used one */
1053 ACPI_FREE (NewBuffer);
1055 return_ACPI_STATUS (Status);