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 "intel_engine.h"
28 #include "intel_reset.h"
34 enum intel_engine_hangcheck_action action;
35 unsigned long action_timestamp;
37 struct intel_instdone instdone;
42 static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
44 u32 tmp = current_instdone | *old_instdone;
47 unchanged = tmp == *old_instdone;
53 static bool subunits_stuck(struct intel_engine_cs *engine)
55 struct drm_i915_private *dev_priv = engine->i915;
56 struct intel_instdone instdone;
57 struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
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 enum intel_engine_hangcheck_action ha;
108 ha = head_stuck(engine, acthd);
109 if (ha != ENGINE_DEAD)
112 if (IS_GEN(engine->i915, 2))
115 /* Is the chip hanging on a WAIT_FOR_EVENT?
116 * If so we can simply poke the RB_WAIT bit
117 * and break the hang. This should work on
118 * all but the second generation chipsets.
120 tmp = ENGINE_READ(engine, RING_CTL);
121 if (tmp & RING_WAIT) {
122 intel_gt_handle_error(engine->gt, engine->mask, 0,
123 "stuck wait on %s", engine->name);
124 ENGINE_WRITE(engine, RING_CTL, tmp);
125 return ENGINE_WAIT_KICK;
131 static void hangcheck_load_sample(struct intel_engine_cs *engine,
132 struct hangcheck *hc)
134 hc->acthd = intel_engine_get_active_head(engine);
135 hc->ring = ENGINE_READ(engine, RING_START);
136 hc->head = ENGINE_READ(engine, RING_HEAD);
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.last_ring = hc->ring;
144 engine->hangcheck.last_head = hc->head;
147 static enum intel_engine_hangcheck_action
148 hangcheck_get_action(struct intel_engine_cs *engine,
149 const struct hangcheck *hc)
151 if (intel_engine_is_idle(engine))
154 if (engine->hangcheck.last_ring != hc->ring)
155 return ENGINE_ACTIVE_SEQNO;
157 if (engine->hangcheck.last_head != hc->head)
158 return ENGINE_ACTIVE_SEQNO;
160 return engine_stuck(engine, hc->acthd);
163 static void hangcheck_accumulate_sample(struct intel_engine_cs *engine,
164 struct hangcheck *hc)
166 unsigned long timeout = I915_ENGINE_DEAD_TIMEOUT;
168 hc->action = hangcheck_get_action(engine, hc);
170 /* We always increment the progress
171 * if the engine is busy and still processing
172 * the same request, so that no single request
173 * can run indefinitely (such as a chain of
174 * batches). The only time we do not increment
175 * the hangcheck score on this ring, if this
176 * engine is in a legitimate wait for another
177 * engine. In that case the waiting engine is a
178 * victim and we want to be sure we catch the
179 * right culprit. Then every time we do kick
180 * the ring, make it as a progress as the seqno
181 * advancement might ensure and if not, it
182 * will catch the hanging engine.
185 switch (hc->action) {
187 case ENGINE_ACTIVE_SEQNO:
188 /* Clear head and subunit states on seqno movement */
191 memset(&engine->hangcheck.instdone, 0,
192 sizeof(engine->hangcheck.instdone));
194 /* Intentional fall through */
195 case ENGINE_WAIT_KICK:
197 engine->hangcheck.action_timestamp = jiffies;
200 case ENGINE_ACTIVE_HEAD:
201 case ENGINE_ACTIVE_SUBUNITS:
203 * Seqno stuck with still active engine gets leeway,
204 * in hopes that it is just a long shader.
206 timeout = I915_SEQNO_DEAD_TIMEOUT;
213 MISSING_CASE(hc->action);
216 hc->stalled = time_after(jiffies,
217 engine->hangcheck.action_timestamp + timeout);
218 hc->wedged = time_after(jiffies,
219 engine->hangcheck.action_timestamp +
220 I915_ENGINE_WEDGED_TIMEOUT);
223 static void hangcheck_declare_hang(struct intel_gt *gt,
224 intel_engine_mask_t hung,
225 intel_engine_mask_t stuck)
227 struct intel_engine_cs *engine;
228 intel_engine_mask_t tmp;
232 /* If some rings hung but others were still busy, only
233 * blame the hanging rings in the synopsis.
237 len = scnprintf(msg, sizeof(msg),
238 "%s on ", stuck == hung ? "no progress" : "hang");
239 for_each_engine_masked(engine, gt->i915, hung, tmp)
240 len += scnprintf(msg + len, sizeof(msg) - len,
241 "%s, ", engine->name);
244 return intel_gt_handle_error(gt, hung, I915_ERROR_CAPTURE, "%s", msg);
248 * This is called when the chip hasn't reported back with completed
249 * batchbuffers in a long time. We keep track per ring seqno progress and
250 * if there are no progress, hangcheck score for that ring is increased.
251 * Further, acthd is inspected to see if the ring is stuck. On stuck case
252 * we kick the ring. If we see no progress on three subsequent calls
253 * we assume chip is wedged and try to fix it by resetting the chip.
255 static void hangcheck_elapsed(struct work_struct *work)
257 struct intel_gt *gt =
258 container_of(work, typeof(*gt), hangcheck.work.work);
259 intel_engine_mask_t hung = 0, stuck = 0, wedged = 0;
260 struct intel_engine_cs *engine;
261 enum intel_engine_id id;
262 intel_wakeref_t wakeref;
264 if (!i915_modparams.enable_hangcheck)
267 if (!READ_ONCE(gt->awake))
270 if (intel_gt_is_wedged(gt))
273 wakeref = intel_runtime_pm_get_if_in_use(>->i915->runtime_pm);
277 /* As enabling the GPU requires fairly extensive mmio access,
278 * periodically arm the mmio checker to see if we are triggering
279 * any invalid access.
281 intel_uncore_arm_unclaimed_mmio_detection(gt->uncore);
283 for_each_engine(engine, gt->i915, id) {
286 intel_engine_signal_breadcrumbs(engine);
288 hangcheck_load_sample(engine, &hc);
289 hangcheck_accumulate_sample(engine, &hc);
290 hangcheck_store_sample(engine, &hc);
293 hung |= engine->mask;
294 if (hc.action != ENGINE_DEAD)
295 stuck |= engine->mask;
299 wedged |= engine->mask;
302 if (GEM_SHOW_DEBUG() && (hung | stuck)) {
303 struct drm_printer p = drm_debug_printer("hangcheck");
305 for_each_engine(engine, gt->i915, id) {
306 if (intel_engine_is_idle(engine))
309 intel_engine_dump(engine, &p, "%s\n", engine->name);
314 dev_err(gt->i915->drm.dev,
315 "GPU recovery timed out,"
316 " cancelling all in-flight rendering.\n");
318 intel_gt_set_wedged(gt);
322 hangcheck_declare_hang(gt, hung, stuck);
324 intel_runtime_pm_put(>->i915->runtime_pm, wakeref);
326 /* Reset timer in case GPU hangs without another request being added */
327 intel_gt_queue_hangcheck(gt);
330 void intel_gt_queue_hangcheck(struct intel_gt *gt)
334 if (unlikely(!i915_modparams.enable_hangcheck))
338 * Don't continually defer the hangcheck so that it is always run at
339 * least once after work has been scheduled on any ring. Otherwise,
340 * we will ignore a hung ring if a second ring is kept busy.
343 delay = round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES);
344 queue_delayed_work(system_long_wq, >->hangcheck.work, delay);
347 void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
349 memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
350 engine->hangcheck.action_timestamp = jiffies;
353 void intel_gt_init_hangcheck(struct intel_gt *gt)
355 INIT_DELAYED_WORK(>->hangcheck.work, hangcheck_elapsed);
358 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
359 #include "selftest_hangcheck.c"