Merge tag 'block-5.11-2021-01-10' of git://git.kernel.dk/linux-block
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / gt / selftest_ring_submission.c
blob3350e7c995bccff92751c8b448f81976d0c6a4c3
1 // SPDX-License-Identifier: MIT
2 /*
3 * Copyright © 2020 Intel Corporation
4 */
6 #include "intel_engine_pm.h"
7 #include "selftests/igt_flush_test.h"
9 static struct i915_vma *create_wally(struct intel_engine_cs *engine)
11 struct drm_i915_gem_object *obj;
12 struct i915_vma *vma;
13 u32 *cs;
14 int err;
16 obj = i915_gem_object_create_internal(engine->i915, 4096);
17 if (IS_ERR(obj))
18 return ERR_CAST(obj);
20 vma = i915_vma_instance(obj, engine->gt->vm, NULL);
21 if (IS_ERR(vma)) {
22 i915_gem_object_put(obj);
23 return vma;
26 err = i915_vma_pin(vma, 0, 0, PIN_USER | PIN_HIGH);
27 if (err) {
28 i915_gem_object_put(obj);
29 return ERR_PTR(err);
32 err = i915_vma_sync(vma);
33 if (err) {
34 i915_gem_object_put(obj);
35 return ERR_PTR(err);
38 cs = i915_gem_object_pin_map(obj, I915_MAP_WC);
39 if (IS_ERR(cs)) {
40 i915_gem_object_put(obj);
41 return ERR_CAST(cs);
44 if (INTEL_GEN(engine->i915) >= 6) {
45 *cs++ = MI_STORE_DWORD_IMM_GEN4;
46 *cs++ = 0;
47 } else if (INTEL_GEN(engine->i915) >= 4) {
48 *cs++ = MI_STORE_DWORD_IMM_GEN4 | MI_USE_GGTT;
49 *cs++ = 0;
50 } else {
51 *cs++ = MI_STORE_DWORD_IMM | MI_MEM_VIRTUAL;
53 *cs++ = vma->node.start + 4000;
54 *cs++ = STACK_MAGIC;
56 *cs++ = MI_BATCH_BUFFER_END;
58 i915_gem_object_flush_map(obj);
59 i915_gem_object_unpin_map(obj);
61 vma->private = intel_context_create(engine); /* dummy residuals */
62 if (IS_ERR(vma->private)) {
63 vma = ERR_CAST(vma->private);
64 i915_gem_object_put(obj);
67 return vma;
70 static int context_sync(struct intel_context *ce)
72 struct i915_request *rq;
73 int err = 0;
75 rq = intel_context_create_request(ce);
76 if (IS_ERR(rq))
77 return PTR_ERR(rq);
79 i915_request_get(rq);
80 i915_request_add(rq);
82 if (i915_request_wait(rq, 0, HZ / 5) < 0)
83 err = -ETIME;
84 i915_request_put(rq);
86 return err;
89 static int new_context_sync(struct intel_engine_cs *engine)
91 struct intel_context *ce;
92 int err;
94 ce = intel_context_create(engine);
95 if (IS_ERR(ce))
96 return PTR_ERR(ce);
98 err = context_sync(ce);
99 intel_context_put(ce);
101 return err;
104 static int mixed_contexts_sync(struct intel_engine_cs *engine, u32 *result)
106 int pass;
107 int err;
109 for (pass = 0; pass < 2; pass++) {
110 WRITE_ONCE(*result, 0);
111 err = context_sync(engine->kernel_context);
112 if (err || READ_ONCE(*result)) {
113 if (!err) {
114 pr_err("pass[%d] wa_bb emitted for the kernel context\n",
115 pass);
116 err = -EINVAL;
118 return err;
121 WRITE_ONCE(*result, 0);
122 err = new_context_sync(engine);
123 if (READ_ONCE(*result) != STACK_MAGIC) {
124 if (!err) {
125 pr_err("pass[%d] wa_bb *NOT* emitted after the kernel context\n",
126 pass);
127 err = -EINVAL;
129 return err;
132 WRITE_ONCE(*result, 0);
133 err = new_context_sync(engine);
134 if (READ_ONCE(*result) != STACK_MAGIC) {
135 if (!err) {
136 pr_err("pass[%d] wa_bb *NOT* emitted for the user context switch\n",
137 pass);
138 err = -EINVAL;
140 return err;
144 return 0;
147 static int double_context_sync_00(struct intel_engine_cs *engine, u32 *result)
149 struct intel_context *ce;
150 int err, i;
152 ce = intel_context_create(engine);
153 if (IS_ERR(ce))
154 return PTR_ERR(ce);
156 for (i = 0; i < 2; i++) {
157 WRITE_ONCE(*result, 0);
158 err = context_sync(ce);
159 if (err)
160 break;
162 intel_context_put(ce);
163 if (err)
164 return err;
166 if (READ_ONCE(*result)) {
167 pr_err("wa_bb emitted between the same user context\n");
168 return -EINVAL;
171 return 0;
174 static int kernel_context_sync_00(struct intel_engine_cs *engine, u32 *result)
176 struct intel_context *ce;
177 int err, i;
179 ce = intel_context_create(engine);
180 if (IS_ERR(ce))
181 return PTR_ERR(ce);
183 for (i = 0; i < 2; i++) {
184 WRITE_ONCE(*result, 0);
185 err = context_sync(ce);
186 if (err)
187 break;
189 err = context_sync(engine->kernel_context);
190 if (err)
191 break;
193 intel_context_put(ce);
194 if (err)
195 return err;
197 if (READ_ONCE(*result)) {
198 pr_err("wa_bb emitted between the same user context [with intervening kernel]\n");
199 return -EINVAL;
202 return 0;
205 static int __live_ctx_switch_wa(struct intel_engine_cs *engine)
207 struct i915_vma *bb;
208 u32 *result;
209 int err;
211 bb = create_wally(engine);
212 if (IS_ERR(bb))
213 return PTR_ERR(bb);
215 result = i915_gem_object_pin_map(bb->obj, I915_MAP_WC);
216 if (IS_ERR(result)) {
217 intel_context_put(bb->private);
218 i915_vma_unpin_and_release(&bb, 0);
219 return PTR_ERR(result);
221 result += 1000;
223 engine->wa_ctx.vma = bb;
225 err = mixed_contexts_sync(engine, result);
226 if (err)
227 goto out;
229 err = double_context_sync_00(engine, result);
230 if (err)
231 goto out;
233 err = kernel_context_sync_00(engine, result);
234 if (err)
235 goto out;
237 out:
238 intel_context_put(engine->wa_ctx.vma->private);
239 i915_vma_unpin_and_release(&engine->wa_ctx.vma, I915_VMA_RELEASE_MAP);
240 return err;
243 static int live_ctx_switch_wa(void *arg)
245 struct intel_gt *gt = arg;
246 struct intel_engine_cs *engine;
247 enum intel_engine_id id;
250 * Exercise the inter-context wa batch.
252 * Between each user context we run a wa batch, and since it may
253 * have implications for user visible state, we have to check that
254 * we do actually execute it.
256 * The trick we use is to replace the normal wa batch with a custom
257 * one that writes to a marker within it, and we can then look for
258 * that marker to confirm if the batch was run when we expect it,
259 * and equally important it was wasn't run when we don't!
262 for_each_engine(engine, gt, id) {
263 struct i915_vma *saved_wa;
264 int err;
266 if (!intel_engine_can_store_dword(engine))
267 continue;
269 if (IS_GEN_RANGE(gt->i915, 4, 5))
270 continue; /* MI_STORE_DWORD is privileged! */
272 saved_wa = fetch_and_zero(&engine->wa_ctx.vma);
274 intel_engine_pm_get(engine);
275 err = __live_ctx_switch_wa(engine);
276 intel_engine_pm_put(engine);
277 if (igt_flush_test(gt->i915))
278 err = -EIO;
280 engine->wa_ctx.vma = saved_wa;
281 if (err)
282 return err;
285 return 0;
288 int intel_ring_submission_live_selftests(struct drm_i915_private *i915)
290 static const struct i915_subtest tests[] = {
291 SUBTEST(live_ctx_switch_wa),
294 if (HAS_EXECLISTS(i915))
295 return 0;
297 return intel_gt_live_subtests(tests, &i915->gt);