Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 1 | /* |
| 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 Wood | 804e11f | 2015-08-17 17:57:43 +0100 | [diff] [blame] | 28 | #include "igt.h" |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 29 | #include <stdlib.h> |
| 30 | #include <stdio.h> |
| 31 | #include <string.h> |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 32 | #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 Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 38 | #include "intel_bufmgr.h" |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 39 | |
| 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 Wood | b2ac264 | 2014-11-28 11:02:44 +0000 | [diff] [blame] | 45 | IGT_TEST_DESCRIPTION("Test whether the kernel rejects relocations with non-gpu" |
| 46 | " domains."); |
| 47 | |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 48 | static drm_intel_bufmgr *bufmgr; |
| 49 | struct intel_batchbuffer *batch; |
| 50 | |
| 51 | #define BAD_GTT_DEST ((512*1024*1024)) /* past end of aperture */ |
| 52 | |
| 53 | static int |
Daniel Vetter | a7a80c2 | 2012-01-10 15:50:20 +0100 | [diff] [blame] | 54 | run_batch(void) |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 55 | { |
| 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 Roper | 07be8fe | 2015-03-05 15:01:00 -0800 | [diff] [blame] | 74 | igt_assert_eq(ret, 0); |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 75 | |
| 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 Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 85 | #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 | |
| 92 | static 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 Wilson | 4de67b2 | 2017-01-02 11:05:21 +0000 | [diff] [blame] | 114 | exec[1].relocs_ptr = to_user_pointer(reloc); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 115 | 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 Wilson | 4de67b2 | 2017-01-02 11:05:21 +0000 | [diff] [blame] | 128 | execbuf.buffers_ptr = to_user_pointer(exec); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 129 | 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 Wilson | c1404e0 | 2014-04-29 07:14:33 +0100 | [diff] [blame] | 143 | igt_assert(ret != 0 && errno == EINVAL); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 144 | |
| 145 | gem_close(fd, handle); |
| 146 | gem_close(fd, handle_target); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 147 | } |
| 148 | |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 149 | int fd; |
| 150 | drm_intel_bo *tmp; |
| 151 | |
Daniel Vetter | 071e9ca | 2013-10-31 16:23:26 +0100 | [diff] [blame] | 152 | igt_main |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 153 | { |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 154 | igt_fixture { |
Micah Fedke | c81d293 | 2015-07-22 21:54:02 +0000 | [diff] [blame] | 155 | fd = drm_open_driver(DRIVER_INTEL); |
Chris Wilson | 9518cb5 | 2017-02-22 15:24:54 +0000 | [diff] [blame] | 156 | igt_require_gem(fd); |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 157 | |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 158 | 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 Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 161 | |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 162 | tmp = drm_intel_bo_alloc(bufmgr, "tmp", 128 * 128, 4096); |
| 163 | } |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 164 | |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 165 | igt_subtest("cpu-domain") { |
Chris Wilson | 10552b5 | 2014-08-30 11:44:51 +0100 | [diff] [blame] | 166 | BEGIN_BATCH(2, 1); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 167 | OUT_BATCH(0); |
| 168 | OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, 0, 0); |
| 169 | ADVANCE_BATCH(); |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 170 | igt_assert(run_batch() == -EINVAL); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 171 | |
Chris Wilson | 10552b5 | 2014-08-30 11:44:51 +0100 | [diff] [blame] | 172 | BEGIN_BATCH(2, 1); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 173 | OUT_BATCH(0); |
| 174 | OUT_RELOC(tmp, I915_GEM_DOMAIN_CPU, I915_GEM_DOMAIN_CPU, 0); |
| 175 | ADVANCE_BATCH(); |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 176 | igt_assert(run_batch() == -EINVAL); |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 177 | } |
| 178 | |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 179 | igt_subtest("gtt-domain") { |
Chris Wilson | 10552b5 | 2014-08-30 11:44:51 +0100 | [diff] [blame] | 180 | BEGIN_BATCH(2, 1); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 181 | OUT_BATCH(0); |
| 182 | OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, 0, 0); |
| 183 | ADVANCE_BATCH(); |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 184 | igt_assert(run_batch() == -EINVAL); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 185 | |
Chris Wilson | 10552b5 | 2014-08-30 11:44:51 +0100 | [diff] [blame] | 186 | BEGIN_BATCH(2, 1); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 187 | OUT_BATCH(0); |
| 188 | OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT, I915_GEM_DOMAIN_GTT, 0); |
| 189 | ADVANCE_BATCH(); |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 190 | igt_assert(run_batch() == -EINVAL); |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 191 | } |
| 192 | |
Daniel Vetter | 44d4a3d | 2013-09-04 14:43:06 +0200 | [diff] [blame] | 193 | /* Note: Older kernels disallow this. Punt on the skip check though |
| 194 | * since this is too old. */ |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 195 | igt_subtest("conflicting-write-domain") { |
Chris Wilson | 10552b5 | 2014-08-30 11:44:51 +0100 | [diff] [blame] | 196 | BEGIN_BATCH(4, 2); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 197 | 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 Vetter | 3c46789 | 2013-09-04 14:27:17 +0200 | [diff] [blame] | 204 | igt_assert(run_batch() == 0); |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 205 | } |
| 206 | |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 207 | igt_subtest("double-write-domain") |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 208 | multi_write_domain(fd); |
| 209 | |
Daniel Vetter | 1caaf0a | 2013-08-12 12:17:35 +0200 | [diff] [blame] | 210 | igt_subtest("invalid-gpu-domain") { |
Chris Wilson | 10552b5 | 2014-08-30 11:44:51 +0100 | [diff] [blame] | 211 | BEGIN_BATCH(2, 1); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 212 | 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 Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 216 | igt_assert(run_batch() == -EINVAL); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 217 | |
Chris Wilson | 10552b5 | 2014-08-30 11:44:51 +0100 | [diff] [blame] | 218 | BEGIN_BATCH(2, 1); |
Daniel Vetter | d8df90d | 2012-12-01 00:51:53 +0100 | [diff] [blame] | 219 | OUT_BATCH(0); |
| 220 | OUT_RELOC(tmp, I915_GEM_DOMAIN_GTT << 1, |
| 221 | I915_GEM_DOMAIN_GTT << 1, 0); |
| 222 | ADVANCE_BATCH(); |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 223 | igt_assert(run_batch() == -EINVAL); |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 224 | } |
| 225 | |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 226 | igt_fixture { |
| 227 | intel_batchbuffer_free(batch); |
| 228 | drm_intel_bufmgr_destroy(bufmgr); |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 229 | |
Daniel Vetter | b3880d3 | 2013-08-14 18:02:46 +0200 | [diff] [blame] | 230 | close(fd); |
| 231 | } |
Daniel Vetter | ab3f4bd | 2011-12-02 18:41:34 +0100 | [diff] [blame] | 232 | } |