summaryrefslogtreecommitdiffstats
path: root/drivers/acpi/interpreter/amfield.c
blob: 356be14c230be5317f64a6453ca8080dfb2aa308 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/******************************************************************************
 *
 * Module Name: amfield - ACPI AML (p-code) execution - field manipulation
 *              $Revision: 74 $
 *
 *****************************************************************************/

/*
 *  Copyright (C) 2000 R. Byron Moore
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */


#include "acpi.h"
#include "acdispat.h"
#include "acinterp.h"
#include "amlcode.h"
#include "acnamesp.h"
#include "achware.h"
#include "acevents.h"


#define _COMPONENT          INTERPRETER
	 MODULE_NAME         ("amfield")


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_setup_field
 *
 * PARAMETERS:  *Obj_desc           - Field to be read or written
 *              *Rgn_desc           - Region containing field
 *              Field_bit_width     - Field Width in bits (8, 16, or 32)
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Common processing for Acpi_aml_read_field and Acpi_aml_write_field
 *
 *  ACPI SPECIFICATION REFERENCES:
 *  Each of the Type1_opcodes is defined as specified in in-line
 *  comments below. For each one, use the following definitions.
 *
 *  Def_bit_field   :=  Bit_field_op    Src_buf Bit_idx Destination
 *  Def_byte_field  :=  Byte_field_op   Src_buf Byte_idx Destination
 *  Def_create_field := Create_field_op Src_buf Bit_idx Num_bits Name_string
 *  Def_dWord_field :=  DWord_field_op  Src_buf Byte_idx Destination
 *  Def_word_field  :=  Word_field_op   Src_buf Byte_idx Destination
 *  Bit_index       :=  Term_arg=>Integer
 *  Byte_index      :=  Term_arg=>Integer
 *  Destination     :=  Name_string
 *  Num_bits        :=  Term_arg=>Integer
 *  Source_buf      :=  Term_arg=>Buffer
 *
 ******************************************************************************/

ACPI_STATUS
acpi_aml_setup_field (
	ACPI_OPERAND_OBJECT     *obj_desc,
	ACPI_OPERAND_OBJECT     *rgn_desc,
	u32                     field_bit_width)
{
	ACPI_STATUS             status = AE_OK;
	u32                     field_byte_width;


	/* Parameter validation */

	if (!obj_desc || !rgn_desc) {
		return (AE_AML_NO_OPERAND);
	}

	if (ACPI_TYPE_REGION != rgn_desc->common.type) {
		return (AE_AML_OPERAND_TYPE);
	}


	/*
	 * TBD: [Future] Acpi 2.0 supports Qword fields
	 *
	 * Init and validate Field width
	 * Possible values are 1, 2, 4
	 */

	field_byte_width = DIV_8 (field_bit_width);

	if ((field_bit_width != 8) &&
		(field_bit_width != 16) &&
		(field_bit_width != 32))
	{
		return (AE_AML_OPERAND_VALUE);
	}


	/*
	 * If the Region Address and Length have not been previously evaluated,
	 * evaluate them and save the results.
	 */
	if (!(rgn_desc->region.flags & AOPOBJ_DATA_VALID)) {

		status = acpi_ds_get_region_arguments (rgn_desc);
		if (ACPI_FAILURE (status)) {
			return (status);
		}
	}


	if ((obj_desc->common.type == ACPI_TYPE_FIELD_UNIT) &&
		(!(obj_desc->common.flags & AOPOBJ_DATA_VALID)))
	{
		/*
		 * Field Buffer and Index have not been previously evaluated,
		 */
		return (AE_AML_INTERNAL);
	}

	if (rgn_desc->region.length <
	   (obj_desc->field.offset & ~((u32) field_byte_width - 1)) +
			field_byte_width)
	{
		/*
		 * Offset rounded up to next multiple of field width
		 * exceeds region length, indicate an error
		 */

		return (AE_AML_REGION_LIMIT);
	}

	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_access_named_field
 *
 * PARAMETERS:  Mode                - ACPI_READ or ACPI_WRITE
 *              Named_field         - Handle for field to be accessed
 *              *Buffer             - Value(s) to be read or written
 *              Buffer_length         - Number of bytes to transfer
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Read or write a named field
 *
 ******************************************************************************/

ACPI_STATUS
acpi_aml_access_named_field (
	u32                     mode,
	ACPI_HANDLE             named_field,
	void                    *buffer,
	u32                     buffer_length)
{
	ACPI_OPERAND_OBJECT     *obj_desc = NULL;
	ACPI_STATUS             status = AE_OK;
	u8                      locked = FALSE;
	u32                     bit_granularity = 0;
	u32                     byte_granularity;
	u32                     datum_length;
	u32                     actual_byte_length;
	u32                     byte_field_length;


	/* Basic data checking */
	if ((!named_field) || (ACPI_READ == mode && !buffer)) {
		return (AE_AML_INTERNAL);
	}

	/* Get the attached field object */

	obj_desc = acpi_ns_get_attached_object (named_field);
	if (!obj_desc) {
		return (AE_AML_INTERNAL);
	}

	/* Check the type */

	if (INTERNAL_TYPE_DEF_FIELD != acpi_ns_get_type (named_field)) {
		return (AE_AML_OPERAND_TYPE);
	}

	/* Obj_desc valid and Named_field is a defined field */


	/* Double-check that the attached object is also a field */

	if (INTERNAL_TYPE_DEF_FIELD != obj_desc->common.type) {
		return (AE_AML_OPERAND_TYPE);
	}


	/*
	 * Granularity was decoded from the field access type
	 * (Any_acc will be the same as Byte_acc)
	 */

	bit_granularity = obj_desc->field_unit.granularity;
	byte_granularity = DIV_8 (bit_granularity);

	/*
	 * Check if request is too large for the field, and silently truncate
	 * if necessary
	 */

	/* TBD: [Errors] should an error be returned in this case? */

	byte_field_length = (u32) DIV_8 (obj_desc->field_unit.length + 7);


	actual_byte_length = buffer_length;
	if (buffer_length > byte_field_length) {
		actual_byte_length = byte_field_length;
	}

	/* TBD: should these round down to a power of 2? */

	if (DIV_8(bit_granularity) > byte_field_length) {
		bit_granularity = MUL_8(byte_field_length);
	}

	if (byte_granularity > byte_field_length) {
		byte_granularity = byte_field_length;
	}


	/* Convert byte count to datum count, round up if necessary */

	datum_length = (actual_byte_length + (byte_granularity-1)) / byte_granularity;


	/* Get the global lock if needed */

	locked = acpi_aml_acquire_global_lock (obj_desc->field_unit.lock_rule);


	/* Perform the actual read or write of the buffer */

	switch (mode)
	{
	case ACPI_READ:

		status = acpi_aml_read_field (obj_desc, buffer, buffer_length,
				  actual_byte_length, datum_length,
				  bit_granularity, byte_granularity);
		break;


	case ACPI_WRITE:

		status = acpi_aml_write_field (obj_desc, buffer, buffer_length,
				  actual_byte_length, datum_length,
				  bit_granularity, byte_granularity);
		break;


	default:

		status = AE_BAD_PARAMETER;
		break;
	}


	/* Release global lock if we acquired it earlier */

	acpi_aml_release_global_lock (locked);

	return (status);
}