blob: bd1af35f7fcfc6497e1330d7092d58128f6eb9bc [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) {
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 Mooreb8e4d892006-01-27 16:43:00 -0500164 ACPI_ERROR((AE_INFO,
165 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)",
166 acpi_ut_get_node_name(obj_desc->
167 common_field.node),
168 obj_desc->common_field.access_byte_width,
169 acpi_ut_get_node_name(rgn_desc->region.
170 node),
171 rgn_desc->region.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 }
173
174 /*
175 * Offset rounded up to next multiple of field width
176 * exceeds region length, indicate an error
177 */
Bob Mooreb8e4d892006-01-27 16:43:00 -0500178 ACPI_ERROR((AE_INFO,
179 "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)",
180 acpi_ut_get_node_name(obj_desc->common_field.node),
181 obj_desc->common_field.base_byte_offset,
182 field_datum_byte_offset,
183 obj_desc->common_field.access_byte_width,
184 acpi_ut_get_node_name(rgn_desc->region.node),
185 rgn_desc->region.length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186
Len Brown4be44fc2005-08-05 00:44:28 -0400187 return_ACPI_STATUS(AE_AML_REGION_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 }
189
Len Brown4be44fc2005-08-05 00:44:28 -0400190 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191}
192
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193/*******************************************************************************
194 *
195 * FUNCTION: acpi_ex_access_region
196 *
Robert Moore44f6c012005-04-18 22:49:35 -0400197 * PARAMETERS: obj_desc - Field to be read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 * field_datum_byte_offset - Byte offset of this datum within the
199 * parent field
Robert Moore44f6c012005-04-18 22:49:35 -0400200 * Value - Where to store value (must at least
Linus Torvalds1da177e2005-04-16 15:20:36 -0700201 * the size of acpi_integer)
202 * Function - Read or Write flag plus other region-
203 * dependent flags
204 *
205 * RETURN: Status
206 *
207 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
208 *
209 ******************************************************************************/
210
211acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400212acpi_ex_access_region(union acpi_operand_object *obj_desc,
213 u32 field_datum_byte_offset,
214 acpi_integer * value, u32 function)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215{
Len Brown4be44fc2005-08-05 00:44:28 -0400216 acpi_status status;
217 union acpi_operand_object *rgn_desc;
218 acpi_physical_address address;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219
Len Brown4be44fc2005-08-05 00:44:28 -0400220 ACPI_FUNCTION_TRACE("ex_access_region");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
222 /*
223 * Ensure that the region operands are fully evaluated and verify
224 * the validity of the request
225 */
Len Brown4be44fc2005-08-05 00:44:28 -0400226 status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
227 if (ACPI_FAILURE(status)) {
228 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700229 }
230
231 /*
232 * The physical address of this field datum is:
233 *
234 * 1) The base of the region, plus
235 * 2) The base offset of the field, plus
236 * 3) The current offset into the field
237 */
238 rgn_desc = obj_desc->common_field.region_obj;
Robert Moore44f6c012005-04-18 22:49:35 -0400239 address = rgn_desc->region.address +
Len Brown4be44fc2005-08-05 00:44:28 -0400240 obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241
242 if ((function & ACPI_IO_MASK) == ACPI_READ) {
Len Brown4be44fc2005-08-05 00:44:28 -0400243 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
244 } else {
245 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 }
247
Len Brown4be44fc2005-08-05 00:44:28 -0400248 ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
249 " Region [%s:%X], Width %X, byte_base %X, Offset %X at %8.8X%8.8X\n",
250 acpi_ut_get_region_name(rgn_desc->region.
251 space_id),
252 rgn_desc->region.space_id,
253 obj_desc->common_field.access_byte_width,
254 obj_desc->common_field.base_byte_offset,
255 field_datum_byte_offset,
256 ACPI_FORMAT_UINT64(address)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700257
258 /* Invoke the appropriate address_space/op_region handler */
259
Len Brown4be44fc2005-08-05 00:44:28 -0400260 status = acpi_ev_address_space_dispatch(rgn_desc, function,
261 address,
262 ACPI_MUL_8(obj_desc->
263 common_field.
264 access_byte_width),
265 value);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700266
Len Brown4be44fc2005-08-05 00:44:28 -0400267 if (ACPI_FAILURE(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700268 if (status == AE_NOT_IMPLEMENTED) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500269 ACPI_ERROR((AE_INFO,
270 "Region %s(%X) not implemented",
271 acpi_ut_get_region_name(rgn_desc->region.
272 space_id),
273 rgn_desc->region.space_id));
Len Brown4be44fc2005-08-05 00:44:28 -0400274 } else if (status == AE_NOT_EXIST) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500275 ACPI_ERROR((AE_INFO,
276 "Region %s(%X) has no handler",
277 acpi_ut_get_region_name(rgn_desc->region.
278 space_id),
279 rgn_desc->region.space_id));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700280 }
281 }
282
Len Brown4be44fc2005-08-05 00:44:28 -0400283 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
285
Linus Torvalds1da177e2005-04-16 15:20:36 -0700286/*******************************************************************************
287 *
288 * FUNCTION: acpi_ex_register_overflow
289 *
Robert Moore44f6c012005-04-18 22:49:35 -0400290 * PARAMETERS: obj_desc - Register(Field) to be written
Linus Torvalds1da177e2005-04-16 15:20:36 -0700291 * Value - Value to be stored
292 *
293 * RETURN: TRUE if value overflows the field, FALSE otherwise
294 *
295 * DESCRIPTION: Check if a value is out of range of the field being written.
296 * Used to check if the values written to Index and Bank registers
297 * are out of range. Normally, the value is simply truncated
298 * to fit the field, but this case is most likely a serious
299 * coding error in the ASL.
300 *
301 ******************************************************************************/
302
Robert Moore44f6c012005-04-18 22:49:35 -0400303static u8
Len Brown4be44fc2005-08-05 00:44:28 -0400304acpi_ex_register_overflow(union acpi_operand_object *obj_desc,
305 acpi_integer value)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306{
307
308 if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
309 /*
310 * The field is large enough to hold the maximum integer, so we can
311 * never overflow it.
312 */
313 return (FALSE);
314 }
315
316 if (value >= ((acpi_integer) 1 << obj_desc->common_field.bit_length)) {
317 /*
318 * The Value is larger than the maximum value that can fit into
319 * the register.
320 */
321 return (TRUE);
322 }
323
324 /* The Value will fit into the field with no truncation */
325
326 return (FALSE);
327}
328
Linus Torvalds1da177e2005-04-16 15:20:36 -0700329/*******************************************************************************
330 *
331 * FUNCTION: acpi_ex_field_datum_io
332 *
Robert Moore44f6c012005-04-18 22:49:35 -0400333 * PARAMETERS: obj_desc - Field to be read
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 * field_datum_byte_offset - Byte offset of this datum within the
335 * parent field
Robert Moore44f6c012005-04-18 22:49:35 -0400336 * Value - Where to store value (must be 64 bits)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700337 * read_write - Read or Write flag
338 *
339 * RETURN: Status
340 *
341 * DESCRIPTION: Read or Write a single datum of a field. The field_type is
342 * demultiplexed here to handle the different types of fields
343 * (buffer_field, region_field, index_field, bank_field)
344 *
345 ******************************************************************************/
346
Robert Moore44f6c012005-04-18 22:49:35 -0400347static acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400348acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
349 u32 field_datum_byte_offset,
350 acpi_integer * value, u32 read_write)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700351{
Len Brown4be44fc2005-08-05 00:44:28 -0400352 acpi_status status;
353 acpi_integer local_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
Len Brown4be44fc2005-08-05 00:44:28 -0400355 ACPI_FUNCTION_TRACE_U32("ex_field_datum_io", field_datum_byte_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 if (read_write == ACPI_READ) {
358 if (!value) {
359 local_value = 0;
Robert Moore44f6c012005-04-18 22:49:35 -0400360
361 /* To support reads without saving return value */
362 value = &local_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 }
364
365 /* Clear the entire return buffer first, [Very Important!] */
366
367 *value = 0;
368 }
369
370 /*
371 * The four types of fields are:
372 *
373 * buffer_field - Read/write from/to a Buffer
374 * region_field - Read/write from/to a Operation Region.
Robert Moore44f6c012005-04-18 22:49:35 -0400375 * bank_field - Write to a Bank Register, then read/write from/to an
376 * operation_region
377 * index_field - Write to an Index Register, then read/write from/to a
378 * Data Register
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379 */
Len Brown4be44fc2005-08-05 00:44:28 -0400380 switch (ACPI_GET_OBJECT_TYPE(obj_desc)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700381 case ACPI_TYPE_BUFFER_FIELD:
382 /*
383 * If the buffer_field arguments have not been previously evaluated,
384 * evaluate them now and save the results.
385 */
386 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
Len Brown4be44fc2005-08-05 00:44:28 -0400387 status = acpi_ds_get_buffer_field_arguments(obj_desc);
388 if (ACPI_FAILURE(status)) {
389 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 }
391 }
392
393 if (read_write == ACPI_READ) {
394 /*
395 * Copy the data from the source buffer.
396 * Length is the field width in bytes.
397 */
Len Brown4be44fc2005-08-05 00:44:28 -0400398 ACPI_MEMCPY(value,
399 (obj_desc->buffer_field.buffer_obj)->buffer.
400 pointer +
401 obj_desc->buffer_field.base_byte_offset +
402 field_datum_byte_offset,
403 obj_desc->common_field.access_byte_width);
404 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 /*
406 * Copy the data to the target buffer.
407 * Length is the field width in bytes.
408 */
Len Brown4be44fc2005-08-05 00:44:28 -0400409 ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
410 pointer +
411 obj_desc->buffer_field.base_byte_offset +
412 field_datum_byte_offset, value,
413 obj_desc->common_field.access_byte_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700414 }
415
416 status = AE_OK;
417 break;
418
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 case ACPI_TYPE_LOCAL_BANK_FIELD:
420
Robert Moore44f6c012005-04-18 22:49:35 -0400421 /*
422 * Ensure that the bank_value is not beyond the capacity of
423 * the register
424 */
Len Brown4be44fc2005-08-05 00:44:28 -0400425 if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
426 (acpi_integer) obj_desc->
427 bank_field.value)) {
428 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700429 }
430
431 /*
432 * For bank_fields, we must write the bank_value to the bank_register
433 * (itself a region_field) before we can access the data.
434 */
Len Brown4be44fc2005-08-05 00:44:28 -0400435 status =
436 acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
437 &obj_desc->bank_field.value,
438 sizeof(obj_desc->bank_field.
439 value));
440 if (ACPI_FAILURE(status)) {
441 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700442 }
443
444 /*
445 * Now that the Bank has been selected, fall through to the
446 * region_field case and write the datum to the Operation Region
447 */
448
449 /*lint -fallthrough */
450
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 case ACPI_TYPE_LOCAL_REGION_FIELD:
452 /*
453 * For simple region_fields, we just directly access the owning
454 * Operation Region.
455 */
Len Brown4be44fc2005-08-05 00:44:28 -0400456 status =
457 acpi_ex_access_region(obj_desc, field_datum_byte_offset,
458 value, read_write);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 break;
460
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 case ACPI_TYPE_LOCAL_INDEX_FIELD:
462
Robert Moore44f6c012005-04-18 22:49:35 -0400463 /*
464 * Ensure that the index_value is not beyond the capacity of
465 * the register
466 */
Len Brown4be44fc2005-08-05 00:44:28 -0400467 if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
468 (acpi_integer) obj_desc->
469 index_field.value)) {
470 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700471 }
472
473 /* Write the index value to the index_register (itself a region_field) */
474
475 field_datum_byte_offset += obj_desc->index_field.value;
476
Len Brown4be44fc2005-08-05 00:44:28 -0400477 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
478 "Write to Index Register: Value %8.8X\n",
479 field_datum_byte_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480
Len Brown4be44fc2005-08-05 00:44:28 -0400481 status =
482 acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
483 &field_datum_byte_offset,
484 sizeof(field_datum_byte_offset));
485 if (ACPI_FAILURE(status)) {
486 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487 }
488
Len Brown4be44fc2005-08-05 00:44:28 -0400489 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
490 "I/O to Data Register: value_ptr %p\n",
491 value));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
493 if (read_write == ACPI_READ) {
494 /* Read the datum from the data_register */
495
Len Brown4be44fc2005-08-05 00:44:28 -0400496 status =
497 acpi_ex_extract_from_field(obj_desc->index_field.
498 data_obj, value,
499 sizeof(acpi_integer));
500 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 /* Write the datum to the data_register */
502
Len Brown4be44fc2005-08-05 00:44:28 -0400503 status =
504 acpi_ex_insert_into_field(obj_desc->index_field.
505 data_obj, value,
506 sizeof(acpi_integer));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 }
508 break;
509
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510 default:
511
Bob Mooreb8e4d892006-01-27 16:43:00 -0500512 ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %X",
513 ACPI_GET_OBJECT_TYPE(obj_desc)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514 status = AE_AML_INTERNAL;
515 break;
516 }
517
Len Brown4be44fc2005-08-05 00:44:28 -0400518 if (ACPI_SUCCESS(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 if (read_write == ACPI_READ) {
Len Brown4be44fc2005-08-05 00:44:28 -0400520 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
521 "Value Read %8.8X%8.8X, Width %d\n",
522 ACPI_FORMAT_UINT64(*value),
523 obj_desc->common_field.
524 access_byte_width));
525 } else {
526 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
527 "Value Written %8.8X%8.8X, Width %d\n",
528 ACPI_FORMAT_UINT64(*value),
529 obj_desc->common_field.
530 access_byte_width));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 }
532 }
533
Len Brown4be44fc2005-08-05 00:44:28 -0400534 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535}
536
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537/*******************************************************************************
538 *
539 * FUNCTION: acpi_ex_write_with_update_rule
540 *
Robert Moore44f6c012005-04-18 22:49:35 -0400541 * PARAMETERS: obj_desc - Field to be written
542 * Mask - bitmask within field datum
543 * field_value - Value to write
544 * field_datum_byte_offset - Offset of datum within field
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 *
546 * RETURN: Status
547 *
548 * DESCRIPTION: Apply the field update rule to a field write
549 *
550 ******************************************************************************/
551
552acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400553acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
554 acpi_integer mask,
555 acpi_integer field_value,
556 u32 field_datum_byte_offset)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557{
Len Brown4be44fc2005-08-05 00:44:28 -0400558 acpi_status status = AE_OK;
559 acpi_integer merged_value;
560 acpi_integer current_value;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561
Len Brown4be44fc2005-08-05 00:44:28 -0400562 ACPI_FUNCTION_TRACE_U32("ex_write_with_update_rule", mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700563
564 /* Start with the new bits */
565
566 merged_value = field_value;
567
568 /* If the mask is all ones, we don't need to worry about the update rule */
569
570 if (mask != ACPI_INTEGER_MAX) {
571 /* Decode the update rule */
572
Len Brown4be44fc2005-08-05 00:44:28 -0400573 switch (obj_desc->common_field.
574 field_flags & AML_FIELD_UPDATE_RULE_MASK) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575 case AML_FIELD_UPDATE_PRESERVE:
576 /*
577 * Check if update rule needs to be applied (not if mask is all
578 * ones) The left shift drops the bits we want to ignore.
579 */
Len Brown4be44fc2005-08-05 00:44:28 -0400580 if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
581 ACPI_MUL_8(obj_desc->common_field.
582 access_byte_width))) != 0) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700583 /*
584 * Read the current contents of the byte/word/dword containing
585 * the field, and merge with the new field value.
586 */
Len Brown4be44fc2005-08-05 00:44:28 -0400587 status =
588 acpi_ex_field_datum_io(obj_desc,
589 field_datum_byte_offset,
590 &current_value,
591 ACPI_READ);
592 if (ACPI_FAILURE(status)) {
593 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594 }
595
596 merged_value |= (current_value & ~mask);
597 }
598 break;
599
600 case AML_FIELD_UPDATE_WRITE_AS_ONES:
601
602 /* Set positions outside the field to all ones */
603
604 merged_value |= ~mask;
605 break;
606
607 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
608
609 /* Set positions outside the field to all zeros */
610
611 merged_value &= mask;
612 break;
613
614 default:
615
Bob Mooreb8e4d892006-01-27 16:43:00 -0500616 ACPI_ERROR((AE_INFO,
617 "Unknown update_rule value: %X",
618 (obj_desc->common_field.
619 field_flags &
620 AML_FIELD_UPDATE_RULE_MASK)));
Len Brown4be44fc2005-08-05 00:44:28 -0400621 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700622 }
623 }
624
Len Brown4be44fc2005-08-05 00:44:28 -0400625 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
626 "Mask %8.8X%8.8X, datum_offset %X, Width %X, Value %8.8X%8.8X, merged_value %8.8X%8.8X\n",
627 ACPI_FORMAT_UINT64(mask),
628 field_datum_byte_offset,
629 obj_desc->common_field.access_byte_width,
630 ACPI_FORMAT_UINT64(field_value),
631 ACPI_FORMAT_UINT64(merged_value)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632
633 /* Write the merged value */
634
Len Brown4be44fc2005-08-05 00:44:28 -0400635 status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
636 &merged_value, ACPI_WRITE);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637
Len Brown4be44fc2005-08-05 00:44:28 -0400638 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700639}
640
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641/*******************************************************************************
642 *
643 * FUNCTION: acpi_ex_extract_from_field
644 *
645 * PARAMETERS: obj_desc - Field to be read
646 * Buffer - Where to store the field data
647 * buffer_length - Length of Buffer
648 *
649 * RETURN: Status
650 *
651 * DESCRIPTION: Retrieve the current value of the given field
652 *
653 ******************************************************************************/
654
655acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400656acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
657 void *buffer, u32 buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658{
Len Brown4be44fc2005-08-05 00:44:28 -0400659 acpi_status status;
660 acpi_integer raw_datum;
661 acpi_integer merged_datum;
662 u32 field_offset = 0;
663 u32 buffer_offset = 0;
664 u32 buffer_tail_bits;
665 u32 datum_count;
666 u32 field_datum_count;
667 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700668
Len Brown4be44fc2005-08-05 00:44:28 -0400669 ACPI_FUNCTION_TRACE("ex_extract_from_field");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700670
671 /* Validate target buffer and clear it */
672
Len Brown4be44fc2005-08-05 00:44:28 -0400673 if (buffer_length <
674 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500675 ACPI_ERROR((AE_INFO,
676 "Field size %X (bits) is too large for buffer (%X)",
677 obj_desc->common_field.bit_length, buffer_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700678
Len Brown4be44fc2005-08-05 00:44:28 -0400679 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700680 }
Len Brown4be44fc2005-08-05 00:44:28 -0400681 ACPI_MEMSET(buffer, 0, buffer_length);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682
683 /* Compute the number of datums (access width data items) */
684
Len Brown4be44fc2005-08-05 00:44:28 -0400685 datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
686 obj_desc->common_field.access_bit_width);
687 field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
688 obj_desc->common_field.
689 start_field_bit_offset,
690 obj_desc->common_field.
691 access_bit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700692
693 /* Priming read from the field */
694
Len Brown4be44fc2005-08-05 00:44:28 -0400695 status =
696 acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
697 ACPI_READ);
698 if (ACPI_FAILURE(status)) {
699 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700 }
Len Brown4be44fc2005-08-05 00:44:28 -0400701 merged_datum =
702 raw_datum >> obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703
704 /* Read the rest of the field */
705
706 for (i = 1; i < field_datum_count; i++) {
707 /* Get next input datum from the field */
708
709 field_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400710 status = acpi_ex_field_datum_io(obj_desc, field_offset,
711 &raw_datum, ACPI_READ);
712 if (ACPI_FAILURE(status)) {
713 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714 }
715
716 /* Merge with previous datum if necessary */
717
718 merged_datum |= raw_datum <<
Len Brown4be44fc2005-08-05 00:44:28 -0400719 (obj_desc->common_field.access_bit_width -
720 obj_desc->common_field.start_field_bit_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700721
722 if (i == datum_count) {
723 break;
724 }
725
726 /* Write merged datum to target buffer */
727
Len Brown4be44fc2005-08-05 00:44:28 -0400728 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
729 ACPI_MIN(obj_desc->common_field.access_byte_width,
730 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700731
732 buffer_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400733 merged_datum =
734 raw_datum >> obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700735 }
736
737 /* Mask off any extra bits in the last datum */
738
Robert Moore44f6c012005-04-18 22:49:35 -0400739 buffer_tail_bits = obj_desc->common_field.bit_length %
Len Brown4be44fc2005-08-05 00:44:28 -0400740 obj_desc->common_field.access_bit_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 if (buffer_tail_bits) {
Len Brown4be44fc2005-08-05 00:44:28 -0400742 merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700743 }
744
745 /* Write the last datum to the buffer */
746
Len Brown4be44fc2005-08-05 00:44:28 -0400747 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
748 ACPI_MIN(obj_desc->common_field.access_byte_width,
749 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750
Len Brown4be44fc2005-08-05 00:44:28 -0400751 return_ACPI_STATUS(AE_OK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752}
753
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754/*******************************************************************************
755 *
756 * FUNCTION: acpi_ex_insert_into_field
757 *
758 * PARAMETERS: obj_desc - Field to be written
759 * Buffer - Data to be written
760 * buffer_length - Length of Buffer
761 *
762 * RETURN: Status
763 *
764 * DESCRIPTION: Store the Buffer contents into the given field
765 *
766 ******************************************************************************/
767
768acpi_status
Len Brown4be44fc2005-08-05 00:44:28 -0400769acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
770 void *buffer, u32 buffer_length)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771{
Len Brown4be44fc2005-08-05 00:44:28 -0400772 acpi_status status;
773 acpi_integer mask;
774 acpi_integer merged_datum;
775 acpi_integer raw_datum = 0;
776 u32 field_offset = 0;
777 u32 buffer_offset = 0;
778 u32 buffer_tail_bits;
779 u32 datum_count;
780 u32 field_datum_count;
781 u32 i;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
Len Brown4be44fc2005-08-05 00:44:28 -0400783 ACPI_FUNCTION_TRACE("ex_insert_into_field");
Linus Torvalds1da177e2005-04-16 15:20:36 -0700784
785 /* Validate input buffer */
786
Len Brown4be44fc2005-08-05 00:44:28 -0400787 if (buffer_length <
788 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
Bob Mooreb8e4d892006-01-27 16:43:00 -0500789 ACPI_ERROR((AE_INFO,
790 "Field size %X (bits) is too large for buffer (%X)",
791 obj_desc->common_field.bit_length, buffer_length));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792
Len Brown4be44fc2005-08-05 00:44:28 -0400793 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794 }
795
796 /* Compute the number of datums (access width data items) */
797
Len Brown4be44fc2005-08-05 00:44:28 -0400798 mask =
799 ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
800 datum_count =
801 ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
802 obj_desc->common_field.access_bit_width);
803 field_datum_count =
804 ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
805 obj_desc->common_field.start_field_bit_offset,
806 obj_desc->common_field.access_bit_width);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808 /* Get initial Datum from the input buffer */
809
Len Brown4be44fc2005-08-05 00:44:28 -0400810 ACPI_MEMCPY(&raw_datum, buffer,
811 ACPI_MIN(obj_desc->common_field.access_byte_width,
812 buffer_length - buffer_offset));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813
Len Brown4be44fc2005-08-05 00:44:28 -0400814 merged_datum =
815 raw_datum << obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816
817 /* Write the entire field */
818
819 for (i = 1; i < field_datum_count; i++) {
820 /* Write merged datum to the target field */
821
822 merged_datum &= mask;
Len Brown4be44fc2005-08-05 00:44:28 -0400823 status = acpi_ex_write_with_update_rule(obj_desc, mask,
824 merged_datum,
825 field_offset);
826 if (ACPI_FAILURE(status)) {
827 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700828 }
829
830 /* Start new output datum by merging with previous input datum */
831
832 field_offset += obj_desc->common_field.access_byte_width;
833 merged_datum = raw_datum >>
Len Brown4be44fc2005-08-05 00:44:28 -0400834 (obj_desc->common_field.access_bit_width -
835 obj_desc->common_field.start_field_bit_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700836 mask = ACPI_INTEGER_MAX;
837
838 if (i == datum_count) {
839 break;
840 }
841
842 /* Get the next input datum from the buffer */
843
844 buffer_offset += obj_desc->common_field.access_byte_width;
Len Brown4be44fc2005-08-05 00:44:28 -0400845 ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
846 ACPI_MIN(obj_desc->common_field.access_byte_width,
847 buffer_length - buffer_offset));
848 merged_datum |=
849 raw_datum << obj_desc->common_field.start_field_bit_offset;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 }
851
852 /* Mask off any extra bits in the last datum */
853
854 buffer_tail_bits = (obj_desc->common_field.bit_length +
Len Brown4be44fc2005-08-05 00:44:28 -0400855 obj_desc->common_field.start_field_bit_offset) %
856 obj_desc->common_field.access_bit_width;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 if (buffer_tail_bits) {
Len Brown4be44fc2005-08-05 00:44:28 -0400858 mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 }
860
861 /* Write the last datum to the field */
862
863 merged_datum &= mask;
Len Brown4be44fc2005-08-05 00:44:28 -0400864 status = acpi_ex_write_with_update_rule(obj_desc,
865 mask, merged_datum,
866 field_offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867
Len Brown4be44fc2005-08-05 00:44:28 -0400868 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700869}