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