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