2 * SPDX-License-Identifier: MIT
4 * Copyright © 2017-2018 Intel Corporation
7 #include <linux/prime_numbers.h>
9 #include "intel_context.h"
10 #include "intel_engine_heartbeat.h"
11 #include "intel_engine_pm.h"
13 #include "intel_gt_requests.h"
14 #include "intel_ring.h"
15 #include "selftest_engine_heartbeat.h"
17 #include "../selftests/i915_random.h"
18 #include "../i915_selftest.h"
20 #include "../selftests/igt_flush_test.h"
21 #include "../selftests/mock_gem_device.h"
22 #include "selftests/mock_timeline.h"
24 static struct page *hwsp_page(struct intel_timeline *tl)
26 struct drm_i915_gem_object *obj = tl->hwsp_ggtt->obj;
28 GEM_BUG_ON(!i915_gem_object_has_pinned_pages(obj));
29 return sg_page(obj->mm.pages->sgl);
32 static unsigned long hwsp_cacheline(struct intel_timeline *tl)
34 unsigned long address = (unsigned long)page_address(hwsp_page(tl));
36 return (address + tl->hwsp_offset) / CACHELINE_BYTES;
39 #define CACHELINES_PER_PAGE (PAGE_SIZE / CACHELINE_BYTES)
41 struct mock_hwsp_freelist {
43 struct radix_tree_root cachelines;
44 struct intel_timeline **history;
45 unsigned long count, max;
46 struct rnd_state prng;
53 static void __mock_hwsp_record(struct mock_hwsp_freelist *state,
55 struct intel_timeline *tl)
57 tl = xchg(&state->history[idx], tl);
59 radix_tree_delete(&state->cachelines, hwsp_cacheline(tl));
60 intel_timeline_put(tl);
64 static int __mock_hwsp_timeline(struct mock_hwsp_freelist *state,
68 struct intel_timeline *tl;
72 unsigned long cacheline;
75 tl = intel_timeline_create(state->gt, NULL);
79 cacheline = hwsp_cacheline(tl);
80 err = radix_tree_insert(&state->cachelines, cacheline, tl);
83 pr_err("HWSP cacheline %lu already used; duplicate allocation!\n",
86 intel_timeline_put(tl);
90 idx = state->count++ % state->max;
91 __mock_hwsp_record(state, idx, tl);
95 i915_prandom_shuffle(state->history,
96 sizeof(*state->history),
97 min(state->count, state->max),
100 count = i915_prandom_u32_max_state(min(state->count, state->max),
103 idx = --state->count % state->max;
104 __mock_hwsp_record(state, idx, NULL);
110 static int mock_hwsp_freelist(void *arg)
112 struct mock_hwsp_freelist state;
113 struct drm_i915_private *i915;
119 { "shuffled", SHUFFLE },
125 i915 = mock_gem_device();
129 INIT_RADIX_TREE(&state.cachelines, GFP_KERNEL);
130 state.prng = I915_RND_STATE_INITIALIZER(i915_selftest.random_seed);
132 state.gt = &i915->gt;
135 * Create a bunch of timelines and check that their HWSP do not overlap.
136 * Free some, and try again.
139 state.max = PAGE_SIZE / sizeof(*state.history);
141 state.history = kcalloc(state.max, sizeof(*state.history), GFP_KERNEL);
142 if (!state.history) {
147 for (p = phases; p->name; p++) {
148 pr_debug("%s(%s)\n", __func__, p->name);
149 for_each_prime_number_from(na, 1, 2 * CACHELINES_PER_PAGE) {
150 err = __mock_hwsp_timeline(&state, na, p->flags);
157 for (na = 0; na < state.max; na++)
158 __mock_hwsp_record(&state, na, NULL);
159 kfree(state.history);
161 drm_dev_put(&i915->drm);
172 static int __igt_sync(struct intel_timeline *tl,
174 const struct __igt_sync *p,
179 if (__intel_timeline_sync_is_later(tl, ctx, p->seqno) != p->expected) {
180 pr_err("%s: %s(ctx=%llu, seqno=%u) expected passed %s but failed\n",
181 name, p->name, ctx, p->seqno, yesno(p->expected));
186 ret = __intel_timeline_sync_set(tl, ctx, p->seqno);
194 static int igt_sync(void *arg)
196 const struct __igt_sync pass[] = {
197 { "unset", 0, false, false },
198 { "new", 0, false, true },
199 { "0a", 0, true, true },
200 { "1a", 1, false, true },
201 { "1b", 1, true, true },
202 { "0b", 0, true, false },
203 { "2a", 2, false, true },
204 { "4", 4, false, true },
205 { "INT_MAX", INT_MAX, false, true },
206 { "INT_MAX-1", INT_MAX-1, true, false },
207 { "INT_MAX+1", (u32)INT_MAX+1, false, true },
208 { "INT_MAX", INT_MAX, true, false },
209 { "UINT_MAX", UINT_MAX, false, true },
210 { "wrap", 0, false, true },
211 { "unwrap", UINT_MAX, true, false },
214 struct intel_timeline tl;
218 mock_timeline_init(&tl, 0);
219 for (p = pass; p->name; p++) {
220 for (order = 1; order < 64; order++) {
221 for (offset = -1; offset <= (order > 1); offset++) {
222 u64 ctx = BIT_ULL(order) + offset;
224 ret = __igt_sync(&tl, ctx, p, "1");
230 mock_timeline_fini(&tl);
232 mock_timeline_init(&tl, 0);
233 for (order = 1; order < 64; order++) {
234 for (offset = -1; offset <= (order > 1); offset++) {
235 u64 ctx = BIT_ULL(order) + offset;
237 for (p = pass; p->name; p++) {
238 ret = __igt_sync(&tl, ctx, p, "2");
246 mock_timeline_fini(&tl);
250 static unsigned int random_engine(struct rnd_state *rnd)
252 return i915_prandom_u32_max_state(I915_NUM_ENGINES, rnd);
255 static int bench_sync(void *arg)
257 struct rnd_state prng;
258 struct intel_timeline tl;
259 unsigned long end_time, count;
262 int order, last_order;
264 mock_timeline_init(&tl, 0);
266 /* Lookups from cache are very fast and so the random number generation
267 * and the loop itself becomes a significant factor in the per-iteration
268 * timings. We try to compensate the results by measuring the overhead
269 * of the prng and subtract it from the reported results.
271 prandom_seed_state(&prng, i915_selftest.random_seed);
274 end_time = jiffies + HZ/10;
278 /* Make sure the compiler doesn't optimise away the prng call */
279 WRITE_ONCE(x, prandom_u32_state(&prng));
282 } while (!time_after(jiffies, end_time));
283 kt = ktime_sub(ktime_get(), kt);
284 pr_debug("%s: %lu random evaluations, %lluns/prng\n",
285 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
286 prng32_1M = div64_ul(ktime_to_ns(kt) << 20, count);
288 /* Benchmark (only) setting random context ids */
289 prandom_seed_state(&prng, i915_selftest.random_seed);
292 end_time = jiffies + HZ/10;
294 u64 id = i915_prandom_u64_state(&prng);
296 __intel_timeline_sync_set(&tl, id, 0);
298 } while (!time_after(jiffies, end_time));
299 kt = ktime_sub(ktime_get(), kt);
300 kt = ktime_sub_ns(kt, (count * prng32_1M * 2) >> 20);
301 pr_info("%s: %lu random insertions, %lluns/insert\n",
302 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
304 /* Benchmark looking up the exact same context ids as we just set */
305 prandom_seed_state(&prng, i915_selftest.random_seed);
309 u64 id = i915_prandom_u64_state(&prng);
311 if (!__intel_timeline_sync_is_later(&tl, id, 0)) {
312 mock_timeline_fini(&tl);
313 pr_err("Lookup of %llu failed\n", id);
317 kt = ktime_sub(ktime_get(), kt);
318 kt = ktime_sub_ns(kt, (count * prng32_1M * 2) >> 20);
319 pr_info("%s: %lu random lookups, %lluns/lookup\n",
320 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
322 mock_timeline_fini(&tl);
325 mock_timeline_init(&tl, 0);
327 /* Benchmark setting the first N (in order) contexts */
330 end_time = jiffies + HZ/10;
332 __intel_timeline_sync_set(&tl, count++, 0);
333 } while (!time_after(jiffies, end_time));
334 kt = ktime_sub(ktime_get(), kt);
335 pr_info("%s: %lu in-order insertions, %lluns/insert\n",
336 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
338 /* Benchmark looking up the exact same context ids as we just set */
342 if (!__intel_timeline_sync_is_later(&tl, end_time, 0)) {
343 pr_err("Lookup of %lu failed\n", end_time);
344 mock_timeline_fini(&tl);
348 kt = ktime_sub(ktime_get(), kt);
349 pr_info("%s: %lu in-order lookups, %lluns/lookup\n",
350 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
352 mock_timeline_fini(&tl);
355 mock_timeline_init(&tl, 0);
357 /* Benchmark searching for a random context id and maybe changing it */
358 prandom_seed_state(&prng, i915_selftest.random_seed);
361 end_time = jiffies + HZ/10;
363 u32 id = random_engine(&prng);
364 u32 seqno = prandom_u32_state(&prng);
366 if (!__intel_timeline_sync_is_later(&tl, id, seqno))
367 __intel_timeline_sync_set(&tl, id, seqno);
370 } while (!time_after(jiffies, end_time));
371 kt = ktime_sub(ktime_get(), kt);
372 kt = ktime_sub_ns(kt, (count * prng32_1M * 2) >> 20);
373 pr_info("%s: %lu repeated insert/lookups, %lluns/op\n",
374 __func__, count, (long long)div64_ul(ktime_to_ns(kt), count));
375 mock_timeline_fini(&tl);
378 /* Benchmark searching for a known context id and changing the seqno */
379 for (last_order = 1, order = 1; order < 32;
380 ({ int tmp = last_order; last_order = order; order += tmp; })) {
381 unsigned int mask = BIT(order) - 1;
383 mock_timeline_init(&tl, 0);
387 end_time = jiffies + HZ/10;
389 /* Without assuming too many details of the underlying
390 * implementation, try to identify its phase-changes
393 u64 id = (u64)(count & mask) << order;
395 __intel_timeline_sync_is_later(&tl, id, 0);
396 __intel_timeline_sync_set(&tl, id, 0);
399 } while (!time_after(jiffies, end_time));
400 kt = ktime_sub(ktime_get(), kt);
401 pr_info("%s: %lu cyclic/%d insert/lookups, %lluns/op\n",
402 __func__, count, order,
403 (long long)div64_ul(ktime_to_ns(kt), count));
404 mock_timeline_fini(&tl);
411 int intel_timeline_mock_selftests(void)
413 static const struct i915_subtest tests[] = {
414 SUBTEST(mock_hwsp_freelist),
419 return i915_subtests(tests, NULL);
422 static int emit_ggtt_store_dw(struct i915_request *rq, u32 addr, u32 value)
426 cs = intel_ring_begin(rq, 4);
430 if (INTEL_GEN(rq->engine->i915) >= 8) {
431 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
435 } else if (INTEL_GEN(rq->engine->i915) >= 4) {
436 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
441 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
447 intel_ring_advance(rq, cs);
452 static struct i915_request *
453 tl_write(struct intel_timeline *tl, struct intel_engine_cs *engine, u32 value)
455 struct i915_request *rq;
458 err = intel_timeline_pin(tl);
464 rq = intel_engine_create_kernel_request(engine);
468 i915_request_get(rq);
470 err = emit_ggtt_store_dw(rq, tl->hwsp_offset, value);
471 i915_request_add(rq);
473 i915_request_put(rq);
478 intel_timeline_unpin(tl);
481 pr_err("Failed to write to timeline!\n");
485 static struct intel_timeline *
486 checked_intel_timeline_create(struct intel_gt *gt)
488 struct intel_timeline *tl;
490 tl = intel_timeline_create(gt, NULL);
494 if (*tl->hwsp_seqno != tl->seqno) {
495 pr_err("Timeline created with incorrect breadcrumb, found %x, expected %x\n",
496 *tl->hwsp_seqno, tl->seqno);
497 intel_timeline_put(tl);
498 return ERR_PTR(-EINVAL);
504 static int live_hwsp_engine(void *arg)
506 #define NUM_TIMELINES 4096
507 struct intel_gt *gt = arg;
508 struct intel_timeline **timelines;
509 struct intel_engine_cs *engine;
510 enum intel_engine_id id;
511 unsigned long count, n;
515 * Create a bunch of timelines and check we can write
516 * independently to each of their breadcrumb slots.
519 timelines = kvmalloc_array(NUM_TIMELINES * I915_NUM_ENGINES,
526 for_each_engine(engine, gt, id) {
527 if (!intel_engine_can_store_dword(engine))
530 intel_engine_pm_get(engine);
532 for (n = 0; n < NUM_TIMELINES; n++) {
533 struct intel_timeline *tl;
534 struct i915_request *rq;
536 tl = checked_intel_timeline_create(gt);
542 rq = tl_write(tl, engine, count);
544 intel_timeline_put(tl);
549 timelines[count++] = tl;
550 i915_request_put(rq);
553 intel_engine_pm_put(engine);
558 if (igt_flush_test(gt->i915))
561 for (n = 0; n < count; n++) {
562 struct intel_timeline *tl = timelines[n];
564 if (!err && *tl->hwsp_seqno != n) {
565 pr_err("Invalid seqno stored in timeline %lu @ %x, found 0x%x\n",
566 n, tl->hwsp_offset, *tl->hwsp_seqno);
570 intel_timeline_put(tl);
578 static int live_hwsp_alternate(void *arg)
580 #define NUM_TIMELINES 4096
581 struct intel_gt *gt = arg;
582 struct intel_timeline **timelines;
583 struct intel_engine_cs *engine;
584 enum intel_engine_id id;
585 unsigned long count, n;
589 * Create a bunch of timelines and check we can write
590 * independently to each of their breadcrumb slots with adjacent
594 timelines = kvmalloc_array(NUM_TIMELINES * I915_NUM_ENGINES,
601 for (n = 0; n < NUM_TIMELINES; n++) {
602 for_each_engine(engine, gt, id) {
603 struct intel_timeline *tl;
604 struct i915_request *rq;
606 if (!intel_engine_can_store_dword(engine))
609 tl = checked_intel_timeline_create(gt);
615 intel_engine_pm_get(engine);
616 rq = tl_write(tl, engine, count);
617 intel_engine_pm_put(engine);
619 intel_timeline_put(tl);
624 timelines[count++] = tl;
625 i915_request_put(rq);
630 if (igt_flush_test(gt->i915))
633 for (n = 0; n < count; n++) {
634 struct intel_timeline *tl = timelines[n];
636 if (!err && *tl->hwsp_seqno != n) {
637 pr_err("Invalid seqno stored in timeline %lu @ %x, found 0x%x\n",
638 n, tl->hwsp_offset, *tl->hwsp_seqno);
642 intel_timeline_put(tl);
650 static int live_hwsp_wrap(void *arg)
652 struct intel_gt *gt = arg;
653 struct intel_engine_cs *engine;
654 struct intel_timeline *tl;
655 enum intel_engine_id id;
659 * Across a seqno wrap, we need to keep the old cacheline alive for
660 * foreign GPU references.
663 tl = intel_timeline_create(gt, NULL);
667 if (!tl->has_initial_breadcrumb || !tl->hwsp_cacheline)
670 err = intel_timeline_pin(tl);
674 for_each_engine(engine, gt, id) {
675 const u32 *hwsp_seqno[2];
676 struct i915_request *rq;
679 if (!intel_engine_can_store_dword(engine))
682 rq = intel_engine_create_kernel_request(engine);
690 mutex_lock_nested(&tl->mutex, SINGLE_DEPTH_NESTING);
691 err = intel_timeline_get_seqno(tl, rq, &seqno[0]);
692 mutex_unlock(&tl->mutex);
694 i915_request_add(rq);
697 pr_debug("seqno[0]:%08x, hwsp_offset:%08x\n",
698 seqno[0], tl->hwsp_offset);
700 err = emit_ggtt_store_dw(rq, tl->hwsp_offset, seqno[0]);
702 i915_request_add(rq);
705 hwsp_seqno[0] = tl->hwsp_seqno;
707 mutex_lock_nested(&tl->mutex, SINGLE_DEPTH_NESTING);
708 err = intel_timeline_get_seqno(tl, rq, &seqno[1]);
709 mutex_unlock(&tl->mutex);
711 i915_request_add(rq);
714 pr_debug("seqno[1]:%08x, hwsp_offset:%08x\n",
715 seqno[1], tl->hwsp_offset);
717 err = emit_ggtt_store_dw(rq, tl->hwsp_offset, seqno[1]);
719 i915_request_add(rq);
722 hwsp_seqno[1] = tl->hwsp_seqno;
724 /* With wrap should come a new hwsp */
725 GEM_BUG_ON(seqno[1] >= seqno[0]);
726 GEM_BUG_ON(hwsp_seqno[0] == hwsp_seqno[1]);
728 i915_request_add(rq);
730 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
731 pr_err("Wait for timeline writes timed out!\n");
736 if (*hwsp_seqno[0] != seqno[0] || *hwsp_seqno[1] != seqno[1]) {
737 pr_err("Bad timeline values: found (%x, %x), expected (%x, %x)\n",
738 *hwsp_seqno[0], *hwsp_seqno[1],
744 intel_gt_retire_requests(gt); /* recycle HWSP */
748 if (igt_flush_test(gt->i915))
751 intel_timeline_unpin(tl);
753 intel_timeline_put(tl);
757 static int live_hwsp_rollover_kernel(void *arg)
759 struct intel_gt *gt = arg;
760 struct intel_engine_cs *engine;
761 enum intel_engine_id id;
765 * Run the host for long enough, and even the kernel context will
766 * see a seqno rollover.
769 for_each_engine(engine, gt, id) {
770 struct intel_context *ce = engine->kernel_context;
771 struct intel_timeline *tl = ce->timeline;
772 struct i915_request *rq[3] = {};
775 st_engine_heartbeat_disable(engine);
776 if (intel_gt_wait_for_idle(gt, HZ / 2)) {
781 GEM_BUG_ON(i915_active_fence_isset(&tl->last_request));
783 timeline_rollback(tl);
784 timeline_rollback(tl);
785 WRITE_ONCE(*(u32 *)tl->hwsp_seqno, tl->seqno);
787 for (i = 0; i < ARRAY_SIZE(rq); i++) {
788 struct i915_request *this;
790 this = i915_request_create(ce);
796 pr_debug("%s: create fence.seqnp:%d\n",
798 lower_32_bits(this->fence.seqno));
800 GEM_BUG_ON(rcu_access_pointer(this->timeline) != tl);
802 rq[i] = i915_request_get(this);
803 i915_request_add(this);
806 /* We expected a wrap! */
807 GEM_BUG_ON(rq[2]->fence.seqno > rq[0]->fence.seqno);
809 if (i915_request_wait(rq[2], 0, HZ / 5) < 0) {
810 pr_err("Wait for timeline wrap timed out!\n");
815 for (i = 0; i < ARRAY_SIZE(rq); i++) {
816 if (!i915_request_completed(rq[i])) {
817 pr_err("Pre-wrap request not completed!\n");
824 for (i = 0; i < ARRAY_SIZE(rq); i++)
825 i915_request_put(rq[i]);
826 st_engine_heartbeat_enable(engine);
831 if (igt_flush_test(gt->i915))
837 static int live_hwsp_rollover_user(void *arg)
839 struct intel_gt *gt = arg;
840 struct intel_engine_cs *engine;
841 enum intel_engine_id id;
845 * Simulate a long running user context, and force the seqno wrap
846 * on the user's timeline.
849 for_each_engine(engine, gt, id) {
850 struct i915_request *rq[3] = {};
851 struct intel_timeline *tl;
852 struct intel_context *ce;
855 ce = intel_context_create(engine);
859 err = intel_context_alloc_state(ce);
864 if (!tl->has_initial_breadcrumb || !tl->hwsp_cacheline)
867 timeline_rollback(tl);
868 timeline_rollback(tl);
869 WRITE_ONCE(*(u32 *)tl->hwsp_seqno, tl->seqno);
871 for (i = 0; i < ARRAY_SIZE(rq); i++) {
872 struct i915_request *this;
874 this = intel_context_create_request(ce);
880 pr_debug("%s: create fence.seqnp:%d\n",
882 lower_32_bits(this->fence.seqno));
884 GEM_BUG_ON(rcu_access_pointer(this->timeline) != tl);
886 rq[i] = i915_request_get(this);
887 i915_request_add(this);
890 /* We expected a wrap! */
891 GEM_BUG_ON(rq[2]->fence.seqno > rq[0]->fence.seqno);
893 if (i915_request_wait(rq[2], 0, HZ / 5) < 0) {
894 pr_err("Wait for timeline wrap timed out!\n");
899 for (i = 0; i < ARRAY_SIZE(rq); i++) {
900 if (!i915_request_completed(rq[i])) {
901 pr_err("Pre-wrap request not completed!\n");
908 for (i = 0; i < ARRAY_SIZE(rq); i++)
909 i915_request_put(rq[i]);
910 intel_context_put(ce);
915 if (igt_flush_test(gt->i915))
921 static int live_hwsp_recycle(void *arg)
923 struct intel_gt *gt = arg;
924 struct intel_engine_cs *engine;
925 enum intel_engine_id id;
930 * Check seqno writes into one timeline at a time. We expect to
931 * recycle the breadcrumb slot between iterations and neither
932 * want to confuse ourselves or the GPU.
936 for_each_engine(engine, gt, id) {
937 IGT_TIMEOUT(end_time);
939 if (!intel_engine_can_store_dword(engine))
942 intel_engine_pm_get(engine);
945 struct intel_timeline *tl;
946 struct i915_request *rq;
948 tl = checked_intel_timeline_create(gt);
954 rq = tl_write(tl, engine, count);
956 intel_timeline_put(tl);
961 if (i915_request_wait(rq, 0, HZ / 5) < 0) {
962 pr_err("Wait for timeline writes timed out!\n");
963 i915_request_put(rq);
964 intel_timeline_put(tl);
969 if (*tl->hwsp_seqno != count) {
970 pr_err("Invalid seqno stored in timeline %lu @ tl->hwsp_offset, found 0x%x\n",
971 count, *tl->hwsp_seqno);
976 i915_request_put(rq);
977 intel_timeline_put(tl);
982 } while (!__igt_timeout(end_time, NULL));
984 intel_engine_pm_put(engine);
992 int intel_timeline_live_selftests(struct drm_i915_private *i915)
994 static const struct i915_subtest tests[] = {
995 SUBTEST(live_hwsp_recycle),
996 SUBTEST(live_hwsp_engine),
997 SUBTEST(live_hwsp_alternate),
998 SUBTEST(live_hwsp_wrap),
999 SUBTEST(live_hwsp_rollover_kernel),
1000 SUBTEST(live_hwsp_rollover_user),
1003 if (intel_gt_is_wedged(&i915->gt))
1006 return intel_gt_live_subtests(tests, &i915->gt);