xref: /openbmc/linux/drivers/gpu/drm/i915/gt/intel_gt_pm.c (revision 7b73a9c8)
1 /*
2  * SPDX-License-Identifier: MIT
3  *
4  * Copyright © 2019 Intel Corporation
5  */
6 
7 #include <linux/suspend.h>
8 
9 #include "i915_drv.h"
10 #include "i915_globals.h"
11 #include "i915_params.h"
12 #include "intel_context.h"
13 #include "intel_engine_pm.h"
14 #include "intel_gt.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 
23 static void user_forcewake(struct intel_gt *gt, bool suspend)
24 {
25 	int count = atomic_read(&gt->user_wakeref);
26 
27 	/* Inside suspend/resume so single threaded, no races to worry about. */
28 	if (likely(!count))
29 		return;
30 
31 	intel_gt_pm_get(gt);
32 	if (suspend) {
33 		GEM_BUG_ON(count > atomic_read(&gt->wakeref.count));
34 		atomic_sub(count, &gt->wakeref.count);
35 	} else {
36 		atomic_add(count, &gt->wakeref.count);
37 	}
38 	intel_gt_pm_put(gt);
39 }
40 
41 static int __gt_unpark(struct intel_wakeref *wf)
42 {
43 	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
44 	struct drm_i915_private *i915 = gt->i915;
45 
46 	GEM_TRACE("\n");
47 
48 	i915_globals_unpark();
49 
50 	/*
51 	 * It seems that the DMC likes to transition between the DC states a lot
52 	 * when there are no connected displays (no active power domains) during
53 	 * command submission.
54 	 *
55 	 * This activity has negative impact on the performance of the chip with
56 	 * huge latencies observed in the interrupt handler and elsewhere.
57 	 *
58 	 * Work around it by grabbing a GT IRQ power domain whilst there is any
59 	 * GT activity, preventing any DC state transitions.
60 	 */
61 	gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
62 	GEM_BUG_ON(!gt->awake);
63 
64 	if (NEEDS_RC6_CTX_CORRUPTION_WA(i915))
65 		intel_uncore_forcewake_get(&i915->uncore, FORCEWAKE_ALL);
66 
67 	intel_rps_unpark(&gt->rps);
68 	i915_pmu_gt_unparked(i915);
69 
70 	intel_gt_unpark_requests(gt);
71 
72 	return 0;
73 }
74 
75 static int __gt_park(struct intel_wakeref *wf)
76 {
77 	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
78 	intel_wakeref_t wakeref = fetch_and_zero(&gt->awake);
79 	struct drm_i915_private *i915 = gt->i915;
80 
81 	GEM_TRACE("\n");
82 
83 	intel_gt_park_requests(gt);
84 
85 	i915_vma_parked(gt);
86 	i915_pmu_gt_parked(i915);
87 	intel_rps_park(&gt->rps);
88 
89 	/* Everything switched off, flush any residual interrupt just in case */
90 	intel_synchronize_irq(i915);
91 
92 	if (NEEDS_RC6_CTX_CORRUPTION_WA(i915)) {
93 		intel_rc6_ctx_wa_check(&i915->gt.rc6);
94 		intel_uncore_forcewake_put(&i915->uncore, FORCEWAKE_ALL);
95 	}
96 
97 	/* Defer dropping the display power well for 100ms, it's slow! */
98 	GEM_BUG_ON(!wakeref);
99 	intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
100 
101 	i915_globals_park();
102 
103 	return 0;
104 }
105 
106 static const struct intel_wakeref_ops wf_ops = {
107 	.get = __gt_unpark,
108 	.put = __gt_park,
109 };
110 
111 void intel_gt_pm_init_early(struct intel_gt *gt)
112 {
113 	intel_wakeref_init(&gt->wakeref, gt->uncore->rpm, &wf_ops);
114 }
115 
116 void intel_gt_pm_init(struct intel_gt *gt)
117 {
118 	/*
119 	 * Enabling power-management should be "self-healing". If we cannot
120 	 * enable a feature, simply leave it disabled with a notice to the
121 	 * user.
122 	 */
123 	intel_rc6_init(&gt->rc6);
124 	intel_rps_init(&gt->rps);
125 }
126 
127 static bool reset_engines(struct intel_gt *gt)
128 {
129 	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
130 		return false;
131 
132 	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
133 }
134 
135 /**
136  * intel_gt_sanitize: called after the GPU has lost power
137  * @gt: the i915 GT container
138  * @force: ignore a failed reset and sanitize engine state anyway
139  *
140  * Anytime we reset the GPU, either with an explicit GPU reset or through a
141  * PCI power cycle, the GPU loses state and we must reset our state tracking
142  * to match. Note that calling intel_gt_sanitize() if the GPU has not
143  * been reset results in much confusion!
144  */
145 void intel_gt_sanitize(struct intel_gt *gt, bool force)
146 {
147 	struct intel_engine_cs *engine;
148 	enum intel_engine_id id;
149 	intel_wakeref_t wakeref;
150 
151 	GEM_TRACE("force:%s\n", yesno(force));
152 
153 	/* Use a raw wakeref to avoid calling intel_display_power_get early */
154 	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
155 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
156 
157 	/*
158 	 * As we have just resumed the machine and woken the device up from
159 	 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
160 	 * back to defaults, recovering from whatever wedged state we left it
161 	 * in and so worth trying to use the device once more.
162 	 */
163 	if (intel_gt_is_wedged(gt))
164 		intel_gt_unset_wedged(gt);
165 
166 	intel_uc_sanitize(&gt->uc);
167 
168 	for_each_engine(engine, gt, id)
169 		if (engine->reset.prepare)
170 			engine->reset.prepare(engine);
171 
172 	intel_uc_reset_prepare(&gt->uc);
173 
174 	if (reset_engines(gt) || force) {
175 		for_each_engine(engine, gt, id)
176 			__intel_engine_reset(engine, false);
177 	}
178 
179 	for_each_engine(engine, gt, id)
180 		if (engine->reset.finish)
181 			engine->reset.finish(engine);
182 
183 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
184 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
185 }
186 
187 void intel_gt_pm_fini(struct intel_gt *gt)
188 {
189 	intel_rc6_fini(&gt->rc6);
190 }
191 
192 int intel_gt_resume(struct intel_gt *gt)
193 {
194 	struct intel_engine_cs *engine;
195 	enum intel_engine_id id;
196 	int err = 0;
197 
198 	GEM_TRACE("\n");
199 
200 	/*
201 	 * After resume, we may need to poke into the pinned kernel
202 	 * contexts to paper over any damage caused by the sudden suspend.
203 	 * Only the kernel contexts should remain pinned over suspend,
204 	 * allowing us to fixup the user contexts on their first pin.
205 	 */
206 	intel_gt_pm_get(gt);
207 
208 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
209 	intel_rc6_sanitize(&gt->rc6);
210 
211 	intel_rps_enable(&gt->rps);
212 	intel_llc_enable(&gt->llc);
213 
214 	for_each_engine(engine, gt, id) {
215 		struct intel_context *ce;
216 
217 		intel_engine_pm_get(engine);
218 
219 		ce = engine->kernel_context;
220 		if (ce) {
221 			GEM_BUG_ON(!intel_context_is_pinned(ce));
222 			ce->ops->reset(ce);
223 		}
224 
225 		engine->serial++; /* kernel context lost */
226 		err = engine->resume(engine);
227 
228 		intel_engine_pm_put(engine);
229 		if (err) {
230 			dev_err(gt->i915->drm.dev,
231 				"Failed to restart %s (%d)\n",
232 				engine->name, err);
233 			break;
234 		}
235 	}
236 
237 	intel_rc6_enable(&gt->rc6);
238 
239 	intel_uc_resume(&gt->uc);
240 
241 	user_forcewake(gt, false);
242 
243 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
244 	intel_gt_pm_put(gt);
245 
246 	return err;
247 }
248 
249 static void wait_for_suspend(struct intel_gt *gt)
250 {
251 	if (!intel_gt_pm_is_awake(gt))
252 		return;
253 
254 	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
255 		/*
256 		 * Forcibly cancel outstanding work and leave
257 		 * the gpu quiet.
258 		 */
259 		intel_gt_set_wedged(gt);
260 	}
261 
262 	intel_gt_pm_wait_for_idle(gt);
263 }
264 
265 void intel_gt_suspend_prepare(struct intel_gt *gt)
266 {
267 	user_forcewake(gt, true);
268 	wait_for_suspend(gt);
269 
270 	intel_uc_suspend(&gt->uc);
271 }
272 
273 static suspend_state_t pm_suspend_target(void)
274 {
275 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
276 	return pm_suspend_target_state;
277 #else
278 	return PM_SUSPEND_TO_IDLE;
279 #endif
280 }
281 
282 void intel_gt_suspend_late(struct intel_gt *gt)
283 {
284 	intel_wakeref_t wakeref;
285 
286 	/* We expect to be idle already; but also want to be independent */
287 	wait_for_suspend(gt);
288 
289 	/*
290 	 * On disabling the device, we want to turn off HW access to memory
291 	 * that we no longer own.
292 	 *
293 	 * However, not all suspend-states disable the device. S0 (s2idle)
294 	 * is effectively runtime-suspend, the device is left powered on
295 	 * but needs to be put into a low power state. We need to keep
296 	 * powermanagement enabled, but we also retain system state and so
297 	 * it remains safe to keep on using our allocated memory.
298 	 */
299 	if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
300 		return;
301 
302 	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
303 		intel_rps_disable(&gt->rps);
304 		intel_rc6_disable(&gt->rc6);
305 		intel_llc_disable(&gt->llc);
306 	}
307 
308 	intel_gt_sanitize(gt, false);
309 
310 	GEM_TRACE("\n");
311 }
312 
313 void intel_gt_runtime_suspend(struct intel_gt *gt)
314 {
315 	intel_uc_runtime_suspend(&gt->uc);
316 
317 	GEM_TRACE("\n");
318 }
319 
320 int intel_gt_runtime_resume(struct intel_gt *gt)
321 {
322 	GEM_TRACE("\n");
323 
324 	intel_gt_init_swizzling(gt);
325 
326 	return intel_uc_runtime_resume(&gt->uc);
327 }
328 
329 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
330 #include "selftest_gt_pm.c"
331 #endif
332