1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2008-2018 Intel Corporation
4  */
5 
6 #include <linux/sched/mm.h>
7 #include <linux/stop_machine.h>
8 #include <linux/string_helpers.h>
9 
10 #include "display/intel_display_reset.h"
11 #include "display/intel_overlay.h"
12 
13 #include "gem/i915_gem_context.h"
14 
15 #include "gt/intel_gt_regs.h"
16 
17 #include "gt/uc/intel_gsc_fw.h"
18 
19 #include "i915_drv.h"
20 #include "i915_file_private.h"
21 #include "i915_gpu_error.h"
22 #include "i915_irq.h"
23 #include "i915_reg.h"
24 #include "intel_breadcrumbs.h"
25 #include "intel_engine_pm.h"
26 #include "intel_engine_regs.h"
27 #include "intel_gt.h"
28 #include "intel_gt_pm.h"
29 #include "intel_gt_print.h"
30 #include "intel_gt_requests.h"
31 #include "intel_mchbar_regs.h"
32 #include "intel_pci_config.h"
33 #include "intel_reset.h"
34 
35 #include "uc/intel_guc.h"
36 
37 #define RESET_MAX_RETRIES 3
38 
client_mark_guilty(struct i915_gem_context * ctx,bool banned)39 static void client_mark_guilty(struct i915_gem_context *ctx, bool banned)
40 {
41 	struct drm_i915_file_private *file_priv = ctx->file_priv;
42 	unsigned long prev_hang;
43 	unsigned int score;
44 
45 	if (IS_ERR_OR_NULL(file_priv))
46 		return;
47 
48 	score = 0;
49 	if (banned)
50 		score = I915_CLIENT_SCORE_CONTEXT_BAN;
51 
52 	prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
53 	if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
54 		score += I915_CLIENT_SCORE_HANG_FAST;
55 
56 	if (score) {
57 		atomic_add(score, &file_priv->ban_score);
58 
59 		drm_dbg(&ctx->i915->drm,
60 			"client %s: gained %u ban score, now %u\n",
61 			ctx->name, score,
62 			atomic_read(&file_priv->ban_score));
63 	}
64 }
65 
mark_guilty(struct i915_request * rq)66 static bool mark_guilty(struct i915_request *rq)
67 {
68 	struct i915_gem_context *ctx;
69 	unsigned long prev_hang;
70 	bool banned;
71 	int i;
72 
73 	if (intel_context_is_closed(rq->context))
74 		return true;
75 
76 	rcu_read_lock();
77 	ctx = rcu_dereference(rq->context->gem_context);
78 	if (ctx && !kref_get_unless_zero(&ctx->ref))
79 		ctx = NULL;
80 	rcu_read_unlock();
81 	if (!ctx)
82 		return intel_context_is_banned(rq->context);
83 
84 	atomic_inc(&ctx->guilty_count);
85 
86 	/* Cool contexts are too cool to be banned! (Used for reset testing.) */
87 	if (!i915_gem_context_is_bannable(ctx)) {
88 		banned = false;
89 		goto out;
90 	}
91 
92 	drm_notice(&ctx->i915->drm,
93 		   "%s context reset due to GPU hang\n",
94 		   ctx->name);
95 
96 	/* Record the timestamp for the last N hangs */
97 	prev_hang = ctx->hang_timestamp[0];
98 	for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++)
99 		ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1];
100 	ctx->hang_timestamp[i] = jiffies;
101 
102 	/* If we have hung N+1 times in rapid succession, we ban the context! */
103 	banned = !i915_gem_context_is_recoverable(ctx);
104 	if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES))
105 		banned = true;
106 	if (banned)
107 		drm_dbg(&ctx->i915->drm, "context %s: guilty %d, banned\n",
108 			ctx->name, atomic_read(&ctx->guilty_count));
109 
110 	client_mark_guilty(ctx, banned);
111 
112 out:
113 	i915_gem_context_put(ctx);
114 	return banned;
115 }
116 
mark_innocent(struct i915_request * rq)117 static void mark_innocent(struct i915_request *rq)
118 {
119 	struct i915_gem_context *ctx;
120 
121 	rcu_read_lock();
122 	ctx = rcu_dereference(rq->context->gem_context);
123 	if (ctx)
124 		atomic_inc(&ctx->active_count);
125 	rcu_read_unlock();
126 }
127 
__i915_request_reset(struct i915_request * rq,bool guilty)128 void __i915_request_reset(struct i915_request *rq, bool guilty)
129 {
130 	bool banned = false;
131 
132 	RQ_TRACE(rq, "guilty? %s\n", str_yes_no(guilty));
133 	GEM_BUG_ON(__i915_request_is_complete(rq));
134 
135 	rcu_read_lock(); /* protect the GEM context */
136 	if (guilty) {
137 		i915_request_set_error_once(rq, -EIO);
138 		__i915_request_skip(rq);
139 		banned = mark_guilty(rq);
140 	} else {
141 		i915_request_set_error_once(rq, -EAGAIN);
142 		mark_innocent(rq);
143 	}
144 	rcu_read_unlock();
145 
146 	if (banned)
147 		intel_context_ban(rq->context, rq);
148 }
149 
i915_in_reset(struct pci_dev * pdev)150 static bool i915_in_reset(struct pci_dev *pdev)
151 {
152 	u8 gdrst;
153 
154 	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
155 	return gdrst & GRDOM_RESET_STATUS;
156 }
157 
i915_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)158 static int i915_do_reset(struct intel_gt *gt,
159 			 intel_engine_mask_t engine_mask,
160 			 unsigned int retry)
161 {
162 	struct pci_dev *pdev = gt->i915->drm.pdev;
163 	int err;
164 
165 	/* Assert reset for at least 50 usec, and wait for acknowledgement. */
166 	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
167 	udelay(50);
168 	err = _wait_for_atomic(i915_in_reset(pdev), 50000, 0);
169 
170 	/* Clear the reset request. */
171 	pci_write_config_byte(pdev, I915_GDRST, 0);
172 	udelay(50);
173 	if (!err)
174 		err = _wait_for_atomic(!i915_in_reset(pdev), 50000, 0);
175 
176 	return err;
177 }
178 
g4x_reset_complete(struct pci_dev * pdev)179 static bool g4x_reset_complete(struct pci_dev *pdev)
180 {
181 	u8 gdrst;
182 
183 	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
184 	return (gdrst & GRDOM_RESET_ENABLE) == 0;
185 }
186 
g33_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)187 static int g33_do_reset(struct intel_gt *gt,
188 			intel_engine_mask_t engine_mask,
189 			unsigned int retry)
190 {
191 	struct pci_dev *pdev = gt->i915->drm.pdev;
192 
193 	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
194 	return _wait_for_atomic(g4x_reset_complete(pdev), 50000, 0);
195 }
196 
g4x_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)197 static int g4x_do_reset(struct intel_gt *gt,
198 			intel_engine_mask_t engine_mask,
199 			unsigned int retry)
200 {
201 	struct pci_dev *pdev = gt->i915->drm.pdev;
202 	struct intel_uncore *uncore = gt->uncore;
203 	int ret;
204 
205 	/* WaVcpClkGateDisableForMediaReset:ctg,elk */
206 	intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, 0, VCP_UNIT_CLOCK_GATE_DISABLE);
207 	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
208 
209 	pci_write_config_byte(pdev, I915_GDRST,
210 			      GRDOM_MEDIA | GRDOM_RESET_ENABLE);
211 	ret =  _wait_for_atomic(g4x_reset_complete(pdev), 50000, 0);
212 	if (ret) {
213 		GT_TRACE(gt, "Wait for media reset failed\n");
214 		goto out;
215 	}
216 
217 	pci_write_config_byte(pdev, I915_GDRST,
218 			      GRDOM_RENDER | GRDOM_RESET_ENABLE);
219 	ret =  _wait_for_atomic(g4x_reset_complete(pdev), 50000, 0);
220 	if (ret) {
221 		GT_TRACE(gt, "Wait for render reset failed\n");
222 		goto out;
223 	}
224 
225 out:
226 	pci_write_config_byte(pdev, I915_GDRST, 0);
227 
228 	intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE, 0);
229 	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
230 
231 	return ret;
232 }
233 
ilk_do_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)234 static int ilk_do_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask,
235 			unsigned int retry)
236 {
237 	struct intel_uncore *uncore = gt->uncore;
238 	int ret;
239 
240 	intel_uncore_write_fw(uncore, ILK_GDSR,
241 			      ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
242 	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
243 					   ILK_GRDOM_RESET_ENABLE, 0,
244 					   5000, 0,
245 					   NULL);
246 	if (ret) {
247 		GT_TRACE(gt, "Wait for render reset failed\n");
248 		goto out;
249 	}
250 
251 	intel_uncore_write_fw(uncore, ILK_GDSR,
252 			      ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
253 	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
254 					   ILK_GRDOM_RESET_ENABLE, 0,
255 					   5000, 0,
256 					   NULL);
257 	if (ret) {
258 		GT_TRACE(gt, "Wait for media reset failed\n");
259 		goto out;
260 	}
261 
262 out:
263 	intel_uncore_write_fw(uncore, ILK_GDSR, 0);
264 	intel_uncore_posting_read_fw(uncore, ILK_GDSR);
265 	return ret;
266 }
267 
268 /* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
gen6_hw_domain_reset(struct intel_gt * gt,u32 hw_domain_mask)269 static int gen6_hw_domain_reset(struct intel_gt *gt, u32 hw_domain_mask)
270 {
271 	struct intel_uncore *uncore = gt->uncore;
272 	int loops;
273 	int err;
274 
275 	/*
276 	 * On some platforms, e.g. Jasperlake, we see that the engine register
277 	 * state is not cleared until shortly after GDRST reports completion,
278 	 * causing a failure as we try to immediately resume while the internal
279 	 * state is still in flux. If we immediately repeat the reset, the
280 	 * second reset appears to serialise with the first, and since it is a
281 	 * no-op, the registers should retain their reset value. However, there
282 	 * is still a concern that upon leaving the second reset, the internal
283 	 * engine state is still in flux and not ready for resuming.
284 	 *
285 	 * Starting on MTL, there are some prep steps that we need to do when
286 	 * resetting some engines that need to be applied every time we write to
287 	 * GEN6_GDRST. As those are time consuming (tens of ms), we don't want
288 	 * to perform that twice, so, since the Jasperlake issue hasn't been
289 	 * observed on MTL, we avoid repeating the reset on newer platforms.
290 	 */
291 	loops = GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 70) ? 2 : 1;
292 
293 	/*
294 	 * GEN6_GDRST is not in the gt power well, no need to check
295 	 * for fifo space for the write or forcewake the chip for
296 	 * the read
297 	 */
298 	do {
299 		intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask);
300 
301 		/* Wait for the device to ack the reset requests. */
302 		err = __intel_wait_for_register_fw(uncore, GEN6_GDRST,
303 						   hw_domain_mask, 0,
304 						   2000, 0,
305 						   NULL);
306 	} while (err == 0 && --loops);
307 	if (err)
308 		GT_TRACE(gt,
309 			 "Wait for 0x%08x engines reset failed\n",
310 			 hw_domain_mask);
311 
312 	/*
313 	 * As we have observed that the engine state is still volatile
314 	 * after GDRST is acked, impose a small delay to let everything settle.
315 	 */
316 	udelay(50);
317 
318 	return err;
319 }
320 
__gen6_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)321 static int __gen6_reset_engines(struct intel_gt *gt,
322 				intel_engine_mask_t engine_mask,
323 				unsigned int retry)
324 {
325 	struct intel_engine_cs *engine;
326 	u32 hw_mask;
327 
328 	if (engine_mask == ALL_ENGINES) {
329 		hw_mask = GEN6_GRDOM_FULL;
330 	} else {
331 		intel_engine_mask_t tmp;
332 
333 		hw_mask = 0;
334 		for_each_engine_masked(engine, gt, engine_mask, tmp) {
335 			hw_mask |= engine->reset_domain;
336 		}
337 	}
338 
339 	return gen6_hw_domain_reset(gt, hw_mask);
340 }
341 
gen6_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)342 static int gen6_reset_engines(struct intel_gt *gt,
343 			      intel_engine_mask_t engine_mask,
344 			      unsigned int retry)
345 {
346 	unsigned long flags;
347 	int ret;
348 
349 	spin_lock_irqsave(&gt->uncore->lock, flags);
350 	ret = __gen6_reset_engines(gt, engine_mask, retry);
351 	spin_unlock_irqrestore(&gt->uncore->lock, flags);
352 
353 	return ret;
354 }
355 
find_sfc_paired_vecs_engine(struct intel_engine_cs * engine)356 static struct intel_engine_cs *find_sfc_paired_vecs_engine(struct intel_engine_cs *engine)
357 {
358 	int vecs_id;
359 
360 	GEM_BUG_ON(engine->class != VIDEO_DECODE_CLASS);
361 
362 	vecs_id = _VECS((engine->instance) / 2);
363 
364 	return engine->gt->engine[vecs_id];
365 }
366 
367 struct sfc_lock_data {
368 	i915_reg_t lock_reg;
369 	i915_reg_t ack_reg;
370 	i915_reg_t usage_reg;
371 	u32 lock_bit;
372 	u32 ack_bit;
373 	u32 usage_bit;
374 	u32 reset_bit;
375 };
376 
get_sfc_forced_lock_data(struct intel_engine_cs * engine,struct sfc_lock_data * sfc_lock)377 static void get_sfc_forced_lock_data(struct intel_engine_cs *engine,
378 				     struct sfc_lock_data *sfc_lock)
379 {
380 	switch (engine->class) {
381 	default:
382 		MISSING_CASE(engine->class);
383 		fallthrough;
384 	case VIDEO_DECODE_CLASS:
385 		sfc_lock->lock_reg = GEN11_VCS_SFC_FORCED_LOCK(engine->mmio_base);
386 		sfc_lock->lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
387 
388 		sfc_lock->ack_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
389 		sfc_lock->ack_bit  = GEN11_VCS_SFC_LOCK_ACK_BIT;
390 
391 		sfc_lock->usage_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
392 		sfc_lock->usage_bit = GEN11_VCS_SFC_USAGE_BIT;
393 		sfc_lock->reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
394 
395 		break;
396 	case VIDEO_ENHANCEMENT_CLASS:
397 		sfc_lock->lock_reg = GEN11_VECS_SFC_FORCED_LOCK(engine->mmio_base);
398 		sfc_lock->lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
399 
400 		sfc_lock->ack_reg = GEN11_VECS_SFC_LOCK_ACK(engine->mmio_base);
401 		sfc_lock->ack_bit  = GEN11_VECS_SFC_LOCK_ACK_BIT;
402 
403 		sfc_lock->usage_reg = GEN11_VECS_SFC_USAGE(engine->mmio_base);
404 		sfc_lock->usage_bit = GEN11_VECS_SFC_USAGE_BIT;
405 		sfc_lock->reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
406 
407 		break;
408 	}
409 }
410 
gen11_lock_sfc(struct intel_engine_cs * engine,u32 * reset_mask,u32 * unlock_mask)411 static int gen11_lock_sfc(struct intel_engine_cs *engine,
412 			  u32 *reset_mask,
413 			  u32 *unlock_mask)
414 {
415 	struct intel_uncore *uncore = engine->uncore;
416 	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
417 	struct sfc_lock_data sfc_lock;
418 	bool lock_obtained, lock_to_other = false;
419 	int ret;
420 
421 	switch (engine->class) {
422 	case VIDEO_DECODE_CLASS:
423 		if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
424 			return 0;
425 
426 		fallthrough;
427 	case VIDEO_ENHANCEMENT_CLASS:
428 		get_sfc_forced_lock_data(engine, &sfc_lock);
429 
430 		break;
431 	default:
432 		return 0;
433 	}
434 
435 	if (!(intel_uncore_read_fw(uncore, sfc_lock.usage_reg) & sfc_lock.usage_bit)) {
436 		struct intel_engine_cs *paired_vecs;
437 
438 		if (engine->class != VIDEO_DECODE_CLASS ||
439 		    GRAPHICS_VER(engine->i915) != 12)
440 			return 0;
441 
442 		/*
443 		 * Wa_14010733141
444 		 *
445 		 * If the VCS-MFX isn't using the SFC, we also need to check
446 		 * whether VCS-HCP is using it.  If so, we need to issue a *VE*
447 		 * forced lock on the VE engine that shares the same SFC.
448 		 */
449 		if (!(intel_uncore_read_fw(uncore,
450 					   GEN12_HCP_SFC_LOCK_STATUS(engine->mmio_base)) &
451 		      GEN12_HCP_SFC_USAGE_BIT))
452 			return 0;
453 
454 		paired_vecs = find_sfc_paired_vecs_engine(engine);
455 		get_sfc_forced_lock_data(paired_vecs, &sfc_lock);
456 		lock_to_other = true;
457 		*unlock_mask |= paired_vecs->mask;
458 	} else {
459 		*unlock_mask |= engine->mask;
460 	}
461 
462 	/*
463 	 * If the engine is using an SFC, tell the engine that a software reset
464 	 * is going to happen. The engine will then try to force lock the SFC.
465 	 * If SFC ends up being locked to the engine we want to reset, we have
466 	 * to reset it as well (we will unlock it once the reset sequence is
467 	 * completed).
468 	 */
469 	intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, 0, sfc_lock.lock_bit);
470 
471 	ret = __intel_wait_for_register_fw(uncore,
472 					   sfc_lock.ack_reg,
473 					   sfc_lock.ack_bit,
474 					   sfc_lock.ack_bit,
475 					   1000, 0, NULL);
476 
477 	/*
478 	 * Was the SFC released while we were trying to lock it?
479 	 *
480 	 * We should reset both the engine and the SFC if:
481 	 *  - We were locking the SFC to this engine and the lock succeeded
482 	 *       OR
483 	 *  - We were locking the SFC to a different engine (Wa_14010733141)
484 	 *    but the SFC was released before the lock was obtained.
485 	 *
486 	 * Otherwise we need only reset the engine by itself and we can
487 	 * leave the SFC alone.
488 	 */
489 	lock_obtained = (intel_uncore_read_fw(uncore, sfc_lock.usage_reg) &
490 			sfc_lock.usage_bit) != 0;
491 	if (lock_obtained == lock_to_other)
492 		return 0;
493 
494 	if (ret) {
495 		ENGINE_TRACE(engine, "Wait for SFC forced lock ack failed\n");
496 		return ret;
497 	}
498 
499 	*reset_mask |= sfc_lock.reset_bit;
500 	return 0;
501 }
502 
gen11_unlock_sfc(struct intel_engine_cs * engine)503 static void gen11_unlock_sfc(struct intel_engine_cs *engine)
504 {
505 	struct intel_uncore *uncore = engine->uncore;
506 	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
507 	struct sfc_lock_data sfc_lock = {};
508 
509 	if (engine->class != VIDEO_DECODE_CLASS &&
510 	    engine->class != VIDEO_ENHANCEMENT_CLASS)
511 		return;
512 
513 	if (engine->class == VIDEO_DECODE_CLASS &&
514 	    (BIT(engine->instance) & vdbox_sfc_access) == 0)
515 		return;
516 
517 	get_sfc_forced_lock_data(engine, &sfc_lock);
518 
519 	intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, sfc_lock.lock_bit, 0);
520 }
521 
__gen11_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)522 static int __gen11_reset_engines(struct intel_gt *gt,
523 				 intel_engine_mask_t engine_mask,
524 				 unsigned int retry)
525 {
526 	struct intel_engine_cs *engine;
527 	intel_engine_mask_t tmp;
528 	u32 reset_mask, unlock_mask = 0;
529 	int ret;
530 
531 	if (engine_mask == ALL_ENGINES) {
532 		reset_mask = GEN11_GRDOM_FULL;
533 	} else {
534 		reset_mask = 0;
535 		for_each_engine_masked(engine, gt, engine_mask, tmp) {
536 			reset_mask |= engine->reset_domain;
537 			ret = gen11_lock_sfc(engine, &reset_mask, &unlock_mask);
538 			if (ret)
539 				goto sfc_unlock;
540 		}
541 	}
542 
543 	ret = gen6_hw_domain_reset(gt, reset_mask);
544 
545 sfc_unlock:
546 	/*
547 	 * We unlock the SFC based on the lock status and not the result of
548 	 * gen11_lock_sfc to make sure that we clean properly if something
549 	 * wrong happened during the lock (e.g. lock acquired after timeout
550 	 * expiration).
551 	 *
552 	 * Due to Wa_14010733141, we may have locked an SFC to an engine that
553 	 * wasn't being reset.  So instead of calling gen11_unlock_sfc()
554 	 * on engine_mask, we instead call it on the mask of engines that our
555 	 * gen11_lock_sfc() calls told us actually had locks attempted.
556 	 */
557 	for_each_engine_masked(engine, gt, unlock_mask, tmp)
558 		gen11_unlock_sfc(engine);
559 
560 	return ret;
561 }
562 
gen8_engine_reset_prepare(struct intel_engine_cs * engine)563 static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
564 {
565 	struct intel_uncore *uncore = engine->uncore;
566 	const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
567 	u32 request, mask, ack;
568 	int ret;
569 
570 	if (I915_SELFTEST_ONLY(should_fail(&engine->reset_timeout, 1)))
571 		return -ETIMEDOUT;
572 
573 	ack = intel_uncore_read_fw(uncore, reg);
574 	if (ack & RESET_CTL_CAT_ERROR) {
575 		/*
576 		 * For catastrophic errors, ready-for-reset sequence
577 		 * needs to be bypassed: HAS#396813
578 		 */
579 		request = RESET_CTL_CAT_ERROR;
580 		mask = RESET_CTL_CAT_ERROR;
581 
582 		/* Catastrophic errors need to be cleared by HW */
583 		ack = 0;
584 	} else if (!(ack & RESET_CTL_READY_TO_RESET)) {
585 		request = RESET_CTL_REQUEST_RESET;
586 		mask = RESET_CTL_READY_TO_RESET;
587 		ack = RESET_CTL_READY_TO_RESET;
588 	} else {
589 		return 0;
590 	}
591 
592 	intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
593 	ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
594 					   700, 0, NULL);
595 	if (ret)
596 		gt_err(engine->gt,
597 		       "%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
598 		       engine->name, request,
599 		       intel_uncore_read_fw(uncore, reg));
600 
601 	return ret;
602 }
603 
gen8_engine_reset_cancel(struct intel_engine_cs * engine)604 static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
605 {
606 	intel_uncore_write_fw(engine->uncore,
607 			      RING_RESET_CTL(engine->mmio_base),
608 			      _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
609 }
610 
gen8_reset_engines(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned int retry)611 static int gen8_reset_engines(struct intel_gt *gt,
612 			      intel_engine_mask_t engine_mask,
613 			      unsigned int retry)
614 {
615 	struct intel_engine_cs *engine;
616 	const bool reset_non_ready = retry >= 1;
617 	intel_engine_mask_t tmp;
618 	unsigned long flags;
619 	int ret;
620 
621 	spin_lock_irqsave(&gt->uncore->lock, flags);
622 
623 	for_each_engine_masked(engine, gt, engine_mask, tmp) {
624 		ret = gen8_engine_reset_prepare(engine);
625 		if (ret && !reset_non_ready)
626 			goto skip_reset;
627 
628 		/*
629 		 * If this is not the first failed attempt to prepare,
630 		 * we decide to proceed anyway.
631 		 *
632 		 * By doing so we risk context corruption and with
633 		 * some gens (kbl), possible system hang if reset
634 		 * happens during active bb execution.
635 		 *
636 		 * We rather take context corruption instead of
637 		 * failed reset with a wedged driver/gpu. And
638 		 * active bb execution case should be covered by
639 		 * stop_engines() we have before the reset.
640 		 */
641 	}
642 
643 	/*
644 	 * Wa_22011100796:dg2, whenever Full soft reset is required,
645 	 * reset all individual engines firstly, and then do a full soft reset.
646 	 *
647 	 * This is best effort, so ignore any error from the initial reset.
648 	 */
649 	if (IS_DG2(gt->i915) && engine_mask == ALL_ENGINES)
650 		__gen11_reset_engines(gt, gt->info.engine_mask, 0);
651 
652 	if (GRAPHICS_VER(gt->i915) >= 11)
653 		ret = __gen11_reset_engines(gt, engine_mask, retry);
654 	else
655 		ret = __gen6_reset_engines(gt, engine_mask, retry);
656 
657 skip_reset:
658 	for_each_engine_masked(engine, gt, engine_mask, tmp)
659 		gen8_engine_reset_cancel(engine);
660 
661 	spin_unlock_irqrestore(&gt->uncore->lock, flags);
662 
663 	return ret;
664 }
665 
mock_reset(struct intel_gt * gt,intel_engine_mask_t mask,unsigned int retry)666 static int mock_reset(struct intel_gt *gt,
667 		      intel_engine_mask_t mask,
668 		      unsigned int retry)
669 {
670 	return 0;
671 }
672 
673 typedef int (*reset_func)(struct intel_gt *,
674 			  intel_engine_mask_t engine_mask,
675 			  unsigned int retry);
676 
intel_get_gpu_reset(const struct intel_gt * gt)677 static reset_func intel_get_gpu_reset(const struct intel_gt *gt)
678 {
679 	struct drm_i915_private *i915 = gt->i915;
680 
681 	if (is_mock_gt(gt))
682 		return mock_reset;
683 	else if (GRAPHICS_VER(i915) >= 8)
684 		return gen8_reset_engines;
685 	else if (GRAPHICS_VER(i915) >= 6)
686 		return gen6_reset_engines;
687 	else if (GRAPHICS_VER(i915) >= 5)
688 		return ilk_do_reset;
689 	else if (IS_G4X(i915))
690 		return g4x_do_reset;
691 	else if (IS_G33(i915) || IS_PINEVIEW(i915))
692 		return g33_do_reset;
693 	else if (GRAPHICS_VER(i915) >= 3)
694 		return i915_do_reset;
695 	else
696 		return NULL;
697 }
698 
__reset_guc(struct intel_gt * gt)699 static int __reset_guc(struct intel_gt *gt)
700 {
701 	u32 guc_domain =
702 		GRAPHICS_VER(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
703 
704 	return gen6_hw_domain_reset(gt, guc_domain);
705 }
706 
needs_wa_14015076503(struct intel_gt * gt,intel_engine_mask_t engine_mask)707 static bool needs_wa_14015076503(struct intel_gt *gt, intel_engine_mask_t engine_mask)
708 {
709 	if (MEDIA_VER_FULL(gt->i915) != IP_VER(13, 0) || !HAS_ENGINE(gt, GSC0))
710 		return false;
711 
712 	if (!__HAS_ENGINE(engine_mask, GSC0))
713 		return false;
714 
715 	return intel_gsc_uc_fw_init_done(&gt->uc.gsc);
716 }
717 
718 static intel_engine_mask_t
wa_14015076503_start(struct intel_gt * gt,intel_engine_mask_t engine_mask,bool first)719 wa_14015076503_start(struct intel_gt *gt, intel_engine_mask_t engine_mask, bool first)
720 {
721 	if (!needs_wa_14015076503(gt, engine_mask))
722 		return engine_mask;
723 
724 	/*
725 	 * wa_14015076503: if the GSC FW is loaded, we need to alert it that
726 	 * we're going to do a GSC engine reset and then wait for 200ms for the
727 	 * FW to get ready for it. However, if this is the first ALL_ENGINES
728 	 * reset attempt and the GSC is not busy, we can try to instead reset
729 	 * the GuC and all the other engines individually to avoid the 200ms
730 	 * wait.
731 	 * Skipping the GSC engine is safe because, differently from other
732 	 * engines, the GSCCS only role is to forward the commands to the GSC
733 	 * FW, so it doesn't have any HW outside of the CS itself and therefore
734 	 * it has no state that we don't explicitly re-init on resume or on
735 	 * context switch LRC or power context). The HW for the GSC uC is
736 	 * managed by the GSC FW so we don't need to care about that.
737 	 */
738 	if (engine_mask == ALL_ENGINES && first && intel_engine_is_idle(gt->engine[GSC0])) {
739 		__reset_guc(gt);
740 		engine_mask = gt->info.engine_mask & ~BIT(GSC0);
741 	} else {
742 		intel_uncore_rmw(gt->uncore,
743 				 HECI_H_GS1(MTL_GSC_HECI2_BASE),
744 				 0, HECI_H_GS1_ER_PREP);
745 
746 		/* make sure the reset bit is clear when writing the CSR reg */
747 		intel_uncore_rmw(gt->uncore,
748 				 HECI_H_CSR(MTL_GSC_HECI2_BASE),
749 				 HECI_H_CSR_RST, HECI_H_CSR_IG);
750 		drm_msleep(200);
751 	}
752 
753 	return engine_mask;
754 }
755 
756 static void
wa_14015076503_end(struct intel_gt * gt,intel_engine_mask_t engine_mask)757 wa_14015076503_end(struct intel_gt *gt, intel_engine_mask_t engine_mask)
758 {
759 	if (!needs_wa_14015076503(gt, engine_mask))
760 		return;
761 
762 	intel_uncore_rmw(gt->uncore,
763 			 HECI_H_GS1(MTL_GSC_HECI2_BASE),
764 			 HECI_H_GS1_ER_PREP, 0);
765 }
766 
__intel_gt_reset(struct intel_gt * gt,intel_engine_mask_t engine_mask)767 static int __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask)
768 {
769 	const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
770 	reset_func reset;
771 	int ret = -ETIMEDOUT;
772 	int retry;
773 
774 	reset = intel_get_gpu_reset(gt);
775 	if (!reset)
776 		return -ENODEV;
777 
778 	/*
779 	 * If the power well sleeps during the reset, the reset
780 	 * request may be dropped and never completes (causing -EIO).
781 	 */
782 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
783 	for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
784 		intel_engine_mask_t reset_mask;
785 
786 		reset_mask = wa_14015076503_start(gt, engine_mask, !retry);
787 
788 		GT_TRACE(gt, "engine_mask=%x\n", reset_mask);
789 		ret = reset(gt, reset_mask, retry);
790 
791 		wa_14015076503_end(gt, reset_mask);
792 	}
793 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
794 
795 	return ret;
796 }
797 
intel_has_gpu_reset(const struct intel_gt * gt)798 bool intel_has_gpu_reset(const struct intel_gt *gt)
799 {
800 	if (!gt->i915->params.reset)
801 		return NULL;
802 
803 	return intel_get_gpu_reset(gt);
804 }
805 
intel_has_reset_engine(const struct intel_gt * gt)806 bool intel_has_reset_engine(const struct intel_gt *gt)
807 {
808 	if (gt->i915->params.reset < 2)
809 		return false;
810 
811 	return INTEL_INFO(gt->i915)->has_reset_engine;
812 }
813 
intel_reset_guc(struct intel_gt * gt)814 int intel_reset_guc(struct intel_gt *gt)
815 {
816 	int ret;
817 
818 	GEM_BUG_ON(!HAS_GT_UC(gt->i915));
819 
820 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
821 	ret = __reset_guc(gt);
822 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
823 
824 	return ret;
825 }
826 
827 /*
828  * Ensure irq handler finishes, and not run again.
829  * Also return the active request so that we only search for it once.
830  */
reset_prepare_engine(struct intel_engine_cs * engine)831 static void reset_prepare_engine(struct intel_engine_cs *engine)
832 {
833 	/*
834 	 * During the reset sequence, we must prevent the engine from
835 	 * entering RC6. As the context state is undefined until we restart
836 	 * the engine, if it does enter RC6 during the reset, the state
837 	 * written to the powercontext is undefined and so we may lose
838 	 * GPU state upon resume, i.e. fail to restart after a reset.
839 	 */
840 	intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
841 	if (engine->reset.prepare)
842 		engine->reset.prepare(engine);
843 }
844 
revoke_mmaps(struct intel_gt * gt)845 static void revoke_mmaps(struct intel_gt *gt)
846 {
847 	int i;
848 
849 	for (i = 0; i < gt->ggtt->num_fences; i++) {
850 		struct drm_vma_offset_node *node;
851 		struct i915_vma *vma;
852 		u64 vma_offset;
853 
854 		vma = READ_ONCE(gt->ggtt->fence_regs[i].vma);
855 		if (!vma)
856 			continue;
857 
858 		if (!i915_vma_has_userfault(vma))
859 			continue;
860 
861 		GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
862 
863 		if (!vma->mmo)
864 			continue;
865 
866 		node = &vma->mmo->vma_node;
867 		vma_offset = vma->gtt_view.partial.offset << PAGE_SHIFT;
868 
869 #ifdef __linux__
870 		unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
871 				    drm_vma_node_offset_addr(node) + vma_offset,
872 				    vma->size,
873 				    1);
874 #else
875 {
876 		struct drm_i915_private *dev_priv = vma->obj->base.dev->dev_private;
877 		struct vm_page *pg;
878 
879 		for (pg = &dev_priv->pgs[atop(vma->node.start)];
880 		     pg != &dev_priv->pgs[atop(vma->node.start + vma->size)];
881 		     pg++)
882 			pmap_page_protect(pg, PROT_NONE);
883 }
884 #endif
885 	}
886 }
887 
reset_prepare(struct intel_gt * gt)888 static intel_engine_mask_t reset_prepare(struct intel_gt *gt)
889 {
890 	struct intel_engine_cs *engine;
891 	intel_engine_mask_t awake = 0;
892 	enum intel_engine_id id;
893 
894 	/**
895 	 * For GuC mode with submission enabled, ensure submission
896 	 * is disabled before stopping ring.
897 	 *
898 	 * For GuC mode with submission disabled, ensure that GuC is not
899 	 * sanitized, do that after engine reset. reset_prepare()
900 	 * is followed by engine reset which in this mode requires GuC to
901 	 * process any CSB FIFO entries generated by the resets.
902 	 */
903 	if (intel_uc_uses_guc_submission(&gt->uc))
904 		intel_uc_reset_prepare(&gt->uc);
905 
906 	for_each_engine(engine, gt, id) {
907 		if (intel_engine_pm_get_if_awake(engine))
908 			awake |= engine->mask;
909 		reset_prepare_engine(engine);
910 	}
911 
912 	return awake;
913 }
914 
gt_revoke(struct intel_gt * gt)915 static void gt_revoke(struct intel_gt *gt)
916 {
917 	revoke_mmaps(gt);
918 }
919 
gt_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask)920 static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
921 {
922 	struct intel_engine_cs *engine;
923 	enum intel_engine_id id;
924 	int err;
925 
926 	/*
927 	 * Everything depends on having the GTT running, so we need to start
928 	 * there.
929 	 */
930 	err = i915_ggtt_enable_hw(gt->i915);
931 	if (err)
932 		return err;
933 
934 	local_bh_disable();
935 	for_each_engine(engine, gt, id)
936 		__intel_engine_reset(engine, stalled_mask & engine->mask);
937 	local_bh_enable();
938 
939 	intel_uc_reset(&gt->uc, ALL_ENGINES);
940 
941 	intel_ggtt_restore_fences(gt->ggtt);
942 
943 	return err;
944 }
945 
reset_finish_engine(struct intel_engine_cs * engine)946 static void reset_finish_engine(struct intel_engine_cs *engine)
947 {
948 	if (engine->reset.finish)
949 		engine->reset.finish(engine);
950 	intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
951 
952 	intel_engine_signal_breadcrumbs(engine);
953 }
954 
reset_finish(struct intel_gt * gt,intel_engine_mask_t awake)955 static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
956 {
957 	struct intel_engine_cs *engine;
958 	enum intel_engine_id id;
959 
960 	for_each_engine(engine, gt, id) {
961 		reset_finish_engine(engine);
962 		if (awake & engine->mask)
963 			intel_engine_pm_put(engine);
964 	}
965 
966 	intel_uc_reset_finish(&gt->uc);
967 }
968 
nop_submit_request(struct i915_request * request)969 static void nop_submit_request(struct i915_request *request)
970 {
971 	RQ_TRACE(request, "-EIO\n");
972 
973 	request = i915_request_mark_eio(request);
974 	if (request) {
975 		i915_request_submit(request);
976 		intel_engine_signal_breadcrumbs(request->engine);
977 
978 		i915_request_put(request);
979 	}
980 }
981 
__intel_gt_set_wedged(struct intel_gt * gt)982 static void __intel_gt_set_wedged(struct intel_gt *gt)
983 {
984 	struct intel_engine_cs *engine;
985 	intel_engine_mask_t awake;
986 	enum intel_engine_id id;
987 
988 	if (test_bit(I915_WEDGED, &gt->reset.flags))
989 		return;
990 
991 	GT_TRACE(gt, "start\n");
992 
993 	/*
994 	 * First, stop submission to hw, but do not yet complete requests by
995 	 * rolling the global seqno forward (since this would complete requests
996 	 * for which we haven't set the fence error to EIO yet).
997 	 */
998 	awake = reset_prepare(gt);
999 
1000 	/* Even if the GPU reset fails, it should still stop the engines */
1001 	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1002 		intel_gt_reset_all_engines(gt);
1003 
1004 	for_each_engine(engine, gt, id)
1005 		engine->submit_request = nop_submit_request;
1006 
1007 	/*
1008 	 * Make sure no request can slip through without getting completed by
1009 	 * either this call here to intel_engine_write_global_seqno, or the one
1010 	 * in nop_submit_request.
1011 	 */
1012 	synchronize_rcu_expedited();
1013 	set_bit(I915_WEDGED, &gt->reset.flags);
1014 
1015 	/* Mark all executing requests as skipped */
1016 	local_bh_disable();
1017 	for_each_engine(engine, gt, id)
1018 		if (engine->reset.cancel)
1019 			engine->reset.cancel(engine);
1020 	intel_uc_cancel_requests(&gt->uc);
1021 	local_bh_enable();
1022 
1023 	reset_finish(gt, awake);
1024 
1025 	GT_TRACE(gt, "end\n");
1026 }
1027 
set_wedged_work(struct work_struct * w)1028 static void set_wedged_work(struct work_struct *w)
1029 {
1030 	struct intel_gt *gt = container_of(w, struct intel_gt, wedge);
1031 	intel_wakeref_t wf;
1032 
1033 	with_intel_runtime_pm(gt->uncore->rpm, wf)
1034 		__intel_gt_set_wedged(gt);
1035 }
1036 
intel_gt_set_wedged(struct intel_gt * gt)1037 void intel_gt_set_wedged(struct intel_gt *gt)
1038 {
1039 	intel_wakeref_t wakeref;
1040 
1041 	if (test_bit(I915_WEDGED, &gt->reset.flags))
1042 		return;
1043 
1044 	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1045 	mutex_lock(&gt->reset.mutex);
1046 
1047 	if (GEM_SHOW_DEBUG()) {
1048 		struct drm_printer p = drm_dbg_printer(&gt->i915->drm,
1049 						       DRM_UT_DRIVER, NULL);
1050 		struct intel_engine_cs *engine;
1051 		enum intel_engine_id id;
1052 
1053 		drm_printf(&p, "called from %pS\n", (void *)_RET_IP_);
1054 		for_each_engine(engine, gt, id) {
1055 			if (intel_engine_is_idle(engine))
1056 				continue;
1057 
1058 			intel_engine_dump(engine, &p, "%s\n", engine->name);
1059 		}
1060 	}
1061 
1062 	__intel_gt_set_wedged(gt);
1063 
1064 	mutex_unlock(&gt->reset.mutex);
1065 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1066 }
1067 
__intel_gt_unset_wedged(struct intel_gt * gt)1068 static bool __intel_gt_unset_wedged(struct intel_gt *gt)
1069 {
1070 	struct intel_gt_timelines *timelines = &gt->timelines;
1071 	struct intel_timeline *tl;
1072 	bool ok;
1073 
1074 	if (!test_bit(I915_WEDGED, &gt->reset.flags))
1075 		return true;
1076 
1077 	/* Never fully initialised, recovery impossible */
1078 	if (intel_gt_has_unrecoverable_error(gt))
1079 		return false;
1080 
1081 	GT_TRACE(gt, "start\n");
1082 
1083 	/*
1084 	 * Before unwedging, make sure that all pending operations
1085 	 * are flushed and errored out - we may have requests waiting upon
1086 	 * third party fences. We marked all inflight requests as EIO, and
1087 	 * every execbuf since returned EIO, for consistency we want all
1088 	 * the currently pending requests to also be marked as EIO, which
1089 	 * is done inside our nop_submit_request - and so we must wait.
1090 	 *
1091 	 * No more can be submitted until we reset the wedged bit.
1092 	 */
1093 	spin_lock(&timelines->lock);
1094 	list_for_each_entry(tl, &timelines->active_list, link) {
1095 		struct dma_fence *fence;
1096 
1097 		fence = i915_active_fence_get(&tl->last_request);
1098 		if (!fence)
1099 			continue;
1100 
1101 		spin_unlock(&timelines->lock);
1102 
1103 		/*
1104 		 * All internal dependencies (i915_requests) will have
1105 		 * been flushed by the set-wedge, but we may be stuck waiting
1106 		 * for external fences. These should all be capped to 10s
1107 		 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded
1108 		 * in the worst case.
1109 		 */
1110 		dma_fence_default_wait(fence, false, MAX_SCHEDULE_TIMEOUT);
1111 		dma_fence_put(fence);
1112 
1113 		/* Restart iteration after droping lock */
1114 		spin_lock(&timelines->lock);
1115 		tl = list_entry(&timelines->active_list, typeof(*tl), link);
1116 	}
1117 	spin_unlock(&timelines->lock);
1118 
1119 	/* We must reset pending GPU events before restoring our submission */
1120 	ok = !HAS_EXECLISTS(gt->i915); /* XXX better agnosticism desired */
1121 	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1122 		ok = intel_gt_reset_all_engines(gt) == 0;
1123 	if (!ok) {
1124 		/*
1125 		 * Warn CI about the unrecoverable wedged condition.
1126 		 * Time for a reboot.
1127 		 */
1128 		add_taint_for_CI(gt->i915, TAINT_WARN);
1129 		return false;
1130 	}
1131 
1132 	/*
1133 	 * Undo nop_submit_request. We prevent all new i915 requests from
1134 	 * being queued (by disallowing execbuf whilst wedged) so having
1135 	 * waited for all active requests above, we know the system is idle
1136 	 * and do not have to worry about a thread being inside
1137 	 * engine->submit_request() as we swap over. So unlike installing
1138 	 * the nop_submit_request on reset, we can do this from normal
1139 	 * context and do not require stop_machine().
1140 	 */
1141 	intel_engines_reset_default_submission(gt);
1142 
1143 	GT_TRACE(gt, "end\n");
1144 
1145 	smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
1146 	clear_bit(I915_WEDGED, &gt->reset.flags);
1147 
1148 	return true;
1149 }
1150 
intel_gt_unset_wedged(struct intel_gt * gt)1151 bool intel_gt_unset_wedged(struct intel_gt *gt)
1152 {
1153 	bool result;
1154 
1155 	mutex_lock(&gt->reset.mutex);
1156 	result = __intel_gt_unset_wedged(gt);
1157 	mutex_unlock(&gt->reset.mutex);
1158 
1159 	return result;
1160 }
1161 
do_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask)1162 static int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
1163 {
1164 	int err, i;
1165 
1166 	err = intel_gt_reset_all_engines(gt);
1167 	for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
1168 		drm_msleep(10 * (i + 1));
1169 		err = intel_gt_reset_all_engines(gt);
1170 	}
1171 	if (err)
1172 		return err;
1173 
1174 	return gt_reset(gt, stalled_mask);
1175 }
1176 
resume(struct intel_gt * gt)1177 static int resume(struct intel_gt *gt)
1178 {
1179 	struct intel_engine_cs *engine;
1180 	enum intel_engine_id id;
1181 	int ret;
1182 
1183 	for_each_engine(engine, gt, id) {
1184 		ret = intel_engine_resume(engine);
1185 		if (ret)
1186 			return ret;
1187 	}
1188 
1189 	return 0;
1190 }
1191 
1192 /**
1193  * intel_gt_reset - reset chip after a hang
1194  * @gt: #intel_gt to reset
1195  * @stalled_mask: mask of the stalled engines with the guilty requests
1196  * @reason: user error message for why we are resetting
1197  *
1198  * Reset the chip.  Useful if a hang is detected. Marks the device as wedged
1199  * on failure.
1200  *
1201  * Procedure is fairly simple:
1202  *   - reset the chip using the reset reg
1203  *   - re-init context state
1204  *   - re-init hardware status page
1205  *   - re-init ring buffer
1206  *   - re-init interrupt state
1207  *   - re-init display
1208  */
intel_gt_reset(struct intel_gt * gt,intel_engine_mask_t stalled_mask,const char * reason)1209 void intel_gt_reset(struct intel_gt *gt,
1210 		    intel_engine_mask_t stalled_mask,
1211 		    const char *reason)
1212 {
1213 	intel_engine_mask_t awake;
1214 	int ret;
1215 
1216 	GT_TRACE(gt, "flags=%lx\n", gt->reset.flags);
1217 
1218 	might_sleep();
1219 	GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
1220 
1221 	/*
1222 	 * FIXME: Revoking cpu mmap ptes cannot be done from a dma_fence
1223 	 * critical section like gpu reset.
1224 	 */
1225 	gt_revoke(gt);
1226 
1227 	mutex_lock(&gt->reset.mutex);
1228 
1229 	/* Clear any previous failed attempts at recovery. Time to try again. */
1230 	if (!__intel_gt_unset_wedged(gt))
1231 		goto unlock;
1232 
1233 	if (reason)
1234 		gt_notice(gt, "Resetting chip for %s\n", reason);
1235 	atomic_inc(&gt->i915->gpu_error.reset_count);
1236 
1237 	awake = reset_prepare(gt);
1238 
1239 	if (!intel_has_gpu_reset(gt)) {
1240 		if (gt->i915->params.reset)
1241 			gt_err(gt, "GPU reset not supported\n");
1242 		else
1243 			gt_dbg(gt, "GPU reset disabled\n");
1244 		goto error;
1245 	}
1246 
1247 	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1248 		intel_runtime_pm_disable_interrupts(gt->i915);
1249 
1250 	if (do_reset(gt, stalled_mask)) {
1251 		gt_err(gt, "Failed to reset chip\n");
1252 		goto taint;
1253 	}
1254 
1255 	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
1256 		intel_runtime_pm_enable_interrupts(gt->i915);
1257 
1258 	intel_overlay_reset(gt->i915);
1259 
1260 	/* sanitize uC after engine reset */
1261 	if (!intel_uc_uses_guc_submission(&gt->uc))
1262 		intel_uc_reset_prepare(&gt->uc);
1263 	/*
1264 	 * Next we need to restore the context, but we don't use those
1265 	 * yet either...
1266 	 *
1267 	 * Ring buffer needs to be re-initialized in the KMS case, or if X
1268 	 * was running at the time of the reset (i.e. we weren't VT
1269 	 * switched away).
1270 	 */
1271 	ret = intel_gt_init_hw(gt);
1272 	if (ret) {
1273 		gt_err(gt, "Failed to initialise HW following reset (%d)\n", ret);
1274 		goto taint;
1275 	}
1276 
1277 	ret = resume(gt);
1278 	if (ret)
1279 		goto taint;
1280 
1281 finish:
1282 	reset_finish(gt, awake);
1283 unlock:
1284 	mutex_unlock(&gt->reset.mutex);
1285 	return;
1286 
1287 taint:
1288 	/*
1289 	 * History tells us that if we cannot reset the GPU now, we
1290 	 * never will. This then impacts everything that is run
1291 	 * subsequently. On failing the reset, we mark the driver
1292 	 * as wedged, preventing further execution on the GPU.
1293 	 * We also want to go one step further and add a taint to the
1294 	 * kernel so that any subsequent faults can be traced back to
1295 	 * this failure. This is important for CI, where if the
1296 	 * GPU/driver fails we would like to reboot and restart testing
1297 	 * rather than continue on into oblivion. For everyone else,
1298 	 * the system should still plod along, but they have been warned!
1299 	 */
1300 	add_taint_for_CI(gt->i915, TAINT_WARN);
1301 error:
1302 	__intel_gt_set_wedged(gt);
1303 	goto finish;
1304 }
1305 
1306 /**
1307  * intel_gt_reset_all_engines() - Reset all engines in the given gt.
1308  * @gt: the GT to reset all engines for.
1309  *
1310  * This function resets all engines within the given gt.
1311  *
1312  * Returns:
1313  * Zero on success, negative error code on failure.
1314  */
intel_gt_reset_all_engines(struct intel_gt * gt)1315 int intel_gt_reset_all_engines(struct intel_gt *gt)
1316 {
1317 	return __intel_gt_reset(gt, ALL_ENGINES);
1318 }
1319 
1320 /**
1321  * intel_gt_reset_engine() - Reset a specific engine within a gt.
1322  * @engine: engine to be reset.
1323  *
1324  * This function resets the specified engine within a gt.
1325  *
1326  * Returns:
1327  * Zero on success, negative error code on failure.
1328  */
intel_gt_reset_engine(struct intel_engine_cs * engine)1329 int intel_gt_reset_engine(struct intel_engine_cs *engine)
1330 {
1331 	return __intel_gt_reset(engine->gt, engine->mask);
1332 }
1333 
__intel_engine_reset_bh(struct intel_engine_cs * engine,const char * msg)1334 int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
1335 {
1336 	struct intel_gt *gt = engine->gt;
1337 	int ret;
1338 
1339 	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
1340 	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
1341 
1342 	if (intel_engine_uses_guc(engine))
1343 		return -ENODEV;
1344 
1345 	if (!intel_engine_pm_get_if_awake(engine))
1346 		return 0;
1347 
1348 	reset_prepare_engine(engine);
1349 
1350 	if (msg)
1351 		drm_notice(&engine->i915->drm,
1352 			   "Resetting %s for %s\n", engine->name, msg);
1353 	i915_increase_reset_engine_count(&engine->i915->gpu_error, engine);
1354 
1355 	ret = intel_gt_reset_engine(engine);
1356 	if (ret) {
1357 		/* If we fail here, we expect to fallback to a global reset */
1358 		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
1359 		goto out;
1360 	}
1361 
1362 	/*
1363 	 * The request that caused the hang is stuck on elsp, we know the
1364 	 * active request and can drop it, adjust head to skip the offending
1365 	 * request to resume executing remaining requests in the queue.
1366 	 */
1367 	__intel_engine_reset(engine, true);
1368 
1369 	/*
1370 	 * The engine and its registers (and workarounds in case of render)
1371 	 * have been reset to their default values. Follow the init_ring
1372 	 * process to program RING_MODE, HWSP and re-enable submission.
1373 	 */
1374 	ret = intel_engine_resume(engine);
1375 
1376 out:
1377 	intel_engine_cancel_stop_cs(engine);
1378 	reset_finish_engine(engine);
1379 	intel_engine_pm_put_async(engine);
1380 	return ret;
1381 }
1382 
1383 /**
1384  * intel_engine_reset - reset GPU engine to recover from a hang
1385  * @engine: engine to reset
1386  * @msg: reason for GPU reset; or NULL for no drm_notice()
1387  *
1388  * Reset a specific GPU engine. Useful if a hang is detected.
1389  * Returns zero on successful reset or otherwise an error code.
1390  *
1391  * Procedure is:
1392  *  - identifies the request that caused the hang and it is dropped
1393  *  - reset engine (which will force the engine to idle)
1394  *  - re-init/configure engine
1395  */
intel_engine_reset(struct intel_engine_cs * engine,const char * msg)1396 int intel_engine_reset(struct intel_engine_cs *engine, const char *msg)
1397 {
1398 	int err;
1399 
1400 	local_bh_disable();
1401 	err = __intel_engine_reset_bh(engine, msg);
1402 	local_bh_enable();
1403 
1404 	return err;
1405 }
1406 
intel_gt_reset_global(struct intel_gt * gt,u32 engine_mask,const char * reason)1407 static void intel_gt_reset_global(struct intel_gt *gt,
1408 				  u32 engine_mask,
1409 				  const char *reason)
1410 {
1411 #ifdef notyet
1412 	struct kobject *kobj = &gt->i915->drm.primary->kdev->kobj;
1413 	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
1414 	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
1415 	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1416 #endif
1417 	struct intel_wedge_me w;
1418 
1419 	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
1420 
1421 	GT_TRACE(gt, "resetting chip, engines=%x\n", engine_mask);
1422 	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
1423 
1424 	/* Use a watchdog to ensure that our reset completes */
1425 	intel_wedge_on_timeout(&w, gt, 60 * HZ) {
1426 		intel_display_reset_prepare(gt->i915);
1427 
1428 		intel_gt_reset(gt, engine_mask, reason);
1429 
1430 		intel_display_reset_finish(gt->i915);
1431 	}
1432 
1433 	if (!test_bit(I915_WEDGED, &gt->reset.flags))
1434 		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
1435 }
1436 
1437 /**
1438  * intel_gt_handle_error - handle a gpu error
1439  * @gt: the intel_gt
1440  * @engine_mask: mask representing engines that are hung
1441  * @flags: control flags
1442  * @fmt: Error message format string
1443  *
1444  * Do some basic checking of register state at error time and
1445  * dump it to the syslog.  Also call i915_capture_error_state() to make
1446  * sure we get a record and make it available in debugfs.  Fire a uevent
1447  * so userspace knows something bad happened (should trigger collection
1448  * of a ring dump etc.).
1449  */
intel_gt_handle_error(struct intel_gt * gt,intel_engine_mask_t engine_mask,unsigned long flags,const char * fmt,...)1450 void intel_gt_handle_error(struct intel_gt *gt,
1451 			   intel_engine_mask_t engine_mask,
1452 			   unsigned long flags,
1453 			   const char *fmt, ...)
1454 {
1455 	struct intel_engine_cs *engine;
1456 	intel_wakeref_t wakeref;
1457 	intel_engine_mask_t tmp;
1458 	char error_msg[80];
1459 	char *msg = NULL;
1460 
1461 	if (fmt) {
1462 		va_list args;
1463 
1464 		va_start(args, fmt);
1465 		vscnprintf(error_msg, sizeof(error_msg), fmt, args);
1466 		va_end(args);
1467 
1468 		msg = error_msg;
1469 	}
1470 
1471 	/*
1472 	 * In most cases it's guaranteed that we get here with an RPM
1473 	 * reference held, for example because there is a pending GPU
1474 	 * request that won't finish until the reset is done. This
1475 	 * isn't the case at least when we get here by doing a
1476 	 * simulated reset via debugfs, so get an RPM reference.
1477 	 */
1478 	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1479 
1480 	engine_mask &= gt->info.engine_mask;
1481 
1482 	if (flags & I915_ERROR_CAPTURE) {
1483 		i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_NONE);
1484 		intel_gt_clear_error_registers(gt, engine_mask);
1485 	}
1486 
1487 	/*
1488 	 * Try engine reset when available. We fall back to full reset if
1489 	 * single reset fails.
1490 	 */
1491 	if (!intel_uc_uses_guc_submission(&gt->uc) &&
1492 	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
1493 		local_bh_disable();
1494 		for_each_engine_masked(engine, gt, engine_mask, tmp) {
1495 			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
1496 			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1497 					     &gt->reset.flags))
1498 				continue;
1499 
1500 			if (__intel_engine_reset_bh(engine, msg) == 0)
1501 				engine_mask &= ~engine->mask;
1502 
1503 			clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
1504 					      &gt->reset.flags);
1505 		}
1506 		local_bh_enable();
1507 	}
1508 
1509 	if (!engine_mask)
1510 		goto out;
1511 
1512 	/* Full reset needs the mutex, stop any other user trying to do so. */
1513 	if (test_and_set_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
1514 		wait_event(gt->reset.queue,
1515 			   !test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
1516 		goto out; /* piggy-back on the other reset */
1517 	}
1518 
1519 	/* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
1520 	synchronize_rcu_expedited();
1521 
1522 	/*
1523 	 * Prevent any other reset-engine attempt. We don't do this for GuC
1524 	 * submission the GuC owns the per-engine reset, not the i915.
1525 	 */
1526 	if (!intel_uc_uses_guc_submission(&gt->uc)) {
1527 		for_each_engine(engine, gt, tmp) {
1528 			while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1529 						&gt->reset.flags))
1530 				wait_on_bit(&gt->reset.flags,
1531 					    I915_RESET_ENGINE + engine->id,
1532 					    TASK_UNINTERRUPTIBLE);
1533 		}
1534 	}
1535 
1536 	/* Flush everyone using a resource about to be clobbered */
1537 	synchronize_srcu_expedited(&gt->reset.backoff_srcu);
1538 
1539 	intel_gt_reset_global(gt, engine_mask, msg);
1540 
1541 	if (!intel_uc_uses_guc_submission(&gt->uc)) {
1542 		for_each_engine(engine, gt, tmp)
1543 			clear_bit_unlock(I915_RESET_ENGINE + engine->id,
1544 					 &gt->reset.flags);
1545 	}
1546 	clear_bit_unlock(I915_RESET_BACKOFF, &gt->reset.flags);
1547 	smp_mb__after_atomic();
1548 	wake_up_all(&gt->reset.queue);
1549 
1550 out:
1551 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1552 }
1553 
_intel_gt_reset_lock(struct intel_gt * gt,int * srcu,bool retry)1554 static int _intel_gt_reset_lock(struct intel_gt *gt, int *srcu, bool retry)
1555 {
1556 	might_lock(&gt->reset.backoff_srcu);
1557 	if (retry)
1558 		might_sleep();
1559 
1560 	rcu_read_lock();
1561 	while (test_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
1562 		rcu_read_unlock();
1563 
1564 		if (!retry)
1565 			return -EBUSY;
1566 
1567 		if (wait_event_interruptible(gt->reset.queue,
1568 					     !test_bit(I915_RESET_BACKOFF,
1569 						       &gt->reset.flags)))
1570 			return -EINTR;
1571 
1572 		rcu_read_lock();
1573 	}
1574 	*srcu = srcu_read_lock(&gt->reset.backoff_srcu);
1575 	rcu_read_unlock();
1576 
1577 	return 0;
1578 }
1579 
intel_gt_reset_trylock(struct intel_gt * gt,int * srcu)1580 int intel_gt_reset_trylock(struct intel_gt *gt, int *srcu)
1581 {
1582 	return _intel_gt_reset_lock(gt, srcu, false);
1583 }
1584 
intel_gt_reset_lock_interruptible(struct intel_gt * gt,int * srcu)1585 int intel_gt_reset_lock_interruptible(struct intel_gt *gt, int *srcu)
1586 {
1587 	return _intel_gt_reset_lock(gt, srcu, true);
1588 }
1589 
intel_gt_reset_unlock(struct intel_gt * gt,int tag)1590 void intel_gt_reset_unlock(struct intel_gt *gt, int tag)
1591 __releases(&gt->reset.backoff_srcu)
1592 {
1593 	srcu_read_unlock(&gt->reset.backoff_srcu, tag);
1594 }
1595 
intel_gt_terminally_wedged(struct intel_gt * gt)1596 int intel_gt_terminally_wedged(struct intel_gt *gt)
1597 {
1598 	might_sleep();
1599 
1600 	if (!intel_gt_is_wedged(gt))
1601 		return 0;
1602 
1603 	if (intel_gt_has_unrecoverable_error(gt))
1604 		return -EIO;
1605 
1606 	/* Reset still in progress? Maybe we will recover? */
1607 	if (wait_event_interruptible(gt->reset.queue,
1608 				     !test_bit(I915_RESET_BACKOFF,
1609 					       &gt->reset.flags)))
1610 		return -EINTR;
1611 
1612 	return intel_gt_is_wedged(gt) ? -EIO : 0;
1613 }
1614 
intel_gt_set_wedged_on_init(struct intel_gt * gt)1615 void intel_gt_set_wedged_on_init(struct intel_gt *gt)
1616 {
1617 	BUILD_BUG_ON(I915_RESET_ENGINE + I915_NUM_ENGINES >
1618 		     I915_WEDGED_ON_INIT);
1619 	intel_gt_set_wedged(gt);
1620 	i915_disable_error_state(gt->i915, -ENODEV);
1621 	set_bit(I915_WEDGED_ON_INIT, &gt->reset.flags);
1622 
1623 	/* Wedged on init is non-recoverable */
1624 	add_taint_for_CI(gt->i915, TAINT_WARN);
1625 }
1626 
intel_gt_set_wedged_on_fini(struct intel_gt * gt)1627 void intel_gt_set_wedged_on_fini(struct intel_gt *gt)
1628 {
1629 	intel_gt_set_wedged(gt);
1630 	i915_disable_error_state(gt->i915, -ENODEV);
1631 	set_bit(I915_WEDGED_ON_FINI, &gt->reset.flags);
1632 	intel_gt_retire_requests(gt); /* cleanup any wedged requests */
1633 }
1634 
intel_gt_init_reset(struct intel_gt * gt)1635 void intel_gt_init_reset(struct intel_gt *gt)
1636 {
1637 	init_waitqueue_head(&gt->reset.queue);
1638 	rw_init(&gt->reset.mutex, "gtres");
1639 	init_srcu_struct(&gt->reset.backoff_srcu);
1640 	INIT_WORK(&gt->wedge, set_wedged_work);
1641 
1642 	/*
1643 	 * While undesirable to wait inside the shrinker, complain anyway.
1644 	 *
1645 	 * If we have to wait during shrinking, we guarantee forward progress
1646 	 * by forcing the reset. Therefore during the reset we must not
1647 	 * re-enter the shrinker. By declaring that we take the reset mutex
1648 	 * within the shrinker, we forbid ourselves from performing any
1649 	 * fs-reclaim or taking related locks during reset.
1650 	 */
1651 	i915_gem_shrinker_taints_mutex(gt->i915, &gt->reset.mutex);
1652 
1653 	/* no GPU until we are ready! */
1654 	__set_bit(I915_WEDGED, &gt->reset.flags);
1655 }
1656 
intel_gt_fini_reset(struct intel_gt * gt)1657 void intel_gt_fini_reset(struct intel_gt *gt)
1658 {
1659 	cleanup_srcu_struct(&gt->reset.backoff_srcu);
1660 }
1661 
intel_wedge_me(struct work_struct * work)1662 static void intel_wedge_me(struct work_struct *work)
1663 {
1664 	struct intel_wedge_me *w = container_of(work, typeof(*w), work.work);
1665 
1666 	gt_err(w->gt, "%s timed out, cancelling all in-flight rendering.\n", w->name);
1667 	set_wedged_work(&w->gt->wedge);
1668 }
1669 
__intel_init_wedge(struct intel_wedge_me * w,struct intel_gt * gt,long timeout,const char * name)1670 void __intel_init_wedge(struct intel_wedge_me *w,
1671 			struct intel_gt *gt,
1672 			long timeout,
1673 			const char *name)
1674 {
1675 	w->gt = gt;
1676 	w->name = name;
1677 
1678 	INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me);
1679 	queue_delayed_work(gt->i915->unordered_wq, &w->work, timeout);
1680 }
1681 
__intel_fini_wedge(struct intel_wedge_me * w)1682 void __intel_fini_wedge(struct intel_wedge_me *w)
1683 {
1684 	cancel_delayed_work_sync(&w->work);
1685 	destroy_delayed_work_on_stack(&w->work);
1686 	w->gt = NULL;
1687 }
1688 
1689 /*
1690  * Wa_22011802037 requires that we (or the GuC) ensure that no command
1691  * streamers are executing MI_FORCE_WAKE while an engine reset is initiated.
1692  */
intel_engine_reset_needs_wa_22011802037(struct intel_gt * gt)1693 bool intel_engine_reset_needs_wa_22011802037(struct intel_gt *gt)
1694 {
1695 	if (GRAPHICS_VER(gt->i915) < 11)
1696 		return false;
1697 
1698 	if (IS_GFX_GT_IP_STEP(gt, IP_VER(12, 70), STEP_A0, STEP_B0))
1699 		return true;
1700 
1701 	if (GRAPHICS_VER_FULL(gt->i915) >= IP_VER(12, 70))
1702 		return false;
1703 
1704 	return true;
1705 }
1706 
1707 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
1708 #include "selftest_reset.c"
1709 #include "selftest_hangcheck.c"
1710 #endif
1711