Update to ACPICA 20121018.
[haiku.git] / src / add-ons / kernel / bus_managers / acpi / acpica / components / executer / exfldio.c
blobbe1ea3ad7cc1d2c6eb04c295d8715becc8888dea
1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2012, Intel Corp.
12 * All rights reserved.
14 * 2. License
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
19 * property rights.
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
38 * conditions are met:
40 * 3. Conditions
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
64 * make.
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
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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
88 * PARTICULAR PURPOSE.
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
97 * LIMITED REMEDY.
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__
119 #include "acpi.h"
120 #include "accommon.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 UINT64 *Value,
137 UINT32 ReadWrite);
139 static BOOLEAN
140 AcpiExRegisterOverflow (
141 ACPI_OPERAND_OBJECT *ObjDesc,
142 UINT64 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;
173 UINT8 SpaceId;
176 ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
179 RgnDesc = ObjDesc->CommonField.RegionObj;
181 /* We must have a valid region */
183 if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
185 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
186 RgnDesc->Common.Type,
187 AcpiUtGetObjectTypeName (RgnDesc)));
189 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
192 SpaceId = RgnDesc->Region.SpaceId;
194 /* Validate the Space ID */
196 if (!AcpiIsValidSpaceId (SpaceId))
198 ACPI_ERROR ((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
199 return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
203 * If the Region Address and Length have not been previously evaluated,
204 * evaluate them now and save the results.
206 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
208 Status = AcpiDsGetRegionArguments (RgnDesc);
209 if (ACPI_FAILURE (Status))
211 return_ACPI_STATUS (Status);
216 * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
217 * address space and the request cannot be directly validated
219 if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
220 SpaceId == ACPI_ADR_SPACE_GSBUS ||
221 SpaceId == ACPI_ADR_SPACE_IPMI)
223 /* SMBus or IPMI has a non-linear address space */
225 return_ACPI_STATUS (AE_OK);
228 #ifdef ACPI_UNDER_DEVELOPMENT
230 * If the Field access is AnyAcc, we can now compute the optimal
231 * access (because we know know the length of the parent region)
233 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
235 if (ACPI_FAILURE (Status))
237 return_ACPI_STATUS (Status);
240 #endif
243 * Validate the request. The entire request from the byte offset for a
244 * length of one field datum (access width) must fit within the region.
245 * (Region length is specified in bytes)
247 if (RgnDesc->Region.Length <
248 (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
249 ObjDesc->CommonField.AccessByteWidth))
251 if (AcpiGbl_EnableInterpreterSlack)
254 * Slack mode only: We will go ahead and allow access to this
255 * field if it is within the region length rounded up to the next
256 * access width boundary. ACPI_SIZE cast for 64-bit compile.
258 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
259 ObjDesc->CommonField.AccessByteWidth) >=
260 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
261 ObjDesc->CommonField.AccessByteWidth +
262 FieldDatumByteOffset))
264 return_ACPI_STATUS (AE_OK);
268 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
271 * This is the case where the AccessType (AccWord, etc.) is wider
272 * than the region itself. For example, a region of length one
273 * byte, and a field with Dword access specified.
275 ACPI_ERROR ((AE_INFO,
276 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
277 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
278 ObjDesc->CommonField.AccessByteWidth,
279 AcpiUtGetNodeName (RgnDesc->Region.Node),
280 RgnDesc->Region.Length));
284 * Offset rounded up to next multiple of field width
285 * exceeds region length, indicate an error
287 ACPI_ERROR ((AE_INFO,
288 "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
289 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
290 ObjDesc->CommonField.BaseByteOffset,
291 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
292 AcpiUtGetNodeName (RgnDesc->Region.Node),
293 RgnDesc->Region.Length));
295 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
298 return_ACPI_STATUS (AE_OK);
302 /*******************************************************************************
304 * FUNCTION: AcpiExAccessRegion
306 * PARAMETERS: ObjDesc - Field to be read
307 * FieldDatumByteOffset - Byte offset of this datum within the
308 * parent field
309 * Value - Where to store value (must at least
310 * 64 bits)
311 * Function - Read or Write flag plus other region-
312 * dependent flags
314 * RETURN: Status
316 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
318 ******************************************************************************/
320 ACPI_STATUS
321 AcpiExAccessRegion (
322 ACPI_OPERAND_OBJECT *ObjDesc,
323 UINT32 FieldDatumByteOffset,
324 UINT64 *Value,
325 UINT32 Function)
327 ACPI_STATUS Status;
328 ACPI_OPERAND_OBJECT *RgnDesc;
329 UINT32 RegionOffset;
332 ACPI_FUNCTION_TRACE (ExAccessRegion);
336 * Ensure that the region operands are fully evaluated and verify
337 * the validity of the request
339 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
340 if (ACPI_FAILURE (Status))
342 return_ACPI_STATUS (Status);
346 * The physical address of this field datum is:
348 * 1) The base of the region, plus
349 * 2) The base offset of the field, plus
350 * 3) The current offset into the field
352 RgnDesc = ObjDesc->CommonField.RegionObj;
353 RegionOffset =
354 ObjDesc->CommonField.BaseByteOffset +
355 FieldDatumByteOffset;
357 if ((Function & ACPI_IO_MASK) == ACPI_READ)
359 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
361 else
363 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
366 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
367 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
368 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
369 RgnDesc->Region.SpaceId,
370 ObjDesc->CommonField.AccessByteWidth,
371 ObjDesc->CommonField.BaseByteOffset,
372 FieldDatumByteOffset,
373 ACPI_CAST_PTR (void, (RgnDesc->Region.Address + RegionOffset))));
375 /* Invoke the appropriate AddressSpace/OpRegion handler */
377 Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
378 Function, RegionOffset,
379 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
381 if (ACPI_FAILURE (Status))
383 if (Status == AE_NOT_IMPLEMENTED)
385 ACPI_ERROR ((AE_INFO,
386 "Region %s (ID=%u) not implemented",
387 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
388 RgnDesc->Region.SpaceId));
390 else if (Status == AE_NOT_EXIST)
392 ACPI_ERROR ((AE_INFO,
393 "Region %s (ID=%u) has no handler",
394 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
395 RgnDesc->Region.SpaceId));
399 return_ACPI_STATUS (Status);
403 /*******************************************************************************
405 * FUNCTION: AcpiExRegisterOverflow
407 * PARAMETERS: ObjDesc - Register(Field) to be written
408 * Value - Value to be stored
410 * RETURN: TRUE if value overflows the field, FALSE otherwise
412 * DESCRIPTION: Check if a value is out of range of the field being written.
413 * Used to check if the values written to Index and Bank registers
414 * are out of range. Normally, the value is simply truncated
415 * to fit the field, but this case is most likely a serious
416 * coding error in the ASL.
418 ******************************************************************************/
420 static BOOLEAN
421 AcpiExRegisterOverflow (
422 ACPI_OPERAND_OBJECT *ObjDesc,
423 UINT64 Value)
426 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
429 * The field is large enough to hold the maximum integer, so we can
430 * never overflow it.
432 return (FALSE);
435 if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
438 * The Value is larger than the maximum value that can fit into
439 * the register.
441 ACPI_ERROR ((AE_INFO,
442 "Index value 0x%8.8X%8.8X overflows field width 0x%X",
443 ACPI_FORMAT_UINT64 (Value),
444 ObjDesc->CommonField.BitLength));
446 return (TRUE);
449 /* The Value will fit into the field with no truncation */
451 return (FALSE);
455 /*******************************************************************************
457 * FUNCTION: AcpiExFieldDatumIo
459 * PARAMETERS: ObjDesc - Field to be read
460 * FieldDatumByteOffset - Byte offset of this datum within the
461 * parent field
462 * Value - Where to store value (must be 64 bits)
463 * ReadWrite - Read or Write flag
465 * RETURN: Status
467 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
468 * demultiplexed here to handle the different types of fields
469 * (BufferField, RegionField, IndexField, BankField)
471 ******************************************************************************/
473 static ACPI_STATUS
474 AcpiExFieldDatumIo (
475 ACPI_OPERAND_OBJECT *ObjDesc,
476 UINT32 FieldDatumByteOffset,
477 UINT64 *Value,
478 UINT32 ReadWrite)
480 ACPI_STATUS Status;
481 UINT64 LocalValue;
484 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
487 if (ReadWrite == ACPI_READ)
489 if (!Value)
491 LocalValue = 0;
493 /* To support reads without saving return value */
494 Value = &LocalValue;
497 /* Clear the entire return buffer first, [Very Important!] */
499 *Value = 0;
503 * The four types of fields are:
505 * BufferField - Read/write from/to a Buffer
506 * RegionField - Read/write from/to a Operation Region.
507 * BankField - Write to a Bank Register, then read/write from/to an
508 * OperationRegion
509 * IndexField - Write to an Index Register, then read/write from/to a
510 * Data Register
512 switch (ObjDesc->Common.Type)
514 case ACPI_TYPE_BUFFER_FIELD:
516 * If the BufferField arguments have not been previously evaluated,
517 * evaluate them now and save the results.
519 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
521 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
522 if (ACPI_FAILURE (Status))
524 return_ACPI_STATUS (Status);
528 if (ReadWrite == ACPI_READ)
531 * Copy the data from the source buffer.
532 * Length is the field width in bytes.
534 ACPI_MEMCPY (Value,
535 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
536 ObjDesc->BufferField.BaseByteOffset +
537 FieldDatumByteOffset,
538 ObjDesc->CommonField.AccessByteWidth);
540 else
543 * Copy the data to the target buffer.
544 * Length is the field width in bytes.
546 ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
547 ObjDesc->BufferField.BaseByteOffset +
548 FieldDatumByteOffset,
549 Value, ObjDesc->CommonField.AccessByteWidth);
552 Status = AE_OK;
553 break;
556 case ACPI_TYPE_LOCAL_BANK_FIELD:
559 * Ensure that the BankValue is not beyond the capacity of
560 * the register
562 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
563 (UINT64) ObjDesc->BankField.Value))
565 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
569 * For BankFields, we must write the BankValue to the BankRegister
570 * (itself a RegionField) before we can access the data.
572 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
573 &ObjDesc->BankField.Value,
574 sizeof (ObjDesc->BankField.Value));
575 if (ACPI_FAILURE (Status))
577 return_ACPI_STATUS (Status);
581 * Now that the Bank has been selected, fall through to the
582 * RegionField case and write the datum to the Operation Region
585 /*lint -fallthrough */
588 case ACPI_TYPE_LOCAL_REGION_FIELD:
590 * For simple RegionFields, we just directly access the owning
591 * Operation Region.
593 Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
594 ReadWrite);
595 break;
598 case ACPI_TYPE_LOCAL_INDEX_FIELD:
602 * Ensure that the IndexValue is not beyond the capacity of
603 * the register
605 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
606 (UINT64) ObjDesc->IndexField.Value))
608 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
611 /* Write the index value to the IndexRegister (itself a RegionField) */
613 FieldDatumByteOffset += ObjDesc->IndexField.Value;
615 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
616 "Write to Index Register: Value %8.8X\n",
617 FieldDatumByteOffset));
619 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
620 &FieldDatumByteOffset,
621 sizeof (FieldDatumByteOffset));
622 if (ACPI_FAILURE (Status))
624 return_ACPI_STATUS (Status);
627 if (ReadWrite == ACPI_READ)
629 /* Read the datum from the DataRegister */
631 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
632 "Read from Data Register\n"));
634 Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
635 Value, sizeof (UINT64));
637 else
639 /* Write the datum to the DataRegister */
641 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
642 "Write to Data Register: Value %8.8X%8.8X\n",
643 ACPI_FORMAT_UINT64 (*Value)));
645 Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
646 Value, sizeof (UINT64));
648 break;
651 default:
653 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
654 ObjDesc->Common.Type));
655 Status = AE_AML_INTERNAL;
656 break;
659 if (ACPI_SUCCESS (Status))
661 if (ReadWrite == ACPI_READ)
663 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
664 "Value Read %8.8X%8.8X, Width %u\n",
665 ACPI_FORMAT_UINT64 (*Value),
666 ObjDesc->CommonField.AccessByteWidth));
668 else
670 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
671 "Value Written %8.8X%8.8X, Width %u\n",
672 ACPI_FORMAT_UINT64 (*Value),
673 ObjDesc->CommonField.AccessByteWidth));
677 return_ACPI_STATUS (Status);
681 /*******************************************************************************
683 * FUNCTION: AcpiExWriteWithUpdateRule
685 * PARAMETERS: ObjDesc - Field to be written
686 * Mask - bitmask within field datum
687 * FieldValue - Value to write
688 * FieldDatumByteOffset - Offset of datum within field
690 * RETURN: Status
692 * DESCRIPTION: Apply the field update rule to a field write
694 ******************************************************************************/
696 ACPI_STATUS
697 AcpiExWriteWithUpdateRule (
698 ACPI_OPERAND_OBJECT *ObjDesc,
699 UINT64 Mask,
700 UINT64 FieldValue,
701 UINT32 FieldDatumByteOffset)
703 ACPI_STATUS Status = AE_OK;
704 UINT64 MergedValue;
705 UINT64 CurrentValue;
708 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
711 /* Start with the new bits */
713 MergedValue = FieldValue;
715 /* If the mask is all ones, we don't need to worry about the update rule */
717 if (Mask != ACPI_UINT64_MAX)
719 /* Decode the update rule */
721 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
723 case AML_FIELD_UPDATE_PRESERVE:
725 * Check if update rule needs to be applied (not if mask is all
726 * ones) The left shift drops the bits we want to ignore.
728 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
729 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
732 * Read the current contents of the byte/word/dword containing
733 * the field, and merge with the new field value.
735 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
736 &CurrentValue, ACPI_READ);
737 if (ACPI_FAILURE (Status))
739 return_ACPI_STATUS (Status);
742 MergedValue |= (CurrentValue & ~Mask);
744 break;
746 case AML_FIELD_UPDATE_WRITE_AS_ONES:
748 /* Set positions outside the field to all ones */
750 MergedValue |= ~Mask;
751 break;
753 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
755 /* Set positions outside the field to all zeros */
757 MergedValue &= Mask;
758 break;
760 default:
762 ACPI_ERROR ((AE_INFO,
763 "Unknown UpdateRule value: 0x%X",
764 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
765 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
769 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
770 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
771 ACPI_FORMAT_UINT64 (Mask),
772 FieldDatumByteOffset,
773 ObjDesc->CommonField.AccessByteWidth,
774 ACPI_FORMAT_UINT64 (FieldValue),
775 ACPI_FORMAT_UINT64 (MergedValue)));
777 /* Write the merged value */
779 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
780 &MergedValue, ACPI_WRITE);
782 return_ACPI_STATUS (Status);
786 /*******************************************************************************
788 * FUNCTION: AcpiExExtractFromField
790 * PARAMETERS: ObjDesc - Field to be read
791 * Buffer - Where to store the field data
792 * BufferLength - Length of Buffer
794 * RETURN: Status
796 * DESCRIPTION: Retrieve the current value of the given field
798 ******************************************************************************/
800 ACPI_STATUS
801 AcpiExExtractFromField (
802 ACPI_OPERAND_OBJECT *ObjDesc,
803 void *Buffer,
804 UINT32 BufferLength)
806 ACPI_STATUS Status;
807 UINT64 RawDatum;
808 UINT64 MergedDatum;
809 UINT32 FieldOffset = 0;
810 UINT32 BufferOffset = 0;
811 UINT32 BufferTailBits;
812 UINT32 DatumCount;
813 UINT32 FieldDatumCount;
814 UINT32 AccessBitWidth;
815 UINT32 i;
818 ACPI_FUNCTION_TRACE (ExExtractFromField);
821 /* Validate target buffer and clear it */
823 if (BufferLength <
824 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
826 ACPI_ERROR ((AE_INFO,
827 "Field size %u (bits) is too large for buffer (%u)",
828 ObjDesc->CommonField.BitLength, BufferLength));
830 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
833 ACPI_MEMSET (Buffer, 0, BufferLength);
834 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
836 /* Handle the simple case here */
838 if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
839 (ObjDesc->CommonField.BitLength == AccessBitWidth))
841 Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
842 return_ACPI_STATUS (Status);
845 /* TBD: Move to common setup code */
847 /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
849 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
851 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
852 AccessBitWidth = sizeof (UINT64) * 8;
855 /* Compute the number of datums (access width data items) */
857 DatumCount = ACPI_ROUND_UP_TO (
858 ObjDesc->CommonField.BitLength, AccessBitWidth);
860 FieldDatumCount = ACPI_ROUND_UP_TO (
861 ObjDesc->CommonField.BitLength +
862 ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
864 /* Priming read from the field */
866 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
867 if (ACPI_FAILURE (Status))
869 return_ACPI_STATUS (Status);
871 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
873 /* Read the rest of the field */
875 for (i = 1; i < FieldDatumCount; i++)
877 /* Get next input datum from the field */
879 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
880 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
881 &RawDatum, ACPI_READ);
882 if (ACPI_FAILURE (Status))
884 return_ACPI_STATUS (Status);
888 * Merge with previous datum if necessary.
890 * Note: Before the shift, check if the shift value will be larger than
891 * the integer size. If so, there is no need to perform the operation.
892 * This avoids the differences in behavior between different compilers
893 * concerning shift values larger than the target data width.
895 if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
896 ACPI_INTEGER_BIT_SIZE)
898 MergedDatum |= RawDatum <<
899 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
902 if (i == DatumCount)
904 break;
907 /* Write merged datum to target buffer */
909 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
910 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
911 BufferLength - BufferOffset));
913 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
914 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
917 /* Mask off any extra bits in the last datum */
919 BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
920 if (BufferTailBits)
922 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
925 /* Write the last datum to the buffer */
927 ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
928 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
929 BufferLength - BufferOffset));
931 return_ACPI_STATUS (AE_OK);
935 /*******************************************************************************
937 * FUNCTION: AcpiExInsertIntoField
939 * PARAMETERS: ObjDesc - Field to be written
940 * Buffer - Data to be written
941 * BufferLength - Length of Buffer
943 * RETURN: Status
945 * DESCRIPTION: Store the Buffer contents into the given field
947 ******************************************************************************/
949 ACPI_STATUS
950 AcpiExInsertIntoField (
951 ACPI_OPERAND_OBJECT *ObjDesc,
952 void *Buffer,
953 UINT32 BufferLength)
955 void *NewBuffer;
956 ACPI_STATUS Status;
957 UINT64 Mask;
958 UINT64 WidthMask;
959 UINT64 MergedDatum;
960 UINT64 RawDatum = 0;
961 UINT32 FieldOffset = 0;
962 UINT32 BufferOffset = 0;
963 UINT32 BufferTailBits;
964 UINT32 DatumCount;
965 UINT32 FieldDatumCount;
966 UINT32 AccessBitWidth;
967 UINT32 RequiredLength;
968 UINT32 i;
971 ACPI_FUNCTION_TRACE (ExInsertIntoField);
974 /* Validate input buffer */
976 NewBuffer = NULL;
977 RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
978 ObjDesc->CommonField.BitLength);
980 * We must have a buffer that is at least as long as the field
981 * we are writing to. This is because individual fields are
982 * indivisible and partial writes are not supported -- as per
983 * the ACPI specification.
985 if (BufferLength < RequiredLength)
987 /* We need to create a new buffer */
989 NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
990 if (!NewBuffer)
992 return_ACPI_STATUS (AE_NO_MEMORY);
996 * Copy the original data to the new buffer, starting
997 * at Byte zero. All unused (upper) bytes of the
998 * buffer will be 0.
1000 ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
1001 Buffer = NewBuffer;
1002 BufferLength = RequiredLength;
1005 /* TBD: Move to common setup code */
1007 /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
1008 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
1010 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
1013 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
1016 * Create the bitmasks used for bit insertion.
1017 * Note: This if/else is used to bypass compiler differences with the
1018 * shift operator
1020 if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
1022 WidthMask = ACPI_UINT64_MAX;
1024 else
1026 WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
1029 Mask = WidthMask &
1030 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
1032 /* Compute the number of datums (access width data items) */
1034 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
1035 AccessBitWidth);
1037 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
1038 ObjDesc->CommonField.StartFieldBitOffset,
1039 AccessBitWidth);
1041 /* Get initial Datum from the input buffer */
1043 ACPI_MEMCPY (&RawDatum, Buffer,
1044 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1045 BufferLength - BufferOffset));
1047 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1049 /* Write the entire field */
1051 for (i = 1; i < FieldDatumCount; i++)
1053 /* Write merged datum to the target field */
1055 MergedDatum &= Mask;
1056 Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
1057 MergedDatum, FieldOffset);
1058 if (ACPI_FAILURE (Status))
1060 goto Exit;
1063 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
1066 * Start new output datum by merging with previous input datum
1067 * if necessary.
1069 * Note: Before the shift, check if the shift value will be larger than
1070 * the integer size. If so, there is no need to perform the operation.
1071 * This avoids the differences in behavior between different compilers
1072 * concerning shift values larger than the target data width.
1074 if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1075 ACPI_INTEGER_BIT_SIZE)
1077 MergedDatum = RawDatum >>
1078 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1080 else
1082 MergedDatum = 0;
1085 Mask = WidthMask;
1087 if (i == DatumCount)
1089 break;
1092 /* Get the next input datum from the buffer */
1094 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1095 ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1096 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1097 BufferLength - BufferOffset));
1099 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1102 /* Mask off any extra bits in the last datum */
1104 BufferTailBits = (ObjDesc->CommonField.BitLength +
1105 ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1106 if (BufferTailBits)
1108 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1111 /* Write the last datum to the field */
1113 MergedDatum &= Mask;
1114 Status = AcpiExWriteWithUpdateRule (ObjDesc,
1115 Mask, MergedDatum, FieldOffset);
1117 Exit:
1118 /* Free temporary buffer if we used one */
1120 if (NewBuffer)
1122 ACPI_FREE (NewBuffer);
1124 return_ACPI_STATUS (Status);