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