blob: 9a7487e334f42d2e9286fa18fd030260a29679a8 [file] [log] [blame]
Daniel Vetterab3f4bd2011-12-02 18:41:34 +01001/*
2 * Copyright © 2011 Intel Corporation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
13 * Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
21 * IN THE SOFTWARE.
22 *
23 * Authors:
24 * Daniel Vetter <daniel.vetter@ffwll.ch>
25 *
26 */
27
Thomas Wood804e11f2015-08-17 17:57:43 +010028#include "igt.h"
Daniel Vetterab3f4bd2011-12-02 18:41:34 +010029#include <stdlib.h>
30#include <stdio.h>
31#include <string.h>
Daniel Vetterab3f4bd2011-12-02 18:41:34 +010032#include <fcntl.h>
33#include <inttypes.h>
34#include <errno.h>
35#include <sys/stat.h>
36#include <sys/time.h>
37#include "drm.h"
Daniel Vetterab3f4bd2011-12-02 18:41:34 +010038#include "intel_bufmgr.h"
Daniel Vetterab3f4bd2011-12-02 18:41:34 +010039
40/* Testcase: Test whether the kernel rejects relocations with non-gpu domains
41 *
42 * If it does not, it'll oops somewhen later on because we don't expect that.
43 */
44
Thomas Woodb2ac2642014-11-28 11:02:44 +000045IGT_TEST_DESCRIPTION("Test whether the kernel rejects relocations with non-gpu"
46 " domains.");
47
Daniel Vetterab3f4bd2011-12-02 18:41:34 +010048static drm_intel_bufmgr *bufmgr;
49struct intel_batchbuffer *batch;
50
51#define BAD_GTT_DEST ((512*1024*1024)) /* past end of aperture */
52
53static int
Daniel Vettera7a80c22012-01-10 15:50:20 +010054run_batch(void)
Daniel Vetterab3f4bd2011-12-02 18:41:34 +010055{
56 unsigned int used = batch->ptr - batch->buffer;
57 int ret;
58
59 if (used == 0)
60 return 0;
61
62 /* Round batchbuffer usage to 2 DWORDs. */
63 if ((used & 4) == 0) {
64 *(uint32_t *) (batch->ptr) = 0; /* noop */
65 batch->ptr += 4;
66 }
67
68 /* Mark the end of the buffer. */
69 *(uint32_t *)(batch->ptr) = MI_BATCH_BUFFER_END; /* noop */
70 batch->ptr += 4;
71 used = batch->ptr - batch->buffer;
72
73 ret = drm_intel_bo_subdata(batch->bo, 0, used, batch->buffer);
Matt Roper07be8fe2015-03-05 15:01:00 -080074 igt_assert_eq(ret, 0);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +010075
76 batch->ptr = NULL;
77
78 ret = drm_intel_bo_mrb_exec(batch->bo, used, NULL, 0, 0, 0);
79
80 intel_batchbuffer_reset(batch);
81
82 return ret;
83}
84
Daniel Vetterd8df90d2012-12-01 00:51:53 +010085#define I915_GEM_GPU_DOMAINS \
86 (I915_GEM_DOMAIN_RENDER | \
87 I915_GEM_DOMAIN_SAMPLER | \
88 I915_GEM_DOMAIN_COMMAND | \
89 I915_GEM_DOMAIN_INSTRUCTION | \
90 I915_GEM_DOMAIN_VERTEX)
91
92static void multi_write_domain(int fd)
93{
94 struct drm_i915_gem_execbuffer2 execbuf;
95 struct drm_i915_gem_exec_object2 exec[2];
96 struct drm_i915_gem_relocation_entry reloc[1];
97 uint32_t handle, handle_target;
98 int ret;
99
100 handle = gem_create(fd, 4096);
101 handle_target = gem_create(fd, 4096);
102
103 exec[0].handle = handle_target;
104 exec[0].relocation_count = 0;
105 exec[0].relocs_ptr = 0;
106 exec[0].alignment = 0;
107 exec[0].offset = 0;
108 exec[0].flags = 0;
109 exec[0].rsvd1 = 0;
110 exec[0].rsvd2 = 0;
111
112 exec[1].handle = handle;
113 exec[1].relocation_count = 1;
Chris Wilson4de67b22017-01-02 11:05:21 +0000114 exec[1].relocs_ptr = to_user_pointer(reloc);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100115 exec[1].alignment = 0;
116 exec[1].offset = 0;
117 exec[1].flags = 0;
118 exec[1].rsvd1 = 0;
119 exec[1].rsvd2 = 0;
120
121 reloc[0].offset = 4;
122 reloc[0].delta = 0;
123 reloc[0].target_handle = handle_target;
124 reloc[0].read_domains = I915_GEM_DOMAIN_RENDER | I915_GEM_DOMAIN_INSTRUCTION;
125 reloc[0].write_domain = I915_GEM_DOMAIN_RENDER | I915_GEM_DOMAIN_INSTRUCTION;
126 reloc[0].presumed_offset = 0;
127
Chris Wilson4de67b22017-01-02 11:05:21 +0000128 execbuf.buffers_ptr = to_user_pointer(exec);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100129 execbuf.buffer_count = 2;
130 execbuf.batch_start_offset = 0;
131 execbuf.batch_len = 8;
132 execbuf.cliprects_ptr = 0;
133 execbuf.num_cliprects = 0;
134 execbuf.DR1 = 0;
135 execbuf.DR4 = 0;
136 execbuf.flags = 0;
137 i915_execbuffer2_set_context_id(execbuf, 0);
138 execbuf.rsvd2 = 0;
139
140 ret = drmIoctl(fd,
141 DRM_IOCTL_I915_GEM_EXECBUFFER2,
142 &execbuf);
Chris Wilsonc1404e02014-04-29 07:14:33 +0100143 igt_assert(ret != 0 && errno == EINVAL);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100144
145 gem_close(fd, handle);
146 gem_close(fd, handle_target);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100147}
148
Daniel Vetterb3880d32013-08-14 18:02:46 +0200149int fd;
150drm_intel_bo *tmp;
151
Daniel Vetter071e9ca2013-10-31 16:23:26 +0100152igt_main
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100153{
Daniel Vetterb3880d32013-08-14 18:02:46 +0200154 igt_fixture {
Micah Fedkec81d2932015-07-22 21:54:02 +0000155 fd = drm_open_driver(DRIVER_INTEL);
Chris Wilson9518cb52017-02-22 15:24:54 +0000156 igt_require_gem(fd);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100157
Daniel Vetterb3880d32013-08-14 18:02:46 +0200158 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
159 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
160 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100161
Daniel Vetterb3880d32013-08-14 18:02:46 +0200162 tmp = drm_intel_bo_alloc(bufmgr, "tmp", 128 * 128, 4096);
163 }
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100164
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200165 igt_subtest("cpu-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100166 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100167 OUT_BATCH(0);
168 OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, 0, 0);
169 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200170 igt_assert(run_batch() == -EINVAL);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100171
Chris Wilson10552b52014-08-30 11:44:51 +0100172 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100173 OUT_BATCH(0);
174 OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU, 0);
175 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200176 igt_assert(run_batch() == -EINVAL);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100177 }
178
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200179 igt_subtest("gtt-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100180 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100181 OUT_BATCH(0);
182 OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, 0, 0);
183 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200184 igt_assert(run_batch() == -EINVAL);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100185
Chris Wilson10552b52014-08-30 11:44:51 +0100186 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100187 OUT_BATCH(0);
188 OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT, 0);
189 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200190 igt_assert(run_batch() == -EINVAL);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100191 }
192
Daniel Vetter44d4a3d2013-09-04 14:43:06 +0200193 /* Note: Older kernels disallow this. Punt on the skip check though
194 * since this is too old. */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200195 igt_subtest("conflicting-write-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100196 BEGIN_BATCH(4, 2);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100197 OUT_BATCH(0);
198 OUT_RELOC(tmp, I915_GEM_DOMAIN_RENDER,
199 I915_GEM_DOMAIN_RENDER, 0);
200 OUT_BATCH(0);
201 OUT_RELOC(tmp, I915_GEM_DOMAIN_INSTRUCTION,
202 I915_GEM_DOMAIN_INSTRUCTION, 0);
203 ADVANCE_BATCH();
Daniel Vetter3c467892013-09-04 14:27:17 +0200204 igt_assert(run_batch() == 0);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100205 }
206
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200207 igt_subtest("double-write-domain")
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100208 multi_write_domain(fd);
209
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200210 igt_subtest("invalid-gpu-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100211 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100212 OUT_BATCH(0);
213 OUT_RELOC(tmp, ~(I915_GEM_GPU_DOMAINS | I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU),
214 0, 0);
215 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200216 igt_assert(run_batch() == -EINVAL);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100217
Chris Wilson10552b52014-08-30 11:44:51 +0100218 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100219 OUT_BATCH(0);
220 OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT << 1,
221 I915_GEM_DOMAIN_GTT << 1, 0);
222 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200223 igt_assert(run_batch() == -EINVAL);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100224 }
225
Daniel Vetterb3880d32013-08-14 18:02:46 +0200226 igt_fixture {
227 intel_batchbuffer_free(batch);
228 drm_intel_bufmgr_destroy(bufmgr);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100229
Daniel Vetterb3880d32013-08-14 18:02:46 +0200230 close(fd);
231 }
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100232}