xref: /haiku/src/add-ons/kernel/bus_managers/acpi/acpica/components/executer/exresolv.c (revision 39d6c466d8217dcb3004074f29c27c60f0fa1ac1)
1 /******************************************************************************
2  *
3  * Module Name: exresolv - AML Interpreter object resolution
4  *
5  *****************************************************************************/
6 
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2015, 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 #include "acpi.h"
117 #include "accommon.h"
118 #include "amlcode.h"
119 #include "acdispat.h"
120 #include "acinterp.h"
121 #include "acnamesp.h"
122 
123 
124 #define _COMPONENT          ACPI_EXECUTER
125         ACPI_MODULE_NAME    ("exresolv")
126 
127 /* Local prototypes */
128 
129 static ACPI_STATUS
130 AcpiExResolveObjectToValue (
131     ACPI_OPERAND_OBJECT     **StackPtr,
132     ACPI_WALK_STATE         *WalkState);
133 
134 
135 /*******************************************************************************
136  *
137  * FUNCTION:    AcpiExResolveToValue
138  *
139  * PARAMETERS:  **StackPtr          - Points to entry on ObjStack, which can
140  *                                    be either an (ACPI_OPERAND_OBJECT *)
141  *                                    or an ACPI_HANDLE.
142  *              WalkState           - Current method state
143  *
144  * RETURN:      Status
145  *
146  * DESCRIPTION: Convert Reference objects to values
147  *
148  ******************************************************************************/
149 
150 ACPI_STATUS
151 AcpiExResolveToValue (
152     ACPI_OPERAND_OBJECT     **StackPtr,
153     ACPI_WALK_STATE         *WalkState)
154 {
155     ACPI_STATUS             Status;
156 
157 
158     ACPI_FUNCTION_TRACE_PTR (ExResolveToValue, StackPtr);
159 
160 
161     if (!StackPtr || !*StackPtr)
162     {
163         ACPI_ERROR ((AE_INFO, "Internal - null pointer"));
164         return_ACPI_STATUS (AE_AML_NO_OPERAND);
165     }
166 
167     /*
168      * The entity pointed to by the StackPtr can be either
169      * 1) A valid ACPI_OPERAND_OBJECT, or
170      * 2) A ACPI_NAMESPACE_NODE (NamedObj)
171      */
172     if (ACPI_GET_DESCRIPTOR_TYPE (*StackPtr) == ACPI_DESC_TYPE_OPERAND)
173     {
174         Status = AcpiExResolveObjectToValue (StackPtr, WalkState);
175         if (ACPI_FAILURE (Status))
176         {
177             return_ACPI_STATUS (Status);
178         }
179 
180         if (!*StackPtr)
181         {
182             ACPI_ERROR ((AE_INFO, "Internal - null pointer"));
183             return_ACPI_STATUS (AE_AML_NO_OPERAND);
184         }
185     }
186 
187     /*
188      * Object on the stack may have changed if AcpiExResolveObjectToValue()
189      * was called (i.e., we can't use an _else_ here.)
190      */
191     if (ACPI_GET_DESCRIPTOR_TYPE (*StackPtr) == ACPI_DESC_TYPE_NAMED)
192     {
193         Status = AcpiExResolveNodeToValue (
194                         ACPI_CAST_INDIRECT_PTR (ACPI_NAMESPACE_NODE, StackPtr),
195                         WalkState);
196         if (ACPI_FAILURE (Status))
197         {
198             return_ACPI_STATUS (Status);
199         }
200     }
201 
202     ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "Resolved object %p\n", *StackPtr));
203     return_ACPI_STATUS (AE_OK);
204 }
205 
206 
207 /*******************************************************************************
208  *
209  * FUNCTION:    AcpiExResolveObjectToValue
210  *
211  * PARAMETERS:  StackPtr        - Pointer to an internal object
212  *              WalkState       - Current method state
213  *
214  * RETURN:      Status
215  *
216  * DESCRIPTION: Retrieve the value from an internal object. The Reference type
217  *              uses the associated AML opcode to determine the value.
218  *
219  ******************************************************************************/
220 
221 static ACPI_STATUS
222 AcpiExResolveObjectToValue (
223     ACPI_OPERAND_OBJECT     **StackPtr,
224     ACPI_WALK_STATE         *WalkState)
225 {
226     ACPI_STATUS             Status = AE_OK;
227     ACPI_OPERAND_OBJECT     *StackDesc;
228     ACPI_OPERAND_OBJECT     *ObjDesc = NULL;
229     UINT8                   RefType;
230 
231 
232     ACPI_FUNCTION_TRACE (ExResolveObjectToValue);
233 
234 
235     StackDesc = *StackPtr;
236 
237     /* This is an object of type ACPI_OPERAND_OBJECT */
238 
239     switch (StackDesc->Common.Type)
240     {
241     case ACPI_TYPE_LOCAL_REFERENCE:
242 
243         RefType = StackDesc->Reference.Class;
244 
245         switch (RefType)
246         {
247         case ACPI_REFCLASS_LOCAL:
248         case ACPI_REFCLASS_ARG:
249             /*
250              * Get the local from the method's state info
251              * Note: this increments the local's object reference count
252              */
253             Status = AcpiDsMethodDataGetValue (RefType,
254                             StackDesc->Reference.Value, WalkState, &ObjDesc);
255             if (ACPI_FAILURE (Status))
256             {
257                 return_ACPI_STATUS (Status);
258             }
259 
260             ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "[Arg/Local %X] ValueObj is %p\n",
261                 StackDesc->Reference.Value, ObjDesc));
262 
263             /*
264              * Now we can delete the original Reference Object and
265              * replace it with the resolved value
266              */
267             AcpiUtRemoveReference (StackDesc);
268             *StackPtr = ObjDesc;
269             break;
270 
271         case ACPI_REFCLASS_INDEX:
272 
273             switch (StackDesc->Reference.TargetType)
274             {
275             case ACPI_TYPE_BUFFER_FIELD:
276 
277                 /* Just return - do not dereference */
278                 break;
279 
280             case ACPI_TYPE_PACKAGE:
281 
282                 /* If method call or CopyObject - do not dereference */
283 
284                 if ((WalkState->Opcode == AML_INT_METHODCALL_OP) ||
285                     (WalkState->Opcode == AML_COPY_OP))
286                 {
287                     break;
288                 }
289 
290                 /* Otherwise, dereference the PackageIndex to a package element */
291 
292                 ObjDesc = *StackDesc->Reference.Where;
293                 if (ObjDesc)
294                 {
295                     /*
296                      * Valid object descriptor, copy pointer to return value
297                      * (i.e., dereference the package index)
298                      * Delete the ref object, increment the returned object
299                      */
300                     AcpiUtRemoveReference (StackDesc);
301                     AcpiUtAddReference (ObjDesc);
302                     *StackPtr = ObjDesc;
303                 }
304                 else
305                 {
306                     /*
307                      * A NULL object descriptor means an uninitialized element of
308                      * the package, can't dereference it
309                      */
310                     ACPI_ERROR ((AE_INFO,
311                         "Attempt to dereference an Index to NULL package element Idx=%p",
312                         StackDesc));
313                     Status = AE_AML_UNINITIALIZED_ELEMENT;
314                 }
315                 break;
316 
317             default:
318 
319                 /* Invalid reference object */
320 
321                 ACPI_ERROR ((AE_INFO,
322                     "Unknown TargetType 0x%X in Index/Reference object %p",
323                     StackDesc->Reference.TargetType, StackDesc));
324                 Status = AE_AML_INTERNAL;
325                 break;
326             }
327             break;
328 
329         case ACPI_REFCLASS_REFOF:
330         case ACPI_REFCLASS_DEBUG:
331         case ACPI_REFCLASS_TABLE:
332 
333             /* Just leave the object as-is, do not dereference */
334 
335             break;
336 
337         case ACPI_REFCLASS_NAME:   /* Reference to a named object */
338 
339             /* Dereference the name */
340 
341             if ((StackDesc->Reference.Node->Type == ACPI_TYPE_DEVICE) ||
342                 (StackDesc->Reference.Node->Type == ACPI_TYPE_THERMAL))
343             {
344                 /* These node types do not have 'real' subobjects */
345 
346                 *StackPtr = (void *) StackDesc->Reference.Node;
347             }
348             else
349             {
350                 /* Get the object pointed to by the namespace node */
351 
352                 *StackPtr = (StackDesc->Reference.Node)->Object;
353                 AcpiUtAddReference (*StackPtr);
354             }
355 
356             AcpiUtRemoveReference (StackDesc);
357             break;
358 
359         default:
360 
361             ACPI_ERROR ((AE_INFO,
362                 "Unknown Reference type 0x%X in %p", RefType, StackDesc));
363             Status = AE_AML_INTERNAL;
364             break;
365         }
366         break;
367 
368     case ACPI_TYPE_BUFFER:
369 
370         Status = AcpiDsGetBufferArguments (StackDesc);
371         break;
372 
373     case ACPI_TYPE_PACKAGE:
374 
375         Status = AcpiDsGetPackageArguments (StackDesc);
376         break;
377 
378     case ACPI_TYPE_BUFFER_FIELD:
379     case ACPI_TYPE_LOCAL_REGION_FIELD:
380     case ACPI_TYPE_LOCAL_BANK_FIELD:
381     case ACPI_TYPE_LOCAL_INDEX_FIELD:
382 
383         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC, "FieldRead SourceDesc=%p Type=%X\n",
384             StackDesc, StackDesc->Common.Type));
385 
386         Status = AcpiExReadDataFromField (WalkState, StackDesc, &ObjDesc);
387 
388         /* Remove a reference to the original operand, then override */
389 
390         AcpiUtRemoveReference (*StackPtr);
391         *StackPtr = (void *) ObjDesc;
392         break;
393 
394     default:
395 
396         break;
397     }
398 
399     return_ACPI_STATUS (Status);
400 }
401 
402 
403 /*******************************************************************************
404  *
405  * FUNCTION:    AcpiExResolveMultiple
406  *
407  * PARAMETERS:  WalkState           - Current state (contains AML opcode)
408  *              Operand             - Starting point for resolution
409  *              ReturnType          - Where the object type is returned
410  *              ReturnDesc          - Where the resolved object is returned
411  *
412  * RETURN:      Status
413  *
414  * DESCRIPTION: Return the base object and type. Traverse a reference list if
415  *              necessary to get to the base object.
416  *
417  ******************************************************************************/
418 
419 ACPI_STATUS
420 AcpiExResolveMultiple (
421     ACPI_WALK_STATE         *WalkState,
422     ACPI_OPERAND_OBJECT     *Operand,
423     ACPI_OBJECT_TYPE        *ReturnType,
424     ACPI_OPERAND_OBJECT     **ReturnDesc)
425 {
426     ACPI_OPERAND_OBJECT     *ObjDesc = ACPI_CAST_PTR (void, Operand);
427     ACPI_NAMESPACE_NODE     *Node = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Operand);
428     ACPI_OBJECT_TYPE        Type;
429     ACPI_STATUS             Status;
430 
431 
432     ACPI_FUNCTION_TRACE (AcpiExResolveMultiple);
433 
434 
435     /* Operand can be either a namespace node or an operand descriptor */
436 
437     switch (ACPI_GET_DESCRIPTOR_TYPE (ObjDesc))
438     {
439     case ACPI_DESC_TYPE_OPERAND:
440 
441         Type = ObjDesc->Common.Type;
442         break;
443 
444     case ACPI_DESC_TYPE_NAMED:
445 
446         Type = ((ACPI_NAMESPACE_NODE *) ObjDesc)->Type;
447         ObjDesc = AcpiNsGetAttachedObject (Node);
448 
449         /* If we had an Alias node, use the attached object for type info */
450 
451         if (Type == ACPI_TYPE_LOCAL_ALIAS)
452         {
453             Type = ((ACPI_NAMESPACE_NODE *) ObjDesc)->Type;
454             ObjDesc = AcpiNsGetAttachedObject ((ACPI_NAMESPACE_NODE *) ObjDesc);
455         }
456 
457         if (!ObjDesc)
458         {
459             ACPI_ERROR ((AE_INFO,
460                 "[%4.4s] Node is unresolved or uninitialized",
461                 AcpiUtGetNodeName (Node)));
462             return_ACPI_STATUS (AE_AML_UNINITIALIZED_NODE);
463         }
464         break;
465 
466     default:
467         return_ACPI_STATUS (AE_AML_OPERAND_TYPE);
468     }
469 
470     /* If type is anything other than a reference, we are done */
471 
472     if (Type != ACPI_TYPE_LOCAL_REFERENCE)
473     {
474         goto Exit;
475     }
476 
477     /*
478      * For reference objects created via the RefOf, Index, or Load/LoadTable
479      * operators, we need to get to the base object (as per the ACPI
480      * specification of the ObjectType and SizeOf operators). This means
481      * traversing the list of possibly many nested references.
482      */
483     while (ObjDesc->Common.Type == ACPI_TYPE_LOCAL_REFERENCE)
484     {
485         switch (ObjDesc->Reference.Class)
486         {
487         case ACPI_REFCLASS_REFOF:
488         case ACPI_REFCLASS_NAME:
489 
490             /* Dereference the reference pointer */
491 
492             if (ObjDesc->Reference.Class == ACPI_REFCLASS_REFOF)
493             {
494                 Node = ObjDesc->Reference.Object;
495             }
496             else /* AML_INT_NAMEPATH_OP */
497             {
498                 Node = ObjDesc->Reference.Node;
499             }
500 
501             /* All "References" point to a NS node */
502 
503             if (ACPI_GET_DESCRIPTOR_TYPE (Node) != ACPI_DESC_TYPE_NAMED)
504             {
505                 ACPI_ERROR ((AE_INFO,
506                     "Not a namespace node %p [%s]",
507                     Node, AcpiUtGetDescriptorName (Node)));
508                 return_ACPI_STATUS (AE_AML_INTERNAL);
509             }
510 
511             /* Get the attached object */
512 
513             ObjDesc = AcpiNsGetAttachedObject (Node);
514             if (!ObjDesc)
515             {
516                 /* No object, use the NS node type */
517 
518                 Type = AcpiNsGetType (Node);
519                 goto Exit;
520             }
521 
522             /* Check for circular references */
523 
524             if (ObjDesc == Operand)
525             {
526                 return_ACPI_STATUS (AE_AML_CIRCULAR_REFERENCE);
527             }
528             break;
529 
530         case ACPI_REFCLASS_INDEX:
531 
532             /* Get the type of this reference (index into another object) */
533 
534             Type = ObjDesc->Reference.TargetType;
535             if (Type != ACPI_TYPE_PACKAGE)
536             {
537                 goto Exit;
538             }
539 
540             /*
541              * The main object is a package, we want to get the type
542              * of the individual package element that is referenced by
543              * the index.
544              *
545              * This could of course in turn be another reference object.
546              */
547             ObjDesc = *(ObjDesc->Reference.Where);
548             if (!ObjDesc)
549             {
550                 /* NULL package elements are allowed */
551 
552                 Type = 0; /* Uninitialized */
553                 goto Exit;
554             }
555             break;
556 
557         case ACPI_REFCLASS_TABLE:
558 
559             Type = ACPI_TYPE_DDB_HANDLE;
560             goto Exit;
561 
562         case ACPI_REFCLASS_LOCAL:
563         case ACPI_REFCLASS_ARG:
564 
565             if (ReturnDesc)
566             {
567                 Status = AcpiDsMethodDataGetValue (ObjDesc->Reference.Class,
568                             ObjDesc->Reference.Value, WalkState, &ObjDesc);
569                 if (ACPI_FAILURE (Status))
570                 {
571                     return_ACPI_STATUS (Status);
572                 }
573                 AcpiUtRemoveReference (ObjDesc);
574             }
575             else
576             {
577                 Status = AcpiDsMethodDataGetNode (ObjDesc->Reference.Class,
578                             ObjDesc->Reference.Value, WalkState, &Node);
579                 if (ACPI_FAILURE (Status))
580                 {
581                     return_ACPI_STATUS (Status);
582                 }
583 
584                 ObjDesc = AcpiNsGetAttachedObject (Node);
585                 if (!ObjDesc)
586                 {
587                     Type = ACPI_TYPE_ANY;
588                     goto Exit;
589                 }
590             }
591             break;
592 
593         case ACPI_REFCLASS_DEBUG:
594 
595             /* The Debug Object is of type "DebugObject" */
596 
597             Type = ACPI_TYPE_DEBUG_OBJECT;
598             goto Exit;
599 
600         default:
601 
602             ACPI_ERROR ((AE_INFO,
603                 "Unknown Reference Class 0x%2.2X", ObjDesc->Reference.Class));
604             return_ACPI_STATUS (AE_AML_INTERNAL);
605         }
606     }
607 
608     /*
609      * Now we are guaranteed to have an object that has not been created
610      * via the RefOf or Index operators.
611      */
612     Type = ObjDesc->Common.Type;
613 
614 
615 Exit:
616     /* Convert internal types to external types */
617 
618     switch (Type)
619     {
620     case ACPI_TYPE_LOCAL_REGION_FIELD:
621     case ACPI_TYPE_LOCAL_BANK_FIELD:
622     case ACPI_TYPE_LOCAL_INDEX_FIELD:
623 
624         Type = ACPI_TYPE_FIELD_UNIT;
625         break;
626 
627     case ACPI_TYPE_LOCAL_SCOPE:
628 
629         /* Per ACPI Specification, Scope is untyped */
630 
631         Type = ACPI_TYPE_ANY;
632         break;
633 
634     default:
635 
636         /* No change to Type required */
637 
638         break;
639     }
640 
641     *ReturnType = Type;
642     if (ReturnDesc)
643     {
644         *ReturnDesc = ObjDesc;
645     }
646     return_ACPI_STATUS (AE_OK);
647 }
648