radeonsi: move EOP event code from r600_pipe_common.c to si_fence.c
[mesa.git] / src / gallium / drivers / radeonsi / si_fence.c
index 9d6bcfe102745f6d1e7e2d9129bee8428f8ce4d0..3b437091b1eb272578410d6c65b9db0eb82c948f 100644 (file)
@@ -53,6 +53,127 @@ struct si_multi_fence {
        struct si_fine_fence fine;
 };
 
+/**
+ * Write an EOP event.
+ *
+ * \param event                EVENT_TYPE_*
+ * \param event_flags  Optional cache flush flags (TC)
+ * \param data_sel     1 = fence, 3 = timestamp
+ * \param buf          Buffer
+ * \param va           GPU address
+ * \param old_value    Previous fence value (for a bug workaround)
+ * \param new_value    Fence value to write for this event.
+ */
+void si_gfx_write_event_eop(struct r600_common_context *ctx,
+                           unsigned event, unsigned event_flags,
+                           unsigned data_sel,
+                           struct r600_resource *buf, uint64_t va,
+                           uint32_t new_fence, unsigned query_type)
+{
+       struct radeon_winsys_cs *cs = ctx->gfx.cs;
+       unsigned op = EVENT_TYPE(event) |
+                     EVENT_INDEX(5) |
+                     event_flags;
+       unsigned sel = EOP_DATA_SEL(data_sel);
+
+       /* Wait for write confirmation before writing data, but don't send
+        * an interrupt. */
+       if (data_sel != EOP_DATA_SEL_DISCARD)
+               sel |= EOP_INT_SEL(EOP_INT_SEL_SEND_DATA_AFTER_WR_CONFIRM);
+
+       if (ctx->chip_class >= GFX9) {
+               /* A ZPASS_DONE or PIXEL_STAT_DUMP_EVENT (of the DB occlusion
+                * counters) must immediately precede every timestamp event to
+                * prevent a GPU hang on GFX9.
+                *
+                * Occlusion queries don't need to do it here, because they
+                * always do ZPASS_DONE before the timestamp.
+                */
+               if (ctx->chip_class == GFX9 &&
+                   query_type != PIPE_QUERY_OCCLUSION_COUNTER &&
+                   query_type != PIPE_QUERY_OCCLUSION_PREDICATE &&
+                   query_type != PIPE_QUERY_OCCLUSION_PREDICATE_CONSERVATIVE) {
+                       struct r600_resource *scratch = ctx->eop_bug_scratch;
+
+                       assert(16 * ctx->screen->info.num_render_backends <=
+                              scratch->b.b.width0);
+                       radeon_emit(cs, PKT3(PKT3_EVENT_WRITE, 2, 0));
+                       radeon_emit(cs, EVENT_TYPE(EVENT_TYPE_ZPASS_DONE) | EVENT_INDEX(1));
+                       radeon_emit(cs, scratch->gpu_address);
+                       radeon_emit(cs, scratch->gpu_address >> 32);
+
+                       radeon_add_to_buffer_list(ctx, &ctx->gfx, scratch,
+                                                 RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);
+               }
+
+               radeon_emit(cs, PKT3(PKT3_RELEASE_MEM, 6, 0));
+               radeon_emit(cs, op);
+               radeon_emit(cs, sel);
+               radeon_emit(cs, va);            /* address lo */
+               radeon_emit(cs, va >> 32);      /* address hi */
+               radeon_emit(cs, new_fence);     /* immediate data lo */
+               radeon_emit(cs, 0); /* immediate data hi */
+               radeon_emit(cs, 0); /* unused */
+       } else {
+               if (ctx->chip_class == CIK ||
+                   ctx->chip_class == VI) {
+                       struct r600_resource *scratch = ctx->eop_bug_scratch;
+                       uint64_t va = scratch->gpu_address;
+
+                       /* Two EOP events are required to make all engines go idle
+                        * (and optional cache flushes executed) before the timestamp
+                        * is written.
+                        */
+                       radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));
+                       radeon_emit(cs, op);
+                       radeon_emit(cs, va);
+                       radeon_emit(cs, ((va >> 32) & 0xffff) | sel);
+                       radeon_emit(cs, 0); /* immediate data */
+                       radeon_emit(cs, 0); /* unused */
+
+                       radeon_add_to_buffer_list(ctx, &ctx->gfx, scratch,
+                                                 RADEON_USAGE_WRITE, RADEON_PRIO_QUERY);
+               }
+
+               radeon_emit(cs, PKT3(PKT3_EVENT_WRITE_EOP, 4, 0));
+               radeon_emit(cs, op);
+               radeon_emit(cs, va);
+               radeon_emit(cs, ((va >> 32) & 0xffff) | sel);
+               radeon_emit(cs, new_fence); /* immediate data */
+               radeon_emit(cs, 0); /* unused */
+       }
+
+       if (buf) {
+               radeon_add_to_buffer_list(ctx, &ctx->gfx, buf, RADEON_USAGE_WRITE,
+                                         RADEON_PRIO_QUERY);
+       }
+}
+
+unsigned si_gfx_write_fence_dwords(struct si_screen *screen)
+{
+       unsigned dwords = 6;
+
+       if (screen->info.chip_class == CIK ||
+           screen->info.chip_class == VI)
+               dwords *= 2;
+
+       return dwords;
+}
+
+void si_gfx_wait_fence(struct r600_common_context *ctx,
+                      uint64_t va, uint32_t ref, uint32_t mask)
+{
+       struct radeon_winsys_cs *cs = ctx->gfx.cs;
+
+       radeon_emit(cs, PKT3(PKT3_WAIT_REG_MEM, 5, 0));
+       radeon_emit(cs, WAIT_REG_MEM_EQUAL | WAIT_REG_MEM_MEM_SPACE(1));
+       radeon_emit(cs, va);
+       radeon_emit(cs, va >> 32);
+       radeon_emit(cs, ref); /* reference value */
+       radeon_emit(cs, mask); /* mask */
+       radeon_emit(cs, 4); /* poll interval */
+}
+
 static void si_add_fence_dependency(struct r600_common_context *rctx,
                                    struct pipe_fence_handle *fence)
 {
@@ -63,11 +184,19 @@ static void si_add_fence_dependency(struct r600_common_context *rctx,
        ws->cs_add_fence_dependency(rctx->gfx.cs, fence);
 }
 
+static void si_add_syncobj_signal(struct r600_common_context *rctx,
+                                 struct pipe_fence_handle *fence)
+{
+       struct radeon_winsys *ws = rctx->ws;
+
+       ws->cs_add_syncobj_signal(rctx->gfx.cs, fence);
+}
+
 static void si_fence_reference(struct pipe_screen *screen,
                               struct pipe_fence_handle **dst,
                               struct pipe_fence_handle *src)
 {
-       struct radeon_winsys *ws = ((struct r600_common_screen*)screen)->ws;
+       struct radeon_winsys *ws = ((struct si_screen*)screen)->ws;
        struct si_multi_fence **rdst = (struct si_multi_fence **)dst;
        struct si_multi_fence *rsrc = (struct si_multi_fence *)src;
 
@@ -106,30 +235,6 @@ struct pipe_fence_handle *si_create_fence(struct pipe_context *ctx,
        return (struct pipe_fence_handle *)fence;
 }
 
-static void si_fence_server_sync(struct pipe_context *ctx,
-                                struct pipe_fence_handle *fence)
-{
-       struct r600_common_context *rctx = (struct r600_common_context *)ctx;
-       struct si_multi_fence *rfence = (struct si_multi_fence *)fence;
-
-       util_queue_fence_wait(&rfence->ready);
-
-       /* Unflushed fences from the same context are no-ops. */
-       if (rfence->gfx_unflushed.ctx &&
-           rfence->gfx_unflushed.ctx == rctx)
-               return;
-
-       /* All unflushed commands will not start execution before
-        * this fence dependency is signalled.
-        *
-        * Should we flush the context to allow more GPU parallelism?
-        */
-       if (rfence->sdma)
-               si_add_fence_dependency(rctx, rfence->sdma);
-       if (rfence->gfx)
-               si_add_fence_dependency(rctx, rfence->gfx);
-}
-
 static bool si_fine_fence_signaled(struct radeon_winsys *rws,
                                   const struct si_fine_fence *fine)
 {
@@ -151,7 +256,7 @@ static void si_fine_fence_set(struct si_context *ctx,
        assert(util_bitcount(flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) == 1);
 
        /* Use uncached system memory for the fence. */
-       u_upload_alloc(ctx->b.b.stream_uploader, 0, 4, 4,
+       u_upload_alloc(ctx->b.cached_gtt_allocator, 0, 4, 4,
                       &fine->offset, (struct pipe_resource **)&fine->buf, (void **)&fence_ptr);
        if (!fine->buf)
                return;
@@ -186,14 +291,11 @@ static boolean si_fence_finish(struct pipe_screen *screen,
                               struct pipe_fence_handle *fence,
                               uint64_t timeout)
 {
-       struct radeon_winsys *rws = ((struct r600_common_screen*)screen)->ws;
+       struct radeon_winsys *rws = ((struct si_screen*)screen)->ws;
        struct si_multi_fence *rfence = (struct si_multi_fence *)fence;
        int64_t abs_timeout = os_time_get_absolute_timeout(timeout);
 
        if (!util_queue_fence_is_signalled(&rfence->ready)) {
-               if (!timeout)
-                       return false;
-
                if (rfence->tc_token) {
                        /* Ensure that si_flush_from_st will be called for
                         * this fence, but only if we're in the API thread
@@ -207,6 +309,9 @@ static boolean si_fence_finish(struct pipe_screen *screen,
                                               timeout == 0);
                }
 
+               if (!timeout)
+                       return false;
+
                if (timeout == PIPE_TIMEOUT_INFINITE) {
                        util_queue_fence_wait(&rfence->ready);
                } else {
@@ -271,7 +376,7 @@ static boolean si_fence_finish(struct pipe_screen *screen,
                         * not going to wait.
                         */
                        threaded_context_unwrap_sync(ctx);
-                       sctx->b.gfx.flush(&sctx->b, timeout ? 0 : RADEON_FLUSH_ASYNC, NULL);
+                       sctx->b.gfx.flush(&sctx->b, timeout ? 0 : PIPE_FLUSH_ASYNC, NULL);
                        rfence->gfx_unflushed.ctx = NULL;
 
                        if (!timeout)
@@ -298,22 +403,39 @@ static boolean si_fence_finish(struct pipe_screen *screen,
 }
 
 static void si_create_fence_fd(struct pipe_context *ctx,
-                              struct pipe_fence_handle **pfence, int fd)
+                              struct pipe_fence_handle **pfence, int fd,
+                              enum pipe_fd_type type)
 {
-       struct r600_common_screen *rscreen = (struct r600_common_screen*)ctx->screen;
-       struct radeon_winsys *ws = rscreen->ws;
+       struct si_screen *sscreen = (struct si_screen*)ctx->screen;
+       struct radeon_winsys *ws = sscreen->ws;
        struct si_multi_fence *rfence;
 
        *pfence = NULL;
 
-       if (!rscreen->info.has_sync_file)
-               return;
-
        rfence = si_create_multi_fence();
        if (!rfence)
                return;
 
-       rfence->gfx = ws->fence_import_sync_file(ws, fd);
+       switch (type) {
+       case PIPE_FD_TYPE_NATIVE_SYNC:
+               if (!sscreen->info.has_fence_to_handle)
+                       goto finish;
+
+               rfence->gfx = ws->fence_import_sync_file(ws, fd);
+               break;
+
+       case PIPE_FD_TYPE_SYNCOBJ:
+               if (!sscreen->info.has_syncobj)
+                       goto finish;
+
+               rfence->gfx = ws->fence_import_syncobj(ws, fd);
+               break;
+
+       default:
+               unreachable("bad fence fd type when importing");
+       }
+
+finish:
        if (!rfence->gfx) {
                FREE(rfence);
                return;
@@ -325,12 +447,12 @@ static void si_create_fence_fd(struct pipe_context *ctx,
 static int si_fence_get_fd(struct pipe_screen *screen,
                           struct pipe_fence_handle *fence)
 {
-       struct r600_common_screen *rscreen = (struct r600_common_screen*)screen;
-       struct radeon_winsys *ws = rscreen->ws;
+       struct si_screen *sscreen = (struct si_screen*)screen;
+       struct radeon_winsys *ws = sscreen->ws;
        struct si_multi_fence *rfence = (struct si_multi_fence *)fence;
        int gfx_fd = -1, sdma_fd = -1;
 
-       if (!rscreen->info.has_sync_file)
+       if (!sscreen->info.has_fence_to_handle)
                return -1;
 
        util_queue_fence_wait(&rfence->ready);
@@ -356,6 +478,8 @@ static int si_fence_get_fd(struct pipe_screen *screen,
 
        /* If we don't have FDs at this point, it means we don't have fences
         * either. */
+       if (sdma_fd == -1 && gfx_fd == -1)
+               return ws->export_signalled_sync_file(ws);
        if (sdma_fd == -1)
                return gfx_fd;
        if (gfx_fd == -1)
@@ -378,10 +502,10 @@ static void si_flush_from_st(struct pipe_context *ctx,
        struct pipe_fence_handle *sdma_fence = NULL;
        bool deferred_fence = false;
        struct si_fine_fence fine = {};
-       unsigned rflags = RADEON_FLUSH_ASYNC;
+       unsigned rflags = PIPE_FLUSH_ASYNC;
 
        if (flags & PIPE_FLUSH_END_OF_FRAME)
-               rflags |= RADEON_FLUSH_END_OF_FRAME;
+               rflags |= PIPE_FLUSH_END_OF_FRAME;
 
        if (flags & (PIPE_FLUSH_TOP_OF_PIPE | PIPE_FLUSH_BOTTOM_OF_PIPE)) {
                assert(flags & PIPE_FLUSH_DEFERRED);
@@ -445,12 +569,14 @@ static void si_flush_from_st(struct pipe_context *ctx,
                }
 
                multi_fence->fine = fine;
+               fine.buf = NULL;
 
                if (flags & TC_FLUSH_ASYNC) {
                        util_queue_fence_signal(&multi_fence->ready);
                        tc_unflushed_batch_token_reference(&multi_fence->tc_token, NULL);
                }
        }
+       assert(!fine.buf);
 finish:
        if (!(flags & PIPE_FLUSH_DEFERRED)) {
                if (rctx->dma.cs)
@@ -459,16 +585,70 @@ finish:
        }
 }
 
+static void si_fence_server_signal(struct pipe_context *ctx,
+                                  struct pipe_fence_handle *fence)
+{
+       struct r600_common_context *rctx = (struct r600_common_context *)ctx;
+       struct si_multi_fence *rfence = (struct si_multi_fence *)fence;
+
+       /* We should have at least one syncobj to signal */
+       assert(rfence->sdma || rfence->gfx);
+
+       if (rfence->sdma)
+               si_add_syncobj_signal(rctx, rfence->sdma);
+       if (rfence->gfx)
+               si_add_syncobj_signal(rctx, rfence->gfx);
+
+       /**
+        * The spec does not require a flush here. We insert a flush
+        * because syncobj based signals are not directly placed into
+        * the command stream. Instead the signal happens when the
+        * submission associated with the syncobj finishes execution.
+        *
+        * Therefore, we must make sure that we flush the pipe to avoid
+        * new work being emitted and getting executed before the signal
+        * operation.
+        */
+       si_flush_from_st(ctx, NULL, PIPE_FLUSH_ASYNC);
+}
+
+static void si_fence_server_sync(struct pipe_context *ctx,
+                                struct pipe_fence_handle *fence)
+{
+       struct r600_common_context *rctx = (struct r600_common_context *)ctx;
+       struct si_multi_fence *rfence = (struct si_multi_fence *)fence;
+
+       util_queue_fence_wait(&rfence->ready);
+
+       /* Unflushed fences from the same context are no-ops. */
+       if (rfence->gfx_unflushed.ctx &&
+           rfence->gfx_unflushed.ctx == rctx)
+               return;
+
+       /* All unflushed commands will not start execution before
+        * this fence dependency is signalled.
+        *
+        * Therefore we must flush before inserting the dependency
+        */
+       si_flush_from_st(ctx, NULL, PIPE_FLUSH_ASYNC);
+
+       if (rfence->sdma)
+               si_add_fence_dependency(rctx, rfence->sdma);
+       if (rfence->gfx)
+               si_add_fence_dependency(rctx, rfence->gfx);
+}
+
 void si_init_fence_functions(struct si_context *ctx)
 {
        ctx->b.b.flush = si_flush_from_st;
        ctx->b.b.create_fence_fd = si_create_fence_fd;
        ctx->b.b.fence_server_sync = si_fence_server_sync;
+       ctx->b.b.fence_server_signal = si_fence_server_signal;
 }
 
 void si_init_screen_fence_functions(struct si_screen *screen)
 {
-       screen->b.b.fence_finish = si_fence_finish;
-       screen->b.b.fence_reference = si_fence_reference;
-       screen->b.b.fence_get_fd = si_fence_get_fd;
+       screen->b.fence_finish = si_fence_finish;
+       screen->b.fence_reference = si_fence_reference;
+       screen->b.fence_get_fd = si_fence_get_fd;
 }