Skip to contentSkip to conversation

drm/radeon: Add support for userspace fence waits

PATCH Superseded Simon Farnsworth 7 participants 13 messages Jan 31, 2012 – Feb 1, 2012

Subsystems (2)

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 drm/radeon: Add support for userspace fence waits

Userspace currently busywaits for fences to complete; on my workload, this busywait consumes 10% of the available CPU time.

Provide an ioctl so that userspace can wait for an EOP interrupt that corresponds to a previous EVENT_WRITE_EOP.

---
I've been working on top of Jerome's tiling patches, so this doesn't apply directly on top of current upstream kernels. I can easily rebase to another version upon request - just point me to a git tree.

My goal is to remove the sched_yield in Mesa's r600_fence_finish given up to date enough kernel; I hope, though, that the interface is clean enough for other users to extend it in the future (e.g. using compute rings).

 drivers/gpu/drm/radeon/radeon.h       |    3 +
 drivers/gpu/drm/radeon/radeon_drv.c   |    3 +-
 drivers/gpu/drm/radeon/radeon_fence.c |    2 +
 drivers/gpu/drm/radeon/radeon_gem.c   |   70 +++++++++++++++++++++++++++++++++
 drivers/gpu/drm/radeon/radeon_kms.c   |    1 +
 include/drm/radeon_drm.h              |   30 ++++++++++++++
 6 files changed, 108 insertions(+), 1 deletions(-)
drivers/gpu/drm/radeon/radeon.h +3 -0
index 2859406..00c187b 100644
@@ -217,6 +217,7 @@ struct radeon_fence_driver {
 	unsigned long			last_jiffies;
 	unsigned long			last_timeout;
 	wait_queue_head_t		queue;
+	wait_queue_head_t		userspace_queue;
 	struct list_head		created;
 	struct list_head		emitted;
 	struct list_head		signaled;
@@ -1348,6 +1349,8 @@ int radeon_gem_set_tiling_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *filp);
 int radeon_gem_get_tiling_ioctl(struct drm_device *dev, void *data,
 				struct drm_file *filp);
+int radeon_gem_wait_user_fence_ioctl(struct drm_device *dev, void *data,
+				     struct drm_file *filp);
 
 /* VRAM scratch page for HDP bug, default vram page */
 struct r600_vram_scratch {
drivers/gpu/drm/radeon/radeon_drv.c +2 -1
index 4ae2e1d..9f82fa9 100644
@@ -56,9 +56,10 @@
  *   2.12.0 - RADEON_CS_KEEP_TILING_FLAGS
  *   2.13.0 - virtual memory support
  *   2.14.0 - add evergreen tiling informations
+ *   2.15.0 - gem_wait_user_fence ioctl
  */
 #define KMS_DRIVER_MAJOR	2
-#define KMS_DRIVER_MINOR	14
+#define KMS_DRIVER_MINOR	15
 #define KMS_DRIVER_PATCHLEVEL	0
 int radeon_driver_load_kms(struct drm_device *dev, unsigned long flags);
 int radeon_driver_unload_kms(struct drm_device *dev);
drivers/gpu/drm/radeon/radeon_fence.c +2 -0
index 64ea3dd..d86bc28 100644
@@ -356,6 +356,7 @@ void radeon_fence_process(struct radeon_device *rdev, int ring)
 	if (wake) {
 		wake_up_all(&rdev->fence_drv[ring].queue);
 	}
+	wake_up_interruptible_all(&rdev->fence_drv[ring].userspace_queue);
 }
 
 int radeon_fence_count_emitted(struct radeon_device *rdev, int ring)
@@ -421,6 +422,7 @@ static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
 	INIT_LIST_HEAD(&rdev->fence_drv[ring].emitted);
 	INIT_LIST_HEAD(&rdev->fence_drv[ring].signaled);
 	init_waitqueue_head(&rdev->fence_drv[ring].queue);
+	init_waitqueue_head(&rdev->fence_drv[ring].userspace_queue);
 	rdev->fence_drv[ring].initialized = false;
 }
 
drivers/gpu/drm/radeon/radeon_gem.c +70 -0

73 lines not on this page · raw message

drivers/gpu/drm/radeon/radeon_kms.c +1 -0
index d335288..0e552cc 100644
@@ -496,5 +496,6 @@ struct drm_ioctl_desc radeon_ioctls_kms[] = {
 	DRM_IOCTL_DEF_DRV(RADEON_GEM_GET_TILING, radeon_gem_get_tiling_ioctl, DRM_AUTH|DRM_UNLOCKED),
 	DRM_IOCTL_DEF_DRV(RADEON_GEM_BUSY, radeon_gem_busy_ioctl, DRM_AUTH|DRM_UNLOCKED),
 	DRM_IOCTL_DEF_DRV(RADEON_GEM_VA, radeon_gem_va_ioctl, DRM_AUTH|DRM_UNLOCKED),
+	DRM_IOCTL_DEF_DRV(RADEON_GEM_WAIT_USER_FENCE, radeon_gem_wait_user_fence_ioctl, DRM_AUTH|DRM_UNLOCKED),
 };
 int radeon_max_kms_ioctl = DRM_ARRAY_SIZE(radeon_ioctls_kms);
include/drm/radeon_drm.h +30 -0
index dd2e9cf..3d4ae93 100644
@@ -510,6 +510,7 @@ typedef struct {
 #define DRM_RADEON_GEM_GET_TILING	0x29
 #define DRM_RADEON_GEM_BUSY		0x2a
 #define DRM_RADEON_GEM_VA		0x2b
+#define DRM_RADEON_GEM_WAIT_USER_FENCE  0x2c
 
 #define DRM_IOCTL_RADEON_CP_INIT    DRM_IOW( DRM_COMMAND_BASE + DRM_RADEON_CP_INIT, drm_radeon_init_t)
 #define DRM_IOCTL_RADEON_CP_START   DRM_IO(  DRM_COMMAND_BASE + DRM_RADEON_CP_START)
@@ -552,6 +553,7 @@ typedef struct {
 #define DRM_IOCTL_RADEON_GEM_GET_TILING	DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_GET_TILING, struct drm_radeon_gem_get_tiling)
 #define DRM_IOCTL_RADEON_GEM_BUSY	DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_BUSY, struct drm_radeon_gem_busy)
 #define DRM_IOCTL_RADEON_GEM_VA		DRM_IOWR(DRM_COMMAND_BASE + DRM_RADEON_GEM_VA, struct drm_radeon_gem_va)
+#define DRM_IOCTL_RADEON_GEM_WAIT_USER_FENCE   DRM_IOW(DRM_COMMAND_BASE + DRM_RADEON_GEM_WAIT_USER_FENCE, struct drm_radeon_gem_wait_user_fence)
 
 typedef struct drm_radeon_init {
 	enum {
@@ -967,4 +969,32 @@ struct drm_radeon_info {
 	uint64_t		value;
 };
 
+/**
+ * struct drm_radeon_gem_wait_user_fence - DRM_RADEON_GEM_WAIT_USER_FENCE ioctl param
+ *
+ * @handle: Handle for the object that the GPU is expected to write
+ * @ring: The ring on which the fence packet was issued
+ * @offset: Offset (in bytes) within that object where the GPU is expected
+ *          to write. Must be DWORD-aligned
+ * @value: The value expected if the GPU has not yet written to this location
+ * @timeout_usec: The maximum time to wait for the GPU, in microseconds
+ *
+ * The DRM_RADEON_GEM_WAIT_USER_FENCE ioctl is meant to allow userspace to
+ * avoid busy-waiting for a EVENT_WRITE_EOP packet to complete (e.g. for
+ * fence sync objects in OpenGL). It expects the EVENT_WRITE_EOP packet to
+ * have requested an interrupt on completion.
+ *
+ * The ioctl will return immediately if the value supplied is not the value
+ * found in the buffer at offset bytes in; otherwise, it will sleep for up
+ * to timeout_usec, waking up when an EVENT_WRITE_EOP packet causes an
+ * interrupt and the value in the buffer might have changed.
+ */
+struct drm_radeon_gem_wait_user_fence {
+	uint32_t                handle;
+	uint32_t                ring;
+	uint64_t                offset;
+	uint32_t                value;
+	uint64_t                timeout_usec;
+};
+
 #endif
Signature · 1 line
1.7.6.4
#1 lore raw
Quoted 7 lines · Simon Farnsworth

Userspace currently busywaits for fences to complete; on my workload, this busywait consumes 10% of the available CPU time.

Provide an ioctl so that userspace can wait for an EOP interrupt that corresponds to a previous EVENT_WRITE_EOP.

Quoted 8 lines

---
I've been working on top of Jerome's tiling patches, so this doesn't apply directly on top of current upstream kernels. I can easily rebase to another version upon request - just point me to a git tree.

My goal is to remove the sched_yield in Mesa's r600_fence_finish given up to date enough kernel; I hope, though, that the interface is clean enough for other users to extend it in the future (e.g. using compute rings).

I'm afraid not: Unless I'm missing something, userspace can't know which ring the kernel submitted the CS to, and the kernel can't guess which ring userspace needs to wait for.

Signature · 6 lines
Earthling Michel Dänzer           |                   http://www.amd.com
Libre software enthusiast         |          Debian, X and DRI developer
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#2 lore raw
Quoted 13 lines · Michel Dänzer
Quoted 12 lines · Simon Farnsworth

Userspace currently busywaits for fences to complete; on my workload, this busywait consumes 10% of the available CPU time.

Provide an ioctl so that userspace can wait for an EOP interrupt that corresponds to a previous EVENT_WRITE_EOP.

---
I've been working on top of Jerome's tiling patches, so this doesn't apply directly on top of current upstream kernels. I can easily rebase to another version upon request - just point me to a git tree.

Quoted 7 lines
Quoted 3 lines

My goal is to remove the sched_yield in Mesa's r600_fence_finish given up to date enough kernel; I hope, though, that the interface is clean enough for other users to extend it in the future (e.g. using compute rings).

I'm afraid not: Unless I'm missing something, userspace can't know which ring the kernel submitted the CS to, and the kernel can't guess which ring userspace needs to wait for.

iirc the plan was to add a return value to cs ioctl and add an ioctl to allow to wait on this return value. ie allowing userspace to wait on specific submited cs.

Cheers,
Jerome

#3 lore raw

2012/1/31 Jerome Glisse <[email protected]>:

Quoted 18 lines · Michel Dänzer
Quoted 17 lines · Simon Farnsworth
Quoted 15 lines

Userspace currently busywaits for fences to complete; on my workload, this busywait consumes 10% of the available CPU time.

Provide an ioctl so that userspace can wait for an EOP interrupt that corresponds to a previous EVENT_WRITE_EOP.

---
I've been working on top of Jerome's tiling patches, so this doesn't apply directly on top of current upstream kernels. I can easily rebase to another version upon request - just point me to a git tree.

My goal is to remove the sched_yield in Mesa's r600_fence_finish given up to date enough kernel; I hope, though, that the interface is clean enough for other users to extend it in the future (e.g. using compute rings).

Quoted 7 lines
Quoted 3 lines

I'm afraid not: Unless I'm missing something, userspace can't know which ring the kernel submitted the CS to, and the kernel can't guess which ring userspace needs to wait for.

iirc the plan was to add a return value to cs ioctl and add an ioctl to allow to wait on this return value. ie allowing userspace to wait on specific submited cs.

You don't need a new API for that, r300g already does that. It adds a dummy relocation and later uses GEM_WAIT_IDLE to wait for it. r600g can be updated to do the same thing without kernel changes (besides, we must support the old kernels as well, so this is a no-brainer).

What would be much more useful is to be able to wait for a fence, which can be in the middle of a CS. Now that's something that would justify changes in the kernel interface.

Marek
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#4 lore raw
Quoted 23 lines · Marek Olšák

2012/1/31 Jerome Glisse <[email protected]>:

Quoted 22 lines · Michel Dänzer
Quoted 20 lines · Simon Farnsworth
Quoted 7 lines

Userspace currently busywaits for fences to complete; on my workload, this busywait consumes 10% of the available CPU time.

Provide an ioctl so that userspace can wait for an EOP interrupt that corresponds to a previous EVENT_WRITE_EOP.

Quoted 8 lines

---
I've been working on top of Jerome's tiling patches, so this doesn't apply directly on top of current upstream kernels. I can easily rebase to another version upon request - just point me to a git tree.

My goal is to remove the sched_yield in Mesa's r600_fence_finish given up to date enough kernel; I hope, though, that the interface is clean enough for other users to extend it in the future (e.g. using compute rings).

I'm afraid not: Unless I'm missing something, userspace can't know which ring the kernel submitted the CS to, and the kernel can't guess which ring userspace needs to wait for.

Quoted 8 lines
Quoted 3 lines

iirc the plan was to add a return value to cs ioctl and add an ioctl to allow to wait on this return value. ie allowing userspace to wait on specific submited cs.

You don't need a new API for that, r300g already does that. It adds a dummy relocation and later uses GEM_WAIT_IDLE to wait for it. r600g can be updated to do the same thing without kernel changes (besides, we must support the old kernels as well, so this is a no-brainer).

One minor problem being that this doesn't support a timeout without spinning. Shouldn't be relevant for Simon's problem though.

Quoted 3 lines

What would be much more useful is to be able to wait for a fence, which can be in the middle of a CS. Now that's something that would justify changes in the kernel interface.

To take advantage of that, one would also need to change Gallium such that it's possible to get a fence without a flush.

Signature · 6 lines
Earthling Michel Dänzer           |                   http://www.amd.com
Libre software enthusiast         |          Debian, X and DRI developer
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel
#5 lore raw

2012/2/1 Michel Dänzer <[email protected]>:

Quoted 37 lines · Marek Olšák
Quoted 23 lines

2012/1/31 Jerome Glisse <[email protected]>:

Quoted 22 lines · Michel Dänzer
Quoted 20 lines · Simon Farnsworth
Quoted 7 lines

Userspace currently busywaits for fences to complete; on my workload, this busywait consumes 10% of the available CPU time.

Provide an ioctl so that userspace can wait for an EOP interrupt that corresponds to a previous EVENT_WRITE_EOP.

Quoted 8 lines

---
I've been working on top of Jerome's tiling patches, so this doesn't apply directly on top of current upstream kernels. I can easily rebase to another version upon request - just point me to a git tree.

My goal is to remove the sched_yield in Mesa's r600_fence_finish given up to date enough kernel; I hope, though, that the interface is clean enough for other users to extend it in the future (e.g. using compute rings).

I'm afraid not: Unless I'm missing something, userspace can't know which ring the kernel submitted the CS to, and the kernel can't guess which ring userspace needs to wait for.

Quoted 8 lines
Quoted 3 lines

iirc the plan was to add a return value to cs ioctl and add an ioctl to allow to wait on this return value. ie allowing userspace to wait on specific submited cs.

You don't need a new API for that, r300g already does that. It adds a dummy relocation and later uses GEM_WAIT_IDLE to wait for it. r600g can be updated to do the same thing without kernel changes (besides, we must support the old kernels as well, so this is a no-brainer).

One minor problem being that this doesn't support a timeout without spinning. Shouldn't be relevant for Simon's problem though.

Quoted 6 lines
Quoted 3 lines

What would be much more useful is to be able to wait for a fence, which can be in the middle of a CS. Now that's something that would justify changes in the kernel interface.

To take advantage of that, one would also need to change Gallium such that it's possible to get a fence without a flush.

There is PIPE_QUERY_GPU_FINISHED, which should be used for that purpose. We already agreed in another discussion that this is the way fences should be done in Gallium. It's just a matter of updating st/mesa to use it.

Marek
_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#6 lore raw
In reply to Simon Farnsworth

Some comments below.

Quoted 32 lines
drivers/gpu/drm/radeon/radeon_gem.c +10 -0
+	struct radeon_device *rdev = dev->dev_private;
+	struct drm_gem_object *gobj;
+	struct radeon_bo *robj;
+	void *buffer_data;
+	uint32_t *fence_data;
+	int r = 0;
+	long timeout;
+	int ring = RADEON_RING_TYPE_GFX_INDEX;
+
+	/* If you're implementing this for other rings, you'll want to

share

drivers/gpu/drm/radeon/radeon_gem.c +21 -0
+	   code with radeon_cs_get_ring in radeon_cs.c */
+	if (args->ring != RADEON_CS_RING_GFX) {
+		return -EINVAL;
+	}
+
+	gobj = drm_gem_object_lookup(dev, filp, args->handle);
+	if (gobj == NULL) {
+		return -ENOENT;
+	}
+	robj = gem_to_radeon_bo(gobj);
+
+	if (gobj->size < args->offset) {
+		r = -EINVAL;
+		goto unreference;
+	}
+
+	r = radeon_bo_reserve(robj, true);
+	if (r) {
+		goto unreference;
+	}
+

drivers/gpu/drm/radeon/radeon_gem.c int radeon_mode_dumb_destroy(struct drm_file *file_priv, · line 579

Quoted 10 lines
drivers/gpu/drm/radeon/radeon_gem.c +10 -0
+	r = radeon_bo_pin(robj, RADEON_GEM_DOMAIN_GTT, NULL);
+	if (r) {
+		goto unreserve;
+	}
+
+	r = radeon_bo_kmap(robj, &buffer_data);
+	if (r) {
+		goto unpin;
+	}
+

Do you need to pin it? I think if you have it reserved and you are in here you shouldn't need to. (unless kmapping requires a pin)/

include/drm/radeon_drm.h struct drm_radeon_info { · line 997

Quoted 13 lines
drivers/gpu/drm/radeon/radeon_gem.c +4 -0
+	radeon_irq_kms_sw_irq_get(rdev, ring);
+
+	fence_data = (uint32_t*)buffer_data;
+	timeout =

an
+ * interrupt and the value in the buffer might have changed.
+ */

include/drm/radeon_drm.h +6 -0
+struct drm_radeon_gem_wait_user_fence {
+	uint32_t                handle;
+	uint32_t                ring;
+	uint64_t                offset;
+	uint32_t                value;
+	uint64_t                timeout_usec;

Align things here, 64 then 64 then 32 32 32 and a 32 pad.

#7 lore raw
Quoted 41 lines · David Airlie

Some comments below.

Quoted 37 lines
drivers/gpu/drm/radeon/radeon_gem.c +10 -0
+	struct radeon_device *rdev = dev->dev_private;
+	struct drm_gem_object *gobj;
+	struct radeon_bo *robj;
+	void *buffer_data;
+	uint32_t *fence_data;
+	int r = 0;
+	long timeout;
+	int ring = RADEON_RING_TYPE_GFX_INDEX;
+
+	/* If you're implementing this for other rings, you'll want to

share

drivers/gpu/drm/radeon/radeon_gem.c +26 -0
+	   code with radeon_cs_get_ring in radeon_cs.c */
+	if (args->ring != RADEON_CS_RING_GFX) {
+		return -EINVAL;
+	}
+
+	gobj = drm_gem_object_lookup(dev, filp, args->handle);
+	if (gobj == NULL) {
+		return -ENOENT;
+	}
+	robj = gem_to_radeon_bo(gobj);
+
+	if (gobj->size < args->offset) {
+		r = -EINVAL;
+		goto unreference;
+	}
+
+	r = radeon_bo_reserve(robj, true);
+	if (r) {
+		goto unreference;
+	}
+
+	r = radeon_bo_pin(robj, RADEON_GEM_DOMAIN_GTT, NULL);
+	if (r) {
+		goto unreserve;
+	}
+
Quoted 8 lines

drivers/gpu/drm/radeon/radeon_gem.c int radeon_mode_dumb_destroy(struct drm_file *file_priv, · line 579

Quoted 5 lines
drivers/gpu/drm/radeon/radeon_gem.c +5 -0
+	r = radeon_bo_kmap(robj, &buffer_data);
+	if (r) {
+		goto unpin;
+	}
+

Do you need to pin it? I think if you have it reserved and you are in here you shouldn't need to. (unless kmapping requires a pin)/

No you don't need to pin, actually it's bad to pin the buffer you might want to wait on might be in vram.

Also you never assume that things could go wrong and that your value might never be written to the buffer. So it will wait forever in the kernel.

As i said in the other mail i would rather as a wait on submited cs ioctl and add some return value from cs ioctl. I can't remember the outcome of this discusion we had when we were doing virtual memory support. Alex ? Michel ? better memory than i do ? :)

Cheers,
Jerome

Quoted 22 lines
Quoted 9 lines
drivers/gpu/drm/radeon/radeon_gem.c +4 -0
+	radeon_irq_kms_sw_irq_get(rdev, ring);
+
+	fence_data = (uint32_t*)buffer_data;
+	timeout =

an
+ * interrupt and the value in the buffer might have changed.
+ */

include/drm/radeon_drm.h struct drm_radeon_info { · line 997

Quoted 6 lines
include/drm/radeon_drm.h +6 -0
+struct drm_radeon_gem_wait_user_fence {
+	uint32_t                handle;
+	uint32_t                ring;
+	uint64_t                offset;
+	uint32_t                value;
+	uint64_t                timeout_usec;

Align things here, 64 then 64 then 32 32 32 and a 32 pad.

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#8 lore raw
Quoted 54 lines · Jerome Glisse
Quoted 41 lines · David Airlie

Some comments below.

Quoted 37 lines

+   struct radeon_device *rdev = dev->dev_private;
+   struct drm_gem_object *gobj;
+   struct radeon_bo *robj;
+   void *buffer_data;
+   uint32_t *fence_data;
+   int r = 0;
+   long timeout;
+   int ring = RADEON_RING_TYPE_GFX_INDEX;
+
+   /* If you're implementing this for other rings, you'll want to share
+      code with radeon_cs_get_ring in radeon_cs.c */
+   if (args->ring != RADEON_CS_RING_GFX) {
+           return -EINVAL;
+   }

drivers/gpu/drm/radeon/radeon_gem.c +22 -0
+
+   gobj = drm_gem_object_lookup(dev, filp, args->handle);
+   if (gobj == NULL) {
+           return -ENOENT;
+   }
+   robj = gem_to_radeon_bo(gobj);
+
+   if (gobj->size < args->offset) {
+           r = -EINVAL;
+           goto unreference;
+   }
+
+   r = radeon_bo_reserve(robj, true);
+   if (r) {
+           goto unreference;
+   }
+
+   r = radeon_bo_pin(robj, RADEON_GEM_DOMAIN_GTT, NULL);
+   if (r) {
+           goto unreserve;
+   }
+
Quoted 8 lines
Quoted 5 lines

+   r = radeon_bo_kmap(robj, &buffer_data);
+   if (r) {
+           goto unpin;
+   }
+

Do you need to pin it? I think if you have it reserved and you are in here you shouldn't need to. (unless kmapping requires a pin)/

No you don't need to pin, actually it's bad to pin the buffer you might want to wait on might be in vram.

Quoted 9 lines

Also you never assume that things could go wrong and that your value might never be written to the buffer. So it will wait forever in the kernel.

As i said in the other mail i would rather as a wait on submited cs ioctl and add some return value from cs ioctl. I can't remember the outcome of this discusion we had when we were doing virtual memory support. Alex ? Michel ? better memory than i do ? :)

I vaguely recall the discussion, but I don't remember the details, I'll have to look through my old mail. Maybe a new CS ioctl flag requesting EOP irqs for the CS would be a better approach than a separate ioctl.

Alex

Quoted 29 lines

Cheers,
Jerome

Quoted 18 lines
Quoted 15 lines

+   radeon_irq_kms_sw_irq_get(rdev, ring);
+
+   fence_data = (uint32_t*)buffer_data;
+   timeout =
an
+ * interrupt and the value in the buffer might have changed.
+ */
+struct drm_radeon_gem_wait_user_fence {
+   uint32_t                handle;
+   uint32_t                ring;
+   uint64_t                offset;
+   uint32_t                value;
+   uint64_t                timeout_usec;

Align things here, 64 then 64 then 32 32 32 and a 32 pad.

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#9 lore raw
Quoted 65 lines · Alex Deucher
Quoted 54 lines · Jerome Glisse
Quoted 41 lines · David Airlie

Some comments below.

Quoted 37 lines

+   struct radeon_device *rdev = dev->dev_private;
+   struct drm_gem_object *gobj;
+   struct radeon_bo *robj;
+   void *buffer_data;
+   uint32_t *fence_data;
+   int r = 0;
+   long timeout;
+   int ring = RADEON_RING_TYPE_GFX_INDEX;
+
+   /* If you're implementing this for other rings, you'll want to share
+      code with radeon_cs_get_ring in radeon_cs.c */
+   if (args->ring != RADEON_CS_RING_GFX) {
+           return -EINVAL;
+   }

drivers/gpu/drm/radeon/radeon_gem.c +22 -0
+
+   gobj = drm_gem_object_lookup(dev, filp, args->handle);
+   if (gobj == NULL) {
+           return -ENOENT;
+   }
+   robj = gem_to_radeon_bo(gobj);
+
+   if (gobj->size < args->offset) {
+           r = -EINVAL;
+           goto unreference;
+   }
+
+   r = radeon_bo_reserve(robj, true);
+   if (r) {
+           goto unreference;
+   }
+
+   r = radeon_bo_pin(robj, RADEON_GEM_DOMAIN_GTT, NULL);
+   if (r) {
+           goto unreserve;
+   }
+
Quoted 8 lines
Quoted 5 lines

+   r = radeon_bo_kmap(robj, &buffer_data);
+   if (r) {
+           goto unpin;
+   }
+

Do you need to pin it? I think if you have it reserved and you are in here you shouldn't need to. (unless kmapping requires a pin)/

No you don't need to pin, actually it's bad to pin the buffer you might want to wait on might be in vram.

Quoted 9 lines

Also you never assume that things could go wrong and that your value might never be written to the buffer. So it will wait forever in the kernel.

As i said in the other mail i would rather as a wait on submited cs ioctl and add some return value from cs ioctl. I can't remember the outcome of this discusion we had when we were doing virtual memory support. Alex ? Michel ? better memory than i do ? :)

Quoted 6 lines

I vaguely recall the discussion, but I don't remember the details, I'll have to look through my old mail. Maybe a new CS ioctl flag requesting EOP irqs for the CS would be a better approach than a separate ioctl.

Alex

I think the idea was to return u64 value of specific ring and only enable irq if new wait cs ioctl was call, a little bit like bo wait.

Will try to dig through my mail too.

Cheers,
Jerome

Quoted 27 lines
Quoted 27 lines
Quoted 19 lines
Quoted 16 lines

+   radeon_irq_kms_sw_irq_get(rdev, ring);
+
+   fence_data = (uint32_t*)buffer_data;
+   timeout =
an
+ * interrupt and the value in the buffer might have changed.
+ */
+struct drm_radeon_gem_wait_user_fence {
+   uint32_t                handle;
+   uint32_t                ring;
+   uint64_t                offset;
+   uint32_t                value;
+   uint64_t                timeout_usec;

Align things here, 64 then 64 then 32 32 32 and a 32 pad.

Quoted 4 lines

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#10 lore raw
Quoted 67 lines · Jerome Glisse
Quoted 66 lines · Alex Deucher
Quoted 51 lines · Jerome Glisse
Quoted 39 lines · David Airlie

Some comments below.

Quoted 36 lines
drivers/gpu/drm/radeon/radeon_gem.c +10 -0
+   struct radeon_device *rdev = dev->dev_private;
+   struct drm_gem_object *gobj;
+   struct radeon_bo *robj;
+   void *buffer_data;
+   uint32_t *fence_data;
+   int r = 0;
+   long timeout;
+   int ring = RADEON_RING_TYPE_GFX_INDEX;
+
+   /* If you're implementing this for other rings, you'll want to

share

drivers/gpu/drm/radeon/radeon_gem.c +25 -0
+      code with radeon_cs_get_ring in radeon_cs.c */
+   if (args->ring != RADEON_CS_RING_GFX) {
+           return -EINVAL;
+   }
+
+   gobj = drm_gem_object_lookup(dev, filp, args->handle);
+   if (gobj == NULL) {
+           return -ENOENT;
+   }
+   robj = gem_to_radeon_bo(gobj);
+
+   if (gobj->size<  args->offset) {
+           r = -EINVAL;
+           goto unreference;
+   }
+
+   r = radeon_bo_reserve(robj, true);
+   if (r) {
+           goto unreference;
+   }
+
+   r = radeon_bo_pin(robj, RADEON_GEM_DOMAIN_GTT, NULL);
+   if (r) {
+           goto unreserve;
+   }
Quoted 8 lines

drivers/gpu/drm/radeon/radeon_gem.c int radeon_mode_dumb_destroy(struct drm_file *file_priv, · line 579

Quoted 6 lines
drivers/gpu/drm/radeon/radeon_gem.c +6 -0
+
+   r = radeon_bo_kmap(robj,&buffer_data);
+   if (r) {
+           goto unpin;
+   }
+

Do you need to pin it? I think if you have it reserved and you are in here you shouldn't need to. (unless kmapping requires a pin)/

No you don't need to pin, actually it's bad to pin the buffer you might want to wait on might be in vram.

Quoted 9 lines

Also you never assume that things could go wrong and that your value might never be written to the buffer. So it will wait forever in the kernel.

As i said in the other mail i would rather as a wait on submited cs ioctl and add some return value from cs ioctl. I can't remember the outcome of this discusion we had when we were doing virtual memory support. Alex ? Michel ? better memory than i do ? :)

I vaguely recall the discussion, but I don't remember the details, I'll have to look through my old mail. Maybe a new CS ioctl flag requesting EOP irqs for the CS would be a better approach than a separate ioctl.

Quoted 8 lines
Quoted 1 line

Alex

I think the idea was to return u64 value of specific ring and only enable irq if new wait cs ioctl was call, a little bit like bo wait.

Will try to dig through my mail too.

Cheers,
Jerome

As far as I remember we wanted the CS ioctl to return ring+fence number (expanded to 64bit) and add another ioctl to wait for a ring to reach that fence number. That has the clear advantages that a) we no longer need a fence bo for each application and b) not all rings can write a fence value from an userspace IB.

I've started to implement that and at least had the 64bit expansion of fence values and the returning of them in the CS ioctl running, but then got to work on other things first. I probably have those patches still laying around somewhere, on the other hand it should be pretty easy to implement...

Regards,
Christian.

#11 lore raw
In reply to Jerome Glisse

Christian,

You said elsewhere that you have half-finished patches that illustrate the interface Jerome prefers - if you send them to me, I can work on finishing them.

Responding to the rest of the message:

Quoted 18 lines · Jerome Glisse
Quoted 16 lines · David Airlie

Some comments below.

drivers/gpu/drm/radeon/radeon_gem.c int radeon_mode_dumb_destroy(struct drm_file *file_priv, · line 579

Quoted 10 lines
drivers/gpu/drm/radeon/radeon_gem.c +10 -0
+	r = radeon_bo_pin(robj, RADEON_GEM_DOMAIN_GTT, NULL);
+	if (r) {
+		goto unreserve;
+	}
+
+	r = radeon_bo_kmap(robj, &buffer_data);
+	if (r) {
+		goto unpin;
+	}
+

Do you need to pin it? I think if you have it reserved and you are in here you shouldn't need to. (unless kmapping requires a pin)/

Quoted 3 lines

No you don't need to pin, actually it's bad to pin the buffer you might want to wait on might be in vram.

I'll remove the pin/unpin calls - I was copying use from elsewhere in the kernel, when trying to work out why it didn't work as expected.

Quoted 4 lines

Also you never assume that things could go wrong and that your value might never be written to the buffer. So it will wait forever in the kernel.

I'm using wait_event_interruptible_timeout - userspace can pass me a timeout if it knows how long it expects to wait (and recover if it takes too long), and will be interrupted by signals. In an earlier version of the code, I didn't enable the EOP interrupts, and was able to recover userspace simply by sending it a SIGSTOP/SIGCONT pair (until the next fence, when it stalled again).

In that respect, this is no different to the existing situation in userspace
- if the GPU is reset before the EVENT_WRITE_EOP executes, userspace will spin until a signal interrupts it. All that changes if userspace uses this ioctl is that userspace will sleep instead of burning a hole in my CPU budget - the recovery strategy is unchanged.

Quoted 5 lines

As i said in the other mail i would rather as a wait on submited cs ioctl and add some return value from cs ioctl. I can't remember the outcome of this discusion we had when we were doing virtual memory support. Alex ? Michel ? better memory than i do ? :)

If someone has half-complete patches illustrating what you mean, I can take on the work of finishing them off. I've cc'd Christian on this mail, as it looks like he has a starting point.

One slight advantage this interface has over a "wait on submitted CS ioctl" interface is that, given suitable userspace changes, it becomes possible to submit multiple fences in one IB, and wait for them individually (or only wait for a subset of them). However, I'm not sure that that's enough of an advantage to outweigh the disadvantages, especially given that the userspace changes required are fairly intrusive.

Quoted 21 lines

Cheers,
Jerome

Quoted 18 lines
Quoted 9 lines
drivers/gpu/drm/radeon/radeon_gem.c +4 -0
+	radeon_irq_kms_sw_irq_get(rdev, ring);
+
+	fence_data = (uint32_t*)buffer_data;
+	timeout =

an
+ * interrupt and the value in the buffer might have changed.
+ */

include/drm/radeon_drm.h struct drm_radeon_info { · line 997

Quoted 6 lines
include/drm/radeon_drm.h +6 -0
+struct drm_radeon_gem_wait_user_fence {
+	uint32_t                handle;
+	uint32_t                ring;
+	uint64_t                offset;
+	uint32_t                value;
+	uint64_t                timeout_usec;

Align things here, 64 then 64 then 32 32 32 and a 32 pad.

Will do in v2.

Signature · 4 lines
Simon Farnsworth
Software Engineer
ONELAN Limited
http://www.onelan.com/

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel

#12 lore raw
Quoted 7 lines · Simon Farnsworth

Christian,

You said elsewhere that you have half-finished patches that illustrate the interface Jerome prefers - if you send them to me, I can work on finishing them.

Responding to the rest of the message:

Well it's probably easier to type that down again instead of searching in a two month old reflog....

Patches are attached, but be aware that they are WIP and beside compile testing completely untested.

Whats missing is the kernel interface, but that shouldn't be to complicated to implement.

Christian.

Quoted 75 lines · Jerome Glisse
Quoted 15 lines · David Airlie
Quoted 14 lines

Some comments below.

Review

Quoted 10 lines
(quoted diff) +10 -0
+	r = radeon_bo_pin(robj, RADEON_GEM_DOMAIN_GTT, NULL);
+	if (r) {
+		goto unreserve;
+	}
+
+	r = radeon_bo_kmap(robj,&buffer_data);
+	if (r) {
+		goto unpin;
+	}
+

Do you need to pin it? I think if you have it reserved and you are in here you shouldn't need to. (unless kmapping requires a pin)/

Quoted 3 lines

No you don't need to pin, actually it's bad to pin the buffer you might want to wait on might be in vram.

I'll remove the pin/unpin calls - I was copying use from elsewhere in the kernel, when trying to work out why it didn't work as expected.

Quoted 4 lines

Also you never assume that things could go wrong and that your value might never be written to the buffer. So it will wait forever in the kernel.

I'm using wait_event_interruptible_timeout - userspace can pass me a timeout if it knows how long it expects to wait (and recover if it takes too long), and will be interrupted by signals. In an earlier version of the code, I didn't enable the EOP interrupts, and was able to recover userspace simply by sending it a SIGSTOP/SIGCONT pair (until the next fence, when it stalled again).

In that respect, this is no different to the existing situation in userspace
- if the GPU is reset before the EVENT_WRITE_EOP executes, userspace will spin until a signal interrupts it. All that changes if userspace uses this ioctl is that userspace will sleep instead of burning a hole in my CPU budget - the recovery strategy is unchanged.

Quoted 5 lines

As i said in the other mail i would rather as a wait on submited cs ioctl and add some return value from cs ioctl. I can't remember the outcome of this discusion we had when we were doing virtual memory support. Alex ? Michel ? better memory than i do ? :)

If someone has half-complete patches illustrating what you mean, I can take on the work of finishing them off. I've cc'd Christian on this mail, as it looks like he has a starting point.

One slight advantage this interface has over a "wait on submitted CS ioctl" interface is that, given suitable userspace changes, it becomes possible to submit multiple fences in one IB, and wait for them individually (or only wait for a subset of them). However, I'm not sure that that's enough of an advantage to outweigh the disadvantages, especially given that the userspace changes required are fairly intrusive.

Quoted 19 lines

Cheers,
Jerome

Quoted 16 lines
Quoted 8 lines
(quoted diff) +4 -0
+	radeon_irq_kms_sw_irq_get(rdev, ring);
+
+	fence_data = (uint32_t*)buffer_data;
+	timeout =

an
+ * interrupt and the value in the buffer might have changed.
+ */

Review

Quoted 6 lines
(quoted diff) +6 -0
+struct drm_radeon_gem_wait_user_fence {
+	uint32_t                handle;
+	uint32_t                ring;
+	uint64_t                offset;
+	uint32_t                value;
+	uint64_t                timeout_usec;

Align things here, 64 then 64 then 32 32 32 and a 32 pad.

Quoted 3 lines

Will do in v2.

From 99f526793e869749209cedc60eb9f109e7046bb3 Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?Christian=20K=C3=B6nig?= <[email protected]>
Date: Wed, 1 Feb 2012 15:58:20 +0100
Subject: [PATCH 1/2] drm/radeon: expand fence sequence values to 64 bit
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

They are protected by a read/write lock anyway, so we actually don't need to use the atomic type.

---
 drivers/gpu/drm/radeon/radeon.h       |    6 +++---
 drivers/gpu/drm/radeon/radeon_fence.c |   29 +++++++++++++++++------------
 2 files changed, 20 insertions(+), 15 deletions(-)
drivers/gpu/drm/radeon/radeon.h +3 -3
index 73e05cb..2f4fb4d 100644
@@ -209,8 +209,8 @@ struct radeon_fence_driver {
 	uint32_t			scratch_reg;
 	uint64_t			gpu_addr;
 	volatile uint32_t		*cpu_addr;
-	atomic_t			seq;
-	uint32_t			last_seq;
+	uint64_t			seq;
+	uint64_t			last_seq;
 	unsigned long			last_jiffies;
 	unsigned long			last_timeout;
 	wait_queue_head_t		queue;
@@ -225,7 +225,7 @@ struct radeon_fence {
 	struct kref			kref;
 	struct list_head		list;
 	/* protected by radeon_fence.lock */
-	uint32_t			seq;
+	uint64_t			seq;
 	bool				emitted;
 	bool				signaled;
 	/* RB, DMA, etc. */
drivers/gpu/drm/radeon/radeon_fence.c +17 -12
index 64ea3dd..ac177c5 100644
@@ -29,7 +29,6 @@
  *    Dave Airlie
  */
 #include <linux/seq_file.h>
-#include <linux/atomic.h>
 #include <linux/wait.h>
 #include <linux/list.h>
 #include <linux/kref.h>
@@ -70,7 +69,7 @@ int radeon_fence_emit(struct radeon_device *rdev, struct radeon_fence *fence)
 		write_unlock_irqrestore(&rdev->fence_lock, irq_flags);
 		return 0;
 	}
-	fence->seq = atomic_add_return(1, &rdev->fence_drv[fence->ring].seq);
+	fence->seq = ++rdev->fence_drv[fence->ring].seq;
 	if (!rdev->ring[fence->ring].ready)
 		/* FIXME: cp is not running assume everythings is done right
 		 * away
@@ -90,12 +89,18 @@ static bool radeon_fence_poll_locked(struct radeon_device *rdev, int ring)
 {
 	struct radeon_fence *fence;
 	struct list_head *i, *n;
-	uint32_t seq;
+	uint64_t seq;
 	bool wake = false;
 	unsigned long cjiffies;
 
 	seq = radeon_fence_read(rdev, ring);
+	seq |= rdev->fence_drv[ring].last_seq & 0xFFFFFFFF00000000;
 	if (seq != rdev->fence_drv[ring].last_seq) {
+		if (seq < rdev->fence_drv[ring].last_seq) {
+			/* sequence wrapped around */
+			seq = (seq & 0xFFFFFFFF) | 
+			      (rdev->fence_drv[ring].seq & 0xFFFFFFFF00000000);
+		}
 		rdev->fence_drv[ring].last_seq = seq;
 		rdev->fence_drv[ring].last_jiffies = jiffies;
 		rdev->fence_drv[ring].last_timeout = RADEON_FENCE_JIFFIES_TIMEOUT;
@@ -216,7 +221,7 @@ int radeon_fence_wait(struct radeon_fence *fence, bool intr)
 {
 	struct radeon_device *rdev;
 	unsigned long irq_flags, timeout;
-	u32 seq;
+	uint64_t last_seq;
 	int r;
 
 	if (fence == NULL) {
@@ -230,8 +235,8 @@ int radeon_fence_wait(struct radeon_fence *fence, bool intr)
 	timeout = rdev->fence_drv[fence->ring].last_timeout;
 retry:
 	/* save current sequence used to check for GPU lockup */
-	seq = rdev->fence_drv[fence->ring].last_seq;
-	trace_radeon_fence_wait_begin(rdev->ddev, seq);
+	last_seq = rdev->fence_drv[fence->ring].last_seq;
+	trace_radeon_fence_wait_begin(rdev->ddev, last_seq);
 	if (intr) {
 		radeon_irq_kms_sw_irq_get(rdev, fence->ring);
 		r = wait_event_interruptible_timeout(rdev->fence_drv[fence->ring].queue,
@@ -246,7 +251,7 @@ retry:
 			 radeon_fence_signaled(fence), timeout);
 		radeon_irq_kms_sw_irq_put(rdev, fence->ring);
 	}
-	trace_radeon_fence_wait_end(rdev->ddev, seq);
+	trace_radeon_fence_wait_end(rdev->ddev, last_seq);
 	if (unlikely(!radeon_fence_signaled(fence))) {
 		/* we were interrupted for some reason and fence isn't
 		 * isn't signaled yet, resume wait
@@ -258,11 +263,11 @@ retry:
 		/* don't protect read access to rdev->fence_drv[t].last_seq
 		 * if we experiencing a lockup the value doesn't change
 		 */
-		if (seq == rdev->fence_drv[fence->ring].last_seq &&
+		if (last_seq == rdev->fence_drv[fence->ring].last_seq &&
 		    radeon_gpu_is_lockup(rdev, &rdev->ring[fence->ring])) {
 			/* good news we believe it's a lockup */
 			printk(KERN_WARNING "GPU lockup (waiting for 0x%08X last fence id 0x%08X)\n",
-			     fence->seq, seq);
+			     (uint32_t)fence->seq, (uint32_t)last_seq);
 			/* FIXME: what should we do ? marking everyone
 			 * as signaled for now
 			 */
@@ -403,7 +408,7 @@ int radeon_fence_driver_start_ring(struct radeon_device *rdev, int ring)
 	}
 	rdev->fence_drv[ring].cpu_addr = &rdev->wb.wb[index/4];
 	rdev->fence_drv[ring].gpu_addr = rdev->wb.gpu_addr + index;
-	radeon_fence_write(rdev, atomic_read(&rdev->fence_drv[ring].seq), ring);
+	radeon_fence_write(rdev, rdev->fence_drv[ring].seq, ring);
 	rdev->fence_drv[ring].initialized = true;
 	DRM_INFO("fence driver on ring %d use gpu addr 0x%08Lx and cpu addr 0x%p\n",
 		 ring, rdev->fence_drv[ring].gpu_addr, rdev->fence_drv[ring].cpu_addr);
@@ -416,7 +421,7 @@ static void radeon_fence_driver_init_ring(struct radeon_device *rdev, int ring)
 	rdev->fence_drv[ring].scratch_reg = -1;
 	rdev->fence_drv[ring].cpu_addr = NULL;
 	rdev->fence_drv[ring].gpu_addr = 0;
-	atomic_set(&rdev->fence_drv[ring].seq, 0);
+	rdev->fence_drv[ring].seq = 0;
 	INIT_LIST_HEAD(&rdev->fence_drv[ring].created);
 	INIT_LIST_HEAD(&rdev->fence_drv[ring].emitted);
 	INIT_LIST_HEAD(&rdev->fence_drv[ring].signaled);
@@ -481,7 +486,7 @@ static int radeon_debugfs_fence_info(struct seq_file *m, void *data)
 			fence = list_entry(rdev->fence_drv[i].emitted.prev,
 					   struct radeon_fence, list);
 			seq_printf(m, "Last emitted fence %p with 0x%08X\n",
-				   fence,  fence->seq);
+				   fence,  (uint32_t)fence->seq);
 		}
 	}
 	return 0;
Signature · 1 line
1.7.5.4
Quoted 1 line

From 2d3d429627d94143b0a664388496dad06a48426a Mon Sep 17 00:00:00 2001

From: =?UTF-8?q?Christian=20K=C3=B6nig?= <[email protected]>
Date: Wed, 1 Feb 2012 19:16:34 +0100
Subject: [PATCH 2/2] drm/radeon: interface waiting for fence values
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

To support waiting for fence values from usermode.

---
 drivers/gpu/drm/radeon/radeon_fence.c |   22 ++++++++++++++++++++++
 1 files changed, 22 insertions(+), 0 deletions(-)
drivers/gpu/drm/radeon/radeon_fence.c +22 -0
index ac177c5..85893c3 100644
@@ -334,6 +334,28 @@ int radeon_fence_wait_last(struct radeon_device *rdev, int ring)
 	return r;
 }
 
+static bool radeon_fence_value_reached(struct radeon_device *rdev,
+				       int ring, uint64_t value)
+{
+	unsigned long irq_flags;
+	bool result;
+	read_lock_irqsave(&rdev->fence_lock, irq_flags);
+	result = rdev->fence_drv[ring].last_seq <= value;
+	read_unlock_irqrestore(&rdev->fence_lock, irq_flags);
+	return result;
+}
+
+int radeon_fence_wait_value(struct radeon_device *rdev, int ring,
+			    uint64_t value, unsigned long timeout)
+{
+	int r;
+	radeon_irq_kms_sw_irq_get(rdev, ring);
+	r = wait_event_interruptible_timeout(rdev->fence_drv[ring].queue,
+			radeon_fence_value_reached(rdev, ring, value), timeout);
+	radeon_irq_kms_sw_irq_put(rdev, ring);
+	return r;
+}
+
 struct radeon_fence *radeon_fence_ref(struct radeon_fence *fence)
 {
 	kref_get(&fence->kref);
Signature · 1 line
1.7.5.4

_______________________________________________
dri-devel mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/dri-devel