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 the GenericSerialBus data buffer:
209 * Status; (Byte 0 of the data buffer)
210 * Length; (Byte 1 of the data buffer)
211 * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
214 Function = ACPI_READ | (AccessorType << 16);
218 Length = ACPI_IPMI_BUFFER_SIZE;
219 Function = ACPI_READ;
222 BufferDesc = AcpiUtCreateBufferObject (Length);
225 return_ACPI_STATUS (AE_NO_MEMORY);
228 /* Lock entire transaction if requested */
230 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
232 /* Call the region handler for the read */
234 Status = AcpiExAccessRegion (ObjDesc, 0,
235 ACPI_CAST_PTR (UINT64, BufferDesc->Buffer.Pointer),
237 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
242 * Allocate a buffer for the contents of the field.
244 * If the field is larger than the current integer width, create
245 * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
246 * the use of arithmetic operators on the returned value if the
247 * field size is equal or smaller than an Integer.
249 * Note: Field.length is in bits.
251 Length = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (ObjDesc->Field.BitLength);
252 if (Length > AcpiGbl_IntegerByteWidth)
254 /* Field is too large for an Integer, create a Buffer instead */
256 BufferDesc = AcpiUtCreateBufferObject (Length);
259 return_ACPI_STATUS (AE_NO_MEMORY);
261 Buffer = BufferDesc->Buffer.Pointer;
265 /* Field will fit within an Integer (normal case) */
267 BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
270 return_ACPI_STATUS (AE_NO_MEMORY);
273 Length = AcpiGbl_IntegerByteWidth;
274 Buffer = &BufferDesc->Integer.Value;
277 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
278 "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
279 ObjDesc, ObjDesc->Common.Type, Buffer, (UINT32) Length));
280 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
281 "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
282 ObjDesc->CommonField.BitLength,
283 ObjDesc->CommonField.StartFieldBitOffset,
284 ObjDesc->CommonField.BaseByteOffset));
286 /* Lock entire transaction if requested */
288 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
290 /* Read from the field */
292 Status = AcpiExExtractFromField (ObjDesc, Buffer, (UINT32) Length);
293 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
297 if (ACPI_FAILURE (Status))
299 AcpiUtRemoveReference (BufferDesc);
303 *RetBufferDesc = BufferDesc;
306 return_ACPI_STATUS (Status);
310 /*******************************************************************************
312 * FUNCTION: AcpiExWriteDataToField
314 * PARAMETERS: SourceDesc - Contains data to write
315 * ObjDesc - The named field
316 * ResultDesc - Where the return value is returned, if any
320 * DESCRIPTION: Write to a named field
322 ******************************************************************************/
325 AcpiExWriteDataToField (
326 ACPI_OPERAND_OBJECT *SourceDesc,
327 ACPI_OPERAND_OBJECT *ObjDesc,
328 ACPI_OPERAND_OBJECT **ResultDesc)
333 ACPI_OPERAND_OBJECT *BufferDesc;
338 ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
341 /* Parameter validation */
343 if (!SourceDesc || !ObjDesc)
345 return_ACPI_STATUS (AE_AML_NO_OPERAND);
348 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
351 * If the BufferField arguments have not been previously evaluated,
352 * evaluate them now and save the results.
354 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
356 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
357 if (ACPI_FAILURE (Status))
359 return_ACPI_STATUS (Status);
363 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
364 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
365 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
366 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
369 * This is an SMBus, GSBus or IPMI write. We will bypass the entire field
370 * mechanism and handoff the buffer directly to the handler. For
371 * these address spaces, the buffer is bi-directional; on a write,
372 * return data is returned in the same buffer.
374 * Source must be a buffer of sufficient size:
375 * ACPI_SMBUS_BUFFER_SIZE, ACPI_GSBUS_BUFFER_SIZE, or ACPI_IPMI_BUFFER_SIZE.
377 * Note: SMBus and GSBus protocol type is passed in upper 16-bits of Function
379 if (SourceDesc->Common.Type != ACPI_TYPE_BUFFER)
381 ACPI_ERROR ((AE_INFO,
382 "SMBus/IPMI/GenericSerialBus write requires Buffer, found type %s",
383 AcpiUtGetObjectTypeName (SourceDesc)));
385 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
388 if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS)
390 Length = ACPI_SMBUS_BUFFER_SIZE;
391 Function = ACPI_WRITE | (ObjDesc->Field.Attribute << 16);
393 else if (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS)
395 AccessorType = ObjDesc->Field.Attribute;
396 Length = AcpiExGetSerialAccessLength (AccessorType,
397 ObjDesc->Field.AccessLength);
400 * Add additional 2 bytes for the GenericSerialBus data buffer:
402 * Status; (Byte 0 of the data buffer)
403 * Length; (Byte 1 of the data buffer)
404 * Data[x-1]; (Bytes 2-x of the arbitrary length data buffer)
407 Function = ACPI_WRITE | (AccessorType << 16);
411 Length = ACPI_IPMI_BUFFER_SIZE;
412 Function = ACPI_WRITE;
415 if (SourceDesc->Buffer.Length < Length)
417 ACPI_ERROR ((AE_INFO,
418 "SMBus/IPMI/GenericSerialBus write requires Buffer of length %u, found length %u",
419 Length, SourceDesc->Buffer.Length));
421 return_ACPI_STATUS (AE_AML_BUFFER_LIMIT);
424 /* Create the bi-directional buffer */
426 BufferDesc = AcpiUtCreateBufferObject (Length);
429 return_ACPI_STATUS (AE_NO_MEMORY);
432 Buffer = BufferDesc->Buffer.Pointer;
433 ACPI_MEMCPY (Buffer, SourceDesc->Buffer.Pointer, Length);
435 /* Lock entire transaction if requested */
437 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
440 * Perform the write (returns status and perhaps data in the
443 Status = AcpiExAccessRegion (ObjDesc, 0,
444 (UINT64 *) Buffer, Function);
445 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
447 *ResultDesc = BufferDesc;
448 return_ACPI_STATUS (Status);
451 /* Get a pointer to the data to be written */
453 switch (SourceDesc->Common.Type)
455 case ACPI_TYPE_INTEGER:
457 Buffer = &SourceDesc->Integer.Value;
458 Length = sizeof (SourceDesc->Integer.Value);
461 case ACPI_TYPE_BUFFER:
463 Buffer = SourceDesc->Buffer.Pointer;
464 Length = SourceDesc->Buffer.Length;
467 case ACPI_TYPE_STRING:
469 Buffer = SourceDesc->String.Pointer;
470 Length = SourceDesc->String.Length;
475 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
478 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
479 "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
480 SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
481 SourceDesc->Common.Type, Buffer, Length));
483 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
484 "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
485 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
486 ObjDesc->Common.Type,
487 ObjDesc->CommonField.BitLength,
488 ObjDesc->CommonField.StartFieldBitOffset,
489 ObjDesc->CommonField.BaseByteOffset));
491 /* Lock entire transaction if requested */
493 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
495 /* Write to the field */
497 Status = AcpiExInsertIntoField (ObjDesc, Buffer, Length);
498 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
500 return_ACPI_STATUS (Status);