intel_gt_pm.c 7.6 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
	GT_TRACE(gt, "\n");
47

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
	intel_rc6_unpark(&gt->rc6);
65
	intel_rps_unpark(&gt->rps);
66 67
	i915_pmu_gt_unparked(i915);

68
	intel_gt_unpark_requests(gt);
69 70 71 72

	return 0;
}

73
static int __gt_park(struct intel_wakeref *wf)
74
{
75 76 77
	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;
78

79
	GT_TRACE(gt, "\n");
80

81
	intel_gt_park_requests(gt);
82

83
	i915_vma_parked(gt);
84
	i915_pmu_gt_parked(i915);
85
	intel_rps_park(&gt->rps);
86
	intel_rc6_park(&gt->rc6);
87

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

91
	/* Defer dropping the display power well for 100ms, it's slow! */
92
	GEM_BUG_ON(!wakeref);
93
	intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
94

95 96
	i915_globals_park();

97 98 99
	return 0;
}

100 101 102 103
static const struct intel_wakeref_ops wf_ops = {
	.get = __gt_unpark,
	.put = __gt_park,
};
104

105
void intel_gt_pm_init_early(struct intel_gt *gt)
106
{
107
	intel_wakeref_init(&gt->wakeref, gt->uncore->rpm, &wf_ops);
108 109
}

110 111 112 113 114 115 116 117
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);
118
	intel_rps_init(&gt->rps);
119 120
}

121
static bool reset_engines(struct intel_gt *gt)
122
{
123
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
124 125
		return false;

126
	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
127 128
}

129
static void gt_sanitize(struct intel_gt *gt, bool force)
130 131 132
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
133
	intel_wakeref_t wakeref;
134

135
	GT_TRACE(gt, "force:%s", yesno(force));
136 137 138 139 140 141 142 143 144 145 146 147 148

	/* 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);
149

150 151
	intel_uc_sanitize(&gt->uc);

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

156 157
	intel_uc_reset_prepare(&gt->uc);

158
	if (reset_engines(gt) || force) {
159
		for_each_engine(engine, gt, id)
160 161
			__intel_engine_reset(engine, false);
	}
162

163
	for_each_engine(engine, gt, id)
164 165
		if (engine->reset.finish)
			engine->reset.finish(engine);
166 167 168

	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
169 170
}

171
void intel_gt_pm_fini(struct intel_gt *gt)
172
{
173
	intel_rc6_fini(&gt->rc6);
174 175
}

176
int intel_gt_resume(struct intel_gt *gt)
177 178 179
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
180
	int err;
181

182
	err = intel_gt_has_init_error(gt);
183 184 185
	if (err)
		return err;

186
	GT_TRACE(gt, "\n");
187

188 189 190 191 192 193
	/*
	 * 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.
	 */
194
	intel_gt_pm_get(gt);
195

196 197
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
	intel_rc6_sanitize(&gt->rc6);
198
	gt_sanitize(gt, true);
199 200 201 202
	if (intel_gt_is_wedged(gt)) {
		err = -EIO;
		goto out_fw;
	}
203

204 205 206 207 208
	/* Only when the HW is re-initialised, can we replay the requests */
	err = intel_gt_init_hw(gt);
	if (err) {
		dev_err(gt->i915->drm.dev,
			"Failed to initialize GPU, declaring it wedged!\n");
209
		goto err_wedged;
210 211
	}

212 213 214
	intel_rps_enable(&gt->rps);
	intel_llc_enable(&gt->llc);

215
	for_each_engine(engine, gt, id) {
216 217 218
		intel_engine_pm_get(engine);

		engine->serial++; /* kernel context lost */
219
		err = intel_engine_resume(engine);
220 221 222 223 224 225

		intel_engine_pm_put(engine);
		if (err) {
			dev_err(gt->i915->drm.dev,
				"Failed to restart %s (%d)\n",
				engine->name, err);
226
			goto err_wedged;
227
		}
228
	}
229 230

	intel_rc6_enable(&gt->rc6);
231 232 233

	intel_uc_resume(&gt->uc);

234 235
	user_forcewake(gt, false);

236
out_fw:
237
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
238 239
	intel_gt_pm_put(gt);
	return err;
240 241 242 243

err_wedged:
	intel_gt_set_wedged(gt);
	goto out_fw;
244
}
245

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

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

	intel_gt_pm_wait_for_idle(gt);
}

263
void intel_gt_suspend_prepare(struct intel_gt *gt)
264
{
265
	user_forcewake(gt, true);
266 267 268 269 270 271 272
	wait_for_suspend(gt);

	intel_uc_suspend(&gt->uc);
}

static suspend_state_t pm_suspend_target(void)
{
273
#if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
274 275 276 277 278 279 280 281 282
	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;
283

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

287 288 289 290 291
	if (is_mock_gt(gt))
		return;

	GEM_BUG_ON(gt->awake);

292 293 294 295 296 297 298 299 300 301 302 303
	/*
	 * 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;
304

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

311
	gt_sanitize(gt, false);
312

313
	GT_TRACE(gt, "\n");
314 315
}

316 317 318
void intel_gt_runtime_suspend(struct intel_gt *gt)
{
	intel_uc_runtime_suspend(&gt->uc);
319

320
	GT_TRACE(gt, "\n");
321 322 323 324
}

int intel_gt_runtime_resume(struct intel_gt *gt)
{
325
	GT_TRACE(gt, "\n");
326
	intel_gt_init_swizzling(gt);
327
	intel_ggtt_restore_fences(gt->ggtt);
328 329 330

	return intel_uc_runtime_resume(&gt->uc);
}
331 332 333 334

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