2 * Copyright © 2016 Intel Corporation
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
11 * The above copyright notice and this permission notice (including the next
12 * paragraph) shall be included in all copies or substantial portions of the
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
26 #include "i915_reset.h"
31 enum intel_engine_hangcheck_action action;
32 unsigned long action_timestamp;
34 struct intel_instdone instdone;
39 static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
41 u32 tmp = current_instdone | *old_instdone;
44 unchanged = tmp == *old_instdone;
50 static bool subunits_stuck(struct intel_engine_cs *engine)
52 struct drm_i915_private *dev_priv = engine->i915;
53 struct intel_instdone instdone;
54 struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
59 if (engine->id != RCS)
62 intel_engine_get_instdone(engine, &instdone);
64 /* There might be unstable subunit states even when
65 * actual head is not moving. Filter out the unstable ones by
66 * accumulating the undone -> done transitions and only
67 * consider those as progress.
69 stuck = instdone_unchanged(instdone.instdone,
70 &accu_instdone->instdone);
71 stuck &= instdone_unchanged(instdone.slice_common,
72 &accu_instdone->slice_common);
74 for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
75 stuck &= instdone_unchanged(instdone.sampler[slice][subslice],
76 &accu_instdone->sampler[slice][subslice]);
77 stuck &= instdone_unchanged(instdone.row[slice][subslice],
78 &accu_instdone->row[slice][subslice]);
84 static enum intel_engine_hangcheck_action
85 head_stuck(struct intel_engine_cs *engine, u64 acthd)
87 if (acthd != engine->hangcheck.acthd) {
89 /* Clear subunit states on head movement */
90 memset(&engine->hangcheck.instdone, 0,
91 sizeof(engine->hangcheck.instdone));
93 return ENGINE_ACTIVE_HEAD;
96 if (!subunits_stuck(engine))
97 return ENGINE_ACTIVE_SUBUNITS;
102 static enum intel_engine_hangcheck_action
103 engine_stuck(struct intel_engine_cs *engine, u64 acthd)
105 struct drm_i915_private *dev_priv = engine->i915;
106 enum intel_engine_hangcheck_action ha;
109 ha = head_stuck(engine, acthd);
110 if (ha != ENGINE_DEAD)
113 if (IS_GEN(dev_priv, 2))
116 /* Is the chip hanging on a WAIT_FOR_EVENT?
117 * If so we can simply poke the RB_WAIT bit
118 * and break the hang. This should work on
119 * all but the second generation chipsets.
121 tmp = I915_READ_CTL(engine);
122 if (tmp & RING_WAIT) {
123 i915_handle_error(dev_priv, BIT(engine->id), 0,
124 "stuck wait on %s", engine->name);
125 I915_WRITE_CTL(engine, tmp);
126 return ENGINE_WAIT_KICK;
132 static void hangcheck_load_sample(struct intel_engine_cs *engine,
133 struct hangcheck *hc)
135 hc->acthd = intel_engine_get_active_head(engine);
136 hc->seqno = intel_engine_get_seqno(engine);
139 static void hangcheck_store_sample(struct intel_engine_cs *engine,
140 const struct hangcheck *hc)
142 engine->hangcheck.acthd = hc->acthd;
143 engine->hangcheck.seqno = hc->seqno;
146 static enum intel_engine_hangcheck_action
147 hangcheck_get_action(struct intel_engine_cs *engine,
148 const struct hangcheck *hc)
150 if (engine->hangcheck.seqno != hc->seqno)
151 return ENGINE_ACTIVE_SEQNO;
153 if (intel_engine_is_idle(engine))
156 return engine_stuck(engine, hc->acthd);
159 static void hangcheck_accumulate_sample(struct intel_engine_cs *engine,
160 struct hangcheck *hc)
162 unsigned long timeout = I915_ENGINE_DEAD_TIMEOUT;
164 hc->action = hangcheck_get_action(engine, hc);
166 /* We always increment the progress
167 * if the engine is busy and still processing
168 * the same request, so that no single request
169 * can run indefinitely (such as a chain of
170 * batches). The only time we do not increment
171 * the hangcheck score on this ring, if this
172 * engine is in a legitimate wait for another
173 * engine. In that case the waiting engine is a
174 * victim and we want to be sure we catch the
175 * right culprit. Then every time we do kick
176 * the ring, make it as a progress as the seqno
177 * advancement might ensure and if not, it
178 * will catch the hanging engine.
181 switch (hc->action) {
183 case ENGINE_ACTIVE_SEQNO:
184 /* Clear head and subunit states on seqno movement */
187 memset(&engine->hangcheck.instdone, 0,
188 sizeof(engine->hangcheck.instdone));
190 /* Intentional fall through */
191 case ENGINE_WAIT_KICK:
193 engine->hangcheck.action_timestamp = jiffies;
196 case ENGINE_ACTIVE_HEAD:
197 case ENGINE_ACTIVE_SUBUNITS:
199 * Seqno stuck with still active engine gets leeway,
200 * in hopes that it is just a long shader.
202 timeout = I915_SEQNO_DEAD_TIMEOUT;
209 MISSING_CASE(hc->action);
212 hc->stalled = time_after(jiffies,
213 engine->hangcheck.action_timestamp + timeout);
214 hc->wedged = time_after(jiffies,
215 engine->hangcheck.action_timestamp +
216 I915_ENGINE_WEDGED_TIMEOUT);
219 static void hangcheck_declare_hang(struct drm_i915_private *i915,
223 struct intel_engine_cs *engine;
228 /* If some rings hung but others were still busy, only
229 * blame the hanging rings in the synopsis.
233 len = scnprintf(msg, sizeof(msg),
234 "%s on ", stuck == hung ? "no progress" : "hang");
235 for_each_engine_masked(engine, i915, hung, tmp)
236 len += scnprintf(msg + len, sizeof(msg) - len,
237 "%s, ", engine->name);
240 return i915_handle_error(i915, hung, I915_ERROR_CAPTURE, "%s", msg);
244 * This is called when the chip hasn't reported back with completed
245 * batchbuffers in a long time. We keep track per ring seqno progress and
246 * if there are no progress, hangcheck score for that ring is increased.
247 * Further, acthd is inspected to see if the ring is stuck. On stuck case
248 * we kick the ring. If we see no progress on three subsequent calls
249 * we assume chip is wedged and try to fix it by resetting the chip.
251 static void i915_hangcheck_elapsed(struct work_struct *work)
253 struct drm_i915_private *dev_priv =
254 container_of(work, typeof(*dev_priv),
255 gpu_error.hangcheck_work.work);
256 struct intel_engine_cs *engine;
257 enum intel_engine_id id;
258 unsigned int hung = 0, stuck = 0, wedged = 0;
260 if (!i915_modparams.enable_hangcheck)
263 if (!READ_ONCE(dev_priv->gt.awake))
266 if (i915_terminally_wedged(&dev_priv->gpu_error))
269 /* As enabling the GPU requires fairly extensive mmio access,
270 * periodically arm the mmio checker to see if we are triggering
271 * any invalid access.
273 intel_uncore_arm_unclaimed_mmio_detection(dev_priv);
275 for_each_engine(engine, dev_priv, id) {
278 intel_engine_signal_breadcrumbs(engine);
280 hangcheck_load_sample(engine, &hc);
281 hangcheck_accumulate_sample(engine, &hc);
282 hangcheck_store_sample(engine, &hc);
285 hung |= intel_engine_flag(engine);
286 if (hc.action != ENGINE_DEAD)
287 stuck |= intel_engine_flag(engine);
291 wedged |= intel_engine_flag(engine);
294 if (GEM_SHOW_DEBUG() && (hung | stuck)) {
295 struct drm_printer p = drm_debug_printer("hangcheck");
297 for_each_engine(engine, dev_priv, id) {
298 if (intel_engine_is_idle(engine))
301 intel_engine_dump(engine, &p, "%s\n", engine->name);
306 dev_err(dev_priv->drm.dev,
307 "GPU recovery timed out,"
308 " cancelling all in-flight rendering.\n");
310 i915_gem_set_wedged(dev_priv);
314 hangcheck_declare_hang(dev_priv, hung, stuck);
316 /* Reset timer in case GPU hangs without another request being added */
317 i915_queue_hangcheck(dev_priv);
320 void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
322 memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
323 engine->hangcheck.action_timestamp = jiffies;
326 void intel_hangcheck_init(struct drm_i915_private *i915)
328 INIT_DELAYED_WORK(&i915->gpu_error.hangcheck_work,
329 i915_hangcheck_elapsed);
332 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
333 #include "selftests/intel_hangcheck.c"