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