gem_busy.c 15.1 KB
Newer Older
Chris Wilson's avatar
Chris Wilson committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * 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.
 */

24 25 26 27
#include <sched.h>
#include <signal.h>
#include <sys/ioctl.h>

Chris Wilson's avatar
Chris Wilson committed
28
#include "igt.h"
29
#include "igt_rand.h"
30
#include "igt_vgem.h"
31
#include "i915/gem_ring.h"
Chris Wilson's avatar
Chris Wilson committed
32

33
#define LOCAL_EXEC_NO_RELOC (1<<11)
34
#define PAGE_ALIGN(x) ALIGN(x, 4096)
35

Chris Wilson's avatar
Chris Wilson committed
36 37 38 39 40
/* Exercise the busy-ioctl, ensuring the ABI is never broken */
IGT_TEST_DESCRIPTION("Basic check of busy-ioctl ABI.");

enum { TEST = 0, BUSY, BATCH };

41 42 43 44 45 46 47 48 49 50 51 52
static bool gem_busy(int fd, uint32_t handle)
{
	struct drm_i915_gem_busy busy;

	memset(&busy, 0, sizeof(busy));
	busy.handle = handle;

	do_ioctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);

	return busy.busy != 0;
}

Chris Wilson's avatar
Chris Wilson committed
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
static void __gem_busy(int fd,
		       uint32_t handle,
		       uint32_t *read,
		       uint32_t *write)
{
	struct drm_i915_gem_busy busy;

	memset(&busy, 0, sizeof(busy));
	busy.handle = handle;

	do_ioctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);

	*write = busy.busy & 0xffff;
	*read = busy.busy >> 16;
}

static bool exec_noop(int fd,
		      uint32_t *handles,
		      unsigned ring,
		      bool write)
{
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 exec[3];

	memset(exec, 0, sizeof(exec));
	exec[0].handle = handles[BUSY];
	exec[1].handle = handles[TEST];
	if (write)
		exec[1].flags |= EXEC_OBJECT_WRITE;
	exec[2].handle = handles[BATCH];

	memset(&execbuf, 0, sizeof(execbuf));
85
	execbuf.buffers_ptr = to_user_pointer(exec);
Chris Wilson's avatar
Chris Wilson committed
86 87 88 89 90 91 92 93 94 95 96 97 98 99
	execbuf.buffer_count = 3;
	execbuf.flags = ring;
	igt_debug("Queuing handle for %s on ring %d\n",
		  write ? "writing" : "reading", ring & 0x7);
	return __gem_execbuf(fd, &execbuf) == 0;
}

static bool still_busy(int fd, uint32_t handle)
{
	uint32_t read, write;
	__gem_busy(fd, handle, &read, &write);
	return write;
}

100
static void semaphore(int fd, unsigned ring, uint32_t flags)
Chris Wilson's avatar
Chris Wilson committed
101 102
{
	uint32_t bbe = MI_BATCH_BUFFER_END;
103
	igt_spin_t *spin;
Chris Wilson's avatar
Chris Wilson committed
104 105 106 107 108
	uint32_t handle[3];
	uint32_t read, write;
	uint32_t active;
	unsigned i;

109 110
	gem_require_ring(fd, ring | flags);

Chris Wilson's avatar
Chris Wilson committed
111 112 113 114 115
	handle[TEST] = gem_create(fd, 4096);
	handle[BATCH] = gem_create(fd, 4096);
	gem_write(fd, handle[BATCH], 0, &bbe, sizeof(bbe));

	/* Create a long running batch which we can use to hog the GPU */
116
	handle[BUSY] = gem_create(fd, 4096);
117 118 119
	spin = igt_spin_batch_new(fd,
				  .engine = ring,
				  .dependency = handle[BUSY]);
Chris Wilson's avatar
Chris Wilson committed
120 121

	/* Queue a batch after the busy, it should block and remain "busy" */
122
	igt_assert(exec_noop(fd, handle, ring | flags, false));
Chris Wilson's avatar
Chris Wilson committed
123 124 125 126 127 128
	igt_assert(still_busy(fd, handle[BUSY]));
	__gem_busy(fd, handle[TEST], &read, &write);
	igt_assert_eq(read, 1 << ring);
	igt_assert_eq(write, 0);

	/* Requeue with a write */
129
	igt_assert(exec_noop(fd, handle, ring | flags, true));
Chris Wilson's avatar
Chris Wilson committed
130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
	igt_assert(still_busy(fd, handle[BUSY]));
	__gem_busy(fd, handle[TEST], &read, &write);
	igt_assert_eq(read, 1 << ring);
	igt_assert_eq(write, ring);

	/* Now queue it for a read across all available rings */
	active = 0;
	for (i = I915_EXEC_RENDER; i <= I915_EXEC_VEBOX; i++) {
		if (exec_noop(fd, handle, i | flags, false))
			active |= 1 << i;
	}
	igt_assert(still_busy(fd, handle[BUSY]));
	__gem_busy(fd, handle[TEST], &read, &write);
	igt_assert_eq(read, active);
	igt_assert_eq(write, ring); /* from the earlier write */

	/* Check that our long batch was long enough */
	igt_assert(still_busy(fd, handle[BUSY]));
148
	igt_spin_batch_free(fd, spin);
Chris Wilson's avatar
Chris Wilson committed
149 150 151 152 153 154 155 156 157 158 159

	/* And make sure it becomes idle again */
	gem_sync(fd, handle[TEST]);
	__gem_busy(fd, handle[TEST], &read, &write);
	igt_assert_eq(read, 0);
	igt_assert_eq(write, 0);

	for (i = TEST; i <= BATCH; i++)
		gem_close(fd, handle[i]);
}

160
#define PARALLEL 1
161
#define HANG 2
162
static void one(int fd, unsigned ring, unsigned test_flags)
163 164
{
	const int gen = intel_gen(intel_get_drm_devid(fd));
165 166 167 168
	struct drm_i915_gem_exec_object2 obj[2];
#define SCRATCH 0
#define BATCH 1
	struct drm_i915_gem_relocation_entry store[1024+1];
169
	struct drm_i915_gem_execbuffer2 execbuf;
170
	unsigned size = ALIGN(ARRAY_SIZE(store)*16 + 4, 4096);
171
	const unsigned uabi = ring & 63;
172 173
	uint32_t read[2], write[2];
	struct timespec tv;
174
	uint32_t *batch, *bbe;
175
	int i, count, timeout;
176 177

	memset(&execbuf, 0, sizeof(execbuf));
178
	execbuf.buffers_ptr = to_user_pointer(obj);
179
	execbuf.buffer_count = 2;
180
	execbuf.flags = ring;
181 182 183 184
	if (gen < 6)
		execbuf.flags |= I915_EXEC_SECURE;

	memset(obj, 0, sizeof(obj));
185
	obj[SCRATCH].handle = gem_create(fd, 4096);
186

187
	obj[BATCH].handle = gem_create(fd, size);
188
	obj[BATCH].relocs_ptr = to_user_pointer(store);
189
	obj[BATCH].relocation_count = ARRAY_SIZE(store);
190
	memset(store, 0, sizeof(store));
191

192 193 194
	batch = gem_mmap__wc(fd, obj[BATCH].handle, 0, size, PROT_WRITE);
	gem_set_domain(fd, obj[BATCH].handle,
			I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT);
195 196

	i = 0;
197 198
	for (count = 0; count < 1024; count++) {
		store[count].target_handle = obj[SCRATCH].handle;
199 200 201 202 203
		store[count].presumed_offset = -1;
		store[count].offset = sizeof(uint32_t) * (i + 1);
		store[count].delta = sizeof(uint32_t) * count;
		store[count].read_domains = I915_GEM_DOMAIN_INSTRUCTION;
		store[count].write_domain = I915_GEM_DOMAIN_INSTRUCTION;
204 205 206 207 208 209 210
		batch[i] = MI_STORE_DWORD_IMM | (gen < 6 ? 1 << 22 : 0);
		if (gen >= 8) {
			batch[++i] = 0;
			batch[++i] = 0;
		} else if (gen >= 4) {
			batch[++i] = 0;
			batch[++i] = 0;
211
			store[count].offset += sizeof(uint32_t);
212 213 214 215
		} else {
			batch[i]--;
			batch[++i] = 0;
		}
216
		batch[++i] = count;
217
		i++;
218
	}
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244

	bbe = &batch[i];
	store[count].target_handle = obj[BATCH].handle; /* recurse */
	store[count].presumed_offset = 0;
	store[count].offset = sizeof(uint32_t) * (i + 1);
	store[count].delta = 0;
	store[count].read_domains = I915_GEM_DOMAIN_COMMAND;
	store[count].write_domain = 0;
	batch[i] = MI_BATCH_BUFFER_START;
	if (gen >= 8) {
		batch[i] |= 1 << 8 | 1;
		batch[++i] = 0;
		batch[++i] = 0;
	} else if (gen >= 6) {
		batch[i] |= 1 << 8;
		batch[++i] = 0;
	} else {
		batch[i] |= 2 << 6;
		batch[++i] = 0;
		if (gen < 4) {
			batch[i] |= 1;
			store[count].delta = 1;
		}
	}
	i++;

245
	igt_assert(i < size/sizeof(*batch));
246
	igt_require(__gem_execbuf(fd, &execbuf) == 0);
247

248 249
	__gem_busy(fd, obj[SCRATCH].handle, &read[SCRATCH], &write[SCRATCH]);
	__gem_busy(fd, obj[BATCH].handle, &read[BATCH], &write[BATCH]);
250

251
	if (test_flags & PARALLEL) {
252
		unsigned other;
253

254 255
		for_each_physical_engine(fd, other) {
			if (other == ring)
256 257
				continue;

258
			if (!gem_can_store_dword(fd, other))
259 260
				continue;

261 262
			igt_debug("Testing %s in parallel\n", e__->name);
			one(fd, other, 0);
263 264 265
		}
	}

266 267 268 269 270 271
	timeout = 120;
	if ((test_flags & HANG) == 0) {
		*bbe = MI_BATCH_BUFFER_END;
		__sync_synchronize();
		timeout = 1;
	}
272

273 274
	igt_assert_eq(write[SCRATCH], uabi);
	igt_assert_eq_u32(read[SCRATCH], 1 << uabi);
275

276
	igt_assert_eq(write[BATCH], 0);
277
	igt_assert_eq_u32(read[BATCH], 1 << uabi);
278 279 280

	/* Calling busy in a loop should be enough to flush the rendering */
	memset(&tv, 0, sizeof(tv));
281
	while (gem_busy(fd, obj[BATCH].handle))
282
		igt_assert(igt_seconds_elapsed(&tv) < timeout);
283
	igt_assert(!gem_busy(fd, obj[SCRATCH].handle));
284

285 286
	munmap(batch, size);
	batch = gem_mmap__wc(fd, obj[SCRATCH].handle, 0, 4096, PROT_READ);
287 288 289 290
	for (i = 0; i < 1024; i++)
		igt_assert_eq_u32(batch[i], i);
	munmap(batch, 4096);

291 292
	gem_close(fd, obj[BATCH].handle);
	gem_close(fd, obj[SCRATCH].handle);
293 294
}

295 296 297 298 299 300 301 302 303 304
static void xchg_u32(void *array, unsigned i, unsigned j)
{
	uint32_t *u32 = array;
	uint32_t tmp = u32[i];
	u32[i] = u32[j];
	u32[j] = tmp;
}

static void close_race(int fd)
{
305
	const unsigned int ncpus = sysconf(_SC_NPROCESSORS_ONLN);
306
	const unsigned int nhandles = gem_measure_ring_inflight(fd, ALL_ENGINES, 0) / 2;
307
	unsigned int engines[16], nengine;
308
	unsigned long *control;
309
	uint32_t *handles;
310 311
	int i;

312 313
	igt_require(ncpus > 1);
	intel_require_memory(nhandles, 4096, CHECK_RAM);
314

315 316
	/*
	 * One thread spawning work and randomly closing handles.
317 318 319
	 * One background thread per cpu checking busyness.
	 */

320 321 322 323 324
	nengine = 0;
	for_each_engine(fd, i)
		engines[nengine++] = i;
	igt_require(nengine);

325 326 327
	control = mmap(NULL, 4096, PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
	igt_assert(control != MAP_FAILED);

328
	handles = mmap(NULL, PAGE_ALIGN(nhandles*sizeof(*handles)),
329 330 331
		   PROT_WRITE, MAP_SHARED | MAP_ANON, -1, 0);
	igt_assert(handles != MAP_FAILED);

332
	igt_fork(child, ncpus - 1) {
333
		struct drm_i915_gem_busy busy;
334 335
		uint32_t indirection[nhandles];
		unsigned long count = 0;
336

337
		for (i = 0; i < nhandles; i++)
338 339 340 341 342 343
			indirection[i] = i;

		hars_petruska_f54_1_random_perturb(child);

		memset(&busy, 0, sizeof(busy));
		do {
344
			igt_permute_array(indirection, nhandles, xchg_u32);
345
			__sync_synchronize();
346 347
			for (i = 0; i < nhandles; i++) {
				busy.handle = handles[indirection[i]];
348 349 350 351 352 353 354 355 356
				/* Check that the busy computation doesn't
				 * explode in the face of random gem_close().
				 */
				drmIoctl(fd, DRM_IOCTL_I915_GEM_BUSY, &busy);
			}
			count++;
		} while(*(volatile long *)control == 0);

		igt_debug("child[%d]: count = %lu\n", child, count);
357
		control[child + 1] = count;
358 359
	}

360 361 362 363 364 365 366 367
	igt_fork(child, 1) {
		struct sched_param rt = {.sched_priority = 99 };
		igt_spin_t *spin[nhandles];
		unsigned long count = 0;

		igt_assert(sched_setscheduler(getpid(), SCHED_RR, &rt) == 0);

		for (i = 0; i < nhandles; i++) {
368 369
			spin[i] = __igt_spin_batch_new(fd,
						       .engine = engines[rand() % nengine]);
370 371
			handles[i] = spin[i]->handle;
		}
372

373 374 375
		igt_until_timeout(20) {
			for (i = 0; i < nhandles; i++) {
				igt_spin_batch_free(fd, spin[i]);
376 377
				spin[i] = __igt_spin_batch_new(fd,
							       .engine = engines[rand() % nengine]);
378 379 380 381 382 383
				handles[i] = spin[i]->handle;
				__sync_synchronize();
			}
			count += nhandles;
		}
		control[0] = count;
384 385
		__sync_synchronize();

386 387
		for (i = 0; i < nhandles; i++)
			igt_spin_batch_free(fd, spin[i]);
388 389 390
	}
	igt_waitchildren();

391 392
	for (i = 0; i < ncpus - 1; i++)
		control[ncpus] += control[i + 1];
393
	igt_info("Total execs %lu, busy-ioctls %lu\n",
394
		 control[0], control[ncpus] * nhandles);
395

396
	munmap(handles, PAGE_ALIGN(nhandles * sizeof(*handles)));
397
	munmap(control, 4096);
398 399

	gem_quiescent_gpu(fd);
400 401
}

Chris Wilson's avatar
Chris Wilson committed
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
static bool has_semaphores(int fd)
{
	struct drm_i915_getparam gp;
	int val = -1;

	memset(&gp, 0, sizeof(gp));
	gp.param = I915_PARAM_HAS_SEMAPHORES;
	gp.value = &val;

	drmIoctl(fd, DRM_IOCTL_I915_GETPARAM, &gp);
	errno = 0;

	return val > 0;
}

417 418
static bool has_extended_busy_ioctl(int fd)
{
419
	igt_spin_t *spin = igt_spin_batch_new(fd, .engine = I915_EXEC_RENDER);
420 421
	uint32_t read, write;

422 423
	__gem_busy(fd, spin->handle, &read, &write);
	igt_spin_batch_free(fd, spin);
424 425 426 427 428 429

	return read != 0;
}

static void basic(int fd, unsigned ring, unsigned flags)
{
430 431 432 433
	igt_spin_t *spin =
		igt_spin_batch_new(fd,
				   .engine = ring,
				   .flags = IGT_SPIN_NO_PREEMPTION);
434
	struct timespec tv;
435
	int timeout;
436 437
	bool busy;

438
	busy = gem_bo_busy(fd, spin->handle);
439 440 441

	timeout = 120;
	if ((flags & HANG) == 0) {
442
		igt_spin_batch_end(spin);
443 444 445 446 447
		timeout = 1;
	}

	igt_assert(busy);
	memset(&tv, 0, sizeof(tv));
448 449 450 451 452 453 454 455 456 457
	while (gem_bo_busy(fd, spin->handle)) {
		if (igt_seconds_elapsed(&tv) > timeout) {
			igt_debugfs_dump(fd, "i915_engine_info");
			igt_debugfs_dump(fd, "i915_hangcheck_info");
			igt_assert_f(igt_seconds_elapsed(&tv) < timeout,
				     "%s batch did not complete within %ds\n",
				     flags & HANG ? "Hanging" : "Normal",
				     timeout);
		}
	}
458

459
	igt_spin_batch_free(fd, spin);
460 461
}

Chris Wilson's avatar
Chris Wilson committed
462 463
igt_main
{
464
	const struct intel_execution_engine *e;
Chris Wilson's avatar
Chris Wilson committed
465 466
	int fd = -1;

467
	igt_fixture {
468
		fd = drm_open_driver_master(DRIVER_INTEL);
469
		igt_require_gem(fd);
470
		igt_require(gem_can_store_dword(fd, 0));
471 472
	}

473
	igt_subtest_group {
474
		igt_fixture {
475
			igt_fork_hang_detector(fd);
476 477 478
		}

		for (e = intel_execution_engines; e->name; e++) {
479 480
			igt_subtest_group {
				igt_subtest_f("%sbusy-%s",
481
					      e->exec_id == 0 ? "basic-" : "",
482 483 484 485 486
					      e->name) {
					igt_require(gem_has_ring(fd, e->exec_id | e->flags));
					gem_quiescent_gpu(fd);
					basic(fd, e->exec_id | e->flags, 0);
				}
487 488 489
			}
		}

490 491 492 493
		igt_subtest_group {
			igt_fixture {
				igt_require(has_extended_busy_ioctl(fd));
				gem_require_mmap_wc(fd);
494
			}
Chris Wilson's avatar
Chris Wilson committed
495

496 497 498 499 500 501
			for (e = intel_execution_engines; e->name; e++) {
				/* default exec-id is purely symbolic */
				if (e->exec_id == 0)
					continue;

				igt_subtest_f("extended-%s", e->name) {
502
					igt_require(gem_ring_has_physical_engine(fd, e->exec_id | e->flags));
503
					igt_require(gem_can_store_dword(fd, e->exec_id | e->flags));
504
					gem_quiescent_gpu(fd);
505
					one(fd, e->exec_id | e->flags, 0);
506 507 508 509 510 511 512 513 514 515
					gem_quiescent_gpu(fd);
				}
			}

			for (e = intel_execution_engines; e->name; e++) {
				/* default exec-id is purely symbolic */
				if (e->exec_id == 0)
					continue;

				igt_subtest_f("extended-parallel-%s", e->name) {
516
					igt_require(gem_ring_has_physical_engine(fd, e->exec_id | e->flags));
517 518
					igt_require(gem_can_store_dword(fd, e->exec_id | e->flags));

519
					gem_quiescent_gpu(fd);
520
					one(fd, e->exec_id | e->flags, PARALLEL);
521 522 523
					gem_quiescent_gpu(fd);
				}
			}
524
		}
525

526 527 528 529 530 531 532 533 534 535
		igt_subtest_group {
			igt_fixture {
				igt_require(has_extended_busy_ioctl(fd));
				igt_require(has_semaphores(fd));
			}

			for (e = intel_execution_engines; e->name; e++) {
				/* default exec-id is purely symbolic */
				if (e->exec_id == 0)
					continue;
536

537 538 539
				igt_subtest_f("extended-semaphore-%s", e->name)
					semaphore(fd, e->exec_id, e->flags);
			}
540
		}
Chris Wilson's avatar
Chris Wilson committed
541

542 543 544
		igt_subtest("close-race")
			close_race(fd);

545 546 547
		igt_fixture {
			igt_stop_hang_detector();
		}
548 549
	}

550 551 552 553 554 555 556 557
	igt_subtest_group {
		igt_hang_t hang;

		igt_fixture {
			hang = igt_allow_hang(fd, 0, 0);
		}

		for (e = intel_execution_engines; e->name; e++) {
558
			igt_subtest_f("%shang-%s",
559
				      e->exec_id == 0 ? "basic-" : "",
560
				      e->name) {
561
				igt_skip_on_simulation();
562 563
				igt_require(gem_has_ring(fd, e->exec_id | e->flags));
				gem_quiescent_gpu(fd);
564
				basic(fd, e->exec_id | e->flags, HANG);
565 566
			}
		}
567

568 569 570 571 572
		igt_subtest_group {
			igt_fixture {
				igt_require(has_extended_busy_ioctl(fd));
				gem_require_mmap_wc(fd);
			}
573

574 575 576 577 578 579
			for (e = intel_execution_engines; e->name; e++) {
				/* default exec-id is purely symbolic */
				if (e->exec_id == 0)
					continue;

				igt_subtest_f("extended-hang-%s", e->name) {
580
					igt_skip_on_simulation();
581
					igt_require(gem_ring_has_physical_engine(fd, e->exec_id | e->flags));
582 583
					igt_require(gem_can_store_dword(fd, e->exec_id | e->flags));

584
					gem_quiescent_gpu(fd);
585
					one(fd, e->exec_id | e->flags, HANG);
586 587
					gem_quiescent_gpu(fd);
				}
588 589
			}
		}
590 591 592 593

		igt_fixture {
			igt_disallow_hang(fd, hang);
		}
594 595 596
	}

	igt_fixture {
Chris Wilson's avatar
Chris Wilson committed
597
		close(fd);
598
	}
Chris Wilson's avatar
Chris Wilson committed
599
}