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