[Intel-gfx] [PATCH 35/37] drm/i915: Live testing for context execution
Chris Wilson
chris at chris-wilson.co.uk
Wed Jan 11 21:09:35 UTC 2017
Check we can create and execution within a context.
Signed-off-by: Chris Wilson <chris at chris-wilson.co.uk>
---
drivers/gpu/drm/i915/i915_gem_context.c | 1 +
drivers/gpu/drm/i915/selftests/i915_gem_context.c | 302 +++++++++++++++++++++
.../gpu/drm/i915/selftests/i915_live_selftests.h | 1 +
3 files changed, 304 insertions(+)
create mode 100644 drivers/gpu/drm/i915/selftests/i915_gem_context.c
diff --git a/drivers/gpu/drm/i915/i915_gem_context.c b/drivers/gpu/drm/i915/i915_gem_context.c
index 6fcb35ecd6a7..9966dfd56a1b 100644
--- a/drivers/gpu/drm/i915/i915_gem_context.c
+++ b/drivers/gpu/drm/i915/i915_gem_context.c
@@ -1167,4 +1167,5 @@ int i915_gem_context_reset_stats_ioctl(struct drm_device *dev,
#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
#include "selftests/mock_context.c"
+#include "selftests/i915_gem_context.c"
#endif
diff --git a/drivers/gpu/drm/i915/selftests/i915_gem_context.c b/drivers/gpu/drm/i915/selftests/i915_gem_context.c
new file mode 100644
index 000000000000..53951dce67e9
--- /dev/null
+++ b/drivers/gpu/drm/i915/selftests/i915_gem_context.c
@@ -0,0 +1,302 @@
+/*
+ * Copyright © 2017 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 "i915_selftest.h"
+#include "fake_drm.h"
+#include "huge_gem_object.h"
+
+static struct i915_vma *vma_lookup(struct drm_i915_gem_object *obj,
+ struct i915_address_space *vm)
+{
+ return i915_gem_obj_lookup_or_create_vma(obj, vm, NULL);
+}
+
+static struct i915_vma *
+gpu_fill_pages(struct i915_vma *vma,
+ unsigned long first_page,
+ unsigned int offset_in_page,
+ unsigned long count,
+ u32 value)
+{
+ struct drm_i915_gem_object *obj;
+ const int gen = INTEL_GEN(vma->vm->i915);
+ unsigned long sz = (4*count + 1)*sizeof(u32);
+ u64 offset;
+ u32 *cmd;
+ int err;
+
+ GEM_BUG_ON(offset_in_page >= PAGE_SIZE);
+
+ obj = i915_gem_object_create_internal(vma->vm->i915,
+ round_up(sz, PAGE_SIZE));
+ if (IS_ERR(obj))
+ return ERR_CAST(obj);
+
+ cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
+ if (IS_ERR(cmd)) {
+ i915_gem_object_put(obj);
+ return ERR_CAST(cmd);
+ }
+
+ offset = PAGE_SIZE * first_page + offset_in_page;
+ offset += vma->node.start;
+ for (sz = 0; sz < count; sz++) {
+ if (gen >= 8) {
+ *cmd++ = MI_STORE_DWORD_IMM_GEN4;
+ *cmd++ = lower_32_bits(offset);
+ *cmd++ = upper_32_bits(offset);
+ *cmd++ = value;
+ } else if (gen >= 6) {
+ *cmd++ = MI_STORE_DWORD_IMM_GEN4;
+ *cmd++ = 0;
+ *cmd++ = offset;
+ *cmd++ = value;
+ } else if (gen >= 4) {
+ *cmd++ = MI_STORE_DWORD_IMM_GEN4 | 1 << 22;
+ *cmd++ = 0;
+ *cmd++ = offset;
+ *cmd++ = value;
+ } else {
+ *cmd++ = MI_STORE_DWORD_IMM | 1 << 22;
+ *cmd++ = offset;
+ *cmd++ = value;
+ }
+ offset += PAGE_SIZE;
+ }
+ *cmd = MI_BATCH_BUFFER_END;
+ i915_gem_object_unpin_map(obj);
+
+ err = i915_gem_object_set_to_gtt_domain(obj, false);
+ if (err) {
+ i915_gem_object_put(obj);
+ return ERR_PTR(err);
+ }
+
+ vma = vma_lookup(obj, vma->vm);
+ if (IS_ERR(vma)) {
+ i915_gem_object_put(obj);
+ return vma;
+ }
+
+ err = i915_vma_pin(vma, 0, 0, PIN_USER);
+ if (err) {
+ i915_gem_object_put(obj);
+ return ERR_PTR(err);
+ }
+
+ return vma;
+}
+
+static int gpu_fill(struct drm_i915_gem_object *obj,
+ struct i915_gem_context *ctx)
+{
+ struct drm_i915_private *i915 = to_i915(obj->base.dev);
+ const unsigned long npages = obj->base.size >> PAGE_SHIFT;
+ struct i915_address_space *vm = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
+ struct intel_engine_cs *engine = i915->engine[RCS];
+ struct i915_vma *vma;
+ unsigned long page;
+ int err;
+
+ vma = vma_lookup(obj, vm);
+ if (IS_ERR(vma))
+ return PTR_ERR(vma);
+
+ err = i915_gem_object_set_to_gtt_domain(obj, false);
+ if (err)
+ return err;
+
+ err = i915_vma_pin(vma, 0, 0, PIN_USER);
+ if (err)
+ return err;
+
+ GEM_BUG_ON(!IS_ALIGNED(npages, 1024));
+ for (page = 0; page < npages; page += 1024) {
+ unsigned int v = page / 1024;
+ struct drm_i915_gem_request *rq;
+ struct i915_vma *batch;
+
+ batch = gpu_fill_pages(vma, page, v*sizeof(u32), 1024, v);
+ if (IS_ERR(batch)) {
+ i915_vma_unpin(vma);
+ return PTR_ERR(batch);
+ }
+
+ rq = i915_gem_request_alloc(engine, ctx);
+ if (IS_ERR(rq)) {
+ i915_vma_unpin(batch);
+ i915_vma_unpin(vma);
+ return PTR_ERR(rq);
+ }
+
+ i915_switch_context(rq);
+ engine->emit_bb_start(rq,
+ batch->node.start, batch->node.size, 0);
+
+ i915_vma_move_to_active(batch, rq, 0);
+ i915_gem_object_set_active_reference(batch->obj);
+ i915_vma_unpin(batch);
+ i915_vma_close(batch);
+
+ i915_vma_move_to_active(vma, rq, 0);
+
+ ww_mutex_lock(&obj->resv->lock, NULL);
+ reservation_object_add_excl_fence(obj->resv, &rq->fence);
+ ww_mutex_unlock(&obj->resv->lock);
+
+ __i915_add_request(rq, true);
+ }
+ i915_vma_unpin(vma);
+
+ return 0;
+}
+
+static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)
+{
+ const bool has_llc = HAS_LLC(to_i915(obj->base.dev));
+ unsigned int n, m;
+ unsigned int need_flush;
+ int err;
+
+ err = i915_gem_obj_prepare_shmem_write(obj, &need_flush);
+ if (err)
+ return err;
+
+ for (n = 0; n < 1024; n++) {
+ u32 *map;
+
+ map = kmap_atomic(i915_gem_object_get_page(obj, n));
+ for (m = 0; m < 1024; m++)
+ map[m] = value;
+ if (!has_llc)
+ drm_clflush_virt_range(map, PAGE_SIZE);
+ kunmap_atomic(map);
+ }
+
+ i915_gem_obj_finish_shmem_access(obj);
+ obj->base.read_domains = I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU;
+ obj->base.write_domain = 0;
+ return 0;
+}
+
+static int cpu_check(struct drm_i915_gem_object *obj,
+ unsigned long num)
+{
+ unsigned int n, m, max = (obj->base.size >> PAGE_SHIFT) / 1024;
+ unsigned int needs_flush;
+ int err;
+
+ err = i915_gem_obj_prepare_shmem_read(obj, &needs_flush);
+ if (err)
+ return err;
+
+ for (n = 0; !err && n < 1024; n++) {
+ u32 *map;
+
+ map = kmap_atomic(i915_gem_object_get_page(obj, n));
+ if (needs_flush & CLFLUSH_BEFORE)
+ drm_clflush_virt_range(map, sizeof(u32)*max);
+ for (m = 0; !err && m < max; m++) {
+ if (map[m] != m) {
+ pr_err("Invalid value in object %lu at page %d, offset %d: found %x expected %x\n",
+ num, n, m, map[m], m);
+ err = -EINVAL;
+ }
+ }
+ kunmap_atomic(map);
+ }
+
+ i915_gem_obj_finish_shmem_access(obj);
+ return err;
+}
+
+static int igt_ctx_exec(void *arg)
+{
+ I915_SELFTEST_TIMEOUT(end_time);
+ LIST_HEAD(objects);
+ struct drm_i915_private *i915 = arg;
+ struct drm_i915_gem_object *obj;
+ struct drm_file *file = fake_file(i915);
+ unsigned int count = 0;
+ int err = 0;
+
+ mutex_lock(&i915->drm.struct_mutex);
+ while (!time_after(jiffies, end_time)) {
+ struct i915_gem_context *ctx;
+ struct i915_address_space *vm;
+ u64 npages;
+ u32 ignored;
+
+ ctx = i915_gem_create_context(i915, file->driver_priv);
+ if (IS_ERR(ctx))
+ break;
+
+ vm = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
+ npages = min(vm->total / 2, 1024ull * 1024 * PAGE_SIZE);
+ npages >>= PAGE_SHIFT + 10;
+ npages <<= PAGE_SHIFT + 10;
+ obj = huge_gem_object(i915, 1024 * PAGE_SIZE, npages);
+ if (IS_ERR(obj))
+ break;
+
+ /* tie the handle to the drm_file for easy reaping */
+ err = drm_gem_handle_create(file, &obj->base, &ignored);
+ if (err) {
+ i915_gem_object_put(obj);
+ break;
+ }
+
+ err = cpu_fill(obj, 0xdeadbeef);
+ if (!err)
+ err = gpu_fill(obj, ctx);
+ if (err) {
+ pr_err("Failed to fill object, err=%d\n", err);
+ break;
+ }
+
+ list_add_tail(&obj->batch_pool_link, &objects);
+ count++;
+ }
+ pr_info("Submitted %d contexts\n", count);
+
+ count = 0;
+ list_for_each_entry(obj, &objects, batch_pool_link) {
+ if (!err)
+ err = cpu_check(obj, count);
+ count++;
+ }
+ mutex_unlock(&i915->drm.struct_mutex);
+
+ fake_file_free(i915, file);
+ return err;
+}
+
+int i915_gem_context_live_selftests(struct drm_i915_private *i915)
+{
+ static const struct i915_subtest tests[] = {
+ SUBTEST(igt_ctx_exec),
+ };
+
+ return i915_subtests(tests, i915);
+}
diff --git a/drivers/gpu/drm/i915/selftests/i915_live_selftests.h b/drivers/gpu/drm/i915/selftests/i915_live_selftests.h
index 94517ad6dbd1..0c925f17b445 100644
--- a/drivers/gpu/drm/i915/selftests/i915_live_selftests.h
+++ b/drivers/gpu/drm/i915/selftests/i915_live_selftests.h
@@ -14,3 +14,4 @@ selftest(requests, i915_gem_request_live_selftests)
selftest(object, i915_gem_object_live_selftests)
selftest(coherency, i915_gem_coherency_live_selftests)
selftest(gtt, i915_gem_gtt_live_selftests)
+selftest(context, i915_gem_context_live_selftests)
--
2.11.0
More information about the Intel-gfx
mailing list