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_FDE(struct acpi_predefined_data *data,
78 union acpi_operand_object **return_object_ptr);
81 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
82 union acpi_operand_object **return_object_ptr);
85 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
86 union acpi_operand_object **return_object_ptr);
89 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
90 union acpi_operand_object *return_object,
93 u8 sort_direction, char *sort_key_name);
96 acpi_ns_remove_null_elements(union acpi_operand_object *package);
99 acpi_ns_sort_list(union acpi_operand_object **elements,
100 u32 count, u32 index, u8 sort_direction);
102 /* Values for sort_direction above */
104 #define ACPI_SORT_ASCENDING 0
105 #define ACPI_SORT_DESCENDING 1
108 * This table contains the names of the predefined methods for which we can
109 * perform more complex repairs.
113 * _ALR: Sort the list ascending by ambient_illuminance
114 * _FDE: Convert a Package or Buffer of BYTEs to a Buffer of DWORDs
115 * _GTM: Convert a Package or Buffer of BYTEs to a Buffer of DWORDs
116 * _PSS: Sort the list descending by Power
117 * _TSS: Sort the list descending by Power
119 static const struct acpi_repair_info acpi_ns_repairable_names[] = {
120 {"_ALR", acpi_ns_repair_ALR},
121 {"_FDE", acpi_ns_repair_FDE},
122 {"_GTM", acpi_ns_repair_FDE}, /* _GTM has same repair as _FDE */
123 {"_PSS", acpi_ns_repair_PSS},
124 {"_TSS", acpi_ns_repair_TSS},
125 {{0, 0, 0, 0}, NULL} /* Table terminator */
128 #define ACPI_FDE_FIELD_COUNT 5
129 #define ACPI_FDE_BYTE_BUFFER_SIZE 5
130 #define ACPI_FDE_DWORD_BUFFER_SIZE (ACPI_FDE_FIELD_COUNT * sizeof (u32))
132 /******************************************************************************
134 * FUNCTION: acpi_ns_complex_repairs
136 * PARAMETERS: Data - Pointer to validation data structure
137 * Node - Namespace node for the method/object
138 * validate_status - Original status of earlier validation
139 * return_object_ptr - Pointer to the object returned from the
140 * evaluation of a method or object
142 * RETURN: Status. AE_OK if repair was successful. If name is not
143 * matched, validate_status is returned.
145 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
148 *****************************************************************************/
151 acpi_ns_complex_repairs(struct acpi_predefined_data *data,
152 struct acpi_namespace_node *node,
153 acpi_status validate_status,
154 union acpi_operand_object **return_object_ptr)
156 const struct acpi_repair_info *predefined;
159 /* Check if this name is in the list of repairable names */
161 predefined = acpi_ns_match_repairable_name(node);
163 return (validate_status);
166 status = predefined->repair_function(data, return_object_ptr);
170 /******************************************************************************
172 * FUNCTION: acpi_ns_match_repairable_name
174 * PARAMETERS: Node - Namespace node for the method/object
176 * RETURN: Pointer to entry in repair table. NULL indicates not found.
178 * DESCRIPTION: Check an object name against the repairable object list.
180 *****************************************************************************/
182 static const struct acpi_repair_info *acpi_ns_match_repairable_name(struct
186 const struct acpi_repair_info *this_name;
188 /* Search info table for a repairable predefined method/object name */
190 this_name = acpi_ns_repairable_names;
191 while (this_name->repair_function) {
192 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
198 return (NULL); /* Not found */
201 /******************************************************************************
203 * FUNCTION: acpi_ns_repair_ALR
205 * PARAMETERS: Data - Pointer to validation data structure
206 * return_object_ptr - Pointer to the object returned from the
207 * evaluation of a method or object
209 * RETURN: Status. AE_OK if object is OK or was repaired successfully
211 * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
212 * ascending by the ambient illuminance values.
214 *****************************************************************************/
217 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
218 union acpi_operand_object **return_object_ptr)
220 union acpi_operand_object *return_object = *return_object_ptr;
223 status = acpi_ns_check_sorted_list(data, return_object, 2, 1,
225 "AmbientIlluminance");
230 /******************************************************************************
232 * FUNCTION: acpi_ns_repair_FDE
234 * PARAMETERS: Data - Pointer to validation data structure
235 * return_object_ptr - Pointer to the object returned from the
236 * evaluation of a method or object
238 * RETURN: Status. AE_OK if object is OK or was repaired successfully
240 * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
241 * value is a Buffer of 5 DWORDs. This function repairs two
243 * 1) The return value is a Buffer of BYTEs, not DWORDs
244 * 2) The return value is a Package of Integer objects
246 *****************************************************************************/
249 acpi_ns_repair_FDE(struct acpi_predefined_data *data,
250 union acpi_operand_object **return_object_ptr)
252 union acpi_operand_object *return_object = *return_object_ptr;
253 union acpi_operand_object *buffer_object;
258 ACPI_FUNCTION_NAME(ns_repair_FDE);
260 switch (return_object->common.type) {
261 case ACPI_TYPE_BUFFER:
263 /* This is the expected type. Length should be (at least) 5 DWORDs */
265 if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
269 /* We can only repair if we have exactly 5 BYTEs */
271 if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
272 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
274 "Incorrect return buffer length %u, expected %u",
275 return_object->buffer.length,
276 ACPI_FDE_DWORD_BUFFER_SIZE));
278 return (AE_AML_OPERAND_TYPE);
281 /* Create the new (larger) buffer object */
284 acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
285 if (!buffer_object) {
286 return (AE_NO_MEMORY);
289 /* Expand each byte to a DWORD */
291 byte_buffer = return_object->buffer.pointer;
293 ACPI_CAST_PTR(u32, buffer_object->buffer.pointer);
295 for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
296 *dword_buffer = (u32) *byte_buffer;
301 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
302 "%s Expanded Byte Buffer to expected DWord Buffer\n",
307 return (AE_AML_OPERAND_TYPE);
310 /* Delete the original return object, return the new buffer object */
312 acpi_ut_remove_reference(return_object);
313 *return_object_ptr = buffer_object;
315 data->flags |= ACPI_OBJECT_REPAIRED;
319 /******************************************************************************
321 * FUNCTION: acpi_ns_repair_TSS
323 * PARAMETERS: Data - Pointer to validation data structure
324 * return_object_ptr - Pointer to the object returned from the
325 * evaluation of a method or object
327 * RETURN: Status. AE_OK if object is OK or was repaired successfully
329 * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
330 * descending by the power dissipation values.
332 *****************************************************************************/
335 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
336 union acpi_operand_object **return_object_ptr)
338 union acpi_operand_object *return_object = *return_object_ptr;
341 status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
342 ACPI_SORT_DESCENDING,
348 /******************************************************************************
350 * FUNCTION: acpi_ns_repair_PSS
352 * PARAMETERS: Data - Pointer to validation data structure
353 * return_object_ptr - Pointer to the object returned from the
354 * evaluation of a method or object
356 * RETURN: Status. AE_OK if object is OK or was repaired successfully
358 * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
359 * by the CPU frequencies. Check that the power dissipation values
360 * are all proportional to CPU frequency (i.e., sorting by
361 * frequency should be the same as sorting by power.)
363 *****************************************************************************/
366 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
367 union acpi_operand_object **return_object_ptr)
369 union acpi_operand_object *return_object = *return_object_ptr;
370 union acpi_operand_object **outer_elements;
371 u32 outer_element_count;
372 union acpi_operand_object **elements;
373 union acpi_operand_object *obj_desc;
379 * Entries (sub-packages) in the _PSS Package must be sorted by power
380 * dissipation, in descending order. If it appears that the list is
381 * incorrectly sorted, sort it. We sort by cpu_frequency, since this
382 * should be proportional to the power.
384 status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
385 ACPI_SORT_DESCENDING,
387 if (ACPI_FAILURE(status)) {
392 * We now know the list is correctly sorted by CPU frequency. Check if
393 * the power dissipation values are proportional.
395 previous_value = ACPI_UINT32_MAX;
396 outer_elements = return_object->package.elements;
397 outer_element_count = return_object->package.count;
399 for (i = 0; i < outer_element_count; i++) {
400 elements = (*outer_elements)->package.elements;
401 obj_desc = elements[1]; /* Index1 = power_dissipation */
403 if ((u32) obj_desc->integer.value > previous_value) {
404 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
406 "SubPackage[%u,%u] - suspicious power dissipation values",
410 previous_value = (u32) obj_desc->integer.value;
417 /******************************************************************************
419 * FUNCTION: acpi_ns_check_sorted_list
421 * PARAMETERS: Data - Pointer to validation data structure
422 * return_object - Pointer to the top-level returned object
423 * expected_count - Minimum length of each sub-package
424 * sort_index - Sub-package entry to sort on
425 * sort_direction - Ascending or descending
426 * sort_key_name - Name of the sort_index field
428 * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
429 * has been repaired by sorting the list.
431 * DESCRIPTION: Check if the package list is valid and sorted correctly by the
432 * sort_index. If not, then sort the list.
434 *****************************************************************************/
437 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
438 union acpi_operand_object *return_object,
441 u8 sort_direction, char *sort_key_name)
443 u32 outer_element_count;
444 union acpi_operand_object **outer_elements;
445 union acpi_operand_object **elements;
446 union acpi_operand_object *obj_desc;
451 ACPI_FUNCTION_NAME(ns_check_sorted_list);
453 /* The top-level object must be a package */
455 if (return_object->common.type != ACPI_TYPE_PACKAGE) {
456 return (AE_AML_OPERAND_TYPE);
460 * Detect any NULL package elements and remove them from the
463 * TBD: We may want to do this for all predefined names that
464 * return a variable-length package of packages.
466 status = acpi_ns_remove_null_elements(return_object);
467 if (status == AE_NULL_ENTRY) {
468 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
469 "%s: NULL elements removed from package\n",
472 /* Exit if package is now zero length */
474 if (!return_object->package.count) {
475 return (AE_NULL_ENTRY);
479 outer_elements = return_object->package.elements;
480 outer_element_count = return_object->package.count;
481 if (!outer_element_count) {
482 return (AE_AML_PACKAGE_LIMIT);
486 if (sort_direction == ACPI_SORT_DESCENDING) {
487 previous_value = ACPI_UINT32_MAX;
490 /* Examine each subpackage */
492 for (i = 0; i < outer_element_count; i++) {
494 /* Each element of the top-level package must also be a package */
496 if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
497 return (AE_AML_OPERAND_TYPE);
500 /* Each sub-package must have the minimum length */
502 if ((*outer_elements)->package.count < expected_count) {
503 return (AE_AML_PACKAGE_LIMIT);
506 elements = (*outer_elements)->package.elements;
507 obj_desc = elements[sort_index];
509 if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
510 return (AE_AML_OPERAND_TYPE);
514 * The list must be sorted in the specified order. If we detect a
515 * discrepancy, issue a warning and sort the entire list
517 if (((sort_direction == ACPI_SORT_ASCENDING) &&
518 (obj_desc->integer.value < previous_value)) ||
519 ((sort_direction == ACPI_SORT_DESCENDING) &&
520 (obj_desc->integer.value > previous_value))) {
522 acpi_ns_sort_list(return_object->package.elements,
523 outer_element_count, sort_index,
525 if (ACPI_FAILURE(status)) {
529 data->flags |= ACPI_OBJECT_REPAIRED;
531 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
532 "%s: Repaired unsorted list - now sorted by %s\n",
533 data->pathname, sort_key_name));
537 previous_value = (u32) obj_desc->integer.value;
544 /******************************************************************************
546 * FUNCTION: acpi_ns_remove_null_elements
548 * PARAMETERS: obj_desc - A Package object
550 * RETURN: Status. AE_NULL_ENTRY means that one or more elements were
553 * DESCRIPTION: Remove all NULL package elements and update the package count.
555 *****************************************************************************/
558 acpi_ns_remove_null_elements(union acpi_operand_object *obj_desc)
560 union acpi_operand_object **source;
561 union acpi_operand_object **dest;
562 acpi_status status = AE_OK;
567 count = obj_desc->package.count;
570 source = obj_desc->package.elements;
573 /* Examine all elements of the package object */
575 for (i = 0; i < count; i++) {
577 status = AE_NULL_ENTRY;
586 if (status == AE_NULL_ENTRY) {
588 /* NULL terminate list and update the package count */
591 obj_desc->package.count = new_count;
597 /******************************************************************************
599 * FUNCTION: acpi_ns_sort_list
601 * PARAMETERS: Elements - Package object element list
602 * Count - Element count for above
603 * Index - Sort by which package element
604 * sort_direction - Ascending or Descending sort
608 * DESCRIPTION: Sort the objects that are in a package element list.
610 * NOTE: Assumes that all NULL elements have been removed from the package.
612 *****************************************************************************/
615 acpi_ns_sort_list(union acpi_operand_object **elements,
616 u32 count, u32 index, u8 sort_direction)
618 union acpi_operand_object *obj_desc1;
619 union acpi_operand_object *obj_desc2;
620 union acpi_operand_object *temp_obj;
624 /* Simple bubble sort */
626 for (i = 1; i < count; i++) {
627 for (j = (count - 1); j >= i; j--) {
628 obj_desc1 = elements[j - 1]->package.elements[index];
629 obj_desc2 = elements[j]->package.elements[index];
631 if (((sort_direction == ACPI_SORT_ASCENDING) &&
632 (obj_desc1->integer.value >
633 obj_desc2->integer.value))
634 || ((sort_direction == ACPI_SORT_DESCENDING)
635 && (obj_desc1->integer.value <
636 obj_desc2->integer.value))) {
637 temp_obj = elements[j - 1];
638 elements[j - 1] = elements[j];
639 elements[j] = temp_obj;