1 /******************************************************************************
2  *
3  * Module Name: exserial - FieldUnit support for serial address spaces
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2021, 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  * Alternatively, you may choose to be licensed under the terms of the
117  * following license:
118  *
119  * Redistribution and use in source and binary forms, with or without
120  * modification, are permitted provided that the following conditions
121  * are met:
122  * 1. Redistributions of source code must retain the above copyright
123  *    notice, this list of conditions, and the following disclaimer,
124  *    without modification.
125  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
126  *    substantially similar to the "NO WARRANTY" disclaimer below
127  *    ("Disclaimer") and any redistribution must be conditioned upon
128  *    including a substantially similar Disclaimer requirement for further
129  *    binary redistribution.
130  * 3. Neither the names of the above-listed copyright holders nor the names
131  *    of any contributors may be used to endorse or promote products derived
132  *    from this software without specific prior written permission.
133  *
134  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
135  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
136  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
137  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
138  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
139  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
140  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
141  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
142  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
143  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
144  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
145  *
146  * Alternatively, you may choose to be licensed under the terms of the
147  * GNU General Public License ("GPL") version 2 as published by the Free
148  * Software Foundation.
149  *
150  *****************************************************************************/
151 
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acdispat.h"
155 #include "acinterp.h"
156 #include "amlcode.h"
157 
158 
159 #define _COMPONENT          ACPI_EXECUTER
160         ACPI_MODULE_NAME    ("exserial")
161 
162 
163 /*******************************************************************************
164  *
165  * FUNCTION:    AcpiExReadGpio
166  *
167  * PARAMETERS:  ObjDesc             - The named field to read
168  *              Buffer              - Where the return data is returned
169  *
170  * RETURN:      Status
171  *
172  * DESCRIPTION: Read from a named field that references a Generic Serial Bus
173  *              field
174  *
175  ******************************************************************************/
176 
177 ACPI_STATUS
178 AcpiExReadGpio (
179     ACPI_OPERAND_OBJECT     *ObjDesc,
180     void                    *Buffer)
181 {
182     ACPI_STATUS             Status;
183 
184 
185     ACPI_FUNCTION_TRACE_PTR (ExReadGpio, ObjDesc);
186 
187 
188     /*
189      * For GPIO (GeneralPurposeIo), the Address will be the bit offset
190      * from the previous Connection() operator, making it effectively a
191      * pin number index. The BitLength is the length of the field, which
192      * is thus the number of pins.
193      */
194     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
195         "GPIO FieldRead [FROM]:  Pin %u Bits %u\n",
196         ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));
197 
198     /* Lock entire transaction if requested */
199 
200     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
201 
202     /* Perform the read */
203 
204     Status = AcpiExAccessRegion (
205         ObjDesc, 0, (UINT64 *) Buffer, ACPI_READ);
206 
207     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
208     return_ACPI_STATUS (Status);
209 }
210 
211 
212 /*******************************************************************************
213  *
214  * FUNCTION:    AcpiExWriteGpio
215  *
216  * PARAMETERS:  SourceDesc          - Contains data to write. Expect to be
217  *                                    an Integer object.
218  *              ObjDesc             - The named field
219  *              ResultDesc          - Where the return value is returned, if any
220  *
221  * RETURN:      Status
222  *
223  * DESCRIPTION: Write to a named field that references a General Purpose I/O
224  *              field.
225  *
226  ******************************************************************************/
227 
228 ACPI_STATUS
229 AcpiExWriteGpio (
230     ACPI_OPERAND_OBJECT     *SourceDesc,
231     ACPI_OPERAND_OBJECT     *ObjDesc,
232     ACPI_OPERAND_OBJECT     **ReturnBuffer)
233 {
234     ACPI_STATUS             Status;
235     void                    *Buffer;
236 
237 
238     ACPI_FUNCTION_TRACE_PTR (ExWriteGpio, ObjDesc);
239 
240 
241     /*
242      * For GPIO (GeneralPurposeIo), we will bypass the entire field
243      * mechanism and handoff the bit address and bit width directly to
244      * the handler. The Address will be the bit offset
245      * from the previous Connection() operator, making it effectively a
246      * pin number index. The BitLength is the length of the field, which
247      * is thus the number of pins.
248      */
249     if (SourceDesc->Common.Type != ACPI_TYPE_INTEGER)
250     {
251         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
252     }
253 
254     ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
255         "GPIO FieldWrite [FROM]: (%s:%X), Value %.8X  [TO]: Pin %u Bits %u\n",
256         AcpiUtGetTypeName (SourceDesc->Common.Type),
257         SourceDesc->Common.Type, (UINT32) SourceDesc->Integer.Value,
258         ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));
259 
260     Buffer = &SourceDesc->Integer.Value;
261 
262     /* Lock entire transaction if requested */
263 
264     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
265 
266     /* Perform the write */
267 
268     Status = AcpiExAccessRegion (
269         ObjDesc, 0, (UINT64 *) Buffer, ACPI_WRITE);
270     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
271     return_ACPI_STATUS (Status);
272 }
273 
274 
275 /*******************************************************************************
276  *
277  * FUNCTION:    AcpiExReadSerialBus
278  *
279  * PARAMETERS:  ObjDesc             - The named field to read
280  *              ReturnBuffer        - Where the return value is returned, if any
281  *
282  * RETURN:      Status
283  *
284  * DESCRIPTION: Read from a named field that references a serial bus
285  *              (SMBus, IPMI, or GSBus).
286  *
287  ******************************************************************************/
288 
289 ACPI_STATUS
290 AcpiExReadSerialBus (
291     ACPI_OPERAND_OBJECT     *ObjDesc,
292     ACPI_OPERAND_OBJECT     **ReturnBuffer)
293 {
294     ACPI_STATUS             Status;
295     UINT32                  BufferLength;
296     ACPI_OPERAND_OBJECT     *BufferDesc;
297     UINT32                  Function;
298     UINT16                  AccessorType;
299 
300 
301     ACPI_FUNCTION_TRACE_PTR (ExReadSerialBus, ObjDesc);
302 
303 
304     /*
305      * This is an SMBus, GSBus or IPMI read. We must create a buffer to
306      * hold the data and then directly access the region handler.
307      *
308      * Note: SMBus and GSBus protocol value is passed in upper 16-bits
309      * of Function
310      *
311      * Common buffer format:
312      *     Status;    (Byte 0 of the data buffer)
313      *     Length;    (Byte 1 of the data buffer)
314      *     Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
315      */
316     switch (ObjDesc->Field.RegionObj->Region.SpaceId)
317     {
318     case ACPI_ADR_SPACE_SMBUS:
319 
320         BufferLength = ACPI_SMBUS_BUFFER_SIZE;
321         Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);
322         break;
323 
324     case ACPI_ADR_SPACE_IPMI:
325 
326         BufferLength = ACPI_IPMI_BUFFER_SIZE;
327         Function = ACPI_READ;
328         break;
329 
330     case ACPI_ADR_SPACE_GSBUS:
331 
332         AccessorType = ObjDesc->Field.Attribute;
333         if (AccessorType == AML_FIELD_ATTRIB_RAW_PROCESS_BYTES)
334         {
335             ACPI_ERROR ((AE_INFO,
336                 "Invalid direct read using bidirectional write-then-read protocol"));
337 
338             return_ACPI_STATUS (AE_AML_PROTOCOL);
339         }
340 
341         Status = AcpiExGetProtocolBufferLength (AccessorType, &BufferLength);
342         if (ACPI_FAILURE (Status))
343         {
344             ACPI_ERROR ((AE_INFO,
345                 "Invalid protocol ID for GSBus: 0x%4.4X", AccessorType));
346 
347             return_ACPI_STATUS (Status);
348         }
349 
350         /* Add header length to get the full size of the buffer */
351 
352         BufferLength += ACPI_SERIAL_HEADER_SIZE;
353         Function = ACPI_READ | (AccessorType << 16);
354         break;
355 
356     default:
357         return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
358     }
359 
360     /* Create the local transfer buffer that is returned to the caller */
361 
362     BufferDesc = AcpiUtCreateBufferObject (BufferLength);
363     if (!BufferDesc)
364     {
365         return_ACPI_STATUS (AE_NO_MEMORY);
366     }
367 
368     /* Lock entire transaction if requested */
369 
370     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
371 
372     /* Call the region handler for the write-then-read */
373 
374     Status = AcpiExAccessRegion (ObjDesc, 0,
375         ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer), Function);
376     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
377 
378     *ReturnBuffer = BufferDesc;
379     return_ACPI_STATUS (Status);
380 }
381 
382 
383 /*******************************************************************************
384  *
385  * FUNCTION:    AcpiExWriteSerialBus
386  *
387  * PARAMETERS:  SourceDesc          - Contains data to write
388  *              ObjDesc             - The named field
389  *              ReturnBuffer        - Where the return value is returned, if any
390  *
391  * RETURN:      Status
392  *
393  * DESCRIPTION: Write to a named field that references a serial bus
394  *              (SMBus, IPMI, GSBus).
395  *
396  ******************************************************************************/
397 
398 ACPI_STATUS
399 AcpiExWriteSerialBus (
400     ACPI_OPERAND_OBJECT     *SourceDesc,
401     ACPI_OPERAND_OBJECT     *ObjDesc,
402     ACPI_OPERAND_OBJECT     **ReturnBuffer)
403 {
404     ACPI_STATUS             Status;
405     UINT32                  BufferLength;
406     UINT32                  DataLength;
407     void                    *Buffer;
408     ACPI_OPERAND_OBJECT     *BufferDesc;
409     UINT32                  Function;
410     UINT16                  AccessorType;
411 
412 
413     ACPI_FUNCTION_TRACE_PTR (ExWriteSerialBus, ObjDesc);
414 
415 
416     /*
417      * This is an SMBus, GSBus or IPMI write. We will bypass the entire
418      * field mechanism and handoff the buffer directly to the handler.
419      * For these address spaces, the buffer is bidirectional; on a
420      * write, return data is returned in the same buffer.
421      *
422      * Source must be a buffer of sufficient size, these are fixed size:
423      * ACPI_SMBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
424      *
425      * Note: SMBus and GSBus protocol type is passed in upper 16-bits
426      * of Function
427      *
428      * Common buffer format:
429      *     Status;    (Byte 0 of the data buffer)
430      *     Length;    (Byte 1 of the data buffer)
431      *     Data[x-1]: (Bytes 2-x of the arbitrary length data buffer)
432      */
433     if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)
434     {
435         ACPI_ERROR ((AE_INFO,
436             "SMBus/IPMI/GenericSerialBus write requires "
437             "Buffer, found type %s",
438             AcpiUtGetObjectTypeName (SourceDesc)));
439 
440         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
441     }
442 
443     switch (ObjDesc->Field.RegionObj->Region.SpaceId)
444     {
445     case ACPI_ADR_SPACE_SMBUS:
446 
447         BufferLength = ACPI_SMBUS_BUFFER_SIZE;
448         Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
449         break;
450 
451     case ACPI_ADR_SPACE_IPMI:
452 
453         BufferLength = ACPI_IPMI_BUFFER_SIZE;
454         Function = ACPI_WRITE;
455         break;
456 
457     case ACPI_ADR_SPACE_GSBUS:
458 
459         AccessorType = ObjDesc->Field.Attribute;
460         Status = AcpiExGetProtocolBufferLength (AccessorType, &BufferLength);
461         if (ACPI_FAILURE (Status))
462         {
463             ACPI_ERROR ((AE_INFO,
464                 "Invalid protocol ID for GSBus: 0x%4.4X", AccessorType));
465 
466             return_ACPI_STATUS (Status);
467         }
468 
469         /* Add header length to get the full size of the buffer */
470 
471         BufferLength += ACPI_SERIAL_HEADER_SIZE;
472         Function = ACPI_WRITE | (AccessorType << 16);
473         break;
474 
475     default:
476         return_ACPI_STATUS (AE_AML_INVALID_SPACE_ID);
477     }
478 
479     /* Create the transfer/bidirectional/return buffer */
480 
481     BufferDesc = AcpiUtCreateBufferObject (BufferLength);
482     if (!BufferDesc)
483     {
484         return_ACPI_STATUS (AE_NO_MEMORY);
485     }
486 
487     /* Copy the input buffer data to the transfer buffer */
488 
489     Buffer = BufferDesc->Buffer.Pointer;
490     DataLength = (BufferLength < SourceDesc->Buffer.Length ?
491         BufferLength : SourceDesc->Buffer.Length);
492     memcpy (Buffer, SourceDesc->Buffer.Pointer, DataLength);
493 
494     /* Lock entire transaction if requested */
495 
496     AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
497 
498     /*
499      * Perform the write (returns status and perhaps data in the
500      * same buffer)
501      */
502     Status = AcpiExAccessRegion (
503         ObjDesc, 0, (UINT64 *) Buffer, Function);
504     AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
505 
506     *ReturnBuffer = BufferDesc;
507     return_ACPI_STATUS (Status);
508 }
509