1 /******************************************************************************
3 * Module Name: exprep - ACPI AML field prep utilities
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2016, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
52 #define _COMPONENT ACPI_EXECUTER
53 ACPI_MODULE_NAME ("exprep")
55 /* Local prototypes */
58 AcpiExDecodeFieldAccess (
59 ACPI_OPERAND_OBJECT
*ObjDesc
,
61 UINT32
*ReturnByteAlignment
);
64 #ifdef ACPI_UNDER_DEVELOPMENT
67 AcpiExGenerateAccess (
68 UINT32 FieldBitOffset
,
69 UINT32 FieldBitLength
,
73 /*******************************************************************************
75 * FUNCTION: AcpiExGenerateAccess
77 * PARAMETERS: FieldBitOffset - Start of field within parent region/buffer
78 * FieldBitLength - Length of field in bits
79 * RegionLength - Length of parent in bytes
81 * RETURN: Field granularity (8, 16, 32 or 64) and
82 * ByteAlignment (1, 2, 3, or 4)
84 * DESCRIPTION: Generate an optimal access width for fields defined with the
87 * NOTE: Need to have the RegionLength in order to check for boundary
88 * conditions (end-of-region). However, the RegionLength is a deferred
89 * operation. Therefore, to complete this implementation, the generation
90 * of this access width must be deferred until the region length has
93 ******************************************************************************/
96 AcpiExGenerateAccess (
97 UINT32 FieldBitOffset
,
98 UINT32 FieldBitLength
,
101 UINT32 FieldByteLength
;
102 UINT32 FieldByteOffset
;
103 UINT32 FieldByteEndOffset
;
104 UINT32 AccessByteWidth
;
105 UINT32 FieldStartOffset
;
106 UINT32 FieldEndOffset
;
107 UINT32 MinimumAccessWidth
= 0xFFFFFFFF;
108 UINT32 MinimumAccesses
= 0xFFFFFFFF;
112 ACPI_FUNCTION_TRACE (ExGenerateAccess
);
115 /* Round Field start offset and length to "minimal" byte boundaries */
117 FieldByteOffset
= ACPI_DIV_8 (
118 ACPI_ROUND_DOWN (FieldBitOffset
, 8));
120 FieldByteEndOffset
= ACPI_DIV_8 (
121 ACPI_ROUND_UP (FieldBitLength
+ FieldBitOffset
, 8));
123 FieldByteLength
= FieldByteEndOffset
- FieldByteOffset
;
125 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
126 "Bit length %u, Bit offset %u\n",
127 FieldBitLength
, FieldBitOffset
));
129 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
130 "Byte Length %u, Byte Offset %u, End Offset %u\n",
131 FieldByteLength
, FieldByteOffset
, FieldByteEndOffset
));
134 * Iterative search for the maximum access width that is both aligned
135 * and does not go beyond the end of the region
137 * Start at ByteAcc and work upwards to QwordAcc max. (1,2,4,8 bytes)
139 for (AccessByteWidth
= 1; AccessByteWidth
<= 8; AccessByteWidth
<<= 1)
142 * 1) Round end offset up to next access boundary and make sure that
143 * this does not go beyond the end of the parent region.
144 * 2) When the Access width is greater than the FieldByteLength, we
145 * are done. (This does not optimize for the perfectly aligned
148 if (ACPI_ROUND_UP (FieldByteEndOffset
, AccessByteWidth
) <=
152 ACPI_ROUND_DOWN (FieldByteOffset
, AccessByteWidth
) /
156 ACPI_ROUND_UP ((FieldByteLength
+ FieldByteOffset
),
157 AccessByteWidth
) / AccessByteWidth
;
159 Accesses
= FieldEndOffset
- FieldStartOffset
;
161 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
162 "AccessWidth %u end is within region\n", AccessByteWidth
));
164 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
165 "Field Start %u, Field End %u -- requires %u accesses\n",
166 FieldStartOffset
, FieldEndOffset
, Accesses
));
168 /* Single access is optimal */
172 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
173 "Entire field can be accessed "
174 "with one operation of size %u\n",
176 return_VALUE (AccessByteWidth
);
180 * Fits in the region, but requires more than one read/write.
181 * try the next wider access on next iteration
183 if (Accesses
< MinimumAccesses
)
185 MinimumAccesses
= Accesses
;
186 MinimumAccessWidth
= AccessByteWidth
;
191 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
192 "AccessWidth %u end is NOT within region\n",
194 if (AccessByteWidth
== 1)
196 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
197 "Field goes beyond end-of-region!\n"));
199 /* Field does not fit in the region at all */
205 * This width goes beyond the end-of-region, back off to
208 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
209 "Backing off to previous optimal access width of %u\n",
210 MinimumAccessWidth
));
211 return_VALUE (MinimumAccessWidth
);
216 * Could not read/write field with one operation,
217 * just use max access width
219 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
220 "Cannot access field in one operation, using width 8\n"));
224 #endif /* ACPI_UNDER_DEVELOPMENT */
227 /*******************************************************************************
229 * FUNCTION: AcpiExDecodeFieldAccess
231 * PARAMETERS: ObjDesc - Field object
232 * FieldFlags - Encoded fieldflags (contains access bits)
233 * ReturnByteAlignment - Where the byte alignment is returned
235 * RETURN: Field granularity (8, 16, 32 or 64) and
236 * ByteAlignment (1, 2, 3, or 4)
238 * DESCRIPTION: Decode the AccessType bits of a field definition.
240 ******************************************************************************/
243 AcpiExDecodeFieldAccess (
244 ACPI_OPERAND_OBJECT
*ObjDesc
,
246 UINT32
*ReturnByteAlignment
)
249 UINT32 ByteAlignment
;
253 ACPI_FUNCTION_TRACE (ExDecodeFieldAccess
);
256 Access
= (FieldFlags
& AML_FIELD_ACCESS_TYPE_MASK
);
260 case AML_FIELD_ACCESS_ANY
:
262 #ifdef ACPI_UNDER_DEVELOPMENT
264 AcpiExGenerateAccess (ObjDesc
->CommonField
.StartFieldBitOffset
,
265 ObjDesc
->CommonField
.BitLength
,
266 0xFFFFFFFF /* Temp until we pass RegionLength as parameter */);
267 BitLength
= ByteAlignment
* 8;
274 case AML_FIELD_ACCESS_BYTE
:
275 case AML_FIELD_ACCESS_BUFFER
: /* ACPI 2.0 (SMBus Buffer) */
281 case AML_FIELD_ACCESS_WORD
:
287 case AML_FIELD_ACCESS_DWORD
:
293 case AML_FIELD_ACCESS_QWORD
: /* ACPI 2.0 */
301 /* Invalid field access type */
303 ACPI_ERROR ((AE_INFO
,
304 "Unknown field access type 0x%X",
310 if (ObjDesc
->Common
.Type
== ACPI_TYPE_BUFFER_FIELD
)
313 * BufferField access can be on any byte boundary, so the
314 * ByteAlignment is always 1 byte -- regardless of any ByteAlignment
315 * implied by the field access type.
320 *ReturnByteAlignment
= ByteAlignment
;
321 return_UINT32 (BitLength
);
325 /*******************************************************************************
327 * FUNCTION: AcpiExPrepCommonFieldObject
329 * PARAMETERS: ObjDesc - The field object
330 * FieldFlags - Access, LockRule, and UpdateRule.
331 * The format of a FieldFlag is described
332 * in the ACPI specification
333 * FieldAttribute - Special attributes (not used)
334 * FieldBitPosition - Field start position
335 * FieldBitLength - Field length in number of bits
339 * DESCRIPTION: Initialize the areas of the field object that are common
340 * to the various types of fields. Note: This is very "sensitive"
341 * code because we are solving the general case for field
344 ******************************************************************************/
347 AcpiExPrepCommonFieldObject (
348 ACPI_OPERAND_OBJECT
*ObjDesc
,
350 UINT8 FieldAttribute
,
351 UINT32 FieldBitPosition
,
352 UINT32 FieldBitLength
)
354 UINT32 AccessBitWidth
;
355 UINT32 ByteAlignment
;
356 UINT32 NearestByteAddress
;
359 ACPI_FUNCTION_TRACE (ExPrepCommonFieldObject
);
363 * Note: the structure being initialized is the
364 * ACPI_COMMON_FIELD_INFO; No structure fields outside of the common
365 * area are initialized by this procedure.
367 ObjDesc
->CommonField
.FieldFlags
= FieldFlags
;
368 ObjDesc
->CommonField
.Attribute
= FieldAttribute
;
369 ObjDesc
->CommonField
.BitLength
= FieldBitLength
;
372 * Decode the access type so we can compute offsets. The access type gives
373 * two pieces of information - the width of each field access and the
374 * necessary ByteAlignment (address granularity) of the access.
376 * For AnyAcc, the AccessBitWidth is the largest width that is both
377 * necessary and possible in an attempt to access the whole field in one
378 * I/O operation. However, for AnyAcc, the ByteAlignment is always one
381 * For all Buffer Fields, the ByteAlignment is always one byte.
383 * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is
384 * the same (equivalent) as the ByteAlignment.
386 AccessBitWidth
= AcpiExDecodeFieldAccess (
387 ObjDesc
, FieldFlags
, &ByteAlignment
);
390 return_ACPI_STATUS (AE_AML_OPERAND_VALUE
);
393 /* Setup width (access granularity) fields (values are: 1, 2, 4, 8) */
395 ObjDesc
->CommonField
.AccessByteWidth
= (UINT8
)
396 ACPI_DIV_8 (AccessBitWidth
);
399 * BaseByteOffset is the address of the start of the field within the
400 * region. It is the byte address of the first *datum* (field-width data
401 * unit) of the field. (i.e., the first datum that contains at least the
402 * first *bit* of the field.)
404 * Note: ByteAlignment is always either equal to the AccessBitWidth or 8
405 * (Byte access), and it defines the addressing granularity of the parent
409 ACPI_ROUND_BITS_DOWN_TO_BYTES (FieldBitPosition
);
410 ObjDesc
->CommonField
.BaseByteOffset
= (UINT32
)
411 ACPI_ROUND_DOWN (NearestByteAddress
, ByteAlignment
);
414 * StartFieldBitOffset is the offset of the first bit of the field within
417 ObjDesc
->CommonField
.StartFieldBitOffset
= (UINT8
)
418 (FieldBitPosition
- ACPI_MUL_8 (ObjDesc
->CommonField
.BaseByteOffset
));
420 return_ACPI_STATUS (AE_OK
);
424 /*******************************************************************************
426 * FUNCTION: AcpiExPrepFieldValue
428 * PARAMETERS: Info - Contains all field creation info
432 * DESCRIPTION: Construct an object of type ACPI_OPERAND_OBJECT with a
433 * subtype of DefField and connect it to the parent Node.
435 ******************************************************************************/
438 AcpiExPrepFieldValue (
439 ACPI_CREATE_FIELD_INFO
*Info
)
441 ACPI_OPERAND_OBJECT
*ObjDesc
;
442 ACPI_OPERAND_OBJECT
*SecondDesc
= NULL
;
444 UINT32 AccessByteWidth
;
448 ACPI_FUNCTION_TRACE (ExPrepFieldValue
);
451 /* Parameter validation */
453 if (Info
->FieldType
!= ACPI_TYPE_LOCAL_INDEX_FIELD
)
455 if (!Info
->RegionNode
)
457 ACPI_ERROR ((AE_INFO
, "Null RegionNode"));
458 return_ACPI_STATUS (AE_AML_NO_OPERAND
);
461 Type
= AcpiNsGetType (Info
->RegionNode
);
462 if (Type
!= ACPI_TYPE_REGION
)
464 ACPI_ERROR ((AE_INFO
, "Needed Region, found type 0x%X (%s)",
465 Type
, AcpiUtGetTypeName (Type
)));
467 return_ACPI_STATUS (AE_AML_OPERAND_TYPE
);
471 /* Allocate a new field object */
473 ObjDesc
= AcpiUtCreateInternalObject (Info
->FieldType
);
476 return_ACPI_STATUS (AE_NO_MEMORY
);
479 /* Initialize areas of the object that are common to all fields */
481 ObjDesc
->CommonField
.Node
= Info
->FieldNode
;
482 Status
= AcpiExPrepCommonFieldObject (ObjDesc
,
483 Info
->FieldFlags
, Info
->Attribute
,
484 Info
->FieldBitPosition
, Info
->FieldBitLength
);
485 if (ACPI_FAILURE (Status
))
487 AcpiUtDeleteObjectDesc (ObjDesc
);
488 return_ACPI_STATUS (Status
);
491 /* Initialize areas of the object that are specific to the field type */
493 switch (Info
->FieldType
)
495 case ACPI_TYPE_LOCAL_REGION_FIELD
:
497 ObjDesc
->Field
.RegionObj
= AcpiNsGetAttachedObject (Info
->RegionNode
);
499 /* Fields specific to GenericSerialBus fields */
501 ObjDesc
->Field
.AccessLength
= Info
->AccessLength
;
503 if (Info
->ConnectionNode
)
505 SecondDesc
= Info
->ConnectionNode
->Object
;
506 if (!(SecondDesc
->Common
.Flags
& AOPOBJ_DATA_VALID
))
508 Status
= AcpiDsGetBufferArguments (SecondDesc
);
509 if (ACPI_FAILURE (Status
))
511 AcpiUtDeleteObjectDesc (ObjDesc
);
512 return_ACPI_STATUS (Status
);
516 ObjDesc
->Field
.ResourceBuffer
=
517 SecondDesc
->Buffer
.Pointer
;
518 ObjDesc
->Field
.ResourceLength
=
519 (UINT16
) SecondDesc
->Buffer
.Length
;
521 else if (Info
->ResourceBuffer
)
523 ObjDesc
->Field
.ResourceBuffer
= Info
->ResourceBuffer
;
524 ObjDesc
->Field
.ResourceLength
= Info
->ResourceLength
;
527 ObjDesc
->Field
.PinNumberIndex
= Info
->PinNumberIndex
;
529 /* Allow full data read from EC address space */
531 if ((ObjDesc
->Field
.RegionObj
->Region
.SpaceId
== ACPI_ADR_SPACE_EC
) &&
532 (ObjDesc
->CommonField
.BitLength
> 8))
534 AccessByteWidth
= ACPI_ROUND_BITS_UP_TO_BYTES (
535 ObjDesc
->CommonField
.BitLength
);
537 /* Maximum byte width supported is 255 */
539 if (AccessByteWidth
< 256)
541 ObjDesc
->CommonField
.AccessByteWidth
=
542 (UINT8
) AccessByteWidth
;
546 /* An additional reference for the container */
548 AcpiUtAddReference (ObjDesc
->Field
.RegionObj
);
550 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
551 "RegionField: BitOff %X, Off %X, Gran %X, Region %p\n",
552 ObjDesc
->Field
.StartFieldBitOffset
,
553 ObjDesc
->Field
.BaseByteOffset
,
554 ObjDesc
->Field
.AccessByteWidth
,
555 ObjDesc
->Field
.RegionObj
));
558 case ACPI_TYPE_LOCAL_BANK_FIELD
:
560 ObjDesc
->BankField
.Value
= Info
->BankValue
;
561 ObjDesc
->BankField
.RegionObj
=
562 AcpiNsGetAttachedObject (Info
->RegionNode
);
563 ObjDesc
->BankField
.BankObj
=
564 AcpiNsGetAttachedObject (Info
->RegisterNode
);
566 /* An additional reference for the attached objects */
568 AcpiUtAddReference (ObjDesc
->BankField
.RegionObj
);
569 AcpiUtAddReference (ObjDesc
->BankField
.BankObj
);
571 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
572 "Bank Field: BitOff %X, Off %X, Gran %X, Region %p, BankReg %p\n",
573 ObjDesc
->BankField
.StartFieldBitOffset
,
574 ObjDesc
->BankField
.BaseByteOffset
,
575 ObjDesc
->Field
.AccessByteWidth
,
576 ObjDesc
->BankField
.RegionObj
,
577 ObjDesc
->BankField
.BankObj
));
580 * Remember location in AML stream of the field unit
581 * opcode and operands -- since the BankValue
582 * operands must be evaluated.
584 SecondDesc
= ObjDesc
->Common
.NextObject
;
585 SecondDesc
->Extra
.AmlStart
= ACPI_CAST_PTR (ACPI_PARSE_OBJECT
,
586 Info
->DataRegisterNode
)->Named
.Data
;
587 SecondDesc
->Extra
.AmlLength
= ACPI_CAST_PTR (ACPI_PARSE_OBJECT
,
588 Info
->DataRegisterNode
)->Named
.Length
;
592 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
594 /* Get the Index and Data registers */
596 ObjDesc
->IndexField
.IndexObj
=
597 AcpiNsGetAttachedObject (Info
->RegisterNode
);
598 ObjDesc
->IndexField
.DataObj
=
599 AcpiNsGetAttachedObject (Info
->DataRegisterNode
);
601 if (!ObjDesc
->IndexField
.DataObj
|| !ObjDesc
->IndexField
.IndexObj
)
603 ACPI_ERROR ((AE_INFO
, "Null Index Object during field prep"));
604 AcpiUtDeleteObjectDesc (ObjDesc
);
605 return_ACPI_STATUS (AE_AML_INTERNAL
);
608 /* An additional reference for the attached objects */
610 AcpiUtAddReference (ObjDesc
->IndexField
.DataObj
);
611 AcpiUtAddReference (ObjDesc
->IndexField
.IndexObj
);
614 * April 2006: Changed to match MS behavior
616 * The value written to the Index register is the byte offset of the
617 * target field in units of the granularity of the IndexField
619 * Previously, the value was calculated as an index in terms of the
620 * width of the Data register, as below:
622 * ObjDesc->IndexField.Value = (UINT32)
623 * (Info->FieldBitPosition / ACPI_MUL_8 (
624 * ObjDesc->Field.AccessByteWidth));
626 * February 2006: Tried value as a byte offset:
627 * ObjDesc->IndexField.Value = (UINT32)
628 * ACPI_DIV_8 (Info->FieldBitPosition);
630 ObjDesc
->IndexField
.Value
= (UINT32
) ACPI_ROUND_DOWN (
631 ACPI_DIV_8 (Info
->FieldBitPosition
),
632 ObjDesc
->IndexField
.AccessByteWidth
);
634 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
635 "IndexField: BitOff %X, Off %X, Value %X, "
636 "Gran %X, Index %p, Data %p\n",
637 ObjDesc
->IndexField
.StartFieldBitOffset
,
638 ObjDesc
->IndexField
.BaseByteOffset
,
639 ObjDesc
->IndexField
.Value
,
640 ObjDesc
->Field
.AccessByteWidth
,
641 ObjDesc
->IndexField
.IndexObj
,
642 ObjDesc
->IndexField
.DataObj
));
647 /* No other types should get here */
653 * Store the constructed descriptor (ObjDesc) into the parent Node,
654 * preserving the current type of that NamedObj.
656 Status
= AcpiNsAttachObject (
657 Info
->FieldNode
, ObjDesc
, AcpiNsGetType (Info
->FieldNode
));
659 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD
,
660 "Set NamedObj %p [%4.4s], ObjDesc %p\n",
661 Info
->FieldNode
, AcpiUtGetNodeName (Info
->FieldNode
), ObjDesc
));
663 /* Remove local reference to the object */
665 AcpiUtRemoveReference (ObjDesc
);
666 return_ACPI_STATUS (Status
);