tests: Instrument gem_seqno_wrap to run in simulation
[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 <assert.h>
37 #include <fcntl.h>
38 #include <unistd.h>
39 #include <sys/types.h>
40 #include <sys/wait.h>
41 #include <limits.h>
42 #include <wordexp.h>
43 #include <signal.h>
44
45 #include "i915_drm.h"
46 #include "intel_bufmgr.h"
47 #include "intel_batchbuffer.h"
48 #include "intel_gpu_tools.h"
49 #include "rendercopy.h"
50
51 static int devid;
52 static int card_index = 0;
53 static uint32_t last_seqno = 0;
54
55 static struct intel_batchbuffer *batch_blt;
56 static struct intel_batchbuffer *batch_3d;
57
58 struct option_struct {
59         int rounds;
60         int background;
61         char cmd[1024];
62         int verbose;
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 scratch_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         assert(buf->bo);
81         buf->tiling = I915_TILING_NONE;
82         buf->data = buf->cpu_mapping = NULL;
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 int
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                 if (*vaddr++ != val) {
109                         printf("%d: 0x%x differs from assumed 0x%x\n",
110                                width * height - size, *vaddr-1, val);
111                         return -1;
112                 }
113         }
114
115         return 0;
116 }
117
118 static drm_intel_bo *
119 create_bo(drm_intel_bufmgr *bufmgr, uint32_t val, int width, int height)
120 {
121         drm_intel_bo *bo;
122
123         bo = drm_intel_bo_alloc(bufmgr, "bo", width * height * 4, 0);
124         assert(bo);
125
126         /* gtt map doesn't have a write parameter, so just keep the mapping
127          * around (to avoid the set_domain with the gtt write domain set) and
128          * manually tell the kernel when we start access the gtt. */
129         drm_intel_gem_bo_map_gtt(bo);
130
131         set_bo(bo, val, width, height);
132
133         return bo;
134 }
135
136 static void release_bo(drm_intel_bo *bo)
137 {
138         drm_intel_gem_bo_unmap_gtt(bo);
139         drm_intel_bo_unreference(bo);
140 }
141
142 static void render_copyfunc(struct scratch_buf *src,
143                             struct scratch_buf *dst,
144                             int width,
145                             int height)
146 {
147         const int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0;
148         render_copyfunc_t rendercopy = get_render_copyfunc(devid);
149         static int warned = 0;
150
151         if (rendercopy) {
152                 rendercopy(batch_3d,
153                            src, src_x, src_y,
154                            width, height,
155                            dst, dst_x, dst_y);
156                 intel_batchbuffer_flush(batch_3d);
157         } else {
158                 if (!warned) {
159                         printf("No render copy found for this gen, "
160                                "test is shallow!\n");
161                         warned = 1;
162                 }
163                 assert(dst->bo);
164                 assert(src->bo);
165                 intel_copy_bo(batch_blt, dst->bo, src->bo, width, height);
166                 intel_batchbuffer_flush(batch_blt);
167         }
168 }
169
170 static void exchange_uint(void *array, unsigned i, unsigned j)
171 {
172         unsigned *i_arr = array;
173         unsigned i_tmp;
174
175         i_tmp = i_arr[i];
176         i_arr[i] = i_arr[j];
177         i_arr[j] = i_tmp;
178 }
179
180 static int run_sync_test(int num_buffers, bool verify)
181 {
182         drm_intel_bufmgr *bufmgr;
183         int max;
184         drm_intel_bo **src, **dst1, **dst2;
185         int width = 128, height = 128;
186         int fd;
187         int i;
188         int r = -1;
189         int failed = 0;
190         unsigned int *p_dst1, *p_dst2;
191         struct scratch_buf *s_src, *s_dst;
192
193         fd = drm_open_any();
194         assert(fd >= 0);
195
196         gem_quiescent_gpu(fd);
197
198         devid = intel_get_drm_devid(fd);
199
200         max = gem_aperture_size (fd) / (1024 * 1024) / 2;
201         if (num_buffers > max)
202                 num_buffers = max;
203
204         bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
205         drm_intel_bufmgr_gem_enable_reuse(bufmgr);
206         batch_blt = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
207         assert(batch_blt);
208         batch_3d = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
209         assert(batch_3d);
210
211         src = malloc(num_buffers * sizeof(**src));
212         assert(src);
213
214         dst1 = malloc(num_buffers * sizeof(**dst1));
215         assert(dst1);
216
217         dst2 = malloc(num_buffers * sizeof(**dst2));
218         assert(dst2);
219
220         s_src = malloc(num_buffers * sizeof(*s_src));
221         assert(s_src);
222
223         s_dst = malloc(num_buffers * sizeof(*s_dst));
224         assert(s_dst);
225
226         p_dst1 = malloc(num_buffers * sizeof(unsigned int));
227         if (p_dst1 == NULL)
228                 return -ENOMEM;
229
230         p_dst2 = malloc(num_buffers * sizeof(unsigned int));
231         if (p_dst2 == NULL)
232                 return -ENOMEM;
233
234         for (i = 0; i < num_buffers; i++) {
235                 p_dst1[i] = p_dst2[i] = i;
236                 src[i] = create_bo(bufmgr, i, width, height);
237                 assert(src[i]);
238                 dst1[i] = create_bo(bufmgr, ~i, width, height);
239                 assert(dst1[i]);
240                 dst2[i] = create_bo(bufmgr, ~i, width, height);
241                 assert(dst2[i]);
242                 init_buffer(bufmgr, &s_src[i], src[i], width, height);
243                 init_buffer(bufmgr, &s_dst[i], dst1[i], width, height);
244         }
245
246         drmtest_permute_array(p_dst1, num_buffers, exchange_uint);
247         drmtest_permute_array(p_dst2, num_buffers, exchange_uint);
248
249         for (i = 0; i < num_buffers; i++)
250                 render_copyfunc(&s_src[i], &s_dst[p_dst1[i]], width, height);
251
252         /* Only sync between buffers if this is actual test run and
253          * not a seqno filler */
254         if (verify) {
255                 for (i = 0; i < num_buffers; i++)
256                         intel_copy_bo(batch_blt, dst2[p_dst2[i]], dst1[p_dst1[i]],
257                                       width, height);
258
259                 for (i = 0; i < num_buffers; i++) {
260                         r = cmp_bo(dst2[p_dst2[i]], i, width, height);
261                         if (r) {
262                                 printf("buffer %d differs, seqno_before_test 0x%x, "
263                                        " approximated seqno on test fail 0x%x\n",
264                                        i, last_seqno, last_seqno + i * 2);
265                                 failed = -1;
266                         }
267                 }
268         }
269
270         for (i = 0; i < num_buffers; i++) {
271                 release_bo(src[i]);
272                 release_bo(dst1[i]);
273                 release_bo(dst2[i]);
274         }
275
276         intel_batchbuffer_free(batch_3d);
277         intel_batchbuffer_free(batch_blt);
278         drm_intel_bufmgr_destroy(bufmgr);
279
280         free(p_dst1);
281         free(p_dst2);
282         free(s_dst);
283         free(s_src);
284         free(dst2);
285         free(dst1);
286         free(src);
287
288         gem_quiescent_gpu(fd);
289
290         close(fd);
291
292         return failed;
293 }
294
295 static int run_cmd(char *s)
296 {
297         int pid;
298         int r = -1;
299         int status = 0;
300         wordexp_t wexp;
301         int i;
302         r = wordexp(s, &wexp, 0);
303         if (r != 0) {
304                 printf("can't parse %s\n", s);
305                 return r;
306         }
307
308         for(i = 0; i < wexp.we_wordc; i++)
309                 printf("argv[%d] = %s\n", i, wexp.we_wordv[i]);
310
311         pid = fork();
312
313         if (pid == 0) {
314                 char path[PATH_MAX];
315                 char full_path[PATH_MAX];
316
317                 if (getcwd(path, PATH_MAX) == NULL)
318                         perror("getcwd");
319
320                 assert(snprintf(full_path, PATH_MAX, "%s/%s", path, wexp.we_wordv[0]) > 0);
321
322                 /* if (!options.verbose) {
323                         close(STDOUT_FILENO);
324                         close(STDERR_FILENO);
325                 }
326                 */
327
328                 r = execv(full_path, wexp.we_wordv);
329                 if (r == -1)
330                         perror("execv failed");
331         } else {
332                 int waitcount = options.timeout;
333
334                 while(waitcount-- > 0) {
335                         r = waitpid(pid, &status, WNOHANG);
336                         if (r == pid) {
337                                 if(WIFEXITED(status)) {
338                                         if (WEXITSTATUS(status))
339                                                 fprintf(stderr,
340                                                     "child returned with %d\n",
341                                                         WEXITSTATUS(status));
342                                         return WEXITSTATUS(status);
343                                 }
344                         } else if (r != 0) {
345                                 perror("waitpid");
346                                 return -errno;
347                         }
348
349                         sleep(3);
350                 }
351
352                 kill(pid, SIGKILL);
353                 return -ETIMEDOUT;
354         }
355
356         return r;
357 }
358
359 static const char *dfs_base = "/sys/kernel/debug/dri";
360 static const char *dfs_entry = "i915_next_seqno";
361
362 static int dfs_open(int mode)
363 {
364         char fname[FILENAME_MAX];
365         int fh;
366
367         snprintf(fname, FILENAME_MAX, "%s/%i/%s",
368                  dfs_base, card_index, dfs_entry);
369
370         fh = open(fname, mode);
371         if (fh == -1) {
372                 fprintf(stderr,
373                         "error %d opening '%s/%d/%s'. too old kernel?\n",
374                         errno, dfs_base, card_index, dfs_entry);
375                 exit(77);
376         }
377
378         return fh;
379 }
380
381 static int __read_seqno(uint32_t *seqno)
382 {
383         int fh;
384         char buf[32];
385         int r;
386         char *p;
387         unsigned long int tmp;
388
389         fh = dfs_open(O_RDONLY);
390
391         r = read(fh, buf, sizeof(buf) - 1);
392         close(fh);
393         if (r < 0) {
394                 perror("read");
395                 return -errno;
396         }
397
398         buf[r] = 0;
399
400         p = strstr(buf, "0x");
401         if (!p)
402                 p = buf;
403
404         errno = 0;
405         tmp = strtoul(p, NULL, 0);
406         if (tmp == ULONG_MAX && errno) {
407                 perror("strtoul");
408                 return -errno;
409         }
410
411         *seqno = tmp;
412
413         if (options.verbose)
414                 printf("next_seqno: 0x%x\n", *seqno);
415
416         return 0;
417 }
418
419 static int read_seqno(void)
420 {
421         uint32_t seqno = 0;
422         int r;
423         int wrap = 0;
424
425         r = __read_seqno(&seqno);
426         assert(r == 0);
427
428         if (last_seqno > seqno)
429                 wrap++;
430
431         last_seqno = seqno;
432
433         return wrap;
434 }
435
436 static int write_seqno(uint32_t seqno)
437 {
438         int fh;
439         char buf[32];
440         int r;
441         uint32_t rb;
442
443         if (options.dontwrap)
444                 return 0;
445
446         fh = dfs_open(O_RDWR);
447         assert(snprintf(buf, sizeof(buf), "0x%x", seqno) > 0);
448
449         r = write(fh, buf, strnlen(buf, sizeof(buf)));
450         close(fh);
451         if (r < 0)
452                 return r;
453
454         assert(r == strnlen(buf, sizeof(buf)));
455
456         last_seqno = seqno;
457
458         if (options.verbose)
459                 printf("next_seqno set to: 0x%x\n", seqno);
460
461         r = __read_seqno(&rb);
462         if (r < 0)
463                 return r;
464
465         if (rb != seqno) {
466                 printf("seqno readback differs rb:0x%x vs w:0x%x\n", rb, seqno);
467                 return -1;
468         }
469
470         return 0;
471 }
472
473 static uint32_t calc_prewrap_val(void)
474 {
475         const int pval = options.prewrap_space;
476
477         if (options.random == 0)
478                 return pval;
479
480         if (pval == 0)
481                 return 0;
482
483         return (random() % pval);
484 }
485
486 static int run_test(void)
487 {
488         int r;
489
490         if (strnlen(options.cmd, sizeof(options.cmd)) > 0) {
491                 r = run_cmd(options.cmd);
492         } else {
493                 r = run_sync_test(options.buffers, true);
494         }
495
496         return r;
497 }
498
499 static void preset_run_once(void)
500 {
501         assert(write_seqno(1) == 0);
502         assert(run_test() == 0);
503
504         assert(write_seqno(0x7fffffff) == 0);
505         assert(run_test() == 0);
506
507         assert(write_seqno(0xffffffff) == 0);
508         assert(run_test() == 0);
509
510         assert(write_seqno(0xfffffff0) == 0);
511         assert(run_test() == 0);
512 }
513
514 static void random_run_once(void)
515 {
516         uint32_t val;
517
518         do {
519                 val = random() % UINT32_MAX;
520                 if (RAND_MAX < UINT32_MAX)
521                         val += random();
522         } while (val == 0);
523
524         assert(write_seqno(val) == 0);
525         assert(run_test() == 0);
526 }
527
528 static void wrap_run_once(void)
529 {
530         const uint32_t pw_val = calc_prewrap_val();
531
532         assert(write_seqno(UINT32_MAX - pw_val) == 0);
533
534         while(!read_seqno())
535                 assert(run_test() == 0);
536 }
537
538 static void background_run_once(void)
539 {
540         const uint32_t pw_val = calc_prewrap_val();
541
542         assert(write_seqno(UINT32_MAX - pw_val) == 0);
543
544         while(!read_seqno())
545                 sleep(3);
546 }
547
548 static void print_usage(const char *s)
549 {
550         printf("%s: [OPTION]...\n", s);
551         printf("    where options are:\n");
552         printf("    -b --background       run in background inducing wraps\n");
553         printf("    -c --cmd=cmdstring    use cmdstring to cross wrap\n");
554         printf("    -n --rounds=num       run num times across wrap boundary, 0 == forever\n");
555         printf("    -t --timeout=sec      set timeout to wait for testrun to sec seconds\n");
556         printf("    -d --dontwrap         don't wrap just run the test\n");
557         printf("    -p --prewrap=n        set seqno to WRAP - n for each testrun\n");
558         printf("    -r --norandom         dont randomize prewrap space\n");
559         printf("    -i --buffers          number of buffers to copy\n");
560         exit(-1);
561 }
562
563 static void parse_options(int argc, char **argv)
564 {
565         int c;
566         int option_index = 0;
567         static struct option long_options[] = {
568                 {"cmd", required_argument, 0, 'c'},
569                 {"rounds", required_argument, 0, 'n'},
570                 {"background", no_argument, 0, 'b'},
571                 {"timeout", required_argument, 0, 't'},
572                 {"dontwrap", no_argument, 0, 'd'},
573                 {"verbose", no_argument, 0, 'v'},
574                 {"prewrap", required_argument, 0, 'p'},
575                 {"norandom", no_argument, 0, 'r'},
576                 {"buffers", required_argument, 0, 'i'},
577         };
578
579         strcpy(options.cmd, "");
580         options.rounds = SLOW_QUICK(50, 2);
581         options.background = 0;
582         options.dontwrap = 0;
583         options.timeout = 20;
584         options.verbose = 0;
585         options.random = 1;
586         options.prewrap_space = 21;
587         options.buffers = 10;
588
589         while((c = getopt_long(argc, argv, "c:n:bvt:dp:ri:",
590                                long_options, &option_index)) != -1) {
591                 switch(c) {
592                 case 'b':
593                         options.background = 1;
594                         printf("running in background inducing wraps\n");
595                         break;
596                 case 'd':
597                         options.dontwrap = 1;
598                         printf("won't wrap after testruns\n");
599                         break;
600                 case 'n':
601                         options.rounds = atoi(optarg);
602                         printf("running %d rounds\n", options.rounds);
603                         break;
604                 case 'c':
605                         strncpy(options.cmd, optarg, sizeof(options.cmd) - 1);
606                         options.cmd[sizeof(options.cmd) - 1] = 0;
607                         printf("cmd set to %s\n", options.cmd);
608                         break;
609                 case 'i':
610                         options.buffers = atoi(optarg);
611                         printf("buffers %d\n", options.buffers);
612                         break;
613                 case 't':
614                         options.timeout = atoi(optarg);
615                         if (options.timeout == 0)
616                                 options.timeout = 10;
617                         printf("setting timeout to %d seconds\n",
618                                options.timeout);
619                         break;
620                 case 'v':
621                         options.verbose = 1;
622                         break;
623                 case 'r':
624                         options.random = 0;
625                         break;
626                 case 'p':
627                         options.prewrap_space = atoi(optarg);
628                         printf("prewrap set to %d (0x%x)\n",
629                                options.prewrap_space, UINT32_MAX -
630                                options.prewrap_space);
631                         break;
632                 default:
633                         printf("unkown command options\n");
634                         print_usage(argv[0]);
635                         break;
636                 }
637         }
638
639         if (optind < argc) {
640                 printf("unkown command options\n");
641                 print_usage(argv[0]);
642         }
643 }
644
645 int main(int argc, char **argv)
646 {
647         int wcount = 0;
648         int r = -1;
649
650         parse_options(argc, argv);
651
652         card_index = drm_get_card(0);
653         assert(card_index != -1);
654
655         srandom(time(NULL));
656
657         while(options.rounds == 0 || wcount < options.rounds) {
658                 if (options.background) {
659                         background_run_once();
660                 } else {
661                         preset_run_once();
662                         random_run_once();
663                         wrap_run_once();
664                 }
665
666                 wcount++;
667
668                 if (options.verbose) {
669                         printf("%s done: %d\n",
670                                options.dontwrap ? "tests" : "wraps", wcount);
671                         fflush(stdout);
672                 }
673         }
674
675         if (options.rounds == wcount) {
676                 if (options.verbose)
677                         printf("done %d wraps successfully\n", wcount);
678                 return 0;
679         }
680
681         return r;
682 }