intel_reset.c 35.5 KB
Newer Older
1 2 3 4 5 6 7
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright © 2008-2018 Intel Corporation
 */

#include <linux/sched/mm.h>
8
#include <linux/stop_machine.h>
9

10 11
#include "display/intel_overlay.h"

12 13
#include "gem/i915_gem_context.h"

14 15
#include "i915_drv.h"
#include "i915_gpu_error.h"
16
#include "i915_irq.h"
17
#include "intel_engine_pm.h"
18
#include "intel_gt.h"
19
#include "intel_gt_pm.h"
20
#include "intel_reset.h"
21 22 23

#include "intel_guc.h"

24 25
#define RESET_MAX_RETRIES 3

26 27 28
/* XXX How to handle concurrent GGTT updates using tiling registers? */
#define RESET_UNDER_STOP_MACHINE 0

29 30 31 32 33 34 35 36 37 38
static void rmw_set_fw(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
{
	intel_uncore_rmw_fw(uncore, reg, 0, set);
}

static void rmw_clear_fw(struct intel_uncore *uncore, i915_reg_t reg, u32 clr)
{
	intel_uncore_rmw_fw(uncore, reg, clr, 0);
}

39 40 41 42 43
static void engine_skip_context(struct i915_request *rq)
{
	struct intel_engine_cs *engine = rq->engine;
	struct i915_gem_context *hung_ctx = rq->gem_context;

44
	lockdep_assert_held(&engine->active.lock);
45

46 47
	if (!i915_request_is_active(rq))
		return;
48

49
	list_for_each_entry_continue(rq, &engine->active.requests, sched.link)
50 51
		if (rq->gem_context == hung_ctx)
			i915_request_skip(rq, -EIO);
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
}

static void client_mark_guilty(struct drm_i915_file_private *file_priv,
			       const struct i915_gem_context *ctx)
{
	unsigned int score;
	unsigned long prev_hang;

	if (i915_gem_context_is_banned(ctx))
		score = I915_CLIENT_SCORE_CONTEXT_BAN;
	else
		score = 0;

	prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
	if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
		score += I915_CLIENT_SCORE_HANG_FAST;

	if (score) {
		atomic_add(score, &file_priv->ban_score);

		DRM_DEBUG_DRIVER("client %s: gained %u ban score, now %u\n",
				 ctx->name, score,
				 atomic_read(&file_priv->ban_score));
	}
}

78
static bool context_mark_guilty(struct i915_gem_context *ctx)
79
{
80 81 82
	unsigned long prev_hang;
	bool banned;
	int i;
83 84 85

	atomic_inc(&ctx->guilty_count);

86 87
	/* Cool contexts are too cool to be banned! (Used for reset testing.) */
	if (!i915_gem_context_is_bannable(ctx))
88
		return false;
89

90 91 92 93 94 95 96 97 98 99
	/* Record the timestamp for the last N hangs */
	prev_hang = ctx->hang_timestamp[0];
	for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++)
		ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1];
	ctx->hang_timestamp[i] = jiffies;

	/* If we have hung N+1 times in rapid succession, we ban the context! */
	banned = !i915_gem_context_is_recoverable(ctx);
	if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES))
		banned = true;
100
	if (banned) {
101 102
		DRM_DEBUG_DRIVER("context %s: guilty %d, banned\n",
				 ctx->name, atomic_read(&ctx->guilty_count));
103 104 105 106 107
		i915_gem_context_set_banned(ctx);
	}

	if (!IS_ERR_OR_NULL(ctx->file_priv))
		client_mark_guilty(ctx->file_priv, ctx);
108 109

	return banned;
110 111 112 113 114 115 116
}

static void context_mark_innocent(struct i915_gem_context *ctx)
{
	atomic_inc(&ctx->active_count);
}

117
void __i915_request_reset(struct i915_request *rq, bool guilty)
118
{
119 120 121 122 123 124
	GEM_TRACE("%s rq=%llx:%lld, guilty? %s\n",
		  rq->engine->name,
		  rq->fence.context,
		  rq->fence.seqno,
		  yesno(guilty));

125
	lockdep_assert_held(&rq->engine->active.lock);
126 127 128 129 130 131 132 133 134 135 136 137
	GEM_BUG_ON(i915_request_completed(rq));

	if (guilty) {
		i915_request_skip(rq, -EIO);
		if (context_mark_guilty(rq->gem_context))
			engine_skip_context(rq);
	} else {
		dma_fence_set_error(&rq->fence, -EAGAIN);
		context_mark_innocent(rq->gem_context);
	}
}

138 139
static void gen3_stop_engine(struct intel_engine_cs *engine)
{
140
	struct intel_uncore *uncore = engine->uncore;
141 142
	const u32 base = engine->mmio_base;

143 144
	GEM_TRACE("%s\n", engine->name);

145
	if (intel_engine_stop_cs(engine))
146
		GEM_TRACE("%s: timed out on STOP_RING\n", engine->name);
147

148 149 150 151
	intel_uncore_write_fw(uncore,
			      RING_HEAD(base),
			      intel_uncore_read_fw(uncore, RING_TAIL(base)));
	intel_uncore_posting_read_fw(uncore, RING_HEAD(base)); /* paranoia */
152

153 154 155
	intel_uncore_write_fw(uncore, RING_HEAD(base), 0);
	intel_uncore_write_fw(uncore, RING_TAIL(base), 0);
	intel_uncore_posting_read_fw(uncore, RING_TAIL(base));
156 157

	/* The ring must be empty before it is disabled */
158
	intel_uncore_write_fw(uncore, RING_CTL(base), 0);
159 160

	/* Check acts as a post */
161
	if (intel_uncore_read_fw(uncore, RING_HEAD(base)))
162
		GEM_TRACE("%s: ring head [%x] not parked\n",
163 164
			  engine->name,
			  intel_uncore_read_fw(uncore, RING_HEAD(base)));
165 166
}

167
static void stop_engines(struct intel_gt *gt, intel_engine_mask_t engine_mask)
168 169
{
	struct intel_engine_cs *engine;
170
	intel_engine_mask_t tmp;
171

172
	if (INTEL_GEN(gt->i915) < 3)
173 174
		return;

175
	for_each_engine_masked(engine, gt->i915, engine_mask, tmp)
176 177 178 179 180 181 182 183 184 185 186
		gen3_stop_engine(engine);
}

static bool i915_in_reset(struct pci_dev *pdev)
{
	u8 gdrst;

	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
	return gdrst & GRDOM_RESET_STATUS;
}

187
static int i915_do_reset(struct intel_gt *gt,
188
			 intel_engine_mask_t engine_mask,
189 190
			 unsigned int retry)
{
191
	struct pci_dev *pdev = gt->i915->drm.pdev;
192 193 194 195
	int err;

	/* Assert reset for at least 20 usec, and wait for acknowledgement. */
	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
196 197
	udelay(50);
	err = wait_for_atomic(i915_in_reset(pdev), 50);
198 199 200

	/* Clear the reset request. */
	pci_write_config_byte(pdev, I915_GDRST, 0);
201
	udelay(50);
202
	if (!err)
203
		err = wait_for_atomic(!i915_in_reset(pdev), 50);
204 205 206 207 208 209 210 211 212 213 214 215

	return err;
}

static bool g4x_reset_complete(struct pci_dev *pdev)
{
	u8 gdrst;

	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
	return (gdrst & GRDOM_RESET_ENABLE) == 0;
}

216
static int g33_do_reset(struct intel_gt *gt,
217
			intel_engine_mask_t engine_mask,
218 219
			unsigned int retry)
{
220
	struct pci_dev *pdev = gt->i915->drm.pdev;
221 222

	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
223
	return wait_for_atomic(g4x_reset_complete(pdev), 50);
224 225
}

226
static int g4x_do_reset(struct intel_gt *gt,
227
			intel_engine_mask_t engine_mask,
228 229
			unsigned int retry)
{
230 231
	struct pci_dev *pdev = gt->i915->drm.pdev;
	struct intel_uncore *uncore = gt->uncore;
232 233 234
	int ret;

	/* WaVcpClkGateDisableForMediaReset:ctg,elk */
235 236
	rmw_set_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
237 238 239

	pci_write_config_byte(pdev, I915_GDRST,
			      GRDOM_MEDIA | GRDOM_RESET_ENABLE);
240
	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
241 242 243 244 245 246 247
	if (ret) {
		DRM_DEBUG_DRIVER("Wait for media reset failed\n");
		goto out;
	}

	pci_write_config_byte(pdev, I915_GDRST,
			      GRDOM_RENDER | GRDOM_RESET_ENABLE);
248
	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
249 250 251 252 253 254 255 256
	if (ret) {
		DRM_DEBUG_DRIVER("Wait for render reset failed\n");
		goto out;
	}

out:
	pci_write_config_byte(pdev, I915_GDRST, 0);

257 258
	rmw_clear_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
259 260 261 262

	return ret;
}

263
static int ironlake_do_reset(struct intel_gt *gt,
264
			     intel_engine_mask_t engine_mask,
265 266
			     unsigned int retry)
{
267
	struct intel_uncore *uncore = gt->uncore;
268 269
	int ret;

270 271 272
	intel_uncore_write_fw(uncore, ILK_GDSR,
			      ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
273 274 275
					   ILK_GRDOM_RESET_ENABLE, 0,
					   5000, 0,
					   NULL);
276 277 278 279 280
	if (ret) {
		DRM_DEBUG_DRIVER("Wait for render reset failed\n");
		goto out;
	}

281 282 283
	intel_uncore_write_fw(uncore, ILK_GDSR,
			      ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
284 285 286
					   ILK_GRDOM_RESET_ENABLE, 0,
					   5000, 0,
					   NULL);
287 288 289 290 291 292
	if (ret) {
		DRM_DEBUG_DRIVER("Wait for media reset failed\n");
		goto out;
	}

out:
293 294
	intel_uncore_write_fw(uncore, ILK_GDSR, 0);
	intel_uncore_posting_read_fw(uncore, ILK_GDSR);
295 296 297 298
	return ret;
}

/* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
299
static int gen6_hw_domain_reset(struct intel_gt *gt, u32 hw_domain_mask)
300
{
301
	struct intel_uncore *uncore = gt->uncore;
302 303 304 305 306 307 308
	int err;

	/*
	 * GEN6_GDRST is not in the gt power well, no need to check
	 * for fifo space for the write or forcewake the chip for
	 * the read
	 */
309
	intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask);
310 311

	/* Wait for the device to ack the reset requests */
312
	err = __intel_wait_for_register_fw(uncore,
313 314 315 316 317 318 319 320 321 322
					   GEN6_GDRST, hw_domain_mask, 0,
					   500, 0,
					   NULL);
	if (err)
		DRM_DEBUG_DRIVER("Wait for 0x%08x engines reset failed\n",
				 hw_domain_mask);

	return err;
}

323
static int gen6_reset_engines(struct intel_gt *gt,
324
			      intel_engine_mask_t engine_mask,
325 326 327
			      unsigned int retry)
{
	struct intel_engine_cs *engine;
328 329 330 331 332 333
	const u32 hw_engine_mask[] = {
		[RCS0]  = GEN6_GRDOM_RENDER,
		[BCS0]  = GEN6_GRDOM_BLT,
		[VCS0]  = GEN6_GRDOM_MEDIA,
		[VCS1]  = GEN8_GRDOM_MEDIA2,
		[VECS0] = GEN6_GRDOM_VECS,
334 335 336 337 338 339
	};
	u32 hw_mask;

	if (engine_mask == ALL_ENGINES) {
		hw_mask = GEN6_GRDOM_FULL;
	} else {
340
		intel_engine_mask_t tmp;
341 342

		hw_mask = 0;
343
		for_each_engine_masked(engine, gt->i915, engine_mask, tmp) {
344
			GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
345
			hw_mask |= hw_engine_mask[engine->id];
346
		}
347 348
	}

349
	return gen6_hw_domain_reset(gt, hw_mask);
350 351
}

352
static u32 gen11_lock_sfc(struct intel_engine_cs *engine)
353
{
354 355
	struct intel_uncore *uncore = engine->uncore;
	u8 vdbox_sfc_access = RUNTIME_INFO(engine->i915)->vdbox_sfc_access;
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401
	i915_reg_t sfc_forced_lock, sfc_forced_lock_ack;
	u32 sfc_forced_lock_bit, sfc_forced_lock_ack_bit;
	i915_reg_t sfc_usage;
	u32 sfc_usage_bit;
	u32 sfc_reset_bit;

	switch (engine->class) {
	case VIDEO_DECODE_CLASS:
		if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
			return 0;

		sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
		sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;

		sfc_forced_lock_ack = GEN11_VCS_SFC_LOCK_STATUS(engine);
		sfc_forced_lock_ack_bit  = GEN11_VCS_SFC_LOCK_ACK_BIT;

		sfc_usage = GEN11_VCS_SFC_LOCK_STATUS(engine);
		sfc_usage_bit = GEN11_VCS_SFC_USAGE_BIT;
		sfc_reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
		break;

	case VIDEO_ENHANCEMENT_CLASS:
		sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
		sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;

		sfc_forced_lock_ack = GEN11_VECS_SFC_LOCK_ACK(engine);
		sfc_forced_lock_ack_bit  = GEN11_VECS_SFC_LOCK_ACK_BIT;

		sfc_usage = GEN11_VECS_SFC_USAGE(engine);
		sfc_usage_bit = GEN11_VECS_SFC_USAGE_BIT;
		sfc_reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
		break;

	default:
		return 0;
	}

	/*
	 * Tell the engine that a software reset is going to happen. The engine
	 * will then try to force lock the SFC (if currently locked, it will
	 * remain so until we tell the engine it is safe to unlock; if currently
	 * unlocked, it will ignore this and all new lock requests). If SFC
	 * ends up being locked to the engine we want to reset, we have to reset
	 * it as well (we will unlock it once the reset sequence is completed).
	 */
402
	rmw_set_fw(uncore, sfc_forced_lock, sfc_forced_lock_bit);
403

404
	if (__intel_wait_for_register_fw(uncore,
405 406 407 408 409 410 411 412
					 sfc_forced_lock_ack,
					 sfc_forced_lock_ack_bit,
					 sfc_forced_lock_ack_bit,
					 1000, 0, NULL)) {
		DRM_DEBUG_DRIVER("Wait for SFC forced lock ack failed\n");
		return 0;
	}

413
	if (intel_uncore_read_fw(uncore, sfc_usage) & sfc_usage_bit)
414 415 416 417 418
		return sfc_reset_bit;

	return 0;
}

419
static void gen11_unlock_sfc(struct intel_engine_cs *engine)
420
{
421 422
	struct intel_uncore *uncore = engine->uncore;
	u8 vdbox_sfc_access = RUNTIME_INFO(engine->i915)->vdbox_sfc_access;
423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443
	i915_reg_t sfc_forced_lock;
	u32 sfc_forced_lock_bit;

	switch (engine->class) {
	case VIDEO_DECODE_CLASS:
		if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
			return;

		sfc_forced_lock = GEN11_VCS_SFC_FORCED_LOCK(engine);
		sfc_forced_lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
		break;

	case VIDEO_ENHANCEMENT_CLASS:
		sfc_forced_lock = GEN11_VECS_SFC_FORCED_LOCK(engine);
		sfc_forced_lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
		break;

	default:
		return;
	}

444
	rmw_clear_fw(uncore, sfc_forced_lock, sfc_forced_lock_bit);
445 446
}

447
static int gen11_reset_engines(struct intel_gt *gt,
448
			       intel_engine_mask_t engine_mask,
449 450
			       unsigned int retry)
{
451 452 453 454 455 456 457 458 459
	const u32 hw_engine_mask[] = {
		[RCS0]  = GEN11_GRDOM_RENDER,
		[BCS0]  = GEN11_GRDOM_BLT,
		[VCS0]  = GEN11_GRDOM_MEDIA,
		[VCS1]  = GEN11_GRDOM_MEDIA2,
		[VCS2]  = GEN11_GRDOM_MEDIA3,
		[VCS3]  = GEN11_GRDOM_MEDIA4,
		[VECS0] = GEN11_GRDOM_VECS,
		[VECS1] = GEN11_GRDOM_VECS2,
460 461
	};
	struct intel_engine_cs *engine;
462
	intel_engine_mask_t tmp;
463 464 465 466 467 468 469
	u32 hw_mask;
	int ret;

	if (engine_mask == ALL_ENGINES) {
		hw_mask = GEN11_GRDOM_FULL;
	} else {
		hw_mask = 0;
470
		for_each_engine_masked(engine, gt->i915, engine_mask, tmp) {
471
			GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
472
			hw_mask |= hw_engine_mask[engine->id];
473
			hw_mask |= gen11_lock_sfc(engine);
474 475 476
		}
	}

477
	ret = gen6_hw_domain_reset(gt, hw_mask);
478 479

	if (engine_mask != ALL_ENGINES)
480
		for_each_engine_masked(engine, gt->i915, engine_mask, tmp)
481
			gen11_unlock_sfc(engine);
482 483 484 485 486 487

	return ret;
}

static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
{
488
	struct intel_uncore *uncore = engine->uncore;
489 490
	const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
	u32 request, mask, ack;
491 492
	int ret;

493
	ack = intel_uncore_read_fw(uncore, reg);
494 495 496 497 498 499 500 501 502 503 504
	if (ack & RESET_CTL_CAT_ERROR) {
		/*
		 * For catastrophic errors, ready-for-reset sequence
		 * needs to be bypassed: HAS#396813
		 */
		request = RESET_CTL_CAT_ERROR;
		mask = RESET_CTL_CAT_ERROR;

		/* Catastrophic errors need to be cleared by HW */
		ack = 0;
	} else if (!(ack & RESET_CTL_READY_TO_RESET)) {
505 506 507 508 509 510
		request = RESET_CTL_REQUEST_RESET;
		mask = RESET_CTL_READY_TO_RESET;
		ack = RESET_CTL_READY_TO_RESET;
	} else {
		return 0;
	}
511

512 513 514
	intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
	ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
					   700, 0, NULL);
515
	if (ret)
516 517 518
		DRM_ERROR("%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
			  engine->name, request,
			  intel_uncore_read_fw(uncore, reg));
519 520 521 522 523 524

	return ret;
}

static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
{
525 526 527
	intel_uncore_write_fw(engine->uncore,
			      RING_RESET_CTL(engine->mmio_base),
			      _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
528 529
}

530
static int gen8_reset_engines(struct intel_gt *gt,
531
			      intel_engine_mask_t engine_mask,
532 533 534 535
			      unsigned int retry)
{
	struct intel_engine_cs *engine;
	const bool reset_non_ready = retry >= 1;
536
	intel_engine_mask_t tmp;
537 538
	int ret;

539
	for_each_engine_masked(engine, gt->i915, engine_mask, tmp) {
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
		ret = gen8_engine_reset_prepare(engine);
		if (ret && !reset_non_ready)
			goto skip_reset;

		/*
		 * If this is not the first failed attempt to prepare,
		 * we decide to proceed anyway.
		 *
		 * By doing so we risk context corruption and with
		 * some gens (kbl), possible system hang if reset
		 * happens during active bb execution.
		 *
		 * We rather take context corruption instead of
		 * failed reset with a wedged driver/gpu. And
		 * active bb execution case should be covered by
555
		 * stop_engines() we have before the reset.
556 557 558
		 */
	}

559 560
	if (INTEL_GEN(gt->i915) >= 11)
		ret = gen11_reset_engines(gt, engine_mask, retry);
561
	else
562
		ret = gen6_reset_engines(gt, engine_mask, retry);
563 564

skip_reset:
565
	for_each_engine_masked(engine, gt->i915, engine_mask, tmp)
566 567 568 569 570
		gen8_engine_reset_cancel(engine);

	return ret;
}

571
typedef int (*reset_func)(struct intel_gt *,
572
			  intel_engine_mask_t engine_mask,
573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592
			  unsigned int retry);

static reset_func intel_get_gpu_reset(struct drm_i915_private *i915)
{
	if (INTEL_GEN(i915) >= 8)
		return gen8_reset_engines;
	else if (INTEL_GEN(i915) >= 6)
		return gen6_reset_engines;
	else if (INTEL_GEN(i915) >= 5)
		return ironlake_do_reset;
	else if (IS_G4X(i915))
		return g4x_do_reset;
	else if (IS_G33(i915) || IS_PINEVIEW(i915))
		return g33_do_reset;
	else if (INTEL_GEN(i915) >= 3)
		return i915_do_reset;
	else
		return NULL;
}

593
int __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask)
594
{
595 596 597
	const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
	reset_func reset;
	int ret = -ETIMEDOUT;
598 599
	int retry;

600
	reset = intel_get_gpu_reset(gt->i915);
601 602
	if (!reset)
		return -ENODEV;
603 604 605 606 607

	/*
	 * If the power well sleeps during the reset, the reset
	 * request may be dropped and never completes (causing -EIO).
	 */
608
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
609
	for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
610 611 612 613 614 615 616 617 618 619 620 621 622 623
		/*
		 * We stop engines, otherwise we might get failed reset and a
		 * dead gpu (on elk). Also as modern gpu as kbl can suffer
		 * from system hang if batchbuffer is progressing when
		 * the reset is issued, regardless of READY_TO_RESET ack.
		 * Thus assume it is best to stop engines on all gens
		 * where we have a gpu reset.
		 *
		 * WaKBLVECSSemaphoreWaitPoll:kbl (on ALL_ENGINES)
		 *
		 * WaMediaResetMainRingCleanup:ctg,elk (presumably)
		 *
		 * FIXME: Wa for more modern gens needs to be validated
		 */
624
		if (retry)
625
			stop_engines(gt, engine_mask);
626

627 628
		GEM_TRACE("engine_mask=%x\n", engine_mask);
		preempt_disable();
629
		ret = reset(gt, engine_mask, retry);
630
		preempt_enable();
631
	}
632
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
633 634 635 636 637 638

	return ret;
}

bool intel_has_gpu_reset(struct drm_i915_private *i915)
{
639 640 641
	if (!i915_modparams.reset)
		return NULL;

642 643 644 645 646 647 648 649
	return intel_get_gpu_reset(i915);
}

bool intel_has_reset_engine(struct drm_i915_private *i915)
{
	return INTEL_INFO(i915)->has_reset_engine && i915_modparams.reset >= 2;
}

650
int intel_reset_guc(struct intel_gt *gt)
651 652
{
	u32 guc_domain =
653
		INTEL_GEN(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
654 655
	int ret;

656
	GEM_BUG_ON(!HAS_GUC(gt->i915));
657

658 659 660
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
	ret = gen6_hw_domain_reset(gt, guc_domain);
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
661 662 663 664 665 666 667 668

	return ret;
}

/*
 * Ensure irq handler finishes, and not run again.
 * Also return the active request so that we only search for it once.
 */
669
static void reset_prepare_engine(struct intel_engine_cs *engine)
670 671 672 673 674 675 676 677
{
	/*
	 * During the reset sequence, we must prevent the engine from
	 * entering RC6. As the context state is undefined until we restart
	 * the engine, if it does enter RC6 during the reset, the state
	 * written to the powercontext is undefined and so we may lose
	 * GPU state upon resume, i.e. fail to restart after a reset.
	 */
678
	intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
679
	engine->reset.prepare(engine);
680 681
}

682
static void revoke_mmaps(struct intel_gt *gt)
683 684 685
{
	int i;

686
	for (i = 0; i < gt->ggtt->num_fences; i++) {
687 688 689 690
		struct drm_vma_offset_node *node;
		struct i915_vma *vma;
		u64 vma_offset;

691
		vma = READ_ONCE(gt->ggtt->fence_regs[i].vma);
692 693 694 695 696 697
		if (!vma)
			continue;

		if (!i915_vma_has_userfault(vma))
			continue;

698
		GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
699 700
		node = &vma->obj->base.vma_node;
		vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
701
		unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
702 703 704 705 706 707
				    drm_vma_node_offset_addr(node) + vma_offset,
				    vma->size,
				    1);
	}
}

708
static intel_engine_mask_t reset_prepare(struct intel_gt *gt)
709 710
{
	struct intel_engine_cs *engine;
711
	intel_engine_mask_t awake = 0;
712 713
	enum intel_engine_id id;

714
	for_each_engine(engine, gt->i915, id) {
715 716
		if (intel_engine_pm_get_if_awake(engine))
			awake |= engine->mask;
717
		reset_prepare_engine(engine);
718
	}
719

720
	intel_uc_reset_prepare(gt->i915);
721 722

	return awake;
723 724
}

725
static void gt_revoke(struct intel_gt *gt)
726
{
727
	revoke_mmaps(gt);
728 729
}

730
static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
731
{
732 733 734 735
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err;

736
	/*
737 738
	 * Everything depends on having the GTT running, so we need to start
	 * there.
739
	 */
740
	err = i915_ggtt_enable_hw(gt->i915);
741 742
	if (err)
		return err;
743

744 745
	for_each_engine(engine, gt->i915, id)
		__intel_engine_reset(engine, stalled_mask & engine->mask);
746

747
	i915_gem_restore_fences(gt->i915);
748

749
	return err;
750 751
}

752
static void reset_finish_engine(struct intel_engine_cs *engine)
753
{
754
	engine->reset.finish(engine);
755
	intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
756 757

	intel_engine_signal_breadcrumbs(engine);
758 759
}

760
static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
761 762 763 764
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;

765
	for_each_engine(engine, gt->i915, id) {
766
		reset_finish_engine(engine);
767 768
		if (awake & engine->mask)
			intel_engine_pm_put(engine);
769
	}
770 771 772 773
}

static void nop_submit_request(struct i915_request *request)
{
774
	struct intel_engine_cs *engine = request->engine;
775 776 777
	unsigned long flags;

	GEM_TRACE("%s fence %llx:%lld -> -EIO\n",
778
		  engine->name, request->fence.context, request->fence.seqno);
779 780
	dma_fence_set_error(&request->fence, -EIO);

781
	spin_lock_irqsave(&engine->active.lock, flags);
782
	__i915_request_submit(request);
783
	i915_request_mark_complete(request);
784
	spin_unlock_irqrestore(&engine->active.lock, flags);
785 786

	intel_engine_queue_breadcrumbs(engine);
787 788
}

789
static void __intel_gt_set_wedged(struct intel_gt *gt)
790 791
{
	struct intel_engine_cs *engine;
792
	intel_engine_mask_t awake;
793 794
	enum intel_engine_id id;

795
	if (test_bit(I915_WEDGED, &gt->reset.flags))
796 797
		return;

798
	if (GEM_SHOW_DEBUG() && !intel_engines_are_idle(gt)) {
799 800
		struct drm_printer p = drm_debug_printer(__func__);

801
		for_each_engine(engine, gt->i915, id)
802 803 804 805 806 807 808 809 810 811
			intel_engine_dump(engine, &p, "%s\n", engine->name);
	}

	GEM_TRACE("start\n");

	/*
	 * First, stop submission to hw, but do not yet complete requests by
	 * rolling the global seqno forward (since this would complete requests
	 * for which we haven't set the fence error to EIO yet).
	 */
812
	awake = reset_prepare(gt);
813

814
	/* Even if the GPU reset fails, it should still stop the engines */
815 816
	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
		__intel_gt_reset(gt, ALL_ENGINES);
817

818
	for_each_engine(engine, gt->i915, id) {
819 820 821
		engine->submit_request = nop_submit_request;
		engine->schedule = NULL;
	}
822
	gt->i915->caps.scheduler = 0;
823 824 825 826 827 828

	/*
	 * Make sure no request can slip through without getting completed by
	 * either this call here to intel_engine_write_global_seqno, or the one
	 * in nop_submit_request.
	 */
C
Chris Wilson 已提交
829
	synchronize_rcu_expedited();
830
	set_bit(I915_WEDGED, &gt->reset.flags);
831 832

	/* Mark all executing requests as skipped */
833
	for_each_engine(engine, gt->i915, id)
834 835
		engine->cancel_requests(engine);

836
	reset_finish(gt, awake);
837 838

	GEM_TRACE("end\n");
839
}
840

841
void intel_gt_set_wedged(struct intel_gt *gt)
842
{
843
	intel_wakeref_t wakeref;
844

845 846 847 848
	mutex_lock(&gt->reset.mutex);
	with_intel_runtime_pm(&gt->i915->runtime_pm, wakeref)
		__intel_gt_set_wedged(gt);
	mutex_unlock(&gt->reset.mutex);
849 850
}

851
static bool __intel_gt_unset_wedged(struct intel_gt *gt)
852
{
853
	struct intel_gt_timelines *timelines = &gt->timelines;
854
	struct intel_timeline *tl;
855

856
	if (!test_bit(I915_WEDGED, &gt->reset.flags))
857 858
		return true;

859
	if (!gt->scratch) /* Never full initialised, recovery impossible */
860 861 862 863 864 865 866 867 868 869 870 871 872 873
		return false;

	GEM_TRACE("start\n");

	/*
	 * Before unwedging, make sure that all pending operations
	 * are flushed and errored out - we may have requests waiting upon
	 * third party fences. We marked all inflight requests as EIO, and
	 * every execbuf since returned EIO, for consistency we want all
	 * the currently pending requests to also be marked as EIO, which
	 * is done inside our nop_submit_request - and so we must wait.
	 *
	 * No more can be submitted until we reset the wedged bit.
	 */
874 875
	mutex_lock(&timelines->mutex);
	list_for_each_entry(tl, &timelines->active_list, link) {
876 877
		struct i915_request *rq;

878
		rq = i915_active_request_get_unlocked(&tl->last_request);
879 880 881 882
		if (!rq)
			continue;

		/*
883 884 885 886 887
		 * All internal dependencies (i915_requests) will have
		 * been flushed by the set-wedge, but we may be stuck waiting
		 * for external fences. These should all be capped to 10s
		 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded
		 * in the worst case.
888
		 */
889
		dma_fence_default_wait(&rq->fence, false, MAX_SCHEDULE_TIMEOUT);
890
		i915_request_put(rq);
891
	}
892
	mutex_unlock(&timelines->mutex);
893

894
	intel_gt_sanitize(gt, false);
895 896 897 898 899 900 901 902 903 904

	/*
	 * Undo nop_submit_request. We prevent all new i915 requests from
	 * being queued (by disallowing execbuf whilst wedged) so having
	 * waited for all active requests above, we know the system is idle
	 * and do not have to worry about a thread being inside
	 * engine->submit_request() as we swap over. So unlike installing
	 * the nop_submit_request on reset, we can do this from normal
	 * context and do not require stop_machine().
	 */
905
	intel_engines_reset_default_submission(gt);
906 907 908 909

	GEM_TRACE("end\n");

	smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
910
	clear_bit(I915_WEDGED, &gt->reset.flags);
911 912

	return true;
913 914
}

915
bool intel_gt_unset_wedged(struct intel_gt *gt)
916 917 918
{
	bool result;

919 920 921
	mutex_lock(&gt->reset.mutex);
	result = __intel_gt_unset_wedged(gt);
	mutex_unlock(&gt->reset.mutex);
922 923 924 925

	return result;
}

926
static int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
927 928 929
{
	int err, i;

930
	gt_revoke(gt);
931

932
	err = __intel_gt_reset(gt, ALL_ENGINES);
933
	for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
934
		msleep(10 * (i + 1));
935
		err = __intel_gt_reset(gt, ALL_ENGINES);
936
	}
937 938
	if (err)
		return err;
939

940
	return gt_reset(gt, stalled_mask);
941 942
}

943
static int resume(struct intel_gt *gt)
944 945 946 947 948
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int ret;

949
	for_each_engine(engine, gt->i915, id) {
950 951 952 953 954 955 956 957
		ret = engine->resume(engine);
		if (ret)
			return ret;
	}

	return 0;
}

958
/**
959 960
 * intel_gt_reset - reset chip after a hang
 * @gt: #intel_gt to reset
961 962 963 964 965 966 967 968 969 970 971 972 973 974
 * @stalled_mask: mask of the stalled engines with the guilty requests
 * @reason: user error message for why we are resetting
 *
 * Reset the chip.  Useful if a hang is detected. Marks the device as wedged
 * on failure.
 *
 * Procedure is fairly simple:
 *   - reset the chip using the reset reg
 *   - re-init context state
 *   - re-init hardware status page
 *   - re-init ring buffer
 *   - re-init interrupt state
 *   - re-init display
 */
975 976 977
void intel_gt_reset(struct intel_gt *gt,
		    intel_engine_mask_t stalled_mask,
		    const char *reason)
978
{
979
	intel_engine_mask_t awake;
980 981
	int ret;

982
	GEM_TRACE("flags=%lx\n", gt->reset.flags);
983 984

	might_sleep();
985 986
	GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
	mutex_lock(&gt->reset.mutex);
987 988

	/* Clear any previous failed attempts at recovery. Time to try again. */
989
	if (!__intel_gt_unset_wedged(gt))
990
		goto unlock;
991 992

	if (reason)
993 994 995
		dev_notice(gt->i915->drm.dev,
			   "Resetting chip for %s\n", reason);
	atomic_inc(&gt->i915->gpu_error.reset_count);
996

997
	awake = reset_prepare(gt);
998

999
	if (!intel_has_gpu_reset(gt->i915)) {
1000
		if (i915_modparams.reset)
1001
			dev_err(gt->i915->drm.dev, "GPU reset not supported\n");
1002 1003 1004 1005 1006
		else
			DRM_DEBUG_DRIVER("GPU reset disabled\n");
		goto error;
	}

1007 1008
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
		intel_runtime_pm_disable_interrupts(gt->i915);
1009

1010 1011
	if (do_reset(gt, stalled_mask)) {
		dev_err(gt->i915->drm.dev, "Failed to reset chip\n");
1012 1013 1014
		goto taint;
	}

1015 1016
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
		intel_runtime_pm_enable_interrupts(gt->i915);
1017

1018
	intel_overlay_reset(gt->i915);
1019 1020 1021 1022 1023 1024 1025 1026 1027

	/*
	 * Next we need to restore the context, but we don't use those
	 * yet either...
	 *
	 * Ring buffer needs to be re-initialized in the KMS case, or if X
	 * was running at the time of the reset (i.e. we weren't VT
	 * switched away).
	 */
1028
	ret = i915_gem_init_hw(gt->i915);
1029 1030 1031
	if (ret) {
		DRM_ERROR("Failed to initialise HW following reset (%d)\n",
			  ret);
1032
		goto taint;
1033 1034
	}

1035
	ret = resume(gt);
1036 1037 1038
	if (ret)
		goto taint;

1039
	intel_gt_queue_hangcheck(gt);
1040 1041

finish:
1042
	reset_finish(gt, awake);
1043
unlock:
1044
	mutex_unlock(&gt->reset.mutex);
1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059
	return;

taint:
	/*
	 * History tells us that if we cannot reset the GPU now, we
	 * never will. This then impacts everything that is run
	 * subsequently. On failing the reset, we mark the driver
	 * as wedged, preventing further execution on the GPU.
	 * We also want to go one step further and add a taint to the
	 * kernel so that any subsequent faults can be traced back to
	 * this failure. This is important for CI, where if the
	 * GPU/driver fails we would like to reboot and restart testing
	 * rather than continue on into oblivion. For everyone else,
	 * the system should still plod along, but they have been warned!
	 */
1060
	add_taint_for_CI(TAINT_WARN);
1061
error:
1062
	__intel_gt_set_wedged(gt);
1063 1064 1065
	goto finish;
}

1066
static inline int intel_gt_reset_engine(struct intel_engine_cs *engine)
1067
{
1068
	return __intel_gt_reset(engine->gt, engine->mask);
1069 1070 1071
}

/**
1072
 * intel_engine_reset - reset GPU engine to recover from a hang
1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
 * @engine: engine to reset
 * @msg: reason for GPU reset; or NULL for no dev_notice()
 *
 * Reset a specific GPU engine. Useful if a hang is detected.
 * Returns zero on successful reset or otherwise an error code.
 *
 * Procedure is:
 *  - identifies the request that caused the hang and it is dropped
 *  - reset engine (which will force the engine to idle)
 *  - re-init/configure engine
 */
1084
int intel_engine_reset(struct intel_engine_cs *engine, const char *msg)
1085
{
1086
	struct intel_gt *gt = engine->gt;
1087 1088
	int ret;

1089 1090
	GEM_TRACE("%s flags=%lx\n", engine->name, gt->reset.flags);
	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
1091

1092
	if (!intel_engine_pm_get_if_awake(engine))
1093 1094
		return 0;

1095
	reset_prepare_engine(engine);
1096 1097 1098 1099

	if (msg)
		dev_notice(engine->i915->drm.dev,
			   "Resetting %s for %s\n", engine->name, msg);
1100
	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
1101 1102

	if (!engine->i915->guc.execbuf_client)
1103
		ret = intel_gt_reset_engine(engine);
1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118
	else
		ret = intel_guc_reset_engine(&engine->i915->guc, engine);
	if (ret) {
		/* If we fail here, we expect to fallback to a global reset */
		DRM_DEBUG_DRIVER("%sFailed to reset %s, ret=%d\n",
				 engine->i915->guc.execbuf_client ? "GuC " : "",
				 engine->name, ret);
		goto out;
	}

	/*
	 * The request that caused the hang is stuck on elsp, we know the
	 * active request and can drop it, adjust head to skip the offending
	 * request to resume executing remaining requests in the queue.
	 */
1119
	__intel_engine_reset(engine, true);
1120 1121 1122 1123 1124 1125

	/*
	 * The engine and its registers (and workarounds in case of render)
	 * have been reset to their default values. Follow the init_ring
	 * process to program RING_MODE, HWSP and re-enable submission.
	 */
1126
	ret = engine->resume(engine);
1127 1128 1129 1130

out:
	intel_engine_cancel_stop_cs(engine);
	reset_finish_engine(engine);
1131
	intel_engine_pm_put(engine);
1132 1133 1134
	return ret;
}

1135 1136 1137
static void intel_gt_reset_global(struct intel_gt *gt,
				  u32 engine_mask,
				  const char *reason)
1138
{
1139
	struct kobject *kobj = &gt->i915->drm.primary->kdev->kobj;
1140 1141 1142
	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
1143
	struct intel_wedge_me w;
1144 1145 1146 1147 1148 1149 1150

	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);

	DRM_DEBUG_DRIVER("resetting chip\n");
	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);

	/* Use a watchdog to ensure that our reset completes */
1151 1152
	intel_wedge_on_timeout(&w, gt, 5 * HZ) {
		intel_prepare_reset(gt->i915);
1153

1154
		/* Flush everyone using a resource about to be clobbered */
1155
		synchronize_srcu_expedited(&gt->reset.backoff_srcu);
1156

1157
		intel_gt_reset(gt, engine_mask, reason);
1158

1159
		intel_finish_reset(gt->i915);
1160 1161
	}

1162
	if (!test_bit(I915_WEDGED, &gt->reset.flags))
1163 1164 1165 1166
		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
}

/**
1167 1168
 * intel_gt_handle_error - handle a gpu error
 * @gt: the intel_gt
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178
 * @engine_mask: mask representing engines that are hung
 * @flags: control flags
 * @fmt: Error message format string
 *
 * Do some basic checking of register state at error time and
 * dump it to the syslog.  Also call i915_capture_error_state() to make
 * sure we get a record and make it available in debugfs.  Fire a uevent
 * so userspace knows something bad happened (should trigger collection
 * of a ring dump etc.).
 */
1179 1180 1181 1182
void intel_gt_handle_error(struct intel_gt *gt,
			   intel_engine_mask_t engine_mask,
			   unsigned long flags,
			   const char *fmt, ...)
1183 1184 1185
{
	struct intel_engine_cs *engine;
	intel_wakeref_t wakeref;
1186
	intel_engine_mask_t tmp;
1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206
	char error_msg[80];
	char *msg = NULL;

	if (fmt) {
		va_list args;

		va_start(args, fmt);
		vscnprintf(error_msg, sizeof(error_msg), fmt, args);
		va_end(args);

		msg = error_msg;
	}

	/*
	 * In most cases it's guaranteed that we get here with an RPM
	 * reference held, for example because there is a pending GPU
	 * request that won't finish until the reset is done. This
	 * isn't the case at least when we get here by doing a
	 * simulated reset via debugfs, so get an RPM reference.
	 */
1207
	wakeref = intel_runtime_pm_get(&gt->i915->runtime_pm);
1208

1209
	engine_mask &= INTEL_INFO(gt->i915)->engine_mask;
1210 1211

	if (flags & I915_ERROR_CAPTURE) {
1212 1213
		i915_capture_error_state(gt->i915, engine_mask, msg);
		intel_gt_clear_error_registers(gt, engine_mask);
1214 1215 1216 1217 1218 1219
	}

	/*
	 * Try engine reset when available. We fall back to full reset if
	 * single reset fails.
	 */
1220 1221
	if (intel_has_reset_engine(gt->i915) && !intel_gt_is_wedged(gt)) {
		for_each_engine_masked(engine, gt->i915, engine_mask, tmp) {
1222 1223
			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1224
					     &gt->reset.flags))
1225 1226
				continue;

1227
			if (intel_engine_reset(engine, msg) == 0)
1228
				engine_mask &= ~engine->mask;
1229

1230 1231
			clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
					      &gt->reset.flags);
1232 1233 1234 1235 1236 1237 1238
		}
	}

	if (!engine_mask)
		goto out;

	/* Full reset needs the mutex, stop any other user trying to do so. */
1239 1240 1241
	if (test_and_set_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
		wait_event(gt->reset.queue,
			   !test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
1242
		goto out; /* piggy-back on the other reset */
1243 1244
	}

1245 1246 1247
	/* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
	synchronize_rcu_expedited();

1248
	/* Prevent any other reset-engine attempt. */
1249
	for_each_engine(engine, gt->i915, tmp) {
1250
		while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1251 1252
					&gt->reset.flags))
			wait_on_bit(&gt->reset.flags,
1253 1254 1255 1256
				    I915_RESET_ENGINE + engine->id,
				    TASK_UNINTERRUPTIBLE);
	}

1257
	intel_gt_reset_global(gt, engine_mask, msg);
1258

1259 1260 1261 1262 1263 1264
	for_each_engine(engine, gt->i915, tmp)
		clear_bit_unlock(I915_RESET_ENGINE + engine->id,
				 &gt->reset.flags);
	clear_bit_unlock(I915_RESET_BACKOFF, &gt->reset.flags);
	smp_mb__after_atomic();
	wake_up_all(&gt->reset.queue);
1265 1266

out:
1267
	intel_runtime_pm_put(&gt->i915->runtime_pm, wakeref);
1268 1269
}

1270
int intel_gt_reset_trylock(struct intel_gt *gt)
1271 1272 1273
{
	int srcu;

1274
	might_lock(&gt->reset.backoff_srcu);
1275 1276
	might_sleep();

1277
	rcu_read_lock();
1278
	while (test_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
1279 1280
		rcu_read_unlock();

1281
		if (wait_event_interruptible(gt->reset.queue,
1282
					     !test_bit(I915_RESET_BACKOFF,
1283
						       &gt->reset.flags)))
1284 1285 1286 1287
			return -EINTR;

		rcu_read_lock();
	}
1288
	srcu = srcu_read_lock(&gt->reset.backoff_srcu);
1289 1290 1291 1292 1293
	rcu_read_unlock();

	return srcu;
}

1294 1295
void intel_gt_reset_unlock(struct intel_gt *gt, int tag)
__releases(&gt->reset.backoff_srcu)
1296
{
1297
	srcu_read_unlock(&gt->reset.backoff_srcu, tag);
1298 1299
}

1300
int intel_gt_terminally_wedged(struct intel_gt *gt)
1301 1302 1303
{
	might_sleep();

1304
	if (!intel_gt_is_wedged(gt))
1305 1306 1307
		return 0;

	/* Reset still in progress? Maybe we will recover? */
1308
	if (!test_bit(I915_RESET_BACKOFF, &gt->reset.flags))
1309 1310 1311
		return -EIO;

	/* XXX intel_reset_finish() still takes struct_mutex!!! */
1312
	if (mutex_is_locked(&gt->i915->drm.struct_mutex))
1313 1314
		return -EAGAIN;

1315
	if (wait_event_interruptible(gt->reset.queue,
1316
				     !test_bit(I915_RESET_BACKOFF,
1317
					       &gt->reset.flags)))
1318 1319
		return -EINTR;

1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332
	return intel_gt_is_wedged(gt) ? -EIO : 0;
}

void intel_gt_init_reset(struct intel_gt *gt)
{
	init_waitqueue_head(&gt->reset.queue);
	mutex_init(&gt->reset.mutex);
	init_srcu_struct(&gt->reset.backoff_srcu);
}

void intel_gt_fini_reset(struct intel_gt *gt)
{
	cleanup_srcu_struct(&gt->reset.backoff_srcu);
1333 1334
}

1335
static void intel_wedge_me(struct work_struct *work)
1336
{
1337
	struct intel_wedge_me *w = container_of(work, typeof(*w), work.work);
1338

1339
	dev_err(w->gt->i915->drm.dev,
1340 1341
		"%s timed out, cancelling all in-flight rendering.\n",
		w->name);
1342
	intel_gt_set_wedged(w->gt);
1343 1344
}

1345 1346 1347 1348
void __intel_init_wedge(struct intel_wedge_me *w,
			struct intel_gt *gt,
			long timeout,
			const char *name)
1349
{
1350
	w->gt = gt;
1351 1352
	w->name = name;

1353
	INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me);
1354 1355 1356
	schedule_delayed_work(&w->work, timeout);
}

1357
void __intel_fini_wedge(struct intel_wedge_me *w)
1358 1359 1360
{
	cancel_delayed_work_sync(&w->work);
	destroy_delayed_work_on_stack(&w->work);
1361
	w->gt = NULL;
1362
}
1363 1364 1365 1366

#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
#include "selftest_reset.c"
#endif