blob: 45ea8bad4fc06c4aa5556ed96d2bd4e32e2f86f1 [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);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100156
Daniel Vetterb3880d32013-08-14 18:02:46 +0200157 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
158 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
159 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100160
Daniel Vetterb3880d32013-08-14 18:02:46 +0200161 tmp = drm_intel_bo_alloc(bufmgr, "tmp", 128 * 128, 4096);
162 }
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100163
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200164 igt_subtest("cpu-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100165 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100166 OUT_BATCH(0);
167 OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, 0, 0);
168 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200169 igt_assert(run_batch() == -EINVAL);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100170
Chris Wilson10552b52014-08-30 11:44:51 +0100171 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100172 OUT_BATCH(0);
173 OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU, 0);
174 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200175 igt_assert(run_batch() == -EINVAL);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100176 }
177
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200178 igt_subtest("gtt-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100179 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100180 OUT_BATCH(0);
181 OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, 0, 0);
182 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200183 igt_assert(run_batch() == -EINVAL);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100184
Chris Wilson10552b52014-08-30 11:44:51 +0100185 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100186 OUT_BATCH(0);
187 OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT, 0);
188 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200189 igt_assert(run_batch() == -EINVAL);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100190 }
191
Daniel Vetter44d4a3d2013-09-04 14:43:06 +0200192 /* Note: Older kernels disallow this. Punt on the skip check though
193 * since this is too old. */
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200194 igt_subtest("conflicting-write-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100195 BEGIN_BATCH(4, 2);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100196 OUT_BATCH(0);
197 OUT_RELOC(tmp, I915_GEM_DOMAIN_RENDER,
198 I915_GEM_DOMAIN_RENDER, 0);
199 OUT_BATCH(0);
200 OUT_RELOC(tmp, I915_GEM_DOMAIN_INSTRUCTION,
201 I915_GEM_DOMAIN_INSTRUCTION, 0);
202 ADVANCE_BATCH();
Daniel Vetter3c467892013-09-04 14:27:17 +0200203 igt_assert(run_batch() == 0);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100204 }
205
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200206 igt_subtest("double-write-domain")
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100207 multi_write_domain(fd);
208
Daniel Vetter1caaf0a2013-08-12 12:17:35 +0200209 igt_subtest("invalid-gpu-domain") {
Chris Wilson10552b52014-08-30 11:44:51 +0100210 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100211 OUT_BATCH(0);
212 OUT_RELOC(tmp, ~(I915_GEM_GPU_DOMAINS | I915_GEM_DOMAIN_GTT | I915_GEM_DOMAIN_CPU),
213 0, 0);
214 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200215 igt_assert(run_batch() == -EINVAL);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100216
Chris Wilson10552b52014-08-30 11:44:51 +0100217 BEGIN_BATCH(2, 1);
Daniel Vetterd8df90d2012-12-01 00:51:53 +0100218 OUT_BATCH(0);
219 OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT << 1,
220 I915_GEM_DOMAIN_GTT << 1, 0);
221 ADVANCE_BATCH();
Daniel Vetterb3880d32013-08-14 18:02:46 +0200222 igt_assert(run_batch() == -EINVAL);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100223 }
224
Daniel Vetterb3880d32013-08-14 18:02:46 +0200225 igt_fixture {
226 intel_batchbuffer_free(batch);
227 drm_intel_bufmgr_destroy(bufmgr);
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100228
Daniel Vetterb3880d32013-08-14 18:02:46 +0200229 close(fd);
230 }
Daniel Vetterab3f4bd2011-12-02 18:41:34 +0100231}