1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2022-2023 Intel Corporation
4  */
5 
6 #include <drm/drm_vblank.h>
7 
8 #include "i915_drv.h"
9 #include "i915_reg.h"
10 #include "intel_color.h"
11 #include "intel_crtc.h"
12 #include "intel_de.h"
13 #include "intel_display_types.h"
14 #include "intel_vblank.h"
15 #include "intel_vrr.h"
16 
17 /*
18  * This timing diagram depicts the video signal in and
19  * around the vertical blanking period.
20  *
21  * Assumptions about the fictitious mode used in this example:
22  *  vblank_start >= 3
23  *  vsync_start = vblank_start + 1
24  *  vsync_end = vblank_start + 2
25  *  vtotal = vblank_start + 3
26  *
27  *           start of vblank:
28  *           latch double buffered registers
29  *           increment frame counter (ctg+)
30  *           generate start of vblank interrupt (gen4+)
31  *           |
32  *           |          frame start:
33  *           |          generate frame start interrupt (aka. vblank interrupt) (gmch)
34  *           |          may be shifted forward 1-3 extra lines via TRANSCONF
35  *           |          |
36  *           |          |  start of vsync:
37  *           |          |  generate vsync interrupt
38  *           |          |  |
39  * ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx___    ___xxxx
40  *       .   \hs/   .      \hs/          \hs/          \hs/   .      \hs/
41  * ----va---> <-----------------vb--------------------> <--------va-------------
42  *       |          |       <----vs----->                     |
43  * -vbs-----> <---vbs+1---> <---vbs+2---> <-----0-----> <-----1-----> <-----2--- (scanline counter gen2)
44  * -vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2---> <-----0--- (scanline counter gen3+)
45  * -vbs-2---> <---vbs-2---> <---vbs-1---> <---vbs-----> <---vbs+1---> <---vbs+2- (scanline counter hsw+ hdmi)
46  *       |          |                                         |
47  *       last visible pixel                                   first visible pixel
48  *                  |                                         increment frame counter (gen3/4)
49  *                  pixel counter = vblank_start * htotal     pixel counter = 0 (gen3/4)
50  *
51  * x  = horizontal active
52  * _  = horizontal blanking
53  * hs = horizontal sync
54  * va = vertical active
55  * vb = vertical blanking
56  * vs = vertical sync
57  * vbs = vblank_start (number)
58  *
59  * Summary:
60  * - most events happen at the start of horizontal sync
61  * - frame start happens at the start of horizontal blank, 1-4 lines
62  *   (depending on TRANSCONF settings) after the start of vblank
63  * - gen3/4 pixel and frame counter are synchronized with the start
64  *   of horizontal active on the first line of vertical active
65  */
66 
67 /*
68  * Called from drm generic code, passed a 'crtc', which we use as a pipe index.
69  */
i915_get_vblank_counter(struct drm_crtc * crtc)70 u32 i915_get_vblank_counter(struct drm_crtc *crtc)
71 {
72 	struct intel_display *display = to_intel_display(crtc->dev);
73 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
74 	const struct drm_display_mode *mode = &vblank->hwmode;
75 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
76 	u32 pixel, vbl_start, hsync_start, htotal;
77 	u64 frame;
78 
79 	/*
80 	 * On i965gm TV output the frame counter only works up to
81 	 * the point when we enable the TV encoder. After that the
82 	 * frame counter ceases to work and reads zero. We need a
83 	 * vblank wait before enabling the TV encoder and so we
84 	 * have to enable vblank interrupts while the frame counter
85 	 * is still in a working state. However the core vblank code
86 	 * does not like us returning non-zero frame counter values
87 	 * when we've told it that we don't have a working frame
88 	 * counter. Thus we must stop non-zero values leaking out.
89 	 */
90 	if (!vblank->max_vblank_count)
91 		return 0;
92 
93 	htotal = mode->crtc_htotal;
94 	hsync_start = mode->crtc_hsync_start;
95 	vbl_start = intel_mode_vblank_start(mode);
96 
97 	/* Convert to pixel count */
98 	vbl_start *= htotal;
99 
100 	/* Start of vblank event occurs at start of hsync */
101 	vbl_start -= htotal - hsync_start;
102 
103 	/*
104 	 * High & low register fields aren't synchronized, so make sure
105 	 * we get a low value that's stable across two reads of the high
106 	 * register.
107 	 */
108 	frame = intel_de_read64_2x32(display, PIPEFRAMEPIXEL(display, pipe),
109 				     PIPEFRAME(display, pipe));
110 
111 	pixel = frame & PIPE_PIXEL_MASK;
112 	frame = (frame >> PIPE_FRAME_LOW_SHIFT) & 0xffffff;
113 
114 	/*
115 	 * The frame counter increments at beginning of active.
116 	 * Cook up a vblank counter by also checking the pixel
117 	 * counter against vblank start.
118 	 */
119 	return (frame + (pixel >= vbl_start)) & 0xffffff;
120 }
121 
g4x_get_vblank_counter(struct drm_crtc * crtc)122 u32 g4x_get_vblank_counter(struct drm_crtc *crtc)
123 {
124 	struct intel_display *display = to_intel_display(crtc->dev);
125 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(crtc);
126 	enum pipe pipe = to_intel_crtc(crtc)->pipe;
127 
128 	if (!vblank->max_vblank_count)
129 		return 0;
130 
131 	return intel_de_read(display, PIPE_FRMCOUNT_G4X(display, pipe));
132 }
133 
intel_crtc_scanlines_since_frame_timestamp(struct intel_crtc * crtc)134 static u32 intel_crtc_scanlines_since_frame_timestamp(struct intel_crtc *crtc)
135 {
136 	struct intel_display *display = to_intel_display(crtc);
137 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base);
138 	const struct drm_display_mode *mode = &vblank->hwmode;
139 	u32 htotal = mode->crtc_htotal;
140 	u32 clock = mode->crtc_clock;
141 	u32 scan_prev_time, scan_curr_time, scan_post_time;
142 
143 	/*
144 	 * To avoid the race condition where we might cross into the
145 	 * next vblank just between the PIPE_FRMTMSTMP and TIMESTAMP_CTR
146 	 * reads. We make sure we read PIPE_FRMTMSTMP and TIMESTAMP_CTR
147 	 * during the same frame.
148 	 */
149 	do {
150 		/*
151 		 * This field provides read back of the display
152 		 * pipe frame time stamp. The time stamp value
153 		 * is sampled at every start of vertical blank.
154 		 */
155 		scan_prev_time = intel_de_read_fw(display,
156 						  PIPE_FRMTMSTMP(crtc->pipe));
157 
158 		/*
159 		 * The TIMESTAMP_CTR register has the current
160 		 * time stamp value.
161 		 */
162 		scan_curr_time = intel_de_read_fw(display, IVB_TIMESTAMP_CTR);
163 
164 		scan_post_time = intel_de_read_fw(display,
165 						  PIPE_FRMTMSTMP(crtc->pipe));
166 	} while (scan_post_time != scan_prev_time);
167 
168 	return div_u64(mul_u32_u32(scan_curr_time - scan_prev_time,
169 				   clock), 1000 * htotal);
170 }
171 
172 /*
173  * On certain encoders on certain platforms, pipe
174  * scanline register will not work to get the scanline,
175  * since the timings are driven from the PORT or issues
176  * with scanline register updates.
177  * This function will use Framestamp and current
178  * timestamp registers to calculate the scanline.
179  */
__intel_get_crtc_scanline_from_timestamp(struct intel_crtc * crtc)180 static u32 __intel_get_crtc_scanline_from_timestamp(struct intel_crtc *crtc)
181 {
182 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base);
183 	const struct drm_display_mode *mode = &vblank->hwmode;
184 	u32 vblank_start = mode->crtc_vblank_start;
185 	u32 vtotal = mode->crtc_vtotal;
186 	u32 scanline;
187 
188 	scanline = intel_crtc_scanlines_since_frame_timestamp(crtc);
189 	scanline = min(scanline, vtotal - 1);
190 	scanline = (scanline + vblank_start) % vtotal;
191 
192 	return scanline;
193 }
194 
intel_crtc_scanline_offset(const struct intel_crtc_state * crtc_state)195 int intel_crtc_scanline_offset(const struct intel_crtc_state *crtc_state)
196 {
197 	struct intel_display *display = to_intel_display(crtc_state);
198 
199 	/*
200 	 * The scanline counter increments at the leading edge of hsync.
201 	 *
202 	 * On most platforms it starts counting from vtotal-1 on the
203 	 * first active line. That means the scanline counter value is
204 	 * always one less than what we would expect. Ie. just after
205 	 * start of vblank, which also occurs at start of hsync (on the
206 	 * last active line), the scanline counter will read vblank_start-1.
207 	 *
208 	 * On gen2 the scanline counter starts counting from 1 instead
209 	 * of vtotal-1, so we have to subtract one.
210 	 *
211 	 * On HSW+ the behaviour of the scanline counter depends on the output
212 	 * type. For DP ports it behaves like most other platforms, but on HDMI
213 	 * there's an extra 1 line difference. So we need to add two instead of
214 	 * one to the value.
215 	 *
216 	 * On VLV/CHV DSI the scanline counter would appear to increment
217 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
218 	 * that means we can't tell whether we're in vblank or not while
219 	 * we're on that particular line. We must still set scanline_offset
220 	 * to 1 so that the vblank timestamps come out correct when we query
221 	 * the scanline counter from within the vblank interrupt handler.
222 	 * However if queried just before the start of vblank we'll get an
223 	 * answer that's slightly in the future.
224 	 */
225 	if (DISPLAY_VER(display) >= 20 || display->platform.battlemage)
226 		return 1;
227 	else if (DISPLAY_VER(display) == 2)
228 		return -1;
229 	else if (HAS_DDI(display) && intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
230 		return 2;
231 	else
232 		return 1;
233 }
234 
235 /*
236  * intel_de_read_fw(), only for fast reads of display block, no need for
237  * forcewake etc.
238  */
__intel_get_crtc_scanline(struct intel_crtc * crtc)239 static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
240 {
241 	struct intel_display *display = to_intel_display(crtc);
242 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base);
243 	const struct drm_display_mode *mode = &vblank->hwmode;
244 	enum pipe pipe = crtc->pipe;
245 	int position, vtotal;
246 
247 	if (!crtc->active)
248 		return 0;
249 
250 	if (crtc->mode_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
251 		return __intel_get_crtc_scanline_from_timestamp(crtc);
252 
253 	vtotal = intel_mode_vtotal(mode);
254 
255 	position = intel_de_read_fw(display, PIPEDSL(display, pipe)) & PIPEDSL_LINE_MASK;
256 
257 	/*
258 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
259 	 * read it just before the start of vblank.  So try it again
260 	 * so we don't accidentally end up spanning a vblank frame
261 	 * increment, causing the pipe_update_end() code to squak at us.
262 	 *
263 	 * The nature of this problem means we can't simply check the ISR
264 	 * bit and return the vblank start value; nor can we use the scanline
265 	 * debug register in the transcoder as it appears to have the same
266 	 * problem.  We may need to extend this to include other platforms,
267 	 * but so far testing only shows the problem on HSW.
268 	 */
269 	if (HAS_DDI(display) && !position) {
270 		int i, temp;
271 
272 		for (i = 0; i < 100; i++) {
273 			udelay(1);
274 			temp = intel_de_read_fw(display,
275 						PIPEDSL(display, pipe)) & PIPEDSL_LINE_MASK;
276 			if (temp != position) {
277 				position = temp;
278 				break;
279 			}
280 		}
281 	}
282 
283 	/*
284 	 * See update_scanline_offset() for the details on the
285 	 * scanline_offset adjustment.
286 	 */
287 	return (position + vtotal + crtc->scanline_offset) % vtotal;
288 }
289 
290 /*
291  * The uncore version of the spin lock functions is used to decide
292  * whether we need to lock the uncore lock or not.  This is only
293  * needed in i915, not in Xe.
294  *
295  * This lock in i915 is needed because some old platforms (at least
296  * IVB and possibly HSW as well), which are not supported in Xe, need
297  * all register accesses to the same cacheline to be serialized,
298  * otherwise they may hang.
299  */
300 #ifdef I915
intel_vblank_section_enter(struct intel_display * display)301 static void intel_vblank_section_enter(struct intel_display *display)
302 	__acquires(i915->uncore.lock)
303 {
304 	struct drm_i915_private *i915 = to_i915(display->drm);
305 	spin_lock(&i915->uncore.lock);
306 }
307 
intel_vblank_section_exit(struct intel_display * display)308 static void intel_vblank_section_exit(struct intel_display *display)
309 	__releases(i915->uncore.lock)
310 {
311 	struct drm_i915_private *i915 = to_i915(display->drm);
312 	spin_unlock(&i915->uncore.lock);
313 }
314 #else
intel_vblank_section_enter(struct intel_display * display)315 static void intel_vblank_section_enter(struct intel_display *display)
316 {
317 }
318 
intel_vblank_section_exit(struct intel_display * display)319 static void intel_vblank_section_exit(struct intel_display *display)
320 {
321 }
322 #endif
323 
i915_get_crtc_scanoutpos(struct drm_crtc * _crtc,bool in_vblank_irq,int * vpos,int * hpos,ktime_t * stime,ktime_t * etime,const struct drm_display_mode * mode)324 static bool i915_get_crtc_scanoutpos(struct drm_crtc *_crtc,
325 				     bool in_vblank_irq,
326 				     int *vpos, int *hpos,
327 				     ktime_t *stime, ktime_t *etime,
328 				     const struct drm_display_mode *mode)
329 {
330 	struct intel_display *display = to_intel_display(_crtc->dev);
331 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
332 	enum pipe pipe = crtc->pipe;
333 	int position;
334 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
335 	unsigned long irqflags;
336 	bool use_scanline_counter = DISPLAY_VER(display) >= 5 ||
337 		display->platform.g4x || DISPLAY_VER(display) == 2 ||
338 		crtc->mode_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
339 
340 	if (drm_WARN_ON(display->drm, !mode->crtc_clock)) {
341 		drm_dbg(display->drm,
342 			"trying to get scanoutpos for disabled pipe %c\n",
343 			pipe_name(pipe));
344 		return false;
345 	}
346 
347 	htotal = mode->crtc_htotal;
348 	hsync_start = mode->crtc_hsync_start;
349 	vtotal = intel_mode_vtotal(mode);
350 	vbl_start = intel_mode_vblank_start(mode);
351 	vbl_end = intel_mode_vblank_end(mode);
352 
353 	/*
354 	 * Enter vblank critical section, as we will do multiple
355 	 * timing critical raw register reads, potentially with
356 	 * preemption disabled, so the following code must not block.
357 	 */
358 	local_irq_save(irqflags);
359 	intel_vblank_section_enter(display);
360 
361 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
362 
363 	/* Get optional system timestamp before query. */
364 	if (stime)
365 		*stime = ktime_get();
366 
367 	if (crtc->mode_flags & I915_MODE_FLAG_VRR) {
368 		int scanlines = intel_crtc_scanlines_since_frame_timestamp(crtc);
369 
370 		position = __intel_get_crtc_scanline(crtc);
371 
372 		/*
373 		 * Already exiting vblank? If so, shift our position
374 		 * so it looks like we're already apporaching the full
375 		 * vblank end. This should make the generated timestamp
376 		 * more or less match when the active portion will start.
377 		 */
378 		if (position >= vbl_start && scanlines < position)
379 			position = min(crtc->vmax_vblank_start + scanlines, vtotal - 1);
380 	} else if (use_scanline_counter) {
381 		/* No obvious pixelcount register. Only query vertical
382 		 * scanout position from Display scan line register.
383 		 */
384 		position = __intel_get_crtc_scanline(crtc);
385 	} else {
386 		/*
387 		 * Have access to pixelcount since start of frame.
388 		 * We can split this into vertical and horizontal
389 		 * scanout position.
390 		 */
391 		position = (intel_de_read_fw(display, PIPEFRAMEPIXEL(display, pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
392 
393 		/* convert to pixel counts */
394 		vbl_start *= htotal;
395 		vbl_end *= htotal;
396 		vtotal *= htotal;
397 
398 		/*
399 		 * In interlaced modes, the pixel counter counts all pixels,
400 		 * so one field will have htotal more pixels. In order to avoid
401 		 * the reported position from jumping backwards when the pixel
402 		 * counter is beyond the length of the shorter field, just
403 		 * clamp the position the length of the shorter field. This
404 		 * matches how the scanline counter based position works since
405 		 * the scanline counter doesn't count the two half lines.
406 		 */
407 		position = min(position, vtotal - 1);
408 
409 		/*
410 		 * Start of vblank interrupt is triggered at start of hsync,
411 		 * just prior to the first active line of vblank. However we
412 		 * consider lines to start at the leading edge of horizontal
413 		 * active. So, should we get here before we've crossed into
414 		 * the horizontal active of the first line in vblank, we would
415 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
416 		 * always add htotal-hsync_start to the current pixel position.
417 		 */
418 		position = (position + htotal - hsync_start) % vtotal;
419 	}
420 
421 	/* Get optional system timestamp after query. */
422 	if (etime)
423 		*etime = ktime_get();
424 
425 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
426 
427 	intel_vblank_section_exit(display);
428 	local_irq_restore(irqflags);
429 
430 	/*
431 	 * While in vblank, position will be negative
432 	 * counting up towards 0 at vbl_end. And outside
433 	 * vblank, position will be positive counting
434 	 * up since vbl_end.
435 	 */
436 	if (position >= vbl_start)
437 		position -= vbl_end;
438 	else
439 		position += vtotal - vbl_end;
440 
441 	if (use_scanline_counter) {
442 		*vpos = position;
443 		*hpos = 0;
444 	} else {
445 		*vpos = position / htotal;
446 		*hpos = position - (*vpos * htotal);
447 	}
448 
449 	return true;
450 }
451 
intel_crtc_get_vblank_timestamp(struct drm_crtc * crtc,int * max_error,ktime_t * vblank_time,bool in_vblank_irq)452 bool intel_crtc_get_vblank_timestamp(struct drm_crtc *crtc, int *max_error,
453 				     ktime_t *vblank_time, bool in_vblank_irq)
454 {
455 	return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
456 		crtc, max_error, vblank_time, in_vblank_irq,
457 		i915_get_crtc_scanoutpos);
458 }
459 
intel_get_crtc_scanline(struct intel_crtc * crtc)460 int intel_get_crtc_scanline(struct intel_crtc *crtc)
461 {
462 	struct intel_display *display = to_intel_display(crtc);
463 	unsigned long irqflags;
464 	int position;
465 
466 	local_irq_save(irqflags);
467 	intel_vblank_section_enter(display);
468 
469 	position = __intel_get_crtc_scanline(crtc);
470 
471 	intel_vblank_section_exit(display);
472 	local_irq_restore(irqflags);
473 
474 	return position;
475 }
476 
pipe_scanline_is_moving(struct intel_display * display,enum pipe pipe)477 static bool pipe_scanline_is_moving(struct intel_display *display,
478 				    enum pipe pipe)
479 {
480 	i915_reg_t reg = PIPEDSL(display, pipe);
481 	u32 line1, line2;
482 
483 	line1 = intel_de_read(display, reg) & PIPEDSL_LINE_MASK;
484 	msleep(5);
485 	line2 = intel_de_read(display, reg) & PIPEDSL_LINE_MASK;
486 
487 	return line1 != line2;
488 }
489 
wait_for_pipe_scanline_moving(struct intel_crtc * crtc,bool state)490 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
491 {
492 	struct intel_display *display = to_intel_display(crtc);
493 	enum pipe pipe = crtc->pipe;
494 
495 	/* Wait for the display line to settle/start moving */
496 	if (wait_for(pipe_scanline_is_moving(display, pipe) == state, 100))
497 		drm_err(display->drm,
498 			"pipe %c scanline %s wait timed out\n",
499 			pipe_name(pipe), str_on_off(state));
500 }
501 
intel_wait_for_pipe_scanline_stopped(struct intel_crtc * crtc)502 void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
503 {
504 	wait_for_pipe_scanline_moving(crtc, false);
505 }
506 
intel_wait_for_pipe_scanline_moving(struct intel_crtc * crtc)507 void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
508 {
509 	wait_for_pipe_scanline_moving(crtc, true);
510 }
511 
intel_crtc_update_active_timings(const struct intel_crtc_state * crtc_state,bool vrr_enable)512 void intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state,
513 				      bool vrr_enable)
514 {
515 	struct intel_display *display = to_intel_display(crtc_state);
516 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
517 	u8 mode_flags = crtc_state->mode_flags;
518 	struct drm_display_mode adjusted_mode;
519 	int vmax_vblank_start = 0;
520 	unsigned long irqflags;
521 
522 	drm_mode_init(&adjusted_mode, &crtc_state->hw.adjusted_mode);
523 
524 	if (vrr_enable) {
525 		drm_WARN_ON(display->drm,
526 			    (mode_flags & I915_MODE_FLAG_VRR) == 0);
527 
528 		adjusted_mode.crtc_vtotal = crtc_state->vrr.vmax;
529 		adjusted_mode.crtc_vblank_end = crtc_state->vrr.vmax;
530 		adjusted_mode.crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
531 		vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
532 	} else {
533 		mode_flags &= ~I915_MODE_FLAG_VRR;
534 	}
535 
536 	/*
537 	 * Belts and suspenders locking to guarantee everyone sees 100%
538 	 * consistent state during fastset seamless refresh rate changes.
539 	 *
540 	 * vblank_time_lock takes care of all drm_vblank.c stuff, and
541 	 * uncore.lock takes care of __intel_get_crtc_scanline() which
542 	 * may get called elsewhere as well.
543 	 *
544 	 * TODO maybe just protect everything (including
545 	 * __intel_get_crtc_scanline()) with vblank_time_lock?
546 	 * Need to audit everything to make sure it's safe.
547 	 */
548 	spin_lock_irqsave(&display->drm->vblank_time_lock, irqflags);
549 	intel_vblank_section_enter(display);
550 
551 	drm_calc_timestamping_constants(&crtc->base, &adjusted_mode);
552 
553 	crtc->vmax_vblank_start = vmax_vblank_start;
554 
555 	crtc->mode_flags = mode_flags;
556 
557 	crtc->scanline_offset = intel_crtc_scanline_offset(crtc_state);
558 	intel_vblank_section_exit(display);
559 	spin_unlock_irqrestore(&display->drm->vblank_time_lock, irqflags);
560 }
561 
intel_mode_vdisplay(const struct drm_display_mode * mode)562 int intel_mode_vdisplay(const struct drm_display_mode *mode)
563 {
564 	int vdisplay = mode->crtc_vdisplay;
565 
566 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
567 		vdisplay = DIV_ROUND_UP(vdisplay, 2);
568 
569 	return vdisplay;
570 }
571 
intel_mode_vblank_start(const struct drm_display_mode * mode)572 int intel_mode_vblank_start(const struct drm_display_mode *mode)
573 {
574 	int vblank_start = mode->crtc_vblank_start;
575 
576 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
577 		vblank_start = DIV_ROUND_UP(vblank_start, 2);
578 
579 	return vblank_start;
580 }
581 
intel_mode_vblank_end(const struct drm_display_mode * mode)582 int intel_mode_vblank_end(const struct drm_display_mode *mode)
583 {
584 	int vblank_end = mode->crtc_vblank_end;
585 
586 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
587 		vblank_end /= 2;
588 
589 	return vblank_end;
590 }
591 
intel_mode_vtotal(const struct drm_display_mode * mode)592 int intel_mode_vtotal(const struct drm_display_mode *mode)
593 {
594 	int vtotal = mode->crtc_vtotal;
595 
596 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
597 		vtotal /= 2;
598 
599 	return vtotal;
600 }
601 
intel_vblank_evade_init(const struct intel_crtc_state * old_crtc_state,const struct intel_crtc_state * new_crtc_state,struct intel_vblank_evade_ctx * evade)602 void intel_vblank_evade_init(const struct intel_crtc_state *old_crtc_state,
603 			     const struct intel_crtc_state *new_crtc_state,
604 			     struct intel_vblank_evade_ctx *evade)
605 {
606 	struct intel_display *display = to_intel_display(new_crtc_state);
607 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
608 	const struct intel_crtc_state *crtc_state;
609 	const struct drm_display_mode *adjusted_mode;
610 
611 	evade->crtc = crtc;
612 
613 	evade->need_vlv_dsi_wa = (display->platform.valleyview ||
614 				  display->platform.cherryview) &&
615 		intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
616 
617 	/*
618 	 * During fastsets/etc. the transcoder is still
619 	 * running with the old timings at this point.
620 	 *
621 	 * TODO: maybe just use the active timings here?
622 	 */
623 	if (intel_crtc_needs_modeset(new_crtc_state))
624 		crtc_state = new_crtc_state;
625 	else
626 		crtc_state = old_crtc_state;
627 
628 	adjusted_mode = &crtc_state->hw.adjusted_mode;
629 
630 	if (crtc->mode_flags & I915_MODE_FLAG_VRR) {
631 		/* timing changes should happen with VRR disabled */
632 		drm_WARN_ON(crtc->base.dev, intel_crtc_needs_modeset(new_crtc_state) ||
633 			    new_crtc_state->update_m_n || new_crtc_state->update_lrr);
634 
635 		if (intel_vrr_is_push_sent(crtc_state))
636 			evade->vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
637 		else
638 			evade->vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
639 	} else {
640 		evade->vblank_start = intel_mode_vblank_start(adjusted_mode);
641 	}
642 
643 	/* FIXME needs to be calibrated sensibly */
644 	evade->min = evade->vblank_start - intel_usecs_to_scanlines(adjusted_mode,
645 								    VBLANK_EVASION_TIME_US);
646 	evade->max = evade->vblank_start - 1;
647 
648 	/*
649 	 * M/N and TRANS_VTOTAL are double buffered on the transcoder's
650 	 * undelayed vblank, so with seamless M/N and LRR we must evade
651 	 * both vblanks.
652 	 *
653 	 * DSB execution waits for the transcoder's undelayed vblank,
654 	 * hence we must kick off the commit before that.
655 	 */
656 	if (intel_color_uses_dsb(new_crtc_state) ||
657 	    new_crtc_state->update_m_n || new_crtc_state->update_lrr)
658 		evade->min -= intel_mode_vblank_start(adjusted_mode) -
659 			intel_mode_vdisplay(adjusted_mode);
660 }
661 
662 /* must be called with vblank interrupt already enabled! */
intel_vblank_evade(struct intel_vblank_evade_ctx * evade)663 int intel_vblank_evade(struct intel_vblank_evade_ctx *evade)
664 {
665 	struct intel_crtc *crtc = evade->crtc;
666 	struct intel_display *display = to_intel_display(crtc);
667 	long timeout = msecs_to_jiffies_timeout(1);
668 	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
669 	DEFINE_WAIT(wait);
670 	int scanline;
671 
672 	if (evade->min <= 0 || evade->max <= 0)
673 		return 0;
674 
675 	for (;;) {
676 		/*
677 		 * prepare_to_wait() has a memory barrier, which guarantees
678 		 * other CPUs can see the task state update by the time we
679 		 * read the scanline.
680 		 */
681 		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
682 
683 		scanline = intel_get_crtc_scanline(crtc);
684 		if (scanline < evade->min || scanline > evade->max)
685 			break;
686 
687 		if (!timeout) {
688 			drm_err(display->drm,
689 				"Potential atomic update failure on pipe %c\n",
690 				pipe_name(crtc->pipe));
691 			break;
692 		}
693 
694 		local_irq_enable();
695 
696 		timeout = schedule_timeout(timeout);
697 
698 		local_irq_disable();
699 	}
700 
701 	finish_wait(wq, &wait);
702 
703 	/*
704 	 * On VLV/CHV DSI the scanline counter would appear to
705 	 * increment approx. 1/3 of a scanline before start of vblank.
706 	 * The registers still get latched at start of vblank however.
707 	 * This means we must not write any registers on the first
708 	 * line of vblank (since not the whole line is actually in
709 	 * vblank). And unfortunately we can't use the interrupt to
710 	 * wait here since it will fire too soon. We could use the
711 	 * frame start interrupt instead since it will fire after the
712 	 * critical scanline, but that would require more changes
713 	 * in the interrupt code. So for now we'll just do the nasty
714 	 * thing and poll for the bad scanline to pass us by.
715 	 *
716 	 * FIXME figure out if BXT+ DSI suffers from this as well
717 	 */
718 	while (evade->need_vlv_dsi_wa && scanline == evade->vblank_start)
719 		scanline = intel_get_crtc_scanline(crtc);
720 
721 	return scanline;
722 }
723