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