1 // SPDX-License-Identifier: MIT
3 * Copyright © 2017-2018 Intel Corporation
6 #include <linux/prime_numbers.h>
7 #include <linux/string_helpers.h>
9 #include "intel_context.h"
10 #include "intel_engine_heartbeat.h"
11 #include "intel_engine_pm.h"
12 #include "intel_engine_regs.h"
13 #include "intel_gpu_commands.h"
15 #include "intel_gt_requests.h"
16 #include "intel_ring.h"
17 #include "selftest_engine_heartbeat.h"
19 #include "../selftests/i915_random.h"
20 #include "../i915_selftest.h"
22 #include "selftests/igt_flush_test.h"
23 #include "selftests/lib_sw_fence.h"
24 #include "selftests/mock_gem_device.h"
25 #include "selftests/mock_timeline.h"
27 static struct page *hwsp_page(struct intel_timeline *tl)
29 struct drm_i915_gem_object *obj = tl->hwsp_ggtt->obj;
31 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
32 return sg_page(obj->mm.pages->sgl);
35 static unsigned long hwsp_cacheline(struct intel_timeline *tl)
37 unsigned long address = (unsigned long)page_address(hwsp_page(tl));
39 return (address + offset_in_page(tl->hwsp_offset)) / TIMELINE_SEQNO_BYTES;
42 static int selftest_tl_pin(struct intel_timeline *tl)
44 struct i915_gem_ww_ctx ww;
47 i915_gem_ww_ctx_init(&ww, false);
49 err = i915_gem_object_lock(tl->hwsp_ggtt->obj, &ww);
51 err = intel_timeline_pin(tl, &ww);
53 if (err == -EDEADLK) {
54 err = i915_gem_ww_ctx_backoff(&ww);
58 i915_gem_ww_ctx_fini(&ww);
62 /* Only half of seqno's are usable, see __intel_timeline_get_seqno() */
63 #define CACHELINES_PER_PAGE (PAGE_SIZE / TIMELINE_SEQNO_BYTES / 2)
65 struct mock_hwsp_freelist {
67 struct radix_tree_root cachelines;
68 struct intel_timeline **history;
69 unsigned long count, max;
70 struct rnd_state prng;
77 static void __mock_hwsp_record(struct mock_hwsp_freelist *state,
79 struct intel_timeline *tl)
81 tl = xchg(&state->history[idx], tl);
83 radix_tree_delete(&state->cachelines, hwsp_cacheline(tl));
84 intel_timeline_unpin(tl);
85 intel_timeline_put(tl);
89 static int __mock_hwsp_timeline(struct mock_hwsp_freelist *state,
93 struct intel_timeline *tl;
97 unsigned long cacheline;
100 tl = intel_timeline_create(state->gt);
104 err = selftest_tl_pin(tl);
106 intel_timeline_put(tl);
110 cacheline = hwsp_cacheline(tl);
111 err = radix_tree_insert(&state->cachelines, cacheline, tl);
113 if (err == -EEXIST) {
114 pr_err("HWSP cacheline %lu already used; duplicate allocation!\n",
117 intel_timeline_unpin(tl);
118 intel_timeline_put(tl);
122 idx = state->count++ % state->max;
123 __mock_hwsp_record(state, idx, tl);
127 i915_prandom_shuffle(state->history,
128 sizeof(*state->history),
129 min(state->count, state->max),
132 count = i915_prandom_u32_max_state(min(state->count, state->max),
135 idx = --state->count % state->max;
136 __mock_hwsp_record(state, idx, NULL);
142 static int mock_hwsp_freelist(void *arg)
144 struct mock_hwsp_freelist state;
145 struct drm_i915_private *i915;
151 { "shuffled", SHUFFLE },
157 i915 = mock_gem_device();
161 INIT_RADIX_TREE(&state.cachelines, GFP_KERNEL);
162 state.prng = I915_RND_STATE_INITIALIZER(i915_selftest.random_seed);
164 state.gt = to_gt(i915);
167 * Create a bunch of timelines and check that their HWSP do not overlap.
168 * Free some, and try again.
171 state.max = PAGE_SIZE / sizeof(*state.history);
173 state.history = kcalloc(state.max, sizeof(*state.history), GFP_KERNEL);
174 if (!state.history) {
179 for (p = phases; p->name; p++) {
180 pr_debug("%s(%s)\n", __func__, p->name);
181 for_each_prime_number_from(na, 1, 2 * CACHELINES_PER_PAGE) {
182 err = __mock_hwsp_timeline(&state, na, p->flags);
189 for (na = 0; na < state.max; na++)
190 __mock_hwsp_record(&state, na, NULL);
191 kfree(state.history);
193 mock_destroy_device(i915);
204 static int __igt_sync(struct intel_timeline *tl,
206 const struct __igt_sync *p,
211 if (__intel_timeline_sync_is_later(tl, ctx, p->seqno) != p->expected) {
212 pr_err("%s: %s(ctx=%llu, seqno=%u) expected passed %s but failed\n",
213 name, p->name, ctx, p->seqno, str_yes_no(p->expected));
218 ret = __intel_timeline_sync_set(tl, ctx, p->seqno);
226 static int igt_sync(void *arg)
228 const struct __igt_sync pass[] = {
229 { "unset", 0, false, false },
230 { "new", 0, false, true },
231 { "0a", 0, true, true },
232 { "1a", 1, false, true },
233 { "1b", 1, true, true },
234 { "0b", 0, true, false },
235 { "2a", 2, false, true },
236 { "4", 4, false, true },
237 { "INT_MAX", INT_MAX, false, true },
238 { "INT_MAX-1", INT_MAX-1, true, false },
239 { "INT_MAX+1", (u32)INT_MAX+1, false, true },
240 { "INT_MAX", INT_MAX, true, false },
241 { "UINT_MAX", UINT_MAX, false, true },
242 { "wrap", 0, false, true },
243 { "unwrap", UINT_MAX, true, false },
246 struct intel_timeline tl;
250 mock_timeline_init(&tl, 0);
251 for (p = pass; p->name; p++) {
252 for (order = 1; order < 64; order++) {
253 for (offset = -1; offset <= (order > 1); offset++) {
254 u64 ctx = BIT_ULL(order) + offset;
256 ret = __igt_sync(&tl, ctx, p, "1");
262 mock_timeline_fini(&tl);
264 mock_timeline_init(&tl, 0);
265 for (order = 1; order < 64; order++) {
266 for (offset = -1; offset <= (order > 1); offset++) {
267 u64 ctx = BIT_ULL(order) + offset;
269 for (p = pass; p->name; p++) {
270 ret = __igt_sync(&tl, ctx, p, "2");
278 mock_timeline_fini(&tl);
282 static unsigned int random_engine(struct rnd_state *rnd)
284 return i915_prandom_u32_max_state(I915_NUM_ENGINES, rnd);
287 static int bench_sync(void *arg)
289 struct rnd_state prng;
290 struct intel_timeline tl;
291 unsigned long end_time, count;
294 int order, last_order;
296 mock_timeline_init(&tl, 0);
298 /* Lookups from cache are very fast and so the random number generation
299 * and the loop itself becomes a significant factor in the per-iteration
300 * timings. We try to compensate the results by measuring the overhead
301 * of the prng and subtract it from the reported results.
303 prandom_seed_state(&prng, i915_selftest.random_seed);
306 end_time = jiffies + HZ/10;
310 /* Make sure the compiler doesn't optimise away the prng call */
311 WRITE_ONCE(x, prandom_u32_state(&prng));
314 } while (!time_after(jiffies, end_time));
315 kt = ktime_sub(ktime_get(), kt);
316 pr_debug("%s: %lu random evaluations, %lluns/prng\n",
317 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
318 prng32_1M = div64_ul(ktime_to_ns(kt) << 20, count);
320 /* Benchmark (only) setting random context ids */
321 prandom_seed_state(&prng, i915_selftest.random_seed);
324 end_time = jiffies + HZ/10;
326 u64 id = i915_prandom_u64_state(&prng);
328 __intel_timeline_sync_set(&tl, id, 0);
330 } while (!time_after(jiffies, end_time));
331 kt = ktime_sub(ktime_get(), kt);
332 kt = ktime_sub_ns(kt, (count * prng32_1M * 2) >> 20);
333 pr_info("%s: %lu random insertions, %lluns/insert\n",
334 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
336 /* Benchmark looking up the exact same context ids as we just set */
337 prandom_seed_state(&prng, i915_selftest.random_seed);
341 u64 id = i915_prandom_u64_state(&prng);
343 if (!__intel_timeline_sync_is_later(&tl, id, 0)) {
344 mock_timeline_fini(&tl);
345 pr_err("Lookup of %llu failed\n", id);
349 kt = ktime_sub(ktime_get(), kt);
350 kt = ktime_sub_ns(kt, (count * prng32_1M * 2) >> 20);
351 pr_info("%s: %lu random lookups, %lluns/lookup\n",
352 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
354 mock_timeline_fini(&tl);
357 mock_timeline_init(&tl, 0);
359 /* Benchmark setting the first N (in order) contexts */
362 end_time = jiffies + HZ/10;
364 __intel_timeline_sync_set(&tl, count++, 0);
365 } while (!time_after(jiffies, end_time));
366 kt = ktime_sub(ktime_get(), kt);
367 pr_info("%s: %lu in-order insertions, %lluns/insert\n",
368 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
370 /* Benchmark looking up the exact same context ids as we just set */
374 if (!__intel_timeline_sync_is_later(&tl, end_time, 0)) {
375 pr_err("Lookup of %lu failed\n", end_time);
376 mock_timeline_fini(&tl);
380 kt = ktime_sub(ktime_get(), kt);
381 pr_info("%s: %lu in-order lookups, %lluns/lookup\n",
382 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
384 mock_timeline_fini(&tl);
387 mock_timeline_init(&tl, 0);
389 /* Benchmark searching for a random context id and maybe changing it */
390 prandom_seed_state(&prng, i915_selftest.random_seed);
393 end_time = jiffies + HZ/10;
395 u32 id = random_engine(&prng);
396 u32 seqno = prandom_u32_state(&prng);
398 if (!__intel_timeline_sync_is_later(&tl, id, seqno))
399 __intel_timeline_sync_set(&tl, id, seqno);
402 } while (!time_after(jiffies, end_time));
403 kt = ktime_sub(ktime_get(), kt);
404 kt = ktime_sub_ns(kt, (count * prng32_1M * 2) >> 20);
405 pr_info("%s: %lu repeated insert/lookups, %lluns/op\n",
406 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
407 mock_timeline_fini(&tl);
410 /* Benchmark searching for a known context id and changing the seqno */
411 for (last_order = 1, order = 1; order < 32;
412 ({ int tmp = last_order; last_order = order; order += tmp; })) {
413 unsigned int mask = BIT(order) - 1;
415 mock_timeline_init(&tl, 0);
419 end_time = jiffies + HZ/10;
421 /* Without assuming too many details of the underlying
422 * implementation, try to identify its phase-changes
425 u64 id = (u64)(count & mask) << order;
427 __intel_timeline_sync_is_later(&tl, id, 0);
428 __intel_timeline_sync_set(&tl, id, 0);
431 } while (!time_after(jiffies, end_time));
432 kt = ktime_sub(ktime_get(), kt);
433 pr_info("%s: %lu cyclic/%d insert/lookups, %lluns/op\n",
434 __func__, count, order,
435 (long long)div64_ul(ktime_to_ns(kt), count));
436 mock_timeline_fini(&tl);
443 int intel_timeline_mock_selftests(void)
445 static const struct i915_subtest tests[] = {
446 SUBTEST(mock_hwsp_freelist),
451 return i915_subtests(tests, NULL);
454 static int emit_ggtt_store_dw(struct i915_request *rq, u32 addr, u32 value)
458 cs = intel_ring_begin(rq, 4);
462 if (GRAPHICS_VER(rq->i915) >= 8) {
463 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
467 } else if (GRAPHICS_VER(rq->i915) >= 4) {
468 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
473 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
479 intel_ring_advance(rq, cs);
484 static struct i915_request *
485 checked_tl_write(struct intel_timeline *tl, struct intel_engine_cs *engine, u32 value)
487 struct i915_request *rq;
490 err = selftest_tl_pin(tl);
496 if (READ_ONCE(*tl->hwsp_seqno) != tl->seqno) {
497 pr_err("Timeline created with incorrect breadcrumb, found %x, expected %x\n",
498 *tl->hwsp_seqno, tl->seqno);
499 intel_timeline_unpin(tl);
500 return ERR_PTR(-EINVAL);
503 rq = intel_engine_create_kernel_request(engine);
507 i915_request_get(rq);
509 err = emit_ggtt_store_dw(rq, tl->hwsp_offset, value);
510 i915_request_add(rq);
512 i915_request_put(rq);
517 intel_timeline_unpin(tl);
520 pr_err("Failed to write to timeline!\n");
524 static int live_hwsp_engine(void *arg)
526 #define NUM_TIMELINES 4096
527 struct intel_gt *gt = arg;
528 struct intel_timeline **timelines;
529 struct intel_engine_cs *engine;
530 enum intel_engine_id id;
531 unsigned long count, n;
535 * Create a bunch of timelines and check we can write
536 * independently to each of their breadcrumb slots.
539 timelines = kvmalloc_array(NUM_TIMELINES * I915_NUM_ENGINES,
546 for_each_engine(engine, gt, id) {
547 if (!intel_engine_can_store_dword(engine))
550 intel_engine_pm_get(engine);
552 for (n = 0; n < NUM_TIMELINES; n++) {
553 struct intel_timeline *tl;
554 struct i915_request *rq;
556 tl = intel_timeline_create(gt);
562 rq = checked_tl_write(tl, engine, count);
564 intel_timeline_put(tl);
569 timelines[count++] = tl;
570 i915_request_put(rq);
573 intel_engine_pm_put(engine);
578 if (igt_flush_test(gt->i915))
581 for (n = 0; n < count; n++) {
582 struct intel_timeline *tl = timelines[n];
584 if (!err && READ_ONCE(*tl->hwsp_seqno) != n) {
585 GEM_TRACE_ERR("Invalid seqno:%lu stored in timeline %llu @ %x, found 0x%x\n",
586 n, tl->fence_context, tl->hwsp_offset, *tl->hwsp_seqno);
590 intel_timeline_put(tl);
598 static int live_hwsp_alternate(void *arg)
600 #define NUM_TIMELINES 4096
601 struct intel_gt *gt = arg;
602 struct intel_timeline **timelines;
603 struct intel_engine_cs *engine;
604 enum intel_engine_id id;
605 unsigned long count, n;
609 * Create a bunch of timelines and check we can write
610 * independently to each of their breadcrumb slots with adjacent
614 timelines = kvmalloc_array(NUM_TIMELINES * I915_NUM_ENGINES,
621 for (n = 0; n < NUM_TIMELINES; n++) {
622 for_each_engine(engine, gt, id) {
623 struct intel_timeline *tl;
624 struct i915_request *rq;
626 if (!intel_engine_can_store_dword(engine))
629 tl = intel_timeline_create(gt);
635 intel_engine_pm_get(engine);
636 rq = checked_tl_write(tl, engine, count);
637 intel_engine_pm_put(engine);
639 intel_timeline_put(tl);
644 timelines[count++] = tl;
645 i915_request_put(rq);
650 if (igt_flush_test(gt->i915))
653 for (n = 0; n < count; n++) {
654 struct intel_timeline *tl = timelines[n];
656 if (!err && READ_ONCE(*tl->hwsp_seqno) != n) {
657 GEM_TRACE_ERR("Invalid seqno:%lu stored in timeline %llu @ %x, found 0x%x\n",
658 n, tl->fence_context, tl->hwsp_offset, *tl->hwsp_seqno);
662 intel_timeline_put(tl);
670 static int live_hwsp_wrap(void *arg)
672 struct intel_gt *gt = arg;
673 struct intel_engine_cs *engine;
674 struct intel_timeline *tl;
675 enum intel_engine_id id;
679 * Across a seqno wrap, we need to keep the old cacheline alive for
680 * foreign GPU references.
683 tl = intel_timeline_create(gt);
687 if (!tl->has_initial_breadcrumb)
690 err = selftest_tl_pin(tl);
694 for_each_engine(engine, gt, id) {
695 const u32 *hwsp_seqno[2];
696 struct i915_request *rq;
699 if (!intel_engine_can_store_dword(engine))
702 rq = intel_engine_create_kernel_request(engine);
710 mutex_lock_nested(&tl->mutex, SINGLE_DEPTH_NESTING);
711 err = intel_timeline_get_seqno(tl, rq, &seqno[0]);
712 mutex_unlock(&tl->mutex);
714 i915_request_add(rq);
717 pr_debug("seqno[0]:%08x, hwsp_offset:%08x\n",
718 seqno[0], tl->hwsp_offset);
720 err = emit_ggtt_store_dw(rq, tl->hwsp_offset, seqno[0]);
722 i915_request_add(rq);
725 hwsp_seqno[0] = tl->hwsp_seqno;
727 mutex_lock_nested(&tl->mutex, SINGLE_DEPTH_NESTING);
728 err = intel_timeline_get_seqno(tl, rq, &seqno[1]);
729 mutex_unlock(&tl->mutex);
731 i915_request_add(rq);
734 pr_debug("seqno[1]:%08x, hwsp_offset:%08x\n",
735 seqno[1], tl->hwsp_offset);
737 err = emit_ggtt_store_dw(rq, tl->hwsp_offset, seqno[1]);
739 i915_request_add(rq);
742 hwsp_seqno[1] = tl->hwsp_seqno;
744 /* With wrap should come a new hwsp */
745 GEM_BUG_ON(seqno[1] >= seqno[0]);
746 GEM_BUG_ON(hwsp_seqno[0] == hwsp_seqno[1]);
748 i915_request_add(rq);
750 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
751 pr_err("Wait for timeline writes timed out!\n");
756 if (READ_ONCE(*hwsp_seqno[0]) != seqno[0] ||
757 READ_ONCE(*hwsp_seqno[1]) != seqno[1]) {
758 pr_err("Bad timeline values: found (%x, %x), expected (%x, %x)\n",
759 *hwsp_seqno[0], *hwsp_seqno[1],
765 intel_gt_retire_requests(gt); /* recycle HWSP */
769 if (igt_flush_test(gt->i915))
772 intel_timeline_unpin(tl);
774 intel_timeline_put(tl);
778 static int emit_read_hwsp(struct i915_request *rq,
782 const u32 gpr = i915_mmio_reg_offset(GEN8_RING_CS_GPR(rq->engine->mmio_base, 0));
785 cs = intel_ring_begin(rq, 12);
789 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
795 *cs++ = MI_LOAD_REGISTER_MEM_GEN8 | MI_USE_GGTT;
800 *cs++ = MI_STORE_REGISTER_MEM_GEN8 | MI_USE_GGTT;
806 intel_ring_advance(rq, cs);
811 struct hwsp_watcher {
812 struct i915_vma *vma;
813 struct i915_request *rq;
818 static bool cmp_lt(u32 a, u32 b)
823 static bool cmp_gte(u32 a, u32 b)
828 static int setup_watcher(struct hwsp_watcher *w, struct intel_gt *gt,
829 struct intel_timeline *tl)
831 struct drm_i915_gem_object *obj;
832 struct i915_vma *vma;
834 obj = i915_gem_object_create_internal(gt->i915, SZ_2M);
838 /* keep the same cache settings as timeline */
839 i915_gem_object_set_pat_index(obj, tl->hwsp_ggtt->obj->pat_index);
840 w->map = i915_gem_object_pin_map_unlocked(obj,
841 page_unmask_bits(tl->hwsp_ggtt->obj->mm.mapping));
842 if (IS_ERR(w->map)) {
843 i915_gem_object_put(obj);
844 return PTR_ERR(w->map);
847 vma = i915_gem_object_ggtt_pin(obj, NULL, 0, 0, 0);
849 i915_gem_object_put(obj);
854 w->addr = i915_ggtt_offset(vma);
858 static void switch_tl_lock(struct i915_request *from, struct i915_request *to)
860 /* some light mutex juggling required; think co-routines */
863 lockdep_unpin_lock(&from->context->timeline->mutex, from->cookie);
864 mutex_unlock(&from->context->timeline->mutex);
868 mutex_lock(&to->context->timeline->mutex);
869 to->cookie = lockdep_pin_lock(&to->context->timeline->mutex);
873 static int create_watcher(struct hwsp_watcher *w,
874 struct intel_engine_cs *engine,
877 struct intel_context *ce;
879 ce = intel_context_create(engine);
883 ce->ring_size = ringsz;
884 w->rq = intel_context_create_request(ce);
885 intel_context_put(ce);
887 return PTR_ERR(w->rq);
889 w->addr = i915_ggtt_offset(w->vma);
891 switch_tl_lock(w->rq, NULL);
896 static int check_watcher(struct hwsp_watcher *w, const char *name,
897 bool (*op)(u32 hwsp, u32 seqno))
899 struct i915_request *rq = fetch_and_zero(&w->rq);
903 GEM_BUG_ON(w->addr - i915_ggtt_offset(w->vma) > w->vma->size);
905 i915_request_get(rq);
906 switch_tl_lock(NULL, rq);
907 i915_request_add(rq);
909 if (i915_request_wait(rq, 0, HZ) < 0) {
916 end = (w->addr - i915_ggtt_offset(w->vma)) / sizeof(*w->map);
917 while (offset < end) {
918 if (!op(w->map[offset + 1], w->map[offset])) {
919 pr_err("Watcher '%s' found HWSP value %x for seqno %x\n",
920 name, w->map[offset + 1], w->map[offset]);
928 i915_request_put(rq);
932 static void cleanup_watcher(struct hwsp_watcher *w)
935 switch_tl_lock(NULL, w->rq);
937 i915_request_add(w->rq);
940 i915_vma_unpin_and_release(&w->vma, I915_VMA_RELEASE_MAP);
943 static bool retire_requests(struct intel_timeline *tl)
945 struct i915_request *rq, *rn;
947 mutex_lock(&tl->mutex);
948 list_for_each_entry_safe(rq, rn, &tl->requests, link)
949 if (!i915_request_retire(rq))
951 mutex_unlock(&tl->mutex);
953 return !i915_active_fence_isset(&tl->last_request);
956 static struct i915_request *wrap_timeline(struct i915_request *rq)
958 struct intel_context *ce = rq->context;
959 struct intel_timeline *tl = ce->timeline;
960 u32 seqno = rq->fence.seqno;
962 while (tl->seqno >= seqno) { /* Cause a wrap */
963 i915_request_put(rq);
964 rq = intel_context_create_request(ce);
968 i915_request_get(rq);
969 i915_request_add(rq);
972 i915_request_put(rq);
973 rq = i915_request_create(ce);
977 i915_request_get(rq);
978 i915_request_add(rq);
983 static int live_hwsp_read(void *arg)
985 struct intel_gt *gt = arg;
986 struct hwsp_watcher watcher[2] = {};
987 struct intel_engine_cs *engine;
988 struct intel_timeline *tl;
989 enum intel_engine_id id;
994 * If we take a reference to the HWSP for reading on the GPU, that
995 * read may be arbitrarily delayed (either by foreign fence or
996 * priority saturation) and a wrap can happen within 30 minutes.
997 * When the GPU read is finally submitted it should be correct,
998 * even across multiple wraps.
1001 if (GRAPHICS_VER(gt->i915) < 8) /* CS convenience [SRM/LRM] */
1004 tl = intel_timeline_create(gt);
1008 if (!tl->has_initial_breadcrumb)
1011 selftest_tl_pin(tl);
1013 for (i = 0; i < ARRAY_SIZE(watcher); i++) {
1014 err = setup_watcher(&watcher[i], gt, tl);
1019 for_each_engine(engine, gt, id) {
1020 struct intel_context *ce;
1021 unsigned long count = 0;
1022 IGT_TIMEOUT(end_time);
1024 /* Create a request we can use for remote reading of the HWSP */
1025 err = create_watcher(&watcher[1], engine, SZ_512K);
1030 struct i915_sw_fence *submit;
1031 struct i915_request *rq;
1034 submit = heap_fence_create(GFP_KERNEL);
1040 err = create_watcher(&watcher[0], engine, SZ_4K);
1044 ce = intel_context_create(engine);
1050 ce->timeline = intel_timeline_get(tl);
1052 /* Ensure timeline is mapped, done during first pin */
1053 err = intel_context_pin(ce);
1055 intel_context_put(ce);
1060 * Start at a new wrap, and set seqno right before another wrap,
1061 * saving 30 minutes of nops
1063 tl->seqno = -12u + 2 * (count & 3);
1064 __intel_timeline_get_seqno(tl, &dummy);
1066 rq = i915_request_create(ce);
1069 intel_context_unpin(ce);
1070 intel_context_put(ce);
1074 err = i915_sw_fence_await_dma_fence(&rq->submit,
1075 &watcher[0].rq->fence, 0,
1078 i915_request_add(rq);
1079 intel_context_unpin(ce);
1080 intel_context_put(ce);
1084 switch_tl_lock(rq, watcher[0].rq);
1085 err = intel_timeline_read_hwsp(rq, watcher[0].rq, &hwsp);
1087 err = emit_read_hwsp(watcher[0].rq, /* before */
1088 rq->fence.seqno, hwsp,
1090 switch_tl_lock(watcher[0].rq, rq);
1092 i915_request_add(rq);
1093 intel_context_unpin(ce);
1094 intel_context_put(ce);
1098 switch_tl_lock(rq, watcher[1].rq);
1099 err = intel_timeline_read_hwsp(rq, watcher[1].rq, &hwsp);
1101 err = emit_read_hwsp(watcher[1].rq, /* after */
1102 rq->fence.seqno, hwsp,
1104 switch_tl_lock(watcher[1].rq, rq);
1106 i915_request_add(rq);
1107 intel_context_unpin(ce);
1108 intel_context_put(ce);
1112 i915_request_get(rq);
1113 i915_request_add(rq);
1115 rq = wrap_timeline(rq);
1116 intel_context_unpin(ce);
1117 intel_context_put(ce);
1123 err = i915_sw_fence_await_dma_fence(&watcher[1].rq->submit,
1127 i915_request_put(rq);
1131 err = check_watcher(&watcher[0], "before", cmp_lt);
1132 i915_sw_fence_commit(submit);
1133 heap_fence_put(submit);
1135 i915_request_put(rq);
1140 /* Flush the timeline before manually wrapping again */
1141 if (i915_request_wait(rq,
1142 I915_WAIT_INTERRUPTIBLE,
1145 i915_request_put(rq);
1148 retire_requests(tl);
1149 i915_request_put(rq);
1151 /* Single requests are limited to half a ring at most */
1152 if (8 * watcher[1].rq->ring->emit >
1153 3 * watcher[1].rq->ring->size)
1156 } while (!__igt_timeout(end_time, NULL) &&
1157 count < (PAGE_SIZE / TIMELINE_SEQNO_BYTES - 1) / 2);
1159 pr_info("%s: simulated %lu wraps\n", engine->name, count);
1160 err = check_watcher(&watcher[1], "after", cmp_gte);
1166 for (i = 0; i < ARRAY_SIZE(watcher); i++)
1167 cleanup_watcher(&watcher[i]);
1169 intel_timeline_unpin(tl);
1171 if (igt_flush_test(gt->i915))
1175 intel_timeline_put(tl);
1179 static int live_hwsp_rollover_kernel(void *arg)
1181 struct intel_gt *gt = arg;
1182 struct intel_engine_cs *engine;
1183 enum intel_engine_id id;
1187 * Run the host for long enough, and even the kernel context will
1188 * see a seqno rollover.
1191 for_each_engine(engine, gt, id) {
1192 struct intel_context *ce = engine->kernel_context;
1193 struct intel_timeline *tl = ce->timeline;
1194 struct i915_request *rq[3] = {};
1197 st_engine_heartbeat_disable(engine);
1198 if (intel_gt_wait_for_idle(gt, HZ / 2)) {
1203 GEM_BUG_ON(i915_active_fence_isset(&tl->last_request));
1205 WRITE_ONCE(*(u32 *)tl->hwsp_seqno, tl->seqno);
1207 for (i = 0; i < ARRAY_SIZE(rq); i++) {
1208 struct i915_request *this;
1210 this = i915_request_create(ce);
1212 err = PTR_ERR(this);
1216 pr_debug("%s: create fence.seqnp:%d\n",
1218 lower_32_bits(this->fence.seqno));
1220 GEM_BUG_ON(rcu_access_pointer(this->timeline) != tl);
1222 rq[i] = i915_request_get(this);
1223 i915_request_add(this);
1226 /* We expected a wrap! */
1227 GEM_BUG_ON(rq[2]->fence.seqno > rq[0]->fence.seqno);
1229 if (i915_request_wait(rq[2], 0, HZ / 5) < 0) {
1230 pr_err("Wait for timeline wrap timed out!\n");
1235 for (i = 0; i < ARRAY_SIZE(rq); i++) {
1236 if (!i915_request_completed(rq[i])) {
1237 pr_err("Pre-wrap request not completed!\n");
1244 for (i = 0; i < ARRAY_SIZE(rq); i++)
1245 i915_request_put(rq[i]);
1246 st_engine_heartbeat_enable(engine);
1251 if (igt_flush_test(gt->i915))
1257 static int live_hwsp_rollover_user(void *arg)
1259 struct intel_gt *gt = arg;
1260 struct intel_engine_cs *engine;
1261 enum intel_engine_id id;
1265 * Simulate a long running user context, and force the seqno wrap
1266 * on the user's timeline.
1269 for_each_engine(engine, gt, id) {
1270 struct i915_request *rq[3] = {};
1271 struct intel_timeline *tl;
1272 struct intel_context *ce;
1275 ce = intel_context_create(engine);
1279 err = intel_context_alloc_state(ce);
1284 if (!tl->has_initial_breadcrumb)
1287 err = intel_context_pin(ce);
1292 WRITE_ONCE(*(u32 *)tl->hwsp_seqno, tl->seqno);
1294 for (i = 0; i < ARRAY_SIZE(rq); i++) {
1295 struct i915_request *this;
1297 this = intel_context_create_request(ce);
1299 err = PTR_ERR(this);
1303 pr_debug("%s: create fence.seqnp:%d\n",
1305 lower_32_bits(this->fence.seqno));
1307 GEM_BUG_ON(rcu_access_pointer(this->timeline) != tl);
1309 rq[i] = i915_request_get(this);
1310 i915_request_add(this);
1313 /* We expected a wrap! */
1314 GEM_BUG_ON(rq[2]->fence.seqno > rq[0]->fence.seqno);
1316 if (i915_request_wait(rq[2], 0, HZ / 5) < 0) {
1317 pr_err("Wait for timeline wrap timed out!\n");
1322 for (i = 0; i < ARRAY_SIZE(rq); i++) {
1323 if (!i915_request_completed(rq[i])) {
1324 pr_err("Pre-wrap request not completed!\n");
1330 intel_context_unpin(ce);
1332 for (i = 0; i < ARRAY_SIZE(rq); i++)
1333 i915_request_put(rq[i]);
1334 intel_context_put(ce);
1339 if (igt_flush_test(gt->i915))
1345 static int live_hwsp_recycle(void *arg)
1347 struct intel_gt *gt = arg;
1348 struct intel_engine_cs *engine;
1349 enum intel_engine_id id;
1350 unsigned long count;
1354 * Check seqno writes into one timeline at a time. We expect to
1355 * recycle the breadcrumb slot between iterations and neither
1356 * want to confuse ourselves or the GPU.
1360 for_each_engine(engine, gt, id) {
1361 IGT_TIMEOUT(end_time);
1363 if (!intel_engine_can_store_dword(engine))
1366 intel_engine_pm_get(engine);
1369 struct intel_timeline *tl;
1370 struct i915_request *rq;
1372 tl = intel_timeline_create(gt);
1378 rq = checked_tl_write(tl, engine, count);
1380 intel_timeline_put(tl);
1385 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
1386 pr_err("Wait for timeline writes timed out!\n");
1387 i915_request_put(rq);
1388 intel_timeline_put(tl);
1393 if (READ_ONCE(*tl->hwsp_seqno) != count) {
1394 GEM_TRACE_ERR("Invalid seqno:%lu stored in timeline %llu @ %x found 0x%x\n",
1395 count, tl->fence_context,
1396 tl->hwsp_offset, *tl->hwsp_seqno);
1401 i915_request_put(rq);
1402 intel_timeline_put(tl);
1407 } while (!__igt_timeout(end_time, NULL));
1409 intel_engine_pm_put(engine);
1417 int intel_timeline_live_selftests(struct drm_i915_private *i915)
1419 static const struct i915_subtest tests[] = {
1420 SUBTEST(live_hwsp_recycle),
1421 SUBTEST(live_hwsp_engine),
1422 SUBTEST(live_hwsp_alternate),
1423 SUBTEST(live_hwsp_wrap),
1424 SUBTEST(live_hwsp_read),
1425 SUBTEST(live_hwsp_rollover_kernel),
1426 SUBTEST(live_hwsp_rollover_user),
1429 if (intel_gt_is_wedged(to_gt(i915)))
1432 return intel_gt_live_subtests(tests, to_gt(i915));