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