1 /* $NetBSD: intel_reset.c,v 1.6 2021/12/19 12:32:15 riastradh Exp $ */
2
3 /*
4 * SPDX-License-Identifier: MIT
5 *
6 * Copyright © 2008-2018 Intel Corporation
7 */
8
9 #include <sys/cdefs.h>
10 __KERNEL_RCSID(0, "$NetBSD: intel_reset.c,v 1.6 2021/12/19 12:32:15 riastradh Exp $");
11
12 #include <linux/sched/mm.h>
13 #include <linux/stop_machine.h>
14
15 #include "display/intel_display_types.h"
16 #include "display/intel_overlay.h"
17
18 #include "gem/i915_gem_context.h"
19
20 #include "i915_drv.h"
21 #include "i915_gpu_error.h"
22 #include "i915_irq.h"
23 #include "intel_engine_pm.h"
24 #include "intel_gt.h"
25 #include "intel_gt_pm.h"
26 #include "intel_reset.h"
27
28 #include "uc/intel_guc.h"
29 #include "uc/intel_guc_submission.h"
30
31 #include <linux/nbsd-namespace.h>
32
33 #define RESET_MAX_RETRIES 3
34
35 /* XXX How to handle concurrent GGTT updates using tiling registers? */
36 #define RESET_UNDER_STOP_MACHINE 0
37
rmw_set_fw(struct intel_uncore * uncore,i915_reg_t reg,u32 set)38 static void rmw_set_fw(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
39 {
40 intel_uncore_rmw_fw(uncore, reg, 0, set);
41 }
42
rmw_clear_fw(struct intel_uncore * uncore,i915_reg_t reg,u32 clr)43 static void rmw_clear_fw(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
44 {
45 intel_uncore_rmw_fw(uncore, reg, clr, 0);
46 }
47
engine_skip_context(struct i915_request * rq)48 static void engine_skip_context(struct i915_request *rq)
49 {
50 struct intel_engine_cs *engine = rq->engine;
51 struct intel_context *hung_ctx = rq->context;
52
53 if (!i915_request_is_active(rq))
54 return;
55
56 lockdep_assert_held(&engine->active.lock);
57 list_for_each_entry_continue(rq, &engine->active.requests, sched.link)
58 if (rq->context == hung_ctx)
59 i915_request_skip(rq, -EIO);
60 }
61
client_mark_guilty(struct i915_gem_context * ctx,bool banned)62 static void client_mark_guilty(struct i915_gem_context *ctx, bool banned)
63 {
64 struct drm_i915_file_private *file_priv = ctx->file_priv;
65 unsigned long prev_hang;
66 unsigned int score;
67
68 if (IS_ERR_OR_NULL(file_priv))
69 return;
70
71 score = 0;
72 if (banned)
73 score = I915_CLIENT_SCORE_CONTEXT_BAN;
74
75 prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
76 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
77 score += I915_CLIENT_SCORE_HANG_FAST;
78
79 if (score) {
80 atomic_add(score, &file_priv->ban_score);
81
82 DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n",
83 ctx->name, score,
84 atomic_read(&file_priv->ban_score));
85 }
86 }
87
mark_guilty(struct i915_request * rq)88 static bool mark_guilty(struct i915_request *rq)
89 {
90 struct i915_gem_context *ctx;
91 unsigned long prev_hang;
92 bool banned;
93 int i;
94
95 rcu_read_lock();
96 ctx = rcu_dereference(rq->context->gem_context);
97 if (ctx && !kref_get_unless_zero(&ctx->ref))
98 ctx = NULL;
99 rcu_read_unlock();
100 if (!ctx)
101 return false;
102
103 if (i915_gem_context_is_closed(ctx)) {
104 intel_context_set_banned(rq->context);
105 banned = true;
106 goto out;
107 }
108
109 atomic_inc(&ctx->guilty_count);
110
111 /* Cool contexts are too cool to be banned! (Used for reset testing.) */
112 if (!i915_gem_context_is_bannable(ctx)) {
113 banned = false;
114 goto out;
115 }
116
117 dev_notice(ctx->i915->drm.dev,
118 "%s context reset due to GPU hang\n",
119 ctx->name);
120
121 /* Record the timestamp for the last N hangs */
122 prev_hang = ctx->hang_timestamp[0];
123 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++)
124 ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1];
125 ctx->hang_timestamp[i] = jiffies;
126
127 /* If we have hung N+1 times in rapid succession, we ban the context! */
128 banned = !i915_gem_context_is_recoverable(ctx);
129 if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES))
130 banned = true;
131 if (banned) {
132 DRM_DEBUG_DRIVER("context %s: guilty %d, banned\n",
133 ctx->name, atomic_read(&ctx->guilty_count));
134 intel_context_set_banned(rq->context);
135 }
136
137 client_mark_guilty(ctx, banned);
138
139 out:
140 i915_gem_context_put(ctx);
141 return banned;
142 }
143
mark_innocent(struct i915_request * rq)144 static void mark_innocent(struct i915_request *rq)
145 {
146 struct i915_gem_context *ctx;
147
148 rcu_read_lock();
149 ctx = rcu_dereference(rq->context->gem_context);
150 if (ctx)
151 atomic_inc(&ctx->active_count);
152 rcu_read_unlock();
153 }
154
__i915_request_reset(struct i915_request * rq,bool guilty)155 void __i915_request_reset(struct i915_request *rq, bool guilty)
156 {
157 RQ_TRACE(rq, "guilty? %s\n", yesno(guilty));
158
159 GEM_BUG_ON(i915_request_completed(rq));
160
161 rcu_read_lock(); /* protect the GEM context */
162 if (guilty) {
163 i915_request_skip(rq, -EIO);
164 if (mark_guilty(rq))
165 engine_skip_context(rq);
166 } else {
167 dma_fence_set_error(&rq->fence, -EAGAIN);
168 mark_innocent(rq);
169 }
170 rcu_read_unlock();
171 }
172
i915_in_reset(struct pci_dev * pdev)173 static bool i915_in_reset(struct pci_dev *pdev)
174 {
175 u8 gdrst;
176
177 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
178 return gdrst & GRDOM_RESET_STATUS;
179 }
180
i915_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)181 static int i915_do_reset(struct intel_gt *gt,
182 intel_engine_mask_t engine_mask,
183 unsigned int retry)
184 {
185 struct pci_dev *pdev = gt->i915->drm.pdev;
186 int err;
187
188 /* Assert reset for at least 20 usec, and wait for acknowledgement. */
189 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
190 udelay(50);
191 err = wait_for_atomic(i915_in_reset(pdev), 50);
192
193 /* Clear the reset request. */
194 pci_write_config_byte(pdev, I915_GDRST, 0);
195 udelay(50);
196 if (!err)
197 err = wait_for_atomic(!i915_in_reset(pdev), 50);
198
199 return err;
200 }
201
g4x_reset_complete(struct pci_dev * pdev)202 static bool g4x_reset_complete(struct pci_dev *pdev)
203 {
204 u8 gdrst;
205
206 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
207 return (gdrst & GRDOM_RESET_ENABLE) == 0;
208 }
209
g33_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)210 static int g33_do_reset(struct intel_gt *gt,
211 intel_engine_mask_t engine_mask,
212 unsigned int retry)
213 {
214 struct pci_dev *pdev = gt->i915->drm.pdev;
215
216 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
217 return wait_for_atomic(g4x_reset_complete(pdev), 50);
218 }
219
g4x_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)220 static int g4x_do_reset(struct intel_gt *gt,
221 intel_engine_mask_t engine_mask,
222 unsigned int retry)
223 {
224 struct pci_dev *pdev = gt->i915->drm.pdev;
225 struct intel_uncore *uncore = gt->uncore;
226 int ret;
227
228 /* WaVcpClkGateDisableForMediaReset:ctg,elk */
229 rmw_set_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
230 intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
231
232 pci_write_config_byte(pdev, I915_GDRST,
233 GRDOM_MEDIA | GRDOM_RESET_ENABLE);
234 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
235 if (ret) {
236 DRM_DEBUG_DRIVER("Wait for media reset failed\n");
237 goto out;
238 }
239
240 pci_write_config_byte(pdev, I915_GDRST,
241 GRDOM_RENDER | GRDOM_RESET_ENABLE);
242 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
243 if (ret) {
244 DRM_DEBUG_DRIVER("Wait for render reset failed\n");
245 goto out;
246 }
247
248 out:
249 pci_write_config_byte(pdev, I915_GDRST, 0);
250
251 rmw_clear_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
252 intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
253
254 return ret;
255 }
256
ilk_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)257 static int ilk_do_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask,
258 unsigned int retry)
259 {
260 struct intel_uncore *uncore = gt->uncore;
261 int ret;
262
263 intel_uncore_write_fw(uncore, ILK_GDSR,
264 ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
265 ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
266 ILK_GRDOM_RESET_ENABLE, 0,
267 5000, 0,
268 NULL);
269 if (ret) {
270 DRM_DEBUG_DRIVER("Wait for render reset failed\n");
271 goto out;
272 }
273
274 intel_uncore_write_fw(uncore, ILK_GDSR,
275 ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
276 ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
277 ILK_GRDOM_RESET_ENABLE, 0,
278 5000, 0,
279 NULL);
280 if (ret) {
281 DRM_DEBUG_DRIVER("Wait for media reset failed\n");
282 goto out;
283 }
284
285 out:
286 intel_uncore_write_fw(uncore, ILK_GDSR, 0);
287 intel_uncore_posting_read_fw(uncore, ILK_GDSR);
288 return ret;
289 }
290
291 /* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
gen6_hw_domain_reset(struct intel_gt * gt,u32 hw_domain_mask)292 static int gen6_hw_domain_reset(struct intel_gt *gt, u32 hw_domain_mask)
293 {
294 struct intel_uncore *uncore = gt->uncore;
295 int err;
296
297 /*
298 * GEN6_GDRST is not in the gt power well, no need to check
299 * for fifo space for the write or forcewake the chip for
300 * the read
301 */
302 intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask);
303
304 /* Wait for the device to ack the reset requests */
305 err = __intel_wait_for_register_fw(uncore,
306 GEN6_GDRST, hw_domain_mask, 0,
307 500, 0,
308 NULL);
309 if (err)
310 DRM_DEBUG_DRIVER("Wait for 0x%08x engines reset failed\n",
311 hw_domain_mask);
312
313 return err;
314 }
315
gen6_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)316 static int gen6_reset_engines(struct intel_gt *gt,
317 intel_engine_mask_t engine_mask,
318 unsigned int retry)
319 {
320 static const u32 hw_engine_mask[] = {
321 [RCS0] = GEN6_GRDOM_RENDER,
322 [BCS0] = GEN6_GRDOM_BLT,
323 [VCS0] = GEN6_GRDOM_MEDIA,
324 [VCS1] = GEN8_GRDOM_MEDIA2,
325 [VECS0] = GEN6_GRDOM_VECS,
326 };
327 struct intel_engine_cs *engine;
328 u32 hw_mask;
329
330 if (engine_mask == ALL_ENGINES) {
331 hw_mask = GEN6_GRDOM_FULL;
332 } else {
333 intel_engine_mask_t tmp;
334
335 hw_mask = 0;
336 for_each_engine_masked(engine, gt, engine_mask, tmp) {
337 GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
338 hw_mask |= hw_engine_mask[engine->id];
339 }
340 }
341
342 return gen6_hw_domain_reset(gt, hw_mask);
343 }
344
gen11_lock_sfc(struct intel_engine_cs * engine,u32 * hw_mask)345 static int gen11_lock_sfc(struct intel_engine_cs *engine, u32 *hw_mask)
346 {
347 struct intel_uncore *uncore = engine->uncore;
348 u8 vdbox_sfc_access = RUNTIME_INFO(engine->i915)->vdbox_sfc_access;
349 i915_reg_t sfc_forced_lock, sfc_forced_lock_ack;
350 u32 sfc_forced_lock_bit, sfc_forced_lock_ack_bit;
351 i915_reg_t sfc_usage;
352 u32 sfc_usage_bit;
353 u32 sfc_reset_bit;
354 int ret;
355
356 switch (engine->class) {
357 case VIDEO_DECODE_CLASS:
358 if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
359 return 0;
360
361 sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
362 sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
363
364 sfc_forced_lock_ack = GEN11_VCS_SFC_LOCK_STATUS(engine);
365 sfc_forced_lock_ack_bit = GEN11_VCS_SFC_LOCK_ACK_BIT;
366
367 sfc_usage = GEN11_VCS_SFC_LOCK_STATUS(engine);
368 sfc_usage_bit = GEN11_VCS_SFC_USAGE_BIT;
369 sfc_reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
370 break;
371
372 case VIDEO_ENHANCEMENT_CLASS:
373 sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
374 sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
375
376 sfc_forced_lock_ack = GEN11_VECS_SFC_LOCK_ACK(engine);
377 sfc_forced_lock_ack_bit = GEN11_VECS_SFC_LOCK_ACK_BIT;
378
379 sfc_usage = GEN11_VECS_SFC_USAGE(engine);
380 sfc_usage_bit = GEN11_VECS_SFC_USAGE_BIT;
381 sfc_reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
382 break;
383
384 default:
385 return 0;
386 }
387
388 /*
389 * If the engine is using a SFC, tell the engine that a software reset
390 * is going to happen. The engine will then try to force lock the SFC.
391 * If SFC ends up being locked to the engine we want to reset, we have
392 * to reset it as well (we will unlock it once the reset sequence is
393 * completed).
394 */
395 if (!(intel_uncore_read_fw(uncore, sfc_usage) & sfc_usage_bit))
396 return 0;
397
398 rmw_set_fw(uncore, sfc_forced_lock, sfc_forced_lock_bit);
399
400 ret = __intel_wait_for_register_fw(uncore,
401 sfc_forced_lock_ack,
402 sfc_forced_lock_ack_bit,
403 sfc_forced_lock_ack_bit,
404 1000, 0, NULL);
405
406 /* Was the SFC released while we were trying to lock it? */
407 if (!(intel_uncore_read_fw(uncore, sfc_usage) & sfc_usage_bit))
408 return 0;
409
410 if (ret) {
411 DRM_DEBUG_DRIVER("Wait for SFC forced lock ack failed\n");
412 return ret;
413 }
414
415 *hw_mask |= sfc_reset_bit;
416 return 0;
417 }
418
gen11_unlock_sfc(struct intel_engine_cs * engine)419 static void gen11_unlock_sfc(struct intel_engine_cs *engine)
420 {
421 struct intel_uncore *uncore = engine->uncore;
422 u8 vdbox_sfc_access = RUNTIME_INFO(engine->i915)->vdbox_sfc_access;
423 i915_reg_t sfc_forced_lock;
424 u32 sfc_forced_lock_bit;
425
426 switch (engine->class) {
427 case VIDEO_DECODE_CLASS:
428 if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
429 return;
430
431 sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
432 sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
433 break;
434
435 case VIDEO_ENHANCEMENT_CLASS:
436 sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
437 sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
438 break;
439
440 default:
441 return;
442 }
443
444 rmw_clear_fw(uncore, sfc_forced_lock, sfc_forced_lock_bit);
445 }
446
gen11_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)447 static int gen11_reset_engines(struct intel_gt *gt,
448 intel_engine_mask_t engine_mask,
449 unsigned int retry)
450 {
451 static const u32 hw_engine_mask[] = {
452 [RCS0] = GEN11_GRDOM_RENDER,
453 [BCS0] = GEN11_GRDOM_BLT,
454 [VCS0] = GEN11_GRDOM_MEDIA,
455 [VCS1] = GEN11_GRDOM_MEDIA2,
456 [VCS2] = GEN11_GRDOM_MEDIA3,
457 [VCS3] = GEN11_GRDOM_MEDIA4,
458 [VECS0] = GEN11_GRDOM_VECS,
459 [VECS1] = GEN11_GRDOM_VECS2,
460 };
461 struct intel_engine_cs *engine;
462 intel_engine_mask_t tmp;
463 u32 hw_mask;
464 int ret;
465
466 if (engine_mask == ALL_ENGINES) {
467 hw_mask = GEN11_GRDOM_FULL;
468 } else {
469 hw_mask = 0;
470 for_each_engine_masked(engine, gt, engine_mask, tmp) {
471 GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
472 hw_mask |= hw_engine_mask[engine->id];
473 ret = gen11_lock_sfc(engine, &hw_mask);
474 if (ret)
475 goto sfc_unlock;
476 }
477 }
478
479 ret = gen6_hw_domain_reset(gt, hw_mask);
480
481 sfc_unlock:
482 /*
483 * We unlock the SFC based on the lock status and not the result of
484 * gen11_lock_sfc to make sure that we clean properly if something
485 * wrong happened during the lock (e.g. lock acquired after timeout
486 * expiration).
487 */
488 if (engine_mask != ALL_ENGINES)
489 for_each_engine_masked(engine, gt, engine_mask, tmp)
490 gen11_unlock_sfc(engine);
491
492 return ret;
493 }
494
gen8_engine_reset_prepare(struct intel_engine_cs * engine)495 static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
496 {
497 struct intel_uncore *uncore = engine->uncore;
498 const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
499 u32 request, mask, ack;
500 int ret;
501
502 ack = intel_uncore_read_fw(uncore, reg);
503 if (ack & RESET_CTL_CAT_ERROR) {
504 /*
505 * For catastrophic errors, ready-for-reset sequence
506 * needs to be bypassed: HAS#396813
507 */
508 request = RESET_CTL_CAT_ERROR;
509 mask = RESET_CTL_CAT_ERROR;
510
511 /* Catastrophic errors need to be cleared by HW */
512 ack = 0;
513 } else if (!(ack & RESET_CTL_READY_TO_RESET)) {
514 request = RESET_CTL_REQUEST_RESET;
515 mask = RESET_CTL_READY_TO_RESET;
516 ack = RESET_CTL_READY_TO_RESET;
517 } else {
518 return 0;
519 }
520
521 intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
522 ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
523 700, 0, NULL);
524 if (ret)
525 DRM_ERROR("%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
526 engine->name, request,
527 intel_uncore_read_fw(uncore, reg));
528
529 return ret;
530 }
531
gen8_engine_reset_cancel(struct intel_engine_cs * engine)532 static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
533 {
534 intel_uncore_write_fw(engine->uncore,
535 RING_RESET_CTL(engine->mmio_base),
536 _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
537 }
538
gen8_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)539 static int gen8_reset_engines(struct intel_gt *gt,
540 intel_engine_mask_t engine_mask,
541 unsigned int retry)
542 {
543 struct intel_engine_cs *engine;
544 const bool reset_non_ready = retry >= 1;
545 intel_engine_mask_t tmp;
546 int ret;
547
548 for_each_engine_masked(engine, gt, engine_mask, tmp) {
549 ret = gen8_engine_reset_prepare(engine);
550 if (ret && !reset_non_ready)
551 goto skip_reset;
552
553 /*
554 * If this is not the first failed attempt to prepare,
555 * we decide to proceed anyway.
556 *
557 * By doing so we risk context corruption and with
558 * some gens (kbl), possible system hang if reset
559 * happens during active bb execution.
560 *
561 * We rather take context corruption instead of
562 * failed reset with a wedged driver/gpu. And
563 * active bb execution case should be covered by
564 * stop_engines() we have before the reset.
565 */
566 }
567
568 if (INTEL_GEN(gt->i915) >= 11)
569 ret = gen11_reset_engines(gt, engine_mask, retry);
570 else
571 ret = gen6_reset_engines(gt, engine_mask, retry);
572
573 skip_reset:
574 for_each_engine_masked(engine, gt, engine_mask, tmp)
575 gen8_engine_reset_cancel(engine);
576
577 return ret;
578 }
579
mock_reset(struct intel_gt * gt,intel_engine_mask_t mask,unsigned int retry)580 static int mock_reset(struct intel_gt *gt,
581 intel_engine_mask_t mask,
582 unsigned int retry)
583 {
584 return 0;
585 }
586
587 typedef int (*reset_func)(struct intel_gt *,
588 intel_engine_mask_t engine_mask,
589 unsigned int retry);
590
intel_get_gpu_reset(const struct intel_gt * gt)591 static reset_func intel_get_gpu_reset(const struct intel_gt *gt)
592 {
593 struct drm_i915_private *i915 = gt->i915;
594
595 if (is_mock_gt(gt))
596 return mock_reset;
597 else if (INTEL_GEN(i915) >= 8)
598 return gen8_reset_engines;
599 else if (INTEL_GEN(i915) >= 6)
600 return gen6_reset_engines;
601 else if (INTEL_GEN(i915) >= 5)
602 return ilk_do_reset;
603 else if (IS_G4X(i915))
604 return g4x_do_reset;
605 else if (IS_G33(i915) || IS_PINEVIEW(i915))
606 return g33_do_reset;
607 else if (INTEL_GEN(i915) >= 3)
608 return i915_do_reset;
609 else
610 return NULL;
611 }
612
__intel_gt_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask)613 int __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask)
614 {
615 const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
616 reset_func reset;
617 int ret = -ETIMEDOUT;
618 int retry;
619
620 reset = intel_get_gpu_reset(gt);
621 if (!reset)
622 return -ENODEV;
623
624 /*
625 * If the power well sleeps during the reset, the reset
626 * request may be dropped and never completes (causing -EIO).
627 */
628 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
629 for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
630 GT_TRACE(gt, "engine_mask=%x\n", engine_mask);
631 preempt_disable();
632 ret = reset(gt, engine_mask, retry);
633 preempt_enable();
634 }
635 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
636
637 return ret;
638 }
639
intel_has_gpu_reset(const struct intel_gt * gt)640 bool intel_has_gpu_reset(const struct intel_gt *gt)
641 {
642 if (!i915_modparams.reset)
643 return NULL;
644
645 return intel_get_gpu_reset(gt);
646 }
647
intel_has_reset_engine(const struct intel_gt * gt)648 bool intel_has_reset_engine(const struct intel_gt *gt)
649 {
650 if (i915_modparams.reset < 2)
651 return false;
652
653 return INTEL_INFO(gt->i915)->has_reset_engine;
654 }
655
intel_reset_guc(struct intel_gt * gt)656 int intel_reset_guc(struct intel_gt *gt)
657 {
658 u32 guc_domain =
659 INTEL_GEN(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
660 int ret;
661
662 GEM_BUG_ON(!HAS_GT_UC(gt->i915));
663
664 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
665 ret = gen6_hw_domain_reset(gt, guc_domain);
666 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
667
668 return ret;
669 }
670
671 /*
672 * Ensure irq handler finishes, and not run again.
673 * Also return the active request so that we only search for it once.
674 */
reset_prepare_engine(struct intel_engine_cs * engine)675 static void reset_prepare_engine(struct intel_engine_cs *engine)
676 {
677 /*
678 * During the reset sequence, we must prevent the engine from
679 * entering RC6. As the context state is undefined until we restart
680 * the engine, if it does enter RC6 during the reset, the state
681 * written to the powercontext is undefined and so we may lose
682 * GPU state upon resume, i.e. fail to restart after a reset.
683 */
684 intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
685 if (engine->reset.prepare)
686 engine->reset.prepare(engine);
687 }
688
revoke_mmaps(struct intel_gt * gt)689 static void revoke_mmaps(struct intel_gt *gt)
690 {
691 int i;
692
693 for (i = 0; i < gt->ggtt->num_fences; i++) {
694 struct drm_vma_offset_node *node;
695 struct i915_vma *vma;
696 u64 vma_offset;
697
698 vma = READ_ONCE(gt->ggtt->fence_regs[i].vma);
699 if (!vma)
700 continue;
701
702 if (!i915_vma_has_userfault(vma))
703 continue;
704
705 GEM_BUG_ON(vma->fence != >->ggtt->fence_regs[i]);
706
707 if (!vma->mmo)
708 continue;
709
710 node = &vma->mmo->vma_node;
711 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
712
713 #ifdef __NetBSD__
714 __USE(vma_offset);
715 __USE(node);
716 paddr_t pa = gt->i915->ggtt.gmadr.start + vma->node.start;
717 vsize_t npgs = vma->size >> PAGE_SHIFT;
718 while (npgs --> 0)
719 pmap_pv_protect(pa + (npgs << PAGE_SHIFT),
720 VM_PROT_NONE);
721 #else
722 unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
723 drm_vma_node_offset_addr(node) + vma_offset,
724 vma->size,
725 1);
726 #endif
727 }
728 }
729
reset_prepare(struct intel_gt * gt)730 static intel_engine_mask_t reset_prepare(struct intel_gt *gt)
731 {
732 struct intel_engine_cs *engine;
733 intel_engine_mask_t awake = 0;
734 enum intel_engine_id id;
735
736 for_each_engine(engine, gt, id) {
737 if (intel_engine_pm_get_if_awake(engine))
738 awake |= engine->mask;
739 reset_prepare_engine(engine);
740 }
741
742 intel_uc_reset_prepare(>->uc);
743
744 return awake;
745 }
746
gt_revoke(struct intel_gt * gt)747 static void gt_revoke(struct intel_gt *gt)
748 {
749 revoke_mmaps(gt);
750 }
751
gt_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask)752 static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
753 {
754 struct intel_engine_cs *engine;
755 enum intel_engine_id id;
756 int err;
757
758 /*
759 * Everything depends on having the GTT running, so we need to start
760 * there.
761 */
762 err = i915_ggtt_enable_hw(gt->i915);
763 if (err)
764 return err;
765
766 for_each_engine(engine, gt, id)
767 __intel_engine_reset(engine, stalled_mask & engine->mask);
768
769 i915_gem_restore_fences(gt->ggtt);
770
771 return err;
772 }
773
reset_finish_engine(struct intel_engine_cs * engine)774 static void reset_finish_engine(struct intel_engine_cs *engine)
775 {
776 if (engine->reset.finish)
777 engine->reset.finish(engine);
778 intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
779
780 intel_engine_signal_breadcrumbs(engine);
781 }
782
reset_finish(struct intel_gt * gt,intel_engine_mask_t awake)783 static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
784 {
785 struct intel_engine_cs *engine;
786 enum intel_engine_id id;
787
788 for_each_engine(engine, gt, id) {
789 reset_finish_engine(engine);
790 if (awake & engine->mask)
791 intel_engine_pm_put(engine);
792 }
793 }
794
nop_submit_request(struct i915_request * request)795 static void nop_submit_request(struct i915_request *request)
796 {
797 struct intel_engine_cs *engine = request->engine;
798 unsigned long flags;
799
800 RQ_TRACE(request, "-EIO\n");
801 dma_fence_set_error(&request->fence, -EIO);
802
803 spin_lock_irqsave(&engine->active.lock, flags);
804 __i915_request_submit(request);
805 i915_request_mark_complete(request);
806 spin_unlock_irqrestore(&engine->active.lock, flags);
807
808 intel_engine_signal_breadcrumbs(engine);
809 }
810
__intel_gt_set_wedged(struct intel_gt * gt)811 static void __intel_gt_set_wedged(struct intel_gt *gt)
812 {
813 struct intel_engine_cs *engine;
814 intel_engine_mask_t awake;
815 enum intel_engine_id id;
816
817 if (test_bit(I915_WEDGED, >->reset.flags))
818 return;
819
820 if (GEM_SHOW_DEBUG() && !intel_engines_are_idle(gt)) {
821 struct drm_printer p = drm_debug_printer(__func__);
822
823 for_each_engine(engine, gt, id)
824 intel_engine_dump(engine, &p, "%s\n", engine->name);
825 }
826
827 GT_TRACE(gt, "start\n");
828
829 /*
830 * First, stop submission to hw, but do not yet complete requests by
831 * rolling the global seqno forward (since this would complete requests
832 * for which we haven't set the fence error to EIO yet).
833 */
834 awake = reset_prepare(gt);
835
836 /* Even if the GPU reset fails, it should still stop the engines */
837 if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
838 __intel_gt_reset(gt, ALL_ENGINES);
839
840 for_each_engine(engine, gt, id)
841 engine->submit_request = nop_submit_request;
842
843 /*
844 * Make sure no request can slip through without getting completed by
845 * either this call here to intel_engine_write_global_seqno, or the one
846 * in nop_submit_request.
847 */
848 synchronize_rcu_expedited();
849 set_bit(I915_WEDGED, >->reset.flags);
850
851 /* Mark all executing requests as skipped */
852 for_each_engine(engine, gt, id)
853 if (engine->reset.cancel)
854 engine->reset.cancel(engine);
855
856 reset_finish(gt, awake);
857
858 GT_TRACE(gt, "end\n");
859 }
860
intel_gt_set_wedged(struct intel_gt * gt)861 void intel_gt_set_wedged(struct intel_gt *gt)
862 {
863 intel_wakeref_t wakeref;
864
865 mutex_lock(>->reset.mutex);
866 with_intel_runtime_pm(gt->uncore->rpm, wakeref)
867 __intel_gt_set_wedged(gt);
868 mutex_unlock(>->reset.mutex);
869 }
870
__intel_gt_unset_wedged(struct intel_gt * gt)871 static bool __intel_gt_unset_wedged(struct intel_gt *gt)
872 {
873 struct intel_gt_timelines *timelines = >->timelines;
874 struct intel_timeline *tl;
875 bool ok;
876
877 if (!test_bit(I915_WEDGED, >->reset.flags))
878 return true;
879
880 /* Never fully initialised, recovery impossible */
881 if (test_bit(I915_WEDGED_ON_INIT, >->reset.flags))
882 return false;
883
884 GT_TRACE(gt, "start\n");
885
886 /*
887 * Before unwedging, make sure that all pending operations
888 * are flushed and errored out - we may have requests waiting upon
889 * third party fences. We marked all inflight requests as EIO, and
890 * every execbuf since returned EIO, for consistency we want all
891 * the currently pending requests to also be marked as EIO, which
892 * is done inside our nop_submit_request - and so we must wait.
893 *
894 * No more can be submitted until we reset the wedged bit.
895 */
896 spin_lock(&timelines->lock);
897 list_for_each_entry(tl, &timelines->active_list, link) {
898 struct dma_fence *fence;
899
900 fence = i915_active_fence_get(&tl->last_request);
901 if (!fence)
902 continue;
903
904 spin_unlock(&timelines->lock);
905
906 /*
907 * All internal dependencies (i915_requests) will have
908 * been flushed by the set-wedge, but we may be stuck waiting
909 * for external fences. These should all be capped to 10s
910 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded
911 * in the worst case.
912 */
913 dma_fence_default_wait(fence, false, MAX_SCHEDULE_TIMEOUT);
914 dma_fence_put(fence);
915
916 /* Restart iteration after droping lock */
917 spin_lock(&timelines->lock);
918 tl = list_entry(&timelines->active_list, typeof(*tl), link);
919 }
920 spin_unlock(&timelines->lock);
921
922 /* We must reset pending GPU events before restoring our submission */
923 ok = !HAS_EXECLISTS(gt->i915); /* XXX better agnosticism desired */
924 if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
925 ok = __intel_gt_reset(gt, ALL_ENGINES) == 0;
926 if (!ok) {
927 /*
928 * Warn CI about the unrecoverable wedged condition.
929 * Time for a reboot.
930 */
931 add_taint_for_CI(TAINT_WARN);
932 return false;
933 }
934
935 /*
936 * Undo nop_submit_request. We prevent all new i915 requests from
937 * being queued (by disallowing execbuf whilst wedged) so having
938 * waited for all active requests above, we know the system is idle
939 * and do not have to worry about a thread being inside
940 * engine->submit_request() as we swap over. So unlike installing
941 * the nop_submit_request on reset, we can do this from normal
942 * context and do not require stop_machine().
943 */
944 intel_engines_reset_default_submission(gt);
945
946 GT_TRACE(gt, "end\n");
947
948 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
949 clear_bit(I915_WEDGED, >->reset.flags);
950
951 return true;
952 }
953
intel_gt_unset_wedged(struct intel_gt * gt)954 bool intel_gt_unset_wedged(struct intel_gt *gt)
955 {
956 bool result;
957
958 mutex_lock(>->reset.mutex);
959 result = __intel_gt_unset_wedged(gt);
960 mutex_unlock(>->reset.mutex);
961
962 return result;
963 }
964
do_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask)965 static int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
966 {
967 int err, i;
968
969 gt_revoke(gt);
970
971 err = __intel_gt_reset(gt, ALL_ENGINES);
972 for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
973 msleep(10 * (i + 1));
974 err = __intel_gt_reset(gt, ALL_ENGINES);
975 }
976 if (err)
977 return err;
978
979 return gt_reset(gt, stalled_mask);
980 }
981
resume(struct intel_gt * gt)982 static int resume(struct intel_gt *gt)
983 {
984 struct intel_engine_cs *engine;
985 enum intel_engine_id id;
986 int ret;
987
988 for_each_engine(engine, gt, id) {
989 ret = engine->resume(engine);
990 if (ret)
991 return ret;
992 }
993
994 return 0;
995 }
996
997 /**
998 * intel_gt_reset - reset chip after a hang
999 * @gt: #intel_gt to reset
1000 * @stalled_mask: mask of the stalled engines with the guilty requests
1001 * @reason: user error message for why we are resetting
1002 *
1003 * Reset the chip. Useful if a hang is detected. Marks the device as wedged
1004 * on failure.
1005 *
1006 * Procedure is fairly simple:
1007 * - reset the chip using the reset reg
1008 * - re-init context state
1009 * - re-init hardware status page
1010 * - re-init ring buffer
1011 * - re-init interrupt state
1012 * - re-init display
1013 */
intel_gt_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask,const char * reason)1014 void intel_gt_reset(struct intel_gt *gt,
1015 intel_engine_mask_t stalled_mask,
1016 const char *reason)
1017 {
1018 intel_engine_mask_t awake;
1019 int ret;
1020
1021 GT_TRACE(gt, "flags=%lx\n", gt->reset.flags);
1022
1023 might_sleep();
1024 GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, >->reset.flags));
1025 mutex_lock(>->reset.mutex);
1026
1027 /* Clear any previous failed attempts at recovery. Time to try again. */
1028 if (!__intel_gt_unset_wedged(gt))
1029 goto unlock;
1030
1031 if (reason)
1032 dev_notice(gt->i915->drm.dev,
1033 "Resetting chip for %s\n", reason);
1034 atomic_inc(>->i915->gpu_error.reset_count);
1035
1036 awake = reset_prepare(gt);
1037
1038 if (!intel_has_gpu_reset(gt)) {
1039 if (i915_modparams.reset)
1040 dev_err(gt->i915->drm.dev, "GPU reset not supported\n");
1041 else
1042 DRM_DEBUG_DRIVER("GPU reset disabled\n");
1043 goto error;
1044 }
1045
1046 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1047 intel_runtime_pm_disable_interrupts(gt->i915);
1048
1049 if (do_reset(gt, stalled_mask)) {
1050 dev_err(gt->i915->drm.dev, "Failed to reset chip\n");
1051 goto taint;
1052 }
1053
1054 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1055 intel_runtime_pm_enable_interrupts(gt->i915);
1056
1057 intel_overlay_reset(gt->i915);
1058
1059 /*
1060 * Next we need to restore the context, but we don't use those
1061 * yet either...
1062 *
1063 * Ring buffer needs to be re-initialized in the KMS case, or if X
1064 * was running at the time of the reset (i.e. we weren't VT
1065 * switched away).
1066 */
1067 ret = intel_gt_init_hw(gt);
1068 if (ret) {
1069 DRM_ERROR("Failed to initialise HW following reset (%d)\n",
1070 ret);
1071 goto taint;
1072 }
1073
1074 ret = resume(gt);
1075 if (ret)
1076 goto taint;
1077
1078 finish:
1079 reset_finish(gt, awake);
1080 unlock:
1081 mutex_unlock(>->reset.mutex);
1082 return;
1083
1084 taint:
1085 /*
1086 * History tells us that if we cannot reset the GPU now, we
1087 * never will. This then impacts everything that is run
1088 * subsequently. On failing the reset, we mark the driver
1089 * as wedged, preventing further execution on the GPU.
1090 * We also want to go one step further and add a taint to the
1091 * kernel so that any subsequent faults can be traced back to
1092 * this failure. This is important for CI, where if the
1093 * GPU/driver fails we would like to reboot and restart testing
1094 * rather than continue on into oblivion. For everyone else,
1095 * the system should still plod along, but they have been warned!
1096 */
1097 add_taint_for_CI(TAINT_WARN);
1098 error:
1099 __intel_gt_set_wedged(gt);
1100 goto finish;
1101 }
1102
intel_gt_reset_engine(struct intel_engine_cs * engine)1103 static inline int intel_gt_reset_engine(struct intel_engine_cs *engine)
1104 {
1105 return __intel_gt_reset(engine->gt, engine->mask);
1106 }
1107
1108 /**
1109 * intel_engine_reset - reset GPU engine to recover from a hang
1110 * @engine: engine to reset
1111 * @msg: reason for GPU reset; or NULL for no dev_notice()
1112 *
1113 * Reset a specific GPU engine. Useful if a hang is detected.
1114 * Returns zero on successful reset or otherwise an error code.
1115 *
1116 * Procedure is:
1117 * - identifies the request that caused the hang and it is dropped
1118 * - reset engine (which will force the engine to idle)
1119 * - re-init/configure engine
1120 */
intel_engine_reset(struct intel_engine_cs * engine,const char * msg)1121 int intel_engine_reset(struct intel_engine_cs *engine, const char *msg)
1122 {
1123 struct intel_gt *gt = engine->gt;
1124 bool uses_guc = intel_engine_in_guc_submission_mode(engine);
1125 int ret;
1126
1127 ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
1128 GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, >->reset.flags));
1129
1130 if (!intel_engine_pm_get_if_awake(engine))
1131 return 0;
1132
1133 reset_prepare_engine(engine);
1134
1135 if (msg)
1136 dev_notice(engine->i915->drm.dev,
1137 "Resetting %s for %s\n", engine->name, msg);
1138 atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
1139
1140 if (!uses_guc)
1141 ret = intel_gt_reset_engine(engine);
1142 else
1143 ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
1144 if (ret) {
1145 /* If we fail here, we expect to fallback to a global reset */
1146 DRM_DEBUG_DRIVER("%sFailed to reset %s, ret=%d\n",
1147 uses_guc ? "GuC " : "",
1148 engine->name, ret);
1149 goto out;
1150 }
1151
1152 /*
1153 * The request that caused the hang is stuck on elsp, we know the
1154 * active request and can drop it, adjust head to skip the offending
1155 * request to resume executing remaining requests in the queue.
1156 */
1157 __intel_engine_reset(engine, true);
1158
1159 /*
1160 * The engine and its registers (and workarounds in case of render)
1161 * have been reset to their default values. Follow the init_ring
1162 * process to program RING_MODE, HWSP and re-enable submission.
1163 */
1164 ret = engine->resume(engine);
1165
1166 out:
1167 intel_engine_cancel_stop_cs(engine);
1168 reset_finish_engine(engine);
1169 intel_engine_pm_put_async(engine);
1170 return ret;
1171 }
1172
intel_gt_reset_global(struct intel_gt * gt,u32 engine_mask,const char * reason)1173 static void intel_gt_reset_global(struct intel_gt *gt,
1174 u32 engine_mask,
1175 const char *reason)
1176 {
1177 #ifndef __NetBSD__ /* XXX kobject uevent...? */
1178 struct kobject *kobj = >->i915->drm.primary->kdev->kobj;
1179 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1180 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1181 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1182 #endif
1183 struct intel_wedge_me w;
1184
1185 #ifndef __NetBSD__
1186 kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
1187 #endif
1188
1189 DRM_DEBUG_DRIVER("resetting chip\n");
1190 #ifndef __NetBSD__
1191 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
1192 #endif
1193
1194 /* Use a watchdog to ensure that our reset completes */
1195 intel_wedge_on_timeout(&w, gt, 5 * HZ) {
1196 intel_prepare_reset(gt->i915);
1197
1198 /* Flush everyone using a resource about to be clobbered */
1199 synchronize_srcu_expedited(>->reset.backoff_srcu);
1200
1201 intel_gt_reset(gt, engine_mask, reason);
1202
1203 intel_finish_reset(gt->i915);
1204 }
1205
1206 #ifndef __NetBSD__ /* XXX kobj uevent...? */
1207 if (!test_bit(I915_WEDGED, >->reset.flags))
1208 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
1209 #endif
1210 }
1211
1212 /**
1213 * intel_gt_handle_error - handle a gpu error
1214 * @gt: the intel_gt
1215 * @engine_mask: mask representing engines that are hung
1216 * @flags: control flags
1217 * @fmt: Error message format string
1218 *
1219 * Do some basic checking of register state at error time and
1220 * dump it to the syslog. Also call i915_capture_error_state() to make
1221 * sure we get a record and make it available in debugfs. Fire a uevent
1222 * so userspace knows something bad happened (should trigger collection
1223 * of a ring dump etc.).
1224 */
intel_gt_handle_error(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned long flags,const char * fmt,...)1225 void intel_gt_handle_error(struct intel_gt *gt,
1226 intel_engine_mask_t engine_mask,
1227 unsigned long flags,
1228 const char *fmt, ...)
1229 {
1230 struct intel_engine_cs *engine;
1231 intel_wakeref_t wakeref;
1232 intel_engine_mask_t tmp;
1233 char error_msg[80];
1234 char *msg = NULL;
1235
1236 if (fmt) {
1237 va_list args;
1238
1239 va_start(args, fmt);
1240 vscnprintf(error_msg, sizeof(error_msg), fmt, args);
1241 va_end(args);
1242
1243 msg = error_msg;
1244 }
1245
1246 /*
1247 * In most cases it's guaranteed that we get here with an RPM
1248 * reference held, for example because there is a pending GPU
1249 * request that won't finish until the reset is done. This
1250 * isn't the case at least when we get here by doing a
1251 * simulated reset via debugfs, so get an RPM reference.
1252 */
1253 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1254
1255 engine_mask &= INTEL_INFO(gt->i915)->engine_mask;
1256
1257 if (flags & I915_ERROR_CAPTURE) {
1258 i915_capture_error_state(gt->i915);
1259 intel_gt_clear_error_registers(gt, engine_mask);
1260 }
1261
1262 /*
1263 * Try engine reset when available. We fall back to full reset if
1264 * single reset fails.
1265 */
1266 if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
1267 for_each_engine_masked(engine, gt, engine_mask, tmp) {
1268 BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
1269 if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1270 >->reset.flags))
1271 continue;
1272
1273 if (intel_engine_reset(engine, msg) == 0)
1274 engine_mask &= ~engine->mask;
1275
1276 clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
1277 >->reset.flags);
1278 }
1279 }
1280
1281 if (!engine_mask)
1282 goto out;
1283
1284 /* Full reset needs the mutex, stop any other user trying to do so. */
1285 if (test_and_set_bit(I915_RESET_BACKOFF, >->reset.flags)) {
1286 int ret;
1287 spin_lock(>->reset.lock);
1288 DRM_SPIN_WAIT_NOINTR_UNTIL(ret,
1289 >->reset.queue,
1290 >->reset.lock,
1291 !test_bit(I915_RESET_BACKOFF, >->reset.flags));
1292 spin_unlock(>->reset.lock);
1293 goto out; /* piggy-back on the other reset */
1294 }
1295
1296 /* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
1297 synchronize_rcu_expedited();
1298
1299 /* Prevent any other reset-engine attempt. */
1300 for_each_engine(engine, gt, tmp) {
1301 while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1302 >->reset.flags))
1303 wait_on_bit(>->reset.flags,
1304 I915_RESET_ENGINE + engine->id,
1305 TASK_UNINTERRUPTIBLE);
1306 }
1307
1308 intel_gt_reset_global(gt, engine_mask, msg);
1309
1310 for_each_engine(engine, gt, tmp)
1311 clear_bit_unlock(I915_RESET_ENGINE + engine->id,
1312 >->reset.flags);
1313 clear_bit_unlock(I915_RESET_BACKOFF, >->reset.flags);
1314 smp_mb__after_atomic();
1315 spin_lock(>->reset.lock);
1316 DRM_SPIN_WAKEUP_ALL(>->reset.queue, >->reset.lock);
1317 spin_unlock(>->reset.lock);
1318
1319 out:
1320 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1321 }
1322
intel_gt_reset_trylock(struct intel_gt * gt,int * srcu)1323 int intel_gt_reset_trylock(struct intel_gt *gt, int *srcu)
1324 {
1325 might_lock(>->reset.backoff_srcu);
1326 might_sleep();
1327
1328 rcu_read_lock();
1329 while (test_bit(I915_RESET_BACKOFF, >->reset.flags)) {
1330 rcu_read_unlock();
1331
1332 int ret;
1333 spin_lock(>->reset.lock);
1334 DRM_SPIN_WAIT_UNTIL(ret, >->reset.queue, >->reset.lock,
1335 !test_bit(I915_RESET_BACKOFF, >->reset.flags));
1336 spin_unlock(>->reset.lock);
1337 if (ret)
1338 return -EINTR;
1339
1340 rcu_read_lock();
1341 }
1342 *srcu = srcu_read_lock(>->reset.backoff_srcu);
1343 rcu_read_unlock();
1344
1345 return 0;
1346 }
1347
intel_gt_reset_unlock(struct intel_gt * gt,int tag)1348 void intel_gt_reset_unlock(struct intel_gt *gt, int tag)
1349 __releases(>->reset.backoff_srcu)
1350 {
1351 srcu_read_unlock(>->reset.backoff_srcu, tag);
1352 }
1353
intel_gt_terminally_wedged(struct intel_gt * gt)1354 int intel_gt_terminally_wedged(struct intel_gt *gt)
1355 {
1356 might_sleep();
1357
1358 if (!intel_gt_is_wedged(gt))
1359 return 0;
1360
1361 if (intel_gt_has_init_error(gt))
1362 return -EIO;
1363
1364 /* Reset still in progress? Maybe we will recover? */
1365 int ret;
1366 spin_lock(>->reset.lock);
1367 DRM_SPIN_WAIT_UNTIL(ret, >->reset.queue, >->reset.lock,
1368 !test_bit(I915_RESET_BACKOFF, >->reset.flags));
1369 spin_unlock(>->reset.lock);
1370 if (ret)
1371 return -EINTR;
1372
1373 return intel_gt_is_wedged(gt) ? -EIO : 0;
1374 }
1375
intel_gt_set_wedged_on_init(struct intel_gt * gt)1376 void intel_gt_set_wedged_on_init(struct intel_gt *gt)
1377 {
1378 BUILD_BUG_ON(I915_RESET_ENGINE + I915_NUM_ENGINES >
1379 I915_WEDGED_ON_INIT);
1380 intel_gt_set_wedged(gt);
1381 set_bit(I915_WEDGED_ON_INIT, >->reset.flags);
1382 }
1383
intel_gt_init_reset(struct intel_gt * gt)1384 void intel_gt_init_reset(struct intel_gt *gt)
1385 {
1386 spin_lock_init(>->reset.lock);
1387 DRM_INIT_WAITQUEUE(>->reset.queue, "i915rst");
1388 mutex_init(>->reset.mutex);
1389 init_srcu_struct(>->reset.backoff_srcu);
1390
1391 /* no GPU until we are ready! */
1392 __set_bit(I915_WEDGED, >->reset.flags);
1393 }
1394
intel_gt_fini_reset(struct intel_gt * gt)1395 void intel_gt_fini_reset(struct intel_gt *gt)
1396 {
1397 cleanup_srcu_struct(>->reset.backoff_srcu);
1398 DRM_DESTROY_WAITQUEUE(>->reset.queue);
1399 mutex_destroy(>->reset.mutex);
1400 spin_lock_destroy(>->reset.lock);
1401 }
1402
intel_wedge_me(struct work_struct * work)1403 static void intel_wedge_me(struct work_struct *work)
1404 {
1405 struct intel_wedge_me *w = container_of(work, typeof(*w), work.work);
1406
1407 dev_err(w->gt->i915->drm.dev,
1408 "%s timed out, cancelling all in-flight rendering.\n",
1409 w->name);
1410 intel_gt_set_wedged(w->gt);
1411 }
1412
__intel_init_wedge(struct intel_wedge_me * w,struct intel_gt * gt,long timeout,const char * name)1413 void __intel_init_wedge(struct intel_wedge_me *w,
1414 struct intel_gt *gt,
1415 long timeout,
1416 const char *name)
1417 {
1418 w->gt = gt;
1419 w->name = name;
1420
1421 INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me);
1422 schedule_delayed_work(&w->work, timeout);
1423 }
1424
__intel_fini_wedge(struct intel_wedge_me * w)1425 void __intel_fini_wedge(struct intel_wedge_me *w)
1426 {
1427 cancel_delayed_work_sync(&w->work);
1428 destroy_delayed_work_on_stack(&w->work);
1429 w->gt = NULL;
1430 }
1431
1432 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1433 #include "selftest_reset.c"
1434 #include "selftest_hangcheck.c"
1435 #endif
1436