917cd2fd538f6d648e7c1fd5516c8c20f13f5791
[reactos.git] / drivers / bus / acpi / acpica / executer / exfield.c
1 /******************************************************************************
2 *
3 * Module Name: exfield - AML execution - FieldUnit read/write
4 *
5 *****************************************************************************/
6
7 /*
8 * Copyright (C) 2000 - 2019, Intel Corp.
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
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.
25 *
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.
29 *
30 * NO WARRANTY
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.
42 */
43
44 #include "acpi.h"
45 #include "accommon.h"
46 #include "acdispat.h"
47 #include "acinterp.h"
48 #include "amlcode.h"
49
50
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME ("exfield")
53
54
55 /*
56 * This table maps the various Attrib protocols to the byte transfer
57 * length. Used for the generic serial bus.
58 */
59 #define ACPI_INVALID_PROTOCOL_ID 0x80
60 #define ACPI_MAX_PROTOCOL_ID 0x0F
61
62 const UINT8 AcpiProtocolLengths[] =
63 {
64 ACPI_INVALID_PROTOCOL_ID, /* 0 - reserved */
65 ACPI_INVALID_PROTOCOL_ID, /* 1 - reserved */
66 0x00, /* 2 - ATTRIB_QUICK */
67 ACPI_INVALID_PROTOCOL_ID, /* 3 - reserved */
68 0x01, /* 4 - ATTRIB_SEND_RECEIVE */
69 ACPI_INVALID_PROTOCOL_ID, /* 5 - reserved */
70 0x01, /* 6 - ATTRIB_BYTE */
71 ACPI_INVALID_PROTOCOL_ID, /* 7 - reserved */
72 0x02, /* 8 - ATTRIB_WORD */
73 ACPI_INVALID_PROTOCOL_ID, /* 9 - reserved */
74 0xFF, /* A - ATTRIB_BLOCK */
75 0xFF, /* B - ATTRIB_BYTES */
76 0x02, /* C - ATTRIB_PROCESS_CALL */
77 0xFF, /* D - ATTRIB_BLOCK_PROCESS_CALL */
78 0xFF, /* E - ATTRIB_RAW_BYTES */
79 0xFF /* F - ATTRIB_RAW_PROCESS_BYTES */
80 };
81
82
83 /*******************************************************************************
84 *
85 * FUNCTION: AcpiExGetProtocolBufferLength
86 *
87 * PARAMETERS: ProtocolId - The type of the protocol indicated by region
88 * field access attributes
89 * ReturnLength - Where the protocol byte transfer length is
90 * returned
91 *
92 * RETURN: Status and decoded byte transfer length
93 *
94 * DESCRIPTION: This routine returns the length of the GenericSerialBus
95 * protocol bytes
96 *
97 ******************************************************************************/
98
99 ACPI_STATUS
100 AcpiExGetProtocolBufferLength (
101 UINT32 ProtocolId,
102 UINT32 *ReturnLength)
103 {
104
105 if ((ProtocolId > ACPI_MAX_PROTOCOL_ID) ||
106 (AcpiProtocolLengths[ProtocolId] == ACPI_INVALID_PROTOCOL_ID))
107 {
108 ACPI_ERROR ((AE_INFO,
109 "Invalid Field/AccessAs protocol ID: 0x%4.4X", ProtocolId));
110
111 return (AE_AML_PROTOCOL);
112 }
113
114 *ReturnLength = AcpiProtocolLengths[ProtocolId];
115 return (AE_OK);
116 }
117
118
119 /*******************************************************************************
120 *
121 * FUNCTION: AcpiExReadDataFromField
122 *
123 * PARAMETERS: WalkState - Current execution state
124 * ObjDesc - The named field
125 * RetBufferDesc - Where the return data object is stored
126 *
127 * RETURN: Status
128 *
129 * DESCRIPTION: Read from a named field. Returns either an Integer or a
130 * Buffer, depending on the size of the field.
131 *
132 ******************************************************************************/
133
134 ACPI_STATUS
135 AcpiExReadDataFromField (
136 ACPI_WALK_STATE *WalkState,
137 ACPI_OPERAND_OBJECT *ObjDesc,
138 ACPI_OPERAND_OBJECT **RetBufferDesc)
139 {
140 ACPI_STATUS Status;
141 ACPI_OPERAND_OBJECT *BufferDesc;
142 void *Buffer;
143 UINT32 BufferLength;
144
145
146 ACPI_FUNCTION_TRACE_PTR (ExReadDataFromField, ObjDesc);
147
148
149 /* Parameter validation */
150
151 if (!ObjDesc)
152 {
153 return_ACPI_STATUS (AE_AML_NO_OPERAND);
154 }
155 if (!RetBufferDesc)
156 {
157 return_ACPI_STATUS (AE_BAD_PARAMETER);
158 }
159
160 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
161 {
162 /*
163 * If the BufferField arguments have not been previously evaluated,
164 * evaluate them now and save the results.
165 */
166 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
167 {
168 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
169 if (ACPI_FAILURE (Status))
170 {
171 return_ACPI_STATUS (Status);
172 }
173 }
174 }
175 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
176 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
177 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
178 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
179 {
180 /* SMBus, GSBus, IPMI serial */
181
182 Status = AcpiExReadSerialBus (ObjDesc, RetBufferDesc);
183 return_ACPI_STATUS (Status);
184 }
185
186 /*
187 * Allocate a buffer for the contents of the field.
188 *
189 * If the field is larger than the current integer width, create
190 * a BUFFER to hold it. Otherwise, use an INTEGER. This allows
191 * the use of arithmetic operators on the returned value if the
192 * field size is equal or smaller than an Integer.
193 *
194 * Note: Field.length is in bits.
195 */
196 BufferLength = (ACPI_SIZE) ACPI_ROUND_BITS_UP_TO_BYTES (
197 ObjDesc->Field.BitLength);
198
199 if (BufferLength > AcpiGbl_IntegerByteWidth)
200 {
201 /* Field is too large for an Integer, create a Buffer instead */
202
203 BufferDesc = AcpiUtCreateBufferObject (BufferLength);
204 if (!BufferDesc)
205 {
206 return_ACPI_STATUS (AE_NO_MEMORY);
207 }
208 Buffer = BufferDesc->Buffer.Pointer;
209 }
210 else
211 {
212 /* Field will fit within an Integer (normal case) */
213
214 BufferDesc = AcpiUtCreateIntegerObject ((UINT64) 0);
215 if (!BufferDesc)
216 {
217 return_ACPI_STATUS (AE_NO_MEMORY);
218 }
219
220 BufferLength = AcpiGbl_IntegerByteWidth;
221 Buffer = &BufferDesc->Integer.Value;
222 }
223
224 if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
225 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
226 {
227 /* General Purpose I/O */
228
229 Status = AcpiExReadGpio (ObjDesc, Buffer);
230 goto Exit;
231 }
232
233 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
234 "FieldRead [TO]: Obj %p, Type %X, Buf %p, ByteLen %X\n",
235 ObjDesc, ObjDesc->Common.Type, Buffer, BufferLength));
236 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
237 "FieldRead [FROM]: BitLen %X, BitOff %X, ByteOff %X\n",
238 ObjDesc->CommonField.BitLength,
239 ObjDesc->CommonField.StartFieldBitOffset,
240 ObjDesc->CommonField.BaseByteOffset));
241
242 /* Lock entire transaction if requested */
243
244 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
245
246 /* Read from the field */
247
248 Status = AcpiExExtractFromField (ObjDesc, Buffer, BufferLength);
249 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
250
251
252 Exit:
253 if (ACPI_FAILURE (Status))
254 {
255 AcpiUtRemoveReference (BufferDesc);
256 }
257 else
258 {
259 *RetBufferDesc = BufferDesc;
260 }
261
262 return_ACPI_STATUS (Status);
263 }
264
265
266 /*******************************************************************************
267 *
268 * FUNCTION: AcpiExWriteDataToField
269 *
270 * PARAMETERS: SourceDesc - Contains data to write
271 * ObjDesc - The named field
272 * ResultDesc - Where the return value is returned, if any
273 *
274 * RETURN: Status
275 *
276 * DESCRIPTION: Write to a named field
277 *
278 ******************************************************************************/
279
280 ACPI_STATUS
281 AcpiExWriteDataToField (
282 ACPI_OPERAND_OBJECT *SourceDesc,
283 ACPI_OPERAND_OBJECT *ObjDesc,
284 ACPI_OPERAND_OBJECT **ResultDesc)
285 {
286 ACPI_STATUS Status;
287 UINT32 BufferLength;
288 void *Buffer;
289
290
291 ACPI_FUNCTION_TRACE_PTR (ExWriteDataToField, ObjDesc);
292
293
294 /* Parameter validation */
295
296 if (!SourceDesc || !ObjDesc)
297 {
298 return_ACPI_STATUS (AE_AML_NO_OPERAND);
299 }
300
301 if (ObjDesc->Common.Type == ACPI_TYPE_BUFFER_FIELD)
302 {
303 /*
304 * If the BufferField arguments have not been previously evaluated,
305 * evaluate them now and save the results.
306 */
307 if (!(ObjDesc->Common.Flags & AOPOBJ_DATA_VALID))
308 {
309 Status = AcpiDsGetBufferFieldArguments (ObjDesc);
310 if (ACPI_FAILURE (Status))
311 {
312 return_ACPI_STATUS (Status);
313 }
314 }
315 }
316 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
317 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GPIO))
318 {
319 /* General Purpose I/O */
320
321 Status = AcpiExWriteGpio (SourceDesc, ObjDesc, ResultDesc);
322 return_ACPI_STATUS (Status);
323 }
324 else if ((ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REGION_FIELD) &&
325 (ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_SMBUS ||
326 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_GSBUS ||
327 ObjDesc->Field.RegionObj->Region.SpaceId == ACPI_ADR_SPACE_IPMI))
328 {
329 /* SMBus, GSBus, IPMI serial */
330
331 Status = AcpiExWriteSerialBus (SourceDesc, ObjDesc, ResultDesc);
332 return_ACPI_STATUS (Status);
333 }
334
335 /* Get a pointer to the data to be written */
336
337 switch (SourceDesc->Common.Type)
338 {
339 case ACPI_TYPE_INTEGER:
340
341 Buffer = &SourceDesc->Integer.Value;
342 BufferLength = sizeof (SourceDesc->Integer.Value);
343 break;
344
345 case ACPI_TYPE_BUFFER:
346
347 Buffer = SourceDesc->Buffer.Pointer;
348 BufferLength = SourceDesc->Buffer.Length;
349 break;
350
351 case ACPI_TYPE_STRING:
352
353 Buffer = SourceDesc->String.Pointer;
354 BufferLength = SourceDesc->String.Length;
355 break;
356
357 default:
358 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
359 }
360
361 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
362 "FieldWrite [FROM]: Obj %p (%s:%X), Buf %p, ByteLen %X\n",
363 SourceDesc, AcpiUtGetTypeName (SourceDesc->Common.Type),
364 SourceDesc->Common.Type, Buffer, BufferLength));
365
366 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
367 "FieldWrite [TO]: Obj %p (%s:%X), BitLen %X, BitOff %X, ByteOff %X\n",
368 ObjDesc, AcpiUtGetTypeName (ObjDesc->Common.Type),
369 ObjDesc->Common.Type,
370 ObjDesc->CommonField.BitLength,
371 ObjDesc->CommonField.StartFieldBitOffset,
372 ObjDesc->CommonField.BaseByteOffset));
373
374 /* Lock entire transaction if requested */
375
376 AcpiExAcquireGlobalLock (ObjDesc->CommonField.FieldFlags);
377
378 /* Write to the field */
379
380 Status = AcpiExInsertIntoField (ObjDesc, Buffer, BufferLength);
381 AcpiExReleaseGlobalLock (ObjDesc->CommonField.FieldFlags);
382 return_ACPI_STATUS (Status);
383 }