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

7 8
#include <linux/suspend.h>

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

23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
static void user_forcewake(struct intel_gt *gt, bool suspend)
{
	int count = atomic_read(&gt->user_wakeref);

	/* Inside suspend/resume so single threaded, no races to worry about. */
	if (likely(!count))
		return;

	intel_gt_pm_get(gt);
	if (suspend) {
		GEM_BUG_ON(count > atomic_read(&gt->wakeref.count));
		atomic_sub(count, &gt->wakeref.count);
	} else {
		atomic_add(count, &gt->wakeref.count);
	}
	intel_gt_pm_put(gt);
}

41
static int __gt_unpark(struct intel_wakeref *wf)
42
{
43 44
	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
	struct drm_i915_private *i915 = gt->i915;
45 46 47

	GEM_TRACE("\n");

48 49
	i915_globals_unpark();

50 51 52 53 54 55 56 57 58 59 60
	/*
	 * 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.
	 */
61 62
	gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
	GEM_BUG_ON(!gt->awake);
63

64
	if (NEEDS_RC6_CTX_CORRUPTION_WA(i915))
65
		intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
66

67
	intel_rps_unpark(&gt->rps);
68 69
	i915_pmu_gt_unparked(i915);

70
	intel_gt_unpark_requests(gt);
71 72 73 74

	return 0;
}

75
static int __gt_park(struct intel_wakeref *wf)
76
{
77 78 79
	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;
80 81 82

	GEM_TRACE("\n");

83
	intel_gt_park_requests(gt);
84

85
	i915_vma_parked(gt);
86
	i915_pmu_gt_parked(i915);
87
	intel_rps_park(&gt->rps);
88

89 90 91
	/* Everything switched off, flush any residual interrupt just in case */
	intel_synchronize_irq(i915);

92
	if (NEEDS_RC6_CTX_CORRUPTION_WA(i915)) {
93 94
		intel_rc6_ctx_wa_check(&gt->rc6);
		intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
95 96
	}

97 98 99
	GEM_BUG_ON(!wakeref);
	intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);

100 101
	i915_globals_park();

102 103 104
	return 0;
}

105 106 107 108
static const struct intel_wakeref_ops wf_ops = {
	.get = __gt_unpark,
	.put = __gt_park,
};
109

110
void intel_gt_pm_init_early(struct intel_gt *gt)
111
{
112
	intel_wakeref_init(&gt->wakeref, gt->uncore->rpm, &wf_ops);
113 114
}

115 116 117 118 119 120 121 122
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);
123
	intel_rps_init(&gt->rps);
124 125
}

126
static bool reset_engines(struct intel_gt *gt)
127
{
128
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
129 130
		return false;

131
	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
132 133 134 135
}

/**
 * intel_gt_sanitize: called after the GPU has lost power
136
 * @gt: the i915 GT container
137 138 139 140 141 142 143
 * @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!
 */
144
void intel_gt_sanitize(struct intel_gt *gt, bool force)
145 146 147
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
148
	intel_wakeref_t wakeref;
149

150 151 152 153 154 155 156 157 158 159 160 161 162 163
	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);
164

165 166
	intel_uc_sanitize(&gt->uc);

167
	for_each_engine(engine, gt, id)
168 169 170
		if (engine->reset.prepare)
			engine->reset.prepare(engine);

171 172
	intel_uc_reset_prepare(&gt->uc);

173
	if (reset_engines(gt) || force) {
174
		for_each_engine(engine, gt, id)
175 176
			__intel_engine_reset(engine, false);
	}
177

178
	for_each_engine(engine, gt, id)
179 180
		if (engine->reset.finish)
			engine->reset.finish(engine);
181 182 183

	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
184 185
}

186
void intel_gt_pm_fini(struct intel_gt *gt)
187
{
188
	intel_rc6_fini(&gt->rc6);
189 190
}

191
int intel_gt_resume(struct intel_gt *gt)
192 193 194
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
195
	int err = 0;
196

197 198
	GEM_TRACE("\n");

199 200 201 202 203 204
	/*
	 * 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.
	 */
205
	intel_gt_pm_get(gt);
206

207 208 209
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
	intel_rc6_sanitize(&gt->rc6);

210 211 212
	intel_rps_enable(&gt->rps);
	intel_llc_enable(&gt->llc);

213
	for_each_engine(engine, gt, id) {
214 215
		struct intel_context *ce;

216 217
		intel_engine_pm_get(engine);

218
		ce = engine->kernel_context;
219 220
		if (ce) {
			GEM_BUG_ON(!intel_context_is_pinned(ce));
221
			ce->ops->reset(ce);
222
		}
223

224 225 226 227 228 229 230 231 232 233
		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;
		}
234
	}
235 236

	intel_rc6_enable(&gt->rc6);
237 238 239

	intel_uc_resume(&gt->uc);

240 241
	user_forcewake(gt, false);

242
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
243 244 245
	intel_gt_pm_put(gt);

	return err;
246
}
247

248
static void wait_for_suspend(struct intel_gt *gt)
249
{
250 251 252
	if (!intel_gt_pm_is_awake(gt))
		return;

253
	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
254 255 256 257 258
		/*
		 * Forcibly cancel outstanding work and leave
		 * the gpu quiet.
		 */
		intel_gt_set_wedged(gt);
259
		intel_gt_retire_requests(gt);
260
	}
261 262 263 264

	intel_gt_pm_wait_for_idle(gt);
}

265
void intel_gt_suspend_prepare(struct intel_gt *gt)
266
{
267
	user_forcewake(gt, true);
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	wait_for_suspend(gt);

	intel_uc_suspend(&gt->uc);
}

static suspend_state_t pm_suspend_target(void)
{
#if IS_ENABLED(CONFIG_PM_SLEEP)
	return pm_suspend_target_state;
#else
	return PM_SUSPEND_TO_IDLE;
#endif
}

void intel_gt_suspend_late(struct intel_gt *gt)
{
	intel_wakeref_t wakeref;
285

286
	/* We expect to be idle already; but also want to be independent */
287
	wait_for_suspend(gt);
288

289 290 291 292 293 294 295 296 297 298 299 300
	/*
	 * On disabling the device, we want to turn off HW access to memory
	 * that we no longer own.
	 *
	 * However, not all suspend-states disable the device. S0 (s2idle)
	 * is effectively runtime-suspend, the device is left powered on
	 * but needs to be put into a low power state. We need to keep
	 * powermanagement enabled, but we also retain system state and so
	 * it remains safe to keep on using our allocated memory.
	 */
	if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
		return;
301

302 303
	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
		intel_rps_disable(&gt->rps);
304
		intel_rc6_disable(&gt->rc6);
305 306
		intel_llc_disable(&gt->llc);
	}
307 308 309 310

	intel_gt_sanitize(gt, false);

	GEM_TRACE("\n");
311 312
}

313 314 315
void intel_gt_runtime_suspend(struct intel_gt *gt)
{
	intel_uc_runtime_suspend(&gt->uc);
316 317

	GEM_TRACE("\n");
318 319 320 321
}

int intel_gt_runtime_resume(struct intel_gt *gt)
{
322 323
	GEM_TRACE("\n");

324 325 326 327
	intel_gt_init_swizzling(gt);

	return intel_uc_runtime_resume(&gt->uc);
}
328 329 330 331

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