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