xref: /openbmc/linux/drivers/gpu/drm/i915/gem/i915_gem_pm.c (revision 7ae9fb1b7ecbb5d85d07857943f677fd1a559b18)
110be98a7SChris Wilson /*
210be98a7SChris Wilson  * SPDX-License-Identifier: MIT
310be98a7SChris Wilson  *
410be98a7SChris Wilson  * Copyright © 2019 Intel Corporation
510be98a7SChris Wilson  */
610be98a7SChris Wilson 
710be98a7SChris Wilson #include "gem/i915_gem_pm.h"
8c56ce956SThomas Hellström #include "gem/i915_gem_ttm_pm.h"
9cb823ed9SChris Wilson #include "gt/intel_gt.h"
1010be98a7SChris Wilson #include "gt/intel_gt_pm.h"
1166101975SChris Wilson #include "gt/intel_gt_requests.h"
1210be98a7SChris Wilson 
1324524e3fSJani Nikula #include "i915_driver.h"
1410be98a7SChris Wilson #include "i915_drv.h"
1510be98a7SChris Wilson 
16ac05a22cSChris Wilson #if defined(CONFIG_X86)
17ac05a22cSChris Wilson #include <asm/smp.h>
18ac05a22cSChris Wilson #else
19ac05a22cSChris Wilson #define wbinvd_on_all_cpus() \
20ac05a22cSChris Wilson 	pr_warn(DRIVER_NAME ": Missing cache flush in %s\n", __func__)
21ac05a22cSChris Wilson #endif
22ac05a22cSChris Wilson 
i915_gem_suspend(struct drm_i915_private * i915)2310be98a7SChris Wilson void i915_gem_suspend(struct drm_i915_private *i915)
2410be98a7SChris Wilson {
254b3823ffSTvrtko Ursulin 	struct intel_gt *gt;
264b3823ffSTvrtko Ursulin 	unsigned int i;
274b3823ffSTvrtko Ursulin 
28639f2f24SVenkata Sandeep Dhanalakota 	GEM_TRACE("%s\n", dev_name(i915->drm.dev));
2910be98a7SChris Wilson 
30*e66c8dcfSAnshuman Gupta 	intel_wakeref_auto(&i915->runtime_pm.userfault_wakeref, 0);
3110be98a7SChris Wilson 	flush_workqueue(i915->wq);
3210be98a7SChris Wilson 
3310be98a7SChris Wilson 	/*
3410be98a7SChris Wilson 	 * We have to flush all the executing contexts to main memory so
3510be98a7SChris Wilson 	 * that they can saved in the hibernation image. To ensure the last
3610be98a7SChris Wilson 	 * context image is coherent, we have to switch away from it. That
3710be98a7SChris Wilson 	 * leaves the i915->kernel_context still active when
3810be98a7SChris Wilson 	 * we actually suspend, and its image in memory may not match the GPU
3910be98a7SChris Wilson 	 * state. Fortunately, the kernel_context is disposable and we do
4010be98a7SChris Wilson 	 * not rely on its state.
4110be98a7SChris Wilson 	 */
424b3823ffSTvrtko Ursulin 	for_each_gt(gt, i915, i)
434b3823ffSTvrtko Ursulin 		intel_gt_suspend_prepare(gt);
4410be98a7SChris Wilson 
4510be98a7SChris Wilson 	i915_gem_drain_freed_objects(i915);
4610be98a7SChris Wilson }
4710be98a7SChris Wilson 
lmem_restore(struct drm_i915_private * i915,u32 flags)48c56ce956SThomas Hellström static int lmem_restore(struct drm_i915_private *i915, u32 flags)
49c56ce956SThomas Hellström {
50c56ce956SThomas Hellström 	struct intel_memory_region *mr;
51c56ce956SThomas Hellström 	int ret = 0, id;
52c56ce956SThomas Hellström 
53c56ce956SThomas Hellström 	for_each_memory_region(mr, i915, id) {
54c56ce956SThomas Hellström 		if (mr->type == INTEL_MEMORY_LOCAL) {
55c56ce956SThomas Hellström 			ret = i915_ttm_restore_region(mr, flags);
56c56ce956SThomas Hellström 			if (ret)
57c56ce956SThomas Hellström 				break;
58c56ce956SThomas Hellström 		}
59c56ce956SThomas Hellström 	}
60c56ce956SThomas Hellström 
61c56ce956SThomas Hellström 	return ret;
62c56ce956SThomas Hellström }
63c56ce956SThomas Hellström 
lmem_suspend(struct drm_i915_private * i915,u32 flags)64c56ce956SThomas Hellström static int lmem_suspend(struct drm_i915_private *i915, u32 flags)
65c56ce956SThomas Hellström {
66c56ce956SThomas Hellström 	struct intel_memory_region *mr;
67c56ce956SThomas Hellström 	int ret = 0, id;
68c56ce956SThomas Hellström 
69c56ce956SThomas Hellström 	for_each_memory_region(mr, i915, id) {
70c56ce956SThomas Hellström 		if (mr->type == INTEL_MEMORY_LOCAL) {
71c56ce956SThomas Hellström 			ret = i915_ttm_backup_region(mr, flags);
72c56ce956SThomas Hellström 			if (ret)
73c56ce956SThomas Hellström 				break;
74c56ce956SThomas Hellström 		}
75c56ce956SThomas Hellström 	}
76c56ce956SThomas Hellström 
77c56ce956SThomas Hellström 	return ret;
78c56ce956SThomas Hellström }
79c56ce956SThomas Hellström 
lmem_recover(struct drm_i915_private * i915)80c56ce956SThomas Hellström static void lmem_recover(struct drm_i915_private *i915)
81c56ce956SThomas Hellström {
82c56ce956SThomas Hellström 	struct intel_memory_region *mr;
83c56ce956SThomas Hellström 	int id;
84c56ce956SThomas Hellström 
85c56ce956SThomas Hellström 	for_each_memory_region(mr, i915, id)
86c56ce956SThomas Hellström 		if (mr->type == INTEL_MEMORY_LOCAL)
87c56ce956SThomas Hellström 			i915_ttm_recover_region(mr);
88c56ce956SThomas Hellström }
89c56ce956SThomas Hellström 
i915_gem_backup_suspend(struct drm_i915_private * i915)90c56ce956SThomas Hellström int i915_gem_backup_suspend(struct drm_i915_private *i915)
91c56ce956SThomas Hellström {
92c56ce956SThomas Hellström 	int ret;
93c56ce956SThomas Hellström 
94c56ce956SThomas Hellström 	/* Opportunistically try to evict unpinned objects */
95c56ce956SThomas Hellström 	ret = lmem_suspend(i915, I915_TTM_BACKUP_ALLOW_GPU);
96c56ce956SThomas Hellström 	if (ret)
97c56ce956SThomas Hellström 		goto out_recover;
98c56ce956SThomas Hellström 
99c56ce956SThomas Hellström 	i915_gem_suspend(i915);
100c56ce956SThomas Hellström 
101c56ce956SThomas Hellström 	/*
102c56ce956SThomas Hellström 	 * More objects may have become unpinned as requests were
103c56ce956SThomas Hellström 	 * retired. Now try to evict again. The gt may be wedged here
104c56ce956SThomas Hellström 	 * in which case we automatically fall back to memcpy.
105a259cc14SThomas Hellström 	 * We allow also backing up pinned objects that have not been
106a259cc14SThomas Hellström 	 * marked for early recover, and that may contain, for example,
107a259cc14SThomas Hellström 	 * page-tables for the migrate context.
108c56ce956SThomas Hellström 	 */
109a259cc14SThomas Hellström 	ret = lmem_suspend(i915, I915_TTM_BACKUP_ALLOW_GPU |
110a259cc14SThomas Hellström 			   I915_TTM_BACKUP_PINNED);
111c56ce956SThomas Hellström 	if (ret)
112c56ce956SThomas Hellström 		goto out_recover;
113c56ce956SThomas Hellström 
114c56ce956SThomas Hellström 	/*
115c56ce956SThomas Hellström 	 * Remaining objects are backed up using memcpy once we've stopped
116c56ce956SThomas Hellström 	 * using the migrate context.
117c56ce956SThomas Hellström 	 */
118c56ce956SThomas Hellström 	ret = lmem_suspend(i915, I915_TTM_BACKUP_PINNED);
119c56ce956SThomas Hellström 	if (ret)
120c56ce956SThomas Hellström 		goto out_recover;
121c56ce956SThomas Hellström 
122c56ce956SThomas Hellström 	return 0;
123c56ce956SThomas Hellström 
124c56ce956SThomas Hellström out_recover:
125c56ce956SThomas Hellström 	lmem_recover(i915);
126c56ce956SThomas Hellström 
127c56ce956SThomas Hellström 	return ret;
128c56ce956SThomas Hellström }
129c56ce956SThomas Hellström 
i915_gem_suspend_late(struct drm_i915_private * i915)13010be98a7SChris Wilson void i915_gem_suspend_late(struct drm_i915_private *i915)
13110be98a7SChris Wilson {
13210be98a7SChris Wilson 	struct drm_i915_gem_object *obj;
13310be98a7SChris Wilson 	struct list_head *phases[] = {
134ecab9be1SChris Wilson 		&i915->mm.shrink_list,
1353b4fa964SChris Wilson 		&i915->mm.purge_list,
13610be98a7SChris Wilson 		NULL
13710be98a7SChris Wilson 	}, **phase;
1384b3823ffSTvrtko Ursulin 	struct intel_gt *gt;
139ecab9be1SChris Wilson 	unsigned long flags;
1404b3823ffSTvrtko Ursulin 	unsigned int i;
141ac05a22cSChris Wilson 	bool flush = false;
14210be98a7SChris Wilson 
14310be98a7SChris Wilson 	/*
14410be98a7SChris Wilson 	 * Neither the BIOS, ourselves or any other kernel
14510be98a7SChris Wilson 	 * expects the system to be in execlists mode on startup,
14610be98a7SChris Wilson 	 * so we need to reset the GPU back to legacy mode. And the only
14710be98a7SChris Wilson 	 * known way to disable logical contexts is through a GPU reset.
14810be98a7SChris Wilson 	 *
14910be98a7SChris Wilson 	 * So in order to leave the system in a known default configuration,
15010be98a7SChris Wilson 	 * always reset the GPU upon unload and suspend. Afterwards we then
15110be98a7SChris Wilson 	 * clean up the GEM state tracking, flushing off the requests and
15210be98a7SChris Wilson 	 * leaving the system in a known idle state.
15310be98a7SChris Wilson 	 *
15410be98a7SChris Wilson 	 * Note that is of the upmost importance that the GPU is idle and
15510be98a7SChris Wilson 	 * all stray writes are flushed *before* we dismantle the backing
15610be98a7SChris Wilson 	 * storage for the pinned objects.
15710be98a7SChris Wilson 	 *
15810be98a7SChris Wilson 	 * However, since we are uncertain that resetting the GPU on older
15910be98a7SChris Wilson 	 * machines is a good idea, we don't - just in case it leaves the
16010be98a7SChris Wilson 	 * machine in an unusable condition.
16110be98a7SChris Wilson 	 */
16210be98a7SChris Wilson 
1634b3823ffSTvrtko Ursulin 	for_each_gt(gt, i915, i)
1644b3823ffSTvrtko Ursulin 		intel_gt_suspend_late(gt);
165c601cb21SChris Wilson 
166ecab9be1SChris Wilson 	spin_lock_irqsave(&i915->mm.obj_lock, flags);
16710be98a7SChris Wilson 	for (phase = phases; *phase; phase++) {
168ac05a22cSChris Wilson 		list_for_each_entry(obj, *phase, mm.link) {
169ac05a22cSChris Wilson 			if (!(obj->cache_coherent & I915_BO_CACHE_COHERENT_FOR_READ))
170ac05a22cSChris Wilson 				flush |= (obj->read_domains & I915_GEM_DOMAIN_CPU) == 0;
171ac05a22cSChris Wilson 			__start_cpu_write(obj); /* presume auto-hibernate */
17210be98a7SChris Wilson 		}
1736951e589SChris Wilson 	}
174ecab9be1SChris Wilson 	spin_unlock_irqrestore(&i915->mm.obj_lock, flags);
175ac05a22cSChris Wilson 	if (flush)
176ac05a22cSChris Wilson 		wbinvd_on_all_cpus();
17710be98a7SChris Wilson }
17810be98a7SChris Wilson 
i915_gem_freeze(struct drm_i915_private * i915)17929d88083SChris Wilson int i915_gem_freeze(struct drm_i915_private *i915)
18029d88083SChris Wilson {
18129d88083SChris Wilson 	/* Discard all purgeable objects, let userspace recover those as
18229d88083SChris Wilson 	 * required after resuming.
18329d88083SChris Wilson 	 */
18429d88083SChris Wilson 	i915_gem_shrink_all(i915);
18529d88083SChris Wilson 
18629d88083SChris Wilson 	return 0;
18729d88083SChris Wilson }
18829d88083SChris Wilson 
i915_gem_freeze_late(struct drm_i915_private * i915)18929d88083SChris Wilson int i915_gem_freeze_late(struct drm_i915_private *i915)
19029d88083SChris Wilson {
19129d88083SChris Wilson 	struct drm_i915_gem_object *obj;
19229d88083SChris Wilson 	intel_wakeref_t wakeref;
19329d88083SChris Wilson 
19429d88083SChris Wilson 	/*
19529d88083SChris Wilson 	 * Called just before we write the hibernation image.
19629d88083SChris Wilson 	 *
19729d88083SChris Wilson 	 * We need to update the domain tracking to reflect that the CPU
19829d88083SChris Wilson 	 * will be accessing all the pages to create and restore from the
19929d88083SChris Wilson 	 * hibernation, and so upon restoration those pages will be in the
20029d88083SChris Wilson 	 * CPU domain.
20129d88083SChris Wilson 	 *
20229d88083SChris Wilson 	 * To make sure the hibernation image contains the latest state,
20329d88083SChris Wilson 	 * we update that state just before writing out the image.
20429d88083SChris Wilson 	 *
20529d88083SChris Wilson 	 * To try and reduce the hibernation image, we manually shrink
20629d88083SChris Wilson 	 * the objects as well, see i915_gem_freeze()
20729d88083SChris Wilson 	 */
20829d88083SChris Wilson 
20929d88083SChris Wilson 	with_intel_runtime_pm(&i915->runtime_pm, wakeref)
210cf41a8f1SMaarten Lankhorst 		i915_gem_shrink(NULL, i915, -1UL, NULL, ~0);
21129d88083SChris Wilson 	i915_gem_drain_freed_objects(i915);
21229d88083SChris Wilson 
21329d88083SChris Wilson 	wbinvd_on_all_cpus();
21429d88083SChris Wilson 	list_for_each_entry(obj, &i915->mm.shrink_list, mm.link)
21529d88083SChris Wilson 		__start_cpu_write(obj);
21629d88083SChris Wilson 
21729d88083SChris Wilson 	return 0;
21829d88083SChris Wilson }
21929d88083SChris Wilson 
i915_gem_resume(struct drm_i915_private * i915)22010be98a7SChris Wilson void i915_gem_resume(struct drm_i915_private *i915)
22110be98a7SChris Wilson {
222e23a4004STvrtko Ursulin 	struct intel_gt *gt;
223e23a4004STvrtko Ursulin 	int ret, i, j;
224c56ce956SThomas Hellström 
225639f2f24SVenkata Sandeep Dhanalakota 	GEM_TRACE("%s\n", dev_name(i915->drm.dev));
22610be98a7SChris Wilson 
227c56ce956SThomas Hellström 	ret = lmem_restore(i915, 0);
228c56ce956SThomas Hellström 	GEM_WARN_ON(ret);
229c56ce956SThomas Hellström 
23010be98a7SChris Wilson 	/*
23110be98a7SChris Wilson 	 * As we didn't flush the kernel context before suspend, we cannot
23210be98a7SChris Wilson 	 * guarantee that the context image is complete. So let's just reset
23310be98a7SChris Wilson 	 * it and start again.
23410be98a7SChris Wilson 	 */
235e23a4004STvrtko Ursulin 	for_each_gt(gt, i915, i)
236e23a4004STvrtko Ursulin 		if (intel_gt_resume(gt))
237e23a4004STvrtko Ursulin 			goto err_wedged;
238c56ce956SThomas Hellström 
239c56ce956SThomas Hellström 	ret = lmem_restore(i915, I915_TTM_BACKUP_ALLOW_GPU);
240c56ce956SThomas Hellström 	GEM_WARN_ON(ret);
241e23a4004STvrtko Ursulin 
242e23a4004STvrtko Ursulin 	return;
243e23a4004STvrtko Ursulin 
244e23a4004STvrtko Ursulin err_wedged:
245e23a4004STvrtko Ursulin 	for_each_gt(gt, i915, j) {
246e23a4004STvrtko Ursulin 		if (!intel_gt_is_wedged(gt)) {
247e23a4004STvrtko Ursulin 			dev_err(i915->drm.dev,
248e23a4004STvrtko Ursulin 				"Failed to re-initialize GPU[%u], declaring it wedged!\n",
249e23a4004STvrtko Ursulin 				j);
250e23a4004STvrtko Ursulin 			intel_gt_set_wedged(gt);
251e23a4004STvrtko Ursulin 		}
252e23a4004STvrtko Ursulin 
253e23a4004STvrtko Ursulin 		if (j == i)
254e23a4004STvrtko Ursulin 			break;
255e23a4004STvrtko Ursulin 	}
25610be98a7SChris Wilson }
257