1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
8 #include "intel_breadcrumbs.h"
9 #include "intel_context.h"
10 #include "intel_engine.h"
11 #include "intel_engine_heartbeat.h"
12 #include "intel_engine_pm.h"
14 #include "intel_gt_pm.h"
15 #include "intel_rc6.h"
16 #include "intel_ring.h"
17 #include "shmem_utils.h"
18 #include "intel_gt_regs.h"
20 static void intel_gsc_idle_msg_enable(struct intel_engine_cs *engine)
22 struct drm_i915_private *i915 = engine->i915;
24 if (MEDIA_VER(i915) >= 13 && engine->id == GSC0) {
25 intel_uncore_write(engine->gt->uncore,
27 _MASKED_BIT_DISABLE(IDLE_MSG_DISABLE));
28 /* hysteresis 0xA=5us as recommended in spec*/
29 intel_uncore_write(engine->gt->uncore,
35 static void dbg_poison_ce(struct intel_context *ce)
37 if (!IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
41 struct drm_i915_gem_object *obj = ce->state->obj;
42 int type = intel_gt_coherent_map_type(ce->engine->gt, obj, true);
45 if (!i915_gem_object_trylock(obj, NULL))
48 map = i915_gem_object_pin_map(obj, type);
50 memset(map, CONTEXT_REDZONE, obj->base.size);
51 i915_gem_object_flush_map(obj);
52 i915_gem_object_unpin_map(obj);
54 i915_gem_object_unlock(obj);
58 static int __engine_unpark(struct intel_wakeref *wf)
60 struct intel_engine_cs *engine =
61 container_of(wf, typeof(*engine), wakeref);
62 struct intel_context *ce;
64 ENGINE_TRACE(engine, "\n");
66 intel_gt_pm_get(engine->gt);
68 /* Discard stale context state from across idling */
69 ce = engine->kernel_context;
71 GEM_BUG_ON(test_bit(CONTEXT_VALID_BIT, &ce->flags));
73 /* Flush all pending HW writes before we touch the context */
74 while (unlikely(intel_context_inflight(ce)))
75 intel_engine_flush_submission(engine);
77 /* First poison the image to verify we never fully trust it */
80 /* Scrub the context image after our loss of control */
83 CE_TRACE(ce, "reset { seqno:%x, *hwsp:%x, ring:%x }\n",
85 READ_ONCE(*ce->timeline->hwsp_seqno),
87 GEM_BUG_ON(ce->timeline->seqno !=
88 READ_ONCE(*ce->timeline->hwsp_seqno));
92 engine->unpark(engine);
94 intel_breadcrumbs_unpark(engine->breadcrumbs);
95 intel_engine_unpark_heartbeat(engine);
99 static void duration(struct dma_fence *fence, struct dma_fence_cb *cb)
101 struct i915_request *rq = to_request(fence);
103 ewma__engine_latency_add(&rq->engine->latency,
104 ktime_us_delta(rq->fence.timestamp,
105 rq->duration.emitted));
109 __queue_and_release_pm(struct i915_request *rq,
110 struct intel_timeline *tl,
111 struct intel_engine_cs *engine)
113 struct intel_gt_timelines *timelines = &engine->gt->timelines;
115 ENGINE_TRACE(engine, "parking\n");
118 * Open coded one half of intel_context_enter, which we have to omit
119 * here (see the large comment below) and because the other part must
120 * not be called due constructing directly with __i915_request_create
121 * which increments active count via intel_context_mark_active.
123 GEM_BUG_ON(rq->context->active_count != 1);
124 __intel_gt_pm_get(engine->gt);
127 * We have to serialise all potential retirement paths with our
128 * submission, as we don't want to underflow either the
129 * engine->wakeref.counter or our timeline->active_count.
131 * Equally, we cannot allow a new submission to start until
132 * after we finish queueing, nor could we allow that submitter
133 * to retire us before we are ready!
135 spin_lock(&timelines->lock);
137 /* Let intel_gt_retire_requests() retire us (acquired under lock) */
138 if (!atomic_fetch_inc(&tl->active_count))
139 list_add_tail(&tl->link, &timelines->active_list);
141 /* Hand the request over to HW and so engine_retire() */
142 __i915_request_queue_bh(rq);
144 /* Let new submissions commence (and maybe retire this timeline) */
145 __intel_wakeref_defer_park(&engine->wakeref);
147 spin_unlock(&timelines->lock);
150 static bool switch_to_kernel_context(struct intel_engine_cs *engine)
152 struct intel_context *ce = engine->kernel_context;
153 struct i915_request *rq;
157 * This is execlist specific behaviour intended to ensure the GPU is
158 * idle by switching to a known 'safe' context. With GuC submission, the
159 * same idle guarantee is achieved by other means (disabling
160 * scheduling). Further, switching to a 'safe' context has no effect
161 * with GuC submission as the scheduler can just switch back again.
163 * FIXME: Move this backend scheduler specific behaviour into the
166 if (intel_engine_uses_guc(engine))
169 /* GPU is pointing to the void, as good as in the kernel context. */
170 if (intel_gt_is_wedged(engine->gt))
173 GEM_BUG_ON(!intel_context_is_barrier(ce));
174 GEM_BUG_ON(ce->timeline->hwsp_ggtt != engine->status_page.vma);
176 /* Already inside the kernel context, safe to power down. */
177 if (engine->wakeref_serial == engine->serial)
181 * Note, we do this without taking the timeline->mutex. We cannot
182 * as we may be called while retiring the kernel context and so
183 * already underneath the timeline->mutex. Instead we rely on the
184 * exclusive property of the __engine_park that prevents anyone
185 * else from creating a request on this engine. This also requires
186 * that the ring is empty and we avoid any waits while constructing
187 * the context, as they assume protection by the timeline->mutex.
188 * This should hold true as we can only park the engine after
189 * retiring the last request, thus all rings should be empty and
190 * all timelines idle.
192 * For unlocking, there are 2 other parties and the GPU who have a
195 * A new gpu user will be waiting on the engine-pm to start their
196 * engine_unpark. New waiters are predicated on engine->wakeref.count
197 * and so intel_wakeref_defer_park() acts like a mutex_unlock of the
200 * The other party is intel_gt_retire_requests(), which is walking the
201 * list of active timelines looking for completions. Meanwhile as soon
202 * as we call __i915_request_queue(), the GPU may complete our request.
203 * Ergo, if we put ourselves on the timelines.active_list
204 * (se intel_timeline_enter()) before we increment the
205 * engine->wakeref.count, we may see the request completion and retire
206 * it causing an underflow of the engine->wakeref.
208 set_bit(CONTEXT_IS_PARKING, &ce->flags);
209 GEM_BUG_ON(atomic_read(&ce->timeline->active_count) < 0);
211 rq = __i915_request_create(ce, GFP_NOWAIT);
213 /* Context switch failed, hope for the best! Maybe reset? */
216 /* Check again on the next retirement. */
217 engine->wakeref_serial = engine->serial + 1;
218 i915_request_add_active_barriers(rq);
220 /* Install ourselves as a preemption barrier */
221 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
222 if (likely(!__i915_request_commit(rq))) { /* engine should be idle! */
224 * Use an interrupt for precise measurement of duration,
225 * otherwise we rely on someone else retiring all the requests
226 * which may delay the signaling (i.e. we will likely wait
227 * until the background request retirement running every
230 BUILD_BUG_ON(sizeof(rq->duration) > sizeof(rq->submitq));
231 dma_fence_add_callback(&rq->fence, &rq->duration.cb, duration);
232 rq->duration.emitted = ktime_get();
235 /* Expose ourselves to the world */
236 __queue_and_release_pm(rq, ce->timeline, engine);
240 clear_bit(CONTEXT_IS_PARKING, &ce->flags);
244 static void call_idle_barriers(struct intel_engine_cs *engine)
246 struct llist_node *node, *next;
248 llist_for_each_safe(node, next, llist_del_all(&engine->barrier_tasks)) {
249 struct dma_fence_cb *cb =
250 container_of((struct list_head *)node,
253 cb->func(ERR_PTR(-EAGAIN), cb);
257 static int __engine_park(struct intel_wakeref *wf)
259 struct intel_engine_cs *engine =
260 container_of(wf, typeof(*engine), wakeref);
262 engine->saturated = 0;
265 * If one and only one request is completed between pm events,
266 * we know that we are inside the kernel context and it is
267 * safe to power down. (We are paranoid in case that runtime
268 * suspend causes corruption to the active context image, and
269 * want to avoid that impacting userspace.)
271 if (!switch_to_kernel_context(engine))
274 ENGINE_TRACE(engine, "parked\n");
276 call_idle_barriers(engine); /* cleanup after wedging */
278 intel_engine_park_heartbeat(engine);
279 intel_breadcrumbs_park(engine->breadcrumbs);
281 /* Must be reset upon idling, or we may miss the busy wakeup. */
282 GEM_BUG_ON(engine->sched_engine->queue_priority_hint != INT_MIN);
285 engine->park(engine);
287 /* While gt calls i915_vma_parked(), we have to break the lock cycle */
288 intel_gt_pm_put_async(engine->gt);
292 static const struct intel_wakeref_ops wf_ops = {
293 .get = __engine_unpark,
294 .put = __engine_park,
297 void intel_engine_init__pm(struct intel_engine_cs *engine)
299 intel_wakeref_init(&engine->wakeref, engine->i915, &wf_ops);
300 intel_engine_init_heartbeat(engine);
302 intel_gsc_idle_msg_enable(engine);
306 * intel_engine_reset_pinned_contexts - Reset the pinned contexts of
308 * @engine: The engine whose pinned contexts we want to reset.
310 * Typically the pinned context LMEM images lose or get their content
311 * corrupted on suspend. This function resets their images.
313 void intel_engine_reset_pinned_contexts(struct intel_engine_cs *engine)
315 struct intel_context *ce;
317 list_for_each_entry(ce, &engine->pinned_contexts_list,
318 pinned_contexts_link) {
319 /* kernel context gets reset at __engine_unpark() */
320 if (ce == engine->kernel_context)
328 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
329 #include "selftest_engine_pm.c"