kms_rotation_crc: Remove useless comments
[platform/upstream/intel-gpu-tools.git] / tests / gem_concurrent_blit.c
1 /*
2  * Copyright © 2009,2012,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  *    Eric Anholt <eric@anholt.net>
25  *    Chris Wilson <chris@chris-wilson.co.uk>
26  *    Daniel Vetter <daniel.vetter@ffwll.ch>
27  *
28  */
29
30 /** @file gem_concurrent_blit.c
31  *
32  * This is a test of pread/pwrite behavior when writing to active
33  * buffers.
34  *
35  * Based on gem_gtt_concurrent_blt.
36  */
37
38 #include <stdlib.h>
39 #include <stdio.h>
40 #include <string.h>
41 #include <fcntl.h>
42 #include <inttypes.h>
43 #include <errno.h>
44 #include <sys/stat.h>
45 #include <sys/time.h>
46 #include <sys/wait.h>
47
48 #include <drm.h>
49
50 #include "ioctl_wrappers.h"
51 #include "drmtest.h"
52 #include "intel_bufmgr.h"
53 #include "intel_batchbuffer.h"
54 #include "intel_io.h"
55 #include "intel_chipset.h"
56 #include "igt_aux.h"
57
58 static void
59 prw_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
60 {
61         int size = width * height, i;
62         uint32_t *tmp;
63
64         tmp = malloc(4*size);
65         if (tmp) {
66                 for (i = 0; i < size; i++)
67                         tmp[i] = val;
68                 drm_intel_bo_subdata(bo, 0, 4*size, tmp);
69                 free(tmp);
70         } else {
71                 for (i = 0; i < size; i++)
72                         drm_intel_bo_subdata(bo, 4*i, 4, &val);
73         }
74 }
75
76 static void
77 prw_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
78 {
79         int size = width * height, i;
80         uint32_t *tmp;
81
82         tmp = malloc(4*size);
83         if (tmp) {
84                 memset(tmp, 0, 4*size);
85                 do_or_die(drm_intel_bo_get_subdata(bo, 0, 4*size, tmp));
86                 for (i = 0; i < size; i++)
87                         igt_assert(tmp[i] == val);
88                 free(tmp);
89         } else {
90                 uint32_t t;
91                 for (i = 0; i < size; i++) {
92                         t = 0;
93                         do_or_die(drm_intel_bo_get_subdata(bo, 4*i, 4, &t));
94                         igt_assert(t == val);
95                 }
96         }
97 }
98
99 static drm_intel_bo *
100 unmapped_create_bo(drm_intel_bufmgr *bufmgr, uint32_t val, int width, int height)
101 {
102         drm_intel_bo *bo;
103
104         bo = drm_intel_bo_alloc(bufmgr, "bo", 4*width*height, 0);
105         igt_assert(bo);
106
107         return bo;
108 }
109
110 static void
111 gtt_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
112 {
113         int size = width * height;
114         uint32_t *vaddr;
115
116         drm_intel_gem_bo_start_gtt_access(bo, true);
117         vaddr = bo->virtual;
118         while (size--)
119                 *vaddr++ = val;
120 }
121
122 static void
123 gtt_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
124 {
125         int size = width * height;
126         uint32_t *vaddr;
127
128         drm_intel_gem_bo_start_gtt_access(bo, false);
129         vaddr = bo->virtual;
130         while (size--)
131                 igt_assert(*vaddr++ == val);
132 }
133
134 static drm_intel_bo *
135 gtt_create_bo(drm_intel_bufmgr *bufmgr, uint32_t val, int width, int height)
136 {
137         drm_intel_bo *bo;
138
139         bo = drm_intel_bo_alloc(bufmgr, "bo", 4*width*height, 0);
140         igt_assert(bo);
141
142         /* gtt map doesn't have a write parameter, so just keep the mapping
143          * around (to avoid the set_domain with the gtt write domain set) and
144          * manually tell the kernel when we start access the gtt. */
145         do_or_die(drm_intel_gem_bo_map_gtt(bo));
146
147         return bo;
148 }
149
150 static void
151 cpu_set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
152 {
153         int size = width * height;
154         uint32_t *vaddr;
155
156         do_or_die(drm_intel_bo_map(bo, true));
157         vaddr = bo->virtual;
158         while (size--)
159                 *vaddr++ = val;
160         drm_intel_bo_unmap(bo);
161 }
162
163 static void
164 cpu_cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
165 {
166         int size = width * height;
167         uint32_t *vaddr;
168
169         do_or_die(drm_intel_bo_map(bo, false));
170         vaddr = bo->virtual;
171         while (size--)
172                 igt_assert(*vaddr++ == val);
173         drm_intel_bo_unmap(bo);
174 }
175
176 struct access_mode {
177         void (*set_bo)(drm_intel_bo *bo, uint32_t val, int w, int h);
178         void (*cmp_bo)(drm_intel_bo *bo, uint32_t val, int w, int h);
179         drm_intel_bo *(*create_bo)(drm_intel_bufmgr *bufmgr,
180                                    uint32_t val, int width, int height);
181         const char *name;
182 };
183
184 struct access_mode access_modes[] = {
185         { .set_bo = prw_set_bo, .cmp_bo = prw_cmp_bo,
186                 .create_bo = unmapped_create_bo, .name = "prw" },
187         { .set_bo = cpu_set_bo, .cmp_bo = cpu_cmp_bo,
188                 .create_bo = unmapped_create_bo, .name = "cpu" },
189         { .set_bo = gtt_set_bo, .cmp_bo = gtt_cmp_bo,
190                 .create_bo = gtt_create_bo, .name = "gtt" },
191 };
192
193 #define MAX_NUM_BUFFERS 1024
194 int num_buffers = MAX_NUM_BUFFERS, fd;
195 drm_intel_bufmgr *bufmgr;
196 struct intel_batchbuffer *batch;
197 int width = 512, height = 512;
198
199 static void do_overwrite_source(struct access_mode *mode,
200                                 drm_intel_bo **src, drm_intel_bo **dst,
201                                 drm_intel_bo *dummy)
202 {
203         int i;
204
205         gem_quiescent_gpu(fd);
206         for (i = 0; i < num_buffers; i++) {
207                 mode->set_bo(src[i], i, width, height);
208                 mode->set_bo(dst[i], i, width, height);
209         }
210         for (i = 0; i < num_buffers; i++)
211                 intel_copy_bo(batch, dst[i], src[i], width*height*4);
212         for (i = num_buffers; i--; )
213                 mode->set_bo(src[i], 0xdeadbeef, width, height);
214         for (i = 0; i < num_buffers; i++)
215                 mode->cmp_bo(dst[i], i, width, height);
216 }
217
218 static void do_early_read(struct access_mode *mode,
219                           drm_intel_bo **src, drm_intel_bo **dst,
220                           drm_intel_bo *dummy)
221 {
222         int i;
223
224         gem_quiescent_gpu(fd);
225         for (i = num_buffers; i--; )
226                 mode->set_bo(src[i], 0xdeadbeef, width, height);
227         for (i = 0; i < num_buffers; i++)
228                 intel_copy_bo(batch, dst[i], src[i], width*height*4);
229         for (i = num_buffers; i--; )
230                 mode->cmp_bo(dst[i], 0xdeadbeef, width, height);
231 }
232
233 static void do_gpu_read_after_write(struct access_mode *mode,
234                                     drm_intel_bo **src, drm_intel_bo **dst,
235                                     drm_intel_bo *dummy)
236 {
237         int i;
238
239         gem_quiescent_gpu(fd);
240         for (i = num_buffers; i--; )
241                 mode->set_bo(src[i], 0xabcdabcd, width, height);
242         for (i = 0; i < num_buffers; i++)
243                 intel_copy_bo(batch, dst[i], src[i], width*height*4);
244         for (i = num_buffers; i--; )
245                 intel_copy_bo(batch, dummy, dst[i], width*height*4);
246         for (i = num_buffers; i--; )
247                 mode->cmp_bo(dst[i], 0xabcdabcd, width, height);
248 }
249
250 typedef void (*do_test)(struct access_mode *mode,
251                         drm_intel_bo **src, drm_intel_bo **dst,
252                         drm_intel_bo *dummy);
253
254 typedef void (*run_wrap)(struct access_mode *mode,
255                          drm_intel_bo **src, drm_intel_bo **dst,
256                          drm_intel_bo *dummy,
257                          do_test do_test_func);
258
259 static void run_single(struct access_mode *mode,
260                        drm_intel_bo **src, drm_intel_bo **dst,
261                        drm_intel_bo *dummy,
262                        do_test do_test_func)
263 {
264         do_test_func(mode, src, dst, dummy);
265 }
266
267
268 static void run_interruptible(struct access_mode *mode,
269                               drm_intel_bo **src, drm_intel_bo **dst,
270                               drm_intel_bo *dummy,
271                               do_test do_test_func)
272 {
273         int loop;
274
275         igt_fork_signal_helper();
276
277         for (loop = 0; loop < 10; loop++)
278                 do_test_func(mode, src, dst, dummy);
279
280         igt_stop_signal_helper();
281 }
282
283 static void run_forked(struct access_mode *mode,
284                        drm_intel_bo **src, drm_intel_bo **dst,
285                        drm_intel_bo *dummy,
286                        do_test do_test_func)
287 {
288         const int old_num_buffers = num_buffers;
289
290         num_buffers /= 16;
291         num_buffers += 2;
292
293         igt_fork_signal_helper();
294
295         igt_fork(child, 16) {
296                 /* recreate process local variables */
297                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
298                 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
299                 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
300                 for (int i = 0; i < num_buffers; i++) {
301                         src[i] = mode->create_bo(bufmgr, i, width, height);
302                         dst[i] = mode->create_bo(bufmgr, ~i, width, height);
303                 }
304                 dummy = mode->create_bo(bufmgr, 0, width, height);
305                 for (int loop = 0; loop < 10; loop++)
306                         do_test_func(mode, src, dst, dummy);
307                 /* as we borrow the fd, we need to reap our bo */
308                 for (int i = 0; i < num_buffers; i++) {
309                         drm_intel_bo_unreference(src[i]);
310                         drm_intel_bo_unreference(dst[i]);
311                 }
312                 drm_intel_bo_unreference(dummy);
313                 intel_batchbuffer_free(batch);
314                 drm_intel_bufmgr_destroy(bufmgr);
315         }
316
317         igt_waitchildren();
318
319         igt_stop_signal_helper();
320
321         num_buffers = old_num_buffers;
322 }
323
324 static void
325 run_basic_modes(struct access_mode *mode,
326                 drm_intel_bo **src, drm_intel_bo **dst,
327                 drm_intel_bo *dummy, const char *suffix,
328                 run_wrap run_wrap_func)
329 {
330         /* try to overwrite the source values */
331         igt_subtest_f("%s-overwrite-source%s", mode->name, suffix)
332                 run_wrap_func(mode, src, dst, dummy, do_overwrite_source);
333
334         /* try to read the results before the copy completes */
335         igt_subtest_f("%s-early-read%s", mode->name, suffix)
336                 run_wrap_func(mode, src, dst, dummy, do_early_read);
337
338         /* and finally try to trick the kernel into loosing the pending write */
339         igt_subtest_f("%s-gpu-read-after-write%s", mode->name, suffix)
340                 run_wrap_func(mode, src, dst, dummy, do_gpu_read_after_write);
341 }
342
343 static void
344 run_modes(struct access_mode *mode)
345 {
346         drm_intel_bo *src[MAX_NUM_BUFFERS], *dst[MAX_NUM_BUFFERS], *dummy = NULL;
347
348         igt_fixture {
349                 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
350                 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
351                 batch = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
352
353                 for (int i = 0; i < num_buffers; i++) {
354                         src[i] = mode->create_bo(bufmgr, i, width, height);
355                         dst[i] = mode->create_bo(bufmgr, ~i, width, height);
356                 }
357                 dummy = mode->create_bo(bufmgr, 0, width, height);
358         }
359
360         run_basic_modes(mode, src, dst, dummy, "", run_single);
361         run_basic_modes(mode, src, dst, dummy, "-interruptible", run_interruptible);
362
363         igt_fixture {
364                 for (int i = 0; i < num_buffers; i++) {
365                         drm_intel_bo_unreference(src[i]);
366                         drm_intel_bo_unreference(dst[i]);
367                 }
368                 drm_intel_bo_unreference(dummy);
369                 intel_batchbuffer_free(batch);
370                 drm_intel_bufmgr_destroy(bufmgr);
371         }
372
373         run_basic_modes(mode, src, dst, dummy, "-forked", run_forked);
374 }
375
376 igt_main
377 {
378         int max, i;
379
380         igt_skip_on_simulation();
381
382         igt_fixture {
383                 fd = drm_open_any();
384
385                 max = gem_aperture_size (fd) / (1024 * 1024) / 2;
386                 if (num_buffers > max)
387                         num_buffers = max;
388
389                 max = intel_get_total_ram_mb() * 3 / 4;
390                 if (num_buffers > max)
391                         num_buffers = max;
392                 num_buffers /= 2;
393                 igt_info("using 2x%d buffers, each 1MiB\n", num_buffers);
394         }
395
396         for (i = 0; i < ARRAY_SIZE(access_modes); i++)
397                 run_modes(&access_modes[i]);
398 }