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