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;
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);
120 * If the Region Address and Length have not been previously evaluated,
121 * evaluate them now and save the results.
123 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
125 Status = AcpiDsGetRegionArguments (RgnDesc);
126 if (ACPI_FAILURE (Status))
128 return_ACPI_STATUS (Status);
133 * Exit now for SMBus or IPMI address space, it has a non-linear
134 * address space and the request cannot be directly validated
136 if (RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
137 RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_IPMI)
139 /* SMBus or IPMI has a non-linear address space */
141 return_ACPI_STATUS (AE_OK);
144 #ifdef ACPI_UNDER_DEVELOPMENT
146 * If the Field access is AnyAcc, we can now compute the optimal
147 * access (because we know know the length of the parent region)
149 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
151 if (ACPI_FAILURE (Status))
153 return_ACPI_STATUS (Status);
159 * Validate the request. The entire request from the byte offset for a
160 * length of one field datum (access width) must fit within the region.
161 * (Region length is specified in bytes)
163 if (RgnDesc->Region.Length <
164 (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
165 ObjDesc->CommonField.AccessByteWidth))
167 if (AcpiGbl_EnableInterpreterSlack)
170 * Slack mode only: We will go ahead and allow access to this
171 * field if it is within the region length rounded up to the next
172 * access width boundary. ACPI_SIZE cast for 64-bit compile.
174 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
175 ObjDesc->CommonField.AccessByteWidth) >=
176 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
177 ObjDesc->CommonField.AccessByteWidth +
178 FieldDatumByteOffset))
180 return_ACPI_STATUS (AE_OK);
184 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
187 * This is the case where the AccessType (AccWord, etc.) is wider
188 * than the region itself. For example, a region of length one
189 * byte, and a field with Dword access specified.
191 ACPI_ERROR ((AE_INFO,
192 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
193 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
194 ObjDesc->CommonField.AccessByteWidth,
195 AcpiUtGetNodeName (RgnDesc->Region.Node),
196 RgnDesc->Region.Length));
200 * Offset rounded up to next multiple of field width
201 * exceeds region length, indicate an error
203 ACPI_ERROR ((AE_INFO,
204 "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
205 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
206 ObjDesc->CommonField.BaseByteOffset,
207 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
208 AcpiUtGetNodeName (RgnDesc->Region.Node),
209 RgnDesc->Region.Length));
211 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
214 return_ACPI_STATUS (AE_OK);
218 /*******************************************************************************
220 * FUNCTION: AcpiExAccessRegion
222 * PARAMETERS: ObjDesc - Field to be read
223 * FieldDatumByteOffset - Byte offset of this datum within the
225 * Value - Where to store value (must at least
227 * Function - Read or Write flag plus other region-
232 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
234 ******************************************************************************/
238 ACPI_OPERAND_OBJECT *ObjDesc,
239 UINT32 FieldDatumByteOffset,
244 ACPI_OPERAND_OBJECT *RgnDesc;
248 ACPI_FUNCTION_TRACE (ExAccessRegion);
252 * Ensure that the region operands are fully evaluated and verify
253 * the validity of the request
255 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
256 if (ACPI_FAILURE (Status))
258 return_ACPI_STATUS (Status);
262 * The physical address of this field datum is:
264 * 1) The base of the region, plus
265 * 2) The base offset of the field, plus
266 * 3) The current offset into the field
268 RgnDesc = ObjDesc->CommonField.RegionObj;
270 ObjDesc->CommonField.BaseByteOffset +
271 FieldDatumByteOffset;
273 if ((Function & ACPI_IO_MASK) == ACPI_READ)
275 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
279 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
282 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
283 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
284 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
285 RgnDesc->Region.SpaceId,
286 ObjDesc->CommonField.AccessByteWidth,
287 ObjDesc->CommonField.BaseByteOffset,
288 FieldDatumByteOffset,
289 ACPI_CAST_PTR (void, (RgnDesc->Region.Address + RegionOffset))));
291 /* Invoke the appropriate AddressSpace/OpRegion handler */
293 Status = AcpiEvAddressSpaceDispatch (RgnDesc, Function, RegionOffset,
294 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
296 if (ACPI_FAILURE (Status))
298 if (Status == AE_NOT_IMPLEMENTED)
300 ACPI_ERROR ((AE_INFO,
301 "Region %s (ID=%u) not implemented",
302 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
303 RgnDesc->Region.SpaceId));
305 else if (Status == AE_NOT_EXIST)
307 ACPI_ERROR ((AE_INFO,
308 "Region %s (ID=%u) has no handler",
309 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
310 RgnDesc->Region.SpaceId));
314 return_ACPI_STATUS (Status);
318 /*******************************************************************************
320 * FUNCTION: AcpiExRegisterOverflow
322 * PARAMETERS: ObjDesc - Register(Field) to be written
323 * Value - Value to be stored
325 * RETURN: TRUE if value overflows the field, FALSE otherwise
327 * DESCRIPTION: Check if a value is out of range of the field being written.
328 * Used to check if the values written to Index and Bank registers
329 * are out of range. Normally, the value is simply truncated
330 * to fit the field, but this case is most likely a serious
331 * coding error in the ASL.
333 ******************************************************************************/
336 AcpiExRegisterOverflow (
337 ACPI_OPERAND_OBJECT *ObjDesc,
341 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
344 * The field is large enough to hold the maximum integer, so we can
350 if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
353 * The Value is larger than the maximum value that can fit into
359 /* The Value will fit into the field with no truncation */
365 /*******************************************************************************
367 * FUNCTION: AcpiExFieldDatumIo
369 * PARAMETERS: ObjDesc - Field to be read
370 * FieldDatumByteOffset - Byte offset of this datum within the
372 * Value - Where to store value (must be 64 bits)
373 * ReadWrite - Read or Write flag
377 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
378 * demultiplexed here to handle the different types of fields
379 * (BufferField, RegionField, IndexField, BankField)
381 ******************************************************************************/
385 ACPI_OPERAND_OBJECT *ObjDesc,
386 UINT32 FieldDatumByteOffset,
394 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
397 if (ReadWrite == ACPI_READ)
403 /* To support reads without saving return value */
407 /* Clear the entire return buffer first, [Very Important!] */
413 * The four types of fields are:
415 * BufferField - Read/write from/to a Buffer
416 * RegionField - Read/write from/to a Operation Region.
417 * BankField - Write to a Bank Register, then read/write from/to an
419 * IndexField - Write to an Index Register, then read/write from/to a
422 switch (ObjDesc->Common.Type)
424 case ACPI_TYPE_BUFFER_FIELD:
426 * If the BufferField arguments have not been previously evaluated,
427 * evaluate them now and save the results.
429 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
431 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
432 if (ACPI_FAILURE (Status))
434 return_ACPI_STATUS (Status);
438 if (ReadWrite == ACPI_READ)
441 * Copy the data from the source buffer.
442 * Length is the field width in bytes.
445 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
446 ObjDesc->BufferField.BaseByteOffset +
447 FieldDatumByteOffset,
448 ObjDesc->CommonField.AccessByteWidth);
453 * Copy the data to the target buffer.
454 * Length is the field width in bytes.
456 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
457 ObjDesc->BufferField.BaseByteOffset +
458 FieldDatumByteOffset,
459 Value, ObjDesc->CommonField.AccessByteWidth);
466 case ACPI_TYPE_LOCAL_BANK_FIELD:
469 * Ensure that the BankValue is not beyond the capacity of
472 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
473 (UINT64) ObjDesc->BankField.Value))
475 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
479 * For BankFields, we must write the BankValue to the BankRegister
480 * (itself a RegionField) before we can access the data.
482 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
483 &ObjDesc->BankField.Value,
484 sizeof (ObjDesc->BankField.Value));
485 if (ACPI_FAILURE (Status))
487 return_ACPI_STATUS (Status);
491 * Now that the Bank has been selected, fall through to the
492 * RegionField case and write the datum to the Operation Region
495 /*lint -fallthrough */
498 case ACPI_TYPE_LOCAL_REGION_FIELD:
500 * For simple RegionFields, we just directly access the owning
503 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
508 case ACPI_TYPE_LOCAL_INDEX_FIELD:
512 * Ensure that the IndexValue is not beyond the capacity of
515 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
516 (UINT64) ObjDesc->IndexField.Value))
518 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
521 /* Write the index value to the IndexRegister (itself a RegionField) */
523 FieldDatumByteOffset += ObjDesc->IndexField.Value;
525 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
526 "Write to Index Register: Value %8.8X\n",
527 FieldDatumByteOffset));
529 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
530 &FieldDatumByteOffset,
531 sizeof (FieldDatumByteOffset));
532 if (ACPI_FAILURE (Status))
534 return_ACPI_STATUS (Status);
537 if (ReadWrite == ACPI_READ)
539 /* Read the datum from the DataRegister */
541 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
542 "Read from Data Register\n"));
544 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
545 Value, sizeof (UINT64));
549 /* Write the datum to the DataRegister */
551 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
552 "Write to Data Register: Value %8.8X%8.8X\n",
553 ACPI_FORMAT_UINT64 (*Value)));
555 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
556 Value, sizeof (UINT64));
563 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
564 ObjDesc->Common.Type));
565 Status = AE_AML_INTERNAL;
569 if (ACPI_SUCCESS (Status))
571 if (ReadWrite == ACPI_READ)
573 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
574 "Value Read %8.8X%8.8X, Width %u\n",
575 ACPI_FORMAT_UINT64 (*Value),
576 ObjDesc->CommonField.AccessByteWidth));
580 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
581 "Value Written %8.8X%8.8X, Width %u\n",
582 ACPI_FORMAT_UINT64 (*Value),
583 ObjDesc->CommonField.AccessByteWidth));
587 return_ACPI_STATUS (Status);
591 /*******************************************************************************
593 * FUNCTION: AcpiExWriteWithUpdateRule
595 * PARAMETERS: ObjDesc - Field to be written
596 * Mask - bitmask within field datum
597 * FieldValue - Value to write
598 * FieldDatumByteOffset - Offset of datum within field
602 * DESCRIPTION: Apply the field update rule to a field write
604 ******************************************************************************/
607 AcpiExWriteWithUpdateRule (
608 ACPI_OPERAND_OBJECT *ObjDesc,
611 UINT32 FieldDatumByteOffset)
613 ACPI_STATUS Status = AE_OK;
618 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
621 /* Start with the new bits */
623 MergedValue = FieldValue;
625 /* If the mask is all ones, we don't need to worry about the update rule */
627 if (Mask != ACPI_UINT64_MAX)
629 /* Decode the update rule */
631 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
633 case AML_FIELD_UPDATE_PRESERVE:
635 * Check if update rule needs to be applied (not if mask is all
636 * ones) The left shift drops the bits we want to ignore.
638 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
639 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
642 * Read the current contents of the byte/word/dword containing
643 * the field, and merge with the new field value.
645 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
646 &CurrentValue, ACPI_READ);
647 if (ACPI_FAILURE (Status))
649 return_ACPI_STATUS (Status);
652 MergedValue |= (CurrentValue & ~Mask);
656 case AML_FIELD_UPDATE_WRITE_AS_ONES:
658 /* Set positions outside the field to all ones */
660 MergedValue |= ~Mask;
663 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
665 /* Set positions outside the field to all zeros */
672 ACPI_ERROR ((AE_INFO,
673 "Unknown UpdateRule value: 0x%X",
674 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
675 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
679 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
680 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
681 ACPI_FORMAT_UINT64 (Mask),
682 FieldDatumByteOffset,
683 ObjDesc->CommonField.AccessByteWidth,
684 ACPI_FORMAT_UINT64 (FieldValue),
685 ACPI_FORMAT_UINT64 (MergedValue)));
687 /* Write the merged value */
689 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
690 &MergedValue, ACPI_WRITE);
692 return_ACPI_STATUS (Status);
696 /*******************************************************************************
698 * FUNCTION: AcpiExExtractFromField
700 * PARAMETERS: ObjDesc - Field to be read
701 * Buffer - Where to store the field data
702 * BufferLength - Length of Buffer
706 * DESCRIPTION: Retrieve the current value of the given field
708 ******************************************************************************/
711 AcpiExExtractFromField (
712 ACPI_OPERAND_OBJECT *ObjDesc,
719 UINT32 FieldOffset = 0;
720 UINT32 BufferOffset = 0;
721 UINT32 BufferTailBits;
723 UINT32 FieldDatumCount;
724 UINT32 AccessBitWidth;
728 ACPI_FUNCTION_TRACE (ExExtractFromField);
731 /* Validate target buffer and clear it */
734 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
736 ACPI_ERROR ((AE_INFO,
737 "Field size %u (bits) is too large for buffer (%u)",
738 ObjDesc->CommonField.BitLength, BufferLength));
740 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
743 ACPI_MEMSET (Buffer, 0, BufferLength);
744 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
746 /* Handle the simple case here */
748 if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
749 (ObjDesc->CommonField.BitLength == AccessBitWidth))
751 Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
752 return_ACPI_STATUS (Status);
755 /* TBD: Move to common setup code */
757 /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
759 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
761 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
762 AccessBitWidth = sizeof (UINT64) * 8;
765 /* Compute the number of datums (access width data items) */
767 DatumCount = ACPI_ROUND_UP_TO (
768 ObjDesc->CommonField.BitLength, AccessBitWidth);
770 FieldDatumCount = ACPI_ROUND_UP_TO (
771 ObjDesc->CommonField.BitLength +
772 ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
774 /* Priming read from the field */
776 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
777 if (ACPI_FAILURE (Status))
779 return_ACPI_STATUS (Status);
781 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
783 /* Read the rest of the field */
785 for (i = 1; i < FieldDatumCount; i++)
787 /* Get next input datum from the field */
789 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
790 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
791 &RawDatum, ACPI_READ);
792 if (ACPI_FAILURE (Status))
794 return_ACPI_STATUS (Status);
798 * Merge with previous datum if necessary.
800 * Note: Before the shift, check if the shift value will be larger than
801 * the integer size. If so, there is no need to perform the operation.
802 * This avoids the differences in behavior between different compilers
803 * concerning shift values larger than the target data width.
805 if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
806 ACPI_INTEGER_BIT_SIZE)
808 MergedDatum |= RawDatum <<
809 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
817 /* Write merged datum to target buffer */
819 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
820 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
821 BufferLength - BufferOffset));
823 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
824 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
827 /* Mask off any extra bits in the last datum */
829 BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
832 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
835 /* Write the last datum to the buffer */
837 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
838 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
839 BufferLength - BufferOffset));
841 return_ACPI_STATUS (AE_OK);
845 /*******************************************************************************
847 * FUNCTION: AcpiExInsertIntoField
849 * PARAMETERS: ObjDesc - Field to be written
850 * Buffer - Data to be written
851 * BufferLength - Length of Buffer
855 * DESCRIPTION: Store the Buffer contents into the given field
857 ******************************************************************************/
860 AcpiExInsertIntoField (
861 ACPI_OPERAND_OBJECT *ObjDesc,
871 UINT32 FieldOffset = 0;
872 UINT32 BufferOffset = 0;
873 UINT32 BufferTailBits;
875 UINT32 FieldDatumCount;
876 UINT32 AccessBitWidth;
877 UINT32 RequiredLength;
881 ACPI_FUNCTION_TRACE (ExInsertIntoField);
884 /* Validate input buffer */
887 RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
888 ObjDesc->CommonField.BitLength);
890 * We must have a buffer that is at least as long as the field
891 * we are writing to. This is because individual fields are
892 * indivisible and partial writes are not supported -- as per
893 * the ACPI specification.
895 if (BufferLength < RequiredLength)
897 /* We need to create a new buffer */
899 NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
902 return_ACPI_STATUS (AE_NO_MEMORY);
906 * Copy the original data to the new buffer, starting
907 * at Byte zero. All unused (upper) bytes of the
910 ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
912 BufferLength = RequiredLength;
915 /* TBD: Move to common setup code */
917 /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
918 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
920 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
923 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
926 * Create the bitmasks used for bit insertion.
927 * Note: This if/else is used to bypass compiler differences with the
930 if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
932 WidthMask = ACPI_UINT64_MAX;
936 WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
940 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
942 /* Compute the number of datums (access width data items) */
944 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
947 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
948 ObjDesc->CommonField.StartFieldBitOffset,
951 /* Get initial Datum from the input buffer */
953 ACPI_MEMCPY (&RawDatum, Buffer,
954 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
955 BufferLength - BufferOffset));
957 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
959 /* Write the entire field */
961 for (i = 1; i < FieldDatumCount; i++)
963 /* Write merged datum to the target field */
966 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
967 MergedDatum, FieldOffset);
968 if (ACPI_FAILURE (Status))
973 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
976 * Start new output datum by merging with previous input datum
979 * Note: Before the shift, check if the shift value will be larger than
980 * the integer size. If so, there is no need to perform the operation.
981 * This avoids the differences in behavior between different compilers
982 * concerning shift values larger than the target data width.
984 if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
985 ACPI_INTEGER_BIT_SIZE)
987 MergedDatum = RawDatum >>
988 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1002 /* Get the next input datum from the buffer */
1004 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1005 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1006 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1007 BufferLength - BufferOffset));
1009 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1012 /* Mask off any extra bits in the last datum */
1014 BufferTailBits = (ObjDesc->CommonField.BitLength +
1015 ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1018 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1021 /* Write the last datum to the field */
1023 MergedDatum &= Mask;
1024 Status = AcpiExWriteWithUpdateRule (ObjDesc,
1025 Mask, MergedDatum, FieldOffset);
1028 /* Free temporary buffer if we used one */
1032 ACPI_FREE (NewBuffer);
1034 return_ACPI_STATUS (Status);