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