2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #include "gem/i915_gem_pm.h"
8 #include "gem/i915_gem_ttm_pm.h"
9 #include "gt/intel_gt.h"
10 #include "gt/intel_gt_pm.h"
11 #include "gt/intel_gt_requests.h"
13 #include "i915_driver.h"
16 #if defined(CONFIG_X86)
19 #define wbinvd_on_all_cpus() \
20 pr_warn(DRIVER_NAME ": Missing cache flush in %s\n", __func__)
23 void i915_gem_suspend(struct drm_i915_private *i915)
28 GEM_TRACE("%s\n", dev_name(i915->drm.dev));
30 intel_wakeref_auto(&i915->runtime_pm.userfault_wakeref, 0);
32 * On rare occasions, we've observed the fence completion triggers
33 * free_engines asynchronously via rcu_call. Ensure those are done.
34 * This path is only called on suspend, so it's an acceptable cost.
38 flush_workqueue(i915->wq);
41 * We have to flush all the executing contexts to main memory so
42 * that they can saved in the hibernation image. To ensure the last
43 * context image is coherent, we have to switch away from it. That
44 * leaves the i915->kernel_context still active when
45 * we actually suspend, and its image in memory may not match the GPU
46 * state. Fortunately, the kernel_context is disposable and we do
47 * not rely on its state.
49 for_each_gt(gt, i915, i)
50 intel_gt_suspend_prepare(gt);
52 i915_gem_drain_freed_objects(i915);
55 static int lmem_restore(struct drm_i915_private *i915, u32 flags)
57 struct intel_memory_region *mr;
60 for_each_memory_region(mr, i915, id) {
61 if (mr->type == INTEL_MEMORY_LOCAL) {
62 ret = i915_ttm_restore_region(mr, flags);
71 static int lmem_suspend(struct drm_i915_private *i915, u32 flags)
73 struct intel_memory_region *mr;
76 for_each_memory_region(mr, i915, id) {
77 if (mr->type == INTEL_MEMORY_LOCAL) {
78 ret = i915_ttm_backup_region(mr, flags);
87 static void lmem_recover(struct drm_i915_private *i915)
89 struct intel_memory_region *mr;
92 for_each_memory_region(mr, i915, id)
93 if (mr->type == INTEL_MEMORY_LOCAL)
94 i915_ttm_recover_region(mr);
97 int i915_gem_backup_suspend(struct drm_i915_private *i915)
101 /* Opportunistically try to evict unpinned objects */
102 ret = lmem_suspend(i915, I915_TTM_BACKUP_ALLOW_GPU);
106 i915_gem_suspend(i915);
109 * More objects may have become unpinned as requests were
110 * retired. Now try to evict again. The gt may be wedged here
111 * in which case we automatically fall back to memcpy.
112 * We allow also backing up pinned objects that have not been
113 * marked for early recover, and that may contain, for example,
114 * page-tables for the migrate context.
116 ret = lmem_suspend(i915, I915_TTM_BACKUP_ALLOW_GPU |
117 I915_TTM_BACKUP_PINNED);
122 * Remaining objects are backed up using memcpy once we've stopped
123 * using the migrate context.
125 ret = lmem_suspend(i915, I915_TTM_BACKUP_PINNED);
137 void i915_gem_suspend_late(struct drm_i915_private *i915)
139 struct drm_i915_gem_object *obj;
140 struct list_head *phases[] = {
141 &i915->mm.shrink_list,
142 &i915->mm.purge_list,
151 * Neither the BIOS, ourselves or any other kernel
152 * expects the system to be in execlists mode on startup,
153 * so we need to reset the GPU back to legacy mode. And the only
154 * known way to disable logical contexts is through a GPU reset.
156 * So in order to leave the system in a known default configuration,
157 * always reset the GPU upon unload and suspend. Afterwards we then
158 * clean up the GEM state tracking, flushing off the requests and
159 * leaving the system in a known idle state.
161 * Note that is of the upmost importance that the GPU is idle and
162 * all stray writes are flushed *before* we dismantle the backing
163 * storage for the pinned objects.
165 * However, since we are uncertain that resetting the GPU on older
166 * machines is a good idea, we don't - just in case it leaves the
167 * machine in an unusable condition.
170 /* Like i915_gem_suspend, flush tasks staged from fence triggers */
173 for_each_gt(gt, i915, i)
174 intel_gt_suspend_late(gt);
176 spin_lock_irqsave(&i915->mm.obj_lock, flags);
177 for (phase = phases; *phase; phase++) {
178 list_for_each_entry(obj, *phase, mm.link) {
179 if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
180 flush |= (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0;
181 __start_cpu_write(obj); /* presume auto-hibernate */
184 spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
186 wbinvd_on_all_cpus();
189 int i915_gem_freeze(struct drm_i915_private *i915)
191 /* Discard all purgeable objects, let userspace recover those as
192 * required after resuming.
194 i915_gem_shrink_all(i915);
199 int i915_gem_freeze_late(struct drm_i915_private *i915)
201 struct drm_i915_gem_object *obj;
202 intel_wakeref_t wakeref;
205 * Called just before we write the hibernation image.
207 * We need to update the domain tracking to reflect that the CPU
208 * will be accessing all the pages to create and restore from the
209 * hibernation, and so upon restoration those pages will be in the
212 * To make sure the hibernation image contains the latest state,
213 * we update that state just before writing out the image.
215 * To try and reduce the hibernation image, we manually shrink
216 * the objects as well, see i915_gem_freeze()
219 with_intel_runtime_pm(&i915->runtime_pm, wakeref)
220 i915_gem_shrink(NULL, i915, -1UL, NULL, ~0);
221 i915_gem_drain_freed_objects(i915);
223 wbinvd_on_all_cpus();
224 list_for_each_entry(obj, &i915->mm.shrink_list, mm.link)
225 __start_cpu_write(obj);
230 void i915_gem_resume(struct drm_i915_private *i915)
235 GEM_TRACE("%s\n", dev_name(i915->drm.dev));
237 ret = lmem_restore(i915, 0);
241 * As we didn't flush the kernel context before suspend, we cannot
242 * guarantee that the context image is complete. So let's just reset
243 * it and start again.
245 for_each_gt(gt, i915, i)
246 if (intel_gt_resume(gt))
249 ret = lmem_restore(i915, I915_TTM_BACKUP_ALLOW_GPU);
255 for_each_gt(gt, i915, j) {
256 if (!intel_gt_is_wedged(gt)) {
257 dev_err(i915->drm.dev,
258 "Failed to re-initialize GPU[%u], declaring it wedged!\n",
260 intel_gt_set_wedged(gt);