Sync ACPICA with Intel's version 20180508 (from previously 20170831).
[dragonfly.git] / sys / contrib / dev / acpica / source / components / executer / exfldio.c
bloba20b3b69e3de47e3d153a0374b32ede32c14feb7
1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
80 * 4. Disclaimer and Export Compliance
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
119 * Redistribution and use in source and binary forms, with or without
120 * modification, are permitted provided that the following conditions
121 * are met:
122 * 1. Redistributions of source code must retain the above copyright
123 * notice, this list of conditions, and the following disclaimer,
124 * without modification.
125 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126 * substantially similar to the "NO WARRANTY" disclaimer below
127 * ("Disclaimer") and any redistribution must be conditioned upon
128 * including a substantially similar Disclaimer requirement for further
129 * binary redistribution.
130 * 3. Neither the names of the above-listed copyright holders nor the names
131 * of any contributors may be used to endorse or promote products derived
132 * from this software without specific prior written permission.
134 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146 * Alternatively, you may choose to be licensed under the terms of the
147 * GNU General Public License ("GPL") version 2 as published by the Free
148 * Software Foundation.
150 *****************************************************************************/
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acinterp.h"
155 #include "amlcode.h"
156 #include "acevents.h"
157 #include "acdispat.h"
160 #define _COMPONENT ACPI_EXECUTER
161 ACPI_MODULE_NAME ("exfldio")
163 /* Local prototypes */
165 static ACPI_STATUS
166 AcpiExFieldDatumIo (
167 ACPI_OPERAND_OBJECT *ObjDesc,
168 UINT32 FieldDatumByteOffset,
169 UINT64 *Value,
170 UINT32 ReadWrite);
172 static BOOLEAN
173 AcpiExRegisterOverflow (
174 ACPI_OPERAND_OBJECT *ObjDesc,
175 UINT64 Value);
177 static ACPI_STATUS
178 AcpiExSetupRegion (
179 ACPI_OPERAND_OBJECT *ObjDesc,
180 UINT32 FieldDatumByteOffset);
183 /*******************************************************************************
185 * FUNCTION: AcpiExSetupRegion
187 * PARAMETERS: ObjDesc - Field to be read or written
188 * FieldDatumByteOffset - Byte offset of this datum within the
189 * parent field
191 * RETURN: Status
193 * DESCRIPTION: Common processing for AcpiExExtractFromField and
194 * AcpiExInsertIntoField. Initialize the Region if necessary and
195 * validate the request.
197 ******************************************************************************/
199 static ACPI_STATUS
200 AcpiExSetupRegion (
201 ACPI_OPERAND_OBJECT *ObjDesc,
202 UINT32 FieldDatumByteOffset)
204 ACPI_STATUS Status = AE_OK;
205 ACPI_OPERAND_OBJECT *RgnDesc;
206 UINT8 SpaceId;
209 ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
212 RgnDesc = ObjDesc->CommonField.RegionObj;
214 /* We must have a valid region */
216 if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
218 ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
219 RgnDesc->Common.Type,
220 AcpiUtGetObjectTypeName (RgnDesc)));
222 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
225 SpaceId = RgnDesc->Region.SpaceId;
227 /* Validate the Space ID */
229 if (!AcpiIsValidSpaceId (SpaceId))
231 ACPI_ERROR ((AE_INFO,
232 "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
233 return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
237 * If the Region Address and Length have not been previously evaluated,
238 * evaluate them now and save the results.
240 if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
242 Status = AcpiDsGetRegionArguments (RgnDesc);
243 if (ACPI_FAILURE (Status))
245 return_ACPI_STATUS (Status);
250 * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
251 * address space and the request cannot be directly validated
253 if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
254 SpaceId == ACPI_ADR_SPACE_GSBUS ||
255 SpaceId == ACPI_ADR_SPACE_IPMI)
257 /* SMBus or IPMI has a non-linear address space */
259 return_ACPI_STATUS (AE_OK);
262 #ifdef ACPI_UNDER_DEVELOPMENT
264 * If the Field access is AnyAcc, we can now compute the optimal
265 * access (because we know know the length of the parent region)
267 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
269 if (ACPI_FAILURE (Status))
271 return_ACPI_STATUS (Status);
274 #endif
277 * Validate the request. The entire request from the byte offset for a
278 * length of one field datum (access width) must fit within the region.
279 * (Region length is specified in bytes)
281 if (RgnDesc->Region.Length <
282 (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
283 ObjDesc->CommonField.AccessByteWidth))
285 if (AcpiGbl_EnableInterpreterSlack)
288 * Slack mode only: We will go ahead and allow access to this
289 * field if it is within the region length rounded up to the next
290 * access width boundary. ACPI_SIZE cast for 64-bit compile.
292 if (ACPI_ROUND_UP (RgnDesc->Region.Length,
293 ObjDesc->CommonField.AccessByteWidth) >=
294 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
295 ObjDesc->CommonField.AccessByteWidth +
296 FieldDatumByteOffset))
298 return_ACPI_STATUS (AE_OK);
302 if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
305 * This is the case where the AccessType (AccWord, etc.) is wider
306 * than the region itself. For example, a region of length one
307 * byte, and a field with Dword access specified.
309 ACPI_ERROR ((AE_INFO,
310 "Field [%4.4s] access width (%u bytes) "
311 "too large for region [%4.4s] (length %u)",
312 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
313 ObjDesc->CommonField.AccessByteWidth,
314 AcpiUtGetNodeName (RgnDesc->Region.Node),
315 RgnDesc->Region.Length));
319 * Offset rounded up to next multiple of field width
320 * exceeds region length, indicate an error
322 ACPI_ERROR ((AE_INFO,
323 "Field [%4.4s] Base+Offset+Width %u+%u+%u "
324 "is beyond end of region [%4.4s] (length %u)",
325 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
326 ObjDesc->CommonField.BaseByteOffset,
327 FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
328 AcpiUtGetNodeName (RgnDesc->Region.Node),
329 RgnDesc->Region.Length));
331 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
334 return_ACPI_STATUS (AE_OK);
338 /*******************************************************************************
340 * FUNCTION: AcpiExAccessRegion
342 * PARAMETERS: ObjDesc - Field to be read
343 * FieldDatumByteOffset - Byte offset of this datum within the
344 * parent field
345 * Value - Where to store value (must at least
346 * 64 bits)
347 * Function - Read or Write flag plus other region-
348 * dependent flags
350 * RETURN: Status
352 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
354 ******************************************************************************/
356 ACPI_STATUS
357 AcpiExAccessRegion (
358 ACPI_OPERAND_OBJECT *ObjDesc,
359 UINT32 FieldDatumByteOffset,
360 UINT64 *Value,
361 UINT32 Function)
363 ACPI_STATUS Status;
364 ACPI_OPERAND_OBJECT *RgnDesc;
365 UINT32 RegionOffset;
368 ACPI_FUNCTION_TRACE (ExAccessRegion);
372 * Ensure that the region operands are fully evaluated and verify
373 * the validity of the request
375 Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
376 if (ACPI_FAILURE (Status))
378 return_ACPI_STATUS (Status);
382 * The physical address of this field datum is:
384 * 1) The base of the region, plus
385 * 2) The base offset of the field, plus
386 * 3) The current offset into the field
388 RgnDesc = ObjDesc->CommonField.RegionObj;
389 RegionOffset =
390 ObjDesc->CommonField.BaseByteOffset +
391 FieldDatumByteOffset;
393 if ((Function & ACPI_IO_MASK) == ACPI_READ)
395 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
397 else
399 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
402 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
403 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %8.8X%8.8X\n",
404 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
405 RgnDesc->Region.SpaceId,
406 ObjDesc->CommonField.AccessByteWidth,
407 ObjDesc->CommonField.BaseByteOffset,
408 FieldDatumByteOffset,
409 ACPI_FORMAT_UINT64 (RgnDesc->Region.Address + RegionOffset)));
411 /* Invoke the appropriate AddressSpace/OpRegion handler */
413 Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
414 Function, RegionOffset,
415 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
417 if (ACPI_FAILURE (Status))
419 if (Status == AE_NOT_IMPLEMENTED)
421 ACPI_ERROR ((AE_INFO,
422 "Region %s (ID=%u) not implemented",
423 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
424 RgnDesc->Region.SpaceId));
426 else if (Status == AE_NOT_EXIST)
428 ACPI_ERROR ((AE_INFO,
429 "Region %s (ID=%u) has no handler",
430 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
431 RgnDesc->Region.SpaceId));
435 return_ACPI_STATUS (Status);
439 /*******************************************************************************
441 * FUNCTION: AcpiExRegisterOverflow
443 * PARAMETERS: ObjDesc - Register(Field) to be written
444 * Value - Value to be stored
446 * RETURN: TRUE if value overflows the field, FALSE otherwise
448 * DESCRIPTION: Check if a value is out of range of the field being written.
449 * Used to check if the values written to Index and Bank registers
450 * are out of range. Normally, the value is simply truncated
451 * to fit the field, but this case is most likely a serious
452 * coding error in the ASL.
454 ******************************************************************************/
456 static BOOLEAN
457 AcpiExRegisterOverflow (
458 ACPI_OPERAND_OBJECT *ObjDesc,
459 UINT64 Value)
462 if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
465 * The field is large enough to hold the maximum integer, so we can
466 * never overflow it.
468 return (FALSE);
471 if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
474 * The Value is larger than the maximum value that can fit into
475 * the register.
477 ACPI_ERROR ((AE_INFO,
478 "Index value 0x%8.8X%8.8X overflows field width 0x%X",
479 ACPI_FORMAT_UINT64 (Value),
480 ObjDesc->CommonField.BitLength));
482 return (TRUE);
485 /* The Value will fit into the field with no truncation */
487 return (FALSE);
491 /*******************************************************************************
493 * FUNCTION: AcpiExFieldDatumIo
495 * PARAMETERS: ObjDesc - Field to be read
496 * FieldDatumByteOffset - Byte offset of this datum within the
497 * parent field
498 * Value - Where to store value (must be 64 bits)
499 * ReadWrite - Read or Write flag
501 * RETURN: Status
503 * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
504 * demultiplexed here to handle the different types of fields
505 * (BufferField, RegionField, IndexField, BankField)
507 ******************************************************************************/
509 static ACPI_STATUS
510 AcpiExFieldDatumIo (
511 ACPI_OPERAND_OBJECT *ObjDesc,
512 UINT32 FieldDatumByteOffset,
513 UINT64 *Value,
514 UINT32 ReadWrite)
516 ACPI_STATUS Status;
517 UINT64 LocalValue;
520 ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
523 if (ReadWrite == ACPI_READ)
525 if (!Value)
527 LocalValue = 0;
529 /* To support reads without saving return value */
530 Value = &LocalValue;
533 /* Clear the entire return buffer first, [Very Important!] */
535 *Value = 0;
539 * The four types of fields are:
541 * BufferField - Read/write from/to a Buffer
542 * RegionField - Read/write from/to a Operation Region.
543 * BankField - Write to a Bank Register, then read/write from/to an
544 * OperationRegion
545 * IndexField - Write to an Index Register, then read/write from/to a
546 * Data Register
548 switch (ObjDesc->Common.Type)
550 case ACPI_TYPE_BUFFER_FIELD:
552 * If the BufferField arguments have not been previously evaluated,
553 * evaluate them now and save the results.
555 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
557 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
558 if (ACPI_FAILURE (Status))
560 return_ACPI_STATUS (Status);
564 if (ReadWrite == ACPI_READ)
567 * Copy the data from the source buffer.
568 * Length is the field width in bytes.
570 memcpy (Value,
571 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
572 ObjDesc->BufferField.BaseByteOffset +
573 FieldDatumByteOffset,
574 ObjDesc->CommonField.AccessByteWidth);
576 else
579 * Copy the data to the target buffer.
580 * Length is the field width in bytes.
582 memcpy ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
583 ObjDesc->BufferField.BaseByteOffset +
584 FieldDatumByteOffset,
585 Value, ObjDesc->CommonField.AccessByteWidth);
588 Status = AE_OK;
589 break;
591 case ACPI_TYPE_LOCAL_BANK_FIELD:
593 * Ensure that the BankValue is not beyond the capacity of
594 * the register
596 if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
597 (UINT64) ObjDesc->BankField.Value))
599 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
603 * For BankFields, we must write the BankValue to the BankRegister
604 * (itself a RegionField) before we can access the data.
606 Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
607 &ObjDesc->BankField.Value,
608 sizeof (ObjDesc->BankField.Value));
609 if (ACPI_FAILURE (Status))
611 return_ACPI_STATUS (Status);
615 * Now that the Bank has been selected, fall through to the
616 * RegionField case and write the datum to the Operation Region
619 /*lint -fallthrough */
621 case ACPI_TYPE_LOCAL_REGION_FIELD:
623 * For simple RegionFields, we just directly access the owning
624 * Operation Region.
626 Status = AcpiExAccessRegion (
627 ObjDesc, FieldDatumByteOffset, Value, ReadWrite);
628 break;
630 case ACPI_TYPE_LOCAL_INDEX_FIELD:
632 * Ensure that the IndexValue is not beyond the capacity of
633 * the register
635 if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
636 (UINT64) ObjDesc->IndexField.Value))
638 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
641 /* Write the index value to the IndexRegister (itself a RegionField) */
643 FieldDatumByteOffset += ObjDesc->IndexField.Value;
645 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
646 "Write to Index Register: Value %8.8X\n",
647 FieldDatumByteOffset));
649 Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
650 &FieldDatumByteOffset, sizeof (FieldDatumByteOffset));
651 if (ACPI_FAILURE (Status))
653 return_ACPI_STATUS (Status);
656 if (ReadWrite == ACPI_READ)
658 /* Read the datum from the DataRegister */
660 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
661 "Read from Data Register\n"));
663 Status = AcpiExExtractFromField (
664 ObjDesc->IndexField.DataObj, Value, sizeof (UINT64));
666 else
668 /* Write the datum to the DataRegister */
670 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
671 "Write to Data Register: Value %8.8X%8.8X\n",
672 ACPI_FORMAT_UINT64 (*Value)));
674 Status = AcpiExInsertIntoField (
675 ObjDesc->IndexField.DataObj, Value, sizeof (UINT64));
677 break;
679 default:
681 ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
682 ObjDesc->Common.Type));
683 Status = AE_AML_INTERNAL;
684 break;
687 if (ACPI_SUCCESS (Status))
689 if (ReadWrite == ACPI_READ)
691 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
692 "Value Read %8.8X%8.8X, Width %u\n",
693 ACPI_FORMAT_UINT64 (*Value),
694 ObjDesc->CommonField.AccessByteWidth));
696 else
698 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
699 "Value Written %8.8X%8.8X, Width %u\n",
700 ACPI_FORMAT_UINT64 (*Value),
701 ObjDesc->CommonField.AccessByteWidth));
705 return_ACPI_STATUS (Status);
709 /*******************************************************************************
711 * FUNCTION: AcpiExWriteWithUpdateRule
713 * PARAMETERS: ObjDesc - Field to be written
714 * Mask - bitmask within field datum
715 * FieldValue - Value to write
716 * FieldDatumByteOffset - Offset of datum within field
718 * RETURN: Status
720 * DESCRIPTION: Apply the field update rule to a field write
722 ******************************************************************************/
724 ACPI_STATUS
725 AcpiExWriteWithUpdateRule (
726 ACPI_OPERAND_OBJECT *ObjDesc,
727 UINT64 Mask,
728 UINT64 FieldValue,
729 UINT32 FieldDatumByteOffset)
731 ACPI_STATUS Status = AE_OK;
732 UINT64 MergedValue;
733 UINT64 CurrentValue;
736 ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
739 /* Start with the new bits */
741 MergedValue = FieldValue;
743 /* If the mask is all ones, we don't need to worry about the update rule */
745 if (Mask != ACPI_UINT64_MAX)
747 /* Decode the update rule */
749 switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
751 case AML_FIELD_UPDATE_PRESERVE:
753 * Check if update rule needs to be applied (not if mask is all
754 * ones) The left shift drops the bits we want to ignore.
756 if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
757 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
760 * Read the current contents of the byte/word/dword containing
761 * the field, and merge with the new field value.
763 Status = AcpiExFieldDatumIo (
764 ObjDesc, FieldDatumByteOffset, &CurrentValue, ACPI_READ);
765 if (ACPI_FAILURE (Status))
767 return_ACPI_STATUS (Status);
770 MergedValue |= (CurrentValue & ~Mask);
772 break;
774 case AML_FIELD_UPDATE_WRITE_AS_ONES:
776 /* Set positions outside the field to all ones */
778 MergedValue |= ~Mask;
779 break;
781 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
783 /* Set positions outside the field to all zeros */
785 MergedValue &= Mask;
786 break;
788 default:
790 ACPI_ERROR ((AE_INFO,
791 "Unknown UpdateRule value: 0x%X",
792 (ObjDesc->CommonField.FieldFlags &
793 AML_FIELD_UPDATE_RULE_MASK)));
794 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
798 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
799 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, "
800 "Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
801 ACPI_FORMAT_UINT64 (Mask),
802 FieldDatumByteOffset,
803 ObjDesc->CommonField.AccessByteWidth,
804 ACPI_FORMAT_UINT64 (FieldValue),
805 ACPI_FORMAT_UINT64 (MergedValue)));
807 /* Write the merged value */
809 Status = AcpiExFieldDatumIo (
810 ObjDesc, FieldDatumByteOffset, &MergedValue, ACPI_WRITE);
812 return_ACPI_STATUS (Status);
816 /*******************************************************************************
818 * FUNCTION: AcpiExExtractFromField
820 * PARAMETERS: ObjDesc - Field to be read
821 * Buffer - Where to store the field data
822 * BufferLength - Length of Buffer
824 * RETURN: Status
826 * DESCRIPTION: Retrieve the current value of the given field
828 ******************************************************************************/
830 ACPI_STATUS
831 AcpiExExtractFromField (
832 ACPI_OPERAND_OBJECT *ObjDesc,
833 void *Buffer,
834 UINT32 BufferLength)
836 ACPI_STATUS Status;
837 UINT64 RawDatum;
838 UINT64 MergedDatum;
839 UINT32 FieldOffset = 0;
840 UINT32 BufferOffset = 0;
841 UINT32 BufferTailBits;
842 UINT32 DatumCount;
843 UINT32 FieldDatumCount;
844 UINT32 AccessBitWidth;
845 UINT32 i;
848 ACPI_FUNCTION_TRACE (ExExtractFromField);
851 /* Validate target buffer and clear it */
853 if (BufferLength <
854 ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
856 ACPI_ERROR ((AE_INFO,
857 "Field size %u (bits) is too large for buffer (%u)",
858 ObjDesc->CommonField.BitLength, BufferLength));
860 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
863 memset (Buffer, 0, BufferLength);
864 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
866 /* Handle the simple case here */
868 if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
869 (ObjDesc->CommonField.BitLength == AccessBitWidth))
871 if (BufferLength >= sizeof (UINT64))
873 Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
875 else
877 /* Use RawDatum (UINT64) to handle buffers < 64 bits */
879 Status = AcpiExFieldDatumIo (ObjDesc, 0, &RawDatum, ACPI_READ);
880 memcpy (Buffer, &RawDatum, BufferLength);
883 return_ACPI_STATUS (Status);
886 /* TBD: Move to common setup code */
888 /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
890 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
892 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
893 AccessBitWidth = sizeof (UINT64) * 8;
896 /* Compute the number of datums (access width data items) */
898 DatumCount = ACPI_ROUND_UP_TO (
899 ObjDesc->CommonField.BitLength, AccessBitWidth);
901 FieldDatumCount = ACPI_ROUND_UP_TO (
902 ObjDesc->CommonField.BitLength +
903 ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
905 /* Priming read from the field */
907 Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
908 if (ACPI_FAILURE (Status))
910 return_ACPI_STATUS (Status);
912 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
914 /* Read the rest of the field */
916 for (i = 1; i < FieldDatumCount; i++)
918 /* Get next input datum from the field */
920 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
921 Status = AcpiExFieldDatumIo (
922 ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
923 if (ACPI_FAILURE (Status))
925 return_ACPI_STATUS (Status);
929 * Merge with previous datum if necessary.
931 * Note: Before the shift, check if the shift value will be larger than
932 * the integer size. If so, there is no need to perform the operation.
933 * This avoids the differences in behavior between different compilers
934 * concerning shift values larger than the target data width.
936 if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
937 ACPI_INTEGER_BIT_SIZE)
939 MergedDatum |= RawDatum <<
940 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
943 if (i == DatumCount)
945 break;
948 /* Write merged datum to target buffer */
950 memcpy (((char *) Buffer) + BufferOffset, &MergedDatum,
951 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
952 BufferLength - BufferOffset));
954 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
955 MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
958 /* Mask off any extra bits in the last datum */
960 BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
961 if (BufferTailBits)
963 MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
966 /* Write the last datum to the buffer */
968 memcpy (((char *) Buffer) + BufferOffset, &MergedDatum,
969 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
970 BufferLength - BufferOffset));
972 return_ACPI_STATUS (AE_OK);
976 /*******************************************************************************
978 * FUNCTION: AcpiExInsertIntoField
980 * PARAMETERS: ObjDesc - Field to be written
981 * Buffer - Data to be written
982 * BufferLength - Length of Buffer
984 * RETURN: Status
986 * DESCRIPTION: Store the Buffer contents into the given field
988 ******************************************************************************/
990 ACPI_STATUS
991 AcpiExInsertIntoField (
992 ACPI_OPERAND_OBJECT *ObjDesc,
993 void *Buffer,
994 UINT32 BufferLength)
996 void *NewBuffer;
997 ACPI_STATUS Status;
998 UINT64 Mask;
999 UINT64 WidthMask;
1000 UINT64 MergedDatum;
1001 UINT64 RawDatum = 0;
1002 UINT32 FieldOffset = 0;
1003 UINT32 BufferOffset = 0;
1004 UINT32 BufferTailBits;
1005 UINT32 DatumCount;
1006 UINT32 FieldDatumCount;
1007 UINT32 AccessBitWidth;
1008 UINT32 RequiredLength;
1009 UINT32 i;
1012 ACPI_FUNCTION_TRACE (ExInsertIntoField);
1015 /* Validate input buffer */
1017 NewBuffer = NULL;
1018 RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
1019 ObjDesc->CommonField.BitLength);
1022 * We must have a buffer that is at least as long as the field
1023 * we are writing to. This is because individual fields are
1024 * indivisible and partial writes are not supported -- as per
1025 * the ACPI specification.
1027 if (BufferLength < RequiredLength)
1029 /* We need to create a new buffer */
1031 NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
1032 if (!NewBuffer)
1034 return_ACPI_STATUS (AE_NO_MEMORY);
1038 * Copy the original data to the new buffer, starting
1039 * at Byte zero. All unused (upper) bytes of the
1040 * buffer will be 0.
1042 memcpy ((char *) NewBuffer, (char *) Buffer, BufferLength);
1043 Buffer = NewBuffer;
1044 BufferLength = RequiredLength;
1047 /* TBD: Move to common setup code */
1049 /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
1050 if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
1052 ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
1055 AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
1057 /* Create the bitmasks used for bit insertion */
1059 WidthMask = ACPI_MASK_BITS_ABOVE_64 (AccessBitWidth);
1060 Mask = WidthMask &
1061 ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
1063 /* Compute the number of datums (access width data items) */
1065 DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
1066 AccessBitWidth);
1068 FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
1069 ObjDesc->CommonField.StartFieldBitOffset,
1070 AccessBitWidth);
1072 /* Get initial Datum from the input buffer */
1074 memcpy (&RawDatum, Buffer,
1075 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1076 BufferLength - BufferOffset));
1078 MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1080 /* Write the entire field */
1082 for (i = 1; i < FieldDatumCount; i++)
1084 /* Write merged datum to the target field */
1086 MergedDatum &= Mask;
1087 Status = AcpiExWriteWithUpdateRule (
1088 ObjDesc, Mask, MergedDatum, FieldOffset);
1089 if (ACPI_FAILURE (Status))
1091 goto Exit;
1094 FieldOffset += ObjDesc->CommonField.AccessByteWidth;
1097 * Start new output datum by merging with previous input datum
1098 * if necessary.
1100 * Note: Before the shift, check if the shift value will be larger than
1101 * the integer size. If so, there is no need to perform the operation.
1102 * This avoids the differences in behavior between different compilers
1103 * concerning shift values larger than the target data width.
1105 if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1106 ACPI_INTEGER_BIT_SIZE)
1108 MergedDatum = RawDatum >>
1109 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1111 else
1113 MergedDatum = 0;
1116 Mask = WidthMask;
1118 if (i == DatumCount)
1120 break;
1123 /* Get the next input datum from the buffer */
1125 BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1126 memcpy (&RawDatum, ((char *) Buffer) + BufferOffset,
1127 ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1128 BufferLength - BufferOffset));
1130 MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1133 /* Mask off any extra bits in the last datum */
1135 BufferTailBits = (ObjDesc->CommonField.BitLength +
1136 ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1137 if (BufferTailBits)
1139 Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1142 /* Write the last datum to the field */
1144 MergedDatum &= Mask;
1145 Status = AcpiExWriteWithUpdateRule (
1146 ObjDesc, Mask, MergedDatum, FieldOffset);
1148 Exit:
1149 /* Free temporary buffer if we used one */
1151 if (NewBuffer)
1153 ACPI_FREE (NewBuffer);
1155 return_ACPI_STATUS (Status);