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