exfldio.c revision 83174
1/******************************************************************************
2 *
3 * Module Name: exfldio - Aml Field I/O
4 *              $Revision: 64 $
5 *
6 *****************************************************************************/
7
8/******************************************************************************
9 *
10 * 1. Copyright Notice
11 *
12 * Some or all of this work - Copyright (c) 1999, 2000, 2001, Intel Corp.
13 * All rights reserved.
14 *
15 * 2. License
16 *
17 * 2.1. This is your license from Intel Corp. under its intellectual property
18 * rights.  You may have additional license terms from the party that provided
19 * you this software, covering your right to use that party's intellectual
20 * property rights.
21 *
22 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23 * copy of the source code appearing in this file ("Covered Code") an
24 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25 * base code distributed originally by Intel ("Original Intel Code") to copy,
26 * make derivatives, distribute, use and display any portion of the Covered
27 * Code in any form, with the right to sublicense such rights; and
28 *
29 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30 * license (with the right to sublicense), under only those claims of Intel
31 * patents that are infringed by the Original Intel Code, to make, use, sell,
32 * offer to sell, and import the Covered Code and derivative works thereof
33 * solely to the minimum extent necessary to exercise the above copyright
34 * license, and in no event shall the patent license extend to any additions
35 * to or modifications of the Original Intel Code.  No other license or right
36 * is granted directly or by implication, estoppel or otherwise;
37 *
38 * The above copyright and patent license is granted only if the following
39 * conditions are met:
40 *
41 * 3. Conditions
42 *
43 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44 * Redistribution of source code of any substantial portion of the Covered
45 * Code or modification with rights to further distribute source must include
46 * the above Copyright Notice, the above License, this list of Conditions,
47 * and the following Disclaimer and Export Compliance provision.  In addition,
48 * Licensee must cause all Covered Code to which Licensee contributes to
49 * contain a file documenting the changes Licensee made to create that Covered
50 * Code and the date of any change.  Licensee must include in that file the
51 * documentation of any changes made by any predecessor Licensee.  Licensee
52 * must include a prominent statement that the modification is derived,
53 * directly or indirectly, from Original Intel Code.
54 *
55 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56 * Redistribution of source code of any substantial portion of the Covered
57 * Code or modification without rights to further distribute source must
58 * include the following Disclaimer and Export Compliance provision in the
59 * documentation and/or other materials provided with distribution.  In
60 * addition, Licensee may not authorize further sublicense of source of any
61 * portion of the Covered Code, and must include terms to the effect that the
62 * license from Licensee to its licensee is limited to the intellectual
63 * property embodied in the software Licensee provides to its licensee, and
64 * not to intellectual property embodied in modifications its licensee may
65 * make.
66 *
67 * 3.3. Redistribution of Executable. Redistribution in executable form of any
68 * substantial portion of the Covered Code or modification must reproduce the
69 * above Copyright Notice, and the following Disclaimer and Export Compliance
70 * provision in the documentation and/or other materials provided with the
71 * distribution.
72 *
73 * 3.4. Intel retains all right, title, and interest in and to the Original
74 * Intel Code.
75 *
76 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77 * Intel shall be used in advertising or otherwise to promote the sale, use or
78 * other dealings in products derived from or relating to the Covered Code
79 * without prior written authorization from Intel.
80 *
81 * 4. Disclaimer and Export Compliance
82 *
83 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84 * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86 * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87 * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89 * PARTICULAR PURPOSE.
90 *
91 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98 * LIMITED REMEDY.
99 *
100 * 4.3. Licensee shall not export, either directly or indirectly, any of this
101 * software or system incorporating such software without first obtaining any
102 * required license or other approval from the U. S. Department of Commerce or
103 * any other agency or department of the United States Government.  In the
104 * event Licensee exports any such software from the United States or
105 * re-exports any such software from a foreign destination, Licensee shall
106 * ensure that the distribution and export/re-export of the software is in
107 * compliance with all laws, regulations, orders, or other restrictions of the
108 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109 * any of its subsidiaries will export/re-export any technical data, process,
110 * software, or service, directly or indirectly, to any country for which the
111 * United States government or any agency thereof requires an export license,
112 * other governmental approval, or letter of assurance, without first obtaining
113 * such license, approval or letter.
114 *
115 *****************************************************************************/
116
117
118#define __EXFLDIO_C__
119
120#include "acpi.h"
121#include "acinterp.h"
122#include "amlcode.h"
123#include "acnamesp.h"
124#include "achware.h"
125#include "acevents.h"
126#include "acdispat.h"
127
128
129#define _COMPONENT          ACPI_EXECUTER
130        MODULE_NAME         ("exfldio")
131
132
133/*******************************************************************************
134 *
135 * FUNCTION:    AcpiExSetupField
136 *
137 * PARAMETERS:  *ObjDesc            - Field to be read or written
138 *              FieldDatumByteOffset     - Current offset into the field
139 *
140 * RETURN:      Status
141 *
142 * DESCRIPTION: Common processing for AcpiExExtractFromField and
143 *              AcpiExInsertIntoField
144 *
145 ******************************************************************************/
146
147ACPI_STATUS
148AcpiExSetupField (
149    ACPI_OPERAND_OBJECT     *ObjDesc,
150    UINT32                  FieldDatumByteOffset)
151{
152    ACPI_STATUS             Status = AE_OK;
153    ACPI_OPERAND_OBJECT     *RgnDesc;
154
155
156    FUNCTION_TRACE_U32 ("ExSetupField", FieldDatumByteOffset);
157
158
159    RgnDesc = ObjDesc->CommonField.RegionObj;
160
161    if (ACPI_TYPE_REGION != RgnDesc->Common.Type)
162    {
163        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %x %s\n",
164            RgnDesc->Common.Type, AcpiUtGetTypeName (RgnDesc->Common.Type)));
165        return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
166    }
167
168
169    /*
170     * If the Region Address and Length have not been previously evaluated,
171     * evaluate them now and save the results.
172     */
173    if (!(RgnDesc->Region.Flags & AOPOBJ_DATA_VALID))
174    {
175
176        Status = AcpiDsGetRegionArguments (RgnDesc);
177        if (ACPI_FAILURE (Status))
178        {
179            return_ACPI_STATUS (Status);
180        }
181    }
182
183
184    /*
185     * Validate the request.  The entire request from the byte offset for a
186     * length of one field datum (access width) must fit within the region.
187     * (Region length is specified in bytes)
188     */
189    if (RgnDesc->Region.Length < (ObjDesc->CommonField.BaseByteOffset +
190                                    FieldDatumByteOffset +
191                                    ObjDesc->CommonField.AccessByteWidth))
192    {
193        if (RgnDesc->Region.Length < ObjDesc->CommonField.AccessByteWidth)
194        {
195            /*
196             * This is the case where the AccessType (AccWord, etc.) is wider
197             * than the region itself.  For example, a region of length one
198             * byte, and a field with Dword access specified.
199             */
200            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
201                "Field access width (%d bytes) too large for region size (%X)\n",
202                ObjDesc->CommonField.AccessByteWidth, RgnDesc->Region.Length));
203        }
204
205        /*
206         * Offset rounded up to next multiple of field width
207         * exceeds region length, indicate an error
208         */
209        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
210            "Field base+offset+width %X+%X+%X exceeds region size (%X bytes) field=%p region=%p\n",
211            ObjDesc->CommonField.BaseByteOffset, FieldDatumByteOffset,
212            ObjDesc->CommonField.AccessByteWidth,
213            RgnDesc->Region.Length, ObjDesc, RgnDesc));
214
215        return_ACPI_STATUS (AE_AML_REGION_LIMIT);
216    }
217
218    return_ACPI_STATUS (AE_OK);
219}
220
221
222/*******************************************************************************
223 *
224 * FUNCTION:    AcpiExReadFieldDatum
225 *
226 * PARAMETERS:  *ObjDesc            - Field to be read
227 *              *Value              - Where to store value (must be 32 bits)
228 *
229 * RETURN:      Status
230 *
231 * DESCRIPTION: Retrieve the value of the given field
232 *
233 ******************************************************************************/
234
235ACPI_STATUS
236AcpiExReadFieldDatum (
237    ACPI_OPERAND_OBJECT     *ObjDesc,
238    UINT32                  FieldDatumByteOffset,
239    UINT32                  *Value)
240{
241    ACPI_STATUS             Status;
242    ACPI_OPERAND_OBJECT     *RgnDesc;
243    ACPI_PHYSICAL_ADDRESS   Address;
244    UINT32                  LocalValue;
245
246
247    FUNCTION_TRACE_U32 ("ExReadFieldDatum", FieldDatumByteOffset);
248
249
250    if (!Value)
251    {
252        LocalValue = 0;
253        Value = &LocalValue;    /*  support reads without saving value  */
254    }
255
256    /* Clear the entire return buffer first, [Very Important!] */
257
258    *Value = 0;
259
260
261    /*
262     * BufferFields - Read from a Buffer
263     * Other Fields - Read from a Operation Region.
264     */
265    switch (ObjDesc->Common.Type)
266    {
267    case ACPI_TYPE_BUFFER_FIELD:
268
269        /*
270         * For BufferFields, we only need to copy the data from the
271         * source buffer.  Length is the field width in bytes.
272         */
273        MEMCPY (Value, (ObjDesc->BufferField.BufferObj)->Buffer.Pointer
274                        + ObjDesc->BufferField.BaseByteOffset + FieldDatumByteOffset,
275                        ObjDesc->CommonField.AccessByteWidth);
276        Status = AE_OK;
277        break;
278
279
280    case INTERNAL_TYPE_REGION_FIELD:
281    case INTERNAL_TYPE_BANK_FIELD:
282
283        /*
284         * For other fields, we need to go through an Operation Region
285         * (Only types that will get here are RegionFields and BankFields)
286         */
287        Status = AcpiExSetupField (ObjDesc, FieldDatumByteOffset);
288        if (ACPI_FAILURE (Status))
289        {
290            return_ACPI_STATUS (Status);
291        }
292
293
294        /*
295         * The physical address of this field datum is:
296         *
297         * 1) The base of the region, plus
298         * 2) The base offset of the field, plus
299         * 3) The current offset into the field
300         */
301        RgnDesc = ObjDesc->CommonField.RegionObj;
302        Address = RgnDesc->Region.Address + ObjDesc->CommonField.BaseByteOffset +
303                    FieldDatumByteOffset;
304
305        ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Region %s(%X) width %X base:off %X:%X at %8.8lX%8.8lX\n",
306            AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
307            RgnDesc->Region.SpaceId, ObjDesc->CommonField.AccessBitWidth,
308            ObjDesc->CommonField.BaseByteOffset, FieldDatumByteOffset,
309            HIDWORD(Address), LODWORD(Address)));
310
311
312        /* Invoke the appropriate AddressSpace/OpRegion handler */
313
314        Status = AcpiEvAddressSpaceDispatch (RgnDesc, ACPI_READ_ADR_SPACE,
315                        Address, ObjDesc->CommonField.AccessBitWidth, Value);
316        if (Status == AE_NOT_IMPLEMENTED)
317        {
318            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Region %s(%X) not implemented\n",
319                AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
320                RgnDesc->Region.SpaceId));
321        }
322
323        else if (Status == AE_NOT_EXIST)
324        {
325            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Region %s(%X) has no handler\n",
326                AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
327                RgnDesc->Region.SpaceId));
328        }
329        break;
330
331
332    default:
333
334        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "%p, wrong source type - %s\n",
335            ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type)));
336        Status = AE_AML_INTERNAL;
337        break;
338    }
339
340
341    ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Returned value=%08lX \n", *Value));
342
343    return_ACPI_STATUS (Status);
344}
345
346
347/*******************************************************************************
348 *
349 * FUNCTION:    AcpiExGetBufferDatum
350 *
351 * PARAMETERS:  MergedDatum         - Value to store
352 *              Buffer              - Receiving buffer
353 *              ByteGranularity     - 1/2/4 Granularity of the field
354 *                                    (aka Datum Size)
355 *              Offset              - Datum offset into the buffer
356 *
357 * RETURN:      none
358 *
359 * DESCRIPTION: Store the merged datum to the buffer according to the
360 *              byte granularity
361 *
362 ******************************************************************************/
363
364static void
365AcpiExGetBufferDatum(
366    UINT32                  *Datum,
367    void                    *Buffer,
368    UINT32                  ByteGranularity,
369    UINT32                  Offset)
370{
371
372    FUNCTION_ENTRY ();
373
374
375    switch (ByteGranularity)
376    {
377    case ACPI_FIELD_BYTE_GRANULARITY:
378        *Datum = ((UINT8 *) Buffer) [Offset];
379        break;
380
381    case ACPI_FIELD_WORD_GRANULARITY:
382        MOVE_UNALIGNED16_TO_32 (Datum, &(((UINT16 *) Buffer) [Offset]));
383        break;
384
385    case ACPI_FIELD_DWORD_GRANULARITY:
386        MOVE_UNALIGNED32_TO_32 (Datum, &(((UINT32 *) Buffer) [Offset]));
387        break;
388    }
389}
390
391
392/*******************************************************************************
393 *
394 * FUNCTION:    AcpiExSetBufferDatum
395 *
396 * PARAMETERS:  MergedDatum         - Value to store
397 *              Buffer              - Receiving buffer
398 *              ByteGranularity     - 1/2/4 Granularity of the field
399 *                                    (aka Datum Size)
400 *              Offset              - Datum offset into the buffer
401 *
402 * RETURN:      none
403 *
404 * DESCRIPTION: Store the merged datum to the buffer according to the
405 *              byte granularity
406 *
407 ******************************************************************************/
408
409static void
410AcpiExSetBufferDatum (
411    UINT32                  MergedDatum,
412    void                    *Buffer,
413    UINT32                  ByteGranularity,
414    UINT32                  Offset)
415{
416
417    FUNCTION_ENTRY ();
418
419
420    switch (ByteGranularity)
421    {
422    case ACPI_FIELD_BYTE_GRANULARITY:
423        ((UINT8 *) Buffer) [Offset] = (UINT8) MergedDatum;
424        break;
425
426    case ACPI_FIELD_WORD_GRANULARITY:
427        MOVE_UNALIGNED16_TO_16 (&(((UINT16 *) Buffer)[Offset]), &MergedDatum);
428        break;
429
430    case ACPI_FIELD_DWORD_GRANULARITY:
431        MOVE_UNALIGNED32_TO_32 (&(((UINT32 *) Buffer)[Offset]), &MergedDatum);
432        break;
433    }
434}
435
436
437/*******************************************************************************
438 *
439 * FUNCTION:    AcpiExExtractFromField
440 *
441 * PARAMETERS:  *ObjDesc            - Field to be read
442 *              *Value              - Where to store value
443 *
444 * RETURN:      Status
445 *
446 * DESCRIPTION: Retrieve the value of the given field
447 *
448 ******************************************************************************/
449
450ACPI_STATUS
451AcpiExExtractFromField (
452    ACPI_OPERAND_OBJECT     *ObjDesc,
453    void                    *Buffer,
454    UINT32                  BufferLength)
455{
456    ACPI_STATUS             Status;
457    UINT32                  FieldDatumByteOffset;
458    UINT32                  DatumOffset;
459    UINT32                  PreviousRawDatum;
460    UINT32                  ThisRawDatum = 0;
461    UINT32                  MergedDatum = 0;
462    UINT32                  ByteFieldLength;
463    UINT32                  DatumCount;
464
465
466    FUNCTION_TRACE ("ExExtractFromField");
467
468
469    /*
470     * The field must fit within the caller's buffer
471     */
472    ByteFieldLength = ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
473    if (ByteFieldLength > BufferLength)
474    {
475        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Field size %X (bytes) too large for buffer (%X)\n",
476            ByteFieldLength, BufferLength));
477
478        return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
479    }
480
481    /* Convert field byte count to datum count, round up if necessary */
482
483    DatumCount = ROUND_UP_TO (ByteFieldLength, ObjDesc->CommonField.AccessByteWidth);
484
485    ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
486        "ByteLen=%x, DatumLen=%x, BitGran=%x, ByteGran=%x\n",
487        ByteFieldLength, DatumCount, ObjDesc->CommonField.AccessBitWidth,
488        ObjDesc->CommonField.AccessByteWidth));
489
490
491    /*
492     * Clear the caller's buffer (the whole buffer length as given)
493     * This is very important, especially in the cases where a byte is read,
494     * but the buffer is really a UINT32 (4 bytes).
495     */
496    MEMSET (Buffer, 0, BufferLength);
497
498    /* Read the first raw datum to prime the loop */
499
500    FieldDatumByteOffset = 0;
501    DatumOffset= 0;
502
503    Status = AcpiExReadFieldDatum (ObjDesc, FieldDatumByteOffset, &PreviousRawDatum);
504    if (ACPI_FAILURE (Status))
505    {
506        return_ACPI_STATUS (Status);
507    }
508
509
510    /* We might actually be done if the request fits in one datum */
511
512    if ((DatumCount == 1) &&
513        (ObjDesc->CommonField.AccessFlags & AFIELD_SINGLE_DATUM))
514    {
515        /* 1) Shift the valid data bits down to start at bit 0 */
516
517        MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
518
519        /* 2) Mask off any upper unused bits (bits not part of the field) */
520
521        if (ObjDesc->CommonField.EndBufferValidBits)
522        {
523            MergedDatum &= MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
524        }
525
526        /* Store the datum to the caller buffer */
527
528        AcpiExSetBufferDatum (MergedDatum, Buffer, ObjDesc->CommonField.AccessByteWidth,
529                DatumOffset);
530
531        return_ACPI_STATUS (AE_OK);
532    }
533
534
535    /* We need to get more raw data to complete one or more field data */
536
537    while (DatumOffset < DatumCount)
538    {
539        FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
540
541        /*
542         * If the field is aligned on a byte boundary, we don't want
543         * to perform a final read, since this would potentially read
544         * past the end of the region.
545         *
546         * TBD: [Investigate] It may make more sense to just split the aligned
547         * and non-aligned cases since the aligned case is so very simple,
548         */
549        if ((ObjDesc->CommonField.StartFieldBitOffset != 0)       ||
550            ((ObjDesc->CommonField.StartFieldBitOffset == 0)      &&
551            (DatumOffset < (DatumCount -1))))
552        {
553            /*
554             * Get the next raw datum, it contains some or all bits
555             * of the current field datum
556             */
557            Status = AcpiExReadFieldDatum (ObjDesc, FieldDatumByteOffset, &ThisRawDatum);
558            if (ACPI_FAILURE (Status))
559            {
560                return_ACPI_STATUS (Status);
561            }
562        }
563
564        /*
565         * Create the (possibly) merged datum to be stored to the caller buffer
566         */
567        if (ObjDesc->CommonField.StartFieldBitOffset == 0)
568        {
569            /* Field is not skewed and we can just copy the datum */
570
571            MergedDatum = PreviousRawDatum;
572        }
573
574        else
575        {
576            /*
577             * Put together the appropriate bits of the two raw data to make a
578             * single complete field datum
579             *
580             * 1) Normalize the first datum down to bit 0
581             */
582            MergedDatum = (PreviousRawDatum >> ObjDesc->CommonField.StartFieldBitOffset);
583
584            /* 2) Insert the second datum "above" the first datum */
585
586            MergedDatum |= (ThisRawDatum << ObjDesc->CommonField.DatumValidBits);
587
588            if ((DatumOffset >= (DatumCount -1)))
589            {
590                /*
591                 * This is the last iteration of the loop.  We need to clear
592                 * any unused bits (bits that are not part of this field) that
593                 * came from the last raw datum before we store the final
594                 * merged datum into the caller buffer.
595                 */
596                if (ObjDesc->CommonField.EndBufferValidBits)
597                {
598                    MergedDatum &=
599                        MASK_BITS_ABOVE (ObjDesc->CommonField.EndBufferValidBits);
600                }
601            }
602        }
603
604
605        /*
606         * Store the merged field datum in the caller's buffer, according to
607         * the granularity of the field (size of each datum).
608         */
609        AcpiExSetBufferDatum (MergedDatum, Buffer, ObjDesc->CommonField.AccessByteWidth,
610                DatumOffset);
611
612        /*
613         * Save the raw datum that was just acquired since it may contain bits
614         * of the *next* field datum.  Update offsets
615         */
616        PreviousRawDatum = ThisRawDatum;
617        DatumOffset++;
618    }
619
620
621    return_ACPI_STATUS (AE_OK);
622}
623
624
625/*******************************************************************************
626 *
627 * FUNCTION:    AcpiExWriteFieldDatum
628 *
629 * PARAMETERS:  *ObjDesc            - Field to be set
630 *              Value               - Value to store
631 *
632 * RETURN:      Status
633 *
634 * DESCRIPTION: Store the value into the given field
635 *
636 ******************************************************************************/
637
638static ACPI_STATUS
639AcpiExWriteFieldDatum (
640    ACPI_OPERAND_OBJECT     *ObjDesc,
641    UINT32                  FieldDatumByteOffset,
642    UINT32                  Value)
643{
644    ACPI_STATUS             Status = AE_OK;
645    ACPI_OPERAND_OBJECT     *RgnDesc = NULL;
646    ACPI_PHYSICAL_ADDRESS   Address;
647
648
649    FUNCTION_TRACE_U32 ("ExWriteFieldDatum", FieldDatumByteOffset);
650
651
652    /*
653     * BufferFields - Read from a Buffer
654     * Other Fields - Read from a Operation Region.
655     */
656    switch (ObjDesc->Common.Type)
657    {
658    case ACPI_TYPE_BUFFER_FIELD:
659
660        /*
661         * For BufferFields, we only need to copy the data to the
662         * target buffer.  Length is the field width in bytes.
663         */
664        MEMCPY ((ObjDesc->BufferField.BufferObj)->Buffer.Pointer
665                + ObjDesc->BufferField.BaseByteOffset + FieldDatumByteOffset,
666                &Value, ObjDesc->CommonField.AccessByteWidth);
667        Status = AE_OK;
668        break;
669
670
671    case INTERNAL_TYPE_REGION_FIELD:
672    case INTERNAL_TYPE_BANK_FIELD:
673
674        /*
675         * For other fields, we need to go through an Operation Region
676         * (Only types that will get here are RegionFields and BankFields)
677         */
678        Status = AcpiExSetupField (ObjDesc, FieldDatumByteOffset);
679        if (ACPI_FAILURE (Status))
680        {
681            return_ACPI_STATUS (Status);
682        }
683
684        /*
685         * The physical address of this field datum is:
686         *
687         * 1) The base of the region, plus
688         * 2) The base offset of the field, plus
689         * 3) The current offset into the field
690         */
691        RgnDesc = ObjDesc->CommonField.RegionObj;
692        Address = RgnDesc->Region.Address +
693                    ObjDesc->CommonField.BaseByteOffset +
694                    FieldDatumByteOffset;
695
696        ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
697            "Store %X in Region %s(%X) at %8.8lX%8.8lX width %X\n",
698            Value, AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
699            RgnDesc->Region.SpaceId, HIDWORD(Address), LODWORD(Address),
700            ObjDesc->CommonField.AccessBitWidth));
701
702        /* Invoke the appropriate AddressSpace/OpRegion handler */
703
704        Status = AcpiEvAddressSpaceDispatch (RgnDesc, ACPI_WRITE_ADR_SPACE,
705                        Address, ObjDesc->CommonField.AccessBitWidth, &Value);
706
707        if (Status == AE_NOT_IMPLEMENTED)
708        {
709            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
710                "**** Region type %s(%X) not implemented\n",
711                AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
712                RgnDesc->Region.SpaceId));
713        }
714
715        else if (Status == AE_NOT_EXIST)
716        {
717            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
718                "**** Region type %s(%X) does not have a handler\n",
719                AcpiUtGetRegionName (RgnDesc->Region.SpaceId),
720                RgnDesc->Region.SpaceId));
721        }
722
723        break;
724
725
726    default:
727
728        ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "%p, wrong source type - %s\n",
729            ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type)));
730        Status = AE_AML_INTERNAL;
731        break;
732    }
733
734
735    ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value written=%08lX \n", Value));
736    return_ACPI_STATUS (Status);
737}
738
739
740/*******************************************************************************
741 *
742 * FUNCTION:    AcpiExWriteFieldDatumWithUpdateRule
743 *
744 * PARAMETERS:  *ObjDesc            - Field to be set
745 *              Value               - Value to store
746 *
747 * RETURN:      Status
748 *
749 * DESCRIPTION: Apply the field update rule to a field write
750 *
751 ******************************************************************************/
752
753static ACPI_STATUS
754AcpiExWriteFieldDatumWithUpdateRule (
755    ACPI_OPERAND_OBJECT     *ObjDesc,
756    UINT32                  Mask,
757    UINT32                  FieldValue,
758    UINT32                  FieldDatumByteOffset)
759{
760    ACPI_STATUS             Status = AE_OK;
761    UINT32                  MergedValue;
762    UINT32                  CurrentValue;
763
764
765    FUNCTION_TRACE ("ExWriteFieldDatumWithUpdateRule");
766
767
768    /* Start with the new bits  */
769
770    MergedValue = FieldValue;
771
772
773    /* If the mask is all ones, we don't need to worry about the update rule */
774
775    if (Mask != ACPI_UINT32_MAX)
776    {
777        /* Decode the update rule */
778
779        switch (ObjDesc->CommonField.UpdateRule)
780        {
781
782        case UPDATE_PRESERVE:
783
784            /*
785             * Check if update rule needs to be applied (not if mask is all
786             * ones)  The left shift drops the bits we want to ignore.
787             */
788            if ((~Mask << (sizeof (Mask) * 8 -
789                            ObjDesc->CommonField.AccessBitWidth)) != 0)
790            {
791                /*
792                 * Read the current contents of the byte/word/dword containing
793                 * the field, and merge with the new field value.
794                 */
795                Status = AcpiExReadFieldDatum (ObjDesc, FieldDatumByteOffset,
796                                &CurrentValue);
797                MergedValue |= (CurrentValue & ~Mask);
798            }
799            break;
800
801
802        case UPDATE_WRITE_AS_ONES:
803
804            /* Set positions outside the field to all ones */
805
806            MergedValue |= ~Mask;
807            break;
808
809
810        case UPDATE_WRITE_AS_ZEROS:
811
812            /* Set positions outside the field to all zeros */
813
814            MergedValue &= Mask;
815            break;
816
817
818        default:
819            ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
820                "WriteWithUpdateRule: Unknown UpdateRule setting: %x\n",
821                ObjDesc->CommonField.UpdateRule));
822            return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
823            break;
824        }
825    }
826
827
828    /* Write the merged value */
829
830    Status = AcpiExWriteFieldDatum (ObjDesc, FieldDatumByteOffset,
831                    MergedValue);
832
833    ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Mask %X DatumOffset %X Value %X, MergedValue %X\n",
834        Mask, FieldDatumByteOffset, FieldValue, MergedValue));
835
836    return_ACPI_STATUS (Status);
837}
838
839
840/*******************************************************************************
841 *
842 * FUNCTION:    AcpiExInsertIntoField
843 *
844 * PARAMETERS:  *ObjDesc            - Field to be set
845 *              Buffer              - Value to store
846 *
847 * RETURN:      Status
848 *
849 * DESCRIPTION: Store the value into the given field
850 *
851 ******************************************************************************/
852
853ACPI_STATUS
854AcpiExInsertIntoField (
855    ACPI_OPERAND_OBJECT     *ObjDesc,
856    void                    *Buffer,
857    UINT32                  BufferLength)
858{
859    ACPI_STATUS             Status;
860    UINT32                  FieldDatumByteOffset;
861    UINT32                  DatumOffset;
862    UINT32                  Mask;
863    UINT32                  MergedDatum;
864    UINT32                  PreviousRawDatum;
865    UINT32                  ThisRawDatum;
866    UINT32                  ByteFieldLength;
867    UINT32                  DatumCount;
868
869
870    FUNCTION_TRACE ("ExInsertIntoField");
871
872
873    /*
874     * Incoming buffer must be at least as long as the field, we do not
875     * allow "partial" field writes.  We do not care if the buffer is
876     * larger than the field, this typically happens when an integer is
877     * written to a field that is actually smaller than an integer.
878     */
879    ByteFieldLength = ROUND_BITS_UP_TO_BYTES (ObjDesc->CommonField.BitLength);
880    if (BufferLength < ByteFieldLength)
881    {
882        ACPI_DEBUG_PRINT ((ACPI_DB_INFO, "Buffer length %X too small for field %X\n",
883            BufferLength, ByteFieldLength));
884
885        /* TBD: Need a better error code */
886
887        return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
888    }
889
890    /* Convert byte count to datum count, round up if necessary */
891
892    DatumCount = ROUND_UP_TO (ByteFieldLength, ObjDesc->CommonField.AccessByteWidth);
893
894    ACPI_DEBUG_PRINT ((ACPI_DB_INFO,
895        "ByteLen=%x, DatumLen=%x, BitGran=%x, ByteGran=%x\n",
896        ByteFieldLength, DatumCount, ObjDesc->CommonField.AccessBitWidth,
897        ObjDesc->CommonField.AccessByteWidth));
898
899
900    /*
901     * Break the request into up to three parts (similar to an I/O request):
902     * 1) non-aligned part at start
903     * 2) aligned part in middle
904     * 3) non-aligned part at the end
905     */
906    FieldDatumByteOffset = 0;
907    DatumOffset= 0;
908
909    /* Get a single datum from the caller's buffer */
910
911    AcpiExGetBufferDatum (&PreviousRawDatum, Buffer,
912            ObjDesc->CommonField.AccessByteWidth, DatumOffset);
913
914    /*
915     * Part1:
916     * Write a partial field datum if field does not begin on a datum boundary
917     * Note: The code in this section also handles the aligned case
918     *
919     * Construct Mask with 1 bits where the field is, 0 bits elsewhere
920     * (Only the bottom 5 bits of BitLength are valid for a shift operation)
921     *
922     * Mask off bits that are "below" the field (if any)
923     */
924    Mask = MASK_BITS_BELOW (ObjDesc->CommonField.StartFieldBitOffset);
925
926    /* If the field fits in one datum, may need to mask upper bits */
927
928    if ((ObjDesc->CommonField.AccessFlags & AFIELD_SINGLE_DATUM) &&
929         ObjDesc->CommonField.EndFieldValidBits)
930    {
931        /* There are bits above the field, mask them off also */
932
933        Mask &= MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
934    }
935
936    /* Shift and mask the value into the field position */
937
938    MergedDatum = (PreviousRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
939    MergedDatum &= Mask;
940
941    /* Apply the update rule (if necessary) and write the datum to the field */
942
943    Status = AcpiExWriteFieldDatumWithUpdateRule (ObjDesc, Mask, MergedDatum,
944                        FieldDatumByteOffset);
945    if (ACPI_FAILURE (Status))
946    {
947        return_ACPI_STATUS (Status);
948    }
949
950    /* If the entire field fits within one datum, we are done. */
951
952    if ((DatumCount == 1) &&
953       (ObjDesc->CommonField.AccessFlags & AFIELD_SINGLE_DATUM))
954    {
955        return_ACPI_STATUS (AE_OK);
956    }
957
958    /*
959     * Part2:
960     * Write the aligned data.
961     *
962     * We don't need to worry about the update rule for these data, because
963     * all of the bits in each datum are part of the field.
964     *
965     * The last datum must be special cased because it might contain bits
966     * that are not part of the field -- therefore the "update rule" must be
967     * applied in Part3 below.
968     */
969    while (DatumOffset < DatumCount)
970    {
971        DatumOffset++;
972        FieldDatumByteOffset += ObjDesc->CommonField.AccessByteWidth;
973
974        /*
975         * Get the next raw buffer datum.  It may contain bits of the previous
976         * field datum
977         */
978        AcpiExGetBufferDatum (&ThisRawDatum, Buffer,
979                ObjDesc->CommonField.AccessByteWidth, DatumOffset);
980
981        /* Create the field datum based on the field alignment */
982
983        if (ObjDesc->CommonField.StartFieldBitOffset != 0)
984        {
985            /*
986             * Put together appropriate bits of the two raw buffer data to make
987             * a single complete field datum
988             */
989            MergedDatum =
990                (PreviousRawDatum >> ObjDesc->CommonField.DatumValidBits) |
991                (ThisRawDatum << ObjDesc->CommonField.StartFieldBitOffset);
992        }
993
994        else
995        {
996            /* Field began aligned on datum boundary */
997
998            MergedDatum = ThisRawDatum;
999        }
1000
1001
1002        /*
1003         * Special handling for the last datum if the field does NOT end on
1004         * a datum boundary.  Update Rule must be applied to the bits outside
1005         * the field.
1006         */
1007        if ((DatumOffset == DatumCount)             &&
1008            ObjDesc->CommonField.EndFieldValidBits)
1009        {
1010            /*
1011             * Part3:
1012             * This is the last datum and the field does not end on a datum boundary.
1013             * Build the partial datum and write with the update rule.
1014             */
1015
1016            /* Mask off the unused bits above (after) the end-of-field */
1017
1018            Mask = MASK_BITS_ABOVE (ObjDesc->CommonField.EndFieldValidBits);
1019            MergedDatum &= Mask;
1020
1021            /* Write the last datum with the update rule */
1022
1023            Status = AcpiExWriteFieldDatumWithUpdateRule (ObjDesc, Mask,
1024                            MergedDatum, FieldDatumByteOffset);
1025            if (ACPI_FAILURE (Status))
1026            {
1027                return_ACPI_STATUS (Status);
1028            }
1029        }
1030
1031        else
1032        {
1033            /* Normal case -- write the completed datum */
1034
1035            Status = AcpiExWriteFieldDatum (ObjDesc,
1036                            FieldDatumByteOffset, MergedDatum);
1037            if (ACPI_FAILURE (Status))
1038            {
1039                return_ACPI_STATUS (Status);
1040            }
1041        }
1042
1043        /*
1044         * Save the most recent datum since it may contain bits of the *next*
1045         * field datum.  Update current byte offset.
1046         */
1047        PreviousRawDatum = ThisRawDatum;
1048    }
1049
1050
1051    return_ACPI_STATUS (Status);
1052}
1053
1054
1055