1*4882a593Smuzhiyun // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2*4882a593Smuzhiyun /*******************************************************************************
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Module Name: nseval - Object evaluation, includes control method execution
5*4882a593Smuzhiyun *
6*4882a593Smuzhiyun ******************************************************************************/
7*4882a593Smuzhiyun
8*4882a593Smuzhiyun #include <acpi/acpi.h>
9*4882a593Smuzhiyun #include "accommon.h"
10*4882a593Smuzhiyun #include "acparser.h"
11*4882a593Smuzhiyun #include "acinterp.h"
12*4882a593Smuzhiyun #include "acnamesp.h"
13*4882a593Smuzhiyun
14*4882a593Smuzhiyun #define _COMPONENT ACPI_NAMESPACE
15*4882a593Smuzhiyun ACPI_MODULE_NAME("nseval")
16*4882a593Smuzhiyun
17*4882a593Smuzhiyun /*******************************************************************************
18*4882a593Smuzhiyun *
19*4882a593Smuzhiyun * FUNCTION: acpi_ns_evaluate
20*4882a593Smuzhiyun *
21*4882a593Smuzhiyun * PARAMETERS: info - Evaluation info block, contains these fields
22*4882a593Smuzhiyun * and more:
23*4882a593Smuzhiyun * prefix_node - Prefix or Method/Object Node to execute
24*4882a593Smuzhiyun * relative_path - Name of method to execute, If NULL, the
25*4882a593Smuzhiyun * Node is the object to execute
26*4882a593Smuzhiyun * parameters - List of parameters to pass to the method,
27*4882a593Smuzhiyun * terminated by NULL. Params itself may be
28*4882a593Smuzhiyun * NULL if no parameters are being passed.
29*4882a593Smuzhiyun * parameter_type - Type of Parameter list
30*4882a593Smuzhiyun * return_object - Where to put method's return value (if
31*4882a593Smuzhiyun * any). If NULL, no value is returned.
32*4882a593Smuzhiyun * flags - ACPI_IGNORE_RETURN_VALUE to delete return
33*4882a593Smuzhiyun *
34*4882a593Smuzhiyun * RETURN: Status
35*4882a593Smuzhiyun *
36*4882a593Smuzhiyun * DESCRIPTION: Execute a control method or return the current value of an
37*4882a593Smuzhiyun * ACPI namespace object.
38*4882a593Smuzhiyun *
39*4882a593Smuzhiyun * MUTEX: Locks interpreter
40*4882a593Smuzhiyun *
41*4882a593Smuzhiyun ******************************************************************************/
acpi_ns_evaluate(struct acpi_evaluate_info * info)42*4882a593Smuzhiyun acpi_status acpi_ns_evaluate(struct acpi_evaluate_info *info)
43*4882a593Smuzhiyun {
44*4882a593Smuzhiyun acpi_status status;
45*4882a593Smuzhiyun
46*4882a593Smuzhiyun ACPI_FUNCTION_TRACE(ns_evaluate);
47*4882a593Smuzhiyun
48*4882a593Smuzhiyun if (!info) {
49*4882a593Smuzhiyun return_ACPI_STATUS(AE_BAD_PARAMETER);
50*4882a593Smuzhiyun }
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun if (!info->node) {
53*4882a593Smuzhiyun /*
54*4882a593Smuzhiyun * Get the actual namespace node for the target object if we
55*4882a593Smuzhiyun * need to. Handles these cases:
56*4882a593Smuzhiyun *
57*4882a593Smuzhiyun * 1) Null node, valid pathname from root (absolute path)
58*4882a593Smuzhiyun * 2) Node and valid pathname (path relative to Node)
59*4882a593Smuzhiyun * 3) Node, Null pathname
60*4882a593Smuzhiyun */
61*4882a593Smuzhiyun status =
62*4882a593Smuzhiyun acpi_ns_get_node(info->prefix_node, info->relative_pathname,
63*4882a593Smuzhiyun ACPI_NS_NO_UPSEARCH, &info->node);
64*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
65*4882a593Smuzhiyun return_ACPI_STATUS(status);
66*4882a593Smuzhiyun }
67*4882a593Smuzhiyun }
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /*
70*4882a593Smuzhiyun * For a method alias, we must grab the actual method node so that
71*4882a593Smuzhiyun * proper scoping context will be established before execution.
72*4882a593Smuzhiyun */
73*4882a593Smuzhiyun if (acpi_ns_get_type(info->node) == ACPI_TYPE_LOCAL_METHOD_ALIAS) {
74*4882a593Smuzhiyun info->node =
75*4882a593Smuzhiyun ACPI_CAST_PTR(struct acpi_namespace_node,
76*4882a593Smuzhiyun info->node->object);
77*4882a593Smuzhiyun }
78*4882a593Smuzhiyun
79*4882a593Smuzhiyun /* Complete the info block initialization */
80*4882a593Smuzhiyun
81*4882a593Smuzhiyun info->return_object = NULL;
82*4882a593Smuzhiyun info->node_flags = info->node->flags;
83*4882a593Smuzhiyun info->obj_desc = acpi_ns_get_attached_object(info->node);
84*4882a593Smuzhiyun
85*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "%s [%p] Value %p\n",
86*4882a593Smuzhiyun info->relative_pathname, info->node,
87*4882a593Smuzhiyun acpi_ns_get_attached_object(info->node)));
88*4882a593Smuzhiyun
89*4882a593Smuzhiyun /* Get info if we have a predefined name (_HID, etc.) */
90*4882a593Smuzhiyun
91*4882a593Smuzhiyun info->predefined =
92*4882a593Smuzhiyun acpi_ut_match_predefined_method(info->node->name.ascii);
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun /* Get the full pathname to the object, for use in warning messages */
95*4882a593Smuzhiyun
96*4882a593Smuzhiyun info->full_pathname = acpi_ns_get_normalized_pathname(info->node, TRUE);
97*4882a593Smuzhiyun if (!info->full_pathname) {
98*4882a593Smuzhiyun return_ACPI_STATUS(AE_NO_MEMORY);
99*4882a593Smuzhiyun }
100*4882a593Smuzhiyun
101*4882a593Smuzhiyun /* Optional object evaluation log */
102*4882a593Smuzhiyun
103*4882a593Smuzhiyun ACPI_DEBUG_PRINT_RAW((ACPI_DB_EVALUATION,
104*4882a593Smuzhiyun "%-26s: %s (%s)\n", " Enter evaluation",
105*4882a593Smuzhiyun &info->full_pathname[1],
106*4882a593Smuzhiyun acpi_ut_get_type_name(info->node->type)));
107*4882a593Smuzhiyun
108*4882a593Smuzhiyun /* Count the number of arguments being passed in */
109*4882a593Smuzhiyun
110*4882a593Smuzhiyun info->param_count = 0;
111*4882a593Smuzhiyun if (info->parameters) {
112*4882a593Smuzhiyun while (info->parameters[info->param_count]) {
113*4882a593Smuzhiyun info->param_count++;
114*4882a593Smuzhiyun }
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun /* Warn on impossible argument count */
117*4882a593Smuzhiyun
118*4882a593Smuzhiyun if (info->param_count > ACPI_METHOD_NUM_ARGS) {
119*4882a593Smuzhiyun ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
120*4882a593Smuzhiyun ACPI_WARN_ALWAYS,
121*4882a593Smuzhiyun "Excess arguments (%u) - using only %u",
122*4882a593Smuzhiyun info->param_count,
123*4882a593Smuzhiyun ACPI_METHOD_NUM_ARGS));
124*4882a593Smuzhiyun
125*4882a593Smuzhiyun info->param_count = ACPI_METHOD_NUM_ARGS;
126*4882a593Smuzhiyun }
127*4882a593Smuzhiyun }
128*4882a593Smuzhiyun
129*4882a593Smuzhiyun /*
130*4882a593Smuzhiyun * For predefined names: Check that the declared argument count
131*4882a593Smuzhiyun * matches the ACPI spec -- otherwise this is a BIOS error.
132*4882a593Smuzhiyun */
133*4882a593Smuzhiyun acpi_ns_check_acpi_compliance(info->full_pathname, info->node,
134*4882a593Smuzhiyun info->predefined);
135*4882a593Smuzhiyun
136*4882a593Smuzhiyun /*
137*4882a593Smuzhiyun * For all names: Check that the incoming argument count for
138*4882a593Smuzhiyun * this method/object matches the actual ASL/AML definition.
139*4882a593Smuzhiyun */
140*4882a593Smuzhiyun acpi_ns_check_argument_count(info->full_pathname, info->node,
141*4882a593Smuzhiyun info->param_count, info->predefined);
142*4882a593Smuzhiyun
143*4882a593Smuzhiyun /* For predefined names: Typecheck all incoming arguments */
144*4882a593Smuzhiyun
145*4882a593Smuzhiyun acpi_ns_check_argument_types(info);
146*4882a593Smuzhiyun
147*4882a593Smuzhiyun /*
148*4882a593Smuzhiyun * Three major evaluation cases:
149*4882a593Smuzhiyun *
150*4882a593Smuzhiyun * 1) Object types that cannot be evaluated by definition
151*4882a593Smuzhiyun * 2) The object is a control method -- execute it
152*4882a593Smuzhiyun * 3) The object is not a method -- just return it's current value
153*4882a593Smuzhiyun */
154*4882a593Smuzhiyun switch (acpi_ns_get_type(info->node)) {
155*4882a593Smuzhiyun case ACPI_TYPE_ANY:
156*4882a593Smuzhiyun case ACPI_TYPE_DEVICE:
157*4882a593Smuzhiyun case ACPI_TYPE_EVENT:
158*4882a593Smuzhiyun case ACPI_TYPE_MUTEX:
159*4882a593Smuzhiyun case ACPI_TYPE_REGION:
160*4882a593Smuzhiyun case ACPI_TYPE_THERMAL:
161*4882a593Smuzhiyun case ACPI_TYPE_LOCAL_SCOPE:
162*4882a593Smuzhiyun /*
163*4882a593Smuzhiyun * 1) Disallow evaluation of these object types. For these,
164*4882a593Smuzhiyun * object evaluation is undefined.
165*4882a593Smuzhiyun */
166*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
167*4882a593Smuzhiyun "%s: This object type [%s] "
168*4882a593Smuzhiyun "never contains data and cannot be evaluated",
169*4882a593Smuzhiyun info->full_pathname,
170*4882a593Smuzhiyun acpi_ut_get_type_name(info->node->type)));
171*4882a593Smuzhiyun
172*4882a593Smuzhiyun status = AE_TYPE;
173*4882a593Smuzhiyun goto cleanup;
174*4882a593Smuzhiyun
175*4882a593Smuzhiyun case ACPI_TYPE_METHOD:
176*4882a593Smuzhiyun /*
177*4882a593Smuzhiyun * 2) Object is a control method - execute it
178*4882a593Smuzhiyun */
179*4882a593Smuzhiyun
180*4882a593Smuzhiyun /* Verify that there is a method object associated with this node */
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun if (!info->obj_desc) {
183*4882a593Smuzhiyun ACPI_ERROR((AE_INFO,
184*4882a593Smuzhiyun "%s: Method has no attached sub-object",
185*4882a593Smuzhiyun info->full_pathname));
186*4882a593Smuzhiyun status = AE_NULL_OBJECT;
187*4882a593Smuzhiyun goto cleanup;
188*4882a593Smuzhiyun }
189*4882a593Smuzhiyun
190*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
191*4882a593Smuzhiyun "**** Execute method [%s] at AML address %p length %X\n",
192*4882a593Smuzhiyun info->full_pathname,
193*4882a593Smuzhiyun info->obj_desc->method.aml_start + 1,
194*4882a593Smuzhiyun info->obj_desc->method.aml_length - 1));
195*4882a593Smuzhiyun
196*4882a593Smuzhiyun /*
197*4882a593Smuzhiyun * Any namespace deletion must acquire both the namespace and
198*4882a593Smuzhiyun * interpreter locks to ensure that no thread is using the portion of
199*4882a593Smuzhiyun * the namespace that is being deleted.
200*4882a593Smuzhiyun *
201*4882a593Smuzhiyun * Execute the method via the interpreter. The interpreter is locked
202*4882a593Smuzhiyun * here before calling into the AML parser
203*4882a593Smuzhiyun */
204*4882a593Smuzhiyun acpi_ex_enter_interpreter();
205*4882a593Smuzhiyun status = acpi_ps_execute_method(info);
206*4882a593Smuzhiyun acpi_ex_exit_interpreter();
207*4882a593Smuzhiyun break;
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun default:
210*4882a593Smuzhiyun /*
211*4882a593Smuzhiyun * 3) All other non-method objects -- get the current object value
212*4882a593Smuzhiyun */
213*4882a593Smuzhiyun
214*4882a593Smuzhiyun /*
215*4882a593Smuzhiyun * Some objects require additional resolution steps (e.g., the Node
216*4882a593Smuzhiyun * may be a field that must be read, etc.) -- we can't just grab
217*4882a593Smuzhiyun * the object out of the node.
218*4882a593Smuzhiyun *
219*4882a593Smuzhiyun * Use resolve_node_to_value() to get the associated value.
220*4882a593Smuzhiyun *
221*4882a593Smuzhiyun * NOTE: we can get away with passing in NULL for a walk state because
222*4882a593Smuzhiyun * the Node is guaranteed to not be a reference to either a method
223*4882a593Smuzhiyun * local or a method argument (because this interface is never called
224*4882a593Smuzhiyun * from a running method.)
225*4882a593Smuzhiyun *
226*4882a593Smuzhiyun * Even though we do not directly invoke the interpreter for object
227*4882a593Smuzhiyun * resolution, we must lock it because we could access an op_region.
228*4882a593Smuzhiyun * The op_region access code assumes that the interpreter is locked.
229*4882a593Smuzhiyun */
230*4882a593Smuzhiyun acpi_ex_enter_interpreter();
231*4882a593Smuzhiyun
232*4882a593Smuzhiyun /* TBD: resolve_node_to_value has a strange interface, fix */
233*4882a593Smuzhiyun
234*4882a593Smuzhiyun info->return_object =
235*4882a593Smuzhiyun ACPI_CAST_PTR(union acpi_operand_object, info->node);
236*4882a593Smuzhiyun
237*4882a593Smuzhiyun status =
238*4882a593Smuzhiyun acpi_ex_resolve_node_to_value(ACPI_CAST_INDIRECT_PTR
239*4882a593Smuzhiyun (struct acpi_namespace_node,
240*4882a593Smuzhiyun &info->return_object), NULL);
241*4882a593Smuzhiyun acpi_ex_exit_interpreter();
242*4882a593Smuzhiyun
243*4882a593Smuzhiyun if (ACPI_FAILURE(status)) {
244*4882a593Smuzhiyun info->return_object = NULL;
245*4882a593Smuzhiyun goto cleanup;
246*4882a593Smuzhiyun }
247*4882a593Smuzhiyun
248*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "Returned object %p [%s]\n",
249*4882a593Smuzhiyun info->return_object,
250*4882a593Smuzhiyun acpi_ut_get_object_type_name(info->
251*4882a593Smuzhiyun return_object)));
252*4882a593Smuzhiyun
253*4882a593Smuzhiyun status = AE_CTRL_RETURN_VALUE; /* Always has a "return value" */
254*4882a593Smuzhiyun break;
255*4882a593Smuzhiyun }
256*4882a593Smuzhiyun
257*4882a593Smuzhiyun /*
258*4882a593Smuzhiyun * For predefined names, check the return value against the ACPI
259*4882a593Smuzhiyun * specification. Some incorrect return value types are repaired.
260*4882a593Smuzhiyun */
261*4882a593Smuzhiyun (void)acpi_ns_check_return_value(info->node, info, info->param_count,
262*4882a593Smuzhiyun status, &info->return_object);
263*4882a593Smuzhiyun
264*4882a593Smuzhiyun /* Check if there is a return value that must be dealt with */
265*4882a593Smuzhiyun
266*4882a593Smuzhiyun if (status == AE_CTRL_RETURN_VALUE) {
267*4882a593Smuzhiyun
268*4882a593Smuzhiyun /* If caller does not want the return value, delete it */
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun if (info->flags & ACPI_IGNORE_RETURN_VALUE) {
271*4882a593Smuzhiyun acpi_ut_remove_reference(info->return_object);
272*4882a593Smuzhiyun info->return_object = NULL;
273*4882a593Smuzhiyun }
274*4882a593Smuzhiyun
275*4882a593Smuzhiyun /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
276*4882a593Smuzhiyun
277*4882a593Smuzhiyun status = AE_OK;
278*4882a593Smuzhiyun } else if (ACPI_FAILURE(status)) {
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun /* If return_object exists, delete it */
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun if (info->return_object) {
283*4882a593Smuzhiyun acpi_ut_remove_reference(info->return_object);
284*4882a593Smuzhiyun info->return_object = NULL;
285*4882a593Smuzhiyun }
286*4882a593Smuzhiyun }
287*4882a593Smuzhiyun
288*4882a593Smuzhiyun ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
289*4882a593Smuzhiyun "*** Completed evaluation of object %s ***\n",
290*4882a593Smuzhiyun info->relative_pathname));
291*4882a593Smuzhiyun
292*4882a593Smuzhiyun cleanup:
293*4882a593Smuzhiyun /* Optional object evaluation log */
294*4882a593Smuzhiyun
295*4882a593Smuzhiyun ACPI_DEBUG_PRINT_RAW((ACPI_DB_EVALUATION,
296*4882a593Smuzhiyun "%-26s: %s\n", " Exit evaluation",
297*4882a593Smuzhiyun &info->full_pathname[1]));
298*4882a593Smuzhiyun
299*4882a593Smuzhiyun /*
300*4882a593Smuzhiyun * Namespace was unlocked by the handling acpi_ns* function, so we
301*4882a593Smuzhiyun * just free the pathname and return
302*4882a593Smuzhiyun */
303*4882a593Smuzhiyun ACPI_FREE(info->full_pathname);
304*4882a593Smuzhiyun info->full_pathname = NULL;
305*4882a593Smuzhiyun return_ACPI_STATUS(status);
306*4882a593Smuzhiyun }
307