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