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