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