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