lib: switch intel_copy_bo to directly take a size
[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 <wordexp.h>
42 #include <signal.h>
43
44 #include "i915_drm.h"
45 #include "intel_bufmgr.h"
46 #include "intel_batchbuffer.h"
47 #include "intel_gpu_tools.h"
48 #include "rendercopy.h"
49
50 static int devid;
51 static int card_index = 0;
52 static uint32_t last_seqno = 0;
53
54 static struct intel_batchbuffer *batch_blt;
55 static struct intel_batchbuffer *batch_3d;
56
57 struct option_struct {
58         int rounds;
59         int background;
60         char cmd[1024];
61         int timeout;
62         int dontwrap;
63         int prewrap_space;
64         int random;
65         int buffers;
66 };
67
68 static struct option_struct options;
69
70 static void init_buffer(drm_intel_bufmgr *bufmgr,
71                         struct scratch_buf *buf,
72                         drm_intel_bo *bo,
73                         int width, int height)
74 {
75         /* buf->bo = drm_intel_bo_alloc(bufmgr, "", size, 4096); */
76         buf->bo = bo;
77         buf->size = width * height * 4;
78         igt_assert(buf->bo);
79         buf->tiling = I915_TILING_NONE;
80         buf->data = buf->cpu_mapping = NULL;
81         buf->num_tiles = width * height * 4;
82         buf->stride = width * 4;
83 }
84
85 static void
86 set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
87 {
88         int size = width * height;
89         uint32_t *vaddr;
90
91         drm_intel_gem_bo_start_gtt_access(bo, true);
92         vaddr = bo->virtual;
93         while (size--)
94                 *vaddr++ = val;
95 }
96
97 static int
98 cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
99 {
100         int size = width * height;
101         uint32_t *vaddr;
102
103         drm_intel_gem_bo_start_gtt_access(bo, false);
104         vaddr = bo->virtual;
105         while (size--) {
106                 if (*vaddr++ != val) {
107                         printf("%d: 0x%x differs from assumed 0x%x\n",
108                                width * height - size, *vaddr-1, val);
109                         return -1;
110                 }
111         }
112
113         return 0;
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 scratch_buf *src,
141                             struct scratch_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         render_copyfunc_t rendercopy = 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                         printf("No render copy found for this gen, "
158                                "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 int 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         int r = -1;
187         int failed = 0;
188         unsigned int *p_dst1, *p_dst2;
189         struct scratch_buf *s_src, *s_dst;
190
191         fd = drm_open_any();
192         igt_assert(fd >= 0);
193
194         gem_quiescent_gpu(fd);
195
196         devid = intel_get_drm_devid(fd);
197
198         max = gem_aperture_size (fd) / (1024 * 1024) / 2;
199         if (num_buffers > max)
200                 num_buffers = max;
201
202         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
203         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
204         batch_blt = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
205         igt_assert(batch_blt);
206         batch_3d = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
207         igt_assert(batch_3d);
208
209         src = malloc(num_buffers * sizeof(*src));
210         igt_assert(src);
211
212         dst1 = malloc(num_buffers * sizeof(*dst1));
213         igt_assert(dst1);
214
215         dst2 = malloc(num_buffers * sizeof(*dst2));
216         igt_assert(dst2);
217
218         s_src = malloc(num_buffers * sizeof(*s_src));
219         igt_assert(s_src);
220
221         s_dst = malloc(num_buffers * sizeof(*s_dst));
222         igt_assert(s_dst);
223
224         p_dst1 = malloc(num_buffers * sizeof(unsigned int));
225         if (p_dst1 == NULL)
226                 return -ENOMEM;
227
228         p_dst2 = malloc(num_buffers * sizeof(unsigned int));
229         if (p_dst2 == NULL)
230                 return -ENOMEM;
231
232         for (i = 0; i < num_buffers; i++) {
233                 p_dst1[i] = p_dst2[i] = i;
234                 src[i] = create_bo(bufmgr, i, width, height);
235                 igt_assert(src[i]);
236                 dst1[i] = create_bo(bufmgr, ~i, width, height);
237                 igt_assert(dst1[i]);
238                 dst2[i] = create_bo(bufmgr, ~i, width, height);
239                 igt_assert(dst2[i]);
240                 init_buffer(bufmgr, &s_src[i], src[i], width, height);
241                 init_buffer(bufmgr, &s_dst[i], dst1[i], width, height);
242         }
243
244         igt_permute_array(p_dst1, num_buffers, exchange_uint);
245         igt_permute_array(p_dst2, num_buffers, exchange_uint);
246
247         for (i = 0; i < num_buffers; i++)
248                 render_copyfunc(&s_src[i], &s_dst[p_dst1[i]], width, height);
249
250         /* Only sync between buffers if this is actual test run and
251          * not a seqno filler */
252         if (verify) {
253                 for (i = 0; i < num_buffers; i++)
254                         intel_copy_bo(batch_blt, dst2[p_dst2[i]], dst1[p_dst1[i]],
255                                       width*height*4);
256
257                 for (i = 0; i < num_buffers; i++) {
258                         r = cmp_bo(dst2[p_dst2[i]], i, width, height);
259                         if (r) {
260                                 printf("buffer %d differs, seqno_before_test 0x%x, "
261                                        " approximated seqno on test fail 0x%x\n",
262                                        i, last_seqno, last_seqno + i * 2);
263                                 failed = -1;
264                         }
265                 }
266         }
267
268         for (i = 0; i < num_buffers; i++) {
269                 release_bo(src[i]);
270                 release_bo(dst1[i]);
271                 release_bo(dst2[i]);
272         }
273
274         intel_batchbuffer_free(batch_3d);
275         intel_batchbuffer_free(batch_blt);
276         drm_intel_bufmgr_destroy(bufmgr);
277
278         free(p_dst1);
279         free(p_dst2);
280         free(s_dst);
281         free(s_src);
282         free(dst2);
283         free(dst1);
284         free(src);
285
286         gem_quiescent_gpu(fd);
287
288         close(fd);
289
290         return failed;
291 }
292
293 static int run_cmd(char *s)
294 {
295         int pid;
296         int r = -1;
297         int status = 0;
298         wordexp_t wexp;
299         int i;
300         r = wordexp(s, &wexp, 0);
301         if (r != 0) {
302                 printf("can't parse %s\n", s);
303                 return r;
304         }
305
306         for(i = 0; i < wexp.we_wordc; i++)
307                 printf("argv[%d] = %s\n", i, wexp.we_wordv[i]);
308
309         pid = fork();
310
311         if (pid == 0) {
312                 char path[PATH_MAX];
313                 char full_path[PATH_MAX];
314
315                 if (getcwd(path, PATH_MAX) == NULL)
316                         perror("getcwd");
317
318                 igt_assert(snprintf(full_path, PATH_MAX, "%s/%s", path, wexp.we_wordv[0]) > 0);
319
320                 r = execv(full_path, wexp.we_wordv);
321                 if (r == -1)
322                         perror("execv failed");
323         } else {
324                 int waitcount = options.timeout;
325
326                 while(waitcount-- > 0) {
327                         r = waitpid(pid, &status, WNOHANG);
328                         if (r == pid) {
329                                 if(WIFEXITED(status)) {
330                                         if (WEXITSTATUS(status))
331                                                 fprintf(stderr,
332                                                     "child returned with %d\n",
333                                                         WEXITSTATUS(status));
334                                         return WEXITSTATUS(status);
335                                 }
336                         } else if (r != 0) {
337                                 perror("waitpid");
338                                 return -errno;
339                         }
340
341                         sleep(3);
342                 }
343
344                 kill(pid, SIGKILL);
345                 return -ETIMEDOUT;
346         }
347
348         return r;
349 }
350
351 static const char *dfs_base = "/sys/kernel/debug/dri";
352 static const char *dfs_entry = "i915_next_seqno";
353
354 static int dfs_open(int mode)
355 {
356         char fname[FILENAME_MAX];
357         int fh;
358
359         snprintf(fname, FILENAME_MAX, "%s/%i/%s",
360                  dfs_base, card_index, dfs_entry);
361
362         fh = open(fname, mode);
363         igt_require(fh >= 0);
364
365         return fh;
366 }
367
368 static int __read_seqno(uint32_t *seqno)
369 {
370         int fh;
371         char buf[32];
372         int r;
373         char *p;
374         unsigned long int tmp;
375
376         fh = dfs_open(O_RDONLY);
377
378         r = read(fh, buf, sizeof(buf) - 1);
379         close(fh);
380         if (r < 0) {
381                 perror("read");
382                 return -errno;
383         }
384
385         buf[r] = 0;
386
387         p = strstr(buf, "0x");
388         if (!p)
389                 p = buf;
390
391         errno = 0;
392         tmp = strtoul(p, NULL, 0);
393         if (tmp == ULONG_MAX && errno) {
394                 perror("strtoul");
395                 return -errno;
396         }
397
398         *seqno = tmp;
399
400         igt_debug("next_seqno: 0x%x\n", *seqno);
401
402         return 0;
403 }
404
405 static int read_seqno(void)
406 {
407         uint32_t seqno = 0;
408         int r;
409         int wrap = 0;
410
411         r = __read_seqno(&seqno);
412         igt_assert(r == 0);
413
414         if (last_seqno > seqno)
415                 wrap++;
416
417         last_seqno = seqno;
418
419         return wrap;
420 }
421
422 static int write_seqno(uint32_t seqno)
423 {
424         int fh;
425         char buf[32];
426         int r;
427         uint32_t rb;
428
429         if (options.dontwrap)
430                 return 0;
431
432         fh = dfs_open(O_RDWR);
433         igt_assert(snprintf(buf, sizeof(buf), "0x%x", seqno) > 0);
434
435         r = write(fh, buf, strnlen(buf, sizeof(buf)));
436         close(fh);
437         if (r < 0)
438                 return r;
439
440         igt_assert(r == strnlen(buf, sizeof(buf)));
441
442         last_seqno = seqno;
443
444         igt_debug("next_seqno set to: 0x%x\n", seqno);
445
446         r = __read_seqno(&rb);
447         if (r < 0)
448                 return r;
449
450         if (rb != seqno) {
451                 printf("seqno readback differs rb:0x%x vs w:0x%x\n", rb, seqno);
452                 return -1;
453         }
454
455         return 0;
456 }
457
458 static uint32_t calc_prewrap_val(void)
459 {
460         const int pval = options.prewrap_space;
461
462         if (options.random == 0)
463                 return pval;
464
465         if (pval == 0)
466                 return 0;
467
468         return (random() % pval);
469 }
470
471 static int run_test(void)
472 {
473         int r;
474
475         if (strnlen(options.cmd, sizeof(options.cmd)) > 0) {
476                 r = run_cmd(options.cmd);
477         } else {
478                 r = run_sync_test(options.buffers, true);
479         }
480
481         return r;
482 }
483
484 static void preset_run_once(void)
485 {
486         igt_assert(write_seqno(1) == 0);
487         igt_assert(run_test() == 0);
488
489         igt_assert(write_seqno(0x7fffffff) == 0);
490         igt_assert(run_test() == 0);
491
492         igt_assert(write_seqno(0xffffffff) == 0);
493         igt_assert(run_test() == 0);
494
495         igt_assert(write_seqno(0xfffffff0) == 0);
496         igt_assert(run_test() == 0);
497 }
498
499 static void random_run_once(void)
500 {
501         uint32_t val;
502
503         do {
504                 val = random() % UINT32_MAX;
505                 if (RAND_MAX < UINT32_MAX)
506                         val += random();
507         } while (val == 0);
508
509         igt_assert(write_seqno(val) == 0);
510         igt_assert(run_test() == 0);
511 }
512
513 static void wrap_run_once(void)
514 {
515         const uint32_t pw_val = calc_prewrap_val();
516
517         igt_assert(write_seqno(UINT32_MAX - pw_val) == 0);
518
519         while(!read_seqno())
520                 igt_assert(run_test() == 0);
521 }
522
523 static void background_run_once(void)
524 {
525         const uint32_t pw_val = calc_prewrap_val();
526
527         igt_assert(write_seqno(UINT32_MAX - pw_val) == 0);
528
529         while(!read_seqno())
530                 sleep(3);
531 }
532
533 static void print_usage(const char *s)
534 {
535         printf("%s: [OPTION]...\n", s);
536         printf("    where options are:\n");
537         printf("    -b --background       run in background inducing wraps\n");
538         printf("    -c --cmd=cmdstring    use cmdstring to cross wrap\n");
539         printf("    -n --rounds=num       run num times across wrap boundary, 0 == forever\n");
540         printf("    -t --timeout=sec      set timeout to wait for testrun to sec seconds\n");
541         printf("    -d --dontwrap         don't wrap just run the test\n");
542         printf("    -p --prewrap=n        set seqno to WRAP - n for each testrun\n");
543         printf("    -r --norandom         dont randomize prewrap space\n");
544         printf("    -i --buffers          number of buffers to copy\n");
545         igt_fail(-1);
546 }
547
548 static void parse_options(int argc, char **argv)
549 {
550         int c;
551         int option_index = 0;
552         static struct option long_options[] = {
553                 {"cmd", required_argument, 0, 'c'},
554                 {"rounds", required_argument, 0, 'n'},
555                 {"background", no_argument, 0, 'b'},
556                 {"timeout", required_argument, 0, 't'},
557                 {"dontwrap", no_argument, 0, 'd'},
558                 {"prewrap", required_argument, 0, 'p'},
559                 {"norandom", no_argument, 0, 'r'},
560                 {"buffers", required_argument, 0, 'i'},
561         };
562
563         strcpy(options.cmd, "");
564         options.rounds = SLOW_QUICK(50, 2);
565         options.background = 0;
566         options.dontwrap = 0;
567         options.timeout = 20;
568         options.random = 1;
569         options.prewrap_space = 21;
570         options.buffers = 10;
571
572         while((c = getopt_long(argc, argv, "c:n:bvt:dp:ri:",
573                                long_options, &option_index)) != -1) {
574                 switch(c) {
575                 case 'b':
576                         options.background = 1;
577                         printf("running in background inducing wraps\n");
578                         break;
579                 case 'd':
580                         options.dontwrap = 1;
581                         printf("won't wrap after testruns\n");
582                         break;
583                 case 'n':
584                         options.rounds = atoi(optarg);
585                         printf("running %d rounds\n", options.rounds);
586                         break;
587                 case 'c':
588                         strncpy(options.cmd, optarg, sizeof(options.cmd) - 1);
589                         options.cmd[sizeof(options.cmd) - 1] = 0;
590                         printf("cmd set to %s\n", options.cmd);
591                         break;
592                 case 'i':
593                         options.buffers = atoi(optarg);
594                         printf("buffers %d\n", options.buffers);
595                         break;
596                 case 't':
597                         options.timeout = atoi(optarg);
598                         if (options.timeout == 0)
599                                 options.timeout = 10;
600                         printf("setting timeout to %d seconds\n",
601                                options.timeout);
602                         break;
603                 case 'r':
604                         options.random = 0;
605                         break;
606                 case 'p':
607                         options.prewrap_space = atoi(optarg);
608                         printf("prewrap set to %d (0x%x)\n",
609                                options.prewrap_space, UINT32_MAX -
610                                options.prewrap_space);
611                         break;
612                 default:
613                         printf("unkown command options\n");
614                         print_usage(argv[0]);
615                         break;
616                 }
617         }
618
619         if (optind < argc) {
620                 printf("unkown command options\n");
621                 print_usage(argv[0]);
622         }
623 }
624
625 int main(int argc, char **argv)
626 {
627         int wcount = 0;
628         int r = -1;
629
630         igt_simple_init();
631
632         parse_options(argc, argv);
633
634         card_index = drm_get_card();
635
636         srandom(time(NULL));
637
638         while(options.rounds == 0 || wcount < options.rounds) {
639                 if (options.background) {
640                         background_run_once();
641                 } else {
642                         preset_run_once();
643                         random_run_once();
644                         wrap_run_once();
645                 }
646
647                 wcount++;
648
649                 igt_debug("%s done: %d\n",
650                           options.dontwrap ? "tests" : "wraps", wcount);
651         }
652
653         if (options.rounds == wcount) {
654                 igt_debug("done %d wraps successfully\n", wcount);
655                 return 0;
656         }
657
658         return r;
659 }