1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2004, R. Byron Moore
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
45 #include <acpi/acpi.h>
46 #include <acpi/acinterp.h>
47 #include <acpi/amlcode.h>
48 #include <acpi/acevents.h>
49 #include <acpi/acdispat.h>
52 #define _COMPONENT ACPI_EXECUTER
53 ACPI_MODULE_NAME ("exfldio")
56 /*******************************************************************************
58 * FUNCTION: acpi_ex_setup_region
60 * PARAMETERS: *obj_desc - Field to be read or written
61 * field_datum_byte_offset - Byte offset of this datum within the
66 * DESCRIPTION: Common processing for acpi_ex_extract_from_field and
67 * acpi_ex_insert_into_field. Initialize the Region if necessary and
68 * validate the request.
70 ******************************************************************************/
73 acpi_ex_setup_region (
74 union acpi_operand_object *obj_desc,
75 u32 field_datum_byte_offset)
77 acpi_status status = AE_OK;
78 union acpi_operand_object *rgn_desc;
81 ACPI_FUNCTION_TRACE_U32 ("ex_setup_region", field_datum_byte_offset);
84 rgn_desc = obj_desc->common_field.region_obj;
86 /* We must have a valid region */
88 if (ACPI_GET_OBJECT_TYPE (rgn_desc) != ACPI_TYPE_REGION) {
89 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR, "Needed Region, found type %X (%s)\n",
90 ACPI_GET_OBJECT_TYPE (rgn_desc),
91 acpi_ut_get_object_type_name (rgn_desc)));
93 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
97 * If the Region Address and Length have not been previously evaluated,
98 * evaluate them now and save the results.
100 if (!(rgn_desc->common.flags & AOPOBJ_DATA_VALID)) {
101 status = acpi_ds_get_region_arguments (rgn_desc);
102 if (ACPI_FAILURE (status)) {
103 return_ACPI_STATUS (status);
107 if (rgn_desc->region.space_id == ACPI_ADR_SPACE_SMBUS) {
108 /* SMBus has a non-linear address space */
110 return_ACPI_STATUS (AE_OK);
113 #ifdef ACPI_UNDER_DEVELOPMENT
115 * If the Field access is any_acc, we can now compute the optimal
116 * access (because we know know the length of the parent region)
118 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
119 if (ACPI_FAILURE (status)) {
120 return_ACPI_STATUS (status);
126 * Validate the request. The entire request from the byte offset for a
127 * length of one field datum (access width) must fit within the region.
128 * (Region length is specified in bytes)
130 if (rgn_desc->region.length < (obj_desc->common_field.base_byte_offset
131 + field_datum_byte_offset
132 + obj_desc->common_field.access_byte_width)) {
133 if (rgn_desc->region.length < obj_desc->common_field.access_byte_width) {
135 * This is the case where the access_type (acc_word, etc.) is wider
136 * than the region itself. For example, a region of length one
137 * byte, and a field with Dword access specified.
139 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
140 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)\n",
141 acpi_ut_get_node_name (obj_desc->common_field.node),
142 obj_desc->common_field.access_byte_width,
143 acpi_ut_get_node_name (rgn_desc->region.node), rgn_desc->region.length));
147 * Offset rounded up to next multiple of field width
148 * exceeds region length, indicate an error
150 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
151 "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)\n",
152 acpi_ut_get_node_name (obj_desc->common_field.node),
153 obj_desc->common_field.base_byte_offset,
154 field_datum_byte_offset, obj_desc->common_field.access_byte_width,
155 acpi_ut_get_node_name (rgn_desc->region.node), rgn_desc->region.length));
159 * Allow access to the field if it is within the region size
160 * rounded up to a multiple of the access byte width. This
161 * overcomes "off-by-one" programming errors in the AML often
162 * found in Toshiba laptops. These errors were allowed by
163 * the Microsoft ASL compiler.
165 u32 rounded_length = ACPI_ROUND_UP(rgn_desc->region.length,
166 obj_desc->common_field.access_byte_width);
168 if (rounded_length < (obj_desc->common_field.base_byte_offset
169 + field_datum_byte_offset
170 + obj_desc->common_field.access_byte_width)) {
171 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
173 static int warn_once = 1;
175 // Could also associate a flag with each field, and
176 // warn once for each field.
177 ACPI_REPORT_WARNING((
178 "The ACPI AML in your computer contains errors, "
179 "please nag the manufacturer to correct it.\n"));
180 ACPI_REPORT_WARNING((
181 "Allowing relaxed access to fields; "
182 "turn on CONFIG_ACPI_DEBUG for details.\n"));
185 return_ACPI_STATUS (AE_OK);
189 return_ACPI_STATUS (AE_AML_REGION_LIMIT);
193 return_ACPI_STATUS (AE_OK);
197 /*******************************************************************************
199 * FUNCTION: acpi_ex_access_region
201 * PARAMETERS: *obj_desc - Field to be read
202 * field_datum_byte_offset - Byte offset of this datum within the
204 * *Value - Where to store value (must at least
205 * the size of acpi_integer)
206 * Function - Read or Write flag plus other region-
211 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
213 ******************************************************************************/
216 acpi_ex_access_region (
217 union acpi_operand_object *obj_desc,
218 u32 field_datum_byte_offset,
223 union acpi_operand_object *rgn_desc;
224 acpi_physical_address address;
227 ACPI_FUNCTION_TRACE ("ex_access_region");
231 * Ensure that the region operands are fully evaluated and verify
232 * the validity of the request
234 status = acpi_ex_setup_region (obj_desc, field_datum_byte_offset);
235 if (ACPI_FAILURE (status)) {
236 return_ACPI_STATUS (status);
240 * The physical address of this field datum is:
242 * 1) The base of the region, plus
243 * 2) The base offset of the field, plus
244 * 3) The current offset into the field
246 rgn_desc = obj_desc->common_field.region_obj;
247 address = rgn_desc->region.address
248 + obj_desc->common_field.base_byte_offset
249 + field_datum_byte_offset;
251 if ((function & ACPI_IO_MASK) == ACPI_READ) {
252 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[READ]"));
255 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "[WRITE]"));
258 ACPI_DEBUG_PRINT_RAW ((ACPI_DB_BFIELD,
259 " Region [%s:%X], Width %X, byte_base %X, Offset %X at %8.8X%8.8X\n",
260 acpi_ut_get_region_name (rgn_desc->region.space_id),
261 rgn_desc->region.space_id,
262 obj_desc->common_field.access_byte_width,
263 obj_desc->common_field.base_byte_offset,
264 field_datum_byte_offset,
265 ACPI_FORMAT_UINT64 (address)));
267 /* Invoke the appropriate address_space/op_region handler */
269 status = acpi_ev_address_space_dispatch (rgn_desc, function,
270 address, ACPI_MUL_8 (obj_desc->common_field.access_byte_width), value);
272 if (ACPI_FAILURE (status)) {
273 if (status == AE_NOT_IMPLEMENTED) {
274 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
275 "Region %s(%X) not implemented\n",
276 acpi_ut_get_region_name (rgn_desc->region.space_id),
277 rgn_desc->region.space_id));
279 else if (status == AE_NOT_EXIST) {
280 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
281 "Region %s(%X) has no handler\n",
282 acpi_ut_get_region_name (rgn_desc->region.space_id),
283 rgn_desc->region.space_id));
287 return_ACPI_STATUS (status);
291 /*******************************************************************************
293 * FUNCTION: acpi_ex_register_overflow
295 * PARAMETERS: *obj_desc - Register(Field) to be written
296 * Value - Value to be stored
298 * RETURN: TRUE if value overflows the field, FALSE otherwise
300 * DESCRIPTION: Check if a value is out of range of the field being written.
301 * Used to check if the values written to Index and Bank registers
302 * are out of range. Normally, the value is simply truncated
303 * to fit the field, but this case is most likely a serious
304 * coding error in the ASL.
306 ******************************************************************************/
309 acpi_ex_register_overflow (
310 union acpi_operand_object *obj_desc,
314 if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
316 * The field is large enough to hold the maximum integer, so we can
322 if (value >= ((acpi_integer) 1 << obj_desc->common_field.bit_length)) {
324 * The Value is larger than the maximum value that can fit into
330 /* The Value will fit into the field with no truncation */
336 /*******************************************************************************
338 * FUNCTION: acpi_ex_field_datum_io
340 * PARAMETERS: *obj_desc - Field to be read
341 * field_datum_byte_offset - Byte offset of this datum within the
343 * *Value - Where to store value (must be 64 bits)
344 * read_write - Read or Write flag
348 * DESCRIPTION: Read or Write a single datum of a field. The field_type is
349 * demultiplexed here to handle the different types of fields
350 * (buffer_field, region_field, index_field, bank_field)
352 ******************************************************************************/
355 acpi_ex_field_datum_io (
356 union acpi_operand_object *obj_desc,
357 u32 field_datum_byte_offset,
362 acpi_integer local_value;
365 ACPI_FUNCTION_TRACE_U32 ("ex_field_datum_io", field_datum_byte_offset);
368 if (read_write == ACPI_READ) {
371 value = &local_value; /* To support reads without saving return value */
374 /* Clear the entire return buffer first, [Very Important!] */
380 * The four types of fields are:
382 * buffer_field - Read/write from/to a Buffer
383 * region_field - Read/write from/to a Operation Region.
384 * bank_field - Write to a Bank Register, then read/write from/to an op_region
385 * index_field - Write to an Index Register, then read/write from/to a Data Register
387 switch (ACPI_GET_OBJECT_TYPE (obj_desc)) {
388 case ACPI_TYPE_BUFFER_FIELD:
390 * If the buffer_field arguments have not been previously evaluated,
391 * evaluate them now and save the results.
393 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
394 status = acpi_ds_get_buffer_field_arguments (obj_desc);
395 if (ACPI_FAILURE (status)) {
396 return_ACPI_STATUS (status);
400 if (read_write == ACPI_READ) {
402 * Copy the data from the source buffer.
403 * Length is the field width in bytes.
405 ACPI_MEMCPY (value, (obj_desc->buffer_field.buffer_obj)->buffer.pointer
406 + obj_desc->buffer_field.base_byte_offset
407 + field_datum_byte_offset,
408 obj_desc->common_field.access_byte_width);
412 * Copy the data to the target buffer.
413 * Length is the field width in bytes.
415 ACPI_MEMCPY ((obj_desc->buffer_field.buffer_obj)->buffer.pointer
416 + obj_desc->buffer_field.base_byte_offset
417 + field_datum_byte_offset,
418 value, obj_desc->common_field.access_byte_width);
425 case ACPI_TYPE_LOCAL_BANK_FIELD:
427 /* Ensure that the bank_value is not beyond the capacity of the register */
429 if (acpi_ex_register_overflow (obj_desc->bank_field.bank_obj,
430 (acpi_integer) obj_desc->bank_field.value)) {
431 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
435 * For bank_fields, we must write the bank_value to the bank_register
436 * (itself a region_field) before we can access the data.
438 status = acpi_ex_insert_into_field (obj_desc->bank_field.bank_obj,
439 &obj_desc->bank_field.value,
440 sizeof (obj_desc->bank_field.value));
441 if (ACPI_FAILURE (status)) {
442 return_ACPI_STATUS (status);
446 * Now that the Bank has been selected, fall through to the
447 * region_field case and write the datum to the Operation Region
450 /*lint -fallthrough */
453 case ACPI_TYPE_LOCAL_REGION_FIELD:
455 * For simple region_fields, we just directly access the owning
458 status = acpi_ex_access_region (obj_desc, field_datum_byte_offset, value,
463 case ACPI_TYPE_LOCAL_INDEX_FIELD:
466 /* Ensure that the index_value is not beyond the capacity of the register */
468 if (acpi_ex_register_overflow (obj_desc->index_field.index_obj,
469 (acpi_integer) obj_desc->index_field.value)) {
470 return_ACPI_STATUS (AE_AML_REGISTER_LIMIT);
473 /* Write the index value to the index_register (itself a region_field) */
475 field_datum_byte_offset += obj_desc->index_field.value;
477 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
478 "Write to Index Register: Value %8.8X\n",
479 field_datum_byte_offset));
481 status = acpi_ex_insert_into_field (obj_desc->index_field.index_obj,
482 &field_datum_byte_offset,
483 sizeof (field_datum_byte_offset));
484 if (ACPI_FAILURE (status)) {
485 return_ACPI_STATUS (status);
488 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
489 "I/O to Data Register: value_ptr %p\n",
492 if (read_write == ACPI_READ) {
493 /* Read the datum from the data_register */
495 status = acpi_ex_extract_from_field (obj_desc->index_field.data_obj,
496 value, sizeof (acpi_integer));
499 /* Write the datum to the data_register */
501 status = acpi_ex_insert_into_field (obj_desc->index_field.data_obj,
502 value, sizeof (acpi_integer));
509 ACPI_REPORT_ERROR (("Wrong object type in field I/O %X\n",
510 ACPI_GET_OBJECT_TYPE (obj_desc)));
511 status = AE_AML_INTERNAL;
515 if (ACPI_SUCCESS (status)) {
516 if (read_write == ACPI_READ) {
517 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Read %8.8X%8.8X, Width %d\n",
518 ACPI_FORMAT_UINT64 (*value),
519 obj_desc->common_field.access_byte_width));
522 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD, "Value Written %8.8X%8.8X, Width %d\n",
523 ACPI_FORMAT_UINT64 (*value),
524 obj_desc->common_field.access_byte_width));
528 return_ACPI_STATUS (status);
532 /*******************************************************************************
534 * FUNCTION: acpi_ex_write_with_update_rule
536 * PARAMETERS: *obj_desc - Field to be set
537 * Value - Value to store
541 * DESCRIPTION: Apply the field update rule to a field write
543 ******************************************************************************/
546 acpi_ex_write_with_update_rule (
547 union acpi_operand_object *obj_desc,
549 acpi_integer field_value,
550 u32 field_datum_byte_offset)
552 acpi_status status = AE_OK;
553 acpi_integer merged_value;
554 acpi_integer current_value;
557 ACPI_FUNCTION_TRACE_U32 ("ex_write_with_update_rule", mask);
560 /* Start with the new bits */
562 merged_value = field_value;
564 /* If the mask is all ones, we don't need to worry about the update rule */
566 if (mask != ACPI_INTEGER_MAX) {
567 /* Decode the update rule */
569 switch (obj_desc->common_field.field_flags & AML_FIELD_UPDATE_RULE_MASK) {
570 case AML_FIELD_UPDATE_PRESERVE:
572 * Check if update rule needs to be applied (not if mask is all
573 * ones) The left shift drops the bits we want to ignore.
575 if ((~mask << (ACPI_MUL_8 (sizeof (mask)) -
576 ACPI_MUL_8 (obj_desc->common_field.access_byte_width))) != 0) {
578 * Read the current contents of the byte/word/dword containing
579 * the field, and merge with the new field value.
581 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
582 ¤t_value, ACPI_READ);
583 if (ACPI_FAILURE (status)) {
584 return_ACPI_STATUS (status);
587 merged_value |= (current_value & ~mask);
591 case AML_FIELD_UPDATE_WRITE_AS_ONES:
593 /* Set positions outside the field to all ones */
595 merged_value |= ~mask;
598 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
600 /* Set positions outside the field to all zeros */
602 merged_value &= mask;
607 ACPI_DEBUG_PRINT ((ACPI_DB_ERROR,
608 "write_with_update_rule: Unknown update_rule setting: %X\n",
609 (obj_desc->common_field.field_flags & AML_FIELD_UPDATE_RULE_MASK)));
610 return_ACPI_STATUS (AE_AML_OPERAND_VALUE);
614 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
615 "Mask %8.8X%8.8X, datum_offset %X, Width %X, Value %8.8X%8.8X, merged_value %8.8X%8.8X\n",
616 ACPI_FORMAT_UINT64 (mask),
617 field_datum_byte_offset,
618 obj_desc->common_field.access_byte_width,
619 ACPI_FORMAT_UINT64 (field_value),
620 ACPI_FORMAT_UINT64 (merged_value)));
622 /* Write the merged value */
624 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
625 &merged_value, ACPI_WRITE);
627 return_ACPI_STATUS (status);
631 /*******************************************************************************
633 * FUNCTION: acpi_ex_get_buffer_datum
635 * PARAMETERS: Datum - Where the Datum is returned
636 * Buffer - Raw field buffer
637 * buffer_length - Entire length (used for big-endian only)
638 * byte_granularity - 1/2/4/8 Granularity of the field
640 * buffer_offset - Datum offset into the buffer
644 * DESCRIPTION: Get a datum from the buffer according to the buffer field
647 ******************************************************************************/
650 acpi_ex_get_buffer_datum (
654 u32 byte_granularity,
660 ACPI_FUNCTION_TRACE_U32 ("ex_get_buffer_datum", byte_granularity);
663 /* Get proper index into buffer (handles big/little endian) */
665 index = ACPI_BUFFER_INDEX (buffer_length, buffer_offset, byte_granularity);
667 /* Move the requested number of bytes */
669 switch (byte_granularity) {
670 case ACPI_FIELD_BYTE_GRANULARITY:
672 *datum = ((u8 *) buffer) [index];
675 case ACPI_FIELD_WORD_GRANULARITY:
677 ACPI_MOVE_16_TO_64 (datum, &(((u16 *) buffer) [index]));
680 case ACPI_FIELD_DWORD_GRANULARITY:
682 ACPI_MOVE_32_TO_64 (datum, &(((u32 *) buffer) [index]));
685 case ACPI_FIELD_QWORD_GRANULARITY:
687 ACPI_MOVE_64_TO_64 (datum, &(((u64 *) buffer) [index]));
691 /* Should not get here */
699 /*******************************************************************************
701 * FUNCTION: acpi_ex_set_buffer_datum
703 * PARAMETERS: merged_datum - Value to store
704 * Buffer - Receiving buffer
705 * buffer_length - Entire length (used for big-endian only)
706 * byte_granularity - 1/2/4/8 Granularity of the field
708 * buffer_offset - Datum offset into the buffer
712 * DESCRIPTION: Store the merged datum to the buffer according to the
715 ******************************************************************************/
718 acpi_ex_set_buffer_datum (
719 acpi_integer merged_datum,
722 u32 byte_granularity,
728 ACPI_FUNCTION_TRACE_U32 ("ex_set_buffer_datum", byte_granularity);
731 /* Get proper index into buffer (handles big/little endian) */
733 index = ACPI_BUFFER_INDEX (buffer_length, buffer_offset, byte_granularity);
735 /* Move the requested number of bytes */
737 switch (byte_granularity) {
738 case ACPI_FIELD_BYTE_GRANULARITY:
740 ((u8 *) buffer) [index] = (u8) merged_datum;
743 case ACPI_FIELD_WORD_GRANULARITY:
745 ACPI_MOVE_64_TO_16 (&(((u16 *) buffer)[index]), &merged_datum);
748 case ACPI_FIELD_DWORD_GRANULARITY:
750 ACPI_MOVE_64_TO_32 (&(((u32 *) buffer)[index]), &merged_datum);
753 case ACPI_FIELD_QWORD_GRANULARITY:
755 ACPI_MOVE_64_TO_64 (&(((u64 *) buffer)[index]), &merged_datum);
759 /* Should not get here */
767 /*******************************************************************************
769 * FUNCTION: acpi_ex_extract_from_field
771 * PARAMETERS: *obj_desc - Field to be read
772 * *Value - Where to store value
776 * DESCRIPTION: Retrieve the value of the given field
778 ******************************************************************************/
781 acpi_ex_extract_from_field (
782 union acpi_operand_object *obj_desc,
787 u32 field_datum_byte_offset;
788 u32 buffer_datum_offset;
789 acpi_integer previous_raw_datum = 0;
790 acpi_integer this_raw_datum = 0;
791 acpi_integer merged_datum = 0;
792 u32 byte_field_length;
797 ACPI_FUNCTION_TRACE ("ex_extract_from_field");
801 * The field must fit within the caller's buffer
803 byte_field_length = ACPI_ROUND_BITS_UP_TO_BYTES (obj_desc->common_field.bit_length);
804 if (byte_field_length > buffer_length) {
805 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
806 "Field size %X (bytes) too large for buffer (%X)\n",
807 byte_field_length, buffer_length));
809 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
812 /* Convert field byte count to datum count, round up if necessary */
814 datum_count = ACPI_ROUND_UP_TO (byte_field_length,
815 obj_desc->common_field.access_byte_width);
818 * If the field is not aligned on a datum boundary and does not
819 * fit within a single datum, we must read an extra datum.
821 * We could just split the aligned and non-aligned cases since the
822 * aligned case is so very simple, but this would require more code.
824 if ((obj_desc->common_field.end_field_valid_bits != 0) &&
825 (!(obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM))) {
829 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
830 "byte_len %X, datum_len %X, byte_gran %X\n",
831 byte_field_length, datum_count,obj_desc->common_field.access_byte_width));
834 * Clear the caller's buffer (the whole buffer length as given)
835 * This is very important, especially in the cases where the buffer
836 * is longer than the size of the field.
838 ACPI_MEMSET (buffer, 0, buffer_length);
840 field_datum_byte_offset = 0;
841 buffer_datum_offset= 0;
843 /* Read the entire field */
845 for (i = 0; i < datum_count; i++) {
846 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
847 &this_raw_datum, ACPI_READ);
848 if (ACPI_FAILURE (status)) {
849 return_ACPI_STATUS (status);
852 /* We might actually be done if the request fits in one datum */
854 if ((datum_count == 1) &&
855 (obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM)) {
856 /* 1) Shift the valid data bits down to start at bit 0 */
858 merged_datum = (this_raw_datum >> obj_desc->common_field.start_field_bit_offset);
860 /* 2) Mask off any upper unused bits (bits not part of the field) */
862 if (obj_desc->common_field.end_buffer_valid_bits) {
863 merged_datum &= ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_buffer_valid_bits);
866 /* Store the datum to the caller buffer */
868 acpi_ex_set_buffer_datum (merged_datum, buffer, buffer_length,
869 obj_desc->common_field.access_byte_width, buffer_datum_offset);
871 return_ACPI_STATUS (AE_OK);
874 /* Special handling for the last datum to ignore extra bits */
876 if ((i >= (datum_count -1)) &&
877 (obj_desc->common_field.end_field_valid_bits)) {
879 * This is the last iteration of the loop. We need to clear
880 * any unused bits (bits that are not part of this field) before
881 * we store the final merged datum into the caller buffer.
884 ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_field_valid_bits);
888 * Create the (possibly) merged datum to be stored to the caller buffer
890 if (obj_desc->common_field.start_field_bit_offset == 0) {
891 /* Field is not skewed and we can just copy the datum */
893 acpi_ex_set_buffer_datum (this_raw_datum, buffer, buffer_length,
894 obj_desc->common_field.access_byte_width, buffer_datum_offset);
895 buffer_datum_offset++;
898 /* Not aligned -- on the first iteration, just save the datum */
902 * Put together the appropriate bits of the two raw data to make a
903 * single complete field datum
905 * 1) Normalize the first datum down to bit 0
907 merged_datum = (previous_raw_datum >> obj_desc->common_field.start_field_bit_offset);
909 /* 2) Insert the second datum "above" the first datum */
911 merged_datum |= (this_raw_datum << obj_desc->common_field.datum_valid_bits);
913 acpi_ex_set_buffer_datum (merged_datum, buffer, buffer_length,
914 obj_desc->common_field.access_byte_width, buffer_datum_offset);
915 buffer_datum_offset++;
919 * Save the raw datum that was just acquired since it may contain bits
920 * of the *next* field datum
922 previous_raw_datum = this_raw_datum;
925 field_datum_byte_offset += obj_desc->common_field.access_byte_width;
928 /* For non-aligned case, there is one last datum to insert */
930 if (obj_desc->common_field.start_field_bit_offset != 0) {
931 merged_datum = (this_raw_datum >> obj_desc->common_field.start_field_bit_offset);
933 acpi_ex_set_buffer_datum (merged_datum, buffer, buffer_length,
934 obj_desc->common_field.access_byte_width, buffer_datum_offset);
937 return_ACPI_STATUS (AE_OK);
941 /*******************************************************************************
943 * FUNCTION: acpi_ex_insert_into_field
945 * PARAMETERS: *obj_desc - Field to be set
946 * Buffer - Value to store
950 * DESCRIPTION: Store the value into the given field
952 ******************************************************************************/
955 acpi_ex_insert_into_field (
956 union acpi_operand_object *obj_desc,
961 u32 field_datum_byte_offset;
964 acpi_integer merged_datum;
965 acpi_integer previous_raw_datum;
966 acpi_integer this_raw_datum;
967 u32 byte_field_length;
971 ACPI_FUNCTION_TRACE ("ex_insert_into_field");
975 * Incoming buffer must be at least as long as the field, we do not
976 * allow "partial" field writes. We do not care if the buffer is
977 * larger than the field, this typically happens when an integer is
978 * written to a field that is actually smaller than an integer.
980 byte_field_length = ACPI_ROUND_BITS_UP_TO_BYTES (
981 obj_desc->common_field.bit_length);
982 if (buffer_length < byte_field_length) {
983 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
984 "Buffer length %X too small for field %X\n",
985 buffer_length, byte_field_length));
987 return_ACPI_STATUS (AE_BUFFER_OVERFLOW);
990 byte_field_length = ACPI_ROUND_BITS_UP_TO_BYTES (
991 obj_desc->common_field.start_field_bit_offset +
992 obj_desc->common_field.bit_length);
994 /* Convert byte count to datum count, round up if necessary */
996 datum_count = ACPI_ROUND_UP_TO (byte_field_length,
997 obj_desc->common_field.access_byte_width);
999 ACPI_DEBUG_PRINT ((ACPI_DB_BFIELD,
1000 "Bytes %X, Datums %X, byte_gran %X\n",
1001 byte_field_length, datum_count, obj_desc->common_field.access_byte_width));
1004 * Break the request into up to three parts (similar to an I/O request):
1005 * 1) non-aligned part at start
1006 * 2) aligned part in middle
1007 * 3) non-aligned part at the end
1009 field_datum_byte_offset = 0;
1012 /* Get a single datum from the caller's buffer */
1014 acpi_ex_get_buffer_datum (&previous_raw_datum, buffer, buffer_length,
1015 obj_desc->common_field.access_byte_width, datum_offset);
1019 * Write a partial field datum if field does not begin on a datum boundary
1020 * Note: The code in this section also handles the aligned case
1022 * Construct Mask with 1 bits where the field is, 0 bits elsewhere
1023 * (Only the bottom 5 bits of bit_length are valid for a shift operation)
1025 * Mask off bits that are "below" the field (if any)
1027 mask = ACPI_MASK_BITS_BELOW (obj_desc->common_field.start_field_bit_offset);
1029 /* If the field fits in one datum, may need to mask upper bits */
1031 if ((obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM) &&
1032 obj_desc->common_field.end_field_valid_bits) {
1033 /* There are bits above the field, mask them off also */
1035 mask &= ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_field_valid_bits);
1038 /* Shift and mask the value into the field position */
1040 merged_datum = (previous_raw_datum << obj_desc->common_field.start_field_bit_offset);
1041 merged_datum &= mask;
1043 /* Apply the update rule (if necessary) and write the datum to the field */
1045 status = acpi_ex_write_with_update_rule (obj_desc, mask, merged_datum,
1046 field_datum_byte_offset);
1047 if (ACPI_FAILURE (status)) {
1048 return_ACPI_STATUS (status);
1051 /* We just wrote the first datum */
1055 /* If the entire field fits within one datum, we are done. */
1057 if ((datum_count == 1) &&
1058 (obj_desc->common_field.flags & AOPOBJ_SINGLE_DATUM)) {
1059 return_ACPI_STATUS (AE_OK);
1064 * Write the aligned data.
1066 * We don't need to worry about the update rule for these data, because
1067 * all of the bits in each datum are part of the field.
1069 * The last datum must be special cased because it might contain bits
1070 * that are not part of the field -- therefore the "update rule" must be
1071 * applied in Part3 below.
1073 while (datum_offset < datum_count) {
1074 field_datum_byte_offset += obj_desc->common_field.access_byte_width;
1077 * Get the next raw buffer datum. It may contain bits of the previous
1080 acpi_ex_get_buffer_datum (&this_raw_datum, buffer, buffer_length,
1081 obj_desc->common_field.access_byte_width, datum_offset);
1083 /* Create the field datum based on the field alignment */
1085 if (obj_desc->common_field.start_field_bit_offset != 0) {
1087 * Put together appropriate bits of the two raw buffer data to make
1088 * a single complete field datum
1091 (previous_raw_datum >> obj_desc->common_field.datum_valid_bits) |
1092 (this_raw_datum << obj_desc->common_field.start_field_bit_offset);
1095 /* Field began aligned on datum boundary */
1097 merged_datum = this_raw_datum;
1101 * Special handling for the last datum if the field does NOT end on
1102 * a datum boundary. Update Rule must be applied to the bits outside
1106 if ((datum_offset == datum_count) &&
1107 (obj_desc->common_field.end_field_valid_bits)) {
1109 * If there are dangling non-aligned bits, perform one more merged write
1110 * Else - field is aligned at the end, no need for any more writes
1115 * This is the last datum and the field does not end on a datum boundary.
1116 * Build the partial datum and write with the update rule.
1118 * Mask off the unused bits above (after) the end-of-field
1120 mask = ACPI_MASK_BITS_ABOVE (obj_desc->common_field.end_field_valid_bits);
1121 merged_datum &= mask;
1123 /* Write the last datum with the update rule */
1125 status = acpi_ex_write_with_update_rule (obj_desc, mask, merged_datum,
1126 field_datum_byte_offset);
1127 if (ACPI_FAILURE (status)) {
1128 return_ACPI_STATUS (status);
1132 /* Normal (aligned) case -- write the completed datum */
1134 status = acpi_ex_field_datum_io (obj_desc, field_datum_byte_offset,
1135 &merged_datum, ACPI_WRITE);
1136 if (ACPI_FAILURE (status)) {
1137 return_ACPI_STATUS (status);
1142 * Save the most recent datum since it may contain bits of the *next*
1143 * field datum. Update current byte offset.
1145 previous_raw_datum = this_raw_datum;
1148 return_ACPI_STATUS (status);