blob: d5a4b2627c190d87a8dfb12fd765f7bee45ba26a [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 Mooreb8e4d892006-01-27 16:43:00 -050097 ACPI_ERROR((AE_INFO, "Needed Region, found type %X (%s)",
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) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400116
Linus Torvalds1da177e2005-04-16 15:20:36 -0700117 /* SMBus has a non-linear address space */
118
Len Brown4be44fc2005-08-05 00:44:28 -0400119 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700120 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121#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)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400127 if (ACPI_FAILURE(status)) {
128 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 }
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 */
Robert Moore44f6c012005-04-18 22:49:35 -0400138 if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset +
Len Brown4be44fc2005-08-05 00:44:28 -0400139 field_datum_byte_offset +
140 obj_desc->common_field.
141 access_byte_width)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 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 */
Len Brown4be44fc2005-08-05 00:44:28 -0400148 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);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155 }
156 }
157
Len Brown4be44fc2005-08-05 00:44:28 -0400158 if (rgn_desc->region.length <
159 obj_desc->common_field.access_byte_width) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 /*
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 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500165 ACPI_ERROR((AE_INFO,
166 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)",
167 acpi_ut_get_node_name(obj_desc->
168 common_field.node),
169 obj_desc->common_field.access_byte_width,
170 acpi_ut_get_node_name(rgn_desc->region.
171 node),
172 rgn_desc->region.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 }
174
175 /*
176 * Offset rounded up to next multiple of field width
177 * exceeds region length, indicate an error
178 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500179 ACPI_ERROR((AE_INFO,
180 "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)",
181 acpi_ut_get_node_name(obj_desc->common_field.node),
182 obj_desc->common_field.base_byte_offset,
183 field_datum_byte_offset,
184 obj_desc->common_field.access_byte_width,
185 acpi_ut_get_node_name(rgn_desc->region.node),
186 rgn_desc->region.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700187
Len Brown4be44fc2005-08-05 00:44:28 -0400188 return_ACPI_STATUS(AE_AML_REGION_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700189 }
190
Len Brown4be44fc2005-08-05 00:44:28 -0400191 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700192}
193
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194/*******************************************************************************
195 *
196 * FUNCTION: acpi_ex_access_region
197 *
Robert Moore44f6c012005-04-18 22:49:35 -0400198 * PARAMETERS: obj_desc - Field to be read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199 * field_datum_byte_offset - Byte offset of this datum within the
200 * parent field
Robert Moore44f6c012005-04-18 22:49:35 -0400201 * Value - Where to store value (must at least
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 * the size of acpi_integer)
203 * Function - Read or Write flag plus other region-
204 * dependent flags
205 *
206 * RETURN: Status
207 *
208 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
209 *
210 ******************************************************************************/
211
212acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400213acpi_ex_access_region(union acpi_operand_object *obj_desc,
214 u32 field_datum_byte_offset,
215 acpi_integer * value, u32 function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216{
Len Brown4be44fc2005-08-05 00:44:28 -0400217 acpi_status status;
218 union acpi_operand_object *rgn_desc;
219 acpi_physical_address address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220
Len Brown4be44fc2005-08-05 00:44:28 -0400221 ACPI_FUNCTION_TRACE("ex_access_region");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700222
223 /*
224 * Ensure that the region operands are fully evaluated and verify
225 * the validity of the request
226 */
Len Brown4be44fc2005-08-05 00:44:28 -0400227 status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
228 if (ACPI_FAILURE(status)) {
229 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 }
231
232 /*
233 * The physical address of this field datum is:
234 *
235 * 1) The base of the region, plus
236 * 2) The base offset of the field, plus
237 * 3) The current offset into the field
238 */
239 rgn_desc = obj_desc->common_field.region_obj;
Robert Moore44f6c012005-04-18 22:49:35 -0400240 address = rgn_desc->region.address +
Len Brown4be44fc2005-08-05 00:44:28 -0400241 obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700242
243 if ((function & ACPI_IO_MASK) == ACPI_READ) {
Len Brown4be44fc2005-08-05 00:44:28 -0400244 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
245 } else {
246 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 }
248
Len Brown4be44fc2005-08-05 00:44:28 -0400249 ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
250 " Region [%s:%X], Width %X, byte_base %X, Offset %X at %8.8X%8.8X\n",
251 acpi_ut_get_region_name(rgn_desc->region.
252 space_id),
253 rgn_desc->region.space_id,
254 obj_desc->common_field.access_byte_width,
255 obj_desc->common_field.base_byte_offset,
256 field_datum_byte_offset,
257 ACPI_FORMAT_UINT64(address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258
259 /* Invoke the appropriate address_space/op_region handler */
260
Len Brown4be44fc2005-08-05 00:44:28 -0400261 status = acpi_ev_address_space_dispatch(rgn_desc, function,
262 address,
263 ACPI_MUL_8(obj_desc->
264 common_field.
265 access_byte_width),
266 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700267
Len Brown4be44fc2005-08-05 00:44:28 -0400268 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700269 if (status == AE_NOT_IMPLEMENTED) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500270 ACPI_ERROR((AE_INFO,
271 "Region %s(%X) not implemented",
272 acpi_ut_get_region_name(rgn_desc->region.
273 space_id),
274 rgn_desc->region.space_id));
Len Brown4be44fc2005-08-05 00:44:28 -0400275 } else if (status == AE_NOT_EXIST) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500276 ACPI_ERROR((AE_INFO,
277 "Region %s(%X) has no handler",
278 acpi_ut_get_region_name(rgn_desc->region.
279 space_id),
280 rgn_desc->region.space_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700281 }
282 }
283
Len Brown4be44fc2005-08-05 00:44:28 -0400284 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285}
286
Linus Torvalds1da177e2005-04-16 15:20:36 -0700287/*******************************************************************************
288 *
289 * FUNCTION: acpi_ex_register_overflow
290 *
Robert Moore44f6c012005-04-18 22:49:35 -0400291 * PARAMETERS: obj_desc - Register(Field) to be written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700292 * Value - Value to be stored
293 *
294 * RETURN: TRUE if value overflows the field, FALSE otherwise
295 *
296 * DESCRIPTION: Check if a value is out of range of the field being written.
297 * Used to check if the values written to Index and Bank registers
298 * are out of range. Normally, the value is simply truncated
299 * to fit the field, but this case is most likely a serious
300 * coding error in the ASL.
301 *
302 ******************************************************************************/
303
Robert Moore44f6c012005-04-18 22:49:35 -0400304static u8
Len Brown4be44fc2005-08-05 00:44:28 -0400305acpi_ex_register_overflow(union acpi_operand_object *obj_desc,
306 acpi_integer value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307{
308
309 if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
310 /*
311 * The field is large enough to hold the maximum integer, so we can
312 * never overflow it.
313 */
314 return (FALSE);
315 }
316
317 if (value >= ((acpi_integer) 1 << obj_desc->common_field.bit_length)) {
318 /*
319 * The Value is larger than the maximum value that can fit into
320 * the register.
321 */
322 return (TRUE);
323 }
324
325 /* The Value will fit into the field with no truncation */
326
327 return (FALSE);
328}
329
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330/*******************************************************************************
331 *
332 * FUNCTION: acpi_ex_field_datum_io
333 *
Robert Moore44f6c012005-04-18 22:49:35 -0400334 * PARAMETERS: obj_desc - Field to be read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 * field_datum_byte_offset - Byte offset of this datum within the
336 * parent field
Robert Moore44f6c012005-04-18 22:49:35 -0400337 * Value - Where to store value (must be 64 bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700338 * read_write - Read or Write flag
339 *
340 * RETURN: Status
341 *
342 * DESCRIPTION: Read or Write a single datum of a field. The field_type is
343 * demultiplexed here to handle the different types of fields
344 * (buffer_field, region_field, index_field, bank_field)
345 *
346 ******************************************************************************/
347
Robert Moore44f6c012005-04-18 22:49:35 -0400348static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400349acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
350 u32 field_datum_byte_offset,
351 acpi_integer * value, u32 read_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700352{
Len Brown4be44fc2005-08-05 00:44:28 -0400353 acpi_status status;
354 acpi_integer local_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355
Len Brown4be44fc2005-08-05 00:44:28 -0400356 ACPI_FUNCTION_TRACE_U32("ex_field_datum_io", field_datum_byte_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 if (read_write == ACPI_READ) {
359 if (!value) {
360 local_value = 0;
Robert Moore44f6c012005-04-18 22:49:35 -0400361
362 /* To support reads without saving return value */
363 value = &local_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 }
365
366 /* Clear the entire return buffer first, [Very Important!] */
367
368 *value = 0;
369 }
370
371 /*
372 * The four types of fields are:
373 *
374 * buffer_field - Read/write from/to a Buffer
375 * region_field - Read/write from/to a Operation Region.
Robert Moore44f6c012005-04-18 22:49:35 -0400376 * bank_field - Write to a Bank Register, then read/write from/to an
377 * operation_region
378 * index_field - Write to an Index Register, then read/write from/to a
379 * Data Register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700380 */
Len Brown4be44fc2005-08-05 00:44:28 -0400381 switch (ACPI_GET_OBJECT_TYPE(obj_desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700382 case ACPI_TYPE_BUFFER_FIELD:
383 /*
384 * If the buffer_field arguments have not been previously evaluated,
385 * evaluate them now and save the results.
386 */
387 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400388 status = acpi_ds_get_buffer_field_arguments(obj_desc);
389 if (ACPI_FAILURE(status)) {
390 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 }
392 }
393
394 if (read_write == ACPI_READ) {
395 /*
396 * Copy the data from the source buffer.
397 * Length is the field width in bytes.
398 */
Len Brown4be44fc2005-08-05 00:44:28 -0400399 ACPI_MEMCPY(value,
400 (obj_desc->buffer_field.buffer_obj)->buffer.
401 pointer +
402 obj_desc->buffer_field.base_byte_offset +
403 field_datum_byte_offset,
404 obj_desc->common_field.access_byte_width);
405 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 /*
407 * Copy the data to the target buffer.
408 * Length is the field width in bytes.
409 */
Len Brown4be44fc2005-08-05 00:44:28 -0400410 ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
411 pointer +
412 obj_desc->buffer_field.base_byte_offset +
413 field_datum_byte_offset, value,
414 obj_desc->common_field.access_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 }
416
417 status = AE_OK;
418 break;
419
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 case ACPI_TYPE_LOCAL_BANK_FIELD:
421
Robert Moore44f6c012005-04-18 22:49:35 -0400422 /*
423 * Ensure that the bank_value is not beyond the capacity of
424 * the register
425 */
Len Brown4be44fc2005-08-05 00:44:28 -0400426 if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
427 (acpi_integer) obj_desc->
428 bank_field.value)) {
429 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 }
431
432 /*
433 * For bank_fields, we must write the bank_value to the bank_register
434 * (itself a region_field) before we can access the data.
435 */
Len Brown4be44fc2005-08-05 00:44:28 -0400436 status =
437 acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
438 &obj_desc->bank_field.value,
439 sizeof(obj_desc->bank_field.
440 value));
441 if (ACPI_FAILURE(status)) {
442 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443 }
444
445 /*
446 * Now that the Bank has been selected, fall through to the
447 * region_field case and write the datum to the Operation Region
448 */
449
450 /*lint -fallthrough */
451
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 case ACPI_TYPE_LOCAL_REGION_FIELD:
453 /*
454 * For simple region_fields, we just directly access the owning
455 * Operation Region.
456 */
Len Brown4be44fc2005-08-05 00:44:28 -0400457 status =
458 acpi_ex_access_region(obj_desc, field_datum_byte_offset,
459 value, read_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 break;
461
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 case ACPI_TYPE_LOCAL_INDEX_FIELD:
463
Robert Moore44f6c012005-04-18 22:49:35 -0400464 /*
465 * Ensure that the index_value is not beyond the capacity of
466 * the register
467 */
Len Brown4be44fc2005-08-05 00:44:28 -0400468 if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
469 (acpi_integer) obj_desc->
470 index_field.value)) {
471 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 }
473
474 /* Write the index value to the index_register (itself a region_field) */
475
476 field_datum_byte_offset += obj_desc->index_field.value;
477
Len Brown4be44fc2005-08-05 00:44:28 -0400478 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
479 "Write to Index Register: Value %8.8X\n",
480 field_datum_byte_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481
Len Brown4be44fc2005-08-05 00:44:28 -0400482 status =
483 acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
484 &field_datum_byte_offset,
485 sizeof(field_datum_byte_offset));
486 if (ACPI_FAILURE(status)) {
487 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488 }
489
Len Brown4be44fc2005-08-05 00:44:28 -0400490 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
491 "I/O to Data Register: value_ptr %p\n",
492 value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493
494 if (read_write == ACPI_READ) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400495
Linus Torvalds1da177e2005-04-16 15:20:36 -0700496 /* Read the datum from the data_register */
497
Len Brown4be44fc2005-08-05 00:44:28 -0400498 status =
499 acpi_ex_extract_from_field(obj_desc->index_field.
500 data_obj, value,
501 sizeof(acpi_integer));
502 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 /* Write the datum to the data_register */
504
Len Brown4be44fc2005-08-05 00:44:28 -0400505 status =
506 acpi_ex_insert_into_field(obj_desc->index_field.
507 data_obj, value,
508 sizeof(acpi_integer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 }
510 break;
511
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 default:
513
Bob Mooreb8e4d892006-01-27 16:43:00 -0500514 ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %X",
515 ACPI_GET_OBJECT_TYPE(obj_desc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 status = AE_AML_INTERNAL;
517 break;
518 }
519
Len Brown4be44fc2005-08-05 00:44:28 -0400520 if (ACPI_SUCCESS(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 if (read_write == ACPI_READ) {
Len Brown4be44fc2005-08-05 00:44:28 -0400522 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
523 "Value Read %8.8X%8.8X, Width %d\n",
524 ACPI_FORMAT_UINT64(*value),
525 obj_desc->common_field.
526 access_byte_width));
527 } else {
528 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
529 "Value Written %8.8X%8.8X, Width %d\n",
530 ACPI_FORMAT_UINT64(*value),
531 obj_desc->common_field.
532 access_byte_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 }
534 }
535
Len Brown4be44fc2005-08-05 00:44:28 -0400536 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537}
538
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539/*******************************************************************************
540 *
541 * FUNCTION: acpi_ex_write_with_update_rule
542 *
Robert Moore44f6c012005-04-18 22:49:35 -0400543 * PARAMETERS: obj_desc - Field to be written
544 * Mask - bitmask within field datum
545 * field_value - Value to write
546 * field_datum_byte_offset - Offset of datum within field
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 *
548 * RETURN: Status
549 *
550 * DESCRIPTION: Apply the field update rule to a field write
551 *
552 ******************************************************************************/
553
554acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400555acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
556 acpi_integer mask,
557 acpi_integer field_value,
558 u32 field_datum_byte_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700559{
Len Brown4be44fc2005-08-05 00:44:28 -0400560 acpi_status status = AE_OK;
561 acpi_integer merged_value;
562 acpi_integer current_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
Len Brown4be44fc2005-08-05 00:44:28 -0400564 ACPI_FUNCTION_TRACE_U32("ex_write_with_update_rule", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565
566 /* Start with the new bits */
567
568 merged_value = field_value;
569
570 /* If the mask is all ones, we don't need to worry about the update rule */
571
572 if (mask != ACPI_INTEGER_MAX) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400573
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574 /* Decode the update rule */
575
Len Brown4be44fc2005-08-05 00:44:28 -0400576 switch (obj_desc->common_field.
577 field_flags & AML_FIELD_UPDATE_RULE_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 case AML_FIELD_UPDATE_PRESERVE:
579 /*
580 * Check if update rule needs to be applied (not if mask is all
581 * ones) The left shift drops the bits we want to ignore.
582 */
Len Brown4be44fc2005-08-05 00:44:28 -0400583 if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
584 ACPI_MUL_8(obj_desc->common_field.
585 access_byte_width))) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 /*
587 * Read the current contents of the byte/word/dword containing
588 * the field, and merge with the new field value.
589 */
Len Brown4be44fc2005-08-05 00:44:28 -0400590 status =
591 acpi_ex_field_datum_io(obj_desc,
592 field_datum_byte_offset,
593 &current_value,
594 ACPI_READ);
595 if (ACPI_FAILURE(status)) {
596 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 }
598
599 merged_value |= (current_value & ~mask);
600 }
601 break;
602
603 case AML_FIELD_UPDATE_WRITE_AS_ONES:
604
605 /* Set positions outside the field to all ones */
606
607 merged_value |= ~mask;
608 break;
609
610 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
611
612 /* Set positions outside the field to all zeros */
613
614 merged_value &= mask;
615 break;
616
617 default:
618
Bob Mooreb8e4d892006-01-27 16:43:00 -0500619 ACPI_ERROR((AE_INFO,
620 "Unknown update_rule value: %X",
621 (obj_desc->common_field.
622 field_flags &
623 AML_FIELD_UPDATE_RULE_MASK)));
Len Brown4be44fc2005-08-05 00:44:28 -0400624 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 }
626 }
627
Len Brown4be44fc2005-08-05 00:44:28 -0400628 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
629 "Mask %8.8X%8.8X, datum_offset %X, Width %X, Value %8.8X%8.8X, merged_value %8.8X%8.8X\n",
630 ACPI_FORMAT_UINT64(mask),
631 field_datum_byte_offset,
632 obj_desc->common_field.access_byte_width,
633 ACPI_FORMAT_UINT64(field_value),
634 ACPI_FORMAT_UINT64(merged_value)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700635
636 /* Write the merged value */
637
Len Brown4be44fc2005-08-05 00:44:28 -0400638 status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
639 &merged_value, ACPI_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640
Len Brown4be44fc2005-08-05 00:44:28 -0400641 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700642}
643
Linus Torvalds1da177e2005-04-16 15:20:36 -0700644/*******************************************************************************
645 *
646 * FUNCTION: acpi_ex_extract_from_field
647 *
648 * PARAMETERS: obj_desc - Field to be read
649 * Buffer - Where to store the field data
650 * buffer_length - Length of Buffer
651 *
652 * RETURN: Status
653 *
654 * DESCRIPTION: Retrieve the current value of the given field
655 *
656 ******************************************************************************/
657
658acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400659acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
660 void *buffer, u32 buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661{
Len Brown4be44fc2005-08-05 00:44:28 -0400662 acpi_status status;
663 acpi_integer raw_datum;
664 acpi_integer merged_datum;
665 u32 field_offset = 0;
666 u32 buffer_offset = 0;
667 u32 buffer_tail_bits;
668 u32 datum_count;
669 u32 field_datum_count;
670 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700671
Len Brown4be44fc2005-08-05 00:44:28 -0400672 ACPI_FUNCTION_TRACE("ex_extract_from_field");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700673
674 /* Validate target buffer and clear it */
675
Len Brown4be44fc2005-08-05 00:44:28 -0400676 if (buffer_length <
677 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500678 ACPI_ERROR((AE_INFO,
679 "Field size %X (bits) is too large for buffer (%X)",
680 obj_desc->common_field.bit_length, buffer_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681
Len Brown4be44fc2005-08-05 00:44:28 -0400682 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 }
Len Brown4be44fc2005-08-05 00:44:28 -0400684 ACPI_MEMSET(buffer, 0, buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685
686 /* Compute the number of datums (access width data items) */
687
Len Brown4be44fc2005-08-05 00:44:28 -0400688 datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
689 obj_desc->common_field.access_bit_width);
690 field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
691 obj_desc->common_field.
692 start_field_bit_offset,
693 obj_desc->common_field.
694 access_bit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700695
696 /* Priming read from the field */
697
Len Brown4be44fc2005-08-05 00:44:28 -0400698 status =
699 acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
700 ACPI_READ);
701 if (ACPI_FAILURE(status)) {
702 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 }
Len Brown4be44fc2005-08-05 00:44:28 -0400704 merged_datum =
705 raw_datum >> obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706
707 /* Read the rest of the field */
708
709 for (i = 1; i < field_datum_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400710
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 /* Get next input datum from the field */
712
713 field_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400714 status = acpi_ex_field_datum_io(obj_desc, field_offset,
715 &raw_datum, ACPI_READ);
716 if (ACPI_FAILURE(status)) {
717 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700718 }
719
720 /* Merge with previous datum if necessary */
721
722 merged_datum |= raw_datum <<
Len Brown4be44fc2005-08-05 00:44:28 -0400723 (obj_desc->common_field.access_bit_width -
724 obj_desc->common_field.start_field_bit_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700725
726 if (i == datum_count) {
727 break;
728 }
729
730 /* Write merged datum to target buffer */
731
Len Brown4be44fc2005-08-05 00:44:28 -0400732 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
733 ACPI_MIN(obj_desc->common_field.access_byte_width,
734 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735
736 buffer_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400737 merged_datum =
738 raw_datum >> obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700739 }
740
741 /* Mask off any extra bits in the last datum */
742
Robert Moore44f6c012005-04-18 22:49:35 -0400743 buffer_tail_bits = obj_desc->common_field.bit_length %
Len Brown4be44fc2005-08-05 00:44:28 -0400744 obj_desc->common_field.access_bit_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700745 if (buffer_tail_bits) {
Len Brown4be44fc2005-08-05 00:44:28 -0400746 merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748
749 /* Write the last datum to the buffer */
750
Len Brown4be44fc2005-08-05 00:44:28 -0400751 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
752 ACPI_MIN(obj_desc->common_field.access_byte_width,
753 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754
Len Brown4be44fc2005-08-05 00:44:28 -0400755 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700756}
757
Linus Torvalds1da177e2005-04-16 15:20:36 -0700758/*******************************************************************************
759 *
760 * FUNCTION: acpi_ex_insert_into_field
761 *
762 * PARAMETERS: obj_desc - Field to be written
763 * Buffer - Data to be written
764 * buffer_length - Length of Buffer
765 *
766 * RETURN: Status
767 *
768 * DESCRIPTION: Store the Buffer contents into the given field
769 *
770 ******************************************************************************/
771
772acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400773acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
774 void *buffer, u32 buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775{
Len Brown4be44fc2005-08-05 00:44:28 -0400776 acpi_status status;
777 acpi_integer mask;
778 acpi_integer merged_datum;
779 acpi_integer raw_datum = 0;
780 u32 field_offset = 0;
781 u32 buffer_offset = 0;
782 u32 buffer_tail_bits;
783 u32 datum_count;
784 u32 field_datum_count;
785 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786
Len Brown4be44fc2005-08-05 00:44:28 -0400787 ACPI_FUNCTION_TRACE("ex_insert_into_field");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700788
789 /* Validate input buffer */
790
Len Brown4be44fc2005-08-05 00:44:28 -0400791 if (buffer_length <
792 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500793 ACPI_ERROR((AE_INFO,
794 "Field size %X (bits) is too large for buffer (%X)",
795 obj_desc->common_field.bit_length, buffer_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796
Len Brown4be44fc2005-08-05 00:44:28 -0400797 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 }
799
800 /* Compute the number of datums (access width data items) */
801
Len Brown4be44fc2005-08-05 00:44:28 -0400802 mask =
803 ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
804 datum_count =
805 ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
806 obj_desc->common_field.access_bit_width);
807 field_datum_count =
808 ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
809 obj_desc->common_field.start_field_bit_offset,
810 obj_desc->common_field.access_bit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700811
812 /* Get initial Datum from the input buffer */
813
Len Brown4be44fc2005-08-05 00:44:28 -0400814 ACPI_MEMCPY(&raw_datum, buffer,
815 ACPI_MIN(obj_desc->common_field.access_byte_width,
816 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817
Len Brown4be44fc2005-08-05 00:44:28 -0400818 merged_datum =
819 raw_datum << obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820
821 /* Write the entire field */
822
823 for (i = 1; i < field_datum_count; i++) {
Bob Moore52fc0b02006-10-02 00:00:00 -0400824
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /* Write merged datum to the target field */
826
827 merged_datum &= mask;
Len Brown4be44fc2005-08-05 00:44:28 -0400828 status = acpi_ex_write_with_update_rule(obj_desc, mask,
829 merged_datum,
830 field_offset);
831 if (ACPI_FAILURE(status)) {
832 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 }
834
835 /* Start new output datum by merging with previous input datum */
836
837 field_offset += obj_desc->common_field.access_byte_width;
838 merged_datum = raw_datum >>
Len Brown4be44fc2005-08-05 00:44:28 -0400839 (obj_desc->common_field.access_bit_width -
840 obj_desc->common_field.start_field_bit_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 mask = ACPI_INTEGER_MAX;
842
843 if (i == datum_count) {
844 break;
845 }
846
847 /* Get the next input datum from the buffer */
848
849 buffer_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400850 ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
851 ACPI_MIN(obj_desc->common_field.access_byte_width,
852 buffer_length - buffer_offset));
853 merged_datum |=
854 raw_datum << obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 }
856
857 /* Mask off any extra bits in the last datum */
858
859 buffer_tail_bits = (obj_desc->common_field.bit_length +
Len Brown4be44fc2005-08-05 00:44:28 -0400860 obj_desc->common_field.start_field_bit_offset) %
861 obj_desc->common_field.access_bit_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862 if (buffer_tail_bits) {
Len Brown4be44fc2005-08-05 00:44:28 -0400863 mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 }
865
866 /* Write the last datum to the field */
867
868 merged_datum &= mask;
Len Brown4be44fc2005-08-05 00:44:28 -0400869 status = acpi_ex_write_with_update_rule(obj_desc,
870 mask, merged_datum,
871 field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
Len Brown4be44fc2005-08-05 00:44:28 -0400873 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874}