1 /******************************************************************************
3 * Module Name: amfield - ACPI AML (p-code) execution - field manipulation
6 *****************************************************************************/
9 * Copyright (C) 2000 R. Byron Moore
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 #define _COMPONENT INTERPRETER
37 MODULE_NAME ("amfield")
40 /*******************************************************************************
42 * FUNCTION: Acpi_aml_setup_field
44 * PARAMETERS: *Obj_desc - Field to be read or written
45 * *Rgn_desc - Region containing field
46 * Field_bit_width - Field Width in bits (8, 16, or 32)
50 * DESCRIPTION: Common processing for Acpi_aml_read_field and Acpi_aml_write_field
52 * ACPI SPECIFICATION REFERENCES:
53 * Each of the Type1_opcodes is defined as specified in in-line
54 * comments below. For each one, use the following definitions.
56 * Def_bit_field := Bit_field_op Src_buf Bit_idx Destination
57 * Def_byte_field := Byte_field_op Src_buf Byte_idx Destination
58 * Def_create_field := Create_field_op Src_buf Bit_idx Num_bits Name_string
59 * Def_dWord_field := DWord_field_op Src_buf Byte_idx Destination
60 * Def_word_field := Word_field_op Src_buf Byte_idx Destination
61 * Bit_index := Term_arg=>Integer
62 * Byte_index := Term_arg=>Integer
63 * Destination := Name_string
64 * Num_bits := Term_arg=>Integer
65 * Source_buf := Term_arg=>Buffer
67 ******************************************************************************/
70 acpi_aml_setup_field (
71 ACPI_OPERAND_OBJECT
*obj_desc
,
72 ACPI_OPERAND_OBJECT
*rgn_desc
,
75 ACPI_STATUS status
= AE_OK
;
79 /* Parameter validation */
81 if (!obj_desc
|| !rgn_desc
) {
82 return (AE_AML_NO_OPERAND
);
85 if (ACPI_TYPE_REGION
!= rgn_desc
->common
.type
) {
86 return (AE_AML_OPERAND_TYPE
);
91 * TBD: [Future] Acpi 2.0 supports Qword fields
93 * Init and validate Field width
94 * Possible values are 1, 2, 4
97 field_byte_width
= DIV_8 (field_bit_width
);
99 if ((field_bit_width
!= 8) &&
100 (field_bit_width
!= 16) &&
101 (field_bit_width
!= 32))
103 return (AE_AML_OPERAND_VALUE
);
108 * If the Region Address and Length have not been previously evaluated,
109 * evaluate them and save the results.
111 if (!(rgn_desc
->region
.flags
& AOPOBJ_DATA_VALID
)) {
113 status
= acpi_ds_get_region_arguments (rgn_desc
);
114 if (ACPI_FAILURE (status
)) {
120 if ((obj_desc
->common
.type
== ACPI_TYPE_FIELD_UNIT
) &&
121 (!(obj_desc
->common
.flags
& AOPOBJ_DATA_VALID
)))
124 * Field Buffer and Index have not been previously evaluated,
126 return (AE_AML_INTERNAL
);
129 if (rgn_desc
->region
.length
<
130 (obj_desc
->field
.offset
& ~((u32
) field_byte_width
- 1)) +
134 * Offset rounded up to next multiple of field width
135 * exceeds region length, indicate an error
138 return (AE_AML_REGION_LIMIT
);
145 /*******************************************************************************
147 * FUNCTION: Acpi_aml_access_named_field
149 * PARAMETERS: Mode - ACPI_READ or ACPI_WRITE
150 * Named_field - Handle for field to be accessed
151 * *Buffer - Value(s) to be read or written
152 * Buffer_length - Number of bytes to transfer
156 * DESCRIPTION: Read or write a named field
158 ******************************************************************************/
161 acpi_aml_access_named_field (
163 ACPI_HANDLE named_field
,
167 ACPI_OPERAND_OBJECT
*obj_desc
= NULL
;
168 ACPI_STATUS status
= AE_OK
;
170 u32 bit_granularity
= 0;
171 u32 byte_granularity
;
173 u32 actual_byte_length
;
174 u32 byte_field_length
;
177 /* Basic data checking */
178 if ((!named_field
) || (ACPI_READ
== mode
&& !buffer
)) {
179 return (AE_AML_INTERNAL
);
182 /* Get the attached field object */
184 obj_desc
= acpi_ns_get_attached_object (named_field
);
186 return (AE_AML_INTERNAL
);
191 if (INTERNAL_TYPE_DEF_FIELD
!= acpi_ns_get_type (named_field
)) {
192 return (AE_AML_OPERAND_TYPE
);
195 /* Obj_desc valid and Named_field is a defined field */
198 /* Double-check that the attached object is also a field */
200 if (INTERNAL_TYPE_DEF_FIELD
!= obj_desc
->common
.type
) {
201 return (AE_AML_OPERAND_TYPE
);
206 * Granularity was decoded from the field access type
207 * (Any_acc will be the same as Byte_acc)
210 bit_granularity
= obj_desc
->field_unit
.granularity
;
211 byte_granularity
= DIV_8 (bit_granularity
);
214 * Check if request is too large for the field, and silently truncate
218 /* TBD: [Errors] should an error be returned in this case? */
220 byte_field_length
= (u32
) DIV_8 (obj_desc
->field_unit
.length
+ 7);
223 actual_byte_length
= buffer_length
;
224 if (buffer_length
> byte_field_length
) {
225 actual_byte_length
= byte_field_length
;
228 /* TBD: should these round down to a power of 2? */
230 if (DIV_8(bit_granularity
) > byte_field_length
) {
231 bit_granularity
= MUL_8(byte_field_length
);
234 if (byte_granularity
> byte_field_length
) {
235 byte_granularity
= byte_field_length
;
239 /* Convert byte count to datum count, round up if necessary */
241 datum_length
= (actual_byte_length
+ (byte_granularity
-1)) / byte_granularity
;
244 /* Get the global lock if needed */
246 locked
= acpi_aml_acquire_global_lock (obj_desc
->field_unit
.lock_rule
);
249 /* Perform the actual read or write of the buffer */
255 status
= acpi_aml_read_field (obj_desc
, buffer
, buffer_length
,
256 actual_byte_length
, datum_length
,
257 bit_granularity
, byte_granularity
);
263 status
= acpi_aml_write_field (obj_desc
, buffer
, buffer_length
,
264 actual_byte_length
, datum_length
,
265 bit_granularity
, byte_granularity
);
271 status
= AE_BAD_PARAMETER
;
276 /* Release global lock if we acquired it earlier */
278 acpi_aml_release_global_lock (locked
);