xref: /linux/drivers/gpu/drm/i915/display/intel_fifo_underrun.c (revision ad36a322619c14ba35872129a401ee214bfad875)
1 /*
2  * Copyright © 2014 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  * Authors:
24  *    Daniel Vetter <daniel.vetter@ffwll.ch>
25  *
26  */
27 
28 #include "i915_drv.h"
29 #include "i915_reg.h"
30 #include "intel_de.h"
31 #include "intel_display_irq.h"
32 #include "intel_display_trace.h"
33 #include "intel_display_types.h"
34 #include "intel_fbc.h"
35 #include "intel_fifo_underrun.h"
36 #include "intel_pch_display.h"
37 
38 /**
39  * DOC: fifo underrun handling
40  *
41  * The i915 driver checks for display fifo underruns using the interrupt signals
42  * provided by the hardware. This is enabled by default and fairly useful to
43  * debug display issues, especially watermark settings.
44  *
45  * If an underrun is detected this is logged into dmesg. To avoid flooding logs
46  * and occupying the cpu underrun interrupts are disabled after the first
47  * occurrence until the next modeset on a given pipe.
48  *
49  * Note that underrun detection on gmch platforms is a bit more ugly since there
50  * is no interrupt (despite that the signalling bit is in the PIPESTAT pipe
51  * interrupt register). Also on some other platforms underrun interrupts are
52  * shared, which means that if we detect an underrun we need to disable underrun
53  * reporting on all pipes.
54  *
55  * The code also supports underrun detection on the PCH transcoder.
56  */
57 
58 static bool ivb_can_enable_err_int(struct drm_device *dev)
59 {
60 	struct intel_display *display = to_intel_display(dev);
61 	struct drm_i915_private *dev_priv = to_i915(dev);
62 	struct intel_crtc *crtc;
63 	enum pipe pipe;
64 
65 	lockdep_assert_held(&dev_priv->irq_lock);
66 
67 	for_each_pipe(dev_priv, pipe) {
68 		crtc = intel_crtc_for_pipe(display, pipe);
69 
70 		if (crtc->cpu_fifo_underrun_disabled)
71 			return false;
72 	}
73 
74 	return true;
75 }
76 
77 static bool cpt_can_enable_serr_int(struct drm_device *dev)
78 {
79 	struct intel_display *display = to_intel_display(dev);
80 	struct drm_i915_private *dev_priv = to_i915(dev);
81 	enum pipe pipe;
82 	struct intel_crtc *crtc;
83 
84 	lockdep_assert_held(&dev_priv->irq_lock);
85 
86 	for_each_pipe(dev_priv, pipe) {
87 		crtc = intel_crtc_for_pipe(display, pipe);
88 
89 		if (crtc->pch_fifo_underrun_disabled)
90 			return false;
91 	}
92 
93 	return true;
94 }
95 
96 static void i9xx_check_fifo_underruns(struct intel_crtc *crtc)
97 {
98 	struct intel_display *display = to_intel_display(crtc);
99 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
100 	i915_reg_t reg = PIPESTAT(dev_priv, crtc->pipe);
101 	u32 enable_mask;
102 
103 	lockdep_assert_held(&dev_priv->irq_lock);
104 
105 	if ((intel_de_read(dev_priv, reg) & PIPE_FIFO_UNDERRUN_STATUS) == 0)
106 		return;
107 
108 	enable_mask = i915_pipestat_enable_mask(dev_priv, crtc->pipe);
109 	intel_de_write(dev_priv, reg, enable_mask | PIPE_FIFO_UNDERRUN_STATUS);
110 	intel_de_posting_read(dev_priv, reg);
111 
112 	trace_intel_cpu_fifo_underrun(display, crtc->pipe);
113 	drm_err(&dev_priv->drm, "pipe %c underrun\n", pipe_name(crtc->pipe));
114 }
115 
116 static void i9xx_set_fifo_underrun_reporting(struct drm_device *dev,
117 					     enum pipe pipe,
118 					     bool enable, bool old)
119 {
120 	struct drm_i915_private *dev_priv = to_i915(dev);
121 	i915_reg_t reg = PIPESTAT(dev_priv, pipe);
122 
123 	lockdep_assert_held(&dev_priv->irq_lock);
124 
125 	if (enable) {
126 		u32 enable_mask = i915_pipestat_enable_mask(dev_priv, pipe);
127 
128 		intel_de_write(dev_priv, reg,
129 			       enable_mask | PIPE_FIFO_UNDERRUN_STATUS);
130 		intel_de_posting_read(dev_priv, reg);
131 	} else {
132 		if (old && intel_de_read(dev_priv, reg) & PIPE_FIFO_UNDERRUN_STATUS)
133 			drm_err(&dev_priv->drm, "pipe %c underrun\n",
134 				pipe_name(pipe));
135 	}
136 }
137 
138 static void ilk_set_fifo_underrun_reporting(struct drm_device *dev,
139 					    enum pipe pipe, bool enable)
140 {
141 	struct drm_i915_private *dev_priv = to_i915(dev);
142 	u32 bit = (pipe == PIPE_A) ?
143 		DE_PIPEA_FIFO_UNDERRUN : DE_PIPEB_FIFO_UNDERRUN;
144 
145 	if (enable)
146 		ilk_enable_display_irq(dev_priv, bit);
147 	else
148 		ilk_disable_display_irq(dev_priv, bit);
149 }
150 
151 static void ivb_check_fifo_underruns(struct intel_crtc *crtc)
152 {
153 	struct intel_display *display = to_intel_display(crtc);
154 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
155 	enum pipe pipe = crtc->pipe;
156 	u32 err_int = intel_de_read(dev_priv, GEN7_ERR_INT);
157 
158 	lockdep_assert_held(&dev_priv->irq_lock);
159 
160 	if ((err_int & ERR_INT_FIFO_UNDERRUN(pipe)) == 0)
161 		return;
162 
163 	intel_de_write(dev_priv, GEN7_ERR_INT, ERR_INT_FIFO_UNDERRUN(pipe));
164 	intel_de_posting_read(dev_priv, GEN7_ERR_INT);
165 
166 	trace_intel_cpu_fifo_underrun(display, pipe);
167 	drm_err(&dev_priv->drm, "fifo underrun on pipe %c\n", pipe_name(pipe));
168 }
169 
170 static void ivb_set_fifo_underrun_reporting(struct drm_device *dev,
171 					    enum pipe pipe, bool enable,
172 					    bool old)
173 {
174 	struct drm_i915_private *dev_priv = to_i915(dev);
175 	if (enable) {
176 		intel_de_write(dev_priv, GEN7_ERR_INT,
177 			       ERR_INT_FIFO_UNDERRUN(pipe));
178 
179 		if (!ivb_can_enable_err_int(dev))
180 			return;
181 
182 		ilk_enable_display_irq(dev_priv, DE_ERR_INT_IVB);
183 	} else {
184 		ilk_disable_display_irq(dev_priv, DE_ERR_INT_IVB);
185 
186 		if (old &&
187 		    intel_de_read(dev_priv, GEN7_ERR_INT) & ERR_INT_FIFO_UNDERRUN(pipe)) {
188 			drm_err(&dev_priv->drm,
189 				"uncleared fifo underrun on pipe %c\n",
190 				pipe_name(pipe));
191 		}
192 	}
193 }
194 
195 static u32
196 icl_pipe_status_underrun_mask(struct drm_i915_private *dev_priv)
197 {
198 	u32 mask = PIPE_STATUS_UNDERRUN;
199 
200 	if (DISPLAY_VER(dev_priv) >= 13)
201 		mask |= PIPE_STATUS_SOFT_UNDERRUN_XELPD |
202 			PIPE_STATUS_HARD_UNDERRUN_XELPD |
203 			PIPE_STATUS_PORT_UNDERRUN_XELPD;
204 
205 	return mask;
206 }
207 
208 static void bdw_set_fifo_underrun_reporting(struct drm_device *dev,
209 					    enum pipe pipe, bool enable)
210 {
211 	struct drm_i915_private *dev_priv = to_i915(dev);
212 	u32 mask = gen8_de_pipe_underrun_mask(dev_priv);
213 
214 	if (enable) {
215 		if (DISPLAY_VER(dev_priv) >= 11)
216 			intel_de_write(dev_priv,
217 				       ICL_PIPESTATUS(dev_priv, pipe),
218 				       icl_pipe_status_underrun_mask(dev_priv));
219 
220 		bdw_enable_pipe_irq(dev_priv, pipe, mask);
221 	} else {
222 		bdw_disable_pipe_irq(dev_priv, pipe, mask);
223 	}
224 }
225 
226 static void ibx_set_fifo_underrun_reporting(struct drm_device *dev,
227 					    enum pipe pch_transcoder,
228 					    bool enable)
229 {
230 	struct drm_i915_private *dev_priv = to_i915(dev);
231 	u32 bit = (pch_transcoder == PIPE_A) ?
232 		SDE_TRANSA_FIFO_UNDER : SDE_TRANSB_FIFO_UNDER;
233 
234 	if (enable)
235 		ibx_enable_display_interrupt(dev_priv, bit);
236 	else
237 		ibx_disable_display_interrupt(dev_priv, bit);
238 }
239 
240 static void cpt_check_pch_fifo_underruns(struct intel_crtc *crtc)
241 {
242 	struct intel_display *display = to_intel_display(crtc);
243 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
244 	enum pipe pch_transcoder = crtc->pipe;
245 	u32 serr_int = intel_de_read(dev_priv, SERR_INT);
246 
247 	lockdep_assert_held(&dev_priv->irq_lock);
248 
249 	if ((serr_int & SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder)) == 0)
250 		return;
251 
252 	intel_de_write(dev_priv, SERR_INT,
253 		       SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
254 	intel_de_posting_read(dev_priv, SERR_INT);
255 
256 	trace_intel_pch_fifo_underrun(display, pch_transcoder);
257 	drm_err(&dev_priv->drm, "pch fifo underrun on pch transcoder %c\n",
258 		pipe_name(pch_transcoder));
259 }
260 
261 static void cpt_set_fifo_underrun_reporting(struct drm_device *dev,
262 					    enum pipe pch_transcoder,
263 					    bool enable, bool old)
264 {
265 	struct drm_i915_private *dev_priv = to_i915(dev);
266 
267 	if (enable) {
268 		intel_de_write(dev_priv, SERR_INT,
269 			       SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder));
270 
271 		if (!cpt_can_enable_serr_int(dev))
272 			return;
273 
274 		ibx_enable_display_interrupt(dev_priv, SDE_ERROR_CPT);
275 	} else {
276 		ibx_disable_display_interrupt(dev_priv, SDE_ERROR_CPT);
277 
278 		if (old && intel_de_read(dev_priv, SERR_INT) &
279 		    SERR_INT_TRANS_FIFO_UNDERRUN(pch_transcoder)) {
280 			drm_err(&dev_priv->drm,
281 				"uncleared pch fifo underrun on pch transcoder %c\n",
282 				pipe_name(pch_transcoder));
283 		}
284 	}
285 }
286 
287 static bool __intel_set_cpu_fifo_underrun_reporting(struct drm_device *dev,
288 						    enum pipe pipe, bool enable)
289 {
290 	struct intel_display *display = to_intel_display(dev);
291 	struct drm_i915_private *dev_priv = to_i915(dev);
292 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
293 	bool old;
294 
295 	lockdep_assert_held(&dev_priv->irq_lock);
296 
297 	old = !crtc->cpu_fifo_underrun_disabled;
298 	crtc->cpu_fifo_underrun_disabled = !enable;
299 
300 	if (HAS_GMCH(dev_priv))
301 		i9xx_set_fifo_underrun_reporting(dev, pipe, enable, old);
302 	else if (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv))
303 		ilk_set_fifo_underrun_reporting(dev, pipe, enable);
304 	else if (DISPLAY_VER(dev_priv) == 7)
305 		ivb_set_fifo_underrun_reporting(dev, pipe, enable, old);
306 	else if (DISPLAY_VER(dev_priv) >= 8)
307 		bdw_set_fifo_underrun_reporting(dev, pipe, enable);
308 
309 	return old;
310 }
311 
312 /**
313  * intel_set_cpu_fifo_underrun_reporting - set cpu fifo underrrun reporting state
314  * @dev_priv: i915 device instance
315  * @pipe: (CPU) pipe to set state for
316  * @enable: whether underruns should be reported or not
317  *
318  * This function sets the fifo underrun state for @pipe. It is used in the
319  * modeset code to avoid false positives since on many platforms underruns are
320  * expected when disabling or enabling the pipe.
321  *
322  * Notice that on some platforms disabling underrun reports for one pipe
323  * disables for all due to shared interrupts. Actual reporting is still per-pipe
324  * though.
325  *
326  * Returns the previous state of underrun reporting.
327  */
328 bool intel_set_cpu_fifo_underrun_reporting(struct drm_i915_private *dev_priv,
329 					   enum pipe pipe, bool enable)
330 {
331 	unsigned long flags;
332 	bool ret;
333 
334 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
335 	ret = __intel_set_cpu_fifo_underrun_reporting(&dev_priv->drm, pipe,
336 						      enable);
337 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
338 
339 	return ret;
340 }
341 
342 /**
343  * intel_set_pch_fifo_underrun_reporting - set PCH fifo underrun reporting state
344  * @dev_priv: i915 device instance
345  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
346  * @enable: whether underruns should be reported or not
347  *
348  * This function makes us disable or enable PCH fifo underruns for a specific
349  * PCH transcoder. Notice that on some PCHs (e.g. CPT/PPT), disabling FIFO
350  * underrun reporting for one transcoder may also disable all the other PCH
351  * error interruts for the other transcoders, due to the fact that there's just
352  * one interrupt mask/enable bit for all the transcoders.
353  *
354  * Returns the previous state of underrun reporting.
355  */
356 bool intel_set_pch_fifo_underrun_reporting(struct drm_i915_private *dev_priv,
357 					   enum pipe pch_transcoder,
358 					   bool enable)
359 {
360 	struct intel_display *display = &dev_priv->display;
361 	struct intel_crtc *crtc =
362 		intel_crtc_for_pipe(display, pch_transcoder);
363 	unsigned long flags;
364 	bool old;
365 
366 	/*
367 	 * NOTE: Pre-LPT has a fixed cpu pipe -> pch transcoder mapping, but LPT
368 	 * has only one pch transcoder A that all pipes can use. To avoid racy
369 	 * pch transcoder -> pipe lookups from interrupt code simply store the
370 	 * underrun statistics in crtc A. Since we never expose this anywhere
371 	 * nor use it outside of the fifo underrun code here using the "wrong"
372 	 * crtc on LPT won't cause issues.
373 	 */
374 
375 	spin_lock_irqsave(&dev_priv->irq_lock, flags);
376 
377 	old = !crtc->pch_fifo_underrun_disabled;
378 	crtc->pch_fifo_underrun_disabled = !enable;
379 
380 	if (HAS_PCH_IBX(dev_priv))
381 		ibx_set_fifo_underrun_reporting(&dev_priv->drm,
382 						pch_transcoder,
383 						enable);
384 	else
385 		cpt_set_fifo_underrun_reporting(&dev_priv->drm,
386 						pch_transcoder,
387 						enable, old);
388 
389 	spin_unlock_irqrestore(&dev_priv->irq_lock, flags);
390 	return old;
391 }
392 
393 /**
394  * intel_cpu_fifo_underrun_irq_handler - handle CPU fifo underrun interrupt
395  * @dev_priv: i915 device instance
396  * @pipe: (CPU) pipe to set state for
397  *
398  * This handles a CPU fifo underrun interrupt, generating an underrun warning
399  * into dmesg if underrun reporting is enabled and then disables the underrun
400  * interrupt to avoid an irq storm.
401  */
402 void intel_cpu_fifo_underrun_irq_handler(struct drm_i915_private *dev_priv,
403 					 enum pipe pipe)
404 {
405 	struct intel_display *display = &dev_priv->display;
406 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
407 	u32 underruns = 0;
408 
409 	/* We may be called too early in init, thanks BIOS! */
410 	if (crtc == NULL)
411 		return;
412 
413 	/* GMCH can't disable fifo underruns, filter them. */
414 	if (HAS_GMCH(dev_priv) &&
415 	    crtc->cpu_fifo_underrun_disabled)
416 		return;
417 
418 	/*
419 	 * Starting with display version 11, the PIPE_STAT register records
420 	 * whether an underrun has happened, and on XELPD+, it will also record
421 	 * whether the underrun was soft/hard and whether it was triggered by
422 	 * the downstream port logic.  We should clear these bits (which use
423 	 * write-1-to-clear logic) too.
424 	 *
425 	 * Note that although the IIR gives us the same underrun and soft/hard
426 	 * information, PIPE_STAT is the only place we can find out whether
427 	 * the underrun was caused by the downstream port.
428 	 */
429 	if (DISPLAY_VER(dev_priv) >= 11) {
430 		underruns = intel_de_read(dev_priv,
431 					  ICL_PIPESTATUS(dev_priv, pipe)) &
432 			icl_pipe_status_underrun_mask(dev_priv);
433 		intel_de_write(dev_priv, ICL_PIPESTATUS(dev_priv, pipe),
434 			       underruns);
435 	}
436 
437 	if (intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false)) {
438 		trace_intel_cpu_fifo_underrun(display, pipe);
439 
440 		if (DISPLAY_VER(dev_priv) >= 11)
441 			drm_err(&dev_priv->drm, "CPU pipe %c FIFO underrun: %s%s%s%s\n",
442 				pipe_name(pipe),
443 				underruns & PIPE_STATUS_SOFT_UNDERRUN_XELPD ? "soft," : "",
444 				underruns & PIPE_STATUS_HARD_UNDERRUN_XELPD ? "hard," : "",
445 				underruns & PIPE_STATUS_PORT_UNDERRUN_XELPD ? "port," : "",
446 				underruns & PIPE_STATUS_UNDERRUN ? "transcoder," : "");
447 		else
448 			drm_err(&dev_priv->drm, "CPU pipe %c FIFO underrun\n", pipe_name(pipe));
449 	}
450 
451 	intel_fbc_handle_fifo_underrun_irq(&dev_priv->display);
452 }
453 
454 /**
455  * intel_pch_fifo_underrun_irq_handler - handle PCH fifo underrun interrupt
456  * @dev_priv: i915 device instance
457  * @pch_transcoder: the PCH transcoder (same as pipe on IVB and older)
458  *
459  * This handles a PCH fifo underrun interrupt, generating an underrun warning
460  * into dmesg if underrun reporting is enabled and then disables the underrun
461  * interrupt to avoid an irq storm.
462  */
463 void intel_pch_fifo_underrun_irq_handler(struct drm_i915_private *dev_priv,
464 					 enum pipe pch_transcoder)
465 {
466 	struct intel_display *display = &dev_priv->display;
467 
468 	if (intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder,
469 						  false)) {
470 		trace_intel_pch_fifo_underrun(display, pch_transcoder);
471 		drm_err(&dev_priv->drm, "PCH transcoder %c FIFO underrun\n",
472 			pipe_name(pch_transcoder));
473 	}
474 }
475 
476 /**
477  * intel_check_cpu_fifo_underruns - check for CPU fifo underruns immediately
478  * @dev_priv: i915 device instance
479  *
480  * Check for CPU fifo underruns immediately. Useful on IVB/HSW where the shared
481  * error interrupt may have been disabled, and so CPU fifo underruns won't
482  * necessarily raise an interrupt, and on GMCH platforms where underruns never
483  * raise an interrupt.
484  */
485 void intel_check_cpu_fifo_underruns(struct drm_i915_private *dev_priv)
486 {
487 	struct intel_crtc *crtc;
488 
489 	spin_lock_irq(&dev_priv->irq_lock);
490 
491 	for_each_intel_crtc(&dev_priv->drm, crtc) {
492 		if (crtc->cpu_fifo_underrun_disabled)
493 			continue;
494 
495 		if (HAS_GMCH(dev_priv))
496 			i9xx_check_fifo_underruns(crtc);
497 		else if (DISPLAY_VER(dev_priv) == 7)
498 			ivb_check_fifo_underruns(crtc);
499 	}
500 
501 	spin_unlock_irq(&dev_priv->irq_lock);
502 }
503 
504 /**
505  * intel_check_pch_fifo_underruns - check for PCH fifo underruns immediately
506  * @dev_priv: i915 device instance
507  *
508  * Check for PCH fifo underruns immediately. Useful on CPT/PPT where the shared
509  * error interrupt may have been disabled, and so PCH fifo underruns won't
510  * necessarily raise an interrupt.
511  */
512 void intel_check_pch_fifo_underruns(struct drm_i915_private *dev_priv)
513 {
514 	struct intel_crtc *crtc;
515 
516 	spin_lock_irq(&dev_priv->irq_lock);
517 
518 	for_each_intel_crtc(&dev_priv->drm, crtc) {
519 		if (crtc->pch_fifo_underrun_disabled)
520 			continue;
521 
522 		if (HAS_PCH_CPT(dev_priv))
523 			cpt_check_pch_fifo_underruns(crtc);
524 	}
525 
526 	spin_unlock_irq(&dev_priv->irq_lock);
527 }
528 
529 void intel_init_fifo_underrun_reporting(struct drm_i915_private *i915,
530 					struct intel_crtc *crtc,
531 					bool enable)
532 {
533 	crtc->cpu_fifo_underrun_disabled = !enable;
534 
535 	/*
536 	 * We track the PCH trancoder underrun reporting state
537 	 * within the crtc. With crtc for pipe A housing the underrun
538 	 * reporting state for PCH transcoder A, crtc for pipe B housing
539 	 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
540 	 * and marking underrun reporting as disabled for the non-existing
541 	 * PCH transcoders B and C would prevent enabling the south
542 	 * error interrupt (see cpt_can_enable_serr_int()).
543 	 */
544 	if (intel_has_pch_trancoder(i915, crtc->pipe))
545 		crtc->pch_fifo_underrun_disabled = !enable;
546 }
547