ACPICA: Split internal error msg routines to a separate file
[linux-2.6-block.git] / drivers / acpi / acpica / nspredef.c
CommitLineData
e8707b34
BM
1/******************************************************************************
2 *
3 * Module Name: nspredef - Validation of ACPI predefined methods and objects
e8707b34
BM
4 *
5 *****************************************************************************/
6
7/*
25f044e6 8 * Copyright (C) 2000 - 2013, Intel Corp.
e8707b34
BM
9 * 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
999e08f9
BM
44#define ACPI_CREATE_PREDEFINED_TABLE
45
e8707b34 46#include <acpi/acpi.h>
e2f7a777
LB
47#include "accommon.h"
48#include "acnamesp.h"
49#include "acpredef.h"
e8707b34
BM
50
51#define _COMPONENT ACPI_NAMESPACE
52ACPI_MODULE_NAME("nspredef")
53
54/*******************************************************************************
55 *
56 * This module validates predefined ACPI objects that appear in the namespace,
57 * at the time they are evaluated (via acpi_evaluate_object). The purpose of this
58 * validation is to detect problems with BIOS-exposed predefined ACPI objects
59 * before the results are returned to the ACPI-related drivers.
60 *
61 * There are several areas that are validated:
62 *
63 * 1) The number of input arguments as defined by the method/object in the
29a241cc 64 * ASL is validated against the ACPI specification.
e8707b34 65 * 2) The type of the return object (if any) is validated against the ACPI
29a241cc 66 * specification.
e8707b34 67 * 3) For returned package objects, the count of package elements is
29a241cc
BM
68 * validated, as well as the type of each package element. Nested
69 * packages are supported.
e8707b34
BM
70 *
71 * For any problems found, a warning message is issued.
72 *
73 ******************************************************************************/
74/* Local prototypes */
e8707b34 75static acpi_status
29a241cc 76acpi_ns_check_reference(struct acpi_evaluate_info *info,
e8707b34
BM
77 union acpi_operand_object *return_object);
78
d5a36100
BM
79static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object);
80
e8707b34
BM
81/*******************************************************************************
82 *
29a241cc 83 * FUNCTION: acpi_ns_check_return_value
e8707b34 84 *
ba494bee 85 * PARAMETERS: node - Namespace node for the method/object
29a241cc 86 * info - Method execution information block
0444e8f6
BM
87 * user_param_count - Number of parameters actually passed
88 * return_status - Status from the object evaluation
a647b5c3
BM
89 * return_object_ptr - Pointer to the object returned from the
90 * evaluation of a method or object
e8707b34
BM
91 *
92 * RETURN: Status
93 *
29a241cc 94 * DESCRIPTION: Check the value returned from a predefined name.
e8707b34
BM
95 *
96 ******************************************************************************/
97
98acpi_status
29a241cc
BM
99acpi_ns_check_return_value(struct acpi_namespace_node *node,
100 struct acpi_evaluate_info *info,
101 u32 user_param_count,
102 acpi_status return_status,
103 union acpi_operand_object **return_object_ptr)
e8707b34 104{
29a241cc 105 acpi_status status;
e8707b34 106 const union acpi_predefined_info *predefined;
eeb4437e
BM
107
108 /* If not a predefined name, we cannot validate the return object */
109
43e5318f 110 predefined = info->predefined;
eeb4437e 111 if (!predefined) {
29a241cc 112 return (AE_OK);
eeb4437e
BM
113 }
114
115 /*
0444e8f6
BM
116 * If the method failed or did not actually return an object, we cannot
117 * validate the return object
e8707b34 118 */
0444e8f6 119 if ((return_status != AE_OK) && (return_status != AE_CTRL_RETURN_VALUE)) {
29a241cc 120 return (AE_OK);
eeb4437e
BM
121 }
122
e8707b34 123 /*
d57b23ad 124 * Return value validation and possible repair.
307a0424 125 *
d57b23ad
BM
126 * 1) Don't perform return value validation/repair if this feature
127 * has been disabled via a global option.
128 *
129 * 2) We have a return value, but if one wasn't expected, just exit,
130 * this is not a problem. For example, if the "Implicit Return"
131 * feature is enabled, methods will always return a value.
132 *
133 * 3) If the return value can be of any type, then we cannot perform
134 * any validation, just exit.
e8707b34 135 */
d57b23ad
BM
136 if (acpi_gbl_disable_auto_repair ||
137 (!predefined->info.expected_btypes) ||
307a0424 138 (predefined->info.expected_btypes == ACPI_RTYPE_ALL)) {
29a241cc 139 return (AE_OK);
e8707b34
BM
140 }
141
142 /*
465da9eb
BM
143 * Check that the type of the main return object is what is expected
144 * for this predefined name
e8707b34 145 */
29a241cc 146 status = acpi_ns_check_object_type(info, return_object_ptr,
e8707b34 147 predefined->info.expected_btypes,
0444e8f6 148 ACPI_NOT_PACKAGE_ELEMENT);
465da9eb
BM
149 if (ACPI_FAILURE(status)) {
150 goto exit;
151 }
152
153 /*
154 * For returned Package objects, check the type of all sub-objects.
155 * Note: Package may have been newly created by call above.
156 */
157 if ((*return_object_ptr)->common.type == ACPI_TYPE_PACKAGE) {
29a241cc
BM
158 info->parent_package = *return_object_ptr;
159 status = acpi_ns_check_package(info, return_object_ptr);
465da9eb
BM
160 if (ACPI_FAILURE(status)) {
161 goto exit;
34c39c75 162 }
e8707b34
BM
163 }
164
ad5babee 165 /*
465da9eb
BM
166 * The return object was OK, or it was successfully repaired above.
167 * Now make some additional checks such as verifying that package
168 * objects are sorted correctly (if required) or buffer objects have
169 * the correct data width (bytes vs. dwords). These repairs are
170 * performed on a per-name basis, i.e., the code is specific to
171 * particular predefined names.
ad5babee 172 */
29a241cc 173 status = acpi_ns_complex_repairs(info, node, status, return_object_ptr);
ad5babee 174
465da9eb 175exit:
0444e8f6
BM
176 /*
177 * If the object validation failed or if we successfully repaired one
178 * or more objects, mark the parent node to suppress further warning
179 * messages during the next evaluation of the same method/object.
180 */
29a241cc 181 if (ACPI_FAILURE(status) || (info->return_flags & ACPI_OBJECT_REPAIRED)) {
0444e8f6
BM
182 node->flags |= ANOBJ_EVALUATED;
183 }
0444e8f6 184
e8707b34
BM
185 return (status);
186}
187
e8707b34
BM
188/*******************************************************************************
189 *
190 * FUNCTION: acpi_ns_check_object_type
191 *
29a241cc 192 * PARAMETERS: info - Method execution information block
a647b5c3
BM
193 * return_object_ptr - Pointer to the object returned from the
194 * evaluation of a method or object
e8707b34
BM
195 * expected_btypes - Bitmap of expected return type(s)
196 * package_index - Index of object within parent package (if
0444e8f6
BM
197 * applicable - ACPI_NOT_PACKAGE_ELEMENT
198 * otherwise)
e8707b34
BM
199 *
200 * RETURN: Status
201 *
202 * DESCRIPTION: Check the type of the return object against the expected object
203 * type(s). Use of Btype allows multiple expected object types.
204 *
205 ******************************************************************************/
206
42f8fb75 207acpi_status
29a241cc 208acpi_ns_check_object_type(struct acpi_evaluate_info *info,
a647b5c3 209 union acpi_operand_object **return_object_ptr,
e8707b34
BM
210 u32 expected_btypes, u32 package_index)
211{
a647b5c3 212 union acpi_operand_object *return_object = *return_object_ptr;
e8707b34 213 acpi_status status = AE_OK;
e8707b34 214 char type_buffer[48]; /* Room for 5 types */
e8707b34 215
e8707b34
BM
216 /* A Namespace node should not get here, but make sure */
217
d5a36100
BM
218 if (return_object &&
219 ACPI_GET_DESCRIPTOR_TYPE(return_object) == ACPI_DESC_TYPE_NAMED) {
29a241cc
BM
220 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
221 info->node_flags,
0444e8f6
BM
222 "Invalid return type - Found a Namespace node [%4.4s] type %s",
223 return_object->node.name.ascii,
224 acpi_ut_get_type_name(return_object->node.
225 type)));
e8707b34
BM
226 return (AE_AML_OPERAND_TYPE);
227 }
228
229 /*
230 * Convert the object type (ACPI_TYPE_xxx) to a bitmapped object type.
231 * The bitmapped type allows multiple possible return types.
232 *
233 * Note, the cases below must handle all of the possible types returned
234 * from all of the predefined names (including elements of returned
235 * packages)
236 */
29a241cc
BM
237 info->return_btype = acpi_ns_get_bitmapped_type(return_object);
238 if (info->return_btype == ACPI_RTYPE_ANY) {
e8707b34 239
e8707b34 240 /* Not one of the supported objects, must be incorrect */
e8707b34
BM
241 goto type_error_exit;
242 }
243
d5a36100 244 /* For reference objects, check that the reference type is correct */
2147d3f0 245
29a241cc
BM
246 if ((info->return_btype & expected_btypes) == ACPI_RTYPE_REFERENCE) {
247 status = acpi_ns_check_reference(info, return_object);
2147d3f0 248 return (status);
e8707b34
BM
249 }
250
d5a36100 251 /* Attempt simple repair of the returned object if necessary */
e8707b34 252
29a241cc 253 status = acpi_ns_simple_repair(info, expected_btypes,
2147d3f0 254 package_index, return_object_ptr);
29a241cc
BM
255 if (ACPI_SUCCESS(status)) {
256 return (AE_OK); /* Successful repair */
257 }
e8707b34 258
e8707b34
BM
259 type_error_exit:
260
261 /* Create a string with all expected types for this predefined object */
262
c34c82bc 263 acpi_ut_get_expected_return_types(type_buffer, expected_btypes);
e8707b34 264
0444e8f6 265 if (package_index == ACPI_NOT_PACKAGE_ELEMENT) {
29a241cc
BM
266 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
267 info->node_flags,
0444e8f6
BM
268 "Return type mismatch - found %s, expected %s",
269 acpi_ut_get_object_type_name
270 (return_object), type_buffer));
e8707b34 271 } else {
29a241cc
BM
272 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname,
273 info->node_flags,
0444e8f6
BM
274 "Return Package type mismatch at index %u - "
275 "found %s, expected %s", package_index,
276 acpi_ut_get_object_type_name
277 (return_object), type_buffer));
e8707b34
BM
278 }
279
280 return (AE_AML_OPERAND_TYPE);
281}
282
283/*******************************************************************************
284 *
285 * FUNCTION: acpi_ns_check_reference
286 *
29a241cc 287 * PARAMETERS: info - Method execution information block
e8707b34
BM
288 * return_object - Object returned from the evaluation of a
289 * method or object
290 *
291 * RETURN: Status
292 *
293 * DESCRIPTION: Check a returned reference object for the correct reference
294 * type. The only reference type that can be returned from a
295 * predefined method is a named reference. All others are invalid.
296 *
297 ******************************************************************************/
298
299static acpi_status
29a241cc 300acpi_ns_check_reference(struct acpi_evaluate_info *info,
e8707b34
BM
301 union acpi_operand_object *return_object)
302{
303
304 /*
305 * Check the reference object for the correct reference type (opcode).
306 * The only type of reference that can be converted to an union acpi_object is
307 * a reference to a named object (reference class: NAME)
308 */
309 if (return_object->reference.class == ACPI_REFCLASS_NAME) {
310 return (AE_OK);
311 }
312
29a241cc 313 ACPI_WARN_PREDEFINED((AE_INFO, info->full_pathname, info->node_flags,
0444e8f6
BM
314 "Return type mismatch - unexpected reference object type [%s] %2.2X",
315 acpi_ut_get_reference_name(return_object),
316 return_object->reference.class));
e8707b34
BM
317
318 return (AE_AML_OPERAND_TYPE);
319}
a647b5c3 320
d5a36100
BM
321/*******************************************************************************
322 *
323 * FUNCTION: acpi_ns_get_bitmapped_type
324 *
325 * PARAMETERS: return_object - Object returned from method/obj evaluation
326 *
327 * RETURN: Object return type. ACPI_RTYPE_ANY indicates that the object
328 * type is not supported. ACPI_RTYPE_NONE indicates that no
329 * object was returned (return_object is NULL).
330 *
331 * DESCRIPTION: Convert object type into a bitmapped object return type.
332 *
333 ******************************************************************************/
334
335static u32 acpi_ns_get_bitmapped_type(union acpi_operand_object *return_object)
336{
337 u32 return_btype;
338
339 if (!return_object) {
340 return (ACPI_RTYPE_NONE);
341 }
342
343 /* Map acpi_object_type to internal bitmapped type */
344
345 switch (return_object->common.type) {
346 case ACPI_TYPE_INTEGER:
347 return_btype = ACPI_RTYPE_INTEGER;
348 break;
349
350 case ACPI_TYPE_BUFFER:
351 return_btype = ACPI_RTYPE_BUFFER;
352 break;
353
354 case ACPI_TYPE_STRING:
355 return_btype = ACPI_RTYPE_STRING;
356 break;
357
358 case ACPI_TYPE_PACKAGE:
359 return_btype = ACPI_RTYPE_PACKAGE;
360 break;
361
362 case ACPI_TYPE_LOCAL_REFERENCE:
363 return_btype = ACPI_RTYPE_REFERENCE;
364 break;
365
366 default:
367 /* Not one of the supported objects, must be incorrect */
368
369 return_btype = ACPI_RTYPE_ANY;
370 break;
371 }
372
373 return (return_btype);
374}