1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
6 #include <linux/suspend.h>
9 #include "i915_params.h"
10 #include "intel_context.h"
11 #include "intel_engine_pm.h"
13 #include "intel_gt_clock_utils.h"
14 #include "intel_gt_pm.h"
15 #include "intel_gt_requests.h"
16 #include "intel_llc.h"
18 #include "intel_rc6.h"
19 #include "intel_rps.h"
20 #include "intel_wakeref.h"
22 static void user_forcewake(struct intel_gt *gt, bool suspend)
24 int count = atomic_read(>->user_wakeref);
26 /* Inside suspend/resume so single threaded, no races to worry about. */
32 GEM_BUG_ON(count > atomic_read(>->wakeref.count));
33 atomic_sub(count, >->wakeref.count);
35 atomic_add(count, >->wakeref.count);
40 static void runtime_begin(struct intel_gt *gt)
43 write_seqcount_begin(>->stats.lock);
44 gt->stats.start = ktime_get();
45 gt->stats.active = true;
46 write_seqcount_end(>->stats.lock);
50 static void runtime_end(struct intel_gt *gt)
53 write_seqcount_begin(>->stats.lock);
54 gt->stats.active = false;
56 ktime_add(gt->stats.total,
57 ktime_sub(ktime_get(), gt->stats.start));
58 write_seqcount_end(>->stats.lock);
62 static int __gt_unpark(struct intel_wakeref *wf)
64 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
65 struct drm_i915_private *i915 = gt->i915;
70 * It seems that the DMC likes to transition between the DC states a lot
71 * when there are no connected displays (no active power domains) during
74 * This activity has negative impact on the performance of the chip with
75 * huge latencies observed in the interrupt handler and elsewhere.
77 * Work around it by grabbing a GT IRQ power domain whilst there is any
78 * GT activity, preventing any DC state transitions.
80 gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
81 GEM_BUG_ON(!gt->awake);
83 intel_rc6_unpark(>->rc6);
84 intel_rps_unpark(>->rps);
85 i915_pmu_gt_unparked(i915);
87 intel_gt_unpark_requests(gt);
93 static int __gt_park(struct intel_wakeref *wf)
95 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
96 intel_wakeref_t wakeref = fetch_and_zero(>->awake);
97 struct drm_i915_private *i915 = gt->i915;
102 intel_gt_park_requests(gt);
105 i915_pmu_gt_parked(i915);
106 intel_rps_park(>->rps);
107 intel_rc6_park(>->rc6);
109 /* Everything switched off, flush any residual interrupt just in case */
110 intel_synchronize_irq(i915);
112 /* Defer dropping the display power well for 100ms, it's slow! */
113 GEM_BUG_ON(!wakeref);
114 intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
119 static const struct intel_wakeref_ops wf_ops = {
124 void intel_gt_pm_init_early(struct intel_gt *gt)
126 intel_wakeref_init(>->wakeref, gt->uncore->rpm, &wf_ops);
127 seqcount_mutex_init(>->stats.lock, >->wakeref.mutex);
130 void intel_gt_pm_init(struct intel_gt *gt)
133 * Enabling power-management should be "self-healing". If we cannot
134 * enable a feature, simply leave it disabled with a notice to the
137 intel_rc6_init(>->rc6);
138 intel_rps_init(>->rps);
141 static bool reset_engines(struct intel_gt *gt)
143 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
146 return __intel_gt_reset(gt, ALL_ENGINES) == 0;
149 static void gt_sanitize(struct intel_gt *gt, bool force)
151 struct intel_engine_cs *engine;
152 enum intel_engine_id id;
153 intel_wakeref_t wakeref;
155 GT_TRACE(gt, "force:%s", yesno(force));
157 /* Use a raw wakeref to avoid calling intel_display_power_get early */
158 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
159 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
161 intel_gt_check_clock_frequency(gt);
164 * As we have just resumed the machine and woken the device up from
165 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
166 * back to defaults, recovering from whatever wedged state we left it
167 * in and so worth trying to use the device once more.
169 if (intel_gt_is_wedged(gt))
170 intel_gt_unset_wedged(gt);
172 for_each_engine(engine, gt, id)
173 if (engine->reset.prepare)
174 engine->reset.prepare(engine);
176 intel_uc_reset_prepare(>->uc);
178 for_each_engine(engine, gt, id)
179 if (engine->sanitize)
180 engine->sanitize(engine);
182 if (reset_engines(gt) || force) {
183 for_each_engine(engine, gt, id)
184 __intel_engine_reset(engine, false);
187 intel_uc_reset(>->uc, false);
189 for_each_engine(engine, gt, id)
190 if (engine->reset.finish)
191 engine->reset.finish(engine);
193 intel_rps_sanitize(>->rps);
195 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
196 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
199 void intel_gt_pm_fini(struct intel_gt *gt)
201 intel_rc6_fini(>->rc6);
204 int intel_gt_resume(struct intel_gt *gt)
206 struct intel_engine_cs *engine;
207 enum intel_engine_id id;
210 err = intel_gt_has_unrecoverable_error(gt);
217 * After resume, we may need to poke into the pinned kernel
218 * contexts to paper over any damage caused by the sudden suspend.
219 * Only the kernel contexts should remain pinned over suspend,
220 * allowing us to fixup the user contexts on their first pin.
222 gt_sanitize(gt, true);
226 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
227 intel_rc6_sanitize(>->rc6);
228 if (intel_gt_is_wedged(gt)) {
233 /* Only when the HW is re-initialised, can we replay the requests */
234 err = intel_gt_init_hw(gt);
236 i915_probe_error(gt->i915,
237 "Failed to initialize GPU, declaring it wedged!\n");
241 intel_uc_reset_finish(>->uc);
243 intel_rps_enable(>->rps);
244 intel_llc_enable(>->llc);
246 for_each_engine(engine, gt, id) {
247 intel_engine_pm_get(engine);
249 engine->serial++; /* kernel context lost */
250 err = intel_engine_resume(engine);
252 intel_engine_pm_put(engine);
254 drm_err(>->i915->drm,
255 "Failed to restart %s (%d)\n",
261 intel_rc6_enable(>->rc6);
263 intel_uc_resume(>->uc);
265 user_forcewake(gt, false);
268 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
273 intel_gt_set_wedged(gt);
277 static void wait_for_suspend(struct intel_gt *gt)
279 if (!intel_gt_pm_is_awake(gt))
282 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
284 * Forcibly cancel outstanding work and leave
287 intel_gt_set_wedged(gt);
288 intel_gt_retire_requests(gt);
291 intel_gt_pm_wait_for_idle(gt);
294 void intel_gt_suspend_prepare(struct intel_gt *gt)
296 user_forcewake(gt, true);
297 wait_for_suspend(gt);
299 intel_uc_suspend(>->uc);
302 static suspend_state_t pm_suspend_target(void)
304 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
305 return pm_suspend_target_state;
307 return PM_SUSPEND_TO_IDLE;
311 void intel_gt_suspend_late(struct intel_gt *gt)
313 intel_wakeref_t wakeref;
315 /* We expect to be idle already; but also want to be independent */
316 wait_for_suspend(gt);
321 GEM_BUG_ON(gt->awake);
324 * On disabling the device, we want to turn off HW access to memory
325 * that we no longer own.
327 * However, not all suspend-states disable the device. S0 (s2idle)
328 * is effectively runtime-suspend, the device is left powered on
329 * but needs to be put into a low power state. We need to keep
330 * powermanagement enabled, but we also retain system state and so
331 * it remains safe to keep on using our allocated memory.
333 if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
336 with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
337 intel_rps_disable(>->rps);
338 intel_rc6_disable(>->rc6);
339 intel_llc_disable(>->llc);
342 gt_sanitize(gt, false);
347 void intel_gt_runtime_suspend(struct intel_gt *gt)
349 intel_uc_runtime_suspend(>->uc);
354 int intel_gt_runtime_resume(struct intel_gt *gt)
357 intel_gt_init_swizzling(gt);
358 intel_ggtt_restore_fences(gt->ggtt);
360 return intel_uc_runtime_resume(>->uc);
363 static ktime_t __intel_gt_get_awake_time(const struct intel_gt *gt)
365 ktime_t total = gt->stats.total;
367 if (gt->stats.active)
368 total = ktime_add(total,
369 ktime_sub(ktime_get(), gt->stats.start));
374 ktime_t intel_gt_get_awake_time(const struct intel_gt *gt)
380 seq = read_seqcount_begin(>->stats.lock);
381 total = __intel_gt_get_awake_time(gt);
382 } while (read_seqcount_retry(>->stats.lock, seq));
387 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
388 #include "selftest_gt_pm.c"