i386/identcpu.c: Add VIA Nano support
[dragonfly.git] / sys / contrib / dev / acpica-unix-20061109 / interpreter / executer / exfldio.c
blob91bd3ac5fa6bc5deb72ccad486c9edabaea73762
1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
4 * $Revision: 1.127 $
6 *****************************************************************************/
8 /******************************************************************************
10 * 1. Copyright Notice
12 * Some or all of this work - Copyright (c) 1999 - 2006, Intel Corp.
13 * All rights reserved.
15 * 2. License
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
20 * property rights.
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
39 * conditions are met:
41 * 3. Conditions
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
65 * make.
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
71 * distribution.
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
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
89 * PARTICULAR PURPOSE.
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
98 * LIMITED REMEDY.
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__
120 #include "acpi.h"
121 #include "acinterp.h"
122 #include "amlcode.h"
123 #include "acevents.h"
124 #include "acdispat.h"
127 #define _COMPONENT ACPI_EXECUTER
128 ACPI_MODULE_NAME ("exfldio")
130 /* Local prototypes */
132 static ACPI_STATUS
133 AcpiExFieldDatumIo (
134 ACPI_OPERAND_OBJECT *ObjDesc,
135 UINT32 FieldDatumByteOffset,
136 ACPI_INTEGER *Value,
137 UINT32 ReadWrite);
139 static BOOLEAN
140 AcpiExRegisterOverflow (
141 ACPI_OPERAND_OBJECT *ObjDesc,
142 ACPI_INTEGER Value);
144 static ACPI_STATUS
145 AcpiExSetupRegion (
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
156 * parent field
158 * RETURN: Status
160 * DESCRIPTION: Common processing for AcpiExExtractFromField and
161 * AcpiExInsertIntoField. Initialize the Region if necessary and
162 * validate the request.
164 ******************************************************************************/
166 static ACPI_STATUS
167 AcpiExSetupRegion (
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 (ACPI_GET_OBJECT_TYPE (RgnDesc) != ACPI_TYPE_REGION)
184 ACPI_ERROR ((AE_INFO, "Needed Region, found type %X (%s)",
185 ACPI_GET_OBJECT_TYPE (RgnDesc),
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);
204 /* Exit if Address/Length have been disallowed by the host OS */
206 if (RgnDesc->Common.Flags & AOPOBJ_INVALID)
208 return_ACPI_STATUS (AE_AML_ILLEGAL_ADDRESS);
212 * Exit now for SMBus address space, it has a non-linear address space
213 * and the request cannot be directly validated
215 if (RgnDesc->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
217 /* SMBus has a non-linear address space */
219 return_ACPI_STATUS (AE_OK);
222 #ifdef ACPI_UNDER_DEVELOPMENT
224 * If the Field access is AnyAcc, we can now compute the optimal
225 * access (because we know know the length of the parent region)
227 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
229 if (ACPI_FAILURE (Status))
231 return_ACPI_STATUS (Status);
234 #endif
237 * Validate the request. The entire request from the byte offset for a
238 * length of one field datum (access width) must fit within the region.
239 * (Region length is specified in bytes)
241 if (RgnDesc->Region.Length <
242 (ObjDesc->CommonField.BaseByteOffset +
243 FieldDatumByteOffset +
244 ObjDesc->CommonField.AccessByteWidth))
246 if (AcpiGbl_EnableInterpreterSlack)
249 * Slack mode only: We will go ahead and allow access to this
250 * field if it is within the region length rounded up to the next
251 * access width boundary.
253 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
254 ObjDesc->CommonField.AccessByteWidth) >=
255 (ObjDesc->CommonField.BaseByteOffset +
256 (ACPI_NATIVE_UINT) ObjDesc->CommonField.AccessByteWidth +
257 FieldDatumByteOffset))
259 return_ACPI_STATUS (AE_OK);
263 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
266 * This is the case where the AccessType (AccWord, etc.) is wider
267 * than the region itself. For example, a region of length one
268 * byte, and a field with Dword access specified.
270 ACPI_ERROR ((AE_INFO,
271 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)",
272 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
273 ObjDesc->CommonField.AccessByteWidth,
274 AcpiUtGetNodeName (RgnDesc->Region.Node),
275 RgnDesc->Region.Length));
279 * Offset rounded up to next multiple of field width
280 * exceeds region length, indicate an error
282 ACPI_ERROR ((AE_INFO,
283 "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)",
284 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
285 ObjDesc->CommonField.BaseByteOffset,
286 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
287 AcpiUtGetNodeName (RgnDesc->Region.Node),
288 RgnDesc->Region.Length));
290 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
293 return_ACPI_STATUS (AE_OK);
297 /*******************************************************************************
299 * FUNCTION: AcpiExAccessRegion
301 * PARAMETERS: ObjDesc - Field to be read
302 * FieldDatumByteOffset - Byte offset of this datum within the
303 * parent field
304 * Value - Where to store value (must at least
305 * the size of ACPI_INTEGER)
306 * Function - Read or Write flag plus other region-
307 * dependent flags
309 * RETURN: Status
311 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
313 ******************************************************************************/
315 ACPI_STATUS
316 AcpiExAccessRegion (
317 ACPI_OPERAND_OBJECT *ObjDesc,
318 UINT32 FieldDatumByteOffset,
319 ACPI_INTEGER *Value,
320 UINT32 Function)
322 ACPI_STATUS Status;
323 ACPI_OPERAND_OBJECT *RgnDesc;
324 ACPI_PHYSICAL_ADDRESS Address;
327 ACPI_FUNCTION_TRACE (ExAccessRegion);
331 * Ensure that the region operands are fully evaluated and verify
332 * the validity of the request
334 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
335 if (ACPI_FAILURE (Status))
337 return_ACPI_STATUS (Status);
341 * The physical address of this field datum is:
343 * 1) The base of the region, plus
344 * 2) The base offset of the field, plus
345 * 3) The current offset into the field
347 RgnDesc = ObjDesc->CommonField.RegionObj;
348 Address = RgnDesc->Region.Address +
349 ObjDesc->CommonField.BaseByteOffset +
350 FieldDatumByteOffset;
352 if ((Function & ACPI_IO_MASK) == ACPI_READ)
354 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
356 else
358 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
361 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
362 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
363 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
364 RgnDesc->Region.SpaceId,
365 ObjDesc->CommonField.AccessByteWidth,
366 ObjDesc->CommonField.BaseByteOffset,
367 FieldDatumByteOffset,
368 (void *) Address));
370 /* Invoke the appropriate AddressSpace/OpRegion handler */
372 Status = AcpiEvAddressSpaceDispatch (RgnDesc, Function,
373 Address,
374 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
376 if (ACPI_FAILURE (Status))
378 if (Status == AE_NOT_IMPLEMENTED)
380 ACPI_ERROR ((AE_INFO,
381 "Region %s(%X) not implemented",
382 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
383 RgnDesc->Region.SpaceId));
385 else if (Status == AE_NOT_EXIST)
387 ACPI_ERROR ((AE_INFO,
388 "Region %s(%X) has no handler",
389 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
390 RgnDesc->Region.SpaceId));
394 return_ACPI_STATUS (Status);
398 /*******************************************************************************
400 * FUNCTION: AcpiExRegisterOverflow
402 * PARAMETERS: ObjDesc - Register(Field) to be written
403 * Value - Value to be stored
405 * RETURN: TRUE if value overflows the field, FALSE otherwise
407 * DESCRIPTION: Check if a value is out of range of the field being written.
408 * Used to check if the values written to Index and Bank registers
409 * are out of range. Normally, the value is simply truncated
410 * to fit the field, but this case is most likely a serious
411 * coding error in the ASL.
413 ******************************************************************************/
415 static BOOLEAN
416 AcpiExRegisterOverflow (
417 ACPI_OPERAND_OBJECT *ObjDesc,
418 ACPI_INTEGER Value)
421 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
424 * The field is large enough to hold the maximum integer, so we can
425 * never overflow it.
427 return (FALSE);
430 if (Value >= ((ACPI_INTEGER) 1 << ObjDesc->CommonField.BitLength))
433 * The Value is larger than the maximum value that can fit into
434 * the register.
436 return (TRUE);
439 /* The Value will fit into the field with no truncation */
441 return (FALSE);
445 /*******************************************************************************
447 * FUNCTION: AcpiExFieldDatumIo
449 * PARAMETERS: ObjDesc - Field to be read
450 * FieldDatumByteOffset - Byte offset of this datum within the
451 * parent field
452 * Value - Where to store value (must be 64 bits)
453 * ReadWrite - Read or Write flag
455 * RETURN: Status
457 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
458 * demultiplexed here to handle the different types of fields
459 * (BufferField, RegionField, IndexField, BankField)
461 ******************************************************************************/
463 static ACPI_STATUS
464 AcpiExFieldDatumIo (
465 ACPI_OPERAND_OBJECT *ObjDesc,
466 UINT32 FieldDatumByteOffset,
467 ACPI_INTEGER *Value,
468 UINT32 ReadWrite)
470 ACPI_STATUS Status;
471 ACPI_INTEGER LocalValue;
474 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
477 if (ReadWrite == ACPI_READ)
479 if (!Value)
481 LocalValue = 0;
483 /* To support reads without saving return value */
484 Value = &LocalValue;
487 /* Clear the entire return buffer first, [Very Important!] */
489 *Value = 0;
493 * The four types of fields are:
495 * BufferField - Read/write from/to a Buffer
496 * RegionField - Read/write from/to a Operation Region.
497 * BankField - Write to a Bank Register, then read/write from/to an
498 * OperationRegion
499 * IndexField - Write to an Index Register, then read/write from/to a
500 * Data Register
502 switch (ACPI_GET_OBJECT_TYPE (ObjDesc))
504 case ACPI_TYPE_BUFFER_FIELD:
506 * If the BufferField arguments have not been previously evaluated,
507 * evaluate them now and save the results.
509 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
511 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
512 if (ACPI_FAILURE (Status))
514 return_ACPI_STATUS (Status);
518 if (ReadWrite == ACPI_READ)
521 * Copy the data from the source buffer.
522 * Length is the field width in bytes.
524 ACPI_MEMCPY (Value,
525 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
526 ObjDesc->BufferField.BaseByteOffset +
527 FieldDatumByteOffset,
528 ObjDesc->CommonField.AccessByteWidth);
530 else
533 * Copy the data to the target buffer.
534 * Length is the field width in bytes.
536 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
537 ObjDesc->BufferField.BaseByteOffset +
538 FieldDatumByteOffset,
539 Value, ObjDesc->CommonField.AccessByteWidth);
542 Status = AE_OK;
543 break;
546 case ACPI_TYPE_LOCAL_BANK_FIELD:
549 * Ensure that the BankValue is not beyond the capacity of
550 * the register
552 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
553 (ACPI_INTEGER) ObjDesc->BankField.Value))
555 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
559 * For BankFields, we must write the BankValue to the BankRegister
560 * (itself a RegionField) before we can access the data.
562 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
563 &ObjDesc->BankField.Value,
564 sizeof (ObjDesc->BankField.Value));
565 if (ACPI_FAILURE (Status))
567 return_ACPI_STATUS (Status);
571 * Now that the Bank has been selected, fall through to the
572 * RegionField case and write the datum to the Operation Region
575 /*lint -fallthrough */
578 case ACPI_TYPE_LOCAL_REGION_FIELD:
580 * For simple RegionFields, we just directly access the owning
581 * Operation Region.
583 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
584 ReadWrite);
585 break;
588 case ACPI_TYPE_LOCAL_INDEX_FIELD:
592 * Ensure that the IndexValue is not beyond the capacity of
593 * the register
595 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
596 (ACPI_INTEGER) ObjDesc->IndexField.Value))
598 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
601 /* Write the index value to the IndexRegister (itself a RegionField) */
603 FieldDatumByteOffset += ObjDesc->IndexField.Value;
605 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
606 "Write to Index Register: Value %8.8X\n",
607 FieldDatumByteOffset));
609 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
610 &FieldDatumByteOffset,
611 sizeof (FieldDatumByteOffset));
612 if (ACPI_FAILURE (Status))
614 return_ACPI_STATUS (Status);
617 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
618 "I/O to Data Register: ValuePtr %p\n", Value));
620 if (ReadWrite == ACPI_READ)
622 /* Read the datum from the DataRegister */
624 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
625 Value, sizeof (ACPI_INTEGER));
627 else
629 /* Write the datum to the DataRegister */
631 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
632 Value, sizeof (ACPI_INTEGER));
634 break;
637 default:
639 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %X",
640 ACPI_GET_OBJECT_TYPE (ObjDesc)));
641 Status = AE_AML_INTERNAL;
642 break;
645 if (ACPI_SUCCESS (Status))
647 if (ReadWrite == ACPI_READ)
649 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
650 "Value Read %8.8X%8.8X, Width %d\n",
651 ACPI_FORMAT_UINT64 (*Value),
652 ObjDesc->CommonField.AccessByteWidth));
654 else
656 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
657 "Value Written %8.8X%8.8X, Width %d\n",
658 ACPI_FORMAT_UINT64 (*Value),
659 ObjDesc->CommonField.AccessByteWidth));
663 return_ACPI_STATUS (Status);
667 /*******************************************************************************
669 * FUNCTION: AcpiExWriteWithUpdateRule
671 * PARAMETERS: ObjDesc - Field to be written
672 * Mask - bitmask within field datum
673 * FieldValue - Value to write
674 * FieldDatumByteOffset - Offset of datum within field
676 * RETURN: Status
678 * DESCRIPTION: Apply the field update rule to a field write
680 ******************************************************************************/
682 ACPI_STATUS
683 AcpiExWriteWithUpdateRule (
684 ACPI_OPERAND_OBJECT *ObjDesc,
685 ACPI_INTEGER Mask,
686 ACPI_INTEGER FieldValue,
687 UINT32 FieldDatumByteOffset)
689 ACPI_STATUS Status = AE_OK;
690 ACPI_INTEGER MergedValue;
691 ACPI_INTEGER CurrentValue;
694 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
697 /* Start with the new bits */
699 MergedValue = FieldValue;
701 /* If the mask is all ones, we don't need to worry about the update rule */
703 if (Mask != ACPI_INTEGER_MAX)
705 /* Decode the update rule */
707 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
709 case AML_FIELD_UPDATE_PRESERVE:
711 * Check if update rule needs to be applied (not if mask is all
712 * ones) The left shift drops the bits we want to ignore.
714 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
715 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
718 * Read the current contents of the byte/word/dword containing
719 * the field, and merge with the new field value.
721 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
722 &CurrentValue, ACPI_READ);
723 if (ACPI_FAILURE (Status))
725 return_ACPI_STATUS (Status);
728 MergedValue |= (CurrentValue & ~Mask);
730 break;
732 case AML_FIELD_UPDATE_WRITE_AS_ONES:
734 /* Set positions outside the field to all ones */
736 MergedValue |= ~Mask;
737 break;
739 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
741 /* Set positions outside the field to all zeros */
743 MergedValue &= Mask;
744 break;
746 default:
748 ACPI_ERROR ((AE_INFO,
749 "Unknown UpdateRule value: %X",
750 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
751 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
755 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
756 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
757 ACPI_FORMAT_UINT64 (Mask),
758 FieldDatumByteOffset,
759 ObjDesc->CommonField.AccessByteWidth,
760 ACPI_FORMAT_UINT64 (FieldValue),
761 ACPI_FORMAT_UINT64 (MergedValue)));
763 /* Write the merged value */
765 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
766 &MergedValue, ACPI_WRITE);
768 return_ACPI_STATUS (Status);
772 /*******************************************************************************
774 * FUNCTION: AcpiExExtractFromField
776 * PARAMETERS: ObjDesc - Field to be read
777 * Buffer - Where to store the field data
778 * BufferLength - Length of Buffer
780 * RETURN: Status
782 * DESCRIPTION: Retrieve the current value of the given field
784 ******************************************************************************/
786 ACPI_STATUS
787 AcpiExExtractFromField (
788 ACPI_OPERAND_OBJECT *ObjDesc,
789 void *Buffer,
790 UINT32 BufferLength)
792 ACPI_STATUS Status;
793 ACPI_INTEGER RawDatum;
794 ACPI_INTEGER MergedDatum;
795 UINT32 FieldOffset = 0;
796 UINT32 BufferOffset = 0;
797 UINT32 BufferTailBits;
798 UINT32 DatumCount;
799 UINT32 FieldDatumCount;
800 UINT32 i;
803 ACPI_FUNCTION_TRACE (ExExtractFromField);
806 /* Validate target buffer and clear it */
808 if (BufferLength <
809 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
811 ACPI_ERROR ((AE_INFO,
812 "Field size %X (bits) is too large for buffer (%X)",
813 ObjDesc->CommonField.BitLength, BufferLength));
815 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
817 ACPI_MEMSET (Buffer, 0, BufferLength);
819 /* Compute the number of datums (access width data items) */
821 DatumCount = ACPI_ROUND_UP_TO (
822 ObjDesc->CommonField.BitLength,
823 ObjDesc->CommonField.AccessBitWidth);
824 FieldDatumCount = ACPI_ROUND_UP_TO (
825 ObjDesc->CommonField.BitLength +
826 ObjDesc->CommonField.StartFieldBitOffset,
827 ObjDesc->CommonField.AccessBitWidth);
829 /* Priming read from the field */
831 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
832 if (ACPI_FAILURE (Status))
834 return_ACPI_STATUS (Status);
836 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
838 /* Read the rest of the field */
840 for (i = 1; i < FieldDatumCount; i++)
842 /* Get next input datum from the field */
844 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
845 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
846 &RawDatum, ACPI_READ);
847 if (ACPI_FAILURE (Status))
849 return_ACPI_STATUS (Status);
853 * Merge with previous datum if necessary.
855 * Note: Before the shift, check if the shift value will be larger than
856 * the integer size. If so, there is no need to perform the operation.
857 * This avoids the differences in behavior between different compilers
858 * concerning shift values larger than the target data width.
860 if ((ObjDesc->CommonField.AccessBitWidth -
861 ObjDesc->CommonField.StartFieldBitOffset) < ACPI_INTEGER_BIT_SIZE)
863 MergedDatum |= RawDatum <<
864 (ObjDesc->CommonField.AccessBitWidth -
865 ObjDesc->CommonField.StartFieldBitOffset);
868 if (i == DatumCount)
870 break;
873 /* Write merged datum to target buffer */
875 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
876 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
877 BufferLength - BufferOffset));
879 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
880 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
883 /* Mask off any extra bits in the last datum */
885 BufferTailBits = ObjDesc->CommonField.BitLength %
886 ObjDesc->CommonField.AccessBitWidth;
887 if (BufferTailBits)
889 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
892 /* Write the last datum to the buffer */
894 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
895 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
896 BufferLength - BufferOffset));
898 return_ACPI_STATUS (AE_OK);
902 /*******************************************************************************
904 * FUNCTION: AcpiExInsertIntoField
906 * PARAMETERS: ObjDesc - Field to be written
907 * Buffer - Data to be written
908 * BufferLength - Length of Buffer
910 * RETURN: Status
912 * DESCRIPTION: Store the Buffer contents into the given field
914 ******************************************************************************/
916 ACPI_STATUS
917 AcpiExInsertIntoField (
918 ACPI_OPERAND_OBJECT *ObjDesc,
919 void *Buffer,
920 UINT32 BufferLength)
922 ACPI_STATUS Status;
923 ACPI_INTEGER Mask;
924 ACPI_INTEGER WidthMask;
925 ACPI_INTEGER MergedDatum;
926 ACPI_INTEGER RawDatum = 0;
927 UINT32 FieldOffset = 0;
928 UINT32 BufferOffset = 0;
929 UINT32 BufferTailBits;
930 UINT32 DatumCount;
931 UINT32 FieldDatumCount;
932 UINT32 i;
935 ACPI_FUNCTION_TRACE (ExInsertIntoField);
938 /* Validate input buffer */
940 if (BufferLength <
941 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
943 ACPI_ERROR ((AE_INFO,
944 "Field size %X (bits) is too large for buffer (%X)",
945 ObjDesc->CommonField.BitLength, BufferLength));
947 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
951 * Create the bitmasks used for bit insertion.
952 * Note: This if/else is used to bypass compiler differences with the
953 * shift operator
955 if (ObjDesc->CommonField.AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
957 WidthMask = ACPI_INTEGER_MAX;
959 else
961 WidthMask = ACPI_MASK_BITS_ABOVE (ObjDesc->CommonField.AccessBitWidth);
964 Mask = WidthMask &
965 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
967 /* Compute the number of datums (access width data items) */
969 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
970 ObjDesc->CommonField.AccessBitWidth);
972 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
973 ObjDesc->CommonField.StartFieldBitOffset,
974 ObjDesc->CommonField.AccessBitWidth);
976 /* Get initial Datum from the input buffer */
978 ACPI_MEMCPY (&RawDatum, Buffer,
979 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
980 BufferLength - BufferOffset));
982 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
984 /* Write the entire field */
986 for (i = 1; i < FieldDatumCount; i++)
988 /* Write merged datum to the target field */
990 MergedDatum &= Mask;
991 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
992 MergedDatum, FieldOffset);
993 if (ACPI_FAILURE (Status))
995 return_ACPI_STATUS (Status);
998 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
1001 * Start new output datum by merging with previous input datum
1002 * if necessary.
1004 * Note: Before the shift, check if the shift value will be larger than
1005 * the integer size. If so, there is no need to perform the operation.
1006 * This avoids the differences in behavior between different compilers
1007 * concerning shift values larger than the target data width.
1009 if ((ObjDesc->CommonField.AccessBitWidth -
1010 ObjDesc->CommonField.StartFieldBitOffset) < ACPI_INTEGER_BIT_SIZE)
1012 MergedDatum = RawDatum >>
1013 (ObjDesc->CommonField.AccessBitWidth -
1014 ObjDesc->CommonField.StartFieldBitOffset);
1016 else
1018 MergedDatum = 0;
1021 Mask = WidthMask;
1023 if (i == DatumCount)
1025 break;
1028 /* Get the next input datum from the buffer */
1030 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1031 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1032 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1033 BufferLength - BufferOffset));
1034 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1037 /* Mask off any extra bits in the last datum */
1039 BufferTailBits = (ObjDesc->CommonField.BitLength +
1040 ObjDesc->CommonField.StartFieldBitOffset) %
1041 ObjDesc->CommonField.AccessBitWidth;
1042 if (BufferTailBits)
1044 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1047 /* Write the last datum to the field */
1049 MergedDatum &= Mask;
1050 Status = AcpiExWriteWithUpdateRule (ObjDesc,
1051 Mask, MergedDatum, FieldOffset);
1053 return_ACPI_STATUS (Status);