]> CyberLeo.Net >> Repos - FreeBSD/releng/10.2.git/blob - sys/contrib/dev/acpica/components/executer/exfield.c
- Copy stable/10@285827 to releng/10.2 in preparation for 10.2-RC1
[FreeBSD/releng/10.2.git] / sys / contrib / dev / acpica / components / executer / exfield.c
1 /******************************************************************************
2  *
3  * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
4  *
5  *****************************************************************************/
6
7 /*
8  * Copyright (C) 2000 - 2015, Intel Corp.
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions, and the following disclaimer,
16  *    without modification.
17  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18  *    substantially similar to the "NO WARRANTY" disclaimer below
19  *    ("Disclaimer") and any redistribution must be conditioned upon
20  *    including a substantially similar Disclaimer requirement for further
21  *    binary redistribution.
22  * 3. Neither the names of the above-listed copyright holders nor the names
23  *    of any contributors may be used to endorse or promote products derived
24  *    from this software without specific prior written permission.
25  *
26  * Alternatively, this software may be distributed under the terms of the
27  * GNU General Public License ("GPL") version 2 as published by the Free
28  * Software Foundation.
29  *
30  * NO WARRANTY
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41  * POSSIBILITY OF SUCH DAMAGES.
42  */
43
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acdispat.h>
47 #include <contrib/dev/acpica/include/acinterp.h>
48 #include <contrib/dev/acpica/include/amlcode.h>
49
50
51 #define _COMPONENT          ACPI_EXECUTER
52         ACPI_MODULE_NAME    ("exfield")
53
54 /* Local prototypes */
55
56 static UINT32
57 AcpiExGetSerialAccessLength (
58     UINT32                  AccessorType,
59     UINT32                  AccessLength);
60
61
62 /*******************************************************************************
63  *
64  * FUNCTION:    AcpiExGetSerialAccessLength
65  *
66  * PARAMETERS:  AccessorType    - The type of the protocol indicated by region
67  *                                field access attributes
68  *              AccessLength    - The access length of the region field
69  *
70  * RETURN:      Decoded access length
71  *
72  * DESCRIPTION: This routine returns the length of the GenericSerialBus
73  *              protocol bytes
74  *
75  ******************************************************************************/
76
77 static UINT32
78 AcpiExGetSerialAccessLength (
79     UINT32                  AccessorType,
80     UINT32                  AccessLength)
81 {
82     UINT32                  Length;
83
84
85     switch (AccessorType)
86     {
87     case AML_FIELD_ATTRIB_QUICK:
88
89         Length = 0;
90         break;
91
92     case AML_FIELD_ATTRIB_SEND_RCV:
93     case AML_FIELD_ATTRIB_BYTE:
94
95         Length = 1;
96         break;
97
98     case AML_FIELD_ATTRIB_WORD:
99     case AML_FIELD_ATTRIB_WORD_CALL:
100
101         Length = 2;
102         break;
103
104     case AML_FIELD_ATTRIB_MULTIBYTE:
105     case AML_FIELD_ATTRIB_RAW_BYTES:
106     case AML_FIELD_ATTRIB_RAW_PROCESS:
107
108         Length = AccessLength;
109         break;
110
111     case AML_FIELD_ATTRIB_BLOCK:
112     case AML_FIELD_ATTRIB_BLOCK_CALL:
113     default:
114
115         Length = ACPI_GSBUS_BUFFER_SIZE - 2;
116         break;
117     }
118
119     return (Length);
120 }
121
122
123 /*******************************************************************************
124  *
125  * FUNCTION:    AcpiExReadDataFromField
126  *
127  * PARAMETERS:  WalkState           - Current execution state
128  *              ObjDesc             - The named field
129  *              RetBufferDesc       - Where the return data object is stored
130  *
131  * RETURN:      Status
132  *
133  * DESCRIPTION: Read from a named field. Returns either an Integer or a
134  *              Buffer, depending on the size of the field.
135  *
136  ******************************************************************************/
137
138 ACPI_STATUS
139 AcpiExReadDataFromField (
140     ACPI_WALK_STATE         *WalkState,
141     ACPI_OPERAND_OBJECT     *ObjDesc,
142     ACPI_OPERAND_OBJECT     **RetBufferDesc)
143 {
144     ACPI_STATUS             Status;
145     ACPI_OPERAND_OBJECT     *BufferDesc;
146     ACPI_SIZE               Length;
147     void                    *Buffer;
148     UINT32                  Function;
149     UINT16                  AccessorType;
150
151
152     ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
153
154
155     /* Parameter validation */
156
157     if (!ObjDesc)
158     {
159         return_ACPI_STATUS (AE_AML_NO_OPERAND);
160     }
161     if (!RetBufferDesc)
162     {
163         return_ACPI_STATUS (AE_BAD_PARAMETER);
164     }
165
166     if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
167     {
168         /*
169          * If the BufferField arguments have not been previously evaluated,
170          * evaluate them now and save the results.
171          */
172         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
173         {
174             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
175             if (ACPI_FAILURE (Status))
176             {
177                 return_ACPI_STATUS (Status);
178             }
179         }
180     }
181     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
182              (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
183               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
184               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
185     {
186         /*
187          * This is an SMBus, GSBus or IPMI read. We must create a buffer to hold
188          * the data and then directly access the region handler.
189          *
190          * Note: SMBus and GSBus protocol value is passed in upper 16-bits of Function
191          */
192         if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
193         {
194             Length = ACPI_SMBUS_BUFFER_SIZE;
195             Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);
196         }
197         else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
198         {
199             AccessorType = ObjDesc->Field.Attribute;
200             Length = AcpiExGetSerialAccessLength (AccessorType,
201                 ObjDesc->Field.AccessLength);
202
203             /*
204              * Add additional 2 bytes for the GenericSerialBus data buffer:
205              *
206              *     Status;      (Byte 0 of the data buffer)
207              *     Length;      (Byte 1 of the data buffer)
208              *     Data[x-1];   (Bytes 2-x of the arbitrary length data buffer)
209              */
210             Length += 2;
211             Function = ACPI_READ | (AccessorType << 16);
212         }
213         else /* IPMI */
214         {
215             Length = ACPI_IPMI_BUFFER_SIZE;
216             Function = ACPI_READ;
217         }
218
219         BufferDesc = AcpiUtCreateBufferObject (Length);
220         if (!BufferDesc)
221         {
222             return_ACPI_STATUS (AE_NO_MEMORY);
223         }
224
225         /* Lock entire transaction if requested */
226
227         AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
228
229         /* Call the region handler for the read */
230
231         Status = AcpiExAccessRegion (ObjDesc, 0,
232                     ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer),
233                     Function);
234         AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
235         goto Exit;
236     }
237
238     /*
239      * Allocate a buffer for the contents of the field.
240      *
241      * If the field is larger than the current integer width, create
242      * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
243      * the use of arithmetic operators on the returned value if the
244      * field size is equal or smaller than an Integer.
245      *
246      * Note: Field.length is in bits.
247      */
248     Length = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->Field.BitLength);
249     if (Length > AcpiGbl_IntegerByteWidth)
250     {
251         /* Field is too large for an Integer, create a Buffer instead */
252
253         BufferDesc = AcpiUtCreateBufferObject (Length);
254         if (!BufferDesc)
255         {
256             return_ACPI_STATUS (AE_NO_MEMORY);
257         }
258         Buffer = BufferDesc->Buffer.Pointer;
259     }
260     else
261     {
262         /* Field will fit within an Integer (normal case) */
263
264         BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
265         if (!BufferDesc)
266         {
267             return_ACPI_STATUS (AE_NO_MEMORY);
268         }
269
270         Length = AcpiGbl_IntegerByteWidth;
271         Buffer = &BufferDesc->Integer.Value;
272     }
273
274     if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
275         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
276     {
277         /*
278          * For GPIO (GeneralPurposeIo), the Address will be the bit offset
279          * from the previous Connection() operator, making it effectively a
280          * pin number index. The BitLength is the length of the field, which
281          * is thus the number of pins.
282          */
283         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
284             "GPIO FieldRead [FROM]:  Pin %u Bits %u\n",
285             ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));
286
287         /* Lock entire transaction if requested */
288
289         AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
290
291         /* Perform the write */
292
293         Status = AcpiExAccessRegion (ObjDesc, 0,
294                     (UINT64 *) Buffer, ACPI_READ);
295         AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
296         if (ACPI_FAILURE (Status))
297         {
298             AcpiUtRemoveReference (BufferDesc);
299         }
300         else
301         {
302             *RetBufferDesc = BufferDesc;
303         }
304         return_ACPI_STATUS (Status);
305     }
306
307     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
308         "FieldRead [TO]:   Obj %p, Type %X, Buf %p, ByteLen %X\n",
309         ObjDesc, ObjDesc->Common.Type, Buffer, (UINT32) Length));
310     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
311         "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
312         ObjDesc->CommonField.BitLength,
313         ObjDesc->CommonField.StartFieldBitOffset,
314         ObjDesc->CommonField.BaseByteOffset));
315
316     /* Lock entire transaction if requested */
317
318     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
319
320     /* Read from the field */
321
322     Status = AcpiExExtractFromField (ObjDesc, Buffer, (UINT32) Length);
323     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
324
325
326 Exit:
327     if (ACPI_FAILURE (Status))
328     {
329         AcpiUtRemoveReference (BufferDesc);
330     }
331     else
332     {
333         *RetBufferDesc = BufferDesc;
334     }
335
336     return_ACPI_STATUS (Status);
337 }
338
339
340 /*******************************************************************************
341  *
342  * FUNCTION:    AcpiExWriteDataToField
343  *
344  * PARAMETERS:  SourceDesc          - Contains data to write
345  *              ObjDesc             - The named field
346  *              ResultDesc          - Where the return value is returned, if any
347  *
348  * RETURN:      Status
349  *
350  * DESCRIPTION: Write to a named field
351  *
352  ******************************************************************************/
353
354 ACPI_STATUS
355 AcpiExWriteDataToField (
356     ACPI_OPERAND_OBJECT     *SourceDesc,
357     ACPI_OPERAND_OBJECT     *ObjDesc,
358     ACPI_OPERAND_OBJECT     **ResultDesc)
359 {
360     ACPI_STATUS             Status;
361     UINT32                  Length;
362     void                    *Buffer;
363     ACPI_OPERAND_OBJECT     *BufferDesc;
364     UINT32                  Function;
365     UINT16                  AccessorType;
366
367
368     ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
369
370
371     /* Parameter validation */
372
373     if (!SourceDesc || !ObjDesc)
374     {
375         return_ACPI_STATUS (AE_AML_NO_OPERAND);
376     }
377
378     if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
379     {
380         /*
381          * If the BufferField arguments have not been previously evaluated,
382          * evaluate them now and save the results.
383          */
384         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
385         {
386             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
387             if (ACPI_FAILURE (Status))
388             {
389                 return_ACPI_STATUS (Status);
390             }
391         }
392     }
393     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
394              (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
395               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
396               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
397     {
398         /*
399          * This is an SMBus, GSBus or IPMI write. We will bypass the entire field
400          * mechanism and handoff the buffer directly to the handler. For
401          * these address spaces, the buffer is bi-directional; on a write,
402          * return data is returned in the same buffer.
403          *
404          * Source must be a buffer of sufficient size:
405          * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
406          *
407          * Note: SMBus and GSBus protocol type is passed in upper 16-bits of Function
408          */
409         if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)
410         {
411             ACPI_ERROR ((AE_INFO,
412                 "SMBus/IPMI/GenericSerialBus write requires Buffer, found type %s",
413                 AcpiUtGetObjectTypeName (SourceDesc)));
414
415             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
416         }
417
418         if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
419         {
420             Length = ACPI_SMBUS_BUFFER_SIZE;
421             Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
422         }
423         else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
424         {
425             AccessorType = ObjDesc->Field.Attribute;
426             Length = AcpiExGetSerialAccessLength (AccessorType,
427                 ObjDesc->Field.AccessLength);
428
429             /*
430              * Add additional 2 bytes for the GenericSerialBus data buffer:
431              *
432              *     Status;      (Byte 0 of the data buffer)
433              *     Length;      (Byte 1 of the data buffer)
434              *     Data[x-1];   (Bytes 2-x of the arbitrary length data buffer)
435              */
436             Length += 2;
437             Function = ACPI_WRITE | (AccessorType << 16);
438         }
439         else /* IPMI */
440         {
441             Length = ACPI_IPMI_BUFFER_SIZE;
442             Function = ACPI_WRITE;
443         }
444
445         if (SourceDesc->Buffer.Length < Length)
446         {
447             ACPI_ERROR ((AE_INFO,
448                 "SMBus/IPMI/GenericSerialBus write requires Buffer of length %u, found length %u",
449                 Length, SourceDesc->Buffer.Length));
450
451             return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
452         }
453
454         /* Create the bi-directional buffer */
455
456         BufferDesc = AcpiUtCreateBufferObject (Length);
457         if (!BufferDesc)
458         {
459             return_ACPI_STATUS (AE_NO_MEMORY);
460         }
461
462         Buffer = BufferDesc->Buffer.Pointer;
463         ACPI_MEMCPY (Buffer, SourceDesc->Buffer.Pointer, Length);
464
465         /* Lock entire transaction if requested */
466
467         AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
468
469         /*
470          * Perform the write (returns status and perhaps data in the
471          * same buffer)
472          */
473         Status = AcpiExAccessRegion (ObjDesc, 0,
474                     (UINT64 *) Buffer, Function);
475         AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
476
477         *ResultDesc = BufferDesc;
478         return_ACPI_STATUS (Status);
479     }
480     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
481              (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
482     {
483         /*
484          * For GPIO (GeneralPurposeIo), we will bypass the entire field
485          * mechanism and handoff the bit address and bit width directly to
486          * the handler. The Address will be the bit offset
487          * from the previous Connection() operator, making it effectively a
488          * pin number index. The BitLength is the length of the field, which
489          * is thus the number of pins.
490          */
491         if (SourceDesc->Common.Type != ACPI_TYPE_INTEGER)
492         {
493             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
494         }
495
496         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
497             "GPIO FieldWrite [FROM]: (%s:%X), Val %.8X  [TO]:  Pin %u Bits %u\n",
498             AcpiUtGetTypeName (SourceDesc->Common.Type),
499             SourceDesc->Common.Type, (UINT32) SourceDesc->Integer.Value,
500             ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));
501
502         Buffer = &SourceDesc->Integer.Value;
503
504         /* Lock entire transaction if requested */
505
506         AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
507
508         /* Perform the write */
509
510         Status = AcpiExAccessRegion (ObjDesc, 0,
511                     (UINT64 *) Buffer, ACPI_WRITE);
512         AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
513         return_ACPI_STATUS (Status);
514     }
515
516     /* Get a pointer to the data to be written */
517
518     switch (SourceDesc->Common.Type)
519     {
520     case ACPI_TYPE_INTEGER:
521
522         Buffer = &SourceDesc->Integer.Value;
523         Length = sizeof (SourceDesc->Integer.Value);
524         break;
525
526     case ACPI_TYPE_BUFFER:
527
528         Buffer = SourceDesc->Buffer.Pointer;
529         Length = SourceDesc->Buffer.Length;
530         break;
531
532     case ACPI_TYPE_STRING:
533
534         Buffer = SourceDesc->String.Pointer;
535         Length = SourceDesc->String.Length;
536         break;
537
538     default:
539
540         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
541     }
542
543     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
544         "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
545         SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
546         SourceDesc->Common.Type, Buffer, Length));
547
548     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
549         "FieldWrite [TO]:   Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
550         ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
551         ObjDesc->Common.Type,
552         ObjDesc->CommonField.BitLength,
553         ObjDesc->CommonField.StartFieldBitOffset,
554         ObjDesc->CommonField.BaseByteOffset));
555
556     /* Lock entire transaction if requested */
557
558     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
559
560     /* Write to the field */
561
562     Status = AcpiExInsertIntoField (ObjDesc, Buffer, Length);
563     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
564
565     return_ACPI_STATUS (Status);
566 }