Linux Audio

Check our new training course

Loading...
Note: File does not exist in v3.1.
  1/*
  2 * Copyright © 2016 Intel Corporation
  3 *
  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:
 10 *
 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
 13 * Software.
 14 *
 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
 21 * IN THE SOFTWARE.
 22 *
 23 */
 24
 25#include "i915_drv.h"
 26#include "intel_engine.h"
 27#include "intel_gt.h"
 28#include "intel_reset.h"
 29
 30struct hangcheck {
 31	u64 acthd;
 32	u32 ring;
 33	u32 head;
 34	enum intel_engine_hangcheck_action action;
 35	unsigned long action_timestamp;
 36	int deadlock;
 37	struct intel_instdone instdone;
 38	bool wedged:1;
 39	bool stalled:1;
 40};
 41
 42static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
 43{
 44	u32 tmp = current_instdone | *old_instdone;
 45	bool unchanged;
 46
 47	unchanged = tmp == *old_instdone;
 48	*old_instdone |= tmp;
 49
 50	return unchanged;
 51}
 52
 53static bool subunits_stuck(struct intel_engine_cs *engine)
 54{
 55	struct drm_i915_private *dev_priv = engine->i915;
 56	struct intel_instdone instdone;
 57	struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
 58	bool stuck;
 59	int slice;
 60	int subslice;
 61
 62	intel_engine_get_instdone(engine, &instdone);
 63
 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.
 68	 */
 69	stuck = instdone_unchanged(instdone.instdone,
 70				   &accu_instdone->instdone);
 71	stuck &= instdone_unchanged(instdone.slice_common,
 72				    &accu_instdone->slice_common);
 73
 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]);
 79	}
 80
 81	return stuck;
 82}
 83
 84static enum intel_engine_hangcheck_action
 85head_stuck(struct intel_engine_cs *engine, u64 acthd)
 86{
 87	if (acthd != engine->hangcheck.acthd) {
 88
 89		/* Clear subunit states on head movement */
 90		memset(&engine->hangcheck.instdone, 0,
 91		       sizeof(engine->hangcheck.instdone));
 92
 93		return ENGINE_ACTIVE_HEAD;
 94	}
 95
 96	if (!subunits_stuck(engine))
 97		return ENGINE_ACTIVE_SUBUNITS;
 98
 99	return ENGINE_DEAD;
100}
101
102static enum intel_engine_hangcheck_action
103engine_stuck(struct intel_engine_cs *engine, u64 acthd)
104{
105	enum intel_engine_hangcheck_action ha;
106	u32 tmp;
107
108	ha = head_stuck(engine, acthd);
109	if (ha != ENGINE_DEAD)
110		return ha;
111
112	if (IS_GEN(engine->i915, 2))
113		return ENGINE_DEAD;
114
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.
119	 */
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;
126	}
127
128	return ENGINE_DEAD;
129}
130
131static void hangcheck_load_sample(struct intel_engine_cs *engine,
132				  struct hangcheck *hc)
133{
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);
137}
138
139static void hangcheck_store_sample(struct intel_engine_cs *engine,
140				   const struct hangcheck *hc)
141{
142	engine->hangcheck.acthd = hc->acthd;
143	engine->hangcheck.last_ring = hc->ring;
144	engine->hangcheck.last_head = hc->head;
145}
146
147static enum intel_engine_hangcheck_action
148hangcheck_get_action(struct intel_engine_cs *engine,
149		     const struct hangcheck *hc)
150{
151	if (intel_engine_is_idle(engine))
152		return ENGINE_IDLE;
153
154	if (engine->hangcheck.last_ring != hc->ring)
155		return ENGINE_ACTIVE_SEQNO;
156
157	if (engine->hangcheck.last_head != hc->head)
158		return ENGINE_ACTIVE_SEQNO;
159
160	return engine_stuck(engine, hc->acthd);
161}
162
163static void hangcheck_accumulate_sample(struct intel_engine_cs *engine,
164					struct hangcheck *hc)
165{
166	unsigned long timeout = I915_ENGINE_DEAD_TIMEOUT;
167
168	hc->action = hangcheck_get_action(engine, hc);
169
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.
183	 */
184
185	switch (hc->action) {
186	case ENGINE_IDLE:
187	case ENGINE_ACTIVE_SEQNO:
188		/* Clear head and subunit states on seqno movement */
189		hc->acthd = 0;
190
191		memset(&engine->hangcheck.instdone, 0,
192		       sizeof(engine->hangcheck.instdone));
193
194		/* Intentional fall through */
195	case ENGINE_WAIT_KICK:
196	case ENGINE_WAIT:
197		engine->hangcheck.action_timestamp = jiffies;
198		break;
199
200	case ENGINE_ACTIVE_HEAD:
201	case ENGINE_ACTIVE_SUBUNITS:
202		/*
203		 * Seqno stuck with still active engine gets leeway,
204		 * in hopes that it is just a long shader.
205		 */
206		timeout = I915_SEQNO_DEAD_TIMEOUT;
207		break;
208
209	case ENGINE_DEAD:
210		break;
211
212	default:
213		MISSING_CASE(hc->action);
214	}
215
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);
221}
222
223static void hangcheck_declare_hang(struct intel_gt *gt,
224				   intel_engine_mask_t hung,
225				   intel_engine_mask_t stuck)
226{
227	struct intel_engine_cs *engine;
228	intel_engine_mask_t tmp;
229	char msg[80];
230	int len;
231
232	/* If some rings hung but others were still busy, only
233	 * blame the hanging rings in the synopsis.
234	 */
235	if (stuck != hung)
236		hung &= ~stuck;
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);
242	msg[len-2] = '\0';
243
244	return intel_gt_handle_error(gt, hung, I915_ERROR_CAPTURE, "%s", msg);
245}
246
247/*
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.
254 */
255static void hangcheck_elapsed(struct work_struct *work)
256{
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;
263
264	if (!i915_modparams.enable_hangcheck)
265		return;
266
267	if (!READ_ONCE(gt->awake))
268		return;
269
270	if (intel_gt_is_wedged(gt))
271		return;
272
273	wakeref = intel_runtime_pm_get_if_in_use(&gt->i915->runtime_pm);
274	if (!wakeref)
275		return;
276
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.
280	 */
281	intel_uncore_arm_unclaimed_mmio_detection(gt->uncore);
282
283	for_each_engine(engine, gt->i915, id) {
284		struct hangcheck hc;
285
286		intel_engine_signal_breadcrumbs(engine);
287
288		hangcheck_load_sample(engine, &hc);
289		hangcheck_accumulate_sample(engine, &hc);
290		hangcheck_store_sample(engine, &hc);
291
292		if (hc.stalled) {
293			hung |= engine->mask;
294			if (hc.action != ENGINE_DEAD)
295				stuck |= engine->mask;
296		}
297
298		if (hc.wedged)
299			wedged |= engine->mask;
300	}
301
302	if (GEM_SHOW_DEBUG() && (hung | stuck)) {
303		struct drm_printer p = drm_debug_printer("hangcheck");
304
305		for_each_engine(engine, gt->i915, id) {
306			if (intel_engine_is_idle(engine))
307				continue;
308
309			intel_engine_dump(engine, &p, "%s\n", engine->name);
310		}
311	}
312
313	if (wedged) {
314		dev_err(gt->i915->drm.dev,
315			"GPU recovery timed out,"
316			" cancelling all in-flight rendering.\n");
317		GEM_TRACE_DUMP();
318		intel_gt_set_wedged(gt);
319	}
320
321	if (hung)
322		hangcheck_declare_hang(gt, hung, stuck);
323
324	intel_runtime_pm_put(&gt->i915->runtime_pm, wakeref);
325
326	/* Reset timer in case GPU hangs without another request being added */
327	intel_gt_queue_hangcheck(gt);
328}
329
330void intel_gt_queue_hangcheck(struct intel_gt *gt)
331{
332	unsigned long delay;
333
334	if (unlikely(!i915_modparams.enable_hangcheck))
335		return;
336
337	/*
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.
341	 */
342
343	delay = round_jiffies_up_relative(DRM_I915_HANGCHECK_JIFFIES);
344	queue_delayed_work(system_long_wq, &gt->hangcheck.work, delay);
345}
346
347void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
348{
349	memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
350	engine->hangcheck.action_timestamp = jiffies;
351}
352
353void intel_gt_init_hangcheck(struct intel_gt *gt)
354{
355	INIT_DELAYED_WORK(&gt->hangcheck.work, hangcheck_elapsed);
356}
357
358#if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
359#include "selftest_hangcheck.c"
360#endif