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

L
Lucas De Marchi 已提交
6
#include <linux/string_helpers.h>
7 8
#include <linux/suspend.h>

9
#include "i915_drv.h"
10
#include "i915_params.h"
11
#include "intel_context.h"
12
#include "intel_engine_pm.h"
13
#include "intel_gt.h"
14
#include "intel_gt_clock_utils.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
#include "intel_wakeref.h"
22
#include "intel_pcode.h"
23
#include "pxp/intel_pxp_pm.h"
24

25 26
#define I915_GT_SUSPEND_IDLE_TIMEOUT (HZ / 2)

27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
static void mtl_media_busy(struct intel_gt *gt)
{
	/* Wa_14017073508: mtl */
	if (IS_MTL_GRAPHICS_STEP(gt->i915, P, STEP_A0, STEP_B0) &&
	    gt->type == GT_MEDIA)
		snb_pcode_write_p(gt->uncore, PCODE_MBOX_GT_STATE,
				  PCODE_MBOX_GT_STATE_MEDIA_BUSY,
				  PCODE_MBOX_GT_STATE_DOMAIN_MEDIA, 0);
}

static void mtl_media_idle(struct intel_gt *gt)
{
	/* Wa_14017073508: mtl */
	if (IS_MTL_GRAPHICS_STEP(gt->i915, P, STEP_A0, STEP_B0) &&
	    gt->type == GT_MEDIA)
		snb_pcode_write_p(gt->uncore, PCODE_MBOX_GT_STATE,
				  PCODE_MBOX_GT_STATE_MEDIA_NOT_BUSY,
				  PCODE_MBOX_GT_STATE_DOMAIN_MEDIA, 0);
}

47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64
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);
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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();
}

87
static int __gt_unpark(struct intel_wakeref *wf)
88
{
89 90
	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
	struct drm_i915_private *i915 = gt->i915;
91

92
	GT_TRACE(gt, "\n");
93

94 95 96
	/* Wa_14017073508: mtl */
	mtl_media_busy(gt);

97 98 99 100 101 102 103 104 105 106 107
	/*
	 * 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.
	 */
108 109
	gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
	GEM_BUG_ON(!gt->awake);
110

111
	intel_rc6_unpark(&gt->rc6);
112
	intel_rps_unpark(&gt->rps);
113
	i915_pmu_gt_unparked(i915);
114
	intel_guc_busyness_unpark(gt);
115

116
	intel_gt_unpark_requests(gt);
117
	runtime_begin(gt);
118 119 120 121

	return 0;
}

122
static int __gt_park(struct intel_wakeref *wf)
123
{
124 125 126
	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;
127

128
	GT_TRACE(gt, "\n");
129

130
	runtime_end(gt);
131
	intel_gt_park_requests(gt);
132

133
	intel_guc_busyness_park(gt);
134
	i915_vma_parked(gt);
135
	i915_pmu_gt_parked(i915);
136
	intel_rps_park(&gt->rps);
137
	intel_rc6_park(&gt->rc6);
138

139 140 141
	/* Everything switched off, flush any residual interrupt just in case */
	intel_synchronize_irq(i915);

142
	/* Defer dropping the display power well for 100ms, it's slow! */
143
	GEM_BUG_ON(!wakeref);
144
	intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
145

146 147 148
	/* Wa_14017073508: mtl */
	mtl_media_idle(gt);

149 150 151
	return 0;
}

152 153 154 155
static const struct intel_wakeref_ops wf_ops = {
	.get = __gt_unpark,
	.put = __gt_park,
};
156

157
void intel_gt_pm_init_early(struct intel_gt *gt)
158
{
159 160 161 162 163 164 165 166
	/*
	 * We access the runtime_pm structure via gt->i915 here rather than
	 * gt->uncore as we do elsewhere in the file because gt->uncore is not
	 * yet initialized for all tiles at this point in the driver startup.
	 * runtime_pm is per-device rather than per-tile, so this is still the
	 * correct structure.
	 */
	intel_wakeref_init(&gt->wakeref, &gt->i915->runtime_pm, &wf_ops);
167
	seqcount_mutex_init(&gt->stats.lock, &gt->wakeref.mutex);
168 169
}

170 171 172 173 174 175 176 177
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);
178
	intel_rps_init(&gt->rps);
179 180
}

181
static bool reset_engines(struct intel_gt *gt)
182
{
183
	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
184 185
		return false;

186
	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
187 188
}

189
static void gt_sanitize(struct intel_gt *gt, bool force)
190 191 192
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
193
	intel_wakeref_t wakeref;
194

L
Lucas De Marchi 已提交
195
	GT_TRACE(gt, "force:%s", str_yes_no(force));
196 197 198 199 200

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

201 202
	intel_gt_check_clock_frequency(gt);

203 204 205 206 207 208 209 210
	/*
	 * 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);
211

212 213 214 215
	/* For GuC mode, ensure submission is disabled before stopping ring */
	intel_uc_reset_prepare(&gt->uc);

	for_each_engine(engine, gt, id) {
216 217 218
		if (engine->reset.prepare)
			engine->reset.prepare(engine);

219 220
		if (engine->sanitize)
			engine->sanitize(engine);
221
	}
222

223
	if (reset_engines(gt) || force) {
224
		for_each_engine(engine, gt, id)
225 226
			__intel_engine_reset(engine, false);
	}
227

228 229
	intel_uc_reset(&gt->uc, false);

230
	for_each_engine(engine, gt, id)
231 232
		if (engine->reset.finish)
			engine->reset.finish(engine);
233

234 235
	intel_rps_sanitize(&gt->rps);

236 237
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
238 239
}

240
void intel_gt_pm_fini(struct intel_gt *gt)
241
{
242
	intel_rc6_fini(&gt->rc6);
243 244
}

245
int intel_gt_resume(struct intel_gt *gt)
246 247 248
{
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
249
	int err;
250

251
	err = intel_gt_has_unrecoverable_error(gt);
252 253 254
	if (err)
		return err;

255
	GT_TRACE(gt, "\n");
256

257 258 259 260 261 262
	/*
	 * 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 已提交
263 264
	gt_sanitize(gt, true);

265
	intel_gt_pm_get(gt);
266

267 268
	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
	intel_rc6_sanitize(&gt->rc6);
269 270 271 272
	if (intel_gt_is_wedged(gt)) {
		err = -EIO;
		goto out_fw;
	}
273

274 275 276
	/* Only when the HW is re-initialised, can we replay the requests */
	err = intel_gt_init_hw(gt);
	if (err) {
277 278
		i915_probe_error(gt->i915,
				 "Failed to initialize GPU, declaring it wedged!\n");
279
		goto err_wedged;
280 281
	}

282 283
	intel_uc_reset_finish(&gt->uc);

284 285 286
	intel_rps_enable(&gt->rps);
	intel_llc_enable(&gt->llc);

287
	for_each_engine(engine, gt, id) {
288 289 290
		intel_engine_pm_get(engine);

		engine->serial++; /* kernel context lost */
291
		err = intel_engine_resume(engine);
292 293 294

		intel_engine_pm_put(engine);
		if (err) {
295
			drm_err(&gt->i915->drm,
296 297
				"Failed to restart %s (%d)\n",
				engine->name, err);
298
			goto err_wedged;
299
		}
300
	}
301 302

	intel_rc6_enable(&gt->rc6);
303 304 305

	intel_uc_resume(&gt->uc);

306 307
	user_forcewake(gt, false);

308
out_fw:
309
	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
310 311
	intel_gt_pm_put(gt);
	return err;
312 313 314 315

err_wedged:
	intel_gt_set_wedged(gt);
	goto out_fw;
316
}
317

318
static void wait_for_suspend(struct intel_gt *gt)
319
{
320 321 322
	if (!intel_gt_pm_is_awake(gt))
		return;

323
	if (intel_gt_wait_for_idle(gt, I915_GT_SUSPEND_IDLE_TIMEOUT) == -ETIME) {
324 325 326 327 328
		/*
		 * Forcibly cancel outstanding work and leave
		 * the gpu quiet.
		 */
		intel_gt_set_wedged(gt);
329
		intel_gt_retire_requests(gt);
330
	}
331 332 333 334

	intel_gt_pm_wait_for_idle(gt);
}

335
void intel_gt_suspend_prepare(struct intel_gt *gt)
336
{
337
	user_forcewake(gt, true);
338 339 340 341 342
	wait_for_suspend(gt);
}

static suspend_state_t pm_suspend_target(void)
{
343
#if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
344 345 346 347 348 349 350 351 352
	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;
353

354
	/* We expect to be idle already; but also want to be independent */
355
	wait_for_suspend(gt);
356

357 358 359 360 361
	if (is_mock_gt(gt))
		return;

	GEM_BUG_ON(gt->awake);

362 363
	intel_uc_suspend(&gt->uc);

364 365 366 367 368 369 370 371 372 373 374 375
	/*
	 * 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;
376

377 378
	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
		intel_rps_disable(&gt->rps);
379
		intel_rc6_disable(&gt->rc6);
380 381
		intel_llc_disable(&gt->llc);
	}
382

383
	gt_sanitize(gt, false);
384

385
	GT_TRACE(gt, "\n");
386 387
}

388 389 390
void intel_gt_runtime_suspend(struct intel_gt *gt)
{
	intel_uc_runtime_suspend(&gt->uc);
391

392
	GT_TRACE(gt, "\n");
393 394 395 396
}

int intel_gt_runtime_resume(struct intel_gt *gt)
{
397 398
	int ret;

399
	GT_TRACE(gt, "\n");
400
	intel_gt_init_swizzling(gt);
401
	intel_ggtt_restore_fences(gt->ggtt);
402

403 404 405 406 407
	ret = intel_uc_runtime_resume(&gt->uc);
	if (ret)
		return ret;

	return 0;
408
}
409

410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433
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;
}

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