lib/igt_kms: Unify pipe name helpers
[platform/upstream/intel-gpu-tools.git] / tests / gem_seqno_wrap.c
1 /*
2  * Copyright (c) 2012 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  *    Mika Kuoppala <mika.kuoppala@intel.com>
25  *
26  */
27
28 /*
29  * This test runs blitcopy -> rendercopy with multiple buffers over wrap
30  * boundary.
31  */
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <sys/types.h>
39 #include <sys/wait.h>
40 #include <limits.h>
41 #include <signal.h>
42 #include <errno.h>
43
44 #include "ioctl_wrappers.h"
45 #include "drmtest.h"
46 #include "igt_core.h"
47 #include "igt_aux.h"
48 #include "intel_bufmgr.h"
49 #include "intel_batchbuffer.h"
50 #include "intel_io.h"
51 #include "intel_chipset.h"
52
53 static int devid;
54 static int card_index = 0;
55 static uint32_t last_seqno = 0;
56
57 static struct intel_batchbuffer *batch_blt;
58 static struct intel_batchbuffer *batch_3d;
59
60 struct option_struct {
61         int rounds;
62         int background;
63         int timeout;
64         int dontwrap;
65         int prewrap_space;
66         int random;
67         int buffers;
68 };
69
70 static struct option_struct options;
71
72 static void init_buffer(drm_intel_bufmgr *bufmgr,
73                         struct igt_buf *buf,
74                         drm_intel_bo *bo,
75                         int width, int height)
76 {
77         /* buf->bo = drm_intel_bo_alloc(bufmgr, "", size, 4096); */
78         buf->bo = bo;
79         buf->size = width * height * 4;
80         igt_assert(buf->bo);
81         buf->tiling = I915_TILING_NONE;
82         buf->num_tiles = width * height * 4;
83         buf->stride = width * 4;
84 }
85
86 static void
87 set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
88 {
89         int size = width * height;
90         uint32_t *vaddr;
91
92         drm_intel_gem_bo_start_gtt_access(bo, true);
93         vaddr = bo->virtual;
94         while (size--)
95                 *vaddr++ = val;
96 }
97
98 static void
99 cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
100 {
101         int size = width * height;
102         uint32_t *vaddr;
103
104         drm_intel_gem_bo_start_gtt_access(bo, false);
105         vaddr = bo->virtual;
106         while (size--) {
107                 igt_assert_f(*vaddr++ == val,
108                              "%d: 0x%x differs from assumed 0x%x\n"
109                              "seqno_before_test 0x%x, "
110                              " approximated seqno on test fail 0x%x\n",
111                              width * height - size, *vaddr-1, val,
112                              last_seqno, last_seqno + val * 2);
113         }
114 }
115
116 static drm_intel_bo *
117 create_bo(drm_intel_bufmgr *bufmgr, uint32_t val, int width, int height)
118 {
119         drm_intel_bo *bo;
120
121         bo = drm_intel_bo_alloc(bufmgr, "bo", width * height * 4, 0);
122         igt_assert(bo);
123
124         /* gtt map doesn't have a write parameter, so just keep the mapping
125          * around (to avoid the set_domain with the gtt write domain set) and
126          * manually tell the kernel when we start access the gtt. */
127         drm_intel_gem_bo_map_gtt(bo);
128
129         set_bo(bo, val, width, height);
130
131         return bo;
132 }
133
134 static void release_bo(drm_intel_bo *bo)
135 {
136         drm_intel_gem_bo_unmap_gtt(bo);
137         drm_intel_bo_unreference(bo);
138 }
139
140 static void render_copyfunc(struct igt_buf *src,
141                             struct igt_buf *dst,
142                             int width,
143                             int height)
144 {
145         const int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0;
146         igt_render_copyfunc_t rendercopy = igt_get_render_copyfunc(devid);
147         static int warned = 0;
148
149         if (rendercopy) {
150                 rendercopy(batch_3d, NULL,
151                            src, src_x, src_y,
152                            width, height,
153                            dst, dst_x, dst_y);
154                 intel_batchbuffer_flush(batch_3d);
155         } else {
156                 if (!warned) {
157                         igt_info("No render copy found for this gen, ""test is shallow!\n");
158                         warned = 1;
159                 }
160                 igt_assert(dst->bo);
161                 igt_assert(src->bo);
162                 intel_copy_bo(batch_blt, dst->bo, src->bo, width*height*4);
163                 intel_batchbuffer_flush(batch_blt);
164         }
165 }
166
167 static void exchange_uint(void *array, unsigned i, unsigned j)
168 {
169         unsigned *i_arr = array;
170         unsigned i_tmp;
171
172         i_tmp = i_arr[i];
173         i_arr[i] = i_arr[j];
174         i_arr[j] = i_tmp;
175 }
176
177 static void run_sync_test(int num_buffers, bool verify)
178 {
179         drm_intel_bufmgr *bufmgr;
180         int max;
181         drm_intel_bo **src, **dst1, **dst2;
182         int width = 128, height = 128;
183         int fd;
184         int i;
185         unsigned int *p_dst1, *p_dst2;
186         struct igt_buf *s_src, *s_dst;
187
188         fd = drm_open_any();
189         igt_assert(fd >= 0);
190
191         gem_quiescent_gpu(fd);
192
193         devid = intel_get_drm_devid(fd);
194
195         max = gem_aperture_size (fd) / (1024 * 1024) / 2;
196         if (num_buffers > max)
197                 num_buffers = max;
198
199         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
200         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
201         batch_blt = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
202         igt_assert(batch_blt);
203         batch_3d = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
204         igt_assert(batch_3d);
205
206         src = malloc(num_buffers * sizeof(*src));
207         igt_assert(src);
208
209         dst1 = malloc(num_buffers * sizeof(*dst1));
210         igt_assert(dst1);
211
212         dst2 = malloc(num_buffers * sizeof(*dst2));
213         igt_assert(dst2);
214
215         s_src = malloc(num_buffers * sizeof(*s_src));
216         igt_assert(s_src);
217
218         s_dst = malloc(num_buffers * sizeof(*s_dst));
219         igt_assert(s_dst);
220
221         p_dst1 = malloc(num_buffers * sizeof(unsigned int));
222         igt_assert(p_dst1);
223
224         p_dst2 = malloc(num_buffers * sizeof(unsigned int));
225         igt_assert(p_dst2);
226
227         for (i = 0; i < num_buffers; i++) {
228                 p_dst1[i] = p_dst2[i] = i;
229                 src[i] = create_bo(bufmgr, i, width, height);
230                 igt_assert(src[i]);
231                 dst1[i] = create_bo(bufmgr, ~i, width, height);
232                 igt_assert(dst1[i]);
233                 dst2[i] = create_bo(bufmgr, ~i, width, height);
234                 igt_assert(dst2[i]);
235                 init_buffer(bufmgr, &s_src[i], src[i], width, height);
236                 init_buffer(bufmgr, &s_dst[i], dst1[i], width, height);
237         }
238
239         igt_permute_array(p_dst1, num_buffers, exchange_uint);
240         igt_permute_array(p_dst2, num_buffers, exchange_uint);
241
242         for (i = 0; i < num_buffers; i++)
243                 render_copyfunc(&s_src[i], &s_dst[p_dst1[i]], width, height);
244
245         /* Only sync between buffers if this is actual test run and
246          * not a seqno filler */
247         if (verify) {
248                 for (i = 0; i < num_buffers; i++)
249                         intel_copy_bo(batch_blt, dst2[p_dst2[i]], dst1[p_dst1[i]],
250                                       width*height*4);
251
252                 for (i = 0; i < num_buffers; i++) {
253                         cmp_bo(dst2[p_dst2[i]], i, width, height);
254                 }
255         }
256
257         for (i = 0; i < num_buffers; i++) {
258                 release_bo(src[i]);
259                 release_bo(dst1[i]);
260                 release_bo(dst2[i]);
261         }
262
263         intel_batchbuffer_free(batch_3d);
264         intel_batchbuffer_free(batch_blt);
265         drm_intel_bufmgr_destroy(bufmgr);
266
267         free(p_dst1);
268         free(p_dst2);
269         free(s_dst);
270         free(s_src);
271         free(dst2);
272         free(dst1);
273         free(src);
274
275         gem_quiescent_gpu(fd);
276
277         close(fd);
278 }
279
280 static const char *dfs_base = "/sys/kernel/debug/dri";
281 static const char *dfs_entry = "i915_next_seqno";
282
283 static int dfs_open(int mode)
284 {
285         char fname[FILENAME_MAX];
286         int fh;
287
288         snprintf(fname, FILENAME_MAX, "%s/%i/%s",
289                  dfs_base, card_index, dfs_entry);
290
291         fh = open(fname, mode);
292         igt_require(fh >= 0);
293
294         return fh;
295 }
296
297 static int __read_seqno(uint32_t *seqno)
298 {
299         int fh;
300         char buf[32];
301         int r;
302         char *p;
303         unsigned long int tmp;
304
305         fh = dfs_open(O_RDONLY);
306
307         r = read(fh, buf, sizeof(buf) - 1);
308         close(fh);
309         if (r < 0) {
310                 igt_warn("read");
311                 return -errno;
312         }
313
314         buf[r] = 0;
315
316         p = strstr(buf, "0x");
317         if (!p)
318                 p = buf;
319
320         errno = 0;
321         tmp = strtoul(p, NULL, 0);
322         if (tmp == ULONG_MAX && errno) {
323                 igt_warn("strtoul");
324                 return -errno;
325         }
326
327         *seqno = tmp;
328
329         igt_debug("next_seqno: 0x%x\n", *seqno);
330
331         return 0;
332 }
333
334 static int read_seqno(void)
335 {
336         uint32_t seqno = 0;
337         int r;
338         int wrap = 0;
339
340         r = __read_seqno(&seqno);
341         igt_assert(r == 0);
342
343         if (last_seqno > seqno)
344                 wrap++;
345
346         last_seqno = seqno;
347
348         return wrap;
349 }
350
351 static int write_seqno(uint32_t seqno)
352 {
353         int fh;
354         char buf[32];
355         int r;
356         uint32_t rb = -1;
357
358         if (options.dontwrap)
359                 return 0;
360
361         fh = dfs_open(O_RDWR);
362         igt_assert(snprintf(buf, sizeof(buf), "0x%x", seqno) > 0);
363
364         r = write(fh, buf, strnlen(buf, sizeof(buf)));
365         close(fh);
366         if (r < 0)
367                 return r;
368
369         igt_assert(r == strnlen(buf, sizeof(buf)));
370
371         last_seqno = seqno;
372
373         igt_debug("next_seqno set to: 0x%x\n", seqno);
374
375         r = __read_seqno(&rb);
376         if (r < 0)
377                 return r;
378
379         if (rb != seqno) {
380                 igt_info("seqno readback differs rb:0x%x vs w:0x%x\n", rb, seqno);
381                 return -1;
382         }
383
384         return 0;
385 }
386
387 static uint32_t calc_prewrap_val(void)
388 {
389         const int pval = options.prewrap_space;
390
391         if (options.random == 0)
392                 return pval;
393
394         if (pval == 0)
395                 return 0;
396
397         return (random() % pval);
398 }
399
400 static void run_test(void)
401 {
402         run_sync_test(options.buffers, true);
403 }
404
405 static void preset_run_once(void)
406 {
407         igt_assert(write_seqno(1) == 0);
408         run_test();
409
410         igt_assert(write_seqno(0x7fffffff) == 0);
411         run_test();
412
413         igt_assert(write_seqno(0xffffffff) == 0);
414         run_test();
415
416         igt_assert(write_seqno(0xfffffff0) == 0);
417         run_test();
418 }
419
420 static void random_run_once(void)
421 {
422         uint32_t val;
423
424         do {
425                 val = random() % UINT32_MAX;
426                 if (RAND_MAX < UINT32_MAX)
427                         val += random();
428         } while (val == 0);
429
430         igt_assert(write_seqno(val) == 0);
431         run_test();
432 }
433
434 static void wrap_run_once(void)
435 {
436         const uint32_t pw_val = calc_prewrap_val();
437
438         igt_assert(write_seqno(UINT32_MAX - pw_val) == 0);
439
440         while(!read_seqno())
441                 run_test();
442 }
443
444 static void background_run_once(void)
445 {
446         const uint32_t pw_val = calc_prewrap_val();
447
448         igt_assert(write_seqno(UINT32_MAX - pw_val) == 0);
449
450         while(!read_seqno())
451                 sleep(3);
452 }
453
454 static int parse_options(int opt, int opt_index)
455 {
456         switch(opt) {
457                 case 'b':
458                         options.background = 1;
459                         igt_info("running in background inducing wraps\n");
460                         break;
461                 case 'd':
462                         options.dontwrap = 1;
463                         igt_info("won't wrap after testruns\n");
464                         break;
465                 case 'n':
466                         options.rounds = atoi(optarg);
467                         igt_info("running %d rounds\n", options.rounds);
468                         break;
469                 case 'i':
470                         options.buffers = atoi(optarg);
471                         igt_info("buffers %d\n", options.buffers);
472                         break;
473                 case 't':
474                         options.timeout = atoi(optarg);
475                         if (options.timeout == 0)
476                                 options.timeout = 10;
477                         igt_info("setting timeout to %d seconds\n", options.timeout);
478                         break;
479                 case 'r':
480                         options.random = 0;
481                         break;
482                 case 'p':
483                         options.prewrap_space = atoi(optarg);
484                         igt_info("prewrap set to %d (0x%x)\n", options.prewrap_space, UINT32_MAX - options.prewrap_space);
485                         break;
486         }
487
488         return 0;
489 }
490
491 int main(int argc, char **argv)
492 {
493         int wcount = 0;
494         int r = -1;
495
496         static struct option long_options[] = {
497                 {"rounds", required_argument, 0, 'n'},
498                 {"background", no_argument, 0, 'b'},
499                 {"timeout", required_argument, 0, 't'},
500                 {"dontwrap", no_argument, 0, 'd'},
501                 {"prewrap", required_argument, 0, 'p'},
502                 {"norandom", no_argument, 0, 'r'},
503                 {"buffers", required_argument, 0, 'i'},
504                 { 0, 0, 0, 0 }
505         };
506
507         const char *help =
508                 "  -b --background       run in background inducing wraps\n"
509                 "  -n --rounds=num       run num times across wrap boundary, 0 == forever\n"
510                 "  -t --timeout=sec      set timeout to wait for testrun to sec seconds\n"
511                 "  -d --dontwrap         don't wrap just run the test\n"
512                 "  -p --prewrap=n        set seqno to WRAP - n for each testrun\n"
513                 "  -r --norandom         dont randomize prewrap space\n"
514                 "  -i --buffers          number of buffers to copy\n";
515
516         options.rounds = SLOW_QUICK(50, 2);
517         options.background = 0;
518         options.dontwrap = 0;
519         options.timeout = 20;
520         options.random = 1;
521         options.prewrap_space = 21;
522         options.buffers = 10;
523
524         igt_simple_init_parse_opts(argc, argv, "n:bvt:dp:ri:", long_options,
525                                    help, parse_options);
526
527         card_index = drm_get_card();
528
529         srandom(time(NULL));
530
531         while(options.rounds == 0 || wcount < options.rounds) {
532                 if (options.background) {
533                         background_run_once();
534                 } else {
535                         preset_run_once();
536                         random_run_once();
537                         wrap_run_once();
538                 }
539
540                 wcount++;
541
542                 igt_debug("%s done: %d\n",
543                           options.dontwrap ? "tests" : "wraps", wcount);
544         }
545
546         if (options.rounds == wcount) {
547                 igt_debug("done %d wraps successfully\n", wcount);
548                 return 0;
549         }
550
551         return r;
552 }