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