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