1 // SPDX-License-Identifier: MIT
3 * Copyright © 2008-2018 Intel Corporation
6 #include <linux/sched/mm.h>
7 #include <linux/stop_machine.h>
8 #include <linux/string_helpers.h>
10 #include "display/intel_display_reset.h"
11 #include "display/intel_overlay.h"
13 #include "gem/i915_gem_context.h"
15 #include "gt/intel_gt_regs.h"
17 #include "gt/uc/intel_gsc_fw.h"
20 #include "i915_file_private.h"
21 #include "i915_gpu_error.h"
24 #include "intel_breadcrumbs.h"
25 #include "intel_engine_pm.h"
26 #include "intel_engine_regs.h"
28 #include "intel_gt_pm.h"
29 #include "intel_gt_requests.h"
30 #include "intel_mchbar_regs.h"
31 #include "intel_pci_config.h"
32 #include "intel_reset.h"
34 #include "uc/intel_guc.h"
36 #define RESET_MAX_RETRIES 3
38 /* XXX How to handle concurrent GGTT updates using tiling registers? */
39 #define RESET_UNDER_STOP_MACHINE 0
41 static void client_mark_guilty(struct i915_gem_context *ctx, bool banned)
43 struct drm_i915_file_private *file_priv = ctx->file_priv;
44 unsigned long prev_hang;
47 if (IS_ERR_OR_NULL(file_priv))
52 score = I915_CLIENT_SCORE_CONTEXT_BAN;
54 prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
55 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
56 score += I915_CLIENT_SCORE_HANG_FAST;
59 atomic_add(score, &file_priv->ban_score);
61 drm_dbg(&ctx->i915->drm,
62 "client %s: gained %u ban score, now %u\n",
64 atomic_read(&file_priv->ban_score));
68 static bool mark_guilty(struct i915_request *rq)
70 struct i915_gem_context *ctx;
71 unsigned long prev_hang;
75 if (intel_context_is_closed(rq->context))
79 ctx = rcu_dereference(rq->context->gem_context);
80 if (ctx && !kref_get_unless_zero(&ctx->ref))
84 return intel_context_is_banned(rq->context);
86 atomic_inc(&ctx->guilty_count);
88 /* Cool contexts are too cool to be banned! (Used for reset testing.) */
89 if (!i915_gem_context_is_bannable(ctx)) {
94 drm_notice(&ctx->i915->drm,
95 "%s context reset due to GPU hang\n",
98 /* Record the timestamp for the last N hangs */
99 prev_hang = ctx->hang_timestamp[0];
100 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++)
101 ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1];
102 ctx->hang_timestamp[i] = jiffies;
104 /* If we have hung N+1 times in rapid succession, we ban the context! */
105 banned = !i915_gem_context_is_recoverable(ctx);
106 if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES))
109 drm_dbg(&ctx->i915->drm, "context %s: guilty %d, banned\n",
110 ctx->name, atomic_read(&ctx->guilty_count));
112 client_mark_guilty(ctx, banned);
115 i915_gem_context_put(ctx);
119 static void mark_innocent(struct i915_request *rq)
121 struct i915_gem_context *ctx;
124 ctx = rcu_dereference(rq->context->gem_context);
126 atomic_inc(&ctx->active_count);
130 void __i915_request_reset(struct i915_request *rq, bool guilty)
134 RQ_TRACE(rq, "guilty? %s\n", str_yes_no(guilty));
135 GEM_BUG_ON(__i915_request_is_complete(rq));
137 rcu_read_lock(); /* protect the GEM context */
139 i915_request_set_error_once(rq, -EIO);
140 __i915_request_skip(rq);
141 banned = mark_guilty(rq);
143 i915_request_set_error_once(rq, -EAGAIN);
149 intel_context_ban(rq->context, rq);
152 static bool i915_in_reset(struct pci_dev *pdev)
156 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
157 return gdrst & GRDOM_RESET_STATUS;
160 static int i915_do_reset(struct intel_gt *gt,
161 intel_engine_mask_t engine_mask,
164 struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
167 /* Assert reset for at least 20 usec, and wait for acknowledgement. */
168 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
170 err = wait_for_atomic(i915_in_reset(pdev), 50);
172 /* Clear the reset request. */
173 pci_write_config_byte(pdev, I915_GDRST, 0);
176 err = wait_for_atomic(!i915_in_reset(pdev), 50);
181 static bool g4x_reset_complete(struct pci_dev *pdev)
185 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
186 return (gdrst & GRDOM_RESET_ENABLE) == 0;
189 static int g33_do_reset(struct intel_gt *gt,
190 intel_engine_mask_t engine_mask,
193 struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
195 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
196 return wait_for_atomic(g4x_reset_complete(pdev), 50);
199 static int g4x_do_reset(struct intel_gt *gt,
200 intel_engine_mask_t engine_mask,
203 struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
204 struct intel_uncore *uncore = gt->uncore;
207 /* WaVcpClkGateDisableForMediaReset:ctg,elk */
208 intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, 0, VCP_UNIT_CLOCK_GATE_DISABLE);
209 intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
211 pci_write_config_byte(pdev, I915_GDRST,
212 GRDOM_MEDIA | GRDOM_RESET_ENABLE);
213 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
215 GT_TRACE(gt, "Wait for media reset failed\n");
219 pci_write_config_byte(pdev, I915_GDRST,
220 GRDOM_RENDER | GRDOM_RESET_ENABLE);
221 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
223 GT_TRACE(gt, "Wait for render reset failed\n");
228 pci_write_config_byte(pdev, I915_GDRST, 0);
230 intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE, 0);
231 intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
236 static int ilk_do_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask,
239 struct intel_uncore *uncore = gt->uncore;
242 intel_uncore_write_fw(uncore, ILK_GDSR,
243 ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
244 ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
245 ILK_GRDOM_RESET_ENABLE, 0,
249 GT_TRACE(gt, "Wait for render reset failed\n");
253 intel_uncore_write_fw(uncore, ILK_GDSR,
254 ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
255 ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
256 ILK_GRDOM_RESET_ENABLE, 0,
260 GT_TRACE(gt, "Wait for media reset failed\n");
265 intel_uncore_write_fw(uncore, ILK_GDSR, 0);
266 intel_uncore_posting_read_fw(uncore, ILK_GDSR);
270 /* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
271 static int gen6_hw_domain_reset(struct intel_gt *gt, u32 hw_domain_mask)
273 struct intel_uncore *uncore = gt->uncore;
278 * On some platforms, e.g. Jasperlake, we see that the engine register
279 * state is not cleared until shortly after GDRST reports completion,
280 * causing a failure as we try to immediately resume while the internal
281 * state is still in flux. If we immediately repeat the reset, the
282 * second reset appears to serialise with the first, and since it is a
283 * no-op, the registers should retain their reset value. However, there
284 * is still a concern that upon leaving the second reset, the internal
285 * engine state is still in flux and not ready for resuming.
287 * Starting on MTL, there are some prep steps that we need to do when
288 * resetting some engines that need to be applied every time we write to
289 * GEN6_GDRST. As those are time consuming (tens of ms), we don't want
290 * to perform that twice, so, since the Jasperlake issue hasn't been
291 * observed on MTL, we avoid repeating the reset on newer platforms.
293 loops = GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 70) ? 2 : 1;
296 * GEN6_GDRST is not in the gt power well, no need to check
297 * for fifo space for the write or forcewake the chip for
301 intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask);
303 /* Wait for the device to ack the reset requests. */
304 err = __intel_wait_for_register_fw(uncore, GEN6_GDRST,
308 } while (err == 0 && --loops);
311 "Wait for 0x%08x engines reset failed\n",
315 * As we have observed that the engine state is still volatile
316 * after GDRST is acked, impose a small delay to let everything settle.
323 static int __gen6_reset_engines(struct intel_gt *gt,
324 intel_engine_mask_t engine_mask,
327 struct intel_engine_cs *engine;
330 if (engine_mask == ALL_ENGINES) {
331 hw_mask = GEN6_GRDOM_FULL;
333 intel_engine_mask_t tmp;
336 for_each_engine_masked(engine, gt, engine_mask, tmp) {
337 hw_mask |= engine->reset_domain;
341 return gen6_hw_domain_reset(gt, hw_mask);
344 static int gen6_reset_engines(struct intel_gt *gt,
345 intel_engine_mask_t engine_mask,
351 spin_lock_irqsave(>->uncore->lock, flags);
352 ret = __gen6_reset_engines(gt, engine_mask, retry);
353 spin_unlock_irqrestore(>->uncore->lock, flags);
358 static struct intel_engine_cs *find_sfc_paired_vecs_engine(struct intel_engine_cs *engine)
362 GEM_BUG_ON(engine->class != VIDEO_DECODE_CLASS);
364 vecs_id = _VECS((engine->instance) / 2);
366 return engine->gt->engine[vecs_id];
369 struct sfc_lock_data {
372 i915_reg_t usage_reg;
379 static void get_sfc_forced_lock_data(struct intel_engine_cs *engine,
380 struct sfc_lock_data *sfc_lock)
382 switch (engine->class) {
384 MISSING_CASE(engine->class);
386 case VIDEO_DECODE_CLASS:
387 sfc_lock->lock_reg = GEN11_VCS_SFC_FORCED_LOCK(engine->mmio_base);
388 sfc_lock->lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
390 sfc_lock->ack_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
391 sfc_lock->ack_bit = GEN11_VCS_SFC_LOCK_ACK_BIT;
393 sfc_lock->usage_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
394 sfc_lock->usage_bit = GEN11_VCS_SFC_USAGE_BIT;
395 sfc_lock->reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
398 case VIDEO_ENHANCEMENT_CLASS:
399 sfc_lock->lock_reg = GEN11_VECS_SFC_FORCED_LOCK(engine->mmio_base);
400 sfc_lock->lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
402 sfc_lock->ack_reg = GEN11_VECS_SFC_LOCK_ACK(engine->mmio_base);
403 sfc_lock->ack_bit = GEN11_VECS_SFC_LOCK_ACK_BIT;
405 sfc_lock->usage_reg = GEN11_VECS_SFC_USAGE(engine->mmio_base);
406 sfc_lock->usage_bit = GEN11_VECS_SFC_USAGE_BIT;
407 sfc_lock->reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
413 static int gen11_lock_sfc(struct intel_engine_cs *engine,
417 struct intel_uncore *uncore = engine->uncore;
418 u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
419 struct sfc_lock_data sfc_lock;
420 bool lock_obtained, lock_to_other = false;
423 switch (engine->class) {
424 case VIDEO_DECODE_CLASS:
425 if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
429 case VIDEO_ENHANCEMENT_CLASS:
430 get_sfc_forced_lock_data(engine, &sfc_lock);
437 if (!(intel_uncore_read_fw(uncore, sfc_lock.usage_reg) & sfc_lock.usage_bit)) {
438 struct intel_engine_cs *paired_vecs;
440 if (engine->class != VIDEO_DECODE_CLASS ||
441 GRAPHICS_VER(engine->i915) != 12)
447 * If the VCS-MFX isn't using the SFC, we also need to check
448 * whether VCS-HCP is using it. If so, we need to issue a *VE*
449 * forced lock on the VE engine that shares the same SFC.
451 if (!(intel_uncore_read_fw(uncore,
452 GEN12_HCP_SFC_LOCK_STATUS(engine->mmio_base)) &
453 GEN12_HCP_SFC_USAGE_BIT))
456 paired_vecs = find_sfc_paired_vecs_engine(engine);
457 get_sfc_forced_lock_data(paired_vecs, &sfc_lock);
458 lock_to_other = true;
459 *unlock_mask |= paired_vecs->mask;
461 *unlock_mask |= engine->mask;
465 * If the engine is using an SFC, tell the engine that a software reset
466 * is going to happen. The engine will then try to force lock the SFC.
467 * If SFC ends up being locked to the engine we want to reset, we have
468 * to reset it as well (we will unlock it once the reset sequence is
471 intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, 0, sfc_lock.lock_bit);
473 ret = __intel_wait_for_register_fw(uncore,
480 * Was the SFC released while we were trying to lock it?
482 * We should reset both the engine and the SFC if:
483 * - We were locking the SFC to this engine and the lock succeeded
485 * - We were locking the SFC to a different engine (Wa_14010733141)
486 * but the SFC was released before the lock was obtained.
488 * Otherwise we need only reset the engine by itself and we can
489 * leave the SFC alone.
491 lock_obtained = (intel_uncore_read_fw(uncore, sfc_lock.usage_reg) &
492 sfc_lock.usage_bit) != 0;
493 if (lock_obtained == lock_to_other)
497 ENGINE_TRACE(engine, "Wait for SFC forced lock ack failed\n");
501 *reset_mask |= sfc_lock.reset_bit;
505 static void gen11_unlock_sfc(struct intel_engine_cs *engine)
507 struct intel_uncore *uncore = engine->uncore;
508 u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
509 struct sfc_lock_data sfc_lock = {};
511 if (engine->class != VIDEO_DECODE_CLASS &&
512 engine->class != VIDEO_ENHANCEMENT_CLASS)
515 if (engine->class == VIDEO_DECODE_CLASS &&
516 (BIT(engine->instance) & vdbox_sfc_access) == 0)
519 get_sfc_forced_lock_data(engine, &sfc_lock);
521 intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, sfc_lock.lock_bit, 0);
524 static int __gen11_reset_engines(struct intel_gt *gt,
525 intel_engine_mask_t engine_mask,
528 struct intel_engine_cs *engine;
529 intel_engine_mask_t tmp;
530 u32 reset_mask, unlock_mask = 0;
533 if (engine_mask == ALL_ENGINES) {
534 reset_mask = GEN11_GRDOM_FULL;
537 for_each_engine_masked(engine, gt, engine_mask, tmp) {
538 reset_mask |= engine->reset_domain;
539 ret = gen11_lock_sfc(engine, &reset_mask, &unlock_mask);
545 ret = gen6_hw_domain_reset(gt, reset_mask);
549 * We unlock the SFC based on the lock status and not the result of
550 * gen11_lock_sfc to make sure that we clean properly if something
551 * wrong happened during the lock (e.g. lock acquired after timeout
554 * Due to Wa_14010733141, we may have locked an SFC to an engine that
555 * wasn't being reset. So instead of calling gen11_unlock_sfc()
556 * on engine_mask, we instead call it on the mask of engines that our
557 * gen11_lock_sfc() calls told us actually had locks attempted.
559 for_each_engine_masked(engine, gt, unlock_mask, tmp)
560 gen11_unlock_sfc(engine);
565 static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
567 struct intel_uncore *uncore = engine->uncore;
568 const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
569 u32 request, mask, ack;
572 if (I915_SELFTEST_ONLY(should_fail(&engine->reset_timeout, 1)))
575 ack = intel_uncore_read_fw(uncore, reg);
576 if (ack & RESET_CTL_CAT_ERROR) {
578 * For catastrophic errors, ready-for-reset sequence
579 * needs to be bypassed: HAS#396813
581 request = RESET_CTL_CAT_ERROR;
582 mask = RESET_CTL_CAT_ERROR;
584 /* Catastrophic errors need to be cleared by HW */
586 } else if (!(ack & RESET_CTL_READY_TO_RESET)) {
587 request = RESET_CTL_REQUEST_RESET;
588 mask = RESET_CTL_READY_TO_RESET;
589 ack = RESET_CTL_READY_TO_RESET;
594 intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
595 ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
598 drm_err(&engine->i915->drm,
599 "%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
600 engine->name, request,
601 intel_uncore_read_fw(uncore, reg));
606 static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
608 intel_uncore_write_fw(engine->uncore,
609 RING_RESET_CTL(engine->mmio_base),
610 _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
613 static int gen8_reset_engines(struct intel_gt *gt,
614 intel_engine_mask_t engine_mask,
617 struct intel_engine_cs *engine;
618 const bool reset_non_ready = retry >= 1;
619 intel_engine_mask_t tmp;
623 spin_lock_irqsave(>->uncore->lock, flags);
625 for_each_engine_masked(engine, gt, engine_mask, tmp) {
626 ret = gen8_engine_reset_prepare(engine);
627 if (ret && !reset_non_ready)
631 * If this is not the first failed attempt to prepare,
632 * we decide to proceed anyway.
634 * By doing so we risk context corruption and with
635 * some gens (kbl), possible system hang if reset
636 * happens during active bb execution.
638 * We rather take context corruption instead of
639 * failed reset with a wedged driver/gpu. And
640 * active bb execution case should be covered by
641 * stop_engines() we have before the reset.
646 * Wa_22011100796:dg2, whenever Full soft reset is required,
647 * reset all individual engines firstly, and then do a full soft reset.
649 * This is best effort, so ignore any error from the initial reset.
651 if (IS_DG2(gt->i915) && engine_mask == ALL_ENGINES)
652 __gen11_reset_engines(gt, gt->info.engine_mask, 0);
654 if (GRAPHICS_VER(gt->i915) >= 11)
655 ret = __gen11_reset_engines(gt, engine_mask, retry);
657 ret = __gen6_reset_engines(gt, engine_mask, retry);
660 for_each_engine_masked(engine, gt, engine_mask, tmp)
661 gen8_engine_reset_cancel(engine);
663 spin_unlock_irqrestore(>->uncore->lock, flags);
668 static int mock_reset(struct intel_gt *gt,
669 intel_engine_mask_t mask,
675 typedef int (*reset_func)(struct intel_gt *,
676 intel_engine_mask_t engine_mask,
679 static reset_func intel_get_gpu_reset(const struct intel_gt *gt)
681 struct drm_i915_private *i915 = gt->i915;
685 else if (GRAPHICS_VER(i915) >= 8)
686 return gen8_reset_engines;
687 else if (GRAPHICS_VER(i915) >= 6)
688 return gen6_reset_engines;
689 else if (GRAPHICS_VER(i915) >= 5)
691 else if (IS_G4X(i915))
693 else if (IS_G33(i915) || IS_PINEVIEW(i915))
695 else if (GRAPHICS_VER(i915) >= 3)
696 return i915_do_reset;
701 static int __reset_guc(struct intel_gt *gt)
704 GRAPHICS_VER(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
706 return gen6_hw_domain_reset(gt, guc_domain);
709 static bool needs_wa_14015076503(struct intel_gt *gt, intel_engine_mask_t engine_mask)
711 if (!IS_METEORLAKE(gt->i915) || !HAS_ENGINE(gt, GSC0))
714 if (!__HAS_ENGINE(engine_mask, GSC0))
717 return intel_gsc_uc_fw_init_done(>->uc.gsc);
720 static intel_engine_mask_t
721 wa_14015076503_start(struct intel_gt *gt, intel_engine_mask_t engine_mask, bool first)
723 if (!needs_wa_14015076503(gt, engine_mask))
727 * wa_14015076503: if the GSC FW is loaded, we need to alert it that
728 * we're going to do a GSC engine reset and then wait for 200ms for the
729 * FW to get ready for it. However, if this is the first ALL_ENGINES
730 * reset attempt and the GSC is not busy, we can try to instead reset
731 * the GuC and all the other engines individually to avoid the 200ms
733 * Skipping the GSC engine is safe because, differently from other
734 * engines, the GSCCS only role is to forward the commands to the GSC
735 * FW, so it doesn't have any HW outside of the CS itself and therefore
736 * it has no state that we don't explicitly re-init on resume or on
737 * context switch LRC or power context). The HW for the GSC uC is
738 * managed by the GSC FW so we don't need to care about that.
740 if (engine_mask == ALL_ENGINES && first && intel_engine_is_idle(gt->engine[GSC0])) {
742 engine_mask = gt->info.engine_mask & ~BIT(GSC0);
744 intel_uncore_rmw(gt->uncore,
745 HECI_H_GS1(MTL_GSC_HECI2_BASE),
746 0, HECI_H_GS1_ER_PREP);
748 /* make sure the reset bit is clear when writing the CSR reg */
749 intel_uncore_rmw(gt->uncore,
750 HECI_H_CSR(MTL_GSC_HECI2_BASE),
751 HECI_H_CSR_RST, HECI_H_CSR_IG);
759 wa_14015076503_end(struct intel_gt *gt, intel_engine_mask_t engine_mask)
761 if (!needs_wa_14015076503(gt, engine_mask))
764 intel_uncore_rmw(gt->uncore,
765 HECI_H_GS1(MTL_GSC_HECI2_BASE),
766 HECI_H_GS1_ER_PREP, 0);
769 int __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask)
771 const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
773 int ret = -ETIMEDOUT;
776 reset = intel_get_gpu_reset(gt);
781 * If the power well sleeps during the reset, the reset
782 * request may be dropped and never completes (causing -EIO).
784 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
785 for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
786 intel_engine_mask_t reset_mask;
788 reset_mask = wa_14015076503_start(gt, engine_mask, !retry);
790 GT_TRACE(gt, "engine_mask=%x\n", reset_mask);
792 ret = reset(gt, reset_mask, retry);
795 wa_14015076503_end(gt, reset_mask);
797 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
802 bool intel_has_gpu_reset(const struct intel_gt *gt)
804 if (!gt->i915->params.reset)
807 return intel_get_gpu_reset(gt);
810 bool intel_has_reset_engine(const struct intel_gt *gt)
812 if (gt->i915->params.reset < 2)
815 return INTEL_INFO(gt->i915)->has_reset_engine;
818 int intel_reset_guc(struct intel_gt *gt)
822 GEM_BUG_ON(!HAS_GT_UC(gt->i915));
824 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
825 ret = __reset_guc(gt);
826 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
832 * Ensure irq handler finishes, and not run again.
833 * Also return the active request so that we only search for it once.
835 static void reset_prepare_engine(struct intel_engine_cs *engine)
838 * During the reset sequence, we must prevent the engine from
839 * entering RC6. As the context state is undefined until we restart
840 * the engine, if it does enter RC6 during the reset, the state
841 * written to the powercontext is undefined and so we may lose
842 * GPU state upon resume, i.e. fail to restart after a reset.
844 intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
845 if (engine->reset.prepare)
846 engine->reset.prepare(engine);
849 static void revoke_mmaps(struct intel_gt *gt)
853 for (i = 0; i < gt->ggtt->num_fences; i++) {
854 struct drm_vma_offset_node *node;
855 struct i915_vma *vma;
858 vma = READ_ONCE(gt->ggtt->fence_regs[i].vma);
862 if (!i915_vma_has_userfault(vma))
865 GEM_BUG_ON(vma->fence != >->ggtt->fence_regs[i]);
870 node = &vma->mmo->vma_node;
871 vma_offset = vma->gtt_view.partial.offset << PAGE_SHIFT;
873 unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
874 drm_vma_node_offset_addr(node) + vma_offset,
880 static intel_engine_mask_t reset_prepare(struct intel_gt *gt)
882 struct intel_engine_cs *engine;
883 intel_engine_mask_t awake = 0;
884 enum intel_engine_id id;
886 /* For GuC mode, ensure submission is disabled before stopping ring */
887 intel_uc_reset_prepare(>->uc);
889 for_each_engine(engine, gt, id) {
890 if (intel_engine_pm_get_if_awake(engine))
891 awake |= engine->mask;
892 reset_prepare_engine(engine);
898 static void gt_revoke(struct intel_gt *gt)
903 static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
905 struct intel_engine_cs *engine;
906 enum intel_engine_id id;
910 * Everything depends on having the GTT running, so we need to start
913 err = i915_ggtt_enable_hw(gt->i915);
918 for_each_engine(engine, gt, id)
919 __intel_engine_reset(engine, stalled_mask & engine->mask);
922 intel_uc_reset(>->uc, ALL_ENGINES);
924 intel_ggtt_restore_fences(gt->ggtt);
929 static void reset_finish_engine(struct intel_engine_cs *engine)
931 if (engine->reset.finish)
932 engine->reset.finish(engine);
933 intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
935 intel_engine_signal_breadcrumbs(engine);
938 static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
940 struct intel_engine_cs *engine;
941 enum intel_engine_id id;
943 for_each_engine(engine, gt, id) {
944 reset_finish_engine(engine);
945 if (awake & engine->mask)
946 intel_engine_pm_put(engine);
949 intel_uc_reset_finish(>->uc);
952 static void nop_submit_request(struct i915_request *request)
954 RQ_TRACE(request, "-EIO\n");
956 request = i915_request_mark_eio(request);
958 i915_request_submit(request);
959 intel_engine_signal_breadcrumbs(request->engine);
961 i915_request_put(request);
965 static void __intel_gt_set_wedged(struct intel_gt *gt)
967 struct intel_engine_cs *engine;
968 intel_engine_mask_t awake;
969 enum intel_engine_id id;
971 if (test_bit(I915_WEDGED, >->reset.flags))
974 GT_TRACE(gt, "start\n");
977 * First, stop submission to hw, but do not yet complete requests by
978 * rolling the global seqno forward (since this would complete requests
979 * for which we haven't set the fence error to EIO yet).
981 awake = reset_prepare(gt);
983 /* Even if the GPU reset fails, it should still stop the engines */
984 if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
985 __intel_gt_reset(gt, ALL_ENGINES);
987 for_each_engine(engine, gt, id)
988 engine->submit_request = nop_submit_request;
991 * Make sure no request can slip through without getting completed by
992 * either this call here to intel_engine_write_global_seqno, or the one
993 * in nop_submit_request.
995 synchronize_rcu_expedited();
996 set_bit(I915_WEDGED, >->reset.flags);
998 /* Mark all executing requests as skipped */
1000 for_each_engine(engine, gt, id)
1001 if (engine->reset.cancel)
1002 engine->reset.cancel(engine);
1003 intel_uc_cancel_requests(>->uc);
1006 reset_finish(gt, awake);
1008 GT_TRACE(gt, "end\n");
1011 void intel_gt_set_wedged(struct intel_gt *gt)
1013 intel_wakeref_t wakeref;
1015 if (test_bit(I915_WEDGED, >->reset.flags))
1018 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1019 mutex_lock(>->reset.mutex);
1021 if (GEM_SHOW_DEBUG()) {
1022 struct drm_printer p = drm_debug_printer(__func__);
1023 struct intel_engine_cs *engine;
1024 enum intel_engine_id id;
1026 drm_printf(&p, "called from %pS\n", (void *)_RET_IP_);
1027 for_each_engine(engine, gt, id) {
1028 if (intel_engine_is_idle(engine))
1031 intel_engine_dump(engine, &p, "%s\n", engine->name);
1035 __intel_gt_set_wedged(gt);
1037 mutex_unlock(>->reset.mutex);
1038 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1041 static bool __intel_gt_unset_wedged(struct intel_gt *gt)
1043 struct intel_gt_timelines *timelines = >->timelines;
1044 struct intel_timeline *tl;
1047 if (!test_bit(I915_WEDGED, >->reset.flags))
1050 /* Never fully initialised, recovery impossible */
1051 if (intel_gt_has_unrecoverable_error(gt))
1054 GT_TRACE(gt, "start\n");
1057 * Before unwedging, make sure that all pending operations
1058 * are flushed and errored out - we may have requests waiting upon
1059 * third party fences. We marked all inflight requests as EIO, and
1060 * every execbuf since returned EIO, for consistency we want all
1061 * the currently pending requests to also be marked as EIO, which
1062 * is done inside our nop_submit_request - and so we must wait.
1064 * No more can be submitted until we reset the wedged bit.
1066 spin_lock(&timelines->lock);
1067 list_for_each_entry(tl, &timelines->active_list, link) {
1068 struct dma_fence *fence;
1070 fence = i915_active_fence_get(&tl->last_request);
1074 spin_unlock(&timelines->lock);
1077 * All internal dependencies (i915_requests) will have
1078 * been flushed by the set-wedge, but we may be stuck waiting
1079 * for external fences. These should all be capped to 10s
1080 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded
1081 * in the worst case.
1083 dma_fence_default_wait(fence, false, MAX_SCHEDULE_TIMEOUT);
1084 dma_fence_put(fence);
1086 /* Restart iteration after droping lock */
1087 spin_lock(&timelines->lock);
1088 tl = list_entry(&timelines->active_list, typeof(*tl), link);
1090 spin_unlock(&timelines->lock);
1092 /* We must reset pending GPU events before restoring our submission */
1093 ok = !HAS_EXECLISTS(gt->i915); /* XXX better agnosticism desired */
1094 if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1095 ok = __intel_gt_reset(gt, ALL_ENGINES) == 0;
1098 * Warn CI about the unrecoverable wedged condition.
1099 * Time for a reboot.
1101 add_taint_for_CI(gt->i915, TAINT_WARN);
1106 * Undo nop_submit_request. We prevent all new i915 requests from
1107 * being queued (by disallowing execbuf whilst wedged) so having
1108 * waited for all active requests above, we know the system is idle
1109 * and do not have to worry about a thread being inside
1110 * engine->submit_request() as we swap over. So unlike installing
1111 * the nop_submit_request on reset, we can do this from normal
1112 * context and do not require stop_machine().
1114 intel_engines_reset_default_submission(gt);
1116 GT_TRACE(gt, "end\n");
1118 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
1119 clear_bit(I915_WEDGED, >->reset.flags);
1124 bool intel_gt_unset_wedged(struct intel_gt *gt)
1128 mutex_lock(>->reset.mutex);
1129 result = __intel_gt_unset_wedged(gt);
1130 mutex_unlock(>->reset.mutex);
1135 static int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
1139 err = __intel_gt_reset(gt, ALL_ENGINES);
1140 for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
1141 msleep(10 * (i + 1));
1142 err = __intel_gt_reset(gt, ALL_ENGINES);
1147 return gt_reset(gt, stalled_mask);
1150 static int resume(struct intel_gt *gt)
1152 struct intel_engine_cs *engine;
1153 enum intel_engine_id id;
1156 for_each_engine(engine, gt, id) {
1157 ret = intel_engine_resume(engine);
1166 * intel_gt_reset - reset chip after a hang
1167 * @gt: #intel_gt to reset
1168 * @stalled_mask: mask of the stalled engines with the guilty requests
1169 * @reason: user error message for why we are resetting
1171 * Reset the chip. Useful if a hang is detected. Marks the device as wedged
1174 * Procedure is fairly simple:
1175 * - reset the chip using the reset reg
1176 * - re-init context state
1177 * - re-init hardware status page
1178 * - re-init ring buffer
1179 * - re-init interrupt state
1182 void intel_gt_reset(struct intel_gt *gt,
1183 intel_engine_mask_t stalled_mask,
1186 intel_engine_mask_t awake;
1189 GT_TRACE(gt, "flags=%lx\n", gt->reset.flags);
1192 GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, >->reset.flags));
1195 * FIXME: Revoking cpu mmap ptes cannot be done from a dma_fence
1196 * critical section like gpu reset.
1200 mutex_lock(>->reset.mutex);
1202 /* Clear any previous failed attempts at recovery. Time to try again. */
1203 if (!__intel_gt_unset_wedged(gt))
1207 drm_notice(>->i915->drm,
1208 "Resetting chip for %s\n", reason);
1209 atomic_inc(>->i915->gpu_error.reset_count);
1211 awake = reset_prepare(gt);
1213 if (!intel_has_gpu_reset(gt)) {
1214 if (gt->i915->params.reset)
1215 drm_err(>->i915->drm, "GPU reset not supported\n");
1217 drm_dbg(>->i915->drm, "GPU reset disabled\n");
1221 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1222 intel_runtime_pm_disable_interrupts(gt->i915);
1224 if (do_reset(gt, stalled_mask)) {
1225 drm_err(>->i915->drm, "Failed to reset chip\n");
1229 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1230 intel_runtime_pm_enable_interrupts(gt->i915);
1232 intel_overlay_reset(gt->i915);
1235 * Next we need to restore the context, but we don't use those
1238 * Ring buffer needs to be re-initialized in the KMS case, or if X
1239 * was running at the time of the reset (i.e. we weren't VT
1242 ret = intel_gt_init_hw(gt);
1244 drm_err(>->i915->drm,
1245 "Failed to initialise HW following reset (%d)\n",
1255 reset_finish(gt, awake);
1257 mutex_unlock(>->reset.mutex);
1262 * History tells us that if we cannot reset the GPU now, we
1263 * never will. This then impacts everything that is run
1264 * subsequently. On failing the reset, we mark the driver
1265 * as wedged, preventing further execution on the GPU.
1266 * We also want to go one step further and add a taint to the
1267 * kernel so that any subsequent faults can be traced back to
1268 * this failure. This is important for CI, where if the
1269 * GPU/driver fails we would like to reboot and restart testing
1270 * rather than continue on into oblivion. For everyone else,
1271 * the system should still plod along, but they have been warned!
1273 add_taint_for_CI(gt->i915, TAINT_WARN);
1275 __intel_gt_set_wedged(gt);
1279 static int intel_gt_reset_engine(struct intel_engine_cs *engine)
1281 return __intel_gt_reset(engine->gt, engine->mask);
1284 int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
1286 struct intel_gt *gt = engine->gt;
1289 ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
1290 GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, >->reset.flags));
1292 if (intel_engine_uses_guc(engine))
1295 if (!intel_engine_pm_get_if_awake(engine))
1298 reset_prepare_engine(engine);
1301 drm_notice(&engine->i915->drm,
1302 "Resetting %s for %s\n", engine->name, msg);
1303 atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
1305 ret = intel_gt_reset_engine(engine);
1307 /* If we fail here, we expect to fallback to a global reset */
1308 ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
1313 * The request that caused the hang is stuck on elsp, we know the
1314 * active request and can drop it, adjust head to skip the offending
1315 * request to resume executing remaining requests in the queue.
1317 __intel_engine_reset(engine, true);
1320 * The engine and its registers (and workarounds in case of render)
1321 * have been reset to their default values. Follow the init_ring
1322 * process to program RING_MODE, HWSP and re-enable submission.
1324 ret = intel_engine_resume(engine);
1327 intel_engine_cancel_stop_cs(engine);
1328 reset_finish_engine(engine);
1329 intel_engine_pm_put_async(engine);
1334 * intel_engine_reset - reset GPU engine to recover from a hang
1335 * @engine: engine to reset
1336 * @msg: reason for GPU reset; or NULL for no drm_notice()
1338 * Reset a specific GPU engine. Useful if a hang is detected.
1339 * Returns zero on successful reset or otherwise an error code.
1342 * - identifies the request that caused the hang and it is dropped
1343 * - reset engine (which will force the engine to idle)
1344 * - re-init/configure engine
1346 int intel_engine_reset(struct intel_engine_cs *engine, const char *msg)
1351 err = __intel_engine_reset_bh(engine, msg);
1357 static void intel_gt_reset_global(struct intel_gt *gt,
1361 struct kobject *kobj = >->i915->drm.primary->kdev->kobj;
1362 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1363 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1364 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1365 struct intel_wedge_me w;
1367 kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
1369 GT_TRACE(gt, "resetting chip, engines=%x\n", engine_mask);
1370 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
1372 /* Use a watchdog to ensure that our reset completes */
1373 intel_wedge_on_timeout(&w, gt, 60 * HZ) {
1374 intel_display_reset_prepare(gt->i915);
1376 intel_gt_reset(gt, engine_mask, reason);
1378 intel_display_reset_finish(gt->i915);
1381 if (!test_bit(I915_WEDGED, >->reset.flags))
1382 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
1386 * intel_gt_handle_error - handle a gpu error
1388 * @engine_mask: mask representing engines that are hung
1389 * @flags: control flags
1390 * @fmt: Error message format string
1392 * Do some basic checking of register state at error time and
1393 * dump it to the syslog. Also call i915_capture_error_state() to make
1394 * sure we get a record and make it available in debugfs. Fire a uevent
1395 * so userspace knows something bad happened (should trigger collection
1396 * of a ring dump etc.).
1398 void intel_gt_handle_error(struct intel_gt *gt,
1399 intel_engine_mask_t engine_mask,
1400 unsigned long flags,
1401 const char *fmt, ...)
1403 struct intel_engine_cs *engine;
1404 intel_wakeref_t wakeref;
1405 intel_engine_mask_t tmp;
1412 va_start(args, fmt);
1413 vscnprintf(error_msg, sizeof(error_msg), fmt, args);
1420 * In most cases it's guaranteed that we get here with an RPM
1421 * reference held, for example because there is a pending GPU
1422 * request that won't finish until the reset is done. This
1423 * isn't the case at least when we get here by doing a
1424 * simulated reset via debugfs, so get an RPM reference.
1426 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1428 engine_mask &= gt->info.engine_mask;
1430 if (flags & I915_ERROR_CAPTURE) {
1431 i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_NONE);
1432 intel_gt_clear_error_registers(gt, engine_mask);
1436 * Try engine reset when available. We fall back to full reset if
1437 * single reset fails.
1439 if (!intel_uc_uses_guc_submission(>->uc) &&
1440 intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
1442 for_each_engine_masked(engine, gt, engine_mask, tmp) {
1443 BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
1444 if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1448 if (__intel_engine_reset_bh(engine, msg) == 0)
1449 engine_mask &= ~engine->mask;
1451 clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
1460 /* Full reset needs the mutex, stop any other user trying to do so. */
1461 if (test_and_set_bit(I915_RESET_BACKOFF, >->reset.flags)) {
1462 wait_event(gt->reset.queue,
1463 !test_bit(I915_RESET_BACKOFF, >->reset.flags));
1464 goto out; /* piggy-back on the other reset */
1467 /* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
1468 synchronize_rcu_expedited();
1471 * Prevent any other reset-engine attempt. We don't do this for GuC
1472 * submission the GuC owns the per-engine reset, not the i915.
1474 if (!intel_uc_uses_guc_submission(>->uc)) {
1475 for_each_engine(engine, gt, tmp) {
1476 while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1478 wait_on_bit(>->reset.flags,
1479 I915_RESET_ENGINE + engine->id,
1480 TASK_UNINTERRUPTIBLE);
1484 /* Flush everyone using a resource about to be clobbered */
1485 synchronize_srcu_expedited(>->reset.backoff_srcu);
1487 intel_gt_reset_global(gt, engine_mask, msg);
1489 if (!intel_uc_uses_guc_submission(>->uc)) {
1490 for_each_engine(engine, gt, tmp)
1491 clear_bit_unlock(I915_RESET_ENGINE + engine->id,
1494 clear_bit_unlock(I915_RESET_BACKOFF, >->reset.flags);
1495 smp_mb__after_atomic();
1496 wake_up_all(>->reset.queue);
1499 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1502 static int _intel_gt_reset_lock(struct intel_gt *gt, int *srcu, bool retry)
1504 might_lock(>->reset.backoff_srcu);
1509 while (test_bit(I915_RESET_BACKOFF, >->reset.flags)) {
1515 if (wait_event_interruptible(gt->reset.queue,
1516 !test_bit(I915_RESET_BACKOFF,
1522 *srcu = srcu_read_lock(>->reset.backoff_srcu);
1528 int intel_gt_reset_trylock(struct intel_gt *gt, int *srcu)
1530 return _intel_gt_reset_lock(gt, srcu, false);
1533 int intel_gt_reset_lock_interruptible(struct intel_gt *gt, int *srcu)
1535 return _intel_gt_reset_lock(gt, srcu, true);
1538 void intel_gt_reset_unlock(struct intel_gt *gt, int tag)
1539 __releases(>->reset.backoff_srcu)
1541 srcu_read_unlock(>->reset.backoff_srcu, tag);
1544 int intel_gt_terminally_wedged(struct intel_gt *gt)
1548 if (!intel_gt_is_wedged(gt))
1551 if (intel_gt_has_unrecoverable_error(gt))
1554 /* Reset still in progress? Maybe we will recover? */
1555 if (wait_event_interruptible(gt->reset.queue,
1556 !test_bit(I915_RESET_BACKOFF,
1560 return intel_gt_is_wedged(gt) ? -EIO : 0;
1563 void intel_gt_set_wedged_on_init(struct intel_gt *gt)
1565 BUILD_BUG_ON(I915_RESET_ENGINE + I915_NUM_ENGINES >
1566 I915_WEDGED_ON_INIT);
1567 intel_gt_set_wedged(gt);
1568 i915_disable_error_state(gt->i915, -ENODEV);
1569 set_bit(I915_WEDGED_ON_INIT, >->reset.flags);
1571 /* Wedged on init is non-recoverable */
1572 add_taint_for_CI(gt->i915, TAINT_WARN);
1575 void intel_gt_set_wedged_on_fini(struct intel_gt *gt)
1577 intel_gt_set_wedged(gt);
1578 i915_disable_error_state(gt->i915, -ENODEV);
1579 set_bit(I915_WEDGED_ON_FINI, >->reset.flags);
1580 intel_gt_retire_requests(gt); /* cleanup any wedged requests */
1583 void intel_gt_init_reset(struct intel_gt *gt)
1585 init_waitqueue_head(>->reset.queue);
1586 mutex_init(>->reset.mutex);
1587 init_srcu_struct(>->reset.backoff_srcu);
1590 * While undesirable to wait inside the shrinker, complain anyway.
1592 * If we have to wait during shrinking, we guarantee forward progress
1593 * by forcing the reset. Therefore during the reset we must not
1594 * re-enter the shrinker. By declaring that we take the reset mutex
1595 * within the shrinker, we forbid ourselves from performing any
1596 * fs-reclaim or taking related locks during reset.
1598 i915_gem_shrinker_taints_mutex(gt->i915, >->reset.mutex);
1600 /* no GPU until we are ready! */
1601 __set_bit(I915_WEDGED, >->reset.flags);
1604 void intel_gt_fini_reset(struct intel_gt *gt)
1606 cleanup_srcu_struct(>->reset.backoff_srcu);
1609 static void intel_wedge_me(struct work_struct *work)
1611 struct intel_wedge_me *w = container_of(work, typeof(*w), work.work);
1613 drm_err(&w->gt->i915->drm,
1614 "%s timed out, cancelling all in-flight rendering.\n",
1616 intel_gt_set_wedged(w->gt);
1619 void __intel_init_wedge(struct intel_wedge_me *w,
1620 struct intel_gt *gt,
1627 INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me);
1628 schedule_delayed_work(&w->work, timeout);
1631 void __intel_fini_wedge(struct intel_wedge_me *w)
1633 cancel_delayed_work_sync(&w->work);
1634 destroy_delayed_work_on_stack(&w->work);
1638 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1639 #include "selftest_reset.c"
1640 #include "selftest_hangcheck.c"