Merge tag 'drm-intel-next-2019-05-24' of git://anongit.freedesktop.org/drm/drm-intel...
[linux-2.6-microblaze.git] / drivers / gpu / drm / i915 / gt / intel_hangcheck.c
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 "intel_reset.h"
26 #include "i915_drv.h"
27
28 struct hangcheck {
29         u64 acthd;
30         u32 ring;
31         u32 head;
32         enum intel_engine_hangcheck_action action;
33         unsigned long action_timestamp;
34         int deadlock;
35         struct intel_instdone instdone;
36         bool wedged:1;
37         bool stalled:1;
38 };
39
40 static bool instdone_unchanged(u32 current_instdone, u32 *old_instdone)
41 {
42         u32 tmp = current_instdone | *old_instdone;
43         bool unchanged;
44
45         unchanged = tmp == *old_instdone;
46         *old_instdone |= tmp;
47
48         return unchanged;
49 }
50
51 static bool subunits_stuck(struct intel_engine_cs *engine)
52 {
53         struct drm_i915_private *dev_priv = engine->i915;
54         struct intel_instdone instdone;
55         struct intel_instdone *accu_instdone = &engine->hangcheck.instdone;
56         bool stuck;
57         int slice;
58         int subslice;
59
60         if (engine->id != RCS0)
61                 return true;
62
63         intel_engine_get_instdone(engine, &instdone);
64
65         /* There might be unstable subunit states even when
66          * actual head is not moving. Filter out the unstable ones by
67          * accumulating the undone -> done transitions and only
68          * consider those as progress.
69          */
70         stuck = instdone_unchanged(instdone.instdone,
71                                    &accu_instdone->instdone);
72         stuck &= instdone_unchanged(instdone.slice_common,
73                                     &accu_instdone->slice_common);
74
75         for_each_instdone_slice_subslice(dev_priv, slice, subslice) {
76                 stuck &= instdone_unchanged(instdone.sampler[slice][subslice],
77                                             &accu_instdone->sampler[slice][subslice]);
78                 stuck &= instdone_unchanged(instdone.row[slice][subslice],
79                                             &accu_instdone->row[slice][subslice]);
80         }
81
82         return stuck;
83 }
84
85 static enum intel_engine_hangcheck_action
86 head_stuck(struct intel_engine_cs *engine, u64 acthd)
87 {
88         if (acthd != engine->hangcheck.acthd) {
89
90                 /* Clear subunit states on head movement */
91                 memset(&engine->hangcheck.instdone, 0,
92                        sizeof(engine->hangcheck.instdone));
93
94                 return ENGINE_ACTIVE_HEAD;
95         }
96
97         if (!subunits_stuck(engine))
98                 return ENGINE_ACTIVE_SUBUNITS;
99
100         return ENGINE_DEAD;
101 }
102
103 static enum intel_engine_hangcheck_action
104 engine_stuck(struct intel_engine_cs *engine, u64 acthd)
105 {
106         struct drm_i915_private *dev_priv = engine->i915;
107         enum intel_engine_hangcheck_action ha;
108         u32 tmp;
109
110         ha = head_stuck(engine, acthd);
111         if (ha != ENGINE_DEAD)
112                 return ha;
113
114         if (IS_GEN(dev_priv, 2))
115                 return ENGINE_DEAD;
116
117         /* Is the chip hanging on a WAIT_FOR_EVENT?
118          * If so we can simply poke the RB_WAIT bit
119          * and break the hang. This should work on
120          * all but the second generation chipsets.
121          */
122         tmp = ENGINE_READ(engine, RING_CTL);
123         if (tmp & RING_WAIT) {
124                 i915_handle_error(dev_priv, engine->mask, 0,
125                                   "stuck wait on %s", engine->name);
126                 ENGINE_WRITE(engine, RING_CTL, tmp);
127                 return ENGINE_WAIT_KICK;
128         }
129
130         return ENGINE_DEAD;
131 }
132
133 static void hangcheck_load_sample(struct intel_engine_cs *engine,
134                                   struct hangcheck *hc)
135 {
136         hc->acthd = intel_engine_get_active_head(engine);
137         hc->ring = ENGINE_READ(engine, RING_START);
138         hc->head = ENGINE_READ(engine, RING_HEAD);
139 }
140
141 static void hangcheck_store_sample(struct intel_engine_cs *engine,
142                                    const struct hangcheck *hc)
143 {
144         engine->hangcheck.acthd = hc->acthd;
145         engine->hangcheck.last_ring = hc->ring;
146         engine->hangcheck.last_head = hc->head;
147 }
148
149 static enum intel_engine_hangcheck_action
150 hangcheck_get_action(struct intel_engine_cs *engine,
151                      const struct hangcheck *hc)
152 {
153         if (intel_engine_is_idle(engine))
154                 return ENGINE_IDLE;
155
156         if (engine->hangcheck.last_ring != hc->ring)
157                 return ENGINE_ACTIVE_SEQNO;
158
159         if (engine->hangcheck.last_head != hc->head)
160                 return ENGINE_ACTIVE_SEQNO;
161
162         return engine_stuck(engine, hc->acthd);
163 }
164
165 static void hangcheck_accumulate_sample(struct intel_engine_cs *engine,
166                                         struct hangcheck *hc)
167 {
168         unsigned long timeout = I915_ENGINE_DEAD_TIMEOUT;
169
170         hc->action = hangcheck_get_action(engine, hc);
171
172         /* We always increment the progress
173          * if the engine is busy and still processing
174          * the same request, so that no single request
175          * can run indefinitely (such as a chain of
176          * batches). The only time we do not increment
177          * the hangcheck score on this ring, if this
178          * engine is in a legitimate wait for another
179          * engine. In that case the waiting engine is a
180          * victim and we want to be sure we catch the
181          * right culprit. Then every time we do kick
182          * the ring, make it as a progress as the seqno
183          * advancement might ensure and if not, it
184          * will catch the hanging engine.
185          */
186
187         switch (hc->action) {
188         case ENGINE_IDLE:
189         case ENGINE_ACTIVE_SEQNO:
190                 /* Clear head and subunit states on seqno movement */
191                 hc->acthd = 0;
192
193                 memset(&engine->hangcheck.instdone, 0,
194                        sizeof(engine->hangcheck.instdone));
195
196                 /* Intentional fall through */
197         case ENGINE_WAIT_KICK:
198         case ENGINE_WAIT:
199                 engine->hangcheck.action_timestamp = jiffies;
200                 break;
201
202         case ENGINE_ACTIVE_HEAD:
203         case ENGINE_ACTIVE_SUBUNITS:
204                 /*
205                  * Seqno stuck with still active engine gets leeway,
206                  * in hopes that it is just a long shader.
207                  */
208                 timeout = I915_SEQNO_DEAD_TIMEOUT;
209                 break;
210
211         case ENGINE_DEAD:
212                 break;
213
214         default:
215                 MISSING_CASE(hc->action);
216         }
217
218         hc->stalled = time_after(jiffies,
219                                  engine->hangcheck.action_timestamp + timeout);
220         hc->wedged = time_after(jiffies,
221                                  engine->hangcheck.action_timestamp +
222                                  I915_ENGINE_WEDGED_TIMEOUT);
223 }
224
225 static void hangcheck_declare_hang(struct drm_i915_private *i915,
226                                    unsigned int hung,
227                                    unsigned int stuck)
228 {
229         struct intel_engine_cs *engine;
230         intel_engine_mask_t tmp;
231         char msg[80];
232         int len;
233
234         /* If some rings hung but others were still busy, only
235          * blame the hanging rings in the synopsis.
236          */
237         if (stuck != hung)
238                 hung &= ~stuck;
239         len = scnprintf(msg, sizeof(msg),
240                         "%s on ", stuck == hung ? "no progress" : "hang");
241         for_each_engine_masked(engine, i915, hung, tmp)
242                 len += scnprintf(msg + len, sizeof(msg) - len,
243                                  "%s, ", engine->name);
244         msg[len-2] = '\0';
245
246         return i915_handle_error(i915, hung, I915_ERROR_CAPTURE, "%s", msg);
247 }
248
249 /*
250  * This is called when the chip hasn't reported back with completed
251  * batchbuffers in a long time. We keep track per ring seqno progress and
252  * if there are no progress, hangcheck score for that ring is increased.
253  * Further, acthd is inspected to see if the ring is stuck. On stuck case
254  * we kick the ring. If we see no progress on three subsequent calls
255  * we assume chip is wedged and try to fix it by resetting the chip.
256  */
257 static void i915_hangcheck_elapsed(struct work_struct *work)
258 {
259         struct drm_i915_private *dev_priv =
260                 container_of(work, typeof(*dev_priv),
261                              gpu_error.hangcheck_work.work);
262         struct intel_engine_cs *engine;
263         enum intel_engine_id id;
264         unsigned int hung = 0, stuck = 0, wedged = 0;
265         intel_wakeref_t wakeref;
266
267         if (!i915_modparams.enable_hangcheck)
268                 return;
269
270         if (!READ_ONCE(dev_priv->gt.awake))
271                 return;
272
273         if (i915_terminally_wedged(dev_priv))
274                 return;
275
276         wakeref = intel_runtime_pm_get_if_in_use(dev_priv);
277         if (!wakeref)
278                 return;
279
280         /* As enabling the GPU requires fairly extensive mmio access,
281          * periodically arm the mmio checker to see if we are triggering
282          * any invalid access.
283          */
284         intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
285
286         for_each_engine(engine, dev_priv, id) {
287                 struct hangcheck hc;
288
289                 intel_engine_signal_breadcrumbs(engine);
290
291                 hangcheck_load_sample(engine, &hc);
292                 hangcheck_accumulate_sample(engine, &hc);
293                 hangcheck_store_sample(engine, &hc);
294
295                 if (hc.stalled) {
296                         hung |= engine->mask;
297                         if (hc.action != ENGINE_DEAD)
298                                 stuck |= engine->mask;
299                 }
300
301                 if (hc.wedged)
302                         wedged |= engine->mask;
303         }
304
305         if (GEM_SHOW_DEBUG() && (hung | stuck)) {
306                 struct drm_printer p = drm_debug_printer("hangcheck");
307
308                 for_each_engine(engine, dev_priv, id) {
309                         if (intel_engine_is_idle(engine))
310                                 continue;
311
312                         intel_engine_dump(engine, &p, "%s\n", engine->name);
313                 }
314         }
315
316         if (wedged) {
317                 dev_err(dev_priv->drm.dev,
318                         "GPU recovery timed out,"
319                         " cancelling all in-flight rendering.\n");
320                 GEM_TRACE_DUMP();
321                 i915_gem_set_wedged(dev_priv);
322         }
323
324         if (hung)
325                 hangcheck_declare_hang(dev_priv, hung, stuck);
326
327         intel_runtime_pm_put(dev_priv, wakeref);
328
329         /* Reset timer in case GPU hangs without another request being added */
330         i915_queue_hangcheck(dev_priv);
331 }
332
333 void intel_engine_init_hangcheck(struct intel_engine_cs *engine)
334 {
335         memset(&engine->hangcheck, 0, sizeof(engine->hangcheck));
336         engine->hangcheck.action_timestamp = jiffies;
337 }
338
339 void intel_hangcheck_init(struct drm_i915_private *i915)
340 {
341         INIT_DELAYED_WORK(&i915->gpu_error.hangcheck_work,
342                           i915_hangcheck_elapsed);
343 }
344
345 #if IS_ENABLED(CONFIG_DRM_I915_SELFTEST)
346 #include "selftest_hangcheck.c"
347 #endif