blob: 9fe27fd04a2ff76286a94dffc97dadb525415a65 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/******************************************************************************
2 *
3 * Module Name: exfldio - Aml Field I/O
4 *
5 *****************************************************************************/
6
7/*
Bob Moore4a90c7e2006-01-13 16:22:00 -05008 * Copyright (C) 2000 - 2006, R. Byron Moore
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * 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
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#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
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_EXECUTER
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("exfldio")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Robert Moore44f6c012005-04-18 22:49:35 -040053/* Local prototypes */
Robert Moore44f6c012005-04-18 22:49:35 -040054static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040055acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
56 u32 field_datum_byte_offset,
57 acpi_integer * value, u32 read_write);
Robert Moore44f6c012005-04-18 22:49:35 -040058
59static u8
Len Brown4be44fc2005-08-05 00:44:28 -040060acpi_ex_register_overflow(union acpi_operand_object *obj_desc,
61 acpi_integer value);
Robert Moore44f6c012005-04-18 22:49:35 -040062
63static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040064acpi_ex_setup_region(union acpi_operand_object *obj_desc,
65 u32 field_datum_byte_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67/*******************************************************************************
68 *
69 * FUNCTION: acpi_ex_setup_region
70 *
Robert Moore44f6c012005-04-18 22:49:35 -040071 * PARAMETERS: obj_desc - Field to be read or written
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 * 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
Robert Moore44f6c012005-04-18 22:49:35 -040083static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -040084acpi_ex_setup_region(union acpi_operand_object *obj_desc,
85 u32 field_datum_byte_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Len Brown4be44fc2005-08-05 00:44:28 -040087 acpi_status status = AE_OK;
88 union acpi_operand_object *rgn_desc;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Len Brown4be44fc2005-08-05 00:44:28 -040090 ACPI_FUNCTION_TRACE_U32("ex_setup_region", field_datum_byte_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 rgn_desc = obj_desc->common_field.region_obj;
93
94 /* We must have a valid region */
95
Len Brown4be44fc2005-08-05 00:44:28 -040096 if (ACPI_GET_OBJECT_TYPE(rgn_desc) != ACPI_TYPE_REGION) {
Bob Moore4a90c7e2006-01-13 16:22:00 -050097 ACPI_REPORT_ERROR(("Needed Region, found type %X (%s)\n",
98 ACPI_GET_OBJECT_TYPE(rgn_desc),
99 acpi_ut_get_object_type_name(rgn_desc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Len Brown4be44fc2005-08-05 00:44:28 -0400101 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 }
103
104 /*
105 * If the Region Address and Length have not been previously evaluated,
106 * evaluate them now and save the results.
107 */
108 if (!(rgn_desc->common.flags & AOPOBJ_DATA_VALID)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400109 status = acpi_ds_get_region_arguments(rgn_desc);
110 if (ACPI_FAILURE(status)) {
111 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113 }
114
115 if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) {
116 /* SMBus has a non-linear address space */
117
Len Brown4be44fc2005-08-05 00:44:28 -0400118 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700119 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120#ifdef ACPI_UNDER_DEVELOPMENT
121 /*
122 * If the Field access is any_acc, we can now compute the optimal
123 * access (because we know know the length of the parent region)
124 */
125 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400126 if (ACPI_FAILURE(status)) {
127 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700128 }
129 }
130#endif
131
132 /*
133 * Validate the request. The entire request from the byte offset for a
134 * length of one field datum (access width) must fit within the region.
135 * (Region length is specified in bytes)
136 */
Robert Moore44f6c012005-04-18 22:49:35 -0400137 if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset +
Len Brown4be44fc2005-08-05 00:44:28 -0400138 field_datum_byte_offset +
139 obj_desc->common_field.
140 access_byte_width)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (acpi_gbl_enable_interpreter_slack) {
142 /*
143 * Slack mode only: We will go ahead and allow access to this
144 * field if it is within the region length rounded up to the next
145 * access width boundary.
146 */
Len Brown4be44fc2005-08-05 00:44:28 -0400147 if (ACPI_ROUND_UP(rgn_desc->region.length,
148 obj_desc->common_field.
149 access_byte_width) >=
150 (obj_desc->common_field.base_byte_offset +
151 (acpi_native_uint) obj_desc->common_field.
152 access_byte_width + field_datum_byte_offset)) {
153 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 }
155 }
156
Len Brown4be44fc2005-08-05 00:44:28 -0400157 if (rgn_desc->region.length <
158 obj_desc->common_field.access_byte_width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 /*
160 * This is the case where the access_type (acc_word, etc.) is wider
161 * than the region itself. For example, a region of length one
162 * byte, and a field with Dword access specified.
163 */
Bob Moore4a90c7e2006-01-13 16:22:00 -0500164 ACPI_REPORT_ERROR(("Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n", acpi_ut_get_node_name(obj_desc->common_field.node), obj_desc->common_field.access_byte_width, acpi_ut_get_node_name(rgn_desc->region.node), rgn_desc->region.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 }
166
167 /*
168 * Offset rounded up to next multiple of field width
169 * exceeds region length, indicate an error
170 */
Bob Moore4a90c7e2006-01-13 16:22:00 -0500171 ACPI_REPORT_ERROR(("Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n", acpi_ut_get_node_name(obj_desc->common_field.node), obj_desc->common_field.base_byte_offset, field_datum_byte_offset, obj_desc->common_field.access_byte_width, acpi_ut_get_node_name(rgn_desc->region.node), rgn_desc->region.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Len Brown4be44fc2005-08-05 00:44:28 -0400173 return_ACPI_STATUS(AE_AML_REGION_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 }
175
Len Brown4be44fc2005-08-05 00:44:28 -0400176 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179/*******************************************************************************
180 *
181 * FUNCTION: acpi_ex_access_region
182 *
Robert Moore44f6c012005-04-18 22:49:35 -0400183 * PARAMETERS: obj_desc - Field to be read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 * field_datum_byte_offset - Byte offset of this datum within the
185 * parent field
Robert Moore44f6c012005-04-18 22:49:35 -0400186 * Value - Where to store value (must at least
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187 * the size of acpi_integer)
188 * Function - Read or Write flag plus other region-
189 * dependent flags
190 *
191 * RETURN: Status
192 *
193 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
194 *
195 ******************************************************************************/
196
197acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400198acpi_ex_access_region(union acpi_operand_object *obj_desc,
199 u32 field_datum_byte_offset,
200 acpi_integer * value, u32 function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201{
Len Brown4be44fc2005-08-05 00:44:28 -0400202 acpi_status status;
203 union acpi_operand_object *rgn_desc;
204 acpi_physical_address address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205
Len Brown4be44fc2005-08-05 00:44:28 -0400206 ACPI_FUNCTION_TRACE("ex_access_region");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207
208 /*
209 * Ensure that the region operands are fully evaluated and verify
210 * the validity of the request
211 */
Len Brown4be44fc2005-08-05 00:44:28 -0400212 status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
213 if (ACPI_FAILURE(status)) {
214 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215 }
216
217 /*
218 * The physical address of this field datum is:
219 *
220 * 1) The base of the region, plus
221 * 2) The base offset of the field, plus
222 * 3) The current offset into the field
223 */
224 rgn_desc = obj_desc->common_field.region_obj;
Robert Moore44f6c012005-04-18 22:49:35 -0400225 address = rgn_desc->region.address +
Len Brown4be44fc2005-08-05 00:44:28 -0400226 obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700227
228 if ((function & ACPI_IO_MASK) == ACPI_READ) {
Len Brown4be44fc2005-08-05 00:44:28 -0400229 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
230 } else {
231 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Len Brown4be44fc2005-08-05 00:44:28 -0400234 ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
235 " Region [%s:%X], Width %X, byte_base %X, Offset %X at %8.8X%8.8X\n",
236 acpi_ut_get_region_name(rgn_desc->region.
237 space_id),
238 rgn_desc->region.space_id,
239 obj_desc->common_field.access_byte_width,
240 obj_desc->common_field.base_byte_offset,
241 field_datum_byte_offset,
242 ACPI_FORMAT_UINT64(address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243
244 /* Invoke the appropriate address_space/op_region handler */
245
Len Brown4be44fc2005-08-05 00:44:28 -0400246 status = acpi_ev_address_space_dispatch(rgn_desc, function,
247 address,
248 ACPI_MUL_8(obj_desc->
249 common_field.
250 access_byte_width),
251 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
Len Brown4be44fc2005-08-05 00:44:28 -0400253 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254 if (status == AE_NOT_IMPLEMENTED) {
Bob Moore4a90c7e2006-01-13 16:22:00 -0500255 ACPI_REPORT_ERROR(("Region %s(%X) not implemented\n",
256 acpi_ut_get_region_name(rgn_desc->
257 region.
258 space_id),
259 rgn_desc->region.space_id));
Len Brown4be44fc2005-08-05 00:44:28 -0400260 } else if (status == AE_NOT_EXIST) {
261 ACPI_REPORT_ERROR(("Region %s(%X) has no handler\n",
262 acpi_ut_get_region_name(rgn_desc->
263 region.
264 space_id),
265 rgn_desc->region.space_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266 }
267 }
268
Len Brown4be44fc2005-08-05 00:44:28 -0400269 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700270}
271
Linus Torvalds1da177e2005-04-16 15:20:36 -0700272/*******************************************************************************
273 *
274 * FUNCTION: acpi_ex_register_overflow
275 *
Robert Moore44f6c012005-04-18 22:49:35 -0400276 * PARAMETERS: obj_desc - Register(Field) to be written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700277 * Value - Value to be stored
278 *
279 * RETURN: TRUE if value overflows the field, FALSE otherwise
280 *
281 * DESCRIPTION: Check if a value is out of range of the field being written.
282 * Used to check if the values written to Index and Bank registers
283 * are out of range. Normally, the value is simply truncated
284 * to fit the field, but this case is most likely a serious
285 * coding error in the ASL.
286 *
287 ******************************************************************************/
288
Robert Moore44f6c012005-04-18 22:49:35 -0400289static u8
Len Brown4be44fc2005-08-05 00:44:28 -0400290acpi_ex_register_overflow(union acpi_operand_object *obj_desc,
291 acpi_integer value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292{
293
294 if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
295 /*
296 * The field is large enough to hold the maximum integer, so we can
297 * never overflow it.
298 */
299 return (FALSE);
300 }
301
302 if (value >= ((acpi_integer) 1 << obj_desc->common_field.bit_length)) {
303 /*
304 * The Value is larger than the maximum value that can fit into
305 * the register.
306 */
307 return (TRUE);
308 }
309
310 /* The Value will fit into the field with no truncation */
311
312 return (FALSE);
313}
314
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315/*******************************************************************************
316 *
317 * FUNCTION: acpi_ex_field_datum_io
318 *
Robert Moore44f6c012005-04-18 22:49:35 -0400319 * PARAMETERS: obj_desc - Field to be read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 * field_datum_byte_offset - Byte offset of this datum within the
321 * parent field
Robert Moore44f6c012005-04-18 22:49:35 -0400322 * Value - Where to store value (must be 64 bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 * read_write - Read or Write flag
324 *
325 * RETURN: Status
326 *
327 * DESCRIPTION: Read or Write a single datum of a field. The field_type is
328 * demultiplexed here to handle the different types of fields
329 * (buffer_field, region_field, index_field, bank_field)
330 *
331 ******************************************************************************/
332
Robert Moore44f6c012005-04-18 22:49:35 -0400333static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400334acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
335 u32 field_datum_byte_offset,
336 acpi_integer * value, u32 read_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337{
Len Brown4be44fc2005-08-05 00:44:28 -0400338 acpi_status status;
339 acpi_integer local_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700340
Len Brown4be44fc2005-08-05 00:44:28 -0400341 ACPI_FUNCTION_TRACE_U32("ex_field_datum_io", field_datum_byte_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700342
343 if (read_write == ACPI_READ) {
344 if (!value) {
345 local_value = 0;
Robert Moore44f6c012005-04-18 22:49:35 -0400346
347 /* To support reads without saving return value */
348 value = &local_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 }
350
351 /* Clear the entire return buffer first, [Very Important!] */
352
353 *value = 0;
354 }
355
356 /*
357 * The four types of fields are:
358 *
359 * buffer_field - Read/write from/to a Buffer
360 * region_field - Read/write from/to a Operation Region.
Robert Moore44f6c012005-04-18 22:49:35 -0400361 * bank_field - Write to a Bank Register, then read/write from/to an
362 * operation_region
363 * index_field - Write to an Index Register, then read/write from/to a
364 * Data Register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700365 */
Len Brown4be44fc2005-08-05 00:44:28 -0400366 switch (ACPI_GET_OBJECT_TYPE(obj_desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 case ACPI_TYPE_BUFFER_FIELD:
368 /*
369 * If the buffer_field arguments have not been previously evaluated,
370 * evaluate them now and save the results.
371 */
372 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400373 status = acpi_ds_get_buffer_field_arguments(obj_desc);
374 if (ACPI_FAILURE(status)) {
375 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 }
377 }
378
379 if (read_write == ACPI_READ) {
380 /*
381 * Copy the data from the source buffer.
382 * Length is the field width in bytes.
383 */
Len Brown4be44fc2005-08-05 00:44:28 -0400384 ACPI_MEMCPY(value,
385 (obj_desc->buffer_field.buffer_obj)->buffer.
386 pointer +
387 obj_desc->buffer_field.base_byte_offset +
388 field_datum_byte_offset,
389 obj_desc->common_field.access_byte_width);
390 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 /*
392 * Copy the data to the target buffer.
393 * Length is the field width in bytes.
394 */
Len Brown4be44fc2005-08-05 00:44:28 -0400395 ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
396 pointer +
397 obj_desc->buffer_field.base_byte_offset +
398 field_datum_byte_offset, value,
399 obj_desc->common_field.access_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400 }
401
402 status = AE_OK;
403 break;
404
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 case ACPI_TYPE_LOCAL_BANK_FIELD:
406
Robert Moore44f6c012005-04-18 22:49:35 -0400407 /*
408 * Ensure that the bank_value is not beyond the capacity of
409 * the register
410 */
Len Brown4be44fc2005-08-05 00:44:28 -0400411 if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
412 (acpi_integer) obj_desc->
413 bank_field.value)) {
414 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 /*
418 * For bank_fields, we must write the bank_value to the bank_register
419 * (itself a region_field) before we can access the data.
420 */
Len Brown4be44fc2005-08-05 00:44:28 -0400421 status =
422 acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
423 &obj_desc->bank_field.value,
424 sizeof(obj_desc->bank_field.
425 value));
426 if (ACPI_FAILURE(status)) {
427 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700428 }
429
430 /*
431 * Now that the Bank has been selected, fall through to the
432 * region_field case and write the datum to the Operation Region
433 */
434
435 /*lint -fallthrough */
436
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437 case ACPI_TYPE_LOCAL_REGION_FIELD:
438 /*
439 * For simple region_fields, we just directly access the owning
440 * Operation Region.
441 */
Len Brown4be44fc2005-08-05 00:44:28 -0400442 status =
443 acpi_ex_access_region(obj_desc, field_datum_byte_offset,
444 value, read_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700445 break;
446
Linus Torvalds1da177e2005-04-16 15:20:36 -0700447 case ACPI_TYPE_LOCAL_INDEX_FIELD:
448
Robert Moore44f6c012005-04-18 22:49:35 -0400449 /*
450 * Ensure that the index_value is not beyond the capacity of
451 * the register
452 */
Len Brown4be44fc2005-08-05 00:44:28 -0400453 if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
454 (acpi_integer) obj_desc->
455 index_field.value)) {
456 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700457 }
458
459 /* Write the index value to the index_register (itself a region_field) */
460
461 field_datum_byte_offset += obj_desc->index_field.value;
462
Len Brown4be44fc2005-08-05 00:44:28 -0400463 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
464 "Write to Index Register: Value %8.8X\n",
465 field_datum_byte_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Len Brown4be44fc2005-08-05 00:44:28 -0400467 status =
468 acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
469 &field_datum_byte_offset,
470 sizeof(field_datum_byte_offset));
471 if (ACPI_FAILURE(status)) {
472 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700473 }
474
Len Brown4be44fc2005-08-05 00:44:28 -0400475 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
476 "I/O to Data Register: value_ptr %p\n",
477 value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700478
479 if (read_write == ACPI_READ) {
480 /* Read the datum from the data_register */
481
Len Brown4be44fc2005-08-05 00:44:28 -0400482 status =
483 acpi_ex_extract_from_field(obj_desc->index_field.
484 data_obj, value,
485 sizeof(acpi_integer));
486 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 /* Write the datum to the data_register */
488
Len Brown4be44fc2005-08-05 00:44:28 -0400489 status =
490 acpi_ex_insert_into_field(obj_desc->index_field.
491 data_obj, value,
492 sizeof(acpi_integer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 }
494 break;
495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 default:
497
Len Brown4be44fc2005-08-05 00:44:28 -0400498 ACPI_REPORT_ERROR(("Wrong object type in field I/O %X\n",
499 ACPI_GET_OBJECT_TYPE(obj_desc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 status = AE_AML_INTERNAL;
501 break;
502 }
503
Len Brown4be44fc2005-08-05 00:44:28 -0400504 if (ACPI_SUCCESS(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505 if (read_write == ACPI_READ) {
Len Brown4be44fc2005-08-05 00:44:28 -0400506 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
507 "Value Read %8.8X%8.8X, Width %d\n",
508 ACPI_FORMAT_UINT64(*value),
509 obj_desc->common_field.
510 access_byte_width));
511 } else {
512 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
513 "Value Written %8.8X%8.8X, Width %d\n",
514 ACPI_FORMAT_UINT64(*value),
515 obj_desc->common_field.
516 access_byte_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 }
518 }
519
Len Brown4be44fc2005-08-05 00:44:28 -0400520 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523/*******************************************************************************
524 *
525 * FUNCTION: acpi_ex_write_with_update_rule
526 *
Robert Moore44f6c012005-04-18 22:49:35 -0400527 * PARAMETERS: obj_desc - Field to be written
528 * Mask - bitmask within field datum
529 * field_value - Value to write
530 * field_datum_byte_offset - Offset of datum within field
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 *
532 * RETURN: Status
533 *
534 * DESCRIPTION: Apply the field update rule to a field write
535 *
536 ******************************************************************************/
537
538acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400539acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
540 acpi_integer mask,
541 acpi_integer field_value,
542 u32 field_datum_byte_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700543{
Len Brown4be44fc2005-08-05 00:44:28 -0400544 acpi_status status = AE_OK;
545 acpi_integer merged_value;
546 acpi_integer current_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547
Len Brown4be44fc2005-08-05 00:44:28 -0400548 ACPI_FUNCTION_TRACE_U32("ex_write_with_update_rule", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 /* Start with the new bits */
551
552 merged_value = field_value;
553
554 /* If the mask is all ones, we don't need to worry about the update rule */
555
556 if (mask != ACPI_INTEGER_MAX) {
557 /* Decode the update rule */
558
Len Brown4be44fc2005-08-05 00:44:28 -0400559 switch (obj_desc->common_field.
560 field_flags & AML_FIELD_UPDATE_RULE_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 case AML_FIELD_UPDATE_PRESERVE:
562 /*
563 * Check if update rule needs to be applied (not if mask is all
564 * ones) The left shift drops the bits we want to ignore.
565 */
Len Brown4be44fc2005-08-05 00:44:28 -0400566 if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
567 ACPI_MUL_8(obj_desc->common_field.
568 access_byte_width))) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569 /*
570 * Read the current contents of the byte/word/dword containing
571 * the field, and merge with the new field value.
572 */
Len Brown4be44fc2005-08-05 00:44:28 -0400573 status =
574 acpi_ex_field_datum_io(obj_desc,
575 field_datum_byte_offset,
576 &current_value,
577 ACPI_READ);
578 if (ACPI_FAILURE(status)) {
579 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700580 }
581
582 merged_value |= (current_value & ~mask);
583 }
584 break;
585
586 case AML_FIELD_UPDATE_WRITE_AS_ONES:
587
588 /* Set positions outside the field to all ones */
589
590 merged_value |= ~mask;
591 break;
592
593 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
594
595 /* Set positions outside the field to all zeros */
596
597 merged_value &= mask;
598 break;
599
600 default:
601
Bob Moore4a90c7e2006-01-13 16:22:00 -0500602 ACPI_REPORT_ERROR(("Unknown update_rule value: %X\n",
603 (obj_desc->common_field.
604 field_flags &
605 AML_FIELD_UPDATE_RULE_MASK)));
Len Brown4be44fc2005-08-05 00:44:28 -0400606 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700607 }
608 }
609
Len Brown4be44fc2005-08-05 00:44:28 -0400610 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
611 "Mask %8.8X%8.8X, datum_offset %X, Width %X, Value %8.8X%8.8X, merged_value %8.8X%8.8X\n",
612 ACPI_FORMAT_UINT64(mask),
613 field_datum_byte_offset,
614 obj_desc->common_field.access_byte_width,
615 ACPI_FORMAT_UINT64(field_value),
616 ACPI_FORMAT_UINT64(merged_value)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700617
618 /* Write the merged value */
619
Len Brown4be44fc2005-08-05 00:44:28 -0400620 status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
621 &merged_value, ACPI_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622
Len Brown4be44fc2005-08-05 00:44:28 -0400623 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700624}
625
Linus Torvalds1da177e2005-04-16 15:20:36 -0700626/*******************************************************************************
627 *
628 * FUNCTION: acpi_ex_extract_from_field
629 *
630 * PARAMETERS: obj_desc - Field to be read
631 * Buffer - Where to store the field data
632 * buffer_length - Length of Buffer
633 *
634 * RETURN: Status
635 *
636 * DESCRIPTION: Retrieve the current value of the given field
637 *
638 ******************************************************************************/
639
640acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400641acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
642 void *buffer, u32 buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643{
Len Brown4be44fc2005-08-05 00:44:28 -0400644 acpi_status status;
645 acpi_integer raw_datum;
646 acpi_integer merged_datum;
647 u32 field_offset = 0;
648 u32 buffer_offset = 0;
649 u32 buffer_tail_bits;
650 u32 datum_count;
651 u32 field_datum_count;
652 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700653
Len Brown4be44fc2005-08-05 00:44:28 -0400654 ACPI_FUNCTION_TRACE("ex_extract_from_field");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700655
656 /* Validate target buffer and clear it */
657
Len Brown4be44fc2005-08-05 00:44:28 -0400658 if (buffer_length <
659 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
Bob Moore4a90c7e2006-01-13 16:22:00 -0500660 ACPI_REPORT_ERROR(("Field size %X (bits) is too large for buffer (%X)\n", obj_desc->common_field.bit_length, buffer_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661
Len Brown4be44fc2005-08-05 00:44:28 -0400662 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700663 }
Len Brown4be44fc2005-08-05 00:44:28 -0400664 ACPI_MEMSET(buffer, 0, buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665
666 /* Compute the number of datums (access width data items) */
667
Len Brown4be44fc2005-08-05 00:44:28 -0400668 datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
669 obj_desc->common_field.access_bit_width);
670 field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
671 obj_desc->common_field.
672 start_field_bit_offset,
673 obj_desc->common_field.
674 access_bit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
676 /* Priming read from the field */
677
Len Brown4be44fc2005-08-05 00:44:28 -0400678 status =
679 acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
680 ACPI_READ);
681 if (ACPI_FAILURE(status)) {
682 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Len Brown4be44fc2005-08-05 00:44:28 -0400684 merged_datum =
685 raw_datum >> obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686
687 /* Read the rest of the field */
688
689 for (i = 1; i < field_datum_count; i++) {
690 /* Get next input datum from the field */
691
692 field_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400693 status = acpi_ex_field_datum_io(obj_desc, field_offset,
694 &raw_datum, ACPI_READ);
695 if (ACPI_FAILURE(status)) {
696 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697 }
698
699 /* Merge with previous datum if necessary */
700
701 merged_datum |= raw_datum <<
Len Brown4be44fc2005-08-05 00:44:28 -0400702 (obj_desc->common_field.access_bit_width -
703 obj_desc->common_field.start_field_bit_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700704
705 if (i == datum_count) {
706 break;
707 }
708
709 /* Write merged datum to target buffer */
710
Len Brown4be44fc2005-08-05 00:44:28 -0400711 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
712 ACPI_MIN(obj_desc->common_field.access_byte_width,
713 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 buffer_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400716 merged_datum =
717 raw_datum >> obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 /* Mask off any extra bits in the last datum */
721
Robert Moore44f6c012005-04-18 22:49:35 -0400722 buffer_tail_bits = obj_desc->common_field.bit_length %
Len Brown4be44fc2005-08-05 00:44:28 -0400723 obj_desc->common_field.access_bit_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700724 if (buffer_tail_bits) {
Len Brown4be44fc2005-08-05 00:44:28 -0400725 merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 }
727
728 /* Write the last datum to the buffer */
729
Len Brown4be44fc2005-08-05 00:44:28 -0400730 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
731 ACPI_MIN(obj_desc->common_field.access_byte_width,
732 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700733
Len Brown4be44fc2005-08-05 00:44:28 -0400734 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735}
736
Linus Torvalds1da177e2005-04-16 15:20:36 -0700737/*******************************************************************************
738 *
739 * FUNCTION: acpi_ex_insert_into_field
740 *
741 * PARAMETERS: obj_desc - Field to be written
742 * Buffer - Data to be written
743 * buffer_length - Length of Buffer
744 *
745 * RETURN: Status
746 *
747 * DESCRIPTION: Store the Buffer contents into the given field
748 *
749 ******************************************************************************/
750
751acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400752acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
753 void *buffer, u32 buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754{
Len Brown4be44fc2005-08-05 00:44:28 -0400755 acpi_status status;
756 acpi_integer mask;
757 acpi_integer merged_datum;
758 acpi_integer raw_datum = 0;
759 u32 field_offset = 0;
760 u32 buffer_offset = 0;
761 u32 buffer_tail_bits;
762 u32 datum_count;
763 u32 field_datum_count;
764 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700765
Len Brown4be44fc2005-08-05 00:44:28 -0400766 ACPI_FUNCTION_TRACE("ex_insert_into_field");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767
768 /* Validate input buffer */
769
Len Brown4be44fc2005-08-05 00:44:28 -0400770 if (buffer_length <
771 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
Bob Moore4a90c7e2006-01-13 16:22:00 -0500772 ACPI_REPORT_ERROR(("Field size %X (bits) is too large for buffer (%X)\n", obj_desc->common_field.bit_length, buffer_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773
Len Brown4be44fc2005-08-05 00:44:28 -0400774 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 }
776
777 /* Compute the number of datums (access width data items) */
778
Len Brown4be44fc2005-08-05 00:44:28 -0400779 mask =
780 ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
781 datum_count =
782 ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
783 obj_desc->common_field.access_bit_width);
784 field_datum_count =
785 ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
786 obj_desc->common_field.start_field_bit_offset,
787 obj_desc->common_field.access_bit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* Get initial Datum from the input buffer */
790
Len Brown4be44fc2005-08-05 00:44:28 -0400791 ACPI_MEMCPY(&raw_datum, buffer,
792 ACPI_MIN(obj_desc->common_field.access_byte_width,
793 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794
Len Brown4be44fc2005-08-05 00:44:28 -0400795 merged_datum =
796 raw_datum << obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797
798 /* Write the entire field */
799
800 for (i = 1; i < field_datum_count; i++) {
801 /* Write merged datum to the target field */
802
803 merged_datum &= mask;
Len Brown4be44fc2005-08-05 00:44:28 -0400804 status = acpi_ex_write_with_update_rule(obj_desc, mask,
805 merged_datum,
806 field_offset);
807 if (ACPI_FAILURE(status)) {
808 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700809 }
810
811 /* Start new output datum by merging with previous input datum */
812
813 field_offset += obj_desc->common_field.access_byte_width;
814 merged_datum = raw_datum >>
Len Brown4be44fc2005-08-05 00:44:28 -0400815 (obj_desc->common_field.access_bit_width -
816 obj_desc->common_field.start_field_bit_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 mask = ACPI_INTEGER_MAX;
818
819 if (i == datum_count) {
820 break;
821 }
822
823 /* Get the next input datum from the buffer */
824
825 buffer_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400826 ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
827 ACPI_MIN(obj_desc->common_field.access_byte_width,
828 buffer_length - buffer_offset));
829 merged_datum |=
830 raw_datum << obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 }
832
833 /* Mask off any extra bits in the last datum */
834
835 buffer_tail_bits = (obj_desc->common_field.bit_length +
Len Brown4be44fc2005-08-05 00:44:28 -0400836 obj_desc->common_field.start_field_bit_offset) %
837 obj_desc->common_field.access_bit_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 if (buffer_tail_bits) {
Len Brown4be44fc2005-08-05 00:44:28 -0400839 mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700840 }
841
842 /* Write the last datum to the field */
843
844 merged_datum &= mask;
Len Brown4be44fc2005-08-05 00:44:28 -0400845 status = acpi_ex_write_with_update_rule(obj_desc,
846 mask, merged_datum,
847 field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
Len Brown4be44fc2005-08-05 00:44:28 -0400849 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850}