1 /******************************************************************************
3 * Module Name: exfield - ACPI AML (p-code) execution - field manipulation
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2014, 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.
53 #define _COMPONENT ACPI_EXECUTER
54 ACPI_MODULE_NAME ("exfield")
56 /* Local prototypes */
59 AcpiExGetSerialAccessLength (
64 /*******************************************************************************
66 * FUNCTION: AcpiExGetSerialAccessLength
68 * PARAMETERS: AccessorType - The type of the protocol indicated by region
69 * field access attributes
70 * AccessLength - The access length of the region field
72 * RETURN: Decoded access length
74 * DESCRIPTION: This routine returns the length of the GenericSerialBus
77 ******************************************************************************/
80 AcpiExGetSerialAccessLength (
89 case AML_FIELD_ATTRIB_QUICK:
94 case AML_FIELD_ATTRIB_SEND_RCV:
95 case AML_FIELD_ATTRIB_BYTE:
100 case AML_FIELD_ATTRIB_WORD:
101 case AML_FIELD_ATTRIB_WORD_CALL:
106 case AML_FIELD_ATTRIB_MULTIBYTE:
107 case AML_FIELD_ATTRIB_RAW_BYTES:
108 case AML_FIELD_ATTRIB_RAW_PROCESS:
110 Length = AccessLength;
113 case AML_FIELD_ATTRIB_BLOCK:
114 case AML_FIELD_ATTRIB_BLOCK_CALL:
117 Length = ACPI_GSBUS_BUFFER_SIZE - 2;
125 /*******************************************************************************
127 * FUNCTION: AcpiExReadDataFromField
129 * PARAMETERS: WalkState - Current execution state
130 * ObjDesc - The named field
131 * RetBufferDesc - Where the return data object is stored
135 * DESCRIPTION: Read from a named field. Returns either an Integer or a
136 * Buffer, depending on the size of the field.
138 ******************************************************************************/
141 AcpiExReadDataFromField (
142 ACPI_WALK_STATE *WalkState,
143 ACPI_OPERAND_OBJECT *ObjDesc,
144 ACPI_OPERAND_OBJECT **RetBufferDesc)
147 ACPI_OPERAND_OBJECT *BufferDesc;
154 ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
157 /* Parameter validation */
161 return_ACPI_STATUS (AE_AML_NO_OPERAND);
165 return_ACPI_STATUS (AE_BAD_PARAMETER);
168 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
171 * If the BufferField arguments have not been previously evaluated,
172 * evaluate them now and save the results.
174 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
176 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
177 if (ACPI_FAILURE (Status))
179 return_ACPI_STATUS (Status);
183 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
184 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
185 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
186 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
189 * This is an SMBus, GSBus or IPMI read. We must create a buffer to hold
190 * the data and then directly access the region handler.
192 * Note: SMBus and GSBus protocol value is passed in upper 16-bits of Function
194 if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
196 Length = ACPI_SMBUS_BUFFER_SIZE;
197 Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);
199 else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
201 AccessorType = ObjDesc->Field.Attribute;
202 Length = AcpiExGetSerialAccessLength (AccessorType,
203 ObjDesc->Field.AccessLength);
206 * Add additional 2 bytes for the GenericSerialBus data buffer:
208 * Status; (Byte 0 of the data buffer)
209 * Length; (Byte 1 of the data buffer)
210 * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
213 Function = ACPI_READ | (AccessorType << 16);
217 Length = ACPI_IPMI_BUFFER_SIZE;
218 Function = ACPI_READ;
221 BufferDesc = AcpiUtCreateBufferObject (Length);
224 return_ACPI_STATUS (AE_NO_MEMORY);
227 /* Lock entire transaction if requested */
229 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
231 /* Call the region handler for the read */
233 Status = AcpiExAccessRegion (ObjDesc, 0,
234 ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer),
236 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
241 * Allocate a buffer for the contents of the field.
243 * If the field is larger than the current integer width, create
244 * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
245 * the use of arithmetic operators on the returned value if the
246 * field size is equal or smaller than an Integer.
248 * Note: Field.length is in bits.
250 Length = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->Field.BitLength);
251 if (Length > AcpiGbl_IntegerByteWidth)
253 /* Field is too large for an Integer, create a Buffer instead */
255 BufferDesc = AcpiUtCreateBufferObject (Length);
258 return_ACPI_STATUS (AE_NO_MEMORY);
260 Buffer = BufferDesc->Buffer.Pointer;
264 /* Field will fit within an Integer (normal case) */
266 BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
269 return_ACPI_STATUS (AE_NO_MEMORY);
272 Length = AcpiGbl_IntegerByteWidth;
273 Buffer = &BufferDesc->Integer.Value;
276 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
277 "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
278 ObjDesc, ObjDesc->Common.Type, Buffer, (UINT32) Length));
279 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
280 "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
281 ObjDesc->CommonField.BitLength,
282 ObjDesc->CommonField.StartFieldBitOffset,
283 ObjDesc->CommonField.BaseByteOffset));
285 /* Lock entire transaction if requested */
287 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
289 /* Read from the field */
291 Status = AcpiExExtractFromField (ObjDesc, Buffer, (UINT32) Length);
292 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
296 if (ACPI_FAILURE (Status))
298 AcpiUtRemoveReference (BufferDesc);
302 *RetBufferDesc = BufferDesc;
305 return_ACPI_STATUS (Status);
309 /*******************************************************************************
311 * FUNCTION: AcpiExWriteDataToField
313 * PARAMETERS: SourceDesc - Contains data to write
314 * ObjDesc - The named field
315 * ResultDesc - Where the return value is returned, if any
319 * DESCRIPTION: Write to a named field
321 ******************************************************************************/
324 AcpiExWriteDataToField (
325 ACPI_OPERAND_OBJECT *SourceDesc,
326 ACPI_OPERAND_OBJECT *ObjDesc,
327 ACPI_OPERAND_OBJECT **ResultDesc)
332 ACPI_OPERAND_OBJECT *BufferDesc;
337 ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
340 /* Parameter validation */
342 if (!SourceDesc || !ObjDesc)
344 return_ACPI_STATUS (AE_AML_NO_OPERAND);
347 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
350 * If the BufferField arguments have not been previously evaluated,
351 * evaluate them now and save the results.
353 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
355 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
356 if (ACPI_FAILURE (Status))
358 return_ACPI_STATUS (Status);
362 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
363 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
364 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
365 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
368 * This is an SMBus, GSBus or IPMI write. We will bypass the entire field
369 * mechanism and handoff the buffer directly to the handler. For
370 * these address spaces, the buffer is bi-directional; on a write,
371 * return data is returned in the same buffer.
373 * Source must be a buffer of sufficient size:
374 * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
376 * Note: SMBus and GSBus protocol type is passed in upper 16-bits of Function
378 if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)
380 ACPI_ERROR ((AE_INFO,
381 "SMBus/IPMI/GenericSerialBus write requires Buffer, found type %s",
382 AcpiUtGetObjectTypeName (SourceDesc)));
384 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
387 if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
389 Length = ACPI_SMBUS_BUFFER_SIZE;
390 Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
392 else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
394 AccessorType = ObjDesc->Field.Attribute;
395 Length = AcpiExGetSerialAccessLength (AccessorType,
396 ObjDesc->Field.AccessLength);
399 * Add additional 2 bytes for the GenericSerialBus data buffer:
401 * Status; (Byte 0 of the data buffer)
402 * Length; (Byte 1 of the data buffer)
403 * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
406 Function = ACPI_WRITE | (AccessorType << 16);
410 Length = ACPI_IPMI_BUFFER_SIZE;
411 Function = ACPI_WRITE;
414 if (SourceDesc->Buffer.Length < Length)
416 ACPI_ERROR ((AE_INFO,
417 "SMBus/IPMI/GenericSerialBus write requires Buffer of length %u, found length %u",
418 Length, SourceDesc->Buffer.Length));
420 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
423 /* Create the bi-directional buffer */
425 BufferDesc = AcpiUtCreateBufferObject (Length);
428 return_ACPI_STATUS (AE_NO_MEMORY);
431 Buffer = BufferDesc->Buffer.Pointer;
432 ACPI_MEMCPY (Buffer, SourceDesc->Buffer.Pointer, Length);
434 /* Lock entire transaction if requested */
436 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
439 * Perform the write (returns status and perhaps data in the
442 Status = AcpiExAccessRegion (ObjDesc, 0,
443 (UINT64 *) Buffer, Function);
444 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
446 *ResultDesc = BufferDesc;
447 return_ACPI_STATUS (Status);
450 /* Get a pointer to the data to be written */
452 switch (SourceDesc->Common.Type)
454 case ACPI_TYPE_INTEGER:
456 Buffer = &SourceDesc->Integer.Value;
457 Length = sizeof (SourceDesc->Integer.Value);
460 case ACPI_TYPE_BUFFER:
462 Buffer = SourceDesc->Buffer.Pointer;
463 Length = SourceDesc->Buffer.Length;
466 case ACPI_TYPE_STRING:
468 Buffer = SourceDesc->String.Pointer;
469 Length = SourceDesc->String.Length;
474 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
477 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
478 "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
479 SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
480 SourceDesc->Common.Type, Buffer, Length));
482 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
483 "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
484 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
485 ObjDesc->Common.Type,
486 ObjDesc->CommonField.BitLength,
487 ObjDesc->CommonField.StartFieldBitOffset,
488 ObjDesc->CommonField.BaseByteOffset));
490 /* Lock entire transaction if requested */
492 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
494 /* Write to the field */
496 Status = AcpiExInsertIntoField (ObjDesc, Buffer, Length);
497 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
499 return_ACPI_STATUS (Status);