freedreno: add screen lock wrappers
[mesa.git] / src / gallium / drivers / freedreno / freedreno_batch_cache.c
1 /*
2 * Copyright (C) 2016 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 "util/hash_table.h"
28 #include "util/set.h"
29 #include "util/list.h"
30 #include "util/u_string.h"
31
32 #include "freedreno_batch.h"
33 #include "freedreno_batch_cache.h"
34 #include "freedreno_context.h"
35 #include "freedreno_resource.h"
36
37 /* Overview:
38 *
39 * The batch cache provides lookup for mapping pipe_framebuffer_state
40 * to a batch.
41 *
42 * It does this via hashtable, with key that roughly matches the
43 * pipe_framebuffer_state, as described below.
44 *
45 * Batch Cache hashtable key:
46 *
47 * To serialize the key, and to avoid dealing with holding a reference to
48 * pipe_surface's (which hold a reference to pipe_resource and complicate
49 * the whole refcnting thing), the key is variable length and inline's the
50 * pertinent details of the pipe_surface.
51 *
52 * Batch:
53 *
54 * Each batch needs to hold a reference to each resource it depends on (ie.
55 * anything that needs a mem2gmem). And a weak reference to resources it
56 * renders to. (If both src[n] and dst[n] are not NULL then they are the
57 * same.)
58 *
59 * When a resource is destroyed, we need to remove entries in the batch
60 * cache that reference the resource, to avoid dangling pointer issues.
61 * So each resource holds a hashset of batches which have reference them
62 * in their hashtable key.
63 *
64 * When a batch has weak reference to no more resources (ie. all the
65 * surfaces it rendered to are destroyed) the batch can be destroyed.
66 * Could happen in an app that renders and never uses the result. More
67 * common scenario, I think, will be that some, but not all, of the
68 * surfaces are destroyed before the batch is submitted.
69 *
70 * If (for example), batch writes to zsbuf but that surface is destroyed
71 * before batch is submitted, we can skip gmem2mem (but still need to
72 * alloc gmem space as before. If the batch depended on previous contents
73 * of that surface, it would be holding a reference so the surface would
74 * not have been destroyed.
75 */
76
77 struct key {
78 uint32_t width, height, layers;
79 uint16_t samples, num_surfs;
80 struct fd_context *ctx;
81 struct {
82 struct pipe_resource *texture;
83 union pipe_surface_desc u;
84 uint8_t pos, samples;
85 uint16_t format;
86 } surf[0];
87 };
88
89 static struct key *
90 key_alloc(unsigned num_surfs)
91 {
92 struct key *key =
93 CALLOC_VARIANT_LENGTH_STRUCT(key, sizeof(key->surf[0]) * num_surfs);
94 return key;
95 }
96
97 static uint32_t
98 key_hash(const void *_key)
99 {
100 const struct key *key = _key;
101 uint32_t hash = _mesa_fnv32_1a_offset_bias;
102 hash = _mesa_fnv32_1a_accumulate_block(hash, key, offsetof(struct key, surf[0]));
103 hash = _mesa_fnv32_1a_accumulate_block(hash, key->surf, sizeof(key->surf[0]) * key->num_surfs);
104 return hash;
105 }
106
107 static bool
108 key_equals(const void *_a, const void *_b)
109 {
110 const struct key *a = _a;
111 const struct key *b = _b;
112 return (memcmp(a, b, offsetof(struct key, surf[0])) == 0) &&
113 (memcmp(a->surf, b->surf, sizeof(a->surf[0]) * a->num_surfs) == 0);
114 }
115
116 void
117 fd_bc_init(struct fd_batch_cache *cache)
118 {
119 cache->ht = _mesa_hash_table_create(NULL, key_hash, key_equals);
120 }
121
122 void
123 fd_bc_fini(struct fd_batch_cache *cache)
124 {
125 _mesa_hash_table_destroy(cache->ht, NULL);
126 }
127
128 static void
129 bc_flush(struct fd_batch_cache *cache, struct fd_context *ctx, bool deferred)
130 {
131 /* fd_batch_flush() (and fd_batch_add_dep() which calls it indirectly)
132 * can cause batches to be unref'd and freed under our feet, so grab
133 * a reference to all the batches we need up-front.
134 */
135 struct fd_batch *batches[ARRAY_SIZE(cache->batches)] = {0};
136 struct fd_batch *batch;
137 unsigned n = 0;
138
139 fd_context_lock(ctx);
140
141 foreach_batch(batch, cache, cache->batch_mask) {
142 if (batch->ctx == ctx) {
143 fd_batch_reference_locked(&batches[n++], batch);
144 }
145 }
146
147 if (deferred) {
148 struct fd_batch *current_batch = fd_context_batch(ctx);
149
150 for (unsigned i = 0; i < n; i++) {
151 if (batches[i] && (batches[i]->ctx == ctx) &&
152 (batches[i] != current_batch)) {
153 fd_batch_add_dep(current_batch, batches[i]);
154 }
155 }
156
157 fd_context_unlock(ctx);
158 } else {
159 fd_context_unlock(ctx);
160
161 for (unsigned i = 0; i < n; i++) {
162 fd_batch_flush(batches[i]);
163 }
164 }
165
166 for (unsigned i = 0; i < n; i++) {
167 fd_batch_reference(&batches[i], NULL);
168 }
169 }
170
171 void
172 fd_bc_flush(struct fd_batch_cache *cache, struct fd_context *ctx)
173 {
174 bc_flush(cache, ctx, false);
175 }
176
177 /* deferred flush doesn't actually flush, but it marks every other
178 * batch associated with the context as dependent on the current
179 * batch. So when the current batch gets flushed, all other batches
180 * that came before also get flushed.
181 */
182 void
183 fd_bc_flush_deferred(struct fd_batch_cache *cache, struct fd_context *ctx)
184 {
185 bc_flush(cache, ctx, true);
186 }
187
188 void
189 fd_bc_invalidate_context(struct fd_context *ctx)
190 {
191 struct fd_batch_cache *cache = &ctx->screen->batch_cache;
192 struct fd_batch *batch;
193
194 fd_screen_lock(ctx->screen);
195
196 foreach_batch(batch, cache, cache->batch_mask) {
197 if (batch->ctx == ctx)
198 fd_bc_invalidate_batch(batch, true);
199 }
200
201 fd_screen_unlock(ctx->screen);
202 }
203
204 /**
205 * Note that when batch is flushed, it needs to remain in the cache so
206 * that fd_bc_invalidate_resource() can work.. otherwise we can have
207 * the case where a rsc is destroyed while a batch still has a dangling
208 * reference to it.
209 *
210 * Note that the cmdstream (or, after the SUBMIT ioctl, the kernel)
211 * would have a reference to the underlying bo, so it is ok for the
212 * rsc to be destroyed before the batch.
213 */
214 void
215 fd_bc_invalidate_batch(struct fd_batch *batch, bool remove)
216 {
217 if (!batch)
218 return;
219
220 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
221 struct key *key = (struct key *)batch->key;
222
223 fd_context_assert_locked(batch->ctx);
224
225 if (remove) {
226 cache->batches[batch->idx] = NULL;
227 cache->batch_mask &= ~(1 << batch->idx);
228 }
229
230 if (!key)
231 return;
232
233 DBG("%p: key=%p", batch, batch->key);
234 for (unsigned idx = 0; idx < key->num_surfs; idx++) {
235 struct fd_resource *rsc = fd_resource(key->surf[idx].texture);
236 rsc->bc_batch_mask &= ~(1 << batch->idx);
237 }
238
239 struct hash_entry *entry =
240 _mesa_hash_table_search_pre_hashed(cache->ht, batch->hash, key);
241 _mesa_hash_table_remove(cache->ht, entry);
242
243 batch->key = NULL;
244 free(key);
245 }
246
247 void
248 fd_bc_invalidate_resource(struct fd_resource *rsc, bool destroy)
249 {
250 struct fd_screen *screen = fd_screen(rsc->base.screen);
251 struct fd_batch *batch;
252
253 fd_screen_lock(screen);
254
255 if (destroy) {
256 foreach_batch(batch, &screen->batch_cache, rsc->batch_mask) {
257 struct set_entry *entry = _mesa_set_search(batch->resources, rsc);
258 _mesa_set_remove(batch->resources, entry);
259 }
260 rsc->batch_mask = 0;
261
262 fd_batch_reference_locked(&rsc->write_batch, NULL);
263 }
264
265 foreach_batch(batch, &screen->batch_cache, rsc->bc_batch_mask)
266 fd_bc_invalidate_batch(batch, false);
267
268 rsc->bc_batch_mask = 0;
269
270 fd_screen_unlock(screen);
271 }
272
273 struct fd_batch *
274 fd_bc_alloc_batch(struct fd_batch_cache *cache, struct fd_context *ctx, bool nondraw)
275 {
276 struct fd_batch *batch;
277 uint32_t idx;
278
279 fd_screen_lock(ctx->screen);
280
281 while ((idx = ffs(~cache->batch_mask)) == 0) {
282 #if 0
283 for (unsigned i = 0; i < ARRAY_SIZE(cache->batches); i++) {
284 batch = cache->batches[i];
285 debug_printf("%d: needs_flush=%d, depends:", batch->idx, batch->needs_flush);
286 set_foreach(batch->dependencies, entry) {
287 struct fd_batch *dep = (struct fd_batch *)entry->key;
288 debug_printf(" %d", dep->idx);
289 }
290 debug_printf("\n");
291 }
292 #endif
293 /* TODO: is LRU the better policy? Or perhaps the batch that
294 * depends on the fewest other batches?
295 */
296 struct fd_batch *flush_batch = NULL;
297 for (unsigned i = 0; i < ARRAY_SIZE(cache->batches); i++) {
298 if (!flush_batch || (cache->batches[i]->seqno < flush_batch->seqno))
299 fd_batch_reference_locked(&flush_batch, cache->batches[i]);
300 }
301
302 /* we can drop lock temporarily here, since we hold a ref,
303 * flush_batch won't disappear under us.
304 */
305 fd_screen_unlock(ctx->screen);
306 DBG("%p: too many batches! flush forced!", flush_batch);
307 fd_batch_flush(flush_batch);
308 fd_screen_lock(ctx->screen);
309
310 /* While the resources get cleaned up automatically, the flush_batch
311 * doesn't get removed from the dependencies of other batches, so
312 * it won't be unref'd and will remain in the table.
313 *
314 * TODO maybe keep a bitmask of batches that depend on me, to make
315 * this easier:
316 */
317 for (unsigned i = 0; i < ARRAY_SIZE(cache->batches); i++) {
318 struct fd_batch *other = cache->batches[i];
319 if (!other)
320 continue;
321 if (other->dependents_mask & (1 << flush_batch->idx)) {
322 other->dependents_mask &= ~(1 << flush_batch->idx);
323 struct fd_batch *ref = flush_batch;
324 fd_batch_reference_locked(&ref, NULL);
325 }
326 }
327
328 fd_batch_reference_locked(&flush_batch, NULL);
329 }
330
331 idx--; /* bit zero returns 1 for ffs() */
332
333 batch = fd_batch_create(ctx, nondraw);
334 if (!batch)
335 goto out;
336
337 batch->seqno = cache->cnt++;
338 batch->idx = idx;
339 cache->batch_mask |= (1 << idx);
340
341 debug_assert(cache->batches[idx] == NULL);
342 cache->batches[idx] = batch;
343
344 out:
345 fd_screen_unlock(ctx->screen);
346
347 return batch;
348 }
349
350 static struct fd_batch *
351 batch_from_key(struct fd_batch_cache *cache, struct key *key,
352 struct fd_context *ctx)
353 {
354 struct fd_batch *batch = NULL;
355 uint32_t hash = key_hash(key);
356 struct hash_entry *entry =
357 _mesa_hash_table_search_pre_hashed(cache->ht, hash, key);
358
359 if (entry) {
360 free(key);
361 fd_batch_reference(&batch, (struct fd_batch *)entry->data);
362 return batch;
363 }
364
365 batch = fd_bc_alloc_batch(cache, ctx, false);
366 #ifdef DEBUG
367 DBG("%p: hash=0x%08x, %ux%u, %u layers, %u samples", batch, hash,
368 key->width, key->height, key->layers, key->samples);
369 for (unsigned idx = 0; idx < key->num_surfs; idx++) {
370 DBG("%p: surf[%u]: %p (%s) (%u,%u / %u,%u,%u)", batch, key->surf[idx].pos,
371 key->surf[idx].texture, util_format_name(key->surf[idx].format),
372 key->surf[idx].u.buf.first_element, key->surf[idx].u.buf.last_element,
373 key->surf[idx].u.tex.first_layer, key->surf[idx].u.tex.last_layer,
374 key->surf[idx].u.tex.level);
375 }
376 #endif
377 if (!batch)
378 return NULL;
379
380 fd_screen_lock(ctx->screen);
381
382 _mesa_hash_table_insert_pre_hashed(cache->ht, hash, key, batch);
383 batch->key = key;
384 batch->hash = hash;
385
386 for (unsigned idx = 0; idx < key->num_surfs; idx++) {
387 struct fd_resource *rsc = fd_resource(key->surf[idx].texture);
388 rsc->bc_batch_mask = (1 << batch->idx);
389 }
390
391 fd_screen_unlock(ctx->screen);
392
393 return batch;
394 }
395
396 static void
397 key_surf(struct key *key, unsigned idx, unsigned pos, struct pipe_surface *psurf)
398 {
399 key->surf[idx].texture = psurf->texture;
400 key->surf[idx].u = psurf->u;
401 key->surf[idx].pos = pos;
402 key->surf[idx].samples = MAX2(1, psurf->nr_samples);
403 key->surf[idx].format = psurf->format;
404 }
405
406 struct fd_batch *
407 fd_batch_from_fb(struct fd_batch_cache *cache, struct fd_context *ctx,
408 const struct pipe_framebuffer_state *pfb)
409 {
410 unsigned idx = 0, n = pfb->nr_cbufs + (pfb->zsbuf ? 1 : 0);
411 struct key *key = key_alloc(n);
412
413 key->width = pfb->width;
414 key->height = pfb->height;
415 key->layers = pfb->layers;
416 key->samples = util_framebuffer_get_num_samples(pfb);
417 key->ctx = ctx;
418
419 if (pfb->zsbuf)
420 key_surf(key, idx++, 0, pfb->zsbuf);
421
422 for (unsigned i = 0; i < pfb->nr_cbufs; i++)
423 if (pfb->cbufs[i])
424 key_surf(key, idx++, i + 1, pfb->cbufs[i]);
425
426 key->num_surfs = idx;
427
428 return batch_from_key(cache, key, ctx);
429 }