xref: /openbmc/linux/drivers/gpu/drm/i915/display/intel_crtc.c (revision c845428b7a9157523103100806bc8130d64769c8)
1fbf756c3SDave Airlie // SPDX-License-Identifier: MIT
2fbf756c3SDave Airlie /*
3fbf756c3SDave Airlie  * Copyright © 2020 Intel Corporation
4fbf756c3SDave Airlie  */
5fbf756c3SDave Airlie #include <linux/kernel.h>
62bbc6fcaSVille Syrjälä #include <linux/pm_qos.h>
7fbf756c3SDave Airlie #include <linux/slab.h>
8fbf756c3SDave Airlie 
9fbf756c3SDave Airlie #include <drm/drm_atomic_helper.h>
10fbf756c3SDave Airlie #include <drm/drm_fourcc.h>
11fbf756c3SDave Airlie #include <drm/drm_plane.h>
122bbc6fcaSVille Syrjälä #include <drm/drm_vblank_work.h>
13fbf756c3SDave Airlie 
14d471008bSDave Airlie #include "i915_vgpu.h"
15fd2b94a5SJani Nikula #include "i9xx_plane.h"
16617ed6c2SJani Nikula #include "icl_dsi.h"
17fbf756c3SDave Airlie #include "intel_atomic.h"
18fbf756c3SDave Airlie #include "intel_atomic_plane.h"
19fbf756c3SDave Airlie #include "intel_color.h"
20fbf756c3SDave Airlie #include "intel_crtc.h"
21fbf756c3SDave Airlie #include "intel_cursor.h"
22fbf756c3SDave Airlie #include "intel_display_debugfs.h"
232b874a02SJani Nikula #include "intel_display_irq.h"
24fd2b94a5SJani Nikula #include "intel_display_trace.h"
25fbf756c3SDave Airlie #include "intel_display_types.h"
26851f15feSVille Syrjälä #include "intel_drrs.h"
27d471008bSDave Airlie #include "intel_dsi.h"
2866560f33SVille Syrjälä #include "intel_fifo_underrun.h"
29fbf756c3SDave Airlie #include "intel_pipe_crc.h"
30d471008bSDave Airlie #include "intel_psr.h"
31fbf756c3SDave Airlie #include "intel_sprite.h"
3262fe4515SJani Nikula #include "intel_vblank.h"
33d471008bSDave Airlie #include "intel_vrr.h"
3446d12f91SDave Airlie #include "skl_universal_plane.h"
35fbf756c3SDave Airlie 
assert_vblank_disabled(struct drm_crtc * crtc)36fbf756c3SDave Airlie static void assert_vblank_disabled(struct drm_crtc *crtc)
37fbf756c3SDave Airlie {
386b9bd7c3SJani Nikula 	struct drm_i915_private *i915 = to_i915(crtc->dev);
396b9bd7c3SJani Nikula 
406b9bd7c3SJani Nikula 	if (I915_STATE_WARN(i915, drm_crtc_vblank_get(crtc) == 0,
41fe735c34SJani Nikula 			    "[CRTC:%d:%s] vblank assertion failure (expected off, current on)\n",
42fe735c34SJani Nikula 			    crtc->base.id, crtc->name))
43fbf756c3SDave Airlie 		drm_crtc_vblank_put(crtc);
44fbf756c3SDave Airlie }
45fbf756c3SDave Airlie 
intel_first_crtc(struct drm_i915_private * i915)467d41745aSVille Syrjälä struct intel_crtc *intel_first_crtc(struct drm_i915_private *i915)
4792e9624aSJani Nikula {
4892e9624aSJani Nikula 	return to_intel_crtc(drm_crtc_from_index(&i915->drm, 0));
4992e9624aSJani Nikula }
5092e9624aSJani Nikula 
intel_crtc_for_pipe(struct drm_i915_private * i915,enum pipe pipe)5192e9624aSJani Nikula struct intel_crtc *intel_crtc_for_pipe(struct drm_i915_private *i915,
5292e9624aSJani Nikula 				       enum pipe pipe)
5392e9624aSJani Nikula {
54cbb8a795SVille Syrjälä 	struct intel_crtc *crtc;
55cbb8a795SVille Syrjälä 
56cbb8a795SVille Syrjälä 	for_each_intel_crtc(&i915->drm, crtc) {
57cbb8a795SVille Syrjälä 		if (crtc->pipe == pipe)
58cbb8a795SVille Syrjälä 			return crtc;
59cbb8a795SVille Syrjälä 	}
60cbb8a795SVille Syrjälä 
61cbb8a795SVille Syrjälä 	return NULL;
6292e9624aSJani Nikula }
6392e9624aSJani Nikula 
intel_crtc_wait_for_next_vblank(struct intel_crtc * crtc)6492e9624aSJani Nikula void intel_crtc_wait_for_next_vblank(struct intel_crtc *crtc)
6592e9624aSJani Nikula {
6692e9624aSJani Nikula 	drm_crtc_wait_one_vblank(&crtc->base);
6792e9624aSJani Nikula }
6892e9624aSJani Nikula 
intel_wait_for_vblank_if_active(struct drm_i915_private * i915,enum pipe pipe)6992e9624aSJani Nikula void intel_wait_for_vblank_if_active(struct drm_i915_private *i915,
7092e9624aSJani Nikula 				     enum pipe pipe)
7192e9624aSJani Nikula {
7292e9624aSJani Nikula 	struct intel_crtc *crtc = intel_crtc_for_pipe(i915, pipe);
7392e9624aSJani Nikula 
7492e9624aSJani Nikula 	if (crtc->active)
7592e9624aSJani Nikula 		intel_crtc_wait_for_next_vblank(crtc);
7692e9624aSJani Nikula }
7792e9624aSJani Nikula 
intel_crtc_get_vblank_counter(struct intel_crtc * crtc)78fbf756c3SDave Airlie u32 intel_crtc_get_vblank_counter(struct intel_crtc *crtc)
79fbf756c3SDave Airlie {
80fbf756c3SDave Airlie 	struct drm_device *dev = crtc->base.dev;
81fbf756c3SDave Airlie 	struct drm_vblank_crtc *vblank = &dev->vblank[drm_crtc_index(&crtc->base)];
82fbf756c3SDave Airlie 
839bb475cdSVille Syrjälä 	if (!crtc->active)
849bb475cdSVille Syrjälä 		return 0;
859bb475cdSVille Syrjälä 
86fbf756c3SDave Airlie 	if (!vblank->max_vblank_count)
87fbf756c3SDave Airlie 		return (u32)drm_crtc_accurate_vblank_count(&crtc->base);
88fbf756c3SDave Airlie 
89fbf756c3SDave Airlie 	return crtc->base.funcs->get_vblank_counter(&crtc->base);
90fbf756c3SDave Airlie }
91fbf756c3SDave Airlie 
intel_crtc_max_vblank_count(const struct intel_crtc_state * crtc_state)92fbf756c3SDave Airlie u32 intel_crtc_max_vblank_count(const struct intel_crtc_state *crtc_state)
93fbf756c3SDave Airlie {
94fbf756c3SDave Airlie 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
95fbf756c3SDave Airlie 
96fbf756c3SDave Airlie 	/*
97fbf756c3SDave Airlie 	 * From Gen 11, In case of dsi cmd mode, frame counter wouldnt
98fbf756c3SDave Airlie 	 * have updated at the beginning of TE, if we want to use
99fbf756c3SDave Airlie 	 * the hw counter, then we would find it updated in only
100fbf756c3SDave Airlie 	 * the next TE, hence switching to sw counter.
101fbf756c3SDave Airlie 	 */
102603a945eSVille Syrjälä 	if (crtc_state->mode_flags & (I915_MODE_FLAG_DSI_USE_TE0 |
103603a945eSVille Syrjälä 				      I915_MODE_FLAG_DSI_USE_TE1))
104fbf756c3SDave Airlie 		return 0;
105fbf756c3SDave Airlie 
106fbf756c3SDave Airlie 	/*
107fbf756c3SDave Airlie 	 * On i965gm the hardware frame counter reads
108fbf756c3SDave Airlie 	 * zero when the TV encoder is enabled :(
109fbf756c3SDave Airlie 	 */
110fbf756c3SDave Airlie 	if (IS_I965GM(dev_priv) &&
111fbf756c3SDave Airlie 	    (crtc_state->output_types & BIT(INTEL_OUTPUT_TVOUT)))
112fbf756c3SDave Airlie 		return 0;
113fbf756c3SDave Airlie 
114005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv))
115fbf756c3SDave Airlie 		return 0xffffffff; /* full 32 bit counter */
116005e9537SMatt Roper 	else if (DISPLAY_VER(dev_priv) >= 3)
117fbf756c3SDave Airlie 		return 0xffffff; /* only 24 bits of frame count */
118fbf756c3SDave Airlie 	else
119fbf756c3SDave Airlie 		return 0; /* Gen2 doesn't have a hardware frame counter */
120fbf756c3SDave Airlie }
121fbf756c3SDave Airlie 
intel_crtc_vblank_on(const struct intel_crtc_state * crtc_state)122fbf756c3SDave Airlie void intel_crtc_vblank_on(const struct intel_crtc_state *crtc_state)
123fbf756c3SDave Airlie {
124fbf756c3SDave Airlie 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
125fbf756c3SDave Airlie 
126fbf756c3SDave Airlie 	assert_vblank_disabled(&crtc->base);
127fbf756c3SDave Airlie 	drm_crtc_set_max_vblank_count(&crtc->base,
128fbf756c3SDave Airlie 				      intel_crtc_max_vblank_count(crtc_state));
129fbf756c3SDave Airlie 	drm_crtc_vblank_on(&crtc->base);
130e43539f8SVille Syrjälä 
131e43539f8SVille Syrjälä 	/*
132e43539f8SVille Syrjälä 	 * Should really happen exactly when we enable the pipe
133e43539f8SVille Syrjälä 	 * but we want the frame counters in the trace, and that
134e43539f8SVille Syrjälä 	 * requires vblank support on some platforms/outputs.
135e43539f8SVille Syrjälä 	 */
136e43539f8SVille Syrjälä 	trace_intel_pipe_enable(crtc);
137fbf756c3SDave Airlie }
138fbf756c3SDave Airlie 
intel_crtc_vblank_off(const struct intel_crtc_state * crtc_state)139fbf756c3SDave Airlie void intel_crtc_vblank_off(const struct intel_crtc_state *crtc_state)
140fbf756c3SDave Airlie {
141fbf756c3SDave Airlie 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
142fbf756c3SDave Airlie 
143e43539f8SVille Syrjälä 	/*
144e43539f8SVille Syrjälä 	 * Should really happen exactly when we disable the pipe
145e43539f8SVille Syrjälä 	 * but we want the frame counters in the trace, and that
146e43539f8SVille Syrjälä 	 * requires vblank support on some platforms/outputs.
147e43539f8SVille Syrjälä 	 */
148e43539f8SVille Syrjälä 	trace_intel_pipe_disable(crtc);
149e43539f8SVille Syrjälä 
150fbf756c3SDave Airlie 	drm_crtc_vblank_off(&crtc->base);
151fbf756c3SDave Airlie 	assert_vblank_disabled(&crtc->base);
152fbf756c3SDave Airlie }
153fbf756c3SDave Airlie 
intel_crtc_state_alloc(struct intel_crtc * crtc)154fbf756c3SDave Airlie struct intel_crtc_state *intel_crtc_state_alloc(struct intel_crtc *crtc)
155fbf756c3SDave Airlie {
156fbf756c3SDave Airlie 	struct intel_crtc_state *crtc_state;
157fbf756c3SDave Airlie 
158fbf756c3SDave Airlie 	crtc_state = kmalloc(sizeof(*crtc_state), GFP_KERNEL);
159fbf756c3SDave Airlie 
160fbf756c3SDave Airlie 	if (crtc_state)
161fbf756c3SDave Airlie 		intel_crtc_state_reset(crtc_state, crtc);
162fbf756c3SDave Airlie 
163fbf756c3SDave Airlie 	return crtc_state;
164fbf756c3SDave Airlie }
165fbf756c3SDave Airlie 
intel_crtc_state_reset(struct intel_crtc_state * crtc_state,struct intel_crtc * crtc)166fbf756c3SDave Airlie void intel_crtc_state_reset(struct intel_crtc_state *crtc_state,
167fbf756c3SDave Airlie 			    struct intel_crtc *crtc)
168fbf756c3SDave Airlie {
169fbf756c3SDave Airlie 	memset(crtc_state, 0, sizeof(*crtc_state));
170fbf756c3SDave Airlie 
171fbf756c3SDave Airlie 	__drm_atomic_helper_crtc_state_reset(&crtc_state->uapi, &crtc->base);
172fbf756c3SDave Airlie 
173fbf756c3SDave Airlie 	crtc_state->cpu_transcoder = INVALID_TRANSCODER;
174fbf756c3SDave Airlie 	crtc_state->master_transcoder = INVALID_TRANSCODER;
175fbf756c3SDave Airlie 	crtc_state->hsw_workaround_pipe = INVALID_PIPE;
176fbf756c3SDave Airlie 	crtc_state->scaler_state.scaler_id = -1;
177fbf756c3SDave Airlie 	crtc_state->mst_master_transcoder = INVALID_TRANSCODER;
178fbf756c3SDave Airlie }
179fbf756c3SDave Airlie 
intel_crtc_alloc(void)180fbf756c3SDave Airlie static struct intel_crtc *intel_crtc_alloc(void)
181fbf756c3SDave Airlie {
182fbf756c3SDave Airlie 	struct intel_crtc_state *crtc_state;
183fbf756c3SDave Airlie 	struct intel_crtc *crtc;
184fbf756c3SDave Airlie 
185fbf756c3SDave Airlie 	crtc = kzalloc(sizeof(*crtc), GFP_KERNEL);
186fbf756c3SDave Airlie 	if (!crtc)
187fbf756c3SDave Airlie 		return ERR_PTR(-ENOMEM);
188fbf756c3SDave Airlie 
189fbf756c3SDave Airlie 	crtc_state = intel_crtc_state_alloc(crtc);
190fbf756c3SDave Airlie 	if (!crtc_state) {
191fbf756c3SDave Airlie 		kfree(crtc);
192fbf756c3SDave Airlie 		return ERR_PTR(-ENOMEM);
193fbf756c3SDave Airlie 	}
194fbf756c3SDave Airlie 
195fbf756c3SDave Airlie 	crtc->base.state = &crtc_state->uapi;
196fbf756c3SDave Airlie 	crtc->config = crtc_state;
197fbf756c3SDave Airlie 
198fbf756c3SDave Airlie 	return crtc;
199fbf756c3SDave Airlie }
200fbf756c3SDave Airlie 
intel_crtc_free(struct intel_crtc * crtc)201fbf756c3SDave Airlie static void intel_crtc_free(struct intel_crtc *crtc)
202fbf756c3SDave Airlie {
203fbf756c3SDave Airlie 	intel_crtc_destroy_state(&crtc->base, crtc->base.state);
204fbf756c3SDave Airlie 	kfree(crtc);
205fbf756c3SDave Airlie }
206fbf756c3SDave Airlie 
intel_crtc_destroy(struct drm_crtc * _crtc)207f15f01a7SVille Syrjälä static void intel_crtc_destroy(struct drm_crtc *_crtc)
208fbf756c3SDave Airlie {
209f15f01a7SVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(_crtc);
210fbf756c3SDave Airlie 
2112bbc6fcaSVille Syrjälä 	cpu_latency_qos_remove_request(&crtc->vblank_pm_qos);
2122bbc6fcaSVille Syrjälä 
213f15f01a7SVille Syrjälä 	drm_crtc_cleanup(&crtc->base);
214f15f01a7SVille Syrjälä 	kfree(crtc);
215fbf756c3SDave Airlie }
216fbf756c3SDave Airlie 
intel_crtc_late_register(struct drm_crtc * crtc)217fbf756c3SDave Airlie static int intel_crtc_late_register(struct drm_crtc *crtc)
218fbf756c3SDave Airlie {
219014f0515SJani Nikula 	intel_crtc_debugfs_add(to_intel_crtc(crtc));
220fbf756c3SDave Airlie 	return 0;
221fbf756c3SDave Airlie }
222fbf756c3SDave Airlie 
223fbf756c3SDave Airlie #define INTEL_CRTC_FUNCS \
224fbf756c3SDave Airlie 	.set_config = drm_atomic_helper_set_config, \
225fbf756c3SDave Airlie 	.destroy = intel_crtc_destroy, \
226fbf756c3SDave Airlie 	.page_flip = drm_atomic_helper_page_flip, \
227fbf756c3SDave Airlie 	.atomic_duplicate_state = intel_crtc_duplicate_state, \
228fbf756c3SDave Airlie 	.atomic_destroy_state = intel_crtc_destroy_state, \
229fbf756c3SDave Airlie 	.set_crc_source = intel_crtc_set_crc_source, \
230fbf756c3SDave Airlie 	.verify_crc_source = intel_crtc_verify_crc_source, \
231fbf756c3SDave Airlie 	.get_crc_sources = intel_crtc_get_crc_sources, \
232fbf756c3SDave Airlie 	.late_register = intel_crtc_late_register
233fbf756c3SDave Airlie 
234fbf756c3SDave Airlie static const struct drm_crtc_funcs bdw_crtc_funcs = {
235fbf756c3SDave Airlie 	INTEL_CRTC_FUNCS,
236fbf756c3SDave Airlie 
237fbf756c3SDave Airlie 	.get_vblank_counter = g4x_get_vblank_counter,
238fbf756c3SDave Airlie 	.enable_vblank = bdw_enable_vblank,
239fbf756c3SDave Airlie 	.disable_vblank = bdw_disable_vblank,
240fbf756c3SDave Airlie 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
241fbf756c3SDave Airlie };
242fbf756c3SDave Airlie 
243fbf756c3SDave Airlie static const struct drm_crtc_funcs ilk_crtc_funcs = {
244fbf756c3SDave Airlie 	INTEL_CRTC_FUNCS,
245fbf756c3SDave Airlie 
246fbf756c3SDave Airlie 	.get_vblank_counter = g4x_get_vblank_counter,
247fbf756c3SDave Airlie 	.enable_vblank = ilk_enable_vblank,
248fbf756c3SDave Airlie 	.disable_vblank = ilk_disable_vblank,
249fbf756c3SDave Airlie 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
250fbf756c3SDave Airlie };
251fbf756c3SDave Airlie 
252fbf756c3SDave Airlie static const struct drm_crtc_funcs g4x_crtc_funcs = {
253fbf756c3SDave Airlie 	INTEL_CRTC_FUNCS,
254fbf756c3SDave Airlie 
255fbf756c3SDave Airlie 	.get_vblank_counter = g4x_get_vblank_counter,
256fbf756c3SDave Airlie 	.enable_vblank = i965_enable_vblank,
257fbf756c3SDave Airlie 	.disable_vblank = i965_disable_vblank,
258fbf756c3SDave Airlie 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
259fbf756c3SDave Airlie };
260fbf756c3SDave Airlie 
261fbf756c3SDave Airlie static const struct drm_crtc_funcs i965_crtc_funcs = {
262fbf756c3SDave Airlie 	INTEL_CRTC_FUNCS,
263fbf756c3SDave Airlie 
264fbf756c3SDave Airlie 	.get_vblank_counter = i915_get_vblank_counter,
265fbf756c3SDave Airlie 	.enable_vblank = i965_enable_vblank,
266fbf756c3SDave Airlie 	.disable_vblank = i965_disable_vblank,
267fbf756c3SDave Airlie 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
268fbf756c3SDave Airlie };
269fbf756c3SDave Airlie 
270fbf756c3SDave Airlie static const struct drm_crtc_funcs i915gm_crtc_funcs = {
271fbf756c3SDave Airlie 	INTEL_CRTC_FUNCS,
272fbf756c3SDave Airlie 
273fbf756c3SDave Airlie 	.get_vblank_counter = i915_get_vblank_counter,
274fbf756c3SDave Airlie 	.enable_vblank = i915gm_enable_vblank,
275fbf756c3SDave Airlie 	.disable_vblank = i915gm_disable_vblank,
276fbf756c3SDave Airlie 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
277fbf756c3SDave Airlie };
278fbf756c3SDave Airlie 
279fbf756c3SDave Airlie static const struct drm_crtc_funcs i915_crtc_funcs = {
280fbf756c3SDave Airlie 	INTEL_CRTC_FUNCS,
281fbf756c3SDave Airlie 
282fbf756c3SDave Airlie 	.get_vblank_counter = i915_get_vblank_counter,
283fbf756c3SDave Airlie 	.enable_vblank = i8xx_enable_vblank,
284fbf756c3SDave Airlie 	.disable_vblank = i8xx_disable_vblank,
285fbf756c3SDave Airlie 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
286fbf756c3SDave Airlie };
287fbf756c3SDave Airlie 
288fbf756c3SDave Airlie static const struct drm_crtc_funcs i8xx_crtc_funcs = {
289fbf756c3SDave Airlie 	INTEL_CRTC_FUNCS,
290fbf756c3SDave Airlie 
291fbf756c3SDave Airlie 	/* no hw vblank counter */
292fbf756c3SDave Airlie 	.enable_vblank = i8xx_enable_vblank,
293fbf756c3SDave Airlie 	.disable_vblank = i8xx_disable_vblank,
294fbf756c3SDave Airlie 	.get_vblank_timestamp = intel_crtc_get_vblank_timestamp,
295fbf756c3SDave Airlie };
296fbf756c3SDave Airlie 
intel_crtc_init(struct drm_i915_private * dev_priv,enum pipe pipe)297fbf756c3SDave Airlie int intel_crtc_init(struct drm_i915_private *dev_priv, enum pipe pipe)
298fbf756c3SDave Airlie {
299fbf756c3SDave Airlie 	struct intel_plane *primary, *cursor;
300fbf756c3SDave Airlie 	const struct drm_crtc_funcs *funcs;
301fbf756c3SDave Airlie 	struct intel_crtc *crtc;
302fbf756c3SDave Airlie 	int sprite, ret;
303fbf756c3SDave Airlie 
304fbf756c3SDave Airlie 	crtc = intel_crtc_alloc();
305fbf756c3SDave Airlie 	if (IS_ERR(crtc))
306fbf756c3SDave Airlie 		return PTR_ERR(crtc);
307fbf756c3SDave Airlie 
308fbf756c3SDave Airlie 	crtc->pipe = pipe;
30918e0deeeSMatt Roper 	crtc->num_scalers = DISPLAY_RUNTIME_INFO(dev_priv)->num_scalers[pipe];
310fbf756c3SDave Airlie 
311005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 9)
31246d12f91SDave Airlie 		primary = skl_universal_plane_create(dev_priv, pipe,
31346d12f91SDave Airlie 						     PLANE_PRIMARY);
31446d12f91SDave Airlie 	else
315fbf756c3SDave Airlie 		primary = intel_primary_plane_create(dev_priv, pipe);
316fbf756c3SDave Airlie 	if (IS_ERR(primary)) {
317fbf756c3SDave Airlie 		ret = PTR_ERR(primary);
318fbf756c3SDave Airlie 		goto fail;
319fbf756c3SDave Airlie 	}
320fbf756c3SDave Airlie 	crtc->plane_ids_mask |= BIT(primary->id);
321fbf756c3SDave Airlie 
32266560f33SVille Syrjälä 	intel_init_fifo_underrun_reporting(dev_priv, crtc, false);
32366560f33SVille Syrjälä 
324fbf756c3SDave Airlie 	for_each_sprite(dev_priv, pipe, sprite) {
325fbf756c3SDave Airlie 		struct intel_plane *plane;
326fbf756c3SDave Airlie 
327005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 9)
32846d12f91SDave Airlie 			plane = skl_universal_plane_create(dev_priv, pipe,
32946d12f91SDave Airlie 							   PLANE_SPRITE0 + sprite);
33046d12f91SDave Airlie 		else
331fbf756c3SDave Airlie 			plane = intel_sprite_plane_create(dev_priv, pipe, sprite);
332fbf756c3SDave Airlie 		if (IS_ERR(plane)) {
333fbf756c3SDave Airlie 			ret = PTR_ERR(plane);
334fbf756c3SDave Airlie 			goto fail;
335fbf756c3SDave Airlie 		}
336fbf756c3SDave Airlie 		crtc->plane_ids_mask |= BIT(plane->id);
337fbf756c3SDave Airlie 	}
338fbf756c3SDave Airlie 
339fbf756c3SDave Airlie 	cursor = intel_cursor_plane_create(dev_priv, pipe);
340fbf756c3SDave Airlie 	if (IS_ERR(cursor)) {
341fbf756c3SDave Airlie 		ret = PTR_ERR(cursor);
342fbf756c3SDave Airlie 		goto fail;
343fbf756c3SDave Airlie 	}
344fbf756c3SDave Airlie 	crtc->plane_ids_mask |= BIT(cursor->id);
345fbf756c3SDave Airlie 
346fbf756c3SDave Airlie 	if (HAS_GMCH(dev_priv)) {
347fbf756c3SDave Airlie 		if (IS_CHERRYVIEW(dev_priv) ||
348fbf756c3SDave Airlie 		    IS_VALLEYVIEW(dev_priv) || IS_G4X(dev_priv))
349fbf756c3SDave Airlie 			funcs = &g4x_crtc_funcs;
35093e7e61eSLucas De Marchi 		else if (DISPLAY_VER(dev_priv) == 4)
351fbf756c3SDave Airlie 			funcs = &i965_crtc_funcs;
352fbf756c3SDave Airlie 		else if (IS_I945GM(dev_priv) || IS_I915GM(dev_priv))
353fbf756c3SDave Airlie 			funcs = &i915gm_crtc_funcs;
35493e7e61eSLucas De Marchi 		else if (DISPLAY_VER(dev_priv) == 3)
355fbf756c3SDave Airlie 			funcs = &i915_crtc_funcs;
356fbf756c3SDave Airlie 		else
357fbf756c3SDave Airlie 			funcs = &i8xx_crtc_funcs;
358fbf756c3SDave Airlie 	} else {
359005e9537SMatt Roper 		if (DISPLAY_VER(dev_priv) >= 8)
360fbf756c3SDave Airlie 			funcs = &bdw_crtc_funcs;
361fbf756c3SDave Airlie 		else
362fbf756c3SDave Airlie 			funcs = &ilk_crtc_funcs;
363fbf756c3SDave Airlie 	}
364fbf756c3SDave Airlie 
365fbf756c3SDave Airlie 	ret = drm_crtc_init_with_planes(&dev_priv->drm, &crtc->base,
366fbf756c3SDave Airlie 					&primary->base, &cursor->base,
367fbf756c3SDave Airlie 					funcs, "pipe %c", pipe_name(pipe));
368fbf756c3SDave Airlie 	if (ret)
369fbf756c3SDave Airlie 		goto fail;
370fbf756c3SDave Airlie 
37189a34600SLucas De Marchi 	if (DISPLAY_VER(dev_priv) >= 11)
372fbf756c3SDave Airlie 		drm_crtc_create_scaling_filter_property(&crtc->base,
373fbf756c3SDave Airlie 						BIT(DRM_SCALING_FILTER_DEFAULT) |
374fbf756c3SDave Airlie 						BIT(DRM_SCALING_FILTER_NEAREST_NEIGHBOR));
375fbf756c3SDave Airlie 
376a2b1d9ecSVille Syrjälä 	intel_color_crtc_init(crtc);
377a2b1d9ecSVille Syrjälä 	intel_drrs_crtc_init(crtc);
378fbf756c3SDave Airlie 	intel_crtc_crc_init(crtc);
379fbf756c3SDave Airlie 
3802bbc6fcaSVille Syrjälä 	cpu_latency_qos_add_request(&crtc->vblank_pm_qos, PM_QOS_DEFAULT_VALUE);
3812bbc6fcaSVille Syrjälä 
382fbf756c3SDave Airlie 	drm_WARN_ON(&dev_priv->drm, drm_crtc_index(&crtc->base) != crtc->pipe);
383fbf756c3SDave Airlie 
384fbf756c3SDave Airlie 	return 0;
385fbf756c3SDave Airlie 
386fbf756c3SDave Airlie fail:
387fbf756c3SDave Airlie 	intel_crtc_free(crtc);
388fbf756c3SDave Airlie 
389fbf756c3SDave Airlie 	return ret;
390fbf756c3SDave Airlie }
391d471008bSDave Airlie 
intel_crtc_needs_vblank_work(const struct intel_crtc_state * crtc_state)3922bbc6fcaSVille Syrjälä static bool intel_crtc_needs_vblank_work(const struct intel_crtc_state *crtc_state)
3932bbc6fcaSVille Syrjälä {
3942bbc6fcaSVille Syrjälä 	return crtc_state->hw.active &&
3952bbc6fcaSVille Syrjälä 		!intel_crtc_needs_modeset(crtc_state) &&
3962bbc6fcaSVille Syrjälä 		!crtc_state->preload_luts &&
397f5e674e9SVille Syrjälä 		intel_crtc_needs_color_update(crtc_state);
3982bbc6fcaSVille Syrjälä }
3992bbc6fcaSVille Syrjälä 
intel_crtc_vblank_work(struct kthread_work * base)4002bbc6fcaSVille Syrjälä static void intel_crtc_vblank_work(struct kthread_work *base)
4012bbc6fcaSVille Syrjälä {
4022bbc6fcaSVille Syrjälä 	struct drm_vblank_work *work = to_drm_vblank_work(base);
4032bbc6fcaSVille Syrjälä 	struct intel_crtc_state *crtc_state =
4042bbc6fcaSVille Syrjälä 		container_of(work, typeof(*crtc_state), vblank_work);
4052bbc6fcaSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4062bbc6fcaSVille Syrjälä 
4072bbc6fcaSVille Syrjälä 	trace_intel_crtc_vblank_work_start(crtc);
4082bbc6fcaSVille Syrjälä 
4092bbc6fcaSVille Syrjälä 	intel_color_load_luts(crtc_state);
4102bbc6fcaSVille Syrjälä 
4112bbc6fcaSVille Syrjälä 	if (crtc_state->uapi.event) {
4122bbc6fcaSVille Syrjälä 		spin_lock_irq(&crtc->base.dev->event_lock);
4132bbc6fcaSVille Syrjälä 		drm_crtc_send_vblank_event(&crtc->base, crtc_state->uapi.event);
4142bbc6fcaSVille Syrjälä 		crtc_state->uapi.event = NULL;
4152bbc6fcaSVille Syrjälä 		spin_unlock_irq(&crtc->base.dev->event_lock);
4162bbc6fcaSVille Syrjälä 	}
4172bbc6fcaSVille Syrjälä 
4182bbc6fcaSVille Syrjälä 	trace_intel_crtc_vblank_work_end(crtc);
4192bbc6fcaSVille Syrjälä }
4202bbc6fcaSVille Syrjälä 
intel_crtc_vblank_work_init(struct intel_crtc_state * crtc_state)4212bbc6fcaSVille Syrjälä static void intel_crtc_vblank_work_init(struct intel_crtc_state *crtc_state)
4222bbc6fcaSVille Syrjälä {
4232bbc6fcaSVille Syrjälä 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4242bbc6fcaSVille Syrjälä 
4252bbc6fcaSVille Syrjälä 	drm_vblank_work_init(&crtc_state->vblank_work, &crtc->base,
4262bbc6fcaSVille Syrjälä 			     intel_crtc_vblank_work);
4272bbc6fcaSVille Syrjälä 	/*
4282bbc6fcaSVille Syrjälä 	 * Interrupt latency is critical for getting the vblank
4292bbc6fcaSVille Syrjälä 	 * work executed as early as possible during the vblank.
4302bbc6fcaSVille Syrjälä 	 */
4312bbc6fcaSVille Syrjälä 	cpu_latency_qos_update_request(&crtc->vblank_pm_qos, 0);
4322bbc6fcaSVille Syrjälä }
4332bbc6fcaSVille Syrjälä 
intel_wait_for_vblank_workers(struct intel_atomic_state * state)4342bbc6fcaSVille Syrjälä void intel_wait_for_vblank_workers(struct intel_atomic_state *state)
4352bbc6fcaSVille Syrjälä {
4362bbc6fcaSVille Syrjälä 	struct intel_crtc_state *crtc_state;
4372bbc6fcaSVille Syrjälä 	struct intel_crtc *crtc;
4382bbc6fcaSVille Syrjälä 	int i;
4392bbc6fcaSVille Syrjälä 
4402bbc6fcaSVille Syrjälä 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
4412bbc6fcaSVille Syrjälä 		if (!intel_crtc_needs_vblank_work(crtc_state))
4422bbc6fcaSVille Syrjälä 			continue;
4432bbc6fcaSVille Syrjälä 
4442bbc6fcaSVille Syrjälä 		drm_vblank_work_flush(&crtc_state->vblank_work);
4452bbc6fcaSVille Syrjälä 		cpu_latency_qos_update_request(&crtc->vblank_pm_qos,
4462bbc6fcaSVille Syrjälä 					       PM_QOS_DEFAULT_VALUE);
4472bbc6fcaSVille Syrjälä 	}
4482bbc6fcaSVille Syrjälä }
4492bbc6fcaSVille Syrjälä 
intel_usecs_to_scanlines(const struct drm_display_mode * adjusted_mode,int usecs)450d471008bSDave Airlie int intel_usecs_to_scanlines(const struct drm_display_mode *adjusted_mode,
451d471008bSDave Airlie 			     int usecs)
452d471008bSDave Airlie {
453d471008bSDave Airlie 	/* paranoia */
454d471008bSDave Airlie 	if (!adjusted_mode->crtc_htotal)
455d471008bSDave Airlie 		return 1;
456d471008bSDave Airlie 
457d471008bSDave Airlie 	return DIV_ROUND_UP(usecs * adjusted_mode->crtc_clock,
458d471008bSDave Airlie 			    1000 * adjusted_mode->crtc_htotal);
459d471008bSDave Airlie }
460d471008bSDave Airlie 
intel_mode_vblank_start(const struct drm_display_mode * mode)461d471008bSDave Airlie static int intel_mode_vblank_start(const struct drm_display_mode *mode)
462d471008bSDave Airlie {
463d471008bSDave Airlie 	int vblank_start = mode->crtc_vblank_start;
464d471008bSDave Airlie 
465d471008bSDave Airlie 	if (mode->flags & DRM_MODE_FLAG_INTERLACE)
466d471008bSDave Airlie 		vblank_start = DIV_ROUND_UP(vblank_start, 2);
467d471008bSDave Airlie 
468d471008bSDave Airlie 	return vblank_start;
469d471008bSDave Airlie }
470d471008bSDave Airlie 
intel_crtc_vblank_evade_scanlines(struct intel_atomic_state * state,struct intel_crtc * crtc,int * min,int * max,int * vblank_start)4717e50ac42SVille Syrjälä static void intel_crtc_vblank_evade_scanlines(struct intel_atomic_state *state,
4727e50ac42SVille Syrjälä 					      struct intel_crtc *crtc,
4737e50ac42SVille Syrjälä 					      int *min, int *max, int *vblank_start)
4747e50ac42SVille Syrjälä {
475deaeb5b6SVille Syrjälä 	const struct intel_crtc_state *old_crtc_state =
476deaeb5b6SVille Syrjälä 		intel_atomic_get_old_crtc_state(state, crtc);
4777e50ac42SVille Syrjälä 	const struct intel_crtc_state *new_crtc_state =
4787e50ac42SVille Syrjälä 		intel_atomic_get_new_crtc_state(state, crtc);
479deaeb5b6SVille Syrjälä 	const struct intel_crtc_state *crtc_state;
480deaeb5b6SVille Syrjälä 	const struct drm_display_mode *adjusted_mode;
4817e50ac42SVille Syrjälä 
482deaeb5b6SVille Syrjälä 	/*
483deaeb5b6SVille Syrjälä 	 * During fastsets/etc. the transcoder is still
484deaeb5b6SVille Syrjälä 	 * running with the old timings at this point.
485deaeb5b6SVille Syrjälä 	 *
486deaeb5b6SVille Syrjälä 	 * TODO: maybe just use the active timings here?
487deaeb5b6SVille Syrjälä 	 */
488deaeb5b6SVille Syrjälä 	if (intel_crtc_needs_modeset(new_crtc_state))
489deaeb5b6SVille Syrjälä 		crtc_state = new_crtc_state;
4907e50ac42SVille Syrjälä 	else
491deaeb5b6SVille Syrjälä 		crtc_state = old_crtc_state;
492deaeb5b6SVille Syrjälä 
493deaeb5b6SVille Syrjälä 	adjusted_mode = &crtc_state->hw.adjusted_mode;
494deaeb5b6SVille Syrjälä 
495deaeb5b6SVille Syrjälä 	if (crtc->mode_flags & I915_MODE_FLAG_VRR) {
496deaeb5b6SVille Syrjälä 		if (intel_vrr_is_push_sent(crtc_state))
497deaeb5b6SVille Syrjälä 			*vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
498deaeb5b6SVille Syrjälä 		else
499deaeb5b6SVille Syrjälä 			*vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
5007e50ac42SVille Syrjälä 	} else {
5017e50ac42SVille Syrjälä 		*vblank_start = intel_mode_vblank_start(adjusted_mode);
5027e50ac42SVille Syrjälä 	}
5037e50ac42SVille Syrjälä 
5047e50ac42SVille Syrjälä 	/* FIXME needs to be calibrated sensibly */
5057e50ac42SVille Syrjälä 	*min = *vblank_start - intel_usecs_to_scanlines(adjusted_mode,
5067e50ac42SVille Syrjälä 							VBLANK_EVASION_TIME_US);
5077e50ac42SVille Syrjälä 	*max = *vblank_start - 1;
5087e50ac42SVille Syrjälä 
5097e50ac42SVille Syrjälä 	/*
5107e50ac42SVille Syrjälä 	 * M/N is double buffered on the transcoder's undelayed vblank,
5117e50ac42SVille Syrjälä 	 * so with seamless M/N we must evade both vblanks.
5127e50ac42SVille Syrjälä 	 */
513*ccb0934aSVille Syrjälä 	if (new_crtc_state->update_m_n)
5147e50ac42SVille Syrjälä 		*min -= adjusted_mode->crtc_vblank_start - adjusted_mode->crtc_vdisplay;
5157e50ac42SVille Syrjälä }
5167e50ac42SVille Syrjälä 
517d471008bSDave Airlie /**
518d471008bSDave Airlie  * intel_pipe_update_start() - start update of a set of display registers
519e19dc8c4SVille Syrjälä  * @state: the atomic state
520e19dc8c4SVille Syrjälä  * @crtc: the crtc
521d471008bSDave Airlie  *
522d471008bSDave Airlie  * Mark the start of an update to pipe registers that should be updated
523d471008bSDave Airlie  * atomically regarding vblank. If the next vblank will happens within
524d471008bSDave Airlie  * the next 100 us, this function waits until the vblank passes.
525d471008bSDave Airlie  *
526d471008bSDave Airlie  * After a successful call to this function, interrupts will be disabled
527d471008bSDave Airlie  * until a subsequent call to intel_pipe_update_end(). That is done to
528d471008bSDave Airlie  * avoid random delays.
529d471008bSDave Airlie  */
intel_pipe_update_start(struct intel_atomic_state * state,struct intel_crtc * crtc)530e19dc8c4SVille Syrjälä void intel_pipe_update_start(struct intel_atomic_state *state,
531e19dc8c4SVille Syrjälä 			     struct intel_crtc *crtc)
532d471008bSDave Airlie {
533d471008bSDave Airlie 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
534e19dc8c4SVille Syrjälä 	struct intel_crtc_state *new_crtc_state =
535e19dc8c4SVille Syrjälä 		intel_atomic_get_new_crtc_state(state, crtc);
536d471008bSDave Airlie 	long timeout = msecs_to_jiffies_timeout(1);
537d471008bSDave Airlie 	int scanline, min, max, vblank_start;
538d471008bSDave Airlie 	wait_queue_head_t *wq = drm_crtc_vblank_waitqueue(&crtc->base);
539d471008bSDave Airlie 	bool need_vlv_dsi_wa = (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
540d471008bSDave Airlie 		intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI);
541d471008bSDave Airlie 	DEFINE_WAIT(wait);
542d471008bSDave Airlie 
543ce21211bSJosé Roberto de Souza 	intel_psr_lock(new_crtc_state);
544ce21211bSJosé Roberto de Souza 
5452e084371SVille Syrjälä 	if (new_crtc_state->do_async_flip)
546d471008bSDave Airlie 		return;
547d471008bSDave Airlie 
5482bbc6fcaSVille Syrjälä 	if (intel_crtc_needs_vblank_work(new_crtc_state))
5492bbc6fcaSVille Syrjälä 		intel_crtc_vblank_work_init(new_crtc_state);
5502bbc6fcaSVille Syrjälä 
5517e50ac42SVille Syrjälä 	intel_crtc_vblank_evade_scanlines(state, crtc, &min, &max, &vblank_start);
552d471008bSDave Airlie 	if (min <= 0 || max <= 0)
553d471008bSDave Airlie 		goto irq_disable;
554d471008bSDave Airlie 
555d471008bSDave Airlie 	if (drm_WARN_ON(&dev_priv->drm, drm_crtc_vblank_get(&crtc->base)))
556d471008bSDave Airlie 		goto irq_disable;
557d471008bSDave Airlie 
558d471008bSDave Airlie 	/*
559d471008bSDave Airlie 	 * Wait for psr to idle out after enabling the VBL interrupts
560d471008bSDave Airlie 	 * VBL interrupts will start the PSR exit and prevent a PSR
561d471008bSDave Airlie 	 * re-entry as well.
562d471008bSDave Airlie 	 */
5633b6f4095SJosé Roberto de Souza 	intel_psr_wait_for_idle_locked(new_crtc_state);
564d471008bSDave Airlie 
565d471008bSDave Airlie 	local_irq_disable();
566d471008bSDave Airlie 
567d471008bSDave Airlie 	crtc->debug.min_vbl = min;
568d471008bSDave Airlie 	crtc->debug.max_vbl = max;
569d471008bSDave Airlie 	trace_intel_pipe_update_start(crtc);
570d471008bSDave Airlie 
571d471008bSDave Airlie 	for (;;) {
572d471008bSDave Airlie 		/*
573d471008bSDave Airlie 		 * prepare_to_wait() has a memory barrier, which guarantees
574d471008bSDave Airlie 		 * other CPUs can see the task state update by the time we
575d471008bSDave Airlie 		 * read the scanline.
576d471008bSDave Airlie 		 */
577d471008bSDave Airlie 		prepare_to_wait(wq, &wait, TASK_UNINTERRUPTIBLE);
578d471008bSDave Airlie 
579d471008bSDave Airlie 		scanline = intel_get_crtc_scanline(crtc);
580d471008bSDave Airlie 		if (scanline < min || scanline > max)
581d471008bSDave Airlie 			break;
582d471008bSDave Airlie 
583d471008bSDave Airlie 		if (!timeout) {
584d471008bSDave Airlie 			drm_err(&dev_priv->drm,
585d471008bSDave Airlie 				"Potential atomic update failure on pipe %c\n",
586d471008bSDave Airlie 				pipe_name(crtc->pipe));
587d471008bSDave Airlie 			break;
588d471008bSDave Airlie 		}
589d471008bSDave Airlie 
590d471008bSDave Airlie 		local_irq_enable();
591d471008bSDave Airlie 
592d471008bSDave Airlie 		timeout = schedule_timeout(timeout);
593d471008bSDave Airlie 
594d471008bSDave Airlie 		local_irq_disable();
595d471008bSDave Airlie 	}
596d471008bSDave Airlie 
597d471008bSDave Airlie 	finish_wait(wq, &wait);
598d471008bSDave Airlie 
599d471008bSDave Airlie 	drm_crtc_vblank_put(&crtc->base);
600d471008bSDave Airlie 
601d471008bSDave Airlie 	/*
602d471008bSDave Airlie 	 * On VLV/CHV DSI the scanline counter would appear to
603d471008bSDave Airlie 	 * increment approx. 1/3 of a scanline before start of vblank.
604d471008bSDave Airlie 	 * The registers still get latched at start of vblank however.
605d471008bSDave Airlie 	 * This means we must not write any registers on the first
606d471008bSDave Airlie 	 * line of vblank (since not the whole line is actually in
607d471008bSDave Airlie 	 * vblank). And unfortunately we can't use the interrupt to
608d471008bSDave Airlie 	 * wait here since it will fire too soon. We could use the
609d471008bSDave Airlie 	 * frame start interrupt instead since it will fire after the
610d471008bSDave Airlie 	 * critical scanline, but that would require more changes
611d471008bSDave Airlie 	 * in the interrupt code. So for now we'll just do the nasty
612d471008bSDave Airlie 	 * thing and poll for the bad scanline to pass us by.
613d471008bSDave Airlie 	 *
614d471008bSDave Airlie 	 * FIXME figure out if BXT+ DSI suffers from this as well
615d471008bSDave Airlie 	 */
616d471008bSDave Airlie 	while (need_vlv_dsi_wa && scanline == vblank_start)
617d471008bSDave Airlie 		scanline = intel_get_crtc_scanline(crtc);
618d471008bSDave Airlie 
619d471008bSDave Airlie 	crtc->debug.scanline_start = scanline;
620d471008bSDave Airlie 	crtc->debug.start_vbl_time = ktime_get();
621d471008bSDave Airlie 	crtc->debug.start_vbl_count = intel_crtc_get_vblank_counter(crtc);
622d471008bSDave Airlie 
623d471008bSDave Airlie 	trace_intel_pipe_update_vblank_evaded(crtc);
624d471008bSDave Airlie 	return;
625d471008bSDave Airlie 
626d471008bSDave Airlie irq_disable:
627d471008bSDave Airlie 	local_irq_disable();
628d471008bSDave Airlie }
629d471008bSDave Airlie 
630d471008bSDave Airlie #if IS_ENABLED(CONFIG_DRM_I915_DEBUG_VBLANK_EVADE)
dbg_vblank_evade(struct intel_crtc * crtc,ktime_t end)631d471008bSDave Airlie static void dbg_vblank_evade(struct intel_crtc *crtc, ktime_t end)
632d471008bSDave Airlie {
633d471008bSDave Airlie 	u64 delta = ktime_to_ns(ktime_sub(end, crtc->debug.start_vbl_time));
634d471008bSDave Airlie 	unsigned int h;
635d471008bSDave Airlie 
636d471008bSDave Airlie 	h = ilog2(delta >> 9);
637d471008bSDave Airlie 	if (h >= ARRAY_SIZE(crtc->debug.vbl.times))
638d471008bSDave Airlie 		h = ARRAY_SIZE(crtc->debug.vbl.times) - 1;
639d471008bSDave Airlie 	crtc->debug.vbl.times[h]++;
640d471008bSDave Airlie 
641d471008bSDave Airlie 	crtc->debug.vbl.sum += delta;
642d471008bSDave Airlie 	if (!crtc->debug.vbl.min || delta < crtc->debug.vbl.min)
643d471008bSDave Airlie 		crtc->debug.vbl.min = delta;
644d471008bSDave Airlie 	if (delta > crtc->debug.vbl.max)
645d471008bSDave Airlie 		crtc->debug.vbl.max = delta;
646d471008bSDave Airlie 
647d471008bSDave Airlie 	if (delta > 1000 * VBLANK_EVASION_TIME_US) {
648d471008bSDave Airlie 		drm_dbg_kms(crtc->base.dev,
649d471008bSDave Airlie 			    "Atomic update on pipe (%c) took %lld us, max time under evasion is %u us\n",
650d471008bSDave Airlie 			    pipe_name(crtc->pipe),
651d471008bSDave Airlie 			    div_u64(delta, 1000),
652d471008bSDave Airlie 			    VBLANK_EVASION_TIME_US);
653d471008bSDave Airlie 		crtc->debug.vbl.over++;
654d471008bSDave Airlie 	}
655d471008bSDave Airlie }
656d471008bSDave Airlie #else
dbg_vblank_evade(struct intel_crtc * crtc,ktime_t end)657d471008bSDave Airlie static void dbg_vblank_evade(struct intel_crtc *crtc, ktime_t end) {}
658d471008bSDave Airlie #endif
659d471008bSDave Airlie 
660d471008bSDave Airlie /**
661d471008bSDave Airlie  * intel_pipe_update_end() - end update of a set of display registers
662e19dc8c4SVille Syrjälä  * @state: the atomic state
663e19dc8c4SVille Syrjälä  * @crtc: the crtc
664d471008bSDave Airlie  *
665d471008bSDave Airlie  * Mark the end of an update started with intel_pipe_update_start(). This
666d471008bSDave Airlie  * re-enables interrupts and verifies the update was actually completed
667d471008bSDave Airlie  * before a vblank.
668d471008bSDave Airlie  */
intel_pipe_update_end(struct intel_atomic_state * state,struct intel_crtc * crtc)669e19dc8c4SVille Syrjälä void intel_pipe_update_end(struct intel_atomic_state *state,
670e19dc8c4SVille Syrjälä 			   struct intel_crtc *crtc)
671d471008bSDave Airlie {
672e19dc8c4SVille Syrjälä 	struct intel_crtc_state *new_crtc_state =
673e19dc8c4SVille Syrjälä 		intel_atomic_get_new_crtc_state(state, crtc);
674d471008bSDave Airlie 	enum pipe pipe = crtc->pipe;
675d471008bSDave Airlie 	int scanline_end = intel_get_crtc_scanline(crtc);
676d471008bSDave Airlie 	u32 end_vbl_count = intel_crtc_get_vblank_counter(crtc);
677d471008bSDave Airlie 	ktime_t end_vbl_time = ktime_get();
678d471008bSDave Airlie 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
679d471008bSDave Airlie 
680ce21211bSJosé Roberto de Souza 	intel_psr_unlock(new_crtc_state);
681ce21211bSJosé Roberto de Souza 
6822e084371SVille Syrjälä 	if (new_crtc_state->do_async_flip)
683d471008bSDave Airlie 		return;
684d471008bSDave Airlie 
685d471008bSDave Airlie 	trace_intel_pipe_update_end(crtc, end_vbl_count, scanline_end);
686d471008bSDave Airlie 
687d471008bSDave Airlie 	/*
688d471008bSDave Airlie 	 * Incase of mipi dsi command mode, we need to set frame update
689d471008bSDave Airlie 	 * request for every commit.
690d471008bSDave Airlie 	 */
691005e9537SMatt Roper 	if (DISPLAY_VER(dev_priv) >= 11 &&
692d471008bSDave Airlie 	    intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
693d471008bSDave Airlie 		icl_dsi_frame_update(new_crtc_state);
694d471008bSDave Airlie 
695d471008bSDave Airlie 	/* We're still in the vblank-evade critical section, this can't race.
696d471008bSDave Airlie 	 * Would be slightly nice to just grab the vblank count and arm the
697d471008bSDave Airlie 	 * event outside of the critical section - the spinlock might spin for a
698d471008bSDave Airlie 	 * while ... */
6992bbc6fcaSVille Syrjälä 	if (intel_crtc_needs_vblank_work(new_crtc_state)) {
7002bbc6fcaSVille Syrjälä 		drm_vblank_work_schedule(&new_crtc_state->vblank_work,
7012bbc6fcaSVille Syrjälä 					 drm_crtc_accurate_vblank_count(&crtc->base) + 1,
7022bbc6fcaSVille Syrjälä 					 false);
7032bbc6fcaSVille Syrjälä 	} else if (new_crtc_state->uapi.event) {
704d471008bSDave Airlie 		drm_WARN_ON(&dev_priv->drm,
705d471008bSDave Airlie 			    drm_crtc_vblank_get(&crtc->base) != 0);
706d471008bSDave Airlie 
707d471008bSDave Airlie 		spin_lock(&crtc->base.dev->event_lock);
708d471008bSDave Airlie 		drm_crtc_arm_vblank_event(&crtc->base,
709d471008bSDave Airlie 					  new_crtc_state->uapi.event);
710d471008bSDave Airlie 		spin_unlock(&crtc->base.dev->event_lock);
711d471008bSDave Airlie 
712d471008bSDave Airlie 		new_crtc_state->uapi.event = NULL;
713d471008bSDave Airlie 	}
714d471008bSDave Airlie 
7154765d061SVille Syrjälä 	/*
7164765d061SVille Syrjälä 	 * Send VRR Push to terminate Vblank. If we are already in vblank
7174765d061SVille Syrjälä 	 * this has to be done _after_ sampling the frame counter, as
7184765d061SVille Syrjälä 	 * otherwise the push would immediately terminate the vblank and
7194765d061SVille Syrjälä 	 * the sampled frame counter would correspond to the next frame
7204765d061SVille Syrjälä 	 * instead of the current frame.
7214765d061SVille Syrjälä 	 *
7224765d061SVille Syrjälä 	 * There is a tiny race here (iff vblank evasion failed us) where
7234765d061SVille Syrjälä 	 * we might sample the frame counter just before vmax vblank start
7244765d061SVille Syrjälä 	 * but the push would be sent just after it. That would cause the
7254765d061SVille Syrjälä 	 * push to affect the next frame instead of the current frame,
7264765d061SVille Syrjälä 	 * which would cause the next frame to terminate already at vmin
7274765d061SVille Syrjälä 	 * vblank start instead of vmax vblank start.
7284765d061SVille Syrjälä 	 */
7294765d061SVille Syrjälä 	intel_vrr_send_push(new_crtc_state);
7304765d061SVille Syrjälä 
731d471008bSDave Airlie 	local_irq_enable();
732d471008bSDave Airlie 
733d471008bSDave Airlie 	if (intel_vgpu_active(dev_priv))
734d471008bSDave Airlie 		return;
735d471008bSDave Airlie 
736d471008bSDave Airlie 	if (crtc->debug.start_vbl_count &&
737d471008bSDave Airlie 	    crtc->debug.start_vbl_count != end_vbl_count) {
738d471008bSDave Airlie 		drm_err(&dev_priv->drm,
739d471008bSDave Airlie 			"Atomic update failure on pipe %c (start=%u end=%u) time %lld us, min %d, max %d, scanline start %d, end %d\n",
740d471008bSDave Airlie 			pipe_name(pipe), crtc->debug.start_vbl_count,
741d471008bSDave Airlie 			end_vbl_count,
742d471008bSDave Airlie 			ktime_us_delta(end_vbl_time,
743d471008bSDave Airlie 				       crtc->debug.start_vbl_time),
744d471008bSDave Airlie 			crtc->debug.min_vbl, crtc->debug.max_vbl,
745d471008bSDave Airlie 			crtc->debug.scanline_start, scanline_end);
746d471008bSDave Airlie 	}
747d471008bSDave Airlie 
748d471008bSDave Airlie 	dbg_vblank_evade(crtc, end_vbl_time);
749d471008bSDave Airlie }
750