2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
25 #include <linux/prime_numbers.h>
27 #include "../i915_selftest.h"
28 #include "i915_random.h"
29 #include "igt_live_test.h"
30 #include "lib_sw_fence.h"
32 #include "mock_context.h"
34 #include "mock_gem_device.h"
36 static int igt_add_request(void *arg)
38 struct drm_i915_private *i915 = arg;
39 struct i915_request *request;
42 /* Basic preliminary test to create a request and let it loose! */
44 mutex_lock(&i915->drm.struct_mutex);
45 request = mock_request(i915->engine[RCS],
51 i915_request_add(request);
55 mutex_unlock(&i915->drm.struct_mutex);
59 static int igt_wait_request(void *arg)
61 const long T = HZ / 4;
62 struct drm_i915_private *i915 = arg;
63 struct i915_request *request;
66 /* Submit a request, then wait upon it */
68 mutex_lock(&i915->drm.struct_mutex);
69 request = mock_request(i915->engine[RCS], i915->kernel_context, T);
75 if (i915_request_wait(request, I915_WAIT_LOCKED, 0) != -ETIME) {
76 pr_err("request wait (busy query) succeeded (expected timeout before submit!)\n");
80 if (i915_request_wait(request, I915_WAIT_LOCKED, T) != -ETIME) {
81 pr_err("request wait succeeded (expected timeout before submit!)\n");
85 if (i915_request_completed(request)) {
86 pr_err("request completed before submit!!\n");
90 i915_request_add(request);
92 if (i915_request_wait(request, I915_WAIT_LOCKED, 0) != -ETIME) {
93 pr_err("request wait (busy query) succeeded (expected timeout after submit!)\n");
97 if (i915_request_completed(request)) {
98 pr_err("request completed immediately!\n");
102 if (i915_request_wait(request, I915_WAIT_LOCKED, T / 2) != -ETIME) {
103 pr_err("request wait succeeded (expected timeout!)\n");
107 if (i915_request_wait(request, I915_WAIT_LOCKED, T) == -ETIME) {
108 pr_err("request wait timed out!\n");
112 if (!i915_request_completed(request)) {
113 pr_err("request not complete after waiting!\n");
117 if (i915_request_wait(request, I915_WAIT_LOCKED, T) == -ETIME) {
118 pr_err("request wait timed out when already complete!\n");
124 mock_device_flush(i915);
125 mutex_unlock(&i915->drm.struct_mutex);
129 static int igt_fence_wait(void *arg)
131 const long T = HZ / 4;
132 struct drm_i915_private *i915 = arg;
133 struct i915_request *request;
136 /* Submit a request, treat it as a fence and wait upon it */
138 mutex_lock(&i915->drm.struct_mutex);
139 request = mock_request(i915->engine[RCS], i915->kernel_context, T);
144 mutex_unlock(&i915->drm.struct_mutex); /* safe as we are single user */
146 if (dma_fence_wait_timeout(&request->fence, false, T) != -ETIME) {
147 pr_err("fence wait success before submit (expected timeout)!\n");
151 mutex_lock(&i915->drm.struct_mutex);
152 i915_request_add(request);
153 mutex_unlock(&i915->drm.struct_mutex);
155 if (dma_fence_is_signaled(&request->fence)) {
156 pr_err("fence signaled immediately!\n");
160 if (dma_fence_wait_timeout(&request->fence, false, T / 2) != -ETIME) {
161 pr_err("fence wait success after submit (expected timeout)!\n");
165 if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
166 pr_err("fence wait timed out (expected success)!\n");
170 if (!dma_fence_is_signaled(&request->fence)) {
171 pr_err("fence unsignaled after waiting!\n");
175 if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
176 pr_err("fence wait timed out when complete (expected success)!\n");
182 mutex_lock(&i915->drm.struct_mutex);
184 mock_device_flush(i915);
185 mutex_unlock(&i915->drm.struct_mutex);
189 static int igt_request_rewind(void *arg)
191 struct drm_i915_private *i915 = arg;
192 struct i915_request *request, *vip;
193 struct i915_gem_context *ctx[2];
196 mutex_lock(&i915->drm.struct_mutex);
197 ctx[0] = mock_context(i915, "A");
198 request = mock_request(i915->engine[RCS], ctx[0], 2 * HZ);
204 i915_request_get(request);
205 i915_request_add(request);
207 ctx[1] = mock_context(i915, "B");
208 vip = mock_request(i915->engine[RCS], ctx[1], 0);
214 /* Simulate preemption by manual reordering */
215 if (!mock_cancel_request(request)) {
216 pr_err("failed to cancel request (already executed)!\n");
217 i915_request_add(vip);
220 i915_request_get(vip);
221 i915_request_add(vip);
223 request->engine->submit_request(request);
226 mutex_unlock(&i915->drm.struct_mutex);
228 if (i915_request_wait(vip, 0, HZ) == -ETIME) {
229 pr_err("timed out waiting for high priority request, vip.seqno=%d, current seqno=%d\n",
230 vip->global_seqno, intel_engine_get_seqno(i915->engine[RCS]));
234 if (i915_request_completed(request)) {
235 pr_err("low priority request already completed\n");
241 i915_request_put(vip);
242 mutex_lock(&i915->drm.struct_mutex);
244 mock_context_close(ctx[1]);
245 i915_request_put(request);
247 mock_context_close(ctx[0]);
248 mock_device_flush(i915);
249 mutex_unlock(&i915->drm.struct_mutex);
254 struct intel_engine_cs *engine;
255 struct i915_gem_context **contexts;
256 atomic_long_t num_waits, num_fences;
257 int ncontexts, max_batch;
258 struct i915_request *(*request_alloc)(struct i915_gem_context *,
259 struct intel_engine_cs *);
262 static struct i915_request *
263 __mock_request_alloc(struct i915_gem_context *ctx,
264 struct intel_engine_cs *engine)
266 return mock_request(engine, ctx, 0);
269 static struct i915_request *
270 __live_request_alloc(struct i915_gem_context *ctx,
271 struct intel_engine_cs *engine)
273 return i915_request_alloc(engine, ctx);
276 static int __igt_breadcrumbs_smoketest(void *arg)
278 struct smoketest *t = arg;
279 struct mutex * const BKL = &t->engine->i915->drm.struct_mutex;
280 const unsigned int max_batch = min(t->ncontexts, t->max_batch) - 1;
281 const unsigned int total = 4 * t->ncontexts + 1;
282 unsigned int num_waits = 0, num_fences = 0;
283 struct i915_request **requests;
284 I915_RND_STATE(prng);
289 * A very simple test to catch the most egregious of list handling bugs.
291 * At its heart, we simply create oodles of requests running across
292 * multiple kthreads and enable signaling on them, for the sole purpose
293 * of stressing our breadcrumb handling. The only inspection we do is
294 * that the fences were marked as signaled.
297 requests = kmalloc_array(total, sizeof(*requests), GFP_KERNEL);
301 order = i915_random_order(total, &prng);
307 while (!kthread_should_stop()) {
308 struct i915_sw_fence *submit, *wait;
309 unsigned int n, count;
311 submit = heap_fence_create(GFP_KERNEL);
317 wait = heap_fence_create(GFP_KERNEL);
319 i915_sw_fence_commit(submit);
320 heap_fence_put(submit);
325 i915_random_reorder(order, total, &prng);
326 count = 1 + i915_prandom_u32_max_state(max_batch, &prng);
328 for (n = 0; n < count; n++) {
329 struct i915_gem_context *ctx =
330 t->contexts[order[n] % t->ncontexts];
331 struct i915_request *rq;
335 rq = t->request_alloc(ctx, t->engine);
343 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
347 requests[n] = i915_request_get(rq);
348 i915_request_add(rq);
353 err = i915_sw_fence_await_dma_fence(wait,
359 i915_request_put(rq);
365 i915_sw_fence_commit(submit);
366 i915_sw_fence_commit(wait);
368 if (!wait_event_timeout(wait->wait,
369 i915_sw_fence_done(wait),
371 struct i915_request *rq = requests[count - 1];
373 pr_err("waiting for %d fences (last %llx:%lld) on %s timed out!\n",
375 rq->fence.context, rq->fence.seqno,
377 i915_gem_set_wedged(t->engine->i915);
378 GEM_BUG_ON(!i915_request_completed(rq));
379 i915_sw_fence_wait(wait);
383 for (n = 0; n < count; n++) {
384 struct i915_request *rq = requests[n];
386 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
388 pr_err("%llu:%llu was not signaled!\n",
389 rq->fence.context, rq->fence.seqno);
393 i915_request_put(rq);
396 heap_fence_put(wait);
397 heap_fence_put(submit);
408 atomic_long_add(num_fences, &t->num_fences);
409 atomic_long_add(num_waits, &t->num_waits);
417 static int mock_breadcrumbs_smoketest(void *arg)
419 struct drm_i915_private *i915 = arg;
420 struct smoketest t = {
421 .engine = i915->engine[RCS],
424 .request_alloc = __mock_request_alloc
426 unsigned int ncpus = num_online_cpus();
427 struct task_struct **threads;
432 * Smoketest our breadcrumb/signal handling for requests across multiple
433 * threads. A very simple test to only catch the most egregious of bugs.
434 * See __igt_breadcrumbs_smoketest();
437 threads = kmalloc_array(ncpus, sizeof(*threads), GFP_KERNEL);
442 kmalloc_array(t.ncontexts, sizeof(*t.contexts), GFP_KERNEL);
448 mutex_lock(&t.engine->i915->drm.struct_mutex);
449 for (n = 0; n < t.ncontexts; n++) {
450 t.contexts[n] = mock_context(t.engine->i915, "mock");
451 if (!t.contexts[n]) {
456 mutex_unlock(&t.engine->i915->drm.struct_mutex);
458 for (n = 0; n < ncpus; n++) {
459 threads[n] = kthread_run(__igt_breadcrumbs_smoketest,
461 if (IS_ERR(threads[n])) {
462 ret = PTR_ERR(threads[n]);
467 get_task_struct(threads[n]);
470 msleep(jiffies_to_msecs(i915_selftest.timeout_jiffies));
472 for (n = 0; n < ncpus; n++) {
475 err = kthread_stop(threads[n]);
479 put_task_struct(threads[n]);
481 pr_info("Completed %lu waits for %lu fence across %d cpus\n",
482 atomic_long_read(&t.num_waits),
483 atomic_long_read(&t.num_fences),
486 mutex_lock(&t.engine->i915->drm.struct_mutex);
488 for (n = 0; n < t.ncontexts; n++) {
491 mock_context_close(t.contexts[n]);
493 mutex_unlock(&t.engine->i915->drm.struct_mutex);
501 int i915_request_mock_selftests(void)
503 static const struct i915_subtest tests[] = {
504 SUBTEST(igt_add_request),
505 SUBTEST(igt_wait_request),
506 SUBTEST(igt_fence_wait),
507 SUBTEST(igt_request_rewind),
508 SUBTEST(mock_breadcrumbs_smoketest),
510 struct drm_i915_private *i915;
511 intel_wakeref_t wakeref;
514 i915 = mock_gem_device();
518 with_intel_runtime_pm(i915, wakeref)
519 err = i915_subtests(tests, i915);
521 drm_dev_put(&i915->drm);
526 static int live_nop_request(void *arg)
528 struct drm_i915_private *i915 = arg;
529 struct intel_engine_cs *engine;
530 intel_wakeref_t wakeref;
531 struct igt_live_test t;
535 /* Submit various sized batches of empty requests, to each engine
536 * (individually), and wait for the batch to complete. We can check
537 * the overhead of submitting requests to the hardware.
540 mutex_lock(&i915->drm.struct_mutex);
541 wakeref = intel_runtime_pm_get(i915);
543 for_each_engine(engine, i915, id) {
544 struct i915_request *request = NULL;
545 unsigned long n, prime;
546 IGT_TIMEOUT(end_time);
547 ktime_t times[2] = {};
549 err = igt_live_test_begin(&t, i915, __func__, engine->name);
553 for_each_prime_number_from(prime, 1, 8192) {
554 times[1] = ktime_get_raw();
556 for (n = 0; n < prime; n++) {
557 request = i915_request_alloc(engine,
558 i915->kernel_context);
559 if (IS_ERR(request)) {
560 err = PTR_ERR(request);
564 /* This space is left intentionally blank.
566 * We do not actually want to perform any
567 * action with this request, we just want
568 * to measure the latency in allocation
569 * and submission of our breadcrumbs -
570 * ensuring that the bare request is sufficient
571 * for the system to work (i.e. proper HEAD
572 * tracking of the rings, interrupt handling,
573 * etc). It also gives us the lowest bounds
577 i915_request_add(request);
579 i915_request_wait(request,
581 MAX_SCHEDULE_TIMEOUT);
583 times[1] = ktime_sub(ktime_get_raw(), times[1]);
587 if (__igt_timeout(end_time, NULL))
591 err = igt_live_test_end(&t);
595 pr_info("Request latencies on %s: 1 = %lluns, %lu = %lluns\n",
597 ktime_to_ns(times[0]),
598 prime, div64_u64(ktime_to_ns(times[1]), prime));
602 intel_runtime_pm_put(i915, wakeref);
603 mutex_unlock(&i915->drm.struct_mutex);
607 static struct i915_vma *empty_batch(struct drm_i915_private *i915)
609 struct drm_i915_gem_object *obj;
610 struct i915_vma *vma;
614 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
616 return ERR_CAST(obj);
618 cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
624 *cmd = MI_BATCH_BUFFER_END;
625 i915_gem_chipset_flush(i915);
627 i915_gem_object_unpin_map(obj);
629 err = i915_gem_object_set_to_gtt_domain(obj, false);
633 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
639 err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_GLOBAL);
646 i915_gem_object_put(obj);
650 static struct i915_request *
651 empty_request(struct intel_engine_cs *engine,
652 struct i915_vma *batch)
654 struct i915_request *request;
657 request = i915_request_alloc(engine, engine->i915->kernel_context);
661 err = engine->emit_bb_start(request,
664 I915_DISPATCH_SECURE);
669 i915_request_add(request);
670 return err ? ERR_PTR(err) : request;
673 static int live_empty_request(void *arg)
675 struct drm_i915_private *i915 = arg;
676 struct intel_engine_cs *engine;
677 intel_wakeref_t wakeref;
678 struct igt_live_test t;
679 struct i915_vma *batch;
683 /* Submit various sized batches of empty requests, to each engine
684 * (individually), and wait for the batch to complete. We can check
685 * the overhead of submitting requests to the hardware.
688 mutex_lock(&i915->drm.struct_mutex);
689 wakeref = intel_runtime_pm_get(i915);
691 batch = empty_batch(i915);
693 err = PTR_ERR(batch);
697 for_each_engine(engine, i915, id) {
698 IGT_TIMEOUT(end_time);
699 struct i915_request *request;
700 unsigned long n, prime;
701 ktime_t times[2] = {};
703 err = igt_live_test_begin(&t, i915, __func__, engine->name);
707 /* Warmup / preload */
708 request = empty_request(engine, batch);
709 if (IS_ERR(request)) {
710 err = PTR_ERR(request);
713 i915_request_wait(request,
715 MAX_SCHEDULE_TIMEOUT);
717 for_each_prime_number_from(prime, 1, 8192) {
718 times[1] = ktime_get_raw();
720 for (n = 0; n < prime; n++) {
721 request = empty_request(engine, batch);
722 if (IS_ERR(request)) {
723 err = PTR_ERR(request);
727 i915_request_wait(request,
729 MAX_SCHEDULE_TIMEOUT);
731 times[1] = ktime_sub(ktime_get_raw(), times[1]);
735 if (__igt_timeout(end_time, NULL))
739 err = igt_live_test_end(&t);
743 pr_info("Batch latencies on %s: 1 = %lluns, %lu = %lluns\n",
745 ktime_to_ns(times[0]),
746 prime, div64_u64(ktime_to_ns(times[1]), prime));
750 i915_vma_unpin(batch);
753 intel_runtime_pm_put(i915, wakeref);
754 mutex_unlock(&i915->drm.struct_mutex);
758 static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
760 struct i915_gem_context *ctx = i915->kernel_context;
761 struct i915_address_space *vm =
762 ctx->ppgtt ? &ctx->ppgtt->vm : &i915->ggtt.vm;
763 struct drm_i915_gem_object *obj;
764 const int gen = INTEL_GEN(i915);
765 struct i915_vma *vma;
769 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
771 return ERR_CAST(obj);
773 vma = i915_vma_instance(obj, vm, NULL);
779 err = i915_vma_pin(vma, 0, 0, PIN_USER);
783 err = i915_gem_object_set_to_wc_domain(obj, true);
787 cmd = i915_gem_object_pin_map(obj, I915_MAP_WC);
794 *cmd++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
795 *cmd++ = lower_32_bits(vma->node.start);
796 *cmd++ = upper_32_bits(vma->node.start);
797 } else if (gen >= 6) {
798 *cmd++ = MI_BATCH_BUFFER_START | 1 << 8;
799 *cmd++ = lower_32_bits(vma->node.start);
801 *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
802 *cmd++ = lower_32_bits(vma->node.start);
804 *cmd++ = MI_BATCH_BUFFER_END; /* terminate early in case of error */
805 i915_gem_chipset_flush(i915);
807 i915_gem_object_unpin_map(obj);
812 i915_gem_object_put(obj);
816 static int recursive_batch_resolve(struct i915_vma *batch)
820 cmd = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
824 *cmd = MI_BATCH_BUFFER_END;
825 i915_gem_chipset_flush(batch->vm->i915);
827 i915_gem_object_unpin_map(batch->obj);
832 static int live_all_engines(void *arg)
834 struct drm_i915_private *i915 = arg;
835 struct intel_engine_cs *engine;
836 struct i915_request *request[I915_NUM_ENGINES];
837 intel_wakeref_t wakeref;
838 struct igt_live_test t;
839 struct i915_vma *batch;
843 /* Check we can submit requests to all engines simultaneously. We
844 * send a recursive batch to each engine - checking that we don't
845 * block doing so, and that they don't complete too soon.
848 mutex_lock(&i915->drm.struct_mutex);
849 wakeref = intel_runtime_pm_get(i915);
851 err = igt_live_test_begin(&t, i915, __func__, "");
855 batch = recursive_batch(i915);
857 err = PTR_ERR(batch);
858 pr_err("%s: Unable to create batch, err=%d\n", __func__, err);
862 for_each_engine(engine, i915, id) {
863 request[id] = i915_request_alloc(engine, i915->kernel_context);
864 if (IS_ERR(request[id])) {
865 err = PTR_ERR(request[id]);
866 pr_err("%s: Request allocation failed with err=%d\n",
871 err = engine->emit_bb_start(request[id],
876 request[id]->batch = batch;
878 if (!i915_gem_object_has_active_reference(batch->obj)) {
879 i915_gem_object_get(batch->obj);
880 i915_gem_object_set_active_reference(batch->obj);
883 err = i915_vma_move_to_active(batch, request[id], 0);
886 i915_request_get(request[id]);
887 i915_request_add(request[id]);
890 for_each_engine(engine, i915, id) {
891 if (i915_request_completed(request[id])) {
892 pr_err("%s(%s): request completed too early!\n",
893 __func__, engine->name);
899 err = recursive_batch_resolve(batch);
901 pr_err("%s: failed to resolve batch, err=%d\n", __func__, err);
905 for_each_engine(engine, i915, id) {
908 timeout = i915_request_wait(request[id],
910 MAX_SCHEDULE_TIMEOUT);
913 pr_err("%s: error waiting for request on %s, err=%d\n",
914 __func__, engine->name, err);
918 GEM_BUG_ON(!i915_request_completed(request[id]));
919 i915_request_put(request[id]);
923 err = igt_live_test_end(&t);
926 for_each_engine(engine, i915, id)
928 i915_request_put(request[id]);
929 i915_vma_unpin(batch);
932 intel_runtime_pm_put(i915, wakeref);
933 mutex_unlock(&i915->drm.struct_mutex);
937 static int live_sequential_engines(void *arg)
939 struct drm_i915_private *i915 = arg;
940 struct i915_request *request[I915_NUM_ENGINES] = {};
941 struct i915_request *prev = NULL;
942 struct intel_engine_cs *engine;
943 intel_wakeref_t wakeref;
944 struct igt_live_test t;
948 /* Check we can submit requests to all engines sequentially, such
949 * that each successive request waits for the earlier ones. This
950 * tests that we don't execute requests out of order, even though
951 * they are running on independent engines.
954 mutex_lock(&i915->drm.struct_mutex);
955 wakeref = intel_runtime_pm_get(i915);
957 err = igt_live_test_begin(&t, i915, __func__, "");
961 for_each_engine(engine, i915, id) {
962 struct i915_vma *batch;
964 batch = recursive_batch(i915);
966 err = PTR_ERR(batch);
967 pr_err("%s: Unable to create batch for %s, err=%d\n",
968 __func__, engine->name, err);
972 request[id] = i915_request_alloc(engine, i915->kernel_context);
973 if (IS_ERR(request[id])) {
974 err = PTR_ERR(request[id]);
975 pr_err("%s: Request allocation failed for %s with err=%d\n",
976 __func__, engine->name, err);
981 err = i915_request_await_dma_fence(request[id],
984 i915_request_add(request[id]);
985 pr_err("%s: Request await failed for %s with err=%d\n",
986 __func__, engine->name, err);
991 err = engine->emit_bb_start(request[id],
996 request[id]->batch = batch;
998 err = i915_vma_move_to_active(batch, request[id], 0);
1001 i915_gem_object_set_active_reference(batch->obj);
1002 i915_vma_get(batch);
1004 i915_request_get(request[id]);
1005 i915_request_add(request[id]);
1010 for_each_engine(engine, i915, id) {
1013 if (i915_request_completed(request[id])) {
1014 pr_err("%s(%s): request completed too early!\n",
1015 __func__, engine->name);
1020 err = recursive_batch_resolve(request[id]->batch);
1022 pr_err("%s: failed to resolve batch, err=%d\n",
1027 timeout = i915_request_wait(request[id],
1029 MAX_SCHEDULE_TIMEOUT);
1032 pr_err("%s: error waiting for request on %s, err=%d\n",
1033 __func__, engine->name, err);
1037 GEM_BUG_ON(!i915_request_completed(request[id]));
1040 err = igt_live_test_end(&t);
1043 for_each_engine(engine, i915, id) {
1049 cmd = i915_gem_object_pin_map(request[id]->batch->obj,
1052 *cmd = MI_BATCH_BUFFER_END;
1053 i915_gem_chipset_flush(i915);
1055 i915_gem_object_unpin_map(request[id]->batch->obj);
1058 i915_vma_put(request[id]->batch);
1059 i915_request_put(request[id]);
1062 intel_runtime_pm_put(i915, wakeref);
1063 mutex_unlock(&i915->drm.struct_mutex);
1068 max_batches(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
1070 struct i915_request *rq;
1074 * Before execlists, all contexts share the same ringbuffer. With
1075 * execlists, each context/engine has a separate ringbuffer and
1076 * for the purposes of this test, inexhaustible.
1078 * For the global ringbuffer though, we have to be very careful
1079 * that we do not wrap while preventing the execution of requests
1080 * with a unsignaled fence.
1082 if (HAS_EXECLISTS(ctx->i915))
1085 rq = i915_request_alloc(engine, ctx);
1091 ret = rq->ring->size - rq->reserved_space;
1092 i915_request_add(rq);
1094 sz = rq->ring->emit - rq->head;
1096 sz += rq->ring->size;
1098 ret /= 2; /* leave half spare, in case of emergency! */
1104 static int live_breadcrumbs_smoketest(void *arg)
1106 struct drm_i915_private *i915 = arg;
1107 struct smoketest t[I915_NUM_ENGINES];
1108 unsigned int ncpus = num_online_cpus();
1109 unsigned long num_waits, num_fences;
1110 struct intel_engine_cs *engine;
1111 struct task_struct **threads;
1112 struct igt_live_test live;
1113 enum intel_engine_id id;
1114 intel_wakeref_t wakeref;
1115 struct drm_file *file;
1120 * Smoketest our breadcrumb/signal handling for requests across multiple
1121 * threads. A very simple test to only catch the most egregious of bugs.
1122 * See __igt_breadcrumbs_smoketest();
1124 * On real hardware this time.
1127 wakeref = intel_runtime_pm_get(i915);
1129 file = mock_file(i915);
1131 ret = PTR_ERR(file);
1135 threads = kcalloc(ncpus * I915_NUM_ENGINES,
1143 memset(&t[0], 0, sizeof(t[0]));
1144 t[0].request_alloc = __live_request_alloc;
1145 t[0].ncontexts = 64;
1146 t[0].contexts = kmalloc_array(t[0].ncontexts,
1147 sizeof(*t[0].contexts),
1149 if (!t[0].contexts) {
1154 mutex_lock(&i915->drm.struct_mutex);
1155 for (n = 0; n < t[0].ncontexts; n++) {
1156 t[0].contexts[n] = live_context(i915, file);
1157 if (!t[0].contexts[n]) {
1163 ret = igt_live_test_begin(&live, i915, __func__, "");
1167 for_each_engine(engine, i915, id) {
1169 t[id].engine = engine;
1170 t[id].max_batch = max_batches(t[0].contexts[0], engine);
1171 if (t[id].max_batch < 0) {
1172 ret = t[id].max_batch;
1173 mutex_unlock(&i915->drm.struct_mutex);
1176 /* One ring interleaved between requests from all cpus */
1177 t[id].max_batch /= num_online_cpus() + 1;
1178 pr_debug("Limiting batches to %d requests on %s\n",
1179 t[id].max_batch, engine->name);
1181 for (n = 0; n < ncpus; n++) {
1182 struct task_struct *tsk;
1184 tsk = kthread_run(__igt_breadcrumbs_smoketest,
1185 &t[id], "igt/%d.%d", id, n);
1188 mutex_unlock(&i915->drm.struct_mutex);
1192 get_task_struct(tsk);
1193 threads[id * ncpus + n] = tsk;
1196 mutex_unlock(&i915->drm.struct_mutex);
1198 msleep(jiffies_to_msecs(i915_selftest.timeout_jiffies));
1203 for_each_engine(engine, i915, id) {
1204 for (n = 0; n < ncpus; n++) {
1205 struct task_struct *tsk = threads[id * ncpus + n];
1211 err = kthread_stop(tsk);
1212 if (err < 0 && !ret)
1215 put_task_struct(tsk);
1218 num_waits += atomic_long_read(&t[id].num_waits);
1219 num_fences += atomic_long_read(&t[id].num_fences);
1221 pr_info("Completed %lu waits for %lu fences across %d engines and %d cpus\n",
1222 num_waits, num_fences, RUNTIME_INFO(i915)->num_rings, ncpus);
1224 mutex_lock(&i915->drm.struct_mutex);
1225 ret = igt_live_test_end(&live) ?: ret;
1227 mutex_unlock(&i915->drm.struct_mutex);
1228 kfree(t[0].contexts);
1232 mock_file_free(i915, file);
1234 intel_runtime_pm_put(i915, wakeref);
1239 int i915_request_live_selftests(struct drm_i915_private *i915)
1241 static const struct i915_subtest tests[] = {
1242 SUBTEST(live_nop_request),
1243 SUBTEST(live_all_engines),
1244 SUBTEST(live_sequential_engines),
1245 SUBTEST(live_empty_request),
1246 SUBTEST(live_breadcrumbs_smoketest),
1249 if (i915_terminally_wedged(&i915->gpu_error))
1252 return i915_subtests(tests, i915);