Back to home page

LXR

 
 

    


File indexing completed on 2025-05-11 08:24:02

0001 /******************************************************************************
0002  *
0003  * Module Name: exfldio - Aml Field I/O
0004  *
0005  *****************************************************************************/
0006 
0007 /******************************************************************************
0008  *
0009  * 1. Copyright Notice
0010  *
0011  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
0012  * All rights reserved.
0013  *
0014  * 2. License
0015  *
0016  * 2.1. This is your license from Intel Corp. under its intellectual property
0017  * rights. You may have additional license terms from the party that provided
0018  * you this software, covering your right to use that party's intellectual
0019  * property rights.
0020  *
0021  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
0022  * copy of the source code appearing in this file ("Covered Code") an
0023  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
0024  * base code distributed originally by Intel ("Original Intel Code") to copy,
0025  * make derivatives, distribute, use and display any portion of the Covered
0026  * Code in any form, with the right to sublicense such rights; and
0027  *
0028  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
0029  * license (with the right to sublicense), under only those claims of Intel
0030  * patents that are infringed by the Original Intel Code, to make, use, sell,
0031  * offer to sell, and import the Covered Code and derivative works thereof
0032  * solely to the minimum extent necessary to exercise the above copyright
0033  * license, and in no event shall the patent license extend to any additions
0034  * to or modifications of the Original Intel Code. No other license or right
0035  * is granted directly or by implication, estoppel or otherwise;
0036  *
0037  * The above copyright and patent license is granted only if the following
0038  * conditions are met:
0039  *
0040  * 3. Conditions
0041  *
0042  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
0043  * Redistribution of source code of any substantial portion of the Covered
0044  * Code or modification with rights to further distribute source must include
0045  * the above Copyright Notice, the above License, this list of Conditions,
0046  * and the following Disclaimer and Export Compliance provision. In addition,
0047  * Licensee must cause all Covered Code to which Licensee contributes to
0048  * contain a file documenting the changes Licensee made to create that Covered
0049  * Code and the date of any change. Licensee must include in that file the
0050  * documentation of any changes made by any predecessor Licensee. Licensee
0051  * must include a prominent statement that the modification is derived,
0052  * directly or indirectly, from Original Intel Code.
0053  *
0054  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
0055  * Redistribution of source code of any substantial portion of the Covered
0056  * Code or modification without rights to further distribute source must
0057  * include the following Disclaimer and Export Compliance provision in the
0058  * documentation and/or other materials provided with distribution. In
0059  * addition, Licensee may not authorize further sublicense of source of any
0060  * portion of the Covered Code, and must include terms to the effect that the
0061  * license from Licensee to its licensee is limited to the intellectual
0062  * property embodied in the software Licensee provides to its licensee, and
0063  * not to intellectual property embodied in modifications its licensee may
0064  * make.
0065  *
0066  * 3.3. Redistribution of Executable. Redistribution in executable form of any
0067  * substantial portion of the Covered Code or modification must reproduce the
0068  * above Copyright Notice, and the following Disclaimer and Export Compliance
0069  * provision in the documentation and/or other materials provided with the
0070  * distribution.
0071  *
0072  * 3.4. Intel retains all right, title, and interest in and to the Original
0073  * Intel Code.
0074  *
0075  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
0076  * Intel shall be used in advertising or otherwise to promote the sale, use or
0077  * other dealings in products derived from or relating to the Covered Code
0078  * without prior written authorization from Intel.
0079  *
0080  * 4. Disclaimer and Export Compliance
0081  *
0082  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
0083  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
0084  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
0085  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
0086  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
0087  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
0088  * PARTICULAR PURPOSE.
0089  *
0090  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
0091  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
0092  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
0093  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
0094  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
0095  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
0096  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
0097  * LIMITED REMEDY.
0098  *
0099  * 4.3. Licensee shall not export, either directly or indirectly, any of this
0100  * software or system incorporating such software without first obtaining any
0101  * required license or other approval from the U. S. Department of Commerce or
0102  * any other agency or department of the United States Government. In the
0103  * event Licensee exports any such software from the United States or
0104  * re-exports any such software from a foreign destination, Licensee shall
0105  * ensure that the distribution and export/re-export of the software is in
0106  * compliance with all laws, regulations, orders, or other restrictions of the
0107  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
0108  * any of its subsidiaries will export/re-export any technical data, process,
0109  * software, or service, directly or indirectly, to any country for which the
0110  * United States government or any agency thereof requires an export license,
0111  * other governmental approval, or letter of assurance, without first obtaining
0112  * such license, approval or letter.
0113  *
0114  *****************************************************************************
0115  *
0116  * Alternatively, you may choose to be licensed under the terms of the
0117  * following license:
0118  *
0119  * Redistribution and use in source and binary forms, with or without
0120  * modification, are permitted provided that the following conditions
0121  * are met:
0122  * 1. Redistributions of source code must retain the above copyright
0123  *    notice, this list of conditions, and the following disclaimer,
0124  *    without modification.
0125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
0126  *    substantially similar to the "NO WARRANTY" disclaimer below
0127  *    ("Disclaimer") and any redistribution must be conditioned upon
0128  *    including a substantially similar Disclaimer requirement for further
0129  *    binary redistribution.
0130  * 3. Neither the names of the above-listed copyright holders nor the names
0131  *    of any contributors may be used to endorse or promote products derived
0132  *    from this software without specific prior written permission.
0133  *
0134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
0135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
0136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
0137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
0138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
0139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
0140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
0141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
0142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
0143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
0144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
0145  *
0146  * Alternatively, you may choose to be licensed under the terms of the
0147  * GNU General Public License ("GPL") version 2 as published by the Free
0148  * Software Foundation.
0149  *
0150  *****************************************************************************/
0151 
0152 #include "acpi.h"
0153 #include "accommon.h"
0154 #include "acinterp.h"
0155 #include "amlcode.h"
0156 #include "acevents.h"
0157 #include "acdispat.h"
0158 
0159 
0160 #define _COMPONENT          ACPI_EXECUTER
0161         ACPI_MODULE_NAME    ("exfldio")
0162 
0163 /* Local prototypes */
0164 
0165 static ACPI_STATUS
0166 AcpiExFieldDatumIo (
0167     ACPI_OPERAND_OBJECT     *ObjDesc,
0168     UINT32                  FieldDatumByteOffset,
0169     UINT64                  *Value,
0170     UINT32                  ReadWrite);
0171 
0172 static BOOLEAN
0173 AcpiExRegisterOverflow (
0174     ACPI_OPERAND_OBJECT     *ObjDesc,
0175     UINT64                  Value);
0176 
0177 static ACPI_STATUS
0178 AcpiExSetupRegion (
0179     ACPI_OPERAND_OBJECT     *ObjDesc,
0180     UINT32                  FieldDatumByteOffset);
0181 
0182 
0183 /*******************************************************************************
0184  *
0185  * FUNCTION:    AcpiExSetupRegion
0186  *
0187  * PARAMETERS:  ObjDesc                 - Field to be read or written
0188  *              FieldDatumByteOffset    - Byte offset of this datum within the
0189  *                                        parent field
0190  *
0191  * RETURN:      Status
0192  *
0193  * DESCRIPTION: Common processing for AcpiExExtractFromField and
0194  *              AcpiExInsertIntoField. Initialize the Region if necessary and
0195  *              validate the request.
0196  *
0197  ******************************************************************************/
0198 
0199 static ACPI_STATUS
0200 AcpiExSetupRegion (
0201     ACPI_OPERAND_OBJECT     *ObjDesc,
0202     UINT32                  FieldDatumByteOffset)
0203 {
0204     ACPI_STATUS             Status = AE_OK;
0205     ACPI_OPERAND_OBJECT     *RgnDesc;
0206     UINT8                   SpaceId;
0207 
0208 
0209     ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
0210 
0211 
0212     RgnDesc = ObjDesc->CommonField.RegionObj;
0213 
0214     /* We must have a valid region */
0215 
0216     if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
0217     {
0218         ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
0219             RgnDesc->Common.Type,
0220             AcpiUtGetObjectTypeName (RgnDesc)));
0221 
0222         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
0223     }
0224 
0225     SpaceId = RgnDesc->Region.SpaceId;
0226 
0227     /* Validate the Space ID */
0228 
0229     if (!AcpiIsValidSpaceId (SpaceId))
0230     {
0231         ACPI_ERROR ((AE_INFO,
0232             "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
0233         return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
0234     }
0235 
0236     /*
0237      * If the Region Address and Length have not been previously evaluated,
0238      * evaluate them now and save the results.
0239      */
0240     if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
0241     {
0242         Status = AcpiDsGetRegionArguments (RgnDesc);
0243         if (ACPI_FAILURE (Status))
0244         {
0245             return_ACPI_STATUS (Status);
0246         }
0247     }
0248 
0249     /*
0250      * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
0251      * address space and the request cannot be directly validated
0252      */
0253     if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
0254         SpaceId == ACPI_ADR_SPACE_GSBUS ||
0255         SpaceId == ACPI_ADR_SPACE_IPMI)
0256     {
0257         /* SMBus or IPMI has a non-linear address space */
0258 
0259         return_ACPI_STATUS (AE_OK);
0260     }
0261 
0262 #ifdef ACPI_UNDER_DEVELOPMENT
0263     /*
0264      * If the Field access is AnyAcc, we can now compute the optimal
0265      * access (because we know the length of the parent region)
0266      */
0267     if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
0268     {
0269         if (ACPI_FAILURE (Status))
0270         {
0271             return_ACPI_STATUS (Status);
0272         }
0273     }
0274 #endif
0275 
0276     /*
0277      * Validate the request. The entire request from the byte offset for a
0278      * length of one field datum (access width) must fit within the region.
0279      * (Region length is specified in bytes)
0280      */
0281     if (RgnDesc->Region.Length <
0282         (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
0283         ObjDesc->CommonField.AccessByteWidth))
0284     {
0285         if (AcpiGbl_EnableInterpreterSlack)
0286         {
0287             /*
0288              * Slack mode only:  We will go ahead and allow access to this
0289              * field if it is within the region length rounded up to the next
0290              * access width boundary. ACPI_SIZE cast for 64-bit compile.
0291              */
0292             if (ACPI_ROUND_UP (RgnDesc->Region.Length,
0293                     ObjDesc->CommonField.AccessByteWidth) >=
0294                 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
0295                     ObjDesc->CommonField.AccessByteWidth +
0296                     FieldDatumByteOffset))
0297             {
0298                 return_ACPI_STATUS (AE_OK);
0299             }
0300         }
0301 
0302         if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
0303         {
0304             /*
0305              * This is the case where the AccessType (AccWord, etc.) is wider
0306              * than the region itself. For example, a region of length one
0307              * byte, and a field with Dword access specified.
0308              */
0309             ACPI_ERROR ((AE_INFO,
0310                 "Field [%4.4s] access width (%u bytes) "
0311                 "too large for region [%4.4s] (length %u)",
0312                 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
0313                 ObjDesc->CommonField.AccessByteWidth,
0314                 AcpiUtGetNodeName (RgnDesc->Region.Node),
0315                 RgnDesc->Region.Length));
0316         }
0317 
0318         /*
0319          * Offset rounded up to next multiple of field width
0320          * exceeds region length, indicate an error
0321          */
0322         ACPI_ERROR ((AE_INFO,
0323             "Field [%4.4s] Base+Offset+Width %u+%u+%u "
0324             "is beyond end of region [%4.4s] (length %u)",
0325             AcpiUtGetNodeName (ObjDesc->CommonField.Node),
0326             ObjDesc->CommonField.BaseByteOffset,
0327             FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
0328             AcpiUtGetNodeName (RgnDesc->Region.Node),
0329             RgnDesc->Region.Length));
0330 
0331         return_ACPI_STATUS (AE_AML_REGION_LIMIT);
0332     }
0333 
0334     return_ACPI_STATUS (AE_OK);
0335 }
0336 
0337 
0338 /*******************************************************************************
0339  *
0340  * FUNCTION:    AcpiExAccessRegion
0341  *
0342  * PARAMETERS:  ObjDesc                 - Field to be read
0343  *              FieldDatumByteOffset    - Byte offset of this datum within the
0344  *                                        parent field
0345  *              Value                   - Where to store value (must at least
0346  *                                        64 bits)
0347  *              Function                - Read or Write flag plus other region-
0348  *                                        dependent flags
0349  *
0350  * RETURN:      Status
0351  *
0352  * DESCRIPTION: Read or Write a single field datum to an Operation Region.
0353  *
0354  ******************************************************************************/
0355 
0356 ACPI_STATUS
0357 AcpiExAccessRegion (
0358     ACPI_OPERAND_OBJECT     *ObjDesc,
0359     UINT32                  FieldDatumByteOffset,
0360     UINT64                  *Value,
0361     UINT32                  Function)
0362 {
0363     ACPI_STATUS             Status;
0364     ACPI_OPERAND_OBJECT     *RgnDesc;
0365     UINT32                  RegionOffset;
0366 
0367 
0368     ACPI_FUNCTION_TRACE (ExAccessRegion);
0369 
0370 
0371     /*
0372      * Ensure that the region operands are fully evaluated and verify
0373      * the validity of the request
0374      */
0375     Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
0376     if (ACPI_FAILURE (Status))
0377     {
0378         return_ACPI_STATUS (Status);
0379     }
0380 
0381     /*
0382      * The physical address of this field datum is:
0383      *
0384      * 1) The base of the region, plus
0385      * 2) The base offset of the field, plus
0386      * 3) The current offset into the field
0387      */
0388     RgnDesc = ObjDesc->CommonField.RegionObj;
0389     RegionOffset =
0390         ObjDesc->CommonField.BaseByteOffset +
0391         FieldDatumByteOffset;
0392 
0393     if ((Function & ACPI_IO_MASK) == ACPI_READ)
0394     {
0395         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
0396     }
0397     else
0398     {
0399         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
0400     }
0401 
0402     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
0403         " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %8.8X%8.8X\n",
0404         AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
0405         RgnDesc->Region.SpaceId,
0406         ObjDesc->CommonField.AccessByteWidth,
0407         ObjDesc->CommonField.BaseByteOffset,
0408         FieldDatumByteOffset,
0409         ACPI_FORMAT_UINT64 (RgnDesc->Region.Address + RegionOffset)));
0410 
0411     /* Invoke the appropriate AddressSpace/OpRegion handler */
0412 
0413     Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
0414         Function, RegionOffset,
0415         ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
0416 
0417     if (ACPI_FAILURE (Status))
0418     {
0419         if (Status == AE_NOT_IMPLEMENTED)
0420         {
0421             ACPI_ERROR ((AE_INFO,
0422                 "Region %s (ID=%u) not implemented",
0423                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
0424                 RgnDesc->Region.SpaceId));
0425         }
0426         else if (Status == AE_NOT_EXIST)
0427         {
0428             ACPI_ERROR ((AE_INFO,
0429                 "Region %s (ID=%u) has no handler",
0430                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
0431                 RgnDesc->Region.SpaceId));
0432         }
0433     }
0434 
0435     return_ACPI_STATUS (Status);
0436 }
0437 
0438 
0439 /*******************************************************************************
0440  *
0441  * FUNCTION:    AcpiExRegisterOverflow
0442  *
0443  * PARAMETERS:  ObjDesc                 - Register(Field) to be written
0444  *              Value                   - Value to be stored
0445  *
0446  * RETURN:      TRUE if value overflows the field, FALSE otherwise
0447  *
0448  * DESCRIPTION: Check if a value is out of range of the field being written.
0449  *              Used to check if the values written to Index and Bank registers
0450  *              are out of range. Normally, the value is simply truncated
0451  *              to fit the field, but this case is most likely a serious
0452  *              coding error in the ASL.
0453  *
0454  ******************************************************************************/
0455 
0456 static BOOLEAN
0457 AcpiExRegisterOverflow (
0458     ACPI_OPERAND_OBJECT     *ObjDesc,
0459     UINT64                  Value)
0460 {
0461 
0462     if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
0463     {
0464         /*
0465          * The field is large enough to hold the maximum integer, so we can
0466          * never overflow it.
0467          */
0468         return (FALSE);
0469     }
0470 
0471     if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
0472     {
0473         /*
0474          * The Value is larger than the maximum value that can fit into
0475          * the register.
0476          */
0477         ACPI_ERROR ((AE_INFO,
0478             "Index value 0x%8.8X%8.8X overflows field width 0x%X",
0479             ACPI_FORMAT_UINT64 (Value),
0480             ObjDesc->CommonField.BitLength));
0481 
0482         return (TRUE);
0483     }
0484 
0485     /* The Value will fit into the field with no truncation */
0486 
0487     return (FALSE);
0488 }
0489 
0490 
0491 /*******************************************************************************
0492  *
0493  * FUNCTION:    AcpiExFieldDatumIo
0494  *
0495  * PARAMETERS:  ObjDesc                 - Field to be read
0496  *              FieldDatumByteOffset    - Byte offset of this datum within the
0497  *                                        parent field
0498  *              Value                   - Where to store value (must be 64 bits)
0499  *              ReadWrite               - Read or Write flag
0500  *
0501  * RETURN:      Status
0502  *
0503  * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
0504  *              demultiplexed here to handle the different types of fields
0505  *              (BufferField, RegionField, IndexField, BankField)
0506  *
0507  ******************************************************************************/
0508 
0509 static ACPI_STATUS
0510 AcpiExFieldDatumIo (
0511     ACPI_OPERAND_OBJECT     *ObjDesc,
0512     UINT32                  FieldDatumByteOffset,
0513     UINT64                  *Value,
0514     UINT32                  ReadWrite)
0515 {
0516     ACPI_STATUS             Status;
0517     UINT64                  LocalValue;
0518 
0519 
0520     ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
0521 
0522 
0523     if (ReadWrite == ACPI_READ)
0524     {
0525         if (!Value)
0526         {
0527             LocalValue = 0;
0528 
0529             /* To support reads without saving return value */
0530             Value = &LocalValue;
0531         }
0532 
0533         /* Clear the entire return buffer first, [Very Important!] */
0534 
0535         *Value = 0;
0536     }
0537 
0538     /*
0539      * The four types of fields are:
0540      *
0541      * BufferField - Read/write from/to a Buffer
0542      * RegionField - Read/write from/to a Operation Region.
0543      * BankField   - Write to a Bank Register, then read/write from/to an
0544      *               OperationRegion
0545      * IndexField  - Write to an Index Register, then read/write from/to a
0546      *               Data Register
0547      */
0548     switch (ObjDesc->Common.Type)
0549     {
0550     case ACPI_TYPE_BUFFER_FIELD:
0551         /*
0552          * If the BufferField arguments have not been previously evaluated,
0553          * evaluate them now and save the results.
0554          */
0555         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
0556         {
0557             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
0558             if (ACPI_FAILURE (Status))
0559             {
0560                 return_ACPI_STATUS (Status);
0561             }
0562         }
0563 
0564         if (ReadWrite == ACPI_READ)
0565         {
0566             /*
0567              * Copy the data from the source buffer.
0568              * Length is the field width in bytes.
0569              */
0570             memcpy (Value,
0571                 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
0572                     ObjDesc->BufferField.BaseByteOffset +
0573                     FieldDatumByteOffset,
0574                 ObjDesc->CommonField.AccessByteWidth);
0575         }
0576         else
0577         {
0578             /*
0579              * Copy the data to the target buffer.
0580              * Length is the field width in bytes.
0581              */
0582             memcpy ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
0583                 ObjDesc->BufferField.BaseByteOffset +
0584                 FieldDatumByteOffset,
0585                 Value, ObjDesc->CommonField.AccessByteWidth);
0586         }
0587 
0588         Status = AE_OK;
0589         break;
0590 
0591     case ACPI_TYPE_LOCAL_BANK_FIELD:
0592         /*
0593          * Ensure that the BankValue is not beyond the capacity of
0594          * the register
0595          */
0596         if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
0597                 (UINT64) ObjDesc->BankField.Value))
0598         {
0599             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
0600         }
0601 
0602         /*
0603          * For BankFields, we must write the BankValue to the BankRegister
0604          * (itself a RegionField) before we can access the data.
0605          */
0606         Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
0607                     &ObjDesc->BankField.Value,
0608                     sizeof (ObjDesc->BankField.Value));
0609         if (ACPI_FAILURE (Status))
0610         {
0611             return_ACPI_STATUS (Status);
0612         }
0613 
0614         /*
0615          * Now that the Bank has been selected, fall through to the
0616          * RegionField case and write the datum to the Operation Region
0617          */
0618 
0619         ACPI_FALLTHROUGH;
0620 
0621     case ACPI_TYPE_LOCAL_REGION_FIELD:
0622         /*
0623          * For simple RegionFields, we just directly access the owning
0624          * Operation Region.
0625          */
0626         Status = AcpiExAccessRegion (
0627             ObjDesc, FieldDatumByteOffset, Value, ReadWrite);
0628         break;
0629 
0630     case ACPI_TYPE_LOCAL_INDEX_FIELD:
0631         /*
0632          * Ensure that the IndexValue is not beyond the capacity of
0633          * the register
0634          */
0635         if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
0636                 (UINT64) ObjDesc->IndexField.Value))
0637         {
0638             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
0639         }
0640 
0641         /* Write the index value to the IndexRegister (itself a RegionField) */
0642 
0643         FieldDatumByteOffset += ObjDesc->IndexField.Value;
0644 
0645         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
0646             "Write to Index Register: Value %8.8X\n",
0647             FieldDatumByteOffset));
0648 
0649         Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
0650             &FieldDatumByteOffset, sizeof (FieldDatumByteOffset));
0651         if (ACPI_FAILURE (Status))
0652         {
0653             return_ACPI_STATUS (Status);
0654         }
0655 
0656         if (ReadWrite == ACPI_READ)
0657         {
0658             /* Read the datum from the DataRegister */
0659 
0660             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
0661                 "Read from Data Register\n"));
0662 
0663             Status = AcpiExExtractFromField (
0664                 ObjDesc->IndexField.DataObj, Value, sizeof (UINT64));
0665         }
0666         else
0667         {
0668             /* Write the datum to the DataRegister */
0669 
0670             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
0671                 "Write to Data Register: Value %8.8X%8.8X\n",
0672                 ACPI_FORMAT_UINT64 (*Value)));
0673 
0674             Status = AcpiExInsertIntoField (
0675                 ObjDesc->IndexField.DataObj, Value, sizeof (UINT64));
0676         }
0677         break;
0678 
0679     default:
0680 
0681         ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
0682             ObjDesc->Common.Type));
0683         Status = AE_AML_INTERNAL;
0684         break;
0685     }
0686 
0687     if (ACPI_SUCCESS (Status))
0688     {
0689         if (ReadWrite == ACPI_READ)
0690         {
0691             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
0692                 "Value Read %8.8X%8.8X, Width %u\n",
0693                 ACPI_FORMAT_UINT64 (*Value),
0694                 ObjDesc->CommonField.AccessByteWidth));
0695         }
0696         else
0697         {
0698             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
0699                 "Value Written %8.8X%8.8X, Width %u\n",
0700                 ACPI_FORMAT_UINT64 (*Value),
0701                 ObjDesc->CommonField.AccessByteWidth));
0702         }
0703     }
0704 
0705     return_ACPI_STATUS (Status);
0706 }
0707 
0708 
0709 /*******************************************************************************
0710  *
0711  * FUNCTION:    AcpiExWriteWithUpdateRule
0712  *
0713  * PARAMETERS:  ObjDesc                 - Field to be written
0714  *              Mask                    - bitmask within field datum
0715  *              FieldValue              - Value to write
0716  *              FieldDatumByteOffset    - Offset of datum within field
0717  *
0718  * RETURN:      Status
0719  *
0720  * DESCRIPTION: Apply the field update rule to a field write
0721  *
0722  ******************************************************************************/
0723 
0724 ACPI_STATUS
0725 AcpiExWriteWithUpdateRule (
0726     ACPI_OPERAND_OBJECT     *ObjDesc,
0727     UINT64                  Mask,
0728     UINT64                  FieldValue,
0729     UINT32                  FieldDatumByteOffset)
0730 {
0731     ACPI_STATUS             Status = AE_OK;
0732     UINT64                  MergedValue;
0733     UINT64                  CurrentValue;
0734 
0735 
0736     ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
0737 
0738 
0739     /* Start with the new bits  */
0740 
0741     MergedValue = FieldValue;
0742 
0743     /* If the mask is all ones, we don't need to worry about the update rule */
0744 
0745     if (Mask != ACPI_UINT64_MAX)
0746     {
0747         /* Decode the update rule */
0748 
0749         switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
0750         {
0751         case AML_FIELD_UPDATE_PRESERVE:
0752             /*
0753              * Check if update rule needs to be applied (not if mask is all
0754              * ones)  The left shift drops the bits we want to ignore.
0755              */
0756             if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
0757                    ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
0758             {
0759                 /*
0760                  * Read the current contents of the byte/word/dword containing
0761                  * the field, and merge with the new field value.
0762                  */
0763                 Status = AcpiExFieldDatumIo (
0764                     ObjDesc, FieldDatumByteOffset, &CurrentValue, ACPI_READ);
0765                 if (ACPI_FAILURE (Status))
0766                 {
0767                     return_ACPI_STATUS (Status);
0768                 }
0769 
0770                 MergedValue |= (CurrentValue & ~Mask);
0771             }
0772             break;
0773 
0774         case AML_FIELD_UPDATE_WRITE_AS_ONES:
0775 
0776             /* Set positions outside the field to all ones */
0777 
0778             MergedValue |= ~Mask;
0779             break;
0780 
0781         case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
0782 
0783             /* Set positions outside the field to all zeros */
0784 
0785             MergedValue &= Mask;
0786             break;
0787 
0788         default:
0789 
0790             ACPI_ERROR ((AE_INFO,
0791                 "Unknown UpdateRule value: 0x%X",
0792                 (ObjDesc->CommonField.FieldFlags &
0793                     AML_FIELD_UPDATE_RULE_MASK)));
0794             return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
0795         }
0796     }
0797 
0798     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
0799         "Mask %8.8X%8.8X, DatumOffset %X, Width %X, "
0800         "Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
0801         ACPI_FORMAT_UINT64 (Mask),
0802         FieldDatumByteOffset,
0803         ObjDesc->CommonField.AccessByteWidth,
0804         ACPI_FORMAT_UINT64 (FieldValue),
0805         ACPI_FORMAT_UINT64 (MergedValue)));
0806 
0807     /* Write the merged value */
0808 
0809     Status = AcpiExFieldDatumIo (
0810         ObjDesc, FieldDatumByteOffset, &MergedValue, ACPI_WRITE);
0811 
0812     return_ACPI_STATUS (Status);
0813 }
0814 
0815 
0816 /*******************************************************************************
0817  *
0818  * FUNCTION:    AcpiExExtractFromField
0819  *
0820  * PARAMETERS:  ObjDesc             - Field to be read
0821  *              Buffer              - Where to store the field data
0822  *              BufferLength        - Length of Buffer
0823  *
0824  * RETURN:      Status
0825  *
0826  * DESCRIPTION: Retrieve the current value of the given field
0827  *
0828  ******************************************************************************/
0829 
0830 ACPI_STATUS
0831 AcpiExExtractFromField (
0832     ACPI_OPERAND_OBJECT     *ObjDesc,
0833     void                    *Buffer,
0834     UINT32                  BufferLength)
0835 {
0836     ACPI_STATUS             Status;
0837     UINT64                  RawDatum;
0838     UINT64                  MergedDatum;
0839     UINT32                  FieldOffset = 0;
0840     UINT32                  BufferOffset = 0;
0841     UINT32                  BufferTailBits;
0842     UINT32                  DatumCount;
0843     UINT32                  FieldDatumCount;
0844     UINT32                  AccessBitWidth;
0845     UINT32                  i;
0846 
0847 
0848     ACPI_FUNCTION_TRACE (ExExtractFromField);
0849 
0850 
0851     /* Validate target buffer and clear it */
0852 
0853     if (BufferLength <
0854         ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
0855     {
0856         ACPI_ERROR ((AE_INFO,
0857             "Field size %u (bits) is too large for buffer (%u)",
0858             ObjDesc->CommonField.BitLength, BufferLength));
0859 
0860         return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
0861     }
0862 
0863     memset (Buffer, 0, BufferLength);
0864     AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
0865 
0866     /* Handle the simple case here */
0867 
0868     if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
0869         (ObjDesc->CommonField.BitLength == AccessBitWidth))
0870     {
0871         if (BufferLength >= sizeof (UINT64))
0872         {
0873             Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
0874         }
0875         else
0876         {
0877             /* Use RawDatum (UINT64) to handle buffers < 64 bits */
0878 
0879             Status = AcpiExFieldDatumIo (ObjDesc, 0, &RawDatum, ACPI_READ);
0880             memcpy (Buffer, &RawDatum, BufferLength);
0881         }
0882 
0883         return_ACPI_STATUS (Status);
0884     }
0885 
0886 /* TBD: Move to common setup code */
0887 
0888     /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
0889 
0890     if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
0891     {
0892         ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
0893         AccessBitWidth = sizeof (UINT64) * 8;
0894     }
0895 
0896     /* Compute the number of datums (access width data items) */
0897 
0898     DatumCount = ACPI_ROUND_UP_TO (
0899         ObjDesc->CommonField.BitLength, AccessBitWidth);
0900 
0901     FieldDatumCount = ACPI_ROUND_UP_TO (
0902         ObjDesc->CommonField.BitLength +
0903         ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
0904 
0905     /* Priming read from the field */
0906 
0907     Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
0908     if (ACPI_FAILURE (Status))
0909     {
0910         return_ACPI_STATUS (Status);
0911     }
0912     MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
0913 
0914     /* Read the rest of the field */
0915 
0916     for (i = 1; i < FieldDatumCount; i++)
0917     {
0918         /* Get next input datum from the field */
0919 
0920         FieldOffset += ObjDesc->CommonField.AccessByteWidth;
0921         Status = AcpiExFieldDatumIo (
0922             ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
0923         if (ACPI_FAILURE (Status))
0924         {
0925             return_ACPI_STATUS (Status);
0926         }
0927 
0928         /*
0929          * Merge with previous datum if necessary.
0930          *
0931          * Note: Before the shift, check if the shift value will be larger than
0932          * the integer size. If so, there is no need to perform the operation.
0933          * This avoids the differences in behavior between different compilers
0934          * concerning shift values larger than the target data width.
0935          */
0936         if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
0937             ACPI_INTEGER_BIT_SIZE)
0938         {
0939             MergedDatum |= RawDatum <<
0940                 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
0941         }
0942 
0943         if (i == DatumCount)
0944         {
0945             break;
0946         }
0947 
0948         /* Write merged datum to target buffer */
0949 
0950         memcpy (((char *) Buffer) + BufferOffset, &MergedDatum,
0951             ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
0952                 BufferLength - BufferOffset));
0953 
0954         BufferOffset += ObjDesc->CommonField.AccessByteWidth;
0955         MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
0956     }
0957 
0958     /* Mask off any extra bits in the last datum */
0959 
0960     BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
0961     if (BufferTailBits)
0962     {
0963         MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
0964     }
0965 
0966     /* Write the last datum to the buffer */
0967 
0968     memcpy (((char *) Buffer) + BufferOffset, &MergedDatum,
0969         ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
0970             BufferLength - BufferOffset));
0971 
0972     return_ACPI_STATUS (AE_OK);
0973 }
0974 
0975 
0976 /*******************************************************************************
0977  *
0978  * FUNCTION:    AcpiExInsertIntoField
0979  *
0980  * PARAMETERS:  ObjDesc             - Field to be written
0981  *              Buffer              - Data to be written
0982  *              BufferLength        - Length of Buffer
0983  *
0984  * RETURN:      Status
0985  *
0986  * DESCRIPTION: Store the Buffer contents into the given field
0987  *
0988  ******************************************************************************/
0989 
0990 ACPI_STATUS
0991 AcpiExInsertIntoField (
0992     ACPI_OPERAND_OBJECT     *ObjDesc,
0993     void                    *Buffer,
0994     UINT32                  BufferLength)
0995 {
0996     void                    *NewBuffer;
0997     ACPI_STATUS             Status;
0998     UINT64                  Mask;
0999     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;
1010 
1011 
1012     ACPI_FUNCTION_TRACE (ExInsertIntoField);
1013 
1014 
1015     /* Validate input buffer */
1016 
1017     NewBuffer = NULL;
1018     RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
1019         ObjDesc->CommonField.BitLength);
1020 
1021     /*
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.
1026      */
1027     if (BufferLength < RequiredLength)
1028     {
1029         /* We need to create a new buffer */
1030 
1031         NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
1032         if (!NewBuffer)
1033         {
1034             return_ACPI_STATUS (AE_NO_MEMORY);
1035         }
1036 
1037         /*
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.
1041          */
1042         memcpy ((char *) NewBuffer, (char *) Buffer, BufferLength);
1043         Buffer = NewBuffer;
1044         BufferLength = RequiredLength;
1045     }
1046 
1047 /* TBD: Move to common setup code */
1048 
1049     /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
1050     if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
1051     {
1052         ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
1053     }
1054 
1055     AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
1056 
1057     /* Create the bitmasks used for bit insertion */
1058 
1059     WidthMask = ACPI_MASK_BITS_ABOVE_64 (AccessBitWidth);
1060     Mask = WidthMask &
1061         ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
1062 
1063     /* Compute the number of datums (access width data items) */
1064 
1065     DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
1066         AccessBitWidth);
1067 
1068     FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
1069         ObjDesc->CommonField.StartFieldBitOffset,
1070         AccessBitWidth);
1071 
1072     /* Get initial Datum from the input buffer */
1073 
1074     memcpy (&RawDatum, Buffer,
1075         ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1076             BufferLength - BufferOffset));
1077 
1078     MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1079 
1080     /* Write the entire field */
1081 
1082     for (i = 1; i < FieldDatumCount; i++)
1083     {
1084         /* Write merged datum to the target field */
1085 
1086         MergedDatum &= Mask;
1087         Status = AcpiExWriteWithUpdateRule (
1088             ObjDesc, Mask, MergedDatum, FieldOffset);
1089         if (ACPI_FAILURE (Status))
1090         {
1091             goto Exit;
1092         }
1093 
1094         FieldOffset += ObjDesc->CommonField.AccessByteWidth;
1095 
1096         /*
1097          * Start new output datum by merging with previous input datum
1098          * if necessary.
1099          *
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.
1104          */
1105         if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1106             ACPI_INTEGER_BIT_SIZE)
1107         {
1108             MergedDatum = RawDatum >>
1109                 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1110         }
1111         else
1112         {
1113             MergedDatum = 0;
1114         }
1115 
1116         Mask = WidthMask;
1117 
1118         if (i == DatumCount)
1119         {
1120             break;
1121         }
1122 
1123         /* Get the next input datum from the buffer */
1124 
1125         BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1126         memcpy (&RawDatum, ((char *) Buffer) + BufferOffset,
1127             ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1128                  BufferLength - BufferOffset));
1129 
1130         MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1131     }
1132 
1133     /* Mask off any extra bits in the last datum */
1134 
1135     BufferTailBits = (ObjDesc->CommonField.BitLength +
1136         ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1137     if (BufferTailBits)
1138     {
1139         Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1140     }
1141 
1142     /* Write the last datum to the field */
1143 
1144     MergedDatum &= Mask;
1145     Status = AcpiExWriteWithUpdateRule (
1146         ObjDesc, Mask, MergedDatum, FieldOffset);
1147 
1148 Exit:
1149     /* Free temporary buffer if we used one */
1150 
1151     if (NewBuffer)
1152     {
1153         ACPI_FREE (NewBuffer);
1154     }
1155     return_ACPI_STATUS (Status);
1156 }