treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / gpu / drm / i915 / gt / intel_gt_pm.c
blobd1c2f034296aca8d65caa89a965c3bd1163f5dc4
1 /*
2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
5 */
7 #include <linux/suspend.h>
9 #include "i915_drv.h"
10 #include "i915_globals.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_pm.h"
16 #include "intel_gt_requests.h"
17 #include "intel_llc.h"
18 #include "intel_pm.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(&gt->user_wakeref);
27 /* Inside suspend/resume so single threaded, no races to worry about. */
28 if (likely(!count))
29 return;
31 intel_gt_pm_get(gt);
32 if (suspend) {
33 GEM_BUG_ON(count > atomic_read(&gt->wakeref.count));
34 atomic_sub(count, &gt->wakeref.count);
35 } else {
36 atomic_add(count, &gt->wakeref.count);
38 intel_gt_pm_put(gt);
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;
46 GT_TRACE(gt, "\n");
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
53 * command submission.
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(&gt->rc6);
65 intel_rps_unpark(&gt->rps);
66 i915_pmu_gt_unparked(i915);
68 intel_gt_unpark_requests(gt);
70 return 0;
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(&gt->awake);
77 struct drm_i915_private *i915 = gt->i915;
79 GT_TRACE(gt, "\n");
81 intel_gt_park_requests(gt);
83 i915_vma_parked(gt);
84 i915_pmu_gt_parked(i915);
85 intel_rps_park(&gt->rps);
86 intel_rc6_park(&gt->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! */
92 GEM_BUG_ON(!wakeref);
93 intel_display_power_put_async(i915, POWER_DOMAIN_GT_IRQ, wakeref);
95 i915_globals_park();
97 return 0;
100 static const struct intel_wakeref_ops wf_ops = {
101 .get = __gt_unpark,
102 .put = __gt_park,
105 void intel_gt_pm_init_early(struct intel_gt *gt)
107 intel_wakeref_init(&gt->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
115 * user.
117 intel_rc6_init(&gt->rc6);
118 intel_rps_init(&gt->rps);
121 static bool reset_engines(struct intel_gt *gt)
123 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
124 return false;
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(&gt->uc);
152 for_each_engine(engine, gt, id)
153 if (engine->reset.prepare)
154 engine->reset.prepare(engine);
156 intel_uc_reset_prepare(&gt->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(&gt->rc6);
176 int intel_gt_resume(struct intel_gt *gt)
178 struct intel_engine_cs *engine;
179 enum intel_engine_id id;
180 int err;
182 err = intel_gt_has_init_error(gt);
183 if (err)
184 return err;
186 GT_TRACE(gt, "\n");
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.
194 intel_gt_pm_get(gt);
196 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
197 intel_rc6_sanitize(&gt->rc6);
198 gt_sanitize(gt, true);
199 if (intel_gt_is_wedged(gt)) {
200 err = -EIO;
201 goto out_fw;
204 /* Only when the HW is re-initialised, can we replay the requests */
205 err = intel_gt_init_hw(gt);
206 if (err) {
207 dev_err(gt->i915->drm.dev,
208 "Failed to initialize GPU, declaring it wedged!\n");
209 goto err_wedged;
212 intel_rps_enable(&gt->rps);
213 intel_llc_enable(&gt->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);
222 if (err) {
223 dev_err(gt->i915->drm.dev,
224 "Failed to restart %s (%d)\n",
225 engine->name, err);
226 goto err_wedged;
230 intel_rc6_enable(&gt->rc6);
232 intel_uc_resume(&gt->uc);
234 user_forcewake(gt, false);
236 out_fw:
237 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
238 intel_gt_pm_put(gt);
239 return err;
241 err_wedged:
242 intel_gt_set_wedged(gt);
243 goto out_fw;
246 static void wait_for_suspend(struct intel_gt *gt)
248 if (!intel_gt_pm_is_awake(gt))
249 return;
251 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
253 * Forcibly cancel outstanding work and leave
254 * the gpu quiet.
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(&gt->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;
275 #else
276 return PM_SUSPEND_TO_IDLE;
277 #endif
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);
287 if (is_mock_gt(gt))
288 return;
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)
303 return;
305 with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
306 intel_rps_disable(&gt->rps);
307 intel_rc6_disable(&gt->rc6);
308 intel_llc_disable(&gt->llc);
311 gt_sanitize(gt, false);
313 GT_TRACE(gt, "\n");
316 void intel_gt_runtime_suspend(struct intel_gt *gt)
318 intel_uc_runtime_suspend(&gt->uc);
320 GT_TRACE(gt, "\n");
323 int intel_gt_runtime_resume(struct intel_gt *gt)
325 GT_TRACE(gt, "\n");
326 intel_gt_init_swizzling(gt);
328 return intel_uc_runtime_resume(&gt->uc);
331 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
332 #include "selftest_gt_pm.c"
333 #endif