xref: /linux/drivers/gpu/drm/i915/selftests/i915_gem.c (revision 0526b56cbc3c489642bd6a5fe4b718dea7ef0ee8)
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2018 Intel Corporation
5  */
6 
7 #include <linux/random.h>
8 
9 #include "gem/i915_gem_internal.h"
10 #include "gem/i915_gem_pm.h"
11 #include "gem/selftests/igt_gem_utils.h"
12 #include "gem/selftests/mock_context.h"
13 #include "gt/intel_gt.h"
14 #include "gt/intel_gt_pm.h"
15 
16 #include "i915_selftest.h"
17 
18 #include "igt_flush_test.h"
19 #include "mock_drm.h"
20 
21 static int switch_to_context(struct i915_gem_context *ctx)
22 {
23 	struct i915_gem_engines_iter it;
24 	struct intel_context *ce;
25 	int err = 0;
26 
27 	for_each_gem_engine(ce, i915_gem_context_lock_engines(ctx), it) {
28 		struct i915_request *rq;
29 
30 		rq = intel_context_create_request(ce);
31 		if (IS_ERR(rq)) {
32 			err = PTR_ERR(rq);
33 			break;
34 		}
35 
36 		i915_request_add(rq);
37 	}
38 	i915_gem_context_unlock_engines(ctx);
39 
40 	return err;
41 }
42 
43 static void trash_stolen(struct drm_i915_private *i915)
44 {
45 	struct i915_ggtt *ggtt = to_gt(i915)->ggtt;
46 	const u64 slot = ggtt->error_capture.start;
47 	const resource_size_t size = resource_size(&i915->dsm.stolen);
48 	unsigned long page;
49 	u32 prng = 0x12345678;
50 
51 	/* XXX: fsck. needs some more thought... */
52 	if (!i915_ggtt_has_aperture(ggtt))
53 		return;
54 
55 	for (page = 0; page < size; page += PAGE_SIZE) {
56 		const dma_addr_t dma = i915->dsm.stolen.start + page;
57 		u32 __iomem *s;
58 		int x;
59 
60 		ggtt->vm.insert_page(&ggtt->vm, dma, slot, I915_CACHE_NONE, 0);
61 
62 		s = io_mapping_map_atomic_wc(&ggtt->iomap, slot);
63 		for (x = 0; x < PAGE_SIZE / sizeof(u32); x++) {
64 			prng = next_pseudo_random32(prng);
65 			iowrite32(prng, &s[x]);
66 		}
67 		io_mapping_unmap_atomic(s);
68 	}
69 
70 	ggtt->vm.clear_range(&ggtt->vm, slot, PAGE_SIZE);
71 }
72 
73 static void simulate_hibernate(struct drm_i915_private *i915)
74 {
75 	intel_wakeref_t wakeref;
76 
77 	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
78 
79 	/*
80 	 * As a final sting in the tail, invalidate stolen. Under a real S4,
81 	 * stolen is lost and needs to be refilled on resume. However, under
82 	 * CI we merely do S4-device testing (as full S4 is too unreliable
83 	 * for automated testing across a cluster), so to simulate the effect
84 	 * of stolen being trashed across S4, we trash it ourselves.
85 	 */
86 	trash_stolen(i915);
87 
88 	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
89 }
90 
91 static int igt_pm_prepare(struct drm_i915_private *i915)
92 {
93 	i915_gem_suspend(i915);
94 
95 	return 0;
96 }
97 
98 static void igt_pm_suspend(struct drm_i915_private *i915)
99 {
100 	intel_wakeref_t wakeref;
101 
102 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
103 		i915_ggtt_suspend(to_gt(i915)->ggtt);
104 		i915_gem_suspend_late(i915);
105 	}
106 }
107 
108 static void igt_pm_hibernate(struct drm_i915_private *i915)
109 {
110 	intel_wakeref_t wakeref;
111 
112 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
113 		i915_ggtt_suspend(to_gt(i915)->ggtt);
114 
115 		i915_gem_freeze(i915);
116 		i915_gem_freeze_late(i915);
117 	}
118 }
119 
120 static void igt_pm_resume(struct drm_i915_private *i915)
121 {
122 	intel_wakeref_t wakeref;
123 
124 	/*
125 	 * Both suspend and hibernate follow the same wakeup path and assume
126 	 * that runtime-pm just works.
127 	 */
128 	with_intel_runtime_pm(&i915->runtime_pm, wakeref) {
129 		i915_ggtt_resume(to_gt(i915)->ggtt);
130 		if (GRAPHICS_VER(i915) >= 8)
131 			setup_private_pat(to_gt(i915));
132 		i915_gem_resume(i915);
133 	}
134 }
135 
136 static int igt_gem_suspend(void *arg)
137 {
138 	struct drm_i915_private *i915 = arg;
139 	struct i915_gem_context *ctx;
140 	struct file *file;
141 	int err;
142 
143 	file = mock_file(i915);
144 	if (IS_ERR(file))
145 		return PTR_ERR(file);
146 
147 	err = -ENOMEM;
148 	ctx = live_context(i915, file);
149 	if (!IS_ERR(ctx))
150 		err = switch_to_context(ctx);
151 	if (err)
152 		goto out;
153 
154 	err = igt_pm_prepare(i915);
155 	if (err)
156 		goto out;
157 
158 	igt_pm_suspend(i915);
159 
160 	/* Here be dragons! Note that with S3RST any S3 may become S4! */
161 	simulate_hibernate(i915);
162 
163 	igt_pm_resume(i915);
164 
165 	err = switch_to_context(ctx);
166 out:
167 	fput(file);
168 	return err;
169 }
170 
171 static int igt_gem_hibernate(void *arg)
172 {
173 	struct drm_i915_private *i915 = arg;
174 	struct i915_gem_context *ctx;
175 	struct file *file;
176 	int err;
177 
178 	file = mock_file(i915);
179 	if (IS_ERR(file))
180 		return PTR_ERR(file);
181 
182 	err = -ENOMEM;
183 	ctx = live_context(i915, file);
184 	if (!IS_ERR(ctx))
185 		err = switch_to_context(ctx);
186 	if (err)
187 		goto out;
188 
189 	err = igt_pm_prepare(i915);
190 	if (err)
191 		goto out;
192 
193 	igt_pm_hibernate(i915);
194 
195 	/* Here be dragons! */
196 	simulate_hibernate(i915);
197 
198 	igt_pm_resume(i915);
199 
200 	err = switch_to_context(ctx);
201 out:
202 	fput(file);
203 	return err;
204 }
205 
206 static int igt_gem_ww_ctx(void *arg)
207 {
208 	struct drm_i915_private *i915 = arg;
209 	struct drm_i915_gem_object *obj, *obj2;
210 	struct i915_gem_ww_ctx ww;
211 	int err = 0;
212 
213 	obj = i915_gem_object_create_internal(i915, PAGE_SIZE);
214 	if (IS_ERR(obj))
215 		return PTR_ERR(obj);
216 
217 	obj2 = i915_gem_object_create_internal(i915, PAGE_SIZE);
218 	if (IS_ERR(obj2)) {
219 		err = PTR_ERR(obj2);
220 		goto put1;
221 	}
222 
223 	i915_gem_ww_ctx_init(&ww, true);
224 retry:
225 	/* Lock the objects, twice for good measure (-EALREADY handling) */
226 	err = i915_gem_object_lock(obj, &ww);
227 	if (!err)
228 		err = i915_gem_object_lock_interruptible(obj, &ww);
229 	if (!err)
230 		err = i915_gem_object_lock_interruptible(obj2, &ww);
231 	if (!err)
232 		err = i915_gem_object_lock(obj2, &ww);
233 
234 	if (err == -EDEADLK) {
235 		err = i915_gem_ww_ctx_backoff(&ww);
236 		if (!err)
237 			goto retry;
238 	}
239 	i915_gem_ww_ctx_fini(&ww);
240 	i915_gem_object_put(obj2);
241 put1:
242 	i915_gem_object_put(obj);
243 	return err;
244 }
245 
246 int i915_gem_live_selftests(struct drm_i915_private *i915)
247 {
248 	static const struct i915_subtest tests[] = {
249 		SUBTEST(igt_gem_suspend),
250 		SUBTEST(igt_gem_hibernate),
251 		SUBTEST(igt_gem_ww_ctx),
252 	};
253 
254 	if (intel_gt_is_wedged(to_gt(i915)))
255 		return 0;
256 
257 	return i915_live_subtests(tests, i915);
258 }
259