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_requests.h"
18 #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 i915_probe_error(gt->i915,
252 "Failed to initialize GPU, declaring it wedged!\n");
256 intel_uc_reset_finish(>->uc);
258 intel_rps_enable(>->rps);
259 intel_llc_enable(>->llc);
261 for_each_engine(engine, gt, id) {
262 intel_engine_pm_get(engine);
264 engine->serial++; /* kernel context lost */
265 err = intel_engine_resume(engine);
267 intel_engine_pm_put(engine);
269 drm_err(>->i915->drm,
270 "Failed to restart %s (%d)\n",
276 intel_rc6_enable(>->rc6);
278 intel_uc_resume(>->uc);
280 intel_pxp_resume(>->pxp);
282 user_forcewake(gt, false);
285 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
290 intel_gt_set_wedged(gt);
294 static void wait_for_suspend(struct intel_gt *gt)
296 if (!intel_gt_pm_is_awake(gt))
299 if (intel_gt_wait_for_idle(gt, I915_GT_SUSPEND_IDLE_TIMEOUT) == -ETIME) {
301 * Forcibly cancel outstanding work and leave
304 intel_gt_set_wedged(gt);
305 intel_gt_retire_requests(gt);
308 intel_gt_pm_wait_for_idle(gt);
311 void intel_gt_suspend_prepare(struct intel_gt *gt)
313 user_forcewake(gt, true);
314 wait_for_suspend(gt);
316 intel_pxp_suspend_prepare(>->pxp);
319 static suspend_state_t pm_suspend_target(void)
321 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
322 return pm_suspend_target_state;
324 return PM_SUSPEND_TO_IDLE;
328 void intel_gt_suspend_late(struct intel_gt *gt)
330 intel_wakeref_t wakeref;
332 /* We expect to be idle already; but also want to be independent */
333 wait_for_suspend(gt);
338 GEM_BUG_ON(gt->awake);
340 intel_uc_suspend(>->uc);
341 intel_pxp_suspend(>->pxp);
344 * On disabling the device, we want to turn off HW access to memory
345 * that we no longer own.
347 * However, not all suspend-states disable the device. S0 (s2idle)
348 * is effectively runtime-suspend, the device is left powered on
349 * but needs to be put into a low power state. We need to keep
350 * powermanagement enabled, but we also retain system state and so
351 * it remains safe to keep on using our allocated memory.
353 if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
356 with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
357 intel_rps_disable(>->rps);
358 intel_rc6_disable(>->rc6);
359 intel_llc_disable(>->llc);
362 gt_sanitize(gt, false);
367 void intel_gt_runtime_suspend(struct intel_gt *gt)
369 intel_pxp_runtime_suspend(>->pxp);
370 intel_uc_runtime_suspend(>->uc);
375 int intel_gt_runtime_resume(struct intel_gt *gt)
380 intel_gt_init_swizzling(gt);
381 intel_ggtt_restore_fences(gt->ggtt);
383 ret = intel_uc_runtime_resume(>->uc);
387 intel_pxp_runtime_resume(>->pxp);
392 static ktime_t __intel_gt_get_awake_time(const struct intel_gt *gt)
394 ktime_t total = gt->stats.total;
396 if (gt->stats.active)
397 total = ktime_add(total,
398 ktime_sub(ktime_get(), gt->stats.start));
403 ktime_t intel_gt_get_awake_time(const struct intel_gt *gt)
409 seq = read_seqcount_begin(>->stats.lock);
410 total = __intel_gt_get_awake_time(gt);
411 } while (read_seqcount_retry(>->stats.lock, seq));
416 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
417 #include "selftest_gt_pm.c"