Home | History | Annotate | Line # | Download | only in utilities
utdelete.c revision 1.11
      1   1.1    jruoho /*******************************************************************************
      2   1.1    jruoho  *
      3   1.1    jruoho  * Module Name: utdelete - object deletion and reference count utilities
      4   1.1    jruoho  *
      5   1.1    jruoho  ******************************************************************************/
      6   1.1    jruoho 
      7   1.2  christos /*
      8  1.10  christos  * Copyright (C) 2000 - 2023, Intel Corp.
      9   1.1    jruoho  * All rights reserved.
     10   1.1    jruoho  *
     11   1.2  christos  * Redistribution and use in source and binary forms, with or without
     12   1.2  christos  * modification, are permitted provided that the following conditions
     13   1.2  christos  * are met:
     14   1.2  christos  * 1. Redistributions of source code must retain the above copyright
     15   1.2  christos  *    notice, this list of conditions, and the following disclaimer,
     16   1.2  christos  *    without modification.
     17   1.2  christos  * 2. Redistributions in binary form must reproduce at minimum a disclaimer
     18   1.2  christos  *    substantially similar to the "NO WARRANTY" disclaimer below
     19   1.2  christos  *    ("Disclaimer") and any redistribution must be conditioned upon
     20   1.2  christos  *    including a substantially similar Disclaimer requirement for further
     21   1.2  christos  *    binary redistribution.
     22   1.2  christos  * 3. Neither the names of the above-listed copyright holders nor the names
     23   1.2  christos  *    of any contributors may be used to endorse or promote products derived
     24   1.2  christos  *    from this software without specific prior written permission.
     25   1.2  christos  *
     26   1.2  christos  * Alternatively, this software may be distributed under the terms of the
     27   1.2  christos  * GNU General Public License ("GPL") version 2 as published by the Free
     28   1.2  christos  * Software Foundation.
     29   1.2  christos  *
     30   1.2  christos  * NO WARRANTY
     31   1.2  christos  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     32   1.2  christos  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     33   1.6  christos  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
     34   1.2  christos  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
     35   1.2  christos  * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     36   1.2  christos  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     37   1.2  christos  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     38   1.2  christos  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
     39   1.2  christos  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
     40   1.2  christos  * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     41   1.2  christos  * POSSIBILITY OF SUCH DAMAGES.
     42   1.2  christos  */
     43   1.1    jruoho 
     44   1.1    jruoho #include "acpi.h"
     45   1.1    jruoho #include "accommon.h"
     46   1.1    jruoho #include "acinterp.h"
     47   1.1    jruoho #include "acnamesp.h"
     48   1.1    jruoho #include "acevents.h"
     49   1.1    jruoho 
     50   1.1    jruoho 
     51   1.1    jruoho #define _COMPONENT          ACPI_UTILITIES
     52   1.1    jruoho         ACPI_MODULE_NAME    ("utdelete")
     53   1.1    jruoho 
     54   1.1    jruoho /* Local prototypes */
     55   1.1    jruoho 
     56   1.1    jruoho static void
     57   1.1    jruoho AcpiUtDeleteInternalObj (
     58   1.1    jruoho     ACPI_OPERAND_OBJECT     *Object);
     59   1.1    jruoho 
     60   1.1    jruoho static void
     61   1.1    jruoho AcpiUtUpdateRefCount (
     62   1.1    jruoho     ACPI_OPERAND_OBJECT     *Object,
     63   1.1    jruoho     UINT32                  Action);
     64   1.1    jruoho 
     65   1.1    jruoho 
     66   1.1    jruoho /*******************************************************************************
     67   1.1    jruoho  *
     68   1.1    jruoho  * FUNCTION:    AcpiUtDeleteInternalObj
     69   1.1    jruoho  *
     70   1.1    jruoho  * PARAMETERS:  Object         - Object to be deleted
     71   1.1    jruoho  *
     72   1.1    jruoho  * RETURN:      None
     73   1.1    jruoho  *
     74   1.1    jruoho  * DESCRIPTION: Low level object deletion, after reference counts have been
     75   1.1    jruoho  *              updated (All reference counts, including sub-objects!)
     76   1.1    jruoho  *
     77   1.1    jruoho  ******************************************************************************/
     78   1.1    jruoho 
     79   1.1    jruoho static void
     80   1.1    jruoho AcpiUtDeleteInternalObj (
     81   1.1    jruoho     ACPI_OPERAND_OBJECT     *Object)
     82   1.1    jruoho {
     83   1.1    jruoho     void                    *ObjPointer = NULL;
     84   1.1    jruoho     ACPI_OPERAND_OBJECT     *HandlerDesc;
     85   1.1    jruoho     ACPI_OPERAND_OBJECT     *SecondDesc;
     86   1.1    jruoho     ACPI_OPERAND_OBJECT     *NextDesc;
     87   1.2  christos     ACPI_OPERAND_OBJECT     *StartDesc;
     88   1.1    jruoho     ACPI_OPERAND_OBJECT     **LastObjPtr;
     89   1.1    jruoho 
     90   1.1    jruoho 
     91   1.1    jruoho     ACPI_FUNCTION_TRACE_PTR (UtDeleteInternalObj, Object);
     92   1.1    jruoho 
     93   1.1    jruoho 
     94   1.1    jruoho     if (!Object)
     95   1.1    jruoho     {
     96   1.1    jruoho         return_VOID;
     97   1.1    jruoho     }
     98   1.1    jruoho 
     99   1.1    jruoho     /*
    100   1.1    jruoho      * Must delete or free any pointers within the object that are not
    101   1.1    jruoho      * actual ACPI objects (for example, a raw buffer pointer).
    102   1.1    jruoho      */
    103   1.1    jruoho     switch (Object->Common.Type)
    104   1.1    jruoho     {
    105   1.1    jruoho     case ACPI_TYPE_STRING:
    106   1.1    jruoho 
    107   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** String %p, ptr %p\n",
    108   1.1    jruoho             Object, Object->String.Pointer));
    109   1.1    jruoho 
    110   1.1    jruoho         /* Free the actual string buffer */
    111   1.1    jruoho 
    112   1.1    jruoho         if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
    113   1.1    jruoho         {
    114   1.1    jruoho             /* But only if it is NOT a pointer into an ACPI table */
    115   1.1    jruoho 
    116   1.1    jruoho             ObjPointer = Object->String.Pointer;
    117   1.1    jruoho         }
    118   1.1    jruoho         break;
    119   1.1    jruoho 
    120   1.1    jruoho     case ACPI_TYPE_BUFFER:
    121   1.1    jruoho 
    122   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "**** Buffer %p, ptr %p\n",
    123   1.1    jruoho             Object, Object->Buffer.Pointer));
    124   1.1    jruoho 
    125   1.1    jruoho         /* Free the actual buffer */
    126   1.1    jruoho 
    127   1.1    jruoho         if (!(Object->Common.Flags & AOPOBJ_STATIC_POINTER))
    128   1.1    jruoho         {
    129   1.1    jruoho             /* But only if it is NOT a pointer into an ACPI table */
    130   1.1    jruoho 
    131   1.1    jruoho             ObjPointer = Object->Buffer.Pointer;
    132   1.1    jruoho         }
    133   1.1    jruoho         break;
    134   1.1    jruoho 
    135   1.1    jruoho     case ACPI_TYPE_PACKAGE:
    136   1.1    jruoho 
    137   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, " **** Package of count %X\n",
    138   1.1    jruoho             Object->Package.Count));
    139   1.1    jruoho 
    140   1.1    jruoho         /*
    141   1.1    jruoho          * Elements of the package are not handled here, they are deleted
    142   1.1    jruoho          * separately
    143   1.1    jruoho          */
    144   1.1    jruoho 
    145   1.1    jruoho         /* Free the (variable length) element pointer array */
    146   1.1    jruoho 
    147   1.1    jruoho         ObjPointer = Object->Package.Elements;
    148   1.1    jruoho         break;
    149   1.1    jruoho 
    150   1.1    jruoho     /*
    151   1.1    jruoho      * These objects have a possible list of notify handlers.
    152   1.1    jruoho      * Device object also may have a GPE block.
    153   1.1    jruoho      */
    154   1.1    jruoho     case ACPI_TYPE_DEVICE:
    155   1.1    jruoho 
    156   1.1    jruoho         if (Object->Device.GpeBlock)
    157   1.1    jruoho         {
    158   1.1    jruoho             (void) AcpiEvDeleteGpeBlock (Object->Device.GpeBlock);
    159   1.1    jruoho         }
    160   1.1    jruoho 
    161   1.6  christos         ACPI_FALLTHROUGH;
    162   1.1    jruoho 
    163   1.1    jruoho     case ACPI_TYPE_PROCESSOR:
    164   1.1    jruoho     case ACPI_TYPE_THERMAL:
    165   1.1    jruoho 
    166   1.2  christos         /* Walk the address handler list for this object */
    167   1.1    jruoho 
    168   1.1    jruoho         HandlerDesc = Object->CommonNotify.Handler;
    169   1.1    jruoho         while (HandlerDesc)
    170   1.1    jruoho         {
    171   1.1    jruoho             NextDesc = HandlerDesc->AddressSpace.Next;
    172   1.1    jruoho             AcpiUtRemoveReference (HandlerDesc);
    173   1.1    jruoho             HandlerDesc = NextDesc;
    174   1.1    jruoho         }
    175   1.1    jruoho         break;
    176   1.1    jruoho 
    177   1.1    jruoho     case ACPI_TYPE_MUTEX:
    178   1.1    jruoho 
    179   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    180   1.1    jruoho             "***** Mutex %p, OS Mutex %p\n",
    181   1.1    jruoho             Object, Object->Mutex.OsMutex));
    182   1.1    jruoho 
    183   1.1    jruoho         if (Object == AcpiGbl_GlobalLockMutex)
    184   1.1    jruoho         {
    185   1.1    jruoho             /* Global Lock has extra semaphore */
    186   1.1    jruoho 
    187   1.1    jruoho             (void) AcpiOsDeleteSemaphore (AcpiGbl_GlobalLockSemaphore);
    188  1.11  christos             AcpiGbl_GlobalLockSemaphore = ACPI_SEMAPHORE_NULL;
    189   1.1    jruoho 
    190   1.1    jruoho             AcpiOsDeleteMutex (Object->Mutex.OsMutex);
    191   1.1    jruoho             AcpiGbl_GlobalLockMutex = NULL;
    192   1.1    jruoho         }
    193   1.1    jruoho         else
    194   1.1    jruoho         {
    195   1.1    jruoho             AcpiExUnlinkMutex (Object);
    196   1.1    jruoho             AcpiOsDeleteMutex (Object->Mutex.OsMutex);
    197   1.1    jruoho         }
    198   1.1    jruoho         break;
    199   1.1    jruoho 
    200   1.1    jruoho     case ACPI_TYPE_EVENT:
    201   1.1    jruoho 
    202   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    203   1.1    jruoho             "***** Event %p, OS Semaphore %p\n",
    204   1.1    jruoho             Object, Object->Event.OsSemaphore));
    205   1.1    jruoho 
    206   1.1    jruoho         (void) AcpiOsDeleteSemaphore (Object->Event.OsSemaphore);
    207  1.11  christos         Object->Event.OsSemaphore = ACPI_SEMAPHORE_NULL;
    208   1.1    jruoho         break;
    209   1.1    jruoho 
    210   1.1    jruoho     case ACPI_TYPE_METHOD:
    211   1.1    jruoho 
    212   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    213   1.1    jruoho             "***** Method %p\n", Object));
    214   1.1    jruoho 
    215   1.1    jruoho         /* Delete the method mutex if it exists */
    216   1.1    jruoho 
    217   1.1    jruoho         if (Object->Method.Mutex)
    218   1.1    jruoho         {
    219   1.1    jruoho             AcpiOsDeleteMutex (Object->Method.Mutex->Mutex.OsMutex);
    220   1.1    jruoho             AcpiUtDeleteObjectDesc (Object->Method.Mutex);
    221   1.1    jruoho             Object->Method.Mutex = NULL;
    222   1.1    jruoho         }
    223   1.2  christos 
    224   1.2  christos         if (Object->Method.Node)
    225   1.2  christos         {
    226   1.2  christos             Object->Method.Node = NULL;
    227   1.2  christos         }
    228   1.1    jruoho         break;
    229   1.1    jruoho 
    230   1.1    jruoho     case ACPI_TYPE_REGION:
    231   1.1    jruoho 
    232   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    233   1.1    jruoho             "***** Region %p\n", Object));
    234   1.1    jruoho 
    235   1.2  christos         /*
    236   1.2  christos          * Update AddressRange list. However, only permanent regions
    237   1.2  christos          * are installed in this list. (Not created within a method)
    238   1.2  christos          */
    239   1.2  christos         if (!(Object->Region.Node->Flags & ANOBJ_TEMPORARY))
    240   1.2  christos         {
    241   1.2  christos             AcpiUtRemoveAddressRange (Object->Region.SpaceId,
    242   1.2  christos                 Object->Region.Node);
    243   1.2  christos         }
    244   1.2  christos 
    245   1.1    jruoho         SecondDesc = AcpiNsGetSecondaryObject (Object);
    246   1.1    jruoho         if (SecondDesc)
    247   1.1    jruoho         {
    248   1.1    jruoho             /*
    249   1.1    jruoho              * Free the RegionContext if and only if the handler is one of the
    250   1.1    jruoho              * default handlers -- and therefore, we created the context object
    251   1.1    jruoho              * locally, it was not created by an external caller.
    252   1.1    jruoho              */
    253   1.1    jruoho             HandlerDesc = Object->Region.Handler;
    254   1.1    jruoho             if (HandlerDesc)
    255   1.1    jruoho             {
    256   1.1    jruoho                 NextDesc = HandlerDesc->AddressSpace.RegionList;
    257   1.2  christos                 StartDesc = NextDesc;
    258   1.1    jruoho                 LastObjPtr = &HandlerDesc->AddressSpace.RegionList;
    259   1.1    jruoho 
    260   1.2  christos                 /* Remove the region object from the handler list */
    261   1.1    jruoho 
    262   1.1    jruoho                 while (NextDesc)
    263   1.1    jruoho                 {
    264   1.1    jruoho                     if (NextDesc == Object)
    265   1.1    jruoho                     {
    266   1.1    jruoho                         *LastObjPtr = NextDesc->Region.Next;
    267   1.1    jruoho                         break;
    268   1.1    jruoho                     }
    269   1.1    jruoho 
    270   1.2  christos                     /* Walk the linked list of handlers */
    271   1.1    jruoho 
    272   1.1    jruoho                     LastObjPtr = &NextDesc->Region.Next;
    273   1.1    jruoho                     NextDesc = NextDesc->Region.Next;
    274   1.2  christos 
    275   1.2  christos                     /* Prevent infinite loop if list is corrupted */
    276   1.2  christos 
    277   1.2  christos                     if (NextDesc == StartDesc)
    278   1.2  christos                     {
    279   1.2  christos                         ACPI_ERROR ((AE_INFO,
    280   1.2  christos                             "Circular region list in address handler object %p",
    281   1.2  christos                             HandlerDesc));
    282   1.2  christos                         return_VOID;
    283   1.2  christos                     }
    284   1.1    jruoho                 }
    285   1.1    jruoho 
    286   1.1    jruoho                 if (HandlerDesc->AddressSpace.HandlerFlags &
    287   1.1    jruoho                     ACPI_ADDR_HANDLER_DEFAULT_INSTALLED)
    288   1.1    jruoho                 {
    289   1.1    jruoho                     /* Deactivate region and free region context */
    290   1.1    jruoho 
    291   1.1    jruoho                     if (HandlerDesc->AddressSpace.Setup)
    292   1.1    jruoho                     {
    293   1.1    jruoho                         (void) HandlerDesc->AddressSpace.Setup (Object,
    294   1.1    jruoho                             ACPI_REGION_DEACTIVATE,
    295   1.1    jruoho                             HandlerDesc->AddressSpace.Context,
    296   1.1    jruoho                             &SecondDesc->Extra.RegionContext);
    297   1.1    jruoho                     }
    298   1.1    jruoho                 }
    299   1.1    jruoho 
    300   1.1    jruoho                 AcpiUtRemoveReference (HandlerDesc);
    301   1.1    jruoho             }
    302   1.1    jruoho 
    303   1.1    jruoho             /* Now we can free the Extra object */
    304   1.1    jruoho 
    305   1.1    jruoho             AcpiUtDeleteObjectDesc (SecondDesc);
    306   1.1    jruoho         }
    307   1.3  christos         if (Object->Field.InternalPccBuffer)
    308   1.3  christos         {
    309   1.3  christos             ACPI_FREE(Object->Field.InternalPccBuffer);
    310   1.3  christos         }
    311   1.3  christos 
    312   1.1    jruoho         break;
    313   1.1    jruoho 
    314   1.1    jruoho     case ACPI_TYPE_BUFFER_FIELD:
    315   1.1    jruoho 
    316   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    317   1.1    jruoho             "***** Buffer Field %p\n", Object));
    318   1.1    jruoho 
    319   1.1    jruoho         SecondDesc = AcpiNsGetSecondaryObject (Object);
    320   1.1    jruoho         if (SecondDesc)
    321   1.1    jruoho         {
    322   1.1    jruoho             AcpiUtDeleteObjectDesc (SecondDesc);
    323   1.1    jruoho         }
    324   1.1    jruoho         break;
    325   1.1    jruoho 
    326   1.1    jruoho     case ACPI_TYPE_LOCAL_BANK_FIELD:
    327   1.1    jruoho 
    328   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    329   1.1    jruoho             "***** Bank Field %p\n", Object));
    330   1.1    jruoho 
    331   1.1    jruoho         SecondDesc = AcpiNsGetSecondaryObject (Object);
    332   1.1    jruoho         if (SecondDesc)
    333   1.1    jruoho         {
    334   1.1    jruoho             AcpiUtDeleteObjectDesc (SecondDesc);
    335   1.1    jruoho         }
    336   1.1    jruoho         break;
    337   1.1    jruoho 
    338   1.7  christos     case ACPI_TYPE_LOCAL_ADDRESS_HANDLER:
    339   1.7  christos 
    340   1.7  christos         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    341   1.7  christos             "***** Address handler %p\n", Object));
    342   1.7  christos 
    343   1.7  christos         AcpiOsDeleteMutex (Object->AddressSpace.ContextMutex);
    344   1.7  christos         break;
    345   1.7  christos 
    346   1.2  christos     default:
    347   1.1    jruoho 
    348   1.1    jruoho         break;
    349   1.1    jruoho     }
    350   1.1    jruoho 
    351   1.1    jruoho     /* Free any allocated memory (pointer within the object) found above */
    352   1.1    jruoho 
    353   1.1    jruoho     if (ObjPointer)
    354   1.1    jruoho     {
    355   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS, "Deleting Object Subptr %p\n",
    356   1.1    jruoho             ObjPointer));
    357   1.1    jruoho         ACPI_FREE (ObjPointer);
    358   1.1    jruoho     }
    359   1.1    jruoho 
    360   1.1    jruoho     /* Now the object can be safely deleted */
    361   1.1    jruoho 
    362   1.2  christos     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS, "%s: Deleting Object %p [%s]\n",
    363   1.2  christos         ACPI_GET_FUNCTION_NAME, Object, AcpiUtGetObjectTypeName (Object)));
    364   1.1    jruoho 
    365   1.1    jruoho     AcpiUtDeleteObjectDesc (Object);
    366   1.1    jruoho     return_VOID;
    367   1.1    jruoho }
    368   1.1    jruoho 
    369   1.1    jruoho 
    370   1.1    jruoho /*******************************************************************************
    371   1.1    jruoho  *
    372   1.1    jruoho  * FUNCTION:    AcpiUtDeleteInternalObjectList
    373   1.1    jruoho  *
    374   1.1    jruoho  * PARAMETERS:  ObjList         - Pointer to the list to be deleted
    375   1.1    jruoho  *
    376   1.1    jruoho  * RETURN:      None
    377   1.1    jruoho  *
    378   1.1    jruoho  * DESCRIPTION: This function deletes an internal object list, including both
    379   1.1    jruoho  *              simple objects and package objects
    380   1.1    jruoho  *
    381   1.1    jruoho  ******************************************************************************/
    382   1.1    jruoho 
    383   1.1    jruoho void
    384   1.1    jruoho AcpiUtDeleteInternalObjectList (
    385   1.1    jruoho     ACPI_OPERAND_OBJECT     **ObjList)
    386   1.1    jruoho {
    387   1.1    jruoho     ACPI_OPERAND_OBJECT     **InternalObj;
    388   1.1    jruoho 
    389   1.1    jruoho 
    390   1.2  christos     ACPI_FUNCTION_ENTRY ();
    391   1.1    jruoho 
    392   1.1    jruoho 
    393   1.1    jruoho     /* Walk the null-terminated internal list */
    394   1.1    jruoho 
    395   1.1    jruoho     for (InternalObj = ObjList; *InternalObj; InternalObj++)
    396   1.1    jruoho     {
    397   1.1    jruoho         AcpiUtRemoveReference (*InternalObj);
    398   1.1    jruoho     }
    399   1.1    jruoho 
    400   1.1    jruoho     /* Free the combined parameter pointer list and object array */
    401   1.1    jruoho 
    402   1.1    jruoho     ACPI_FREE (ObjList);
    403   1.2  christos     return;
    404   1.1    jruoho }
    405   1.1    jruoho 
    406   1.1    jruoho 
    407   1.1    jruoho /*******************************************************************************
    408   1.1    jruoho  *
    409   1.1    jruoho  * FUNCTION:    AcpiUtUpdateRefCount
    410   1.1    jruoho  *
    411   1.1    jruoho  * PARAMETERS:  Object          - Object whose ref count is to be updated
    412   1.2  christos  *              Action          - What to do (REF_INCREMENT or REF_DECREMENT)
    413   1.1    jruoho  *
    414   1.2  christos  * RETURN:      None. Sets new reference count within the object
    415   1.1    jruoho  *
    416   1.2  christos  * DESCRIPTION: Modify the reference count for an internal acpi object
    417   1.1    jruoho  *
    418   1.1    jruoho  ******************************************************************************/
    419   1.1    jruoho 
    420   1.1    jruoho static void
    421   1.1    jruoho AcpiUtUpdateRefCount (
    422   1.1    jruoho     ACPI_OPERAND_OBJECT     *Object,
    423   1.1    jruoho     UINT32                  Action)
    424   1.1    jruoho {
    425   1.2  christos     UINT16                  OriginalCount;
    426   1.2  christos     UINT16                  NewCount = 0;
    427   1.2  christos     ACPI_CPU_FLAGS          LockFlags;
    428   1.2  christos     const char              *Message;
    429   1.1    jruoho 
    430   1.1    jruoho 
    431   1.1    jruoho     ACPI_FUNCTION_NAME (UtUpdateRefCount);
    432   1.1    jruoho 
    433   1.1    jruoho 
    434   1.1    jruoho     if (!Object)
    435   1.1    jruoho     {
    436   1.1    jruoho         return;
    437   1.1    jruoho     }
    438   1.1    jruoho 
    439   1.1    jruoho     /*
    440   1.2  christos      * Always get the reference count lock. Note: Interpreter and/or
    441   1.2  christos      * Namespace is not always locked when this function is called.
    442   1.1    jruoho      */
    443   1.2  christos     LockFlags = AcpiOsAcquireLock (AcpiGbl_ReferenceCountLock);
    444   1.2  christos     OriginalCount = Object->Common.ReferenceCount;
    445   1.2  christos 
    446   1.2  christos     /* Perform the reference count action (increment, decrement) */
    447   1.2  christos 
    448   1.1    jruoho     switch (Action)
    449   1.1    jruoho     {
    450   1.1    jruoho     case REF_INCREMENT:
    451   1.1    jruoho 
    452   1.2  christos         NewCount = OriginalCount + 1;
    453   1.1    jruoho         Object->Common.ReferenceCount = NewCount;
    454   1.2  christos         AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
    455   1.2  christos 
    456   1.2  christos         /* The current reference count should never be zero here */
    457   1.2  christos 
    458   1.2  christos         if (!OriginalCount)
    459   1.2  christos         {
    460   1.2  christos             ACPI_WARNING ((AE_INFO,
    461   1.2  christos                 "Obj %p, Reference Count was zero before increment\n",
    462   1.2  christos                 Object));
    463   1.2  christos         }
    464   1.1    jruoho 
    465   1.1    jruoho         ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    466   1.2  christos             "Obj %p Type %.2X [%s] Refs %.2X [Incremented]\n",
    467   1.2  christos             Object, Object->Common.Type,
    468   1.2  christos             AcpiUtGetObjectTypeName (Object), NewCount));
    469   1.2  christos         Message = "Incremement";
    470   1.1    jruoho         break;
    471   1.1    jruoho 
    472   1.1    jruoho     case REF_DECREMENT:
    473   1.1    jruoho 
    474   1.2  christos         /* The current reference count must be non-zero */
    475   1.2  christos 
    476   1.2  christos         if (OriginalCount)
    477   1.1    jruoho         {
    478   1.2  christos             NewCount = OriginalCount - 1;
    479   1.2  christos             Object->Common.ReferenceCount = NewCount;
    480   1.1    jruoho         }
    481   1.1    jruoho 
    482   1.2  christos         AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
    483   1.1    jruoho 
    484   1.2  christos         if (!OriginalCount)
    485   1.1    jruoho         {
    486   1.2  christos             ACPI_WARNING ((AE_INFO,
    487   1.2  christos                 "Obj %p, Reference Count is already zero, cannot decrement\n",
    488   1.2  christos                 Object));
    489   1.8  christos 	    return;
    490   1.1    jruoho         }
    491   1.1    jruoho 
    492   1.2  christos         ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
    493   1.2  christos             "%s: Obj %p Type %.2X Refs %.2X [Decremented]\n",
    494   1.2  christos             ACPI_GET_FUNCTION_NAME, Object, Object->Common.Type, NewCount));
    495   1.2  christos 
    496   1.2  christos         /* Actually delete the object on a reference count of zero */
    497   1.2  christos 
    498   1.1    jruoho         if (NewCount == 0)
    499   1.1    jruoho         {
    500   1.1    jruoho             AcpiUtDeleteInternalObj (Object);
    501   1.1    jruoho         }
    502   1.2  christos         Message = "Decrement";
    503   1.1    jruoho         break;
    504   1.1    jruoho 
    505   1.1    jruoho     default:
    506   1.1    jruoho 
    507   1.2  christos         AcpiOsReleaseLock (AcpiGbl_ReferenceCountLock, LockFlags);
    508   1.2  christos         ACPI_ERROR ((AE_INFO, "Unknown Reference Count action (0x%X)",
    509   1.2  christos             Action));
    510   1.2  christos         return;
    511   1.1    jruoho     }
    512   1.1    jruoho 
    513   1.1    jruoho     /*
    514   1.1    jruoho      * Sanity check the reference count, for debug purposes only.
    515   1.1    jruoho      * (A deleted object will have a huge reference count)
    516   1.1    jruoho      */
    517   1.2  christos     if (NewCount > ACPI_MAX_REFERENCE_COUNT)
    518   1.1    jruoho     {
    519   1.1    jruoho         ACPI_WARNING ((AE_INFO,
    520   1.2  christos             "Large Reference Count (0x%X) in object %p, Type=0x%.2X Operation=%s",
    521   1.2  christos             NewCount, Object, Object->Common.Type, Message));
    522   1.1    jruoho     }
    523   1.1    jruoho }
    524   1.1    jruoho 
    525   1.1    jruoho 
    526   1.1    jruoho /*******************************************************************************
    527   1.1    jruoho  *
    528   1.1    jruoho  * FUNCTION:    AcpiUtUpdateObjectReference
    529   1.1    jruoho  *
    530   1.4  christos  * PARAMETERS:  Object              - Increment or decrement the ref count for
    531   1.4  christos  *                                    this object and all sub-objects
    532   1.2  christos  *              Action              - Either REF_INCREMENT or REF_DECREMENT
    533   1.1    jruoho  *
    534   1.1    jruoho  * RETURN:      Status
    535   1.1    jruoho  *
    536   1.4  christos  * DESCRIPTION: Increment or decrement the object reference count
    537   1.1    jruoho  *
    538   1.1    jruoho  * Object references are incremented when:
    539   1.1    jruoho  * 1) An object is attached to a Node (namespace object)
    540   1.1    jruoho  * 2) An object is copied (all subobjects must be incremented)
    541   1.1    jruoho  *
    542   1.1    jruoho  * Object references are decremented when:
    543   1.1    jruoho  * 1) An object is detached from an Node
    544   1.1    jruoho  *
    545   1.1    jruoho  ******************************************************************************/
    546   1.1    jruoho 
    547   1.1    jruoho ACPI_STATUS
    548   1.1    jruoho AcpiUtUpdateObjectReference (
    549   1.1    jruoho     ACPI_OPERAND_OBJECT     *Object,
    550   1.1    jruoho     UINT16                  Action)
    551   1.1    jruoho {
    552   1.1    jruoho     ACPI_STATUS             Status = AE_OK;
    553   1.1    jruoho     ACPI_GENERIC_STATE      *StateList = NULL;
    554   1.1    jruoho     ACPI_OPERAND_OBJECT     *NextObject = NULL;
    555   1.2  christos     ACPI_OPERAND_OBJECT     *PrevObject;
    556   1.1    jruoho     ACPI_GENERIC_STATE      *State;
    557   1.1    jruoho     UINT32                  i;
    558   1.1    jruoho 
    559   1.1    jruoho 
    560   1.2  christos     ACPI_FUNCTION_NAME (UtUpdateObjectReference);
    561   1.1    jruoho 
    562   1.1    jruoho 
    563   1.1    jruoho     while (Object)
    564   1.1    jruoho     {
    565   1.1    jruoho         /* Make sure that this isn't a namespace handle */
    566   1.1    jruoho 
    567   1.1    jruoho         if (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED)
    568   1.1    jruoho         {
    569   1.1    jruoho             ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    570   1.1    jruoho                 "Object %p is NS handle\n", Object));
    571   1.2  christos             return (AE_OK);
    572   1.1    jruoho         }
    573   1.1    jruoho 
    574   1.1    jruoho         /*
    575   1.4  christos          * All sub-objects must have their reference count updated
    576   1.2  christos          * also. Different object types have different subobjects.
    577   1.1    jruoho          */
    578   1.1    jruoho         switch (Object->Common.Type)
    579   1.1    jruoho         {
    580   1.1    jruoho         case ACPI_TYPE_DEVICE:
    581   1.1    jruoho         case ACPI_TYPE_PROCESSOR:
    582   1.1    jruoho         case ACPI_TYPE_POWER:
    583   1.1    jruoho         case ACPI_TYPE_THERMAL:
    584   1.2  christos             /*
    585   1.2  christos              * Update the notify objects for these types (if present)
    586   1.2  christos              * Two lists, system and device notify handlers.
    587   1.2  christos              */
    588   1.2  christos             for (i = 0; i < ACPI_NUM_NOTIFY_TYPES; i++)
    589   1.2  christos             {
    590   1.2  christos                 PrevObject = Object->CommonNotify.NotifyList[i];
    591   1.2  christos                 while (PrevObject)
    592   1.2  christos                 {
    593   1.2  christos                     NextObject = PrevObject->Notify.Next[i];
    594   1.2  christos                     AcpiUtUpdateRefCount (PrevObject, Action);
    595   1.2  christos                     PrevObject = NextObject;
    596   1.2  christos                 }
    597   1.2  christos             }
    598   1.1    jruoho             break;
    599   1.1    jruoho 
    600   1.1    jruoho         case ACPI_TYPE_PACKAGE:
    601   1.1    jruoho             /*
    602   1.1    jruoho              * We must update all the sub-objects of the package,
    603   1.1    jruoho              * each of whom may have their own sub-objects.
    604   1.1    jruoho              */
    605   1.1    jruoho             for (i = 0; i < Object->Package.Count; i++)
    606   1.1    jruoho             {
    607   1.1    jruoho                 /*
    608   1.2  christos                  * Null package elements are legal and can be simply
    609   1.2  christos                  * ignored.
    610   1.1    jruoho                  */
    611   1.2  christos                 NextObject = Object->Package.Elements[i];
    612   1.2  christos                 if (!NextObject)
    613   1.1    jruoho                 {
    614   1.2  christos                     continue;
    615   1.2  christos                 }
    616   1.2  christos 
    617   1.2  christos                 switch (NextObject->Common.Type)
    618   1.2  christos                 {
    619   1.2  christos                 case ACPI_TYPE_INTEGER:
    620   1.2  christos                 case ACPI_TYPE_STRING:
    621   1.2  christos                 case ACPI_TYPE_BUFFER:
    622   1.2  christos                     /*
    623   1.2  christos                      * For these very simple sub-objects, we can just
    624   1.2  christos                      * update the reference count here and continue.
    625   1.2  christos                      * Greatly increases performance of this operation.
    626   1.2  christos                      */
    627   1.2  christos                     AcpiUtUpdateRefCount (NextObject, Action);
    628   1.2  christos                     break;
    629   1.2  christos 
    630   1.2  christos                 default:
    631   1.2  christos                     /*
    632   1.2  christos                      * For complex sub-objects, push them onto the stack
    633   1.2  christos                      * for later processing (this eliminates recursion.)
    634   1.2  christos                      */
    635   1.2  christos                     Status = AcpiUtCreateUpdateStateAndPush (
    636   1.2  christos                         NextObject, Action, &StateList);
    637   1.2  christos                     if (ACPI_FAILURE (Status))
    638   1.2  christos                     {
    639   1.2  christos                         goto ErrorExit;
    640   1.2  christos                     }
    641   1.2  christos                     break;
    642   1.1    jruoho                 }
    643   1.1    jruoho             }
    644   1.4  christos 
    645   1.2  christos             NextObject = NULL;
    646   1.1    jruoho             break;
    647   1.1    jruoho 
    648   1.1    jruoho         case ACPI_TYPE_BUFFER_FIELD:
    649   1.1    jruoho 
    650   1.1    jruoho             NextObject = Object->BufferField.BufferObj;
    651   1.1    jruoho             break;
    652   1.1    jruoho 
    653   1.1    jruoho         case ACPI_TYPE_LOCAL_BANK_FIELD:
    654   1.1    jruoho 
    655   1.1    jruoho             NextObject = Object->BankField.BankObj;
    656   1.1    jruoho             Status = AcpiUtCreateUpdateStateAndPush (
    657   1.2  christos                 Object->BankField.RegionObj, Action, &StateList);
    658   1.1    jruoho             if (ACPI_FAILURE (Status))
    659   1.1    jruoho             {
    660   1.1    jruoho                 goto ErrorExit;
    661   1.1    jruoho             }
    662   1.1    jruoho             break;
    663   1.1    jruoho 
    664   1.1    jruoho         case ACPI_TYPE_LOCAL_INDEX_FIELD:
    665   1.1    jruoho 
    666   1.1    jruoho             NextObject = Object->IndexField.IndexObj;
    667   1.1    jruoho             Status = AcpiUtCreateUpdateStateAndPush (
    668   1.2  christos                 Object->IndexField.DataObj, Action, &StateList);
    669   1.1    jruoho             if (ACPI_FAILURE (Status))
    670   1.1    jruoho             {
    671   1.1    jruoho                 goto ErrorExit;
    672   1.1    jruoho             }
    673   1.1    jruoho             break;
    674   1.1    jruoho 
    675   1.1    jruoho         case ACPI_TYPE_LOCAL_REFERENCE:
    676   1.1    jruoho             /*
    677   1.1    jruoho              * The target of an Index (a package, string, or buffer) or a named
    678   1.1    jruoho              * reference must track changes to the ref count of the index or
    679   1.1    jruoho              * target object.
    680   1.1    jruoho              */
    681   1.1    jruoho             if ((Object->Reference.Class == ACPI_REFCLASS_INDEX) ||
    682   1.1    jruoho                 (Object->Reference.Class== ACPI_REFCLASS_NAME))
    683   1.1    jruoho             {
    684   1.1    jruoho                 NextObject = Object->Reference.Object;
    685   1.1    jruoho             }
    686   1.1    jruoho             break;
    687   1.1    jruoho 
    688   1.5  christos         case ACPI_TYPE_LOCAL_REGION_FIELD:
    689   1.1    jruoho         case ACPI_TYPE_REGION:
    690   1.1    jruoho         default:
    691   1.2  christos 
    692   1.1    jruoho             break; /* No subobjects for all other types */
    693   1.1    jruoho         }
    694   1.1    jruoho 
    695   1.1    jruoho         /*
    696   1.1    jruoho          * Now we can update the count in the main object. This can only
    697   1.1    jruoho          * happen after we update the sub-objects in case this causes the
    698   1.1    jruoho          * main object to be deleted.
    699   1.1    jruoho          */
    700   1.1    jruoho         AcpiUtUpdateRefCount (Object, Action);
    701   1.1    jruoho         Object = NULL;
    702   1.1    jruoho 
    703   1.1    jruoho         /* Move on to the next object to be updated */
    704   1.1    jruoho 
    705   1.1    jruoho         if (NextObject)
    706   1.1    jruoho         {
    707   1.1    jruoho             Object = NextObject;
    708   1.1    jruoho             NextObject = NULL;
    709   1.1    jruoho         }
    710   1.1    jruoho         else if (StateList)
    711   1.1    jruoho         {
    712   1.1    jruoho             State = AcpiUtPopGenericState (&StateList);
    713   1.1    jruoho             Object = State->Update.Object;
    714   1.1    jruoho             AcpiUtDeleteGenericState (State);
    715   1.1    jruoho         }
    716   1.1    jruoho     }
    717   1.1    jruoho 
    718   1.2  christos     return (AE_OK);
    719   1.1    jruoho 
    720   1.1    jruoho 
    721   1.1    jruoho ErrorExit:
    722   1.1    jruoho 
    723   1.1    jruoho     ACPI_EXCEPTION ((AE_INFO, Status,
    724   1.1    jruoho         "Could not update object reference count"));
    725   1.1    jruoho 
    726   1.1    jruoho     /* Free any stacked Update State objects */
    727   1.1    jruoho 
    728   1.1    jruoho     while (StateList)
    729   1.1    jruoho     {
    730   1.1    jruoho         State = AcpiUtPopGenericState (&StateList);
    731   1.1    jruoho         AcpiUtDeleteGenericState (State);
    732   1.1    jruoho     }
    733   1.1    jruoho 
    734   1.2  christos     return (Status);
    735   1.1    jruoho }
    736   1.1    jruoho 
    737   1.1    jruoho 
    738   1.1    jruoho /*******************************************************************************
    739   1.1    jruoho  *
    740   1.1    jruoho  * FUNCTION:    AcpiUtAddReference
    741   1.1    jruoho  *
    742   1.1    jruoho  * PARAMETERS:  Object          - Object whose reference count is to be
    743   1.1    jruoho  *                                incremented
    744   1.1    jruoho  *
    745   1.1    jruoho  * RETURN:      None
    746   1.1    jruoho  *
    747   1.1    jruoho  * DESCRIPTION: Add one reference to an ACPI object
    748   1.1    jruoho  *
    749   1.1    jruoho  ******************************************************************************/
    750   1.1    jruoho 
    751   1.1    jruoho void
    752   1.1    jruoho AcpiUtAddReference (
    753   1.1    jruoho     ACPI_OPERAND_OBJECT     *Object)
    754   1.1    jruoho {
    755   1.1    jruoho 
    756   1.2  christos     ACPI_FUNCTION_NAME (UtAddReference);
    757   1.1    jruoho 
    758   1.1    jruoho 
    759   1.1    jruoho     /* Ensure that we have a valid object */
    760   1.1    jruoho 
    761   1.1    jruoho     if (!AcpiUtValidInternalObject (Object))
    762   1.1    jruoho     {
    763   1.2  christos         return;
    764   1.1    jruoho     }
    765   1.1    jruoho 
    766   1.1    jruoho     ACPI_DEBUG_PRINT ((ACPI_DB_ALLOCATIONS,
    767   1.1    jruoho         "Obj %p Current Refs=%X [To Be Incremented]\n",
    768   1.1    jruoho         Object, Object->Common.ReferenceCount));
    769   1.1    jruoho 
    770   1.1    jruoho     /* Increment the reference count */
    771   1.1    jruoho 
    772   1.1    jruoho     (void) AcpiUtUpdateObjectReference (Object, REF_INCREMENT);
    773   1.2  christos     return;
    774   1.1    jruoho }
    775   1.1    jruoho 
    776   1.1    jruoho 
    777   1.1    jruoho /*******************************************************************************
    778   1.1    jruoho  *
    779   1.1    jruoho  * FUNCTION:    AcpiUtRemoveReference
    780   1.1    jruoho  *
    781   1.1    jruoho  * PARAMETERS:  Object         - Object whose ref count will be decremented
    782   1.1    jruoho  *
    783   1.1    jruoho  * RETURN:      None
    784   1.1    jruoho  *
    785   1.1    jruoho  * DESCRIPTION: Decrement the reference count of an ACPI internal object
    786   1.1    jruoho  *
    787   1.1    jruoho  ******************************************************************************/
    788   1.1    jruoho 
    789   1.1    jruoho void
    790   1.1    jruoho AcpiUtRemoveReference (
    791   1.1    jruoho     ACPI_OPERAND_OBJECT     *Object)
    792   1.1    jruoho {
    793   1.1    jruoho 
    794   1.2  christos     ACPI_FUNCTION_NAME (UtRemoveReference);
    795   1.1    jruoho 
    796   1.1    jruoho 
    797   1.1    jruoho     /*
    798   1.1    jruoho      * Allow a NULL pointer to be passed in, just ignore it. This saves
    799   1.1    jruoho      * each caller from having to check. Also, ignore NS nodes.
    800   1.1    jruoho      */
    801   1.1    jruoho     if (!Object ||
    802   1.1    jruoho         (ACPI_GET_DESCRIPTOR_TYPE (Object) == ACPI_DESC_TYPE_NAMED))
    803   1.1    jruoho 
    804   1.1    jruoho     {
    805   1.2  christos         return;
    806   1.1    jruoho     }
    807   1.1    jruoho 
    808   1.1    jruoho     /* Ensure that we have a valid object */
    809   1.1    jruoho 
    810   1.1    jruoho     if (!AcpiUtValidInternalObject (Object))
    811   1.1    jruoho     {
    812   1.2  christos         return;
    813   1.1    jruoho     }
    814   1.1    jruoho 
    815   1.2  christos     ACPI_DEBUG_PRINT_RAW ((ACPI_DB_ALLOCATIONS,
    816   1.2  christos         "%s: Obj %p Current Refs=%X [To Be Decremented]\n",
    817   1.2  christos         ACPI_GET_FUNCTION_NAME, Object, Object->Common.ReferenceCount));
    818   1.1    jruoho 
    819   1.1    jruoho     /*
    820   1.1    jruoho      * Decrement the reference count, and only actually delete the object
    821   1.1    jruoho      * if the reference count becomes 0. (Must also decrement the ref count
    822   1.1    jruoho      * of all subobjects!)
    823   1.1    jruoho      */
    824   1.1    jruoho     (void) AcpiUtUpdateObjectReference (Object, REF_DECREMENT);
    825   1.2  christos     return;
    826   1.1    jruoho }
    827