1 /******************************************************************************
2  *
3  * Module Name: exfield - AML execution - FieldUnit read/write
4  *
5  *****************************************************************************/
6 
7 /*
8  * Copyright (C) 2000 - 2021, 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 MERCHANTABILITY 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 static 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 and whether if a
142  *              field is created by the CreateField() operator.
143  *
144  ******************************************************************************/
145 
146 ACPI_STATUS
147 AcpiExReadDataFromField (
148     ACPI_WALK_STATE         *WalkState,
149     ACPI_OPERAND_OBJECT     *ObjDesc,
150     ACPI_OPERAND_OBJECT     **RetBufferDesc)
151 {
152     ACPI_STATUS             Status;
153     ACPI_OPERAND_OBJECT     *BufferDesc;
154     void                    *Buffer;
155     UINT32                  BufferLength;
156 
157 
158     ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
159 
160 
161     /* Parameter validation */
162 
163     if (!ObjDesc)
164     {
165         return_ACPI_STATUS (AE_AML_NO_OPERAND);
166     }
167     if (!RetBufferDesc)
168     {
169         return_ACPI_STATUS (AE_BAD_PARAMETER);
170     }
171 
172     if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
173     {
174         /*
175          * If the BufferField arguments have not been previously evaluated,
176          * evaluate them now and save the results.
177          */
178         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
179         {
180             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
181             if (ACPI_FAILURE (Status))
182             {
183                 return_ACPI_STATUS (Status);
184             }
185         }
186     }
187     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
188         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
189          ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
190          ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI  ||
191          ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_RT))
192     {
193         /* SMBus, GSBus, IPMI serial */
194 
195         Status = AcpiExReadSerialBus (ObjDesc, RetBufferDesc);
196         return_ACPI_STATUS (Status);
197     }
198 
199     /*
200      * Allocate a buffer for the contents of the field.
201      *
202      * If the field is larger than the current integer width, create
203      * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
204      * the use of arithmetic operators on the returned value if the
205      * field size is equal or smaller than an Integer.
206      *
207      * However, all buffer fields created by CreateField operator needs to
208      * remain as a buffer to match other AML interpreter implementations.
209      *
210      * Note: Field.length is in bits.
211      */
212     BufferLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
213         ObjDesc->Field.BitLength);
214 
215     if (BufferLength > AcpiGbl_IntegerByteWidth ||
216         (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD &&
217         ObjDesc->BufferField.IsCreateField))
218     {
219         /* Field is too large for an Integer, create a Buffer instead */
220 
221         BufferDesc = AcpiUtCreateBufferObject (BufferLength);
222         if (!BufferDesc)
223         {
224             return_ACPI_STATUS (AE_NO_MEMORY);
225         }
226         Buffer = BufferDesc->Buffer.Pointer;
227     }
228     else
229     {
230         /* Field will fit within an Integer (normal case) */
231 
232         BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
233         if (!BufferDesc)
234         {
235             return_ACPI_STATUS (AE_NO_MEMORY);
236         }
237 
238         BufferLength = AcpiGbl_IntegerByteWidth;
239         Buffer = &BufferDesc->Integer.Value;
240     }
241 
242     if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
243         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
244     {
245         /* General Purpose I/O */
246 
247         Status = AcpiExReadGpio (ObjDesc, Buffer);
248         goto Exit;
249     }
250     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
251         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_COMM))
252     {
253         /*
254          * Reading from a PCC field unit does not require the handler because
255          * it only requires reading from the InternalPccBuffer.
256          */
257         ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
258             "PCC FieldRead bits %u\n", ObjDesc->Field.BitLength));
259 
260         memcpy (Buffer, ObjDesc->Field.RegionObj->Field.InternalPccBuffer +
261         ObjDesc->Field.BaseByteOffset, (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
262             ObjDesc->Field.BitLength));
263 
264         *RetBufferDesc = BufferDesc;
265         return AE_OK;
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, BufferLength));
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, BufferLength);
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                  BufferLength;
323     UINT32                  DataLength;
324     void                    *Buffer;
325 
326 
327     ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
328 
329 
330     /* Parameter validation */
331 
332     if (!SourceDesc || !ObjDesc)
333     {
334         return_ACPI_STATUS (AE_AML_NO_OPERAND);
335     }
336 
337     if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
338     {
339         /*
340          * If the BufferField arguments have not been previously evaluated,
341          * evaluate them now and save the results.
342          */
343         if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
344         {
345             Status = AcpiDsGetBufferFieldArguments (ObjDesc);
346             if (ACPI_FAILURE (Status))
347             {
348                 return_ACPI_STATUS (Status);
349             }
350         }
351     }
352     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
353         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
354     {
355         /* General Purpose I/O */
356 
357         Status = AcpiExWriteGpio (SourceDesc, ObjDesc, ResultDesc);
358         return_ACPI_STATUS (Status);
359     }
360     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
361         (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
362          ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
363          ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI  ||
364          ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_RT))
365     {
366         /* SMBus, GSBus, IPMI serial */
367 
368         Status = AcpiExWriteSerialBus (SourceDesc, ObjDesc, ResultDesc);
369         return_ACPI_STATUS (Status);
370     }
371     else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
372              (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_PLATFORM_COMM))
373     {
374         /*
375          * According to the spec a write to the COMD field will invoke the
376          * region handler. Otherwise, write to the PccInternal buffer. This
377          * implementation will use the offsets specified rather than the name
378          * of the field. This is considered safer because some firmware tools
379          * are known to obfiscate named objects.
380          */
381         DataLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
382             ObjDesc->Field.BitLength);
383         memcpy (ObjDesc->Field.RegionObj->Field.InternalPccBuffer +
384             ObjDesc->Field.BaseByteOffset,
385             SourceDesc->Buffer.Pointer, DataLength);
386 
387         if ((ObjDesc->Field.RegionObj->Region.Address == PCC_MASTER_SUBSPACE &&
388            MASTER_SUBSPACE_COMMAND (ObjDesc->Field.BaseByteOffset)) ||
389            GENERIC_SUBSPACE_COMMAND (ObjDesc->Field.BaseByteOffset))
390         {
391             /* Perform the write */
392 
393             ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
394                 "PCC COMD field has been written. Invoking PCC handler now.\n"));
395 
396             Status = AcpiExAccessRegion (
397                 ObjDesc, 0, (UINT64 *) ObjDesc->Field.RegionObj->Field.InternalPccBuffer,
398                 ACPI_WRITE);
399             return_ACPI_STATUS (Status);
400         }
401         return (AE_OK);
402     }
403 
404 
405     /* Get a pointer to the data to be written */
406 
407     switch (SourceDesc->Common.Type)
408     {
409     case ACPI_TYPE_INTEGER:
410 
411         Buffer = &SourceDesc->Integer.Value;
412         BufferLength = sizeof (SourceDesc->Integer.Value);
413         break;
414 
415     case ACPI_TYPE_BUFFER:
416 
417         Buffer = SourceDesc->Buffer.Pointer;
418         BufferLength = SourceDesc->Buffer.Length;
419         break;
420 
421     case ACPI_TYPE_STRING:
422 
423         Buffer = SourceDesc->String.Pointer;
424         BufferLength = SourceDesc->String.Length;
425         break;
426 
427     default:
428         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
429     }
430 
431     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
432         "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
433         SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
434         SourceDesc->Common.Type, Buffer, BufferLength));
435 
436     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
437         "FieldWrite [TO]:   Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
438         ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
439         ObjDesc->Common.Type,
440         ObjDesc->CommonField.BitLength,
441         ObjDesc->CommonField.StartFieldBitOffset,
442         ObjDesc->CommonField.BaseByteOffset));
443 
444     /* Lock entire transaction if requested */
445 
446     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
447 
448     /* Write to the field */
449 
450     Status = AcpiExInsertIntoField (ObjDesc, Buffer, BufferLength);
451     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
452     return_ACPI_STATUS (Status);
453 }
454