Prepare for 64bit relocation addresses
[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                 OUT_BATCH(0 << 16 | 0);
96                 OUT_BATCH(pitch);
97                 OUT_RELOC_FENCED(dummy_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
98                 ADVANCE_BATCH();
99
100                 if (IS_GEN6(devid) || IS_GEN7(devid)) {
101                         BEGIN_BATCH(3);
102                         OUT_BATCH(XY_SETUP_CLIP_BLT_CMD);
103                         OUT_BATCH(0);
104                         OUT_BATCH(0);
105                         ADVANCE_BATCH();
106                 }
107         }
108         intel_batchbuffer_flush(batch);
109
110         drm_intel_bo_unreference(dummy_bo);
111 }
112
113 static uint32_t
114 tiled_bo_create (int fd)
115 {
116         uint32_t handle;
117
118         handle = gem_create(fd, OBJECT_SIZE);
119
120         gem_set_tiling(fd, handle, I915_TILING_X, WIDTH*4);
121
122         return handle;
123 }
124
125 static uint32_t
126 batch_create (int fd)
127 {
128         uint32_t buf[] = { MI_BATCH_BUFFER_END, 0 };
129         uint32_t batch_handle;
130
131         batch_handle = gem_create(fd, BATCH_SIZE);
132
133         gem_write(fd, batch_handle, 0, buf, sizeof(buf));
134
135         return batch_handle;
136 }
137
138 static void fill_reloc(struct drm_i915_gem_relocation_entry *reloc, uint32_t handle)
139 {
140         reloc->offset = 2 * sizeof(uint32_t);
141         reloc->target_handle = handle;
142         reloc->read_domains = I915_GEM_DOMAIN_RENDER;
143         reloc->write_domain = 0;
144 }
145
146 #define BUSY_LOAD (1 << 0)
147 #define INTERRUPTIBLE (1 << 1)
148
149 static void run_test(int fd, int num_fences, int expected_errno,
150                      unsigned flags)
151 {
152         struct drm_i915_gem_execbuffer2 execbuf[2];
153         struct drm_i915_gem_exec_object2 exec[2][2*MAX_FENCES+3];
154         struct drm_i915_gem_relocation_entry reloc[2*MAX_FENCES+2];
155
156         int i, n;
157         int loop = 1000;
158
159         if (flags & BUSY_LOAD) {
160                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
161                 batch = intel_batchbuffer_alloc(bufmgr, devid);
162
163                 /* Takes forever otherwise. */
164                 loop = 50;
165         }
166
167         if (flags & INTERRUPTIBLE)
168                 igt_fork_signal_helper();
169
170         memset(execbuf, 0, sizeof(execbuf));
171         memset(exec, 0, sizeof(exec));
172         memset(reloc, 0, sizeof(reloc));
173
174         for (n = 0; n < 2*num_fences; n++) {
175                 uint32_t handle = tiled_bo_create(fd);
176                 exec[1][2*num_fences - n-1].handle = exec[0][n].handle = handle;
177                 fill_reloc(&reloc[n], handle);
178         }
179
180         for (i = 0; i < 2; i++) {
181                 for (n = 0; n < num_fences; n++)
182                         exec[i][n].flags = EXEC_OBJECT_NEEDS_FENCE;
183
184                 exec[i][2*num_fences].handle = batch_create(fd);
185                 exec[i][2*num_fences].relocs_ptr = (uintptr_t)reloc;
186                 exec[i][2*num_fences].relocation_count = 2*num_fences;
187
188                 execbuf[i].buffers_ptr = (uintptr_t)exec[i];
189                 execbuf[i].buffer_count = 2*num_fences+1;
190                 execbuf[i].batch_len = 2*sizeof(uint32_t);
191         }
192
193         do {
194                 int ret;
195
196                 if (flags & BUSY_LOAD)
197                         emit_dummy_load();
198
199                 ret = drmIoctl(fd,
200                                DRM_IOCTL_I915_GEM_EXECBUFFER2,
201                                &execbuf[0]);
202                 igt_assert(expected_errno ?
203                        ret < 0 && errno == expected_errno :
204                        ret == 0);
205
206                 ret = drmIoctl(fd,
207                                DRM_IOCTL_I915_GEM_EXECBUFFER2,
208                                &execbuf[1]);
209                 igt_assert(expected_errno ?
210                        ret < 0 && errno == expected_errno :
211                        ret == 0);
212         } while (--loop);
213
214         if (flags & INTERRUPTIBLE)
215                 igt_stop_signal_helper();
216 }
217
218 int fd;
219 int num_fences;
220
221 igt_main
222 {
223         igt_skip_on_simulation();
224
225         igt_fixture {
226                 fd = drm_open_any();
227                 num_fences = gem_available_fences(fd);
228                 igt_assert(num_fences > 4);
229                 devid = intel_get_drm_devid(fd);
230
231                 igt_assert(num_fences <= MAX_FENCES);
232         }
233
234         igt_subtest("2-spare-fences")
235                 run_test(fd, num_fences - 2, 0, 0);
236         for (unsigned flags = 0; flags < 4; flags++) {
237                 igt_subtest_f("no-spare-fences%s%s",
238                               flags & BUSY_LOAD ? "-busy" : "",
239                               flags & INTERRUPTIBLE ? "-interruptible" : "")
240                         run_test(fd, num_fences, 0, flags);
241         }
242         igt_subtest("too-many-fences")
243                 run_test(fd, num_fences + 1, intel_gen(devid) >= 4 ? 0 : EDEADLK, 0);
244
245         igt_fixture
246                 close(fd);
247 }