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"
29 #include "mock_context.h"
30 #include "mock_gem_device.h"
32 static int igt_add_request(void *arg)
34 struct drm_i915_private *i915 = arg;
35 struct i915_request *request;
38 /* Basic preliminary test to create a request and let it loose! */
40 mutex_lock(&i915->drm.struct_mutex);
41 request = mock_request(i915->engine[RCS],
47 i915_request_add(request);
51 mutex_unlock(&i915->drm.struct_mutex);
55 static int igt_wait_request(void *arg)
57 const long T = HZ / 4;
58 struct drm_i915_private *i915 = arg;
59 struct i915_request *request;
62 /* Submit a request, then wait upon it */
64 mutex_lock(&i915->drm.struct_mutex);
65 request = mock_request(i915->engine[RCS], i915->kernel_context, T);
71 if (i915_request_wait(request, I915_WAIT_LOCKED, 0) != -ETIME) {
72 pr_err("request wait (busy query) succeeded (expected timeout before submit!)\n");
76 if (i915_request_wait(request, I915_WAIT_LOCKED, T) != -ETIME) {
77 pr_err("request wait succeeded (expected timeout before submit!)\n");
81 if (i915_request_completed(request)) {
82 pr_err("request completed before submit!!\n");
86 i915_request_add(request);
88 if (i915_request_wait(request, I915_WAIT_LOCKED, 0) != -ETIME) {
89 pr_err("request wait (busy query) succeeded (expected timeout after submit!)\n");
93 if (i915_request_completed(request)) {
94 pr_err("request completed immediately!\n");
98 if (i915_request_wait(request, I915_WAIT_LOCKED, T / 2) != -ETIME) {
99 pr_err("request wait succeeded (expected timeout!)\n");
103 if (i915_request_wait(request, I915_WAIT_LOCKED, T) == -ETIME) {
104 pr_err("request wait timed out!\n");
108 if (!i915_request_completed(request)) {
109 pr_err("request not complete after waiting!\n");
113 if (i915_request_wait(request, I915_WAIT_LOCKED, T) == -ETIME) {
114 pr_err("request wait timed out when already complete!\n");
120 mock_device_flush(i915);
121 mutex_unlock(&i915->drm.struct_mutex);
125 static int igt_fence_wait(void *arg)
127 const long T = HZ / 4;
128 struct drm_i915_private *i915 = arg;
129 struct i915_request *request;
132 /* Submit a request, treat it as a fence and wait upon it */
134 mutex_lock(&i915->drm.struct_mutex);
135 request = mock_request(i915->engine[RCS], i915->kernel_context, T);
140 mutex_unlock(&i915->drm.struct_mutex); /* safe as we are single user */
142 if (dma_fence_wait_timeout(&request->fence, false, T) != -ETIME) {
143 pr_err("fence wait success before submit (expected timeout)!\n");
147 mutex_lock(&i915->drm.struct_mutex);
148 i915_request_add(request);
149 mutex_unlock(&i915->drm.struct_mutex);
151 if (dma_fence_is_signaled(&request->fence)) {
152 pr_err("fence signaled immediately!\n");
156 if (dma_fence_wait_timeout(&request->fence, false, T / 2) != -ETIME) {
157 pr_err("fence wait success after submit (expected timeout)!\n");
161 if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
162 pr_err("fence wait timed out (expected success)!\n");
166 if (!dma_fence_is_signaled(&request->fence)) {
167 pr_err("fence unsignaled after waiting!\n");
171 if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
172 pr_err("fence wait timed out when complete (expected success)!\n");
178 mutex_lock(&i915->drm.struct_mutex);
180 mock_device_flush(i915);
181 mutex_unlock(&i915->drm.struct_mutex);
185 static int igt_request_rewind(void *arg)
187 struct drm_i915_private *i915 = arg;
188 struct i915_request *request, *vip;
189 struct i915_gem_context *ctx[2];
192 mutex_lock(&i915->drm.struct_mutex);
193 ctx[0] = mock_context(i915, "A");
194 request = mock_request(i915->engine[RCS], ctx[0], 2 * HZ);
200 i915_request_get(request);
201 i915_request_add(request);
203 ctx[1] = mock_context(i915, "B");
204 vip = mock_request(i915->engine[RCS], ctx[1], 0);
210 /* Simulate preemption by manual reordering */
211 if (!mock_cancel_request(request)) {
212 pr_err("failed to cancel request (already executed)!\n");
213 i915_request_add(vip);
216 i915_request_get(vip);
217 i915_request_add(vip);
219 request->engine->submit_request(request);
222 mutex_unlock(&i915->drm.struct_mutex);
224 if (i915_request_wait(vip, 0, HZ) == -ETIME) {
225 pr_err("timed out waiting for high priority request, vip.seqno=%d, current seqno=%d\n",
226 vip->global_seqno, intel_engine_get_seqno(i915->engine[RCS]));
230 if (i915_request_completed(request)) {
231 pr_err("low priority request already completed\n");
237 i915_request_put(vip);
238 mutex_lock(&i915->drm.struct_mutex);
240 mock_context_close(ctx[1]);
241 i915_request_put(request);
243 mock_context_close(ctx[0]);
244 mock_device_flush(i915);
245 mutex_unlock(&i915->drm.struct_mutex);
249 int i915_request_mock_selftests(void)
251 static const struct i915_subtest tests[] = {
252 SUBTEST(igt_add_request),
253 SUBTEST(igt_wait_request),
254 SUBTEST(igt_fence_wait),
255 SUBTEST(igt_request_rewind),
257 struct drm_i915_private *i915;
260 i915 = mock_gem_device();
264 err = i915_subtests(tests, i915);
265 drm_dev_unref(&i915->drm);
271 struct drm_i915_private *i915;
275 unsigned int reset_count;
278 static int begin_live_test(struct live_test *t,
279 struct drm_i915_private *i915,
289 err = i915_gem_wait_for_idle(i915, I915_WAIT_LOCKED);
291 pr_err("%s(%s): failed to idle before, with err=%d!",
296 i915->gpu_error.missed_irq_rings = 0;
297 t->reset_count = i915_reset_count(&i915->gpu_error);
302 static int end_live_test(struct live_test *t)
304 struct drm_i915_private *i915 = t->i915;
306 i915_retire_requests(i915);
308 if (wait_for(intel_engines_are_idle(i915), 10)) {
309 pr_err("%s(%s): GPU not idle\n", t->func, t->name);
313 if (t->reset_count != i915_reset_count(&i915->gpu_error)) {
314 pr_err("%s(%s): GPU was reset %d times!\n",
316 i915_reset_count(&i915->gpu_error) - t->reset_count);
320 if (i915->gpu_error.missed_irq_rings) {
321 pr_err("%s(%s): Missed interrupts on engines %lx\n",
322 t->func, t->name, i915->gpu_error.missed_irq_rings);
329 static int live_nop_request(void *arg)
331 struct drm_i915_private *i915 = arg;
332 struct intel_engine_cs *engine;
337 /* Submit various sized batches of empty requests, to each engine
338 * (individually), and wait for the batch to complete. We can check
339 * the overhead of submitting requests to the hardware.
342 mutex_lock(&i915->drm.struct_mutex);
344 for_each_engine(engine, i915, id) {
345 IGT_TIMEOUT(end_time);
346 struct i915_request *request;
347 unsigned long n, prime;
348 ktime_t times[2] = {};
350 err = begin_live_test(&t, i915, __func__, engine->name);
354 for_each_prime_number_from(prime, 1, 8192) {
355 times[1] = ktime_get_raw();
357 for (n = 0; n < prime; n++) {
358 request = i915_request_alloc(engine,
359 i915->kernel_context);
360 if (IS_ERR(request)) {
361 err = PTR_ERR(request);
365 /* This space is left intentionally blank.
367 * We do not actually want to perform any
368 * action with this request, we just want
369 * to measure the latency in allocation
370 * and submission of our breadcrumbs -
371 * ensuring that the bare request is sufficient
372 * for the system to work (i.e. proper HEAD
373 * tracking of the rings, interrupt handling,
374 * etc). It also gives us the lowest bounds
378 i915_request_add(request);
380 i915_request_wait(request,
382 MAX_SCHEDULE_TIMEOUT);
384 times[1] = ktime_sub(ktime_get_raw(), times[1]);
388 if (__igt_timeout(end_time, NULL))
392 err = end_live_test(&t);
396 pr_info("Request latencies on %s: 1 = %lluns, %lu = %lluns\n",
398 ktime_to_ns(times[0]),
399 prime, div64_u64(ktime_to_ns(times[1]), prime));
403 mutex_unlock(&i915->drm.struct_mutex);
407 static struct i915_vma *empty_batch(struct drm_i915_private *i915)
409 struct drm_i915_gem_object *obj;
410 struct i915_vma *vma;
414 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
416 return ERR_CAST(obj);
418 cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
424 *cmd = MI_BATCH_BUFFER_END;
425 i915_gem_chipset_flush(i915);
427 i915_gem_object_unpin_map(obj);
429 err = i915_gem_object_set_to_gtt_domain(obj, false);
433 vma = i915_vma_instance(obj, &i915->ggtt.base, NULL);
439 err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_GLOBAL);
446 i915_gem_object_put(obj);
450 static struct i915_request *
451 empty_request(struct intel_engine_cs *engine,
452 struct i915_vma *batch)
454 struct i915_request *request;
457 request = i915_request_alloc(engine, engine->i915->kernel_context);
461 err = engine->emit_bb_start(request,
464 I915_DISPATCH_SECURE);
469 __i915_request_add(request, err == 0);
470 return err ? ERR_PTR(err) : request;
473 static int live_empty_request(void *arg)
475 struct drm_i915_private *i915 = arg;
476 struct intel_engine_cs *engine;
478 struct i915_vma *batch;
482 /* Submit various sized batches of empty requests, to each engine
483 * (individually), and wait for the batch to complete. We can check
484 * the overhead of submitting requests to the hardware.
487 mutex_lock(&i915->drm.struct_mutex);
489 batch = empty_batch(i915);
491 err = PTR_ERR(batch);
495 for_each_engine(engine, i915, id) {
496 IGT_TIMEOUT(end_time);
497 struct i915_request *request;
498 unsigned long n, prime;
499 ktime_t times[2] = {};
501 err = begin_live_test(&t, i915, __func__, engine->name);
505 /* Warmup / preload */
506 request = empty_request(engine, batch);
507 if (IS_ERR(request)) {
508 err = PTR_ERR(request);
511 i915_request_wait(request,
513 MAX_SCHEDULE_TIMEOUT);
515 for_each_prime_number_from(prime, 1, 8192) {
516 times[1] = ktime_get_raw();
518 for (n = 0; n < prime; n++) {
519 request = empty_request(engine, batch);
520 if (IS_ERR(request)) {
521 err = PTR_ERR(request);
525 i915_request_wait(request,
527 MAX_SCHEDULE_TIMEOUT);
529 times[1] = ktime_sub(ktime_get_raw(), times[1]);
533 if (__igt_timeout(end_time, NULL))
537 err = end_live_test(&t);
541 pr_info("Batch latencies on %s: 1 = %lluns, %lu = %lluns\n",
543 ktime_to_ns(times[0]),
544 prime, div64_u64(ktime_to_ns(times[1]), prime));
548 i915_vma_unpin(batch);
551 mutex_unlock(&i915->drm.struct_mutex);
555 static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
557 struct i915_gem_context *ctx = i915->kernel_context;
558 struct i915_address_space *vm = ctx->ppgtt ? &ctx->ppgtt->base : &i915->ggtt.base;
559 struct drm_i915_gem_object *obj;
560 const int gen = INTEL_GEN(i915);
561 struct i915_vma *vma;
565 obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
567 return ERR_CAST(obj);
569 vma = i915_vma_instance(obj, vm, NULL);
575 err = i915_vma_pin(vma, 0, 0, PIN_USER);
579 err = i915_gem_object_set_to_wc_domain(obj, true);
583 cmd = i915_gem_object_pin_map(obj, I915_MAP_WC);
590 *cmd++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
591 *cmd++ = lower_32_bits(vma->node.start);
592 *cmd++ = upper_32_bits(vma->node.start);
593 } else if (gen >= 6) {
594 *cmd++ = MI_BATCH_BUFFER_START | 1 << 8;
595 *cmd++ = lower_32_bits(vma->node.start);
596 } else if (gen >= 4) {
597 *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
598 *cmd++ = lower_32_bits(vma->node.start);
600 *cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT | 1;
601 *cmd++ = lower_32_bits(vma->node.start);
603 *cmd++ = MI_BATCH_BUFFER_END; /* terminate early in case of error */
604 i915_gem_chipset_flush(i915);
606 i915_gem_object_unpin_map(obj);
611 i915_gem_object_put(obj);
615 static int recursive_batch_resolve(struct i915_vma *batch)
619 cmd = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
623 *cmd = MI_BATCH_BUFFER_END;
624 i915_gem_chipset_flush(batch->vm->i915);
626 i915_gem_object_unpin_map(batch->obj);
631 static int live_all_engines(void *arg)
633 struct drm_i915_private *i915 = arg;
634 struct intel_engine_cs *engine;
635 struct i915_request *request[I915_NUM_ENGINES];
636 struct i915_vma *batch;
641 /* Check we can submit requests to all engines simultaneously. We
642 * send a recursive batch to each engine - checking that we don't
643 * block doing so, and that they don't complete too soon.
646 mutex_lock(&i915->drm.struct_mutex);
648 err = begin_live_test(&t, i915, __func__, "");
652 batch = recursive_batch(i915);
654 err = PTR_ERR(batch);
655 pr_err("%s: Unable to create batch, err=%d\n", __func__, err);
659 for_each_engine(engine, i915, id) {
660 request[id] = i915_request_alloc(engine, i915->kernel_context);
661 if (IS_ERR(request[id])) {
662 err = PTR_ERR(request[id]);
663 pr_err("%s: Request allocation failed with err=%d\n",
668 err = engine->emit_bb_start(request[id],
673 request[id]->batch = batch;
675 if (!i915_gem_object_has_active_reference(batch->obj)) {
676 i915_gem_object_get(batch->obj);
677 i915_gem_object_set_active_reference(batch->obj);
680 i915_vma_move_to_active(batch, request[id], 0);
681 i915_request_get(request[id]);
682 i915_request_add(request[id]);
685 for_each_engine(engine, i915, id) {
686 if (i915_request_completed(request[id])) {
687 pr_err("%s(%s): request completed too early!\n",
688 __func__, engine->name);
694 err = recursive_batch_resolve(batch);
696 pr_err("%s: failed to resolve batch, err=%d\n", __func__, err);
700 for_each_engine(engine, i915, id) {
703 timeout = i915_request_wait(request[id],
705 MAX_SCHEDULE_TIMEOUT);
708 pr_err("%s: error waiting for request on %s, err=%d\n",
709 __func__, engine->name, err);
713 GEM_BUG_ON(!i915_request_completed(request[id]));
714 i915_request_put(request[id]);
718 err = end_live_test(&t);
721 for_each_engine(engine, i915, id)
723 i915_request_put(request[id]);
724 i915_vma_unpin(batch);
727 mutex_unlock(&i915->drm.struct_mutex);
731 static int live_sequential_engines(void *arg)
733 struct drm_i915_private *i915 = arg;
734 struct i915_request *request[I915_NUM_ENGINES] = {};
735 struct i915_request *prev = NULL;
736 struct intel_engine_cs *engine;
741 /* Check we can submit requests to all engines sequentially, such
742 * that each successive request waits for the earlier ones. This
743 * tests that we don't execute requests out of order, even though
744 * they are running on independent engines.
747 mutex_lock(&i915->drm.struct_mutex);
749 err = begin_live_test(&t, i915, __func__, "");
753 for_each_engine(engine, i915, id) {
754 struct i915_vma *batch;
756 batch = recursive_batch(i915);
758 err = PTR_ERR(batch);
759 pr_err("%s: Unable to create batch for %s, err=%d\n",
760 __func__, engine->name, err);
764 request[id] = i915_request_alloc(engine, i915->kernel_context);
765 if (IS_ERR(request[id])) {
766 err = PTR_ERR(request[id]);
767 pr_err("%s: Request allocation failed for %s with err=%d\n",
768 __func__, engine->name, err);
773 err = i915_request_await_dma_fence(request[id],
776 i915_request_add(request[id]);
777 pr_err("%s: Request await failed for %s with err=%d\n",
778 __func__, engine->name, err);
783 err = engine->emit_bb_start(request[id],
788 request[id]->batch = batch;
790 i915_vma_move_to_active(batch, request[id], 0);
791 i915_gem_object_set_active_reference(batch->obj);
794 i915_request_get(request[id]);
795 i915_request_add(request[id]);
800 for_each_engine(engine, i915, id) {
803 if (i915_request_completed(request[id])) {
804 pr_err("%s(%s): request completed too early!\n",
805 __func__, engine->name);
810 err = recursive_batch_resolve(request[id]->batch);
812 pr_err("%s: failed to resolve batch, err=%d\n",
817 timeout = i915_request_wait(request[id],
819 MAX_SCHEDULE_TIMEOUT);
822 pr_err("%s: error waiting for request on %s, err=%d\n",
823 __func__, engine->name, err);
827 GEM_BUG_ON(!i915_request_completed(request[id]));
830 err = end_live_test(&t);
833 for_each_engine(engine, i915, id) {
839 cmd = i915_gem_object_pin_map(request[id]->batch->obj,
842 *cmd = MI_BATCH_BUFFER_END;
843 i915_gem_chipset_flush(i915);
845 i915_gem_object_unpin_map(request[id]->batch->obj);
848 i915_vma_put(request[id]->batch);
849 i915_request_put(request[id]);
852 mutex_unlock(&i915->drm.struct_mutex);
856 int i915_request_live_selftests(struct drm_i915_private *i915)
858 static const struct i915_subtest tests[] = {
859 SUBTEST(live_nop_request),
860 SUBTEST(live_all_engines),
861 SUBTEST(live_sequential_engines),
862 SUBTEST(live_empty_request),
864 return i915_subtests(tests, i915);