2 * SPDX-License-Identifier: MIT
4 * Copyright © 2017 Intel Corporation
7 #include <linux/prime_numbers.h>
8 #include <linux/string_helpers.h>
10 #include "gem/i915_gem_internal.h"
11 #include "gem/i915_gem_pm.h"
12 #include "gt/intel_engine_pm.h"
13 #include "gt/intel_engine_regs.h"
14 #include "gt/intel_gt.h"
15 #include "gt/intel_gt_requests.h"
16 #include "gt/intel_reset.h"
17 #include "i915_selftest.h"
19 #include "gem/selftests/igt_gem_utils.h"
20 #include "selftests/i915_random.h"
21 #include "selftests/igt_flush_test.h"
22 #include "selftests/igt_live_test.h"
23 #include "selftests/igt_reset.h"
24 #include "selftests/igt_spinner.h"
25 #include "selftests/mock_drm.h"
26 #include "selftests/mock_gem_device.h"
28 #include "huge_gem_object.h"
29 #include "igt_gem_utils.h"
31 #define DW_PER_PAGE (PAGE_SIZE / sizeof(u32))
33 static int live_nop_switch(void *arg)
35 const unsigned int nctx = 1024;
36 struct drm_i915_private *i915 = arg;
37 struct intel_engine_cs *engine;
38 struct i915_gem_context **ctx;
39 struct igt_live_test t;
45 * Create as many contexts as we can feasibly get away with
46 * and check we can switch between them rapidly.
48 * Serves as very simple stress test for submission and HW switching
52 if (!DRIVER_CAPS(i915)->has_logical_contexts)
55 file = mock_file(i915);
59 ctx = kcalloc(nctx, sizeof(*ctx), GFP_KERNEL);
65 for (n = 0; n < nctx; n++) {
66 ctx[n] = live_context(i915, file);
68 err = PTR_ERR(ctx[n]);
73 for_each_uabi_engine(engine, i915) {
74 struct i915_request *rq = NULL;
75 unsigned long end_time, prime;
76 ktime_t times[2] = {};
78 times[0] = ktime_get_raw();
79 for (n = 0; n < nctx; n++) {
80 struct i915_request *this;
82 this = igt_request_alloc(ctx[n], engine);
88 i915_request_await_dma_fence(this, &rq->fence);
91 rq = i915_request_get(this);
92 i915_request_add(this);
94 if (i915_request_wait(rq, 0, 10 * HZ) < 0) {
95 pr_err("Failed to populated %d contexts\n", nctx);
96 intel_gt_set_wedged(engine->gt);
101 i915_request_put(rq);
103 times[1] = ktime_get_raw();
105 pr_info("Populated %d contexts on %s in %lluns\n",
106 nctx, engine->name, ktime_to_ns(times[1] - times[0]));
108 err = igt_live_test_begin(&t, i915, __func__, engine->name);
112 end_time = jiffies + i915_selftest.timeout_jiffies;
113 for_each_prime_number_from(prime, 2, 8192) {
114 times[1] = ktime_get_raw();
117 for (n = 0; n < prime; n++) {
118 struct i915_request *this;
120 this = igt_request_alloc(ctx[n % nctx], engine);
126 if (rq) { /* Force submission order */
127 i915_request_await_dma_fence(this, &rq->fence);
128 i915_request_put(rq);
132 * This space is left intentionally blank.
134 * We do not actually want to perform any
135 * action with this request, we just want
136 * to measure the latency in allocation
137 * and submission of our breadcrumbs -
138 * ensuring that the bare request is sufficient
139 * for the system to work (i.e. proper HEAD
140 * tracking of the rings, interrupt handling,
141 * etc). It also gives us the lowest bounds
145 rq = i915_request_get(this);
146 i915_request_add(this);
149 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
150 pr_err("Switching between %ld contexts timed out\n",
152 intel_gt_set_wedged(engine->gt);
153 i915_request_put(rq);
156 i915_request_put(rq);
158 times[1] = ktime_sub(ktime_get_raw(), times[1]);
162 if (__igt_timeout(end_time, NULL))
166 err = igt_live_test_end(&t);
170 pr_info("Switch latencies on %s: 1 = %lluns, %lu = %lluns\n",
172 ktime_to_ns(times[0]),
173 prime - 1, div64_u64(ktime_to_ns(times[1]), prime - 1));
183 struct parallel_switch {
184 struct kthread_worker *worker;
185 struct kthread_work work;
186 struct intel_context *ce[2];
190 static void __live_parallel_switch1(struct kthread_work *work)
192 struct parallel_switch *arg =
193 container_of(work, typeof(*arg), work);
194 IGT_TIMEOUT(end_time);
200 struct i915_request *rq = NULL;
203 for (n = 0; !arg->result && n < ARRAY_SIZE(arg->ce); n++) {
204 struct i915_request *prev = rq;
206 rq = i915_request_create(arg->ce[n]);
208 i915_request_put(prev);
209 arg->result = PTR_ERR(rq);
213 i915_request_get(rq);
216 i915_request_await_dma_fence(rq,
218 i915_request_put(prev);
221 i915_request_add(rq);
224 if (IS_ERR_OR_NULL(rq))
227 if (i915_request_wait(rq, 0, HZ) < 0)
228 arg->result = -ETIME;
230 i915_request_put(rq);
233 } while (!arg->result && !__igt_timeout(end_time, NULL));
235 pr_info("%s: %lu switches (sync) <%d>\n",
236 arg->ce[0]->engine->name, count, arg->result);
239 static void __live_parallel_switchN(struct kthread_work *work)
241 struct parallel_switch *arg =
242 container_of(work, typeof(*arg), work);
243 struct i915_request *rq = NULL;
244 IGT_TIMEOUT(end_time);
251 for (n = 0; !arg->result && n < ARRAY_SIZE(arg->ce); n++) {
252 struct i915_request *prev = rq;
254 rq = i915_request_create(arg->ce[n]);
256 i915_request_put(prev);
257 arg->result = PTR_ERR(rq);
261 i915_request_get(rq);
264 i915_request_await_dma_fence(rq,
266 i915_request_put(prev);
269 i915_request_add(rq);
273 } while (!arg->result && !__igt_timeout(end_time, NULL));
275 if (!IS_ERR_OR_NULL(rq))
276 i915_request_put(rq);
278 pr_info("%s: %lu switches (many) <%d>\n",
279 arg->ce[0]->engine->name, count, arg->result);
282 static int live_parallel_switch(void *arg)
284 struct drm_i915_private *i915 = arg;
285 static void (* const func[])(struct kthread_work *) = {
286 __live_parallel_switch1,
287 __live_parallel_switchN,
290 struct parallel_switch *data = NULL;
291 struct i915_gem_engines *engines;
292 struct i915_gem_engines_iter it;
293 void (* const *fn)(struct kthread_work *);
294 struct i915_gem_context *ctx;
295 struct intel_context *ce;
301 * Check we can process switches on all engines simultaneously.
304 if (!DRIVER_CAPS(i915)->has_logical_contexts)
307 file = mock_file(i915);
309 return PTR_ERR(file);
311 ctx = live_context(i915, file);
317 engines = i915_gem_context_lock_engines(ctx);
318 count = engines->num_engines;
320 data = kcalloc(count, sizeof(*data), GFP_KERNEL);
322 i915_gem_context_unlock_engines(ctx);
327 m = 0; /* Use the first context as our template for the engines */
328 for_each_gem_engine(ce, engines, it) {
329 err = intel_context_pin(ce);
331 i915_gem_context_unlock_engines(ctx);
334 data[m++].ce[0] = intel_context_get(ce);
336 i915_gem_context_unlock_engines(ctx);
338 /* Clone the same set of engines into the other contexts */
339 for (n = 1; n < ARRAY_SIZE(data->ce); n++) {
340 ctx = live_context(i915, file);
346 for (m = 0; m < count; m++) {
350 ce = intel_context_create(data[m].ce[0]->engine);
356 err = intel_context_pin(ce);
358 intel_context_put(ce);
366 for (n = 0; n < count; n++) {
367 struct kthread_worker *worker;
372 worker = kthread_create_worker(0, "igt/parallel:%s",
373 data[n].ce[0]->engine->name);
374 if (IS_ERR(worker)) {
375 err = PTR_ERR(worker);
379 data[n].worker = worker;
382 for (fn = func; !err && *fn; fn++) {
383 struct igt_live_test t;
385 err = igt_live_test_begin(&t, i915, __func__, "");
389 for (n = 0; n < count; n++) {
394 kthread_init_work(&data[n].work, *fn);
395 kthread_queue_work(data[n].worker, &data[n].work);
398 for (n = 0; n < count; n++) {
400 kthread_flush_work(&data[n].work);
401 if (data[n].result && !err)
402 err = data[n].result;
406 if (igt_live_test_end(&t)) {
413 for (n = 0; n < count; n++) {
414 for (m = 0; m < ARRAY_SIZE(data->ce); m++) {
418 intel_context_unpin(data[n].ce[m]);
419 intel_context_put(data[n].ce[m]);
423 kthread_destroy_worker(data[n].worker);
431 static unsigned long real_page_count(struct drm_i915_gem_object *obj)
433 return huge_gem_object_phys_size(obj) >> PAGE_SHIFT;
436 static unsigned long fake_page_count(struct drm_i915_gem_object *obj)
438 return huge_gem_object_dma_size(obj) >> PAGE_SHIFT;
441 static int gpu_fill(struct intel_context *ce,
442 struct drm_i915_gem_object *obj,
445 struct i915_vma *vma;
448 GEM_BUG_ON(obj->base.size > ce->vm->total);
449 GEM_BUG_ON(!intel_engine_can_store_dword(ce->engine));
451 vma = i915_vma_instance(obj, ce->vm, NULL);
455 err = i915_vma_pin(vma, 0, 0, PIN_HIGH | PIN_USER);
460 * Within the GTT the huge objects maps every page onto
461 * its 1024 real pages (using phys_pfn = dma_pfn % 1024).
462 * We set the nth dword within the page using the nth
463 * mapping via the GTT - this should exercise the GTT mapping
464 * whilst checking that each context provides a unique view
467 err = igt_gpu_fill_dw(ce, vma,
468 (dw * real_page_count(obj)) << PAGE_SHIFT |
470 real_page_count(obj),
477 static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)
479 const bool has_llc = HAS_LLC(to_i915(obj->base.dev));
480 unsigned int need_flush;
484 i915_gem_object_lock(obj, NULL);
485 err = i915_gem_object_prepare_write(obj, &need_flush);
489 for (n = 0; n < real_page_count(obj); n++) {
492 map = kmap_atomic(i915_gem_object_get_page(obj, n));
493 for (m = 0; m < DW_PER_PAGE; m++)
496 drm_clflush_virt_range(map, PAGE_SIZE);
500 i915_gem_object_finish_access(obj);
501 obj->read_domains = I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU;
502 obj->write_domain = 0;
504 i915_gem_object_unlock(obj);
508 static noinline int cpu_check(struct drm_i915_gem_object *obj,
509 unsigned int idx, unsigned int max)
511 unsigned int needs_flush;
515 i915_gem_object_lock(obj, NULL);
516 err = i915_gem_object_prepare_read(obj, &needs_flush);
520 for (n = 0; n < real_page_count(obj); n++) {
523 map = kmap_atomic(i915_gem_object_get_page(obj, n));
524 if (needs_flush & CLFLUSH_BEFORE)
525 drm_clflush_virt_range(map, PAGE_SIZE);
527 for (m = 0; m < max; m++) {
529 pr_err("%pS: Invalid value at object %d page %ld/%ld, offset %d/%d: found %x expected %x\n",
530 __builtin_return_address(0), idx,
531 n, real_page_count(obj), m, max,
538 for (; m < DW_PER_PAGE; m++) {
539 if (map[m] != STACK_MAGIC) {
540 pr_err("%pS: Invalid value at object %d page %ld, offset %d: found %x expected %x (uninitialised)\n",
541 __builtin_return_address(0), idx, n, m,
542 map[m], STACK_MAGIC);
554 i915_gem_object_finish_access(obj);
556 i915_gem_object_unlock(obj);
560 static int file_add_object(struct file *file, struct drm_i915_gem_object *obj)
564 GEM_BUG_ON(obj->base.handle_count);
566 /* tie the object to the drm_file for easy reaping */
567 err = idr_alloc(&to_drm_file(file)->object_idr,
568 &obj->base, 1, 0, GFP_KERNEL);
572 i915_gem_object_get(obj);
573 obj->base.handle_count++;
577 static struct drm_i915_gem_object *
578 create_test_object(struct i915_address_space *vm,
580 struct list_head *objects)
582 struct drm_i915_gem_object *obj;
586 /* Keep in GEM's good graces */
587 intel_gt_retire_requests(vm->gt);
589 size = min(vm->total / 2, 1024ull * DW_PER_PAGE * PAGE_SIZE);
590 size = round_down(size, DW_PER_PAGE * PAGE_SIZE);
592 obj = huge_gem_object(vm->i915, DW_PER_PAGE * PAGE_SIZE, size);
596 err = file_add_object(file, obj);
597 i915_gem_object_put(obj);
601 err = cpu_fill(obj, STACK_MAGIC);
603 pr_err("Failed to fill object with cpu, err=%d\n",
608 list_add_tail(&obj->st_link, objects);
612 static unsigned long max_dwords(struct drm_i915_gem_object *obj)
614 unsigned long npages = fake_page_count(obj);
616 GEM_BUG_ON(!IS_ALIGNED(npages, DW_PER_PAGE));
617 return npages / DW_PER_PAGE;
620 static void throttle_release(struct i915_request **q, int count)
624 for (i = 0; i < count; i++) {
625 if (IS_ERR_OR_NULL(q[i]))
628 i915_request_put(fetch_and_zero(&q[i]));
632 static int throttle(struct intel_context *ce,
633 struct i915_request **q, int count)
637 if (!IS_ERR_OR_NULL(q[0])) {
638 if (i915_request_wait(q[0],
639 I915_WAIT_INTERRUPTIBLE,
640 MAX_SCHEDULE_TIMEOUT) < 0)
643 i915_request_put(q[0]);
646 for (i = 0; i < count - 1; i++)
649 q[i] = intel_context_create_request(ce);
651 return PTR_ERR(q[i]);
653 i915_request_get(q[i]);
654 i915_request_add(q[i]);
659 static int igt_ctx_exec(void *arg)
661 struct drm_i915_private *i915 = arg;
662 struct intel_engine_cs *engine;
666 * Create a few different contexts (with different mm) and write
667 * through each ctx/mm using the GPU making sure those writes end
668 * up in the expected pages of our obj.
671 if (!DRIVER_CAPS(i915)->has_logical_contexts)
674 for_each_uabi_engine(engine, i915) {
675 struct drm_i915_gem_object *obj = NULL;
676 unsigned long ncontexts, ndwords, dw;
677 struct i915_request *tq[5] = {};
678 struct igt_live_test t;
679 IGT_TIMEOUT(end_time);
683 if (!intel_engine_can_store_dword(engine))
686 if (!engine->context_size)
687 continue; /* No logical context support in HW */
689 file = mock_file(i915);
691 return PTR_ERR(file);
693 err = igt_live_test_begin(&t, i915, __func__, engine->name);
700 while (!time_after(jiffies, end_time)) {
701 struct i915_gem_context *ctx;
702 struct intel_context *ce;
704 ctx = kernel_context(i915, NULL);
710 ce = i915_gem_context_get_engine(ctx, engine->legacy_idx);
711 GEM_BUG_ON(IS_ERR(ce));
714 obj = create_test_object(ce->vm, file, &objects);
717 intel_context_put(ce);
718 kernel_context_close(ctx);
723 err = gpu_fill(ce, obj, dw);
725 pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) [full-ppgtt? %s], err=%d\n",
726 ndwords, dw, max_dwords(obj),
728 str_yes_no(i915_gem_context_has_full_ppgtt(ctx)),
730 intel_context_put(ce);
731 kernel_context_close(ctx);
735 err = throttle(ce, tq, ARRAY_SIZE(tq));
737 intel_context_put(ce);
738 kernel_context_close(ctx);
742 if (++dw == max_dwords(obj)) {
750 intel_context_put(ce);
751 kernel_context_close(ctx);
754 pr_info("Submitted %lu contexts to %s, filling %lu dwords\n",
755 ncontexts, engine->name, ndwords);
758 list_for_each_entry(obj, &objects, st_link) {
760 min_t(unsigned int, ndwords - dw, max_dwords(obj));
762 err = cpu_check(obj, ncontexts++, rem);
770 throttle_release(tq, ARRAY_SIZE(tq));
771 if (igt_live_test_end(&t))
778 i915_gem_drain_freed_objects(i915);
784 static int igt_shared_ctx_exec(void *arg)
786 struct drm_i915_private *i915 = arg;
787 struct i915_request *tq[5] = {};
788 struct i915_gem_context *parent;
789 struct intel_engine_cs *engine;
790 struct igt_live_test t;
795 * Create a few different contexts with the same mm and write
796 * through each ctx using the GPU making sure those writes end
797 * up in the expected pages of our obj.
799 if (!DRIVER_CAPS(i915)->has_logical_contexts)
802 file = mock_file(i915);
804 return PTR_ERR(file);
806 parent = live_context(i915, file);
807 if (IS_ERR(parent)) {
808 err = PTR_ERR(parent);
812 if (!parent->vm) { /* not full-ppgtt; nothing to share */
817 err = igt_live_test_begin(&t, i915, __func__, "");
821 for_each_uabi_engine(engine, i915) {
822 unsigned long ncontexts, ndwords, dw;
823 struct drm_i915_gem_object *obj = NULL;
824 IGT_TIMEOUT(end_time);
827 if (!intel_engine_can_store_dword(engine))
833 while (!time_after(jiffies, end_time)) {
834 struct i915_gem_context *ctx;
835 struct intel_context *ce;
837 ctx = kernel_context(i915, parent->vm);
843 ce = i915_gem_context_get_engine(ctx, engine->legacy_idx);
844 GEM_BUG_ON(IS_ERR(ce));
847 obj = create_test_object(parent->vm,
851 intel_context_put(ce);
852 kernel_context_close(ctx);
857 err = gpu_fill(ce, obj, dw);
859 pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) [full-ppgtt? %s], err=%d\n",
860 ndwords, dw, max_dwords(obj),
862 str_yes_no(i915_gem_context_has_full_ppgtt(ctx)),
864 intel_context_put(ce);
865 kernel_context_close(ctx);
869 err = throttle(ce, tq, ARRAY_SIZE(tq));
871 intel_context_put(ce);
872 kernel_context_close(ctx);
876 if (++dw == max_dwords(obj)) {
884 intel_context_put(ce);
885 kernel_context_close(ctx);
887 pr_info("Submitted %lu contexts to %s, filling %lu dwords\n",
888 ncontexts, engine->name, ndwords);
891 list_for_each_entry(obj, &objects, st_link) {
893 min_t(unsigned int, ndwords - dw, max_dwords(obj));
895 err = cpu_check(obj, ncontexts++, rem);
902 i915_gem_drain_freed_objects(i915);
905 throttle_release(tq, ARRAY_SIZE(tq));
906 if (igt_live_test_end(&t))
913 static int rpcs_query_batch(struct drm_i915_gem_object *rpcs,
914 struct i915_vma *vma,
915 struct intel_engine_cs *engine)
919 GEM_BUG_ON(GRAPHICS_VER(vma->vm->i915) < 8);
921 cmd = i915_gem_object_pin_map(rpcs, I915_MAP_WB);
925 *cmd++ = MI_STORE_REGISTER_MEM_GEN8;
926 *cmd++ = i915_mmio_reg_offset(GEN8_R_PWR_CLK_STATE(engine->mmio_base));
927 *cmd++ = lower_32_bits(i915_vma_offset(vma));
928 *cmd++ = upper_32_bits(i915_vma_offset(vma));
929 *cmd = MI_BATCH_BUFFER_END;
931 __i915_gem_object_flush_map(rpcs, 0, 64);
932 i915_gem_object_unpin_map(rpcs);
934 intel_gt_chipset_flush(vma->vm->gt);
940 emit_rpcs_query(struct drm_i915_gem_object *obj,
941 struct intel_context *ce,
942 struct i915_request **rq_out)
944 struct drm_i915_private *i915 = to_i915(obj->base.dev);
945 struct i915_request *rq;
946 struct i915_gem_ww_ctx ww;
947 struct i915_vma *batch;
948 struct i915_vma *vma;
949 struct drm_i915_gem_object *rpcs;
952 GEM_BUG_ON(!intel_engine_can_store_dword(ce->engine));
954 if (GRAPHICS_VER(i915) < 8)
957 vma = i915_vma_instance(obj, ce->vm, NULL);
961 rpcs = i915_gem_object_create_internal(i915, PAGE_SIZE);
963 return PTR_ERR(rpcs);
965 batch = i915_vma_instance(rpcs, ce->vm, NULL);
967 err = PTR_ERR(batch);
971 i915_gem_ww_ctx_init(&ww, false);
973 err = i915_gem_object_lock(obj, &ww);
975 err = i915_gem_object_lock(rpcs, &ww);
977 err = i915_gem_object_set_to_gtt_domain(obj, false);
979 err = i915_vma_pin_ww(vma, &ww, 0, 0, PIN_USER);
983 err = i915_vma_pin_ww(batch, &ww, 0, 0, PIN_USER);
987 err = rpcs_query_batch(rpcs, vma, ce->engine);
991 rq = i915_request_create(ce);
997 err = i915_vma_move_to_active(batch, rq, 0);
1001 err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
1005 if (rq->engine->emit_init_breadcrumb) {
1006 err = rq->engine->emit_init_breadcrumb(rq);
1011 err = rq->engine->emit_bb_start(rq,
1012 i915_vma_offset(batch),
1013 i915_vma_size(batch),
1018 *rq_out = i915_request_get(rq);
1022 i915_request_set_error_once(rq, err);
1023 i915_request_add(rq);
1025 i915_vma_unpin(batch);
1027 i915_vma_unpin(vma);
1029 if (err == -EDEADLK) {
1030 err = i915_gem_ww_ctx_backoff(&ww);
1034 i915_gem_ww_ctx_fini(&ww);
1035 i915_gem_object_put(rpcs);
1039 #define TEST_IDLE BIT(0)
1040 #define TEST_BUSY BIT(1)
1041 #define TEST_RESET BIT(2)
1044 __sseu_prepare(const char *name,
1046 struct intel_context *ce,
1047 struct igt_spinner **spin)
1049 struct i915_request *rq;
1053 if (!(flags & (TEST_BUSY | TEST_RESET)))
1056 *spin = kzalloc(sizeof(**spin), GFP_KERNEL);
1060 ret = igt_spinner_init(*spin, ce->engine->gt);
1064 rq = igt_spinner_create_request(*spin, ce, MI_NOOP);
1070 i915_request_add(rq);
1072 if (!igt_wait_for_spinner(*spin, rq)) {
1073 pr_err("%s: Spinner failed to start!\n", name);
1081 igt_spinner_end(*spin);
1083 igt_spinner_fini(*spin);
1085 kfree(fetch_and_zero(spin));
1090 __read_slice_count(struct intel_context *ce,
1091 struct drm_i915_gem_object *obj,
1092 struct igt_spinner *spin,
1095 struct i915_request *rq = NULL;
1096 u32 s_mask, s_shift;
1101 ret = emit_rpcs_query(obj, ce, &rq);
1106 igt_spinner_end(spin);
1108 ret = i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT);
1109 i915_request_put(rq);
1113 buf = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WB);
1119 if (GRAPHICS_VER(ce->engine->i915) >= 11) {
1120 s_mask = GEN11_RPCS_S_CNT_MASK;
1121 s_shift = GEN11_RPCS_S_CNT_SHIFT;
1123 s_mask = GEN8_RPCS_S_CNT_MASK;
1124 s_shift = GEN8_RPCS_S_CNT_SHIFT;
1128 cnt = (val & s_mask) >> s_shift;
1131 i915_gem_object_unpin_map(obj);
1137 __check_rpcs(const char *name, u32 rpcs, int slices, unsigned int expected,
1138 const char *prefix, const char *suffix)
1140 if (slices == expected)
1144 pr_err("%s: %s read slice count failed with %d%s\n",
1145 name, prefix, slices, suffix);
1149 pr_err("%s: %s slice count %d is not %u%s\n",
1150 name, prefix, slices, expected, suffix);
1152 pr_info("RPCS=0x%x; %u%sx%u%s\n",
1154 (rpcs & GEN8_RPCS_S_CNT_ENABLE) ? "*" : "",
1155 (rpcs & GEN8_RPCS_SS_CNT_MASK) >> GEN8_RPCS_SS_CNT_SHIFT,
1156 (rpcs & GEN8_RPCS_SS_CNT_ENABLE) ? "*" : "");
1162 __sseu_finish(const char *name,
1164 struct intel_context *ce,
1165 struct drm_i915_gem_object *obj,
1166 unsigned int expected,
1167 struct igt_spinner *spin)
1169 unsigned int slices = hweight32(ce->engine->sseu.slice_mask);
1173 if (flags & TEST_RESET) {
1174 ret = intel_engine_reset(ce->engine, "sseu");
1179 ret = __read_slice_count(ce, obj,
1180 flags & TEST_RESET ? NULL : spin, &rpcs);
1181 ret = __check_rpcs(name, rpcs, ret, expected, "Context", "!");
1185 ret = __read_slice_count(ce->engine->kernel_context, obj, NULL, &rpcs);
1186 ret = __check_rpcs(name, rpcs, ret, slices, "Kernel context", "!");
1190 igt_spinner_end(spin);
1192 if ((flags & TEST_IDLE) && ret == 0) {
1193 ret = igt_flush_test(ce->engine->i915);
1197 ret = __read_slice_count(ce, obj, NULL, &rpcs);
1198 ret = __check_rpcs(name, rpcs, ret, expected,
1199 "Context", " after idle!");
1206 __sseu_test(const char *name,
1208 struct intel_context *ce,
1209 struct drm_i915_gem_object *obj,
1210 struct intel_sseu sseu)
1212 struct igt_spinner *spin = NULL;
1215 intel_engine_pm_get(ce->engine);
1217 ret = __sseu_prepare(name, flags, ce, &spin);
1221 ret = intel_context_reconfigure_sseu(ce, sseu);
1225 ret = __sseu_finish(name, flags, ce, obj,
1226 hweight32(sseu.slice_mask), spin);
1230 igt_spinner_end(spin);
1231 igt_spinner_fini(spin);
1235 intel_engine_pm_put(ce->engine);
1240 __igt_ctx_sseu(struct drm_i915_private *i915,
1244 struct drm_i915_gem_object *obj;
1248 if (GRAPHICS_VER(i915) < 9)
1251 if (flags & TEST_RESET)
1252 igt_global_reset_lock(to_gt(i915));
1254 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
1261 struct intel_engine_cs *engine;
1262 struct intel_context *ce;
1263 struct intel_sseu pg_sseu;
1265 engine = intel_engine_lookup_user(i915,
1266 I915_ENGINE_CLASS_RENDER,
1271 if (hweight32(engine->sseu.slice_mask) < 2)
1274 if (!engine->gt->info.sseu.has_slice_pg)
1278 * Gen11 VME friendly power-gated configuration with
1279 * half enabled sub-slices.
1281 pg_sseu = engine->sseu;
1282 pg_sseu.slice_mask = 1;
1283 pg_sseu.subslice_mask =
1284 ~(~0 << (hweight32(engine->sseu.subslice_mask) / 2));
1286 pr_info("%s: SSEU subtest '%s', flags=%x, def_slices=%u, pg_slices=%u\n",
1287 engine->name, name, flags,
1288 hweight32(engine->sseu.slice_mask),
1289 hweight32(pg_sseu.slice_mask));
1291 ce = intel_context_create(engine);
1297 ret = intel_context_pin(ce);
1301 /* First set the default mask. */
1302 ret = __sseu_test(name, flags, ce, obj, engine->sseu);
1306 /* Then set a power-gated configuration. */
1307 ret = __sseu_test(name, flags, ce, obj, pg_sseu);
1311 /* Back to defaults. */
1312 ret = __sseu_test(name, flags, ce, obj, engine->sseu);
1316 /* One last power-gated configuration for the road. */
1317 ret = __sseu_test(name, flags, ce, obj, pg_sseu);
1322 intel_context_unpin(ce);
1324 intel_context_put(ce);
1327 if (igt_flush_test(i915))
1331 i915_gem_object_put(obj);
1334 if (flags & TEST_RESET)
1335 igt_global_reset_unlock(to_gt(i915));
1338 pr_err("%s: Failed with %d!\n", name, ret);
1343 static int igt_ctx_sseu(void *arg)
1348 } *phase, phases[] = {
1349 { .name = "basic", .flags = 0 },
1350 { .name = "idle", .flags = TEST_IDLE },
1351 { .name = "busy", .flags = TEST_BUSY },
1352 { .name = "busy-reset", .flags = TEST_BUSY | TEST_RESET },
1353 { .name = "busy-idle", .flags = TEST_BUSY | TEST_IDLE },
1354 { .name = "reset-idle", .flags = TEST_RESET | TEST_IDLE },
1359 for (i = 0, phase = phases; ret == 0 && i < ARRAY_SIZE(phases);
1361 ret = __igt_ctx_sseu(arg, phase->name, phase->flags);
1366 static int igt_ctx_readonly(void *arg)
1368 struct drm_i915_private *i915 = arg;
1369 unsigned long idx, ndwords, dw, num_engines;
1370 struct drm_i915_gem_object *obj = NULL;
1371 struct i915_request *tq[5] = {};
1372 struct i915_gem_engines_iter it;
1373 struct i915_address_space *vm;
1374 struct i915_gem_context *ctx;
1375 struct intel_context *ce;
1376 struct igt_live_test t;
1377 I915_RND_STATE(prng);
1378 IGT_TIMEOUT(end_time);
1384 * Create a few read-only objects (with the occasional writable object)
1385 * and try to write into these object checking that the GPU discards
1386 * any write to a read-only object.
1389 file = mock_file(i915);
1391 return PTR_ERR(file);
1393 err = igt_live_test_begin(&t, i915, __func__, "");
1397 ctx = live_context(i915, file);
1403 vm = ctx->vm ?: &to_gt(i915)->ggtt->alias->vm;
1404 if (!vm || !vm->has_read_only) {
1410 for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it)
1411 if (intel_engine_can_store_dword(ce->engine))
1413 i915_gem_context_unlock_engines(ctx);
1417 while (!time_after(jiffies, end_time)) {
1418 for_each_gem_engine(ce,
1419 i915_gem_context_lock_engines(ctx), it) {
1420 if (!intel_engine_can_store_dword(ce->engine))
1424 obj = create_test_object(ce->vm, file, &objects);
1427 i915_gem_context_unlock_engines(ctx);
1431 if (prandom_u32_state(&prng) & 1)
1432 i915_gem_object_set_readonly(obj);
1435 err = gpu_fill(ce, obj, dw);
1437 pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) [full-ppgtt? %s], err=%d\n",
1438 ndwords, dw, max_dwords(obj),
1440 str_yes_no(i915_gem_context_has_full_ppgtt(ctx)),
1442 i915_gem_context_unlock_engines(ctx);
1446 err = throttle(ce, tq, ARRAY_SIZE(tq));
1448 i915_gem_context_unlock_engines(ctx);
1452 if (++dw == max_dwords(obj)) {
1458 i915_gem_context_unlock_engines(ctx);
1460 pr_info("Submitted %lu dwords (across %lu engines)\n",
1461 ndwords, num_engines);
1465 list_for_each_entry(obj, &objects, st_link) {
1467 min_t(unsigned int, ndwords - dw, max_dwords(obj));
1468 unsigned int num_writes;
1471 if (i915_gem_object_is_readonly(obj))
1474 err = cpu_check(obj, idx++, num_writes);
1482 throttle_release(tq, ARRAY_SIZE(tq));
1483 if (igt_live_test_end(&t))
1490 static int check_scratch(struct i915_address_space *vm, u64 offset)
1492 struct drm_mm_node *node;
1494 mutex_lock(&vm->mutex);
1495 node = __drm_mm_interval_first(&vm->mm,
1496 offset, offset + sizeof(u32) - 1);
1497 mutex_unlock(&vm->mutex);
1498 if (!node || node->start > offset)
1501 GEM_BUG_ON(offset >= node->start + node->size);
1503 pr_err("Target offset 0x%08x_%08x overlaps with a node in the mm!\n",
1504 upper_32_bits(offset), lower_32_bits(offset));
1508 static int write_to_scratch(struct i915_gem_context *ctx,
1509 struct intel_engine_cs *engine,
1510 struct drm_i915_gem_object *obj,
1511 u64 offset, u32 value)
1513 struct drm_i915_private *i915 = ctx->i915;
1514 struct i915_address_space *vm;
1515 struct i915_request *rq;
1516 struct i915_vma *vma;
1520 GEM_BUG_ON(offset < I915_GTT_PAGE_SIZE);
1522 err = check_scratch(ctx->vm, offset);
1526 cmd = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WB);
1528 return PTR_ERR(cmd);
1530 *cmd++ = MI_STORE_DWORD_IMM_GEN4;
1531 if (GRAPHICS_VER(i915) >= 8) {
1532 *cmd++ = lower_32_bits(offset);
1533 *cmd++ = upper_32_bits(offset);
1539 *cmd = MI_BATCH_BUFFER_END;
1540 __i915_gem_object_flush_map(obj, 0, 64);
1541 i915_gem_object_unpin_map(obj);
1543 intel_gt_chipset_flush(engine->gt);
1545 vm = i915_gem_context_get_eb_vm(ctx);
1546 vma = i915_vma_instance(obj, vm, NULL);
1552 err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_OFFSET_FIXED);
1556 rq = igt_request_alloc(ctx, engine);
1562 err = igt_vma_move_to_active_unlocked(vma, rq, 0);
1566 if (rq->engine->emit_init_breadcrumb) {
1567 err = rq->engine->emit_init_breadcrumb(rq);
1572 err = engine->emit_bb_start(rq, i915_vma_offset(vma),
1573 i915_vma_size(vma), 0);
1577 i915_vma_unpin(vma);
1579 i915_request_add(rq);
1583 i915_request_set_error_once(rq, err);
1584 i915_request_add(rq);
1586 i915_vma_unpin(vma);
1591 err = i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT);
1596 static int read_from_scratch(struct i915_gem_context *ctx,
1597 struct intel_engine_cs *engine,
1598 struct drm_i915_gem_object *obj,
1599 u64 offset, u32 *value)
1601 struct drm_i915_private *i915 = ctx->i915;
1602 struct i915_address_space *vm;
1603 const u32 result = 0x100;
1604 struct i915_request *rq;
1605 struct i915_vma *vma;
1610 GEM_BUG_ON(offset < I915_GTT_PAGE_SIZE);
1612 err = check_scratch(ctx->vm, offset);
1616 if (GRAPHICS_VER(i915) >= 8) {
1617 const u32 GPR0 = engine->mmio_base + 0x600;
1619 vm = i915_gem_context_get_eb_vm(ctx);
1620 vma = i915_vma_instance(obj, vm, NULL);
1626 err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_OFFSET_FIXED);
1630 cmd = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WB);
1636 memset(cmd, POISON_INUSE, PAGE_SIZE);
1637 *cmd++ = MI_LOAD_REGISTER_MEM_GEN8;
1639 *cmd++ = lower_32_bits(offset);
1640 *cmd++ = upper_32_bits(offset);
1641 *cmd++ = MI_STORE_REGISTER_MEM_GEN8;
1645 *cmd = MI_BATCH_BUFFER_END;
1647 i915_gem_object_flush_map(obj);
1648 i915_gem_object_unpin_map(obj);
1652 const u32 reg = engine->mmio_base + 0x420;
1654 /* hsw: register access even to 3DPRIM! is protected */
1655 vm = i915_vm_get(&engine->gt->ggtt->vm);
1656 vma = i915_vma_instance(obj, vm, NULL);
1662 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
1666 cmd = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WB);
1672 memset(cmd, POISON_INUSE, PAGE_SIZE);
1673 *cmd++ = MI_LOAD_REGISTER_MEM;
1676 *cmd++ = MI_STORE_REGISTER_MEM | MI_USE_GGTT;
1678 *cmd++ = i915_vma_offset(vma) + result;
1679 *cmd = MI_BATCH_BUFFER_END;
1681 i915_gem_object_flush_map(obj);
1682 i915_gem_object_unpin_map(obj);
1684 flags = I915_DISPATCH_SECURE;
1687 intel_gt_chipset_flush(engine->gt);
1689 rq = igt_request_alloc(ctx, engine);
1695 err = igt_vma_move_to_active_unlocked(vma, rq, EXEC_OBJECT_WRITE);
1699 if (rq->engine->emit_init_breadcrumb) {
1700 err = rq->engine->emit_init_breadcrumb(rq);
1705 err = engine->emit_bb_start(rq, i915_vma_offset(vma),
1706 i915_vma_size(vma), flags);
1710 i915_vma_unpin(vma);
1712 i915_request_add(rq);
1714 i915_gem_object_lock(obj, NULL);
1715 err = i915_gem_object_set_to_cpu_domain(obj, false);
1716 i915_gem_object_unlock(obj);
1720 cmd = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WB);
1726 *value = cmd[result / sizeof(*cmd)];
1727 i915_gem_object_unpin_map(obj);
1731 i915_request_set_error_once(rq, err);
1732 i915_request_add(rq);
1734 i915_vma_unpin(vma);
1739 err = i915_gem_object_wait(obj, 0, MAX_SCHEDULE_TIMEOUT);
1744 static int check_scratch_page(struct i915_gem_context *ctx, u32 *out)
1746 struct i915_address_space *vm;
1754 if (!vm->scratch[0]) {
1755 pr_err("No scratch page!\n");
1759 vaddr = __px_vaddr(vm->scratch[0]);
1761 memcpy(out, vaddr, sizeof(*out));
1762 if (memchr_inv(vaddr, *out, PAGE_SIZE)) {
1763 pr_err("Inconsistent initial state of scratch page!\n");
1770 static int igt_vm_isolation(void *arg)
1772 struct drm_i915_private *i915 = arg;
1773 struct i915_gem_context *ctx_a, *ctx_b;
1774 struct drm_i915_gem_object *obj_a, *obj_b;
1775 unsigned long num_engines, count;
1776 struct intel_engine_cs *engine;
1777 struct igt_live_test t;
1778 I915_RND_STATE(prng);
1784 if (GRAPHICS_VER(i915) < 7)
1788 * The simple goal here is that a write into one context is not
1789 * observed in a second (separate page tables and scratch).
1792 file = mock_file(i915);
1794 return PTR_ERR(file);
1796 err = igt_live_test_begin(&t, i915, __func__, "");
1800 ctx_a = live_context(i915, file);
1801 if (IS_ERR(ctx_a)) {
1802 err = PTR_ERR(ctx_a);
1806 ctx_b = live_context(i915, file);
1807 if (IS_ERR(ctx_b)) {
1808 err = PTR_ERR(ctx_b);
1812 /* We can only test vm isolation, if the vm are distinct */
1813 if (ctx_a->vm == ctx_b->vm)
1816 /* Read the initial state of the scratch page */
1817 err = check_scratch_page(ctx_a, &expected);
1821 err = check_scratch_page(ctx_b, &expected);
1825 vm_total = ctx_a->vm->total;
1826 GEM_BUG_ON(ctx_b->vm->total != vm_total);
1828 obj_a = i915_gem_object_create_internal(i915, PAGE_SIZE);
1829 if (IS_ERR(obj_a)) {
1830 err = PTR_ERR(obj_a);
1834 obj_b = i915_gem_object_create_internal(i915, PAGE_SIZE);
1835 if (IS_ERR(obj_b)) {
1836 err = PTR_ERR(obj_b);
1842 for_each_uabi_engine(engine, i915) {
1843 IGT_TIMEOUT(end_time);
1844 unsigned long this = 0;
1846 if (!intel_engine_can_store_dword(engine))
1849 /* Not all engines have their own GPR! */
1850 if (GRAPHICS_VER(i915) < 8 && engine->class != RENDER_CLASS)
1853 while (!__igt_timeout(end_time, NULL)) {
1854 u32 value = 0xc5c5c5c5;
1857 /* Leave enough space at offset 0 for the batch */
1858 offset = igt_random_offset(&prng,
1859 I915_GTT_PAGE_SIZE, vm_total,
1860 sizeof(u32), alignof_dword);
1862 err = write_to_scratch(ctx_a, engine, obj_a,
1863 offset, 0xdeadbeef);
1865 err = read_from_scratch(ctx_b, engine, obj_b,
1870 if (value != expected) {
1871 pr_err("%s: Read %08x from scratch (offset 0x%08x_%08x), after %lu reads!\n",
1872 engine->name, value,
1873 upper_32_bits(offset),
1874 lower_32_bits(offset),
1885 pr_info("Checked %lu scratch offsets across %lu engines\n",
1886 count, num_engines);
1889 i915_gem_object_put(obj_b);
1891 i915_gem_object_put(obj_a);
1893 if (igt_live_test_end(&t))
1899 int i915_gem_context_live_selftests(struct drm_i915_private *i915)
1901 static const struct i915_subtest tests[] = {
1902 SUBTEST(live_nop_switch),
1903 SUBTEST(live_parallel_switch),
1904 SUBTEST(igt_ctx_exec),
1905 SUBTEST(igt_ctx_readonly),
1906 SUBTEST(igt_ctx_sseu),
1907 SUBTEST(igt_shared_ctx_exec),
1908 SUBTEST(igt_vm_isolation),
1911 if (intel_gt_is_wedged(to_gt(i915)))
1914 return i915_live_subtests(tests, i915);