freedreno: native fence fd support
authorRob Clark <robdclark@gmail.com>
Mon, 15 Aug 2016 18:27:10 +0000 (14:27 -0400)
committerRob Clark <robdclark@gmail.com>
Fri, 2 Dec 2016 01:24:46 +0000 (20:24 -0500)
Signed-off-by: Rob Clark <robdclark@gmail.com>
src/gallium/drivers/freedreno/freedreno_batch.c
src/gallium/drivers/freedreno/freedreno_batch.h
src/gallium/drivers/freedreno/freedreno_context.c
src/gallium/drivers/freedreno/freedreno_fence.c
src/gallium/drivers/freedreno/freedreno_fence.h
src/gallium/drivers/freedreno/freedreno_gmem.c
src/gallium/drivers/freedreno/freedreno_screen.c

index 369b7e9cda443a598fb25460231b9d7108097442..646c76fb57d3f023e7c07bf90301c72d91e011cd 100644 (file)
@@ -60,6 +60,8 @@ batch_init(struct fd_batch *batch)
        fd_ringbuffer_set_parent(batch->draw, batch->gmem);
        fd_ringbuffer_set_parent(batch->binning, batch->gmem);
 
+       batch->in_fence_fd = -1;
+
        batch->cleared = batch->partial_cleared = 0;
        batch->restore = batch->resolve = 0;
        batch->needs_flush = false;
@@ -109,6 +111,9 @@ batch_fini(struct fd_batch *batch)
 {
        pipe_resource_reference(&batch->query_buf, NULL);
 
+       if (batch->in_fence_fd != -1)
+               close(batch->in_fence_fd);
+
        fd_ringbuffer_del(batch->draw);
        fd_ringbuffer_del(batch->binning);
        fd_ringbuffer_del(batch->gmem);
index 1e9545971be935bf30709e22792dac05870220ef..095d214468b009a297bfcb5186dcf57f54400fc5 100644 (file)
@@ -68,6 +68,9 @@ struct fd_batch {
        unsigned seqno;
        unsigned idx;
 
+       int in_fence_fd;
+       bool needs_out_fence_fd;
+
        struct fd_context *ctx;
 
        struct util_queue_fence flush_fence;
index 70220f88deeda5d4275635bc32a2c59ae584f59b..03645075bdca05df92b6afe0873ba02d6ec87f33 100644 (file)
@@ -44,6 +44,9 @@ fd_context_flush(struct pipe_context *pctx, struct pipe_fence_handle **fence,
 {
        struct fd_context *ctx = fd_context(pctx);
 
+       if (flags & PIPE_FLUSH_FENCE_FD)
+               ctx->batch->needs_out_fence_fd = true;
+
        if (!ctx->screen->reorder) {
                fd_batch_flush(ctx->batch, true);
        } else {
@@ -251,6 +254,8 @@ fd_context_init(struct fd_context *ctx, struct pipe_screen *pscreen,
        pctx->flush = fd_context_flush;
        pctx->emit_string_marker = fd_emit_string_marker;
        pctx->set_debug_callback = fd_set_debug_callback;
+       pctx->create_fence_fd = fd_create_fence_fd;
+       pctx->fence_server_sync = fd_fence_server_sync;
 
        /* TODO what about compute?  Ideally it creates it's own independent
         * batches per compute job (since it isn't using tiling, so no point
index a5f717169f9fabca8fff47bd238e07b2e5e9c640..f20c6ac120e21539d06a026486a36e03279c826a 100644 (file)
@@ -26,6 +26,8 @@
  *    Rob Clark <robclark@freedesktop.org>
  */
 
+#include <libsync.h>
+
 #include "util/u_inlines.h"
 
 #include "freedreno_fence.h"
@@ -36,16 +38,23 @@ struct pipe_fence_handle {
        struct pipe_reference reference;
        struct fd_context *ctx;
        struct fd_screen *screen;
+       int fence_fd;
        uint32_t timestamp;
 };
 
-void
-fd_fence_ref(struct pipe_screen *pscreen,
+static void fd_fence_destroy(struct pipe_fence_handle *fence)
+{
+       if (fence->fence_fd != -1)
+               close(fence->fence_fd);
+       FREE(fence);
+}
+
+void fd_fence_ref(struct pipe_screen *pscreen,
                struct pipe_fence_handle **ptr,
                struct pipe_fence_handle *pfence)
 {
        if (pipe_reference(&(*ptr)->reference, &pfence->reference))
-               FREE(*ptr);
+               fd_fence_destroy(*ptr);
 
        *ptr = pfence;
 }
@@ -55,14 +64,42 @@ boolean fd_fence_finish(struct pipe_screen *pscreen,
                struct pipe_fence_handle *fence,
                uint64_t timeout)
 {
+       if (fence->fence_fd != -1) {
+               int ret = sync_wait(fence->fence_fd, timeout / 1000000);
+               return ret == 0;
+       }
+
        if (fd_pipe_wait_timeout(fence->screen->pipe, fence->timestamp, timeout))
                return false;
 
        return true;
 }
 
+void fd_create_fence_fd(struct pipe_context *pctx,
+               struct pipe_fence_handle **pfence, int fd)
+{
+       *pfence = fd_fence_create(fd_context(pctx), 0, dup(fd));
+}
+
+void fd_fence_server_sync(struct pipe_context *pctx,
+               struct pipe_fence_handle *fence)
+{
+       struct fd_context *ctx = fd_context(pctx);
+       struct fd_batch *batch = ctx->batch;
+
+       if (sync_accumulate("freedreno", &batch->in_fence_fd, fence->fence_fd)) {
+               /* error */
+       }
+}
+
+int fd_fence_get_fd(struct pipe_screen *pscreen,
+               struct pipe_fence_handle *fence)
+{
+       return dup(fence->fence_fd);
+}
+
 struct pipe_fence_handle * fd_fence_create(struct fd_context *ctx,
-               uint32_t timestamp)
+               uint32_t timestamp, int fence_fd)
 {
        struct pipe_fence_handle *fence;
 
@@ -75,6 +112,7 @@ struct pipe_fence_handle * fd_fence_create(struct fd_context *ctx,
        fence->ctx = ctx;
        fence->screen = ctx->screen;
        fence->timestamp = timestamp;
+       fence->fence_fd = fence_fd;
 
        return fence;
 }
index 32bfacc7627f02d3682471dfe836f983fa181c5d..1de2d0a5146666abcf1c6c487e55b7022c874799 100644 (file)
@@ -38,9 +38,15 @@ boolean fd_fence_finish(struct pipe_screen *screen,
                struct pipe_context *ctx,
                struct pipe_fence_handle *pfence,
                uint64_t timeout);
+void fd_create_fence_fd(struct pipe_context *pctx,
+               struct pipe_fence_handle **pfence, int fd);
+void fd_fence_server_sync(struct pipe_context *pctx,
+               struct pipe_fence_handle *fence);
+int fd_fence_get_fd(struct pipe_screen *pscreen,
+               struct pipe_fence_handle *pfence);
 
 struct fd_context;
 struct pipe_fence_handle * fd_fence_create(struct fd_context *ctx,
-               uint32_t timestamp);
+               uint32_t timestamp, int fence_fd);
 
 #endif /* FREEDRENO_FENCE_H_ */
index 6d243c6d1e2df958d96617712bee21354536a5ff..69fb16d058bdafe579c4bc4c568fd1d8eac477b9 100644 (file)
@@ -399,10 +399,13 @@ fd_gmem_render_tiles(struct fd_batch *batch)
                ctx->stats.batch_gmem++;
        }
 
-       fd_ringbuffer_flush(batch->gmem);
+       int out_fence_fd = -1;
+       fd_ringbuffer_flush2(batch->gmem, batch->in_fence_fd,
+                       batch->needs_out_fence_fd ? &out_fence_fd : NULL);
 
        fd_fence_ref(&ctx->screen->base, &ctx->last_fence, NULL);
-       ctx->last_fence = fd_fence_create(ctx, fd_ringbuffer_timestamp(batch->gmem));
+       ctx->last_fence = fd_fence_create(ctx,
+                       fd_ringbuffer_timestamp(batch->gmem), out_fence_fd);
 }
 
 /* tile needs restore if it isn't completely contained within the
index 07212dc0941936708618adeac320122151a8f4d7..8ab0e37fa0d599ce5770476f0f544d8b3ec43e2b 100644 (file)
@@ -380,7 +380,7 @@ fd_screen_get_param(struct pipe_screen *pscreen, enum pipe_cap param)
        case PIPE_CAP_UMA:
                return 1;
        case PIPE_CAP_NATIVE_FENCE_FD:
-               return 0;
+               return fd_device_version(screen->dev) >= FD_VERSION_FENCE_FD;
        }
        debug_printf("unknown param %d\n", param);
        return 0;
@@ -713,6 +713,7 @@ fd_screen_create(struct fd_device *dev)
 
        pscreen->fence_reference = fd_fence_ref;
        pscreen->fence_finish = fd_fence_finish;
+       pscreen->fence_get_fd = fd_fence_get_fd;
 
        slab_create_parent(&screen->transfer_pool, sizeof(struct fd_transfer), 16);