1 /* -*- mode: C; c-file-style: "k&r"; tab-width 4; indent-tabs-mode: t; -*- */
4 * Copyright (C) 2016 Rob Clark <robclark@freedesktop.org>
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice (including the next
14 * paragraph) shall be included in all copies or substantial portions of the
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
20 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26 * Rob Clark <robclark@freedesktop.org>
33 #include "freedreno_drmif.h"
34 #include "freedreno_priv.h"
36 drm_private void bo_del(struct fd_bo *bo);
37 drm_private extern pthread_mutex_t table_lock;
40 add_bucket(struct fd_bo_cache *cache, int size)
42 unsigned int i = cache->num_buckets;
44 assert(i < ARRAY_SIZE(cache->cache_bucket));
46 list_inithead(&cache->cache_bucket[i].list);
47 cache->cache_bucket[i].size = size;
52 * @coarse: if true, only power-of-two bucket sizes, otherwise
53 * fill in for a bit smoother size curve..
56 fd_bo_cache_init(struct fd_bo_cache *cache, int course)
58 unsigned long size, cache_max_size = 64 * 1024 * 1024;
60 /* OK, so power of two buckets was too wasteful of memory.
61 * Give 3 other sizes between each power of two, to hopefully
62 * cover things accurately enough. (The alternative is
63 * probably to just go for exact matching of sizes, and assume
64 * that for things like composited window resize the tiled
65 * width/height alignment and rounding of sizes to pages will
66 * get us useful cache hit rates anyway)
68 add_bucket(cache, 4096);
69 add_bucket(cache, 4096 * 2);
71 add_bucket(cache, 4096 * 3);
73 /* Initialize the linked lists for BO reuse cache. */
74 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
75 add_bucket(cache, size);
77 add_bucket(cache, size + size * 1 / 4);
78 add_bucket(cache, size + size * 2 / 4);
79 add_bucket(cache, size + size * 3 / 4);
84 /* Frees older cached buffers. Called under table_lock */
86 fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time)
90 if (cache->time == time)
93 for (i = 0; i < cache->num_buckets; i++) {
94 struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
97 while (!LIST_IS_EMPTY(&bucket->list)) {
98 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
100 /* keep things in cache for at least 1 second: */
101 if (time && ((time - bo->free_time) <= 1))
113 static struct fd_bo_bucket * get_bucket(struct fd_bo_cache *cache, uint32_t size)
117 /* hmm, this is what intel does, but I suppose we could calculate our
118 * way to the correct bucket size rather than looping..
120 for (i = 0; i < cache->num_buckets; i++) {
121 struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
122 if (bucket->size >= size) {
130 static int is_idle(struct fd_bo *bo)
132 return fd_bo_cpu_prep(bo, NULL,
133 DRM_FREEDRENO_PREP_READ |
134 DRM_FREEDRENO_PREP_WRITE |
135 DRM_FREEDRENO_PREP_NOSYNC) == 0;
138 static struct fd_bo *find_in_bucket(struct fd_bo_bucket *bucket, uint32_t flags)
140 struct fd_bo *bo = NULL;
142 /* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could
143 * skip the busy check.. if it is only going to be a render target
144 * then we probably don't need to stall..
146 * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail
147 * (MRU, since likely to be in GPU cache), rather than head (LRU)..
149 pthread_mutex_lock(&table_lock);
150 if (!LIST_IS_EMPTY(&bucket->list)) {
151 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
152 /* TODO check for compatible flags? */
159 pthread_mutex_unlock(&table_lock);
164 /* NOTE: size is potentially rounded up to bucket size: */
165 drm_private struct fd_bo *
166 fd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size, uint32_t flags)
168 struct fd_bo *bo = NULL;
169 struct fd_bo_bucket *bucket;
171 *size = ALIGN(*size, 4096);
172 bucket = get_bucket(cache, *size);
174 /* see if we can be green and recycle: */
177 *size = bucket->size;
178 bo = find_in_bucket(bucket, flags);
181 if (bo->funcs->madvise(bo, TRUE) <= 0) {
182 /* we've lost the backing pages, delete and try again: */
183 pthread_mutex_lock(&table_lock);
185 pthread_mutex_unlock(&table_lock);
188 atomic_set(&bo->refcnt, 1);
189 fd_device_ref(bo->dev);
198 fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo)
200 struct fd_bo_bucket *bucket = get_bucket(cache, bo->size);
202 /* see if we can be green and recycle: */
204 struct timespec time;
206 bo->funcs->madvise(bo, FALSE);
208 clock_gettime(CLOCK_MONOTONIC, &time);
210 bo->free_time = time.tv_sec;
212 list_addtail(&bo->list, &bucket->list);
213 fd_bo_cache_cleanup(cache, time.tv_sec);
215 /* bo's in the bucket cache don't have a ref and
216 * don't hold a ref to the dev:
218 fd_device_del_locked(bo->dev);