1 /******************************************************************************
3 * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2015, Intel Corp.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
44 #include <contrib/dev/acpica/include/acpi.h>
45 #include <contrib/dev/acpica/include/accommon.h>
46 #include <contrib/dev/acpica/include/acdispat.h>
47 #include <contrib/dev/acpica/include/acinterp.h>
48 #include <contrib/dev/acpica/include/amlcode.h>
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exfield")
54 /* Local prototypes */
57 AcpiExGetSerialAccessLength (
62 /*******************************************************************************
64 * FUNCTION: AcpiExGetSerialAccessLength
66 * PARAMETERS: AccessorType - The type of the protocol indicated by region
67 * field access attributes
68 * AccessLength - The access length of the region field
70 * RETURN: Decoded access length
72 * DESCRIPTION: This routine returns the length of the GenericSerialBus
75 ******************************************************************************/
78 AcpiExGetSerialAccessLength (
87 case AML_FIELD_ATTRIB_QUICK:
92 case AML_FIELD_ATTRIB_SEND_RCV:
93 case AML_FIELD_ATTRIB_BYTE:
98 case AML_FIELD_ATTRIB_WORD:
99 case AML_FIELD_ATTRIB_WORD_CALL:
104 case AML_FIELD_ATTRIB_MULTIBYTE:
105 case AML_FIELD_ATTRIB_RAW_BYTES:
106 case AML_FIELD_ATTRIB_RAW_PROCESS:
108 Length = AccessLength;
111 case AML_FIELD_ATTRIB_BLOCK:
112 case AML_FIELD_ATTRIB_BLOCK_CALL:
115 Length = ACPI_GSBUS_BUFFER_SIZE - 2;
123 /*******************************************************************************
125 * FUNCTION: AcpiExReadDataFromField
127 * PARAMETERS: WalkState - Current execution state
128 * ObjDesc - The named field
129 * RetBufferDesc - Where the return data object is stored
133 * DESCRIPTION: Read from a named field. Returns either an Integer or a
134 * Buffer, depending on the size of the field.
136 ******************************************************************************/
139 AcpiExReadDataFromField (
140 ACPI_WALK_STATE *WalkState,
141 ACPI_OPERAND_OBJECT *ObjDesc,
142 ACPI_OPERAND_OBJECT **RetBufferDesc)
145 ACPI_OPERAND_OBJECT *BufferDesc;
152 ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
155 /* Parameter validation */
159 return_ACPI_STATUS (AE_AML_NO_OPERAND);
163 return_ACPI_STATUS (AE_BAD_PARAMETER);
166 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
169 * If the BufferField arguments have not been previously evaluated,
170 * evaluate them now and save the results.
172 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
174 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
175 if (ACPI_FAILURE (Status))
177 return_ACPI_STATUS (Status);
181 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
182 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
183 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
184 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
187 * This is an SMBus, GSBus or IPMI read. We must create a buffer to hold
188 * the data and then directly access the region handler.
190 * Note: SMBus and GSBus protocol value is passed in upper 16-bits of Function
192 if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
194 Length = ACPI_SMBUS_BUFFER_SIZE;
195 Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);
197 else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
199 AccessorType = ObjDesc->Field.Attribute;
200 Length = AcpiExGetSerialAccessLength (AccessorType,
201 ObjDesc->Field.AccessLength);
204 * Add additional 2 bytes for the GenericSerialBus data buffer:
206 * Status; (Byte 0 of the data buffer)
207 * Length; (Byte 1 of the data buffer)
208 * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
211 Function = ACPI_READ | (AccessorType << 16);
215 Length = ACPI_IPMI_BUFFER_SIZE;
216 Function = ACPI_READ;
219 BufferDesc = AcpiUtCreateBufferObject (Length);
222 return_ACPI_STATUS (AE_NO_MEMORY);
225 /* Lock entire transaction if requested */
227 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
229 /* Call the region handler for the read */
231 Status = AcpiExAccessRegion (ObjDesc, 0,
232 ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer),
234 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
239 * Allocate a buffer for the contents of the field.
241 * If the field is larger than the current integer width, create
242 * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
243 * the use of arithmetic operators on the returned value if the
244 * field size is equal or smaller than an Integer.
246 * Note: Field.length is in bits.
248 Length = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->Field.BitLength);
249 if (Length > AcpiGbl_IntegerByteWidth)
251 /* Field is too large for an Integer, create a Buffer instead */
253 BufferDesc = AcpiUtCreateBufferObject (Length);
256 return_ACPI_STATUS (AE_NO_MEMORY);
258 Buffer = BufferDesc->Buffer.Pointer;
262 /* Field will fit within an Integer (normal case) */
264 BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
267 return_ACPI_STATUS (AE_NO_MEMORY);
270 Length = AcpiGbl_IntegerByteWidth;
271 Buffer = &BufferDesc->Integer.Value;
274 if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
275 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
278 * For GPIO (GeneralPurposeIo), the Address will be the bit offset
279 * from the previous Connection() operator, making it effectively a
280 * pin number index. The BitLength is the length of the field, which
281 * is thus the number of pins.
283 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
284 "GPIO FieldRead [FROM]: Pin %u Bits %u\n",
285 ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));
287 /* Lock entire transaction if requested */
289 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
291 /* Perform the write */
293 Status = AcpiExAccessRegion (ObjDesc, 0,
294 (UINT64 *) Buffer, ACPI_READ);
295 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
296 if (ACPI_FAILURE (Status))
298 AcpiUtRemoveReference (BufferDesc);
302 *RetBufferDesc = BufferDesc;
304 return_ACPI_STATUS (Status);
307 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
308 "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
309 ObjDesc, ObjDesc->Common.Type, Buffer, (UINT32) Length));
310 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
311 "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
312 ObjDesc->CommonField.BitLength,
313 ObjDesc->CommonField.StartFieldBitOffset,
314 ObjDesc->CommonField.BaseByteOffset));
316 /* Lock entire transaction if requested */
318 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
320 /* Read from the field */
322 Status = AcpiExExtractFromField (ObjDesc, Buffer, (UINT32) Length);
323 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
327 if (ACPI_FAILURE (Status))
329 AcpiUtRemoveReference (BufferDesc);
333 *RetBufferDesc = BufferDesc;
336 return_ACPI_STATUS (Status);
340 /*******************************************************************************
342 * FUNCTION: AcpiExWriteDataToField
344 * PARAMETERS: SourceDesc - Contains data to write
345 * ObjDesc - The named field
346 * ResultDesc - Where the return value is returned, if any
350 * DESCRIPTION: Write to a named field
352 ******************************************************************************/
355 AcpiExWriteDataToField (
356 ACPI_OPERAND_OBJECT *SourceDesc,
357 ACPI_OPERAND_OBJECT *ObjDesc,
358 ACPI_OPERAND_OBJECT **ResultDesc)
363 ACPI_OPERAND_OBJECT *BufferDesc;
368 ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
371 /* Parameter validation */
373 if (!SourceDesc || !ObjDesc)
375 return_ACPI_STATUS (AE_AML_NO_OPERAND);
378 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
381 * If the BufferField arguments have not been previously evaluated,
382 * evaluate them now and save the results.
384 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
386 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
387 if (ACPI_FAILURE (Status))
389 return_ACPI_STATUS (Status);
393 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
394 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
395 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
396 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
399 * This is an SMBus, GSBus or IPMI write. We will bypass the entire field
400 * mechanism and handoff the buffer directly to the handler. For
401 * these address spaces, the buffer is bi-directional; on a write,
402 * return data is returned in the same buffer.
404 * Source must be a buffer of sufficient size:
405 * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
407 * Note: SMBus and GSBus protocol type is passed in upper 16-bits of Function
409 if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)
411 ACPI_ERROR ((AE_INFO,
412 "SMBus/IPMI/GenericSerialBus write requires Buffer, found type %s",
413 AcpiUtGetObjectTypeName (SourceDesc)));
415 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
418 if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
420 Length = ACPI_SMBUS_BUFFER_SIZE;
421 Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
423 else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
425 AccessorType = ObjDesc->Field.Attribute;
426 Length = AcpiExGetSerialAccessLength (AccessorType,
427 ObjDesc->Field.AccessLength);
430 * Add additional 2 bytes for the GenericSerialBus data buffer:
432 * Status; (Byte 0 of the data buffer)
433 * Length; (Byte 1 of the data buffer)
434 * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
437 Function = ACPI_WRITE | (AccessorType << 16);
441 Length = ACPI_IPMI_BUFFER_SIZE;
442 Function = ACPI_WRITE;
445 if (SourceDesc->Buffer.Length < Length)
447 ACPI_ERROR ((AE_INFO,
448 "SMBus/IPMI/GenericSerialBus write requires Buffer of length %u, found length %u",
449 Length, SourceDesc->Buffer.Length));
451 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
454 /* Create the bi-directional buffer */
456 BufferDesc = AcpiUtCreateBufferObject (Length);
459 return_ACPI_STATUS (AE_NO_MEMORY);
462 Buffer = BufferDesc->Buffer.Pointer;
463 ACPI_MEMCPY (Buffer, SourceDesc->Buffer.Pointer, Length);
465 /* Lock entire transaction if requested */
467 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
470 * Perform the write (returns status and perhaps data in the
473 Status = AcpiExAccessRegion (ObjDesc, 0,
474 (UINT64 *) Buffer, Function);
475 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
477 *ResultDesc = BufferDesc;
478 return_ACPI_STATUS (Status);
480 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
481 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
484 * For GPIO (GeneralPurposeIo), we will bypass the entire field
485 * mechanism and handoff the bit address and bit width directly to
486 * the handler. The Address will be the bit offset
487 * from the previous Connection() operator, making it effectively a
488 * pin number index. The BitLength is the length of the field, which
489 * is thus the number of pins.
491 if (SourceDesc->Common.Type != ACPI_TYPE_INTEGER)
493 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
496 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
497 "GPIO FieldWrite [FROM]: (%s:%X), Val %.8X [TO]: Pin %u Bits %u\n",
498 AcpiUtGetTypeName (SourceDesc->Common.Type),
499 SourceDesc->Common.Type, (UINT32) SourceDesc->Integer.Value,
500 ObjDesc->Field.PinNumberIndex, ObjDesc->Field.BitLength));
502 Buffer = &SourceDesc->Integer.Value;
504 /* Lock entire transaction if requested */
506 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
508 /* Perform the write */
510 Status = AcpiExAccessRegion (ObjDesc, 0,
511 (UINT64 *) Buffer, ACPI_WRITE);
512 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
513 return_ACPI_STATUS (Status);
516 /* Get a pointer to the data to be written */
518 switch (SourceDesc->Common.Type)
520 case ACPI_TYPE_INTEGER:
522 Buffer = &SourceDesc->Integer.Value;
523 Length = sizeof (SourceDesc->Integer.Value);
526 case ACPI_TYPE_BUFFER:
528 Buffer = SourceDesc->Buffer.Pointer;
529 Length = SourceDesc->Buffer.Length;
532 case ACPI_TYPE_STRING:
534 Buffer = SourceDesc->String.Pointer;
535 Length = SourceDesc->String.Length;
540 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
543 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
544 "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
545 SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
546 SourceDesc->Common.Type, Buffer, Length));
548 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
549 "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
550 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
551 ObjDesc->Common.Type,
552 ObjDesc->CommonField.BitLength,
553 ObjDesc->CommonField.StartFieldBitOffset,
554 ObjDesc->CommonField.BaseByteOffset));
556 /* Lock entire transaction if requested */
558 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
560 /* Write to the field */
562 Status = AcpiExInsertIntoField (ObjDesc, Buffer, Length);
563 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
565 return_ACPI_STATUS (Status);