intel_gt_pm.c 9.1 KB
Newer Older
C
Chris Wilson 已提交
1
// SPDX-License-Identifier: MIT
2 3 4 5
/*
 * Copyright © 2019 Intel Corporation
 */

6 7
#include <linux/suspend.h>

8
#include "i915_drv.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_clock_utils.h"
14
#include "intel_gt_pm.h"
15
#include "intel_gt_requests.h"
16
#include "intel_llc.h"
17
#include "intel_pm.h"
18
#include "intel_rc6.h"
19
#include "intel_rps.h"
20
#include "intel_wakeref.h"
21
#include "pxp/intel_pxp_pm.h"
22

23 24
#define I915_GT_SUSPEND_IDLE_TIMEOUT (HZ / 2)

25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
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);
}

43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
static void runtime_begin(struct intel_gt *gt)
{
	local_irq_disable();
	write_seqcount_begin(&gt->stats.lock);
	gt->stats.start = ktime_get();
	gt->stats.active = true;
	write_seqcount_end(&gt->stats.lock);
	local_irq_enable();
}

static void runtime_end(struct intel_gt *gt)
{
	local_irq_disable();
	write_seqcount_begin(&gt->stats.lock);
	gt->stats.active = false;
	gt->stats.total =
		ktime_add(gt->stats.total,
			  ktime_sub(ktime_get(), gt->stats.start));
	write_seqcount_end(&gt->stats.lock);
	local_irq_enable();
}

65
static int __gt_unpark(struct intel_wakeref *wf)
66
{
67 68
	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
	struct drm_i915_private *i915 = gt->i915;
69

70
	GT_TRACE(gt, "\n");
71 72 73 74 75 76 77 78 79 80 81 82

	/*
	 * 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.
	 */
83 84
	gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
	GEM_BUG_ON(!gt->awake);
85

86
	intel_rc6_unpark(&gt->rc6);
87
	intel_rps_unpark(&gt->rps);
88
	i915_pmu_gt_unparked(i915);
89
	intel_guc_busyness_unpark(gt);
90

91
	intel_gt_unpark_requests(gt);
92
	runtime_begin(gt);
93 94 95 96

	return 0;
}

97
static int __gt_park(struct intel_wakeref *wf)
98
{
99 100 101
	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;
102

103
	GT_TRACE(gt, "\n");
104

105
	runtime_end(gt);
106
	intel_gt_park_requests(gt);
107

108
	intel_guc_busyness_park(gt);
109
	i915_vma_parked(gt);
110
	i915_pmu_gt_parked(i915);
111
	intel_rps_park(&gt->rps);
112
	intel_rc6_park(&gt->rc6);
113

114 115 116
	/* Everything switched off, flush any residual interrupt just in case */
	intel_synchronize_irq(i915);

117
	/* Defer dropping the display power well for 100ms, it's slow! */
118
	GEM_BUG_ON(!wakeref);
119
	intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
120 121 122 123

	return 0;
}

124 125 126 127
static const struct intel_wakeref_ops wf_ops = {
	.get = __gt_unpark,
	.put = __gt_park,
};
128

129
void intel_gt_pm_init_early(struct intel_gt *gt)
130
{
131
	intel_wakeref_init(&gt->wakeref, gt->uncore->rpm, &wf_ops);
132
	seqcount_mutex_init(&gt->stats.lock, &gt->wakeref.mutex);
133 134
}

135 136 137 138 139 140 141 142
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);
143
	intel_rps_init(&gt->rps);
144 145
}

146
static bool reset_engines(struct intel_gt *gt)
147
{
148
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
149 150
		return false;

151
	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
152 153
}

154
static void gt_sanitize(struct intel_gt *gt, bool force)
155 156 157
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
158
	intel_wakeref_t wakeref;
159

160
	GT_TRACE(gt, "force:%s", yesno(force));
161 162 163 164 165

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

166 167
	intel_gt_check_clock_frequency(gt);

168 169 170 171 172 173 174 175
	/*
	 * 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);
176

177
	for_each_engine(engine, gt, id)
178 179 180
		if (engine->reset.prepare)
			engine->reset.prepare(engine);

181 182
	intel_uc_reset_prepare(&gt->uc);

183 184 185 186
	for_each_engine(engine, gt, id)
		if (engine->sanitize)
			engine->sanitize(engine);

187
	if (reset_engines(gt) || force) {
188
		for_each_engine(engine, gt, id)
189 190
			__intel_engine_reset(engine, false);
	}
191

192 193
	intel_uc_reset(&gt->uc, false);

194
	for_each_engine(engine, gt, id)
195 196
		if (engine->reset.finish)
			engine->reset.finish(engine);
197

198 199
	intel_rps_sanitize(&gt->rps);

200 201
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
202 203
}

204
void intel_gt_pm_fini(struct intel_gt *gt)
205
{
206
	intel_rc6_fini(&gt->rc6);
207 208
}

209
int intel_gt_resume(struct intel_gt *gt)
210 211 212
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
213
	int err;
214

215
	err = intel_gt_has_unrecoverable_error(gt);
216 217 218
	if (err)
		return err;

219
	GT_TRACE(gt, "\n");
220

221 222 223 224 225 226
	/*
	 * 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.
	 */
C
Chris Wilson 已提交
227 228
	gt_sanitize(gt, true);

229
	intel_gt_pm_get(gt);
230

231 232
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
	intel_rc6_sanitize(&gt->rc6);
233 234 235 236
	if (intel_gt_is_wedged(gt)) {
		err = -EIO;
		goto out_fw;
	}
237

238 239 240
	/* Only when the HW is re-initialised, can we replay the requests */
	err = intel_gt_init_hw(gt);
	if (err) {
241 242
		i915_probe_error(gt->i915,
				 "Failed to initialize GPU, declaring it wedged!\n");
243
		goto err_wedged;
244 245
	}

246 247
	intel_uc_reset_finish(&gt->uc);

248 249 250
	intel_rps_enable(&gt->rps);
	intel_llc_enable(&gt->llc);

251
	for_each_engine(engine, gt, id) {
252 253 254
		intel_engine_pm_get(engine);

		engine->serial++; /* kernel context lost */
255
		err = intel_engine_resume(engine);
256 257 258

		intel_engine_pm_put(engine);
		if (err) {
259
			drm_err(&gt->i915->drm,
260 261
				"Failed to restart %s (%d)\n",
				engine->name, err);
262
			goto err_wedged;
263
		}
264
	}
265 266

	intel_rc6_enable(&gt->rc6);
267 268 269

	intel_uc_resume(&gt->uc);

270 271
	intel_pxp_resume(&gt->pxp);

272 273
	user_forcewake(gt, false);

274
out_fw:
275
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
276 277
	intel_gt_pm_put(gt);
	return err;
278 279 280 281

err_wedged:
	intel_gt_set_wedged(gt);
	goto out_fw;
282
}
283

284
static void wait_for_suspend(struct intel_gt *gt)
285
{
286 287 288
	if (!intel_gt_pm_is_awake(gt))
		return;

289
	if (intel_gt_wait_for_idle(gt, I915_GT_SUSPEND_IDLE_TIMEOUT) == -ETIME) {
290 291 292 293 294
		/*
		 * Forcibly cancel outstanding work and leave
		 * the gpu quiet.
		 */
		intel_gt_set_wedged(gt);
295
		intel_gt_retire_requests(gt);
296
	}
297 298 299 300

	intel_gt_pm_wait_for_idle(gt);
}

301
void intel_gt_suspend_prepare(struct intel_gt *gt)
302
{
303
	user_forcewake(gt, true);
304
	wait_for_suspend(gt);
305 306

	intel_pxp_suspend(&gt->pxp, false);
307 308 309 310
}

static suspend_state_t pm_suspend_target(void)
{
311
#if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
312 313 314 315 316 317 318 319 320
	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;
321

322
	/* We expect to be idle already; but also want to be independent */
323
	wait_for_suspend(gt);
324

325 326 327 328 329
	if (is_mock_gt(gt))
		return;

	GEM_BUG_ON(gt->awake);

330 331
	intel_uc_suspend(&gt->uc);

332 333 334 335 336 337 338 339 340 341 342 343
	/*
	 * 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;
344

345 346
	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
		intel_rps_disable(&gt->rps);
347
		intel_rc6_disable(&gt->rc6);
348 349
		intel_llc_disable(&gt->llc);
	}
350

351
	gt_sanitize(gt, false);
352

353
	GT_TRACE(gt, "\n");
354 355
}

356 357
void intel_gt_runtime_suspend(struct intel_gt *gt)
{
358
	intel_pxp_suspend(&gt->pxp, true);
359
	intel_uc_runtime_suspend(&gt->uc);
360

361
	GT_TRACE(gt, "\n");
362 363 364 365
}

int intel_gt_runtime_resume(struct intel_gt *gt)
{
366 367
	int ret;

368
	GT_TRACE(gt, "\n");
369
	intel_gt_init_swizzling(gt);
370
	intel_ggtt_restore_fences(gt->ggtt);
371

372 373 374 375 376 377 378
	ret = intel_uc_runtime_resume(&gt->uc);
	if (ret)
		return ret;

	intel_pxp_resume(&gt->pxp);

	return 0;
379
}
380

381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
static ktime_t __intel_gt_get_awake_time(const struct intel_gt *gt)
{
	ktime_t total = gt->stats.total;

	if (gt->stats.active)
		total = ktime_add(total,
				  ktime_sub(ktime_get(), gt->stats.start));

	return total;
}

ktime_t intel_gt_get_awake_time(const struct intel_gt *gt)
{
	unsigned int seq;
	ktime_t total;

	do {
		seq = read_seqcount_begin(&gt->stats.lock);
		total = __intel_gt_get_awake_time(gt);
	} while (read_seqcount_retry(&gt->stats.lock, seq));

	return total;
}

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