xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/executer/exfldio.c (revision 6dde014f768e0ee49bae871ed857cf433837b9ff)
10174267aSFredrik Holmqvist /******************************************************************************
20174267aSFredrik Holmqvist  *
30174267aSFredrik Holmqvist  * Module Name: exfldio - Aml Field I/O
40174267aSFredrik Holmqvist  *
50174267aSFredrik Holmqvist  *****************************************************************************/
60174267aSFredrik Holmqvist 
70174267aSFredrik Holmqvist /******************************************************************************
80174267aSFredrik Holmqvist  *
90174267aSFredrik Holmqvist  * 1. Copyright Notice
100174267aSFredrik Holmqvist  *
11*6dde014fSPulkoMandy  * Some or all of this work - Copyright (c) 1999 - 2024, Intel Corp.
120174267aSFredrik Holmqvist  * All rights reserved.
130174267aSFredrik Holmqvist  *
140174267aSFredrik Holmqvist  * 2. License
150174267aSFredrik Holmqvist  *
160174267aSFredrik Holmqvist  * 2.1. This is your license from Intel Corp. under its intellectual property
170174267aSFredrik Holmqvist  * rights. You may have additional license terms from the party that provided
180174267aSFredrik Holmqvist  * you this software, covering your right to use that party's intellectual
190174267aSFredrik Holmqvist  * property rights.
200174267aSFredrik Holmqvist  *
210174267aSFredrik Holmqvist  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
220174267aSFredrik Holmqvist  * copy of the source code appearing in this file ("Covered Code") an
230174267aSFredrik Holmqvist  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
240174267aSFredrik Holmqvist  * base code distributed originally by Intel ("Original Intel Code") to copy,
250174267aSFredrik Holmqvist  * make derivatives, distribute, use and display any portion of the Covered
260174267aSFredrik Holmqvist  * Code in any form, with the right to sublicense such rights; and
270174267aSFredrik Holmqvist  *
280174267aSFredrik Holmqvist  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
290174267aSFredrik Holmqvist  * license (with the right to sublicense), under only those claims of Intel
300174267aSFredrik Holmqvist  * patents that are infringed by the Original Intel Code, to make, use, sell,
310174267aSFredrik Holmqvist  * offer to sell, and import the Covered Code and derivative works thereof
320174267aSFredrik Holmqvist  * solely to the minimum extent necessary to exercise the above copyright
330174267aSFredrik Holmqvist  * license, and in no event shall the patent license extend to any additions
340174267aSFredrik Holmqvist  * to or modifications of the Original Intel Code. No other license or right
350174267aSFredrik Holmqvist  * is granted directly or by implication, estoppel or otherwise;
360174267aSFredrik Holmqvist  *
370174267aSFredrik Holmqvist  * The above copyright and patent license is granted only if the following
380174267aSFredrik Holmqvist  * conditions are met:
390174267aSFredrik Holmqvist  *
400174267aSFredrik Holmqvist  * 3. Conditions
410174267aSFredrik Holmqvist  *
420174267aSFredrik Holmqvist  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
430174267aSFredrik Holmqvist  * Redistribution of source code of any substantial portion of the Covered
440174267aSFredrik Holmqvist  * Code or modification with rights to further distribute source must include
450174267aSFredrik Holmqvist  * the above Copyright Notice, the above License, this list of Conditions,
460174267aSFredrik Holmqvist  * and the following Disclaimer and Export Compliance provision. In addition,
470174267aSFredrik Holmqvist  * Licensee must cause all Covered Code to which Licensee contributes to
480174267aSFredrik Holmqvist  * contain a file documenting the changes Licensee made to create that Covered
490174267aSFredrik Holmqvist  * Code and the date of any change. Licensee must include in that file the
500174267aSFredrik Holmqvist  * documentation of any changes made by any predecessor Licensee. Licensee
510174267aSFredrik Holmqvist  * must include a prominent statement that the modification is derived,
520174267aSFredrik Holmqvist  * directly or indirectly, from Original Intel Code.
530174267aSFredrik Holmqvist  *
540174267aSFredrik Holmqvist  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
550174267aSFredrik Holmqvist  * Redistribution of source code of any substantial portion of the Covered
560174267aSFredrik Holmqvist  * Code or modification without rights to further distribute source must
570174267aSFredrik Holmqvist  * include the following Disclaimer and Export Compliance provision in the
580174267aSFredrik Holmqvist  * documentation and/or other materials provided with distribution. In
590174267aSFredrik Holmqvist  * addition, Licensee may not authorize further sublicense of source of any
600174267aSFredrik Holmqvist  * portion of the Covered Code, and must include terms to the effect that the
610174267aSFredrik Holmqvist  * license from Licensee to its licensee is limited to the intellectual
620174267aSFredrik Holmqvist  * property embodied in the software Licensee provides to its licensee, and
630174267aSFredrik Holmqvist  * not to intellectual property embodied in modifications its licensee may
640174267aSFredrik Holmqvist  * make.
650174267aSFredrik Holmqvist  *
660174267aSFredrik Holmqvist  * 3.3. Redistribution of Executable. Redistribution in executable form of any
670174267aSFredrik Holmqvist  * substantial portion of the Covered Code or modification must reproduce the
680174267aSFredrik Holmqvist  * above Copyright Notice, and the following Disclaimer and Export Compliance
690174267aSFredrik Holmqvist  * provision in the documentation and/or other materials provided with the
700174267aSFredrik Holmqvist  * distribution.
710174267aSFredrik Holmqvist  *
720174267aSFredrik Holmqvist  * 3.4. Intel retains all right, title, and interest in and to the Original
730174267aSFredrik Holmqvist  * Intel Code.
740174267aSFredrik Holmqvist  *
750174267aSFredrik Holmqvist  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
760174267aSFredrik Holmqvist  * Intel shall be used in advertising or otherwise to promote the sale, use or
770174267aSFredrik Holmqvist  * other dealings in products derived from or relating to the Covered Code
780174267aSFredrik Holmqvist  * without prior written authorization from Intel.
790174267aSFredrik Holmqvist  *
800174267aSFredrik Holmqvist  * 4. Disclaimer and Export Compliance
810174267aSFredrik Holmqvist  *
820174267aSFredrik Holmqvist  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
830174267aSFredrik Holmqvist  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
840174267aSFredrik Holmqvist  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
850174267aSFredrik Holmqvist  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
860174267aSFredrik Holmqvist  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
870174267aSFredrik Holmqvist  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
880174267aSFredrik Holmqvist  * PARTICULAR PURPOSE.
890174267aSFredrik Holmqvist  *
900174267aSFredrik Holmqvist  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
910174267aSFredrik Holmqvist  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
920174267aSFredrik Holmqvist  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
930174267aSFredrik Holmqvist  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
940174267aSFredrik Holmqvist  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
950174267aSFredrik Holmqvist  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
960174267aSFredrik Holmqvist  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
970174267aSFredrik Holmqvist  * LIMITED REMEDY.
980174267aSFredrik Holmqvist  *
990174267aSFredrik Holmqvist  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1000174267aSFredrik Holmqvist  * software or system incorporating such software without first obtaining any
1010174267aSFredrik Holmqvist  * required license or other approval from the U. S. Department of Commerce or
1020174267aSFredrik Holmqvist  * any other agency or department of the United States Government. In the
1030174267aSFredrik Holmqvist  * event Licensee exports any such software from the United States or
1040174267aSFredrik Holmqvist  * re-exports any such software from a foreign destination, Licensee shall
1050174267aSFredrik Holmqvist  * ensure that the distribution and export/re-export of the software is in
1060174267aSFredrik Holmqvist  * compliance with all laws, regulations, orders, or other restrictions of the
1070174267aSFredrik Holmqvist  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1080174267aSFredrik Holmqvist  * any of its subsidiaries will export/re-export any technical data, process,
1090174267aSFredrik Holmqvist  * software, or service, directly or indirectly, to any country for which the
1100174267aSFredrik Holmqvist  * United States government or any agency thereof requires an export license,
1110174267aSFredrik Holmqvist  * other governmental approval, or letter of assurance, without first obtaining
1120174267aSFredrik Holmqvist  * such license, approval or letter.
1130174267aSFredrik Holmqvist  *
114ff2e2f81SFredrik Holmqvist  *****************************************************************************
115ff2e2f81SFredrik Holmqvist  *
116ff2e2f81SFredrik Holmqvist  * Alternatively, you may choose to be licensed under the terms of the
117ff2e2f81SFredrik Holmqvist  * following license:
118ff2e2f81SFredrik Holmqvist  *
119ff2e2f81SFredrik Holmqvist  * Redistribution and use in source and binary forms, with or without
120ff2e2f81SFredrik Holmqvist  * modification, are permitted provided that the following conditions
121ff2e2f81SFredrik Holmqvist  * are met:
122ff2e2f81SFredrik Holmqvist  * 1. Redistributions of source code must retain the above copyright
123ff2e2f81SFredrik Holmqvist  *    notice, this list of conditions, and the following disclaimer,
124ff2e2f81SFredrik Holmqvist  *    without modification.
125ff2e2f81SFredrik Holmqvist  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126ff2e2f81SFredrik Holmqvist  *    substantially similar to the "NO WARRANTY" disclaimer below
127ff2e2f81SFredrik Holmqvist  *    ("Disclaimer") and any redistribution must be conditioned upon
128ff2e2f81SFredrik Holmqvist  *    including a substantially similar Disclaimer requirement for further
129ff2e2f81SFredrik Holmqvist  *    binary redistribution.
130ff2e2f81SFredrik Holmqvist  * 3. Neither the names of the above-listed copyright holders nor the names
131ff2e2f81SFredrik Holmqvist  *    of any contributors may be used to endorse or promote products derived
132ff2e2f81SFredrik Holmqvist  *    from this software without specific prior written permission.
133ff2e2f81SFredrik Holmqvist  *
134ff2e2f81SFredrik Holmqvist  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135ff2e2f81SFredrik Holmqvist  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136ff2e2f81SFredrik Holmqvist  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137ff2e2f81SFredrik Holmqvist  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138ff2e2f81SFredrik Holmqvist  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139ff2e2f81SFredrik Holmqvist  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140ff2e2f81SFredrik Holmqvist  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141ff2e2f81SFredrik Holmqvist  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142ff2e2f81SFredrik Holmqvist  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143ff2e2f81SFredrik Holmqvist  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144ff2e2f81SFredrik Holmqvist  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145ff2e2f81SFredrik Holmqvist  *
146ff2e2f81SFredrik Holmqvist  * Alternatively, you may choose to be licensed under the terms of the
147ff2e2f81SFredrik Holmqvist  * GNU General Public License ("GPL") version 2 as published by the Free
148ff2e2f81SFredrik Holmqvist  * Software Foundation.
149ff2e2f81SFredrik Holmqvist  *
1500174267aSFredrik Holmqvist  *****************************************************************************/
1510174267aSFredrik Holmqvist 
1520174267aSFredrik Holmqvist #include "acpi.h"
1530174267aSFredrik Holmqvist #include "accommon.h"
1540174267aSFredrik Holmqvist #include "acinterp.h"
1550174267aSFredrik Holmqvist #include "amlcode.h"
1560174267aSFredrik Holmqvist #include "acevents.h"
1570174267aSFredrik Holmqvist #include "acdispat.h"
1580174267aSFredrik Holmqvist 
1590174267aSFredrik Holmqvist 
1600174267aSFredrik Holmqvist #define _COMPONENT          ACPI_EXECUTER
1610174267aSFredrik Holmqvist         ACPI_MODULE_NAME    ("exfldio")
1620174267aSFredrik Holmqvist 
1630174267aSFredrik Holmqvist /* Local prototypes */
1640174267aSFredrik Holmqvist 
1650174267aSFredrik Holmqvist static ACPI_STATUS
1660174267aSFredrik Holmqvist AcpiExFieldDatumIo (
1670174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
1680174267aSFredrik Holmqvist     UINT32                  FieldDatumByteOffset,
1690174267aSFredrik Holmqvist     UINT64                  *Value,
1700174267aSFredrik Holmqvist     UINT32                  ReadWrite);
1710174267aSFredrik Holmqvist 
1720174267aSFredrik Holmqvist static BOOLEAN
1730174267aSFredrik Holmqvist AcpiExRegisterOverflow (
1740174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
1750174267aSFredrik Holmqvist     UINT64                  Value);
1760174267aSFredrik Holmqvist 
1770174267aSFredrik Holmqvist static ACPI_STATUS
1780174267aSFredrik Holmqvist AcpiExSetupRegion (
1790174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
1800174267aSFredrik Holmqvist     UINT32                  FieldDatumByteOffset);
1810174267aSFredrik Holmqvist 
1820174267aSFredrik Holmqvist 
1830174267aSFredrik Holmqvist /*******************************************************************************
1840174267aSFredrik Holmqvist  *
1850174267aSFredrik Holmqvist  * FUNCTION:    AcpiExSetupRegion
1860174267aSFredrik Holmqvist  *
1870174267aSFredrik Holmqvist  * PARAMETERS:  ObjDesc                 - Field to be read or written
1880174267aSFredrik Holmqvist  *              FieldDatumByteOffset    - Byte offset of this datum within the
1890174267aSFredrik Holmqvist  *                                        parent field
1900174267aSFredrik Holmqvist  *
1910174267aSFredrik Holmqvist  * RETURN:      Status
1920174267aSFredrik Holmqvist  *
1930174267aSFredrik Holmqvist  * DESCRIPTION: Common processing for AcpiExExtractFromField and
1940174267aSFredrik Holmqvist  *              AcpiExInsertIntoField. Initialize the Region if necessary and
1950174267aSFredrik Holmqvist  *              validate the request.
1960174267aSFredrik Holmqvist  *
1970174267aSFredrik Holmqvist  ******************************************************************************/
1980174267aSFredrik Holmqvist 
1990174267aSFredrik Holmqvist static ACPI_STATUS
AcpiExSetupRegion(ACPI_OPERAND_OBJECT * ObjDesc,UINT32 FieldDatumByteOffset)2000174267aSFredrik Holmqvist AcpiExSetupRegion (
2010174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
2020174267aSFredrik Holmqvist     UINT32                  FieldDatumByteOffset)
2030174267aSFredrik Holmqvist {
2040174267aSFredrik Holmqvist     ACPI_STATUS             Status = AE_OK;
2050174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *RgnDesc;
2066822cda0SFredrik Holmqvist     UINT8                   SpaceId;
2070174267aSFredrik Holmqvist 
2080174267aSFredrik Holmqvist 
2090174267aSFredrik Holmqvist     ACPI_FUNCTION_TRACE_U32 (ExSetupRegion, FieldDatumByteOffset);
2100174267aSFredrik Holmqvist 
2110174267aSFredrik Holmqvist 
2120174267aSFredrik Holmqvist     RgnDesc = ObjDesc->CommonField.RegionObj;
2130174267aSFredrik Holmqvist 
2140174267aSFredrik Holmqvist     /* We must have a valid region */
2150174267aSFredrik Holmqvist 
2160174267aSFredrik Holmqvist     if (RgnDesc->Common.Type != ACPI_TYPE_REGION)
2170174267aSFredrik Holmqvist     {
2180174267aSFredrik Holmqvist         ACPI_ERROR ((AE_INFO, "Needed Region, found type 0x%X (%s)",
2190174267aSFredrik Holmqvist             RgnDesc->Common.Type,
2200174267aSFredrik Holmqvist             AcpiUtGetObjectTypeName (RgnDesc)));
2210174267aSFredrik Holmqvist 
2220174267aSFredrik Holmqvist         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
2230174267aSFredrik Holmqvist     }
2240174267aSFredrik Holmqvist 
2256822cda0SFredrik Holmqvist     SpaceId = RgnDesc->Region.SpaceId;
2266822cda0SFredrik Holmqvist 
2276822cda0SFredrik Holmqvist     /* Validate the Space ID */
2286822cda0SFredrik Holmqvist 
2296822cda0SFredrik Holmqvist     if (!AcpiIsValidSpaceId (SpaceId))
2306822cda0SFredrik Holmqvist     {
231b64e8511SFredrik Holmqvist         ACPI_ERROR ((AE_INFO,
232b64e8511SFredrik Holmqvist             "Invalid/unknown Address Space ID: 0x%2.2X", SpaceId));
2336822cda0SFredrik Holmqvist         return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
2346822cda0SFredrik Holmqvist     }
2356822cda0SFredrik Holmqvist 
2360174267aSFredrik Holmqvist     /*
2370174267aSFredrik Holmqvist      * If the Region Address and Length have not been previously evaluated,
2380174267aSFredrik Holmqvist      * evaluate them now and save the results.
2390174267aSFredrik Holmqvist      */
2400174267aSFredrik Holmqvist     if (!(RgnDesc->Common.Flags & AOPOBJ_DATA_VALID))
2410174267aSFredrik Holmqvist     {
2420174267aSFredrik Holmqvist         Status = AcpiDsGetRegionArguments (RgnDesc);
2430174267aSFredrik Holmqvist         if (ACPI_FAILURE (Status))
2440174267aSFredrik Holmqvist         {
2450174267aSFredrik Holmqvist             return_ACPI_STATUS (Status);
2460174267aSFredrik Holmqvist         }
2470174267aSFredrik Holmqvist     }
2480174267aSFredrik Holmqvist 
2490174267aSFredrik Holmqvist     /*
2506822cda0SFredrik Holmqvist      * Exit now for SMBus, GSBus or IPMI address space, it has a non-linear
2510174267aSFredrik Holmqvist      * address space and the request cannot be directly validated
2520174267aSFredrik Holmqvist      */
2536822cda0SFredrik Holmqvist     if (SpaceId == ACPI_ADR_SPACE_SMBUS ||
2546822cda0SFredrik Holmqvist         SpaceId == ACPI_ADR_SPACE_GSBUS ||
2556822cda0SFredrik Holmqvist         SpaceId == ACPI_ADR_SPACE_IPMI)
2560174267aSFredrik Holmqvist     {
2570174267aSFredrik Holmqvist         /* SMBus or IPMI has a non-linear address space */
2580174267aSFredrik Holmqvist 
2590174267aSFredrik Holmqvist         return_ACPI_STATUS (AE_OK);
2600174267aSFredrik Holmqvist     }
2610174267aSFredrik Holmqvist 
2620174267aSFredrik Holmqvist #ifdef ACPI_UNDER_DEVELOPMENT
2630174267aSFredrik Holmqvist     /*
2640174267aSFredrik Holmqvist      * If the Field access is AnyAcc, we can now compute the optimal
26515338de9SPulkoMandy      * access (because we know the length of the parent region)
2660174267aSFredrik Holmqvist      */
2670174267aSFredrik Holmqvist     if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
2680174267aSFredrik Holmqvist     {
2690174267aSFredrik Holmqvist         if (ACPI_FAILURE (Status))
2700174267aSFredrik Holmqvist         {
2710174267aSFredrik Holmqvist             return_ACPI_STATUS (Status);
2720174267aSFredrik Holmqvist         }
2730174267aSFredrik Holmqvist     }
2740174267aSFredrik Holmqvist #endif
2750174267aSFredrik Holmqvist 
2760174267aSFredrik Holmqvist     /*
2770174267aSFredrik Holmqvist      * Validate the request. The entire request from the byte offset for a
2780174267aSFredrik Holmqvist      * length of one field datum (access width) must fit within the region.
2790174267aSFredrik Holmqvist      * (Region length is specified in bytes)
2800174267aSFredrik Holmqvist      */
2810174267aSFredrik Holmqvist     if (RgnDesc->Region.Length <
2820174267aSFredrik Holmqvist         (ObjDesc->CommonField.BaseByteOffset + FieldDatumByteOffset +
2830174267aSFredrik Holmqvist         ObjDesc->CommonField.AccessByteWidth))
2840174267aSFredrik Holmqvist     {
2850174267aSFredrik Holmqvist         if (AcpiGbl_EnableInterpreterSlack)
2860174267aSFredrik Holmqvist         {
2870174267aSFredrik Holmqvist             /*
2880174267aSFredrik Holmqvist              * Slack mode only:  We will go ahead and allow access to this
2890174267aSFredrik Holmqvist              * field if it is within the region length rounded up to the next
2900174267aSFredrik Holmqvist              * access width boundary. ACPI_SIZE cast for 64-bit compile.
2910174267aSFredrik Holmqvist              */
2920174267aSFredrik Holmqvist             if (ACPI_ROUND_UP (RgnDesc->Region.Length,
2930174267aSFredrik Holmqvist                     ObjDesc->CommonField.AccessByteWidth) >=
2940174267aSFredrik Holmqvist                 ((ACPI_SIZE) ObjDesc->CommonField.BaseByteOffset +
2950174267aSFredrik Holmqvist                     ObjDesc->CommonField.AccessByteWidth +
2960174267aSFredrik Holmqvist                     FieldDatumByteOffset))
2970174267aSFredrik Holmqvist             {
2980174267aSFredrik Holmqvist                 return_ACPI_STATUS (AE_OK);
2990174267aSFredrik Holmqvist             }
3000174267aSFredrik Holmqvist         }
3010174267aSFredrik Holmqvist 
3020174267aSFredrik Holmqvist         if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
3030174267aSFredrik Holmqvist         {
3040174267aSFredrik Holmqvist             /*
3050174267aSFredrik Holmqvist              * This is the case where the AccessType (AccWord, etc.) is wider
3060174267aSFredrik Holmqvist              * than the region itself. For example, a region of length one
3070174267aSFredrik Holmqvist              * byte, and a field with Dword access specified.
3080174267aSFredrik Holmqvist              */
3090174267aSFredrik Holmqvist             ACPI_ERROR ((AE_INFO,
310b64e8511SFredrik Holmqvist                 "Field [%4.4s] access width (%u bytes) "
311b64e8511SFredrik Holmqvist                 "too large for region [%4.4s] (length %u)",
3120174267aSFredrik Holmqvist                 AcpiUtGetNodeName (ObjDesc->CommonField.Node),
3130174267aSFredrik Holmqvist                 ObjDesc->CommonField.AccessByteWidth,
3140174267aSFredrik Holmqvist                 AcpiUtGetNodeName (RgnDesc->Region.Node),
3150174267aSFredrik Holmqvist                 RgnDesc->Region.Length));
3160174267aSFredrik Holmqvist         }
3170174267aSFredrik Holmqvist 
3180174267aSFredrik Holmqvist         /*
3190174267aSFredrik Holmqvist          * Offset rounded up to next multiple of field width
3200174267aSFredrik Holmqvist          * exceeds region length, indicate an error
3210174267aSFredrik Holmqvist          */
3220174267aSFredrik Holmqvist         ACPI_ERROR ((AE_INFO,
323b64e8511SFredrik Holmqvist             "Field [%4.4s] Base+Offset+Width %u+%u+%u "
324b64e8511SFredrik Holmqvist             "is beyond end of region [%4.4s] (length %u)",
3250174267aSFredrik Holmqvist             AcpiUtGetNodeName (ObjDesc->CommonField.Node),
3260174267aSFredrik Holmqvist             ObjDesc->CommonField.BaseByteOffset,
3270174267aSFredrik Holmqvist             FieldDatumByteOffset, ObjDesc->CommonField.AccessByteWidth,
3280174267aSFredrik Holmqvist             AcpiUtGetNodeName (RgnDesc->Region.Node),
3290174267aSFredrik Holmqvist             RgnDesc->Region.Length));
3300174267aSFredrik Holmqvist 
3310174267aSFredrik Holmqvist         return_ACPI_STATUS (AE_AML_REGION_LIMIT);
3320174267aSFredrik Holmqvist     }
3330174267aSFredrik Holmqvist 
3340174267aSFredrik Holmqvist     return_ACPI_STATUS (AE_OK);
3350174267aSFredrik Holmqvist }
3360174267aSFredrik Holmqvist 
3370174267aSFredrik Holmqvist 
3380174267aSFredrik Holmqvist /*******************************************************************************
3390174267aSFredrik Holmqvist  *
3400174267aSFredrik Holmqvist  * FUNCTION:    AcpiExAccessRegion
3410174267aSFredrik Holmqvist  *
3420174267aSFredrik Holmqvist  * PARAMETERS:  ObjDesc                 - Field to be read
3430174267aSFredrik Holmqvist  *              FieldDatumByteOffset    - Byte offset of this datum within the
3440174267aSFredrik Holmqvist  *                                        parent field
3450174267aSFredrik Holmqvist  *              Value                   - Where to store value (must at least
3460174267aSFredrik Holmqvist  *                                        64 bits)
3470174267aSFredrik Holmqvist  *              Function                - Read or Write flag plus other region-
3480174267aSFredrik Holmqvist  *                                        dependent flags
3490174267aSFredrik Holmqvist  *
3500174267aSFredrik Holmqvist  * RETURN:      Status
3510174267aSFredrik Holmqvist  *
3520174267aSFredrik Holmqvist  * DESCRIPTION: Read or Write a single field datum to an Operation Region.
3530174267aSFredrik Holmqvist  *
3540174267aSFredrik Holmqvist  ******************************************************************************/
3550174267aSFredrik Holmqvist 
3560174267aSFredrik Holmqvist ACPI_STATUS
AcpiExAccessRegion(ACPI_OPERAND_OBJECT * ObjDesc,UINT32 FieldDatumByteOffset,UINT64 * Value,UINT32 Function)3570174267aSFredrik Holmqvist AcpiExAccessRegion (
3580174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
3590174267aSFredrik Holmqvist     UINT32                  FieldDatumByteOffset,
3600174267aSFredrik Holmqvist     UINT64                  *Value,
3610174267aSFredrik Holmqvist     UINT32                  Function)
3620174267aSFredrik Holmqvist {
3630174267aSFredrik Holmqvist     ACPI_STATUS             Status;
3640174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *RgnDesc;
3650174267aSFredrik Holmqvist     UINT32                  RegionOffset;
3660174267aSFredrik Holmqvist 
3670174267aSFredrik Holmqvist 
3680174267aSFredrik Holmqvist     ACPI_FUNCTION_TRACE (ExAccessRegion);
3690174267aSFredrik Holmqvist 
3700174267aSFredrik Holmqvist 
3710174267aSFredrik Holmqvist     /*
3720174267aSFredrik Holmqvist      * Ensure that the region operands are fully evaluated and verify
3730174267aSFredrik Holmqvist      * the validity of the request
3740174267aSFredrik Holmqvist      */
3750174267aSFredrik Holmqvist     Status = AcpiExSetupRegion (ObjDesc, FieldDatumByteOffset);
3760174267aSFredrik Holmqvist     if (ACPI_FAILURE (Status))
3770174267aSFredrik Holmqvist     {
3780174267aSFredrik Holmqvist         return_ACPI_STATUS (Status);
3790174267aSFredrik Holmqvist     }
3800174267aSFredrik Holmqvist 
3810174267aSFredrik Holmqvist     /*
3820174267aSFredrik Holmqvist      * The physical address of this field datum is:
3830174267aSFredrik Holmqvist      *
3840174267aSFredrik Holmqvist      * 1) The base of the region, plus
3850174267aSFredrik Holmqvist      * 2) The base offset of the field, plus
3860174267aSFredrik Holmqvist      * 3) The current offset into the field
3870174267aSFredrik Holmqvist      */
3880174267aSFredrik Holmqvist     RgnDesc = ObjDesc->CommonField.RegionObj;
3890174267aSFredrik Holmqvist     RegionOffset =
3900174267aSFredrik Holmqvist         ObjDesc->CommonField.BaseByteOffset +
3910174267aSFredrik Holmqvist         FieldDatumByteOffset;
3920174267aSFredrik Holmqvist 
3930174267aSFredrik Holmqvist     if ((Function & ACPI_IO_MASK) == ACPI_READ)
3940174267aSFredrik Holmqvist     {
3950174267aSFredrik Holmqvist         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
3960174267aSFredrik Holmqvist     }
3970174267aSFredrik Holmqvist     else
3980174267aSFredrik Holmqvist     {
3990174267aSFredrik Holmqvist         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
4000174267aSFredrik Holmqvist     }
4010174267aSFredrik Holmqvist 
4020174267aSFredrik Holmqvist     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
403e226d1d0SFredrik Holmqvist         " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %8.8X%8.8X\n",
4040174267aSFredrik Holmqvist         AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
4050174267aSFredrik Holmqvist         RgnDesc->Region.SpaceId,
4060174267aSFredrik Holmqvist         ObjDesc->CommonField.AccessByteWidth,
4070174267aSFredrik Holmqvist         ObjDesc->CommonField.BaseByteOffset,
4080174267aSFredrik Holmqvist         FieldDatumByteOffset,
409e226d1d0SFredrik Holmqvist         ACPI_FORMAT_UINT64 (RgnDesc->Region.Address + RegionOffset)));
4100174267aSFredrik Holmqvist 
4110174267aSFredrik Holmqvist     /* Invoke the appropriate AddressSpace/OpRegion handler */
4120174267aSFredrik Holmqvist 
4136822cda0SFredrik Holmqvist     Status = AcpiEvAddressSpaceDispatch (RgnDesc, ObjDesc,
4146822cda0SFredrik Holmqvist         Function, RegionOffset,
4150174267aSFredrik Holmqvist         ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth), Value);
4160174267aSFredrik Holmqvist 
4170174267aSFredrik Holmqvist     if (ACPI_FAILURE (Status))
4180174267aSFredrik Holmqvist     {
4190174267aSFredrik Holmqvist         if (Status == AE_NOT_IMPLEMENTED)
4200174267aSFredrik Holmqvist         {
4210174267aSFredrik Holmqvist             ACPI_ERROR ((AE_INFO,
4220174267aSFredrik Holmqvist                 "Region %s (ID=%u) not implemented",
4230174267aSFredrik Holmqvist                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
4240174267aSFredrik Holmqvist                 RgnDesc->Region.SpaceId));
4250174267aSFredrik Holmqvist         }
4260174267aSFredrik Holmqvist         else if (Status == AE_NOT_EXIST)
4270174267aSFredrik Holmqvist         {
4280174267aSFredrik Holmqvist             ACPI_ERROR ((AE_INFO,
4290174267aSFredrik Holmqvist                 "Region %s (ID=%u) has no handler",
4300174267aSFredrik Holmqvist                 AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
4310174267aSFredrik Holmqvist                 RgnDesc->Region.SpaceId));
4320174267aSFredrik Holmqvist         }
4330174267aSFredrik Holmqvist     }
4340174267aSFredrik Holmqvist 
4350174267aSFredrik Holmqvist     return_ACPI_STATUS (Status);
4360174267aSFredrik Holmqvist }
4370174267aSFredrik Holmqvist 
4380174267aSFredrik Holmqvist 
4390174267aSFredrik Holmqvist /*******************************************************************************
4400174267aSFredrik Holmqvist  *
4410174267aSFredrik Holmqvist  * FUNCTION:    AcpiExRegisterOverflow
4420174267aSFredrik Holmqvist  *
4430174267aSFredrik Holmqvist  * PARAMETERS:  ObjDesc                 - Register(Field) to be written
4440174267aSFredrik Holmqvist  *              Value                   - Value to be stored
4450174267aSFredrik Holmqvist  *
4460174267aSFredrik Holmqvist  * RETURN:      TRUE if value overflows the field, FALSE otherwise
4470174267aSFredrik Holmqvist  *
4480174267aSFredrik Holmqvist  * DESCRIPTION: Check if a value is out of range of the field being written.
4490174267aSFredrik Holmqvist  *              Used to check if the values written to Index and Bank registers
4500174267aSFredrik Holmqvist  *              are out of range. Normally, the value is simply truncated
4510174267aSFredrik Holmqvist  *              to fit the field, but this case is most likely a serious
4520174267aSFredrik Holmqvist  *              coding error in the ASL.
4530174267aSFredrik Holmqvist  *
4540174267aSFredrik Holmqvist  ******************************************************************************/
4550174267aSFredrik Holmqvist 
4560174267aSFredrik Holmqvist static BOOLEAN
AcpiExRegisterOverflow(ACPI_OPERAND_OBJECT * ObjDesc,UINT64 Value)4570174267aSFredrik Holmqvist AcpiExRegisterOverflow (
4580174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
4590174267aSFredrik Holmqvist     UINT64                  Value)
4600174267aSFredrik Holmqvist {
4610174267aSFredrik Holmqvist 
4620174267aSFredrik Holmqvist     if (ObjDesc->CommonField.BitLength >= ACPI_INTEGER_BIT_SIZE)
4630174267aSFredrik Holmqvist     {
4640174267aSFredrik Holmqvist         /*
4650174267aSFredrik Holmqvist          * The field is large enough to hold the maximum integer, so we can
4660174267aSFredrik Holmqvist          * never overflow it.
4670174267aSFredrik Holmqvist          */
4680174267aSFredrik Holmqvist         return (FALSE);
4690174267aSFredrik Holmqvist     }
4700174267aSFredrik Holmqvist 
4710174267aSFredrik Holmqvist     if (Value >= ((UINT64) 1 << ObjDesc->CommonField.BitLength))
4720174267aSFredrik Holmqvist     {
4730174267aSFredrik Holmqvist         /*
4740174267aSFredrik Holmqvist          * The Value is larger than the maximum value that can fit into
4750174267aSFredrik Holmqvist          * the register.
4760174267aSFredrik Holmqvist          */
4776822cda0SFredrik Holmqvist         ACPI_ERROR ((AE_INFO,
4786822cda0SFredrik Holmqvist             "Index value 0x%8.8X%8.8X overflows field width 0x%X",
4796822cda0SFredrik Holmqvist             ACPI_FORMAT_UINT64 (Value),
4806822cda0SFredrik Holmqvist             ObjDesc->CommonField.BitLength));
4816822cda0SFredrik Holmqvist 
4820174267aSFredrik Holmqvist         return (TRUE);
4830174267aSFredrik Holmqvist     }
4840174267aSFredrik Holmqvist 
4850174267aSFredrik Holmqvist     /* The Value will fit into the field with no truncation */
4860174267aSFredrik Holmqvist 
4870174267aSFredrik Holmqvist     return (FALSE);
4880174267aSFredrik Holmqvist }
4890174267aSFredrik Holmqvist 
4900174267aSFredrik Holmqvist 
4910174267aSFredrik Holmqvist /*******************************************************************************
4920174267aSFredrik Holmqvist  *
4930174267aSFredrik Holmqvist  * FUNCTION:    AcpiExFieldDatumIo
4940174267aSFredrik Holmqvist  *
4950174267aSFredrik Holmqvist  * PARAMETERS:  ObjDesc                 - Field to be read
4960174267aSFredrik Holmqvist  *              FieldDatumByteOffset    - Byte offset of this datum within the
4970174267aSFredrik Holmqvist  *                                        parent field
4980174267aSFredrik Holmqvist  *              Value                   - Where to store value (must be 64 bits)
4990174267aSFredrik Holmqvist  *              ReadWrite               - Read or Write flag
5000174267aSFredrik Holmqvist  *
5010174267aSFredrik Holmqvist  * RETURN:      Status
5020174267aSFredrik Holmqvist  *
5030174267aSFredrik Holmqvist  * DESCRIPTION: Read or Write a single datum of a field. The FieldType is
5040174267aSFredrik Holmqvist  *              demultiplexed here to handle the different types of fields
5050174267aSFredrik Holmqvist  *              (BufferField, RegionField, IndexField, BankField)
5060174267aSFredrik Holmqvist  *
5070174267aSFredrik Holmqvist  ******************************************************************************/
5080174267aSFredrik Holmqvist 
5090174267aSFredrik Holmqvist static ACPI_STATUS
AcpiExFieldDatumIo(ACPI_OPERAND_OBJECT * ObjDesc,UINT32 FieldDatumByteOffset,UINT64 * Value,UINT32 ReadWrite)5100174267aSFredrik Holmqvist AcpiExFieldDatumIo (
5110174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
5120174267aSFredrik Holmqvist     UINT32                  FieldDatumByteOffset,
5130174267aSFredrik Holmqvist     UINT64                  *Value,
5140174267aSFredrik Holmqvist     UINT32                  ReadWrite)
5150174267aSFredrik Holmqvist {
5160174267aSFredrik Holmqvist     ACPI_STATUS             Status;
5170174267aSFredrik Holmqvist     UINT64                  LocalValue;
5180174267aSFredrik Holmqvist 
5190174267aSFredrik Holmqvist 
5200174267aSFredrik Holmqvist     ACPI_FUNCTION_TRACE_U32 (ExFieldDatumIo, FieldDatumByteOffset);
5210174267aSFredrik Holmqvist 
5220174267aSFredrik Holmqvist 
5230174267aSFredrik Holmqvist     if (ReadWrite == ACPI_READ)
5240174267aSFredrik Holmqvist     {
5250174267aSFredrik Holmqvist         if (!Value)
5260174267aSFredrik Holmqvist         {
5270174267aSFredrik Holmqvist             LocalValue = 0;
5280174267aSFredrik Holmqvist 
5290174267aSFredrik Holmqvist             /* To support reads without saving return value */
5300174267aSFredrik Holmqvist             Value = &LocalValue;
5310174267aSFredrik Holmqvist         }
5320174267aSFredrik Holmqvist 
5330174267aSFredrik Holmqvist         /* Clear the entire return buffer first, [Very Important!] */
5340174267aSFredrik Holmqvist 
5350174267aSFredrik Holmqvist         *Value = 0;
5360174267aSFredrik Holmqvist     }
5370174267aSFredrik Holmqvist 
5380174267aSFredrik Holmqvist     /*
5390174267aSFredrik Holmqvist      * The four types of fields are:
5400174267aSFredrik Holmqvist      *
5410174267aSFredrik Holmqvist      * BufferField - Read/write from/to a Buffer
5420174267aSFredrik Holmqvist      * RegionField - Read/write from/to a Operation Region.
5430174267aSFredrik Holmqvist      * BankField   - Write to a Bank Register, then read/write from/to an
5440174267aSFredrik Holmqvist      *               OperationRegion
5450174267aSFredrik Holmqvist      * IndexField  - Write to an Index Register, then read/write from/to a
5460174267aSFredrik Holmqvist      *               Data Register
5470174267aSFredrik Holmqvist      */
5480174267aSFredrik Holmqvist     switch (ObjDesc->Common.Type)
5490174267aSFredrik Holmqvist     {
5500174267aSFredrik Holmqvist     case ACPI_TYPE_BUFFER_FIELD:
5510174267aSFredrik Holmqvist         /*
5520174267aSFredrik Holmqvist          * If the BufferField arguments have not been previously evaluated,
5530174267aSFredrik Holmqvist          * evaluate them now and save the results.
5540174267aSFredrik Holmqvist          */
5550174267aSFredrik Holmqvist         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
5560174267aSFredrik Holmqvist         {
5570174267aSFredrik Holmqvist             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
5580174267aSFredrik Holmqvist             if (ACPI_FAILURE (Status))
5590174267aSFredrik Holmqvist             {
5600174267aSFredrik Holmqvist                 return_ACPI_STATUS (Status);
5610174267aSFredrik Holmqvist             }
5620174267aSFredrik Holmqvist         }
5630174267aSFredrik Holmqvist 
5640174267aSFredrik Holmqvist         if (ReadWrite == ACPI_READ)
5650174267aSFredrik Holmqvist         {
5660174267aSFredrik Holmqvist             /*
5670174267aSFredrik Holmqvist              * Copy the data from the source buffer.
5680174267aSFredrik Holmqvist              * Length is the field width in bytes.
5690174267aSFredrik Holmqvist              */
570e226d1d0SFredrik Holmqvist             memcpy (Value,
5710174267aSFredrik Holmqvist                 (ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
5720174267aSFredrik Holmqvist                     ObjDesc->BufferField.BaseByteOffset +
5730174267aSFredrik Holmqvist                     FieldDatumByteOffset,
5740174267aSFredrik Holmqvist                 ObjDesc->CommonField.AccessByteWidth);
5750174267aSFredrik Holmqvist         }
5760174267aSFredrik Holmqvist         else
5770174267aSFredrik Holmqvist         {
5780174267aSFredrik Holmqvist             /*
5790174267aSFredrik Holmqvist              * Copy the data to the target buffer.
5800174267aSFredrik Holmqvist              * Length is the field width in bytes.
5810174267aSFredrik Holmqvist              */
582e226d1d0SFredrik Holmqvist             memcpy ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer +
5830174267aSFredrik Holmqvist                 ObjDesc->BufferField.BaseByteOffset +
5840174267aSFredrik Holmqvist                 FieldDatumByteOffset,
5850174267aSFredrik Holmqvist                 Value, ObjDesc->CommonField.AccessByteWidth);
5860174267aSFredrik Holmqvist         }
5870174267aSFredrik Holmqvist 
5880174267aSFredrik Holmqvist         Status = AE_OK;
5890174267aSFredrik Holmqvist         break;
5900174267aSFredrik Holmqvist 
5910174267aSFredrik Holmqvist     case ACPI_TYPE_LOCAL_BANK_FIELD:
5920174267aSFredrik Holmqvist         /*
5930174267aSFredrik Holmqvist          * Ensure that the BankValue is not beyond the capacity of
5940174267aSFredrik Holmqvist          * the register
5950174267aSFredrik Holmqvist          */
5960174267aSFredrik Holmqvist         if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
5970174267aSFredrik Holmqvist                 (UINT64) ObjDesc->BankField.Value))
5980174267aSFredrik Holmqvist         {
5990174267aSFredrik Holmqvist             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
6000174267aSFredrik Holmqvist         }
6010174267aSFredrik Holmqvist 
6020174267aSFredrik Holmqvist         /*
6030174267aSFredrik Holmqvist          * For BankFields, we must write the BankValue to the BankRegister
6040174267aSFredrik Holmqvist          * (itself a RegionField) before we can access the data.
6050174267aSFredrik Holmqvist          */
6060174267aSFredrik Holmqvist         Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
6070174267aSFredrik Holmqvist                     &ObjDesc->BankField.Value,
6080174267aSFredrik Holmqvist                     sizeof (ObjDesc->BankField.Value));
6090174267aSFredrik Holmqvist         if (ACPI_FAILURE (Status))
6100174267aSFredrik Holmqvist         {
6110174267aSFredrik Holmqvist             return_ACPI_STATUS (Status);
6120174267aSFredrik Holmqvist         }
6130174267aSFredrik Holmqvist 
6140174267aSFredrik Holmqvist         /*
6150174267aSFredrik Holmqvist          * Now that the Bank has been selected, fall through to the
6160174267aSFredrik Holmqvist          * RegionField case and write the datum to the Operation Region
6170174267aSFredrik Holmqvist          */
6180174267aSFredrik Holmqvist 
6190ffed378SFredrik Holmqvist         ACPI_FALLTHROUGH;
6200174267aSFredrik Holmqvist 
6210174267aSFredrik Holmqvist     case ACPI_TYPE_LOCAL_REGION_FIELD:
6220174267aSFredrik Holmqvist         /*
6230174267aSFredrik Holmqvist          * For simple RegionFields, we just directly access the owning
6240174267aSFredrik Holmqvist          * Operation Region.
6250174267aSFredrik Holmqvist          */
626b64e8511SFredrik Holmqvist         Status = AcpiExAccessRegion (
627b64e8511SFredrik Holmqvist             ObjDesc, FieldDatumByteOffset, Value, ReadWrite);
6280174267aSFredrik Holmqvist         break;
6290174267aSFredrik Holmqvist 
6300174267aSFredrik Holmqvist     case ACPI_TYPE_LOCAL_INDEX_FIELD:
6310174267aSFredrik Holmqvist         /*
6320174267aSFredrik Holmqvist          * Ensure that the IndexValue is not beyond the capacity of
6330174267aSFredrik Holmqvist          * the register
6340174267aSFredrik Holmqvist          */
6350174267aSFredrik Holmqvist         if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
6360174267aSFredrik Holmqvist                 (UINT64) ObjDesc->IndexField.Value))
6370174267aSFredrik Holmqvist         {
6380174267aSFredrik Holmqvist             return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
6390174267aSFredrik Holmqvist         }
6400174267aSFredrik Holmqvist 
6410174267aSFredrik Holmqvist         /* Write the index value to the IndexRegister (itself a RegionField) */
6420174267aSFredrik Holmqvist 
6430174267aSFredrik Holmqvist         FieldDatumByteOffset += ObjDesc->IndexField.Value;
6440174267aSFredrik Holmqvist 
6450174267aSFredrik Holmqvist         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
6460174267aSFredrik Holmqvist             "Write to Index Register: Value %8.8X\n",
6470174267aSFredrik Holmqvist             FieldDatumByteOffset));
6480174267aSFredrik Holmqvist 
6490174267aSFredrik Holmqvist         Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
650b64e8511SFredrik Holmqvist             &FieldDatumByteOffset, sizeof (FieldDatumByteOffset));
6510174267aSFredrik Holmqvist         if (ACPI_FAILURE (Status))
6520174267aSFredrik Holmqvist         {
6530174267aSFredrik Holmqvist             return_ACPI_STATUS (Status);
6540174267aSFredrik Holmqvist         }
6550174267aSFredrik Holmqvist 
6560174267aSFredrik Holmqvist         if (ReadWrite == ACPI_READ)
6570174267aSFredrik Holmqvist         {
6580174267aSFredrik Holmqvist             /* Read the datum from the DataRegister */
6590174267aSFredrik Holmqvist 
6600174267aSFredrik Holmqvist             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
6610174267aSFredrik Holmqvist                 "Read from Data Register\n"));
6620174267aSFredrik Holmqvist 
663b64e8511SFredrik Holmqvist             Status = AcpiExExtractFromField (
664b64e8511SFredrik Holmqvist                 ObjDesc->IndexField.DataObj, Value, sizeof (UINT64));
6650174267aSFredrik Holmqvist         }
6660174267aSFredrik Holmqvist         else
6670174267aSFredrik Holmqvist         {
6680174267aSFredrik Holmqvist             /* Write the datum to the DataRegister */
6690174267aSFredrik Holmqvist 
6700174267aSFredrik Holmqvist             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
6710174267aSFredrik Holmqvist                 "Write to Data Register: Value %8.8X%8.8X\n",
6720174267aSFredrik Holmqvist                 ACPI_FORMAT_UINT64 (*Value)));
6730174267aSFredrik Holmqvist 
674b64e8511SFredrik Holmqvist             Status = AcpiExInsertIntoField (
675b64e8511SFredrik Holmqvist                 ObjDesc->IndexField.DataObj, Value, sizeof (UINT64));
6760174267aSFredrik Holmqvist         }
6770174267aSFredrik Holmqvist         break;
6780174267aSFredrik Holmqvist 
6790174267aSFredrik Holmqvist     default:
6800174267aSFredrik Holmqvist 
6810174267aSFredrik Holmqvist         ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
6820174267aSFredrik Holmqvist             ObjDesc->Common.Type));
6830174267aSFredrik Holmqvist         Status = AE_AML_INTERNAL;
6840174267aSFredrik Holmqvist         break;
6850174267aSFredrik Holmqvist     }
6860174267aSFredrik Holmqvist 
6870174267aSFredrik Holmqvist     if (ACPI_SUCCESS (Status))
6880174267aSFredrik Holmqvist     {
6890174267aSFredrik Holmqvist         if (ReadWrite == ACPI_READ)
6900174267aSFredrik Holmqvist         {
6910174267aSFredrik Holmqvist             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
6920174267aSFredrik Holmqvist                 "Value Read %8.8X%8.8X, Width %u\n",
6930174267aSFredrik Holmqvist                 ACPI_FORMAT_UINT64 (*Value),
6940174267aSFredrik Holmqvist                 ObjDesc->CommonField.AccessByteWidth));
6950174267aSFredrik Holmqvist         }
6960174267aSFredrik Holmqvist         else
6970174267aSFredrik Holmqvist         {
6980174267aSFredrik Holmqvist             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
6990174267aSFredrik Holmqvist                 "Value Written %8.8X%8.8X, Width %u\n",
7000174267aSFredrik Holmqvist                 ACPI_FORMAT_UINT64 (*Value),
7010174267aSFredrik Holmqvist                 ObjDesc->CommonField.AccessByteWidth));
7020174267aSFredrik Holmqvist         }
7030174267aSFredrik Holmqvist     }
7040174267aSFredrik Holmqvist 
7050174267aSFredrik Holmqvist     return_ACPI_STATUS (Status);
7060174267aSFredrik Holmqvist }
7070174267aSFredrik Holmqvist 
7080174267aSFredrik Holmqvist 
7090174267aSFredrik Holmqvist /*******************************************************************************
7100174267aSFredrik Holmqvist  *
7110174267aSFredrik Holmqvist  * FUNCTION:    AcpiExWriteWithUpdateRule
7120174267aSFredrik Holmqvist  *
7130174267aSFredrik Holmqvist  * PARAMETERS:  ObjDesc                 - Field to be written
7140174267aSFredrik Holmqvist  *              Mask                    - bitmask within field datum
7150174267aSFredrik Holmqvist  *              FieldValue              - Value to write
7160174267aSFredrik Holmqvist  *              FieldDatumByteOffset    - Offset of datum within field
7170174267aSFredrik Holmqvist  *
7180174267aSFredrik Holmqvist  * RETURN:      Status
7190174267aSFredrik Holmqvist  *
7200174267aSFredrik Holmqvist  * DESCRIPTION: Apply the field update rule to a field write
7210174267aSFredrik Holmqvist  *
7220174267aSFredrik Holmqvist  ******************************************************************************/
7230174267aSFredrik Holmqvist 
7240174267aSFredrik Holmqvist ACPI_STATUS
AcpiExWriteWithUpdateRule(ACPI_OPERAND_OBJECT * ObjDesc,UINT64 Mask,UINT64 FieldValue,UINT32 FieldDatumByteOffset)7250174267aSFredrik Holmqvist AcpiExWriteWithUpdateRule (
7260174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
7270174267aSFredrik Holmqvist     UINT64                  Mask,
7280174267aSFredrik Holmqvist     UINT64                  FieldValue,
7290174267aSFredrik Holmqvist     UINT32                  FieldDatumByteOffset)
7300174267aSFredrik Holmqvist {
7310174267aSFredrik Holmqvist     ACPI_STATUS             Status = AE_OK;
7320174267aSFredrik Holmqvist     UINT64                  MergedValue;
7330174267aSFredrik Holmqvist     UINT64                  CurrentValue;
7340174267aSFredrik Holmqvist 
7350174267aSFredrik Holmqvist 
7360174267aSFredrik Holmqvist     ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
7370174267aSFredrik Holmqvist 
7380174267aSFredrik Holmqvist 
7390174267aSFredrik Holmqvist     /* Start with the new bits  */
7400174267aSFredrik Holmqvist 
7410174267aSFredrik Holmqvist     MergedValue = FieldValue;
7420174267aSFredrik Holmqvist 
7430174267aSFredrik Holmqvist     /* If the mask is all ones, we don't need to worry about the update rule */
7440174267aSFredrik Holmqvist 
7450174267aSFredrik Holmqvist     if (Mask != ACPI_UINT64_MAX)
7460174267aSFredrik Holmqvist     {
7470174267aSFredrik Holmqvist         /* Decode the update rule */
7480174267aSFredrik Holmqvist 
7490174267aSFredrik Holmqvist         switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
7500174267aSFredrik Holmqvist         {
7510174267aSFredrik Holmqvist         case AML_FIELD_UPDATE_PRESERVE:
7520174267aSFredrik Holmqvist             /*
7530174267aSFredrik Holmqvist              * Check if update rule needs to be applied (not if mask is all
7540174267aSFredrik Holmqvist              * ones)  The left shift drops the bits we want to ignore.
7550174267aSFredrik Holmqvist              */
7560174267aSFredrik Holmqvist             if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
7570174267aSFredrik Holmqvist                    ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
7580174267aSFredrik Holmqvist             {
7590174267aSFredrik Holmqvist                 /*
7600174267aSFredrik Holmqvist                  * Read the current contents of the byte/word/dword containing
7610174267aSFredrik Holmqvist                  * the field, and merge with the new field value.
7620174267aSFredrik Holmqvist                  */
763b64e8511SFredrik Holmqvist                 Status = AcpiExFieldDatumIo (
764b64e8511SFredrik Holmqvist                     ObjDesc, FieldDatumByteOffset, &CurrentValue, ACPI_READ);
7650174267aSFredrik Holmqvist                 if (ACPI_FAILURE (Status))
7660174267aSFredrik Holmqvist                 {
7670174267aSFredrik Holmqvist                     return_ACPI_STATUS (Status);
7680174267aSFredrik Holmqvist                 }
7690174267aSFredrik Holmqvist 
7700174267aSFredrik Holmqvist                 MergedValue |= (CurrentValue & ~Mask);
7710174267aSFredrik Holmqvist             }
7720174267aSFredrik Holmqvist             break;
7730174267aSFredrik Holmqvist 
7740174267aSFredrik Holmqvist         case AML_FIELD_UPDATE_WRITE_AS_ONES:
7750174267aSFredrik Holmqvist 
7760174267aSFredrik Holmqvist             /* Set positions outside the field to all ones */
7770174267aSFredrik Holmqvist 
7780174267aSFredrik Holmqvist             MergedValue |= ~Mask;
7790174267aSFredrik Holmqvist             break;
7800174267aSFredrik Holmqvist 
7810174267aSFredrik Holmqvist         case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
7820174267aSFredrik Holmqvist 
7830174267aSFredrik Holmqvist             /* Set positions outside the field to all zeros */
7840174267aSFredrik Holmqvist 
7850174267aSFredrik Holmqvist             MergedValue &= Mask;
7860174267aSFredrik Holmqvist             break;
7870174267aSFredrik Holmqvist 
7880174267aSFredrik Holmqvist         default:
7890174267aSFredrik Holmqvist 
7900174267aSFredrik Holmqvist             ACPI_ERROR ((AE_INFO,
7910174267aSFredrik Holmqvist                 "Unknown UpdateRule value: 0x%X",
792b64e8511SFredrik Holmqvist                 (ObjDesc->CommonField.FieldFlags &
793b64e8511SFredrik Holmqvist                     AML_FIELD_UPDATE_RULE_MASK)));
7940174267aSFredrik Holmqvist             return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
7950174267aSFredrik Holmqvist         }
7960174267aSFredrik Holmqvist     }
7970174267aSFredrik Holmqvist 
7980174267aSFredrik Holmqvist     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
799b64e8511SFredrik Holmqvist         "Mask %8.8X%8.8X, DatumOffset %X, Width %X, "
800b64e8511SFredrik Holmqvist         "Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
8010174267aSFredrik Holmqvist         ACPI_FORMAT_UINT64 (Mask),
8020174267aSFredrik Holmqvist         FieldDatumByteOffset,
8030174267aSFredrik Holmqvist         ObjDesc->CommonField.AccessByteWidth,
8040174267aSFredrik Holmqvist         ACPI_FORMAT_UINT64 (FieldValue),
8050174267aSFredrik Holmqvist         ACPI_FORMAT_UINT64 (MergedValue)));
8060174267aSFredrik Holmqvist 
8070174267aSFredrik Holmqvist     /* Write the merged value */
8080174267aSFredrik Holmqvist 
809b64e8511SFredrik Holmqvist     Status = AcpiExFieldDatumIo (
810b64e8511SFredrik Holmqvist         ObjDesc, FieldDatumByteOffset, &MergedValue, ACPI_WRITE);
8110174267aSFredrik Holmqvist 
8120174267aSFredrik Holmqvist     return_ACPI_STATUS (Status);
8130174267aSFredrik Holmqvist }
8140174267aSFredrik Holmqvist 
8150174267aSFredrik Holmqvist 
8160174267aSFredrik Holmqvist /*******************************************************************************
8170174267aSFredrik Holmqvist  *
8180174267aSFredrik Holmqvist  * FUNCTION:    AcpiExExtractFromField
8190174267aSFredrik Holmqvist  *
8200174267aSFredrik Holmqvist  * PARAMETERS:  ObjDesc             - Field to be read
8210174267aSFredrik Holmqvist  *              Buffer              - Where to store the field data
8220174267aSFredrik Holmqvist  *              BufferLength        - Length of Buffer
8230174267aSFredrik Holmqvist  *
8240174267aSFredrik Holmqvist  * RETURN:      Status
8250174267aSFredrik Holmqvist  *
8260174267aSFredrik Holmqvist  * DESCRIPTION: Retrieve the current value of the given field
8270174267aSFredrik Holmqvist  *
8280174267aSFredrik Holmqvist  ******************************************************************************/
8290174267aSFredrik Holmqvist 
8300174267aSFredrik Holmqvist ACPI_STATUS
AcpiExExtractFromField(ACPI_OPERAND_OBJECT * ObjDesc,void * Buffer,UINT32 BufferLength)8310174267aSFredrik Holmqvist AcpiExExtractFromField (
8320174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
8330174267aSFredrik Holmqvist     void                    *Buffer,
8340174267aSFredrik Holmqvist     UINT32                  BufferLength)
8350174267aSFredrik Holmqvist {
8360174267aSFredrik Holmqvist     ACPI_STATUS             Status;
8370174267aSFredrik Holmqvist     UINT64                  RawDatum;
8380174267aSFredrik Holmqvist     UINT64                  MergedDatum;
8390174267aSFredrik Holmqvist     UINT32                  FieldOffset = 0;
8400174267aSFredrik Holmqvist     UINT32                  BufferOffset = 0;
8410174267aSFredrik Holmqvist     UINT32                  BufferTailBits;
8420174267aSFredrik Holmqvist     UINT32                  DatumCount;
8430174267aSFredrik Holmqvist     UINT32                  FieldDatumCount;
8440174267aSFredrik Holmqvist     UINT32                  AccessBitWidth;
8450174267aSFredrik Holmqvist     UINT32                  i;
8460174267aSFredrik Holmqvist 
8470174267aSFredrik Holmqvist 
8480174267aSFredrik Holmqvist     ACPI_FUNCTION_TRACE (ExExtractFromField);
8490174267aSFredrik Holmqvist 
8500174267aSFredrik Holmqvist 
8510174267aSFredrik Holmqvist     /* Validate target buffer and clear it */
8520174267aSFredrik Holmqvist 
8530174267aSFredrik Holmqvist     if (BufferLength <
8540174267aSFredrik Holmqvist         ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
8550174267aSFredrik Holmqvist     {
8560174267aSFredrik Holmqvist         ACPI_ERROR ((AE_INFO,
8570174267aSFredrik Holmqvist             "Field size %u (bits) is too large for buffer (%u)",
8580174267aSFredrik Holmqvist             ObjDesc->CommonField.BitLength, BufferLength));
8590174267aSFredrik Holmqvist 
8600174267aSFredrik Holmqvist         return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
8610174267aSFredrik Holmqvist     }
8620174267aSFredrik Holmqvist 
863e226d1d0SFredrik Holmqvist     memset (Buffer, 0, BufferLength);
8640174267aSFredrik Holmqvist     AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
8650174267aSFredrik Holmqvist 
8660174267aSFredrik Holmqvist     /* Handle the simple case here */
8670174267aSFredrik Holmqvist 
8680174267aSFredrik Holmqvist     if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
8690174267aSFredrik Holmqvist         (ObjDesc->CommonField.BitLength == AccessBitWidth))
8700174267aSFredrik Holmqvist     {
871c70258b7SJérôme Duval         if (BufferLength >= sizeof (UINT64))
872c70258b7SJérôme Duval         {
8730174267aSFredrik Holmqvist             Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
874c70258b7SJérôme Duval         }
875c70258b7SJérôme Duval         else
876c70258b7SJérôme Duval         {
877c70258b7SJérôme Duval             /* Use RawDatum (UINT64) to handle buffers < 64 bits */
878c70258b7SJérôme Duval 
879c70258b7SJérôme Duval             Status = AcpiExFieldDatumIo (ObjDesc, 0, &RawDatum, ACPI_READ);
880e226d1d0SFredrik Holmqvist             memcpy (Buffer, &RawDatum, BufferLength);
881c70258b7SJérôme Duval         }
882c70258b7SJérôme Duval 
8830174267aSFredrik Holmqvist         return_ACPI_STATUS (Status);
8840174267aSFredrik Holmqvist     }
8850174267aSFredrik Holmqvist 
8860174267aSFredrik Holmqvist /* TBD: Move to common setup code */
8870174267aSFredrik Holmqvist 
8880174267aSFredrik Holmqvist     /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
8890174267aSFredrik Holmqvist 
8900174267aSFredrik Holmqvist     if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
8910174267aSFredrik Holmqvist     {
8920174267aSFredrik Holmqvist         ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
8930174267aSFredrik Holmqvist         AccessBitWidth = sizeof (UINT64) * 8;
8940174267aSFredrik Holmqvist     }
8950174267aSFredrik Holmqvist 
8960174267aSFredrik Holmqvist     /* Compute the number of datums (access width data items) */
8970174267aSFredrik Holmqvist 
8980174267aSFredrik Holmqvist     DatumCount = ACPI_ROUND_UP_TO (
8990174267aSFredrik Holmqvist         ObjDesc->CommonField.BitLength, AccessBitWidth);
9000174267aSFredrik Holmqvist 
9010174267aSFredrik Holmqvist     FieldDatumCount = ACPI_ROUND_UP_TO (
9020174267aSFredrik Holmqvist         ObjDesc->CommonField.BitLength +
9030174267aSFredrik Holmqvist         ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
9040174267aSFredrik Holmqvist 
9050174267aSFredrik Holmqvist     /* Priming read from the field */
9060174267aSFredrik Holmqvist 
9070174267aSFredrik Holmqvist     Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
9080174267aSFredrik Holmqvist     if (ACPI_FAILURE (Status))
9090174267aSFredrik Holmqvist     {
9100174267aSFredrik Holmqvist         return_ACPI_STATUS (Status);
9110174267aSFredrik Holmqvist     }
9120174267aSFredrik Holmqvist     MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
9130174267aSFredrik Holmqvist 
9140174267aSFredrik Holmqvist     /* Read the rest of the field */
9150174267aSFredrik Holmqvist 
9160174267aSFredrik Holmqvist     for (i = 1; i < FieldDatumCount; i++)
9170174267aSFredrik Holmqvist     {
9180174267aSFredrik Holmqvist         /* Get next input datum from the field */
9190174267aSFredrik Holmqvist 
9200174267aSFredrik Holmqvist         FieldOffset += ObjDesc->CommonField.AccessByteWidth;
921b64e8511SFredrik Holmqvist         Status = AcpiExFieldDatumIo (
922b64e8511SFredrik Holmqvist             ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
9230174267aSFredrik Holmqvist         if (ACPI_FAILURE (Status))
9240174267aSFredrik Holmqvist         {
9250174267aSFredrik Holmqvist             return_ACPI_STATUS (Status);
9260174267aSFredrik Holmqvist         }
9270174267aSFredrik Holmqvist 
9280174267aSFredrik Holmqvist         /*
9290174267aSFredrik Holmqvist          * Merge with previous datum if necessary.
9300174267aSFredrik Holmqvist          *
9310174267aSFredrik Holmqvist          * Note: Before the shift, check if the shift value will be larger than
9320174267aSFredrik Holmqvist          * the integer size. If so, there is no need to perform the operation.
9330174267aSFredrik Holmqvist          * This avoids the differences in behavior between different compilers
9340174267aSFredrik Holmqvist          * concerning shift values larger than the target data width.
9350174267aSFredrik Holmqvist          */
9360174267aSFredrik Holmqvist         if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
9370174267aSFredrik Holmqvist             ACPI_INTEGER_BIT_SIZE)
9380174267aSFredrik Holmqvist         {
9390174267aSFredrik Holmqvist             MergedDatum |= RawDatum <<
9400174267aSFredrik Holmqvist                 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
9410174267aSFredrik Holmqvist         }
9420174267aSFredrik Holmqvist 
9430174267aSFredrik Holmqvist         if (i == DatumCount)
9440174267aSFredrik Holmqvist         {
9450174267aSFredrik Holmqvist             break;
9460174267aSFredrik Holmqvist         }
9470174267aSFredrik Holmqvist 
9480174267aSFredrik Holmqvist         /* Write merged datum to target buffer */
9490174267aSFredrik Holmqvist 
950e226d1d0SFredrik Holmqvist         memcpy (((char *) Buffer) + BufferOffset, &MergedDatum,
9510174267aSFredrik Holmqvist             ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
9520174267aSFredrik Holmqvist                 BufferLength - BufferOffset));
9530174267aSFredrik Holmqvist 
9540174267aSFredrik Holmqvist         BufferOffset += ObjDesc->CommonField.AccessByteWidth;
9550174267aSFredrik Holmqvist         MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
9560174267aSFredrik Holmqvist     }
9570174267aSFredrik Holmqvist 
9580174267aSFredrik Holmqvist     /* Mask off any extra bits in the last datum */
9590174267aSFredrik Holmqvist 
9600174267aSFredrik Holmqvist     BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
9610174267aSFredrik Holmqvist     if (BufferTailBits)
9620174267aSFredrik Holmqvist     {
9630174267aSFredrik Holmqvist         MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
9640174267aSFredrik Holmqvist     }
9650174267aSFredrik Holmqvist 
9660174267aSFredrik Holmqvist     /* Write the last datum to the buffer */
9670174267aSFredrik Holmqvist 
968e226d1d0SFredrik Holmqvist     memcpy (((char *) Buffer) + BufferOffset, &MergedDatum,
9690174267aSFredrik Holmqvist         ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
9700174267aSFredrik Holmqvist             BufferLength - BufferOffset));
9710174267aSFredrik Holmqvist 
9720174267aSFredrik Holmqvist     return_ACPI_STATUS (AE_OK);
9730174267aSFredrik Holmqvist }
9740174267aSFredrik Holmqvist 
9750174267aSFredrik Holmqvist 
9760174267aSFredrik Holmqvist /*******************************************************************************
9770174267aSFredrik Holmqvist  *
9780174267aSFredrik Holmqvist  * FUNCTION:    AcpiExInsertIntoField
9790174267aSFredrik Holmqvist  *
9800174267aSFredrik Holmqvist  * PARAMETERS:  ObjDesc             - Field to be written
9810174267aSFredrik Holmqvist  *              Buffer              - Data to be written
9820174267aSFredrik Holmqvist  *              BufferLength        - Length of Buffer
9830174267aSFredrik Holmqvist  *
9840174267aSFredrik Holmqvist  * RETURN:      Status
9850174267aSFredrik Holmqvist  *
9860174267aSFredrik Holmqvist  * DESCRIPTION: Store the Buffer contents into the given field
9870174267aSFredrik Holmqvist  *
9880174267aSFredrik Holmqvist  ******************************************************************************/
9890174267aSFredrik Holmqvist 
9900174267aSFredrik Holmqvist ACPI_STATUS
AcpiExInsertIntoField(ACPI_OPERAND_OBJECT * ObjDesc,void * Buffer,UINT32 BufferLength)9910174267aSFredrik Holmqvist AcpiExInsertIntoField (
9920174267aSFredrik Holmqvist     ACPI_OPERAND_OBJECT     *ObjDesc,
9930174267aSFredrik Holmqvist     void                    *Buffer,
9940174267aSFredrik Holmqvist     UINT32                  BufferLength)
9950174267aSFredrik Holmqvist {
9960174267aSFredrik Holmqvist     void                    *NewBuffer;
9970174267aSFredrik Holmqvist     ACPI_STATUS             Status;
9980174267aSFredrik Holmqvist     UINT64                  Mask;
9990174267aSFredrik Holmqvist     UINT64                  WidthMask;
10000174267aSFredrik Holmqvist     UINT64                  MergedDatum;
10010174267aSFredrik Holmqvist     UINT64                  RawDatum = 0;
10020174267aSFredrik Holmqvist     UINT32                  FieldOffset = 0;
10030174267aSFredrik Holmqvist     UINT32                  BufferOffset = 0;
10040174267aSFredrik Holmqvist     UINT32                  BufferTailBits;
10050174267aSFredrik Holmqvist     UINT32                  DatumCount;
10060174267aSFredrik Holmqvist     UINT32                  FieldDatumCount;
10070174267aSFredrik Holmqvist     UINT32                  AccessBitWidth;
10080174267aSFredrik Holmqvist     UINT32                  RequiredLength;
10090174267aSFredrik Holmqvist     UINT32                  i;
10100174267aSFredrik Holmqvist 
10110174267aSFredrik Holmqvist 
10120174267aSFredrik Holmqvist     ACPI_FUNCTION_TRACE (ExInsertIntoField);
10130174267aSFredrik Holmqvist 
10140174267aSFredrik Holmqvist 
10150174267aSFredrik Holmqvist     /* Validate input buffer */
10160174267aSFredrik Holmqvist 
10170174267aSFredrik Holmqvist     NewBuffer = NULL;
10180174267aSFredrik Holmqvist     RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
10190174267aSFredrik Holmqvist         ObjDesc->CommonField.BitLength);
1020b64e8511SFredrik Holmqvist 
10210174267aSFredrik Holmqvist     /*
10220174267aSFredrik Holmqvist      * We must have a buffer that is at least as long as the field
10230174267aSFredrik Holmqvist      * we are writing to. This is because individual fields are
10240174267aSFredrik Holmqvist      * indivisible and partial writes are not supported -- as per
10250174267aSFredrik Holmqvist      * the ACPI specification.
10260174267aSFredrik Holmqvist      */
10270174267aSFredrik Holmqvist     if (BufferLength < RequiredLength)
10280174267aSFredrik Holmqvist     {
10290174267aSFredrik Holmqvist         /* We need to create a new buffer */
10300174267aSFredrik Holmqvist 
10310174267aSFredrik Holmqvist         NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
10320174267aSFredrik Holmqvist         if (!NewBuffer)
10330174267aSFredrik Holmqvist         {
10340174267aSFredrik Holmqvist             return_ACPI_STATUS (AE_NO_MEMORY);
10350174267aSFredrik Holmqvist         }
10360174267aSFredrik Holmqvist 
10370174267aSFredrik Holmqvist         /*
10380174267aSFredrik Holmqvist          * Copy the original data to the new buffer, starting
10390174267aSFredrik Holmqvist          * at Byte zero. All unused (upper) bytes of the
10400174267aSFredrik Holmqvist          * buffer will be 0.
10410174267aSFredrik Holmqvist          */
1042e226d1d0SFredrik Holmqvist         memcpy ((char *) NewBuffer, (char *) Buffer, BufferLength);
10430174267aSFredrik Holmqvist         Buffer = NewBuffer;
10440174267aSFredrik Holmqvist         BufferLength = RequiredLength;
10450174267aSFredrik Holmqvist     }
10460174267aSFredrik Holmqvist 
10470174267aSFredrik Holmqvist /* TBD: Move to common setup code */
10480174267aSFredrik Holmqvist 
10490174267aSFredrik Holmqvist     /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
10500174267aSFredrik Holmqvist     if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
10510174267aSFredrik Holmqvist     {
10520174267aSFredrik Holmqvist         ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
10530174267aSFredrik Holmqvist     }
10540174267aSFredrik Holmqvist 
10550174267aSFredrik Holmqvist     AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
10560174267aSFredrik Holmqvist 
1057ff2e2f81SFredrik Holmqvist     /* Create the bitmasks used for bit insertion */
10580174267aSFredrik Holmqvist 
1059ff2e2f81SFredrik Holmqvist     WidthMask = ACPI_MASK_BITS_ABOVE_64 (AccessBitWidth);
10600174267aSFredrik Holmqvist     Mask = WidthMask &
10610174267aSFredrik Holmqvist         ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
10620174267aSFredrik Holmqvist 
10630174267aSFredrik Holmqvist     /* Compute the number of datums (access width data items) */
10640174267aSFredrik Holmqvist 
10650174267aSFredrik Holmqvist     DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
10660174267aSFredrik Holmqvist         AccessBitWidth);
10670174267aSFredrik Holmqvist 
10680174267aSFredrik Holmqvist     FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
10690174267aSFredrik Holmqvist         ObjDesc->CommonField.StartFieldBitOffset,
10700174267aSFredrik Holmqvist         AccessBitWidth);
10710174267aSFredrik Holmqvist 
10720174267aSFredrik Holmqvist     /* Get initial Datum from the input buffer */
10730174267aSFredrik Holmqvist 
1074e226d1d0SFredrik Holmqvist     memcpy (&RawDatum, Buffer,
10750174267aSFredrik Holmqvist         ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
10760174267aSFredrik Holmqvist             BufferLength - BufferOffset));
10770174267aSFredrik Holmqvist 
10780174267aSFredrik Holmqvist     MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
10790174267aSFredrik Holmqvist 
10800174267aSFredrik Holmqvist     /* Write the entire field */
10810174267aSFredrik Holmqvist 
10820174267aSFredrik Holmqvist     for (i = 1; i < FieldDatumCount; i++)
10830174267aSFredrik Holmqvist     {
10840174267aSFredrik Holmqvist         /* Write merged datum to the target field */
10850174267aSFredrik Holmqvist 
10860174267aSFredrik Holmqvist         MergedDatum &= Mask;
1087b64e8511SFredrik Holmqvist         Status = AcpiExWriteWithUpdateRule (
1088b64e8511SFredrik Holmqvist             ObjDesc, Mask, MergedDatum, FieldOffset);
10890174267aSFredrik Holmqvist         if (ACPI_FAILURE (Status))
10900174267aSFredrik Holmqvist         {
10910174267aSFredrik Holmqvist             goto Exit;
10920174267aSFredrik Holmqvist         }
10930174267aSFredrik Holmqvist 
10940174267aSFredrik Holmqvist         FieldOffset += ObjDesc->CommonField.AccessByteWidth;
10950174267aSFredrik Holmqvist 
10960174267aSFredrik Holmqvist         /*
10970174267aSFredrik Holmqvist          * Start new output datum by merging with previous input datum
10980174267aSFredrik Holmqvist          * if necessary.
10990174267aSFredrik Holmqvist          *
11000174267aSFredrik Holmqvist          * Note: Before the shift, check if the shift value will be larger than
11010174267aSFredrik Holmqvist          * the integer size. If so, there is no need to perform the operation.
11020174267aSFredrik Holmqvist          * This avoids the differences in behavior between different compilers
11030174267aSFredrik Holmqvist          * concerning shift values larger than the target data width.
11040174267aSFredrik Holmqvist          */
11050174267aSFredrik Holmqvist         if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
11060174267aSFredrik Holmqvist             ACPI_INTEGER_BIT_SIZE)
11070174267aSFredrik Holmqvist         {
11080174267aSFredrik Holmqvist             MergedDatum = RawDatum >>
11090174267aSFredrik Holmqvist                 (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
11100174267aSFredrik Holmqvist         }
11110174267aSFredrik Holmqvist         else
11120174267aSFredrik Holmqvist         {
11130174267aSFredrik Holmqvist             MergedDatum = 0;
11140174267aSFredrik Holmqvist         }
11150174267aSFredrik Holmqvist 
11160174267aSFredrik Holmqvist         Mask = WidthMask;
11170174267aSFredrik Holmqvist 
11180174267aSFredrik Holmqvist         if (i == DatumCount)
11190174267aSFredrik Holmqvist         {
11200174267aSFredrik Holmqvist             break;
11210174267aSFredrik Holmqvist         }
11220174267aSFredrik Holmqvist 
11230174267aSFredrik Holmqvist         /* Get the next input datum from the buffer */
11240174267aSFredrik Holmqvist 
11250174267aSFredrik Holmqvist         BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1126e226d1d0SFredrik Holmqvist         memcpy (&RawDatum, ((char *) Buffer) + BufferOffset,
11270174267aSFredrik Holmqvist             ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
11280174267aSFredrik Holmqvist                  BufferLength - BufferOffset));
11290174267aSFredrik Holmqvist 
11300174267aSFredrik Holmqvist         MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
11310174267aSFredrik Holmqvist     }
11320174267aSFredrik Holmqvist 
11330174267aSFredrik Holmqvist     /* Mask off any extra bits in the last datum */
11340174267aSFredrik Holmqvist 
11350174267aSFredrik Holmqvist     BufferTailBits = (ObjDesc->CommonField.BitLength +
11360174267aSFredrik Holmqvist         ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
11370174267aSFredrik Holmqvist     if (BufferTailBits)
11380174267aSFredrik Holmqvist     {
11390174267aSFredrik Holmqvist         Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
11400174267aSFredrik Holmqvist     }
11410174267aSFredrik Holmqvist 
11420174267aSFredrik Holmqvist     /* Write the last datum to the field */
11430174267aSFredrik Holmqvist 
11440174267aSFredrik Holmqvist     MergedDatum &= Mask;
1145b64e8511SFredrik Holmqvist     Status = AcpiExWriteWithUpdateRule (
1146b64e8511SFredrik Holmqvist         ObjDesc, Mask, MergedDatum, FieldOffset);
11470174267aSFredrik Holmqvist 
11480174267aSFredrik Holmqvist Exit:
11490174267aSFredrik Holmqvist     /* Free temporary buffer if we used one */
11500174267aSFredrik Holmqvist 
11510174267aSFredrik Holmqvist     if (NewBuffer)
11520174267aSFredrik Holmqvist     {
11530174267aSFredrik Holmqvist         ACPI_FREE (NewBuffer);
11540174267aSFredrik Holmqvist     }
11550174267aSFredrik Holmqvist     return_ACPI_STATUS (Status);
11560174267aSFredrik Holmqvist }
1157