intel_gt_pm.c 6.2 KB
Newer Older
1 2 3 4 5 6 7
/*
 * SPDX-License-Identifier: MIT
 *
 * Copyright © 2019 Intel Corporation
 */

#include "i915_drv.h"
8
#include "i915_globals.h"
9
#include "i915_params.h"
10
#include "intel_context.h"
11
#include "intel_engine_pm.h"
12
#include "intel_gt.h"
13
#include "intel_gt_pm.h"
14
#include "intel_gt_requests.h"
15
#include "intel_llc.h"
16
#include "intel_pm.h"
17
#include "intel_rc6.h"
18
#include "intel_rps.h"
19 20
#include "intel_wakeref.h"

21
static int __gt_unpark(struct intel_wakeref *wf)
22
{
23 24
	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
	struct drm_i915_private *i915 = gt->i915;
25 26 27

	GEM_TRACE("\n");

28 29
	i915_globals_unpark();

30 31 32 33 34 35 36 37 38 39 40
	/*
	 * It seems that the DMC likes to transition between the DC states a lot
	 * when there are no connected displays (no active power domains) during
	 * command submission.
	 *
	 * This activity has negative impact on the performance of the chip with
	 * huge latencies observed in the interrupt handler and elsewhere.
	 *
	 * Work around it by grabbing a GT IRQ power domain whilst there is any
	 * GT activity, preventing any DC state transitions.
	 */
41 42
	gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
	GEM_BUG_ON(!gt->awake);
43

44
	intel_rps_unpark(&gt->rps);
45 46
	i915_pmu_gt_unparked(i915);

47
	intel_gt_unpark_requests(gt);
48 49 50 51

	return 0;
}

52
static int __gt_park(struct intel_wakeref *wf)
53
{
54 55 56
	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
	intel_wakeref_t wakeref = fetch_and_zero(&gt->awake);
	struct drm_i915_private *i915 = gt->i915;
57 58 59

	GEM_TRACE("\n");

60
	intel_gt_park_requests(gt);
61

62
	i915_vma_parked(gt);
63
	i915_pmu_gt_parked(i915);
64
	intel_rps_park(&gt->rps);
65

66 67 68
	/* Everything switched off, flush any residual interrupt just in case */
	intel_synchronize_irq(i915);

69 70 71
	GEM_BUG_ON(!wakeref);
	intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);

72 73
	i915_globals_park();

74 75 76
	return 0;
}

77 78 79 80 81
static const struct intel_wakeref_ops wf_ops = {
	.get = __gt_unpark,
	.put = __gt_park,
	.flags = INTEL_WAKEREF_PUT_ASYNC,
};
82

83
void intel_gt_pm_init_early(struct intel_gt *gt)
84
{
85
	intel_wakeref_init(&gt->wakeref, gt->uncore->rpm, &wf_ops);
86 87
}

88 89 90 91 92 93 94 95
void intel_gt_pm_init(struct intel_gt *gt)
{
	/*
	 * Enabling power-management should be "self-healing". If we cannot
	 * enable a feature, simply leave it disabled with a notice to the
	 * user.
	 */
	intel_rc6_init(&gt->rc6);
96
	intel_rps_init(&gt->rps);
97 98
}

99
static bool reset_engines(struct intel_gt *gt)
100
{
101
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
102 103
		return false;

104
	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
105 106 107 108
}

/**
 * intel_gt_sanitize: called after the GPU has lost power
109
 * @gt: the i915 GT container
110 111 112 113 114 115 116
 * @force: ignore a failed reset and sanitize engine state anyway
 *
 * Anytime we reset the GPU, either with an explicit GPU reset or through a
 * PCI power cycle, the GPU loses state and we must reset our state tracking
 * to match. Note that calling intel_gt_sanitize() if the GPU has not
 * been reset results in much confusion!
 */
117
void intel_gt_sanitize(struct intel_gt *gt, bool force)
118 119 120
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
121
	intel_wakeref_t wakeref;
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136
	GEM_TRACE("force:%s\n", yesno(force));

	/* Use a raw wakeref to avoid calling intel_display_power_get early */
	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);

	/*
	 * As we have just resumed the machine and woken the device up from
	 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
	 * back to defaults, recovering from whatever wedged state we left it
	 * in and so worth trying to use the device once more.
	 */
	if (intel_gt_is_wedged(gt))
		intel_gt_unset_wedged(gt);
137

138 139
	intel_uc_sanitize(&gt->uc);

140
	for_each_engine(engine, gt, id)
141 142 143
		if (engine->reset.prepare)
			engine->reset.prepare(engine);

144 145
	intel_uc_reset_prepare(&gt->uc);

146
	if (reset_engines(gt) || force) {
147
		for_each_engine(engine, gt, id)
148 149
			__intel_engine_reset(engine, false);
	}
150

151
	for_each_engine(engine, gt, id)
152 153
		if (engine->reset.finish)
			engine->reset.finish(engine);
154 155 156

	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
157 158
}

159
void intel_gt_pm_fini(struct intel_gt *gt)
160
{
161
	intel_rc6_fini(&gt->rc6);
162 163
}

164
int intel_gt_resume(struct intel_gt *gt)
165 166 167
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
168
	int err = 0;
169

170 171
	GEM_TRACE("\n");

172 173 174 175 176 177
	/*
	 * After resume, we may need to poke into the pinned kernel
	 * contexts to paper over any damage caused by the sudden suspend.
	 * Only the kernel contexts should remain pinned over suspend,
	 * allowing us to fixup the user contexts on their first pin.
	 */
178
	intel_gt_pm_get(gt);
179

180 181 182
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
	intel_rc6_sanitize(&gt->rc6);

183 184 185
	intel_rps_enable(&gt->rps);
	intel_llc_enable(&gt->llc);

186
	for_each_engine(engine, gt, id) {
187 188
		struct intel_context *ce;

189 190
		intel_engine_pm_get(engine);

191
		ce = engine->kernel_context;
192 193
		if (ce) {
			GEM_BUG_ON(!intel_context_is_pinned(ce));
194
			ce->ops->reset(ce);
195
		}
196

197 198 199 200 201 202 203 204 205 206
		engine->serial++; /* kernel context lost */
		err = engine->resume(engine);

		intel_engine_pm_put(engine);
		if (err) {
			dev_err(gt->i915->drm.dev,
				"Failed to restart %s (%d)\n",
				engine->name, err);
			break;
		}
207
	}
208 209

	intel_rc6_enable(&gt->rc6);
210 211 212

	intel_uc_resume(&gt->uc);

213
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
214 215 216
	intel_gt_pm_put(gt);

	return err;
217
}
218

219 220
static void wait_for_idle(struct intel_gt *gt)
{
221
	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
222 223 224 225 226 227
		/*
		 * Forcibly cancel outstanding work and leave
		 * the gpu quiet.
		 */
		intel_gt_set_wedged(gt);
	}
228 229 230 231 232 233 234 235 236 237 238

	intel_gt_pm_wait_for_idle(gt);
}

void intel_gt_suspend(struct intel_gt *gt)
{
	intel_wakeref_t wakeref;

	/* We expect to be idle already; but also want to be independent */
	wait_for_idle(gt);

239 240
	intel_uc_suspend(&gt->uc);

241 242
	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
		intel_rps_disable(&gt->rps);
243
		intel_rc6_disable(&gt->rc6);
244 245
		intel_llc_disable(&gt->llc);
	}
246 247 248 249

	intel_gt_sanitize(gt, false);

	GEM_TRACE("\n");
250 251
}

252 253 254
void intel_gt_runtime_suspend(struct intel_gt *gt)
{
	intel_uc_runtime_suspend(&gt->uc);
255 256

	GEM_TRACE("\n");
257 258 259 260
}

int intel_gt_runtime_resume(struct intel_gt *gt)
{
261 262
	GEM_TRACE("\n");

263 264 265 266
	intel_gt_init_swizzling(gt);

	return intel_uc_runtime_resume(&gt->uc);
}
267 268 269 270

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