2 * SPDX-License-Identifier: MIT
4 * Copyright © 2019 Intel Corporation
8 #include "i915_request.h"
10 #include "intel_context.h"
11 #include "intel_engine_heartbeat.h"
12 #include "intel_engine_pm.h"
13 #include "intel_engine.h"
15 #include "intel_reset.h"
18 * While the engine is active, we send a periodic pulse along the engine
19 * to check on its health and to flush any idle-barriers. If that request
20 * is stuck, and we fail to preempt it, we declare the engine hung and
21 * issue a reset -- in the hope that restores progress.
24 static bool next_heartbeat(struct intel_engine_cs *engine)
28 delay = READ_ONCE(engine->props.heartbeat_interval_ms);
32 delay = msecs_to_jiffies_timeout(delay);
34 delay = round_jiffies_up_relative(delay);
35 mod_delayed_work(system_highpri_wq, &engine->heartbeat.work, delay);
40 static void idle_pulse(struct intel_engine_cs *engine, struct i915_request *rq)
42 engine->wakeref_serial = READ_ONCE(engine->serial) + 1;
43 i915_request_add_active_barriers(rq);
46 static void show_heartbeat(const struct i915_request *rq,
47 struct intel_engine_cs *engine)
49 struct drm_printer p = drm_debug_printer("heartbeat");
51 intel_engine_dump(engine, &p,
52 "%s heartbeat {seqno:%llx:%lld, prio:%d} not ticking\n",
56 rq->sched.attr.priority);
59 static void heartbeat(struct work_struct *wrk)
61 struct i915_sched_attr attr = {
62 .priority = I915_USER_PRIORITY(I915_PRIORITY_MIN),
64 struct intel_engine_cs *engine =
65 container_of(wrk, typeof(*engine), heartbeat.work.work);
66 struct intel_context *ce = engine->kernel_context;
67 struct i915_request *rq;
70 /* Just in case everything has gone horribly wrong, give it a kick */
71 intel_engine_flush_submission(engine);
73 rq = engine->heartbeat.systole;
74 if (rq && i915_request_completed(rq)) {
76 engine->heartbeat.systole = NULL;
79 if (!intel_engine_pm_get_if_awake(engine))
82 if (intel_gt_is_wedged(engine->gt))
85 if (engine->heartbeat.systole) {
86 if (!i915_sw_fence_signaled(&rq->submit)) {
88 * Not yet submitted, system is stalled.
90 * This more often happens for ring submission,
91 * where all contexts are funnelled into a common
92 * ringbuffer. If one context is blocked on an
93 * external fence, not only is it not submitted,
94 * but all other contexts, including the kernel
95 * context are stuck waiting for the signal.
97 } else if (engine->schedule &&
98 rq->sched.attr.priority < I915_PRIORITY_BARRIER) {
100 * Gradually raise the priority of the heartbeat to
101 * give high priority work [which presumably desires
102 * low latency and no jitter] the chance to naturally
103 * complete before being preempted.
105 attr.priority = I915_PRIORITY_MASK;
106 if (rq->sched.attr.priority >= attr.priority)
107 attr.priority |= I915_USER_PRIORITY(I915_PRIORITY_HEARTBEAT);
108 if (rq->sched.attr.priority >= attr.priority)
109 attr.priority = I915_PRIORITY_BARRIER;
112 engine->schedule(rq, &attr);
115 if (IS_ENABLED(CONFIG_DRM_I915_DEBUG_GEM))
116 show_heartbeat(rq, engine);
118 intel_gt_handle_error(engine->gt, engine->mask,
120 "stopped heartbeat on %s",
126 serial = READ_ONCE(engine->serial);
127 if (engine->wakeref_serial == serial)
130 if (!mutex_trylock(&ce->timeline->mutex)) {
131 /* Unable to lock the kernel timeline, is the engine stuck? */
132 if (xchg(&engine->heartbeat.blocked, serial) == serial)
133 intel_gt_handle_error(engine->gt, engine->mask,
135 "no heartbeat on %s",
140 intel_context_enter(ce);
141 rq = __i915_request_create(ce, GFP_NOWAIT | __GFP_NOWARN);
142 intel_context_exit(ce);
146 idle_pulse(engine, rq);
147 if (engine->i915->params.enable_hangcheck)
148 engine->heartbeat.systole = i915_request_get(rq);
150 __i915_request_commit(rq);
151 __i915_request_queue(rq, &attr);
154 mutex_unlock(&ce->timeline->mutex);
156 if (!next_heartbeat(engine))
157 i915_request_put(fetch_and_zero(&engine->heartbeat.systole));
158 intel_engine_pm_put(engine);
161 void intel_engine_unpark_heartbeat(struct intel_engine_cs *engine)
163 if (!IS_ACTIVE(CONFIG_DRM_I915_HEARTBEAT_INTERVAL))
166 next_heartbeat(engine);
169 void intel_engine_park_heartbeat(struct intel_engine_cs *engine)
171 if (cancel_delayed_work(&engine->heartbeat.work))
172 i915_request_put(fetch_and_zero(&engine->heartbeat.systole));
175 void intel_engine_init_heartbeat(struct intel_engine_cs *engine)
177 INIT_DELAYED_WORK(&engine->heartbeat.work, heartbeat);
180 int intel_engine_set_heartbeat(struct intel_engine_cs *engine,
185 /* Send one last pulse before to cleanup persistent hogs */
186 if (!delay && IS_ACTIVE(CONFIG_DRM_I915_PREEMPT_TIMEOUT)) {
187 err = intel_engine_pulse(engine);
192 WRITE_ONCE(engine->props.heartbeat_interval_ms, delay);
194 if (intel_engine_pm_get_if_awake(engine)) {
196 intel_engine_unpark_heartbeat(engine);
198 intel_engine_park_heartbeat(engine);
199 intel_engine_pm_put(engine);
205 int intel_engine_pulse(struct intel_engine_cs *engine)
207 struct i915_sched_attr attr = { .priority = I915_PRIORITY_BARRIER };
208 struct intel_context *ce = engine->kernel_context;
209 struct i915_request *rq;
212 if (!intel_engine_has_preemption(engine))
215 if (!intel_engine_pm_get_if_awake(engine))
218 if (mutex_lock_interruptible(&ce->timeline->mutex)) {
223 intel_context_enter(ce);
224 rq = __i915_request_create(ce, GFP_NOWAIT | __GFP_NOWARN);
225 intel_context_exit(ce);
231 __set_bit(I915_FENCE_FLAG_SENTINEL, &rq->fence.flags);
232 idle_pulse(engine, rq);
234 __i915_request_commit(rq);
235 __i915_request_queue(rq, &attr);
236 GEM_BUG_ON(rq->sched.attr.priority < I915_PRIORITY_BARRIER);
240 mutex_unlock(&ce->timeline->mutex);
242 intel_engine_pm_put(engine);
246 int intel_engine_flush_barriers(struct intel_engine_cs *engine)
248 struct i915_request *rq;
251 if (llist_empty(&engine->barrier_tasks))
254 if (!intel_engine_pm_get_if_awake(engine))
257 rq = i915_request_create(engine->kernel_context);
263 idle_pulse(engine, rq);
264 i915_request_add(rq);
267 intel_engine_pm_put(engine);
271 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
272 #include "selftest_engine_heartbeat.c"