162306a36Sopenharmony_ci// SPDX-License-Identifier: MIT
262306a36Sopenharmony_ci/*
362306a36Sopenharmony_ci * Copyright © 2008-2018 Intel Corporation
462306a36Sopenharmony_ci */
562306a36Sopenharmony_ci
662306a36Sopenharmony_ci#include <linux/sched/mm.h>
762306a36Sopenharmony_ci#include <linux/stop_machine.h>
862306a36Sopenharmony_ci#include <linux/string_helpers.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include "display/intel_display_reset.h"
1162306a36Sopenharmony_ci#include "display/intel_overlay.h"
1262306a36Sopenharmony_ci
1362306a36Sopenharmony_ci#include "gem/i915_gem_context.h"
1462306a36Sopenharmony_ci
1562306a36Sopenharmony_ci#include "gt/intel_gt_regs.h"
1662306a36Sopenharmony_ci
1762306a36Sopenharmony_ci#include "gt/uc/intel_gsc_fw.h"
1862306a36Sopenharmony_ci
1962306a36Sopenharmony_ci#include "i915_drv.h"
2062306a36Sopenharmony_ci#include "i915_file_private.h"
2162306a36Sopenharmony_ci#include "i915_gpu_error.h"
2262306a36Sopenharmony_ci#include "i915_irq.h"
2362306a36Sopenharmony_ci#include "i915_reg.h"
2462306a36Sopenharmony_ci#include "intel_breadcrumbs.h"
2562306a36Sopenharmony_ci#include "intel_engine_pm.h"
2662306a36Sopenharmony_ci#include "intel_engine_regs.h"
2762306a36Sopenharmony_ci#include "intel_gt.h"
2862306a36Sopenharmony_ci#include "intel_gt_pm.h"
2962306a36Sopenharmony_ci#include "intel_gt_requests.h"
3062306a36Sopenharmony_ci#include "intel_mchbar_regs.h"
3162306a36Sopenharmony_ci#include "intel_pci_config.h"
3262306a36Sopenharmony_ci#include "intel_reset.h"
3362306a36Sopenharmony_ci
3462306a36Sopenharmony_ci#include "uc/intel_guc.h"
3562306a36Sopenharmony_ci
3662306a36Sopenharmony_ci#define RESET_MAX_RETRIES 3
3762306a36Sopenharmony_ci
3862306a36Sopenharmony_cistatic void client_mark_guilty(struct i915_gem_context *ctx, bool banned)
3962306a36Sopenharmony_ci{
4062306a36Sopenharmony_ci	struct drm_i915_file_private *file_priv = ctx->file_priv;
4162306a36Sopenharmony_ci	unsigned long prev_hang;
4262306a36Sopenharmony_ci	unsigned int score;
4362306a36Sopenharmony_ci
4462306a36Sopenharmony_ci	if (IS_ERR_OR_NULL(file_priv))
4562306a36Sopenharmony_ci		return;
4662306a36Sopenharmony_ci
4762306a36Sopenharmony_ci	score = 0;
4862306a36Sopenharmony_ci	if (banned)
4962306a36Sopenharmony_ci		score = I915_CLIENT_SCORE_CONTEXT_BAN;
5062306a36Sopenharmony_ci
5162306a36Sopenharmony_ci	prev_hang = xchg(&file_priv->hang_timestamp, jiffies);
5262306a36Sopenharmony_ci	if (time_before(jiffies, prev_hang + I915_CLIENT_FAST_HANG_JIFFIES))
5362306a36Sopenharmony_ci		score += I915_CLIENT_SCORE_HANG_FAST;
5462306a36Sopenharmony_ci
5562306a36Sopenharmony_ci	if (score) {
5662306a36Sopenharmony_ci		atomic_add(score, &file_priv->ban_score);
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci		drm_dbg(&ctx->i915->drm,
5962306a36Sopenharmony_ci			"client %s: gained %u ban score, now %u\n",
6062306a36Sopenharmony_ci			ctx->name, score,
6162306a36Sopenharmony_ci			atomic_read(&file_priv->ban_score));
6262306a36Sopenharmony_ci	}
6362306a36Sopenharmony_ci}
6462306a36Sopenharmony_ci
6562306a36Sopenharmony_cistatic bool mark_guilty(struct i915_request *rq)
6662306a36Sopenharmony_ci{
6762306a36Sopenharmony_ci	struct i915_gem_context *ctx;
6862306a36Sopenharmony_ci	unsigned long prev_hang;
6962306a36Sopenharmony_ci	bool banned;
7062306a36Sopenharmony_ci	int i;
7162306a36Sopenharmony_ci
7262306a36Sopenharmony_ci	if (intel_context_is_closed(rq->context))
7362306a36Sopenharmony_ci		return true;
7462306a36Sopenharmony_ci
7562306a36Sopenharmony_ci	rcu_read_lock();
7662306a36Sopenharmony_ci	ctx = rcu_dereference(rq->context->gem_context);
7762306a36Sopenharmony_ci	if (ctx && !kref_get_unless_zero(&ctx->ref))
7862306a36Sopenharmony_ci		ctx = NULL;
7962306a36Sopenharmony_ci	rcu_read_unlock();
8062306a36Sopenharmony_ci	if (!ctx)
8162306a36Sopenharmony_ci		return intel_context_is_banned(rq->context);
8262306a36Sopenharmony_ci
8362306a36Sopenharmony_ci	atomic_inc(&ctx->guilty_count);
8462306a36Sopenharmony_ci
8562306a36Sopenharmony_ci	/* Cool contexts are too cool to be banned! (Used for reset testing.) */
8662306a36Sopenharmony_ci	if (!i915_gem_context_is_bannable(ctx)) {
8762306a36Sopenharmony_ci		banned = false;
8862306a36Sopenharmony_ci		goto out;
8962306a36Sopenharmony_ci	}
9062306a36Sopenharmony_ci
9162306a36Sopenharmony_ci	drm_notice(&ctx->i915->drm,
9262306a36Sopenharmony_ci		   "%s context reset due to GPU hang\n",
9362306a36Sopenharmony_ci		   ctx->name);
9462306a36Sopenharmony_ci
9562306a36Sopenharmony_ci	/* Record the timestamp for the last N hangs */
9662306a36Sopenharmony_ci	prev_hang = ctx->hang_timestamp[0];
9762306a36Sopenharmony_ci	for (i = 0; i < ARRAY_SIZE(ctx->hang_timestamp) - 1; i++)
9862306a36Sopenharmony_ci		ctx->hang_timestamp[i] = ctx->hang_timestamp[i + 1];
9962306a36Sopenharmony_ci	ctx->hang_timestamp[i] = jiffies;
10062306a36Sopenharmony_ci
10162306a36Sopenharmony_ci	/* If we have hung N+1 times in rapid succession, we ban the context! */
10262306a36Sopenharmony_ci	banned = !i915_gem_context_is_recoverable(ctx);
10362306a36Sopenharmony_ci	if (time_before(jiffies, prev_hang + CONTEXT_FAST_HANG_JIFFIES))
10462306a36Sopenharmony_ci		banned = true;
10562306a36Sopenharmony_ci	if (banned)
10662306a36Sopenharmony_ci		drm_dbg(&ctx->i915->drm, "context %s: guilty %d, banned\n",
10762306a36Sopenharmony_ci			ctx->name, atomic_read(&ctx->guilty_count));
10862306a36Sopenharmony_ci
10962306a36Sopenharmony_ci	client_mark_guilty(ctx, banned);
11062306a36Sopenharmony_ci
11162306a36Sopenharmony_ciout:
11262306a36Sopenharmony_ci	i915_gem_context_put(ctx);
11362306a36Sopenharmony_ci	return banned;
11462306a36Sopenharmony_ci}
11562306a36Sopenharmony_ci
11662306a36Sopenharmony_cistatic void mark_innocent(struct i915_request *rq)
11762306a36Sopenharmony_ci{
11862306a36Sopenharmony_ci	struct i915_gem_context *ctx;
11962306a36Sopenharmony_ci
12062306a36Sopenharmony_ci	rcu_read_lock();
12162306a36Sopenharmony_ci	ctx = rcu_dereference(rq->context->gem_context);
12262306a36Sopenharmony_ci	if (ctx)
12362306a36Sopenharmony_ci		atomic_inc(&ctx->active_count);
12462306a36Sopenharmony_ci	rcu_read_unlock();
12562306a36Sopenharmony_ci}
12662306a36Sopenharmony_ci
12762306a36Sopenharmony_civoid __i915_request_reset(struct i915_request *rq, bool guilty)
12862306a36Sopenharmony_ci{
12962306a36Sopenharmony_ci	bool banned = false;
13062306a36Sopenharmony_ci
13162306a36Sopenharmony_ci	RQ_TRACE(rq, "guilty? %s\n", str_yes_no(guilty));
13262306a36Sopenharmony_ci	GEM_BUG_ON(__i915_request_is_complete(rq));
13362306a36Sopenharmony_ci
13462306a36Sopenharmony_ci	rcu_read_lock(); /* protect the GEM context */
13562306a36Sopenharmony_ci	if (guilty) {
13662306a36Sopenharmony_ci		i915_request_set_error_once(rq, -EIO);
13762306a36Sopenharmony_ci		__i915_request_skip(rq);
13862306a36Sopenharmony_ci		banned = mark_guilty(rq);
13962306a36Sopenharmony_ci	} else {
14062306a36Sopenharmony_ci		i915_request_set_error_once(rq, -EAGAIN);
14162306a36Sopenharmony_ci		mark_innocent(rq);
14262306a36Sopenharmony_ci	}
14362306a36Sopenharmony_ci	rcu_read_unlock();
14462306a36Sopenharmony_ci
14562306a36Sopenharmony_ci	if (banned)
14662306a36Sopenharmony_ci		intel_context_ban(rq->context, rq);
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistatic bool i915_in_reset(struct pci_dev *pdev)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	u8 gdrst;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
15462306a36Sopenharmony_ci	return gdrst & GRDOM_RESET_STATUS;
15562306a36Sopenharmony_ci}
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_cistatic int i915_do_reset(struct intel_gt *gt,
15862306a36Sopenharmony_ci			 intel_engine_mask_t engine_mask,
15962306a36Sopenharmony_ci			 unsigned int retry)
16062306a36Sopenharmony_ci{
16162306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
16262306a36Sopenharmony_ci	int err;
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci	/* Assert reset for at least 20 usec, and wait for acknowledgement. */
16562306a36Sopenharmony_ci	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
16662306a36Sopenharmony_ci	udelay(50);
16762306a36Sopenharmony_ci	err = wait_for_atomic(i915_in_reset(pdev), 50);
16862306a36Sopenharmony_ci
16962306a36Sopenharmony_ci	/* Clear the reset request. */
17062306a36Sopenharmony_ci	pci_write_config_byte(pdev, I915_GDRST, 0);
17162306a36Sopenharmony_ci	udelay(50);
17262306a36Sopenharmony_ci	if (!err)
17362306a36Sopenharmony_ci		err = wait_for_atomic(!i915_in_reset(pdev), 50);
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	return err;
17662306a36Sopenharmony_ci}
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_cistatic bool g4x_reset_complete(struct pci_dev *pdev)
17962306a36Sopenharmony_ci{
18062306a36Sopenharmony_ci	u8 gdrst;
18162306a36Sopenharmony_ci
18262306a36Sopenharmony_ci	pci_read_config_byte(pdev, I915_GDRST, &gdrst);
18362306a36Sopenharmony_ci	return (gdrst & GRDOM_RESET_ENABLE) == 0;
18462306a36Sopenharmony_ci}
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_cistatic int g33_do_reset(struct intel_gt *gt,
18762306a36Sopenharmony_ci			intel_engine_mask_t engine_mask,
18862306a36Sopenharmony_ci			unsigned int retry)
18962306a36Sopenharmony_ci{
19062306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
19162306a36Sopenharmony_ci
19262306a36Sopenharmony_ci	pci_write_config_byte(pdev, I915_GDRST, GRDOM_RESET_ENABLE);
19362306a36Sopenharmony_ci	return wait_for_atomic(g4x_reset_complete(pdev), 50);
19462306a36Sopenharmony_ci}
19562306a36Sopenharmony_ci
19662306a36Sopenharmony_cistatic int g4x_do_reset(struct intel_gt *gt,
19762306a36Sopenharmony_ci			intel_engine_mask_t engine_mask,
19862306a36Sopenharmony_ci			unsigned int retry)
19962306a36Sopenharmony_ci{
20062306a36Sopenharmony_ci	struct pci_dev *pdev = to_pci_dev(gt->i915->drm.dev);
20162306a36Sopenharmony_ci	struct intel_uncore *uncore = gt->uncore;
20262306a36Sopenharmony_ci	int ret;
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	/* WaVcpClkGateDisableForMediaReset:ctg,elk */
20562306a36Sopenharmony_ci	intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, 0, VCP_UNIT_CLOCK_GATE_DISABLE);
20662306a36Sopenharmony_ci	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
20762306a36Sopenharmony_ci
20862306a36Sopenharmony_ci	pci_write_config_byte(pdev, I915_GDRST,
20962306a36Sopenharmony_ci			      GRDOM_MEDIA | GRDOM_RESET_ENABLE);
21062306a36Sopenharmony_ci	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
21162306a36Sopenharmony_ci	if (ret) {
21262306a36Sopenharmony_ci		GT_TRACE(gt, "Wait for media reset failed\n");
21362306a36Sopenharmony_ci		goto out;
21462306a36Sopenharmony_ci	}
21562306a36Sopenharmony_ci
21662306a36Sopenharmony_ci	pci_write_config_byte(pdev, I915_GDRST,
21762306a36Sopenharmony_ci			      GRDOM_RENDER | GRDOM_RESET_ENABLE);
21862306a36Sopenharmony_ci	ret =  wait_for_atomic(g4x_reset_complete(pdev), 50);
21962306a36Sopenharmony_ci	if (ret) {
22062306a36Sopenharmony_ci		GT_TRACE(gt, "Wait for render reset failed\n");
22162306a36Sopenharmony_ci		goto out;
22262306a36Sopenharmony_ci	}
22362306a36Sopenharmony_ci
22462306a36Sopenharmony_ciout:
22562306a36Sopenharmony_ci	pci_write_config_byte(pdev, I915_GDRST, 0);
22662306a36Sopenharmony_ci
22762306a36Sopenharmony_ci	intel_uncore_rmw_fw(uncore, VDECCLK_GATE_D, VCP_UNIT_CLOCK_GATE_DISABLE, 0);
22862306a36Sopenharmony_ci	intel_uncore_posting_read_fw(uncore, VDECCLK_GATE_D);
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci	return ret;
23162306a36Sopenharmony_ci}
23262306a36Sopenharmony_ci
23362306a36Sopenharmony_cistatic int ilk_do_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask,
23462306a36Sopenharmony_ci			unsigned int retry)
23562306a36Sopenharmony_ci{
23662306a36Sopenharmony_ci	struct intel_uncore *uncore = gt->uncore;
23762306a36Sopenharmony_ci	int ret;
23862306a36Sopenharmony_ci
23962306a36Sopenharmony_ci	intel_uncore_write_fw(uncore, ILK_GDSR,
24062306a36Sopenharmony_ci			      ILK_GRDOM_RENDER | ILK_GRDOM_RESET_ENABLE);
24162306a36Sopenharmony_ci	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
24262306a36Sopenharmony_ci					   ILK_GRDOM_RESET_ENABLE, 0,
24362306a36Sopenharmony_ci					   5000, 0,
24462306a36Sopenharmony_ci					   NULL);
24562306a36Sopenharmony_ci	if (ret) {
24662306a36Sopenharmony_ci		GT_TRACE(gt, "Wait for render reset failed\n");
24762306a36Sopenharmony_ci		goto out;
24862306a36Sopenharmony_ci	}
24962306a36Sopenharmony_ci
25062306a36Sopenharmony_ci	intel_uncore_write_fw(uncore, ILK_GDSR,
25162306a36Sopenharmony_ci			      ILK_GRDOM_MEDIA | ILK_GRDOM_RESET_ENABLE);
25262306a36Sopenharmony_ci	ret = __intel_wait_for_register_fw(uncore, ILK_GDSR,
25362306a36Sopenharmony_ci					   ILK_GRDOM_RESET_ENABLE, 0,
25462306a36Sopenharmony_ci					   5000, 0,
25562306a36Sopenharmony_ci					   NULL);
25662306a36Sopenharmony_ci	if (ret) {
25762306a36Sopenharmony_ci		GT_TRACE(gt, "Wait for media reset failed\n");
25862306a36Sopenharmony_ci		goto out;
25962306a36Sopenharmony_ci	}
26062306a36Sopenharmony_ci
26162306a36Sopenharmony_ciout:
26262306a36Sopenharmony_ci	intel_uncore_write_fw(uncore, ILK_GDSR, 0);
26362306a36Sopenharmony_ci	intel_uncore_posting_read_fw(uncore, ILK_GDSR);
26462306a36Sopenharmony_ci	return ret;
26562306a36Sopenharmony_ci}
26662306a36Sopenharmony_ci
26762306a36Sopenharmony_ci/* Reset the hardware domains (GENX_GRDOM_*) specified by mask */
26862306a36Sopenharmony_cistatic int gen6_hw_domain_reset(struct intel_gt *gt, u32 hw_domain_mask)
26962306a36Sopenharmony_ci{
27062306a36Sopenharmony_ci	struct intel_uncore *uncore = gt->uncore;
27162306a36Sopenharmony_ci	int loops;
27262306a36Sopenharmony_ci	int err;
27362306a36Sopenharmony_ci
27462306a36Sopenharmony_ci	/*
27562306a36Sopenharmony_ci	 * On some platforms, e.g. Jasperlake, we see that the engine register
27662306a36Sopenharmony_ci	 * state is not cleared until shortly after GDRST reports completion,
27762306a36Sopenharmony_ci	 * causing a failure as we try to immediately resume while the internal
27862306a36Sopenharmony_ci	 * state is still in flux. If we immediately repeat the reset, the
27962306a36Sopenharmony_ci	 * second reset appears to serialise with the first, and since it is a
28062306a36Sopenharmony_ci	 * no-op, the registers should retain their reset value. However, there
28162306a36Sopenharmony_ci	 * is still a concern that upon leaving the second reset, the internal
28262306a36Sopenharmony_ci	 * engine state is still in flux and not ready for resuming.
28362306a36Sopenharmony_ci	 *
28462306a36Sopenharmony_ci	 * Starting on MTL, there are some prep steps that we need to do when
28562306a36Sopenharmony_ci	 * resetting some engines that need to be applied every time we write to
28662306a36Sopenharmony_ci	 * GEN6_GDRST. As those are time consuming (tens of ms), we don't want
28762306a36Sopenharmony_ci	 * to perform that twice, so, since the Jasperlake issue hasn't been
28862306a36Sopenharmony_ci	 * observed on MTL, we avoid repeating the reset on newer platforms.
28962306a36Sopenharmony_ci	 */
29062306a36Sopenharmony_ci	loops = GRAPHICS_VER_FULL(gt->i915) < IP_VER(12, 70) ? 2 : 1;
29162306a36Sopenharmony_ci
29262306a36Sopenharmony_ci	/*
29362306a36Sopenharmony_ci	 * GEN6_GDRST is not in the gt power well, no need to check
29462306a36Sopenharmony_ci	 * for fifo space for the write or forcewake the chip for
29562306a36Sopenharmony_ci	 * the read
29662306a36Sopenharmony_ci	 */
29762306a36Sopenharmony_ci	do {
29862306a36Sopenharmony_ci		intel_uncore_write_fw(uncore, GEN6_GDRST, hw_domain_mask);
29962306a36Sopenharmony_ci
30062306a36Sopenharmony_ci		/* Wait for the device to ack the reset requests. */
30162306a36Sopenharmony_ci		err = __intel_wait_for_register_fw(uncore, GEN6_GDRST,
30262306a36Sopenharmony_ci						   hw_domain_mask, 0,
30362306a36Sopenharmony_ci						   2000, 0,
30462306a36Sopenharmony_ci						   NULL);
30562306a36Sopenharmony_ci	} while (err == 0 && --loops);
30662306a36Sopenharmony_ci	if (err)
30762306a36Sopenharmony_ci		GT_TRACE(gt,
30862306a36Sopenharmony_ci			 "Wait for 0x%08x engines reset failed\n",
30962306a36Sopenharmony_ci			 hw_domain_mask);
31062306a36Sopenharmony_ci
31162306a36Sopenharmony_ci	/*
31262306a36Sopenharmony_ci	 * As we have observed that the engine state is still volatile
31362306a36Sopenharmony_ci	 * after GDRST is acked, impose a small delay to let everything settle.
31462306a36Sopenharmony_ci	 */
31562306a36Sopenharmony_ci	udelay(50);
31662306a36Sopenharmony_ci
31762306a36Sopenharmony_ci	return err;
31862306a36Sopenharmony_ci}
31962306a36Sopenharmony_ci
32062306a36Sopenharmony_cistatic int __gen6_reset_engines(struct intel_gt *gt,
32162306a36Sopenharmony_ci				intel_engine_mask_t engine_mask,
32262306a36Sopenharmony_ci				unsigned int retry)
32362306a36Sopenharmony_ci{
32462306a36Sopenharmony_ci	struct intel_engine_cs *engine;
32562306a36Sopenharmony_ci	u32 hw_mask;
32662306a36Sopenharmony_ci
32762306a36Sopenharmony_ci	if (engine_mask == ALL_ENGINES) {
32862306a36Sopenharmony_ci		hw_mask = GEN6_GRDOM_FULL;
32962306a36Sopenharmony_ci	} else {
33062306a36Sopenharmony_ci		intel_engine_mask_t tmp;
33162306a36Sopenharmony_ci
33262306a36Sopenharmony_ci		hw_mask = 0;
33362306a36Sopenharmony_ci		for_each_engine_masked(engine, gt, engine_mask, tmp) {
33462306a36Sopenharmony_ci			hw_mask |= engine->reset_domain;
33562306a36Sopenharmony_ci		}
33662306a36Sopenharmony_ci	}
33762306a36Sopenharmony_ci
33862306a36Sopenharmony_ci	return gen6_hw_domain_reset(gt, hw_mask);
33962306a36Sopenharmony_ci}
34062306a36Sopenharmony_ci
34162306a36Sopenharmony_cistatic int gen6_reset_engines(struct intel_gt *gt,
34262306a36Sopenharmony_ci			      intel_engine_mask_t engine_mask,
34362306a36Sopenharmony_ci			      unsigned int retry)
34462306a36Sopenharmony_ci{
34562306a36Sopenharmony_ci	unsigned long flags;
34662306a36Sopenharmony_ci	int ret;
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_ci	spin_lock_irqsave(&gt->uncore->lock, flags);
34962306a36Sopenharmony_ci	ret = __gen6_reset_engines(gt, engine_mask, retry);
35062306a36Sopenharmony_ci	spin_unlock_irqrestore(&gt->uncore->lock, flags);
35162306a36Sopenharmony_ci
35262306a36Sopenharmony_ci	return ret;
35362306a36Sopenharmony_ci}
35462306a36Sopenharmony_ci
35562306a36Sopenharmony_cistatic struct intel_engine_cs *find_sfc_paired_vecs_engine(struct intel_engine_cs *engine)
35662306a36Sopenharmony_ci{
35762306a36Sopenharmony_ci	int vecs_id;
35862306a36Sopenharmony_ci
35962306a36Sopenharmony_ci	GEM_BUG_ON(engine->class != VIDEO_DECODE_CLASS);
36062306a36Sopenharmony_ci
36162306a36Sopenharmony_ci	vecs_id = _VECS((engine->instance) / 2);
36262306a36Sopenharmony_ci
36362306a36Sopenharmony_ci	return engine->gt->engine[vecs_id];
36462306a36Sopenharmony_ci}
36562306a36Sopenharmony_ci
36662306a36Sopenharmony_cistruct sfc_lock_data {
36762306a36Sopenharmony_ci	i915_reg_t lock_reg;
36862306a36Sopenharmony_ci	i915_reg_t ack_reg;
36962306a36Sopenharmony_ci	i915_reg_t usage_reg;
37062306a36Sopenharmony_ci	u32 lock_bit;
37162306a36Sopenharmony_ci	u32 ack_bit;
37262306a36Sopenharmony_ci	u32 usage_bit;
37362306a36Sopenharmony_ci	u32 reset_bit;
37462306a36Sopenharmony_ci};
37562306a36Sopenharmony_ci
37662306a36Sopenharmony_cistatic void get_sfc_forced_lock_data(struct intel_engine_cs *engine,
37762306a36Sopenharmony_ci				     struct sfc_lock_data *sfc_lock)
37862306a36Sopenharmony_ci{
37962306a36Sopenharmony_ci	switch (engine->class) {
38062306a36Sopenharmony_ci	default:
38162306a36Sopenharmony_ci		MISSING_CASE(engine->class);
38262306a36Sopenharmony_ci		fallthrough;
38362306a36Sopenharmony_ci	case VIDEO_DECODE_CLASS:
38462306a36Sopenharmony_ci		sfc_lock->lock_reg = GEN11_VCS_SFC_FORCED_LOCK(engine->mmio_base);
38562306a36Sopenharmony_ci		sfc_lock->lock_bit = GEN11_VCS_SFC_FORCED_LOCK_BIT;
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_ci		sfc_lock->ack_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
38862306a36Sopenharmony_ci		sfc_lock->ack_bit  = GEN11_VCS_SFC_LOCK_ACK_BIT;
38962306a36Sopenharmony_ci
39062306a36Sopenharmony_ci		sfc_lock->usage_reg = GEN11_VCS_SFC_LOCK_STATUS(engine->mmio_base);
39162306a36Sopenharmony_ci		sfc_lock->usage_bit = GEN11_VCS_SFC_USAGE_BIT;
39262306a36Sopenharmony_ci		sfc_lock->reset_bit = GEN11_VCS_SFC_RESET_BIT(engine->instance);
39362306a36Sopenharmony_ci
39462306a36Sopenharmony_ci		break;
39562306a36Sopenharmony_ci	case VIDEO_ENHANCEMENT_CLASS:
39662306a36Sopenharmony_ci		sfc_lock->lock_reg = GEN11_VECS_SFC_FORCED_LOCK(engine->mmio_base);
39762306a36Sopenharmony_ci		sfc_lock->lock_bit = GEN11_VECS_SFC_FORCED_LOCK_BIT;
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci		sfc_lock->ack_reg = GEN11_VECS_SFC_LOCK_ACK(engine->mmio_base);
40062306a36Sopenharmony_ci		sfc_lock->ack_bit  = GEN11_VECS_SFC_LOCK_ACK_BIT;
40162306a36Sopenharmony_ci
40262306a36Sopenharmony_ci		sfc_lock->usage_reg = GEN11_VECS_SFC_USAGE(engine->mmio_base);
40362306a36Sopenharmony_ci		sfc_lock->usage_bit = GEN11_VECS_SFC_USAGE_BIT;
40462306a36Sopenharmony_ci		sfc_lock->reset_bit = GEN11_VECS_SFC_RESET_BIT(engine->instance);
40562306a36Sopenharmony_ci
40662306a36Sopenharmony_ci		break;
40762306a36Sopenharmony_ci	}
40862306a36Sopenharmony_ci}
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_cistatic int gen11_lock_sfc(struct intel_engine_cs *engine,
41162306a36Sopenharmony_ci			  u32 *reset_mask,
41262306a36Sopenharmony_ci			  u32 *unlock_mask)
41362306a36Sopenharmony_ci{
41462306a36Sopenharmony_ci	struct intel_uncore *uncore = engine->uncore;
41562306a36Sopenharmony_ci	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
41662306a36Sopenharmony_ci	struct sfc_lock_data sfc_lock;
41762306a36Sopenharmony_ci	bool lock_obtained, lock_to_other = false;
41862306a36Sopenharmony_ci	int ret;
41962306a36Sopenharmony_ci
42062306a36Sopenharmony_ci	switch (engine->class) {
42162306a36Sopenharmony_ci	case VIDEO_DECODE_CLASS:
42262306a36Sopenharmony_ci		if ((BIT(engine->instance) & vdbox_sfc_access) == 0)
42362306a36Sopenharmony_ci			return 0;
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci		fallthrough;
42662306a36Sopenharmony_ci	case VIDEO_ENHANCEMENT_CLASS:
42762306a36Sopenharmony_ci		get_sfc_forced_lock_data(engine, &sfc_lock);
42862306a36Sopenharmony_ci
42962306a36Sopenharmony_ci		break;
43062306a36Sopenharmony_ci	default:
43162306a36Sopenharmony_ci		return 0;
43262306a36Sopenharmony_ci	}
43362306a36Sopenharmony_ci
43462306a36Sopenharmony_ci	if (!(intel_uncore_read_fw(uncore, sfc_lock.usage_reg) & sfc_lock.usage_bit)) {
43562306a36Sopenharmony_ci		struct intel_engine_cs *paired_vecs;
43662306a36Sopenharmony_ci
43762306a36Sopenharmony_ci		if (engine->class != VIDEO_DECODE_CLASS ||
43862306a36Sopenharmony_ci		    GRAPHICS_VER(engine->i915) != 12)
43962306a36Sopenharmony_ci			return 0;
44062306a36Sopenharmony_ci
44162306a36Sopenharmony_ci		/*
44262306a36Sopenharmony_ci		 * Wa_14010733141
44362306a36Sopenharmony_ci		 *
44462306a36Sopenharmony_ci		 * If the VCS-MFX isn't using the SFC, we also need to check
44562306a36Sopenharmony_ci		 * whether VCS-HCP is using it.  If so, we need to issue a *VE*
44662306a36Sopenharmony_ci		 * forced lock on the VE engine that shares the same SFC.
44762306a36Sopenharmony_ci		 */
44862306a36Sopenharmony_ci		if (!(intel_uncore_read_fw(uncore,
44962306a36Sopenharmony_ci					   GEN12_HCP_SFC_LOCK_STATUS(engine->mmio_base)) &
45062306a36Sopenharmony_ci		      GEN12_HCP_SFC_USAGE_BIT))
45162306a36Sopenharmony_ci			return 0;
45262306a36Sopenharmony_ci
45362306a36Sopenharmony_ci		paired_vecs = find_sfc_paired_vecs_engine(engine);
45462306a36Sopenharmony_ci		get_sfc_forced_lock_data(paired_vecs, &sfc_lock);
45562306a36Sopenharmony_ci		lock_to_other = true;
45662306a36Sopenharmony_ci		*unlock_mask |= paired_vecs->mask;
45762306a36Sopenharmony_ci	} else {
45862306a36Sopenharmony_ci		*unlock_mask |= engine->mask;
45962306a36Sopenharmony_ci	}
46062306a36Sopenharmony_ci
46162306a36Sopenharmony_ci	/*
46262306a36Sopenharmony_ci	 * If the engine is using an SFC, tell the engine that a software reset
46362306a36Sopenharmony_ci	 * is going to happen. The engine will then try to force lock the SFC.
46462306a36Sopenharmony_ci	 * If SFC ends up being locked to the engine we want to reset, we have
46562306a36Sopenharmony_ci	 * to reset it as well (we will unlock it once the reset sequence is
46662306a36Sopenharmony_ci	 * completed).
46762306a36Sopenharmony_ci	 */
46862306a36Sopenharmony_ci	intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, 0, sfc_lock.lock_bit);
46962306a36Sopenharmony_ci
47062306a36Sopenharmony_ci	ret = __intel_wait_for_register_fw(uncore,
47162306a36Sopenharmony_ci					   sfc_lock.ack_reg,
47262306a36Sopenharmony_ci					   sfc_lock.ack_bit,
47362306a36Sopenharmony_ci					   sfc_lock.ack_bit,
47462306a36Sopenharmony_ci					   1000, 0, NULL);
47562306a36Sopenharmony_ci
47662306a36Sopenharmony_ci	/*
47762306a36Sopenharmony_ci	 * Was the SFC released while we were trying to lock it?
47862306a36Sopenharmony_ci	 *
47962306a36Sopenharmony_ci	 * We should reset both the engine and the SFC if:
48062306a36Sopenharmony_ci	 *  - We were locking the SFC to this engine and the lock succeeded
48162306a36Sopenharmony_ci	 *       OR
48262306a36Sopenharmony_ci	 *  - We were locking the SFC to a different engine (Wa_14010733141)
48362306a36Sopenharmony_ci	 *    but the SFC was released before the lock was obtained.
48462306a36Sopenharmony_ci	 *
48562306a36Sopenharmony_ci	 * Otherwise we need only reset the engine by itself and we can
48662306a36Sopenharmony_ci	 * leave the SFC alone.
48762306a36Sopenharmony_ci	 */
48862306a36Sopenharmony_ci	lock_obtained = (intel_uncore_read_fw(uncore, sfc_lock.usage_reg) &
48962306a36Sopenharmony_ci			sfc_lock.usage_bit) != 0;
49062306a36Sopenharmony_ci	if (lock_obtained == lock_to_other)
49162306a36Sopenharmony_ci		return 0;
49262306a36Sopenharmony_ci
49362306a36Sopenharmony_ci	if (ret) {
49462306a36Sopenharmony_ci		ENGINE_TRACE(engine, "Wait for SFC forced lock ack failed\n");
49562306a36Sopenharmony_ci		return ret;
49662306a36Sopenharmony_ci	}
49762306a36Sopenharmony_ci
49862306a36Sopenharmony_ci	*reset_mask |= sfc_lock.reset_bit;
49962306a36Sopenharmony_ci	return 0;
50062306a36Sopenharmony_ci}
50162306a36Sopenharmony_ci
50262306a36Sopenharmony_cistatic void gen11_unlock_sfc(struct intel_engine_cs *engine)
50362306a36Sopenharmony_ci{
50462306a36Sopenharmony_ci	struct intel_uncore *uncore = engine->uncore;
50562306a36Sopenharmony_ci	u8 vdbox_sfc_access = engine->gt->info.vdbox_sfc_access;
50662306a36Sopenharmony_ci	struct sfc_lock_data sfc_lock = {};
50762306a36Sopenharmony_ci
50862306a36Sopenharmony_ci	if (engine->class != VIDEO_DECODE_CLASS &&
50962306a36Sopenharmony_ci	    engine->class != VIDEO_ENHANCEMENT_CLASS)
51062306a36Sopenharmony_ci		return;
51162306a36Sopenharmony_ci
51262306a36Sopenharmony_ci	if (engine->class == VIDEO_DECODE_CLASS &&
51362306a36Sopenharmony_ci	    (BIT(engine->instance) & vdbox_sfc_access) == 0)
51462306a36Sopenharmony_ci		return;
51562306a36Sopenharmony_ci
51662306a36Sopenharmony_ci	get_sfc_forced_lock_data(engine, &sfc_lock);
51762306a36Sopenharmony_ci
51862306a36Sopenharmony_ci	intel_uncore_rmw_fw(uncore, sfc_lock.lock_reg, sfc_lock.lock_bit, 0);
51962306a36Sopenharmony_ci}
52062306a36Sopenharmony_ci
52162306a36Sopenharmony_cistatic int __gen11_reset_engines(struct intel_gt *gt,
52262306a36Sopenharmony_ci				 intel_engine_mask_t engine_mask,
52362306a36Sopenharmony_ci				 unsigned int retry)
52462306a36Sopenharmony_ci{
52562306a36Sopenharmony_ci	struct intel_engine_cs *engine;
52662306a36Sopenharmony_ci	intel_engine_mask_t tmp;
52762306a36Sopenharmony_ci	u32 reset_mask, unlock_mask = 0;
52862306a36Sopenharmony_ci	int ret;
52962306a36Sopenharmony_ci
53062306a36Sopenharmony_ci	if (engine_mask == ALL_ENGINES) {
53162306a36Sopenharmony_ci		reset_mask = GEN11_GRDOM_FULL;
53262306a36Sopenharmony_ci	} else {
53362306a36Sopenharmony_ci		reset_mask = 0;
53462306a36Sopenharmony_ci		for_each_engine_masked(engine, gt, engine_mask, tmp) {
53562306a36Sopenharmony_ci			reset_mask |= engine->reset_domain;
53662306a36Sopenharmony_ci			ret = gen11_lock_sfc(engine, &reset_mask, &unlock_mask);
53762306a36Sopenharmony_ci			if (ret)
53862306a36Sopenharmony_ci				goto sfc_unlock;
53962306a36Sopenharmony_ci		}
54062306a36Sopenharmony_ci	}
54162306a36Sopenharmony_ci
54262306a36Sopenharmony_ci	ret = gen6_hw_domain_reset(gt, reset_mask);
54362306a36Sopenharmony_ci
54462306a36Sopenharmony_cisfc_unlock:
54562306a36Sopenharmony_ci	/*
54662306a36Sopenharmony_ci	 * We unlock the SFC based on the lock status and not the result of
54762306a36Sopenharmony_ci	 * gen11_lock_sfc to make sure that we clean properly if something
54862306a36Sopenharmony_ci	 * wrong happened during the lock (e.g. lock acquired after timeout
54962306a36Sopenharmony_ci	 * expiration).
55062306a36Sopenharmony_ci	 *
55162306a36Sopenharmony_ci	 * Due to Wa_14010733141, we may have locked an SFC to an engine that
55262306a36Sopenharmony_ci	 * wasn't being reset.  So instead of calling gen11_unlock_sfc()
55362306a36Sopenharmony_ci	 * on engine_mask, we instead call it on the mask of engines that our
55462306a36Sopenharmony_ci	 * gen11_lock_sfc() calls told us actually had locks attempted.
55562306a36Sopenharmony_ci	 */
55662306a36Sopenharmony_ci	for_each_engine_masked(engine, gt, unlock_mask, tmp)
55762306a36Sopenharmony_ci		gen11_unlock_sfc(engine);
55862306a36Sopenharmony_ci
55962306a36Sopenharmony_ci	return ret;
56062306a36Sopenharmony_ci}
56162306a36Sopenharmony_ci
56262306a36Sopenharmony_cistatic int gen8_engine_reset_prepare(struct intel_engine_cs *engine)
56362306a36Sopenharmony_ci{
56462306a36Sopenharmony_ci	struct intel_uncore *uncore = engine->uncore;
56562306a36Sopenharmony_ci	const i915_reg_t reg = RING_RESET_CTL(engine->mmio_base);
56662306a36Sopenharmony_ci	u32 request, mask, ack;
56762306a36Sopenharmony_ci	int ret;
56862306a36Sopenharmony_ci
56962306a36Sopenharmony_ci	if (I915_SELFTEST_ONLY(should_fail(&engine->reset_timeout, 1)))
57062306a36Sopenharmony_ci		return -ETIMEDOUT;
57162306a36Sopenharmony_ci
57262306a36Sopenharmony_ci	ack = intel_uncore_read_fw(uncore, reg);
57362306a36Sopenharmony_ci	if (ack & RESET_CTL_CAT_ERROR) {
57462306a36Sopenharmony_ci		/*
57562306a36Sopenharmony_ci		 * For catastrophic errors, ready-for-reset sequence
57662306a36Sopenharmony_ci		 * needs to be bypassed: HAS#396813
57762306a36Sopenharmony_ci		 */
57862306a36Sopenharmony_ci		request = RESET_CTL_CAT_ERROR;
57962306a36Sopenharmony_ci		mask = RESET_CTL_CAT_ERROR;
58062306a36Sopenharmony_ci
58162306a36Sopenharmony_ci		/* Catastrophic errors need to be cleared by HW */
58262306a36Sopenharmony_ci		ack = 0;
58362306a36Sopenharmony_ci	} else if (!(ack & RESET_CTL_READY_TO_RESET)) {
58462306a36Sopenharmony_ci		request = RESET_CTL_REQUEST_RESET;
58562306a36Sopenharmony_ci		mask = RESET_CTL_READY_TO_RESET;
58662306a36Sopenharmony_ci		ack = RESET_CTL_READY_TO_RESET;
58762306a36Sopenharmony_ci	} else {
58862306a36Sopenharmony_ci		return 0;
58962306a36Sopenharmony_ci	}
59062306a36Sopenharmony_ci
59162306a36Sopenharmony_ci	intel_uncore_write_fw(uncore, reg, _MASKED_BIT_ENABLE(request));
59262306a36Sopenharmony_ci	ret = __intel_wait_for_register_fw(uncore, reg, mask, ack,
59362306a36Sopenharmony_ci					   700, 0, NULL);
59462306a36Sopenharmony_ci	if (ret)
59562306a36Sopenharmony_ci		drm_err(&engine->i915->drm,
59662306a36Sopenharmony_ci			"%s reset request timed out: {request: %08x, RESET_CTL: %08x}\n",
59762306a36Sopenharmony_ci			engine->name, request,
59862306a36Sopenharmony_ci			intel_uncore_read_fw(uncore, reg));
59962306a36Sopenharmony_ci
60062306a36Sopenharmony_ci	return ret;
60162306a36Sopenharmony_ci}
60262306a36Sopenharmony_ci
60362306a36Sopenharmony_cistatic void gen8_engine_reset_cancel(struct intel_engine_cs *engine)
60462306a36Sopenharmony_ci{
60562306a36Sopenharmony_ci	intel_uncore_write_fw(engine->uncore,
60662306a36Sopenharmony_ci			      RING_RESET_CTL(engine->mmio_base),
60762306a36Sopenharmony_ci			      _MASKED_BIT_DISABLE(RESET_CTL_REQUEST_RESET));
60862306a36Sopenharmony_ci}
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_cistatic int gen8_reset_engines(struct intel_gt *gt,
61162306a36Sopenharmony_ci			      intel_engine_mask_t engine_mask,
61262306a36Sopenharmony_ci			      unsigned int retry)
61362306a36Sopenharmony_ci{
61462306a36Sopenharmony_ci	struct intel_engine_cs *engine;
61562306a36Sopenharmony_ci	const bool reset_non_ready = retry >= 1;
61662306a36Sopenharmony_ci	intel_engine_mask_t tmp;
61762306a36Sopenharmony_ci	unsigned long flags;
61862306a36Sopenharmony_ci	int ret;
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_ci	spin_lock_irqsave(&gt->uncore->lock, flags);
62162306a36Sopenharmony_ci
62262306a36Sopenharmony_ci	for_each_engine_masked(engine, gt, engine_mask, tmp) {
62362306a36Sopenharmony_ci		ret = gen8_engine_reset_prepare(engine);
62462306a36Sopenharmony_ci		if (ret && !reset_non_ready)
62562306a36Sopenharmony_ci			goto skip_reset;
62662306a36Sopenharmony_ci
62762306a36Sopenharmony_ci		/*
62862306a36Sopenharmony_ci		 * If this is not the first failed attempt to prepare,
62962306a36Sopenharmony_ci		 * we decide to proceed anyway.
63062306a36Sopenharmony_ci		 *
63162306a36Sopenharmony_ci		 * By doing so we risk context corruption and with
63262306a36Sopenharmony_ci		 * some gens (kbl), possible system hang if reset
63362306a36Sopenharmony_ci		 * happens during active bb execution.
63462306a36Sopenharmony_ci		 *
63562306a36Sopenharmony_ci		 * We rather take context corruption instead of
63662306a36Sopenharmony_ci		 * failed reset with a wedged driver/gpu. And
63762306a36Sopenharmony_ci		 * active bb execution case should be covered by
63862306a36Sopenharmony_ci		 * stop_engines() we have before the reset.
63962306a36Sopenharmony_ci		 */
64062306a36Sopenharmony_ci	}
64162306a36Sopenharmony_ci
64262306a36Sopenharmony_ci	/*
64362306a36Sopenharmony_ci	 * Wa_22011100796:dg2, whenever Full soft reset is required,
64462306a36Sopenharmony_ci	 * reset all individual engines firstly, and then do a full soft reset.
64562306a36Sopenharmony_ci	 *
64662306a36Sopenharmony_ci	 * This is best effort, so ignore any error from the initial reset.
64762306a36Sopenharmony_ci	 */
64862306a36Sopenharmony_ci	if (IS_DG2(gt->i915) && engine_mask == ALL_ENGINES)
64962306a36Sopenharmony_ci		__gen11_reset_engines(gt, gt->info.engine_mask, 0);
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci	if (GRAPHICS_VER(gt->i915) >= 11)
65262306a36Sopenharmony_ci		ret = __gen11_reset_engines(gt, engine_mask, retry);
65362306a36Sopenharmony_ci	else
65462306a36Sopenharmony_ci		ret = __gen6_reset_engines(gt, engine_mask, retry);
65562306a36Sopenharmony_ci
65662306a36Sopenharmony_ciskip_reset:
65762306a36Sopenharmony_ci	for_each_engine_masked(engine, gt, engine_mask, tmp)
65862306a36Sopenharmony_ci		gen8_engine_reset_cancel(engine);
65962306a36Sopenharmony_ci
66062306a36Sopenharmony_ci	spin_unlock_irqrestore(&gt->uncore->lock, flags);
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci	return ret;
66362306a36Sopenharmony_ci}
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_cistatic int mock_reset(struct intel_gt *gt,
66662306a36Sopenharmony_ci		      intel_engine_mask_t mask,
66762306a36Sopenharmony_ci		      unsigned int retry)
66862306a36Sopenharmony_ci{
66962306a36Sopenharmony_ci	return 0;
67062306a36Sopenharmony_ci}
67162306a36Sopenharmony_ci
67262306a36Sopenharmony_citypedef int (*reset_func)(struct intel_gt *,
67362306a36Sopenharmony_ci			  intel_engine_mask_t engine_mask,
67462306a36Sopenharmony_ci			  unsigned int retry);
67562306a36Sopenharmony_ci
67662306a36Sopenharmony_cistatic reset_func intel_get_gpu_reset(const struct intel_gt *gt)
67762306a36Sopenharmony_ci{
67862306a36Sopenharmony_ci	struct drm_i915_private *i915 = gt->i915;
67962306a36Sopenharmony_ci
68062306a36Sopenharmony_ci	if (is_mock_gt(gt))
68162306a36Sopenharmony_ci		return mock_reset;
68262306a36Sopenharmony_ci	else if (GRAPHICS_VER(i915) >= 8)
68362306a36Sopenharmony_ci		return gen8_reset_engines;
68462306a36Sopenharmony_ci	else if (GRAPHICS_VER(i915) >= 6)
68562306a36Sopenharmony_ci		return gen6_reset_engines;
68662306a36Sopenharmony_ci	else if (GRAPHICS_VER(i915) >= 5)
68762306a36Sopenharmony_ci		return ilk_do_reset;
68862306a36Sopenharmony_ci	else if (IS_G4X(i915))
68962306a36Sopenharmony_ci		return g4x_do_reset;
69062306a36Sopenharmony_ci	else if (IS_G33(i915) || IS_PINEVIEW(i915))
69162306a36Sopenharmony_ci		return g33_do_reset;
69262306a36Sopenharmony_ci	else if (GRAPHICS_VER(i915) >= 3)
69362306a36Sopenharmony_ci		return i915_do_reset;
69462306a36Sopenharmony_ci	else
69562306a36Sopenharmony_ci		return NULL;
69662306a36Sopenharmony_ci}
69762306a36Sopenharmony_ci
69862306a36Sopenharmony_cistatic int __reset_guc(struct intel_gt *gt)
69962306a36Sopenharmony_ci{
70062306a36Sopenharmony_ci	u32 guc_domain =
70162306a36Sopenharmony_ci		GRAPHICS_VER(gt->i915) >= 11 ? GEN11_GRDOM_GUC : GEN9_GRDOM_GUC;
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci	return gen6_hw_domain_reset(gt, guc_domain);
70462306a36Sopenharmony_ci}
70562306a36Sopenharmony_ci
70662306a36Sopenharmony_cistatic bool needs_wa_14015076503(struct intel_gt *gt, intel_engine_mask_t engine_mask)
70762306a36Sopenharmony_ci{
70862306a36Sopenharmony_ci	if (!IS_METEORLAKE(gt->i915) || !HAS_ENGINE(gt, GSC0))
70962306a36Sopenharmony_ci		return false;
71062306a36Sopenharmony_ci
71162306a36Sopenharmony_ci	if (!__HAS_ENGINE(engine_mask, GSC0))
71262306a36Sopenharmony_ci		return false;
71362306a36Sopenharmony_ci
71462306a36Sopenharmony_ci	return intel_gsc_uc_fw_init_done(&gt->uc.gsc);
71562306a36Sopenharmony_ci}
71662306a36Sopenharmony_ci
71762306a36Sopenharmony_cistatic intel_engine_mask_t
71862306a36Sopenharmony_ciwa_14015076503_start(struct intel_gt *gt, intel_engine_mask_t engine_mask, bool first)
71962306a36Sopenharmony_ci{
72062306a36Sopenharmony_ci	if (!needs_wa_14015076503(gt, engine_mask))
72162306a36Sopenharmony_ci		return engine_mask;
72262306a36Sopenharmony_ci
72362306a36Sopenharmony_ci	/*
72462306a36Sopenharmony_ci	 * wa_14015076503: if the GSC FW is loaded, we need to alert it that
72562306a36Sopenharmony_ci	 * we're going to do a GSC engine reset and then wait for 200ms for the
72662306a36Sopenharmony_ci	 * FW to get ready for it. However, if this is the first ALL_ENGINES
72762306a36Sopenharmony_ci	 * reset attempt and the GSC is not busy, we can try to instead reset
72862306a36Sopenharmony_ci	 * the GuC and all the other engines individually to avoid the 200ms
72962306a36Sopenharmony_ci	 * wait.
73062306a36Sopenharmony_ci	 * Skipping the GSC engine is safe because, differently from other
73162306a36Sopenharmony_ci	 * engines, the GSCCS only role is to forward the commands to the GSC
73262306a36Sopenharmony_ci	 * FW, so it doesn't have any HW outside of the CS itself and therefore
73362306a36Sopenharmony_ci	 * it has no state that we don't explicitly re-init on resume or on
73462306a36Sopenharmony_ci	 * context switch LRC or power context). The HW for the GSC uC is
73562306a36Sopenharmony_ci	 * managed by the GSC FW so we don't need to care about that.
73662306a36Sopenharmony_ci	 */
73762306a36Sopenharmony_ci	if (engine_mask == ALL_ENGINES && first && intel_engine_is_idle(gt->engine[GSC0])) {
73862306a36Sopenharmony_ci		__reset_guc(gt);
73962306a36Sopenharmony_ci		engine_mask = gt->info.engine_mask & ~BIT(GSC0);
74062306a36Sopenharmony_ci	} else {
74162306a36Sopenharmony_ci		intel_uncore_rmw(gt->uncore,
74262306a36Sopenharmony_ci				 HECI_H_GS1(MTL_GSC_HECI2_BASE),
74362306a36Sopenharmony_ci				 0, HECI_H_GS1_ER_PREP);
74462306a36Sopenharmony_ci
74562306a36Sopenharmony_ci		/* make sure the reset bit is clear when writing the CSR reg */
74662306a36Sopenharmony_ci		intel_uncore_rmw(gt->uncore,
74762306a36Sopenharmony_ci				 HECI_H_CSR(MTL_GSC_HECI2_BASE),
74862306a36Sopenharmony_ci				 HECI_H_CSR_RST, HECI_H_CSR_IG);
74962306a36Sopenharmony_ci		msleep(200);
75062306a36Sopenharmony_ci	}
75162306a36Sopenharmony_ci
75262306a36Sopenharmony_ci	return engine_mask;
75362306a36Sopenharmony_ci}
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_cistatic void
75662306a36Sopenharmony_ciwa_14015076503_end(struct intel_gt *gt, intel_engine_mask_t engine_mask)
75762306a36Sopenharmony_ci{
75862306a36Sopenharmony_ci	if (!needs_wa_14015076503(gt, engine_mask))
75962306a36Sopenharmony_ci		return;
76062306a36Sopenharmony_ci
76162306a36Sopenharmony_ci	intel_uncore_rmw(gt->uncore,
76262306a36Sopenharmony_ci			 HECI_H_GS1(MTL_GSC_HECI2_BASE),
76362306a36Sopenharmony_ci			 HECI_H_GS1_ER_PREP, 0);
76462306a36Sopenharmony_ci}
76562306a36Sopenharmony_ci
76662306a36Sopenharmony_ciint __intel_gt_reset(struct intel_gt *gt, intel_engine_mask_t engine_mask)
76762306a36Sopenharmony_ci{
76862306a36Sopenharmony_ci	const int retries = engine_mask == ALL_ENGINES ? RESET_MAX_RETRIES : 1;
76962306a36Sopenharmony_ci	reset_func reset;
77062306a36Sopenharmony_ci	int ret = -ETIMEDOUT;
77162306a36Sopenharmony_ci	int retry;
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci	reset = intel_get_gpu_reset(gt);
77462306a36Sopenharmony_ci	if (!reset)
77562306a36Sopenharmony_ci		return -ENODEV;
77662306a36Sopenharmony_ci
77762306a36Sopenharmony_ci	/*
77862306a36Sopenharmony_ci	 * If the power well sleeps during the reset, the reset
77962306a36Sopenharmony_ci	 * request may be dropped and never completes (causing -EIO).
78062306a36Sopenharmony_ci	 */
78162306a36Sopenharmony_ci	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
78262306a36Sopenharmony_ci	for (retry = 0; ret == -ETIMEDOUT && retry < retries; retry++) {
78362306a36Sopenharmony_ci		intel_engine_mask_t reset_mask;
78462306a36Sopenharmony_ci
78562306a36Sopenharmony_ci		reset_mask = wa_14015076503_start(gt, engine_mask, !retry);
78662306a36Sopenharmony_ci
78762306a36Sopenharmony_ci		GT_TRACE(gt, "engine_mask=%x\n", reset_mask);
78862306a36Sopenharmony_ci		preempt_disable();
78962306a36Sopenharmony_ci		ret = reset(gt, reset_mask, retry);
79062306a36Sopenharmony_ci		preempt_enable();
79162306a36Sopenharmony_ci
79262306a36Sopenharmony_ci		wa_14015076503_end(gt, reset_mask);
79362306a36Sopenharmony_ci	}
79462306a36Sopenharmony_ci	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
79562306a36Sopenharmony_ci
79662306a36Sopenharmony_ci	return ret;
79762306a36Sopenharmony_ci}
79862306a36Sopenharmony_ci
79962306a36Sopenharmony_cibool intel_has_gpu_reset(const struct intel_gt *gt)
80062306a36Sopenharmony_ci{
80162306a36Sopenharmony_ci	if (!gt->i915->params.reset)
80262306a36Sopenharmony_ci		return NULL;
80362306a36Sopenharmony_ci
80462306a36Sopenharmony_ci	return intel_get_gpu_reset(gt);
80562306a36Sopenharmony_ci}
80662306a36Sopenharmony_ci
80762306a36Sopenharmony_cibool intel_has_reset_engine(const struct intel_gt *gt)
80862306a36Sopenharmony_ci{
80962306a36Sopenharmony_ci	if (gt->i915->params.reset < 2)
81062306a36Sopenharmony_ci		return false;
81162306a36Sopenharmony_ci
81262306a36Sopenharmony_ci	return INTEL_INFO(gt->i915)->has_reset_engine;
81362306a36Sopenharmony_ci}
81462306a36Sopenharmony_ci
81562306a36Sopenharmony_ciint intel_reset_guc(struct intel_gt *gt)
81662306a36Sopenharmony_ci{
81762306a36Sopenharmony_ci	int ret;
81862306a36Sopenharmony_ci
81962306a36Sopenharmony_ci	GEM_BUG_ON(!HAS_GT_UC(gt->i915));
82062306a36Sopenharmony_ci
82162306a36Sopenharmony_ci	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
82262306a36Sopenharmony_ci	ret = __reset_guc(gt);
82362306a36Sopenharmony_ci	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
82462306a36Sopenharmony_ci
82562306a36Sopenharmony_ci	return ret;
82662306a36Sopenharmony_ci}
82762306a36Sopenharmony_ci
82862306a36Sopenharmony_ci/*
82962306a36Sopenharmony_ci * Ensure irq handler finishes, and not run again.
83062306a36Sopenharmony_ci * Also return the active request so that we only search for it once.
83162306a36Sopenharmony_ci */
83262306a36Sopenharmony_cistatic void reset_prepare_engine(struct intel_engine_cs *engine)
83362306a36Sopenharmony_ci{
83462306a36Sopenharmony_ci	/*
83562306a36Sopenharmony_ci	 * During the reset sequence, we must prevent the engine from
83662306a36Sopenharmony_ci	 * entering RC6. As the context state is undefined until we restart
83762306a36Sopenharmony_ci	 * the engine, if it does enter RC6 during the reset, the state
83862306a36Sopenharmony_ci	 * written to the powercontext is undefined and so we may lose
83962306a36Sopenharmony_ci	 * GPU state upon resume, i.e. fail to restart after a reset.
84062306a36Sopenharmony_ci	 */
84162306a36Sopenharmony_ci	intel_uncore_forcewake_get(engine->uncore, FORCEWAKE_ALL);
84262306a36Sopenharmony_ci	if (engine->reset.prepare)
84362306a36Sopenharmony_ci		engine->reset.prepare(engine);
84462306a36Sopenharmony_ci}
84562306a36Sopenharmony_ci
84662306a36Sopenharmony_cistatic void revoke_mmaps(struct intel_gt *gt)
84762306a36Sopenharmony_ci{
84862306a36Sopenharmony_ci	int i;
84962306a36Sopenharmony_ci
85062306a36Sopenharmony_ci	for (i = 0; i < gt->ggtt->num_fences; i++) {
85162306a36Sopenharmony_ci		struct drm_vma_offset_node *node;
85262306a36Sopenharmony_ci		struct i915_vma *vma;
85362306a36Sopenharmony_ci		u64 vma_offset;
85462306a36Sopenharmony_ci
85562306a36Sopenharmony_ci		vma = READ_ONCE(gt->ggtt->fence_regs[i].vma);
85662306a36Sopenharmony_ci		if (!vma)
85762306a36Sopenharmony_ci			continue;
85862306a36Sopenharmony_ci
85962306a36Sopenharmony_ci		if (!i915_vma_has_userfault(vma))
86062306a36Sopenharmony_ci			continue;
86162306a36Sopenharmony_ci
86262306a36Sopenharmony_ci		GEM_BUG_ON(vma->fence != &gt->ggtt->fence_regs[i]);
86362306a36Sopenharmony_ci
86462306a36Sopenharmony_ci		if (!vma->mmo)
86562306a36Sopenharmony_ci			continue;
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_ci		node = &vma->mmo->vma_node;
86862306a36Sopenharmony_ci		vma_offset = vma->gtt_view.partial.offset << PAGE_SHIFT;
86962306a36Sopenharmony_ci
87062306a36Sopenharmony_ci		unmap_mapping_range(gt->i915->drm.anon_inode->i_mapping,
87162306a36Sopenharmony_ci				    drm_vma_node_offset_addr(node) + vma_offset,
87262306a36Sopenharmony_ci				    vma->size,
87362306a36Sopenharmony_ci				    1);
87462306a36Sopenharmony_ci	}
87562306a36Sopenharmony_ci}
87662306a36Sopenharmony_ci
87762306a36Sopenharmony_cistatic intel_engine_mask_t reset_prepare(struct intel_gt *gt)
87862306a36Sopenharmony_ci{
87962306a36Sopenharmony_ci	struct intel_engine_cs *engine;
88062306a36Sopenharmony_ci	intel_engine_mask_t awake = 0;
88162306a36Sopenharmony_ci	enum intel_engine_id id;
88262306a36Sopenharmony_ci
88362306a36Sopenharmony_ci	/* For GuC mode, ensure submission is disabled before stopping ring */
88462306a36Sopenharmony_ci	intel_uc_reset_prepare(&gt->uc);
88562306a36Sopenharmony_ci
88662306a36Sopenharmony_ci	for_each_engine(engine, gt, id) {
88762306a36Sopenharmony_ci		if (intel_engine_pm_get_if_awake(engine))
88862306a36Sopenharmony_ci			awake |= engine->mask;
88962306a36Sopenharmony_ci		reset_prepare_engine(engine);
89062306a36Sopenharmony_ci	}
89162306a36Sopenharmony_ci
89262306a36Sopenharmony_ci	return awake;
89362306a36Sopenharmony_ci}
89462306a36Sopenharmony_ci
89562306a36Sopenharmony_cistatic void gt_revoke(struct intel_gt *gt)
89662306a36Sopenharmony_ci{
89762306a36Sopenharmony_ci	revoke_mmaps(gt);
89862306a36Sopenharmony_ci}
89962306a36Sopenharmony_ci
90062306a36Sopenharmony_cistatic int gt_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
90162306a36Sopenharmony_ci{
90262306a36Sopenharmony_ci	struct intel_engine_cs *engine;
90362306a36Sopenharmony_ci	enum intel_engine_id id;
90462306a36Sopenharmony_ci	int err;
90562306a36Sopenharmony_ci
90662306a36Sopenharmony_ci	/*
90762306a36Sopenharmony_ci	 * Everything depends on having the GTT running, so we need to start
90862306a36Sopenharmony_ci	 * there.
90962306a36Sopenharmony_ci	 */
91062306a36Sopenharmony_ci	err = i915_ggtt_enable_hw(gt->i915);
91162306a36Sopenharmony_ci	if (err)
91262306a36Sopenharmony_ci		return err;
91362306a36Sopenharmony_ci
91462306a36Sopenharmony_ci	local_bh_disable();
91562306a36Sopenharmony_ci	for_each_engine(engine, gt, id)
91662306a36Sopenharmony_ci		__intel_engine_reset(engine, stalled_mask & engine->mask);
91762306a36Sopenharmony_ci	local_bh_enable();
91862306a36Sopenharmony_ci
91962306a36Sopenharmony_ci	intel_uc_reset(&gt->uc, ALL_ENGINES);
92062306a36Sopenharmony_ci
92162306a36Sopenharmony_ci	intel_ggtt_restore_fences(gt->ggtt);
92262306a36Sopenharmony_ci
92362306a36Sopenharmony_ci	return err;
92462306a36Sopenharmony_ci}
92562306a36Sopenharmony_ci
92662306a36Sopenharmony_cistatic void reset_finish_engine(struct intel_engine_cs *engine)
92762306a36Sopenharmony_ci{
92862306a36Sopenharmony_ci	if (engine->reset.finish)
92962306a36Sopenharmony_ci		engine->reset.finish(engine);
93062306a36Sopenharmony_ci	intel_uncore_forcewake_put(engine->uncore, FORCEWAKE_ALL);
93162306a36Sopenharmony_ci
93262306a36Sopenharmony_ci	intel_engine_signal_breadcrumbs(engine);
93362306a36Sopenharmony_ci}
93462306a36Sopenharmony_ci
93562306a36Sopenharmony_cistatic void reset_finish(struct intel_gt *gt, intel_engine_mask_t awake)
93662306a36Sopenharmony_ci{
93762306a36Sopenharmony_ci	struct intel_engine_cs *engine;
93862306a36Sopenharmony_ci	enum intel_engine_id id;
93962306a36Sopenharmony_ci
94062306a36Sopenharmony_ci	for_each_engine(engine, gt, id) {
94162306a36Sopenharmony_ci		reset_finish_engine(engine);
94262306a36Sopenharmony_ci		if (awake & engine->mask)
94362306a36Sopenharmony_ci			intel_engine_pm_put(engine);
94462306a36Sopenharmony_ci	}
94562306a36Sopenharmony_ci
94662306a36Sopenharmony_ci	intel_uc_reset_finish(&gt->uc);
94762306a36Sopenharmony_ci}
94862306a36Sopenharmony_ci
94962306a36Sopenharmony_cistatic void nop_submit_request(struct i915_request *request)
95062306a36Sopenharmony_ci{
95162306a36Sopenharmony_ci	RQ_TRACE(request, "-EIO\n");
95262306a36Sopenharmony_ci
95362306a36Sopenharmony_ci	request = i915_request_mark_eio(request);
95462306a36Sopenharmony_ci	if (request) {
95562306a36Sopenharmony_ci		i915_request_submit(request);
95662306a36Sopenharmony_ci		intel_engine_signal_breadcrumbs(request->engine);
95762306a36Sopenharmony_ci
95862306a36Sopenharmony_ci		i915_request_put(request);
95962306a36Sopenharmony_ci	}
96062306a36Sopenharmony_ci}
96162306a36Sopenharmony_ci
96262306a36Sopenharmony_cistatic void __intel_gt_set_wedged(struct intel_gt *gt)
96362306a36Sopenharmony_ci{
96462306a36Sopenharmony_ci	struct intel_engine_cs *engine;
96562306a36Sopenharmony_ci	intel_engine_mask_t awake;
96662306a36Sopenharmony_ci	enum intel_engine_id id;
96762306a36Sopenharmony_ci
96862306a36Sopenharmony_ci	if (test_bit(I915_WEDGED, &gt->reset.flags))
96962306a36Sopenharmony_ci		return;
97062306a36Sopenharmony_ci
97162306a36Sopenharmony_ci	GT_TRACE(gt, "start\n");
97262306a36Sopenharmony_ci
97362306a36Sopenharmony_ci	/*
97462306a36Sopenharmony_ci	 * First, stop submission to hw, but do not yet complete requests by
97562306a36Sopenharmony_ci	 * rolling the global seqno forward (since this would complete requests
97662306a36Sopenharmony_ci	 * for which we haven't set the fence error to EIO yet).
97762306a36Sopenharmony_ci	 */
97862306a36Sopenharmony_ci	awake = reset_prepare(gt);
97962306a36Sopenharmony_ci
98062306a36Sopenharmony_ci	/* Even if the GPU reset fails, it should still stop the engines */
98162306a36Sopenharmony_ci	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
98262306a36Sopenharmony_ci		__intel_gt_reset(gt, ALL_ENGINES);
98362306a36Sopenharmony_ci
98462306a36Sopenharmony_ci	for_each_engine(engine, gt, id)
98562306a36Sopenharmony_ci		engine->submit_request = nop_submit_request;
98662306a36Sopenharmony_ci
98762306a36Sopenharmony_ci	/*
98862306a36Sopenharmony_ci	 * Make sure no request can slip through without getting completed by
98962306a36Sopenharmony_ci	 * either this call here to intel_engine_write_global_seqno, or the one
99062306a36Sopenharmony_ci	 * in nop_submit_request.
99162306a36Sopenharmony_ci	 */
99262306a36Sopenharmony_ci	synchronize_rcu_expedited();
99362306a36Sopenharmony_ci	set_bit(I915_WEDGED, &gt->reset.flags);
99462306a36Sopenharmony_ci
99562306a36Sopenharmony_ci	/* Mark all executing requests as skipped */
99662306a36Sopenharmony_ci	local_bh_disable();
99762306a36Sopenharmony_ci	for_each_engine(engine, gt, id)
99862306a36Sopenharmony_ci		if (engine->reset.cancel)
99962306a36Sopenharmony_ci			engine->reset.cancel(engine);
100062306a36Sopenharmony_ci	intel_uc_cancel_requests(&gt->uc);
100162306a36Sopenharmony_ci	local_bh_enable();
100262306a36Sopenharmony_ci
100362306a36Sopenharmony_ci	reset_finish(gt, awake);
100462306a36Sopenharmony_ci
100562306a36Sopenharmony_ci	GT_TRACE(gt, "end\n");
100662306a36Sopenharmony_ci}
100762306a36Sopenharmony_ci
100862306a36Sopenharmony_civoid intel_gt_set_wedged(struct intel_gt *gt)
100962306a36Sopenharmony_ci{
101062306a36Sopenharmony_ci	intel_wakeref_t wakeref;
101162306a36Sopenharmony_ci
101262306a36Sopenharmony_ci	if (test_bit(I915_WEDGED, &gt->reset.flags))
101362306a36Sopenharmony_ci		return;
101462306a36Sopenharmony_ci
101562306a36Sopenharmony_ci	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
101662306a36Sopenharmony_ci	mutex_lock(&gt->reset.mutex);
101762306a36Sopenharmony_ci
101862306a36Sopenharmony_ci	if (GEM_SHOW_DEBUG()) {
101962306a36Sopenharmony_ci		struct drm_printer p = drm_debug_printer(__func__);
102062306a36Sopenharmony_ci		struct intel_engine_cs *engine;
102162306a36Sopenharmony_ci		enum intel_engine_id id;
102262306a36Sopenharmony_ci
102362306a36Sopenharmony_ci		drm_printf(&p, "called from %pS\n", (void *)_RET_IP_);
102462306a36Sopenharmony_ci		for_each_engine(engine, gt, id) {
102562306a36Sopenharmony_ci			if (intel_engine_is_idle(engine))
102662306a36Sopenharmony_ci				continue;
102762306a36Sopenharmony_ci
102862306a36Sopenharmony_ci			intel_engine_dump(engine, &p, "%s\n", engine->name);
102962306a36Sopenharmony_ci		}
103062306a36Sopenharmony_ci	}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci	__intel_gt_set_wedged(gt);
103362306a36Sopenharmony_ci
103462306a36Sopenharmony_ci	mutex_unlock(&gt->reset.mutex);
103562306a36Sopenharmony_ci	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
103662306a36Sopenharmony_ci}
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_cistatic bool __intel_gt_unset_wedged(struct intel_gt *gt)
103962306a36Sopenharmony_ci{
104062306a36Sopenharmony_ci	struct intel_gt_timelines *timelines = &gt->timelines;
104162306a36Sopenharmony_ci	struct intel_timeline *tl;
104262306a36Sopenharmony_ci	bool ok;
104362306a36Sopenharmony_ci
104462306a36Sopenharmony_ci	if (!test_bit(I915_WEDGED, &gt->reset.flags))
104562306a36Sopenharmony_ci		return true;
104662306a36Sopenharmony_ci
104762306a36Sopenharmony_ci	/* Never fully initialised, recovery impossible */
104862306a36Sopenharmony_ci	if (intel_gt_has_unrecoverable_error(gt))
104962306a36Sopenharmony_ci		return false;
105062306a36Sopenharmony_ci
105162306a36Sopenharmony_ci	GT_TRACE(gt, "start\n");
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	/*
105462306a36Sopenharmony_ci	 * Before unwedging, make sure that all pending operations
105562306a36Sopenharmony_ci	 * are flushed and errored out - we may have requests waiting upon
105662306a36Sopenharmony_ci	 * third party fences. We marked all inflight requests as EIO, and
105762306a36Sopenharmony_ci	 * every execbuf since returned EIO, for consistency we want all
105862306a36Sopenharmony_ci	 * the currently pending requests to also be marked as EIO, which
105962306a36Sopenharmony_ci	 * is done inside our nop_submit_request - and so we must wait.
106062306a36Sopenharmony_ci	 *
106162306a36Sopenharmony_ci	 * No more can be submitted until we reset the wedged bit.
106262306a36Sopenharmony_ci	 */
106362306a36Sopenharmony_ci	spin_lock(&timelines->lock);
106462306a36Sopenharmony_ci	list_for_each_entry(tl, &timelines->active_list, link) {
106562306a36Sopenharmony_ci		struct dma_fence *fence;
106662306a36Sopenharmony_ci
106762306a36Sopenharmony_ci		fence = i915_active_fence_get(&tl->last_request);
106862306a36Sopenharmony_ci		if (!fence)
106962306a36Sopenharmony_ci			continue;
107062306a36Sopenharmony_ci
107162306a36Sopenharmony_ci		spin_unlock(&timelines->lock);
107262306a36Sopenharmony_ci
107362306a36Sopenharmony_ci		/*
107462306a36Sopenharmony_ci		 * All internal dependencies (i915_requests) will have
107562306a36Sopenharmony_ci		 * been flushed by the set-wedge, but we may be stuck waiting
107662306a36Sopenharmony_ci		 * for external fences. These should all be capped to 10s
107762306a36Sopenharmony_ci		 * (I915_FENCE_TIMEOUT) so this wait should not be unbounded
107862306a36Sopenharmony_ci		 * in the worst case.
107962306a36Sopenharmony_ci		 */
108062306a36Sopenharmony_ci		dma_fence_default_wait(fence, false, MAX_SCHEDULE_TIMEOUT);
108162306a36Sopenharmony_ci		dma_fence_put(fence);
108262306a36Sopenharmony_ci
108362306a36Sopenharmony_ci		/* Restart iteration after droping lock */
108462306a36Sopenharmony_ci		spin_lock(&timelines->lock);
108562306a36Sopenharmony_ci		tl = list_entry(&timelines->active_list, typeof(*tl), link);
108662306a36Sopenharmony_ci	}
108762306a36Sopenharmony_ci	spin_unlock(&timelines->lock);
108862306a36Sopenharmony_ci
108962306a36Sopenharmony_ci	/* We must reset pending GPU events before restoring our submission */
109062306a36Sopenharmony_ci	ok = !HAS_EXECLISTS(gt->i915); /* XXX better agnosticism desired */
109162306a36Sopenharmony_ci	if (!INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
109262306a36Sopenharmony_ci		ok = __intel_gt_reset(gt, ALL_ENGINES) == 0;
109362306a36Sopenharmony_ci	if (!ok) {
109462306a36Sopenharmony_ci		/*
109562306a36Sopenharmony_ci		 * Warn CI about the unrecoverable wedged condition.
109662306a36Sopenharmony_ci		 * Time for a reboot.
109762306a36Sopenharmony_ci		 */
109862306a36Sopenharmony_ci		add_taint_for_CI(gt->i915, TAINT_WARN);
109962306a36Sopenharmony_ci		return false;
110062306a36Sopenharmony_ci	}
110162306a36Sopenharmony_ci
110262306a36Sopenharmony_ci	/*
110362306a36Sopenharmony_ci	 * Undo nop_submit_request. We prevent all new i915 requests from
110462306a36Sopenharmony_ci	 * being queued (by disallowing execbuf whilst wedged) so having
110562306a36Sopenharmony_ci	 * waited for all active requests above, we know the system is idle
110662306a36Sopenharmony_ci	 * and do not have to worry about a thread being inside
110762306a36Sopenharmony_ci	 * engine->submit_request() as we swap over. So unlike installing
110862306a36Sopenharmony_ci	 * the nop_submit_request on reset, we can do this from normal
110962306a36Sopenharmony_ci	 * context and do not require stop_machine().
111062306a36Sopenharmony_ci	 */
111162306a36Sopenharmony_ci	intel_engines_reset_default_submission(gt);
111262306a36Sopenharmony_ci
111362306a36Sopenharmony_ci	GT_TRACE(gt, "end\n");
111462306a36Sopenharmony_ci
111562306a36Sopenharmony_ci	smp_mb__before_atomic(); /* complete takeover before enabling execbuf */
111662306a36Sopenharmony_ci	clear_bit(I915_WEDGED, &gt->reset.flags);
111762306a36Sopenharmony_ci
111862306a36Sopenharmony_ci	return true;
111962306a36Sopenharmony_ci}
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_cibool intel_gt_unset_wedged(struct intel_gt *gt)
112262306a36Sopenharmony_ci{
112362306a36Sopenharmony_ci	bool result;
112462306a36Sopenharmony_ci
112562306a36Sopenharmony_ci	mutex_lock(&gt->reset.mutex);
112662306a36Sopenharmony_ci	result = __intel_gt_unset_wedged(gt);
112762306a36Sopenharmony_ci	mutex_unlock(&gt->reset.mutex);
112862306a36Sopenharmony_ci
112962306a36Sopenharmony_ci	return result;
113062306a36Sopenharmony_ci}
113162306a36Sopenharmony_ci
113262306a36Sopenharmony_cistatic int do_reset(struct intel_gt *gt, intel_engine_mask_t stalled_mask)
113362306a36Sopenharmony_ci{
113462306a36Sopenharmony_ci	int err, i;
113562306a36Sopenharmony_ci
113662306a36Sopenharmony_ci	err = __intel_gt_reset(gt, ALL_ENGINES);
113762306a36Sopenharmony_ci	for (i = 0; err && i < RESET_MAX_RETRIES; i++) {
113862306a36Sopenharmony_ci		msleep(10 * (i + 1));
113962306a36Sopenharmony_ci		err = __intel_gt_reset(gt, ALL_ENGINES);
114062306a36Sopenharmony_ci	}
114162306a36Sopenharmony_ci	if (err)
114262306a36Sopenharmony_ci		return err;
114362306a36Sopenharmony_ci
114462306a36Sopenharmony_ci	return gt_reset(gt, stalled_mask);
114562306a36Sopenharmony_ci}
114662306a36Sopenharmony_ci
114762306a36Sopenharmony_cistatic int resume(struct intel_gt *gt)
114862306a36Sopenharmony_ci{
114962306a36Sopenharmony_ci	struct intel_engine_cs *engine;
115062306a36Sopenharmony_ci	enum intel_engine_id id;
115162306a36Sopenharmony_ci	int ret;
115262306a36Sopenharmony_ci
115362306a36Sopenharmony_ci	for_each_engine(engine, gt, id) {
115462306a36Sopenharmony_ci		ret = intel_engine_resume(engine);
115562306a36Sopenharmony_ci		if (ret)
115662306a36Sopenharmony_ci			return ret;
115762306a36Sopenharmony_ci	}
115862306a36Sopenharmony_ci
115962306a36Sopenharmony_ci	return 0;
116062306a36Sopenharmony_ci}
116162306a36Sopenharmony_ci
116262306a36Sopenharmony_ci/**
116362306a36Sopenharmony_ci * intel_gt_reset - reset chip after a hang
116462306a36Sopenharmony_ci * @gt: #intel_gt to reset
116562306a36Sopenharmony_ci * @stalled_mask: mask of the stalled engines with the guilty requests
116662306a36Sopenharmony_ci * @reason: user error message for why we are resetting
116762306a36Sopenharmony_ci *
116862306a36Sopenharmony_ci * Reset the chip.  Useful if a hang is detected. Marks the device as wedged
116962306a36Sopenharmony_ci * on failure.
117062306a36Sopenharmony_ci *
117162306a36Sopenharmony_ci * Procedure is fairly simple:
117262306a36Sopenharmony_ci *   - reset the chip using the reset reg
117362306a36Sopenharmony_ci *   - re-init context state
117462306a36Sopenharmony_ci *   - re-init hardware status page
117562306a36Sopenharmony_ci *   - re-init ring buffer
117662306a36Sopenharmony_ci *   - re-init interrupt state
117762306a36Sopenharmony_ci *   - re-init display
117862306a36Sopenharmony_ci */
117962306a36Sopenharmony_civoid intel_gt_reset(struct intel_gt *gt,
118062306a36Sopenharmony_ci		    intel_engine_mask_t stalled_mask,
118162306a36Sopenharmony_ci		    const char *reason)
118262306a36Sopenharmony_ci{
118362306a36Sopenharmony_ci	intel_engine_mask_t awake;
118462306a36Sopenharmony_ci	int ret;
118562306a36Sopenharmony_ci
118662306a36Sopenharmony_ci	GT_TRACE(gt, "flags=%lx\n", gt->reset.flags);
118762306a36Sopenharmony_ci
118862306a36Sopenharmony_ci	might_sleep();
118962306a36Sopenharmony_ci	GEM_BUG_ON(!test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
119062306a36Sopenharmony_ci
119162306a36Sopenharmony_ci	/*
119262306a36Sopenharmony_ci	 * FIXME: Revoking cpu mmap ptes cannot be done from a dma_fence
119362306a36Sopenharmony_ci	 * critical section like gpu reset.
119462306a36Sopenharmony_ci	 */
119562306a36Sopenharmony_ci	gt_revoke(gt);
119662306a36Sopenharmony_ci
119762306a36Sopenharmony_ci	mutex_lock(&gt->reset.mutex);
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci	/* Clear any previous failed attempts at recovery. Time to try again. */
120062306a36Sopenharmony_ci	if (!__intel_gt_unset_wedged(gt))
120162306a36Sopenharmony_ci		goto unlock;
120262306a36Sopenharmony_ci
120362306a36Sopenharmony_ci	if (reason)
120462306a36Sopenharmony_ci		drm_notice(&gt->i915->drm,
120562306a36Sopenharmony_ci			   "Resetting chip for %s\n", reason);
120662306a36Sopenharmony_ci	atomic_inc(&gt->i915->gpu_error.reset_count);
120762306a36Sopenharmony_ci
120862306a36Sopenharmony_ci	awake = reset_prepare(gt);
120962306a36Sopenharmony_ci
121062306a36Sopenharmony_ci	if (!intel_has_gpu_reset(gt)) {
121162306a36Sopenharmony_ci		if (gt->i915->params.reset)
121262306a36Sopenharmony_ci			drm_err(&gt->i915->drm, "GPU reset not supported\n");
121362306a36Sopenharmony_ci		else
121462306a36Sopenharmony_ci			drm_dbg(&gt->i915->drm, "GPU reset disabled\n");
121562306a36Sopenharmony_ci		goto error;
121662306a36Sopenharmony_ci	}
121762306a36Sopenharmony_ci
121862306a36Sopenharmony_ci	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
121962306a36Sopenharmony_ci		intel_runtime_pm_disable_interrupts(gt->i915);
122062306a36Sopenharmony_ci
122162306a36Sopenharmony_ci	if (do_reset(gt, stalled_mask)) {
122262306a36Sopenharmony_ci		drm_err(&gt->i915->drm, "Failed to reset chip\n");
122362306a36Sopenharmony_ci		goto taint;
122462306a36Sopenharmony_ci	}
122562306a36Sopenharmony_ci
122662306a36Sopenharmony_ci	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
122762306a36Sopenharmony_ci		intel_runtime_pm_enable_interrupts(gt->i915);
122862306a36Sopenharmony_ci
122962306a36Sopenharmony_ci	intel_overlay_reset(gt->i915);
123062306a36Sopenharmony_ci
123162306a36Sopenharmony_ci	/*
123262306a36Sopenharmony_ci	 * Next we need to restore the context, but we don't use those
123362306a36Sopenharmony_ci	 * yet either...
123462306a36Sopenharmony_ci	 *
123562306a36Sopenharmony_ci	 * Ring buffer needs to be re-initialized in the KMS case, or if X
123662306a36Sopenharmony_ci	 * was running at the time of the reset (i.e. we weren't VT
123762306a36Sopenharmony_ci	 * switched away).
123862306a36Sopenharmony_ci	 */
123962306a36Sopenharmony_ci	ret = intel_gt_init_hw(gt);
124062306a36Sopenharmony_ci	if (ret) {
124162306a36Sopenharmony_ci		drm_err(&gt->i915->drm,
124262306a36Sopenharmony_ci			"Failed to initialise HW following reset (%d)\n",
124362306a36Sopenharmony_ci			ret);
124462306a36Sopenharmony_ci		goto taint;
124562306a36Sopenharmony_ci	}
124662306a36Sopenharmony_ci
124762306a36Sopenharmony_ci	ret = resume(gt);
124862306a36Sopenharmony_ci	if (ret)
124962306a36Sopenharmony_ci		goto taint;
125062306a36Sopenharmony_ci
125162306a36Sopenharmony_cifinish:
125262306a36Sopenharmony_ci	reset_finish(gt, awake);
125362306a36Sopenharmony_ciunlock:
125462306a36Sopenharmony_ci	mutex_unlock(&gt->reset.mutex);
125562306a36Sopenharmony_ci	return;
125662306a36Sopenharmony_ci
125762306a36Sopenharmony_citaint:
125862306a36Sopenharmony_ci	/*
125962306a36Sopenharmony_ci	 * History tells us that if we cannot reset the GPU now, we
126062306a36Sopenharmony_ci	 * never will. This then impacts everything that is run
126162306a36Sopenharmony_ci	 * subsequently. On failing the reset, we mark the driver
126262306a36Sopenharmony_ci	 * as wedged, preventing further execution on the GPU.
126362306a36Sopenharmony_ci	 * We also want to go one step further and add a taint to the
126462306a36Sopenharmony_ci	 * kernel so that any subsequent faults can be traced back to
126562306a36Sopenharmony_ci	 * this failure. This is important for CI, where if the
126662306a36Sopenharmony_ci	 * GPU/driver fails we would like to reboot and restart testing
126762306a36Sopenharmony_ci	 * rather than continue on into oblivion. For everyone else,
126862306a36Sopenharmony_ci	 * the system should still plod along, but they have been warned!
126962306a36Sopenharmony_ci	 */
127062306a36Sopenharmony_ci	add_taint_for_CI(gt->i915, TAINT_WARN);
127162306a36Sopenharmony_cierror:
127262306a36Sopenharmony_ci	__intel_gt_set_wedged(gt);
127362306a36Sopenharmony_ci	goto finish;
127462306a36Sopenharmony_ci}
127562306a36Sopenharmony_ci
127662306a36Sopenharmony_cistatic int intel_gt_reset_engine(struct intel_engine_cs *engine)
127762306a36Sopenharmony_ci{
127862306a36Sopenharmony_ci	return __intel_gt_reset(engine->gt, engine->mask);
127962306a36Sopenharmony_ci}
128062306a36Sopenharmony_ci
128162306a36Sopenharmony_ciint __intel_engine_reset_bh(struct intel_engine_cs *engine, const char *msg)
128262306a36Sopenharmony_ci{
128362306a36Sopenharmony_ci	struct intel_gt *gt = engine->gt;
128462306a36Sopenharmony_ci	int ret;
128562306a36Sopenharmony_ci
128662306a36Sopenharmony_ci	ENGINE_TRACE(engine, "flags=%lx\n", gt->reset.flags);
128762306a36Sopenharmony_ci	GEM_BUG_ON(!test_bit(I915_RESET_ENGINE + engine->id, &gt->reset.flags));
128862306a36Sopenharmony_ci
128962306a36Sopenharmony_ci	if (intel_engine_uses_guc(engine))
129062306a36Sopenharmony_ci		return -ENODEV;
129162306a36Sopenharmony_ci
129262306a36Sopenharmony_ci	if (!intel_engine_pm_get_if_awake(engine))
129362306a36Sopenharmony_ci		return 0;
129462306a36Sopenharmony_ci
129562306a36Sopenharmony_ci	reset_prepare_engine(engine);
129662306a36Sopenharmony_ci
129762306a36Sopenharmony_ci	if (msg)
129862306a36Sopenharmony_ci		drm_notice(&engine->i915->drm,
129962306a36Sopenharmony_ci			   "Resetting %s for %s\n", engine->name, msg);
130062306a36Sopenharmony_ci	i915_increase_reset_engine_count(&engine->i915->gpu_error, engine);
130162306a36Sopenharmony_ci
130262306a36Sopenharmony_ci	ret = intel_gt_reset_engine(engine);
130362306a36Sopenharmony_ci	if (ret) {
130462306a36Sopenharmony_ci		/* If we fail here, we expect to fallback to a global reset */
130562306a36Sopenharmony_ci		ENGINE_TRACE(engine, "Failed to reset %s, err: %d\n", engine->name, ret);
130662306a36Sopenharmony_ci		goto out;
130762306a36Sopenharmony_ci	}
130862306a36Sopenharmony_ci
130962306a36Sopenharmony_ci	/*
131062306a36Sopenharmony_ci	 * The request that caused the hang is stuck on elsp, we know the
131162306a36Sopenharmony_ci	 * active request and can drop it, adjust head to skip the offending
131262306a36Sopenharmony_ci	 * request to resume executing remaining requests in the queue.
131362306a36Sopenharmony_ci	 */
131462306a36Sopenharmony_ci	__intel_engine_reset(engine, true);
131562306a36Sopenharmony_ci
131662306a36Sopenharmony_ci	/*
131762306a36Sopenharmony_ci	 * The engine and its registers (and workarounds in case of render)
131862306a36Sopenharmony_ci	 * have been reset to their default values. Follow the init_ring
131962306a36Sopenharmony_ci	 * process to program RING_MODE, HWSP and re-enable submission.
132062306a36Sopenharmony_ci	 */
132162306a36Sopenharmony_ci	ret = intel_engine_resume(engine);
132262306a36Sopenharmony_ci
132362306a36Sopenharmony_ciout:
132462306a36Sopenharmony_ci	intel_engine_cancel_stop_cs(engine);
132562306a36Sopenharmony_ci	reset_finish_engine(engine);
132662306a36Sopenharmony_ci	intel_engine_pm_put_async(engine);
132762306a36Sopenharmony_ci	return ret;
132862306a36Sopenharmony_ci}
132962306a36Sopenharmony_ci
133062306a36Sopenharmony_ci/**
133162306a36Sopenharmony_ci * intel_engine_reset - reset GPU engine to recover from a hang
133262306a36Sopenharmony_ci * @engine: engine to reset
133362306a36Sopenharmony_ci * @msg: reason for GPU reset; or NULL for no drm_notice()
133462306a36Sopenharmony_ci *
133562306a36Sopenharmony_ci * Reset a specific GPU engine. Useful if a hang is detected.
133662306a36Sopenharmony_ci * Returns zero on successful reset or otherwise an error code.
133762306a36Sopenharmony_ci *
133862306a36Sopenharmony_ci * Procedure is:
133962306a36Sopenharmony_ci *  - identifies the request that caused the hang and it is dropped
134062306a36Sopenharmony_ci *  - reset engine (which will force the engine to idle)
134162306a36Sopenharmony_ci *  - re-init/configure engine
134262306a36Sopenharmony_ci */
134362306a36Sopenharmony_ciint intel_engine_reset(struct intel_engine_cs *engine, const char *msg)
134462306a36Sopenharmony_ci{
134562306a36Sopenharmony_ci	int err;
134662306a36Sopenharmony_ci
134762306a36Sopenharmony_ci	local_bh_disable();
134862306a36Sopenharmony_ci	err = __intel_engine_reset_bh(engine, msg);
134962306a36Sopenharmony_ci	local_bh_enable();
135062306a36Sopenharmony_ci
135162306a36Sopenharmony_ci	return err;
135262306a36Sopenharmony_ci}
135362306a36Sopenharmony_ci
135462306a36Sopenharmony_cistatic void intel_gt_reset_global(struct intel_gt *gt,
135562306a36Sopenharmony_ci				  u32 engine_mask,
135662306a36Sopenharmony_ci				  const char *reason)
135762306a36Sopenharmony_ci{
135862306a36Sopenharmony_ci	struct kobject *kobj = &gt->i915->drm.primary->kdev->kobj;
135962306a36Sopenharmony_ci	char *error_event[] = { I915_ERROR_UEVENT "=1", NULL };
136062306a36Sopenharmony_ci	char *reset_event[] = { I915_RESET_UEVENT "=1", NULL };
136162306a36Sopenharmony_ci	char *reset_done_event[] = { I915_ERROR_UEVENT "=0", NULL };
136262306a36Sopenharmony_ci	struct intel_wedge_me w;
136362306a36Sopenharmony_ci
136462306a36Sopenharmony_ci	kobject_uevent_env(kobj, KOBJ_CHANGE, error_event);
136562306a36Sopenharmony_ci
136662306a36Sopenharmony_ci	GT_TRACE(gt, "resetting chip, engines=%x\n", engine_mask);
136762306a36Sopenharmony_ci	kobject_uevent_env(kobj, KOBJ_CHANGE, reset_event);
136862306a36Sopenharmony_ci
136962306a36Sopenharmony_ci	/* Use a watchdog to ensure that our reset completes */
137062306a36Sopenharmony_ci	intel_wedge_on_timeout(&w, gt, 60 * HZ) {
137162306a36Sopenharmony_ci		intel_display_reset_prepare(gt->i915);
137262306a36Sopenharmony_ci
137362306a36Sopenharmony_ci		intel_gt_reset(gt, engine_mask, reason);
137462306a36Sopenharmony_ci
137562306a36Sopenharmony_ci		intel_display_reset_finish(gt->i915);
137662306a36Sopenharmony_ci	}
137762306a36Sopenharmony_ci
137862306a36Sopenharmony_ci	if (!test_bit(I915_WEDGED, &gt->reset.flags))
137962306a36Sopenharmony_ci		kobject_uevent_env(kobj, KOBJ_CHANGE, reset_done_event);
138062306a36Sopenharmony_ci}
138162306a36Sopenharmony_ci
138262306a36Sopenharmony_ci/**
138362306a36Sopenharmony_ci * intel_gt_handle_error - handle a gpu error
138462306a36Sopenharmony_ci * @gt: the intel_gt
138562306a36Sopenharmony_ci * @engine_mask: mask representing engines that are hung
138662306a36Sopenharmony_ci * @flags: control flags
138762306a36Sopenharmony_ci * @fmt: Error message format string
138862306a36Sopenharmony_ci *
138962306a36Sopenharmony_ci * Do some basic checking of register state at error time and
139062306a36Sopenharmony_ci * dump it to the syslog.  Also call i915_capture_error_state() to make
139162306a36Sopenharmony_ci * sure we get a record and make it available in debugfs.  Fire a uevent
139262306a36Sopenharmony_ci * so userspace knows something bad happened (should trigger collection
139362306a36Sopenharmony_ci * of a ring dump etc.).
139462306a36Sopenharmony_ci */
139562306a36Sopenharmony_civoid intel_gt_handle_error(struct intel_gt *gt,
139662306a36Sopenharmony_ci			   intel_engine_mask_t engine_mask,
139762306a36Sopenharmony_ci			   unsigned long flags,
139862306a36Sopenharmony_ci			   const char *fmt, ...)
139962306a36Sopenharmony_ci{
140062306a36Sopenharmony_ci	struct intel_engine_cs *engine;
140162306a36Sopenharmony_ci	intel_wakeref_t wakeref;
140262306a36Sopenharmony_ci	intel_engine_mask_t tmp;
140362306a36Sopenharmony_ci	char error_msg[80];
140462306a36Sopenharmony_ci	char *msg = NULL;
140562306a36Sopenharmony_ci
140662306a36Sopenharmony_ci	if (fmt) {
140762306a36Sopenharmony_ci		va_list args;
140862306a36Sopenharmony_ci
140962306a36Sopenharmony_ci		va_start(args, fmt);
141062306a36Sopenharmony_ci		vscnprintf(error_msg, sizeof(error_msg), fmt, args);
141162306a36Sopenharmony_ci		va_end(args);
141262306a36Sopenharmony_ci
141362306a36Sopenharmony_ci		msg = error_msg;
141462306a36Sopenharmony_ci	}
141562306a36Sopenharmony_ci
141662306a36Sopenharmony_ci	/*
141762306a36Sopenharmony_ci	 * In most cases it's guaranteed that we get here with an RPM
141862306a36Sopenharmony_ci	 * reference held, for example because there is a pending GPU
141962306a36Sopenharmony_ci	 * request that won't finish until the reset is done. This
142062306a36Sopenharmony_ci	 * isn't the case at least when we get here by doing a
142162306a36Sopenharmony_ci	 * simulated reset via debugfs, so get an RPM reference.
142262306a36Sopenharmony_ci	 */
142362306a36Sopenharmony_ci	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
142462306a36Sopenharmony_ci
142562306a36Sopenharmony_ci	engine_mask &= gt->info.engine_mask;
142662306a36Sopenharmony_ci
142762306a36Sopenharmony_ci	if (flags & I915_ERROR_CAPTURE) {
142862306a36Sopenharmony_ci		i915_capture_error_state(gt, engine_mask, CORE_DUMP_FLAG_NONE);
142962306a36Sopenharmony_ci		intel_gt_clear_error_registers(gt, engine_mask);
143062306a36Sopenharmony_ci	}
143162306a36Sopenharmony_ci
143262306a36Sopenharmony_ci	/*
143362306a36Sopenharmony_ci	 * Try engine reset when available. We fall back to full reset if
143462306a36Sopenharmony_ci	 * single reset fails.
143562306a36Sopenharmony_ci	 */
143662306a36Sopenharmony_ci	if (!intel_uc_uses_guc_submission(&gt->uc) &&
143762306a36Sopenharmony_ci	    intel_has_reset_engine(gt) && !intel_gt_is_wedged(gt)) {
143862306a36Sopenharmony_ci		local_bh_disable();
143962306a36Sopenharmony_ci		for_each_engine_masked(engine, gt, engine_mask, tmp) {
144062306a36Sopenharmony_ci			BUILD_BUG_ON(I915_RESET_MODESET >= I915_RESET_ENGINE);
144162306a36Sopenharmony_ci			if (test_and_set_bit(I915_RESET_ENGINE + engine->id,
144262306a36Sopenharmony_ci					     &gt->reset.flags))
144362306a36Sopenharmony_ci				continue;
144462306a36Sopenharmony_ci
144562306a36Sopenharmony_ci			if (__intel_engine_reset_bh(engine, msg) == 0)
144662306a36Sopenharmony_ci				engine_mask &= ~engine->mask;
144762306a36Sopenharmony_ci
144862306a36Sopenharmony_ci			clear_and_wake_up_bit(I915_RESET_ENGINE + engine->id,
144962306a36Sopenharmony_ci					      &gt->reset.flags);
145062306a36Sopenharmony_ci		}
145162306a36Sopenharmony_ci		local_bh_enable();
145262306a36Sopenharmony_ci	}
145362306a36Sopenharmony_ci
145462306a36Sopenharmony_ci	if (!engine_mask)
145562306a36Sopenharmony_ci		goto out;
145662306a36Sopenharmony_ci
145762306a36Sopenharmony_ci	/* Full reset needs the mutex, stop any other user trying to do so. */
145862306a36Sopenharmony_ci	if (test_and_set_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
145962306a36Sopenharmony_ci		wait_event(gt->reset.queue,
146062306a36Sopenharmony_ci			   !test_bit(I915_RESET_BACKOFF, &gt->reset.flags));
146162306a36Sopenharmony_ci		goto out; /* piggy-back on the other reset */
146262306a36Sopenharmony_ci	}
146362306a36Sopenharmony_ci
146462306a36Sopenharmony_ci	/* Make sure i915_reset_trylock() sees the I915_RESET_BACKOFF */
146562306a36Sopenharmony_ci	synchronize_rcu_expedited();
146662306a36Sopenharmony_ci
146762306a36Sopenharmony_ci	/*
146862306a36Sopenharmony_ci	 * Prevent any other reset-engine attempt. We don't do this for GuC
146962306a36Sopenharmony_ci	 * submission the GuC owns the per-engine reset, not the i915.
147062306a36Sopenharmony_ci	 */
147162306a36Sopenharmony_ci	if (!intel_uc_uses_guc_submission(&gt->uc)) {
147262306a36Sopenharmony_ci		for_each_engine(engine, gt, tmp) {
147362306a36Sopenharmony_ci			while (test_and_set_bit(I915_RESET_ENGINE + engine->id,
147462306a36Sopenharmony_ci						&gt->reset.flags))
147562306a36Sopenharmony_ci				wait_on_bit(&gt->reset.flags,
147662306a36Sopenharmony_ci					    I915_RESET_ENGINE + engine->id,
147762306a36Sopenharmony_ci					    TASK_UNINTERRUPTIBLE);
147862306a36Sopenharmony_ci		}
147962306a36Sopenharmony_ci	}
148062306a36Sopenharmony_ci
148162306a36Sopenharmony_ci	/* Flush everyone using a resource about to be clobbered */
148262306a36Sopenharmony_ci	synchronize_srcu_expedited(&gt->reset.backoff_srcu);
148362306a36Sopenharmony_ci
148462306a36Sopenharmony_ci	intel_gt_reset_global(gt, engine_mask, msg);
148562306a36Sopenharmony_ci
148662306a36Sopenharmony_ci	if (!intel_uc_uses_guc_submission(&gt->uc)) {
148762306a36Sopenharmony_ci		for_each_engine(engine, gt, tmp)
148862306a36Sopenharmony_ci			clear_bit_unlock(I915_RESET_ENGINE + engine->id,
148962306a36Sopenharmony_ci					 &gt->reset.flags);
149062306a36Sopenharmony_ci	}
149162306a36Sopenharmony_ci	clear_bit_unlock(I915_RESET_BACKOFF, &gt->reset.flags);
149262306a36Sopenharmony_ci	smp_mb__after_atomic();
149362306a36Sopenharmony_ci	wake_up_all(&gt->reset.queue);
149462306a36Sopenharmony_ci
149562306a36Sopenharmony_ciout:
149662306a36Sopenharmony_ci	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
149762306a36Sopenharmony_ci}
149862306a36Sopenharmony_ci
149962306a36Sopenharmony_cistatic int _intel_gt_reset_lock(struct intel_gt *gt, int *srcu, bool retry)
150062306a36Sopenharmony_ci{
150162306a36Sopenharmony_ci	might_lock(&gt->reset.backoff_srcu);
150262306a36Sopenharmony_ci	if (retry)
150362306a36Sopenharmony_ci		might_sleep();
150462306a36Sopenharmony_ci
150562306a36Sopenharmony_ci	rcu_read_lock();
150662306a36Sopenharmony_ci	while (test_bit(I915_RESET_BACKOFF, &gt->reset.flags)) {
150762306a36Sopenharmony_ci		rcu_read_unlock();
150862306a36Sopenharmony_ci
150962306a36Sopenharmony_ci		if (!retry)
151062306a36Sopenharmony_ci			return -EBUSY;
151162306a36Sopenharmony_ci
151262306a36Sopenharmony_ci		if (wait_event_interruptible(gt->reset.queue,
151362306a36Sopenharmony_ci					     !test_bit(I915_RESET_BACKOFF,
151462306a36Sopenharmony_ci						       &gt->reset.flags)))
151562306a36Sopenharmony_ci			return -EINTR;
151662306a36Sopenharmony_ci
151762306a36Sopenharmony_ci		rcu_read_lock();
151862306a36Sopenharmony_ci	}
151962306a36Sopenharmony_ci	*srcu = srcu_read_lock(&gt->reset.backoff_srcu);
152062306a36Sopenharmony_ci	rcu_read_unlock();
152162306a36Sopenharmony_ci
152262306a36Sopenharmony_ci	return 0;
152362306a36Sopenharmony_ci}
152462306a36Sopenharmony_ci
152562306a36Sopenharmony_ciint intel_gt_reset_trylock(struct intel_gt *gt, int *srcu)
152662306a36Sopenharmony_ci{
152762306a36Sopenharmony_ci	return _intel_gt_reset_lock(gt, srcu, false);
152862306a36Sopenharmony_ci}
152962306a36Sopenharmony_ci
153062306a36Sopenharmony_ciint intel_gt_reset_lock_interruptible(struct intel_gt *gt, int *srcu)
153162306a36Sopenharmony_ci{
153262306a36Sopenharmony_ci	return _intel_gt_reset_lock(gt, srcu, true);
153362306a36Sopenharmony_ci}
153462306a36Sopenharmony_ci
153562306a36Sopenharmony_civoid intel_gt_reset_unlock(struct intel_gt *gt, int tag)
153662306a36Sopenharmony_ci__releases(&gt->reset.backoff_srcu)
153762306a36Sopenharmony_ci{
153862306a36Sopenharmony_ci	srcu_read_unlock(&gt->reset.backoff_srcu, tag);
153962306a36Sopenharmony_ci}
154062306a36Sopenharmony_ci
154162306a36Sopenharmony_ciint intel_gt_terminally_wedged(struct intel_gt *gt)
154262306a36Sopenharmony_ci{
154362306a36Sopenharmony_ci	might_sleep();
154462306a36Sopenharmony_ci
154562306a36Sopenharmony_ci	if (!intel_gt_is_wedged(gt))
154662306a36Sopenharmony_ci		return 0;
154762306a36Sopenharmony_ci
154862306a36Sopenharmony_ci	if (intel_gt_has_unrecoverable_error(gt))
154962306a36Sopenharmony_ci		return -EIO;
155062306a36Sopenharmony_ci
155162306a36Sopenharmony_ci	/* Reset still in progress? Maybe we will recover? */
155262306a36Sopenharmony_ci	if (wait_event_interruptible(gt->reset.queue,
155362306a36Sopenharmony_ci				     !test_bit(I915_RESET_BACKOFF,
155462306a36Sopenharmony_ci					       &gt->reset.flags)))
155562306a36Sopenharmony_ci		return -EINTR;
155662306a36Sopenharmony_ci
155762306a36Sopenharmony_ci	return intel_gt_is_wedged(gt) ? -EIO : 0;
155862306a36Sopenharmony_ci}
155962306a36Sopenharmony_ci
156062306a36Sopenharmony_civoid intel_gt_set_wedged_on_init(struct intel_gt *gt)
156162306a36Sopenharmony_ci{
156262306a36Sopenharmony_ci	BUILD_BUG_ON(I915_RESET_ENGINE + I915_NUM_ENGINES >
156362306a36Sopenharmony_ci		     I915_WEDGED_ON_INIT);
156462306a36Sopenharmony_ci	intel_gt_set_wedged(gt);
156562306a36Sopenharmony_ci	i915_disable_error_state(gt->i915, -ENODEV);
156662306a36Sopenharmony_ci	set_bit(I915_WEDGED_ON_INIT, &gt->reset.flags);
156762306a36Sopenharmony_ci
156862306a36Sopenharmony_ci	/* Wedged on init is non-recoverable */
156962306a36Sopenharmony_ci	add_taint_for_CI(gt->i915, TAINT_WARN);
157062306a36Sopenharmony_ci}
157162306a36Sopenharmony_ci
157262306a36Sopenharmony_civoid intel_gt_set_wedged_on_fini(struct intel_gt *gt)
157362306a36Sopenharmony_ci{
157462306a36Sopenharmony_ci	intel_gt_set_wedged(gt);
157562306a36Sopenharmony_ci	i915_disable_error_state(gt->i915, -ENODEV);
157662306a36Sopenharmony_ci	set_bit(I915_WEDGED_ON_FINI, &gt->reset.flags);
157762306a36Sopenharmony_ci	intel_gt_retire_requests(gt); /* cleanup any wedged requests */
157862306a36Sopenharmony_ci}
157962306a36Sopenharmony_ci
158062306a36Sopenharmony_civoid intel_gt_init_reset(struct intel_gt *gt)
158162306a36Sopenharmony_ci{
158262306a36Sopenharmony_ci	init_waitqueue_head(&gt->reset.queue);
158362306a36Sopenharmony_ci	mutex_init(&gt->reset.mutex);
158462306a36Sopenharmony_ci	init_srcu_struct(&gt->reset.backoff_srcu);
158562306a36Sopenharmony_ci
158662306a36Sopenharmony_ci	/*
158762306a36Sopenharmony_ci	 * While undesirable to wait inside the shrinker, complain anyway.
158862306a36Sopenharmony_ci	 *
158962306a36Sopenharmony_ci	 * If we have to wait during shrinking, we guarantee forward progress
159062306a36Sopenharmony_ci	 * by forcing the reset. Therefore during the reset we must not
159162306a36Sopenharmony_ci	 * re-enter the shrinker. By declaring that we take the reset mutex
159262306a36Sopenharmony_ci	 * within the shrinker, we forbid ourselves from performing any
159362306a36Sopenharmony_ci	 * fs-reclaim or taking related locks during reset.
159462306a36Sopenharmony_ci	 */
159562306a36Sopenharmony_ci	i915_gem_shrinker_taints_mutex(gt->i915, &gt->reset.mutex);
159662306a36Sopenharmony_ci
159762306a36Sopenharmony_ci	/* no GPU until we are ready! */
159862306a36Sopenharmony_ci	__set_bit(I915_WEDGED, &gt->reset.flags);
159962306a36Sopenharmony_ci}
160062306a36Sopenharmony_ci
160162306a36Sopenharmony_civoid intel_gt_fini_reset(struct intel_gt *gt)
160262306a36Sopenharmony_ci{
160362306a36Sopenharmony_ci	cleanup_srcu_struct(&gt->reset.backoff_srcu);
160462306a36Sopenharmony_ci}
160562306a36Sopenharmony_ci
160662306a36Sopenharmony_cistatic void intel_wedge_me(struct work_struct *work)
160762306a36Sopenharmony_ci{
160862306a36Sopenharmony_ci	struct intel_wedge_me *w = container_of(work, typeof(*w), work.work);
160962306a36Sopenharmony_ci
161062306a36Sopenharmony_ci	drm_err(&w->gt->i915->drm,
161162306a36Sopenharmony_ci		"%s timed out, cancelling all in-flight rendering.\n",
161262306a36Sopenharmony_ci		w->name);
161362306a36Sopenharmony_ci	intel_gt_set_wedged(w->gt);
161462306a36Sopenharmony_ci}
161562306a36Sopenharmony_ci
161662306a36Sopenharmony_civoid __intel_init_wedge(struct intel_wedge_me *w,
161762306a36Sopenharmony_ci			struct intel_gt *gt,
161862306a36Sopenharmony_ci			long timeout,
161962306a36Sopenharmony_ci			const char *name)
162062306a36Sopenharmony_ci{
162162306a36Sopenharmony_ci	w->gt = gt;
162262306a36Sopenharmony_ci	w->name = name;
162362306a36Sopenharmony_ci
162462306a36Sopenharmony_ci	INIT_DELAYED_WORK_ONSTACK(&w->work, intel_wedge_me);
162562306a36Sopenharmony_ci	queue_delayed_work(gt->i915->unordered_wq, &w->work, timeout);
162662306a36Sopenharmony_ci}
162762306a36Sopenharmony_ci
162862306a36Sopenharmony_civoid __intel_fini_wedge(struct intel_wedge_me *w)
162962306a36Sopenharmony_ci{
163062306a36Sopenharmony_ci	cancel_delayed_work_sync(&w->work);
163162306a36Sopenharmony_ci	destroy_delayed_work_on_stack(&w->work);
163262306a36Sopenharmony_ci	w->gt = NULL;
163362306a36Sopenharmony_ci}
163462306a36Sopenharmony_ci
163562306a36Sopenharmony_ci#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
163662306a36Sopenharmony_ci#include "selftest_reset.c"
163762306a36Sopenharmony_ci#include "selftest_hangcheck.c"
163862306a36Sopenharmony_ci#endif
1639