2 * SPDX-License-Identifier: MIT
4 * Copyright © 2008-2018 Intel Corporation
7 #include <linux/sched/mm.h>
8 #include <linux/stop_machine.h>
11 #include "i915_gpu_error.h"
12 #include "i915_reset.h"
14 #include "intel_guc.h"
16 #define RESET_MAX_RETRIES 3
18 /* XXX How to handle concurrent GGTT updates using tiling registers? */
19 #define RESET_UNDER_STOP_MACHINE 0
21 static void engine_skip_context(struct i915_request *rq)
23 struct intel_engine_cs *engine = rq->engine;
24 struct i915_gem_context *hung_ctx = rq->gem_context;
26 lockdep_assert_held(&engine->timeline.lock);
28 if (!i915_request_is_active(rq))
31 list_for_each_entry_continue(rq, &engine->timeline.requests, link)
32 if (rq->gem_context == hung_ctx)
33 i915_request_skip(rq, -EIO);
36 static void client_mark_guilty(struct drm_i915_file_private *file_priv,
37 const struct i915_gem_context *ctx)
40 unsigned long prev_hang;
42 if (i915_gem_context_is_banned(ctx))
43 score = I915_CLIENT_SCORE_CONTEXT_BAN;
47 prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
48 if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
49 score += I915_CLIENT_SCORE_HANG_FAST;
52 atomic_add(score, &file_priv->ban_score);
54 DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n",
56 atomic_read(&file_priv->ban_score));
60 static bool context_mark_guilty(struct i915_gem_context *ctx)
62 unsigned long prev_hang;
66 atomic_inc(&ctx->guilty_count);
68 /* Cool contexts are too cool to be banned! (Used for reset testing.) */
69 if (!i915_gem_context_is_bannable(ctx))
72 /* Record the timestamp for the last N hangs */
73 prev_hang = ctx->hang_timestamp[0];
74 for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++)
75 ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1];
76 ctx->hang_timestamp[i] = jiffies;
78 /* If we have hung N+1 times in rapid succession, we ban the context! */
79 banned = !i915_gem_context_is_recoverable(ctx);
80 if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES))
83 DRM_DEBUG_DRIVER("context %s: guilty %d, banned\n",
84 ctx->name, atomic_read(&ctx->guilty_count));
85 i915_gem_context_set_banned(ctx);
88 if (!IS_ERR_OR_NULL(ctx->file_priv))
89 client_mark_guilty(ctx->file_priv, ctx);
94 static void context_mark_innocent(struct i915_gem_context *ctx)
96 atomic_inc(&ctx->active_count);
99 void i915_reset_request(struct i915_request *rq, bool guilty)
101 lockdep_assert_held(&rq->engine->timeline.lock);
102 GEM_BUG_ON(i915_request_completed(rq));
105 i915_request_skip(rq, -EIO);
106 if (context_mark_guilty(rq->gem_context))
107 engine_skip_context(rq);
109 dma_fence_set_error(&rq->fence, -EAGAIN);
110 context_mark_innocent(rq->gem_context);
114 static void gen3_stop_engine(struct intel_engine_cs *engine)
116 struct drm_i915_private *dev_priv = engine->i915;
117 const u32 base = engine->mmio_base;
119 GEM_TRACE("%s\n", engine->name);
121 if (intel_engine_stop_cs(engine))
122 GEM_TRACE("%s: timed out on STOP_RING\n", engine->name);
124 I915_WRITE_FW(RING_HEAD(base), I915_READ_FW(RING_TAIL(base)));
125 POSTING_READ_FW(RING_HEAD(base)); /* paranoia */
127 I915_WRITE_FW(RING_HEAD(base), 0);
128 I915_WRITE_FW(RING_TAIL(base), 0);
129 POSTING_READ_FW(RING_TAIL(base));
131 /* The ring must be empty before it is disabled */
132 I915_WRITE_FW(RING_CTL(base), 0);
134 /* Check acts as a post */
135 if (I915_READ_FW(RING_HEAD(base)))
136 GEM_TRACE("%s: ring head [%x] not parked\n",
137 engine->name, I915_READ_FW(RING_HEAD(base)));
140 static void i915_stop_engines(struct drm_i915_private *i915,
141 unsigned int engine_mask)
143 struct intel_engine_cs *engine;
144 enum intel_engine_id id;
146 if (INTEL_GEN(i915) < 3)
149 for_each_engine_masked(engine, i915, engine_mask, id)
150 gen3_stop_engine(engine);
153 static bool i915_in_reset(struct pci_dev *pdev)
157 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
158 return gdrst & GRDOM_RESET_STATUS;
161 static int i915_do_reset(struct drm_i915_private *i915,
162 unsigned int engine_mask,
165 struct pci_dev *pdev = i915->drm.pdev;
168 /* Assert reset for at least 20 usec, and wait for acknowledgement. */
169 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
171 err = wait_for_atomic(i915_in_reset(pdev), 50);
173 /* Clear the reset request. */
174 pci_write_config_byte(pdev, I915_GDRST, 0);
177 err = wait_for_atomic(!i915_in_reset(pdev), 50);
182 static bool g4x_reset_complete(struct pci_dev *pdev)
186 pci_read_config_byte(pdev, I915_GDRST, &gdrst);
187 return (gdrst & GRDOM_RESET_ENABLE) == 0;
190 static int g33_do_reset(struct drm_i915_private *i915,
191 unsigned int engine_mask,
194 struct pci_dev *pdev = i915->drm.pdev;
196 pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
197 return wait_for_atomic(g4x_reset_complete(pdev), 50);
200 static int g4x_do_reset(struct drm_i915_private *dev_priv,
201 unsigned int engine_mask,
204 struct pci_dev *pdev = dev_priv->drm.pdev;
207 /* WaVcpClkGateDisableForMediaReset:ctg,elk */
208 I915_WRITE_FW(VDECCLK_GATE_D,
209 I915_READ(VDECCLK_GATE_D) | VCP_UNIT_CLOCK_GATE_DISABLE);
210 POSTING_READ_FW(VDECCLK_GATE_D);
212 pci_write_config_byte(pdev, I915_GDRST,
213 GRDOM_MEDIA | GRDOM_RESET_ENABLE);
214 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
216 DRM_DEBUG_DRIVER("Wait for media reset failed\n");
220 pci_write_config_byte(pdev, I915_GDRST,
221 GRDOM_RENDER | GRDOM_RESET_ENABLE);
222 ret = wait_for_atomic(g4x_reset_complete(pdev), 50);
224 DRM_DEBUG_DRIVER("Wait for render reset failed\n");
229 pci_write_config_byte(pdev, I915_GDRST, 0);
231 I915_WRITE_FW(VDECCLK_GATE_D,
232 I915_READ(VDECCLK_GATE_D) & ~VCP_UNIT_CLOCK_GATE_DISABLE);
233 POSTING_READ_FW(VDECCLK_GATE_D);
238 static int ironlake_do_reset(struct drm_i915_private *dev_priv,
239 unsigned int engine_mask,
244 I915_WRITE_FW(ILK_GDSR, ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
245 ret = __intel_wait_for_register_fw(dev_priv, ILK_GDSR,
246 ILK_GRDOM_RESET_ENABLE, 0,
250 DRM_DEBUG_DRIVER("Wait for render reset failed\n");
254 I915_WRITE_FW(ILK_GDSR, ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
255 ret = __intel_wait_for_register_fw(dev_priv, ILK_GDSR,
256 ILK_GRDOM_RESET_ENABLE, 0,
260 DRM_DEBUG_DRIVER("Wait for media reset failed\n");
265 I915_WRITE_FW(ILK_GDSR, 0);
266 POSTING_READ_FW(ILK_GDSR);
270 /* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
271 static int gen6_hw_domain_reset(struct drm_i915_private *dev_priv,
277 * GEN6_GDRST is not in the gt power well, no need to check
278 * for fifo space for the write or forcewake the chip for
281 I915_WRITE_FW(GEN6_GDRST, hw_domain_mask);
283 /* Wait for the device to ack the reset requests */
284 err = __intel_wait_for_register_fw(dev_priv,
285 GEN6_GDRST, hw_domain_mask, 0,
289 DRM_DEBUG_DRIVER("Wait for 0x%08x engines reset failed\n",
295 static int gen6_reset_engines(struct drm_i915_private *i915,
296 unsigned int engine_mask,
299 struct intel_engine_cs *engine;
300 const u32 hw_engine_mask[] = {
301 [RCS0] = GEN6_GRDOM_RENDER,
302 [BCS0] = GEN6_GRDOM_BLT,
303 [VCS0] = GEN6_GRDOM_MEDIA,
304 [VCS1] = GEN8_GRDOM_MEDIA2,
305 [VECS0] = GEN6_GRDOM_VECS,
309 if (engine_mask == ALL_ENGINES) {
310 hw_mask = GEN6_GRDOM_FULL;
315 for_each_engine_masked(engine, i915, engine_mask, tmp) {
316 GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
317 hw_mask |= hw_engine_mask[engine->id];
321 return gen6_hw_domain_reset(i915, hw_mask);
324 static u32 gen11_lock_sfc(struct drm_i915_private *dev_priv,
325 struct intel_engine_cs *engine)
327 u8 vdbox_sfc_access = RUNTIME_INFO(dev_priv)->vdbox_sfc_access;
328 i915_reg_t sfc_forced_lock, sfc_forced_lock_ack;
329 u32 sfc_forced_lock_bit, sfc_forced_lock_ack_bit;
330 i915_reg_t sfc_usage;
334 switch (engine->class) {
335 case VIDEO_DECODE_CLASS:
336 if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
339 sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
340 sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
342 sfc_forced_lock_ack = GEN11_VCS_SFC_LOCK_STATUS(engine);
343 sfc_forced_lock_ack_bit = GEN11_VCS_SFC_LOCK_ACK_BIT;
345 sfc_usage = GEN11_VCS_SFC_LOCK_STATUS(engine);
346 sfc_usage_bit = GEN11_VCS_SFC_USAGE_BIT;
347 sfc_reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
350 case VIDEO_ENHANCEMENT_CLASS:
351 sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
352 sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
354 sfc_forced_lock_ack = GEN11_VECS_SFC_LOCK_ACK(engine);
355 sfc_forced_lock_ack_bit = GEN11_VECS_SFC_LOCK_ACK_BIT;
357 sfc_usage = GEN11_VECS_SFC_USAGE(engine);
358 sfc_usage_bit = GEN11_VECS_SFC_USAGE_BIT;
359 sfc_reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
367 * Tell the engine that a software reset is going to happen. The engine
368 * will then try to force lock the SFC (if currently locked, it will
369 * remain so until we tell the engine it is safe to unlock; if currently
370 * unlocked, it will ignore this and all new lock requests). If SFC
371 * ends up being locked to the engine we want to reset, we have to reset
372 * it as well (we will unlock it once the reset sequence is completed).
374 I915_WRITE_FW(sfc_forced_lock,
375 I915_READ_FW(sfc_forced_lock) | sfc_forced_lock_bit);
377 if (__intel_wait_for_register_fw(dev_priv,
379 sfc_forced_lock_ack_bit,
380 sfc_forced_lock_ack_bit,
382 DRM_DEBUG_DRIVER("Wait for SFC forced lock ack failed\n");
386 if (I915_READ_FW(sfc_usage) & sfc_usage_bit)
387 return sfc_reset_bit;
392 static void gen11_unlock_sfc(struct drm_i915_private *dev_priv,
393 struct intel_engine_cs *engine)
395 u8 vdbox_sfc_access = RUNTIME_INFO(dev_priv)->vdbox_sfc_access;
396 i915_reg_t sfc_forced_lock;
397 u32 sfc_forced_lock_bit;
399 switch (engine->class) {
400 case VIDEO_DECODE_CLASS:
401 if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
404 sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
405 sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
408 case VIDEO_ENHANCEMENT_CLASS:
409 sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
410 sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
417 I915_WRITE_FW(sfc_forced_lock,
418 I915_READ_FW(sfc_forced_lock) & ~sfc_forced_lock_bit);
421 static int gen11_reset_engines(struct drm_i915_private *i915,
422 unsigned int engine_mask,
425 const u32 hw_engine_mask[] = {
426 [RCS0] = GEN11_GRDOM_RENDER,
427 [BCS0] = GEN11_GRDOM_BLT,
428 [VCS0] = GEN11_GRDOM_MEDIA,
429 [VCS1] = GEN11_GRDOM_MEDIA2,
430 [VCS2] = GEN11_GRDOM_MEDIA3,
431 [VCS3] = GEN11_GRDOM_MEDIA4,
432 [VECS0] = GEN11_GRDOM_VECS,
433 [VECS1] = GEN11_GRDOM_VECS2,
435 struct intel_engine_cs *engine;
440 if (engine_mask == ALL_ENGINES) {
441 hw_mask = GEN11_GRDOM_FULL;
444 for_each_engine_masked(engine, i915, engine_mask, tmp) {
445 GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
446 hw_mask |= hw_engine_mask[engine->id];
447 hw_mask |= gen11_lock_sfc(i915, engine);
451 ret = gen6_hw_domain_reset(i915, hw_mask);
453 if (engine_mask != ALL_ENGINES)
454 for_each_engine_masked(engine, i915, engine_mask, tmp)
455 gen11_unlock_sfc(i915, engine);
460 static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
462 struct drm_i915_private *dev_priv = engine->i915;
465 I915_WRITE_FW(RING_RESET_CTL(engine->mmio_base),
466 _MASKED_BIT_ENABLE(RESET_CTL_REQUEST_RESET));
468 ret = __intel_wait_for_register_fw(dev_priv,
469 RING_RESET_CTL(engine->mmio_base),
470 RESET_CTL_READY_TO_RESET,
471 RESET_CTL_READY_TO_RESET,
475 DRM_ERROR("%s: reset request timeout\n", engine->name);
480 static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
482 struct drm_i915_private *dev_priv = engine->i915;
484 I915_WRITE_FW(RING_RESET_CTL(engine->mmio_base),
485 _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
488 static int gen8_reset_engines(struct drm_i915_private *i915,
489 unsigned int engine_mask,
492 struct intel_engine_cs *engine;
493 const bool reset_non_ready = retry >= 1;
497 for_each_engine_masked(engine, i915, engine_mask, tmp) {
498 ret = gen8_engine_reset_prepare(engine);
499 if (ret && !reset_non_ready)
503 * If this is not the first failed attempt to prepare,
504 * we decide to proceed anyway.
506 * By doing so we risk context corruption and with
507 * some gens (kbl), possible system hang if reset
508 * happens during active bb execution.
510 * We rather take context corruption instead of
511 * failed reset with a wedged driver/gpu. And
512 * active bb execution case should be covered by
513 * i915_stop_engines we have before the reset.
517 if (INTEL_GEN(i915) >= 11)
518 ret = gen11_reset_engines(i915, engine_mask, retry);
520 ret = gen6_reset_engines(i915, engine_mask, retry);
523 for_each_engine_masked(engine, i915, engine_mask, tmp)
524 gen8_engine_reset_cancel(engine);
529 typedef int (*reset_func)(struct drm_i915_private *,
530 unsigned int engine_mask,
533 static reset_func intel_get_gpu_reset(struct drm_i915_private *i915)
535 if (INTEL_GEN(i915) >= 8)
536 return gen8_reset_engines;
537 else if (INTEL_GEN(i915) >= 6)
538 return gen6_reset_engines;
539 else if (INTEL_GEN(i915) >= 5)
540 return ironlake_do_reset;
541 else if (IS_G4X(i915))
543 else if (IS_G33(i915) || IS_PINEVIEW(i915))
545 else if (INTEL_GEN(i915) >= 3)
546 return i915_do_reset;
551 int intel_gpu_reset(struct drm_i915_private *i915, unsigned int engine_mask)
553 const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
555 int ret = -ETIMEDOUT;
558 reset = intel_get_gpu_reset(i915);
563 * If the power well sleeps during the reset, the reset
564 * request may be dropped and never completes (causing -EIO).
566 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
567 for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
569 * We stop engines, otherwise we might get failed reset and a
570 * dead gpu (on elk). Also as modern gpu as kbl can suffer
571 * from system hang if batchbuffer is progressing when
572 * the reset is issued, regardless of READY_TO_RESET ack.
573 * Thus assume it is best to stop engines on all gens
574 * where we have a gpu reset.
576 * WaKBLVECSSemaphoreWaitPoll:kbl (on ALL_ENGINES)
578 * WaMediaResetMainRingCleanup:ctg,elk (presumably)
580 * FIXME: Wa for more modern gens needs to be validated
583 i915_stop_engines(i915, engine_mask);
585 GEM_TRACE("engine_mask=%x\n", engine_mask);
587 ret = reset(i915, engine_mask, retry);
590 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
595 bool intel_has_gpu_reset(struct drm_i915_private *i915)
600 if (!i915_modparams.reset)
603 return intel_get_gpu_reset(i915);
606 bool intel_has_reset_engine(struct drm_i915_private *i915)
608 return INTEL_INFO(i915)->has_reset_engine && i915_modparams.reset >= 2;
611 int intel_reset_guc(struct drm_i915_private *i915)
614 INTEL_GEN(i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
617 GEM_BUG_ON(!HAS_GUC(i915));
619 intel_uncore_forcewake_get(i915, FORCEWAKE_ALL);
620 ret = gen6_hw_domain_reset(i915, guc_domain);
621 intel_uncore_forcewake_put(i915, FORCEWAKE_ALL);
627 * Ensure irq handler finishes, and not run again.
628 * Also return the active request so that we only search for it once.
630 static void reset_prepare_engine(struct intel_engine_cs *engine)
633 * During the reset sequence, we must prevent the engine from
634 * entering RC6. As the context state is undefined until we restart
635 * the engine, if it does enter RC6 during the reset, the state
636 * written to the powercontext is undefined and so we may lose
637 * GPU state upon resume, i.e. fail to restart after a reset.
639 intel_uncore_forcewake_get(engine->i915, FORCEWAKE_ALL);
640 engine->reset.prepare(engine);
643 static void revoke_mmaps(struct drm_i915_private *i915)
647 for (i = 0; i < i915->num_fence_regs; i++) {
648 struct drm_vma_offset_node *node;
649 struct i915_vma *vma;
652 vma = READ_ONCE(i915->fence_regs[i].vma);
656 if (!i915_vma_has_userfault(vma))
659 GEM_BUG_ON(vma->fence != &i915->fence_regs[i]);
660 node = &vma->obj->base.vma_node;
661 vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
662 unmap_mapping_range(i915->drm.anon_inode->i_mapping,
663 drm_vma_node_offset_addr(node) + vma_offset,
669 static void reset_prepare(struct drm_i915_private *i915)
671 struct intel_engine_cs *engine;
672 enum intel_engine_id id;
674 for_each_engine(engine, i915, id)
675 reset_prepare_engine(engine);
677 intel_uc_reset_prepare(i915);
681 static int gt_reset(struct drm_i915_private *i915, unsigned int stalled_mask)
683 struct intel_engine_cs *engine;
684 enum intel_engine_id id;
688 * Everything depends on having the GTT running, so we need to start
691 err = i915_ggtt_enable_hw(i915);
695 for_each_engine(engine, i915, id)
696 intel_engine_reset(engine, stalled_mask & engine->mask);
698 i915_gem_restore_fences(i915);
703 static void reset_finish_engine(struct intel_engine_cs *engine)
705 engine->reset.finish(engine);
706 intel_uncore_forcewake_put(engine->i915, FORCEWAKE_ALL);
709 struct i915_gpu_restart {
710 struct work_struct work;
711 struct drm_i915_private *i915;
714 static void restart_work(struct work_struct *work)
716 struct i915_gpu_restart *arg = container_of(work, typeof(*arg), work);
717 struct drm_i915_private *i915 = arg->i915;
718 struct intel_engine_cs *engine;
719 enum intel_engine_id id;
720 intel_wakeref_t wakeref;
722 wakeref = intel_runtime_pm_get(i915);
723 mutex_lock(&i915->drm.struct_mutex);
724 WRITE_ONCE(i915->gpu_error.restart, NULL);
726 for_each_engine(engine, i915, id) {
727 struct i915_request *rq;
730 * Ostensibily, we always want a context loaded for powersaving,
731 * so if the engine is idle after the reset, send a request
732 * to load our scratch kernel_context.
734 if (!intel_engine_is_idle(engine))
737 rq = i915_request_alloc(engine, i915->kernel_context);
739 i915_request_add(rq);
742 mutex_unlock(&i915->drm.struct_mutex);
743 intel_runtime_pm_put(i915, wakeref);
748 static void reset_finish(struct drm_i915_private *i915)
750 struct intel_engine_cs *engine;
751 enum intel_engine_id id;
753 for_each_engine(engine, i915, id)
754 reset_finish_engine(engine);
757 static void reset_restart(struct drm_i915_private *i915)
759 struct i915_gpu_restart *arg;
762 * Following the reset, ensure that we always reload context for
763 * powersaving, and to correct engine->last_retired_context. Since
764 * this requires us to submit a request, queue a worker to do that
765 * task for us to evade any locking here.
767 if (READ_ONCE(i915->gpu_error.restart))
770 arg = kmalloc(sizeof(*arg), GFP_KERNEL);
773 INIT_WORK(&arg->work, restart_work);
775 WRITE_ONCE(i915->gpu_error.restart, arg);
776 queue_work(i915->wq, &arg->work);
780 static void nop_submit_request(struct i915_request *request)
782 struct intel_engine_cs *engine = request->engine;
785 GEM_TRACE("%s fence %llx:%lld -> -EIO\n",
786 engine->name, request->fence.context, request->fence.seqno);
787 dma_fence_set_error(&request->fence, -EIO);
789 spin_lock_irqsave(&engine->timeline.lock, flags);
790 __i915_request_submit(request);
791 i915_request_mark_complete(request);
792 spin_unlock_irqrestore(&engine->timeline.lock, flags);
794 intel_engine_queue_breadcrumbs(engine);
797 static void __i915_gem_set_wedged(struct drm_i915_private *i915)
799 struct i915_gpu_error *error = &i915->gpu_error;
800 struct intel_engine_cs *engine;
801 enum intel_engine_id id;
803 if (test_bit(I915_WEDGED, &error->flags))
806 if (GEM_SHOW_DEBUG() && !intel_engines_are_idle(i915)) {
807 struct drm_printer p = drm_debug_printer(__func__);
809 for_each_engine(engine, i915, id)
810 intel_engine_dump(engine, &p, "%s\n", engine->name);
813 GEM_TRACE("start\n");
816 * First, stop submission to hw, but do not yet complete requests by
817 * rolling the global seqno forward (since this would complete requests
818 * for which we haven't set the fence error to EIO yet).
820 for_each_engine(engine, i915, id)
821 reset_prepare_engine(engine);
823 /* Even if the GPU reset fails, it should still stop the engines */
824 if (!INTEL_INFO(i915)->gpu_reset_clobbers_display)
825 intel_gpu_reset(i915, ALL_ENGINES);
827 for_each_engine(engine, i915, id) {
828 engine->submit_request = nop_submit_request;
829 engine->schedule = NULL;
831 i915->caps.scheduler = 0;
834 * Make sure no request can slip through without getting completed by
835 * either this call here to intel_engine_write_global_seqno, or the one
836 * in nop_submit_request.
838 synchronize_rcu_expedited();
840 /* Mark all executing requests as skipped */
841 for_each_engine(engine, i915, id)
842 engine->cancel_requests(engine);
844 for_each_engine(engine, i915, id) {
845 reset_finish_engine(engine);
846 intel_engine_signal_breadcrumbs(engine);
849 smp_mb__before_atomic();
850 set_bit(I915_WEDGED, &error->flags);
855 void i915_gem_set_wedged(struct drm_i915_private *i915)
857 struct i915_gpu_error *error = &i915->gpu_error;
859 mutex_lock(&error->wedge_mutex);
860 __i915_gem_set_wedged(i915);
861 mutex_unlock(&error->wedge_mutex);
864 static bool __i915_gem_unset_wedged(struct drm_i915_private *i915)
866 struct i915_gpu_error *error = &i915->gpu_error;
867 struct i915_timeline *tl;
869 if (!test_bit(I915_WEDGED, &error->flags))
872 if (!i915->gt.scratch) /* Never full initialised, recovery impossible */
875 GEM_TRACE("start\n");
878 * Before unwedging, make sure that all pending operations
879 * are flushed and errored out - we may have requests waiting upon
880 * third party fences. We marked all inflight requests as EIO, and
881 * every execbuf since returned EIO, for consistency we want all
882 * the currently pending requests to also be marked as EIO, which
883 * is done inside our nop_submit_request - and so we must wait.
885 * No more can be submitted until we reset the wedged bit.
887 mutex_lock(&i915->gt.timelines.mutex);
888 list_for_each_entry(tl, &i915->gt.timelines.active_list, link) {
889 struct i915_request *rq;
891 rq = i915_active_request_get_unlocked(&tl->last_request);
896 * All internal dependencies (i915_requests) will have
897 * been flushed by the set-wedge, but we may be stuck waiting
898 * for external fences. These should all be capped to 10s
899 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded
902 dma_fence_default_wait(&rq->fence, false, MAX_SCHEDULE_TIMEOUT);
903 i915_request_put(rq);
905 mutex_unlock(&i915->gt.timelines.mutex);
907 intel_engines_sanitize(i915, false);
910 * Undo nop_submit_request. We prevent all new i915 requests from
911 * being queued (by disallowing execbuf whilst wedged) so having
912 * waited for all active requests above, we know the system is idle
913 * and do not have to worry about a thread being inside
914 * engine->submit_request() as we swap over. So unlike installing
915 * the nop_submit_request on reset, we can do this from normal
916 * context and do not require stop_machine().
918 intel_engines_reset_default_submission(i915);
922 smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
923 clear_bit(I915_WEDGED, &i915->gpu_error.flags);
928 bool i915_gem_unset_wedged(struct drm_i915_private *i915)
930 struct i915_gpu_error *error = &i915->gpu_error;
933 mutex_lock(&error->wedge_mutex);
934 result = __i915_gem_unset_wedged(i915);
935 mutex_unlock(&error->wedge_mutex);
940 static int do_reset(struct drm_i915_private *i915, unsigned int stalled_mask)
944 err = intel_gpu_reset(i915, ALL_ENGINES);
945 for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
946 msleep(10 * (i + 1));
947 err = intel_gpu_reset(i915, ALL_ENGINES);
952 return gt_reset(i915, stalled_mask);
956 * i915_reset - reset chip after a hang
957 * @i915: #drm_i915_private to reset
958 * @stalled_mask: mask of the stalled engines with the guilty requests
959 * @reason: user error message for why we are resetting
961 * Reset the chip. Useful if a hang is detected. Marks the device as wedged
964 * Procedure is fairly simple:
965 * - reset the chip using the reset reg
966 * - re-init context state
967 * - re-init hardware status page
968 * - re-init ring buffer
969 * - re-init interrupt state
972 void i915_reset(struct drm_i915_private *i915,
973 unsigned int stalled_mask,
976 struct i915_gpu_error *error = &i915->gpu_error;
979 GEM_TRACE("flags=%lx\n", error->flags);
982 assert_rpm_wakelock_held(i915);
983 GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &error->flags));
985 /* Clear any previous failed attempts at recovery. Time to try again. */
986 if (!__i915_gem_unset_wedged(i915))
990 dev_notice(i915->drm.dev, "Resetting chip for %s\n", reason);
991 error->reset_count++;
995 if (!intel_has_gpu_reset(i915)) {
996 if (i915_modparams.reset)
997 dev_err(i915->drm.dev, "GPU reset not supported\n");
999 DRM_DEBUG_DRIVER("GPU reset disabled\n");
1003 if (INTEL_INFO(i915)->gpu_reset_clobbers_display)
1004 intel_runtime_pm_disable_interrupts(i915);
1006 if (do_reset(i915, stalled_mask)) {
1007 dev_err(i915->drm.dev, "Failed to reset chip\n");
1011 if (INTEL_INFO(i915)->gpu_reset_clobbers_display)
1012 intel_runtime_pm_enable_interrupts(i915);
1014 intel_overlay_reset(i915);
1017 * Next we need to restore the context, but we don't use those
1020 * Ring buffer needs to be re-initialized in the KMS case, or if X
1021 * was running at the time of the reset (i.e. we weren't VT
1024 ret = i915_gem_init_hw(i915);
1026 DRM_ERROR("Failed to initialise HW following reset (%d)\n",
1031 i915_queue_hangcheck(i915);
1035 if (!__i915_wedged(error))
1036 reset_restart(i915);
1041 * History tells us that if we cannot reset the GPU now, we
1042 * never will. This then impacts everything that is run
1043 * subsequently. On failing the reset, we mark the driver
1044 * as wedged, preventing further execution on the GPU.
1045 * We also want to go one step further and add a taint to the
1046 * kernel so that any subsequent faults can be traced back to
1047 * this failure. This is important for CI, where if the
1048 * GPU/driver fails we would like to reboot and restart testing
1049 * rather than continue on into oblivion. For everyone else,
1050 * the system should still plod along, but they have been warned!
1052 add_taint(TAINT_WARN, LOCKDEP_STILL_OK);
1054 __i915_gem_set_wedged(i915);
1058 static inline int intel_gt_reset_engine(struct drm_i915_private *i915,
1059 struct intel_engine_cs *engine)
1061 return intel_gpu_reset(i915, engine->mask);
1065 * i915_reset_engine - reset GPU engine to recover from a hang
1066 * @engine: engine to reset
1067 * @msg: reason for GPU reset; or NULL for no dev_notice()
1069 * Reset a specific GPU engine. Useful if a hang is detected.
1070 * Returns zero on successful reset or otherwise an error code.
1073 * - identifies the request that caused the hang and it is dropped
1074 * - reset engine (which will force the engine to idle)
1075 * - re-init/configure engine
1077 int i915_reset_engine(struct intel_engine_cs *engine, const char *msg)
1079 struct i915_gpu_error *error = &engine->i915->gpu_error;
1082 GEM_TRACE("%s flags=%lx\n", engine->name, error->flags);
1083 GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &error->flags));
1085 reset_prepare_engine(engine);
1088 dev_notice(engine->i915->drm.dev,
1089 "Resetting %s for %s\n", engine->name, msg);
1090 error->reset_engine_count[engine->id]++;
1092 if (!engine->i915->guc.execbuf_client)
1093 ret = intel_gt_reset_engine(engine->i915, engine);
1095 ret = intel_guc_reset_engine(&engine->i915->guc, engine);
1097 /* If we fail here, we expect to fallback to a global reset */
1098 DRM_DEBUG_DRIVER("%sFailed to reset %s, ret=%d\n",
1099 engine->i915->guc.execbuf_client ? "GuC " : "",
1105 * The request that caused the hang is stuck on elsp, we know the
1106 * active request and can drop it, adjust head to skip the offending
1107 * request to resume executing remaining requests in the queue.
1109 intel_engine_reset(engine, true);
1112 * The engine and its registers (and workarounds in case of render)
1113 * have been reset to their default values. Follow the init_ring
1114 * process to program RING_MODE, HWSP and re-enable submission.
1116 ret = engine->init_hw(engine);
1121 intel_engine_cancel_stop_cs(engine);
1122 reset_finish_engine(engine);
1126 static void i915_reset_device(struct drm_i915_private *i915,
1130 struct i915_gpu_error *error = &i915->gpu_error;
1131 struct kobject *kobj = &i915->drm.primary->kdev->kobj;
1132 char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1133 char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1134 char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1135 struct i915_wedge_me w;
1137 kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
1139 DRM_DEBUG_DRIVER("resetting chip\n");
1140 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
1142 /* Use a watchdog to ensure that our reset completes */
1143 i915_wedge_on_timeout(&w, i915, 5 * HZ) {
1144 intel_prepare_reset(i915);
1146 /* Flush everyone using a resource about to be clobbered */
1147 synchronize_srcu_expedited(&error->reset_backoff_srcu);
1149 mutex_lock(&error->wedge_mutex);
1150 i915_reset(i915, engine_mask, reason);
1151 mutex_unlock(&error->wedge_mutex);
1153 intel_finish_reset(i915);
1156 if (!test_bit(I915_WEDGED, &error->flags))
1157 kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
1160 void i915_clear_error_registers(struct drm_i915_private *dev_priv)
1164 if (!IS_GEN(dev_priv, 2))
1165 I915_WRITE(PGTBL_ER, I915_READ(PGTBL_ER));
1167 if (INTEL_GEN(dev_priv) < 4)
1168 I915_WRITE(IPEIR, I915_READ(IPEIR));
1170 I915_WRITE(IPEIR_I965, I915_READ(IPEIR_I965));
1172 I915_WRITE(EIR, I915_READ(EIR));
1173 eir = I915_READ(EIR);
1176 * some errors might have become stuck,
1179 DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
1180 I915_WRITE(EMR, I915_READ(EMR) | eir);
1181 I915_WRITE(IIR, I915_MASTER_ERROR_INTERRUPT);
1184 if (INTEL_GEN(dev_priv) >= 8) {
1185 I915_WRITE(GEN8_RING_FAULT_REG,
1186 I915_READ(GEN8_RING_FAULT_REG) & ~RING_FAULT_VALID);
1187 POSTING_READ(GEN8_RING_FAULT_REG);
1188 } else if (INTEL_GEN(dev_priv) >= 6) {
1189 struct intel_engine_cs *engine;
1190 enum intel_engine_id id;
1192 for_each_engine(engine, dev_priv, id) {
1193 I915_WRITE(RING_FAULT_REG(engine),
1194 I915_READ(RING_FAULT_REG(engine)) &
1197 POSTING_READ(RING_FAULT_REG(dev_priv->engine[RCS0]));
1202 * i915_handle_error - handle a gpu error
1203 * @i915: i915 device private
1204 * @engine_mask: mask representing engines that are hung
1205 * @flags: control flags
1206 * @fmt: Error message format string
1208 * Do some basic checking of register state at error time and
1209 * dump it to the syslog. Also call i915_capture_error_state() to make
1210 * sure we get a record and make it available in debugfs. Fire a uevent
1211 * so userspace knows something bad happened (should trigger collection
1212 * of a ring dump etc.).
1214 void i915_handle_error(struct drm_i915_private *i915,
1216 unsigned long flags,
1217 const char *fmt, ...)
1219 struct i915_gpu_error *error = &i915->gpu_error;
1220 struct intel_engine_cs *engine;
1221 intel_wakeref_t wakeref;
1229 va_start(args, fmt);
1230 vscnprintf(error_msg, sizeof(error_msg), fmt, args);
1237 * In most cases it's guaranteed that we get here with an RPM
1238 * reference held, for example because there is a pending GPU
1239 * request that won't finish until the reset is done. This
1240 * isn't the case at least when we get here by doing a
1241 * simulated reset via debugfs, so get an RPM reference.
1243 wakeref = intel_runtime_pm_get(i915);
1245 engine_mask &= INTEL_INFO(i915)->engine_mask;
1247 if (flags & I915_ERROR_CAPTURE) {
1248 i915_capture_error_state(i915, engine_mask, msg);
1249 i915_clear_error_registers(i915);
1253 * Try engine reset when available. We fall back to full reset if
1254 * single reset fails.
1256 if (intel_has_reset_engine(i915) && !__i915_wedged(error)) {
1257 for_each_engine_masked(engine, i915, engine_mask, tmp) {
1258 BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
1259 if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1263 if (i915_reset_engine(engine, msg) == 0)
1264 engine_mask &= ~engine->mask;
1266 clear_bit(I915_RESET_ENGINE + engine->id,
1268 wake_up_bit(&error->flags,
1269 I915_RESET_ENGINE + engine->id);
1276 /* Full reset needs the mutex, stop any other user trying to do so. */
1277 if (test_and_set_bit(I915_RESET_BACKOFF, &error->flags)) {
1278 wait_event(error->reset_queue,
1279 !test_bit(I915_RESET_BACKOFF, &error->flags));
1280 goto out; /* piggy-back on the other reset */
1283 /* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
1284 synchronize_rcu_expedited();
1286 /* Prevent any other reset-engine attempt. */
1287 for_each_engine(engine, i915, tmp) {
1288 while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1290 wait_on_bit(&error->flags,
1291 I915_RESET_ENGINE + engine->id,
1292 TASK_UNINTERRUPTIBLE);
1295 i915_reset_device(i915, engine_mask, msg);
1297 for_each_engine(engine, i915, tmp) {
1298 clear_bit(I915_RESET_ENGINE + engine->id,
1302 clear_bit(I915_RESET_BACKOFF, &error->flags);
1303 wake_up_all(&error->reset_queue);
1306 intel_runtime_pm_put(i915, wakeref);
1309 int i915_reset_trylock(struct drm_i915_private *i915)
1311 struct i915_gpu_error *error = &i915->gpu_error;
1314 might_lock(&error->reset_backoff_srcu);
1318 while (test_bit(I915_RESET_BACKOFF, &error->flags)) {
1321 if (wait_event_interruptible(error->reset_queue,
1322 !test_bit(I915_RESET_BACKOFF,
1328 srcu = srcu_read_lock(&error->reset_backoff_srcu);
1334 void i915_reset_unlock(struct drm_i915_private *i915, int tag)
1335 __releases(&i915->gpu_error.reset_backoff_srcu)
1337 struct i915_gpu_error *error = &i915->gpu_error;
1339 srcu_read_unlock(&error->reset_backoff_srcu, tag);
1342 int i915_terminally_wedged(struct drm_i915_private *i915)
1344 struct i915_gpu_error *error = &i915->gpu_error;
1348 if (!__i915_wedged(error))
1351 /* Reset still in progress? Maybe we will recover? */
1352 if (!test_bit(I915_RESET_BACKOFF, &error->flags))
1355 /* XXX intel_reset_finish() still takes struct_mutex!!! */
1356 if (mutex_is_locked(&i915->drm.struct_mutex))
1359 if (wait_event_interruptible(error->reset_queue,
1360 !test_bit(I915_RESET_BACKOFF,
1364 return __i915_wedged(error) ? -EIO : 0;
1367 bool i915_reset_flush(struct drm_i915_private *i915)
1371 cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work);
1373 flush_workqueue(i915->wq);
1374 GEM_BUG_ON(READ_ONCE(i915->gpu_error.restart));
1376 mutex_lock(&i915->drm.struct_mutex);
1377 err = i915_gem_wait_for_idle(i915,
1379 I915_WAIT_FOR_IDLE_BOOST,
1380 MAX_SCHEDULE_TIMEOUT);
1381 mutex_unlock(&i915->drm.struct_mutex);
1386 static void i915_wedge_me(struct work_struct *work)
1388 struct i915_wedge_me *w = container_of(work, typeof(*w), work.work);
1390 dev_err(w->i915->drm.dev,
1391 "%s timed out, cancelling all in-flight rendering.\n",
1393 i915_gem_set_wedged(w->i915);
1396 void __i915_init_wedge(struct i915_wedge_me *w,
1397 struct drm_i915_private *i915,
1404 INIT_DELAYED_WORK_ONSTACK(&w->work, i915_wedge_me);
1405 schedule_delayed_work(&w->work, timeout);
1408 void __i915_fini_wedge(struct i915_wedge_me *w)
1410 cancel_delayed_work_sync(&w->work);
1411 destroy_delayed_work_on_stack(&w->work);