1 /******************************************************************************
3 * Module Name: nsrepair2 - Repair for objects returned by specific
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2009, Intel Corp.
10 * All rights reserved.
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
15 * 1. Redistributions of source code must retain the above copyright
16 * notice, this list of conditions, and the following disclaimer,
17 * without modification.
18 * 2. Redistributions in binary form must reproduce at minimum a disclaimer
19 * substantially similar to the "NO WARRANTY" disclaimer below
20 * ("Disclaimer") and any redistribution must be conditioned upon
21 * including a substantially similar Disclaimer requirement for further
22 * binary redistribution.
23 * 3. Neither the names of the above-listed copyright holders nor the names
24 * of any contributors may be used to endorse or promote products derived
25 * from this software without specific prior written permission.
27 * Alternatively, this software may be distributed under the terms of the
28 * GNU General Public License ("GPL") version 2 as published by the Free
29 * Software Foundation.
32 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
35 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36 * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
38 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
39 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
40 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
41 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
42 * POSSIBILITY OF SUCH DAMAGES.
45 #include <acpi/acpi.h>
49 #define _COMPONENT ACPI_NAMESPACE
50 ACPI_MODULE_NAME("nsrepair2")
53 * Information structure and handler for ACPI predefined names that can
54 * be repaired on a per-name basis.
57 acpi_status(*acpi_repair_function) (struct acpi_predefined_data *data,
58 union acpi_operand_object **return_object_ptr);
60 typedef struct acpi_repair_info {
61 char name[ACPI_NAME_SIZE];
62 acpi_repair_function repair_function;
66 /* Local prototypes */
68 static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
73 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
74 union acpi_operand_object **return_object_ptr);
77 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
78 union acpi_operand_object **return_object_ptr);
81 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
82 union acpi_operand_object **return_object_ptr);
85 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
86 union acpi_operand_object *return_object,
89 u8 sort_direction, char *sort_key_name);
92 acpi_ns_remove_null_elements(union acpi_operand_object *package);
95 acpi_ns_sort_list(union acpi_operand_object **elements,
96 u32 count, u32 index, u8 sort_direction);
98 /* Values for sort_direction above */
100 #define ACPI_SORT_ASCENDING 0
101 #define ACPI_SORT_DESCENDING 1
104 * This table contains the names of the predefined methods for which we can
105 * perform more complex repairs.
107 * _ALR: Sort the list ascending by ambient_illuminance if necessary
108 * _PSS: Sort the list descending by Power if necessary
109 * _TSS: Sort the list descending by Power if necessary
111 static const struct acpi_repair_info acpi_ns_repairable_names[] = {
112 {"_ALR", acpi_ns_repair_ALR},
113 {"_PSS", acpi_ns_repair_PSS},
114 {"_TSS", acpi_ns_repair_TSS},
115 {{0, 0, 0, 0}, NULL} /* Table terminator */
118 /******************************************************************************
120 * FUNCTION: acpi_ns_complex_repairs
122 * PARAMETERS: Data - Pointer to validation data structure
123 * Node - Namespace node for the method/object
124 * validate_status - Original status of earlier validation
125 * return_object_ptr - Pointer to the object returned from the
126 * evaluation of a method or object
128 * RETURN: Status. AE_OK if repair was successful. If name is not
129 * matched, validate_status is returned.
131 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
134 *****************************************************************************/
137 acpi_ns_complex_repairs(struct acpi_predefined_data *data,
138 struct acpi_namespace_node *node,
139 acpi_status validate_status,
140 union acpi_operand_object **return_object_ptr)
142 const struct acpi_repair_info *predefined;
145 /* Check if this name is in the list of repairable names */
147 predefined = acpi_ns_match_repairable_name(node);
149 return (validate_status);
152 status = predefined->repair_function(data, return_object_ptr);
156 /******************************************************************************
158 * FUNCTION: acpi_ns_match_repairable_name
160 * PARAMETERS: Node - Namespace node for the method/object
162 * RETURN: Pointer to entry in repair table. NULL indicates not found.
164 * DESCRIPTION: Check an object name against the repairable object list.
166 *****************************************************************************/
168 static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
172 const struct acpi_repair_info *this_name;
174 /* Search info table for a repairable predefined method/object name */
176 this_name = acpi_ns_repairable_names;
177 while (this_name->repair_function) {
178 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
184 return (NULL); /* Not found */
187 /******************************************************************************
189 * FUNCTION: acpi_ns_repair_ALR
191 * PARAMETERS: Data - Pointer to validation data structure
192 * return_object_ptr - Pointer to the object returned from the
193 * evaluation of a method or object
195 * RETURN: Status. AE_OK if object is OK or was repaired successfully
197 * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
198 * ascending by the ambient illuminance values.
200 *****************************************************************************/
203 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
204 union acpi_operand_object **return_object_ptr)
206 union acpi_operand_object *return_object = *return_object_ptr;
209 status = acpi_ns_check_sorted_list(data, return_object, 2, 1,
211 "AmbientIlluminance");
216 /******************************************************************************
218 * FUNCTION: acpi_ns_repair_TSS
220 * PARAMETERS: Data - Pointer to validation data structure
221 * return_object_ptr - Pointer to the object returned from the
222 * evaluation of a method or object
224 * RETURN: Status. AE_OK if object is OK or was repaired successfully
226 * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
227 * descending by the power dissipation values.
229 *****************************************************************************/
232 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
233 union acpi_operand_object **return_object_ptr)
235 union acpi_operand_object *return_object = *return_object_ptr;
238 status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
239 ACPI_SORT_DESCENDING,
245 /******************************************************************************
247 * FUNCTION: acpi_ns_repair_PSS
249 * PARAMETERS: Data - Pointer to validation data structure
250 * return_object_ptr - Pointer to the object returned from the
251 * evaluation of a method or object
253 * RETURN: Status. AE_OK if object is OK or was repaired successfully
255 * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
256 * by the CPU frequencies. Check that the power dissipation values
257 * are all proportional to CPU frequency (i.e., sorting by
258 * frequency should be the same as sorting by power.)
260 *****************************************************************************/
263 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
264 union acpi_operand_object **return_object_ptr)
266 union acpi_operand_object *return_object = *return_object_ptr;
267 union acpi_operand_object **outer_elements;
268 u32 outer_element_count;
269 union acpi_operand_object **elements;
270 union acpi_operand_object *obj_desc;
276 * Entries (sub-packages) in the _PSS Package must be sorted by power
277 * dissipation, in descending order. If it appears that the list is
278 * incorrectly sorted, sort it. We sort by cpu_frequency, since this
279 * should be proportional to the power.
281 status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
282 ACPI_SORT_DESCENDING,
284 if (ACPI_FAILURE(status)) {
289 * We now know the list is correctly sorted by CPU frequency. Check if
290 * the power dissipation values are proportional.
292 previous_value = ACPI_UINT32_MAX;
293 outer_elements = return_object->package.elements;
294 outer_element_count = return_object->package.count;
296 for (i = 0; i < outer_element_count; i++) {
297 elements = (*outer_elements)->package.elements;
298 obj_desc = elements[1]; /* Index1 = power_dissipation */
300 if ((u32) obj_desc->integer.value > previous_value) {
301 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
303 "SubPackage[%u,%u] - suspicious power dissipation values",
307 previous_value = (u32) obj_desc->integer.value;
314 /******************************************************************************
316 * FUNCTION: acpi_ns_check_sorted_list
318 * PARAMETERS: Data - Pointer to validation data structure
319 * return_object - Pointer to the top-level returned object
320 * expected_count - Minimum length of each sub-package
321 * sort_index - Sub-package entry to sort on
322 * sort_direction - Ascending or descending
323 * sort_key_name - Name of the sort_index field
325 * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
326 * has been repaired by sorting the list.
328 * DESCRIPTION: Check if the package list is valid and sorted correctly by the
329 * sort_index. If not, then sort the list.
331 *****************************************************************************/
334 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
335 union acpi_operand_object *return_object,
338 u8 sort_direction, char *sort_key_name)
340 u32 outer_element_count;
341 union acpi_operand_object **outer_elements;
342 union acpi_operand_object **elements;
343 union acpi_operand_object *obj_desc;
348 /* The top-level object must be a package */
350 if (return_object->common.type != ACPI_TYPE_PACKAGE) {
351 return (AE_AML_OPERAND_TYPE);
355 * Detect any NULL package elements and remove them from the
358 * TBD: We may want to do this for all predefined names that
359 * return a variable-length package of packages.
361 status = acpi_ns_remove_null_elements(return_object);
362 if (status == AE_NULL_ENTRY) {
363 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
364 "NULL elements removed from package"));
366 /* Exit if package is now zero length */
368 if (!return_object->package.count) {
369 return (AE_NULL_ENTRY);
373 outer_elements = return_object->package.elements;
374 outer_element_count = return_object->package.count;
375 if (!outer_element_count) {
376 return (AE_AML_PACKAGE_LIMIT);
380 if (sort_direction == ACPI_SORT_DESCENDING) {
381 previous_value = ACPI_UINT32_MAX;
384 /* Examine each subpackage */
386 for (i = 0; i < outer_element_count; i++) {
388 /* Each element of the top-level package must also be a package */
390 if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
391 return (AE_AML_OPERAND_TYPE);
394 /* Each sub-package must have the minimum length */
396 if ((*outer_elements)->package.count < expected_count) {
397 return (AE_AML_PACKAGE_LIMIT);
400 elements = (*outer_elements)->package.elements;
401 obj_desc = elements[sort_index];
403 if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
404 return (AE_AML_OPERAND_TYPE);
408 * The list must be sorted in the specified order. If we detect a
409 * discrepancy, issue a warning and sort the entire list
411 if (((sort_direction == ACPI_SORT_ASCENDING) &&
412 (obj_desc->integer.value < previous_value)) ||
413 ((sort_direction == ACPI_SORT_DESCENDING) &&
414 (obj_desc->integer.value > previous_value))) {
416 acpi_ns_sort_list(return_object->package.elements,
417 outer_element_count, sort_index,
419 if (ACPI_FAILURE(status)) {
423 data->flags |= ACPI_OBJECT_REPAIRED;
425 ACPI_INFO_PREDEFINED((AE_INFO, data->pathname,
427 "Repaired unsorted list - now sorted by %s",
432 previous_value = (u32) obj_desc->integer.value;
439 /******************************************************************************
441 * FUNCTION: acpi_ns_remove_null_elements
443 * PARAMETERS: obj_desc - A Package object
445 * RETURN: Status. AE_NULL_ENTRY means that one or more elements were
448 * DESCRIPTION: Remove all NULL package elements and update the package count.
450 *****************************************************************************/
453 acpi_ns_remove_null_elements(union acpi_operand_object *obj_desc)
455 union acpi_operand_object **source;
456 union acpi_operand_object **dest;
457 acpi_status status = AE_OK;
462 count = obj_desc->package.count;
465 source = obj_desc->package.elements;
468 /* Examine all elements of the package object */
470 for (i = 0; i < count; i++) {
472 status = AE_NULL_ENTRY;
481 if (status == AE_NULL_ENTRY) {
483 /* NULL terminate list and update the package count */
486 obj_desc->package.count = new_count;
492 /******************************************************************************
494 * FUNCTION: acpi_ns_sort_list
496 * PARAMETERS: Elements - Package object element list
497 * Count - Element count for above
498 * Index - Sort by which package element
499 * sort_direction - Ascending or Descending sort
503 * DESCRIPTION: Sort the objects that are in a package element list.
505 * NOTE: Assumes that all NULL elements have been removed from the package.
507 *****************************************************************************/
510 acpi_ns_sort_list(union acpi_operand_object **elements,
511 u32 count, u32 index, u8 sort_direction)
513 union acpi_operand_object *obj_desc1;
514 union acpi_operand_object *obj_desc2;
515 union acpi_operand_object *temp_obj;
519 /* Simple bubble sort */
521 for (i = 1; i < count; i++) {
522 for (j = (count - 1); j >= i; j--) {
523 obj_desc1 = elements[j - 1]->package.elements[index];
524 obj_desc2 = elements[j]->package.elements[index];
526 if (((sort_direction == ACPI_SORT_ASCENDING) &&
527 (obj_desc1->integer.value >
528 obj_desc2->integer.value))
529 || ((sort_direction == ACPI_SORT_DESCENDING)
530 && (obj_desc1->integer.value <
531 obj_desc2->integer.value))) {
532 temp_obj = elements[j - 1];
533 elements[j - 1] = elements[j];
534 elements[j] = temp_obj;