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