Commit | Line | Data |
---|---|---|
1da177e4 LT |
1 | /****************************************************************************** |
2 | * | |
3 | * Module Name: exfldio - Aml Field I/O | |
4 | * | |
5 | *****************************************************************************/ | |
6 | ||
7 | /* | |
8 | * Copyright (C) 2000 - 2005, R. Byron Moore | |
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 | ||
1da177e4 LT |
44 | #include <acpi/acpi.h> |
45 | #include <acpi/acinterp.h> | |
46 | #include <acpi/amlcode.h> | |
47 | #include <acpi/acevents.h> | |
48 | #include <acpi/acdispat.h> | |
49 | ||
1da177e4 | 50 | #define _COMPONENT ACPI_EXECUTER |
4be44fcd | 51 | ACPI_MODULE_NAME("exfldio") |
1da177e4 | 52 | |
44f6c012 | 53 | /* Local prototypes */ |
44f6c012 | 54 | static acpi_status |
4be44fcd LB |
55 | acpi_ex_field_datum_io(union acpi_operand_object *obj_desc, |
56 | u32 field_datum_byte_offset, | |
57 | acpi_integer * value, u32 read_write); | |
44f6c012 RM |
58 | |
59 | static u8 | |
4be44fcd LB |
60 | acpi_ex_register_overflow(union acpi_operand_object *obj_desc, |
61 | acpi_integer value); | |
44f6c012 RM |
62 | |
63 | static acpi_status | |
4be44fcd LB |
64 | acpi_ex_setup_region(union acpi_operand_object *obj_desc, |
65 | u32 field_datum_byte_offset); | |
1da177e4 LT |
66 | |
67 | /******************************************************************************* | |
68 | * | |
69 | * FUNCTION: acpi_ex_setup_region | |
70 | * | |
44f6c012 | 71 | * PARAMETERS: obj_desc - Field to be read or written |
1da177e4 LT |
72 | * field_datum_byte_offset - Byte offset of this datum within the |
73 | * parent field | |
74 | * | |
75 | * RETURN: Status | |
76 | * | |
77 | * DESCRIPTION: Common processing for acpi_ex_extract_from_field and | |
78 | * acpi_ex_insert_into_field. Initialize the Region if necessary and | |
79 | * validate the request. | |
80 | * | |
81 | ******************************************************************************/ | |
82 | ||
44f6c012 | 83 | static acpi_status |
4be44fcd LB |
84 | acpi_ex_setup_region(union acpi_operand_object *obj_desc, |
85 | u32 field_datum_byte_offset) | |
1da177e4 | 86 | { |
4be44fcd LB |
87 | acpi_status status = AE_OK; |
88 | union acpi_operand_object *rgn_desc; | |
1da177e4 | 89 | |
4be44fcd | 90 | ACPI_FUNCTION_TRACE_U32("ex_setup_region", field_datum_byte_offset); |
1da177e4 LT |
91 | |
92 | rgn_desc = obj_desc->common_field.region_obj; | |
93 | ||
94 | /* We must have a valid region */ | |
95 | ||
4be44fcd LB |
96 | if (ACPI_GET_OBJECT_TYPE(rgn_desc) != ACPI_TYPE_REGION) { |
97 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, | |
98 | "Needed Region, found type %X (%s)\n", | |
99 | ACPI_GET_OBJECT_TYPE(rgn_desc), | |
100 | acpi_ut_get_object_type_name(rgn_desc))); | |
1da177e4 | 101 | |
4be44fcd | 102 | return_ACPI_STATUS(AE_AML_OPERAND_TYPE); |
1da177e4 LT |
103 | } |
104 | ||
105 | /* | |
106 | * If the Region Address and Length have not been previously evaluated, | |
107 | * evaluate them now and save the results. | |
108 | */ | |
109 | if (!(rgn_desc->common.flags & AOPOBJ_DATA_VALID)) { | |
4be44fcd LB |
110 | status = acpi_ds_get_region_arguments(rgn_desc); |
111 | if (ACPI_FAILURE(status)) { | |
112 | return_ACPI_STATUS(status); | |
1da177e4 LT |
113 | } |
114 | } | |
115 | ||
116 | if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) { | |
117 | /* SMBus has a non-linear address space */ | |
118 | ||
4be44fcd | 119 | return_ACPI_STATUS(AE_OK); |
1da177e4 | 120 | } |
1da177e4 LT |
121 | #ifdef ACPI_UNDER_DEVELOPMENT |
122 | /* | |
123 | * If the Field access is any_acc, we can now compute the optimal | |
124 | * access (because we know know the length of the parent region) | |
125 | */ | |
126 | if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) { | |
4be44fcd LB |
127 | if (ACPI_FAILURE(status)) { |
128 | return_ACPI_STATUS(status); | |
1da177e4 LT |
129 | } |
130 | } | |
131 | #endif | |
132 | ||
133 | /* | |
134 | * Validate the request. The entire request from the byte offset for a | |
135 | * length of one field datum (access width) must fit within the region. | |
136 | * (Region length is specified in bytes) | |
137 | */ | |
44f6c012 | 138 | if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset + |
4be44fcd LB |
139 | field_datum_byte_offset + |
140 | obj_desc->common_field. | |
141 | access_byte_width)) { | |
1da177e4 LT |
142 | if (acpi_gbl_enable_interpreter_slack) { |
143 | /* | |
144 | * Slack mode only: We will go ahead and allow access to this | |
145 | * field if it is within the region length rounded up to the next | |
146 | * access width boundary. | |
147 | */ | |
4be44fcd LB |
148 | if (ACPI_ROUND_UP(rgn_desc->region.length, |
149 | obj_desc->common_field. | |
150 | access_byte_width) >= | |
151 | (obj_desc->common_field.base_byte_offset + | |
152 | (acpi_native_uint) obj_desc->common_field. | |
153 | access_byte_width + field_datum_byte_offset)) { | |
154 | return_ACPI_STATUS(AE_OK); | |
1da177e4 LT |
155 | } |
156 | } | |
157 | ||
4be44fcd LB |
158 | if (rgn_desc->region.length < |
159 | obj_desc->common_field.access_byte_width) { | |
1da177e4 LT |
160 | /* |
161 | * This is the case where the access_type (acc_word, etc.) is wider | |
162 | * than the region itself. For example, a region of length one | |
163 | * byte, and a field with Dword access specified. | |
164 | */ | |
4be44fcd LB |
165 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
166 | "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n", | |
167 | acpi_ut_get_node_name(obj_desc-> | |
168 | common_field. | |
169 | node), | |
170 | obj_desc->common_field. | |
171 | access_byte_width, | |
172 | acpi_ut_get_node_name(rgn_desc-> | |
173 | region.node), | |
174 | rgn_desc->region.length)); | |
1da177e4 LT |
175 | } |
176 | ||
177 | /* | |
178 | * Offset rounded up to next multiple of field width | |
179 | * exceeds region length, indicate an error | |
180 | */ | |
4be44fcd LB |
181 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
182 | "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n", | |
183 | acpi_ut_get_node_name(obj_desc->common_field. | |
184 | node), | |
185 | obj_desc->common_field.base_byte_offset, | |
186 | field_datum_byte_offset, | |
187 | obj_desc->common_field.access_byte_width, | |
188 | acpi_ut_get_node_name(rgn_desc->region.node), | |
189 | rgn_desc->region.length)); | |
190 | ||
191 | return_ACPI_STATUS(AE_AML_REGION_LIMIT); | |
1da177e4 LT |
192 | } |
193 | ||
4be44fcd | 194 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
195 | } |
196 | ||
1da177e4 LT |
197 | /******************************************************************************* |
198 | * | |
199 | * FUNCTION: acpi_ex_access_region | |
200 | * | |
44f6c012 | 201 | * PARAMETERS: obj_desc - Field to be read |
1da177e4 LT |
202 | * field_datum_byte_offset - Byte offset of this datum within the |
203 | * parent field | |
44f6c012 | 204 | * Value - Where to store value (must at least |
1da177e4 LT |
205 | * the size of acpi_integer) |
206 | * Function - Read or Write flag plus other region- | |
207 | * dependent flags | |
208 | * | |
209 | * RETURN: Status | |
210 | * | |
211 | * DESCRIPTION: Read or Write a single field datum to an Operation Region. | |
212 | * | |
213 | ******************************************************************************/ | |
214 | ||
215 | acpi_status | |
4be44fcd LB |
216 | acpi_ex_access_region(union acpi_operand_object *obj_desc, |
217 | u32 field_datum_byte_offset, | |
218 | acpi_integer * value, u32 function) | |
1da177e4 | 219 | { |
4be44fcd LB |
220 | acpi_status status; |
221 | union acpi_operand_object *rgn_desc; | |
222 | acpi_physical_address address; | |
1da177e4 | 223 | |
4be44fcd | 224 | ACPI_FUNCTION_TRACE("ex_access_region"); |
1da177e4 LT |
225 | |
226 | /* | |
227 | * Ensure that the region operands are fully evaluated and verify | |
228 | * the validity of the request | |
229 | */ | |
4be44fcd LB |
230 | status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset); |
231 | if (ACPI_FAILURE(status)) { | |
232 | return_ACPI_STATUS(status); | |
1da177e4 LT |
233 | } |
234 | ||
235 | /* | |
236 | * The physical address of this field datum is: | |
237 | * | |
238 | * 1) The base of the region, plus | |
239 | * 2) The base offset of the field, plus | |
240 | * 3) The current offset into the field | |
241 | */ | |
242 | rgn_desc = obj_desc->common_field.region_obj; | |
44f6c012 | 243 | address = rgn_desc->region.address + |
4be44fcd | 244 | obj_desc->common_field.base_byte_offset + field_datum_byte_offset; |
1da177e4 LT |
245 | |
246 | if ((function & ACPI_IO_MASK) == ACPI_READ) { | |
4be44fcd LB |
247 | ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]")); |
248 | } else { | |
249 | ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]")); | |
1da177e4 LT |
250 | } |
251 | ||
4be44fcd LB |
252 | ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD, |
253 | " Region [%s:%X], Width %X, byte_base %X, Offset %X at %8.8X%8.8X\n", | |
254 | acpi_ut_get_region_name(rgn_desc->region. | |
255 | space_id), | |
256 | rgn_desc->region.space_id, | |
257 | obj_desc->common_field.access_byte_width, | |
258 | obj_desc->common_field.base_byte_offset, | |
259 | field_datum_byte_offset, | |
260 | ACPI_FORMAT_UINT64(address))); | |
1da177e4 LT |
261 | |
262 | /* Invoke the appropriate address_space/op_region handler */ | |
263 | ||
4be44fcd LB |
264 | status = acpi_ev_address_space_dispatch(rgn_desc, function, |
265 | address, | |
266 | ACPI_MUL_8(obj_desc-> | |
267 | common_field. | |
268 | access_byte_width), | |
269 | value); | |
1da177e4 | 270 | |
4be44fcd | 271 | if (ACPI_FAILURE(status)) { |
1da177e4 | 272 | if (status == AE_NOT_IMPLEMENTED) { |
4be44fcd LB |
273 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
274 | "Region %s(%X) not implemented\n", | |
275 | acpi_ut_get_region_name(rgn_desc-> | |
276 | region. | |
277 | space_id), | |
278 | rgn_desc->region.space_id)); | |
279 | } else if (status == AE_NOT_EXIST) { | |
280 | ACPI_REPORT_ERROR(("Region %s(%X) has no handler\n", | |
281 | acpi_ut_get_region_name(rgn_desc-> | |
282 | region. | |
283 | space_id), | |
284 | rgn_desc->region.space_id)); | |
1da177e4 LT |
285 | } |
286 | } | |
287 | ||
4be44fcd | 288 | return_ACPI_STATUS(status); |
1da177e4 LT |
289 | } |
290 | ||
1da177e4 LT |
291 | /******************************************************************************* |
292 | * | |
293 | * FUNCTION: acpi_ex_register_overflow | |
294 | * | |
44f6c012 | 295 | * PARAMETERS: obj_desc - Register(Field) to be written |
1da177e4 LT |
296 | * Value - Value to be stored |
297 | * | |
298 | * RETURN: TRUE if value overflows the field, FALSE otherwise | |
299 | * | |
300 | * DESCRIPTION: Check if a value is out of range of the field being written. | |
301 | * Used to check if the values written to Index and Bank registers | |
302 | * are out of range. Normally, the value is simply truncated | |
303 | * to fit the field, but this case is most likely a serious | |
304 | * coding error in the ASL. | |
305 | * | |
306 | ******************************************************************************/ | |
307 | ||
44f6c012 | 308 | static u8 |
4be44fcd LB |
309 | acpi_ex_register_overflow(union acpi_operand_object *obj_desc, |
310 | acpi_integer value) | |
1da177e4 LT |
311 | { |
312 | ||
313 | if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) { | |
314 | /* | |
315 | * The field is large enough to hold the maximum integer, so we can | |
316 | * never overflow it. | |
317 | */ | |
318 | return (FALSE); | |
319 | } | |
320 | ||
321 | if (value >= ((acpi_integer) 1 << obj_desc->common_field.bit_length)) { | |
322 | /* | |
323 | * The Value is larger than the maximum value that can fit into | |
324 | * the register. | |
325 | */ | |
326 | return (TRUE); | |
327 | } | |
328 | ||
329 | /* The Value will fit into the field with no truncation */ | |
330 | ||
331 | return (FALSE); | |
332 | } | |
333 | ||
1da177e4 LT |
334 | /******************************************************************************* |
335 | * | |
336 | * FUNCTION: acpi_ex_field_datum_io | |
337 | * | |
44f6c012 | 338 | * PARAMETERS: obj_desc - Field to be read |
1da177e4 LT |
339 | * field_datum_byte_offset - Byte offset of this datum within the |
340 | * parent field | |
44f6c012 | 341 | * Value - Where to store value (must be 64 bits) |
1da177e4 LT |
342 | * read_write - Read or Write flag |
343 | * | |
344 | * RETURN: Status | |
345 | * | |
346 | * DESCRIPTION: Read or Write a single datum of a field. The field_type is | |
347 | * demultiplexed here to handle the different types of fields | |
348 | * (buffer_field, region_field, index_field, bank_field) | |
349 | * | |
350 | ******************************************************************************/ | |
351 | ||
44f6c012 | 352 | static acpi_status |
4be44fcd LB |
353 | acpi_ex_field_datum_io(union acpi_operand_object *obj_desc, |
354 | u32 field_datum_byte_offset, | |
355 | acpi_integer * value, u32 read_write) | |
1da177e4 | 356 | { |
4be44fcd LB |
357 | acpi_status status; |
358 | acpi_integer local_value; | |
1da177e4 | 359 | |
4be44fcd | 360 | ACPI_FUNCTION_TRACE_U32("ex_field_datum_io", field_datum_byte_offset); |
1da177e4 LT |
361 | |
362 | if (read_write == ACPI_READ) { | |
363 | if (!value) { | |
364 | local_value = 0; | |
44f6c012 RM |
365 | |
366 | /* To support reads without saving return value */ | |
367 | value = &local_value; | |
1da177e4 LT |
368 | } |
369 | ||
370 | /* Clear the entire return buffer first, [Very Important!] */ | |
371 | ||
372 | *value = 0; | |
373 | } | |
374 | ||
375 | /* | |
376 | * The four types of fields are: | |
377 | * | |
378 | * buffer_field - Read/write from/to a Buffer | |
379 | * region_field - Read/write from/to a Operation Region. | |
44f6c012 RM |
380 | * bank_field - Write to a Bank Register, then read/write from/to an |
381 | * operation_region | |
382 | * index_field - Write to an Index Register, then read/write from/to a | |
383 | * Data Register | |
1da177e4 | 384 | */ |
4be44fcd | 385 | switch (ACPI_GET_OBJECT_TYPE(obj_desc)) { |
1da177e4 LT |
386 | case ACPI_TYPE_BUFFER_FIELD: |
387 | /* | |
388 | * If the buffer_field arguments have not been previously evaluated, | |
389 | * evaluate them now and save the results. | |
390 | */ | |
391 | if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) { | |
4be44fcd LB |
392 | status = acpi_ds_get_buffer_field_arguments(obj_desc); |
393 | if (ACPI_FAILURE(status)) { | |
394 | return_ACPI_STATUS(status); | |
1da177e4 LT |
395 | } |
396 | } | |
397 | ||
398 | if (read_write == ACPI_READ) { | |
399 | /* | |
400 | * Copy the data from the source buffer. | |
401 | * Length is the field width in bytes. | |
402 | */ | |
4be44fcd LB |
403 | ACPI_MEMCPY(value, |
404 | (obj_desc->buffer_field.buffer_obj)->buffer. | |
405 | pointer + | |
406 | obj_desc->buffer_field.base_byte_offset + | |
407 | field_datum_byte_offset, | |
408 | obj_desc->common_field.access_byte_width); | |
409 | } else { | |
1da177e4 LT |
410 | /* |
411 | * Copy the data to the target buffer. | |
412 | * Length is the field width in bytes. | |
413 | */ | |
4be44fcd LB |
414 | ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer. |
415 | pointer + | |
416 | obj_desc->buffer_field.base_byte_offset + | |
417 | field_datum_byte_offset, value, | |
418 | obj_desc->common_field.access_byte_width); | |
1da177e4 LT |
419 | } |
420 | ||
421 | status = AE_OK; | |
422 | break; | |
423 | ||
1da177e4 LT |
424 | case ACPI_TYPE_LOCAL_BANK_FIELD: |
425 | ||
44f6c012 RM |
426 | /* |
427 | * Ensure that the bank_value is not beyond the capacity of | |
428 | * the register | |
429 | */ | |
4be44fcd LB |
430 | if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj, |
431 | (acpi_integer) obj_desc-> | |
432 | bank_field.value)) { | |
433 | return_ACPI_STATUS(AE_AML_REGISTER_LIMIT); | |
1da177e4 LT |
434 | } |
435 | ||
436 | /* | |
437 | * For bank_fields, we must write the bank_value to the bank_register | |
438 | * (itself a region_field) before we can access the data. | |
439 | */ | |
4be44fcd LB |
440 | status = |
441 | acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj, | |
442 | &obj_desc->bank_field.value, | |
443 | sizeof(obj_desc->bank_field. | |
444 | value)); | |
445 | if (ACPI_FAILURE(status)) { | |
446 | return_ACPI_STATUS(status); | |
1da177e4 LT |
447 | } |
448 | ||
449 | /* | |
450 | * Now that the Bank has been selected, fall through to the | |
451 | * region_field case and write the datum to the Operation Region | |
452 | */ | |
453 | ||
454 | /*lint -fallthrough */ | |
455 | ||
1da177e4 LT |
456 | case ACPI_TYPE_LOCAL_REGION_FIELD: |
457 | /* | |
458 | * For simple region_fields, we just directly access the owning | |
459 | * Operation Region. | |
460 | */ | |
4be44fcd LB |
461 | status = |
462 | acpi_ex_access_region(obj_desc, field_datum_byte_offset, | |
463 | value, read_write); | |
1da177e4 LT |
464 | break; |
465 | ||
1da177e4 LT |
466 | case ACPI_TYPE_LOCAL_INDEX_FIELD: |
467 | ||
44f6c012 RM |
468 | /* |
469 | * Ensure that the index_value is not beyond the capacity of | |
470 | * the register | |
471 | */ | |
4be44fcd LB |
472 | if (acpi_ex_register_overflow(obj_desc->index_field.index_obj, |
473 | (acpi_integer) obj_desc-> | |
474 | index_field.value)) { | |
475 | return_ACPI_STATUS(AE_AML_REGISTER_LIMIT); | |
1da177e4 LT |
476 | } |
477 | ||
478 | /* Write the index value to the index_register (itself a region_field) */ | |
479 | ||
480 | field_datum_byte_offset += obj_desc->index_field.value; | |
481 | ||
4be44fcd LB |
482 | ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, |
483 | "Write to Index Register: Value %8.8X\n", | |
484 | field_datum_byte_offset)); | |
1da177e4 | 485 | |
4be44fcd LB |
486 | status = |
487 | acpi_ex_insert_into_field(obj_desc->index_field.index_obj, | |
488 | &field_datum_byte_offset, | |
489 | sizeof(field_datum_byte_offset)); | |
490 | if (ACPI_FAILURE(status)) { | |
491 | return_ACPI_STATUS(status); | |
1da177e4 LT |
492 | } |
493 | ||
4be44fcd LB |
494 | ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, |
495 | "I/O to Data Register: value_ptr %p\n", | |
496 | value)); | |
1da177e4 LT |
497 | |
498 | if (read_write == ACPI_READ) { | |
499 | /* Read the datum from the data_register */ | |
500 | ||
4be44fcd LB |
501 | status = |
502 | acpi_ex_extract_from_field(obj_desc->index_field. | |
503 | data_obj, value, | |
504 | sizeof(acpi_integer)); | |
505 | } else { | |
1da177e4 LT |
506 | /* Write the datum to the data_register */ |
507 | ||
4be44fcd LB |
508 | status = |
509 | acpi_ex_insert_into_field(obj_desc->index_field. | |
510 | data_obj, value, | |
511 | sizeof(acpi_integer)); | |
1da177e4 LT |
512 | } |
513 | break; | |
514 | ||
1da177e4 LT |
515 | default: |
516 | ||
4be44fcd LB |
517 | ACPI_REPORT_ERROR(("Wrong object type in field I/O %X\n", |
518 | ACPI_GET_OBJECT_TYPE(obj_desc))); | |
1da177e4 LT |
519 | status = AE_AML_INTERNAL; |
520 | break; | |
521 | } | |
522 | ||
4be44fcd | 523 | if (ACPI_SUCCESS(status)) { |
1da177e4 | 524 | if (read_write == ACPI_READ) { |
4be44fcd LB |
525 | ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, |
526 | "Value Read %8.8X%8.8X, Width %d\n", | |
527 | ACPI_FORMAT_UINT64(*value), | |
528 | obj_desc->common_field. | |
529 | access_byte_width)); | |
530 | } else { | |
531 | ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, | |
532 | "Value Written %8.8X%8.8X, Width %d\n", | |
533 | ACPI_FORMAT_UINT64(*value), | |
534 | obj_desc->common_field. | |
535 | access_byte_width)); | |
1da177e4 LT |
536 | } |
537 | } | |
538 | ||
4be44fcd | 539 | return_ACPI_STATUS(status); |
1da177e4 LT |
540 | } |
541 | ||
1da177e4 LT |
542 | /******************************************************************************* |
543 | * | |
544 | * FUNCTION: acpi_ex_write_with_update_rule | |
545 | * | |
44f6c012 RM |
546 | * PARAMETERS: obj_desc - Field to be written |
547 | * Mask - bitmask within field datum | |
548 | * field_value - Value to write | |
549 | * field_datum_byte_offset - Offset of datum within field | |
1da177e4 LT |
550 | * |
551 | * RETURN: Status | |
552 | * | |
553 | * DESCRIPTION: Apply the field update rule to a field write | |
554 | * | |
555 | ******************************************************************************/ | |
556 | ||
557 | acpi_status | |
4be44fcd LB |
558 | acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc, |
559 | acpi_integer mask, | |
560 | acpi_integer field_value, | |
561 | u32 field_datum_byte_offset) | |
1da177e4 | 562 | { |
4be44fcd LB |
563 | acpi_status status = AE_OK; |
564 | acpi_integer merged_value; | |
565 | acpi_integer current_value; | |
1da177e4 | 566 | |
4be44fcd | 567 | ACPI_FUNCTION_TRACE_U32("ex_write_with_update_rule", mask); |
1da177e4 LT |
568 | |
569 | /* Start with the new bits */ | |
570 | ||
571 | merged_value = field_value; | |
572 | ||
573 | /* If the mask is all ones, we don't need to worry about the update rule */ | |
574 | ||
575 | if (mask != ACPI_INTEGER_MAX) { | |
576 | /* Decode the update rule */ | |
577 | ||
4be44fcd LB |
578 | switch (obj_desc->common_field. |
579 | field_flags & AML_FIELD_UPDATE_RULE_MASK) { | |
1da177e4 LT |
580 | case AML_FIELD_UPDATE_PRESERVE: |
581 | /* | |
582 | * Check if update rule needs to be applied (not if mask is all | |
583 | * ones) The left shift drops the bits we want to ignore. | |
584 | */ | |
4be44fcd LB |
585 | if ((~mask << (ACPI_MUL_8(sizeof(mask)) - |
586 | ACPI_MUL_8(obj_desc->common_field. | |
587 | access_byte_width))) != 0) { | |
1da177e4 LT |
588 | /* |
589 | * Read the current contents of the byte/word/dword containing | |
590 | * the field, and merge with the new field value. | |
591 | */ | |
4be44fcd LB |
592 | status = |
593 | acpi_ex_field_datum_io(obj_desc, | |
594 | field_datum_byte_offset, | |
595 | ¤t_value, | |
596 | ACPI_READ); | |
597 | if (ACPI_FAILURE(status)) { | |
598 | return_ACPI_STATUS(status); | |
1da177e4 LT |
599 | } |
600 | ||
601 | merged_value |= (current_value & ~mask); | |
602 | } | |
603 | break; | |
604 | ||
605 | case AML_FIELD_UPDATE_WRITE_AS_ONES: | |
606 | ||
607 | /* Set positions outside the field to all ones */ | |
608 | ||
609 | merged_value |= ~mask; | |
610 | break; | |
611 | ||
612 | case AML_FIELD_UPDATE_WRITE_AS_ZEROS: | |
613 | ||
614 | /* Set positions outside the field to all zeros */ | |
615 | ||
616 | merged_value &= mask; | |
617 | break; | |
618 | ||
619 | default: | |
620 | ||
4be44fcd LB |
621 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, |
622 | "write_with_update_rule: Unknown update_rule setting: %X\n", | |
623 | (obj_desc->common_field. | |
624 | field_flags & | |
625 | AML_FIELD_UPDATE_RULE_MASK))); | |
626 | return_ACPI_STATUS(AE_AML_OPERAND_VALUE); | |
1da177e4 LT |
627 | } |
628 | } | |
629 | ||
4be44fcd LB |
630 | ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, |
631 | "Mask %8.8X%8.8X, datum_offset %X, Width %X, Value %8.8X%8.8X, merged_value %8.8X%8.8X\n", | |
632 | ACPI_FORMAT_UINT64(mask), | |
633 | field_datum_byte_offset, | |
634 | obj_desc->common_field.access_byte_width, | |
635 | ACPI_FORMAT_UINT64(field_value), | |
636 | ACPI_FORMAT_UINT64(merged_value))); | |
1da177e4 LT |
637 | |
638 | /* Write the merged value */ | |
639 | ||
4be44fcd LB |
640 | status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset, |
641 | &merged_value, ACPI_WRITE); | |
1da177e4 | 642 | |
4be44fcd | 643 | return_ACPI_STATUS(status); |
1da177e4 LT |
644 | } |
645 | ||
1da177e4 LT |
646 | /******************************************************************************* |
647 | * | |
648 | * FUNCTION: acpi_ex_extract_from_field | |
649 | * | |
650 | * PARAMETERS: obj_desc - Field to be read | |
651 | * Buffer - Where to store the field data | |
652 | * buffer_length - Length of Buffer | |
653 | * | |
654 | * RETURN: Status | |
655 | * | |
656 | * DESCRIPTION: Retrieve the current value of the given field | |
657 | * | |
658 | ******************************************************************************/ | |
659 | ||
660 | acpi_status | |
4be44fcd LB |
661 | acpi_ex_extract_from_field(union acpi_operand_object *obj_desc, |
662 | void *buffer, u32 buffer_length) | |
1da177e4 | 663 | { |
4be44fcd LB |
664 | acpi_status status; |
665 | acpi_integer raw_datum; | |
666 | acpi_integer merged_datum; | |
667 | u32 field_offset = 0; | |
668 | u32 buffer_offset = 0; | |
669 | u32 buffer_tail_bits; | |
670 | u32 datum_count; | |
671 | u32 field_datum_count; | |
672 | u32 i; | |
673 | ||
674 | ACPI_FUNCTION_TRACE("ex_extract_from_field"); | |
1da177e4 LT |
675 | |
676 | /* Validate target buffer and clear it */ | |
677 | ||
4be44fcd LB |
678 | if (buffer_length < |
679 | ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) { | |
680 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, | |
681 | "Field size %X (bits) is too large for buffer (%X)\n", | |
682 | obj_desc->common_field.bit_length, | |
683 | buffer_length)); | |
1da177e4 | 684 | |
4be44fcd | 685 | return_ACPI_STATUS(AE_BUFFER_OVERFLOW); |
1da177e4 | 686 | } |
4be44fcd | 687 | ACPI_MEMSET(buffer, 0, buffer_length); |
1da177e4 LT |
688 | |
689 | /* Compute the number of datums (access width data items) */ | |
690 | ||
4be44fcd LB |
691 | datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length, |
692 | obj_desc->common_field.access_bit_width); | |
693 | field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length + | |
694 | obj_desc->common_field. | |
695 | start_field_bit_offset, | |
696 | obj_desc->common_field. | |
697 | access_bit_width); | |
1da177e4 LT |
698 | |
699 | /* Priming read from the field */ | |
700 | ||
4be44fcd LB |
701 | status = |
702 | acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum, | |
703 | ACPI_READ); | |
704 | if (ACPI_FAILURE(status)) { | |
705 | return_ACPI_STATUS(status); | |
1da177e4 | 706 | } |
4be44fcd LB |
707 | merged_datum = |
708 | raw_datum >> obj_desc->common_field.start_field_bit_offset; | |
1da177e4 LT |
709 | |
710 | /* Read the rest of the field */ | |
711 | ||
712 | for (i = 1; i < field_datum_count; i++) { | |
713 | /* Get next input datum from the field */ | |
714 | ||
715 | field_offset += obj_desc->common_field.access_byte_width; | |
4be44fcd LB |
716 | status = acpi_ex_field_datum_io(obj_desc, field_offset, |
717 | &raw_datum, ACPI_READ); | |
718 | if (ACPI_FAILURE(status)) { | |
719 | return_ACPI_STATUS(status); | |
1da177e4 LT |
720 | } |
721 | ||
722 | /* Merge with previous datum if necessary */ | |
723 | ||
724 | merged_datum |= raw_datum << | |
4be44fcd LB |
725 | (obj_desc->common_field.access_bit_width - |
726 | obj_desc->common_field.start_field_bit_offset); | |
1da177e4 LT |
727 | |
728 | if (i == datum_count) { | |
729 | break; | |
730 | } | |
731 | ||
732 | /* Write merged datum to target buffer */ | |
733 | ||
4be44fcd LB |
734 | ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum, |
735 | ACPI_MIN(obj_desc->common_field.access_byte_width, | |
736 | buffer_length - buffer_offset)); | |
1da177e4 LT |
737 | |
738 | buffer_offset += obj_desc->common_field.access_byte_width; | |
4be44fcd LB |
739 | merged_datum = |
740 | raw_datum >> obj_desc->common_field.start_field_bit_offset; | |
1da177e4 LT |
741 | } |
742 | ||
743 | /* Mask off any extra bits in the last datum */ | |
744 | ||
44f6c012 | 745 | buffer_tail_bits = obj_desc->common_field.bit_length % |
4be44fcd | 746 | obj_desc->common_field.access_bit_width; |
1da177e4 | 747 | if (buffer_tail_bits) { |
4be44fcd | 748 | merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits); |
1da177e4 LT |
749 | } |
750 | ||
751 | /* Write the last datum to the buffer */ | |
752 | ||
4be44fcd LB |
753 | ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum, |
754 | ACPI_MIN(obj_desc->common_field.access_byte_width, | |
755 | buffer_length - buffer_offset)); | |
1da177e4 | 756 | |
4be44fcd | 757 | return_ACPI_STATUS(AE_OK); |
1da177e4 LT |
758 | } |
759 | ||
1da177e4 LT |
760 | /******************************************************************************* |
761 | * | |
762 | * FUNCTION: acpi_ex_insert_into_field | |
763 | * | |
764 | * PARAMETERS: obj_desc - Field to be written | |
765 | * Buffer - Data to be written | |
766 | * buffer_length - Length of Buffer | |
767 | * | |
768 | * RETURN: Status | |
769 | * | |
770 | * DESCRIPTION: Store the Buffer contents into the given field | |
771 | * | |
772 | ******************************************************************************/ | |
773 | ||
774 | acpi_status | |
4be44fcd LB |
775 | acpi_ex_insert_into_field(union acpi_operand_object *obj_desc, |
776 | void *buffer, u32 buffer_length) | |
1da177e4 | 777 | { |
4be44fcd LB |
778 | acpi_status status; |
779 | acpi_integer mask; | |
780 | acpi_integer merged_datum; | |
781 | acpi_integer raw_datum = 0; | |
782 | u32 field_offset = 0; | |
783 | u32 buffer_offset = 0; | |
784 | u32 buffer_tail_bits; | |
785 | u32 datum_count; | |
786 | u32 field_datum_count; | |
787 | u32 i; | |
788 | ||
789 | ACPI_FUNCTION_TRACE("ex_insert_into_field"); | |
1da177e4 LT |
790 | |
791 | /* Validate input buffer */ | |
792 | ||
4be44fcd LB |
793 | if (buffer_length < |
794 | ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) { | |
795 | ACPI_DEBUG_PRINT((ACPI_DB_ERROR, | |
796 | "Field size %X (bits) is too large for buffer (%X)\n", | |
797 | obj_desc->common_field.bit_length, | |
798 | buffer_length)); | |
1da177e4 | 799 | |
4be44fcd | 800 | return_ACPI_STATUS(AE_BUFFER_OVERFLOW); |
1da177e4 LT |
801 | } |
802 | ||
803 | /* Compute the number of datums (access width data items) */ | |
804 | ||
4be44fcd LB |
805 | mask = |
806 | ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset); | |
807 | datum_count = | |
808 | ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length, | |
809 | obj_desc->common_field.access_bit_width); | |
810 | field_datum_count = | |
811 | ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length + | |
812 | obj_desc->common_field.start_field_bit_offset, | |
813 | obj_desc->common_field.access_bit_width); | |
1da177e4 LT |
814 | |
815 | /* Get initial Datum from the input buffer */ | |
816 | ||
4be44fcd LB |
817 | ACPI_MEMCPY(&raw_datum, buffer, |
818 | ACPI_MIN(obj_desc->common_field.access_byte_width, | |
819 | buffer_length - buffer_offset)); | |
1da177e4 | 820 | |
4be44fcd LB |
821 | merged_datum = |
822 | raw_datum << obj_desc->common_field.start_field_bit_offset; | |
1da177e4 LT |
823 | |
824 | /* Write the entire field */ | |
825 | ||
826 | for (i = 1; i < field_datum_count; i++) { | |
827 | /* Write merged datum to the target field */ | |
828 | ||
829 | merged_datum &= mask; | |
4be44fcd LB |
830 | status = acpi_ex_write_with_update_rule(obj_desc, mask, |
831 | merged_datum, | |
832 | field_offset); | |
833 | if (ACPI_FAILURE(status)) { | |
834 | return_ACPI_STATUS(status); | |
1da177e4 LT |
835 | } |
836 | ||
837 | /* Start new output datum by merging with previous input datum */ | |
838 | ||
839 | field_offset += obj_desc->common_field.access_byte_width; | |
840 | merged_datum = raw_datum >> | |
4be44fcd LB |
841 | (obj_desc->common_field.access_bit_width - |
842 | obj_desc->common_field.start_field_bit_offset); | |
1da177e4 LT |
843 | mask = ACPI_INTEGER_MAX; |
844 | ||
845 | if (i == datum_count) { | |
846 | break; | |
847 | } | |
848 | ||
849 | /* Get the next input datum from the buffer */ | |
850 | ||
851 | buffer_offset += obj_desc->common_field.access_byte_width; | |
4be44fcd LB |
852 | ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset, |
853 | ACPI_MIN(obj_desc->common_field.access_byte_width, | |
854 | buffer_length - buffer_offset)); | |
855 | merged_datum |= | |
856 | raw_datum << obj_desc->common_field.start_field_bit_offset; | |
1da177e4 LT |
857 | } |
858 | ||
859 | /* Mask off any extra bits in the last datum */ | |
860 | ||
861 | buffer_tail_bits = (obj_desc->common_field.bit_length + | |
4be44fcd LB |
862 | obj_desc->common_field.start_field_bit_offset) % |
863 | obj_desc->common_field.access_bit_width; | |
1da177e4 | 864 | if (buffer_tail_bits) { |
4be44fcd | 865 | mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits); |
1da177e4 LT |
866 | } |
867 | ||
868 | /* Write the last datum to the field */ | |
869 | ||
870 | merged_datum &= mask; | |
4be44fcd LB |
871 | status = acpi_ex_write_with_update_rule(obj_desc, |
872 | mask, merged_datum, | |
873 | field_offset); | |
1da177e4 | 874 | |
4be44fcd | 875 | return_ACPI_STATUS(status); |
1da177e4 | 876 | } |