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 "gem/i915_gem_pm.h"
28 #include "gem/selftests/mock_context.h"
30 #include "i915_random.h"
31 #include "i915_selftest.h"
32 #include "igt_live_test.h"
33 #include "lib_sw_fence.h"
36 #include "mock_gem_device.h"
38 static int igt_add_request(void *arg)
40 struct drm_i915_private *i915 = arg;
41 struct i915_request *request;
44 /* Basic preliminary test to create a request and let it loose! */
46 mutex_lock(&i915->drm.struct_mutex);
47 request = mock_request(i915->engine[RCS0],
53 i915_request_add(request);
57 mutex_unlock(&i915->drm.struct_mutex);
61 static int igt_wait_request(void *arg)
63 const long T = HZ / 4;
64 struct drm_i915_private *i915 = arg;
65 struct i915_request *request;
68 /* Submit a request, then wait upon it */
70 mutex_lock(&i915->drm.struct_mutex);
71 request = mock_request(i915->engine[RCS0], i915->kernel_context, T);
77 if (i915_request_wait(request, 0, 0) != -ETIME) {
78 pr_err("request wait (busy query) succeeded (expected timeout before submit!)\n");
82 if (i915_request_wait(request, 0, T) != -ETIME) {
83 pr_err("request wait succeeded (expected timeout before submit!)\n");
87 if (i915_request_completed(request)) {
88 pr_err("request completed before submit!!\n");
92 i915_request_add(request);
94 if (i915_request_wait(request, 0, 0) != -ETIME) {
95 pr_err("request wait (busy query) succeeded (expected timeout after submit!)\n");
99 if (i915_request_completed(request)) {
100 pr_err("request completed immediately!\n");
104 if (i915_request_wait(request, 0, T / 2) != -ETIME) {
105 pr_err("request wait succeeded (expected timeout!)\n");
109 if (i915_request_wait(request, 0, T) == -ETIME) {
110 pr_err("request wait timed out!\n");
114 if (!i915_request_completed(request)) {
115 pr_err("request not complete after waiting!\n");
119 if (i915_request_wait(request, 0, T) == -ETIME) {
120 pr_err("request wait timed out when already complete!\n");
126 mock_device_flush(i915);
127 mutex_unlock(&i915->drm.struct_mutex);
131 static int igt_fence_wait(void *arg)
133 const long T = HZ / 4;
134 struct drm_i915_private *i915 = arg;
135 struct i915_request *request;
138 /* Submit a request, treat it as a fence and wait upon it */
140 mutex_lock(&i915->drm.struct_mutex);
141 request = mock_request(i915->engine[RCS0], i915->kernel_context, T);
147 if (dma_fence_wait_timeout(&request->fence, false, T) != -ETIME) {
148 pr_err("fence wait success before submit (expected timeout)!\n");
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[RCS0], 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[RCS0], 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\n");
233 if (i915_request_completed(request)) {
234 pr_err("low priority request already completed\n");
240 i915_request_put(vip);
241 mutex_lock(&i915->drm.struct_mutex);
243 mock_context_close(ctx[1]);
244 i915_request_put(request);
246 mock_context_close(ctx[0]);
247 mock_device_flush(i915);
248 mutex_unlock(&i915->drm.struct_mutex);
253 struct intel_engine_cs *engine;
254 struct i915_gem_context **contexts;
255 atomic_long_t num_waits, num_fences;
256 int ncontexts, max_batch;
257 struct i915_request *(*request_alloc)(struct i915_gem_context *,
258 struct intel_engine_cs *);
261 static struct i915_request *
262 __mock_request_alloc(struct i915_gem_context *ctx,
263 struct intel_engine_cs *engine)
265 return mock_request(engine, ctx, 0);
268 static struct i915_request *
269 __live_request_alloc(struct i915_gem_context *ctx,
270 struct intel_engine_cs *engine)
272 return igt_request_alloc(ctx, engine);
275 static int __igt_breadcrumbs_smoketest(void *arg)
277 struct smoketest *t = arg;
278 struct mutex * const BKL = &t->engine->i915->drm.struct_mutex;
279 const unsigned int max_batch = min(t->ncontexts, t->max_batch) - 1;
280 const unsigned int total = 4 * t->ncontexts + 1;
281 unsigned int num_waits = 0, num_fences = 0;
282 struct i915_request **requests;
283 I915_RND_STATE(prng);
288 * A very simple test to catch the most egregious of list handling bugs.
290 * At its heart, we simply create oodles of requests running across
291 * multiple kthreads and enable signaling on them, for the sole purpose
292 * of stressing our breadcrumb handling. The only inspection we do is
293 * that the fences were marked as signaled.
296 requests = kmalloc_array(total, sizeof(*requests), GFP_KERNEL);
300 order = i915_random_order(total, &prng);
306 while (!kthread_should_stop()) {
307 struct i915_sw_fence *submit, *wait;
308 unsigned int n, count;
310 submit = heap_fence_create(GFP_KERNEL);
316 wait = heap_fence_create(GFP_KERNEL);
318 i915_sw_fence_commit(submit);
319 heap_fence_put(submit);
324 i915_random_reorder(order, total, &prng);
325 count = 1 + i915_prandom_u32_max_state(max_batch, &prng);
327 for (n = 0; n < count; n++) {
328 struct i915_gem_context *ctx =
329 t->contexts[order[n] % t->ncontexts];
330 struct i915_request *rq;
334 rq = t->request_alloc(ctx, t->engine);
342 err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
346 requests[n] = i915_request_get(rq);
347 i915_request_add(rq);
352 err = i915_sw_fence_await_dma_fence(wait,
358 i915_request_put(rq);
364 i915_sw_fence_commit(submit);
365 i915_sw_fence_commit(wait);
367 if (!wait_event_timeout(wait->wait,
368 i915_sw_fence_done(wait),
370 struct i915_request *rq = requests[count - 1];
372 pr_err("waiting for %d fences (last %llx:%lld) on %s timed out!\n",
374 rq->fence.context, rq->fence.seqno,
376 i915_gem_set_wedged(t->engine->i915);
377 GEM_BUG_ON(!i915_request_completed(rq));
378 i915_sw_fence_wait(wait);
382 for (n = 0; n < count; n++) {
383 struct i915_request *rq = requests[n];
385 if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
387 pr_err("%llu:%llu was not signaled!\n",
388 rq->fence.context, rq->fence.seqno);
392 i915_request_put(rq);
395 heap_fence_put(wait);
396 heap_fence_put(submit);
407 atomic_long_add(num_fences, &t->num_fences);
408 atomic_long_add(num_waits, &t->num_waits);
416 static int mock_breadcrumbs_smoketest(void *arg)
418 struct drm_i915_private *i915 = arg;
419 struct smoketest t = {
420 .engine = i915->engine[RCS0],
423 .request_alloc = __mock_request_alloc
425 unsigned int ncpus = num_online_cpus();
426 struct task_struct **threads;
431 * Smoketest our breadcrumb/signal handling for requests across multiple
432 * threads. A very simple test to only catch the most egregious of bugs.
433 * See __igt_breadcrumbs_smoketest();
436 threads = kmalloc_array(ncpus, sizeof(*threads), GFP_KERNEL);
441 kmalloc_array(t.ncontexts, sizeof(*t.contexts), GFP_KERNEL);
447 mutex_lock(&t.engine->i915->drm.struct_mutex);
448 for (n = 0; n < t.ncontexts; n++) {
449 t.contexts[n] = mock_context(t.engine->i915, "mock");
450 if (!t.contexts[n]) {
455 mutex_unlock(&t.engine->i915->drm.struct_mutex);
457 for (n = 0; n < ncpus; n++) {
458 threads[n] = kthread_run(__igt_breadcrumbs_smoketest,
460 if (IS_ERR(threads[n])) {
461 ret = PTR_ERR(threads[n]);
466 get_task_struct(threads[n]);
469 msleep(jiffies_to_msecs(i915_selftest.timeout_jiffies));
471 for (n = 0; n < ncpus; n++) {
474 err = kthread_stop(threads[n]);
478 put_task_struct(threads[n]);
480 pr_info("Completed %lu waits for %lu fence across %d cpus\n",
481 atomic_long_read(&t.num_waits),
482 atomic_long_read(&t.num_fences),
485 mutex_lock(&t.engine->i915->drm.struct_mutex);
487 for (n = 0; n < t.ncontexts; n++) {
490 mock_context_close(t.contexts[n]);
492 mutex_unlock(&t.engine->i915->drm.struct_mutex);
500 int i915_request_mock_selftests(void)
502 static const struct i915_subtest tests[] = {
503 SUBTEST(igt_add_request),
504 SUBTEST(igt_wait_request),
505 SUBTEST(igt_fence_wait),
506 SUBTEST(igt_request_rewind),
507 SUBTEST(mock_breadcrumbs_smoketest),
509 struct drm_i915_private *i915;
510 intel_wakeref_t wakeref;
513 i915 = mock_gem_device();
517 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
518 err = i915_subtests(tests, i915);
520 drm_dev_put(&i915->drm);
525 static int live_nop_request(void *arg)
527 struct drm_i915_private *i915 = arg;
528 struct intel_engine_cs *engine;
529 intel_wakeref_t wakeref;
530 struct igt_live_test t;
534 /* Submit various sized batches of empty requests, to each engine
535 * (individually), and wait for the batch to complete. We can check
536 * the overhead of submitting requests to the hardware.
539 mutex_lock(&i915->drm.struct_mutex);
540 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
542 for_each_engine(engine, i915, id) {
543 struct i915_request *request = NULL;
544 unsigned long n, prime;
545 IGT_TIMEOUT(end_time);
546 ktime_t times[2] = {};
548 err = igt_live_test_begin(&t, i915, __func__, engine->name);
552 for_each_prime_number_from(prime, 1, 8192) {
553 times[1] = ktime_get_raw();
555 for (n = 0; n < prime; n++) {
556 request = i915_request_create(engine->kernel_context);
557 if (IS_ERR(request)) {
558 err = PTR_ERR(request);
562 /* This space is left intentionally blank.
564 * We do not actually want to perform any
565 * action with this request, we just want
566 * to measure the latency in allocation
567 * and submission of our breadcrumbs -
568 * ensuring that the bare request is sufficient
569 * for the system to work (i.e. proper HEAD
570 * tracking of the rings, interrupt handling,
571 * etc). It also gives us the lowest bounds
575 i915_request_add(request);
577 i915_request_wait(request, 0, MAX_SCHEDULE_TIMEOUT);
579 times[1] = ktime_sub(ktime_get_raw(), times[1]);
583 if (__igt_timeout(end_time, NULL))
587 err = igt_live_test_end(&t);
591 pr_info("Request latencies on %s: 1 = %lluns, %lu = %lluns\n",
593 ktime_to_ns(times[0]),
594 prime, div64_u64(ktime_to_ns(times[1]), prime));
598 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
599 mutex_unlock(&i915->drm.struct_mutex);
603 static struct i915_vma *empty_batch(struct drm_i915_private *i915)
605 struct drm_i915_gem_object *obj;
606 struct i915_vma *vma;
610 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
612 return ERR_CAST(obj);
614 cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
620 *cmd = MI_BATCH_BUFFER_END;
622 __i915_gem_object_flush_map(obj, 0, 64);
623 i915_gem_object_unpin_map(obj);
625 i915_gem_chipset_flush(i915);
627 vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
633 err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_GLOBAL);
640 i915_gem_object_put(obj);
644 static struct i915_request *
645 empty_request(struct intel_engine_cs *engine,
646 struct i915_vma *batch)
648 struct i915_request *request;
651 request = i915_request_create(engine->kernel_context);
655 err = engine->emit_bb_start(request,
658 I915_DISPATCH_SECURE);
663 i915_request_add(request);
664 return err ? ERR_PTR(err) : request;
667 static int live_empty_request(void *arg)
669 struct drm_i915_private *i915 = arg;
670 struct intel_engine_cs *engine;
671 intel_wakeref_t wakeref;
672 struct igt_live_test t;
673 struct i915_vma *batch;
677 /* Submit various sized batches of empty requests, to each engine
678 * (individually), and wait for the batch to complete. We can check
679 * the overhead of submitting requests to the hardware.
682 mutex_lock(&i915->drm.struct_mutex);
683 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
685 batch = empty_batch(i915);
687 err = PTR_ERR(batch);
691 for_each_engine(engine, i915, id) {
692 IGT_TIMEOUT(end_time);
693 struct i915_request *request;
694 unsigned long n, prime;
695 ktime_t times[2] = {};
697 err = igt_live_test_begin(&t, i915, __func__, engine->name);
701 /* Warmup / preload */
702 request = empty_request(engine, batch);
703 if (IS_ERR(request)) {
704 err = PTR_ERR(request);
707 i915_request_wait(request, 0, MAX_SCHEDULE_TIMEOUT);
709 for_each_prime_number_from(prime, 1, 8192) {
710 times[1] = ktime_get_raw();
712 for (n = 0; n < prime; n++) {
713 request = empty_request(engine, batch);
714 if (IS_ERR(request)) {
715 err = PTR_ERR(request);
719 i915_request_wait(request, 0, MAX_SCHEDULE_TIMEOUT);
721 times[1] = ktime_sub(ktime_get_raw(), times[1]);
725 if (__igt_timeout(end_time, NULL))
729 err = igt_live_test_end(&t);
733 pr_info("Batch latencies on %s: 1 = %lluns, %lu = %lluns\n",
735 ktime_to_ns(times[0]),
736 prime, div64_u64(ktime_to_ns(times[1]), prime));
740 i915_vma_unpin(batch);
743 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
744 mutex_unlock(&i915->drm.struct_mutex);
748 static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
750 struct i915_gem_context *ctx = i915->kernel_context;
751 struct i915_address_space *vm = ctx->vm ?: &i915->ggtt.vm;
752 struct drm_i915_gem_object *obj;
753 const int gen = INTEL_GEN(i915);
754 struct i915_vma *vma;
758 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
760 return ERR_CAST(obj);
762 vma = i915_vma_instance(obj, vm, NULL);
768 err = i915_vma_pin(vma, 0, 0, PIN_USER);
772 cmd = i915_gem_object_pin_map(obj, I915_MAP_WC);
779 *cmd++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
780 *cmd++ = lower_32_bits(vma->node.start);
781 *cmd++ = upper_32_bits(vma->node.start);
782 } else if (gen >= 6) {
783 *cmd++ = MI_BATCH_BUFFER_START | 1 << 8;
784 *cmd++ = lower_32_bits(vma->node.start);
786 *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
787 *cmd++ = lower_32_bits(vma->node.start);
789 *cmd++ = MI_BATCH_BUFFER_END; /* terminate early in case of error */
791 __i915_gem_object_flush_map(obj, 0, 64);
792 i915_gem_object_unpin_map(obj);
794 i915_gem_chipset_flush(i915);
799 i915_gem_object_put(obj);
803 static int recursive_batch_resolve(struct i915_vma *batch)
807 cmd = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
811 *cmd = MI_BATCH_BUFFER_END;
812 i915_gem_chipset_flush(batch->vm->i915);
814 i915_gem_object_unpin_map(batch->obj);
819 static int live_all_engines(void *arg)
821 struct drm_i915_private *i915 = arg;
822 struct intel_engine_cs *engine;
823 struct i915_request *request[I915_NUM_ENGINES];
824 intel_wakeref_t wakeref;
825 struct igt_live_test t;
826 struct i915_vma *batch;
830 /* Check we can submit requests to all engines simultaneously. We
831 * send a recursive batch to each engine - checking that we don't
832 * block doing so, and that they don't complete too soon.
835 mutex_lock(&i915->drm.struct_mutex);
836 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
838 err = igt_live_test_begin(&t, i915, __func__, "");
842 batch = recursive_batch(i915);
844 err = PTR_ERR(batch);
845 pr_err("%s: Unable to create batch, err=%d\n", __func__, err);
849 for_each_engine(engine, i915, id) {
850 request[id] = i915_request_create(engine->kernel_context);
851 if (IS_ERR(request[id])) {
852 err = PTR_ERR(request[id]);
853 pr_err("%s: Request allocation failed with err=%d\n",
858 err = engine->emit_bb_start(request[id],
863 request[id]->batch = batch;
865 i915_vma_lock(batch);
866 err = i915_vma_move_to_active(batch, request[id], 0);
867 i915_vma_unlock(batch);
870 i915_request_get(request[id]);
871 i915_request_add(request[id]);
874 for_each_engine(engine, i915, id) {
875 if (i915_request_completed(request[id])) {
876 pr_err("%s(%s): request completed too early!\n",
877 __func__, engine->name);
883 err = recursive_batch_resolve(batch);
885 pr_err("%s: failed to resolve batch, err=%d\n", __func__, err);
889 for_each_engine(engine, i915, id) {
892 timeout = i915_request_wait(request[id], 0,
893 MAX_SCHEDULE_TIMEOUT);
896 pr_err("%s: error waiting for request on %s, err=%d\n",
897 __func__, engine->name, err);
901 GEM_BUG_ON(!i915_request_completed(request[id]));
902 i915_request_put(request[id]);
906 err = igt_live_test_end(&t);
909 for_each_engine(engine, i915, id)
911 i915_request_put(request[id]);
912 i915_vma_unpin(batch);
915 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
916 mutex_unlock(&i915->drm.struct_mutex);
920 static int live_sequential_engines(void *arg)
922 struct drm_i915_private *i915 = arg;
923 struct i915_request *request[I915_NUM_ENGINES] = {};
924 struct i915_request *prev = NULL;
925 struct intel_engine_cs *engine;
926 intel_wakeref_t wakeref;
927 struct igt_live_test t;
931 /* Check we can submit requests to all engines sequentially, such
932 * that each successive request waits for the earlier ones. This
933 * tests that we don't execute requests out of order, even though
934 * they are running on independent engines.
937 mutex_lock(&i915->drm.struct_mutex);
938 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
940 err = igt_live_test_begin(&t, i915, __func__, "");
944 for_each_engine(engine, i915, id) {
945 struct i915_vma *batch;
947 batch = recursive_batch(i915);
949 err = PTR_ERR(batch);
950 pr_err("%s: Unable to create batch for %s, err=%d\n",
951 __func__, engine->name, err);
955 request[id] = i915_request_create(engine->kernel_context);
956 if (IS_ERR(request[id])) {
957 err = PTR_ERR(request[id]);
958 pr_err("%s: Request allocation failed for %s with err=%d\n",
959 __func__, engine->name, err);
964 err = i915_request_await_dma_fence(request[id],
967 i915_request_add(request[id]);
968 pr_err("%s: Request await failed for %s with err=%d\n",
969 __func__, engine->name, err);
974 err = engine->emit_bb_start(request[id],
979 request[id]->batch = batch;
981 i915_vma_lock(batch);
982 err = i915_vma_move_to_active(batch, request[id], 0);
983 i915_vma_unlock(batch);
986 i915_request_get(request[id]);
987 i915_request_add(request[id]);
992 for_each_engine(engine, i915, id) {
995 if (i915_request_completed(request[id])) {
996 pr_err("%s(%s): request completed too early!\n",
997 __func__, engine->name);
1002 err = recursive_batch_resolve(request[id]->batch);
1004 pr_err("%s: failed to resolve batch, err=%d\n",
1009 timeout = i915_request_wait(request[id], 0,
1010 MAX_SCHEDULE_TIMEOUT);
1013 pr_err("%s: error waiting for request on %s, err=%d\n",
1014 __func__, engine->name, err);
1018 GEM_BUG_ON(!i915_request_completed(request[id]));
1021 err = igt_live_test_end(&t);
1024 for_each_engine(engine, i915, id) {
1030 cmd = i915_gem_object_pin_map(request[id]->batch->obj,
1033 *cmd = MI_BATCH_BUFFER_END;
1034 i915_gem_chipset_flush(i915);
1036 i915_gem_object_unpin_map(request[id]->batch->obj);
1039 i915_vma_put(request[id]->batch);
1040 i915_request_put(request[id]);
1043 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1044 mutex_unlock(&i915->drm.struct_mutex);
1049 max_batches(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
1051 struct i915_request *rq;
1055 * Before execlists, all contexts share the same ringbuffer. With
1056 * execlists, each context/engine has a separate ringbuffer and
1057 * for the purposes of this test, inexhaustible.
1059 * For the global ringbuffer though, we have to be very careful
1060 * that we do not wrap while preventing the execution of requests
1061 * with a unsignaled fence.
1063 if (HAS_EXECLISTS(ctx->i915))
1066 rq = igt_request_alloc(ctx, engine);
1072 ret = rq->ring->size - rq->reserved_space;
1073 i915_request_add(rq);
1075 sz = rq->ring->emit - rq->head;
1077 sz += rq->ring->size;
1079 ret /= 2; /* leave half spare, in case of emergency! */
1085 static int live_breadcrumbs_smoketest(void *arg)
1087 struct drm_i915_private *i915 = arg;
1088 struct smoketest t[I915_NUM_ENGINES];
1089 unsigned int ncpus = num_online_cpus();
1090 unsigned long num_waits, num_fences;
1091 struct intel_engine_cs *engine;
1092 struct task_struct **threads;
1093 struct igt_live_test live;
1094 enum intel_engine_id id;
1095 intel_wakeref_t wakeref;
1096 struct drm_file *file;
1101 * Smoketest our breadcrumb/signal handling for requests across multiple
1102 * threads. A very simple test to only catch the most egregious of bugs.
1103 * See __igt_breadcrumbs_smoketest();
1105 * On real hardware this time.
1108 wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1110 file = mock_file(i915);
1112 ret = PTR_ERR(file);
1116 threads = kcalloc(ncpus * I915_NUM_ENGINES,
1124 memset(&t[0], 0, sizeof(t[0]));
1125 t[0].request_alloc = __live_request_alloc;
1126 t[0].ncontexts = 64;
1127 t[0].contexts = kmalloc_array(t[0].ncontexts,
1128 sizeof(*t[0].contexts),
1130 if (!t[0].contexts) {
1135 mutex_lock(&i915->drm.struct_mutex);
1136 for (n = 0; n < t[0].ncontexts; n++) {
1137 t[0].contexts[n] = live_context(i915, file);
1138 if (!t[0].contexts[n]) {
1144 ret = igt_live_test_begin(&live, i915, __func__, "");
1148 for_each_engine(engine, i915, id) {
1150 t[id].engine = engine;
1151 t[id].max_batch = max_batches(t[0].contexts[0], engine);
1152 if (t[id].max_batch < 0) {
1153 ret = t[id].max_batch;
1154 mutex_unlock(&i915->drm.struct_mutex);
1157 /* One ring interleaved between requests from all cpus */
1158 t[id].max_batch /= num_online_cpus() + 1;
1159 pr_debug("Limiting batches to %d requests on %s\n",
1160 t[id].max_batch, engine->name);
1162 for (n = 0; n < ncpus; n++) {
1163 struct task_struct *tsk;
1165 tsk = kthread_run(__igt_breadcrumbs_smoketest,
1166 &t[id], "igt/%d.%d", id, n);
1169 mutex_unlock(&i915->drm.struct_mutex);
1173 get_task_struct(tsk);
1174 threads[id * ncpus + n] = tsk;
1177 mutex_unlock(&i915->drm.struct_mutex);
1179 msleep(jiffies_to_msecs(i915_selftest.timeout_jiffies));
1184 for_each_engine(engine, i915, id) {
1185 for (n = 0; n < ncpus; n++) {
1186 struct task_struct *tsk = threads[id * ncpus + n];
1192 err = kthread_stop(tsk);
1193 if (err < 0 && !ret)
1196 put_task_struct(tsk);
1199 num_waits += atomic_long_read(&t[id].num_waits);
1200 num_fences += atomic_long_read(&t[id].num_fences);
1202 pr_info("Completed %lu waits for %lu fences across %d engines and %d cpus\n",
1203 num_waits, num_fences, RUNTIME_INFO(i915)->num_engines, ncpus);
1205 mutex_lock(&i915->drm.struct_mutex);
1206 ret = igt_live_test_end(&live) ?: ret;
1208 mutex_unlock(&i915->drm.struct_mutex);
1209 kfree(t[0].contexts);
1213 mock_file_free(i915, file);
1215 intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1220 int i915_request_live_selftests(struct drm_i915_private *i915)
1222 static const struct i915_subtest tests[] = {
1223 SUBTEST(live_nop_request),
1224 SUBTEST(live_all_engines),
1225 SUBTEST(live_sequential_engines),
1226 SUBTEST(live_empty_request),
1227 SUBTEST(live_breadcrumbs_smoketest),
1230 if (i915_terminally_wedged(i915))
1233 return i915_subtests(tests, i915);