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