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

7
#include "gem/i915_gem_pm.h"
8 9
#include "gt/intel_gt_pm.h"

10 11 12
#include "i915_drv.h"
#include "i915_globals.h"

13
static void i915_gem_park(struct drm_i915_private *i915)
14
{
15 16
	struct intel_engine_cs *engine;
	enum intel_engine_id id;
17 18 19

	lockdep_assert_held(&i915->drm.struct_mutex);

20
	for_each_engine(engine, i915, id)
21
		i915_gem_batch_pool_fini(&engine->batch_pool);
22

23 24 25 26
	i915_timelines_park(i915);
	i915_vma_parked(i915);

	i915_globals_park();
27 28 29 30 31
}

static void idle_work_handler(struct work_struct *work)
{
	struct drm_i915_private *i915 =
32
		container_of(work, typeof(*i915), gem.idle_work);
33
	bool restart = true;
34

35
	cancel_delayed_work(&i915->gem.retire_work);
36
	mutex_lock(&i915->drm.struct_mutex);
37

38
	intel_wakeref_lock(&i915->gt.wakeref);
39
	if (!intel_wakeref_active(&i915->gt.wakeref) && !work_pending(work)) {
40
		i915_gem_park(i915);
41 42
		restart = false;
	}
43
	intel_wakeref_unlock(&i915->gt.wakeref);
44 45

	mutex_unlock(&i915->drm.struct_mutex);
46 47 48 49
	if (restart)
		queue_delayed_work(i915->wq,
				   &i915->gem.retire_work,
				   round_jiffies_up_relative(HZ));
50 51 52 53 54 55 56 57 58 59 60 61 62
}

static void retire_work_handler(struct work_struct *work)
{
	struct drm_i915_private *i915 =
		container_of(work, typeof(*i915), gem.retire_work.work);

	/* Come back later if the device is busy... */
	if (mutex_trylock(&i915->drm.struct_mutex)) {
		i915_retire_requests(i915);
		mutex_unlock(&i915->drm.struct_mutex);
	}

63 64 65
	queue_delayed_work(i915->wq,
			   &i915->gem.retire_work,
			   round_jiffies_up_relative(HZ));
66 67
}

68 69 70
static int pm_notifier(struct notifier_block *nb,
		       unsigned long action,
		       void *data)
71
{
72 73
	struct drm_i915_private *i915 =
		container_of(nb, typeof(*i915), gem.pm_notifier);
74

75 76 77 78 79 80 81
	switch (action) {
	case INTEL_GT_UNPARK:
		i915_globals_unpark();
		queue_delayed_work(i915->wq,
				   &i915->gem.retire_work,
				   round_jiffies_up_relative(HZ));
		break;
82

83
	case INTEL_GT_PARK:
84
		queue_work(i915->wq, &i915->gem.idle_work);
85 86
		break;
	}
87

88
	return NOTIFY_OK;
89 90
}

91
static bool switch_to_kernel_context_sync(struct drm_i915_private *i915)
92
{
93
	bool result = true;
94

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
	do {
		if (i915_gem_wait_for_idle(i915,
					   I915_WAIT_LOCKED |
					   I915_WAIT_FOR_IDLE_BOOST,
					   I915_GEM_IDLE_TIMEOUT) == -ETIME) {
			/* XXX hide warning from gem_eio */
			if (i915_modparams.reset) {
				dev_err(i915->drm.dev,
					"Failed to idle engines, declaring wedged!\n");
				GEM_TRACE_DUMP();
			}

			/*
			 * Forcibly cancel outstanding work and leave
			 * the gpu quiet.
			 */
			i915_gem_set_wedged(i915);
			result = false;
		}
	} while (i915_retire_requests(i915) && result);
115

116 117
	GEM_BUG_ON(i915->gt.awake);
	return result;
118 119 120 121
}

bool i915_gem_load_power_context(struct drm_i915_private *i915)
{
122
	return switch_to_kernel_context_sync(i915);
123 124 125 126 127 128
}

void i915_gem_suspend(struct drm_i915_private *i915)
{
	GEM_TRACE("\n");

129
	intel_wakeref_auto(&i915->mm.userfault_wakeref, 0);
130
	flush_workqueue(i915->wq);
131 132 133 134 135 136 137 138 139 140 141 142

	mutex_lock(&i915->drm.struct_mutex);

	/*
	 * We have to flush all the executing contexts to main memory so
	 * that they can saved in the hibernation image. To ensure the last
	 * context image is coherent, we have to switch away from it. That
	 * leaves the i915->kernel_context still active when
	 * we actually suspend, and its image in memory may not match the GPU
	 * state. Fortunately, the kernel_context is disposable and we do
	 * not rely on its state.
	 */
143
	switch_to_kernel_context_sync(i915);
144 145

	mutex_unlock(&i915->drm.struct_mutex);
146 147 148 149 150

	/*
	 * Assert that we successfully flushed all the work and
	 * reset the GPU back to its idle, low power state.
	 */
151 152
	GEM_BUG_ON(i915->gt.awake);
	flush_work(&i915->gem.idle_work);
153

154
	cancel_delayed_work_sync(&i915->gpu_error.hangcheck_work);
155

156
	i915_gem_drain_freed_objects(i915);
157 158 159 160 161 162 163 164 165 166

	intel_uc_suspend(i915);
}

void i915_gem_suspend_late(struct drm_i915_private *i915)
{
	struct drm_i915_gem_object *obj;
	struct list_head *phases[] = {
		&i915->mm.unbound_list,
		&i915->mm.bound_list,
167
		&i915->mm.purge_list,
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
		NULL
	}, **phase;

	/*
	 * Neither the BIOS, ourselves or any other kernel
	 * expects the system to be in execlists mode on startup,
	 * so we need to reset the GPU back to legacy mode. And the only
	 * known way to disable logical contexts is through a GPU reset.
	 *
	 * So in order to leave the system in a known default configuration,
	 * always reset the GPU upon unload and suspend. Afterwards we then
	 * clean up the GEM state tracking, flushing off the requests and
	 * leaving the system in a known idle state.
	 *
	 * Note that is of the upmost importance that the GPU is idle and
	 * all stray writes are flushed *before* we dismantle the backing
	 * storage for the pinned objects.
	 *
	 * However, since we are uncertain that resetting the GPU on older
	 * machines is a good idea, we don't - just in case it leaves the
	 * machine in an unusable condition.
	 */

	for (phase = phases; *phase; phase++) {
192 193
		list_for_each_entry(obj, *phase, mm.link) {
			i915_gem_object_lock(obj);
194
			WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
195 196
			i915_gem_object_unlock(obj);
		}
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
	}

	intel_uc_sanitize(i915);
	i915_gem_sanitize(i915);
}

void i915_gem_resume(struct drm_i915_private *i915)
{
	GEM_TRACE("\n");

	WARN_ON(i915->gt.awake);

	mutex_lock(&i915->drm.struct_mutex);
	intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);

	i915_gem_restore_gtt_mappings(i915);
	i915_gem_restore_fences(i915);

	/*
	 * As we didn't flush the kernel context before suspend, we cannot
	 * guarantee that the context image is complete. So let's just reset
	 * it and start again.
	 */
	intel_gt_resume(i915);

	if (i915_gem_init_hw(i915))
		goto err_wedged;

	intel_uc_resume(i915);

	/* Always reload a context for powersaving. */
	if (!i915_gem_load_power_context(i915))
		goto err_wedged;

out_unlock:
	intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
	mutex_unlock(&i915->drm.struct_mutex);
	return;

err_wedged:
	if (!i915_reset_failed(i915)) {
		dev_err(i915->drm.dev,
			"Failed to re-initialize GPU, declaring it wedged!\n");
		i915_gem_set_wedged(i915);
	}
	goto out_unlock;
}

void i915_gem_init__pm(struct drm_i915_private *i915)
{
247
	INIT_WORK(&i915->gem.idle_work, idle_work_handler);
248
	INIT_DELAYED_WORK(&i915->gem.retire_work, retire_work_handler);
249 250 251 252

	i915->gem.pm_notifier.notifier_call = pm_notifier;
	blocking_notifier_chain_register(&i915->gt.pm_notifications,
					 &i915->gem.pm_notifier);
253
}