1 /******************************************************************************
3 * Module Name: nsrepair2 - Repair for objects returned by specific
6 *****************************************************************************/
9 * Copyright (C) 2000 - 2011, 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 #define __NSREPAIR2_C__
47 #include <contrib/dev/acpica/include/acpi.h>
48 #include <contrib/dev/acpica/include/accommon.h>
49 #include <contrib/dev/acpica/include/acnamesp.h>
51 #define _COMPONENT ACPI_NAMESPACE
52 ACPI_MODULE_NAME ("nsrepair2")
56 * Information structure and handler for ACPI predefined names that can
57 * be repaired on a per-name basis.
60 ACPI_STATUS (*ACPI_REPAIR_FUNCTION) (
61 ACPI_PREDEFINED_DATA *Data,
62 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
64 typedef struct acpi_repair_info
66 char Name[ACPI_NAME_SIZE];
67 ACPI_REPAIR_FUNCTION RepairFunction;
72 /* Local prototypes */
74 static const ACPI_REPAIR_INFO *
75 AcpiNsMatchRepairableName (
76 ACPI_NAMESPACE_NODE *Node);
80 ACPI_PREDEFINED_DATA *Data,
81 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
85 ACPI_PREDEFINED_DATA *Data,
86 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
90 ACPI_PREDEFINED_DATA *Data,
91 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
95 ACPI_PREDEFINED_DATA *Data,
96 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
100 ACPI_PREDEFINED_DATA *Data,
101 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
105 ACPI_PREDEFINED_DATA *Data,
106 ACPI_OPERAND_OBJECT **ReturnObjectPtr);
109 AcpiNsCheckSortedList (
110 ACPI_PREDEFINED_DATA *Data,
111 ACPI_OPERAND_OBJECT *ReturnObject,
112 UINT32 ExpectedCount,
119 ACPI_OPERAND_OBJECT **Elements,
122 UINT8 SortDirection);
124 /* Values for SortDirection above */
126 #define ACPI_SORT_ASCENDING 0
127 #define ACPI_SORT_DESCENDING 1
131 * This table contains the names of the predefined methods for which we can
132 * perform more complex repairs.
136 * _ALR: Sort the list ascending by AmbientIlluminance
137 * _CID: Strings: uppercase all, remove any leading asterisk
138 * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
139 * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
140 * _HID: Strings: uppercase all, remove any leading asterisk
141 * _PSS: Sort the list descending by Power
142 * _TSS: Sort the list descending by Power
144 * Names that must be packages, but cannot be sorted:
146 * _BCL: Values are tied to the Package index where they appear, and cannot
147 * be moved or sorted. These index values are used for _BQC and _BCM.
148 * However, we can fix the case where a buffer is returned, by converting
149 * it to a Package of integers.
151 static const ACPI_REPAIR_INFO AcpiNsRepairableNames[] =
153 {"_ALR", AcpiNsRepair_ALR},
154 {"_CID", AcpiNsRepair_CID},
155 {"_FDE", AcpiNsRepair_FDE},
156 {"_GTM", AcpiNsRepair_FDE}, /* _GTM has same repair as _FDE */
157 {"_HID", AcpiNsRepair_HID},
158 {"_PSS", AcpiNsRepair_PSS},
159 {"_TSS", AcpiNsRepair_TSS},
160 {{0,0,0,0}, NULL} /* Table terminator */
164 #define ACPI_FDE_FIELD_COUNT 5
165 #define ACPI_FDE_BYTE_BUFFER_SIZE 5
166 #define ACPI_FDE_DWORD_BUFFER_SIZE (ACPI_FDE_FIELD_COUNT * sizeof (UINT32))
169 /******************************************************************************
171 * FUNCTION: AcpiNsComplexRepairs
173 * PARAMETERS: Data - Pointer to validation data structure
174 * Node - Namespace node for the method/object
175 * ValidateStatus - Original status of earlier validation
176 * ReturnObjectPtr - Pointer to the object returned from the
177 * evaluation of a method or object
179 * RETURN: Status. AE_OK if repair was successful. If name is not
180 * matched, ValidateStatus is returned.
182 * DESCRIPTION: Attempt to repair/convert a return object of a type that was
185 *****************************************************************************/
188 AcpiNsComplexRepairs (
189 ACPI_PREDEFINED_DATA *Data,
190 ACPI_NAMESPACE_NODE *Node,
191 ACPI_STATUS ValidateStatus,
192 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
194 const ACPI_REPAIR_INFO *Predefined;
198 /* Check if this name is in the list of repairable names */
200 Predefined = AcpiNsMatchRepairableName (Node);
203 return (ValidateStatus);
206 Status = Predefined->RepairFunction (Data, ReturnObjectPtr);
211 /******************************************************************************
213 * FUNCTION: AcpiNsMatchRepairableName
215 * PARAMETERS: Node - Namespace node for the method/object
217 * RETURN: Pointer to entry in repair table. NULL indicates not found.
219 * DESCRIPTION: Check an object name against the repairable object list.
221 *****************************************************************************/
223 static const ACPI_REPAIR_INFO *
224 AcpiNsMatchRepairableName (
225 ACPI_NAMESPACE_NODE *Node)
227 const ACPI_REPAIR_INFO *ThisName;
230 /* Search info table for a repairable predefined method/object name */
232 ThisName = AcpiNsRepairableNames;
233 while (ThisName->RepairFunction)
235 if (ACPI_COMPARE_NAME (Node->Name.Ascii, ThisName->Name))
242 return (NULL); /* Not found */
246 /******************************************************************************
248 * FUNCTION: AcpiNsRepair_ALR
250 * PARAMETERS: Data - Pointer to validation data structure
251 * ReturnObjectPtr - 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 _ALR object. If necessary, sort the object list
257 * ascending by the ambient illuminance values.
259 *****************************************************************************/
263 ACPI_PREDEFINED_DATA *Data,
264 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
266 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
270 Status = AcpiNsCheckSortedList (Data, ReturnObject, 2, 1,
271 ACPI_SORT_ASCENDING, "AmbientIlluminance");
277 /******************************************************************************
279 * FUNCTION: AcpiNsRepair_FDE
281 * PARAMETERS: Data - Pointer to validation data structure
282 * ReturnObjectPtr - Pointer to the object returned from the
283 * evaluation of a method or object
285 * RETURN: Status. AE_OK if object is OK or was repaired successfully
287 * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
288 * value is a Buffer of 5 DWORDs. This function repairs a common
289 * problem where the return value is a Buffer of BYTEs, not
292 *****************************************************************************/
296 ACPI_PREDEFINED_DATA *Data,
297 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
299 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
300 ACPI_OPERAND_OBJECT *BufferObject;
306 ACPI_FUNCTION_NAME (NsRepair_FDE);
309 switch (ReturnObject->Common.Type)
311 case ACPI_TYPE_BUFFER:
313 /* This is the expected type. Length should be (at least) 5 DWORDs */
315 if (ReturnObject->Buffer.Length >= ACPI_FDE_DWORD_BUFFER_SIZE)
320 /* We can only repair if we have exactly 5 BYTEs */
322 if (ReturnObject->Buffer.Length != ACPI_FDE_BYTE_BUFFER_SIZE)
324 ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
325 "Incorrect return buffer length %u, expected %u",
326 ReturnObject->Buffer.Length, ACPI_FDE_DWORD_BUFFER_SIZE));
328 return (AE_AML_OPERAND_TYPE);
331 /* Create the new (larger) buffer object */
333 BufferObject = AcpiUtCreateBufferObject (ACPI_FDE_DWORD_BUFFER_SIZE);
336 return (AE_NO_MEMORY);
339 /* Expand each byte to a DWORD */
341 ByteBuffer = ReturnObject->Buffer.Pointer;
342 DwordBuffer = ACPI_CAST_PTR (UINT32, BufferObject->Buffer.Pointer);
344 for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++)
346 *DwordBuffer = (UINT32) *ByteBuffer;
351 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
352 "%s Expanded Byte Buffer to expected DWord Buffer\n",
357 return (AE_AML_OPERAND_TYPE);
360 /* Delete the original return object, return the new buffer object */
362 AcpiUtRemoveReference (ReturnObject);
363 *ReturnObjectPtr = BufferObject;
365 Data->Flags |= ACPI_OBJECT_REPAIRED;
370 /******************************************************************************
372 * FUNCTION: AcpiNsRepair_CID
374 * PARAMETERS: Data - Pointer to validation data structure
375 * ReturnObjectPtr - Pointer to the object returned from the
376 * evaluation of a method or object
378 * RETURN: Status. AE_OK if object is OK or was repaired successfully
380 * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
381 * letters are uppercase and that there is no leading asterisk.
382 * If a Package, ensure same for all string elements.
384 *****************************************************************************/
388 ACPI_PREDEFINED_DATA *Data,
389 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
392 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
393 ACPI_OPERAND_OBJECT **ElementPtr;
394 ACPI_OPERAND_OBJECT *OriginalElement;
395 UINT16 OriginalRefCount;
399 /* Check for _CID as a simple string */
401 if (ReturnObject->Common.Type == ACPI_TYPE_STRING)
403 Status = AcpiNsRepair_HID (Data, ReturnObjectPtr);
407 /* Exit if not a Package */
409 if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
414 /* Examine each element of the _CID package */
416 ElementPtr = ReturnObject->Package.Elements;
417 for (i = 0; i < ReturnObject->Package.Count; i++)
419 OriginalElement = *ElementPtr;
420 OriginalRefCount = OriginalElement->Common.ReferenceCount;
422 Status = AcpiNsRepair_HID (Data, ElementPtr);
423 if (ACPI_FAILURE (Status))
428 /* Take care with reference counts */
430 if (OriginalElement != *ElementPtr)
432 /* Element was replaced */
434 (*ElementPtr)->Common.ReferenceCount =
437 AcpiUtRemoveReference (OriginalElement);
447 /******************************************************************************
449 * FUNCTION: AcpiNsRepair_HID
451 * PARAMETERS: Data - Pointer to validation data structure
452 * ReturnObjectPtr - Pointer to the object returned from the
453 * evaluation of a method or object
455 * RETURN: Status. AE_OK if object is OK or was repaired successfully
457 * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
458 * letters are uppercase and that there is no leading asterisk.
460 *****************************************************************************/
464 ACPI_PREDEFINED_DATA *Data,
465 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
467 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
468 ACPI_OPERAND_OBJECT *NewString;
473 ACPI_FUNCTION_NAME (NsRepair_HID);
476 /* We only care about string _HID objects (not integers) */
478 if (ReturnObject->Common.Type != ACPI_TYPE_STRING)
483 if (ReturnObject->String.Length == 0)
485 ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
486 "Invalid zero-length _HID or _CID string"));
488 /* Return AE_OK anyway, let driver handle it */
490 Data->Flags |= ACPI_OBJECT_REPAIRED;
494 /* It is simplest to always create a new string object */
496 NewString = AcpiUtCreateStringObject (ReturnObject->String.Length);
499 return (AE_NO_MEMORY);
503 * Remove a leading asterisk if present. For some unknown reason, there
504 * are many machines in the field that contains IDs like this.
506 * Examples: "*PNP0C03", "*ACPI0003"
508 Source = ReturnObject->String.Pointer;
512 NewString->String.Length--;
514 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
515 "%s: Removed invalid leading asterisk\n", Data->Pathname));
519 * Copy and uppercase the string. From the ACPI specification:
521 * A valid PNP ID must be of the form "AAA####" where A is an uppercase
522 * letter and # is a hex digit. A valid ACPI ID must be of the form
523 * "ACPI####" where # is a hex digit.
525 for (Dest = NewString->String.Pointer; *Source; Dest++, Source++)
527 *Dest = (char) ACPI_TOUPPER (*Source);
530 AcpiUtRemoveReference (ReturnObject);
531 *ReturnObjectPtr = NewString;
536 /******************************************************************************
538 * FUNCTION: AcpiNsRepair_TSS
540 * PARAMETERS: Data - Pointer to validation data structure
541 * ReturnObjectPtr - Pointer to the object returned from the
542 * evaluation of a method or object
544 * RETURN: Status. AE_OK if object is OK or was repaired successfully
546 * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
547 * descending by the power dissipation values.
549 *****************************************************************************/
553 ACPI_PREDEFINED_DATA *Data,
554 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
556 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
560 Status = AcpiNsCheckSortedList (Data, ReturnObject, 5, 1,
561 ACPI_SORT_DESCENDING, "PowerDissipation");
567 /******************************************************************************
569 * FUNCTION: AcpiNsRepair_PSS
571 * PARAMETERS: Data - Pointer to validation data structure
572 * ReturnObjectPtr - Pointer to the object returned from the
573 * evaluation of a method or object
575 * RETURN: Status. AE_OK if object is OK or was repaired successfully
577 * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
578 * by the CPU frequencies. Check that the power dissipation values
579 * are all proportional to CPU frequency (i.e., sorting by
580 * frequency should be the same as sorting by power.)
582 *****************************************************************************/
586 ACPI_PREDEFINED_DATA *Data,
587 ACPI_OPERAND_OBJECT **ReturnObjectPtr)
589 ACPI_OPERAND_OBJECT *ReturnObject = *ReturnObjectPtr;
590 ACPI_OPERAND_OBJECT **OuterElements;
591 UINT32 OuterElementCount;
592 ACPI_OPERAND_OBJECT **Elements;
593 ACPI_OPERAND_OBJECT *ObjDesc;
594 UINT32 PreviousValue;
600 * Entries (sub-packages) in the _PSS Package must be sorted by power
601 * dissipation, in descending order. If it appears that the list is
602 * incorrectly sorted, sort it. We sort by CpuFrequency, since this
603 * should be proportional to the power.
605 Status =AcpiNsCheckSortedList (Data, ReturnObject, 6, 0,
606 ACPI_SORT_DESCENDING, "CpuFrequency");
607 if (ACPI_FAILURE (Status))
613 * We now know the list is correctly sorted by CPU frequency. Check if
614 * the power dissipation values are proportional.
616 PreviousValue = ACPI_UINT32_MAX;
617 OuterElements = ReturnObject->Package.Elements;
618 OuterElementCount = ReturnObject->Package.Count;
620 for (i = 0; i < OuterElementCount; i++)
622 Elements = (*OuterElements)->Package.Elements;
623 ObjDesc = Elements[1]; /* Index1 = PowerDissipation */
625 if ((UINT32) ObjDesc->Integer.Value > PreviousValue)
627 ACPI_WARN_PREDEFINED ((AE_INFO, Data->Pathname, Data->NodeFlags,
628 "SubPackage[%u,%u] - suspicious power dissipation values",
632 PreviousValue = (UINT32) ObjDesc->Integer.Value;
640 /******************************************************************************
642 * FUNCTION: AcpiNsCheckSortedList
644 * PARAMETERS: Data - Pointer to validation data structure
645 * ReturnObject - Pointer to the top-level returned object
646 * ExpectedCount - Minimum length of each sub-package
647 * SortIndex - Sub-package entry to sort on
648 * SortDirection - Ascending or descending
649 * SortKeyName - Name of the SortIndex field
651 * RETURN: Status. AE_OK if the list is valid and is sorted correctly or
652 * has been repaired by sorting the list.
654 * DESCRIPTION: Check if the package list is valid and sorted correctly by the
655 * SortIndex. If not, then sort the list.
657 *****************************************************************************/
660 AcpiNsCheckSortedList (
661 ACPI_PREDEFINED_DATA *Data,
662 ACPI_OPERAND_OBJECT *ReturnObject,
663 UINT32 ExpectedCount,
668 UINT32 OuterElementCount;
669 ACPI_OPERAND_OBJECT **OuterElements;
670 ACPI_OPERAND_OBJECT **Elements;
671 ACPI_OPERAND_OBJECT *ObjDesc;
673 UINT32 PreviousValue;
676 ACPI_FUNCTION_NAME (NsCheckSortedList);
679 /* The top-level object must be a package */
681 if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
683 return (AE_AML_OPERAND_TYPE);
687 * NOTE: assumes list of sub-packages contains no NULL elements.
688 * Any NULL elements should have been removed by earlier call
689 * to AcpiNsRemoveNullElements.
691 OuterElements = ReturnObject->Package.Elements;
692 OuterElementCount = ReturnObject->Package.Count;
693 if (!OuterElementCount)
695 return (AE_AML_PACKAGE_LIMIT);
699 if (SortDirection == ACPI_SORT_DESCENDING)
701 PreviousValue = ACPI_UINT32_MAX;
704 /* Examine each subpackage */
706 for (i = 0; i < OuterElementCount; i++)
708 /* Each element of the top-level package must also be a package */
710 if ((*OuterElements)->Common.Type != ACPI_TYPE_PACKAGE)
712 return (AE_AML_OPERAND_TYPE);
715 /* Each sub-package must have the minimum length */
717 if ((*OuterElements)->Package.Count < ExpectedCount)
719 return (AE_AML_PACKAGE_LIMIT);
722 Elements = (*OuterElements)->Package.Elements;
723 ObjDesc = Elements[SortIndex];
725 if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
727 return (AE_AML_OPERAND_TYPE);
731 * The list must be sorted in the specified order. If we detect a
732 * discrepancy, sort the entire list.
734 if (((SortDirection == ACPI_SORT_ASCENDING) &&
735 (ObjDesc->Integer.Value < PreviousValue)) ||
736 ((SortDirection == ACPI_SORT_DESCENDING) &&
737 (ObjDesc->Integer.Value > PreviousValue)))
739 AcpiNsSortList (ReturnObject->Package.Elements,
740 OuterElementCount, SortIndex, SortDirection);
742 Data->Flags |= ACPI_OBJECT_REPAIRED;
744 ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
745 "%s: Repaired unsorted list - now sorted by %s\n",
746 Data->Pathname, SortKeyName));
750 PreviousValue = (UINT32) ObjDesc->Integer.Value;
758 /******************************************************************************
760 * FUNCTION: AcpiNsSortList
762 * PARAMETERS: Elements - Package object element list
763 * Count - Element count for above
764 * Index - Sort by which package element
765 * SortDirection - Ascending or Descending sort
769 * DESCRIPTION: Sort the objects that are in a package element list.
771 * NOTE: Assumes that all NULL elements have been removed from the package,
772 * and that all elements have been verified to be of type Integer.
774 *****************************************************************************/
778 ACPI_OPERAND_OBJECT **Elements,
783 ACPI_OPERAND_OBJECT *ObjDesc1;
784 ACPI_OPERAND_OBJECT *ObjDesc2;
785 ACPI_OPERAND_OBJECT *TempObj;
790 /* Simple bubble sort */
792 for (i = 1; i < Count; i++)
794 for (j = (Count - 1); j >= i; j--)
796 ObjDesc1 = Elements[j-1]->Package.Elements[Index];
797 ObjDesc2 = Elements[j]->Package.Elements[Index];
799 if (((SortDirection == ACPI_SORT_ASCENDING) &&
800 (ObjDesc1->Integer.Value > ObjDesc2->Integer.Value)) ||
802 ((SortDirection == ACPI_SORT_DESCENDING) &&
803 (ObjDesc1->Integer.Value < ObjDesc2->Integer.Value)))
805 TempObj = Elements[j-1];
806 Elements[j-1] = Elements[j];
807 Elements[j] = TempObj;