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