Bring in acpica 20110211, still not ok anyway
[dragonfly.git] / sys / contrib / dev / acpica-unix / namespace / nseval.c
1 /*******************************************************************************
2  *
3  * Module Name: nseval - Object evaluation, includes control method execution
4  *
5  ******************************************************************************/
6
7 /******************************************************************************
8  *
9  * 1. Copyright Notice
10  *
11  * Some or all of this work - Copyright (c) 1999 - 2011, 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  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government.  In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************/
116
117 #define __NSEVAL_C__
118
119 #include "acpi.h"
120 #include "accommon.h"
121 #include "acparser.h"
122 #include "acinterp.h"
123 #include "acnamesp.h"
124
125
126 #define _COMPONENT          ACPI_NAMESPACE
127         ACPI_MODULE_NAME    ("nseval")
128
129 /* Local prototypes */
130
131 static void
132 AcpiNsExecModuleCode (
133     ACPI_OPERAND_OBJECT     *MethodObj,
134     ACPI_EVALUATE_INFO      *Info);
135
136
137 /*******************************************************************************
138  *
139  * FUNCTION:    AcpiNsEvaluate
140  *
141  * PARAMETERS:  Info            - Evaluation info block, contains:
142  *                  PrefixNode      - Prefix or Method/Object Node to execute
143  *                  Pathname        - Name of method to execute, If NULL, the
144  *                                    Node is the object to execute
145  *                  Parameters      - List of parameters to pass to the method,
146  *                                    terminated by NULL. Params itself may be
147  *                                    NULL if no parameters are being passed.
148  *                  ReturnObject    - Where to put method's return value (if
149  *                                    any). If NULL, no value is returned.
150  *                  ParameterType   - Type of Parameter list
151  *                  ReturnObject    - Where to put method's return value (if
152  *                                    any). If NULL, no value is returned.
153  *                  Flags           - ACPI_IGNORE_RETURN_VALUE to delete return
154  *
155  * RETURN:      Status
156  *
157  * DESCRIPTION: Execute a control method or return the current value of an
158  *              ACPI namespace object.
159  *
160  * MUTEX:       Locks interpreter
161  *
162  ******************************************************************************/
163
164 ACPI_STATUS
165 AcpiNsEvaluate (
166     ACPI_EVALUATE_INFO      *Info)
167 {
168     ACPI_STATUS             Status;
169     ACPI_NAMESPACE_NODE     *Node;
170
171
172     ACPI_FUNCTION_TRACE (NsEvaluate);
173
174
175     if (!Info)
176     {
177         return_ACPI_STATUS (AE_BAD_PARAMETER);
178     }
179
180     /* Initialize the return value to an invalid object */
181
182     Info->ReturnObject = NULL;
183     Info->ParamCount = 0;
184
185     /*
186      * Get the actual namespace node for the target object. Handles these cases:
187      *
188      * 1) Null node, Pathname (absolute path)
189      * 2) Node, Pathname (path relative to Node)
190      * 3) Node, Null Pathname
191      */
192     Status = AcpiNsGetNode (Info->PrefixNode, Info->Pathname,
193                 ACPI_NS_NO_UPSEARCH, &Info->ResolvedNode);
194     if (ACPI_FAILURE (Status))
195     {
196         return_ACPI_STATUS (Status);
197     }
198
199     /*
200      * For a method alias, we must grab the actual method node so that proper
201      * scoping context will be established before execution.
202      */
203     if (AcpiNsGetType (Info->ResolvedNode) == ACPI_TYPE_LOCAL_METHOD_ALIAS)
204     {
205         Info->ResolvedNode =
206             ACPI_CAST_PTR (ACPI_NAMESPACE_NODE, Info->ResolvedNode->Object);
207     }
208
209     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%s [%p] Value %p\n", Info->Pathname,
210         Info->ResolvedNode, AcpiNsGetAttachedObject (Info->ResolvedNode)));
211
212     Node = Info->ResolvedNode;
213
214     /*
215      * Two major cases here:
216      *
217      * 1) The object is a control method -- execute it
218      * 2) The object is not a method -- just return it's current value
219      */
220     if (AcpiNsGetType (Info->ResolvedNode) == ACPI_TYPE_METHOD)
221     {
222         /*
223          * 1) Object is a control method - execute it
224          */
225
226         /* Verify that there is a method object associated with this node */
227
228         Info->ObjDesc = AcpiNsGetAttachedObject (Info->ResolvedNode);
229         if (!Info->ObjDesc)
230         {
231             ACPI_ERROR ((AE_INFO, "Control method has no attached sub-object"));
232             return_ACPI_STATUS (AE_NULL_OBJECT);
233         }
234
235         /* Count the number of arguments being passed to the method */
236
237         if (Info->Parameters)
238         {
239             while (Info->Parameters[Info->ParamCount])
240             {
241                 if (Info->ParamCount > ACPI_METHOD_MAX_ARG)
242                 {
243                     return_ACPI_STATUS (AE_LIMIT);
244                 }
245                 Info->ParamCount++;
246             }
247         }
248
249         ACPI_DUMP_PATHNAME (Info->ResolvedNode, "ACPI: Execute Method",
250             ACPI_LV_INFO, _COMPONENT);
251
252         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
253             "Method at AML address %p Length %X\n",
254             Info->ObjDesc->Method.AmlStart + 1,
255             Info->ObjDesc->Method.AmlLength - 1));
256
257         /*
258          * Any namespace deletion must acquire both the namespace and
259          * interpreter locks to ensure that no thread is using the portion of
260          * the namespace that is being deleted.
261          *
262          * Execute the method via the interpreter. The interpreter is locked
263          * here before calling into the AML parser
264          */
265         AcpiExEnterInterpreter ();
266         Status = AcpiPsExecuteMethod (Info);
267         AcpiExExitInterpreter ();
268     }
269     else
270     {
271         /*
272          * 2) Object is not a method, return its current value
273          *
274          * Disallow certain object types. For these, "evaluation" is undefined.
275          */
276         switch (Info->ResolvedNode->Type)
277         {
278         case ACPI_TYPE_DEVICE:
279         case ACPI_TYPE_EVENT:
280         case ACPI_TYPE_MUTEX:
281         case ACPI_TYPE_REGION:
282         case ACPI_TYPE_THERMAL:
283         case ACPI_TYPE_LOCAL_SCOPE:
284
285             ACPI_ERROR ((AE_INFO,
286                 "[%4.4s] Evaluation of object type [%s] is not supported",
287                 Info->ResolvedNode->Name.Ascii,
288                 AcpiUtGetTypeName (Info->ResolvedNode->Type)));
289
290             return_ACPI_STATUS (AE_TYPE);
291
292         default:
293             break;
294         }
295
296         /*
297          * Objects require additional resolution steps (e.g., the Node may be
298          * a field that must be read, etc.) -- we can't just grab the object
299          * out of the node.
300          *
301          * Use ResolveNodeToValue() to get the associated value.
302          *
303          * NOTE: we can get away with passing in NULL for a walk state because
304          * ResolvedNode is guaranteed to not be a reference to either a method
305          * local or a method argument (because this interface is never called
306          * from a running method.)
307          *
308          * Even though we do not directly invoke the interpreter for object
309          * resolution, we must lock it because we could access an opregion.
310          * The opregion access code assumes that the interpreter is locked.
311          */
312         AcpiExEnterInterpreter ();
313
314         /* Function has a strange interface */
315
316         Status = AcpiExResolveNodeToValue (&Info->ResolvedNode, NULL);
317         AcpiExExitInterpreter ();
318
319         /*
320          * If AcpiExResolveNodeToValue() succeeded, the return value was placed
321          * in ResolvedNode.
322          */
323         if (ACPI_SUCCESS (Status))
324         {
325             Status = AE_CTRL_RETURN_VALUE;
326             Info->ReturnObject =
327                 ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Info->ResolvedNode);
328
329             ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Returning object %p [%s]\n",
330                 Info->ReturnObject,
331                 AcpiUtGetObjectTypeName (Info->ReturnObject)));
332         }
333     }
334
335     /*
336      * Check input argument count against the ASL-defined count for a method.
337      * Also check predefined names: argument count and return value against
338      * the ACPI specification. Some incorrect return value types are repaired.
339      */
340     (void) AcpiNsCheckPredefinedNames (Node, Info->ParamCount,
341                 Status, &Info->ReturnObject);
342
343     /* Check if there is a return value that must be dealt with */
344
345     if (Status == AE_CTRL_RETURN_VALUE)
346     {
347         /* If caller does not want the return value, delete it */
348
349         if (Info->Flags & ACPI_IGNORE_RETURN_VALUE)
350         {
351             AcpiUtRemoveReference (Info->ReturnObject);
352             Info->ReturnObject = NULL;
353         }
354
355         /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
356
357         Status = AE_OK;
358     }
359
360     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
361         "*** Completed evaluation of object %s ***\n", Info->Pathname));
362
363     /*
364      * Namespace was unlocked by the handling AcpiNs* function, so we
365      * just return
366      */
367     return_ACPI_STATUS (Status);
368 }
369
370
371 /*******************************************************************************
372  *
373  * FUNCTION:    AcpiNsExecModuleCodeList
374  *
375  * PARAMETERS:  None
376  *
377  * RETURN:      None. Exceptions during method execution are ignored, since
378  *              we cannot abort a table load.
379  *
380  * DESCRIPTION: Execute all elements of the global module-level code list.
381  *              Each element is executed as a single control method.
382  *
383  ******************************************************************************/
384
385 void
386 AcpiNsExecModuleCodeList (
387     void)
388 {
389     ACPI_OPERAND_OBJECT     *Prev;
390     ACPI_OPERAND_OBJECT     *Next;
391     ACPI_EVALUATE_INFO      *Info;
392     UINT32                  MethodCount = 0;
393
394
395     ACPI_FUNCTION_TRACE (NsExecModuleCodeList);
396
397
398     /* Exit now if the list is empty */
399
400     Next = AcpiGbl_ModuleCodeList;
401     if (!Next)
402     {
403         return_VOID;
404     }
405
406     /* Allocate the evaluation information block */
407
408     Info = ACPI_ALLOCATE (sizeof (ACPI_EVALUATE_INFO));
409     if (!Info)
410     {
411         return_VOID;
412     }
413
414     /* Walk the list, executing each "method" */
415
416     while (Next)
417     {
418         Prev = Next;
419         Next = Next->Method.Mutex;
420
421         /* Clear the link field and execute the method */
422
423         Prev->Method.Mutex = NULL;
424         AcpiNsExecModuleCode (Prev, Info);
425         MethodCount++;
426
427         /* Delete the (temporary) method object */
428
429         AcpiUtRemoveReference (Prev);
430     }
431
432     ACPI_INFO ((AE_INFO,
433         "Executed %u blocks of module-level executable AML code",
434         MethodCount));
435
436     ACPI_FREE (Info);
437     AcpiGbl_ModuleCodeList = NULL;
438     return_VOID;
439 }
440
441
442 /*******************************************************************************
443  *
444  * FUNCTION:    AcpiNsExecModuleCode
445  *
446  * PARAMETERS:  MethodObj           - Object container for the module-level code
447  *              Info                - Info block for method evaluation
448  *
449  * RETURN:      None. Exceptions during method execution are ignored, since
450  *              we cannot abort a table load.
451  *
452  * DESCRIPTION: Execute a control method containing a block of module-level
453  *              executable AML code. The control method is temporarily
454  *              installed to the root node, then evaluated.
455  *
456  ******************************************************************************/
457
458 static void
459 AcpiNsExecModuleCode (
460     ACPI_OPERAND_OBJECT     *MethodObj,
461     ACPI_EVALUATE_INFO      *Info)
462 {
463     ACPI_OPERAND_OBJECT     *ParentObj;
464     ACPI_NAMESPACE_NODE     *ParentNode;
465     ACPI_OBJECT_TYPE        Type;
466     ACPI_STATUS             Status;
467
468
469     ACPI_FUNCTION_TRACE (NsExecModuleCode);
470
471
472     /*
473      * Get the parent node. We cheat by using the NextObject field
474      * of the method object descriptor.
475      */
476     ParentNode = ACPI_CAST_PTR (ACPI_NAMESPACE_NODE,
477                     MethodObj->Method.NextObject);
478     Type = AcpiNsGetType (ParentNode);
479
480     /*
481      * Get the region handler and save it in the method object. We may need
482      * this if an operation region declaration causes a _REG method to be run.
483      *
484      * We can't do this in AcpiPsLinkModuleCode because
485      * AcpiGbl_RootNode->Object is NULL at PASS1.
486      */
487     if ((Type == ACPI_TYPE_DEVICE) && ParentNode->Object)
488     {
489         MethodObj->Method.Dispatch.Handler =
490             ParentNode->Object->Device.Handler;
491     }
492
493     /* Must clear NextObject (AcpiNsAttachObject needs the field) */
494
495     MethodObj->Method.NextObject = NULL;
496
497     /* Initialize the evaluation information block */
498
499     ACPI_MEMSET (Info, 0, sizeof (ACPI_EVALUATE_INFO));
500     Info->PrefixNode = ParentNode;
501
502     /*
503      * Get the currently attached parent object. Add a reference, because the
504      * ref count will be decreased when the method object is installed to
505      * the parent node.
506      */
507     ParentObj = AcpiNsGetAttachedObject (ParentNode);
508     if (ParentObj)
509     {
510         AcpiUtAddReference (ParentObj);
511     }
512
513     /* Install the method (module-level code) in the parent node */
514
515     Status = AcpiNsAttachObject (ParentNode, MethodObj,
516                 ACPI_TYPE_METHOD);
517     if (ACPI_FAILURE (Status))
518     {
519         goto Exit;
520     }
521
522     /* Execute the parent node as a control method */
523
524     Status = AcpiNsEvaluate (Info);
525
526     ACPI_DEBUG_PRINT ((ACPI_DB_INIT, "Executed module-level code at %p\n",
527         MethodObj->Method.AmlStart));
528
529     /* Delete a possible implicit return value (in slack mode) */
530
531     if (Info->ReturnObject)
532     {
533         AcpiUtRemoveReference (Info->ReturnObject);
534     }
535
536     /* Detach the temporary method object */
537
538     AcpiNsDetachObject (ParentNode);
539
540     /* Restore the original parent object */
541
542     if (ParentObj)
543     {
544         Status = AcpiNsAttachObject (ParentNode, ParentObj, Type);
545     }
546     else
547     {
548         ParentNode->Type = (UINT8) Type;
549     }
550
551 Exit:
552     if (ParentObj)
553     {
554         AcpiUtRemoveReference (ParentObj);
555     }
556     return_VOID;
557 }
558