1 /******************************************************************************
3 * Module Name: nsrepair2 - Repair for objects returned by specific
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2013, 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
61 typedef struct acpi_repair_info {
62 char name[ACPI_NAME_SIZE];
63 acpi_repair_function repair_function;
67 /* Local prototypes */
69 static const struct acpi_repair_info *acpi_ns_match_complex_repair(struct
74 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
75 union acpi_operand_object **return_object_ptr);
78 acpi_ns_repair_CID(struct acpi_predefined_data *data,
79 union acpi_operand_object **return_object_ptr);
82 acpi_ns_repair_FDE(struct acpi_predefined_data *data,
83 union acpi_operand_object **return_object_ptr);
86 acpi_ns_repair_HID(struct acpi_predefined_data *data,
87 union acpi_operand_object **return_object_ptr);
90 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
91 union acpi_operand_object **return_object_ptr);
94 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
95 union acpi_operand_object **return_object_ptr);
98 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
99 union acpi_operand_object *return_object,
102 u8 sort_direction, char *sort_key_name);
105 acpi_ns_sort_list(union acpi_operand_object **elements,
106 u32 count, u32 index, u8 sort_direction);
108 /* Values for sort_direction above */
110 #define ACPI_SORT_ASCENDING 0
111 #define ACPI_SORT_DESCENDING 1
114 * This table contains the names of the predefined methods for which we can
115 * perform more complex repairs.
119 * _ALR: Sort the list ascending by ambient_illuminance
120 * _CID: Strings: uppercase all, remove any leading asterisk
121 * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
122 * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
123 * _HID: Strings: uppercase all, remove any leading asterisk
124 * _PSS: Sort the list descending by Power
125 * _TSS: Sort the list descending by Power
127 * Names that must be packages, but cannot be sorted:
129 * _BCL: Values are tied to the Package index where they appear, and cannot
130 * be moved or sorted. These index values are used for _BQC and _BCM.
131 * However, we can fix the case where a buffer is returned, by converting
132 * it to a Package of integers.
134 static const struct acpi_repair_info acpi_ns_repairable_names[] = {
135 {"_ALR", acpi_ns_repair_ALR},
136 {"_CID", acpi_ns_repair_CID},
137 {"_FDE", acpi_ns_repair_FDE},
138 {"_GTM", acpi_ns_repair_FDE}, /* _GTM has same repair as _FDE */
139 {"_HID", acpi_ns_repair_HID},
140 {"_PSS", acpi_ns_repair_PSS},
141 {"_TSS", acpi_ns_repair_TSS},
142 {{0, 0, 0, 0}, NULL} /* Table terminator */
145 #define ACPI_FDE_FIELD_COUNT 5
146 #define ACPI_FDE_BYTE_BUFFER_SIZE 5
147 #define ACPI_FDE_DWORD_BUFFER_SIZE (ACPI_FDE_FIELD_COUNT * sizeof (u32))
149 /******************************************************************************
151 * FUNCTION: acpi_ns_complex_repairs
153 * PARAMETERS: data - Pointer to validation data structure
154 * node - Namespace node for the method/object
155 * validate_status - Original status of earlier validation
156 * return_object_ptr - Pointer to the object returned from the
157 * evaluation of a method or object
159 * RETURN: Status. AE_OK if repair was successful. If name is not
160 * matched, validate_status is returned.
162 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
165 *****************************************************************************/
168 acpi_ns_complex_repairs(struct acpi_predefined_data *data,
169 struct acpi_namespace_node *node,
170 acpi_status validate_status,
171 union acpi_operand_object **return_object_ptr)
173 const struct acpi_repair_info *predefined;
176 /* Check if this name is in the list of repairable names */
178 predefined = acpi_ns_match_complex_repair(node);
180 return (validate_status);
183 status = predefined->repair_function(data, return_object_ptr);
187 /******************************************************************************
189 * FUNCTION: acpi_ns_match_complex_repair
191 * PARAMETERS: node - Namespace node for the method/object
193 * RETURN: Pointer to entry in repair table. NULL indicates not found.
195 * DESCRIPTION: Check an object name against the repairable object list.
197 *****************************************************************************/
199 static const struct acpi_repair_info *acpi_ns_match_complex_repair(struct
203 const struct acpi_repair_info *this_name;
205 /* Search info table for a repairable predefined method/object name */
207 this_name = acpi_ns_repairable_names;
208 while (this_name->repair_function) {
209 if (ACPI_COMPARE_NAME(node->name.ascii, this_name->name)) {
215 return (NULL); /* Not found */
218 /******************************************************************************
220 * FUNCTION: acpi_ns_repair_ALR
222 * PARAMETERS: data - Pointer to validation data structure
223 * return_object_ptr - Pointer to the object returned from the
224 * evaluation of a method or object
226 * RETURN: Status. AE_OK if object is OK or was repaired successfully
228 * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
229 * ascending by the ambient illuminance values.
231 *****************************************************************************/
234 acpi_ns_repair_ALR(struct acpi_predefined_data *data,
235 union acpi_operand_object **return_object_ptr)
237 union acpi_operand_object *return_object = *return_object_ptr;
240 status = acpi_ns_check_sorted_list(data, return_object, 2, 1,
242 "AmbientIlluminance");
247 /******************************************************************************
249 * FUNCTION: acpi_ns_repair_FDE
251 * PARAMETERS: data - Pointer to validation data structure
252 * return_object_ptr - Pointer to the object returned from the
253 * evaluation of a method or object
255 * RETURN: Status. AE_OK if object is OK or was repaired successfully
257 * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
258 * value is a Buffer of 5 DWORDs. This function repairs a common
259 * problem where the return value is a Buffer of BYTEs, not
262 *****************************************************************************/
265 acpi_ns_repair_FDE(struct acpi_predefined_data *data,
266 union acpi_operand_object **return_object_ptr)
268 union acpi_operand_object *return_object = *return_object_ptr;
269 union acpi_operand_object *buffer_object;
274 ACPI_FUNCTION_NAME(ns_repair_FDE);
276 switch (return_object->common.type) {
277 case ACPI_TYPE_BUFFER:
279 /* This is the expected type. Length should be (at least) 5 DWORDs */
281 if (return_object->buffer.length >= ACPI_FDE_DWORD_BUFFER_SIZE) {
285 /* We can only repair if we have exactly 5 BYTEs */
287 if (return_object->buffer.length != ACPI_FDE_BYTE_BUFFER_SIZE) {
288 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
290 "Incorrect return buffer length %u, expected %u",
291 return_object->buffer.length,
292 ACPI_FDE_DWORD_BUFFER_SIZE));
294 return (AE_AML_OPERAND_TYPE);
297 /* Create the new (larger) buffer object */
300 acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE);
301 if (!buffer_object) {
302 return (AE_NO_MEMORY);
305 /* Expand each byte to a DWORD */
307 byte_buffer = return_object->buffer.pointer;
309 ACPI_CAST_PTR(u32, buffer_object->buffer.pointer);
311 for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++) {
312 *dword_buffer = (u32) *byte_buffer;
317 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
318 "%s Expanded Byte Buffer to expected DWord Buffer\n",
323 return (AE_AML_OPERAND_TYPE);
326 /* Delete the original return object, return the new buffer object */
328 acpi_ut_remove_reference(return_object);
329 *return_object_ptr = buffer_object;
331 data->flags |= ACPI_OBJECT_REPAIRED;
335 /******************************************************************************
337 * FUNCTION: acpi_ns_repair_CID
339 * PARAMETERS: data - Pointer to validation data structure
340 * return_object_ptr - Pointer to the object returned from the
341 * evaluation of a method or object
343 * RETURN: Status. AE_OK if object is OK or was repaired successfully
345 * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
346 * letters are uppercase and that there is no leading asterisk.
347 * If a Package, ensure same for all string elements.
349 *****************************************************************************/
352 acpi_ns_repair_CID(struct acpi_predefined_data *data,
353 union acpi_operand_object **return_object_ptr)
356 union acpi_operand_object *return_object = *return_object_ptr;
357 union acpi_operand_object **element_ptr;
358 union acpi_operand_object *original_element;
359 u16 original_ref_count;
362 /* Check for _CID as a simple string */
364 if (return_object->common.type == ACPI_TYPE_STRING) {
365 status = acpi_ns_repair_HID(data, return_object_ptr);
369 /* Exit if not a Package */
371 if (return_object->common.type != ACPI_TYPE_PACKAGE) {
375 /* Examine each element of the _CID package */
377 element_ptr = return_object->package.elements;
378 for (i = 0; i < return_object->package.count; i++) {
379 original_element = *element_ptr;
380 original_ref_count = original_element->common.reference_count;
382 status = acpi_ns_repair_HID(data, element_ptr);
383 if (ACPI_FAILURE(status)) {
387 /* Take care with reference counts */
389 if (original_element != *element_ptr) {
391 /* Element was replaced */
393 (*element_ptr)->common.reference_count =
396 acpi_ut_remove_reference(original_element);
405 /******************************************************************************
407 * FUNCTION: acpi_ns_repair_HID
409 * PARAMETERS: data - Pointer to validation data structure
410 * return_object_ptr - Pointer to the object returned from the
411 * evaluation of a method or object
413 * RETURN: Status. AE_OK if object is OK or was repaired successfully
415 * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
416 * letters are uppercase and that there is no leading asterisk.
418 *****************************************************************************/
421 acpi_ns_repair_HID(struct acpi_predefined_data *data,
422 union acpi_operand_object **return_object_ptr)
424 union acpi_operand_object *return_object = *return_object_ptr;
425 union acpi_operand_object *new_string;
429 ACPI_FUNCTION_NAME(ns_repair_HID);
431 /* We only care about string _HID objects (not integers) */
433 if (return_object->common.type != ACPI_TYPE_STRING) {
437 if (return_object->string.length == 0) {
438 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname, data->node_flags,
439 "Invalid zero-length _HID or _CID string"));
441 /* Return AE_OK anyway, let driver handle it */
443 data->flags |= ACPI_OBJECT_REPAIRED;
447 /* It is simplest to always create a new string object */
449 new_string = acpi_ut_create_string_object(return_object->string.length);
451 return (AE_NO_MEMORY);
455 * Remove a leading asterisk if present. For some unknown reason, there
456 * are many machines in the field that contains IDs like this.
458 * Examples: "*PNP0C03", "*ACPI0003"
460 source = return_object->string.pointer;
461 if (*source == '*') {
463 new_string->string.length--;
465 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
466 "%s: Removed invalid leading asterisk\n",
471 * Copy and uppercase the string. From the ACPI 5.0 specification:
473 * A valid PNP ID must be of the form "AAA####" where A is an uppercase
474 * letter and # is a hex digit. A valid ACPI ID must be of the form
475 * "NNNN####" where N is an uppercase letter or decimal digit, and
478 for (dest = new_string->string.pointer; *source; dest++, source++) {
479 *dest = (char)ACPI_TOUPPER(*source);
482 acpi_ut_remove_reference(return_object);
483 *return_object_ptr = new_string;
487 /******************************************************************************
489 * FUNCTION: acpi_ns_repair_TSS
491 * PARAMETERS: data - Pointer to validation data structure
492 * return_object_ptr - Pointer to the object returned from the
493 * evaluation of a method or object
495 * RETURN: Status. AE_OK if object is OK or was repaired successfully
497 * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
498 * descending by the power dissipation values.
500 *****************************************************************************/
503 acpi_ns_repair_TSS(struct acpi_predefined_data *data,
504 union acpi_operand_object **return_object_ptr)
506 union acpi_operand_object *return_object = *return_object_ptr;
508 struct acpi_namespace_node *node;
511 * We can only sort the _TSS return package if there is no _PSS in the
512 * same scope. This is because if _PSS is present, the ACPI specification
513 * dictates that the _TSS Power Dissipation field is to be ignored, and
514 * therefore some BIOSs leave garbage values in the _TSS Power field(s).
515 * In this case, it is best to just return the _TSS package as-is.
519 acpi_ns_get_node(data->node, "^_PSS", ACPI_NS_NO_UPSEARCH, &node);
520 if (ACPI_SUCCESS(status)) {
524 status = acpi_ns_check_sorted_list(data, return_object, 5, 1,
525 ACPI_SORT_DESCENDING,
531 /******************************************************************************
533 * FUNCTION: acpi_ns_repair_PSS
535 * PARAMETERS: data - Pointer to validation data structure
536 * return_object_ptr - Pointer to the object returned from the
537 * evaluation of a method or object
539 * RETURN: Status. AE_OK if object is OK or was repaired successfully
541 * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
542 * by the CPU frequencies. Check that the power dissipation values
543 * are all proportional to CPU frequency (i.e., sorting by
544 * frequency should be the same as sorting by power.)
546 *****************************************************************************/
549 acpi_ns_repair_PSS(struct acpi_predefined_data *data,
550 union acpi_operand_object **return_object_ptr)
552 union acpi_operand_object *return_object = *return_object_ptr;
553 union acpi_operand_object **outer_elements;
554 u32 outer_element_count;
555 union acpi_operand_object **elements;
556 union acpi_operand_object *obj_desc;
562 * Entries (sub-packages) in the _PSS Package must be sorted by power
563 * dissipation, in descending order. If it appears that the list is
564 * incorrectly sorted, sort it. We sort by cpu_frequency, since this
565 * should be proportional to the power.
567 status = acpi_ns_check_sorted_list(data, return_object, 6, 0,
568 ACPI_SORT_DESCENDING,
570 if (ACPI_FAILURE(status)) {
575 * We now know the list is correctly sorted by CPU frequency. Check if
576 * the power dissipation values are proportional.
578 previous_value = ACPI_UINT32_MAX;
579 outer_elements = return_object->package.elements;
580 outer_element_count = return_object->package.count;
582 for (i = 0; i < outer_element_count; i++) {
583 elements = (*outer_elements)->package.elements;
584 obj_desc = elements[1]; /* Index1 = power_dissipation */
586 if ((u32) obj_desc->integer.value > previous_value) {
587 ACPI_WARN_PREDEFINED((AE_INFO, data->pathname,
589 "SubPackage[%u,%u] - suspicious power dissipation values",
593 previous_value = (u32) obj_desc->integer.value;
600 /******************************************************************************
602 * FUNCTION: acpi_ns_check_sorted_list
604 * PARAMETERS: data - Pointer to validation data structure
605 * return_object - Pointer to the top-level returned object
606 * expected_count - Minimum length of each sub-package
607 * sort_index - Sub-package entry to sort on
608 * sort_direction - Ascending or descending
609 * sort_key_name - Name of the sort_index field
611 * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
612 * has been repaired by sorting the list.
614 * DESCRIPTION: Check if the package list is valid and sorted correctly by the
615 * sort_index. If not, then sort the list.
617 *****************************************************************************/
620 acpi_ns_check_sorted_list(struct acpi_predefined_data *data,
621 union acpi_operand_object *return_object,
624 u8 sort_direction, char *sort_key_name)
626 u32 outer_element_count;
627 union acpi_operand_object **outer_elements;
628 union acpi_operand_object **elements;
629 union acpi_operand_object *obj_desc;
633 ACPI_FUNCTION_NAME(ns_check_sorted_list);
635 /* The top-level object must be a package */
637 if (return_object->common.type != ACPI_TYPE_PACKAGE) {
638 return (AE_AML_OPERAND_TYPE);
642 * NOTE: assumes list of sub-packages contains no NULL elements.
643 * Any NULL elements should have been removed by earlier call
644 * to acpi_ns_remove_null_elements.
646 outer_elements = return_object->package.elements;
647 outer_element_count = return_object->package.count;
648 if (!outer_element_count) {
649 return (AE_AML_PACKAGE_LIMIT);
653 if (sort_direction == ACPI_SORT_DESCENDING) {
654 previous_value = ACPI_UINT32_MAX;
657 /* Examine each subpackage */
659 for (i = 0; i < outer_element_count; i++) {
661 /* Each element of the top-level package must also be a package */
663 if ((*outer_elements)->common.type != ACPI_TYPE_PACKAGE) {
664 return (AE_AML_OPERAND_TYPE);
667 /* Each sub-package must have the minimum length */
669 if ((*outer_elements)->package.count < expected_count) {
670 return (AE_AML_PACKAGE_LIMIT);
673 elements = (*outer_elements)->package.elements;
674 obj_desc = elements[sort_index];
676 if (obj_desc->common.type != ACPI_TYPE_INTEGER) {
677 return (AE_AML_OPERAND_TYPE);
681 * The list must be sorted in the specified order. If we detect a
682 * discrepancy, sort the entire list.
684 if (((sort_direction == ACPI_SORT_ASCENDING) &&
685 (obj_desc->integer.value < previous_value)) ||
686 ((sort_direction == ACPI_SORT_DESCENDING) &&
687 (obj_desc->integer.value > previous_value))) {
688 acpi_ns_sort_list(return_object->package.elements,
689 outer_element_count, sort_index,
692 data->flags |= ACPI_OBJECT_REPAIRED;
694 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR,
695 "%s: Repaired unsorted list - now sorted by %s\n",
696 data->pathname, sort_key_name));
700 previous_value = (u32) obj_desc->integer.value;
707 /******************************************************************************
709 * FUNCTION: acpi_ns_sort_list
711 * PARAMETERS: elements - Package object element list
712 * count - Element count for above
713 * index - Sort by which package element
714 * sort_direction - Ascending or Descending sort
718 * DESCRIPTION: Sort the objects that are in a package element list.
720 * NOTE: Assumes that all NULL elements have been removed from the package,
721 * and that all elements have been verified to be of type Integer.
723 *****************************************************************************/
726 acpi_ns_sort_list(union acpi_operand_object **elements,
727 u32 count, u32 index, u8 sort_direction)
729 union acpi_operand_object *obj_desc1;
730 union acpi_operand_object *obj_desc2;
731 union acpi_operand_object *temp_obj;
735 /* Simple bubble sort */
737 for (i = 1; i < count; i++) {
738 for (j = (count - 1); j >= i; j--) {
739 obj_desc1 = elements[j - 1]->package.elements[index];
740 obj_desc2 = elements[j]->package.elements[index];
742 if (((sort_direction == ACPI_SORT_ASCENDING) &&
743 (obj_desc1->integer.value >
744 obj_desc2->integer.value))
745 || ((sort_direction == ACPI_SORT_DESCENDING)
746 && (obj_desc1->integer.value <
747 obj_desc2->integer.value))) {
748 temp_obj = elements[j - 1];
749 elements[j - 1] = elements[j];
750 elements[j] = temp_obj;