gem_softpin.c 14.8 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 25 26 27 28
/*
 * Copyright © 2015 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.
 *
 * Authors:
 *    Vinay Belgaumkar <vinay.belgaumkar@intel.com>
 *    Thomas Daniel <thomas.daniel@intel.com>
 *
 */

29 30
#include "igt.h"

31 32 33
#define EXEC_OBJECT_PINNED	(1<<4)
#define EXEC_OBJECT_SUPPORTS_48B_ADDRESS (1<<3)

34 35 36 37 38 39 40 41 42 43 44 45
/* gen8_canonical_addr
 * Used to convert any address into canonical form, i.e. [63:48] == [47].
 * Based on kernel's sign_extend64 implementation.
 * @address - a virtual address
*/
#define GEN8_HIGH_ADDRESS_BIT 47
static uint64_t gen8_canonical_addr(uint64_t address)
{
	__u8 shift = 63 - GEN8_HIGH_ADDRESS_BIT;
	return (__s64)(address << shift) >> shift;
}

46
static void test_invalid(int fd)
47
{
48 49 50 51 52
	const uint32_t bbe = MI_BATCH_BUFFER_END;
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 object;

	memset(&execbuf, 0, sizeof(execbuf));
53
	execbuf.buffers_ptr = to_user_pointer(&object);
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
	execbuf.buffer_count = 1;

	memset(&object, 0, sizeof(object));
	object.handle = gem_create(fd, 2*4096);
	object.flags = EXEC_OBJECT_SUPPORTS_48B_ADDRESS | EXEC_OBJECT_PINNED;
	gem_write(fd, object.handle, 0, &bbe, sizeof(bbe));

	/* Check invalid alignment */
	object.offset = 4096;
	object.alignment = 64*1024;
	igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);
	object.alignment = 0;

	/* Check wraparound */
	object.offset = -4096ULL;
	igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);

	/* Check beyond bounds of aperture */
	object.offset = gem_aperture_size(fd) - 4096;
	object.offset = gen8_canonical_addr(object.offset);
	igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);

	/* Check gen8 canonical addressing */
	if (gem_aperture_size(fd) > 1ull<<GEN8_HIGH_ADDRESS_BIT) {
		object.offset = 1ull << GEN8_HIGH_ADDRESS_BIT;
		igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);

		object.offset = gen8_canonical_addr(object.offset);
		igt_assert_eq(__gem_execbuf(fd, &execbuf), 0);
83 84
	}

85 86 87 88 89
	/* Check extended range */
	if (gem_aperture_size(fd) > 1ull<<32) {
		object.flags = EXEC_OBJECT_PINNED;
		object.offset = 1ull<<32;
		igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);
90

91 92 93 94
		object.offset = gen8_canonical_addr(object.offset);
		object.flags |= EXEC_OBJECT_SUPPORTS_48B_ADDRESS;
		igt_assert_eq(__gem_execbuf(fd, &execbuf), 0);
	}
95 96
}

97
static void test_softpin(int fd)
98
{
99 100
	const uint32_t size = 1024 * 1024;
	const uint32_t bbe = MI_BATCH_BUFFER_END;
101
	struct drm_i915_gem_execbuffer2 execbuf;
102 103
	struct drm_i915_gem_exec_object2 object;
	uint64_t offset, end;
104
	uint32_t last_handle;
105 106
	int loop;

107 108
	last_handle = gem_create(fd, size);

109
	memset(&execbuf, 0, sizeof(execbuf));
110
	execbuf.buffers_ptr = to_user_pointer(&object);
111 112 113 114 115 116 117 118 119
	execbuf.buffer_count = 1;
	for (loop = 0; loop < 1024; loop++) {
		memset(&object, 0, sizeof(object));
		object.handle = gem_create(fd, 2*size);
		gem_write(fd, object.handle, 0, &bbe, sizeof(bbe));

		/* Find a hole */
		gem_execbuf(fd, &execbuf);
		gem_close(fd, object.handle);
120
		gem_close(fd, last_handle);
121 122 123 124 125 126 127 128 129 130 131 132 133 134

		igt_debug("Made a 2 MiB hole: %08llx\n",
			  object.offset);

		object.handle = gem_create(fd, size);
		gem_write(fd, object.handle, 0, &bbe, sizeof(bbe));
		object.flags |= EXEC_OBJECT_PINNED;

		end = object.offset + size;
		for (offset = object.offset; offset <= end; offset += 4096) {
			object.offset = offset;
			gem_execbuf(fd, &execbuf);
			igt_assert_eq_u64(object.offset, offset);
		}
135

136
		last_handle = object.handle;
137 138 139
	}
}

140
static void test_overlap(int fd)
141
{
142 143
	const uint32_t size = 1024 * 1024;
	const uint32_t bbe = MI_BATCH_BUFFER_END;
144
	struct drm_i915_gem_execbuffer2 execbuf;
145 146 147 148 149 150 151 152 153 154 155 156
	struct drm_i915_gem_exec_object2 object[2];
	uint64_t offset;
	uint32_t handle;

	handle = gem_create(fd, 3*size);
	gem_write(fd, handle, 0, &bbe, sizeof(bbe));

	memset(object, 0, sizeof(object));
	object[0].handle = handle;

	/* Find a hole */
	memset(&execbuf, 0, sizeof(execbuf));
157
	execbuf.buffers_ptr = to_user_pointer(object);
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
	execbuf.buffer_count = 1;
	gem_execbuf(fd, &execbuf);

	igt_debug("Made a 3x1 MiB hole: %08llx\n",
		  object[0].offset);

	object[0].handle = gem_create(fd, size);
	object[0].offset += size;
	object[0].flags |= EXEC_OBJECT_PINNED;
	object[1].handle = gem_create(fd, size);
	object[1].flags |= EXEC_OBJECT_PINNED;
	gem_write(fd, object[1].handle, 0, &bbe, sizeof(bbe));
	execbuf.buffer_count = 2;

	/* Check that we fit into our hole */
	object[1].offset = object[0].offset - size;
	gem_execbuf(fd, &execbuf);
	igt_assert_eq_u64(object[1].offset + size, object[0].offset);

	object[1].offset = object[0].offset + size;
	gem_execbuf(fd, &execbuf);
	igt_assert_eq_u64(object[1].offset - size, object[0].offset);

	/* Try all possible page-aligned overlaps */
	for (offset = object[0].offset - size + 4096;
	     offset < object[0].offset + size;
	     offset += 4096) {
		object[1].offset = offset;
		igt_debug("[0]=[%08llx - %08llx] [1]=[%08llx - %08llx]\n",
			  (long long)object[0].offset,
			  (long long)object[0].offset + size,
			  (long long)object[1].offset,
			  (long long)object[1].offset + size);
		igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);
		igt_assert_eq_u64(object[1].offset, offset);
	}
194

195 196 197
	gem_close(fd, object[1].handle);
	gem_close(fd, object[0].handle);
	gem_close(fd, handle);
198 199
}

200
static uint64_t busy_batch(int fd)
201
{
202 203
	const int gen = intel_gen(intel_get_drm_devid(fd));
	const int has_64bit_reloc = gen >= 8;
204
	struct drm_i915_gem_execbuffer2 execbuf;
205 206 207 208
	struct drm_i915_gem_exec_object2 object[2];
	uint32_t *map;
	int factor = 10;
	int i = 0;
209

210 211 212 213 214 215 216 217 218 219
	memset(object, 0, sizeof(object));
	object[0].handle = gem_create(fd, 1024*1024);
	object[1].handle = gem_create(fd, 4096);
	map = gem_mmap__cpu(fd, object[1].handle, 0, 4096, PROT_WRITE);
	gem_set_domain(fd, object[1].handle,
		       I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);

	*map = MI_BATCH_BUFFER_END;

	memset(&execbuf, 0, sizeof(execbuf));
220
	execbuf.buffers_ptr = to_user_pointer(object);
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
	execbuf.buffer_count = 2;
	if (gen >= 6)
		execbuf.flags = I915_EXEC_BLT;
	gem_execbuf(fd, &execbuf);

	igt_debug("Active offsets = [%08llx, %08llx]\n",
		  object[0].offset, object[1].offset);

#define COPY_BLT_CMD		(2<<29|0x53<<22|0x6)
#define BLT_WRITE_ALPHA		(1<<21)
#define BLT_WRITE_RGB		(1<<20)
	gem_set_domain(fd, object[1].handle,
		       I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
	while (factor--) {
		/* XY_SRC_COPY */
		map[i++] = COPY_BLT_CMD | BLT_WRITE_ALPHA | BLT_WRITE_RGB;
		if (has_64bit_reloc)
			map[i-1] += 2;
		map[i++] = 0xcc << 16 | 1 << 25 | 1 << 24 | (4*1024);
		map[i++] = 0;
		map[i++] = 256 << 16 | 1024;
		map[i++] = object[0].offset;
		if (has_64bit_reloc)
			map[i++] = object[0].offset >> 32;
		map[i++] = 0;
		map[i++] = 4096;
		map[i++] = object[0].offset;
		if (has_64bit_reloc)
			map[i++] = object[0].offset >> 32;
250
	}
251 252
	map[i++] = MI_BATCH_BUFFER_END;
	munmap(map, 4096);
253

254 255 256 257 258
	object[0].flags = EXEC_OBJECT_PINNED | EXEC_OBJECT_WRITE;
	object[1].flags = EXEC_OBJECT_PINNED;
	gem_execbuf(fd, &execbuf);
	gem_close(fd, object[0].handle);
	gem_close(fd, object[1].handle);
259

260
	return object[1].offset;
261 262
}

263
static void test_evict_active(int fd)
264
{
265
	const uint32_t bbe = MI_BATCH_BUFFER_END;
266
	struct drm_i915_gem_execbuffer2 execbuf;
267 268
	struct drm_i915_gem_exec_object2 object;
	uint64_t expected;
269

270 271 272
	memset(&object, 0, sizeof(object));
	object.handle = gem_create(fd, 4096);
	gem_write(fd, object.handle, 0, &bbe, sizeof(bbe));
273

274
	memset(&execbuf, 0, sizeof(execbuf));
275
	execbuf.buffers_ptr = to_user_pointer(&object);
276 277
	execbuf.buffer_count = 1;

278 279 280
	expected = busy_batch(fd);
	object.offset = expected;
	object.flags = EXEC_OBJECT_PINNED;
281

282 283 284
	/* Replace the active batch with ourselves, forcing an eviction */
	gem_execbuf(fd, &execbuf);
	igt_assert_eq_u64(object.offset, expected);
285 286

	gem_close(fd, object.handle);
287 288
}

289 290 291 292 293 294 295 296
static void test_evict_snoop(int fd)
{
	const uint32_t bbe = MI_BATCH_BUFFER_END;
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 object[2];
	uint64_t hole;

	igt_require(!gem_has_llc(fd));
297
	igt_require(!gem_uses_ppgtt(fd));
298 299

	memset(&execbuf, 0, sizeof(execbuf));
300
	execbuf.buffers_ptr = to_user_pointer(object);
301 302 303 304
	execbuf.buffer_count = 1;

	/* Find a hole */
	memset(object, 0, sizeof(object));
305
	object[0].handle = gem_create(fd, 5*4096);
306 307 308
	gem_write(fd, object[0].handle, 0, &bbe, sizeof(bbe));
	gem_execbuf(fd, &execbuf);
	gem_close(fd, object[0].handle);
309
	hole = object[0].offset + 4096;
310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345

	/* Create a snoop + uncached pair */
	object[0].handle = gem_create(fd, 4096);
	object[0].flags = EXEC_OBJECT_PINNED;
	gem_set_caching(fd, object[0].handle, 1);
	object[1].handle = gem_create(fd, 4096);
	object[1].flags = EXEC_OBJECT_PINNED;
	gem_write(fd, object[1].handle, 4096-sizeof(bbe), &bbe, sizeof(bbe));
	execbuf.buffer_count = 2;

	/* snoop abutting before uncached -> error */
	object[0].offset = hole;
	object[1].offset = hole + 4096;
	igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);

	/* snoop abutting after uncached -> error */
	object[0].offset = hole + 4096;
	object[1].offset = hole;
	igt_assert_eq(__gem_execbuf(fd, &execbuf), -EINVAL);

	/* with gap -> okay */
	object[0].offset = hole + 2*4096;
	object[1].offset = hole;
	igt_assert_eq(__gem_execbuf(fd, &execbuf), 0);

	/* And we should force the snoop away (or the GPU may hang) */
	object[0].flags = 0;
	object[1].offset = hole + 4096;
	igt_assert_eq(__gem_execbuf(fd, &execbuf), 0);
	igt_assert(object[0].offset != hole);
	igt_assert(object[0].offset != hole + 2*4096);

	gem_close(fd, object[0].handle);
	gem_close(fd, object[1].handle);
}

346
static void test_evict_hang(int fd)
347
{
348
	const uint32_t bbe = MI_BATCH_BUFFER_END;
349
	struct drm_i915_gem_execbuffer2 execbuf;
350
	struct drm_i915_gem_exec_object2 object;
351
	igt_hang_t hang;
352
	uint64_t expected;
353

354 355 356
	memset(&object, 0, sizeof(object));
	object.handle = gem_create(fd, 4096);
	gem_write(fd, object.handle, 0, &bbe, sizeof(bbe));
357

358
	memset(&execbuf, 0, sizeof(execbuf));
359
	execbuf.buffers_ptr = to_user_pointer(&object);
360
	execbuf.buffer_count = 1;
361

362 363
	hang = igt_hang_ctx(fd, 0, 0, 0);
	expected = hang.spin->obj[1].offset;
364

365
	/* Replace the hung batch with ourselves, forcing an eviction */
366 367
	object.offset = expected;
	object.flags = EXEC_OBJECT_PINNED;
368
	gem_execbuf(fd, &execbuf);
369
	igt_assert_eq_u64(object.offset, expected);
370

371
	igt_post_hang_ring(fd, hang);
372
	gem_close(fd, object.handle);
373
}
374

375
static void xchg_offset(void *array, unsigned i, unsigned j)
376
{
377 378 379 380
	struct drm_i915_gem_exec_object2 *object = array;
	uint64_t tmp = object[i].offset;
	object[i].offset = object[j].offset;
	object[j].offset = tmp;
381 382
}

383 384
enum sleep { NOSLEEP, SUSPEND, HIBERNATE };
static void test_noreloc(int fd, enum sleep sleep)
385
{
386 387 388
	const int gen = intel_gen(intel_get_drm_devid(fd));
	const uint32_t size = 4096;
	const uint32_t bbe = MI_BATCH_BUFFER_END;
389
	struct drm_i915_gem_execbuffer2 execbuf;
390 391 392 393 394 395 396 397 398 399 400 401 402 403
	struct drm_i915_gem_exec_object2 object[257];
	uint64_t offset;
	uint32_t handle;
	uint32_t *batch, *b;
	int i, loop;

	handle = gem_create(fd, (ARRAY_SIZE(object)+1)*size);
	gem_write(fd, handle, 0, &bbe, sizeof(bbe));

	memset(object, 0, sizeof(object));
	object[0].handle = handle;

	/* Find a hole */
	memset(&execbuf, 0, sizeof(execbuf));
404
	execbuf.buffers_ptr = to_user_pointer(object);
405
	execbuf.buffer_count = 1;
406
	if (gen < 6)
407
		execbuf.flags |= I915_EXEC_SECURE;
408 409 410 411 412 413 414 415 416 417 418
	gem_execbuf(fd, &execbuf);
	gem_close(fd, object[0].handle);

	igt_debug("Made a %dx%d KiB hole: %08llx\n",
		  (int)ARRAY_SIZE(object), size/1024, object[0].offset);

	offset = object[0].offset;
	for (i = 0; i < ARRAY_SIZE(object) - 1; i++) {
		object[i].handle = gem_create(fd, size);
		object[i].offset = offset + i*size;
		object[i].flags = EXEC_OBJECT_PINNED | EXEC_OBJECT_WRITE;
419
	}
420 421 422 423 424 425 426 427
	object[i].handle = gem_create(fd, 2*size);
	object[i].offset = offset + i*size;
	object[i].flags = EXEC_OBJECT_PINNED;

	b = batch = gem_mmap__cpu(fd, object[i].handle, 0, 2*size, PROT_WRITE);
	gem_set_domain(fd, object[i].handle,
		       I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
	for (i = 0; i < ARRAY_SIZE(object) - 1; i++) {
428 429 430 431 432 433
		*b++ = MI_STORE_DWORD_IMM | (gen < 6 ? 1 << 22 : 0);
		if (gen >= 8) {
			*b++ = object[i].offset;
			*b++ = object[i].offset >> 32;
		} else if (gen >= 4) {
			*b++ = 0;
434 435
			*b++ = object[i].offset;
		} else {
436
			b[-1]--;
437 438 439 440 441 442 443
			*b++ = object[i].offset;
		}
		*b++ = i;
	}
	*b++ = MI_BATCH_BUFFER_END;
	igt_assert(b - batch <= 2*size/sizeof(uint32_t));
	munmap(batch, size);
444

445 446 447 448
	execbuf.buffer_count = ARRAY_SIZE(object);
	for (loop = 0; loop < 1024; loop++) {
		igt_permute_array(object, ARRAY_SIZE(object)-1, xchg_offset);
		gem_execbuf(fd, &execbuf);
449

450 451 452 453 454
		if ((loop & 127) == 0) {
			switch (sleep) {
			case NOSLEEP:
				break;
			case SUSPEND:
455 456
				igt_system_suspend_autoresume(SUSPEND_STATE_MEM,
							      SUSPEND_TEST_NONE);
457 458
				break;
			case HIBERNATE:
459 460
				igt_system_suspend_autoresume(SUSPEND_STATE_DISK,
							      SUSPEND_TEST_NONE);
461 462 463 464
				break;
			}
		}

465 466
		for (i = 0; i < ARRAY_SIZE(object) - 1; i++) {
			uint32_t val;
467

468
			gem_read(fd, object[i].handle, 0, &val, sizeof(val));
469
			igt_assert_eq(val, (object[i].offset - offset)/size);
470
		}
471
	}
472

473 474 475
	for (i = 0; i < ARRAY_SIZE(object); i++)
		gem_close(fd, object[i].handle);
}
476

477 478 479
igt_main
{
	int fd = -1;
480

481 482 483
	igt_skip_on_simulation();

	igt_fixture {
484
		fd = drm_open_driver_master(DRIVER_INTEL);
485
		igt_require_gem(fd);
486
		igt_require(gem_has_softpin(fd));
487
		igt_require(gem_can_store_dword(fd, 0));
488 489
	}

490 491 492 493 494 495
	igt_subtest("invalid")
		test_invalid(fd);
	igt_subtest("softpin")
		test_softpin(fd);
	igt_subtest("overlap")
		test_overlap(fd);
496

497
	igt_subtest("noreloc")
498
		test_noreloc(fd, NOSLEEP);
499
	igt_subtest("noreloc-interruptible")
500
		igt_while_interruptible(true) test_noreloc(fd, NOSLEEP);
501 502 503 504
	igt_subtest("noreloc-S3")
		test_noreloc(fd, SUSPEND);
	igt_subtest("noreloc-S4")
		test_noreloc(fd, HIBERNATE);
505

506 507
	for (int signal = 0; signal <= 1; signal++) {
		igt_subtest_f("evict-active%s", signal ? "-interruptible" : "")
508
			igt_while_interruptible(signal) test_evict_active(fd);
509
		igt_subtest_f("evict-snoop%s", signal ? "-interruptible" : "")
510
			igt_while_interruptible(signal) test_evict_snoop(fd);
511
	}
512 513 514 515 516
	igt_subtest("evict-hang")
		test_evict_hang(fd);

	igt_fixture
		close(fd);
517
}