2 * Copyright (c) 2012 Intel Corporation
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:
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
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
24 * Mika Kuoppala <mika.kuoppala@intel.com>
29 * This test runs blitcopy -> rendercopy with multiple buffers over wrap
38 #include <sys/types.h>
46 #include "ioctl_wrappers.h"
49 #include "intel_bufmgr.h"
50 #include "intel_batchbuffer.h"
52 #include "intel_chipset.h"
55 static int card_index = 0;
56 static uint32_t last_seqno = 0;
58 static struct intel_batchbuffer *batch_blt;
59 static struct intel_batchbuffer *batch_3d;
61 struct option_struct {
72 static struct option_struct options;
74 static void init_buffer(drm_intel_bufmgr *bufmgr,
77 int width, int height)
79 /* buf->bo = drm_intel_bo_alloc(bufmgr, "", size, 4096); */
81 buf->size = width * height * 4;
83 buf->tiling = I915_TILING_NONE;
84 buf->num_tiles = width * height * 4;
85 buf->stride = width * 4;
89 set_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
91 int size = width * height;
94 drm_intel_gem_bo_start_gtt_access(bo, true);
101 cmp_bo(drm_intel_bo *bo, uint32_t val, int width, int height)
103 int size = width * height;
106 drm_intel_gem_bo_start_gtt_access(bo, false);
109 if (*vaddr++ != val) {
110 printf("%d: 0x%x differs from assumed 0x%x\n",
111 width * height - size, *vaddr-1, val);
119 static drm_intel_bo *
120 create_bo(drm_intel_bufmgr *bufmgr, uint32_t val, int width, int height)
124 bo = drm_intel_bo_alloc(bufmgr, "bo", width * height * 4, 0);
127 /* gtt map doesn't have a write parameter, so just keep the mapping
128 * around (to avoid the set_domain with the gtt write domain set) and
129 * manually tell the kernel when we start access the gtt. */
130 drm_intel_gem_bo_map_gtt(bo);
132 set_bo(bo, val, width, height);
137 static void release_bo(drm_intel_bo *bo)
139 drm_intel_gem_bo_unmap_gtt(bo);
140 drm_intel_bo_unreference(bo);
143 static void render_copyfunc(struct igt_buf *src,
148 const int src_x = 0, src_y = 0, dst_x = 0, dst_y = 0;
149 igt_render_copyfunc_t rendercopy = igt_get_render_copyfunc(devid);
150 static int warned = 0;
153 rendercopy(batch_3d, NULL,
157 intel_batchbuffer_flush(batch_3d);
160 printf("No render copy found for this gen, "
161 "test is shallow!\n");
166 intel_copy_bo(batch_blt, dst->bo, src->bo, width*height*4);
167 intel_batchbuffer_flush(batch_blt);
171 static void exchange_uint(void *array, unsigned i, unsigned j)
173 unsigned *i_arr = array;
181 static int run_sync_test(int num_buffers, bool verify)
183 drm_intel_bufmgr *bufmgr;
185 drm_intel_bo **src, **dst1, **dst2;
186 int width = 128, height = 128;
191 unsigned int *p_dst1, *p_dst2;
192 struct igt_buf *s_src, *s_dst;
197 gem_quiescent_gpu(fd);
199 devid = intel_get_drm_devid(fd);
201 max = gem_aperture_size (fd) / (1024 * 1024) / 2;
202 if (num_buffers > max)
205 bufmgr = drm_intel_bufmgr_gem_init(fd, 4096);
206 drm_intel_bufmgr_gem_enable_reuse(bufmgr);
207 batch_blt = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
208 igt_assert(batch_blt);
209 batch_3d = intel_batchbuffer_alloc(bufmgr, intel_get_drm_devid(fd));
210 igt_assert(batch_3d);
212 src = malloc(num_buffers * sizeof(*src));
215 dst1 = malloc(num_buffers * sizeof(*dst1));
218 dst2 = malloc(num_buffers * sizeof(*dst2));
221 s_src = malloc(num_buffers * sizeof(*s_src));
224 s_dst = malloc(num_buffers * sizeof(*s_dst));
227 p_dst1 = malloc(num_buffers * sizeof(unsigned int));
231 p_dst2 = malloc(num_buffers * sizeof(unsigned int));
235 for (i = 0; i < num_buffers; i++) {
236 p_dst1[i] = p_dst2[i] = i;
237 src[i] = create_bo(bufmgr, i, width, height);
239 dst1[i] = create_bo(bufmgr, ~i, width, height);
241 dst2[i] = create_bo(bufmgr, ~i, width, height);
243 init_buffer(bufmgr, &s_src[i], src[i], width, height);
244 init_buffer(bufmgr, &s_dst[i], dst1[i], width, height);
247 igt_permute_array(p_dst1, num_buffers, exchange_uint);
248 igt_permute_array(p_dst2, num_buffers, exchange_uint);
250 for (i = 0; i < num_buffers; i++)
251 render_copyfunc(&s_src[i], &s_dst[p_dst1[i]], width, height);
253 /* Only sync between buffers if this is actual test run and
254 * not a seqno filler */
256 for (i = 0; i < num_buffers; i++)
257 intel_copy_bo(batch_blt, dst2[p_dst2[i]], dst1[p_dst1[i]],
260 for (i = 0; i < num_buffers; i++) {
261 r = cmp_bo(dst2[p_dst2[i]], i, width, height);
263 printf("buffer %d differs, seqno_before_test 0x%x, "
264 " approximated seqno on test fail 0x%x\n",
265 i, last_seqno, last_seqno + i * 2);
271 for (i = 0; i < num_buffers; i++) {
277 intel_batchbuffer_free(batch_3d);
278 intel_batchbuffer_free(batch_blt);
279 drm_intel_bufmgr_destroy(bufmgr);
289 gem_quiescent_gpu(fd);
296 static int run_cmd(char *s)
303 r = wordexp(s, &wexp, 0);
305 printf("can't parse %s\n", s);
309 for(i = 0; i < wexp.we_wordc; i++)
310 printf("argv[%d] = %s\n", i, wexp.we_wordv[i]);
316 char full_path[PATH_MAX];
318 if (getcwd(path, PATH_MAX) == NULL)
321 igt_assert(snprintf(full_path, PATH_MAX, "%s/%s", path, wexp.we_wordv[0]) > 0);
323 r = execv(full_path, wexp.we_wordv);
325 perror("execv failed");
327 int waitcount = options.timeout;
329 while(waitcount-- > 0) {
330 r = waitpid(pid, &status, WNOHANG);
332 if(WIFEXITED(status)) {
333 if (WEXITSTATUS(status))
335 "child returned with %d\n",
336 WEXITSTATUS(status));
337 return WEXITSTATUS(status);
354 static const char *dfs_base = "/sys/kernel/debug/dri";
355 static const char *dfs_entry = "i915_next_seqno";
357 static int dfs_open(int mode)
359 char fname[FILENAME_MAX];
362 snprintf(fname, FILENAME_MAX, "%s/%i/%s",
363 dfs_base, card_index, dfs_entry);
365 fh = open(fname, mode);
366 igt_require(fh >= 0);
371 static int __read_seqno(uint32_t *seqno)
377 unsigned long int tmp;
379 fh = dfs_open(O_RDONLY);
381 r = read(fh, buf, sizeof(buf) - 1);
390 p = strstr(buf, "0x");
395 tmp = strtoul(p, NULL, 0);
396 if (tmp == ULONG_MAX && errno) {
403 igt_debug("next_seqno: 0x%x\n", *seqno);
408 static int read_seqno(void)
414 r = __read_seqno(&seqno);
417 if (last_seqno > seqno)
425 static int write_seqno(uint32_t seqno)
432 if (options.dontwrap)
435 fh = dfs_open(O_RDWR);
436 igt_assert(snprintf(buf, sizeof(buf), "0x%x", seqno) > 0);
438 r = write(fh, buf, strnlen(buf, sizeof(buf)));
443 igt_assert(r == strnlen(buf, sizeof(buf)));
447 igt_debug("next_seqno set to: 0x%x\n", seqno);
449 r = __read_seqno(&rb);
454 printf("seqno readback differs rb:0x%x vs w:0x%x\n", rb, seqno);
461 static uint32_t calc_prewrap_val(void)
463 const int pval = options.prewrap_space;
465 if (options.random == 0)
471 return (random() % pval);
474 static int run_test(void)
478 if (strnlen(options.cmd, sizeof(options.cmd)) > 0) {
479 r = run_cmd(options.cmd);
481 r = run_sync_test(options.buffers, true);
487 static void preset_run_once(void)
489 igt_assert(write_seqno(1) == 0);
490 igt_assert(run_test() == 0);
492 igt_assert(write_seqno(0x7fffffff) == 0);
493 igt_assert(run_test() == 0);
495 igt_assert(write_seqno(0xffffffff) == 0);
496 igt_assert(run_test() == 0);
498 igt_assert(write_seqno(0xfffffff0) == 0);
499 igt_assert(run_test() == 0);
502 static void random_run_once(void)
507 val = random() % UINT32_MAX;
508 if (RAND_MAX < UINT32_MAX)
512 igt_assert(write_seqno(val) == 0);
513 igt_assert(run_test() == 0);
516 static void wrap_run_once(void)
518 const uint32_t pw_val = calc_prewrap_val();
520 igt_assert(write_seqno(UINT32_MAX - pw_val) == 0);
523 igt_assert(run_test() == 0);
526 static void background_run_once(void)
528 const uint32_t pw_val = calc_prewrap_val();
530 igt_assert(write_seqno(UINT32_MAX - pw_val) == 0);
536 static void print_usage(const char *s)
538 printf("%s: [OPTION]...\n", s);
539 printf(" where options are:\n");
540 printf(" -b --background run in background inducing wraps\n");
541 printf(" -c --cmd=cmdstring use cmdstring to cross wrap\n");
542 printf(" -n --rounds=num run num times across wrap boundary, 0 == forever\n");
543 printf(" -t --timeout=sec set timeout to wait for testrun to sec seconds\n");
544 printf(" -d --dontwrap don't wrap just run the test\n");
545 printf(" -p --prewrap=n set seqno to WRAP - n for each testrun\n");
546 printf(" -r --norandom dont randomize prewrap space\n");
547 printf(" -i --buffers number of buffers to copy\n");
551 static void parse_options(int argc, char **argv)
554 int option_index = 0;
555 static struct option long_options[] = {
556 {"cmd", required_argument, 0, 'c'},
557 {"rounds", required_argument, 0, 'n'},
558 {"background", no_argument, 0, 'b'},
559 {"timeout", required_argument, 0, 't'},
560 {"dontwrap", no_argument, 0, 'd'},
561 {"prewrap", required_argument, 0, 'p'},
562 {"norandom", no_argument, 0, 'r'},
563 {"buffers", required_argument, 0, 'i'},
566 strcpy(options.cmd, "");
567 options.rounds = SLOW_QUICK(50, 2);
568 options.background = 0;
569 options.dontwrap = 0;
570 options.timeout = 20;
572 options.prewrap_space = 21;
573 options.buffers = 10;
575 while((c = getopt_long(argc, argv, "c:n:bvt:dp:ri:",
576 long_options, &option_index)) != -1) {
579 options.background = 1;
580 printf("running in background inducing wraps\n");
583 options.dontwrap = 1;
584 printf("won't wrap after testruns\n");
587 options.rounds = atoi(optarg);
588 printf("running %d rounds\n", options.rounds);
591 strncpy(options.cmd, optarg, sizeof(options.cmd) - 1);
592 options.cmd[sizeof(options.cmd) - 1] = 0;
593 printf("cmd set to %s\n", options.cmd);
596 options.buffers = atoi(optarg);
597 printf("buffers %d\n", options.buffers);
600 options.timeout = atoi(optarg);
601 if (options.timeout == 0)
602 options.timeout = 10;
603 printf("setting timeout to %d seconds\n",
610 options.prewrap_space = atoi(optarg);
611 printf("prewrap set to %d (0x%x)\n",
612 options.prewrap_space, UINT32_MAX -
613 options.prewrap_space);
616 printf("unkown command options\n");
617 print_usage(argv[0]);
623 printf("unkown command options\n");
624 print_usage(argv[0]);
628 int main(int argc, char **argv)
635 parse_options(argc, argv);
637 card_index = drm_get_card();
641 while(options.rounds == 0 || wcount < options.rounds) {
642 if (options.background) {
643 background_run_once();
652 igt_debug("%s done: %d\n",
653 options.dontwrap ? "tests" : "wraps", wcount);
656 if (options.rounds == wcount) {
657 igt_debug("done %d wraps successfully\n", wcount);