xref: /netbsd-src/sys/external/bsd/acpica/dist/namespace/nseval.c (revision 2c7d7e3ca2e4f0b675c6c58e614f6aede66c678e)
128c506b8Sjruoho /*******************************************************************************
228c506b8Sjruoho  *
328c506b8Sjruoho  * Module Name: nseval - Object evaluation, includes control method execution
428c506b8Sjruoho  *
528c506b8Sjruoho  ******************************************************************************/
628c506b8Sjruoho 
7159c4e26Sjruoho /*
8*2c7d7e3cSchristos  * Copyright (C) 2000 - 2023, Intel Corp.
928c506b8Sjruoho  * All rights reserved.
1028c506b8Sjruoho  *
11159c4e26Sjruoho  * Redistribution and use in source and binary forms, with or without
12159c4e26Sjruoho  * modification, are permitted provided that the following conditions
13159c4e26Sjruoho  * are met:
14159c4e26Sjruoho  * 1. Redistributions of source code must retain the above copyright
15159c4e26Sjruoho  *    notice, this list of conditions, and the following disclaimer,
16159c4e26Sjruoho  *    without modification.
17159c4e26Sjruoho  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18159c4e26Sjruoho  *    substantially similar to the "NO WARRANTY" disclaimer below
19159c4e26Sjruoho  *    ("Disclaimer") and any redistribution must be conditioned upon
20159c4e26Sjruoho  *    including a substantially similar Disclaimer requirement for further
21159c4e26Sjruoho  *    binary redistribution.
22159c4e26Sjruoho  * 3. Neither the names of the above-listed copyright holders nor the names
23159c4e26Sjruoho  *    of any contributors may be used to endorse or promote products derived
24159c4e26Sjruoho  *    from this software without specific prior written permission.
2528c506b8Sjruoho  *
26159c4e26Sjruoho  * Alternatively, this software may be distributed under the terms of the
27159c4e26Sjruoho  * GNU General Public License ("GPL") version 2 as published by the Free
28159c4e26Sjruoho  * Software Foundation.
2928c506b8Sjruoho  *
30159c4e26Sjruoho  * NO WARRANTY
31159c4e26Sjruoho  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32159c4e26Sjruoho  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
3398244dcfSchristos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34159c4e26Sjruoho  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35159c4e26Sjruoho  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36159c4e26Sjruoho  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37159c4e26Sjruoho  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38159c4e26Sjruoho  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39159c4e26Sjruoho  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40159c4e26Sjruoho  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41159c4e26Sjruoho  * POSSIBILITY OF SUCH DAMAGES.
42159c4e26Sjruoho  */
4328c506b8Sjruoho 
4428c506b8Sjruoho #include "acpi.h"
4528c506b8Sjruoho #include "accommon.h"
4628c506b8Sjruoho #include "acparser.h"
4728c506b8Sjruoho #include "acinterp.h"
4828c506b8Sjruoho #include "acnamesp.h"
4928c506b8Sjruoho 
5028c506b8Sjruoho 
5128c506b8Sjruoho #define _COMPONENT          ACPI_NAMESPACE
5228c506b8Sjruoho         ACPI_MODULE_NAME    ("nseval")
5328c506b8Sjruoho 
5428c506b8Sjruoho 
5528c506b8Sjruoho /*******************************************************************************
5628c506b8Sjruoho  *
5728c506b8Sjruoho  * FUNCTION:    AcpiNsEvaluate
5828c506b8Sjruoho  *
599b9ee194Schristos  * PARAMETERS:  Info            - Evaluation info block, contains these fields
609b9ee194Schristos  *                                and more:
6128c506b8Sjruoho  *                  PrefixNode      - Prefix or Method/Object Node to execute
62a2c051a9Schristos  *                  RelativePath    - Name of method to execute, If NULL, the
6328c506b8Sjruoho  *                                    Node is the object to execute
6428c506b8Sjruoho  *                  Parameters      - List of parameters to pass to the method,
6528c506b8Sjruoho  *                                    terminated by NULL. Params itself may be
6628c506b8Sjruoho  *                                    NULL if no parameters are being passed.
6728c506b8Sjruoho  *                  ParameterType   - Type of Parameter list
6828c506b8Sjruoho  *                  ReturnObject    - Where to put method's return value (if
6928c506b8Sjruoho  *                                    any). If NULL, no value is returned.
7028c506b8Sjruoho  *                  Flags           - ACPI_IGNORE_RETURN_VALUE to delete return
7128c506b8Sjruoho  *
7228c506b8Sjruoho  * RETURN:      Status
7328c506b8Sjruoho  *
7428c506b8Sjruoho  * DESCRIPTION: Execute a control method or return the current value of an
7528c506b8Sjruoho  *              ACPI namespace object.
7628c506b8Sjruoho  *
7728c506b8Sjruoho  * MUTEX:       Locks interpreter
7828c506b8Sjruoho  *
7928c506b8Sjruoho  ******************************************************************************/
8028c506b8Sjruoho 
8128c506b8Sjruoho ACPI_STATUS
AcpiNsEvaluate(ACPI_EVALUATE_INFO * Info)8228c506b8Sjruoho AcpiNsEvaluate (
8328c506b8Sjruoho     ACPI_EVALUATE_INFO      *Info)
8428c506b8Sjruoho {
8528c506b8Sjruoho     ACPI_STATUS             Status;
8628c506b8Sjruoho 
8728c506b8Sjruoho 
8828c506b8Sjruoho     ACPI_FUNCTION_TRACE (NsEvaluate);
8928c506b8Sjruoho 
9028c506b8Sjruoho 
9128c506b8Sjruoho     if (!Info)
9228c506b8Sjruoho     {
9328c506b8Sjruoho         return_ACPI_STATUS (AE_BAD_PARAMETER);
9428c506b8Sjruoho     }
9528c506b8Sjruoho 
96a2c051a9Schristos     if (!Info->Node)
97a2c051a9Schristos     {
9828c506b8Sjruoho         /*
99a2c051a9Schristos          * Get the actual namespace node for the target object if we
100a2c051a9Schristos          * need to. Handles these cases:
10128c506b8Sjruoho          *
102a2c051a9Schristos          * 1) Null node, valid pathname from root (absolute path)
103a2c051a9Schristos          * 2) Node and valid pathname (path relative to Node)
104a2c051a9Schristos          * 3) Node, Null pathname
10528c506b8Sjruoho          */
106a2c051a9Schristos         Status = AcpiNsGetNode (Info->PrefixNode, Info->RelativePathname,
107a2c051a9Schristos             ACPI_NS_NO_UPSEARCH, &Info->Node);
10828c506b8Sjruoho         if (ACPI_FAILURE (Status))
10928c506b8Sjruoho         {
11028c506b8Sjruoho             return_ACPI_STATUS (Status);
11128c506b8Sjruoho         }
11228c506b8Sjruoho     }
11328c506b8Sjruoho 
11428c506b8Sjruoho     /*
115a2c051a9Schristos      * For a method alias, we must grab the actual method node so that
116a2c051a9Schristos      * proper scoping context will be established before execution.
11728c506b8Sjruoho      */
118a2c051a9Schristos     if (AcpiNsGetType (Info->Node) == ACPI_TYPE_LOCAL_METHOD_ALIAS)
11928c506b8Sjruoho     {
120a2c051a9Schristos         Info->Node = ACPI_CAST_PTR (
121a2c051a9Schristos             ACPI_NAMESPACE_NODE, Info->Node->Object);
12228c506b8Sjruoho     }
12328c506b8Sjruoho 
124a2c051a9Schristos     /* Complete the info block initialization */
12528c506b8Sjruoho 
126a2c051a9Schristos     Info->ReturnObject = NULL;
127a2c051a9Schristos     Info->NodeFlags = Info->Node->Flags;
128a2c051a9Schristos     Info->ObjDesc = AcpiNsGetAttachedObject (Info->Node);
129a2c051a9Schristos 
130a2c051a9Schristos     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "%s [%p] Value %p\n",
131a2c051a9Schristos         Info->RelativePathname, Info->Node,
132a2c051a9Schristos         AcpiNsGetAttachedObject (Info->Node)));
133a2c051a9Schristos 
134a2c051a9Schristos     /* Get info if we have a predefined name (_HID, etc.) */
135a2c051a9Schristos 
136a2c051a9Schristos     Info->Predefined = AcpiUtMatchPredefinedMethod (Info->Node->Name.Ascii);
137a2c051a9Schristos 
138a2c051a9Schristos     /* Get the full pathname to the object, for use in warning messages */
139a2c051a9Schristos 
14081bd9c9cSchristos     Info->FullPathname = AcpiNsGetNormalizedPathname (Info->Node, TRUE);
141a2c051a9Schristos     if (!Info->FullPathname)
142a2c051a9Schristos     {
143a2c051a9Schristos         return_ACPI_STATUS (AE_NO_MEMORY);
144a2c051a9Schristos     }
145a2c051a9Schristos 
1467efa3256Schristos     /* Optional object evaluation log */
1477efa3256Schristos 
1487efa3256Schristos     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
1497efa3256Schristos         "%-26s:  %s (%s)\n", "   Enter evaluation",
1507efa3256Schristos         &Info->FullPathname[1], AcpiUtGetTypeName (Info->Node->Type)));
1517efa3256Schristos 
152a2c051a9Schristos     /* Count the number of arguments being passed in */
153a2c051a9Schristos 
154a2c051a9Schristos     Info->ParamCount = 0;
15528c506b8Sjruoho     if (Info->Parameters)
15628c506b8Sjruoho     {
15728c506b8Sjruoho         while (Info->Parameters[Info->ParamCount])
15828c506b8Sjruoho         {
15928c506b8Sjruoho             Info->ParamCount++;
16028c506b8Sjruoho         }
161a2c051a9Schristos 
162a2c051a9Schristos         /* Warn on impossible argument count */
163a2c051a9Schristos 
164a2c051a9Schristos         if (Info->ParamCount > ACPI_METHOD_NUM_ARGS)
165a2c051a9Schristos         {
166a2c051a9Schristos             ACPI_WARN_PREDEFINED ((AE_INFO, Info->FullPathname, ACPI_WARN_ALWAYS,
167a2c051a9Schristos                 "Excess arguments (%u) - using only %u",
168a2c051a9Schristos                 Info->ParamCount, ACPI_METHOD_NUM_ARGS));
169a2c051a9Schristos 
170a2c051a9Schristos             Info->ParamCount = ACPI_METHOD_NUM_ARGS;
171a2c051a9Schristos         }
17228c506b8Sjruoho     }
17328c506b8Sjruoho 
174a2c051a9Schristos     /*
175a2c051a9Schristos      * For predefined names: Check that the declared argument count
176a2c051a9Schristos      * matches the ACPI spec -- otherwise this is a BIOS error.
177a2c051a9Schristos      */
178a2c051a9Schristos     AcpiNsCheckAcpiCompliance (Info->FullPathname, Info->Node,
179a2c051a9Schristos         Info->Predefined);
180a2c051a9Schristos 
181a2c051a9Schristos     /*
182a2c051a9Schristos      * For all names: Check that the incoming argument count for
183a2c051a9Schristos      * this method/object matches the actual ASL/AML definition.
184a2c051a9Schristos      */
185a2c051a9Schristos     AcpiNsCheckArgumentCount (Info->FullPathname, Info->Node,
186a2c051a9Schristos         Info->ParamCount, Info->Predefined);
187a2c051a9Schristos 
188a2c051a9Schristos     /* For predefined names: Typecheck all incoming arguments */
189a2c051a9Schristos 
190a2c051a9Schristos     AcpiNsCheckArgumentTypes (Info);
191a2c051a9Schristos 
192a2c051a9Schristos     /*
193a2c051a9Schristos      * Three major evaluation cases:
194a2c051a9Schristos      *
195a2c051a9Schristos      * 1) Object types that cannot be evaluated by definition
196a2c051a9Schristos      * 2) The object is a control method -- execute it
197a2c051a9Schristos      * 3) The object is not a method -- just return it's current value
198a2c051a9Schristos      */
199a2c051a9Schristos     switch (AcpiNsGetType (Info->Node))
200a2c051a9Schristos     {
201f45f09e8Schristos     case ACPI_TYPE_ANY:
202a2c051a9Schristos     case ACPI_TYPE_DEVICE:
203a2c051a9Schristos     case ACPI_TYPE_EVENT:
204a2c051a9Schristos     case ACPI_TYPE_MUTEX:
205a2c051a9Schristos     case ACPI_TYPE_REGION:
206a2c051a9Schristos     case ACPI_TYPE_THERMAL:
207a2c051a9Schristos     case ACPI_TYPE_LOCAL_SCOPE:
208a2c051a9Schristos         /*
209f45f09e8Schristos          * 1) Disallow evaluation of these object types. For these,
210f45f09e8Schristos          *    object evaluation is undefined.
211a2c051a9Schristos          */
212a2c051a9Schristos         ACPI_ERROR ((AE_INFO,
213f45f09e8Schristos             "%s: This object type [%s] "
214f45f09e8Schristos             "never contains data and cannot be evaluated",
215f45f09e8Schristos             Info->FullPathname, AcpiUtGetTypeName (Info->Node->Type)));
216a2c051a9Schristos 
217a2c051a9Schristos         Status = AE_TYPE;
218a2c051a9Schristos         goto Cleanup;
219a2c051a9Schristos 
220a2c051a9Schristos     case ACPI_TYPE_METHOD:
221a2c051a9Schristos         /*
222a2c051a9Schristos          * 2) Object is a control method - execute it
223a2c051a9Schristos          */
224a2c051a9Schristos 
225a2c051a9Schristos         /* Verify that there is a method object associated with this node */
226a2c051a9Schristos 
227a2c051a9Schristos         if (!Info->ObjDesc)
228a2c051a9Schristos         {
229a2c051a9Schristos             ACPI_ERROR ((AE_INFO, "%s: Method has no attached sub-object",
230a2c051a9Schristos                 Info->FullPathname));
231a2c051a9Schristos             Status = AE_NULL_OBJECT;
232a2c051a9Schristos             goto Cleanup;
233a2c051a9Schristos         }
23428c506b8Sjruoho 
23528c506b8Sjruoho         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
236a2c051a9Schristos             "**** Execute method [%s] at AML address %p length %X\n",
237a2c051a9Schristos             Info->FullPathname,
23828c506b8Sjruoho             Info->ObjDesc->Method.AmlStart + 1,
23928c506b8Sjruoho             Info->ObjDesc->Method.AmlLength - 1));
24028c506b8Sjruoho 
24128c506b8Sjruoho         /*
24228c506b8Sjruoho          * Any namespace deletion must acquire both the namespace and
24328c506b8Sjruoho          * interpreter locks to ensure that no thread is using the portion of
24428c506b8Sjruoho          * the namespace that is being deleted.
24528c506b8Sjruoho          *
24628c506b8Sjruoho          * Execute the method via the interpreter. The interpreter is locked
24728c506b8Sjruoho          * here before calling into the AML parser
24828c506b8Sjruoho          */
24928c506b8Sjruoho         AcpiExEnterInterpreter ();
25028c506b8Sjruoho         Status = AcpiPsExecuteMethod (Info);
25128c506b8Sjruoho         AcpiExExitInterpreter ();
252a2c051a9Schristos         break;
25328c506b8Sjruoho 
25428c506b8Sjruoho     default:
255a2c051a9Schristos         /*
256a2c051a9Schristos          * 3) All other non-method objects -- get the current object value
257a2c051a9Schristos          */
25828c506b8Sjruoho 
25928c506b8Sjruoho         /*
260a2c051a9Schristos          * Some objects require additional resolution steps (e.g., the Node
261a2c051a9Schristos          * may be a field that must be read, etc.) -- we can't just grab
262a2c051a9Schristos          * the object out of the node.
26328c506b8Sjruoho          *
26428c506b8Sjruoho          * Use ResolveNodeToValue() to get the associated value.
26528c506b8Sjruoho          *
26628c506b8Sjruoho          * NOTE: we can get away with passing in NULL for a walk state because
267a2c051a9Schristos          * the Node is guaranteed to not be a reference to either a method
26828c506b8Sjruoho          * local or a method argument (because this interface is never called
26928c506b8Sjruoho          * from a running method.)
27028c506b8Sjruoho          *
27128c506b8Sjruoho          * Even though we do not directly invoke the interpreter for object
272a2c051a9Schristos          * resolution, we must lock it because we could access an OpRegion.
273a2c051a9Schristos          * The OpRegion access code assumes that the interpreter is locked.
27428c506b8Sjruoho          */
27528c506b8Sjruoho         AcpiExEnterInterpreter ();
27628c506b8Sjruoho 
277a2c051a9Schristos         /* TBD: ResolveNodeToValue has a strange interface, fix */
27828c506b8Sjruoho 
279a2c051a9Schristos         Info->ReturnObject = ACPI_CAST_PTR (ACPI_OPERAND_OBJECT, Info->Node);
280a2c051a9Schristos 
281a2c051a9Schristos         Status = AcpiExResolveNodeToValue (ACPI_CAST_INDIRECT_PTR (
282a2c051a9Schristos             ACPI_NAMESPACE_NODE, &Info->ReturnObject), NULL);
28328c506b8Sjruoho         AcpiExExitInterpreter ();
28428c506b8Sjruoho 
285a2c051a9Schristos         if (ACPI_FAILURE (Status))
28628c506b8Sjruoho         {
28781bd9c9cSchristos             Info->ReturnObject = NULL;
288a2c051a9Schristos             goto Cleanup;
289a2c051a9Schristos         }
29028c506b8Sjruoho 
291a2c051a9Schristos         ACPI_DEBUG_PRINT ((ACPI_DB_NAMES, "Returned object %p [%s]\n",
29228c506b8Sjruoho             Info->ReturnObject,
29328c506b8Sjruoho             AcpiUtGetObjectTypeName (Info->ReturnObject)));
294a2c051a9Schristos 
295a2c051a9Schristos         Status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
296a2c051a9Schristos         break;
29728c506b8Sjruoho     }
29828c506b8Sjruoho 
29928c506b8Sjruoho     /*
300a2c051a9Schristos      * For predefined names, check the return value against the ACPI
301a2c051a9Schristos      * specification. Some incorrect return value types are repaired.
30228c506b8Sjruoho      */
303a2c051a9Schristos     (void) AcpiNsCheckReturnValue (Info->Node, Info, Info->ParamCount,
30428c506b8Sjruoho         Status, &Info->ReturnObject);
30528c506b8Sjruoho 
30628c506b8Sjruoho     /* Check if there is a return value that must be dealt with */
30728c506b8Sjruoho 
30828c506b8Sjruoho     if (Status == AE_CTRL_RETURN_VALUE)
30928c506b8Sjruoho     {
31028c506b8Sjruoho         /* If caller does not want the return value, delete it */
31128c506b8Sjruoho 
31228c506b8Sjruoho         if (Info->Flags & ACPI_IGNORE_RETURN_VALUE)
31328c506b8Sjruoho         {
31428c506b8Sjruoho             AcpiUtRemoveReference (Info->ReturnObject);
31528c506b8Sjruoho             Info->ReturnObject = NULL;
31628c506b8Sjruoho         }
31728c506b8Sjruoho 
31828c506b8Sjruoho         /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
31928c506b8Sjruoho 
32028c506b8Sjruoho         Status = AE_OK;
32128c506b8Sjruoho     }
322f45f09e8Schristos     else if (ACPI_FAILURE(Status))
323f45f09e8Schristos     {
324f45f09e8Schristos         /* If ReturnObject exists, delete it */
325f45f09e8Schristos 
326f45f09e8Schristos         if (Info->ReturnObject)
327f45f09e8Schristos         {
328f45f09e8Schristos             AcpiUtRemoveReference (Info->ReturnObject);
329f45f09e8Schristos             Info->ReturnObject = NULL;
330f45f09e8Schristos         }
331f45f09e8Schristos     }
33228c506b8Sjruoho 
33328c506b8Sjruoho     ACPI_DEBUG_PRINT ((ACPI_DB_NAMES,
334a2c051a9Schristos         "*** Completed evaluation of object %s ***\n",
335a2c051a9Schristos         Info->RelativePathname));
33628c506b8Sjruoho 
337a2c051a9Schristos Cleanup:
3387efa3256Schristos     /* Optional object evaluation log */
3397efa3256Schristos 
3407efa3256Schristos     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_EVALUATION,
3417efa3256Schristos         "%-26s:  %s\n", "   Exit evaluation",
3427efa3256Schristos         &Info->FullPathname[1]));
3437efa3256Schristos 
34428c506b8Sjruoho     /*
34528c506b8Sjruoho      * Namespace was unlocked by the handling AcpiNs* function, so we
346a2c051a9Schristos      * just free the pathname and return
34728c506b8Sjruoho      */
348a2c051a9Schristos     ACPI_FREE (Info->FullPathname);
349a2c051a9Schristos     Info->FullPathname = NULL;
35028c506b8Sjruoho     return_ACPI_STATUS (Status);
35128c506b8Sjruoho }
352