packaging: only on x86* arch
[platform/upstream/intel-gpu-tools.git] / tests / gem_reloc_vs_gpu.c
1 /*
2  * Copyright © 2011,2013 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
28 #define _GNU_SOURCE
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/time.h>
37 #include <signal.h>
38 #include <sys/wait.h>
39
40 #include <drm.h>
41
42 #include "ioctl_wrappers.h"
43 #include "drmtest.h"
44 #include "intel_chipset.h"
45 #include "intel_io.h"
46 #include "igt_debugfs.h"
47 #include "igt_aux.h"
48
49 /*
50  * Testcase: Kernel relocations vs. gpu races
51  *
52  */
53
54 static drm_intel_bufmgr *bufmgr;
55 struct intel_batchbuffer *batch;
56
57 uint32_t blob[2048*2048];
58 #define NUM_TARGET_BOS 16
59 drm_intel_bo *pc_target_bo[NUM_TARGET_BOS];
60 drm_intel_bo *dummy_bo;
61 drm_intel_bo *special_bo;
62 uint32_t devid;
63 int special_reloc_ofs;
64 int special_batch_len;
65
66 static void create_special_bo(void)
67 {
68         uint32_t data[1024];
69         int len = 0;
70         int small_pitch = 64;
71 #define BATCH(dw) data[len++] = (dw);
72
73         memset(data, 0, 4096);
74         special_bo = drm_intel_bo_alloc(bufmgr, "special batch", 4096, 4096);
75
76         if (intel_gen(devid) >= 8) {
77                 BATCH(MI_NOOP);
78                 BATCH(XY_COLOR_BLT_CMD_NOLEN | 5 |
79                                 COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
80         } else {
81                 BATCH(XY_COLOR_BLT_CMD_NOLEN | 4 |
82                                 COLOR_BLT_WRITE_ALPHA | XY_COLOR_BLT_WRITE_RGB);
83         }
84
85         BATCH((3 << 24) | (0xf0 << 16) | small_pitch);
86         BATCH(0);
87         BATCH(1 << 16 | 1);
88         special_reloc_ofs = 4*len;
89         BATCH(0);
90         if (intel_gen(devid) >= 8)
91                 BATCH(0);
92         BATCH(0xdeadbeef);
93
94 #define CMD_POLY_STIPPLE_OFFSET       0x7906
95         /* batchbuffer end */
96         if (IS_GEN5(batch->devid)) {
97                 BATCH(CMD_POLY_STIPPLE_OFFSET << 16);
98                 BATCH(0);
99         }
100         igt_assert(len % 2 == 0);
101         BATCH(MI_NOOP);
102         BATCH(MI_BATCH_BUFFER_END);
103
104         drm_intel_bo_subdata(special_bo, 0, 4096, data);
105         special_batch_len = len*4;
106 }
107
108 static void emit_dummy_load(int pitch)
109 {
110         int i;
111         uint32_t tile_flags = 0;
112
113         if (IS_965(devid)) {
114                 pitch /= 4;
115                 tile_flags = XY_SRC_COPY_BLT_SRC_TILED |
116                         XY_SRC_COPY_BLT_DST_TILED;
117         }
118
119         for (i = 0; i < 10; i++) {
120                 BLIT_COPY_BATCH_START(tile_flags);
121                 OUT_BATCH((3 << 24) | /* 32 bits */
122                           (0xcc << 16) | /* copy ROP */
123                           pitch);
124                 OUT_BATCH(0 << 16 | 1024);
125                 OUT_BATCH((2048) << 16 | (2048));
126                 OUT_RELOC_FENCED(dummy_bo, I915_GEM_DOMAIN_RENDER, I915_GEM_DOMAIN_RENDER, 0);
127                 OUT_BATCH(0 << 16 | 0);
128                 OUT_BATCH(pitch);
129                 OUT_RELOC_FENCED(dummy_bo, I915_GEM_DOMAIN_RENDER, 0, 0);
130                 ADVANCE_BATCH();
131
132                 if (batch->gen >= 6) {
133                         BEGIN_BATCH(3, 0);
134                         OUT_BATCH(XY_SETUP_CLIP_BLT_CMD);
135                         OUT_BATCH(0);
136                         OUT_BATCH(0);
137                         ADVANCE_BATCH();
138                 }
139         }
140         intel_batchbuffer_flush(batch);
141 }
142
143 static void faulting_reloc_and_emit(int fd, drm_intel_bo *target_bo)
144 {
145         struct drm_i915_gem_execbuffer2 execbuf;
146         struct drm_i915_gem_exec_object2 exec[2];
147         struct drm_i915_gem_relocation_entry reloc[1];
148         uint32_t handle_relocs;
149         void *gtt_relocs;
150         int ring;
151
152         if (intel_gen(devid) >= 6)
153                 ring = I915_EXEC_BLT;
154         else
155                 ring = 0;
156
157         exec[0].handle = target_bo->handle;
158         exec[0].relocation_count = 0;
159         exec[0].relocs_ptr = 0;
160         exec[0].alignment = 0;
161         exec[0].offset = 0;
162         exec[0].flags = 0;
163         exec[0].rsvd1 = 0;
164         exec[0].rsvd2 = 0;
165
166         reloc[0].offset = special_reloc_ofs;
167         reloc[0].delta = 0;
168         reloc[0].target_handle = target_bo->handle;
169         reloc[0].read_domains = I915_GEM_DOMAIN_RENDER;
170         reloc[0].write_domain = I915_GEM_DOMAIN_RENDER;
171         reloc[0].presumed_offset = 0;
172
173         handle_relocs = gem_create(fd, 4096);
174         gem_write(fd, handle_relocs, 0, reloc, sizeof(reloc));
175         gtt_relocs = gem_mmap(fd, handle_relocs, 4096,
176                               PROT_READ | PROT_WRITE);
177         igt_assert(gtt_relocs);
178
179         exec[1].handle = special_bo->handle;
180         exec[1].relocation_count = 1;
181         /* A newly mmap gtt bo will fault on first access. */
182         exec[1].relocs_ptr = (uintptr_t)gtt_relocs;
183         exec[1].alignment = 0;
184         exec[1].offset = 0;
185         exec[1].flags = 0;
186         exec[1].rsvd1 = 0;
187         exec[1].rsvd2 = 0;
188
189         execbuf.buffers_ptr = (uintptr_t)exec;
190         execbuf.buffer_count = 2;
191         execbuf.batch_start_offset = 0;
192         execbuf.batch_len = special_batch_len;
193         execbuf.cliprects_ptr = 0;
194         execbuf.num_cliprects = 0;
195         execbuf.DR1 = 0;
196         execbuf.DR4 = 0;
197         execbuf.flags = ring;
198         i915_execbuffer2_set_context_id(execbuf, 0);
199         execbuf.rsvd2 = 0;
200
201         gem_execbuf(fd, &execbuf);
202
203         gem_close(fd, handle_relocs);
204 }
205
206 static void reloc_and_emit(int fd, drm_intel_bo *target_bo)
207 {
208         int ring;
209
210         if (intel_gen(devid) >= 6)
211                 ring = I915_EXEC_BLT;
212         else
213                 ring = 0;
214
215         drm_intel_bo_emit_reloc(special_bo, special_reloc_ofs,
216                                 target_bo,
217                                 0,
218                                 I915_GEM_DOMAIN_RENDER,
219                                 I915_GEM_DOMAIN_RENDER);
220         drm_intel_bo_mrb_exec(special_bo, special_batch_len, NULL,
221                               0, 0, ring);
222
223 }
224
225 static void do_test(int fd, bool faulting_reloc)
226 {
227         uint32_t tiling_mode = I915_TILING_X;
228         unsigned long pitch, act_size;
229         uint32_t test;
230         int i;
231
232         if (faulting_reloc)
233                 igt_disable_prefault();
234
235         act_size = 2048;
236         dummy_bo = drm_intel_bo_alloc_tiled(bufmgr, "tiled dummy_bo", act_size, act_size,
237                                       4, &tiling_mode, &pitch, 0);
238
239         drm_intel_bo_subdata(dummy_bo, 0, act_size*act_size*4, blob);
240
241         create_special_bo();
242
243         for (i = 0; i < NUM_TARGET_BOS; i++) {
244                 pc_target_bo[i] = drm_intel_bo_alloc(bufmgr, "special batch", 4096, 4096);
245                 emit_dummy_load(pitch);
246                 igt_assert(pc_target_bo[i]->offset == 0);
247
248                 if (faulting_reloc)
249                         faulting_reloc_and_emit(fd, pc_target_bo[i]);
250                 else
251                         reloc_and_emit(fd, pc_target_bo[i]);
252         }
253
254         /* Only check at the end to avoid unnecessary synchronous behaviour. */
255         for (i = 0; i < NUM_TARGET_BOS; i++) {
256                 drm_intel_bo_get_subdata(pc_target_bo[i], 0, 4, &test);
257                 igt_assert_f(test == 0xdeadbeef,
258                              "mismatch in buffer %i: 0x%08x instead of 0xdeadbeef\n", i, test);
259                 drm_intel_bo_unreference(pc_target_bo[i]);
260         }
261
262         drm_intel_gem_bo_map_gtt(dummy_bo);
263         drm_intel_gem_bo_unmap_gtt(dummy_bo);
264
265         drm_intel_bo_unreference(special_bo);
266         drm_intel_bo_unreference(dummy_bo);
267
268         if (faulting_reloc)
269                 igt_enable_prefault();
270 }
271
272 #define INTERRUPT       (1 << 0)
273 #define FAULTING        (1 << 1)
274 #define THRASH          (1 << 2)
275 #define THRASH_INACTIVE (1 << 3)
276 #define ALL_FLAGS       (INTERRUPT | FAULTING | THRASH | THRASH_INACTIVE)
277 static void do_forked_test(int fd, unsigned flags)
278 {
279         int num_threads = sysconf(_SC_NPROCESSORS_ONLN);
280         struct igt_helper_process thrasher = {};
281
282         if (flags & (THRASH | THRASH_INACTIVE)) {
283                 uint64_t val = (flags & THRASH_INACTIVE) ?
284                                 (DROP_RETIRE | DROP_BOUND | DROP_UNBOUND) : DROP_ALL;
285
286                 igt_fork_helper(&thrasher) {
287                         while (1) {
288                                 usleep(1000);
289                                 igt_drop_caches_set(val);
290                         }
291                 }
292         }
293
294         igt_fork(i, num_threads * 4) {
295                 /* re-create process local data */
296                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
297                 batch = intel_batchbuffer_alloc(bufmgr, devid);
298
299                 if (flags & INTERRUPT)
300                         igt_fork_signal_helper();
301
302                 do_test(fd, flags & FAULTING);
303
304                 if (flags & INTERRUPT)
305                         igt_stop_signal_helper();
306         }
307
308         igt_waitchildren();
309         if (flags & (THRASH | THRASH_INACTIVE))
310                 igt_stop_helper(&thrasher);
311 }
312
313 int fd;
314
315 #define MAX_BLT_SIZE 128
316 igt_main
317 {
318         igt_skip_on_simulation();
319
320         memset(blob, 'A', sizeof(blob));
321
322         igt_fixture {
323                 fd = drm_open_any();
324
325                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
326                 /* disable reuse, otherwise the test fails */
327                 //drm_intel_bufmgr_gem_enable_reuse(bufmgr);
328                 devid = intel_get_drm_devid(fd);
329                 batch = intel_batchbuffer_alloc(bufmgr, devid);
330         }
331
332         igt_subtest("normal")
333                 do_test(fd, false);
334
335         igt_subtest("faulting-reloc")
336                 do_test(fd, true);
337
338         igt_fork_signal_helper();
339         igt_subtest("interruptible")
340                 do_test(fd, false);
341
342         igt_subtest("faulting-reloc-interruptible")
343                 do_test(fd, true);
344         igt_stop_signal_helper();
345
346         for (unsigned flags = 0; flags <= ALL_FLAGS; flags++) {
347                 if ((flags & THRASH) && (flags & THRASH_INACTIVE))
348                         continue;
349
350                 igt_subtest_f("forked%s%s%s%s",
351                               flags & INTERRUPT ? "-interruptible" : "",
352                               flags & FAULTING ? "-faulting-reloc" : "",
353                               flags & THRASH ? "-thrashing" : "",
354                               flags & THRASH_INACTIVE ? "-thrash-inactive" : "")
355                         do_forked_test(fd, flags);
356         }
357
358         igt_fixture {
359                 intel_batchbuffer_free(batch);
360                 drm_intel_bufmgr_destroy(bufmgr);
361
362                 close(fd);
363         }
364 }