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