summaryrefslogtreecommitdiffstats
path: root/drivers/acpi/interpreter/amdyadic.c
blob: 69980c293b20d7fe1b19fec024f95e2088479623 (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
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/******************************************************************************
 *
 * Module Name: amdyadic - ACPI AML (p-code) execution for dyadic operators
 *              $Revision: 63 $
 *
 *****************************************************************************/

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


#define _COMPONENT          INTERPRETER
	 MODULE_NAME         ("amdyadic")


/*****************************************************************************
 *
 * FUNCTION:    Acpi_aml_exec_dyadic1
 *
 * PARAMETERS:  Opcode              - The opcode to be executed
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Execute Type 1 dyadic operator with numeric operands:
 *              Notify_op
 *
 * ALLOCATION:  Deletes both operands
 *
 ****************************************************************************/

ACPI_STATUS
acpi_aml_exec_dyadic1 (
	u16                     opcode,
	ACPI_WALK_STATE         *walk_state)
{
	ACPI_OPERAND_OBJECT     *obj_desc = NULL;
	ACPI_OPERAND_OBJECT     *val_desc = NULL;
	ACPI_NAMESPACE_NODE     *node;
	ACPI_STATUS             status = AE_OK;


	/* Resolve all operands */

	status = acpi_aml_resolve_operands (opcode, WALK_OPERANDS, walk_state);
	/* Get the operands */

	status |= acpi_ds_obj_stack_pop_object (&val_desc, walk_state);
	status |= acpi_ds_obj_stack_pop_object (&obj_desc, walk_state);
	if (ACPI_FAILURE (status)) {
		/* Invalid parameters on object stack  */

		goto cleanup;
	}


	/* Examine the opcode */

	switch (opcode)
	{

	/* Def_notify  :=  Notify_op   Notify_object   Notify_value */

	case AML_NOTIFY_OP:

		/* The Obj_desc is actually an Node */

		node = (ACPI_NAMESPACE_NODE *) obj_desc;
		obj_desc = NULL;

		/* Object must be a device or thermal zone */

		if (node && val_desc) {
			switch (node->type)
			{
			case ACPI_TYPE_DEVICE:
			case ACPI_TYPE_THERMAL:

				/*
				 * Requires that Device and Thermal_zone be compatible
				 * mappings
				 */

				/* Dispatch the notify to the appropriate handler */

				acpi_ev_notify_dispatch (node, val_desc->number.value);
				break;

			default:
				status = AE_AML_OPERAND_TYPE;
			}
		}
		break;

	default:

		REPORT_ERROR ("Acpi_aml_exec_dyadic1: Unknown dyadic opcode");
		status = AE_AML_BAD_OPCODE;
	}


cleanup:

	/* Always delete both operands */

	acpi_cm_remove_reference (val_desc);
	acpi_cm_remove_reference (obj_desc);


	return (status);
}


/*****************************************************************************
 *
 * FUNCTION:    Acpi_aml_exec_dyadic2_r
 *
 * PARAMETERS:  Opcode              - The opcode to be executed
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Execute Type 2 dyadic operator with numeric operands and
 *              one or two result operands.
 *
 * ALLOCATION:  Deletes one operand descriptor -- other remains on stack
 *
 ****************************************************************************/

ACPI_STATUS
acpi_aml_exec_dyadic2_r (
	u16                     opcode,
	ACPI_WALK_STATE         *walk_state,
	ACPI_OPERAND_OBJECT     **return_desc)
{
	ACPI_OPERAND_OBJECT     *obj_desc   = NULL;
	ACPI_OPERAND_OBJECT     *obj_desc2  = NULL;
	ACPI_OPERAND_OBJECT     *res_desc   = NULL;
	ACPI_OPERAND_OBJECT     *res_desc2  = NULL;
	ACPI_OPERAND_OBJECT     *ret_desc   = NULL;
	ACPI_OPERAND_OBJECT     *ret_desc2  = NULL;
	ACPI_STATUS             status      = AE_OK;
	u32                     remainder;
	u32                     num_operands = 3;
	NATIVE_CHAR             *new_buf;


	/* Resolve all operands */

	status = acpi_aml_resolve_operands (opcode, WALK_OPERANDS, walk_state);
	/* Get all operands */

	if (AML_DIVIDE_OP == opcode) {
		num_operands = 4;
		status |= acpi_ds_obj_stack_pop_object (&res_desc2, walk_state);
	}

	status |= acpi_ds_obj_stack_pop_object (&res_desc, walk_state);
	status |= acpi_ds_obj_stack_pop_object (&obj_desc2, walk_state);
	status |= acpi_ds_obj_stack_pop_object (&obj_desc, walk_state);
	if (ACPI_FAILURE (status)) {
		goto cleanup;
	}


	/* Create an internal return object if necessary */

	switch (opcode)
	{
	case AML_ADD_OP:
	case AML_BIT_AND_OP:
	case AML_BIT_NAND_OP:
	case AML_BIT_OR_OP:
	case AML_BIT_NOR_OP:
	case AML_BIT_XOR_OP:
	case AML_DIVIDE_OP:
	case AML_MULTIPLY_OP:
	case AML_SHIFT_LEFT_OP:
	case AML_SHIFT_RIGHT_OP:
	case AML_SUBTRACT_OP:

		ret_desc = acpi_cm_create_internal_object (ACPI_TYPE_NUMBER);
		if (!ret_desc) {
			status = AE_NO_MEMORY;
			goto cleanup;
		}

		break;
	}


	/*
	 * Execute the opcode
	 */

	switch (opcode)
	{

	/* Def_add :=  Add_op  Operand1    Operand2    Result  */

	case AML_ADD_OP:

		ret_desc->number.value = obj_desc->number.value +
				 obj_desc2->number.value;
		break;


	/* Def_and :=  And_op  Operand1    Operand2    Result  */

	case AML_BIT_AND_OP:

		ret_desc->number.value = obj_desc->number.value &
				 obj_desc2->number.value;
		break;


	/* Def_nAnd := NAnd_op Operand1    Operand2    Result  */

	case AML_BIT_NAND_OP:

		ret_desc->number.value = ~(obj_desc->number.value &
				   obj_desc2->number.value);
		break;


	/* Def_or  :=  Or_op   Operand1    Operand2    Result  */

	case AML_BIT_OR_OP:

		ret_desc->number.value = obj_desc->number.value |
				 obj_desc2->number.value;
		break;


	/* Def_nOr :=  NOr_op  Operand1    Operand2    Result  */

	case AML_BIT_NOR_OP:

		ret_desc->number.value = ~(obj_desc->number.value |
				   obj_desc2->number.value);
		break;


	/* Def_xOr :=  XOr_op  Operand1    Operand2    Result  */

	case AML_BIT_XOR_OP:

		ret_desc->number.value = obj_desc->number.value ^
				 obj_desc2->number.value;
		break;


	/* Def_divide  :=  Divide_op Dividend Divisor Remainder Quotient   */

	case AML_DIVIDE_OP:

		if ((u32) 0 == obj_desc2->number.value) {
			REPORT_ERROR ("Aml_exec_dyadic2_r/Divide_op: Divide by zero");

			status = AE_AML_DIVIDE_BY_ZERO;
			goto cleanup;
		}

		ret_desc2 = acpi_cm_create_internal_object (ACPI_TYPE_NUMBER);
		if (!ret_desc2) {
			status = AE_NO_MEMORY;
			goto cleanup;
		}

		remainder               = obj_desc->number.value %
				   obj_desc2->number.value;
		ret_desc->number.value  = remainder;

		/* Result (what we used to call the quotient) */

		ret_desc2->number.value = obj_desc->number.value /
				  obj_desc2->number.value;
		break;


	/* Def_multiply := Multiply_op Operand1    Operand2    Result  */

	case AML_MULTIPLY_OP:

		ret_desc->number.value = obj_desc->number.value *
				 obj_desc2->number.value;
		break;


	/* Def_shift_left  :=  Shift_left_op Operand Shift_count Result */

	case AML_SHIFT_LEFT_OP:

		ret_desc->number.value = obj_desc->number.value <<
				 obj_desc2->number.value;
		break;


	/* Def_shift_right :=  Shift_right_op  Operand Shift_count Result  */

	case AML_SHIFT_RIGHT_OP:

		ret_desc->number.value = obj_desc->number.value >>
				 obj_desc2->number.value;
		break;


	/* Def_subtract := Subtract_op Operand1    Operand2    Result  */

	case AML_SUBTRACT_OP:

		ret_desc->number.value = obj_desc->number.value -
				 obj_desc2->number.value;
		break;


	/* Def_concat  :=  Concat_op   Data1   Data2   Result  */

	case AML_CONCAT_OP:

		if (obj_desc2->common.type != obj_desc->common.type) {
			status = AE_AML_OPERAND_TYPE;
			goto cleanup;
		}

		/* Both operands are now known to be the same */

		if (ACPI_TYPE_STRING == obj_desc->common.type) {
			ret_desc = acpi_cm_create_internal_object (ACPI_TYPE_STRING);
			if (!ret_desc) {
				status = AE_NO_MEMORY;
				goto cleanup;
			}

			/* Operand1 is string  */

			new_buf = acpi_cm_allocate (obj_desc->string.length +
					  obj_desc2->string.length + 1);
			if (!new_buf) {
				REPORT_ERROR
					("Aml_exec_dyadic2_r/Concat_op: String allocation failure");
				status = AE_NO_MEMORY;
				goto cleanup;
			}

			STRCPY (new_buf, obj_desc->string.pointer);
			STRCPY (new_buf + obj_desc->string.length,
					  obj_desc2->string.pointer);

			/* Point the return object to the new string */

			ret_desc->string.pointer = new_buf;
			ret_desc->string.length = obj_desc->string.length +=
					  obj_desc2->string.length;
		}

		else {
			/* Operand1 is not a string ==> must be a buffer */

			ret_desc = acpi_cm_create_internal_object (ACPI_TYPE_BUFFER);
			if (!ret_desc) {
				status = AE_NO_MEMORY;
				goto cleanup;
			}

			new_buf = acpi_cm_allocate (obj_desc->buffer.length +
					  obj_desc2->buffer.length);
			if (!new_buf) {
				REPORT_ERROR
					("Aml_exec_dyadic2_r/Concat_op: Buffer allocation failure");
				status = AE_NO_MEMORY;
				goto cleanup;
			}

			MEMCPY (new_buf, obj_desc->buffer.pointer,
					  obj_desc->buffer.length);
			MEMCPY (new_buf + obj_desc->buffer.length, obj_desc2->buffer.pointer,
					  obj_desc2->buffer.length);

			/*
			 * Point the return object to the new buffer
			 */

			ret_desc->buffer.pointer    = (u8 *) new_buf;
			ret_desc->buffer.length     = obj_desc->buffer.length +
					 obj_desc2->buffer.length;
		}
		break;


	default:

		REPORT_ERROR ("Acpi_aml_exec_dyadic2_r: Unknown dyadic opcode");
		status = AE_AML_BAD_OPCODE;
		goto cleanup;
	}


	/*
	 * Store the result of the operation (which is now in Obj_desc) into
	 * the result descriptor, or the location pointed to by the result
	 * descriptor (Res_desc).
	 */

	status = acpi_aml_exec_store (ret_desc, res_desc, walk_state);
	if (ACPI_FAILURE (status)) {
		goto cleanup;
	}

	if (AML_DIVIDE_OP == opcode) {
		status = acpi_aml_exec_store (ret_desc2, res_desc2, walk_state);

		/*
		 * Since the remainder is not returned, remove a reference to
		 * the object we created earlier
		 */

		acpi_cm_remove_reference (ret_desc2);
	}


cleanup:

	/* Always delete the operands */

	acpi_cm_remove_reference (obj_desc);
	acpi_cm_remove_reference (obj_desc2);


	/* Delete return object on error */

	if (ACPI_FAILURE (status)) {
		/* On failure, delete the result ops */

		acpi_cm_remove_reference (res_desc);
		acpi_cm_remove_reference (res_desc2);

		if (ret_desc) {
			/* And delete the internal return object */

			acpi_cm_remove_reference (ret_desc);
			ret_desc = NULL;
		}
	}

	/* Set the return object and exit */

	*return_desc = ret_desc;
	return (status);
}


/*****************************************************************************
 *
 * FUNCTION:    Acpi_aml_exec_dyadic2_s
 *
 * PARAMETERS:  Opcode              - The opcode to be executed
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Execute Type 2 dyadic synchronization operator
 *
 * ALLOCATION:  Deletes one operand descriptor -- other remains on stack
 *
 ****************************************************************************/

ACPI_STATUS
acpi_aml_exec_dyadic2_s (
	u16                     opcode,
	ACPI_WALK_STATE         *walk_state,
	ACPI_OPERAND_OBJECT     **return_desc)
{
	ACPI_OPERAND_OBJECT     *obj_desc;
	ACPI_OPERAND_OBJECT     *time_desc;
	ACPI_OPERAND_OBJECT     *ret_desc = NULL;
	ACPI_STATUS             status;


	/* Resolve all operands */

	status = acpi_aml_resolve_operands (opcode, WALK_OPERANDS, walk_state);
	/* Get all operands */

	status |= acpi_ds_obj_stack_pop_object (&time_desc, walk_state);
	status |= acpi_ds_obj_stack_pop_object (&obj_desc, walk_state);
	if (ACPI_FAILURE (status)) {
		/* Invalid parameters on object stack  */

		goto cleanup;
	}


	/* Create the internal return object */

	ret_desc = acpi_cm_create_internal_object (ACPI_TYPE_NUMBER);
	if (!ret_desc) {
		status = AE_NO_MEMORY;
		goto cleanup;
	}

	/* Default return value is FALSE, operation did not time out */

	ret_desc->number.value = 0;


	/* Examine the opcode */

	switch (opcode)
	{

	/* Def_acquire :=  Acquire_op  Mutex_object Timeout */

	case AML_ACQUIRE_OP:

		status = acpi_aml_system_acquire_mutex (time_desc, obj_desc);
		break;


	/* Def_wait := Wait_op Acpi_event_object Timeout */

	case AML_WAIT_OP:

		status = acpi_aml_system_wait_event (time_desc, obj_desc);
		break;


	default:

		REPORT_ERROR ("Acpi_aml_exec_dyadic2_s: Unknown dyadic synchronization opcode");
		status = AE_AML_BAD_OPCODE;
		goto cleanup;
	}


	/*
	 * Return a boolean indicating if operation timed out
	 * (TRUE) or not (FALSE)
	 */

	if (status == AE_TIME) {
		ret_desc->number.value = (u32)(-1);  /* TRUE, op timed out */
		status = AE_OK;
	}


cleanup:

	/* Delete params */

	acpi_cm_remove_reference (time_desc);
	acpi_cm_remove_reference (obj_desc);

	/* Delete return object on error */

	if (ACPI_FAILURE (status) &&
		(ret_desc))
	{
		acpi_cm_remove_reference (ret_desc);
		ret_desc = NULL;
	}


	/* Set the return object and exit */

	*return_desc = ret_desc;
	return (status);
}


/*****************************************************************************
 *
 * FUNCTION:    Acpi_aml_exec_dyadic2
 *
 * PARAMETERS:  Opcode              - The opcode to be executed
 *
 * RETURN:      Status
 *
 * DESCRIPTION: Execute Type 2 dyadic operator with numeric operands and
 *              no result operands
 *
 * ALLOCATION:  Deletes one operand descriptor -- other remains on stack
 *              containing result value
 *
 ****************************************************************************/

ACPI_STATUS
acpi_aml_exec_dyadic2 (
	u16                     opcode,
	ACPI_WALK_STATE         *walk_state,
	ACPI_OPERAND_OBJECT     **return_desc)
{
	ACPI_OPERAND_OBJECT     *obj_desc;
	ACPI_OPERAND_OBJECT     *obj_desc2;
	ACPI_OPERAND_OBJECT     *ret_desc = NULL;
	ACPI_STATUS             status;
	u8                      lboolean;


	/* Resolve all operands */

	status = acpi_aml_resolve_operands (opcode, WALK_OPERANDS, walk_state);
	/* Get all operands */

	status |= acpi_ds_obj_stack_pop_object (&obj_desc2, walk_state);
	status |= acpi_ds_obj_stack_pop_object (&obj_desc, walk_state);
	if (ACPI_FAILURE (status)) {
		/* Invalid parameters on object stack  */

		goto cleanup;
	}


	/* Create the internal return object */

	ret_desc = acpi_cm_create_internal_object (ACPI_TYPE_NUMBER);
	if (!ret_desc) {
		status = AE_NO_MEMORY;
		goto cleanup;
	}

	/*
	 * Execute the Opcode
	 */

	lboolean = FALSE;
	switch (opcode)
	{

	/* Def_lAnd := LAnd_op Operand1    Operand2    */

	case AML_LAND_OP:

		lboolean = (u8) (obj_desc->number.value &&
				  obj_desc2->number.value);
		break;


	/* Def_lEqual  :=  LEqual_op   Operand1    Operand2    */

	case AML_LEQUAL_OP:

		lboolean = (u8) (obj_desc->number.value ==
				  obj_desc2->number.value);
		break;


	/* Def_lGreater := LGreater_op Operand1    Operand2    */

	case AML_LGREATER_OP:

		lboolean = (u8) (obj_desc->number.value >
				  obj_desc2->number.value);
		break;


	/* Def_lLess   :=  LLess_op Operand1   Operand2    */

	case AML_LLESS_OP:

		lboolean = (u8) (obj_desc->number.value <
				  obj_desc2->number.value);
		break;


	/* Def_lOr :=  LOr_op  Operand1    Operand2    */

	case AML_LOR_OP:

		lboolean = (u8) (obj_desc->number.value ||
				  obj_desc2->number.value);
		break;


	default:

		REPORT_ERROR ("Acpi_aml_exec_dyadic2: Unknown dyadic opcode");
		status = AE_AML_BAD_OPCODE;
		goto cleanup;
		break;
	}


	/* Set return value to logical TRUE (all ones) or FALSE (zero) */

	if (lboolean) {
		ret_desc->number.value = 0xffffffff;
	}
	else {
		ret_desc->number.value = 0;
	}


cleanup:

	/* Always delete operands */

	acpi_cm_remove_reference (obj_desc);
	acpi_cm_remove_reference (obj_desc2);


	/* Delete return object on error */

	if (ACPI_FAILURE (status) &&
		(ret_desc))
	{
		acpi_cm_remove_reference (ret_desc);
		ret_desc = NULL;
	}


	/* Set the return object and exit */

	*return_desc = ret_desc;
	return (status);
}