Home | History | Annotate | Line # | Download | only in executer
exfldio.c revision 1.1.1.3
      1      1.1    jruoho /******************************************************************************
      2      1.1    jruoho  *
      3      1.1    jruoho  * Module Name: exfldio - Aml Field I/O
      4      1.1    jruoho  *
      5      1.1    jruoho  *****************************************************************************/
      6      1.1    jruoho 
      7  1.1.1.2    jruoho /*
      8  1.1.1.3  christos  * Copyright (C) 2000 - 2013, Intel Corp.
      9      1.1    jruoho  * All rights reserved.
     10      1.1    jruoho  *
     11  1.1.1.2    jruoho  * Redistribution and use in source and binary forms, with or without
     12  1.1.1.2    jruoho  * modification, are permitted provided that the following conditions
     13  1.1.1.2    jruoho  * are met:
     14  1.1.1.2    jruoho  * 1. Redistributions of source code must retain the above copyright
     15  1.1.1.2    jruoho  *    notice, this list of conditions, and the following disclaimer,
     16  1.1.1.2    jruoho  *    without modification.
     17  1.1.1.2    jruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18  1.1.1.2    jruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
     19  1.1.1.2    jruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
     20  1.1.1.2    jruoho  *    including a substantially similar Disclaimer requirement for further
     21  1.1.1.2    jruoho  *    binary redistribution.
     22  1.1.1.2    jruoho  * 3. Neither the names of the above-listed copyright holders nor the names
     23  1.1.1.2    jruoho  *    of any contributors may be used to endorse or promote products derived
     24  1.1.1.2    jruoho  *    from this software without specific prior written permission.
     25  1.1.1.2    jruoho  *
     26  1.1.1.2    jruoho  * Alternatively, this software may be distributed under the terms of the
     27  1.1.1.2    jruoho  * GNU General Public License ("GPL") version 2 as published by the Free
     28  1.1.1.2    jruoho  * Software Foundation.
     29  1.1.1.2    jruoho  *
     30  1.1.1.2    jruoho  * NO WARRANTY
     31  1.1.1.2    jruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32  1.1.1.2    jruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33  1.1.1.2    jruoho  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
     34  1.1.1.2    jruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35  1.1.1.2    jruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36  1.1.1.2    jruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37  1.1.1.2    jruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38  1.1.1.2    jruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39  1.1.1.2    jruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40  1.1.1.2    jruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41  1.1.1.2    jruoho  * POSSIBILITY OF SUCH DAMAGES.
     42  1.1.1.2    jruoho  */
     43      1.1    jruoho 
     44      1.1    jruoho 
     45      1.1    jruoho #define __EXFLDIO_C__
     46      1.1    jruoho 
     47      1.1    jruoho #include "acpi.h"
     48      1.1    jruoho #include "accommon.h"
     49      1.1    jruoho #include "acinterp.h"
     50      1.1    jruoho #include "amlcode.h"
     51      1.1    jruoho #include "acevents.h"
     52      1.1    jruoho #include "acdispat.h"
     53      1.1    jruoho 
     54      1.1    jruoho 
     55      1.1    jruoho #define _COMPONENT          ACPI_EXECUTER
     56      1.1    jruoho         ACPI_MODULE_NAME    ("exfldio")
     57      1.1    jruoho 
     58      1.1    jruoho /* Local prototypes */
     59      1.1    jruoho 
     60      1.1    jruoho static ACPI_STATUS
     61      1.1    jruoho AcpiExFieldDatumIo (
     62      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
     63      1.1    jruoho     UINT32                  FieldDatumByteOffset,
     64      1.1    jruoho     UINT64                  *Value,
     65      1.1    jruoho     UINT32                  ReadWrite);
     66      1.1    jruoho 
     67      1.1    jruoho static BOOLEAN
     68      1.1    jruoho AcpiExRegisterOverflow (
     69      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
     70      1.1    jruoho     UINT64                  Value);
     71      1.1    jruoho 
     72      1.1    jruoho static ACPI_STATUS
     73      1.1    jruoho AcpiExSetupRegion (
     74      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
     75      1.1    jruoho     UINT32                  FieldDatumByteOffset);
     76      1.1    jruoho 
     77      1.1    jruoho 
     78      1.1    jruoho /*******************************************************************************
     79      1.1    jruoho  *
     80      1.1    jruoho  * FUNCTION:    AcpiExSetupRegion
     81      1.1    jruoho  *
     82      1.1    jruoho  * PARAMETERS:  ObjDesc                 - Field to be read or written
     83      1.1    jruoho  *              FieldDatumByteOffset    - Byte offset of this datum within the
     84      1.1    jruoho  *                                        parent field
     85      1.1    jruoho  *
     86      1.1    jruoho  * RETURN:      Status
     87      1.1    jruoho  *
     88      1.1    jruoho  * DESCRIPTION: Common processing for AcpiExExtractFromField and
     89  1.1.1.3  christos  *              AcpiExInsertIntoField. Initialize the Region if necessary and
     90      1.1    jruoho  *              validate the request.
     91      1.1    jruoho  *
     92      1.1    jruoho  ******************************************************************************/
     93      1.1    jruoho 
     94      1.1    jruoho static ACPI_STATUS
     95      1.1    jruoho AcpiExSetupRegion (
     96      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
     97      1.1    jruoho     UINT32                  FieldDatumByteOffset)
     98      1.1    jruoho {
     99      1.1    jruoho     ACPI_STATUS             Status = AE_OK;
    100      1.1    jruoho     ACPI_OPERAND_OBJECT     *RgnDesc;
    101  1.1.1.3  christos     UINT8                   SpaceId;
    102      1.1    jruoho 
    103      1.1    jruoho 
    104      1.1    jruoho     ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
    105      1.1    jruoho 
    106      1.1    jruoho 
    107      1.1    jruoho     RgnDesc = ObjDesc->CommonField.RegionObj;
    108      1.1    jruoho 
    109      1.1    jruoho     /* We must have a valid region */
    110      1.1    jruoho 
    111      1.1    jruoho     if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
    112      1.1    jruoho     {
    113      1.1    jruoho         ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
    114      1.1    jruoho             RgnDesc->Common.Type,
    115      1.1    jruoho             AcpiUtGetObjectTypeName (RgnDesc)));
    116      1.1    jruoho 
    117      1.1    jruoho         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
    118      1.1    jruoho     }
    119      1.1    jruoho 
    120  1.1.1.3  christos     SpaceId = RgnDesc->Region.SpaceId;
    121  1.1.1.3  christos 
    122  1.1.1.3  christos     /* Validate the Space ID */
    123  1.1.1.3  christos 
    124  1.1.1.3  christos     if (!AcpiIsValidSpaceId (SpaceId))
    125  1.1.1.3  christos     {
    126  1.1.1.3  christos         ACPI_ERROR ((AE_INFO, "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
    127  1.1.1.3  christos         return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
    128  1.1.1.3  christos     }
    129  1.1.1.3  christos 
    130      1.1    jruoho     /*
    131      1.1    jruoho      * If the Region Address and Length have not been previously evaluated,
    132      1.1    jruoho      * evaluate them now and save the results.
    133      1.1    jruoho      */
    134      1.1    jruoho     if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
    135      1.1    jruoho     {
    136      1.1    jruoho         Status = AcpiDsGetRegionArguments (RgnDesc);
    137      1.1    jruoho         if (ACPI_FAILURE (Status))
    138      1.1    jruoho         {
    139      1.1    jruoho             return_ACPI_STATUS (Status);
    140      1.1    jruoho         }
    141      1.1    jruoho     }
    142      1.1    jruoho 
    143      1.1    jruoho     /*
    144  1.1.1.3  christos      * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
    145      1.1    jruoho      * address space and the request cannot be directly validated
    146      1.1    jruoho      */
    147  1.1.1.3  christos     if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
    148  1.1.1.3  christos         SpaceId == ACPI_ADR_SPACE_GSBUS ||
    149  1.1.1.3  christos         SpaceId == ACPI_ADR_SPACE_IPMI)
    150      1.1    jruoho     {
    151      1.1    jruoho         /* SMBus or IPMI has a non-linear address space */
    152      1.1    jruoho 
    153      1.1    jruoho         return_ACPI_STATUS (AE_OK);
    154      1.1    jruoho     }
    155      1.1    jruoho 
    156      1.1    jruoho #ifdef ACPI_UNDER_DEVELOPMENT
    157      1.1    jruoho     /*
    158      1.1    jruoho      * If the Field access is AnyAcc, we can now compute the optimal
    159      1.1    jruoho      * access (because we know know the length of the parent region)
    160      1.1    jruoho      */
    161      1.1    jruoho     if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
    162      1.1    jruoho     {
    163      1.1    jruoho         if (ACPI_FAILURE (Status))
    164      1.1    jruoho         {
    165      1.1    jruoho             return_ACPI_STATUS (Status);
    166      1.1    jruoho         }
    167      1.1    jruoho     }
    168      1.1    jruoho #endif
    169      1.1    jruoho 
    170      1.1    jruoho     /*
    171  1.1.1.3  christos      * Validate the request. The entire request from the byte offset for a
    172      1.1    jruoho      * length of one field datum (access width) must fit within the region.
    173      1.1    jruoho      * (Region length is specified in bytes)
    174      1.1    jruoho      */
    175      1.1    jruoho     if (RgnDesc->Region.Length <
    176      1.1    jruoho             (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
    177      1.1    jruoho             ObjDesc->CommonField.AccessByteWidth))
    178      1.1    jruoho     {
    179      1.1    jruoho         if (AcpiGbl_EnableInterpreterSlack)
    180      1.1    jruoho         {
    181      1.1    jruoho             /*
    182      1.1    jruoho              * Slack mode only:  We will go ahead and allow access to this
    183      1.1    jruoho              * field if it is within the region length rounded up to the next
    184      1.1    jruoho              * access width boundary. ACPI_SIZE cast for 64-bit compile.
    185      1.1    jruoho              */
    186      1.1    jruoho             if (ACPI_ROUND_UP (RgnDesc->Region.Length,
    187      1.1    jruoho                     ObjDesc->CommonField.AccessByteWidth) >=
    188      1.1    jruoho                 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
    189      1.1    jruoho                     ObjDesc->CommonField.AccessByteWidth +
    190      1.1    jruoho                     FieldDatumByteOffset))
    191      1.1    jruoho             {
    192      1.1    jruoho                 return_ACPI_STATUS (AE_OK);
    193      1.1    jruoho             }
    194      1.1    jruoho         }
    195      1.1    jruoho 
    196      1.1    jruoho         if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
    197      1.1    jruoho         {
    198      1.1    jruoho             /*
    199      1.1    jruoho              * This is the case where the AccessType (AccWord, etc.) is wider
    200  1.1.1.3  christos              * than the region itself. For example, a region of length one
    201      1.1    jruoho              * byte, and a field with Dword access specified.
    202      1.1    jruoho              */
    203      1.1    jruoho             ACPI_ERROR ((AE_INFO,
    204      1.1    jruoho                 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
    205      1.1    jruoho                 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
    206      1.1    jruoho                 ObjDesc->CommonField.AccessByteWidth,
    207      1.1    jruoho                 AcpiUtGetNodeName (RgnDesc->Region.Node),
    208      1.1    jruoho                 RgnDesc->Region.Length));
    209      1.1    jruoho         }
    210      1.1    jruoho 
    211      1.1    jruoho         /*
    212      1.1    jruoho          * Offset rounded up to next multiple of field width
    213      1.1    jruoho          * exceeds region length, indicate an error
    214      1.1    jruoho          */
    215      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    216      1.1    jruoho             "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
    217      1.1    jruoho             AcpiUtGetNodeName (ObjDesc->CommonField.Node),
    218      1.1    jruoho             ObjDesc->CommonField.BaseByteOffset,
    219      1.1    jruoho             FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
    220      1.1    jruoho             AcpiUtGetNodeName (RgnDesc->Region.Node),
    221      1.1    jruoho             RgnDesc->Region.Length));
    222      1.1    jruoho 
    223      1.1    jruoho         return_ACPI_STATUS (AE_AML_REGION_LIMIT);
    224      1.1    jruoho     }
    225      1.1    jruoho 
    226      1.1    jruoho     return_ACPI_STATUS (AE_OK);
    227      1.1    jruoho }
    228      1.1    jruoho 
    229      1.1    jruoho 
    230      1.1    jruoho /*******************************************************************************
    231      1.1    jruoho  *
    232      1.1    jruoho  * FUNCTION:    AcpiExAccessRegion
    233      1.1    jruoho  *
    234      1.1    jruoho  * PARAMETERS:  ObjDesc                 - Field to be read
    235      1.1    jruoho  *              FieldDatumByteOffset    - Byte offset of this datum within the
    236      1.1    jruoho  *                                        parent field
    237      1.1    jruoho  *              Value                   - Where to store value (must at least
    238      1.1    jruoho  *                                        64 bits)
    239      1.1    jruoho  *              Function                - Read or Write flag plus other region-
    240      1.1    jruoho  *                                        dependent flags
    241      1.1    jruoho  *
    242      1.1    jruoho  * RETURN:      Status
    243      1.1    jruoho  *
    244      1.1    jruoho  * DESCRIPTION: Read or Write a single field datum to an Operation Region.
    245      1.1    jruoho  *
    246      1.1    jruoho  ******************************************************************************/
    247      1.1    jruoho 
    248      1.1    jruoho ACPI_STATUS
    249      1.1    jruoho AcpiExAccessRegion (
    250      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    251      1.1    jruoho     UINT32                  FieldDatumByteOffset,
    252      1.1    jruoho     UINT64                  *Value,
    253      1.1    jruoho     UINT32                  Function)
    254      1.1    jruoho {
    255      1.1    jruoho     ACPI_STATUS             Status;
    256      1.1    jruoho     ACPI_OPERAND_OBJECT     *RgnDesc;
    257      1.1    jruoho     UINT32                  RegionOffset;
    258      1.1    jruoho 
    259      1.1    jruoho 
    260      1.1    jruoho     ACPI_FUNCTION_TRACE (ExAccessRegion);
    261      1.1    jruoho 
    262      1.1    jruoho 
    263      1.1    jruoho     /*
    264      1.1    jruoho      * Ensure that the region operands are fully evaluated and verify
    265      1.1    jruoho      * the validity of the request
    266      1.1    jruoho      */
    267      1.1    jruoho     Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
    268      1.1    jruoho     if (ACPI_FAILURE (Status))
    269      1.1    jruoho     {
    270      1.1    jruoho         return_ACPI_STATUS (Status);
    271      1.1    jruoho     }
    272      1.1    jruoho 
    273      1.1    jruoho     /*
    274      1.1    jruoho      * The physical address of this field datum is:
    275      1.1    jruoho      *
    276      1.1    jruoho      * 1) The base of the region, plus
    277      1.1    jruoho      * 2) The base offset of the field, plus
    278      1.1    jruoho      * 3) The current offset into the field
    279      1.1    jruoho      */
    280      1.1    jruoho     RgnDesc = ObjDesc->CommonField.RegionObj;
    281      1.1    jruoho     RegionOffset =
    282      1.1    jruoho         ObjDesc->CommonField.BaseByteOffset +
    283      1.1    jruoho         FieldDatumByteOffset;
    284      1.1    jruoho 
    285      1.1    jruoho     if ((Function & ACPI_IO_MASK) == ACPI_READ)
    286      1.1    jruoho     {
    287      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
    288      1.1    jruoho     }
    289      1.1    jruoho     else
    290      1.1    jruoho     {
    291      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
    292      1.1    jruoho     }
    293      1.1    jruoho 
    294      1.1    jruoho     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
    295      1.1    jruoho         " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
    296      1.1    jruoho         AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
    297      1.1    jruoho         RgnDesc->Region.SpaceId,
    298      1.1    jruoho         ObjDesc->CommonField.AccessByteWidth,
    299      1.1    jruoho         ObjDesc->CommonField.BaseByteOffset,
    300      1.1    jruoho         FieldDatumByteOffset,
    301      1.1    jruoho         ACPI_CAST_PTR (void, (RgnDesc->Region.Address + RegionOffset))));
    302      1.1    jruoho 
    303      1.1    jruoho     /* Invoke the appropriate AddressSpace/OpRegion handler */
    304      1.1    jruoho 
    305  1.1.1.3  christos     Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
    306  1.1.1.3  christos                 Function, RegionOffset,
    307      1.1    jruoho                 ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
    308      1.1    jruoho 
    309      1.1    jruoho     if (ACPI_FAILURE (Status))
    310      1.1    jruoho     {
    311      1.1    jruoho         if (Status == AE_NOT_IMPLEMENTED)
    312      1.1    jruoho         {
    313      1.1    jruoho             ACPI_ERROR ((AE_INFO,
    314  1.1.1.2    jruoho                 "Region %s (ID=%u) not implemented",
    315      1.1    jruoho                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
    316      1.1    jruoho                 RgnDesc->Region.SpaceId));
    317      1.1    jruoho         }
    318      1.1    jruoho         else if (Status == AE_NOT_EXIST)
    319      1.1    jruoho         {
    320      1.1    jruoho             ACPI_ERROR ((AE_INFO,
    321  1.1.1.2    jruoho                 "Region %s (ID=%u) has no handler",
    322      1.1    jruoho                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
    323      1.1    jruoho                 RgnDesc->Region.SpaceId));
    324      1.1    jruoho         }
    325      1.1    jruoho     }
    326      1.1    jruoho 
    327      1.1    jruoho     return_ACPI_STATUS (Status);
    328      1.1    jruoho }
    329      1.1    jruoho 
    330      1.1    jruoho 
    331      1.1    jruoho /*******************************************************************************
    332      1.1    jruoho  *
    333      1.1    jruoho  * FUNCTION:    AcpiExRegisterOverflow
    334      1.1    jruoho  *
    335      1.1    jruoho  * PARAMETERS:  ObjDesc                 - Register(Field) to be written
    336      1.1    jruoho  *              Value                   - Value to be stored
    337      1.1    jruoho  *
    338      1.1    jruoho  * RETURN:      TRUE if value overflows the field, FALSE otherwise
    339      1.1    jruoho  *
    340      1.1    jruoho  * DESCRIPTION: Check if a value is out of range of the field being written.
    341      1.1    jruoho  *              Used to check if the values written to Index and Bank registers
    342  1.1.1.3  christos  *              are out of range. Normally, the value is simply truncated
    343      1.1    jruoho  *              to fit the field, but this case is most likely a serious
    344      1.1    jruoho  *              coding error in the ASL.
    345      1.1    jruoho  *
    346      1.1    jruoho  ******************************************************************************/
    347      1.1    jruoho 
    348      1.1    jruoho static BOOLEAN
    349      1.1    jruoho AcpiExRegisterOverflow (
    350      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    351      1.1    jruoho     UINT64                  Value)
    352      1.1    jruoho {
    353      1.1    jruoho 
    354      1.1    jruoho     if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
    355      1.1    jruoho     {
    356      1.1    jruoho         /*
    357      1.1    jruoho          * The field is large enough to hold the maximum integer, so we can
    358      1.1    jruoho          * never overflow it.
    359      1.1    jruoho          */
    360      1.1    jruoho         return (FALSE);
    361      1.1    jruoho     }
    362      1.1    jruoho 
    363      1.1    jruoho     if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
    364      1.1    jruoho     {
    365      1.1    jruoho         /*
    366      1.1    jruoho          * The Value is larger than the maximum value that can fit into
    367      1.1    jruoho          * the register.
    368      1.1    jruoho          */
    369  1.1.1.3  christos         ACPI_ERROR ((AE_INFO,
    370  1.1.1.3  christos             "Index value 0x%8.8X%8.8X overflows field width 0x%X",
    371  1.1.1.3  christos             ACPI_FORMAT_UINT64 (Value),
    372  1.1.1.3  christos             ObjDesc->CommonField.BitLength));
    373  1.1.1.3  christos 
    374      1.1    jruoho         return (TRUE);
    375      1.1    jruoho     }
    376      1.1    jruoho 
    377      1.1    jruoho     /* The Value will fit into the field with no truncation */
    378      1.1    jruoho 
    379      1.1    jruoho     return (FALSE);
    380      1.1    jruoho }
    381      1.1    jruoho 
    382      1.1    jruoho 
    383      1.1    jruoho /*******************************************************************************
    384      1.1    jruoho  *
    385      1.1    jruoho  * FUNCTION:    AcpiExFieldDatumIo
    386      1.1    jruoho  *
    387      1.1    jruoho  * PARAMETERS:  ObjDesc                 - Field to be read
    388      1.1    jruoho  *              FieldDatumByteOffset    - Byte offset of this datum within the
    389      1.1    jruoho  *                                        parent field
    390      1.1    jruoho  *              Value                   - Where to store value (must be 64 bits)
    391      1.1    jruoho  *              ReadWrite               - Read or Write flag
    392      1.1    jruoho  *
    393      1.1    jruoho  * RETURN:      Status
    394      1.1    jruoho  *
    395  1.1.1.3  christos  * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
    396      1.1    jruoho  *              demultiplexed here to handle the different types of fields
    397      1.1    jruoho  *              (BufferField, RegionField, IndexField, BankField)
    398      1.1    jruoho  *
    399      1.1    jruoho  ******************************************************************************/
    400      1.1    jruoho 
    401      1.1    jruoho static ACPI_STATUS
    402      1.1    jruoho AcpiExFieldDatumIo (
    403      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    404      1.1    jruoho     UINT32                  FieldDatumByteOffset,
    405      1.1    jruoho     UINT64                  *Value,
    406      1.1    jruoho     UINT32                  ReadWrite)
    407      1.1    jruoho {
    408      1.1    jruoho     ACPI_STATUS             Status;
    409      1.1    jruoho     UINT64                  LocalValue;
    410      1.1    jruoho 
    411      1.1    jruoho 
    412      1.1    jruoho     ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
    413      1.1    jruoho 
    414      1.1    jruoho 
    415      1.1    jruoho     if (ReadWrite == ACPI_READ)
    416      1.1    jruoho     {
    417      1.1    jruoho         if (!Value)
    418      1.1    jruoho         {
    419      1.1    jruoho             LocalValue = 0;
    420      1.1    jruoho 
    421      1.1    jruoho             /* To support reads without saving return value */
    422      1.1    jruoho             Value = &LocalValue;
    423      1.1    jruoho         }
    424      1.1    jruoho 
    425      1.1    jruoho         /* Clear the entire return buffer first, [Very Important!] */
    426      1.1    jruoho 
    427      1.1    jruoho         *Value = 0;
    428      1.1    jruoho     }
    429      1.1    jruoho 
    430      1.1    jruoho     /*
    431      1.1    jruoho      * The four types of fields are:
    432      1.1    jruoho      *
    433      1.1    jruoho      * BufferField - Read/write from/to a Buffer
    434      1.1    jruoho      * RegionField - Read/write from/to a Operation Region.
    435      1.1    jruoho      * BankField   - Write to a Bank Register, then read/write from/to an
    436      1.1    jruoho      *               OperationRegion
    437      1.1    jruoho      * IndexField  - Write to an Index Register, then read/write from/to a
    438      1.1    jruoho      *               Data Register
    439      1.1    jruoho      */
    440      1.1    jruoho     switch (ObjDesc->Common.Type)
    441      1.1    jruoho     {
    442      1.1    jruoho     case ACPI_TYPE_BUFFER_FIELD:
    443      1.1    jruoho         /*
    444      1.1    jruoho          * If the BufferField arguments have not been previously evaluated,
    445      1.1    jruoho          * evaluate them now and save the results.
    446      1.1    jruoho          */
    447      1.1    jruoho         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
    448      1.1    jruoho         {
    449      1.1    jruoho             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
    450      1.1    jruoho             if (ACPI_FAILURE (Status))
    451      1.1    jruoho             {
    452      1.1    jruoho                 return_ACPI_STATUS (Status);
    453      1.1    jruoho             }
    454      1.1    jruoho         }
    455      1.1    jruoho 
    456      1.1    jruoho         if (ReadWrite == ACPI_READ)
    457      1.1    jruoho         {
    458      1.1    jruoho             /*
    459      1.1    jruoho              * Copy the data from the source buffer.
    460      1.1    jruoho              * Length is the field width in bytes.
    461      1.1    jruoho              */
    462      1.1    jruoho             ACPI_MEMCPY (Value,
    463      1.1    jruoho                 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
    464      1.1    jruoho                     ObjDesc->BufferField.BaseByteOffset +
    465      1.1    jruoho                     FieldDatumByteOffset,
    466      1.1    jruoho                 ObjDesc->CommonField.AccessByteWidth);
    467      1.1    jruoho         }
    468      1.1    jruoho         else
    469      1.1    jruoho         {
    470      1.1    jruoho             /*
    471      1.1    jruoho              * Copy the data to the target buffer.
    472      1.1    jruoho              * Length is the field width in bytes.
    473      1.1    jruoho              */
    474      1.1    jruoho             ACPI_MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
    475      1.1    jruoho                 ObjDesc->BufferField.BaseByteOffset +
    476      1.1    jruoho                 FieldDatumByteOffset,
    477      1.1    jruoho                 Value, ObjDesc->CommonField.AccessByteWidth);
    478      1.1    jruoho         }
    479      1.1    jruoho 
    480      1.1    jruoho         Status = AE_OK;
    481      1.1    jruoho         break;
    482      1.1    jruoho 
    483      1.1    jruoho     case ACPI_TYPE_LOCAL_BANK_FIELD:
    484      1.1    jruoho         /*
    485      1.1    jruoho          * Ensure that the BankValue is not beyond the capacity of
    486      1.1    jruoho          * the register
    487      1.1    jruoho          */
    488      1.1    jruoho         if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
    489      1.1    jruoho                 (UINT64) ObjDesc->BankField.Value))
    490      1.1    jruoho         {
    491      1.1    jruoho             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
    492      1.1    jruoho         }
    493      1.1    jruoho 
    494      1.1    jruoho         /*
    495      1.1    jruoho          * For BankFields, we must write the BankValue to the BankRegister
    496      1.1    jruoho          * (itself a RegionField) before we can access the data.
    497      1.1    jruoho          */
    498      1.1    jruoho         Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
    499      1.1    jruoho                     &ObjDesc->BankField.Value,
    500      1.1    jruoho                     sizeof (ObjDesc->BankField.Value));
    501      1.1    jruoho         if (ACPI_FAILURE (Status))
    502      1.1    jruoho         {
    503      1.1    jruoho             return_ACPI_STATUS (Status);
    504      1.1    jruoho         }
    505      1.1    jruoho 
    506      1.1    jruoho         /*
    507      1.1    jruoho          * Now that the Bank has been selected, fall through to the
    508      1.1    jruoho          * RegionField case and write the datum to the Operation Region
    509      1.1    jruoho          */
    510      1.1    jruoho 
    511      1.1    jruoho         /*lint -fallthrough */
    512      1.1    jruoho 
    513      1.1    jruoho     case ACPI_TYPE_LOCAL_REGION_FIELD:
    514      1.1    jruoho         /*
    515      1.1    jruoho          * For simple RegionFields, we just directly access the owning
    516      1.1    jruoho          * Operation Region.
    517      1.1    jruoho          */
    518      1.1    jruoho         Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
    519      1.1    jruoho                     ReadWrite);
    520      1.1    jruoho         break;
    521      1.1    jruoho 
    522      1.1    jruoho     case ACPI_TYPE_LOCAL_INDEX_FIELD:
    523      1.1    jruoho         /*
    524      1.1    jruoho          * Ensure that the IndexValue is not beyond the capacity of
    525      1.1    jruoho          * the register
    526      1.1    jruoho          */
    527      1.1    jruoho         if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
    528      1.1    jruoho                 (UINT64) ObjDesc->IndexField.Value))
    529      1.1    jruoho         {
    530      1.1    jruoho             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
    531      1.1    jruoho         }
    532      1.1    jruoho 
    533      1.1    jruoho         /* Write the index value to the IndexRegister (itself a RegionField) */
    534      1.1    jruoho 
    535      1.1    jruoho         FieldDatumByteOffset += ObjDesc->IndexField.Value;
    536      1.1    jruoho 
    537      1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
    538      1.1    jruoho             "Write to Index Register: Value %8.8X\n",
    539      1.1    jruoho             FieldDatumByteOffset));
    540      1.1    jruoho 
    541      1.1    jruoho         Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
    542      1.1    jruoho                     &FieldDatumByteOffset,
    543      1.1    jruoho                     sizeof (FieldDatumByteOffset));
    544      1.1    jruoho         if (ACPI_FAILURE (Status))
    545      1.1    jruoho         {
    546      1.1    jruoho             return_ACPI_STATUS (Status);
    547      1.1    jruoho         }
    548      1.1    jruoho 
    549      1.1    jruoho         if (ReadWrite == ACPI_READ)
    550      1.1    jruoho         {
    551      1.1    jruoho             /* Read the datum from the DataRegister */
    552      1.1    jruoho 
    553      1.1    jruoho             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
    554      1.1    jruoho                 "Read from Data Register\n"));
    555      1.1    jruoho 
    556      1.1    jruoho             Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
    557      1.1    jruoho                         Value, sizeof (UINT64));
    558      1.1    jruoho         }
    559      1.1    jruoho         else
    560      1.1    jruoho         {
    561      1.1    jruoho             /* Write the datum to the DataRegister */
    562      1.1    jruoho 
    563      1.1    jruoho             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
    564      1.1    jruoho                 "Write to Data Register: Value %8.8X%8.8X\n",
    565      1.1    jruoho                 ACPI_FORMAT_UINT64 (*Value)));
    566      1.1    jruoho 
    567      1.1    jruoho             Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
    568      1.1    jruoho                         Value, sizeof (UINT64));
    569      1.1    jruoho         }
    570      1.1    jruoho         break;
    571      1.1    jruoho 
    572      1.1    jruoho     default:
    573      1.1    jruoho 
    574      1.1    jruoho         ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
    575      1.1    jruoho             ObjDesc->Common.Type));
    576      1.1    jruoho         Status = AE_AML_INTERNAL;
    577      1.1    jruoho         break;
    578      1.1    jruoho     }
    579      1.1    jruoho 
    580      1.1    jruoho     if (ACPI_SUCCESS (Status))
    581      1.1    jruoho     {
    582      1.1    jruoho         if (ReadWrite == ACPI_READ)
    583      1.1    jruoho         {
    584      1.1    jruoho             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
    585      1.1    jruoho                 "Value Read %8.8X%8.8X, Width %u\n",
    586      1.1    jruoho                 ACPI_FORMAT_UINT64 (*Value),
    587      1.1    jruoho                 ObjDesc->CommonField.AccessByteWidth));
    588      1.1    jruoho         }
    589      1.1    jruoho         else
    590      1.1    jruoho         {
    591      1.1    jruoho             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
    592      1.1    jruoho                 "Value Written %8.8X%8.8X, Width %u\n",
    593      1.1    jruoho                 ACPI_FORMAT_UINT64 (*Value),
    594      1.1    jruoho                 ObjDesc->CommonField.AccessByteWidth));
    595      1.1    jruoho         }
    596      1.1    jruoho     }
    597      1.1    jruoho 
    598      1.1    jruoho     return_ACPI_STATUS (Status);
    599      1.1    jruoho }
    600      1.1    jruoho 
    601      1.1    jruoho 
    602      1.1    jruoho /*******************************************************************************
    603      1.1    jruoho  *
    604      1.1    jruoho  * FUNCTION:    AcpiExWriteWithUpdateRule
    605      1.1    jruoho  *
    606      1.1    jruoho  * PARAMETERS:  ObjDesc                 - Field to be written
    607      1.1    jruoho  *              Mask                    - bitmask within field datum
    608      1.1    jruoho  *              FieldValue              - Value to write
    609      1.1    jruoho  *              FieldDatumByteOffset    - Offset of datum within field
    610      1.1    jruoho  *
    611      1.1    jruoho  * RETURN:      Status
    612      1.1    jruoho  *
    613      1.1    jruoho  * DESCRIPTION: Apply the field update rule to a field write
    614      1.1    jruoho  *
    615      1.1    jruoho  ******************************************************************************/
    616      1.1    jruoho 
    617      1.1    jruoho ACPI_STATUS
    618      1.1    jruoho AcpiExWriteWithUpdateRule (
    619      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    620      1.1    jruoho     UINT64                  Mask,
    621      1.1    jruoho     UINT64                  FieldValue,
    622      1.1    jruoho     UINT32                  FieldDatumByteOffset)
    623      1.1    jruoho {
    624      1.1    jruoho     ACPI_STATUS             Status = AE_OK;
    625      1.1    jruoho     UINT64                  MergedValue;
    626      1.1    jruoho     UINT64                  CurrentValue;
    627      1.1    jruoho 
    628      1.1    jruoho 
    629      1.1    jruoho     ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
    630      1.1    jruoho 
    631      1.1    jruoho 
    632      1.1    jruoho     /* Start with the new bits  */
    633      1.1    jruoho 
    634      1.1    jruoho     MergedValue = FieldValue;
    635      1.1    jruoho 
    636      1.1    jruoho     /* If the mask is all ones, we don't need to worry about the update rule */
    637      1.1    jruoho 
    638      1.1    jruoho     if (Mask != ACPI_UINT64_MAX)
    639      1.1    jruoho     {
    640      1.1    jruoho         /* Decode the update rule */
    641      1.1    jruoho 
    642      1.1    jruoho         switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
    643      1.1    jruoho         {
    644      1.1    jruoho         case AML_FIELD_UPDATE_PRESERVE:
    645      1.1    jruoho             /*
    646      1.1    jruoho              * Check if update rule needs to be applied (not if mask is all
    647      1.1    jruoho              * ones)  The left shift drops the bits we want to ignore.
    648      1.1    jruoho              */
    649      1.1    jruoho             if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
    650      1.1    jruoho                            ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
    651      1.1    jruoho             {
    652      1.1    jruoho                 /*
    653      1.1    jruoho                  * Read the current contents of the byte/word/dword containing
    654      1.1    jruoho                  * the field, and merge with the new field value.
    655      1.1    jruoho                  */
    656      1.1    jruoho                 Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
    657      1.1    jruoho                             &CurrentValue, ACPI_READ);
    658      1.1    jruoho                 if (ACPI_FAILURE (Status))
    659      1.1    jruoho                 {
    660      1.1    jruoho                     return_ACPI_STATUS (Status);
    661      1.1    jruoho                 }
    662      1.1    jruoho 
    663      1.1    jruoho                 MergedValue |= (CurrentValue & ~Mask);
    664      1.1    jruoho             }
    665      1.1    jruoho             break;
    666      1.1    jruoho 
    667      1.1    jruoho         case AML_FIELD_UPDATE_WRITE_AS_ONES:
    668      1.1    jruoho 
    669      1.1    jruoho             /* Set positions outside the field to all ones */
    670      1.1    jruoho 
    671      1.1    jruoho             MergedValue |= ~Mask;
    672      1.1    jruoho             break;
    673      1.1    jruoho 
    674      1.1    jruoho         case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
    675      1.1    jruoho 
    676      1.1    jruoho             /* Set positions outside the field to all zeros */
    677      1.1    jruoho 
    678      1.1    jruoho             MergedValue &= Mask;
    679      1.1    jruoho             break;
    680      1.1    jruoho 
    681      1.1    jruoho         default:
    682      1.1    jruoho 
    683      1.1    jruoho             ACPI_ERROR ((AE_INFO,
    684      1.1    jruoho                 "Unknown UpdateRule value: 0x%X",
    685      1.1    jruoho                 (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
    686      1.1    jruoho             return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
    687      1.1    jruoho         }
    688      1.1    jruoho     }
    689      1.1    jruoho 
    690      1.1    jruoho     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
    691      1.1    jruoho         "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
    692      1.1    jruoho         ACPI_FORMAT_UINT64 (Mask),
    693      1.1    jruoho         FieldDatumByteOffset,
    694      1.1    jruoho         ObjDesc->CommonField.AccessByteWidth,
    695      1.1    jruoho         ACPI_FORMAT_UINT64 (FieldValue),
    696      1.1    jruoho         ACPI_FORMAT_UINT64 (MergedValue)));
    697      1.1    jruoho 
    698      1.1    jruoho     /* Write the merged value */
    699      1.1    jruoho 
    700      1.1    jruoho     Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
    701      1.1    jruoho                 &MergedValue, ACPI_WRITE);
    702      1.1    jruoho 
    703      1.1    jruoho     return_ACPI_STATUS (Status);
    704      1.1    jruoho }
    705      1.1    jruoho 
    706      1.1    jruoho 
    707      1.1    jruoho /*******************************************************************************
    708      1.1    jruoho  *
    709      1.1    jruoho  * FUNCTION:    AcpiExExtractFromField
    710      1.1    jruoho  *
    711      1.1    jruoho  * PARAMETERS:  ObjDesc             - Field to be read
    712      1.1    jruoho  *              Buffer              - Where to store the field data
    713      1.1    jruoho  *              BufferLength        - Length of Buffer
    714      1.1    jruoho  *
    715      1.1    jruoho  * RETURN:      Status
    716      1.1    jruoho  *
    717      1.1    jruoho  * DESCRIPTION: Retrieve the current value of the given field
    718      1.1    jruoho  *
    719      1.1    jruoho  ******************************************************************************/
    720      1.1    jruoho 
    721      1.1    jruoho ACPI_STATUS
    722      1.1    jruoho AcpiExExtractFromField (
    723      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    724      1.1    jruoho     void                    *Buffer,
    725      1.1    jruoho     UINT32                  BufferLength)
    726      1.1    jruoho {
    727      1.1    jruoho     ACPI_STATUS             Status;
    728      1.1    jruoho     UINT64                  RawDatum;
    729      1.1    jruoho     UINT64                  MergedDatum;
    730      1.1    jruoho     UINT32                  FieldOffset = 0;
    731      1.1    jruoho     UINT32                  BufferOffset = 0;
    732      1.1    jruoho     UINT32                  BufferTailBits;
    733      1.1    jruoho     UINT32                  DatumCount;
    734      1.1    jruoho     UINT32                  FieldDatumCount;
    735      1.1    jruoho     UINT32                  AccessBitWidth;
    736      1.1    jruoho     UINT32                  i;
    737      1.1    jruoho 
    738      1.1    jruoho 
    739      1.1    jruoho     ACPI_FUNCTION_TRACE (ExExtractFromField);
    740      1.1    jruoho 
    741      1.1    jruoho 
    742      1.1    jruoho     /* Validate target buffer and clear it */
    743      1.1    jruoho 
    744      1.1    jruoho     if (BufferLength <
    745      1.1    jruoho         ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
    746      1.1    jruoho     {
    747      1.1    jruoho         ACPI_ERROR ((AE_INFO,
    748      1.1    jruoho             "Field size %u (bits) is too large for buffer (%u)",
    749      1.1    jruoho             ObjDesc->CommonField.BitLength, BufferLength));
    750      1.1    jruoho 
    751      1.1    jruoho         return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
    752      1.1    jruoho     }
    753      1.1    jruoho 
    754      1.1    jruoho     ACPI_MEMSET (Buffer, 0, BufferLength);
    755      1.1    jruoho     AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
    756      1.1    jruoho 
    757      1.1    jruoho     /* Handle the simple case here */
    758      1.1    jruoho 
    759      1.1    jruoho     if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
    760      1.1    jruoho         (ObjDesc->CommonField.BitLength == AccessBitWidth))
    761      1.1    jruoho     {
    762  1.1.1.3  christos         if (BufferLength >= sizeof (UINT64))
    763  1.1.1.3  christos         {
    764  1.1.1.3  christos             Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
    765  1.1.1.3  christos         }
    766  1.1.1.3  christos         else
    767  1.1.1.3  christos         {
    768  1.1.1.3  christos             /* Use RawDatum (UINT64) to handle buffers < 64 bits */
    769  1.1.1.3  christos 
    770  1.1.1.3  christos             Status = AcpiExFieldDatumIo (ObjDesc, 0, &RawDatum, ACPI_READ);
    771  1.1.1.3  christos             ACPI_MEMCPY (Buffer, &RawDatum, BufferLength);
    772  1.1.1.3  christos         }
    773  1.1.1.3  christos 
    774      1.1    jruoho         return_ACPI_STATUS (Status);
    775      1.1    jruoho     }
    776      1.1    jruoho 
    777      1.1    jruoho /* TBD: Move to common setup code */
    778      1.1    jruoho 
    779      1.1    jruoho     /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
    780      1.1    jruoho 
    781      1.1    jruoho     if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
    782      1.1    jruoho     {
    783      1.1    jruoho         ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
    784      1.1    jruoho         AccessBitWidth = sizeof (UINT64) * 8;
    785      1.1    jruoho     }
    786      1.1    jruoho 
    787      1.1    jruoho     /* Compute the number of datums (access width data items) */
    788      1.1    jruoho 
    789      1.1    jruoho     DatumCount = ACPI_ROUND_UP_TO (
    790      1.1    jruoho         ObjDesc->CommonField.BitLength, AccessBitWidth);
    791      1.1    jruoho 
    792      1.1    jruoho     FieldDatumCount = ACPI_ROUND_UP_TO (
    793      1.1    jruoho         ObjDesc->CommonField.BitLength +
    794      1.1    jruoho         ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
    795      1.1    jruoho 
    796      1.1    jruoho     /* Priming read from the field */
    797      1.1    jruoho 
    798      1.1    jruoho     Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
    799      1.1    jruoho     if (ACPI_FAILURE (Status))
    800      1.1    jruoho     {
    801      1.1    jruoho         return_ACPI_STATUS (Status);
    802      1.1    jruoho     }
    803      1.1    jruoho     MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
    804      1.1    jruoho 
    805      1.1    jruoho     /* Read the rest of the field */
    806      1.1    jruoho 
    807      1.1    jruoho     for (i = 1; i < FieldDatumCount; i++)
    808      1.1    jruoho     {
    809      1.1    jruoho         /* Get next input datum from the field */
    810      1.1    jruoho 
    811      1.1    jruoho         FieldOffset += ObjDesc->CommonField.AccessByteWidth;
    812      1.1    jruoho         Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
    813      1.1    jruoho                     &RawDatum, ACPI_READ);
    814      1.1    jruoho         if (ACPI_FAILURE (Status))
    815      1.1    jruoho         {
    816      1.1    jruoho             return_ACPI_STATUS (Status);
    817      1.1    jruoho         }
    818      1.1    jruoho 
    819      1.1    jruoho         /*
    820      1.1    jruoho          * Merge with previous datum if necessary.
    821      1.1    jruoho          *
    822      1.1    jruoho          * Note: Before the shift, check if the shift value will be larger than
    823      1.1    jruoho          * the integer size. If so, there is no need to perform the operation.
    824      1.1    jruoho          * This avoids the differences in behavior between different compilers
    825      1.1    jruoho          * concerning shift values larger than the target data width.
    826      1.1    jruoho          */
    827      1.1    jruoho         if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
    828      1.1    jruoho             ACPI_INTEGER_BIT_SIZE)
    829      1.1    jruoho         {
    830      1.1    jruoho             MergedDatum |= RawDatum <<
    831      1.1    jruoho                 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
    832      1.1    jruoho         }
    833      1.1    jruoho 
    834      1.1    jruoho         if (i == DatumCount)
    835      1.1    jruoho         {
    836      1.1    jruoho             break;
    837      1.1    jruoho         }
    838      1.1    jruoho 
    839      1.1    jruoho         /* Write merged datum to target buffer */
    840      1.1    jruoho 
    841      1.1    jruoho         ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
    842      1.1    jruoho             ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
    843      1.1    jruoho                 BufferLength - BufferOffset));
    844      1.1    jruoho 
    845      1.1    jruoho         BufferOffset += ObjDesc->CommonField.AccessByteWidth;
    846      1.1    jruoho         MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
    847      1.1    jruoho     }
    848      1.1    jruoho 
    849      1.1    jruoho     /* Mask off any extra bits in the last datum */
    850      1.1    jruoho 
    851      1.1    jruoho     BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
    852      1.1    jruoho     if (BufferTailBits)
    853      1.1    jruoho     {
    854      1.1    jruoho         MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
    855      1.1    jruoho     }
    856      1.1    jruoho 
    857      1.1    jruoho     /* Write the last datum to the buffer */
    858      1.1    jruoho 
    859      1.1    jruoho     ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
    860      1.1    jruoho         ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
    861      1.1    jruoho             BufferLength - BufferOffset));
    862      1.1    jruoho 
    863      1.1    jruoho     return_ACPI_STATUS (AE_OK);
    864      1.1    jruoho }
    865      1.1    jruoho 
    866      1.1    jruoho 
    867      1.1    jruoho /*******************************************************************************
    868      1.1    jruoho  *
    869      1.1    jruoho  * FUNCTION:    AcpiExInsertIntoField
    870      1.1    jruoho  *
    871      1.1    jruoho  * PARAMETERS:  ObjDesc             - Field to be written
    872      1.1    jruoho  *              Buffer              - Data to be written
    873      1.1    jruoho  *              BufferLength        - Length of Buffer
    874      1.1    jruoho  *
    875      1.1    jruoho  * RETURN:      Status
    876      1.1    jruoho  *
    877      1.1    jruoho  * DESCRIPTION: Store the Buffer contents into the given field
    878      1.1    jruoho  *
    879      1.1    jruoho  ******************************************************************************/
    880      1.1    jruoho 
    881      1.1    jruoho ACPI_STATUS
    882      1.1    jruoho AcpiExInsertIntoField (
    883      1.1    jruoho     ACPI_OPERAND_OBJECT     *ObjDesc,
    884      1.1    jruoho     void                    *Buffer,
    885      1.1    jruoho     UINT32                  BufferLength)
    886      1.1    jruoho {
    887      1.1    jruoho     void                    *NewBuffer;
    888      1.1    jruoho     ACPI_STATUS             Status;
    889      1.1    jruoho     UINT64                  Mask;
    890      1.1    jruoho     UINT64                  WidthMask;
    891      1.1    jruoho     UINT64                  MergedDatum;
    892      1.1    jruoho     UINT64                  RawDatum = 0;
    893      1.1    jruoho     UINT32                  FieldOffset = 0;
    894      1.1    jruoho     UINT32                  BufferOffset = 0;
    895      1.1    jruoho     UINT32                  BufferTailBits;
    896      1.1    jruoho     UINT32                  DatumCount;
    897      1.1    jruoho     UINT32                  FieldDatumCount;
    898      1.1    jruoho     UINT32                  AccessBitWidth;
    899      1.1    jruoho     UINT32                  RequiredLength;
    900      1.1    jruoho     UINT32                  i;
    901      1.1    jruoho 
    902      1.1    jruoho 
    903      1.1    jruoho     ACPI_FUNCTION_TRACE (ExInsertIntoField);
    904      1.1    jruoho 
    905      1.1    jruoho 
    906      1.1    jruoho     /* Validate input buffer */
    907      1.1    jruoho 
    908      1.1    jruoho     NewBuffer = NULL;
    909      1.1    jruoho     RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
    910      1.1    jruoho                         ObjDesc->CommonField.BitLength);
    911      1.1    jruoho     /*
    912      1.1    jruoho      * We must have a buffer that is at least as long as the field
    913  1.1.1.3  christos      * we are writing to. This is because individual fields are
    914      1.1    jruoho      * indivisible and partial writes are not supported -- as per
    915      1.1    jruoho      * the ACPI specification.
    916      1.1    jruoho      */
    917      1.1    jruoho     if (BufferLength < RequiredLength)
    918      1.1    jruoho     {
    919      1.1    jruoho         /* We need to create a new buffer */
    920      1.1    jruoho 
    921      1.1    jruoho         NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
    922      1.1    jruoho         if (!NewBuffer)
    923      1.1    jruoho         {
    924      1.1    jruoho             return_ACPI_STATUS (AE_NO_MEMORY);
    925      1.1    jruoho         }
    926      1.1    jruoho 
    927      1.1    jruoho         /*
    928      1.1    jruoho          * Copy the original data to the new buffer, starting
    929  1.1.1.3  christos          * at Byte zero. All unused (upper) bytes of the
    930      1.1    jruoho          * buffer will be 0.
    931      1.1    jruoho          */
    932      1.1    jruoho         ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
    933      1.1    jruoho         Buffer = NewBuffer;
    934      1.1    jruoho         BufferLength = RequiredLength;
    935      1.1    jruoho     }
    936      1.1    jruoho 
    937      1.1    jruoho /* TBD: Move to common setup code */
    938      1.1    jruoho 
    939      1.1    jruoho     /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
    940      1.1    jruoho     if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
    941      1.1    jruoho     {
    942      1.1    jruoho         ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
    943      1.1    jruoho     }
    944      1.1    jruoho 
    945      1.1    jruoho     AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
    946      1.1    jruoho 
    947      1.1    jruoho     /*
    948      1.1    jruoho      * Create the bitmasks used for bit insertion.
    949      1.1    jruoho      * Note: This if/else is used to bypass compiler differences with the
    950      1.1    jruoho      * shift operator
    951      1.1    jruoho      */
    952      1.1    jruoho     if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
    953      1.1    jruoho     {
    954      1.1    jruoho         WidthMask = ACPI_UINT64_MAX;
    955      1.1    jruoho     }
    956      1.1    jruoho     else
    957      1.1    jruoho     {
    958      1.1    jruoho         WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
    959      1.1    jruoho     }
    960      1.1    jruoho 
    961      1.1    jruoho     Mask = WidthMask &
    962      1.1    jruoho         ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
    963      1.1    jruoho 
    964      1.1    jruoho     /* Compute the number of datums (access width data items) */
    965      1.1    jruoho 
    966      1.1    jruoho     DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
    967      1.1    jruoho         AccessBitWidth);
    968      1.1    jruoho 
    969      1.1    jruoho     FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
    970      1.1    jruoho         ObjDesc->CommonField.StartFieldBitOffset,
    971      1.1    jruoho         AccessBitWidth);
    972      1.1    jruoho 
    973      1.1    jruoho     /* Get initial Datum from the input buffer */
    974      1.1    jruoho 
    975      1.1    jruoho     ACPI_MEMCPY (&RawDatum, Buffer,
    976      1.1    jruoho         ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
    977      1.1    jruoho             BufferLength - BufferOffset));
    978      1.1    jruoho 
    979      1.1    jruoho     MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
    980      1.1    jruoho 
    981      1.1    jruoho     /* Write the entire field */
    982      1.1    jruoho 
    983      1.1    jruoho     for (i = 1; i < FieldDatumCount; i++)
    984      1.1    jruoho     {
    985      1.1    jruoho         /* Write merged datum to the target field */
    986      1.1    jruoho 
    987      1.1    jruoho         MergedDatum &= Mask;
    988      1.1    jruoho         Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
    989      1.1    jruoho                     MergedDatum, FieldOffset);
    990      1.1    jruoho         if (ACPI_FAILURE (Status))
    991      1.1    jruoho         {
    992      1.1    jruoho             goto Exit;
    993      1.1    jruoho         }
    994      1.1    jruoho 
    995      1.1    jruoho         FieldOffset += ObjDesc->CommonField.AccessByteWidth;
    996      1.1    jruoho 
    997      1.1    jruoho         /*
    998      1.1    jruoho          * Start new output datum by merging with previous input datum
    999      1.1    jruoho          * if necessary.
   1000      1.1    jruoho          *
   1001      1.1    jruoho          * Note: Before the shift, check if the shift value will be larger than
   1002      1.1    jruoho          * the integer size. If so, there is no need to perform the operation.
   1003      1.1    jruoho          * This avoids the differences in behavior between different compilers
   1004      1.1    jruoho          * concerning shift values larger than the target data width.
   1005      1.1    jruoho          */
   1006      1.1    jruoho         if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
   1007      1.1    jruoho             ACPI_INTEGER_BIT_SIZE)
   1008      1.1    jruoho         {
   1009      1.1    jruoho             MergedDatum = RawDatum >>
   1010      1.1    jruoho                 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
   1011      1.1    jruoho         }
   1012      1.1    jruoho         else
   1013      1.1    jruoho         {
   1014      1.1    jruoho             MergedDatum = 0;
   1015      1.1    jruoho         }
   1016      1.1    jruoho 
   1017      1.1    jruoho         Mask = WidthMask;
   1018      1.1    jruoho 
   1019      1.1    jruoho         if (i == DatumCount)
   1020      1.1    jruoho         {
   1021      1.1    jruoho             break;
   1022      1.1    jruoho         }
   1023      1.1    jruoho 
   1024      1.1    jruoho         /* Get the next input datum from the buffer */
   1025      1.1    jruoho 
   1026      1.1    jruoho         BufferOffset += ObjDesc->CommonField.AccessByteWidth;
   1027      1.1    jruoho         ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
   1028      1.1    jruoho             ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
   1029      1.1    jruoho                  BufferLength - BufferOffset));
   1030      1.1    jruoho 
   1031      1.1    jruoho         MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
   1032      1.1    jruoho     }
   1033      1.1    jruoho 
   1034      1.1    jruoho     /* Mask off any extra bits in the last datum */
   1035      1.1    jruoho 
   1036      1.1    jruoho     BufferTailBits = (ObjDesc->CommonField.BitLength +
   1037      1.1    jruoho         ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
   1038      1.1    jruoho     if (BufferTailBits)
   1039      1.1    jruoho     {
   1040      1.1    jruoho         Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
   1041      1.1    jruoho     }
   1042      1.1    jruoho 
   1043      1.1    jruoho     /* Write the last datum to the field */
   1044      1.1    jruoho 
   1045      1.1    jruoho     MergedDatum &= Mask;
   1046      1.1    jruoho     Status = AcpiExWriteWithUpdateRule (ObjDesc,
   1047      1.1    jruoho                 Mask, MergedDatum, FieldOffset);
   1048      1.1    jruoho 
   1049      1.1    jruoho Exit:
   1050      1.1    jruoho     /* Free temporary buffer if we used one */
   1051      1.1    jruoho 
   1052      1.1    jruoho     if (NewBuffer)
   1053      1.1    jruoho     {
   1054      1.1    jruoho         ACPI_FREE (NewBuffer);
   1055      1.1    jruoho     }
   1056      1.1    jruoho     return_ACPI_STATUS (Status);
   1057      1.1    jruoho }
   1058