1 /******************************************************************************
3 * Module Name: exfldio - Aml Field I/O
5 *****************************************************************************/
8 * Copyright (C) 2000 - 2010, 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
;
90 ACPI_FUNCTION_TRACE_U32(ex_setup_region
, field_datum_byte_offset
);
92 rgn_desc
= obj_desc
->common_field
.region_obj
;
94 /* We must have a valid region */
96 if (rgn_desc
->common
.type
!= ACPI_TYPE_REGION
) {
97 ACPI_ERROR((AE_INFO
, "Needed Region, found type %X (%s)",
98 rgn_desc
->common
.type
,
99 acpi_ut_get_object_type_name(rgn_desc
)));
101 return_ACPI_STATUS(AE_AML_OPERAND_TYPE
);
105 * If the Region Address and Length have not been previously evaluated,
106 * evaluate them now and save the results.
108 if (!(rgn_desc
->common
.flags
& AOPOBJ_DATA_VALID
)) {
109 status
= acpi_ds_get_region_arguments(rgn_desc
);
110 if (ACPI_FAILURE(status
)) {
111 return_ACPI_STATUS(status
);
115 /* Exit if Address/Length have been disallowed by the host OS */
117 if (rgn_desc
->common
.flags
& AOPOBJ_INVALID
) {
118 return_ACPI_STATUS(AE_AML_ILLEGAL_ADDRESS
);
122 * Exit now for SMBus or IPMI address space, it has a non-linear address space
123 * and the request cannot be directly validated
125 if (rgn_desc
->region
.space_id
== ACPI_ADR_SPACE_SMBUS
||
126 rgn_desc
->region
.space_id
== ACPI_ADR_SPACE_IPMI
) {
128 /* SMBus or IPMI has a non-linear address space */
130 return_ACPI_STATUS(AE_OK
);
132 #ifdef ACPI_UNDER_DEVELOPMENT
134 * If the Field access is any_acc, we can now compute the optimal
135 * access (because we know know the length of the parent region)
137 if (!(obj_desc
->common
.flags
& AOPOBJ_DATA_VALID
)) {
138 if (ACPI_FAILURE(status
)) {
139 return_ACPI_STATUS(status
);
145 * Validate the request. The entire request from the byte offset for a
146 * length of one field datum (access width) must fit within the region.
147 * (Region length is specified in bytes)
149 if (rgn_desc
->region
.length
<
150 (obj_desc
->common_field
.base_byte_offset
+
151 field_datum_byte_offset
+
152 obj_desc
->common_field
.access_byte_width
)) {
153 if (acpi_gbl_enable_interpreter_slack
) {
155 * Slack mode only: We will go ahead and allow access to this
156 * field if it is within the region length rounded up to the next
157 * access width boundary. acpi_size cast for 64-bit compile.
159 if (ACPI_ROUND_UP(rgn_desc
->region
.length
,
160 obj_desc
->common_field
.
161 access_byte_width
) >=
162 ((acpi_size
) obj_desc
->common_field
.
164 obj_desc
->common_field
.access_byte_width
+
165 field_datum_byte_offset
)) {
166 return_ACPI_STATUS(AE_OK
);
170 if (rgn_desc
->region
.length
<
171 obj_desc
->common_field
.access_byte_width
) {
173 * This is the case where the access_type (acc_word, etc.) is wider
174 * than the region itself. For example, a region of length one
175 * byte, and a field with Dword access specified.
178 "Field [%4.4s] access width (%d bytes) too large for region [%4.4s] (length %X)",
179 acpi_ut_get_node_name(obj_desc
->
181 obj_desc
->common_field
.access_byte_width
,
182 acpi_ut_get_node_name(rgn_desc
->region
.
184 rgn_desc
->region
.length
));
188 * Offset rounded up to next multiple of field width
189 * exceeds region length, indicate an error
192 "Field [%4.4s] Base+Offset+Width %X+%X+%X is beyond end of region [%4.4s] (length %X)",
193 acpi_ut_get_node_name(obj_desc
->common_field
.node
),
194 obj_desc
->common_field
.base_byte_offset
,
195 field_datum_byte_offset
,
196 obj_desc
->common_field
.access_byte_width
,
197 acpi_ut_get_node_name(rgn_desc
->region
.node
),
198 rgn_desc
->region
.length
));
200 return_ACPI_STATUS(AE_AML_REGION_LIMIT
);
203 return_ACPI_STATUS(AE_OK
);
206 /*******************************************************************************
208 * FUNCTION: acpi_ex_access_region
210 * PARAMETERS: obj_desc - Field to be read
211 * field_datum_byte_offset - Byte offset of this datum within the
213 * Value - Where to store value (must at least
215 * Function - Read or Write flag plus other region-
220 * DESCRIPTION: Read or Write a single field datum to an Operation Region.
222 ******************************************************************************/
225 acpi_ex_access_region(union acpi_operand_object
*obj_desc
,
226 u32 field_datum_byte_offset
, u64
*value
, u32 function
)
229 union acpi_operand_object
*rgn_desc
;
232 ACPI_FUNCTION_TRACE(ex_access_region
);
235 * Ensure that the region operands are fully evaluated and verify
236 * the validity of the request
238 status
= acpi_ex_setup_region(obj_desc
, field_datum_byte_offset
);
239 if (ACPI_FAILURE(status
)) {
240 return_ACPI_STATUS(status
);
244 * The physical address of this field datum is:
246 * 1) The base of the region, plus
247 * 2) The base offset of the field, plus
248 * 3) The current offset into the field
250 rgn_desc
= obj_desc
->common_field
.region_obj
;
252 obj_desc
->common_field
.base_byte_offset
+ field_datum_byte_offset
;
254 if ((function
& ACPI_IO_MASK
) == ACPI_READ
) {
255 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
, "[READ]"));
257 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
, "[WRITE]"));
260 ACPI_DEBUG_PRINT_RAW((ACPI_DB_BFIELD
,
261 " Region [%s:%X], Width %X, ByteBase %X, Offset %X at %p\n",
262 acpi_ut_get_region_name(rgn_desc
->region
.
264 rgn_desc
->region
.space_id
,
265 obj_desc
->common_field
.access_byte_width
,
266 obj_desc
->common_field
.base_byte_offset
,
267 field_datum_byte_offset
, ACPI_CAST_PTR(void,
273 /* Invoke the appropriate address_space/op_region handler */
276 acpi_ev_address_space_dispatch(rgn_desc
, function
, region_offset
,
277 ACPI_MUL_8(obj_desc
->common_field
.
281 if (ACPI_FAILURE(status
)) {
282 if (status
== AE_NOT_IMPLEMENTED
) {
284 "Region %s(%X) not implemented",
285 acpi_ut_get_region_name(rgn_desc
->region
.
287 rgn_desc
->region
.space_id
));
288 } else if (status
== AE_NOT_EXIST
) {
290 "Region %s(%X) has no handler",
291 acpi_ut_get_region_name(rgn_desc
->region
.
293 rgn_desc
->region
.space_id
));
297 return_ACPI_STATUS(status
);
300 /*******************************************************************************
302 * FUNCTION: acpi_ex_register_overflow
304 * PARAMETERS: obj_desc - Register(Field) to be written
305 * Value - Value to be stored
307 * RETURN: TRUE if value overflows the field, FALSE otherwise
309 * DESCRIPTION: Check if a value is out of range of the field being written.
310 * Used to check if the values written to Index and Bank registers
311 * are out of range. Normally, the value is simply truncated
312 * to fit the field, but this case is most likely a serious
313 * coding error in the ASL.
315 ******************************************************************************/
318 acpi_ex_register_overflow(union acpi_operand_object
*obj_desc
, u64 value
)
321 if (obj_desc
->common_field
.bit_length
>= ACPI_INTEGER_BIT_SIZE
) {
323 * The field is large enough to hold the maximum integer, so we can
329 if (value
>= ((u64
) 1 << obj_desc
->common_field
.bit_length
)) {
331 * The Value is larger than the maximum value that can fit into
337 /* The Value will fit into the field with no truncation */
342 /*******************************************************************************
344 * FUNCTION: acpi_ex_field_datum_io
346 * PARAMETERS: obj_desc - Field to be read
347 * field_datum_byte_offset - Byte offset of this datum within the
349 * Value - Where to store value (must be 64 bits)
350 * read_write - Read or Write flag
354 * DESCRIPTION: Read or Write a single datum of a field. The field_type is
355 * demultiplexed here to handle the different types of fields
356 * (buffer_field, region_field, index_field, bank_field)
358 ******************************************************************************/
361 acpi_ex_field_datum_io(union acpi_operand_object
*obj_desc
,
362 u32 field_datum_byte_offset
, u64
*value
, u32 read_write
)
367 ACPI_FUNCTION_TRACE_U32(ex_field_datum_io
, field_datum_byte_offset
);
369 if (read_write
== ACPI_READ
) {
373 /* To support reads without saving return value */
374 value
= &local_value
;
377 /* Clear the entire return buffer first, [Very Important!] */
383 * The four types of fields are:
385 * buffer_field - Read/write from/to a Buffer
386 * region_field - Read/write from/to a Operation Region.
387 * bank_field - Write to a Bank Register, then read/write from/to an
389 * index_field - Write to an Index Register, then read/write from/to a
392 switch (obj_desc
->common
.type
) {
393 case ACPI_TYPE_BUFFER_FIELD
:
395 * If the buffer_field arguments have not been previously evaluated,
396 * evaluate them now and save the results.
398 if (!(obj_desc
->common
.flags
& AOPOBJ_DATA_VALID
)) {
399 status
= acpi_ds_get_buffer_field_arguments(obj_desc
);
400 if (ACPI_FAILURE(status
)) {
401 return_ACPI_STATUS(status
);
405 if (read_write
== ACPI_READ
) {
407 * Copy the data from the source buffer.
408 * Length is the field width in bytes.
411 (obj_desc
->buffer_field
.buffer_obj
)->buffer
.
413 obj_desc
->buffer_field
.base_byte_offset
+
414 field_datum_byte_offset
,
415 obj_desc
->common_field
.access_byte_width
);
418 * Copy the data to the target buffer.
419 * Length is the field width in bytes.
421 ACPI_MEMCPY((obj_desc
->buffer_field
.buffer_obj
)->buffer
.
423 obj_desc
->buffer_field
.base_byte_offset
+
424 field_datum_byte_offset
, value
,
425 obj_desc
->common_field
.access_byte_width
);
431 case ACPI_TYPE_LOCAL_BANK_FIELD
:
434 * Ensure that the bank_value is not beyond the capacity of
437 if (acpi_ex_register_overflow(obj_desc
->bank_field
.bank_obj
,
438 (u64
) obj_desc
->bank_field
.
440 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT
);
444 * For bank_fields, we must write the bank_value to the bank_register
445 * (itself a region_field) before we can access the data.
448 acpi_ex_insert_into_field(obj_desc
->bank_field
.bank_obj
,
449 &obj_desc
->bank_field
.value
,
450 sizeof(obj_desc
->bank_field
.
452 if (ACPI_FAILURE(status
)) {
453 return_ACPI_STATUS(status
);
457 * Now that the Bank has been selected, fall through to the
458 * region_field case and write the datum to the Operation Region
461 /*lint -fallthrough */
463 case ACPI_TYPE_LOCAL_REGION_FIELD
:
465 * For simple region_fields, we just directly access the owning
469 acpi_ex_access_region(obj_desc
, field_datum_byte_offset
,
473 case ACPI_TYPE_LOCAL_INDEX_FIELD
:
476 * Ensure that the index_value is not beyond the capacity of
479 if (acpi_ex_register_overflow(obj_desc
->index_field
.index_obj
,
480 (u64
) obj_desc
->index_field
.
482 return_ACPI_STATUS(AE_AML_REGISTER_LIMIT
);
485 /* Write the index value to the index_register (itself a region_field) */
487 field_datum_byte_offset
+= obj_desc
->index_field
.value
;
489 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
,
490 "Write to Index Register: Value %8.8X\n",
491 field_datum_byte_offset
));
494 acpi_ex_insert_into_field(obj_desc
->index_field
.index_obj
,
495 &field_datum_byte_offset
,
496 sizeof(field_datum_byte_offset
));
497 if (ACPI_FAILURE(status
)) {
498 return_ACPI_STATUS(status
);
501 if (read_write
== ACPI_READ
) {
503 /* Read the datum from the data_register */
505 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
,
506 "Read from Data Register\n"));
509 acpi_ex_extract_from_field(obj_desc
->index_field
.
513 /* Write the datum to the data_register */
515 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
,
516 "Write to Data Register: Value %8.8X%8.8X\n",
517 ACPI_FORMAT_UINT64(*value
)));
520 acpi_ex_insert_into_field(obj_desc
->index_field
.
528 ACPI_ERROR((AE_INFO
, "Wrong object type in field I/O %X",
529 obj_desc
->common
.type
));
530 status
= AE_AML_INTERNAL
;
534 if (ACPI_SUCCESS(status
)) {
535 if (read_write
== ACPI_READ
) {
536 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
,
537 "Value Read %8.8X%8.8X, Width %d\n",
538 ACPI_FORMAT_UINT64(*value
),
539 obj_desc
->common_field
.
542 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
,
543 "Value Written %8.8X%8.8X, Width %d\n",
544 ACPI_FORMAT_UINT64(*value
),
545 obj_desc
->common_field
.
550 return_ACPI_STATUS(status
);
553 /*******************************************************************************
555 * FUNCTION: acpi_ex_write_with_update_rule
557 * PARAMETERS: obj_desc - Field to be written
558 * Mask - bitmask within field datum
559 * field_value - Value to write
560 * field_datum_byte_offset - Offset of datum within field
564 * DESCRIPTION: Apply the field update rule to a field write
566 ******************************************************************************/
569 acpi_ex_write_with_update_rule(union acpi_operand_object
*obj_desc
,
571 u64 field_value
, u32 field_datum_byte_offset
)
573 acpi_status status
= AE_OK
;
577 ACPI_FUNCTION_TRACE_U32(ex_write_with_update_rule
, mask
);
579 /* Start with the new bits */
581 merged_value
= field_value
;
583 /* If the mask is all ones, we don't need to worry about the update rule */
585 if (mask
!= ACPI_UINT64_MAX
) {
587 /* Decode the update rule */
589 switch (obj_desc
->common_field
.
590 field_flags
& AML_FIELD_UPDATE_RULE_MASK
) {
591 case AML_FIELD_UPDATE_PRESERVE
:
593 * Check if update rule needs to be applied (not if mask is all
594 * ones) The left shift drops the bits we want to ignore.
596 if ((~mask
<< (ACPI_MUL_8(sizeof(mask
)) -
597 ACPI_MUL_8(obj_desc
->common_field
.
598 access_byte_width
))) != 0) {
600 * Read the current contents of the byte/word/dword containing
601 * the field, and merge with the new field value.
604 acpi_ex_field_datum_io(obj_desc
,
605 field_datum_byte_offset
,
608 if (ACPI_FAILURE(status
)) {
609 return_ACPI_STATUS(status
);
612 merged_value
|= (current_value
& ~mask
);
616 case AML_FIELD_UPDATE_WRITE_AS_ONES
:
618 /* Set positions outside the field to all ones */
620 merged_value
|= ~mask
;
623 case AML_FIELD_UPDATE_WRITE_AS_ZEROS
:
625 /* Set positions outside the field to all zeros */
627 merged_value
&= mask
;
633 "Unknown UpdateRule value: %X",
634 (obj_desc
->common_field
.
636 AML_FIELD_UPDATE_RULE_MASK
)));
637 return_ACPI_STATUS(AE_AML_OPERAND_VALUE
);
641 ACPI_DEBUG_PRINT((ACPI_DB_BFIELD
,
642 "Mask %8.8X%8.8X, DatumOffset %X, Width %X, Value %8.8X%8.8X, MergedValue %8.8X%8.8X\n",
643 ACPI_FORMAT_UINT64(mask
),
644 field_datum_byte_offset
,
645 obj_desc
->common_field
.access_byte_width
,
646 ACPI_FORMAT_UINT64(field_value
),
647 ACPI_FORMAT_UINT64(merged_value
)));
649 /* Write the merged value */
651 status
= acpi_ex_field_datum_io(obj_desc
, field_datum_byte_offset
,
652 &merged_value
, ACPI_WRITE
);
654 return_ACPI_STATUS(status
);
657 /*******************************************************************************
659 * FUNCTION: acpi_ex_extract_from_field
661 * PARAMETERS: obj_desc - Field to be read
662 * Buffer - Where to store the field data
663 * buffer_length - Length of Buffer
667 * DESCRIPTION: Retrieve the current value of the given field
669 ******************************************************************************/
672 acpi_ex_extract_from_field(union acpi_operand_object
*obj_desc
,
673 void *buffer
, u32 buffer_length
)
678 u32 field_offset
= 0;
679 u32 buffer_offset
= 0;
680 u32 buffer_tail_bits
;
682 u32 field_datum_count
;
685 ACPI_FUNCTION_TRACE(ex_extract_from_field
);
687 /* Validate target buffer and clear it */
690 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc
->common_field
.bit_length
)) {
692 "Field size %X (bits) is too large for buffer (%X)",
693 obj_desc
->common_field
.bit_length
, buffer_length
));
695 return_ACPI_STATUS(AE_BUFFER_OVERFLOW
);
697 ACPI_MEMSET(buffer
, 0, buffer_length
);
699 /* Compute the number of datums (access width data items) */
701 datum_count
= ACPI_ROUND_UP_TO(obj_desc
->common_field
.bit_length
,
702 obj_desc
->common_field
.access_bit_width
);
703 field_datum_count
= ACPI_ROUND_UP_TO(obj_desc
->common_field
.bit_length
+
704 obj_desc
->common_field
.
705 start_field_bit_offset
,
706 obj_desc
->common_field
.
709 /* Priming read from the field */
712 acpi_ex_field_datum_io(obj_desc
, field_offset
, &raw_datum
,
714 if (ACPI_FAILURE(status
)) {
715 return_ACPI_STATUS(status
);
718 raw_datum
>> obj_desc
->common_field
.start_field_bit_offset
;
720 /* Read the rest of the field */
722 for (i
= 1; i
< field_datum_count
; i
++) {
724 /* Get next input datum from the field */
726 field_offset
+= obj_desc
->common_field
.access_byte_width
;
727 status
= acpi_ex_field_datum_io(obj_desc
, field_offset
,
728 &raw_datum
, ACPI_READ
);
729 if (ACPI_FAILURE(status
)) {
730 return_ACPI_STATUS(status
);
734 * Merge with previous datum if necessary.
736 * Note: Before the shift, check if the shift value will be larger than
737 * the integer size. If so, there is no need to perform the operation.
738 * This avoids the differences in behavior between different compilers
739 * concerning shift values larger than the target data width.
741 if ((obj_desc
->common_field
.access_bit_width
-
742 obj_desc
->common_field
.start_field_bit_offset
) <
743 ACPI_INTEGER_BIT_SIZE
) {
745 raw_datum
<< (obj_desc
->common_field
.
747 obj_desc
->common_field
.
748 start_field_bit_offset
);
751 if (i
== datum_count
) {
755 /* Write merged datum to target buffer */
757 ACPI_MEMCPY(((char *)buffer
) + buffer_offset
, &merged_datum
,
758 ACPI_MIN(obj_desc
->common_field
.access_byte_width
,
759 buffer_length
- buffer_offset
));
761 buffer_offset
+= obj_desc
->common_field
.access_byte_width
;
763 raw_datum
>> obj_desc
->common_field
.start_field_bit_offset
;
766 /* Mask off any extra bits in the last datum */
768 buffer_tail_bits
= obj_desc
->common_field
.bit_length
%
769 obj_desc
->common_field
.access_bit_width
;
770 if (buffer_tail_bits
) {
771 merged_datum
&= ACPI_MASK_BITS_ABOVE(buffer_tail_bits
);
774 /* Write the last datum to the buffer */
776 ACPI_MEMCPY(((char *)buffer
) + buffer_offset
, &merged_datum
,
777 ACPI_MIN(obj_desc
->common_field
.access_byte_width
,
778 buffer_length
- buffer_offset
));
780 return_ACPI_STATUS(AE_OK
);
783 /*******************************************************************************
785 * FUNCTION: acpi_ex_insert_into_field
787 * PARAMETERS: obj_desc - Field to be written
788 * Buffer - Data to be written
789 * buffer_length - Length of Buffer
793 * DESCRIPTION: Store the Buffer contents into the given field
795 ******************************************************************************/
798 acpi_ex_insert_into_field(union acpi_operand_object
*obj_desc
,
799 void *buffer
, u32 buffer_length
)
806 u32 field_offset
= 0;
807 u32 buffer_offset
= 0;
808 u32 buffer_tail_bits
;
810 u32 field_datum_count
;
815 ACPI_FUNCTION_TRACE(ex_insert_into_field
);
817 /* Validate input buffer */
821 ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc
->common_field
.bit_length
);
823 * We must have a buffer that is at least as long as the field
824 * we are writing to. This is because individual fields are
825 * indivisible and partial writes are not supported -- as per
826 * the ACPI specification.
828 if (buffer_length
< required_length
) {
830 /* We need to create a new buffer */
832 new_buffer
= ACPI_ALLOCATE_ZEROED(required_length
);
834 return_ACPI_STATUS(AE_NO_MEMORY
);
838 * Copy the original data to the new buffer, starting
839 * at Byte zero. All unused (upper) bytes of the
842 ACPI_MEMCPY((char *)new_buffer
, (char *)buffer
, buffer_length
);
844 buffer_length
= required_length
;
848 * Create the bitmasks used for bit insertion.
849 * Note: This if/else is used to bypass compiler differences with the
852 if (obj_desc
->common_field
.access_bit_width
== ACPI_INTEGER_BIT_SIZE
) {
853 width_mask
= ACPI_UINT64_MAX
;
856 ACPI_MASK_BITS_ABOVE(obj_desc
->common_field
.
861 ACPI_MASK_BITS_BELOW(obj_desc
->common_field
.start_field_bit_offset
);
863 /* Compute the number of datums (access width data items) */
865 datum_count
= ACPI_ROUND_UP_TO(obj_desc
->common_field
.bit_length
,
866 obj_desc
->common_field
.access_bit_width
);
868 field_datum_count
= ACPI_ROUND_UP_TO(obj_desc
->common_field
.bit_length
+
869 obj_desc
->common_field
.
870 start_field_bit_offset
,
871 obj_desc
->common_field
.
874 /* Get initial Datum from the input buffer */
876 ACPI_MEMCPY(&raw_datum
, buffer
,
877 ACPI_MIN(obj_desc
->common_field
.access_byte_width
,
878 buffer_length
- buffer_offset
));
881 raw_datum
<< obj_desc
->common_field
.start_field_bit_offset
;
883 /* Write the entire field */
885 for (i
= 1; i
< field_datum_count
; i
++) {
887 /* Write merged datum to the target field */
889 merged_datum
&= mask
;
890 status
= acpi_ex_write_with_update_rule(obj_desc
, mask
,
893 if (ACPI_FAILURE(status
)) {
897 field_offset
+= obj_desc
->common_field
.access_byte_width
;
900 * Start new output datum by merging with previous input datum
903 * Note: Before the shift, check if the shift value will be larger than
904 * the integer size. If so, there is no need to perform the operation.
905 * This avoids the differences in behavior between different compilers
906 * concerning shift values larger than the target data width.
908 if ((obj_desc
->common_field
.access_bit_width
-
909 obj_desc
->common_field
.start_field_bit_offset
) <
910 ACPI_INTEGER_BIT_SIZE
) {
912 raw_datum
>> (obj_desc
->common_field
.
914 obj_desc
->common_field
.
915 start_field_bit_offset
);
922 if (i
== datum_count
) {
926 /* Get the next input datum from the buffer */
928 buffer_offset
+= obj_desc
->common_field
.access_byte_width
;
929 ACPI_MEMCPY(&raw_datum
, ((char *)buffer
) + buffer_offset
,
930 ACPI_MIN(obj_desc
->common_field
.access_byte_width
,
931 buffer_length
- buffer_offset
));
933 raw_datum
<< obj_desc
->common_field
.start_field_bit_offset
;
936 /* Mask off any extra bits in the last datum */
938 buffer_tail_bits
= (obj_desc
->common_field
.bit_length
+
939 obj_desc
->common_field
.start_field_bit_offset
) %
940 obj_desc
->common_field
.access_bit_width
;
941 if (buffer_tail_bits
) {
942 mask
&= ACPI_MASK_BITS_ABOVE(buffer_tail_bits
);
945 /* Write the last datum to the field */
947 merged_datum
&= mask
;
948 status
= acpi_ex_write_with_update_rule(obj_desc
,
953 /* Free temporary buffer if we used one */
956 ACPI_FREE(new_buffer
);
958 return_ACPI_STATUS(status
);