i915_gem_pm.c 4.5 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
#include "gt/intel_gt.h"
9
#include "gt/intel_gt_pm.h"
10
#include "gt/intel_gt_requests.h"
11

12 13
#include "i915_drv.h"

14
static bool switch_to_kernel_context_sync(struct intel_gt *gt)
15
{
16
	bool result = !intel_gt_is_wedged(gt);
17

18
	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
19 20 21 22 23
		/* XXX hide warning from gem_eio */
		if (i915_modparams.reset) {
			dev_err(gt->i915->drm.dev,
				"Failed to idle engines, declaring wedged!\n");
			GEM_TRACE_DUMP();
24
		}
25 26 27 28 29 30 31 32

		/*
		 * Forcibly cancel outstanding work and leave
		 * the gpu quiet.
		 */
		intel_gt_set_wedged(gt);
		result = false;
	}
33

34 35 36
	if (intel_gt_pm_wait_for_idle(gt))
		result = false;

37
	return result;
38 39
}

40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
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);
}

58 59 60 61
void i915_gem_suspend(struct drm_i915_private *i915)
{
	GEM_TRACE("\n");

62
	intel_wakeref_auto(&i915->ggtt.userfault_wakeref, 0);
63
	flush_workqueue(i915->wq);
64

65 66
	user_forcewake(&i915->gt, true);

67 68 69 70 71 72 73 74 75
	/*
	 * 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.
	 */
76
	intel_gt_suspend(&i915->gt);
77

78
	i915_gem_drain_freed_objects(i915);
79 80
}

81 82 83 84 85 86 87
static struct drm_i915_gem_object *first_mm_object(struct list_head *list)
{
	return list_first_entry_or_null(list,
					struct drm_i915_gem_object,
					mm.link);
}

88 89 90 91
void i915_gem_suspend_late(struct drm_i915_private *i915)
{
	struct drm_i915_gem_object *obj;
	struct list_head *phases[] = {
92
		&i915->mm.shrink_list,
93
		&i915->mm.purge_list,
94 95
		NULL
	}, **phase;
96
	unsigned long flags;
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

	/*
	 * 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.
	 */

118
	spin_lock_irqsave(&i915->mm.obj_lock, flags);
119
	for (phase = phases; *phase; phase++) {
120 121 122 123 124 125 126 127 128 129 130
		LIST_HEAD(keep);

		while ((obj = first_mm_object(*phase))) {
			list_move_tail(&obj->mm.link, &keep);

			/* Beware the background _i915_gem_free_objects */
			if (!kref_get_unless_zero(&obj->base.refcount))
				continue;

			spin_unlock_irqrestore(&i915->mm.obj_lock, flags);

131
			i915_gem_object_lock(obj);
132
			WARN_ON(i915_gem_object_set_to_gtt_domain(obj, false));
133
			i915_gem_object_unlock(obj);
134 135 136
			i915_gem_object_put(obj);

			spin_lock_irqsave(&i915->mm.obj_lock, flags);
137
		}
138 139

		list_splice_tail(&keep, *phase);
140
	}
141
	spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
142 143 144 145 146 147 148 149
}

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

	intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);

150
	if (intel_gt_init_hw(&i915->gt))
151 152
		goto err_wedged;

153 154 155 156 157
	/*
	 * 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.
	 */
158
	if (intel_gt_resume(&i915->gt))
159 160 161
		goto err_wedged;

	/* Always reload a context for powersaving. */
162
	if (!switch_to_kernel_context_sync(&i915->gt))
163 164
		goto err_wedged;

165 166
	user_forcewake(&i915->gt, false);

167 168 169 170 171
out_unlock:
	intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
	return;

err_wedged:
172
	if (!intel_gt_is_wedged(&i915->gt)) {
173 174
		dev_err(i915->drm.dev,
			"Failed to re-initialize GPU, declaring it wedged!\n");
175
		intel_gt_set_wedged(&i915->gt);
176 177 178
	}
	goto out_unlock;
}