97b8be82ddd30c944eb699f677e8610d6128cc0a
[reactos.git] / reactos / drivers / bus / acpi / acpica / executer / exstore.c
1 /******************************************************************************
2 *
3 * Module Name: exstore - AML Interpreter object store support
4 *
5 *****************************************************************************/
6
7 /******************************************************************************
8 *
9 * 1. Copyright Notice
10 *
11 * Some or all of this work - Copyright (c) 1999 - 2014, Intel Corp.
12 * All rights reserved.
13 *
14 * 2. License
15 *
16 * 2.1. This is your license from Intel Corp. under its intellectual property
17 * rights. You may have additional license terms from the party that provided
18 * you this software, covering your right to use that party's intellectual
19 * property rights.
20 *
21 * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
22 * copy of the source code appearing in this file ("Covered Code") an
23 * irrevocable, perpetual, worldwide license under Intel's copyrights in the
24 * base code distributed originally by Intel ("Original Intel Code") to copy,
25 * make derivatives, distribute, use and display any portion of the Covered
26 * Code in any form, with the right to sublicense such rights; and
27 *
28 * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
29 * license (with the right to sublicense), under only those claims of Intel
30 * patents that are infringed by the Original Intel Code, to make, use, sell,
31 * offer to sell, and import the Covered Code and derivative works thereof
32 * solely to the minimum extent necessary to exercise the above copyright
33 * license, and in no event shall the patent license extend to any additions
34 * to or modifications of the Original Intel Code. No other license or right
35 * is granted directly or by implication, estoppel or otherwise;
36 *
37 * The above copyright and patent license is granted only if the following
38 * conditions are met:
39 *
40 * 3. Conditions
41 *
42 * 3.1. Redistribution of Source with Rights to Further Distribute Source.
43 * Redistribution of source code of any substantial portion of the Covered
44 * Code or modification with rights to further distribute source must include
45 * the above Copyright Notice, the above License, this list of Conditions,
46 * and the following Disclaimer and Export Compliance provision. In addition,
47 * Licensee must cause all Covered Code to which Licensee contributes to
48 * contain a file documenting the changes Licensee made to create that Covered
49 * Code and the date of any change. Licensee must include in that file the
50 * documentation of any changes made by any predecessor Licensee. Licensee
51 * must include a prominent statement that the modification is derived,
52 * directly or indirectly, from Original Intel Code.
53 *
54 * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
55 * Redistribution of source code of any substantial portion of the Covered
56 * Code or modification without rights to further distribute source must
57 * include the following Disclaimer and Export Compliance provision in the
58 * documentation and/or other materials provided with distribution. In
59 * addition, Licensee may not authorize further sublicense of source of any
60 * portion of the Covered Code, and must include terms to the effect that the
61 * license from Licensee to its licensee is limited to the intellectual
62 * property embodied in the software Licensee provides to its licensee, and
63 * not to intellectual property embodied in modifications its licensee may
64 * make.
65 *
66 * 3.3. Redistribution of Executable. Redistribution in executable form of any
67 * substantial portion of the Covered Code or modification must reproduce the
68 * above Copyright Notice, and the following Disclaimer and Export Compliance
69 * provision in the documentation and/or other materials provided with the
70 * distribution.
71 *
72 * 3.4. Intel retains all right, title, and interest in and to the Original
73 * Intel Code.
74 *
75 * 3.5. Neither the name Intel nor any other trademark owned or controlled by
76 * Intel shall be used in advertising or otherwise to promote the sale, use or
77 * other dealings in products derived from or relating to the Covered Code
78 * without prior written authorization from Intel.
79 *
80 * 4. Disclaimer and Export Compliance
81 *
82 * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
83 * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
84 * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
85 * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
86 * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
87 * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
88 * PARTICULAR PURPOSE.
89 *
90 * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
91 * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
92 * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
93 * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
94 * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
95 * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
96 * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
97 * LIMITED REMEDY.
98 *
99 * 4.3. Licensee shall not export, either directly or indirectly, any of this
100 * software or system incorporating such software without first obtaining any
101 * required license or other approval from the U. S. Department of Commerce or
102 * any other agency or department of the United States Government. In the
103 * event Licensee exports any such software from the United States or
104 * re-exports any such software from a foreign destination, Licensee shall
105 * ensure that the distribution and export/re-export of the software is in
106 * compliance with all laws, regulations, orders, or other restrictions of the
107 * U.S. Export Administration Regulations. Licensee agrees that neither it nor
108 * any of its subsidiaries will export/re-export any technical data, process,
109 * software, or service, directly or indirectly, to any country for which the
110 * United States government or any agency thereof requires an export license,
111 * other governmental approval, or letter of assurance, without first obtaining
112 * such license, approval or letter.
113 *
114 *****************************************************************************/
115
116 #define __EXSTORE_C__
117
118 #include "acpi.h"
119 #include "accommon.h"
120 #include "acdispat.h"
121 #include "acinterp.h"
122 #include "amlcode.h"
123 #include "acnamesp.h"
124
125
126 #define _COMPONENT ACPI_EXECUTER
127 ACPI_MODULE_NAME ("exstore")
128
129 /* Local prototypes */
130
131 static ACPI_STATUS
132 AcpiExStoreObjectToIndex (
133 ACPI_OPERAND_OBJECT *ValDesc,
134 ACPI_OPERAND_OBJECT *DestDesc,
135 ACPI_WALK_STATE *WalkState);
136
137 static ACPI_STATUS
138 AcpiExStoreDirectToNode (
139 ACPI_OPERAND_OBJECT *SourceDesc,
140 ACPI_NAMESPACE_NODE *Node,
141 ACPI_WALK_STATE *WalkState);
142
143
144 /*******************************************************************************
145 *
146 * FUNCTION: AcpiExStore
147 *
148 * PARAMETERS: *SourceDesc - Value to be stored
149 * *DestDesc - Where to store it. Must be an NS node
150 * or ACPI_OPERAND_OBJECT of type
151 * Reference;
152 * WalkState - Current walk state
153 *
154 * RETURN: Status
155 *
156 * DESCRIPTION: Store the value described by SourceDesc into the location
157 * described by DestDesc. Called by various interpreter
158 * functions to store the result of an operation into
159 * the destination operand -- not just simply the actual "Store"
160 * ASL operator.
161 *
162 ******************************************************************************/
163
164 ACPI_STATUS
165 AcpiExStore (
166 ACPI_OPERAND_OBJECT *SourceDesc,
167 ACPI_OPERAND_OBJECT *DestDesc,
168 ACPI_WALK_STATE *WalkState)
169 {
170 ACPI_STATUS Status = AE_OK;
171 ACPI_OPERAND_OBJECT *RefDesc = DestDesc;
172
173
174 ACPI_FUNCTION_TRACE_PTR (ExStore, DestDesc);
175
176
177 /* Validate parameters */
178
179 if (!SourceDesc || !DestDesc)
180 {
181 ACPI_ERROR ((AE_INFO, "Null parameter"));
182 return_ACPI_STATUS (AE_AML_NO_OPERAND);
183 }
184
185 /* DestDesc can be either a namespace node or an ACPI object */
186
187 if (ACPI_GET_DESCRIPTOR_TYPE (DestDesc) == ACPI_DESC_TYPE_NAMED)
188 {
189 /*
190 * Dest is a namespace node,
191 * Storing an object into a Named node.
192 */
193 Status = AcpiExStoreObjectToNode (SourceDesc,
194 (ACPI_NAMESPACE_NODE *) DestDesc, WalkState,
195 ACPI_IMPLICIT_CONVERSION);
196
197 return_ACPI_STATUS (Status);
198 }
199
200 /* Destination object must be a Reference or a Constant object */
201
202 switch (DestDesc->Common.Type)
203 {
204 case ACPI_TYPE_LOCAL_REFERENCE:
205
206 break;
207
208 case ACPI_TYPE_INTEGER:
209
210 /* Allow stores to Constants -- a Noop as per ACPI spec */
211
212 if (DestDesc->Common.Flags & AOPOBJ_AML_CONSTANT)
213 {
214 return_ACPI_STATUS (AE_OK);
215 }
216
217 /*lint -fallthrough */
218
219 default:
220
221 /* Destination is not a Reference object */
222
223 ACPI_ERROR ((AE_INFO,
224 "Target is not a Reference or Constant object - %s [%p]",
225 AcpiUtGetObjectTypeName (DestDesc), DestDesc));
226
227 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
228 }
229
230 /*
231 * Examine the Reference class. These cases are handled:
232 *
233 * 1) Store to Name (Change the object associated with a name)
234 * 2) Store to an indexed area of a Buffer or Package
235 * 3) Store to a Method Local or Arg
236 * 4) Store to the debug object
237 */
238 switch (RefDesc->Reference.Class)
239 {
240 case ACPI_REFCLASS_REFOF:
241
242 /* Storing an object into a Name "container" */
243
244 Status = AcpiExStoreObjectToNode (SourceDesc,
245 RefDesc->Reference.Object,
246 WalkState, ACPI_IMPLICIT_CONVERSION);
247 break;
248
249 case ACPI_REFCLASS_INDEX:
250
251 /* Storing to an Index (pointer into a packager or buffer) */
252
253 Status = AcpiExStoreObjectToIndex (SourceDesc, RefDesc, WalkState);
254 break;
255
256 case ACPI_REFCLASS_LOCAL:
257 case ACPI_REFCLASS_ARG:
258
259 /* Store to a method local/arg */
260
261 Status = AcpiDsStoreObjectToLocal (RefDesc->Reference.Class,
262 RefDesc->Reference.Value, SourceDesc, WalkState);
263 break;
264
265 case ACPI_REFCLASS_DEBUG:
266 /*
267 * Storing to the Debug object causes the value stored to be
268 * displayed and otherwise has no effect -- see ACPI Specification
269 */
270 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
271 "**** Write to Debug Object: Object %p %s ****:\n\n",
272 SourceDesc, AcpiUtGetObjectTypeName (SourceDesc)));
273
274 ACPI_DEBUG_OBJECT (SourceDesc, 0, 0);
275 break;
276
277 default:
278
279 ACPI_ERROR ((AE_INFO, "Unknown Reference Class 0x%2.2X",
280 RefDesc->Reference.Class));
281 ACPI_DUMP_ENTRY (RefDesc, ACPI_LV_INFO);
282
283 Status = AE_AML_INTERNAL;
284 break;
285 }
286
287 return_ACPI_STATUS (Status);
288 }
289
290
291 /*******************************************************************************
292 *
293 * FUNCTION: AcpiExStoreObjectToIndex
294 *
295 * PARAMETERS: *SourceDesc - Value to be stored
296 * *DestDesc - Named object to receive the value
297 * WalkState - Current walk state
298 *
299 * RETURN: Status
300 *
301 * DESCRIPTION: Store the object to indexed Buffer or Package element
302 *
303 ******************************************************************************/
304
305 static ACPI_STATUS
306 AcpiExStoreObjectToIndex (
307 ACPI_OPERAND_OBJECT *SourceDesc,
308 ACPI_OPERAND_OBJECT *IndexDesc,
309 ACPI_WALK_STATE *WalkState)
310 {
311 ACPI_STATUS Status = AE_OK;
312 ACPI_OPERAND_OBJECT *ObjDesc;
313 ACPI_OPERAND_OBJECT *NewDesc;
314 UINT8 Value = 0;
315 UINT32 i;
316
317
318 ACPI_FUNCTION_TRACE (ExStoreObjectToIndex);
319
320
321 /*
322 * Destination must be a reference pointer, and
323 * must point to either a buffer or a package
324 */
325 switch (IndexDesc->Reference.TargetType)
326 {
327 case ACPI_TYPE_PACKAGE:
328 /*
329 * Storing to a package element. Copy the object and replace
330 * any existing object with the new object. No implicit
331 * conversion is performed.
332 *
333 * The object at *(IndexDesc->Reference.Where) is the
334 * element within the package that is to be modified.
335 * The parent package object is at IndexDesc->Reference.Object
336 */
337 ObjDesc = *(IndexDesc->Reference.Where);
338
339 if (SourceDesc->Common.Type == ACPI_TYPE_LOCAL_REFERENCE &&
340 SourceDesc->Reference.Class == ACPI_REFCLASS_TABLE)
341 {
342 /* This is a DDBHandle, just add a reference to it */
343
344 AcpiUtAddReference (SourceDesc);
345 NewDesc = SourceDesc;
346 }
347 else
348 {
349 /* Normal object, copy it */
350
351 Status = AcpiUtCopyIobjectToIobject (SourceDesc, &NewDesc, WalkState);
352 if (ACPI_FAILURE (Status))
353 {
354 return_ACPI_STATUS (Status);
355 }
356 }
357
358 if (ObjDesc)
359 {
360 /* Decrement reference count by the ref count of the parent package */
361
362 for (i = 0;
363 i < ((ACPI_OPERAND_OBJECT *)
364 IndexDesc->Reference.Object)->Common.ReferenceCount;
365 i++)
366 {
367 AcpiUtRemoveReference (ObjDesc);
368 }
369 }
370
371 *(IndexDesc->Reference.Where) = NewDesc;
372
373 /* Increment ref count by the ref count of the parent package-1 */
374
375 for (i = 1;
376 i < ((ACPI_OPERAND_OBJECT *)
377 IndexDesc->Reference.Object)->Common.ReferenceCount;
378 i++)
379 {
380 AcpiUtAddReference (NewDesc);
381 }
382
383 break;
384
385 case ACPI_TYPE_BUFFER_FIELD:
386 /*
387 * Store into a Buffer or String (not actually a real BufferField)
388 * at a location defined by an Index.
389 *
390 * The first 8-bit element of the source object is written to the
391 * 8-bit Buffer location defined by the Index destination object,
392 * according to the ACPI 2.0 specification.
393 */
394
395 /*
396 * Make sure the target is a Buffer or String. An error should
397 * not happen here, since the ReferenceObject was constructed
398 * by the INDEX_OP code.
399 */
400 ObjDesc = IndexDesc->Reference.Object;
401 if ((ObjDesc->Common.Type != ACPI_TYPE_BUFFER) &&
402 (ObjDesc->Common.Type != ACPI_TYPE_STRING))
403 {
404 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
405 }
406
407 /*
408 * The assignment of the individual elements will be slightly
409 * different for each source type.
410 */
411 switch (SourceDesc->Common.Type)
412 {
413 case ACPI_TYPE_INTEGER:
414
415 /* Use the least-significant byte of the integer */
416
417 Value = (UINT8) (SourceDesc->Integer.Value);
418 break;
419
420 case ACPI_TYPE_BUFFER:
421 case ACPI_TYPE_STRING:
422
423 /* Note: Takes advantage of common string/buffer fields */
424
425 Value = SourceDesc->Buffer.Pointer[0];
426 break;
427
428 default:
429
430 /* All other types are invalid */
431
432 ACPI_ERROR ((AE_INFO,
433 "Source must be Integer/Buffer/String type, not %s",
434 AcpiUtGetObjectTypeName (SourceDesc)));
435 return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
436 }
437
438 /* Store the source value into the target buffer byte */
439
440 ObjDesc->Buffer.Pointer[IndexDesc->Reference.Value] = Value;
441 break;
442
443 default:
444 ACPI_ERROR ((AE_INFO,
445 "Target is not a Package or BufferField"));
446 Status = AE_AML_OPERAND_TYPE;
447 break;
448 }
449
450 return_ACPI_STATUS (Status);
451 }
452
453
454 /*******************************************************************************
455 *
456 * FUNCTION: AcpiExStoreObjectToNode
457 *
458 * PARAMETERS: SourceDesc - Value to be stored
459 * Node - Named object to receive the value
460 * WalkState - Current walk state
461 * ImplicitConversion - Perform implicit conversion (yes/no)
462 *
463 * RETURN: Status
464 *
465 * DESCRIPTION: Store the object to the named object.
466 *
467 * The Assignment of an object to a named object is handled here
468 * The value passed in will replace the current value (if any)
469 * with the input value.
470 *
471 * When storing into an object the data is converted to the
472 * target object type then stored in the object. This means
473 * that the target object type (for an initialized target) will
474 * not be changed by a store operation. A CopyObject can change
475 * the target type, however.
476 *
477 * The ImplicitConversion flag is set to NO/FALSE only when
478 * storing to an ArgX -- as per the rules of the ACPI spec.
479 *
480 * Assumes parameters are already validated.
481 *
482 ******************************************************************************/
483
484 ACPI_STATUS
485 AcpiExStoreObjectToNode (
486 ACPI_OPERAND_OBJECT *SourceDesc,
487 ACPI_NAMESPACE_NODE *Node,
488 ACPI_WALK_STATE *WalkState,
489 UINT8 ImplicitConversion)
490 {
491 ACPI_STATUS Status = AE_OK;
492 ACPI_OPERAND_OBJECT *TargetDesc;
493 ACPI_OPERAND_OBJECT *NewDesc;
494 ACPI_OBJECT_TYPE TargetType;
495
496
497 ACPI_FUNCTION_TRACE_PTR (ExStoreObjectToNode, SourceDesc);
498
499
500 /* Get current type of the node, and object attached to Node */
501
502 TargetType = AcpiNsGetType (Node);
503 TargetDesc = AcpiNsGetAttachedObject (Node);
504
505 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Storing %p (%s) to node %p (%s)\n",
506 SourceDesc, AcpiUtGetObjectTypeName (SourceDesc),
507 Node, AcpiUtGetTypeName (TargetType)));
508
509 /*
510 * Resolve the source object to an actual value
511 * (If it is a reference object)
512 */
513 Status = AcpiExResolveObject (&SourceDesc, TargetType, WalkState);
514 if (ACPI_FAILURE (Status))
515 {
516 return_ACPI_STATUS (Status);
517 }
518
519 /* Do the actual store operation */
520
521 switch (TargetType)
522 {
523 case ACPI_TYPE_INTEGER:
524 case ACPI_TYPE_STRING:
525 case ACPI_TYPE_BUFFER:
526 /*
527 * The simple data types all support implicit source operand
528 * conversion before the store.
529 */
530
531 if ((WalkState->Opcode == AML_COPY_OP) ||
532 !ImplicitConversion)
533 {
534 /*
535 * However, CopyObject and Stores to ArgX do not perform
536 * an implicit conversion, as per the ACPI specification.
537 * A direct store is performed instead.
538 */
539 Status = AcpiExStoreDirectToNode (SourceDesc, Node,
540 WalkState);
541 break;
542 }
543
544 /* Store with implicit source operand conversion support */
545
546 Status = AcpiExStoreObjectToObject (SourceDesc, TargetDesc,
547 &NewDesc, WalkState);
548 if (ACPI_FAILURE (Status))
549 {
550 return_ACPI_STATUS (Status);
551 }
552
553 if (NewDesc != TargetDesc)
554 {
555 /*
556 * Store the new NewDesc as the new value of the Name, and set
557 * the Name's type to that of the value being stored in it.
558 * SourceDesc reference count is incremented by AttachObject.
559 *
560 * Note: This may change the type of the node if an explicit
561 * store has been performed such that the node/object type
562 * has been changed.
563 */
564 Status = AcpiNsAttachObject (Node, NewDesc,
565 NewDesc->Common.Type);
566
567 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
568 "Store %s into %s via Convert/Attach\n",
569 AcpiUtGetObjectTypeName (SourceDesc),
570 AcpiUtGetObjectTypeName (NewDesc)));
571 }
572 break;
573
574 case ACPI_TYPE_BUFFER_FIELD:
575 case ACPI_TYPE_LOCAL_REGION_FIELD:
576 case ACPI_TYPE_LOCAL_BANK_FIELD:
577 case ACPI_TYPE_LOCAL_INDEX_FIELD:
578 /*
579 * For all fields, always write the source data to the target
580 * field. Any required implicit source operand conversion is
581 * performed in the function below as necessary. Note, field
582 * objects must retain their original type permanently.
583 */
584 Status = AcpiExWriteDataToField (SourceDesc, TargetDesc,
585 &WalkState->ResultObj);
586 break;
587
588 default:
589 /*
590 * No conversions for all other types. Directly store a copy of
591 * the source object. This is the ACPI spec-defined behavior for
592 * the CopyObject operator.
593 *
594 * NOTE: For the Store operator, this is a departure from the
595 * ACPI spec, which states "If conversion is impossible, abort
596 * the running control method". Instead, this code implements
597 * "If conversion is impossible, treat the Store operation as
598 * a CopyObject".
599 */
600 Status = AcpiExStoreDirectToNode (SourceDesc, Node,
601 WalkState);
602 break;
603 }
604
605 return_ACPI_STATUS (Status);
606 }
607
608
609 /*******************************************************************************
610 *
611 * FUNCTION: AcpiExStoreDirectToNode
612 *
613 * PARAMETERS: SourceDesc - Value to be stored
614 * Node - Named object to receive the value
615 * WalkState - Current walk state
616 *
617 * RETURN: Status
618 *
619 * DESCRIPTION: "Store" an object directly to a node. This involves a copy
620 * and an attach.
621 *
622 ******************************************************************************/
623
624 static ACPI_STATUS
625 AcpiExStoreDirectToNode (
626 ACPI_OPERAND_OBJECT *SourceDesc,
627 ACPI_NAMESPACE_NODE *Node,
628 ACPI_WALK_STATE *WalkState)
629 {
630 ACPI_STATUS Status;
631 ACPI_OPERAND_OBJECT *NewDesc;
632
633
634 ACPI_FUNCTION_TRACE (ExStoreDirectToNode);
635
636
637 ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
638 "Storing [%s] (%p) directly into node [%s] (%p)"
639 " with no implicit conversion\n",
640 AcpiUtGetObjectTypeName (SourceDesc), SourceDesc,
641 AcpiUtGetTypeName (Node->Type), Node));
642
643 /* Copy the source object to a new object */
644
645 Status = AcpiUtCopyIobjectToIobject (SourceDesc, &NewDesc, WalkState);
646 if (ACPI_FAILURE (Status))
647 {
648 return_ACPI_STATUS (Status);
649 }
650
651 /* Attach the new object to the node */
652
653 Status = AcpiNsAttachObject (Node, NewDesc, NewDesc->Common.Type);
654 AcpiUtRemoveReference (NewDesc);
655 return_ACPI_STATUS (Status);
656 }