prime_busy.c 6.49 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 29 30 31 32 33 34 35 36 37
/*
 * 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.
 */

#include "igt.h"

#include <sys/poll.h>

IGT_TEST_DESCRIPTION("Basic check of polling for prime fences.");

static bool prime_busy(struct pollfd *pfd, bool excl)
{
	pfd->events = excl ? POLLOUT : POLLIN;
	return poll(pfd, 1, 0) == 0;
}

#define BEFORE 0x1
#define AFTER 0x2
38 39
#define HANG 0x4
#define POLL 0x8
40

41
static void busy(int fd, unsigned ring, unsigned flags)
42 43 44 45 46 47 48 49 50 51 52
{
	const int gen = intel_gen(intel_get_drm_devid(fd));
	struct drm_i915_gem_exec_object2 obj[2];
	struct pollfd pfd[2];
#define SCRATCH 0
#define BATCH 1
	struct drm_i915_gem_relocation_entry store[1024+1];
	struct drm_i915_gem_execbuffer2 execbuf;
	unsigned size = ALIGN(ARRAY_SIZE(store)*16 + 4, 4096);
	struct timespec tv;
	uint32_t *batch, *bbe;
53
	int i, count, timeout;
54

55 56
	gem_quiescent_gpu(fd);

57 58 59
	memset(&execbuf, 0, sizeof(execbuf));
	execbuf.buffers_ptr = (uintptr_t)obj;
	execbuf.buffer_count = 2;
60
	execbuf.flags = ring;
61 62 63 64 65 66 67 68 69 70 71
	if (gen < 6)
		execbuf.flags |= I915_EXEC_SECURE;

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

	obj[BATCH].handle = gem_create(fd, size);
	obj[BATCH].relocs_ptr = (uintptr_t)store;
	obj[BATCH].relocation_count = ARRAY_SIZE(store);
	memset(store, 0, sizeof(store));

72
	if (flags & BEFORE) {
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133
		memset(pfd, 0, sizeof(pfd));
		pfd[SCRATCH].fd = prime_handle_to_fd(fd, obj[SCRATCH].handle);
		pfd[BATCH].fd = prime_handle_to_fd(fd, obj[BATCH].handle);
	}

	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);

	i = 0;
	for (count = 0; count < 1024; count++) {
		store[count].target_handle = obj[SCRATCH].handle;
		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;
		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;
			store[count].offset += sizeof(uint32_t);
		} else {
			batch[i]--;
			batch[++i] = 0;
		}
		batch[++i] = count;
		i++;
	}

	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++;

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

134
	if (flags & AFTER) {
135 136 137 138 139 140 141 142 143 144 145
		memset(pfd, 0, sizeof(pfd));
		pfd[SCRATCH].fd = prime_handle_to_fd(fd, obj[SCRATCH].handle);
		pfd[BATCH].fd = prime_handle_to_fd(fd, obj[BATCH].handle);
	}

	igt_assert(prime_busy(&pfd[SCRATCH], false));
	igt_assert(prime_busy(&pfd[SCRATCH], true));

	igt_assert(!prime_busy(&pfd[BATCH], false));
	igt_assert(prime_busy(&pfd[BATCH], true));

146 147 148 149 150 151
	timeout = 120;
	if ((flags & HANG) == 0) {
		*bbe = MI_BATCH_BUFFER_END;
		__sync_synchronize();
		timeout = 1;
	}
152 153

	/* Calling busy in a loop should be enough to flush the rendering */
154 155 156 157 158 159 160 161
	if (flags & POLL) {
		pfd[BATCH].events = POLLOUT;
		igt_assert(poll(pfd, 1, timeout * 1000) == 1);
	} else {
		memset(&tv, 0, sizeof(tv));
		while (prime_busy(&pfd[BATCH], true))
			igt_assert(igt_seconds_elapsed(&tv) < timeout);
	}
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
	igt_assert(!prime_busy(&pfd[SCRATCH], true));

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

	gem_close(fd, obj[BATCH].handle);
	gem_close(fd, obj[SCRATCH].handle);

	close(pfd[BATCH].fd);
	close(pfd[SCRATCH].fd);
}

177 178 179
static void test_engine_mode(int fd,
			     const struct intel_execution_engine *e,
			     const char *name, unsigned int flags)
180
{
181
	igt_hang_t hang = {};
182

183 184 185 186 187 188
	igt_subtest_group {
		igt_fixture {
			gem_require_ring(fd, e->exec_id | e->flags);
			igt_require(gem_can_store_dword(fd, e->exec_id | e->flags));

			if ((flags & HANG) == 0)
189
			{
190
				igt_fork_hang_detector(fd);
191
			}
192
			else
193 194
			{
				igt_skip_on_simulation();
195
				hang = igt_allow_hang(fd, 0, 0);
196
			}
197
		}
198

199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
		igt_subtest_f("%s%s-%s",
			      !e->exec_id && !(flags & HANG) ? "basic-" : "",
			      name, e->name)
			busy(fd, e->exec_id | e->flags, flags);

		igt_subtest_f("%swait-%s-%s",
			      !e->exec_id && !(flags & HANG) ? "basic-" : "",
			      name, e->name)
			busy(fd, e->exec_id | e->flags, flags | POLL);

		igt_fixture {
			if ((flags & HANG) == 0)
				igt_stop_hang_detector();
			else
				igt_disallow_hang(fd, hang);
		}
215 216 217
	}
}

218 219 220 221 222
igt_main
{
	const struct intel_execution_engine *e;
	int fd = -1;

223
	igt_fixture {
224
		fd = drm_open_driver_master(DRIVER_INTEL);
225 226
		igt_require_gem(fd);
	}
227 228 229 230 231 232 233 234

	igt_subtest_group {
		const struct mode {
			const char *name;
			unsigned int flags;
		} modes[] = {
			{ "before", BEFORE },
			{ "after", AFTER },
235
			{ "hang", BEFORE | HANG },
236 237 238
			{ NULL },
		};

239
		igt_fixture
240 241 242
			gem_require_mmap_wc(fd);

		for (e = intel_execution_engines; e->name; e++) {
243
			for (const struct mode *m = modes; m->name; m++)
244
				test_engine_mode(fd, e, m->name, m->flags);
245 246 247 248 249 250
		}
	}

	igt_fixture
		close(fd);
}