2 /******************************************************************************
4 * Module Name: exprep - ACPI AML (p-code) execution - field prep utilities
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2012, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acinterp.h>
50 #include <contrib/dev/acpica/include/amlcode.h>
51 #include <contrib/dev/acpica/include/acnamesp.h>
52 #include <contrib/dev/acpica/include/acdispat.h>
55 #define _COMPONENT ACPI_EXECUTER
56 ACPI_MODULE_NAME ("exprep")
58 /* Local prototypes */
61 AcpiExDecodeFieldAccess (
62 ACPI_OPERAND_OBJECT *ObjDesc,
64 UINT32 *ReturnByteAlignment);
67 #ifdef ACPI_UNDER_DEVELOPMENT
70 AcpiExGenerateAccess (
71 UINT32 FieldBitOffset,
72 UINT32 FieldBitLength,
75 /*******************************************************************************
77 * FUNCTION: AcpiExGenerateAccess
79 * PARAMETERS: FieldBitOffset - Start of field within parent region/buffer
80 * FieldBitLength - Length of field in bits
81 * RegionLength - Length of parent in bytes
83 * RETURN: Field granularity (8, 16, 32 or 64) and
84 * ByteAlignment (1, 2, 3, or 4)
86 * DESCRIPTION: Generate an optimal access width for fields defined with the
89 * NOTE: Need to have the RegionLength in order to check for boundary
90 * conditions (end-of-region). However, the RegionLength is a deferred
91 * operation. Therefore, to complete this implementation, the generation
92 * of this access width must be deferred until the region length has
95 ******************************************************************************/
98 AcpiExGenerateAccess (
99 UINT32 FieldBitOffset,
100 UINT32 FieldBitLength,
103 UINT32 FieldByteLength;
104 UINT32 FieldByteOffset;
105 UINT32 FieldByteEndOffset;
106 UINT32 AccessByteWidth;
107 UINT32 FieldStartOffset;
108 UINT32 FieldEndOffset;
109 UINT32 MinimumAccessWidth = 0xFFFFFFFF;
110 UINT32 MinimumAccesses = 0xFFFFFFFF;
114 ACPI_FUNCTION_TRACE (ExGenerateAccess);
117 /* Round Field start offset and length to "minimal" byte boundaries */
119 FieldByteOffset = ACPI_DIV_8 (ACPI_ROUND_DOWN (FieldBitOffset, 8));
120 FieldByteEndOffset = ACPI_DIV_8 (ACPI_ROUND_UP (FieldBitLength +
122 FieldByteLength = FieldByteEndOffset - FieldByteOffset;
124 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
125 "Bit length %u, Bit offset %u\n",
126 FieldBitLength, FieldBitOffset));
128 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
129 "Byte Length %u, Byte Offset %u, End Offset %u\n",
130 FieldByteLength, FieldByteOffset, FieldByteEndOffset));
133 * Iterative search for the maximum access width that is both aligned
134 * and does not go beyond the end of the region
136 * Start at ByteAcc and work upwards to QwordAcc max. (1,2,4,8 bytes)
138 for (AccessByteWidth = 1; AccessByteWidth <= 8; AccessByteWidth <<= 1)
141 * 1) Round end offset up to next access boundary and make sure that
142 * this does not go beyond the end of the parent region.
143 * 2) When the Access width is greater than the FieldByteLength, we
144 * are done. (This does not optimize for the perfectly aligned
147 if (ACPI_ROUND_UP (FieldByteEndOffset, AccessByteWidth) <= RegionLength)
150 ACPI_ROUND_DOWN (FieldByteOffset, AccessByteWidth) /
154 ACPI_ROUND_UP ((FieldByteLength + FieldByteOffset),
155 AccessByteWidth) / AccessByteWidth;
157 Accesses = FieldEndOffset - FieldStartOffset;
159 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
160 "AccessWidth %u end is within region\n", AccessByteWidth));
162 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
163 "Field Start %u, Field End %u -- requires %u accesses\n",
164 FieldStartOffset, FieldEndOffset, Accesses));
166 /* Single access is optimal */
170 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
171 "Entire field can be accessed with one operation of size %u\n",
173 return_VALUE (AccessByteWidth);
177 * Fits in the region, but requires more than one read/write.
178 * try the next wider access on next iteration
180 if (Accesses < MinimumAccesses)
182 MinimumAccesses = Accesses;
183 MinimumAccessWidth = AccessByteWidth;
188 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
189 "AccessWidth %u end is NOT within region\n", AccessByteWidth));
190 if (AccessByteWidth == 1)
192 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
193 "Field goes beyond end-of-region!\n"));
195 /* Field does not fit in the region at all */
201 * This width goes beyond the end-of-region, back off to
204 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
205 "Backing off to previous optimal access width of %u\n",
206 MinimumAccessWidth));
207 return_VALUE (MinimumAccessWidth);
212 * Could not read/write field with one operation,
213 * just use max access width
215 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
216 "Cannot access field in one operation, using width 8\n"));
219 #endif /* ACPI_UNDER_DEVELOPMENT */
222 /*******************************************************************************
224 * FUNCTION: AcpiExDecodeFieldAccess
226 * PARAMETERS: ObjDesc - Field object
227 * FieldFlags - Encoded fieldflags (contains access bits)
228 * ReturnByteAlignment - Where the byte alignment is returned
230 * RETURN: Field granularity (8, 16, 32 or 64) and
231 * ByteAlignment (1, 2, 3, or 4)
233 * DESCRIPTION: Decode the AccessType bits of a field definition.
235 ******************************************************************************/
238 AcpiExDecodeFieldAccess (
239 ACPI_OPERAND_OBJECT *ObjDesc,
241 UINT32 *ReturnByteAlignment)
244 UINT32 ByteAlignment;
248 ACPI_FUNCTION_TRACE (ExDecodeFieldAccess);
251 Access = (FieldFlags & AML_FIELD_ACCESS_TYPE_MASK);
255 case AML_FIELD_ACCESS_ANY:
257 #ifdef ACPI_UNDER_DEVELOPMENT
259 AcpiExGenerateAccess (ObjDesc->CommonField.StartFieldBitOffset,
260 ObjDesc->CommonField.BitLength,
261 0xFFFFFFFF /* Temp until we pass RegionLength as parameter */);
262 BitLength = ByteAlignment * 8;
269 case AML_FIELD_ACCESS_BYTE:
270 case AML_FIELD_ACCESS_BUFFER: /* ACPI 2.0 (SMBus Buffer) */
275 case AML_FIELD_ACCESS_WORD:
280 case AML_FIELD_ACCESS_DWORD:
285 case AML_FIELD_ACCESS_QWORD: /* ACPI 2.0 */
291 /* Invalid field access type */
293 ACPI_ERROR ((AE_INFO,
294 "Unknown field access type 0x%X",
299 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
302 * BufferField access can be on any byte boundary, so the
303 * ByteAlignment is always 1 byte -- regardless of any ByteAlignment
304 * implied by the field access type.
309 *ReturnByteAlignment = ByteAlignment;
310 return_UINT32 (BitLength);
314 /*******************************************************************************
316 * FUNCTION: AcpiExPrepCommonFieldObject
318 * PARAMETERS: ObjDesc - The field object
319 * FieldFlags - Access, LockRule, and UpdateRule.
320 * The format of a FieldFlag is described
321 * in the ACPI specification
322 * FieldAttribute - Special attributes (not used)
323 * FieldBitPosition - Field start position
324 * FieldBitLength - Field length in number of bits
328 * DESCRIPTION: Initialize the areas of the field object that are common
329 * to the various types of fields. Note: This is very "sensitive"
330 * code because we are solving the general case for field
333 ******************************************************************************/
336 AcpiExPrepCommonFieldObject (
337 ACPI_OPERAND_OBJECT *ObjDesc,
339 UINT8 FieldAttribute,
340 UINT32 FieldBitPosition,
341 UINT32 FieldBitLength)
343 UINT32 AccessBitWidth;
344 UINT32 ByteAlignment;
345 UINT32 NearestByteAddress;
348 ACPI_FUNCTION_TRACE (ExPrepCommonFieldObject);
352 * Note: the structure being initialized is the
353 * ACPI_COMMON_FIELD_INFO; No structure fields outside of the common
354 * area are initialized by this procedure.
356 ObjDesc->CommonField.FieldFlags = FieldFlags;
357 ObjDesc->CommonField.Attribute = FieldAttribute;
358 ObjDesc->CommonField.BitLength = FieldBitLength;
361 * Decode the access type so we can compute offsets. The access type gives
362 * two pieces of information - the width of each field access and the
363 * necessary ByteAlignment (address granularity) of the access.
365 * For AnyAcc, the AccessBitWidth is the largest width that is both
366 * necessary and possible in an attempt to access the whole field in one
367 * I/O operation. However, for AnyAcc, the ByteAlignment is always one
370 * For all Buffer Fields, the ByteAlignment is always one byte.
372 * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is
373 * the same (equivalent) as the ByteAlignment.
375 AccessBitWidth = AcpiExDecodeFieldAccess (ObjDesc, FieldFlags,
379 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
382 /* Setup width (access granularity) fields (values are: 1, 2, 4, 8) */
384 ObjDesc->CommonField.AccessByteWidth = (UINT8)
385 ACPI_DIV_8 (AccessBitWidth);
388 * BaseByteOffset is the address of the start of the field within the
389 * region. It is the byte address of the first *datum* (field-width data
390 * unit) of the field. (i.e., the first datum that contains at least the
391 * first *bit* of the field.)
393 * Note: ByteAlignment is always either equal to the AccessBitWidth or 8
394 * (Byte access), and it defines the addressing granularity of the parent
398 ACPI_ROUND_BITS_DOWN_TO_BYTES (FieldBitPosition);
399 ObjDesc->CommonField.BaseByteOffset = (UINT32)
400 ACPI_ROUND_DOWN (NearestByteAddress, ByteAlignment);
403 * StartFieldBitOffset is the offset of the first bit of the field within
406 ObjDesc->CommonField.StartFieldBitOffset = (UINT8)
407 (FieldBitPosition - ACPI_MUL_8 (ObjDesc->CommonField.BaseByteOffset));
409 return_ACPI_STATUS (AE_OK);
413 /*******************************************************************************
415 * FUNCTION: AcpiExPrepFieldValue
417 * PARAMETERS: Info - Contains all field creation info
421 * DESCRIPTION: Construct an object of type ACPI_OPERAND_OBJECT with a
422 * subtype of DefField and connect it to the parent Node.
424 ******************************************************************************/
427 AcpiExPrepFieldValue (
428 ACPI_CREATE_FIELD_INFO *Info)
430 ACPI_OPERAND_OBJECT *ObjDesc;
431 ACPI_OPERAND_OBJECT *SecondDesc = NULL;
433 UINT32 AccessByteWidth;
437 ACPI_FUNCTION_TRACE (ExPrepFieldValue);
440 /* Parameter validation */
442 if (Info->FieldType != ACPI_TYPE_LOCAL_INDEX_FIELD)
444 if (!Info->RegionNode)
446 ACPI_ERROR ((AE_INFO, "Null RegionNode"));
447 return_ACPI_STATUS (AE_AML_NO_OPERAND);
450 Type = AcpiNsGetType (Info->RegionNode);
451 if (Type != ACPI_TYPE_REGION)
453 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
454 Type, AcpiUtGetTypeName (Type)));
456 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
460 /* Allocate a new field object */
462 ObjDesc = AcpiUtCreateInternalObject (Info->FieldType);
465 return_ACPI_STATUS (AE_NO_MEMORY);
468 /* Initialize areas of the object that are common to all fields */
470 ObjDesc->CommonField.Node = Info->FieldNode;
471 Status = AcpiExPrepCommonFieldObject (ObjDesc,
472 Info->FieldFlags, Info->Attribute,
473 Info->FieldBitPosition, Info->FieldBitLength);
474 if (ACPI_FAILURE (Status))
476 AcpiUtDeleteObjectDesc (ObjDesc);
477 return_ACPI_STATUS (Status);
480 /* Initialize areas of the object that are specific to the field type */
482 switch (Info->FieldType)
484 case ACPI_TYPE_LOCAL_REGION_FIELD:
486 ObjDesc->Field.RegionObj = AcpiNsGetAttachedObject (Info->RegionNode);
488 /* Fields specific to GenericSerialBus fields */
490 ObjDesc->Field.AccessLength = Info->AccessLength;
492 if (Info->ConnectionNode)
494 SecondDesc = Info->ConnectionNode->Object;
495 if (!(SecondDesc->Common.Flags & AOPOBJ_DATA_VALID))
497 Status = AcpiDsGetBufferArguments (SecondDesc);
498 if (ACPI_FAILURE (Status))
500 AcpiUtDeleteObjectDesc (ObjDesc);
501 return_ACPI_STATUS (Status);
505 ObjDesc->Field.ResourceBuffer = SecondDesc->Buffer.Pointer;
506 ObjDesc->Field.ResourceLength = (UINT16) SecondDesc->Buffer.Length;
508 else if (Info->ResourceBuffer)
510 ObjDesc->Field.ResourceBuffer = Info->ResourceBuffer;
511 ObjDesc->Field.ResourceLength = Info->ResourceLength;
514 /* Allow full data read from EC address space */
516 if ((ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_EC) &&
517 (ObjDesc->CommonField.BitLength > 8))
519 AccessByteWidth = ACPI_ROUND_BITS_UP_TO_BYTES (
520 ObjDesc->CommonField.BitLength);
522 /* Maximum byte width supported is 255 */
524 if (AccessByteWidth < 256)
526 ObjDesc->CommonField.AccessByteWidth = (UINT8) AccessByteWidth;
530 /* An additional reference for the container */
532 AcpiUtAddReference (ObjDesc->Field.RegionObj);
534 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
535 "RegionField: BitOff %X, Off %X, Gran %X, Region %p\n",
536 ObjDesc->Field.StartFieldBitOffset, ObjDesc->Field.BaseByteOffset,
537 ObjDesc->Field.AccessByteWidth, ObjDesc->Field.RegionObj));
541 case ACPI_TYPE_LOCAL_BANK_FIELD:
543 ObjDesc->BankField.Value = Info->BankValue;
544 ObjDesc->BankField.RegionObj =
545 AcpiNsGetAttachedObject (Info->RegionNode);
546 ObjDesc->BankField.BankObj =
547 AcpiNsGetAttachedObject (Info->RegisterNode);
549 /* An additional reference for the attached objects */
551 AcpiUtAddReference (ObjDesc->BankField.RegionObj);
552 AcpiUtAddReference (ObjDesc->BankField.BankObj);
554 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
555 "Bank Field: BitOff %X, Off %X, Gran %X, Region %p, BankReg %p\n",
556 ObjDesc->BankField.StartFieldBitOffset,
557 ObjDesc->BankField.BaseByteOffset,
558 ObjDesc->Field.AccessByteWidth,
559 ObjDesc->BankField.RegionObj,
560 ObjDesc->BankField.BankObj));
563 * Remember location in AML stream of the field unit
564 * opcode and operands -- since the BankValue
565 * operands must be evaluated.
567 SecondDesc = ObjDesc->Common.NextObject;
568 SecondDesc->Extra.AmlStart = ACPI_CAST_PTR (ACPI_PARSE_OBJECT,
569 Info->DataRegisterNode)->Named.Data;
570 SecondDesc->Extra.AmlLength = ACPI_CAST_PTR (ACPI_PARSE_OBJECT,
571 Info->DataRegisterNode)->Named.Length;
576 case ACPI_TYPE_LOCAL_INDEX_FIELD:
578 /* Get the Index and Data registers */
580 ObjDesc->IndexField.IndexObj =
581 AcpiNsGetAttachedObject (Info->RegisterNode);
582 ObjDesc->IndexField.DataObj =
583 AcpiNsGetAttachedObject (Info->DataRegisterNode);
585 if (!ObjDesc->IndexField.DataObj || !ObjDesc->IndexField.IndexObj)
587 ACPI_ERROR ((AE_INFO, "Null Index Object during field prep"));
588 AcpiUtDeleteObjectDesc (ObjDesc);
589 return_ACPI_STATUS (AE_AML_INTERNAL);
592 /* An additional reference for the attached objects */
594 AcpiUtAddReference (ObjDesc->IndexField.DataObj);
595 AcpiUtAddReference (ObjDesc->IndexField.IndexObj);
598 * April 2006: Changed to match MS behavior
600 * The value written to the Index register is the byte offset of the
601 * target field in units of the granularity of the IndexField
603 * Previously, the value was calculated as an index in terms of the
604 * width of the Data register, as below:
606 * ObjDesc->IndexField.Value = (UINT32)
607 * (Info->FieldBitPosition / ACPI_MUL_8 (
608 * ObjDesc->Field.AccessByteWidth));
610 * February 2006: Tried value as a byte offset:
611 * ObjDesc->IndexField.Value = (UINT32)
612 * ACPI_DIV_8 (Info->FieldBitPosition);
614 ObjDesc->IndexField.Value = (UINT32) ACPI_ROUND_DOWN (
615 ACPI_DIV_8 (Info->FieldBitPosition),
616 ObjDesc->IndexField.AccessByteWidth);
618 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
619 "IndexField: BitOff %X, Off %X, Value %X, Gran %X, Index %p, Data %p\n",
620 ObjDesc->IndexField.StartFieldBitOffset,
621 ObjDesc->IndexField.BaseByteOffset,
622 ObjDesc->IndexField.Value,
623 ObjDesc->Field.AccessByteWidth,
624 ObjDesc->IndexField.IndexObj,
625 ObjDesc->IndexField.DataObj));
629 /* No other types should get here */
634 * Store the constructed descriptor (ObjDesc) into the parent Node,
635 * preserving the current type of that NamedObj.
637 Status = AcpiNsAttachObject (Info->FieldNode, ObjDesc,
638 AcpiNsGetType (Info->FieldNode));
640 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Set NamedObj %p [%4.4s], ObjDesc %p\n",
641 Info->FieldNode, AcpiUtGetNodeName (Info->FieldNode), ObjDesc));
643 /* Remove local reference to the object */
645 AcpiUtRemoveReference (ObjDesc);
646 return_ACPI_STATUS (Status);