blob: f52829cc294b2fd8abc5d45114ccceea4ab233be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*******************************************************************************
2 *
Bob Moore41195322006-05-26 16:36:00 -04003 * Module Name: nseval - Object evaluation, includes control method execution
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
5 ******************************************************************************/
6
7/*
Bob Moorea8357b02010-01-22 19:07:36 +08008 * Copyright (C) 2000 - 2010, Intel Corp.
Linus Torvalds1da177e2005-04-16 15:20:36 -07009 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions, and the following disclaimer,
16 * without modification.
17 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18 * substantially similar to the "NO WARRANTY" disclaimer below
19 * ("Disclaimer") and any redistribution must be conditioned upon
20 * including a substantially similar Disclaimer requirement for further
21 * binary redistribution.
22 * 3. Neither the names of the above-listed copyright holders nor the names
23 * of any contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * Alternatively, this software may be distributed under the terms of the
27 * GNU General Public License ("GPL") version 2 as published by the Free
28 * Software Foundation.
29 *
30 * NO WARRANTY
31 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41 * POSSIBILITY OF SUCH DAMAGES.
42 */
43
Linus Torvalds1da177e2005-04-16 15:20:36 -070044#include <acpi/acpi.h>
Len Browne2f7a772009-01-09 00:30:03 -050045#include "accommon.h"
46#include "acparser.h"
47#include "acinterp.h"
48#include "acnamesp.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070049
Linus Torvalds1da177e2005-04-16 15:20:36 -070050#define _COMPONENT ACPI_NAMESPACE
Len Brown4be44fc2005-08-05 00:44:28 -040051ACPI_MODULE_NAME("nseval")
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
Lin Ming7f0c8262009-08-13 14:03:15 +080053/* Local prototypes */
54static void
55acpi_ns_exec_module_code(union acpi_operand_object *method_obj,
56 struct acpi_evaluate_info *info);
57
Linus Torvalds1da177e2005-04-16 15:20:36 -070058/*******************************************************************************
59 *
Bob Moore41195322006-05-26 16:36:00 -040060 * FUNCTION: acpi_ns_evaluate
Linus Torvalds1da177e2005-04-16 15:20:36 -070061 *
Bob Moore41195322006-05-26 16:36:00 -040062 * PARAMETERS: Info - Evaluation info block, contains:
63 * prefix_node - Prefix or Method/Object Node to execute
64 * Pathname - Name of method to execute, If NULL, the
65 * Node is the object to execute
Robert Moore44f6c012005-04-18 22:49:35 -040066 * Parameters - List of parameters to pass to the method,
67 * terminated by NULL. Params itself may be
Linus Torvalds1da177e2005-04-16 15:20:36 -070068 * NULL if no parameters are being passed.
Robert Moore44f6c012005-04-18 22:49:35 -040069 * return_object - Where to put method's return value (if
70 * any). If NULL, no value is returned.
71 * parameter_type - Type of Parameter list
72 * return_object - Where to put method's return value (if
73 * any). If NULL, no value is returned.
Bob Moore41195322006-05-26 16:36:00 -040074 * Flags - ACPI_IGNORE_RETURN_VALUE to delete return
Linus Torvalds1da177e2005-04-16 15:20:36 -070075 *
76 * RETURN: Status
77 *
Bob Moore41195322006-05-26 16:36:00 -040078 * DESCRIPTION: Execute a control method or return the current value of an
79 * ACPI namespace object.
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 *
Bob Moore41195322006-05-26 16:36:00 -040081 * MUTEX: Locks interpreter
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 *
83 ******************************************************************************/
Lin Ming7f0c8262009-08-13 14:03:15 +080084
Len Brownfd350942007-05-09 23:34:35 -040085acpi_status acpi_ns_evaluate(struct acpi_evaluate_info * info)
Linus Torvalds1da177e2005-04-16 15:20:36 -070086{
Len Brown4be44fc2005-08-05 00:44:28 -040087 acpi_status status;
Bob Mooree8707b32008-09-28 15:26:17 +080088 struct acpi_namespace_node *node;
Linus Torvalds1da177e2005-04-16 15:20:36 -070089
Bob Moore41195322006-05-26 16:36:00 -040090 ACPI_FUNCTION_TRACE(ns_evaluate);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091
92 if (!info) {
Len Brown4be44fc2005-08-05 00:44:28 -040093 return_ACPI_STATUS(AE_BAD_PARAMETER);
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 }
95
96 /* Initialize the return value to an invalid object */
97
98 info->return_object = NULL;
Bob Mooreeeb44372008-11-13 11:19:24 +080099 info->param_count = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100
Bob Moore41195322006-05-26 16:36:00 -0400101 /*
102 * Get the actual namespace node for the target object. Handles these cases:
103 *
104 * 1) Null node, Pathname (absolute path)
105 * 2) Node, Pathname (path relative to Node)
106 * 3) Node, Null Pathname
107 */
108 status = acpi_ns_get_node(info->prefix_node, info->pathname,
109 ACPI_NS_NO_UPSEARCH, &info->resolved_node);
Len Brown4be44fc2005-08-05 00:44:28 -0400110 if (ACPI_FAILURE(status)) {
111 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700112 }
113
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 /*
115 * For a method alias, we must grab the actual method node so that proper
116 * scoping context will be established before execution.
117 */
Bob Moore41195322006-05-26 16:36:00 -0400118 if (acpi_ns_get_type(info->resolved_node) ==
119 ACPI_TYPE_LOCAL_METHOD_ALIAS) {
120 info->resolved_node =
Len Brown4be44fc2005-08-05 00:44:28 -0400121 ACPI_CAST_PTR(struct acpi_namespace_node,
Bob Moore41195322006-05-26 16:36:00 -0400122 info->resolved_node->object);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123 }
124
Bob Moore41195322006-05-26 16:36:00 -0400125 ACPI_DEBUG_PRINT((ACPI_DB_NAMES, "%s [%p] Value %p\n", info->pathname,
126 info->resolved_node,
127 acpi_ns_get_attached_object(info->resolved_node)));
128
Bob Mooree8707b32008-09-28 15:26:17 +0800129 node = info->resolved_node;
130
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131 /*
132 * Two major cases here:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 *
Bob Moore41195322006-05-26 16:36:00 -0400134 * 1) The object is a control method -- execute it
135 * 2) The object is not a method -- just return it's current value
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
Bob Moore41195322006-05-26 16:36:00 -0400137 if (acpi_ns_get_type(info->resolved_node) == ACPI_TYPE_METHOD) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138 /*
Bob Moore41195322006-05-26 16:36:00 -0400139 * 1) Object is a control method - execute it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
Bob Moore41195322006-05-26 16:36:00 -0400141
142 /* Verify that there is a method object associated with this node */
143
144 info->obj_desc =
145 acpi_ns_get_attached_object(info->resolved_node);
146 if (!info->obj_desc) {
147 ACPI_ERROR((AE_INFO,
148 "Control method has no attached sub-object"));
149 return_ACPI_STATUS(AE_NULL_OBJECT);
150 }
151
Bob Mooreeeb44372008-11-13 11:19:24 +0800152 /* Count the number of arguments being passed to the method */
Bob Mooref3454ae2008-06-10 12:25:42 +0800153
Bob Mooref3454ae2008-06-10 12:25:42 +0800154 if (info->parameters) {
Bob Mooreeeb44372008-11-13 11:19:24 +0800155 while (info->parameters[info->param_count]) {
156 if (info->param_count > ACPI_METHOD_MAX_ARG) {
157 return_ACPI_STATUS(AE_LIMIT);
158 }
Bob Mooref3454ae2008-06-10 12:25:42 +0800159 info->param_count++;
Bob Mooreeeb44372008-11-13 11:19:24 +0800160 }
Bob Mooref3454ae2008-06-10 12:25:42 +0800161 }
162
Bob Mooref3454ae2008-06-10 12:25:42 +0800163
Bob Moore7bcc06e2009-02-18 14:58:08 +0800164 ACPI_DUMP_PATHNAME(info->resolved_node, "ACPI: Execute Method",
Bob Moore41195322006-05-26 16:36:00 -0400165 ACPI_LV_INFO, _COMPONENT);
166
167 ACPI_DEBUG_PRINT((ACPI_DB_EXEC,
168 "Method at AML address %p Length %X\n",
169 info->obj_desc->method.aml_start + 1,
170 info->obj_desc->method.aml_length - 1));
171
172 /*
173 * Any namespace deletion must acquire both the namespace and
174 * interpreter locks to ensure that no thread is using the portion of
175 * the namespace that is being deleted.
176 *
177 * Execute the method via the interpreter. The interpreter is locked
178 * here before calling into the AML parser
179 */
Len Brown4d2acd92007-05-09 22:56:38 -0400180 acpi_ex_enter_interpreter();
Bob Moore41195322006-05-26 16:36:00 -0400181 status = acpi_ps_execute_method(info);
182 acpi_ex_exit_interpreter();
Len Brown4be44fc2005-08-05 00:44:28 -0400183 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184 /*
Bob Moore41195322006-05-26 16:36:00 -0400185 * 2) Object is not a method, return its current value
Bob Mooreb68bacf2008-09-27 10:29:31 +0800186 *
187 * Disallow certain object types. For these, "evaluation" is undefined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188 */
Bob Mooreb68bacf2008-09-27 10:29:31 +0800189 switch (info->resolved_node->type) {
190 case ACPI_TYPE_DEVICE:
191 case ACPI_TYPE_EVENT:
192 case ACPI_TYPE_MUTEX:
193 case ACPI_TYPE_REGION:
194 case ACPI_TYPE_THERMAL:
195 case ACPI_TYPE_LOCAL_SCOPE:
196
197 ACPI_ERROR((AE_INFO,
198 "[%4.4s] Evaluation of object type [%s] is not supported",
199 info->resolved_node->name.ascii,
200 acpi_ut_get_type_name(info->resolved_node->
201 type)));
202
203 return_ACPI_STATUS(AE_TYPE);
204
205 default:
206 break;
207 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208
Bob Moore41195322006-05-26 16:36:00 -0400209 /*
210 * Objects require additional resolution steps (e.g., the Node may be
211 * a field that must be read, etc.) -- we can't just grab the object
212 * out of the node.
213 *
214 * Use resolve_node_to_value() to get the associated value.
215 *
216 * NOTE: we can get away with passing in NULL for a walk state because
217 * resolved_node is guaranteed to not be a reference to either a method
218 * local or a method argument (because this interface is never called
219 * from a running method.)
220 *
221 * Even though we do not directly invoke the interpreter for object
222 * resolution, we must lock it because we could access an opregion.
223 * The opregion access code assumes that the interpreter is locked.
224 */
Len Brown4d2acd92007-05-09 22:56:38 -0400225 acpi_ex_enter_interpreter();
Bob Moore52fc0b02006-10-02 00:00:00 -0400226
Bob Moore41195322006-05-26 16:36:00 -0400227 /* Function has a strange interface */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700228
Bob Moore41195322006-05-26 16:36:00 -0400229 status =
230 acpi_ex_resolve_node_to_value(&info->resolved_node, NULL);
231 acpi_ex_exit_interpreter();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232
Linus Torvalds1da177e2005-04-16 15:20:36 -0700233 /*
234 * If acpi_ex_resolve_node_to_value() succeeded, the return value was placed
235 * in resolved_node.
236 */
Len Brown4be44fc2005-08-05 00:44:28 -0400237 if (ACPI_SUCCESS(status)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 status = AE_CTRL_RETURN_VALUE;
Bob Moore41195322006-05-26 16:36:00 -0400239 info->return_object =
240 ACPI_CAST_PTR(union acpi_operand_object,
241 info->resolved_node);
242
Len Brown4be44fc2005-08-05 00:44:28 -0400243 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
244 "Returning object %p [%s]\n",
245 info->return_object,
246 acpi_ut_get_object_type_name(info->
247 return_object)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 }
249 }
250
Bob Mooreeeb44372008-11-13 11:19:24 +0800251 /*
252 * Check input argument count against the ASL-defined count for a method.
253 * Also check predefined names: argument count and return value against
254 * the ACPI specification. Some incorrect return value types are repaired.
255 */
256 (void)acpi_ns_check_predefined_names(node, info->param_count,
257 status, &info->return_object);
Bob Mooree8707b32008-09-28 15:26:17 +0800258
259 /* Check if there is a return value that must be dealt with */
260
Bob Moore41195322006-05-26 16:36:00 -0400261 if (status == AE_CTRL_RETURN_VALUE) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700262
Bob Moore41195322006-05-26 16:36:00 -0400263 /* If caller does not want the return value, delete it */
264
265 if (info->flags & ACPI_IGNORE_RETURN_VALUE) {
266 acpi_ut_remove_reference(info->return_object);
267 info->return_object = NULL;
268 }
269
270 /* Map AE_CTRL_RETURN_VALUE to AE_OK, we are done with it */
271
272 status = AE_OK;
273 }
274
275 ACPI_DEBUG_PRINT((ACPI_DB_NAMES,
276 "*** Completed evaluation of object %s ***\n",
277 info->pathname));
278
279 /*
280 * Namespace was unlocked by the handling acpi_ns* function, so we
281 * just return
282 */
Len Brown4be44fc2005-08-05 00:44:28 -0400283 return_ACPI_STATUS(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284}
Lin Ming7f0c8262009-08-13 14:03:15 +0800285
286/*******************************************************************************
287 *
288 * FUNCTION: acpi_ns_exec_module_code_list
289 *
290 * PARAMETERS: None
291 *
292 * RETURN: None. Exceptions during method execution are ignored, since
293 * we cannot abort a table load.
294 *
295 * DESCRIPTION: Execute all elements of the global module-level code list.
296 * Each element is executed as a single control method.
297 *
298 ******************************************************************************/
299
300void acpi_ns_exec_module_code_list(void)
301{
302 union acpi_operand_object *prev;
303 union acpi_operand_object *next;
304 struct acpi_evaluate_info *info;
305 u32 method_count = 0;
306
307 ACPI_FUNCTION_TRACE(ns_exec_module_code_list);
308
309 /* Exit now if the list is empty */
310
311 next = acpi_gbl_module_code_list;
312 if (!next) {
313 return_VOID;
314 }
315
316 /* Allocate the evaluation information block */
317
318 info = ACPI_ALLOCATE(sizeof(struct acpi_evaluate_info));
319 if (!info) {
320 return_VOID;
321 }
322
323 /* Walk the list, executing each "method" */
324
325 while (next) {
326 prev = next;
327 next = next->method.mutex;
328
329 /* Clear the link field and execute the method */
330
331 prev->method.mutex = NULL;
332 acpi_ns_exec_module_code(prev, info);
333 method_count++;
334
335 /* Delete the (temporary) method object */
336
337 acpi_ut_remove_reference(prev);
338 }
339
340 ACPI_INFO((AE_INFO,
341 "Executed %u blocks of module-level executable AML code",
342 method_count));
343
344 ACPI_FREE(info);
345 acpi_gbl_module_code_list = NULL;
346 return_VOID;
347}
348
349/*******************************************************************************
350 *
351 * FUNCTION: acpi_ns_exec_module_code
352 *
353 * PARAMETERS: method_obj - Object container for the module-level code
354 * Info - Info block for method evaluation
355 *
356 * RETURN: None. Exceptions during method execution are ignored, since
357 * we cannot abort a table load.
358 *
359 * DESCRIPTION: Execute a control method containing a block of module-level
360 * executable AML code. The control method is temporarily
361 * installed to the root node, then evaluated.
362 *
363 ******************************************************************************/
364
365static void
366acpi_ns_exec_module_code(union acpi_operand_object *method_obj,
367 struct acpi_evaluate_info *info)
368{
Lin Ming9a884ab2009-11-12 09:57:53 +0800369 union acpi_operand_object *parent_obj;
370 struct acpi_namespace_node *parent_node;
371 acpi_object_type type;
Lin Ming7f0c8262009-08-13 14:03:15 +0800372 acpi_status status;
373
374 ACPI_FUNCTION_TRACE(ns_exec_module_code);
375
Lin Ming9a884ab2009-11-12 09:57:53 +0800376 /*
377 * Get the parent node. We cheat by using the next_object field
378 * of the method object descriptor.
379 */
380 parent_node = ACPI_CAST_PTR(struct acpi_namespace_node,
381 method_obj->method.next_object);
382 type = acpi_ns_get_type(parent_node);
383
Lin Minge31c32c2009-12-11 15:28:27 +0800384 /*
385 * Get the region handler and save it in the method object. We may need
386 * this if an operation region declaration causes a _REG method to be run.
387 *
388 * We can't do this in acpi_ps_link_module_code because
389 * acpi_gbl_root_node->Object is NULL at PASS1.
390 */
391 if ((type == ACPI_TYPE_DEVICE) && parent_node->object) {
392 method_obj->method.extra.handler =
393 parent_node->object->device.handler;
394 }
395
Lin Ming9a884ab2009-11-12 09:57:53 +0800396 /* Must clear next_object (acpi_ns_attach_object needs the field) */
397
398 method_obj->method.next_object = NULL;
399
Lin Ming7f0c8262009-08-13 14:03:15 +0800400 /* Initialize the evaluation information block */
401
402 ACPI_MEMSET(info, 0, sizeof(struct acpi_evaluate_info));
Lin Ming9a884ab2009-11-12 09:57:53 +0800403 info->prefix_node = parent_node;
Lin Ming7f0c8262009-08-13 14:03:15 +0800404
405 /*
Lin Ming9a884ab2009-11-12 09:57:53 +0800406 * Get the currently attached parent object. Add a reference, because the
Lin Ming7f0c8262009-08-13 14:03:15 +0800407 * ref count will be decreased when the method object is installed to
Lin Ming9a884ab2009-11-12 09:57:53 +0800408 * the parent node.
Lin Ming7f0c8262009-08-13 14:03:15 +0800409 */
Lin Ming9a884ab2009-11-12 09:57:53 +0800410 parent_obj = acpi_ns_get_attached_object(parent_node);
411 if (parent_obj) {
412 acpi_ut_add_reference(parent_obj);
413 }
Lin Ming7f0c8262009-08-13 14:03:15 +0800414
Lin Ming9a884ab2009-11-12 09:57:53 +0800415 /* Install the method (module-level code) in the parent node */
Lin Ming7f0c8262009-08-13 14:03:15 +0800416
Lin Ming9a884ab2009-11-12 09:57:53 +0800417 status = acpi_ns_attach_object(parent_node, method_obj,
Lin Ming7f0c8262009-08-13 14:03:15 +0800418 ACPI_TYPE_METHOD);
419 if (ACPI_FAILURE(status)) {
420 goto exit;
421 }
422
Lin Ming9a884ab2009-11-12 09:57:53 +0800423 /* Execute the parent node as a control method */
Lin Ming7f0c8262009-08-13 14:03:15 +0800424
425 status = acpi_ns_evaluate(info);
426
427 ACPI_DEBUG_PRINT((ACPI_DB_INIT, "Executed module-level code at %p\n",
428 method_obj->method.aml_start));
429
Lin Ming583061c2009-12-11 14:36:47 +0800430 /* Delete a possible implicit return value (in slack mode) */
431
432 if (info->return_object) {
433 acpi_ut_remove_reference(info->return_object);
434 }
435
Lin Ming7f0c8262009-08-13 14:03:15 +0800436 /* Detach the temporary method object */
437
Lin Ming9a884ab2009-11-12 09:57:53 +0800438 acpi_ns_detach_object(parent_node);
Lin Ming7f0c8262009-08-13 14:03:15 +0800439
Lin Ming9a884ab2009-11-12 09:57:53 +0800440 /* Restore the original parent object */
Lin Ming7f0c8262009-08-13 14:03:15 +0800441
Lin Ming9a884ab2009-11-12 09:57:53 +0800442 if (parent_obj) {
443 status = acpi_ns_attach_object(parent_node, parent_obj, type);
444 } else {
445 parent_node->type = (u8)type;
446 }
Lin Ming7f0c8262009-08-13 14:03:15 +0800447
448 exit:
Lin Ming9a884ab2009-11-12 09:57:53 +0800449 if (parent_obj) {
450 acpi_ut_remove_reference(parent_obj);
451 }
Lin Ming7f0c8262009-08-13 14:03:15 +0800452 return_VOID;
453}