2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
7 #include <linux/suspend.h>
10 #include "i915_globals.h"
11 #include "i915_params.h"
12 #include "intel_context.h"
13 #include "intel_engine_pm.h"
15 #include "intel_gt_pm.h"
16 #include "intel_gt_requests.h"
17 #include "intel_llc.h"
19 #include "intel_rc6.h"
20 #include "intel_rps.h"
21 #include "intel_wakeref.h"
23 static void user_forcewake(struct intel_gt *gt, bool suspend)
25 int count = atomic_read(>->user_wakeref);
27 /* Inside suspend/resume so single threaded, no races to worry about. */
33 GEM_BUG_ON(count > atomic_read(>->wakeref.count));
34 atomic_sub(count, >->wakeref.count);
36 atomic_add(count, >->wakeref.count);
41 static int __gt_unpark(struct intel_wakeref *wf)
43 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
44 struct drm_i915_private *i915 = gt->i915;
48 i915_globals_unpark();
51 * It seems that the DMC likes to transition between the DC states a lot
52 * when there are no connected displays (no active power domains) during
55 * This activity has negative impact on the performance of the chip with
56 * huge latencies observed in the interrupt handler and elsewhere.
58 * Work around it by grabbing a GT IRQ power domain whilst there is any
59 * GT activity, preventing any DC state transitions.
61 gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
62 GEM_BUG_ON(!gt->awake);
64 intel_rc6_unpark(>->rc6);
65 intel_rps_unpark(>->rps);
66 i915_pmu_gt_unparked(i915);
68 intel_gt_unpark_requests(gt);
73 static int __gt_park(struct intel_wakeref *wf)
75 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
76 intel_wakeref_t wakeref = fetch_and_zero(>->awake);
77 struct drm_i915_private *i915 = gt->i915;
81 intel_gt_park_requests(gt);
84 i915_pmu_gt_parked(i915);
85 intel_rps_park(>->rps);
86 intel_rc6_park(>->rc6);
88 /* Everything switched off, flush any residual interrupt just in case */
89 intel_synchronize_irq(i915);
91 /* Defer dropping the display power well for 100ms, it's slow! */
93 intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
100 static const struct intel_wakeref_ops wf_ops = {
105 void intel_gt_pm_init_early(struct intel_gt *gt)
107 intel_wakeref_init(>->wakeref, gt->uncore->rpm, &wf_ops);
110 void intel_gt_pm_init(struct intel_gt *gt)
113 * Enabling power-management should be "self-healing". If we cannot
114 * enable a feature, simply leave it disabled with a notice to the
117 intel_rc6_init(>->rc6);
118 intel_rps_init(>->rps);
121 static bool reset_engines(struct intel_gt *gt)
123 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
126 return __intel_gt_reset(gt, ALL_ENGINES) == 0;
129 static void gt_sanitize(struct intel_gt *gt, bool force)
131 struct intel_engine_cs *engine;
132 enum intel_engine_id id;
133 intel_wakeref_t wakeref;
135 GT_TRACE(gt, "force:%s", yesno(force));
137 /* Use a raw wakeref to avoid calling intel_display_power_get early */
138 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
139 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
142 * As we have just resumed the machine and woken the device up from
143 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
144 * back to defaults, recovering from whatever wedged state we left it
145 * in and so worth trying to use the device once more.
147 if (intel_gt_is_wedged(gt))
148 intel_gt_unset_wedged(gt);
150 intel_uc_sanitize(>->uc);
152 for_each_engine(engine, gt, id)
153 if (engine->reset.prepare)
154 engine->reset.prepare(engine);
156 intel_uc_reset_prepare(>->uc);
158 if (reset_engines(gt) || force) {
159 for_each_engine(engine, gt, id)
160 __intel_engine_reset(engine, false);
163 for_each_engine(engine, gt, id)
164 if (engine->reset.finish)
165 engine->reset.finish(engine);
167 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
168 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
171 void intel_gt_pm_fini(struct intel_gt *gt)
173 intel_rc6_fini(>->rc6);
176 int intel_gt_resume(struct intel_gt *gt)
178 struct intel_engine_cs *engine;
179 enum intel_engine_id id;
182 err = intel_gt_has_init_error(gt);
189 * After resume, we may need to poke into the pinned kernel
190 * contexts to paper over any damage caused by the sudden suspend.
191 * Only the kernel contexts should remain pinned over suspend,
192 * allowing us to fixup the user contexts on their first pin.
196 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
197 intel_rc6_sanitize(>->rc6);
198 gt_sanitize(gt, true);
199 if (intel_gt_is_wedged(gt)) {
204 /* Only when the HW is re-initialised, can we replay the requests */
205 err = intel_gt_init_hw(gt);
207 dev_err(gt->i915->drm.dev,
208 "Failed to initialize GPU, declaring it wedged!\n");
212 intel_rps_enable(>->rps);
213 intel_llc_enable(>->llc);
215 for_each_engine(engine, gt, id) {
216 intel_engine_pm_get(engine);
218 engine->serial++; /* kernel context lost */
219 err = engine->resume(engine);
221 intel_engine_pm_put(engine);
223 dev_err(gt->i915->drm.dev,
224 "Failed to restart %s (%d)\n",
230 intel_rc6_enable(>->rc6);
232 intel_uc_resume(>->uc);
234 user_forcewake(gt, false);
237 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
242 intel_gt_set_wedged(gt);
246 static void wait_for_suspend(struct intel_gt *gt)
248 if (!intel_gt_pm_is_awake(gt))
251 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
253 * Forcibly cancel outstanding work and leave
256 intel_gt_set_wedged(gt);
257 intel_gt_retire_requests(gt);
260 intel_gt_pm_wait_for_idle(gt);
263 void intel_gt_suspend_prepare(struct intel_gt *gt)
265 user_forcewake(gt, true);
266 wait_for_suspend(gt);
268 intel_uc_suspend(>->uc);
271 static suspend_state_t pm_suspend_target(void)
273 #if IS_ENABLED(CONFIG_SUSPEND) && IS_ENABLED(CONFIG_PM_SLEEP)
274 return pm_suspend_target_state;
276 return PM_SUSPEND_TO_IDLE;
280 void intel_gt_suspend_late(struct intel_gt *gt)
282 intel_wakeref_t wakeref;
284 /* We expect to be idle already; but also want to be independent */
285 wait_for_suspend(gt);
290 GEM_BUG_ON(gt->awake);
293 * On disabling the device, we want to turn off HW access to memory
294 * that we no longer own.
296 * However, not all suspend-states disable the device. S0 (s2idle)
297 * is effectively runtime-suspend, the device is left powered on
298 * but needs to be put into a low power state. We need to keep
299 * powermanagement enabled, but we also retain system state and so
300 * it remains safe to keep on using our allocated memory.
302 if (pm_suspend_target() == PM_SUSPEND_TO_IDLE)
305 with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
306 intel_rps_disable(>->rps);
307 intel_rc6_disable(>->rc6);
308 intel_llc_disable(>->llc);
311 gt_sanitize(gt, false);
316 void intel_gt_runtime_suspend(struct intel_gt *gt)
318 intel_uc_runtime_suspend(>->uc);
323 int intel_gt_runtime_resume(struct intel_gt *gt)
326 intel_gt_init_swizzling(gt);
328 return intel_uc_runtime_resume(>->uc);
331 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
332 #include "selftest_gt_pm.c"