igt_gt.c 16.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
/*
 * Copyright © 2014 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
#include <limits.h>
25
#include <string.h>
Arkadiusz Hiler's avatar
Arkadiusz Hiler committed
26
#include <strings.h>
27
#include <signal.h>
28
#include <errno.h>
29 30
#include <sys/types.h>
#include <sys/stat.h>
31
#include <sys/ioctl.h>
32
#include <fcntl.h>
33

34
#include "drmtest.h"
35
#include "igt_aux.h"
36 37
#include "igt_core.h"
#include "igt_gt.h"
38
#include "igt_sysfs.h"
39 40 41
#include "igt_debugfs.h"
#include "ioctl_wrappers.h"
#include "intel_reg.h"
42
#include "intel_chipset.h"
43
#include "igt_dummyload.h"
44

45 46 47
/**
 * SECTION:igt_gt
 * @short_description: GT support library
48
 * @title: GT
49
 * @include: igt.h
50 51 52 53 54 55
 *
 * This library provides various auxiliary helper functions to handle general
 * interactions with the GT like forcewake handling, injecting hangs or stopping
 * engines.
 */

56 57
static bool has_gpu_reset(int fd)
{
58 59 60 61
	static int once = -1;
	if (once < 0) {
		struct drm_i915_getparam gp;
		int val = 0;
62

63 64 65
		memset(&gp, 0, sizeof(gp));
		gp.param = 35; /* HAS_GPU_RESET */
		gp.value = &val;
66

67 68 69 70 71 72
		if (ioctl(fd, DRM_IOCTL_I915_GETPARAM, &gp))
			once = intel_gen(intel_get_drm_devid(fd)) >= 5;
		else
			once = val > 0;
	}
	return once;
73
}
74

75
static void eat_error_state(int dev)
76
{
77
	int dir;
78

79 80 81 82 83 84 85
	dir = igt_sysfs_open(dev, NULL);
	if (dir < 0)
		return;

	/* Any write to the error state clears it */
	igt_sysfs_set(dir, "error", "");
	close(dir);
86 87
}

88 89 90 91 92 93 94 95
/**
 * igt_require_hang_ring:
 * @fd: open i915 drm file descriptor
 * @ring: execbuf ring flag
 *
 * Convenience helper to check whether advanced hang injection is supported by
 * the kernel. Uses igt_skip to automatically skip the test/subtest if this
 * isn't the case.
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
 *
 * Note that we can't simply just call this from igt_hang_ring since some
 * tests want to exercise gpu wedging behavior. For which we intentionally
 * disable gpu reset support, but still want to inject a hang, see for example
 * tests/gem_eio.c Instead, we expect that the first invocation of
 * igt_require_hand_ring be from a vanilla context and use the has_gpu_reset()
 * determined then for all later instances. This allows us the convenience
 * of double checking when injecting hangs, whilst pushing the complexity
 * to the tests that are deliberating trying to break the box.
 *
 * This function is also controlled by the environment variables:
 *
 * IGT_HANG (boolean) - if false, skip all tests that try to inject a hang.
 * Default: true
 *
 * IGT_HANG_WITHOUT_RESET (boolean) - if true, allow the hang even if the
 * kernel does not support GPU recovery. The machine will be wedged afterwards
 * (and so require a reboot between testing), but it does allow limited testing
 * to be done under hang injection.
 * Default: false
116
 */
117
void igt_require_hang_ring(int fd, int ring)
118
{
119 120 121 122
	if (!igt_check_boolean_env_var("IGT_HANG", true))
		igt_skip("hang injection disabled by user");

	gem_require_ring(fd, ring);
123
	gem_context_require_bannable(fd);
124 125
	if (!igt_check_boolean_env_var("IGT_HANG_WITHOUT_RESET", false))
		igt_require(has_gpu_reset(fd));
126 127
}

128 129
static unsigned context_get_ban(int fd, unsigned ctx)
{
130 131 132 133
	struct drm_i915_gem_context_param param = {
		.ctx_id = ctx,
		.param = I915_CONTEXT_PARAM_BANNABLE,
	};
134 135 136

	if (__gem_context_get_param(fd, &param) == -EINVAL) {
		igt_assert(param.value == 0);
137
		param.param = I915_CONTEXT_PARAM_BAN_PERIOD;
138 139 140 141 142 143 144 145
		gem_context_get_param(fd, &param);
	}

	return param.value;
}

static void context_set_ban(int fd, unsigned ctx, unsigned ban)
{
146 147 148 149 150
	struct drm_i915_gem_context_param param = {
		.ctx_id = ctx,
		.param = I915_CONTEXT_PARAM_BANNABLE,
		.value = ban,
	};
151 152 153

	if(__gem_context_set_param(fd, &param) == -EINVAL) {
		igt_assert(param.value == ban);
154
		param.param = I915_CONTEXT_PARAM_BAN_PERIOD;
155 156 157 158
		gem_context_set_param(fd, &param);
	}
}

159 160
igt_hang_t igt_allow_hang(int fd, unsigned ctx, unsigned flags)
{
161 162 163
	struct drm_i915_gem_context_param param = {
		.ctx_id = ctx,
	};
164 165
	unsigned ban;

166 167 168 169 170 171 172
	/*
	 * If the driver is already wedged, we don't expect it to be able
	 * to recover from reset and for it to remain wedged. It's hard to
	 * say even if we do hang/reset making the test suspect.
	 */
	igt_require_gem(fd);

173 174
	if (!igt_check_boolean_env_var("IGT_HANG", true))
		igt_skip("hang injection disabled by user");
175
	gem_context_require_bannable(fd);
176 177 178
	if (!igt_check_boolean_env_var("IGT_HANG_WITHOUT_RESET", false))
		igt_require(has_gpu_reset(fd));

179 180
	igt_require(igt_sysfs_set_parameter
		    (fd, "reset", "%d", INT_MAX /* any reset method */));
181

182
	if ((flags & HANG_ALLOW_CAPTURE) == 0) {
183
		param.param = I915_CONTEXT_PARAM_NO_ERROR_CAPTURE;
184 185 186 187 188 189 190 191
		param.value = 1;
		/* Older kernels may not have NO_ERROR_CAPTURE, in which case
		 * we just eat the error state in post-hang (and hope we eat
		 * the right one).
		 */
		__gem_context_set_param(fd, &param);
	}

192 193 194
	ban = context_get_ban(fd, ctx);
	if ((flags & HANG_ALLOW_BAN) == 0)
		context_set_ban(fd, ctx, 0);
195 196 197 198 199 200

	return (struct igt_hang){ 0, ctx, ban, flags };
}

void igt_disallow_hang(int fd, igt_hang_t arg)
{
201
	context_set_ban(fd, arg.ctx, arg.ban);
202 203

	if ((arg.flags & HANG_ALLOW_CAPTURE) == 0) {
204 205 206
		struct drm_i915_gem_context_param param = {
			.ctx_id = arg.ctx,
			.param = I915_CONTEXT_PARAM_NO_ERROR_CAPTURE,
207 208 209 210 211
			.value = 0,
		};
		__gem_context_set_param(fd, &param);

		eat_error_state(fd);
212 213 214
	}
}

215 216 217 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 245 246 247 248 249 250 251 252
/**
 * has_ctx_exec:
 * @fd: open i915 drm file descriptor
 * @ring: execbuf ring flag
 * @ctx: Context to be checked
 *
 * This helper function checks if non default context submission is allowed
 * on a ring.
 *
 * Returns:
 * True if allowed
 *
 */
static bool has_ctx_exec(int fd, unsigned ring, uint32_t ctx)
{
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 exec;

	/* silly ABI, the kernel thinks everyone who has BSD also has BSD2 */
	if ((ring & ~(3<<13)) == I915_EXEC_BSD) {
		if (ring & (3 << 13) && !gem_has_bsd2(fd))
			return false;
	}

	memset(&exec, 0, sizeof(exec));
	memset(&execbuf, 0, sizeof(execbuf));
	execbuf.buffers_ptr = to_user_pointer(&exec);
	execbuf.buffer_count = 1;
	execbuf.flags = ring;
	execbuf.rsvd1 = ctx;
	/*
	 * If context submission is not allowed, this will return EINVAL
	 * Otherwise, this will return ENOENT on account of no gem obj
	 * being submitted
	 */
	return __gem_execbuf(fd, &execbuf) == -ENOENT;
}

253
/**
254
 * igt_hang_ring_ctx:
255
 * @fd: open i915 drm file descriptor
256
 * @ctx: the contxt specifier
257
 * @ring: execbuf ring flag
258
 * @flags: set of flags to control execution
259
 * @offset: The resultant gtt offset of the exec obj
260
 *
261
 * This helper function injects a hanging batch associated with @ctx into @ring.
262
 * It returns a #igt_hang_t structure which must be passed to
263 264
 * igt_post_hang_ring() for hang post-processing (after the gpu hang
 * interaction has been tested.
265 266 267 268
 *
 * Returns:
 * Structure with helper internal state for igt_post_hang_ring().
 */
269
igt_hang_t igt_hang_ctx(int fd, uint32_t ctx, int ring, unsigned flags)
270
{
271
	struct drm_i915_gem_context_param param;
272
	igt_spin_t *spin;
273 274
	unsigned ban;

275 276
	igt_require_hang_ring(fd, ring);

277 278
	/* check if non-default ctx submission is allowed */
	igt_require(ctx == 0 || has_ctx_exec(fd, ring, ctx));
279

280
	param.ctx_id = ctx;
281
	param.size = 0;
282 283

	if ((flags & HANG_ALLOW_CAPTURE) == 0) {
284
		param.param = I915_CONTEXT_PARAM_NO_ERROR_CAPTURE;
285 286 287 288 289 290 291 292
		param.value = 1;
		/* Older kernels may not have NO_ERROR_CAPTURE, in which case
		 * we just eat the error state in post-hang (and hope we eat
		 * the right one).
		 */
		__gem_context_set_param(fd, &param);
	}

293 294 295
	ban = context_get_ban(fd, ctx);
	if ((flags & HANG_ALLOW_BAN) == 0)
		context_set_ban(fd, ctx, 0);
296

297 298 299 300
	spin = __igt_spin_batch_new(fd,
				    .ctx = ctx,
				    .engine = ring,
				    .flags = IGT_SPIN_NO_PREEMPTION);
301

302
	return (igt_hang_t){ spin, ctx, ban, flags };
303 304 305 306 307 308 309 310
}

/**
 * igt_hang_ring:
 * @fd: open i915 drm file descriptor
 * @ring: execbuf ring flag
 *
 * This helper function injects a hanging batch into @ring. It returns a
311
 * #igt_hang_t structure which must be passed to igt_post_hang_ring() for
312 313 314 315 316
 * hang post-processing (after the gpu hang interaction has been tested.
 *
 * Returns:
 * Structure with helper internal state for igt_post_hang_ring().
 */
317
igt_hang_t igt_hang_ring(int fd, int ring)
318
{
319
	return igt_hang_ctx(fd, 0, ring, 0);
320 321
}

322
/**
323
 * igt_post_hang_ring:
324 325 326 327 328 329
 * @fd: open i915 drm file descriptor
 * @arg: hang state from igt_hang_ring()
 *
 * This function does the necessary post-processing after a gpu hang injected
 * with igt_hang_ring().
 */
330
void igt_post_hang_ring(int fd, igt_hang_t arg)
331
{
332
	if (!arg.spin)
333 334
		return;

335 336
	gem_sync(fd, arg.spin->handle); /* Wait until it hangs */
	igt_spin_batch_free(fd, arg.spin);
337

338
	context_set_ban(fd, arg.ctx, arg.ban);
339 340

	if ((arg.flags & HANG_ALLOW_CAPTURE) == 0) {
341 342 343
		struct drm_i915_gem_context_param param = {
			.ctx_id = arg.ctx,
			.param = I915_CONTEXT_PARAM_NO_ERROR_CAPTURE,
344 345 346 347 348
			.value = 0,
		};
		__gem_context_set_param(fd, &param);

		eat_error_state(fd);
349
	}
350
}
351

352 353 354 355 356 357 358 359
/**
 * igt_force_gpu_reset:
 *
 * forces a gpu reset using the i915_wedged debugfs interface. To be used to
 * recover from situations where the hangcheck didn't trigger and/or the gpu is
 * stuck, either because the test manually disabled gpu resets or because the
 * test hit an hangcheck bug
 */
360
void igt_force_gpu_reset(int drm_fd)
361
{
362
	int dir, wedged;
363 364 365

	igt_debug("Triggering GPU reset\n");

366
	dir = igt_debugfs_dir(drm_fd);
367

368 369
	igt_sysfs_set(dir, "i915_wedged", "-1");
	igt_sysfs_scanf(dir, "i915_wedged", "%d", &wedged);
370

371
	close(dir);
372
	errno = 0;
373 374

	igt_assert(!wedged);
375 376
}

377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
/* GPU abusers */
static struct igt_helper_process hang_helper;
static void __attribute__((noreturn))
hang_helper_process(pid_t pid, int fd)
{
	while (1) {
		if (kill(pid, 0)) /* Parent has died, so must we. */
			exit(0);

		igt_post_hang_ring(fd,
				   igt_hang_ring(fd, I915_EXEC_DEFAULT));

		sleep(1);
	}
}

/**
 * igt_fork_hang_helper:
 *
 * Fork a child process using #igt_fork_helper to hang the default engine
 * of the GPU at regular intervals.
 *
 * This is useful to exercise slow running code (such as aperture placement)
 * which needs to be robust against a GPU reset.
 *
402 403
 * This function automatically skips when test requirements aren't met using
 * igt_skip().
404
 */
405
void igt_fork_hang_helper(void)
406 407 408
{
	int fd, gen;

409
	fd = drm_open_driver(DRIVER_INTEL);
410 411

	gen = intel_gen(intel_get_drm_devid(fd));
412
	igt_skip_on(gen < 5);
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429

	igt_fork_helper(&hang_helper)
		hang_helper_process(getppid(), fd);

	close(fd);
}

/**
 * igt_stop_hang_helper:
 *
 * Stops the child process spawned with igt_fork_hang_helper().
 *
 * In tests with subtests this function can be called outside of failure
 * catching code blocks like #igt_fixture or #igt_subtest.
 */
void igt_stop_hang_helper(void)
{
430 431
	if (hang_helper.running)
		igt_stop_helper(&hang_helper);
432 433 434 435 436 437 438 439 440 441 442
}

/**
 * igt_open_forcewake_handle:
 *
 * This functions opens the debugfs forcewake file and so prevents the GT from
 * suspending. The reference is automatically dropped when the is closed.
 *
 * Returns:
 * The file descriptor of the forcewake handle or -1 if that didn't work out.
 */
443
int igt_open_forcewake_handle(int fd)
444 445 446
{
	if (getenv("IGT_NO_FORCEWAKE"))
		return -1;
447
	return igt_debugfs_open(fd, "i915_forcewake_user", O_WRONLY);
448
}
449

450
#if defined(__x86_64__) || defined(__i386__)
451 452
static unsigned int clflush_size;

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491
int igt_setup_clflush(void)
{
	FILE *file;
	char *line = NULL;
	size_t size = 0;
	int first_stanza = 1;
	int has_clflush = 0;

	if (clflush_size)
		return 1;

	file = fopen("/proc/cpuinfo", "r");
	if (file == NULL)
		return 0;

	while (getline(&line, &size, file) != -1) {
		if (strncmp(line, "processor", 9) == 0) {
			if (!first_stanza)
				break;
			first_stanza = 0;
		}

		if (strncmp(line, "flags", 5) == 0) {
			if (strstr(line, "clflush"))
				has_clflush = 1;
		}

		if (strncmp(line, "clflush size", 12) == 0) {
			char *colon = strchr(line, ':');
			if (colon)
				clflush_size = atoi(colon + 1);
		}
	}
	free(line);
	fclose(file);

	return has_clflush && clflush_size;
}

492
__attribute__((target("sse2")))
493 494 495 496 497 498 499
void igt_clflush_range(void *addr, int size)
{
	char *p, *end;

	end = (char *)addr + size;
	p = (char *)((uintptr_t)addr & ~((uintptr_t)clflush_size - 1));

500
	__builtin_ia32_mfence();
501
	for (; p < end; p += clflush_size)
502
		__builtin_ia32_clflush(p);
503
	__builtin_ia32_clflush(end - 1); /* magic serialisation for byt+ */
504
	__builtin_ia32_mfence();
505
}
506
#else
507 508 509 510 511 512 513 514
int igt_setup_clflush(void)
{
	/* requires mfence + clflush, both SSE2 instructions */
	return 0;
}

void igt_clflush_range(void *addr, int size)
{
515
	fprintf(stderr, "igt_clflush_range() unsupported\n");
516
}
517
#endif
518 519 520 521 522 523 524 525 526 527 528 529

/**
 * intel_detect_and_clear_missed_irq:
 * @fd: open i915 drm file descriptor, used to quiesce the gpu
 *
 * This functions idles the GPU and then queries whether there has
 * been a missed interrupt reported by the driver. Afterwards it
 * clears the missed interrupt flag, in order to disable the timer
 * fallback for the next test.
 */
unsigned intel_detect_and_clear_missed_interrupts(int fd)
{
530 531
	unsigned missed;
	int dir;
532 533 534

	gem_quiescent_gpu(fd);

535 536 537
	dir = igt_debugfs_dir(fd);

	missed = 0;
538
	igt_sysfs_scanf(dir, "i915_ring_missed_irq", "%x", &missed);
539 540 541 542
	if (missed)
		igt_sysfs_set(dir, "i915_ring_missed_irq", "0");

	close(dir);
543

544
	errno = 0;
545 546
	return missed;
}
547 548

const struct intel_execution_engine intel_execution_engines[] = {
549
	{ "default", NULL, 0, 0 },
550 551 552 553 554 555
	{ "render", "rcs0", I915_EXEC_RENDER, 0 },
	{ "bsd", "vcs0", I915_EXEC_BSD, 0 },
	{ "bsd1", "vcs0", I915_EXEC_BSD, 1<<13 /*I915_EXEC_BSD_RING1*/ },
	{ "bsd2", "vcs1", I915_EXEC_BSD, 2<<13 /*I915_EXEC_BSD_RING2*/ },
	{ "blt", "bcs0", I915_EXEC_BLT, 0 },
	{ "vebox", "vecs0", I915_EXEC_VEBOX, 0 },
556 557
	{ NULL, 0, 0 }
};
558 559 560 561 562 563 564 565 566 567

bool gem_can_store_dword(int fd, unsigned int engine)
{
	uint16_t devid = intel_get_drm_devid(fd);
	const struct intel_device_info *info = intel_get_device_info(devid);
	const int gen = ffs(info->gen);

	if (gen <= 2) /* requires physical addresses */
		return false;

568 569 570
	if (gen == 3 && (info->is_grantsdale || info->is_alviso))
		return false; /* only supports physical addresses */

571
	if (gen == 6 && ((engine & 0x3f) == I915_EXEC_BSD))
572 573 574 575 576 577 578
		return false; /* kills the machine! */

	if (info->is_broadwater)
		return false; /* Not sure yet... */

	return true;
}
579 580 581 582 583 584 585

const struct intel_execution_engine2 intel_execution_engines2[] = {
	{ "rcs0", I915_ENGINE_CLASS_RENDER, 0 },
	{ "bcs0", I915_ENGINE_CLASS_COPY, 0 },
	{ "vcs0", I915_ENGINE_CLASS_VIDEO, 0 },
	{ "vcs1", I915_ENGINE_CLASS_VIDEO, 1 },
	{ "vecs0", I915_ENGINE_CLASS_VIDEO_ENHANCE, 0 },
586
	{ }
587 588 589 590 591 592 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
};

unsigned int
gem_class_instance_to_eb_flags(int gem_fd,
			       enum drm_i915_gem_engine_class class,
			       unsigned int instance)
{
	if (class != I915_ENGINE_CLASS_VIDEO)
		igt_assert(instance == 0);
	else
		igt_assert(instance >= 0 && instance <= 1);

	switch (class) {
	case I915_ENGINE_CLASS_RENDER:
		return I915_EXEC_RENDER;
	case I915_ENGINE_CLASS_COPY:
		return I915_EXEC_BLT;
	case I915_ENGINE_CLASS_VIDEO:
		if (instance == 0) {
			if (gem_has_bsd2(gem_fd))
				return I915_EXEC_BSD | I915_EXEC_BSD_RING1;
			else
				return I915_EXEC_BSD;

		} else {
			return I915_EXEC_BSD | I915_EXEC_BSD_RING2;
		}
	case I915_ENGINE_CLASS_VIDEO_ENHANCE:
		return I915_EXEC_VEBOX;
	case I915_ENGINE_CLASS_INVALID:
	default:
		igt_assert(0);
	};
}

bool gem_has_engine(int gem_fd,
		    enum drm_i915_gem_engine_class class,
		    unsigned int instance)
{
	return gem_has_ring(gem_fd,
			    gem_class_instance_to_eb_flags(gem_fd, class,
							   instance));
}
630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652

bool gem_ring_is_physical_engine(int fd, unsigned ring)
{
	if (ring == I915_EXEC_DEFAULT)
		return false;

	/* BSD uses an extra flag to chose between aliasing modes */
	if ((ring & 63) == I915_EXEC_BSD) {
		bool explicit_bsd = ring & (3 << 13);
		bool has_bsd2 = gem_has_bsd2(fd);
		return explicit_bsd ? has_bsd2 : !has_bsd2;
	}

	return true;
}

bool gem_ring_has_physical_engine(int fd, unsigned ring)
{
	if (!gem_ring_is_physical_engine(fd, ring))
		return false;

	return gem_has_ring(fd, ring);
}