1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include <linux/string_helpers.h>
7 #include <linux/suspend.h>
11 #include "i915_params.h"
12 #include "intel_context.h"
13 #include "intel_engine_pm.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_rc6.h"
21 #include "intel_rps.h"
22 #include "intel_wakeref.h"
23 #include "pxp/intel_pxp_pm.h"
25 #define I915_GT_SUSPEND_IDLE_TIMEOUT (HZ / 2)
27 static void user_forcewake(struct intel_gt *gt, bool suspend)
29 int count = atomic_read(>->user_wakeref);
31 /* Inside suspend/resume so single threaded, no races to worry about. */
37 GEM_BUG_ON(count > atomic_read(>->wakeref.count));
38 atomic_sub(count, >->wakeref.count);
40 atomic_add(count, >->wakeref.count);
45 static void runtime_begin(struct intel_gt *gt)
48 write_seqcount_begin(>->stats.lock);
49 gt->stats.start = ktime_get();
50 gt->stats.active = true;
51 write_seqcount_end(>->stats.lock);
55 static void runtime_end(struct intel_gt *gt)
58 write_seqcount_begin(>->stats.lock);
59 gt->stats.active = false;
61 ktime_add(gt->stats.total,
62 ktime_sub(ktime_get(), gt->stats.start));
63 write_seqcount_end(>->stats.lock);
67 static int __gt_unpark(struct intel_wakeref *wf)
69 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
70 struct drm_i915_private *i915 = gt->i915;
75 * It seems that the DMC likes to transition between the DC states a lot
76 * when there are no connected displays (no active power domains) during
79 * This activity has negative impact on the performance of the chip with
80 * huge latencies observed in the interrupt handler and elsewhere.
82 * Work around it by grabbing a GT IRQ power domain whilst there is any
83 * GT activity, preventing any DC state transitions.
85 gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
86 GEM_BUG_ON(!gt->awake);
88 intel_rc6_unpark(>->rc6);
89 intel_rps_unpark(>->rps);
90 i915_pmu_gt_unparked(i915);
91 intel_guc_busyness_unpark(gt);
93 intel_gt_unpark_requests(gt);
99 static int __gt_park(struct intel_wakeref *wf)
101 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
102 intel_wakeref_t wakeref = fetch_and_zero(>->awake);
103 struct drm_i915_private *i915 = gt->i915;
108 intel_gt_park_requests(gt);
110 intel_guc_busyness_park(gt);
112 i915_pmu_gt_parked(i915);
113 intel_rps_park(>->rps);
114 intel_rc6_park(>->rc6);
116 /* Everything switched off, flush any residual interrupt just in case */
117 intel_synchronize_irq(i915);
119 /* Defer dropping the display power well for 100ms, it's slow! */
120 GEM_BUG_ON(!wakeref);
121 intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
126 static const struct intel_wakeref_ops wf_ops = {
131 void intel_gt_pm_init_early(struct intel_gt *gt)
134 * We access the runtime_pm structure via gt->i915 here rather than
135 * gt->uncore as we do elsewhere in the file because gt->uncore is not
136 * yet initialized for all tiles at this point in the driver startup.
137 * runtime_pm is per-device rather than per-tile, so this is still the
140 intel_wakeref_init(>->wakeref, >->i915->runtime_pm, &wf_ops);
141 seqcount_mutex_init(>->stats.lock, >->wakeref.mutex);
144 void intel_gt_pm_init(struct intel_gt *gt)
147 * Enabling power-management should be "self-healing". If we cannot
148 * enable a feature, simply leave it disabled with a notice to the
151 intel_rc6_init(>->rc6);
152 intel_rps_init(>->rps);
155 static bool reset_engines(struct intel_gt *gt)
157 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
160 return __intel_gt_reset(gt, ALL_ENGINES) == 0;
163 static void gt_sanitize(struct intel_gt *gt, bool force)
165 struct intel_engine_cs *engine;
166 enum intel_engine_id id;
167 intel_wakeref_t wakeref;
169 GT_TRACE(gt, "force:%s", str_yes_no(force));
171 /* Use a raw wakeref to avoid calling intel_display_power_get early */
172 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
173 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
175 intel_gt_check_clock_frequency(gt);
178 * As we have just resumed the machine and woken the device up from
179 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
180 * back to defaults, recovering from whatever wedged state we left it
181 * in and so worth trying to use the device once more.
183 if (intel_gt_is_wedged(gt))
184 intel_gt_unset_wedged(gt);
186 /* For GuC mode, ensure submission is disabled before stopping ring */
187 intel_uc_reset_prepare(>->uc);
189 for_each_engine(engine, gt, id) {
190 if (engine->reset.prepare)
191 engine->reset.prepare(engine);
193 if (engine->sanitize)
194 engine->sanitize(engine);
197 if (reset_engines(gt) || force) {
198 for_each_engine(engine, gt, id)
199 __intel_engine_reset(engine, false);
202 intel_uc_reset(>->uc, false);
204 for_each_engine(engine, gt, id)
205 if (engine->reset.finish)
206 engine->reset.finish(engine);
208 intel_rps_sanitize(>->rps);
210 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
211 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
214 void intel_gt_pm_fini(struct intel_gt *gt)
216 intel_rc6_fini(>->rc6);
219 int intel_gt_resume(struct intel_gt *gt)
221 struct intel_engine_cs *engine;
222 enum intel_engine_id id;
225 err = intel_gt_has_unrecoverable_error(gt);
232 * After resume, we may need to poke into the pinned kernel
233 * contexts to paper over any damage caused by the sudden suspend.
234 * Only the kernel contexts should remain pinned over suspend,
235 * allowing us to fixup the user contexts on their first pin.
237 gt_sanitize(gt, true);
241 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
242 intel_rc6_sanitize(>->rc6);
243 if (intel_gt_is_wedged(gt)) {
248 /* Only when the HW is re-initialised, can we replay the requests */
249 err = intel_gt_init_hw(gt);
251 gt_probe_error(gt, "Failed to initialize GPU, declaring it wedged!\n");
255 intel_uc_reset_finish(>->uc);
257 intel_rps_enable(>->rps);
258 intel_llc_enable(>->llc);
260 for_each_engine(engine, gt, id) {
261 intel_engine_pm_get(engine);
263 engine->serial++; /* kernel context lost */
264 err = intel_engine_resume(engine);
266 intel_engine_pm_put(engine);
268 gt_err(gt, "Failed to restart %s (%d)\n",
274 intel_rc6_enable(>->rc6);
276 intel_uc_resume(>->uc);
278 user_forcewake(gt, false);
281 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
286 intel_gt_set_wedged(gt);
290 static void wait_for_suspend(struct intel_gt *gt)
292 if (!intel_gt_pm_is_awake(gt))
295 if (intel_gt_wait_for_idle(gt, I915_GT_SUSPEND_IDLE_TIMEOUT) == -ETIME) {
297 * Forcibly cancel outstanding work and leave
300 intel_gt_set_wedged(gt);
301 intel_gt_retire_requests(gt);
304 intel_gt_pm_wait_for_idle(gt);
307 void intel_gt_suspend_prepare(struct intel_gt *gt)
309 user_forcewake(gt, true);
310 wait_for_suspend(gt);
313 static suspend_state_t pm_suspend_target(void)
315 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
316 return pm_suspend_target_state;
318 return PM_SUSPEND_TO_IDLE;
322 void intel_gt_suspend_late(struct intel_gt *gt)
324 intel_wakeref_t wakeref;
326 /* We expect to be idle already; but also want to be independent */
327 wait_for_suspend(gt);
332 GEM_BUG_ON(gt->awake);
334 intel_uc_suspend(>->uc);
337 * On disabling the device, we want to turn off HW access to memory
338 * that we no longer own.
340 * However, not all suspend-states disable the device. S0 (s2idle)
341 * is effectively runtime-suspend, the device is left powered on
342 * but needs to be put into a low power state. We need to keep
343 * powermanagement enabled, but we also retain system state and so
344 * it remains safe to keep on using our allocated memory.
346 if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
349 with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
350 intel_rps_disable(>->rps);
351 intel_rc6_disable(>->rc6);
352 intel_llc_disable(>->llc);
355 gt_sanitize(gt, false);
360 void intel_gt_runtime_suspend(struct intel_gt *gt)
362 intel_uc_runtime_suspend(>->uc);
367 int intel_gt_runtime_resume(struct intel_gt *gt)
372 intel_gt_init_swizzling(gt);
373 intel_ggtt_restore_fences(gt->ggtt);
375 ret = intel_uc_runtime_resume(>->uc);
382 static ktime_t __intel_gt_get_awake_time(const struct intel_gt *gt)
384 ktime_t total = gt->stats.total;
386 if (gt->stats.active)
387 total = ktime_add(total,
388 ktime_sub(ktime_get(), gt->stats.start));
393 ktime_t intel_gt_get_awake_time(const struct intel_gt *gt)
399 seq = read_seqcount_begin(>->stats.lock);
400 total = __intel_gt_get_awake_time(gt);
401 } while (read_seqcount_retry(>->stats.lock, seq));
406 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
407 #include "selftest_gt_pm.c"