1 // SPDX-License-Identifier: MIT
2 /*
3  * Copyright © 2020 Intel Corporation
4  */
5 #include <linux/kernel.h>
6 #include <linux/pm_qos.h>
7 #include <linux/slab.h>
8 
9 #include <drm/drm_atomic_helper.h>
10 #include <drm/drm_fourcc.h>
11 #include <drm/drm_plane.h>
12 #include <drm/drm_plane_helper.h>
13 #include <drm/drm_vblank_work.h>
14 
15 #include "i915_vgpu.h"
16 #include "i9xx_plane.h"
17 #include "icl_dsi.h"
18 #include "intel_atomic.h"
19 #include "intel_atomic_plane.h"
20 #include "intel_color.h"
21 #include "intel_crtc.h"
22 #include "intel_cursor.h"
23 #include "intel_display_debugfs.h"
24 #include "intel_display_trace.h"
25 #include "intel_display_types.h"
26 #include "intel_dsi.h"
27 #include "intel_pipe_crc.h"
28 #include "intel_psr.h"
29 #include "intel_sprite.h"
30 #include "intel_vrr.h"
31 #include "skl_universal_plane.h"
32 
33 static void assert_vblank_disabled(struct drm_crtc *crtc)
34 {
35 	if (I915_STATE_WARN_ON(drm_crtc_vblank_get(crtc) == 0))
36 		drm_crtc_vblank_put(crtc);
37 }
38 
39 struct intel_crtc *intel_first_crtc(struct drm_i915_private *i915)
40 {
41 	return to_intel_crtc(drm_crtc_from_index(&i915->drm, 0));
42 }
43 
44 struct intel_crtc *intel_crtc_for_pipe(struct drm_i915_private *i915,
45 				       enum pipe pipe)
46 {
47 	struct intel_crtc *crtc;
48 
49 	for_each_intel_crtc(&i915->drm, crtc) {
50 		if (crtc->pipe == pipe)
51 			return crtc;
52 	}
53 
54 	return NULL;
55 }
56 
57 void intel_crtc_wait_for_next_vblank(struct intel_crtc *crtc)
58 {
59 	drm_crtc_wait_one_vblank(&crtc->base);
60 }
61 
62 void intel_wait_for_vblank_if_active(struct drm_i915_private *i915,
63 				     enum pipe pipe)
64 {
65 	struct intel_crtc *crtc = intel_crtc_for_pipe(i915, pipe);
66 
67 	if (crtc->active)
68 		intel_crtc_wait_for_next_vblank(crtc);
69 }
70 
71 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
72 {
73 	struct drm_device *dev = crtc->base.dev;
74 	struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
75 
76 	if (!crtc->active)
77 		return 0;
78 
79 	if (!vblank->max_vblank_count)
80 		return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
81 
82 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
83 }
84 
85 u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
86 {
87 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
88 
89 	/*
90 	 * From Gen 11, In case of dsi cmd mode, frame counter wouldnt
91 	 * have updated at the beginning of TE, if we want to use
92 	 * the hw counter, then we would find it updated in only
93 	 * the next TE, hence switching to sw counter.
94 	 */
95 	if (crtc_state->mode_flags & (I915_MODE_FLAG_DSI_USE_TE0 |
96 				      I915_MODE_FLAG_DSI_USE_TE1))
97 		return 0;
98 
99 	/*
100 	 * On i965gm the hardware frame counter reads
101 	 * zero when the TV encoder is enabled :(
102 	 */
103 	if (IS_I965GM(dev_priv) &&
104 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
105 		return 0;
106 
107 	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
108 		return 0xffffffff; /* full 32 bit counter */
109 	else if (DISPLAY_VER(dev_priv) >= 3)
110 		return 0xffffff; /* only 24 bits of frame count */
111 	else
112 		return 0; /* Gen2 doesn't have a hardware frame counter */
113 }
114 
115 void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
116 {
117 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
118 
119 	assert_vblank_disabled(&crtc->base);
120 	drm_crtc_set_max_vblank_count(&crtc->base,
121 				      intel_crtc_max_vblank_count(crtc_state));
122 	drm_crtc_vblank_on(&crtc->base);
123 
124 	/*
125 	 * Should really happen exactly when we enable the pipe
126 	 * but we want the frame counters in the trace, and that
127 	 * requires vblank support on some platforms/outputs.
128 	 */
129 	trace_intel_pipe_enable(crtc);
130 }
131 
132 void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
133 {
134 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
135 
136 	/*
137 	 * Should really happen exactly when we disable the pipe
138 	 * but we want the frame counters in the trace, and that
139 	 * requires vblank support on some platforms/outputs.
140 	 */
141 	trace_intel_pipe_disable(crtc);
142 
143 	drm_crtc_vblank_off(&crtc->base);
144 	assert_vblank_disabled(&crtc->base);
145 }
146 
147 struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
148 {
149 	struct intel_crtc_state *crtc_state;
150 
151 	crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
152 
153 	if (crtc_state)
154 		intel_crtc_state_reset(crtc_state, crtc);
155 
156 	return crtc_state;
157 }
158 
159 void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
160 			    struct intel_crtc *crtc)
161 {
162 	memset(crtc_state, 0, sizeof(*crtc_state));
163 
164 	__drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
165 
166 	crtc_state->cpu_transcoder = INVALID_TRANSCODER;
167 	crtc_state->master_transcoder = INVALID_TRANSCODER;
168 	crtc_state->hsw_workaround_pipe = INVALID_PIPE;
169 	crtc_state->scaler_state.scaler_id = -1;
170 	crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
171 }
172 
173 static struct intel_crtc *intel_crtc_alloc(void)
174 {
175 	struct intel_crtc_state *crtc_state;
176 	struct intel_crtc *crtc;
177 
178 	crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
179 	if (!crtc)
180 		return ERR_PTR(-ENOMEM);
181 
182 	crtc_state = intel_crtc_state_alloc(crtc);
183 	if (!crtc_state) {
184 		kfree(crtc);
185 		return ERR_PTR(-ENOMEM);
186 	}
187 
188 	crtc->base.state = &crtc_state->uapi;
189 	crtc->config = crtc_state;
190 
191 	return crtc;
192 }
193 
194 static void intel_crtc_free(struct intel_crtc *crtc)
195 {
196 	intel_crtc_destroy_state(&crtc->base, crtc->base.state);
197 	kfree(crtc);
198 }
199 
200 static void intel_crtc_destroy(struct drm_crtc *_crtc)
201 {
202 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
203 
204 	cpu_latency_qos_remove_request(&crtc->vblank_pm_qos);
205 
206 	drm_crtc_cleanup(&crtc->base);
207 	kfree(crtc);
208 }
209 
210 static int intel_crtc_late_register(struct drm_crtc *crtc)
211 {
212 	intel_crtc_debugfs_add(crtc);
213 	return 0;
214 }
215 
216 #define INTEL_CRTC_FUNCS \
217 	.set_config = drm_atomic_helper_set_config, \
218 	.destroy = intel_crtc_destroy, \
219 	.page_flip = drm_atomic_helper_page_flip, \
220 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
221 	.atomic_destroy_state = intel_crtc_destroy_state, \
222 	.set_crc_source = intel_crtc_set_crc_source, \
223 	.verify_crc_source = intel_crtc_verify_crc_source, \
224 	.get_crc_sources = intel_crtc_get_crc_sources, \
225 	.late_register = intel_crtc_late_register
226 
227 static const struct drm_crtc_funcs bdw_crtc_funcs = {
228 	INTEL_CRTC_FUNCS,
229 
230 	.get_vblank_counter = g4x_get_vblank_counter,
231 	.enable_vblank = bdw_enable_vblank,
232 	.disable_vblank = bdw_disable_vblank,
233 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
234 };
235 
236 static const struct drm_crtc_funcs ilk_crtc_funcs = {
237 	INTEL_CRTC_FUNCS,
238 
239 	.get_vblank_counter = g4x_get_vblank_counter,
240 	.enable_vblank = ilk_enable_vblank,
241 	.disable_vblank = ilk_disable_vblank,
242 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
243 };
244 
245 static const struct drm_crtc_funcs g4x_crtc_funcs = {
246 	INTEL_CRTC_FUNCS,
247 
248 	.get_vblank_counter = g4x_get_vblank_counter,
249 	.enable_vblank = i965_enable_vblank,
250 	.disable_vblank = i965_disable_vblank,
251 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
252 };
253 
254 static const struct drm_crtc_funcs i965_crtc_funcs = {
255 	INTEL_CRTC_FUNCS,
256 
257 	.get_vblank_counter = i915_get_vblank_counter,
258 	.enable_vblank = i965_enable_vblank,
259 	.disable_vblank = i965_disable_vblank,
260 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
261 };
262 
263 static const struct drm_crtc_funcs i915gm_crtc_funcs = {
264 	INTEL_CRTC_FUNCS,
265 
266 	.get_vblank_counter = i915_get_vblank_counter,
267 	.enable_vblank = i915gm_enable_vblank,
268 	.disable_vblank = i915gm_disable_vblank,
269 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
270 };
271 
272 static const struct drm_crtc_funcs i915_crtc_funcs = {
273 	INTEL_CRTC_FUNCS,
274 
275 	.get_vblank_counter = i915_get_vblank_counter,
276 	.enable_vblank = i8xx_enable_vblank,
277 	.disable_vblank = i8xx_disable_vblank,
278 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
279 };
280 
281 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
282 	INTEL_CRTC_FUNCS,
283 
284 	/* no hw vblank counter */
285 	.enable_vblank = i8xx_enable_vblank,
286 	.disable_vblank = i8xx_disable_vblank,
287 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
288 };
289 
290 int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
291 {
292 	struct intel_plane *primary, *cursor;
293 	const struct drm_crtc_funcs *funcs;
294 	struct intel_crtc *crtc;
295 	int sprite, ret;
296 
297 	crtc = intel_crtc_alloc();
298 	if (IS_ERR(crtc))
299 		return PTR_ERR(crtc);
300 
301 	crtc->pipe = pipe;
302 	crtc->num_scalers = RUNTIME_INFO(dev_priv)->num_scalers[pipe];
303 
304 	if (DISPLAY_VER(dev_priv) >= 9)
305 		primary = skl_universal_plane_create(dev_priv, pipe,
306 						     PLANE_PRIMARY);
307 	else
308 		primary = intel_primary_plane_create(dev_priv, pipe);
309 	if (IS_ERR(primary)) {
310 		ret = PTR_ERR(primary);
311 		goto fail;
312 	}
313 	crtc->plane_ids_mask |= BIT(primary->id);
314 
315 	for_each_sprite(dev_priv, pipe, sprite) {
316 		struct intel_plane *plane;
317 
318 		if (DISPLAY_VER(dev_priv) >= 9)
319 			plane = skl_universal_plane_create(dev_priv, pipe,
320 							   PLANE_SPRITE0 + sprite);
321 		else
322 			plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
323 		if (IS_ERR(plane)) {
324 			ret = PTR_ERR(plane);
325 			goto fail;
326 		}
327 		crtc->plane_ids_mask |= BIT(plane->id);
328 	}
329 
330 	cursor = intel_cursor_plane_create(dev_priv, pipe);
331 	if (IS_ERR(cursor)) {
332 		ret = PTR_ERR(cursor);
333 		goto fail;
334 	}
335 	crtc->plane_ids_mask |= BIT(cursor->id);
336 
337 	if (HAS_GMCH(dev_priv)) {
338 		if (IS_CHERRYVIEW(dev_priv) ||
339 		    IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
340 			funcs = &g4x_crtc_funcs;
341 		else if (DISPLAY_VER(dev_priv) == 4)
342 			funcs = &i965_crtc_funcs;
343 		else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
344 			funcs = &i915gm_crtc_funcs;
345 		else if (DISPLAY_VER(dev_priv) == 3)
346 			funcs = &i915_crtc_funcs;
347 		else
348 			funcs = &i8xx_crtc_funcs;
349 	} else {
350 		if (DISPLAY_VER(dev_priv) >= 8)
351 			funcs = &bdw_crtc_funcs;
352 		else
353 			funcs = &ilk_crtc_funcs;
354 	}
355 
356 	ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
357 					&primary->base, &cursor->base,
358 					funcs, "pipe %c", pipe_name(pipe));
359 	if (ret)
360 		goto fail;
361 
362 	if (DISPLAY_VER(dev_priv) >= 11)
363 		drm_crtc_create_scaling_filter_property(&crtc->base,
364 						BIT(DRM_SCALING_FILTER_DEFAULT) |
365 						BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR));
366 
367 	intel_color_init(crtc);
368 
369 	intel_crtc_crc_init(crtc);
370 
371 	cpu_latency_qos_add_request(&crtc->vblank_pm_qos, PM_QOS_DEFAULT_VALUE);
372 
373 	drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
374 
375 	return 0;
376 
377 fail:
378 	intel_crtc_free(crtc);
379 
380 	return ret;
381 }
382 
383 static bool intel_crtc_needs_vblank_work(const struct intel_crtc_state *crtc_state)
384 {
385 	return crtc_state->hw.active &&
386 		!intel_crtc_needs_modeset(crtc_state) &&
387 		!crtc_state->preload_luts &&
388 		(crtc_state->uapi.color_mgmt_changed ||
389 		 crtc_state->update_pipe);
390 }
391 
392 static void intel_crtc_vblank_work(struct kthread_work *base)
393 {
394 	struct drm_vblank_work *work = to_drm_vblank_work(base);
395 	struct intel_crtc_state *crtc_state =
396 		container_of(work, typeof(*crtc_state), vblank_work);
397 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
398 
399 	trace_intel_crtc_vblank_work_start(crtc);
400 
401 	intel_color_load_luts(crtc_state);
402 
403 	if (crtc_state->uapi.event) {
404 		spin_lock_irq(&crtc->base.dev->event_lock);
405 		drm_crtc_send_vblank_event(&crtc->base, crtc_state->uapi.event);
406 		crtc_state->uapi.event = NULL;
407 		spin_unlock_irq(&crtc->base.dev->event_lock);
408 	}
409 
410 	trace_intel_crtc_vblank_work_end(crtc);
411 }
412 
413 static void intel_crtc_vblank_work_init(struct intel_crtc_state *crtc_state)
414 {
415 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
416 
417 	drm_vblank_work_init(&crtc_state->vblank_work, &crtc->base,
418 			     intel_crtc_vblank_work);
419 	/*
420 	 * Interrupt latency is critical for getting the vblank
421 	 * work executed as early as possible during the vblank.
422 	 */
423 	cpu_latency_qos_update_request(&crtc->vblank_pm_qos, 0);
424 }
425 
426 void intel_wait_for_vblank_workers(struct intel_atomic_state *state)
427 {
428 	struct intel_crtc_state *crtc_state;
429 	struct intel_crtc *crtc;
430 	int i;
431 
432 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
433 		if (!intel_crtc_needs_vblank_work(crtc_state))
434 			continue;
435 
436 		drm_vblank_work_flush(&crtc_state->vblank_work);
437 		cpu_latency_qos_update_request(&crtc->vblank_pm_qos,
438 					       PM_QOS_DEFAULT_VALUE);
439 	}
440 }
441 
442 int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
443 			     int usecs)
444 {
445 	/* paranoia */
446 	if (!adjusted_mode->crtc_htotal)
447 		return 1;
448 
449 	return DIV_ROUND_UP(usecs * adjusted_mode->crtc_clock,
450 			    1000 * adjusted_mode->crtc_htotal);
451 }
452 
453 static int intel_mode_vblank_start(const struct drm_display_mode *mode)
454 {
455 	int vblank_start = mode->crtc_vblank_start;
456 
457 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
458 		vblank_start = DIV_ROUND_UP(vblank_start, 2);
459 
460 	return vblank_start;
461 }
462 
463 /**
464  * intel_pipe_update_start() - start update of a set of display registers
465  * @new_crtc_state: the new crtc state
466  *
467  * Mark the start of an update to pipe registers that should be updated
468  * atomically regarding vblank. If the next vblank will happens within
469  * the next 100 us, this function waits until the vblank passes.
470  *
471  * After a successful call to this function, interrupts will be disabled
472  * until a subsequent call to intel_pipe_update_end(). That is done to
473  * avoid random delays.
474  */
475 void intel_pipe_update_start(struct intel_crtc_state *new_crtc_state)
476 {
477 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
478 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
479 	const struct drm_display_mode *adjusted_mode = &new_crtc_state->hw.adjusted_mode;
480 	long timeout = msecs_to_jiffies_timeout(1);
481 	int scanline, min, max, vblank_start;
482 	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
483 	bool need_vlv_dsi_wa = (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
484 		intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
485 	DEFINE_WAIT(wait);
486 
487 	if (new_crtc_state->uapi.async_flip)
488 		return;
489 
490 	if (intel_crtc_needs_vblank_work(new_crtc_state))
491 		intel_crtc_vblank_work_init(new_crtc_state);
492 
493 	if (new_crtc_state->vrr.enable) {
494 		if (intel_vrr_is_push_sent(new_crtc_state))
495 			vblank_start = intel_vrr_vmin_vblank_start(new_crtc_state);
496 		else
497 			vblank_start = intel_vrr_vmax_vblank_start(new_crtc_state);
498 	} else {
499 		vblank_start = intel_mode_vblank_start(adjusted_mode);
500 	}
501 
502 	/* FIXME needs to be calibrated sensibly */
503 	min = vblank_start - intel_usecs_to_scanlines(adjusted_mode,
504 						      VBLANK_EVASION_TIME_US);
505 	max = vblank_start - 1;
506 
507 	if (min <= 0 || max <= 0)
508 		goto irq_disable;
509 
510 	if (drm_WARN_ON(&dev_priv->drm, drm_crtc_vblank_get(&crtc->base)))
511 		goto irq_disable;
512 
513 	/*
514 	 * Wait for psr to idle out after enabling the VBL interrupts
515 	 * VBL interrupts will start the PSR exit and prevent a PSR
516 	 * re-entry as well.
517 	 */
518 	intel_psr_wait_for_idle(new_crtc_state);
519 
520 	local_irq_disable();
521 
522 	crtc->debug.min_vbl = min;
523 	crtc->debug.max_vbl = max;
524 	trace_intel_pipe_update_start(crtc);
525 
526 	for (;;) {
527 		/*
528 		 * prepare_to_wait() has a memory barrier, which guarantees
529 		 * other CPUs can see the task state update by the time we
530 		 * read the scanline.
531 		 */
532 		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
533 
534 		scanline = intel_get_crtc_scanline(crtc);
535 		if (scanline < min || scanline > max)
536 			break;
537 
538 		if (!timeout) {
539 			drm_err(&dev_priv->drm,
540 				"Potential atomic update failure on pipe %c\n",
541 				pipe_name(crtc->pipe));
542 			break;
543 		}
544 
545 		local_irq_enable();
546 
547 		timeout = schedule_timeout(timeout);
548 
549 		local_irq_disable();
550 	}
551 
552 	finish_wait(wq, &wait);
553 
554 	drm_crtc_vblank_put(&crtc->base);
555 
556 	/*
557 	 * On VLV/CHV DSI the scanline counter would appear to
558 	 * increment approx. 1/3 of a scanline before start of vblank.
559 	 * The registers still get latched at start of vblank however.
560 	 * This means we must not write any registers on the first
561 	 * line of vblank (since not the whole line is actually in
562 	 * vblank). And unfortunately we can't use the interrupt to
563 	 * wait here since it will fire too soon. We could use the
564 	 * frame start interrupt instead since it will fire after the
565 	 * critical scanline, but that would require more changes
566 	 * in the interrupt code. So for now we'll just do the nasty
567 	 * thing and poll for the bad scanline to pass us by.
568 	 *
569 	 * FIXME figure out if BXT+ DSI suffers from this as well
570 	 */
571 	while (need_vlv_dsi_wa && scanline == vblank_start)
572 		scanline = intel_get_crtc_scanline(crtc);
573 
574 	crtc->debug.scanline_start = scanline;
575 	crtc->debug.start_vbl_time = ktime_get();
576 	crtc->debug.start_vbl_count = intel_crtc_get_vblank_counter(crtc);
577 
578 	trace_intel_pipe_update_vblank_evaded(crtc);
579 	return;
580 
581 irq_disable:
582 	local_irq_disable();
583 }
584 
585 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_VBLANK_EVADE)
586 static void dbg_vblank_evade(struct intel_crtc *crtc, ktime_t end)
587 {
588 	u64 delta = ktime_to_ns(ktime_sub(end, crtc->debug.start_vbl_time));
589 	unsigned int h;
590 
591 	h = ilog2(delta >> 9);
592 	if (h >= ARRAY_SIZE(crtc->debug.vbl.times))
593 		h = ARRAY_SIZE(crtc->debug.vbl.times) - 1;
594 	crtc->debug.vbl.times[h]++;
595 
596 	crtc->debug.vbl.sum += delta;
597 	if (!crtc->debug.vbl.min || delta < crtc->debug.vbl.min)
598 		crtc->debug.vbl.min = delta;
599 	if (delta > crtc->debug.vbl.max)
600 		crtc->debug.vbl.max = delta;
601 
602 	if (delta > 1000 * VBLANK_EVASION_TIME_US) {
603 		drm_dbg_kms(crtc->base.dev,
604 			    "Atomic update on pipe (%c) took %lld us, max time under evasion is %u us\n",
605 			    pipe_name(crtc->pipe),
606 			    div_u64(delta, 1000),
607 			    VBLANK_EVASION_TIME_US);
608 		crtc->debug.vbl.over++;
609 	}
610 }
611 #else
612 static void dbg_vblank_evade(struct intel_crtc *crtc, ktime_t end) {}
613 #endif
614 
615 /**
616  * intel_pipe_update_end() - end update of a set of display registers
617  * @new_crtc_state: the new crtc state
618  *
619  * Mark the end of an update started with intel_pipe_update_start(). This
620  * re-enables interrupts and verifies the update was actually completed
621  * before a vblank.
622  */
623 void intel_pipe_update_end(struct intel_crtc_state *new_crtc_state)
624 {
625 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
626 	enum pipe pipe = crtc->pipe;
627 	int scanline_end = intel_get_crtc_scanline(crtc);
628 	u32 end_vbl_count = intel_crtc_get_vblank_counter(crtc);
629 	ktime_t end_vbl_time = ktime_get();
630 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
631 
632 	if (new_crtc_state->uapi.async_flip)
633 		return;
634 
635 	trace_intel_pipe_update_end(crtc, end_vbl_count, scanline_end);
636 
637 	/*
638 	 * Incase of mipi dsi command mode, we need to set frame update
639 	 * request for every commit.
640 	 */
641 	if (DISPLAY_VER(dev_priv) >= 11 &&
642 	    intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
643 		icl_dsi_frame_update(new_crtc_state);
644 
645 	/* We're still in the vblank-evade critical section, this can't race.
646 	 * Would be slightly nice to just grab the vblank count and arm the
647 	 * event outside of the critical section - the spinlock might spin for a
648 	 * while ... */
649 	if (intel_crtc_needs_vblank_work(new_crtc_state)) {
650 		drm_vblank_work_schedule(&new_crtc_state->vblank_work,
651 					 drm_crtc_accurate_vblank_count(&crtc->base) + 1,
652 					 false);
653 	} else if (new_crtc_state->uapi.event) {
654 		drm_WARN_ON(&dev_priv->drm,
655 			    drm_crtc_vblank_get(&crtc->base) != 0);
656 
657 		spin_lock(&crtc->base.dev->event_lock);
658 		drm_crtc_arm_vblank_event(&crtc->base,
659 					  new_crtc_state->uapi.event);
660 		spin_unlock(&crtc->base.dev->event_lock);
661 
662 		new_crtc_state->uapi.event = NULL;
663 	}
664 
665 	/*
666 	 * Send VRR Push to terminate Vblank. If we are already in vblank
667 	 * this has to be done _after_ sampling the frame counter, as
668 	 * otherwise the push would immediately terminate the vblank and
669 	 * the sampled frame counter would correspond to the next frame
670 	 * instead of the current frame.
671 	 *
672 	 * There is a tiny race here (iff vblank evasion failed us) where
673 	 * we might sample the frame counter just before vmax vblank start
674 	 * but the push would be sent just after it. That would cause the
675 	 * push to affect the next frame instead of the current frame,
676 	 * which would cause the next frame to terminate already at vmin
677 	 * vblank start instead of vmax vblank start.
678 	 */
679 	intel_vrr_send_push(new_crtc_state);
680 
681 	local_irq_enable();
682 
683 	if (intel_vgpu_active(dev_priv))
684 		return;
685 
686 	if (crtc->debug.start_vbl_count &&
687 	    crtc->debug.start_vbl_count != end_vbl_count) {
688 		drm_err(&dev_priv->drm,
689 			"Atomic update failure on pipe %c (start=%u end=%u) time %lld us, min %d, max %d, scanline start %d, end %d\n",
690 			pipe_name(pipe), crtc->debug.start_vbl_count,
691 			end_vbl_count,
692 			ktime_us_delta(end_vbl_time,
693 				       crtc->debug.start_vbl_time),
694 			crtc->debug.min_vbl, crtc->debug.max_vbl,
695 			crtc->debug.scanline_start, scanline_end);
696 	}
697 
698 	dbg_vblank_evade(crtc, end_vbl_time);
699 }
700