xref: /openbmc/linux/drivers/gpu/drm/i915/gt/intel_gt_pm.c (revision aeb64ff3)
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 	GEM_BUG_ON(!wakeref);
98 	intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);
99 
100 	i915_globals_park();
101 
102 	return 0;
103 }
104 
105 static const struct intel_wakeref_ops wf_ops = {
106 	.get = __gt_unpark,
107 	.put = __gt_park,
108 };
109 
110 void intel_gt_pm_init_early(struct intel_gt *gt)
111 {
112 	intel_wakeref_init(&gt->wakeref, gt->uncore->rpm, &wf_ops);
113 }
114 
115 void intel_gt_pm_init(struct intel_gt *gt)
116 {
117 	/*
118 	 * Enabling power-management should be "self-healing". If we cannot
119 	 * enable a feature, simply leave it disabled with a notice to the
120 	 * user.
121 	 */
122 	intel_rc6_init(&gt->rc6);
123 	intel_rps_init(&gt->rps);
124 }
125 
126 static bool reset_engines(struct intel_gt *gt)
127 {
128 	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
129 		return false;
130 
131 	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
132 }
133 
134 /**
135  * intel_gt_sanitize: called after the GPU has lost power
136  * @gt: the i915 GT container
137  * @force: ignore a failed reset and sanitize engine state anyway
138  *
139  * Anytime we reset the GPU, either with an explicit GPU reset or through a
140  * PCI power cycle, the GPU loses state and we must reset our state tracking
141  * to match. Note that calling intel_gt_sanitize() if the GPU has not
142  * been reset results in much confusion!
143  */
144 void intel_gt_sanitize(struct intel_gt *gt, bool force)
145 {
146 	struct intel_engine_cs *engine;
147 	enum intel_engine_id id;
148 	intel_wakeref_t wakeref;
149 
150 	GEM_TRACE("force:%s\n", yesno(force));
151 
152 	/* Use a raw wakeref to avoid calling intel_display_power_get early */
153 	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
154 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
155 
156 	/*
157 	 * As we have just resumed the machine and woken the device up from
158 	 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
159 	 * back to defaults, recovering from whatever wedged state we left it
160 	 * in and so worth trying to use the device once more.
161 	 */
162 	if (intel_gt_is_wedged(gt))
163 		intel_gt_unset_wedged(gt);
164 
165 	intel_uc_sanitize(&gt->uc);
166 
167 	for_each_engine(engine, gt, id)
168 		if (engine->reset.prepare)
169 			engine->reset.prepare(engine);
170 
171 	intel_uc_reset_prepare(&gt->uc);
172 
173 	if (reset_engines(gt) || force) {
174 		for_each_engine(engine, gt, id)
175 			__intel_engine_reset(engine, false);
176 	}
177 
178 	for_each_engine(engine, gt, id)
179 		if (engine->reset.finish)
180 			engine->reset.finish(engine);
181 
182 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
183 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
184 }
185 
186 void intel_gt_pm_fini(struct intel_gt *gt)
187 {
188 	intel_rc6_fini(&gt->rc6);
189 }
190 
191 int intel_gt_resume(struct intel_gt *gt)
192 {
193 	struct intel_engine_cs *engine;
194 	enum intel_engine_id id;
195 	int err = 0;
196 
197 	GEM_TRACE("\n");
198 
199 	/*
200 	 * After resume, we may need to poke into the pinned kernel
201 	 * contexts to paper over any damage caused by the sudden suspend.
202 	 * Only the kernel contexts should remain pinned over suspend,
203 	 * allowing us to fixup the user contexts on their first pin.
204 	 */
205 	intel_gt_pm_get(gt);
206 
207 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
208 	intel_rc6_sanitize(&gt->rc6);
209 
210 	intel_rps_enable(&gt->rps);
211 	intel_llc_enable(&gt->llc);
212 
213 	for_each_engine(engine, gt, id) {
214 		struct intel_context *ce;
215 
216 		intel_engine_pm_get(engine);
217 
218 		ce = engine->kernel_context;
219 		if (ce) {
220 			GEM_BUG_ON(!intel_context_is_pinned(ce));
221 			ce->ops->reset(ce);
222 		}
223 
224 		engine->serial++; /* kernel context lost */
225 		err = engine->resume(engine);
226 
227 		intel_engine_pm_put(engine);
228 		if (err) {
229 			dev_err(gt->i915->drm.dev,
230 				"Failed to restart %s (%d)\n",
231 				engine->name, err);
232 			break;
233 		}
234 	}
235 
236 	intel_rc6_enable(&gt->rc6);
237 
238 	intel_uc_resume(&gt->uc);
239 
240 	user_forcewake(gt, false);
241 
242 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
243 	intel_gt_pm_put(gt);
244 
245 	return err;
246 }
247 
248 static void wait_for_suspend(struct intel_gt *gt)
249 {
250 	if (!intel_gt_pm_is_awake(gt))
251 		return;
252 
253 	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
254 		/*
255 		 * Forcibly cancel outstanding work and leave
256 		 * the gpu quiet.
257 		 */
258 		intel_gt_set_wedged(gt);
259 	}
260 
261 	intel_gt_pm_wait_for_idle(gt);
262 }
263 
264 void intel_gt_suspend_prepare(struct intel_gt *gt)
265 {
266 	user_forcewake(gt, true);
267 	wait_for_suspend(gt);
268 
269 	intel_uc_suspend(&gt->uc);
270 }
271 
272 static suspend_state_t pm_suspend_target(void)
273 {
274 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
275 	return pm_suspend_target_state;
276 #else
277 	return PM_SUSPEND_TO_IDLE;
278 #endif
279 }
280 
281 void intel_gt_suspend_late(struct intel_gt *gt)
282 {
283 	intel_wakeref_t wakeref;
284 
285 	/* We expect to be idle already; but also want to be independent */
286 	wait_for_suspend(gt);
287 
288 	/*
289 	 * On disabling the device, we want to turn off HW access to memory
290 	 * that we no longer own.
291 	 *
292 	 * However, not all suspend-states disable the device. S0 (s2idle)
293 	 * is effectively runtime-suspend, the device is left powered on
294 	 * but needs to be put into a low power state. We need to keep
295 	 * powermanagement enabled, but we also retain system state and so
296 	 * it remains safe to keep on using our allocated memory.
297 	 */
298 	if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
299 		return;
300 
301 	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
302 		intel_rps_disable(&gt->rps);
303 		intel_rc6_disable(&gt->rc6);
304 		intel_llc_disable(&gt->llc);
305 	}
306 
307 	intel_gt_sanitize(gt, false);
308 
309 	GEM_TRACE("\n");
310 }
311 
312 void intel_gt_runtime_suspend(struct intel_gt *gt)
313 {
314 	intel_uc_runtime_suspend(&gt->uc);
315 
316 	GEM_TRACE("\n");
317 }
318 
319 int intel_gt_runtime_resume(struct intel_gt *gt)
320 {
321 	GEM_TRACE("\n");
322 
323 	intel_gt_init_swizzling(gt);
324 
325 	return intel_uc_runtime_resume(&gt->uc);
326 }
327 
328 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
329 #include "selftest_gt_pm.c"
330 #endif
331