xref: /onnv-gate/usr/src/uts/intel/io/acpica/utilities/utcache.c (revision 7851:e828bbb1689c)
13446Smrj /******************************************************************************
23446Smrj  *
33446Smrj  * Module Name: utcache - local cache allocation routines
4*7851SDana.Myers@Sun.COM  *              $Revision: 1.9 $
53446Smrj  *
63446Smrj  *****************************************************************************/
73446Smrj 
83446Smrj /******************************************************************************
93446Smrj  *
103446Smrj  * 1. Copyright Notice
113446Smrj  *
12*7851SDana.Myers@Sun.COM  * Some or all of this work - Copyright (c) 1999 - 2008, Intel Corp.
133446Smrj  * All rights reserved.
143446Smrj  *
153446Smrj  * 2. License
163446Smrj  *
173446Smrj  * 2.1. This is your license from Intel Corp. under its intellectual property
183446Smrj  * rights.  You may have additional license terms from the party that provided
193446Smrj  * you this software, covering your right to use that party's intellectual
203446Smrj  * property rights.
213446Smrj  *
223446Smrj  * 2.2. Intel grants, free of charge, to any person ("Licensee") obtaining a
233446Smrj  * copy of the source code appearing in this file ("Covered Code") an
243446Smrj  * irrevocable, perpetual, worldwide license under Intel's copyrights in the
253446Smrj  * base code distributed originally by Intel ("Original Intel Code") to copy,
263446Smrj  * make derivatives, distribute, use and display any portion of the Covered
273446Smrj  * Code in any form, with the right to sublicense such rights; and
283446Smrj  *
293446Smrj  * 2.3. Intel grants Licensee a non-exclusive and non-transferable patent
303446Smrj  * license (with the right to sublicense), under only those claims of Intel
313446Smrj  * patents that are infringed by the Original Intel Code, to make, use, sell,
323446Smrj  * offer to sell, and import the Covered Code and derivative works thereof
333446Smrj  * solely to the minimum extent necessary to exercise the above copyright
343446Smrj  * license, and in no event shall the patent license extend to any additions
353446Smrj  * to or modifications of the Original Intel Code.  No other license or right
363446Smrj  * is granted directly or by implication, estoppel or otherwise;
373446Smrj  *
383446Smrj  * The above copyright and patent license is granted only if the following
393446Smrj  * conditions are met:
403446Smrj  *
413446Smrj  * 3. Conditions
423446Smrj  *
433446Smrj  * 3.1. Redistribution of Source with Rights to Further Distribute Source.
443446Smrj  * Redistribution of source code of any substantial portion of the Covered
453446Smrj  * Code or modification with rights to further distribute source must include
463446Smrj  * the above Copyright Notice, the above License, this list of Conditions,
473446Smrj  * and the following Disclaimer and Export Compliance provision.  In addition,
483446Smrj  * Licensee must cause all Covered Code to which Licensee contributes to
493446Smrj  * contain a file documenting the changes Licensee made to create that Covered
503446Smrj  * Code and the date of any change.  Licensee must include in that file the
513446Smrj  * documentation of any changes made by any predecessor Licensee.  Licensee
523446Smrj  * must include a prominent statement that the modification is derived,
533446Smrj  * directly or indirectly, from Original Intel Code.
543446Smrj  *
553446Smrj  * 3.2. Redistribution of Source with no Rights to Further Distribute Source.
563446Smrj  * Redistribution of source code of any substantial portion of the Covered
573446Smrj  * Code or modification without rights to further distribute source must
583446Smrj  * include the following Disclaimer and Export Compliance provision in the
593446Smrj  * documentation and/or other materials provided with distribution.  In
603446Smrj  * addition, Licensee may not authorize further sublicense of source of any
613446Smrj  * portion of the Covered Code, and must include terms to the effect that the
623446Smrj  * license from Licensee to its licensee is limited to the intellectual
633446Smrj  * property embodied in the software Licensee provides to its licensee, and
643446Smrj  * not to intellectual property embodied in modifications its licensee may
653446Smrj  * make.
663446Smrj  *
673446Smrj  * 3.3. Redistribution of Executable. Redistribution in executable form of any
683446Smrj  * substantial portion of the Covered Code or modification must reproduce the
693446Smrj  * above Copyright Notice, and the following Disclaimer and Export Compliance
703446Smrj  * provision in the documentation and/or other materials provided with the
713446Smrj  * distribution.
723446Smrj  *
733446Smrj  * 3.4. Intel retains all right, title, and interest in and to the Original
743446Smrj  * Intel Code.
753446Smrj  *
763446Smrj  * 3.5. Neither the name Intel nor any other trademark owned or controlled by
773446Smrj  * Intel shall be used in advertising or otherwise to promote the sale, use or
783446Smrj  * other dealings in products derived from or relating to the Covered Code
793446Smrj  * without prior written authorization from Intel.
803446Smrj  *
813446Smrj  * 4. Disclaimer and Export Compliance
823446Smrj  *
833446Smrj  * 4.1. INTEL MAKES NO WARRANTY OF ANY KIND REGARDING ANY SOFTWARE PROVIDED
843446Smrj  * HERE.  ANY SOFTWARE ORIGINATING FROM INTEL OR DERIVED FROM INTEL SOFTWARE
853446Smrj  * IS PROVIDED "AS IS," AND INTEL WILL NOT PROVIDE ANY SUPPORT,  ASSISTANCE,
863446Smrj  * INSTALLATION, TRAINING OR OTHER SERVICES.  INTEL WILL NOT PROVIDE ANY
873446Smrj  * UPDATES, ENHANCEMENTS OR EXTENSIONS.  INTEL SPECIFICALLY DISCLAIMS ANY
883446Smrj  * IMPLIED WARRANTIES OF MERCHANTABILITY, NONINFRINGEMENT AND FITNESS FOR A
893446Smrj  * PARTICULAR PURPOSE.
903446Smrj  *
913446Smrj  * 4.2. IN NO EVENT SHALL INTEL HAVE ANY LIABILITY TO LICENSEE, ITS LICENSEES
923446Smrj  * OR ANY OTHER THIRD PARTY, FOR ANY LOST PROFITS, LOST DATA, LOSS OF USE OR
933446Smrj  * COSTS OF PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES, OR FOR ANY INDIRECT,
943446Smrj  * SPECIAL OR CONSEQUENTIAL DAMAGES ARISING OUT OF THIS AGREEMENT, UNDER ANY
953446Smrj  * CAUSE OF ACTION OR THEORY OF LIABILITY, AND IRRESPECTIVE OF WHETHER INTEL
963446Smrj  * HAS ADVANCE NOTICE OF THE POSSIBILITY OF SUCH DAMAGES.  THESE LIMITATIONS
973446Smrj  * SHALL APPLY NOTWITHSTANDING THE FAILURE OF THE ESSENTIAL PURPOSE OF ANY
983446Smrj  * LIMITED REMEDY.
993446Smrj  *
1003446Smrj  * 4.3. Licensee shall not export, either directly or indirectly, any of this
1013446Smrj  * software or system incorporating such software without first obtaining any
1023446Smrj  * required license or other approval from the U. S. Department of Commerce or
1033446Smrj  * any other agency or department of the United States Government.  In the
1043446Smrj  * event Licensee exports any such software from the United States or
1053446Smrj  * re-exports any such software from a foreign destination, Licensee shall
1063446Smrj  * ensure that the distribution and export/re-export of the software is in
1073446Smrj  * compliance with all laws, regulations, orders, or other restrictions of the
1083446Smrj  * U.S. Export Administration Regulations. Licensee agrees that neither it nor
1093446Smrj  * any of its subsidiaries will export/re-export any technical data, process,
1103446Smrj  * software, or service, directly or indirectly, to any country for which the
1113446Smrj  * United States government or any agency thereof requires an export license,
1123446Smrj  * other governmental approval, or letter of assurance, without first obtaining
1133446Smrj  * such license, approval or letter.
1143446Smrj  *
1153446Smrj  *****************************************************************************/
1163446Smrj 
1173446Smrj #define __UTCACHE_C__
1183446Smrj 
1193446Smrj #include "acpi.h"
1203446Smrj 
1213446Smrj #define _COMPONENT          ACPI_UTILITIES
1223446Smrj         ACPI_MODULE_NAME    ("utcache")
1233446Smrj 
1243446Smrj 
1253446Smrj #ifdef ACPI_USE_LOCAL_CACHE
1263446Smrj /*******************************************************************************
1273446Smrj  *
1283446Smrj  * FUNCTION:    AcpiOsCreateCache
1293446Smrj  *
1303446Smrj  * PARAMETERS:  CacheName       - Ascii name for the cache
1313446Smrj  *              ObjectSize      - Size of each cached object
1323446Smrj  *              MaxDepth        - Maximum depth of the cache (in objects)
1333446Smrj  *              ReturnCache     - Where the new cache object is returned
1343446Smrj  *
1353446Smrj  * RETURN:      Status
1363446Smrj  *
1373446Smrj  * DESCRIPTION: Create a cache object
1383446Smrj  *
1393446Smrj  ******************************************************************************/
1403446Smrj 
1413446Smrj ACPI_STATUS
1423446Smrj AcpiOsCreateCache (
1433446Smrj     char                    *CacheName,
1443446Smrj     UINT16                  ObjectSize,
1453446Smrj     UINT16                  MaxDepth,
1463446Smrj     ACPI_MEMORY_LIST        **ReturnCache)
1473446Smrj {
1483446Smrj     ACPI_MEMORY_LIST        *Cache;
1493446Smrj 
1503446Smrj 
1513446Smrj     ACPI_FUNCTION_ENTRY ();
1523446Smrj 
1533446Smrj 
1543446Smrj     if (!CacheName || !ReturnCache || (ObjectSize < 16))
1553446Smrj     {
1563446Smrj         return (AE_BAD_PARAMETER);
1573446Smrj     }
1583446Smrj 
1593446Smrj     /* Create the cache object */
1603446Smrj 
1613446Smrj     Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
1623446Smrj     if (!Cache)
1633446Smrj     {
1643446Smrj         return (AE_NO_MEMORY);
1653446Smrj     }
1663446Smrj 
1673446Smrj     /* Populate the cache object and return it */
1683446Smrj 
1693446Smrj     ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
1703446Smrj     Cache->LinkOffset = 8;
1713446Smrj     Cache->ListName   = CacheName;
1723446Smrj     Cache->ObjectSize = ObjectSize;
1733446Smrj     Cache->MaxDepth   = MaxDepth;
1743446Smrj 
1753446Smrj     *ReturnCache = Cache;
1763446Smrj     return (AE_OK);
1773446Smrj }
1783446Smrj 
1793446Smrj 
1803446Smrj /*******************************************************************************
1813446Smrj  *
1823446Smrj  * FUNCTION:    AcpiOsPurgeCache
1833446Smrj  *
1843446Smrj  * PARAMETERS:  Cache           - Handle to cache object
1853446Smrj  *
1863446Smrj  * RETURN:      Status
1873446Smrj  *
1883446Smrj  * DESCRIPTION: Free all objects within the requested cache.
1893446Smrj  *
1903446Smrj  ******************************************************************************/
1913446Smrj 
1923446Smrj ACPI_STATUS
1933446Smrj AcpiOsPurgeCache (
1943446Smrj     ACPI_MEMORY_LIST        *Cache)
1953446Smrj {
1963446Smrj     char                    *Next;
1973446Smrj 
1983446Smrj 
1993446Smrj     ACPI_FUNCTION_ENTRY ();
2003446Smrj 
2013446Smrj 
2023446Smrj     if (!Cache)
2033446Smrj     {
2043446Smrj         return (AE_BAD_PARAMETER);
2053446Smrj     }
2063446Smrj 
2073446Smrj     /* Walk the list of objects in this cache */
2083446Smrj 
2093446Smrj     while (Cache->ListHead)
2103446Smrj     {
2113446Smrj         /* Delete and unlink one cached state object */
2123446Smrj 
2133446Smrj         Next = *(ACPI_CAST_INDIRECT_PTR (char,
2143446Smrj                     &(((char *) Cache->ListHead)[Cache->LinkOffset])));
2153446Smrj         ACPI_FREE (Cache->ListHead);
2163446Smrj 
2173446Smrj         Cache->ListHead = Next;
2183446Smrj         Cache->CurrentDepth--;
2193446Smrj     }
2203446Smrj 
2213446Smrj     return (AE_OK);
2223446Smrj }
2233446Smrj 
2243446Smrj 
2253446Smrj /*******************************************************************************
2263446Smrj  *
2273446Smrj  * FUNCTION:    AcpiOsDeleteCache
2283446Smrj  *
2293446Smrj  * PARAMETERS:  Cache           - Handle to cache object
2303446Smrj  *
2313446Smrj  * RETURN:      Status
2323446Smrj  *
2333446Smrj  * DESCRIPTION: Free all objects within the requested cache and delete the
2343446Smrj  *              cache object.
2353446Smrj  *
2363446Smrj  ******************************************************************************/
2373446Smrj 
2383446Smrj ACPI_STATUS
2393446Smrj AcpiOsDeleteCache (
2403446Smrj     ACPI_MEMORY_LIST        *Cache)
2413446Smrj {
2423446Smrj     ACPI_STATUS             Status;
2433446Smrj 
2443446Smrj 
2453446Smrj     ACPI_FUNCTION_ENTRY ();
2463446Smrj 
2473446Smrj 
2483446Smrj    /* Purge all objects in the cache */
2493446Smrj 
2503446Smrj     Status = AcpiOsPurgeCache (Cache);
2513446Smrj     if (ACPI_FAILURE (Status))
2523446Smrj     {
2533446Smrj         return (Status);
2543446Smrj     }
2553446Smrj 
2563446Smrj     /* Now we can delete the cache object */
2573446Smrj 
2583446Smrj     AcpiOsFree (Cache);
2593446Smrj     return (AE_OK);
2603446Smrj }
2613446Smrj 
2623446Smrj 
2633446Smrj /*******************************************************************************
2643446Smrj  *
2653446Smrj  * FUNCTION:    AcpiOsReleaseObject
2663446Smrj  *
2673446Smrj  * PARAMETERS:  Cache       - Handle to cache object
2683446Smrj  *              Object      - The object to be released
2693446Smrj  *
2703446Smrj  * RETURN:      None
2713446Smrj  *
2723446Smrj  * DESCRIPTION: Release an object to the specified cache.  If cache is full,
2733446Smrj  *              the object is deleted.
2743446Smrj  *
2753446Smrj  ******************************************************************************/
2763446Smrj 
2773446Smrj ACPI_STATUS
2783446Smrj AcpiOsReleaseObject (
2793446Smrj     ACPI_MEMORY_LIST        *Cache,
2803446Smrj     void                    *Object)
2813446Smrj {
2823446Smrj     ACPI_STATUS             Status;
2833446Smrj 
2843446Smrj 
2853446Smrj     ACPI_FUNCTION_ENTRY ();
2863446Smrj 
2873446Smrj 
2883446Smrj     if (!Cache || !Object)
2893446Smrj     {
2903446Smrj         return (AE_BAD_PARAMETER);
2913446Smrj     }
2923446Smrj 
2933446Smrj     /* If cache is full, just free this object */
2943446Smrj 
2953446Smrj     if (Cache->CurrentDepth >= Cache->MaxDepth)
2963446Smrj     {
2973446Smrj         ACPI_FREE (Object);
2983446Smrj         ACPI_MEM_TRACKING (Cache->TotalFreed++);
2993446Smrj     }
3003446Smrj 
3013446Smrj     /* Otherwise put this object back into the cache */
3023446Smrj 
3033446Smrj     else
3043446Smrj     {
3053446Smrj         Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
3063446Smrj         if (ACPI_FAILURE (Status))
3073446Smrj         {
3083446Smrj             return (Status);
3093446Smrj         }
3103446Smrj 
3113446Smrj         /* Mark the object as cached */
3123446Smrj 
3133446Smrj         ACPI_MEMSET (Object, 0xCA, Cache->ObjectSize);
3143446Smrj         ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_CACHED);
3153446Smrj 
3163446Smrj         /* Put the object at the head of the cache list */
3173446Smrj 
3183446Smrj         * (ACPI_CAST_INDIRECT_PTR (char,
3193446Smrj             &(((char *) Object)[Cache->LinkOffset]))) = Cache->ListHead;
3203446Smrj         Cache->ListHead = Object;
3213446Smrj         Cache->CurrentDepth++;
3223446Smrj 
3233446Smrj         (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
3243446Smrj     }
3253446Smrj 
3263446Smrj     return (AE_OK);
3273446Smrj }
3283446Smrj 
3293446Smrj 
3303446Smrj /*******************************************************************************
3313446Smrj  *
3323446Smrj  * FUNCTION:    AcpiOsAcquireObject
3333446Smrj  *
3343446Smrj  * PARAMETERS:  Cache           - Handle to cache object
3353446Smrj  *
3363446Smrj  * RETURN:      the acquired object.  NULL on error
3373446Smrj  *
3383446Smrj  * DESCRIPTION: Get an object from the specified cache.  If cache is empty,
3393446Smrj  *              the object is allocated.
3403446Smrj  *
3413446Smrj  ******************************************************************************/
3423446Smrj 
3433446Smrj void *
3443446Smrj AcpiOsAcquireObject (
3453446Smrj     ACPI_MEMORY_LIST        *Cache)
3463446Smrj {
3473446Smrj     ACPI_STATUS             Status;
3483446Smrj     void                    *Object;
3493446Smrj 
3503446Smrj 
3513446Smrj     ACPI_FUNCTION_NAME (OsAcquireObject);
3523446Smrj 
3533446Smrj 
3543446Smrj     if (!Cache)
3553446Smrj     {
3563446Smrj         return (NULL);
3573446Smrj     }
3583446Smrj 
3593446Smrj     Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
3603446Smrj     if (ACPI_FAILURE (Status))
3613446Smrj     {
3623446Smrj         return (NULL);
3633446Smrj     }
3643446Smrj 
3653446Smrj     ACPI_MEM_TRACKING (Cache->Requests++);
3663446Smrj 
3673446Smrj     /* Check the cache first */
3683446Smrj 
3693446Smrj     if (Cache->ListHead)
3703446Smrj     {
3713446Smrj         /* There is an object available, use it */
3723446Smrj 
3733446Smrj         Object = Cache->ListHead;
3743446Smrj         Cache->ListHead = *(ACPI_CAST_INDIRECT_PTR (char,
3753446Smrj                                 &(((char *) Object)[Cache->LinkOffset])));
3763446Smrj 
3773446Smrj         Cache->CurrentDepth--;
3783446Smrj 
3793446Smrj         ACPI_MEM_TRACKING (Cache->Hits++);
3803446Smrj         ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
3813446Smrj             "Object %p from %s cache\n", Object, Cache->ListName));
3823446Smrj 
3833446Smrj         Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
3843446Smrj         if (ACPI_FAILURE (Status))
3853446Smrj         {
3863446Smrj             return (NULL);
3873446Smrj         }
3883446Smrj 
3893446Smrj         /* Clear (zero) the previously used Object */
3903446Smrj 
3913446Smrj         ACPI_MEMSET (Object, 0, Cache->ObjectSize);
3923446Smrj     }
3933446Smrj     else
3943446Smrj     {
3953446Smrj         /* The cache is empty, create a new object */
3963446Smrj 
3973446Smrj         ACPI_MEM_TRACKING (Cache->TotalAllocated++);
3983446Smrj 
399*7851SDana.Myers@Sun.COM #ifdef ACPI_DBG_TRACK_ALLOCATIONS
400*7851SDana.Myers@Sun.COM         if ((Cache->TotalAllocated - Cache->TotalFreed) > Cache->MaxOccupied)
401*7851SDana.Myers@Sun.COM         {
402*7851SDana.Myers@Sun.COM             Cache->MaxOccupied = Cache->TotalAllocated - Cache->TotalFreed;
403*7851SDana.Myers@Sun.COM         }
404*7851SDana.Myers@Sun.COM #endif
405*7851SDana.Myers@Sun.COM 
4063446Smrj         /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
4073446Smrj 
4083446Smrj         Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
4093446Smrj         if (ACPI_FAILURE (Status))
4103446Smrj         {
4113446Smrj             return (NULL);
4123446Smrj         }
4133446Smrj 
4143446Smrj         Object = ACPI_ALLOCATE_ZEROED (Cache->ObjectSize);
4153446Smrj         if (!Object)
4163446Smrj         {
4173446Smrj             return (NULL);
4183446Smrj         }
4193446Smrj     }
4203446Smrj 
4213446Smrj     return (Object);
4223446Smrj }
4233446Smrj #endif /* ACPI_USE_LOCAL_CACHE */
4243446Smrj 
4253446Smrj 
426