• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012-2018 Rob Clark <robclark@freedesktop.org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21  * SOFTWARE.
22  *
23  * Authors:
24  *    Rob Clark <robclark@freedesktop.org>
25  */
26 
27 #include "freedreno_drmif.h"
28 #include "freedreno_priv.h"
29 
30 void bo_del(struct fd_bo *bo);
31 extern simple_mtx_t table_lock;
32 
33 static void
add_bucket(struct fd_bo_cache * cache,int size)34 add_bucket(struct fd_bo_cache *cache, int size)
35 {
36    unsigned int i = cache->num_buckets;
37 
38    assert(i < ARRAY_SIZE(cache->cache_bucket));
39 
40    list_inithead(&cache->cache_bucket[i].list);
41    cache->cache_bucket[i].size = size;
42    cache->num_buckets++;
43 }
44 
45 /**
46  * @coarse: if true, only power-of-two bucket sizes, otherwise
47  *    fill in for a bit smoother size curve..
48  */
49 void
fd_bo_cache_init(struct fd_bo_cache * cache,int coarse)50 fd_bo_cache_init(struct fd_bo_cache *cache, int coarse)
51 {
52    unsigned long size, cache_max_size = 64 * 1024 * 1024;
53 
54    /* OK, so power of two buckets was too wasteful of memory.
55     * Give 3 other sizes between each power of two, to hopefully
56     * cover things accurately enough.  (The alternative is
57     * probably to just go for exact matching of sizes, and assume
58     * that for things like composited window resize the tiled
59     * width/height alignment and rounding of sizes to pages will
60     * get us useful cache hit rates anyway)
61     */
62    add_bucket(cache, 4096);
63    add_bucket(cache, 4096 * 2);
64    if (!coarse)
65       add_bucket(cache, 4096 * 3);
66 
67    /* Initialize the linked lists for BO reuse cache. */
68    for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
69       add_bucket(cache, size);
70       if (!coarse) {
71          add_bucket(cache, size + size * 1 / 4);
72          add_bucket(cache, size + size * 2 / 4);
73          add_bucket(cache, size + size * 3 / 4);
74       }
75    }
76 }
77 
78 /* Frees older cached buffers.  Called under table_lock */
79 void
fd_bo_cache_cleanup(struct fd_bo_cache * cache,time_t time)80 fd_bo_cache_cleanup(struct fd_bo_cache *cache, time_t time)
81 {
82    int i;
83 
84    simple_mtx_assert_locked(&table_lock);
85 
86    if (cache->time == time)
87       return;
88 
89    for (i = 0; i < cache->num_buckets; i++) {
90       struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
91       struct fd_bo *bo;
92 
93       while (!list_is_empty(&bucket->list)) {
94          bo = list_entry(bucket->list.next, struct fd_bo, list);
95 
96          /* keep things in cache for at least 1 second: */
97          if (time && ((time - bo->free_time) <= 1))
98             break;
99 
100          VG_BO_OBTAIN(bo);
101          list_del(&bo->list);
102          bo_del(bo);
103       }
104    }
105 
106    cache->time = time;
107 }
108 
109 static struct fd_bo_bucket *
get_bucket(struct fd_bo_cache * cache,uint32_t size)110 get_bucket(struct fd_bo_cache *cache, uint32_t size)
111 {
112    int i;
113 
114    /* hmm, this is what intel does, but I suppose we could calculate our
115     * way to the correct bucket size rather than looping..
116     */
117    for (i = 0; i < cache->num_buckets; i++) {
118       struct fd_bo_bucket *bucket = &cache->cache_bucket[i];
119       if (bucket->size >= size) {
120          return bucket;
121       }
122    }
123 
124    return NULL;
125 }
126 
127 static struct fd_bo *
find_in_bucket(struct fd_bo_bucket * bucket,uint32_t flags)128 find_in_bucket(struct fd_bo_bucket *bucket, uint32_t flags)
129 {
130    struct fd_bo *bo = NULL;
131 
132    /* TODO .. if we had an ALLOC_FOR_RENDER flag like intel, we could
133     * skip the busy check.. if it is only going to be a render target
134     * then we probably don't need to stall..
135     *
136     * NOTE that intel takes ALLOC_FOR_RENDER bo's from the list tail
137     * (MRU, since likely to be in GPU cache), rather than head (LRU)..
138     */
139    simple_mtx_lock(&table_lock);
140    list_for_each_entry (struct fd_bo, entry, &bucket->list, list) {
141       if (fd_bo_state(entry) != FD_BO_STATE_IDLE)
142          break;
143       if (entry->alloc_flags == flags) {
144          bo = entry;
145          list_delinit(&bo->list);
146          break;
147       }
148    }
149    simple_mtx_unlock(&table_lock);
150 
151    return bo;
152 }
153 
154 /* NOTE: size is potentially rounded up to bucket size: */
155 struct fd_bo *
fd_bo_cache_alloc(struct fd_bo_cache * cache,uint32_t * size,uint32_t flags)156 fd_bo_cache_alloc(struct fd_bo_cache *cache, uint32_t *size, uint32_t flags)
157 {
158    struct fd_bo *bo = NULL;
159    struct fd_bo_bucket *bucket;
160 
161    *size = align(*size, 4096);
162    bucket = get_bucket(cache, *size);
163 
164    /* see if we can be green and recycle: */
165 retry:
166    if (bucket) {
167       *size = bucket->size;
168       bo = find_in_bucket(bucket, flags);
169       if (bo) {
170          VG_BO_OBTAIN(bo);
171          if (bo->funcs->madvise(bo, true) <= 0) {
172             /* we've lost the backing pages, delete and try again: */
173             simple_mtx_lock(&table_lock);
174             bo_del(bo);
175             simple_mtx_unlock(&table_lock);
176             goto retry;
177          }
178          p_atomic_set(&bo->refcnt, 1);
179          bo->reloc_flags = FD_RELOC_FLAGS_INIT;
180          return bo;
181       }
182    }
183 
184    return NULL;
185 }
186 
187 int
fd_bo_cache_free(struct fd_bo_cache * cache,struct fd_bo * bo)188 fd_bo_cache_free(struct fd_bo_cache *cache, struct fd_bo *bo)
189 {
190    simple_mtx_assert_locked(&table_lock);
191 
192    if (bo->nosync || bo->shared)
193       return -1;
194 
195    struct fd_bo_bucket *bucket = get_bucket(cache, bo->size);
196 
197    /* see if we can be green and recycle: */
198    if (bucket) {
199       struct timespec time;
200 
201       bo->funcs->madvise(bo, false);
202 
203       clock_gettime(CLOCK_MONOTONIC, &time);
204 
205       bo->free_time = time.tv_sec;
206       VG_BO_RELEASE(bo);
207       list_addtail(&bo->list, &bucket->list);
208       fd_bo_cache_cleanup(cache, time.tv_sec);
209 
210       return 0;
211    }
212 
213    return -1;
214 }
215