1 /******************************************************************************
3 * Module Name: nsrepair2 - Repair for objects returned by specific
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2012, 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_CID(struct acpi_predefined_data
*data
,
78 union acpi_operand_object
**return_object_ptr
);
81 acpi_ns_repair_FDE(struct acpi_predefined_data
*data
,
82 union acpi_operand_object
**return_object_ptr
);
85 acpi_ns_repair_HID(struct acpi_predefined_data
*data
,
86 union acpi_operand_object
**return_object_ptr
);
89 acpi_ns_repair_PSS(struct acpi_predefined_data
*data
,
90 union acpi_operand_object
**return_object_ptr
);
93 acpi_ns_repair_TSS(struct acpi_predefined_data
*data
,
94 union acpi_operand_object
**return_object_ptr
);
97 acpi_ns_check_sorted_list(struct acpi_predefined_data
*data
,
98 union acpi_operand_object
*return_object
,
101 u8 sort_direction
, char *sort_key_name
);
104 acpi_ns_sort_list(union acpi_operand_object
**elements
,
105 u32 count
, u32 index
, u8 sort_direction
);
107 /* Values for sort_direction above */
109 #define ACPI_SORT_ASCENDING 0
110 #define ACPI_SORT_DESCENDING 1
113 * This table contains the names of the predefined methods for which we can
114 * perform more complex repairs.
118 * _ALR: Sort the list ascending by ambient_illuminance
119 * _CID: Strings: uppercase all, remove any leading asterisk
120 * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
121 * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
122 * _HID: Strings: uppercase all, remove any leading asterisk
123 * _PSS: Sort the list descending by Power
124 * _TSS: Sort the list descending by Power
126 * Names that must be packages, but cannot be sorted:
128 * _BCL: Values are tied to the Package index where they appear, and cannot
129 * be moved or sorted. These index values are used for _BQC and _BCM.
130 * However, we can fix the case where a buffer is returned, by converting
131 * it to a Package of integers.
133 static const struct acpi_repair_info acpi_ns_repairable_names
[] = {
134 {"_ALR", acpi_ns_repair_ALR
},
135 {"_CID", acpi_ns_repair_CID
},
136 {"_FDE", acpi_ns_repair_FDE
},
137 {"_GTM", acpi_ns_repair_FDE
}, /* _GTM has same repair as _FDE */
138 {"_HID", acpi_ns_repair_HID
},
139 {"_PSS", acpi_ns_repair_PSS
},
140 {"_TSS", acpi_ns_repair_TSS
},
141 {{0, 0, 0, 0}, NULL
} /* Table terminator */
144 #define ACPI_FDE_FIELD_COUNT 5
145 #define ACPI_FDE_BYTE_BUFFER_SIZE 5
146 #define ACPI_FDE_DWORD_BUFFER_SIZE (ACPI_FDE_FIELD_COUNT * sizeof (u32))
148 /******************************************************************************
150 * FUNCTION: acpi_ns_complex_repairs
152 * PARAMETERS: Data - Pointer to validation data structure
153 * Node - Namespace node for the method/object
154 * validate_status - Original status of earlier validation
155 * return_object_ptr - Pointer to the object returned from the
156 * evaluation of a method or object
158 * RETURN: Status. AE_OK if repair was successful. If name is not
159 * matched, validate_status is returned.
161 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
164 *****************************************************************************/
167 acpi_ns_complex_repairs(struct acpi_predefined_data
*data
,
168 struct acpi_namespace_node
*node
,
169 acpi_status validate_status
,
170 union acpi_operand_object
**return_object_ptr
)
172 const struct acpi_repair_info
*predefined
;
175 /* Check if this name is in the list of repairable names */
177 predefined
= acpi_ns_match_repairable_name(node
);
179 return (validate_status
);
182 status
= predefined
->repair_function(data
, return_object_ptr
);
186 /******************************************************************************
188 * FUNCTION: acpi_ns_match_repairable_name
190 * PARAMETERS: Node - Namespace node for the method/object
192 * RETURN: Pointer to entry in repair table. NULL indicates not found.
194 * DESCRIPTION: Check an object name against the repairable object list.
196 *****************************************************************************/
198 static const struct acpi_repair_info
*acpi_ns_match_repairable_name(struct
202 const struct acpi_repair_info
*this_name
;
204 /* Search info table for a repairable predefined method/object name */
206 this_name
= acpi_ns_repairable_names
;
207 while (this_name
->repair_function
) {
208 if (ACPI_COMPARE_NAME(node
->name
.ascii
, this_name
->name
)) {
214 return (NULL
); /* Not found */
217 /******************************************************************************
219 * FUNCTION: acpi_ns_repair_ALR
221 * PARAMETERS: Data - Pointer to validation data structure
222 * return_object_ptr - Pointer to the object returned from the
223 * evaluation of a method or object
225 * RETURN: Status. AE_OK if object is OK or was repaired successfully
227 * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
228 * ascending by the ambient illuminance values.
230 *****************************************************************************/
233 acpi_ns_repair_ALR(struct acpi_predefined_data
*data
,
234 union acpi_operand_object
**return_object_ptr
)
236 union acpi_operand_object
*return_object
= *return_object_ptr
;
239 status
= acpi_ns_check_sorted_list(data
, return_object
, 2, 1,
241 "AmbientIlluminance");
246 /******************************************************************************
248 * FUNCTION: acpi_ns_repair_FDE
250 * PARAMETERS: Data - Pointer to validation data structure
251 * return_object_ptr - Pointer to the object returned from the
252 * evaluation of a method or object
254 * RETURN: Status. AE_OK if object is OK or was repaired successfully
256 * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
257 * value is a Buffer of 5 DWORDs. This function repairs a common
258 * problem where the return value is a Buffer of BYTEs, not
261 *****************************************************************************/
264 acpi_ns_repair_FDE(struct acpi_predefined_data
*data
,
265 union acpi_operand_object
**return_object_ptr
)
267 union acpi_operand_object
*return_object
= *return_object_ptr
;
268 union acpi_operand_object
*buffer_object
;
273 ACPI_FUNCTION_NAME(ns_repair_FDE
);
275 switch (return_object
->common
.type
) {
276 case ACPI_TYPE_BUFFER
:
278 /* This is the expected type. Length should be (at least) 5 DWORDs */
280 if (return_object
->buffer
.length
>= ACPI_FDE_DWORD_BUFFER_SIZE
) {
284 /* We can only repair if we have exactly 5 BYTEs */
286 if (return_object
->buffer
.length
!= ACPI_FDE_BYTE_BUFFER_SIZE
) {
287 ACPI_WARN_PREDEFINED((AE_INFO
, data
->pathname
,
289 "Incorrect return buffer length %u, expected %u",
290 return_object
->buffer
.length
,
291 ACPI_FDE_DWORD_BUFFER_SIZE
));
293 return (AE_AML_OPERAND_TYPE
);
296 /* Create the new (larger) buffer object */
299 acpi_ut_create_buffer_object(ACPI_FDE_DWORD_BUFFER_SIZE
);
300 if (!buffer_object
) {
301 return (AE_NO_MEMORY
);
304 /* Expand each byte to a DWORD */
306 byte_buffer
= return_object
->buffer
.pointer
;
308 ACPI_CAST_PTR(u32
, buffer_object
->buffer
.pointer
);
310 for (i
= 0; i
< ACPI_FDE_FIELD_COUNT
; i
++) {
311 *dword_buffer
= (u32
) *byte_buffer
;
316 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
317 "%s Expanded Byte Buffer to expected DWord Buffer\n",
322 return (AE_AML_OPERAND_TYPE
);
325 /* Delete the original return object, return the new buffer object */
327 acpi_ut_remove_reference(return_object
);
328 *return_object_ptr
= buffer_object
;
330 data
->flags
|= ACPI_OBJECT_REPAIRED
;
334 /******************************************************************************
336 * FUNCTION: acpi_ns_repair_CID
338 * PARAMETERS: Data - Pointer to validation data structure
339 * return_object_ptr - Pointer to the object returned from the
340 * evaluation of a method or object
342 * RETURN: Status. AE_OK if object is OK or was repaired successfully
344 * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
345 * letters are uppercase and that there is no leading asterisk.
346 * If a Package, ensure same for all string elements.
348 *****************************************************************************/
351 acpi_ns_repair_CID(struct acpi_predefined_data
*data
,
352 union acpi_operand_object
**return_object_ptr
)
355 union acpi_operand_object
*return_object
= *return_object_ptr
;
356 union acpi_operand_object
**element_ptr
;
357 union acpi_operand_object
*original_element
;
358 u16 original_ref_count
;
361 /* Check for _CID as a simple string */
363 if (return_object
->common
.type
== ACPI_TYPE_STRING
) {
364 status
= acpi_ns_repair_HID(data
, return_object_ptr
);
368 /* Exit if not a Package */
370 if (return_object
->common
.type
!= ACPI_TYPE_PACKAGE
) {
374 /* Examine each element of the _CID package */
376 element_ptr
= return_object
->package
.elements
;
377 for (i
= 0; i
< return_object
->package
.count
; i
++) {
378 original_element
= *element_ptr
;
379 original_ref_count
= original_element
->common
.reference_count
;
381 status
= acpi_ns_repair_HID(data
, element_ptr
);
382 if (ACPI_FAILURE(status
)) {
386 /* Take care with reference counts */
388 if (original_element
!= *element_ptr
) {
390 /* Element was replaced */
392 (*element_ptr
)->common
.reference_count
=
395 acpi_ut_remove_reference(original_element
);
404 /******************************************************************************
406 * FUNCTION: acpi_ns_repair_HID
408 * PARAMETERS: Data - Pointer to validation data structure
409 * return_object_ptr - Pointer to the object returned from the
410 * evaluation of a method or object
412 * RETURN: Status. AE_OK if object is OK or was repaired successfully
414 * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
415 * letters are uppercase and that there is no leading asterisk.
417 *****************************************************************************/
420 acpi_ns_repair_HID(struct acpi_predefined_data
*data
,
421 union acpi_operand_object
**return_object_ptr
)
423 union acpi_operand_object
*return_object
= *return_object_ptr
;
424 union acpi_operand_object
*new_string
;
428 ACPI_FUNCTION_NAME(ns_repair_HID
);
430 /* We only care about string _HID objects (not integers) */
432 if (return_object
->common
.type
!= ACPI_TYPE_STRING
) {
436 if (return_object
->string
.length
== 0) {
437 ACPI_WARN_PREDEFINED((AE_INFO
, data
->pathname
, data
->node_flags
,
438 "Invalid zero-length _HID or _CID string"));
440 /* Return AE_OK anyway, let driver handle it */
442 data
->flags
|= ACPI_OBJECT_REPAIRED
;
446 /* It is simplest to always create a new string object */
448 new_string
= acpi_ut_create_string_object(return_object
->string
.length
);
450 return (AE_NO_MEMORY
);
454 * Remove a leading asterisk if present. For some unknown reason, there
455 * are many machines in the field that contains IDs like this.
457 * Examples: "*PNP0C03", "*ACPI0003"
459 source
= return_object
->string
.pointer
;
460 if (*source
== '*') {
462 new_string
->string
.length
--;
464 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
465 "%s: Removed invalid leading asterisk\n",
470 * Copy and uppercase the string. From the ACPI 5.0 specification:
472 * A valid PNP ID must be of the form "AAA####" where A is an uppercase
473 * letter and # is a hex digit. A valid ACPI ID must be of the form
474 * "NNNN####" where N is an uppercase letter or decimal digit, and
477 for (dest
= new_string
->string
.pointer
; *source
; dest
++, source
++) {
478 *dest
= (char)ACPI_TOUPPER(*source
);
481 acpi_ut_remove_reference(return_object
);
482 *return_object_ptr
= new_string
;
486 /******************************************************************************
488 * FUNCTION: acpi_ns_repair_TSS
490 * PARAMETERS: Data - Pointer to validation data structure
491 * return_object_ptr - Pointer to the object returned from the
492 * evaluation of a method or object
494 * RETURN: Status. AE_OK if object is OK or was repaired successfully
496 * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
497 * descending by the power dissipation values.
499 *****************************************************************************/
502 acpi_ns_repair_TSS(struct acpi_predefined_data
*data
,
503 union acpi_operand_object
**return_object_ptr
)
505 union acpi_operand_object
*return_object
= *return_object_ptr
;
507 struct acpi_namespace_node
*node
;
510 * We can only sort the _TSS return package if there is no _PSS in the
511 * same scope. This is because if _PSS is present, the ACPI specification
512 * dictates that the _TSS Power Dissipation field is to be ignored, and
513 * therefore some BIOSs leave garbage values in the _TSS Power field(s).
514 * In this case, it is best to just return the _TSS package as-is.
518 acpi_ns_get_node(data
->node
, "^_PSS", ACPI_NS_NO_UPSEARCH
, &node
);
519 if (ACPI_SUCCESS(status
)) {
523 status
= acpi_ns_check_sorted_list(data
, return_object
, 5, 1,
524 ACPI_SORT_DESCENDING
,
530 /******************************************************************************
532 * FUNCTION: acpi_ns_repair_PSS
534 * PARAMETERS: Data - Pointer to validation data structure
535 * return_object_ptr - Pointer to the object returned from the
536 * evaluation of a method or object
538 * RETURN: Status. AE_OK if object is OK or was repaired successfully
540 * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
541 * by the CPU frequencies. Check that the power dissipation values
542 * are all proportional to CPU frequency (i.e., sorting by
543 * frequency should be the same as sorting by power.)
545 *****************************************************************************/
548 acpi_ns_repair_PSS(struct acpi_predefined_data
*data
,
549 union acpi_operand_object
**return_object_ptr
)
551 union acpi_operand_object
*return_object
= *return_object_ptr
;
552 union acpi_operand_object
**outer_elements
;
553 u32 outer_element_count
;
554 union acpi_operand_object
**elements
;
555 union acpi_operand_object
*obj_desc
;
561 * Entries (sub-packages) in the _PSS Package must be sorted by power
562 * dissipation, in descending order. If it appears that the list is
563 * incorrectly sorted, sort it. We sort by cpu_frequency, since this
564 * should be proportional to the power.
566 status
= acpi_ns_check_sorted_list(data
, return_object
, 6, 0,
567 ACPI_SORT_DESCENDING
,
569 if (ACPI_FAILURE(status
)) {
574 * We now know the list is correctly sorted by CPU frequency. Check if
575 * the power dissipation values are proportional.
577 previous_value
= ACPI_UINT32_MAX
;
578 outer_elements
= return_object
->package
.elements
;
579 outer_element_count
= return_object
->package
.count
;
581 for (i
= 0; i
< outer_element_count
; i
++) {
582 elements
= (*outer_elements
)->package
.elements
;
583 obj_desc
= elements
[1]; /* Index1 = power_dissipation */
585 if ((u32
) obj_desc
->integer
.value
> previous_value
) {
586 ACPI_WARN_PREDEFINED((AE_INFO
, data
->pathname
,
588 "SubPackage[%u,%u] - suspicious power dissipation values",
592 previous_value
= (u32
) obj_desc
->integer
.value
;
599 /******************************************************************************
601 * FUNCTION: acpi_ns_check_sorted_list
603 * PARAMETERS: Data - Pointer to validation data structure
604 * return_object - Pointer to the top-level returned object
605 * expected_count - Minimum length of each sub-package
606 * sort_index - Sub-package entry to sort on
607 * sort_direction - Ascending or descending
608 * sort_key_name - Name of the sort_index field
610 * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
611 * has been repaired by sorting the list.
613 * DESCRIPTION: Check if the package list is valid and sorted correctly by the
614 * sort_index. If not, then sort the list.
616 *****************************************************************************/
619 acpi_ns_check_sorted_list(struct acpi_predefined_data
*data
,
620 union acpi_operand_object
*return_object
,
623 u8 sort_direction
, char *sort_key_name
)
625 u32 outer_element_count
;
626 union acpi_operand_object
**outer_elements
;
627 union acpi_operand_object
**elements
;
628 union acpi_operand_object
*obj_desc
;
632 ACPI_FUNCTION_NAME(ns_check_sorted_list
);
634 /* The top-level object must be a package */
636 if (return_object
->common
.type
!= ACPI_TYPE_PACKAGE
) {
637 return (AE_AML_OPERAND_TYPE
);
641 * NOTE: assumes list of sub-packages contains no NULL elements.
642 * Any NULL elements should have been removed by earlier call
643 * to acpi_ns_remove_null_elements.
645 outer_elements
= return_object
->package
.elements
;
646 outer_element_count
= return_object
->package
.count
;
647 if (!outer_element_count
) {
648 return (AE_AML_PACKAGE_LIMIT
);
652 if (sort_direction
== ACPI_SORT_DESCENDING
) {
653 previous_value
= ACPI_UINT32_MAX
;
656 /* Examine each subpackage */
658 for (i
= 0; i
< outer_element_count
; i
++) {
660 /* Each element of the top-level package must also be a package */
662 if ((*outer_elements
)->common
.type
!= ACPI_TYPE_PACKAGE
) {
663 return (AE_AML_OPERAND_TYPE
);
666 /* Each sub-package must have the minimum length */
668 if ((*outer_elements
)->package
.count
< expected_count
) {
669 return (AE_AML_PACKAGE_LIMIT
);
672 elements
= (*outer_elements
)->package
.elements
;
673 obj_desc
= elements
[sort_index
];
675 if (obj_desc
->common
.type
!= ACPI_TYPE_INTEGER
) {
676 return (AE_AML_OPERAND_TYPE
);
680 * The list must be sorted in the specified order. If we detect a
681 * discrepancy, sort the entire list.
683 if (((sort_direction
== ACPI_SORT_ASCENDING
) &&
684 (obj_desc
->integer
.value
< previous_value
)) ||
685 ((sort_direction
== ACPI_SORT_DESCENDING
) &&
686 (obj_desc
->integer
.value
> previous_value
))) {
687 acpi_ns_sort_list(return_object
->package
.elements
,
688 outer_element_count
, sort_index
,
691 data
->flags
|= ACPI_OBJECT_REPAIRED
;
693 ACPI_DEBUG_PRINT((ACPI_DB_REPAIR
,
694 "%s: Repaired unsorted list - now sorted by %s\n",
695 data
->pathname
, sort_key_name
));
699 previous_value
= (u32
) obj_desc
->integer
.value
;
706 /******************************************************************************
708 * FUNCTION: acpi_ns_sort_list
710 * PARAMETERS: Elements - Package object element list
711 * Count - Element count for above
712 * Index - Sort by which package element
713 * sort_direction - Ascending or Descending sort
717 * DESCRIPTION: Sort the objects that are in a package element list.
719 * NOTE: Assumes that all NULL elements have been removed from the package,
720 * and that all elements have been verified to be of type Integer.
722 *****************************************************************************/
725 acpi_ns_sort_list(union acpi_operand_object
**elements
,
726 u32 count
, u32 index
, u8 sort_direction
)
728 union acpi_operand_object
*obj_desc1
;
729 union acpi_operand_object
*obj_desc2
;
730 union acpi_operand_object
*temp_obj
;
734 /* Simple bubble sort */
736 for (i
= 1; i
< count
; i
++) {
737 for (j
= (count
- 1); j
>= i
; j
--) {
738 obj_desc1
= elements
[j
- 1]->package
.elements
[index
];
739 obj_desc2
= elements
[j
]->package
.elements
[index
];
741 if (((sort_direction
== ACPI_SORT_ASCENDING
) &&
742 (obj_desc1
->integer
.value
>
743 obj_desc2
->integer
.value
))
744 || ((sort_direction
== ACPI_SORT_DESCENDING
)
745 && (obj_desc1
->integer
.value
<
746 obj_desc2
->integer
.value
))) {
747 temp_obj
= elements
[j
- 1];
748 elements
[j
- 1] = elements
[j
];
749 elements
[j
] = temp_obj
;