1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2011, Intel Corp.
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.
44 #include <acpi/acpi.h>
51 #define _COMPONENT ACPI_EXECUTER
52 ACPI_MODULE_NAME("exfldio")
54 /* Local prototypes */
56 acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
57 u32 field_datum_byte_offset,
58 u64 *value, u32 read_write);
61 acpi_ex_register_overflow(union acpi_operand_object *obj_desc, u64 value);
64 acpi_ex_setup_region(union acpi_operand_object *obj_desc,
65 u32 field_datum_byte_offset);
67 /*******************************************************************************
69 * FUNCTION: acpi_ex_setup_region
71 * PARAMETERS: obj_desc - Field to be read or written
72 * field_datum_byte_offset - Byte offset of this datum within the
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.
81 ******************************************************************************/
84 acpi_ex_setup_region(union acpi_operand_object *obj_desc,
85 u32 field_datum_byte_offset)
87 acpi_status status = AE_OK;
88 union acpi_operand_object *rgn_desc;
91 ACPI_FUNCTION_TRACE_U32(ex_setup_region, field_datum_byte_offset);
93 rgn_desc = obj_desc->common_field.region_obj;
95 /* We must have a valid region */
97 if (rgn_desc->common.type != ACPI_TYPE_REGION) {
98 ACPI_ERROR((AE_INFO, "Needed Region, found type 0x%X (%s)",
99 rgn_desc->common.type,
100 acpi_ut_get_object_type_name(rgn_desc)));
102 return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
105 space_id = rgn_desc->region.space_id;
107 /* Validate the Space ID */
109 if (!acpi_is_valid_space_id(space_id)) {
111 "Invalid/unknown Address Space ID: 0x%2.2X",
113 return_ACPI_STATUS(AE_AML_INVALID_SPACE_ID);
117 * If the Region Address and Length have not been previously evaluated,
118 * evaluate them now and save the results.
120 if (!(rgn_desc->common.flags & AOPOBJ_DATA_VALID)) {
121 status = acpi_ds_get_region_arguments(rgn_desc);
122 if (ACPI_FAILURE(status)) {
123 return_ACPI_STATUS(status);
127 /* Exit if Address/Length have been disallowed by the host OS */
129 if (rgn_desc->common.flags & AOPOBJ_INVALID) {
130 return_ACPI_STATUS(AE_AML_ILLEGAL_ADDRESS);
134 * Exit now for SMBus or IPMI address space, it has a non-linear
135 * address space and the request cannot be directly validated
137 if (space_id == ACPI_ADR_SPACE_SMBUS ||
138 space_id == ACPI_ADR_SPACE_IPMI) {
140 /* SMBus or IPMI has a non-linear address space */
142 return_ACPI_STATUS(AE_OK);
144 #ifdef ACPI_UNDER_DEVELOPMENT
146 * If the Field access is any_acc, we can now compute the optimal
147 * access (because we know know the length of the parent region)
149 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
150 if (ACPI_FAILURE(status)) {
151 return_ACPI_STATUS(status);
157 * Validate the request. The entire request from the byte offset for a
158 * length of one field datum (access width) must fit within the region.
159 * (Region length is specified in bytes)
161 if (rgn_desc->region.length <
162 (obj_desc->common_field.base_byte_offset + field_datum_byte_offset +
163 obj_desc->common_field.access_byte_width)) {
164 if (acpi_gbl_enable_interpreter_slack) {
166 * Slack mode only: We will go ahead and allow access to this
167 * field if it is within the region length rounded up to the next
168 * access width boundary. acpi_size cast for 64-bit compile.
170 if (ACPI_ROUND_UP(rgn_desc->region.length,
171 obj_desc->common_field.
172 access_byte_width) >=
173 ((acpi_size) obj_desc->common_field.
175 obj_desc->common_field.access_byte_width +
176 field_datum_byte_offset)) {
177 return_ACPI_STATUS(AE_OK);
181 if (rgn_desc->region.length <
182 obj_desc->common_field.access_byte_width) {
184 * This is the case where the access_type (acc_word, etc.) is wider
185 * than the region itself. For example, a region of length one
186 * byte, and a field with Dword access specified.
189 "Field [%4.4s] access width (%u bytes) too large for region [%4.4s] (length %u)",
190 acpi_ut_get_node_name(obj_desc->
192 obj_desc->common_field.access_byte_width,
193 acpi_ut_get_node_name(rgn_desc->region.
195 rgn_desc->region.length));
199 * Offset rounded up to next multiple of field width
200 * exceeds region length, indicate an error
203 "Field [%4.4s] Base+Offset+Width %u+%u+%u is beyond end of region [%4.4s] (length %u)",
204 acpi_ut_get_node_name(obj_desc->common_field.node),
205 obj_desc->common_field.base_byte_offset,
206 field_datum_byte_offset,
207 obj_desc->common_field.access_byte_width,
208 acpi_ut_get_node_name(rgn_desc->region.node),
209 rgn_desc->region.length));
211 return_ACPI_STATUS(AE_AML_REGION_LIMIT);
214 return_ACPI_STATUS(AE_OK);
217 /*******************************************************************************
219 * FUNCTION: acpi_ex_access_region
221 * PARAMETERS: obj_desc - Field to be read
222 * field_datum_byte_offset - Byte offset of this datum within the
224 * Value - Where to store value (must at least
226 * Function - Read or Write flag plus other region-
231 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
233 ******************************************************************************/
236 acpi_ex_access_region(union acpi_operand_object *obj_desc,
237 u32 field_datum_byte_offset, u64 *value, u32 function)
240 union acpi_operand_object *rgn_desc;
243 ACPI_FUNCTION_TRACE(ex_access_region);
246 * Ensure that the region operands are fully evaluated and verify
247 * the validity of the request
249 status = acpi_ex_setup_region(obj_desc, field_datum_byte_offset);
250 if (ACPI_FAILURE(status)) {
251 return_ACPI_STATUS(status);
255 * The physical address of this field datum is:
257 * 1) The base of the region, plus
258 * 2) The base offset of the field, plus
259 * 3) The current offset into the field
261 rgn_desc = obj_desc->common_field.region_obj;
263 obj_desc->common_field.base_byte_offset + field_datum_byte_offset;
265 if ((function & ACPI_IO_MASK) == ACPI_READ) {
266 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[READ]"));
268 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD, "[WRITE]"));
271 ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD,
272 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
273 acpi_ut_get_region_name(rgn_desc->region.
275 rgn_desc->region.space_id,
276 obj_desc->common_field.access_byte_width,
277 obj_desc->common_field.base_byte_offset,
278 field_datum_byte_offset, ACPI_CAST_PTR(void,
284 /* Invoke the appropriate address_space/op_region handler */
286 status = acpi_ev_address_space_dispatch(rgn_desc, obj_desc,
287 function, region_offset,
288 ACPI_MUL_8(obj_desc->
293 if (ACPI_FAILURE(status)) {
294 if (status == AE_NOT_IMPLEMENTED) {
296 "Region %s (ID=%u) not implemented",
297 acpi_ut_get_region_name(rgn_desc->region.
299 rgn_desc->region.space_id));
300 } else if (status == AE_NOT_EXIST) {
302 "Region %s (ID=%u) has no handler",
303 acpi_ut_get_region_name(rgn_desc->region.
305 rgn_desc->region.space_id));
309 return_ACPI_STATUS(status);
312 /*******************************************************************************
314 * FUNCTION: acpi_ex_register_overflow
316 * PARAMETERS: obj_desc - Register(Field) to be written
317 * Value - Value to be stored
319 * RETURN: TRUE if value overflows the field, FALSE otherwise
321 * DESCRIPTION: Check if a value is out of range of the field being written.
322 * Used to check if the values written to Index and Bank registers
323 * are out of range. Normally, the value is simply truncated
324 * to fit the field, but this case is most likely a serious
325 * coding error in the ASL.
327 ******************************************************************************/
330 acpi_ex_register_overflow(union acpi_operand_object *obj_desc, u64 value)
332 ACPI_FUNCTION_NAME(ex_register_overflow);
334 if (obj_desc->common_field.bit_length >= ACPI_INTEGER_BIT_SIZE) {
336 * The field is large enough to hold the maximum integer, so we can
342 if (value >= ((u64) 1 << obj_desc->common_field.bit_length)) {
344 * The Value is larger than the maximum value that can fit into
348 "Index value 0x%8.8X%8.8X overflows field width 0x%X",
349 ACPI_FORMAT_UINT64(value),
350 obj_desc->common_field.bit_length));
355 /* The Value will fit into the field with no truncation */
360 /*******************************************************************************
362 * FUNCTION: acpi_ex_field_datum_io
364 * PARAMETERS: obj_desc - Field to be read
365 * field_datum_byte_offset - Byte offset of this datum within the
367 * Value - Where to store value (must be 64 bits)
368 * read_write - Read or Write flag
372 * DESCRIPTION: Read or Write a single datum of a field. The field_type is
373 * demultiplexed here to handle the different types of fields
374 * (buffer_field, region_field, index_field, bank_field)
376 ******************************************************************************/
379 acpi_ex_field_datum_io(union acpi_operand_object *obj_desc,
380 u32 field_datum_byte_offset, u64 *value, u32 read_write)
385 ACPI_FUNCTION_TRACE_U32(ex_field_datum_io, field_datum_byte_offset);
387 if (read_write == ACPI_READ) {
391 /* To support reads without saving return value */
392 value = &local_value;
395 /* Clear the entire return buffer first, [Very Important!] */
401 * The four types of fields are:
403 * buffer_field - Read/write from/to a Buffer
404 * region_field - Read/write from/to a Operation Region.
405 * bank_field - Write to a Bank Register, then read/write from/to an
407 * index_field - Write to an Index Register, then read/write from/to a
410 switch (obj_desc->common.type) {
411 case ACPI_TYPE_BUFFER_FIELD:
413 * If the buffer_field arguments have not been previously evaluated,
414 * evaluate them now and save the results.
416 if (!(obj_desc->common.flags & AOPOBJ_DATA_VALID)) {
417 status = acpi_ds_get_buffer_field_arguments(obj_desc);
418 if (ACPI_FAILURE(status)) {
419 return_ACPI_STATUS(status);
423 if (read_write == ACPI_READ) {
425 * Copy the data from the source buffer.
426 * Length is the field width in bytes.
429 (obj_desc->buffer_field.buffer_obj)->buffer.
431 obj_desc->buffer_field.base_byte_offset +
432 field_datum_byte_offset,
433 obj_desc->common_field.access_byte_width);
436 * Copy the data to the target buffer.
437 * Length is the field width in bytes.
439 ACPI_MEMCPY((obj_desc->buffer_field.buffer_obj)->buffer.
441 obj_desc->buffer_field.base_byte_offset +
442 field_datum_byte_offset, value,
443 obj_desc->common_field.access_byte_width);
449 case ACPI_TYPE_LOCAL_BANK_FIELD:
452 * Ensure that the bank_value is not beyond the capacity of
455 if (acpi_ex_register_overflow(obj_desc->bank_field.bank_obj,
456 (u64) obj_desc->bank_field.
458 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
462 * For bank_fields, we must write the bank_value to the bank_register
463 * (itself a region_field) before we can access the data.
466 acpi_ex_insert_into_field(obj_desc->bank_field.bank_obj,
467 &obj_desc->bank_field.value,
468 sizeof(obj_desc->bank_field.
470 if (ACPI_FAILURE(status)) {
471 return_ACPI_STATUS(status);
475 * Now that the Bank has been selected, fall through to the
476 * region_field case and write the datum to the Operation Region
479 /*lint -fallthrough */
481 case ACPI_TYPE_LOCAL_REGION_FIELD:
483 * For simple region_fields, we just directly access the owning
487 acpi_ex_access_region(obj_desc, field_datum_byte_offset,
491 case ACPI_TYPE_LOCAL_INDEX_FIELD:
494 * Ensure that the index_value is not beyond the capacity of
497 if (acpi_ex_register_overflow(obj_desc->index_field.index_obj,
498 (u64) obj_desc->index_field.
500 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT);
503 /* Write the index value to the index_register (itself a region_field) */
505 field_datum_byte_offset += obj_desc->index_field.value;
507 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
508 "Write to Index Register: Value %8.8X\n",
509 field_datum_byte_offset));
512 acpi_ex_insert_into_field(obj_desc->index_field.index_obj,
513 &field_datum_byte_offset,
514 sizeof(field_datum_byte_offset));
515 if (ACPI_FAILURE(status)) {
516 return_ACPI_STATUS(status);
519 if (read_write == ACPI_READ) {
521 /* Read the datum from the data_register */
523 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
524 "Read from Data Register\n"));
527 acpi_ex_extract_from_field(obj_desc->index_field.
531 /* Write the datum to the data_register */
533 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
534 "Write to Data Register: Value %8.8X%8.8X\n",
535 ACPI_FORMAT_UINT64(*value)));
538 acpi_ex_insert_into_field(obj_desc->index_field.
546 ACPI_ERROR((AE_INFO, "Wrong object type in field I/O %u",
547 obj_desc->common.type));
548 status = AE_AML_INTERNAL;
552 if (ACPI_SUCCESS(status)) {
553 if (read_write == ACPI_READ) {
554 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
555 "Value Read %8.8X%8.8X, Width %u\n",
556 ACPI_FORMAT_UINT64(*value),
557 obj_desc->common_field.
560 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
561 "Value Written %8.8X%8.8X, Width %u\n",
562 ACPI_FORMAT_UINT64(*value),
563 obj_desc->common_field.
568 return_ACPI_STATUS(status);
571 /*******************************************************************************
573 * FUNCTION: acpi_ex_write_with_update_rule
575 * PARAMETERS: obj_desc - Field to be written
576 * Mask - bitmask within field datum
577 * field_value - Value to write
578 * field_datum_byte_offset - Offset of datum within field
582 * DESCRIPTION: Apply the field update rule to a field write
584 ******************************************************************************/
587 acpi_ex_write_with_update_rule(union acpi_operand_object *obj_desc,
589 u64 field_value, u32 field_datum_byte_offset)
591 acpi_status status = AE_OK;
595 ACPI_FUNCTION_TRACE_U32(ex_write_with_update_rule, mask);
597 /* Start with the new bits */
599 merged_value = field_value;
601 /* If the mask is all ones, we don't need to worry about the update rule */
603 if (mask != ACPI_UINT64_MAX) {
605 /* Decode the update rule */
607 switch (obj_desc->common_field.
608 field_flags & AML_FIELD_UPDATE_RULE_MASK) {
609 case AML_FIELD_UPDATE_PRESERVE:
611 * Check if update rule needs to be applied (not if mask is all
612 * ones) The left shift drops the bits we want to ignore.
614 if ((~mask << (ACPI_MUL_8(sizeof(mask)) -
615 ACPI_MUL_8(obj_desc->common_field.
616 access_byte_width))) != 0) {
618 * Read the current contents of the byte/word/dword containing
619 * the field, and merge with the new field value.
622 acpi_ex_field_datum_io(obj_desc,
623 field_datum_byte_offset,
626 if (ACPI_FAILURE(status)) {
627 return_ACPI_STATUS(status);
630 merged_value |= (current_value & ~mask);
634 case AML_FIELD_UPDATE_WRITE_AS_ONES:
636 /* Set positions outside the field to all ones */
638 merged_value |= ~mask;
641 case AML_FIELD_UPDATE_WRITE_AS_ZEROS:
643 /* Set positions outside the field to all zeros */
645 merged_value &= mask;
651 "Unknown UpdateRule value: 0x%X",
652 (obj_desc->common_field.
654 AML_FIELD_UPDATE_RULE_MASK)));
655 return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
659 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
660 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
661 ACPI_FORMAT_UINT64(mask),
662 field_datum_byte_offset,
663 obj_desc->common_field.access_byte_width,
664 ACPI_FORMAT_UINT64(field_value),
665 ACPI_FORMAT_UINT64(merged_value)));
667 /* Write the merged value */
669 status = acpi_ex_field_datum_io(obj_desc, field_datum_byte_offset,
670 &merged_value, ACPI_WRITE);
672 return_ACPI_STATUS(status);
675 /*******************************************************************************
677 * FUNCTION: acpi_ex_extract_from_field
679 * PARAMETERS: obj_desc - Field to be read
680 * Buffer - Where to store the field data
681 * buffer_length - Length of Buffer
685 * DESCRIPTION: Retrieve the current value of the given field
687 ******************************************************************************/
690 acpi_ex_extract_from_field(union acpi_operand_object *obj_desc,
691 void *buffer, u32 buffer_length)
696 u32 field_offset = 0;
697 u32 buffer_offset = 0;
698 u32 buffer_tail_bits;
700 u32 field_datum_count;
701 u32 access_bit_width;
704 ACPI_FUNCTION_TRACE(ex_extract_from_field);
706 /* Validate target buffer and clear it */
709 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length)) {
711 "Field size %u (bits) is too large for buffer (%u)",
712 obj_desc->common_field.bit_length, buffer_length));
714 return_ACPI_STATUS(AE_BUFFER_OVERFLOW);
717 ACPI_MEMSET(buffer, 0, buffer_length);
718 access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
720 /* Handle the simple case here */
722 if ((obj_desc->common_field.start_field_bit_offset == 0) &&
723 (obj_desc->common_field.bit_length == access_bit_width)) {
724 status = acpi_ex_field_datum_io(obj_desc, 0, buffer, ACPI_READ);
725 return_ACPI_STATUS(status);
728 /* TBD: Move to common setup code */
730 /* Field algorithm is limited to sizeof(u64), truncate if needed */
732 if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
733 obj_desc->common_field.access_byte_width = sizeof(u64);
734 access_bit_width = sizeof(u64) * 8;
737 /* Compute the number of datums (access width data items) */
740 ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
743 field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
744 obj_desc->common_field.
745 start_field_bit_offset,
748 /* Priming read from the field */
751 acpi_ex_field_datum_io(obj_desc, field_offset, &raw_datum,
753 if (ACPI_FAILURE(status)) {
754 return_ACPI_STATUS(status);
757 raw_datum >> obj_desc->common_field.start_field_bit_offset;
759 /* Read the rest of the field */
761 for (i = 1; i < field_datum_count; i++) {
763 /* Get next input datum from the field */
765 field_offset += obj_desc->common_field.access_byte_width;
766 status = acpi_ex_field_datum_io(obj_desc, field_offset,
767 &raw_datum, ACPI_READ);
768 if (ACPI_FAILURE(status)) {
769 return_ACPI_STATUS(status);
773 * Merge with previous datum if necessary.
775 * Note: Before the shift, check if the shift value will be larger than
776 * the integer size. If so, there is no need to perform the operation.
777 * This avoids the differences in behavior between different compilers
778 * concerning shift values larger than the target data width.
780 if (access_bit_width -
781 obj_desc->common_field.start_field_bit_offset <
782 ACPI_INTEGER_BIT_SIZE) {
784 raw_datum << (access_bit_width -
785 obj_desc->common_field.
786 start_field_bit_offset);
789 if (i == datum_count) {
793 /* Write merged datum to target buffer */
795 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
796 ACPI_MIN(obj_desc->common_field.access_byte_width,
797 buffer_length - buffer_offset));
799 buffer_offset += obj_desc->common_field.access_byte_width;
801 raw_datum >> obj_desc->common_field.start_field_bit_offset;
804 /* Mask off any extra bits in the last datum */
806 buffer_tail_bits = obj_desc->common_field.bit_length % access_bit_width;
807 if (buffer_tail_bits) {
808 merged_datum &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
811 /* Write the last datum to the buffer */
813 ACPI_MEMCPY(((char *)buffer) + buffer_offset, &merged_datum,
814 ACPI_MIN(obj_desc->common_field.access_byte_width,
815 buffer_length - buffer_offset));
817 return_ACPI_STATUS(AE_OK);
820 /*******************************************************************************
822 * FUNCTION: acpi_ex_insert_into_field
824 * PARAMETERS: obj_desc - Field to be written
825 * Buffer - Data to be written
826 * buffer_length - Length of Buffer
830 * DESCRIPTION: Store the Buffer contents into the given field
832 ******************************************************************************/
835 acpi_ex_insert_into_field(union acpi_operand_object *obj_desc,
836 void *buffer, u32 buffer_length)
844 u32 field_offset = 0;
845 u32 buffer_offset = 0;
846 u32 buffer_tail_bits;
848 u32 field_datum_count;
849 u32 access_bit_width;
853 ACPI_FUNCTION_TRACE(ex_insert_into_field);
855 /* Validate input buffer */
859 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.bit_length);
861 * We must have a buffer that is at least as long as the field
862 * we are writing to. This is because individual fields are
863 * indivisible and partial writes are not supported -- as per
864 * the ACPI specification.
866 if (buffer_length < required_length) {
868 /* We need to create a new buffer */
870 new_buffer = ACPI_ALLOCATE_ZEROED(required_length);
872 return_ACPI_STATUS(AE_NO_MEMORY);
876 * Copy the original data to the new buffer, starting
877 * at Byte zero. All unused (upper) bytes of the
880 ACPI_MEMCPY((char *)new_buffer, (char *)buffer, buffer_length);
882 buffer_length = required_length;
885 /* TBD: Move to common setup code */
887 /* Algo is limited to sizeof(u64), so cut the access_byte_width */
888 if (obj_desc->common_field.access_byte_width > sizeof(u64)) {
889 obj_desc->common_field.access_byte_width = sizeof(u64);
892 access_bit_width = ACPI_MUL_8(obj_desc->common_field.access_byte_width);
895 * Create the bitmasks used for bit insertion.
896 * Note: This if/else is used to bypass compiler differences with the
899 if (access_bit_width == ACPI_INTEGER_BIT_SIZE) {
900 width_mask = ACPI_UINT64_MAX;
902 width_mask = ACPI_MASK_BITS_ABOVE(access_bit_width);
906 ACPI_MASK_BITS_BELOW(obj_desc->common_field.start_field_bit_offset);
908 /* Compute the number of datums (access width data items) */
910 datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length,
913 field_datum_count = ACPI_ROUND_UP_TO(obj_desc->common_field.bit_length +
914 obj_desc->common_field.
915 start_field_bit_offset,
918 /* Get initial Datum from the input buffer */
920 ACPI_MEMCPY(&raw_datum, buffer,
921 ACPI_MIN(obj_desc->common_field.access_byte_width,
922 buffer_length - buffer_offset));
925 raw_datum << obj_desc->common_field.start_field_bit_offset;
927 /* Write the entire field */
929 for (i = 1; i < field_datum_count; i++) {
931 /* Write merged datum to the target field */
933 merged_datum &= mask;
934 status = acpi_ex_write_with_update_rule(obj_desc, mask,
937 if (ACPI_FAILURE(status)) {
941 field_offset += obj_desc->common_field.access_byte_width;
944 * Start new output datum by merging with previous input datum
947 * Note: Before the shift, check if the shift value will be larger than
948 * the integer size. If so, there is no need to perform the operation.
949 * This avoids the differences in behavior between different compilers
950 * concerning shift values larger than the target data width.
952 if ((access_bit_width -
953 obj_desc->common_field.start_field_bit_offset) <
954 ACPI_INTEGER_BIT_SIZE) {
956 raw_datum >> (access_bit_width -
957 obj_desc->common_field.
958 start_field_bit_offset);
965 if (i == datum_count) {
969 /* Get the next input datum from the buffer */
971 buffer_offset += obj_desc->common_field.access_byte_width;
972 ACPI_MEMCPY(&raw_datum, ((char *)buffer) + buffer_offset,
973 ACPI_MIN(obj_desc->common_field.access_byte_width,
974 buffer_length - buffer_offset));
977 raw_datum << obj_desc->common_field.start_field_bit_offset;
980 /* Mask off any extra bits in the last datum */
982 buffer_tail_bits = (obj_desc->common_field.bit_length +
983 obj_desc->common_field.start_field_bit_offset) %
985 if (buffer_tail_bits) {
986 mask &= ACPI_MASK_BITS_ABOVE(buffer_tail_bits);
989 /* Write the last datum to the field */
991 merged_datum &= mask;
992 status = acpi_ex_write_with_update_rule(obj_desc,
997 /* Free temporary buffer if we used one */
1000 ACPI_FREE(new_buffer);
1002 return_ACPI_STATUS(status);