1 // SPDX-License-Identifier: MIT
3 * Copyright © 2019 Intel Corporation
7 #include "i915_request.h"
9 #include "intel_context.h"
10 #include "intel_engine_heartbeat.h"
11 #include "intel_engine_pm.h"
12 #include "intel_engine.h"
14 #include "intel_reset.h"
17 * While the engine is active, we send a periodic pulse along the engine
18 * to check on its health and to flush any idle-barriers. If that request
19 * is stuck, and we fail to preempt it, we declare the engine hung and
20 * issue a reset -- in the hope that restores progress.
23 static bool next_heartbeat(struct intel_engine_cs *engine)
27 delay = READ_ONCE(engine->props.heartbeat_interval_ms);
31 delay = msecs_to_jiffies_timeout(delay);
33 delay = round_jiffies_up_relative(delay);
34 mod_delayed_work(system_highpri_wq, &engine->heartbeat.work, delay + 1);
39 static struct i915_request *
40 heartbeat_create(struct intel_context *ce, gfp_t gfp)
42 struct i915_request *rq;
44 intel_context_enter(ce);
45 rq = __i915_request_create(ce, gfp);
46 intel_context_exit(ce);
51 static void idle_pulse(struct intel_engine_cs *engine, struct i915_request *rq)
53 engine->wakeref_serial = READ_ONCE(engine->serial) + 1;
54 i915_request_add_active_barriers(rq);
55 if (!engine->heartbeat.systole && intel_engine_has_heartbeat(engine))
56 engine->heartbeat.systole = i915_request_get(rq);
59 static void heartbeat_commit(struct i915_request *rq,
60 const struct i915_sched_attr *attr)
62 idle_pulse(rq->engine, rq);
64 __i915_request_commit(rq);
65 __i915_request_queue(rq, attr);
68 static void show_heartbeat(const struct i915_request *rq,
69 struct intel_engine_cs *engine)
71 struct drm_printer p = drm_debug_printer("heartbeat");
74 intel_engine_dump(engine, &p,
75 "%s heartbeat not ticking\n",
78 intel_engine_dump(engine, &p,
79 "%s heartbeat {seqno:%llx:%lld, prio:%d} not ticking\n",
83 rq->sched.attr.priority);
88 reset_engine(struct intel_engine_cs *engine, struct i915_request *rq)
90 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
91 show_heartbeat(rq, engine);
93 if (intel_engine_uses_guc(engine))
95 * GuC itself is toast or GuC's hang detection
96 * is disabled. Either way, need to find the
97 * hang culprit manually.
99 intel_guc_find_hung_context(engine);
101 intel_gt_handle_error(engine->gt, engine->mask,
103 "stopped heartbeat on %s",
107 static void heartbeat(struct work_struct *wrk)
109 struct i915_sched_attr attr = { .priority = I915_PRIORITY_MIN };
110 struct intel_engine_cs *engine =
111 container_of(wrk, typeof(*engine), heartbeat.work.work);
112 struct intel_context *ce = engine->kernel_context;
113 struct i915_request *rq;
114 unsigned long serial;
116 /* Just in case everything has gone horribly wrong, give it a kick */
117 intel_engine_flush_submission(engine);
119 rq = engine->heartbeat.systole;
120 if (rq && i915_request_completed(rq)) {
121 i915_request_put(rq);
122 engine->heartbeat.systole = NULL;
125 if (!intel_engine_pm_get_if_awake(engine))
128 if (intel_gt_is_wedged(engine->gt))
131 if (i915_sched_engine_disabled(engine->sched_engine)) {
132 reset_engine(engine, engine->heartbeat.systole);
136 if (engine->heartbeat.systole) {
137 long delay = READ_ONCE(engine->props.heartbeat_interval_ms);
139 /* Safeguard against too-fast worker invocations */
140 if (!time_after(jiffies,
141 rq->emitted_jiffies + msecs_to_jiffies(delay)))
144 if (!i915_sw_fence_signaled(&rq->submit)) {
146 * Not yet submitted, system is stalled.
148 * This more often happens for ring submission,
149 * where all contexts are funnelled into a common
150 * ringbuffer. If one context is blocked on an
151 * external fence, not only is it not submitted,
152 * but all other contexts, including the kernel
153 * context are stuck waiting for the signal.
155 } else if (engine->sched_engine->schedule &&
156 rq->sched.attr.priority < I915_PRIORITY_BARRIER) {
158 * Gradually raise the priority of the heartbeat to
159 * give high priority work [which presumably desires
160 * low latency and no jitter] the chance to naturally
161 * complete before being preempted.
164 if (rq->sched.attr.priority >= attr.priority)
165 attr.priority = I915_PRIORITY_HEARTBEAT;
166 if (rq->sched.attr.priority >= attr.priority)
167 attr.priority = I915_PRIORITY_BARRIER;
170 engine->sched_engine->schedule(rq, &attr);
173 reset_engine(engine, rq);
176 rq->emitted_jiffies = jiffies;
180 serial = READ_ONCE(engine->serial);
181 if (engine->wakeref_serial == serial)
184 if (!mutex_trylock(&ce->timeline->mutex)) {
185 /* Unable to lock the kernel timeline, is the engine stuck? */
186 if (xchg(&engine->heartbeat.blocked, serial) == serial)
187 intel_gt_handle_error(engine->gt, engine->mask,
189 "no heartbeat on %s",
194 rq = heartbeat_create(ce, GFP_NOWAIT | __GFP_NOWARN);
198 heartbeat_commit(rq, &attr);
201 mutex_unlock(&ce->timeline->mutex);
203 if (!engine->i915->params.enable_hangcheck || !next_heartbeat(engine))
204 i915_request_put(fetch_and_zero(&engine->heartbeat.systole));
205 intel_engine_pm_put(engine);
208 void intel_engine_unpark_heartbeat(struct intel_engine_cs *engine)
210 if (!IS_ACTIVE(CONFIG_DRM_I915_HEARTBEAT_INTERVAL))
213 next_heartbeat(engine);
216 void intel_engine_park_heartbeat(struct intel_engine_cs *engine)
218 if (cancel_delayed_work(&engine->heartbeat.work))
219 i915_request_put(fetch_and_zero(&engine->heartbeat.systole));
222 void intel_gt_unpark_heartbeats(struct intel_gt *gt)
224 struct intel_engine_cs *engine;
225 enum intel_engine_id id;
227 for_each_engine(engine, gt, id)
228 if (intel_engine_pm_is_awake(engine))
229 intel_engine_unpark_heartbeat(engine);
232 void intel_gt_park_heartbeats(struct intel_gt *gt)
234 struct intel_engine_cs *engine;
235 enum intel_engine_id id;
237 for_each_engine(engine, gt, id)
238 intel_engine_park_heartbeat(engine);
241 void intel_engine_init_heartbeat(struct intel_engine_cs *engine)
243 INIT_DELAYED_WORK(&engine->heartbeat.work, heartbeat);
246 static int __intel_engine_pulse(struct intel_engine_cs *engine)
248 struct i915_sched_attr attr = { .priority = I915_PRIORITY_BARRIER };
249 struct intel_context *ce = engine->kernel_context;
250 struct i915_request *rq;
252 lockdep_assert_held(&ce->timeline->mutex);
253 GEM_BUG_ON(!intel_engine_has_preemption(engine));
254 GEM_BUG_ON(!intel_engine_pm_is_awake(engine));
256 rq = heartbeat_create(ce, GFP_NOWAIT | __GFP_NOWARN);
260 __set_bit(I915_FENCE_FLAG_SENTINEL, &rq->fence.flags);
262 heartbeat_commit(rq, &attr);
263 GEM_BUG_ON(rq->sched.attr.priority < I915_PRIORITY_BARRIER);
268 static unsigned long set_heartbeat(struct intel_engine_cs *engine,
273 old = xchg(&engine->props.heartbeat_interval_ms, delay);
275 intel_engine_unpark_heartbeat(engine);
277 intel_engine_park_heartbeat(engine);
282 int intel_engine_set_heartbeat(struct intel_engine_cs *engine,
285 struct intel_context *ce = engine->kernel_context;
288 if (!delay && !intel_engine_has_preempt_reset(engine))
291 intel_engine_pm_get(engine);
293 err = mutex_lock_interruptible(&ce->timeline->mutex);
297 if (delay != engine->props.heartbeat_interval_ms) {
298 unsigned long saved = set_heartbeat(engine, delay);
300 /* recheck current execution */
301 if (intel_engine_has_preemption(engine)) {
302 err = __intel_engine_pulse(engine);
304 set_heartbeat(engine, saved);
308 mutex_unlock(&ce->timeline->mutex);
311 intel_engine_pm_put(engine);
315 int intel_engine_pulse(struct intel_engine_cs *engine)
317 struct intel_context *ce = engine->kernel_context;
320 if (!intel_engine_has_preemption(engine))
323 if (!intel_engine_pm_get_if_awake(engine))
327 if (!mutex_lock_interruptible(&ce->timeline->mutex)) {
328 err = __intel_engine_pulse(engine);
329 mutex_unlock(&ce->timeline->mutex);
332 intel_engine_flush_submission(engine);
333 intel_engine_pm_put(engine);
337 int intel_engine_flush_barriers(struct intel_engine_cs *engine)
339 struct i915_sched_attr attr = { .priority = I915_PRIORITY_MIN };
340 struct intel_context *ce = engine->kernel_context;
341 struct i915_request *rq;
344 if (llist_empty(&engine->barrier_tasks))
347 if (!intel_engine_pm_get_if_awake(engine))
350 if (mutex_lock_interruptible(&ce->timeline->mutex)) {
355 rq = heartbeat_create(ce, GFP_KERNEL);
361 heartbeat_commit(rq, &attr);
365 mutex_unlock(&ce->timeline->mutex);
367 intel_engine_pm_put(engine);
371 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
372 #include "selftest_engine_heartbeat.c"