1433d6423SLionel Sambuc /******************************************************************************
2433d6423SLionel Sambuc *
3433d6423SLionel Sambuc * Module Name: utcache - local cache allocation routines
4433d6423SLionel Sambuc *
5433d6423SLionel Sambuc *****************************************************************************/
6433d6423SLionel Sambuc
7*29492bb7SDavid van Moolenbroek /*
8*29492bb7SDavid van Moolenbroek * Copyright (C) 2000 - 2014, Intel Corp.
9433d6423SLionel Sambuc * All rights reserved.
10433d6423SLionel Sambuc *
11*29492bb7SDavid van Moolenbroek * Redistribution and use in source and binary forms, with or without
12*29492bb7SDavid van Moolenbroek * modification, are permitted provided that the following conditions
13*29492bb7SDavid van Moolenbroek * are met:
14*29492bb7SDavid van Moolenbroek * 1. Redistributions of source code must retain the above copyright
15*29492bb7SDavid van Moolenbroek * notice, this list of conditions, and the following disclaimer,
16*29492bb7SDavid van Moolenbroek * without modification.
17*29492bb7SDavid van Moolenbroek * 2. Redistributions in binary form must reproduce at minimum a disclaimer
18*29492bb7SDavid van Moolenbroek * substantially similar to the "NO WARRANTY" disclaimer below
19*29492bb7SDavid van Moolenbroek * ("Disclaimer") and any redistribution must be conditioned upon
20*29492bb7SDavid van Moolenbroek * including a substantially similar Disclaimer requirement for further
21*29492bb7SDavid van Moolenbroek * binary redistribution.
22*29492bb7SDavid van Moolenbroek * 3. Neither the names of the above-listed copyright holders nor the names
23*29492bb7SDavid van Moolenbroek * of any contributors may be used to endorse or promote products derived
24*29492bb7SDavid van Moolenbroek * from this software without specific prior written permission.
25433d6423SLionel Sambuc *
26*29492bb7SDavid van Moolenbroek * Alternatively, this software may be distributed under the terms of the
27*29492bb7SDavid van Moolenbroek * GNU General Public License ("GPL") version 2 as published by the Free
28*29492bb7SDavid van Moolenbroek * Software Foundation.
29433d6423SLionel Sambuc *
30*29492bb7SDavid van Moolenbroek * NO WARRANTY
31*29492bb7SDavid van Moolenbroek * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32*29492bb7SDavid van Moolenbroek * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33*29492bb7SDavid van Moolenbroek * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR
34*29492bb7SDavid van Moolenbroek * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35*29492bb7SDavid van Moolenbroek * HOLDERS OR CONTRIBUTORS BE LIABLE FOR SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
36*29492bb7SDavid van Moolenbroek * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
37*29492bb7SDavid van Moolenbroek * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
38*29492bb7SDavid van Moolenbroek * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39*29492bb7SDavid van Moolenbroek * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
40*29492bb7SDavid van Moolenbroek * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
41*29492bb7SDavid van Moolenbroek * POSSIBILITY OF SUCH DAMAGES.
42*29492bb7SDavid van Moolenbroek */
43433d6423SLionel Sambuc
44433d6423SLionel Sambuc #include "acpi.h"
45433d6423SLionel Sambuc #include "accommon.h"
46433d6423SLionel Sambuc
47433d6423SLionel Sambuc #define _COMPONENT ACPI_UTILITIES
48433d6423SLionel Sambuc ACPI_MODULE_NAME ("utcache")
49433d6423SLionel Sambuc
50433d6423SLionel Sambuc
51433d6423SLionel Sambuc #ifdef ACPI_USE_LOCAL_CACHE
52433d6423SLionel Sambuc /*******************************************************************************
53433d6423SLionel Sambuc *
54433d6423SLionel Sambuc * FUNCTION: AcpiOsCreateCache
55433d6423SLionel Sambuc *
56433d6423SLionel Sambuc * PARAMETERS: CacheName - Ascii name for the cache
57433d6423SLionel Sambuc * ObjectSize - Size of each cached object
58433d6423SLionel Sambuc * MaxDepth - Maximum depth of the cache (in objects)
59433d6423SLionel Sambuc * ReturnCache - Where the new cache object is returned
60433d6423SLionel Sambuc *
61433d6423SLionel Sambuc * RETURN: Status
62433d6423SLionel Sambuc *
63433d6423SLionel Sambuc * DESCRIPTION: Create a cache object
64433d6423SLionel Sambuc *
65433d6423SLionel Sambuc ******************************************************************************/
66433d6423SLionel Sambuc
67433d6423SLionel Sambuc ACPI_STATUS
AcpiOsCreateCache(char * CacheName,UINT16 ObjectSize,UINT16 MaxDepth,ACPI_MEMORY_LIST ** ReturnCache)68433d6423SLionel Sambuc AcpiOsCreateCache (
69433d6423SLionel Sambuc char *CacheName,
70433d6423SLionel Sambuc UINT16 ObjectSize,
71433d6423SLionel Sambuc UINT16 MaxDepth,
72433d6423SLionel Sambuc ACPI_MEMORY_LIST **ReturnCache)
73433d6423SLionel Sambuc {
74433d6423SLionel Sambuc ACPI_MEMORY_LIST *Cache;
75433d6423SLionel Sambuc
76433d6423SLionel Sambuc
77433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
78433d6423SLionel Sambuc
79433d6423SLionel Sambuc
80433d6423SLionel Sambuc if (!CacheName || !ReturnCache || (ObjectSize < 16))
81433d6423SLionel Sambuc {
82433d6423SLionel Sambuc return (AE_BAD_PARAMETER);
83433d6423SLionel Sambuc }
84433d6423SLionel Sambuc
85433d6423SLionel Sambuc /* Create the cache object */
86433d6423SLionel Sambuc
87433d6423SLionel Sambuc Cache = AcpiOsAllocate (sizeof (ACPI_MEMORY_LIST));
88433d6423SLionel Sambuc if (!Cache)
89433d6423SLionel Sambuc {
90433d6423SLionel Sambuc return (AE_NO_MEMORY);
91433d6423SLionel Sambuc }
92433d6423SLionel Sambuc
93433d6423SLionel Sambuc /* Populate the cache object and return it */
94433d6423SLionel Sambuc
95433d6423SLionel Sambuc ACPI_MEMSET (Cache, 0, sizeof (ACPI_MEMORY_LIST));
96433d6423SLionel Sambuc Cache->ListName = CacheName;
97433d6423SLionel Sambuc Cache->ObjectSize = ObjectSize;
98433d6423SLionel Sambuc Cache->MaxDepth = MaxDepth;
99433d6423SLionel Sambuc
100433d6423SLionel Sambuc *ReturnCache = Cache;
101433d6423SLionel Sambuc return (AE_OK);
102433d6423SLionel Sambuc }
103433d6423SLionel Sambuc
104433d6423SLionel Sambuc
105433d6423SLionel Sambuc /*******************************************************************************
106433d6423SLionel Sambuc *
107433d6423SLionel Sambuc * FUNCTION: AcpiOsPurgeCache
108433d6423SLionel Sambuc *
109433d6423SLionel Sambuc * PARAMETERS: Cache - Handle to cache object
110433d6423SLionel Sambuc *
111433d6423SLionel Sambuc * RETURN: Status
112433d6423SLionel Sambuc *
113433d6423SLionel Sambuc * DESCRIPTION: Free all objects within the requested cache.
114433d6423SLionel Sambuc *
115433d6423SLionel Sambuc ******************************************************************************/
116433d6423SLionel Sambuc
117433d6423SLionel Sambuc ACPI_STATUS
AcpiOsPurgeCache(ACPI_MEMORY_LIST * Cache)118433d6423SLionel Sambuc AcpiOsPurgeCache (
119433d6423SLionel Sambuc ACPI_MEMORY_LIST *Cache)
120433d6423SLionel Sambuc {
121*29492bb7SDavid van Moolenbroek void *Next;
122433d6423SLionel Sambuc ACPI_STATUS Status;
123433d6423SLionel Sambuc
124433d6423SLionel Sambuc
125433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
126433d6423SLionel Sambuc
127433d6423SLionel Sambuc
128433d6423SLionel Sambuc if (!Cache)
129433d6423SLionel Sambuc {
130433d6423SLionel Sambuc return (AE_BAD_PARAMETER);
131433d6423SLionel Sambuc }
132433d6423SLionel Sambuc
133433d6423SLionel Sambuc Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
134433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
135433d6423SLionel Sambuc {
136433d6423SLionel Sambuc return (Status);
137433d6423SLionel Sambuc }
138433d6423SLionel Sambuc
139433d6423SLionel Sambuc /* Walk the list of objects in this cache */
140433d6423SLionel Sambuc
141433d6423SLionel Sambuc while (Cache->ListHead)
142433d6423SLionel Sambuc {
143433d6423SLionel Sambuc /* Delete and unlink one cached state object */
144433d6423SLionel Sambuc
145*29492bb7SDavid van Moolenbroek Next = ACPI_GET_DESCRIPTOR_PTR (Cache->ListHead);
146433d6423SLionel Sambuc ACPI_FREE (Cache->ListHead);
147433d6423SLionel Sambuc
148433d6423SLionel Sambuc Cache->ListHead = Next;
149433d6423SLionel Sambuc Cache->CurrentDepth--;
150433d6423SLionel Sambuc }
151433d6423SLionel Sambuc
152433d6423SLionel Sambuc (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
153433d6423SLionel Sambuc return (AE_OK);
154433d6423SLionel Sambuc }
155433d6423SLionel Sambuc
156433d6423SLionel Sambuc
157433d6423SLionel Sambuc /*******************************************************************************
158433d6423SLionel Sambuc *
159433d6423SLionel Sambuc * FUNCTION: AcpiOsDeleteCache
160433d6423SLionel Sambuc *
161433d6423SLionel Sambuc * PARAMETERS: Cache - Handle to cache object
162433d6423SLionel Sambuc *
163433d6423SLionel Sambuc * RETURN: Status
164433d6423SLionel Sambuc *
165433d6423SLionel Sambuc * DESCRIPTION: Free all objects within the requested cache and delete the
166433d6423SLionel Sambuc * cache object.
167433d6423SLionel Sambuc *
168433d6423SLionel Sambuc ******************************************************************************/
169433d6423SLionel Sambuc
170433d6423SLionel Sambuc ACPI_STATUS
AcpiOsDeleteCache(ACPI_MEMORY_LIST * Cache)171433d6423SLionel Sambuc AcpiOsDeleteCache (
172433d6423SLionel Sambuc ACPI_MEMORY_LIST *Cache)
173433d6423SLionel Sambuc {
174433d6423SLionel Sambuc ACPI_STATUS Status;
175433d6423SLionel Sambuc
176433d6423SLionel Sambuc
177433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
178433d6423SLionel Sambuc
179433d6423SLionel Sambuc
180433d6423SLionel Sambuc /* Purge all objects in the cache */
181433d6423SLionel Sambuc
182433d6423SLionel Sambuc Status = AcpiOsPurgeCache (Cache);
183433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
184433d6423SLionel Sambuc {
185433d6423SLionel Sambuc return (Status);
186433d6423SLionel Sambuc }
187433d6423SLionel Sambuc
188433d6423SLionel Sambuc /* Now we can delete the cache object */
189433d6423SLionel Sambuc
190433d6423SLionel Sambuc AcpiOsFree (Cache);
191433d6423SLionel Sambuc return (AE_OK);
192433d6423SLionel Sambuc }
193433d6423SLionel Sambuc
194433d6423SLionel Sambuc
195433d6423SLionel Sambuc /*******************************************************************************
196433d6423SLionel Sambuc *
197433d6423SLionel Sambuc * FUNCTION: AcpiOsReleaseObject
198433d6423SLionel Sambuc *
199433d6423SLionel Sambuc * PARAMETERS: Cache - Handle to cache object
200433d6423SLionel Sambuc * Object - The object to be released
201433d6423SLionel Sambuc *
202433d6423SLionel Sambuc * RETURN: None
203433d6423SLionel Sambuc *
204433d6423SLionel Sambuc * DESCRIPTION: Release an object to the specified cache. If cache is full,
205433d6423SLionel Sambuc * the object is deleted.
206433d6423SLionel Sambuc *
207433d6423SLionel Sambuc ******************************************************************************/
208433d6423SLionel Sambuc
209433d6423SLionel Sambuc ACPI_STATUS
AcpiOsReleaseObject(ACPI_MEMORY_LIST * Cache,void * Object)210433d6423SLionel Sambuc AcpiOsReleaseObject (
211433d6423SLionel Sambuc ACPI_MEMORY_LIST *Cache,
212433d6423SLionel Sambuc void *Object)
213433d6423SLionel Sambuc {
214433d6423SLionel Sambuc ACPI_STATUS Status;
215433d6423SLionel Sambuc
216433d6423SLionel Sambuc
217433d6423SLionel Sambuc ACPI_FUNCTION_ENTRY ();
218433d6423SLionel Sambuc
219433d6423SLionel Sambuc
220433d6423SLionel Sambuc if (!Cache || !Object)
221433d6423SLionel Sambuc {
222433d6423SLionel Sambuc return (AE_BAD_PARAMETER);
223433d6423SLionel Sambuc }
224433d6423SLionel Sambuc
225433d6423SLionel Sambuc /* If cache is full, just free this object */
226433d6423SLionel Sambuc
227433d6423SLionel Sambuc if (Cache->CurrentDepth >= Cache->MaxDepth)
228433d6423SLionel Sambuc {
229433d6423SLionel Sambuc ACPI_FREE (Object);
230433d6423SLionel Sambuc ACPI_MEM_TRACKING (Cache->TotalFreed++);
231433d6423SLionel Sambuc }
232433d6423SLionel Sambuc
233433d6423SLionel Sambuc /* Otherwise put this object back into the cache */
234433d6423SLionel Sambuc
235433d6423SLionel Sambuc else
236433d6423SLionel Sambuc {
237433d6423SLionel Sambuc Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
238433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
239433d6423SLionel Sambuc {
240433d6423SLionel Sambuc return (Status);
241433d6423SLionel Sambuc }
242433d6423SLionel Sambuc
243433d6423SLionel Sambuc /* Mark the object as cached */
244433d6423SLionel Sambuc
245433d6423SLionel Sambuc ACPI_MEMSET (Object, 0xCA, Cache->ObjectSize);
246433d6423SLionel Sambuc ACPI_SET_DESCRIPTOR_TYPE (Object, ACPI_DESC_TYPE_CACHED);
247433d6423SLionel Sambuc
248433d6423SLionel Sambuc /* Put the object at the head of the cache list */
249433d6423SLionel Sambuc
250*29492bb7SDavid van Moolenbroek ACPI_SET_DESCRIPTOR_PTR (Object, Cache->ListHead);
251433d6423SLionel Sambuc Cache->ListHead = Object;
252433d6423SLionel Sambuc Cache->CurrentDepth++;
253433d6423SLionel Sambuc
254433d6423SLionel Sambuc (void) AcpiUtReleaseMutex (ACPI_MTX_CACHES);
255433d6423SLionel Sambuc }
256433d6423SLionel Sambuc
257433d6423SLionel Sambuc return (AE_OK);
258433d6423SLionel Sambuc }
259433d6423SLionel Sambuc
260433d6423SLionel Sambuc
261433d6423SLionel Sambuc /*******************************************************************************
262433d6423SLionel Sambuc *
263433d6423SLionel Sambuc * FUNCTION: AcpiOsAcquireObject
264433d6423SLionel Sambuc *
265433d6423SLionel Sambuc * PARAMETERS: Cache - Handle to cache object
266433d6423SLionel Sambuc *
267433d6423SLionel Sambuc * RETURN: the acquired object. NULL on error
268433d6423SLionel Sambuc *
269433d6423SLionel Sambuc * DESCRIPTION: Get an object from the specified cache. If cache is empty,
270433d6423SLionel Sambuc * the object is allocated.
271433d6423SLionel Sambuc *
272433d6423SLionel Sambuc ******************************************************************************/
273433d6423SLionel Sambuc
274433d6423SLionel Sambuc void *
AcpiOsAcquireObject(ACPI_MEMORY_LIST * Cache)275433d6423SLionel Sambuc AcpiOsAcquireObject (
276433d6423SLionel Sambuc ACPI_MEMORY_LIST *Cache)
277433d6423SLionel Sambuc {
278433d6423SLionel Sambuc ACPI_STATUS Status;
279433d6423SLionel Sambuc void *Object;
280433d6423SLionel Sambuc
281433d6423SLionel Sambuc
282433d6423SLionel Sambuc ACPI_FUNCTION_NAME (OsAcquireObject);
283433d6423SLionel Sambuc
284433d6423SLionel Sambuc
285433d6423SLionel Sambuc if (!Cache)
286433d6423SLionel Sambuc {
287*29492bb7SDavid van Moolenbroek return_PTR (NULL);
288433d6423SLionel Sambuc }
289433d6423SLionel Sambuc
290433d6423SLionel Sambuc Status = AcpiUtAcquireMutex (ACPI_MTX_CACHES);
291433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
292433d6423SLionel Sambuc {
293*29492bb7SDavid van Moolenbroek return_PTR (NULL);
294433d6423SLionel Sambuc }
295433d6423SLionel Sambuc
296433d6423SLionel Sambuc ACPI_MEM_TRACKING (Cache->Requests++);
297433d6423SLionel Sambuc
298433d6423SLionel Sambuc /* Check the cache first */
299433d6423SLionel Sambuc
300433d6423SLionel Sambuc if (Cache->ListHead)
301433d6423SLionel Sambuc {
302433d6423SLionel Sambuc /* There is an object available, use it */
303433d6423SLionel Sambuc
304433d6423SLionel Sambuc Object = Cache->ListHead;
305*29492bb7SDavid van Moolenbroek Cache->ListHead = ACPI_GET_DESCRIPTOR_PTR (Object);
306433d6423SLionel Sambuc
307433d6423SLionel Sambuc Cache->CurrentDepth--;
308433d6423SLionel Sambuc
309433d6423SLionel Sambuc ACPI_MEM_TRACKING (Cache->Hits++);
310433d6423SLionel Sambuc ACPI_DEBUG_PRINT ((ACPI_DB_EXEC,
311433d6423SLionel Sambuc "Object %p from %s cache\n", Object, Cache->ListName));
312433d6423SLionel Sambuc
313433d6423SLionel Sambuc Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
314433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
315433d6423SLionel Sambuc {
316*29492bb7SDavid van Moolenbroek return_PTR (NULL);
317433d6423SLionel Sambuc }
318433d6423SLionel Sambuc
319433d6423SLionel Sambuc /* Clear (zero) the previously used Object */
320433d6423SLionel Sambuc
321433d6423SLionel Sambuc ACPI_MEMSET (Object, 0, Cache->ObjectSize);
322433d6423SLionel Sambuc }
323433d6423SLionel Sambuc else
324433d6423SLionel Sambuc {
325433d6423SLionel Sambuc /* The cache is empty, create a new object */
326433d6423SLionel Sambuc
327433d6423SLionel Sambuc ACPI_MEM_TRACKING (Cache->TotalAllocated++);
328433d6423SLionel Sambuc
329433d6423SLionel Sambuc #ifdef ACPI_DBG_TRACK_ALLOCATIONS
330433d6423SLionel Sambuc if ((Cache->TotalAllocated - Cache->TotalFreed) > Cache->MaxOccupied)
331433d6423SLionel Sambuc {
332433d6423SLionel Sambuc Cache->MaxOccupied = Cache->TotalAllocated - Cache->TotalFreed;
333433d6423SLionel Sambuc }
334433d6423SLionel Sambuc #endif
335433d6423SLionel Sambuc
336433d6423SLionel Sambuc /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
337433d6423SLionel Sambuc
338433d6423SLionel Sambuc Status = AcpiUtReleaseMutex (ACPI_MTX_CACHES);
339433d6423SLionel Sambuc if (ACPI_FAILURE (Status))
340433d6423SLionel Sambuc {
341*29492bb7SDavid van Moolenbroek return_PTR (NULL);
342433d6423SLionel Sambuc }
343433d6423SLionel Sambuc
344433d6423SLionel Sambuc Object = ACPI_ALLOCATE_ZEROED (Cache->ObjectSize);
345433d6423SLionel Sambuc if (!Object)
346433d6423SLionel Sambuc {
347*29492bb7SDavid van Moolenbroek return_PTR (NULL);
348433d6423SLionel Sambuc }
349433d6423SLionel Sambuc }
350433d6423SLionel Sambuc
351*29492bb7SDavid van Moolenbroek return_PTR (Object);
352433d6423SLionel Sambuc }
353433d6423SLionel Sambuc #endif /* ACPI_USE_LOCAL_CACHE */
354