freedreno: make fd_batch a one-shot thing
[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 DBG("%p", batch);
227
228 util_copy_framebuffer_state(&batch->framebuffer, NULL);
229
230 mtx_lock(&batch->ctx->screen->lock);
231 fd_bc_invalidate_batch(batch, true);
232 mtx_unlock(&batch->ctx->screen->lock);
233
234 batch_fini(batch);
235
236 batch_reset_resources(batch);
237 debug_assert(batch->resources->entries == 0);
238 _mesa_set_destroy(batch->resources, NULL);
239
240 batch_flush_reset_dependencies(batch, false);
241 debug_assert(batch->dependents_mask == 0);
242
243 free(batch);
244 }
245
246 void
247 __fd_batch_describe(char* buf, const struct fd_batch *batch)
248 {
249 util_sprintf(buf, "fd_batch<%u>", batch->seqno);
250 }
251
252 void
253 fd_batch_sync(struct fd_batch *batch)
254 {
255 if (!batch->ctx->screen->reorder)
256 return;
257 util_queue_fence_wait(&batch->flush_fence);
258 }
259
260 static void
261 batch_flush_func(void *job, int id)
262 {
263 struct fd_batch *batch = job;
264
265 DBG("%p", batch);
266
267 fd_gmem_render_tiles(batch);
268 batch_reset_resources(batch);
269 }
270
271 static void
272 batch_cleanup_func(void *job, int id)
273 {
274 struct fd_batch *batch = job;
275 fd_batch_reference(&batch, NULL);
276 }
277
278 static void
279 batch_flush(struct fd_batch *batch, bool force)
280 {
281 DBG("%p: needs_flush=%d", batch, batch->needs_flush);
282
283 if (batch->flushed)
284 return;
285
286 batch->needs_flush = false;
287
288 /* close out the draw cmds by making sure any active queries are
289 * paused:
290 */
291 fd_batch_set_stage(batch, FD_STAGE_NULL);
292
293 fd_context_all_dirty(batch->ctx);
294 batch_flush_reset_dependencies(batch, true);
295
296 batch->flushed = true;
297
298 if (batch->ctx->screen->reorder) {
299 struct fd_batch *tmp = NULL;
300 fd_batch_reference(&tmp, batch);
301
302 if (!util_queue_is_initialized(&batch->ctx->flush_queue))
303 util_queue_init(&batch->ctx->flush_queue, "flush_queue", 16, 1, 0);
304
305 util_queue_add_job(&batch->ctx->flush_queue,
306 batch, &batch->flush_fence,
307 batch_flush_func, batch_cleanup_func);
308 } else {
309 fd_gmem_render_tiles(batch);
310 batch_reset_resources(batch);
311 }
312
313 debug_assert(batch->reference.count > 0);
314
315 mtx_lock(&batch->ctx->screen->lock);
316 fd_bc_invalidate_batch(batch, false);
317 mtx_unlock(&batch->ctx->screen->lock);
318 }
319
320 /* NOTE: could drop the last ref to batch
321 *
322 * @sync: synchronize with flush_queue, ensures batch is *actually* flushed
323 * to kernel before this returns, as opposed to just being queued to be
324 * flushed
325 * @force: force a flush even if no rendering, mostly useful if you need
326 * a fence to sync on
327 */
328 void
329 fd_batch_flush(struct fd_batch *batch, bool sync, bool force)
330 {
331 struct fd_batch *tmp = NULL;
332 bool newbatch = false;
333
334 /* NOTE: we need to hold an extra ref across the body of flush,
335 * since the last ref to this batch could be dropped when cleaning
336 * up used_resources
337 */
338 fd_batch_reference(&tmp, batch);
339
340 if (batch == batch->ctx->batch) {
341 batch->ctx->batch = NULL;
342 newbatch = true;
343 }
344
345 batch_flush(tmp, force);
346
347 if (newbatch) {
348 struct fd_context *ctx = batch->ctx;
349 struct fd_batch *new_batch =
350 fd_batch_from_fb(&ctx->screen->batch_cache, ctx, &batch->framebuffer);
351
352 util_copy_framebuffer_state(&new_batch->framebuffer, &batch->framebuffer);
353
354 fd_batch_reference(&batch, NULL);
355 ctx->batch = new_batch;
356 }
357
358 if (sync)
359 fd_batch_sync(tmp);
360
361 fd_batch_reference(&tmp, NULL);
362 }
363
364 /* does 'batch' depend directly or indirectly on 'other' ? */
365 static bool
366 batch_depends_on(struct fd_batch *batch, struct fd_batch *other)
367 {
368 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
369 struct fd_batch *dep;
370
371 if (batch->dependents_mask & (1 << other->idx))
372 return true;
373
374 foreach_batch(dep, cache, batch->dependents_mask)
375 if (batch_depends_on(batch, dep))
376 return true;
377
378 return false;
379 }
380
381 void
382 fd_batch_add_dep(struct fd_batch *batch, struct fd_batch *dep)
383 {
384 if (batch->dependents_mask & (1 << dep->idx))
385 return;
386
387 /* a loop should not be possible */
388 debug_assert(!batch_depends_on(dep, batch));
389
390 struct fd_batch *other = NULL;
391 fd_batch_reference_locked(&other, dep);
392 batch->dependents_mask |= (1 << dep->idx);
393 DBG("%p: added dependency on %p", batch, dep);
394 }
395
396 static void
397 flush_write_batch(struct fd_resource *rsc)
398 {
399 struct fd_batch *b = NULL;
400 fd_batch_reference(&b, rsc->write_batch);
401
402 mtx_unlock(&b->ctx->screen->lock);
403 fd_batch_flush(b, true, false);
404 mtx_lock(&b->ctx->screen->lock);
405
406 fd_bc_invalidate_batch(b, false);
407 fd_batch_reference_locked(&b, NULL);
408 }
409
410 void
411 fd_batch_resource_used(struct fd_batch *batch, struct fd_resource *rsc, bool write)
412 {
413 pipe_mutex_assert_locked(batch->ctx->screen->lock);
414
415 if (rsc->stencil)
416 fd_batch_resource_used(batch, rsc->stencil, write);
417
418 DBG("%p: %s %p", batch, write ? "write" : "read", rsc);
419
420 if (write)
421 rsc->valid = true;
422
423 /* note, invalidate write batch, to avoid further writes to rsc
424 * resulting in a write-after-read hazard.
425 */
426
427 if (write) {
428 /* if we are pending read or write by any other batch: */
429 if (rsc->batch_mask & ~(1 << batch->idx)) {
430 struct fd_batch_cache *cache = &batch->ctx->screen->batch_cache;
431 struct fd_batch *dep;
432
433 if (rsc->write_batch && rsc->write_batch != batch)
434 flush_write_batch(rsc);
435
436 foreach_batch(dep, cache, rsc->batch_mask) {
437 struct fd_batch *b = NULL;
438 if (dep == batch)
439 continue;
440 /* note that batch_add_dep could flush and unref dep, so
441 * we need to hold a reference to keep it live for the
442 * fd_bc_invalidate_batch()
443 */
444 fd_batch_reference(&b, dep);
445 fd_batch_add_dep(batch, b);
446 fd_bc_invalidate_batch(b, false);
447 fd_batch_reference_locked(&b, NULL);
448 }
449 }
450 fd_batch_reference_locked(&rsc->write_batch, batch);
451 } else {
452 /* If reading a resource pending a write, go ahead and flush the
453 * writer. This avoids situations where we end up having to
454 * flush the current batch in _resource_used()
455 */
456 if (rsc->write_batch && rsc->write_batch != batch)
457 flush_write_batch(rsc);
458 }
459
460 if (rsc->batch_mask & (1 << batch->idx))
461 return;
462
463 debug_assert(!_mesa_set_search(batch->resources, rsc));
464
465 _mesa_set_add(batch->resources, rsc);
466 rsc->batch_mask |= (1 << batch->idx);
467 }
468
469 void
470 fd_batch_check_size(struct fd_batch *batch)
471 {
472 debug_assert(!batch->flushed);
473
474 if (fd_device_version(batch->ctx->screen->dev) >= FD_VERSION_UNLIMITED_CMDS)
475 return;
476
477 struct fd_ringbuffer *ring = batch->draw;
478 if (((ring->cur - ring->start) > (ring->size/4 - 0x1000)) ||
479 (fd_mesa_debug & FD_DBG_FLUSH))
480 fd_batch_flush(batch, true, false);
481 }
482
483 /* emit a WAIT_FOR_IDLE only if needed, ie. if there has not already
484 * been one since last draw:
485 */
486 void
487 fd_wfi(struct fd_batch *batch, struct fd_ringbuffer *ring)
488 {
489 if (batch->needs_wfi) {
490 if (batch->ctx->screen->gpu_id >= 500)
491 OUT_WFI5(ring);
492 else
493 OUT_WFI(ring);
494 batch->needs_wfi = false;
495 }
496 }