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