1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2018 Intel Corporation
5  */
6 
7 #include "../i915_drv.h"
8 
9 #include "../i915_selftest.h"
10 #include "igt_flush_test.h"
11 #include "igt_live_test.h"
12 
13 int igt_live_test_begin(struct igt_live_test *t,
14 			struct drm_i915_private *i915,
15 			const char *func,
16 			const char *name)
17 {
18 	struct intel_engine_cs *engine;
19 	enum intel_engine_id id;
20 	int err;
21 
22 	lockdep_assert_held(&i915->drm.struct_mutex);
23 
24 	t->i915 = i915;
25 	t->func = func;
26 	t->name = name;
27 
28 	err = i915_gem_wait_for_idle(i915,
29 				     I915_WAIT_INTERRUPTIBLE |
30 				     I915_WAIT_LOCKED,
31 				     MAX_SCHEDULE_TIMEOUT);
32 	if (err) {
33 		pr_err("%s(%s): failed to idle before, with err=%d!",
34 		       func, name, err);
35 		return err;
36 	}
37 
38 	t->reset_global = i915_reset_count(&i915->gpu_error);
39 
40 	for_each_engine(engine, i915, id)
41 		t->reset_engine[id] =
42 			i915_reset_engine_count(&i915->gpu_error, engine);
43 
44 	return 0;
45 }
46 
47 int igt_live_test_end(struct igt_live_test *t)
48 {
49 	struct drm_i915_private *i915 = t->i915;
50 	struct intel_engine_cs *engine;
51 	enum intel_engine_id id;
52 
53 	lockdep_assert_held(&i915->drm.struct_mutex);
54 
55 	if (igt_flush_test(i915, I915_WAIT_LOCKED))
56 		return -EIO;
57 
58 	if (t->reset_global != i915_reset_count(&i915->gpu_error)) {
59 		pr_err("%s(%s): GPU was reset %d times!\n",
60 		       t->func, t->name,
61 		       i915_reset_count(&i915->gpu_error) - t->reset_global);
62 		return -EIO;
63 	}
64 
65 	for_each_engine(engine, i915, id) {
66 		if (t->reset_engine[id] ==
67 		    i915_reset_engine_count(&i915->gpu_error, engine))
68 			continue;
69 
70 		pr_err("%s(%s): engine '%s' was reset %d times!\n",
71 		       t->func, t->name, engine->name,
72 		       i915_reset_engine_count(&i915->gpu_error, engine) -
73 		       t->reset_engine[id]);
74 		return -EIO;
75 	}
76 
77 	return 0;
78 }
79