xref: /linux/drivers/gpu/drm/i915/display/intel_vblank.c (revision 785151f50ddacac06c7a3c5f3d31642794507fdf)
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) >= 9 ||
228 		 display->platform.broadwell || display->platform.haswell)
229 		return intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI) ? 2 : 1;
230 	else if (DISPLAY_VER(display) >= 3)
231 		return 1;
232 	else
233 		return -1;
234 }
235 
236 /*
237  * intel_de_read_fw(), only for fast reads of display block, no need for
238  * forcewake etc.
239  */
__intel_get_crtc_scanline(struct intel_crtc * crtc)240 static int __intel_get_crtc_scanline(struct intel_crtc *crtc)
241 {
242 	struct intel_display *display = to_intel_display(crtc);
243 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base);
244 	const struct drm_display_mode *mode = &vblank->hwmode;
245 	enum pipe pipe = crtc->pipe;
246 	int position, vtotal;
247 
248 	if (!crtc->active)
249 		return 0;
250 
251 	if (crtc->mode_flags & I915_MODE_FLAG_GET_SCANLINE_FROM_TIMESTAMP)
252 		return __intel_get_crtc_scanline_from_timestamp(crtc);
253 
254 	vtotal = intel_mode_vtotal(mode);
255 
256 	position = intel_de_read_fw(display, PIPEDSL(display, pipe)) & PIPEDSL_LINE_MASK;
257 
258 	/*
259 	 * On HSW, the DSL reg (0x70000) appears to return 0 if we
260 	 * read it just before the start of vblank.  So try it again
261 	 * so we don't accidentally end up spanning a vblank frame
262 	 * increment, causing the pipe_update_end() code to squak at us.
263 	 *
264 	 * The nature of this problem means we can't simply check the ISR
265 	 * bit and return the vblank start value; nor can we use the scanline
266 	 * debug register in the transcoder as it appears to have the same
267 	 * problem.  We may need to extend this to include other platforms,
268 	 * but so far testing only shows the problem on HSW.
269 	 */
270 	if (HAS_DDI(display) && !position) {
271 		int i, temp;
272 
273 		for (i = 0; i < 100; i++) {
274 			udelay(1);
275 			temp = intel_de_read_fw(display,
276 						PIPEDSL(display, pipe)) & PIPEDSL_LINE_MASK;
277 			if (temp != position) {
278 				position = temp;
279 				break;
280 			}
281 		}
282 	}
283 
284 	/*
285 	 * See update_scanline_offset() for the details on the
286 	 * scanline_offset adjustment.
287 	 */
288 	return (position + vtotal + crtc->scanline_offset) % vtotal;
289 }
290 
291 /*
292  * The uncore version of the spin lock functions is used to decide
293  * whether we need to lock the uncore lock or not.  This is only
294  * needed in i915, not in Xe.
295  *
296  * This lock in i915 is needed because some old platforms (at least
297  * IVB and possibly HSW as well), which are not supported in Xe, need
298  * all register accesses to the same cacheline to be serialized,
299  * otherwise they may hang.
300  */
301 #ifdef I915
intel_vblank_section_enter(struct intel_display * display)302 static void intel_vblank_section_enter(struct intel_display *display)
303 	__acquires(i915->uncore.lock)
304 {
305 	struct drm_i915_private *i915 = to_i915(display->drm);
306 	spin_lock(&i915->uncore.lock);
307 }
308 
intel_vblank_section_exit(struct intel_display * display)309 static void intel_vblank_section_exit(struct intel_display *display)
310 	__releases(i915->uncore.lock)
311 {
312 	struct drm_i915_private *i915 = to_i915(display->drm);
313 	spin_unlock(&i915->uncore.lock);
314 }
315 #else
intel_vblank_section_enter(struct intel_display * display)316 static void intel_vblank_section_enter(struct intel_display *display)
317 {
318 }
319 
intel_vblank_section_exit(struct intel_display * display)320 static void intel_vblank_section_exit(struct intel_display *display)
321 {
322 }
323 #endif
324 
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)325 static bool i915_get_crtc_scanoutpos(struct drm_crtc *_crtc,
326 				     bool in_vblank_irq,
327 				     int *vpos, int *hpos,
328 				     ktime_t *stime, ktime_t *etime,
329 				     const struct drm_display_mode *mode)
330 {
331 	struct intel_display *display = to_intel_display(_crtc->dev);
332 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
333 	enum pipe pipe = crtc->pipe;
334 	int position;
335 	int vbl_start, vbl_end, hsync_start, htotal, vtotal;
336 	unsigned long irqflags;
337 	bool use_scanline_counter = DISPLAY_VER(display) >= 5 ||
338 		display->platform.g4x || DISPLAY_VER(display) == 2 ||
339 		crtc->mode_flags & I915_MODE_FLAG_USE_SCANLINE_COUNTER;
340 
341 	if (drm_WARN_ON(display->drm, !mode->crtc_clock)) {
342 		drm_dbg(display->drm,
343 			"trying to get scanoutpos for disabled pipe %c\n",
344 			pipe_name(pipe));
345 		return false;
346 	}
347 
348 	htotal = mode->crtc_htotal;
349 	hsync_start = mode->crtc_hsync_start;
350 	vtotal = intel_mode_vtotal(mode);
351 	vbl_start = intel_mode_vblank_start(mode);
352 	vbl_end = intel_mode_vblank_end(mode);
353 
354 	/*
355 	 * Enter vblank critical section, as we will do multiple
356 	 * timing critical raw register reads, potentially with
357 	 * preemption disabled, so the following code must not block.
358 	 */
359 	local_irq_save(irqflags);
360 	intel_vblank_section_enter(display);
361 
362 	/* preempt_disable_rt() should go right here in PREEMPT_RT patchset. */
363 
364 	/* Get optional system timestamp before query. */
365 	if (stime)
366 		*stime = ktime_get();
367 
368 	if (crtc->mode_flags & I915_MODE_FLAG_VRR) {
369 		int scanlines = intel_crtc_scanlines_since_frame_timestamp(crtc);
370 
371 		position = __intel_get_crtc_scanline(crtc);
372 
373 		/*
374 		 * Already exiting vblank? If so, shift our position
375 		 * so it looks like we're already approaching the full
376 		 * vblank end. This should make the generated timestamp
377 		 * more or less match when the active portion will start.
378 		 */
379 		if (position >= vbl_start && scanlines < position)
380 			position = min(crtc->vmax_vblank_start + scanlines, vtotal - 1);
381 	} else if (use_scanline_counter) {
382 		/* No obvious pixelcount register. Only query vertical
383 		 * scanout position from Display scan line register.
384 		 */
385 		position = __intel_get_crtc_scanline(crtc);
386 	} else {
387 		/*
388 		 * Have access to pixelcount since start of frame.
389 		 * We can split this into vertical and horizontal
390 		 * scanout position.
391 		 */
392 		position = (intel_de_read_fw(display, PIPEFRAMEPIXEL(display, pipe)) & PIPE_PIXEL_MASK) >> PIPE_PIXEL_SHIFT;
393 
394 		/* convert to pixel counts */
395 		vbl_start *= htotal;
396 		vbl_end *= htotal;
397 		vtotal *= htotal;
398 
399 		/*
400 		 * In interlaced modes, the pixel counter counts all pixels,
401 		 * so one field will have htotal more pixels. In order to avoid
402 		 * the reported position from jumping backwards when the pixel
403 		 * counter is beyond the length of the shorter field, just
404 		 * clamp the position the length of the shorter field. This
405 		 * matches how the scanline counter based position works since
406 		 * the scanline counter doesn't count the two half lines.
407 		 */
408 		position = min(position, vtotal - 1);
409 
410 		/*
411 		 * Start of vblank interrupt is triggered at start of hsync,
412 		 * just prior to the first active line of vblank. However we
413 		 * consider lines to start at the leading edge of horizontal
414 		 * active. So, should we get here before we've crossed into
415 		 * the horizontal active of the first line in vblank, we would
416 		 * not set the DRM_SCANOUTPOS_INVBL flag. In order to fix that,
417 		 * always add htotal-hsync_start to the current pixel position.
418 		 */
419 		position = (position + htotal - hsync_start) % vtotal;
420 	}
421 
422 	/* Get optional system timestamp after query. */
423 	if (etime)
424 		*etime = ktime_get();
425 
426 	/* preempt_enable_rt() should go right here in PREEMPT_RT patchset. */
427 
428 	intel_vblank_section_exit(display);
429 	local_irq_restore(irqflags);
430 
431 	/*
432 	 * While in vblank, position will be negative
433 	 * counting up towards 0 at vbl_end. And outside
434 	 * vblank, position will be positive counting
435 	 * up since vbl_end.
436 	 */
437 	if (position >= vbl_start)
438 		position -= vbl_end;
439 	else
440 		position += vtotal - vbl_end;
441 
442 	if (use_scanline_counter) {
443 		*vpos = position;
444 		*hpos = 0;
445 	} else {
446 		*vpos = position / htotal;
447 		*hpos = position - (*vpos * htotal);
448 	}
449 
450 	return true;
451 }
452 
intel_crtc_get_vblank_timestamp(struct drm_crtc * crtc,int * max_error,ktime_t * vblank_time,bool in_vblank_irq)453 bool intel_crtc_get_vblank_timestamp(struct drm_crtc *crtc, int *max_error,
454 				     ktime_t *vblank_time, bool in_vblank_irq)
455 {
456 	return drm_crtc_vblank_helper_get_vblank_timestamp_internal(
457 		crtc, max_error, vblank_time, in_vblank_irq,
458 		i915_get_crtc_scanoutpos);
459 }
460 
intel_get_crtc_scanline(struct intel_crtc * crtc)461 int intel_get_crtc_scanline(struct intel_crtc *crtc)
462 {
463 	struct intel_display *display = to_intel_display(crtc);
464 	unsigned long irqflags;
465 	int position;
466 
467 	local_irq_save(irqflags);
468 	intel_vblank_section_enter(display);
469 
470 	position = __intel_get_crtc_scanline(crtc);
471 
472 	intel_vblank_section_exit(display);
473 	local_irq_restore(irqflags);
474 
475 	return position;
476 }
477 
pipe_scanline_is_moving(struct intel_display * display,enum pipe pipe)478 static bool pipe_scanline_is_moving(struct intel_display *display,
479 				    enum pipe pipe)
480 {
481 	i915_reg_t reg = PIPEDSL(display, pipe);
482 	u32 line1, line2;
483 
484 	line1 = intel_de_read(display, reg) & PIPEDSL_LINE_MASK;
485 	msleep(5);
486 	line2 = intel_de_read(display, reg) & PIPEDSL_LINE_MASK;
487 
488 	return line1 != line2;
489 }
490 
wait_for_pipe_scanline_moving(struct intel_crtc * crtc,bool state)491 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
492 {
493 	struct intel_display *display = to_intel_display(crtc);
494 	enum pipe pipe = crtc->pipe;
495 
496 	/* Wait for the display line to settle/start moving */
497 	if (wait_for(pipe_scanline_is_moving(display, pipe) == state, 100))
498 		drm_err(display->drm,
499 			"pipe %c scanline %s wait timed out\n",
500 			pipe_name(pipe), str_on_off(state));
501 }
502 
intel_wait_for_pipe_scanline_stopped(struct intel_crtc * crtc)503 void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
504 {
505 	wait_for_pipe_scanline_moving(crtc, false);
506 }
507 
intel_wait_for_pipe_scanline_moving(struct intel_crtc * crtc)508 void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
509 {
510 	wait_for_pipe_scanline_moving(crtc, true);
511 }
512 
intel_crtc_active_timings(struct drm_display_mode * mode,int * vmax_vblank_start,const struct intel_crtc_state * crtc_state,bool vrr_enable)513 static void intel_crtc_active_timings(struct drm_display_mode *mode,
514 				      int *vmax_vblank_start,
515 				      const struct intel_crtc_state *crtc_state,
516 				      bool vrr_enable)
517 {
518 	drm_mode_init(mode, &crtc_state->hw.adjusted_mode);
519 	*vmax_vblank_start = 0;
520 
521 	if (!vrr_enable)
522 		return;
523 
524 	mode->crtc_vtotal = intel_vrr_vmax_vtotal(crtc_state);
525 	mode->crtc_vblank_end = intel_vrr_vmax_vtotal(crtc_state);
526 	mode->crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
527 	*vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
528 }
529 
intel_crtc_update_active_timings(const struct intel_crtc_state * crtc_state,bool vrr_enable)530 void intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state,
531 				      bool vrr_enable)
532 {
533 	struct intel_display *display = to_intel_display(crtc_state);
534 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
535 	u8 mode_flags = crtc_state->mode_flags;
536 	struct drm_display_mode adjusted_mode;
537 	int vmax_vblank_start = 0;
538 	unsigned long irqflags;
539 
540 	intel_crtc_active_timings(&adjusted_mode, &vmax_vblank_start,
541 				  crtc_state, vrr_enable);
542 
543 	if (vrr_enable)
544 		drm_WARN_ON(display->drm, (mode_flags & I915_MODE_FLAG_VRR) == 0);
545 	else
546 		mode_flags &= ~I915_MODE_FLAG_VRR;
547 
548 	/*
549 	 * Belts and suspenders locking to guarantee everyone sees 100%
550 	 * consistent state during fastset seamless refresh rate changes.
551 	 *
552 	 * vblank_time_lock takes care of all drm_vblank.c stuff, and
553 	 * uncore.lock takes care of __intel_get_crtc_scanline() which
554 	 * may get called elsewhere as well.
555 	 *
556 	 * TODO maybe just protect everything (including
557 	 * __intel_get_crtc_scanline()) with vblank_time_lock?
558 	 * Need to audit everything to make sure it's safe.
559 	 */
560 	spin_lock_irqsave(&display->drm->vblank_time_lock, irqflags);
561 	intel_vblank_section_enter(display);
562 
563 	drm_calc_timestamping_constants(&crtc->base, &adjusted_mode);
564 
565 	crtc->vmax_vblank_start = vmax_vblank_start;
566 
567 	crtc->mode_flags = mode_flags;
568 
569 	crtc->scanline_offset = intel_crtc_scanline_offset(crtc_state);
570 	intel_vblank_section_exit(display);
571 	spin_unlock_irqrestore(&display->drm->vblank_time_lock, irqflags);
572 }
573 
intel_mode_vdisplay(const struct drm_display_mode * mode)574 int intel_mode_vdisplay(const struct drm_display_mode *mode)
575 {
576 	int vdisplay = mode->crtc_vdisplay;
577 
578 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
579 		vdisplay = DIV_ROUND_UP(vdisplay, 2);
580 
581 	return vdisplay;
582 }
583 
intel_mode_vblank_start(const struct drm_display_mode * mode)584 int intel_mode_vblank_start(const struct drm_display_mode *mode)
585 {
586 	int vblank_start = mode->crtc_vblank_start;
587 
588 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
589 		vblank_start = DIV_ROUND_UP(vblank_start, 2);
590 
591 	return vblank_start;
592 }
593 
intel_mode_vblank_end(const struct drm_display_mode * mode)594 int intel_mode_vblank_end(const struct drm_display_mode *mode)
595 {
596 	int vblank_end = mode->crtc_vblank_end;
597 
598 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
599 		vblank_end /= 2;
600 
601 	return vblank_end;
602 }
603 
intel_mode_vtotal(const struct drm_display_mode * mode)604 int intel_mode_vtotal(const struct drm_display_mode *mode)
605 {
606 	int vtotal = mode->crtc_vtotal;
607 
608 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
609 		vtotal /= 2;
610 
611 	return vtotal;
612 }
613 
intel_mode_vblank_delay(const struct drm_display_mode * mode)614 int intel_mode_vblank_delay(const struct drm_display_mode *mode)
615 {
616 	return intel_mode_vblank_start(mode) - intel_mode_vdisplay(mode);
617 }
618 
619 static const struct intel_crtc_state *
pre_commit_crtc_state(const struct intel_crtc_state * old_crtc_state,const struct intel_crtc_state * new_crtc_state)620 pre_commit_crtc_state(const struct intel_crtc_state *old_crtc_state,
621 		      const struct intel_crtc_state *new_crtc_state)
622 {
623 	/*
624 	 * During fastsets/etc. the transcoder is still
625 	 * running with the old timings at this point.
626 	 */
627 	if (intel_crtc_needs_modeset(new_crtc_state))
628 		return new_crtc_state;
629 	else
630 		return old_crtc_state;
631 }
632 
633 const struct intel_crtc_state *
intel_pre_commit_crtc_state(struct intel_atomic_state * state,struct intel_crtc * crtc)634 intel_pre_commit_crtc_state(struct intel_atomic_state *state,
635 			    struct intel_crtc *crtc)
636 {
637 	const struct intel_crtc_state *old_crtc_state =
638 		intel_atomic_get_old_crtc_state(state, crtc);
639 	const struct intel_crtc_state *new_crtc_state =
640 		intel_atomic_get_new_crtc_state(state, crtc);
641 
642 	return pre_commit_crtc_state(old_crtc_state, new_crtc_state);
643 }
644 
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)645 void intel_vblank_evade_init(const struct intel_crtc_state *old_crtc_state,
646 			     const struct intel_crtc_state *new_crtc_state,
647 			     struct intel_vblank_evade_ctx *evade)
648 {
649 	struct intel_display *display = to_intel_display(new_crtc_state);
650 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
651 	const struct intel_crtc_state *crtc_state;
652 	const struct drm_display_mode *adjusted_mode;
653 	int vblank_delay;
654 
655 	evade->crtc = crtc;
656 
657 	evade->need_vlv_dsi_wa = (display->platform.valleyview ||
658 				  display->platform.cherryview) &&
659 		intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
660 
661 	/* TODO: maybe just use the active timings here? */
662 	crtc_state = pre_commit_crtc_state(old_crtc_state, new_crtc_state);
663 
664 	adjusted_mode = &crtc_state->hw.adjusted_mode;
665 
666 	if (crtc->mode_flags & I915_MODE_FLAG_VRR) {
667 		/* timing changes should happen with VRR disabled */
668 		drm_WARN_ON(crtc->base.dev, intel_crtc_needs_modeset(new_crtc_state) ||
669 			    new_crtc_state->update_m_n || new_crtc_state->update_lrr);
670 
671 		if (intel_vrr_is_push_sent(crtc_state))
672 			evade->vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
673 		else
674 			evade->vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
675 
676 		vblank_delay = intel_vrr_vblank_delay(crtc_state);
677 	} else {
678 		evade->vblank_start = intel_mode_vblank_start(adjusted_mode);
679 
680 		vblank_delay = intel_mode_vblank_delay(adjusted_mode);
681 	}
682 
683 	/* FIXME needs to be calibrated sensibly */
684 	evade->min = evade->vblank_start - intel_usecs_to_scanlines(adjusted_mode,
685 								    VBLANK_EVASION_TIME_US);
686 	evade->max = evade->vblank_start - 1;
687 
688 	/*
689 	 * M/N and TRANS_VTOTAL are double buffered on the transcoder's
690 	 * undelayed vblank, so with seamless M/N and LRR we must evade
691 	 * both vblanks.
692 	 *
693 	 * DSB execution waits for the transcoder's undelayed vblank,
694 	 * hence we must kick off the commit before that.
695 	 */
696 	if (intel_color_uses_dsb(new_crtc_state) ||
697 	    new_crtc_state->update_m_n || new_crtc_state->update_lrr)
698 		evade->min -= vblank_delay;
699 }
700 
701 /* must be called with vblank interrupt already enabled! */
intel_vblank_evade(struct intel_vblank_evade_ctx * evade)702 int intel_vblank_evade(struct intel_vblank_evade_ctx *evade)
703 {
704 	struct intel_crtc *crtc = evade->crtc;
705 	struct intel_display *display = to_intel_display(crtc);
706 	long timeout = msecs_to_jiffies_timeout(1);
707 	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
708 	DEFINE_WAIT(wait);
709 	int scanline;
710 
711 	if (evade->min <= 0 || evade->max <= 0)
712 		return 0;
713 
714 	for (;;) {
715 		/*
716 		 * prepare_to_wait() has a memory barrier, which guarantees
717 		 * other CPUs can see the task state update by the time we
718 		 * read the scanline.
719 		 */
720 		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
721 
722 		scanline = intel_get_crtc_scanline(crtc);
723 		if (scanline < evade->min || scanline > evade->max)
724 			break;
725 
726 		if (!timeout) {
727 			drm_err(display->drm,
728 				"Potential atomic update failure on pipe %c\n",
729 				pipe_name(crtc->pipe));
730 			break;
731 		}
732 
733 		local_irq_enable();
734 
735 		timeout = schedule_timeout(timeout);
736 
737 		local_irq_disable();
738 	}
739 
740 	finish_wait(wq, &wait);
741 
742 	/*
743 	 * On VLV/CHV DSI the scanline counter would appear to
744 	 * increment approx. 1/3 of a scanline before start of vblank.
745 	 * The registers still get latched at start of vblank however.
746 	 * This means we must not write any registers on the first
747 	 * line of vblank (since not the whole line is actually in
748 	 * vblank). And unfortunately we can't use the interrupt to
749 	 * wait here since it will fire too soon. We could use the
750 	 * frame start interrupt instead since it will fire after the
751 	 * critical scanline, but that would require more changes
752 	 * in the interrupt code. So for now we'll just do the nasty
753 	 * thing and poll for the bad scanline to pass us by.
754 	 *
755 	 * FIXME figure out if BXT+ DSI suffers from this as well
756 	 */
757 	while (evade->need_vlv_dsi_wa && scanline == evade->vblank_start)
758 		scanline = intel_get_crtc_scanline(crtc);
759 
760 	return scanline;
761 }
762