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