2 * SPDX-License-Identifier: MIT
4 * Copyright © 2008-2018 Intel Corporation
7 #include <linux/sched/mm.h>
10 #include "i915_gpu_error.h"
11 #include "i915_reset.h"
13 #include "intel_guc.h"
15 #define RESET_MAX_RETRIES 3
17 static void engine_skip_context(struct i915_request *rq)
19 struct intel_engine_cs *engine = rq->engine;
20 struct i915_gem_context *hung_ctx = rq->gem_context;
21 struct i915_timeline *timeline = rq->timeline;
24 GEM_BUG_ON(timeline == &engine->timeline);
26 spin_lock_irqsave(&engine->timeline.lock, flags);
27 spin_lock(&timeline->lock);
29 list_for_each_entry_continue(rq, &engine->timeline.requests, link)
30 if (rq->gem_context == hung_ctx)
31 i915_request_skip(rq, -EIO);
33 list_for_each_entry(rq, &timeline->requests, link)
34 i915_request_skip(rq, -EIO);
36 spin_unlock(&timeline->lock);
37 spin_unlock_irqrestore(&engine->timeline.lock, flags);
40 static void client_mark_guilty(struct drm_i915_file_private *file_priv,
41 const struct i915_gem_context *ctx)
44 unsigned long prev_hang;
46 if (i915_gem_context_is_banned(ctx))
47 score = I915_CLIENT_SCORE_CONTEXT_BAN;
51 prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
52 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
53 score += I915_CLIENT_SCORE_HANG_FAST;
56 atomic_add(score, &file_priv->ban_score);
58 DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n",
60 atomic_read(&file_priv->ban_score));
64 static void context_mark_guilty(struct i915_gem_context *ctx)
67 bool banned, bannable;
69 atomic_inc(&ctx->guilty_count);
71 bannable = i915_gem_context_is_bannable(ctx);
72 score = atomic_add_return(CONTEXT_SCORE_GUILTY, &ctx->ban_score);
73 banned = score >= CONTEXT_SCORE_BAN_THRESHOLD;
75 /* Cool contexts don't accumulate client ban score */
80 DRM_DEBUG_DRIVER("context %s: guilty %d, score %u, banned\n",
81 ctx->name, atomic_read(&ctx->guilty_count),
83 i915_gem_context_set_banned(ctx);
86 if (!IS_ERR_OR_NULL(ctx->file_priv))
87 client_mark_guilty(ctx->file_priv, ctx);
90 static void context_mark_innocent(struct i915_gem_context *ctx)
92 atomic_inc(&ctx->active_count);
95 static void gen3_stop_engine(struct intel_engine_cs *engine)
97 struct drm_i915_private *dev_priv = engine->i915;
98 const u32 base = engine->mmio_base;
100 if (intel_engine_stop_cs(engine))
101 DRM_DEBUG_DRIVER("%s: timed out on STOP_RING\n", engine->name);
103 I915_WRITE_FW(RING_HEAD(base), I915_READ_FW(RING_TAIL(base)));
104 POSTING_READ_FW(RING_HEAD(base)); /* paranoia */
106 I915_WRITE_FW(RING_HEAD(base), 0);
107 I915_WRITE_FW(RING_TAIL(base), 0);
108 POSTING_READ_FW(RING_TAIL(base));
110 /* The ring must be empty before it is disabled */
111 I915_WRITE_FW(RING_CTL(base), 0);
113 /* Check acts as a post */
114 if (I915_READ_FW(RING_HEAD(base)) != 0)
115 DRM_DEBUG_DRIVER("%s: ring head not parked\n",
119 static void i915_stop_engines(struct drm_i915_private *i915,
120 unsigned int engine_mask)
122 struct intel_engine_cs *engine;
123 enum intel_engine_id id;
125 if (INTEL_GEN(i915) < 3)
128 for_each_engine_masked(engine, i915, engine_mask, id)
129 gen3_stop_engine(engine);
132 static bool i915_in_reset(struct pci_dev *pdev)
136 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
137 return gdrst & GRDOM_RESET_STATUS;
140 static int i915_do_reset(struct drm_i915_private *i915,
141 unsigned int engine_mask,
144 struct pci_dev *pdev = i915->drm.pdev;
147 /* Assert reset for at least 20 usec, and wait for acknowledgement. */
148 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
150 err = wait_for_atomic(i915_in_reset(pdev), 50);
152 /* Clear the reset request. */
153 pci_write_config_byte(pdev, I915_GDRST, 0);
156 err = wait_for_atomic(!i915_in_reset(pdev), 50);
161 static bool g4x_reset_complete(struct pci_dev *pdev)
165 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
166 return (gdrst & GRDOM_RESET_ENABLE) == 0;
169 static int g33_do_reset(struct drm_i915_private *i915,
170 unsigned int engine_mask,
173 struct pci_dev *pdev = i915->drm.pdev;
175 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
176 return wait_for_atomic(g4x_reset_complete(pdev), 50);
179 static int g4x_do_reset(struct drm_i915_private *dev_priv,
180 unsigned int engine_mask,
183 struct pci_dev *pdev = dev_priv->drm.pdev;
186 /* WaVcpClkGateDisableForMediaReset:ctg,elk */
187 I915_WRITE_FW(VDECCLK_GATE_D,
188 I915_READ(VDECCLK_GATE_D) | VCP_UNIT_CLOCK_GATE_DISABLE);
189 POSTING_READ_FW(VDECCLK_GATE_D);
191 pci_write_config_byte(pdev, I915_GDRST,
192 GRDOM_MEDIA | GRDOM_RESET_ENABLE);
193 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
195 DRM_DEBUG_DRIVER("Wait for media reset failed\n");
199 pci_write_config_byte(pdev, I915_GDRST,
200 GRDOM_RENDER | GRDOM_RESET_ENABLE);
201 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
203 DRM_DEBUG_DRIVER("Wait for render reset failed\n");
208 pci_write_config_byte(pdev, I915_GDRST, 0);
210 I915_WRITE_FW(VDECCLK_GATE_D,
211 I915_READ(VDECCLK_GATE_D) & ~VCP_UNIT_CLOCK_GATE_DISABLE);
212 POSTING_READ_FW(VDECCLK_GATE_D);
217 static int ironlake_do_reset(struct drm_i915_private *dev_priv,
218 unsigned int engine_mask,
223 I915_WRITE_FW(ILK_GDSR, ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
224 ret = __intel_wait_for_register_fw(dev_priv, ILK_GDSR,
225 ILK_GRDOM_RESET_ENABLE, 0,
229 DRM_DEBUG_DRIVER("Wait for render reset failed\n");
233 I915_WRITE_FW(ILK_GDSR, ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
234 ret = __intel_wait_for_register_fw(dev_priv, ILK_GDSR,
235 ILK_GRDOM_RESET_ENABLE, 0,
239 DRM_DEBUG_DRIVER("Wait for media reset failed\n");
244 I915_WRITE_FW(ILK_GDSR, 0);
245 POSTING_READ_FW(ILK_GDSR);
249 /* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
250 static int gen6_hw_domain_reset(struct drm_i915_private *dev_priv,
256 * GEN6_GDRST is not in the gt power well, no need to check
257 * for fifo space for the write or forcewake the chip for
260 I915_WRITE_FW(GEN6_GDRST, hw_domain_mask);
262 /* Wait for the device to ack the reset requests */
263 err = __intel_wait_for_register_fw(dev_priv,
264 GEN6_GDRST, hw_domain_mask, 0,
268 DRM_DEBUG_DRIVER("Wait for 0x%08x engines reset failed\n",
274 static int gen6_reset_engines(struct drm_i915_private *i915,
275 unsigned int engine_mask,
278 struct intel_engine_cs *engine;
279 const u32 hw_engine_mask[I915_NUM_ENGINES] = {
280 [RCS] = GEN6_GRDOM_RENDER,
281 [BCS] = GEN6_GRDOM_BLT,
282 [VCS] = GEN6_GRDOM_MEDIA,
283 [VCS2] = GEN8_GRDOM_MEDIA2,
284 [VECS] = GEN6_GRDOM_VECS,
288 if (engine_mask == ALL_ENGINES) {
289 hw_mask = GEN6_GRDOM_FULL;
294 for_each_engine_masked(engine, i915, engine_mask, tmp)
295 hw_mask |= hw_engine_mask[engine->id];
298 return gen6_hw_domain_reset(i915, hw_mask);
301 static u32 gen11_lock_sfc(struct drm_i915_private *dev_priv,
302 struct intel_engine_cs *engine)
304 u8 vdbox_sfc_access = RUNTIME_INFO(dev_priv)->vdbox_sfc_access;
305 i915_reg_t sfc_forced_lock, sfc_forced_lock_ack;
306 u32 sfc_forced_lock_bit, sfc_forced_lock_ack_bit;
307 i915_reg_t sfc_usage;
311 switch (engine->class) {
312 case VIDEO_DECODE_CLASS:
313 if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
316 sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
317 sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
319 sfc_forced_lock_ack = GEN11_VCS_SFC_LOCK_STATUS(engine);
320 sfc_forced_lock_ack_bit = GEN11_VCS_SFC_LOCK_ACK_BIT;
322 sfc_usage = GEN11_VCS_SFC_LOCK_STATUS(engine);
323 sfc_usage_bit = GEN11_VCS_SFC_USAGE_BIT;
324 sfc_reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
327 case VIDEO_ENHANCEMENT_CLASS:
328 sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
329 sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
331 sfc_forced_lock_ack = GEN11_VECS_SFC_LOCK_ACK(engine);
332 sfc_forced_lock_ack_bit = GEN11_VECS_SFC_LOCK_ACK_BIT;
334 sfc_usage = GEN11_VECS_SFC_USAGE(engine);
335 sfc_usage_bit = GEN11_VECS_SFC_USAGE_BIT;
336 sfc_reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
344 * Tell the engine that a software reset is going to happen. The engine
345 * will then try to force lock the SFC (if currently locked, it will
346 * remain so until we tell the engine it is safe to unlock; if currently
347 * unlocked, it will ignore this and all new lock requests). If SFC
348 * ends up being locked to the engine we want to reset, we have to reset
349 * it as well (we will unlock it once the reset sequence is completed).
351 I915_WRITE_FW(sfc_forced_lock,
352 I915_READ_FW(sfc_forced_lock) | sfc_forced_lock_bit);
354 if (__intel_wait_for_register_fw(dev_priv,
356 sfc_forced_lock_ack_bit,
357 sfc_forced_lock_ack_bit,
359 DRM_DEBUG_DRIVER("Wait for SFC forced lock ack failed\n");
363 if (I915_READ_FW(sfc_usage) & sfc_usage_bit)
364 return sfc_reset_bit;
369 static void gen11_unlock_sfc(struct drm_i915_private *dev_priv,
370 struct intel_engine_cs *engine)
372 u8 vdbox_sfc_access = RUNTIME_INFO(dev_priv)->vdbox_sfc_access;
373 i915_reg_t sfc_forced_lock;
374 u32 sfc_forced_lock_bit;
376 switch (engine->class) {
377 case VIDEO_DECODE_CLASS:
378 if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
381 sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
382 sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
385 case VIDEO_ENHANCEMENT_CLASS:
386 sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
387 sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
394 I915_WRITE_FW(sfc_forced_lock,
395 I915_READ_FW(sfc_forced_lock) & ~sfc_forced_lock_bit);
398 static int gen11_reset_engines(struct drm_i915_private *i915,
399 unsigned int engine_mask,
402 const u32 hw_engine_mask[I915_NUM_ENGINES] = {
403 [RCS] = GEN11_GRDOM_RENDER,
404 [BCS] = GEN11_GRDOM_BLT,
405 [VCS] = GEN11_GRDOM_MEDIA,
406 [VCS2] = GEN11_GRDOM_MEDIA2,
407 [VCS3] = GEN11_GRDOM_MEDIA3,
408 [VCS4] = GEN11_GRDOM_MEDIA4,
409 [VECS] = GEN11_GRDOM_VECS,
410 [VECS2] = GEN11_GRDOM_VECS2,
412 struct intel_engine_cs *engine;
417 BUILD_BUG_ON(VECS2 + 1 != I915_NUM_ENGINES);
419 if (engine_mask == ALL_ENGINES) {
420 hw_mask = GEN11_GRDOM_FULL;
423 for_each_engine_masked(engine, i915, engine_mask, tmp) {
424 hw_mask |= hw_engine_mask[engine->id];
425 hw_mask |= gen11_lock_sfc(i915, engine);
429 ret = gen6_hw_domain_reset(i915, hw_mask);
431 if (engine_mask != ALL_ENGINES)
432 for_each_engine_masked(engine, i915, engine_mask, tmp)
433 gen11_unlock_sfc(i915, engine);
438 static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
440 struct drm_i915_private *dev_priv = engine->i915;
443 I915_WRITE_FW(RING_RESET_CTL(engine->mmio_base),
444 _MASKED_BIT_ENABLE(RESET_CTL_REQUEST_RESET));
446 ret = __intel_wait_for_register_fw(dev_priv,
447 RING_RESET_CTL(engine->mmio_base),
448 RESET_CTL_READY_TO_RESET,
449 RESET_CTL_READY_TO_RESET,
453 DRM_ERROR("%s: reset request timeout\n", engine->name);
458 static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
460 struct drm_i915_private *dev_priv = engine->i915;
462 I915_WRITE_FW(RING_RESET_CTL(engine->mmio_base),
463 _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
466 static int gen8_reset_engines(struct drm_i915_private *i915,
467 unsigned int engine_mask,
470 struct intel_engine_cs *engine;
471 const bool reset_non_ready = retry >= 1;
475 for_each_engine_masked(engine, i915, engine_mask, tmp) {
476 ret = gen8_engine_reset_prepare(engine);
477 if (ret && !reset_non_ready)
481 * If this is not the first failed attempt to prepare,
482 * we decide to proceed anyway.
484 * By doing so we risk context corruption and with
485 * some gens (kbl), possible system hang if reset
486 * happens during active bb execution.
488 * We rather take context corruption instead of
489 * failed reset with a wedged driver/gpu. And
490 * active bb execution case should be covered by
491 * i915_stop_engines we have before the reset.
495 if (INTEL_GEN(i915) >= 11)
496 ret = gen11_reset_engines(i915, engine_mask, retry);
498 ret = gen6_reset_engines(i915, engine_mask, retry);
501 for_each_engine_masked(engine, i915, engine_mask, tmp)
502 gen8_engine_reset_cancel(engine);
507 typedef int (*reset_func)(struct drm_i915_private *,
508 unsigned int engine_mask,
511 static reset_func intel_get_gpu_reset(struct drm_i915_private *i915)
513 if (!i915_modparams.reset)
516 if (INTEL_GEN(i915) >= 8)
517 return gen8_reset_engines;
518 else if (INTEL_GEN(i915) >= 6)
519 return gen6_reset_engines;
520 else if (INTEL_GEN(i915) >= 5)
521 return ironlake_do_reset;
522 else if (IS_G4X(i915))
524 else if (IS_G33(i915) || IS_PINEVIEW(i915))
526 else if (INTEL_GEN(i915) >= 3)
527 return i915_do_reset;
532 int intel_gpu_reset(struct drm_i915_private *i915, unsigned int engine_mask)
534 const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
536 int ret = -ETIMEDOUT;
539 reset = intel_get_gpu_reset(i915);
544 * If the power well sleeps during the reset, the reset
545 * request may be dropped and never completes (causing -EIO).
547 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
548 for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
550 * We stop engines, otherwise we might get failed reset and a
551 * dead gpu (on elk). Also as modern gpu as kbl can suffer
552 * from system hang if batchbuffer is progressing when
553 * the reset is issued, regardless of READY_TO_RESET ack.
554 * Thus assume it is best to stop engines on all gens
555 * where we have a gpu reset.
557 * WaKBLVECSSemaphoreWaitPoll:kbl (on ALL_ENGINES)
559 * WaMediaResetMainRingCleanup:ctg,elk (presumably)
561 * FIXME: Wa for more modern gens needs to be validated
563 i915_stop_engines(i915, engine_mask);
565 GEM_TRACE("engine_mask=%x\n", engine_mask);
567 ret = reset(i915, engine_mask, retry);
570 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
575 bool intel_has_gpu_reset(struct drm_i915_private *i915)
580 return intel_get_gpu_reset(i915);
583 bool intel_has_reset_engine(struct drm_i915_private *i915)
585 return INTEL_INFO(i915)->has_reset_engine && i915_modparams.reset >= 2;
588 int intel_reset_guc(struct drm_i915_private *i915)
591 INTEL_GEN(i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
594 GEM_BUG_ON(!HAS_GUC(i915));
596 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
597 ret = gen6_hw_domain_reset(i915, guc_domain);
598 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
604 * Ensure irq handler finishes, and not run again.
605 * Also return the active request so that we only search for it once.
607 static struct i915_request *
608 reset_prepare_engine(struct intel_engine_cs *engine)
610 struct i915_request *rq;
613 * During the reset sequence, we must prevent the engine from
614 * entering RC6. As the context state is undefined until we restart
615 * the engine, if it does enter RC6 during the reset, the state
616 * written to the powercontext is undefined and so we may lose
617 * GPU state upon resume, i.e. fail to restart after a reset.
619 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
621 rq = engine->reset.prepare(engine);
622 if (rq && rq->fence.error == -EIO)
623 rq = ERR_PTR(-EIO); /* Previous reset failed! */
628 static int reset_prepare(struct drm_i915_private *i915)
630 struct intel_engine_cs *engine;
631 struct i915_request *rq;
632 enum intel_engine_id id;
635 for_each_engine(engine, i915, id) {
636 rq = reset_prepare_engine(engine);
642 engine->hangcheck.active_request = rq;
645 i915_gem_revoke_fences(i915);
646 intel_uc_sanitize(i915);
651 /* Returns the request if it was guilty of the hang */
652 static struct i915_request *
653 reset_request(struct intel_engine_cs *engine,
654 struct i915_request *rq,
658 * The guilty request will get skipped on a hung engine.
660 * Users of client default contexts do not rely on logical
661 * state preserved between batches so it is safe to execute
662 * queued requests following the hang. Non default contexts
663 * rely on preserved state, so skipping a batch loses the
664 * evolution of the state and it needs to be considered corrupted.
665 * Executing more queued batches on top of corrupted state is
666 * risky. But we take the risk by trying to advance through
667 * the queued requests in order to make the client behaviour
668 * more predictable around resets, by not throwing away random
669 * amount of batches it has prepared for execution. Sophisticated
670 * clients can use gem_reset_stats_ioctl and dma fence status
671 * (exported via sync_file info ioctl on explicit fences) to observe
672 * when it loses the context state and should rebuild accordingly.
674 * The context ban, and ultimately the client ban, mechanism are safety
675 * valves if client submission ends up resulting in nothing more than
679 if (i915_request_completed(rq)) {
680 GEM_TRACE("%s pardoned global=%d (fence %llx:%lld), current %d\n",
681 engine->name, rq->global_seqno,
682 rq->fence.context, rq->fence.seqno,
683 intel_engine_get_seqno(engine));
688 context_mark_guilty(rq->gem_context);
689 i915_request_skip(rq, -EIO);
691 /* If this context is now banned, skip all pending requests. */
692 if (i915_gem_context_is_banned(rq->gem_context))
693 engine_skip_context(rq);
696 * Since this is not the hung engine, it may have advanced
697 * since the hang declaration. Double check by refinding
698 * the active request at the time of the reset.
700 rq = i915_gem_find_active_request(engine);
704 context_mark_innocent(rq->gem_context);
705 dma_fence_set_error(&rq->fence, -EAGAIN);
707 /* Rewind the engine to replay the incomplete rq */
708 spin_lock_irqsave(&engine->timeline.lock, flags);
709 rq = list_prev_entry(rq, link);
710 if (&rq->link == &engine->timeline.requests)
712 spin_unlock_irqrestore(&engine->timeline.lock, flags);
719 static void reset_engine(struct intel_engine_cs *engine,
720 struct i915_request *rq,
724 rq = reset_request(engine, rq, stalled);
726 /* Setup the CS to resume from the breadcrumb of the hung request */
727 engine->reset.reset(engine, rq);
730 static void gt_reset(struct drm_i915_private *i915, unsigned int stalled_mask)
732 struct intel_engine_cs *engine;
733 enum intel_engine_id id;
735 lockdep_assert_held(&i915->drm.struct_mutex);
737 i915_retire_requests(i915);
739 for_each_engine(engine, i915, id) {
740 struct intel_context *ce;
743 engine->hangcheck.active_request,
744 stalled_mask & ENGINE_MASK(id));
745 ce = fetch_and_zero(&engine->last_retired_context);
747 intel_context_unpin(ce);
750 * Ostensibily, we always want a context loaded for powersaving,
751 * so if the engine is idle after the reset, send a request
752 * to load our scratch kernel_context.
754 * More mysteriously, if we leave the engine idle after a reset,
755 * the next userspace batch may hang, with what appears to be
756 * an incoherent read by the CS (presumably stale TLB). An
757 * empty request appears sufficient to paper over the glitch.
759 if (intel_engine_is_idle(engine)) {
760 struct i915_request *rq;
762 rq = i915_request_alloc(engine, i915->kernel_context);
764 i915_request_add(rq);
768 i915_gem_restore_fences(i915);
771 static void reset_finish_engine(struct intel_engine_cs *engine)
773 engine->reset.finish(engine);
775 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
778 static void reset_finish(struct drm_i915_private *i915)
780 struct intel_engine_cs *engine;
781 enum intel_engine_id id;
783 lockdep_assert_held(&i915->drm.struct_mutex);
785 for_each_engine(engine, i915, id) {
786 engine->hangcheck.active_request = NULL;
787 reset_finish_engine(engine);
791 static void nop_submit_request(struct i915_request *request)
795 GEM_TRACE("%s fence %llx:%lld -> -EIO\n",
796 request->engine->name,
797 request->fence.context, request->fence.seqno);
798 dma_fence_set_error(&request->fence, -EIO);
800 spin_lock_irqsave(&request->engine->timeline.lock, flags);
801 __i915_request_submit(request);
802 intel_engine_write_global_seqno(request->engine, request->global_seqno);
803 spin_unlock_irqrestore(&request->engine->timeline.lock, flags);
806 void i915_gem_set_wedged(struct drm_i915_private *i915)
808 struct i915_gpu_error *error = &i915->gpu_error;
809 struct intel_engine_cs *engine;
810 enum intel_engine_id id;
812 mutex_lock(&error->wedge_mutex);
813 if (test_bit(I915_WEDGED, &error->flags)) {
814 mutex_unlock(&error->wedge_mutex);
818 if (GEM_SHOW_DEBUG() && !intel_engines_are_idle(i915)) {
819 struct drm_printer p = drm_debug_printer(__func__);
821 for_each_engine(engine, i915, id)
822 intel_engine_dump(engine, &p, "%s\n", engine->name);
825 GEM_TRACE("start\n");
828 * First, stop submission to hw, but do not yet complete requests by
829 * rolling the global seqno forward (since this would complete requests
830 * for which we haven't set the fence error to EIO yet).
832 for_each_engine(engine, i915, id)
833 reset_prepare_engine(engine);
835 /* Even if the GPU reset fails, it should still stop the engines */
836 if (INTEL_GEN(i915) >= 5)
837 intel_gpu_reset(i915, ALL_ENGINES);
839 for_each_engine(engine, i915, id) {
840 engine->submit_request = nop_submit_request;
841 engine->schedule = NULL;
843 i915->caps.scheduler = 0;
846 * Make sure no request can slip through without getting completed by
847 * either this call here to intel_engine_write_global_seqno, or the one
848 * in nop_submit_request.
852 /* Mark all executing requests as skipped */
853 for_each_engine(engine, i915, id)
854 engine->cancel_requests(engine);
856 for_each_engine(engine, i915, id) {
857 reset_finish_engine(engine);
858 intel_engine_wakeup(engine);
861 smp_mb__before_atomic();
862 set_bit(I915_WEDGED, &error->flags);
865 mutex_unlock(&error->wedge_mutex);
867 wake_up_all(&error->reset_queue);
870 bool i915_gem_unset_wedged(struct drm_i915_private *i915)
872 struct i915_gpu_error *error = &i915->gpu_error;
873 struct i915_timeline *tl;
876 lockdep_assert_held(&i915->drm.struct_mutex);
878 if (!test_bit(I915_WEDGED, &error->flags))
881 if (!i915->gt.scratch) /* Never full initialised, recovery impossible */
884 mutex_lock(&error->wedge_mutex);
886 GEM_TRACE("start\n");
889 * Before unwedging, make sure that all pending operations
890 * are flushed and errored out - we may have requests waiting upon
891 * third party fences. We marked all inflight requests as EIO, and
892 * every execbuf since returned EIO, for consistency we want all
893 * the currently pending requests to also be marked as EIO, which
894 * is done inside our nop_submit_request - and so we must wait.
896 * No more can be submitted until we reset the wedged bit.
898 list_for_each_entry(tl, &i915->gt.timelines, link) {
899 struct i915_request *rq;
901 rq = i915_gem_active_peek(&tl->last_request,
902 &i915->drm.struct_mutex);
907 * We can't use our normal waiter as we want to
908 * avoid recursively trying to handle the current
909 * reset. The basic dma_fence_default_wait() installs
910 * a callback for dma_fence_signal(), which is
911 * triggered by our nop handler (indirectly, the
912 * callback enables the signaler thread which is
913 * woken by the nop_submit_request() advancing the seqno
914 * and when the seqno passes the fence, the signaler
915 * then signals the fence waking us up).
917 if (dma_fence_default_wait(&rq->fence, true,
918 MAX_SCHEDULE_TIMEOUT) < 0)
921 i915_retire_requests(i915);
922 GEM_BUG_ON(i915->gt.active_requests);
924 intel_engines_sanitize(i915, false);
927 * Undo nop_submit_request. We prevent all new i915 requests from
928 * being queued (by disallowing execbuf whilst wedged) so having
929 * waited for all active requests above, we know the system is idle
930 * and do not have to worry about a thread being inside
931 * engine->submit_request() as we swap over. So unlike installing
932 * the nop_submit_request on reset, we can do this from normal
933 * context and do not require stop_machine().
935 intel_engines_reset_default_submission(i915);
936 i915_gem_contexts_lost(i915);
940 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
941 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
944 mutex_unlock(&i915->gpu_error.wedge_mutex);
950 * i915_reset - reset chip after a hang
951 * @i915: #drm_i915_private to reset
952 * @stalled_mask: mask of the stalled engines with the guilty requests
953 * @reason: user error message for why we are resetting
955 * Reset the chip. Useful if a hang is detected. Marks the device as wedged
958 * Caller must hold the struct_mutex.
960 * Procedure is fairly simple:
961 * - reset the chip using the reset reg
962 * - re-init context state
963 * - re-init hardware status page
964 * - re-init ring buffer
965 * - re-init interrupt state
968 void i915_reset(struct drm_i915_private *i915,
969 unsigned int stalled_mask,
972 struct i915_gpu_error *error = &i915->gpu_error;
976 GEM_TRACE("flags=%lx\n", error->flags);
979 lockdep_assert_held(&i915->drm.struct_mutex);
980 assert_rpm_wakelock_held(i915);
981 GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &error->flags));
983 if (!test_bit(I915_RESET_HANDOFF, &error->flags))
986 /* Clear any previous failed attempts at recovery. Time to try again. */
987 if (!i915_gem_unset_wedged(i915))
991 dev_notice(i915->drm.dev, "Resetting chip for %s\n", reason);
992 error->reset_count++;
994 ret = reset_prepare(i915);
996 dev_err(i915->drm.dev, "GPU recovery failed\n");
1000 if (!intel_has_gpu_reset(i915)) {
1001 if (i915_modparams.reset)
1002 dev_err(i915->drm.dev, "GPU reset not supported\n");
1004 DRM_DEBUG_DRIVER("GPU reset disabled\n");
1008 for (i = 0; i < RESET_MAX_RETRIES; i++) {
1009 ret = intel_gpu_reset(i915, ALL_ENGINES);
1016 dev_err(i915->drm.dev, "Failed to reset chip\n");
1020 /* Ok, now get things going again... */
1023 * Everything depends on having the GTT running, so we need to start
1026 ret = i915_ggtt_enable_hw(i915);
1028 DRM_ERROR("Failed to re-enable GGTT following reset (%d)\n",
1033 gt_reset(i915, stalled_mask);
1034 intel_overlay_reset(i915);
1037 * Next we need to restore the context, but we don't use those
1040 * Ring buffer needs to be re-initialized in the KMS case, or if X
1041 * was running at the time of the reset (i.e. we weren't VT
1044 ret = i915_gem_init_hw(i915);
1046 DRM_ERROR("Failed to initialise HW following reset (%d)\n",
1051 i915_queue_hangcheck(i915);
1056 clear_bit(I915_RESET_HANDOFF, &error->flags);
1057 wake_up_bit(&error->flags, I915_RESET_HANDOFF);
1062 * History tells us that if we cannot reset the GPU now, we
1063 * never will. This then impacts everything that is run
1064 * subsequently. On failing the reset, we mark the driver
1065 * as wedged, preventing further execution on the GPU.
1066 * We also want to go one step further and add a taint to the
1067 * kernel so that any subsequent faults can be traced back to
1068 * this failure. This is important for CI, where if the
1069 * GPU/driver fails we would like to reboot and restart testing
1070 * rather than continue on into oblivion. For everyone else,
1071 * the system should still plod along, but they have been warned!
1073 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
1075 i915_gem_set_wedged(i915);
1076 i915_retire_requests(i915);
1080 static inline int intel_gt_reset_engine(struct drm_i915_private *i915,
1081 struct intel_engine_cs *engine)
1083 return intel_gpu_reset(i915, intel_engine_flag(engine));
1087 * i915_reset_engine - reset GPU engine to recover from a hang
1088 * @engine: engine to reset
1089 * @msg: reason for GPU reset; or NULL for no dev_notice()
1091 * Reset a specific GPU engine. Useful if a hang is detected.
1092 * Returns zero on successful reset or otherwise an error code.
1095 * - identifies the request that caused the hang and it is dropped
1096 * - reset engine (which will force the engine to idle)
1097 * - re-init/configure engine
1099 int i915_reset_engine(struct intel_engine_cs *engine, const char *msg)
1101 struct i915_gpu_error *error = &engine->i915->gpu_error;
1102 struct i915_request *active_request;
1105 GEM_TRACE("%s flags=%lx\n", engine->name, error->flags);
1106 GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &error->flags));
1108 active_request = reset_prepare_engine(engine);
1109 if (IS_ERR_OR_NULL(active_request)) {
1110 /* Either the previous reset failed, or we pardon the reset. */
1111 ret = PTR_ERR(active_request);
1116 dev_notice(engine->i915->drm.dev,
1117 "Resetting %s for %s\n", engine->name, msg);
1118 error->reset_engine_count[engine->id]++;
1120 if (!engine->i915->guc.execbuf_client)
1121 ret = intel_gt_reset_engine(engine->i915, engine);
1123 ret = intel_guc_reset_engine(&engine->i915->guc, engine);
1125 /* If we fail here, we expect to fallback to a global reset */
1126 DRM_DEBUG_DRIVER("%sFailed to reset %s, ret=%d\n",
1127 engine->i915->guc.execbuf_client ? "GuC " : "",
1133 * The request that caused the hang is stuck on elsp, we know the
1134 * active request and can drop it, adjust head to skip the offending
1135 * request to resume executing remaining requests in the queue.
1137 reset_engine(engine, active_request, true);
1140 * The engine and its registers (and workarounds in case of render)
1141 * have been reset to their default values. Follow the init_ring
1142 * process to program RING_MODE, HWSP and re-enable submission.
1144 ret = engine->init_hw(engine);
1149 intel_engine_cancel_stop_cs(engine);
1150 reset_finish_engine(engine);
1154 static void i915_reset_device(struct drm_i915_private *i915,
1158 struct i915_gpu_error *error = &i915->gpu_error;
1159 struct kobject *kobj = &i915->drm.primary->kdev->kobj;
1160 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1161 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1162 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1163 struct i915_wedge_me w;
1165 kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
1167 DRM_DEBUG_DRIVER("resetting chip\n");
1168 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
1170 /* Use a watchdog to ensure that our reset completes */
1171 i915_wedge_on_timeout(&w, i915, 5 * HZ) {
1172 intel_prepare_reset(i915);
1174 error->reason = reason;
1175 error->stalled_mask = engine_mask;
1177 /* Signal that locked waiters should reset the GPU */
1178 smp_mb__before_atomic();
1179 set_bit(I915_RESET_HANDOFF, &error->flags);
1180 wake_up_all(&error->wait_queue);
1183 * Wait for anyone holding the lock to wakeup, without
1184 * blocking indefinitely on struct_mutex.
1187 if (mutex_trylock(&i915->drm.struct_mutex)) {
1188 i915_reset(i915, engine_mask, reason);
1189 mutex_unlock(&i915->drm.struct_mutex);
1191 } while (wait_on_bit_timeout(&error->flags,
1193 TASK_UNINTERRUPTIBLE,
1196 error->stalled_mask = 0;
1197 error->reason = NULL;
1199 intel_finish_reset(i915);
1202 if (!test_bit(I915_WEDGED, &error->flags))
1203 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
1206 void i915_clear_error_registers(struct drm_i915_private *dev_priv)
1210 if (!IS_GEN(dev_priv, 2))
1211 I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
1213 if (INTEL_GEN(dev_priv) < 4)
1214 I915_WRITE(IPEIR, I915_READ(IPEIR));
1216 I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
1218 I915_WRITE(EIR, I915_READ(EIR));
1219 eir = I915_READ(EIR);
1222 * some errors might have become stuck,
1225 DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
1226 I915_WRITE(EMR, I915_READ(EMR) | eir);
1227 I915_WRITE(IIR, I915_MASTER_ERROR_INTERRUPT);
1230 if (INTEL_GEN(dev_priv) >= 8) {
1231 I915_WRITE(GEN8_RING_FAULT_REG,
1232 I915_READ(GEN8_RING_FAULT_REG) & ~RING_FAULT_VALID);
1233 POSTING_READ(GEN8_RING_FAULT_REG);
1234 } else if (INTEL_GEN(dev_priv) >= 6) {
1235 struct intel_engine_cs *engine;
1236 enum intel_engine_id id;
1238 for_each_engine(engine, dev_priv, id) {
1239 I915_WRITE(RING_FAULT_REG(engine),
1240 I915_READ(RING_FAULT_REG(engine)) &
1243 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS]));
1248 * i915_handle_error - handle a gpu error
1249 * @i915: i915 device private
1250 * @engine_mask: mask representing engines that are hung
1251 * @flags: control flags
1252 * @fmt: Error message format string
1254 * Do some basic checking of register state at error time and
1255 * dump it to the syslog. Also call i915_capture_error_state() to make
1256 * sure we get a record and make it available in debugfs. Fire a uevent
1257 * so userspace knows something bad happened (should trigger collection
1258 * of a ring dump etc.).
1260 void i915_handle_error(struct drm_i915_private *i915,
1262 unsigned long flags,
1263 const char *fmt, ...)
1265 struct intel_engine_cs *engine;
1266 intel_wakeref_t wakeref;
1274 va_start(args, fmt);
1275 vscnprintf(error_msg, sizeof(error_msg), fmt, args);
1282 * In most cases it's guaranteed that we get here with an RPM
1283 * reference held, for example because there is a pending GPU
1284 * request that won't finish until the reset is done. This
1285 * isn't the case at least when we get here by doing a
1286 * simulated reset via debugfs, so get an RPM reference.
1288 wakeref = intel_runtime_pm_get(i915);
1290 engine_mask &= INTEL_INFO(i915)->ring_mask;
1292 if (flags & I915_ERROR_CAPTURE) {
1293 i915_capture_error_state(i915, engine_mask, msg);
1294 i915_clear_error_registers(i915);
1298 * Try engine reset when available. We fall back to full reset if
1299 * single reset fails.
1301 if (intel_has_reset_engine(i915) &&
1302 !i915_terminally_wedged(&i915->gpu_error)) {
1303 for_each_engine_masked(engine, i915, engine_mask, tmp) {
1304 BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
1305 if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1306 &i915->gpu_error.flags))
1309 if (i915_reset_engine(engine, msg) == 0)
1310 engine_mask &= ~intel_engine_flag(engine);
1312 clear_bit(I915_RESET_ENGINE + engine->id,
1313 &i915->gpu_error.flags);
1314 wake_up_bit(&i915->gpu_error.flags,
1315 I915_RESET_ENGINE + engine->id);
1322 /* Full reset needs the mutex, stop any other user trying to do so. */
1323 if (test_and_set_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags)) {
1324 wait_event(i915->gpu_error.reset_queue,
1325 !test_bit(I915_RESET_BACKOFF,
1326 &i915->gpu_error.flags));
1330 /* Prevent any other reset-engine attempt. */
1331 for_each_engine(engine, i915, tmp) {
1332 while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1333 &i915->gpu_error.flags))
1334 wait_on_bit(&i915->gpu_error.flags,
1335 I915_RESET_ENGINE + engine->id,
1336 TASK_UNINTERRUPTIBLE);
1339 i915_reset_device(i915, engine_mask, msg);
1341 for_each_engine(engine, i915, tmp) {
1342 clear_bit(I915_RESET_ENGINE + engine->id,
1343 &i915->gpu_error.flags);
1346 clear_bit(I915_RESET_BACKOFF, &i915->gpu_error.flags);
1347 wake_up_all(&i915->gpu_error.reset_queue);
1350 intel_runtime_pm_put(i915, wakeref);
1353 static void i915_wedge_me(struct work_struct *work)
1355 struct i915_wedge_me *w = container_of(work, typeof(*w), work.work);
1357 dev_err(w->i915->drm.dev,
1358 "%s timed out, cancelling all in-flight rendering.\n",
1360 i915_gem_set_wedged(w->i915);
1363 void __i915_init_wedge(struct i915_wedge_me *w,
1364 struct drm_i915_private *i915,
1371 INIT_DELAYED_WORK_ONSTACK(&w->work, i915_wedge_me);
1372 schedule_delayed_work(&w->work, timeout);
1375 void __i915_fini_wedge(struct i915_wedge_me *w)
1377 cancel_delayed_work_sync(&w->work);
1378 destroy_delayed_work_on_stack(&w->work);