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