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