1 /*
2 * Copyright (C) 2016 Etnaviv Project
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 * Christian Gmeiner <christian.gmeiner@gmail.com>
25 */
26
27 #ifdef HAVE_CONFIG_H
28 # include <config.h>
29 #endif
30
31 #include "etnaviv_priv.h"
32 #include "etnaviv_drmif.h"
33
34 drm_private void bo_del(struct etna_bo *bo);
35 drm_private extern pthread_mutex_t table_lock;
36
add_bucket(struct etna_bo_cache * cache,int size)37 static void add_bucket(struct etna_bo_cache *cache, int size)
38 {
39 unsigned i = cache->num_buckets;
40
41 assert(i < ARRAY_SIZE(cache->cache_bucket));
42
43 list_inithead(&cache->cache_bucket[i].list);
44 cache->cache_bucket[i].size = size;
45 cache->num_buckets++;
46 }
47
etna_bo_cache_init(struct etna_bo_cache * cache)48 drm_private void etna_bo_cache_init(struct etna_bo_cache *cache)
49 {
50 unsigned long size, cache_max_size = 64 * 1024 * 1024;
51
52 /* OK, so power of two buckets was too wasteful of memory.
53 * Give 3 other sizes between each power of two, to hopefully
54 * cover things accurately enough. (The alternative is
55 * probably to just go for exact matching of sizes, and assume
56 * that for things like composited window resize the tiled
57 * width/height alignment and rounding of sizes to pages will
58 * get us useful cache hit rates anyway)
59 */
60 add_bucket(cache, 4096);
61 add_bucket(cache, 4096 * 2);
62 add_bucket(cache, 4096 * 3);
63
64 /* Initialize the linked lists for BO reuse cache. */
65 for (size = 4 * 4096; size <= cache_max_size; size *= 2) {
66 add_bucket(cache, size);
67 add_bucket(cache, size + size * 1 / 4);
68 add_bucket(cache, size + size * 2 / 4);
69 add_bucket(cache, size + size * 3 / 4);
70 }
71 }
72
73 /* Frees older cached buffers. Called under table_lock */
etna_bo_cache_cleanup(struct etna_bo_cache * cache,time_t time)74 drm_private void etna_bo_cache_cleanup(struct etna_bo_cache *cache, time_t time)
75 {
76 unsigned i;
77
78 if (cache->time == time)
79 return;
80
81 for (i = 0; i < cache->num_buckets; i++) {
82 struct etna_bo_bucket *bucket = &cache->cache_bucket[i];
83 struct etna_bo *bo;
84
85 while (!LIST_IS_EMPTY(&bucket->list)) {
86 bo = LIST_ENTRY(struct etna_bo, bucket->list.next, list);
87
88 /* keep things in cache for at least 1 second: */
89 if (time && ((time - bo->free_time) <= 1))
90 break;
91
92 list_del(&bo->list);
93 bo_del(bo);
94 }
95 }
96
97 cache->time = time;
98 }
99
get_bucket(struct etna_bo_cache * cache,uint32_t size)100 static struct etna_bo_bucket *get_bucket(struct etna_bo_cache *cache, uint32_t size)
101 {
102 unsigned i;
103
104 /* hmm, this is what intel does, but I suppose we could calculate our
105 * way to the correct bucket size rather than looping..
106 */
107 for (i = 0; i < cache->num_buckets; i++) {
108 struct etna_bo_bucket *bucket = &cache->cache_bucket[i];
109 if (bucket->size >= size) {
110 return bucket;
111 }
112 }
113
114 return NULL;
115 }
116
is_idle(struct etna_bo * bo)117 static int is_idle(struct etna_bo *bo)
118 {
119 return etna_bo_cpu_prep(bo,
120 DRM_ETNA_PREP_READ |
121 DRM_ETNA_PREP_WRITE |
122 DRM_ETNA_PREP_NOSYNC) == 0;
123 }
124
find_in_bucket(struct etna_bo_bucket * bucket,uint32_t flags)125 static struct etna_bo *find_in_bucket(struct etna_bo_bucket *bucket, uint32_t flags)
126 {
127 struct etna_bo *bo = NULL;
128
129 pthread_mutex_lock(&table_lock);
130 while (!LIST_IS_EMPTY(&bucket->list)) {
131 bo = LIST_ENTRY(struct etna_bo, bucket->list.next, list);
132
133 if (bo->flags == flags && is_idle(bo)) {
134 list_del(&bo->list);
135 break;
136 }
137
138 bo = NULL;
139 break;
140 }
141 pthread_mutex_unlock(&table_lock);
142
143 return bo;
144 }
145
146 /* allocate a new (un-tiled) buffer object
147 *
148 * NOTE: size is potentially rounded up to bucket size
149 */
etna_bo_cache_alloc(struct etna_bo_cache * cache,uint32_t * size,uint32_t flags)150 drm_private struct etna_bo *etna_bo_cache_alloc(struct etna_bo_cache *cache, uint32_t *size,
151 uint32_t flags)
152 {
153 struct etna_bo *bo;
154 struct etna_bo_bucket *bucket;
155
156 *size = ALIGN(*size, 4096);
157 bucket = get_bucket(cache, *size);
158
159 /* see if we can be green and recycle: */
160 if (bucket) {
161 *size = bucket->size;
162 bo = find_in_bucket(bucket, flags);
163 if (bo) {
164 atomic_set(&bo->refcnt, 1);
165 etna_device_ref(bo->dev);
166 return bo;
167 }
168 }
169
170 return NULL;
171 }
172
etna_bo_cache_free(struct etna_bo_cache * cache,struct etna_bo * bo)173 drm_private int etna_bo_cache_free(struct etna_bo_cache *cache, struct etna_bo *bo)
174 {
175 struct etna_bo_bucket *bucket = get_bucket(cache, bo->size);
176
177 /* see if we can be green and recycle: */
178 if (bucket) {
179 struct timespec time;
180
181 clock_gettime(CLOCK_MONOTONIC, &time);
182
183 bo->free_time = time.tv_sec;
184 list_addtail(&bo->list, &bucket->list);
185 etna_bo_cache_cleanup(cache, time.tv_sec);
186
187 /* bo's in the bucket cache don't have a ref and
188 * don't hold a ref to the dev:
189 */
190 etna_device_del_locked(bo->dev);
191
192 return 0;
193 }
194
195 return -1;
196 }
197