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