tests/gem_reset_stats: check non root access to reset_stats
[platform/upstream/intel-gpu-tools.git] / tests / gem_reset_stats.c
1 /*
2  * Copyright (c) 2013 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 #include <unistd.h>
29 #include <stdlib.h>
30 #include <stdio.h>
31 #include <string.h>
32 #include <fcntl.h>
33 #include <inttypes.h>
34 #include <errno.h>
35 #include <sys/stat.h>
36 #include <sys/ioctl.h>
37 #include <sys/mman.h>
38 #include <time.h>
39
40 #include "i915_drm.h"
41 #include "intel_bufmgr.h"
42 #include "intel_batchbuffer.h"
43 #include "intel_gpu_tools.h"
44 #include "rendercopy.h"
45 #include "igt_debugfs.h"
46
47 #define RS_NO_ERROR      0
48 #define RS_BATCH_ACTIVE  (1 << 0)
49 #define RS_BATCH_PENDING (1 << 1)
50 #define RS_UNKNOWN       (1 << 2)
51
52 struct local_drm_i915_reset_stats {
53         __u32 ctx_id;
54         __u32 flags;
55         __u32 reset_count;
56         __u32 batch_active;
57         __u32 batch_pending;
58         __u32 pad;
59 };
60
61 struct local_drm_i915_gem_context_create {
62         __u32 ctx_id;
63         __u32 pad;
64 };
65
66 struct local_drm_i915_gem_context_destroy {
67         __u32 ctx_id;
68         __u32 pad;
69 };
70
71 #define MAX_FD 32
72
73 #define CONTEXT_CREATE_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2d, struct local_drm_i915_gem_context_create)
74 #define CONTEXT_DESTROY_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2e, struct local_drm_i915_gem_context_destroy)
75 #define GET_RESET_STATS_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x32, struct local_drm_i915_reset_stats)
76
77 static igt_debugfs_t dfs;
78
79 static uint32_t context_create(int fd)
80 {
81         struct local_drm_i915_gem_context_create create;
82         int ret;
83
84         create.ctx_id = rand();
85         create.pad = rand();
86
87         ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
88         igt_assert(ret == 0);
89
90         return create.ctx_id;
91 }
92
93 static int context_destroy(int fd, uint32_t ctx_id)
94 {
95         int ret;
96         struct local_drm_i915_gem_context_destroy destroy;
97
98         destroy.ctx_id = ctx_id;
99         destroy.pad = rand();
100
101         ret = drmIoctl(fd, CONTEXT_DESTROY_IOCTL, &destroy);
102         if (ret != 0)
103                 return -errno;
104
105         return 0;
106 }
107
108 static int gem_reset_stats(int fd, int ctx_id,
109                            struct local_drm_i915_reset_stats *rs)
110 {
111         int ret;
112
113         rs->ctx_id = ctx_id;
114         rs->flags = 0;
115         rs->reset_count = rand();
116         rs->batch_active = rand();
117         rs->batch_pending = rand();
118         rs->pad = 0;
119
120         do {
121                 ret = ioctl(fd, GET_RESET_STATS_IOCTL, rs);
122         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
123
124         if (ret < 0)
125                 return -errno;
126
127         return 0;
128 }
129
130 static int gem_reset_status(int fd, int ctx_id)
131 {
132         int ret;
133         struct local_drm_i915_reset_stats rs;
134
135         ret = gem_reset_stats(fd, ctx_id, &rs);
136         if (ret)
137                 return ret;
138
139         if (rs.batch_active)
140                 return RS_BATCH_ACTIVE;
141         if (rs.batch_pending)
142                 return RS_BATCH_PENDING;
143
144         return RS_NO_ERROR;
145 }
146
147 static int gem_exec(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
148 {
149         int ret;
150
151         ret = ioctl(fd,
152                     DRM_IOCTL_I915_GEM_EXECBUFFER2,
153                     execbuf);
154
155         if (ret < 0)
156                 return -errno;
157
158         return 0;
159 }
160
161 static int exec_valid(int fd, int ctx)
162 {
163         struct drm_i915_gem_execbuffer2 execbuf;
164         struct drm_i915_gem_exec_object2 exec;
165         int ret;
166
167         uint32_t buf[2] = { MI_BATCH_BUFFER_END, 0 };
168
169         exec.handle = gem_create(fd, 4096);
170         gem_write(fd, exec.handle, 0, buf, sizeof(buf));
171         exec.relocation_count = 0;
172         exec.relocs_ptr = 0;
173         exec.alignment = 0;
174         exec.offset = 0;
175         exec.flags = 0;
176         exec.rsvd1 = 0;
177         exec.rsvd2 = 0;
178
179         execbuf.buffers_ptr = (uintptr_t)&exec;
180         execbuf.buffer_count = 1;
181         execbuf.batch_start_offset = 0;
182         execbuf.batch_len = sizeof(buf);
183         execbuf.cliprects_ptr = 0;
184         execbuf.num_cliprects = 0;
185         execbuf.DR1 = 0;
186         execbuf.DR4 = 0;
187         execbuf.flags = 0;
188         i915_execbuffer2_set_context_id(execbuf, ctx);
189         execbuf.rsvd2 = 0;
190
191         ret = gem_exec(fd, &execbuf);
192         if (ret < 0)
193                 return ret;
194
195         return exec.handle;
196 }
197
198 static void stop_rings(void)
199 {
200         int fd;
201
202         fd = igt_debugfs_open(&dfs, "i915_ring_stop", O_WRONLY);
203         igt_assert(fd >= 0);
204
205         igt_assert(write(fd, "0xff", 4) == 4);
206         close(fd);
207 }
208
209 #define BUFSIZE (4 * 1024)
210 #define ITEMS   (BUFSIZE >> 2)
211
212 static int inject_hang(int fd, int ctx)
213 {
214         struct drm_i915_gem_execbuffer2 execbuf;
215         struct drm_i915_gem_exec_object2 exec;
216         uint64_t gtt_off;
217         uint32_t *buf;
218         int roff, i;
219         unsigned cmd_len = 2;
220
221         srandom(time(NULL));
222
223         if (intel_gen(intel_get_drm_devid(fd)) >= 8)
224                 cmd_len = 3;
225
226         buf = malloc(BUFSIZE);
227         igt_assert(buf != NULL);
228
229         buf[0] = MI_BATCH_BUFFER_END;
230         buf[1] = MI_NOOP;
231
232         exec.handle = gem_create(fd, BUFSIZE);
233         gem_write(fd, exec.handle, 0, buf, BUFSIZE);
234         exec.relocation_count = 0;
235         exec.relocs_ptr = 0;
236         exec.alignment = 0;
237         exec.offset = 0;
238         exec.flags = 0;
239         exec.rsvd1 = 0;
240         exec.rsvd2 = 0;
241
242         execbuf.buffers_ptr = (uintptr_t)&exec;
243         execbuf.buffer_count = 1;
244         execbuf.batch_start_offset = 0;
245         execbuf.batch_len = BUFSIZE;
246         execbuf.cliprects_ptr = 0;
247         execbuf.num_cliprects = 0;
248         execbuf.DR1 = 0;
249         execbuf.DR4 = 0;
250         execbuf.flags = 0;
251         i915_execbuffer2_set_context_id(execbuf, ctx);
252         execbuf.rsvd2 = 0;
253
254         igt_assert(gem_exec(fd, &execbuf) == 0);
255
256         gtt_off = exec.offset;
257
258         for (i = 0; i < ITEMS; i++)
259                 buf[i] = MI_NOOP;
260
261         roff = random() % (ITEMS - cmd_len);
262         buf[roff] = MI_BATCH_BUFFER_START | (cmd_len - 2);
263         buf[roff + 1] = (gtt_off & 0xfffffffc) + (roff << 2);
264         if (cmd_len == 3)
265                 buf[roff + 2] = gtt_off & 0xffffffff00000000ull;
266
267 #ifdef VERBOSE
268         printf("loop injected at 0x%lx (off 0x%x, bo_start 0x%lx, bo_end 0x%lx)\n",
269                (long unsigned int)((roff << 2) + gtt_off),
270                roff << 2, (long unsigned int)gtt_off,
271                (long unsigned int)(gtt_off + BUFSIZE - 1));
272 #endif
273         gem_write(fd, exec.handle, 0, buf, BUFSIZE);
274
275         exec.relocation_count = 0;
276         exec.relocs_ptr = 0;
277         exec.alignment = 0;
278         exec.offset = 0;
279         exec.flags = 0;
280         exec.rsvd1 = 0;
281         exec.rsvd2 = 0;
282
283         execbuf.buffers_ptr = (uintptr_t)&exec;
284         execbuf.buffer_count = 1;
285         execbuf.batch_start_offset = 0;
286         execbuf.batch_len = BUFSIZE;
287         execbuf.cliprects_ptr = 0;
288         execbuf.num_cliprects = 0;
289         execbuf.DR1 = 0;
290         execbuf.DR4 = 0;
291         execbuf.flags = 0;
292         i915_execbuffer2_set_context_id(execbuf, ctx);
293         execbuf.rsvd2 = 0;
294
295         igt_assert(gem_exec(fd, &execbuf) == 0);
296
297         igt_assert(gtt_off == exec.offset);
298
299         free(buf);
300
301         stop_rings();
302
303         return exec.handle;
304 }
305
306 static int _assert_reset_status(int fd, int ctx, int status)
307 {
308         int rs;
309
310         rs = gem_reset_status(fd, ctx);
311         if (rs < 0) {
312                 printf("reset status for %d ctx %d returned %d\n",
313                        fd, ctx, rs);
314                 return rs;
315         }
316
317         if (rs != status) {
318                 printf("%d:%d reset status %d differs from assumed %d\n",
319                        fd, ctx, rs, status);
320
321                 return 1;
322         }
323
324         return 0;
325 }
326
327 #define assert_reset_status(fd, ctx, status) \
328         igt_assert(_assert_reset_status(fd, ctx, status) == 0)
329
330 static void test_rs(int num_fds, int hang_index, int rs_assumed_no_hang)
331 {
332         int i;
333         int fd[MAX_FD];
334         int h[MAX_FD];
335
336         igt_assert (num_fds <= MAX_FD);
337         igt_assert (hang_index < MAX_FD);
338
339         for (i = 0; i < num_fds; i++) {
340                 fd[i] = drm_open_any();
341                 igt_assert(fd[i]);
342         }
343
344         for (i = 0; i < num_fds; i++)
345                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
346
347         for (i = 0; i < num_fds; i++) {
348                 if (i == hang_index)
349                         h[i] = inject_hang(fd[i], 0);
350                 else
351                         h[i] = exec_valid(fd[i], 0);
352         }
353
354         gem_sync(fd[num_fds - 1], h[num_fds - 1]);
355
356         for (i = 0; i < num_fds; i++) {
357                 if (hang_index < 0) {
358                         assert_reset_status(fd[i], 0, rs_assumed_no_hang);
359                         continue;
360                 }
361
362                 if (i < hang_index)
363                         assert_reset_status(fd[i], 0, RS_NO_ERROR);
364                 if (i == hang_index)
365                         assert_reset_status(fd[i], 0, RS_BATCH_ACTIVE);
366                 if (i > hang_index)
367                         assert_reset_status(fd[i], 0, RS_BATCH_PENDING);
368         }
369
370         for (i = 0; i < num_fds; i++) {
371                 gem_close(fd[i], h[i]);
372                 close(fd[i]);
373         }
374 }
375
376 #define MAX_CTX 100
377 static void test_rs_ctx(int num_fds, int num_ctx, int hang_index,
378                         int hang_context)
379 {
380         int i, j;
381         int fd[MAX_FD];
382         int h[MAX_FD][MAX_CTX];
383         int ctx[MAX_FD][MAX_CTX];
384
385         igt_assert (num_fds <= MAX_FD);
386         igt_assert (hang_index < MAX_FD);
387
388         igt_assert (num_ctx <= MAX_CTX);
389         igt_assert (hang_context < MAX_CTX);
390
391         test_rs(num_fds, -1, RS_NO_ERROR);
392
393         for (i = 0; i < num_fds; i++) {
394                 fd[i] = drm_open_any();
395                 igt_assert(fd[i]);
396                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
397
398                 for (j = 0; j < num_ctx; j++) {
399                         ctx[i][j] = context_create(fd[i]);
400
401                 }
402
403                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
404         }
405
406         for (i = 0; i < num_fds; i++) {
407
408                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
409
410                 for (j = 0; j < num_ctx; j++)
411                         assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
412
413                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
414         }
415
416         for (i = 0; i < num_fds; i++) {
417                 for (j = 0; j < num_ctx; j++) {
418                         if (i == hang_index && j == hang_context)
419                                 h[i][j] = inject_hang(fd[i], ctx[i][j]);
420                         else
421                                 h[i][j] = exec_valid(fd[i], ctx[i][j]);
422                 }
423         }
424
425         gem_sync(fd[num_fds - 1], ctx[num_fds - 1][num_ctx - 1]);
426
427         for (i = 0; i < num_fds; i++)
428                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
429
430         for (i = 0; i < num_fds; i++) {
431                 for (j = 0; j < num_ctx; j++) {
432                         if (i < hang_index)
433                                 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
434                         if (i == hang_index && j < hang_context)
435                                 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
436                         if (i == hang_index && j == hang_context)
437                                 assert_reset_status(fd[i], ctx[i][j],
438                                                     RS_BATCH_ACTIVE);
439                         if (i == hang_index && j > hang_context)
440                                 assert_reset_status(fd[i], ctx[i][j],
441                                                     RS_BATCH_PENDING);
442                         if (i > hang_index)
443                                 assert_reset_status(fd[i], ctx[i][j],
444                                                     RS_BATCH_PENDING);
445                 }
446         }
447
448         for (i = 0; i < num_fds; i++) {
449                 for (j = 0; j < num_ctx; j++) {
450                         gem_close(fd[i], h[i][j]);
451                         igt_assert(context_destroy(fd[i], ctx[i][j]) == 0);
452                 }
453
454                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
455
456                 close(fd[i]);
457         }
458 }
459
460 static void test_ban(void)
461 {
462         int h1,h2,h3,h4,h5,h6,h7;
463         int ctx_good, ctx_bad;
464         int fd;
465         int retry = 10;
466         int active_count = 0, pending_count = 0;
467         struct local_drm_i915_reset_stats rs_bad, rs_good;
468
469         fd = drm_open_any();
470         igt_assert(fd >= 0);
471
472         assert_reset_status(fd, 0, RS_NO_ERROR);
473
474         ctx_good = context_create(fd);
475         ctx_bad = context_create(fd);
476
477         assert_reset_status(fd, 0, RS_NO_ERROR);
478         assert_reset_status(fd, ctx_good, RS_NO_ERROR);
479         assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
480
481         h1 = exec_valid(fd, ctx_bad);
482         igt_assert(h1 >= 0);
483         h5 = exec_valid(fd, ctx_good);
484         igt_assert(h5 >= 0);
485
486         assert_reset_status(fd, ctx_good, RS_NO_ERROR);
487         assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
488
489         h2 = inject_hang(fd, ctx_bad);
490         igt_assert(h2 >= 0);
491         active_count++;
492         /* Second hang will be pending for this */
493         pending_count++;
494
495         h6 = exec_valid(fd, ctx_good);
496         h7 = exec_valid(fd, ctx_good);
497
498         while (retry--) {
499                 h3 = inject_hang(fd, ctx_bad);
500                 igt_assert(h3 >= 0);
501                 gem_sync(fd, h3);
502                 active_count++;
503                 /* This second hand will count as pending */
504                 assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
505
506                 h4 = exec_valid(fd, ctx_bad);
507                 if (h4 == -EIO) {
508                         gem_close(fd, h3);
509                         break;
510                 }
511
512                 /* Should not happen often but sometimes hang is declared too slow
513                  * due to our way of faking hang using loop */
514
515                 igt_assert(h4 >= 0);
516                 gem_close(fd, h3);
517                 gem_close(fd, h4);
518
519                 printf("retrying for ban (%d)\n", retry);
520         }
521
522         igt_assert(h4 == -EIO);
523         assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
524
525         gem_sync(fd, h7);
526         assert_reset_status(fd, ctx_good, RS_BATCH_PENDING);
527
528         igt_assert(gem_reset_stats(fd, ctx_good, &rs_good) == 0);
529         igt_assert(gem_reset_stats(fd, ctx_bad, &rs_bad) == 0);
530
531         igt_assert(rs_bad.batch_active == active_count);
532         igt_assert(rs_bad.batch_pending == pending_count);
533         igt_assert(rs_good.batch_active == 0);
534         igt_assert(rs_good.batch_pending == 2);
535
536         gem_close(fd, h1);
537         gem_close(fd, h2);
538         gem_close(fd, h6);
539         gem_close(fd, h7);
540
541         h1 = exec_valid(fd, ctx_good);
542         igt_assert(h1 >= 0);
543         gem_close(fd, h1);
544
545         igt_assert(context_destroy(fd, ctx_good) == 0);
546         igt_assert(context_destroy(fd, ctx_bad) == 0);
547         igt_assert(gem_reset_status(fd, ctx_good) < 0);
548         igt_assert(gem_reset_status(fd, ctx_bad) < 0);
549         igt_assert(exec_valid(fd, ctx_good) < 0);
550         igt_assert(exec_valid(fd, ctx_bad) < 0);
551
552         close(fd);
553 }
554
555 static void test_nonrelated_hang(void)
556 {
557         int h1,h2;
558         int fd1,fd2;
559         int ctx_guilty, ctx_unrelated;
560
561         fd1 = drm_open_any();
562         fd2 = drm_open_any();
563         assert_reset_status(fd1, 0, RS_NO_ERROR);
564         assert_reset_status(fd2, 0, RS_NO_ERROR);
565         ctx_guilty = context_create(fd1);
566         ctx_unrelated = context_create(fd2);
567
568         assert_reset_status(fd1, ctx_guilty, RS_NO_ERROR);
569         assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
570
571         h1 = inject_hang(fd1, ctx_guilty);
572         igt_assert(h1 >= 0);
573         gem_sync(fd1, h1);
574         assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
575         assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
576
577         h2 = exec_valid(fd2, ctx_unrelated);
578         igt_assert(h2 >= 0);
579         gem_sync(fd2, h2);
580         assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
581         assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
582         gem_close(fd1, h1);
583         gem_close(fd2, h2);
584
585         igt_assert(context_destroy(fd1, ctx_guilty) == 0);
586         igt_assert(context_destroy(fd2, ctx_unrelated) == 0);
587
588         close(fd1);
589         close(fd2);
590 }
591
592 static int get_reset_count(int fd, int ctx)
593 {
594         int ret;
595         struct local_drm_i915_reset_stats rs;
596
597         ret = gem_reset_stats(fd, ctx, &rs);
598         if (ret)
599                 return ret;
600
601         return rs.reset_count;
602 }
603
604 static void test_double_destroy_pending(void)
605 {
606         int fd, h;
607         uint32_t ctx;
608
609         fd = drm_open_any();
610         igt_assert(fd >= 0);
611         ctx = context_create(fd);
612
613         assert_reset_status(fd, ctx, RS_NO_ERROR);
614
615         h = inject_hang(fd, ctx);
616         igt_assert(h >= 0);
617         igt_assert(context_destroy(fd, ctx) == 0);
618         igt_assert(context_destroy(fd, ctx) == -ENOENT);
619
620         gem_close(fd, h);
621         close(fd);
622 }
623
624 static void test_close_pending(void)
625 {
626         int fd, h;
627
628         fd = drm_open_any();
629         igt_assert(fd >= 0);
630
631         assert_reset_status(fd, 0, RS_NO_ERROR);
632
633         h = inject_hang(fd, 0);
634         igt_assert(h >= 0);
635
636         gem_close(fd, h);
637         close(fd);
638 }
639
640 static void drop_root(void)
641 {
642         igt_assert(getuid() == 0);
643
644         igt_assert(setgid(2) == 0);
645         igt_assert(setuid(2) == 0);
646
647         igt_assert(getgid() == 2);
648         igt_assert(getuid() == 2);
649 }
650
651 static void __test_count(const bool create_ctx)
652 {
653         int fd, h, ctx;
654         long c1, c2;
655
656         fd = drm_open_any();
657         igt_assert(fd >= 0);
658         if (create_ctx)
659                 ctx = context_create(fd);
660         else
661                 ctx = 0;
662
663         assert_reset_status(fd, ctx, RS_NO_ERROR);
664
665         c1 = get_reset_count(fd, ctx);
666         igt_assert(c1 >= 0);
667
668         h = inject_hang(fd, ctx);
669         igt_assert (h >= 0);
670         gem_sync(fd, h);
671
672         assert_reset_status(fd, ctx, RS_BATCH_ACTIVE);
673         c2 = get_reset_count(fd, ctx);
674         igt_assert(c2 >= 0);
675         igt_assert(c2 == (c1 + 1));
676
677         igt_fork(child, 1) {
678                 drop_root();
679
680                 c2 = get_reset_count(fd, ctx);
681
682                 if (ctx == 0)
683                         igt_assert(c2 == -EPERM);
684                 else
685                         igt_assert(c2 == 0);
686         }
687
688         igt_waitchildren();
689
690         gem_close(fd, h);
691
692         if (create_ctx)
693                 context_destroy(fd, ctx);
694
695         close(fd);
696 }
697
698 static void test_count(void)
699 {
700         return __test_count(false);
701 }
702
703 static void test_count_context(void)
704 {
705         return __test_count(true);
706 }
707
708 static void test_global_reset_count(void)
709 {
710         test_count();
711         test_count_context();
712 }
713
714 static int _test_params(int fd, int ctx, uint32_t flags, uint32_t pad)
715 {
716         struct local_drm_i915_reset_stats rs;
717         int ret;
718
719         rs.ctx_id = ctx;
720         rs.flags = flags;
721         rs.reset_count = rand();
722         rs.batch_active = rand();
723         rs.batch_pending = rand();
724         rs.pad = pad;
725
726         do {
727                 ret = ioctl(fd, GET_RESET_STATS_IOCTL, &rs);
728         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
729
730         if (ret < 0)
731                 return -errno;
732
733         return 0;
734 }
735
736 typedef enum { root = 0, user } cap_t;
737
738 static void test_param_ctx(const int fd, const int ctx, const cap_t cap)
739 {
740         const uint32_t bad = rand() + 1;
741
742         if (ctx == 0) {
743                 if (cap == root)
744                         igt_assert(_test_params(fd, ctx, 0, 0) == 0);
745                 else
746                         igt_assert(_test_params(fd, ctx, 0, 0) == -EPERM);
747         }
748
749         igt_assert(_test_params(fd, ctx, 0, bad) == -EINVAL);
750         igt_assert(_test_params(fd, ctx, bad, 0) == -EINVAL);
751         igt_assert(_test_params(fd, ctx, bad, bad) == -EINVAL);
752 }
753
754 static void check_params(const int fd, const int ctx, cap_t cap)
755 {
756         igt_assert(ioctl(fd, GET_RESET_STATS_IOCTL, 0) == -1);
757         igt_assert(_test_params(fd, 0xbadbad, 0, 0) == -ENOENT);
758
759         test_param_ctx(fd, 0, cap);
760         test_param_ctx(fd, ctx, cap);
761 }
762
763 static void _test_param(const int fd, const int ctx)
764 {
765         check_params(fd, ctx, root);
766
767         igt_fork(child, 1) {
768                 check_params(fd, ctx, root);
769
770                 drop_root();
771
772                 check_params(fd, ctx, user);
773         }
774
775         check_params(fd, ctx, root);
776
777         igt_waitchildren();
778 }
779
780 static void test_params(void)
781 {
782         int fd, ctx;
783
784         fd = drm_open_any();
785         igt_assert(fd >= 0);
786         ctx = context_create(fd);
787
788         _test_param(fd, ctx);
789
790         close(fd);
791 }
792
793
794 igt_main
795 {
796         struct local_drm_i915_gem_context_create create;
797         uint32_t devid;
798         int fd;
799         int ret;
800
801         igt_skip_on_simulation();
802
803         igt_fixture {
804                 fd = drm_open_any();
805                 devid = intel_get_drm_devid(fd);
806                 igt_require_f(intel_gen(devid) >= 4,
807                               "Architecture %d too old\n", intel_gen(devid));
808
809                 ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
810                 igt_skip_on_f(ret != 0 && (errno == ENODEV || errno == EINVAL),
811                               "Kernel is too old, or contexts not supported: %s\n",
812                               strerror(errno));
813
814                 assert(igt_debugfs_init(&dfs) == 0);
815
816                 close(fd);
817         }
818
819         igt_subtest("basic-reset-status")
820                 test_rs(4, 1, 0);
821
822         igt_subtest("context-reset-status")
823                 test_rs_ctx(4, 4, 1, 2);
824
825         igt_subtest("ban")
826                 test_ban();
827
828         igt_subtest("ctx-unrelated")
829                 test_nonrelated_hang();
830
831         igt_subtest("global-count")
832                 test_global_reset_count();
833
834         igt_subtest("double-destroy-pending")
835                 test_double_destroy_pending();
836
837         igt_subtest("close-pending")
838                 test_close_pending();
839
840         igt_subtest("params")
841                 test_params();
842 }