1 /*
2 * Copyright 2013-2017 Advanced Micro Devices, Inc.
3 * All Rights Reserved.
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice (including the next
13 * paragraph) shall be included in all copies or substantial portions of the
14 * Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 *
24 */
25
26 #include "si_build_pm4.h"
27 #include "util/os_time.h"
28 #include "util/u_memory.h"
29 #include "util/u_queue.h"
30 #include "util/u_upload_mgr.h"
31
32 #include <libsync.h>
33
34 struct si_fine_fence {
35 struct si_resource *buf;
36 unsigned offset;
37 };
38
39 struct si_multi_fence {
40 struct pipe_reference reference;
41 struct pipe_fence_handle *gfx;
42 struct pipe_fence_handle *sdma;
43 struct tc_unflushed_batch_token *tc_token;
44 struct util_queue_fence ready;
45
46 /* If the context wasn't flushed at fence creation, this is non-NULL. */
47 struct {
48 struct si_context *ctx;
49 unsigned ib_index;
50 } gfx_unflushed;
51
52 struct si_fine_fence fine;
53 };
54
55 /**
56 * Write an EOP event.
57 *
58 * \param event EVENT_TYPE_*
59 * \param event_flags Optional cache flush flags (TC)
60 * \param dst_sel MEM or TC_L2
61 * \param int_sel NONE or SEND_DATA_AFTER_WR_CONFIRM
62 * \param data_sel DISCARD, VALUE_32BIT, TIMESTAMP, or GDS
63 * \param buf Buffer
64 * \param va GPU address
65 * \param old_value Previous fence value (for a bug workaround)
66 * \param new_value Fence value to write for this event.
67 */
si_cp_release_mem(struct si_context * ctx,struct radeon_cmdbuf * cs,unsigned event,unsigned event_flags,unsigned dst_sel,unsigned int_sel,unsigned data_sel,struct si_resource * buf,uint64_t va,uint32_t new_fence,unsigned query_type)68 void si_cp_release_mem(struct si_context *ctx, struct radeon_cmdbuf *cs, unsigned event,
69 unsigned event_flags, unsigned dst_sel, unsigned int_sel, unsigned data_sel,
70 struct si_resource *buf, uint64_t va, uint32_t new_fence,
71 unsigned query_type)
72 {
73 unsigned op = EVENT_TYPE(event) |
74 EVENT_INDEX(event == V_028A90_CS_DONE || event == V_028A90_PS_DONE ? 6 : 5) |
75 event_flags;
76 unsigned sel = EOP_DST_SEL(dst_sel) | EOP_INT_SEL(int_sel) | EOP_DATA_SEL(data_sel);
77 bool compute_ib = !ctx->has_graphics || cs == ctx->prim_discard_compute_cs;
78
79 if (ctx->chip_class >= GFX9 || (compute_ib && ctx->chip_class >= GFX7)) {
80 /* A ZPASS_DONE or PIXEL_STAT_DUMP_EVENT (of the DB occlusion
81 * counters) must immediately precede every timestamp event to
82 * prevent a GPU hang on GFX9.
83 *
84 * Occlusion queries don't need to do it here, because they
85 * always do ZPASS_DONE before the timestamp.
86 */
87 if (ctx->chip_class == GFX9 && !compute_ib && query_type != PIPE_QUERY_OCCLUSION_COUNTER &&
88 query_type != PIPE_QUERY_OCCLUSION_PREDICATE &&
89 query_type != PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
90 struct si_resource *scratch = unlikely(ctx->ws->cs_is_secure(ctx->gfx_cs)) ?
91 ctx->eop_bug_scratch_tmz : ctx->eop_bug_scratch;
92
93 assert(16 * ctx->screen->info.num_render_backends <= scratch->b.b.width0);
94 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
95 radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1));
96 radeon_emit(cs, scratch->gpu_address);
97 radeon_emit(cs, scratch->gpu_address >> 32);
98
99 radeon_add_to_buffer_list(ctx, ctx->gfx_cs, scratch, RADEON_USAGE_WRITE,
100 RADEON_PRIO_QUERY);
101 }
102
103 radeon_emit(cs, PKT3(PKT3_RELEASE_MEM, ctx->chip_class >= GFX9 ? 6 : 5, 0));
104 radeon_emit(cs, op);
105 radeon_emit(cs, sel);
106 radeon_emit(cs, va); /* address lo */
107 radeon_emit(cs, va >> 32); /* address hi */
108 radeon_emit(cs, new_fence); /* immediate data lo */
109 radeon_emit(cs, 0); /* immediate data hi */
110 if (ctx->chip_class >= GFX9)
111 radeon_emit(cs, 0); /* unused */
112 } else {
113 if (ctx->chip_class == GFX7 || ctx->chip_class == GFX8) {
114 struct si_resource *scratch = ctx->eop_bug_scratch;
115 uint64_t va = scratch->gpu_address;
116
117 /* Two EOP events are required to make all engines go idle
118 * (and optional cache flushes executed) before the timestamp
119 * is written.
120 */
121 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));
122 radeon_emit(cs, op);
123 radeon_emit(cs, va);
124 radeon_emit(cs, ((va >> 32) & 0xffff) | sel);
125 radeon_emit(cs, 0); /* immediate data */
126 radeon_emit(cs, 0); /* unused */
127
128 radeon_add_to_buffer_list(ctx, ctx->gfx_cs, scratch, RADEON_USAGE_WRITE,
129 RADEON_PRIO_QUERY);
130 }
131
132 radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));
133 radeon_emit(cs, op);
134 radeon_emit(cs, va);
135 radeon_emit(cs, ((va >> 32) & 0xffff) | sel);
136 radeon_emit(cs, new_fence); /* immediate data */
137 radeon_emit(cs, 0); /* unused */
138 }
139
140 if (buf) {
141 radeon_add_to_buffer_list(ctx, ctx->gfx_cs, buf, RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);
142 }
143 }
144
si_cp_write_fence_dwords(struct si_screen * screen)145 unsigned si_cp_write_fence_dwords(struct si_screen *screen)
146 {
147 unsigned dwords = 6;
148
149 if (screen->info.chip_class == GFX7 || screen->info.chip_class == GFX8)
150 dwords *= 2;
151
152 return dwords;
153 }
154
si_cp_wait_mem(struct si_context * ctx,struct radeon_cmdbuf * cs,uint64_t va,uint32_t ref,uint32_t mask,unsigned flags)155 void si_cp_wait_mem(struct si_context *ctx, struct radeon_cmdbuf *cs, uint64_t va, uint32_t ref,
156 uint32_t mask, unsigned flags)
157 {
158 radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
159 radeon_emit(cs, WAIT_REG_MEM_MEM_SPACE(1) | flags);
160 radeon_emit(cs, va);
161 radeon_emit(cs, va >> 32);
162 radeon_emit(cs, ref); /* reference value */
163 radeon_emit(cs, mask); /* mask */
164 radeon_emit(cs, 4); /* poll interval */
165 }
166
si_add_fence_dependency(struct si_context * sctx,struct pipe_fence_handle * fence)167 static void si_add_fence_dependency(struct si_context *sctx, struct pipe_fence_handle *fence)
168 {
169 struct radeon_winsys *ws = sctx->ws;
170
171 if (sctx->sdma_cs)
172 ws->cs_add_fence_dependency(sctx->sdma_cs, fence, 0);
173 ws->cs_add_fence_dependency(sctx->gfx_cs, fence, 0);
174 }
175
si_add_syncobj_signal(struct si_context * sctx,struct pipe_fence_handle * fence)176 static void si_add_syncobj_signal(struct si_context *sctx, struct pipe_fence_handle *fence)
177 {
178 sctx->ws->cs_add_syncobj_signal(sctx->gfx_cs, fence);
179 }
180
si_fence_reference(struct pipe_screen * screen,struct pipe_fence_handle ** dst,struct pipe_fence_handle * src)181 static void si_fence_reference(struct pipe_screen *screen, struct pipe_fence_handle **dst,
182 struct pipe_fence_handle *src)
183 {
184 struct radeon_winsys *ws = ((struct si_screen *)screen)->ws;
185 struct si_multi_fence **sdst = (struct si_multi_fence **)dst;
186 struct si_multi_fence *ssrc = (struct si_multi_fence *)src;
187
188 if (pipe_reference(&(*sdst)->reference, &ssrc->reference)) {
189 ws->fence_reference(&(*sdst)->gfx, NULL);
190 ws->fence_reference(&(*sdst)->sdma, NULL);
191 tc_unflushed_batch_token_reference(&(*sdst)->tc_token, NULL);
192 si_resource_reference(&(*sdst)->fine.buf, NULL);
193 FREE(*sdst);
194 }
195 *sdst = ssrc;
196 }
197
si_create_multi_fence()198 static struct si_multi_fence *si_create_multi_fence()
199 {
200 struct si_multi_fence *fence = CALLOC_STRUCT(si_multi_fence);
201 if (!fence)
202 return NULL;
203
204 pipe_reference_init(&fence->reference, 1);
205 util_queue_fence_init(&fence->ready);
206
207 return fence;
208 }
209
si_create_fence(struct pipe_context * ctx,struct tc_unflushed_batch_token * tc_token)210 struct pipe_fence_handle *si_create_fence(struct pipe_context *ctx,
211 struct tc_unflushed_batch_token *tc_token)
212 {
213 struct si_multi_fence *fence = si_create_multi_fence();
214 if (!fence)
215 return NULL;
216
217 util_queue_fence_reset(&fence->ready);
218 tc_unflushed_batch_token_reference(&fence->tc_token, tc_token);
219
220 return (struct pipe_fence_handle *)fence;
221 }
222
si_fine_fence_signaled(struct radeon_winsys * rws,const struct si_fine_fence * fine)223 static bool si_fine_fence_signaled(struct radeon_winsys *rws, const struct si_fine_fence *fine)
224 {
225 char *map =
226 rws->buffer_map(fine->buf->buf, NULL, PIPE_MAP_READ | PIPE_MAP_UNSYNCHRONIZED);
227 if (!map)
228 return false;
229
230 uint32_t *fence = (uint32_t *)(map + fine->offset);
231 return *fence != 0;
232 }
233
si_fine_fence_set(struct si_context * ctx,struct si_fine_fence * fine,unsigned flags)234 static void si_fine_fence_set(struct si_context *ctx, struct si_fine_fence *fine, unsigned flags)
235 {
236 uint32_t *fence_ptr;
237
238 assert(util_bitcount(flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) == 1);
239
240 /* Use cached system memory for the fence. */
241 u_upload_alloc(ctx->cached_gtt_allocator, 0, 4, 4, &fine->offset,
242 (struct pipe_resource **)&fine->buf, (void **)&fence_ptr);
243 if (!fine->buf)
244 return;
245
246 *fence_ptr = 0;
247
248 if (flags & PIPE_FLUSH_TOP_OF_PIPE) {
249 uint32_t value = 0x80000000;
250
251 si_cp_write_data(ctx, fine->buf, fine->offset, 4, V_370_MEM, V_370_PFP, &value);
252 } else if (flags & PIPE_FLUSH_BOTTOM_OF_PIPE) {
253 uint64_t fence_va = fine->buf->gpu_address + fine->offset;
254
255 radeon_add_to_buffer_list(ctx, ctx->gfx_cs, fine->buf, RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);
256 si_cp_release_mem(ctx, ctx->gfx_cs, V_028A90_BOTTOM_OF_PIPE_TS, 0, EOP_DST_SEL_MEM,
257 EOP_INT_SEL_NONE, EOP_DATA_SEL_VALUE_32BIT, NULL, fence_va, 0x80000000,
258 PIPE_QUERY_GPU_FINISHED);
259 } else {
260 assert(false);
261 }
262 }
263
si_fence_finish(struct pipe_screen * screen,struct pipe_context * ctx,struct pipe_fence_handle * fence,uint64_t timeout)264 static bool si_fence_finish(struct pipe_screen *screen, struct pipe_context *ctx,
265 struct pipe_fence_handle *fence, uint64_t timeout)
266 {
267 struct radeon_winsys *rws = ((struct si_screen *)screen)->ws;
268 struct si_multi_fence *sfence = (struct si_multi_fence *)fence;
269 struct si_context *sctx;
270 int64_t abs_timeout = os_time_get_absolute_timeout(timeout);
271
272 ctx = threaded_context_unwrap_sync(ctx);
273 sctx = (struct si_context *)(ctx ? ctx : NULL);
274
275 if (!util_queue_fence_is_signalled(&sfence->ready)) {
276 if (sfence->tc_token) {
277 /* Ensure that si_flush_from_st will be called for
278 * this fence, but only if we're in the API thread
279 * where the context is current.
280 *
281 * Note that the batch containing the flush may already
282 * be in flight in the driver thread, so the fence
283 * may not be ready yet when this call returns.
284 */
285 threaded_context_flush(ctx, sfence->tc_token, timeout == 0);
286 }
287
288 if (!timeout)
289 return false;
290
291 if (timeout == PIPE_TIMEOUT_INFINITE) {
292 util_queue_fence_wait(&sfence->ready);
293 } else {
294 if (!util_queue_fence_wait_timeout(&sfence->ready, abs_timeout))
295 return false;
296 }
297
298 if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
299 int64_t time = os_time_get_nano();
300 timeout = abs_timeout > time ? abs_timeout - time : 0;
301 }
302 }
303
304 if (sfence->sdma) {
305 if (!rws->fence_wait(rws, sfence->sdma, timeout))
306 return false;
307
308 /* Recompute the timeout after waiting. */
309 if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
310 int64_t time = os_time_get_nano();
311 timeout = abs_timeout > time ? abs_timeout - time : 0;
312 }
313 }
314
315 if (!sfence->gfx)
316 return true;
317
318 if (sfence->fine.buf && si_fine_fence_signaled(rws, &sfence->fine)) {
319 rws->fence_reference(&sfence->gfx, NULL);
320 si_resource_reference(&sfence->fine.buf, NULL);
321 return true;
322 }
323
324 /* Flush the gfx IB if it hasn't been flushed yet. */
325 if (sctx && sfence->gfx_unflushed.ctx == sctx &&
326 sfence->gfx_unflushed.ib_index == sctx->num_gfx_cs_flushes) {
327 /* Section 4.1.2 (Signaling) of the OpenGL 4.6 (Core profile)
328 * spec says:
329 *
330 * "If the sync object being blocked upon will not be
331 * signaled in finite time (for example, by an associated
332 * fence command issued previously, but not yet flushed to
333 * the graphics pipeline), then ClientWaitSync may hang
334 * forever. To help prevent this behavior, if
335 * ClientWaitSync is called and all of the following are
336 * true:
337 *
338 * * the SYNC_FLUSH_COMMANDS_BIT bit is set in flags,
339 * * sync is unsignaled when ClientWaitSync is called,
340 * * and the calls to ClientWaitSync and FenceSync were
341 * issued from the same context,
342 *
343 * then the GL will behave as if the equivalent of Flush
344 * were inserted immediately after the creation of sync."
345 *
346 * This means we need to flush for such fences even when we're
347 * not going to wait.
348 */
349 si_flush_gfx_cs(sctx, (timeout ? 0 : PIPE_FLUSH_ASYNC) | RADEON_FLUSH_START_NEXT_GFX_IB_NOW,
350 NULL);
351 sfence->gfx_unflushed.ctx = NULL;
352
353 if (!timeout)
354 return false;
355
356 /* Recompute the timeout after all that. */
357 if (timeout && timeout != PIPE_TIMEOUT_INFINITE) {
358 int64_t time = os_time_get_nano();
359 timeout = abs_timeout > time ? abs_timeout - time : 0;
360 }
361 }
362
363 if (rws->fence_wait(rws, sfence->gfx, timeout))
364 return true;
365
366 /* Re-check in case the GPU is slow or hangs, but the commands before
367 * the fine-grained fence have completed. */
368 if (sfence->fine.buf && si_fine_fence_signaled(rws, &sfence->fine))
369 return true;
370
371 return false;
372 }
373
si_create_fence_fd(struct pipe_context * ctx,struct pipe_fence_handle ** pfence,int fd,enum pipe_fd_type type)374 static void si_create_fence_fd(struct pipe_context *ctx, struct pipe_fence_handle **pfence, int fd,
375 enum pipe_fd_type type)
376 {
377 struct si_screen *sscreen = (struct si_screen *)ctx->screen;
378 struct radeon_winsys *ws = sscreen->ws;
379 struct si_multi_fence *sfence;
380
381 *pfence = NULL;
382
383 sfence = si_create_multi_fence();
384 if (!sfence)
385 return;
386
387 switch (type) {
388 case PIPE_FD_TYPE_NATIVE_SYNC:
389 if (!sscreen->info.has_fence_to_handle)
390 goto finish;
391
392 sfence->gfx = ws->fence_import_sync_file(ws, fd);
393 break;
394
395 case PIPE_FD_TYPE_SYNCOBJ:
396 if (!sscreen->info.has_syncobj)
397 goto finish;
398
399 sfence->gfx = ws->fence_import_syncobj(ws, fd);
400 break;
401
402 default:
403 unreachable("bad fence fd type when importing");
404 }
405
406 finish:
407 if (!sfence->gfx) {
408 FREE(sfence);
409 return;
410 }
411
412 *pfence = (struct pipe_fence_handle *)sfence;
413 }
414
si_fence_get_fd(struct pipe_screen * screen,struct pipe_fence_handle * fence)415 static int si_fence_get_fd(struct pipe_screen *screen, struct pipe_fence_handle *fence)
416 {
417 struct si_screen *sscreen = (struct si_screen *)screen;
418 struct radeon_winsys *ws = sscreen->ws;
419 struct si_multi_fence *sfence = (struct si_multi_fence *)fence;
420 int gfx_fd = -1, sdma_fd = -1;
421
422 if (!sscreen->info.has_fence_to_handle)
423 return -1;
424
425 util_queue_fence_wait(&sfence->ready);
426
427 /* Deferred fences aren't supported. */
428 assert(!sfence->gfx_unflushed.ctx);
429 if (sfence->gfx_unflushed.ctx)
430 return -1;
431
432 if (sfence->sdma) {
433 sdma_fd = ws->fence_export_sync_file(ws, sfence->sdma);
434 if (sdma_fd == -1)
435 return -1;
436 }
437 if (sfence->gfx) {
438 gfx_fd = ws->fence_export_sync_file(ws, sfence->gfx);
439 if (gfx_fd == -1) {
440 if (sdma_fd != -1)
441 close(sdma_fd);
442 return -1;
443 }
444 }
445
446 /* If we don't have FDs at this point, it means we don't have fences
447 * either. */
448 if (sdma_fd == -1 && gfx_fd == -1)
449 return ws->export_signalled_sync_file(ws);
450 if (sdma_fd == -1)
451 return gfx_fd;
452 if (gfx_fd == -1)
453 return sdma_fd;
454
455 /* Get a fence that will be a combination of both fences. */
456 sync_accumulate("radeonsi", &gfx_fd, sdma_fd);
457 close(sdma_fd);
458 return gfx_fd;
459 }
460
si_flush_from_st(struct pipe_context * ctx,struct pipe_fence_handle ** fence,unsigned flags)461 static void si_flush_from_st(struct pipe_context *ctx, struct pipe_fence_handle **fence,
462 unsigned flags)
463 {
464 struct pipe_screen *screen = ctx->screen;
465 struct si_context *sctx = (struct si_context *)ctx;
466 struct radeon_winsys *ws = sctx->ws;
467 struct pipe_fence_handle *gfx_fence = NULL;
468 struct pipe_fence_handle *sdma_fence = NULL;
469 bool deferred_fence = false;
470 struct si_fine_fence fine = {};
471 unsigned rflags = PIPE_FLUSH_ASYNC;
472
473 if (flags & PIPE_FLUSH_END_OF_FRAME)
474 rflags |= PIPE_FLUSH_END_OF_FRAME;
475
476 if (flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) {
477 assert(flags & PIPE_FLUSH_DEFERRED);
478 assert(fence);
479
480 si_fine_fence_set(sctx, &fine, flags);
481 }
482
483 /* DMA IBs are preambles to gfx IBs, therefore must be flushed first. */
484 if (sctx->sdma_cs)
485 si_flush_dma_cs(sctx, rflags, fence ? &sdma_fence : NULL);
486
487 if (!radeon_emitted(sctx->gfx_cs, sctx->initial_gfx_cs_size)) {
488 if (fence)
489 ws->fence_reference(&gfx_fence, sctx->last_gfx_fence);
490 if (!(flags & PIPE_FLUSH_DEFERRED))
491 ws->cs_sync_flush(sctx->gfx_cs);
492 } else {
493 /* Instead of flushing, create a deferred fence. Constraints:
494 * - the gallium frontend must allow a deferred flush.
495 * - the gallium frontend must request a fence.
496 * - fence_get_fd is not allowed.
497 * Thread safety in fence_finish must be ensured by the gallium frontend.
498 */
499 if (flags & PIPE_FLUSH_DEFERRED && !(flags & PIPE_FLUSH_FENCE_FD) && fence) {
500 gfx_fence = sctx->ws->cs_get_next_fence(sctx->gfx_cs);
501 deferred_fence = true;
502 } else {
503 si_flush_gfx_cs(sctx, rflags, fence ? &gfx_fence : NULL);
504 }
505 }
506
507 /* Both engines can signal out of order, so we need to keep both fences. */
508 if (fence) {
509 struct si_multi_fence *multi_fence;
510
511 if (flags & TC_FLUSH_ASYNC) {
512 multi_fence = (struct si_multi_fence *)*fence;
513 assert(multi_fence);
514 } else {
515 multi_fence = si_create_multi_fence();
516 if (!multi_fence) {
517 ws->fence_reference(&sdma_fence, NULL);
518 ws->fence_reference(&gfx_fence, NULL);
519 goto finish;
520 }
521
522 screen->fence_reference(screen, fence, NULL);
523 *fence = (struct pipe_fence_handle *)multi_fence;
524 }
525
526 /* If both fences are NULL, fence_finish will always return true. */
527 multi_fence->gfx = gfx_fence;
528 multi_fence->sdma = sdma_fence;
529
530 if (deferred_fence) {
531 multi_fence->gfx_unflushed.ctx = sctx;
532 multi_fence->gfx_unflushed.ib_index = sctx->num_gfx_cs_flushes;
533 }
534
535 multi_fence->fine = fine;
536 fine.buf = NULL;
537
538 if (flags & TC_FLUSH_ASYNC) {
539 util_queue_fence_signal(&multi_fence->ready);
540 tc_unflushed_batch_token_reference(&multi_fence->tc_token, NULL);
541 }
542 }
543 assert(!fine.buf);
544 finish:
545 if (!(flags & (PIPE_FLUSH_DEFERRED | PIPE_FLUSH_ASYNC))) {
546 if (sctx->sdma_cs)
547 ws->cs_sync_flush(sctx->sdma_cs);
548 ws->cs_sync_flush(sctx->gfx_cs);
549 }
550 }
551
si_fence_server_signal(struct pipe_context * ctx,struct pipe_fence_handle * fence)552 static void si_fence_server_signal(struct pipe_context *ctx, struct pipe_fence_handle *fence)
553 {
554 struct si_context *sctx = (struct si_context *)ctx;
555 struct si_multi_fence *sfence = (struct si_multi_fence *)fence;
556
557 /* We should have at least one syncobj to signal */
558 assert(sfence->sdma || sfence->gfx);
559
560 if (sfence->sdma)
561 si_add_syncobj_signal(sctx, sfence->sdma);
562 if (sfence->gfx)
563 si_add_syncobj_signal(sctx, sfence->gfx);
564
565 /**
566 * The spec does not require a flush here. We insert a flush
567 * because syncobj based signals are not directly placed into
568 * the command stream. Instead the signal happens when the
569 * submission associated with the syncobj finishes execution.
570 *
571 * Therefore, we must make sure that we flush the pipe to avoid
572 * new work being emitted and getting executed before the signal
573 * operation.
574 *
575 * Set sctx->initial_gfx_cs_size to force IB submission even if
576 * it is empty.
577 */
578 sctx->initial_gfx_cs_size = 0;
579 si_flush_from_st(ctx, NULL, PIPE_FLUSH_ASYNC);
580 }
581
si_fence_server_sync(struct pipe_context * ctx,struct pipe_fence_handle * fence)582 static void si_fence_server_sync(struct pipe_context *ctx, struct pipe_fence_handle *fence)
583 {
584 struct si_context *sctx = (struct si_context *)ctx;
585 struct si_multi_fence *sfence = (struct si_multi_fence *)fence;
586
587 util_queue_fence_wait(&sfence->ready);
588
589 /* Unflushed fences from the same context are no-ops. */
590 if (sfence->gfx_unflushed.ctx && sfence->gfx_unflushed.ctx == sctx)
591 return;
592
593 /* All unflushed commands will not start execution before this fence
594 * dependency is signalled. That's fine. Flushing is very expensive
595 * if we get fence_server_sync after every draw call. (which happens
596 * with Android/SurfaceFlinger)
597 *
598 * In a nutshell, when CPU overhead is greater than GPU overhead,
599 * or when the time it takes to execute an IB on the GPU is less than
600 * the time it takes to create and submit that IB, flushing decreases
601 * performance. Therefore, DO NOT FLUSH.
602 */
603 if (sfence->sdma)
604 si_add_fence_dependency(sctx, sfence->sdma);
605 if (sfence->gfx)
606 si_add_fence_dependency(sctx, sfence->gfx);
607 }
608
si_init_fence_functions(struct si_context * ctx)609 void si_init_fence_functions(struct si_context *ctx)
610 {
611 ctx->b.flush = si_flush_from_st;
612 ctx->b.create_fence_fd = si_create_fence_fd;
613 ctx->b.fence_server_sync = si_fence_server_sync;
614 ctx->b.fence_server_signal = si_fence_server_signal;
615 }
616
si_init_screen_fence_functions(struct si_screen * screen)617 void si_init_screen_fence_functions(struct si_screen *screen)
618 {
619 screen->b.fence_finish = si_fence_finish;
620 screen->b.fence_reference = si_fence_reference;
621 screen->b.fence_get_fd = si_fence_get_fd;
622 }
623