2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
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"
19 static int __engine_unpark(struct intel_wakeref *wf)
21 struct intel_engine_cs *engine =
22 container_of(wf, typeof(*engine), wakeref);
23 struct intel_context *ce;
25 ENGINE_TRACE(engine, "\n");
27 intel_gt_pm_get(engine->gt);
29 /* Discard stale context state from across idling */
30 ce = engine->kernel_context;
32 GEM_BUG_ON(test_bit(CONTEXT_VALID_BIT, &ce->flags));
34 /* First poison the image to verify we never fully trust it */
35 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM) && ce->state) {
36 struct drm_i915_gem_object *obj = ce->state->obj;
37 int type = i915_coherent_map_type(engine->i915);
40 map = i915_gem_object_pin_map(obj, type);
42 memset(map, CONTEXT_REDZONE, obj->base.size);
43 i915_gem_object_flush_map(obj);
44 i915_gem_object_unpin_map(obj);
52 engine->unpark(engine);
54 intel_engine_unpark_heartbeat(engine);
58 #if IS_ENABLED(CONFIG_LOCKDEP)
60 static inline unsigned long __timeline_mark_lock(struct intel_context *ce)
64 local_irq_save(flags);
65 mutex_acquire(&ce->timeline->mutex.dep_map, 2, 0, _THIS_IP_);
70 static inline void __timeline_mark_unlock(struct intel_context *ce,
73 mutex_release(&ce->timeline->mutex.dep_map, _THIS_IP_);
74 local_irq_restore(flags);
79 static inline unsigned long __timeline_mark_lock(struct intel_context *ce)
84 static inline void __timeline_mark_unlock(struct intel_context *ce,
89 #endif /* !IS_ENABLED(CONFIG_LOCKDEP) */
91 static void duration(struct dma_fence *fence, struct dma_fence_cb *cb)
93 struct i915_request *rq = to_request(fence);
95 ewma__engine_latency_add(&rq->engine->latency,
96 ktime_us_delta(rq->fence.timestamp,
97 rq->duration.emitted));
101 __queue_and_release_pm(struct i915_request *rq,
102 struct intel_timeline *tl,
103 struct intel_engine_cs *engine)
105 struct intel_gt_timelines *timelines = &engine->gt->timelines;
107 ENGINE_TRACE(engine, "parking\n");
110 * We have to serialise all potential retirement paths with our
111 * submission, as we don't want to underflow either the
112 * engine->wakeref.counter or our timeline->active_count.
114 * Equally, we cannot allow a new submission to start until
115 * after we finish queueing, nor could we allow that submitter
116 * to retire us before we are ready!
118 spin_lock(&timelines->lock);
120 /* Let intel_gt_retire_requests() retire us (acquired under lock) */
121 if (!atomic_fetch_inc(&tl->active_count))
122 list_add_tail(&tl->link, &timelines->active_list);
124 /* Hand the request over to HW and so engine_retire() */
125 __i915_request_queue(rq, NULL);
127 /* Let new submissions commence (and maybe retire this timeline) */
128 __intel_wakeref_defer_park(&engine->wakeref);
130 spin_unlock(&timelines->lock);
133 static bool switch_to_kernel_context(struct intel_engine_cs *engine)
135 struct intel_context *ce = engine->kernel_context;
136 struct i915_request *rq;
140 /* GPU is pointing to the void, as good as in the kernel context. */
141 if (intel_gt_is_wedged(engine->gt))
144 GEM_BUG_ON(!intel_context_is_barrier(ce));
145 GEM_BUG_ON(ce->timeline->hwsp_ggtt != engine->status_page.vma);
147 /* Already inside the kernel context, safe to power down. */
148 if (engine->wakeref_serial == engine->serial)
152 * Note, we do this without taking the timeline->mutex. We cannot
153 * as we may be called while retiring the kernel context and so
154 * already underneath the timeline->mutex. Instead we rely on the
155 * exclusive property of the __engine_park that prevents anyone
156 * else from creating a request on this engine. This also requires
157 * that the ring is empty and we avoid any waits while constructing
158 * the context, as they assume protection by the timeline->mutex.
159 * This should hold true as we can only park the engine after
160 * retiring the last request, thus all rings should be empty and
161 * all timelines idle.
163 * For unlocking, there are 2 other parties and the GPU who have a
166 * A new gpu user will be waiting on the engine-pm to start their
167 * engine_unpark. New waiters are predicated on engine->wakeref.count
168 * and so intel_wakeref_defer_park() acts like a mutex_unlock of the
171 * The other party is intel_gt_retire_requests(), which is walking the
172 * list of active timelines looking for completions. Meanwhile as soon
173 * as we call __i915_request_queue(), the GPU may complete our request.
174 * Ergo, if we put ourselves on the timelines.active_list
175 * (se intel_timeline_enter()) before we increment the
176 * engine->wakeref.count, we may see the request completion and retire
177 * it causing an underflow of the engine->wakeref.
179 flags = __timeline_mark_lock(ce);
180 GEM_BUG_ON(atomic_read(&ce->timeline->active_count) < 0);
182 rq = __i915_request_create(ce, GFP_NOWAIT);
184 /* Context switch failed, hope for the best! Maybe reset? */
187 /* Check again on the next retirement. */
188 engine->wakeref_serial = engine->serial + 1;
189 i915_request_add_active_barriers(rq);
191 /* Install ourselves as a preemption barrier */
192 rq->sched.attr.priority = I915_PRIORITY_BARRIER;
193 if (likely(!__i915_request_commit(rq))) { /* engine should be idle! */
195 * Use an interrupt for precise measurement of duration,
196 * otherwise we rely on someone else retiring all the requests
197 * which may delay the signaling (i.e. we will likely wait
198 * until the background request retirement running every
201 BUILD_BUG_ON(sizeof(rq->duration) > sizeof(rq->submitq));
202 dma_fence_add_callback(&rq->fence, &rq->duration.cb, duration);
203 rq->duration.emitted = ktime_get();
206 /* Expose ourselves to the world */
207 __queue_and_release_pm(rq, ce->timeline, engine);
211 __timeline_mark_unlock(ce, flags);
215 static void call_idle_barriers(struct intel_engine_cs *engine)
217 struct llist_node *node, *next;
219 llist_for_each_safe(node, next, llist_del_all(&engine->barrier_tasks)) {
220 struct dma_fence_cb *cb =
221 container_of((struct list_head *)node,
224 cb->func(ERR_PTR(-EAGAIN), cb);
228 static int __engine_park(struct intel_wakeref *wf)
230 struct intel_engine_cs *engine =
231 container_of(wf, typeof(*engine), wakeref);
233 engine->saturated = 0;
236 * If one and only one request is completed between pm events,
237 * we know that we are inside the kernel context and it is
238 * safe to power down. (We are paranoid in case that runtime
239 * suspend causes corruption to the active context image, and
240 * want to avoid that impacting userspace.)
242 if (!switch_to_kernel_context(engine))
245 ENGINE_TRACE(engine, "parked\n");
247 call_idle_barriers(engine); /* cleanup after wedging */
249 intel_engine_park_heartbeat(engine);
250 intel_engine_disarm_breadcrumbs(engine);
252 /* Must be reset upon idling, or we may miss the busy wakeup. */
253 GEM_BUG_ON(engine->execlists.queue_priority_hint != INT_MIN);
256 engine->park(engine);
258 engine->execlists.no_priolist = false;
260 /* While gt calls i915_vma_parked(), we have to break the lock cycle */
261 intel_gt_pm_put_async(engine->gt);
265 static const struct intel_wakeref_ops wf_ops = {
266 .get = __engine_unpark,
267 .put = __engine_park,
270 void intel_engine_init__pm(struct intel_engine_cs *engine)
272 struct intel_runtime_pm *rpm = engine->uncore->rpm;
274 intel_wakeref_init(&engine->wakeref, rpm, &wf_ops);
275 intel_engine_init_heartbeat(engine);
278 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
279 #include "selftest_engine_pm.c"