AMD-GFX Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Dave Airlie <airlied@gmail.com>
To: amd-gfx@lists.freedesktop.org, dri-devel@lists.freedesktop.org
Subject: [PATCH 5/8] sync_file: add support for a semaphore object (v2)
Date: Wed, 12 Apr 2017 14:57:23 +1000	[thread overview]
Message-ID: <20170412045726.13689-6-airlied@gmail.com> (raw)
In-Reply-To: <20170412045726.13689-1-airlied@gmail.com>

From: Dave Airlie <airlied@redhat.com>

This object can be used to implement the Vulkan semaphores.

The object behaviour differs from fence, in that you can
replace the underlying fence, and you cannot merge semaphores.

v2: change replace fence API to have a return value,
don't allow polling on semaphore objects,
don't worry about polling when replacing fences (since
we don't allow polling anymore).
switch APIs to one to retreive a fence from a sync_file
to make AMDGPU implementation more robust.

Signed-off-by: Dave Airlie <airlied@redhat.com>
---
 drivers/dma-buf/sync_file.c    | 73 ++++++++++++++++++++++++++++++++++++------
 include/linux/sync_file.h      |  4 +++
 include/uapi/linux/sync_file.h | 14 ++++++++
 3 files changed, 81 insertions(+), 10 deletions(-)

diff --git a/drivers/dma-buf/sync_file.c b/drivers/dma-buf/sync_file.c
index 57ea7c7..01c12e7 100644
--- a/drivers/dma-buf/sync_file.c
+++ b/drivers/dma-buf/sync_file.c
@@ -44,7 +44,7 @@ int sync_file_validate_type_flags(uint32_t type, uint32_t flags)
 {
 	if (flags)
 		return -EINVAL;
-	if (type != SYNC_FILE_TYPE_FENCE)
+	if (type != SYNC_FILE_TYPE_FENCE && type != SYNC_FILE_TYPE_SEMAPHORE)
 		return -EINVAL;
 	return 0;
 }
@@ -165,6 +165,28 @@ struct sync_file *sync_file_fdget(int fd)
 EXPORT_SYMBOL(sync_file_fdget);
 
 /**
+ * sync_file_get_fence_from_sync_file - get the fence related to the sync_file
+ * @sync_file:		sync_file fd to get the fence from
+ *
+ * Returns a fence that represents all fence in the sync_file. On error NULL is returned.
+ */
+struct dma_fence *sync_file_get_fence_from_sync_file(struct sync_file *sync_file)
+{
+	struct dma_fence *fence;
+
+	if (!rcu_access_pointer(sync_file->fence)) {
+		fput(sync_file->file);
+		return NULL;
+	}
+
+	rcu_read_lock();
+	fence = dma_fence_get_rcu_safe(&sync_file->fence);
+	rcu_read_unlock();
+	return fence;
+}
+EXPORT_SYMBOL(sync_file_get_fence_from_sync_file);
+
+/**
  * sync_file_get_fence - get the fence related to the sync_file fd
  * @fd:		sync_file fd to get the fence from
  *
@@ -180,14 +202,7 @@ struct dma_fence *sync_file_get_fence(int fd)
 	if (!sync_file)
 		return NULL;
 
-	if (!rcu_access_pointer(sync_file->fence)) {
-		fput(sync_file->file);
-		return NULL;
-	}
-
-	rcu_read_lock();
-	fence = dma_fence_get_rcu_safe(&sync_file->fence);
-	rcu_read_unlock();
+	fence = sync_file_get_fence_from_sync_file(sync_file);
 
 	fput(sync_file->file);
 
@@ -202,6 +217,39 @@ sync_file_get_fence_locked(struct sync_file *sync_file)
 					 sync_file_held(sync_file));
 }
 
+/**
+ * sync_file_replace_fence - replace the fence related to the sync_file
+ * @sync_file:	 sync file to replace fence in
+ * @fence: fence to replace with (or NULL for no fence).
+ * Returns previous fence.
+ */
+int sync_file_replace_fence(struct sync_file *sync_file,
+			    struct dma_fence *fence,
+			    struct dma_fence **old_fence)
+{
+	struct dma_fence *ret_fence = NULL;
+
+	/* don't allow replace on fence sync files */
+	if (sync_file->type != SYNC_FILE_TYPE_SEMAPHORE)
+		return -EINVAL;
+
+	if (fence == sync_file->fence)
+		return -EINVAL;
+
+	if (fence)
+		dma_fence_get(fence);
+
+	mutex_lock(&sync_file->lock);
+
+	ret_fence = sync_file_get_fence_locked(sync_file);
+	RCU_INIT_POINTER(sync_file->fence, fence);
+
+	mutex_unlock(&sync_file->lock);
+	*old_fence = ret_fence;
+	return 0;
+}
+EXPORT_SYMBOL(sync_file_replace_fence);
+
 static int sync_file_set_fence(struct sync_file *sync_file,
 			       struct dma_fence **fences, int num_fences)
 {
@@ -406,6 +454,10 @@ static unsigned int sync_file_poll(struct file *file, poll_table *wait)
 	unsigned int ret_val = 0;
 	struct dma_fence *fence;
 
+	/* ban polls on the sync file semaphores */
+	if (!sync_file || sync_file->type != SYNC_FILE_TYPE_FENCE)
+		return POLLERR | POLLNVAL;
+
 	poll_wait(file, &sync_file->wq, wait);
 
 	mutex_lock(&sync_file->lock);
@@ -453,7 +505,8 @@ static long sync_file_ioctl_merge(struct sync_file *sync_file,
 
 	data.name[sizeof(data.name) - 1] = '\0';
 
-	if (sync_file->type != fence2->type) {
+	if (sync_file->type != fence2->type ||
+	    sync_file->type != SYNC_FILE_TYPE_FENCE) {
 		err = -EINVAL;
 		goto err_put_fd;
 	}
diff --git a/include/linux/sync_file.h b/include/linux/sync_file.h
index 4bf661b..14dff25 100644
--- a/include/linux/sync_file.h
+++ b/include/linux/sync_file.h
@@ -61,5 +61,9 @@ int sync_file_validate_type_flags(uint32_t type, uint32_t flags);
 struct sync_file *sync_file_alloc(uint32_t type, uint32_t flags);
 struct sync_file *sync_file_create(struct dma_fence *fence, uint32_t type, uint32_t flags);
 struct dma_fence *sync_file_get_fence(int fd);
+struct dma_fence *sync_file_get_fence_from_sync_file(struct sync_file *sync_file);
 struct sync_file *sync_file_fdget(int fd);
+int sync_file_replace_fence(struct sync_file *sync_file,
+			    struct dma_fence *fence,
+			    struct dma_fence **old_fence);
 #endif /* _LINUX_SYNC_H */
diff --git a/include/uapi/linux/sync_file.h b/include/uapi/linux/sync_file.h
index f439cda..5f266e0 100644
--- a/include/uapi/linux/sync_file.h
+++ b/include/uapi/linux/sync_file.h
@@ -80,6 +80,20 @@ struct sync_file_info {
 #define SYNC_FILE_TYPE_FENCE 0
 
 /**
+ * DOC: SYNC_FILE_TYPE_SEMAPHORE - semaphore sync file object
+ *
+ * This is a sync file that operates like a Vulkan semaphore.
+ * The object should just be imported/exported but not use the
+ * sync file ioctls (except info).
+ * This object can have it's backing fence replaced multiple times.
+ * Each signal operation assigns a backing fence.
+ * Each wait operation waits on the current fence, and removes it.
+ * These operations should happen via driver command submission interfaces.
+ * This is useful for shared vulkan semaphores.
+ */
+#define SYNC_FILE_TYPE_SEMAPHORE 1
+
+/**
  * struct sync_file_type - data returned from sync file type ioctl
  * @type:	sync_file type
  * @flags:	sync_file creation flags
-- 
2.9.3

_______________________________________________
dri-devel mailing list
dri-devel@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/dri-devel

      parent reply	other threads:[~2017-04-12  4:57 UTC|newest]

Thread overview: 14+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2017-04-12  4:57 drm sync objects (vn+1) Dave Airlie
2017-04-12  4:57 ` [PATCH 2/8] sync_file: export some interfaces needed by drm sync objects Dave Airlie
     [not found] ` <20170412045726.13689-1-airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-04-12  4:57   ` [PATCH 1/8] sync_file: add type/flags to sync file object creation Dave Airlie
2017-04-12  4:57   ` [PATCH 3/8] drm: introduce sync objects as sync file objects with no fd (v2.1) Dave Airlie
2017-04-12 13:08     ` Emil Velikov
2017-04-12  4:57   ` [PATCH 4/8] sync_file: add a mutex to protect fence and callback members. (v4) Dave Airlie
2017-04-12  4:57   ` [PATCH 6/8] drm/syncobj: add semaphore support helpers. (v2) Dave Airlie
2017-04-12  4:57   ` [PATCH 7/8] amdgpu/cs: split out fence dependency checking Dave Airlie
2017-04-12  4:57   ` [PATCH 8/8] amdgpu: use sync file for shared semaphores (v3) Dave Airlie
     [not found]     ` <20170412045726.13689-9-airlied-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
2017-04-12  9:55       ` Christian König
     [not found]         ` <43ed515c-7278-3687-022b-9ecd650669fb-ANTagKRnAhcb1SvskN2V4Q@public.gmane.org>
2017-04-12 10:18           ` Chris Wilson
2017-04-12 10:25             ` Christian König
2017-04-12 15:31       ` Alex Deucher
2017-04-12  4:57 ` Dave Airlie [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20170412045726.13689-6-airlied@gmail.com \
    --to=airlied@gmail.com \
    --cc=amd-gfx@lists.freedesktop.org \
    --cc=dri-devel@lists.freedesktop.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox