From 796ad6fe975a58112565fa4f84c24fe0af46c9e2 Mon Sep 17 00:00:00 2001 From: Chris Wilson Date: Tue, 31 Jul 2018 00:52:34 +0100 Subject: [PATCH] iris: Wrap userptr for creating bo --- src/gallium/drivers/iris/iris_bufmgr.c | 53 +++++++++++++++++++++++++- src/gallium/drivers/iris/iris_bufmgr.h | 10 +++++ 2 files changed, 62 insertions(+), 1 deletion(-) diff --git a/src/gallium/drivers/iris/iris_bufmgr.c b/src/gallium/drivers/iris/iris_bufmgr.c index 3a43371b00d..72964c4f068 100644 --- a/src/gallium/drivers/iris/iris_bufmgr.c +++ b/src/gallium/drivers/iris/iris_bufmgr.c @@ -652,6 +652,57 @@ iris_bo_alloc_tiled(struct iris_bufmgr *bufmgr, const char *name, flags, tiling_mode, pitch); } +struct iris_bo * +iris_bo_create_userptr(struct iris_bufmgr *bufmgr, const char *name, + void *ptr, size_t size, + enum iris_memory_zone memzone) +{ + struct iris_bo *bo; + + bo = calloc(1, sizeof(*bo)); + if (!bo) + return NULL; + + struct drm_i915_gem_userptr arg = { + .user_ptr = (uintptr_t)ptr, + .user_size = size, + }; + if (drm_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_USERPTR, &arg)) + goto err_free; + bo->gem_handle = arg.handle; + + /* Check the buffer for validity before we try and use it in a batch */ + struct drm_i915_gem_set_domain sd = { + .handle = bo->gem_handle, + .read_domains = I915_GEM_DOMAIN_CPU, + }; + if (drm_ioctl(bufmgr->fd, DRM_IOCTL_I915_GEM_SET_DOMAIN, &sd)) + goto err_close; + + bo->name = name; + bo->size = size; + bo->map_cpu = ptr; + + bo->bufmgr = bufmgr; + bo->kflags = EXEC_OBJECT_SUPPORTS_48B_ADDRESS | EXEC_OBJECT_PINNED; + bo->gtt_offset = vma_alloc(bufmgr, memzone, size, 1); + if (bo->gtt_offset == 0ull) + goto err_close; + + p_atomic_set(&bo->refcount, 1); + bo->cache_coherent = true; + bo->index = -1; + bo->idle = true; + + return bo; + +err_close: + drm_ioctl(bufmgr->fd, DRM_IOCTL_GEM_CLOSE, &bo->gem_handle); +err_free: + free(bo); + return NULL; +} + /** * Returns a iris_bo wrapping the given buffer object handle. * @@ -740,7 +791,7 @@ bo_free(struct iris_bo *bo) { struct iris_bufmgr *bufmgr = bo->bufmgr; - if (bo->map_cpu) { + if (bo->map_cpu && !bo->userptr) { VG_NOACCESS(bo->map_cpu, bo->size); munmap(bo->map_cpu, bo->size); } diff --git a/src/gallium/drivers/iris/iris_bufmgr.h b/src/gallium/drivers/iris/iris_bufmgr.h index b64dbee96b2..03cce6e70c0 100644 --- a/src/gallium/drivers/iris/iris_bufmgr.h +++ b/src/gallium/drivers/iris/iris_bufmgr.h @@ -182,6 +182,11 @@ struct iris_bo { * Boolean of whether this buffer is cache coherent */ bool cache_coherent; + + /** + * Boolean of whether this buffer points into user memory + */ + bool userptr; }; #define BO_ALLOC_ZEROED (1<<0) @@ -217,6 +222,11 @@ struct iris_bo *iris_bo_alloc_tiled(struct iris_bufmgr *bufmgr, unsigned flags, enum iris_memory_zone memzone); +struct iris_bo * +iris_bo_create_userptr(struct iris_bufmgr *bufmgr, const char *name, + void *ptr, size_t size, + enum iris_memory_zone memzone); + /** Takes a reference on a buffer object */ static inline void iris_bo_reference(struct iris_bo *bo) -- 2.30.2