gem_cpu_reloc.c 7.18 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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
/*
 * Copyright © 2012 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:
 *    Chris Wilson <chris@chris-wilson.co.uk>
 *
 */

/*
 * Testcase: Test the relocations through the CPU domain
 *
 * Attempt to stress test performing relocations whilst the batch is in the
 * CPU domain.
 *
 * A freshly allocated buffer starts in the CPU domain, and the pwrite
 * should also be performed whilst in the CPU domain and so we should
 * execute the relocations within the CPU domain. If for any reason one of
 * those steps should land it in the GTT domain, we take the secondary
 * precaution of filling the mappable portion of the GATT.
 *
 * In order to detect whether a relocation fails, we first fill a target
 * buffer with a sequence of invalid commands that would cause the GPU to
 * immediate hang, and then attempt to overwrite them with a legal, if
 * short, batchbuffer using a BLT. Then we come to execute the bo, if the
 * relocation fail and we either copy across all zeros or garbage, then the
 * GPU will hang.
 */

Thomas Wood's avatar
Thomas Wood committed
48
#include "igt.h"
Chris Wilson's avatar
Chris Wilson committed
49 50 51 52 53 54 55 56
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
Daniel Vetter's avatar
Daniel Vetter committed
57 58 59

#include <drm.h>

Chris Wilson's avatar
Chris Wilson committed
60 61
#include "intel_bufmgr.h"

62 63
IGT_TEST_DESCRIPTION("Test the relocations through the CPU domain.");

64 65
static uint32_t use_blt;

Chris Wilson's avatar
Chris Wilson committed
66 67 68 69 70 71 72 73 74 75 76 77 78 79
static void copy(int fd, uint32_t batch, uint32_t src, uint32_t dst)
{
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_relocation_entry gem_reloc[2];
	struct drm_i915_gem_exec_object2 gem_exec[3];

	gem_reloc[0].offset = 4 * sizeof(uint32_t);
	gem_reloc[0].delta = 0;
	gem_reloc[0].target_handle = dst;
	gem_reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
	gem_reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
	gem_reloc[0].presumed_offset = 0;

	gem_reloc[1].offset = 7 * sizeof(uint32_t);
Daniel Vetter's avatar
Daniel Vetter committed
80
	if (intel_gen(intel_get_drm_devid(fd)) >= 8)
81
		gem_reloc[1].offset += sizeof(uint32_t);
Chris Wilson's avatar
Chris Wilson committed
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
	gem_reloc[1].delta = 0;
	gem_reloc[1].target_handle = src;
	gem_reloc[1].read_domains = I915_GEM_DOMAIN_RENDER;
	gem_reloc[1].write_domain = 0;
	gem_reloc[1].presumed_offset = 0;

	memset(gem_exec, 0, sizeof(gem_exec));
	gem_exec[0].handle = src;
	gem_exec[1].handle = dst;
	gem_exec[2].handle = batch;
	gem_exec[2].relocation_count = 2;
	gem_exec[2].relocs_ptr = (uintptr_t)gem_reloc;

	memset(&execbuf, 0, sizeof(execbuf));
	execbuf.buffers_ptr = (uintptr_t)gem_exec;
	execbuf.buffer_count = 3;
	execbuf.batch_len = 4096;
99
	execbuf.flags = use_blt;
Chris Wilson's avatar
Chris Wilson committed
100

101
	gem_execbuf(fd, &execbuf);
Chris Wilson's avatar
Chris Wilson committed
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
}

static void exec(int fd, uint32_t handle)
{
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 gem_exec;

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

	memset(&execbuf, 0, sizeof(execbuf));
	execbuf.buffers_ptr = (uintptr_t)&gem_exec;
	execbuf.buffer_count = 1;
	execbuf.batch_len = 4096;

117
	gem_execbuf(fd, &execbuf);
Chris Wilson's avatar
Chris Wilson committed
118 119
}

Ben Widawsky's avatar
Ben Widawsky committed
120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
uint32_t gen6_batch[] = {
	(XY_SRC_COPY_BLT_CMD | 6 |
	 XY_SRC_COPY_BLT_WRITE_ALPHA |
	 XY_SRC_COPY_BLT_WRITE_RGB),
	(3 << 24 | /* 32 bits */
	 0xcc << 16 | /* copy ROP */
	 4096),
	0 << 16 | 0, /* dst x1, y1 */
	1 << 16 | 2,
	0, /* dst relocation */
	0 << 16 | 0, /* src x1, y1 */
	4096,
	0, /* src relocation */
	MI_BATCH_BUFFER_END,
};

uint32_t gen8_batch[] = {
	(XY_SRC_COPY_BLT_CMD | 8 |
	 XY_SRC_COPY_BLT_WRITE_ALPHA |
	 XY_SRC_COPY_BLT_WRITE_RGB),
	(3 << 24 | /* 32 bits */
	 0xcc << 16 | /* copy ROP */
	 4096),
	0 << 16 | 0, /* dst x1, y1 */
	1 << 16 | 2,
	0, /* dst relocation */
	0, /* FIXME */
	0 << 16 | 0, /* src x1, y1 */
	4096,
	0, /* src relocation */
	0, /* FIXME */
	MI_BATCH_BUFFER_END,
};

uint32_t *batch = gen6_batch;
uint32_t batch_size = sizeof(gen6_batch);

157
static void run_test(int fd, int count)
Chris Wilson's avatar
Chris Wilson committed
158 159 160 161
{
	const uint32_t hang[] = {-1, -1, -1, -1};
	const uint32_t end[] = {MI_BATCH_BUFFER_END, 0};
	uint32_t noop;
162
	uint32_t *handles;
163
	int i;
Chris Wilson's avatar
Chris Wilson committed
164

165 166 167 168 169 170
	noop = intel_get_drm_devid(fd);

	use_blt = 0;
	if (intel_gen(noop) >= 6)
		use_blt = I915_EXEC_BLT;

Ben Widawsky's avatar
Ben Widawsky committed
171 172 173 174 175
	if (intel_gen(noop) >= 8) {
		batch = gen8_batch;
		batch_size += 2 * 4;
	}

176
	handles = malloc (count * sizeof(uint32_t));
Daniel Vetter's avatar
Daniel Vetter committed
177
	igt_assert(handles);
178

Chris Wilson's avatar
Chris Wilson committed
179 180 181 182 183
	noop = gem_create(fd, 4096);
	gem_write(fd, noop, 0, end, sizeof(end));

	/* fill the entire gart with batches and run them */
	for (i = 0; i < count; i++) {
184 185 186
		uint32_t bad;

		handles[i] = gem_create(fd, 4096);
Ben Widawsky's avatar
Ben Widawsky committed
187
		gem_write(fd, handles[i], 0, batch, batch_size);
188 189 190

		bad = gem_create(fd, 4096);
		gem_write(fd, bad, 0, hang, sizeof(hang));
191
		gem_write(fd, bad, 4096-sizeof(end), end, sizeof(end));
192 193 194 195 196

		/* launch the newly created batch */
		copy(fd, handles[i], noop, bad);
		exec(fd, bad);
		gem_close(fd, bad);
Chris Wilson's avatar
Chris Wilson committed
197

Daniel Vetter's avatar
Daniel Vetter committed
198
		igt_progress("gem_cpu_reloc: ", i, 2*count);
199 200 201 202 203
	}

	/* And again in reverse to try and catch the relocation code out */
	for (i = 0; i < count; i++) {
		uint32_t bad;
Chris Wilson's avatar
Chris Wilson committed
204 205 206

		bad = gem_create(fd, 4096);
		gem_write(fd, bad, 0, hang, sizeof(hang));
207
		gem_write(fd, bad, 4096-sizeof(end), end, sizeof(end));
Chris Wilson's avatar
Chris Wilson committed
208 209

		/* launch the newly created batch */
210
		copy(fd, handles[count-i-1], noop, bad);
Chris Wilson's avatar
Chris Wilson committed
211 212 213
		exec(fd, bad);
		gem_close(fd, bad);

Daniel Vetter's avatar
Daniel Vetter committed
214
		igt_progress("gem_cpu_reloc: ", count+i, 3*count);
215 216 217 218 219 220 221 222
	}

	/* Third time lucky? */
	for (i = 0; i < count; i++) {
		uint32_t bad;

		bad = gem_create(fd, 4096);
		gem_write(fd, bad, 0, hang, sizeof(hang));
223
		gem_write(fd, bad, 4096-sizeof(end), end, sizeof(end));
224 225 226 227 228 229 230 231

		/* launch the newly created batch */
		gem_set_domain(fd, handles[i],
			       I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU);
		copy(fd, handles[i], noop, bad);
		exec(fd, bad);
		gem_close(fd, bad);

Daniel Vetter's avatar
Daniel Vetter committed
232
		igt_progress("gem_cpu_reloc: ", 2*count+i, 3*count);
Chris Wilson's avatar
Chris Wilson committed
233 234
	}

235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
	igt_info("Subtest suceeded, cleanup up - this might take a while.\n");
	for (i = 0; i < count; i++) {
		gem_close(fd, handles[i]);
	}
	gem_close(fd, noop);
	free(handles);
}

igt_main
{
	uint64_t aper_size;
	int fd, count;

	igt_fixture {
		fd = drm_open_any();
	}

	igt_subtest("basic") {
		run_test (fd, 10);
	}

	igt_skip_on_simulation();

	igt_subtest("full") {
		aper_size = gem_mappable_aperture_size();
		count = aper_size / 4096 * 2;

		/* count + 2 (noop & bad) buffers. A gem object appears to
                   require about 2kb + buffer + kernel overhead */
		intel_require_memory(2+count, 2048+4096, CHECK_RAM);

		run_test (fd, count);
	}

	igt_fixture {
		close(fd);
	}
Chris Wilson's avatar
Chris Wilson committed
272
}