1 // SPDX-License-Identifier: MIT
3 * Copyright © 2018 Intel Corporation
6 #include <linux/prime_numbers.h>
8 #include "i915_selftest.h"
9 #include "intel_engine_heartbeat.h"
10 #include "intel_engine_pm.h"
11 #include "intel_reset.h"
12 #include "intel_ring.h"
13 #include "selftest_engine_heartbeat.h"
14 #include "selftests/i915_random.h"
15 #include "selftests/igt_flush_test.h"
16 #include "selftests/igt_live_test.h"
17 #include "selftests/igt_spinner.h"
18 #include "selftests/lib_sw_fence.h"
19 #include "shmem_utils.h"
21 #include "gem/selftests/igt_gem_utils.h"
22 #include "gem/selftests/mock_context.h"
24 #define CS_GPR(engine, n) ((engine)->mmio_base + 0x600 + (n) * 4)
26 #define NUM_GPR_DW (NUM_GPR * 2) /* each GPR is 2 dwords */
28 static struct i915_vma *create_scratch(struct intel_gt *gt)
30 return __vm_create_scratch_for_read(>->ggtt->vm, PAGE_SIZE);
33 static bool is_active(struct i915_request *rq)
35 if (i915_request_is_active(rq))
38 if (i915_request_on_hold(rq))
41 if (i915_request_has_initial_breadcrumb(rq) && i915_request_started(rq))
47 static int wait_for_submit(struct intel_engine_cs *engine,
48 struct i915_request *rq,
49 unsigned long timeout)
51 /* Ignore our own attempts to suppress excess tasklets */
52 tasklet_hi_schedule(&engine->execlists.tasklet);
56 bool done = time_after(jiffies, timeout);
58 if (i915_request_completed(rq)) /* that was quick! */
61 /* Wait until the HW has acknowleged the submission (or err) */
62 intel_engine_flush_submission(engine);
63 if (!READ_ONCE(engine->execlists.pending[0]) && is_active(rq))
73 static int emit_semaphore_signal(struct intel_context *ce, void *slot)
76 i915_ggtt_offset(ce->engine->status_page.vma) +
78 struct i915_request *rq;
81 rq = intel_context_create_request(ce);
85 cs = intel_ring_begin(rq, 4);
91 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
96 intel_ring_advance(rq, cs);
98 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
103 static int context_flush(struct intel_context *ce, long timeout)
105 struct i915_request *rq;
106 struct dma_fence *fence;
109 rq = intel_engine_create_kernel_request(ce->engine);
113 fence = i915_active_fence_get(&ce->timeline->last_request);
115 i915_request_await_dma_fence(rq, fence);
116 dma_fence_put(fence);
119 rq = i915_request_get(rq);
120 i915_request_add(rq);
121 if (i915_request_wait(rq, 0, timeout) < 0)
123 i915_request_put(rq);
125 rmb(); /* We know the request is written, make sure all state is too! */
129 static int live_lrc_layout(void *arg)
131 struct intel_gt *gt = arg;
132 struct intel_engine_cs *engine;
133 enum intel_engine_id id;
138 * Check the registers offsets we use to create the initial reg state
139 * match the layout saved by HW.
142 lrc = (u32 *)__get_free_page(GFP_KERNEL); /* requires page alignment */
145 GEM_BUG_ON(offset_in_page(lrc));
148 for_each_engine(engine, gt, id) {
152 if (!engine->default_state)
155 hw = shmem_pin_map(engine->default_state);
160 hw += LRC_STATE_OFFSET / sizeof(*hw);
162 __lrc_init_regs(memset(lrc, POISON_INUSE, PAGE_SIZE),
163 engine->kernel_context, engine, true);
175 pr_debug("%s: skipped instruction %x at dword %d\n",
176 engine->name, lri, dw);
181 if ((lri & GENMASK(31, 23)) != MI_INSTR(0x22, 0)) {
182 pr_err("%s: Expected LRI command at dword %d, found %08x\n",
183 engine->name, dw, lri);
188 if (lrc[dw] != lri) {
189 pr_err("%s: LRI command mismatch at dword %d, expected %08x found %08x\n",
190 engine->name, dw, lri, lrc[dw]);
200 if (hw[dw] != lrc[dw]) {
201 pr_err("%s: Different registers found at dword %d, expected %x, found %x\n",
202 engine->name, dw, hw[dw], lrc[dw]);
208 * Skip over the actual register value as we
209 * expect that to differ.
214 } while ((lrc[dw] & ~BIT(0)) != MI_BATCH_BUFFER_END);
217 pr_info("%s: HW register image:\n", engine->name);
218 igt_hexdump(hw, PAGE_SIZE);
220 pr_info("%s: SW register image:\n", engine->name);
221 igt_hexdump(lrc, PAGE_SIZE);
224 shmem_unpin_map(engine->default_state, hw);
229 free_page((unsigned long)lrc);
233 static int find_offset(const u32 *lri, u32 offset)
237 for (i = 0; i < PAGE_SIZE / sizeof(u32); i++)
238 if (lri[i] == offset)
244 static int live_lrc_fixed(void *arg)
246 struct intel_gt *gt = arg;
247 struct intel_engine_cs *engine;
248 enum intel_engine_id id;
252 * Check the assumed register offsets match the actual locations in
256 for_each_engine(engine, gt, id) {
263 i915_mmio_reg_offset(RING_START(engine->mmio_base)),
268 i915_mmio_reg_offset(RING_CTL(engine->mmio_base)),
273 i915_mmio_reg_offset(RING_HEAD(engine->mmio_base)),
278 i915_mmio_reg_offset(RING_TAIL(engine->mmio_base)),
283 i915_mmio_reg_offset(RING_MI_MODE(engine->mmio_base)),
284 lrc_ring_mi_mode(engine),
288 i915_mmio_reg_offset(RING_BBSTATE(engine->mmio_base)),
293 i915_mmio_reg_offset(RING_BB_PER_CTX_PTR(engine->mmio_base)),
294 lrc_ring_wa_bb_per_ctx(engine),
295 "RING_BB_PER_CTX_PTR"
298 i915_mmio_reg_offset(RING_INDIRECT_CTX(engine->mmio_base)),
299 lrc_ring_indirect_ptr(engine),
300 "RING_INDIRECT_CTX_PTR"
303 i915_mmio_reg_offset(RING_INDIRECT_CTX_OFFSET(engine->mmio_base)),
304 lrc_ring_indirect_offset(engine),
305 "RING_INDIRECT_CTX_OFFSET"
308 i915_mmio_reg_offset(RING_CTX_TIMESTAMP(engine->mmio_base)),
313 i915_mmio_reg_offset(GEN8_RING_CS_GPR(engine->mmio_base, 0)),
314 lrc_ring_gpr0(engine),
318 i915_mmio_reg_offset(RING_CMD_BUF_CCTL(engine->mmio_base)),
319 lrc_ring_cmd_buf_cctl(engine),
326 if (!engine->default_state)
329 hw = shmem_pin_map(engine->default_state);
334 hw += LRC_STATE_OFFSET / sizeof(*hw);
336 for (t = tbl; t->name; t++) {
337 int dw = find_offset(hw, t->reg);
339 if (dw != t->offset) {
340 pr_err("%s: Offset for %s [0x%x] mismatch, found %x, expected %x\n",
350 shmem_unpin_map(engine->default_state, hw);
356 static int __live_lrc_state(struct intel_engine_cs *engine,
357 struct i915_vma *scratch)
359 struct intel_context *ce;
360 struct i915_request *rq;
361 struct i915_gem_ww_ctx ww;
367 u32 expected[MAX_IDX];
372 ce = intel_context_create(engine);
376 i915_gem_ww_ctx_init(&ww, false);
378 err = i915_gem_object_lock(scratch->obj, &ww);
380 err = intel_context_pin_ww(ce, &ww);
384 rq = i915_request_create(ce);
390 cs = intel_ring_begin(rq, 4 * MAX_IDX);
393 i915_request_add(rq);
397 *cs++ = MI_STORE_REGISTER_MEM_GEN8 | MI_USE_GGTT;
398 *cs++ = i915_mmio_reg_offset(RING_START(engine->mmio_base));
399 *cs++ = i915_ggtt_offset(scratch) + RING_START_IDX * sizeof(u32);
402 expected[RING_START_IDX] = i915_ggtt_offset(ce->ring->vma);
404 *cs++ = MI_STORE_REGISTER_MEM_GEN8 | MI_USE_GGTT;
405 *cs++ = i915_mmio_reg_offset(RING_TAIL(engine->mmio_base));
406 *cs++ = i915_ggtt_offset(scratch) + RING_TAIL_IDX * sizeof(u32);
409 err = i915_request_await_object(rq, scratch->obj, true);
411 err = i915_vma_move_to_active(scratch, rq, EXEC_OBJECT_WRITE);
413 i915_request_get(rq);
414 i915_request_add(rq);
418 intel_engine_flush_submission(engine);
419 expected[RING_TAIL_IDX] = ce->ring->tail;
421 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
426 cs = i915_gem_object_pin_map(scratch->obj, I915_MAP_WB);
432 for (n = 0; n < MAX_IDX; n++) {
433 if (cs[n] != expected[n]) {
434 pr_err("%s: Stored register[%d] value[0x%x] did not match expected[0x%x]\n",
435 engine->name, n, cs[n], expected[n]);
441 i915_gem_object_unpin_map(scratch->obj);
444 i915_request_put(rq);
446 intel_context_unpin(ce);
448 if (err == -EDEADLK) {
449 err = i915_gem_ww_ctx_backoff(&ww);
453 i915_gem_ww_ctx_fini(&ww);
454 intel_context_put(ce);
458 static int live_lrc_state(void *arg)
460 struct intel_gt *gt = arg;
461 struct intel_engine_cs *engine;
462 struct i915_vma *scratch;
463 enum intel_engine_id id;
467 * Check the live register state matches what we expect for this
471 scratch = create_scratch(gt);
473 return PTR_ERR(scratch);
475 for_each_engine(engine, gt, id) {
476 err = __live_lrc_state(engine, scratch);
481 if (igt_flush_test(gt->i915))
484 i915_vma_unpin_and_release(&scratch, 0);
488 static int gpr_make_dirty(struct intel_context *ce)
490 struct i915_request *rq;
494 rq = intel_context_create_request(ce);
498 cs = intel_ring_begin(rq, 2 * NUM_GPR_DW + 2);
500 i915_request_add(rq);
504 *cs++ = MI_LOAD_REGISTER_IMM(NUM_GPR_DW);
505 for (n = 0; n < NUM_GPR_DW; n++) {
506 *cs++ = CS_GPR(ce->engine, n);
511 intel_ring_advance(rq, cs);
513 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
514 i915_request_add(rq);
519 static struct i915_request *
520 __gpr_read(struct intel_context *ce, struct i915_vma *scratch, u32 *slot)
523 i915_ggtt_offset(ce->engine->status_page.vma) +
524 offset_in_page(slot);
525 struct i915_request *rq;
530 rq = intel_context_create_request(ce);
534 cs = intel_ring_begin(rq, 6 + 4 * NUM_GPR_DW);
536 i915_request_add(rq);
540 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
543 *cs++ = MI_SEMAPHORE_WAIT |
544 MI_SEMAPHORE_GLOBAL_GTT |
546 MI_SEMAPHORE_SAD_NEQ_SDD;
551 for (n = 0; n < NUM_GPR_DW; n++) {
552 *cs++ = MI_STORE_REGISTER_MEM_GEN8 | MI_USE_GGTT;
553 *cs++ = CS_GPR(ce->engine, n);
554 *cs++ = i915_ggtt_offset(scratch) + n * sizeof(u32);
558 i915_vma_lock(scratch);
559 err = i915_request_await_object(rq, scratch->obj, true);
561 err = i915_vma_move_to_active(scratch, rq, EXEC_OBJECT_WRITE);
562 i915_vma_unlock(scratch);
564 i915_request_get(rq);
565 i915_request_add(rq);
567 i915_request_put(rq);
574 static int __live_lrc_gpr(struct intel_engine_cs *engine,
575 struct i915_vma *scratch,
578 u32 *slot = memset32(engine->status_page.addr + 1000, 0, 4);
579 struct intel_context *ce;
580 struct i915_request *rq;
585 if (INTEL_GEN(engine->i915) < 9 && engine->class != RENDER_CLASS)
586 return 0; /* GPR only on rcs0 for gen8 */
588 err = gpr_make_dirty(engine->kernel_context);
592 ce = intel_context_create(engine);
596 rq = __gpr_read(ce, scratch, slot);
602 err = wait_for_submit(engine, rq, HZ / 2);
607 err = gpr_make_dirty(engine->kernel_context);
611 err = emit_semaphore_signal(engine->kernel_context, slot);
615 err = wait_for_submit(engine, rq, HZ / 2);
623 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
628 cs = i915_gem_object_pin_map(scratch->obj, I915_MAP_WB);
634 for (n = 0; n < NUM_GPR_DW; n++) {
636 pr_err("%s: GPR[%d].%s was not zero, found 0x%08x!\n",
638 n / 2, n & 1 ? "udw" : "ldw",
645 i915_gem_object_unpin_map(scratch->obj);
648 memset32(&slot[0], -1, 4);
650 i915_request_put(rq);
652 intel_context_put(ce);
656 static int live_lrc_gpr(void *arg)
658 struct intel_gt *gt = arg;
659 struct intel_engine_cs *engine;
660 struct i915_vma *scratch;
661 enum intel_engine_id id;
665 * Check that GPR registers are cleared in new contexts as we need
666 * to avoid leaking any information from previous contexts.
669 scratch = create_scratch(gt);
671 return PTR_ERR(scratch);
673 for_each_engine(engine, gt, id) {
674 st_engine_heartbeat_disable(engine);
676 err = __live_lrc_gpr(engine, scratch, false);
680 err = __live_lrc_gpr(engine, scratch, true);
685 st_engine_heartbeat_enable(engine);
686 if (igt_flush_test(gt->i915))
692 i915_vma_unpin_and_release(&scratch, 0);
696 static struct i915_request *
697 create_timestamp(struct intel_context *ce, void *slot, int idx)
700 i915_ggtt_offset(ce->engine->status_page.vma) +
701 offset_in_page(slot);
702 struct i915_request *rq;
706 rq = intel_context_create_request(ce);
710 cs = intel_ring_begin(rq, 10);
716 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
719 *cs++ = MI_SEMAPHORE_WAIT |
720 MI_SEMAPHORE_GLOBAL_GTT |
722 MI_SEMAPHORE_SAD_NEQ_SDD;
727 *cs++ = MI_STORE_REGISTER_MEM_GEN8 | MI_USE_GGTT;
728 *cs++ = i915_mmio_reg_offset(RING_CTX_TIMESTAMP(rq->engine->mmio_base));
729 *cs++ = offset + idx * sizeof(u32);
732 intel_ring_advance(rq, cs);
734 rq->sched.attr.priority = I915_PRIORITY_MASK;
737 i915_request_get(rq);
738 i915_request_add(rq);
740 i915_request_put(rq);
747 struct lrc_timestamp {
748 struct intel_engine_cs *engine;
749 struct intel_context *ce[2];
753 static bool timestamp_advanced(u32 start, u32 end)
755 return (s32)(end - start) > 0;
758 static int __lrc_timestamp(const struct lrc_timestamp *arg, bool preempt)
760 u32 *slot = memset32(arg->engine->status_page.addr + 1000, 0, 4);
761 struct i915_request *rq;
765 arg->ce[0]->lrc_reg_state[CTX_TIMESTAMP] = arg->poison;
766 rq = create_timestamp(arg->ce[0], slot, 1);
770 err = wait_for_submit(rq->engine, rq, HZ / 2);
775 arg->ce[1]->lrc_reg_state[CTX_TIMESTAMP] = 0xdeadbeef;
776 err = emit_semaphore_signal(arg->ce[1], slot);
784 /* And wait for switch to kernel (to save our context to memory) */
785 err = context_flush(arg->ce[0], HZ / 2);
789 if (!timestamp_advanced(arg->poison, slot[1])) {
790 pr_err("%s(%s): invalid timestamp on restore, context:%x, request:%x\n",
791 arg->engine->name, preempt ? "preempt" : "simple",
792 arg->poison, slot[1]);
796 timestamp = READ_ONCE(arg->ce[0]->lrc_reg_state[CTX_TIMESTAMP]);
797 if (!timestamp_advanced(slot[1], timestamp)) {
798 pr_err("%s(%s): invalid timestamp on save, request:%x, context:%x\n",
799 arg->engine->name, preempt ? "preempt" : "simple",
805 memset32(slot, -1, 4);
806 i915_request_put(rq);
810 static int live_lrc_timestamp(void *arg)
812 struct lrc_timestamp data = {};
813 struct intel_gt *gt = arg;
814 enum intel_engine_id id;
815 const u32 poison[] = {
823 * We want to verify that the timestamp is saved and restore across
824 * context switches and is monotonic.
826 * So we do this with a little bit of LRC poisoning to check various
827 * boundary conditions, and see what happens if we preempt the context
828 * with a second request (carrying more poison into the timestamp).
831 for_each_engine(data.engine, gt, id) {
834 st_engine_heartbeat_disable(data.engine);
836 for (i = 0; i < ARRAY_SIZE(data.ce); i++) {
837 struct intel_context *tmp;
839 tmp = intel_context_create(data.engine);
845 err = intel_context_pin(tmp);
847 intel_context_put(tmp);
854 for (i = 0; i < ARRAY_SIZE(poison); i++) {
855 data.poison = poison[i];
857 err = __lrc_timestamp(&data, false);
861 err = __lrc_timestamp(&data, true);
867 st_engine_heartbeat_enable(data.engine);
868 for (i = 0; i < ARRAY_SIZE(data.ce); i++) {
872 intel_context_unpin(data.ce[i]);
873 intel_context_put(data.ce[i]);
876 if (igt_flush_test(gt->i915))
885 static struct i915_vma *
886 create_user_vma(struct i915_address_space *vm, unsigned long size)
888 struct drm_i915_gem_object *obj;
889 struct i915_vma *vma;
892 obj = i915_gem_object_create_internal(vm->i915, size);
894 return ERR_CAST(obj);
896 vma = i915_vma_instance(obj, vm, NULL);
898 i915_gem_object_put(obj);
902 err = i915_vma_pin(vma, 0, 0, PIN_USER);
904 i915_gem_object_put(obj);
911 static struct i915_vma *
912 store_context(struct intel_context *ce, struct i915_vma *scratch)
914 struct i915_vma *batch;
918 batch = create_user_vma(ce->vm, SZ_64K);
922 cs = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
928 defaults = shmem_pin_map(ce->engine->default_state);
930 i915_gem_object_unpin_map(batch->obj);
932 return ERR_PTR(-ENOMEM);
938 hw += LRC_STATE_OFFSET / sizeof(*hw);
940 u32 len = hw[dw] & 0x7f;
947 if ((hw[dw] & GENMASK(31, 23)) != MI_INSTR(0x22, 0)) {
955 *cs++ = MI_STORE_REGISTER_MEM_GEN8;
957 *cs++ = lower_32_bits(scratch->node.start + x);
958 *cs++ = upper_32_bits(scratch->node.start + x);
963 } while (dw < PAGE_SIZE / sizeof(u32) &&
964 (hw[dw] & ~BIT(0)) != MI_BATCH_BUFFER_END);
966 *cs++ = MI_BATCH_BUFFER_END;
968 shmem_unpin_map(ce->engine->default_state, defaults);
970 i915_gem_object_flush_map(batch->obj);
971 i915_gem_object_unpin_map(batch->obj);
976 static int move_to_active(struct i915_request *rq,
977 struct i915_vma *vma,
983 err = i915_request_await_object(rq, vma->obj, flags);
985 err = i915_vma_move_to_active(vma, rq, flags);
986 i915_vma_unlock(vma);
991 static struct i915_request *
992 record_registers(struct intel_context *ce,
993 struct i915_vma *before,
994 struct i915_vma *after,
997 struct i915_vma *b_before, *b_after;
998 struct i915_request *rq;
1002 b_before = store_context(ce, before);
1003 if (IS_ERR(b_before))
1004 return ERR_CAST(b_before);
1006 b_after = store_context(ce, after);
1007 if (IS_ERR(b_after)) {
1008 rq = ERR_CAST(b_after);
1012 rq = intel_context_create_request(ce);
1016 err = move_to_active(rq, before, EXEC_OBJECT_WRITE);
1020 err = move_to_active(rq, b_before, 0);
1024 err = move_to_active(rq, after, EXEC_OBJECT_WRITE);
1028 err = move_to_active(rq, b_after, 0);
1032 cs = intel_ring_begin(rq, 14);
1038 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1039 *cs++ = MI_BATCH_BUFFER_START_GEN8 | BIT(8);
1040 *cs++ = lower_32_bits(b_before->node.start);
1041 *cs++ = upper_32_bits(b_before->node.start);
1043 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1044 *cs++ = MI_SEMAPHORE_WAIT |
1045 MI_SEMAPHORE_GLOBAL_GTT |
1047 MI_SEMAPHORE_SAD_NEQ_SDD;
1049 *cs++ = i915_ggtt_offset(ce->engine->status_page.vma) +
1050 offset_in_page(sema);
1054 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1055 *cs++ = MI_BATCH_BUFFER_START_GEN8 | BIT(8);
1056 *cs++ = lower_32_bits(b_after->node.start);
1057 *cs++ = upper_32_bits(b_after->node.start);
1059 intel_ring_advance(rq, cs);
1061 WRITE_ONCE(*sema, 0);
1062 i915_request_get(rq);
1063 i915_request_add(rq);
1065 i915_vma_put(b_after);
1067 i915_vma_put(b_before);
1071 i915_request_add(rq);
1076 static struct i915_vma *load_context(struct intel_context *ce, u32 poison)
1078 struct i915_vma *batch;
1082 batch = create_user_vma(ce->vm, SZ_64K);
1086 cs = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
1088 i915_vma_put(batch);
1089 return ERR_CAST(cs);
1092 defaults = shmem_pin_map(ce->engine->default_state);
1094 i915_gem_object_unpin_map(batch->obj);
1095 i915_vma_put(batch);
1096 return ERR_PTR(-ENOMEM);
1101 hw += LRC_STATE_OFFSET / sizeof(*hw);
1103 u32 len = hw[dw] & 0x7f;
1110 if ((hw[dw] & GENMASK(31, 23)) != MI_INSTR(0x22, 0)) {
1116 len = (len + 1) / 2;
1117 *cs++ = MI_LOAD_REGISTER_IMM(len);
1123 } while (dw < PAGE_SIZE / sizeof(u32) &&
1124 (hw[dw] & ~BIT(0)) != MI_BATCH_BUFFER_END);
1126 *cs++ = MI_BATCH_BUFFER_END;
1128 shmem_unpin_map(ce->engine->default_state, defaults);
1130 i915_gem_object_flush_map(batch->obj);
1131 i915_gem_object_unpin_map(batch->obj);
1136 static int poison_registers(struct intel_context *ce, u32 poison, u32 *sema)
1138 struct i915_request *rq;
1139 struct i915_vma *batch;
1143 batch = load_context(ce, poison);
1145 return PTR_ERR(batch);
1147 rq = intel_context_create_request(ce);
1153 err = move_to_active(rq, batch, 0);
1157 cs = intel_ring_begin(rq, 8);
1163 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
1164 *cs++ = MI_BATCH_BUFFER_START_GEN8 | BIT(8);
1165 *cs++ = lower_32_bits(batch->node.start);
1166 *cs++ = upper_32_bits(batch->node.start);
1168 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1169 *cs++ = i915_ggtt_offset(ce->engine->status_page.vma) +
1170 offset_in_page(sema);
1174 intel_ring_advance(rq, cs);
1176 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
1178 i915_request_add(rq);
1180 i915_vma_put(batch);
1184 static bool is_moving(u32 a, u32 b)
1189 static int compare_isolation(struct intel_engine_cs *engine,
1190 struct i915_vma *ref[2],
1191 struct i915_vma *result[2],
1192 struct intel_context *ce,
1195 u32 x, dw, *hw, *lrc;
1200 A[0] = i915_gem_object_pin_map(ref[0]->obj, I915_MAP_WC);
1202 return PTR_ERR(A[0]);
1204 A[1] = i915_gem_object_pin_map(ref[1]->obj, I915_MAP_WC);
1206 err = PTR_ERR(A[1]);
1210 B[0] = i915_gem_object_pin_map(result[0]->obj, I915_MAP_WC);
1212 err = PTR_ERR(B[0]);
1216 B[1] = i915_gem_object_pin_map(result[1]->obj, I915_MAP_WC);
1218 err = PTR_ERR(B[1]);
1222 lrc = i915_gem_object_pin_map(ce->state->obj,
1223 i915_coherent_map_type(engine->i915));
1228 lrc += LRC_STATE_OFFSET / sizeof(*hw);
1230 defaults = shmem_pin_map(ce->engine->default_state);
1239 hw += LRC_STATE_OFFSET / sizeof(*hw);
1241 u32 len = hw[dw] & 0x7f;
1248 if ((hw[dw] & GENMASK(31, 23)) != MI_INSTR(0x22, 0)) {
1254 len = (len + 1) / 2;
1256 if (!is_moving(A[0][x], A[1][x]) &&
1257 (A[0][x] != B[0][x] || A[1][x] != B[1][x])) {
1258 switch (hw[dw] & 4095) {
1259 case 0x30: /* RING_HEAD */
1260 case 0x34: /* RING_TAIL */
1264 pr_err("%s[%d]: Mismatch for register %4x, default %08x, reference %08x, result (%08x, %08x), poison %08x, context %08x\n",
1267 A[0][x], B[0][x], B[1][x],
1268 poison, lrc[dw + 1]);
1275 } while (dw < PAGE_SIZE / sizeof(u32) &&
1276 (hw[dw] & ~BIT(0)) != MI_BATCH_BUFFER_END);
1278 shmem_unpin_map(ce->engine->default_state, defaults);
1280 i915_gem_object_unpin_map(ce->state->obj);
1282 i915_gem_object_unpin_map(result[1]->obj);
1284 i915_gem_object_unpin_map(result[0]->obj);
1286 i915_gem_object_unpin_map(ref[1]->obj);
1288 i915_gem_object_unpin_map(ref[0]->obj);
1292 static int __lrc_isolation(struct intel_engine_cs *engine, u32 poison)
1294 u32 *sema = memset32(engine->status_page.addr + 1000, 0, 1);
1295 struct i915_vma *ref[2], *result[2];
1296 struct intel_context *A, *B;
1297 struct i915_request *rq;
1300 A = intel_context_create(engine);
1304 B = intel_context_create(engine);
1310 ref[0] = create_user_vma(A->vm, SZ_64K);
1311 if (IS_ERR(ref[0])) {
1312 err = PTR_ERR(ref[0]);
1316 ref[1] = create_user_vma(A->vm, SZ_64K);
1317 if (IS_ERR(ref[1])) {
1318 err = PTR_ERR(ref[1]);
1322 rq = record_registers(A, ref[0], ref[1], sema);
1328 WRITE_ONCE(*sema, 1);
1331 if (i915_request_wait(rq, 0, HZ / 2) < 0) {
1332 i915_request_put(rq);
1336 i915_request_put(rq);
1338 result[0] = create_user_vma(A->vm, SZ_64K);
1339 if (IS_ERR(result[0])) {
1340 err = PTR_ERR(result[0]);
1344 result[1] = create_user_vma(A->vm, SZ_64K);
1345 if (IS_ERR(result[1])) {
1346 err = PTR_ERR(result[1]);
1350 rq = record_registers(A, result[0], result[1], sema);
1356 err = poison_registers(B, poison, sema);
1358 WRITE_ONCE(*sema, -1);
1359 i915_request_put(rq);
1363 if (i915_request_wait(rq, 0, HZ / 2) < 0) {
1364 i915_request_put(rq);
1368 i915_request_put(rq);
1370 err = compare_isolation(engine, ref, result, A, poison);
1373 i915_vma_put(result[1]);
1375 i915_vma_put(result[0]);
1377 i915_vma_put(ref[1]);
1379 i915_vma_put(ref[0]);
1381 intel_context_put(B);
1383 intel_context_put(A);
1387 static bool skip_isolation(const struct intel_engine_cs *engine)
1389 if (engine->class == COPY_ENGINE_CLASS && INTEL_GEN(engine->i915) == 9)
1392 if (engine->class == RENDER_CLASS && INTEL_GEN(engine->i915) == 11)
1398 static int live_lrc_isolation(void *arg)
1400 struct intel_gt *gt = arg;
1401 struct intel_engine_cs *engine;
1402 enum intel_engine_id id;
1403 const u32 poison[] = {
1413 * Our goal is try and verify that per-context state cannot be
1414 * tampered with by another non-privileged client.
1416 * We take the list of context registers from the LRI in the default
1417 * context image and attempt to modify that list from a remote context.
1420 for_each_engine(engine, gt, id) {
1423 /* Just don't even ask */
1424 if (!IS_ENABLED(CONFIG_DRM_I915_SELFTEST_BROKEN) &&
1425 skip_isolation(engine))
1428 intel_engine_pm_get(engine);
1429 for (i = 0; i < ARRAY_SIZE(poison); i++) {
1432 result = __lrc_isolation(engine, poison[i]);
1436 result = __lrc_isolation(engine, ~poison[i]);
1440 intel_engine_pm_put(engine);
1441 if (igt_flush_test(gt->i915)) {
1450 static int indirect_ctx_submit_req(struct intel_context *ce)
1452 struct i915_request *rq;
1455 rq = intel_context_create_request(ce);
1459 i915_request_get(rq);
1460 i915_request_add(rq);
1462 if (i915_request_wait(rq, 0, HZ / 5) < 0)
1465 i915_request_put(rq);
1470 #define CTX_BB_CANARY_OFFSET (3 * 1024)
1471 #define CTX_BB_CANARY_INDEX (CTX_BB_CANARY_OFFSET / sizeof(u32))
1474 emit_indirect_ctx_bb_canary(const struct intel_context *ce, u32 *cs)
1476 *cs++ = MI_STORE_REGISTER_MEM_GEN8 |
1477 MI_SRM_LRM_GLOBAL_GTT |
1479 *cs++ = i915_mmio_reg_offset(RING_START(0));
1480 *cs++ = i915_ggtt_offset(ce->state) +
1481 context_wa_bb_offset(ce) +
1482 CTX_BB_CANARY_OFFSET;
1489 indirect_ctx_bb_setup(struct intel_context *ce)
1491 u32 *cs = context_indirect_bb(ce);
1493 cs[CTX_BB_CANARY_INDEX] = 0xdeadf00d;
1495 setup_indirect_ctx_bb(ce, ce->engine, emit_indirect_ctx_bb_canary);
1498 static bool check_ring_start(struct intel_context *ce)
1500 const u32 * const ctx_bb = (void *)(ce->lrc_reg_state) -
1501 LRC_STATE_OFFSET + context_wa_bb_offset(ce);
1503 if (ctx_bb[CTX_BB_CANARY_INDEX] == ce->lrc_reg_state[CTX_RING_START])
1506 pr_err("ring start mismatch: canary 0x%08x vs state 0x%08x\n",
1507 ctx_bb[CTX_BB_CANARY_INDEX],
1508 ce->lrc_reg_state[CTX_RING_START]);
1513 static int indirect_ctx_bb_check(struct intel_context *ce)
1517 err = indirect_ctx_submit_req(ce);
1521 if (!check_ring_start(ce))
1527 static int __live_lrc_indirect_ctx_bb(struct intel_engine_cs *engine)
1529 struct intel_context *a, *b;
1532 a = intel_context_create(engine);
1535 err = intel_context_pin(a);
1539 b = intel_context_create(engine);
1544 err = intel_context_pin(b);
1548 /* We use the already reserved extra page in context state */
1549 if (!a->wa_bb_page) {
1550 GEM_BUG_ON(b->wa_bb_page);
1551 GEM_BUG_ON(INTEL_GEN(engine->i915) == 12);
1556 * In order to test that our per context bb is truly per context,
1557 * and executes at the intended spot on context restoring process,
1558 * make the batch store the ring start value to memory.
1559 * As ring start is restored apriori of starting the indirect ctx bb and
1560 * as it will be different for each context, it fits to this purpose.
1562 indirect_ctx_bb_setup(a);
1563 indirect_ctx_bb_setup(b);
1565 err = indirect_ctx_bb_check(a);
1569 err = indirect_ctx_bb_check(b);
1572 intel_context_unpin(b);
1574 intel_context_put(b);
1576 intel_context_unpin(a);
1578 intel_context_put(a);
1583 static int live_lrc_indirect_ctx_bb(void *arg)
1585 struct intel_gt *gt = arg;
1586 struct intel_engine_cs *engine;
1587 enum intel_engine_id id;
1590 for_each_engine(engine, gt, id) {
1591 intel_engine_pm_get(engine);
1592 err = __live_lrc_indirect_ctx_bb(engine);
1593 intel_engine_pm_put(engine);
1595 if (igt_flush_test(gt->i915))
1605 static void garbage_reset(struct intel_engine_cs *engine,
1606 struct i915_request *rq)
1608 const unsigned int bit = I915_RESET_ENGINE + engine->id;
1609 unsigned long *lock = &engine->gt->reset.flags;
1612 if (!test_and_set_bit(bit, lock)) {
1613 tasklet_disable(&engine->execlists.tasklet);
1615 if (!rq->fence.error)
1616 __intel_engine_reset_bh(engine, NULL);
1618 tasklet_enable(&engine->execlists.tasklet);
1619 clear_and_wake_up_bit(bit, lock);
1624 static struct i915_request *garbage(struct intel_context *ce,
1625 struct rnd_state *prng)
1627 struct i915_request *rq;
1630 err = intel_context_pin(ce);
1632 return ERR_PTR(err);
1634 prandom_bytes_state(prng,
1636 ce->engine->context_size -
1639 rq = intel_context_create_request(ce);
1645 i915_request_get(rq);
1646 i915_request_add(rq);
1650 intel_context_unpin(ce);
1651 return ERR_PTR(err);
1654 static int __lrc_garbage(struct intel_engine_cs *engine, struct rnd_state *prng)
1656 struct intel_context *ce;
1657 struct i915_request *hang;
1660 ce = intel_context_create(engine);
1664 hang = garbage(ce, prng);
1666 err = PTR_ERR(hang);
1670 if (wait_for_submit(engine, hang, HZ / 2)) {
1671 i915_request_put(hang);
1676 intel_context_set_banned(ce);
1677 garbage_reset(engine, hang);
1679 intel_engine_flush_submission(engine);
1680 if (!hang->fence.error) {
1681 i915_request_put(hang);
1682 pr_err("%s: corrupted context was not reset\n",
1688 if (i915_request_wait(hang, 0, HZ / 2) < 0) {
1689 pr_err("%s: corrupted context did not recover\n",
1691 i915_request_put(hang);
1695 i915_request_put(hang);
1698 intel_context_put(ce);
1702 static int live_lrc_garbage(void *arg)
1704 struct intel_gt *gt = arg;
1705 struct intel_engine_cs *engine;
1706 enum intel_engine_id id;
1709 * Verify that we can recover if one context state is completely
1713 if (!IS_ENABLED(CONFIG_DRM_I915_SELFTEST_BROKEN))
1716 for_each_engine(engine, gt, id) {
1717 I915_RND_STATE(prng);
1720 if (!intel_has_reset_engine(engine->gt))
1723 intel_engine_pm_get(engine);
1724 for (i = 0; i < 3; i++) {
1725 err = __lrc_garbage(engine, &prng);
1729 intel_engine_pm_put(engine);
1731 if (igt_flush_test(gt->i915))
1740 static int __live_pphwsp_runtime(struct intel_engine_cs *engine)
1742 struct intel_context *ce;
1743 struct i915_request *rq;
1744 IGT_TIMEOUT(end_time);
1747 ce = intel_context_create(engine);
1751 ce->runtime.num_underflow = 0;
1752 ce->runtime.max_underflow = 0;
1755 unsigned int loop = 1024;
1758 rq = intel_context_create_request(ce);
1765 i915_request_get(rq);
1767 i915_request_add(rq);
1770 if (__igt_timeout(end_time, NULL))
1773 i915_request_put(rq);
1776 err = i915_request_wait(rq, 0, HZ / 5);
1778 pr_err("%s: request not completed!\n", engine->name);
1782 igt_flush_test(engine->i915);
1784 pr_info("%s: pphwsp runtime %lluns, average %lluns\n",
1786 intel_context_get_total_runtime_ns(ce),
1787 intel_context_get_avg_runtime_ns(ce));
1790 if (ce->runtime.num_underflow) {
1791 pr_err("%s: pphwsp underflow %u time(s), max %u cycles!\n",
1793 ce->runtime.num_underflow,
1794 ce->runtime.max_underflow);
1800 i915_request_put(rq);
1802 intel_context_put(ce);
1806 static int live_pphwsp_runtime(void *arg)
1808 struct intel_gt *gt = arg;
1809 struct intel_engine_cs *engine;
1810 enum intel_engine_id id;
1814 * Check that cumulative context runtime as stored in the pphwsp[16]
1818 for_each_engine(engine, gt, id) {
1819 err = __live_pphwsp_runtime(engine);
1824 if (igt_flush_test(gt->i915))
1830 int intel_lrc_live_selftests(struct drm_i915_private *i915)
1832 static const struct i915_subtest tests[] = {
1833 SUBTEST(live_lrc_layout),
1834 SUBTEST(live_lrc_fixed),
1835 SUBTEST(live_lrc_state),
1836 SUBTEST(live_lrc_gpr),
1837 SUBTEST(live_lrc_isolation),
1838 SUBTEST(live_lrc_timestamp),
1839 SUBTEST(live_lrc_garbage),
1840 SUBTEST(live_pphwsp_runtime),
1841 SUBTEST(live_lrc_indirect_ctx_bb),
1844 if (!HAS_LOGICAL_RING_CONTEXTS(i915))
1847 return intel_gt_live_subtests(tests, &i915->gt);