drm/i915: Create a fake object for testing huge allocations
authorChris Wilson <chris@chris-wilson.co.uk>
Mon, 13 Feb 2017 17:15:20 +0000 (17:15 +0000)
committerChris Wilson <chris@chris-wilson.co.uk>
Mon, 13 Feb 2017 20:45:34 +0000 (20:45 +0000)
We would like to be able to exercise huge allocations even on memory
constrained devices. To do this we create an object that allocates only
a few pages and remaps them across its whole range - each page is reused
multiple times. We can therefore pretend we are rendering into a much
larger object.

Signed-off-by: Chris Wilson <chris@chris-wilson.co.uk>
Reviewed-by: Matthew Auld <matthew.auld@intel.com>
Link: http://patchwork.freedesktop.org/patch/msgid/20170213171558.20942-9-chris@chris-wilson.co.uk
drivers/gpu/drm/i915/i915_gem.c
drivers/gpu/drm/i915/i915_gem_object.h
drivers/gpu/drm/i915/selftests/huge_gem_object.c [new file with mode: 0644]
drivers/gpu/drm/i915/selftests/huge_gem_object.h [new file with mode: 0644]

index db240a8..59e10b3 100644 (file)
@@ -5024,4 +5024,5 @@ i915_gem_object_get_dma_address(struct drm_i915_gem_object *obj,
 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
 #include "selftests/scatterlist.c"
 #include "selftests/mock_gem_device.c"
+#include "selftests/huge_gem_object.c"
 #endif
index 290eaa7..4114cc8 100644 (file)
@@ -167,14 +167,18 @@ struct drm_i915_gem_object {
        /** Record of address bit 17 of each page at last unbind. */
        unsigned long *bit_17;
 
-       struct i915_gem_userptr {
-               uintptr_t ptr;
-               unsigned read_only :1;
-
-               struct i915_mm_struct *mm;
-               struct i915_mmu_object *mmu_object;
-               struct work_struct *work;
-       } userptr;
+       union {
+               struct i915_gem_userptr {
+                       uintptr_t ptr;
+                       unsigned read_only :1;
+
+                       struct i915_mm_struct *mm;
+                       struct i915_mmu_object *mmu_object;
+                       struct work_struct *work;
+               } userptr;
+
+               unsigned long scratch;
+       };
 
        /** for phys allocated objects */
        struct drm_dma_handle *phys_handle;
diff --git a/drivers/gpu/drm/i915/selftests/huge_gem_object.c b/drivers/gpu/drm/i915/selftests/huge_gem_object.c
new file mode 100644 (file)
index 0000000..4e681fc
--- /dev/null
@@ -0,0 +1,135 @@
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#include "huge_gem_object.h"
+
+static void huge_free_pages(struct drm_i915_gem_object *obj,
+                           struct sg_table *pages)
+{
+       unsigned long nreal = obj->scratch / PAGE_SIZE;
+       struct scatterlist *sg;
+
+       for (sg = pages->sgl; sg && nreal--; sg = __sg_next(sg))
+               __free_page(sg_page(sg));
+
+       sg_free_table(pages);
+       kfree(pages);
+}
+
+static struct sg_table *
+huge_get_pages(struct drm_i915_gem_object *obj)
+{
+#define GFP (GFP_KERNEL | __GFP_NOWARN | __GFP_NORETRY)
+       const unsigned long nreal = obj->scratch / PAGE_SIZE;
+       const unsigned long npages = obj->base.size / PAGE_SIZE;
+       struct scatterlist *sg, *src, *end;
+       struct sg_table *pages;
+       unsigned long n;
+
+       pages = kmalloc(sizeof(*pages), GFP);
+       if (!pages)
+               return ERR_PTR(-ENOMEM);
+
+       if (sg_alloc_table(pages, npages, GFP)) {
+               kfree(pages);
+               return ERR_PTR(-ENOMEM);
+       }
+
+       sg = pages->sgl;
+       for (n = 0; n < nreal; n++) {
+               struct page *page;
+
+               page = alloc_page(GFP | __GFP_HIGHMEM);
+               if (!page) {
+                       sg_mark_end(sg);
+                       goto err;
+               }
+
+               sg_set_page(sg, page, PAGE_SIZE, 0);
+               sg = __sg_next(sg);
+       }
+       if (nreal < npages) {
+               for (end = sg, src = pages->sgl; sg; sg = __sg_next(sg)) {
+                       sg_set_page(sg, sg_page(src), PAGE_SIZE, 0);
+                       src = __sg_next(src);
+                       if (src == end)
+                               src = pages->sgl;
+               }
+       }
+
+       if (i915_gem_gtt_prepare_pages(obj, pages))
+               goto err;
+
+       return pages;
+
+err:
+       huge_free_pages(obj, pages);
+       return ERR_PTR(-ENOMEM);
+#undef GFP
+}
+
+static void huge_put_pages(struct drm_i915_gem_object *obj,
+                          struct sg_table *pages)
+{
+       i915_gem_gtt_finish_pages(obj, pages);
+       huge_free_pages(obj, pages);
+
+       obj->mm.dirty = false;
+}
+
+static const struct drm_i915_gem_object_ops huge_ops = {
+       .flags = I915_GEM_OBJECT_HAS_STRUCT_PAGE |
+                I915_GEM_OBJECT_IS_SHRINKABLE,
+       .get_pages = huge_get_pages,
+       .put_pages = huge_put_pages,
+};
+
+struct drm_i915_gem_object *
+huge_gem_object(struct drm_i915_private *i915,
+               phys_addr_t phys_size,
+               dma_addr_t dma_size)
+{
+       struct drm_i915_gem_object *obj;
+
+       GEM_BUG_ON(!phys_size || phys_size > dma_size);
+       GEM_BUG_ON(!IS_ALIGNED(phys_size, PAGE_SIZE));
+       GEM_BUG_ON(!IS_ALIGNED(dma_size, I915_GTT_PAGE_SIZE));
+
+       if (overflows_type(dma_size, obj->base.size))
+               return ERR_PTR(-E2BIG);
+
+       obj = i915_gem_object_alloc(i915);
+       if (!obj)
+               return ERR_PTR(-ENOMEM);
+
+       drm_gem_private_object_init(&i915->drm, &obj->base, dma_size);
+       i915_gem_object_init(obj, &huge_ops);
+
+       obj->base.write_domain = I915_GEM_DOMAIN_CPU;
+       obj->base.read_domains = I915_GEM_DOMAIN_CPU;
+       obj->cache_level = HAS_LLC(i915) ? I915_CACHE_LLC : I915_CACHE_NONE;
+       obj->scratch = phys_size;
+
+       return obj;
+}
diff --git a/drivers/gpu/drm/i915/selftests/huge_gem_object.h b/drivers/gpu/drm/i915/selftests/huge_gem_object.h
new file mode 100644 (file)
index 0000000..a6133a9
--- /dev/null
@@ -0,0 +1,45 @@
+/*
+ * Copyright © 2016 Intel Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice (including the next
+ * paragraph) shall be included in all copies or substantial portions of the
+ * Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
+ * IN THE SOFTWARE.
+ *
+ */
+
+#ifndef __HUGE_GEM_OBJECT_H
+#define __HUGE_GEM_OBJECT_H
+
+struct drm_i915_gem_object *
+huge_gem_object(struct drm_i915_private *i915,
+               phys_addr_t phys_size,
+               dma_addr_t dma_size);
+
+static inline phys_addr_t
+huge_gem_object_phys_size(struct drm_i915_gem_object *obj)
+{
+       return obj->scratch;
+}
+
+static inline dma_addr_t
+huge_gem_object_dma_size(struct drm_i915_gem_object *obj)
+{
+       return obj->base.size;
+}
+
+#endif /* !__HUGE_GEM_OBJECT_H */