drm/i915: Add lmem fault handler
diff mbox series

Message ID 20191205101417.15054-1-abdiel.janulgue@linux.intel.com
State New
Headers show
Series
  • drm/i915: Add lmem fault handler
Related show

Commit Message

Abdiel Janulgue Dec. 5, 2019, 10:14 a.m. UTC
Fault handler to handle missing pages for lmem objects.

Signed-off-by: Abdiel Janulgue <abdiel.janulgue@linux.intel.com>
Signed-off-by: Matthew Auld <matthew.auld@intel.com>
Cc: Chris Wilson <chris@chris-wilson.co.uk>
Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
---
 drivers/gpu/drm/i915/gem/i915_gem_lmem.c | 43 ++++++++++++++++++++++++
 drivers/gpu/drm/i915/gem/i915_gem_lmem.h |  6 ++++
 drivers/gpu/drm/i915/gem/i915_gem_mman.c | 15 +++++++--
 drivers/gpu/drm/i915/gem/i915_gem_mman.h |  1 +
 4 files changed, 63 insertions(+), 2 deletions(-)

Comments

Matthew Auld Dec. 5, 2019, 10:20 a.m. UTC | #1
On Thu, 5 Dec 2019 at 10:14, Abdiel Janulgue
<abdiel.janulgue@linux.intel.com> wrote:
>
> Fault handler to handle missing pages for lmem objects.
>
> Signed-off-by: Abdiel Janulgue <abdiel.janulgue@linux.intel.com>
> Signed-off-by: Matthew Auld <matthew.auld@intel.com>
> Cc: Chris Wilson <chris@chris-wilson.co.uk>
> Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
> ---
>  drivers/gpu/drm/i915/gem/i915_gem_lmem.c | 43 ++++++++++++++++++++++++
>  drivers/gpu/drm/i915/gem/i915_gem_lmem.h |  6 ++++
>  drivers/gpu/drm/i915/gem/i915_gem_mman.c | 15 +++++++--
>  drivers/gpu/drm/i915/gem/i915_gem_mman.h |  1 +
>  4 files changed, 63 insertions(+), 2 deletions(-)
>
> diff --git a/drivers/gpu/drm/i915/gem/i915_gem_lmem.c b/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
> index 0e2bf6b7e143..78ac8d160cd7 100644
> --- a/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
> +++ b/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
> @@ -6,8 +6,40 @@
>  #include "intel_memory_region.h"
>  #include "gem/i915_gem_region.h"
>  #include "gem/i915_gem_lmem.h"
> +#include "gem/i915_gem_mman.h"
>  #include "i915_drv.h"
>
> +vm_fault_t vm_fault_lmem(struct vm_fault *vmf)
> +{
> +       struct vm_area_struct *area = vmf->vma;
> +       struct i915_mmap_offset *priv = area->vm_private_data;
> +       struct drm_i915_gem_object *obj = priv->obj;
> +       unsigned long size = area->vm_end - area->vm_start;
> +       bool write = area->vm_flags & VM_WRITE;
> +       vm_fault_t vmf_ret;
> +       int i, ret;
> +
> +       /* Sanity check that we allow writing into this object */
> +       if (i915_gem_object_is_readonly(obj) && write)
> +               return VM_FAULT_SIGBUS;
> +
> +       ret = i915_gem_object_pin_pages(obj);
> +       if (ret)
> +               return i915_error_to_vmf_fault(ret);

Don't we need to handle -ENXIO in i915_error_to_vmf_fault?

> +
> +       for (i = 0; i < size >> PAGE_SHIFT; i++) {
> +               vmf_ret = vmf_insert_pfn(area,
> +                                        (unsigned long)area->vm_start + i * PAGE_SIZE,
> +                                        i915_gem_object_lmem_io_pfn(obj, i));
> +               if (vmf_ret != VM_FAULT_NOPAGE)
> +                       break;
> +       }
> +
> +       i915_gem_object_unpin_pages(obj);
> +
> +       return vmf_ret;
> +}
> +
>  const struct drm_i915_gem_object_ops i915_gem_lmem_obj_ops = {
>         .flags = I915_GEM_OBJECT_HAS_IOMEM,
>
> @@ -56,6 +88,17 @@ i915_gem_object_lmem_io_map(struct drm_i915_gem_object *obj,
>         return io_mapping_map_wc(&obj->mm.region->iomap, offset, size);
>  }
>
> +unsigned long i915_gem_object_lmem_io_pfn(struct drm_i915_gem_object *obj,
> +                                         unsigned long n)
> +{
> +       struct intel_memory_region *mem = obj->mm.region;
> +       resource_size_t offset;
> +
> +       offset = i915_gem_object_get_dma_address(obj, n);

offset -= mem->region.start; for poor old fake local-memory.
Chris Wilson Dec. 5, 2019, 10:31 a.m. UTC | #2
Quoting Matthew Auld (2019-12-05 10:20:55)
> On Thu, 5 Dec 2019 at 10:14, Abdiel Janulgue
> <abdiel.janulgue@linux.intel.com> wrote:
> >
> > Fault handler to handle missing pages for lmem objects.
> >
> > Signed-off-by: Abdiel Janulgue <abdiel.janulgue@linux.intel.com>
> > Signed-off-by: Matthew Auld <matthew.auld@intel.com>
> > Cc: Chris Wilson <chris@chris-wilson.co.uk>
> > Cc: Joonas Lahtinen <joonas.lahtinen@linux.intel.com>
> > ---
> >  drivers/gpu/drm/i915/gem/i915_gem_lmem.c | 43 ++++++++++++++++++++++++
> >  drivers/gpu/drm/i915/gem/i915_gem_lmem.h |  6 ++++
> >  drivers/gpu/drm/i915/gem/i915_gem_mman.c | 15 +++++++--
> >  drivers/gpu/drm/i915/gem/i915_gem_mman.h |  1 +
> >  4 files changed, 63 insertions(+), 2 deletions(-)
> >
> > diff --git a/drivers/gpu/drm/i915/gem/i915_gem_lmem.c b/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
> > index 0e2bf6b7e143..78ac8d160cd7 100644
> > --- a/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
> > +++ b/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
> > @@ -6,8 +6,40 @@
> >  #include "intel_memory_region.h"
> >  #include "gem/i915_gem_region.h"
> >  #include "gem/i915_gem_lmem.h"
> > +#include "gem/i915_gem_mman.h"
> >  #include "i915_drv.h"
> >
> > +vm_fault_t vm_fault_lmem(struct vm_fault *vmf)
> > +{
> > +       struct vm_area_struct *area = vmf->vma;
> > +       struct i915_mmap_offset *priv = area->vm_private_data;
> > +       struct drm_i915_gem_object *obj = priv->obj;
> > +       unsigned long size = area->vm_end - area->vm_start;
> > +       bool write = area->vm_flags & VM_WRITE;
> > +       vm_fault_t vmf_ret;
> > +       int i, ret;
> > +
> > +       /* Sanity check that we allow writing into this object */
> > +       if (i915_gem_object_is_readonly(obj) && write)
> > +               return VM_FAULT_SIGBUS;
> > +
> > +       ret = i915_gem_object_pin_pages(obj);
> > +       if (ret)
> > +               return i915_error_to_vmf_fault(ret);
> 
> Don't we need to handle -ENXIO in i915_error_to_vmf_fault?
> 
> > +
> > +       for (i = 0; i < size >> PAGE_SHIFT; i++) {
> > +               vmf_ret = vmf_insert_pfn(area,
> > +                                        (unsigned long)area->vm_start + i * PAGE_SIZE,
> > +                                        i915_gem_object_lmem_io_pfn(obj, i));
> > +               if (vmf_ret != VM_FAULT_NOPAGE)
> > +                       break;
> > +       }
> > +
> > +       i915_gem_object_unpin_pages(obj);
> > +
> > +       return vmf_ret;
> > +}
> > +
> >  const struct drm_i915_gem_object_ops i915_gem_lmem_obj_ops = {
> >         .flags = I915_GEM_OBJECT_HAS_IOMEM,
> >
> > @@ -56,6 +88,17 @@ i915_gem_object_lmem_io_map(struct drm_i915_gem_object *obj,
> >         return io_mapping_map_wc(&obj->mm.region->iomap, offset, size);
> >  }
> >
> > +unsigned long i915_gem_object_lmem_io_pfn(struct drm_i915_gem_object *obj,
> > +                                         unsigned long n)
> > +{
> > +       struct intel_memory_region *mem = obj->mm.region;
> > +       resource_size_t offset;
> > +
> > +       offset = i915_gem_object_get_dma_address(obj, n);
> 
> offset -= mem->region.start; for poor old fake local-memory.

And so one asks for the selftests...
-Chris

Patch
diff mbox series

diff --git a/drivers/gpu/drm/i915/gem/i915_gem_lmem.c b/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
index 0e2bf6b7e143..78ac8d160cd7 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_lmem.c
@@ -6,8 +6,40 @@ 
 #include "intel_memory_region.h"
 #include "gem/i915_gem_region.h"
 #include "gem/i915_gem_lmem.h"
+#include "gem/i915_gem_mman.h"
 #include "i915_drv.h"
 
+vm_fault_t vm_fault_lmem(struct vm_fault *vmf)
+{
+	struct vm_area_struct *area = vmf->vma;
+	struct i915_mmap_offset *priv = area->vm_private_data;
+	struct drm_i915_gem_object *obj = priv->obj;
+	unsigned long size = area->vm_end - area->vm_start;
+	bool write = area->vm_flags & VM_WRITE;
+	vm_fault_t vmf_ret;
+	int i, ret;
+
+	/* Sanity check that we allow writing into this object */
+	if (i915_gem_object_is_readonly(obj) && write)
+		return VM_FAULT_SIGBUS;
+
+	ret = i915_gem_object_pin_pages(obj);
+	if (ret)
+		return i915_error_to_vmf_fault(ret);
+
+	for (i = 0; i < size >> PAGE_SHIFT; i++) {
+		vmf_ret = vmf_insert_pfn(area,
+					 (unsigned long)area->vm_start + i * PAGE_SIZE,
+					 i915_gem_object_lmem_io_pfn(obj, i));
+		if (vmf_ret != VM_FAULT_NOPAGE)
+			break;
+	}
+
+	i915_gem_object_unpin_pages(obj);
+
+	return vmf_ret;
+}
+
 const struct drm_i915_gem_object_ops i915_gem_lmem_obj_ops = {
 	.flags = I915_GEM_OBJECT_HAS_IOMEM,
 
@@ -56,6 +88,17 @@  i915_gem_object_lmem_io_map(struct drm_i915_gem_object *obj,
 	return io_mapping_map_wc(&obj->mm.region->iomap, offset, size);
 }
 
+unsigned long i915_gem_object_lmem_io_pfn(struct drm_i915_gem_object *obj,
+					  unsigned long n)
+{
+	struct intel_memory_region *mem = obj->mm.region;
+	resource_size_t offset;
+
+	offset = i915_gem_object_get_dma_address(obj, n);
+
+	return (mem->io_start + offset) >> PAGE_SHIFT;
+}
+
 bool i915_gem_object_is_lmem(struct drm_i915_gem_object *obj)
 {
 	return obj->ops == &i915_gem_lmem_obj_ops;
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_lmem.h b/drivers/gpu/drm/i915/gem/i915_gem_lmem.h
index 7c176b8b7d2f..917ebef1529f 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_lmem.h
+++ b/drivers/gpu/drm/i915/gem/i915_gem_lmem.h
@@ -7,6 +7,7 @@ 
 #define __I915_GEM_LMEM_H
 
 #include <linux/types.h>
+#include <linux/mman.h>
 
 struct drm_i915_private;
 struct drm_i915_gem_object;
@@ -22,8 +23,13 @@  void __iomem *
 i915_gem_object_lmem_io_map_page_atomic(struct drm_i915_gem_object *obj,
 					unsigned long n);
 
+unsigned long i915_gem_object_lmem_io_pfn(struct drm_i915_gem_object *obj,
+					  unsigned long n);
+
 bool i915_gem_object_is_lmem(struct drm_i915_gem_object *obj);
 
+vm_fault_t vm_fault_lmem(struct vm_fault *vmf);
+
 struct drm_i915_gem_object *
 i915_gem_object_create_lmem(struct drm_i915_private *i915,
 			    resource_size_t size,
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_mman.c b/drivers/gpu/drm/i915/gem/i915_gem_mman.c
index 3a3f30bc8ac7..5f6451ede53d 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_mman.c
+++ b/drivers/gpu/drm/i915/gem/i915_gem_mman.c
@@ -11,6 +11,7 @@ 
 #include "gt/intel_gt.h"
 #include "gt/intel_gt_requests.h"
 
+#include "i915_gem_lmem.h"
 #include "i915_drv.h"
 #include "i915_gem_gtt.h"
 #include "i915_gem_ioctls.h"
@@ -203,7 +204,7 @@  compute_partial_view(const struct drm_i915_gem_object *obj,
 	return view;
 }
 
-static vm_fault_t i915_error_to_vmf_fault(int err)
+vm_fault_t i915_error_to_vmf_fault(int err)
 {
 	switch (err) {
 	default:
@@ -560,7 +561,8 @@  __assign_mmap_offset(struct drm_file *file,
 	}
 
 	if (mmap_type != I915_MMAP_TYPE_GTT &&
-	    !i915_gem_object_has_struct_page(obj)) {
+	    !i915_gem_object_has_struct_page(obj) &&
+	    !i915_gem_object_is_lmem(obj)) {
 		err = -ENODEV;
 		goto out;
 	}
@@ -685,6 +687,12 @@  static const struct vm_operations_struct vm_ops_cpu = {
 	.close = vm_close,
 };
 
+static const struct vm_operations_struct vm_ops_lmem = {
+       .fault = vm_fault_lmem,
+       .open = vm_open,
+       .close = vm_close,
+};
+
 /*
  * This overcomes the limitation in drm_gem_mmap's assignment of a
  * drm_gem_object as the vma->vm_private_data. Since we need to
@@ -775,6 +783,9 @@  int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma)
 	}
 	vma->vm_page_prot = pgprot_decrypted(vma->vm_page_prot);
 
+	if (i915_gem_object_is_lmem(mmo->obj))
+		vma->vm_ops = &vm_ops_lmem;
+
 	return 0;
 }
 
diff --git a/drivers/gpu/drm/i915/gem/i915_gem_mman.h b/drivers/gpu/drm/i915/gem/i915_gem_mman.h
index 862e01b7cb69..aded4d0564c9 100644
--- a/drivers/gpu/drm/i915/gem/i915_gem_mman.h
+++ b/drivers/gpu/drm/i915/gem/i915_gem_mman.h
@@ -23,6 +23,7 @@  int i915_gem_mmap(struct file *filp, struct vm_area_struct *vma);
 int i915_gem_dumb_mmap_offset(struct drm_file *file_priv,
 			      struct drm_device *dev,
 			      u32 handle, u64 *offset);
+vm_fault_t i915_error_to_vmf_fault(int err);
 
 void __i915_gem_object_release_mmap_gtt(struct drm_i915_gem_object *obj);
 void i915_gem_object_release_mmap(struct drm_i915_gem_object *obj);