intel_reset.c 38.6 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 18
#include "intel_engine_pm.h"
#include "intel_gt_pm.h"
19
#include "intel_reset.h"
20 21 22

#include "intel_guc.h"

23 24
#define RESET_MAX_RETRIES 3

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

28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
static void rmw_set(struct intel_uncore *uncore, i915_reg_t reg, u32 set)
{
	intel_uncore_rmw(uncore, reg, 0, set);
}

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

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);
}

48 49 50 51 52
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;

53
	lockdep_assert_held(&engine->active.lock);
54

55 56
	if (!i915_request_is_active(rq))
		return;
57

58
	list_for_each_entry_continue(rq, &engine->active.requests, sched.link)
59 60
		if (rq->gem_context == hung_ctx)
			i915_request_skip(rq, -EIO);
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
}

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));
	}
}

87
static bool context_mark_guilty(struct i915_gem_context *ctx)
88
{
89 90 91
	unsigned long prev_hang;
	bool banned;
	int i;
92 93 94

	atomic_inc(&ctx->guilty_count);

95 96
	/* Cool contexts are too cool to be banned! (Used for reset testing.) */
	if (!i915_gem_context_is_bannable(ctx))
97
		return false;
98

99 100 101 102 103 104 105 106 107 108
	/* 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;
109
	if (banned) {
110 111
		DRM_DEBUG_DRIVER("context %s: guilty %d, banned\n",
				 ctx->name, atomic_read(&ctx->guilty_count));
112 113 114 115 116
		i915_gem_context_set_banned(ctx);
	}

	if (!IS_ERR_OR_NULL(ctx->file_priv))
		client_mark_guilty(ctx->file_priv, ctx);
117 118

	return banned;
119 120 121 122 123 124 125
}

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

126 127
void i915_reset_request(struct i915_request *rq, bool guilty)
{
128 129 130 131 132 133
	GEM_TRACE("%s rq=%llx:%lld, guilty? %s\n",
		  rq->engine->name,
		  rq->fence.context,
		  rq->fence.seqno,
		  yesno(guilty));

134
	lockdep_assert_held(&rq->engine->active.lock);
135 136 137 138 139 140 141 142 143 144 145 146
	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);
	}
}

147 148
static void gen3_stop_engine(struct intel_engine_cs *engine)
{
149
	struct intel_uncore *uncore = engine->uncore;
150 151
	const u32 base = engine->mmio_base;

152 153
	GEM_TRACE("%s\n", engine->name);

154
	if (intel_engine_stop_cs(engine))
155
		GEM_TRACE("%s: timed out on STOP_RING\n", engine->name);
156

157 158 159 160
	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 */
161

162 163 164
	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));
165 166

	/* The ring must be empty before it is disabled */
167
	intel_uncore_write_fw(uncore, RING_CTL(base), 0);
168 169

	/* Check acts as a post */
170
	if (intel_uncore_read_fw(uncore, RING_HEAD(base)))
171
		GEM_TRACE("%s: ring head [%x] not parked\n",
172 173
			  engine->name,
			  intel_uncore_read_fw(uncore, RING_HEAD(base)));
174 175 176
}

static void i915_stop_engines(struct drm_i915_private *i915,
177
			      intel_engine_mask_t engine_mask)
178 179
{
	struct intel_engine_cs *engine;
180
	intel_engine_mask_t tmp;
181 182 183 184

	if (INTEL_GEN(i915) < 3)
		return;

185
	for_each_engine_masked(engine, i915, engine_mask, tmp)
186 187 188 189 190 191 192 193 194 195 196 197
		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;
}

static int i915_do_reset(struct drm_i915_private *i915,
198
			 intel_engine_mask_t engine_mask,
199 200 201 202 203 204 205
			 unsigned int retry)
{
	struct pci_dev *pdev = i915->drm.pdev;
	int err;

	/* Assert reset for at least 20 usec, and wait for acknowledgement. */
	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
206 207
	udelay(50);
	err = wait_for_atomic(i915_in_reset(pdev), 50);
208 209 210

	/* Clear the reset request. */
	pci_write_config_byte(pdev, I915_GDRST, 0);
211
	udelay(50);
212
	if (!err)
213
		err = wait_for_atomic(!i915_in_reset(pdev), 50);
214 215 216 217 218 219 220 221 222 223 224 225 226

	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;
}

static int g33_do_reset(struct drm_i915_private *i915,
227
			intel_engine_mask_t engine_mask,
228 229 230 231 232
			unsigned int retry)
{
	struct pci_dev *pdev = i915->drm.pdev;

	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
233
	return wait_for_atomic(g4x_reset_complete(pdev), 50);
234 235
}

236
static int g4x_do_reset(struct drm_i915_private *i915,
237
			intel_engine_mask_t engine_mask,
238 239
			unsigned int retry)
{
240 241
	struct pci_dev *pdev = i915->drm.pdev;
	struct intel_uncore *uncore = &i915->uncore;
242 243 244
	int ret;

	/* WaVcpClkGateDisableForMediaReset:ctg,elk */
245 246
	rmw_set_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
247 248 249

	pci_write_config_byte(pdev, I915_GDRST,
			      GRDOM_MEDIA | GRDOM_RESET_ENABLE);
250
	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
251 252 253 254 255 256 257
	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);
258
	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
259 260 261 262 263 264 265 266
	if (ret) {
		DRM_DEBUG_DRIVER("Wait for render reset failed\n");
		goto out;
	}

out:
	pci_write_config_byte(pdev, I915_GDRST, 0);

267 268
	rmw_clear_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
269 270 271 272

	return ret;
}

273
static int ironlake_do_reset(struct drm_i915_private *i915,
274
			     intel_engine_mask_t engine_mask,
275 276
			     unsigned int retry)
{
277
	struct intel_uncore *uncore = &i915->uncore;
278 279
	int ret;

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

291 292 293
	intel_uncore_write_fw(uncore, ILK_GDSR,
			      ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
294 295 296
					   ILK_GRDOM_RESET_ENABLE, 0,
					   5000, 0,
					   NULL);
297 298 299 300 301 302
	if (ret) {
		DRM_DEBUG_DRIVER("Wait for media reset failed\n");
		goto out;
	}

out:
303 304
	intel_uncore_write_fw(uncore, ILK_GDSR, 0);
	intel_uncore_posting_read_fw(uncore, ILK_GDSR);
305 306 307 308
	return ret;
}

/* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
309
static int gen6_hw_domain_reset(struct drm_i915_private *i915,
310 311
				u32 hw_domain_mask)
{
312
	struct intel_uncore *uncore = &i915->uncore;
313 314 315 316 317 318 319
	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
	 */
320
	intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask);
321 322

	/* Wait for the device to ack the reset requests */
323
	err = __intel_wait_for_register_fw(uncore,
324 325 326 327 328 329 330 331 332 333 334
					   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;
}

static int gen6_reset_engines(struct drm_i915_private *i915,
335
			      intel_engine_mask_t engine_mask,
336 337 338
			      unsigned int retry)
{
	struct intel_engine_cs *engine;
339 340 341 342 343 344
	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,
345 346 347 348 349 350
	};
	u32 hw_mask;

	if (engine_mask == ALL_ENGINES) {
		hw_mask = GEN6_GRDOM_FULL;
	} else {
351
		intel_engine_mask_t tmp;
352 353

		hw_mask = 0;
354 355
		for_each_engine_masked(engine, i915, engine_mask, tmp) {
			GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
356
			hw_mask |= hw_engine_mask[engine->id];
357
		}
358 359 360 361 362
	}

	return gen6_hw_domain_reset(i915, hw_mask);
}

363
static u32 gen11_lock_sfc(struct intel_engine_cs *engine)
364
{
365 366
	struct intel_uncore *uncore = engine->uncore;
	u8 vdbox_sfc_access = RUNTIME_INFO(engine->i915)->vdbox_sfc_access;
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 402 403 404 405 406 407 408 409 410 411 412
	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).
	 */
413
	rmw_set_fw(uncore, sfc_forced_lock, sfc_forced_lock_bit);
414

415
	if (__intel_wait_for_register_fw(uncore,
416 417 418 419 420 421 422 423
					 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;
	}

424
	if (intel_uncore_read_fw(uncore, sfc_usage) & sfc_usage_bit)
425 426 427 428 429
		return sfc_reset_bit;

	return 0;
}

430
static void gen11_unlock_sfc(struct intel_engine_cs *engine)
431
{
432 433
	struct intel_uncore *uncore = engine->uncore;
	u8 vdbox_sfc_access = RUNTIME_INFO(engine->i915)->vdbox_sfc_access;
434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
	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;
	}

455
	rmw_clear_fw(uncore, sfc_forced_lock, sfc_forced_lock_bit);
456 457 458
}

static int gen11_reset_engines(struct drm_i915_private *i915,
459
			       intel_engine_mask_t engine_mask,
460 461
			       unsigned int retry)
{
462 463 464 465 466 467 468 469 470
	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,
471 472
	};
	struct intel_engine_cs *engine;
473
	intel_engine_mask_t tmp;
474 475 476 477 478 479 480 481
	u32 hw_mask;
	int ret;

	if (engine_mask == ALL_ENGINES) {
		hw_mask = GEN11_GRDOM_FULL;
	} else {
		hw_mask = 0;
		for_each_engine_masked(engine, i915, engine_mask, tmp) {
482
			GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
483
			hw_mask |= hw_engine_mask[engine->id];
484
			hw_mask |= gen11_lock_sfc(engine);
485 486 487 488 489 490 491
		}
	}

	ret = gen6_hw_domain_reset(i915, hw_mask);

	if (engine_mask != ALL_ENGINES)
		for_each_engine_masked(engine, i915, engine_mask, tmp)
492
			gen11_unlock_sfc(engine);
493 494 495 496 497 498

	return ret;
}

static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
{
499
	struct intel_uncore *uncore = engine->uncore;
500 501
	const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
	u32 request, mask, ack;
502 503
	int ret;

504
	ack = intel_uncore_read_fw(uncore, reg);
505 506 507 508 509 510 511 512 513 514 515
	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)) {
516 517 518 519 520 521
		request = RESET_CTL_REQUEST_RESET;
		mask = RESET_CTL_READY_TO_RESET;
		ack = RESET_CTL_READY_TO_RESET;
	} else {
		return 0;
	}
522

523 524 525
	intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
	ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
					   700, 0, NULL);
526
	if (ret)
527 528 529
		DRM_ERROR("%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
			  engine->name, request,
			  intel_uncore_read_fw(uncore, reg));
530 531 532 533 534 535

	return ret;
}

static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
{
536 537 538
	intel_uncore_write_fw(engine->uncore,
			      RING_RESET_CTL(engine->mmio_base),
			      _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
539 540 541
}

static int gen8_reset_engines(struct drm_i915_private *i915,
542
			      intel_engine_mask_t engine_mask,
543 544 545 546
			      unsigned int retry)
{
	struct intel_engine_cs *engine;
	const bool reset_non_ready = retry >= 1;
547
	intel_engine_mask_t tmp;
548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582
	int ret;

	for_each_engine_masked(engine, i915, engine_mask, tmp) {
		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
		 * i915_stop_engines we have before the reset.
		 */
	}

	if (INTEL_GEN(i915) >= 11)
		ret = gen11_reset_engines(i915, engine_mask, retry);
	else
		ret = gen6_reset_engines(i915, engine_mask, retry);

skip_reset:
	for_each_engine_masked(engine, i915, engine_mask, tmp)
		gen8_engine_reset_cancel(engine);

	return ret;
}

typedef int (*reset_func)(struct drm_i915_private *,
583
			  intel_engine_mask_t engine_mask,
584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603
			  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;
}

604 605
int intel_gpu_reset(struct drm_i915_private *i915,
		    intel_engine_mask_t engine_mask)
606
{
607 608 609
	const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
	reset_func reset;
	int ret = -ETIMEDOUT;
610 611
	int retry;

612 613 614
	reset = intel_get_gpu_reset(i915);
	if (!reset)
		return -ENODEV;
615 616 617 618 619

	/*
	 * If the power well sleeps during the reset, the reset
	 * request may be dropped and never completes (causing -EIO).
	 */
620
	intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
621
	for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
622 623 624 625 626 627 628 629 630 631 632 633 634 635
		/*
		 * 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
		 */
636 637
		if (retry)
			i915_stop_engines(i915, engine_mask);
638

639 640 641 642
		GEM_TRACE("engine_mask=%x\n", engine_mask);
		preempt_disable();
		ret = reset(i915, engine_mask, retry);
		preempt_enable();
643
	}
644
	intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
645 646 647 648 649 650

	return ret;
}

bool intel_has_gpu_reset(struct drm_i915_private *i915)
{
651 652 653
	if (!i915_modparams.reset)
		return NULL;

654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
	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;
}

int intel_reset_guc(struct drm_i915_private *i915)
{
	u32 guc_domain =
		INTEL_GEN(i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
	int ret;

	GEM_BUG_ON(!HAS_GUC(i915));

670
	intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
671
	ret = gen6_hw_domain_reset(i915, guc_domain);
672
	intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
673 674 675 676 677 678 679 680

	return ret;
}

/*
 * Ensure irq handler finishes, and not run again.
 * Also return the active request so that we only search for it once.
 */
681
static void reset_prepare_engine(struct intel_engine_cs *engine)
682 683 684 685 686 687 688 689
{
	/*
	 * 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.
	 */
690
	intel_engine_pm_get(engine);
691
	intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
692
	engine->reset.prepare(engine);
693 694
}

695 696 697 698
static void revoke_mmaps(struct drm_i915_private *i915)
{
	int i;

699
	for (i = 0; i < i915->ggtt.num_fences; i++) {
700 701 702 703
		struct drm_vma_offset_node *node;
		struct i915_vma *vma;
		u64 vma_offset;

704
		vma = READ_ONCE(i915->ggtt.fence_regs[i].vma);
705 706 707 708 709 710
		if (!vma)
			continue;

		if (!i915_vma_has_userfault(vma))
			continue;

711
		GEM_BUG_ON(vma->fence != &i915->ggtt.fence_regs[i]);
712 713 714 715 716 717 718 719 720
		node = &vma->obj->base.vma_node;
		vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
		unmap_mapping_range(i915->drm.anon_inode->i_mapping,
				    drm_vma_node_offset_addr(node) + vma_offset,
				    vma->size,
				    1);
	}
}

721
static void reset_prepare(struct drm_i915_private *i915)
722 723 724 725
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;

726
	intel_gt_pm_get(i915);
727 728
	for_each_engine(engine, i915, id)
		reset_prepare_engine(engine);
729

730
	intel_uc_reset_prepare(i915);
731 732 733 734
}

static void gt_revoke(struct drm_i915_private *i915)
{
735
	revoke_mmaps(i915);
736 737
}

738 739
static int gt_reset(struct drm_i915_private *i915,
		    intel_engine_mask_t stalled_mask)
740
{
741 742 743 744
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err;

745
	/*
746 747
	 * Everything depends on having the GTT running, so we need to start
	 * there.
748
	 */
749 750 751
	err = i915_ggtt_enable_hw(i915);
	if (err)
		return err;
752

753
	for_each_engine(engine, i915, id)
754
		intel_engine_reset(engine, stalled_mask & engine->mask);
755

756
	i915_gem_restore_fences(i915);
757

758
	return err;
759 760
}

761
static void reset_finish_engine(struct intel_engine_cs *engine)
762
{
763
	engine->reset.finish(engine);
764
	intel_engine_pm_put(engine);
765
	intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
766 767 768 769 770 771 772
}

static void reset_finish(struct drm_i915_private *i915)
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;

773
	for_each_engine(engine, i915, id) {
774
		reset_finish_engine(engine);
775 776
		intel_engine_signal_breadcrumbs(engine);
	}
777
	intel_gt_pm_put(i915);
778 779 780 781
}

static void nop_submit_request(struct i915_request *request)
{
782
	struct intel_engine_cs *engine = request->engine;
783 784 785
	unsigned long flags;

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

789
	spin_lock_irqsave(&engine->active.lock, flags);
790
	__i915_request_submit(request);
791
	i915_request_mark_complete(request);
792
	spin_unlock_irqrestore(&engine->active.lock, flags);
793 794

	intel_engine_queue_breadcrumbs(engine);
795 796
}

797
static void __i915_gem_set_wedged(struct drm_i915_private *i915)
798 799 800 801 802
{
	struct i915_gpu_error *error = &i915->gpu_error;
	struct intel_engine_cs *engine;
	enum intel_engine_id id;

803
	if (test_bit(I915_WEDGED, &error->flags))
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819
		return;

	if (GEM_SHOW_DEBUG() && !intel_engines_are_idle(i915)) {
		struct drm_printer p = drm_debug_printer(__func__);

		for_each_engine(engine, i915, id)
			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).
	 */
820
	reset_prepare(i915);
821

822
	/* Even if the GPU reset fails, it should still stop the engines */
823
	if (!INTEL_INFO(i915)->gpu_reset_clobbers_display)
824 825 826 827 828 829 830 831 832 833 834 835 836
		intel_gpu_reset(i915, ALL_ENGINES);

	for_each_engine(engine, i915, id) {
		engine->submit_request = nop_submit_request;
		engine->schedule = NULL;
	}
	i915->caps.scheduler = 0;

	/*
	 * 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 已提交
837
	synchronize_rcu_expedited();
838
	set_bit(I915_WEDGED, &error->flags);
839 840 841 842 843

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

844
	reset_finish(i915);
845 846

	GEM_TRACE("end\n");
847
}
848

849 850 851
void i915_gem_set_wedged(struct drm_i915_private *i915)
{
	struct i915_gpu_error *error = &i915->gpu_error;
852
	intel_wakeref_t wakeref;
853 854

	mutex_lock(&error->wedge_mutex);
855
	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
856
		__i915_gem_set_wedged(i915);
857
	mutex_unlock(&error->wedge_mutex);
858 859
}

860
static bool __i915_gem_unset_wedged(struct drm_i915_private *i915)
861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882
{
	struct i915_gpu_error *error = &i915->gpu_error;
	struct i915_timeline *tl;

	if (!test_bit(I915_WEDGED, &error->flags))
		return true;

	if (!i915->gt.scratch) /* Never full initialised, recovery impossible */
		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.
	 */
883
	mutex_lock(&i915->gt.timelines.mutex);
C
Chris Wilson 已提交
884
	list_for_each_entry(tl, &i915->gt.timelines.active_list, link) {
885 886
		struct i915_request *rq;

887
		rq = i915_active_request_get_unlocked(&tl->last_request);
888 889 890 891
		if (!rq)
			continue;

		/*
892 893 894 895 896
		 * 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.
897
		 */
898
		dma_fence_default_wait(&rq->fence, false, MAX_SCHEDULE_TIMEOUT);
899
		i915_request_put(rq);
900
	}
901
	mutex_unlock(&i915->gt.timelines.mutex);
902

903
	intel_gt_sanitize(i915, false);
904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919

	/*
	 * 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().
	 */
	intel_engines_reset_default_submission(i915);

	GEM_TRACE("end\n");

	smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
	clear_bit(I915_WEDGED, &i915->gpu_error.flags);
920 921

	return true;
922 923
}

924 925 926 927 928 929 930 931 932 933 934 935
bool i915_gem_unset_wedged(struct drm_i915_private *i915)
{
	struct i915_gpu_error *error = &i915->gpu_error;
	bool result;

	mutex_lock(&error->wedge_mutex);
	result = __i915_gem_unset_wedged(i915);
	mutex_unlock(&error->wedge_mutex);

	return result;
}

936 937
static int do_reset(struct drm_i915_private *i915,
		    intel_engine_mask_t stalled_mask)
938 939 940
{
	int err, i;

941 942
	gt_revoke(i915);

943
	err = intel_gpu_reset(i915, ALL_ENGINES);
944
	for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
945 946
		msleep(10 * (i + 1));
		err = intel_gpu_reset(i915, ALL_ENGINES);
947
	}
948 949
	if (err)
		return err;
950

951
	return gt_reset(i915, stalled_mask);
952 953
}

954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971
/**
 * i915_reset - reset chip after a hang
 * @i915: #drm_i915_private to reset
 * @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
 */
void i915_reset(struct drm_i915_private *i915,
972
		intel_engine_mask_t stalled_mask,
973 974 975 976 977 978 979 980 981
		const char *reason)
{
	struct i915_gpu_error *error = &i915->gpu_error;
	int ret;

	GEM_TRACE("flags=%lx\n", error->flags);

	might_sleep();
	GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &error->flags));
982
	mutex_lock(&error->wedge_mutex);
983 984

	/* Clear any previous failed attempts at recovery. Time to try again. */
985
	if (!__i915_gem_unset_wedged(i915))
986
		goto unlock;
987 988 989 990 991

	if (reason)
		dev_notice(i915->drm.dev, "Resetting chip for %s\n", reason);
	error->reset_count++;

992
	reset_prepare(i915);
993 994 995 996 997 998 999 1000 1001

	if (!intel_has_gpu_reset(i915)) {
		if (i915_modparams.reset)
			dev_err(i915->drm.dev, "GPU reset not supported\n");
		else
			DRM_DEBUG_DRIVER("GPU reset disabled\n");
		goto error;
	}

1002 1003 1004
	if (INTEL_INFO(i915)->gpu_reset_clobbers_display)
		intel_runtime_pm_disable_interrupts(i915);

1005
	if (do_reset(i915, stalled_mask)) {
1006 1007 1008 1009
		dev_err(i915->drm.dev, "Failed to reset chip\n");
		goto taint;
	}

1010 1011 1012
	if (INTEL_INFO(i915)->gpu_reset_clobbers_display)
		intel_runtime_pm_enable_interrupts(i915);

1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033
	intel_overlay_reset(i915);

	/*
	 * 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).
	 */
	ret = i915_gem_init_hw(i915);
	if (ret) {
		DRM_ERROR("Failed to initialise HW following reset (%d)\n",
			  ret);
		goto error;
	}

	i915_queue_hangcheck(i915);

finish:
	reset_finish(i915);
1034
unlock:
1035
	mutex_unlock(&error->wedge_mutex);
1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
	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!
	 */
1051
	add_taint_for_CI(TAINT_WARN);
1052
error:
1053
	__i915_gem_set_wedged(i915);
1054 1055 1056 1057 1058 1059
	goto finish;
}

static inline int intel_gt_reset_engine(struct drm_i915_private *i915,
					struct intel_engine_cs *engine)
{
1060
	return intel_gpu_reset(i915, engine->mask);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083
}

/**
 * i915_reset_engine - reset GPU engine to recover from a hang
 * @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
 */
int i915_reset_engine(struct intel_engine_cs *engine, const char *msg)
{
	struct i915_gpu_error *error = &engine->i915->gpu_error;
	int ret;

	GEM_TRACE("%s flags=%lx\n", engine->name, error->flags);
	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &error->flags));

1084 1085 1086
	if (!intel_wakeref_active(&engine->wakeref))
		return 0;

1087
	reset_prepare_engine(engine);
1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110

	if (msg)
		dev_notice(engine->i915->drm.dev,
			   "Resetting %s for %s\n", engine->name, msg);
	error->reset_engine_count[engine->id]++;

	if (!engine->i915->guc.execbuf_client)
		ret = intel_gt_reset_engine(engine->i915, engine);
	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.
	 */
1111
	intel_engine_reset(engine, true);
1112 1113 1114 1115 1116 1117

	/*
	 * 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.
	 */
1118
	ret = engine->resume(engine);
1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147
	if (ret)
		goto out;

out:
	intel_engine_cancel_stop_cs(engine);
	reset_finish_engine(engine);
	return ret;
}

static void i915_reset_device(struct drm_i915_private *i915,
			      u32 engine_mask,
			      const char *reason)
{
	struct i915_gpu_error *error = &i915->gpu_error;
	struct kobject *kobj = &i915->drm.primary->kdev->kobj;
	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 };
	struct i915_wedge_me w;

	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 */
	i915_wedge_on_timeout(&w, i915, 5 * HZ) {
		intel_prepare_reset(i915);

1148
		/* Flush everyone using a resource about to be clobbered */
1149
		synchronize_srcu_expedited(&error->reset_backoff_srcu);
1150

1151
		i915_reset(i915, engine_mask, reason);
1152 1153 1154 1155 1156 1157 1158 1159

		intel_finish_reset(i915);
	}

	if (!test_bit(I915_WEDGED, &error->flags))
		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
}

1160
static void clear_register(struct intel_uncore *uncore, i915_reg_t reg)
1161
{
1162
	intel_uncore_rmw(uncore, reg, 0, 0);
1163 1164
}

1165 1166 1167 1168 1169 1170
static void gen8_clear_engine_error_register(struct intel_engine_cs *engine)
{
	GEN6_RING_FAULT_REG_RMW(engine, RING_FAULT_VALID, 0);
	GEN6_RING_FAULT_REG_POSTING_READ(engine);
}

1171 1172
static void clear_error_registers(struct drm_i915_private *i915,
				  intel_engine_mask_t engine_mask)
1173
{
1174
	struct intel_uncore *uncore = &i915->uncore;
1175 1176
	u32 eir;

1177 1178
	if (!IS_GEN(i915, 2))
		clear_register(uncore, PGTBL_ER);
1179

1180 1181
	if (INTEL_GEN(i915) < 4)
		clear_register(uncore, IPEIR(RENDER_RING_BASE));
1182
	else
1183
		clear_register(uncore, IPEIR_I965);
1184

1185 1186
	clear_register(uncore, EIR);
	eir = intel_uncore_read(uncore, EIR);
1187 1188 1189 1190 1191 1192
	if (eir) {
		/*
		 * some errors might have become stuck,
		 * mask them.
		 */
		DRM_DEBUG_DRIVER("EIR stuck: 0x%08x, masking\n", eir);
1193
		rmw_set(uncore, EMR, eir);
1194 1195
		intel_uncore_write(uncore, GEN2_IIR,
				   I915_MASTER_ERROR_INTERRUPT);
1196 1197
	}

1198 1199 1200 1201
	if (INTEL_GEN(i915) >= 8) {
		rmw_clear(uncore, GEN8_RING_FAULT_REG, RING_FAULT_VALID);
		intel_uncore_posting_read(uncore, GEN8_RING_FAULT_REG);
	} else if (INTEL_GEN(i915) >= 6) {
1202 1203 1204
		struct intel_engine_cs *engine;
		enum intel_engine_id id;

1205 1206
		for_each_engine_masked(engine, i915, engine_mask, id)
			gen8_clear_engine_error_register(engine);
1207 1208 1209
	}
}

1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272
static void gen6_check_faults(struct drm_i915_private *dev_priv)
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	u32 fault;

	for_each_engine(engine, dev_priv, id) {
		fault = GEN6_RING_FAULT_REG_READ(engine);
		if (fault & RING_FAULT_VALID) {
			DRM_DEBUG_DRIVER("Unexpected fault\n"
					 "\tAddr: 0x%08lx\n"
					 "\tAddress space: %s\n"
					 "\tSource ID: %d\n"
					 "\tType: %d\n",
					 fault & PAGE_MASK,
					 fault & RING_FAULT_GTTSEL_MASK ? "GGTT" : "PPGTT",
					 RING_FAULT_SRCID(fault),
					 RING_FAULT_FAULT_TYPE(fault));
		}
	}
}

static void gen8_check_faults(struct drm_i915_private *dev_priv)
{
	u32 fault = I915_READ(GEN8_RING_FAULT_REG);

	if (fault & RING_FAULT_VALID) {
		u32 fault_data0, fault_data1;
		u64 fault_addr;

		fault_data0 = I915_READ(GEN8_FAULT_TLB_DATA0);
		fault_data1 = I915_READ(GEN8_FAULT_TLB_DATA1);
		fault_addr = ((u64)(fault_data1 & FAULT_VA_HIGH_BITS) << 44) |
			     ((u64)fault_data0 << 12);

		DRM_DEBUG_DRIVER("Unexpected fault\n"
				 "\tAddr: 0x%08x_%08x\n"
				 "\tAddress space: %s\n"
				 "\tEngine ID: %d\n"
				 "\tSource ID: %d\n"
				 "\tType: %d\n",
				 upper_32_bits(fault_addr),
				 lower_32_bits(fault_addr),
				 fault_data1 & FAULT_GTT_SEL ? "GGTT" : "PPGTT",
				 GEN8_RING_FAULT_ENGINE_ID(fault),
				 RING_FAULT_SRCID(fault),
				 RING_FAULT_FAULT_TYPE(fault));
	}
}

void i915_check_and_clear_faults(struct drm_i915_private *i915)
{
	/* From GEN8 onwards we only have one 'All Engine Fault Register' */
	if (INTEL_GEN(i915) >= 8)
		gen8_check_faults(i915);
	else if (INTEL_GEN(i915) >= 6)
		gen6_check_faults(i915);
	else
		return;

	clear_error_registers(i915, ALL_ENGINES);
}

1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286
/**
 * i915_handle_error - handle a gpu error
 * @i915: i915 device private
 * @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.).
 */
void i915_handle_error(struct drm_i915_private *i915,
1287
		       intel_engine_mask_t engine_mask,
1288 1289 1290
		       unsigned long flags,
		       const char *fmt, ...)
{
1291
	struct i915_gpu_error *error = &i915->gpu_error;
1292 1293
	struct intel_engine_cs *engine;
	intel_wakeref_t wakeref;
1294
	intel_engine_mask_t tmp;
1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314
	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.
	 */
1315
	wakeref = intel_runtime_pm_get(&i915->runtime_pm);
1316

1317
	engine_mask &= INTEL_INFO(i915)->engine_mask;
1318 1319 1320

	if (flags & I915_ERROR_CAPTURE) {
		i915_capture_error_state(i915, engine_mask, msg);
1321
		clear_error_registers(i915, engine_mask);
1322 1323 1324 1325 1326 1327
	}

	/*
	 * Try engine reset when available. We fall back to full reset if
	 * single reset fails.
	 */
1328
	if (intel_has_reset_engine(i915) && !__i915_wedged(error)) {
1329 1330 1331
		for_each_engine_masked(engine, i915, engine_mask, tmp) {
			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1332
					     &error->flags))
1333 1334 1335
				continue;

			if (i915_reset_engine(engine, msg) == 0)
1336
				engine_mask &= ~engine->mask;
1337 1338

			clear_bit(I915_RESET_ENGINE + engine->id,
1339 1340
				  &error->flags);
			wake_up_bit(&error->flags,
1341 1342 1343 1344 1345 1346 1347 1348
				    I915_RESET_ENGINE + engine->id);
		}
	}

	if (!engine_mask)
		goto out;

	/* Full reset needs the mutex, stop any other user trying to do so. */
1349 1350 1351
	if (test_and_set_bit(I915_RESET_BACKOFF, &error->flags)) {
		wait_event(error->reset_queue,
			   !test_bit(I915_RESET_BACKOFF, &error->flags));
1352
		goto out; /* piggy-back on the other reset */
1353 1354
	}

1355 1356 1357
	/* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
	synchronize_rcu_expedited();

1358 1359 1360
	/* Prevent any other reset-engine attempt. */
	for_each_engine(engine, i915, tmp) {
		while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1361 1362
					&error->flags))
			wait_on_bit(&error->flags,
1363 1364 1365 1366 1367 1368 1369 1370
				    I915_RESET_ENGINE + engine->id,
				    TASK_UNINTERRUPTIBLE);
	}

	i915_reset_device(i915, engine_mask, msg);

	for_each_engine(engine, i915, tmp) {
		clear_bit(I915_RESET_ENGINE + engine->id,
1371
			  &error->flags);
1372 1373
	}

1374 1375
	clear_bit(I915_RESET_BACKOFF, &error->flags);
	wake_up_all(&error->reset_queue);
1376 1377

out:
1378
	intel_runtime_pm_put(&i915->runtime_pm, wakeref);
1379 1380
}

1381 1382 1383 1384 1385
int i915_reset_trylock(struct drm_i915_private *i915)
{
	struct i915_gpu_error *error = &i915->gpu_error;
	int srcu;

1386 1387 1388
	might_lock(&error->reset_backoff_srcu);
	might_sleep();

1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413
	rcu_read_lock();
	while (test_bit(I915_RESET_BACKOFF, &error->flags)) {
		rcu_read_unlock();

		if (wait_event_interruptible(error->reset_queue,
					     !test_bit(I915_RESET_BACKOFF,
						       &error->flags)))
			return -EINTR;

		rcu_read_lock();
	}
	srcu = srcu_read_lock(&error->reset_backoff_srcu);
	rcu_read_unlock();

	return srcu;
}

void i915_reset_unlock(struct drm_i915_private *i915, int tag)
__releases(&i915->gpu_error.reset_backoff_srcu)
{
	struct i915_gpu_error *error = &i915->gpu_error;

	srcu_read_unlock(&error->reset_backoff_srcu, tag);
}

1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438
int i915_terminally_wedged(struct drm_i915_private *i915)
{
	struct i915_gpu_error *error = &i915->gpu_error;

	might_sleep();

	if (!__i915_wedged(error))
		return 0;

	/* Reset still in progress? Maybe we will recover? */
	if (!test_bit(I915_RESET_BACKOFF, &error->flags))
		return -EIO;

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

	if (wait_event_interruptible(error->reset_queue,
				     !test_bit(I915_RESET_BACKOFF,
					       &error->flags)))
		return -EINTR;

	return __i915_wedged(error) ? -EIO : 0;
}

1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
static void i915_wedge_me(struct work_struct *work)
{
	struct i915_wedge_me *w = container_of(work, typeof(*w), work.work);

	dev_err(w->i915->drm.dev,
		"%s timed out, cancelling all in-flight rendering.\n",
		w->name);
	i915_gem_set_wedged(w->i915);
}

void __i915_init_wedge(struct i915_wedge_me *w,
		       struct drm_i915_private *i915,
		       long timeout,
		       const char *name)
{
	w->i915 = i915;
	w->name = name;

	INIT_DELAYED_WORK_ONSTACK(&w->work, i915_wedge_me);
	schedule_delayed_work(&w->work, timeout);
}

void __i915_fini_wedge(struct i915_wedge_me *w)
{
	cancel_delayed_work_sync(&w->work);
	destroy_delayed_work_on_stack(&w->work);
	w->i915 = NULL;
}
1467 1468 1469 1470

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