xref: /illumos-gate/usr/src/common/acpica/namespace/nsrepair2.c (revision 2a1fd0ffe121888d44fdec321c25b53dcfaa9118)
1 /******************************************************************************
2  *
3  * Module Name: nsrepair2 - Repair for objects returned by specific
4  *                          predefined methods
5  *
6  *****************************************************************************/
7 
8 /******************************************************************************
9  *
10  * 1. Copyright Notice
11  *
12  * Some or all of this work - Copyright (c) 1999 - 2018, Intel Corp.
13  * All rights reserved.
14  *
15  * 2. License
16  *
17  * 2.1. This is your license from Intel Corp. under its intellectual property
18  * rights. You may have additional license terms from the party that provided
19  * you this software, covering your right to use that party's intellectual
20  * property rights.
21  *
22  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
23  * copy of the source code appearing in this file ("Covered Code") an
24  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
25  * base code distributed originally by Intel ("Original Intel Code") to copy,
26  * make derivatives, distribute, use and display any portion of the Covered
27  * Code in any form, with the right to sublicense such rights; and
28  *
29  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
30  * license (with the right to sublicense), under only those claims of Intel
31  * patents that are infringed by the Original Intel Code, to make, use, sell,
32  * offer to sell, and import the Covered Code and derivative works thereof
33  * solely to the minimum extent necessary to exercise the above copyright
34  * license, and in no event shall the patent license extend to any additions
35  * to or modifications of the Original Intel Code. No other license or right
36  * is granted directly or by implication, estoppel or otherwise;
37  *
38  * The above copyright and patent license is granted only if the following
39  * conditions are met:
40  *
41  * 3. Conditions
42  *
43  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
44  * Redistribution of source code of any substantial portion of the Covered
45  * Code or modification with rights to further distribute source must include
46  * the above Copyright Notice, the above License, this list of Conditions,
47  * and the following Disclaimer and Export Compliance provision. In addition,
48  * Licensee must cause all Covered Code to which Licensee contributes to
49  * contain a file documenting the changes Licensee made to create that Covered
50  * Code and the date of any change. Licensee must include in that file the
51  * documentation of any changes made by any predecessor Licensee. Licensee
52  * must include a prominent statement that the modification is derived,
53  * directly or indirectly, from Original Intel Code.
54  *
55  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
56  * Redistribution of source code of any substantial portion of the Covered
57  * Code or modification without rights to further distribute source must
58  * include the following Disclaimer and Export Compliance provision in the
59  * documentation and/or other materials provided with distribution. In
60  * addition, Licensee may not authorize further sublicense of source of any
61  * portion of the Covered Code, and must include terms to the effect that the
62  * license from Licensee to its licensee is limited to the intellectual
63  * property embodied in the software Licensee provides to its licensee, and
64  * not to intellectual property embodied in modifications its licensee may
65  * make.
66  *
67  * 3.3. Redistribution of Executable. Redistribution in executable form of any
68  * substantial portion of the Covered Code or modification must reproduce the
69  * above Copyright Notice, and the following Disclaimer and Export Compliance
70  * provision in the documentation and/or other materials provided with the
71  * distribution.
72  *
73  * 3.4. Intel retains all right, title, and interest in and to the Original
74  * Intel Code.
75  *
76  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
77  * Intel shall be used in advertising or otherwise to promote the sale, use or
78  * other dealings in products derived from or relating to the Covered Code
79  * without prior written authorization from Intel.
80  *
81  * 4. Disclaimer and Export Compliance
82  *
83  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
84  * HERE. ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
85  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT, ASSISTANCE,
86  * INSTALLATION, TRAINING OR OTHER SERVICES. INTEL WILL NOT PROVIDE ANY
87  * UPDATES, ENHANCEMENTS OR EXTENSIONS. INTEL SPECIFICALLY DISCLAIMS ANY
88  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
89  * PARTICULAR PURPOSE.
90  *
91  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
92  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
93  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
94  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
95  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
96  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES. THESE LIMITATIONS
97  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
98  * LIMITED REMEDY.
99  *
100  * 4.3. Licensee shall not export, either directly or indirectly, any of this
101  * software or system incorporating such software without first obtaining any
102  * required license or other approval from the U. S. Department of Commerce or
103  * any other agency or department of the United States Government. In the
104  * event Licensee exports any such software from the United States or
105  * re-exports any such software from a foreign destination, Licensee shall
106  * ensure that the distribution and export/re-export of the software is in
107  * compliance with all laws, regulations, orders, or other restrictions of the
108  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
109  * any of its subsidiaries will export/re-export any technical data, process,
110  * software, or service, directly or indirectly, to any country for which the
111  * United States government or any agency thereof requires an export license,
112  * other governmental approval, or letter of assurance, without first obtaining
113  * such license, approval or letter.
114  *
115  *****************************************************************************
116  *
117  * Alternatively, you may choose to be licensed under the terms of the
118  * following license:
119  *
120  * Redistribution and use in source and binary forms, with or without
121  * modification, are permitted provided that the following conditions
122  * are met:
123  * 1. Redistributions of source code must retain the above copyright
124  *    notice, this list of conditions, and the following disclaimer,
125  *    without modification.
126  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
127  *    substantially similar to the "NO WARRANTY" disclaimer below
128  *    ("Disclaimer") and any redistribution must be conditioned upon
129  *    including a substantially similar Disclaimer requirement for further
130  *    binary redistribution.
131  * 3. Neither the names of the above-listed copyright holders nor the names
132  *    of any contributors may be used to endorse or promote products derived
133  *    from this software without specific prior written permission.
134  *
135  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
136  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
137  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
138  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
139  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
140  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
141  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
142  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
143  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
144  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
145  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
146  *
147  * Alternatively, you may choose to be licensed under the terms of the
148  * GNU General Public License ("GPL") version 2 as published by the Free
149  * Software Foundation.
150  *
151  *****************************************************************************/
152 
153 #include "acpi.h"
154 #include "accommon.h"
155 #include "acnamesp.h"
156 
157 #define _COMPONENT          ACPI_NAMESPACE
158         ACPI_MODULE_NAME    ("nsrepair2")
159 
160 
161 /*
162  * Information structure and handler for ACPI predefined names that can
163  * be repaired on a per-name basis.
164  */
165 typedef
166 ACPI_STATUS (*ACPI_REPAIR_FUNCTION) (
167     ACPI_EVALUATE_INFO      *Info,
168     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
169 
170 typedef struct acpi_repair_info
171 {
172     char                    Name[ACPI_NAME_SIZE];
173     ACPI_REPAIR_FUNCTION    RepairFunction;
174 
175 } ACPI_REPAIR_INFO;
176 
177 
178 /* Local prototypes */
179 
180 static const ACPI_REPAIR_INFO *
181 AcpiNsMatchComplexRepair (
182     ACPI_NAMESPACE_NODE     *Node);
183 
184 static ACPI_STATUS
185 AcpiNsRepair_ALR (
186     ACPI_EVALUATE_INFO      *Info,
187     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
188 
189 static ACPI_STATUS
190 AcpiNsRepair_CID (
191     ACPI_EVALUATE_INFO      *Info,
192     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
193 
194 static ACPI_STATUS
195 AcpiNsRepair_CST (
196     ACPI_EVALUATE_INFO      *Info,
197     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
198 
199 static ACPI_STATUS
200 AcpiNsRepair_FDE (
201     ACPI_EVALUATE_INFO      *Info,
202     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
203 
204 static ACPI_STATUS
205 AcpiNsRepair_HID (
206     ACPI_EVALUATE_INFO      *Info,
207     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
208 
209 static ACPI_STATUS
210 AcpiNsRepair_PRT (
211     ACPI_EVALUATE_INFO      *Info,
212     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
213 
214 static ACPI_STATUS
215 AcpiNsRepair_PSS (
216     ACPI_EVALUATE_INFO      *Info,
217     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
218 
219 static ACPI_STATUS
220 AcpiNsRepair_TSS (
221     ACPI_EVALUATE_INFO      *Info,
222     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
223 
224 static ACPI_STATUS
225 AcpiNsCheckSortedList (
226     ACPI_EVALUATE_INFO      *Info,
227     ACPI_OPERAND_OBJECT     *ReturnObject,
228     UINT32                  StartIndex,
229     UINT32                  ExpectedCount,
230     UINT32                  SortIndex,
231     UINT8                   SortDirection,
232     char                    *SortKeyName);
233 
234 /* Values for SortDirection above */
235 
236 #define ACPI_SORT_ASCENDING     0
237 #define ACPI_SORT_DESCENDING    1
238 
239 static void
240 AcpiNsRemoveElement (
241     ACPI_OPERAND_OBJECT     *ObjDesc,
242     UINT32                  Index);
243 
244 static void
245 AcpiNsSortList (
246     ACPI_OPERAND_OBJECT     **Elements,
247     UINT32                  Count,
248     UINT32                  Index,
249     UINT8                   SortDirection);
250 
251 
252 /*
253  * This table contains the names of the predefined methods for which we can
254  * perform more complex repairs.
255  *
256  * As necessary:
257  *
258  * _ALR: Sort the list ascending by AmbientIlluminance
259  * _CID: Strings: uppercase all, remove any leading asterisk
260  * _CST: Sort the list ascending by C state type
261  * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
262  * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
263  * _HID: Strings: uppercase all, remove any leading asterisk
264  * _PRT: Fix reversed SourceName and SourceIndex
265  * _PSS: Sort the list descending by Power
266  * _TSS: Sort the list descending by Power
267  *
268  * Names that must be packages, but cannot be sorted:
269  *
270  * _BCL: Values are tied to the Package index where they appear, and cannot
271  * be moved or sorted. These index values are used for _BQC and _BCM.
272  * However, we can fix the case where a buffer is returned, by converting
273  * it to a Package of integers.
274  */
275 static const ACPI_REPAIR_INFO       AcpiNsRepairableNames[] =
276 {
277     {"_ALR", AcpiNsRepair_ALR},
278     {"_CID", AcpiNsRepair_CID},
279     {"_CST", AcpiNsRepair_CST},
280     {"_FDE", AcpiNsRepair_FDE},
281     {"_GTM", AcpiNsRepair_FDE},     /* _GTM has same repair as _FDE */
282     {"_HID", AcpiNsRepair_HID},
283     {"_PRT", AcpiNsRepair_PRT},
284     {"_PSS", AcpiNsRepair_PSS},
285     {"_TSS", AcpiNsRepair_TSS},
286     {{0,0,0,0}, NULL}               /* Table terminator */
287 };
288 
289 
290 #define ACPI_FDE_FIELD_COUNT        5
291 #define ACPI_FDE_BYTE_BUFFER_SIZE   5
292 #define ACPI_FDE_DWORD_BUFFER_SIZE  (ACPI_FDE_FIELD_COUNT * sizeof (UINT32))
293 
294 
295 /******************************************************************************
296  *
297  * FUNCTION:    AcpiNsComplexRepairs
298  *
299  * PARAMETERS:  Info                - Method execution information block
300  *              Node                - Namespace node for the method/object
301  *              ValidateStatus      - Original status of earlier validation
302  *              ReturnObjectPtr     - Pointer to the object returned from the
303  *                                    evaluation of a method or object
304  *
305  * RETURN:      Status. AE_OK if repair was successful. If name is not
306  *              matched, ValidateStatus is returned.
307  *
308  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
309  *              not expected.
310  *
311  *****************************************************************************/
312 
313 ACPI_STATUS
314 AcpiNsComplexRepairs (
315     ACPI_EVALUATE_INFO      *Info,
316     ACPI_NAMESPACE_NODE     *Node,
317     ACPI_STATUS             ValidateStatus,
318     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
319 {
320     const ACPI_REPAIR_INFO  *Predefined;
321     ACPI_STATUS             Status;
322 
323 
324     /* Check if this name is in the list of repairable names */
325 
326     Predefined = AcpiNsMatchComplexRepair (Node);
327     if (!Predefined)
328     {
329         return (ValidateStatus);
330     }
331 
332     Status = Predefined->RepairFunction (Info, ReturnObjectPtr);
333     return (Status);
334 }
335 
336 
337 /******************************************************************************
338  *
339  * FUNCTION:    AcpiNsMatchComplexRepair
340  *
341  * PARAMETERS:  Node                - Namespace node for the method/object
342  *
343  * RETURN:      Pointer to entry in repair table. NULL indicates not found.
344  *
345  * DESCRIPTION: Check an object name against the repairable object list.
346  *
347  *****************************************************************************/
348 
349 static const ACPI_REPAIR_INFO *
350 AcpiNsMatchComplexRepair (
351     ACPI_NAMESPACE_NODE     *Node)
352 {
353     const ACPI_REPAIR_INFO  *ThisName;
354 
355 
356     /* Search info table for a repairable predefined method/object name */
357 
358     ThisName = AcpiNsRepairableNames;
359     while (ThisName->RepairFunction)
360     {
361         if (ACPI_COMPARE_NAME (Node->Name.Ascii, ThisName->Name))
362         {
363             return (ThisName);
364         }
365 
366         ThisName++;
367     }
368 
369     return (NULL); /* Not found */
370 }
371 
372 
373 /******************************************************************************
374  *
375  * FUNCTION:    AcpiNsRepair_ALR
376  *
377  * PARAMETERS:  Info                - Method execution information block
378  *              ReturnObjectPtr     - Pointer to the object returned from the
379  *                                    evaluation of a method or object
380  *
381  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
382  *
383  * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
384  *              ascending by the ambient illuminance values.
385  *
386  *****************************************************************************/
387 
388 static ACPI_STATUS
389 AcpiNsRepair_ALR (
390     ACPI_EVALUATE_INFO      *Info,
391     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
392 {
393     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
394     ACPI_STATUS             Status;
395 
396 
397     Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 2, 1,
398         ACPI_SORT_ASCENDING, "AmbientIlluminance");
399 
400     return (Status);
401 }
402 
403 
404 /******************************************************************************
405  *
406  * FUNCTION:    AcpiNsRepair_FDE
407  *
408  * PARAMETERS:  Info                - Method execution information block
409  *              ReturnObjectPtr     - Pointer to the object returned from the
410  *                                    evaluation of a method or object
411  *
412  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
413  *
414  * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
415  *              value is a Buffer of 5 DWORDs. This function repairs a common
416  *              problem where the return value is a Buffer of BYTEs, not
417  *              DWORDs.
418  *
419  *****************************************************************************/
420 
421 static ACPI_STATUS
422 AcpiNsRepair_FDE (
423     ACPI_EVALUATE_INFO      *Info,
424     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
425 {
426     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
427     ACPI_OPERAND_OBJECT     *BufferObject;
428     UINT8                   *ByteBuffer;
429     UINT32                  *DwordBuffer;
430     UINT32                  i;
431 
432 
433     ACPI_FUNCTION_NAME (NsRepair_FDE);
434 
435 
436     switch (ReturnObject->Common.Type)
437     {
438     case ACPI_TYPE_BUFFER:
439 
440         /* This is the expected type. Length should be (at least) 5 DWORDs */
441 
442         if (ReturnObject->Buffer.Length >= ACPI_FDE_DWORD_BUFFER_SIZE)
443         {
444             return (AE_OK);
445         }
446 
447         /* We can only repair if we have exactly 5 BYTEs */
448 
449         if (ReturnObject->Buffer.Length != ACPI_FDE_BYTE_BUFFER_SIZE)
450         {
451             ACPI_WARN_PREDEFINED ((AE_INFO,
452                 Info->FullPathname, Info->NodeFlags,
453                 "Incorrect return buffer length %u, expected %u",
454                 ReturnObject->Buffer.Length, ACPI_FDE_DWORD_BUFFER_SIZE));
455 
456             return (AE_AML_OPERAND_TYPE);
457         }
458 
459         /* Create the new (larger) buffer object */
460 
461         BufferObject = AcpiUtCreateBufferObject (
462             ACPI_FDE_DWORD_BUFFER_SIZE);
463         if (!BufferObject)
464         {
465             return (AE_NO_MEMORY);
466         }
467 
468         /* Expand each byte to a DWORD */
469 
470         ByteBuffer = ReturnObject->Buffer.Pointer;
471         DwordBuffer = ACPI_CAST_PTR (UINT32,
472             BufferObject->Buffer.Pointer);
473 
474         for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++)
475         {
476             *DwordBuffer = (UINT32) *ByteBuffer;
477             DwordBuffer++;
478             ByteBuffer++;
479         }
480 
481         ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
482             "%s Expanded Byte Buffer to expected DWord Buffer\n",
483             Info->FullPathname));
484         break;
485 
486     default:
487 
488         return (AE_AML_OPERAND_TYPE);
489     }
490 
491     /* Delete the original return object, return the new buffer object */
492 
493     AcpiUtRemoveReference (ReturnObject);
494     *ReturnObjectPtr = BufferObject;
495 
496     Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
497     return (AE_OK);
498 }
499 
500 
501 /******************************************************************************
502  *
503  * FUNCTION:    AcpiNsRepair_CID
504  *
505  * PARAMETERS:  Info                - Method execution information block
506  *              ReturnObjectPtr     - Pointer to the object returned from the
507  *                                    evaluation of a method or object
508  *
509  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
510  *
511  * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
512  *              letters are uppercase and that there is no leading asterisk.
513  *              If a Package, ensure same for all string elements.
514  *
515  *****************************************************************************/
516 
517 static ACPI_STATUS
518 AcpiNsRepair_CID (
519     ACPI_EVALUATE_INFO      *Info,
520     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
521 {
522     ACPI_STATUS             Status;
523     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
524     ACPI_OPERAND_OBJECT     **ElementPtr;
525     ACPI_OPERAND_OBJECT     *OriginalElement;
526     UINT16                  OriginalRefCount;
527     UINT32                  i;
528 
529 
530     /* Check for _CID as a simple string */
531 
532     if (ReturnObject->Common.Type == ACPI_TYPE_STRING)
533     {
534         Status = AcpiNsRepair_HID (Info, ReturnObjectPtr);
535         return (Status);
536     }
537 
538     /* Exit if not a Package */
539 
540     if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
541     {
542         return (AE_OK);
543     }
544 
545     /* Examine each element of the _CID package */
546 
547     ElementPtr = ReturnObject->Package.Elements;
548     for (i = 0; i < ReturnObject->Package.Count; i++)
549     {
550         OriginalElement = *ElementPtr;
551         OriginalRefCount = OriginalElement->Common.ReferenceCount;
552 
553         Status = AcpiNsRepair_HID (Info, ElementPtr);
554         if (ACPI_FAILURE (Status))
555         {
556             return (Status);
557         }
558 
559         if (OriginalElement != *ElementPtr)
560         {
561             /* Update reference count of new object */
562 
563             (*ElementPtr)->Common.ReferenceCount =
564                 OriginalRefCount;
565         }
566 
567         ElementPtr++;
568     }
569 
570     return (AE_OK);
571 }
572 
573 
574 /******************************************************************************
575  *
576  * FUNCTION:    AcpiNsRepair_CST
577  *
578  * PARAMETERS:  Info                - Method execution information block
579  *              ReturnObjectPtr     - Pointer to the object returned from the
580  *                                    evaluation of a method or object
581  *
582  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
583  *
584  * DESCRIPTION: Repair for the _CST object:
585  *              1. Sort the list ascending by C state type
586  *              2. Ensure type cannot be zero
587  *              3. A subpackage count of zero means _CST is meaningless
588  *              4. Count must match the number of C state subpackages
589  *
590  *****************************************************************************/
591 
592 static ACPI_STATUS
593 AcpiNsRepair_CST (
594     ACPI_EVALUATE_INFO      *Info,
595     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
596 {
597     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
598     ACPI_OPERAND_OBJECT     **OuterElements;
599     UINT32                  OuterElementCount;
600     ACPI_OPERAND_OBJECT     *ObjDesc;
601     ACPI_STATUS             Status;
602     BOOLEAN                 Removing;
603     UINT32                  i;
604 
605 
606     ACPI_FUNCTION_NAME (NsRepair_CST);
607 
608 
609     /*
610      * Check if the C-state type values are proportional.
611      */
612     OuterElementCount = ReturnObject->Package.Count - 1;
613     i = 0;
614     while (i < OuterElementCount)
615     {
616         OuterElements = &ReturnObject->Package.Elements[i + 1];
617         Removing = FALSE;
618 
619         if ((*OuterElements)->Package.Count == 0)
620         {
621             ACPI_WARN_PREDEFINED ((AE_INFO,
622                 Info->FullPathname, Info->NodeFlags,
623                 "SubPackage[%u] - removing entry due to zero count", i));
624             Removing = TRUE;
625             goto RemoveElement;
626         }
627 
628         ObjDesc = (*OuterElements)->Package.Elements[1]; /* Index1 = Type */
629         if ((UINT32) ObjDesc->Integer.Value == 0)
630         {
631             ACPI_WARN_PREDEFINED ((AE_INFO,
632                 Info->FullPathname, Info->NodeFlags,
633                 "SubPackage[%u] - removing entry due to invalid Type(0)", i));
634             Removing = TRUE;
635         }
636 
637 RemoveElement:
638         if (Removing)
639         {
640             AcpiNsRemoveElement (ReturnObject, i + 1);
641             OuterElementCount--;
642         }
643         else
644         {
645             i++;
646         }
647     }
648 
649     /* Update top-level package count, Type "Integer" checked elsewhere */
650 
651     ObjDesc = ReturnObject->Package.Elements[0];
652     ObjDesc->Integer.Value = OuterElementCount;
653 
654     /*
655      * Entries (subpackages) in the _CST Package must be sorted by the
656      * C-state type, in ascending order.
657      */
658     Status = AcpiNsCheckSortedList (Info, ReturnObject, 1, 4, 1,
659         ACPI_SORT_ASCENDING, "C-State Type");
660     if (ACPI_FAILURE (Status))
661     {
662         return (Status);
663     }
664 
665     return (AE_OK);
666 }
667 
668 
669 /******************************************************************************
670  *
671  * FUNCTION:    AcpiNsRepair_HID
672  *
673  * PARAMETERS:  Info                - Method execution information block
674  *              ReturnObjectPtr     - Pointer to the object returned from the
675  *                                    evaluation of a method or object
676  *
677  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
678  *
679  * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
680  *              letters are uppercase and that there is no leading asterisk.
681  *
682  *****************************************************************************/
683 
684 static ACPI_STATUS
685 AcpiNsRepair_HID (
686     ACPI_EVALUATE_INFO      *Info,
687     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
688 {
689     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
690     ACPI_OPERAND_OBJECT     *NewString;
691     char                    *Source;
692     char                    *Dest;
693 
694 
695     ACPI_FUNCTION_NAME (NsRepair_HID);
696 
697 
698     /* We only care about string _HID objects (not integers) */
699 
700     if (ReturnObject->Common.Type != ACPI_TYPE_STRING)
701     {
702         return (AE_OK);
703     }
704 
705     if (ReturnObject->String.Length == 0)
706     {
707         ACPI_WARN_PREDEFINED ((AE_INFO,
708             Info->FullPathname, Info->NodeFlags,
709             "Invalid zero-length _HID or _CID string"));
710 
711         /* Return AE_OK anyway, let driver handle it */
712 
713         Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
714         return (AE_OK);
715     }
716 
717     /* It is simplest to always create a new string object */
718 
719     NewString = AcpiUtCreateStringObject (ReturnObject->String.Length);
720     if (!NewString)
721     {
722         return (AE_NO_MEMORY);
723     }
724 
725     /*
726      * Remove a leading asterisk if present. For some unknown reason, there
727      * are many machines in the field that contains IDs like this.
728      *
729      * Examples: "*PNP0C03", "*ACPI0003"
730      */
731     Source = ReturnObject->String.Pointer;
732     if (*Source == '*')
733     {
734         Source++;
735         NewString->String.Length--;
736 
737         ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
738             "%s: Removed invalid leading asterisk\n", Info->FullPathname));
739     }
740 
741     /*
742      * Copy and uppercase the string. From the ACPI 5.0 specification:
743      *
744      * A valid PNP ID must be of the form "AAA####" where A is an uppercase
745      * letter and # is a hex digit. A valid ACPI ID must be of the form
746      * "NNNN####" where N is an uppercase letter or decimal digit, and
747      * # is a hex digit.
748      */
749     for (Dest = NewString->String.Pointer; *Source; Dest++, Source++)
750     {
751         *Dest = (char) toupper ((int) *Source);
752     }
753 
754     AcpiUtRemoveReference (ReturnObject);
755     *ReturnObjectPtr = NewString;
756     return (AE_OK);
757 }
758 
759 
760 /******************************************************************************
761  *
762  * FUNCTION:    AcpiNsRepair_PRT
763  *
764  * PARAMETERS:  Info                - Method execution information block
765  *              ReturnObjectPtr     - Pointer to the object returned from the
766  *                                    evaluation of a method or object
767  *
768  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
769  *
770  * DESCRIPTION: Repair for the _PRT object. If necessary, fix reversed
771  *              SourceName and SourceIndex field, a common BIOS bug.
772  *
773  *****************************************************************************/
774 
775 static ACPI_STATUS
776 AcpiNsRepair_PRT (
777     ACPI_EVALUATE_INFO      *Info,
778     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
779 {
780     ACPI_OPERAND_OBJECT     *PackageObject = *ReturnObjectPtr;
781     ACPI_OPERAND_OBJECT     **TopObjectList;
782     ACPI_OPERAND_OBJECT     **SubObjectList;
783     ACPI_OPERAND_OBJECT     *ObjDesc;
784     ACPI_OPERAND_OBJECT     *SubPackage;
785     UINT32                  ElementCount;
786     UINT32                  Index;
787 
788 
789     /* Each element in the _PRT package is a subpackage */
790 
791     TopObjectList = PackageObject->Package.Elements;
792     ElementCount = PackageObject->Package.Count;
793 
794     /* Examine each subpackage */
795 
796     for (Index = 0; Index < ElementCount; Index++, TopObjectList++)
797     {
798         SubPackage = *TopObjectList;
799         SubObjectList = SubPackage->Package.Elements;
800 
801         /* Check for minimum required element count */
802 
803         if (SubPackage->Package.Count < 4)
804         {
805             continue;
806         }
807 
808         /*
809          * If the BIOS has erroneously reversed the _PRT SourceName (index 2)
810          * and the SourceIndex (index 3), fix it. _PRT is important enough to
811          * workaround this BIOS error. This also provides compatibility with
812          * other ACPI implementations.
813          */
814         ObjDesc = SubObjectList[3];
815         if (!ObjDesc || (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
816         {
817             SubObjectList[3] = SubObjectList[2];
818             SubObjectList[2] = ObjDesc;
819             Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
820 
821             ACPI_WARN_PREDEFINED ((AE_INFO,
822                 Info->FullPathname, Info->NodeFlags,
823                 "PRT[%X]: Fixed reversed SourceName and SourceIndex",
824                 Index));
825         }
826     }
827 
828     return (AE_OK);
829 }
830 
831 
832 /******************************************************************************
833  *
834  * FUNCTION:    AcpiNsRepair_PSS
835  *
836  * PARAMETERS:  Info                - Method execution information block
837  *              ReturnObjectPtr     - Pointer to the object returned from the
838  *                                    evaluation of a method or object
839  *
840  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
841  *
842  * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
843  *              by the CPU frequencies. Check that the power dissipation values
844  *              are all proportional to CPU frequency (i.e., sorting by
845  *              frequency should be the same as sorting by power.)
846  *
847  *****************************************************************************/
848 
849 static ACPI_STATUS
850 AcpiNsRepair_PSS (
851     ACPI_EVALUATE_INFO      *Info,
852     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
853 {
854     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
855     ACPI_OPERAND_OBJECT     **OuterElements;
856     UINT32                  OuterElementCount;
857     ACPI_OPERAND_OBJECT     **Elements;
858     ACPI_OPERAND_OBJECT     *ObjDesc;
859     UINT32                  PreviousValue;
860     ACPI_STATUS             Status;
861     UINT32                  i;
862 
863 
864     /*
865      * Entries (subpackages) in the _PSS Package must be sorted by power
866      * dissipation, in descending order. If it appears that the list is
867      * incorrectly sorted, sort it. We sort by CpuFrequency, since this
868      * should be proportional to the power.
869      */
870     Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 6, 0,
871         ACPI_SORT_DESCENDING, "CpuFrequency");
872     if (ACPI_FAILURE (Status))
873     {
874         return (Status);
875     }
876 
877     /*
878      * We now know the list is correctly sorted by CPU frequency. Check if
879      * the power dissipation values are proportional.
880      */
881     PreviousValue = ACPI_UINT32_MAX;
882     OuterElements = ReturnObject->Package.Elements;
883     OuterElementCount = ReturnObject->Package.Count;
884 
885     for (i = 0; i < OuterElementCount; i++)
886     {
887         Elements = (*OuterElements)->Package.Elements;
888         ObjDesc = Elements[1]; /* Index1 = PowerDissipation */
889 
890         if ((UINT32) ObjDesc->Integer.Value > PreviousValue)
891         {
892             ACPI_WARN_PREDEFINED ((AE_INFO,
893                 Info->FullPathname, Info->NodeFlags,
894                 "SubPackage[%u,%u] - suspicious power dissipation values",
895                 i-1, i));
896         }
897 
898         PreviousValue = (UINT32) ObjDesc->Integer.Value;
899         OuterElements++;
900     }
901 
902     return (AE_OK);
903 }
904 
905 
906 /******************************************************************************
907  *
908  * FUNCTION:    AcpiNsRepair_TSS
909  *
910  * PARAMETERS:  Info                - Method execution information block
911  *              ReturnObjectPtr     - Pointer to the object returned from the
912  *                                    evaluation of a method or object
913  *
914  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
915  *
916  * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
917  *              descending by the power dissipation values.
918  *
919  *****************************************************************************/
920 
921 static ACPI_STATUS
922 AcpiNsRepair_TSS (
923     ACPI_EVALUATE_INFO      *Info,
924     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
925 {
926     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
927     ACPI_STATUS             Status;
928     ACPI_NAMESPACE_NODE     *Node;
929 
930 
931     /*
932      * We can only sort the _TSS return package if there is no _PSS in the
933      * same scope. This is because if _PSS is present, the ACPI specification
934      * dictates that the _TSS Power Dissipation field is to be ignored, and
935      * therefore some BIOSs leave garbage values in the _TSS Power field(s).
936      * In this case, it is best to just return the _TSS package as-is.
937      * (May, 2011)
938      */
939     Status = AcpiNsGetNode (Info->Node, "^_PSS",
940         ACPI_NS_NO_UPSEARCH, &Node);
941     if (ACPI_SUCCESS (Status))
942     {
943         return (AE_OK);
944     }
945 
946     Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 5, 1,
947         ACPI_SORT_DESCENDING, "PowerDissipation");
948 
949     return (Status);
950 }
951 
952 
953 /******************************************************************************
954  *
955  * FUNCTION:    AcpiNsCheckSortedList
956  *
957  * PARAMETERS:  Info                - Method execution information block
958  *              ReturnObject        - Pointer to the top-level returned object
959  *              StartIndex          - Index of the first subpackage
960  *              ExpectedCount       - Minimum length of each subpackage
961  *              SortIndex           - Subpackage entry to sort on
962  *              SortDirection       - Ascending or descending
963  *              SortKeyName         - Name of the SortIndex field
964  *
965  * RETURN:      Status. AE_OK if the list is valid and is sorted correctly or
966  *              has been repaired by sorting the list.
967  *
968  * DESCRIPTION: Check if the package list is valid and sorted correctly by the
969  *              SortIndex. If not, then sort the list.
970  *
971  *****************************************************************************/
972 
973 static ACPI_STATUS
974 AcpiNsCheckSortedList (
975     ACPI_EVALUATE_INFO      *Info,
976     ACPI_OPERAND_OBJECT     *ReturnObject,
977     UINT32                  StartIndex,
978     UINT32                  ExpectedCount,
979     UINT32                  SortIndex,
980     UINT8                   SortDirection,
981     char                    *SortKeyName)
982 {
983     UINT32                  OuterElementCount;
984     ACPI_OPERAND_OBJECT     **OuterElements;
985     ACPI_OPERAND_OBJECT     **Elements;
986     ACPI_OPERAND_OBJECT     *ObjDesc;
987     UINT32                  i;
988     UINT32                  PreviousValue;
989 
990 
991     ACPI_FUNCTION_NAME (NsCheckSortedList);
992 
993 
994     /* The top-level object must be a package */
995 
996     if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
997     {
998         return (AE_AML_OPERAND_TYPE);
999     }
1000 
1001     /*
1002      * NOTE: assumes list of subpackages contains no NULL elements.
1003      * Any NULL elements should have been removed by earlier call
1004      * to AcpiNsRemoveNullElements.
1005      */
1006     OuterElementCount = ReturnObject->Package.Count;
1007     if (!OuterElementCount || StartIndex >= OuterElementCount)
1008     {
1009         return (AE_AML_PACKAGE_LIMIT);
1010     }
1011 
1012     OuterElements = &ReturnObject->Package.Elements[StartIndex];
1013     OuterElementCount -= StartIndex;
1014 
1015     PreviousValue = 0;
1016     if (SortDirection == ACPI_SORT_DESCENDING)
1017     {
1018         PreviousValue = ACPI_UINT32_MAX;
1019     }
1020 
1021     /* Examine each subpackage */
1022 
1023     for (i = 0; i < OuterElementCount; i++)
1024     {
1025         /* Each element of the top-level package must also be a package */
1026 
1027         if ((*OuterElements)->Common.Type != ACPI_TYPE_PACKAGE)
1028         {
1029             return (AE_AML_OPERAND_TYPE);
1030         }
1031 
1032         /* Each subpackage must have the minimum length */
1033 
1034         if ((*OuterElements)->Package.Count < ExpectedCount)
1035         {
1036             return (AE_AML_PACKAGE_LIMIT);
1037         }
1038 
1039         Elements = (*OuterElements)->Package.Elements;
1040         ObjDesc = Elements[SortIndex];
1041 
1042         if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
1043         {
1044             return (AE_AML_OPERAND_TYPE);
1045         }
1046 
1047         /*
1048          * The list must be sorted in the specified order. If we detect a
1049          * discrepancy, sort the entire list.
1050          */
1051         if (((SortDirection == ACPI_SORT_ASCENDING) &&
1052                 (ObjDesc->Integer.Value < PreviousValue)) ||
1053             ((SortDirection == ACPI_SORT_DESCENDING) &&
1054                 (ObjDesc->Integer.Value > PreviousValue)))
1055         {
1056             AcpiNsSortList (&ReturnObject->Package.Elements[StartIndex],
1057                 OuterElementCount, SortIndex, SortDirection);
1058 
1059             Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
1060 
1061             ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
1062                 "%s: Repaired unsorted list - now sorted by %s\n",
1063                 Info->FullPathname, SortKeyName));
1064             return (AE_OK);
1065         }
1066 
1067         PreviousValue = (UINT32) ObjDesc->Integer.Value;
1068         OuterElements++;
1069     }
1070 
1071     return (AE_OK);
1072 }
1073 
1074 
1075 /******************************************************************************
1076  *
1077  * FUNCTION:    AcpiNsSortList
1078  *
1079  * PARAMETERS:  Elements            - Package object element list
1080  *              Count               - Element count for above
1081  *              Index               - Sort by which package element
1082  *              SortDirection       - Ascending or Descending sort
1083  *
1084  * RETURN:      None
1085  *
1086  * DESCRIPTION: Sort the objects that are in a package element list.
1087  *
1088  * NOTE: Assumes that all NULL elements have been removed from the package,
1089  *       and that all elements have been verified to be of type Integer.
1090  *
1091  *****************************************************************************/
1092 
1093 static void
1094 AcpiNsSortList (
1095     ACPI_OPERAND_OBJECT     **Elements,
1096     UINT32                  Count,
1097     UINT32                  Index,
1098     UINT8                   SortDirection)
1099 {
1100     ACPI_OPERAND_OBJECT     *ObjDesc1;
1101     ACPI_OPERAND_OBJECT     *ObjDesc2;
1102     ACPI_OPERAND_OBJECT     *TempObj;
1103     UINT32                  i;
1104     UINT32                  j;
1105 
1106 
1107     /* Simple bubble sort */
1108 
1109     for (i = 1; i < Count; i++)
1110     {
1111         for (j = (Count - 1); j >= i; j--)
1112         {
1113             ObjDesc1 = Elements[j-1]->Package.Elements[Index];
1114             ObjDesc2 = Elements[j]->Package.Elements[Index];
1115 
1116             if (((SortDirection == ACPI_SORT_ASCENDING) &&
1117                     (ObjDesc1->Integer.Value > ObjDesc2->Integer.Value)) ||
1118 
1119                 ((SortDirection == ACPI_SORT_DESCENDING) &&
1120                     (ObjDesc1->Integer.Value < ObjDesc2->Integer.Value)))
1121             {
1122                 TempObj = Elements[j-1];
1123                 Elements[j-1] = Elements[j];
1124                 Elements[j] = TempObj;
1125             }
1126         }
1127     }
1128 }
1129 
1130 
1131 /******************************************************************************
1132  *
1133  * FUNCTION:    AcpiNsRemoveElement
1134  *
1135  * PARAMETERS:  ObjDesc             - Package object element list
1136  *              Index               - Index of element to remove
1137  *
1138  * RETURN:      None
1139  *
1140  * DESCRIPTION: Remove the requested element of a package and delete it.
1141  *
1142  *****************************************************************************/
1143 
1144 static void
1145 AcpiNsRemoveElement (
1146     ACPI_OPERAND_OBJECT     *ObjDesc,
1147     UINT32                  Index)
1148 {
1149     ACPI_OPERAND_OBJECT     **Source;
1150     ACPI_OPERAND_OBJECT     **Dest;
1151     UINT32                  Count;
1152     UINT32                  NewCount;
1153     UINT32                  i;
1154 
1155 
1156     ACPI_FUNCTION_NAME (NsRemoveElement);
1157 
1158 
1159     Count = ObjDesc->Package.Count;
1160     NewCount = Count - 1;
1161 
1162     Source = ObjDesc->Package.Elements;
1163     Dest = Source;
1164 
1165     /* Examine all elements of the package object, remove matched index */
1166 
1167     for (i = 0; i < Count; i++)
1168     {
1169         if (i == Index)
1170         {
1171             AcpiUtRemoveReference (*Source); /* Remove one ref for being in pkg */
1172             AcpiUtRemoveReference (*Source);
1173         }
1174         else
1175         {
1176             *Dest = *Source;
1177             Dest++;
1178         }
1179 
1180         Source++;
1181     }
1182 
1183     /* NULL terminate list and update the package count */
1184 
1185     *Dest = NULL;
1186     ObjDesc->Package.Count = NewCount;
1187 }
1188