xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/executer/exfield.c (revision 6c4a44e36ba846c54467103f884d65dfa13e7fcb)
1 /******************************************************************************
2  *
3  * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
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 __EXFIELD_C__
118 
119 #include "acpi.h"
120 #include "accommon.h"
121 #include "acdispat.h"
122 #include "acinterp.h"
123 
124 
125 #define _COMPONENT          ACPI_EXECUTER
126         ACPI_MODULE_NAME    ("exfield")
127 
128 
129 /*******************************************************************************
130  *
131  * FUNCTION:    AcpiExReadDataFromField
132  *
133  * PARAMETERS:  WalkState           - Current execution state
134  *              ObjDesc             - The named field
135  *              RetBufferDesc       - Where the return data object is stored
136  *
137  * RETURN:      Status
138  *
139  * DESCRIPTION: Read from a named field.  Returns either an Integer or a
140  *              Buffer, depending on the size of the field.
141  *
142  ******************************************************************************/
143 
144 ACPI_STATUS
145 AcpiExReadDataFromField (
146     ACPI_WALK_STATE         *WalkState,
147     ACPI_OPERAND_OBJECT     *ObjDesc,
148     ACPI_OPERAND_OBJECT     **RetBufferDesc)
149 {
150     ACPI_STATUS             Status;
151     ACPI_OPERAND_OBJECT     *BufferDesc;
152     ACPI_SIZE               Length;
153     void                    *Buffer;
154     UINT32                  Function;
155 
156 
157     ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
158 
159 
160     /* Parameter validation */
161 
162     if (!ObjDesc)
163     {
164         return_ACPI_STATUS (AE_AML_NO_OPERAND);
165     }
166     if (!RetBufferDesc)
167     {
168         return_ACPI_STATUS (AE_BAD_PARAMETER);
169     }
170 
171     if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
172     {
173         /*
174          * If the BufferField arguments have not been previously evaluated,
175          * evaluate them now and save the results.
176          */
177         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
178         {
179             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
180             if (ACPI_FAILURE (Status))
181             {
182                 return_ACPI_STATUS (Status);
183             }
184         }
185     }
186     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
187              (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
188               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
189               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
190     {
191         /*
192          * This is an SMBus, GSBus or IPMI read. We must create a buffer to hold
193          * the data and then directly access the region handler.
194          *
195          * Note: SMBus and GSBus protocol value is passed in upper 16-bits of Function
196          */
197         if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
198         {
199             Length = ACPI_SMBUS_BUFFER_SIZE;
200             Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);
201         }
202         else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
203         {
204             Length = ACPI_GSBUS_BUFFER_SIZE;
205             Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);
206         }
207         else /* IPMI */
208         {
209             Length = ACPI_IPMI_BUFFER_SIZE;
210             Function = ACPI_READ;
211         }
212 
213         BufferDesc = AcpiUtCreateBufferObject (Length);
214         if (!BufferDesc)
215         {
216             return_ACPI_STATUS (AE_NO_MEMORY);
217         }
218 
219         /* Lock entire transaction if requested */
220 
221         AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
222 
223         /* Call the region handler for the read */
224 
225         Status = AcpiExAccessRegion (ObjDesc, 0,
226                     ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer),
227                     Function);
228         AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
229         goto Exit;
230     }
231 
232     /*
233      * Allocate a buffer for the contents of the field.
234      *
235      * If the field is larger than the current integer width, create
236      * a BUFFER to hold it.  Otherwise, use an INTEGER.  This allows
237      * the use of arithmetic operators on the returned value if the
238      * field size is equal or smaller than an Integer.
239      *
240      * Note: Field.length is in bits.
241      */
242     Length = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->Field.BitLength);
243     if (Length > AcpiGbl_IntegerByteWidth)
244     {
245         /* Field is too large for an Integer, create a Buffer instead */
246 
247         BufferDesc = AcpiUtCreateBufferObject (Length);
248         if (!BufferDesc)
249         {
250             return_ACPI_STATUS (AE_NO_MEMORY);
251         }
252         Buffer = BufferDesc->Buffer.Pointer;
253     }
254     else
255     {
256         /* Field will fit within an Integer (normal case) */
257 
258         BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
259         if (!BufferDesc)
260         {
261             return_ACPI_STATUS (AE_NO_MEMORY);
262         }
263 
264         Length = AcpiGbl_IntegerByteWidth;
265         Buffer = &BufferDesc->Integer.Value;
266     }
267 
268     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
269         "FieldRead [TO]:   Obj %p, Type %X, Buf %p, ByteLen %X\n",
270         ObjDesc, ObjDesc->Common.Type, Buffer, (UINT32) Length));
271     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
272         "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
273         ObjDesc->CommonField.BitLength,
274         ObjDesc->CommonField.StartFieldBitOffset,
275         ObjDesc->CommonField.BaseByteOffset));
276 
277     /* Lock entire transaction if requested */
278 
279     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
280 
281     /* Read from the field */
282 
283     Status = AcpiExExtractFromField (ObjDesc, Buffer, (UINT32) Length);
284     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
285 
286 
287 Exit:
288     if (ACPI_FAILURE (Status))
289     {
290         AcpiUtRemoveReference (BufferDesc);
291     }
292     else
293     {
294         *RetBufferDesc = BufferDesc;
295     }
296 
297     return_ACPI_STATUS (Status);
298 }
299 
300 
301 /*******************************************************************************
302  *
303  * FUNCTION:    AcpiExWriteDataToField
304  *
305  * PARAMETERS:  SourceDesc          - Contains data to write
306  *              ObjDesc             - The named field
307  *              ResultDesc          - Where the return value is returned, if any
308  *
309  * RETURN:      Status
310  *
311  * DESCRIPTION: Write to a named field
312  *
313  ******************************************************************************/
314 
315 ACPI_STATUS
316 AcpiExWriteDataToField (
317     ACPI_OPERAND_OBJECT     *SourceDesc,
318     ACPI_OPERAND_OBJECT     *ObjDesc,
319     ACPI_OPERAND_OBJECT     **ResultDesc)
320 {
321     ACPI_STATUS             Status;
322     UINT32                  Length;
323     void                    *Buffer;
324     ACPI_OPERAND_OBJECT     *BufferDesc;
325     UINT32                  Function;
326 
327 
328     ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
329 
330 
331     /* Parameter validation */
332 
333     if (!SourceDesc || !ObjDesc)
334     {
335         return_ACPI_STATUS (AE_AML_NO_OPERAND);
336     }
337 
338     if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
339     {
340         /*
341          * If the BufferField arguments have not been previously evaluated,
342          * evaluate them now and save the results.
343          */
344         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
345         {
346             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
347             if (ACPI_FAILURE (Status))
348             {
349                 return_ACPI_STATUS (Status);
350             }
351         }
352     }
353     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
354              (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
355               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
356               ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
357     {
358         /*
359          * This is an SMBus, GSBus or IPMI write. We will bypass the entire field
360          * mechanism and handoff the buffer directly to the handler. For
361          * these address spaces, the buffer is bi-directional; on a write,
362          * return data is returned in the same buffer.
363          *
364          * Source must be a buffer of sufficient size:
365          * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
366          *
367          * Note: SMBus and GSBus protocol type is passed in upper 16-bits of Function
368          */
369         if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)
370         {
371             ACPI_ERROR ((AE_INFO,
372                 "SMBus/IPMI/GenericSerialBus write requires Buffer, found type %s",
373                 AcpiUtGetObjectTypeName (SourceDesc)));
374 
375             return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
376         }
377 
378         if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
379         {
380             Length = ACPI_SMBUS_BUFFER_SIZE;
381             Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
382         }
383         else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
384         {
385             Length = ACPI_GSBUS_BUFFER_SIZE;
386             Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
387         }
388         else /* IPMI */
389         {
390             Length = ACPI_IPMI_BUFFER_SIZE;
391             Function = ACPI_WRITE;
392         }
393 
394         if (SourceDesc->Buffer.Length < Length)
395         {
396             ACPI_ERROR ((AE_INFO,
397                 "SMBus/IPMI/GenericSerialBus write requires Buffer of length %u, found length %u",
398                 Length, SourceDesc->Buffer.Length));
399 
400             return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
401         }
402 
403         /* Create the bi-directional buffer */
404 
405         BufferDesc = AcpiUtCreateBufferObject (Length);
406         if (!BufferDesc)
407         {
408             return_ACPI_STATUS (AE_NO_MEMORY);
409         }
410 
411         Buffer = BufferDesc->Buffer.Pointer;
412         ACPI_MEMCPY (Buffer, SourceDesc->Buffer.Pointer, Length);
413 
414         /* Lock entire transaction if requested */
415 
416         AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
417 
418         /*
419          * Perform the write (returns status and perhaps data in the
420          * same buffer)
421          */
422         Status = AcpiExAccessRegion (ObjDesc, 0,
423                     (UINT64 *) Buffer, Function);
424         AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
425 
426         *ResultDesc = BufferDesc;
427         return_ACPI_STATUS (Status);
428     }
429 
430     /* Get a pointer to the data to be written */
431 
432     switch (SourceDesc->Common.Type)
433     {
434     case ACPI_TYPE_INTEGER:
435         Buffer = &SourceDesc->Integer.Value;
436         Length = sizeof (SourceDesc->Integer.Value);
437         break;
438 
439     case ACPI_TYPE_BUFFER:
440         Buffer = SourceDesc->Buffer.Pointer;
441         Length = SourceDesc->Buffer.Length;
442         break;
443 
444     case ACPI_TYPE_STRING:
445         Buffer = SourceDesc->String.Pointer;
446         Length = SourceDesc->String.Length;
447         break;
448 
449     default:
450         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
451     }
452 
453     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
454         "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
455         SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
456         SourceDesc->Common.Type, Buffer, Length));
457 
458     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
459         "FieldWrite [TO]:   Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
460         ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
461         ObjDesc->Common.Type,
462         ObjDesc->CommonField.BitLength,
463         ObjDesc->CommonField.StartFieldBitOffset,
464         ObjDesc->CommonField.BaseByteOffset));
465 
466     /* Lock entire transaction if requested */
467 
468     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
469 
470     /* Write to the field */
471 
472     Status = AcpiExInsertIntoField (ObjDesc, Buffer, Length);
473     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
474 
475     return_ACPI_STATUS (Status);
476 }
477 
478 
479