xref: /openbmc/linux/drivers/gpu/drm/i915/gt/intel_gt_pm.c (revision 31e67366)
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_clock_utils.h"
16 #include "intel_gt_pm.h"
17 #include "intel_gt_requests.h"
18 #include "intel_llc.h"
19 #include "intel_pm.h"
20 #include "intel_rc6.h"
21 #include "intel_rps.h"
22 #include "intel_wakeref.h"
23 
24 static void user_forcewake(struct intel_gt *gt, bool suspend)
25 {
26 	int count = atomic_read(&gt->user_wakeref);
27 
28 	/* Inside suspend/resume so single threaded, no races to worry about. */
29 	if (likely(!count))
30 		return;
31 
32 	intel_gt_pm_get(gt);
33 	if (suspend) {
34 		GEM_BUG_ON(count > atomic_read(&gt->wakeref.count));
35 		atomic_sub(count, &gt->wakeref.count);
36 	} else {
37 		atomic_add(count, &gt->wakeref.count);
38 	}
39 	intel_gt_pm_put(gt);
40 }
41 
42 static void runtime_begin(struct intel_gt *gt)
43 {
44 	local_irq_disable();
45 	write_seqcount_begin(&gt->stats.lock);
46 	gt->stats.start = ktime_get();
47 	gt->stats.active = true;
48 	write_seqcount_end(&gt->stats.lock);
49 	local_irq_enable();
50 }
51 
52 static void runtime_end(struct intel_gt *gt)
53 {
54 	local_irq_disable();
55 	write_seqcount_begin(&gt->stats.lock);
56 	gt->stats.active = false;
57 	gt->stats.total =
58 		ktime_add(gt->stats.total,
59 			  ktime_sub(ktime_get(), gt->stats.start));
60 	write_seqcount_end(&gt->stats.lock);
61 	local_irq_enable();
62 }
63 
64 static int __gt_unpark(struct intel_wakeref *wf)
65 {
66 	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
67 	struct drm_i915_private *i915 = gt->i915;
68 
69 	GT_TRACE(gt, "\n");
70 
71 	i915_globals_unpark();
72 
73 	/*
74 	 * It seems that the DMC likes to transition between the DC states a lot
75 	 * when there are no connected displays (no active power domains) during
76 	 * command submission.
77 	 *
78 	 * This activity has negative impact on the performance of the chip with
79 	 * huge latencies observed in the interrupt handler and elsewhere.
80 	 *
81 	 * Work around it by grabbing a GT IRQ power domain whilst there is any
82 	 * GT activity, preventing any DC state transitions.
83 	 */
84 	gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
85 	GEM_BUG_ON(!gt->awake);
86 
87 	intel_rc6_unpark(&gt->rc6);
88 	intel_rps_unpark(&gt->rps);
89 	i915_pmu_gt_unparked(i915);
90 
91 	intel_gt_unpark_requests(gt);
92 	runtime_begin(gt);
93 
94 	return 0;
95 }
96 
97 static int __gt_park(struct intel_wakeref *wf)
98 {
99 	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
100 	intel_wakeref_t wakeref = fetch_and_zero(&gt->awake);
101 	struct drm_i915_private *i915 = gt->i915;
102 
103 	GT_TRACE(gt, "\n");
104 
105 	runtime_end(gt);
106 	intel_gt_park_requests(gt);
107 
108 	i915_vma_parked(gt);
109 	i915_pmu_gt_parked(i915);
110 	intel_rps_park(&gt->rps);
111 	intel_rc6_park(&gt->rc6);
112 
113 	/* Everything switched off, flush any residual interrupt just in case */
114 	intel_synchronize_irq(i915);
115 
116 	/* Defer dropping the display power well for 100ms, it's slow! */
117 	GEM_BUG_ON(!wakeref);
118 	intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
119 
120 	i915_globals_park();
121 
122 	return 0;
123 }
124 
125 static const struct intel_wakeref_ops wf_ops = {
126 	.get = __gt_unpark,
127 	.put = __gt_park,
128 };
129 
130 void intel_gt_pm_init_early(struct intel_gt *gt)
131 {
132 	intel_wakeref_init(&gt->wakeref, gt->uncore->rpm, &wf_ops);
133 	seqcount_mutex_init(&gt->stats.lock, &gt->wakeref.mutex);
134 }
135 
136 void intel_gt_pm_init(struct intel_gt *gt)
137 {
138 	/*
139 	 * Enabling power-management should be "self-healing". If we cannot
140 	 * enable a feature, simply leave it disabled with a notice to the
141 	 * user.
142 	 */
143 	intel_rc6_init(&gt->rc6);
144 	intel_rps_init(&gt->rps);
145 }
146 
147 static bool reset_engines(struct intel_gt *gt)
148 {
149 	if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
150 		return false;
151 
152 	return __intel_gt_reset(gt, ALL_ENGINES) == 0;
153 }
154 
155 static void gt_sanitize(struct intel_gt *gt, bool force)
156 {
157 	struct intel_engine_cs *engine;
158 	enum intel_engine_id id;
159 	intel_wakeref_t wakeref;
160 
161 	GT_TRACE(gt, "force:%s", yesno(force));
162 
163 	/* Use a raw wakeref to avoid calling intel_display_power_get early */
164 	wakeref = intel_runtime_pm_get(gt->uncore->rpm);
165 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
166 
167 	intel_gt_check_clock_frequency(gt);
168 
169 	/*
170 	 * As we have just resumed the machine and woken the device up from
171 	 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
172 	 * back to defaults, recovering from whatever wedged state we left it
173 	 * in and so worth trying to use the device once more.
174 	 */
175 	if (intel_gt_is_wedged(gt))
176 		intel_gt_unset_wedged(gt);
177 
178 	intel_uc_sanitize(&gt->uc);
179 
180 	for_each_engine(engine, gt, id)
181 		if (engine->reset.prepare)
182 			engine->reset.prepare(engine);
183 
184 	intel_uc_reset_prepare(&gt->uc);
185 
186 	for_each_engine(engine, gt, id)
187 		if (engine->sanitize)
188 			engine->sanitize(engine);
189 
190 	if (reset_engines(gt) || force) {
191 		for_each_engine(engine, gt, id)
192 			__intel_engine_reset(engine, false);
193 	}
194 
195 	for_each_engine(engine, gt, id)
196 		if (engine->reset.finish)
197 			engine->reset.finish(engine);
198 
199 	intel_rps_sanitize(&gt->rps);
200 
201 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
202 	intel_runtime_pm_put(gt->uncore->rpm, wakeref);
203 }
204 
205 void intel_gt_pm_fini(struct intel_gt *gt)
206 {
207 	intel_rc6_fini(&gt->rc6);
208 }
209 
210 int intel_gt_resume(struct intel_gt *gt)
211 {
212 	struct intel_engine_cs *engine;
213 	enum intel_engine_id id;
214 	int err;
215 
216 	err = intel_gt_has_unrecoverable_error(gt);
217 	if (err)
218 		return err;
219 
220 	GT_TRACE(gt, "\n");
221 
222 	/*
223 	 * After resume, we may need to poke into the pinned kernel
224 	 * contexts to paper over any damage caused by the sudden suspend.
225 	 * Only the kernel contexts should remain pinned over suspend,
226 	 * allowing us to fixup the user contexts on their first pin.
227 	 */
228 	gt_sanitize(gt, true);
229 
230 	intel_gt_pm_get(gt);
231 
232 	intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
233 	intel_rc6_sanitize(&gt->rc6);
234 	if (intel_gt_is_wedged(gt)) {
235 		err = -EIO;
236 		goto out_fw;
237 	}
238 
239 	/* Only when the HW is re-initialised, can we replay the requests */
240 	err = intel_gt_init_hw(gt);
241 	if (err) {
242 		i915_probe_error(gt->i915,
243 				 "Failed to initialize GPU, declaring it wedged!\n");
244 		goto err_wedged;
245 	}
246 
247 	intel_rps_enable(&gt->rps);
248 	intel_llc_enable(&gt->llc);
249 
250 	for_each_engine(engine, gt, id) {
251 		intel_engine_pm_get(engine);
252 
253 		engine->serial++; /* kernel context lost */
254 		err = intel_engine_resume(engine);
255 
256 		intel_engine_pm_put(engine);
257 		if (err) {
258 			drm_err(&gt->i915->drm,
259 				"Failed to restart %s (%d)\n",
260 				engine->name, err);
261 			goto err_wedged;
262 		}
263 	}
264 
265 	intel_rc6_enable(&gt->rc6);
266 
267 	intel_uc_resume(&gt->uc);
268 
269 	user_forcewake(gt, false);
270 
271 out_fw:
272 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
273 	intel_gt_pm_put(gt);
274 	return err;
275 
276 err_wedged:
277 	intel_gt_set_wedged(gt);
278 	goto out_fw;
279 }
280 
281 static void wait_for_suspend(struct intel_gt *gt)
282 {
283 	if (!intel_gt_pm_is_awake(gt))
284 		return;
285 
286 	if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
287 		/*
288 		 * Forcibly cancel outstanding work and leave
289 		 * the gpu quiet.
290 		 */
291 		intel_gt_set_wedged(gt);
292 		intel_gt_retire_requests(gt);
293 	}
294 
295 	intel_gt_pm_wait_for_idle(gt);
296 }
297 
298 void intel_gt_suspend_prepare(struct intel_gt *gt)
299 {
300 	user_forcewake(gt, true);
301 	wait_for_suspend(gt);
302 
303 	intel_uc_suspend(&gt->uc);
304 }
305 
306 static suspend_state_t pm_suspend_target(void)
307 {
308 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
309 	return pm_suspend_target_state;
310 #else
311 	return PM_SUSPEND_TO_IDLE;
312 #endif
313 }
314 
315 void intel_gt_suspend_late(struct intel_gt *gt)
316 {
317 	intel_wakeref_t wakeref;
318 
319 	/* We expect to be idle already; but also want to be independent */
320 	wait_for_suspend(gt);
321 
322 	if (is_mock_gt(gt))
323 		return;
324 
325 	GEM_BUG_ON(gt->awake);
326 
327 	/*
328 	 * On disabling the device, we want to turn off HW access to memory
329 	 * that we no longer own.
330 	 *
331 	 * However, not all suspend-states disable the device. S0 (s2idle)
332 	 * is effectively runtime-suspend, the device is left powered on
333 	 * but needs to be put into a low power state. We need to keep
334 	 * powermanagement enabled, but we also retain system state and so
335 	 * it remains safe to keep on using our allocated memory.
336 	 */
337 	if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
338 		return;
339 
340 	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
341 		intel_rps_disable(&gt->rps);
342 		intel_rc6_disable(&gt->rc6);
343 		intel_llc_disable(&gt->llc);
344 	}
345 
346 	gt_sanitize(gt, false);
347 
348 	GT_TRACE(gt, "\n");
349 }
350 
351 void intel_gt_runtime_suspend(struct intel_gt *gt)
352 {
353 	intel_uc_runtime_suspend(&gt->uc);
354 
355 	GT_TRACE(gt, "\n");
356 }
357 
358 int intel_gt_runtime_resume(struct intel_gt *gt)
359 {
360 	GT_TRACE(gt, "\n");
361 	intel_gt_init_swizzling(gt);
362 	intel_ggtt_restore_fences(gt->ggtt);
363 
364 	return intel_uc_runtime_resume(&gt->uc);
365 }
366 
367 static ktime_t __intel_gt_get_awake_time(const struct intel_gt *gt)
368 {
369 	ktime_t total = gt->stats.total;
370 
371 	if (gt->stats.active)
372 		total = ktime_add(total,
373 				  ktime_sub(ktime_get(), gt->stats.start));
374 
375 	return total;
376 }
377 
378 ktime_t intel_gt_get_awake_time(const struct intel_gt *gt)
379 {
380 	unsigned int seq;
381 	ktime_t total;
382 
383 	do {
384 		seq = read_seqcount_begin(&gt->stats.lock);
385 		total = __intel_gt_get_awake_time(gt);
386 	} while (read_seqcount_retry(&gt->stats.lock, seq));
387 
388 	return total;
389 }
390 
391 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
392 #include "selftest_gt_pm.c"
393 #endif
394