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