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 - 2012, 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
132static ACPI_STATUS
133AcpiExFieldDatumIo (
134    ACPI_OPERAND_OBJECT     *ObjDesc,
135    UINT32                  FieldDatumByteOffset,
136    UINT64                  *Value,
137    UINT32                  ReadWrite);
138
139static BOOLEAN
140AcpiExRegisterOverflow (
141    ACPI_OPERAND_OBJECT     *ObjDesc,
142    UINT64                  Value);
143
144static ACPI_STATUS
145AcpiExSetupRegion (
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
166static ACPI_STATUS
167AcpiExSetupRegion (
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
320ACPI_STATUS
321AcpiExAccessRegion (
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
420static BOOLEAN
421AcpiExRegisterOverflow (
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
473static ACPI_STATUS
474AcpiExFieldDatumIo (
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
556    case ACPI_TYPE_LOCAL_BANK_FIELD:
557
558        /*
559         * Ensure that the BankValue is not beyond the capacity of
560         * the register
561         */
562        if (AcpiExRegisterOverflow (ObjDesc->BankField.BankObj,
563                (UINT64) ObjDesc->BankField.Value))
564        {
565            return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
566        }
567
568        /*
569         * For BankFields, we must write the BankValue to the BankRegister
570         * (itself a RegionField) before we can access the data.
571         */
572        Status = AcpiExInsertIntoField (ObjDesc->BankField.BankObj,
573                    &ObjDesc->BankField.Value,
574                    sizeof (ObjDesc->BankField.Value));
575        if (ACPI_FAILURE (Status))
576        {
577            return_ACPI_STATUS (Status);
578        }
579
580        /*
581         * Now that the Bank has been selected, fall through to the
582         * RegionField case and write the datum to the Operation Region
583         */
584
585        /*lint -fallthrough */
586
587
588    case ACPI_TYPE_LOCAL_REGION_FIELD:
589        /*
590         * For simple RegionFields, we just directly access the owning
591         * Operation Region.
592         */
593        Status = AcpiExAccessRegion (ObjDesc, FieldDatumByteOffset, Value,
594                    ReadWrite);
595        break;
596
597
598    case ACPI_TYPE_LOCAL_INDEX_FIELD:
599
600
601        /*
602         * Ensure that the IndexValue is not beyond the capacity of
603         * the register
604         */
605        if (AcpiExRegisterOverflow (ObjDesc->IndexField.IndexObj,
606                (UINT64) ObjDesc->IndexField.Value))
607        {
608            return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
609        }
610
611        /* Write the index value to the IndexRegister (itself a RegionField) */
612
613        FieldDatumByteOffset += ObjDesc->IndexField.Value;
614
615        ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
616            "Write to Index Register: Value %8.8X\n",
617            FieldDatumByteOffset));
618
619        Status = AcpiExInsertIntoField (ObjDesc->IndexField.IndexObj,
620                    &FieldDatumByteOffset,
621                    sizeof (FieldDatumByteOffset));
622        if (ACPI_FAILURE (Status))
623        {
624            return_ACPI_STATUS (Status);
625        }
626
627        if (ReadWrite == ACPI_READ)
628        {
629            /* Read the datum from the DataRegister */
630
631            ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
632                "Read from Data Register\n"));
633
634            Status = AcpiExExtractFromField (ObjDesc->IndexField.DataObj,
635                        Value, sizeof (UINT64));
636        }
637        else
638        {
639            /* Write the datum to the DataRegister */
640
641            ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
642                "Write to Data Register: Value %8.8X%8.8X\n",
643                ACPI_FORMAT_UINT64 (*Value)));
644
645            Status = AcpiExInsertIntoField (ObjDesc->IndexField.DataObj,
646                        Value, sizeof (UINT64));
647        }
648        break;
649
650
651    default:
652
653        ACPI_ERROR ((AE_INFO, "Wrong object type in field I/O %u",
654            ObjDesc->Common.Type));
655        Status = AE_AML_INTERNAL;
656        break;
657    }
658
659    if (ACPI_SUCCESS (Status))
660    {
661        if (ReadWrite == ACPI_READ)
662        {
663            ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
664                "Value Read %8.8X%8.8X, Width %u\n",
665                ACPI_FORMAT_UINT64 (*Value),
666                ObjDesc->CommonField.AccessByteWidth));
667        }
668        else
669        {
670            ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
671                "Value Written %8.8X%8.8X, Width %u\n",
672                ACPI_FORMAT_UINT64 (*Value),
673                ObjDesc->CommonField.AccessByteWidth));
674        }
675    }
676
677    return_ACPI_STATUS (Status);
678}
679
680
681/*******************************************************************************
682 *
683 * FUNCTION:    AcpiExWriteWithUpdateRule
684 *
685 * PARAMETERS:  ObjDesc                 - Field to be written
686 *              Mask                    - bitmask within field datum
687 *              FieldValue              - Value to write
688 *              FieldDatumByteOffset    - Offset of datum within field
689 *
690 * RETURN:      Status
691 *
692 * DESCRIPTION: Apply the field update rule to a field write
693 *
694 ******************************************************************************/
695
696ACPI_STATUS
697AcpiExWriteWithUpdateRule (
698    ACPI_OPERAND_OBJECT     *ObjDesc,
699    UINT64                  Mask,
700    UINT64                  FieldValue,
701    UINT32                  FieldDatumByteOffset)
702{
703    ACPI_STATUS             Status = AE_OK;
704    UINT64                  MergedValue;
705    UINT64                  CurrentValue;
706
707
708    ACPI_FUNCTION_TRACE_U32 (ExWriteWithUpdateRule, Mask);
709
710
711    /* Start with the new bits  */
712
713    MergedValue = FieldValue;
714
715    /* If the mask is all ones, we don't need to worry about the update rule */
716
717    if (Mask != ACPI_UINT64_MAX)
718    {
719        /* Decode the update rule */
720
721        switch (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)
722        {
723        case AML_FIELD_UPDATE_PRESERVE:
724            /*
725             * Check if update rule needs to be applied (not if mask is all
726             * ones)  The left shift drops the bits we want to ignore.
727             */
728            if ((~Mask << (ACPI_MUL_8 (sizeof (Mask)) -
729                           ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth))) != 0)
730            {
731                /*
732                 * Read the current contents of the byte/word/dword containing
733                 * the field, and merge with the new field value.
734                 */
735                Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
736                            &CurrentValue, ACPI_READ);
737                if (ACPI_FAILURE (Status))
738                {
739                    return_ACPI_STATUS (Status);
740                }
741
742                MergedValue |= (CurrentValue & ~Mask);
743            }
744            break;
745
746        case AML_FIELD_UPDATE_WRITE_AS_ONES:
747
748            /* Set positions outside the field to all ones */
749
750            MergedValue |= ~Mask;
751            break;
752
753        case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
754
755            /* Set positions outside the field to all zeros */
756
757            MergedValue &= Mask;
758            break;
759
760        default:
761
762            ACPI_ERROR ((AE_INFO,
763                "Unknown UpdateRule value: 0x%X",
764                (ObjDesc->CommonField.FieldFlags & AML_FIELD_UPDATE_RULE_MASK)));
765            return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
766        }
767    }
768
769    ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
770        "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
771        ACPI_FORMAT_UINT64 (Mask),
772        FieldDatumByteOffset,
773        ObjDesc->CommonField.AccessByteWidth,
774        ACPI_FORMAT_UINT64 (FieldValue),
775        ACPI_FORMAT_UINT64 (MergedValue)));
776
777    /* Write the merged value */
778
779    Status = AcpiExFieldDatumIo (ObjDesc, FieldDatumByteOffset,
780                &MergedValue, ACPI_WRITE);
781
782    return_ACPI_STATUS (Status);
783}
784
785
786/*******************************************************************************
787 *
788 * FUNCTION:    AcpiExExtractFromField
789 *
790 * PARAMETERS:  ObjDesc             - Field to be read
791 *              Buffer              - Where to store the field data
792 *              BufferLength        - Length of Buffer
793 *
794 * RETURN:      Status
795 *
796 * DESCRIPTION: Retrieve the current value of the given field
797 *
798 ******************************************************************************/
799
800ACPI_STATUS
801AcpiExExtractFromField (
802    ACPI_OPERAND_OBJECT     *ObjDesc,
803    void                    *Buffer,
804    UINT32                  BufferLength)
805{
806    ACPI_STATUS             Status;
807    UINT64                  RawDatum;
808    UINT64                  MergedDatum;
809    UINT32                  FieldOffset = 0;
810    UINT32                  BufferOffset = 0;
811    UINT32                  BufferTailBits;
812    UINT32                  DatumCount;
813    UINT32                  FieldDatumCount;
814    UINT32                  AccessBitWidth;
815    UINT32                  i;
816
817
818    ACPI_FUNCTION_TRACE (ExExtractFromField);
819
820
821    /* Validate target buffer and clear it */
822
823    if (BufferLength <
824        ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength))
825    {
826        ACPI_ERROR ((AE_INFO,
827            "Field size %u (bits) is too large for buffer (%u)",
828            ObjDesc->CommonField.BitLength, BufferLength));
829
830        return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
831    }
832
833    ACPI_MEMSET (Buffer, 0, BufferLength);
834    AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
835
836    /* Handle the simple case here */
837
838    if ((ObjDesc->CommonField.StartFieldBitOffset == 0) &&
839        (ObjDesc->CommonField.BitLength == AccessBitWidth))
840    {
841        Status = AcpiExFieldDatumIo (ObjDesc, 0, Buffer, ACPI_READ);
842        return_ACPI_STATUS (Status);
843    }
844
845/* TBD: Move to common setup code */
846
847    /* Field algorithm is limited to sizeof(UINT64), truncate if needed */
848
849    if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
850    {
851        ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
852        AccessBitWidth = sizeof (UINT64) * 8;
853    }
854
855    /* Compute the number of datums (access width data items) */
856
857    DatumCount = ACPI_ROUND_UP_TO (
858        ObjDesc->CommonField.BitLength, AccessBitWidth);
859
860    FieldDatumCount = ACPI_ROUND_UP_TO (
861        ObjDesc->CommonField.BitLength +
862        ObjDesc->CommonField.StartFieldBitOffset, AccessBitWidth);
863
864    /* Priming read from the field */
865
866    Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset, &RawDatum, ACPI_READ);
867    if (ACPI_FAILURE (Status))
868    {
869        return_ACPI_STATUS (Status);
870    }
871    MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
872
873    /* Read the rest of the field */
874
875    for (i = 1; i < FieldDatumCount; i++)
876    {
877        /* Get next input datum from the field */
878
879        FieldOffset += ObjDesc->CommonField.AccessByteWidth;
880        Status = AcpiExFieldDatumIo (ObjDesc, FieldOffset,
881                    &RawDatum, ACPI_READ);
882        if (ACPI_FAILURE (Status))
883        {
884            return_ACPI_STATUS (Status);
885        }
886
887        /*
888         * Merge with previous datum if necessary.
889         *
890         * Note: Before the shift, check if the shift value will be larger than
891         * the integer size. If so, there is no need to perform the operation.
892         * This avoids the differences in behavior between different compilers
893         * concerning shift values larger than the target data width.
894         */
895        if (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset <
896            ACPI_INTEGER_BIT_SIZE)
897        {
898            MergedDatum |= RawDatum <<
899                (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
900        }
901
902        if (i == DatumCount)
903        {
904            break;
905        }
906
907        /* Write merged datum to target buffer */
908
909        ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
910            ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
911                BufferLength - BufferOffset));
912
913        BufferOffset += ObjDesc->CommonField.AccessByteWidth;
914        MergedDatum = RawDatum >> ObjDesc->CommonField.StartFieldBitOffset;
915    }
916
917    /* Mask off any extra bits in the last datum */
918
919    BufferTailBits = ObjDesc->CommonField.BitLength % AccessBitWidth;
920    if (BufferTailBits)
921    {
922        MergedDatum &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
923    }
924
925    /* Write the last datum to the buffer */
926
927    ACPI_MEMCPY (((char *) Buffer) + BufferOffset, &MergedDatum,
928        ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
929            BufferLength - BufferOffset));
930
931    return_ACPI_STATUS (AE_OK);
932}
933
934
935/*******************************************************************************
936 *
937 * FUNCTION:    AcpiExInsertIntoField
938 *
939 * PARAMETERS:  ObjDesc             - Field to be written
940 *              Buffer              - Data to be written
941 *              BufferLength        - Length of Buffer
942 *
943 * RETURN:      Status
944 *
945 * DESCRIPTION: Store the Buffer contents into the given field
946 *
947 ******************************************************************************/
948
949ACPI_STATUS
950AcpiExInsertIntoField (
951    ACPI_OPERAND_OBJECT     *ObjDesc,
952    void                    *Buffer,
953    UINT32                  BufferLength)
954{
955    void                    *NewBuffer;
956    ACPI_STATUS             Status;
957    UINT64                  Mask;
958    UINT64                  WidthMask;
959    UINT64                  MergedDatum;
960    UINT64                  RawDatum = 0;
961    UINT32                  FieldOffset = 0;
962    UINT32                  BufferOffset = 0;
963    UINT32                  BufferTailBits;
964    UINT32                  DatumCount;
965    UINT32                  FieldDatumCount;
966    UINT32                  AccessBitWidth;
967    UINT32                  RequiredLength;
968    UINT32                  i;
969
970
971    ACPI_FUNCTION_TRACE (ExInsertIntoField);
972
973
974    /* Validate input buffer */
975
976    NewBuffer = NULL;
977    RequiredLength = ACPI_ROUND_BITS_UP_TO_BYTES (
978                        ObjDesc->CommonField.BitLength);
979    /*
980     * We must have a buffer that is at least as long as the field
981     * we are writing to. This is because individual fields are
982     * indivisible and partial writes are not supported -- as per
983     * the ACPI specification.
984     */
985    if (BufferLength < RequiredLength)
986    {
987        /* We need to create a new buffer */
988
989        NewBuffer = ACPI_ALLOCATE_ZEROED (RequiredLength);
990        if (!NewBuffer)
991        {
992            return_ACPI_STATUS (AE_NO_MEMORY);
993        }
994
995        /*
996         * Copy the original data to the new buffer, starting
997         * at Byte zero. All unused (upper) bytes of the
998         * buffer will be 0.
999         */
1000        ACPI_MEMCPY ((char *) NewBuffer, (char *) Buffer, BufferLength);
1001        Buffer = NewBuffer;
1002        BufferLength = RequiredLength;
1003    }
1004
1005/* TBD: Move to common setup code */
1006
1007    /* Algo is limited to sizeof(UINT64), so cut the AccessByteWidth */
1008    if (ObjDesc->CommonField.AccessByteWidth > sizeof (UINT64))
1009    {
1010        ObjDesc->CommonField.AccessByteWidth = sizeof (UINT64);
1011    }
1012
1013    AccessBitWidth = ACPI_MUL_8 (ObjDesc->CommonField.AccessByteWidth);
1014
1015    /*
1016     * Create the bitmasks used for bit insertion.
1017     * Note: This if/else is used to bypass compiler differences with the
1018     * shift operator
1019     */
1020    if (AccessBitWidth == ACPI_INTEGER_BIT_SIZE)
1021    {
1022        WidthMask = ACPI_UINT64_MAX;
1023    }
1024    else
1025    {
1026        WidthMask = ACPI_MASK_BITS_ABOVE (AccessBitWidth);
1027    }
1028
1029    Mask = WidthMask &
1030        ACPI_MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
1031
1032    /* Compute the number of datums (access width data items) */
1033
1034    DatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength,
1035        AccessBitWidth);
1036
1037    FieldDatumCount = ACPI_ROUND_UP_TO (ObjDesc->CommonField.BitLength +
1038        ObjDesc->CommonField.StartFieldBitOffset,
1039        AccessBitWidth);
1040
1041    /* Get initial Datum from the input buffer */
1042
1043    ACPI_MEMCPY (&RawDatum, Buffer,
1044        ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1045            BufferLength - BufferOffset));
1046
1047    MergedDatum = RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1048
1049    /* Write the entire field */
1050
1051    for (i = 1; i < FieldDatumCount; i++)
1052    {
1053        /* Write merged datum to the target field */
1054
1055        MergedDatum &= Mask;
1056        Status = AcpiExWriteWithUpdateRule (ObjDesc, Mask,
1057                    MergedDatum, FieldOffset);
1058        if (ACPI_FAILURE (Status))
1059        {
1060            goto Exit;
1061        }
1062
1063        FieldOffset += ObjDesc->CommonField.AccessByteWidth;
1064
1065        /*
1066         * Start new output datum by merging with previous input datum
1067         * if necessary.
1068         *
1069         * Note: Before the shift, check if the shift value will be larger than
1070         * the integer size. If so, there is no need to perform the operation.
1071         * This avoids the differences in behavior between different compilers
1072         * concerning shift values larger than the target data width.
1073         */
1074        if ((AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset) <
1075            ACPI_INTEGER_BIT_SIZE)
1076        {
1077            MergedDatum = RawDatum >>
1078                (AccessBitWidth - ObjDesc->CommonField.StartFieldBitOffset);
1079        }
1080        else
1081        {
1082            MergedDatum = 0;
1083        }
1084
1085        Mask = WidthMask;
1086
1087        if (i == DatumCount)
1088        {
1089            break;
1090        }
1091
1092        /* Get the next input datum from the buffer */
1093
1094        BufferOffset += ObjDesc->CommonField.AccessByteWidth;
1095        ACPI_MEMCPY (&RawDatum, ((char *) Buffer) + BufferOffset,
1096            ACPI_MIN(ObjDesc->CommonField.AccessByteWidth,
1097                 BufferLength - BufferOffset));
1098
1099        MergedDatum |= RawDatum << ObjDesc->CommonField.StartFieldBitOffset;
1100    }
1101
1102    /* Mask off any extra bits in the last datum */
1103
1104    BufferTailBits = (ObjDesc->CommonField.BitLength +
1105        ObjDesc->CommonField.StartFieldBitOffset) % AccessBitWidth;
1106    if (BufferTailBits)
1107    {
1108        Mask &= ACPI_MASK_BITS_ABOVE (BufferTailBits);
1109    }
1110
1111    /* Write the last datum to the field */
1112
1113    MergedDatum &= Mask;
1114    Status = AcpiExWriteWithUpdateRule (ObjDesc,
1115                Mask, MergedDatum, FieldOffset);
1116
1117Exit:
1118    /* Free temporary buffer if we used one */
1119
1120    if (NewBuffer)
1121    {
1122        ACPI_FREE (NewBuffer);
1123    }
1124    return_ACPI_STATUS (Status);
1125}
1126