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