2 /******************************************************************************
4 * Module Name: exprep - ACPI AML (p-code) execution - field prep utilities
6 *****************************************************************************/
8 /******************************************************************************
12 * Some or all of this work - Copyright (c) 1999 - 2010, 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 *****************************************************************************/
119 #include <contrib/dev/acpica/include/acpi.h>
120 #include <contrib/dev/acpica/include/accommon.h>
121 #include <contrib/dev/acpica/include/acinterp.h>
122 #include <contrib/dev/acpica/include/amlcode.h>
123 #include <contrib/dev/acpica/include/acnamesp.h>
126 #define _COMPONENT ACPI_EXECUTER
127 ACPI_MODULE_NAME ("exprep")
129 /* Local prototypes */
132 AcpiExDecodeFieldAccess (
133 ACPI_OPERAND_OBJECT *ObjDesc,
135 UINT32 *ReturnByteAlignment);
138 #ifdef ACPI_UNDER_DEVELOPMENT
141 AcpiExGenerateAccess (
142 UINT32 FieldBitOffset,
143 UINT32 FieldBitLength,
144 UINT32 RegionLength);
146 /*******************************************************************************
148 * FUNCTION: AcpiExGenerateAccess
150 * PARAMETERS: FieldBitOffset - Start of field within parent region/buffer
151 * FieldBitLength - Length of field in bits
152 * RegionLength - Length of parent in bytes
154 * RETURN: Field granularity (8, 16, 32 or 64) and
155 * ByteAlignment (1, 2, 3, or 4)
157 * DESCRIPTION: Generate an optimal access width for fields defined with the
160 * NOTE: Need to have the RegionLength in order to check for boundary
161 * conditions (end-of-region). However, the RegionLength is a deferred
162 * operation. Therefore, to complete this implementation, the generation
163 * of this access width must be deferred until the region length has
166 ******************************************************************************/
169 AcpiExGenerateAccess (
170 UINT32 FieldBitOffset,
171 UINT32 FieldBitLength,
174 UINT32 FieldByteLength;
175 UINT32 FieldByteOffset;
176 UINT32 FieldByteEndOffset;
177 UINT32 AccessByteWidth;
178 UINT32 FieldStartOffset;
179 UINT32 FieldEndOffset;
180 UINT32 MinimumAccessWidth = 0xFFFFFFFF;
181 UINT32 MinimumAccesses = 0xFFFFFFFF;
185 ACPI_FUNCTION_TRACE (ExGenerateAccess);
188 /* Round Field start offset and length to "minimal" byte boundaries */
190 FieldByteOffset = ACPI_DIV_8 (ACPI_ROUND_DOWN (FieldBitOffset, 8));
191 FieldByteEndOffset = ACPI_DIV_8 (ACPI_ROUND_UP (FieldBitLength +
193 FieldByteLength = FieldByteEndOffset - FieldByteOffset;
195 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
196 "Bit length %u, Bit offset %u\n",
197 FieldBitLength, FieldBitOffset));
199 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
200 "Byte Length %u, Byte Offset %u, End Offset %u\n",
201 FieldByteLength, FieldByteOffset, FieldByteEndOffset));
204 * Iterative search for the maximum access width that is both aligned
205 * and does not go beyond the end of the region
207 * Start at ByteAcc and work upwards to QwordAcc max. (1,2,4,8 bytes)
209 for (AccessByteWidth = 1; AccessByteWidth <= 8; AccessByteWidth <<= 1)
212 * 1) Round end offset up to next access boundary and make sure that
213 * this does not go beyond the end of the parent region.
214 * 2) When the Access width is greater than the FieldByteLength, we
215 * are done. (This does not optimize for the perfectly aligned
218 if (ACPI_ROUND_UP (FieldByteEndOffset, AccessByteWidth) <= RegionLength)
221 ACPI_ROUND_DOWN (FieldByteOffset, AccessByteWidth) /
225 ACPI_ROUND_UP ((FieldByteLength + FieldByteOffset),
226 AccessByteWidth) / AccessByteWidth;
228 Accesses = FieldEndOffset - FieldStartOffset;
230 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
231 "AccessWidth %u end is within region\n", AccessByteWidth));
233 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
234 "Field Start %u, Field End %u -- requires %u accesses\n",
235 FieldStartOffset, FieldEndOffset, Accesses));
237 /* Single access is optimal */
241 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
242 "Entire field can be accessed with one operation of size %u\n",
244 return_VALUE (AccessByteWidth);
248 * Fits in the region, but requires more than one read/write.
249 * try the next wider access on next iteration
251 if (Accesses < MinimumAccesses)
253 MinimumAccesses = Accesses;
254 MinimumAccessWidth = AccessByteWidth;
259 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
260 "AccessWidth %u end is NOT within region\n", AccessByteWidth));
261 if (AccessByteWidth == 1)
263 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
264 "Field goes beyond end-of-region!\n"));
266 /* Field does not fit in the region at all */
272 * This width goes beyond the end-of-region, back off to
275 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
276 "Backing off to previous optimal access width of %u\n",
277 MinimumAccessWidth));
278 return_VALUE (MinimumAccessWidth);
283 * Could not read/write field with one operation,
284 * just use max access width
286 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
287 "Cannot access field in one operation, using width 8\n"));
290 #endif /* ACPI_UNDER_DEVELOPMENT */
293 /*******************************************************************************
295 * FUNCTION: AcpiExDecodeFieldAccess
297 * PARAMETERS: ObjDesc - Field object
298 * FieldFlags - Encoded fieldflags (contains access bits)
299 * ReturnByteAlignment - Where the byte alignment is returned
301 * RETURN: Field granularity (8, 16, 32 or 64) and
302 * ByteAlignment (1, 2, 3, or 4)
304 * DESCRIPTION: Decode the AccessType bits of a field definition.
306 ******************************************************************************/
309 AcpiExDecodeFieldAccess (
310 ACPI_OPERAND_OBJECT *ObjDesc,
312 UINT32 *ReturnByteAlignment)
315 UINT32 ByteAlignment;
319 ACPI_FUNCTION_TRACE (ExDecodeFieldAccess);
322 Access = (FieldFlags & AML_FIELD_ACCESS_TYPE_MASK);
326 case AML_FIELD_ACCESS_ANY:
328 #ifdef ACPI_UNDER_DEVELOPMENT
330 AcpiExGenerateAccess (ObjDesc->CommonField.StartFieldBitOffset,
331 ObjDesc->CommonField.BitLength,
332 0xFFFFFFFF /* Temp until we pass RegionLength as parameter */);
333 BitLength = ByteAlignment * 8;
340 case AML_FIELD_ACCESS_BYTE:
341 case AML_FIELD_ACCESS_BUFFER: /* ACPI 2.0 (SMBus Buffer) */
346 case AML_FIELD_ACCESS_WORD:
351 case AML_FIELD_ACCESS_DWORD:
356 case AML_FIELD_ACCESS_QWORD: /* ACPI 2.0 */
362 /* Invalid field access type */
364 ACPI_ERROR ((AE_INFO,
365 "Unknown field access type 0x%X",
370 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
373 * BufferField access can be on any byte boundary, so the
374 * ByteAlignment is always 1 byte -- regardless of any ByteAlignment
375 * implied by the field access type.
380 *ReturnByteAlignment = ByteAlignment;
381 return_UINT32 (BitLength);
385 /*******************************************************************************
387 * FUNCTION: AcpiExPrepCommonFieldObject
389 * PARAMETERS: ObjDesc - The field object
390 * FieldFlags - Access, LockRule, and UpdateRule.
391 * The format of a FieldFlag is described
392 * in the ACPI specification
393 * FieldAttribute - Special attributes (not used)
394 * FieldBitPosition - Field start position
395 * FieldBitLength - Field length in number of bits
399 * DESCRIPTION: Initialize the areas of the field object that are common
400 * to the various types of fields. Note: This is very "sensitive"
401 * code because we are solving the general case for field
404 ******************************************************************************/
407 AcpiExPrepCommonFieldObject (
408 ACPI_OPERAND_OBJECT *ObjDesc,
410 UINT8 FieldAttribute,
411 UINT32 FieldBitPosition,
412 UINT32 FieldBitLength)
414 UINT32 AccessBitWidth;
415 UINT32 ByteAlignment;
416 UINT32 NearestByteAddress;
419 ACPI_FUNCTION_TRACE (ExPrepCommonFieldObject);
423 * Note: the structure being initialized is the
424 * ACPI_COMMON_FIELD_INFO; No structure fields outside of the common
425 * area are initialized by this procedure.
427 ObjDesc->CommonField.FieldFlags = FieldFlags;
428 ObjDesc->CommonField.Attribute = FieldAttribute;
429 ObjDesc->CommonField.BitLength = FieldBitLength;
432 * Decode the access type so we can compute offsets. The access type gives
433 * two pieces of information - the width of each field access and the
434 * necessary ByteAlignment (address granularity) of the access.
436 * For AnyAcc, the AccessBitWidth is the largest width that is both
437 * necessary and possible in an attempt to access the whole field in one
438 * I/O operation. However, for AnyAcc, the ByteAlignment is always one
441 * For all Buffer Fields, the ByteAlignment is always one byte.
443 * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is
444 * the same (equivalent) as the ByteAlignment.
446 AccessBitWidth = AcpiExDecodeFieldAccess (ObjDesc, FieldFlags,
450 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
453 /* Setup width (access granularity) fields (values are: 1, 2, 4, 8) */
455 ObjDesc->CommonField.AccessByteWidth = (UINT8)
456 ACPI_DIV_8 (AccessBitWidth);
459 * BaseByteOffset is the address of the start of the field within the
460 * region. It is the byte address of the first *datum* (field-width data
461 * unit) of the field. (i.e., the first datum that contains at least the
462 * first *bit* of the field.)
464 * Note: ByteAlignment is always either equal to the AccessBitWidth or 8
465 * (Byte access), and it defines the addressing granularity of the parent
469 ACPI_ROUND_BITS_DOWN_TO_BYTES (FieldBitPosition);
470 ObjDesc->CommonField.BaseByteOffset = (UINT32)
471 ACPI_ROUND_DOWN (NearestByteAddress, ByteAlignment);
474 * StartFieldBitOffset is the offset of the first bit of the field within
477 ObjDesc->CommonField.StartFieldBitOffset = (UINT8)
478 (FieldBitPosition - ACPI_MUL_8 (ObjDesc->CommonField.BaseByteOffset));
480 return_ACPI_STATUS (AE_OK);
484 /*******************************************************************************
486 * FUNCTION: AcpiExPrepFieldValue
488 * PARAMETERS: Info - Contains all field creation info
492 * DESCRIPTION: Construct an ACPI_OPERAND_OBJECT of type DefField and
493 * connect it to the parent Node.
495 ******************************************************************************/
498 AcpiExPrepFieldValue (
499 ACPI_CREATE_FIELD_INFO *Info)
501 ACPI_OPERAND_OBJECT *ObjDesc;
502 ACPI_OPERAND_OBJECT *SecondDesc = NULL;
504 UINT32 AccessByteWidth;
508 ACPI_FUNCTION_TRACE (ExPrepFieldValue);
511 /* Parameter validation */
513 if (Info->FieldType != ACPI_TYPE_LOCAL_INDEX_FIELD)
515 if (!Info->RegionNode)
517 ACPI_ERROR ((AE_INFO, "Null RegionNode"));
518 return_ACPI_STATUS (AE_AML_NO_OPERAND);
521 Type = AcpiNsGetType (Info->RegionNode);
522 if (Type != ACPI_TYPE_REGION)
524 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
525 Type, AcpiUtGetTypeName (Type)));
527 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
531 /* Allocate a new field object */
533 ObjDesc = AcpiUtCreateInternalObject (Info->FieldType);
536 return_ACPI_STATUS (AE_NO_MEMORY);
539 /* Initialize areas of the object that are common to all fields */
541 ObjDesc->CommonField.Node = Info->FieldNode;
542 Status = AcpiExPrepCommonFieldObject (ObjDesc,
543 Info->FieldFlags, Info->Attribute,
544 Info->FieldBitPosition, Info->FieldBitLength);
545 if (ACPI_FAILURE (Status))
547 AcpiUtDeleteObjectDesc (ObjDesc);
548 return_ACPI_STATUS (Status);
551 /* Initialize areas of the object that are specific to the field type */
553 switch (Info->FieldType)
555 case ACPI_TYPE_LOCAL_REGION_FIELD:
557 ObjDesc->Field.RegionObj = AcpiNsGetAttachedObject (Info->RegionNode);
559 /* Allow full data read from EC address space */
561 if ((ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_EC) &&
562 (ObjDesc->CommonField.BitLength > 8))
564 AccessByteWidth = ACPI_ROUND_BITS_UP_TO_BYTES (
565 ObjDesc->CommonField.BitLength);
567 /* Maximum byte width supported is 255 */
569 if (AccessByteWidth < 256)
571 ObjDesc->CommonField.AccessByteWidth = (UINT8) AccessByteWidth;
575 /* An additional reference for the container */
577 AcpiUtAddReference (ObjDesc->Field.RegionObj);
579 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
580 "RegionField: BitOff %X, Off %X, Gran %X, Region %p\n",
581 ObjDesc->Field.StartFieldBitOffset, ObjDesc->Field.BaseByteOffset,
582 ObjDesc->Field.AccessByteWidth, ObjDesc->Field.RegionObj));
586 case ACPI_TYPE_LOCAL_BANK_FIELD:
588 ObjDesc->BankField.Value = Info->BankValue;
589 ObjDesc->BankField.RegionObj =
590 AcpiNsGetAttachedObject (Info->RegionNode);
591 ObjDesc->BankField.BankObj =
592 AcpiNsGetAttachedObject (Info->RegisterNode);
594 /* An additional reference for the attached objects */
596 AcpiUtAddReference (ObjDesc->BankField.RegionObj);
597 AcpiUtAddReference (ObjDesc->BankField.BankObj);
599 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
600 "Bank Field: BitOff %X, Off %X, Gran %X, Region %p, BankReg %p\n",
601 ObjDesc->BankField.StartFieldBitOffset,
602 ObjDesc->BankField.BaseByteOffset,
603 ObjDesc->Field.AccessByteWidth,
604 ObjDesc->BankField.RegionObj,
605 ObjDesc->BankField.BankObj));
608 * Remember location in AML stream of the field unit
609 * opcode and operands -- since the BankValue
610 * operands must be evaluated.
612 SecondDesc = ObjDesc->Common.NextObject;
613 SecondDesc->Extra.AmlStart = ACPI_CAST_PTR (ACPI_PARSE_OBJECT,
614 Info->DataRegisterNode)->Named.Data;
615 SecondDesc->Extra.AmlLength = ACPI_CAST_PTR (ACPI_PARSE_OBJECT,
616 Info->DataRegisterNode)->Named.Length;
621 case ACPI_TYPE_LOCAL_INDEX_FIELD:
623 /* Get the Index and Data registers */
625 ObjDesc->IndexField.IndexObj =
626 AcpiNsGetAttachedObject (Info->RegisterNode);
627 ObjDesc->IndexField.DataObj =
628 AcpiNsGetAttachedObject (Info->DataRegisterNode);
630 if (!ObjDesc->IndexField.DataObj || !ObjDesc->IndexField.IndexObj)
632 ACPI_ERROR ((AE_INFO, "Null Index Object during field prep"));
633 AcpiUtDeleteObjectDesc (ObjDesc);
634 return_ACPI_STATUS (AE_AML_INTERNAL);
637 /* An additional reference for the attached objects */
639 AcpiUtAddReference (ObjDesc->IndexField.DataObj);
640 AcpiUtAddReference (ObjDesc->IndexField.IndexObj);
643 * April 2006: Changed to match MS behavior
645 * The value written to the Index register is the byte offset of the
646 * target field in units of the granularity of the IndexField
648 * Previously, the value was calculated as an index in terms of the
649 * width of the Data register, as below:
651 * ObjDesc->IndexField.Value = (UINT32)
652 * (Info->FieldBitPosition / ACPI_MUL_8 (
653 * ObjDesc->Field.AccessByteWidth));
655 * February 2006: Tried value as a byte offset:
656 * ObjDesc->IndexField.Value = (UINT32)
657 * ACPI_DIV_8 (Info->FieldBitPosition);
659 ObjDesc->IndexField.Value = (UINT32) ACPI_ROUND_DOWN (
660 ACPI_DIV_8 (Info->FieldBitPosition),
661 ObjDesc->IndexField.AccessByteWidth);
663 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
664 "IndexField: BitOff %X, Off %X, Value %X, Gran %X, Index %p, Data %p\n",
665 ObjDesc->IndexField.StartFieldBitOffset,
666 ObjDesc->IndexField.BaseByteOffset,
667 ObjDesc->IndexField.Value,
668 ObjDesc->Field.AccessByteWidth,
669 ObjDesc->IndexField.IndexObj,
670 ObjDesc->IndexField.DataObj));
674 /* No other types should get here */
679 * Store the constructed descriptor (ObjDesc) into the parent Node,
680 * preserving the current type of that NamedObj.
682 Status = AcpiNsAttachObject (Info->FieldNode, ObjDesc,
683 AcpiNsGetType (Info->FieldNode));
685 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Set NamedObj %p [%4.4s], ObjDesc %p\n",
686 Info->FieldNode, AcpiUtGetNodeName (Info->FieldNode), ObjDesc));
688 /* Remove local reference to the object */
690 AcpiUtRemoveReference (ObjDesc);
691 return_ACPI_STATUS (Status);