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>
29 #include "freedreno_drmif.h"
30 #include "freedreno_priv.h"
32 drm_private void bo_del(struct fd_bo *bo);
33 drm_private extern pthread_mutex_t table_lock;
36 add_bucket(struct fd_bo_cache *cache, int size)
38 unsigned int i = cache->num_buckets;
40 assert(i < ARRAY_SIZE(cache->cache_bucket));
42 list_inithead(&cache->cache_bucket[i].list);
43 cache->cache_bucket[i].size = size;
48 * @coarse: if true, only power-of-two bucket sizes, otherwise
49 * fill in for a bit smoother size curve..
52 fd_bo_cache_init(struct fd_bo_cache *cache, int course)
54 unsigned long size, cache_max_size = 64 * 1024 * 1024;
56 /* OK, so power of two buckets was too wasteful of memory.
57 * Give 3 other sizes between each power of two, to hopefully
58 * cover things accurately enough. (The alternative is
59 * probably to just go for exact matching of sizes, and assume
60 * that for things like composited window resize the tiled
61 * width/height alignment and rounding of sizes to pages will
62 * get us useful cache hit rates anyway)
64 add_bucket(cache, 4096);
65 add_bucket(cache, 4096 * 2);
67 add_bucket(cache, 4096 * 3);
69 /* Initialize the linked lists for BO reuse cache. */
70 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
71 add_bucket(cache, size);
73 add_bucket(cache, size + size * 1 / 4);
74 add_bucket(cache, size + size * 2 / 4);
75 add_bucket(cache, size + size * 3 / 4);
80 /* Frees older cached buffers. Called under table_lock */
82 fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time)
86 if (cache->time == time)
89 for (i = 0; i < cache->num_buckets; i++) {
90 struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
93 while (!LIST_IS_EMPTY(&bucket->list)) {
94 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
96 /* keep things in cache for at least 1 second: */
97 if (time && ((time - bo->free_time) <= 1))
109 static struct fd_bo_bucket * get_bucket(struct fd_bo_cache *cache, uint32_t size)
113 /* hmm, this is what intel does, but I suppose we could calculate our
114 * way to the correct bucket size rather than looping..
116 for (i = 0; i < cache->num_buckets; i++) {
117 struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
118 if (bucket->size >= size) {
126 static int is_idle(struct fd_bo *bo)
128 return fd_bo_cpu_prep(bo, NULL,
129 DRM_FREEDRENO_PREP_READ |
130 DRM_FREEDRENO_PREP_WRITE |
131 DRM_FREEDRENO_PREP_NOSYNC) == 0;
134 static struct fd_bo *find_in_bucket(struct fd_bo_bucket *bucket, uint32_t flags)
136 struct fd_bo *bo = NULL;
138 /* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could
139 * skip the busy check.. if it is only going to be a render target
140 * then we probably don't need to stall..
142 * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail
143 * (MRU, since likely to be in GPU cache), rather than head (LRU)..
145 pthread_mutex_lock(&table_lock);
146 if (!LIST_IS_EMPTY(&bucket->list)) {
147 bo = LIST_ENTRY(struct fd_bo, bucket->list.next, list);
148 /* TODO check for compatible flags? */
155 pthread_mutex_unlock(&table_lock);
160 /* NOTE: size is potentially rounded up to bucket size: */
161 drm_private struct fd_bo *
162 fd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size, uint32_t flags)
164 struct fd_bo *bo = NULL;
165 struct fd_bo_bucket *bucket;
167 *size = ALIGN(*size, 4096);
168 bucket = get_bucket(cache, *size);
170 /* see if we can be green and recycle: */
173 *size = bucket->size;
174 bo = find_in_bucket(bucket, flags);
177 if (bo->funcs->madvise(bo, TRUE) <= 0) {
178 /* we've lost the backing pages, delete and try again: */
179 pthread_mutex_lock(&table_lock);
181 pthread_mutex_unlock(&table_lock);
184 atomic_set(&bo->refcnt, 1);
185 fd_device_ref(bo->dev);
194 fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo)
196 struct fd_bo_bucket *bucket = get_bucket(cache, bo->size);
198 /* see if we can be green and recycle: */
200 struct timespec time;
202 bo->funcs->madvise(bo, FALSE);
204 clock_gettime(CLOCK_MONOTONIC, &time);
206 bo->free_time = time.tv_sec;
208 list_addtail(&bo->list, &bucket->list);
209 fd_bo_cache_cleanup(cache, time.tv_sec);
211 /* bo's in the bucket cache don't have a ref and
212 * don't hold a ref to the dev:
214 fd_device_del_locked(bo->dev);