xref: /openbmc/linux/drivers/gpu/drm/i915/gt/intel_gt_pm.c (revision 501f94d0)
1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2019 Intel Corporation
4  */
5 
6 #include <linux/string_helpers.h>
7 #include <linux/suspend.h>
8 
9 #include "i915_drv.h"
10 #include "i915_params.h"
11 #include "intel_context.h"
12 #include "intel_engine_pm.h"
13 #include "intel_gt.h"
14 #include "intel_gt_clock_utils.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 #include "pxp/intel_pxp_pm.h"
23 
24 #define I915_GT_SUSPEND_IDLE_TIMEOUT (HZ / 2)
25 
26 static void user_forcewake(struct intel_gt *gt, bool suspend)
27 {
28 	int count = atomic_read(&gt->user_wakeref);
29 
30 	/* Inside suspend/resume so single threaded, no races to worry about. */
31 	if (likely(!count))
32 		return;
33 
34 	intel_gt_pm_get(gt);
35 	if (suspend) {
36 		GEM_BUG_ON(count > atomic_read(&gt->wakeref.count));
37 		atomic_sub(count, &gt->wakeref.count);
38 	} else {
39 		atomic_add(count, &gt->wakeref.count);
40 	}
41 	intel_gt_pm_put(gt);
42 }
43 
44 static void runtime_begin(struct intel_gt *gt)
45 {
46 	local_irq_disable();
47 	write_seqcount_begin(&gt->stats.lock);
48 	gt->stats.start = ktime_get();
49 	gt->stats.active = true;
50 	write_seqcount_end(&gt->stats.lock);
51 	local_irq_enable();
52 }
53 
54 static void runtime_end(struct intel_gt *gt)
55 {
56 	local_irq_disable();
57 	write_seqcount_begin(&gt->stats.lock);
58 	gt->stats.active = false;
59 	gt->stats.total =
60 		ktime_add(gt->stats.total,
61 			  ktime_sub(ktime_get(), gt->stats.start));
62 	write_seqcount_end(&gt->stats.lock);
63 	local_irq_enable();
64 }
65 
66 static int __gt_unpark(struct intel_wakeref *wf)
67 {
68 	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
69 	struct drm_i915_private *i915 = gt->i915;
70 
71 	GT_TRACE(gt, "\n");
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 	intel_guc_busyness_unpark(gt);
91 
92 	intel_gt_unpark_requests(gt);
93 	runtime_begin(gt);
94 
95 	return 0;
96 }
97 
98 static int __gt_park(struct intel_wakeref *wf)
99 {
100 	struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
101 	intel_wakeref_t wakeref = fetch_and_zero(&gt->awake);
102 	struct drm_i915_private *i915 = gt->i915;
103 
104 	GT_TRACE(gt, "\n");
105 
106 	runtime_end(gt);
107 	intel_gt_park_requests(gt);
108 
109 	intel_guc_busyness_park(gt);
110 	i915_vma_parked(gt);
111 	i915_pmu_gt_parked(i915);
112 	intel_rps_park(&gt->rps);
113 	intel_rc6_park(&gt->rc6);
114 
115 	/* Everything switched off, flush any residual interrupt just in case */
116 	intel_synchronize_irq(i915);
117 
118 	/* Defer dropping the display power well for 100ms, it's slow! */
119 	GEM_BUG_ON(!wakeref);
120 	intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
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", str_yes_no(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 	for_each_engine(engine, gt, id)
179 		if (engine->reset.prepare)
180 			engine->reset.prepare(engine);
181 
182 	intel_uc_reset_prepare(&gt->uc);
183 
184 	for_each_engine(engine, gt, id)
185 		if (engine->sanitize)
186 			engine->sanitize(engine);
187 
188 	if (reset_engines(gt) || force) {
189 		for_each_engine(engine, gt, id)
190 			__intel_engine_reset(engine, false);
191 	}
192 
193 	intel_uc_reset(&gt->uc, false);
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_uc_reset_finish(&gt->uc);
248 
249 	intel_rps_enable(&gt->rps);
250 	intel_llc_enable(&gt->llc);
251 
252 	for_each_engine(engine, gt, id) {
253 		intel_engine_pm_get(engine);
254 
255 		engine->serial++; /* kernel context lost */
256 		err = intel_engine_resume(engine);
257 
258 		intel_engine_pm_put(engine);
259 		if (err) {
260 			drm_err(&gt->i915->drm,
261 				"Failed to restart %s (%d)\n",
262 				engine->name, err);
263 			goto err_wedged;
264 		}
265 	}
266 
267 	intel_rc6_enable(&gt->rc6);
268 
269 	intel_uc_resume(&gt->uc);
270 
271 	intel_pxp_resume(&gt->pxp);
272 
273 	user_forcewake(gt, false);
274 
275 out_fw:
276 	intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
277 	intel_gt_pm_put(gt);
278 	return err;
279 
280 err_wedged:
281 	intel_gt_set_wedged(gt);
282 	goto out_fw;
283 }
284 
285 static void wait_for_suspend(struct intel_gt *gt)
286 {
287 	if (!intel_gt_pm_is_awake(gt))
288 		return;
289 
290 	if (intel_gt_wait_for_idle(gt, I915_GT_SUSPEND_IDLE_TIMEOUT) == -ETIME) {
291 		/*
292 		 * Forcibly cancel outstanding work and leave
293 		 * the gpu quiet.
294 		 */
295 		intel_gt_set_wedged(gt);
296 		intel_gt_retire_requests(gt);
297 	}
298 
299 	intel_gt_pm_wait_for_idle(gt);
300 }
301 
302 void intel_gt_suspend_prepare(struct intel_gt *gt)
303 {
304 	user_forcewake(gt, true);
305 	wait_for_suspend(gt);
306 
307 	intel_pxp_suspend_prepare(&gt->pxp);
308 }
309 
310 static suspend_state_t pm_suspend_target(void)
311 {
312 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
313 	return pm_suspend_target_state;
314 #else
315 	return PM_SUSPEND_TO_IDLE;
316 #endif
317 }
318 
319 void intel_gt_suspend_late(struct intel_gt *gt)
320 {
321 	intel_wakeref_t wakeref;
322 
323 	/* We expect to be idle already; but also want to be independent */
324 	wait_for_suspend(gt);
325 
326 	if (is_mock_gt(gt))
327 		return;
328 
329 	GEM_BUG_ON(gt->awake);
330 
331 	intel_uc_suspend(&gt->uc);
332 	intel_pxp_suspend(&gt->pxp);
333 
334 	/*
335 	 * On disabling the device, we want to turn off HW access to memory
336 	 * that we no longer own.
337 	 *
338 	 * However, not all suspend-states disable the device. S0 (s2idle)
339 	 * is effectively runtime-suspend, the device is left powered on
340 	 * but needs to be put into a low power state. We need to keep
341 	 * powermanagement enabled, but we also retain system state and so
342 	 * it remains safe to keep on using our allocated memory.
343 	 */
344 	if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
345 		return;
346 
347 	with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
348 		intel_rps_disable(&gt->rps);
349 		intel_rc6_disable(&gt->rc6);
350 		intel_llc_disable(&gt->llc);
351 	}
352 
353 	gt_sanitize(gt, false);
354 
355 	GT_TRACE(gt, "\n");
356 }
357 
358 void intel_gt_runtime_suspend(struct intel_gt *gt)
359 {
360 	intel_pxp_runtime_suspend(&gt->pxp);
361 	intel_uc_runtime_suspend(&gt->uc);
362 
363 	GT_TRACE(gt, "\n");
364 }
365 
366 int intel_gt_runtime_resume(struct intel_gt *gt)
367 {
368 	int ret;
369 
370 	GT_TRACE(gt, "\n");
371 	intel_gt_init_swizzling(gt);
372 	intel_ggtt_restore_fences(gt->ggtt);
373 
374 	ret = intel_uc_runtime_resume(&gt->uc);
375 	if (ret)
376 		return ret;
377 
378 	intel_pxp_runtime_resume(&gt->pxp);
379 
380 	return 0;
381 }
382 
383 static ktime_t __intel_gt_get_awake_time(const struct intel_gt *gt)
384 {
385 	ktime_t total = gt->stats.total;
386 
387 	if (gt->stats.active)
388 		total = ktime_add(total,
389 				  ktime_sub(ktime_get(), gt->stats.start));
390 
391 	return total;
392 }
393 
394 ktime_t intel_gt_get_awake_time(const struct intel_gt *gt)
395 {
396 	unsigned int seq;
397 	ktime_t total;
398 
399 	do {
400 		seq = read_seqcount_begin(&gt->stats.lock);
401 		total = __intel_gt_get_awake_time(gt);
402 	} while (read_seqcount_retry(&gt->stats.lock, seq));
403 
404 	return total;
405 }
406 
407 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
408 #include "selftest_gt_pm.c"
409 #endif
410