1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999, 2000, 2001, Intel Corp.
13 * All rights reserved.
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights. You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code. No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
38 * The above copyright and patent license is granted only if the following
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision. In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change. Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee. Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution. In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
73 * 3.4. Intel retains all right, title, and interest in and to the Original
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
81 * 4. Disclaimer and Export Compliance
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government. In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
115 *****************************************************************************/
118 #define __EXFLDIO_C__
121 #include "acinterp.h"
123 #include "acnamesp.h"
125 #include "acevents.h"
126 #include "acdispat.h"
129 #define _COMPONENT ACPI_EXECUTER
130 MODULE_NAME ("exfldio")
133 /*******************************************************************************
135 * FUNCTION: AcpiExSetupField
137 * PARAMETERS: *ObjDesc - Field to be read or written
138 * FieldDatumByteOffset - Current offset into the field
142 * DESCRIPTION: Common processing for AcpiExExtractFromField and
143 * AcpiExInsertIntoField
145 ******************************************************************************/
149 ACPI_OPERAND_OBJECT *ObjDesc,
150 UINT32 FieldDatumByteOffset)
152 ACPI_STATUS Status = AE_OK;
153 ACPI_OPERAND_OBJECT *RgnDesc;
156 FUNCTION_TRACE_U32 ("ExSetupField", FieldDatumByteOffset);
158 RgnDesc = ObjDesc->CommonField.RegionObj;
160 if (ACPI_TYPE_REGION != RgnDesc->Common.Type)
162 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %x %s\n",
163 RgnDesc->Common.Type, AcpiUtGetTypeName (RgnDesc->Common.Type)));
164 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
169 * If the Region Address and Length have not been previously evaluated,
170 * evaluate them now and save the results.
172 if (!(RgnDesc->Region.Flags & AOPOBJ_DATA_VALID))
175 Status = AcpiDsGetRegionArguments (RgnDesc);
176 if (ACPI_FAILURE (Status))
178 return_ACPI_STATUS (Status);
184 * Validate the request. The entire request from the byte offset for a
185 * length of one field datum (access width) must fit within the region.
186 * (Region length is specified in bytes)
188 if (RgnDesc->Region.Length < (ObjDesc->CommonField.BaseByteOffset +
189 FieldDatumByteOffset +
190 ObjDesc->CommonField.AccessByteWidth))
192 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
195 * This is the case where the AccessType (AccWord, etc.) is wider
196 * than the region itself. For example, a region of length one
197 * byte, and a field with Dword access specified.
199 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
200 "Field access width (%d bytes) too large for region size (%X)\n",
201 ObjDesc->CommonField.AccessByteWidth, RgnDesc->Region.Length));
205 * Offset rounded up to next multiple of field width
206 * exceeds region length, indicate an error
208 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
209 "Field base+offset+width %X+%X+%X exceeds region size (%X bytes) field=%p region=%p\n",
210 ObjDesc->CommonField.BaseByteOffset, FieldDatumByteOffset,
211 ObjDesc->CommonField.AccessByteWidth,
212 RgnDesc->Region.Length, ObjDesc, RgnDesc));
214 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
217 return_ACPI_STATUS (AE_OK);
221 /*******************************************************************************
223 * FUNCTION: AcpiExReadFieldDatum
225 * PARAMETERS: *ObjDesc - Field to be read
226 * *Value - Where to store value (must be 32 bits)
230 * DESCRIPTION: Retrieve the value of the given field
232 ******************************************************************************/
235 AcpiExReadFieldDatum (
236 ACPI_OPERAND_OBJECT *ObjDesc,
237 UINT32 FieldDatumByteOffset,
241 ACPI_OPERAND_OBJECT *RgnDesc;
242 ACPI_PHYSICAL_ADDRESS Address;
246 FUNCTION_TRACE_U32 ("ExReadFieldDatum", FieldDatumByteOffset);
252 Value = &LocalValue; /* support reads without saving value */
255 /* Clear the entire return buffer first, [Very Important!] */
261 * BufferFields - Read from a Buffer
262 * Other Fields - Read from a Operation Region.
264 switch (ObjDesc->Common.Type)
266 case ACPI_TYPE_BUFFER_FIELD:
269 * For BufferFields, we only need to copy the data from the
270 * source buffer. Length is the field width in bytes.
272 MEMCPY (Value, (ObjDesc->BufferField.BufferObj)->Buffer.Pointer
273 + ObjDesc->BufferField.BaseByteOffset + FieldDatumByteOffset,
274 ObjDesc->CommonField.AccessByteWidth);
279 case INTERNAL_TYPE_REGION_FIELD:
280 case INTERNAL_TYPE_BANK_FIELD:
283 * For other fields, we need to go through an Operation Region
284 * (Only types that will get here are RegionFields and BankFields)
286 Status = AcpiExSetupField (ObjDesc, FieldDatumByteOffset);
287 if (ACPI_FAILURE (Status))
289 return_ACPI_STATUS (Status);
294 * The physical address of this field datum is:
296 * 1) The base of the region, plus
297 * 2) The base offset of the field, plus
298 * 3) The current offset into the field
300 RgnDesc = ObjDesc->CommonField.RegionObj;
301 Address = RgnDesc->Region.Address + ObjDesc->CommonField.BaseByteOffset +
302 FieldDatumByteOffset;
304 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Region %s(%X) width %X base:off %X:%X at %8.8lX%8.8lX\n",
305 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
306 RgnDesc->Region.SpaceId, ObjDesc->CommonField.AccessBitWidth,
307 ObjDesc->CommonField.BaseByteOffset, FieldDatumByteOffset,
308 HIDWORD(Address), LODWORD(Address)));
311 /* Invoke the appropriate AddressSpace/OpRegion handler */
313 Status = AcpiEvAddressSpaceDispatch (RgnDesc, ACPI_READ_ADR_SPACE,
314 Address, ObjDesc->CommonField.AccessBitWidth, Value);
315 if (Status == AE_NOT_IMPLEMENTED)
317 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Region %s(%X) not implemented\n",
318 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
319 RgnDesc->Region.SpaceId));
322 else if (Status == AE_NOT_EXIST)
324 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Region %s(%X) has no handler\n",
325 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
326 RgnDesc->Region.SpaceId));
333 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "%p, wrong source type - %s\n",
334 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type)));
335 Status = AE_AML_INTERNAL;
340 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Returned value=%08lX \n", *Value));
342 return_ACPI_STATUS (Status);
346 /*******************************************************************************
348 * FUNCTION: AcpiExGetBufferDatum
350 * PARAMETERS: MergedDatum - Value to store
351 * Buffer - Receiving buffer
352 * ByteGranularity - 1/2/4 Granularity of the field
354 * Offset - Datum offset into the buffer
358 * DESCRIPTION: Store the merged datum to the buffer according to the
361 ******************************************************************************/
364 AcpiExGetBufferDatum(
367 UINT32 ByteGranularity,
371 switch (ByteGranularity)
373 case ACPI_FIELD_BYTE_GRANULARITY:
374 *Datum = ((UINT8 *) Buffer) [Offset];
377 case ACPI_FIELD_WORD_GRANULARITY:
378 MOVE_UNALIGNED16_TO_32 (Datum, &(((UINT16 *) Buffer) [Offset]));
381 case ACPI_FIELD_DWORD_GRANULARITY:
382 MOVE_UNALIGNED32_TO_32 (Datum, &(((UINT32 *) Buffer) [Offset]));
388 /*******************************************************************************
390 * FUNCTION: AcpiExSetBufferDatum
392 * PARAMETERS: MergedDatum - Value to store
393 * Buffer - Receiving buffer
394 * ByteGranularity - 1/2/4 Granularity of the field
396 * Offset - Datum offset into the buffer
400 * DESCRIPTION: Store the merged datum to the buffer according to the
403 ******************************************************************************/
406 AcpiExSetBufferDatum (
409 UINT32 ByteGranularity,
413 switch (ByteGranularity)
415 case ACPI_FIELD_BYTE_GRANULARITY:
416 ((UINT8 *) Buffer) [Offset] = (UINT8) MergedDatum;
419 case ACPI_FIELD_WORD_GRANULARITY:
420 MOVE_UNALIGNED16_TO_16 (&(((UINT16 *) Buffer)[Offset]), &MergedDatum);
423 case ACPI_FIELD_DWORD_GRANULARITY:
424 MOVE_UNALIGNED32_TO_32 (&(((UINT32 *) Buffer)[Offset]), &MergedDatum);
430 /*******************************************************************************
432 * FUNCTION: AcpiExExtractFromField
434 * PARAMETERS: *ObjDesc - Field to be read
435 * *Value - Where to store value
439 * DESCRIPTION: Retrieve the value of the given field
441 ******************************************************************************/
444 AcpiExExtractFromField (
445 ACPI_OPERAND_OBJECT *ObjDesc,
450 UINT32 FieldDatumByteOffset;
452 UINT32 PreviousRawDatum;
453 UINT32 ThisRawDatum = 0;
454 UINT32 MergedDatum = 0;
455 UINT32 ByteFieldLength;
459 FUNCTION_TRACE ("ExExtractFromField");
463 * The field must fit within the caller's buffer
465 ByteFieldLength = ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
466 if (ByteFieldLength > BufferLength)
468 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Field size %X (bytes) too large for buffer (%X)\n",
469 ByteFieldLength, BufferLength));
471 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
474 /* Convert field byte count to datum count, round up if necessary */
476 DatumCount = ROUND_UP_TO (ByteFieldLength, ObjDesc->CommonField.AccessByteWidth);
478 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
479 "ByteLen=%x, DatumLen=%x, BitGran=%x, ByteGran=%x\n",
480 ByteFieldLength, DatumCount, ObjDesc->CommonField.AccessBitWidth,
481 ObjDesc->CommonField.AccessByteWidth));
485 * Clear the caller's buffer (the whole buffer length as given)
486 * This is very important, especially in the cases where a byte is read,
487 * but the buffer is really a UINT32 (4 bytes).
489 MEMSET (Buffer, 0, BufferLength);
491 /* Read the first raw datum to prime the loop */
493 FieldDatumByteOffset = 0;
496 Status = AcpiExReadFieldDatum (ObjDesc, FieldDatumByteOffset, &PreviousRawDatum);
497 if (ACPI_FAILURE (Status))
499 return_ACPI_STATUS (Status);
503 /* We might actually be done if the request fits in one datum */
505 if ((DatumCount == 1) &&
506 (ObjDesc->CommonField.AccessFlags & AFIELD_SINGLE_DATUM))
508 /* 1) Shift the valid data bits down to start at bit 0 */
510 MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
512 /* 2) Mask off any upper unused bits (bits not part of the field) */
514 if (ObjDesc->CommonField.EndBufferValidBits)
516 MergedDatum &= MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
519 /* Store the datum to the caller buffer */
521 AcpiExSetBufferDatum (MergedDatum, Buffer, ObjDesc->CommonField.AccessByteWidth,
524 return_ACPI_STATUS (AE_OK);
528 /* We need to get more raw data to complete one or more field data */
530 while (DatumOffset < DatumCount)
532 FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
535 * If the field is aligned on a byte boundary, we don't want
536 * to perform a final read, since this would potentially read
537 * past the end of the region.
539 * TBD: [Investigate] It may make more sense to just split the aligned
540 * and non-aligned cases since the aligned case is so very simple,
542 if ((ObjDesc->CommonField.StartFieldBitOffset != 0) ||
543 ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
544 (DatumOffset < (DatumCount -1))))
547 * Get the next raw datum, it contains some or all bits
548 * of the current field datum
550 Status = AcpiExReadFieldDatum (ObjDesc, FieldDatumByteOffset, &ThisRawDatum);
551 if (ACPI_FAILURE (Status))
553 return_ACPI_STATUS (Status);
558 * Create the (possibly) merged datum to be stored to the caller buffer
560 if (ObjDesc->CommonField.StartFieldBitOffset == 0)
562 /* Field is not skewed and we can just copy the datum */
564 MergedDatum = PreviousRawDatum;
570 * Put together the appropriate bits of the two raw data to make a
571 * single complete field datum
573 * 1) Normalize the first datum down to bit 0
575 MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
577 /* 2) Insert the second datum "above" the first datum */
579 MergedDatum |= (ThisRawDatum << ObjDesc->CommonField.DatumValidBits);
581 if ((DatumOffset >= (DatumCount -1)))
584 * This is the last iteration of the loop. We need to clear
585 * any unused bits (bits that are not part of this field) that
586 * came from the last raw datum before we store the final
587 * merged datum into the caller buffer.
589 if (ObjDesc->CommonField.EndBufferValidBits)
592 MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
599 * Store the merged field datum in the caller's buffer, according to
600 * the granularity of the field (size of each datum).
602 AcpiExSetBufferDatum (MergedDatum, Buffer, ObjDesc->CommonField.AccessByteWidth,
606 * Save the raw datum that was just acquired since it may contain bits
607 * of the *next* field datum. Update offsets
609 PreviousRawDatum = ThisRawDatum;
614 return_ACPI_STATUS (AE_OK);
618 /*******************************************************************************
620 * FUNCTION: AcpiExWriteFieldDatum
622 * PARAMETERS: *ObjDesc - Field to be set
623 * Value - Value to store
627 * DESCRIPTION: Store the value into the given field
629 ******************************************************************************/
632 AcpiExWriteFieldDatum (
633 ACPI_OPERAND_OBJECT *ObjDesc,
634 UINT32 FieldDatumByteOffset,
637 ACPI_STATUS Status = AE_OK;
638 ACPI_OPERAND_OBJECT *RgnDesc = NULL;
639 ACPI_PHYSICAL_ADDRESS Address;
642 FUNCTION_TRACE_U32 ("ExWriteFieldDatum", FieldDatumByteOffset);
647 * BufferFields - Read from a Buffer
648 * Other Fields - Read from a Operation Region.
650 switch (ObjDesc->Common.Type)
652 case ACPI_TYPE_BUFFER_FIELD:
655 * For BufferFields, we only need to copy the data to the
656 * target buffer. Length is the field width in bytes.
658 MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer
659 + ObjDesc->BufferField.BaseByteOffset + FieldDatumByteOffset,
660 &Value, ObjDesc->CommonField.AccessByteWidth);
665 case INTERNAL_TYPE_REGION_FIELD:
666 case INTERNAL_TYPE_BANK_FIELD:
669 * For other fields, we need to go through an Operation Region
670 * (Only types that will get here are RegionFields and BankFields)
672 Status = AcpiExSetupField (ObjDesc, FieldDatumByteOffset);
673 if (ACPI_FAILURE (Status))
675 return_ACPI_STATUS (Status);
679 * The physical address of this field datum is:
681 * 1) The base of the region, plus
682 * 2) The base offset of the field, plus
683 * 3) The current offset into the field
685 RgnDesc = ObjDesc->CommonField.RegionObj;
686 Address = RgnDesc->Region.Address +
687 ObjDesc->CommonField.BaseByteOffset +
688 FieldDatumByteOffset;
690 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
691 "Store %X in Region %s(%X) at %8.8lX%8.8lX width %X\n",
692 Value, AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
693 RgnDesc->Region.SpaceId, HIDWORD(Address), LODWORD(Address),
694 ObjDesc->CommonField.AccessBitWidth));
696 /* Invoke the appropriate AddressSpace/OpRegion handler */
698 Status = AcpiEvAddressSpaceDispatch (RgnDesc, ACPI_WRITE_ADR_SPACE,
699 Address, ObjDesc->CommonField.AccessBitWidth, &Value);
701 if (Status == AE_NOT_IMPLEMENTED)
703 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
704 "**** Region type %s(%X) not implemented\n",
705 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
706 RgnDesc->Region.SpaceId));
709 else if (Status == AE_NOT_EXIST)
711 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
712 "**** Region type %s(%X) does not have a handler\n",
713 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
714 RgnDesc->Region.SpaceId));
722 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "%p, wrong source type - %s\n",
723 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type)));
724 Status = AE_AML_INTERNAL;
729 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value written=%08lX \n", Value));
730 return_ACPI_STATUS (Status);
734 /*******************************************************************************
736 * FUNCTION: AcpiExWriteFieldDatumWithUpdateRule
738 * PARAMETERS: *ObjDesc - Field to be set
739 * Value - Value to store
743 * DESCRIPTION: Apply the field update rule to a field write
745 ******************************************************************************/
748 AcpiExWriteFieldDatumWithUpdateRule (
749 ACPI_OPERAND_OBJECT *ObjDesc,
752 UINT32 FieldDatumByteOffset)
754 ACPI_STATUS Status = AE_OK;
759 FUNCTION_TRACE ("ExWriteFieldDatumWithUpdateRule");
762 /* Start with the new bits */
764 MergedValue = FieldValue;
767 /* If the mask is all ones, we don't need to worry about the update rule */
769 if (Mask != ACPI_UINT32_MAX)
771 /* Decode the update rule */
773 switch (ObjDesc->CommonField.UpdateRule)
776 case UPDATE_PRESERVE:
779 * Check if update rule needs to be applied (not if mask is all
780 * ones) The left shift drops the bits we want to ignore.
782 if ((~Mask << (sizeof (Mask) * 8 -
783 ObjDesc->CommonField.AccessBitWidth)) != 0)
786 * Read the current contents of the byte/word/dword containing
787 * the field, and merge with the new field value.
789 Status = AcpiExReadFieldDatum (ObjDesc, FieldDatumByteOffset,
791 MergedValue |= (CurrentValue & ~Mask);
796 case UPDATE_WRITE_AS_ONES:
798 /* Set positions outside the field to all ones */
800 MergedValue |= ~Mask;
804 case UPDATE_WRITE_AS_ZEROS:
806 /* Set positions outside the field to all zeros */
813 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
814 "WriteWithUpdateRule: Unknown UpdateRule setting: %x\n",
815 ObjDesc->CommonField.UpdateRule));
816 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
822 /* Write the merged value */
824 Status = AcpiExWriteFieldDatum (ObjDesc, FieldDatumByteOffset,
827 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Mask %X DatumOffset %X Value %X, MergedValue %X\n",
828 Mask, FieldDatumByteOffset, FieldValue, MergedValue));
830 return_ACPI_STATUS (Status);
834 /*******************************************************************************
836 * FUNCTION: AcpiExInsertIntoField
838 * PARAMETERS: *ObjDesc - Field to be set
839 * Buffer - Value to store
843 * DESCRIPTION: Store the value into the given field
845 ******************************************************************************/
848 AcpiExInsertIntoField (
849 ACPI_OPERAND_OBJECT *ObjDesc,
854 UINT32 FieldDatumByteOffset;
858 UINT32 PreviousRawDatum;
860 UINT32 ByteFieldLength;
864 FUNCTION_TRACE ("ExInsertIntoField");
868 * Incoming buffer must be at least as long as the field, we do not
869 * allow "partial" field writes. We do not care if the buffer is
870 * larger than the field, this typically happens when an integer is
871 * written to a field that is actually smaller than an integer.
873 ByteFieldLength = ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
874 if (BufferLength < ByteFieldLength)
876 ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Buffer length %X too small for field %X\n",
877 BufferLength, ByteFieldLength));
879 /* TBD: Need a better error code */
881 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
884 /* Convert byte count to datum count, round up if necessary */
886 DatumCount = ROUND_UP_TO (ByteFieldLength, ObjDesc->CommonField.AccessByteWidth);
888 ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
889 "ByteLen=%x, DatumLen=%x, BitGran=%x, ByteGran=%x\n",
890 ByteFieldLength, DatumCount, ObjDesc->CommonField.AccessBitWidth,
891 ObjDesc->CommonField.AccessByteWidth));
895 * Break the request into up to three parts (similar to an I/O request):
896 * 1) non-aligned part at start
897 * 2) aligned part in middle
898 * 3) non-aligned part at the end
900 FieldDatumByteOffset = 0;
903 /* Get a single datum from the caller's buffer */
905 AcpiExGetBufferDatum (&PreviousRawDatum, Buffer,
906 ObjDesc->CommonField.AccessByteWidth, DatumOffset);
910 * Write a partial field datum if field does not begin on a datum boundary
911 * Note: The code in this section also handles the aligned case
913 * Construct Mask with 1 bits where the field is, 0 bits elsewhere
914 * (Only the bottom 5 bits of BitLength are valid for a shift operation)
916 * Mask off bits that are "below" the field (if any)
918 Mask = MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
920 /* If the field fits in one datum, may need to mask upper bits */
922 if ((ObjDesc->CommonField.AccessFlags & AFIELD_SINGLE_DATUM) &&
923 ObjDesc->CommonField.EndFieldValidBits)
925 /* There are bits above the field, mask them off also */
927 Mask &= MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
930 /* Shift and mask the value into the field position */
932 MergedDatum = (PreviousRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
935 /* Apply the update rule (if necessary) and write the datum to the field */
937 Status = AcpiExWriteFieldDatumWithUpdateRule (ObjDesc, Mask, MergedDatum,
938 FieldDatumByteOffset);
939 if (ACPI_FAILURE (Status))
941 return_ACPI_STATUS (Status);
944 /* If the entire field fits within one datum, we are done. */
946 if ((DatumCount == 1) &&
947 (ObjDesc->CommonField.AccessFlags & AFIELD_SINGLE_DATUM))
949 return_ACPI_STATUS (AE_OK);
954 * Write the aligned data.
956 * We don't need to worry about the update rule for these data, because
957 * all of the bits in each datum are part of the field.
959 * The last datum must be special cased because it might contain bits
960 * that are not part of the field -- therefore the "update rule" must be
961 * applied in Part3 below.
963 while (DatumOffset < DatumCount)
966 FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
969 * Get the next raw buffer datum. It may contain bits of the previous
972 AcpiExGetBufferDatum (&ThisRawDatum, Buffer,
973 ObjDesc->CommonField.AccessByteWidth, DatumOffset);
975 /* Create the field datum based on the field alignment */
977 if (ObjDesc->CommonField.StartFieldBitOffset != 0)
980 * Put together appropriate bits of the two raw buffer data to make
981 * a single complete field datum
984 (PreviousRawDatum >> ObjDesc->CommonField.DatumValidBits) |
985 (ThisRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
990 /* Field began aligned on datum boundary */
992 MergedDatum = ThisRawDatum;
997 * Special handling for the last datum if the field does NOT end on
998 * a datum boundary. Update Rule must be applied to the bits outside
1001 if ((DatumOffset == DatumCount) &&
1002 ObjDesc->CommonField.EndFieldValidBits)
1006 * This is the last datum and the field does not end on a datum boundary.
1007 * Build the partial datum and write with the update rule.
1010 /* Mask off the unused bits above (after) the end-of-field */
1012 Mask = MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
1013 MergedDatum &= Mask;
1015 /* Write the last datum with the update rule */
1017 Status = AcpiExWriteFieldDatumWithUpdateRule (ObjDesc, Mask,
1018 MergedDatum, FieldDatumByteOffset);
1019 if (ACPI_FAILURE (Status))
1021 return_ACPI_STATUS (Status);
1027 /* Normal case -- write the completed datum */
1029 Status = AcpiExWriteFieldDatum (ObjDesc,
1030 FieldDatumByteOffset, MergedDatum);
1031 if (ACPI_FAILURE (Status))
1033 return_ACPI_STATUS (Status);
1038 * Save the most recent datum since it may contain bits of the *next*
1039 * field datum. Update current byte offset.
1041 PreviousRawDatum = ThisRawDatum;
1045 return_ACPI_STATUS (Status);