intel_reset.c 38.9 KB
Newer Older
C
Chris Wilson 已提交
1
// SPDX-License-Identifier: MIT
2 3 4 5 6
/*
 * Copyright © 2008-2018 Intel Corporation
 */

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

9
#include "display/intel_display_types.h"
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_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 28
#define RESET_MAX_RETRIES 3

29 30 31
/* XXX How to handle concurrent GGTT updates using tiling registers? */
#define RESET_UNDER_STOP_MACHINE 0

32 33 34 35 36 37 38 39 40 41
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);
}

42
static void skip_context(struct i915_request *rq)
43
{
44
	struct intel_context *hung_ctx = rq->context;
45

46 47 48
	list_for_each_entry_from_rcu(rq, &hung_ctx->timeline->requests, link) {
		if (!i915_request_is_active(rq))
			return;
49

50 51 52 53
		if (rq->context == hung_ctx) {
			i915_request_set_error_once(rq, -EIO);
			__i915_request_skip(rq);
		}
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 62 63 64
	unsigned int score;

	if (IS_ERR_OR_NULL(file_priv))
		return;
65

66 67
	score = 0;
	if (banned)
68 69 70 71 72 73 74 75 76
		score = I915_CLIENT_SCORE_CONTEXT_BAN;

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

77 78 79 80
		drm_dbg(&ctx->i915->drm,
			"client %s: gained %u ban score, now %u\n",
			ctx->name, score,
			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 88 89
	unsigned long prev_hang;
	bool banned;
	int i;
90

91 92 93 94 95
	if (intel_context_is_closed(rq->context)) {
		intel_context_set_banned(rq->context);
		return true;
	}

96 97 98 99 100
	rcu_read_lock();
	ctx = rcu_dereference(rq->context->gem_context);
	if (ctx && !kref_get_unless_zero(&ctx->ref))
		ctx = NULL;
	rcu_read_unlock();
101
	if (!ctx)
102
		return intel_context_is_banned(rq->context);
103

104 105
	atomic_inc(&ctx->guilty_count);

106
	/* Cool contexts are too cool to be banned! (Used for reset testing.) */
107 108 109 110
	if (!i915_gem_context_is_bannable(ctx)) {
		banned = false;
		goto out;
	}
111

112
	drm_notice(&ctx->i915->drm,
113 114 115
		   "%s context reset due to GPU hang\n",
		   ctx->name);

116 117 118 119 120 121 122 123 124 125
	/* 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;
126
	if (banned) {
127 128
		drm_dbg(&ctx->i915->drm, "context %s: guilty %d, banned\n",
			ctx->name, atomic_read(&ctx->guilty_count));
129
		intel_context_set_banned(rq->context);
130 131
	}

132
	client_mark_guilty(ctx, banned);
133

134 135
out:
	i915_gem_context_put(ctx);
136
	return banned;
137 138
}

139
static void mark_innocent(struct i915_request *rq)
140
{
141 142 143 144 145 146 147
	struct i915_gem_context *ctx;

	rcu_read_lock();
	ctx = rcu_dereference(rq->context->gem_context);
	if (ctx)
		atomic_inc(&ctx->active_count);
	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 158
		i915_request_set_error_once(rq, -EIO);
		__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 169 170 171 172 173 174 175
static bool i915_in_reset(struct pci_dev *pdev)
{
	u8 gdrst;

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

176
static int i915_do_reset(struct intel_gt *gt,
177
			 intel_engine_mask_t engine_mask,
178 179
			 unsigned int retry)
{
180
	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
181 182 183 184
	int err;

	/* Assert reset for at least 20 usec, and wait for acknowledgement. */
	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
185 186
	udelay(50);
	err = wait_for_atomic(i915_in_reset(pdev), 50);
187 188 189

	/* Clear the reset request. */
	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 195 196 197 198 199 200 201 202 203 204

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

205
static int g33_do_reset(struct intel_gt *gt,
206
			intel_engine_mask_t engine_mask,
207 208
			unsigned int retry)
{
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 218
			unsigned int retry)
{
219
	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
220
	struct intel_uncore *uncore = gt->uncore;
221 222 223
	int ret;

	/* WaVcpClkGateDisableForMediaReset:ctg,elk */
224 225
	rmw_set_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
226 227 228

	pci_write_config_byte(pdev, I915_GDRST,
			      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 233 234 235 236
		goto out;
	}

	pci_write_config_byte(pdev, I915_GDRST,
			      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 241 242 243 244 245
		goto out;
	}

out:
	pci_write_config_byte(pdev, I915_GDRST, 0);

246 247
	rmw_clear_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE);
	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
248 249 250 251

	return ret;
}

252 253
static int ilk_do_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask,
			unsigned int retry)
254
{
255
	struct intel_uncore *uncore = gt->uncore;
256 257
	int ret;

258 259 260
	intel_uncore_write_fw(uncore, ILK_GDSR,
			      ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
261 262 263
					   ILK_GRDOM_RESET_ENABLE, 0,
					   5000, 0,
					   NULL);
264
	if (ret) {
265
		GT_TRACE(gt, "Wait for render reset failed\n");
266 267 268
		goto out;
	}

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

out:
281 282
	intel_uncore_write_fw(uncore, ILK_GDSR, 0);
	intel_uncore_posting_read_fw(uncore, ILK_GDSR);
283 284 285 286
	return ret;
}

/* 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 291 292 293 294 295 296
	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
	 */
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 302 303 304
					   GEN6_GDRST, hw_domain_mask, 0,
					   500, 0,
					   NULL);
	if (err)
305 306 307
		GT_TRACE(gt,
			 "Wait for 0x%08x engines reset failed\n",
			 hw_domain_mask);
308 309 310 311

	return err;
}

312
static int gen6_reset_engines(struct intel_gt *gt,
313
			      intel_engine_mask_t engine_mask,
314 315
			      unsigned int retry)
{
316
	static const u32 hw_engine_mask[] = {
317 318 319 320 321
		[RCS0]  = GEN6_GRDOM_RENDER,
		[BCS0]  = GEN6_GRDOM_BLT,
		[VCS0]  = GEN6_GRDOM_MEDIA,
		[VCS1]  = GEN8_GRDOM_MEDIA2,
		[VECS0] = GEN6_GRDOM_VECS,
322
	};
323
	struct intel_engine_cs *engine;
324 325 326 327 328
	u32 hw_mask;

	if (engine_mask == ALL_ENGINES) {
		hw_mask = GEN6_GRDOM_FULL;
	} 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
}

A
Aditya Swarup 已提交
341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 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
static struct intel_engine_cs *find_sfc_paired_vecs_engine(struct intel_engine_cs *engine)
{
	int vecs_id;

	GEM_BUG_ON(engine->class != VIDEO_DECODE_CLASS);

	vecs_id = _VECS((engine->instance) / 2);

	return engine->gt->engine[vecs_id];
}

struct sfc_lock_data {
	i915_reg_t lock_reg;
	i915_reg_t ack_reg;
	i915_reg_t usage_reg;
	u32 lock_bit;
	u32 ack_bit;
	u32 usage_bit;
	u32 reset_bit;
};

static void get_sfc_forced_lock_data(struct intel_engine_cs *engine,
				     struct sfc_lock_data *sfc_lock)
{
	switch (engine->class) {
	default:
		MISSING_CASE(engine->class);
		fallthrough;
	case VIDEO_DECODE_CLASS:
		sfc_lock->lock_reg = GEN11_VCS_SFC_FORCED_LOCK(engine);
		sfc_lock->lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;

		sfc_lock->ack_reg = GEN11_VCS_SFC_LOCK_STATUS(engine);
		sfc_lock->ack_bit  = GEN11_VCS_SFC_LOCK_ACK_BIT;

		sfc_lock->usage_reg = GEN11_VCS_SFC_LOCK_STATUS(engine);
		sfc_lock->usage_bit = GEN11_VCS_SFC_USAGE_BIT;
		sfc_lock->reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);

		break;
	case VIDEO_ENHANCEMENT_CLASS:
		sfc_lock->lock_reg = GEN11_VECS_SFC_FORCED_LOCK(engine);
		sfc_lock->lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;

		sfc_lock->ack_reg = GEN11_VECS_SFC_LOCK_ACK(engine);
		sfc_lock->ack_bit  = GEN11_VECS_SFC_LOCK_ACK_BIT;

		sfc_lock->usage_reg = GEN11_VECS_SFC_USAGE(engine);
		sfc_lock->usage_bit = GEN11_VECS_SFC_USAGE_BIT;
		sfc_lock->reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);

		break;
	}
}

static int gen11_lock_sfc(struct intel_engine_cs *engine,
			  u32 *reset_mask,
			  u32 *unlock_mask)
399
{
400
	struct intel_uncore *uncore = engine->uncore;
401
	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
A
Aditya Swarup 已提交
402 403
	struct sfc_lock_data sfc_lock;
	bool lock_obtained, lock_to_other = false;
404
	int ret;
405 406 407 408 409 410

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

A
Aditya Swarup 已提交
411 412 413
		fallthrough;
	case VIDEO_ENHANCEMENT_CLASS:
		get_sfc_forced_lock_data(engine, &sfc_lock);
414 415

		break;
A
Aditya Swarup 已提交
416 417 418
	default:
		return 0;
	}
419

A
Aditya Swarup 已提交
420 421
	if (!(intel_uncore_read_fw(uncore, sfc_lock.usage_reg) & sfc_lock.usage_bit)) {
		struct intel_engine_cs *paired_vecs;
422

A
Aditya Swarup 已提交
423
		if (engine->class != VIDEO_DECODE_CLASS ||
424
		    GRAPHICS_VER(engine->i915) != 12)
A
Aditya Swarup 已提交
425
			return 0;
426

A
Aditya Swarup 已提交
427 428 429 430 431 432 433 434 435 436 437
		/*
		 * Wa_14010733141
		 *
		 * If the VCS-MFX isn't using the SFC, we also need to check
		 * whether VCS-HCP is using it.  If so, we need to issue a *VE*
		 * forced lock on the VE engine that shares the same SFC.
		 */
		if (!(intel_uncore_read_fw(uncore,
					   GEN12_HCP_SFC_LOCK_STATUS(engine)) &
		      GEN12_HCP_SFC_USAGE_BIT))
			return 0;
438

A
Aditya Swarup 已提交
439 440 441 442 443 444
		paired_vecs = find_sfc_paired_vecs_engine(engine);
		get_sfc_forced_lock_data(paired_vecs, &sfc_lock);
		lock_to_other = true;
		*unlock_mask |= paired_vecs->mask;
	} else {
		*unlock_mask |= engine->mask;
445 446 447
	}

	/*
A
Aditya Swarup 已提交
448
	 * If the engine is using an SFC, tell the engine that a software reset
449 450 451 452
	 * is going to happen. The engine will then try to force lock the SFC.
	 * 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).
453
	 */
A
Aditya Swarup 已提交
454
	rmw_set_fw(uncore, sfc_lock.lock_reg, sfc_lock.lock_bit);
455

456
	ret = __intel_wait_for_register_fw(uncore,
A
Aditya Swarup 已提交
457 458 459
					   sfc_lock.ack_reg,
					   sfc_lock.ack_bit,
					   sfc_lock.ack_bit,
460 461
					   1000, 0, NULL);

A
Aditya Swarup 已提交
462 463 464 465 466 467 468 469 470 471 472 473 474 475 476
	/*
	 * Was the SFC released while we were trying to lock it?
	 *
	 * We should reset both the engine and the SFC if:
	 *  - We were locking the SFC to this engine and the lock succeeded
	 *       OR
	 *  - We were locking the SFC to a different engine (Wa_14010733141)
	 *    but the SFC was released before the lock was obtained.
	 *
	 * Otherwise we need only reset the engine by itself and we can
	 * leave the SFC alone.
	 */
	lock_obtained = (intel_uncore_read_fw(uncore, sfc_lock.usage_reg) &
			sfc_lock.usage_bit) != 0;
	if (lock_obtained == lock_to_other)
477 478
		return 0;

479
	if (ret) {
480
		ENGINE_TRACE(engine, "Wait for SFC forced lock ack failed\n");
481 482
		return ret;
	}
483

A
Aditya Swarup 已提交
484
	*reset_mask |= sfc_lock.reset_bit;
485 486 487
	return 0;
}

488
static void gen11_unlock_sfc(struct intel_engine_cs *engine)
489
{
490
	struct intel_uncore *uncore = engine->uncore;
491
	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
A
Aditya Swarup 已提交
492
	struct sfc_lock_data sfc_lock = {};
493

A
Aditya Swarup 已提交
494 495 496
	if (engine->class != VIDEO_DECODE_CLASS &&
	    engine->class != VIDEO_ENHANCEMENT_CLASS)
		return;
497

A
Aditya Swarup 已提交
498 499
	if (engine->class == VIDEO_DECODE_CLASS &&
	    (BIT(engine->instance) & vdbox_sfc_access) == 0)
500 501
		return;

A
Aditya Swarup 已提交
502 503 504
	get_sfc_forced_lock_data(engine, &sfc_lock);

	rmw_clear_fw(uncore, sfc_lock.lock_reg, sfc_lock.lock_bit);
505 506
}

507
static int gen11_reset_engines(struct intel_gt *gt,
508
			       intel_engine_mask_t engine_mask,
509 510
			       unsigned int retry)
{
511
	static const u32 hw_engine_mask[] = {
512 513 514 515 516 517
		[RCS0]  = GEN11_GRDOM_RENDER,
		[BCS0]  = GEN11_GRDOM_BLT,
		[VCS0]  = GEN11_GRDOM_MEDIA,
		[VCS1]  = GEN11_GRDOM_MEDIA2,
		[VCS2]  = GEN11_GRDOM_MEDIA3,
		[VCS3]  = GEN11_GRDOM_MEDIA4,
518 519 520 521
		[VCS4]  = GEN11_GRDOM_MEDIA5,
		[VCS5]  = GEN11_GRDOM_MEDIA6,
		[VCS6]  = GEN11_GRDOM_MEDIA7,
		[VCS7]  = GEN11_GRDOM_MEDIA8,
522 523
		[VECS0] = GEN11_GRDOM_VECS,
		[VECS1] = GEN11_GRDOM_VECS2,
524 525
		[VECS2] = GEN11_GRDOM_VECS3,
		[VECS3] = GEN11_GRDOM_VECS4,
526 527
	};
	struct intel_engine_cs *engine;
528
	intel_engine_mask_t tmp;
A
Aditya Swarup 已提交
529
	u32 reset_mask, unlock_mask = 0;
530 531 532
	int ret;

	if (engine_mask == ALL_ENGINES) {
A
Aditya Swarup 已提交
533
		reset_mask = GEN11_GRDOM_FULL;
534
	} else {
A
Aditya Swarup 已提交
535
		reset_mask = 0;
536
		for_each_engine_masked(engine, gt, engine_mask, tmp) {
537
			GEM_BUG_ON(engine->id >= ARRAY_SIZE(hw_engine_mask));
A
Aditya Swarup 已提交
538 539
			reset_mask |= hw_engine_mask[engine->id];
			ret = gen11_lock_sfc(engine, &reset_mask, &unlock_mask);
540 541
			if (ret)
				goto sfc_unlock;
542 543 544
		}
	}

A
Aditya Swarup 已提交
545
	ret = gen6_hw_domain_reset(gt, reset_mask);
546

547 548 549 550 551 552
sfc_unlock:
	/*
	 * We unlock the SFC based on the lock status and not the result of
	 * gen11_lock_sfc to make sure that we clean properly if something
	 * wrong happened during the lock (e.g. lock acquired after timeout
	 * expiration).
A
Aditya Swarup 已提交
553 554 555 556 557
	 *
	 * Due to Wa_14010733141, we may have locked an SFC to an engine that
	 * wasn't being reset.  So instead of calling gen11_unlock_sfc()
	 * on engine_mask, we instead call it on the mask of engines that our
	 * gen11_lock_sfc() calls told us actually had locks attempted.
558
	 */
A
Aditya Swarup 已提交
559 560
	for_each_engine_masked(engine, gt, unlock_mask, tmp)
		gen11_unlock_sfc(engine);
561 562 563 564 565 566

	return ret;
}

static int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
{
567
	struct intel_uncore *uncore = engine->uncore;
568 569
	const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
	u32 request, mask, ack;
570 571
	int ret;

572 573 574
	if (I915_SELFTEST_ONLY(should_fail(&engine->reset_timeout, 1)))
		return -ETIMEDOUT;

575
	ack = intel_uncore_read_fw(uncore, reg);
576 577 578 579 580 581 582 583 584 585 586
	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)) {
587 588 589 590 591 592
		request = RESET_CTL_REQUEST_RESET;
		mask = RESET_CTL_READY_TO_RESET;
		ack = RESET_CTL_READY_TO_RESET;
	} else {
		return 0;
	}
593

594 595 596
	intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
	ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
					   700, 0, NULL);
597
	if (ret)
598 599 600 601
		drm_err(&engine->i915->drm,
			"%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
			engine->name, request,
			intel_uncore_read_fw(uncore, reg));
602 603 604 605 606 607

	return ret;
}

static void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
{
608 609 610
	intel_uncore_write_fw(engine->uncore,
			      RING_RESET_CTL(engine->mmio_base),
			      _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
611 612
}

613
static int gen8_reset_engines(struct intel_gt *gt,
614
			      intel_engine_mask_t engine_mask,
615 616 617 618
			      unsigned int retry)
{
	struct intel_engine_cs *engine;
	const bool reset_non_ready = retry >= 1;
619
	intel_engine_mask_t tmp;
620 621
	int ret;

622
	for_each_engine_masked(engine, gt, engine_mask, tmp) {
623 624 625 626 627 628 629 630 631 632 633 634 635 636 637
		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
638
		 * stop_engines() we have before the reset.
639 640 641
		 */
	}

642
	if (GRAPHICS_VER(gt->i915) >= 11)
643
		ret = gen11_reset_engines(gt, engine_mask, retry);
644
	else
645
		ret = gen6_reset_engines(gt, engine_mask, retry);
646 647

skip_reset:
648
	for_each_engine_masked(engine, gt, engine_mask, tmp)
649 650 651 652 653
		gen8_engine_reset_cancel(engine);

	return ret;
}

654 655 656 657 658 659 660
static int mock_reset(struct intel_gt *gt,
		      intel_engine_mask_t mask,
		      unsigned int retry)
{
	return 0;
}

661
typedef int (*reset_func)(struct intel_gt *,
662
			  intel_engine_mask_t engine_mask,
663 664
			  unsigned int retry);

665
static reset_func intel_get_gpu_reset(const struct intel_gt *gt)
666
{
667 668
	struct drm_i915_private *i915 = gt->i915;

669 670
	if (is_mock_gt(gt))
		return mock_reset;
671
	else if (GRAPHICS_VER(i915) >= 8)
672
		return gen8_reset_engines;
673
	else if (GRAPHICS_VER(i915) >= 6)
674
		return gen6_reset_engines;
675
	else if (GRAPHICS_VER(i915) >= 5)
676
		return ilk_do_reset;
677 678 679 680
	else if (IS_G4X(i915))
		return g4x_do_reset;
	else if (IS_G33(i915) || IS_PINEVIEW(i915))
		return g33_do_reset;
681
	else if (GRAPHICS_VER(i915) >= 3)
682 683 684 685 686
		return i915_do_reset;
	else
		return NULL;
}

687
int __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask)
688
{
689 690 691
	const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
	reset_func reset;
	int ret = -ETIMEDOUT;
692 693
	int retry;

694
	reset = intel_get_gpu_reset(gt);
695 696
	if (!reset)
		return -ENODEV;
697 698 699 700 701

	/*
	 * If the power well sleeps during the reset, the reset
	 * request may be dropped and never completes (causing -EIO).
	 */
702
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
703
	for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
704
		GT_TRACE(gt, "engine_mask=%x\n", engine_mask);
705
		preempt_disable();
706
		ret = reset(gt, engine_mask, retry);
707
		preempt_enable();
708
	}
709
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
710 711 712 713

	return ret;
}

714
bool intel_has_gpu_reset(const struct intel_gt *gt)
715
{
716
	if (!gt->i915->params.reset)
717 718
		return NULL;

719
	return intel_get_gpu_reset(gt);
720 721
}

722
bool intel_has_reset_engine(const struct intel_gt *gt)
723
{
724
	if (gt->i915->params.reset < 2)
725 726 727
		return false;

	return INTEL_INFO(gt->i915)->has_reset_engine;
728 729
}

730
int intel_reset_guc(struct intel_gt *gt)
731 732
{
	u32 guc_domain =
733
		GRAPHICS_VER(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
734 735
	int ret;

736
	GEM_BUG_ON(!HAS_GT_UC(gt->i915));
737

738 739 740
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
	ret = gen6_hw_domain_reset(gt, guc_domain);
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
741 742 743 744 745 746 747 748

	return ret;
}

/*
 * Ensure irq handler finishes, and not run again.
 * Also return the active request so that we only search for it once.
 */
749
static void reset_prepare_engine(struct intel_engine_cs *engine)
750 751 752 753 754 755 756 757
{
	/*
	 * 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.
	 */
758
	intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
759 760
	if (engine->reset.prepare)
		engine->reset.prepare(engine);
761 762
}

763
static void revoke_mmaps(struct intel_gt *gt)
764 765 766
{
	int i;

767
	for (i = 0; i < gt->ggtt->num_fences; i++) {
768 769 770 771
		struct drm_vma_offset_node *node;
		struct i915_vma *vma;
		u64 vma_offset;

772
		vma = READ_ONCE(gt->ggtt->fence_regs[i].vma);
773 774 775 776 777 778
		if (!vma)
			continue;

		if (!i915_vma_has_userfault(vma))
			continue;

779
		GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
780 781 782 783 784

		if (!vma->mmo)
			continue;

		node = &vma->mmo->vma_node;
785
		vma_offset = vma->ggtt_view.partial.offset << PAGE_SHIFT;
786

787
		unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
788 789 790 791 792 793
				    drm_vma_node_offset_addr(node) + vma_offset,
				    vma->size,
				    1);
	}
}

794
static intel_engine_mask_t reset_prepare(struct intel_gt *gt)
795 796
{
	struct intel_engine_cs *engine;
797
	intel_engine_mask_t awake = 0;
798 799
	enum intel_engine_id id;

800
	for_each_engine(engine, gt, id) {
801 802
		if (intel_engine_pm_get_if_awake(engine))
			awake |= engine->mask;
803
		reset_prepare_engine(engine);
804
	}
805

806
	intel_uc_reset_prepare(&gt->uc);
807 808

	return awake;
809 810
}

811
static void gt_revoke(struct intel_gt *gt)
812
{
813
	revoke_mmaps(gt);
814 815
}

816
static int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
817
{
818 819 820 821
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int err;

822
	/*
823 824
	 * Everything depends on having the GTT running, so we need to start
	 * there.
825
	 */
826
	err = i915_ggtt_enable_hw(gt->i915);
827 828
	if (err)
		return err;
829

830
	local_bh_disable();
831
	for_each_engine(engine, gt, id)
832
		__intel_engine_reset(engine, stalled_mask & engine->mask);
833
	local_bh_enable();
834

835
	intel_ggtt_restore_fences(gt->ggtt);
836

837
	return err;
838 839
}

840
static void reset_finish_engine(struct intel_engine_cs *engine)
841
{
842 843
	if (engine->reset.finish)
		engine->reset.finish(engine);
844
	intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
845

846
	intel_engine_signal_breadcrumbs(engine);
847 848
}

849
static void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
850 851 852 853
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;

854
	for_each_engine(engine, gt, id) {
855
		reset_finish_engine(engine);
856 857
		if (awake & engine->mask)
			intel_engine_pm_put(engine);
858
	}
859 860 861 862
}

static void nop_submit_request(struct i915_request *request)
{
863
	RQ_TRACE(request, "-EIO\n");
864

865 866 867 868
	request = i915_request_mark_eio(request);
	if (request) {
		i915_request_submit(request);
		intel_engine_signal_breadcrumbs(request->engine);
869

870 871
		i915_request_put(request);
	}
872 873
}

874
static void __intel_gt_set_wedged(struct intel_gt *gt)
875 876
{
	struct intel_engine_cs *engine;
877
	intel_engine_mask_t awake;
878 879
	enum intel_engine_id id;

880
	if (test_bit(I915_WEDGED, &gt->reset.flags))
881 882
		return;

883
	GT_TRACE(gt, "start\n");
884 885 886 887 888 889

	/*
	 * 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).
	 */
890
	awake = reset_prepare(gt);
891

892
	/* Even if the GPU reset fails, it should still stop the engines */
893 894
	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
		__intel_gt_reset(gt, ALL_ENGINES);
895

896
	for_each_engine(engine, gt, id)
897 898 899 900 901 902 903
		engine->submit_request = nop_submit_request;

	/*
	 * 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 已提交
904
	synchronize_rcu_expedited();
905
	set_bit(I915_WEDGED, &gt->reset.flags);
906 907

	/* Mark all executing requests as skipped */
908
	local_bh_disable();
909
	for_each_engine(engine, gt, id)
910 911
		if (engine->reset.cancel)
			engine->reset.cancel(engine);
912
	local_bh_enable();
913

914
	reset_finish(gt, awake);
915

916
	GT_TRACE(gt, "end\n");
917
}
918

919
void intel_gt_set_wedged(struct intel_gt *gt)
920
{
921
	intel_wakeref_t wakeref;
922

923 924 925 926
	if (test_bit(I915_WEDGED, &gt->reset.flags))
		return;

	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
927
	mutex_lock(&gt->reset.mutex);
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944

	if (GEM_SHOW_DEBUG()) {
		struct drm_printer p = drm_debug_printer(__func__);
		struct intel_engine_cs *engine;
		enum intel_engine_id id;

		drm_printf(&p, "called from %pS\n", (void *)_RET_IP_);
		for_each_engine(engine, gt, id) {
			if (intel_engine_is_idle(engine))
				continue;

			intel_engine_dump(engine, &p, "%s\n", engine->name);
		}
	}

	__intel_gt_set_wedged(gt);

945
	mutex_unlock(&gt->reset.mutex);
946
	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
947 948
}

949
static bool __intel_gt_unset_wedged(struct intel_gt *gt)
950
{
951
	struct intel_gt_timelines *timelines = &gt->timelines;
952
	struct intel_timeline *tl;
953
	bool ok;
954

955
	if (!test_bit(I915_WEDGED, &gt->reset.flags))
956 957
		return true;

958
	/* Never fully initialised, recovery impossible */
959
	if (intel_gt_has_unrecoverable_error(gt))
960 961
		return false;

962
	GT_TRACE(gt, "start\n");
963 964 965 966 967 968 969 970 971 972 973

	/*
	 * 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.
	 */
974
	spin_lock(&timelines->lock);
975
	list_for_each_entry(tl, &timelines->active_list, link) {
976
		struct dma_fence *fence;
977

978 979
		fence = i915_active_fence_get(&tl->last_request);
		if (!fence)
980 981
			continue;

982
		spin_unlock(&timelines->lock);
983

984
		/*
985 986 987 988 989
		 * 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.
990
		 */
991 992
		dma_fence_default_wait(fence, false, MAX_SCHEDULE_TIMEOUT);
		dma_fence_put(fence);
993 994

		/* Restart iteration after droping lock */
995
		spin_lock(&timelines->lock);
996
		tl = list_entry(&timelines->active_list, typeof(*tl), link);
997
	}
998
	spin_unlock(&timelines->lock);
999

1000 1001 1002 1003
	/* We must reset pending GPU events before restoring our submission */
	ok = !HAS_EXECLISTS(gt->i915); /* XXX better agnosticism desired */
	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
		ok = __intel_gt_reset(gt, ALL_ENGINES) == 0;
1004 1005 1006 1007 1008
	if (!ok) {
		/*
		 * Warn CI about the unrecoverable wedged condition.
		 * Time for a reboot.
		 */
1009
		add_taint_for_CI(gt->i915, TAINT_WARN);
1010
		return false;
1011
	}
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021

	/*
	 * 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().
	 */
1022
	intel_engines_reset_default_submission(gt);
1023

1024
	GT_TRACE(gt, "end\n");
1025 1026

	smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
1027
	clear_bit(I915_WEDGED, &gt->reset.flags);
1028 1029

	return true;
1030 1031
}

1032
bool intel_gt_unset_wedged(struct intel_gt *gt)
1033 1034 1035
{
	bool result;

1036 1037 1038
	mutex_lock(&gt->reset.mutex);
	result = __intel_gt_unset_wedged(gt);
	mutex_unlock(&gt->reset.mutex);
1039 1040 1041 1042

	return result;
}

1043
static int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
1044 1045 1046
{
	int err, i;

1047
	err = __intel_gt_reset(gt, ALL_ENGINES);
1048
	for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
1049
		msleep(10 * (i + 1));
1050
		err = __intel_gt_reset(gt, ALL_ENGINES);
1051
	}
1052 1053
	if (err)
		return err;
1054

1055
	return gt_reset(gt, stalled_mask);
1056 1057
}

1058
static int resume(struct intel_gt *gt)
1059 1060 1061 1062 1063
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
	int ret;

1064
	for_each_engine(engine, gt, id) {
1065
		ret = intel_engine_resume(engine);
1066 1067 1068 1069 1070 1071 1072
		if (ret)
			return ret;
	}

	return 0;
}

1073
/**
1074 1075
 * intel_gt_reset - reset chip after a hang
 * @gt: #intel_gt to reset
1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089
 * @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
 */
1090 1091 1092
void intel_gt_reset(struct intel_gt *gt,
		    intel_engine_mask_t stalled_mask,
		    const char *reason)
1093
{
1094
	intel_engine_mask_t awake;
1095 1096
	int ret;

1097
	GT_TRACE(gt, "flags=%lx\n", gt->reset.flags);
1098 1099

	might_sleep();
1100
	GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
1101 1102 1103 1104 1105 1106 1107

	/*
	 * FIXME: Revoking cpu mmap ptes cannot be done from a dma_fence
	 * critical section like gpu reset.
	 */
	gt_revoke(gt);

1108
	mutex_lock(&gt->reset.mutex);
1109 1110

	/* Clear any previous failed attempts at recovery. Time to try again. */
1111
	if (!__intel_gt_unset_wedged(gt))
1112
		goto unlock;
1113 1114

	if (reason)
1115
		drm_notice(&gt->i915->drm,
1116 1117
			   "Resetting chip for %s\n", reason);
	atomic_inc(&gt->i915->gpu_error.reset_count);
1118

1119
	awake = reset_prepare(gt);
1120

1121
	if (!intel_has_gpu_reset(gt)) {
1122
		if (gt->i915->params.reset)
1123
			drm_err(&gt->i915->drm, "GPU reset not supported\n");
1124
		else
1125
			drm_dbg(&gt->i915->drm, "GPU reset disabled\n");
1126 1127 1128
		goto error;
	}

1129 1130
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
		intel_runtime_pm_disable_interrupts(gt->i915);
1131

1132
	if (do_reset(gt, stalled_mask)) {
1133
		drm_err(&gt->i915->drm, "Failed to reset chip\n");
1134 1135 1136
		goto taint;
	}

1137 1138
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
		intel_runtime_pm_enable_interrupts(gt->i915);
1139

1140
	intel_overlay_reset(gt->i915);
1141 1142 1143 1144 1145 1146 1147 1148 1149

	/*
	 * 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).
	 */
1150
	ret = intel_gt_init_hw(gt);
1151
	if (ret) {
1152 1153 1154
		drm_err(&gt->i915->drm,
			"Failed to initialise HW following reset (%d)\n",
			ret);
1155
		goto taint;
1156 1157
	}

1158
	ret = resume(gt);
1159 1160 1161
	if (ret)
		goto taint;

1162
finish:
1163
	reset_finish(gt, awake);
1164
unlock:
1165
	mutex_unlock(&gt->reset.mutex);
1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180
	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!
	 */
1181
	add_taint_for_CI(gt->i915, TAINT_WARN);
1182
error:
1183
	__intel_gt_set_wedged(gt);
1184 1185 1186
	goto finish;
}

C
Chris Wilson 已提交
1187
static int intel_gt_reset_engine(struct intel_engine_cs *engine)
1188
{
1189
	return __intel_gt_reset(engine->gt, engine->mask);
1190 1191
}

1192
int __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
1193
{
1194
	struct intel_gt *gt = engine->gt;
1195 1196
	int ret;

1197
	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
1198
	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
1199

1200
	if (!intel_engine_pm_get_if_awake(engine))
1201 1202
		return 0;

1203
	reset_prepare_engine(engine);
1204 1205

	if (msg)
1206
		drm_notice(&engine->i915->drm,
1207
			   "Resetting %s for %s\n", engine->name, msg);
1208
	atomic_inc(&engine->i915->gpu_error.reset_engine_count[engine->uabi_class]);
1209

1210
	if (intel_engine_uses_guc(engine))
1211
		ret = intel_guc_reset_engine(&engine->gt->uc.guc, engine);
1212 1213
	else
		ret = intel_gt_reset_engine(engine);
1214 1215
	if (ret) {
		/* If we fail here, we expect to fallback to a global reset */
1216
		ENGINE_TRACE(engine, "Failed to reset, err: %d\n", ret);
1217 1218 1219 1220 1221 1222 1223 1224
		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.
	 */
1225
	__intel_engine_reset(engine, true);
1226 1227 1228 1229 1230 1231

	/*
	 * 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.
	 */
1232
	ret = intel_engine_resume(engine);
1233 1234 1235 1236

out:
	intel_engine_cancel_stop_cs(engine);
	reset_finish_engine(engine);
1237
	intel_engine_pm_put_async(engine);
1238 1239 1240
	return ret;
}

1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264
/**
 * intel_engine_reset - reset GPU engine to recover from a hang
 * @engine: engine to reset
 * @msg: reason for GPU reset; or NULL for no drm_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 intel_engine_reset(struct intel_engine_cs *engine, const char *msg)
{
	int err;

	local_bh_disable();
	err = __intel_engine_reset_bh(engine, msg);
	local_bh_enable();

	return err;
}

1265 1266 1267
static void intel_gt_reset_global(struct intel_gt *gt,
				  u32 engine_mask,
				  const char *reason)
1268
{
1269
	struct kobject *kobj = &gt->i915->drm.primary->kdev->kobj;
1270 1271 1272
	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 };
1273
	struct intel_wedge_me w;
1274 1275 1276

	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);

1277
	GT_TRACE(gt, "resetting chip, engines=%x\n", engine_mask);
1278 1279 1280
	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);

	/* Use a watchdog to ensure that our reset completes */
1281
	intel_wedge_on_timeout(&w, gt, 5 * HZ) {
1282
		intel_display_prepare_reset(gt->i915);
1283

1284
		/* Flush everyone using a resource about to be clobbered */
1285
		synchronize_srcu_expedited(&gt->reset.backoff_srcu);
1286

1287
		intel_gt_reset(gt, engine_mask, reason);
1288

1289
		intel_display_finish_reset(gt->i915);
1290 1291
	}

1292
	if (!test_bit(I915_WEDGED, &gt->reset.flags))
1293 1294 1295 1296
		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
}

/**
1297 1298
 * intel_gt_handle_error - handle a gpu error
 * @gt: the intel_gt
1299 1300 1301 1302 1303 1304 1305 1306 1307 1308
 * @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.).
 */
1309 1310 1311 1312
void intel_gt_handle_error(struct intel_gt *gt,
			   intel_engine_mask_t engine_mask,
			   unsigned long flags,
			   const char *fmt, ...)
1313 1314 1315
{
	struct intel_engine_cs *engine;
	intel_wakeref_t wakeref;
1316
	intel_engine_mask_t tmp;
1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
	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.
	 */
1337
	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
1338

1339
	engine_mask &= gt->info.engine_mask;
1340 1341

	if (flags & I915_ERROR_CAPTURE) {
1342
		i915_capture_error_state(gt, engine_mask);
1343
		intel_gt_clear_error_registers(gt, engine_mask);
1344 1345 1346 1347 1348 1349
	}

	/*
	 * Try engine reset when available. We fall back to full reset if
	 * single reset fails.
	 */
1350
	if (intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
1351
		local_bh_disable();
1352
		for_each_engine_masked(engine, gt, engine_mask, tmp) {
1353 1354
			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1355
					     &gt->reset.flags))
1356 1357
				continue;

1358
			if (__intel_engine_reset_bh(engine, msg) == 0)
1359
				engine_mask &= ~engine->mask;
1360

1361 1362
			clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
					      &gt->reset.flags);
1363
		}
1364
		local_bh_enable();
1365 1366 1367 1368 1369 1370
	}

	if (!engine_mask)
		goto out;

	/* Full reset needs the mutex, stop any other user trying to do so. */
1371 1372 1373
	if (test_and_set_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
		wait_event(gt->reset.queue,
			   !test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
1374
		goto out; /* piggy-back on the other reset */
1375 1376
	}

1377 1378 1379
	/* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
	synchronize_rcu_expedited();

1380
	/* Prevent any other reset-engine attempt. */
1381
	for_each_engine(engine, gt, tmp) {
1382
		while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
1383 1384
					&gt->reset.flags))
			wait_on_bit(&gt->reset.flags,
1385 1386 1387 1388
				    I915_RESET_ENGINE + engine->id,
				    TASK_UNINTERRUPTIBLE);
	}

1389
	intel_gt_reset_global(gt, engine_mask, msg);
1390

1391
	for_each_engine(engine, gt, tmp)
1392 1393 1394 1395 1396
		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);
1397 1398

out:
1399
	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
1400 1401
}

1402
int intel_gt_reset_trylock(struct intel_gt *gt, int *srcu)
1403
{
1404
	might_lock(&gt->reset.backoff_srcu);
1405 1406
	might_sleep();

1407
	rcu_read_lock();
1408
	while (test_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
1409 1410
		rcu_read_unlock();

1411
		if (wait_event_interruptible(gt->reset.queue,
1412
					     !test_bit(I915_RESET_BACKOFF,
1413
						       &gt->reset.flags)))
1414 1415 1416 1417
			return -EINTR;

		rcu_read_lock();
	}
1418
	*srcu = srcu_read_lock(&gt->reset.backoff_srcu);
1419 1420
	rcu_read_unlock();

1421
	return 0;
1422 1423
}

1424 1425
void intel_gt_reset_unlock(struct intel_gt *gt, int tag)
__releases(&gt->reset.backoff_srcu)
1426
{
1427
	srcu_read_unlock(&gt->reset.backoff_srcu, tag);
1428 1429
}

1430
int intel_gt_terminally_wedged(struct intel_gt *gt)
1431 1432 1433
{
	might_sleep();

1434
	if (!intel_gt_is_wedged(gt))
1435 1436
		return 0;

1437
	if (intel_gt_has_unrecoverable_error(gt))
1438 1439
		return -EIO;

1440
	/* Reset still in progress? Maybe we will recover? */
1441
	if (wait_event_interruptible(gt->reset.queue,
1442
				     !test_bit(I915_RESET_BACKOFF,
1443
					       &gt->reset.flags)))
1444 1445
		return -EINTR;

1446 1447 1448
	return intel_gt_is_wedged(gt) ? -EIO : 0;
}

1449 1450 1451 1452 1453 1454
void intel_gt_set_wedged_on_init(struct intel_gt *gt)
{
	BUILD_BUG_ON(I915_RESET_ENGINE + I915_NUM_ENGINES >
		     I915_WEDGED_ON_INIT);
	intel_gt_set_wedged(gt);
	set_bit(I915_WEDGED_ON_INIT, &gt->reset.flags);
1455 1456

	/* Wedged on init is non-recoverable */
1457
	add_taint_for_CI(gt->i915, TAINT_WARN);
1458 1459 1460 1461 1462 1463
}

void intel_gt_set_wedged_on_fini(struct intel_gt *gt)
{
	intel_gt_set_wedged(gt);
	set_bit(I915_WEDGED_ON_FINI, &gt->reset.flags);
1464
	intel_gt_retire_requests(gt); /* cleanup any wedged requests */
1465 1466
}

1467 1468 1469 1470 1471
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);
1472

1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
	/*
	 * While undesirable to wait inside the shrinker, complain anyway.
	 *
	 * If we have to wait during shrinking, we guarantee forward progress
	 * by forcing the reset. Therefore during the reset we must not
	 * re-enter the shrinker. By declaring that we take the reset mutex
	 * within the shrinker, we forbid ourselves from performing any
	 * fs-reclaim or taking related locks during reset.
	 */
	i915_gem_shrinker_taints_mutex(gt->i915, &gt->reset.mutex);

1484 1485
	/* no GPU until we are ready! */
	__set_bit(I915_WEDGED, &gt->reset.flags);
1486 1487 1488 1489 1490
}

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

1493
static void intel_wedge_me(struct work_struct *work)
1494
{
1495
	struct intel_wedge_me *w = container_of(work, typeof(*w), work.work);
1496

1497
	drm_err(&w->gt->i915->drm,
1498 1499
		"%s timed out, cancelling all in-flight rendering.\n",
		w->name);
1500
	intel_gt_set_wedged(w->gt);
1501 1502
}

1503 1504 1505 1506
void __intel_init_wedge(struct intel_wedge_me *w,
			struct intel_gt *gt,
			long timeout,
			const char *name)
1507
{
1508
	w->gt = gt;
1509 1510
	w->name = name;

1511
	INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me);
1512 1513 1514
	schedule_delayed_work(&w->work, timeout);
}

1515
void __intel_fini_wedge(struct intel_wedge_me *w)
1516 1517 1518
{
	cancel_delayed_work_sync(&w->work);
	destroy_delayed_work_on_stack(&w->work);
1519
	w->gt = NULL;
1520
}
1521 1522 1523

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