Merge tag 'trace-v5.11-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/rostedt...
[linux/fpc-iii.git] / drivers / acpi / acpica / utcache.c
blob1b03a2747401e8e781c338fd20c17d5d01e0fac1
1 // SPDX-License-Identifier: BSD-3-Clause OR GPL-2.0
2 /******************************************************************************
4 * Module Name: utcache - local cache allocation routines
6 * Copyright (C) 2000 - 2020, Intel Corp.
8 *****************************************************************************/
10 #include <acpi/acpi.h>
11 #include "accommon.h"
13 #define _COMPONENT ACPI_UTILITIES
14 ACPI_MODULE_NAME("utcache")
16 #ifdef ACPI_USE_LOCAL_CACHE
17 /*******************************************************************************
19 * FUNCTION: acpi_os_create_cache
21 * PARAMETERS: cache_name - Ascii name for the cache
22 * object_size - Size of each cached object
23 * max_depth - Maximum depth of the cache (in objects)
24 * return_cache - Where the new cache object is returned
26 * RETURN: Status
28 * DESCRIPTION: Create a cache object
30 ******************************************************************************/
31 acpi_status
32 acpi_os_create_cache(char *cache_name,
33 u16 object_size,
34 u16 max_depth, struct acpi_memory_list **return_cache)
36 struct acpi_memory_list *cache;
38 ACPI_FUNCTION_ENTRY();
40 if (!cache_name || !return_cache || !object_size) {
41 return (AE_BAD_PARAMETER);
44 /* Create the cache object */
46 cache = acpi_os_allocate(sizeof(struct acpi_memory_list));
47 if (!cache) {
48 return (AE_NO_MEMORY);
51 /* Populate the cache object and return it */
53 memset(cache, 0, sizeof(struct acpi_memory_list));
54 cache->list_name = cache_name;
55 cache->object_size = object_size;
56 cache->max_depth = max_depth;
58 *return_cache = cache;
59 return (AE_OK);
62 /*******************************************************************************
64 * FUNCTION: acpi_os_purge_cache
66 * PARAMETERS: cache - Handle to cache object
68 * RETURN: Status
70 * DESCRIPTION: Free all objects within the requested cache.
72 ******************************************************************************/
74 acpi_status acpi_os_purge_cache(struct acpi_memory_list *cache)
76 void *next;
77 acpi_status status;
79 ACPI_FUNCTION_ENTRY();
81 if (!cache) {
82 return (AE_BAD_PARAMETER);
85 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
86 if (ACPI_FAILURE(status)) {
87 return (status);
90 /* Walk the list of objects in this cache */
92 while (cache->list_head) {
94 /* Delete and unlink one cached state object */
96 next = ACPI_GET_DESCRIPTOR_PTR(cache->list_head);
97 ACPI_FREE(cache->list_head);
99 cache->list_head = next;
100 cache->current_depth--;
103 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
104 return (AE_OK);
107 /*******************************************************************************
109 * FUNCTION: acpi_os_delete_cache
111 * PARAMETERS: cache - Handle to cache object
113 * RETURN: Status
115 * DESCRIPTION: Free all objects within the requested cache and delete the
116 * cache object.
118 ******************************************************************************/
120 acpi_status acpi_os_delete_cache(struct acpi_memory_list *cache)
122 acpi_status status;
124 ACPI_FUNCTION_ENTRY();
126 /* Purge all objects in the cache */
128 status = acpi_os_purge_cache(cache);
129 if (ACPI_FAILURE(status)) {
130 return (status);
133 /* Now we can delete the cache object */
135 acpi_os_free(cache);
136 return (AE_OK);
139 /*******************************************************************************
141 * FUNCTION: acpi_os_release_object
143 * PARAMETERS: cache - Handle to cache object
144 * object - The object to be released
146 * RETURN: None
148 * DESCRIPTION: Release an object to the specified cache. If cache is full,
149 * the object is deleted.
151 ******************************************************************************/
153 acpi_status acpi_os_release_object(struct acpi_memory_list *cache, void *object)
155 acpi_status status;
157 ACPI_FUNCTION_ENTRY();
159 if (!cache || !object) {
160 return (AE_BAD_PARAMETER);
163 /* If cache is full, just free this object */
165 if (cache->current_depth >= cache->max_depth) {
166 ACPI_FREE(object);
167 ACPI_MEM_TRACKING(cache->total_freed++);
170 /* Otherwise put this object back into the cache */
172 else {
173 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
174 if (ACPI_FAILURE(status)) {
175 return (status);
178 /* Mark the object as cached */
180 memset(object, 0xCA, cache->object_size);
181 ACPI_SET_DESCRIPTOR_TYPE(object, ACPI_DESC_TYPE_CACHED);
183 /* Put the object at the head of the cache list */
185 ACPI_SET_DESCRIPTOR_PTR(object, cache->list_head);
186 cache->list_head = object;
187 cache->current_depth++;
189 (void)acpi_ut_release_mutex(ACPI_MTX_CACHES);
192 return (AE_OK);
195 /*******************************************************************************
197 * FUNCTION: acpi_os_acquire_object
199 * PARAMETERS: cache - Handle to cache object
201 * RETURN: the acquired object. NULL on error
203 * DESCRIPTION: Get an object from the specified cache. If cache is empty,
204 * the object is allocated.
206 ******************************************************************************/
208 void *acpi_os_acquire_object(struct acpi_memory_list *cache)
210 acpi_status status;
211 void *object;
213 ACPI_FUNCTION_TRACE(os_acquire_object);
215 if (!cache) {
216 return_PTR(NULL);
219 status = acpi_ut_acquire_mutex(ACPI_MTX_CACHES);
220 if (ACPI_FAILURE(status)) {
221 return_PTR(NULL);
224 ACPI_MEM_TRACKING(cache->requests++);
226 /* Check the cache first */
228 if (cache->list_head) {
230 /* There is an object available, use it */
232 object = cache->list_head;
233 cache->list_head = ACPI_GET_DESCRIPTOR_PTR(object);
235 cache->current_depth--;
237 ACPI_MEM_TRACKING(cache->hits++);
238 ACPI_DEBUG_PRINT_RAW((ACPI_DB_EXEC,
239 "%s: Object %p from %s cache\n",
240 ACPI_GET_FUNCTION_NAME, object,
241 cache->list_name));
243 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
244 if (ACPI_FAILURE(status)) {
245 return_PTR(NULL);
248 /* Clear (zero) the previously used Object */
250 memset(object, 0, cache->object_size);
251 } else {
252 /* The cache is empty, create a new object */
254 ACPI_MEM_TRACKING(cache->total_allocated++);
256 #ifdef ACPI_DBG_TRACK_ALLOCATIONS
257 if ((cache->total_allocated - cache->total_freed) >
258 cache->max_occupied) {
259 cache->max_occupied =
260 cache->total_allocated - cache->total_freed;
262 #endif
264 /* Avoid deadlock with ACPI_ALLOCATE_ZEROED */
266 status = acpi_ut_release_mutex(ACPI_MTX_CACHES);
267 if (ACPI_FAILURE(status)) {
268 return_PTR(NULL);
271 object = ACPI_ALLOCATE_ZEROED(cache->object_size);
272 if (!object) {
273 return_PTR(NULL);
277 return_PTR(object);
279 #endif /* ACPI_USE_LOCAL_CACHE */