xref: /linux/drivers/acpi/acpica/exprep.c (revision 11b3125073d16929403d3aa7b2ae6a482060a937)
195857638SErik Schmauss // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
295b482a8SLen Brown /******************************************************************************
395b482a8SLen Brown  *
41fad8738SBob Moore  * Module Name: exprep - ACPI AML field prep utilities
595b482a8SLen Brown  *
6612c2932SBob Moore  * Copyright (C) 2000 - 2023, Intel Corp.
795b482a8SLen Brown  *
895857638SErik Schmauss  *****************************************************************************/
995b482a8SLen Brown 
1095b482a8SLen Brown #include <acpi/acpi.h>
11e2f7a777SLen Brown #include "accommon.h"
12e2f7a777SLen Brown #include "acinterp.h"
13e2f7a777SLen Brown #include "amlcode.h"
14e2f7a777SLen Brown #include "acnamesp.h"
159ce81784SBob Moore #include "acdispat.h"
1695b482a8SLen Brown 
1795b482a8SLen Brown #define _COMPONENT          ACPI_EXECUTER
1895b482a8SLen Brown ACPI_MODULE_NAME("exprep")
1995b482a8SLen Brown 
2095b482a8SLen Brown /* Local prototypes */
2195b482a8SLen Brown static u32
2295b482a8SLen Brown acpi_ex_decode_field_access(union acpi_operand_object *obj_desc,
2395b482a8SLen Brown 			    u8 field_flags, u32 * return_byte_alignment);
2495b482a8SLen Brown 
2595b482a8SLen Brown #ifdef ACPI_UNDER_DEVELOPMENT
2695b482a8SLen Brown 
2795b482a8SLen Brown static u32
2895b482a8SLen Brown acpi_ex_generate_access(u32 field_bit_offset,
2995b482a8SLen Brown 			u32 field_bit_length, u32 region_length);
3095b482a8SLen Brown 
3195b482a8SLen Brown /*******************************************************************************
3295b482a8SLen Brown  *
3395b482a8SLen Brown  * FUNCTION:    acpi_ex_generate_access
3495b482a8SLen Brown  *
3595b482a8SLen Brown  * PARAMETERS:  field_bit_offset    - Start of field within parent region/buffer
3695b482a8SLen Brown  *              field_bit_length    - Length of field in bits
3795b482a8SLen Brown  *              region_length       - Length of parent in bytes
3895b482a8SLen Brown  *
3995b482a8SLen Brown  * RETURN:      Field granularity (8, 16, 32 or 64) and
4095b482a8SLen Brown  *              byte_alignment (1, 2, 3, or 4)
4195b482a8SLen Brown  *
4295b482a8SLen Brown  * DESCRIPTION: Generate an optimal access width for fields defined with the
4395b482a8SLen Brown  *              any_acc keyword.
4495b482a8SLen Brown  *
4595b482a8SLen Brown  * NOTE: Need to have the region_length in order to check for boundary
4695b482a8SLen Brown  *       conditions (end-of-region). However, the region_length is a deferred
4795b482a8SLen Brown  *       operation. Therefore, to complete this implementation, the generation
4895b482a8SLen Brown  *       of this access width must be deferred until the region length has
4995b482a8SLen Brown  *       been evaluated.
5095b482a8SLen Brown  *
5195b482a8SLen Brown  ******************************************************************************/
5295b482a8SLen Brown 
5395b482a8SLen Brown static u32
acpi_ex_generate_access(u32 field_bit_offset,u32 field_bit_length,u32 region_length)5495b482a8SLen Brown acpi_ex_generate_access(u32 field_bit_offset,
5595b482a8SLen Brown 			u32 field_bit_length, u32 region_length)
5695b482a8SLen Brown {
5795b482a8SLen Brown 	u32 field_byte_length;
5895b482a8SLen Brown 	u32 field_byte_offset;
5995b482a8SLen Brown 	u32 field_byte_end_offset;
6095b482a8SLen Brown 	u32 access_byte_width;
6195b482a8SLen Brown 	u32 field_start_offset;
6295b482a8SLen Brown 	u32 field_end_offset;
6395b482a8SLen Brown 	u32 minimum_access_width = 0xFFFFFFFF;
6495b482a8SLen Brown 	u32 minimum_accesses = 0xFFFFFFFF;
6595b482a8SLen Brown 	u32 accesses;
6695b482a8SLen Brown 
6795b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ex_generate_access);
6895b482a8SLen Brown 
6995b482a8SLen Brown 	/* Round Field start offset and length to "minimal" byte boundaries */
7095b482a8SLen Brown 
7195b482a8SLen Brown 	field_byte_offset = ACPI_DIV_8(ACPI_ROUND_DOWN(field_bit_offset, 8));
721fad8738SBob Moore 
731fad8738SBob Moore 	field_byte_end_offset =
741fad8738SBob Moore 	    ACPI_DIV_8(ACPI_ROUND_UP(field_bit_length + field_bit_offset, 8));
751fad8738SBob Moore 
7695b482a8SLen Brown 	field_byte_length = field_byte_end_offset - field_byte_offset;
7795b482a8SLen Brown 
7895b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
79b27d6597SBob Moore 			  "Bit length %u, Bit offset %u\n",
8095b482a8SLen Brown 			  field_bit_length, field_bit_offset));
8195b482a8SLen Brown 
8295b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
83b27d6597SBob Moore 			  "Byte Length %u, Byte Offset %u, End Offset %u\n",
8495b482a8SLen Brown 			  field_byte_length, field_byte_offset,
8595b482a8SLen Brown 			  field_byte_end_offset));
8695b482a8SLen Brown 
8795b482a8SLen Brown 	/*
8895b482a8SLen Brown 	 * Iterative search for the maximum access width that is both aligned
8995b482a8SLen Brown 	 * and does not go beyond the end of the region
9095b482a8SLen Brown 	 *
9195b482a8SLen Brown 	 * Start at byte_acc and work upwards to qword_acc max. (1,2,4,8 bytes)
9295b482a8SLen Brown 	 */
9395b482a8SLen Brown 	for (access_byte_width = 1; access_byte_width <= 8;
9495b482a8SLen Brown 	     access_byte_width <<= 1) {
9595b482a8SLen Brown 		/*
9695b482a8SLen Brown 		 * 1) Round end offset up to next access boundary and make sure that
9795b482a8SLen Brown 		 *    this does not go beyond the end of the parent region.
9895b482a8SLen Brown 		 * 2) When the Access width is greater than the field_byte_length, we
9995b482a8SLen Brown 		 *    are done. (This does not optimize for the perfectly aligned
10095b482a8SLen Brown 		 *    case yet).
10195b482a8SLen Brown 		 */
10295b482a8SLen Brown 		if (ACPI_ROUND_UP(field_byte_end_offset, access_byte_width) <=
10395b482a8SLen Brown 		    region_length) {
10495b482a8SLen Brown 			field_start_offset =
10595b482a8SLen Brown 			    ACPI_ROUND_DOWN(field_byte_offset,
10695b482a8SLen Brown 					    access_byte_width) /
10795b482a8SLen Brown 			    access_byte_width;
10895b482a8SLen Brown 
10995b482a8SLen Brown 			field_end_offset =
11095b482a8SLen Brown 			    ACPI_ROUND_UP((field_byte_length +
11195b482a8SLen Brown 					   field_byte_offset),
11295b482a8SLen Brown 					  access_byte_width) /
11395b482a8SLen Brown 			    access_byte_width;
11495b482a8SLen Brown 
11595b482a8SLen Brown 			accesses = field_end_offset - field_start_offset;
11695b482a8SLen Brown 
11795b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
118b27d6597SBob Moore 					  "AccessWidth %u end is within region\n",
11995b482a8SLen Brown 					  access_byte_width));
12095b482a8SLen Brown 
12195b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
122b27d6597SBob Moore 					  "Field Start %u, Field End %u -- requires %u accesses\n",
12395b482a8SLen Brown 					  field_start_offset, field_end_offset,
12495b482a8SLen Brown 					  accesses));
12595b482a8SLen Brown 
12695b482a8SLen Brown 			/* Single access is optimal */
12795b482a8SLen Brown 
12895b482a8SLen Brown 			if (accesses <= 1) {
12995b482a8SLen Brown 				ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
1301fad8738SBob Moore 						  "Entire field can be accessed "
1311fad8738SBob Moore 						  "with one operation of size %u\n",
13295b482a8SLen Brown 						  access_byte_width));
13395b482a8SLen Brown 				return_VALUE(access_byte_width);
13495b482a8SLen Brown 			}
13595b482a8SLen Brown 
13695b482a8SLen Brown 			/*
13795b482a8SLen Brown 			 * Fits in the region, but requires more than one read/write.
13895b482a8SLen Brown 			 * try the next wider access on next iteration
13995b482a8SLen Brown 			 */
14095b482a8SLen Brown 			if (accesses < minimum_accesses) {
14195b482a8SLen Brown 				minimum_accesses = accesses;
14295b482a8SLen Brown 				minimum_access_width = access_byte_width;
14395b482a8SLen Brown 			}
14495b482a8SLen Brown 		} else {
14595b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
146b27d6597SBob Moore 					  "AccessWidth %u end is NOT within region\n",
14795b482a8SLen Brown 					  access_byte_width));
14895b482a8SLen Brown 			if (access_byte_width == 1) {
14995b482a8SLen Brown 				ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
15095b482a8SLen Brown 						  "Field goes beyond end-of-region!\n"));
15195b482a8SLen Brown 
15295b482a8SLen Brown 				/* Field does not fit in the region at all */
15395b482a8SLen Brown 
15495b482a8SLen Brown 				return_VALUE(0);
15595b482a8SLen Brown 			}
15695b482a8SLen Brown 
15795b482a8SLen Brown 			/*
15895b482a8SLen Brown 			 * This width goes beyond the end-of-region, back off to
15995b482a8SLen Brown 			 * previous access
16095b482a8SLen Brown 			 */
16195b482a8SLen Brown 			ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
162b27d6597SBob Moore 					  "Backing off to previous optimal access width of %u\n",
16395b482a8SLen Brown 					  minimum_access_width));
16495b482a8SLen Brown 			return_VALUE(minimum_access_width);
16595b482a8SLen Brown 		}
16695b482a8SLen Brown 	}
16795b482a8SLen Brown 
16895b482a8SLen Brown 	/*
16995b482a8SLen Brown 	 * Could not read/write field with one operation,
17095b482a8SLen Brown 	 * just use max access width
17195b482a8SLen Brown 	 */
17295b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
17395b482a8SLen Brown 			  "Cannot access field in one operation, using width 8\n"));
1741fad8738SBob Moore 
17595b482a8SLen Brown 	return_VALUE(8);
17695b482a8SLen Brown }
17795b482a8SLen Brown #endif				/* ACPI_UNDER_DEVELOPMENT */
17895b482a8SLen Brown 
17995b482a8SLen Brown /*******************************************************************************
18095b482a8SLen Brown  *
18195b482a8SLen Brown  * FUNCTION:    acpi_ex_decode_field_access
18295b482a8SLen Brown  *
18395b482a8SLen Brown  * PARAMETERS:  obj_desc            - Field object
18495b482a8SLen Brown  *              field_flags         - Encoded fieldflags (contains access bits)
18595b482a8SLen Brown  *              return_byte_alignment - Where the byte alignment is returned
18695b482a8SLen Brown  *
18795b482a8SLen Brown  * RETURN:      Field granularity (8, 16, 32 or 64) and
18895b482a8SLen Brown  *              byte_alignment (1, 2, 3, or 4)
18995b482a8SLen Brown  *
19095b482a8SLen Brown  * DESCRIPTION: Decode the access_type bits of a field definition.
19195b482a8SLen Brown  *
19295b482a8SLen Brown  ******************************************************************************/
19395b482a8SLen Brown 
19495b482a8SLen Brown static u32
acpi_ex_decode_field_access(union acpi_operand_object * obj_desc,u8 field_flags,u32 * return_byte_alignment)19595b482a8SLen Brown acpi_ex_decode_field_access(union acpi_operand_object *obj_desc,
19695b482a8SLen Brown 			    u8 field_flags, u32 * return_byte_alignment)
19795b482a8SLen Brown {
19895b482a8SLen Brown 	u32 access;
19995b482a8SLen Brown 	u32 byte_alignment;
20095b482a8SLen Brown 	u32 bit_length;
20195b482a8SLen Brown 
20295b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ex_decode_field_access);
20395b482a8SLen Brown 
20495b482a8SLen Brown 	access = (field_flags & AML_FIELD_ACCESS_TYPE_MASK);
20595b482a8SLen Brown 
20695b482a8SLen Brown 	switch (access) {
20795b482a8SLen Brown 	case AML_FIELD_ACCESS_ANY:
20895b482a8SLen Brown 
20995b482a8SLen Brown #ifdef ACPI_UNDER_DEVELOPMENT
21095b482a8SLen Brown 		byte_alignment =
21195b482a8SLen Brown 		    acpi_ex_generate_access(obj_desc->common_field.
21295b482a8SLen Brown 					    start_field_bit_offset,
21395b482a8SLen Brown 					    obj_desc->common_field.bit_length,
21495b482a8SLen Brown 					    0xFFFFFFFF
21595b482a8SLen Brown 					    /* Temp until we pass region_length as parameter */
21695b482a8SLen Brown 		    );
21795b482a8SLen Brown 		bit_length = byte_alignment * 8;
21895b482a8SLen Brown #endif
21995b482a8SLen Brown 
22095b482a8SLen Brown 		byte_alignment = 1;
22195b482a8SLen Brown 		bit_length = 8;
22295b482a8SLen Brown 		break;
22395b482a8SLen Brown 
22495b482a8SLen Brown 	case AML_FIELD_ACCESS_BYTE:
22595b482a8SLen Brown 	case AML_FIELD_ACCESS_BUFFER:	/* ACPI 2.0 (SMBus Buffer) */
2261d1ea1b7SChao Guan 
22795b482a8SLen Brown 		byte_alignment = 1;
22895b482a8SLen Brown 		bit_length = 8;
22995b482a8SLen Brown 		break;
23095b482a8SLen Brown 
23195b482a8SLen Brown 	case AML_FIELD_ACCESS_WORD:
2321d1ea1b7SChao Guan 
23395b482a8SLen Brown 		byte_alignment = 2;
23495b482a8SLen Brown 		bit_length = 16;
23595b482a8SLen Brown 		break;
23695b482a8SLen Brown 
23795b482a8SLen Brown 	case AML_FIELD_ACCESS_DWORD:
2381d1ea1b7SChao Guan 
23995b482a8SLen Brown 		byte_alignment = 4;
24095b482a8SLen Brown 		bit_length = 32;
24195b482a8SLen Brown 		break;
24295b482a8SLen Brown 
24395b482a8SLen Brown 	case AML_FIELD_ACCESS_QWORD:	/* ACPI 2.0 */
2441d1ea1b7SChao Guan 
24595b482a8SLen Brown 		byte_alignment = 8;
24695b482a8SLen Brown 		bit_length = 64;
24795b482a8SLen Brown 		break;
24895b482a8SLen Brown 
24995b482a8SLen Brown 	default:
2501d1ea1b7SChao Guan 
25195b482a8SLen Brown 		/* Invalid field access type */
25295b482a8SLen Brown 
253f6a22b0bSBob Moore 		ACPI_ERROR((AE_INFO, "Unknown field access type 0x%X", access));
2541fad8738SBob Moore 
255fd1af712SBob Moore 		return_UINT32(0);
25695b482a8SLen Brown 	}
25795b482a8SLen Brown 
2583371c19cSBob Moore 	if (obj_desc->common.type == ACPI_TYPE_BUFFER_FIELD) {
25995b482a8SLen Brown 		/*
26095b482a8SLen Brown 		 * buffer_field access can be on any byte boundary, so the
26195b482a8SLen Brown 		 * byte_alignment is always 1 byte -- regardless of any byte_alignment
26295b482a8SLen Brown 		 * implied by the field access type.
26395b482a8SLen Brown 		 */
26495b482a8SLen Brown 		byte_alignment = 1;
26595b482a8SLen Brown 	}
26695b482a8SLen Brown 
26795b482a8SLen Brown 	*return_byte_alignment = byte_alignment;
268fd1af712SBob Moore 	return_UINT32(bit_length);
26995b482a8SLen Brown }
27095b482a8SLen Brown 
27195b482a8SLen Brown /*******************************************************************************
27295b482a8SLen Brown  *
27395b482a8SLen Brown  * FUNCTION:    acpi_ex_prep_common_field_object
27495b482a8SLen Brown  *
27595b482a8SLen Brown  * PARAMETERS:  obj_desc            - The field object
27695b482a8SLen Brown  *              field_flags         - Access, lock_rule, and update_rule.
27795b482a8SLen Brown  *                                    The format of a field_flag is described
27895b482a8SLen Brown  *                                    in the ACPI specification
27995b482a8SLen Brown  *              field_attribute     - Special attributes (not used)
28095b482a8SLen Brown  *              field_bit_position  - Field start position
28195b482a8SLen Brown  *              field_bit_length    - Field length in number of bits
28295b482a8SLen Brown  *
28395b482a8SLen Brown  * RETURN:      Status
28495b482a8SLen Brown  *
28595b482a8SLen Brown  * DESCRIPTION: Initialize the areas of the field object that are common
28695b482a8SLen Brown  *              to the various types of fields. Note: This is very "sensitive"
28795b482a8SLen Brown  *              code because we are solving the general case for field
28895b482a8SLen Brown  *              alignment.
28995b482a8SLen Brown  *
29095b482a8SLen Brown  ******************************************************************************/
29195b482a8SLen Brown 
29295b482a8SLen Brown acpi_status
acpi_ex_prep_common_field_object(union acpi_operand_object * obj_desc,u8 field_flags,u8 field_attribute,u32 field_bit_position,u32 field_bit_length)29395b482a8SLen Brown acpi_ex_prep_common_field_object(union acpi_operand_object *obj_desc,
29495b482a8SLen Brown 				 u8 field_flags,
29595b482a8SLen Brown 				 u8 field_attribute,
29695b482a8SLen Brown 				 u32 field_bit_position, u32 field_bit_length)
29795b482a8SLen Brown {
29895b482a8SLen Brown 	u32 access_bit_width;
29995b482a8SLen Brown 	u32 byte_alignment;
30095b482a8SLen Brown 	u32 nearest_byte_address;
30195b482a8SLen Brown 
30295b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ex_prep_common_field_object);
30395b482a8SLen Brown 
30495b482a8SLen Brown 	/*
30595b482a8SLen Brown 	 * Note: the structure being initialized is the
30695b482a8SLen Brown 	 * ACPI_COMMON_FIELD_INFO;  No structure fields outside of the common
30795b482a8SLen Brown 	 * area are initialized by this procedure.
30895b482a8SLen Brown 	 */
30995b482a8SLen Brown 	obj_desc->common_field.field_flags = field_flags;
31095b482a8SLen Brown 	obj_desc->common_field.attribute = field_attribute;
31195b482a8SLen Brown 	obj_desc->common_field.bit_length = field_bit_length;
31295b482a8SLen Brown 
31395b482a8SLen Brown 	/*
31495b482a8SLen Brown 	 * Decode the access type so we can compute offsets. The access type gives
31595b482a8SLen Brown 	 * two pieces of information - the width of each field access and the
31695b482a8SLen Brown 	 * necessary byte_alignment (address granularity) of the access.
31795b482a8SLen Brown 	 *
31895b482a8SLen Brown 	 * For any_acc, the access_bit_width is the largest width that is both
31995b482a8SLen Brown 	 * necessary and possible in an attempt to access the whole field in one
32095b482a8SLen Brown 	 * I/O operation. However, for any_acc, the byte_alignment is always one
32195b482a8SLen Brown 	 * byte.
32295b482a8SLen Brown 	 *
32395b482a8SLen Brown 	 * For all Buffer Fields, the byte_alignment is always one byte.
32495b482a8SLen Brown 	 *
32595b482a8SLen Brown 	 * For all other access types (Byte, Word, Dword, Qword), the Bitwidth is
32695b482a8SLen Brown 	 * the same (equivalent) as the byte_alignment.
32795b482a8SLen Brown 	 */
3281fad8738SBob Moore 	access_bit_width =
3291fad8738SBob Moore 	    acpi_ex_decode_field_access(obj_desc, field_flags, &byte_alignment);
33095b482a8SLen Brown 	if (!access_bit_width) {
33195b482a8SLen Brown 		return_ACPI_STATUS(AE_AML_OPERAND_VALUE);
33295b482a8SLen Brown 	}
33395b482a8SLen Brown 
33409387b43SBob Moore 	/* Setup width (access granularity) fields (values are: 1, 2, 4, 8) */
33595b482a8SLen Brown 
33695b482a8SLen Brown 	obj_desc->common_field.access_byte_width = (u8)
33709387b43SBob Moore 	    ACPI_DIV_8(access_bit_width);
33895b482a8SLen Brown 
33995b482a8SLen Brown 	/*
34095b482a8SLen Brown 	 * base_byte_offset is the address of the start of the field within the
34195b482a8SLen Brown 	 * region. It is the byte address of the first *datum* (field-width data
34295b482a8SLen Brown 	 * unit) of the field. (i.e., the first datum that contains at least the
34395b482a8SLen Brown 	 * first *bit* of the field.)
34495b482a8SLen Brown 	 *
34595b482a8SLen Brown 	 * Note: byte_alignment is always either equal to the access_bit_width or 8
34695b482a8SLen Brown 	 * (Byte access), and it defines the addressing granularity of the parent
34795b482a8SLen Brown 	 * region or buffer.
34895b482a8SLen Brown 	 */
34995b482a8SLen Brown 	nearest_byte_address =
35095b482a8SLen Brown 	    ACPI_ROUND_BITS_DOWN_TO_BYTES(field_bit_position);
35195b482a8SLen Brown 	obj_desc->common_field.base_byte_offset = (u32)
35295b482a8SLen Brown 	    ACPI_ROUND_DOWN(nearest_byte_address, byte_alignment);
35395b482a8SLen Brown 
35495b482a8SLen Brown 	/*
35595b482a8SLen Brown 	 * start_field_bit_offset is the offset of the first bit of the field within
35695b482a8SLen Brown 	 * a field datum.
35795b482a8SLen Brown 	 */
35895b482a8SLen Brown 	obj_desc->common_field.start_field_bit_offset = (u8)
35995b482a8SLen Brown 	    (field_bit_position -
36095b482a8SLen Brown 	     ACPI_MUL_8(obj_desc->common_field.base_byte_offset));
36195b482a8SLen Brown 
36295b482a8SLen Brown 	return_ACPI_STATUS(AE_OK);
36395b482a8SLen Brown }
36495b482a8SLen Brown 
36595b482a8SLen Brown /*******************************************************************************
36695b482a8SLen Brown  *
36795b482a8SLen Brown  * FUNCTION:    acpi_ex_prep_field_value
36895b482a8SLen Brown  *
369ba494beeSBob Moore  * PARAMETERS:  info    - Contains all field creation info
37095b482a8SLen Brown  *
37195b482a8SLen Brown  * RETURN:      Status
37295b482a8SLen Brown  *
373cf48958eSBob Moore  * DESCRIPTION: Construct an object of type union acpi_operand_object with a
374cf48958eSBob Moore  *              subtype of def_field and connect it to the parent Node.
37595b482a8SLen Brown  *
37695b482a8SLen Brown  ******************************************************************************/
37795b482a8SLen Brown 
acpi_ex_prep_field_value(struct acpi_create_field_info * info)37895b482a8SLen Brown acpi_status acpi_ex_prep_field_value(struct acpi_create_field_info *info)
37995b482a8SLen Brown {
38095b482a8SLen Brown 	union acpi_operand_object *obj_desc;
38195b482a8SLen Brown 	union acpi_operand_object *second_desc = NULL;
38295b482a8SLen Brown 	acpi_status status;
38309387b43SBob Moore 	u32 access_byte_width;
38409387b43SBob Moore 	u32 type;
38595b482a8SLen Brown 
38695b482a8SLen Brown 	ACPI_FUNCTION_TRACE(ex_prep_field_value);
38795b482a8SLen Brown 
38895b482a8SLen Brown 	/* Parameter validation */
38995b482a8SLen Brown 
39095b482a8SLen Brown 	if (info->field_type != ACPI_TYPE_LOCAL_INDEX_FIELD) {
39195b482a8SLen Brown 		if (!info->region_node) {
39295b482a8SLen Brown 			ACPI_ERROR((AE_INFO, "Null RegionNode"));
39395b482a8SLen Brown 			return_ACPI_STATUS(AE_AML_NO_OPERAND);
39495b482a8SLen Brown 		}
39595b482a8SLen Brown 
39695b482a8SLen Brown 		type = acpi_ns_get_type(info->region_node);
39795b482a8SLen Brown 		if (type != ACPI_TYPE_REGION) {
39895b482a8SLen Brown 			ACPI_ERROR((AE_INFO,
39909387b43SBob Moore 				    "Needed Region, found type 0x%X (%s)", type,
40009387b43SBob Moore 				    acpi_ut_get_type_name(type)));
40195b482a8SLen Brown 
40295b482a8SLen Brown 			return_ACPI_STATUS(AE_AML_OPERAND_TYPE);
40395b482a8SLen Brown 		}
40495b482a8SLen Brown 	}
40595b482a8SLen Brown 
40695b482a8SLen Brown 	/* Allocate a new field object */
40795b482a8SLen Brown 
40895b482a8SLen Brown 	obj_desc = acpi_ut_create_internal_object(info->field_type);
40995b482a8SLen Brown 	if (!obj_desc) {
41095b482a8SLen Brown 		return_ACPI_STATUS(AE_NO_MEMORY);
41195b482a8SLen Brown 	}
41295b482a8SLen Brown 
41395b482a8SLen Brown 	/* Initialize areas of the object that are common to all fields */
41495b482a8SLen Brown 
41595b482a8SLen Brown 	obj_desc->common_field.node = info->field_node;
41609387b43SBob Moore 	status = acpi_ex_prep_common_field_object(obj_desc,
41709387b43SBob Moore 						  info->field_flags,
41895b482a8SLen Brown 						  info->attribute,
41995b482a8SLen Brown 						  info->field_bit_position,
42095b482a8SLen Brown 						  info->field_bit_length);
42195b482a8SLen Brown 	if (ACPI_FAILURE(status)) {
42295b482a8SLen Brown 		acpi_ut_delete_object_desc(obj_desc);
42395b482a8SLen Brown 		return_ACPI_STATUS(status);
42495b482a8SLen Brown 	}
42595b482a8SLen Brown 
42695b482a8SLen Brown 	/* Initialize areas of the object that are specific to the field type */
42795b482a8SLen Brown 
42895b482a8SLen Brown 	switch (info->field_type) {
42995b482a8SLen Brown 	case ACPI_TYPE_LOCAL_REGION_FIELD:
43095b482a8SLen Brown 
43195b482a8SLen Brown 		obj_desc->field.region_obj =
43295b482a8SLen Brown 		    acpi_ns_get_attached_object(info->region_node);
43395b482a8SLen Brown 
4349ce81784SBob Moore 		/* Fields specific to generic_serial_bus fields */
4359ce81784SBob Moore 
4369ce81784SBob Moore 		obj_desc->field.access_length = info->access_length;
4379ce81784SBob Moore 
4389ce81784SBob Moore 		if (info->connection_node) {
4399ce81784SBob Moore 			second_desc = info->connection_node->object;
440*a0a2459bSAleksandrs Vinarskis 			if (second_desc == NULL) {
441*a0a2459bSAleksandrs Vinarskis 				break;
442*a0a2459bSAleksandrs Vinarskis 			}
4439ce81784SBob Moore 			if (!(second_desc->common.flags & AOPOBJ_DATA_VALID)) {
4449ce81784SBob Moore 				status =
4459ce81784SBob Moore 				    acpi_ds_get_buffer_arguments(second_desc);
4469ce81784SBob Moore 				if (ACPI_FAILURE(status)) {
4479ce81784SBob Moore 					acpi_ut_delete_object_desc(obj_desc);
4489ce81784SBob Moore 					return_ACPI_STATUS(status);
4499ce81784SBob Moore 				}
4509ce81784SBob Moore 			}
4519ce81784SBob Moore 
4529ce81784SBob Moore 			obj_desc->field.resource_buffer =
4539ce81784SBob Moore 			    second_desc->buffer.pointer;
4549ce81784SBob Moore 			obj_desc->field.resource_length =
4559ce81784SBob Moore 			    (u16)second_desc->buffer.length;
4569ce81784SBob Moore 		} else if (info->resource_buffer) {
4579ce81784SBob Moore 			obj_desc->field.resource_buffer = info->resource_buffer;
4589ce81784SBob Moore 			obj_desc->field.resource_length = info->resource_length;
4599ce81784SBob Moore 		}
4609ce81784SBob Moore 
46175ec6e55SBob Moore 		obj_desc->field.pin_number_index = info->pin_number_index;
46275ec6e55SBob Moore 
46309387b43SBob Moore 		/* Allow full data read from EC address space */
46409387b43SBob Moore 
46509387b43SBob Moore 		if ((obj_desc->field.region_obj->region.space_id ==
46609387b43SBob Moore 		     ACPI_ADR_SPACE_EC)
46709387b43SBob Moore 		    && (obj_desc->common_field.bit_length > 8)) {
46809387b43SBob Moore 			access_byte_width =
46909387b43SBob Moore 			    ACPI_ROUND_BITS_UP_TO_BYTES(obj_desc->common_field.
47009387b43SBob Moore 							bit_length);
47109387b43SBob Moore 
47209387b43SBob Moore 			/* Maximum byte width supported is 255 */
47309387b43SBob Moore 
47409387b43SBob Moore 			if (access_byte_width < 256) {
47509387b43SBob Moore 				obj_desc->common_field.access_byte_width =
47609387b43SBob Moore 				    (u8)access_byte_width;
47709387b43SBob Moore 			}
47809387b43SBob Moore 		}
47995b482a8SLen Brown 		ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
48095b482a8SLen Brown 				  "RegionField: BitOff %X, Off %X, Gran %X, Region %p\n",
48195b482a8SLen Brown 				  obj_desc->field.start_field_bit_offset,
48295b482a8SLen Brown 				  obj_desc->field.base_byte_offset,
48395b482a8SLen Brown 				  obj_desc->field.access_byte_width,
48495b482a8SLen Brown 				  obj_desc->field.region_obj));
48595b482a8SLen Brown 		break;
48695b482a8SLen Brown 
48795b482a8SLen Brown 	case ACPI_TYPE_LOCAL_BANK_FIELD:
48895b482a8SLen Brown 
48995b482a8SLen Brown 		obj_desc->bank_field.value = info->bank_value;
49095b482a8SLen Brown 		obj_desc->bank_field.region_obj =
49195b482a8SLen Brown 		    acpi_ns_get_attached_object(info->region_node);
49295b482a8SLen Brown 		obj_desc->bank_field.bank_obj =
49395b482a8SLen Brown 		    acpi_ns_get_attached_object(info->register_node);
49495b482a8SLen Brown 
49595b482a8SLen Brown 		/* An additional reference for the attached objects */
49695b482a8SLen Brown 
49795b482a8SLen Brown 		acpi_ut_add_reference(obj_desc->bank_field.region_obj);
49895b482a8SLen Brown 		acpi_ut_add_reference(obj_desc->bank_field.bank_obj);
49995b482a8SLen Brown 
50095b482a8SLen Brown 		ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
50195b482a8SLen Brown 				  "Bank Field: BitOff %X, Off %X, Gran %X, Region %p, BankReg %p\n",
50295b482a8SLen Brown 				  obj_desc->bank_field.start_field_bit_offset,
50395b482a8SLen Brown 				  obj_desc->bank_field.base_byte_offset,
50495b482a8SLen Brown 				  obj_desc->field.access_byte_width,
50595b482a8SLen Brown 				  obj_desc->bank_field.region_obj,
50695b482a8SLen Brown 				  obj_desc->bank_field.bank_obj));
50795b482a8SLen Brown 
50895b482a8SLen Brown 		/*
50995b482a8SLen Brown 		 * Remember location in AML stream of the field unit
51095b482a8SLen Brown 		 * opcode and operands -- since the bank_value
51195b482a8SLen Brown 		 * operands must be evaluated.
51295b482a8SLen Brown 		 */
51395b482a8SLen Brown 		second_desc = obj_desc->common.next_object;
51495b482a8SLen Brown 		second_desc->extra.aml_start =
51595b482a8SLen Brown 		    ACPI_CAST_PTR(union acpi_parse_object,
51695b482a8SLen Brown 				  info->data_register_node)->named.data;
51795b482a8SLen Brown 		second_desc->extra.aml_length =
51895b482a8SLen Brown 		    ACPI_CAST_PTR(union acpi_parse_object,
51995b482a8SLen Brown 				  info->data_register_node)->named.length;
52095b482a8SLen Brown 
52195b482a8SLen Brown 		break;
52295b482a8SLen Brown 
52395b482a8SLen Brown 	case ACPI_TYPE_LOCAL_INDEX_FIELD:
52495b482a8SLen Brown 
52595b482a8SLen Brown 		/* Get the Index and Data registers */
52695b482a8SLen Brown 
52795b482a8SLen Brown 		obj_desc->index_field.index_obj =
52895b482a8SLen Brown 		    acpi_ns_get_attached_object(info->register_node);
52995b482a8SLen Brown 		obj_desc->index_field.data_obj =
53095b482a8SLen Brown 		    acpi_ns_get_attached_object(info->data_register_node);
53195b482a8SLen Brown 
53295b482a8SLen Brown 		if (!obj_desc->index_field.data_obj
53395b482a8SLen Brown 		    || !obj_desc->index_field.index_obj) {
53495b482a8SLen Brown 			ACPI_ERROR((AE_INFO,
53595b482a8SLen Brown 				    "Null Index Object during field prep"));
53695b482a8SLen Brown 			acpi_ut_delete_object_desc(obj_desc);
53795b482a8SLen Brown 			return_ACPI_STATUS(AE_AML_INTERNAL);
53895b482a8SLen Brown 		}
53995b482a8SLen Brown 
54095b482a8SLen Brown 		/* An additional reference for the attached objects */
54195b482a8SLen Brown 
54295b482a8SLen Brown 		acpi_ut_add_reference(obj_desc->index_field.data_obj);
54395b482a8SLen Brown 		acpi_ut_add_reference(obj_desc->index_field.index_obj);
54495b482a8SLen Brown 
54595b482a8SLen Brown 		/*
54695b482a8SLen Brown 		 * April 2006: Changed to match MS behavior
54795b482a8SLen Brown 		 *
54895b482a8SLen Brown 		 * The value written to the Index register is the byte offset of the
54995b482a8SLen Brown 		 * target field in units of the granularity of the index_field
55095b482a8SLen Brown 		 *
55195b482a8SLen Brown 		 * Previously, the value was calculated as an index in terms of the
55295b482a8SLen Brown 		 * width of the Data register, as below:
55395b482a8SLen Brown 		 *
55495b482a8SLen Brown 		 *      obj_desc->index_field.Value = (u32)
55595b482a8SLen Brown 		 *          (Info->field_bit_position / ACPI_MUL_8 (
55695b482a8SLen Brown 		 *              obj_desc->Field.access_byte_width));
55795b482a8SLen Brown 		 *
55895b482a8SLen Brown 		 * February 2006: Tried value as a byte offset:
55995b482a8SLen Brown 		 *      obj_desc->index_field.Value = (u32)
56095b482a8SLen Brown 		 *          ACPI_DIV_8 (Info->field_bit_position);
56195b482a8SLen Brown 		 */
56295b482a8SLen Brown 		obj_desc->index_field.value =
56395b482a8SLen Brown 		    (u32) ACPI_ROUND_DOWN(ACPI_DIV_8(info->field_bit_position),
56495b482a8SLen Brown 					  obj_desc->index_field.
56595b482a8SLen Brown 					  access_byte_width);
56695b482a8SLen Brown 
56795b482a8SLen Brown 		ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
5681fad8738SBob Moore 				  "IndexField: BitOff %X, Off %X, Value %X, "
5691fad8738SBob Moore 				  "Gran %X, Index %p, Data %p\n",
57095b482a8SLen Brown 				  obj_desc->index_field.start_field_bit_offset,
57195b482a8SLen Brown 				  obj_desc->index_field.base_byte_offset,
57295b482a8SLen Brown 				  obj_desc->index_field.value,
57395b482a8SLen Brown 				  obj_desc->field.access_byte_width,
57495b482a8SLen Brown 				  obj_desc->index_field.index_obj,
57595b482a8SLen Brown 				  obj_desc->index_field.data_obj));
57695b482a8SLen Brown 		break;
57795b482a8SLen Brown 
57895b482a8SLen Brown 	default:
5791d1ea1b7SChao Guan 
58095b482a8SLen Brown 		/* No other types should get here */
5811d1ea1b7SChao Guan 
58295b482a8SLen Brown 		break;
58395b482a8SLen Brown 	}
58495b482a8SLen Brown 
58595b482a8SLen Brown 	/*
58695b482a8SLen Brown 	 * Store the constructed descriptor (obj_desc) into the parent Node,
58795b482a8SLen Brown 	 * preserving the current type of that named_obj.
58895b482a8SLen Brown 	 */
5891fad8738SBob Moore 	status =
5901fad8738SBob Moore 	    acpi_ns_attach_object(info->field_node, obj_desc,
59195b482a8SLen Brown 				  acpi_ns_get_type(info->field_node));
59295b482a8SLen Brown 
59395b482a8SLen Brown 	ACPI_DEBUG_PRINT((ACPI_DB_BFIELD,
59495b482a8SLen Brown 			  "Set NamedObj %p [%4.4s], ObjDesc %p\n",
59595b482a8SLen Brown 			  info->field_node,
59695b482a8SLen Brown 			  acpi_ut_get_node_name(info->field_node), obj_desc));
59795b482a8SLen Brown 
59895b482a8SLen Brown 	/* Remove local reference to the object */
59995b482a8SLen Brown 
60095b482a8SLen Brown 	acpi_ut_remove_reference(obj_desc);
60195b482a8SLen Brown 	return_ACPI_STATUS(status);
60295b482a8SLen Brown }
603