1 /******************************************************************************
2  *
3  * Module Name: exfield - AML execution - FieldUnit read/write
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2019, 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 "acpi.h"
45 #include "accommon.h"
46 #include "acdispat.h"
47 #include "acinterp.h"
48 #include "amlcode.h"
49 
50 
51 #define _COMPONENT          ACPI_EXECUTER
52         ACPI_MODULE_NAME    ("exfield")
53 
54 
55 /*
56  * This table maps the various Attrib protocols to the byte transfer
57  * length. Used for the generic serial bus.
58  */
59 #define ACPI_INVALID_PROTOCOL_ID        0x80
60 #define ACPI_MAX_PROTOCOL_ID            0x0F
61 
62 const UINT8     AcpiProtocolLengths[] =
63 {
64     ACPI_INVALID_PROTOCOL_ID,   /* 0 - reserved */
65     ACPI_INVALID_PROTOCOL_ID,   /* 1 - reserved */
66     0x00,                       /* 2 - ATTRIB_QUICK */
67     ACPI_INVALID_PROTOCOL_ID,   /* 3 - reserved */
68     0x01,                       /* 4 - ATTRIB_SEND_RECEIVE */
69     ACPI_INVALID_PROTOCOL_ID,   /* 5 - reserved */
70     0x01,                       /* 6 - ATTRIB_BYTE */
71     ACPI_INVALID_PROTOCOL_ID,   /* 7 - reserved */
72     0x02,                       /* 8 - ATTRIB_WORD */
73     ACPI_INVALID_PROTOCOL_ID,   /* 9 - reserved */
74     0xFF,                       /* A - ATTRIB_BLOCK  */
75     0xFF,                       /* B - ATTRIB_BYTES */
76     0x02,                       /* C - ATTRIB_PROCESS_CALL */
77     0xFF,                       /* D - ATTRIB_BLOCK_PROCESS_CALL */
78     0xFF,                       /* E - ATTRIB_RAW_BYTES */
79     0xFF                        /* F - ATTRIB_RAW_PROCESS_BYTES */
80 };
81 
82 #define PCC_MASTER_SUBSPACE     3
83 
84 /*
85  * The following macros determine a given offset is a COMD field.
86  * According to the specification, generic subspaces (types 0-2) contains a
87  * 2-byte COMD field at offset 4 and master subspaces (type 3) contains a 4-byte
88  * COMD field starting at offset 12.
89  */
90 #define GENERIC_SUBSPACE_COMMAND(a)     (4 == a || a == 5)
91 #define MASTER_SUBSPACE_COMMAND(a)      (12 <= a && a <= 15)
92 
93 
94 /*******************************************************************************
95  *
96  * FUNCTION:    AcpiExGetProtocolBufferLength
97  *
98  * PARAMETERS:  ProtocolId      - The type of the protocol indicated by region
99  *                                field access attributes
100  *              ReturnLength    - Where the protocol byte transfer length is
101  *                                returned
102  *
103  * RETURN:      Status and decoded byte transfer length
104  *
105  * DESCRIPTION: This routine returns the length of the GenericSerialBus
106  *              protocol bytes
107  *
108  ******************************************************************************/
109 
110 ACPI_STATUS
111 AcpiExGetProtocolBufferLength (
112     UINT32                  ProtocolId,
113     UINT32                  *ReturnLength)
114 {
115 
116     if ((ProtocolId > ACPI_MAX_PROTOCOL_ID) ||
117         (AcpiProtocolLengths[ProtocolId] == ACPI_INVALID_PROTOCOL_ID))
118     {
119         ACPI_ERROR ((AE_INFO,
120             "Invalid Field/AccessAs protocol ID: 0x%4.4X", ProtocolId));
121 
122         return (AE_AML_PROTOCOL);
123     }
124 
125     *ReturnLength = AcpiProtocolLengths[ProtocolId];
126     return (AE_OK);
127 }
128 
129 
130 /*******************************************************************************
131  *
132  * FUNCTION:    AcpiExReadDataFromField
133  *
134  * PARAMETERS:  WalkState           - Current execution state
135  *              ObjDesc             - The named field
136  *              RetBufferDesc       - Where the return data object is stored
137  *
138  * RETURN:      Status
139  *
140  * DESCRIPTION: Read from a named field. Returns either an Integer or a
141  *              Buffer, depending on the size of the field.
142  *
143  ******************************************************************************/
144 
145 ACPI_STATUS
146 AcpiExReadDataFromField (
147     ACPI_WALK_STATE         *WalkState,
148     ACPI_OPERAND_OBJECT     *ObjDesc,
149     ACPI_OPERAND_OBJECT     **RetBufferDesc)
150 {
151     ACPI_STATUS             Status;
152     ACPI_OPERAND_OBJECT     *BufferDesc;
153     void                    *Buffer;
154     UINT32                  BufferLength;
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         /* SMBus, GSBus, IPMI serial */
192 
193         Status = AcpiExReadSerialBus (ObjDesc, RetBufferDesc);
194         return_ACPI_STATUS (Status);
195     }
196 
197     /*
198      * Allocate a buffer for the contents of the field.
199      *
200      * If the field is larger than the current integer width, create
201      * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
202      * the use of arithmetic operators on the returned value if the
203      * field size is equal or smaller than an Integer.
204      *
205      * Note: Field.length is in bits.
206      */
207     BufferLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
208         ObjDesc->Field.BitLength);
209 
210     if (BufferLength > AcpiGbl_IntegerByteWidth)
211     {
212         /* Field is too large for an Integer, create a Buffer instead */
213 
214         BufferDesc = AcpiUtCreateBufferObject (BufferLength);
215         if (!BufferDesc)
216         {
217             return_ACPI_STATUS (AE_NO_MEMORY);
218         }
219         Buffer = BufferDesc->Buffer.Pointer;
220     }
221     else
222     {
223         /* Field will fit within an Integer (normal case) */
224 
225         BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
226         if (!BufferDesc)
227         {
228             return_ACPI_STATUS (AE_NO_MEMORY);
229         }
230 
231         BufferLength = AcpiGbl_IntegerByteWidth;
232         Buffer = &BufferDesc->Integer.Value;
233     }
234 
235     if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
236         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
237     {
238         /* General Purpose I/O */
239 
240         Status = AcpiExReadGpio (ObjDesc, Buffer);
241         goto Exit;
242     }
243     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
244         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_COMM))
245     {
246         /*
247          * Reading from a PCC field unit does not require the handler because
248          * it only requires reading from the InternalPccBuffer.
249          */
250         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
251             "PCC FieldRead bits %u\n", ObjDesc->Field.BitLength));
252 
253         memcpy (Buffer, ObjDesc->Field.RegionObj->Field.InternalPccBuffer +
254         ObjDesc->Field.BaseByteOffset, (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
255             ObjDesc->Field.BitLength));
256 
257         *RetBufferDesc = BufferDesc;
258         return AE_OK;
259     }
260 
261     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
262         "FieldRead [TO]:   Obj %p, Type %X, Buf %p, ByteLen %X\n",
263         ObjDesc, ObjDesc->Common.Type, Buffer, BufferLength));
264     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
265         "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
266         ObjDesc->CommonField.BitLength,
267         ObjDesc->CommonField.StartFieldBitOffset,
268         ObjDesc->CommonField.BaseByteOffset));
269 
270     /* Lock entire transaction if requested */
271 
272     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
273 
274     /* Read from the field */
275 
276     Status = AcpiExExtractFromField (ObjDesc, Buffer, BufferLength);
277     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
278 
279 
280 Exit:
281     if (ACPI_FAILURE (Status))
282     {
283         AcpiUtRemoveReference (BufferDesc);
284     }
285     else
286     {
287         *RetBufferDesc = BufferDesc;
288     }
289 
290     return_ACPI_STATUS (Status);
291 }
292 
293 
294 /*******************************************************************************
295  *
296  * FUNCTION:    AcpiExWriteDataToField
297  *
298  * PARAMETERS:  SourceDesc          - Contains data to write
299  *              ObjDesc             - The named field
300  *              ResultDesc          - Where the return value is returned, if any
301  *
302  * RETURN:      Status
303  *
304  * DESCRIPTION: Write to a named field
305  *
306  ******************************************************************************/
307 
308 ACPI_STATUS
309 AcpiExWriteDataToField (
310     ACPI_OPERAND_OBJECT     *SourceDesc,
311     ACPI_OPERAND_OBJECT     *ObjDesc,
312     ACPI_OPERAND_OBJECT     **ResultDesc)
313 {
314     ACPI_STATUS             Status;
315     UINT32                  BufferLength;
316     UINT32                  DataLength;
317     void                    *Buffer;
318 
319 
320     ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
321 
322 
323     /* Parameter validation */
324 
325     if (!SourceDesc || !ObjDesc)
326     {
327         return_ACPI_STATUS (AE_AML_NO_OPERAND);
328     }
329 
330     if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
331     {
332         /*
333          * If the BufferField arguments have not been previously evaluated,
334          * evaluate them now and save the results.
335          */
336         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
337         {
338             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
339             if (ACPI_FAILURE (Status))
340             {
341                 return_ACPI_STATUS (Status);
342             }
343         }
344     }
345     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
346         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
347     {
348         /* General Purpose I/O */
349 
350         Status = AcpiExWriteGpio (SourceDesc, ObjDesc, ResultDesc);
351         return_ACPI_STATUS (Status);
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         /* SMBus, GSBus, IPMI serial */
359 
360         Status = AcpiExWriteSerialBus (SourceDesc, ObjDesc, ResultDesc);
361         return_ACPI_STATUS (Status);
362     }
363     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
364              (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_COMM))
365     {
366         /*
367          * According to the spec a write to the COMD field will invoke the
368          * region handler. Otherwise, write to the PccInternal buffer. This
369          * implementation will use the offsets specified rather than the name
370          * of the field. This is considered safer because some firmware tools
371          * are known to obfiscate named objects.
372          */
373         DataLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
374             ObjDesc->Field.BitLength);
375         memcpy (ObjDesc->Field.RegionObj->Field.InternalPccBuffer +
376             ObjDesc->Field.BaseByteOffset,
377             SourceDesc->Buffer.Pointer, DataLength);
378 
379         if ((ObjDesc->Field.RegionObj->Region.Address == PCC_MASTER_SUBSPACE &&
380            MASTER_SUBSPACE_COMMAND (ObjDesc->Field.BaseByteOffset)) ||
381            GENERIC_SUBSPACE_COMMAND (ObjDesc->Field.BaseByteOffset))
382         {
383             /* Perform the write */
384 
385             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
386                 "PCC COMD field has been written. Invoking PCC handler now.\n"));
387 
388             Status = AcpiExAccessRegion (
389                 ObjDesc, 0, (UINT64 *) ObjDesc->Field.RegionObj->Field.InternalPccBuffer,
390                 ACPI_WRITE);
391             return_ACPI_STATUS (Status);
392         }
393         return (AE_OK);
394     }
395 
396 
397     /* Get a pointer to the data to be written */
398 
399     switch (SourceDesc->Common.Type)
400     {
401     case ACPI_TYPE_INTEGER:
402 
403         Buffer = &SourceDesc->Integer.Value;
404         BufferLength = sizeof (SourceDesc->Integer.Value);
405         break;
406 
407     case ACPI_TYPE_BUFFER:
408 
409         Buffer = SourceDesc->Buffer.Pointer;
410         BufferLength = SourceDesc->Buffer.Length;
411         break;
412 
413     case ACPI_TYPE_STRING:
414 
415         Buffer = SourceDesc->String.Pointer;
416         BufferLength = SourceDesc->String.Length;
417         break;
418 
419     default:
420         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
421     }
422 
423     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
424         "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
425         SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
426         SourceDesc->Common.Type, Buffer, BufferLength));
427 
428     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
429         "FieldWrite [TO]:   Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
430         ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
431         ObjDesc->Common.Type,
432         ObjDesc->CommonField.BitLength,
433         ObjDesc->CommonField.StartFieldBitOffset,
434         ObjDesc->CommonField.BaseByteOffset));
435 
436     /* Lock entire transaction if requested */
437 
438     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
439 
440     /* Write to the field */
441 
442     Status = AcpiExInsertIntoField (ObjDesc, Buffer, BufferLength);
443     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
444     return_ACPI_STATUS (Status);
445 }
446