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.
54 #define _COMPONENT ACPI_EXECUTER
55 ACPI_MODULE_NAME ("exfield")
57 /* Local prototypes */
60 AcpiExGetSerialAccessLength (
65 /*******************************************************************************
67 * FUNCTION: AcpiExGetSerialAccessLength
69 * PARAMETERS: AccessorType - The type of the protocol indicated by region
70 * field access attributes
71 * AccessLength - The access length of the region field
73 * RETURN: Decoded access length
75 * DESCRIPTION: This routine returns the length of the GenericSerialBus
78 ******************************************************************************/
81 AcpiExGetSerialAccessLength (
90 case AML_FIELD_ATTRIB_QUICK:
95 case AML_FIELD_ATTRIB_SEND_RCV:
96 case AML_FIELD_ATTRIB_BYTE:
101 case AML_FIELD_ATTRIB_WORD:
102 case AML_FIELD_ATTRIB_WORD_CALL:
107 case AML_FIELD_ATTRIB_MULTIBYTE:
108 case AML_FIELD_ATTRIB_RAW_BYTES:
109 case AML_FIELD_ATTRIB_RAW_PROCESS:
111 Length = AccessLength;
114 case AML_FIELD_ATTRIB_BLOCK:
115 case AML_FIELD_ATTRIB_BLOCK_CALL:
118 Length = ACPI_GSBUS_BUFFER_SIZE - 2;
126 /*******************************************************************************
128 * FUNCTION: AcpiExReadDataFromField
130 * PARAMETERS: WalkState - Current execution state
131 * ObjDesc - The named field
132 * RetBufferDesc - Where the return data object is stored
136 * DESCRIPTION: Read from a named field. Returns either an Integer or a
137 * Buffer, depending on the size of the field.
139 ******************************************************************************/
142 AcpiExReadDataFromField (
143 ACPI_WALK_STATE *WalkState,
144 ACPI_OPERAND_OBJECT *ObjDesc,
145 ACPI_OPERAND_OBJECT **RetBufferDesc)
148 ACPI_OPERAND_OBJECT *BufferDesc;
155 ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
158 /* Parameter validation */
162 return_ACPI_STATUS (AE_AML_NO_OPERAND);
166 return_ACPI_STATUS (AE_BAD_PARAMETER);
169 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
172 * If the BufferField arguments have not been previously evaluated,
173 * evaluate them now and save the results.
175 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
177 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
178 if (ACPI_FAILURE (Status))
180 return_ACPI_STATUS (Status);
184 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
185 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
186 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
187 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
190 * This is an SMBus, GSBus or IPMI read. We must create a buffer to hold
191 * the data and then directly access the region handler.
193 * Note: SMBus and GSBus protocol value is passed in upper 16-bits of Function
195 if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
197 Length = ACPI_SMBUS_BUFFER_SIZE;
198 Function = ACPI_READ | (ObjDesc->Field.Attribute << 16);
200 else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
202 AccessorType = ObjDesc->Field.Attribute;
203 Length = AcpiExGetSerialAccessLength (AccessorType,
204 ObjDesc->Field.AccessLength);
207 * Add additional 2 bytes for modeled GenericSerialBus data buffer:
209 * BYTEStatus; // Byte 0 of the data buffer
210 * BYTELength; // Byte 1 of the data buffer
211 * BYTE[x-1]Data; // Bytes 2-x of the arbitrary length data buffer,
215 Function = ACPI_READ | (AccessorType << 16);
219 Length = ACPI_IPMI_BUFFER_SIZE;
220 Function = ACPI_READ;
223 BufferDesc = AcpiUtCreateBufferObject (Length);
226 return_ACPI_STATUS (AE_NO_MEMORY);
229 /* Lock entire transaction if requested */
231 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
233 /* Call the region handler for the read */
235 Status = AcpiExAccessRegion (ObjDesc, 0,
236 ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer),
238 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
243 * Allocate a buffer for the contents of the field.
245 * If the field is larger than the current integer width, create
246 * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
247 * the use of arithmetic operators on the returned value if the
248 * field size is equal or smaller than an Integer.
250 * Note: Field.length is in bits.
252 Length = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->Field.BitLength);
253 if (Length > AcpiGbl_IntegerByteWidth)
255 /* Field is too large for an Integer, create a Buffer instead */
257 BufferDesc = AcpiUtCreateBufferObject (Length);
260 return_ACPI_STATUS (AE_NO_MEMORY);
262 Buffer = BufferDesc->Buffer.Pointer;
266 /* Field will fit within an Integer (normal case) */
268 BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
271 return_ACPI_STATUS (AE_NO_MEMORY);
274 Length = AcpiGbl_IntegerByteWidth;
275 Buffer = &BufferDesc->Integer.Value;
278 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
279 "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
280 ObjDesc, ObjDesc->Common.Type, Buffer, (UINT32) Length));
281 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
282 "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
283 ObjDesc->CommonField.BitLength,
284 ObjDesc->CommonField.StartFieldBitOffset,
285 ObjDesc->CommonField.BaseByteOffset));
287 /* Lock entire transaction if requested */
289 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
291 /* Read from the field */
293 Status = AcpiExExtractFromField (ObjDesc, Buffer, (UINT32) Length);
294 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
298 if (ACPI_FAILURE (Status))
300 AcpiUtRemoveReference (BufferDesc);
304 *RetBufferDesc = BufferDesc;
307 return_ACPI_STATUS (Status);
311 /*******************************************************************************
313 * FUNCTION: AcpiExWriteDataToField
315 * PARAMETERS: SourceDesc - Contains data to write
316 * ObjDesc - The named field
317 * ResultDesc - Where the return value is returned, if any
321 * DESCRIPTION: Write to a named field
323 ******************************************************************************/
326 AcpiExWriteDataToField (
327 ACPI_OPERAND_OBJECT *SourceDesc,
328 ACPI_OPERAND_OBJECT *ObjDesc,
329 ACPI_OPERAND_OBJECT **ResultDesc)
334 ACPI_OPERAND_OBJECT *BufferDesc;
339 ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
342 /* Parameter validation */
344 if (!SourceDesc || !ObjDesc)
346 return_ACPI_STATUS (AE_AML_NO_OPERAND);
349 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
352 * If the BufferField arguments have not been previously evaluated,
353 * evaluate them now and save the results.
355 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
357 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
358 if (ACPI_FAILURE (Status))
360 return_ACPI_STATUS (Status);
364 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
365 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
366 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
367 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
370 * This is an SMBus, GSBus or IPMI write. We will bypass the entire field
371 * mechanism and handoff the buffer directly to the handler. For
372 * these address spaces, the buffer is bi-directional; on a write,
373 * return data is returned in the same buffer.
375 * Source must be a buffer of sufficient size:
376 * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
378 * Note: SMBus and GSBus protocol type is passed in upper 16-bits of Function
380 if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)
382 ACPI_ERROR ((AE_INFO,
383 "SMBus/IPMI/GenericSerialBus write requires Buffer, found type %s",
384 AcpiUtGetObjectTypeName (SourceDesc)));
386 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
389 if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
391 Length = ACPI_SMBUS_BUFFER_SIZE;
392 Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
394 else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
396 AccessorType = ObjDesc->Field.Attribute;
397 Length = AcpiExGetSerialAccessLength (AccessorType,
398 ObjDesc->Field.AccessLength);
401 * Add additional 2 bytes for modeled GenericSerialBus data buffer:
403 * BYTEStatus; // Byte 0 of the data buffer
404 * BYTELength; // Byte 1 of the data buffer
405 * BYTE[x-1]Data; // Bytes 2-x of the arbitrary length data buffer,
409 Function = ACPI_WRITE | (AccessorType << 16);
413 Length = ACPI_IPMI_BUFFER_SIZE;
414 Function = ACPI_WRITE;
417 if (SourceDesc->Buffer.Length < Length)
419 ACPI_ERROR ((AE_INFO,
420 "SMBus/IPMI/GenericSerialBus write requires Buffer of length %u, found length %u",
421 Length, SourceDesc->Buffer.Length));
423 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
426 /* Create the bi-directional buffer */
428 BufferDesc = AcpiUtCreateBufferObject (Length);
431 return_ACPI_STATUS (AE_NO_MEMORY);
434 Buffer = BufferDesc->Buffer.Pointer;
435 ACPI_MEMCPY (Buffer, SourceDesc->Buffer.Pointer, Length);
437 /* Lock entire transaction if requested */
439 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
442 * Perform the write (returns status and perhaps data in the
445 Status = AcpiExAccessRegion (ObjDesc, 0,
446 (UINT64 *) Buffer, Function);
447 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
449 *ResultDesc = BufferDesc;
450 return_ACPI_STATUS (Status);
453 /* Get a pointer to the data to be written */
455 switch (SourceDesc->Common.Type)
457 case ACPI_TYPE_INTEGER:
459 Buffer = &SourceDesc->Integer.Value;
460 Length = sizeof (SourceDesc->Integer.Value);
463 case ACPI_TYPE_BUFFER:
465 Buffer = SourceDesc->Buffer.Pointer;
466 Length = SourceDesc->Buffer.Length;
469 case ACPI_TYPE_STRING:
471 Buffer = SourceDesc->String.Pointer;
472 Length = SourceDesc->String.Length;
477 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
480 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
481 "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
482 SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
483 SourceDesc->Common.Type, Buffer, Length));
485 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
486 "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
487 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
488 ObjDesc->Common.Type,
489 ObjDesc->CommonField.BitLength,
490 ObjDesc->CommonField.StartFieldBitOffset,
491 ObjDesc->CommonField.BaseByteOffset));
493 /* Lock entire transaction if requested */
495 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
497 /* Write to the field */
499 Status = AcpiExInsertIntoField (ObjDesc, Buffer, Length);
500 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
502 return_ACPI_STATUS (Status);