summaryrefslogtreecommitdiffstats
path: root/drivers/acpi/interpreter/amresolv.c
blob: c08a4731cb014e784951948a54378ab332b79283 (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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465

/******************************************************************************
 *
 * Module Name: amresolv - AML Interpreter object resolution
 *              $Revision: 81 $
 *
 *****************************************************************************/

/*
 *  Copyright (C) 2000, 2001 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 "amlcode.h"
#include "acparser.h"
#include "acdispat.h"
#include "acinterp.h"
#include "acnamesp.h"
#include "actables.h"
#include "acevents.h"


#define _COMPONENT          INTERPRETER
	 MODULE_NAME         ("amresolv")


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_get_field_unit_value
 *
 * PARAMETERS:  *Field_desc         - Pointer to a Field_unit
 *              *Result_desc        - Pointer to an empty descriptor
 *                                    which will become a Number
 *                                    containing the field's value.
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Retrieve the value from a Field_unit
 *
 ******************************************************************************/

ACPI_STATUS
acpi_aml_get_field_unit_value (
	ACPI_OPERAND_OBJECT     *field_desc,
	ACPI_OPERAND_OBJECT     *result_desc)
{
	ACPI_STATUS             status = AE_OK;
	u32                     mask;
	u8                      *location = NULL;
	u8                      locked = FALSE;


	if (!field_desc) {
		status = AE_AML_NO_OPERAND;
	}

	if (!(field_desc->common.flags & AOPOBJ_DATA_VALID)) {
		status = acpi_ds_get_field_unit_arguments (field_desc);
		if (ACPI_FAILURE (status)) {
			return (status);
		}
	}

	if (!field_desc->field_unit.container) {
		status = AE_AML_INTERNAL;
	}

	else if (ACPI_TYPE_BUFFER != field_desc->field_unit.container->common.type) {
		status = AE_AML_OPERAND_TYPE;
	}

	else if (!result_desc) {
		status = AE_AML_INTERNAL;
	}

	if (ACPI_FAILURE (status)) {
		return (status);
	}


		/* Get the global lock if needed */

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

	/* Field location is (base of buffer) + (byte offset) */

	location = field_desc->field_unit.container->buffer.pointer
			 + field_desc->field_unit.offset;

	/*
	 * Construct Mask with as many 1 bits as the field width
	 *
	 * NOTE: Only the bottom 5 bits are valid for a shift operation, so
	 *  special care must be taken for any shift greater than 31 bits.
	 *
	 * TBD: [Unhandled] Fields greater than 32-bits will not work.
	 */

	if (field_desc->field_unit.length < 32) {
		mask = ((u32) 1 << field_desc->field_unit.length) - (u32) 1;
	}
	else {
		mask = ACPI_UINT32_MAX;
	}

	result_desc->integer.type = (u8) ACPI_TYPE_INTEGER;

	/* Get the 32 bit value at the location */

	MOVE_UNALIGNED32_TO_32 (&result_desc->integer.value, location);

	/*
	 * Shift the 32-bit word containing the field, and mask off the
	 * resulting value
	 */

	result_desc->integer.value =
		(result_desc->integer.value >> field_desc->field_unit.bit_offset) & mask;

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

	acpi_aml_release_global_lock (locked);

	return (status);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_resolve_to_value
 *
 * PARAMETERS:  **Stack_ptr         - Points to entry on Obj_stack, which can
 *                                    be either an (ACPI_OPERAND_OBJECT  *)
 *                                    or an ACPI_HANDLE.
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Convert Reference objects to values
 *
 ******************************************************************************/

ACPI_STATUS
acpi_aml_resolve_to_value (
	ACPI_OPERAND_OBJECT     **stack_ptr,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_STATUS             status = AE_OK;


	if (!stack_ptr || !*stack_ptr) {
		return (AE_AML_NO_OPERAND);
	}


	/*
	 * The entity pointed to by the Stack_ptr can be either
	 * 1) A valid ACPI_OPERAND_OBJECT, or
	 * 2) A ACPI_NAMESPACE_NODE (Named_obj)
	 */

	if (VALID_DESCRIPTOR_TYPE (*stack_ptr, ACPI_DESC_TYPE_INTERNAL)) {

		status = acpi_aml_resolve_object_to_value (stack_ptr, walk_state);
		if (ACPI_FAILURE (status)) {
			return (status);
		}
	}

	/*
	 * Object on the stack may have changed if Acpi_aml_resolve_object_to_value()
	 * was called (i.e., we can't use an _else_ here.)
	 */

	if (VALID_DESCRIPTOR_TYPE (*stack_ptr, ACPI_DESC_TYPE_NAMED)) {
		status = acpi_aml_resolve_node_to_value ((ACPI_NAMESPACE_NODE **) stack_ptr, walk_state);
	}


	return (status);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_aml_resolve_object_to_value
 *
 * PARAMETERS:  Stack_ptr       - Pointer to a stack location that contains a
 *                                ptr to an internal object.
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Retrieve the value from an internal object.  The Reference type
 *              uses the associated AML opcode to determine the value.
 *
 ******************************************************************************/

ACPI_STATUS
acpi_aml_resolve_object_to_value (
	ACPI_OPERAND_OBJECT     **stack_ptr,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_OPERAND_OBJECT     *stack_desc;
	ACPI_STATUS             status = AE_OK;
	ACPI_HANDLE             temp_handle = NULL;
	ACPI_OPERAND_OBJECT     *obj_desc = NULL;
	u32                     index = 0;
	u16                     opcode;


	stack_desc = *stack_ptr;

	/* This is an ACPI_OPERAND_OBJECT  */

	switch (stack_desc->common.type)
	{

	case INTERNAL_TYPE_REFERENCE:

		opcode = stack_desc->reference.op_code;

		switch (opcode)
		{

		case AML_NAME_OP:

			/*
			 * Convert indirect name ptr to a direct name ptr.
			 * Then, Acpi_aml_resolve_node_to_value can be used to get the value
			 */

			temp_handle = stack_desc->reference.object;

			/* Delete the Reference Object */

			acpi_cm_remove_reference (stack_desc);

			/* Put direct name pointer onto stack and exit */

			(*stack_ptr) = temp_handle;
			status = AE_OK;
			break;


		case AML_LOCAL_OP:

			index = stack_desc->reference.offset;

			/*
			 * Get the local from the method's state info
			 * Note: this increments the local's object reference count
			 */

			status = acpi_ds_method_data_get_value (MTH_TYPE_LOCAL, index,
					 walk_state, &obj_desc);
			if (ACPI_FAILURE (status)) {
				return (status);
			}

			/*
			 * Now we can delete the original Reference Object and
			 * replace it with the resolve value
			 */

			acpi_cm_remove_reference (stack_desc);
			*stack_ptr = obj_desc;

			if (ACPI_TYPE_INTEGER == obj_desc->common.type) {
				/* Value is a Number */

			}

			break;


		case AML_ARG_OP:

			index = stack_desc->reference.offset;


			/*
			 * Get the argument from the method's state info
			 * Note: this increments the object reference count
			 */

			status = acpi_ds_method_data_get_value (MTH_TYPE_ARG, index,
					 walk_state, &obj_desc);
			if (ACPI_FAILURE (status)) {
				return (status);
			}

			/*
			 * Now we can delete the original Reference Object and
			 * replace it with the resolve value
			 */

			acpi_cm_remove_reference (stack_desc);
			*stack_ptr = obj_desc;

			if (ACPI_TYPE_INTEGER == obj_desc->common.type) {
				/* Value is a Number */

			}

			break;


		/*
		 * TBD: [Restructure] These next three opcodes change the type of
		 * the object, which is actually a no-no.
		 */

		case AML_ZERO_OP:

			stack_desc->common.type = (u8) ACPI_TYPE_INTEGER;
			stack_desc->integer.value = 0;
			break;


		case AML_ONE_OP:

			stack_desc->common.type = (u8) ACPI_TYPE_INTEGER;
			stack_desc->integer.value = 1;
			break;


		case AML_ONES_OP:

			stack_desc->common.type = (u8) ACPI_TYPE_INTEGER;
			stack_desc->integer.value = ACPI_INTEGER_MAX;

			/* Truncate value if we are executing from a 32-bit ACPI table */

			acpi_aml_truncate_for32bit_table (stack_desc, walk_state);
			break;


		case AML_INDEX_OP:

			switch (stack_desc->reference.target_type)
			{
			case ACPI_TYPE_BUFFER_FIELD:

				/* Just return - leave the Reference on the stack */
				break;


			case ACPI_TYPE_PACKAGE:
				obj_desc = *stack_desc->reference.where;
				if (obj_desc) {
					/*
					 * Valid obj descriptor, copy pointer to return value
					 * (i.e., dereference the package index)
					 * Delete the ref object, increment the returned object
					 */
					acpi_cm_remove_reference (stack_desc);
					acpi_cm_add_reference (obj_desc);
					*stack_ptr = obj_desc;
				}

				else {
					/*
					 * A NULL object descriptor means an unitialized element of
					 * the package, can't deref it
					 */

					status = AE_AML_UNINITIALIZED_ELEMENT;
				}
				break;

			default:
				/* Invalid reference OBJ*/

				status = AE_AML_INTERNAL;
				break;
			}

			break;


		case AML_DEBUG_OP:

			/* Just leave the object as-is */
			break;


		default:

			status = AE_AML_INTERNAL;

		}   /* switch (Opcode) */


		if (ACPI_FAILURE (status)) {
			return (status);
		}

		break; /* case INTERNAL_TYPE_REFERENCE */


	case ACPI_TYPE_FIELD_UNIT:

		obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_ANY);
		if (!obj_desc) {
			/* Descriptor allocation failure  */

			return (AE_NO_MEMORY);
		}

		status = acpi_aml_get_field_unit_value (stack_desc, obj_desc);
		if (ACPI_FAILURE (status)) {
			acpi_cm_remove_reference (obj_desc);
			obj_desc = NULL;
		}

		*stack_ptr = (void *) obj_desc;
		break;


	case INTERNAL_TYPE_BANK_FIELD:

		obj_desc = acpi_cm_create_internal_object (ACPI_TYPE_ANY);
		if (!obj_desc) {
			/* Descriptor allocation failure */

			return (AE_NO_MEMORY);
		}

		status = acpi_aml_get_field_unit_value (stack_desc, obj_desc);
		if (ACPI_FAILURE (status)) {
			acpi_cm_remove_reference (obj_desc);
			obj_desc = NULL;
		}

		*stack_ptr = (void *) obj_desc;
		break;


	/* TBD: [Future] - may need to handle Index_field, and Def_field someday */

	default:

		break;

	}   /* switch (Stack_desc->Common.Type) */


	return (status);
}