xref: /linux/drivers/gpu/drm/i915/display/intel_crtc.c (revision 53597deca0e38c30e6cd4ba2114fa42d2bcd85bb)
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_print.h>
13 #include <drm/drm_vblank.h>
14 #include <drm/drm_vblank_work.h>
15 
16 #include "i9xx_plane.h"
17 #include "icl_dsi.h"
18 #include "intel_atomic.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_dsi.h"
28 #include "intel_fifo_underrun.h"
29 #include "intel_parent.h"
30 #include "intel_pipe_crc.h"
31 #include "intel_plane.h"
32 #include "intel_psr.h"
33 #include "intel_sprite.h"
34 #include "intel_vblank.h"
35 #include "intel_vrr.h"
36 #include "skl_universal_plane.h"
37 
38 static void assert_vblank_disabled(struct drm_crtc *crtc)
39 {
40 	struct intel_display *display = to_intel_display(crtc->dev);
41 
42 	if (INTEL_DISPLAY_STATE_WARN(display, drm_crtc_vblank_get(crtc) == 0,
43 				     "[CRTC:%d:%s] vblank assertion failure (expected off, current on)\n",
44 				     crtc->base.id, crtc->name))
45 		drm_crtc_vblank_put(crtc);
46 }
47 
48 struct intel_crtc *intel_first_crtc(struct intel_display *display)
49 {
50 	return to_intel_crtc(drm_crtc_from_index(display->drm, 0));
51 }
52 
53 struct intel_crtc *intel_crtc_for_pipe(struct intel_display *display,
54 				       enum pipe pipe)
55 {
56 	struct intel_crtc *crtc;
57 
58 	for_each_intel_crtc(display->drm, crtc) {
59 		if (crtc->pipe == pipe)
60 			return crtc;
61 	}
62 
63 	return NULL;
64 }
65 
66 void intel_crtc_wait_for_next_vblank(struct intel_crtc *crtc)
67 {
68 	drm_crtc_wait_one_vblank(&crtc->base);
69 }
70 
71 void intel_wait_for_vblank_if_active(struct intel_display *display,
72 				     enum pipe pipe)
73 {
74 	struct intel_crtc *crtc = intel_crtc_for_pipe(display, pipe);
75 
76 	if (crtc->active)
77 		intel_crtc_wait_for_next_vblank(crtc);
78 }
79 
80 u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
81 {
82 	struct drm_vblank_crtc *vblank = drm_crtc_vblank_crtc(&crtc->base);
83 
84 	if (!crtc->active)
85 		return 0;
86 
87 	if (!vblank->max_vblank_count) {
88 		/* On preempt-rt we cannot take the vblank spinlock since this function is called from tracepoints */
89 		if (IS_ENABLED(CONFIG_PREEMPT_RT))
90 			return (u32)drm_crtc_vblank_count(&crtc->base);
91 		else
92 			return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
93 	}
94 
95 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
96 }
97 
98 u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
99 {
100 	struct intel_display *display = to_intel_display(crtc_state);
101 
102 	/*
103 	 * From Gen 11, in case of dsi cmd mode, frame counter wouldn't
104 	 * have updated at the beginning of TE, if we want to use
105 	 * the hw counter, then we would find it updated in only
106 	 * the next TE, hence switching to sw counter.
107 	 */
108 	if (crtc_state->mode_flags & (I915_MODE_FLAG_DSI_USE_TE0 |
109 				      I915_MODE_FLAG_DSI_USE_TE1))
110 		return 0;
111 
112 	/*
113 	 * On i965gm the hardware frame counter reads
114 	 * zero when the TV encoder is enabled :(
115 	 */
116 	if (display->platform.i965gm &&
117 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
118 		return 0;
119 
120 	if (DISPLAY_VER(display) >= 5 || display->platform.g4x)
121 		return 0xffffffff; /* full 32 bit counter */
122 	else if (DISPLAY_VER(display) >= 3)
123 		return 0xffffff; /* only 24 bits of frame count */
124 	else
125 		return 0; /* Gen2 doesn't have a hardware frame counter */
126 }
127 
128 void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
129 {
130 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
131 
132 	crtc->vblank_psr_notify = intel_psr_needs_vblank_notification(crtc_state);
133 
134 	assert_vblank_disabled(&crtc->base);
135 	drm_crtc_set_max_vblank_count(&crtc->base,
136 				      intel_crtc_max_vblank_count(crtc_state));
137 	drm_crtc_vblank_on(&crtc->base);
138 
139 	/*
140 	 * Should really happen exactly when we enable the pipe
141 	 * but we want the frame counters in the trace, and that
142 	 * requires vblank support on some platforms/outputs.
143 	 */
144 	trace_intel_pipe_enable(crtc);
145 }
146 
147 void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
148 {
149 	struct intel_display *display = to_intel_display(crtc_state);
150 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
151 
152 	/*
153 	 * Should really happen exactly when we disable the pipe
154 	 * but we want the frame counters in the trace, and that
155 	 * requires vblank support on some platforms/outputs.
156 	 */
157 	trace_intel_pipe_disable(crtc);
158 
159 	drm_crtc_vblank_off(&crtc->base);
160 	assert_vblank_disabled(&crtc->base);
161 
162 	crtc->vblank_psr_notify = false;
163 
164 	flush_work(&display->irq.vblank_notify_work);
165 }
166 
167 struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
168 {
169 	struct intel_crtc_state *crtc_state;
170 
171 	crtc_state = kmalloc_obj(*crtc_state);
172 
173 	if (crtc_state)
174 		intel_crtc_state_reset(crtc_state, crtc);
175 
176 	return crtc_state;
177 }
178 
179 void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
180 			    struct intel_crtc *crtc)
181 {
182 	memset(crtc_state, 0, sizeof(*crtc_state));
183 
184 	__drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
185 
186 	crtc_state->cpu_transcoder = INVALID_TRANSCODER;
187 	crtc_state->master_transcoder = INVALID_TRANSCODER;
188 	crtc_state->hsw_workaround_pipe = INVALID_PIPE;
189 	crtc_state->scaler_state.scaler_id = -1;
190 	crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
191 	crtc_state->max_link_bpp_x16 = INT_MAX;
192 }
193 
194 static struct intel_crtc *intel_crtc_alloc(void)
195 {
196 	struct intel_crtc_state *crtc_state;
197 	struct intel_crtc *crtc;
198 
199 	crtc = kzalloc_obj(*crtc);
200 	if (!crtc)
201 		return ERR_PTR(-ENOMEM);
202 
203 	crtc_state = intel_crtc_state_alloc(crtc);
204 	if (!crtc_state) {
205 		kfree(crtc);
206 		return ERR_PTR(-ENOMEM);
207 	}
208 
209 	crtc->base.state = &crtc_state->uapi;
210 	crtc->config = crtc_state;
211 
212 	INIT_LIST_HEAD(&crtc->pipe_head);
213 
214 	return crtc;
215 }
216 
217 static void intel_crtc_free(struct intel_crtc *crtc)
218 {
219 	intel_crtc_destroy_state(&crtc->base, crtc->base.state);
220 	kfree(crtc);
221 }
222 
223 static void intel_crtc_destroy(struct drm_crtc *_crtc)
224 {
225 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
226 
227 	list_del(&crtc->pipe_head);
228 
229 	cpu_latency_qos_remove_request(&crtc->vblank_pm_qos);
230 
231 	drm_crtc_cleanup(&crtc->base);
232 	kfree(crtc);
233 }
234 
235 static int intel_crtc_late_register(struct drm_crtc *crtc)
236 {
237 	intel_crtc_debugfs_add(to_intel_crtc(crtc));
238 	return 0;
239 }
240 
241 #define INTEL_CRTC_FUNCS \
242 	.set_config = drm_atomic_helper_set_config, \
243 	.destroy = intel_crtc_destroy, \
244 	.page_flip = drm_atomic_helper_page_flip, \
245 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
246 	.atomic_destroy_state = intel_crtc_destroy_state, \
247 	.set_crc_source = intel_crtc_set_crc_source, \
248 	.verify_crc_source = intel_crtc_verify_crc_source, \
249 	.get_crc_sources = intel_crtc_get_crc_sources, \
250 	.late_register = intel_crtc_late_register
251 
252 static const struct drm_crtc_funcs bdw_crtc_funcs = {
253 	INTEL_CRTC_FUNCS,
254 
255 	.get_vblank_counter = g4x_get_vblank_counter,
256 	.enable_vblank = bdw_enable_vblank,
257 	.disable_vblank = bdw_disable_vblank,
258 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
259 };
260 
261 static const struct drm_crtc_funcs ilk_crtc_funcs = {
262 	INTEL_CRTC_FUNCS,
263 
264 	.get_vblank_counter = g4x_get_vblank_counter,
265 	.enable_vblank = ilk_enable_vblank,
266 	.disable_vblank = ilk_disable_vblank,
267 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
268 };
269 
270 static const struct drm_crtc_funcs g4x_crtc_funcs = {
271 	INTEL_CRTC_FUNCS,
272 
273 	.get_vblank_counter = g4x_get_vblank_counter,
274 	.enable_vblank = i965_enable_vblank,
275 	.disable_vblank = i965_disable_vblank,
276 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
277 };
278 
279 static const struct drm_crtc_funcs i965_crtc_funcs = {
280 	INTEL_CRTC_FUNCS,
281 
282 	.get_vblank_counter = i915_get_vblank_counter,
283 	.enable_vblank = i965_enable_vblank,
284 	.disable_vblank = i965_disable_vblank,
285 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
286 };
287 
288 static const struct drm_crtc_funcs i915gm_crtc_funcs = {
289 	INTEL_CRTC_FUNCS,
290 
291 	.get_vblank_counter = i915_get_vblank_counter,
292 	.enable_vblank = i915gm_enable_vblank,
293 	.disable_vblank = i915gm_disable_vblank,
294 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
295 };
296 
297 static const struct drm_crtc_funcs i915_crtc_funcs = {
298 	INTEL_CRTC_FUNCS,
299 
300 	.get_vblank_counter = i915_get_vblank_counter,
301 	.enable_vblank = i8xx_enable_vblank,
302 	.disable_vblank = i8xx_disable_vblank,
303 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
304 };
305 
306 static const struct drm_crtc_funcs i8xx_crtc_funcs = {
307 	INTEL_CRTC_FUNCS,
308 
309 	/* no hw vblank counter */
310 	.enable_vblank = i8xx_enable_vblank,
311 	.disable_vblank = i8xx_disable_vblank,
312 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
313 };
314 
315 static void add_crtc_to_pipe_list(struct intel_display *display, struct intel_crtc *crtc)
316 {
317 	struct intel_crtc *iter;
318 
319 	list_for_each_entry(iter, &display->pipe_list, pipe_head) {
320 		if (crtc->pipe < iter->pipe) {
321 			list_add_tail(&crtc->pipe_head, &iter->pipe_head);
322 			return;
323 		}
324 	}
325 
326 	list_add_tail(&crtc->pipe_head, &display->pipe_list);
327 }
328 
329 static int __intel_crtc_init(struct intel_display *display, enum pipe pipe)
330 {
331 	struct intel_plane *primary, *cursor;
332 	const struct drm_crtc_funcs *funcs;
333 	struct intel_crtc *crtc;
334 	int sprite, ret;
335 
336 	crtc = intel_crtc_alloc();
337 	if (IS_ERR(crtc))
338 		return PTR_ERR(crtc);
339 
340 	crtc->pipe = pipe;
341 	crtc->num_scalers = DISPLAY_RUNTIME_INFO(display)->num_scalers[pipe];
342 
343 	if (DISPLAY_VER(display) >= 9)
344 		primary = skl_universal_plane_create(display, pipe, PLANE_1);
345 	else
346 		primary = intel_primary_plane_create(display, pipe);
347 	if (IS_ERR(primary)) {
348 		ret = PTR_ERR(primary);
349 		goto fail;
350 	}
351 	crtc->plane_ids_mask |= BIT(primary->id);
352 
353 	intel_init_fifo_underrun_reporting(display, crtc, false);
354 
355 	for_each_sprite(display, pipe, sprite) {
356 		struct intel_plane *plane;
357 
358 		if (DISPLAY_VER(display) >= 9)
359 			plane = skl_universal_plane_create(display, pipe, PLANE_2 + sprite);
360 		else
361 			plane = intel_sprite_plane_create(display, pipe, sprite);
362 		if (IS_ERR(plane)) {
363 			ret = PTR_ERR(plane);
364 			goto fail;
365 		}
366 		crtc->plane_ids_mask |= BIT(plane->id);
367 	}
368 
369 	cursor = intel_cursor_plane_create(display, pipe);
370 	if (IS_ERR(cursor)) {
371 		ret = PTR_ERR(cursor);
372 		goto fail;
373 	}
374 	crtc->plane_ids_mask |= BIT(cursor->id);
375 
376 	if (HAS_GMCH(display)) {
377 		if (display->platform.cherryview ||
378 		    display->platform.valleyview ||
379 		    display->platform.g4x)
380 			funcs = &g4x_crtc_funcs;
381 		else if (DISPLAY_VER(display) == 4)
382 			funcs = &i965_crtc_funcs;
383 		else if (display->platform.i945gm ||
384 			 display->platform.i915gm)
385 			funcs = &i915gm_crtc_funcs;
386 		else if (DISPLAY_VER(display) == 3)
387 			funcs = &i915_crtc_funcs;
388 		else
389 			funcs = &i8xx_crtc_funcs;
390 	} else {
391 		if (DISPLAY_VER(display) >= 8)
392 			funcs = &bdw_crtc_funcs;
393 		else
394 			funcs = &ilk_crtc_funcs;
395 	}
396 
397 	ret = drm_crtc_init_with_planes(display->drm, &crtc->base,
398 					&primary->base, &cursor->base,
399 					funcs, "pipe %c", pipe_name(pipe));
400 	if (ret)
401 		goto fail;
402 
403 	if (DISPLAY_VER(display) >= 11)
404 		drm_crtc_create_scaling_filter_property(&crtc->base,
405 						BIT(DRM_SCALING_FILTER_DEFAULT) |
406 						BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR));
407 
408 	intel_color_crtc_init(crtc);
409 	intel_drrs_crtc_init(crtc);
410 	intel_crtc_crc_init(crtc);
411 
412 	cpu_latency_qos_add_request(&crtc->vblank_pm_qos, PM_QOS_DEFAULT_VALUE);
413 
414 	if (HAS_CASF(display) && crtc->num_scalers >= 2)
415 		drm_crtc_create_sharpness_strength_property(&crtc->base);
416 
417 	add_crtc_to_pipe_list(display, crtc);
418 
419 	return 0;
420 
421 fail:
422 	intel_crtc_free(crtc);
423 
424 	return ret;
425 }
426 
427 #define HAS_PIPE(display, pipe) (DISPLAY_RUNTIME_INFO(display)->pipe_mask & BIT(pipe))
428 
429 /*
430  * Expose the pipes in order A, C, B, D on discrete platforms to trick user
431  * space into using pipes that are more likely to be available for both a) user
432  * space if pipe B has been reserved for the joiner, and b) the joiner if pipe A
433  * doesn't need the joiner.
434  *
435  * Swap pipes B and C only if both are available i.e. not fused off.
436  */
437 static enum pipe reorder_pipe(struct intel_display *display, enum pipe pipe)
438 {
439 	if (!display->platform.dgfx || !HAS_PIPE(display, PIPE_B) || !HAS_PIPE(display, PIPE_C))
440 		return pipe;
441 
442 	switch (pipe) {
443 	case PIPE_B:
444 		return PIPE_C;
445 	case PIPE_C:
446 		return PIPE_B;
447 	default:
448 		return pipe;
449 	}
450 }
451 
452 int intel_crtc_init(struct intel_display *display)
453 {
454 	enum pipe pipe;
455 	int ret;
456 
457 	drm_dbg_kms(display->drm, "%d display pipe%s available.\n",
458 		    INTEL_NUM_PIPES(display), str_plural(INTEL_NUM_PIPES(display)));
459 
460 	for_each_pipe(display, pipe) {
461 		ret = __intel_crtc_init(display, reorder_pipe(display, pipe));
462 		if (ret)
463 			return ret;
464 	}
465 
466 	return 0;
467 }
468 
469 int intel_crtc_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
470 					   struct drm_file *file)
471 {
472 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
473 	struct drm_crtc *drm_crtc;
474 	struct intel_crtc *crtc;
475 
476 	drm_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
477 	if (!drm_crtc)
478 		return -ENOENT;
479 
480 	crtc = to_intel_crtc(drm_crtc);
481 	pipe_from_crtc_id->pipe = crtc->pipe;
482 
483 	return 0;
484 }
485 
486 static bool intel_crtc_needs_vblank_work(const struct intel_crtc_state *crtc_state)
487 {
488 	struct intel_display *display = to_intel_display(crtc_state);
489 
490 	return crtc_state->hw.active &&
491 		!crtc_state->preload_luts &&
492 		!intel_crtc_needs_modeset(crtc_state) &&
493 		(intel_crtc_needs_color_update(crtc_state) &&
494 		 !HAS_DOUBLE_BUFFERED_LUT(display)) &&
495 		!intel_color_uses_dsb(crtc_state) &&
496 		!crtc_state->use_dsb;
497 }
498 
499 static void intel_crtc_vblank_work(struct kthread_work *base)
500 {
501 	struct drm_vblank_work *work = to_drm_vblank_work(base);
502 	struct intel_crtc_state *crtc_state =
503 		container_of(work, typeof(*crtc_state), vblank_work);
504 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
505 
506 	trace_intel_crtc_vblank_work_start(crtc);
507 
508 	intel_color_load_luts(crtc_state);
509 
510 	if (crtc_state->uapi.event) {
511 		spin_lock_irq(&crtc->base.dev->event_lock);
512 		drm_crtc_send_vblank_event(&crtc->base, crtc_state->uapi.event);
513 		spin_unlock_irq(&crtc->base.dev->event_lock);
514 		crtc_state->uapi.event = NULL;
515 	}
516 
517 	trace_intel_crtc_vblank_work_end(crtc);
518 }
519 
520 static void intel_crtc_vblank_work_init(struct intel_crtc_state *crtc_state)
521 {
522 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
523 
524 	drm_vblank_work_init(&crtc_state->vblank_work, &crtc->base,
525 			     intel_crtc_vblank_work);
526 	/*
527 	 * Interrupt latency is critical for getting the vblank
528 	 * work executed as early as possible during the vblank.
529 	 */
530 	cpu_latency_qos_update_request(&crtc->vblank_pm_qos, 0);
531 }
532 
533 void intel_wait_for_vblank_workers(struct intel_atomic_state *state)
534 {
535 	struct intel_crtc_state *crtc_state;
536 	struct intel_crtc *crtc;
537 	int i;
538 
539 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
540 		if (!intel_crtc_needs_vblank_work(crtc_state))
541 			continue;
542 
543 		drm_vblank_work_flush(&crtc_state->vblank_work);
544 		cpu_latency_qos_update_request(&crtc->vblank_pm_qos,
545 					       PM_QOS_DEFAULT_VALUE);
546 	}
547 }
548 
549 int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
550 			     int usecs)
551 {
552 	/* paranoia */
553 	if (!adjusted_mode->crtc_htotal)
554 		return 1;
555 
556 	return DIV_ROUND_UP_ULL(mul_u32_u32(usecs, adjusted_mode->crtc_clock),
557 				1000 * adjusted_mode->crtc_htotal);
558 }
559 
560 int intel_scanlines_to_usecs(const struct drm_display_mode *adjusted_mode,
561 			     int scanlines)
562 {
563 	/* paranoia */
564 	if (!adjusted_mode->crtc_clock)
565 		return 1;
566 
567 	return DIV_ROUND_UP_ULL(mul_u32_u32(scanlines, adjusted_mode->crtc_htotal * 1000),
568 				adjusted_mode->crtc_clock);
569 }
570 
571 /**
572  * intel_pipe_update_start() - start update of a set of display registers
573  * @state: the atomic state
574  * @crtc: the crtc
575  *
576  * Mark the start of an update to pipe registers that should be updated
577  * atomically regarding vblank. If the next vblank will happens within
578  * the next 100 us, this function waits until the vblank passes.
579  *
580  * After a successful call to this function, interrupts will be disabled
581  * until a subsequent call to intel_pipe_update_end(). That is done to
582  * avoid random delays.
583  */
584 void intel_pipe_update_start(struct intel_atomic_state *state,
585 			     struct intel_crtc *crtc)
586 {
587 	struct intel_display *display = to_intel_display(state);
588 	const struct intel_crtc_state *old_crtc_state =
589 		intel_atomic_get_old_crtc_state(state, crtc);
590 	struct intel_crtc_state *new_crtc_state =
591 		intel_atomic_get_new_crtc_state(state, crtc);
592 	struct intel_vblank_evade_ctx evade;
593 	int scanline;
594 
595 	drm_WARN_ON(display->drm, new_crtc_state->use_dsb);
596 
597 	intel_psr_lock(new_crtc_state);
598 
599 	if (new_crtc_state->do_async_flip) {
600 		intel_crtc_prepare_vblank_event(new_crtc_state,
601 						&crtc->flip_done_event);
602 		return;
603 	}
604 
605 	if (intel_crtc_needs_vblank_work(new_crtc_state))
606 		intel_crtc_vblank_work_init(new_crtc_state);
607 
608 	if (state->base.legacy_cursor_update) {
609 		struct intel_plane *plane;
610 		struct intel_plane_state *old_plane_state, *new_plane_state;
611 		int i;
612 
613 		for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
614 						     new_plane_state, i) {
615 			if (old_plane_state->hw.crtc == &crtc->base)
616 				intel_plane_init_cursor_vblank_work(old_plane_state,
617 								    new_plane_state);
618 		}
619 	}
620 
621 	intel_vblank_evade_init(old_crtc_state, new_crtc_state, &evade);
622 
623 	if (drm_WARN_ON(display->drm, drm_crtc_vblank_get(&crtc->base)))
624 		goto irq_disable;
625 
626 	/*
627 	 * Wait for psr to idle out after enabling the VBL interrupts
628 	 * VBL interrupts will start the PSR exit and prevent a PSR
629 	 * re-entry as well.
630 	 */
631 	intel_psr_wait_for_idle_locked(new_crtc_state);
632 
633 	local_irq_disable();
634 
635 	crtc->debug.min_vbl = evade.min;
636 	crtc->debug.max_vbl = evade.max;
637 	trace_intel_pipe_update_start(crtc);
638 
639 	scanline = intel_vblank_evade(&evade);
640 
641 	drm_crtc_vblank_put(&crtc->base);
642 
643 	crtc->debug.scanline_start = scanline;
644 	crtc->debug.start_vbl_time = ktime_get();
645 	crtc->debug.start_vbl_count = intel_crtc_get_vblank_counter(crtc);
646 
647 	trace_intel_pipe_update_vblank_evaded(crtc);
648 	return;
649 
650 irq_disable:
651 	local_irq_disable();
652 }
653 
654 #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_VBLANK_EVADE)
655 static void dbg_vblank_evade(struct intel_crtc *crtc, ktime_t end)
656 {
657 	u64 delta = ktime_to_ns(ktime_sub(end, crtc->debug.start_vbl_time));
658 	unsigned int h;
659 
660 	h = ilog2(delta >> 9);
661 	if (h >= ARRAY_SIZE(crtc->debug.vbl.times))
662 		h = ARRAY_SIZE(crtc->debug.vbl.times) - 1;
663 	crtc->debug.vbl.times[h]++;
664 
665 	crtc->debug.vbl.sum += delta;
666 	if (!crtc->debug.vbl.min || delta < crtc->debug.vbl.min)
667 		crtc->debug.vbl.min = delta;
668 	if (delta > crtc->debug.vbl.max)
669 		crtc->debug.vbl.max = delta;
670 
671 	if (delta > 1000 * VBLANK_EVASION_TIME_US) {
672 		drm_dbg_kms(crtc->base.dev,
673 			    "Atomic update on pipe (%c) took %lld us, max time under evasion is %u us\n",
674 			    pipe_name(crtc->pipe),
675 			    div_u64(delta, 1000),
676 			    VBLANK_EVASION_TIME_US);
677 		crtc->debug.vbl.over++;
678 	}
679 }
680 #else
681 static void dbg_vblank_evade(struct intel_crtc *crtc, ktime_t end) {}
682 #endif
683 
684 void intel_crtc_arm_vblank_event(struct intel_crtc_state *crtc_state)
685 {
686 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
687 	unsigned long irqflags;
688 
689 	if (!crtc_state->uapi.event)
690 		return;
691 
692 	drm_WARN_ON(crtc->base.dev, drm_crtc_vblank_get(&crtc->base) != 0);
693 
694 	spin_lock_irqsave(&crtc->base.dev->event_lock, irqflags);
695 	drm_crtc_arm_vblank_event(&crtc->base, crtc_state->uapi.event);
696 	spin_unlock_irqrestore(&crtc->base.dev->event_lock, irqflags);
697 
698 	crtc_state->uapi.event = NULL;
699 }
700 
701 void intel_crtc_prepare_vblank_event(struct intel_crtc_state *crtc_state,
702 				     struct drm_pending_vblank_event **event)
703 {
704 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
705 	unsigned long irqflags;
706 
707 	spin_lock_irqsave(&crtc->base.dev->event_lock, irqflags);
708 	*event = crtc_state->uapi.event;
709 	spin_unlock_irqrestore(&crtc->base.dev->event_lock, irqflags);
710 
711 	crtc_state->uapi.event = NULL;
712 }
713 
714 /**
715  * intel_pipe_update_end() - end update of a set of display registers
716  * @state: the atomic state
717  * @crtc: the crtc
718  *
719  * Mark the end of an update started with intel_pipe_update_start(). This
720  * re-enables interrupts and verifies the update was actually completed
721  * before a vblank.
722  */
723 void intel_pipe_update_end(struct intel_atomic_state *state,
724 			   struct intel_crtc *crtc)
725 {
726 	struct intel_display *display = to_intel_display(state);
727 	struct intel_crtc_state *new_crtc_state =
728 		intel_atomic_get_new_crtc_state(state, crtc);
729 	enum pipe pipe = crtc->pipe;
730 	int scanline_end = intel_get_crtc_scanline(crtc);
731 	u32 end_vbl_count = intel_crtc_get_vblank_counter(crtc);
732 	ktime_t end_vbl_time = ktime_get();
733 
734 	drm_WARN_ON(display->drm, new_crtc_state->use_dsb);
735 
736 	if (new_crtc_state->do_async_flip)
737 		goto out;
738 
739 	trace_intel_pipe_update_end(crtc, end_vbl_count, scanline_end);
740 
741 	/*
742 	 * Incase of mipi dsi command mode, we need to set frame update
743 	 * request for every commit.
744 	 */
745 	if (DISPLAY_VER(display) >= 11 &&
746 	    intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
747 		icl_dsi_frame_update(new_crtc_state);
748 
749 	/* We're still in the vblank-evade critical section, this can't race.
750 	 * Would be slightly nice to just grab the vblank count and arm the
751 	 * event outside of the critical section - the spinlock might spin for a
752 	 * while ... */
753 	if (intel_crtc_needs_vblank_work(new_crtc_state)) {
754 		drm_vblank_work_schedule(&new_crtc_state->vblank_work,
755 					 drm_crtc_accurate_vblank_count(&crtc->base) + 1,
756 					 false);
757 	} else {
758 		intel_crtc_arm_vblank_event(new_crtc_state);
759 	}
760 
761 	if (state->base.legacy_cursor_update) {
762 		struct intel_plane *plane;
763 		struct intel_plane_state *old_plane_state;
764 		int i;
765 
766 		for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
767 			if (old_plane_state->hw.crtc == &crtc->base &&
768 			    old_plane_state->unpin_work.vblank) {
769 				drm_vblank_work_schedule(&old_plane_state->unpin_work,
770 							 drm_crtc_accurate_vblank_count(&crtc->base) + 1,
771 							 false);
772 
773 				/* Remove plane from atomic state, cleanup/free is done from vblank worker. */
774 				memset(&state->base.planes[i], 0, sizeof(state->base.planes[i]));
775 			}
776 		}
777 	}
778 
779 	/*
780 	 * Send VRR Push to terminate Vblank. If we are already in vblank
781 	 * this has to be done _after_ sampling the frame counter, as
782 	 * otherwise the push would immediately terminate the vblank and
783 	 * the sampled frame counter would correspond to the next frame
784 	 * instead of the current frame.
785 	 *
786 	 * There is a tiny race here (iff vblank evasion failed us) where
787 	 * we might sample the frame counter just before vmax vblank start
788 	 * but the push would be sent just after it. That would cause the
789 	 * push to affect the next frame instead of the current frame,
790 	 * which would cause the next frame to terminate already at vmin
791 	 * vblank start instead of vmax vblank start.
792 	 */
793 	if (!state->base.legacy_cursor_update ||
794 	    (intel_psr_use_trans_push(new_crtc_state) &&
795 	     !new_crtc_state->vrr.enable))
796 		intel_vrr_send_push(NULL, new_crtc_state);
797 
798 	local_irq_enable();
799 
800 	if (intel_parent_vgpu_active(display))
801 		goto out;
802 
803 	if (crtc->debug.start_vbl_count &&
804 	    crtc->debug.start_vbl_count != end_vbl_count) {
805 		drm_err(display->drm,
806 			"Atomic update failure on pipe %c (start=%u end=%u) time %lld us, min %d, max %d, scanline start %d, end %d\n",
807 			pipe_name(pipe), crtc->debug.start_vbl_count,
808 			end_vbl_count,
809 			ktime_us_delta(end_vbl_time,
810 				       crtc->debug.start_vbl_time),
811 			crtc->debug.min_vbl, crtc->debug.max_vbl,
812 			crtc->debug.scanline_start, scanline_end);
813 	}
814 
815 	dbg_vblank_evade(crtc, end_vbl_time);
816 
817 out:
818 	intel_psr_unlock(new_crtc_state);
819 }
820 
821 bool intel_crtc_enable_changed(const struct intel_crtc_state *old_crtc_state,
822 			       const struct intel_crtc_state *new_crtc_state)
823 {
824 	return old_crtc_state->hw.enable != new_crtc_state->hw.enable;
825 }
826 
827 bool intel_any_crtc_enable_changed(struct intel_atomic_state *state)
828 {
829 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
830 	struct intel_crtc *crtc;
831 	int i;
832 
833 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
834 					    new_crtc_state, i) {
835 		if (intel_crtc_enable_changed(old_crtc_state, new_crtc_state))
836 			return true;
837 	}
838 
839 	return false;
840 }
841 
842 bool intel_crtc_active_changed(const struct intel_crtc_state *old_crtc_state,
843 			       const struct intel_crtc_state *new_crtc_state)
844 {
845 	return old_crtc_state->hw.active != new_crtc_state->hw.active;
846 }
847 
848 bool intel_any_crtc_active_changed(struct intel_atomic_state *state)
849 {
850 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
851 	struct intel_crtc *crtc;
852 	int i;
853 
854 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
855 					    new_crtc_state, i) {
856 		if (intel_crtc_active_changed(old_crtc_state, new_crtc_state))
857 			return true;
858 	}
859 
860 	return false;
861 }
862 
863 unsigned int intel_crtc_bw_num_active_planes(const struct intel_crtc_state *crtc_state)
864 {
865 	/*
866 	 * We assume cursors are small enough
867 	 * to not cause bandwidth problems.
868 	 */
869 	return hweight8(crtc_state->active_planes & ~BIT(PLANE_CURSOR));
870 }
871 
872 unsigned int intel_crtc_bw_data_rate(const struct intel_crtc_state *crtc_state)
873 {
874 	struct intel_display *display = to_intel_display(crtc_state);
875 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
876 	unsigned int data_rate = 0;
877 	enum plane_id plane_id;
878 
879 	for_each_plane_id_on_crtc(crtc, plane_id) {
880 		/*
881 		 * We assume cursors are small enough
882 		 * to not cause bandwidth problems.
883 		 */
884 		if (plane_id == PLANE_CURSOR)
885 			continue;
886 
887 		data_rate += crtc_state->data_rate[plane_id];
888 
889 		if (DISPLAY_VER(display) < 11)
890 			data_rate += crtc_state->data_rate_y[plane_id];
891 	}
892 
893 	return data_rate;
894 }
895 
896 /* "Maximum Pipe Read Bandwidth" */
897 int intel_crtc_bw_min_cdclk(const struct intel_crtc_state *crtc_state)
898 {
899 	struct intel_display *display = to_intel_display(crtc_state);
900 
901 	if (DISPLAY_VER(display) < 12)
902 		return 0;
903 
904 	return DIV_ROUND_UP_ULL(mul_u32_u32(intel_crtc_bw_data_rate(crtc_state), 10), 512);
905 }
906