gem_exec_bad_domains.c 6.56 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 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
/*
 * Copyright © 2011 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:
 *    Daniel Vetter <daniel.vetter@ffwll.ch>
 *
 */

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <fcntl.h>
#include <inttypes.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/time.h>
#include "drm.h"
#include "i915_drm.h"
#include "drmtest.h"
#include "intel_bufmgr.h"
#include "intel_batchbuffer.h"
#include "intel_gpu_tools.h"

/* Testcase: Test whether the kernel rejects relocations with non-gpu domains
 *
 * If it does not, it'll oops somewhen later on because we don't expect that.
 */

static drm_intel_bufmgr *bufmgr;
struct intel_batchbuffer *batch;

#define BAD_GTT_DEST ((512*1024*1024)) /* past end of aperture */

static int
Daniel Vetter's avatar
Daniel Vetter committed
55
run_batch(void)
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 85
{
	unsigned int used = batch->ptr - batch->buffer;
	int ret;

	if (used == 0)
		return 0;

	/* Round batchbuffer usage to 2 DWORDs. */
	if ((used & 4) == 0) {
		*(uint32_t *) (batch->ptr) = 0; /* noop */
		batch->ptr += 4;
	}

	/* Mark the end of the buffer. */
	*(uint32_t *)(batch->ptr) = MI_BATCH_BUFFER_END; /* noop */
	batch->ptr += 4;
	used = batch->ptr - batch->buffer;

	ret = drm_intel_bo_subdata(batch->bo, 0, used, batch->buffer);
	assert(ret == 0);

	batch->ptr = NULL;

	ret = drm_intel_bo_mrb_exec(batch->bo, used, NULL, 0, 0, 0);

	intel_batchbuffer_reset(batch);

	return ret;
}

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 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153
#define I915_GEM_GPU_DOMAINS \
	(I915_GEM_DOMAIN_RENDER | \
	 I915_GEM_DOMAIN_SAMPLER | \
	 I915_GEM_DOMAIN_COMMAND | \
	 I915_GEM_DOMAIN_INSTRUCTION | \
	 I915_GEM_DOMAIN_VERTEX)

static void multi_write_domain(int fd)
{
	struct drm_i915_gem_execbuffer2 execbuf;
	struct drm_i915_gem_exec_object2 exec[2];
	struct drm_i915_gem_relocation_entry reloc[1];
	uint32_t handle, handle_target;
	int ret;

	handle = gem_create(fd, 4096);
	handle_target = gem_create(fd, 4096);

	exec[0].handle = handle_target;
	exec[0].relocation_count = 0;
	exec[0].relocs_ptr = 0;
	exec[0].alignment = 0;
	exec[0].offset = 0;
	exec[0].flags = 0;
	exec[0].rsvd1 = 0;
	exec[0].rsvd2 = 0;

	exec[1].handle = handle;
	exec[1].relocation_count = 1;
	exec[1].relocs_ptr = (uintptr_t) reloc;
	exec[1].alignment = 0;
	exec[1].offset = 0;
	exec[1].flags = 0;
	exec[1].rsvd1 = 0;
	exec[1].rsvd2 = 0;

	reloc[0].offset = 4;
	reloc[0].delta = 0;
	reloc[0].target_handle = handle_target;
	reloc[0].read_domains = I915_GEM_DOMAIN_RENDER | I915_GEM_DOMAIN_INSTRUCTION;
	reloc[0].write_domain = I915_GEM_DOMAIN_RENDER | I915_GEM_DOMAIN_INSTRUCTION;
	reloc[0].presumed_offset = 0;

	execbuf.buffers_ptr = (uintptr_t)exec;
	execbuf.buffer_count = 2;
	execbuf.batch_start_offset = 0;
	execbuf.batch_len = 8;
	execbuf.cliprects_ptr = 0;
	execbuf.num_cliprects = 0;
	execbuf.DR1 = 0;
	execbuf.DR4 = 0;
	execbuf.flags = 0;
	i915_execbuffer2_set_context_id(execbuf, 0);
	execbuf.rsvd2 = 0;

	ret = drmIoctl(fd,
		       DRM_IOCTL_I915_GEM_EXECBUFFER2,
		       &execbuf);

	gem_close(fd, handle);
	gem_close(fd, handle_target);

	if (ret == 0 || errno != EINVAL) {
		fprintf(stderr, "multiple write domains not rejected\n");
		exit(1);
	}
}

154 155 156 157 158
int main(int argc, char **argv)
{
	int fd, ret;
	drm_intel_bo *tmp;

159 160
	drmtest_subtest_init(argc, argv);

161 162 163 164 165 166 167 168
	fd = drm_open_any();

	bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
	drm_intel_bufmgr_gem_enable_reuse(bufmgr);
	batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));

	tmp = drm_intel_bo_alloc(bufmgr, "tmp", 128 * 128, 4096);

169
	drmtest_subtest("cpu-domain") {
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188
		BEGIN_BATCH(2);
		OUT_BATCH(0);
		OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, 0, 0);
		ADVANCE_BATCH();
		ret = run_batch();
		if (ret != -EINVAL) {
			fprintf(stderr, "(cpu, 0) reloc not rejected\n");
			exit(1);
		}

		BEGIN_BATCH(2);
		OUT_BATCH(0);
		OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU, 0);
		ADVANCE_BATCH();
		ret = run_batch();
		if (ret != -EINVAL) {
			fprintf(stderr, "(cpu, cpu) reloc not rejected\n");
			exit(1);
		}
189 190
	}

191
	drmtest_subtest("gtt-domain") {
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
		BEGIN_BATCH(2);
		OUT_BATCH(0);
		OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, 0, 0);
		ADVANCE_BATCH();
		ret = run_batch();
		if (ret != -EINVAL) {
			fprintf(stderr, "(gtt, 0) reloc not rejected\n");
			exit(1);
		}

		BEGIN_BATCH(2);
		OUT_BATCH(0);
		OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT, 0);
		ADVANCE_BATCH();
		ret = run_batch();
		if (ret != -EINVAL) {
			fprintf(stderr, "(gtt, gtt) reloc not rejected\n");
			exit(1);
		}
211 212
	}

213 214
#if 0 /* kernel checks have been eased, doesn't reject conflicting write domains
	 any more */
215
	drmtest_subtest("conflicting-write-domain") {
216 217 218 219 220 221 222 223 224 225 226 227 228
		BEGIN_BATCH(4);
		OUT_BATCH(0);
		OUT_RELOC(tmp, I915_GEM_DOMAIN_RENDER,
			  I915_GEM_DOMAIN_RENDER, 0);
		OUT_BATCH(0);
		OUT_RELOC(tmp, I915_GEM_DOMAIN_INSTRUCTION,
			  I915_GEM_DOMAIN_INSTRUCTION, 0);
		ADVANCE_BATCH();
		ret = run_batch();
		if (ret != -EINVAL) {
			fprintf(stderr, "conflicting write domains not rejected\n");
			exit(1);
		}
229
	}
230
#endif
231

232
	drmtest_subtest("double-write-domain")
233 234
		multi_write_domain(fd);

235
	drmtest_subtest("invalid-gpu-domain") {
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
		BEGIN_BATCH(2);
		OUT_BATCH(0);
		OUT_RELOC(tmp, ~(I915_GEM_GPU_DOMAINS | I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU),
			  0, 0);
		ADVANCE_BATCH();
		ret = run_batch();
		if (ret != -EINVAL) {
			fprintf(stderr, "invalid gpu read domains not rejected\n");
			exit(1);
		}

		BEGIN_BATCH(2);
		OUT_BATCH(0);
		OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT << 1,
			  I915_GEM_DOMAIN_GTT << 1, 0);
		ADVANCE_BATCH();
		ret = run_batch();
		if (ret != -EINVAL) {
			fprintf(stderr, "invalid gpu domain not rejected\n");
			exit(1);
		}
257 258 259 260 261 262 263 264 265
	}

	intel_batchbuffer_free(batch);
	drm_intel_bufmgr_destroy(bufmgr);

	close(fd);

	return 0;
}