tests/gem_reset_stats: end hanging batch properly
[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 #define _GNU_SOURCE
29 #include <stdbool.h>
30 #include <unistd.h>
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34 #include <fcntl.h>
35 #include <inttypes.h>
36 #include <errno.h>
37 #include <sys/stat.h>
38 #include <sys/ioctl.h>
39 #include <sys/mman.h>
40 #include <time.h>
41 #include <signal.h>
42
43 #include "ioctl_wrappers.h"
44 #include "drmtest.h"
45 #include "igt_debugfs.h"
46 #include "intel_chipset.h"
47 #include "intel_io.h"
48 #include "igt_aux.h"
49
50 #define RS_NO_ERROR      0
51 #define RS_BATCH_ACTIVE  (1 << 0)
52 #define RS_BATCH_PENDING (1 << 1)
53 #define RS_UNKNOWN       (1 << 2)
54
55 static uint32_t devid;
56 static bool hw_contexts;
57
58 struct local_drm_i915_reset_stats {
59         __u32 ctx_id;
60         __u32 flags;
61         __u32 reset_count;
62         __u32 batch_active;
63         __u32 batch_pending;
64         __u32 pad;
65 };
66
67 struct local_drm_i915_gem_context_create {
68         __u32 ctx_id;
69         __u32 pad;
70 };
71
72 struct local_drm_i915_gem_context_destroy {
73         __u32 ctx_id;
74         __u32 pad;
75 };
76
77 #define MAX_FD 32
78
79 #define CONTEXT_CREATE_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2d, struct local_drm_i915_gem_context_create)
80 #define CONTEXT_DESTROY_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x2e, struct local_drm_i915_gem_context_destroy)
81 #define GET_RESET_STATS_IOCTL DRM_IOWR(DRM_COMMAND_BASE + 0x32, struct local_drm_i915_reset_stats)
82
83 #define LOCAL_I915_EXEC_VEBOX   (4 << 0)
84
85 struct target_ring;
86
87 static bool gem_has_render(int fd)
88 {
89         return true;
90 }
91
92 static bool has_context(const struct target_ring *ring);
93
94 static const struct target_ring {
95         uint32_t exec;
96         bool (*present)(int fd);
97         bool (*contexts)(const struct target_ring *ring);
98         const char *name;
99 } rings[] = {
100         { I915_EXEC_RENDER, gem_has_render, has_context, "render" },
101         { I915_EXEC_BLT, gem_has_blt, has_context, "blt" },
102         { I915_EXEC_BSD, gem_has_bsd, has_context, "bsd" },
103         { LOCAL_I915_EXEC_VEBOX, gem_has_vebox, has_context, "vebox" },
104 };
105
106 static bool has_context(const struct target_ring *ring)
107 {
108         if (!hw_contexts)
109                 return false;
110
111         if(ring->exec == I915_EXEC_RENDER)
112                 return true;
113
114         return false;
115 }
116
117 #define NUM_RINGS (sizeof(rings)/sizeof(struct target_ring))
118
119 static const struct target_ring *current_ring;
120
121 static uint32_t context_create(int fd)
122 {
123         struct local_drm_i915_gem_context_create create;
124         int ret;
125
126         create.ctx_id = rand();
127         create.pad = rand();
128
129         ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
130         igt_assert(ret == 0);
131
132         return create.ctx_id;
133 }
134
135 static int context_destroy(int fd, uint32_t ctx_id)
136 {
137         int ret;
138         struct local_drm_i915_gem_context_destroy destroy;
139
140         destroy.ctx_id = ctx_id;
141         destroy.pad = rand();
142
143         ret = drmIoctl(fd, CONTEXT_DESTROY_IOCTL, &destroy);
144         if (ret != 0)
145                 return -errno;
146
147         return 0;
148 }
149
150 static int gem_reset_stats(int fd, int ctx_id,
151                            struct local_drm_i915_reset_stats *rs)
152 {
153         int ret;
154
155         rs->ctx_id = ctx_id;
156         rs->flags = 0;
157         rs->reset_count = rand();
158         rs->batch_active = rand();
159         rs->batch_pending = rand();
160         rs->pad = 0;
161
162         do {
163                 ret = ioctl(fd, GET_RESET_STATS_IOCTL, rs);
164         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
165
166         if (ret < 0)
167                 return -errno;
168
169         return 0;
170 }
171
172 static int gem_reset_status(int fd, int ctx_id)
173 {
174         int ret;
175         struct local_drm_i915_reset_stats rs;
176
177         ret = gem_reset_stats(fd, ctx_id, &rs);
178         if (ret)
179                 return ret;
180
181         if (rs.batch_active)
182                 return RS_BATCH_ACTIVE;
183         if (rs.batch_pending)
184                 return RS_BATCH_PENDING;
185
186         return RS_NO_ERROR;
187 }
188
189 static int gem_exec(int fd, struct drm_i915_gem_execbuffer2 *execbuf)
190 {
191         int ret;
192
193         ret = ioctl(fd,
194                     DRM_IOCTL_I915_GEM_EXECBUFFER2,
195                     execbuf);
196
197         if (ret < 0)
198                 return -errno;
199
200         return 0;
201 }
202
203 static int exec_valid_ring(int fd, int ctx, int ring)
204 {
205         struct drm_i915_gem_execbuffer2 execbuf;
206         struct drm_i915_gem_exec_object2 exec;
207         int ret;
208
209         uint32_t buf[2] = { MI_BATCH_BUFFER_END, 0 };
210
211         exec.handle = gem_create(fd, 4096);
212         gem_write(fd, exec.handle, 0, buf, sizeof(buf));
213         exec.relocation_count = 0;
214         exec.relocs_ptr = 0;
215         exec.alignment = 0;
216         exec.offset = 0;
217         exec.flags = 0;
218         exec.rsvd1 = 0;
219         exec.rsvd2 = 0;
220
221         execbuf.buffers_ptr = (uintptr_t)&exec;
222         execbuf.buffer_count = 1;
223         execbuf.batch_start_offset = 0;
224         execbuf.batch_len = sizeof(buf);
225         execbuf.cliprects_ptr = 0;
226         execbuf.num_cliprects = 0;
227         execbuf.DR1 = 0;
228         execbuf.DR4 = 0;
229         execbuf.flags = ring;
230         i915_execbuffer2_set_context_id(execbuf, ctx);
231         execbuf.rsvd2 = 0;
232
233         ret = gem_exec(fd, &execbuf);
234         if (ret < 0)
235                 return ret;
236
237         return exec.handle;
238 }
239
240 static int exec_valid(int fd, int ctx)
241 {
242         return exec_valid_ring(fd, ctx, current_ring->exec);
243 }
244
245 static void stop_rings(const int mask)
246 {
247         int fd;
248         char buf[80];
249
250         igt_assert((mask & ~((1 << NUM_RINGS) - 1)) == 0);
251         igt_assert(snprintf(buf, sizeof(buf), "0x%02x", mask) == 4);
252         fd = igt_debugfs_open("i915_ring_stop", O_WRONLY);
253         igt_assert(fd >= 0);
254
255         igt_assert(write(fd, buf, 4) == 4);
256         close(fd);
257 }
258
259 #define BUFSIZE (4 * 1024)
260 #define ITEMS   (BUFSIZE >> 2)
261
262 static int ring_to_mask(int ring)
263 {
264         for (unsigned i = 0; i < NUM_RINGS; i++) {
265                 const struct target_ring *r = &rings[i];
266
267                 if (r->exec == ring)
268                         return (1 << i);
269         }
270
271         igt_assert(0);
272
273         return -1;
274 }
275
276 static int inject_hang_ring(int fd, int ctx, int ring)
277 {
278         struct drm_i915_gem_execbuffer2 execbuf;
279         struct drm_i915_gem_exec_object2 exec;
280         uint64_t gtt_off;
281         uint32_t *buf;
282         int roff, i;
283         unsigned cmd_len = 2;
284
285         srandom(time(NULL));
286
287         if (intel_gen(devid) >= 8)
288                 cmd_len = 3;
289
290         buf = malloc(BUFSIZE);
291         igt_assert(buf != NULL);
292
293         buf[0] = MI_BATCH_BUFFER_END;
294         buf[1] = MI_NOOP;
295
296         exec.handle = gem_create(fd, BUFSIZE);
297         gem_write(fd, exec.handle, 0, buf, BUFSIZE);
298         exec.relocation_count = 0;
299         exec.relocs_ptr = 0;
300         exec.alignment = 0;
301         exec.offset = 0;
302         exec.flags = 0;
303         exec.rsvd1 = 0;
304         exec.rsvd2 = 0;
305
306         execbuf.buffers_ptr = (uintptr_t)&exec;
307         execbuf.buffer_count = 1;
308         execbuf.batch_start_offset = 0;
309         execbuf.batch_len = BUFSIZE;
310         execbuf.cliprects_ptr = 0;
311         execbuf.num_cliprects = 0;
312         execbuf.DR1 = 0;
313         execbuf.DR4 = 0;
314         execbuf.flags = ring;
315         i915_execbuffer2_set_context_id(execbuf, ctx);
316         execbuf.rsvd2 = 0;
317
318         igt_assert(gem_exec(fd, &execbuf) == 0);
319
320         gtt_off = exec.offset;
321
322         for (i = 0; i < ITEMS; i++)
323                 buf[i] = MI_NOOP;
324
325         roff = random() % (ITEMS - cmd_len);
326         buf[roff] = MI_BATCH_BUFFER_START | (cmd_len - 2);
327         buf[roff + 1] = (gtt_off & 0xfffffffc) + (roff << 2);
328         if (cmd_len == 3)
329                 buf[roff + 2] = (gtt_off & 0xffffffff00000000ull) >> 32;
330
331         buf[roff + cmd_len] = MI_BATCH_BUFFER_END;
332
333 #ifdef VERBOSE
334         printf("loop injected at 0x%lx (off 0x%x, bo_start 0x%lx, bo_end 0x%lx)\n",
335                (long unsigned int)((roff << 2) + gtt_off),
336                roff << 2, (long unsigned int)gtt_off,
337                (long unsigned int)(gtt_off + BUFSIZE - 1));
338 #endif
339         gem_write(fd, exec.handle, 0, buf, BUFSIZE);
340
341         exec.relocation_count = 0;
342         exec.relocs_ptr = 0;
343         exec.alignment = 0;
344         exec.offset = 0;
345         exec.flags = 0;
346         exec.rsvd1 = 0;
347         exec.rsvd2 = 0;
348
349         execbuf.buffers_ptr = (uintptr_t)&exec;
350         execbuf.buffer_count = 1;
351         execbuf.batch_start_offset = 0;
352         execbuf.batch_len = BUFSIZE;
353         execbuf.cliprects_ptr = 0;
354         execbuf.num_cliprects = 0;
355         execbuf.DR1 = 0;
356         execbuf.DR4 = 0;
357         execbuf.flags = ring;
358         i915_execbuffer2_set_context_id(execbuf, ctx);
359         execbuf.rsvd2 = 0;
360
361         igt_assert(gem_exec(fd, &execbuf) == 0);
362
363         igt_assert(gtt_off == exec.offset);
364
365         free(buf);
366
367         stop_rings(ring_to_mask(ring));
368
369         return exec.handle;
370 }
371
372 static int inject_hang(int fd, int ctx)
373 {
374         return inject_hang_ring(fd, ctx, current_ring->exec);
375 }
376
377 static int _assert_reset_status(int fd, int ctx, int status)
378 {
379         int rs;
380
381         rs = gem_reset_status(fd, ctx);
382         if (rs < 0) {
383                 printf("reset status for %d ctx %d returned %d\n",
384                        fd, ctx, rs);
385                 return rs;
386         }
387
388         if (rs != status) {
389                 printf("%d:%d reset status %d differs from assumed %d\n",
390                        fd, ctx, rs, status);
391
392                 return 1;
393         }
394
395         return 0;
396 }
397
398 #define assert_reset_status(fd, ctx, status) \
399         igt_assert(_assert_reset_status(fd, ctx, status) == 0)
400
401 static void test_rs(int num_fds, int hang_index, int rs_assumed_no_hang)
402 {
403         int i;
404         int fd[MAX_FD];
405         int h[MAX_FD];
406
407         igt_assert (num_fds <= MAX_FD);
408         igt_assert (hang_index < MAX_FD);
409
410         for (i = 0; i < num_fds; i++) {
411                 fd[i] = drm_open_any();
412                 igt_assert(fd[i]);
413         }
414
415         for (i = 0; i < num_fds; i++)
416                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
417
418         for (i = 0; i < num_fds; i++) {
419                 if (i == hang_index)
420                         h[i] = inject_hang(fd[i], 0);
421                 else
422                         h[i] = exec_valid(fd[i], 0);
423         }
424
425         gem_sync(fd[num_fds - 1], h[num_fds - 1]);
426
427         for (i = 0; i < num_fds; i++) {
428                 if (hang_index < 0) {
429                         assert_reset_status(fd[i], 0, rs_assumed_no_hang);
430                         continue;
431                 }
432
433                 if (i < hang_index)
434                         assert_reset_status(fd[i], 0, RS_NO_ERROR);
435                 if (i == hang_index)
436                         assert_reset_status(fd[i], 0, RS_BATCH_ACTIVE);
437                 if (i > hang_index)
438                         assert_reset_status(fd[i], 0, RS_BATCH_PENDING);
439         }
440
441         for (i = 0; i < num_fds; i++) {
442                 gem_close(fd[i], h[i]);
443                 close(fd[i]);
444         }
445 }
446
447 #define MAX_CTX 100
448 static void test_rs_ctx(int num_fds, int num_ctx, int hang_index,
449                         int hang_context)
450 {
451         int i, j;
452         int fd[MAX_FD];
453         int h[MAX_FD][MAX_CTX];
454         int ctx[MAX_FD][MAX_CTX];
455
456         igt_assert (num_fds <= MAX_FD);
457         igt_assert (hang_index < MAX_FD);
458
459         igt_assert (num_ctx <= MAX_CTX);
460         igt_assert (hang_context < MAX_CTX);
461
462         test_rs(num_fds, -1, RS_NO_ERROR);
463
464         for (i = 0; i < num_fds; i++) {
465                 fd[i] = drm_open_any();
466                 igt_assert(fd[i]);
467                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
468
469                 for (j = 0; j < num_ctx; j++) {
470                         ctx[i][j] = context_create(fd[i]);
471
472                 }
473
474                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
475         }
476
477         for (i = 0; i < num_fds; i++) {
478
479                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
480
481                 for (j = 0; j < num_ctx; j++)
482                         assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
483
484                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
485         }
486
487         for (i = 0; i < num_fds; i++) {
488                 for (j = 0; j < num_ctx; j++) {
489                         if (i == hang_index && j == hang_context)
490                                 h[i][j] = inject_hang(fd[i], ctx[i][j]);
491                         else
492                                 h[i][j] = exec_valid(fd[i], ctx[i][j]);
493                 }
494         }
495
496         gem_sync(fd[num_fds - 1], ctx[num_fds - 1][num_ctx - 1]);
497
498         for (i = 0; i < num_fds; i++)
499                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
500
501         for (i = 0; i < num_fds; i++) {
502                 for (j = 0; j < num_ctx; j++) {
503                         if (i < hang_index)
504                                 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
505                         if (i == hang_index && j < hang_context)
506                                 assert_reset_status(fd[i], ctx[i][j], RS_NO_ERROR);
507                         if (i == hang_index && j == hang_context)
508                                 assert_reset_status(fd[i], ctx[i][j],
509                                                     RS_BATCH_ACTIVE);
510                         if (i == hang_index && j > hang_context)
511                                 assert_reset_status(fd[i], ctx[i][j],
512                                                     RS_BATCH_PENDING);
513                         if (i > hang_index)
514                                 assert_reset_status(fd[i], ctx[i][j],
515                                                     RS_BATCH_PENDING);
516                 }
517         }
518
519         for (i = 0; i < num_fds; i++) {
520                 for (j = 0; j < num_ctx; j++) {
521                         gem_close(fd[i], h[i][j]);
522                         igt_assert(context_destroy(fd[i], ctx[i][j]) == 0);
523                 }
524
525                 assert_reset_status(fd[i], 0, RS_NO_ERROR);
526
527                 close(fd[i]);
528         }
529 }
530
531 static void test_ban(void)
532 {
533         int h1,h2,h3,h4,h5,h6,h7;
534         int fd_bad, fd_good;
535         int retry = 10;
536         int active_count = 0, pending_count = 0;
537         struct local_drm_i915_reset_stats rs_bad, rs_good;
538
539         fd_bad = drm_open_any();
540         igt_assert(fd_bad >= 0);
541
542         fd_good = drm_open_any();
543         igt_assert(fd_good >= 0);
544
545         assert_reset_status(fd_bad, 0, RS_NO_ERROR);
546         assert_reset_status(fd_good, 0, RS_NO_ERROR);
547
548         h1 = exec_valid(fd_bad, 0);
549         igt_assert(h1 >= 0);
550         h5 = exec_valid(fd_good, 0);
551         igt_assert(h5 >= 0);
552
553         assert_reset_status(fd_bad, 0, RS_NO_ERROR);
554         assert_reset_status(fd_good, 0, RS_NO_ERROR);
555
556         h2 = inject_hang(fd_bad, 0);
557         igt_assert(h2 >= 0);
558         active_count++;
559         /* Second hang will be pending for this */
560         pending_count++;
561
562         h6 = exec_valid(fd_good, 0);
563         h7 = exec_valid(fd_good, 0);
564
565         while (retry--) {
566                 h3 = inject_hang(fd_bad, 0);
567                 igt_assert(h3 >= 0);
568                 gem_sync(fd_bad, h3);
569                 active_count++;
570                 /* This second hand will count as pending */
571                 assert_reset_status(fd_bad, 0, RS_BATCH_ACTIVE);
572
573                 h4 = exec_valid(fd_bad, 0);
574                 if (h4 == -EIO) {
575                         gem_close(fd_bad, h3);
576                         break;
577                 }
578
579                 /* Should not happen often but sometimes hang is declared too slow
580                  * due to our way of faking hang using loop */
581
582                 igt_assert(h4 >= 0);
583                 gem_close(fd_bad, h3);
584                 gem_close(fd_bad, h4);
585
586                 printf("retrying for ban (%d)\n", retry);
587         }
588
589         igt_assert(h4 == -EIO);
590         assert_reset_status(fd_bad, 0, RS_BATCH_ACTIVE);
591
592         gem_sync(fd_good, h7);
593         assert_reset_status(fd_good, 0, RS_BATCH_PENDING);
594
595         igt_assert(gem_reset_stats(fd_good, 0, &rs_good) == 0);
596         igt_assert(gem_reset_stats(fd_bad, 0, &rs_bad) == 0);
597
598         igt_assert(rs_bad.batch_active == active_count);
599         igt_assert(rs_bad.batch_pending == pending_count);
600         igt_assert(rs_good.batch_active == 0);
601         igt_assert(rs_good.batch_pending == 2);
602
603         gem_close(fd_bad, h1);
604         gem_close(fd_bad, h2);
605         gem_close(fd_good, h6);
606         gem_close(fd_good, h7);
607
608         h1 = exec_valid(fd_good, 0);
609         igt_assert(h1 >= 0);
610         gem_close(fd_good, h1);
611
612         close(fd_bad);
613         close(fd_good);
614
615         igt_assert(gem_reset_status(fd_bad, 0) < 0);
616         igt_assert(gem_reset_status(fd_good, 0) < 0);
617 }
618
619 static void test_ban_ctx(void)
620 {
621         int h1,h2,h3,h4,h5,h6,h7;
622         int ctx_good, ctx_bad;
623         int fd;
624         int retry = 10;
625         int active_count = 0, pending_count = 0;
626         struct local_drm_i915_reset_stats rs_bad, rs_good;
627
628         fd = drm_open_any();
629         igt_assert(fd >= 0);
630
631         assert_reset_status(fd, 0, RS_NO_ERROR);
632
633         ctx_good = context_create(fd);
634         ctx_bad = context_create(fd);
635
636         assert_reset_status(fd, 0, RS_NO_ERROR);
637         assert_reset_status(fd, ctx_good, RS_NO_ERROR);
638         assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
639
640         h1 = exec_valid(fd, ctx_bad);
641         igt_assert(h1 >= 0);
642         h5 = exec_valid(fd, ctx_good);
643         igt_assert(h5 >= 0);
644
645         assert_reset_status(fd, ctx_good, RS_NO_ERROR);
646         assert_reset_status(fd, ctx_bad, RS_NO_ERROR);
647
648         h2 = inject_hang(fd, ctx_bad);
649         igt_assert(h2 >= 0);
650         active_count++;
651         /* Second hang will be pending for this */
652         pending_count++;
653
654         h6 = exec_valid(fd, ctx_good);
655         h7 = exec_valid(fd, ctx_good);
656
657         while (retry--) {
658                 h3 = inject_hang(fd, ctx_bad);
659                 igt_assert(h3 >= 0);
660                 gem_sync(fd, h3);
661                 active_count++;
662                 /* This second hand will count as pending */
663                 assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
664
665                 h4 = exec_valid(fd, ctx_bad);
666                 if (h4 == -EIO) {
667                         gem_close(fd, h3);
668                         break;
669                 }
670
671                 /* Should not happen often but sometimes hang is declared too slow
672                  * due to our way of faking hang using loop */
673
674                 igt_assert(h4 >= 0);
675                 gem_close(fd, h3);
676                 gem_close(fd, h4);
677
678                 printf("retrying for ban (%d)\n", retry);
679         }
680
681         igt_assert(h4 == -EIO);
682         assert_reset_status(fd, ctx_bad, RS_BATCH_ACTIVE);
683
684         gem_sync(fd, h7);
685         assert_reset_status(fd, ctx_good, RS_BATCH_PENDING);
686
687         igt_assert(gem_reset_stats(fd, ctx_good, &rs_good) == 0);
688         igt_assert(gem_reset_stats(fd, ctx_bad, &rs_bad) == 0);
689
690         igt_assert(rs_bad.batch_active == active_count);
691         igt_assert(rs_bad.batch_pending == pending_count);
692         igt_assert(rs_good.batch_active == 0);
693         igt_assert(rs_good.batch_pending == 2);
694
695         gem_close(fd, h1);
696         gem_close(fd, h2);
697         gem_close(fd, h6);
698         gem_close(fd, h7);
699
700         h1 = exec_valid(fd, ctx_good);
701         igt_assert(h1 >= 0);
702         gem_close(fd, h1);
703
704         igt_assert(context_destroy(fd, ctx_good) == 0);
705         igt_assert(context_destroy(fd, ctx_bad) == 0);
706         igt_assert(gem_reset_status(fd, ctx_good) < 0);
707         igt_assert(gem_reset_status(fd, ctx_bad) < 0);
708         igt_assert(exec_valid(fd, ctx_good) < 0);
709         igt_assert(exec_valid(fd, ctx_bad) < 0);
710
711         close(fd);
712 }
713
714 static void test_unrelated_ctx(void)
715 {
716         int h1,h2;
717         int fd1,fd2;
718         int ctx_guilty, ctx_unrelated;
719
720         fd1 = drm_open_any();
721         fd2 = drm_open_any();
722         assert_reset_status(fd1, 0, RS_NO_ERROR);
723         assert_reset_status(fd2, 0, RS_NO_ERROR);
724         ctx_guilty = context_create(fd1);
725         ctx_unrelated = context_create(fd2);
726
727         assert_reset_status(fd1, ctx_guilty, RS_NO_ERROR);
728         assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
729
730         h1 = inject_hang(fd1, ctx_guilty);
731         igt_assert(h1 >= 0);
732         gem_sync(fd1, h1);
733         assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
734         assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
735
736         h2 = exec_valid(fd2, ctx_unrelated);
737         igt_assert(h2 >= 0);
738         gem_sync(fd2, h2);
739         assert_reset_status(fd1, ctx_guilty, RS_BATCH_ACTIVE);
740         assert_reset_status(fd2, ctx_unrelated, RS_NO_ERROR);
741         gem_close(fd1, h1);
742         gem_close(fd2, h2);
743
744         igt_assert(context_destroy(fd1, ctx_guilty) == 0);
745         igt_assert(context_destroy(fd2, ctx_unrelated) == 0);
746
747         close(fd1);
748         close(fd2);
749 }
750
751 static int get_reset_count(int fd, int ctx)
752 {
753         int ret;
754         struct local_drm_i915_reset_stats rs;
755
756         ret = gem_reset_stats(fd, ctx, &rs);
757         if (ret)
758                 return ret;
759
760         return rs.reset_count;
761 }
762
763 static void test_close_pending_ctx(void)
764 {
765         int fd, h;
766         uint32_t ctx;
767
768         fd = drm_open_any();
769         igt_assert(fd >= 0);
770         ctx = context_create(fd);
771
772         assert_reset_status(fd, ctx, RS_NO_ERROR);
773
774         h = inject_hang(fd, ctx);
775         igt_assert(h >= 0);
776         igt_assert(context_destroy(fd, ctx) == 0);
777         igt_assert(context_destroy(fd, ctx) == -ENOENT);
778
779         gem_close(fd, h);
780         close(fd);
781 }
782
783 static void test_close_pending(void)
784 {
785         int fd, h;
786
787         fd = drm_open_any();
788         igt_assert(fd >= 0);
789
790         assert_reset_status(fd, 0, RS_NO_ERROR);
791
792         h = inject_hang(fd, 0);
793         igt_assert(h >= 0);
794
795         gem_close(fd, h);
796         close(fd);
797 }
798
799 static void exec_noop_on_each_ring(int fd, const bool reverse)
800 {
801         uint32_t batch[2] = {MI_BATCH_BUFFER_END, 0};
802         uint32_t handle;
803         struct drm_i915_gem_execbuffer2 execbuf;
804         struct drm_i915_gem_exec_object2 exec[1];
805
806         handle = gem_create(fd, 4096);
807         gem_write(fd, handle, 0, batch, sizeof(batch));
808
809         exec[0].handle = handle;
810         exec[0].relocation_count = 0;
811         exec[0].relocs_ptr = 0;
812         exec[0].alignment = 0;
813         exec[0].offset = 0;
814         exec[0].flags = 0;
815         exec[0].rsvd1 = 0;
816         exec[0].rsvd2 = 0;
817
818         execbuf.buffers_ptr = (uintptr_t)exec;
819         execbuf.buffer_count = 1;
820         execbuf.batch_start_offset = 0;
821         execbuf.batch_len = 8;
822         execbuf.cliprects_ptr = 0;
823         execbuf.num_cliprects = 0;
824         execbuf.DR1 = 0;
825         execbuf.DR4 = 0;
826         execbuf.flags = 0;
827         i915_execbuffer2_set_context_id(execbuf, 0);
828         execbuf.rsvd2 = 0;
829
830         for (unsigned i = 0; i < NUM_RINGS; i++) {
831                 const struct target_ring *ring;
832
833                 ring = reverse ? &rings[NUM_RINGS - 1 - i] : &rings[i];
834
835                 if (ring->present(fd)) {
836                         execbuf.flags = ring->exec;
837                         do_ioctl(fd, DRM_IOCTL_I915_GEM_EXECBUFFER2, &execbuf);
838                 }
839         }
840
841         gem_sync(fd, handle);
842         gem_close(fd, handle);
843 }
844
845 static void test_close_pending_fork(const bool reverse)
846 {
847         int pid;
848         int fd, h;
849
850         fd = drm_open_any();
851         igt_assert(fd >= 0);
852
853         assert_reset_status(fd, 0, RS_NO_ERROR);
854
855         h = inject_hang(fd, 0);
856         igt_assert(h >= 0);
857
858         sleep(1);
859
860         /* Avoid helpers as we need to kill the child
861          * without any extra signal handling on behalf of
862          * lib/drmtest.c
863          */
864         pid = fork();
865         if (pid == 0) {
866                 const int fd2 = drm_open_any();
867                 igt_assert(fd2 >= 0);
868
869                 /* The crucial component is that we schedule the same noop batch
870                  * on each ring. This exercises batch_obj reference counting,
871                  * when gpu is reset and ring lists are cleared.
872                  */
873                 exec_noop_on_each_ring(fd2, reverse);
874
875                 close(fd2);
876                 return;
877         } else {
878                 igt_assert(pid > 0);
879                 sleep(1);
880
881                 /* Kill the child to reduce refcounts on
882                    batch_objs */
883                 kill(pid, SIGKILL);
884         }
885
886         gem_close(fd, h);
887         close(fd);
888
889         /* Then we just wait on hang to happen */
890         fd = drm_open_any();
891         igt_assert(fd >= 0);
892
893         h = exec_valid(fd, 0);
894         igt_assert(h >= 0);
895
896         gem_sync(fd, h);
897         gem_close(fd, h);
898         close(fd);
899 }
900
901 static void test_reset_count(const bool create_ctx)
902 {
903         int fd, h, ctx;
904         long c1, c2;
905
906         fd = drm_open_any();
907         igt_assert(fd >= 0);
908         if (create_ctx)
909                 ctx = context_create(fd);
910         else
911                 ctx = 0;
912
913         assert_reset_status(fd, ctx, RS_NO_ERROR);
914
915         c1 = get_reset_count(fd, ctx);
916         igt_assert(c1 >= 0);
917
918         h = inject_hang(fd, ctx);
919         igt_assert (h >= 0);
920         gem_sync(fd, h);
921
922         assert_reset_status(fd, ctx, RS_BATCH_ACTIVE);
923         c2 = get_reset_count(fd, ctx);
924         igt_assert(c2 >= 0);
925         igt_assert(c2 == (c1 + 1));
926
927         igt_fork(child, 1) {
928                 igt_drop_root();
929
930                 c2 = get_reset_count(fd, ctx);
931
932                 if (ctx == 0)
933                         igt_assert(c2 == -EPERM);
934                 else
935                         igt_assert(c2 == 0);
936         }
937
938         igt_waitchildren();
939
940         gem_close(fd, h);
941
942         if (create_ctx)
943                 context_destroy(fd, ctx);
944
945         close(fd);
946 }
947
948 static int _test_params(int fd, int ctx, uint32_t flags, uint32_t pad)
949 {
950         struct local_drm_i915_reset_stats rs;
951         int ret;
952
953         rs.ctx_id = ctx;
954         rs.flags = flags;
955         rs.reset_count = rand();
956         rs.batch_active = rand();
957         rs.batch_pending = rand();
958         rs.pad = pad;
959
960         do {
961                 ret = ioctl(fd, GET_RESET_STATS_IOCTL, &rs);
962         } while (ret == -1 && (errno == EINTR || errno == EAGAIN));
963
964         if (ret < 0)
965                 return -errno;
966
967         return 0;
968 }
969
970 typedef enum { root = 0, user } cap_t;
971
972 static void _check_param_ctx(const int fd, const int ctx, const cap_t cap)
973 {
974         const uint32_t bad = rand() + 1;
975
976         if (ctx == 0) {
977                 if (cap == root)
978                         igt_assert(_test_params(fd, ctx, 0, 0) == 0);
979                 else
980                         igt_assert(_test_params(fd, ctx, 0, 0) == -EPERM);
981         }
982
983         igt_assert(_test_params(fd, ctx, 0, bad) == -EINVAL);
984         igt_assert(_test_params(fd, ctx, bad, 0) == -EINVAL);
985         igt_assert(_test_params(fd, ctx, bad, bad) == -EINVAL);
986 }
987
988 static void check_params(const int fd, const int ctx, cap_t cap)
989 {
990         igt_assert(ioctl(fd, GET_RESET_STATS_IOCTL, 0) == -1);
991         igt_assert(_test_params(fd, 0xbadbad, 0, 0) == -ENOENT);
992
993         _check_param_ctx(fd, ctx, cap);
994 }
995
996 static void _test_param(const int fd, const int ctx)
997 {
998         check_params(fd, ctx, root);
999
1000         igt_fork(child, 1) {
1001                 check_params(fd, ctx, root);
1002
1003                 igt_drop_root();
1004
1005                 check_params(fd, ctx, user);
1006         }
1007
1008         check_params(fd, ctx, root);
1009
1010         igt_waitchildren();
1011 }
1012
1013 static void test_params_ctx(void)
1014 {
1015         int fd, ctx;
1016
1017         fd = drm_open_any();
1018         igt_assert(fd >= 0);
1019         ctx = context_create(fd);
1020
1021         _test_param(fd, ctx);
1022
1023         close(fd);
1024 }
1025
1026 static void test_params(void)
1027 {
1028         int fd;
1029
1030         fd = drm_open_any();
1031         igt_assert(fd >= 0);
1032
1033         _test_param(fd, 0);
1034
1035         close(fd);
1036
1037 }
1038
1039 static bool gem_has_hw_contexts(int fd)
1040 {
1041         struct local_drm_i915_gem_context_create create;
1042         int ret;
1043
1044         memset(&create, 0, sizeof(create));
1045         ret = drmIoctl(fd, CONTEXT_CREATE_IOCTL, &create);
1046
1047         if (ret == 0) {
1048                 drmIoctl(fd, CONTEXT_DESTROY_IOCTL, &create);
1049                 return true;
1050         }
1051
1052         return false;
1053 }
1054
1055 static bool gem_has_reset_stats(int fd)
1056 {
1057         struct local_drm_i915_reset_stats rs;
1058         int ret;
1059
1060         /* Carefully set flags and pad to zero, otherwise
1061            we get -EINVAL
1062         */
1063         memset(&rs, 0, sizeof(rs));
1064
1065         ret = drmIoctl(fd, GET_RESET_STATS_IOCTL, &rs);
1066         if (ret == 0)
1067                 return true;
1068
1069         /* If we get EPERM, we have support but did not
1070            have CAP_SYSADM */
1071         if (ret == -1 && errno == EPERM)
1072                 return true;
1073
1074         return false;
1075 }
1076
1077 #define RING_HAS_CONTEXTS (current_ring->contexts(current_ring))
1078 #define RUN_CTX_TEST(...) do { igt_skip_on(RING_HAS_CONTEXTS == false); __VA_ARGS__; } while (0)
1079
1080 static int fd;
1081
1082 igt_main
1083 {
1084         igt_skip_on_simulation();
1085
1086         igt_fixture {
1087                 bool has_reset_stats;
1088                 fd = drm_open_any();
1089                 devid = intel_get_drm_devid(fd);
1090
1091                 hw_contexts = gem_has_hw_contexts(fd);
1092                 has_reset_stats = gem_has_reset_stats(fd);
1093
1094                 igt_require_f(has_reset_stats,
1095                               "No reset stats ioctl support. Too old kernel?\n");
1096         }
1097
1098         igt_subtest("params")
1099                 test_params();
1100
1101         for (int i = 0; i < NUM_RINGS; i++) {
1102                 const char *name;
1103
1104                 current_ring = &rings[i];
1105                 name = current_ring->name;
1106
1107                 igt_fixture
1108                         gem_require_ring(fd, current_ring->exec);
1109
1110                 igt_fixture
1111                         igt_require_f(intel_gen(devid) >= 4,
1112                                       "gen %d doesn't support reset\n", intel_gen(devid));
1113
1114                 igt_subtest_f("params-ctx-%s", name)
1115                         RUN_CTX_TEST(test_params_ctx());
1116
1117                 igt_subtest_f("reset-stats-%s", name)
1118                         test_rs(4, 1, 0);
1119
1120                 igt_subtest_f("reset-stats-ctx-%s", name)
1121                         RUN_CTX_TEST(test_rs_ctx(4, 4, 1, 2));
1122
1123                 igt_subtest_f("ban-%s", name)
1124                         test_ban();
1125
1126                 igt_subtest_f("ban-ctx-%s", name)
1127                         RUN_CTX_TEST(test_ban_ctx());
1128
1129                 igt_subtest_f("reset-count-%s", name)
1130                         test_reset_count(false);
1131
1132                 igt_subtest_f("reset-count-ctx-%s", name)
1133                         RUN_CTX_TEST(test_reset_count(true));
1134
1135                 igt_subtest_f("unrelated-ctx-%s", name)
1136                         RUN_CTX_TEST(test_unrelated_ctx());
1137
1138                 igt_subtest_f("close-pending-%s", name) {
1139                         test_close_pending();
1140                         gem_quiescent_gpu(fd);
1141                 }
1142
1143                 igt_subtest_f("close-pending-ctx-%s", name) {
1144                         RUN_CTX_TEST(test_close_pending_ctx());
1145                         gem_quiescent_gpu(fd);
1146                 }
1147
1148                 igt_subtest_f("close-pending-fork-%s", name) {
1149                         test_close_pending_fork(true);
1150                         test_close_pending_fork(false);
1151                 }
1152         }
1153
1154         igt_fixture
1155                 close(fd);
1156 }