2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
8 #include "i915_globals.h"
9 #include "i915_params.h"
10 #include "intel_context.h"
11 #include "intel_engine_pm.h"
13 #include "intel_gt_pm.h"
14 #include "intel_gt_requests.h"
15 #include "intel_llc.h"
17 #include "intel_rc6.h"
18 #include "intel_rps.h"
19 #include "intel_wakeref.h"
21 static void user_forcewake(struct intel_gt *gt, bool suspend)
23 int count = atomic_read(>->user_wakeref);
25 /* Inside suspend/resume so single threaded, no races to worry about. */
31 GEM_BUG_ON(count > atomic_read(>->wakeref.count));
32 atomic_sub(count, >->wakeref.count);
34 atomic_add(count, >->wakeref.count);
39 static int __gt_unpark(struct intel_wakeref *wf)
41 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
42 struct drm_i915_private *i915 = gt->i915;
46 i915_globals_unpark();
49 * It seems that the DMC likes to transition between the DC states a lot
50 * when there are no connected displays (no active power domains) during
53 * This activity has negative impact on the performance of the chip with
54 * huge latencies observed in the interrupt handler and elsewhere.
56 * Work around it by grabbing a GT IRQ power domain whilst there is any
57 * GT activity, preventing any DC state transitions.
59 gt->awake = intel_display_power_get(i915, POWER_DOMAIN_GT_IRQ);
60 GEM_BUG_ON(!gt->awake);
62 intel_rps_unpark(>->rps);
63 i915_pmu_gt_unparked(i915);
65 intel_gt_unpark_requests(gt);
70 static int __gt_park(struct intel_wakeref *wf)
72 struct intel_gt *gt = container_of(wf, typeof(*gt), wakeref);
73 intel_wakeref_t wakeref = fetch_and_zero(>->awake);
74 struct drm_i915_private *i915 = gt->i915;
78 intel_gt_park_requests(gt);
81 i915_pmu_gt_parked(i915);
82 intel_rps_park(>->rps);
84 /* Everything switched off, flush any residual interrupt just in case */
85 intel_synchronize_irq(i915);
88 intel_display_power_put(i915, POWER_DOMAIN_GT_IRQ, wakeref);
95 static const struct intel_wakeref_ops wf_ops = {
98 .flags = INTEL_WAKEREF_PUT_ASYNC,
101 void intel_gt_pm_init_early(struct intel_gt *gt)
103 intel_wakeref_init(>->wakeref, gt->uncore->rpm, &wf_ops);
106 void intel_gt_pm_init(struct intel_gt *gt)
109 * Enabling power-management should be "self-healing". If we cannot
110 * enable a feature, simply leave it disabled with a notice to the
113 intel_rc6_init(>->rc6);
114 intel_rps_init(>->rps);
117 static bool reset_engines(struct intel_gt *gt)
119 if (INTEL_INFO(gt->i915)->gpu_reset_clobbers_display)
122 return __intel_gt_reset(gt, ALL_ENGINES) == 0;
126 * intel_gt_sanitize: called after the GPU has lost power
127 * @gt: the i915 GT container
128 * @force: ignore a failed reset and sanitize engine state anyway
130 * Anytime we reset the GPU, either with an explicit GPU reset or through a
131 * PCI power cycle, the GPU loses state and we must reset our state tracking
132 * to match. Note that calling intel_gt_sanitize() if the GPU has not
133 * been reset results in much confusion!
135 void intel_gt_sanitize(struct intel_gt *gt, bool force)
137 struct intel_engine_cs *engine;
138 enum intel_engine_id id;
139 intel_wakeref_t wakeref;
141 GEM_TRACE("force:%s\n", yesno(force));
143 /* Use a raw wakeref to avoid calling intel_display_power_get early */
144 wakeref = intel_runtime_pm_get(gt->uncore->rpm);
145 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
148 * As we have just resumed the machine and woken the device up from
149 * deep PCI sleep (presumably D3_cold), assume the HW has been reset
150 * back to defaults, recovering from whatever wedged state we left it
151 * in and so worth trying to use the device once more.
153 if (intel_gt_is_wedged(gt))
154 intel_gt_unset_wedged(gt);
156 intel_uc_sanitize(>->uc);
158 for_each_engine(engine, gt, id)
159 if (engine->reset.prepare)
160 engine->reset.prepare(engine);
162 intel_uc_reset_prepare(>->uc);
164 if (reset_engines(gt) || force) {
165 for_each_engine(engine, gt, id)
166 __intel_engine_reset(engine, false);
169 for_each_engine(engine, gt, id)
170 if (engine->reset.finish)
171 engine->reset.finish(engine);
173 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
174 intel_runtime_pm_put(gt->uncore->rpm, wakeref);
177 void intel_gt_pm_fini(struct intel_gt *gt)
179 intel_rc6_fini(>->rc6);
182 int intel_gt_resume(struct intel_gt *gt)
184 struct intel_engine_cs *engine;
185 enum intel_engine_id id;
191 * After resume, we may need to poke into the pinned kernel
192 * contexts to paper over any damage caused by the sudden suspend.
193 * Only the kernel contexts should remain pinned over suspend,
194 * allowing us to fixup the user contexts on their first pin.
198 intel_uncore_forcewake_get(gt->uncore, FORCEWAKE_ALL);
199 intel_rc6_sanitize(>->rc6);
201 intel_rps_enable(>->rps);
202 intel_llc_enable(>->llc);
204 for_each_engine(engine, gt, id) {
205 struct intel_context *ce;
207 intel_engine_pm_get(engine);
209 ce = engine->kernel_context;
211 GEM_BUG_ON(!intel_context_is_pinned(ce));
215 engine->serial++; /* kernel context lost */
216 err = engine->resume(engine);
218 intel_engine_pm_put(engine);
220 dev_err(gt->i915->drm.dev,
221 "Failed to restart %s (%d)\n",
227 intel_rc6_enable(>->rc6);
229 intel_uc_resume(>->uc);
231 user_forcewake(gt, false);
233 intel_uncore_forcewake_put(gt->uncore, FORCEWAKE_ALL);
239 static void wait_for_idle(struct intel_gt *gt)
241 if (intel_gt_wait_for_idle(gt, I915_GEM_IDLE_TIMEOUT) == -ETIME) {
243 * Forcibly cancel outstanding work and leave
246 intel_gt_set_wedged(gt);
249 intel_gt_pm_wait_for_idle(gt);
252 void intel_gt_suspend(struct intel_gt *gt)
254 intel_wakeref_t wakeref;
256 user_forcewake(gt, true);
258 /* We expect to be idle already; but also want to be independent */
261 intel_uc_suspend(>->uc);
263 with_intel_runtime_pm(gt->uncore->rpm, wakeref) {
264 intel_rps_disable(>->rps);
265 intel_rc6_disable(>->rc6);
266 intel_llc_disable(>->llc);
269 intel_gt_sanitize(gt, false);
274 void intel_gt_runtime_suspend(struct intel_gt *gt)
276 intel_uc_runtime_suspend(>->uc);
281 int intel_gt_runtime_resume(struct intel_gt *gt)
285 intel_gt_init_swizzling(gt);
287 return intel_uc_runtime_resume(>->uc);
290 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
291 #include "selftest_gt_pm.c"