freedreno: re-work fd_batch_reference() locking
[mesa.git] / src / gallium / drivers / freedreno / freedreno_batch.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/list.h"
28 #include "util/set.h"
29 #include "util/hash_table.h"
30 #include "util/u_string.h"
31
32 #include "freedreno_batch.h"
33 #include "freedreno_context.h"
34 #include "freedreno_fence.h"
35 #include "freedreno_resource.h"
36 #include "freedreno_query_hw.h"
37
38 static void
39 batch_init(struct fd_batch *batch)
40 {
41 struct fd_context *ctx = batch->ctx;
42 unsigned size = 0;
43
44 if (ctx->screen->reorder)
45 util_queue_fence_init(&batch->flush_fence);
46
47 /* if kernel is too old to support unlimited # of cmd buffers, we
48 * have no option but to allocate large worst-case sizes so that
49 * we don't need to grow the ringbuffer. Performance is likely to
50 * suffer, but there is no good alternative.
51 */
52 if ((fd_device_version(ctx->screen->dev) < FD_VERSION_UNLIMITED_CMDS) ||
53 (fd_mesa_debug & FD_DBG_NOGROW)){
54 size = 0x100000;
55 }
56
57 batch->draw = fd_ringbuffer_new(ctx->pipe, size);
58 if (!batch->nondraw) {
59 batch->binning = fd_ringbuffer_new(ctx->pipe, size);
60 batch->gmem = fd_ringbuffer_new(ctx->pipe, size);
61
62 fd_ringbuffer_set_parent(batch->gmem, NULL);
63 fd_ringbuffer_set_parent(batch->draw, batch->gmem);
64 fd_ringbuffer_set_parent(batch->binning, batch->gmem);
65 } else {
66 fd_ringbuffer_set_parent(batch->draw, NULL);
67 }
68
69 batch->in_fence_fd = -1;
70 batch->fence = fd_fence_create(batch);
71
72 batch->cleared = batch->partial_cleared = 0;
73 batch->restore = batch->resolve = 0;
74 batch->needs_flush = false;
75 batch->flushed = false;
76 batch->gmem_reason = 0;
77 batch->num_draws = 0;
78 batch->stage = FD_STAGE_NULL;
79
80 fd_reset_wfi(batch);
81
82 /* reset maximal bounds: */
83 batch->max_scissor.minx = batch->max_scissor.miny = ~0;
84 batch->max_scissor.maxx = batch->max_scissor.maxy = 0;
85
86 util_dynarray_init(&batch->draw_patches, NULL);
87
88 if (is_a3xx(ctx->screen))
89 util_dynarray_init(&batch->rbrc_patches, NULL);
90
91 assert(batch->resources->entries == 0);
92
93 util_dynarray_init(&batch->samples, NULL);
94 }
95
96 struct fd_batch *
97 fd_batch_create(struct fd_context *ctx, bool nondraw)
98 {
99 struct fd_batch *batch = CALLOC_STRUCT(fd_batch);
100
101 if (!batch)
102 return NULL;
103
104 DBG("%p", batch);
105
106 pipe_reference_init(&batch->reference, 1);
107 batch->ctx = ctx;
108 batch->nondraw = nondraw;
109
110 batch->resources = _mesa_set_create(NULL, _mesa_hash_pointer,
111 _mesa_key_pointer_equal);
112
113 batch_init(batch);
114
115 return batch;
116 }
117
118 static void
119 batch_fini(struct fd_batch *batch)
120 {
121 DBG("%p", batch);
122
123 pipe_resource_reference(&batch->query_buf, NULL);
124
125 if (batch->in_fence_fd != -1)
126 close(batch->in_fence_fd);
127
128 /* in case batch wasn't flushed but fence was created: */
129 fd_fence_populate(batch->fence, 0, -1);
130
131 fd_fence_ref(NULL, &batch->fence, NULL);
132
133 fd_ringbuffer_del(batch->draw);
134 if (!batch->nondraw) {
135 fd_ringbuffer_del(batch->binning);
136 fd_ringbuffer_del(batch->gmem);
137 } else {
138 debug_assert(!batch->binning);
139 debug_assert(!batch->gmem);
140 }
141 if (batch->lrz_clear) {
142 fd_ringbuffer_del(batch->lrz_clear);
143 batch->lrz_clear = NULL;
144 }
145
146 util_dynarray_fini(&batch->draw_patches);
147
148 if (is_a3xx(batch->ctx->screen))
149 util_dynarray_fini(&batch->rbrc_patches);
150
151 while (batch->samples.size > 0) {
152 struct fd_hw_sample *samp =
153 util_dynarray_pop(&batch->samples, struct fd_hw_sample *);
154 fd_hw_sample_reference(batch->ctx, &samp, NULL);
155 }
156 util_dynarray_fini(&batch->samples);
157
158 if (batch->ctx->screen->reorder)
159 util_queue_fence_destroy(&batch->flush_fence);
160 }
161
162 static void
163 batch_flush_reset_dependencies(struct fd_batch *batch, bool flush)
164 {
165 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
166 struct fd_batch *dep;
167
168 foreach_batch(dep, cache, batch->dependents_mask) {
169 if (flush)
170 fd_batch_flush(dep, false, false);
171 fd_batch_reference(&dep, NULL);
172 }
173
174 batch->dependents_mask = 0;
175 }
176
177 static void
178 batch_reset_resources_locked(struct fd_batch *batch)
179 {
180 struct set_entry *entry;
181
182 pipe_mutex_assert_locked(batch->ctx->screen->lock);
183
184 set_foreach(batch->resources, entry) {
185 struct fd_resource *rsc = (struct fd_resource *)entry->key;
186 _mesa_set_remove(batch->resources, entry);
187 debug_assert(rsc->batch_mask & (1 << batch->idx));
188 rsc->batch_mask &= ~(1 << batch->idx);
189 if (rsc->write_batch == batch)
190 fd_batch_reference_locked(&rsc->write_batch, NULL);
191 }
192 }
193
194 static void
195 batch_reset_resources(struct fd_batch *batch)
196 {
197 mtx_lock(&batch->ctx->screen->lock);
198 batch_reset_resources_locked(batch);
199 mtx_unlock(&batch->ctx->screen->lock);
200 }
201
202 static void
203 batch_reset(struct fd_batch *batch)
204 {
205 DBG("%p", batch);
206
207 fd_batch_sync(batch);
208
209 batch_flush_reset_dependencies(batch, false);
210 batch_reset_resources(batch);
211
212 batch_fini(batch);
213 batch_init(batch);
214 }
215
216 void
217 fd_batch_reset(struct fd_batch *batch)
218 {
219 if (batch->needs_flush)
220 batch_reset(batch);
221 }
222
223 void
224 __fd_batch_destroy(struct fd_batch *batch)
225 {
226 struct fd_context *ctx = batch->ctx;
227
228 DBG("%p", batch);
229
230 fd_context_assert_locked(batch->ctx);
231
232 fd_bc_invalidate_batch(batch, true);
233
234 batch_reset_resources_locked(batch);
235 debug_assert(batch->resources->entries == 0);
236 _mesa_set_destroy(batch->resources, NULL);
237
238 batch_flush_reset_dependencies(batch, false);
239 debug_assert(batch->dependents_mask == 0);
240
241 fd_context_unlock(ctx);
242 util_copy_framebuffer_state(&batch->framebuffer, NULL);
243 batch_fini(batch);
244 free(batch);
245 fd_context_lock(ctx);
246 }
247
248 void
249 __fd_batch_describe(char* buf, const struct fd_batch *batch)
250 {
251 util_sprintf(buf, "fd_batch<%u>", batch->seqno);
252 }
253
254 void
255 fd_batch_sync(struct fd_batch *batch)
256 {
257 if (!batch->ctx->screen->reorder)
258 return;
259 util_queue_fence_wait(&batch->flush_fence);
260 }
261
262 static void
263 batch_flush_func(void *job, int id)
264 {
265 struct fd_batch *batch = job;
266
267 DBG("%p", batch);
268
269 fd_gmem_render_tiles(batch);
270 batch_reset_resources(batch);
271 }
272
273 static void
274 batch_cleanup_func(void *job, int id)
275 {
276 struct fd_batch *batch = job;
277 fd_batch_reference(&batch, NULL);
278 }
279
280 static void
281 batch_flush(struct fd_batch *batch, bool force)
282 {
283 DBG("%p: needs_flush=%d", batch, batch->needs_flush);
284
285 if (batch->flushed)
286 return;
287
288 batch->needs_flush = false;
289
290 /* close out the draw cmds by making sure any active queries are
291 * paused:
292 */
293 fd_batch_set_stage(batch, FD_STAGE_NULL);
294
295 fd_context_all_dirty(batch->ctx);
296 batch_flush_reset_dependencies(batch, true);
297
298 batch->flushed = true;
299
300 if (batch->ctx->screen->reorder) {
301 struct fd_batch *tmp = NULL;
302 fd_batch_reference(&tmp, batch);
303
304 if (!util_queue_is_initialized(&batch->ctx->flush_queue))
305 util_queue_init(&batch->ctx->flush_queue, "flush_queue", 16, 1, 0);
306
307 util_queue_add_job(&batch->ctx->flush_queue,
308 batch, &batch->flush_fence,
309 batch_flush_func, batch_cleanup_func);
310 } else {
311 fd_gmem_render_tiles(batch);
312 batch_reset_resources(batch);
313 }
314
315 debug_assert(batch->reference.count > 0);
316
317 mtx_lock(&batch->ctx->screen->lock);
318 fd_bc_invalidate_batch(batch, false);
319 mtx_unlock(&batch->ctx->screen->lock);
320 }
321
322 /* NOTE: could drop the last ref to batch
323 *
324 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
325 * to kernel before this returns, as opposed to just being queued to be
326 * flushed
327 * @force: force a flush even if no rendering, mostly useful if you need
328 * a fence to sync on
329 */
330 void
331 fd_batch_flush(struct fd_batch *batch, bool sync, bool force)
332 {
333 struct fd_batch *tmp = NULL;
334 bool newbatch = false;
335
336 /* NOTE: we need to hold an extra ref across the body of flush,
337 * since the last ref to this batch could be dropped when cleaning
338 * up used_resources
339 */
340 fd_batch_reference(&tmp, batch);
341
342 if (batch == batch->ctx->batch) {
343 batch->ctx->batch = NULL;
344 newbatch = true;
345 }
346
347 batch_flush(tmp, force);
348
349 if (newbatch) {
350 struct fd_context *ctx = batch->ctx;
351 struct fd_batch *new_batch =
352 fd_batch_from_fb(&ctx->screen->batch_cache, ctx, &batch->framebuffer);
353
354 util_copy_framebuffer_state(&new_batch->framebuffer, &batch->framebuffer);
355
356 fd_batch_reference(&batch, NULL);
357 ctx->batch = new_batch;
358 }
359
360 if (sync)
361 fd_batch_sync(tmp);
362
363 fd_batch_reference(&tmp, NULL);
364 }
365
366 /* does 'batch' depend directly or indirectly on 'other' ? */
367 static bool
368 batch_depends_on(struct fd_batch *batch, struct fd_batch *other)
369 {
370 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
371 struct fd_batch *dep;
372
373 if (batch->dependents_mask & (1 << other->idx))
374 return true;
375
376 foreach_batch(dep, cache, batch->dependents_mask)
377 if (batch_depends_on(batch, dep))
378 return true;
379
380 return false;
381 }
382
383 void
384 fd_batch_add_dep(struct fd_batch *batch, struct fd_batch *dep)
385 {
386 if (batch->dependents_mask & (1 << dep->idx))
387 return;
388
389 /* a loop should not be possible */
390 debug_assert(!batch_depends_on(dep, batch));
391
392 struct fd_batch *other = NULL;
393 fd_batch_reference_locked(&other, dep);
394 batch->dependents_mask |= (1 << dep->idx);
395 DBG("%p: added dependency on %p", batch, dep);
396 }
397
398 static void
399 flush_write_batch(struct fd_resource *rsc)
400 {
401 struct fd_batch *b = NULL;
402 fd_batch_reference(&b, rsc->write_batch);
403
404 mtx_unlock(&b->ctx->screen->lock);
405 fd_batch_flush(b, true, false);
406 mtx_lock(&b->ctx->screen->lock);
407
408 fd_bc_invalidate_batch(b, false);
409 fd_batch_reference_locked(&b, NULL);
410 }
411
412 void
413 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool write)
414 {
415 pipe_mutex_assert_locked(batch->ctx->screen->lock);
416
417 if (rsc->stencil)
418 fd_batch_resource_used(batch, rsc->stencil, write);
419
420 DBG("%p: %s %p", batch, write ? "write" : "read", rsc);
421
422 if (write)
423 rsc->valid = true;
424
425 /* note, invalidate write batch, to avoid further writes to rsc
426 * resulting in a write-after-read hazard.
427 */
428
429 if (write) {
430 /* if we are pending read or write by any other batch: */
431 if (rsc->batch_mask & ~(1 << batch->idx)) {
432 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
433 struct fd_batch *dep;
434
435 if (rsc->write_batch && rsc->write_batch != batch)
436 flush_write_batch(rsc);
437
438 foreach_batch(dep, cache, rsc->batch_mask) {
439 struct fd_batch *b = NULL;
440 if (dep == batch)
441 continue;
442 /* note that batch_add_dep could flush and unref dep, so
443 * we need to hold a reference to keep it live for the
444 * fd_bc_invalidate_batch()
445 */
446 fd_batch_reference(&b, dep);
447 fd_batch_add_dep(batch, b);
448 fd_bc_invalidate_batch(b, false);
449 fd_batch_reference_locked(&b, NULL);
450 }
451 }
452 fd_batch_reference_locked(&rsc->write_batch, batch);
453 } else {
454 /* If reading a resource pending a write, go ahead and flush the
455 * writer. This avoids situations where we end up having to
456 * flush the current batch in _resource_used()
457 */
458 if (rsc->write_batch && rsc->write_batch != batch)
459 flush_write_batch(rsc);
460 }
461
462 if (rsc->batch_mask & (1 << batch->idx))
463 return;
464
465 debug_assert(!_mesa_set_search(batch->resources, rsc));
466
467 _mesa_set_add(batch->resources, rsc);
468 rsc->batch_mask |= (1 << batch->idx);
469 }
470
471 void
472 fd_batch_check_size(struct fd_batch *batch)
473 {
474 debug_assert(!batch->flushed);
475
476 if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
477 return;
478
479 struct fd_ringbuffer *ring = batch->draw;
480 if (((ring->cur - ring->start) > (ring->size/4 - 0x1000)) ||
481 (fd_mesa_debug & FD_DBG_FLUSH))
482 fd_batch_flush(batch, true, false);
483 }
484
485 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
486 * been one since last draw:
487 */
488 void
489 fd_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring)
490 {
491 if (batch->needs_wfi) {
492 if (batch->ctx->screen->gpu_id >= 500)
493 OUT_WFI5(ring);
494 else
495 OUT_WFI(ring);
496 batch->needs_wfi = false;
497 }
498 }