[RFC] Explicit synchronization for Nouveau

2014-09-26 Thread Lauri Peltonen
 to Nouveau.  It's not a lot of code, because I only use a relatively
small subset of the android sync driver functionality.  Thanks to Maarten's
rewrite, all I need to do is to allow creating a sync_fence from a drm fence in
order to pass it to user space.  I don't need to use sync_pt or sync_timeline,
or fill in sync_timeline_ops.

I can see why the upstream has been reluctant to de-stage the android sync
driver in its current form, since (even though it now builds on struct fence)
it still duplicates some of the drm fence concepts.  I'd like to think that my
patches only use the parts of the android sync driver that genuinely are
missing from the drm fence model: allowing user space to operate on fence
objects that are independent of buffer objects.

The last two patches are mocks that show how (2) and (3) might work out.  I
haven't done any testing with them yet.  Before going any further, I'd like to
get your feedback.  Can you see the benefits of explicit sync as an alternative
synchronization model?  Do you think we could use the android sync_fence for
passing fences between user space?  Or did you have something else in mind for
explicit sync in the drm world?


Thanks,
Lauri


Lauri Peltonen (7):
  android: Support creating sync fence from drm fences
  drm/nouveau: Split nouveau_fence_sync
  drm/nouveau: Add fence fd helpers
  drm/nouveau: Support fence fd's at kickoff
  libdrm: nouveau: Support fence fds
  drm/nouveau: Support marking buffers for explicit sync
  drm/prime: Support explicit fence on export

-- 
1.8.1.5



[RFC PATCH 1/7] android: Support creating sync fence from drm fences

2014-09-26 Thread Lauri Peltonen
Modify sync_fence_create to accept an array of 'struct fence' objects.
This will allow drm drivers to create sync_fence objects and pass sync
fd's between user space with minimal modifications, without ever creating
sync_timeline or sync_pt objects, and without implementing the
sync_timeline_ops interface.

Modify the sync driver debug code to not assume that every 'struct fence'
(that is associated with a 'struct sync_fence') is embedded within a
'struct sync_pt'.

Signed-off-by: Lauri Peltonen 
---
 drivers/staging/android/sw_sync.c|  3 ++-
 drivers/staging/android/sync.c   | 34 ++---
 drivers/staging/android/sync.h   | 11 ++-
 drivers/staging/android/sync_debug.c | 37 +---
 4 files changed, 44 insertions(+), 41 deletions(-)

diff --git a/drivers/staging/android/sw_sync.c 
b/drivers/staging/android/sw_sync.c
index a76db3f..6949812 100644
--- a/drivers/staging/android/sw_sync.c
+++ b/drivers/staging/android/sw_sync.c
@@ -184,7 +184,8 @@ static long sw_sync_ioctl_create_fence(struct 
sw_sync_timeline *obj,
}

data.name[sizeof(data.name) - 1] = '\0';
-   fence = sync_fence_create(data.name, pt);
+   fence = sync_fence_create(data.name,
+ (struct fence *[]){ >base }, 1);
if (fence == NULL) {
sync_pt_free(pt);
err = -ENOMEM;
diff --git a/drivers/staging/android/sync.c b/drivers/staging/android/sync.c
index e7b2e02..1d0d968 100644
--- a/drivers/staging/android/sync.c
+++ b/drivers/staging/android/sync.c
@@ -187,28 +187,32 @@ static void fence_check_cb_func(struct fence *f, struct 
fence_cb *cb)
wake_up_all(>wq);
 }

-/* TODO: implement a create which takes more that one sync_pt */
-struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt)
+struct sync_fence *sync_fence_create(const char *name,
+struct fence **fences, int num_fences)
 {
-   struct sync_fence *fence;
+   struct sync_fence *sync_fence;
+   int size = offsetof(struct sync_fence, cbs[num_fences]);
+   int i;

-   fence = sync_fence_alloc(offsetof(struct sync_fence, cbs[1]), name);
-   if (fence == NULL)
+   sync_fence = sync_fence_alloc(size, name);
+   if (sync_fence == NULL)
return NULL;

-   fence->num_fences = 1;
-   atomic_set(>status, 1);
+   sync_fence->num_fences = num_fences;
+   atomic_set(_fence->status, 0);

-   fence_get(>base);
-   fence->cbs[0].sync_pt = >base;
-   fence->cbs[0].fence = fence;
-   if (fence_add_callback(>base, >cbs[0].cb,
-  fence_check_cb_func))
-   atomic_dec(>status);
+   for (i = 0; i < num_fences; i++) {
+   struct fence *f = fences[i];
+   struct sync_fence_cb *cb = _fence->cbs[i];

-   sync_fence_debug_add(fence);
+   cb->sync_pt = fence_get(f);
+   cb->fence = sync_fence;
+   if (!fence_add_callback(f, >cb, fence_check_cb_func))
+   atomic_inc(_fence->status);
+   }
+   sync_fence_debug_add(sync_fence);

-   return fence;
+   return sync_fence;
 }
 EXPORT_SYMBOL(sync_fence_create);

diff --git a/drivers/staging/android/sync.h b/drivers/staging/android/sync.h
index 66b0f43..b8ad72c 100644
--- a/drivers/staging/android/sync.h
+++ b/drivers/staging/android/sync.h
@@ -246,13 +246,14 @@ void sync_pt_free(struct sync_pt *pt);

 /**
  * sync_fence_create() - creates a sync fence
- * @name:  name of fence to create
- * @pt:sync_pt to add to the fence
+ * @name:  name of the sync fence to create
+ * @fences:fences to add to the sync fence
+ * @num_fences:the number of fences in the @fences array
  *
- * Creates a fence containg @pt.  Once this is called, the fence takes
- * ownership of @pt.
+ * Creates a sync fence from an array of drm fences. Takes refs to @fences.
  */
-struct sync_fence *sync_fence_create(const char *name, struct sync_pt *pt);
+struct sync_fence *sync_fence_create(const char *name,
+struct fence **fences, int num_fences);

 /*
  * API for sync_fence consumers
diff --git a/drivers/staging/android/sync_debug.c 
b/drivers/staging/android/sync_debug.c
index 257fc91..2d8873e 100644
--- a/drivers/staging/android/sync_debug.c
+++ b/drivers/staging/android/sync_debug.c
@@ -81,33 +81,33 @@ static const char *sync_status_str(int status)
return "error";
 }

-static void sync_print_pt(struct seq_file *s, struct sync_pt *pt, bool fence)
+static void sync_print_pt(struct seq_file *s, struct fence *pt, bool fence)
 {
int status = 1;
-   struct sync_timeline *parent = sync_pt_parent(pt);

-   if (fence_is_signaled_locked(>base))
-   status = pt->base.status;
+   

[RFC PATCH 2/7] drm/nouveau: Split nouveau_fence_sync

2014-09-26 Thread Lauri Peltonen
Split nouveau_fence_sync to two functions:

 * nouveau_fence_sync, which only adds a fence wait to the channel
   command stream, and
 * nouveau_bo_sync, which gets the fences from the reservation object
   and passes them to nouveau_fence_sync.

Signed-off-by: Lauri Peltonen 
---
 drm/nouveau_bo.c  | 38 +++-
 drm/nouveau_bo.h  |  2 ++
 drm/nouveau_display.c |  4 ++--
 drm/nouveau_fence.c   | 54 ---
 drm/nouveau_fence.h   |  2 +-
 drm/nouveau_gem.c |  2 +-
 6 files changed, 51 insertions(+), 51 deletions(-)

diff --git a/drm/nouveau_bo.c b/drm/nouveau_bo.c
index 70c3cb5..534734a 100644
--- a/drm/nouveau_bo.c
+++ b/drm/nouveau_bo.c
@@ -463,6 +463,42 @@ nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo)
 }

 int
+nouveau_bo_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool 
exclusive)
+{
+   struct fence *fence;
+   struct reservation_object *resv = nvbo->bo.resv;
+   struct reservation_object_list *fobj;
+   int ret = 0, i;
+
+   if (!exclusive) {
+   ret = reservation_object_reserve_shared(resv);
+
+   if (ret)
+   return ret;
+   }
+
+   fobj = reservation_object_get_list(resv);
+   fence = reservation_object_get_excl(resv);
+
+   if (fence && (!exclusive || !fobj || !fobj->shared_count))
+   return nouveau_fence_sync(fence, chan);
+
+   if (!exclusive || !fobj)
+   return ret;
+
+   for (i = 0; i < fobj->shared_count && !ret; ++i) {
+   fence = rcu_dereference_protected(fobj->shared[i],
+   reservation_object_held(resv));
+   ret = nouveau_fence_sync(fence, chan);
+
+   if (ret)
+   break;
+   }
+
+   return ret;
+}
+
+int
 nouveau_bo_validate(struct nouveau_bo *nvbo, bool interruptible,
bool no_wait_gpu)
 {
@@ -1070,7 +1106,7 @@ nouveau_bo_move_m2mf(struct ttm_buffer_object *bo, int 
evict, bool intr,
}

mutex_lock_nested(>mutex, SINGLE_DEPTH_NESTING);
-   ret = nouveau_fence_sync(nouveau_bo(bo), chan, true);
+   ret = nouveau_bo_sync(nouveau_bo(bo), chan, true);
if (ret == 0) {
ret = drm->ttm.move(chan, bo, >mem, new_mem);
if (ret == 0) {
diff --git a/drm/nouveau_bo.h b/drm/nouveau_bo.h
index 8ab877b..f97bc26 100644
--- a/drm/nouveau_bo.h
+++ b/drm/nouveau_bo.h
@@ -84,6 +84,8 @@ int  nouveau_bo_validate(struct nouveau_bo *, bool 
interruptible,
 bool no_wait_gpu);
 void nouveau_bo_sync_for_device(struct nouveau_bo *nvbo);
 void nouveau_bo_sync_for_cpu(struct nouveau_bo *nvbo);
+int  nouveau_bo_sync(struct nouveau_bo *, struct nouveau_channel *,
+bool exclusive);

 struct nouveau_vma *
 nouveau_bo_vma_find(struct nouveau_bo *, struct nouveau_vm *);
diff --git a/drm/nouveau_display.c b/drm/nouveau_display.c
index 6d0a3cd..41b130c 100644
--- a/drm/nouveau_display.c
+++ b/drm/nouveau_display.c
@@ -658,7 +658,7 @@ nouveau_page_flip_emit(struct nouveau_channel *chan,
spin_unlock_irqrestore(>event_lock, flags);

/* Synchronize with the old framebuffer */
-   ret = nouveau_fence_sync(old_bo, chan, false);
+   ret = nouveau_bo_sync(old_bo, chan, false);
if (ret)
goto fail;

@@ -722,7 +722,7 @@ nouveau_crtc_page_flip(struct drm_crtc *crtc, struct 
drm_framebuffer *fb,
goto fail_unpin;

/* synchronise rendering channel with the kernel's channel */
-   ret = nouveau_fence_sync(new_bo, chan, false);
+   ret = nouveau_bo_sync(new_bo, chan, false);
if (ret) {
ttm_bo_unreserve(_bo->bo);
goto fail_unpin;
diff --git a/drm/nouveau_fence.c b/drm/nouveau_fence.c
index decfe6c..39b5436 100644
--- a/drm/nouveau_fence.c
+++ b/drm/nouveau_fence.c
@@ -342,57 +342,19 @@ nouveau_fence_wait(struct nouveau_fence *fence, bool 
lazy, bool intr)
 }

 int
-nouveau_fence_sync(struct nouveau_bo *nvbo, struct nouveau_channel *chan, bool 
exclusive)
+nouveau_fence_sync(struct fence *fence, struct nouveau_channel *chan)
 {
struct nouveau_fence_chan *fctx = chan->fence;
-   struct fence *fence;
-   struct reservation_object *resv = nvbo->bo.resv;
-   struct reservation_object_list *fobj;
+   struct nouveau_channel *prev = NULL;
struct nouveau_fence *f;
-   int ret = 0, i;
-
-   if (!exclusive) {
-   ret = reservation_object_reserve_shared(resv);
-
-   if (ret)
-   return ret;
-   }
-
-   fobj = reservation_object_get_list(resv);
-   fence = reservation_object_get_excl(resv);
-
-   if (fence && (!exclusive || !fobj || !fobj->shared_count)) {
-   struct nouveau_channel *prev = NULL;
-
-   f

[RFC PATCH 3/7] drm/nouveau: Add fence fd helpers

2014-09-26 Thread Lauri Peltonen
Add nouveau_fence_install, which installs a drm fence as a file
descriptor that can be returned to user space.

Add nouveau_fence_sync_fd, which pushes semaphore wait commands for
each Nouveau fence contained within the sync fd.  If the sync fd
contains non-Nouveau fences, those are waited on the CPU.

Add missing fence_value_str and timeline_value_str callbacks to
nouveau fence_ops.

Signed-off-by: Lauri Peltonen 
---
 drm/nouveau_fence.c | 71 -
 drm/nouveau_fence.h |  2 ++
 2 files changed, 72 insertions(+), 1 deletion(-)

diff --git a/drm/nouveau_fence.c b/drm/nouveau_fence.c
index 39b5436..e67d467 100644
--- a/drm/nouveau_fence.c
+++ b/drm/nouveau_fence.c
@@ -37,6 +37,8 @@
 #include "nouveau_dma.h"
 #include "nouveau_fence.h"

+#include "../drivers/staging/android/sync.h"
+
 static const struct fence_ops nouveau_fence_ops_uevent;
 static const struct fence_ops nouveau_fence_ops_legacy;

@@ -471,11 +473,78 @@ static bool nouveau_fence_enable_signaling(struct fence 
*f)
return ret;
 }

+static void nouveau_fence_timeline_value_str(struct fence *fence,
+char *str, int size)
+{
+   struct nouveau_fence *f = from_fence(fence);
+   struct nouveau_fence_chan *fctx = nouveau_fctx(f);
+   u32 cur;
+
+   cur = f->channel ? fctx->read(f->channel) : 0;
+   snprintf(str, size, "%d", cur);
+}
+
+static void
+nouveau_fence_value_str(struct fence *fence, char *str, int size)
+{
+   snprintf(str, size, "%d", fence->seqno);
+}
+
 static const struct fence_ops nouveau_fence_ops_uevent = {
.get_driver_name = nouveau_fence_get_get_driver_name,
.get_timeline_name = nouveau_fence_get_timeline_name,
.enable_signaling = nouveau_fence_enable_signaling,
.signaled = nouveau_fence_is_signaled,
.wait = fence_default_wait,
-   .release = NULL
+   .release = NULL,
+   .fence_value_str = nouveau_fence_value_str,
+   .timeline_value_str = nouveau_fence_timeline_value_str,
 };
+
+int
+nouveau_fence_install(struct fence *fence, const char *name, int *fd_out)
+{
+#ifdef CONFIG_SYNC
+   struct sync_fence *f;
+   int fd;
+
+   f = sync_fence_create(name, , 1);
+   if (!f)
+   return -ENOMEM;
+
+   fd = get_unused_fd();
+   if (fd < 0) {
+   sync_fence_put(f);
+   return fd;
+   }
+   sync_fence_install(f, fd);
+   *fd_out = fd;
+   return 0;
+#else
+   return -ENODEV;
+#endif
+}
+
+int
+nouveau_fence_sync_fd(int fence_fd, struct nouveau_channel *chan)
+{
+#ifdef CONFIG_SYNC
+   int i, ret = 0;
+   struct sync_fence *fence;
+
+   fence = sync_fence_fdget(fence_fd);
+   if (!fence)
+   return -EINVAL;
+
+   for (i = 0; i < fence->num_fences; ++i) {
+   struct fence *pt = fence->cbs[i].sync_pt;
+
+   ret = nouveau_fence_sync(pt, chan);
+   if (ret)
+   break;
+   }
+   sync_fence_put(fence);
+   return ret;
+#else
+   return -ENODEV;
+#endif
+}
diff --git a/drm/nouveau_fence.h b/drm/nouveau_fence.h
index 8b70166..76342ea 100644
--- a/drm/nouveau_fence.h
+++ b/drm/nouveau_fence.h
@@ -27,6 +27,8 @@ bool nouveau_fence_done(struct nouveau_fence *);
 void nouveau_fence_work(struct fence *, void (*)(void *), void *);
 int  nouveau_fence_wait(struct nouveau_fence *, bool lazy, bool intr);
 int  nouveau_fence_sync(struct fence *, struct nouveau_channel *);
+int  nouveau_fence_sync_fd(int, struct nouveau_channel *);
+int  nouveau_fence_install(struct fence *, const char *name, int *);

 struct nouveau_fence_chan {
spinlock_t lock;
-- 
1.8.1.5



[RFC PATCH 4/7] drm/nouveau: Support fence fd's at kickoff

2014-09-26 Thread Lauri Peltonen
Add a new NOUVEAU_GEM_PUSHBUF_2 ioctl that accepts and emits a sync
fence fd from/to user space if the user space requests it by passing
corresponding flags.

Signed-off-by: Lauri Peltonen 
---
 drm/nouveau_drm.c  |  1 +
 drm/nouveau_gem.c  | 46 ++
 drm/nouveau_gem.h  |  2 ++
 drm/uapi/drm/nouveau_drm.h | 11 +++
 4 files changed, 56 insertions(+), 4 deletions(-)

diff --git a/drm/nouveau_drm.c b/drm/nouveau_drm.c
index 244d78f..74d5ac6 100644
--- a/drm/nouveau_drm.c
+++ b/drm/nouveau_drm.c
@@ -812,6 +812,7 @@ nouveau_ioctls[] = {
DRM_IOCTL_DEF_DRV(NOUVEAU_GPUOBJ_FREE, nouveau_abi16_ioctl_gpuobj_free, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_NEW, nouveau_gem_ioctl_new, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF, nouveau_gem_ioctl_pushbuf, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
+   DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_PUSHBUF_2, nouveau_gem_ioctl_pushbuf_2, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
diff --git a/drm/nouveau_gem.c b/drm/nouveau_gem.c
index 78398d4..ee5782c 100644
--- a/drm/nouveau_gem.c
+++ b/drm/nouveau_gem.c
@@ -636,15 +636,16 @@ nouveau_gem_pushbuf_reloc_apply(struct nouveau_cli *cli,
return ret;
 }

-int
-nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
- struct drm_file *file_priv)
+static int
+__nouveau_gem_ioctl_pushbuf(struct drm_device *dev,
+   struct drm_nouveau_gem_pushbuf *req,
+   struct drm_nouveau_gem_pushbuf_2 *req_2,
+   struct drm_file *file_priv)
 {
struct nouveau_abi16 *abi16 = nouveau_abi16_get(file_priv, dev);
struct nouveau_cli *cli = nouveau_cli(file_priv);
struct nouveau_abi16_chan *temp;
struct nouveau_drm *drm = nouveau_drm(dev);
-   struct drm_nouveau_gem_pushbuf *req = data;
struct drm_nouveau_gem_pushbuf_push *push;
struct drm_nouveau_gem_pushbuf_bo *bo;
struct nouveau_channel *chan = NULL;
@@ -725,6 +726,14 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void 
*data,
}
}

+   if (req_2 && (req_2->flags & NOUVEAU_GEM_PUSHBUF_2_FENCE_WAIT)) {
+   ret = nouveau_fence_sync_fd(req_2->fence, chan);
+   if (ret) {
+   NV_PRINTK(error, cli, "fence wait: %d\n", ret);
+   goto out;
+   }
+   }
+
if (chan->dma.ib_max) {
ret = nouveau_dma_wait(chan, req->nr_push + 1, 16);
if (ret) {
@@ -800,6 +809,16 @@ nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void 
*data,
goto out;
}

+   if (req_2 && (req_2->flags & NOUVEAU_GEM_PUSHBUF_2_FENCE_EMIT)) {
+   ret = nouveau_fence_install(>base, "nv-pushbuf",
+   _2->fence);
+   if (ret) {
+   NV_PRINTK(error, cli, "fence install: %d\n", ret);
+   WIND_RING(chan);
+   goto out;
+   }
+   }
+
 out:
validate_fini(, fence, bo);
nouveau_fence_unref();
@@ -825,6 +844,25 @@ out_next:
return nouveau_abi16_put(abi16, ret);
 }

+int
+nouveau_gem_ioctl_pushbuf_2(struct drm_device *dev, void *data,
+   struct drm_file *file_priv)
+{
+   struct drm_nouveau_gem_pushbuf_2 *req_2 = data;
+   struct drm_nouveau_gem_pushbuf *req = _2->base;
+
+   return __nouveau_gem_ioctl_pushbuf(dev, req, req_2, file_priv);
+}
+
+int
+nouveau_gem_ioctl_pushbuf(struct drm_device *dev, void *data,
+ struct drm_file *file_priv)
+{
+   struct drm_nouveau_gem_pushbuf *req = data;
+
+   return __nouveau_gem_ioctl_pushbuf(dev, req, NULL, file_priv);
+}
+
 static inline uint32_t
 domain_to_ttm(struct nouveau_bo *nvbo, uint32_t domain)
 {
diff --git a/drm/nouveau_gem.h b/drm/nouveau_gem.h
index ddab762..7454dea 100644
--- a/drm/nouveau_gem.h
+++ b/drm/nouveau_gem.h
@@ -27,6 +27,8 @@ extern int nouveau_gem_ioctl_new(struct drm_device *, void *,
 struct drm_file *);
 extern int nouveau_gem_ioctl_pushbuf(struct drm_device *, void *,
 struct drm_file *);
+extern int nouveau_gem_ioctl_pushbuf_2(struct drm_device *, void *,
+  struct drm_file *);
 extern int nouveau_gem_ioctl_cpu_prep(struct drm_device *,

[RFC PATCH 5/7] libdrm: nouveau: Support fence fds

2014-09-26 Thread Lauri Peltonen
Add a new nouveau_pushbuf_kick_fence function that takes and emits
a sync fence fd.  The fence fd can be waited on, or merged with
other fence fd's, or passed back to kernel as a prerquisite for a
subsequent hw operation.

Signed-off-by: Lauri Peltonen 
---
 include/drm/nouveau_drm.h | 10 +
 nouveau/nouveau.h |  2 +
 nouveau/pushbuf.c | 93 ---
 3 files changed, 76 insertions(+), 29 deletions(-)

diff --git a/include/drm/nouveau_drm.h b/include/drm/nouveau_drm.h
index b18cad0..3e40210 100644
--- a/include/drm/nouveau_drm.h
+++ b/include/drm/nouveau_drm.h
@@ -171,6 +171,15 @@ struct drm_nouveau_gem_pushbuf {
uint64_t gart_available;
 };

+#define NOUVEAU_GEM_PUSHBUF_2_FENCE_WAIT 0x0001
+#define NOUVEAU_GEM_PUSHBUF_2_FENCE_EMIT 0x0002
+struct drm_nouveau_gem_pushbuf_2 {
+   struct drm_nouveau_gem_pushbuf base;
+   uint32_t flags;
+   int32_t  fence;
+   uint64_t reserved;
+};
+
 #define NOUVEAU_GEM_CPU_PREP_NOWAIT  0x0001
 #define NOUVEAU_GEM_CPU_PREP_NOBLOCK 0x0002
 #define NOUVEAU_GEM_CPU_PREP_WRITE   0x0004
@@ -204,5 +213,6 @@ struct drm_nouveau_sarea {
 #define DRM_NOUVEAU_GEM_CPU_PREP   0x42
 #define DRM_NOUVEAU_GEM_CPU_FINI   0x43
 #define DRM_NOUVEAU_GEM_INFO   0x44
+#define DRM_NOUVEAU_GEM_PUSHBUF_2  0x45

 #endif /* __NOUVEAU_DRM_H__ */
diff --git a/nouveau/nouveau.h b/nouveau/nouveau.h
index a55e2b0..281420f 100644
--- a/nouveau/nouveau.h
+++ b/nouveau/nouveau.h
@@ -225,6 +225,8 @@ void nouveau_pushbuf_reloc(struct nouveau_pushbuf *, struct 
nouveau_bo *,
 int  nouveau_pushbuf_validate(struct nouveau_pushbuf *);
 uint32_t nouveau_pushbuf_refd(struct nouveau_pushbuf *, struct nouveau_bo *);
 int  nouveau_pushbuf_kick(struct nouveau_pushbuf *, struct nouveau_object 
*channel);
+int  nouveau_pushbuf_kick_fence(struct nouveau_pushbuf *,
+   struct nouveau_object *channel, int *fence);
 struct nouveau_bufctx *
 nouveau_pushbuf_bufctx(struct nouveau_pushbuf *, struct nouveau_bufctx *);

diff --git a/nouveau/pushbuf.c b/nouveau/pushbuf.c
index 6e703a4..8667d05 100644
--- a/nouveau/pushbuf.c
+++ b/nouveau/pushbuf.c
@@ -33,6 +33,7 @@
 #include 
 #include 
 #include 
+#include 

 #include 
 #include 
@@ -77,7 +78,7 @@ nouveau_pushbuf(struct nouveau_pushbuf *push)
 }

 static int pushbuf_validate(struct nouveau_pushbuf *, bool);
-static int pushbuf_flush(struct nouveau_pushbuf *);
+static int pushbuf_flush(struct nouveau_pushbuf *, int *);

 static bool
 pushbuf_kref_fits(struct nouveau_pushbuf *push, struct nouveau_bo *bo,
@@ -172,7 +173,7 @@ pushbuf_kref(struct nouveau_pushbuf *push, struct 
nouveau_bo *bo,
 */
fpush = cli_push_get(push->client, bo);
if (fpush && fpush != push)
-   pushbuf_flush(fpush);
+   pushbuf_flush(fpush, NULL);

kref = cli_kref_get(push->client, bo);
if (kref) {
@@ -305,18 +306,21 @@ pushbuf_dump(struct nouveau_pushbuf_krec *krec, int 
krec_id, int chid)
 }

 static int
-pushbuf_submit(struct nouveau_pushbuf *push, struct nouveau_object *chan)
+pushbuf_submit(struct nouveau_pushbuf *push, struct nouveau_object *chan,
+  int *fence)
 {
struct nouveau_pushbuf_priv *nvpb = nouveau_pushbuf(push);
struct nouveau_pushbuf_krec *krec = nvpb->list;
struct nouveau_device *dev = push->client->device;
struct drm_nouveau_gem_pushbuf_bo_presumed *info;
struct drm_nouveau_gem_pushbuf_bo *kref;
-   struct drm_nouveau_gem_pushbuf req;
+   struct drm_nouveau_gem_pushbuf_2 req_2;
+   struct drm_nouveau_gem_pushbuf *req = _2.base;
struct nouveau_fifo *fifo = chan->data;
struct nouveau_bo *bo;
int krec_id = 0;
int ret = 0, i;
+   int fence_out = -1;

if (chan->oclass != NOUVEAU_FIFO_CHANNEL_CLASS)
return -EINVAL;
@@ -326,30 +330,42 @@ pushbuf_submit(struct nouveau_pushbuf *push, struct 
nouveau_object *chan)

nouveau_pushbuf_data(push, NULL, 0, 0);

+   /* TODO: If fence is requested, force kickoff. */
while (krec && krec->nr_push) {
-   req.channel = fifo->channel;
-   req.nr_buffers = krec->nr_buffer;
-   req.buffers = (uint64_t)(unsigned long)krec->buffer;
-   req.nr_relocs = krec->nr_reloc;
-   req.nr_push = krec->nr_push;
-   req.relocs = (uint64_t)(unsigned long)krec->reloc;
-   req.push = (uint64_t)(unsigned long)krec->push;
-   req.suffix0 = nvpb->suffix0;
-   req.suffix1 = nvpb->suffix1;
-   req.vram_available = 0; /* for valgrind */
-   req.gart_available = 0;
+   req->chan

[RFC PATCH 6/7] drm/nouveau: Support marking buffers for explicit sync

2014-09-26 Thread Lauri Peltonen
Do not attach fences automatically to buffers that are marked for
explicit synchronization.

Signed-off-by: Lauri Peltonen 
---
 drm/nouveau_bo.c   |  8 
 drm/nouveau_bo.h   |  4 ++--
 drm/nouveau_drm.c  |  1 +
 drm/nouveau_gem.c  | 47 +++---
 drm/nouveau_gem.h  |  6 --
 drm/nouveau_ttm.c  |  8 
 drm/nv50_display.c |  2 +-
 drm/uapi/drm/nouveau_drm.h |  5 -
 8 files changed, 60 insertions(+), 21 deletions(-)

diff --git a/drm/nouveau_bo.c b/drm/nouveau_bo.c
index 534734a..68b7bdd 100644
--- a/drm/nouveau_bo.c
+++ b/drm/nouveau_bo.c
@@ -180,7 +180,7 @@ nouveau_bo_fixup_align(struct nouveau_bo *nvbo, u32 flags,

 int
 nouveau_bo_new(struct drm_device *dev, int size, int align,
-  uint32_t flags, uint32_t tile_mode, uint32_t tile_flags,
+  uint32_t flags, uint32_t tile_mode, uint32_t bo_flags,
   struct sg_table *sg,
   struct nouveau_bo **pnvbo)
 {
@@ -211,7 +211,7 @@ nouveau_bo_new(struct drm_device *dev, int size, int align,
INIT_LIST_HEAD(>entry);
INIT_LIST_HEAD(>vma_list);
nvbo->tile_mode = tile_mode;
-   nvbo->tile_flags = tile_flags;
+   nvbo->bo_flags = bo_flags;
nvbo->bo.bdev = >ttm.bdev;

if (!nv_device_is_cpu_coherent(nvkm_device(>device)))
@@ -272,7 +272,7 @@ set_placement_range(struct nouveau_bo *nvbo, uint32_t type)
 * speed up when alpha-blending and depth-test are enabled
 * at the same time.
 */
-   if (nvbo->tile_flags & NOUVEAU_GEM_TILE_ZETA) {
+   if (nvbo->bo_flags & NOUVEAU_GEM_TILE_ZETA) {
fpfn = vram_pages / 2;
lpfn = ~0;
} else {
@@ -1291,7 +1291,7 @@ nouveau_bo_vm_bind(struct ttm_buffer_object *bo, struct 
ttm_mem_reg *new_mem,
if (drm->device.info.family >= NV_DEVICE_INFO_V0_CELSIUS) {
*new_tile = nv10_bo_set_tiling(dev, offset, new_mem->size,
nvbo->tile_mode,
-   nvbo->tile_flags);
+   nvbo->bo_flags);
}

return 0;
diff --git a/drm/nouveau_bo.h b/drm/nouveau_bo.h
index f97bc26..ff1edba 100644
--- a/drm/nouveau_bo.h
+++ b/drm/nouveau_bo.h
@@ -25,7 +25,7 @@ struct nouveau_bo {
unsigned page_shift;

u32 tile_mode;
-   u32 tile_flags;
+   u32 bo_flags;
struct nouveau_drm_tile *tile;

/* Only valid if allocated via nouveau_gem_new() and iff you hold a
@@ -68,7 +68,7 @@ extern struct ttm_bo_driver nouveau_bo_driver;

 void nouveau_bo_move_init(struct nouveau_drm *);
 int  nouveau_bo_new(struct drm_device *, int size, int align, u32 flags,
-   u32 tile_mode, u32 tile_flags, struct sg_table *sg,
+   u32 tile_mode, u32 bo_flags, struct sg_table *sg,
struct nouveau_bo **);
 int  nouveau_bo_pin(struct nouveau_bo *, u32 flags);
 int  nouveau_bo_unpin(struct nouveau_bo *);
diff --git a/drm/nouveau_drm.c b/drm/nouveau_drm.c
index 74d5ac6..3d84f3a 100644
--- a/drm/nouveau_drm.c
+++ b/drm/nouveau_drm.c
@@ -816,6 +816,7 @@ nouveau_ioctls[] = {
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_PREP, nouveau_gem_ioctl_cpu_prep, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_CPU_FINI, nouveau_gem_ioctl_cpu_fini, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_INFO, nouveau_gem_ioctl_info, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
+   DRM_IOCTL_DEF_DRV(NOUVEAU_GEM_SET_INFO, nouveau_gem_ioctl_set_info, 
DRM_UNLOCKED|DRM_AUTH|DRM_RENDER_ALLOW),
 };

 long
diff --git a/drm/nouveau_gem.c b/drm/nouveau_gem.c
index ee5782c..bb19507 100644
--- a/drm/nouveau_gem.c
+++ b/drm/nouveau_gem.c
@@ -149,7 +149,7 @@ nouveau_gem_object_close(struct drm_gem_object *gem, struct 
drm_file *file_priv)

 int
 nouveau_gem_new(struct drm_device *dev, int size, int align, uint32_t domain,
-   uint32_t tile_mode, uint32_t tile_flags,
+   uint32_t tile_mode, uint32_t bo_flags,
struct nouveau_bo **pnvbo)
 {
struct nouveau_drm *drm = nouveau_drm(dev);
@@ -165,7 +165,7 @@ nouveau_gem_new(struct drm_device *dev, int size, int 
align, uint32_t domain,
flags |= TTM_PL_FLAG_SYSTEM;

ret = nouveau_bo_new(dev, size, align, flags, tile_mode,
-tile_flags, NULL, pnvbo);
+bo_flags, NULL, pnvbo);
if (ret)
return ret;
nvbo = *pnvbo;
@@ -216,7 +216,21 @@ nouveau_gem_info(struct drm_file *file_priv, struct 
drm_gem_object *gem,
rep->size = nvbo->bo.mem.num_pages << PAGE_SHIFT;
rep->map_handle = drm_vma_node_offset_addr(>bo.vma_node)

[RFC PATCH 7/7] drm/prime: Support explicit fence on export

2014-09-26 Thread Lauri Peltonen
Allow user space to provide an explicit sync fence fd when exporting
a dma-buf from gem handle.  The fence will be stored as the explicit
fence to the reservation object.

Signed-off-by: Lauri Peltonen 
---
 drivers/gpu/drm/drm_prime.c | 41 +
 include/uapi/drm/drm.h  |  9 -
 2 files changed, 41 insertions(+), 9 deletions(-)

diff --git a/drivers/gpu/drm/drm_prime.c b/drivers/gpu/drm/drm_prime.c
index 2807a77..c69df2e 100644
--- a/drivers/gpu/drm/drm_prime.c
+++ b/drivers/gpu/drm/drm_prime.c
@@ -28,8 +28,10 @@

 #include 
 #include 
+#include 
 #include 
 #include "drm_internal.h"
+#include "../../staging/android/sync.h"

 /*
  * DMA-BUF/GEM Object references and lifetime overview:
@@ -329,7 +331,7 @@ static const struct dma_buf_ops drm_gem_prime_dmabuf_ops =  
{
  * drm_gem_prime_export - helper library implemention of the export callback
  * @dev: drm_device to export from
  * @obj: GEM object to export
- * @flags: flags like DRM_CLOEXEC
+ * @flags: flags like DRM_CLOEXEC or DRM_SYNC_FD
  *
  * This is the implementation of the gem_prime_export functions for GEM drivers
  * using the PRIME helpers.
@@ -385,7 +387,7 @@ static struct dma_buf *export_and_register_object(struct 
drm_device *dev,
  * @dev: dev to export the buffer from
  * @file_priv: drm file-private structure
  * @handle: buffer handle to export
- * @flags: flags like DRM_CLOEXEC
+ * @flags: flags like DRM_CLOEXEC or DRM_SYNC_FD
  * @prime_fd: pointer to storage for the fd id of the create dma-buf
  *
  * This is the PRIME export function which must be used mandatorily by GEM
@@ -401,6 +403,24 @@ int drm_gem_prime_handle_to_fd(struct drm_device *dev,
struct drm_gem_object *obj;
int ret = 0;
struct dma_buf *dmabuf;
+   struct fence *fence = NULL;
+
+   if (flags & DRM_SYNC_FD) {
+#ifdef CONFIG_SYNC
+   struct sync_fence *sf = sync_fence_fdget(*prime_fd);
+   if (!sf)
+   return -ENOENT;
+   if (sf->num_fences != 1) {
+   sync_fence_put(sf);
+   return -EINVAL;
+   }
+   fence = fence_get(sf->cbs[0].sync_pt);
+   sync_fence_put(sf);
+   flags &= ~DRM_SYNC_FD;
+#else
+   return -ENODEV;
+#endif
+   }

mutex_lock(_priv->prime.lock);
obj = drm_gem_object_lookup(dev, file_priv, handle);
@@ -453,6 +473,14 @@ out_have_obj:
goto fail_put_dmabuf;

 out_have_handle:
+   if (fence) {
+   if (!dmabuf->resv) {
+   ret = -ENODEV;
+   goto fail_put_dmabuf;
+   }
+   reservation_object_add_excl_fence(dmabuf->resv, fence);
+   }
+
ret = dma_buf_fd(dmabuf, flags);
/*
 * We must _not_ remove the buffer from the handle cache since the newly
@@ -475,6 +503,7 @@ out:
drm_gem_object_unreference_unlocked(obj);
 out_unlock:
mutex_unlock(_priv->prime.lock);
+   fence_put(fence);

return ret;
 }
@@ -624,7 +653,6 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, 
void *data,
 struct drm_file *file_priv)
 {
struct drm_prime_handle *args = data;
-   uint32_t flags;

if (!drm_core_check_feature(dev, DRIVER_PRIME))
return -EINVAL;
@@ -633,14 +661,11 @@ int drm_prime_handle_to_fd_ioctl(struct drm_device *dev, 
void *data,
return -ENOSYS;

/* check flags are valid */
-   if (args->flags & ~DRM_CLOEXEC)
+   if (args->flags & ~(DRM_CLOEXEC | DRM_SYNC_FD))
return -EINVAL;

-   /* we only want to pass DRM_CLOEXEC which is == O_CLOEXEC */
-   flags = args->flags & DRM_CLOEXEC;
-
return dev->driver->prime_handle_to_fd(dev, file_priv,
-   args->handle, flags, >fd);
+   args->handle, args->flags, >fd);
 }

 int drm_prime_fd_to_handle_ioctl(struct drm_device *dev, void *data,
diff --git a/include/uapi/drm/drm.h b/include/uapi/drm/drm.h
index b0b8556..a11b893 100644
--- a/include/uapi/drm/drm.h
+++ b/include/uapi/drm/drm.h
@@ -661,13 +661,20 @@ struct drm_set_client_cap {
 };

 #define DRM_CLOEXEC O_CLOEXEC
+#define DRM_SYNC_FD O_DSYNC
 struct drm_prime_handle {
__u32 handle;

/** Flags.. only applicable for handle->fd */
__u32 flags;

-   /** Returned dmabuf file descriptor */
+   /**
+* DRM_IOCTL_PRIME_FD_TO_HANDLE:
+*   in: dma-buf fd
+* DRM_IOCTL_PRIME_HANDLE_TO_FD:
+*   in: sync fence fd if DRM_SYNC_FD flag is passed
+*   out: dma-buf fd
+*/
__s32 fd;
 };

-- 
1.8.1.5



[RFC] Explicit synchronization for Nouveau

2014-10-01 Thread Lauri Peltonen
Thanks Daniel for your input!

On Mon, Sep 29, 2014 at 09:43:02AM +0200, Daniel Vetter wrote:
> On Fri, Sep 26, 2014 at 01:00:05PM +0300, Lauri Peltonen wrote:
> > (2) Stop automatically storing fences to the buffers that user space wants 
> > to
> > synchronize explicitly.
> 
> The problem with this approach is that you then need hw faulting to make
> sure the memory is there. Implicit fences aren't just used for syncing,
> but also to make sure that the gpu still has access to the buffer as long
> as it needs it. So you need at least a non-exclusive fence attached for
> each command submission.
> 
> Of course on Android you don't have swap (would kill the puny mmc within
> seconds) and you don't care for letting userspace pin most of memory for
> gfx. So you'll get away with no fences at all. But for upstream I don't
> see a good solution unfortunately. Ideas very much welcome.
> 
> > (3) Allow user space to attach an explicit fence to dma-buf when exporting 
> > to
> > another driver that uses implicit sync.
> > 
> > There are still some open issues beyond these.  For example, can we skip
> > acquiring the ww mutex for explicitly synchronized buffers?  I think we 
> > could
> > eventually, at least on unified memory systems where we don't need to 
> > migrate
> > between heaps (our downstream Tegra GPU driver does not lock any buffers at
> > submit, it just grabs refcounts for hw).  Another quirk is that now Nouveau
> > waits on the buffer fences when closing the gem object to ensure that it
> > doesn't unmap too early.  We need to rework that for explicit sync, but that
> > shouldn't be difficult.
> 
> See above, but you can't avoid to attach fences as long as we still use a
> buffer-object based gfx memory management model. At least afaics. Which
> means you need the ordering guarantees imposed by ww mutexes to ensure
> that the oddball implicit ordered client can't deadlock the kernel's
> memory management code.

Implicit fences attached to individual buffers are one way for residency
management.  Do you think a working set based model could work in the DRM
framework?  For example, something like this:

- Allow user space to create "working set objects" and associate buffers with
  them.  If the user space doesn't want to manage working sets explicitly, it
  could also use an implicit default working set that contains all buffers that
  are mapped to the channel vm (on Android we could always use the default
  working set since we don't need to manage residency).  The working sets are
  initially marked as dirty.
- User space tells which working sets are referenced by each work submission.
  Kernel locks these working sets, pins all buffers in dirty working sets, and
  resets the dirty bits.  After kicking off work, kernel stores the fence to
  the _working sets_, and then releases the locks (if an implicit default
  working set is used, then this would be roughly equivalent to storing a fence
  to channel vm that tells "this is the last hw operation that might have
  touched buffers in this address space").
- If swapping doesn't happen, then we just need to check the working set dirty
  bits at each submit.
- When a buffer is swapped out, all working sets that refer to it need to be
  marked as dirty.
- When a buffer is swapped out or unmapped, we need to wait for the fences from
  all working sets that refer to the buffer.

Initially one might think of working sets as a mere optimization - we now need
to process a few working sets at every submit instead of many individual
buffers.  However, it makes a huge difference because of fences: fences that
are attached to buffers are used for implicitly synchronizing work across
different channels and engines.  They are in the performance critical path, and
we want to carefully manage them (that's the idea of explicit synchronization).
The working set fences, on the other hand, would only be used to guarantee that
we don't swap out or unmap something that the GPU might be accessing.  We never
need to wait for those fences (except when swapping or unmapping), so we can be
conservative without hurting performance.


> Imo de-staging the android syncpt stuff needs to happen first, before drivers
> can use it. Since non-staging stuff really shouldn't depend upon code from
> staging.

Fully agree.  I thought the best way towards that would be to show some driver
code that _would_ use it. :)


> I'm all for adding explicit syncing. Our plans are roughly.  - Add both an in
> and and out fence to execbuf to sync with other rendering and give userspace
> a fence back. Needs to different flags probably.
> 
> - Maybe add an ioctl to dma-bufs to get at the current implicit fences
>   attached to them (both an exclusive and non-exclusive version). This

[RFC PATCH 7/7] drm/prime: Support explicit fence on export

2014-10-01 Thread Lauri Peltonen
On Mon, Sep 29, 2014 at 09:46:48AM +0200, Daniel Vetter wrote:
> On Fri, Sep 26, 2014 at 01:00:12PM +0300, Lauri Peltonen wrote:
> > Allow user space to provide an explicit sync fence fd when exporting
> > a dma-buf from gem handle.  The fence will be stored as the explicit
> > fence to the reservation object.
> > 
> > Signed-off-by: Lauri Peltonen 
> 
> All existing userspace treats dma_bufs as long-lived objects. Well, all
> the userspace that expects implicit syncing, afaik Android shovels lots of
> dma-bufs around all the time (since ion is using them as it's native
> buffer handles).
> 
> So adding an exclusive fence once at export time isn't going to be
> terribly useful.
> 
> So I think a better approach would be to add this as ioctls to the dma-buf
> fd itself. Then you can also add a "give me the fence(s) for this dma_buf"
> ioctl, which is useful for interop the other way round (i.e. implicit ->
> explicit).

Yes, I like this.  I thought that one could support long-lived dma-bufs by
doing a "re-export" when a new fence needs to be attached, but your model is
indeed much nicer!


On Sat, Sep 27, 2014 at 08:49:39AM +0200, Maarten Lankhorst wrote:
> This is never true. A default resv gets allocated, see dma_buf's create
> function.

Ah, ok.  I'll keep that in mind when writing new versions. :-)


Thanks,
Lauri



[RFC] Explicit synchronization for Nouveau

2014-10-02 Thread Lauri Peltonen
+Rom who seems to be presenting about mainlining android sync at linux plumbers


On Wed, Oct 01, 2014 at 05:58:52PM +0200, Maarten Lankhorst wrote:
> You could neuter implicit fences by always attaching the fences as
> shared when explicit syncing is used. This would work correctly with
> eviction, and wouldn't cause any unneeded syncing. :)

Yes, that will probably work!  So, just to reiterate that I understood you and
Daniel correctly:

- de-stage sync_fence and it's user space API (the tedious part)
- add dma-buf ioctls for extracting and attaching explicit fences
- Nouveau specific: allow flagging gem buffers for explicit sync
  - do not check pre-fences from explicitly synced buffers at submit 
  - continue to attach a shared fence after submit so that pinning and
unmapping continue to work

Then working sets and getting rid of locking all buffers individually 
can be dealt with later as an optimization.


On Wed, Oct 01, 2014 at 07:27:21PM +0200, Daniel Vetter wrote:
> On Wed, Oct 01, 2014 at 06:14:16PM +0300, Lauri Peltonen wrote:
> > Implicit fences attached to individual buffers are one way for residency
> > management.  Do you think a working set based model could work in the DRM
> > framework?  For example, something like this:
> > 
> > - Allow user space to create "working set objects" and associate buffers 
> > with
> >   them.  If the user space doesn't want to manage working sets explicitly, 
> > it
> >   could also use an implicit default working set that contains all buffers 
> > that
> >   are mapped to the channel vm (on Android we could always use the default
> >   working set since we don't need to manage residency).  The working sets 
> > are
> >   initially marked as dirty.
> > - User space tells which working sets are referenced by each work 
> > submission.
> >   Kernel locks these working sets, pins all buffers in dirty working sets, 
> > and
> >   resets the dirty bits.  After kicking off work, kernel stores the fence to
> >   the _working sets_, and then releases the locks (if an implicit default
> >   working set is used, then this would be roughly equivalent to storing a 
> > fence
> >   to channel vm that tells "this is the last hw operation that might have
> >   touched buffers in this address space").
> > - If swapping doesn't happen, then we just need to check the working set 
> > dirty
> >   bits at each submit.
> > - When a buffer is swapped out, all working sets that refer to it need to be
> >   marked as dirty.
> > - When a buffer is swapped out or unmapped, we need to wait for the fences 
> > from
> >   all working sets that refer to the buffer.
> > 
> > Initially one might think of working sets as a mere optimization - we now 
> > need
> > to process a few working sets at every submit instead of many individual
> > buffers.  However, it makes a huge difference because of fences: fences that
> > are attached to buffers are used for implicitly synchronizing work across
> > different channels and engines.  They are in the performance critical path, 
> > and
> > we want to carefully manage them (that's the idea of explicit 
> > synchronization).
> > The working set fences, on the other hand, would only be used to guarantee 
> > that
> > we don't swap out or unmap something that the GPU might be accessing.  We 
> > never
> > need to wait for those fences (except when swapping or unmapping), so we 
> > can be
> > conservative without hurting performance.
> 
> Yeah, within the driver (i.e. for private objects which are never exported
> to dma_buf) we can recently do stuff like this. And your above idea is
> roughly one of the things we're tossing around for i915.
> 
> But the cool stuff with drm is that cmd submission is driver-specific, so
> you can just go wild with nouveau. Of course you have to coninvce the
> nouveau guys (and also have open-source users for the new interface).
> 
> For shared buffers I think we should stick with the implicit fences for a
> while simply because I'm not sure whether it's really worth the fuzz. And
> reworking all the drivers and dma-buf for some working sets is a lot of
> fuzz ;-) Like Maarten said you can mostly short-circuit the implicit
> fencing by only attaching shared fences.

Yes, I'll try to do that.


> In case you're curious: The idea is to have a 1:1 association between
> ppgtt address spaces and what you call the working set above, to implement
> the buffer svm model in ocl2. Mostly because we expect that applications
> won't get the more fine-grained buffer list right anyway. And this kind of
> gang-scheduling of working set sizes shoul

[RFC] Explicit synchronization for Nouveau

2014-10-06 Thread Lauri Peltonen
On Thu, Oct 02, 2014 at 10:44:05PM +0200, Daniel Vetter wrote:
> On Thu, Oct 02, 2014 at 05:59:51PM +0300, Lauri Peltonen wrote:
> > Yes, that will probably work!  So, just to reiterate that I understood you 
> > and
> > Daniel correctly:
> > 
> > - de-stage sync_fence and it's user space API (the tedious part)
> > - add dma-buf ioctls for extracting and attaching explicit fences
> > - Nouveau specific: allow flagging gem buffers for explicit sync
> >   - do not check pre-fences from explicitly synced buffers at submit 
> >   - continue to attach a shared fence after submit so that pinning and
> > unmapping continue to work
> 
> - Have explicit in/out fences for the pushbuf ioctl is missing I
>   guess in this step?

Yes, I was missing that. :)


> I also think we need some kind of demonstration vehicle using nouveau to
> satisfy Dave Airlie's open-source userspace requirements for new
> interfaces. Might be good to chat with him to make sure we have that
> covered (and how much needs to be there really).

Agreed.


> Also, the problem is that to actually push android stuff out of staging
> you need a use-case in upstream, which means an open-source gpu driver.
> There's not a lot of companies who have both that and ship android, and
> definitely not the nexus/android lead platforms.
> 
> Display side would be easier since there's a bunch of kms drivers now
> upstream. But given that google decided to go ahead with their own adf
> instead of drm-kms that's also a non-starter.

Hmm..  Maybe we could use TegraDRM on the display side..  That and Nouveau
would already be two upstream drivers that support explicit sync on Tegra K1.

Also, if we bring sync fd's out of staging, one idea would be to add support
for EGL_ANDROID_native_fence_sync in mesa, along with some tests.  That would
demonstrate converting between sync fd's and EGLSync objects.


> Hm, usually we expose such test interfaces through debugfs - that way
> production system won't ever ship with it (since there's too many exploits in
> there, especially with secure boot). But since you need it for validation
> tests (at least for the i915 suite) it should always be there when you need
> it.
> 
> Exposing this as a configurable driver in dev is imo a no-go. But we
> should be able to easily convert this into a few debugfs files, so not too
> much fuzz hopefully.

Good idea!


> > > Aside: Will you be at XDC or linux plumbers? Either would be a perfect
> > > place to discuss plans and ideas - I'll attend both.
> > 
> > I wasn't going to, but let's see.  The former is pretty soon and the latter 
> > is
> > sold out.  At least Andy Ritger from Nvidia is coming to XDC for sure, and 
> > he's
> > been involved in our internal discussions around these topics. So I suggest 
> > you
> > have a chat with him at least!  :)
> 
> I'll definitely have a chat (and some beers) with Andy, been a while I've
> last seen him ;-)

Change of plans, I'll attend XDC, so see you there!  I'll even give a short
talk about explicit sync to get some discussions going. :)


Thanks, 
Lauri