1 2 /****************************************************************************** 3 * 4 * Module Name: exprep - ACPI AML (p-code) execution - field prep utilities 5 * 6 *****************************************************************************/ 7 8 /* 9 * Copyright (C) 2000 - 2011, Intel Corp. 10 * All rights reserved. 11 * 12 * Redistribution and use in source and binary forms, with or without 13 * modification, are permitted provided that the following conditions 14 * are met: 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. 26 * 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. 30 * 31 * NO WARRANTY 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. 43 */ 44 45 #define __EXPREP_C__ 46 47 #include "acpi.h" 48 #include "accommon.h" 49 #include "acinterp.h" 50 #include "amlcode.h" 51 #include "acnamesp.h" 52 53 54 #define _COMPONENT ACPI_EXECUTER 55 ACPI_MODULE_NAME ("exprep") 56 57 /* Local prototypes */ 58 59 static UINT32 60 AcpiExDecodeFieldAccess ( 61 ACPI_OPERAND_OBJECT *ObjDesc, 62 UINT8 FieldFlags, 63 UINT32 *ReturnByteAlignment); 64 65 66 #ifdef ACPI_UNDER_DEVELOPMENT 67 68 static UINT32 69 AcpiExGenerateAccess ( 70 UINT32 FieldBitOffset, 71 UINT32 FieldBitLength, 72 UINT32 RegionLength); 73 74 /******************************************************************************* 75 * 76 * FUNCTION: AcpiExGenerateAccess 77 * 78 * PARAMETERS: FieldBitOffset - Start of field within parent region/buffer 79 * FieldBitLength - Length of field in bits 80 * RegionLength - Length of parent in bytes 81 * 82 * RETURN: Field granularity (8, 16, 32 or 64) and 83 * ByteAlignment (1, 2, 3, or 4) 84 * 85 * DESCRIPTION: Generate an optimal access width for fields defined with the 86 * AnyAcc keyword. 87 * 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. 93 * 94 ******************************************************************************/ 95 96 static UINT32 97 AcpiExGenerateAccess ( 98 UINT32 FieldBitOffset, 99 UINT32 FieldBitLength, 100 UINT32 RegionLength) 101 { 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; 111 112 113 ACPI_FUNCTION_TRACE (ExGenerateAccess); 114 115 116 /* Round Field start offset and length to "minimal" byte boundaries */ 117 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; 122 123 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 124 "Bit length %u, Bit offset %u\n", 125 FieldBitLength, FieldBitOffset)); 126 127 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 128 "Byte Length %u, Byte Offset %u, End Offset %u\n", 129 FieldByteLength, FieldByteOffset, FieldByteEndOffset)); 130 131 /* 132 * Iterative search for the maximum access width that is both aligned 133 * and does not go beyond the end of the region 134 * 135 * Start at ByteAcc and work upwards to QwordAcc max. (1,2,4,8 bytes) 136 */ 137 for (AccessByteWidth = 1; AccessByteWidth <= 8; AccessByteWidth <<= 1) 138 { 139 /* 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). 145 */ 146 if (ACPI_ROUND_UP (FieldByteEndOffset, AccessByteWidth) <= RegionLength) 147 { 148 FieldStartOffset = 149 ACPI_ROUND_DOWN (FieldByteOffset, AccessByteWidth) / 150 AccessByteWidth; 151 152 FieldEndOffset = 153 ACPI_ROUND_UP ((FieldByteLength + FieldByteOffset), 154 AccessByteWidth) / AccessByteWidth; 155 156 Accesses = FieldEndOffset - FieldStartOffset; 157 158 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 159 "AccessWidth %u end is within region\n", AccessByteWidth)); 160 161 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 162 "Field Start %u, Field End %u -- requires %u accesses\n", 163 FieldStartOffset, FieldEndOffset, Accesses)); 164 165 /* Single access is optimal */ 166 167 if (Accesses <= 1) 168 { 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); 173 } 174 175 /* 176 * Fits in the region, but requires more than one read/write. 177 * try the next wider access on next iteration 178 */ 179 if (Accesses < MinimumAccesses) 180 { 181 MinimumAccesses = Accesses; 182 MinimumAccessWidth = AccessByteWidth; 183 } 184 } 185 else 186 { 187 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 188 "AccessWidth %u end is NOT within region\n", AccessByteWidth)); 189 if (AccessByteWidth == 1) 190 { 191 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 192 "Field goes beyond end-of-region!\n")); 193 194 /* Field does not fit in the region at all */ 195 196 return_VALUE (0); 197 } 198 199 /* 200 * This width goes beyond the end-of-region, back off to 201 * previous access 202 */ 203 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 204 "Backing off to previous optimal access width of %u\n", 205 MinimumAccessWidth)); 206 return_VALUE (MinimumAccessWidth); 207 } 208 } 209 210 /* 211 * Could not read/write field with one operation, 212 * just use max access width 213 */ 214 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 215 "Cannot access field in one operation, using width 8\n")); 216 return_VALUE (8); 217 } 218 #endif /* ACPI_UNDER_DEVELOPMENT */ 219 220 221 /******************************************************************************* 222 * 223 * FUNCTION: AcpiExDecodeFieldAccess 224 * 225 * PARAMETERS: ObjDesc - Field object 226 * FieldFlags - Encoded fieldflags (contains access bits) 227 * ReturnByteAlignment - Where the byte alignment is returned 228 * 229 * RETURN: Field granularity (8, 16, 32 or 64) and 230 * ByteAlignment (1, 2, 3, or 4) 231 * 232 * DESCRIPTION: Decode the AccessType bits of a field definition. 233 * 234 ******************************************************************************/ 235 236 static UINT32 237 AcpiExDecodeFieldAccess ( 238 ACPI_OPERAND_OBJECT *ObjDesc, 239 UINT8 FieldFlags, 240 UINT32 *ReturnByteAlignment) 241 { 242 UINT32 Access; 243 UINT32 ByteAlignment; 244 UINT32 BitLength; 245 246 247 ACPI_FUNCTION_TRACE (ExDecodeFieldAccess); 248 249 250 Access = (FieldFlags & AML_FIELD_ACCESS_TYPE_MASK); 251 252 switch (Access) 253 { 254 case AML_FIELD_ACCESS_ANY: 255 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 263 264 ByteAlignment = 1; 265 BitLength = 8; 266 break; 267 268 case AML_FIELD_ACCESS_BYTE: 269 case AML_FIELD_ACCESS_BUFFER: /* ACPI 2.0 (SMBus Buffer) */ 270 ByteAlignment = 1; 271 BitLength = 8; 272 break; 273 274 case AML_FIELD_ACCESS_WORD: 275 ByteAlignment = 2; 276 BitLength = 16; 277 break; 278 279 case AML_FIELD_ACCESS_DWORD: 280 ByteAlignment = 4; 281 BitLength = 32; 282 break; 283 284 case AML_FIELD_ACCESS_QWORD: /* ACPI 2.0 */ 285 ByteAlignment = 8; 286 BitLength = 64; 287 break; 288 289 default: 290 /* Invalid field access type */ 291 292 ACPI_ERROR ((AE_INFO, 293 "Unknown field access type 0x%X", 294 Access)); 295 return_UINT32 (0); 296 } 297 298 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD) 299 { 300 /* 301 * BufferField access can be on any byte boundary, so the 302 * ByteAlignment is always 1 byte -- regardless of any ByteAlignment 303 * implied by the field access type. 304 */ 305 ByteAlignment = 1; 306 } 307 308 *ReturnByteAlignment = ByteAlignment; 309 return_UINT32 (BitLength); 310 } 311 312 313 /******************************************************************************* 314 * 315 * FUNCTION: AcpiExPrepCommonFieldObject 316 * 317 * PARAMETERS: ObjDesc - The field object 318 * FieldFlags - Access, LockRule, and UpdateRule. 319 * The format of a FieldFlag is described 320 * in the ACPI specification 321 * FieldAttribute - Special attributes (not used) 322 * FieldBitPosition - Field start position 323 * FieldBitLength - Field length in number of bits 324 * 325 * RETURN: Status 326 * 327 * DESCRIPTION: Initialize the areas of the field object that are common 328 * to the various types of fields. Note: This is very "sensitive" 329 * code because we are solving the general case for field 330 * alignment. 331 * 332 ******************************************************************************/ 333 334 ACPI_STATUS 335 AcpiExPrepCommonFieldObject ( 336 ACPI_OPERAND_OBJECT *ObjDesc, 337 UINT8 FieldFlags, 338 UINT8 FieldAttribute, 339 UINT32 FieldBitPosition, 340 UINT32 FieldBitLength) 341 { 342 UINT32 AccessBitWidth; 343 UINT32 ByteAlignment; 344 UINT32 NearestByteAddress; 345 346 347 ACPI_FUNCTION_TRACE (ExPrepCommonFieldObject); 348 349 350 /* 351 * Note: the structure being initialized is the 352 * ACPI_COMMON_FIELD_INFO; No structure fields outside of the common 353 * area are initialized by this procedure. 354 */ 355 ObjDesc->CommonField.FieldFlags = FieldFlags; 356 ObjDesc->CommonField.Attribute = FieldAttribute; 357 ObjDesc->CommonField.BitLength = FieldBitLength; 358 359 /* 360 * Decode the access type so we can compute offsets. The access type gives 361 * two pieces of information - the width of each field access and the 362 * necessary ByteAlignment (address granularity) of the access. 363 * 364 * For AnyAcc, the AccessBitWidth is the largest width that is both 365 * necessary and possible in an attempt to access the whole field in one 366 * I/O operation. However, for AnyAcc, the ByteAlignment is always one 367 * byte. 368 * 369 * For all Buffer Fields, the ByteAlignment is always one byte. 370 * 371 * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is 372 * the same (equivalent) as the ByteAlignment. 373 */ 374 AccessBitWidth = AcpiExDecodeFieldAccess (ObjDesc, FieldFlags, 375 &ByteAlignment); 376 if (!AccessBitWidth) 377 { 378 return_ACPI_STATUS (AE_AML_OPERAND_VALUE); 379 } 380 381 /* Setup width (access granularity) fields (values are: 1, 2, 4, 8) */ 382 383 ObjDesc->CommonField.AccessByteWidth = (UINT8) 384 ACPI_DIV_8 (AccessBitWidth); 385 386 /* 387 * BaseByteOffset is the address of the start of the field within the 388 * region. It is the byte address of the first *datum* (field-width data 389 * unit) of the field. (i.e., the first datum that contains at least the 390 * first *bit* of the field.) 391 * 392 * Note: ByteAlignment is always either equal to the AccessBitWidth or 8 393 * (Byte access), and it defines the addressing granularity of the parent 394 * region or buffer. 395 */ 396 NearestByteAddress = 397 ACPI_ROUND_BITS_DOWN_TO_BYTES (FieldBitPosition); 398 ObjDesc->CommonField.BaseByteOffset = (UINT32) 399 ACPI_ROUND_DOWN (NearestByteAddress, ByteAlignment); 400 401 /* 402 * StartFieldBitOffset is the offset of the first bit of the field within 403 * a field datum. 404 */ 405 ObjDesc->CommonField.StartFieldBitOffset = (UINT8) 406 (FieldBitPosition - ACPI_MUL_8 (ObjDesc->CommonField.BaseByteOffset)); 407 408 return_ACPI_STATUS (AE_OK); 409 } 410 411 412 /******************************************************************************* 413 * 414 * FUNCTION: AcpiExPrepFieldValue 415 * 416 * PARAMETERS: Info - Contains all field creation info 417 * 418 * RETURN: Status 419 * 420 * DESCRIPTION: Construct an ACPI_OPERAND_OBJECT of type DefField and 421 * connect it to the parent Node. 422 * 423 ******************************************************************************/ 424 425 ACPI_STATUS 426 AcpiExPrepFieldValue ( 427 ACPI_CREATE_FIELD_INFO *Info) 428 { 429 ACPI_OPERAND_OBJECT *ObjDesc; 430 ACPI_OPERAND_OBJECT *SecondDesc = NULL; 431 ACPI_STATUS Status; 432 UINT32 AccessByteWidth; 433 UINT32 Type; 434 435 436 ACPI_FUNCTION_TRACE (ExPrepFieldValue); 437 438 439 /* Parameter validation */ 440 441 if (Info->FieldType != ACPI_TYPE_LOCAL_INDEX_FIELD) 442 { 443 if (!Info->RegionNode) 444 { 445 ACPI_ERROR ((AE_INFO, "Null RegionNode")); 446 return_ACPI_STATUS (AE_AML_NO_OPERAND); 447 } 448 449 Type = AcpiNsGetType (Info->RegionNode); 450 if (Type != ACPI_TYPE_REGION) 451 { 452 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)", 453 Type, AcpiUtGetTypeName (Type))); 454 455 return_ACPI_STATUS (AE_AML_OPERAND_TYPE); 456 } 457 } 458 459 /* Allocate a new field object */ 460 461 ObjDesc = AcpiUtCreateInternalObject (Info->FieldType); 462 if (!ObjDesc) 463 { 464 return_ACPI_STATUS (AE_NO_MEMORY); 465 } 466 467 /* Initialize areas of the object that are common to all fields */ 468 469 ObjDesc->CommonField.Node = Info->FieldNode; 470 Status = AcpiExPrepCommonFieldObject (ObjDesc, 471 Info->FieldFlags, Info->Attribute, 472 Info->FieldBitPosition, Info->FieldBitLength); 473 if (ACPI_FAILURE (Status)) 474 { 475 AcpiUtDeleteObjectDesc (ObjDesc); 476 return_ACPI_STATUS (Status); 477 } 478 479 /* Initialize areas of the object that are specific to the field type */ 480 481 switch (Info->FieldType) 482 { 483 case ACPI_TYPE_LOCAL_REGION_FIELD: 484 485 ObjDesc->Field.RegionObj = AcpiNsGetAttachedObject (Info->RegionNode); 486 487 /* Allow full data read from EC address space */ 488 489 if ((ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_EC) && 490 (ObjDesc->CommonField.BitLength > 8)) 491 { 492 AccessByteWidth = ACPI_ROUND_BITS_UP_TO_BYTES ( 493 ObjDesc->CommonField.BitLength); 494 495 /* Maximum byte width supported is 255 */ 496 497 if (AccessByteWidth < 256) 498 { 499 ObjDesc->CommonField.AccessByteWidth = (UINT8) AccessByteWidth; 500 } 501 } 502 503 /* An additional reference for the container */ 504 505 AcpiUtAddReference (ObjDesc->Field.RegionObj); 506 507 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 508 "RegionField: BitOff %X, Off %X, Gran %X, Region %p\n", 509 ObjDesc->Field.StartFieldBitOffset, ObjDesc->Field.BaseByteOffset, 510 ObjDesc->Field.AccessByteWidth, ObjDesc->Field.RegionObj)); 511 break; 512 513 514 case ACPI_TYPE_LOCAL_BANK_FIELD: 515 516 ObjDesc->BankField.Value = Info->BankValue; 517 ObjDesc->BankField.RegionObj = 518 AcpiNsGetAttachedObject (Info->RegionNode); 519 ObjDesc->BankField.BankObj = 520 AcpiNsGetAttachedObject (Info->RegisterNode); 521 522 /* An additional reference for the attached objects */ 523 524 AcpiUtAddReference (ObjDesc->BankField.RegionObj); 525 AcpiUtAddReference (ObjDesc->BankField.BankObj); 526 527 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 528 "Bank Field: BitOff %X, Off %X, Gran %X, Region %p, BankReg %p\n", 529 ObjDesc->BankField.StartFieldBitOffset, 530 ObjDesc->BankField.BaseByteOffset, 531 ObjDesc->Field.AccessByteWidth, 532 ObjDesc->BankField.RegionObj, 533 ObjDesc->BankField.BankObj)); 534 535 /* 536 * Remember location in AML stream of the field unit 537 * opcode and operands -- since the BankValue 538 * operands must be evaluated. 539 */ 540 SecondDesc = ObjDesc->Common.NextObject; 541 SecondDesc->Extra.AmlStart = ACPI_CAST_PTR (ACPI_PARSE_OBJECT, 542 Info->DataRegisterNode)->Named.Data; 543 SecondDesc->Extra.AmlLength = ACPI_CAST_PTR (ACPI_PARSE_OBJECT, 544 Info->DataRegisterNode)->Named.Length; 545 546 break; 547 548 549 case ACPI_TYPE_LOCAL_INDEX_FIELD: 550 551 /* Get the Index and Data registers */ 552 553 ObjDesc->IndexField.IndexObj = 554 AcpiNsGetAttachedObject (Info->RegisterNode); 555 ObjDesc->IndexField.DataObj = 556 AcpiNsGetAttachedObject (Info->DataRegisterNode); 557 558 if (!ObjDesc->IndexField.DataObj || !ObjDesc->IndexField.IndexObj) 559 { 560 ACPI_ERROR ((AE_INFO, "Null Index Object during field prep")); 561 AcpiUtDeleteObjectDesc (ObjDesc); 562 return_ACPI_STATUS (AE_AML_INTERNAL); 563 } 564 565 /* An additional reference for the attached objects */ 566 567 AcpiUtAddReference (ObjDesc->IndexField.DataObj); 568 AcpiUtAddReference (ObjDesc->IndexField.IndexObj); 569 570 /* 571 * April 2006: Changed to match MS behavior 572 * 573 * The value written to the Index register is the byte offset of the 574 * target field in units of the granularity of the IndexField 575 * 576 * Previously, the value was calculated as an index in terms of the 577 * width of the Data register, as below: 578 * 579 * ObjDesc->IndexField.Value = (UINT32) 580 * (Info->FieldBitPosition / ACPI_MUL_8 ( 581 * ObjDesc->Field.AccessByteWidth)); 582 * 583 * February 2006: Tried value as a byte offset: 584 * ObjDesc->IndexField.Value = (UINT32) 585 * ACPI_DIV_8 (Info->FieldBitPosition); 586 */ 587 ObjDesc->IndexField.Value = (UINT32) ACPI_ROUND_DOWN ( 588 ACPI_DIV_8 (Info->FieldBitPosition), 589 ObjDesc->IndexField.AccessByteWidth); 590 591 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, 592 "IndexField: BitOff %X, Off %X, Value %X, Gran %X, Index %p, Data %p\n", 593 ObjDesc->IndexField.StartFieldBitOffset, 594 ObjDesc->IndexField.BaseByteOffset, 595 ObjDesc->IndexField.Value, 596 ObjDesc->Field.AccessByteWidth, 597 ObjDesc->IndexField.IndexObj, 598 ObjDesc->IndexField.DataObj)); 599 break; 600 601 default: 602 /* No other types should get here */ 603 break; 604 } 605 606 /* 607 * Store the constructed descriptor (ObjDesc) into the parent Node, 608 * preserving the current type of that NamedObj. 609 */ 610 Status = AcpiNsAttachObject (Info->FieldNode, ObjDesc, 611 AcpiNsGetType (Info->FieldNode)); 612 613 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Set NamedObj %p [%4.4s], ObjDesc %p\n", 614 Info->FieldNode, AcpiUtGetNodeName (Info->FieldNode), ObjDesc)); 615 616 /* Remove local reference to the object */ 617 618 AcpiUtRemoveReference (ObjDesc); 619 return_ACPI_STATUS (Status); 620 } 621 622