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