i915_request.c 32.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
/*
 * Copyright © 2016 Intel Corporation
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 * IN THE SOFTWARE.
 *
 */

25 26
#include <linux/prime_numbers.h>

27 28 29
#include "gem/i915_gem_pm.h"
#include "gem/selftests/mock_context.h"

30 31
#include "gt/intel_gt.h"

32
#include "i915_random.h"
33
#include "i915_selftest.h"
34
#include "igt_live_test.h"
35
#include "igt_spinner.h"
36
#include "lib_sw_fence.h"
37

38
#include "mock_drm.h"
39 40
#include "mock_gem_device.h"

41 42 43 44 45 46 47 48 49 50 51 52
static unsigned int num_uabi_engines(struct drm_i915_private *i915)
{
	struct intel_engine_cs *engine;
	unsigned int count;

	count = 0;
	for_each_uabi_engine(engine, i915)
		count++;

	return count;
}

53 54 55
static int igt_add_request(void *arg)
{
	struct drm_i915_private *i915 = arg;
56
	struct i915_request *request;
57 58 59

	/* Basic preliminary test to create a request and let it loose! */

60
	request = mock_request(i915->engine[RCS0]->kernel_context, HZ / 10);
61
	if (!request)
62
		return -ENOMEM;
63

64
	i915_request_add(request);
65

66
	return 0;
67 68
}

69 70 71 72
static int igt_wait_request(void *arg)
{
	const long T = HZ / 4;
	struct drm_i915_private *i915 = arg;
73
	struct i915_request *request;
74 75 76 77
	int err = -EINVAL;

	/* Submit a request, then wait upon it */

78
	request = mock_request(i915->engine[RCS0]->kernel_context, T);
79 80 81
	if (!request)
		return -ENOMEM;

82
	i915_request_get(request);
83

84
	if (i915_request_wait(request, 0, 0) != -ETIME) {
85
		pr_err("request wait (busy query) succeeded (expected timeout before submit!)\n");
86
		goto out_request;
87 88
	}

89
	if (i915_request_wait(request, 0, T) != -ETIME) {
90
		pr_err("request wait succeeded (expected timeout before submit!)\n");
91
		goto out_request;
92 93
	}

94
	if (i915_request_completed(request)) {
95
		pr_err("request completed before submit!!\n");
96
		goto out_request;
97 98
	}

99
	i915_request_add(request);
100

101
	if (i915_request_wait(request, 0, 0) != -ETIME) {
102
		pr_err("request wait (busy query) succeeded (expected timeout after submit!)\n");
103
		goto out_request;
104 105
	}

106
	if (i915_request_completed(request)) {
107
		pr_err("request completed immediately!\n");
108
		goto out_request;
109 110
	}

111
	if (i915_request_wait(request, 0, T / 2) != -ETIME) {
112
		pr_err("request wait succeeded (expected timeout!)\n");
113
		goto out_request;
114 115
	}

116
	if (i915_request_wait(request, 0, T) == -ETIME) {
117
		pr_err("request wait timed out!\n");
118
		goto out_request;
119 120
	}

121
	if (!i915_request_completed(request)) {
122
		pr_err("request not complete after waiting!\n");
123
		goto out_request;
124 125
	}

126
	if (i915_request_wait(request, 0, T) == -ETIME) {
127
		pr_err("request wait timed out when already complete!\n");
128
		goto out_request;
129 130 131
	}

	err = 0;
132 133
out_request:
	i915_request_put(request);
134 135 136 137
	mock_device_flush(i915);
	return err;
}

138 139 140 141
static int igt_fence_wait(void *arg)
{
	const long T = HZ / 4;
	struct drm_i915_private *i915 = arg;
142
	struct i915_request *request;
143 144 145 146
	int err = -EINVAL;

	/* Submit a request, treat it as a fence and wait upon it */

147
	request = mock_request(i915->engine[RCS0]->kernel_context, T);
148 149
	if (!request)
		return -ENOMEM;
150 151 152

	if (dma_fence_wait_timeout(&request->fence, false, T) != -ETIME) {
		pr_err("fence wait success before submit (expected timeout)!\n");
153
		goto out;
154 155
	}

156
	i915_request_add(request);
157 158 159

	if (dma_fence_is_signaled(&request->fence)) {
		pr_err("fence signaled immediately!\n");
160
		goto out;
161 162 163 164
	}

	if (dma_fence_wait_timeout(&request->fence, false, T / 2) != -ETIME) {
		pr_err("fence wait success after submit (expected timeout)!\n");
165
		goto out;
166 167 168 169
	}

	if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
		pr_err("fence wait timed out (expected success)!\n");
170
		goto out;
171 172 173 174
	}

	if (!dma_fence_is_signaled(&request->fence)) {
		pr_err("fence unsignaled after waiting!\n");
175
		goto out;
176 177 178 179
	}

	if (dma_fence_wait_timeout(&request->fence, false, T) <= 0) {
		pr_err("fence wait timed out when complete (expected success)!\n");
180
		goto out;
181 182 183
	}

	err = 0;
184
out:
185 186 187 188
	mock_device_flush(i915);
	return err;
}

189 190 191
static int igt_request_rewind(void *arg)
{
	struct drm_i915_private *i915 = arg;
192
	struct i915_request *request, *vip;
193
	struct i915_gem_context *ctx[2];
194
	struct intel_context *ce;
195 196 197
	int err = -EINVAL;

	ctx[0] = mock_context(i915, "A");
198

199 200 201 202
	ce = i915_gem_context_get_engine(ctx[0], RCS0);
	GEM_BUG_ON(IS_ERR(ce));
	request = mock_request(ce, 2 * HZ);
	intel_context_put(ce);
203 204 205 206 207
	if (!request) {
		err = -ENOMEM;
		goto err_context_0;
	}

208 209
	i915_request_get(request);
	i915_request_add(request);
210 211

	ctx[1] = mock_context(i915, "B");
212

213 214 215 216
	ce = i915_gem_context_get_engine(ctx[1], RCS0);
	GEM_BUG_ON(IS_ERR(ce));
	vip = mock_request(ce, 0);
	intel_context_put(ce);
217 218 219 220 221 222 223 224
	if (!vip) {
		err = -ENOMEM;
		goto err_context_1;
	}

	/* Simulate preemption by manual reordering */
	if (!mock_cancel_request(request)) {
		pr_err("failed to cancel request (already executed)!\n");
225
		i915_request_add(vip);
226 227
		goto err_context_1;
	}
228 229
	i915_request_get(vip);
	i915_request_add(vip);
230
	rcu_read_lock();
231
	request->engine->submit_request(request);
232
	rcu_read_unlock();
233 234


235
	if (i915_request_wait(vip, 0, HZ) == -ETIME) {
236
		pr_err("timed out waiting for high priority request\n");
237 238 239
		goto err;
	}

240
	if (i915_request_completed(request)) {
241 242 243 244 245 246
		pr_err("low priority request already completed\n");
		goto err;
	}

	err = 0;
err:
247
	i915_request_put(vip);
248 249
err_context_1:
	mock_context_close(ctx[1]);
250
	i915_request_put(request);
251 252 253 254 255 256
err_context_0:
	mock_context_close(ctx[0]);
	mock_device_flush(i915);
	return err;
}

257 258 259 260 261
struct smoketest {
	struct intel_engine_cs *engine;
	struct i915_gem_context **contexts;
	atomic_long_t num_waits, num_fences;
	int ncontexts, max_batch;
262
	struct i915_request *(*request_alloc)(struct intel_context *ce);
263 264 265
};

static struct i915_request *
266
__mock_request_alloc(struct intel_context *ce)
267
{
268
	return mock_request(ce, 0);
269 270 271
}

static struct i915_request *
272
__live_request_alloc(struct intel_context *ce)
273
{
274
	return intel_context_create_request(ce);
275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
}

static int __igt_breadcrumbs_smoketest(void *arg)
{
	struct smoketest *t = arg;
	const unsigned int max_batch = min(t->ncontexts, t->max_batch) - 1;
	const unsigned int total = 4 * t->ncontexts + 1;
	unsigned int num_waits = 0, num_fences = 0;
	struct i915_request **requests;
	I915_RND_STATE(prng);
	unsigned int *order;
	int err = 0;

	/*
	 * A very simple test to catch the most egregious of list handling bugs.
	 *
	 * At its heart, we simply create oodles of requests running across
	 * multiple kthreads and enable signaling on them, for the sole purpose
	 * of stressing our breadcrumb handling. The only inspection we do is
	 * that the fences were marked as signaled.
	 */

297
	requests = kcalloc(total, sizeof(*requests), GFP_KERNEL);
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	if (!requests)
		return -ENOMEM;

	order = i915_random_order(total, &prng);
	if (!order) {
		err = -ENOMEM;
		goto out_requests;
	}

	while (!kthread_should_stop()) {
		struct i915_sw_fence *submit, *wait;
		unsigned int n, count;

		submit = heap_fence_create(GFP_KERNEL);
		if (!submit) {
			err = -ENOMEM;
			break;
		}

		wait = heap_fence_create(GFP_KERNEL);
		if (!wait) {
			i915_sw_fence_commit(submit);
			heap_fence_put(submit);
			err = ENOMEM;
			break;
		}

		i915_random_reorder(order, total, &prng);
		count = 1 + i915_prandom_u32_max_state(max_batch, &prng);

		for (n = 0; n < count; n++) {
			struct i915_gem_context *ctx =
				t->contexts[order[n] % t->ncontexts];
			struct i915_request *rq;
332
			struct intel_context *ce;
333

334
			ce = i915_gem_context_get_engine(ctx, t->engine->legacy_idx);
335 336 337
			GEM_BUG_ON(IS_ERR(ce));
			rq = t->request_alloc(ce);
			intel_context_put(ce);
338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368
			if (IS_ERR(rq)) {
				err = PTR_ERR(rq);
				count = n;
				break;
			}

			err = i915_sw_fence_await_sw_fence_gfp(&rq->submit,
							       submit,
							       GFP_KERNEL);

			requests[n] = i915_request_get(rq);
			i915_request_add(rq);

			if (err >= 0)
				err = i915_sw_fence_await_dma_fence(wait,
								    &rq->fence,
								    0,
								    GFP_KERNEL);

			if (err < 0) {
				i915_request_put(rq);
				count = n;
				break;
			}
		}

		i915_sw_fence_commit(submit);
		i915_sw_fence_commit(wait);

		if (!wait_event_timeout(wait->wait,
					i915_sw_fence_done(wait),
369
					5 * HZ)) {
370 371
			struct i915_request *rq = requests[count - 1];

372 373
			pr_err("waiting for %d/%d fences (last %llx:%lld) on %s timed out!\n",
			       atomic_read(&wait->pending), count,
374 375
			       rq->fence.context, rq->fence.seqno,
			       t->engine->name);
376 377
			GEM_TRACE_DUMP();

378
			intel_gt_set_wedged(t->engine->gt);
379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421
			GEM_BUG_ON(!i915_request_completed(rq));
			i915_sw_fence_wait(wait);
			err = -EIO;
		}

		for (n = 0; n < count; n++) {
			struct i915_request *rq = requests[n];

			if (!test_bit(DMA_FENCE_FLAG_SIGNALED_BIT,
				      &rq->fence.flags)) {
				pr_err("%llu:%llu was not signaled!\n",
				       rq->fence.context, rq->fence.seqno);
				err = -EINVAL;
			}

			i915_request_put(rq);
		}

		heap_fence_put(wait);
		heap_fence_put(submit);

		if (err < 0)
			break;

		num_fences += count;
		num_waits++;

		cond_resched();
	}

	atomic_long_add(num_fences, &t->num_fences);
	atomic_long_add(num_waits, &t->num_waits);

	kfree(order);
out_requests:
	kfree(requests);
	return err;
}

static int mock_breadcrumbs_smoketest(void *arg)
{
	struct drm_i915_private *i915 = arg;
	struct smoketest t = {
422
		.engine = i915->engine[RCS0],
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437
		.ncontexts = 1024,
		.max_batch = 1024,
		.request_alloc = __mock_request_alloc
	};
	unsigned int ncpus = num_online_cpus();
	struct task_struct **threads;
	unsigned int n;
	int ret = 0;

	/*
	 * Smoketest our breadcrumb/signal handling for requests across multiple
	 * threads. A very simple test to only catch the most egregious of bugs.
	 * See __igt_breadcrumbs_smoketest();
	 */

438
	threads = kcalloc(ncpus, sizeof(*threads), GFP_KERNEL);
439 440 441
	if (!threads)
		return -ENOMEM;

442
	t.contexts = kcalloc(t.ncontexts, sizeof(*t.contexts), GFP_KERNEL);
443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
	if (!t.contexts) {
		ret = -ENOMEM;
		goto out_threads;
	}

	for (n = 0; n < t.ncontexts; n++) {
		t.contexts[n] = mock_context(t.engine->i915, "mock");
		if (!t.contexts[n]) {
			ret = -ENOMEM;
			goto out_contexts;
		}
	}

	for (n = 0; n < ncpus; n++) {
		threads[n] = kthread_run(__igt_breadcrumbs_smoketest,
					 &t, "igt/%d", n);
		if (IS_ERR(threads[n])) {
			ret = PTR_ERR(threads[n]);
			ncpus = n;
			break;
		}

		get_task_struct(threads[n]);
	}

468
	yield(); /* start all threads before we begin */
469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
	msleep(jiffies_to_msecs(i915_selftest.timeout_jiffies));

	for (n = 0; n < ncpus; n++) {
		int err;

		err = kthread_stop(threads[n]);
		if (err < 0 && !ret)
			ret = err;

		put_task_struct(threads[n]);
	}
	pr_info("Completed %lu waits for %lu fence across %d cpus\n",
		atomic_long_read(&t.num_waits),
		atomic_long_read(&t.num_fences),
		ncpus);

out_contexts:
	for (n = 0; n < t.ncontexts; n++) {
		if (!t.contexts[n])
			break;
		mock_context_close(t.contexts[n]);
	}
	kfree(t.contexts);
out_threads:
	kfree(threads);
	return ret;
}

497
int i915_request_mock_selftests(void)
498 499 500
{
	static const struct i915_subtest tests[] = {
		SUBTEST(igt_add_request),
501
		SUBTEST(igt_wait_request),
502
		SUBTEST(igt_fence_wait),
503
		SUBTEST(igt_request_rewind),
504
		SUBTEST(mock_breadcrumbs_smoketest),
505 506
	};
	struct drm_i915_private *i915;
507
	intel_wakeref_t wakeref;
508
	int err = 0;
509 510 511 512 513

	i915 = mock_gem_device();
	if (!i915)
		return -ENOMEM;

514
	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
515
		err = i915_subtests(tests, i915);
516

517
	drm_dev_put(&i915->drm);
518 519 520

	return err;
}
521 522 523 524 525

static int live_nop_request(void *arg)
{
	struct drm_i915_private *i915 = arg;
	struct intel_engine_cs *engine;
526
	struct igt_live_test t;
527
	int err = -ENODEV;
528

529 530
	/*
	 * Submit various sized batches of empty requests, to each engine
531 532 533 534
	 * (individually), and wait for the batch to complete. We can check
	 * the overhead of submitting requests to the hardware.
	 */

535
	for_each_uabi_engine(engine, i915) {
536
		unsigned long n, prime;
537
		IGT_TIMEOUT(end_time);
538 539
		ktime_t times[2] = {};

540
		err = igt_live_test_begin(&t, i915, __func__, engine->name);
541
		if (err)
542
			return err;
543 544

		for_each_prime_number_from(prime, 1, 8192) {
545 546
			struct i915_request *request = NULL;

547 548 549
			times[1] = ktime_get_raw();

			for (n = 0; n < prime; n++) {
550
				i915_request_put(request);
551
				request = i915_request_create(engine->kernel_context);
552 553
				if (IS_ERR(request))
					return PTR_ERR(request);
554

555 556
				/*
				 * This space is left intentionally blank.
557 558 559 560 561 562 563 564 565 566 567 568
				 *
				 * We do not actually want to perform any
				 * action with this request, we just want
				 * to measure the latency in allocation
				 * and submission of our breadcrumbs -
				 * ensuring that the bare request is sufficient
				 * for the system to work (i.e. proper HEAD
				 * tracking of the rings, interrupt handling,
				 * etc). It also gives us the lowest bounds
				 * for latency.
				 */

569
				i915_request_get(request);
570
				i915_request_add(request);
571
			}
572
			i915_request_wait(request, 0, MAX_SCHEDULE_TIMEOUT);
573
			i915_request_put(request);
574 575 576 577 578 579 580 581 582

			times[1] = ktime_sub(ktime_get_raw(), times[1]);
			if (prime == 1)
				times[0] = times[1];

			if (__igt_timeout(end_time, NULL))
				break;
		}

583
		err = igt_live_test_end(&t);
584
		if (err)
585
			return err;
586 587 588 589 590 591 592 593 594 595

		pr_info("Request latencies on %s: 1 = %lluns, %lu = %lluns\n",
			engine->name,
			ktime_to_ns(times[0]),
			prime, div64_u64(ktime_to_ns(times[1]), prime));
	}

	return err;
}

596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611
static struct i915_vma *empty_batch(struct drm_i915_private *i915)
{
	struct drm_i915_gem_object *obj;
	struct i915_vma *vma;
	u32 *cmd;
	int err;

	obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
	if (IS_ERR(obj))
		return ERR_CAST(obj);

	cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
	if (IS_ERR(cmd)) {
		err = PTR_ERR(cmd);
		goto err;
	}
612

613
	*cmd = MI_BATCH_BUFFER_END;
614

615
	__i915_gem_object_flush_map(obj, 0, 64);
616 617
	i915_gem_object_unpin_map(obj);

618
	intel_gt_chipset_flush(&i915->gt);
619

620
	vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
621 622 623 624 625 626 627 628 629
	if (IS_ERR(vma)) {
		err = PTR_ERR(vma);
		goto err;
	}

	err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_GLOBAL);
	if (err)
		goto err;

630 631 632 633 634
	/* Force the wait wait now to avoid including it in the benchmark */
	err = i915_vma_sync(vma);
	if (err)
		goto err_pin;

635 636
	return vma;

637 638
err_pin:
	i915_vma_unpin(vma);
639 640 641 642 643
err:
	i915_gem_object_put(obj);
	return ERR_PTR(err);
}

644
static struct i915_request *
645 646 647
empty_request(struct intel_engine_cs *engine,
	      struct i915_vma *batch)
{
648
	struct i915_request *request;
649 650
	int err;

651
	request = i915_request_create(engine->kernel_context);
652 653 654 655 656 657 658 659 660 661
	if (IS_ERR(request))
		return request;

	err = engine->emit_bb_start(request,
				    batch->node.start,
				    batch->node.size,
				    I915_DISPATCH_SECURE);
	if (err)
		goto out_request;

662
	i915_request_get(request);
663
out_request:
664
	i915_request_add(request);
665 666 667 668 669 670 671
	return err ? ERR_PTR(err) : request;
}

static int live_empty_request(void *arg)
{
	struct drm_i915_private *i915 = arg;
	struct intel_engine_cs *engine;
672
	struct igt_live_test t;
673 674 675
	struct i915_vma *batch;
	int err = 0;

676 677
	/*
	 * Submit various sized batches of empty requests, to each engine
678 679 680 681 682
	 * (individually), and wait for the batch to complete. We can check
	 * the overhead of submitting requests to the hardware.
	 */

	batch = empty_batch(i915);
683 684
	if (IS_ERR(batch))
		return PTR_ERR(batch);
685

686
	for_each_uabi_engine(engine, i915) {
687
		IGT_TIMEOUT(end_time);
688
		struct i915_request *request;
689 690 691
		unsigned long n, prime;
		ktime_t times[2] = {};

692
		err = igt_live_test_begin(&t, i915, __func__, engine->name);
693 694 695 696 697 698 699 700 701
		if (err)
			goto out_batch;

		/* Warmup / preload */
		request = empty_request(engine, batch);
		if (IS_ERR(request)) {
			err = PTR_ERR(request);
			goto out_batch;
		}
702
		i915_request_wait(request, 0, MAX_SCHEDULE_TIMEOUT);
703 704 705 706 707

		for_each_prime_number_from(prime, 1, 8192) {
			times[1] = ktime_get_raw();

			for (n = 0; n < prime; n++) {
708
				i915_request_put(request);
709 710 711 712 713 714
				request = empty_request(engine, batch);
				if (IS_ERR(request)) {
					err = PTR_ERR(request);
					goto out_batch;
				}
			}
715
			i915_request_wait(request, 0, MAX_SCHEDULE_TIMEOUT);
716 717 718 719 720 721 722 723

			times[1] = ktime_sub(ktime_get_raw(), times[1]);
			if (prime == 1)
				times[0] = times[1];

			if (__igt_timeout(end_time, NULL))
				break;
		}
724
		i915_request_put(request);
725

726
		err = igt_live_test_end(&t);
727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
		if (err)
			goto out_batch;

		pr_info("Batch latencies on %s: 1 = %lluns, %lu = %lluns\n",
			engine->name,
			ktime_to_ns(times[0]),
			prime, div64_u64(ktime_to_ns(times[1]), prime));
	}

out_batch:
	i915_vma_unpin(batch);
	i915_vma_put(batch);
	return err;
}

742 743 744 745 746
static struct i915_vma *recursive_batch(struct drm_i915_private *i915)
{
	struct i915_gem_context *ctx = i915->kernel_context;
	struct drm_i915_gem_object *obj;
	const int gen = INTEL_GEN(i915);
747
	struct i915_address_space *vm;
748 749 750 751 752 753 754 755
	struct i915_vma *vma;
	u32 *cmd;
	int err;

	obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
	if (IS_ERR(obj))
		return ERR_CAST(obj);

756
	vm = i915_gem_context_get_vm_rcu(ctx);
757
	vma = i915_vma_instance(obj, vm, NULL);
758
	i915_vm_put(vm);
759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
	if (IS_ERR(vma)) {
		err = PTR_ERR(vma);
		goto err;
	}

	err = i915_vma_pin(vma, 0, 0, PIN_USER);
	if (err)
		goto err;

	cmd = i915_gem_object_pin_map(obj, I915_MAP_WC);
	if (IS_ERR(cmd)) {
		err = PTR_ERR(cmd);
		goto err;
	}

	if (gen >= 8) {
		*cmd++ = MI_BATCH_BUFFER_START | 1 << 8 | 1;
		*cmd++ = lower_32_bits(vma->node.start);
		*cmd++ = upper_32_bits(vma->node.start);
	} else if (gen >= 6) {
		*cmd++ = MI_BATCH_BUFFER_START | 1 << 8;
		*cmd++ = lower_32_bits(vma->node.start);
	} else {
782
		*cmd++ = MI_BATCH_BUFFER_START | MI_BATCH_GTT;
783 784 785 786
		*cmd++ = lower_32_bits(vma->node.start);
	}
	*cmd++ = MI_BATCH_BUFFER_END; /* terminate early in case of error */

787
	__i915_gem_object_flush_map(obj, 0, 64);
788 789
	i915_gem_object_unpin_map(obj);

790
	intel_gt_chipset_flush(&i915->gt);
791

792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807
	return vma;

err:
	i915_gem_object_put(obj);
	return ERR_PTR(err);
}

static int recursive_batch_resolve(struct i915_vma *batch)
{
	u32 *cmd;

	cmd = i915_gem_object_pin_map(batch->obj, I915_MAP_WC);
	if (IS_ERR(cmd))
		return PTR_ERR(cmd);

	*cmd = MI_BATCH_BUFFER_END;
808
	intel_gt_chipset_flush(batch->vm->gt);
809 810 811 812 813 814 815 816 817

	i915_gem_object_unpin_map(batch->obj);

	return 0;
}

static int live_all_engines(void *arg)
{
	struct drm_i915_private *i915 = arg;
818
	const unsigned int nengines = num_uabi_engines(i915);
819
	struct intel_engine_cs *engine;
820
	struct i915_request **request;
821
	struct igt_live_test t;
822
	struct i915_vma *batch;
823
	unsigned int idx;
824 825
	int err;

826 827
	/*
	 * Check we can submit requests to all engines simultaneously. We
828 829 830 831
	 * send a recursive batch to each engine - checking that we don't
	 * block doing so, and that they don't complete too soon.
	 */

832 833 834 835
	request = kcalloc(nengines, sizeof(*request), GFP_KERNEL);
	if (!request)
		return -ENOMEM;

836
	err = igt_live_test_begin(&t, i915, __func__, "");
837
	if (err)
838
		goto out_free;
839 840 841 842 843

	batch = recursive_batch(i915);
	if (IS_ERR(batch)) {
		err = PTR_ERR(batch);
		pr_err("%s: Unable to create batch, err=%d\n", __func__, err);
844
		goto out_free;
845 846
	}

847 848 849 850 851
	idx = 0;
	for_each_uabi_engine(engine, i915) {
		request[idx] = i915_request_create(engine->kernel_context);
		if (IS_ERR(request[idx])) {
			err = PTR_ERR(request[idx]);
852 853 854 855 856
			pr_err("%s: Request allocation failed with err=%d\n",
			       __func__, err);
			goto out_request;
		}

857
		err = engine->emit_bb_start(request[idx],
858 859 860 861
					    batch->node.start,
					    batch->node.size,
					    0);
		GEM_BUG_ON(err);
862
		request[idx]->batch = batch;
863

864
		i915_vma_lock(batch);
865
		err = i915_request_await_object(request[idx], batch->obj, 0);
866
		if (err == 0)
867
			err = i915_vma_move_to_active(batch, request[idx], 0);
868
		i915_vma_unlock(batch);
869 870
		GEM_BUG_ON(err);

871 872 873
		i915_request_get(request[idx]);
		i915_request_add(request[idx]);
		idx++;
874 875
	}

876 877 878
	idx = 0;
	for_each_uabi_engine(engine, i915) {
		if (i915_request_completed(request[idx])) {
879 880 881 882 883
			pr_err("%s(%s): request completed too early!\n",
			       __func__, engine->name);
			err = -EINVAL;
			goto out_request;
		}
884
		idx++;
885 886 887 888 889 890 891 892
	}

	err = recursive_batch_resolve(batch);
	if (err) {
		pr_err("%s: failed to resolve batch, err=%d\n", __func__, err);
		goto out_request;
	}

893 894
	idx = 0;
	for_each_uabi_engine(engine, i915) {
895 896
		long timeout;

897
		timeout = i915_request_wait(request[idx], 0,
898 899 900 901 902 903 904 905
					    MAX_SCHEDULE_TIMEOUT);
		if (timeout < 0) {
			err = timeout;
			pr_err("%s: error waiting for request on %s, err=%d\n",
			       __func__, engine->name, err);
			goto out_request;
		}

906 907 908 909
		GEM_BUG_ON(!i915_request_completed(request[idx]));
		i915_request_put(request[idx]);
		request[idx] = NULL;
		idx++;
910 911
	}

912
	err = igt_live_test_end(&t);
913 914

out_request:
915 916 917 918 919 920
	idx = 0;
	for_each_uabi_engine(engine, i915) {
		if (request[idx])
			i915_request_put(request[idx]);
		idx++;
	}
921 922
	i915_vma_unpin(batch);
	i915_vma_put(batch);
923 924
out_free:
	kfree(request);
925 926 927
	return err;
}

928 929 930
static int live_sequential_engines(void *arg)
{
	struct drm_i915_private *i915 = arg;
931 932
	const unsigned int nengines = num_uabi_engines(i915);
	struct i915_request **request;
933
	struct i915_request *prev = NULL;
934
	struct intel_engine_cs *engine;
935
	struct igt_live_test t;
936
	unsigned int idx;
937 938
	int err;

939 940
	/*
	 * Check we can submit requests to all engines sequentially, such
941 942 943 944 945
	 * that each successive request waits for the earlier ones. This
	 * tests that we don't execute requests out of order, even though
	 * they are running on independent engines.
	 */

946 947 948 949
	request = kcalloc(nengines, sizeof(*request), GFP_KERNEL);
	if (!request)
		return -ENOMEM;

950
	err = igt_live_test_begin(&t, i915, __func__, "");
951
	if (err)
952
		goto out_free;
953

954 955
	idx = 0;
	for_each_uabi_engine(engine, i915) {
956 957 958 959 960 961 962
		struct i915_vma *batch;

		batch = recursive_batch(i915);
		if (IS_ERR(batch)) {
			err = PTR_ERR(batch);
			pr_err("%s: Unable to create batch for %s, err=%d\n",
			       __func__, engine->name, err);
963
			goto out_free;
964 965
		}

966 967 968
		request[idx] = i915_request_create(engine->kernel_context);
		if (IS_ERR(request[idx])) {
			err = PTR_ERR(request[idx]);
969 970 971 972 973 974
			pr_err("%s: Request allocation failed for %s with err=%d\n",
			       __func__, engine->name, err);
			goto out_request;
		}

		if (prev) {
975
			err = i915_request_await_dma_fence(request[idx],
976
							   &prev->fence);
977
			if (err) {
978
				i915_request_add(request[idx]);
979 980 981 982 983 984
				pr_err("%s: Request await failed for %s with err=%d\n",
				       __func__, engine->name, err);
				goto out_request;
			}
		}

985
		err = engine->emit_bb_start(request[idx],
986 987 988 989
					    batch->node.start,
					    batch->node.size,
					    0);
		GEM_BUG_ON(err);
990
		request[idx]->batch = batch;
991

992
		i915_vma_lock(batch);
993 994
		err = i915_request_await_object(request[idx],
						batch->obj, false);
995
		if (err == 0)
996
			err = i915_vma_move_to_active(batch, request[idx], 0);
997
		i915_vma_unlock(batch);
998 999
		GEM_BUG_ON(err);

1000 1001
		i915_request_get(request[idx]);
		i915_request_add(request[idx]);
1002

1003 1004
		prev = request[idx];
		idx++;
1005 1006
	}

1007 1008
	idx = 0;
	for_each_uabi_engine(engine, i915) {
1009 1010
		long timeout;

1011
		if (i915_request_completed(request[idx])) {
1012 1013 1014 1015 1016 1017
			pr_err("%s(%s): request completed too early!\n",
			       __func__, engine->name);
			err = -EINVAL;
			goto out_request;
		}

1018
		err = recursive_batch_resolve(request[idx]->batch);
1019 1020 1021 1022 1023 1024
		if (err) {
			pr_err("%s: failed to resolve batch, err=%d\n",
			       __func__, err);
			goto out_request;
		}

1025
		timeout = i915_request_wait(request[idx], 0,
1026 1027 1028 1029 1030 1031 1032 1033
					    MAX_SCHEDULE_TIMEOUT);
		if (timeout < 0) {
			err = timeout;
			pr_err("%s: error waiting for request on %s, err=%d\n",
			       __func__, engine->name, err);
			goto out_request;
		}

1034 1035
		GEM_BUG_ON(!i915_request_completed(request[idx]));
		idx++;
1036 1037
	}

1038
	err = igt_live_test_end(&t);
1039 1040

out_request:
1041 1042
	idx = 0;
	for_each_uabi_engine(engine, i915) {
1043 1044
		u32 *cmd;

1045
		if (!request[idx])
1046 1047
			break;

1048
		cmd = i915_gem_object_pin_map(request[idx]->batch->obj,
1049 1050 1051
					      I915_MAP_WC);
		if (!IS_ERR(cmd)) {
			*cmd = MI_BATCH_BUFFER_END;
1052
			intel_gt_chipset_flush(engine->gt);
1053

1054
			i915_gem_object_unpin_map(request[idx]->batch->obj);
1055 1056
		}

1057 1058 1059
		i915_vma_put(request[idx]->batch);
		i915_request_put(request[idx]);
		idx++;
1060
	}
1061 1062
out_free:
	kfree(request);
1063 1064 1065
	return err;
}

1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077
static int __live_parallel_engine1(void *arg)
{
	struct intel_engine_cs *engine = arg;
	IGT_TIMEOUT(end_time);
	unsigned long count;

	count = 0;
	do {
		struct i915_request *rq;
		int err;

		rq = i915_request_create(engine->kernel_context);
1078
		if (IS_ERR(rq))
1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
			return PTR_ERR(rq);

		i915_request_get(rq);
		i915_request_add(rq);

		err = 0;
		if (i915_request_wait(rq, 0, HZ / 5) < 0)
			err = -ETIME;
		i915_request_put(rq);
		if (err)
			return err;

		count++;
	} while (!__igt_timeout(end_time, NULL));

	pr_info("%s: %lu request + sync\n", engine->name, count);
	return 0;
}

static int __live_parallel_engineN(void *arg)
{
	struct intel_engine_cs *engine = arg;
	IGT_TIMEOUT(end_time);
	unsigned long count;

	count = 0;
	do {
		struct i915_request *rq;

		rq = i915_request_create(engine->kernel_context);
1109
		if (IS_ERR(rq))
1110 1111 1112 1113 1114 1115 1116 1117 1118 1119
			return PTR_ERR(rq);

		i915_request_add(rq);
		count++;
	} while (!__igt_timeout(end_time, NULL));

	pr_info("%s: %lu requests\n", engine->name, count);
	return 0;
}

1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191
static bool wake_all(struct drm_i915_private *i915)
{
	if (atomic_dec_and_test(&i915->selftest.counter)) {
		wake_up_var(&i915->selftest.counter);
		return true;
	}

	return false;
}

static int wait_for_all(struct drm_i915_private *i915)
{
	if (wake_all(i915))
		return 0;

	if (wait_var_event_timeout(&i915->selftest.counter,
				   !atomic_read(&i915->selftest.counter),
				   i915_selftest.timeout_jiffies))
		return 0;

	return -ETIME;
}

static int __live_parallel_spin(void *arg)
{
	struct intel_engine_cs *engine = arg;
	struct igt_spinner spin;
	struct i915_request *rq;
	int err = 0;

	/*
	 * Create a spinner running for eternity on each engine. If a second
	 * spinner is incorrectly placed on the same engine, it will not be
	 * able to start in time.
	 */

	if (igt_spinner_init(&spin, engine->gt)) {
		wake_all(engine->i915);
		return -ENOMEM;
	}

	rq = igt_spinner_create_request(&spin,
					engine->kernel_context,
					MI_NOOP); /* no preemption */
	if (IS_ERR(rq)) {
		err = PTR_ERR(rq);
		if (err == -ENODEV)
			err = 0;
		wake_all(engine->i915);
		goto out_spin;
	}

	i915_request_get(rq);
	i915_request_add(rq);
	if (igt_wait_for_spinner(&spin, rq)) {
		/* Occupy this engine for the whole test */
		err = wait_for_all(engine->i915);
	} else {
		pr_err("Failed to start spinner on %s\n", engine->name);
		err = -EINVAL;
	}
	igt_spinner_end(&spin);

	if (err == 0 && i915_request_wait(rq, 0, HZ / 5) < 0)
		err = -EIO;
	i915_request_put(rq);

out_spin:
	igt_spinner_fini(&spin);
	return err;
}

1192 1193 1194 1195 1196 1197
static int live_parallel_engines(void *arg)
{
	struct drm_i915_private *i915 = arg;
	static int (* const func[])(void *arg) = {
		__live_parallel_engine1,
		__live_parallel_engineN,
1198
		__live_parallel_spin,
1199 1200
		NULL,
	};
1201
	const unsigned int nengines = num_uabi_engines(i915);
1202 1203
	struct intel_engine_cs *engine;
	int (* const *fn)(void *arg);
1204
	struct task_struct **tsk;
1205 1206 1207 1208 1209 1210 1211
	int err = 0;

	/*
	 * Check we can submit requests to all engines concurrently. This
	 * tests that we load up the system maximally.
	 */

1212 1213 1214 1215
	tsk = kcalloc(nengines, sizeof(*tsk), GFP_KERNEL);
	if (!tsk)
		return -ENOMEM;

1216
	for (fn = func; !err && *fn; fn++) {
1217
		char name[KSYM_NAME_LEN];
1218
		struct igt_live_test t;
1219
		unsigned int idx;
1220

1221 1222
		snprintf(name, sizeof(name), "%pS", fn);
		err = igt_live_test_begin(&t, i915, __func__, name);
1223 1224 1225
		if (err)
			break;

1226 1227
		atomic_set(&i915->selftest.counter, nengines);

1228 1229 1230 1231 1232 1233 1234
		idx = 0;
		for_each_uabi_engine(engine, i915) {
			tsk[idx] = kthread_run(*fn, engine,
					       "igt/parallel:%s",
					       engine->name);
			if (IS_ERR(tsk[idx])) {
				err = PTR_ERR(tsk[idx]);
1235 1236
				break;
			}
1237
			get_task_struct(tsk[idx++]);
1238 1239
		}

1240 1241
		yield(); /* start all threads before we kthread_stop() */

1242 1243
		idx = 0;
		for_each_uabi_engine(engine, i915) {
1244 1245
			int status;

1246 1247
			if (IS_ERR(tsk[idx]))
				break;
1248

1249
			status = kthread_stop(tsk[idx]);
1250 1251 1252
			if (status && !err)
				err = status;

1253
			put_task_struct(tsk[idx++]);
1254 1255 1256 1257 1258 1259
		}

		if (igt_live_test_end(&t))
			err = -EIO;
	}

1260
	kfree(tsk);
1261 1262 1263
	return err;
}

1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281
static int
max_batches(struct i915_gem_context *ctx, struct intel_engine_cs *engine)
{
	struct i915_request *rq;
	int ret;

	/*
	 * Before execlists, all contexts share the same ringbuffer. With
	 * execlists, each context/engine has a separate ringbuffer and
	 * for the purposes of this test, inexhaustible.
	 *
	 * For the global ringbuffer though, we have to be very careful
	 * that we do not wrap while preventing the execution of requests
	 * with a unsignaled fence.
	 */
	if (HAS_EXECLISTS(ctx->i915))
		return INT_MAX;

1282
	rq = igt_request_alloc(ctx, engine);
1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303
	if (IS_ERR(rq)) {
		ret = PTR_ERR(rq);
	} else {
		int sz;

		ret = rq->ring->size - rq->reserved_space;
		i915_request_add(rq);

		sz = rq->ring->emit - rq->head;
		if (sz < 0)
			sz += rq->ring->size;
		ret /= sz;
		ret /= 2; /* leave half spare, in case of emergency! */
	}

	return ret;
}

static int live_breadcrumbs_smoketest(void *arg)
{
	struct drm_i915_private *i915 = arg;
1304 1305
	const unsigned int nengines = num_uabi_engines(i915);
	const unsigned int ncpus = num_online_cpus();
1306 1307 1308 1309 1310
	unsigned long num_waits, num_fences;
	struct intel_engine_cs *engine;
	struct task_struct **threads;
	struct igt_live_test live;
	intel_wakeref_t wakeref;
1311 1312
	struct smoketest *smoke;
	unsigned int n, idx;
1313
	struct file *file;
1314 1315 1316 1317 1318 1319 1320 1321 1322 1323
	int ret = 0;

	/*
	 * Smoketest our breadcrumb/signal handling for requests across multiple
	 * threads. A very simple test to only catch the most egregious of bugs.
	 * See __igt_breadcrumbs_smoketest();
	 *
	 * On real hardware this time.
	 */

1324
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1325 1326 1327 1328 1329 1330 1331

	file = mock_file(i915);
	if (IS_ERR(file)) {
		ret = PTR_ERR(file);
		goto out_rpm;
	}

1332 1333
	smoke = kcalloc(nengines, sizeof(*smoke), GFP_KERNEL);
	if (!smoke) {
1334 1335 1336 1337
		ret = -ENOMEM;
		goto out_file;
	}

1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349
	threads = kcalloc(ncpus * nengines, sizeof(*threads), GFP_KERNEL);
	if (!threads) {
		ret = -ENOMEM;
		goto out_smoke;
	}

	smoke[0].request_alloc = __live_request_alloc;
	smoke[0].ncontexts = 64;
	smoke[0].contexts = kcalloc(smoke[0].ncontexts,
				    sizeof(*smoke[0].contexts),
				    GFP_KERNEL);
	if (!smoke[0].contexts) {
1350 1351 1352 1353
		ret = -ENOMEM;
		goto out_threads;
	}

1354 1355 1356
	for (n = 0; n < smoke[0].ncontexts; n++) {
		smoke[0].contexts[n] = live_context(i915, file);
		if (!smoke[0].contexts[n]) {
1357 1358 1359 1360 1361 1362 1363 1364 1365
			ret = -ENOMEM;
			goto out_contexts;
		}
	}

	ret = igt_live_test_begin(&live, i915, __func__, "");
	if (ret)
		goto out_contexts;

1366 1367 1368 1369 1370 1371 1372 1373
	idx = 0;
	for_each_uabi_engine(engine, i915) {
		smoke[idx] = smoke[0];
		smoke[idx].engine = engine;
		smoke[idx].max_batch =
			max_batches(smoke[0].contexts[0], engine);
		if (smoke[idx].max_batch < 0) {
			ret = smoke[idx].max_batch;
1374 1375 1376
			goto out_flush;
		}
		/* One ring interleaved between requests from all cpus */
1377
		smoke[idx].max_batch /= num_online_cpus() + 1;
1378
		pr_debug("Limiting batches to %d requests on %s\n",
1379
			 smoke[idx].max_batch, engine->name);
1380 1381 1382 1383 1384

		for (n = 0; n < ncpus; n++) {
			struct task_struct *tsk;

			tsk = kthread_run(__igt_breadcrumbs_smoketest,
1385
					  &smoke[idx], "igt/%d.%d", idx, n);
1386 1387 1388 1389 1390 1391
			if (IS_ERR(tsk)) {
				ret = PTR_ERR(tsk);
				goto out_flush;
			}

			get_task_struct(tsk);
1392
			threads[idx * ncpus + n] = tsk;
1393
		}
1394 1395

		idx++;
1396 1397
	}

1398
	yield(); /* start all threads before we begin */
1399 1400 1401
	msleep(jiffies_to_msecs(i915_selftest.timeout_jiffies));

out_flush:
1402
	idx = 0;
1403 1404
	num_waits = 0;
	num_fences = 0;
1405
	for_each_uabi_engine(engine, i915) {
1406
		for (n = 0; n < ncpus; n++) {
1407
			struct task_struct *tsk = threads[idx * ncpus + n];
1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419
			int err;

			if (!tsk)
				continue;

			err = kthread_stop(tsk);
			if (err < 0 && !ret)
				ret = err;

			put_task_struct(tsk);
		}

1420 1421 1422
		num_waits += atomic_long_read(&smoke[idx].num_waits);
		num_fences += atomic_long_read(&smoke[idx].num_fences);
		idx++;
1423 1424
	}
	pr_info("Completed %lu waits for %lu fences across %d engines and %d cpus\n",
1425
		num_waits, num_fences, RUNTIME_INFO(i915)->num_engines, ncpus);
1426 1427 1428

	ret = igt_live_test_end(&live) ?: ret;
out_contexts:
1429
	kfree(smoke[0].contexts);
1430 1431
out_threads:
	kfree(threads);
1432 1433
out_smoke:
	kfree(smoke);
1434
out_file:
1435
	fput(file);
1436
out_rpm:
1437
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1438 1439 1440 1441

	return ret;
}

1442
int i915_request_live_selftests(struct drm_i915_private *i915)
1443 1444 1445
{
	static const struct i915_subtest tests[] = {
		SUBTEST(live_nop_request),
1446
		SUBTEST(live_all_engines),
1447
		SUBTEST(live_sequential_engines),
1448
		SUBTEST(live_parallel_engines),
1449
		SUBTEST(live_empty_request),
1450
		SUBTEST(live_breadcrumbs_smoketest),
1451
	};
1452

1453
	if (intel_gt_is_wedged(&i915->gt))
1454 1455
		return 0;

1456 1457
	return i915_subtests(tests, i915);
}