Indentation fix, cleanup.
[AROS.git] / arch / all-pc / acpica / source / components / executer / exprep.c
blobc6292241db1914f805aca8634db2d7dc032fd9b3
1 /******************************************************************************
3 * Module Name: exprep - ACPI AML (p-code) execution - field prep utilities
5 *****************************************************************************/
7 /*
8 * Copyright (C) 2000 - 2013, Intel Corp.
9 * All rights reserved.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
30 * NO WARRANTY
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.
44 #define __EXPREP_C__
46 #include "acpi.h"
47 #include "accommon.h"
48 #include "acinterp.h"
49 #include "amlcode.h"
50 #include "acnamesp.h"
51 #include "acdispat.h"
54 #define _COMPONENT ACPI_EXECUTER
55 ACPI_MODULE_NAME ("exprep")
57 /* Local prototypes */
59 static UINT32
60 AcpiExDecodeFieldAccess (
61 ACPI_OPERAND_OBJECT *ObjDesc,
62 UINT8 FieldFlags,
63 UINT32 *ReturnByteAlignment);
66 #ifdef ACPI_UNDER_DEVELOPMENT
68 static UINT32
69 AcpiExGenerateAccess (
70 UINT32 FieldBitOffset,
71 UINT32 FieldBitLength,
72 UINT32 RegionLength);
74 /*******************************************************************************
76 * FUNCTION: AcpiExGenerateAccess
78 * PARAMETERS: FieldBitOffset - Start of field within parent region/buffer
79 * FieldBitLength - Length of field in bits
80 * RegionLength - Length of parent in bytes
82 * RETURN: Field granularity (8, 16, 32 or 64) and
83 * ByteAlignment (1, 2, 3, or 4)
85 * DESCRIPTION: Generate an optimal access width for fields defined with the
86 * AnyAcc keyword.
88 * NOTE: Need to have the RegionLength in order to check for boundary
89 * conditions (end-of-region). However, the RegionLength is a deferred
90 * operation. Therefore, to complete this implementation, the generation
91 * of this access width must be deferred until the region length has
92 * been evaluated.
94 ******************************************************************************/
96 static UINT32
97 AcpiExGenerateAccess (
98 UINT32 FieldBitOffset,
99 UINT32 FieldBitLength,
100 UINT32 RegionLength)
102 UINT32 FieldByteLength;
103 UINT32 FieldByteOffset;
104 UINT32 FieldByteEndOffset;
105 UINT32 AccessByteWidth;
106 UINT32 FieldStartOffset;
107 UINT32 FieldEndOffset;
108 UINT32 MinimumAccessWidth = 0xFFFFFFFF;
109 UINT32 MinimumAccesses = 0xFFFFFFFF;
110 UINT32 Accesses;
113 ACPI_FUNCTION_TRACE (ExGenerateAccess);
116 /* Round Field start offset and length to "minimal" byte boundaries */
118 FieldByteOffset = ACPI_DIV_8 (ACPI_ROUND_DOWN (FieldBitOffset, 8));
119 FieldByteEndOffset = ACPI_DIV_8 (ACPI_ROUND_UP (FieldBitLength +
120 FieldBitOffset, 8));
121 FieldByteLength = FieldByteEndOffset - FieldByteOffset;
123 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
124 "Bit length %u, Bit offset %u\n",
125 FieldBitLength, FieldBitOffset));
127 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
128 "Byte Length %u, Byte Offset %u, End Offset %u\n",
129 FieldByteLength, FieldByteOffset, FieldByteEndOffset));
132 * Iterative search for the maximum access width that is both aligned
133 * and does not go beyond the end of the region
135 * Start at ByteAcc and work upwards to QwordAcc max. (1,2,4,8 bytes)
137 for (AccessByteWidth = 1; AccessByteWidth <= 8; AccessByteWidth <<= 1)
140 * 1) Round end offset up to next access boundary and make sure that
141 * this does not go beyond the end of the parent region.
142 * 2) When the Access width is greater than the FieldByteLength, we
143 * are done. (This does not optimize for the perfectly aligned
144 * case yet).
146 if (ACPI_ROUND_UP (FieldByteEndOffset, AccessByteWidth) <= RegionLength)
148 FieldStartOffset =
149 ACPI_ROUND_DOWN (FieldByteOffset, AccessByteWidth) /
150 AccessByteWidth;
152 FieldEndOffset =
153 ACPI_ROUND_UP ((FieldByteLength + FieldByteOffset),
154 AccessByteWidth) / AccessByteWidth;
156 Accesses = FieldEndOffset - FieldStartOffset;
158 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
159 "AccessWidth %u end is within region\n", AccessByteWidth));
161 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
162 "Field Start %u, Field End %u -- requires %u accesses\n",
163 FieldStartOffset, FieldEndOffset, Accesses));
165 /* Single access is optimal */
167 if (Accesses <= 1)
169 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
170 "Entire field can be accessed with one operation of size %u\n",
171 AccessByteWidth));
172 return_VALUE (AccessByteWidth);
176 * Fits in the region, but requires more than one read/write.
177 * try the next wider access on next iteration
179 if (Accesses < MinimumAccesses)
181 MinimumAccesses = Accesses;
182 MinimumAccessWidth = AccessByteWidth;
185 else
187 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
188 "AccessWidth %u end is NOT within region\n", AccessByteWidth));
189 if (AccessByteWidth == 1)
191 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
192 "Field goes beyond end-of-region!\n"));
194 /* Field does not fit in the region at all */
196 return_VALUE (0);
200 * This width goes beyond the end-of-region, back off to
201 * previous access
203 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
204 "Backing off to previous optimal access width of %u\n",
205 MinimumAccessWidth));
206 return_VALUE (MinimumAccessWidth);
211 * Could not read/write field with one operation,
212 * just use max access width
214 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
215 "Cannot access field in one operation, using width 8\n"));
216 return_VALUE (8);
218 #endif /* ACPI_UNDER_DEVELOPMENT */
221 /*******************************************************************************
223 * FUNCTION: AcpiExDecodeFieldAccess
225 * PARAMETERS: ObjDesc - Field object
226 * FieldFlags - Encoded fieldflags (contains access bits)
227 * ReturnByteAlignment - Where the byte alignment is returned
229 * RETURN: Field granularity (8, 16, 32 or 64) and
230 * ByteAlignment (1, 2, 3, or 4)
232 * DESCRIPTION: Decode the AccessType bits of a field definition.
234 ******************************************************************************/
236 static UINT32
237 AcpiExDecodeFieldAccess (
238 ACPI_OPERAND_OBJECT *ObjDesc,
239 UINT8 FieldFlags,
240 UINT32 *ReturnByteAlignment)
242 UINT32 Access;
243 UINT32 ByteAlignment;
244 UINT32 BitLength;
247 ACPI_FUNCTION_TRACE (ExDecodeFieldAccess);
250 Access = (FieldFlags & AML_FIELD_ACCESS_TYPE_MASK);
252 switch (Access)
254 case AML_FIELD_ACCESS_ANY:
256 #ifdef ACPI_UNDER_DEVELOPMENT
257 ByteAlignment =
258 AcpiExGenerateAccess (ObjDesc->CommonField.StartFieldBitOffset,
259 ObjDesc->CommonField.BitLength,
260 0xFFFFFFFF /* Temp until we pass RegionLength as parameter */);
261 BitLength = ByteAlignment * 8;
262 #endif
264 ByteAlignment = 1;
265 BitLength = 8;
266 break;
268 case AML_FIELD_ACCESS_BYTE:
269 case AML_FIELD_ACCESS_BUFFER: /* ACPI 2.0 (SMBus Buffer) */
271 ByteAlignment = 1;
272 BitLength = 8;
273 break;
275 case AML_FIELD_ACCESS_WORD:
277 ByteAlignment = 2;
278 BitLength = 16;
279 break;
281 case AML_FIELD_ACCESS_DWORD:
283 ByteAlignment = 4;
284 BitLength = 32;
285 break;
287 case AML_FIELD_ACCESS_QWORD: /* ACPI 2.0 */
289 ByteAlignment = 8;
290 BitLength = 64;
291 break;
293 default:
295 /* Invalid field access type */
297 ACPI_ERROR ((AE_INFO,
298 "Unknown field access type 0x%X",
299 Access));
300 return_UINT32 (0);
303 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
306 * BufferField access can be on any byte boundary, so the
307 * ByteAlignment is always 1 byte -- regardless of any ByteAlignment
308 * implied by the field access type.
310 ByteAlignment = 1;
313 *ReturnByteAlignment = ByteAlignment;
314 return_UINT32 (BitLength);
318 /*******************************************************************************
320 * FUNCTION: AcpiExPrepCommonFieldObject
322 * PARAMETERS: ObjDesc - The field object
323 * FieldFlags - Access, LockRule, and UpdateRule.
324 * The format of a FieldFlag is described
325 * in the ACPI specification
326 * FieldAttribute - Special attributes (not used)
327 * FieldBitPosition - Field start position
328 * FieldBitLength - Field length in number of bits
330 * RETURN: Status
332 * DESCRIPTION: Initialize the areas of the field object that are common
333 * to the various types of fields. Note: This is very "sensitive"
334 * code because we are solving the general case for field
335 * alignment.
337 ******************************************************************************/
339 ACPI_STATUS
340 AcpiExPrepCommonFieldObject (
341 ACPI_OPERAND_OBJECT *ObjDesc,
342 UINT8 FieldFlags,
343 UINT8 FieldAttribute,
344 UINT32 FieldBitPosition,
345 UINT32 FieldBitLength)
347 UINT32 AccessBitWidth;
348 UINT32 ByteAlignment;
349 UINT32 NearestByteAddress;
352 ACPI_FUNCTION_TRACE (ExPrepCommonFieldObject);
356 * Note: the structure being initialized is the
357 * ACPI_COMMON_FIELD_INFO; No structure fields outside of the common
358 * area are initialized by this procedure.
360 ObjDesc->CommonField.FieldFlags = FieldFlags;
361 ObjDesc->CommonField.Attribute = FieldAttribute;
362 ObjDesc->CommonField.BitLength = FieldBitLength;
365 * Decode the access type so we can compute offsets. The access type gives
366 * two pieces of information - the width of each field access and the
367 * necessary ByteAlignment (address granularity) of the access.
369 * For AnyAcc, the AccessBitWidth is the largest width that is both
370 * necessary and possible in an attempt to access the whole field in one
371 * I/O operation. However, for AnyAcc, the ByteAlignment is always one
372 * byte.
374 * For all Buffer Fields, the ByteAlignment is always one byte.
376 * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is
377 * the same (equivalent) as the ByteAlignment.
379 AccessBitWidth = AcpiExDecodeFieldAccess (ObjDesc, FieldFlags,
380 &ByteAlignment);
381 if (!AccessBitWidth)
383 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
386 /* Setup width (access granularity) fields (values are: 1, 2, 4, 8) */
388 ObjDesc->CommonField.AccessByteWidth = (UINT8)
389 ACPI_DIV_8 (AccessBitWidth);
392 * BaseByteOffset is the address of the start of the field within the
393 * region. It is the byte address of the first *datum* (field-width data
394 * unit) of the field. (i.e., the first datum that contains at least the
395 * first *bit* of the field.)
397 * Note: ByteAlignment is always either equal to the AccessBitWidth or 8
398 * (Byte access), and it defines the addressing granularity of the parent
399 * region or buffer.
401 NearestByteAddress =
402 ACPI_ROUND_BITS_DOWN_TO_BYTES (FieldBitPosition);
403 ObjDesc->CommonField.BaseByteOffset = (UINT32)
404 ACPI_ROUND_DOWN (NearestByteAddress, ByteAlignment);
407 * StartFieldBitOffset is the offset of the first bit of the field within
408 * a field datum.
410 ObjDesc->CommonField.StartFieldBitOffset = (UINT8)
411 (FieldBitPosition - ACPI_MUL_8 (ObjDesc->CommonField.BaseByteOffset));
413 return_ACPI_STATUS (AE_OK);
417 /*******************************************************************************
419 * FUNCTION: AcpiExPrepFieldValue
421 * PARAMETERS: Info - Contains all field creation info
423 * RETURN: Status
425 * DESCRIPTION: Construct an object of type ACPI_OPERAND_OBJECT with a
426 * subtype of DefField and connect it to the parent Node.
428 ******************************************************************************/
430 ACPI_STATUS
431 AcpiExPrepFieldValue (
432 ACPI_CREATE_FIELD_INFO *Info)
434 ACPI_OPERAND_OBJECT *ObjDesc;
435 ACPI_OPERAND_OBJECT *SecondDesc = NULL;
436 ACPI_STATUS Status;
437 UINT32 AccessByteWidth;
438 UINT32 Type;
441 ACPI_FUNCTION_TRACE (ExPrepFieldValue);
444 /* Parameter validation */
446 if (Info->FieldType != ACPI_TYPE_LOCAL_INDEX_FIELD)
448 if (!Info->RegionNode)
450 ACPI_ERROR ((AE_INFO, "Null RegionNode"));
451 return_ACPI_STATUS (AE_AML_NO_OPERAND);
454 Type = AcpiNsGetType (Info->RegionNode);
455 if (Type != ACPI_TYPE_REGION)
457 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
458 Type, AcpiUtGetTypeName (Type)));
460 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
464 /* Allocate a new field object */
466 ObjDesc = AcpiUtCreateInternalObject (Info->FieldType);
467 if (!ObjDesc)
469 return_ACPI_STATUS (AE_NO_MEMORY);
472 /* Initialize areas of the object that are common to all fields */
474 ObjDesc->CommonField.Node = Info->FieldNode;
475 Status = AcpiExPrepCommonFieldObject (ObjDesc,
476 Info->FieldFlags, Info->Attribute,
477 Info->FieldBitPosition, Info->FieldBitLength);
478 if (ACPI_FAILURE (Status))
480 AcpiUtDeleteObjectDesc (ObjDesc);
481 return_ACPI_STATUS (Status);
484 /* Initialize areas of the object that are specific to the field type */
486 switch (Info->FieldType)
488 case ACPI_TYPE_LOCAL_REGION_FIELD:
490 ObjDesc->Field.RegionObj = AcpiNsGetAttachedObject (Info->RegionNode);
492 /* Fields specific to GenericSerialBus fields */
494 ObjDesc->Field.AccessLength = Info->AccessLength;
496 if (Info->ConnectionNode)
498 SecondDesc = Info->ConnectionNode->Object;
499 if (!(SecondDesc->Common.Flags & AOPOBJ_DATA_VALID))
501 Status = AcpiDsGetBufferArguments (SecondDesc);
502 if (ACPI_FAILURE (Status))
504 AcpiUtDeleteObjectDesc (ObjDesc);
505 return_ACPI_STATUS (Status);
509 ObjDesc->Field.ResourceBuffer = SecondDesc->Buffer.Pointer;
510 ObjDesc->Field.ResourceLength = (UINT16) SecondDesc->Buffer.Length;
512 else if (Info->ResourceBuffer)
514 ObjDesc->Field.ResourceBuffer = Info->ResourceBuffer;
515 ObjDesc->Field.ResourceLength = Info->ResourceLength;
518 /* Allow full data read from EC address space */
520 if ((ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_EC) &&
521 (ObjDesc->CommonField.BitLength > 8))
523 AccessByteWidth = ACPI_ROUND_BITS_UP_TO_BYTES (
524 ObjDesc->CommonField.BitLength);
526 /* Maximum byte width supported is 255 */
528 if (AccessByteWidth < 256)
530 ObjDesc->CommonField.AccessByteWidth = (UINT8) AccessByteWidth;
534 /* An additional reference for the container */
536 AcpiUtAddReference (ObjDesc->Field.RegionObj);
538 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
539 "RegionField: BitOff %X, Off %X, Gran %X, Region %p\n",
540 ObjDesc->Field.StartFieldBitOffset, ObjDesc->Field.BaseByteOffset,
541 ObjDesc->Field.AccessByteWidth, ObjDesc->Field.RegionObj));
542 break;
544 case ACPI_TYPE_LOCAL_BANK_FIELD:
546 ObjDesc->BankField.Value = Info->BankValue;
547 ObjDesc->BankField.RegionObj =
548 AcpiNsGetAttachedObject (Info->RegionNode);
549 ObjDesc->BankField.BankObj =
550 AcpiNsGetAttachedObject (Info->RegisterNode);
552 /* An additional reference for the attached objects */
554 AcpiUtAddReference (ObjDesc->BankField.RegionObj);
555 AcpiUtAddReference (ObjDesc->BankField.BankObj);
557 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
558 "Bank Field: BitOff %X, Off %X, Gran %X, Region %p, BankReg %p\n",
559 ObjDesc->BankField.StartFieldBitOffset,
560 ObjDesc->BankField.BaseByteOffset,
561 ObjDesc->Field.AccessByteWidth,
562 ObjDesc->BankField.RegionObj,
563 ObjDesc->BankField.BankObj));
566 * Remember location in AML stream of the field unit
567 * opcode and operands -- since the BankValue
568 * operands must be evaluated.
570 SecondDesc = ObjDesc->Common.NextObject;
571 SecondDesc->Extra.AmlStart = ACPI_CAST_PTR (ACPI_PARSE_OBJECT,
572 Info->DataRegisterNode)->Named.Data;
573 SecondDesc->Extra.AmlLength = ACPI_CAST_PTR (ACPI_PARSE_OBJECT,
574 Info->DataRegisterNode)->Named.Length;
576 break;
578 case ACPI_TYPE_LOCAL_INDEX_FIELD:
580 /* Get the Index and Data registers */
582 ObjDesc->IndexField.IndexObj =
583 AcpiNsGetAttachedObject (Info->RegisterNode);
584 ObjDesc->IndexField.DataObj =
585 AcpiNsGetAttachedObject (Info->DataRegisterNode);
587 if (!ObjDesc->IndexField.DataObj || !ObjDesc->IndexField.IndexObj)
589 ACPI_ERROR ((AE_INFO, "Null Index Object during field prep"));
590 AcpiUtDeleteObjectDesc (ObjDesc);
591 return_ACPI_STATUS (AE_AML_INTERNAL);
594 /* An additional reference for the attached objects */
596 AcpiUtAddReference (ObjDesc->IndexField.DataObj);
597 AcpiUtAddReference (ObjDesc->IndexField.IndexObj);
600 * April 2006: Changed to match MS behavior
602 * The value written to the Index register is the byte offset of the
603 * target field in units of the granularity of the IndexField
605 * Previously, the value was calculated as an index in terms of the
606 * width of the Data register, as below:
608 * ObjDesc->IndexField.Value = (UINT32)
609 * (Info->FieldBitPosition / ACPI_MUL_8 (
610 * ObjDesc->Field.AccessByteWidth));
612 * February 2006: Tried value as a byte offset:
613 * ObjDesc->IndexField.Value = (UINT32)
614 * ACPI_DIV_8 (Info->FieldBitPosition);
616 ObjDesc->IndexField.Value = (UINT32) ACPI_ROUND_DOWN (
617 ACPI_DIV_8 (Info->FieldBitPosition),
618 ObjDesc->IndexField.AccessByteWidth);
620 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
621 "IndexField: BitOff %X, Off %X, Value %X, Gran %X, Index %p, Data %p\n",
622 ObjDesc->IndexField.StartFieldBitOffset,
623 ObjDesc->IndexField.BaseByteOffset,
624 ObjDesc->IndexField.Value,
625 ObjDesc->Field.AccessByteWidth,
626 ObjDesc->IndexField.IndexObj,
627 ObjDesc->IndexField.DataObj));
628 break;
630 default:
632 /* No other types should get here */
634 break;
638 * Store the constructed descriptor (ObjDesc) into the parent Node,
639 * preserving the current type of that NamedObj.
641 Status = AcpiNsAttachObject (Info->FieldNode, ObjDesc,
642 AcpiNsGetType (Info->FieldNode));
644 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Set NamedObj %p [%4.4s], ObjDesc %p\n",
645 Info->FieldNode, AcpiUtGetNodeName (Info->FieldNode), ObjDesc));
647 /* Remove local reference to the object */
649 AcpiUtRemoveReference (ObjDesc);
650 return_ACPI_STATUS (Status);