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