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