Skip to contentSkip to conversation

libdrm_intel: Add support for userptr objects

RFC In review Tvrtko Ursulin 5 participants 17 messages Feb 26, 2014 – Sep 17, 2014 DRM drivers

lore
Share & tools
lore
#0 lore raw

RFC 1/1 libdrm_intel: Add support for userptr objects

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
include/drm/i915_drm.h +16 -0
index 2f4eb8c..d32ef99 100644
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_GET_CACHING	0x30
 #define DRM_I915_REG_READ		0x31
 #define DRM_I915_GET_RESET_STATS	0x32
+#define DRM_I915_GEM_USERPTR		0x34
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -273,6 +274,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
 #define DRM_IOCTL_I915_REG_READ			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_REG_READ, struct drm_i915_reg_read)
 #define DRM_IOCTL_I915_GET_RESET_STATS		DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GET_RESET_STATS, struct drm_i915_reset_stats)
+#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR,  struct drm_i915_gem_userptr)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -498,6 +500,20 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;
+	__u32 flags;
+#define I915_USERPTR_READ_ONLY 0x1
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+
 struct drm_i915_gem_set_domain {
 	/** Handle for the object */
 	__u32 handle;
intel/intel_bufmgr.c +13 -0
index 905556f..7f3d795 100644
@@ -60,6 +60,19 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 	return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
 }
 
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode,
+					  uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags)
+{
+	if (bufmgr->bo_alloc_userptr)
+		return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
+						stride, size, flags);
+	return NULL;
+}
+
 drm_intel_bo *
 drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
                         int x, int y, int cpp, uint32_t *tiling_mode,
intel/intel_bufmgr.h +5 -0
index 9383c72..be83a56 100644
@@ -113,6 +113,11 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 					    const char *name,
 					    unsigned long size,
 					    unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					const char *name,
+					void *addr, uint32_t tiling_mode,
+					uint32_t stride, unsigned long size,
+					unsigned long flags);
 drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
 				       const char *name,
 				       int x, int y, int cpp,
intel/intel_bufmgr_gem.c +153 -1

226 lines not on this page · raw message

intel/intel_bufmgr_priv.h +11 -1
index 2592d42..3aa1abb 100644
@@ -60,7 +60,17 @@ struct _drm_intel_bufmgr {
 					      const char *name,
 					      unsigned long size,
 					      unsigned int alignment);
-
+	/**
+	 * Allocate a buffer object from an existing user accessible
+	 * address malloc'd with the provided size.
+	 * Alignment is used when mapping to the gtt.
+	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
+	 */
+	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode, uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags);
 	/**
 	 * Allocate a tiled buffer object.
 	 *
Signature · 1 line
1.9.0
#1 lore raw
Quoted 38 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
include/drm/i915_drm.h +2 -0
index 2f4eb8c..d32ef99 100644
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_GET_CACHING	0x30
 #define DRM_I915_REG_READ		0x31
 #define DRM_I915_GET_RESET_STATS	0x32
+#define DRM_I915_GEM_USERPTR		0x34
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -273,6 +274,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
 #define DRM_IOCTL_I915_REG_READ			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_REG_READ, struct drm_i915_reg_read)
 #define DRM_IOCTL_I915_GET_RESET_STATS		DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GET_RESET_STATS, struct drm_i915_reset_stats)
+#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR,  struct drm_i915_gem_userptr)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 505

Quoted 6 lines
include/drm/i915_drm.h +3 -0
@@ -498,6 +500,20 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;

Adding alignment might be a safe bet.

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 507

Quoted 2 lines
include/drm/i915_drm.h +2 -0
+	__u32 flags;
+#define I915_USERPTR_READ_ONLY 0x1

I'll eventually want something like:
#define I915_MIRRORED_GVA 0x2 /* Request the same GPU address */

Quoted 57 lines
include/drm/i915_drm.h +9 -0
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+
 struct drm_i915_gem_set_domain {
 	/** Handle for the object */
 	__u32 handle;
intel/intel_bufmgr.c +13 -0
index 905556f..7f3d795 100644
@@ -60,6 +60,19 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 	return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
 }
 
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode,
+					  uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags)
+{
+	if (bufmgr->bo_alloc_userptr)
+		return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
+						stride, size, flags);
+	return NULL;
+}
+
 drm_intel_bo *
 drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
                         int x, int y, int cpp, uint32_t *tiling_mode,
intel/intel_bufmgr.h +5 -0
index 9383c72..be83a56 100644
@@ -113,6 +113,11 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 					    const char *name,
 					    unsigned long size,
 					    unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					const char *name,
+					void *addr, uint32_t tiling_mode,
+					uint32_t stride, unsigned long size,
+					unsigned long flags);
 drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
 				       const char *name,
 				       int x, int y, int cpp,
intel/intel_bufmgr_gem.c +0 -0
index 007a6d8..7cad945 100644

intel/intel_bufmgr_gem.c struct _drm_intel_bo_gem { · line 189

Quoted 8 lines
intel/intel_bufmgr_gem.c +5 -0
@@ -182,6 +182,11 @@ struct _drm_intel_bo_gem {
 	void *mem_virtual;
 	/** GTT virtual address for the buffer, saved across map/unmap cycles */
 	void *gtt_virtual;
+	/**
+	 * Virtual address of the buffer allocated by user, used for userptr
+	 * objects only.
+	 */
+	void *user_virtual;

Can we not just re-use mem_virtual? Do you intend to provide the ability to have two distinct CPU mappings to the same object?

Quoted 17 lines
intel/intel_bufmgr_gem.c +5 -0
 	int map_count;
 	drmMMListHead vma_list;
 
@@ -221,6 +226,11 @@ struct _drm_intel_bo_gem {
 	bool idle;
 
 	/**
+	 * Boolean of whether this buffer was allocated with userptr
+	 */
+	bool is_userptr;
+
+	/**
 	 * Size in bytes of this buffer and its relocation descendents.
 	 *
 	 * Used to avoid costly tree walking in
@@ -847,6 +857,80 @@ drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
 					       tiling, stride);

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 867

Quoted 10 lines
intel/intel_bufmgr_gem.c +8 -0
 }
 
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+				const char *name,
+				void *addr,
+				uint32_t tiling_mode,
+				uint32_t stride,
+				unsigned long size,
+				unsigned long flags)

I'd vote for dropping tiling_mode, stride. Make size and flags explicitly sized types. Not doing this has bitten us already.

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+{
+	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+	drm_intel_bo_gem *bo_gem;
+	int ret;
+	struct drm_i915_gem_userptr userptr;
+
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (tiling_mode != I915_TILING_NONE)

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 887

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+		return NULL;
+
+	bo_gem = calloc(1, sizeof(*bo_gem));
+	if (!bo_gem)
+		return NULL;
+
+	bo_gem->bo.size = size;
+
+	VG_CLEAR(userptr);
+	userptr.user_ptr = (__u64)((unsigned long)addr);

How are we getting away with non page aligned addresses? I'd vote for moving some of the memalign checks into here if all cases require page aligned.

Quoted 25 lines
intel/intel_bufmgr_gem.c +25 -0
+	userptr.user_size = size;
+	userptr.flags = flags;
+
+	ret = drmIoctl(bufmgr_gem->fd,
+			DRM_IOCTL_I915_GEM_USERPTR,
+			&userptr);
+	if (ret != 0) {
+		DBG("bo_create_userptr: "
+		    "ioctl failed with user ptr %p size 0x%lx, "
+		    "user flags 0x%lx\n", addr, size, flags);
+		free(bo_gem);
+		return NULL;
+	}
+
+	bo_gem->gem_handle = userptr.handle;
+	bo_gem->bo.handle = bo_gem->gem_handle;
+	bo_gem->bo.bufmgr    = bufmgr;
+	bo_gem->is_userptr   = true;
+	bo_gem->bo.virtual   = addr;
+	/* Save the address provided by user */
+	bo_gem->user_virtual = addr;
+	bo_gem->tiling_mode  = I915_TILING_NONE;
+	bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
+	bo_gem->stride       = 0;
+

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 922

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+	DRMINITLISTHEAD(&bo_gem->name_list);
+	DRMINITLISTHEAD(&bo_gem->vma_list);
+
+	bo_gem->name = name;
+	atomic_set(&bo_gem->refcount, 1);
+	bo_gem->validate_index = -1;
+	bo_gem->reloc_tree_fences = 0;
+	bo_gem->used_as_reloc_target = false;
+	bo_gem->has_error = false;
+	bo_gem->reusable = false;

TODO for someone: Some room to consolidate this with the other create functions.

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 924

Quoted 2 lines
intel/intel_bufmgr_gem.c +2 -0
+
+	drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);

I'm looking at the code and trying to figure out how this is relevant. It's not introduced by you directly. Just looking at the math it all seems to kind of mix-up aperture and non-aperture usages. Just talking to myself...

Quoted 36 lines
intel/intel_bufmgr_gem.c +18 -0
+
+	DBG("bo_create_userptr: "
+	    "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
+		addr, bo_gem->gem_handle, bo_gem->name,
+		size, stride, tiling_mode);
+
+	return &bo_gem->bo;
+}
+
 /**
  * Returns a drm_intel_bo wrapping the given buffer object handle.
  *
@@ -1173,6 +1257,12 @@ static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
 	struct drm_i915_gem_set_domain set_domain;
 	int ret;
 
+	if (bo_gem->is_userptr) {
+		/* Return the same user ptr */
+		bo->virtual = bo_gem->user_virtual;
+		return 0;
+	}
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count++ == 0)
@@ -1241,6 +1331,9 @@ map_gtt(drm_intel_bo *bo)
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	if (bo_gem->map_count++ == 0)
 		drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
 

intel/intel_bufmgr_gem.c int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo) · line 1481

Quoted 5 lines
intel/intel_bufmgr_gem.c +1 -1
@@ -1385,13 +1478,18 @@ int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
 
 static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
 {
-	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+	drm_intel_bufmgr_gem *bufmgr_gem;

I'm missing why this hunk was changed. But, okay.

Quoted 78 lines
intel/intel_bufmgr_gem.c +44 -0
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret = 0;
 
 	if (bo == NULL)
 		return 0;
 
+	if (bo_gem->is_userptr)
+		return 0;
+
+	bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count <= 0) {
@@ -1449,6 +1547,9 @@ drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pwrite pwrite;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pwrite);
 	pwrite.handle = bo_gem->gem_handle;
 	pwrite.offset = offset;
@@ -1501,6 +1602,9 @@ drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pread pread;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pread);
 	pread.handle = bo_gem->gem_handle;
 	pread.offset = offset;
@@ -2460,6 +2564,12 @@ drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	/* Linear buffers have no stride. By ensuring that we only ever use
 	 * stride 0 with linear buffers, we simplify our code.
 	 */
@@ -3181,6 +3291,44 @@ drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
 	bo_gem->aub_annotation_count = count;
 }
 
+static bool
+has_userptr(int fd)
+{
+	int ret;
+	void *ptr;
+	long pgsz;
+	struct drm_i915_gem_userptr userptr;
+	struct drm_gem_close close_bo;
+
+	pgsz = sysconf(_SC_PAGESIZE);
+	assert(ret > 0);
+
+	ret = posix_memalign(&ptr, pgsz, pgsz);
+	assert(ret == 0);
+
+	memset(&userptr, 0, sizeof(userptr));
+	userptr.user_ptr = (__u64)(unsigned long)ptr;
+	userptr.user_size = pgsz;
+
+retry:
+	ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
+	if (ret) {
+		if (errno == ENODEV && userptr.flags == 0) {
+			userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
+			goto retry;
+		}
+		free(ptr);

intel/intel_bufmgr_gem.c drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo, · line 3330

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+		return false;
+	}
+
+	close_bo.handle = userptr.handle;
+	ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
+	assert(ret == 0);
+	free(ptr);
+
+	return true;
+}

I think a param for USERPTR is warranted. Or did we decide not to do this already?

Quoted 28 lines
intel/intel_bufmgr_gem.c +5 -0
+
 /**
  * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
  * and manage map buffer objections.
@@ -3273,6 +3421,10 @@ drm_intel_bufmgr_gem_init(int fd, int batch_size)
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_relaxed_fencing = ret == 0;
 
+	if (has_userptr(fd))
+		bufmgr_gem->bufmgr.bo_alloc_userptr =
+			drm_intel_gem_bo_alloc_userptr;
+
 	gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_wait_timeout = ret == 0;
intel/intel_bufmgr_priv.h +4 -1
index 2592d42..3aa1abb 100644
@@ -60,7 +60,17 @@ struct _drm_intel_bufmgr {
 					      const char *name,
 					      unsigned long size,
 					      unsigned int alignment);
-
+	/**
+	 * Allocate a buffer object from an existing user accessible
+	 * address malloc'd with the provided size.
+	 * Alignment is used when mapping to the gtt.

intel/intel_bufmgr_priv.h struct _drm_intel_bufmgr { · line 76

Quoted 10 lines
intel/intel_bufmgr_priv.h +7 -0
+	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
+	 */
+	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode, uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags);
 	/**
 	 * Allocate a tiled buffer object.
 	 *

Probably don't need the special function pointer yet since I don't think we can yet envision use cases which will require any kind of special handling. A simple has_userptr in bufmgr_gem will probably suffice. But I don't care too much either way.

I couldn't spot any real bugs...

Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#2 lore raw
Quoted 42 lines · Ben Widawsky
Quoted 41 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

---
  include/drm/i915_drm.h    |  16 +++++
  intel/intel_bufmgr.c      |  13 ++++
  intel/intel_bufmgr.h      |   5 ++
  intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
  intel/intel_bufmgr_priv.h |  12 +++-
  5 files changed, 198 insertions(+), 2 deletions(-)
include/drm/i915_drm.h +2 -0
index 2f4eb8c..d32ef99 100644
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {
  #define DRM_I915_GEM_GET_CACHING	0x30
  #define DRM_I915_REG_READ		0x31
  #define DRM_I915_GET_RESET_STATS	0x32
+#define DRM_I915_GEM_USERPTR		0x34
 
  #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
  #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -273,6 +274,7 @@ typedef struct _drm_i915_sarea {
  #define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
  #define DRM_IOCTL_I915_REG_READ			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_REG_READ, struct drm_i915_reg_read)
  #define DRM_IOCTL_I915_GET_RESET_STATS		DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GET_RESET_STATS, struct drm_i915_reset_stats)
+#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR,  struct drm_i915_gem_userptr)
 
  /* Allow drivers to submit batchbuffers directly to hardware, relying
   * on the security mechanisms provided by hardware.
@@ -498,6 +500,20 @@ struct drm_i915_gem_mmap_gtt {
  	__u64 offset;
  };
 
Quoted 5 lines

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 505

Quoted 3 lines
include/drm/i915_drm.h +3 -0
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;

Adding alignment might be a safe bet.

Hmmmm, at first I thought you are raising a good point. But then I don't understand why I don't see any aligned types in linux/include/uapi/drm/i915_drm.h ?

Quoted 5 lines

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 507

Quoted 2 lines
include/drm/i915_drm.h +2 -0
+	__u32 flags;
+#define I915_USERPTR_READ_ONLY 0x1

I'll eventually want something like:
#define I915_MIRRORED_GVA 0x2 /* Request the same GPU address */

Plenty of free flags. :)

Quoted 60 lines
Quoted 60 lines
include/drm/i915_drm.h +9 -0
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+
  struct drm_i915_gem_set_domain {
  	/** Handle for the object */
  	__u32 handle;
intel/intel_bufmgr.c +13 -0
index 905556f..7f3d795 100644
@@ -60,6 +60,19 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
  	return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
  }
 
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode,
+					  uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags)
+{
+	if (bufmgr->bo_alloc_userptr)
+		return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
+						stride, size, flags);
+	return NULL;
+}
+
  drm_intel_bo *
  drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
                          int x, int y, int cpp, uint32_t *tiling_mode,
intel/intel_bufmgr.h +5 -0
index 9383c72..be83a56 100644
@@ -113,6 +113,11 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
  					    const char *name,
  					    unsigned long size,
  					    unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					const char *name,
+					void *addr, uint32_t tiling_mode,
+					uint32_t stride, unsigned long size,
+					unsigned long flags);
  drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
  				       const char *name,
  				       int x, int y, int cpp,
intel/intel_bufmgr_gem.c +0 -0
index 007a6d8..7cad945 100644
@@ -182,6 +182,11 @@ struct _drm_intel_bo_gem {
  	void *mem_virtual;
  	/** GTT virtual address for the buffer, saved across map/unmap cycles */
  	void *gtt_virtual;
Quoted 8 lines

intel/intel_bufmgr_gem.c struct _drm_intel_bo_gem { · line 189

Quoted 5 lines
intel/intel_bufmgr_gem.c +5 -0
+	/**
+	 * Virtual address of the buffer allocated by user, used for userptr
+	 * objects only.
+	 */
+	void *user_virtual;

Can we not just re-use mem_virtual? Do you intend to provide the ability to have two distinct CPU mappings to the same object?

Haven't heard anything like that. By quick look at the code seems that reusing mem_virtual would require special casing some parts which touch it. So it may be it is actually cleaner as it is.

Quoted 19 lines
Quoted 19 lines
intel/intel_bufmgr_gem.c +5 -0
  	int map_count;
  	drmMMListHead vma_list;
 
@@ -221,6 +226,11 @@ struct _drm_intel_bo_gem {
  	bool idle;
 
  	/**
+	 * Boolean of whether this buffer was allocated with userptr
+	 */
+	bool is_userptr;
+
+	/**
  	 * Size in bytes of this buffer and its relocation descendents.
  	 *
  	 * Used to avoid costly tree walking in
@@ -847,6 +857,80 @@ drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
  					       tiling, stride);
  }
 
Quoted 11 lines

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 867

Quoted 8 lines
intel/intel_bufmgr_gem.c +8 -0
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+				const char *name,
+				void *addr,
+				uint32_t tiling_mode,
+				uint32_t stride,
+				unsigned long size,
+				unsigned long flags)

I'd vote for dropping tiling_mode, stride. Make size and flags explicitly sized types. Not doing this has bitten us already.

Some people expressed interest in tiling so I thought to preserve it in the API.

Kernel even allows it, and it is just because Chris found bspec references saying it will break badly on some platforms, that I (or maybe it was both of us) decided to disable it on this level for time being.

So I think it is just a matter of coming up with a blacklist and it would be possible to use it then.

Quoted 16 lines
Quoted 16 lines
intel/intel_bufmgr_gem.c +16 -0
+{
+	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+	drm_intel_bo_gem *bo_gem;
+	int ret;
+	struct drm_i915_gem_userptr userptr;
+
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (tiling_mode != I915_TILING_NONE)
+		return NULL;
+
+	bo_gem = calloc(1, sizeof(*bo_gem));
+	if (!bo_gem)
+		return NULL;
+
Quoted 8 lines

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 887

Quoted 4 lines
intel/intel_bufmgr_gem.c +4 -0
+	bo_gem->bo.size = size;
+
+	VG_CLEAR(userptr);
+	userptr.user_ptr = (__u64)((unsigned long)addr);

How are we getting away with non page aligned addresses? I'd vote for moving some of the memalign checks into here if all cases require page aligned.

Kernel will reject any unaligned address or size in the ioctl.

Quoted 82 lines
Quoted 25 lines
intel/intel_bufmgr_gem.c +25 -0
+	userptr.user_size = size;
+	userptr.flags = flags;
+
+	ret = drmIoctl(bufmgr_gem->fd,
+			DRM_IOCTL_I915_GEM_USERPTR,
+			&userptr);
+	if (ret != 0) {
+		DBG("bo_create_userptr: "
+		    "ioctl failed with user ptr %p size 0x%lx, "
+		    "user flags 0x%lx\n", addr, size, flags);
+		free(bo_gem);
+		return NULL;
+	}
+
+	bo_gem->gem_handle = userptr.handle;
+	bo_gem->bo.handle = bo_gem->gem_handle;
+	bo_gem->bo.bufmgr    = bufmgr;
+	bo_gem->is_userptr   = true;
+	bo_gem->bo.virtual   = addr;
+	/* Save the address provided by user */
+	bo_gem->user_virtual = addr;
+	bo_gem->tiling_mode  = I915_TILING_NONE;
+	bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
+	bo_gem->stride       = 0;
+

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 922

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+	DRMINITLISTHEAD(&bo_gem->name_list);
+	DRMINITLISTHEAD(&bo_gem->vma_list);
+
+	bo_gem->name = name;
+	atomic_set(&bo_gem->refcount, 1);
+	bo_gem->validate_index = -1;
+	bo_gem->reloc_tree_fences = 0;
+	bo_gem->used_as_reloc_target = false;
+	bo_gem->has_error = false;
+	bo_gem->reusable = false;

TODO for someone: Some room to consolidate this with the other create functions.

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 924

Quoted 2 lines
intel/intel_bufmgr_gem.c +2 -0
+
+	drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);

I'm looking at the code and trying to figure out how this is relevant. It's not introduced by you directly. Just looking at the math it all seems to kind of mix-up aperture and non-aperture usages. Just talking to myself...

Quoted 35 lines
intel/intel_bufmgr_gem.c +18 -0
+
+	DBG("bo_create_userptr: "
+	    "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
+		addr, bo_gem->gem_handle, bo_gem->name,
+		size, stride, tiling_mode);
+
+	return &bo_gem->bo;
+}
+
  /**
   * Returns a drm_intel_bo wrapping the given buffer object handle.
   *
@@ -1173,6 +1257,12 @@ static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
  	struct drm_i915_gem_set_domain set_domain;
  	int ret;
 
+	if (bo_gem->is_userptr) {
+		/* Return the same user ptr */
+		bo->virtual = bo_gem->user_virtual;
+		return 0;
+	}
+
  	pthread_mutex_lock(&bufmgr_gem->lock);
 
  	if (bo_gem->map_count++ == 0)
@@ -1241,6 +1331,9 @@ map_gtt(drm_intel_bo *bo)
  	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
  	if (bo_gem->map_count++ == 0)
  		drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
 
Quoted 8 lines

intel/intel_bufmgr_gem.c int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo) · line 1481

Quoted 6 lines
intel/intel_bufmgr_gem.c +1 -1
@@ -1385,13 +1478,18 @@ int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
 
  static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
  {
-	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+	drm_intel_bufmgr_gem *bufmgr_gem;

I'm missing why this hunk was changed. But, okay.

Just to ensure NULL bo is handled nicely I guess.

Quoted 83 lines
Quoted 83 lines
intel/intel_bufmgr_gem.c +49 -0
  	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  	int ret = 0;
 
  	if (bo == NULL)
  		return 0;
 
+	if (bo_gem->is_userptr)
+		return 0;
+
+	bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+
  	pthread_mutex_lock(&bufmgr_gem->lock);
 
  	if (bo_gem->map_count <= 0) {
@@ -1449,6 +1547,9 @@ drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
  	struct drm_i915_gem_pwrite pwrite;
  	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
  	VG_CLEAR(pwrite);
  	pwrite.handle = bo_gem->gem_handle;
  	pwrite.offset = offset;
@@ -1501,6 +1602,9 @@ drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
  	struct drm_i915_gem_pread pread;
  	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
  	VG_CLEAR(pread);
  	pread.handle = bo_gem->gem_handle;
  	pread.offset = offset;
@@ -2460,6 +2564,12 @@ drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
  	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
  	int ret;
 
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
  	/* Linear buffers have no stride. By ensuring that we only ever use
  	 * stride 0 with linear buffers, we simplify our code.
  	 */
@@ -3181,6 +3291,44 @@ drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
  	bo_gem->aub_annotation_count = count;
  }
 
+static bool
+has_userptr(int fd)
+{
+	int ret;
+	void *ptr;
+	long pgsz;
+	struct drm_i915_gem_userptr userptr;
+	struct drm_gem_close close_bo;
+
+	pgsz = sysconf(_SC_PAGESIZE);
+	assert(ret > 0);
+
+	ret = posix_memalign(&ptr, pgsz, pgsz);
+	assert(ret == 0);
+
+	memset(&userptr, 0, sizeof(userptr));
+	userptr.user_ptr = (__u64)(unsigned long)ptr;
+	userptr.user_size = pgsz;
+
+retry:
+	ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
+	if (ret) {
+		if (errno == ENODEV && userptr.flags == 0) {
+			userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
+			goto retry;
+		}
+		free(ptr);
+		return false;
+	}
+
+	close_bo.handle = userptr.handle;
+	ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
Quoted 8 lines

intel/intel_bufmgr_gem.c drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo, · line 3330

Quoted 5 lines
intel/intel_bufmgr_gem.c +5 -0
+	assert(ret == 0);
+	free(ptr);
+
+	return true;
+}

I think a param for USERPTR is warranted. Or did we decide not to do this already?

I asked for it, but people with authority said no. It is all very weak, fragile and dangerous anyway - param or feature detect like the above.

We would really need a proper feature detect mechanism, like text based in sysfs or something.

Quoted 35 lines
Quoted 35 lines
intel/intel_bufmgr_gem.c +5 -0
+
  /**
   * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
   * and manage map buffer objections.
@@ -3273,6 +3421,10 @@ drm_intel_bufmgr_gem_init(int fd, int batch_size)
  	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  	bufmgr_gem->has_relaxed_fencing = ret == 0;
 
+	if (has_userptr(fd))
+		bufmgr_gem->bufmgr.bo_alloc_userptr =
+			drm_intel_gem_bo_alloc_userptr;
+
  	gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
  	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
  	bufmgr_gem->has_wait_timeout = ret == 0;
intel/intel_bufmgr_priv.h +11 -1
index 2592d42..3aa1abb 100644
@@ -60,7 +60,17 @@ struct _drm_intel_bufmgr {
  					      const char *name,
  					      unsigned long size,
  					      unsigned int alignment);
-
+	/**
+	 * Allocate a buffer object from an existing user accessible
+	 * address malloc'd with the provided size.
+	 * Alignment is used when mapping to the gtt.
+	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
+	 */
+	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode, uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags);
Quoted 8 lines

intel/intel_bufmgr_priv.h struct _drm_intel_bufmgr { · line 76

Quoted 3 lines
intel/intel_bufmgr_priv.h +0 -0
  	/**
  	 * Allocate a tiled buffer object.
  	 *

Probably don't need the special function pointer yet since I don't think we can yet envision use cases which will require any kind of special handling. A simple has_userptr in bufmgr_gem will probably suffice. But I don't care too much either way.

What do you mean?

Quoted 1 line

I couldn't spot any real bugs...

Cool, I am glad there is some interest for this.

Regards,

Tvrtko

#3 lore raw
Quoted 50 lines · Tvrtko Ursulin
Quoted 42 lines · Ben Widawsky
Quoted 41 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
include/drm/i915_drm.h +2 -0
index 2f4eb8c..d32ef99 100644
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_GET_CACHING	0x30
 #define DRM_I915_REG_READ		0x31
 #define DRM_I915_GET_RESET_STATS	0x32
+#define DRM_I915_GEM_USERPTR		0x34
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -273,6 +274,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
 #define DRM_IOCTL_I915_REG_READ			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_REG_READ, struct drm_i915_reg_read)
 #define DRM_IOCTL_I915_GET_RESET_STATS		DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GET_RESET_STATS, struct drm_i915_reset_stats)
+#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR,  struct drm_i915_gem_userptr)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -498,6 +500,20 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
Quoted 5 lines

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 505

Quoted 3 lines
include/drm/i915_drm.h +3 -0
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;

Adding alignment might be a safe bet.

Quoted 3 lines

Hmmmm, at first I thought you are raising a good point. But then I don't understand why I don't see any aligned types in linux/include/uapi/drm/i915_drm.h ?

I meant an alignment field. I was thinking if some buffers have weird alignment requirements for the GPU (but not the CPU) making that info available to the kernel would be important.

As another potentially lacking field (which I happen not to care about for the vmap/userptr style usage), we have certain buffers that need to live within a certain part of the GPU address space.

I should mention that when I wrote the previous email, I didn't have access to the latest kernel implementation when I sent the mail. I had been assuming (because I've been wrapped up in my own, different world), that the address space allocation was done at the time of the IOCTL. It is not, and therefore, my request for the existing code does not make sense.

With that though, I should describe my usecase briefly. I want the ability to carve out the address space at the time of the IOCTL. As such, I need to know some of these attributes.

Quoted 78 lines
Quoted 6 lines

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 507

Quoted 3 lines
include/drm/i915_drm.h +2 -0
+	__u32 flags;
+#define I915_USERPTR_READ_ONLY 0x1

I'll eventually want something like:
#define I915_MIRRORED_GVA 0x2 /* Request the same GPU address */

Plenty of free flags. :)

Quoted 60 lines
Quoted 60 lines
include/drm/i915_drm.h +9 -0
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+
 struct drm_i915_gem_set_domain {
 	/** Handle for the object */
 	__u32 handle;
intel/intel_bufmgr.c +13 -0
index 905556f..7f3d795 100644
@@ -60,6 +60,19 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 	return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
 }
 
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode,
+					  uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags)
+{
+	if (bufmgr->bo_alloc_userptr)
+		return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
+						stride, size, flags);
+	return NULL;
+}
+
 drm_intel_bo *
 drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
                         int x, int y, int cpp, uint32_t *tiling_mode,
intel/intel_bufmgr.h +5 -0
index 9383c72..be83a56 100644
@@ -113,6 +113,11 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 					    const char *name,
 					    unsigned long size,
 					    unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					const char *name,
+					void *addr, uint32_t tiling_mode,
+					uint32_t stride, unsigned long size,
+					unsigned long flags);
 drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
 				       const char *name,
 				       int x, int y, int cpp,
intel/intel_bufmgr_gem.c +0 -0
index 007a6d8..7cad945 100644
@@ -182,6 +182,11 @@ struct _drm_intel_bo_gem {
 	void *mem_virtual;
 	/** GTT virtual address for the buffer, saved across map/unmap cycles */
 	void *gtt_virtual;
Quoted 8 lines

intel/intel_bufmgr_gem.c struct _drm_intel_bo_gem { · line 189

Quoted 5 lines
intel/intel_bufmgr_gem.c +5 -0
+	/**
+	 * Virtual address of the buffer allocated by user, used for userptr
+	 * objects only.
+	 */
+	void *user_virtual;

Can we not just re-use mem_virtual? Do you intend to provide the ability to have two distinct CPU mappings to the same object?

Quoted 4 lines

Haven't heard anything like that. By quick look at the code seems that reusing mem_virtual would require special casing some parts which touch it. So it may be it is actually cleaner as it is.

I didn't look too hard, but I think it might *just work* if you use mem_virtual. I will try it and let you know.

Quoted 34 lines
Quoted 19 lines
Quoted 19 lines
intel/intel_bufmgr_gem.c +5 -0
 	int map_count;
 	drmMMListHead vma_list;
 
@@ -221,6 +226,11 @@ struct _drm_intel_bo_gem {
 	bool idle;
 
 	/**
+	 * Boolean of whether this buffer was allocated with userptr
+	 */
+	bool is_userptr;
+
+	/**
 	 * Size in bytes of this buffer and its relocation descendents.
 	 *
 	 * Used to avoid costly tree walking in
@@ -847,6 +857,80 @@ drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
 					       tiling, stride);
 }
 
Quoted 11 lines

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 867

Quoted 8 lines
intel/intel_bufmgr_gem.c +8 -0
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+				const char *name,
+				void *addr,
+				uint32_t tiling_mode,
+				uint32_t stride,
+				unsigned long size,
+				unsigned long flags)

I'd vote for dropping tiling_mode, stride. Make size and flags explicitly sized types. Not doing this has bitten us already.

Some people expressed interest in tiling so I thought to preserve it in the API.

Quoted 7 lines

Kernel even allows it, and it is just because Chris found bspec references saying it will break badly on some platforms, that I (or maybe it was both of us) decided to disable it on this level for time being.

So I think it is just a matter of coming up with a blacklist and it would be possible to use it then.

Well again, maybe this is specific to my usecase, but I can never imagine a use for such fields at this stage in the buffer's lifecycle. Could you get "some people" to describe how they want to use it?

Quoted 212 lines
Quoted 16 lines
Quoted 16 lines
intel/intel_bufmgr_gem.c +16 -0
+{
+	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+	drm_intel_bo_gem *bo_gem;
+	int ret;
+	struct drm_i915_gem_userptr userptr;
+
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (tiling_mode != I915_TILING_NONE)
+		return NULL;
+
+	bo_gem = calloc(1, sizeof(*bo_gem));
+	if (!bo_gem)
+		return NULL;
+
Quoted 8 lines

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 887

Quoted 4 lines
intel/intel_bufmgr_gem.c +4 -0
+	bo_gem->bo.size = size;
+
+	VG_CLEAR(userptr);
+	userptr.user_ptr = (__u64)((unsigned long)addr);

How are we getting away with non page aligned addresses? I'd vote for moving some of the memalign checks into here if all cases require page aligned.

Kernel will reject any unaligned address or size in the ioctl.

Quoted 82 lines
Quoted 25 lines
intel/intel_bufmgr_gem.c +25 -0
+	userptr.user_size = size;
+	userptr.flags = flags;
+
+	ret = drmIoctl(bufmgr_gem->fd,
+			DRM_IOCTL_I915_GEM_USERPTR,
+			&userptr);
+	if (ret != 0) {
+		DBG("bo_create_userptr: "
+		    "ioctl failed with user ptr %p size 0x%lx, "
+		    "user flags 0x%lx\n", addr, size, flags);
+		free(bo_gem);
+		return NULL;
+	}
+
+	bo_gem->gem_handle = userptr.handle;
+	bo_gem->bo.handle = bo_gem->gem_handle;
+	bo_gem->bo.bufmgr    = bufmgr;
+	bo_gem->is_userptr   = true;
+	bo_gem->bo.virtual   = addr;
+	/* Save the address provided by user */
+	bo_gem->user_virtual = addr;
+	bo_gem->tiling_mode  = I915_TILING_NONE;
+	bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
+	bo_gem->stride       = 0;
+

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 922

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+	DRMINITLISTHEAD(&bo_gem->name_list);
+	DRMINITLISTHEAD(&bo_gem->vma_list);
+
+	bo_gem->name = name;
+	atomic_set(&bo_gem->refcount, 1);
+	bo_gem->validate_index = -1;
+	bo_gem->reloc_tree_fences = 0;
+	bo_gem->used_as_reloc_target = false;
+	bo_gem->has_error = false;
+	bo_gem->reusable = false;

TODO for someone: Some room to consolidate this with the other create functions.

intel/intel_bufmgr_gem.c drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name, · line 924

Quoted 2 lines
intel/intel_bufmgr_gem.c +2 -0
+
+	drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);

I'm looking at the code and trying to figure out how this is relevant. It's not introduced by you directly. Just looking at the math it all seems to kind of mix-up aperture and non-aperture usages. Just talking to myself...

Quoted 35 lines
intel/intel_bufmgr_gem.c +18 -0
+
+	DBG("bo_create_userptr: "
+	    "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
+		addr, bo_gem->gem_handle, bo_gem->name,
+		size, stride, tiling_mode);
+
+	return &bo_gem->bo;
+}
+
 /**
  * Returns a drm_intel_bo wrapping the given buffer object handle.
  *
@@ -1173,6 +1257,12 @@ static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
 	struct drm_i915_gem_set_domain set_domain;
 	int ret;
 
+	if (bo_gem->is_userptr) {
+		/* Return the same user ptr */
+		bo->virtual = bo_gem->user_virtual;
+		return 0;
+	}
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count++ == 0)
@@ -1241,6 +1331,9 @@ map_gtt(drm_intel_bo *bo)
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	if (bo_gem->map_count++ == 0)
 		drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
 
Quoted 8 lines

intel/intel_bufmgr_gem.c int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo) · line 1481

Quoted 6 lines
intel/intel_bufmgr_gem.c +1 -1
@@ -1385,13 +1478,18 @@ int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
 
 static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
 {
-	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+	drm_intel_bufmgr_gem *bufmgr_gem;

I'm missing why this hunk was changed. But, okay.

Just to ensure NULL bo is handled nicely I guess.

Quoted 83 lines
Quoted 83 lines
intel/intel_bufmgr_gem.c +49 -0
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret = 0;
 
 	if (bo == NULL)
 		return 0;
 
+	if (bo_gem->is_userptr)
+		return 0;
+
+	bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count <= 0) {
@@ -1449,6 +1547,9 @@ drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pwrite pwrite;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pwrite);
 	pwrite.handle = bo_gem->gem_handle;
 	pwrite.offset = offset;
@@ -1501,6 +1602,9 @@ drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pread pread;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pread);
 	pread.handle = bo_gem->gem_handle;
 	pread.offset = offset;
@@ -2460,6 +2564,12 @@ drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	/* Linear buffers have no stride. By ensuring that we only ever use
 	 * stride 0 with linear buffers, we simplify our code.
 	 */
@@ -3181,6 +3291,44 @@ drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
 	bo_gem->aub_annotation_count = count;
 }
 
+static bool
+has_userptr(int fd)
+{
+	int ret;
+	void *ptr;
+	long pgsz;
+	struct drm_i915_gem_userptr userptr;
+	struct drm_gem_close close_bo;
+
+	pgsz = sysconf(_SC_PAGESIZE);
+	assert(ret > 0);
+
+	ret = posix_memalign(&ptr, pgsz, pgsz);
+	assert(ret == 0);
+
+	memset(&userptr, 0, sizeof(userptr));
+	userptr.user_ptr = (__u64)(unsigned long)ptr;
+	userptr.user_size = pgsz;
+
+retry:
+	ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
+	if (ret) {
+		if (errno == ENODEV && userptr.flags == 0) {
+			userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
+			goto retry;
+		}
+		free(ptr);
+		return false;
+	}
+
+	close_bo.handle = userptr.handle;
+	ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
Quoted 8 lines

intel/intel_bufmgr_gem.c drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo, · line 3330

Quoted 5 lines
intel/intel_bufmgr_gem.c +5 -0
+	assert(ret == 0);
+	free(ptr);
+
+	return true;
+}

I think a param for USERPTR is warranted. Or did we decide not to do this already?

Quoted 6 lines

I asked for it, but people with authority said no. It is all very weak, fragile and dangerous anyway - param or feature detect like the above.

We would really need a proper feature detect mechanism, like text based in sysfs or something.

I don't see why a param is fragile. Feature detect OTOH is almost always implemented in a fragile manner.

Quoted 44 lines
Quoted 35 lines
Quoted 35 lines
intel/intel_bufmgr_gem.c +5 -0
+
 /**
  * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
  * and manage map buffer objections.
@@ -3273,6 +3421,10 @@ drm_intel_bufmgr_gem_init(int fd, int batch_size)
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_relaxed_fencing = ret == 0;
 
+	if (has_userptr(fd))
+		bufmgr_gem->bufmgr.bo_alloc_userptr =
+			drm_intel_gem_bo_alloc_userptr;
+
 	gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_wait_timeout = ret == 0;
intel/intel_bufmgr_priv.h +11 -1
index 2592d42..3aa1abb 100644
@@ -60,7 +60,17 @@ struct _drm_intel_bufmgr {
 					      const char *name,
 					      unsigned long size,
 					      unsigned int alignment);
-
+	/**
+	 * Allocate a buffer object from an existing user accessible
+	 * address malloc'd with the provided size.
+	 * Alignment is used when mapping to the gtt.
+	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
+	 */
+	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode, uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags);
Quoted 8 lines

intel/intel_bufmgr_priv.h struct _drm_intel_bufmgr { · line 76

Quoted 3 lines
intel/intel_bufmgr_priv.h +0 -0
 	/**
 	 * Allocate a tiled buffer object.
 	 *

Probably don't need the special function pointer yet since I don't think we can yet envision use cases which will require any kind of special handling. A simple has_userptr in bufmgr_gem will probably suffice. But I don't care too much either way.

Quoted 1 line

What do you mean?

Don't add bo_alloc_userptr to the bufmgr. Just add the prototype to intel_bufmgr.h.

Quoted 5 lines
Quoted 2 lines

I couldn't spot any real bugs...

Cool, I am glad there is some interest for this.

I'm pretty close to running this with most of the changes I had asked you for. I need to see how much of your igt test I can reuse now.

Quoted 3 lines

Regards,

Tvrtko

Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#4 lore raw
Quoted 51 lines · Ben Widawsky
Quoted 50 lines · Tvrtko Ursulin
Quoted 47 lines · Ben Widawsky
Quoted 38 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
include/drm/i915_drm.h +2 -0
index 2f4eb8c..d32ef99 100644
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_GET_CACHING	0x30
 #define DRM_I915_REG_READ		0x31
 #define DRM_I915_GET_RESET_STATS	0x32
+#define DRM_I915_GEM_USERPTR		0x34
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -273,6 +274,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
 #define DRM_IOCTL_I915_REG_READ			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_REG_READ, struct drm_i915_reg_read)
 #define DRM_IOCTL_I915_GET_RESET_STATS		DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GET_RESET_STATS, struct drm_i915_reset_stats)
+#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR,  struct drm_i915_gem_userptr)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 505

Quoted 6 lines
include/drm/i915_drm.h +3 -0
@@ -498,6 +500,20 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;

Adding alignment might be a safe bet.

Quoted 7 lines
Quoted 3 lines

Hmmmm, at first I thought you are raising a good point. But then I don't understand why I don't see any aligned types in linux/include/uapi/drm/i915_drm.h ?

I meant an alignment field. I was thinking if some buffers have weird alignment requirements for the GPU (but not the CPU) making that info available to the kernel would be important.

We have a flags param and we can always extend the ioctl at the end, so imo no need to preemptively add stuff we don't need yet. -Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#5 lore raw
In reply to Ben Widawsky
Quoted 8 lines · Ben Widawsky
Quoted 7 lines · Tvrtko Ursulin

Some people expressed interest in tiling so I thought to preserve it in the API.

Kernel even allows it, and it is just because Chris found bspec references saying it will break badly on some platforms, that I (or maybe it was both of us) decided to disable it on this level for time being.

Quoted 7 lines
Quoted 3 lines

So I think it is just a matter of coming up with a blacklist and it would be possible to use it then.

Well again, maybe this is specific to my usecase, but I can never imagine a use for such fields at this stage in the buffer's lifecycle. Could you get "some people" to describe how they want to use it?

Actually thinking about it more, when I collated requirements from various groups they were not all that interested. But Chris was actually in favour of keeping it in the kernel rather than disabling it altogether. So I decided to keep it in the userspace API and only reject the attempts to use it for time being.

Quoted 6 lines
Quoted 6 lines
Quoted 2 lines

I think a param for USERPTR is warranted. Or did we decide not to do this already?

I asked for it, but people with authority said no. It is all very weak, fragile and dangerous anyway - param or feature detect like the above.

Quoted 6 lines
Quoted 3 lines

We would really need a proper feature detect mechanism, like text based in sysfs or something.

I don't see why a param is fragile. Feature detect OTOH is almost always implemented in a fragile manner.

Not if we had a text file in sysfs with names rather than numbers (getparam) without a central allocation authority.

HAS_PARAM(FEATURE1) actually just tricks you into thinking it's fine, while actually you are just asking "Do I have feature 48". What is feature 48? Who knows... some features never make it to upstream, some do and then get their HAS_PARAM number reallocated so it is really weak.

Something like "grep userptr /sys/kernel/debug/dri/0/i915_features", or "stat /sys/kernel/debug/dri/0/i915/features/userptr" would be much better.

This way or the other, it seems to be there is not consensus with upstream gate keepers whether to have it or not. It was Chris actually who ripped it out. Personally I can see both arguments which is why I think we should come up with something better.

Quoted 9 lines
Quoted 6 lines
Quoted 4 lines

Probably don't need the special function pointer yet since I don't think we can yet envision use cases which will require any kind of special handling. A simple has_userptr in bufmgr_gem will probably suffice. But I don't care too much either way.

What do you mean?

Don't add bo_alloc_userptr to the bufmgr. Just add the prototype to intel_bufmgr.h.

Not sure, wouldn't like to make inconsistent.

Quoted 2 lines

I'm pretty close to running this with most of the changes I had asked you for. I need to see how much of your igt test I can reuse now.

Cool, so there is nothing for me to do. :)

Regards,

Tvrtko

#6 lore raw
In reply to Tvrtko Ursulin
Quoted 45 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
include/drm/i915_drm.h +6 -0
index 2f4eb8c..d32ef99 100644
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_GET_CACHING	0x30
 #define DRM_I915_REG_READ		0x31
 #define DRM_I915_GET_RESET_STATS	0x32
+#define DRM_I915_GEM_USERPTR		0x34
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -273,6 +274,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
 #define DRM_IOCTL_I915_REG_READ			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_REG_READ, struct drm_i915_reg_read)
 #define DRM_IOCTL_I915_GET_RESET_STATS		DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GET_RESET_STATS, struct drm_i915_reset_stats)
+#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR,  struct drm_i915_gem_userptr)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -498,6 +500,20 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;
+	__u32 flags;

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 516

Quoted 10 lines
include/drm/i915_drm.h +10 -0
+#define I915_USERPTR_READ_ONLY 0x1
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+

Oh yeah. I want a ctx_id here as well. Chris, any objection to adding this?

[snip]

Signature · 1 line
Ben Widawsky, Intel Open Source Technology Center
#7 lore raw
Quoted 10 lines · Ben Widawsky
Quoted 9 lines · Tvrtko Ursulin
include/drm/i915_drm.h +9 -0
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;
+	__u32 flags;
+#define I915_USERPTR_READ_ONLY 0x1
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
Quoted 8 lines

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 516

Quoted 5 lines
include/drm/i915_drm.h +5 -0
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+

Oh yeah. I want a ctx_id here as well. Chris, any objection to adding this?

What for? bo are file-scoped not context-scoped.
-Chris

Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#8 lore raw
Quoted 20 lines · Chris Wilson
Quoted 10 lines · Ben Widawsky
Quoted 9 lines · Tvrtko Ursulin
include/drm/i915_drm.h +9 -0
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;
+	__u32 flags;
+#define I915_USERPTR_READ_ONLY 0x1
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
Quoted 8 lines

include/drm/i915_drm.h struct drm_i915_gem_mmap_gtt { · line 516

Quoted 5 lines
include/drm/i915_drm.h +5 -0
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+

Oh yeah. I want a ctx_id here as well. Chris, any objection to adding this?

Quoted 1 line

What for? bo are file-scoped not context-scoped.

I think what Ben actually wants is a ctx_id in the soft-pin ioctl. Makes his mirrored ppgtt POC a 2 ioctl thing, but the resulting orthogonality of interfaces is imo something much to be preferred. Since we still need real bos for scanout targets and similar, and I'm going to rip everyone's head off if we do that by shmem-mapping+userptr ;-)
-Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#9 lore raw
In reply to Tvrtko Ursulin
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

Quoted 8 lines
---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)

Apart from couple of remarks below I couldn't find anything that would prevent merging this. Well, except maybe that it'd be very nice to have some feedback from someone using it, we do have an API/ABI guarantee on libdrm after all.

Signature · 1 line
Damien
Quoted 271 lines
include/drm/i915_drm.h +16 -0
index 2f4eb8c..d32ef99 100644
@@ -223,6 +223,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_I915_GEM_GET_CACHING	0x30
 #define DRM_I915_REG_READ		0x31
 #define DRM_I915_GET_RESET_STATS	0x32
+#define DRM_I915_GEM_USERPTR		0x34
 
 #define DRM_IOCTL_I915_INIT		DRM_IOW( DRM_COMMAND_BASE + DRM_I915_INIT, drm_i915_init_t)
 #define DRM_IOCTL_I915_FLUSH		DRM_IO ( DRM_COMMAND_BASE + DRM_I915_FLUSH)
@@ -273,6 +274,7 @@ typedef struct _drm_i915_sarea {
 #define DRM_IOCTL_I915_GEM_CONTEXT_DESTROY	DRM_IOW (DRM_COMMAND_BASE + DRM_I915_GEM_CONTEXT_DESTROY, struct drm_i915_gem_context_destroy)
 #define DRM_IOCTL_I915_REG_READ			DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_REG_READ, struct drm_i915_reg_read)
 #define DRM_IOCTL_I915_GET_RESET_STATS		DRM_IOWR (DRM_COMMAND_BASE + DRM_I915_GET_RESET_STATS, struct drm_i915_reset_stats)
+#define DRM_IOCTL_I915_GEM_USERPTR	DRM_IOWR(DRM_COMMAND_BASE + DRM_I915_GEM_USERPTR,  struct drm_i915_gem_userptr)
 
 /* Allow drivers to submit batchbuffers directly to hardware, relying
  * on the security mechanisms provided by hardware.
@@ -498,6 +500,20 @@ struct drm_i915_gem_mmap_gtt {
 	__u64 offset;
 };
 
+struct drm_i915_gem_userptr {
+	__u64 user_ptr;
+	__u64 user_size;
+	__u32 flags;
+#define I915_USERPTR_READ_ONLY 0x1
+#define I915_USERPTR_UNSYNCHRONIZED 0x80000000
+	/**
+	* Returned handle for the object.
+	*
+	* Object handles are nonzero.
+	*/
+	__u32 handle;
+};
+
 struct drm_i915_gem_set_domain {
 	/** Handle for the object */
 	__u32 handle;
intel/intel_bufmgr.c +13 -0
index 905556f..7f3d795 100644
@@ -60,6 +60,19 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 	return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
 }
 
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode,
+					  uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags)
+{
+	if (bufmgr->bo_alloc_userptr)
+		return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
+						stride, size, flags);
+	return NULL;
+}
+
 drm_intel_bo *
 drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
                         int x, int y, int cpp, uint32_t *tiling_mode,
intel/intel_bufmgr.h +5 -0
index 9383c72..be83a56 100644
@@ -113,6 +113,11 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 					    const char *name,
 					    unsigned long size,
 					    unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					const char *name,
+					void *addr, uint32_t tiling_mode,
+					uint32_t stride, unsigned long size,
+					unsigned long flags);
 drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
 				       const char *name,
 				       int x, int y, int cpp,
intel/intel_bufmgr_gem.c +112 -1
index 007a6d8..7cad945 100644
@@ -182,6 +182,11 @@ struct _drm_intel_bo_gem {
 	void *mem_virtual;
 	/** GTT virtual address for the buffer, saved across map/unmap cycles */
 	void *gtt_virtual;
+	/**
+	 * Virtual address of the buffer allocated by user, used for userptr
+	 * objects only.
+	 */
+	void *user_virtual;
 	int map_count;
 	drmMMListHead vma_list;
 
@@ -221,6 +226,11 @@ struct _drm_intel_bo_gem {
 	bool idle;
 
 	/**
+	 * Boolean of whether this buffer was allocated with userptr
+	 */
+	bool is_userptr;
+
+	/**
 	 * Size in bytes of this buffer and its relocation descendents.
 	 *
 	 * Used to avoid costly tree walking in
@@ -847,6 +857,80 @@ drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
 					       tiling, stride);
 }
 
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+				const char *name,
+				void *addr,
+				uint32_t tiling_mode,
+				uint32_t stride,
+				unsigned long size,
+				unsigned long flags)
+{
+	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+	drm_intel_bo_gem *bo_gem;
+	int ret;
+	struct drm_i915_gem_userptr userptr;
+
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (tiling_mode != I915_TILING_NONE)
+		return NULL;
+
+	bo_gem = calloc(1, sizeof(*bo_gem));
+	if (!bo_gem)
+		return NULL;
+
+	bo_gem->bo.size = size;
+
+	VG_CLEAR(userptr);
+	userptr.user_ptr = (__u64)((unsigned long)addr);
+	userptr.user_size = size;
+	userptr.flags = flags;
+
+	ret = drmIoctl(bufmgr_gem->fd,
+			DRM_IOCTL_I915_GEM_USERPTR,
+			&userptr);
+	if (ret != 0) {
+		DBG("bo_create_userptr: "
+		    "ioctl failed with user ptr %p size 0x%lx, "
+		    "user flags 0x%lx\n", addr, size, flags);
+		free(bo_gem);
+		return NULL;
+	}
+
+	bo_gem->gem_handle = userptr.handle;
+	bo_gem->bo.handle = bo_gem->gem_handle;
+	bo_gem->bo.bufmgr    = bufmgr;
+	bo_gem->is_userptr   = true;
+	bo_gem->bo.virtual   = addr;
+	/* Save the address provided by user */
+	bo_gem->user_virtual = addr;
+	bo_gem->tiling_mode  = I915_TILING_NONE;
+	bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
+	bo_gem->stride       = 0;
+
+	DRMINITLISTHEAD(&bo_gem->name_list);
+	DRMINITLISTHEAD(&bo_gem->vma_list);
+
+	bo_gem->name = name;
+	atomic_set(&bo_gem->refcount, 1);
+	bo_gem->validate_index = -1;
+	bo_gem->reloc_tree_fences = 0;
+	bo_gem->used_as_reloc_target = false;
+	bo_gem->has_error = false;
+	bo_gem->reusable = false;
+
+	drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
+
+	DBG("bo_create_userptr: "
+	    "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
+		addr, bo_gem->gem_handle, bo_gem->name,
+		size, stride, tiling_mode);
+
+	return &bo_gem->bo;
+}
+
 /**
  * Returns a drm_intel_bo wrapping the given buffer object handle.
  *
@@ -1173,6 +1257,12 @@ static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
 	struct drm_i915_gem_set_domain set_domain;
 	int ret;
 
+	if (bo_gem->is_userptr) {
+		/* Return the same user ptr */
+		bo->virtual = bo_gem->user_virtual;
+		return 0;
+	}
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count++ == 0)
@@ -1241,6 +1331,9 @@ map_gtt(drm_intel_bo *bo)
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	if (bo_gem->map_count++ == 0)
 		drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
 
@@ -1385,13 +1478,18 @@ int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
 
 static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
 {
-	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+	drm_intel_bufmgr_gem *bufmgr_gem;
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret = 0;
 
 	if (bo == NULL)
 		return 0;
 
+	if (bo_gem->is_userptr)
+		return 0;
+
+	bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count <= 0) {
@@ -1449,6 +1547,9 @@ drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pwrite pwrite;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pwrite);
 	pwrite.handle = bo_gem->gem_handle;
 	pwrite.offset = offset;
@@ -1501,6 +1602,9 @@ drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pread pread;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pread);
 	pread.handle = bo_gem->gem_handle;
 	pread.offset = offset;
@@ -2460,6 +2564,12 @@ drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	/* Linear buffers have no stride. By ensuring that we only ever use
 	 * stride 0 with linear buffers, we simplify our code.
 	 */
@@ -3181,6 +3291,44 @@ drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
 	bo_gem->aub_annotation_count = count;
 }
 
+static bool

intel/intel_bufmgr_gem.c drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo, · line 3304

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+has_userptr(int fd)
+{
+	int ret;
+	void *ptr;
+	long pgsz;
+	struct drm_i915_gem_userptr userptr;
+	struct drm_gem_close close_bo;
+
+	pgsz = sysconf(_SC_PAGESIZE);
+	assert(ret > 0);

You're asserting on an non initialized variable.

intel/intel_bufmgr_gem.c drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo, · line 3307

Quoted 3 lines
intel/intel_bufmgr_gem.c +3 -0
+
+	ret = posix_memalign(&ptr, pgsz, pgsz);
+	assert(ret == 0);

In general I'd avoid asserting in a library when it's not about asserting an invariant and properly handle the error case (by returning false here).

Quoted 9 lines
intel/intel_bufmgr_gem.c +9 -0
+
+	memset(&userptr, 0, sizeof(userptr));
+	userptr.user_ptr = (__u64)(unsigned long)ptr;
+	userptr.user_size = pgsz;
+
+retry:
+	ret = drmIoctl(fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
+	if (ret) {
+		if (errno == ENODEV && userptr.flags == 0) {

intel/intel_bufmgr_gem.c drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo, · line 3326

Quoted 10 lines
intel/intel_bufmgr_gem.c +10 -0
+			userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
+			goto retry;
+		}
+		free(ptr);
+		return false;
+	}
+
+	close_bo.handle = userptr.handle;
+	ret = drmIoctl(fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
+	assert(ret == 0);

Same remark.

Quoted 49 lines
intel/intel_bufmgr_gem.c +9 -0
+	free(ptr);
+
+	return true;
+}
+
 /**
  * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
  * and manage map buffer objections.
@@ -3273,6 +3421,10 @@ drm_intel_bufmgr_gem_init(int fd, int batch_size)
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_relaxed_fencing = ret == 0;
 
+	if (has_userptr(fd))
+		bufmgr_gem->bufmgr.bo_alloc_userptr =
+			drm_intel_gem_bo_alloc_userptr;
+
 	gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_wait_timeout = ret == 0;
intel/intel_bufmgr_priv.h +11 -1
index 2592d42..3aa1abb 100644
@@ -60,7 +60,17 @@ struct _drm_intel_bufmgr {
 					      const char *name,
 					      unsigned long size,
 					      unsigned int alignment);
-
+	/**
+	 * Allocate a buffer object from an existing user accessible
+	 * address malloc'd with the provided size.
+	 * Alignment is used when mapping to the gtt.
+	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
+	 */
+	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode, uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags);
 	/**
 	 * Allocate a tiled buffer object.
 	 *
Signature · 6 lines
1.9.0

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx
#10 lore raw
Quoted 18 lines · Damien Lespiau
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

Quoted 8 lines
---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
Quoted 4 lines

Apart from couple of remarks below I couldn't find anything that would prevent merging this. Well, except maybe that it'd be very nice to have some feedback from someone using it, we do have an API/ABI guarantee on libdrm after all.

Actually, if you're retouching this patch, maybe spliting the i915_drm.h update would be good, that's something we can push straight away.

Signature · 1 line
Damien
#11 lore raw
In reply to Damien Lespiau
Quoted 18 lines · Damien Lespiau
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

Quoted 8 lines
---
  include/drm/i915_drm.h    |  16 +++++
  intel/intel_bufmgr.c      |  13 ++++
  intel/intel_bufmgr.h      |   5 ++
  intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
  intel/intel_bufmgr_priv.h |  12 +++-
  5 files changed, 198 insertions(+), 2 deletions(-)
Quoted 4 lines

Apart from couple of remarks below I couldn't find anything that would prevent merging this. Well, except maybe that it'd be very nice to have some feedback from someone using it, we do have an API/ABI guarantee on libdrm after all.

Looks like I've forgotten to reply to this. I did address the other review comments and sent out a v2 back then.

But for what users are concerned, apart from internal ones who have been using this API for some years now, I don't know of any.

Tvrtko

#12 lore raw
Quoted 25 lines · Tvrtko Ursulin
Quoted 18 lines · Damien Lespiau
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

Quoted 8 lines
---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
Quoted 4 lines

Apart from couple of remarks below I couldn't find anything that would prevent merging this. Well, except maybe that it'd be very nice to have some feedback from someone using it, we do have an API/ABI guarantee on libdrm after all.

Quoted 5 lines

Looks like I've forgotten to reply to this. I did address the other review comments and sent out a v2 back then.

But for what users are concerned, apart from internal ones who have been using this API for some years now, I don't know of any.

Well, considering this is only a wrapper of an ioctl() already upstreamed, I'm inclined to just push it as is. Daniel any thoughts?

Signature · 1 line
Damien
#13 lore raw
Quoted 26 lines · Damien Lespiau
Quoted 25 lines · Tvrtko Ursulin
Quoted 22 lines · Damien Lespiau
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

Quoted 8 lines
---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)

Apart from couple of remarks below I couldn't find anything that would prevent merging this. Well, except maybe that it'd be very nice to have some feedback from someone using it, we do have an API/ABI guarantee on libdrm after all.

Quoted 8 lines
Quoted 5 lines

Looks like I've forgotten to reply to this. I did address the other review comments and sent out a v2 back then.

But for what users are concerned, apart from internal ones who have been using this API for some years now, I don't know of any.

Well, considering this is only a wrapper of an ioctl() already upstreamed, I'm inclined to just push it as is. Daniel any thoughts?

Since both igt and sna have their own wrappers and the beinget patch for this hasn't surfaced yet we don't really have a public open-source user for this yet. My understanding of Dave's stance is that we should hold off with committing until this is requirement is fulfilled. -Daniel

Signature · 3 lines
Daniel Vetter
Software Engineer, Intel Corporation
+41 (0) 79 365 57 48 - http://blog.ffwll.ch
#14 lore raw
Quoted 33 lines · Daniel Vetter
Quoted 32 lines · Damien Lespiau
Quoted 30 lines · Tvrtko Ursulin
Quoted 18 lines · Damien Lespiau
Quoted 8 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

Quoted 8 lines
---
 include/drm/i915_drm.h    |  16 +++++
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 154 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 5 files changed, 198 insertions(+), 2 deletions(-)
Quoted 4 lines

Apart from couple of remarks below I couldn't find anything that would prevent merging this. Well, except maybe that it'd be very nice to have some feedback from someone using it, we do have an API/ABI guarantee on libdrm after all.

Looks like I've forgotten to reply to this. I did address the other review comments and sent out a v2 back then.

But for what users are concerned, apart from internal ones who have been using this API for some years now, I don't know of any.

Quoted 7 lines
Quoted 2 lines

Well, considering this is only a wrapper of an ioctl() already upstreamed, I'm inclined to just push it as is. Daniel any thoughts?

Since both igt and sna have their own wrappers and the beinget patch for this hasn't surfaced yet we don't really have a public open-source user for this yet. My understanding of Dave's stance is that we should hold off with committing until this is requirement is fulfilled.

The interface is fairly ugly since it combines the tiling into the create, which is just as convenient as a second step.

But if you want a user, just wire up an accelerated glReadPixels. Now, this is only really beneficial as the current ReadPixels is not well optimised and it assumes that userptr drm_intel_bo is automatically synced on destruction:

src/mesa/drivers/dri/i965/intel_pixel_read.c +98 -0
index beb3152..1b1f7e4 100644
@@ -161,6 +161,103 @@ do_blit_readpixels(struct gl_context * ctx,
    return true;
 }
 
+static bool
+do_userptr_readpixels(struct gl_context * ctx,
+                     GLint x, GLint y, GLsizei width, GLsizei height,
+                     GLenum format, GLenum type,
+                     const struct gl_pixelstore_attrib *pack, GLvoid * pixels)
+{
+   struct brw_context *brw = brw_context(ctx);
+   GLuint dst_offset;
+   drm_intel_bo *dst_buffer;
+   GLint dst_x, dst_y;
+   GLuint dirty;
+
+   DBG("%s\n", __FUNCTION__);
+
+   assert(_mesa_is_bufferobj(pack->BufferObj));
+
+   struct gl_renderbuffer *rb = ctx->ReadBuffer->_ColorReadBuffer;
+   struct intel_renderbuffer *irb = intel_renderbuffer(rb);
+   uintptr_t start, end;
+
+   /* Currently this function only supports reading from color buffers. */
+   if (!_mesa_is_color_format(format))
+      return false;
+
+   assert(irb != NULL);
+
+   if (ctx->_ImageTransferState ||
+       !_mesa_format_matches_format_and_type(irb->mt->format, format, type,
+                                             false)) {
+      DBG("%s - bad format for blit\n", __FUNCTION__);
+      return false;
+   }
+
+   if (pack->SwapBytes || pack->LsbFirst) {
+      DBG("%s: bad packing params\n", __FUNCTION__);
+      return false;
+   }
+
+   int dst_stride = _mesa_image_row_stride(pack, width, format, type);
+   bool dst_flip = false;
+   /* Mesa flips the dst_stride for pack->Invert, but we want our mt to have a
+    * normal dst_stride.
+    */
+   struct gl_pixelstore_attrib uninverted_pack = *pack;
+   if (pack->Invert) {
+      dst_stride = -dst_stride;
+      dst_flip = true;
+      uninverted_pack.Invert = false;
+   }
+
+   dst_offset = (GLintptr)pixels;
+   dst_offset += _mesa_image_offset(2, &uninverted_pack, width, height,
+                                   format, type, 0, 0, 0);
+
+   if (!_mesa_clip_copytexsubimage(ctx,
+                                  &dst_x, &dst_y,
+                                  &x, &y,
+                                  &width, &height)) {
+      return true;
+   }
+
+   dirty = brw->front_buffer_dirty;
+   intel_prepare_render(brw);
+   brw->front_buffer_dirty = dirty;
+
+   start = (uintptr_t)(pixel + dst_offset) & ~4095;
+   end = (uintptr_t)(pixel + dst_offset + dst_stride * height + 4095) & ~4095;
+
+   dst_buffer = drm_intel_bo_alloc_userptr(bufmgr, "glReadPixels",
+                                          (void *)start,
+                                          I915_TILING_NONE, dst_stride,
+                                          end - start, 0);
+
+   struct intel_mipmap_tree *pbo_mt =
+      intel_miptree_create_for_bo(brw,
+                                  dst_buffer,
+                                  irb->mt->format,
+                                  (uintptr_t)(pixel + dst_offset) - start,
+                                  width, height,
+                                  dst_stride);
+
+   if (!intel_miptree_blit(brw,
+                           irb->mt, irb->mt_level, irb->mt_layer,
+                           x, y, _mesa_is_winsys_fbo(ctx->ReadBuffer),
+                           pbo_mt, 0, 0,
+                           0, 0, dst_flip,
+                           width, height, GL_COPY)) {
+      return false;
+   }
+
+   intel_miptree_release(&pbo_mt);
+   drm_intel_bo_unreference(dst_buffer);
+
+   DBG("%s - DONE\n", __FUNCTION__);
+
+   return true;
+}
+
 void
 intelReadPixels(struct gl_context * ctx,

GLint x, GLint y, GLsizei width, GLsizei height,
@@ -182,6 +280,13 @@ intelReadPixels(struct gl_context * ctx,

       perf_debug("%s: fallback to CPU mapping in PBO case\n", __FUNCTION__);
    }

+ if (do_userptr_readpixels(ctx,

+                            x, y, width, height,
+                            format, type,
+                            pack, pixels)) {
+      return;
+   }
+
    /* glReadPixels() wont dirty the front buffer, so reset the dirty
     * flag after calling intel_prepare_render(). */
    dirty = brw->front_buffer_dirty;
Signature · 1 line
Chris Wilson, Intel Open Source Technology Centre
#15 lore raw

PATCH 1/1 intel: Add support for userptr objects

In reply to Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

v2: Improved error handling in feature detection per review comments.

---
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 163 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 4 files changed, 191 insertions(+), 2 deletions(-)
intel/intel_bufmgr.c +13 -0
index 905556f..7f3d795 100644
@@ -60,6 +60,19 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 	return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
 }
 
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode,
+					  uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags)
+{
+	if (bufmgr->bo_alloc_userptr)
+		return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
+						stride, size, flags);
+	return NULL;
+}
+
 drm_intel_bo *
 drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
                         int x, int y, int cpp, uint32_t *tiling_mode,
intel/intel_bufmgr.h +5 -0
index 9383c72..be83a56 100644
@@ -113,6 +113,11 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 					    const char *name,
 					    unsigned long size,
 					    unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					const char *name,
+					void *addr, uint32_t tiling_mode,
+					uint32_t stride, unsigned long size,
+					unsigned long flags);
 drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
 				       const char *name,
 				       int x, int y, int cpp,
intel/intel_bufmgr_gem.c +162 -1

235 lines not on this page · raw message

intel/intel_bufmgr_priv.h +11 -1
index 2592d42..3aa1abb 100644
@@ -60,7 +60,17 @@ struct _drm_intel_bufmgr {
 					      const char *name,
 					      unsigned long size,
 					      unsigned int alignment);
-
+	/**
+	 * Allocate a buffer object from an existing user accessible
+	 * address malloc'd with the provided size.
+	 * Alignment is used when mapping to the gtt.
+	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
+	 */
+	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode, uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags);
 	/**
 	 * Allocate a tiled buffer object.
 	 *
Signature · 1 line
1.9.3
Reviewed-by on v1 1/1
#16 lore raw

Re: [PATCH] intel: Add support for userptr objects

Quoted 4 lines · Tvrtko Ursulin

From: Tvrtko Ursulin <[email protected]>

Allow userptr objects to be created and used via libdrm_intel.

Quoted 8 lines

At the moment tiling and mapping to GTT aperture is not supported due hardware limitations across different generations and uncertainty about its usefulness.

v2: Improved error handling in feature detection per review comments.

---

Pushed a slightly modified version of this patch as libdrm now has explicit symbol visibility.

Signature · 1 line
Damien
Quoted 326 lines
 intel/intel_bufmgr.c      |  13 ++++
 intel/intel_bufmgr.h      |   5 ++
 intel/intel_bufmgr_gem.c  | 163 +++++++++++++++++++++++++++++++++++++++++++++-
 intel/intel_bufmgr_priv.h |  12 +++-
 4 files changed, 191 insertions(+), 2 deletions(-)
intel/intel_bufmgr.c +13 -0
index 905556f..7f3d795 100644
@@ -60,6 +60,19 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 	return bufmgr->bo_alloc_for_render(bufmgr, name, size, alignment);
 }
 
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode,
+					  uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags)
+{
+	if (bufmgr->bo_alloc_userptr)
+		return bufmgr->bo_alloc_userptr(bufmgr, name, addr, tiling_mode,
+						stride, size, flags);
+	return NULL;
+}
+
 drm_intel_bo *
 drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
                         int x, int y, int cpp, uint32_t *tiling_mode,
intel/intel_bufmgr.h +5 -0
index 9383c72..be83a56 100644
@@ -113,6 +113,11 @@ drm_intel_bo *drm_intel_bo_alloc_for_render(drm_intel_bufmgr *bufmgr,
 					    const char *name,
 					    unsigned long size,
 					    unsigned int alignment);
+drm_intel_bo *drm_intel_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+					const char *name,
+					void *addr, uint32_t tiling_mode,
+					uint32_t stride, unsigned long size,
+					unsigned long flags);
 drm_intel_bo *drm_intel_bo_alloc_tiled(drm_intel_bufmgr *bufmgr,
 				       const char *name,
 				       int x, int y, int cpp,
intel/intel_bufmgr_gem.c +162 -1
index 007a6d8..6dd3986 100644
@@ -182,6 +182,11 @@ struct _drm_intel_bo_gem {
 	void *mem_virtual;
 	/** GTT virtual address for the buffer, saved across map/unmap cycles */
 	void *gtt_virtual;
+	/**
+	 * Virtual address of the buffer allocated by user, used for userptr
+	 * objects only.
+	 */
+	void *user_virtual;
 	int map_count;
 	drmMMListHead vma_list;
 
@@ -221,6 +226,11 @@ struct _drm_intel_bo_gem {
 	bool idle;
 
 	/**
+	 * Boolean of whether this buffer was allocated with userptr
+	 */
+	bool is_userptr;
+
+	/**
 	 * Size in bytes of this buffer and its relocation descendents.
 	 *
 	 * Used to avoid costly tree walking in
@@ -847,6 +857,80 @@ drm_intel_gem_bo_alloc_tiled(drm_intel_bufmgr *bufmgr, const char *name,
 					       tiling, stride);
 }
 
+static drm_intel_bo *
+drm_intel_gem_bo_alloc_userptr(drm_intel_bufmgr *bufmgr,
+				const char *name,
+				void *addr,
+				uint32_t tiling_mode,
+				uint32_t stride,
+				unsigned long size,
+				unsigned long flags)
+{
+	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bufmgr;
+	drm_intel_bo_gem *bo_gem;
+	int ret;
+	struct drm_i915_gem_userptr userptr;
+
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (tiling_mode != I915_TILING_NONE)
+		return NULL;
+
+	bo_gem = calloc(1, sizeof(*bo_gem));
+	if (!bo_gem)
+		return NULL;
+
+	bo_gem->bo.size = size;
+
+	VG_CLEAR(userptr);
+	userptr.user_ptr = (__u64)((unsigned long)addr);
+	userptr.user_size = size;
+	userptr.flags = flags;
+
+	ret = drmIoctl(bufmgr_gem->fd,
+			DRM_IOCTL_I915_GEM_USERPTR,
+			&userptr);
+	if (ret != 0) {
+		DBG("bo_create_userptr: "
+		    "ioctl failed with user ptr %p size 0x%lx, "
+		    "user flags 0x%lx\n", addr, size, flags);
+		free(bo_gem);
+		return NULL;
+	}
+
+	bo_gem->gem_handle = userptr.handle;
+	bo_gem->bo.handle = bo_gem->gem_handle;
+	bo_gem->bo.bufmgr    = bufmgr;
+	bo_gem->is_userptr   = true;
+	bo_gem->bo.virtual   = addr;
+	/* Save the address provided by user */
+	bo_gem->user_virtual = addr;
+	bo_gem->tiling_mode  = I915_TILING_NONE;
+	bo_gem->swizzle_mode = I915_BIT_6_SWIZZLE_NONE;
+	bo_gem->stride       = 0;
+
+	DRMINITLISTHEAD(&bo_gem->name_list);
+	DRMINITLISTHEAD(&bo_gem->vma_list);
+
+	bo_gem->name = name;
+	atomic_set(&bo_gem->refcount, 1);
+	bo_gem->validate_index = -1;
+	bo_gem->reloc_tree_fences = 0;
+	bo_gem->used_as_reloc_target = false;
+	bo_gem->has_error = false;
+	bo_gem->reusable = false;
+
+	drm_intel_bo_gem_set_in_aperture_size(bufmgr_gem, bo_gem);
+
+	DBG("bo_create_userptr: "
+	    "ptr %p buf %d (%s) size %ldb, stride 0x%x, tile mode %d\n",
+		addr, bo_gem->gem_handle, bo_gem->name,
+		size, stride, tiling_mode);
+
+	return &bo_gem->bo;
+}
+
 /**
  * Returns a drm_intel_bo wrapping the given buffer object handle.
  *
@@ -1173,6 +1257,12 @@ static int drm_intel_gem_bo_map(drm_intel_bo *bo, int write_enable)
 	struct drm_i915_gem_set_domain set_domain;
 	int ret;
 
+	if (bo_gem->is_userptr) {
+		/* Return the same user ptr */
+		bo->virtual = bo_gem->user_virtual;
+		return 0;
+	}
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count++ == 0)
@@ -1241,6 +1331,9 @@ map_gtt(drm_intel_bo *bo)
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	if (bo_gem->map_count++ == 0)
 		drm_intel_gem_bo_open_vma(bufmgr_gem, bo_gem);
 
@@ -1385,13 +1478,18 @@ int drm_intel_gem_bo_map_unsynchronized(drm_intel_bo *bo)
 
 static int drm_intel_gem_bo_unmap(drm_intel_bo *bo)
 {
-	drm_intel_bufmgr_gem *bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+	drm_intel_bufmgr_gem *bufmgr_gem;
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret = 0;
 
 	if (bo == NULL)
 		return 0;
 
+	if (bo_gem->is_userptr)
+		return 0;
+
+	bufmgr_gem = (drm_intel_bufmgr_gem *) bo->bufmgr;
+
 	pthread_mutex_lock(&bufmgr_gem->lock);
 
 	if (bo_gem->map_count <= 0) {
@@ -1449,6 +1547,9 @@ drm_intel_gem_bo_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pwrite pwrite;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pwrite);
 	pwrite.handle = bo_gem->gem_handle;
 	pwrite.offset = offset;
@@ -1501,6 +1602,9 @@ drm_intel_gem_bo_get_subdata(drm_intel_bo *bo, unsigned long offset,
 	struct drm_i915_gem_pread pread;
 	int ret;
 
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	VG_CLEAR(pread);
 	pread.handle = bo_gem->gem_handle;
 	pread.offset = offset;
@@ -2460,6 +2564,12 @@ drm_intel_gem_bo_set_tiling(drm_intel_bo *bo, uint32_t * tiling_mode,
 	drm_intel_bo_gem *bo_gem = (drm_intel_bo_gem *) bo;
 	int ret;
 
+	/* Tiling with userptr surfaces is not supported
+	 * on all hardware so refuse it for time being.
+	 */
+	if (bo_gem->is_userptr)
+		return -EINVAL;
+
 	/* Linear buffers have no stride. By ensuring that we only ever use
 	 * stride 0 with linear buffers, we simplify our code.
 	 */
@@ -3181,6 +3291,53 @@ drm_intel_bufmgr_gem_set_aub_annotations(drm_intel_bo *bo,
 	bo_gem->aub_annotation_count = count;
 }
 
+static bool
+has_userptr(drm_intel_bufmgr_gem *bufmgr_gem)
+{
+	int ret;
+	void *ptr;
+	long pgsz;
+	struct drm_i915_gem_userptr userptr;
+	struct drm_gem_close close_bo;
+
+	pgsz = sysconf(_SC_PAGESIZE);
+	assert(pgsz > 0);
+
+	ret = posix_memalign(&ptr, pgsz, pgsz);
+	if (ret) {
+		DBG("Failed to get a page (%ld) for userptr detection!\n",
+			pgsz);
+		return false;
+	}
+
+	memset(&userptr, 0, sizeof(userptr));
+	userptr.user_ptr = (__u64)(unsigned long)ptr;
+	userptr.user_size = pgsz;
+
+retry:
+	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GEM_USERPTR, &userptr);
+	if (ret) {
+		if (errno == ENODEV && userptr.flags == 0) {
+			userptr.flags = I915_USERPTR_UNSYNCHRONIZED;
+			goto retry;
+		}
+		free(ptr);
+		return false;
+	}
+
+	close_bo.handle = userptr.handle;
+	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_GEM_CLOSE, &close_bo);
+	if (ret == 0) {
+		free(ptr);
+	} else {
+		fprintf(stderr, "Failed to release test userptr object! (%d) "
+				"i915 kernel driver may not be sane!\n", errno);
+		return false;
+	}
+
+	return true;
+}
+
 /**
  * Initializes the GEM buffer manager, which uses the kernel to allocate, map,
  * and manage map buffer objections.
@@ -3273,6 +3430,10 @@ drm_intel_bufmgr_gem_init(int fd, int batch_size)
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_relaxed_fencing = ret == 0;
 
+	if (has_userptr(bufmgr_gem))
+		bufmgr_gem->bufmgr.bo_alloc_userptr =
+			drm_intel_gem_bo_alloc_userptr;
+
 	gp.param = I915_PARAM_HAS_WAIT_TIMEOUT;
 	ret = drmIoctl(bufmgr_gem->fd, DRM_IOCTL_I915_GETPARAM, &gp);
 	bufmgr_gem->has_wait_timeout = ret == 0;
intel/intel_bufmgr_priv.h +11 -1
index 2592d42..3aa1abb 100644
@@ -60,7 +60,17 @@ struct _drm_intel_bufmgr {
 					      const char *name,
 					      unsigned long size,
 					      unsigned int alignment);
-
+	/**
+	 * Allocate a buffer object from an existing user accessible
+	 * address malloc'd with the provided size.
+	 * Alignment is used when mapping to the gtt.
+	 * Flags may be I915_VMAP_READ_ONLY or I915_USERPTR_UNSYNCHRONIZED
+	 */
+	drm_intel_bo *(*bo_alloc_userptr)(drm_intel_bufmgr *bufmgr,
+					  const char *name, void *addr,
+					  uint32_t tiling_mode, uint32_t stride,
+					  unsigned long size,
+					  unsigned long flags);
 	/**
 	 * Allocate a tiled buffer object.
 	 *
Signature · 6 lines
1.9.3

_______________________________________________
Intel-gfx mailing list
[email protected]
http://lists.freedesktop.org/mailman/listinfo/intel-gfx