diff mbox

[01/13,v4] drm/i915: Add i915_gem_object_create_from_data()

Message ID 1436466554-24806-2-git-send-email-david.s.gordon@intel.com (mailing list archive)
State New, archived
Headers show

Commit Message

Dave Gordon July 9, 2015, 6:29 p.m. UTC
i915_gem_object_create_from_data() is a generic function to save data
from a plain linear buffer in a new pageable gem object that can later
be accessed by the CPU and/or GPU.

We will need this for the microcontroller firmware loading support code.

Derived from i915_gem_object_write(), originally by Alex Dai

v2:
    Change of function: now allocates & fills a new object, rather than
        writing to an existing object
    New name courtesy of Chris Wilson
    Explicit domain-setting and other improvements per review comments
        by Chris Wilson & Daniel Vetter

v4:
    Rebased

Issue: VIZ-4884
Signed-off-by: Alex Dai <yu.dai@intel.com>
Signed-off-by: Dave Gordon <david.s.gordon@intel.com>
---
 drivers/gpu/drm/i915/i915_drv.h |  2 ++
 drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++++++++++++
 2 files changed, 42 insertions(+)

Comments

Tom.O'Rourke@intel.com July 18, 2015, 12:36 a.m. UTC | #1
On Thu, Jul 09, 2015 at 07:29:02PM +0100, Dave Gordon wrote:
> i915_gem_object_create_from_data() is a generic function to save data
> from a plain linear buffer in a new pageable gem object that can later
> be accessed by the CPU and/or GPU.
> 
> We will need this for the microcontroller firmware loading support code.
> 
> Derived from i915_gem_object_write(), originally by Alex Dai
> 
> v2:
>     Change of function: now allocates & fills a new object, rather than
>         writing to an existing object
>     New name courtesy of Chris Wilson
>     Explicit domain-setting and other improvements per review comments
>         by Chris Wilson & Daniel Vetter
> 
> v4:
>     Rebased
> 
> Issue: VIZ-4884
> Signed-off-by: Alex Dai <yu.dai@intel.com>
> Signed-off-by: Dave Gordon <david.s.gordon@intel.com>
> ---
Reviewed-by: Tom O'Rourke <Tom.O'Rourke@intel.com>
>  drivers/gpu/drm/i915/i915_drv.h |  2 ++
>  drivers/gpu/drm/i915/i915_gem.c | 40 ++++++++++++++++++++++++++++++++++++++++
>  2 files changed, 42 insertions(+)
> 
> diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
> index 464b28d..3c91507 100644
> --- a/drivers/gpu/drm/i915/i915_drv.h
> +++ b/drivers/gpu/drm/i915/i915_drv.h
> @@ -2755,6 +2755,8 @@ void i915_gem_object_init(struct drm_i915_gem_object *obj,
>  			 const struct drm_i915_gem_object_ops *ops);
>  struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
>  						  size_t size);
> +struct drm_i915_gem_object *i915_gem_object_create_from_data(
> +		struct drm_device *dev, const void *data, size_t size);
>  void i915_init_vm(struct drm_i915_private *dev_priv,
>  		  struct i915_address_space *vm);
>  void i915_gem_free_object(struct drm_gem_object *obj);
> diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c
> index a0bff41..dbbb649 100644
> --- a/drivers/gpu/drm/i915/i915_gem.c
> +++ b/drivers/gpu/drm/i915/i915_gem.c
> @@ -5478,3 +5478,43 @@ bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
>  
>  	return false;
>  }
> +
> +/* Allocate a new GEM object and fill it with the supplied data */
> +struct drm_i915_gem_object *
> +i915_gem_object_create_from_data(struct drm_device *dev,
> +			         const void *data, size_t size)
> +{
> +	struct drm_i915_gem_object *obj;
> +	struct sg_table *sg;
> +	size_t bytes;
> +	int ret;
> +
> +	obj = i915_gem_alloc_object(dev, round_up(size, PAGE_SIZE));
> +	if (IS_ERR_OR_NULL(obj))
> +		return obj;
> +
> +	ret = i915_gem_object_set_to_cpu_domain(obj, true);
> +	if (ret)
> +		goto fail;
> +
> +	ret = i915_gem_object_get_pages(obj);
> +	if (ret)
> +		goto fail;
> +
> +	i915_gem_object_pin_pages(obj);
> +	sg = obj->pages;
> +	bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
> +	i915_gem_object_unpin_pages(obj);
> +
> +	if (WARN_ON(bytes != size)) {
> +		DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
> +		ret = -EFAULT;
> +		goto fail;
> +	}
> +
> +	return obj;
> +
> +fail:
> +	drm_gem_object_unreference(&obj->base);
> +	return ERR_PTR(ret);
> +}
> -- 
> 1.9.1
>
diff mbox

Patch

diff --git a/drivers/gpu/drm/i915/i915_drv.h b/drivers/gpu/drm/i915/i915_drv.h
index 464b28d..3c91507 100644
--- a/drivers/gpu/drm/i915/i915_drv.h
+++ b/drivers/gpu/drm/i915/i915_drv.h
@@ -2755,6 +2755,8 @@  void i915_gem_object_init(struct drm_i915_gem_object *obj,
 			 const struct drm_i915_gem_object_ops *ops);
 struct drm_i915_gem_object *i915_gem_alloc_object(struct drm_device *dev,
 						  size_t size);
+struct drm_i915_gem_object *i915_gem_object_create_from_data(
+		struct drm_device *dev, const void *data, size_t size);
 void i915_init_vm(struct drm_i915_private *dev_priv,
 		  struct i915_address_space *vm);
 void i915_gem_free_object(struct drm_gem_object *obj);
diff --git a/drivers/gpu/drm/i915/i915_gem.c b/drivers/gpu/drm/i915/i915_gem.c
index a0bff41..dbbb649 100644
--- a/drivers/gpu/drm/i915/i915_gem.c
+++ b/drivers/gpu/drm/i915/i915_gem.c
@@ -5478,3 +5478,43 @@  bool i915_gem_obj_is_pinned(struct drm_i915_gem_object *obj)
 
 	return false;
 }
+
+/* Allocate a new GEM object and fill it with the supplied data */
+struct drm_i915_gem_object *
+i915_gem_object_create_from_data(struct drm_device *dev,
+			         const void *data, size_t size)
+{
+	struct drm_i915_gem_object *obj;
+	struct sg_table *sg;
+	size_t bytes;
+	int ret;
+
+	obj = i915_gem_alloc_object(dev, round_up(size, PAGE_SIZE));
+	if (IS_ERR_OR_NULL(obj))
+		return obj;
+
+	ret = i915_gem_object_set_to_cpu_domain(obj, true);
+	if (ret)
+		goto fail;
+
+	ret = i915_gem_object_get_pages(obj);
+	if (ret)
+		goto fail;
+
+	i915_gem_object_pin_pages(obj);
+	sg = obj->pages;
+	bytes = sg_copy_from_buffer(sg->sgl, sg->nents, (void *)data, size);
+	i915_gem_object_unpin_pages(obj);
+
+	if (WARN_ON(bytes != size)) {
+		DRM_ERROR("Incomplete copy, wrote %zu of %zu", bytes, size);
+		ret = -EFAULT;
+		goto fail;
+	}
+
+	return obj;
+
+fail:
+	drm_gem_object_unreference(&obj->base);
+	return ERR_PTR(ret);
+}