1 // SPDX-License-Identifier: MIT
3 * Copyright © 2018 Intel Corporation
6 #include <linux/prime_numbers.h>
8 #include "gem/i915_gem_pm.h"
9 #include "gt/intel_engine_heartbeat.h"
10 #include "gt/intel_reset.h"
11 #include "gt/selftest_engine_heartbeat.h"
13 #include "i915_selftest.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"
20 #include "gem/selftests/igt_gem_utils.h"
21 #include "gem/selftests/mock_context.h"
23 #define CS_GPR(engine, n) ((engine)->mmio_base + 0x600 + (n) * 4)
25 #define NUM_GPR_DW (NUM_GPR * 2) /* each GPR is 2 dwords */
27 static bool is_active(struct i915_request *rq)
29 if (i915_request_is_active(rq))
32 if (i915_request_on_hold(rq))
35 if (i915_request_has_initial_breadcrumb(rq) && i915_request_started(rq))
41 static int wait_for_submit(struct intel_engine_cs *engine,
42 struct i915_request *rq,
43 unsigned long timeout)
45 /* Ignore our own attempts to suppress excess tasklets */
46 tasklet_hi_schedule(&engine->sched_engine->tasklet);
50 bool done = time_after(jiffies, timeout);
52 if (i915_request_completed(rq)) /* that was quick! */
55 /* Wait until the HW has acknowleged the submission (or err) */
56 intel_engine_flush_submission(engine);
57 if (!READ_ONCE(engine->execlists.pending[0]) && is_active(rq))
67 static int wait_for_reset(struct intel_engine_cs *engine,
68 struct i915_request *rq,
69 unsigned long timeout)
75 intel_engine_flush_submission(engine);
77 if (READ_ONCE(engine->execlists.pending[0]))
80 if (i915_request_completed(rq))
83 if (READ_ONCE(rq->fence.error))
85 } while (time_before(jiffies, timeout));
87 flush_scheduled_work();
89 if (rq->fence.error != -EIO) {
90 pr_err("%s: hanging request %llx:%lld not reset\n",
97 /* Give the request a jiffie to complete after flushing the worker */
98 if (i915_request_wait(rq, 0,
99 max(0l, (long)(timeout - jiffies)) + 1) < 0) {
100 pr_err("%s: hanging request %llx:%lld did not complete\n",
110 static int live_sanitycheck(void *arg)
112 struct intel_gt *gt = arg;
113 struct intel_engine_cs *engine;
114 enum intel_engine_id id;
115 struct igt_spinner spin;
118 if (!HAS_LOGICAL_RING_CONTEXTS(gt->i915))
121 if (igt_spinner_init(&spin, gt))
124 for_each_engine(engine, gt, id) {
125 struct intel_context *ce;
126 struct i915_request *rq;
128 ce = intel_context_create(engine);
134 rq = igt_spinner_create_request(&spin, ce, MI_NOOP);
140 i915_request_add(rq);
141 if (!igt_wait_for_spinner(&spin, rq)) {
142 GEM_TRACE("spinner failed to start\n");
144 intel_gt_set_wedged(gt);
149 igt_spinner_end(&spin);
150 if (igt_flush_test(gt->i915)) {
156 intel_context_put(ce);
161 igt_spinner_fini(&spin);
165 static int live_unlite_restore(struct intel_gt *gt, int prio)
167 struct intel_engine_cs *engine;
168 enum intel_engine_id id;
169 struct igt_spinner spin;
173 * Check that we can correctly context switch between 2 instances
174 * on the same engine from the same parent context.
177 if (igt_spinner_init(&spin, gt))
181 for_each_engine(engine, gt, id) {
182 struct intel_context *ce[2] = {};
183 struct i915_request *rq[2];
184 struct igt_live_test t;
187 if (prio && !intel_engine_has_preemption(engine))
190 if (!intel_engine_can_store_dword(engine))
193 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
197 st_engine_heartbeat_disable(engine);
199 for (n = 0; n < ARRAY_SIZE(ce); n++) {
200 struct intel_context *tmp;
202 tmp = intel_context_create(engine);
208 err = intel_context_pin(tmp);
210 intel_context_put(tmp);
215 * Setup the pair of contexts such that if we
216 * lite-restore using the RING_TAIL from ce[1] it
217 * will execute garbage from ce[0]->ring.
219 memset(tmp->ring->vaddr,
220 POISON_INUSE, /* IPEHR: 0x5a5a5a5a [hung!] */
221 tmp->ring->vma->size);
225 GEM_BUG_ON(!ce[1]->ring->size);
226 intel_ring_reset(ce[1]->ring, ce[1]->ring->size / 2);
227 lrc_update_regs(ce[1], engine, ce[1]->ring->head);
229 rq[0] = igt_spinner_create_request(&spin, ce[0], MI_ARB_CHECK);
231 err = PTR_ERR(rq[0]);
235 i915_request_get(rq[0]);
236 i915_request_add(rq[0]);
237 GEM_BUG_ON(rq[0]->postfix > ce[1]->ring->emit);
239 if (!igt_wait_for_spinner(&spin, rq[0])) {
240 i915_request_put(rq[0]);
244 rq[1] = i915_request_create(ce[1]);
246 err = PTR_ERR(rq[1]);
247 i915_request_put(rq[0]);
253 * Ensure we do the switch to ce[1] on completion.
255 * rq[0] is already submitted, so this should reduce
256 * to a no-op (a wait on a request on the same engine
257 * uses the submit fence, not the completion fence),
258 * but it will install a dependency on rq[1] for rq[0]
259 * that will prevent the pair being reordered by
262 i915_request_await_dma_fence(rq[1], &rq[0]->fence);
265 i915_request_get(rq[1]);
266 i915_request_add(rq[1]);
267 GEM_BUG_ON(rq[1]->postfix <= rq[0]->postfix);
268 i915_request_put(rq[0]);
271 struct i915_sched_attr attr = {
275 /* Alternatively preempt the spinner with ce[1] */
276 engine->sched_engine->schedule(rq[1], &attr);
279 /* And switch back to ce[0] for good measure */
280 rq[0] = i915_request_create(ce[0]);
282 err = PTR_ERR(rq[0]);
283 i915_request_put(rq[1]);
287 i915_request_await_dma_fence(rq[0], &rq[1]->fence);
288 i915_request_get(rq[0]);
289 i915_request_add(rq[0]);
290 GEM_BUG_ON(rq[0]->postfix > rq[1]->postfix);
291 i915_request_put(rq[1]);
292 i915_request_put(rq[0]);
295 intel_engine_flush_submission(engine);
296 igt_spinner_end(&spin);
297 for (n = 0; n < ARRAY_SIZE(ce); n++) {
298 if (IS_ERR_OR_NULL(ce[n]))
301 intel_context_unpin(ce[n]);
302 intel_context_put(ce[n]);
305 st_engine_heartbeat_enable(engine);
306 if (igt_live_test_end(&t))
312 igt_spinner_fini(&spin);
316 static int live_unlite_switch(void *arg)
318 return live_unlite_restore(arg, 0);
321 static int live_unlite_preempt(void *arg)
323 return live_unlite_restore(arg, I915_PRIORITY_MAX);
326 static int live_unlite_ring(void *arg)
328 struct intel_gt *gt = arg;
329 struct intel_engine_cs *engine;
330 struct igt_spinner spin;
331 enum intel_engine_id id;
335 * Setup a preemption event that will cause almost the entire ring
336 * to be unwound, potentially fooling our intel_ring_direction()
337 * into emitting a forward lite-restore instead of the rollback.
340 if (igt_spinner_init(&spin, gt))
343 for_each_engine(engine, gt, id) {
344 struct intel_context *ce[2] = {};
345 struct i915_request *rq;
346 struct igt_live_test t;
349 if (!intel_engine_has_preemption(engine))
352 if (!intel_engine_can_store_dword(engine))
355 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
359 st_engine_heartbeat_disable(engine);
361 for (n = 0; n < ARRAY_SIZE(ce); n++) {
362 struct intel_context *tmp;
364 tmp = intel_context_create(engine);
370 err = intel_context_pin(tmp);
372 intel_context_put(tmp);
376 memset32(tmp->ring->vaddr,
377 0xdeadbeef, /* trigger a hang if executed */
378 tmp->ring->vma->size / sizeof(u32));
383 /* Create max prio spinner, followed by N low prio nops */
384 rq = igt_spinner_create_request(&spin, ce[0], MI_ARB_CHECK);
390 i915_request_get(rq);
391 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
392 i915_request_add(rq);
394 if (!igt_wait_for_spinner(&spin, rq)) {
395 intel_gt_set_wedged(gt);
396 i915_request_put(rq);
401 /* Fill the ring, until we will cause a wrap */
403 while (intel_ring_direction(ce[0]->ring,
405 ce[0]->ring->tail) <= 0) {
406 struct i915_request *tmp;
408 tmp = intel_context_create_request(ce[0]);
411 i915_request_put(rq);
415 i915_request_add(tmp);
416 intel_engine_flush_submission(engine);
419 intel_engine_flush_submission(engine);
420 pr_debug("%s: Filled ring with %d nop tails {size:%x, tail:%x, emit:%x, rq.tail:%x}\n",
426 GEM_BUG_ON(intel_ring_direction(ce[0]->ring,
428 ce[0]->ring->tail) <= 0);
429 i915_request_put(rq);
431 /* Create a second ring to preempt the first ring after rq[0] */
432 rq = intel_context_create_request(ce[1]);
438 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
439 i915_request_get(rq);
440 i915_request_add(rq);
442 err = wait_for_submit(engine, rq, HZ / 2);
443 i915_request_put(rq);
445 pr_err("%s: preemption request was not submitted\n",
450 pr_debug("%s: ring[0]:{ tail:%x, emit:%x }, ring[1]:{ tail:%x, emit:%x }\n",
452 ce[0]->ring->tail, ce[0]->ring->emit,
453 ce[1]->ring->tail, ce[1]->ring->emit);
456 intel_engine_flush_submission(engine);
457 igt_spinner_end(&spin);
458 for (n = 0; n < ARRAY_SIZE(ce); n++) {
459 if (IS_ERR_OR_NULL(ce[n]))
462 intel_context_unpin(ce[n]);
463 intel_context_put(ce[n]);
465 st_engine_heartbeat_enable(engine);
466 if (igt_live_test_end(&t))
472 igt_spinner_fini(&spin);
476 static int live_pin_rewind(void *arg)
478 struct intel_gt *gt = arg;
479 struct intel_engine_cs *engine;
480 enum intel_engine_id id;
484 * We have to be careful not to trust intel_ring too much, for example
485 * ring->head is updated upon retire which is out of sync with pinning
486 * the context. Thus we cannot use ring->head to set CTX_RING_HEAD,
487 * or else we risk writing an older, stale value.
489 * To simulate this, let's apply a bit of deliberate sabotague.
492 for_each_engine(engine, gt, id) {
493 struct intel_context *ce;
494 struct i915_request *rq;
495 struct intel_ring *ring;
496 struct igt_live_test t;
498 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
503 ce = intel_context_create(engine);
509 err = intel_context_pin(ce);
511 intel_context_put(ce);
515 /* Keep the context awake while we play games */
516 err = i915_active_acquire(&ce->active);
518 intel_context_unpin(ce);
519 intel_context_put(ce);
524 /* Poison the ring, and offset the next request from HEAD */
525 memset32(ring->vaddr, STACK_MAGIC, ring->size / sizeof(u32));
526 ring->emit = ring->size / 2;
527 ring->tail = ring->emit;
528 GEM_BUG_ON(ring->head);
530 intel_context_unpin(ce);
532 /* Submit a simple nop request */
533 GEM_BUG_ON(intel_context_is_pinned(ce));
534 rq = intel_context_create_request(ce);
535 i915_active_release(&ce->active); /* e.g. async retire */
536 intel_context_put(ce);
541 GEM_BUG_ON(!rq->head);
542 i915_request_add(rq);
544 /* Expect not to hang! */
545 if (igt_live_test_end(&t)) {
554 static int engine_lock_reset_tasklet(struct intel_engine_cs *engine)
556 tasklet_disable(&engine->sched_engine->tasklet);
559 if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
560 &engine->gt->reset.flags)) {
562 tasklet_enable(&engine->sched_engine->tasklet);
564 intel_gt_set_wedged(engine->gt);
571 static void engine_unlock_reset_tasklet(struct intel_engine_cs *engine)
573 clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
574 &engine->gt->reset.flags);
577 tasklet_enable(&engine->sched_engine->tasklet);
580 static int live_hold_reset(void *arg)
582 struct intel_gt *gt = arg;
583 struct intel_engine_cs *engine;
584 enum intel_engine_id id;
585 struct igt_spinner spin;
589 * In order to support offline error capture for fast preempt reset,
590 * we need to decouple the guilty request and ensure that it and its
591 * descendents are not executed while the capture is in progress.
594 if (!intel_has_reset_engine(gt))
597 if (igt_spinner_init(&spin, gt))
600 for_each_engine(engine, gt, id) {
601 struct intel_context *ce;
602 struct i915_request *rq;
604 ce = intel_context_create(engine);
610 st_engine_heartbeat_disable(engine);
612 rq = igt_spinner_create_request(&spin, ce, MI_ARB_CHECK);
617 i915_request_add(rq);
619 if (!igt_wait_for_spinner(&spin, rq)) {
620 intel_gt_set_wedged(gt);
625 /* We have our request executing, now remove it and reset */
627 err = engine_lock_reset_tasklet(engine);
631 engine->sched_engine->tasklet.callback(&engine->sched_engine->tasklet);
632 GEM_BUG_ON(execlists_active(&engine->execlists) != rq);
634 i915_request_get(rq);
635 execlists_hold(engine, rq);
636 GEM_BUG_ON(!i915_request_on_hold(rq));
638 __intel_engine_reset_bh(engine, NULL);
639 GEM_BUG_ON(rq->fence.error != -EIO);
641 engine_unlock_reset_tasklet(engine);
643 /* Check that we do not resubmit the held request */
644 if (!i915_request_wait(rq, 0, HZ / 5)) {
645 pr_err("%s: on hold request completed!\n",
647 i915_request_put(rq);
651 GEM_BUG_ON(!i915_request_on_hold(rq));
653 /* But is resubmitted on release */
654 execlists_unhold(engine, rq);
655 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
656 pr_err("%s: held request did not complete!\n",
658 intel_gt_set_wedged(gt);
661 i915_request_put(rq);
664 st_engine_heartbeat_enable(engine);
665 intel_context_put(ce);
670 igt_spinner_fini(&spin);
674 static const char *error_repr(int err)
676 return err ? "bad" : "good";
679 static int live_error_interrupt(void *arg)
681 static const struct error_phase {
682 enum { GOOD = 0, BAD = -EIO } error[2];
687 { { GOOD, GOOD } }, /* sentinel */
689 struct intel_gt *gt = arg;
690 struct intel_engine_cs *engine;
691 enum intel_engine_id id;
694 * We hook up the CS_MASTER_ERROR_INTERRUPT to have forewarning
695 * of invalid commands in user batches that will cause a GPU hang.
696 * This is a faster mechanism than using hangcheck/heartbeats, but
697 * only detects problems the HW knows about -- it will not warn when
700 * To verify our detection and reset, we throw some invalid commands
701 * at the HW and wait for the interrupt.
704 if (!intel_has_reset_engine(gt))
707 for_each_engine(engine, gt, id) {
708 const struct error_phase *p;
711 st_engine_heartbeat_disable(engine);
713 for (p = phases; p->error[0] != GOOD; p++) {
714 struct i915_request *client[ARRAY_SIZE(phases->error)];
718 memset(client, 0, sizeof(*client));
719 for (i = 0; i < ARRAY_SIZE(client); i++) {
720 struct intel_context *ce;
721 struct i915_request *rq;
723 ce = intel_context_create(engine);
729 rq = intel_context_create_request(ce);
730 intel_context_put(ce);
736 if (rq->engine->emit_init_breadcrumb) {
737 err = rq->engine->emit_init_breadcrumb(rq);
739 i915_request_add(rq);
744 cs = intel_ring_begin(rq, 2);
746 i915_request_add(rq);
759 client[i] = i915_request_get(rq);
760 i915_request_add(rq);
763 err = wait_for_submit(engine, client[0], HZ / 2);
765 pr_err("%s: first request did not start within time!\n",
771 for (i = 0; i < ARRAY_SIZE(client); i++) {
772 if (i915_request_wait(client[i], 0, HZ / 5) < 0)
773 pr_debug("%s: %s request incomplete!\n",
775 error_repr(p->error[i]));
777 if (!i915_request_started(client[i])) {
778 pr_err("%s: %s request not started!\n",
780 error_repr(p->error[i]));
785 /* Kick the tasklet to process the error */
786 intel_engine_flush_submission(engine);
787 if (client[i]->fence.error != p->error[i]) {
788 pr_err("%s: %s request (%s) with wrong error code: %d\n",
790 error_repr(p->error[i]),
791 i915_request_completed(client[i]) ? "completed" : "running",
792 client[i]->fence.error);
799 for (i = 0; i < ARRAY_SIZE(client); i++)
801 i915_request_put(client[i]);
803 pr_err("%s: failed at phase[%zd] { %d, %d }\n",
804 engine->name, p - phases,
805 p->error[0], p->error[1]);
810 st_engine_heartbeat_enable(engine);
812 intel_gt_set_wedged(gt);
821 emit_semaphore_chain(struct i915_request *rq, struct i915_vma *vma, int idx)
825 cs = intel_ring_begin(rq, 10);
829 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
831 *cs++ = MI_SEMAPHORE_WAIT |
832 MI_SEMAPHORE_GLOBAL_GTT |
834 MI_SEMAPHORE_SAD_NEQ_SDD;
836 *cs++ = i915_ggtt_offset(vma) + 4 * idx;
840 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
841 *cs++ = i915_ggtt_offset(vma) + 4 * (idx - 1);
851 *cs++ = MI_ARB_ON_OFF | MI_ARB_DISABLE;
853 intel_ring_advance(rq, cs);
857 static struct i915_request *
858 semaphore_queue(struct intel_engine_cs *engine, struct i915_vma *vma, int idx)
860 struct intel_context *ce;
861 struct i915_request *rq;
864 ce = intel_context_create(engine);
868 rq = intel_context_create_request(ce);
873 if (rq->engine->emit_init_breadcrumb)
874 err = rq->engine->emit_init_breadcrumb(rq);
876 err = emit_semaphore_chain(rq, vma, idx);
878 i915_request_get(rq);
879 i915_request_add(rq);
884 intel_context_put(ce);
889 release_queue(struct intel_engine_cs *engine,
890 struct i915_vma *vma,
893 struct i915_sched_attr attr = {
896 struct i915_request *rq;
899 rq = intel_engine_create_kernel_request(engine);
903 cs = intel_ring_begin(rq, 4);
905 i915_request_add(rq);
909 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
910 *cs++ = i915_ggtt_offset(vma) + 4 * (idx - 1);
914 intel_ring_advance(rq, cs);
916 i915_request_get(rq);
917 i915_request_add(rq);
920 engine->sched_engine->schedule(rq, &attr);
921 local_bh_enable(); /* kick tasklet */
923 i915_request_put(rq);
929 slice_semaphore_queue(struct intel_engine_cs *outer,
930 struct i915_vma *vma,
933 struct intel_engine_cs *engine;
934 struct i915_request *head;
935 enum intel_engine_id id;
938 head = semaphore_queue(outer, vma, n++);
940 return PTR_ERR(head);
942 for_each_engine(engine, outer->gt, id) {
943 if (!intel_engine_has_preemption(engine))
946 for (i = 0; i < count; i++) {
947 struct i915_request *rq;
949 rq = semaphore_queue(engine, vma, n++);
955 i915_request_put(rq);
959 err = release_queue(outer, vma, n, I915_PRIORITY_BARRIER);
963 if (i915_request_wait(head, 0,
964 2 * outer->gt->info.num_engines * (count + 2) * (count + 3)) < 0) {
965 pr_err("%s: Failed to slice along semaphore chain of length (%d, %d)!\n",
966 outer->name, count, n);
968 intel_gt_set_wedged(outer->gt);
973 i915_request_put(head);
977 static int live_timeslice_preempt(void *arg)
979 struct intel_gt *gt = arg;
980 struct drm_i915_gem_object *obj;
981 struct intel_engine_cs *engine;
982 enum intel_engine_id id;
983 struct i915_vma *vma;
988 * If a request takes too long, we would like to give other users
989 * a fair go on the GPU. In particular, users may create batches
990 * that wait upon external input, where that input may even be
991 * supplied by another GPU job. To avoid blocking forever, we
992 * need to preempt the current task and replace it with another
995 if (!IS_ACTIVE(CONFIG_DRM_I915_TIMESLICE_DURATION))
998 obj = i915_gem_object_create_internal(gt->i915, PAGE_SIZE);
1000 return PTR_ERR(obj);
1002 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
1008 vaddr = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
1009 if (IS_ERR(vaddr)) {
1010 err = PTR_ERR(vaddr);
1014 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
1018 err = i915_vma_sync(vma);
1022 for_each_engine(engine, gt, id) {
1023 if (!intel_engine_has_preemption(engine))
1026 memset(vaddr, 0, PAGE_SIZE);
1028 st_engine_heartbeat_disable(engine);
1029 err = slice_semaphore_queue(engine, vma, 5);
1030 st_engine_heartbeat_enable(engine);
1034 if (igt_flush_test(gt->i915)) {
1041 i915_vma_unpin(vma);
1043 i915_gem_object_unpin_map(obj);
1045 i915_gem_object_put(obj);
1049 static struct i915_request *
1050 create_rewinder(struct intel_context *ce,
1051 struct i915_request *wait,
1052 void *slot, int idx)
1055 i915_ggtt_offset(ce->engine->status_page.vma) +
1056 offset_in_page(slot);
1057 struct i915_request *rq;
1061 rq = intel_context_create_request(ce);
1066 err = i915_request_await_dma_fence(rq, &wait->fence);
1071 cs = intel_ring_begin(rq, 14);
1077 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
1080 *cs++ = MI_SEMAPHORE_WAIT |
1081 MI_SEMAPHORE_GLOBAL_GTT |
1083 MI_SEMAPHORE_SAD_GTE_SDD;
1088 *cs++ = MI_STORE_REGISTER_MEM_GEN8 | MI_USE_GGTT;
1089 *cs++ = i915_mmio_reg_offset(RING_TIMESTAMP(rq->engine->mmio_base));
1090 *cs++ = offset + idx * sizeof(u32);
1093 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1098 intel_ring_advance(rq, cs);
1102 i915_request_get(rq);
1103 i915_request_add(rq);
1105 i915_request_put(rq);
1106 return ERR_PTR(err);
1112 static int live_timeslice_rewind(void *arg)
1114 struct intel_gt *gt = arg;
1115 struct intel_engine_cs *engine;
1116 enum intel_engine_id id;
1119 * The usual presumption on timeslice expiration is that we replace
1120 * the active context with another. However, given a chain of
1121 * dependencies we may end up with replacing the context with itself,
1122 * but only a few of those requests, forcing us to rewind the
1123 * RING_TAIL of the original request.
1125 if (!IS_ACTIVE(CONFIG_DRM_I915_TIMESLICE_DURATION))
1128 for_each_engine(engine, gt, id) {
1129 enum { A1, A2, B1 };
1130 enum { X = 1, Z, Y };
1131 struct i915_request *rq[3] = {};
1132 struct intel_context *ce;
1133 unsigned long timeslice;
1137 if (!intel_engine_has_timeslices(engine))
1141 * A:rq1 -- semaphore wait, timestamp X
1142 * A:rq2 -- write timestamp Y
1144 * B:rq1 [await A:rq1] -- write timestamp Z
1146 * Force timeslice, release semaphore.
1148 * Expect execution/evaluation order XZY
1151 st_engine_heartbeat_disable(engine);
1152 timeslice = xchg(&engine->props.timeslice_duration_ms, 1);
1154 slot = memset32(engine->status_page.addr + 1000, 0, 4);
1156 ce = intel_context_create(engine);
1162 rq[A1] = create_rewinder(ce, NULL, slot, X);
1163 if (IS_ERR(rq[A1])) {
1164 intel_context_put(ce);
1168 rq[A2] = create_rewinder(ce, NULL, slot, Y);
1169 intel_context_put(ce);
1173 err = wait_for_submit(engine, rq[A2], HZ / 2);
1175 pr_err("%s: failed to submit first context\n",
1180 ce = intel_context_create(engine);
1186 rq[B1] = create_rewinder(ce, rq[A1], slot, Z);
1187 intel_context_put(ce);
1191 err = wait_for_submit(engine, rq[B1], HZ / 2);
1193 pr_err("%s: failed to submit second context\n",
1198 /* ELSP[] = { { A:rq1, A:rq2 }, { B:rq1 } } */
1199 ENGINE_TRACE(engine, "forcing tasklet for rewind\n");
1200 while (i915_request_is_active(rq[A2])) { /* semaphore yield! */
1201 /* Wait for the timeslice to kick in */
1202 del_timer(&engine->execlists.timer);
1203 tasklet_hi_schedule(&engine->sched_engine->tasklet);
1204 intel_engine_flush_submission(engine);
1206 /* -> ELSP[] = { { A:rq1 }, { B:rq1 } } */
1207 GEM_BUG_ON(!i915_request_is_active(rq[A1]));
1208 GEM_BUG_ON(!i915_request_is_active(rq[B1]));
1209 GEM_BUG_ON(i915_request_is_active(rq[A2]));
1211 /* Release the hounds! */
1213 wmb(); /* "pairs" with GPU; paranoid kick of internal CPU$ */
1215 for (i = 1; i <= 3; i++) {
1216 unsigned long timeout = jiffies + HZ / 2;
1218 while (!READ_ONCE(slot[i]) &&
1219 time_before(jiffies, timeout))
1222 if (!time_before(jiffies, timeout)) {
1223 pr_err("%s: rq[%d] timed out\n",
1224 engine->name, i - 1);
1229 pr_debug("%s: slot[%d]:%x\n", engine->name, i, slot[i]);
1233 if (slot[Z] - slot[X] >= slot[Y] - slot[X]) {
1234 pr_err("%s: timeslicing did not run context B [%u] before A [%u]!\n",
1242 memset32(&slot[0], -1, 4);
1245 engine->props.timeslice_duration_ms = timeslice;
1246 st_engine_heartbeat_enable(engine);
1247 for (i = 0; i < 3; i++)
1248 i915_request_put(rq[i]);
1249 if (igt_flush_test(gt->i915))
1258 static struct i915_request *nop_request(struct intel_engine_cs *engine)
1260 struct i915_request *rq;
1262 rq = intel_engine_create_kernel_request(engine);
1266 i915_request_get(rq);
1267 i915_request_add(rq);
1272 static long slice_timeout(struct intel_engine_cs *engine)
1276 /* Enough time for a timeslice to kick in, and kick out */
1277 timeout = 2 * msecs_to_jiffies_timeout(timeslice(engine));
1279 /* Enough time for the nop request to complete */
1285 static int live_timeslice_queue(void *arg)
1287 struct intel_gt *gt = arg;
1288 struct drm_i915_gem_object *obj;
1289 struct intel_engine_cs *engine;
1290 enum intel_engine_id id;
1291 struct i915_vma *vma;
1296 * Make sure that even if ELSP[0] and ELSP[1] are filled with
1297 * timeslicing between them disabled, we *do* enable timeslicing
1298 * if the queue demands it. (Normally, we do not submit if
1299 * ELSP[1] is already occupied, so must rely on timeslicing to
1300 * eject ELSP[0] in favour of the queue.)
1302 if (!IS_ACTIVE(CONFIG_DRM_I915_TIMESLICE_DURATION))
1305 obj = i915_gem_object_create_internal(gt->i915, PAGE_SIZE);
1307 return PTR_ERR(obj);
1309 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
1315 vaddr = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
1316 if (IS_ERR(vaddr)) {
1317 err = PTR_ERR(vaddr);
1321 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
1325 err = i915_vma_sync(vma);
1329 for_each_engine(engine, gt, id) {
1330 struct i915_sched_attr attr = { .priority = I915_PRIORITY_MAX };
1331 struct i915_request *rq, *nop;
1333 if (!intel_engine_has_preemption(engine))
1336 st_engine_heartbeat_disable(engine);
1337 memset(vaddr, 0, PAGE_SIZE);
1339 /* ELSP[0]: semaphore wait */
1340 rq = semaphore_queue(engine, vma, 0);
1345 engine->sched_engine->schedule(rq, &attr);
1346 err = wait_for_submit(engine, rq, HZ / 2);
1348 pr_err("%s: Timed out trying to submit semaphores\n",
1353 /* ELSP[1]: nop request */
1354 nop = nop_request(engine);
1359 err = wait_for_submit(engine, nop, HZ / 2);
1360 i915_request_put(nop);
1362 pr_err("%s: Timed out trying to submit nop\n",
1367 GEM_BUG_ON(i915_request_completed(rq));
1368 GEM_BUG_ON(execlists_active(&engine->execlists) != rq);
1370 /* Queue: semaphore signal, matching priority as semaphore */
1371 err = release_queue(engine, vma, 1, effective_prio(rq));
1375 /* Wait until we ack the release_queue and start timeslicing */
1378 intel_engine_flush_submission(engine);
1379 } while (READ_ONCE(engine->execlists.pending[0]));
1381 /* Timeslice every jiffy, so within 2 we should signal */
1382 if (i915_request_wait(rq, 0, slice_timeout(engine)) < 0) {
1383 struct drm_printer p =
1384 drm_info_printer(gt->i915->drm.dev);
1386 pr_err("%s: Failed to timeslice into queue\n",
1388 intel_engine_dump(engine, &p,
1389 "%s\n", engine->name);
1391 memset(vaddr, 0xff, PAGE_SIZE);
1395 i915_request_put(rq);
1397 st_engine_heartbeat_enable(engine);
1403 i915_vma_unpin(vma);
1405 i915_gem_object_unpin_map(obj);
1407 i915_gem_object_put(obj);
1411 static int live_timeslice_nopreempt(void *arg)
1413 struct intel_gt *gt = arg;
1414 struct intel_engine_cs *engine;
1415 enum intel_engine_id id;
1416 struct igt_spinner spin;
1420 * We should not timeslice into a request that is marked with
1421 * I915_REQUEST_NOPREEMPT.
1423 if (!IS_ACTIVE(CONFIG_DRM_I915_TIMESLICE_DURATION))
1426 if (igt_spinner_init(&spin, gt))
1429 for_each_engine(engine, gt, id) {
1430 struct intel_context *ce;
1431 struct i915_request *rq;
1432 unsigned long timeslice;
1434 if (!intel_engine_has_preemption(engine))
1437 ce = intel_context_create(engine);
1443 st_engine_heartbeat_disable(engine);
1444 timeslice = xchg(&engine->props.timeslice_duration_ms, 1);
1446 /* Create an unpreemptible spinner */
1448 rq = igt_spinner_create_request(&spin, ce, MI_ARB_CHECK);
1449 intel_context_put(ce);
1455 i915_request_get(rq);
1456 i915_request_add(rq);
1458 if (!igt_wait_for_spinner(&spin, rq)) {
1459 i915_request_put(rq);
1464 set_bit(I915_FENCE_FLAG_NOPREEMPT, &rq->fence.flags);
1465 i915_request_put(rq);
1467 /* Followed by a maximum priority barrier (heartbeat) */
1469 ce = intel_context_create(engine);
1475 rq = intel_context_create_request(ce);
1476 intel_context_put(ce);
1482 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
1483 i915_request_get(rq);
1484 i915_request_add(rq);
1487 * Wait until the barrier is in ELSP, and we know timeslicing
1488 * will have been activated.
1490 if (wait_for_submit(engine, rq, HZ / 2)) {
1491 i915_request_put(rq);
1497 * Since the ELSP[0] request is unpreemptible, it should not
1498 * allow the maximum priority barrier through. Wait long
1499 * enough to see if it is timesliced in by mistake.
1501 if (i915_request_wait(rq, 0, slice_timeout(engine)) >= 0) {
1502 pr_err("%s: I915_PRIORITY_BARRIER request completed, bypassing no-preempt request\n",
1506 i915_request_put(rq);
1509 igt_spinner_end(&spin);
1511 xchg(&engine->props.timeslice_duration_ms, timeslice);
1512 st_engine_heartbeat_enable(engine);
1516 if (igt_flush_test(gt->i915)) {
1522 igt_spinner_fini(&spin);
1526 static int live_busywait_preempt(void *arg)
1528 struct intel_gt *gt = arg;
1529 struct i915_gem_context *ctx_hi, *ctx_lo;
1530 struct intel_engine_cs *engine;
1531 struct drm_i915_gem_object *obj;
1532 struct i915_vma *vma;
1533 enum intel_engine_id id;
1538 * Verify that even without HAS_LOGICAL_RING_PREEMPTION, we can
1539 * preempt the busywaits used to synchronise between rings.
1542 ctx_hi = kernel_context(gt->i915);
1545 ctx_hi->sched.priority = I915_CONTEXT_MAX_USER_PRIORITY;
1547 ctx_lo = kernel_context(gt->i915);
1550 ctx_lo->sched.priority = I915_CONTEXT_MIN_USER_PRIORITY;
1552 obj = i915_gem_object_create_internal(gt->i915, PAGE_SIZE);
1558 map = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
1564 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
1570 err = i915_vma_pin(vma, 0, 0, PIN_GLOBAL);
1574 err = i915_vma_sync(vma);
1578 for_each_engine(engine, gt, id) {
1579 struct i915_request *lo, *hi;
1580 struct igt_live_test t;
1583 if (!intel_engine_has_preemption(engine))
1586 if (!intel_engine_can_store_dword(engine))
1589 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
1595 * We create two requests. The low priority request
1596 * busywaits on a semaphore (inside the ringbuffer where
1597 * is should be preemptible) and the high priority requests
1598 * uses a MI_STORE_DWORD_IMM to update the semaphore value
1599 * allowing the first request to complete. If preemption
1600 * fails, we hang instead.
1603 lo = igt_request_alloc(ctx_lo, engine);
1609 cs = intel_ring_begin(lo, 8);
1612 i915_request_add(lo);
1616 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1617 *cs++ = i915_ggtt_offset(vma);
1621 /* XXX Do we need a flush + invalidate here? */
1623 *cs++ = MI_SEMAPHORE_WAIT |
1624 MI_SEMAPHORE_GLOBAL_GTT |
1626 MI_SEMAPHORE_SAD_EQ_SDD;
1628 *cs++ = i915_ggtt_offset(vma);
1631 intel_ring_advance(lo, cs);
1633 i915_request_get(lo);
1634 i915_request_add(lo);
1636 if (wait_for(READ_ONCE(*map), 10)) {
1637 i915_request_put(lo);
1642 /* Low priority request should be busywaiting now */
1643 if (i915_request_wait(lo, 0, 1) != -ETIME) {
1644 i915_request_put(lo);
1645 pr_err("%s: Busywaiting request did not!\n",
1651 hi = igt_request_alloc(ctx_hi, engine);
1654 i915_request_put(lo);
1658 cs = intel_ring_begin(hi, 4);
1661 i915_request_add(hi);
1662 i915_request_put(lo);
1666 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
1667 *cs++ = i915_ggtt_offset(vma);
1671 intel_ring_advance(hi, cs);
1672 i915_request_add(hi);
1674 if (i915_request_wait(lo, 0, HZ / 5) < 0) {
1675 struct drm_printer p = drm_info_printer(gt->i915->drm.dev);
1677 pr_err("%s: Failed to preempt semaphore busywait!\n",
1680 intel_engine_dump(engine, &p, "%s\n", engine->name);
1683 i915_request_put(lo);
1684 intel_gt_set_wedged(gt);
1688 GEM_BUG_ON(READ_ONCE(*map));
1689 i915_request_put(lo);
1691 if (igt_live_test_end(&t)) {
1699 i915_vma_unpin(vma);
1701 i915_gem_object_unpin_map(obj);
1703 i915_gem_object_put(obj);
1705 kernel_context_close(ctx_lo);
1707 kernel_context_close(ctx_hi);
1711 static struct i915_request *
1712 spinner_create_request(struct igt_spinner *spin,
1713 struct i915_gem_context *ctx,
1714 struct intel_engine_cs *engine,
1717 struct intel_context *ce;
1718 struct i915_request *rq;
1720 ce = i915_gem_context_get_engine(ctx, engine->legacy_idx);
1722 return ERR_CAST(ce);
1724 rq = igt_spinner_create_request(spin, ce, arb);
1725 intel_context_put(ce);
1729 static int live_preempt(void *arg)
1731 struct intel_gt *gt = arg;
1732 struct i915_gem_context *ctx_hi, *ctx_lo;
1733 struct igt_spinner spin_hi, spin_lo;
1734 struct intel_engine_cs *engine;
1735 enum intel_engine_id id;
1738 if (igt_spinner_init(&spin_hi, gt))
1741 if (igt_spinner_init(&spin_lo, gt))
1744 ctx_hi = kernel_context(gt->i915);
1747 ctx_hi->sched.priority = I915_CONTEXT_MAX_USER_PRIORITY;
1749 ctx_lo = kernel_context(gt->i915);
1752 ctx_lo->sched.priority = I915_CONTEXT_MIN_USER_PRIORITY;
1754 for_each_engine(engine, gt, id) {
1755 struct igt_live_test t;
1756 struct i915_request *rq;
1758 if (!intel_engine_has_preemption(engine))
1761 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
1766 rq = spinner_create_request(&spin_lo, ctx_lo, engine,
1773 i915_request_add(rq);
1774 if (!igt_wait_for_spinner(&spin_lo, rq)) {
1775 GEM_TRACE("lo spinner failed to start\n");
1777 intel_gt_set_wedged(gt);
1782 rq = spinner_create_request(&spin_hi, ctx_hi, engine,
1785 igt_spinner_end(&spin_lo);
1790 i915_request_add(rq);
1791 if (!igt_wait_for_spinner(&spin_hi, rq)) {
1792 GEM_TRACE("hi spinner failed to start\n");
1794 intel_gt_set_wedged(gt);
1799 igt_spinner_end(&spin_hi);
1800 igt_spinner_end(&spin_lo);
1802 if (igt_live_test_end(&t)) {
1810 kernel_context_close(ctx_lo);
1812 kernel_context_close(ctx_hi);
1814 igt_spinner_fini(&spin_lo);
1816 igt_spinner_fini(&spin_hi);
1820 static int live_late_preempt(void *arg)
1822 struct intel_gt *gt = arg;
1823 struct i915_gem_context *ctx_hi, *ctx_lo;
1824 struct igt_spinner spin_hi, spin_lo;
1825 struct intel_engine_cs *engine;
1826 struct i915_sched_attr attr = {};
1827 enum intel_engine_id id;
1830 if (igt_spinner_init(&spin_hi, gt))
1833 if (igt_spinner_init(&spin_lo, gt))
1836 ctx_hi = kernel_context(gt->i915);
1840 ctx_lo = kernel_context(gt->i915);
1844 /* Make sure ctx_lo stays before ctx_hi until we trigger preemption. */
1845 ctx_lo->sched.priority = 1;
1847 for_each_engine(engine, gt, id) {
1848 struct igt_live_test t;
1849 struct i915_request *rq;
1851 if (!intel_engine_has_preemption(engine))
1854 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
1859 rq = spinner_create_request(&spin_lo, ctx_lo, engine,
1866 i915_request_add(rq);
1867 if (!igt_wait_for_spinner(&spin_lo, rq)) {
1868 pr_err("First context failed to start\n");
1872 rq = spinner_create_request(&spin_hi, ctx_hi, engine,
1875 igt_spinner_end(&spin_lo);
1880 i915_request_add(rq);
1881 if (igt_wait_for_spinner(&spin_hi, rq)) {
1882 pr_err("Second context overtook first?\n");
1886 attr.priority = I915_PRIORITY_MAX;
1887 engine->sched_engine->schedule(rq, &attr);
1889 if (!igt_wait_for_spinner(&spin_hi, rq)) {
1890 pr_err("High priority context failed to preempt the low priority context\n");
1895 igt_spinner_end(&spin_hi);
1896 igt_spinner_end(&spin_lo);
1898 if (igt_live_test_end(&t)) {
1906 kernel_context_close(ctx_lo);
1908 kernel_context_close(ctx_hi);
1910 igt_spinner_fini(&spin_lo);
1912 igt_spinner_fini(&spin_hi);
1916 igt_spinner_end(&spin_hi);
1917 igt_spinner_end(&spin_lo);
1918 intel_gt_set_wedged(gt);
1923 struct preempt_client {
1924 struct igt_spinner spin;
1925 struct i915_gem_context *ctx;
1928 static int preempt_client_init(struct intel_gt *gt, struct preempt_client *c)
1930 c->ctx = kernel_context(gt->i915);
1934 if (igt_spinner_init(&c->spin, gt))
1940 kernel_context_close(c->ctx);
1944 static void preempt_client_fini(struct preempt_client *c)
1946 igt_spinner_fini(&c->spin);
1947 kernel_context_close(c->ctx);
1950 static int live_nopreempt(void *arg)
1952 struct intel_gt *gt = arg;
1953 struct intel_engine_cs *engine;
1954 struct preempt_client a, b;
1955 enum intel_engine_id id;
1959 * Verify that we can disable preemption for an individual request
1960 * that may be being observed and not want to be interrupted.
1963 if (preempt_client_init(gt, &a))
1965 if (preempt_client_init(gt, &b))
1967 b.ctx->sched.priority = I915_PRIORITY_MAX;
1969 for_each_engine(engine, gt, id) {
1970 struct i915_request *rq_a, *rq_b;
1972 if (!intel_engine_has_preemption(engine))
1975 engine->execlists.preempt_hang.count = 0;
1977 rq_a = spinner_create_request(&a.spin,
1981 err = PTR_ERR(rq_a);
1985 /* Low priority client, but unpreemptable! */
1986 __set_bit(I915_FENCE_FLAG_NOPREEMPT, &rq_a->fence.flags);
1988 i915_request_add(rq_a);
1989 if (!igt_wait_for_spinner(&a.spin, rq_a)) {
1990 pr_err("First client failed to start\n");
1994 rq_b = spinner_create_request(&b.spin,
1998 err = PTR_ERR(rq_b);
2002 i915_request_add(rq_b);
2004 /* B is much more important than A! (But A is unpreemptable.) */
2005 GEM_BUG_ON(rq_prio(rq_b) <= rq_prio(rq_a));
2007 /* Wait long enough for preemption and timeslicing */
2008 if (igt_wait_for_spinner(&b.spin, rq_b)) {
2009 pr_err("Second client started too early!\n");
2013 igt_spinner_end(&a.spin);
2015 if (!igt_wait_for_spinner(&b.spin, rq_b)) {
2016 pr_err("Second client failed to start\n");
2020 igt_spinner_end(&b.spin);
2022 if (engine->execlists.preempt_hang.count) {
2023 pr_err("Preemption recorded x%d; should have been suppressed!\n",
2024 engine->execlists.preempt_hang.count);
2029 if (igt_flush_test(gt->i915))
2035 preempt_client_fini(&b);
2037 preempt_client_fini(&a);
2041 igt_spinner_end(&b.spin);
2042 igt_spinner_end(&a.spin);
2043 intel_gt_set_wedged(gt);
2048 struct live_preempt_cancel {
2049 struct intel_engine_cs *engine;
2050 struct preempt_client a, b;
2053 static int __cancel_active0(struct live_preempt_cancel *arg)
2055 struct i915_request *rq;
2056 struct igt_live_test t;
2059 /* Preempt cancel of ELSP0 */
2060 GEM_TRACE("%s(%s)\n", __func__, arg->engine->name);
2061 if (igt_live_test_begin(&t, arg->engine->i915,
2062 __func__, arg->engine->name))
2065 rq = spinner_create_request(&arg->a.spin,
2066 arg->a.ctx, arg->engine,
2071 clear_bit(CONTEXT_BANNED, &rq->context->flags);
2072 i915_request_get(rq);
2073 i915_request_add(rq);
2074 if (!igt_wait_for_spinner(&arg->a.spin, rq)) {
2079 intel_context_set_banned(rq->context);
2080 err = intel_engine_pulse(arg->engine);
2084 err = wait_for_reset(arg->engine, rq, HZ / 2);
2086 pr_err("Cancelled inflight0 request did not reset\n");
2091 i915_request_put(rq);
2092 if (igt_live_test_end(&t))
2097 static int __cancel_active1(struct live_preempt_cancel *arg)
2099 struct i915_request *rq[2] = {};
2100 struct igt_live_test t;
2103 /* Preempt cancel of ELSP1 */
2104 GEM_TRACE("%s(%s)\n", __func__, arg->engine->name);
2105 if (igt_live_test_begin(&t, arg->engine->i915,
2106 __func__, arg->engine->name))
2109 rq[0] = spinner_create_request(&arg->a.spin,
2110 arg->a.ctx, arg->engine,
2111 MI_NOOP); /* no preemption */
2113 return PTR_ERR(rq[0]);
2115 clear_bit(CONTEXT_BANNED, &rq[0]->context->flags);
2116 i915_request_get(rq[0]);
2117 i915_request_add(rq[0]);
2118 if (!igt_wait_for_spinner(&arg->a.spin, rq[0])) {
2123 rq[1] = spinner_create_request(&arg->b.spin,
2124 arg->b.ctx, arg->engine,
2126 if (IS_ERR(rq[1])) {
2127 err = PTR_ERR(rq[1]);
2131 clear_bit(CONTEXT_BANNED, &rq[1]->context->flags);
2132 i915_request_get(rq[1]);
2133 err = i915_request_await_dma_fence(rq[1], &rq[0]->fence);
2134 i915_request_add(rq[1]);
2138 intel_context_set_banned(rq[1]->context);
2139 err = intel_engine_pulse(arg->engine);
2143 igt_spinner_end(&arg->a.spin);
2144 err = wait_for_reset(arg->engine, rq[1], HZ / 2);
2148 if (rq[0]->fence.error != 0) {
2149 pr_err("Normal inflight0 request did not complete\n");
2154 if (rq[1]->fence.error != -EIO) {
2155 pr_err("Cancelled inflight1 request did not report -EIO\n");
2161 i915_request_put(rq[1]);
2162 i915_request_put(rq[0]);
2163 if (igt_live_test_end(&t))
2168 static int __cancel_queued(struct live_preempt_cancel *arg)
2170 struct i915_request *rq[3] = {};
2171 struct igt_live_test t;
2174 /* Full ELSP and one in the wings */
2175 GEM_TRACE("%s(%s)\n", __func__, arg->engine->name);
2176 if (igt_live_test_begin(&t, arg->engine->i915,
2177 __func__, arg->engine->name))
2180 rq[0] = spinner_create_request(&arg->a.spin,
2181 arg->a.ctx, arg->engine,
2184 return PTR_ERR(rq[0]);
2186 clear_bit(CONTEXT_BANNED, &rq[0]->context->flags);
2187 i915_request_get(rq[0]);
2188 i915_request_add(rq[0]);
2189 if (!igt_wait_for_spinner(&arg->a.spin, rq[0])) {
2194 rq[1] = igt_request_alloc(arg->b.ctx, arg->engine);
2195 if (IS_ERR(rq[1])) {
2196 err = PTR_ERR(rq[1]);
2200 clear_bit(CONTEXT_BANNED, &rq[1]->context->flags);
2201 i915_request_get(rq[1]);
2202 err = i915_request_await_dma_fence(rq[1], &rq[0]->fence);
2203 i915_request_add(rq[1]);
2207 rq[2] = spinner_create_request(&arg->b.spin,
2208 arg->a.ctx, arg->engine,
2210 if (IS_ERR(rq[2])) {
2211 err = PTR_ERR(rq[2]);
2215 i915_request_get(rq[2]);
2216 err = i915_request_await_dma_fence(rq[2], &rq[1]->fence);
2217 i915_request_add(rq[2]);
2221 intel_context_set_banned(rq[2]->context);
2222 err = intel_engine_pulse(arg->engine);
2226 err = wait_for_reset(arg->engine, rq[2], HZ / 2);
2230 if (rq[0]->fence.error != -EIO) {
2231 pr_err("Cancelled inflight0 request did not report -EIO\n");
2236 if (rq[1]->fence.error != 0) {
2237 pr_err("Normal inflight1 request did not complete\n");
2242 if (rq[2]->fence.error != -EIO) {
2243 pr_err("Cancelled queued request did not report -EIO\n");
2249 i915_request_put(rq[2]);
2250 i915_request_put(rq[1]);
2251 i915_request_put(rq[0]);
2252 if (igt_live_test_end(&t))
2257 static int __cancel_hostile(struct live_preempt_cancel *arg)
2259 struct i915_request *rq;
2262 /* Preempt cancel non-preemptible spinner in ELSP0 */
2263 if (!IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT))
2266 if (!intel_has_reset_engine(arg->engine->gt))
2269 GEM_TRACE("%s(%s)\n", __func__, arg->engine->name);
2270 rq = spinner_create_request(&arg->a.spin,
2271 arg->a.ctx, arg->engine,
2272 MI_NOOP); /* preemption disabled */
2276 clear_bit(CONTEXT_BANNED, &rq->context->flags);
2277 i915_request_get(rq);
2278 i915_request_add(rq);
2279 if (!igt_wait_for_spinner(&arg->a.spin, rq)) {
2284 intel_context_set_banned(rq->context);
2285 err = intel_engine_pulse(arg->engine); /* force reset */
2289 err = wait_for_reset(arg->engine, rq, HZ / 2);
2291 pr_err("Cancelled inflight0 request did not reset\n");
2296 i915_request_put(rq);
2297 if (igt_flush_test(arg->engine->i915))
2302 static void force_reset_timeout(struct intel_engine_cs *engine)
2304 engine->reset_timeout.probability = 999;
2305 atomic_set(&engine->reset_timeout.times, -1);
2308 static void cancel_reset_timeout(struct intel_engine_cs *engine)
2310 memset(&engine->reset_timeout, 0, sizeof(engine->reset_timeout));
2313 static int __cancel_fail(struct live_preempt_cancel *arg)
2315 struct intel_engine_cs *engine = arg->engine;
2316 struct i915_request *rq;
2319 if (!IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT))
2322 if (!intel_has_reset_engine(engine->gt))
2325 GEM_TRACE("%s(%s)\n", __func__, engine->name);
2326 rq = spinner_create_request(&arg->a.spin,
2328 MI_NOOP); /* preemption disabled */
2332 clear_bit(CONTEXT_BANNED, &rq->context->flags);
2333 i915_request_get(rq);
2334 i915_request_add(rq);
2335 if (!igt_wait_for_spinner(&arg->a.spin, rq)) {
2340 intel_context_set_banned(rq->context);
2342 err = intel_engine_pulse(engine);
2346 force_reset_timeout(engine);
2348 /* force preempt reset [failure] */
2349 while (!engine->execlists.pending[0])
2350 intel_engine_flush_submission(engine);
2351 del_timer_sync(&engine->execlists.preempt);
2352 intel_engine_flush_submission(engine);
2354 cancel_reset_timeout(engine);
2356 /* after failure, require heartbeats to reset device */
2357 intel_engine_set_heartbeat(engine, 1);
2358 err = wait_for_reset(engine, rq, HZ / 2);
2359 intel_engine_set_heartbeat(engine,
2360 engine->defaults.heartbeat_interval_ms);
2362 pr_err("Cancelled inflight0 request did not reset\n");
2367 i915_request_put(rq);
2368 if (igt_flush_test(engine->i915))
2373 static int live_preempt_cancel(void *arg)
2375 struct intel_gt *gt = arg;
2376 struct live_preempt_cancel data;
2377 enum intel_engine_id id;
2381 * To cancel an inflight context, we need to first remove it from the
2382 * GPU. That sounds like preemption! Plus a little bit of bookkeeping.
2385 if (preempt_client_init(gt, &data.a))
2387 if (preempt_client_init(gt, &data.b))
2390 for_each_engine(data.engine, gt, id) {
2391 if (!intel_engine_has_preemption(data.engine))
2394 err = __cancel_active0(&data);
2398 err = __cancel_active1(&data);
2402 err = __cancel_queued(&data);
2406 err = __cancel_hostile(&data);
2410 err = __cancel_fail(&data);
2417 preempt_client_fini(&data.b);
2419 preempt_client_fini(&data.a);
2424 igt_spinner_end(&data.b.spin);
2425 igt_spinner_end(&data.a.spin);
2426 intel_gt_set_wedged(gt);
2430 static int live_suppress_self_preempt(void *arg)
2432 struct i915_sched_attr attr = { .priority = I915_PRIORITY_MAX };
2433 struct intel_gt *gt = arg;
2434 struct intel_engine_cs *engine;
2435 struct preempt_client a, b;
2436 enum intel_engine_id id;
2440 * Verify that if a preemption request does not cause a change in
2441 * the current execution order, the preempt-to-idle injection is
2442 * skipped and that we do not accidentally apply it after the CS
2446 if (intel_uc_uses_guc_submission(>->uc))
2447 return 0; /* presume black blox */
2449 if (intel_vgpu_active(gt->i915))
2450 return 0; /* GVT forces single port & request submission */
2452 if (preempt_client_init(gt, &a))
2454 if (preempt_client_init(gt, &b))
2457 for_each_engine(engine, gt, id) {
2458 struct i915_request *rq_a, *rq_b;
2461 if (!intel_engine_has_preemption(engine))
2464 if (igt_flush_test(gt->i915))
2467 st_engine_heartbeat_disable(engine);
2468 engine->execlists.preempt_hang.count = 0;
2470 rq_a = spinner_create_request(&a.spin,
2474 err = PTR_ERR(rq_a);
2475 st_engine_heartbeat_enable(engine);
2479 i915_request_add(rq_a);
2480 if (!igt_wait_for_spinner(&a.spin, rq_a)) {
2481 pr_err("First client failed to start\n");
2482 st_engine_heartbeat_enable(engine);
2486 /* Keep postponing the timer to avoid premature slicing */
2487 mod_timer(&engine->execlists.timer, jiffies + HZ);
2488 for (depth = 0; depth < 8; depth++) {
2489 rq_b = spinner_create_request(&b.spin,
2493 err = PTR_ERR(rq_b);
2494 st_engine_heartbeat_enable(engine);
2497 i915_request_add(rq_b);
2499 GEM_BUG_ON(i915_request_completed(rq_a));
2500 engine->sched_engine->schedule(rq_a, &attr);
2501 igt_spinner_end(&a.spin);
2503 if (!igt_wait_for_spinner(&b.spin, rq_b)) {
2504 pr_err("Second client failed to start\n");
2505 st_engine_heartbeat_enable(engine);
2512 igt_spinner_end(&a.spin);
2514 if (engine->execlists.preempt_hang.count) {
2515 pr_err("Preemption on %s recorded x%d, depth %d; should have been suppressed!\n",
2517 engine->execlists.preempt_hang.count,
2519 st_engine_heartbeat_enable(engine);
2524 st_engine_heartbeat_enable(engine);
2525 if (igt_flush_test(gt->i915))
2531 preempt_client_fini(&b);
2533 preempt_client_fini(&a);
2537 igt_spinner_end(&b.spin);
2538 igt_spinner_end(&a.spin);
2539 intel_gt_set_wedged(gt);
2544 static int live_chain_preempt(void *arg)
2546 struct intel_gt *gt = arg;
2547 struct intel_engine_cs *engine;
2548 struct preempt_client hi, lo;
2549 enum intel_engine_id id;
2553 * Build a chain AB...BA between two contexts (A, B) and request
2554 * preemption of the last request. It should then complete before
2555 * the previously submitted spinner in B.
2558 if (preempt_client_init(gt, &hi))
2561 if (preempt_client_init(gt, &lo))
2564 for_each_engine(engine, gt, id) {
2565 struct i915_sched_attr attr = { .priority = I915_PRIORITY_MAX };
2566 struct igt_live_test t;
2567 struct i915_request *rq;
2568 int ring_size, count, i;
2570 if (!intel_engine_has_preemption(engine))
2573 rq = spinner_create_request(&lo.spin,
2579 i915_request_get(rq);
2580 i915_request_add(rq);
2582 ring_size = rq->wa_tail - rq->head;
2584 ring_size += rq->ring->size;
2585 ring_size = rq->ring->size / ring_size;
2586 pr_debug("%s(%s): Using maximum of %d requests\n",
2587 __func__, engine->name, ring_size);
2589 igt_spinner_end(&lo.spin);
2590 if (i915_request_wait(rq, 0, HZ / 2) < 0) {
2591 pr_err("Timed out waiting to flush %s\n", engine->name);
2592 i915_request_put(rq);
2595 i915_request_put(rq);
2597 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
2602 for_each_prime_number_from(count, 1, ring_size) {
2603 rq = spinner_create_request(&hi.spin,
2608 i915_request_add(rq);
2609 if (!igt_wait_for_spinner(&hi.spin, rq))
2612 rq = spinner_create_request(&lo.spin,
2617 i915_request_add(rq);
2619 for (i = 0; i < count; i++) {
2620 rq = igt_request_alloc(lo.ctx, engine);
2623 i915_request_add(rq);
2626 rq = igt_request_alloc(hi.ctx, engine);
2630 i915_request_get(rq);
2631 i915_request_add(rq);
2632 engine->sched_engine->schedule(rq, &attr);
2634 igt_spinner_end(&hi.spin);
2635 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
2636 struct drm_printer p =
2637 drm_info_printer(gt->i915->drm.dev);
2639 pr_err("Failed to preempt over chain of %d\n",
2641 intel_engine_dump(engine, &p,
2642 "%s\n", engine->name);
2643 i915_request_put(rq);
2646 igt_spinner_end(&lo.spin);
2647 i915_request_put(rq);
2649 rq = igt_request_alloc(lo.ctx, engine);
2653 i915_request_get(rq);
2654 i915_request_add(rq);
2656 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
2657 struct drm_printer p =
2658 drm_info_printer(gt->i915->drm.dev);
2660 pr_err("Failed to flush low priority chain of %d requests\n",
2662 intel_engine_dump(engine, &p,
2663 "%s\n", engine->name);
2665 i915_request_put(rq);
2668 i915_request_put(rq);
2671 if (igt_live_test_end(&t)) {
2679 preempt_client_fini(&lo);
2681 preempt_client_fini(&hi);
2685 igt_spinner_end(&hi.spin);
2686 igt_spinner_end(&lo.spin);
2687 intel_gt_set_wedged(gt);
2692 static int create_gang(struct intel_engine_cs *engine,
2693 struct i915_request **prev)
2695 struct drm_i915_gem_object *obj;
2696 struct intel_context *ce;
2697 struct i915_request *rq;
2698 struct i915_vma *vma;
2702 ce = intel_context_create(engine);
2706 obj = i915_gem_object_create_internal(engine->i915, 4096);
2712 vma = i915_vma_instance(obj, ce->vm, NULL);
2718 err = i915_vma_pin(vma, 0, 0, PIN_USER);
2722 cs = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
2728 /* Semaphore target: spin until zero */
2729 *cs++ = MI_ARB_ON_OFF | MI_ARB_ENABLE;
2731 *cs++ = MI_SEMAPHORE_WAIT |
2733 MI_SEMAPHORE_SAD_EQ_SDD;
2735 *cs++ = lower_32_bits(vma->node.start);
2736 *cs++ = upper_32_bits(vma->node.start);
2739 u64 offset = (*prev)->batch->node.start;
2741 /* Terminate the spinner in the next lower priority batch. */
2742 *cs++ = MI_STORE_DWORD_IMM_GEN4;
2743 *cs++ = lower_32_bits(offset);
2744 *cs++ = upper_32_bits(offset);
2748 *cs++ = MI_BATCH_BUFFER_END;
2749 i915_gem_object_flush_map(obj);
2750 i915_gem_object_unpin_map(obj);
2752 rq = intel_context_create_request(ce);
2758 rq->batch = i915_vma_get(vma);
2759 i915_request_get(rq);
2762 err = i915_request_await_object(rq, vma->obj, false);
2764 err = i915_vma_move_to_active(vma, rq, 0);
2766 err = rq->engine->emit_bb_start(rq,
2769 i915_vma_unlock(vma);
2770 i915_request_add(rq);
2774 i915_gem_object_put(obj);
2775 intel_context_put(ce);
2777 rq->mock.link.next = &(*prev)->mock.link;
2782 i915_vma_put(rq->batch);
2783 i915_request_put(rq);
2785 i915_gem_object_put(obj);
2787 intel_context_put(ce);
2791 static int __live_preempt_ring(struct intel_engine_cs *engine,
2792 struct igt_spinner *spin,
2793 int queue_sz, int ring_sz)
2795 struct intel_context *ce[2] = {};
2796 struct i915_request *rq;
2797 struct igt_live_test t;
2801 if (igt_live_test_begin(&t, engine->i915, __func__, engine->name))
2804 for (n = 0; n < ARRAY_SIZE(ce); n++) {
2805 struct intel_context *tmp;
2807 tmp = intel_context_create(engine);
2813 tmp->ring_size = ring_sz;
2815 err = intel_context_pin(tmp);
2817 intel_context_put(tmp);
2821 memset32(tmp->ring->vaddr,
2822 0xdeadbeef, /* trigger a hang if executed */
2823 tmp->ring->vma->size / sizeof(u32));
2828 rq = igt_spinner_create_request(spin, ce[0], MI_ARB_CHECK);
2834 i915_request_get(rq);
2835 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
2836 i915_request_add(rq);
2838 if (!igt_wait_for_spinner(spin, rq)) {
2839 intel_gt_set_wedged(engine->gt);
2840 i915_request_put(rq);
2845 /* Fill the ring, until we will cause a wrap */
2847 while (ce[0]->ring->tail - rq->wa_tail <= queue_sz) {
2848 struct i915_request *tmp;
2850 tmp = intel_context_create_request(ce[0]);
2853 i915_request_put(rq);
2857 i915_request_add(tmp);
2858 intel_engine_flush_submission(engine);
2861 intel_engine_flush_submission(engine);
2862 pr_debug("%s: Filled %d with %d nop tails {size:%x, tail:%x, emit:%x, rq.tail:%x}\n",
2863 engine->name, queue_sz, n,
2868 i915_request_put(rq);
2870 /* Create a second request to preempt the first ring */
2871 rq = intel_context_create_request(ce[1]);
2877 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
2878 i915_request_get(rq);
2879 i915_request_add(rq);
2881 err = wait_for_submit(engine, rq, HZ / 2);
2882 i915_request_put(rq);
2884 pr_err("%s: preemption request was not submitted\n",
2889 pr_debug("%s: ring[0]:{ tail:%x, emit:%x }, ring[1]:{ tail:%x, emit:%x }\n",
2891 ce[0]->ring->tail, ce[0]->ring->emit,
2892 ce[1]->ring->tail, ce[1]->ring->emit);
2895 intel_engine_flush_submission(engine);
2896 igt_spinner_end(spin);
2897 for (n = 0; n < ARRAY_SIZE(ce); n++) {
2898 if (IS_ERR_OR_NULL(ce[n]))
2901 intel_context_unpin(ce[n]);
2902 intel_context_put(ce[n]);
2904 if (igt_live_test_end(&t))
2909 static int live_preempt_ring(void *arg)
2911 struct intel_gt *gt = arg;
2912 struct intel_engine_cs *engine;
2913 struct igt_spinner spin;
2914 enum intel_engine_id id;
2918 * Check that we rollback large chunks of a ring in order to do a
2919 * preemption event. Similar to live_unlite_ring, but looking at
2920 * ring size rather than the impact of intel_ring_direction().
2923 if (igt_spinner_init(&spin, gt))
2926 for_each_engine(engine, gt, id) {
2929 if (!intel_engine_has_preemption(engine))
2932 if (!intel_engine_can_store_dword(engine))
2935 st_engine_heartbeat_disable(engine);
2937 for (n = 0; n <= 3; n++) {
2938 err = __live_preempt_ring(engine, &spin,
2939 n * SZ_4K / 4, SZ_4K);
2944 st_engine_heartbeat_enable(engine);
2949 igt_spinner_fini(&spin);
2953 static int live_preempt_gang(void *arg)
2955 struct intel_gt *gt = arg;
2956 struct intel_engine_cs *engine;
2957 enum intel_engine_id id;
2960 * Build as long a chain of preempters as we can, with each
2961 * request higher priority than the last. Once we are ready, we release
2962 * the last batch which then precolates down the chain, each releasing
2963 * the next oldest in turn. The intent is to simply push as hard as we
2964 * can with the number of preemptions, trying to exceed narrow HW
2965 * limits. At a minimum, we insist that we can sort all the user
2966 * high priority levels into execution order.
2969 for_each_engine(engine, gt, id) {
2970 struct i915_request *rq = NULL;
2971 struct igt_live_test t;
2972 IGT_TIMEOUT(end_time);
2977 if (!intel_engine_has_preemption(engine))
2980 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name))
2984 struct i915_sched_attr attr = { .priority = prio++ };
2986 err = create_gang(engine, &rq);
2990 /* Submit each spinner at increasing priority */
2991 engine->sched_engine->schedule(rq, &attr);
2992 } while (prio <= I915_PRIORITY_MAX &&
2993 !__igt_timeout(end_time, NULL));
2994 pr_debug("%s: Preempt chain of %d requests\n",
2995 engine->name, prio);
2998 * Such that the last spinner is the highest priority and
2999 * should execute first. When that spinner completes,
3000 * it will terminate the next lowest spinner until there
3001 * are no more spinners and the gang is complete.
3003 cs = i915_gem_object_pin_map_unlocked(rq->batch->obj, I915_MAP_WC);
3006 i915_gem_object_unpin_map(rq->batch->obj);
3009 intel_gt_set_wedged(gt);
3012 while (rq) { /* wait for each rq from highest to lowest prio */
3013 struct i915_request *n = list_next_entry(rq, mock.link);
3015 if (err == 0 && i915_request_wait(rq, 0, HZ / 5) < 0) {
3016 struct drm_printer p =
3017 drm_info_printer(engine->i915->drm.dev);
3019 pr_err("Failed to flush chain of %d requests, at %d\n",
3021 intel_engine_dump(engine, &p,
3022 "%s\n", engine->name);
3027 i915_vma_put(rq->batch);
3028 i915_request_put(rq);
3032 if (igt_live_test_end(&t))
3041 static struct i915_vma *
3042 create_gpr_user(struct intel_engine_cs *engine,
3043 struct i915_vma *result,
3044 unsigned int offset)
3046 struct drm_i915_gem_object *obj;
3047 struct i915_vma *vma;
3052 obj = i915_gem_object_create_internal(engine->i915, 4096);
3054 return ERR_CAST(obj);
3056 vma = i915_vma_instance(obj, result->vm, NULL);
3058 i915_gem_object_put(obj);
3062 err = i915_vma_pin(vma, 0, 0, PIN_USER);
3065 return ERR_PTR(err);
3068 cs = i915_gem_object_pin_map_unlocked(obj, I915_MAP_WC);
3071 return ERR_CAST(cs);
3074 /* All GPR are clear for new contexts. We use GPR(0) as a constant */
3075 *cs++ = MI_LOAD_REGISTER_IMM(1);
3076 *cs++ = CS_GPR(engine, 0);
3079 for (i = 1; i < NUM_GPR; i++) {
3085 * As we read and write into the context saved GPR[i], if
3086 * we restart this batch buffer from an earlier point, we
3087 * will repeat the increment and store a value > 1.
3090 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCA, MI_MATH_REG(i));
3091 *cs++ = MI_MATH_LOAD(MI_MATH_REG_SRCB, MI_MATH_REG(0));
3092 *cs++ = MI_MATH_ADD;
3093 *cs++ = MI_MATH_STORE(MI_MATH_REG(i), MI_MATH_REG_ACCU);
3095 addr = result->node.start + offset + i * sizeof(*cs);
3096 *cs++ = MI_STORE_REGISTER_MEM_GEN8;
3097 *cs++ = CS_GPR(engine, 2 * i);
3098 *cs++ = lower_32_bits(addr);
3099 *cs++ = upper_32_bits(addr);
3101 *cs++ = MI_SEMAPHORE_WAIT |
3103 MI_SEMAPHORE_SAD_GTE_SDD;
3105 *cs++ = lower_32_bits(result->node.start);
3106 *cs++ = upper_32_bits(result->node.start);
3109 *cs++ = MI_BATCH_BUFFER_END;
3110 i915_gem_object_flush_map(obj);
3111 i915_gem_object_unpin_map(obj);
3116 static struct i915_vma *create_global(struct intel_gt *gt, size_t sz)
3118 struct drm_i915_gem_object *obj;
3119 struct i915_vma *vma;
3122 obj = i915_gem_object_create_internal(gt->i915, sz);
3124 return ERR_CAST(obj);
3126 vma = i915_vma_instance(obj, >->ggtt->vm, NULL);
3128 i915_gem_object_put(obj);
3132 err = i915_ggtt_pin(vma, NULL, 0, 0);
3135 return ERR_PTR(err);
3141 static struct i915_request *
3142 create_gpr_client(struct intel_engine_cs *engine,
3143 struct i915_vma *global,
3144 unsigned int offset)
3146 struct i915_vma *batch, *vma;
3147 struct intel_context *ce;
3148 struct i915_request *rq;
3151 ce = intel_context_create(engine);
3153 return ERR_CAST(ce);
3155 vma = i915_vma_instance(global->obj, ce->vm, NULL);
3161 err = i915_vma_pin(vma, 0, 0, PIN_USER);
3165 batch = create_gpr_user(engine, vma, offset);
3166 if (IS_ERR(batch)) {
3167 err = PTR_ERR(batch);
3171 rq = intel_context_create_request(ce);
3178 err = i915_request_await_object(rq, vma->obj, false);
3180 err = i915_vma_move_to_active(vma, rq, 0);
3181 i915_vma_unlock(vma);
3183 i915_vma_lock(batch);
3185 err = i915_request_await_object(rq, batch->obj, false);
3187 err = i915_vma_move_to_active(batch, rq, 0);
3189 err = rq->engine->emit_bb_start(rq,
3192 i915_vma_unlock(batch);
3193 i915_vma_unpin(batch);
3196 i915_request_get(rq);
3197 i915_request_add(rq);
3200 i915_vma_put(batch);
3202 i915_vma_unpin(vma);
3204 intel_context_put(ce);
3205 return err ? ERR_PTR(err) : rq;
3208 static int preempt_user(struct intel_engine_cs *engine,
3209 struct i915_vma *global,
3212 struct i915_sched_attr attr = {
3213 .priority = I915_PRIORITY_MAX
3215 struct i915_request *rq;
3219 rq = intel_engine_create_kernel_request(engine);
3223 cs = intel_ring_begin(rq, 4);
3225 i915_request_add(rq);
3229 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
3230 *cs++ = i915_ggtt_offset(global);
3234 intel_ring_advance(rq, cs);
3236 i915_request_get(rq);
3237 i915_request_add(rq);
3239 engine->sched_engine->schedule(rq, &attr);
3241 if (i915_request_wait(rq, 0, HZ / 2) < 0)
3243 i915_request_put(rq);
3248 static int live_preempt_user(void *arg)
3250 struct intel_gt *gt = arg;
3251 struct intel_engine_cs *engine;
3252 struct i915_vma *global;
3253 enum intel_engine_id id;
3258 * In our other tests, we look at preemption in carefully
3259 * controlled conditions in the ringbuffer. Since most of the
3260 * time is spent in user batches, most of our preemptions naturally
3261 * occur there. We want to verify that when we preempt inside a batch
3262 * we continue on from the current instruction and do not roll back
3263 * to the start, or another earlier arbitration point.
3265 * To verify this, we create a batch which is a mixture of
3266 * MI_MATH (gpr++) MI_SRM (gpr) and preemption points. Then with
3267 * a few preempting contexts thrown into the mix, we look for any
3268 * repeated instructions (which show up as incorrect values).
3271 global = create_global(gt, 4096);
3273 return PTR_ERR(global);
3275 result = i915_gem_object_pin_map_unlocked(global->obj, I915_MAP_WC);
3276 if (IS_ERR(result)) {
3277 i915_vma_unpin_and_release(&global, 0);
3278 return PTR_ERR(result);
3281 for_each_engine(engine, gt, id) {
3282 struct i915_request *client[3] = {};
3283 struct igt_live_test t;
3286 if (!intel_engine_has_preemption(engine))
3289 if (GRAPHICS_VER(gt->i915) == 8 && engine->class != RENDER_CLASS)
3290 continue; /* we need per-context GPR */
3292 if (igt_live_test_begin(&t, gt->i915, __func__, engine->name)) {
3297 memset(result, 0, 4096);
3299 for (i = 0; i < ARRAY_SIZE(client); i++) {
3300 struct i915_request *rq;
3302 rq = create_gpr_client(engine, global,
3303 NUM_GPR * i * sizeof(u32));
3312 /* Continuously preempt the set of 3 running contexts */
3313 for (i = 1; i <= NUM_GPR; i++) {
3314 err = preempt_user(engine, global, i);
3319 if (READ_ONCE(result[0]) != NUM_GPR) {
3320 pr_err("%s: Failed to release semaphore\n",
3326 for (i = 0; i < ARRAY_SIZE(client); i++) {
3329 if (i915_request_wait(client[i], 0, HZ / 2) < 0) {
3334 for (gpr = 1; gpr < NUM_GPR; gpr++) {
3335 if (result[NUM_GPR * i + gpr] != 1) {
3336 pr_err("%s: Invalid result, client %d, gpr %d, result: %d\n",
3338 i, gpr, result[NUM_GPR * i + gpr]);
3346 for (i = 0; i < ARRAY_SIZE(client); i++) {
3350 i915_request_put(client[i]);
3353 /* Flush the semaphores on error */
3354 smp_store_mb(result[0], -1);
3355 if (igt_live_test_end(&t))
3361 i915_vma_unpin_and_release(&global, I915_VMA_RELEASE_MAP);
3365 static int live_preempt_timeout(void *arg)
3367 struct intel_gt *gt = arg;
3368 struct i915_gem_context *ctx_hi, *ctx_lo;
3369 struct igt_spinner spin_lo;
3370 struct intel_engine_cs *engine;
3371 enum intel_engine_id id;
3375 * Check that we force preemption to occur by cancelling the previous
3376 * context if it refuses to yield the GPU.
3378 if (!IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT))
3381 if (!intel_has_reset_engine(gt))
3384 if (igt_spinner_init(&spin_lo, gt))
3387 ctx_hi = kernel_context(gt->i915);
3390 ctx_hi->sched.priority = I915_CONTEXT_MAX_USER_PRIORITY;
3392 ctx_lo = kernel_context(gt->i915);
3395 ctx_lo->sched.priority = I915_CONTEXT_MIN_USER_PRIORITY;
3397 for_each_engine(engine, gt, id) {
3398 unsigned long saved_timeout;
3399 struct i915_request *rq;
3401 if (!intel_engine_has_preemption(engine))
3404 rq = spinner_create_request(&spin_lo, ctx_lo, engine,
3405 MI_NOOP); /* preemption disabled */
3411 i915_request_add(rq);
3412 if (!igt_wait_for_spinner(&spin_lo, rq)) {
3413 intel_gt_set_wedged(gt);
3418 rq = igt_request_alloc(ctx_hi, engine);
3420 igt_spinner_end(&spin_lo);
3425 /* Flush the previous CS ack before changing timeouts */
3426 while (READ_ONCE(engine->execlists.pending[0]))
3429 saved_timeout = engine->props.preempt_timeout_ms;
3430 engine->props.preempt_timeout_ms = 1; /* in ms, -> 1 jiffie */
3432 i915_request_get(rq);
3433 i915_request_add(rq);
3435 intel_engine_flush_submission(engine);
3436 engine->props.preempt_timeout_ms = saved_timeout;
3438 if (i915_request_wait(rq, 0, HZ / 10) < 0) {
3439 intel_gt_set_wedged(gt);
3440 i915_request_put(rq);
3445 igt_spinner_end(&spin_lo);
3446 i915_request_put(rq);
3451 kernel_context_close(ctx_lo);
3453 kernel_context_close(ctx_hi);
3455 igt_spinner_fini(&spin_lo);
3459 static int random_range(struct rnd_state *rnd, int min, int max)
3461 return i915_prandom_u32_max_state(max - min, rnd) + min;
3464 static int random_priority(struct rnd_state *rnd)
3466 return random_range(rnd, I915_PRIORITY_MIN, I915_PRIORITY_MAX);
3469 struct preempt_smoke {
3470 struct intel_gt *gt;
3471 struct i915_gem_context **contexts;
3472 struct intel_engine_cs *engine;
3473 struct drm_i915_gem_object *batch;
3474 unsigned int ncontext;
3475 struct rnd_state prng;
3476 unsigned long count;
3479 static struct i915_gem_context *smoke_context(struct preempt_smoke *smoke)
3481 return smoke->contexts[i915_prandom_u32_max_state(smoke->ncontext,
3485 static int smoke_submit(struct preempt_smoke *smoke,
3486 struct i915_gem_context *ctx, int prio,
3487 struct drm_i915_gem_object *batch)
3489 struct i915_request *rq;
3490 struct i915_vma *vma = NULL;
3494 struct i915_address_space *vm;
3496 vm = i915_gem_context_get_vm_rcu(ctx);
3497 vma = i915_vma_instance(batch, vm, NULL);
3500 return PTR_ERR(vma);
3502 err = i915_vma_pin(vma, 0, 0, PIN_USER);
3507 ctx->sched.priority = prio;
3509 rq = igt_request_alloc(ctx, smoke->engine);
3517 err = i915_request_await_object(rq, vma->obj, false);
3519 err = i915_vma_move_to_active(vma, rq, 0);
3521 err = rq->engine->emit_bb_start(rq,
3524 i915_vma_unlock(vma);
3527 i915_request_add(rq);
3531 i915_vma_unpin(vma);
3536 static int smoke_crescendo_thread(void *arg)
3538 struct preempt_smoke *smoke = arg;
3539 IGT_TIMEOUT(end_time);
3540 unsigned long count;
3544 struct i915_gem_context *ctx = smoke_context(smoke);
3547 err = smoke_submit(smoke,
3548 ctx, count % I915_PRIORITY_MAX,
3554 } while (count < smoke->ncontext && !__igt_timeout(end_time, NULL));
3556 smoke->count = count;
3560 static int smoke_crescendo(struct preempt_smoke *smoke, unsigned int flags)
3561 #define BATCH BIT(0)
3563 struct task_struct *tsk[I915_NUM_ENGINES] = {};
3564 struct preempt_smoke arg[I915_NUM_ENGINES];
3565 struct intel_engine_cs *engine;
3566 enum intel_engine_id id;
3567 unsigned long count;
3570 for_each_engine(engine, smoke->gt, id) {
3572 arg[id].engine = engine;
3573 if (!(flags & BATCH))
3574 arg[id].batch = NULL;
3577 tsk[id] = kthread_run(smoke_crescendo_thread, &arg,
3578 "igt/smoke:%d", id);
3579 if (IS_ERR(tsk[id])) {
3580 err = PTR_ERR(tsk[id]);
3583 get_task_struct(tsk[id]);
3586 yield(); /* start all threads before we kthread_stop() */
3589 for_each_engine(engine, smoke->gt, id) {
3592 if (IS_ERR_OR_NULL(tsk[id]))
3595 status = kthread_stop(tsk[id]);
3599 count += arg[id].count;
3601 put_task_struct(tsk[id]);
3604 pr_info("Submitted %lu crescendo:%x requests across %d engines and %d contexts\n",
3605 count, flags, smoke->gt->info.num_engines, smoke->ncontext);
3609 static int smoke_random(struct preempt_smoke *smoke, unsigned int flags)
3611 enum intel_engine_id id;
3612 IGT_TIMEOUT(end_time);
3613 unsigned long count;
3617 for_each_engine(smoke->engine, smoke->gt, id) {
3618 struct i915_gem_context *ctx = smoke_context(smoke);
3621 err = smoke_submit(smoke,
3622 ctx, random_priority(&smoke->prng),
3623 flags & BATCH ? smoke->batch : NULL);
3629 } while (count < smoke->ncontext && !__igt_timeout(end_time, NULL));
3631 pr_info("Submitted %lu random:%x requests across %d engines and %d contexts\n",
3632 count, flags, smoke->gt->info.num_engines, smoke->ncontext);
3636 static int live_preempt_smoke(void *arg)
3638 struct preempt_smoke smoke = {
3640 .prng = I915_RND_STATE_INITIALIZER(i915_selftest.random_seed),
3643 const unsigned int phase[] = { 0, BATCH };
3644 struct igt_live_test t;
3649 smoke.contexts = kmalloc_array(smoke.ncontext,
3650 sizeof(*smoke.contexts),
3652 if (!smoke.contexts)
3656 i915_gem_object_create_internal(smoke.gt->i915, PAGE_SIZE);
3657 if (IS_ERR(smoke.batch)) {
3658 err = PTR_ERR(smoke.batch);
3662 cs = i915_gem_object_pin_map_unlocked(smoke.batch, I915_MAP_WB);
3667 for (n = 0; n < PAGE_SIZE / sizeof(*cs) - 1; n++)
3668 cs[n] = MI_ARB_CHECK;
3669 cs[n] = MI_BATCH_BUFFER_END;
3670 i915_gem_object_flush_map(smoke.batch);
3671 i915_gem_object_unpin_map(smoke.batch);
3673 if (igt_live_test_begin(&t, smoke.gt->i915, __func__, "all")) {
3678 for (n = 0; n < smoke.ncontext; n++) {
3679 smoke.contexts[n] = kernel_context(smoke.gt->i915);
3680 if (!smoke.contexts[n])
3684 for (n = 0; n < ARRAY_SIZE(phase); n++) {
3685 err = smoke_crescendo(&smoke, phase[n]);
3689 err = smoke_random(&smoke, phase[n]);
3695 if (igt_live_test_end(&t))
3698 for (n = 0; n < smoke.ncontext; n++) {
3699 if (!smoke.contexts[n])
3701 kernel_context_close(smoke.contexts[n]);
3705 i915_gem_object_put(smoke.batch);
3707 kfree(smoke.contexts);
3712 static int nop_virtual_engine(struct intel_gt *gt,
3713 struct intel_engine_cs **siblings,
3714 unsigned int nsibling,
3717 #define CHAIN BIT(0)
3719 IGT_TIMEOUT(end_time);
3720 struct i915_request *request[16] = {};
3721 struct intel_context *ve[16];
3722 unsigned long n, prime, nc;
3723 struct igt_live_test t;
3724 ktime_t times[2] = {};
3727 GEM_BUG_ON(!nctx || nctx > ARRAY_SIZE(ve));
3729 for (n = 0; n < nctx; n++) {
3730 ve[n] = intel_execlists_create_virtual(siblings, nsibling);
3731 if (IS_ERR(ve[n])) {
3732 err = PTR_ERR(ve[n]);
3737 err = intel_context_pin(ve[n]);
3739 intel_context_put(ve[n]);
3745 err = igt_live_test_begin(&t, gt->i915, __func__, ve[0]->engine->name);
3749 for_each_prime_number_from(prime, 1, 8192) {
3750 times[1] = ktime_get_raw();
3752 if (flags & CHAIN) {
3753 for (nc = 0; nc < nctx; nc++) {
3754 for (n = 0; n < prime; n++) {
3755 struct i915_request *rq;
3757 rq = i915_request_create(ve[nc]);
3764 i915_request_put(request[nc]);
3765 request[nc] = i915_request_get(rq);
3766 i915_request_add(rq);
3770 for (n = 0; n < prime; n++) {
3771 for (nc = 0; nc < nctx; nc++) {
3772 struct i915_request *rq;
3774 rq = i915_request_create(ve[nc]);
3781 i915_request_put(request[nc]);
3782 request[nc] = i915_request_get(rq);
3783 i915_request_add(rq);
3788 for (nc = 0; nc < nctx; nc++) {
3789 if (i915_request_wait(request[nc], 0, HZ / 10) < 0) {
3790 pr_err("%s(%s): wait for %llx:%lld timed out\n",
3791 __func__, ve[0]->engine->name,
3792 request[nc]->fence.context,
3793 request[nc]->fence.seqno);
3795 GEM_TRACE("%s(%s) failed at request %llx:%lld\n",
3796 __func__, ve[0]->engine->name,
3797 request[nc]->fence.context,
3798 request[nc]->fence.seqno);
3800 intel_gt_set_wedged(gt);
3805 times[1] = ktime_sub(ktime_get_raw(), times[1]);
3807 times[0] = times[1];
3809 for (nc = 0; nc < nctx; nc++) {
3810 i915_request_put(request[nc]);
3814 if (__igt_timeout(end_time, NULL))
3818 err = igt_live_test_end(&t);
3822 pr_info("Requestx%d latencies on %s: 1 = %lluns, %lu = %lluns\n",
3823 nctx, ve[0]->engine->name, ktime_to_ns(times[0]),
3824 prime, div64_u64(ktime_to_ns(times[1]), prime));
3827 if (igt_flush_test(gt->i915))
3830 for (nc = 0; nc < nctx; nc++) {
3831 i915_request_put(request[nc]);
3832 intel_context_unpin(ve[nc]);
3833 intel_context_put(ve[nc]);
3839 __select_siblings(struct intel_gt *gt,
3841 struct intel_engine_cs **siblings,
3842 bool (*filter)(const struct intel_engine_cs *))
3847 for (inst = 0; inst <= MAX_ENGINE_INSTANCE; inst++) {
3848 if (!gt->engine_class[class][inst])
3851 if (filter && !filter(gt->engine_class[class][inst]))
3854 siblings[n++] = gt->engine_class[class][inst];
3861 select_siblings(struct intel_gt *gt,
3863 struct intel_engine_cs **siblings)
3865 return __select_siblings(gt, class, siblings, NULL);
3868 static int live_virtual_engine(void *arg)
3870 struct intel_gt *gt = arg;
3871 struct intel_engine_cs *siblings[MAX_ENGINE_INSTANCE + 1];
3872 struct intel_engine_cs *engine;
3873 enum intel_engine_id id;
3877 if (intel_uc_uses_guc_submission(>->uc))
3880 for_each_engine(engine, gt, id) {
3881 err = nop_virtual_engine(gt, &engine, 1, 1, 0);
3883 pr_err("Failed to wrap engine %s: err=%d\n",
3889 for (class = 0; class <= MAX_ENGINE_CLASS; class++) {
3892 nsibling = select_siblings(gt, class, siblings);
3896 for (n = 1; n <= nsibling + 1; n++) {
3897 err = nop_virtual_engine(gt, siblings, nsibling,
3903 err = nop_virtual_engine(gt, siblings, nsibling, n, CHAIN);
3911 static int mask_virtual_engine(struct intel_gt *gt,
3912 struct intel_engine_cs **siblings,
3913 unsigned int nsibling)
3915 struct i915_request *request[MAX_ENGINE_INSTANCE + 1];
3916 struct intel_context *ve;
3917 struct igt_live_test t;
3922 * Check that by setting the execution mask on a request, we can
3923 * restrict it to our desired engine within the virtual engine.
3926 ve = intel_execlists_create_virtual(siblings, nsibling);
3932 err = intel_context_pin(ve);
3936 err = igt_live_test_begin(&t, gt->i915, __func__, ve->engine->name);
3940 for (n = 0; n < nsibling; n++) {
3941 request[n] = i915_request_create(ve);
3942 if (IS_ERR(request[n])) {
3943 err = PTR_ERR(request[n]);
3948 /* Reverse order as it's more likely to be unnatural */
3949 request[n]->execution_mask = siblings[nsibling - n - 1]->mask;
3951 i915_request_get(request[n]);
3952 i915_request_add(request[n]);
3955 for (n = 0; n < nsibling; n++) {
3956 if (i915_request_wait(request[n], 0, HZ / 10) < 0) {
3957 pr_err("%s(%s): wait for %llx:%lld timed out\n",
3958 __func__, ve->engine->name,
3959 request[n]->fence.context,
3960 request[n]->fence.seqno);
3962 GEM_TRACE("%s(%s) failed at request %llx:%lld\n",
3963 __func__, ve->engine->name,
3964 request[n]->fence.context,
3965 request[n]->fence.seqno);
3967 intel_gt_set_wedged(gt);
3972 if (request[n]->engine != siblings[nsibling - n - 1]) {
3973 pr_err("Executed on wrong sibling '%s', expected '%s'\n",
3974 request[n]->engine->name,
3975 siblings[nsibling - n - 1]->name);
3981 err = igt_live_test_end(&t);
3983 if (igt_flush_test(gt->i915))
3986 for (n = 0; n < nsibling; n++)
3987 i915_request_put(request[n]);
3990 intel_context_unpin(ve);
3992 intel_context_put(ve);
3997 static int live_virtual_mask(void *arg)
3999 struct intel_gt *gt = arg;
4000 struct intel_engine_cs *siblings[MAX_ENGINE_INSTANCE + 1];
4004 if (intel_uc_uses_guc_submission(>->uc))
4007 for (class = 0; class <= MAX_ENGINE_CLASS; class++) {
4008 unsigned int nsibling;
4010 nsibling = select_siblings(gt, class, siblings);
4014 err = mask_virtual_engine(gt, siblings, nsibling);
4022 static int slicein_virtual_engine(struct intel_gt *gt,
4023 struct intel_engine_cs **siblings,
4024 unsigned int nsibling)
4026 const long timeout = slice_timeout(siblings[0]);
4027 struct intel_context *ce;
4028 struct i915_request *rq;
4029 struct igt_spinner spin;
4034 * Virtual requests must take part in timeslicing on the target engines.
4037 if (igt_spinner_init(&spin, gt))
4040 for (n = 0; n < nsibling; n++) {
4041 ce = intel_context_create(siblings[n]);
4047 rq = igt_spinner_create_request(&spin, ce, MI_ARB_CHECK);
4048 intel_context_put(ce);
4054 i915_request_add(rq);
4057 ce = intel_execlists_create_virtual(siblings, nsibling);
4063 rq = intel_context_create_request(ce);
4064 intel_context_put(ce);
4070 i915_request_get(rq);
4071 i915_request_add(rq);
4072 if (i915_request_wait(rq, 0, timeout) < 0) {
4073 GEM_TRACE_ERR("%s(%s) failed to slice in virtual request\n",
4074 __func__, rq->engine->name);
4076 intel_gt_set_wedged(gt);
4079 i915_request_put(rq);
4082 igt_spinner_end(&spin);
4083 if (igt_flush_test(gt->i915))
4085 igt_spinner_fini(&spin);
4089 static int sliceout_virtual_engine(struct intel_gt *gt,
4090 struct intel_engine_cs **siblings,
4091 unsigned int nsibling)
4093 const long timeout = slice_timeout(siblings[0]);
4094 struct intel_context *ce;
4095 struct i915_request *rq;
4096 struct igt_spinner spin;
4101 * Virtual requests must allow others a fair timeslice.
4104 if (igt_spinner_init(&spin, gt))
4107 /* XXX We do not handle oversubscription and fairness with normal rq */
4108 for (n = 0; n < nsibling; n++) {
4109 ce = intel_execlists_create_virtual(siblings, nsibling);
4115 rq = igt_spinner_create_request(&spin, ce, MI_ARB_CHECK);
4116 intel_context_put(ce);
4122 i915_request_add(rq);
4125 for (n = 0; !err && n < nsibling; n++) {
4126 ce = intel_context_create(siblings[n]);
4132 rq = intel_context_create_request(ce);
4133 intel_context_put(ce);
4139 i915_request_get(rq);
4140 i915_request_add(rq);
4141 if (i915_request_wait(rq, 0, timeout) < 0) {
4142 GEM_TRACE_ERR("%s(%s) failed to slice out virtual request\n",
4143 __func__, siblings[n]->name);
4145 intel_gt_set_wedged(gt);
4148 i915_request_put(rq);
4152 igt_spinner_end(&spin);
4153 if (igt_flush_test(gt->i915))
4155 igt_spinner_fini(&spin);
4159 static int live_virtual_slice(void *arg)
4161 struct intel_gt *gt = arg;
4162 struct intel_engine_cs *siblings[MAX_ENGINE_INSTANCE + 1];
4166 if (intel_uc_uses_guc_submission(>->uc))
4169 for (class = 0; class <= MAX_ENGINE_CLASS; class++) {
4170 unsigned int nsibling;
4172 nsibling = __select_siblings(gt, class, siblings,
4173 intel_engine_has_timeslices);
4177 err = slicein_virtual_engine(gt, siblings, nsibling);
4181 err = sliceout_virtual_engine(gt, siblings, nsibling);
4189 static int preserved_virtual_engine(struct intel_gt *gt,
4190 struct intel_engine_cs **siblings,
4191 unsigned int nsibling)
4193 struct i915_request *last = NULL;
4194 struct intel_context *ve;
4195 struct i915_vma *scratch;
4196 struct igt_live_test t;
4202 __vm_create_scratch_for_read_pinned(&siblings[0]->gt->ggtt->vm,
4204 if (IS_ERR(scratch))
4205 return PTR_ERR(scratch);
4207 err = i915_vma_sync(scratch);
4211 ve = intel_execlists_create_virtual(siblings, nsibling);
4217 err = intel_context_pin(ve);
4221 err = igt_live_test_begin(&t, gt->i915, __func__, ve->engine->name);
4225 for (n = 0; n < NUM_GPR_DW; n++) {
4226 struct intel_engine_cs *engine = siblings[n % nsibling];
4227 struct i915_request *rq;
4229 rq = i915_request_create(ve);
4235 i915_request_put(last);
4236 last = i915_request_get(rq);
4238 cs = intel_ring_begin(rq, 8);
4240 i915_request_add(rq);
4245 *cs++ = MI_STORE_REGISTER_MEM_GEN8 | MI_USE_GGTT;
4246 *cs++ = CS_GPR(engine, n);
4247 *cs++ = i915_ggtt_offset(scratch) + n * sizeof(u32);
4250 *cs++ = MI_LOAD_REGISTER_IMM(1);
4251 *cs++ = CS_GPR(engine, (n + 1) % NUM_GPR_DW);
4255 intel_ring_advance(rq, cs);
4257 /* Restrict this request to run on a particular engine */
4258 rq->execution_mask = engine->mask;
4259 i915_request_add(rq);
4262 if (i915_request_wait(last, 0, HZ / 5) < 0) {
4267 cs = i915_gem_object_pin_map_unlocked(scratch->obj, I915_MAP_WB);
4273 for (n = 0; n < NUM_GPR_DW; n++) {
4275 pr_err("Incorrect value[%d] found for GPR[%d]\n",
4282 i915_gem_object_unpin_map(scratch->obj);
4285 if (igt_live_test_end(&t))
4287 i915_request_put(last);
4289 intel_context_unpin(ve);
4291 intel_context_put(ve);
4293 i915_vma_unpin_and_release(&scratch, 0);
4297 static int live_virtual_preserved(void *arg)
4299 struct intel_gt *gt = arg;
4300 struct intel_engine_cs *siblings[MAX_ENGINE_INSTANCE + 1];
4304 * Check that the context image retains non-privileged (user) registers
4305 * from one engine to the next. For this we check that the CS_GPR
4309 if (intel_uc_uses_guc_submission(>->uc))
4312 /* As we use CS_GPR we cannot run before they existed on all engines. */
4313 if (GRAPHICS_VER(gt->i915) < 9)
4316 for (class = 0; class <= MAX_ENGINE_CLASS; class++) {
4319 nsibling = select_siblings(gt, class, siblings);
4323 err = preserved_virtual_engine(gt, siblings, nsibling);
4331 static int bond_virtual_engine(struct intel_gt *gt,
4333 struct intel_engine_cs **siblings,
4334 unsigned int nsibling,
4336 #define BOND_SCHEDULE BIT(0)
4338 struct intel_engine_cs *master;
4339 struct i915_request *rq[16];
4340 enum intel_engine_id id;
4341 struct igt_spinner spin;
4346 * A set of bonded requests is intended to be run concurrently
4347 * across a number of engines. We use one request per-engine
4348 * and a magic fence to schedule each of the bonded requests
4349 * at the same time. A consequence of our current scheduler is that
4350 * we only move requests to the HW ready queue when the request
4351 * becomes ready, that is when all of its prerequisite fences have
4352 * been signaled. As one of those fences is the master submit fence,
4353 * there is a delay on all secondary fences as the HW may be
4354 * currently busy. Equally, as all the requests are independent,
4355 * they may have other fences that delay individual request
4356 * submission to HW. Ergo, we do not guarantee that all requests are
4357 * immediately submitted to HW at the same time, just that if the
4358 * rules are abided by, they are ready at the same time as the
4359 * first is submitted. Userspace can embed semaphores in its batch
4360 * to ensure parallel execution of its phases as it requires.
4361 * Though naturally it gets requested that perhaps the scheduler should
4362 * take care of parallel execution, even across preemption events on
4363 * different HW. (The proper answer is of course "lalalala".)
4365 * With the submit-fence, we have identified three possible phases
4366 * of synchronisation depending on the master fence: queued (not
4367 * ready), executing, and signaled. The first two are quite simple
4368 * and checked below. However, the signaled master fence handling is
4369 * contentious. Currently we do not distinguish between a signaled
4370 * fence and an expired fence, as once signaled it does not convey
4371 * any information about the previous execution. It may even be freed
4372 * and hence checking later it may not exist at all. Ergo we currently
4373 * do not apply the bonding constraint for an already signaled fence,
4374 * as our expectation is that it should not constrain the secondaries
4375 * and is outside of the scope of the bonded request API (i.e. all
4376 * userspace requests are meant to be running in parallel). As
4377 * it imposes no constraint, and is effectively a no-op, we do not
4378 * check below as normal execution flows are checked extensively above.
4380 * XXX Is the degenerate handling of signaled submit fences the
4381 * expected behaviour for userpace?
4384 GEM_BUG_ON(nsibling >= ARRAY_SIZE(rq) - 1);
4386 if (igt_spinner_init(&spin, gt))
4390 rq[0] = ERR_PTR(-ENOMEM);
4391 for_each_engine(master, gt, id) {
4392 struct i915_sw_fence fence = {};
4393 struct intel_context *ce;
4395 if (master->class == class)
4398 ce = intel_context_create(master);
4404 memset_p((void *)rq, ERR_PTR(-EINVAL), ARRAY_SIZE(rq));
4406 rq[0] = igt_spinner_create_request(&spin, ce, MI_NOOP);
4407 intel_context_put(ce);
4408 if (IS_ERR(rq[0])) {
4409 err = PTR_ERR(rq[0]);
4412 i915_request_get(rq[0]);
4414 if (flags & BOND_SCHEDULE) {
4415 onstack_fence_init(&fence);
4416 err = i915_sw_fence_await_sw_fence_gfp(&rq[0]->submit,
4421 i915_request_add(rq[0]);
4425 if (!(flags & BOND_SCHEDULE) &&
4426 !igt_wait_for_spinner(&spin, rq[0])) {
4431 for (n = 0; n < nsibling; n++) {
4432 struct intel_context *ve;
4434 ve = intel_execlists_create_virtual(siblings, nsibling);
4437 onstack_fence_fini(&fence);
4441 err = intel_virtual_engine_attach_bond(ve->engine,
4445 intel_context_put(ve);
4446 onstack_fence_fini(&fence);
4450 err = intel_context_pin(ve);
4451 intel_context_put(ve);
4453 onstack_fence_fini(&fence);
4457 rq[n + 1] = i915_request_create(ve);
4458 intel_context_unpin(ve);
4459 if (IS_ERR(rq[n + 1])) {
4460 err = PTR_ERR(rq[n + 1]);
4461 onstack_fence_fini(&fence);
4464 i915_request_get(rq[n + 1]);
4466 err = i915_request_await_execution(rq[n + 1],
4468 ve->engine->bond_execute);
4469 i915_request_add(rq[n + 1]);
4471 onstack_fence_fini(&fence);
4475 onstack_fence_fini(&fence);
4476 intel_engine_flush_submission(master);
4477 igt_spinner_end(&spin);
4479 if (i915_request_wait(rq[0], 0, HZ / 10) < 0) {
4480 pr_err("Master request did not execute (on %s)!\n",
4481 rq[0]->engine->name);
4486 for (n = 0; n < nsibling; n++) {
4487 if (i915_request_wait(rq[n + 1], 0,
4488 MAX_SCHEDULE_TIMEOUT) < 0) {
4493 if (rq[n + 1]->engine != siblings[n]) {
4494 pr_err("Bonded request did not execute on target engine: expected %s, used %s; master was %s\n",
4496 rq[n + 1]->engine->name,
4497 rq[0]->engine->name);
4503 for (n = 0; !IS_ERR(rq[n]); n++)
4504 i915_request_put(rq[n]);
4505 rq[0] = ERR_PTR(-ENOMEM);
4509 for (n = 0; !IS_ERR(rq[n]); n++)
4510 i915_request_put(rq[n]);
4511 if (igt_flush_test(gt->i915))
4514 igt_spinner_fini(&spin);
4518 static int live_virtual_bond(void *arg)
4520 static const struct phase {
4525 { "schedule", BOND_SCHEDULE },
4528 struct intel_gt *gt = arg;
4529 struct intel_engine_cs *siblings[MAX_ENGINE_INSTANCE + 1];
4533 if (intel_uc_uses_guc_submission(>->uc))
4536 for (class = 0; class <= MAX_ENGINE_CLASS; class++) {
4537 const struct phase *p;
4540 nsibling = select_siblings(gt, class, siblings);
4544 for (p = phases; p->name; p++) {
4545 err = bond_virtual_engine(gt,
4546 class, siblings, nsibling,
4549 pr_err("%s(%s): failed class=%d, nsibling=%d, err=%d\n",
4550 __func__, p->name, class, nsibling, err);
4559 static int reset_virtual_engine(struct intel_gt *gt,
4560 struct intel_engine_cs **siblings,
4561 unsigned int nsibling)
4563 struct intel_engine_cs *engine;
4564 struct intel_context *ve;
4565 struct igt_spinner spin;
4566 struct i915_request *rq;
4571 * In order to support offline error capture for fast preempt reset,
4572 * we need to decouple the guilty request and ensure that it and its
4573 * descendents are not executed while the capture is in progress.
4576 if (igt_spinner_init(&spin, gt))
4579 ve = intel_execlists_create_virtual(siblings, nsibling);
4585 for (n = 0; n < nsibling; n++)
4586 st_engine_heartbeat_disable(siblings[n]);
4588 rq = igt_spinner_create_request(&spin, ve, MI_ARB_CHECK);
4593 i915_request_add(rq);
4595 if (!igt_wait_for_spinner(&spin, rq)) {
4596 intel_gt_set_wedged(gt);
4601 engine = rq->engine;
4602 GEM_BUG_ON(engine == ve->engine);
4604 /* Take ownership of the reset and tasklet */
4605 err = engine_lock_reset_tasklet(engine);
4609 engine->sched_engine->tasklet.callback(&engine->sched_engine->tasklet);
4610 GEM_BUG_ON(execlists_active(&engine->execlists) != rq);
4612 /* Fake a preemption event; failed of course */
4613 spin_lock_irq(&engine->sched_engine->lock);
4614 __unwind_incomplete_requests(engine);
4615 spin_unlock_irq(&engine->sched_engine->lock);
4616 GEM_BUG_ON(rq->engine != engine);
4618 /* Reset the engine while keeping our active request on hold */
4619 execlists_hold(engine, rq);
4620 GEM_BUG_ON(!i915_request_on_hold(rq));
4622 __intel_engine_reset_bh(engine, NULL);
4623 GEM_BUG_ON(rq->fence.error != -EIO);
4625 /* Release our grasp on the engine, letting CS flow again */
4626 engine_unlock_reset_tasklet(engine);
4628 /* Check that we do not resubmit the held request */
4629 i915_request_get(rq);
4630 if (!i915_request_wait(rq, 0, HZ / 5)) {
4631 pr_err("%s: on hold request completed!\n",
4633 intel_gt_set_wedged(gt);
4637 GEM_BUG_ON(!i915_request_on_hold(rq));
4639 /* But is resubmitted on release */
4640 execlists_unhold(engine, rq);
4641 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
4642 pr_err("%s: held request did not complete!\n",
4644 intel_gt_set_wedged(gt);
4649 i915_request_put(rq);
4651 for (n = 0; n < nsibling; n++)
4652 st_engine_heartbeat_enable(siblings[n]);
4654 intel_context_put(ve);
4656 igt_spinner_fini(&spin);
4660 static int live_virtual_reset(void *arg)
4662 struct intel_gt *gt = arg;
4663 struct intel_engine_cs *siblings[MAX_ENGINE_INSTANCE + 1];
4667 * Check that we handle a reset event within a virtual engine.
4668 * Only the physical engine is reset, but we have to check the flow
4669 * of the virtual requests around the reset, and make sure it is not
4673 if (intel_uc_uses_guc_submission(>->uc))
4676 if (!intel_has_reset_engine(gt))
4679 for (class = 0; class <= MAX_ENGINE_CLASS; class++) {
4682 nsibling = select_siblings(gt, class, siblings);
4686 err = reset_virtual_engine(gt, siblings, nsibling);
4694 int intel_execlists_live_selftests(struct drm_i915_private *i915)
4696 static const struct i915_subtest tests[] = {
4697 SUBTEST(live_sanitycheck),
4698 SUBTEST(live_unlite_switch),
4699 SUBTEST(live_unlite_preempt),
4700 SUBTEST(live_unlite_ring),
4701 SUBTEST(live_pin_rewind),
4702 SUBTEST(live_hold_reset),
4703 SUBTEST(live_error_interrupt),
4704 SUBTEST(live_timeslice_preempt),
4705 SUBTEST(live_timeslice_rewind),
4706 SUBTEST(live_timeslice_queue),
4707 SUBTEST(live_timeslice_nopreempt),
4708 SUBTEST(live_busywait_preempt),
4709 SUBTEST(live_preempt),
4710 SUBTEST(live_late_preempt),
4711 SUBTEST(live_nopreempt),
4712 SUBTEST(live_preempt_cancel),
4713 SUBTEST(live_suppress_self_preempt),
4714 SUBTEST(live_chain_preempt),
4715 SUBTEST(live_preempt_ring),
4716 SUBTEST(live_preempt_gang),
4717 SUBTEST(live_preempt_timeout),
4718 SUBTEST(live_preempt_user),
4719 SUBTEST(live_preempt_smoke),
4720 SUBTEST(live_virtual_engine),
4721 SUBTEST(live_virtual_mask),
4722 SUBTEST(live_virtual_preserved),
4723 SUBTEST(live_virtual_slice),
4724 SUBTEST(live_virtual_bond),
4725 SUBTEST(live_virtual_reset),
4728 if (i915->gt.submission_method != INTEL_SUBMISSION_ELSP)
4731 if (intel_gt_is_wedged(&i915->gt))
4734 return intel_gt_live_subtests(tests, &i915->gt);