i915_gem_context.c 40.3 KB
Newer Older
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * Copyright © 2017 Intel Corporation
5 6
 */

7 8
#include <linux/prime_numbers.h>

9
#include "gem/i915_gem_pm.h"
10
#include "gt/intel_engine_pm.h"
11
#include "gt/intel_gt.h"
12
#include "gt/intel_gt_requests.h"
13 14 15
#include "gt/intel_reset.h"
#include "i915_selftest.h"

16 17 18 19 20 21 22 23
#include "gem/selftests/igt_gem_utils.h"
#include "selftests/i915_random.h"
#include "selftests/igt_flush_test.h"
#include "selftests/igt_live_test.h"
#include "selftests/igt_reset.h"
#include "selftests/igt_spinner.h"
#include "selftests/mock_drm.h"
#include "selftests/mock_gem_device.h"
24 25

#include "huge_gem_object.h"
26
#include "igt_gem_utils.h"
27 28 29

#define DW_PER_PAGE (PAGE_SIZE / sizeof(u32))

30 31 32 33 34 35
static inline struct i915_address_space *ctx_vm(struct i915_gem_context *ctx)
{
	/* single threaded, private ctx */
	return rcu_dereference_protected(ctx->vm, true);
}

36 37 38 39 40 41
static int live_nop_switch(void *arg)
{
	const unsigned int nctx = 1024;
	struct drm_i915_private *i915 = arg;
	struct intel_engine_cs *engine;
	struct i915_gem_context **ctx;
42
	struct igt_live_test t;
43
	struct file *file;
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
	unsigned long n;
	int err = -ENODEV;

	/*
	 * Create as many contexts as we can feasibly get away with
	 * and check we can switch between them rapidly.
	 *
	 * Serves as very simple stress test for submission and HW switching
	 * between contexts.
	 */

	if (!DRIVER_CAPS(i915)->has_logical_contexts)
		return 0;

	file = mock_file(i915);
	if (IS_ERR(file))
		return PTR_ERR(file);

	ctx = kcalloc(nctx, sizeof(*ctx), GFP_KERNEL);
	if (!ctx) {
		err = -ENOMEM;
65
		goto out_file;
66 67 68
	}

	for (n = 0; n < nctx; n++) {
69
		ctx[n] = live_context(i915, file);
70 71
		if (IS_ERR(ctx[n])) {
			err = PTR_ERR(ctx[n]);
72
			goto out_file;
73 74 75
		}
	}

76
	for_each_uabi_engine(engine, i915) {
77
		struct i915_request *rq = NULL;
78 79 80 81 82
		unsigned long end_time, prime;
		ktime_t times[2] = {};

		times[0] = ktime_get_raw();
		for (n = 0; n < nctx; n++) {
83 84 85 86 87
			struct i915_request *this;

			this = igt_request_alloc(ctx[n], engine);
			if (IS_ERR(this)) {
				err = PTR_ERR(this);
88
				goto out_file;
89
			}
90 91 92 93 94 95
			if (rq) {
				i915_request_await_dma_fence(this, &rq->fence);
				i915_request_put(rq);
			}
			rq = i915_request_get(this);
			i915_request_add(this);
96
		}
97
		if (i915_request_wait(rq, 0, HZ / 5) < 0) {
98
			pr_err("Failed to populated %d contexts\n", nctx);
99
			intel_gt_set_wedged(&i915->gt);
100
			i915_request_put(rq);
101
			err = -EIO;
102
			goto out_file;
103
		}
104
		i915_request_put(rq);
105 106 107 108 109 110

		times[1] = ktime_get_raw();

		pr_info("Populated %d contexts on %s in %lluns\n",
			nctx, engine->name, ktime_to_ns(times[1] - times[0]));

111
		err = igt_live_test_begin(&t, i915, __func__, engine->name);
112
		if (err)
113
			goto out_file;
114 115 116 117 118

		end_time = jiffies + i915_selftest.timeout_jiffies;
		for_each_prime_number_from(prime, 2, 8192) {
			times[1] = ktime_get_raw();

119
			rq = NULL;
120
			for (n = 0; n < prime; n++) {
121 122 123 124 125
				struct i915_request *this;

				this = igt_request_alloc(ctx[n % nctx], engine);
				if (IS_ERR(this)) {
					err = PTR_ERR(this);
126
					goto out_file;
127 128
				}

129 130 131 132 133
				if (rq) { /* Force submission order */
					i915_request_await_dma_fence(this, &rq->fence);
					i915_request_put(rq);
				}

134 135 136 137 138 139 140 141 142 143 144 145 146 147
				/*
				 * This space is left intentionally blank.
				 *
				 * 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.
				 */

148 149
				rq = i915_request_get(this);
				i915_request_add(this);
150
			}
151
			GEM_BUG_ON(!rq);
152
			if (i915_request_wait(rq, 0, HZ / 5) < 0) {
153 154
				pr_err("Switching between %ld contexts timed out\n",
				       prime);
155
				intel_gt_set_wedged(&i915->gt);
156
				i915_request_put(rq);
157 158
				break;
			}
159
			i915_request_put(rq);
160 161 162 163 164 165 166 167 168

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

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

169
		err = igt_live_test_end(&t);
170
		if (err)
171
			goto out_file;
172 173 174 175 176 177 178

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

179
out_file:
180
	fput(file);
181 182 183
	return err;
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
struct parallel_switch {
	struct task_struct *tsk;
	struct intel_context *ce[2];
};

static int __live_parallel_switch1(void *data)
{
	struct parallel_switch *arg = data;
	IGT_TIMEOUT(end_time);
	unsigned long count;

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

200 201 202
		err = 0;
		for (n = 0; !err && n < ARRAY_SIZE(arg->ce); n++) {
			struct i915_request *prev = rq;
203 204

			rq = i915_request_create(arg->ce[n]);
205 206
			if (IS_ERR(rq)) {
				i915_request_put(prev);
207
				return PTR_ERR(rq);
208
			}
209 210

			i915_request_get(rq);
211 212 213 214 215
			if (prev) {
				err = i915_request_await_dma_fence(rq, &prev->fence);
				i915_request_put(prev);
			}

216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233
			i915_request_add(rq);
		}
		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 switches (sync)\n", arg->ce[0]->engine->name, count);
	return 0;
}

static int __live_parallel_switchN(void *data)
{
	struct parallel_switch *arg = data;
234
	struct i915_request *rq = NULL;
235 236 237 238 239 240 241
	IGT_TIMEOUT(end_time);
	unsigned long count;
	int n;

	count = 0;
	do {
		for (n = 0; n < ARRAY_SIZE(arg->ce); n++) {
242 243
			struct i915_request *prev = rq;
			int err = 0;
244 245

			rq = i915_request_create(arg->ce[n]);
246 247
			if (IS_ERR(rq)) {
				i915_request_put(prev);
248
				return PTR_ERR(rq);
249 250 251 252 253 254 255
			}

			i915_request_get(rq);
			if (prev) {
				err = i915_request_await_dma_fence(rq, &prev->fence);
				i915_request_put(prev);
			}
256 257

			i915_request_add(rq);
258 259 260 261
			if (err) {
				i915_request_put(rq);
				return err;
			}
262 263 264 265
		}

		count++;
	} while (!__igt_timeout(end_time, NULL));
266
	i915_request_put(rq);
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

	pr_info("%s: %lu switches (many)\n", arg->ce[0]->engine->name, count);
	return 0;
}

static int live_parallel_switch(void *arg)
{
	struct drm_i915_private *i915 = arg;
	static int (* const func[])(void *arg) = {
		__live_parallel_switch1,
		__live_parallel_switchN,
		NULL,
	};
	struct parallel_switch *data = NULL;
	struct i915_gem_engines *engines;
	struct i915_gem_engines_iter it;
	int (* const *fn)(void *arg);
	struct i915_gem_context *ctx;
	struct intel_context *ce;
286
	struct file *file;
287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303
	int n, m, count;
	int err = 0;

	/*
	 * Check we can process switches on all engines simultaneously.
	 */

	if (!DRIVER_CAPS(i915)->has_logical_contexts)
		return 0;

	file = mock_file(i915);
	if (IS_ERR(file))
		return PTR_ERR(file);

	ctx = live_context(i915, file);
	if (IS_ERR(ctx)) {
		err = PTR_ERR(ctx);
304
		goto out_file;
305 306 307 308 309 310 311 312 313
	}

	engines = i915_gem_context_lock_engines(ctx);
	count = engines->num_engines;

	data = kcalloc(count, sizeof(*data), GFP_KERNEL);
	if (!data) {
		i915_gem_context_unlock_engines(ctx);
		err = -ENOMEM;
314
		goto out_file;
315 316 317 318 319 320 321
	}

	m = 0; /* Use the first context as our template for the engines */
	for_each_gem_engine(ce, engines, it) {
		err = intel_context_pin(ce);
		if (err) {
			i915_gem_context_unlock_engines(ctx);
322
			goto out;
323 324 325 326 327 328 329 330 331 332
		}
		data[m++].ce[0] = intel_context_get(ce);
	}
	i915_gem_context_unlock_engines(ctx);

	/* Clone the same set of engines into the other contexts */
	for (n = 1; n < ARRAY_SIZE(data->ce); n++) {
		ctx = live_context(i915, file);
		if (IS_ERR(ctx)) {
			err = PTR_ERR(ctx);
333
			goto out;
334 335 336 337 338 339 340 341
		}

		for (m = 0; m < count; m++) {
			if (!data[m].ce[0])
				continue;

			ce = intel_context_create(ctx, data[m].ce[0]->engine);
			if (IS_ERR(ce))
342
				goto out;
343 344 345 346

			err = intel_context_pin(ce);
			if (err) {
				intel_context_put(ce);
347
				goto out;
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375
			}

			data[m].ce[n] = ce;
		}
	}

	for (fn = func; !err && *fn; fn++) {
		struct igt_live_test t;
		int n;

		err = igt_live_test_begin(&t, i915, __func__, "");
		if (err)
			break;

		for (n = 0; n < count; n++) {
			if (!data[n].ce[0])
				continue;

			data[n].tsk = kthread_run(*fn, &data[n],
						  "igt/parallel:%s",
						  data[n].ce[0]->engine->name);
			if (IS_ERR(data[n].tsk)) {
				err = PTR_ERR(data[n].tsk);
				break;
			}
			get_task_struct(data[n].tsk);
		}

376 377
		yield(); /* start all threads before we kthread_stop() */

378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395
		for (n = 0; n < count; n++) {
			int status;

			if (IS_ERR_OR_NULL(data[n].tsk))
				continue;

			status = kthread_stop(data[n].tsk);
			if (status && !err)
				err = status;

			put_task_struct(data[n].tsk);
			data[n].tsk = NULL;
		}

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

396
out:
397 398 399 400 401 402 403 404 405 406
	for (n = 0; n < count; n++) {
		for (m = 0; m < ARRAY_SIZE(data->ce); m++) {
			if (!data[n].ce[m])
				continue;

			intel_context_unpin(data[n].ce[m]);
			intel_context_put(data[n].ce[m]);
		}
	}
	kfree(data);
407
out_file:
408
	fput(file);
409 410 411
	return err;
}

412 413 414 415 416 417 418 419 420 421
static unsigned long real_page_count(struct drm_i915_gem_object *obj)
{
	return huge_gem_object_phys_size(obj) >> PAGE_SHIFT;
}

static unsigned long fake_page_count(struct drm_i915_gem_object *obj)
{
	return huge_gem_object_dma_size(obj) >> PAGE_SHIFT;
}

422 423
static int gpu_fill(struct intel_context *ce,
		    struct drm_i915_gem_object *obj,
424 425 426 427 428
		    unsigned int dw)
{
	struct i915_vma *vma;
	int err;

429 430
	GEM_BUG_ON(obj->base.size > ce->vm->total);
	GEM_BUG_ON(!intel_engine_can_store_dword(ce->engine));
431

432
	vma = i915_vma_instance(obj, ce->vm, NULL);
433 434 435 436 437 438 439
	if (IS_ERR(vma))
		return PTR_ERR(vma);

	err = i915_vma_pin(vma, 0, 0, PIN_HIGH | PIN_USER);
	if (err)
		return err;

440 441
	/*
	 * Within the GTT the huge objects maps every page onto
442 443 444 445 446 447
	 * its 1024 real pages (using phys_pfn = dma_pfn % 1024).
	 * We set the nth dword within the page using the nth
	 * mapping via the GTT - this should exercise the GTT mapping
	 * whilst checking that each context provides a unique view
	 * into the object.
	 */
448
	err = igt_gpu_fill_dw(ce, vma,
449 450 451 452
			      (dw * real_page_count(obj)) << PAGE_SHIFT |
			      (dw * sizeof(u32)),
			      real_page_count(obj),
			      dw);
453 454 455 456 457 458 459 460 461 462 463
	i915_vma_unpin(vma);

	return err;
}

static int cpu_fill(struct drm_i915_gem_object *obj, u32 value)
{
	const bool has_llc = HAS_LLC(to_i915(obj->base.dev));
	unsigned int n, m, need_flush;
	int err;

464
	err = i915_gem_object_prepare_write(obj, &need_flush);
465 466 467 468 469 470 471 472 473 474 475 476 477 478
	if (err)
		return err;

	for (n = 0; n < real_page_count(obj); n++) {
		u32 *map;

		map = kmap_atomic(i915_gem_object_get_page(obj, n));
		for (m = 0; m < DW_PER_PAGE; m++)
			map[m] = value;
		if (!has_llc)
			drm_clflush_virt_range(map, PAGE_SIZE);
		kunmap_atomic(map);
	}

479
	i915_gem_object_finish_access(obj);
480 481
	obj->read_domains = I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU;
	obj->write_domain = 0;
482 483 484
	return 0;
}

485 486
static noinline int cpu_check(struct drm_i915_gem_object *obj,
			      unsigned int idx, unsigned int max)
487 488 489 490
{
	unsigned int n, m, needs_flush;
	int err;

491
	err = i915_gem_object_prepare_read(obj, &needs_flush);
492 493 494 495 496 497 498 499 500 501 502 503
	if (err)
		return err;

	for (n = 0; n < real_page_count(obj); n++) {
		u32 *map;

		map = kmap_atomic(i915_gem_object_get_page(obj, n));
		if (needs_flush & CLFLUSH_BEFORE)
			drm_clflush_virt_range(map, PAGE_SIZE);

		for (m = 0; m < max; m++) {
			if (map[m] != m) {
504 505 506 507
				pr_err("%pS: Invalid value at object %d page %d/%ld, offset %d/%d: found %x expected %x\n",
				       __builtin_return_address(0), idx,
				       n, real_page_count(obj), m, max,
				       map[m], m);
508 509 510 511 512 513
				err = -EINVAL;
				goto out_unmap;
			}
		}

		for (; m < DW_PER_PAGE; m++) {
514
			if (map[m] != STACK_MAGIC) {
515 516 517
				pr_err("%pS: Invalid value at object %d page %d, offset %d: found %x expected %x (uninitialised)\n",
				       __builtin_return_address(0), idx, n, m,
				       map[m], STACK_MAGIC);
518 519 520 521 522 523 524 525 526 527 528
				err = -EINVAL;
				goto out_unmap;
			}
		}

out_unmap:
		kunmap_atomic(map);
		if (err)
			break;
	}

529
	i915_gem_object_finish_access(obj);
530 531 532
	return err;
}

533
static int file_add_object(struct file *file, struct drm_i915_gem_object *obj)
534 535 536 537 538 539
{
	int err;

	GEM_BUG_ON(obj->base.handle_count);

	/* tie the object to the drm_file for easy reaping */
540 541
	err = idr_alloc(&to_drm_file(file)->object_idr,
			&obj->base, 1, 0, GFP_KERNEL);
542
	if (err < 0)
543
		return err;
544 545 546 547 548 549

	i915_gem_object_get(obj);
	obj->base.handle_count++;
	return 0;
}

550
static struct drm_i915_gem_object *
551
create_test_object(struct i915_address_space *vm,
552
		   struct file *file,
553 554 555 556 557 558
		   struct list_head *objects)
{
	struct drm_i915_gem_object *obj;
	u64 size;
	int err;

559
	/* Keep in GEM's good graces */
560
	intel_gt_retire_requests(vm->gt);
561

562 563 564
	size = min(vm->total / 2, 1024ull * DW_PER_PAGE * PAGE_SIZE);
	size = round_down(size, DW_PER_PAGE * PAGE_SIZE);

565
	obj = huge_gem_object(vm->i915, DW_PER_PAGE * PAGE_SIZE, size);
566 567 568
	if (IS_ERR(obj))
		return obj;

569
	err = file_add_object(file, obj);
570 571 572 573
	i915_gem_object_put(obj);
	if (err)
		return ERR_PTR(err);

574
	err = cpu_fill(obj, STACK_MAGIC);
575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
	if (err) {
		pr_err("Failed to fill object with cpu, err=%d\n",
		       err);
		return ERR_PTR(err);
	}

	list_add_tail(&obj->st_link, objects);
	return obj;
}

static unsigned long max_dwords(struct drm_i915_gem_object *obj)
{
	unsigned long npages = fake_page_count(obj);

	GEM_BUG_ON(!IS_ALIGNED(npages, DW_PER_PAGE));
	return npages / DW_PER_PAGE;
}

593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631
static void throttle_release(struct i915_request **q, int count)
{
	int i;

	for (i = 0; i < count; i++) {
		if (IS_ERR_OR_NULL(q[i]))
			continue;

		i915_request_put(fetch_and_zero(&q[i]));
	}
}

static int throttle(struct intel_context *ce,
		    struct i915_request **q, int count)
{
	int i;

	if (!IS_ERR_OR_NULL(q[0])) {
		if (i915_request_wait(q[0],
				      I915_WAIT_INTERRUPTIBLE,
				      MAX_SCHEDULE_TIMEOUT) < 0)
			return -EINTR;

		i915_request_put(q[0]);
	}

	for (i = 0; i < count - 1; i++)
		q[i] = q[i + 1];

	q[i] = intel_context_create_request(ce);
	if (IS_ERR(q[i]))
		return PTR_ERR(q[i]);

	i915_request_get(q[i]);
	i915_request_add(q[i]);

	return 0;
}

632 633 634
static int igt_ctx_exec(void *arg)
{
	struct drm_i915_private *i915 = arg;
635
	struct intel_engine_cs *engine;
636
	int err = -ENODEV;
637

638 639
	/*
	 * Create a few different contexts (with different mm) and write
640 641 642 643
	 * through each ctx/mm using the GPU making sure those writes end
	 * up in the expected pages of our obj.
	 */

644 645 646
	if (!DRIVER_CAPS(i915)->has_logical_contexts)
		return 0;

647
	for_each_uabi_engine(engine, i915) {
648 649
		struct drm_i915_gem_object *obj = NULL;
		unsigned long ncontexts, ndwords, dw;
650
		struct i915_request *tq[5] = {};
651 652 653
		struct igt_live_test t;
		IGT_TIMEOUT(end_time);
		LIST_HEAD(objects);
654
		struct file *file;
655 656 657 658 659 660 661 662 663 664 665 666 667

		if (!intel_engine_can_store_dword(engine))
			continue;

		if (!engine->context_size)
			continue; /* No logical context support in HW */

		file = mock_file(i915);
		if (IS_ERR(file))
			return PTR_ERR(file);

		err = igt_live_test_begin(&t, i915, __func__, engine->name);
		if (err)
668
			goto out_file;
669 670 671 672 673 674

		ncontexts = 0;
		ndwords = 0;
		dw = 0;
		while (!time_after(jiffies, end_time)) {
			struct i915_gem_context *ctx;
675
			struct intel_context *ce;
676

677
			ctx = kernel_context(i915);
678 679
			if (IS_ERR(ctx)) {
				err = PTR_ERR(ctx);
680
				goto out_file;
681 682
			}

683
			ce = i915_gem_context_get_engine(ctx, engine->legacy_idx);
684
			GEM_BUG_ON(IS_ERR(ce));
685

686
			if (!obj) {
687
				obj = create_test_object(ce->vm, file, &objects);
688 689
				if (IS_ERR(obj)) {
					err = PTR_ERR(obj);
690
					intel_context_put(ce);
691
					kernel_context_close(ctx);
692
					goto out_file;
693 694 695
				}
			}

696
			err = gpu_fill(ce, obj, dw);
697
			if (err) {
C
Chris Wilson 已提交
698
				pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) [full-ppgtt? %s], err=%d\n",
699
				       ndwords, dw, max_dwords(obj),
C
Chris Wilson 已提交
700
				       engine->name,
701 702
				       yesno(!!rcu_access_pointer(ctx->vm)),
				       err);
703
				intel_context_put(ce);
704
				kernel_context_close(ctx);
705
				goto out_file;
706 707 708 709 710
			}

			err = throttle(ce, tq, ARRAY_SIZE(tq));
			if (err) {
				intel_context_put(ce);
711
				kernel_context_close(ctx);
712
				goto out_file;
713 714 715 716 717 718 719 720 721
			}

			if (++dw == max_dwords(obj)) {
				obj = NULL;
				dw = 0;
			}

			ndwords++;
			ncontexts++;
722 723

			intel_context_put(ce);
724
			kernel_context_close(ctx);
725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741
		}

		pr_info("Submitted %lu contexts to %s, filling %lu dwords\n",
			ncontexts, engine->name, ndwords);

		ncontexts = dw = 0;
		list_for_each_entry(obj, &objects, st_link) {
			unsigned int rem =
				min_t(unsigned int, ndwords - dw, max_dwords(obj));

			err = cpu_check(obj, ncontexts++, rem);
			if (err)
				break;

			dw += rem;
		}

742
out_file:
743
		throttle_release(tq, ARRAY_SIZE(tq));
744 745 746
		if (igt_live_test_end(&t))
			err = -EIO;

747
		fput(file);
748 749
		if (err)
			return err;
750 751

		i915_gem_drain_freed_objects(i915);
752 753 754 755 756 757 758 759
	}

	return 0;
}

static int igt_shared_ctx_exec(void *arg)
{
	struct drm_i915_private *i915 = arg;
760
	struct i915_request *tq[5] = {};
761 762 763
	struct i915_gem_context *parent;
	struct intel_engine_cs *engine;
	struct igt_live_test t;
764
	struct file *file;
765 766 767 768 769 770 771 772 773 774
	int err = 0;

	/*
	 * Create a few different contexts with the same mm and write
	 * through each ctx using the GPU making sure those writes end
	 * up in the expected pages of our obj.
	 */
	if (!DRIVER_CAPS(i915)->has_logical_contexts)
		return 0;

775 776 777 778
	file = mock_file(i915);
	if (IS_ERR(file))
		return PTR_ERR(file);

779 780 781
	parent = live_context(i915, file);
	if (IS_ERR(parent)) {
		err = PTR_ERR(parent);
782
		goto out_file;
783 784
	}

785
	if (!parent->vm) { /* not full-ppgtt; nothing to share */
786
		err = 0;
787
		goto out_file;
788 789
	}

790
	err = igt_live_test_begin(&t, i915, __func__, "");
791
	if (err)
792
		goto out_file;
793

794
	for_each_uabi_engine(engine, i915) {
795 796 797 798
		unsigned long ncontexts, ndwords, dw;
		struct drm_i915_gem_object *obj = NULL;
		IGT_TIMEOUT(end_time);
		LIST_HEAD(objects);
799

800 801
		if (!intel_engine_can_store_dword(engine))
			continue;
802

803 804 805 806 807
		dw = 0;
		ndwords = 0;
		ncontexts = 0;
		while (!time_after(jiffies, end_time)) {
			struct i915_gem_context *ctx;
808
			struct intel_context *ce;
809

810 811 812 813 814
			ctx = kernel_context(i915);
			if (IS_ERR(ctx)) {
				err = PTR_ERR(ctx);
				goto out_test;
			}
815

816
			mutex_lock(&ctx->mutex);
817
			__assign_ppgtt(ctx, ctx_vm(parent));
818
			mutex_unlock(&ctx->mutex);
819

820
			ce = i915_gem_context_get_engine(ctx, engine->legacy_idx);
821 822
			GEM_BUG_ON(IS_ERR(ce));

823
			if (!obj) {
824 825
				obj = create_test_object(ctx_vm(parent),
							 file, &objects);
826 827
				if (IS_ERR(obj)) {
					err = PTR_ERR(obj);
828
					intel_context_put(ce);
829 830
					kernel_context_close(ctx);
					goto out_test;
831 832 833
				}
			}

834
			err = gpu_fill(ce, obj, dw);
835
			if (err) {
C
Chris Wilson 已提交
836
				pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) [full-ppgtt? %s], err=%d\n",
837
				       ndwords, dw, max_dwords(obj),
C
Chris Wilson 已提交
838
				       engine->name,
839 840
				       yesno(!!rcu_access_pointer(ctx->vm)),
				       err);
841 842 843 844 845 846 847 848 849
				intel_context_put(ce);
				kernel_context_close(ctx);
				goto out_test;
			}

			err = throttle(ce, tq, ARRAY_SIZE(tq));
			if (err) {
				intel_context_put(ce);
				kernel_context_close(ctx);
850
				goto out_test;
851 852
			}

853 854
			if (++dw == max_dwords(obj)) {
				obj = NULL;
855
				dw = 0;
856
			}
857

858
			ndwords++;
859
			ncontexts++;
860 861 862

			intel_context_put(ce);
			kernel_context_close(ctx);
863
		}
864 865
		pr_info("Submitted %lu contexts to %s, filling %lu dwords\n",
			ncontexts, engine->name, ndwords);
866

867 868 869 870
		ncontexts = dw = 0;
		list_for_each_entry(obj, &objects, st_link) {
			unsigned int rem =
				min_t(unsigned int, ndwords - dw, max_dwords(obj));
871

872 873 874
			err = cpu_check(obj, ncontexts++, rem);
			if (err)
				goto out_test;
875

876 877
			dw += rem;
		}
878 879

		i915_gem_drain_freed_objects(i915);
880
	}
881
out_test:
882
	throttle_release(tq, ARRAY_SIZE(tq));
883
	if (igt_live_test_end(&t))
884
		err = -EIO;
885
out_file:
886
	fput(file);
887 888 889
	return err;
}

890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914
static struct i915_vma *rpcs_query_batch(struct i915_vma *vma)
{
	struct drm_i915_gem_object *obj;
	u32 *cmd;
	int err;

	if (INTEL_GEN(vma->vm->i915) < 8)
		return ERR_PTR(-EINVAL);

	obj = i915_gem_object_create_internal(vma->vm->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;
	}

	*cmd++ = MI_STORE_REGISTER_MEM_GEN8;
	*cmd++ = i915_mmio_reg_offset(GEN8_R_PWR_CLK_STATE);
	*cmd++ = lower_32_bits(vma->node.start);
	*cmd++ = upper_32_bits(vma->node.start);
	*cmd = MI_BATCH_BUFFER_END;

915
	__i915_gem_object_flush_map(obj, 0, 64);
916 917
	i915_gem_object_unpin_map(obj);

918 919
	intel_gt_chipset_flush(vma->vm->gt);

920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938
	vma = i915_vma_instance(obj, vma->vm, NULL);
	if (IS_ERR(vma)) {
		err = PTR_ERR(vma);
		goto err;
	}

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

	return vma;

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

static int
emit_rpcs_query(struct drm_i915_gem_object *obj,
939
		struct intel_context *ce,
940 941 942 943 944 945 946
		struct i915_request **rq_out)
{
	struct i915_request *rq;
	struct i915_vma *batch;
	struct i915_vma *vma;
	int err;

947
	GEM_BUG_ON(!intel_engine_can_store_dword(ce->engine));
948

949
	vma = i915_vma_instance(obj, ce->vm, NULL);
950 951 952
	if (IS_ERR(vma))
		return PTR_ERR(vma);

953
	i915_gem_object_lock(obj);
954
	err = i915_gem_object_set_to_gtt_domain(obj, false);
955
	i915_gem_object_unlock(obj);
956 957 958 959 960 961 962 963 964 965 966 967 968
	if (err)
		return err;

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

	batch = rpcs_query_batch(vma);
	if (IS_ERR(batch)) {
		err = PTR_ERR(batch);
		goto err_vma;
	}

969
	rq = i915_request_create(ce);
970 971 972 973 974
	if (IS_ERR(rq)) {
		err = PTR_ERR(rq);
		goto err_batch;
	}

975 976 977
	err = rq->engine->emit_bb_start(rq,
					batch->node.start, batch->node.size,
					0);
978 979 980
	if (err)
		goto err_request;

981
	i915_vma_lock(batch);
982 983 984
	err = i915_request_await_object(rq, batch->obj, false);
	if (err == 0)
		err = i915_vma_move_to_active(batch, rq, 0);
985
	i915_vma_unlock(batch);
986 987 988
	if (err)
		goto skip_request;

989
	i915_vma_lock(vma);
990 991 992
	err = i915_request_await_object(rq, vma->obj, true);
	if (err == 0)
		err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
993
	i915_vma_unlock(vma);
994 995 996
	if (err)
		goto skip_request;

997
	i915_vma_unpin_and_release(&batch, 0);
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010
	i915_vma_unpin(vma);

	*rq_out = i915_request_get(rq);

	i915_request_add(rq);

	return 0;

skip_request:
	i915_request_skip(rq, err);
err_request:
	i915_request_add(rq);
err_batch:
1011
	i915_vma_unpin_and_release(&batch, 0);
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
err_vma:
	i915_vma_unpin(vma);

	return err;
}

#define TEST_IDLE	BIT(0)
#define TEST_BUSY	BIT(1)
#define TEST_RESET	BIT(2)

static int
1023
__sseu_prepare(const char *name,
1024
	       unsigned int flags,
1025
	       struct intel_context *ce,
1026
	       struct igt_spinner **spin)
1027
{
1028 1029
	struct i915_request *rq;
	int ret;
1030

1031 1032 1033
	*spin = NULL;
	if (!(flags & (TEST_BUSY | TEST_RESET)))
		return 0;
1034

1035 1036 1037
	*spin = kzalloc(sizeof(**spin), GFP_KERNEL);
	if (!*spin)
		return -ENOMEM;
1038

1039
	ret = igt_spinner_init(*spin, ce->engine->gt);
1040 1041
	if (ret)
		goto err_free;
1042

1043
	rq = igt_spinner_create_request(*spin, ce, MI_NOOP);
1044 1045 1046 1047
	if (IS_ERR(rq)) {
		ret = PTR_ERR(rq);
		goto err_fini;
	}
1048

1049
	i915_request_add(rq);
1050

1051 1052 1053 1054
	if (!igt_wait_for_spinner(*spin, rq)) {
		pr_err("%s: Spinner failed to start!\n", name);
		ret = -ETIMEDOUT;
		goto err_end;
1055 1056
	}

1057 1058 1059 1060 1061 1062 1063 1064
	return 0;

err_end:
	igt_spinner_end(*spin);
err_fini:
	igt_spinner_fini(*spin);
err_free:
	kfree(fetch_and_zero(spin));
1065 1066 1067 1068
	return ret;
}

static int
1069
__read_slice_count(struct intel_context *ce,
1070 1071 1072 1073 1074 1075 1076 1077 1078 1079
		   struct drm_i915_gem_object *obj,
		   struct igt_spinner *spin,
		   u32 *rpcs)
{
	struct i915_request *rq = NULL;
	u32 s_mask, s_shift;
	unsigned int cnt;
	u32 *buf, val;
	long ret;

1080
	ret = emit_rpcs_query(obj, ce, &rq);
1081 1082 1083 1084 1085 1086
	if (ret)
		return ret;

	if (spin)
		igt_spinner_end(spin);

1087
	ret = i915_request_wait(rq, 0, MAX_SCHEDULE_TIMEOUT);
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097
	i915_request_put(rq);
	if (ret < 0)
		return ret;

	buf = i915_gem_object_pin_map(obj, I915_MAP_WB);
	if (IS_ERR(buf)) {
		ret = PTR_ERR(buf);
		return ret;
	}

1098
	if (INTEL_GEN(ce->engine->i915) >= 11) {
1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140
		s_mask = GEN11_RPCS_S_CNT_MASK;
		s_shift = GEN11_RPCS_S_CNT_SHIFT;
	} else {
		s_mask = GEN8_RPCS_S_CNT_MASK;
		s_shift = GEN8_RPCS_S_CNT_SHIFT;
	}

	val = *buf;
	cnt = (val & s_mask) >> s_shift;
	*rpcs = val;

	i915_gem_object_unpin_map(obj);

	return cnt;
}

static int
__check_rpcs(const char *name, u32 rpcs, int slices, unsigned int expected,
	     const char *prefix, const char *suffix)
{
	if (slices == expected)
		return 0;

	if (slices < 0) {
		pr_err("%s: %s read slice count failed with %d%s\n",
		       name, prefix, slices, suffix);
		return slices;
	}

	pr_err("%s: %s slice count %d is not %u%s\n",
	       name, prefix, slices, expected, suffix);

	pr_info("RPCS=0x%x; %u%sx%u%s\n",
		rpcs, slices,
		(rpcs & GEN8_RPCS_S_CNT_ENABLE) ? "*" : "",
		(rpcs & GEN8_RPCS_SS_CNT_MASK) >> GEN8_RPCS_SS_CNT_SHIFT,
		(rpcs & GEN8_RPCS_SS_CNT_ENABLE) ? "*" : "");

	return -EINVAL;
}

static int
1141
__sseu_finish(const char *name,
1142
	      unsigned int flags,
1143
	      struct intel_context *ce,
1144 1145 1146 1147
	      struct drm_i915_gem_object *obj,
	      unsigned int expected,
	      struct igt_spinner *spin)
{
1148
	unsigned int slices = hweight32(ce->engine->sseu.slice_mask);
1149 1150 1151 1152
	u32 rpcs = 0;
	int ret = 0;

	if (flags & TEST_RESET) {
1153
		ret = intel_engine_reset(ce->engine, "sseu");
1154 1155 1156 1157
		if (ret)
			goto out;
	}

1158
	ret = __read_slice_count(ce, obj,
1159 1160 1161 1162 1163
				 flags & TEST_RESET ? NULL : spin, &rpcs);
	ret = __check_rpcs(name, rpcs, ret, expected, "Context", "!");
	if (ret)
		goto out;

1164
	ret = __read_slice_count(ce->engine->kernel_context, obj, NULL, &rpcs);
1165 1166 1167 1168 1169 1170 1171
	ret = __check_rpcs(name, rpcs, ret, slices, "Kernel context", "!");

out:
	if (spin)
		igt_spinner_end(spin);

	if ((flags & TEST_IDLE) && ret == 0) {
1172
		ret = igt_flush_test(ce->engine->i915);
1173 1174 1175
		if (ret)
			return ret;

1176
		ret = __read_slice_count(ce, obj, NULL, &rpcs);
1177 1178 1179 1180 1181 1182 1183 1184
		ret = __check_rpcs(name, rpcs, ret, expected,
				   "Context", " after idle!");
	}

	return ret;
}

static int
1185
__sseu_test(const char *name,
1186
	    unsigned int flags,
1187
	    struct intel_context *ce,
1188 1189 1190 1191 1192 1193
	    struct drm_i915_gem_object *obj,
	    struct intel_sseu sseu)
{
	struct igt_spinner *spin = NULL;
	int ret;

1194 1195
	intel_engine_pm_get(ce->engine);

1196
	ret = __sseu_prepare(name, flags, ce, &spin);
1197
	if (ret)
1198
		goto out_pm;
1199

1200
	ret = intel_context_reconfigure_sseu(ce, sseu);
1201
	if (ret)
1202
		goto out_spin;
1203

1204
	ret = __sseu_finish(name, flags, ce, obj,
1205 1206
			    hweight32(sseu.slice_mask), spin);

1207
out_spin:
1208 1209 1210 1211 1212
	if (spin) {
		igt_spinner_end(spin);
		igt_spinner_fini(spin);
		kfree(spin);
	}
1213 1214
out_pm:
	intel_engine_pm_put(ce->engine);
1215 1216 1217 1218 1219 1220 1221 1222 1223
	return ret;
}

static int
__igt_ctx_sseu(struct drm_i915_private *i915,
	       const char *name,
	       unsigned int flags)
{
	struct drm_i915_gem_object *obj;
1224
	int inst = 0;
C
Chris Wilson 已提交
1225
	int ret = 0;
1226

1227
	if (INTEL_GEN(i915) < 9 || !RUNTIME_INFO(i915)->sseu.has_slice_pg)
1228 1229 1230
		return 0;

	if (flags & TEST_RESET)
1231
		igt_global_reset_lock(&i915->gt);
1232 1233 1234 1235 1236 1237 1238

	obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
	if (IS_ERR(obj)) {
		ret = PTR_ERR(obj);
		goto out_unlock;
	}

1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
	do {
		struct intel_engine_cs *engine;
		struct intel_context *ce;
		struct intel_sseu pg_sseu;

		engine = intel_engine_lookup_user(i915,
						  I915_ENGINE_CLASS_RENDER,
						  inst++);
		if (!engine)
			break;
1249

1250 1251
		if (hweight32(engine->sseu.slice_mask) < 2)
			continue;
1252

1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
		/*
		 * Gen11 VME friendly power-gated configuration with
		 * half enabled sub-slices.
		 */
		pg_sseu = engine->sseu;
		pg_sseu.slice_mask = 1;
		pg_sseu.subslice_mask =
			~(~0 << (hweight32(engine->sseu.subslice_mask) / 2));

		pr_info("%s: SSEU subtest '%s', flags=%x, def_slices=%u, pg_slices=%u\n",
			engine->name, name, flags,
			hweight32(engine->sseu.slice_mask),
			hweight32(pg_sseu.slice_mask));

		ce = intel_context_create(engine->kernel_context->gem_context,
					  engine);
		if (IS_ERR(ce)) {
			ret = PTR_ERR(ce);
			goto out_put;
		}
1273

1274 1275 1276
		ret = intel_context_pin(ce);
		if (ret)
			goto out_ce;
1277

1278 1279 1280 1281
		/* First set the default mask. */
		ret = __sseu_test(name, flags, ce, obj, engine->sseu);
		if (ret)
			goto out_unpin;
1282

1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
		/* Then set a power-gated configuration. */
		ret = __sseu_test(name, flags, ce, obj, pg_sseu);
		if (ret)
			goto out_unpin;

		/* Back to defaults. */
		ret = __sseu_test(name, flags, ce, obj, engine->sseu);
		if (ret)
			goto out_unpin;

		/* One last power-gated configuration for the road. */
		ret = __sseu_test(name, flags, ce, obj, pg_sseu);
		if (ret)
			goto out_unpin;

out_unpin:
		intel_context_unpin(ce);
out_ce:
		intel_context_put(ce);
	} while (!ret);
1303

1304
	if (igt_flush_test(i915))
1305 1306
		ret = -EIO;

1307
out_put:
1308
	i915_gem_object_put(obj);
1309 1310 1311

out_unlock:
	if (flags & TEST_RESET)
1312
		igt_global_reset_unlock(&i915->gt);
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342

	if (ret)
		pr_err("%s: Failed with %d!\n", name, ret);

	return ret;
}

static int igt_ctx_sseu(void *arg)
{
	struct {
		const char *name;
		unsigned int flags;
	} *phase, phases[] = {
		{ .name = "basic", .flags = 0 },
		{ .name = "idle", .flags = TEST_IDLE },
		{ .name = "busy", .flags = TEST_BUSY },
		{ .name = "busy-reset", .flags = TEST_BUSY | TEST_RESET },
		{ .name = "busy-idle", .flags = TEST_BUSY | TEST_IDLE },
		{ .name = "reset-idle", .flags = TEST_RESET | TEST_IDLE },
	};
	unsigned int i;
	int ret = 0;

	for (i = 0, phase = phases; ret == 0 && i < ARRAY_SIZE(phases);
	     i++, phase++)
		ret = __igt_ctx_sseu(arg, phase->name, phase->flags);

	return ret;
}

1343 1344 1345 1346
static int igt_ctx_readonly(void *arg)
{
	struct drm_i915_private *i915 = arg;
	struct drm_i915_gem_object *obj = NULL;
1347
	struct i915_request *tq[5] = {};
1348
	struct i915_address_space *vm;
1349
	struct i915_gem_context *ctx;
1350
	unsigned long idx, ndwords, dw;
1351
	struct igt_live_test t;
1352 1353 1354
	I915_RND_STATE(prng);
	IGT_TIMEOUT(end_time);
	LIST_HEAD(objects);
1355
	struct file *file;
1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367
	int err = -ENODEV;

	/*
	 * Create a few read-only objects (with the occasional writable object)
	 * and try to write into these object checking that the GPU discards
	 * any write to a read-only object.
	 */

	file = mock_file(i915);
	if (IS_ERR(file))
		return PTR_ERR(file);

1368
	err = igt_live_test_begin(&t, i915, __func__, "");
1369
	if (err)
1370
		goto out_file;
1371

1372
	ctx = live_context(i915, file);
1373 1374
	if (IS_ERR(ctx)) {
		err = PTR_ERR(ctx);
1375
		goto out_file;
1376 1377
	}

1378
	vm = ctx_vm(ctx) ?: &i915->ggtt.alias->vm;
1379
	if (!vm || !vm->has_read_only) {
1380
		err = 0;
1381
		goto out_file;
1382 1383 1384 1385 1386
	}

	ndwords = 0;
	dw = 0;
	while (!time_after(jiffies, end_time)) {
1387 1388
		struct i915_gem_engines_iter it;
		struct intel_context *ce;
1389

1390 1391 1392
		for_each_gem_engine(ce,
				    i915_gem_context_lock_engines(ctx), it) {
			if (!intel_engine_can_store_dword(ce->engine))
1393 1394 1395
				continue;

			if (!obj) {
1396
				obj = create_test_object(ce->vm, file, &objects);
1397 1398
				if (IS_ERR(obj)) {
					err = PTR_ERR(obj);
1399
					i915_gem_context_unlock_engines(ctx);
1400
					goto out_file;
1401 1402
				}

1403 1404
				if (prandom_u32_state(&prng) & 1)
					i915_gem_object_set_readonly(obj);
1405 1406
			}

1407
			err = gpu_fill(ce, obj, dw);
1408
			if (err) {
C
Chris Wilson 已提交
1409
				pr_err("Failed to fill dword %lu [%lu/%lu] with gpu (%s) [full-ppgtt? %s], err=%d\n",
1410
				       ndwords, dw, max_dwords(obj),
1411
				       ce->engine->name,
1412
				       yesno(!!ctx_vm(ctx)),
1413
				       err);
1414
				i915_gem_context_unlock_engines(ctx);
1415
				goto out_file;
1416 1417
			}

1418 1419 1420
			err = throttle(ce, tq, ARRAY_SIZE(tq));
			if (err) {
				i915_gem_context_unlock_engines(ctx);
1421
				goto out_file;
1422 1423
			}

1424 1425 1426 1427 1428 1429
			if (++dw == max_dwords(obj)) {
				obj = NULL;
				dw = 0;
			}
			ndwords++;
		}
1430
		i915_gem_context_unlock_engines(ctx);
1431 1432
	}
	pr_info("Submitted %lu dwords (across %u engines)\n",
1433
		ndwords, RUNTIME_INFO(i915)->num_engines);
1434 1435

	dw = 0;
1436
	idx = 0;
1437 1438 1439 1440 1441 1442
	list_for_each_entry(obj, &objects, st_link) {
		unsigned int rem =
			min_t(unsigned int, ndwords - dw, max_dwords(obj));
		unsigned int num_writes;

		num_writes = rem;
1443
		if (i915_gem_object_is_readonly(obj))
1444 1445
			num_writes = 0;

1446
		err = cpu_check(obj, idx++, num_writes);
1447 1448 1449 1450 1451 1452
		if (err)
			break;

		dw += rem;
	}

1453
out_file:
1454
	throttle_release(tq, ARRAY_SIZE(tq));
1455
	if (igt_live_test_end(&t))
1456 1457
		err = -EIO;

1458
	fput(file);
1459 1460 1461
	return err;
}

1462
static int check_scratch(struct i915_address_space *vm, u64 offset)
1463 1464
{
	struct drm_mm_node *node =
1465
		__drm_mm_interval_first(&vm->mm,
1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482
					offset, offset + sizeof(u32) - 1);
	if (!node || node->start > offset)
		return 0;

	GEM_BUG_ON(offset >= node->start + node->size);

	pr_err("Target offset 0x%08x_%08x overlaps with a node in the mm!\n",
	       upper_32_bits(offset), lower_32_bits(offset));
	return -EINVAL;
}

static int write_to_scratch(struct i915_gem_context *ctx,
			    struct intel_engine_cs *engine,
			    u64 offset, u32 value)
{
	struct drm_i915_private *i915 = ctx->i915;
	struct drm_i915_gem_object *obj;
1483
	struct i915_address_space *vm;
1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510
	struct i915_request *rq;
	struct i915_vma *vma;
	u32 *cmd;
	int err;

	GEM_BUG_ON(offset < I915_GTT_PAGE_SIZE);

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

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

	*cmd++ = MI_STORE_DWORD_IMM_GEN4;
	if (INTEL_GEN(i915) >= 8) {
		*cmd++ = lower_32_bits(offset);
		*cmd++ = upper_32_bits(offset);
	} else {
		*cmd++ = 0;
		*cmd++ = offset;
	}
	*cmd++ = value;
	*cmd = MI_BATCH_BUFFER_END;
1511
	__i915_gem_object_flush_map(obj, 0, 64);
1512 1513
	i915_gem_object_unpin_map(obj);

1514 1515
	intel_gt_chipset_flush(engine->gt);

1516 1517
	vm = i915_gem_context_get_vm_rcu(ctx);
	vma = i915_vma_instance(obj, vm, NULL);
1518 1519
	if (IS_ERR(vma)) {
		err = PTR_ERR(vma);
1520
		goto err_vm;
1521 1522 1523 1524
	}

	err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_OFFSET_FIXED);
	if (err)
1525
		goto err_vm;
1526

1527
	err = check_scratch(vm, offset);
1528 1529 1530
	if (err)
		goto err_unpin;

1531
	rq = igt_request_alloc(ctx, engine);
1532 1533 1534 1535 1536 1537 1538 1539 1540
	if (IS_ERR(rq)) {
		err = PTR_ERR(rq);
		goto err_unpin;
	}

	err = engine->emit_bb_start(rq, vma->node.start, vma->node.size, 0);
	if (err)
		goto err_request;

1541
	i915_vma_lock(vma);
1542 1543 1544
	err = i915_request_await_object(rq, vma->obj, false);
	if (err == 0)
		err = i915_vma_move_to_active(vma, rq, 0);
1545
	i915_vma_unlock(vma);
1546 1547 1548
	if (err)
		goto skip_request;

1549
	i915_vma_unpin_and_release(&vma, 0);
1550 1551 1552

	i915_request_add(rq);

1553
	i915_vm_put(vm);
1554 1555 1556 1557 1558 1559 1560 1561
	return 0;

skip_request:
	i915_request_skip(rq, err);
err_request:
	i915_request_add(rq);
err_unpin:
	i915_vma_unpin(vma);
1562 1563
err_vm:
	i915_vm_put(vm);
1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574
err:
	i915_gem_object_put(obj);
	return err;
}

static int read_from_scratch(struct i915_gem_context *ctx,
			     struct intel_engine_cs *engine,
			     u64 offset, u32 *value)
{
	struct drm_i915_private *i915 = ctx->i915;
	struct drm_i915_gem_object *obj;
1575
	struct i915_address_space *vm;
1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614
	const u32 RCS_GPR0 = 0x2600; /* not all engines have their own GPR! */
	const u32 result = 0x100;
	struct i915_request *rq;
	struct i915_vma *vma;
	u32 *cmd;
	int err;

	GEM_BUG_ON(offset < I915_GTT_PAGE_SIZE);

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

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

	memset(cmd, POISON_INUSE, PAGE_SIZE);
	if (INTEL_GEN(i915) >= 8) {
		*cmd++ = MI_LOAD_REGISTER_MEM_GEN8;
		*cmd++ = RCS_GPR0;
		*cmd++ = lower_32_bits(offset);
		*cmd++ = upper_32_bits(offset);
		*cmd++ = MI_STORE_REGISTER_MEM_GEN8;
		*cmd++ = RCS_GPR0;
		*cmd++ = result;
		*cmd++ = 0;
	} else {
		*cmd++ = MI_LOAD_REGISTER_MEM;
		*cmd++ = RCS_GPR0;
		*cmd++ = offset;
		*cmd++ = MI_STORE_REGISTER_MEM;
		*cmd++ = RCS_GPR0;
		*cmd++ = result;
	}
	*cmd = MI_BATCH_BUFFER_END;

1615 1616
	i915_gem_object_flush_map(obj);
	i915_gem_object_unpin_map(obj);
1617

1618 1619
	intel_gt_chipset_flush(engine->gt);

1620 1621
	vm = i915_gem_context_get_vm_rcu(ctx);
	vma = i915_vma_instance(obj, vm, NULL);
1622 1623
	if (IS_ERR(vma)) {
		err = PTR_ERR(vma);
1624
		goto err_vm;
1625 1626 1627 1628
	}

	err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_OFFSET_FIXED);
	if (err)
1629
		goto err_vm;
1630

1631
	err = check_scratch(vm, offset);
1632 1633 1634
	if (err)
		goto err_unpin;

1635
	rq = igt_request_alloc(ctx, engine);
1636 1637 1638 1639 1640 1641 1642 1643 1644
	if (IS_ERR(rq)) {
		err = PTR_ERR(rq);
		goto err_unpin;
	}

	err = engine->emit_bb_start(rq, vma->node.start, vma->node.size, 0);
	if (err)
		goto err_request;

1645
	i915_vma_lock(vma);
1646 1647 1648
	err = i915_request_await_object(rq, vma->obj, true);
	if (err == 0)
		err = i915_vma_move_to_active(vma, rq, EXEC_OBJECT_WRITE);
1649
	i915_vma_unlock(vma);
1650 1651 1652 1653 1654 1655 1656 1657
	if (err)
		goto skip_request;

	i915_vma_unpin(vma);
	i915_vma_close(vma);

	i915_request_add(rq);

1658
	i915_gem_object_lock(obj);
1659
	err = i915_gem_object_set_to_cpu_domain(obj, false);
1660
	i915_gem_object_unlock(obj);
1661
	if (err)
1662
		goto err_vm;
1663 1664 1665 1666

	cmd = i915_gem_object_pin_map(obj, I915_MAP_WB);
	if (IS_ERR(cmd)) {
		err = PTR_ERR(cmd);
1667
		goto err_vm;
1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681
	}

	*value = cmd[result / sizeof(*cmd)];
	i915_gem_object_unpin_map(obj);
	i915_gem_object_put(obj);

	return 0;

skip_request:
	i915_request_skip(rq, err);
err_request:
	i915_request_add(rq);
err_unpin:
	i915_vma_unpin(vma);
1682 1683
err_vm:
	i915_vm_put(vm);
1684 1685 1686 1687 1688 1689 1690 1691 1692 1693
err:
	i915_gem_object_put(obj);
	return err;
}

static int igt_vm_isolation(void *arg)
{
	struct drm_i915_private *i915 = arg;
	struct i915_gem_context *ctx_a, *ctx_b;
	struct intel_engine_cs *engine;
1694
	struct igt_live_test t;
1695 1696
	I915_RND_STATE(prng);
	unsigned long count;
1697
	struct file *file;
1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712
	u64 vm_total;
	int err;

	if (INTEL_GEN(i915) < 7)
		return 0;

	/*
	 * The simple goal here is that a write into one context is not
	 * observed in a second (separate page tables and scratch).
	 */

	file = mock_file(i915);
	if (IS_ERR(file))
		return PTR_ERR(file);

1713
	err = igt_live_test_begin(&t, i915, __func__, "");
1714
	if (err)
1715
		goto out_file;
1716

1717
	ctx_a = live_context(i915, file);
1718 1719
	if (IS_ERR(ctx_a)) {
		err = PTR_ERR(ctx_a);
1720
		goto out_file;
1721 1722
	}

1723
	ctx_b = live_context(i915, file);
1724 1725
	if (IS_ERR(ctx_b)) {
		err = PTR_ERR(ctx_b);
1726
		goto out_file;
1727 1728 1729
	}

	/* We can only test vm isolation, if the vm are distinct */
1730
	if (ctx_vm(ctx_a) == ctx_vm(ctx_b))
1731
		goto out_file;
1732

1733 1734
	vm_total = ctx_vm(ctx_a)->total;
	GEM_BUG_ON(ctx_vm(ctx_b)->total != vm_total);
1735 1736 1737
	vm_total -= I915_GTT_PAGE_SIZE;

	count = 0;
1738
	for_each_uabi_engine(engine, i915) {
1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750
		IGT_TIMEOUT(end_time);
		unsigned long this = 0;

		if (!intel_engine_can_store_dword(engine))
			continue;

		while (!__igt_timeout(end_time, NULL)) {
			u32 value = 0xc5c5c5c5;
			u64 offset;

			div64_u64_rem(i915_prandom_u64_state(&prng),
				      vm_total, &offset);
1751
			offset = round_down(offset, alignof_dword);
1752 1753 1754 1755 1756 1757 1758 1759
			offset += I915_GTT_PAGE_SIZE;

			err = write_to_scratch(ctx_a, engine,
					       offset, 0xdeadbeef);
			if (err == 0)
				err = read_from_scratch(ctx_b, engine,
							offset, &value);
			if (err)
1760
				goto out_file;
1761 1762 1763 1764 1765 1766 1767 1768

			if (value) {
				pr_err("%s: Read %08x from scratch (offset 0x%08x_%08x), after %lu reads!\n",
				       engine->name, value,
				       upper_32_bits(offset),
				       lower_32_bits(offset),
				       this);
				err = -EINVAL;
1769
				goto out_file;
1770 1771 1772 1773 1774 1775 1776
			}

			this++;
		}
		count += this;
	}
	pr_info("Checked %lu scratch offsets across %d engines\n",
1777
		count, RUNTIME_INFO(i915)->num_engines);
1778

1779
out_file:
1780
	if (igt_live_test_end(&t))
1781
		err = -EIO;
1782
	fput(file);
1783 1784 1785
	return err;
}

1786 1787 1788 1789 1790
static bool skip_unused_engines(struct intel_context *ce, void *data)
{
	return !ce->state;
}

1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813
static void mock_barrier_task(void *data)
{
	unsigned int *counter = data;

	++*counter;
}

static int mock_context_barrier(void *arg)
{
#undef pr_fmt
#define pr_fmt(x) "context_barrier_task():" # x
	struct drm_i915_private *i915 = arg;
	struct i915_gem_context *ctx;
	struct i915_request *rq;
	unsigned int counter;
	int err;

	/*
	 * The context barrier provides us with a callback after it emits
	 * a request; useful for retiring old state after loading new.
	 */

	ctx = mock_context(i915, "mock");
1814 1815
	if (!ctx)
		return -ENOMEM;
1816 1817

	counter = 0;
1818
	err = context_barrier_task(ctx, 0,
1819
				   NULL, NULL, mock_barrier_task, &counter);
1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830
	if (err) {
		pr_err("Failed at line %d, err=%d\n", __LINE__, err);
		goto out;
	}
	if (counter == 0) {
		pr_err("Did not retire immediately with 0 engines\n");
		err = -EINVAL;
		goto out;
	}

	counter = 0;
1831
	err = context_barrier_task(ctx, ALL_ENGINES,
1832 1833 1834 1835
				   skip_unused_engines,
				   NULL,
				   mock_barrier_task,
				   &counter);
1836 1837 1838 1839 1840
	if (err) {
		pr_err("Failed at line %d, err=%d\n", __LINE__, err);
		goto out;
	}
	if (counter == 0) {
1841
		pr_err("Did not retire immediately for all unused engines\n");
1842 1843 1844 1845
		err = -EINVAL;
		goto out;
	}

1846
	rq = igt_request_alloc(ctx, i915->engine[RCS0]);
1847 1848 1849 1850 1851 1852 1853 1854
	if (IS_ERR(rq)) {
		pr_err("Request allocation failed!\n");
		goto out;
	}
	i915_request_add(rq);

	counter = 0;
	context_barrier_inject_fault = BIT(RCS0);
1855
	err = context_barrier_task(ctx, ALL_ENGINES,
1856
				   NULL, NULL, mock_barrier_task, &counter);
1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869
	context_barrier_inject_fault = 0;
	if (err == -ENXIO)
		err = 0;
	else
		pr_err("Did not hit fault injection!\n");
	if (counter != 0) {
		pr_err("Invoked callback on error!\n");
		err = -EIO;
	}
	if (err)
		goto out;

	counter = 0;
1870
	err = context_barrier_task(ctx, ALL_ENGINES,
1871 1872 1873 1874
				   skip_unused_engines,
				   NULL,
				   mock_barrier_task,
				   &counter);
1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892
	if (err) {
		pr_err("Failed at line %d, err=%d\n", __LINE__, err);
		goto out;
	}
	mock_device_flush(i915);
	if (counter == 0) {
		pr_err("Did not retire on each active engines\n");
		err = -EINVAL;
		goto out;
	}

out:
	mock_context_close(ctx);
	return err;
#undef pr_fmt
#define pr_fmt(x) x
}

1893 1894 1895
int i915_gem_context_mock_selftests(void)
{
	static const struct i915_subtest tests[] = {
1896
		SUBTEST(mock_context_barrier),
1897 1898 1899 1900 1901 1902 1903 1904 1905 1906
	};
	struct drm_i915_private *i915;
	int err;

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

	err = i915_subtests(tests, i915);

1907
	drm_dev_put(&i915->drm);
1908 1909 1910
	return err;
}

1911
int i915_gem_context_live_selftests(struct drm_i915_private *i915)
1912 1913
{
	static const struct i915_subtest tests[] = {
1914
		SUBTEST(live_nop_switch),
1915
		SUBTEST(live_parallel_switch),
1916
		SUBTEST(igt_ctx_exec),
1917
		SUBTEST(igt_ctx_readonly),
1918
		SUBTEST(igt_ctx_sseu),
1919
		SUBTEST(igt_shared_ctx_exec),
1920
		SUBTEST(igt_vm_isolation),
1921
	};
1922

1923
	if (intel_gt_is_wedged(&i915->gt))
1924 1925
		return 0;

1926
	return i915_live_subtests(tests, i915);
1927
}