1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
7 /******************************************************************************
11 * Some or all of this work - Copyright (c) 1999 - 2011, Intel Corp.
12 * All rights reserved.
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
72 * 3.4. Intel retains all right, title, and interest in and to the Original
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************/
117 #define __EXFLDIO_C__
120 #include "accommon.h"
121 #include "acinterp.h"
123 #include "acevents.h"
124 #include "acdispat.h"
127 #define _COMPONENT ACPI_EXECUTER
128 ACPI_MODULE_NAME ("exfldio")
130 /* Local prototypes */
134 ACPI_OPERAND_OBJECT *ObjDesc,
135 UINT32 FieldDatumByteOffset,
140 AcpiExRegisterOverflow (
141 ACPI_OPERAND_OBJECT *ObjDesc,
146 ACPI_OPERAND_OBJECT *ObjDesc,
147 UINT32 FieldDatumByteOffset);
150 /*******************************************************************************
152 * FUNCTION: AcpiExSetupRegion
154 * PARAMETERS: ObjDesc - Field to be read or written
155 * FieldDatumByteOffset - Byte offset of this datum within the
160 * DESCRIPTION: Common processing for AcpiExExtractFromField and
161 * AcpiExInsertIntoField. Initialize the Region if necessary and
162 * validate the request.
164 ******************************************************************************/
168 ACPI_OPERAND_OBJECT *ObjDesc,
169 UINT32 FieldDatumByteOffset)
171 ACPI_STATUS Status = AE_OK;
172 ACPI_OPERAND_OBJECT *RgnDesc;
175 ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
178 RgnDesc = ObjDesc->CommonField.RegionObj;
180 /* We must have a valid region */
182 if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
184 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
185 RgnDesc->Common.Type,
186 AcpiUtGetObjectTypeName (RgnDesc)));
188 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
192 * If the Region Address and Length have not been previously evaluated,
193 * evaluate them now and save the results.
195 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
197 Status = AcpiDsGetRegionArguments (RgnDesc);
198 if (ACPI_FAILURE (Status))
200 return_ACPI_STATUS (Status);
205 * Exit now for SMBus or IPMI address space, it has a non-linear
206 * address space and the request cannot be directly validated
208 if (RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
209 RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_IPMI)
211 /* SMBus or IPMI has a non-linear address space */
213 return_ACPI_STATUS (AE_OK);
216 #ifdef ACPI_UNDER_DEVELOPMENT
218 * If the Field access is AnyAcc, we can now compute the optimal
219 * access (because we know know the length of the parent region)
221 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
223 if (ACPI_FAILURE (Status))
225 return_ACPI_STATUS (Status);
231 * Validate the request. The entire request from the byte offset for a
232 * length of one field datum (access width) must fit within the region.
233 * (Region length is specified in bytes)
235 if (RgnDesc->Region.Length <
236 (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
237 ObjDesc->CommonField.AccessByteWidth))
239 if (AcpiGbl_EnableInterpreterSlack)
242 * Slack mode only: We will go ahead and allow access to this
243 * field if it is within the region length rounded up to the next
244 * access width boundary. ACPI_SIZE cast for 64-bit compile.
246 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
247 ObjDesc->CommonField.AccessByteWidth) >=
248 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
249 ObjDesc->CommonField.AccessByteWidth +
250 FieldDatumByteOffset))
252 return_ACPI_STATUS (AE_OK);
256 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
259 * This is the case where the AccessType (AccWord, etc.) is wider
260 * than the region itself. For example, a region of length one
261 * byte, and a field with Dword access specified.
263 ACPI_ERROR ((AE_INFO,
264 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
265 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
266 ObjDesc->CommonField.AccessByteWidth,
267 AcpiUtGetNodeName (RgnDesc->Region.Node),
268 RgnDesc->Region.Length));
272 * Offset rounded up to next multiple of field width
273 * exceeds region length, indicate an error
275 ACPI_ERROR ((AE_INFO,
276 "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
277 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
278 ObjDesc->CommonField.BaseByteOffset,
279 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
280 AcpiUtGetNodeName (RgnDesc->Region.Node),
281 RgnDesc->Region.Length));
283 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
286 return_ACPI_STATUS (AE_OK);
290 /*******************************************************************************
292 * FUNCTION: AcpiExAccessRegion
294 * PARAMETERS: ObjDesc - Field to be read
295 * FieldDatumByteOffset - Byte offset of this datum within the
297 * Value - Where to store value (must at least
299 * Function - Read or Write flag plus other region-
304 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
306 ******************************************************************************/
310 ACPI_OPERAND_OBJECT *ObjDesc,
311 UINT32 FieldDatumByteOffset,
316 ACPI_OPERAND_OBJECT *RgnDesc;
320 ACPI_FUNCTION_TRACE (ExAccessRegion);
324 * Ensure that the region operands are fully evaluated and verify
325 * the validity of the request
327 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
328 if (ACPI_FAILURE (Status))
330 return_ACPI_STATUS (Status);
334 * The physical address of this field datum is:
336 * 1) The base of the region, plus
337 * 2) The base offset of the field, plus
338 * 3) The current offset into the field
340 RgnDesc = ObjDesc->CommonField.RegionObj;
342 ObjDesc->CommonField.BaseByteOffset +
343 FieldDatumByteOffset;
345 if ((Function & ACPI_IO_MASK) == ACPI_READ)
347 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
351 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
354 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
355 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
356 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
357 RgnDesc->Region.SpaceId,
358 ObjDesc->CommonField.AccessByteWidth,
359 ObjDesc->CommonField.BaseByteOffset,
360 FieldDatumByteOffset,
361 ACPI_CAST_PTR (void, (RgnDesc->Region.Address + RegionOffset))));
363 /* Invoke the appropriate AddressSpace/OpRegion handler */
365 Status = AcpiEvAddressSpaceDispatch (RgnDesc, Function, RegionOffset,
366 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
368 if (ACPI_FAILURE (Status))
370 if (Status == AE_NOT_IMPLEMENTED)
372 ACPI_ERROR ((AE_INFO,
373 "Region %s (ID=%u) not implemented",
374 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
375 RgnDesc->Region.SpaceId));
377 else if (Status == AE_NOT_EXIST)
379 ACPI_ERROR ((AE_INFO,
380 "Region %s (ID=%u) has no handler",
381 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
382 RgnDesc->Region.SpaceId));
386 return_ACPI_STATUS (Status);
390 /*******************************************************************************
392 * FUNCTION: AcpiExRegisterOverflow
394 * PARAMETERS: ObjDesc - Register(Field) to be written
395 * Value - Value to be stored
397 * RETURN: TRUE if value overflows the field, FALSE otherwise
399 * DESCRIPTION: Check if a value is out of range of the field being written.
400 * Used to check if the values written to Index and Bank registers
401 * are out of range. Normally, the value is simply truncated
402 * to fit the field, but this case is most likely a serious
403 * coding error in the ASL.
405 ******************************************************************************/
408 AcpiExRegisterOverflow (
409 ACPI_OPERAND_OBJECT *ObjDesc,
413 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
416 * The field is large enough to hold the maximum integer, so we can
422 if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
425 * The Value is larger than the maximum value that can fit into
431 /* The Value will fit into the field with no truncation */
437 /*******************************************************************************
439 * FUNCTION: AcpiExFieldDatumIo
441 * PARAMETERS: ObjDesc - Field to be read
442 * FieldDatumByteOffset - Byte offset of this datum within the
444 * Value - Where to store value (must be 64 bits)
445 * ReadWrite - Read or Write flag
449 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
450 * demultiplexed here to handle the different types of fields
451 * (BufferField, RegionField, IndexField, BankField)
453 ******************************************************************************/
457 ACPI_OPERAND_OBJECT *ObjDesc,
458 UINT32 FieldDatumByteOffset,
466 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
469 if (ReadWrite == ACPI_READ)
475 /* To support reads without saving return value */
479 /* Clear the entire return buffer first, [Very Important!] */
485 * The four types of fields are:
487 * BufferField - Read/write from/to a Buffer
488 * RegionField - Read/write from/to a Operation Region.
489 * BankField - Write to a Bank Register, then read/write from/to an
491 * IndexField - Write to an Index Register, then read/write from/to a
494 switch (ObjDesc->Common.Type)
496 case ACPI_TYPE_BUFFER_FIELD:
498 * If the BufferField arguments have not been previously evaluated,
499 * evaluate them now and save the results.
501 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
503 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
504 if (ACPI_FAILURE (Status))
506 return_ACPI_STATUS (Status);
510 if (ReadWrite == ACPI_READ)
513 * Copy the data from the source buffer.
514 * Length is the field width in bytes.
517 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
518 ObjDesc->BufferField.BaseByteOffset +
519 FieldDatumByteOffset,
520 ObjDesc->CommonField.AccessByteWidth);
525 * Copy the data to the target buffer.
526 * Length is the field width in bytes.
528 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
529 ObjDesc->BufferField.BaseByteOffset +
530 FieldDatumByteOffset,
531 Value, ObjDesc->CommonField.AccessByteWidth);
538 case ACPI_TYPE_LOCAL_BANK_FIELD:
541 * Ensure that the BankValue is not beyond the capacity of
544 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
545 (UINT64) ObjDesc->BankField.Value))
547 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
551 * For BankFields, we must write the BankValue to the BankRegister
552 * (itself a RegionField) before we can access the data.
554 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
555 &ObjDesc->BankField.Value,
556 sizeof (ObjDesc->BankField.Value));
557 if (ACPI_FAILURE (Status))
559 return_ACPI_STATUS (Status);
563 * Now that the Bank has been selected, fall through to the
564 * RegionField case and write the datum to the Operation Region
567 /*lint -fallthrough */
570 case ACPI_TYPE_LOCAL_REGION_FIELD:
572 * For simple RegionFields, we just directly access the owning
575 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
580 case ACPI_TYPE_LOCAL_INDEX_FIELD:
584 * Ensure that the IndexValue is not beyond the capacity of
587 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
588 (UINT64) ObjDesc->IndexField.Value))
590 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
593 /* Write the index value to the IndexRegister (itself a RegionField) */
595 FieldDatumByteOffset += ObjDesc->IndexField.Value;
597 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
598 "Write to Index Register: Value %8.8X\n",
599 FieldDatumByteOffset));
601 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
602 &FieldDatumByteOffset,
603 sizeof (FieldDatumByteOffset));
604 if (ACPI_FAILURE (Status))
606 return_ACPI_STATUS (Status);
609 if (ReadWrite == ACPI_READ)
611 /* Read the datum from the DataRegister */
613 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
614 "Read from Data Register\n"));
616 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
617 Value, sizeof (UINT64));
621 /* Write the datum to the DataRegister */
623 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
624 "Write to Data Register: Value %8.8X%8.8X\n",
625 ACPI_FORMAT_UINT64 (*Value)));
627 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
628 Value, sizeof (UINT64));
635 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
636 ObjDesc->Common.Type));
637 Status = AE_AML_INTERNAL;
641 if (ACPI_SUCCESS (Status))
643 if (ReadWrite == ACPI_READ)
645 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
646 "Value Read %8.8X%8.8X, Width %u\n",
647 ACPI_FORMAT_UINT64 (*Value),
648 ObjDesc->CommonField.AccessByteWidth));
652 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
653 "Value Written %8.8X%8.8X, Width %u\n",
654 ACPI_FORMAT_UINT64 (*Value),
655 ObjDesc->CommonField.AccessByteWidth));
659 return_ACPI_STATUS (Status);
663 /*******************************************************************************
665 * FUNCTION: AcpiExWriteWithUpdateRule
667 * PARAMETERS: ObjDesc - Field to be written
668 * Mask - bitmask within field datum
669 * FieldValue - Value to write
670 * FieldDatumByteOffset - Offset of datum within field
674 * DESCRIPTION: Apply the field update rule to a field write
676 ******************************************************************************/
679 AcpiExWriteWithUpdateRule (
680 ACPI_OPERAND_OBJECT *ObjDesc,
683 UINT32 FieldDatumByteOffset)
685 ACPI_STATUS Status = AE_OK;
690 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
693 /* Start with the new bits */
695 MergedValue = FieldValue;
697 /* If the mask is all ones, we don't need to worry about the update rule */
699 if (Mask != ACPI_UINT64_MAX)
701 /* Decode the update rule */
703 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
705 case AML_FIELD_UPDATE_PRESERVE:
707 * Check if update rule needs to be applied (not if mask is all
708 * ones) The left shift drops the bits we want to ignore.
710 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
711 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
714 * Read the current contents of the byte/word/dword containing
715 * the field, and merge with the new field value.
717 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
718 &CurrentValue, ACPI_READ);
719 if (ACPI_FAILURE (Status))
721 return_ACPI_STATUS (Status);
724 MergedValue |= (CurrentValue & ~Mask);
728 case AML_FIELD_UPDATE_WRITE_AS_ONES:
730 /* Set positions outside the field to all ones */
732 MergedValue |= ~Mask;
735 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
737 /* Set positions outside the field to all zeros */
744 ACPI_ERROR ((AE_INFO,
745 "Unknown UpdateRule value: 0x%X",
746 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
747 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
751 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
752 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
753 ACPI_FORMAT_UINT64 (Mask),
754 FieldDatumByteOffset,
755 ObjDesc->CommonField.AccessByteWidth,
756 ACPI_FORMAT_UINT64 (FieldValue),
757 ACPI_FORMAT_UINT64 (MergedValue)));
759 /* Write the merged value */
761 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
762 &MergedValue, ACPI_WRITE);
764 return_ACPI_STATUS (Status);
768 /*******************************************************************************
770 * FUNCTION: AcpiExExtractFromField
772 * PARAMETERS: ObjDesc - Field to be read
773 * Buffer - Where to store the field data
774 * BufferLength - Length of Buffer
778 * DESCRIPTION: Retrieve the current value of the given field
780 ******************************************************************************/
783 AcpiExExtractFromField (
784 ACPI_OPERAND_OBJECT *ObjDesc,
791 UINT32 FieldOffset = 0;
792 UINT32 BufferOffset = 0;
793 UINT32 BufferTailBits;
795 UINT32 FieldDatumCount;
796 UINT32 AccessBitWidth;
800 ACPI_FUNCTION_TRACE (ExExtractFromField);
803 /* Validate target buffer and clear it */
806 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
808 ACPI_ERROR ((AE_INFO,
809 "Field size %u (bits) is too large for buffer (%u)",
810 ObjDesc->CommonField.BitLength, BufferLength));
812 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
815 ACPI_MEMSET (Buffer, 0, BufferLength);
816 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
818 /* Handle the simple case here */
820 if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
821 (ObjDesc->CommonField.BitLength == AccessBitWidth))
823 Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
824 return_ACPI_STATUS (Status);
827 /* TBD: Move to common setup code */
829 /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
831 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
833 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
834 AccessBitWidth = sizeof (UINT64) * 8;
837 /* Compute the number of datums (access width data items) */
839 DatumCount = ACPI_ROUND_UP_TO (
840 ObjDesc->CommonField.BitLength, AccessBitWidth);
842 FieldDatumCount = ACPI_ROUND_UP_TO (
843 ObjDesc->CommonField.BitLength +
844 ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
846 /* Priming read from the field */
848 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
849 if (ACPI_FAILURE (Status))
851 return_ACPI_STATUS (Status);
853 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
855 /* Read the rest of the field */
857 for (i = 1; i < FieldDatumCount; i++)
859 /* Get next input datum from the field */
861 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
862 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
863 &RawDatum, ACPI_READ);
864 if (ACPI_FAILURE (Status))
866 return_ACPI_STATUS (Status);
870 * Merge with previous datum if necessary.
872 * Note: Before the shift, check if the shift value will be larger than
873 * the integer size. If so, there is no need to perform the operation.
874 * This avoids the differences in behavior between different compilers
875 * concerning shift values larger than the target data width.
877 if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
878 ACPI_INTEGER_BIT_SIZE)
880 MergedDatum |= RawDatum <<
881 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
889 /* Write merged datum to target buffer */
891 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
892 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
893 BufferLength - BufferOffset));
895 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
896 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
899 /* Mask off any extra bits in the last datum */
901 BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
904 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
907 /* Write the last datum to the buffer */
909 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
910 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
911 BufferLength - BufferOffset));
913 return_ACPI_STATUS (AE_OK);
917 /*******************************************************************************
919 * FUNCTION: AcpiExInsertIntoField
921 * PARAMETERS: ObjDesc - Field to be written
922 * Buffer - Data to be written
923 * BufferLength - Length of Buffer
927 * DESCRIPTION: Store the Buffer contents into the given field
929 ******************************************************************************/
932 AcpiExInsertIntoField (
933 ACPI_OPERAND_OBJECT *ObjDesc,
943 UINT32 FieldOffset = 0;
944 UINT32 BufferOffset = 0;
945 UINT32 BufferTailBits;
947 UINT32 FieldDatumCount;
948 UINT32 AccessBitWidth;
949 UINT32 RequiredLength;
953 ACPI_FUNCTION_TRACE (ExInsertIntoField);
956 /* Validate input buffer */
959 RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
960 ObjDesc->CommonField.BitLength);
962 * We must have a buffer that is at least as long as the field
963 * we are writing to. This is because individual fields are
964 * indivisible and partial writes are not supported -- as per
965 * the ACPI specification.
967 if (BufferLength < RequiredLength)
969 /* We need to create a new buffer */
971 NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
974 return_ACPI_STATUS (AE_NO_MEMORY);
978 * Copy the original data to the new buffer, starting
979 * at Byte zero. All unused (upper) bytes of the
982 ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
984 BufferLength = RequiredLength;
987 /* TBD: Move to common setup code */
989 /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
990 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
992 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
995 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
998 * Create the bitmasks used for bit insertion.
999 * Note: This if/else is used to bypass compiler differences with the
1002 if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
1004 WidthMask = ACPI_UINT64_MAX;
1008 WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
1012 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
1014 /* Compute the number of datums (access width data items) */
1016 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
1019 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
1020 ObjDesc->CommonField.StartFieldBitOffset,
1023 /* Get initial Datum from the input buffer */
1025 ACPI_MEMCPY (&RawDatum, Buffer,
1026 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1027 BufferLength - BufferOffset));
1029 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1031 /* Write the entire field */
1033 for (i = 1; i < FieldDatumCount; i++)
1035 /* Write merged datum to the target field */
1037 MergedDatum &= Mask;
1038 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
1039 MergedDatum, FieldOffset);
1040 if (ACPI_FAILURE (Status))
1045 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
1048 * Start new output datum by merging with previous input datum
1051 * Note: Before the shift, check if the shift value will be larger than
1052 * the integer size. If so, there is no need to perform the operation.
1053 * This avoids the differences in behavior between different compilers
1054 * concerning shift values larger than the target data width.
1056 if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1057 ACPI_INTEGER_BIT_SIZE)
1059 MergedDatum = RawDatum >>
1060 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1069 if (i == DatumCount)
1074 /* Get the next input datum from the buffer */
1076 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1077 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1078 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1079 BufferLength - BufferOffset));
1081 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1084 /* Mask off any extra bits in the last datum */
1086 BufferTailBits = (ObjDesc->CommonField.BitLength +
1087 ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1090 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1093 /* Write the last datum to the field */
1095 MergedDatum &= Mask;
1096 Status = AcpiExWriteWithUpdateRule (ObjDesc,
1097 Mask, MergedDatum, FieldOffset);
1100 /* Free temporary buffer if we used one */
1104 ACPI_FREE (NewBuffer);
1106 return_ACPI_STATUS (Status);