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