xref: /openbmc/linux/drivers/gpu/drm/i915/gt/intel_reset.c (revision ec84b2a4)
124f90d66SChris Wilson // SPDX-License-Identifier: MIT
2112ed2d3SChris Wilson /*
3112ed2d3SChris Wilson  * Copyright © 2008-2018 Intel Corporation
4112ed2d3SChris Wilson  */
5112ed2d3SChris Wilson 
6112ed2d3SChris Wilson #include <linux/sched/mm.h>
7112ed2d3SChris Wilson #include <linux/stop_machine.h>
801fabda8SLucas De Marchi #include <linux/string_helpers.h>
9112ed2d3SChris Wilson 
1059c6106eSJani Nikula #include "display/intel_display_reset.h"
11df0566a6SJani Nikula #include "display/intel_overlay.h"
12df0566a6SJani Nikula 
1310be98a7SChris Wilson #include "gem/i915_gem_context.h"
1410be98a7SChris Wilson 
150d6419e9SMatt Roper #include "gt/intel_gt_regs.h"
160d6419e9SMatt Roper 
17b7d70b8bSDaniele Ceraolo Spurio #include "gt/uc/intel_gsc_fw.h"
18b7d70b8bSDaniele Ceraolo Spurio 
19112ed2d3SChris Wilson #include "i915_drv.h"
205472b3f2SJani Nikula #include "i915_file_private.h"
21112ed2d3SChris Wilson #include "i915_gpu_error.h"
22440e2b3dSJani Nikula #include "i915_irq.h"
23476f62b8SJani Nikula #include "i915_reg.h"
24b3786b29SChris Wilson #include "intel_breadcrumbs.h"
2579ffac85SChris Wilson #include "intel_engine_pm.h"
26202b1f4cSMatt Roper #include "intel_engine_regs.h"
27eaf522f6STvrtko Ursulin #include "intel_gt.h"
2879ffac85SChris Wilson #include "intel_gt_pm.h"
29b0573472SChris Wilson #include "intel_gt_requests.h"
30e30e6c7bSMatt Roper #include "intel_mchbar_regs.h"
317e470f10SJani Nikula #include "intel_pci_config.h"
32112ed2d3SChris Wilson #include "intel_reset.h"
33112ed2d3SChris Wilson 
340f261b24SDaniele Ceraolo Spurio #include "uc/intel_guc.h"
35112ed2d3SChris Wilson 
36112ed2d3SChris Wilson #define RESET_MAX_RETRIES 3
37112ed2d3SChris Wilson 
client_mark_guilty(struct i915_gem_context * ctx,bool banned)38e6ba7648SChris Wilson static void client_mark_guilty(struct i915_gem_context *ctx, bool banned)
39112ed2d3SChris Wilson {
409f3ccd40SChris Wilson 	struct drm_i915_file_private *file_priv = ctx->file_priv;
41112ed2d3SChris Wilson 	unsigned long prev_hang;
429f3ccd40SChris Wilson 	unsigned int score;
43112ed2d3SChris Wilson 
449f3ccd40SChris Wilson 	if (IS_ERR_OR_NULL(file_priv))
459f3ccd40SChris Wilson 		return;
469f3ccd40SChris Wilson 
47112ed2d3SChris Wilson 	score = 0;
489f3ccd40SChris Wilson 	if (banned)
499f3ccd40SChris Wilson 		score = I915_CLIENT_SCORE_CONTEXT_BAN;
50112ed2d3SChris Wilson 
51112ed2d3SChris Wilson 	prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
52112ed2d3SChris Wilson 	if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
53112ed2d3SChris Wilson 		score += I915_CLIENT_SCORE_HANG_FAST;
54112ed2d3SChris Wilson 
55112ed2d3SChris Wilson 	if (score) {
56112ed2d3SChris Wilson 		atomic_add(score, &file_priv->ban_score);
57112ed2d3SChris Wilson 
58f8474622SWambui Karuga 		drm_dbg(&ctx->i915->drm,
59f8474622SWambui Karuga 			"client %s: gained %u ban score, now %u\n",
60112ed2d3SChris Wilson 			ctx->name, score,
61112ed2d3SChris Wilson 			atomic_read(&file_priv->ban_score));
62112ed2d3SChris Wilson 	}
63112ed2d3SChris Wilson }
64112ed2d3SChris Wilson 
mark_guilty(struct i915_request * rq)659f3ccd40SChris Wilson static bool mark_guilty(struct i915_request *rq)
66112ed2d3SChris Wilson {
67e6ba7648SChris Wilson 	struct i915_gem_context *ctx;
68112ed2d3SChris Wilson 	unsigned long prev_hang;
69112ed2d3SChris Wilson 	bool banned;
70112ed2d3SChris Wilson 	int i;
71112ed2d3SChris Wilson 
72ae8ac10dSMatthew Brost 	if (intel_context_is_closed(rq->context))
738e37d699SChris Wilson 		return true;
748e37d699SChris Wilson 
756a8679c0SChris Wilson 	rcu_read_lock();
766a8679c0SChris Wilson 	ctx = rcu_dereference(rq->context->gem_context);
776a8679c0SChris Wilson 	if (ctx && !kref_get_unless_zero(&ctx->ref))
786a8679c0SChris Wilson 		ctx = NULL;
796a8679c0SChris Wilson 	rcu_read_unlock();
80e6ba7648SChris Wilson 	if (!ctx)
81be90e344SChris Wilson 		return intel_context_is_banned(rq->context);
82e8887bb3SChris Wilson 
83112ed2d3SChris Wilson 	atomic_inc(&ctx->guilty_count);
84112ed2d3SChris Wilson 
85112ed2d3SChris Wilson 	/* Cool contexts are too cool to be banned! (Used for reset testing.) */
866a8679c0SChris Wilson 	if (!i915_gem_context_is_bannable(ctx)) {
876a8679c0SChris Wilson 		banned = false;
886a8679c0SChris Wilson 		goto out;
896a8679c0SChris Wilson 	}
90112ed2d3SChris Wilson 
91dc483ba5SJani Nikula 	drm_notice(&ctx->i915->drm,
92dfd9c1b4SChris Wilson 		   "%s context reset due to GPU hang\n",
93dfd9c1b4SChris Wilson 		   ctx->name);
94dfd9c1b4SChris Wilson 
95112ed2d3SChris Wilson 	/* Record the timestamp for the last N hangs */
96112ed2d3SChris Wilson 	prev_hang = ctx->hang_timestamp[0];
97112ed2d3SChris Wilson 	for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++)
98112ed2d3SChris Wilson 		ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1];
99112ed2d3SChris Wilson 	ctx->hang_timestamp[i] = jiffies;
100112ed2d3SChris Wilson 
101112ed2d3SChris Wilson 	/* If we have hung N+1 times in rapid succession, we ban the context! */
102112ed2d3SChris Wilson 	banned = !i915_gem_context_is_recoverable(ctx);
103112ed2d3SChris Wilson 	if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES))
104112ed2d3SChris Wilson 		banned = true;
105ae8ac10dSMatthew Brost 	if (banned)
106f8474622SWambui Karuga 		drm_dbg(&ctx->i915->drm, "context %s: guilty %d, banned\n",
107112ed2d3SChris Wilson 			ctx->name, atomic_read(&ctx->guilty_count));
108112ed2d3SChris Wilson 
109e6ba7648SChris Wilson 	client_mark_guilty(ctx, banned);
110112ed2d3SChris Wilson 
1116a8679c0SChris Wilson out:
1126a8679c0SChris Wilson 	i915_gem_context_put(ctx);
113112ed2d3SChris Wilson 	return banned;
114112ed2d3SChris Wilson }
115112ed2d3SChris Wilson 
mark_innocent(struct i915_request * rq)1169f3ccd40SChris Wilson static void mark_innocent(struct i915_request *rq)
117112ed2d3SChris Wilson {
1186a8679c0SChris Wilson 	struct i915_gem_context *ctx;
1196a8679c0SChris Wilson 
1206a8679c0SChris Wilson 	rcu_read_lock();
1216a8679c0SChris Wilson 	ctx = rcu_dereference(rq->context->gem_context);
1226a8679c0SChris Wilson 	if (ctx)
1236a8679c0SChris Wilson 		atomic_inc(&ctx->active_count);
1246a8679c0SChris Wilson 	rcu_read_unlock();
125112ed2d3SChris Wilson }
126112ed2d3SChris Wilson 
__i915_request_reset(struct i915_request * rq,bool guilty)127cb823ed9SChris Wilson void __i915_request_reset(struct i915_request *rq, bool guilty)
128112ed2d3SChris Wilson {
129ae8ac10dSMatthew Brost 	bool banned = false;
130ae8ac10dSMatthew Brost 
13101fabda8SLucas De Marchi 	RQ_TRACE(rq, "guilty? %s\n", str_yes_no(guilty));
132163433e5SChris Wilson 	GEM_BUG_ON(__i915_request_is_complete(rq));
133112ed2d3SChris Wilson 
134e8887bb3SChris Wilson 	rcu_read_lock(); /* protect the GEM context */
135112ed2d3SChris Wilson 	if (guilty) {
13636e191f0SChris Wilson 		i915_request_set_error_once(rq, -EIO);
13736e191f0SChris Wilson 		__i915_request_skip(rq);
138ae8ac10dSMatthew Brost 		banned = mark_guilty(rq);
139112ed2d3SChris Wilson 	} else {
14036e191f0SChris Wilson 		i915_request_set_error_once(rq, -EAGAIN);
1419f3ccd40SChris Wilson 		mark_innocent(rq);
142112ed2d3SChris Wilson 	}
143e8887bb3SChris Wilson 	rcu_read_unlock();
144ae8ac10dSMatthew Brost 
145ae8ac10dSMatthew Brost 	if (banned)
146ae8ac10dSMatthew Brost 		intel_context_ban(rq->context, rq);
147112ed2d3SChris Wilson }
148112ed2d3SChris Wilson 
i915_in_reset(struct pci_dev * pdev)149112ed2d3SChris Wilson static bool i915_in_reset(struct pci_dev *pdev)
150112ed2d3SChris Wilson {
151112ed2d3SChris Wilson 	u8 gdrst;
152112ed2d3SChris Wilson 
153112ed2d3SChris Wilson 	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
154112ed2d3SChris Wilson 	return gdrst & GRDOM_RESET_STATUS;
155112ed2d3SChris Wilson }
156112ed2d3SChris Wilson 
i915_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)157cb823ed9SChris Wilson static int i915_do_reset(struct intel_gt *gt,
158112ed2d3SChris Wilson 			 intel_engine_mask_t engine_mask,
159112ed2d3SChris Wilson 			 unsigned int retry)
160112ed2d3SChris Wilson {
161e322551fSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
162112ed2d3SChris Wilson 	int err;
163112ed2d3SChris Wilson 
164112ed2d3SChris Wilson 	/* Assert reset for at least 20 usec, and wait for acknowledgement. */
165112ed2d3SChris Wilson 	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
166112ed2d3SChris Wilson 	udelay(50);
167112ed2d3SChris Wilson 	err = wait_for_atomic(i915_in_reset(pdev), 50);
168112ed2d3SChris Wilson 
169112ed2d3SChris Wilson 	/* Clear the reset request. */
170112ed2d3SChris Wilson 	pci_write_config_byte(pdev, I915_GDRST, 0);
171112ed2d3SChris Wilson 	udelay(50);
172112ed2d3SChris Wilson 	if (!err)
173112ed2d3SChris Wilson 		err = wait_for_atomic(!i915_in_reset(pdev), 50);
174112ed2d3SChris Wilson 
175112ed2d3SChris Wilson 	return err;
176112ed2d3SChris Wilson }
177112ed2d3SChris Wilson 
g4x_reset_complete(struct pci_dev * pdev)178112ed2d3SChris Wilson static bool g4x_reset_complete(struct pci_dev *pdev)
179112ed2d3SChris Wilson {
180112ed2d3SChris Wilson 	u8 gdrst;
181112ed2d3SChris Wilson 
182112ed2d3SChris Wilson 	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
183112ed2d3SChris Wilson 	return (gdrst & GRDOM_RESET_ENABLE) == 0;
184112ed2d3SChris Wilson }
185112ed2d3SChris Wilson 
g33_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)186cb823ed9SChris Wilson static int g33_do_reset(struct intel_gt *gt,
187112ed2d3SChris Wilson 			intel_engine_mask_t engine_mask,
188112ed2d3SChris Wilson 			unsigned int retry)
189112ed2d3SChris Wilson {
190e322551fSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
191112ed2d3SChris Wilson 
192112ed2d3SChris Wilson 	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
193112ed2d3SChris Wilson 	return wait_for_atomic(g4x_reset_complete(pdev), 50);
194112ed2d3SChris Wilson }
195112ed2d3SChris Wilson 
g4x_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)196cb823ed9SChris Wilson static int g4x_do_reset(struct intel_gt *gt,
197112ed2d3SChris Wilson 			intel_engine_mask_t engine_mask,
198112ed2d3SChris Wilson 			unsigned int retry)
199112ed2d3SChris Wilson {
200e322551fSThomas Zimmermann 	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
201cb823ed9SChris Wilson 	struct intel_uncore *uncore = gt->uncore;
202112ed2d3SChris Wilson 	int ret;
203112ed2d3SChris Wilson 
204112ed2d3SChris Wilson 	/* WaVcpClkGateDisableForMediaReset:ctg,elk */
2054050e6f2SJani Nikula 	intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, 0, VCP_UNIT_CLOCK_GATE_DISABLE);
206112ed2d3SChris Wilson 	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
207112ed2d3SChris Wilson 
208112ed2d3SChris Wilson 	pci_write_config_byte(pdev, I915_GDRST,
209112ed2d3SChris Wilson 			      GRDOM_MEDIA | GRDOM_RESET_ENABLE);
210112ed2d3SChris Wilson 	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
211112ed2d3SChris Wilson 	if (ret) {
212cb56a07dSChris Wilson 		GT_TRACE(gt, "Wait for media reset failed\n");
213112ed2d3SChris Wilson 		goto out;
214112ed2d3SChris Wilson 	}
215112ed2d3SChris Wilson 
216112ed2d3SChris Wilson 	pci_write_config_byte(pdev, I915_GDRST,
217112ed2d3SChris Wilson 			      GRDOM_RENDER | GRDOM_RESET_ENABLE);
218112ed2d3SChris Wilson 	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
219112ed2d3SChris Wilson 	if (ret) {
220cb56a07dSChris Wilson 		GT_TRACE(gt, "Wait for render reset failed\n");
221112ed2d3SChris Wilson 		goto out;
222112ed2d3SChris Wilson 	}
223112ed2d3SChris Wilson 
224112ed2d3SChris Wilson out:
225112ed2d3SChris Wilson 	pci_write_config_byte(pdev, I915_GDRST, 0);
226112ed2d3SChris Wilson 
2274050e6f2SJani Nikula 	intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE, 0);
228112ed2d3SChris Wilson 	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
229112ed2d3SChris Wilson 
230112ed2d3SChris Wilson 	return ret;
231112ed2d3SChris Wilson }
232112ed2d3SChris Wilson 
ilk_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)2339eae5e27SLucas De Marchi static int ilk_do_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask,
234112ed2d3SChris Wilson 			unsigned int retry)
235112ed2d3SChris Wilson {
236cb823ed9SChris Wilson 	struct intel_uncore *uncore = gt->uncore;
237112ed2d3SChris Wilson 	int ret;
238112ed2d3SChris Wilson 
239112ed2d3SChris Wilson 	intel_uncore_write_fw(uncore, ILK_GDSR,
240112ed2d3SChris Wilson 			      ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
241112ed2d3SChris Wilson 	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
242112ed2d3SChris Wilson 					   ILK_GRDOM_RESET_ENABLE, 0,
243112ed2d3SChris Wilson 					   5000, 0,
244112ed2d3SChris Wilson 					   NULL);
245112ed2d3SChris Wilson 	if (ret) {
246cb56a07dSChris Wilson 		GT_TRACE(gt, "Wait for render reset failed\n");
247112ed2d3SChris Wilson 		goto out;
248112ed2d3SChris Wilson 	}
249112ed2d3SChris Wilson 
250112ed2d3SChris Wilson 	intel_uncore_write_fw(uncore, ILK_GDSR,
251112ed2d3SChris Wilson 			      ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
252112ed2d3SChris Wilson 	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
253112ed2d3SChris Wilson 					   ILK_GRDOM_RESET_ENABLE, 0,
254112ed2d3SChris Wilson 					   5000, 0,
255112ed2d3SChris Wilson 					   NULL);
256112ed2d3SChris Wilson 	if (ret) {
257cb56a07dSChris Wilson 		GT_TRACE(gt, "Wait for media reset failed\n");
258112ed2d3SChris Wilson 		goto out;
259112ed2d3SChris Wilson 	}
260112ed2d3SChris Wilson 
261112ed2d3SChris Wilson out:
262112ed2d3SChris Wilson 	intel_uncore_write_fw(uncore, ILK_GDSR, 0);
263112ed2d3SChris Wilson 	intel_uncore_posting_read_fw(uncore, ILK_GDSR);
264112ed2d3SChris Wilson 	return ret;
265112ed2d3SChris Wilson }
266112ed2d3SChris Wilson 
267112ed2d3SChris Wilson /* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
gen6_hw_domain_reset(struct intel_gt * gt,u32 hw_domain_mask)268cb823ed9SChris Wilson static int gen6_hw_domain_reset(struct intel_gt *gt, u32 hw_domain_mask)
269112ed2d3SChris Wilson {
270cb823ed9SChris Wilson 	struct intel_uncore *uncore = gt->uncore;
271625af472SDaniele Ceraolo Spurio 	int loops;
272112ed2d3SChris Wilson 	int err;
273112ed2d3SChris Wilson 
274112ed2d3SChris Wilson 	/*
275625af472SDaniele Ceraolo Spurio 	 * On some platforms, e.g. Jasperlake, we see that the engine register
276625af472SDaniele Ceraolo Spurio 	 * state is not cleared until shortly after GDRST reports completion,
277625af472SDaniele Ceraolo Spurio 	 * causing a failure as we try to immediately resume while the internal
278625af472SDaniele Ceraolo Spurio 	 * state is still in flux. If we immediately repeat the reset, the
279625af472SDaniele Ceraolo Spurio 	 * second reset appears to serialise with the first, and since it is a
280625af472SDaniele Ceraolo Spurio 	 * no-op, the registers should retain their reset value. However, there
281625af472SDaniele Ceraolo Spurio 	 * is still a concern that upon leaving the second reset, the internal
282625af472SDaniele Ceraolo Spurio 	 * engine state is still in flux and not ready for resuming.
283625af472SDaniele Ceraolo Spurio 	 *
284625af472SDaniele Ceraolo Spurio 	 * Starting on MTL, there are some prep steps that we need to do when
285625af472SDaniele Ceraolo Spurio 	 * resetting some engines that need to be applied every time we write to
286625af472SDaniele Ceraolo Spurio 	 * GEN6_GDRST. As those are time consuming (tens of ms), we don't want
287625af472SDaniele Ceraolo Spurio 	 * to perform that twice, so, since the Jasperlake issue hasn't been
288625af472SDaniele Ceraolo Spurio 	 * observed on MTL, we avoid repeating the reset on newer platforms.
289625af472SDaniele Ceraolo Spurio 	 */
290625af472SDaniele Ceraolo Spurio 	loops = GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 70) ? 2 : 1;
291625af472SDaniele Ceraolo Spurio 
292625af472SDaniele Ceraolo Spurio 	/*
293112ed2d3SChris Wilson 	 * GEN6_GDRST is not in the gt power well, no need to check
294112ed2d3SChris Wilson 	 * for fifo space for the write or forcewake the chip for
295112ed2d3SChris Wilson 	 * the read
296112ed2d3SChris Wilson 	 */
2973db9d590SChris Wilson 	do {
298112ed2d3SChris Wilson 		intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask);
299112ed2d3SChris Wilson 
300625af472SDaniele Ceraolo Spurio 		/* Wait for the device to ack the reset requests. */
3013db9d590SChris Wilson 		err = __intel_wait_for_register_fw(uncore, GEN6_GDRST,
3023db9d590SChris Wilson 						   hw_domain_mask, 0,
3033db9d590SChris Wilson 						   2000, 0,
304112ed2d3SChris Wilson 						   NULL);
3053db9d590SChris Wilson 	} while (err == 0 && --loops);
306112ed2d3SChris Wilson 	if (err)
307cb56a07dSChris Wilson 		GT_TRACE(gt,
308f8474622SWambui Karuga 			 "Wait for 0x%08x engines reset failed\n",
309112ed2d3SChris Wilson 			 hw_domain_mask);
310112ed2d3SChris Wilson 
3113db9d590SChris Wilson 	/*
3123db9d590SChris Wilson 	 * As we have observed that the engine state is still volatile
3133db9d590SChris Wilson 	 * after GDRST is acked, impose a small delay to let everything settle.
3143db9d590SChris Wilson 	 */
3153db9d590SChris Wilson 	udelay(50);
3163db9d590SChris Wilson 
317112ed2d3SChris Wilson 	return err;
318112ed2d3SChris Wilson }
319112ed2d3SChris Wilson 
__gen6_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)320336561a9SChris Wilson static int __gen6_reset_engines(struct intel_gt *gt,
321112ed2d3SChris Wilson 				intel_engine_mask_t engine_mask,
322112ed2d3SChris Wilson 				unsigned int retry)
323112ed2d3SChris Wilson {
324b9dcb97bSColin Ian King 	struct intel_engine_cs *engine;
325112ed2d3SChris Wilson 	u32 hw_mask;
326112ed2d3SChris Wilson 
327112ed2d3SChris Wilson 	if (engine_mask == ALL_ENGINES) {
328112ed2d3SChris Wilson 		hw_mask = GEN6_GRDOM_FULL;
329112ed2d3SChris Wilson 	} else {
330112ed2d3SChris Wilson 		intel_engine_mask_t tmp;
331112ed2d3SChris Wilson 
332112ed2d3SChris Wilson 		hw_mask = 0;
333a50134b1STvrtko Ursulin 		for_each_engine_masked(engine, gt, engine_mask, tmp) {
33420cddfccSTejas Upadhyay 			hw_mask |= engine->reset_domain;
335112ed2d3SChris Wilson 		}
336112ed2d3SChris Wilson 	}
337112ed2d3SChris Wilson 
338cb823ed9SChris Wilson 	return gen6_hw_domain_reset(gt, hw_mask);
339112ed2d3SChris Wilson }
340112ed2d3SChris Wilson 
gen6_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)341336561a9SChris Wilson static int gen6_reset_engines(struct intel_gt *gt,
342336561a9SChris Wilson 			      intel_engine_mask_t engine_mask,
343336561a9SChris Wilson 			      unsigned int retry)
344336561a9SChris Wilson {
345336561a9SChris Wilson 	unsigned long flags;
346336561a9SChris Wilson 	int ret;
347336561a9SChris Wilson 
348336561a9SChris Wilson 	spin_lock_irqsave(&gt->uncore->lock, flags);
349336561a9SChris Wilson 	ret = __gen6_reset_engines(gt, engine_mask, retry);
350336561a9SChris Wilson 	spin_unlock_irqrestore(&gt->uncore->lock, flags);
351336561a9SChris Wilson 
352336561a9SChris Wilson 	return ret;
353336561a9SChris Wilson }
354336561a9SChris Wilson 
find_sfc_paired_vecs_engine(struct intel_engine_cs * engine)3555b26d57fSAditya Swarup static struct intel_engine_cs *find_sfc_paired_vecs_engine(struct intel_engine_cs *engine)
3565b26d57fSAditya Swarup {
3575b26d57fSAditya Swarup 	int vecs_id;
3585b26d57fSAditya Swarup 
3595b26d57fSAditya Swarup 	GEM_BUG_ON(engine->class != VIDEO_DECODE_CLASS);
3605b26d57fSAditya Swarup 
3615b26d57fSAditya Swarup 	vecs_id = _VECS((engine->instance) / 2);
3625b26d57fSAditya Swarup 
3635b26d57fSAditya Swarup 	return engine->gt->engine[vecs_id];
3645b26d57fSAditya Swarup }
3655b26d57fSAditya Swarup 
3665b26d57fSAditya Swarup struct sfc_lock_data {
3675b26d57fSAditya Swarup 	i915_reg_t lock_reg;
3685b26d57fSAditya Swarup 	i915_reg_t ack_reg;
3695b26d57fSAditya Swarup 	i915_reg_t usage_reg;
3705b26d57fSAditya Swarup 	u32 lock_bit;
3715b26d57fSAditya Swarup 	u32 ack_bit;
3725b26d57fSAditya Swarup 	u32 usage_bit;
3735b26d57fSAditya Swarup 	u32 reset_bit;
3745b26d57fSAditya Swarup };
3755b26d57fSAditya Swarup 
get_sfc_forced_lock_data(struct intel_engine_cs * engine,struct sfc_lock_data * sfc_lock)3765b26d57fSAditya Swarup static void get_sfc_forced_lock_data(struct intel_engine_cs *engine,
3775b26d57fSAditya Swarup 				     struct sfc_lock_data *sfc_lock)
3785b26d57fSAditya Swarup {
3795b26d57fSAditya Swarup 	switch (engine->class) {
3805b26d57fSAditya Swarup 	default:
3815b26d57fSAditya Swarup 		MISSING_CASE(engine->class);
3825b26d57fSAditya Swarup 		fallthrough;
3835b26d57fSAditya Swarup 	case VIDEO_DECODE_CLASS:
38493cc7aa0SMatt Roper 		sfc_lock->lock_reg = GEN11_VCS_SFC_FORCED_LOCK(engine->mmio_base);
3855b26d57fSAditya Swarup 		sfc_lock->lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
3865b26d57fSAditya Swarup 
38793cc7aa0SMatt Roper 		sfc_lock->ack_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
3885b26d57fSAditya Swarup 		sfc_lock->ack_bit  = GEN11_VCS_SFC_LOCK_ACK_BIT;
3895b26d57fSAditya Swarup 
39093cc7aa0SMatt Roper 		sfc_lock->usage_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
3915b26d57fSAditya Swarup 		sfc_lock->usage_bit = GEN11_VCS_SFC_USAGE_BIT;
3925b26d57fSAditya Swarup 		sfc_lock->reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
3935b26d57fSAditya Swarup 
3945b26d57fSAditya Swarup 		break;
3955b26d57fSAditya Swarup 	case VIDEO_ENHANCEMENT_CLASS:
39693cc7aa0SMatt Roper 		sfc_lock->lock_reg = GEN11_VECS_SFC_FORCED_LOCK(engine->mmio_base);
3975b26d57fSAditya Swarup 		sfc_lock->lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
3985b26d57fSAditya Swarup 
39993cc7aa0SMatt Roper 		sfc_lock->ack_reg = GEN11_VECS_SFC_LOCK_ACK(engine->mmio_base);
4005b26d57fSAditya Swarup 		sfc_lock->ack_bit  = GEN11_VECS_SFC_LOCK_ACK_BIT;
4015b26d57fSAditya Swarup 
40293cc7aa0SMatt Roper 		sfc_lock->usage_reg = GEN11_VECS_SFC_USAGE(engine->mmio_base);
4035b26d57fSAditya Swarup 		sfc_lock->usage_bit = GEN11_VECS_SFC_USAGE_BIT;
4045b26d57fSAditya Swarup 		sfc_lock->reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
4055b26d57fSAditya Swarup 
4065b26d57fSAditya Swarup 		break;
4075b26d57fSAditya Swarup 	}
4085b26d57fSAditya Swarup }
4095b26d57fSAditya Swarup 
gen11_lock_sfc(struct intel_engine_cs * engine,u32 * reset_mask,u32 * unlock_mask)4105b26d57fSAditya Swarup static int gen11_lock_sfc(struct intel_engine_cs *engine,
4115b26d57fSAditya Swarup 			  u32 *reset_mask,
4125b26d57fSAditya Swarup 			  u32 *unlock_mask)
413112ed2d3SChris Wilson {
414112ed2d3SChris Wilson 	struct intel_uncore *uncore = engine->uncore;
415792592e7SDaniele Ceraolo Spurio 	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
4165b26d57fSAditya Swarup 	struct sfc_lock_data sfc_lock;
4175b26d57fSAditya Swarup 	bool lock_obtained, lock_to_other = false;
4180d333ac7SDaniele Ceraolo Spurio 	int ret;
419112ed2d3SChris Wilson 
420112ed2d3SChris Wilson 	switch (engine->class) {
421112ed2d3SChris Wilson 	case VIDEO_DECODE_CLASS:
422112ed2d3SChris Wilson 		if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
423112ed2d3SChris Wilson 			return 0;
424112ed2d3SChris Wilson 
4255b26d57fSAditya Swarup 		fallthrough;
426112ed2d3SChris Wilson 	case VIDEO_ENHANCEMENT_CLASS:
4275b26d57fSAditya Swarup 		get_sfc_forced_lock_data(engine, &sfc_lock);
428112ed2d3SChris Wilson 
429112ed2d3SChris Wilson 		break;
430112ed2d3SChris Wilson 	default:
431112ed2d3SChris Wilson 		return 0;
432112ed2d3SChris Wilson 	}
433112ed2d3SChris Wilson 
4345b26d57fSAditya Swarup 	if (!(intel_uncore_read_fw(uncore, sfc_lock.usage_reg) & sfc_lock.usage_bit)) {
4355b26d57fSAditya Swarup 		struct intel_engine_cs *paired_vecs;
4365b26d57fSAditya Swarup 
4375b26d57fSAditya Swarup 		if (engine->class != VIDEO_DECODE_CLASS ||
438c816723bSLucas De Marchi 		    GRAPHICS_VER(engine->i915) != 12)
4395b26d57fSAditya Swarup 			return 0;
4405b26d57fSAditya Swarup 
441112ed2d3SChris Wilson 		/*
4425b26d57fSAditya Swarup 		 * Wa_14010733141
4435b26d57fSAditya Swarup 		 *
4445b26d57fSAditya Swarup 		 * If the VCS-MFX isn't using the SFC, we also need to check
4455b26d57fSAditya Swarup 		 * whether VCS-HCP is using it.  If so, we need to issue a *VE*
4465b26d57fSAditya Swarup 		 * forced lock on the VE engine that shares the same SFC.
4475b26d57fSAditya Swarup 		 */
4485b26d57fSAditya Swarup 		if (!(intel_uncore_read_fw(uncore,
44993cc7aa0SMatt Roper 					   GEN12_HCP_SFC_LOCK_STATUS(engine->mmio_base)) &
4505b26d57fSAditya Swarup 		      GEN12_HCP_SFC_USAGE_BIT))
4515b26d57fSAditya Swarup 			return 0;
4525b26d57fSAditya Swarup 
4535b26d57fSAditya Swarup 		paired_vecs = find_sfc_paired_vecs_engine(engine);
4545b26d57fSAditya Swarup 		get_sfc_forced_lock_data(paired_vecs, &sfc_lock);
4555b26d57fSAditya Swarup 		lock_to_other = true;
4565b26d57fSAditya Swarup 		*unlock_mask |= paired_vecs->mask;
4575b26d57fSAditya Swarup 	} else {
4585b26d57fSAditya Swarup 		*unlock_mask |= engine->mask;
4595b26d57fSAditya Swarup 	}
4605b26d57fSAditya Swarup 
4615b26d57fSAditya Swarup 	/*
4625b26d57fSAditya Swarup 	 * If the engine is using an SFC, tell the engine that a software reset
4630d333ac7SDaniele Ceraolo Spurio 	 * is going to happen. The engine will then try to force lock the SFC.
4640d333ac7SDaniele Ceraolo Spurio 	 * If SFC ends up being locked to the engine we want to reset, we have
4650d333ac7SDaniele Ceraolo Spurio 	 * to reset it as well (we will unlock it once the reset sequence is
4660d333ac7SDaniele Ceraolo Spurio 	 * completed).
467112ed2d3SChris Wilson 	 */
4684050e6f2SJani Nikula 	intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, 0, sfc_lock.lock_bit);
469112ed2d3SChris Wilson 
4700d333ac7SDaniele Ceraolo Spurio 	ret = __intel_wait_for_register_fw(uncore,
4715b26d57fSAditya Swarup 					   sfc_lock.ack_reg,
4725b26d57fSAditya Swarup 					   sfc_lock.ack_bit,
4735b26d57fSAditya Swarup 					   sfc_lock.ack_bit,
4740d333ac7SDaniele Ceraolo Spurio 					   1000, 0, NULL);
4750d333ac7SDaniele Ceraolo Spurio 
4765b26d57fSAditya Swarup 	/*
4775b26d57fSAditya Swarup 	 * Was the SFC released while we were trying to lock it?
4785b26d57fSAditya Swarup 	 *
4795b26d57fSAditya Swarup 	 * We should reset both the engine and the SFC if:
4805b26d57fSAditya Swarup 	 *  - We were locking the SFC to this engine and the lock succeeded
4815b26d57fSAditya Swarup 	 *       OR
4825b26d57fSAditya Swarup 	 *  - We were locking the SFC to a different engine (Wa_14010733141)
4835b26d57fSAditya Swarup 	 *    but the SFC was released before the lock was obtained.
4845b26d57fSAditya Swarup 	 *
4855b26d57fSAditya Swarup 	 * Otherwise we need only reset the engine by itself and we can
4865b26d57fSAditya Swarup 	 * leave the SFC alone.
4875b26d57fSAditya Swarup 	 */
4885b26d57fSAditya Swarup 	lock_obtained = (intel_uncore_read_fw(uncore, sfc_lock.usage_reg) &
4895b26d57fSAditya Swarup 			sfc_lock.usage_bit) != 0;
4905b26d57fSAditya Swarup 	if (lock_obtained == lock_to_other)
491112ed2d3SChris Wilson 		return 0;
4920d333ac7SDaniele Ceraolo Spurio 
4930d333ac7SDaniele Ceraolo Spurio 	if (ret) {
494cb56a07dSChris Wilson 		ENGINE_TRACE(engine, "Wait for SFC forced lock ack failed\n");
4950d333ac7SDaniele Ceraolo Spurio 		return ret;
496112ed2d3SChris Wilson 	}
497112ed2d3SChris Wilson 
4985b26d57fSAditya Swarup 	*reset_mask |= sfc_lock.reset_bit;
499112ed2d3SChris Wilson 	return 0;
500112ed2d3SChris Wilson }
501112ed2d3SChris Wilson 
gen11_unlock_sfc(struct intel_engine_cs * engine)502112ed2d3SChris Wilson static void gen11_unlock_sfc(struct intel_engine_cs *engine)
503112ed2d3SChris Wilson {
504112ed2d3SChris Wilson 	struct intel_uncore *uncore = engine->uncore;
505792592e7SDaniele Ceraolo Spurio 	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
5065b26d57fSAditya Swarup 	struct sfc_lock_data sfc_lock = {};
507112ed2d3SChris Wilson 
5085b26d57fSAditya Swarup 	if (engine->class != VIDEO_DECODE_CLASS &&
5095b26d57fSAditya Swarup 	    engine->class != VIDEO_ENHANCEMENT_CLASS)
510112ed2d3SChris Wilson 		return;
511112ed2d3SChris Wilson 
5125b26d57fSAditya Swarup 	if (engine->class == VIDEO_DECODE_CLASS &&
5135b26d57fSAditya Swarup 	    (BIT(engine->instance) & vdbox_sfc_access) == 0)
514112ed2d3SChris Wilson 		return;
515112ed2d3SChris Wilson 
5165b26d57fSAditya Swarup 	get_sfc_forced_lock_data(engine, &sfc_lock);
5175b26d57fSAditya Swarup 
5184050e6f2SJani Nikula 	intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, sfc_lock.lock_bit, 0);
519112ed2d3SChris Wilson }
520112ed2d3SChris Wilson 
__gen11_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)521336561a9SChris Wilson static int __gen11_reset_engines(struct intel_gt *gt,
522112ed2d3SChris Wilson 				 intel_engine_mask_t engine_mask,
523112ed2d3SChris Wilson 				 unsigned int retry)
524112ed2d3SChris Wilson {
525112ed2d3SChris Wilson 	struct intel_engine_cs *engine;
526112ed2d3SChris Wilson 	intel_engine_mask_t tmp;
5275b26d57fSAditya Swarup 	u32 reset_mask, unlock_mask = 0;
528112ed2d3SChris Wilson 	int ret;
529112ed2d3SChris Wilson 
530112ed2d3SChris Wilson 	if (engine_mask == ALL_ENGINES) {
5315b26d57fSAditya Swarup 		reset_mask = GEN11_GRDOM_FULL;
532112ed2d3SChris Wilson 	} else {
5335b26d57fSAditya Swarup 		reset_mask = 0;
534a50134b1STvrtko Ursulin 		for_each_engine_masked(engine, gt, engine_mask, tmp) {
53520cddfccSTejas Upadhyay 			reset_mask |= engine->reset_domain;
5365b26d57fSAditya Swarup 			ret = gen11_lock_sfc(engine, &reset_mask, &unlock_mask);
5370d333ac7SDaniele Ceraolo Spurio 			if (ret)
5380d333ac7SDaniele Ceraolo Spurio 				goto sfc_unlock;
539112ed2d3SChris Wilson 		}
540112ed2d3SChris Wilson 	}
541112ed2d3SChris Wilson 
5425b26d57fSAditya Swarup 	ret = gen6_hw_domain_reset(gt, reset_mask);
543112ed2d3SChris Wilson 
5440d333ac7SDaniele Ceraolo Spurio sfc_unlock:
5450d333ac7SDaniele Ceraolo Spurio 	/*
5460d333ac7SDaniele Ceraolo Spurio 	 * We unlock the SFC based on the lock status and not the result of
5470d333ac7SDaniele Ceraolo Spurio 	 * gen11_lock_sfc to make sure that we clean properly if something
5480d333ac7SDaniele Ceraolo Spurio 	 * wrong happened during the lock (e.g. lock acquired after timeout
5490d333ac7SDaniele Ceraolo Spurio 	 * expiration).
5505b26d57fSAditya Swarup 	 *
5515b26d57fSAditya Swarup 	 * Due to Wa_14010733141, we may have locked an SFC to an engine that
5525b26d57fSAditya Swarup 	 * wasn't being reset.  So instead of calling gen11_unlock_sfc()
5535b26d57fSAditya Swarup 	 * on engine_mask, we instead call it on the mask of engines that our
5545b26d57fSAditya Swarup 	 * gen11_lock_sfc() calls told us actually had locks attempted.
5550d333ac7SDaniele Ceraolo Spurio 	 */
5565b26d57fSAditya Swarup 	for_each_engine_masked(engine, gt, unlock_mask, tmp)
557112ed2d3SChris Wilson 		gen11_unlock_sfc(engine);
558112ed2d3SChris Wilson 
559112ed2d3SChris Wilson 	return ret;
560112ed2d3SChris Wilson }
561112ed2d3SChris Wilson 
gen8_engine_reset_prepare(struct intel_engine_cs * engine)562112ed2d3SChris Wilson static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
563112ed2d3SChris Wilson {
564112ed2d3SChris Wilson 	struct intel_uncore *uncore = engine->uncore;
565112ed2d3SChris Wilson 	const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
566112ed2d3SChris Wilson 	u32 request, mask, ack;
567112ed2d3SChris Wilson 	int ret;
568112ed2d3SChris Wilson 
5690a7d355eSChris Wilson 	if (I915_SELFTEST_ONLY(should_fail(&engine->reset_timeout, 1)))
5700a7d355eSChris Wilson 		return -ETIMEDOUT;
5710a7d355eSChris Wilson 
572112ed2d3SChris Wilson 	ack = intel_uncore_read_fw(uncore, reg);
573112ed2d3SChris Wilson 	if (ack & RESET_CTL_CAT_ERROR) {
574112ed2d3SChris Wilson 		/*
575112ed2d3SChris Wilson 		 * For catastrophic errors, ready-for-reset sequence
576112ed2d3SChris Wilson 		 * needs to be bypassed: HAS#396813
577112ed2d3SChris Wilson 		 */
578112ed2d3SChris Wilson 		request = RESET_CTL_CAT_ERROR;
579112ed2d3SChris Wilson 		mask = RESET_CTL_CAT_ERROR;
580112ed2d3SChris Wilson 
581112ed2d3SChris Wilson 		/* Catastrophic errors need to be cleared by HW */
582112ed2d3SChris Wilson 		ack = 0;
583112ed2d3SChris Wilson 	} else if (!(ack & RESET_CTL_READY_TO_RESET)) {
584112ed2d3SChris Wilson 		request = RESET_CTL_REQUEST_RESET;
585112ed2d3SChris Wilson 		mask = RESET_CTL_READY_TO_RESET;
586112ed2d3SChris Wilson 		ack = RESET_CTL_READY_TO_RESET;
587112ed2d3SChris Wilson 	} else {
588112ed2d3SChris Wilson 		return 0;
589112ed2d3SChris Wilson 	}
590112ed2d3SChris Wilson 
591112ed2d3SChris Wilson 	intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
592112ed2d3SChris Wilson 	ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
593112ed2d3SChris Wilson 					   700, 0, NULL);
594112ed2d3SChris Wilson 	if (ret)
595f8474622SWambui Karuga 		drm_err(&engine->i915->drm,
596f8474622SWambui Karuga 			"%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
597112ed2d3SChris Wilson 			engine->name, request,
598112ed2d3SChris Wilson 			intel_uncore_read_fw(uncore, reg));
599112ed2d3SChris Wilson 
600112ed2d3SChris Wilson 	return ret;
601112ed2d3SChris Wilson }
602112ed2d3SChris Wilson 
gen8_engine_reset_cancel(struct intel_engine_cs * engine)603112ed2d3SChris Wilson static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
604112ed2d3SChris Wilson {
605112ed2d3SChris Wilson 	intel_uncore_write_fw(engine->uncore,
606112ed2d3SChris Wilson 			      RING_RESET_CTL(engine->mmio_base),
607112ed2d3SChris Wilson 			      _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
608112ed2d3SChris Wilson }
609112ed2d3SChris Wilson 
gen8_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)610cb823ed9SChris Wilson static int gen8_reset_engines(struct intel_gt *gt,
611112ed2d3SChris Wilson 			      intel_engine_mask_t engine_mask,
612112ed2d3SChris Wilson 			      unsigned int retry)
613112ed2d3SChris Wilson {
614112ed2d3SChris Wilson 	struct intel_engine_cs *engine;
615112ed2d3SChris Wilson 	const bool reset_non_ready = retry >= 1;
616112ed2d3SChris Wilson 	intel_engine_mask_t tmp;
617336561a9SChris Wilson 	unsigned long flags;
618112ed2d3SChris Wilson 	int ret;
619112ed2d3SChris Wilson 
620336561a9SChris Wilson 	spin_lock_irqsave(&gt->uncore->lock, flags);
621336561a9SChris Wilson 
622a50134b1STvrtko Ursulin 	for_each_engine_masked(engine, gt, engine_mask, tmp) {
623112ed2d3SChris Wilson 		ret = gen8_engine_reset_prepare(engine);
624112ed2d3SChris Wilson 		if (ret && !reset_non_ready)
625112ed2d3SChris Wilson 			goto skip_reset;
626112ed2d3SChris Wilson 
627112ed2d3SChris Wilson 		/*
628112ed2d3SChris Wilson 		 * If this is not the first failed attempt to prepare,
629112ed2d3SChris Wilson 		 * we decide to proceed anyway.
630112ed2d3SChris Wilson 		 *
631112ed2d3SChris Wilson 		 * By doing so we risk context corruption and with
632112ed2d3SChris Wilson 		 * some gens (kbl), possible system hang if reset
633112ed2d3SChris Wilson 		 * happens during active bb execution.
634112ed2d3SChris Wilson 		 *
635112ed2d3SChris Wilson 		 * We rather take context corruption instead of
636112ed2d3SChris Wilson 		 * failed reset with a wedged driver/gpu. And
637112ed2d3SChris Wilson 		 * active bb execution case should be covered by
638cb823ed9SChris Wilson 		 * stop_engines() we have before the reset.
639112ed2d3SChris Wilson 		 */
640112ed2d3SChris Wilson 	}
641112ed2d3SChris Wilson 
642154cfae6SBruce Chang 	/*
643154cfae6SBruce Chang 	 * Wa_22011100796:dg2, whenever Full soft reset is required,
644154cfae6SBruce Chang 	 * reset all individual engines firstly, and then do a full soft reset.
645154cfae6SBruce Chang 	 *
646154cfae6SBruce Chang 	 * This is best effort, so ignore any error from the initial reset.
647154cfae6SBruce Chang 	 */
648154cfae6SBruce Chang 	if (IS_DG2(gt->i915) && engine_mask == ALL_ENGINES)
649336561a9SChris Wilson 		__gen11_reset_engines(gt, gt->info.engine_mask, 0);
650154cfae6SBruce Chang 
651c816723bSLucas De Marchi 	if (GRAPHICS_VER(gt->i915) >= 11)
652336561a9SChris Wilson 		ret = __gen11_reset_engines(gt, engine_mask, retry);
653112ed2d3SChris Wilson 	else
654336561a9SChris Wilson 		ret = __gen6_reset_engines(gt, engine_mask, retry);
655112ed2d3SChris Wilson 
656112ed2d3SChris Wilson skip_reset:
657a50134b1STvrtko Ursulin 	for_each_engine_masked(engine, gt, engine_mask, tmp)
658112ed2d3SChris Wilson 		gen8_engine_reset_cancel(engine);
659112ed2d3SChris Wilson 
660336561a9SChris Wilson 	spin_unlock_irqrestore(&gt->uncore->lock, flags);
661336561a9SChris Wilson 
662112ed2d3SChris Wilson 	return ret;
663112ed2d3SChris Wilson }
664112ed2d3SChris Wilson 
mock_reset(struct intel_gt * gt,intel_engine_mask_t mask,unsigned int retry)6654abc6e7cSChris Wilson static int mock_reset(struct intel_gt *gt,
6664abc6e7cSChris Wilson 		      intel_engine_mask_t mask,
6674abc6e7cSChris Wilson 		      unsigned int retry)
6684abc6e7cSChris Wilson {
6694abc6e7cSChris Wilson 	return 0;
6704abc6e7cSChris Wilson }
6714abc6e7cSChris Wilson 
672cb823ed9SChris Wilson typedef int (*reset_func)(struct intel_gt *,
673112ed2d3SChris Wilson 			  intel_engine_mask_t engine_mask,
674112ed2d3SChris Wilson 			  unsigned int retry);
675112ed2d3SChris Wilson 
intel_get_gpu_reset(const struct intel_gt * gt)676260e6b71SChris Wilson static reset_func intel_get_gpu_reset(const struct intel_gt *gt)
677112ed2d3SChris Wilson {
678260e6b71SChris Wilson 	struct drm_i915_private *i915 = gt->i915;
679260e6b71SChris Wilson 
6804abc6e7cSChris Wilson 	if (is_mock_gt(gt))
6814abc6e7cSChris Wilson 		return mock_reset;
682c816723bSLucas De Marchi 	else if (GRAPHICS_VER(i915) >= 8)
683112ed2d3SChris Wilson 		return gen8_reset_engines;
684c816723bSLucas De Marchi 	else if (GRAPHICS_VER(i915) >= 6)
685112ed2d3SChris Wilson 		return gen6_reset_engines;
686c816723bSLucas De Marchi 	else if (GRAPHICS_VER(i915) >= 5)
6879eae5e27SLucas De Marchi 		return ilk_do_reset;
688112ed2d3SChris Wilson 	else if (IS_G4X(i915))
689112ed2d3SChris Wilson 		return g4x_do_reset;
690112ed2d3SChris Wilson 	else if (IS_G33(i915) || IS_PINEVIEW(i915))
691112ed2d3SChris Wilson 		return g33_do_reset;
692c816723bSLucas De Marchi 	else if (GRAPHICS_VER(i915) >= 3)
693112ed2d3SChris Wilson 		return i915_do_reset;
694112ed2d3SChris Wilson 	else
695112ed2d3SChris Wilson 		return NULL;
696112ed2d3SChris Wilson }
697112ed2d3SChris Wilson 
__reset_guc(struct intel_gt * gt)698b7d70b8bSDaniele Ceraolo Spurio static int __reset_guc(struct intel_gt *gt)
699b7d70b8bSDaniele Ceraolo Spurio {
700b7d70b8bSDaniele Ceraolo Spurio 	u32 guc_domain =
701b7d70b8bSDaniele Ceraolo Spurio 		GRAPHICS_VER(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
702b7d70b8bSDaniele Ceraolo Spurio 
703b7d70b8bSDaniele Ceraolo Spurio 	return gen6_hw_domain_reset(gt, guc_domain);
704b7d70b8bSDaniele Ceraolo Spurio }
705b7d70b8bSDaniele Ceraolo Spurio 
needs_wa_14015076503(struct intel_gt * gt,intel_engine_mask_t engine_mask)706b7d70b8bSDaniele Ceraolo Spurio static bool needs_wa_14015076503(struct intel_gt *gt, intel_engine_mask_t engine_mask)
707b7d70b8bSDaniele Ceraolo Spurio {
708*ec84b2a4SMatt Roper 	if (MEDIA_VER_FULL(gt->i915) != IP_VER(13, 0) || !HAS_ENGINE(gt, GSC0))
709b7d70b8bSDaniele Ceraolo Spurio 		return false;
710b7d70b8bSDaniele Ceraolo Spurio 
711b7d70b8bSDaniele Ceraolo Spurio 	if (!__HAS_ENGINE(engine_mask, GSC0))
712b7d70b8bSDaniele Ceraolo Spurio 		return false;
713b7d70b8bSDaniele Ceraolo Spurio 
714b7d70b8bSDaniele Ceraolo Spurio 	return intel_gsc_uc_fw_init_done(&gt->uc.gsc);
715b7d70b8bSDaniele Ceraolo Spurio }
716b7d70b8bSDaniele Ceraolo Spurio 
717b7d70b8bSDaniele Ceraolo Spurio static intel_engine_mask_t
wa_14015076503_start(struct intel_gt * gt,intel_engine_mask_t engine_mask,bool first)718b7d70b8bSDaniele Ceraolo Spurio wa_14015076503_start(struct intel_gt *gt, intel_engine_mask_t engine_mask, bool first)
719b7d70b8bSDaniele Ceraolo Spurio {
720b7d70b8bSDaniele Ceraolo Spurio 	if (!needs_wa_14015076503(gt, engine_mask))
721b7d70b8bSDaniele Ceraolo Spurio 		return engine_mask;
722b7d70b8bSDaniele Ceraolo Spurio 
723b7d70b8bSDaniele Ceraolo Spurio 	/*
724b7d70b8bSDaniele Ceraolo Spurio 	 * wa_14015076503: if the GSC FW is loaded, we need to alert it that
725b7d70b8bSDaniele Ceraolo Spurio 	 * we're going to do a GSC engine reset and then wait for 200ms for the
726b7d70b8bSDaniele Ceraolo Spurio 	 * FW to get ready for it. However, if this is the first ALL_ENGINES
727b7d70b8bSDaniele Ceraolo Spurio 	 * reset attempt and the GSC is not busy, we can try to instead reset
728b7d70b8bSDaniele Ceraolo Spurio 	 * the GuC and all the other engines individually to avoid the 200ms
729b7d70b8bSDaniele Ceraolo Spurio 	 * wait.
730b7d70b8bSDaniele Ceraolo Spurio 	 * Skipping the GSC engine is safe because, differently from other
731b7d70b8bSDaniele Ceraolo Spurio 	 * engines, the GSCCS only role is to forward the commands to the GSC
732b7d70b8bSDaniele Ceraolo Spurio 	 * FW, so it doesn't have any HW outside of the CS itself and therefore
733b7d70b8bSDaniele Ceraolo Spurio 	 * it has no state that we don't explicitly re-init on resume or on
734b7d70b8bSDaniele Ceraolo Spurio 	 * context switch LRC or power context). The HW for the GSC uC is
735b7d70b8bSDaniele Ceraolo Spurio 	 * managed by the GSC FW so we don't need to care about that.
736b7d70b8bSDaniele Ceraolo Spurio 	 */
737b7d70b8bSDaniele Ceraolo Spurio 	if (engine_mask == ALL_ENGINES && first && intel_engine_is_idle(gt->engine[GSC0])) {
738b7d70b8bSDaniele Ceraolo Spurio 		__reset_guc(gt);
739b7d70b8bSDaniele Ceraolo Spurio 		engine_mask = gt->info.engine_mask & ~BIT(GSC0);
740b7d70b8bSDaniele Ceraolo Spurio 	} else {
741b7d70b8bSDaniele Ceraolo Spurio 		intel_uncore_rmw(gt->uncore,
742b7d70b8bSDaniele Ceraolo Spurio 				 HECI_H_GS1(MTL_GSC_HECI2_BASE),
743b7d70b8bSDaniele Ceraolo Spurio 				 0, HECI_H_GS1_ER_PREP);
744b7d70b8bSDaniele Ceraolo Spurio 
745b7d70b8bSDaniele Ceraolo Spurio 		/* make sure the reset bit is clear when writing the CSR reg */
746b7d70b8bSDaniele Ceraolo Spurio 		intel_uncore_rmw(gt->uncore,
747b7d70b8bSDaniele Ceraolo Spurio 				 HECI_H_CSR(MTL_GSC_HECI2_BASE),
748b7d70b8bSDaniele Ceraolo Spurio 				 HECI_H_CSR_RST, HECI_H_CSR_IG);
749b7d70b8bSDaniele Ceraolo Spurio 		msleep(200);
750b7d70b8bSDaniele Ceraolo Spurio 	}
751b7d70b8bSDaniele Ceraolo Spurio 
752b7d70b8bSDaniele Ceraolo Spurio 	return engine_mask;
753b7d70b8bSDaniele Ceraolo Spurio }
754b7d70b8bSDaniele Ceraolo Spurio 
755b7d70b8bSDaniele Ceraolo Spurio static void
wa_14015076503_end(struct intel_gt * gt,intel_engine_mask_t engine_mask)756b7d70b8bSDaniele Ceraolo Spurio wa_14015076503_end(struct intel_gt *gt, intel_engine_mask_t engine_mask)
757b7d70b8bSDaniele Ceraolo Spurio {
758b7d70b8bSDaniele Ceraolo Spurio 	if (!needs_wa_14015076503(gt, engine_mask))
759b7d70b8bSDaniele Ceraolo Spurio 		return;
760b7d70b8bSDaniele Ceraolo Spurio 
761b7d70b8bSDaniele Ceraolo Spurio 	intel_uncore_rmw(gt->uncore,
762b7d70b8bSDaniele Ceraolo Spurio 			 HECI_H_GS1(MTL_GSC_HECI2_BASE),
763b7d70b8bSDaniele Ceraolo Spurio 			 HECI_H_GS1_ER_PREP, 0);
764b7d70b8bSDaniele Ceraolo Spurio }
765b7d70b8bSDaniele Ceraolo Spurio 
__intel_gt_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask)766cb823ed9SChris Wilson int __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask)
767112ed2d3SChris Wilson {
768112ed2d3SChris Wilson 	const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
769112ed2d3SChris Wilson 	reset_func reset;
770112ed2d3SChris Wilson 	int ret = -ETIMEDOUT;
771112ed2d3SChris Wilson 	int retry;
772112ed2d3SChris Wilson 
773260e6b71SChris Wilson 	reset = intel_get_gpu_reset(gt);
774112ed2d3SChris Wilson 	if (!reset)
775112ed2d3SChris Wilson 		return -ENODEV;
776112ed2d3SChris Wilson 
777112ed2d3SChris Wilson 	/*
778112ed2d3SChris Wilson 	 * If the power well sleeps during the reset, the reset
779112ed2d3SChris Wilson 	 * request may be dropped and never completes (causing -EIO).
780112ed2d3SChris Wilson 	 */
781cb823ed9SChris Wilson 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
782112ed2d3SChris Wilson 	for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
783b7d70b8bSDaniele Ceraolo Spurio 		intel_engine_mask_t reset_mask;
784b7d70b8bSDaniele Ceraolo Spurio 
785b7d70b8bSDaniele Ceraolo Spurio 		reset_mask = wa_14015076503_start(gt, engine_mask, !retry);
786b7d70b8bSDaniele Ceraolo Spurio 
787b7d70b8bSDaniele Ceraolo Spurio 		GT_TRACE(gt, "engine_mask=%x\n", reset_mask);
788112ed2d3SChris Wilson 		preempt_disable();
789b7d70b8bSDaniele Ceraolo Spurio 		ret = reset(gt, reset_mask, retry);
790112ed2d3SChris Wilson 		preempt_enable();
791b7d70b8bSDaniele Ceraolo Spurio 
792b7d70b8bSDaniele Ceraolo Spurio 		wa_14015076503_end(gt, reset_mask);
793112ed2d3SChris Wilson 	}
794cb823ed9SChris Wilson 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
795112ed2d3SChris Wilson 
796112ed2d3SChris Wilson 	return ret;
797112ed2d3SChris Wilson }
798112ed2d3SChris Wilson 
intel_has_gpu_reset(const struct intel_gt * gt)799260e6b71SChris Wilson bool intel_has_gpu_reset(const struct intel_gt *gt)
800112ed2d3SChris Wilson {
8018a25c4beSJani Nikula 	if (!gt->i915->params.reset)
802112ed2d3SChris Wilson 		return NULL;
803112ed2d3SChris Wilson 
804260e6b71SChris Wilson 	return intel_get_gpu_reset(gt);
805112ed2d3SChris Wilson }
806112ed2d3SChris Wilson 
intel_has_reset_engine(const struct intel_gt * gt)807260e6b71SChris Wilson bool intel_has_reset_engine(const struct intel_gt *gt)
808112ed2d3SChris Wilson {
8098a25c4beSJani Nikula 	if (gt->i915->params.reset < 2)
810260e6b71SChris Wilson 		return false;
811260e6b71SChris Wilson 
812b409db08STvrtko Ursulin 	return INTEL_INFO(gt->i915)->has_reset_engine;
813112ed2d3SChris Wilson }
814112ed2d3SChris Wilson 
intel_reset_guc(struct intel_gt * gt)815cb823ed9SChris Wilson int intel_reset_guc(struct intel_gt *gt)
816112ed2d3SChris Wilson {
817112ed2d3SChris Wilson 	int ret;
818112ed2d3SChris Wilson 
819702668e6SDaniele Ceraolo Spurio 	GEM_BUG_ON(!HAS_GT_UC(gt->i915));
820112ed2d3SChris Wilson 
821cb823ed9SChris Wilson 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
822b7d70b8bSDaniele Ceraolo Spurio 	ret = __reset_guc(gt);
823cb823ed9SChris Wilson 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
824112ed2d3SChris Wilson 
825112ed2d3SChris Wilson 	return ret;
826112ed2d3SChris Wilson }
827112ed2d3SChris Wilson 
828112ed2d3SChris Wilson /*
829112ed2d3SChris Wilson  * Ensure irq handler finishes, and not run again.
830112ed2d3SChris Wilson  * Also return the active request so that we only search for it once.
831112ed2d3SChris Wilson  */
reset_prepare_engine(struct intel_engine_cs * engine)832112ed2d3SChris Wilson static void reset_prepare_engine(struct intel_engine_cs *engine)
833112ed2d3SChris Wilson {
834112ed2d3SChris Wilson 	/*
835112ed2d3SChris Wilson 	 * During the reset sequence, we must prevent the engine from
836112ed2d3SChris Wilson 	 * entering RC6. As the context state is undefined until we restart
837112ed2d3SChris Wilson 	 * the engine, if it does enter RC6 during the reset, the state
838112ed2d3SChris Wilson 	 * written to the powercontext is undefined and so we may lose
839112ed2d3SChris Wilson 	 * GPU state upon resume, i.e. fail to restart after a reset.
840112ed2d3SChris Wilson 	 */
841112ed2d3SChris Wilson 	intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
842e26b6d43SChris Wilson 	if (engine->reset.prepare)
843112ed2d3SChris Wilson 		engine->reset.prepare(engine);
844112ed2d3SChris Wilson }
845112ed2d3SChris Wilson 
revoke_mmaps(struct intel_gt * gt)846cb823ed9SChris Wilson static void revoke_mmaps(struct intel_gt *gt)
847112ed2d3SChris Wilson {
848112ed2d3SChris Wilson 	int i;
849112ed2d3SChris Wilson 
850cb823ed9SChris Wilson 	for (i = 0; i < gt->ggtt->num_fences; i++) {
851112ed2d3SChris Wilson 		struct drm_vma_offset_node *node;
852112ed2d3SChris Wilson 		struct i915_vma *vma;
853112ed2d3SChris Wilson 		u64 vma_offset;
854112ed2d3SChris Wilson 
855cb823ed9SChris Wilson 		vma = READ_ONCE(gt->ggtt->fence_regs[i].vma);
856112ed2d3SChris Wilson 		if (!vma)
857112ed2d3SChris Wilson 			continue;
858112ed2d3SChris Wilson 
859112ed2d3SChris Wilson 		if (!i915_vma_has_userfault(vma))
860112ed2d3SChris Wilson 			continue;
861112ed2d3SChris Wilson 
862cb823ed9SChris Wilson 		GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
863cc662126SAbdiel Janulgue 
864cc662126SAbdiel Janulgue 		if (!vma->mmo)
865cc662126SAbdiel Janulgue 			continue;
866cc662126SAbdiel Janulgue 
867cc662126SAbdiel Janulgue 		node = &vma->mmo->vma_node;
8683bb6a442SNiranjana Vishwanathapura 		vma_offset = vma->gtt_view.partial.offset << PAGE_SHIFT;
869cc662126SAbdiel Janulgue 
870cb823ed9SChris Wilson 		unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
871112ed2d3SChris Wilson 				    drm_vma_node_offset_addr(node) + vma_offset,
872112ed2d3SChris Wilson 				    vma->size,
873112ed2d3SChris Wilson 				    1);
874112ed2d3SChris Wilson 	}
875112ed2d3SChris Wilson }
876112ed2d3SChris Wilson 
reset_prepare(struct intel_gt * gt)877cb823ed9SChris Wilson static intel_engine_mask_t reset_prepare(struct intel_gt *gt)
878112ed2d3SChris Wilson {
879112ed2d3SChris Wilson 	struct intel_engine_cs *engine;
88018398904SChris Wilson 	intel_engine_mask_t awake = 0;
881112ed2d3SChris Wilson 	enum intel_engine_id id;
882112ed2d3SChris Wilson 
883dac38381SUmesh Nerlige Ramappa 	/* For GuC mode, ensure submission is disabled before stopping ring */
884dac38381SUmesh Nerlige Ramappa 	intel_uc_reset_prepare(&gt->uc);
885dac38381SUmesh Nerlige Ramappa 
8865d904e3cSTvrtko Ursulin 	for_each_engine(engine, gt, id) {
88718398904SChris Wilson 		if (intel_engine_pm_get_if_awake(engine))
88818398904SChris Wilson 			awake |= engine->mask;
889112ed2d3SChris Wilson 		reset_prepare_engine(engine);
89018398904SChris Wilson 	}
891112ed2d3SChris Wilson 
89218398904SChris Wilson 	return awake;
893112ed2d3SChris Wilson }
894112ed2d3SChris Wilson 
gt_revoke(struct intel_gt * gt)895cb823ed9SChris Wilson static void gt_revoke(struct intel_gt *gt)
896112ed2d3SChris Wilson {
897cb823ed9SChris Wilson 	revoke_mmaps(gt);
898112ed2d3SChris Wilson }
899112ed2d3SChris Wilson 
gt_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask)900cb823ed9SChris Wilson static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
901112ed2d3SChris Wilson {
902112ed2d3SChris Wilson 	struct intel_engine_cs *engine;
903112ed2d3SChris Wilson 	enum intel_engine_id id;
904112ed2d3SChris Wilson 	int err;
905112ed2d3SChris Wilson 
906112ed2d3SChris Wilson 	/*
907112ed2d3SChris Wilson 	 * Everything depends on having the GTT running, so we need to start
908112ed2d3SChris Wilson 	 * there.
909112ed2d3SChris Wilson 	 */
910cb823ed9SChris Wilson 	err = i915_ggtt_enable_hw(gt->i915);
911112ed2d3SChris Wilson 	if (err)
912112ed2d3SChris Wilson 		return err;
913112ed2d3SChris Wilson 
91416f2941aSChris Wilson 	local_bh_disable();
9155d904e3cSTvrtko Ursulin 	for_each_engine(engine, gt, id)
916cb823ed9SChris Wilson 		__intel_engine_reset(engine, stalled_mask & engine->mask);
91716f2941aSChris Wilson 	local_bh_enable();
918112ed2d3SChris Wilson 
919303760aaSUmesh Nerlige Ramappa 	intel_uc_reset(&gt->uc, ALL_ENGINES);
920eb5e7da7SMatthew Brost 
921f899f786SChris Wilson 	intel_ggtt_restore_fences(gt->ggtt);
922112ed2d3SChris Wilson 
923112ed2d3SChris Wilson 	return err;
924112ed2d3SChris Wilson }
925112ed2d3SChris Wilson 
reset_finish_engine(struct intel_engine_cs * engine)926112ed2d3SChris Wilson static void reset_finish_engine(struct intel_engine_cs *engine)
927112ed2d3SChris Wilson {
928e26b6d43SChris Wilson 	if (engine->reset.finish)
929112ed2d3SChris Wilson 		engine->reset.finish(engine);
930112ed2d3SChris Wilson 	intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
93118398904SChris Wilson 
93254400257SChris Wilson 	intel_engine_signal_breadcrumbs(engine);
933112ed2d3SChris Wilson }
934112ed2d3SChris Wilson 
reset_finish(struct intel_gt * gt,intel_engine_mask_t awake)935cb823ed9SChris Wilson static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
936112ed2d3SChris Wilson {
937112ed2d3SChris Wilson 	struct intel_engine_cs *engine;
938112ed2d3SChris Wilson 	enum intel_engine_id id;
939112ed2d3SChris Wilson 
9405d904e3cSTvrtko Ursulin 	for_each_engine(engine, gt, id) {
941112ed2d3SChris Wilson 		reset_finish_engine(engine);
94218398904SChris Wilson 		if (awake & engine->mask)
94318398904SChris Wilson 			intel_engine_pm_put(engine);
944112ed2d3SChris Wilson 	}
945eb5e7da7SMatthew Brost 
946eb5e7da7SMatthew Brost 	intel_uc_reset_finish(&gt->uc);
947112ed2d3SChris Wilson }
948112ed2d3SChris Wilson 
nop_submit_request(struct i915_request * request)949112ed2d3SChris Wilson static void nop_submit_request(struct i915_request *request)
950112ed2d3SChris Wilson {
9513fbbbef4SChris Wilson 	RQ_TRACE(request, "-EIO\n");
952112ed2d3SChris Wilson 
953c10e4a79SChris Wilson 	request = i915_request_mark_eio(request);
954c10e4a79SChris Wilson 	if (request) {
955c10e4a79SChris Wilson 		i915_request_submit(request);
956c10e4a79SChris Wilson 		intel_engine_signal_breadcrumbs(request->engine);
957112ed2d3SChris Wilson 
958c10e4a79SChris Wilson 		i915_request_put(request);
959c10e4a79SChris Wilson 	}
960112ed2d3SChris Wilson }
961112ed2d3SChris Wilson 
__intel_gt_set_wedged(struct intel_gt * gt)962cb823ed9SChris Wilson static void __intel_gt_set_wedged(struct intel_gt *gt)
963112ed2d3SChris Wilson {
964112ed2d3SChris Wilson 	struct intel_engine_cs *engine;
96518398904SChris Wilson 	intel_engine_mask_t awake;
966112ed2d3SChris Wilson 	enum intel_engine_id id;
967112ed2d3SChris Wilson 
968cb823ed9SChris Wilson 	if (test_bit(I915_WEDGED, &gt->reset.flags))
969112ed2d3SChris Wilson 		return;
970112ed2d3SChris Wilson 
9713fbbbef4SChris Wilson 	GT_TRACE(gt, "start\n");
972112ed2d3SChris Wilson 
973112ed2d3SChris Wilson 	/*
974112ed2d3SChris Wilson 	 * First, stop submission to hw, but do not yet complete requests by
975112ed2d3SChris Wilson 	 * rolling the global seqno forward (since this would complete requests
976112ed2d3SChris Wilson 	 * for which we haven't set the fence error to EIO yet).
977112ed2d3SChris Wilson 	 */
978cb823ed9SChris Wilson 	awake = reset_prepare(gt);
979112ed2d3SChris Wilson 
980112ed2d3SChris Wilson 	/* Even if the GPU reset fails, it should still stop the engines */
981cb823ed9SChris Wilson 	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
982cb823ed9SChris Wilson 		__intel_gt_reset(gt, ALL_ENGINES);
983112ed2d3SChris Wilson 
9845d904e3cSTvrtko Ursulin 	for_each_engine(engine, gt, id)
985112ed2d3SChris Wilson 		engine->submit_request = nop_submit_request;
986112ed2d3SChris Wilson 
987112ed2d3SChris Wilson 	/*
988112ed2d3SChris Wilson 	 * Make sure no request can slip through without getting completed by
989112ed2d3SChris Wilson 	 * either this call here to intel_engine_write_global_seqno, or the one
990112ed2d3SChris Wilson 	 * in nop_submit_request.
991112ed2d3SChris Wilson 	 */
992112ed2d3SChris Wilson 	synchronize_rcu_expedited();
993cb823ed9SChris Wilson 	set_bit(I915_WEDGED, &gt->reset.flags);
994112ed2d3SChris Wilson 
995112ed2d3SChris Wilson 	/* Mark all executing requests as skipped */
99616f2941aSChris Wilson 	local_bh_disable();
9975d904e3cSTvrtko Ursulin 	for_each_engine(engine, gt, id)
998e26b6d43SChris Wilson 		if (engine->reset.cancel)
999e26b6d43SChris Wilson 			engine->reset.cancel(engine);
1000eb5e7da7SMatthew Brost 	intel_uc_cancel_requests(&gt->uc);
100116f2941aSChris Wilson 	local_bh_enable();
1002112ed2d3SChris Wilson 
1003cb823ed9SChris Wilson 	reset_finish(gt, awake);
1004112ed2d3SChris Wilson 
10053fbbbef4SChris Wilson 	GT_TRACE(gt, "end\n");
1006112ed2d3SChris Wilson }
1007112ed2d3SChris Wilson 
intel_gt_set_wedged(struct intel_gt * gt)1008cb823ed9SChris Wilson void intel_gt_set_wedged(struct intel_gt *gt)
1009112ed2d3SChris Wilson {
1010112ed2d3SChris Wilson 	intel_wakeref_t wakeref;
1011112ed2d3SChris Wilson 
1012a2847782SChris Wilson 	if (test_bit(I915_WEDGED, &gt->reset.flags))
1013a2847782SChris Wilson 		return;
1014a2847782SChris Wilson 
1015a2847782SChris Wilson 	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1016cb823ed9SChris Wilson 	mutex_lock(&gt->reset.mutex);
1017a2847782SChris Wilson 
1018a2847782SChris Wilson 	if (GEM_SHOW_DEBUG()) {
1019a2847782SChris Wilson 		struct drm_printer p = drm_debug_printer(__func__);
1020a2847782SChris Wilson 		struct intel_engine_cs *engine;
1021a2847782SChris Wilson 		enum intel_engine_id id;
1022a2847782SChris Wilson 
1023a2847782SChris Wilson 		drm_printf(&p, "called from %pS\n", (void *)_RET_IP_);
1024a2847782SChris Wilson 		for_each_engine(engine, gt, id) {
1025a2847782SChris Wilson 			if (intel_engine_is_idle(engine))
1026a2847782SChris Wilson 				continue;
1027a2847782SChris Wilson 
1028a2847782SChris Wilson 			intel_engine_dump(engine, &p, "%s\n", engine->name);
1029a2847782SChris Wilson 		}
1030a2847782SChris Wilson 	}
1031a2847782SChris Wilson 
1032cb823ed9SChris Wilson 	__intel_gt_set_wedged(gt);
1033a2847782SChris Wilson 
1034cb823ed9SChris Wilson 	mutex_unlock(&gt->reset.mutex);
1035a2847782SChris Wilson 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1036112ed2d3SChris Wilson }
1037112ed2d3SChris Wilson 
__intel_gt_unset_wedged(struct intel_gt * gt)1038cb823ed9SChris Wilson static bool __intel_gt_unset_wedged(struct intel_gt *gt)
1039112ed2d3SChris Wilson {
1040cb823ed9SChris Wilson 	struct intel_gt_timelines *timelines = &gt->timelines;
1041f0c02c1bSTvrtko Ursulin 	struct intel_timeline *tl;
10421d6f1d16SChris Wilson 	bool ok;
1043112ed2d3SChris Wilson 
1044cb823ed9SChris Wilson 	if (!test_bit(I915_WEDGED, &gt->reset.flags))
1045112ed2d3SChris Wilson 		return true;
1046112ed2d3SChris Wilson 
10475311f517SMichał Winiarski 	/* Never fully initialised, recovery impossible */
10483f04bdceSMichał Winiarski 	if (intel_gt_has_unrecoverable_error(gt))
1049112ed2d3SChris Wilson 		return false;
1050112ed2d3SChris Wilson 
10513fbbbef4SChris Wilson 	GT_TRACE(gt, "start\n");
1052112ed2d3SChris Wilson 
1053112ed2d3SChris Wilson 	/*
1054112ed2d3SChris Wilson 	 * Before unwedging, make sure that all pending operations
1055112ed2d3SChris Wilson 	 * are flushed and errored out - we may have requests waiting upon
1056112ed2d3SChris Wilson 	 * third party fences. We marked all inflight requests as EIO, and
1057112ed2d3SChris Wilson 	 * every execbuf since returned EIO, for consistency we want all
1058112ed2d3SChris Wilson 	 * the currently pending requests to also be marked as EIO, which
1059112ed2d3SChris Wilson 	 * is done inside our nop_submit_request - and so we must wait.
1060112ed2d3SChris Wilson 	 *
1061112ed2d3SChris Wilson 	 * No more can be submitted until we reset the wedged bit.
1062112ed2d3SChris Wilson 	 */
106388cec497SChris Wilson 	spin_lock(&timelines->lock);
1064cb823ed9SChris Wilson 	list_for_each_entry(tl, &timelines->active_list, link) {
1065b1e3177bSChris Wilson 		struct dma_fence *fence;
1066112ed2d3SChris Wilson 
1067b1e3177bSChris Wilson 		fence = i915_active_fence_get(&tl->last_request);
1068b1e3177bSChris Wilson 		if (!fence)
1069112ed2d3SChris Wilson 			continue;
1070112ed2d3SChris Wilson 
107188cec497SChris Wilson 		spin_unlock(&timelines->lock);
1072338aade9SChris Wilson 
1073112ed2d3SChris Wilson 		/*
1074112ed2d3SChris Wilson 		 * All internal dependencies (i915_requests) will have
1075112ed2d3SChris Wilson 		 * been flushed by the set-wedge, but we may be stuck waiting
1076112ed2d3SChris Wilson 		 * for external fences. These should all be capped to 10s
1077112ed2d3SChris Wilson 		 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded
1078112ed2d3SChris Wilson 		 * in the worst case.
1079112ed2d3SChris Wilson 		 */
1080b1e3177bSChris Wilson 		dma_fence_default_wait(fence, false, MAX_SCHEDULE_TIMEOUT);
1081b1e3177bSChris Wilson 		dma_fence_put(fence);
1082338aade9SChris Wilson 
1083338aade9SChris Wilson 		/* Restart iteration after droping lock */
108488cec497SChris Wilson 		spin_lock(&timelines->lock);
1085338aade9SChris Wilson 		tl = list_entry(&timelines->active_list, typeof(*tl), link);
1086112ed2d3SChris Wilson 	}
108788cec497SChris Wilson 	spin_unlock(&timelines->lock);
1088112ed2d3SChris Wilson 
10891d6f1d16SChris Wilson 	/* We must reset pending GPU events before restoring our submission */
10901d6f1d16SChris Wilson 	ok = !HAS_EXECLISTS(gt->i915); /* XXX better agnosticism desired */
10911d6f1d16SChris Wilson 	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
10921d6f1d16SChris Wilson 		ok = __intel_gt_reset(gt, ALL_ENGINES) == 0;
1093542a5c66SChris Wilson 	if (!ok) {
1094542a5c66SChris Wilson 		/*
1095542a5c66SChris Wilson 		 * Warn CI about the unrecoverable wedged condition.
1096542a5c66SChris Wilson 		 * Time for a reboot.
1097542a5c66SChris Wilson 		 */
109865706203SMichał Winiarski 		add_taint_for_CI(gt->i915, TAINT_WARN);
10991d6f1d16SChris Wilson 		return false;
1100542a5c66SChris Wilson 	}
1101112ed2d3SChris Wilson 
1102112ed2d3SChris Wilson 	/*
1103112ed2d3SChris Wilson 	 * Undo nop_submit_request. We prevent all new i915 requests from
1104112ed2d3SChris Wilson 	 * being queued (by disallowing execbuf whilst wedged) so having
1105112ed2d3SChris Wilson 	 * waited for all active requests above, we know the system is idle
1106112ed2d3SChris Wilson 	 * and do not have to worry about a thread being inside
1107112ed2d3SChris Wilson 	 * engine->submit_request() as we swap over. So unlike installing
1108112ed2d3SChris Wilson 	 * the nop_submit_request on reset, we can do this from normal
1109112ed2d3SChris Wilson 	 * context and do not require stop_machine().
1110112ed2d3SChris Wilson 	 */
1111cb823ed9SChris Wilson 	intel_engines_reset_default_submission(gt);
1112112ed2d3SChris Wilson 
11133fbbbef4SChris Wilson 	GT_TRACE(gt, "end\n");
1114112ed2d3SChris Wilson 
1115112ed2d3SChris Wilson 	smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
1116cb823ed9SChris Wilson 	clear_bit(I915_WEDGED, &gt->reset.flags);
1117112ed2d3SChris Wilson 
1118112ed2d3SChris Wilson 	return true;
1119112ed2d3SChris Wilson }
1120112ed2d3SChris Wilson 
intel_gt_unset_wedged(struct intel_gt * gt)1121cb823ed9SChris Wilson bool intel_gt_unset_wedged(struct intel_gt *gt)
1122112ed2d3SChris Wilson {
1123112ed2d3SChris Wilson 	bool result;
1124112ed2d3SChris Wilson 
1125cb823ed9SChris Wilson 	mutex_lock(&gt->reset.mutex);
1126cb823ed9SChris Wilson 	result = __intel_gt_unset_wedged(gt);
1127cb823ed9SChris Wilson 	mutex_unlock(&gt->reset.mutex);
1128112ed2d3SChris Wilson 
1129112ed2d3SChris Wilson 	return result;
1130112ed2d3SChris Wilson }
1131112ed2d3SChris Wilson 
do_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask)1132cb823ed9SChris Wilson static int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
1133112ed2d3SChris Wilson {
1134112ed2d3SChris Wilson 	int err, i;
1135112ed2d3SChris Wilson 
1136cb823ed9SChris Wilson 	err = __intel_gt_reset(gt, ALL_ENGINES);
1137112ed2d3SChris Wilson 	for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
1138112ed2d3SChris Wilson 		msleep(10 * (i + 1));
1139cb823ed9SChris Wilson 		err = __intel_gt_reset(gt, ALL_ENGINES);
1140112ed2d3SChris Wilson 	}
1141112ed2d3SChris Wilson 	if (err)
1142112ed2d3SChris Wilson 		return err;
1143112ed2d3SChris Wilson 
1144cb823ed9SChris Wilson 	return gt_reset(gt, stalled_mask);
1145112ed2d3SChris Wilson }
1146112ed2d3SChris Wilson 
resume(struct intel_gt * gt)1147cb823ed9SChris Wilson static int resume(struct intel_gt *gt)
1148092be382SChris Wilson {
1149092be382SChris Wilson 	struct intel_engine_cs *engine;
1150092be382SChris Wilson 	enum intel_engine_id id;
1151092be382SChris Wilson 	int ret;
1152092be382SChris Wilson 
11535d904e3cSTvrtko Ursulin 	for_each_engine(engine, gt, id) {
1154faea1792SDaniele Ceraolo Spurio 		ret = intel_engine_resume(engine);
1155092be382SChris Wilson 		if (ret)
1156092be382SChris Wilson 			return ret;
1157092be382SChris Wilson 	}
1158092be382SChris Wilson 
1159092be382SChris Wilson 	return 0;
1160092be382SChris Wilson }
1161092be382SChris Wilson 
1162112ed2d3SChris Wilson /**
1163cb823ed9SChris Wilson  * intel_gt_reset - reset chip after a hang
1164cb823ed9SChris Wilson  * @gt: #intel_gt to reset
1165112ed2d3SChris Wilson  * @stalled_mask: mask of the stalled engines with the guilty requests
1166112ed2d3SChris Wilson  * @reason: user error message for why we are resetting
1167112ed2d3SChris Wilson  *
1168112ed2d3SChris Wilson  * Reset the chip.  Useful if a hang is detected. Marks the device as wedged
1169112ed2d3SChris Wilson  * on failure.
1170112ed2d3SChris Wilson  *
1171112ed2d3SChris Wilson  * Procedure is fairly simple:
1172112ed2d3SChris Wilson  *   - reset the chip using the reset reg
1173112ed2d3SChris Wilson  *   - re-init context state
1174112ed2d3SChris Wilson  *   - re-init hardware status page
1175112ed2d3SChris Wilson  *   - re-init ring buffer
1176112ed2d3SChris Wilson  *   - re-init interrupt state
1177112ed2d3SChris Wilson  *   - re-init display
1178112ed2d3SChris Wilson  */
intel_gt_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask,const char * reason)1179cb823ed9SChris Wilson void intel_gt_reset(struct intel_gt *gt,
1180112ed2d3SChris Wilson 		    intel_engine_mask_t stalled_mask,
1181112ed2d3SChris Wilson 		    const char *reason)
1182112ed2d3SChris Wilson {
118318398904SChris Wilson 	intel_engine_mask_t awake;
1184112ed2d3SChris Wilson 	int ret;
1185112ed2d3SChris Wilson 
11863fbbbef4SChris Wilson 	GT_TRACE(gt, "flags=%lx\n", gt->reset.flags);
1187112ed2d3SChris Wilson 
1188112ed2d3SChris Wilson 	might_sleep();
1189cb823ed9SChris Wilson 	GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
11905b0a78ecSMaarten Lankhorst 
11915b0a78ecSMaarten Lankhorst 	/*
11925b0a78ecSMaarten Lankhorst 	 * FIXME: Revoking cpu mmap ptes cannot be done from a dma_fence
11935b0a78ecSMaarten Lankhorst 	 * critical section like gpu reset.
11945b0a78ecSMaarten Lankhorst 	 */
11955b0a78ecSMaarten Lankhorst 	gt_revoke(gt);
11965b0a78ecSMaarten Lankhorst 
1197cb823ed9SChris Wilson 	mutex_lock(&gt->reset.mutex);
1198112ed2d3SChris Wilson 
1199112ed2d3SChris Wilson 	/* Clear any previous failed attempts at recovery. Time to try again. */
1200cb823ed9SChris Wilson 	if (!__intel_gt_unset_wedged(gt))
120133df8a76SChris Wilson 		goto unlock;
1202112ed2d3SChris Wilson 
1203112ed2d3SChris Wilson 	if (reason)
1204dc483ba5SJani Nikula 		drm_notice(&gt->i915->drm,
1205cb823ed9SChris Wilson 			   "Resetting chip for %s\n", reason);
1206cb823ed9SChris Wilson 	atomic_inc(&gt->i915->gpu_error.reset_count);
1207112ed2d3SChris Wilson 
1208cb823ed9SChris Wilson 	awake = reset_prepare(gt);
1209112ed2d3SChris Wilson 
1210260e6b71SChris Wilson 	if (!intel_has_gpu_reset(gt)) {
12118a25c4beSJani Nikula 		if (gt->i915->params.reset)
1212dc483ba5SJani Nikula 			drm_err(&gt->i915->drm, "GPU reset not supported\n");
1213112ed2d3SChris Wilson 		else
1214f8474622SWambui Karuga 			drm_dbg(&gt->i915->drm, "GPU reset disabled\n");
1215112ed2d3SChris Wilson 		goto error;
1216112ed2d3SChris Wilson 	}
1217112ed2d3SChris Wilson 
1218cb823ed9SChris Wilson 	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1219cb823ed9SChris Wilson 		intel_runtime_pm_disable_interrupts(gt->i915);
1220112ed2d3SChris Wilson 
1221cb823ed9SChris Wilson 	if (do_reset(gt, stalled_mask)) {
1222dc483ba5SJani Nikula 		drm_err(&gt->i915->drm, "Failed to reset chip\n");
1223112ed2d3SChris Wilson 		goto taint;
1224112ed2d3SChris Wilson 	}
1225112ed2d3SChris Wilson 
1226cb823ed9SChris Wilson 	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1227cb823ed9SChris Wilson 		intel_runtime_pm_enable_interrupts(gt->i915);
1228112ed2d3SChris Wilson 
1229cb823ed9SChris Wilson 	intel_overlay_reset(gt->i915);
1230112ed2d3SChris Wilson 
1231112ed2d3SChris Wilson 	/*
1232112ed2d3SChris Wilson 	 * Next we need to restore the context, but we don't use those
1233112ed2d3SChris Wilson 	 * yet either...
1234112ed2d3SChris Wilson 	 *
1235112ed2d3SChris Wilson 	 * Ring buffer needs to be re-initialized in the KMS case, or if X
1236112ed2d3SChris Wilson 	 * was running at the time of the reset (i.e. we weren't VT
1237112ed2d3SChris Wilson 	 * switched away).
1238112ed2d3SChris Wilson 	 */
123961fa60ffSTvrtko Ursulin 	ret = intel_gt_init_hw(gt);
1240112ed2d3SChris Wilson 	if (ret) {
1241f8474622SWambui Karuga 		drm_err(&gt->i915->drm,
1242f8474622SWambui Karuga 			"Failed to initialise HW following reset (%d)\n",
1243112ed2d3SChris Wilson 			ret);
1244092be382SChris Wilson 		goto taint;
1245112ed2d3SChris Wilson 	}
1246112ed2d3SChris Wilson 
1247cb823ed9SChris Wilson 	ret = resume(gt);
1248092be382SChris Wilson 	if (ret)
1249092be382SChris Wilson 		goto taint;
1250092be382SChris Wilson 
1251112ed2d3SChris Wilson finish:
1252cb823ed9SChris Wilson 	reset_finish(gt, awake);
125333df8a76SChris Wilson unlock:
1254cb823ed9SChris Wilson 	mutex_unlock(&gt->reset.mutex);
1255112ed2d3SChris Wilson 	return;
1256112ed2d3SChris Wilson 
1257112ed2d3SChris Wilson taint:
1258112ed2d3SChris Wilson 	/*
1259112ed2d3SChris Wilson 	 * History tells us that if we cannot reset the GPU now, we
1260112ed2d3SChris Wilson 	 * never will. This then impacts everything that is run
1261112ed2d3SChris Wilson 	 * subsequently. On failing the reset, we mark the driver
1262112ed2d3SChris Wilson 	 * as wedged, preventing further execution on the GPU.
1263112ed2d3SChris Wilson 	 * We also want to go one step further and add a taint to the
1264112ed2d3SChris Wilson 	 * kernel so that any subsequent faults can be traced back to
1265112ed2d3SChris Wilson 	 * this failure. This is important for CI, where if the
1266112ed2d3SChris Wilson 	 * GPU/driver fails we would like to reboot and restart testing
1267112ed2d3SChris Wilson 	 * rather than continue on into oblivion. For everyone else,
1268112ed2d3SChris Wilson 	 * the system should still plod along, but they have been warned!
1269112ed2d3SChris Wilson 	 */
127065706203SMichał Winiarski 	add_taint_for_CI(gt->i915, TAINT_WARN);
1271112ed2d3SChris Wilson error:
1272cb823ed9SChris Wilson 	__intel_gt_set_wedged(gt);
1273112ed2d3SChris Wilson 	goto finish;
1274112ed2d3SChris Wilson }
1275112ed2d3SChris Wilson 
intel_gt_reset_engine(struct intel_engine_cs * engine)12769834dfefSChris Wilson static int intel_gt_reset_engine(struct intel_engine_cs *engine)
1277112ed2d3SChris Wilson {
1278cb823ed9SChris Wilson 	return __intel_gt_reset(engine->gt, engine->mask);
1279112ed2d3SChris Wilson }
1280112ed2d3SChris Wilson 
__intel_engine_reset_bh(struct intel_engine_cs * engine,const char * msg)128116f2941aSChris Wilson int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
1282112ed2d3SChris Wilson {
1283cb823ed9SChris Wilson 	struct intel_gt *gt = engine->gt;
1284112ed2d3SChris Wilson 	int ret;
1285112ed2d3SChris Wilson 
1286639f2f24SVenkata Sandeep Dhanalakota 	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
1287cb823ed9SChris Wilson 	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
1288112ed2d3SChris Wilson 
1289eb5e7da7SMatthew Brost 	if (intel_engine_uses_guc(engine))
1290eb5e7da7SMatthew Brost 		return -ENODEV;
1291eb5e7da7SMatthew Brost 
129218398904SChris Wilson 	if (!intel_engine_pm_get_if_awake(engine))
129379ffac85SChris Wilson 		return 0;
129479ffac85SChris Wilson 
1295112ed2d3SChris Wilson 	reset_prepare_engine(engine);
1296112ed2d3SChris Wilson 
1297112ed2d3SChris Wilson 	if (msg)
1298dc483ba5SJani Nikula 		drm_notice(&engine->i915->drm,
1299112ed2d3SChris Wilson 			   "Resetting %s for %s\n", engine->name, msg);
1300cd378c37STvrtko Ursulin 	i915_increase_reset_engine_count(&engine->i915->gpu_error, engine);
1301112ed2d3SChris Wilson 
1302c92c36edSChris Wilson 	ret = intel_gt_reset_engine(engine);
1303112ed2d3SChris Wilson 	if (ret) {
1304112ed2d3SChris Wilson 		/* If we fail here, we expect to fallback to a global reset */
1305eb5e7da7SMatthew Brost 		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
1306112ed2d3SChris Wilson 		goto out;
1307112ed2d3SChris Wilson 	}
1308112ed2d3SChris Wilson 
1309112ed2d3SChris Wilson 	/*
1310112ed2d3SChris Wilson 	 * The request that caused the hang is stuck on elsp, we know the
1311112ed2d3SChris Wilson 	 * active request and can drop it, adjust head to skip the offending
1312112ed2d3SChris Wilson 	 * request to resume executing remaining requests in the queue.
1313112ed2d3SChris Wilson 	 */
1314cb823ed9SChris Wilson 	__intel_engine_reset(engine, true);
1315112ed2d3SChris Wilson 
1316112ed2d3SChris Wilson 	/*
1317112ed2d3SChris Wilson 	 * The engine and its registers (and workarounds in case of render)
1318112ed2d3SChris Wilson 	 * have been reset to their default values. Follow the init_ring
1319112ed2d3SChris Wilson 	 * process to program RING_MODE, HWSP and re-enable submission.
1320112ed2d3SChris Wilson 	 */
1321faea1792SDaniele Ceraolo Spurio 	ret = intel_engine_resume(engine);
1322112ed2d3SChris Wilson 
1323112ed2d3SChris Wilson out:
1324112ed2d3SChris Wilson 	intel_engine_cancel_stop_cs(engine);
1325112ed2d3SChris Wilson 	reset_finish_engine(engine);
132607779a76SChris Wilson 	intel_engine_pm_put_async(engine);
1327112ed2d3SChris Wilson 	return ret;
1328112ed2d3SChris Wilson }
1329112ed2d3SChris Wilson 
133016f2941aSChris Wilson /**
133116f2941aSChris Wilson  * intel_engine_reset - reset GPU engine to recover from a hang
133216f2941aSChris Wilson  * @engine: engine to reset
133316f2941aSChris Wilson  * @msg: reason for GPU reset; or NULL for no drm_notice()
133416f2941aSChris Wilson  *
133516f2941aSChris Wilson  * Reset a specific GPU engine. Useful if a hang is detected.
133616f2941aSChris Wilson  * Returns zero on successful reset or otherwise an error code.
133716f2941aSChris Wilson  *
133816f2941aSChris Wilson  * Procedure is:
133916f2941aSChris Wilson  *  - identifies the request that caused the hang and it is dropped
134016f2941aSChris Wilson  *  - reset engine (which will force the engine to idle)
134116f2941aSChris Wilson  *  - re-init/configure engine
134216f2941aSChris Wilson  */
intel_engine_reset(struct intel_engine_cs * engine,const char * msg)134316f2941aSChris Wilson int intel_engine_reset(struct intel_engine_cs *engine, const char *msg)
134416f2941aSChris Wilson {
134516f2941aSChris Wilson 	int err;
134616f2941aSChris Wilson 
134716f2941aSChris Wilson 	local_bh_disable();
134816f2941aSChris Wilson 	err = __intel_engine_reset_bh(engine, msg);
134916f2941aSChris Wilson 	local_bh_enable();
135016f2941aSChris Wilson 
135116f2941aSChris Wilson 	return err;
135216f2941aSChris Wilson }
135316f2941aSChris Wilson 
intel_gt_reset_global(struct intel_gt * gt,u32 engine_mask,const char * reason)1354cb823ed9SChris Wilson static void intel_gt_reset_global(struct intel_gt *gt,
1355112ed2d3SChris Wilson 				  u32 engine_mask,
1356112ed2d3SChris Wilson 				  const char *reason)
1357112ed2d3SChris Wilson {
1358cb823ed9SChris Wilson 	struct kobject *kobj = &gt->i915->drm.primary->kdev->kobj;
1359112ed2d3SChris Wilson 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1360112ed2d3SChris Wilson 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1361112ed2d3SChris Wilson 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1362cb823ed9SChris Wilson 	struct intel_wedge_me w;
1363112ed2d3SChris Wilson 
1364112ed2d3SChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
1365112ed2d3SChris Wilson 
1366cb56a07dSChris Wilson 	GT_TRACE(gt, "resetting chip, engines=%x\n", engine_mask);
1367112ed2d3SChris Wilson 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
1368112ed2d3SChris Wilson 
1369112ed2d3SChris Wilson 	/* Use a watchdog to ensure that our reset completes */
1370d24e7855SChris Wilson 	intel_wedge_on_timeout(&w, gt, 60 * HZ) {
137159c6106eSJani Nikula 		intel_display_reset_prepare(gt->i915);
1372112ed2d3SChris Wilson 
1373cb823ed9SChris Wilson 		intel_gt_reset(gt, engine_mask, reason);
1374112ed2d3SChris Wilson 
137559c6106eSJani Nikula 		intel_display_reset_finish(gt->i915);
1376112ed2d3SChris Wilson 	}
1377112ed2d3SChris Wilson 
1378cb823ed9SChris Wilson 	if (!test_bit(I915_WEDGED, &gt->reset.flags))
1379112ed2d3SChris Wilson 		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
1380112ed2d3SChris Wilson }
1381112ed2d3SChris Wilson 
1382112ed2d3SChris Wilson /**
1383cb823ed9SChris Wilson  * intel_gt_handle_error - handle a gpu error
1384cb823ed9SChris Wilson  * @gt: the intel_gt
1385112ed2d3SChris Wilson  * @engine_mask: mask representing engines that are hung
1386112ed2d3SChris Wilson  * @flags: control flags
1387112ed2d3SChris Wilson  * @fmt: Error message format string
1388112ed2d3SChris Wilson  *
1389112ed2d3SChris Wilson  * Do some basic checking of register state at error time and
1390112ed2d3SChris Wilson  * dump it to the syslog.  Also call i915_capture_error_state() to make
1391112ed2d3SChris Wilson  * sure we get a record and make it available in debugfs.  Fire a uevent
1392112ed2d3SChris Wilson  * so userspace knows something bad happened (should trigger collection
1393112ed2d3SChris Wilson  * of a ring dump etc.).
1394112ed2d3SChris Wilson  */
intel_gt_handle_error(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned long flags,const char * fmt,...)1395cb823ed9SChris Wilson void intel_gt_handle_error(struct intel_gt *gt,
1396112ed2d3SChris Wilson 			   intel_engine_mask_t engine_mask,
1397112ed2d3SChris Wilson 			   unsigned long flags,
1398112ed2d3SChris Wilson 			   const char *fmt, ...)
1399112ed2d3SChris Wilson {
1400112ed2d3SChris Wilson 	struct intel_engine_cs *engine;
1401112ed2d3SChris Wilson 	intel_wakeref_t wakeref;
1402112ed2d3SChris Wilson 	intel_engine_mask_t tmp;
1403112ed2d3SChris Wilson 	char error_msg[80];
1404112ed2d3SChris Wilson 	char *msg = NULL;
1405112ed2d3SChris Wilson 
1406112ed2d3SChris Wilson 	if (fmt) {
1407112ed2d3SChris Wilson 		va_list args;
1408112ed2d3SChris Wilson 
1409112ed2d3SChris Wilson 		va_start(args, fmt);
1410112ed2d3SChris Wilson 		vscnprintf(error_msg, sizeof(error_msg), fmt, args);
1411112ed2d3SChris Wilson 		va_end(args);
1412112ed2d3SChris Wilson 
1413112ed2d3SChris Wilson 		msg = error_msg;
1414112ed2d3SChris Wilson 	}
1415112ed2d3SChris Wilson 
1416112ed2d3SChris Wilson 	/*
1417112ed2d3SChris Wilson 	 * In most cases it's guaranteed that we get here with an RPM
1418112ed2d3SChris Wilson 	 * reference held, for example because there is a pending GPU
1419112ed2d3SChris Wilson 	 * request that won't finish until the reset is done. This
1420112ed2d3SChris Wilson 	 * isn't the case at least when we get here by doing a
1421112ed2d3SChris Wilson 	 * simulated reset via debugfs, so get an RPM reference.
1422112ed2d3SChris Wilson 	 */
1423cd6a8513SChris Wilson 	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1424112ed2d3SChris Wilson 
1425792592e7SDaniele Ceraolo Spurio 	engine_mask &= gt->info.engine_mask;
1426112ed2d3SChris Wilson 
1427112ed2d3SChris Wilson 	if (flags & I915_ERROR_CAPTURE) {
1428a6f0f9cfSAlan Previn 		i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_NONE);
1429cb823ed9SChris Wilson 		intel_gt_clear_error_registers(gt, engine_mask);
1430112ed2d3SChris Wilson 	}
1431112ed2d3SChris Wilson 
1432112ed2d3SChris Wilson 	/*
1433112ed2d3SChris Wilson 	 * Try engine reset when available. We fall back to full reset if
1434112ed2d3SChris Wilson 	 * single reset fails.
1435112ed2d3SChris Wilson 	 */
1436eb5e7da7SMatthew Brost 	if (!intel_uc_uses_guc_submission(&gt->uc) &&
1437eb5e7da7SMatthew Brost 	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
143816f2941aSChris Wilson 		local_bh_disable();
1439a50134b1STvrtko Ursulin 		for_each_engine_masked(engine, gt, engine_mask, tmp) {
1440112ed2d3SChris Wilson 			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
1441112ed2d3SChris Wilson 			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1442cb823ed9SChris Wilson 					     &gt->reset.flags))
1443112ed2d3SChris Wilson 				continue;
1444112ed2d3SChris Wilson 
144516f2941aSChris Wilson 			if (__intel_engine_reset_bh(engine, msg) == 0)
1446112ed2d3SChris Wilson 				engine_mask &= ~engine->mask;
1447112ed2d3SChris Wilson 
1448cb823ed9SChris Wilson 			clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
1449cb823ed9SChris Wilson 					      &gt->reset.flags);
1450112ed2d3SChris Wilson 		}
145116f2941aSChris Wilson 		local_bh_enable();
1452112ed2d3SChris Wilson 	}
1453112ed2d3SChris Wilson 
1454112ed2d3SChris Wilson 	if (!engine_mask)
1455112ed2d3SChris Wilson 		goto out;
1456112ed2d3SChris Wilson 
1457112ed2d3SChris Wilson 	/* Full reset needs the mutex, stop any other user trying to do so. */
1458cb823ed9SChris Wilson 	if (test_and_set_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
1459cb823ed9SChris Wilson 		wait_event(gt->reset.queue,
1460cb823ed9SChris Wilson 			   !test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
1461112ed2d3SChris Wilson 		goto out; /* piggy-back on the other reset */
1462112ed2d3SChris Wilson 	}
1463112ed2d3SChris Wilson 
1464112ed2d3SChris Wilson 	/* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
1465112ed2d3SChris Wilson 	synchronize_rcu_expedited();
1466112ed2d3SChris Wilson 
146703f060b7SMatthew Brost 	/*
146803f060b7SMatthew Brost 	 * Prevent any other reset-engine attempt. We don't do this for GuC
146903f060b7SMatthew Brost 	 * submission the GuC owns the per-engine reset, not the i915.
147003f060b7SMatthew Brost 	 */
147103f060b7SMatthew Brost 	if (!intel_uc_uses_guc_submission(&gt->uc)) {
14725d904e3cSTvrtko Ursulin 		for_each_engine(engine, gt, tmp) {
1473112ed2d3SChris Wilson 			while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1474cb823ed9SChris Wilson 						&gt->reset.flags))
1475cb823ed9SChris Wilson 				wait_on_bit(&gt->reset.flags,
1476112ed2d3SChris Wilson 					    I915_RESET_ENGINE + engine->id,
1477112ed2d3SChris Wilson 					    TASK_UNINTERRUPTIBLE);
1478112ed2d3SChris Wilson 		}
147903f060b7SMatthew Brost 	}
1480112ed2d3SChris Wilson 
14811dab4561SChris Wilson 	/* Flush everyone using a resource about to be clobbered */
14821dab4561SChris Wilson 	synchronize_srcu_expedited(&gt->reset.backoff_srcu);
14831dab4561SChris Wilson 
1484cb823ed9SChris Wilson 	intel_gt_reset_global(gt, engine_mask, msg);
1485112ed2d3SChris Wilson 
148603f060b7SMatthew Brost 	if (!intel_uc_uses_guc_submission(&gt->uc)) {
14875d904e3cSTvrtko Ursulin 		for_each_engine(engine, gt, tmp)
1488cb823ed9SChris Wilson 			clear_bit_unlock(I915_RESET_ENGINE + engine->id,
1489cb823ed9SChris Wilson 					 &gt->reset.flags);
149003f060b7SMatthew Brost 	}
1491cb823ed9SChris Wilson 	clear_bit_unlock(I915_RESET_BACKOFF, &gt->reset.flags);
1492cb823ed9SChris Wilson 	smp_mb__after_atomic();
1493cb823ed9SChris Wilson 	wake_up_all(&gt->reset.queue);
1494112ed2d3SChris Wilson 
1495112ed2d3SChris Wilson out:
1496cd6a8513SChris Wilson 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1497112ed2d3SChris Wilson }
1498112ed2d3SChris Wilson 
_intel_gt_reset_lock(struct intel_gt * gt,int * srcu,bool retry)1499178b8a36SJohn Harrison static int _intel_gt_reset_lock(struct intel_gt *gt, int *srcu, bool retry)
1500112ed2d3SChris Wilson {
1501cb823ed9SChris Wilson 	might_lock(&gt->reset.backoff_srcu);
1502178b8a36SJohn Harrison 	if (retry)
1503112ed2d3SChris Wilson 		might_sleep();
1504112ed2d3SChris Wilson 
1505112ed2d3SChris Wilson 	rcu_read_lock();
1506cb823ed9SChris Wilson 	while (test_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
1507112ed2d3SChris Wilson 		rcu_read_unlock();
1508112ed2d3SChris Wilson 
1509178b8a36SJohn Harrison 		if (!retry)
1510178b8a36SJohn Harrison 			return -EBUSY;
1511178b8a36SJohn Harrison 
1512cb823ed9SChris Wilson 		if (wait_event_interruptible(gt->reset.queue,
1513112ed2d3SChris Wilson 					     !test_bit(I915_RESET_BACKOFF,
1514cb823ed9SChris Wilson 						       &gt->reset.flags)))
1515112ed2d3SChris Wilson 			return -EINTR;
1516112ed2d3SChris Wilson 
1517112ed2d3SChris Wilson 		rcu_read_lock();
1518112ed2d3SChris Wilson 	}
1519eebab60fSChris Wilson 	*srcu = srcu_read_lock(&gt->reset.backoff_srcu);
1520112ed2d3SChris Wilson 	rcu_read_unlock();
1521112ed2d3SChris Wilson 
1522eebab60fSChris Wilson 	return 0;
1523112ed2d3SChris Wilson }
1524112ed2d3SChris Wilson 
intel_gt_reset_trylock(struct intel_gt * gt,int * srcu)1525178b8a36SJohn Harrison int intel_gt_reset_trylock(struct intel_gt *gt, int *srcu)
1526178b8a36SJohn Harrison {
1527178b8a36SJohn Harrison 	return _intel_gt_reset_lock(gt, srcu, false);
1528178b8a36SJohn Harrison }
1529178b8a36SJohn Harrison 
intel_gt_reset_lock_interruptible(struct intel_gt * gt,int * srcu)1530178b8a36SJohn Harrison int intel_gt_reset_lock_interruptible(struct intel_gt *gt, int *srcu)
1531178b8a36SJohn Harrison {
1532178b8a36SJohn Harrison 	return _intel_gt_reset_lock(gt, srcu, true);
1533178b8a36SJohn Harrison }
1534178b8a36SJohn Harrison 
intel_gt_reset_unlock(struct intel_gt * gt,int tag)1535cb823ed9SChris Wilson void intel_gt_reset_unlock(struct intel_gt *gt, int tag)
1536cb823ed9SChris Wilson __releases(&gt->reset.backoff_srcu)
1537112ed2d3SChris Wilson {
1538cb823ed9SChris Wilson 	srcu_read_unlock(&gt->reset.backoff_srcu, tag);
1539112ed2d3SChris Wilson }
1540112ed2d3SChris Wilson 
intel_gt_terminally_wedged(struct intel_gt * gt)1541cb823ed9SChris Wilson int intel_gt_terminally_wedged(struct intel_gt *gt)
1542112ed2d3SChris Wilson {
1543112ed2d3SChris Wilson 	might_sleep();
1544112ed2d3SChris Wilson 
1545cb823ed9SChris Wilson 	if (!intel_gt_is_wedged(gt))
1546112ed2d3SChris Wilson 		return 0;
1547112ed2d3SChris Wilson 
15483f04bdceSMichał Winiarski 	if (intel_gt_has_unrecoverable_error(gt))
1549112ed2d3SChris Wilson 		return -EIO;
1550112ed2d3SChris Wilson 
1551b761a7b4SChris Wilson 	/* Reset still in progress? Maybe we will recover? */
1552cb823ed9SChris Wilson 	if (wait_event_interruptible(gt->reset.queue,
1553112ed2d3SChris Wilson 				     !test_bit(I915_RESET_BACKOFF,
1554cb823ed9SChris Wilson 					       &gt->reset.flags)))
1555112ed2d3SChris Wilson 		return -EINTR;
1556112ed2d3SChris Wilson 
1557cb823ed9SChris Wilson 	return intel_gt_is_wedged(gt) ? -EIO : 0;
1558112ed2d3SChris Wilson }
1559112ed2d3SChris Wilson 
intel_gt_set_wedged_on_init(struct intel_gt * gt)15605311f517SMichał Winiarski void intel_gt_set_wedged_on_init(struct intel_gt *gt)
15615311f517SMichał Winiarski {
15625311f517SMichał Winiarski 	BUILD_BUG_ON(I915_RESET_ENGINE + I915_NUM_ENGINES >
15635311f517SMichał Winiarski 		     I915_WEDGED_ON_INIT);
15645311f517SMichał Winiarski 	intel_gt_set_wedged(gt);
15659a7fc952STvrtko Ursulin 	i915_disable_error_state(gt->i915, -ENODEV);
15665311f517SMichał Winiarski 	set_bit(I915_WEDGED_ON_INIT, &gt->reset.flags);
15673f04bdceSMichał Winiarski 
15683f04bdceSMichał Winiarski 	/* Wedged on init is non-recoverable */
156965706203SMichał Winiarski 	add_taint_for_CI(gt->i915, TAINT_WARN);
15703f04bdceSMichał Winiarski }
15713f04bdceSMichał Winiarski 
intel_gt_set_wedged_on_fini(struct intel_gt * gt)15723f04bdceSMichał Winiarski void intel_gt_set_wedged_on_fini(struct intel_gt *gt)
15733f04bdceSMichał Winiarski {
15743f04bdceSMichał Winiarski 	intel_gt_set_wedged(gt);
15759a7fc952STvrtko Ursulin 	i915_disable_error_state(gt->i915, -ENODEV);
15763f04bdceSMichał Winiarski 	set_bit(I915_WEDGED_ON_FINI, &gt->reset.flags);
1577b0573472SChris Wilson 	intel_gt_retire_requests(gt); /* cleanup any wedged requests */
15785311f517SMichał Winiarski }
15795311f517SMichał Winiarski 
intel_gt_init_reset(struct intel_gt * gt)1580cb823ed9SChris Wilson void intel_gt_init_reset(struct intel_gt *gt)
1581112ed2d3SChris Wilson {
1582cb823ed9SChris Wilson 	init_waitqueue_head(&gt->reset.queue);
1583cb823ed9SChris Wilson 	mutex_init(&gt->reset.mutex);
1584cb823ed9SChris Wilson 	init_srcu_struct(&gt->reset.backoff_srcu);
158545b152f7SChris Wilson 
1586cecb2af4SChris Wilson 	/*
1587cecb2af4SChris Wilson 	 * While undesirable to wait inside the shrinker, complain anyway.
1588cecb2af4SChris Wilson 	 *
1589cecb2af4SChris Wilson 	 * If we have to wait during shrinking, we guarantee forward progress
1590cecb2af4SChris Wilson 	 * by forcing the reset. Therefore during the reset we must not
1591cecb2af4SChris Wilson 	 * re-enter the shrinker. By declaring that we take the reset mutex
1592cecb2af4SChris Wilson 	 * within the shrinker, we forbid ourselves from performing any
1593cecb2af4SChris Wilson 	 * fs-reclaim or taking related locks during reset.
1594cecb2af4SChris Wilson 	 */
1595cecb2af4SChris Wilson 	i915_gem_shrinker_taints_mutex(gt->i915, &gt->reset.mutex);
1596cecb2af4SChris Wilson 
159745b152f7SChris Wilson 	/* no GPU until we are ready! */
159845b152f7SChris Wilson 	__set_bit(I915_WEDGED, &gt->reset.flags);
1599cb823ed9SChris Wilson }
1600112ed2d3SChris Wilson 
intel_gt_fini_reset(struct intel_gt * gt)1601cb823ed9SChris Wilson void intel_gt_fini_reset(struct intel_gt *gt)
1602cb823ed9SChris Wilson {
1603cb823ed9SChris Wilson 	cleanup_srcu_struct(&gt->reset.backoff_srcu);
1604cb823ed9SChris Wilson }
1605cb823ed9SChris Wilson 
intel_wedge_me(struct work_struct * work)1606cb823ed9SChris Wilson static void intel_wedge_me(struct work_struct *work)
1607cb823ed9SChris Wilson {
1608cb823ed9SChris Wilson 	struct intel_wedge_me *w = container_of(work, typeof(*w), work.work);
1609cb823ed9SChris Wilson 
1610dc483ba5SJani Nikula 	drm_err(&w->gt->i915->drm,
1611112ed2d3SChris Wilson 		"%s timed out, cancelling all in-flight rendering.\n",
1612112ed2d3SChris Wilson 		w->name);
1613cb823ed9SChris Wilson 	intel_gt_set_wedged(w->gt);
1614112ed2d3SChris Wilson }
1615112ed2d3SChris Wilson 
__intel_init_wedge(struct intel_wedge_me * w,struct intel_gt * gt,long timeout,const char * name)1616cb823ed9SChris Wilson void __intel_init_wedge(struct intel_wedge_me *w,
1617cb823ed9SChris Wilson 			struct intel_gt *gt,
1618112ed2d3SChris Wilson 			long timeout,
1619112ed2d3SChris Wilson 			const char *name)
1620112ed2d3SChris Wilson {
1621cb823ed9SChris Wilson 	w->gt = gt;
1622112ed2d3SChris Wilson 	w->name = name;
1623112ed2d3SChris Wilson 
1624cb823ed9SChris Wilson 	INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me);
1625848a4e5cSLuca Coelho 	queue_delayed_work(gt->i915->unordered_wq, &w->work, timeout);
1626112ed2d3SChris Wilson }
1627112ed2d3SChris Wilson 
__intel_fini_wedge(struct intel_wedge_me * w)1628cb823ed9SChris Wilson void __intel_fini_wedge(struct intel_wedge_me *w)
1629112ed2d3SChris Wilson {
1630112ed2d3SChris Wilson 	cancel_delayed_work_sync(&w->work);
1631112ed2d3SChris Wilson 	destroy_delayed_work_on_stack(&w->work);
1632cb823ed9SChris Wilson 	w->gt = NULL;
1633112ed2d3SChris Wilson }
1634932309fbSMichal Wajdeczko 
163567f7fba8SMatt Roper /*
163667f7fba8SMatt Roper  * Wa_22011802037 requires that we (or the GuC) ensure that no command
163767f7fba8SMatt Roper  * streamers are executing MI_FORCE_WAKE while an engine reset is initiated.
163867f7fba8SMatt Roper  */
intel_engine_reset_needs_wa_22011802037(struct intel_gt * gt)163967f7fba8SMatt Roper bool intel_engine_reset_needs_wa_22011802037(struct intel_gt *gt)
164067f7fba8SMatt Roper {
164167f7fba8SMatt Roper 	if (GRAPHICS_VER(gt->i915) < 11)
164267f7fba8SMatt Roper 		return false;
164367f7fba8SMatt Roper 
1644b3749611SMatt Roper 	if (IS_GFX_GT_IP_STEP(gt, IP_VER(12, 70), STEP_A0, STEP_B0))
164567f7fba8SMatt Roper 		return true;
164667f7fba8SMatt Roper 
164767f7fba8SMatt Roper 	if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70))
164867f7fba8SMatt Roper 		return false;
164967f7fba8SMatt Roper 
165067f7fba8SMatt Roper 	return true;
165167f7fba8SMatt Roper }
165267f7fba8SMatt Roper 
1653932309fbSMichal Wajdeczko #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1654932309fbSMichal Wajdeczko #include "selftest_reset.c"
1655058179e7SChris Wilson #include "selftest_hangcheck.c"
1656932309fbSMichal Wajdeczko #endif
1657