summaryrefslogtreecommitdiffstats
path: root/drivers/acpi/dispatcher/dsmthdat.c
blob: e6913fabf4345a0659d04e58c8bcea20687e53fe (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
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
/*******************************************************************************
 *
 * Module Name: dsmthdat - control method arguments and local variables
 *              $Revision: 36 $
 *
 ******************************************************************************/

/*
 *  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 "acparser.h"
#include "acdispat.h"
#include "acinterp.h"
#include "amlcode.h"
#include "acnamesp.h"


#define _COMPONENT          DISPATCHER
	 MODULE_NAME         ("dsmthdat")


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_init
 *
 * PARAMETERS:  *Obj_desc
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Initialize the data structures that hold the method's arguments
 *              and locals.  The data struct is an array of NTEs for each.
 *              This allows Ref_of and De_ref_of to work properly for these
 *              special data types.
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_init (
	ACPI_WALK_STATE         *walk_state)
{
	u32                     i;


	/*
	 * Walk_state fields are initialized to zero by the
	 * Acpi_cm_callocate().
	 *
	 * An Node is assigned to each argument and local so
	 * that Ref_of() can return a pointer to the Node.
	 */

	/* Init the method arguments */

	for (i = 0; i < MTH_NUM_ARGS; i++) {
		MOVE_UNALIGNED32_TO_32 (&walk_state->arguments[i].name,
				 NAMEOF_ARG_NTE);
		walk_state->arguments[i].name      |= (i << 24);
		walk_state->arguments[i].data_type  = ACPI_DESC_TYPE_NAMED;
		walk_state->arguments[i].type       = ACPI_TYPE_ANY;
		walk_state->arguments[i].flags      = ANOBJ_END_OF_PEER_LIST | ANOBJ_METHOD_ARG;
	}

	/* Init the method locals */

	for (i = 0; i < MTH_NUM_LOCALS; i++) {
		MOVE_UNALIGNED32_TO_32 (&walk_state->local_variables[i].name,
				 NAMEOF_LOCAL_NTE);

		walk_state->local_variables[i].name  |= (i << 24);
		walk_state->local_variables[i].data_type = ACPI_DESC_TYPE_NAMED;
		walk_state->local_variables[i].type   = ACPI_TYPE_ANY;
		walk_state->local_variables[i].flags  = ANOBJ_END_OF_PEER_LIST | ANOBJ_METHOD_LOCAL;
	}

	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_delete_all
 *
 * PARAMETERS:  None
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Delete method locals and arguments.  Arguments are only
 *              deleted if this method was called from another method.
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_delete_all (
	ACPI_WALK_STATE         *walk_state)
{
	u32                     index;
	ACPI_OPERAND_OBJECT     *object;


	/* Delete the locals */

	for (index = 0; index < MTH_NUM_LOCALS; index++) {
		object = walk_state->local_variables[index].object;
		if (object) {
			/* Remove first */

			walk_state->local_variables[index].object = NULL;

			/* Was given a ref when stored */

			acpi_cm_remove_reference (object);
	   }
	}


	/* Delete the arguments */

	for (index = 0; index < MTH_NUM_ARGS; index++) {
		object = walk_state->arguments[index].object;
		if (object) {
			/* Remove first */

			walk_state->arguments[index].object = NULL;

			 /* Was given a ref when stored */

			acpi_cm_remove_reference (object);
		}
	}

	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_init_args
 *
 * PARAMETERS:  None
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Initialize arguments for a method
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_init_args (
	ACPI_OPERAND_OBJECT     **params,
	u32                     max_param_count,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_STATUS             status;
	u32                     mindex;
	u32                     pindex;


	if (!params) {
		return (AE_OK);
	}

	/* Copy passed parameters into the new method stack frame  */

	for (pindex = mindex = 0;
		(mindex < MTH_NUM_ARGS) && (pindex < max_param_count);
		mindex++)
	{
		if (params[pindex]) {
			/*
			 * A valid parameter.
			 * Set the current method argument to the
			 * Params[Pindex++] argument object descriptor
			 */
			status = acpi_ds_method_data_set_value (MTH_TYPE_ARG, mindex,
					 params[pindex], walk_state);
			if (ACPI_FAILURE (status)) {
				break;
			}

			pindex++;
		}

		else {
			break;
		}
	}

	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_get_entry
 *
 * PARAMETERS:  Type                - Either MTH_TYPE_LOCAL or MTH_TYPE_ARG
 *              Index               - Which local_var or argument to get
 *              Entry               - Pointer to where a pointer to the stack
 *                                    entry is returned.
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Get the address of the stack entry given by Type:Index
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_get_entry (
	u32                     type,
	u32                     index,
	ACPI_WALK_STATE         *walk_state,
	ACPI_OPERAND_OBJECT     ***entry)
{


	/*
	 * Get the requested object.
	 * The stack "Type" is either a Local_variable or an Argument
	 */

	switch (type)
	{

	case MTH_TYPE_LOCAL:

		if (index > MTH_MAX_LOCAL) {
			return (AE_BAD_PARAMETER);
		}

		*entry =
			(ACPI_OPERAND_OBJECT  **) &walk_state->local_variables[index].object;
		break;


	case MTH_TYPE_ARG:

		if (index > MTH_MAX_ARG) {
			return (AE_BAD_PARAMETER);
		}

		*entry =
			(ACPI_OPERAND_OBJECT  **) &walk_state->arguments[index].object;
		break;


	default:
		return (AE_BAD_PARAMETER);
	}


	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_set_entry
 *
 * PARAMETERS:  Type                - Either MTH_TYPE_LOCAL or MTH_TYPE_ARG
 *              Index               - Which local_var or argument to get
 *              Object              - Object to be inserted into the stack entry
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Insert an object onto the method stack at entry Type:Index.
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_set_entry (
	u32                     type,
	u32                     index,
	ACPI_OPERAND_OBJECT     *object,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_STATUS             status;
	ACPI_OPERAND_OBJECT     **entry;


	/* Get a pointer to the stack entry to set */

	status = acpi_ds_method_data_get_entry (type, index, walk_state, &entry);
	if (ACPI_FAILURE (status)) {
		return (status);
	}

	/* Increment ref count so object can't be deleted while installed */

	acpi_cm_add_reference (object);

	/* Install the object into the stack entry */

	*entry = object;

	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_get_type
 *
 * PARAMETERS:  Type                - Either MTH_TYPE_LOCAL or MTH_TYPE_ARG
 *              Index               - Which local_var or argument whose type
 *                                      to get
 *
 * RETURN:      Data type of selected Arg or Local
 *              Used only in Exec_monadic2()/Type_op.
 *
 ******************************************************************************/

OBJECT_TYPE_INTERNAL
acpi_ds_method_data_get_type (
	u32                     type,
	u32                     index,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_STATUS             status;
	ACPI_OPERAND_OBJECT     **entry;
	ACPI_OPERAND_OBJECT     *object;


	/* Get a pointer to the requested stack entry */

	status = acpi_ds_method_data_get_entry (type, index, walk_state, &entry);
	if (ACPI_FAILURE (status)) {
		return ((ACPI_TYPE_NOT_FOUND));
	}

	/* Get the object from the method stack */

	object = *entry;

	/* Get the object type */

	if (!object) {
		/* Any == 0 => "uninitialized" -- see spec 15.2.3.5.2.28 */
		return (ACPI_TYPE_ANY);
	}

	return (object->common.type);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_get_nte
 *
 * PARAMETERS:  Type                - Either MTH_TYPE_LOCAL or MTH_TYPE_ARG
 *              Index               - Which local_var or argument whose type
 *                                      to get
 *
 * RETURN:      Get the Node associated with a local or arg.
 *
 ******************************************************************************/

ACPI_NAMESPACE_NODE *
acpi_ds_method_data_get_nte (
	u32                     type,
	u32                     index,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_NAMESPACE_NODE     *node = NULL;


	switch (type)
	{

	case MTH_TYPE_LOCAL:

		if (index > MTH_MAX_LOCAL) {
			return (node);
		}

		node =  &walk_state->local_variables[index];
		break;


	case MTH_TYPE_ARG:

		if (index > MTH_MAX_ARG) {
			return (node);
		}

		node = &walk_state->arguments[index];
		break;


	default:
		break;
	}


	return (node);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_get_value
 *
 * PARAMETERS:  Type                - Either MTH_TYPE_LOCAL or MTH_TYPE_ARG
 *              Index               - Which local_var or argument to get
 *              *Dest_desc           - Descriptor into which selected Arg
 *                                    or Local value should be copied
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Retrieve value of selected Arg or Local from the method frame
 *              at the current top of the method stack.
 *              Used only in Acpi_aml_resolve_to_value().
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_get_value (
	u32                     type,
	u32                     index,
	ACPI_WALK_STATE         *walk_state,
	ACPI_OPERAND_OBJECT     **dest_desc)
{
	ACPI_STATUS             status;
	ACPI_OPERAND_OBJECT     **entry;
	ACPI_OPERAND_OBJECT     *object;


	/* Validate the object descriptor */

	if (!dest_desc) {
		return (AE_BAD_PARAMETER);
	}


	/* Get a pointer to the requested method stack entry */

	status = acpi_ds_method_data_get_entry (type, index, walk_state, &entry);
	if (ACPI_FAILURE (status)) {
		return (status);
	}

	/* Get the object from the method stack */

	object = *entry;


	/* Examine the returned object, it must be valid. */

	if (!object) {
		/*
		 * Index points to uninitialized object stack value.
		 * This means that either 1) The expected argument was
		 * not passed to the method, or 2) A local variable
		 * was referenced by the method (via the ASL)
		 * before it was initialized.  Either case is an error.
		 */

		switch (type)
		{
		case MTH_TYPE_ARG:
			return (AE_AML_UNINITIALIZED_ARG);
			break;

		case MTH_TYPE_LOCAL:
			return (AE_AML_UNINITIALIZED_LOCAL);
			break;
		}
	}


	/*
	 * Index points to initialized and valid object stack value.
	 * Return an additional reference to the object
	 */

	*dest_desc = object;
	acpi_cm_add_reference (object);

	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_delete_value
 *
 * PARAMETERS:  Type                - Either MTH_TYPE_LOCAL or MTH_TYPE_ARG
 *              Index               - Which local_var or argument to delete
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Delete the entry at Type:Index on the method stack.  Inserts
 *              a null into the stack slot after the object is deleted.
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_delete_value (
	u32                     type,
	u32                     index,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_STATUS             status;
	ACPI_OPERAND_OBJECT     **entry;
	ACPI_OPERAND_OBJECT     *object;


	/* Get a pointer to the requested entry */

	status = acpi_ds_method_data_get_entry (type, index, walk_state, &entry);
	if (ACPI_FAILURE (status)) {
		return (status);
	}

	/* Get the current entry in this slot k */

	object = *entry;

	/*
	 * Undefine the Arg or Local by setting its descriptor
	 * pointer to NULL. Locals/Args can contain both
	 * ACPI_OPERAND_OBJECTS and ACPI_NAMESPACE_NODEs
	 */
	*entry = NULL;


	if ((object) &&
		(VALID_DESCRIPTOR_TYPE (object, ACPI_DESC_TYPE_INTERNAL)))
	{
		/*
		 * There is a valid object in this slot
		 * Decrement the reference count by one to balance the
		 * increment when the object was stored in the slot.
		 */

		acpi_cm_remove_reference (object);
	}


	return (AE_OK);
}


/*******************************************************************************
 *
 * FUNCTION:    Acpi_ds_method_data_set_value
 *
 * PARAMETERS:  Type                - Either MTH_TYPE_LOCAL or MTH_TYPE_ARG
 *              Index               - Which local_var or argument to set
 *              *Src_desc           - Value to be stored
 *              *Dest_desc          - Descriptor into which *Src_desc
 *                                    can be copied, or NULL if one must
 *                                    be allocated for the purpose.  If
 *                                    provided, this descriptor will be
 *                                    used for the new value.
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Store a value in an Arg or Local.  The Src_desc is installed
 *              as the new value for the Arg or Local and the reference count
 *              is incremented.
 *
 ******************************************************************************/

ACPI_STATUS
acpi_ds_method_data_set_value (
	u32                     type,
	u32                     index,
	ACPI_OPERAND_OBJECT     *src_desc,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_STATUS             status;
	ACPI_OPERAND_OBJECT     **entry;


	/* Parameter validation */

	if (!src_desc) {
		return (AE_BAD_PARAMETER);
	}


	/* Get a pointer to the requested method stack entry */

	status = acpi_ds_method_data_get_entry (type, index, walk_state, &entry);
	if (ACPI_FAILURE (status)) {
		goto cleanup;
	}

	if (*entry == src_desc) {
		goto cleanup;
	}


	/*
	 * If there is an object already in this slot, we either
	 * have to delete it, or if this is an argument and there
	 * is an object reference stored there, we have to do
	 * an indirect store!
	 */

	if (*entry) {
		/*
		 * Check for an indirect store if an argument
		 * contains an object reference (stored as an Node).
		 * We don't allow this automatic dereferencing for
		 * locals, since a store to a local should overwrite
		 * anything there, including an object reference.
		 *
		 * If both Arg0 and Local0 contain Ref_of (Local4):
		 *
		 * Store (1, Arg0)             - Causes indirect store to local4
		 * Store (1, Local0)           - Stores 1 in local0, overwriting
		 *                                  the reference to local4
		 * Store (1, De_refof (Local0)) - Causes indirect store to local4
		 *
		 * Weird, but true.
		 */

		if ((type == MTH_TYPE_ARG) &&
			(VALID_DESCRIPTOR_TYPE (*entry, ACPI_DESC_TYPE_NAMED)))
		{
			/* Detach an existing object from the Node */

			acpi_ns_detach_object ((ACPI_NAMESPACE_NODE *) *entry);

			/*
			 * Store this object into the Node
			 * (do the indirect store)
			 */

			status = acpi_ns_attach_object ((ACPI_NAMESPACE_NODE *) *entry, src_desc,
					   src_desc->common.type);
			return (status);
		}


		/*
		 * Otherwise, just delete the existing object
		 * before storing the new one
		 */

		acpi_ds_method_data_delete_value (type, index, walk_state);
	}


	/*
	 * Install the Obj_stack descriptor (*Src_desc) into
	 * the descriptor for the Arg or Local.
	 * Install the new object in the stack entry
	 * (increments the object reference count by one)
	 */

	status = acpi_ds_method_data_set_entry (type, index, src_desc, walk_state);
	if (ACPI_FAILURE (status)) {
		goto cleanup;
	}

	/* Normal exit */

	return (AE_OK);


	/* Error exit */

cleanup:

	return (status);
}