Sync ACPICA with Intel's version 20180927:
[dragonfly.git] / sys / contrib / dev / acpica / source / components / executer / exfield.c
blobccd6bee0b9af008dc3a87158e75c00697acf2e7b
1 /******************************************************************************
3 * Module Name: exfield - AML execution - FieldUnit read/write
5 *****************************************************************************/
7 /******************************************************************************
9 * 1. Copyright Notice
11 * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
12 * All rights reserved.
14 * 2. License
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.
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
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;
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
40 * 3. Conditions
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.
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.
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.
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
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.
80 * 4. Disclaimer and Export Compliance
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.
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.
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.
114 *****************************************************************************
116 * Alternatively, you may choose to be licensed under the terms of the
117 * following license:
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.
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.
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.
150 *****************************************************************************/
152 #include "acpi.h"
153 #include "accommon.h"
154 #include "acdispat.h"
155 #include "acinterp.h"
156 #include "amlcode.h"
159 #define _COMPONENT ACPI_EXECUTER
160 ACPI_MODULE_NAME ("exfield")
164 * This table maps the various Attrib protocols to the byte transfer
165 * length. Used for the generic serial bus.
167 #define ACPI_INVALID_PROTOCOL_ID 0x80
168 #define ACPI_MAX_PROTOCOL_ID 0x0F
170 const UINT8 AcpiProtocolLengths[] =
172 ACPI_INVALID_PROTOCOL_ID, /* 0 - reserved */
173 ACPI_INVALID_PROTOCOL_ID, /* 1 - reserved */
174 0x00, /* 2 - ATTRIB_QUICK */
175 ACPI_INVALID_PROTOCOL_ID, /* 3 - reserved */
176 0x01, /* 4 - ATTRIB_SEND_RECEIVE */
177 ACPI_INVALID_PROTOCOL_ID, /* 5 - reserved */
178 0x01, /* 6 - ATTRIB_BYTE */
179 ACPI_INVALID_PROTOCOL_ID, /* 7 - reserved */
180 0x02, /* 8 - ATTRIB_WORD */
181 ACPI_INVALID_PROTOCOL_ID, /* 9 - reserved */
182 0xFF, /* A - ATTRIB_BLOCK */
183 0xFF, /* B - ATTRIB_BYTES */
184 0x02, /* C - ATTRIB_PROCESS_CALL */
185 0xFF, /* D - ATTRIB_BLOCK_PROCESS_CALL */
186 0xFF, /* E - ATTRIB_RAW_BYTES */
187 0xFF /* F - ATTRIB_RAW_PROCESS_BYTES */
191 /*******************************************************************************
193 * FUNCTION: AcpiExGetProtocolBufferLength
195 * PARAMETERS: ProtocolId - The type of the protocol indicated by region
196 * field access attributes
197 * ReturnLength - Where the protocol byte transfer length is
198 * returned
200 * RETURN: Status and decoded byte transfer length
202 * DESCRIPTION: This routine returns the length of the GenericSerialBus
203 * protocol bytes
205 ******************************************************************************/
207 ACPI_STATUS
208 AcpiExGetProtocolBufferLength (
209 UINT32 ProtocolId,
210 UINT32 *ReturnLength)
213 if ((ProtocolId > ACPI_MAX_PROTOCOL_ID) ||
214 (AcpiProtocolLengths[ProtocolId] == ACPI_INVALID_PROTOCOL_ID))
216 ACPI_ERROR ((AE_INFO,
217 "Invalid Field/AccessAs protocol ID: 0x%4.4X", ProtocolId));
219 return (AE_AML_PROTOCOL);
222 *ReturnLength = AcpiProtocolLengths[ProtocolId];
223 return (AE_OK);
227 /*******************************************************************************
229 * FUNCTION: AcpiExReadDataFromField
231 * PARAMETERS: WalkState - Current execution state
232 * ObjDesc - The named field
233 * RetBufferDesc - Where the return data object is stored
235 * RETURN: Status
237 * DESCRIPTION: Read from a named field. Returns either an Integer or a
238 * Buffer, depending on the size of the field.
240 ******************************************************************************/
242 ACPI_STATUS
243 AcpiExReadDataFromField (
244 ACPI_WALK_STATE *WalkState,
245 ACPI_OPERAND_OBJECT *ObjDesc,
246 ACPI_OPERAND_OBJECT **RetBufferDesc)
248 ACPI_STATUS Status;
249 ACPI_OPERAND_OBJECT *BufferDesc;
250 void *Buffer;
251 UINT32 BufferLength;
254 ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
257 /* Parameter validation */
259 if (!ObjDesc)
261 return_ACPI_STATUS (AE_AML_NO_OPERAND);
263 if (!RetBufferDesc)
265 return_ACPI_STATUS (AE_BAD_PARAMETER);
268 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
271 * If the BufferField arguments have not been previously evaluated,
272 * evaluate them now and save the results.
274 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
276 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
277 if (ACPI_FAILURE (Status))
279 return_ACPI_STATUS (Status);
283 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
284 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
285 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
286 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
288 /* SMBus, GSBus, IPMI serial */
290 Status = AcpiExReadSerialBus (ObjDesc, RetBufferDesc);
291 return_ACPI_STATUS (Status);
295 * Allocate a buffer for the contents of the field.
297 * If the field is larger than the current integer width, create
298 * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
299 * the use of arithmetic operators on the returned value if the
300 * field size is equal or smaller than an Integer.
302 * Note: Field.length is in bits.
304 BufferLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
305 ObjDesc->Field.BitLength);
307 if (BufferLength > AcpiGbl_IntegerByteWidth)
309 /* Field is too large for an Integer, create a Buffer instead */
311 BufferDesc = AcpiUtCreateBufferObject (BufferLength);
312 if (!BufferDesc)
314 return_ACPI_STATUS (AE_NO_MEMORY);
316 Buffer = BufferDesc->Buffer.Pointer;
318 else
320 /* Field will fit within an Integer (normal case) */
322 BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
323 if (!BufferDesc)
325 return_ACPI_STATUS (AE_NO_MEMORY);
328 BufferLength = AcpiGbl_IntegerByteWidth;
329 Buffer = &BufferDesc->Integer.Value;
332 if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
333 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
335 /* General Purpose I/O */
337 Status = AcpiExReadGpio (ObjDesc, Buffer);
338 goto Exit;
341 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
342 "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
343 ObjDesc, ObjDesc->Common.Type, Buffer, BufferLength));
344 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
345 "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
346 ObjDesc->CommonField.BitLength,
347 ObjDesc->CommonField.StartFieldBitOffset,
348 ObjDesc->CommonField.BaseByteOffset));
350 /* Lock entire transaction if requested */
352 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
354 /* Read from the field */
356 Status = AcpiExExtractFromField (ObjDesc, Buffer, BufferLength);
357 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
360 Exit:
361 if (ACPI_FAILURE (Status))
363 AcpiUtRemoveReference (BufferDesc);
365 else
367 *RetBufferDesc = BufferDesc;
370 return_ACPI_STATUS (Status);
374 /*******************************************************************************
376 * FUNCTION: AcpiExWriteDataToField
378 * PARAMETERS: SourceDesc - Contains data to write
379 * ObjDesc - The named field
380 * ResultDesc - Where the return value is returned, if any
382 * RETURN: Status
384 * DESCRIPTION: Write to a named field
386 ******************************************************************************/
388 ACPI_STATUS
389 AcpiExWriteDataToField (
390 ACPI_OPERAND_OBJECT *SourceDesc,
391 ACPI_OPERAND_OBJECT *ObjDesc,
392 ACPI_OPERAND_OBJECT **ResultDesc)
394 ACPI_STATUS Status;
395 UINT32 BufferLength;
396 void *Buffer;
399 ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
402 /* Parameter validation */
404 if (!SourceDesc || !ObjDesc)
406 return_ACPI_STATUS (AE_AML_NO_OPERAND);
409 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
412 * If the BufferField arguments have not been previously evaluated,
413 * evaluate them now and save the results.
415 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
417 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
418 if (ACPI_FAILURE (Status))
420 return_ACPI_STATUS (Status);
424 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
425 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
427 /* General Purpose I/O */
429 Status = AcpiExWriteGpio (SourceDesc, ObjDesc, ResultDesc);
430 return_ACPI_STATUS (Status);
432 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
433 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
434 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
435 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
437 /* SMBus, GSBus, IPMI serial */
439 Status = AcpiExWriteSerialBus (SourceDesc, ObjDesc, ResultDesc);
440 return_ACPI_STATUS (Status);
443 /* Get a pointer to the data to be written */
445 switch (SourceDesc->Common.Type)
447 case ACPI_TYPE_INTEGER:
449 Buffer = &SourceDesc->Integer.Value;
450 BufferLength = sizeof (SourceDesc->Integer.Value);
451 break;
453 case ACPI_TYPE_BUFFER:
455 Buffer = SourceDesc->Buffer.Pointer;
456 BufferLength = SourceDesc->Buffer.Length;
457 break;
459 case ACPI_TYPE_STRING:
461 Buffer = SourceDesc->String.Pointer;
462 BufferLength = SourceDesc->String.Length;
463 break;
465 default:
466 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
469 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
470 "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
471 SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
472 SourceDesc->Common.Type, Buffer, BufferLength));
474 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
475 "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
476 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
477 ObjDesc->Common.Type,
478 ObjDesc->CommonField.BitLength,
479 ObjDesc->CommonField.StartFieldBitOffset,
480 ObjDesc->CommonField.BaseByteOffset));
482 /* Lock entire transaction if requested */
484 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
486 /* Write to the field */
488 Status = AcpiExInsertIntoField (ObjDesc, Buffer, BufferLength);
489 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
490 return_ACPI_STATUS (Status);