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