42b7841dfd15aaf190c0bf7529b169b525a146a8
[platform/upstream/intel-gpu-tools.git] / tests / gem_fenced_exec_thrash.c
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  *    Chris Wilson <chris@chris-wilson.co.uk>
25  *
26  */
27
28 #define _GNU_SOURCE
29
30 #include <stdlib.h>
31 #include <sys/ioctl.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37
38 #include <drm.h>
39
40 #include "ioctl_wrappers.h"
41 #include "drmtest.h"
42 #include "intel_chipset.h"
43 #include "intel_io.h"
44 #include "igt_aux.h"
45
46 #define WIDTH 1024
47 #define HEIGHT 1024
48 #define OBJECT_SIZE (4*WIDTH*HEIGHT)
49
50 #define BATCH_SIZE 4096
51
52 #define MAX_FENCES 32
53
54 /*
55  * Testcase: execbuf fence accounting
56  *
57  * We had a bug where we were falsely accounting upon reservation already
58  * fenced buffers as occupying a fence register even if they did not require
59  * one for the batch.
60  *
61  * We aim to exercise this by performing a sequence of fenced BLT
62  * with 2*num_avail_fence buffers, but alternating which half are fenced in
63  * each command.
64  */
65
66 static drm_intel_bufmgr *bufmgr;
67 struct intel_batchbuffer *batch;
68 uint32_t devid;
69
70 static void emit_dummy_load(void)
71 {
72         int i;
73         uint32_t tile_flags = 0;
74         uint32_t tiling_mode = I915_TILING_X;
75         unsigned long pitch;
76         drm_intel_bo *dummy_bo;
77
78         dummy_bo = drm_intel_bo_alloc_tiled(bufmgr, "tiled dummy_bo", 2048, 2048,
79                                       4, &tiling_mode, &pitch, 0);
80
81         if (IS_965(devid)) {
82                 pitch /= 4;
83                 tile_flags = XY_SRC_COPY_BLT_SRC_TILED |
84                         XY_SRC_COPY_BLT_DST_TILED;
85         }
86
87         for (i = 0; i < 5; i++) {
88                 BLIT_COPY_BATCH_START(devid, tile_flags);
89                 OUT_BATCH((3 << 24) | /* 32 bits */
90                           (0xcc << 16) | /* copy ROP */
91                           pitch);
92                 OUT_BATCH(0 << 16 | 1024);
93                 OUT_BATCH((2048) << 16 | (2048));
94                 OUT_RELOC_FENCED(dummy_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
95                 BLIT_RELOC_UDW(devid);
96                 OUT_BATCH(0 << 16 | 0);
97                 OUT_BATCH(pitch);
98                 OUT_RELOC_FENCED(dummy_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
99                 BLIT_RELOC_UDW(devid);
100                 ADVANCE_BATCH();
101
102                 if (IS_GEN6(devid) || IS_GEN7(devid)) {
103                         BEGIN_BATCH(3);
104                         OUT_BATCH(XY_SETUP_CLIP_BLT_CMD);
105                         OUT_BATCH(0);
106                         OUT_BATCH(0);
107                         ADVANCE_BATCH();
108                 }
109         }
110         intel_batchbuffer_flush(batch);
111
112         drm_intel_bo_unreference(dummy_bo);
113 }
114
115 static uint32_t
116 tiled_bo_create (int fd)
117 {
118         uint32_t handle;
119
120         handle = gem_create(fd, OBJECT_SIZE);
121
122         gem_set_tiling(fd, handle, I915_TILING_X, WIDTH*4);
123
124         return handle;
125 }
126
127 static uint32_t
128 batch_create (int fd)
129 {
130         uint32_t buf[] = { MI_BATCH_BUFFER_END, 0 };
131         uint32_t batch_handle;
132
133         batch_handle = gem_create(fd, BATCH_SIZE);
134
135         gem_write(fd, batch_handle, 0, buf, sizeof(buf));
136
137         return batch_handle;
138 }
139
140 static void fill_reloc(struct drm_i915_gem_relocation_entry *reloc, uint32_t handle)
141 {
142         reloc->offset = 2 * sizeof(uint32_t);
143         reloc->target_handle = handle;
144         reloc->read_domains = I915_GEM_DOMAIN_RENDER;
145         reloc->write_domain = 0;
146 }
147
148 #define BUSY_LOAD (1 << 0)
149 #define INTERRUPTIBLE (1 << 1)
150
151 static void run_test(int fd, int num_fences, int expected_errno,
152                      unsigned flags)
153 {
154         struct drm_i915_gem_execbuffer2 execbuf[2];
155         struct drm_i915_gem_exec_object2 exec[2][2*MAX_FENCES+3];
156         struct drm_i915_gem_relocation_entry reloc[2*MAX_FENCES+2];
157
158         int i, n;
159         int loop = 1000;
160
161         if (flags & BUSY_LOAD) {
162                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
163                 batch = intel_batchbuffer_alloc(bufmgr, devid);
164
165                 /* Takes forever otherwise. */
166                 loop = 50;
167         }
168
169         if (flags & INTERRUPTIBLE)
170                 igt_fork_signal_helper();
171
172         memset(execbuf, 0, sizeof(execbuf));
173         memset(exec, 0, sizeof(exec));
174         memset(reloc, 0, sizeof(reloc));
175
176         for (n = 0; n < 2*num_fences; n++) {
177                 uint32_t handle = tiled_bo_create(fd);
178                 exec[1][2*num_fences - n-1].handle = exec[0][n].handle = handle;
179                 fill_reloc(&reloc[n], handle);
180         }
181
182         for (i = 0; i < 2; i++) {
183                 for (n = 0; n < num_fences; n++)
184                         exec[i][n].flags = EXEC_OBJECT_NEEDS_FENCE;
185
186                 exec[i][2*num_fences].handle = batch_create(fd);
187                 exec[i][2*num_fences].relocs_ptr = (uintptr_t)reloc;
188                 exec[i][2*num_fences].relocation_count = 2*num_fences;
189
190                 execbuf[i].buffers_ptr = (uintptr_t)exec[i];
191                 execbuf[i].buffer_count = 2*num_fences+1;
192                 execbuf[i].batch_len = 2*sizeof(uint32_t);
193         }
194
195         do {
196                 int ret;
197
198                 if (flags & BUSY_LOAD)
199                         emit_dummy_load();
200
201                 ret = drmIoctl(fd,
202                                DRM_IOCTL_I915_GEM_EXECBUFFER2,
203                                &execbuf[0]);
204                 igt_assert(expected_errno ?
205                        ret < 0 && errno == expected_errno :
206                        ret == 0);
207
208                 ret = drmIoctl(fd,
209                                DRM_IOCTL_I915_GEM_EXECBUFFER2,
210                                &execbuf[1]);
211                 igt_assert(expected_errno ?
212                        ret < 0 && errno == expected_errno :
213                        ret == 0);
214         } while (--loop);
215
216         if (flags & INTERRUPTIBLE)
217                 igt_stop_signal_helper();
218 }
219
220 int fd;
221 int num_fences;
222
223 igt_main
224 {
225         igt_skip_on_simulation();
226
227         igt_fixture {
228                 fd = drm_open_any();
229                 num_fences = gem_available_fences(fd);
230                 igt_assert(num_fences > 4);
231                 devid = intel_get_drm_devid(fd);
232
233                 igt_assert(num_fences <= MAX_FENCES);
234         }
235
236         igt_subtest("2-spare-fences")
237                 run_test(fd, num_fences - 2, 0, 0);
238         for (unsigned flags = 0; flags < 4; flags++) {
239                 igt_subtest_f("no-spare-fences%s%s",
240                               flags & BUSY_LOAD ? "-busy" : "",
241                               flags & INTERRUPTIBLE ? "-interruptible" : "")
242                         run_test(fd, num_fences, 0, flags);
243         }
244         igt_subtest("too-many-fences")
245                 run_test(fd, num_fences + 1, intel_gen(devid) >= 4 ? 0 : EDEADLK, 0);
246
247         igt_fixture
248                 close(fd);
249 }