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