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