1 /******************************************************************************
2  *
3  * Module Name: nsrepair2 - Repair for objects returned by specific
4  *                          predefined methods
5  *
6  *****************************************************************************/
7 
8 /*
9  * Copyright (C) 2000 - 2020, Intel Corp.
10  * All rights reserved.
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions
14  * are met:
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.
26  *
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.
30  *
31  * NO WARRANTY
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.
43  */
44 
45 #include "acpi.h"
46 #include "accommon.h"
47 #include "acnamesp.h"
48 
49 #define _COMPONENT          ACPI_NAMESPACE
50         ACPI_MODULE_NAME    ("nsrepair2")
51 
52 
53 /*
54  * Information structure and handler for ACPI predefined names that can
55  * be repaired on a per-name basis.
56  */
57 typedef
58 ACPI_STATUS (*ACPI_REPAIR_FUNCTION) (
59     ACPI_EVALUATE_INFO      *Info,
60     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
61 
62 typedef struct acpi_repair_info
63 {
64     char                    Name[ACPI_NAMESEG_SIZE];
65     ACPI_REPAIR_FUNCTION    RepairFunction;
66 
67 } ACPI_REPAIR_INFO;
68 
69 
70 /* Local prototypes */
71 
72 static const ACPI_REPAIR_INFO *
73 AcpiNsMatchComplexRepair (
74     ACPI_NAMESPACE_NODE     *Node);
75 
76 static ACPI_STATUS
77 AcpiNsRepair_ALR (
78     ACPI_EVALUATE_INFO      *Info,
79     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
80 
81 static ACPI_STATUS
82 AcpiNsRepair_CID (
83     ACPI_EVALUATE_INFO      *Info,
84     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
85 
86 static ACPI_STATUS
87 AcpiNsRepair_CST (
88     ACPI_EVALUATE_INFO      *Info,
89     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
90 
91 static ACPI_STATUS
92 AcpiNsRepair_FDE (
93     ACPI_EVALUATE_INFO      *Info,
94     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
95 
96 static ACPI_STATUS
97 AcpiNsRepair_HID (
98     ACPI_EVALUATE_INFO      *Info,
99     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
100 
101 static ACPI_STATUS
102 AcpiNsRepair_PRT (
103     ACPI_EVALUATE_INFO      *Info,
104     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
105 
106 static ACPI_STATUS
107 AcpiNsRepair_PSS (
108     ACPI_EVALUATE_INFO      *Info,
109     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
110 
111 static ACPI_STATUS
112 AcpiNsRepair_TSS (
113     ACPI_EVALUATE_INFO      *Info,
114     ACPI_OPERAND_OBJECT     **ReturnObjectPtr);
115 
116 static ACPI_STATUS
117 AcpiNsCheckSortedList (
118     ACPI_EVALUATE_INFO      *Info,
119     ACPI_OPERAND_OBJECT     *ReturnObject,
120     UINT32                  StartIndex,
121     UINT32                  ExpectedCount,
122     UINT32                  SortIndex,
123     UINT8                   SortDirection,
124     char                    *SortKeyName);
125 
126 /* Values for SortDirection above */
127 
128 #define ACPI_SORT_ASCENDING     0
129 #define ACPI_SORT_DESCENDING    1
130 
131 static void
132 AcpiNsRemoveElement (
133     ACPI_OPERAND_OBJECT     *ObjDesc,
134     UINT32                  Index);
135 
136 static void
137 AcpiNsSortList (
138     ACPI_OPERAND_OBJECT     **Elements,
139     UINT32                  Count,
140     UINT32                  Index,
141     UINT8                   SortDirection);
142 
143 
144 /*
145  * This table contains the names of the predefined methods for which we can
146  * perform more complex repairs.
147  *
148  * As necessary:
149  *
150  * _ALR: Sort the list ascending by AmbientIlluminance
151  * _CID: Strings: uppercase all, remove any leading asterisk
152  * _CST: Sort the list ascending by C state type
153  * _FDE: Convert Buffer of BYTEs to a Buffer of DWORDs
154  * _GTM: Convert Buffer of BYTEs to a Buffer of DWORDs
155  * _HID: Strings: uppercase all, remove any leading asterisk
156  * _PRT: Fix reversed SourceName and SourceIndex
157  * _PSS: Sort the list descending by Power
158  * _TSS: Sort the list descending by Power
159  *
160  * Names that must be packages, but cannot be sorted:
161  *
162  * _BCL: Values are tied to the Package index where they appear, and cannot
163  * be moved or sorted. These index values are used for _BQC and _BCM.
164  * However, we can fix the case where a buffer is returned, by converting
165  * it to a Package of integers.
166  */
167 static const ACPI_REPAIR_INFO       AcpiNsRepairableNames[] =
168 {
169     {"_ALR", AcpiNsRepair_ALR},
170     {"_CID", AcpiNsRepair_CID},
171     {"_CST", AcpiNsRepair_CST},
172     {"_FDE", AcpiNsRepair_FDE},
173     {"_GTM", AcpiNsRepair_FDE},     /* _GTM has same repair as _FDE */
174     {"_HID", AcpiNsRepair_HID},
175     {"_PRT", AcpiNsRepair_PRT},
176     {"_PSS", AcpiNsRepair_PSS},
177     {"_TSS", AcpiNsRepair_TSS},
178     {{0,0,0,0}, NULL}               /* Table terminator */
179 };
180 
181 
182 #define ACPI_FDE_FIELD_COUNT        5
183 #define ACPI_FDE_BYTE_BUFFER_SIZE   5
184 #define ACPI_FDE_DWORD_BUFFER_SIZE  (ACPI_FDE_FIELD_COUNT * (UINT32) sizeof (UINT32))
185 
186 
187 /******************************************************************************
188  *
189  * FUNCTION:    AcpiNsComplexRepairs
190  *
191  * PARAMETERS:  Info                - Method execution information block
192  *              Node                - Namespace node for the method/object
193  *              ValidateStatus      - Original status of earlier validation
194  *              ReturnObjectPtr     - Pointer to the object returned from the
195  *                                    evaluation of a method or object
196  *
197  * RETURN:      Status. AE_OK if repair was successful. If name is not
198  *              matched, ValidateStatus is returned.
199  *
200  * DESCRIPTION: Attempt to repair/convert a return object of a type that was
201  *              not expected.
202  *
203  *****************************************************************************/
204 
205 ACPI_STATUS
206 AcpiNsComplexRepairs (
207     ACPI_EVALUATE_INFO      *Info,
208     ACPI_NAMESPACE_NODE     *Node,
209     ACPI_STATUS             ValidateStatus,
210     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
211 {
212     const ACPI_REPAIR_INFO  *Predefined;
213     ACPI_STATUS             Status;
214 
215 
216     ACPI_FUNCTION_TRACE (NsComplexRepairs);
217 
218     /* Check if this name is in the list of repairable names */
219 
220     Predefined = AcpiNsMatchComplexRepair (Node);
221     if (!Predefined)
222     {
223         return_ACPI_STATUS (ValidateStatus);
224     }
225 
226     Status = Predefined->RepairFunction (Info, ReturnObjectPtr);
227     return_ACPI_STATUS (Status);
228 }
229 
230 
231 /******************************************************************************
232  *
233  * FUNCTION:    AcpiNsMatchComplexRepair
234  *
235  * PARAMETERS:  Node                - Namespace node for the method/object
236  *
237  * RETURN:      Pointer to entry in repair table. NULL indicates not found.
238  *
239  * DESCRIPTION: Check an object name against the repairable object list.
240  *
241  *****************************************************************************/
242 
243 static const ACPI_REPAIR_INFO *
244 AcpiNsMatchComplexRepair (
245     ACPI_NAMESPACE_NODE     *Node)
246 {
247     const ACPI_REPAIR_INFO  *ThisName;
248 
249 
250     /* Search info table for a repairable predefined method/object name */
251 
252     ThisName = AcpiNsRepairableNames;
253     while (ThisName->RepairFunction)
254     {
255         if (ACPI_COMPARE_NAMESEG (Node->Name.Ascii, ThisName->Name))
256         {
257             return (ThisName);
258         }
259 
260         ThisName++;
261     }
262 
263     return (NULL); /* Not found */
264 }
265 
266 
267 /******************************************************************************
268  *
269  * FUNCTION:    AcpiNsRepair_ALR
270  *
271  * PARAMETERS:  Info                - Method execution information block
272  *              ReturnObjectPtr     - Pointer to the object returned from the
273  *                                    evaluation of a method or object
274  *
275  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
276  *
277  * DESCRIPTION: Repair for the _ALR object. If necessary, sort the object list
278  *              ascending by the ambient illuminance values.
279  *
280  *****************************************************************************/
281 
282 static ACPI_STATUS
283 AcpiNsRepair_ALR (
284     ACPI_EVALUATE_INFO      *Info,
285     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
286 {
287     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
288     ACPI_STATUS             Status;
289 
290 
291     Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 2, 1,
292         ACPI_SORT_ASCENDING, "AmbientIlluminance");
293 
294     return (Status);
295 }
296 
297 
298 /******************************************************************************
299  *
300  * FUNCTION:    AcpiNsRepair_FDE
301  *
302  * PARAMETERS:  Info                - Method execution information block
303  *              ReturnObjectPtr     - Pointer to the object returned from the
304  *                                    evaluation of a method or object
305  *
306  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
307  *
308  * DESCRIPTION: Repair for the _FDE and _GTM objects. The expected return
309  *              value is a Buffer of 5 DWORDs. This function repairs a common
310  *              problem where the return value is a Buffer of BYTEs, not
311  *              DWORDs.
312  *
313  *****************************************************************************/
314 
315 static ACPI_STATUS
316 AcpiNsRepair_FDE (
317     ACPI_EVALUATE_INFO      *Info,
318     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
319 {
320     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
321     ACPI_OPERAND_OBJECT     *BufferObject;
322     UINT8                   *ByteBuffer;
323     UINT32                  *DwordBuffer;
324     UINT32                  i;
325 
326 
327     ACPI_FUNCTION_NAME (NsRepair_FDE);
328 
329 
330     switch (ReturnObject->Common.Type)
331     {
332     case ACPI_TYPE_BUFFER:
333 
334         /* This is the expected type. Length should be (at least) 5 DWORDs */
335 
336         if (ReturnObject->Buffer.Length >= ACPI_FDE_DWORD_BUFFER_SIZE)
337         {
338             return (AE_OK);
339         }
340 
341         /* We can only repair if we have exactly 5 BYTEs */
342 
343         if (ReturnObject->Buffer.Length != ACPI_FDE_BYTE_BUFFER_SIZE)
344         {
345             ACPI_WARN_PREDEFINED ((AE_INFO,
346                 Info->FullPathname, Info->NodeFlags,
347                 "Incorrect return buffer length %u, expected %u",
348                 ReturnObject->Buffer.Length, ACPI_FDE_DWORD_BUFFER_SIZE));
349 
350             return (AE_AML_OPERAND_TYPE);
351         }
352 
353         /* Create the new (larger) buffer object */
354 
355         BufferObject = AcpiUtCreateBufferObject (
356             ACPI_FDE_DWORD_BUFFER_SIZE);
357         if (!BufferObject)
358         {
359             return (AE_NO_MEMORY);
360         }
361 
362         /* Expand each byte to a DWORD */
363 
364         ByteBuffer = ReturnObject->Buffer.Pointer;
365         DwordBuffer = ACPI_CAST_PTR (UINT32,
366             BufferObject->Buffer.Pointer);
367 
368         for (i = 0; i < ACPI_FDE_FIELD_COUNT; i++)
369         {
370             *DwordBuffer = (UINT32) *ByteBuffer;
371             DwordBuffer++;
372             ByteBuffer++;
373         }
374 
375         ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
376             "%s Expanded Byte Buffer to expected DWord Buffer\n",
377             Info->FullPathname));
378         break;
379 
380     default:
381 
382         return (AE_AML_OPERAND_TYPE);
383     }
384 
385     /* Delete the original return object, return the new buffer object */
386 
387     AcpiUtRemoveReference (ReturnObject);
388     *ReturnObjectPtr = BufferObject;
389 
390     Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
391     return (AE_OK);
392 }
393 
394 
395 /******************************************************************************
396  *
397  * FUNCTION:    AcpiNsRepair_CID
398  *
399  * PARAMETERS:  Info                - Method execution information block
400  *              ReturnObjectPtr     - Pointer to the object returned from the
401  *                                    evaluation of a method or object
402  *
403  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
404  *
405  * DESCRIPTION: Repair for the _CID object. If a string, ensure that all
406  *              letters are uppercase and that there is no leading asterisk.
407  *              If a Package, ensure same for all string elements.
408  *
409  *****************************************************************************/
410 
411 static ACPI_STATUS
412 AcpiNsRepair_CID (
413     ACPI_EVALUATE_INFO      *Info,
414     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
415 {
416     ACPI_STATUS             Status;
417     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
418     ACPI_OPERAND_OBJECT     **ElementPtr;
419     ACPI_OPERAND_OBJECT     *OriginalElement;
420     UINT16                  OriginalRefCount;
421     UINT32                  i;
422 
423     ACPI_FUNCTION_TRACE (NsRepair_CID);
424 
425     /* Check for _CID as a simple string */
426 
427     if (ReturnObject->Common.Type == ACPI_TYPE_STRING)
428     {
429         Status = AcpiNsRepair_HID (Info, ReturnObjectPtr);
430         return_ACPI_STATUS (Status);
431     }
432 
433     /* Exit if not a Package */
434 
435     if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
436     {
437         return_ACPI_STATUS (AE_OK);
438     }
439 
440     /* Examine each element of the _CID package */
441 
442     ElementPtr = ReturnObject->Package.Elements;
443     for (i = 0; i < ReturnObject->Package.Count; i++)
444     {
445         OriginalElement = *ElementPtr;
446         OriginalRefCount = OriginalElement->Common.ReferenceCount;
447 
448         Status = AcpiNsRepair_HID (Info, ElementPtr);
449         if (ACPI_FAILURE (Status))
450         {
451             return_ACPI_STATUS (Status);
452         }
453 
454         if (OriginalElement != *ElementPtr)
455         {
456             /* Update reference count of new object */
457 
458             (*ElementPtr)->Common.ReferenceCount =
459                 OriginalRefCount;
460         }
461 
462         ElementPtr++;
463     }
464 
465     return_ACPI_STATUS (AE_OK);
466 }
467 
468 
469 /******************************************************************************
470  *
471  * FUNCTION:    AcpiNsRepair_CST
472  *
473  * PARAMETERS:  Info                - Method execution information block
474  *              ReturnObjectPtr     - Pointer to the object returned from the
475  *                                    evaluation of a method or object
476  *
477  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
478  *
479  * DESCRIPTION: Repair for the _CST object:
480  *              1. Sort the list ascending by C state type
481  *              2. Ensure type cannot be zero
482  *              3. A subpackage count of zero means _CST is meaningless
483  *              4. Count must match the number of C state subpackages
484  *
485  *****************************************************************************/
486 
487 static ACPI_STATUS
488 AcpiNsRepair_CST (
489     ACPI_EVALUATE_INFO      *Info,
490     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
491 {
492     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
493     ACPI_OPERAND_OBJECT     **OuterElements;
494     UINT32                  OuterElementCount;
495     ACPI_OPERAND_OBJECT     *ObjDesc;
496     ACPI_STATUS             Status;
497     BOOLEAN                 Removing;
498     UINT32                  i;
499 
500 
501     ACPI_FUNCTION_NAME (NsRepair_CST);
502 
503 
504     /*
505      * Check if the C-state type values are proportional.
506      */
507     OuterElementCount = ReturnObject->Package.Count - 1;
508     i = 0;
509     while (i < OuterElementCount)
510     {
511         OuterElements = &ReturnObject->Package.Elements[i + 1];
512         Removing = FALSE;
513 
514         if ((*OuterElements)->Package.Count == 0)
515         {
516             ACPI_WARN_PREDEFINED ((AE_INFO,
517                 Info->FullPathname, Info->NodeFlags,
518                 "SubPackage[%u] - removing entry due to zero count", i));
519             Removing = TRUE;
520             goto RemoveElement;
521         }
522 
523         ObjDesc = (*OuterElements)->Package.Elements[1]; /* Index1 = Type */
524         if ((UINT32) ObjDesc->Integer.Value == 0)
525         {
526             ACPI_WARN_PREDEFINED ((AE_INFO,
527                 Info->FullPathname, Info->NodeFlags,
528                 "SubPackage[%u] - removing entry due to invalid Type(0)", i));
529             Removing = TRUE;
530         }
531 
532 RemoveElement:
533         if (Removing)
534         {
535             AcpiNsRemoveElement (ReturnObject, i + 1);
536             OuterElementCount--;
537         }
538         else
539         {
540             i++;
541         }
542     }
543 
544     /* Update top-level package count, Type "Integer" checked elsewhere */
545 
546     ObjDesc = ReturnObject->Package.Elements[0];
547     ObjDesc->Integer.Value = OuterElementCount;
548 
549     /*
550      * Entries (subpackages) in the _CST Package must be sorted by the
551      * C-state type, in ascending order.
552      */
553     Status = AcpiNsCheckSortedList (Info, ReturnObject, 1, 4, 1,
554         ACPI_SORT_ASCENDING, "C-State Type");
555     if (ACPI_FAILURE (Status))
556     {
557         return (Status);
558     }
559 
560     return (AE_OK);
561 }
562 
563 
564 /******************************************************************************
565  *
566  * FUNCTION:    AcpiNsRepair_HID
567  *
568  * PARAMETERS:  Info                - Method execution information block
569  *              ReturnObjectPtr     - Pointer to the object returned from the
570  *                                    evaluation of a method or object
571  *
572  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
573  *
574  * DESCRIPTION: Repair for the _HID object. If a string, ensure that all
575  *              letters are uppercase and that there is no leading asterisk.
576  *
577  *****************************************************************************/
578 
579 static ACPI_STATUS
580 AcpiNsRepair_HID (
581     ACPI_EVALUATE_INFO      *Info,
582     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
583 {
584     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
585     char                    *Dest;
586     char                    *Source;
587 
588 
589     ACPI_FUNCTION_NAME (NsRepair_HID);
590 
591 
592     /* We only care about string _HID objects (not integers) */
593 
594     if (ReturnObject->Common.Type != ACPI_TYPE_STRING)
595     {
596         return_ACPI_STATUS (AE_OK);
597     }
598 
599     if (ReturnObject->String.Length == 0)
600     {
601         ACPI_WARN_PREDEFINED ((AE_INFO,
602             Info->FullPathname, Info->NodeFlags,
603             "Invalid zero-length _HID or _CID string"));
604 
605         /* Return AE_OK anyway, let driver handle it */
606 
607         Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
608         return_ACPI_STATUS (AE_OK);
609     }
610 
611     /*
612      * Remove a leading asterisk if present. For some unknown reason, there
613      * are many machines in the field that contains IDs like this.
614      *
615      * Examples: "*PNP0C03", "*ACPI0003"
616      */
617     Source = ReturnObject->String.Pointer;
618     if (*Source == '*')
619     {
620         Source++;
621         ReturnObject->String.Length--;
622 
623         ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
624             "%s: Removed invalid leading asterisk\n", Info->FullPathname));
625     }
626 
627     /*
628      * Copy and uppercase the string. From the ACPI 5.0 specification:
629      *
630      * A valid PNP ID must be of the form "AAA####" where A is an uppercase
631      * letter and # is a hex digit. A valid ACPI ID must be of the form
632      * "NNNN####" where N is an uppercase letter or decimal digit, and
633      * # is a hex digit.
634      */
635     for (Dest = ReturnObject->String.Pointer; *Source; Dest++, Source++)
636     {
637         *Dest = (char) toupper ((int) *Source);
638     }
639     ReturnObject->String.Pointer[ReturnObject->String.Length] = 0;
640 
641     return_ACPI_STATUS (AE_OK);
642 }
643 
644 
645 /******************************************************************************
646  *
647  * FUNCTION:    AcpiNsRepair_PRT
648  *
649  * PARAMETERS:  Info                - Method execution information block
650  *              ReturnObjectPtr     - Pointer to the object returned from the
651  *                                    evaluation of a method or object
652  *
653  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
654  *
655  * DESCRIPTION: Repair for the _PRT object. If necessary, fix reversed
656  *              SourceName and SourceIndex field, a common BIOS bug.
657  *
658  *****************************************************************************/
659 
660 static ACPI_STATUS
661 AcpiNsRepair_PRT (
662     ACPI_EVALUATE_INFO      *Info,
663     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
664 {
665     ACPI_OPERAND_OBJECT     *PackageObject = *ReturnObjectPtr;
666     ACPI_OPERAND_OBJECT     **TopObjectList;
667     ACPI_OPERAND_OBJECT     **SubObjectList;
668     ACPI_OPERAND_OBJECT     *ObjDesc;
669     ACPI_OPERAND_OBJECT     *SubPackage;
670     UINT32                  ElementCount;
671     UINT32                  Index;
672 
673 
674     /* Each element in the _PRT package is a subpackage */
675 
676     TopObjectList = PackageObject->Package.Elements;
677     ElementCount = PackageObject->Package.Count;
678 
679     /* Examine each subpackage */
680 
681     for (Index = 0; Index < ElementCount; Index++, TopObjectList++)
682     {
683         SubPackage = *TopObjectList;
684         SubObjectList = SubPackage->Package.Elements;
685 
686         /* Check for minimum required element count */
687 
688         if (SubPackage->Package.Count < 4)
689         {
690             continue;
691         }
692 
693         /*
694          * If the BIOS has erroneously reversed the _PRT SourceName (index 2)
695          * and the SourceIndex (index 3), fix it. _PRT is important enough to
696          * workaround this BIOS error. This also provides compatibility with
697          * other ACPI implementations.
698          */
699         ObjDesc = SubObjectList[3];
700         if (!ObjDesc || (ObjDesc->Common.Type != ACPI_TYPE_INTEGER))
701         {
702             SubObjectList[3] = SubObjectList[2];
703             SubObjectList[2] = ObjDesc;
704             Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
705 
706             ACPI_WARN_PREDEFINED ((AE_INFO,
707                 Info->FullPathname, Info->NodeFlags,
708                 "PRT[%X]: Fixed reversed SourceName and SourceIndex",
709                 Index));
710         }
711     }
712 
713     return (AE_OK);
714 }
715 
716 
717 /******************************************************************************
718  *
719  * FUNCTION:    AcpiNsRepair_PSS
720  *
721  * PARAMETERS:  Info                - Method execution information block
722  *              ReturnObjectPtr     - Pointer to the object returned from the
723  *                                    evaluation of a method or object
724  *
725  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
726  *
727  * DESCRIPTION: Repair for the _PSS object. If necessary, sort the object list
728  *              by the CPU frequencies. Check that the power dissipation values
729  *              are all proportional to CPU frequency (i.e., sorting by
730  *              frequency should be the same as sorting by power.)
731  *
732  *****************************************************************************/
733 
734 static ACPI_STATUS
735 AcpiNsRepair_PSS (
736     ACPI_EVALUATE_INFO      *Info,
737     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
738 {
739     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
740     ACPI_OPERAND_OBJECT     **OuterElements;
741     UINT32                  OuterElementCount;
742     ACPI_OPERAND_OBJECT     **Elements;
743     ACPI_OPERAND_OBJECT     *ObjDesc;
744     UINT32                  PreviousValue;
745     ACPI_STATUS             Status;
746     UINT32                  i;
747 
748 
749     /*
750      * Entries (subpackages) in the _PSS Package must be sorted by power
751      * dissipation, in descending order. If it appears that the list is
752      * incorrectly sorted, sort it. We sort by CpuFrequency, since this
753      * should be proportional to the power.
754      */
755     Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 6, 0,
756         ACPI_SORT_DESCENDING, "CpuFrequency");
757     if (ACPI_FAILURE (Status))
758     {
759         return (Status);
760     }
761 
762     /*
763      * We now know the list is correctly sorted by CPU frequency. Check if
764      * the power dissipation values are proportional.
765      */
766     PreviousValue = ACPI_UINT32_MAX;
767     OuterElements = ReturnObject->Package.Elements;
768     OuterElementCount = ReturnObject->Package.Count;
769 
770     for (i = 0; i < OuterElementCount; i++)
771     {
772         Elements = (*OuterElements)->Package.Elements;
773         ObjDesc = Elements[1]; /* Index1 = PowerDissipation */
774 
775         if ((UINT32) ObjDesc->Integer.Value > PreviousValue)
776         {
777             ACPI_WARN_PREDEFINED ((AE_INFO,
778                 Info->FullPathname, Info->NodeFlags,
779                 "SubPackage[%u,%u] - suspicious power dissipation values",
780                 i-1, i));
781         }
782 
783         PreviousValue = (UINT32) ObjDesc->Integer.Value;
784         OuterElements++;
785     }
786 
787     return (AE_OK);
788 }
789 
790 
791 /******************************************************************************
792  *
793  * FUNCTION:    AcpiNsRepair_TSS
794  *
795  * PARAMETERS:  Info                - Method execution information block
796  *              ReturnObjectPtr     - Pointer to the object returned from the
797  *                                    evaluation of a method or object
798  *
799  * RETURN:      Status. AE_OK if object is OK or was repaired successfully
800  *
801  * DESCRIPTION: Repair for the _TSS object. If necessary, sort the object list
802  *              descending by the power dissipation values.
803  *
804  *****************************************************************************/
805 
806 static ACPI_STATUS
807 AcpiNsRepair_TSS (
808     ACPI_EVALUATE_INFO      *Info,
809     ACPI_OPERAND_OBJECT     **ReturnObjectPtr)
810 {
811     ACPI_OPERAND_OBJECT     *ReturnObject = *ReturnObjectPtr;
812     ACPI_STATUS             Status;
813     ACPI_NAMESPACE_NODE     *Node;
814 
815 
816     /*
817      * We can only sort the _TSS return package if there is no _PSS in the
818      * same scope. This is because if _PSS is present, the ACPI specification
819      * dictates that the _TSS Power Dissipation field is to be ignored, and
820      * therefore some BIOSs leave garbage values in the _TSS Power field(s).
821      * In this case, it is best to just return the _TSS package as-is.
822      * (May, 2011)
823      */
824     Status = AcpiNsGetNode (Info->Node, "^_PSS",
825         ACPI_NS_NO_UPSEARCH, &Node);
826     if (ACPI_SUCCESS (Status))
827     {
828         return (AE_OK);
829     }
830 
831     Status = AcpiNsCheckSortedList (Info, ReturnObject, 0, 5, 1,
832         ACPI_SORT_DESCENDING, "PowerDissipation");
833 
834     return (Status);
835 }
836 
837 
838 /******************************************************************************
839  *
840  * FUNCTION:    AcpiNsCheckSortedList
841  *
842  * PARAMETERS:  Info                - Method execution information block
843  *              ReturnObject        - Pointer to the top-level returned object
844  *              StartIndex          - Index of the first subpackage
845  *              ExpectedCount       - Minimum length of each subpackage
846  *              SortIndex           - Subpackage entry to sort on
847  *              SortDirection       - Ascending or descending
848  *              SortKeyName         - Name of the SortIndex field
849  *
850  * RETURN:      Status. AE_OK if the list is valid and is sorted correctly or
851  *              has been repaired by sorting the list.
852  *
853  * DESCRIPTION: Check if the package list is valid and sorted correctly by the
854  *              SortIndex. If not, then sort the list.
855  *
856  *****************************************************************************/
857 
858 static ACPI_STATUS
859 AcpiNsCheckSortedList (
860     ACPI_EVALUATE_INFO      *Info,
861     ACPI_OPERAND_OBJECT     *ReturnObject,
862     UINT32                  StartIndex,
863     UINT32                  ExpectedCount,
864     UINT32                  SortIndex,
865     UINT8                   SortDirection,
866     char                    *SortKeyName)
867 {
868     UINT32                  OuterElementCount;
869     ACPI_OPERAND_OBJECT     **OuterElements;
870     ACPI_OPERAND_OBJECT     **Elements;
871     ACPI_OPERAND_OBJECT     *ObjDesc;
872     UINT32                  i;
873     UINT32                  PreviousValue;
874 
875 
876     ACPI_FUNCTION_NAME (NsCheckSortedList);
877 
878 
879     /* The top-level object must be a package */
880 
881     if (ReturnObject->Common.Type != ACPI_TYPE_PACKAGE)
882     {
883         return (AE_AML_OPERAND_TYPE);
884     }
885 
886     /*
887      * NOTE: assumes list of subpackages contains no NULL elements.
888      * Any NULL elements should have been removed by earlier call
889      * to AcpiNsRemoveNullElements.
890      */
891     OuterElementCount = ReturnObject->Package.Count;
892     if (!OuterElementCount || StartIndex >= OuterElementCount)
893     {
894         return (AE_AML_PACKAGE_LIMIT);
895     }
896 
897     OuterElements = &ReturnObject->Package.Elements[StartIndex];
898     OuterElementCount -= StartIndex;
899 
900     PreviousValue = 0;
901     if (SortDirection == ACPI_SORT_DESCENDING)
902     {
903         PreviousValue = ACPI_UINT32_MAX;
904     }
905 
906     /* Examine each subpackage */
907 
908     for (i = 0; i < OuterElementCount; i++)
909     {
910         /* Each element of the top-level package must also be a package */
911 
912         if ((*OuterElements)->Common.Type != ACPI_TYPE_PACKAGE)
913         {
914             return (AE_AML_OPERAND_TYPE);
915         }
916 
917         /* Each subpackage must have the minimum length */
918 
919         if ((*OuterElements)->Package.Count < ExpectedCount)
920         {
921             return (AE_AML_PACKAGE_LIMIT);
922         }
923 
924         Elements = (*OuterElements)->Package.Elements;
925         ObjDesc = Elements[SortIndex];
926 
927         if (ObjDesc->Common.Type != ACPI_TYPE_INTEGER)
928         {
929             return (AE_AML_OPERAND_TYPE);
930         }
931 
932         /*
933          * The list must be sorted in the specified order. If we detect a
934          * discrepancy, sort the entire list.
935          */
936         if (((SortDirection == ACPI_SORT_ASCENDING) &&
937                 (ObjDesc->Integer.Value < PreviousValue)) ||
938             ((SortDirection == ACPI_SORT_DESCENDING) &&
939                 (ObjDesc->Integer.Value > PreviousValue)))
940         {
941             AcpiNsSortList (&ReturnObject->Package.Elements[StartIndex],
942                 OuterElementCount, SortIndex, SortDirection);
943 
944             Info->ReturnFlags |= ACPI_OBJECT_REPAIRED;
945 
946             ACPI_DEBUG_PRINT ((ACPI_DB_REPAIR,
947                 "%s: Repaired unsorted list - now sorted by %s\n",
948                 Info->FullPathname, SortKeyName));
949             return (AE_OK);
950         }
951 
952         PreviousValue = (UINT32) ObjDesc->Integer.Value;
953         OuterElements++;
954     }
955 
956     return (AE_OK);
957 }
958 
959 
960 /******************************************************************************
961  *
962  * FUNCTION:    AcpiNsSortList
963  *
964  * PARAMETERS:  Elements            - Package object element list
965  *              Count               - Element count for above
966  *              Index               - Sort by which package element
967  *              SortDirection       - Ascending or Descending sort
968  *
969  * RETURN:      None
970  *
971  * DESCRIPTION: Sort the objects that are in a package element list.
972  *
973  * NOTE: Assumes that all NULL elements have been removed from the package,
974  *       and that all elements have been verified to be of type Integer.
975  *
976  *****************************************************************************/
977 
978 static void
979 AcpiNsSortList (
980     ACPI_OPERAND_OBJECT     **Elements,
981     UINT32                  Count,
982     UINT32                  Index,
983     UINT8                   SortDirection)
984 {
985     ACPI_OPERAND_OBJECT     *ObjDesc1;
986     ACPI_OPERAND_OBJECT     *ObjDesc2;
987     ACPI_OPERAND_OBJECT     *TempObj;
988     UINT32                  i;
989     UINT32                  j;
990 
991 
992     /* Simple bubble sort */
993 
994     for (i = 1; i < Count; i++)
995     {
996         for (j = (Count - 1); j >= i; j--)
997         {
998             ObjDesc1 = Elements[j-1]->Package.Elements[Index];
999             ObjDesc2 = Elements[j]->Package.Elements[Index];
1000 
1001             if (((SortDirection == ACPI_SORT_ASCENDING) &&
1002                     (ObjDesc1->Integer.Value > ObjDesc2->Integer.Value)) ||
1003 
1004                 ((SortDirection == ACPI_SORT_DESCENDING) &&
1005                     (ObjDesc1->Integer.Value < ObjDesc2->Integer.Value)))
1006             {
1007                 TempObj = Elements[j-1];
1008                 Elements[j-1] = Elements[j];
1009                 Elements[j] = TempObj;
1010             }
1011         }
1012     }
1013 }
1014 
1015 
1016 /******************************************************************************
1017  *
1018  * FUNCTION:    AcpiNsRemoveElement
1019  *
1020  * PARAMETERS:  ObjDesc             - Package object element list
1021  *              Index               - Index of element to remove
1022  *
1023  * RETURN:      None
1024  *
1025  * DESCRIPTION: Remove the requested element of a package and delete it.
1026  *
1027  *****************************************************************************/
1028 
1029 static void
1030 AcpiNsRemoveElement (
1031     ACPI_OPERAND_OBJECT     *ObjDesc,
1032     UINT32                  Index)
1033 {
1034     ACPI_OPERAND_OBJECT     **Source;
1035     ACPI_OPERAND_OBJECT     **Dest;
1036     UINT32                  Count;
1037     UINT32                  NewCount;
1038     UINT32                  i;
1039 
1040 
1041     ACPI_FUNCTION_NAME (NsRemoveElement);
1042 
1043 
1044     Count = ObjDesc->Package.Count;
1045     NewCount = Count - 1;
1046 
1047     Source = ObjDesc->Package.Elements;
1048     Dest = Source;
1049 
1050     /* Examine all elements of the package object, remove matched index */
1051 
1052     for (i = 0; i < Count; i++)
1053     {
1054         if (i == Index)
1055         {
1056             AcpiUtRemoveReference (*Source); /* Remove one ref for being in pkg */
1057             AcpiUtRemoveReference (*Source);
1058         }
1059         else
1060         {
1061             *Dest = *Source;
1062             Dest++;
1063         }
1064 
1065         Source++;
1066     }
1067 
1068     /* NULL terminate list and update the package count */
1069 
1070     *Dest = NULL;
1071     ObjDesc->Package.Count = NewCount;
1072 }
1073