xref: /linux/drivers/gpu/drm/i915/display/intel_display.c (revision 3027ce13e04eee76539ca65c2cb1028a01c8c508)
1 /*
2  * Copyright © 2006-2007 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Authors:
24  *	Eric Anholt <eric@anholt.net>
25  */
26 
27 #include <linux/dma-resv.h>
28 #include <linux/i2c.h>
29 #include <linux/input.h>
30 #include <linux/kernel.h>
31 #include <linux/module.h>
32 #include <linux/slab.h>
33 #include <linux/string_helpers.h>
34 
35 #include <drm/display/drm_dp_helper.h>
36 #include <drm/display/drm_dp_tunnel.h>
37 #include <drm/drm_atomic.h>
38 #include <drm/drm_atomic_helper.h>
39 #include <drm/drm_atomic_uapi.h>
40 #include <drm/drm_damage_helper.h>
41 #include <drm/drm_edid.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_probe_helper.h>
44 #include <drm/drm_rect.h>
45 
46 #include "gem/i915_gem_lmem.h"
47 #include "gem/i915_gem_object.h"
48 
49 #include "g4x_dp.h"
50 #include "g4x_hdmi.h"
51 #include "hsw_ips.h"
52 #include "i915_config.h"
53 #include "i915_drv.h"
54 #include "i915_reg.h"
55 #include "i915_utils.h"
56 #include "i9xx_plane.h"
57 #include "i9xx_wm.h"
58 #include "intel_atomic.h"
59 #include "intel_atomic_plane.h"
60 #include "intel_audio.h"
61 #include "intel_bw.h"
62 #include "intel_cdclk.h"
63 #include "intel_clock_gating.h"
64 #include "intel_color.h"
65 #include "intel_crt.h"
66 #include "intel_crtc.h"
67 #include "intel_crtc_state_dump.h"
68 #include "intel_ddi.h"
69 #include "intel_de.h"
70 #include "intel_display_driver.h"
71 #include "intel_display_power.h"
72 #include "intel_display_types.h"
73 #include "intel_dmc.h"
74 #include "intel_dp.h"
75 #include "intel_dp_link_training.h"
76 #include "intel_dp_mst.h"
77 #include "intel_dp_tunnel.h"
78 #include "intel_dpll.h"
79 #include "intel_dpll_mgr.h"
80 #include "intel_dpt.h"
81 #include "intel_dpt_common.h"
82 #include "intel_drrs.h"
83 #include "intel_dsb.h"
84 #include "intel_dsi.h"
85 #include "intel_dvo.h"
86 #include "intel_fb.h"
87 #include "intel_fbc.h"
88 #include "intel_fbdev.h"
89 #include "intel_fdi.h"
90 #include "intel_fifo_underrun.h"
91 #include "intel_frontbuffer.h"
92 #include "intel_hdmi.h"
93 #include "intel_hotplug.h"
94 #include "intel_link_bw.h"
95 #include "intel_lvds.h"
96 #include "intel_lvds_regs.h"
97 #include "intel_modeset_setup.h"
98 #include "intel_modeset_verify.h"
99 #include "intel_overlay.h"
100 #include "intel_panel.h"
101 #include "intel_pch_display.h"
102 #include "intel_pch_refclk.h"
103 #include "intel_pcode.h"
104 #include "intel_pipe_crc.h"
105 #include "intel_plane_initial.h"
106 #include "intel_pmdemand.h"
107 #include "intel_pps.h"
108 #include "intel_psr.h"
109 #include "intel_psr_regs.h"
110 #include "intel_sdvo.h"
111 #include "intel_snps_phy.h"
112 #include "intel_tc.h"
113 #include "intel_tv.h"
114 #include "intel_vblank.h"
115 #include "intel_vdsc.h"
116 #include "intel_vdsc_regs.h"
117 #include "intel_vga.h"
118 #include "intel_vrr.h"
119 #include "intel_wm.h"
120 #include "skl_scaler.h"
121 #include "skl_universal_plane.h"
122 #include "skl_watermark.h"
123 #include "vlv_dsi.h"
124 #include "vlv_dsi_pll.h"
125 #include "vlv_dsi_regs.h"
126 #include "vlv_sideband.h"
127 
128 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
129 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
130 static void hsw_set_transconf(const struct intel_crtc_state *crtc_state);
131 static void bdw_set_pipe_misc(const struct intel_crtc_state *crtc_state);
132 
133 /* returns HPLL frequency in kHz */
134 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
135 {
136 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
137 
138 	/* Obtain SKU information */
139 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
140 		CCK_FUSE_HPLL_FREQ_MASK;
141 
142 	return vco_freq[hpll_freq] * 1000;
143 }
144 
145 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
146 		      const char *name, u32 reg, int ref_freq)
147 {
148 	u32 val;
149 	int divider;
150 
151 	val = vlv_cck_read(dev_priv, reg);
152 	divider = val & CCK_FREQUENCY_VALUES;
153 
154 	drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
155 		 (divider << CCK_FREQUENCY_STATUS_SHIFT),
156 		 "%s change in progress\n", name);
157 
158 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
159 }
160 
161 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
162 			   const char *name, u32 reg)
163 {
164 	int hpll;
165 
166 	vlv_cck_get(dev_priv);
167 
168 	if (dev_priv->hpll_freq == 0)
169 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
170 
171 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
172 
173 	vlv_cck_put(dev_priv);
174 
175 	return hpll;
176 }
177 
178 void intel_update_czclk(struct drm_i915_private *dev_priv)
179 {
180 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
181 		return;
182 
183 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
184 						      CCK_CZ_CLOCK_CONTROL);
185 
186 	drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
187 		dev_priv->czclk_freq);
188 }
189 
190 static bool is_hdr_mode(const struct intel_crtc_state *crtc_state)
191 {
192 	return (crtc_state->active_planes &
193 		~(icl_hdr_plane_mask() | BIT(PLANE_CURSOR))) == 0;
194 }
195 
196 /* WA Display #0827: Gen9:all */
197 static void
198 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
199 {
200 	intel_de_rmw(dev_priv, CLKGATE_DIS_PSL(pipe),
201 		     DUPS1_GATING_DIS | DUPS2_GATING_DIS,
202 		     enable ? DUPS1_GATING_DIS | DUPS2_GATING_DIS : 0);
203 }
204 
205 /* Wa_2006604312:icl,ehl */
206 static void
207 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
208 		       bool enable)
209 {
210 	intel_de_rmw(dev_priv, CLKGATE_DIS_PSL(pipe),
211 		     DPFR_GATING_DIS,
212 		     enable ? DPFR_GATING_DIS : 0);
213 }
214 
215 /* Wa_1604331009:icl,jsl,ehl */
216 static void
217 icl_wa_cursorclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
218 		       bool enable)
219 {
220 	intel_de_rmw(dev_priv, CLKGATE_DIS_PSL(pipe),
221 		     CURSOR_GATING_DIS,
222 		     enable ? CURSOR_GATING_DIS : 0);
223 }
224 
225 static bool
226 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
227 {
228 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
229 }
230 
231 bool
232 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
233 {
234 	return crtc_state->sync_mode_slaves_mask != 0;
235 }
236 
237 bool
238 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
239 {
240 	return is_trans_port_sync_master(crtc_state) ||
241 		is_trans_port_sync_slave(crtc_state);
242 }
243 
244 static enum pipe bigjoiner_master_pipe(const struct intel_crtc_state *crtc_state)
245 {
246 	return ffs(crtc_state->bigjoiner_pipes) - 1;
247 }
248 
249 u8 intel_crtc_bigjoiner_slave_pipes(const struct intel_crtc_state *crtc_state)
250 {
251 	if (crtc_state->bigjoiner_pipes)
252 		return crtc_state->bigjoiner_pipes & ~BIT(bigjoiner_master_pipe(crtc_state));
253 	else
254 		return 0;
255 }
256 
257 bool intel_crtc_is_bigjoiner_slave(const struct intel_crtc_state *crtc_state)
258 {
259 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
260 
261 	return crtc_state->bigjoiner_pipes &&
262 		crtc->pipe != bigjoiner_master_pipe(crtc_state);
263 }
264 
265 bool intel_crtc_is_bigjoiner_master(const struct intel_crtc_state *crtc_state)
266 {
267 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
268 
269 	return crtc_state->bigjoiner_pipes &&
270 		crtc->pipe == bigjoiner_master_pipe(crtc_state);
271 }
272 
273 static int intel_bigjoiner_num_pipes(const struct intel_crtc_state *crtc_state)
274 {
275 	return hweight8(crtc_state->bigjoiner_pipes);
276 }
277 
278 u8 intel_crtc_joined_pipe_mask(const struct intel_crtc_state *crtc_state)
279 {
280 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
281 
282 	return BIT(crtc->pipe) | crtc_state->bigjoiner_pipes;
283 }
284 
285 struct intel_crtc *intel_master_crtc(const struct intel_crtc_state *crtc_state)
286 {
287 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
288 
289 	if (intel_crtc_is_bigjoiner_slave(crtc_state))
290 		return intel_crtc_for_pipe(i915, bigjoiner_master_pipe(crtc_state));
291 	else
292 		return to_intel_crtc(crtc_state->uapi.crtc);
293 }
294 
295 static void
296 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
297 {
298 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
299 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
300 
301 	if (DISPLAY_VER(dev_priv) >= 4) {
302 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
303 
304 		/* Wait for the Pipe State to go off */
305 		if (intel_de_wait_for_clear(dev_priv, TRANSCONF(cpu_transcoder),
306 					    TRANSCONF_STATE_ENABLE, 100))
307 			drm_WARN(&dev_priv->drm, 1, "pipe_off wait timed out\n");
308 	} else {
309 		intel_wait_for_pipe_scanline_stopped(crtc);
310 	}
311 }
312 
313 void assert_transcoder(struct drm_i915_private *dev_priv,
314 		       enum transcoder cpu_transcoder, bool state)
315 {
316 	bool cur_state;
317 	enum intel_display_power_domain power_domain;
318 	intel_wakeref_t wakeref;
319 
320 	/* we keep both pipes enabled on 830 */
321 	if (IS_I830(dev_priv))
322 		state = true;
323 
324 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
325 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
326 	if (wakeref) {
327 		u32 val = intel_de_read(dev_priv, TRANSCONF(cpu_transcoder));
328 		cur_state = !!(val & TRANSCONF_ENABLE);
329 
330 		intel_display_power_put(dev_priv, power_domain, wakeref);
331 	} else {
332 		cur_state = false;
333 	}
334 
335 	I915_STATE_WARN(dev_priv, cur_state != state,
336 			"transcoder %s assertion failure (expected %s, current %s)\n",
337 			transcoder_name(cpu_transcoder), str_on_off(state),
338 			str_on_off(cur_state));
339 }
340 
341 static void assert_plane(struct intel_plane *plane, bool state)
342 {
343 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
344 	enum pipe pipe;
345 	bool cur_state;
346 
347 	cur_state = plane->get_hw_state(plane, &pipe);
348 
349 	I915_STATE_WARN(i915, cur_state != state,
350 			"%s assertion failure (expected %s, current %s)\n",
351 			plane->base.name, str_on_off(state),
352 			str_on_off(cur_state));
353 }
354 
355 #define assert_plane_enabled(p) assert_plane(p, true)
356 #define assert_plane_disabled(p) assert_plane(p, false)
357 
358 static void assert_planes_disabled(struct intel_crtc *crtc)
359 {
360 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
361 	struct intel_plane *plane;
362 
363 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
364 		assert_plane_disabled(plane);
365 }
366 
367 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
368 			 struct intel_digital_port *dig_port,
369 			 unsigned int expected_mask)
370 {
371 	u32 port_mask;
372 	i915_reg_t dpll_reg;
373 
374 	switch (dig_port->base.port) {
375 	default:
376 		MISSING_CASE(dig_port->base.port);
377 		fallthrough;
378 	case PORT_B:
379 		port_mask = DPLL_PORTB_READY_MASK;
380 		dpll_reg = DPLL(0);
381 		break;
382 	case PORT_C:
383 		port_mask = DPLL_PORTC_READY_MASK;
384 		dpll_reg = DPLL(0);
385 		expected_mask <<= 4;
386 		break;
387 	case PORT_D:
388 		port_mask = DPLL_PORTD_READY_MASK;
389 		dpll_reg = DPIO_PHY_STATUS;
390 		break;
391 	}
392 
393 	if (intel_de_wait(dev_priv, dpll_reg, port_mask, expected_mask, 1000))
394 		drm_WARN(&dev_priv->drm, 1,
395 			 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
396 			 dig_port->base.base.base.id, dig_port->base.base.name,
397 			 intel_de_read(dev_priv, dpll_reg) & port_mask,
398 			 expected_mask);
399 }
400 
401 void intel_enable_transcoder(const struct intel_crtc_state *new_crtc_state)
402 {
403 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
404 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
405 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
406 	enum pipe pipe = crtc->pipe;
407 	u32 val;
408 
409 	drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
410 
411 	assert_planes_disabled(crtc);
412 
413 	/*
414 	 * A pipe without a PLL won't actually be able to drive bits from
415 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
416 	 * need the check.
417 	 */
418 	if (HAS_GMCH(dev_priv)) {
419 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
420 			assert_dsi_pll_enabled(dev_priv);
421 		else
422 			assert_pll_enabled(dev_priv, pipe);
423 	} else {
424 		if (new_crtc_state->has_pch_encoder) {
425 			/* if driving the PCH, we need FDI enabled */
426 			assert_fdi_rx_pll_enabled(dev_priv,
427 						  intel_crtc_pch_transcoder(crtc));
428 			assert_fdi_tx_pll_enabled(dev_priv,
429 						  (enum pipe) cpu_transcoder);
430 		}
431 		/* FIXME: assert CPU port conditions for SNB+ */
432 	}
433 
434 	/* Wa_22012358565:adl-p */
435 	if (DISPLAY_VER(dev_priv) == 13)
436 		intel_de_rmw(dev_priv, PIPE_ARB_CTL(pipe),
437 			     0, PIPE_ARB_USE_PROG_SLOTS);
438 
439 	if (DISPLAY_VER(dev_priv) >= 14) {
440 		u32 clear = DP_DSC_INSERT_SF_AT_EOL_WA;
441 		u32 set = 0;
442 
443 		if (DISPLAY_VER(dev_priv) == 14)
444 			set |= DP_FEC_BS_JITTER_WA;
445 
446 		intel_de_rmw(dev_priv,
447 			     hsw_chicken_trans_reg(dev_priv, cpu_transcoder),
448 			     clear, set);
449 	}
450 
451 	val = intel_de_read(dev_priv, TRANSCONF(cpu_transcoder));
452 	if (val & TRANSCONF_ENABLE) {
453 		/* we keep both pipes enabled on 830 */
454 		drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
455 		return;
456 	}
457 
458 	/* Wa_1409098942:adlp+ */
459 	if (DISPLAY_VER(dev_priv) >= 13 &&
460 	    new_crtc_state->dsc.compression_enable) {
461 		val &= ~TRANSCONF_PIXEL_COUNT_SCALING_MASK;
462 		val |= REG_FIELD_PREP(TRANSCONF_PIXEL_COUNT_SCALING_MASK,
463 				      TRANSCONF_PIXEL_COUNT_SCALING_X4);
464 	}
465 
466 	intel_de_write(dev_priv, TRANSCONF(cpu_transcoder),
467 		       val | TRANSCONF_ENABLE);
468 	intel_de_posting_read(dev_priv, TRANSCONF(cpu_transcoder));
469 
470 	/*
471 	 * Until the pipe starts PIPEDSL reads will return a stale value,
472 	 * which causes an apparent vblank timestamp jump when PIPEDSL
473 	 * resets to its proper value. That also messes up the frame count
474 	 * when it's derived from the timestamps. So let's wait for the
475 	 * pipe to start properly before we call drm_crtc_vblank_on()
476 	 */
477 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
478 		intel_wait_for_pipe_scanline_moving(crtc);
479 }
480 
481 void intel_disable_transcoder(const struct intel_crtc_state *old_crtc_state)
482 {
483 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
484 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
485 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
486 	enum pipe pipe = crtc->pipe;
487 	u32 val;
488 
489 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
490 
491 	/*
492 	 * Make sure planes won't keep trying to pump pixels to us,
493 	 * or we might hang the display.
494 	 */
495 	assert_planes_disabled(crtc);
496 
497 	val = intel_de_read(dev_priv, TRANSCONF(cpu_transcoder));
498 	if ((val & TRANSCONF_ENABLE) == 0)
499 		return;
500 
501 	/*
502 	 * Double wide has implications for planes
503 	 * so best keep it disabled when not needed.
504 	 */
505 	if (old_crtc_state->double_wide)
506 		val &= ~TRANSCONF_DOUBLE_WIDE;
507 
508 	/* Don't disable pipe or pipe PLLs if needed */
509 	if (!IS_I830(dev_priv))
510 		val &= ~TRANSCONF_ENABLE;
511 
512 	/* Wa_1409098942:adlp+ */
513 	if (DISPLAY_VER(dev_priv) >= 13 &&
514 	    old_crtc_state->dsc.compression_enable)
515 		val &= ~TRANSCONF_PIXEL_COUNT_SCALING_MASK;
516 
517 	intel_de_write(dev_priv, TRANSCONF(cpu_transcoder), val);
518 
519 	if (DISPLAY_VER(dev_priv) >= 12)
520 		intel_de_rmw(dev_priv, hsw_chicken_trans_reg(dev_priv, cpu_transcoder),
521 			     FECSTALL_DIS_DPTSTREAM_DPTTG, 0);
522 
523 	if ((val & TRANSCONF_ENABLE) == 0)
524 		intel_wait_for_pipe_off(old_crtc_state);
525 }
526 
527 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
528 {
529 	unsigned int size = 0;
530 	int i;
531 
532 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
533 		size += rot_info->plane[i].dst_stride * rot_info->plane[i].width;
534 
535 	return size;
536 }
537 
538 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
539 {
540 	unsigned int size = 0;
541 	int i;
542 
543 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++) {
544 		unsigned int plane_size;
545 
546 		if (rem_info->plane[i].linear)
547 			plane_size = rem_info->plane[i].size;
548 		else
549 			plane_size = rem_info->plane[i].dst_stride * rem_info->plane[i].height;
550 
551 		if (plane_size == 0)
552 			continue;
553 
554 		if (rem_info->plane_alignment)
555 			size = ALIGN(size, rem_info->plane_alignment);
556 
557 		size += plane_size;
558 	}
559 
560 	return size;
561 }
562 
563 bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
564 {
565 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
566 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
567 
568 	return DISPLAY_VER(dev_priv) < 4 ||
569 		(plane->fbc && !plane_state->no_fbc_reason &&
570 		 plane_state->view.gtt.type == I915_GTT_VIEW_NORMAL);
571 }
572 
573 /*
574  * Convert the x/y offsets into a linear offset.
575  * Only valid with 0/180 degree rotation, which is fine since linear
576  * offset is only used with linear buffers on pre-hsw and tiled buffers
577  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
578  */
579 u32 intel_fb_xy_to_linear(int x, int y,
580 			  const struct intel_plane_state *state,
581 			  int color_plane)
582 {
583 	const struct drm_framebuffer *fb = state->hw.fb;
584 	unsigned int cpp = fb->format->cpp[color_plane];
585 	unsigned int pitch = state->view.color_plane[color_plane].mapping_stride;
586 
587 	return y * pitch + x * cpp;
588 }
589 
590 /*
591  * Add the x/y offsets derived from fb->offsets[] to the user
592  * specified plane src x/y offsets. The resulting x/y offsets
593  * specify the start of scanout from the beginning of the gtt mapping.
594  */
595 void intel_add_fb_offsets(int *x, int *y,
596 			  const struct intel_plane_state *state,
597 			  int color_plane)
598 
599 {
600 	*x += state->view.color_plane[color_plane].x;
601 	*y += state->view.color_plane[color_plane].y;
602 }
603 
604 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
605 			      u32 pixel_format, u64 modifier)
606 {
607 	struct intel_crtc *crtc;
608 	struct intel_plane *plane;
609 
610 	if (!HAS_DISPLAY(dev_priv))
611 		return 0;
612 
613 	/*
614 	 * We assume the primary plane for pipe A has
615 	 * the highest stride limits of them all,
616 	 * if in case pipe A is disabled, use the first pipe from pipe_mask.
617 	 */
618 	crtc = intel_first_crtc(dev_priv);
619 	if (!crtc)
620 		return 0;
621 
622 	plane = to_intel_plane(crtc->base.primary);
623 
624 	return plane->max_stride(plane, pixel_format, modifier,
625 				 DRM_MODE_ROTATE_0);
626 }
627 
628 void intel_set_plane_visible(struct intel_crtc_state *crtc_state,
629 			     struct intel_plane_state *plane_state,
630 			     bool visible)
631 {
632 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
633 
634 	plane_state->uapi.visible = visible;
635 
636 	if (visible)
637 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
638 	else
639 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
640 }
641 
642 void intel_plane_fixup_bitmasks(struct intel_crtc_state *crtc_state)
643 {
644 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
645 	struct drm_plane *plane;
646 
647 	/*
648 	 * Active_planes aliases if multiple "primary" or cursor planes
649 	 * have been used on the same (or wrong) pipe. plane_mask uses
650 	 * unique ids, hence we can use that to reconstruct active_planes.
651 	 */
652 	crtc_state->enabled_planes = 0;
653 	crtc_state->active_planes = 0;
654 
655 	drm_for_each_plane_mask(plane, &dev_priv->drm,
656 				crtc_state->uapi.plane_mask) {
657 		crtc_state->enabled_planes |= BIT(to_intel_plane(plane)->id);
658 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
659 	}
660 }
661 
662 void intel_plane_disable_noatomic(struct intel_crtc *crtc,
663 				  struct intel_plane *plane)
664 {
665 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
666 	struct intel_crtc_state *crtc_state =
667 		to_intel_crtc_state(crtc->base.state);
668 	struct intel_plane_state *plane_state =
669 		to_intel_plane_state(plane->base.state);
670 
671 	drm_dbg_kms(&dev_priv->drm,
672 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
673 		    plane->base.base.id, plane->base.name,
674 		    crtc->base.base.id, crtc->base.name);
675 
676 	intel_set_plane_visible(crtc_state, plane_state, false);
677 	intel_plane_fixup_bitmasks(crtc_state);
678 	crtc_state->data_rate[plane->id] = 0;
679 	crtc_state->data_rate_y[plane->id] = 0;
680 	crtc_state->rel_data_rate[plane->id] = 0;
681 	crtc_state->rel_data_rate_y[plane->id] = 0;
682 	crtc_state->min_cdclk[plane->id] = 0;
683 
684 	if ((crtc_state->active_planes & ~BIT(PLANE_CURSOR)) == 0 &&
685 	    hsw_ips_disable(crtc_state)) {
686 		crtc_state->ips_enabled = false;
687 		intel_crtc_wait_for_next_vblank(crtc);
688 	}
689 
690 	/*
691 	 * Vblank time updates from the shadow to live plane control register
692 	 * are blocked if the memory self-refresh mode is active at that
693 	 * moment. So to make sure the plane gets truly disabled, disable
694 	 * first the self-refresh mode. The self-refresh enable bit in turn
695 	 * will be checked/applied by the HW only at the next frame start
696 	 * event which is after the vblank start event, so we need to have a
697 	 * wait-for-vblank between disabling the plane and the pipe.
698 	 */
699 	if (HAS_GMCH(dev_priv) &&
700 	    intel_set_memory_cxsr(dev_priv, false))
701 		intel_crtc_wait_for_next_vblank(crtc);
702 
703 	/*
704 	 * Gen2 reports pipe underruns whenever all planes are disabled.
705 	 * So disable underrun reporting before all the planes get disabled.
706 	 */
707 	if (DISPLAY_VER(dev_priv) == 2 && !crtc_state->active_planes)
708 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
709 
710 	intel_plane_disable_arm(plane, crtc_state);
711 	intel_crtc_wait_for_next_vblank(crtc);
712 }
713 
714 unsigned int
715 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
716 {
717 	int x = 0, y = 0;
718 
719 	intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
720 					  plane_state->view.color_plane[0].offset, 0);
721 
722 	return y;
723 }
724 
725 static void icl_set_pipe_chicken(const struct intel_crtc_state *crtc_state)
726 {
727 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
728 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
729 	enum pipe pipe = crtc->pipe;
730 	u32 tmp;
731 
732 	tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
733 
734 	/*
735 	 * Display WA #1153: icl
736 	 * enable hardware to bypass the alpha math
737 	 * and rounding for per-pixel values 00 and 0xff
738 	 */
739 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
740 	/*
741 	 * Display WA # 1605353570: icl
742 	 * Set the pixel rounding bit to 1 for allowing
743 	 * passthrough of Frame buffer pixels unmodified
744 	 * across pipe
745 	 */
746 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
747 
748 	/*
749 	 * Underrun recovery must always be disabled on display 13+.
750 	 * DG2 chicken bit meaning is inverted compared to other platforms.
751 	 */
752 	if (IS_DG2(dev_priv))
753 		tmp &= ~UNDERRUN_RECOVERY_ENABLE_DG2;
754 	else if (DISPLAY_VER(dev_priv) >= 13)
755 		tmp |= UNDERRUN_RECOVERY_DISABLE_ADLP;
756 
757 	/* Wa_14010547955:dg2 */
758 	if (IS_DG2(dev_priv))
759 		tmp |= DG2_RENDER_CCSTAG_4_3_EN;
760 
761 	intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
762 }
763 
764 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
765 {
766 	struct drm_crtc *crtc;
767 	bool cleanup_done;
768 
769 	drm_for_each_crtc(crtc, &dev_priv->drm) {
770 		struct drm_crtc_commit *commit;
771 		spin_lock(&crtc->commit_lock);
772 		commit = list_first_entry_or_null(&crtc->commit_list,
773 						  struct drm_crtc_commit, commit_entry);
774 		cleanup_done = commit ?
775 			try_wait_for_completion(&commit->cleanup_done) : true;
776 		spin_unlock(&crtc->commit_lock);
777 
778 		if (cleanup_done)
779 			continue;
780 
781 		intel_crtc_wait_for_next_vblank(to_intel_crtc(crtc));
782 
783 		return true;
784 	}
785 
786 	return false;
787 }
788 
789 /*
790  * Finds the encoder associated with the given CRTC. This can only be
791  * used when we know that the CRTC isn't feeding multiple encoders!
792  */
793 struct intel_encoder *
794 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
795 			   const struct intel_crtc_state *crtc_state)
796 {
797 	const struct drm_connector_state *connector_state;
798 	const struct drm_connector *connector;
799 	struct intel_encoder *encoder = NULL;
800 	struct intel_crtc *master_crtc;
801 	int num_encoders = 0;
802 	int i;
803 
804 	master_crtc = intel_master_crtc(crtc_state);
805 
806 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
807 		if (connector_state->crtc != &master_crtc->base)
808 			continue;
809 
810 		encoder = to_intel_encoder(connector_state->best_encoder);
811 		num_encoders++;
812 	}
813 
814 	drm_WARN(state->base.dev, num_encoders != 1,
815 		 "%d encoders for pipe %c\n",
816 		 num_encoders, pipe_name(master_crtc->pipe));
817 
818 	return encoder;
819 }
820 
821 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
822 {
823 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
824 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
825 	const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
826 	enum pipe pipe = crtc->pipe;
827 	int width = drm_rect_width(dst);
828 	int height = drm_rect_height(dst);
829 	int x = dst->x1;
830 	int y = dst->y1;
831 
832 	if (!crtc_state->pch_pfit.enabled)
833 		return;
834 
835 	/* Force use of hard-coded filter coefficients
836 	 * as some pre-programmed values are broken,
837 	 * e.g. x201.
838 	 */
839 	if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
840 		intel_de_write_fw(dev_priv, PF_CTL(pipe), PF_ENABLE |
841 				  PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
842 	else
843 		intel_de_write_fw(dev_priv, PF_CTL(pipe), PF_ENABLE |
844 				  PF_FILTER_MED_3x3);
845 	intel_de_write_fw(dev_priv, PF_WIN_POS(pipe),
846 			  PF_WIN_XPOS(x) | PF_WIN_YPOS(y));
847 	intel_de_write_fw(dev_priv, PF_WIN_SZ(pipe),
848 			  PF_WIN_XSIZE(width) | PF_WIN_YSIZE(height));
849 }
850 
851 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *crtc)
852 {
853 	if (crtc->overlay)
854 		(void) intel_overlay_switch_off(crtc->overlay);
855 
856 	/* Let userspace switch the overlay on again. In most cases userspace
857 	 * has to recompute where to put it anyway.
858 	 */
859 }
860 
861 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
862 {
863 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
864 
865 	if (!crtc_state->nv12_planes)
866 		return false;
867 
868 	/* WA Display #0827: Gen9:all */
869 	if (DISPLAY_VER(dev_priv) == 9)
870 		return true;
871 
872 	return false;
873 }
874 
875 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
876 {
877 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
878 
879 	/* Wa_2006604312:icl,ehl */
880 	if (crtc_state->scaler_state.scaler_users > 0 && DISPLAY_VER(dev_priv) == 11)
881 		return true;
882 
883 	return false;
884 }
885 
886 static bool needs_cursorclk_wa(const struct intel_crtc_state *crtc_state)
887 {
888 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
889 
890 	/* Wa_1604331009:icl,jsl,ehl */
891 	if (is_hdr_mode(crtc_state) &&
892 	    crtc_state->active_planes & BIT(PLANE_CURSOR) &&
893 	    DISPLAY_VER(dev_priv) == 11)
894 		return true;
895 
896 	return false;
897 }
898 
899 static void intel_async_flip_vtd_wa(struct drm_i915_private *i915,
900 				    enum pipe pipe, bool enable)
901 {
902 	if (DISPLAY_VER(i915) == 9) {
903 		/*
904 		 * "Plane N strech max must be programmed to 11b (x1)
905 		 *  when Async flips are enabled on that plane."
906 		 */
907 		intel_de_rmw(i915, CHICKEN_PIPESL_1(pipe),
908 			     SKL_PLANE1_STRETCH_MAX_MASK,
909 			     enable ? SKL_PLANE1_STRETCH_MAX_X1 : SKL_PLANE1_STRETCH_MAX_X8);
910 	} else {
911 		/* Also needed on HSW/BDW albeit undocumented */
912 		intel_de_rmw(i915, CHICKEN_PIPESL_1(pipe),
913 			     HSW_PRI_STRETCH_MAX_MASK,
914 			     enable ? HSW_PRI_STRETCH_MAX_X1 : HSW_PRI_STRETCH_MAX_X8);
915 	}
916 }
917 
918 static bool needs_async_flip_vtd_wa(const struct intel_crtc_state *crtc_state)
919 {
920 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
921 
922 	return crtc_state->uapi.async_flip && i915_vtd_active(i915) &&
923 		(DISPLAY_VER(i915) == 9 || IS_BROADWELL(i915) || IS_HASWELL(i915));
924 }
925 
926 static void intel_encoders_audio_enable(struct intel_atomic_state *state,
927 					struct intel_crtc *crtc)
928 {
929 	const struct intel_crtc_state *crtc_state =
930 		intel_atomic_get_new_crtc_state(state, crtc);
931 	const struct drm_connector_state *conn_state;
932 	struct drm_connector *conn;
933 	int i;
934 
935 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
936 		struct intel_encoder *encoder =
937 			to_intel_encoder(conn_state->best_encoder);
938 
939 		if (conn_state->crtc != &crtc->base)
940 			continue;
941 
942 		if (encoder->audio_enable)
943 			encoder->audio_enable(encoder, crtc_state, conn_state);
944 	}
945 }
946 
947 static void intel_encoders_audio_disable(struct intel_atomic_state *state,
948 					 struct intel_crtc *crtc)
949 {
950 	const struct intel_crtc_state *old_crtc_state =
951 		intel_atomic_get_old_crtc_state(state, crtc);
952 	const struct drm_connector_state *old_conn_state;
953 	struct drm_connector *conn;
954 	int i;
955 
956 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
957 		struct intel_encoder *encoder =
958 			to_intel_encoder(old_conn_state->best_encoder);
959 
960 		if (old_conn_state->crtc != &crtc->base)
961 			continue;
962 
963 		if (encoder->audio_disable)
964 			encoder->audio_disable(encoder, old_crtc_state, old_conn_state);
965 	}
966 }
967 
968 #define is_enabling(feature, old_crtc_state, new_crtc_state) \
969 	((!(old_crtc_state)->feature || intel_crtc_needs_modeset(new_crtc_state)) && \
970 	 (new_crtc_state)->feature)
971 #define is_disabling(feature, old_crtc_state, new_crtc_state) \
972 	((old_crtc_state)->feature && \
973 	 (!(new_crtc_state)->feature || intel_crtc_needs_modeset(new_crtc_state)))
974 
975 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
976 			    const struct intel_crtc_state *new_crtc_state)
977 {
978 	if (!new_crtc_state->hw.active)
979 		return false;
980 
981 	return is_enabling(active_planes, old_crtc_state, new_crtc_state);
982 }
983 
984 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
985 			     const struct intel_crtc_state *new_crtc_state)
986 {
987 	if (!old_crtc_state->hw.active)
988 		return false;
989 
990 	return is_disabling(active_planes, old_crtc_state, new_crtc_state);
991 }
992 
993 static bool vrr_params_changed(const struct intel_crtc_state *old_crtc_state,
994 			       const struct intel_crtc_state *new_crtc_state)
995 {
996 	return old_crtc_state->vrr.flipline != new_crtc_state->vrr.flipline ||
997 		old_crtc_state->vrr.vmin != new_crtc_state->vrr.vmin ||
998 		old_crtc_state->vrr.vmax != new_crtc_state->vrr.vmax ||
999 		old_crtc_state->vrr.guardband != new_crtc_state->vrr.guardband ||
1000 		old_crtc_state->vrr.pipeline_full != new_crtc_state->vrr.pipeline_full;
1001 }
1002 
1003 static bool vrr_enabling(const struct intel_crtc_state *old_crtc_state,
1004 			 const struct intel_crtc_state *new_crtc_state)
1005 {
1006 	if (!new_crtc_state->hw.active)
1007 		return false;
1008 
1009 	return is_enabling(vrr.enable, old_crtc_state, new_crtc_state) ||
1010 		(new_crtc_state->vrr.enable &&
1011 		 (new_crtc_state->update_m_n || new_crtc_state->update_lrr ||
1012 		  vrr_params_changed(old_crtc_state, new_crtc_state)));
1013 }
1014 
1015 static bool vrr_disabling(const struct intel_crtc_state *old_crtc_state,
1016 			  const struct intel_crtc_state *new_crtc_state)
1017 {
1018 	if (!old_crtc_state->hw.active)
1019 		return false;
1020 
1021 	return is_disabling(vrr.enable, old_crtc_state, new_crtc_state) ||
1022 		(old_crtc_state->vrr.enable &&
1023 		 (new_crtc_state->update_m_n || new_crtc_state->update_lrr ||
1024 		  vrr_params_changed(old_crtc_state, new_crtc_state)));
1025 }
1026 
1027 static bool audio_enabling(const struct intel_crtc_state *old_crtc_state,
1028 			   const struct intel_crtc_state *new_crtc_state)
1029 {
1030 	if (!new_crtc_state->hw.active)
1031 		return false;
1032 
1033 	return is_enabling(has_audio, old_crtc_state, new_crtc_state) ||
1034 		(new_crtc_state->has_audio &&
1035 		 memcmp(old_crtc_state->eld, new_crtc_state->eld, MAX_ELD_BYTES) != 0);
1036 }
1037 
1038 static bool audio_disabling(const struct intel_crtc_state *old_crtc_state,
1039 			    const struct intel_crtc_state *new_crtc_state)
1040 {
1041 	if (!old_crtc_state->hw.active)
1042 		return false;
1043 
1044 	return is_disabling(has_audio, old_crtc_state, new_crtc_state) ||
1045 		(old_crtc_state->has_audio &&
1046 		 memcmp(old_crtc_state->eld, new_crtc_state->eld, MAX_ELD_BYTES) != 0);
1047 }
1048 
1049 #undef is_disabling
1050 #undef is_enabling
1051 
1052 static void intel_post_plane_update(struct intel_atomic_state *state,
1053 				    struct intel_crtc *crtc)
1054 {
1055 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1056 	const struct intel_crtc_state *old_crtc_state =
1057 		intel_atomic_get_old_crtc_state(state, crtc);
1058 	const struct intel_crtc_state *new_crtc_state =
1059 		intel_atomic_get_new_crtc_state(state, crtc);
1060 	enum pipe pipe = crtc->pipe;
1061 
1062 	intel_psr_post_plane_update(state, crtc);
1063 
1064 	intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
1065 
1066 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
1067 		intel_update_watermarks(dev_priv);
1068 
1069 	intel_fbc_post_update(state, crtc);
1070 
1071 	if (needs_async_flip_vtd_wa(old_crtc_state) &&
1072 	    !needs_async_flip_vtd_wa(new_crtc_state))
1073 		intel_async_flip_vtd_wa(dev_priv, pipe, false);
1074 
1075 	if (needs_nv12_wa(old_crtc_state) &&
1076 	    !needs_nv12_wa(new_crtc_state))
1077 		skl_wa_827(dev_priv, pipe, false);
1078 
1079 	if (needs_scalerclk_wa(old_crtc_state) &&
1080 	    !needs_scalerclk_wa(new_crtc_state))
1081 		icl_wa_scalerclkgating(dev_priv, pipe, false);
1082 
1083 	if (needs_cursorclk_wa(old_crtc_state) &&
1084 	    !needs_cursorclk_wa(new_crtc_state))
1085 		icl_wa_cursorclkgating(dev_priv, pipe, false);
1086 
1087 	if (intel_crtc_needs_color_update(new_crtc_state))
1088 		intel_color_post_update(new_crtc_state);
1089 
1090 	if (audio_enabling(old_crtc_state, new_crtc_state))
1091 		intel_encoders_audio_enable(state, crtc);
1092 }
1093 
1094 static void intel_crtc_enable_flip_done(struct intel_atomic_state *state,
1095 					struct intel_crtc *crtc)
1096 {
1097 	const struct intel_crtc_state *crtc_state =
1098 		intel_atomic_get_new_crtc_state(state, crtc);
1099 	u8 update_planes = crtc_state->update_planes;
1100 	const struct intel_plane_state __maybe_unused *plane_state;
1101 	struct intel_plane *plane;
1102 	int i;
1103 
1104 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1105 		if (plane->pipe == crtc->pipe &&
1106 		    update_planes & BIT(plane->id))
1107 			plane->enable_flip_done(plane);
1108 	}
1109 }
1110 
1111 static void intel_crtc_disable_flip_done(struct intel_atomic_state *state,
1112 					 struct intel_crtc *crtc)
1113 {
1114 	const struct intel_crtc_state *crtc_state =
1115 		intel_atomic_get_new_crtc_state(state, crtc);
1116 	u8 update_planes = crtc_state->update_planes;
1117 	const struct intel_plane_state __maybe_unused *plane_state;
1118 	struct intel_plane *plane;
1119 	int i;
1120 
1121 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1122 		if (plane->pipe == crtc->pipe &&
1123 		    update_planes & BIT(plane->id))
1124 			plane->disable_flip_done(plane);
1125 	}
1126 }
1127 
1128 static void intel_crtc_async_flip_disable_wa(struct intel_atomic_state *state,
1129 					     struct intel_crtc *crtc)
1130 {
1131 	const struct intel_crtc_state *old_crtc_state =
1132 		intel_atomic_get_old_crtc_state(state, crtc);
1133 	const struct intel_crtc_state *new_crtc_state =
1134 		intel_atomic_get_new_crtc_state(state, crtc);
1135 	u8 disable_async_flip_planes = old_crtc_state->async_flip_planes &
1136 				       ~new_crtc_state->async_flip_planes;
1137 	const struct intel_plane_state *old_plane_state;
1138 	struct intel_plane *plane;
1139 	bool need_vbl_wait = false;
1140 	int i;
1141 
1142 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
1143 		if (plane->need_async_flip_disable_wa &&
1144 		    plane->pipe == crtc->pipe &&
1145 		    disable_async_flip_planes & BIT(plane->id)) {
1146 			/*
1147 			 * Apart from the async flip bit we want to
1148 			 * preserve the old state for the plane.
1149 			 */
1150 			plane->async_flip(plane, old_crtc_state,
1151 					  old_plane_state, false);
1152 			need_vbl_wait = true;
1153 		}
1154 	}
1155 
1156 	if (need_vbl_wait)
1157 		intel_crtc_wait_for_next_vblank(crtc);
1158 }
1159 
1160 static void intel_pre_plane_update(struct intel_atomic_state *state,
1161 				   struct intel_crtc *crtc)
1162 {
1163 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1164 	const struct intel_crtc_state *old_crtc_state =
1165 		intel_atomic_get_old_crtc_state(state, crtc);
1166 	const struct intel_crtc_state *new_crtc_state =
1167 		intel_atomic_get_new_crtc_state(state, crtc);
1168 	enum pipe pipe = crtc->pipe;
1169 
1170 	if (vrr_disabling(old_crtc_state, new_crtc_state)) {
1171 		intel_vrr_disable(old_crtc_state);
1172 		intel_crtc_update_active_timings(old_crtc_state, false);
1173 	}
1174 
1175 	if (audio_disabling(old_crtc_state, new_crtc_state))
1176 		intel_encoders_audio_disable(state, crtc);
1177 
1178 	intel_drrs_deactivate(old_crtc_state);
1179 
1180 	intel_psr_pre_plane_update(state, crtc);
1181 
1182 	if (hsw_ips_pre_update(state, crtc))
1183 		intel_crtc_wait_for_next_vblank(crtc);
1184 
1185 	if (intel_fbc_pre_update(state, crtc))
1186 		intel_crtc_wait_for_next_vblank(crtc);
1187 
1188 	if (!needs_async_flip_vtd_wa(old_crtc_state) &&
1189 	    needs_async_flip_vtd_wa(new_crtc_state))
1190 		intel_async_flip_vtd_wa(dev_priv, pipe, true);
1191 
1192 	/* Display WA 827 */
1193 	if (!needs_nv12_wa(old_crtc_state) &&
1194 	    needs_nv12_wa(new_crtc_state))
1195 		skl_wa_827(dev_priv, pipe, true);
1196 
1197 	/* Wa_2006604312:icl,ehl */
1198 	if (!needs_scalerclk_wa(old_crtc_state) &&
1199 	    needs_scalerclk_wa(new_crtc_state))
1200 		icl_wa_scalerclkgating(dev_priv, pipe, true);
1201 
1202 	/* Wa_1604331009:icl,jsl,ehl */
1203 	if (!needs_cursorclk_wa(old_crtc_state) &&
1204 	    needs_cursorclk_wa(new_crtc_state))
1205 		icl_wa_cursorclkgating(dev_priv, pipe, true);
1206 
1207 	/*
1208 	 * Vblank time updates from the shadow to live plane control register
1209 	 * are blocked if the memory self-refresh mode is active at that
1210 	 * moment. So to make sure the plane gets truly disabled, disable
1211 	 * first the self-refresh mode. The self-refresh enable bit in turn
1212 	 * will be checked/applied by the HW only at the next frame start
1213 	 * event which is after the vblank start event, so we need to have a
1214 	 * wait-for-vblank between disabling the plane and the pipe.
1215 	 */
1216 	if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
1217 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
1218 		intel_crtc_wait_for_next_vblank(crtc);
1219 
1220 	/*
1221 	 * IVB workaround: must disable low power watermarks for at least
1222 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
1223 	 * when scaling is disabled.
1224 	 *
1225 	 * WaCxSRDisabledForSpriteScaling:ivb
1226 	 */
1227 	if (old_crtc_state->hw.active &&
1228 	    new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
1229 		intel_crtc_wait_for_next_vblank(crtc);
1230 
1231 	/*
1232 	 * If we're doing a modeset we don't need to do any
1233 	 * pre-vblank watermark programming here.
1234 	 */
1235 	if (!intel_crtc_needs_modeset(new_crtc_state)) {
1236 		/*
1237 		 * For platforms that support atomic watermarks, program the
1238 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
1239 		 * will be the intermediate values that are safe for both pre- and
1240 		 * post- vblank; when vblank happens, the 'active' values will be set
1241 		 * to the final 'target' values and we'll do this again to get the
1242 		 * optimal watermarks.  For gen9+ platforms, the values we program here
1243 		 * will be the final target values which will get automatically latched
1244 		 * at vblank time; no further programming will be necessary.
1245 		 *
1246 		 * If a platform hasn't been transitioned to atomic watermarks yet,
1247 		 * we'll continue to update watermarks the old way, if flags tell
1248 		 * us to.
1249 		 */
1250 		if (!intel_initial_watermarks(state, crtc))
1251 			if (new_crtc_state->update_wm_pre)
1252 				intel_update_watermarks(dev_priv);
1253 	}
1254 
1255 	/*
1256 	 * Gen2 reports pipe underruns whenever all planes are disabled.
1257 	 * So disable underrun reporting before all the planes get disabled.
1258 	 *
1259 	 * We do this after .initial_watermarks() so that we have a
1260 	 * chance of catching underruns with the intermediate watermarks
1261 	 * vs. the old plane configuration.
1262 	 */
1263 	if (DISPLAY_VER(dev_priv) == 2 && planes_disabling(old_crtc_state, new_crtc_state))
1264 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
1265 
1266 	/*
1267 	 * WA for platforms where async address update enable bit
1268 	 * is double buffered and only latched at start of vblank.
1269 	 */
1270 	if (old_crtc_state->async_flip_planes & ~new_crtc_state->async_flip_planes)
1271 		intel_crtc_async_flip_disable_wa(state, crtc);
1272 }
1273 
1274 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
1275 				      struct intel_crtc *crtc)
1276 {
1277 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1278 	const struct intel_crtc_state *new_crtc_state =
1279 		intel_atomic_get_new_crtc_state(state, crtc);
1280 	unsigned int update_mask = new_crtc_state->update_planes;
1281 	const struct intel_plane_state *old_plane_state;
1282 	struct intel_plane *plane;
1283 	unsigned fb_bits = 0;
1284 	int i;
1285 
1286 	intel_crtc_dpms_overlay_disable(crtc);
1287 
1288 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
1289 		if (crtc->pipe != plane->pipe ||
1290 		    !(update_mask & BIT(plane->id)))
1291 			continue;
1292 
1293 		intel_plane_disable_arm(plane, new_crtc_state);
1294 
1295 		if (old_plane_state->uapi.visible)
1296 			fb_bits |= plane->frontbuffer_bit;
1297 	}
1298 
1299 	intel_frontbuffer_flip(dev_priv, fb_bits);
1300 }
1301 
1302 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
1303 {
1304 	struct drm_i915_private *i915 = to_i915(state->base.dev);
1305 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
1306 	struct intel_crtc *crtc;
1307 	int i;
1308 
1309 	/*
1310 	 * Make sure the DPLL state is up-to-date for fastset TypeC ports after non-blocking commits.
1311 	 * TODO: Update the DPLL state for all cases in the encoder->update_prepare() hook.
1312 	 */
1313 	if (i915->display.dpll.mgr) {
1314 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
1315 			if (intel_crtc_needs_modeset(new_crtc_state))
1316 				continue;
1317 
1318 			new_crtc_state->shared_dpll = old_crtc_state->shared_dpll;
1319 			new_crtc_state->dpll_hw_state = old_crtc_state->dpll_hw_state;
1320 		}
1321 	}
1322 }
1323 
1324 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
1325 					  struct intel_crtc *crtc)
1326 {
1327 	const struct intel_crtc_state *crtc_state =
1328 		intel_atomic_get_new_crtc_state(state, crtc);
1329 	const struct drm_connector_state *conn_state;
1330 	struct drm_connector *conn;
1331 	int i;
1332 
1333 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1334 		struct intel_encoder *encoder =
1335 			to_intel_encoder(conn_state->best_encoder);
1336 
1337 		if (conn_state->crtc != &crtc->base)
1338 			continue;
1339 
1340 		if (encoder->pre_pll_enable)
1341 			encoder->pre_pll_enable(state, encoder,
1342 						crtc_state, conn_state);
1343 	}
1344 }
1345 
1346 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
1347 				      struct intel_crtc *crtc)
1348 {
1349 	const struct intel_crtc_state *crtc_state =
1350 		intel_atomic_get_new_crtc_state(state, crtc);
1351 	const struct drm_connector_state *conn_state;
1352 	struct drm_connector *conn;
1353 	int i;
1354 
1355 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1356 		struct intel_encoder *encoder =
1357 			to_intel_encoder(conn_state->best_encoder);
1358 
1359 		if (conn_state->crtc != &crtc->base)
1360 			continue;
1361 
1362 		if (encoder->pre_enable)
1363 			encoder->pre_enable(state, encoder,
1364 					    crtc_state, conn_state);
1365 	}
1366 }
1367 
1368 static void intel_encoders_enable(struct intel_atomic_state *state,
1369 				  struct intel_crtc *crtc)
1370 {
1371 	const struct intel_crtc_state *crtc_state =
1372 		intel_atomic_get_new_crtc_state(state, crtc);
1373 	const struct drm_connector_state *conn_state;
1374 	struct drm_connector *conn;
1375 	int i;
1376 
1377 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1378 		struct intel_encoder *encoder =
1379 			to_intel_encoder(conn_state->best_encoder);
1380 
1381 		if (conn_state->crtc != &crtc->base)
1382 			continue;
1383 
1384 		if (encoder->enable)
1385 			encoder->enable(state, encoder,
1386 					crtc_state, conn_state);
1387 		intel_opregion_notify_encoder(encoder, true);
1388 	}
1389 }
1390 
1391 static void intel_encoders_disable(struct intel_atomic_state *state,
1392 				   struct intel_crtc *crtc)
1393 {
1394 	const struct intel_crtc_state *old_crtc_state =
1395 		intel_atomic_get_old_crtc_state(state, crtc);
1396 	const struct drm_connector_state *old_conn_state;
1397 	struct drm_connector *conn;
1398 	int i;
1399 
1400 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
1401 		struct intel_encoder *encoder =
1402 			to_intel_encoder(old_conn_state->best_encoder);
1403 
1404 		if (old_conn_state->crtc != &crtc->base)
1405 			continue;
1406 
1407 		intel_opregion_notify_encoder(encoder, false);
1408 		if (encoder->disable)
1409 			encoder->disable(state, encoder,
1410 					 old_crtc_state, old_conn_state);
1411 	}
1412 }
1413 
1414 static void intel_encoders_post_disable(struct intel_atomic_state *state,
1415 					struct intel_crtc *crtc)
1416 {
1417 	const struct intel_crtc_state *old_crtc_state =
1418 		intel_atomic_get_old_crtc_state(state, crtc);
1419 	const struct drm_connector_state *old_conn_state;
1420 	struct drm_connector *conn;
1421 	int i;
1422 
1423 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
1424 		struct intel_encoder *encoder =
1425 			to_intel_encoder(old_conn_state->best_encoder);
1426 
1427 		if (old_conn_state->crtc != &crtc->base)
1428 			continue;
1429 
1430 		if (encoder->post_disable)
1431 			encoder->post_disable(state, encoder,
1432 					      old_crtc_state, old_conn_state);
1433 	}
1434 }
1435 
1436 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
1437 					    struct intel_crtc *crtc)
1438 {
1439 	const struct intel_crtc_state *old_crtc_state =
1440 		intel_atomic_get_old_crtc_state(state, crtc);
1441 	const struct drm_connector_state *old_conn_state;
1442 	struct drm_connector *conn;
1443 	int i;
1444 
1445 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
1446 		struct intel_encoder *encoder =
1447 			to_intel_encoder(old_conn_state->best_encoder);
1448 
1449 		if (old_conn_state->crtc != &crtc->base)
1450 			continue;
1451 
1452 		if (encoder->post_pll_disable)
1453 			encoder->post_pll_disable(state, encoder,
1454 						  old_crtc_state, old_conn_state);
1455 	}
1456 }
1457 
1458 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
1459 				       struct intel_crtc *crtc)
1460 {
1461 	const struct intel_crtc_state *crtc_state =
1462 		intel_atomic_get_new_crtc_state(state, crtc);
1463 	const struct drm_connector_state *conn_state;
1464 	struct drm_connector *conn;
1465 	int i;
1466 
1467 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
1468 		struct intel_encoder *encoder =
1469 			to_intel_encoder(conn_state->best_encoder);
1470 
1471 		if (conn_state->crtc != &crtc->base)
1472 			continue;
1473 
1474 		if (encoder->update_pipe)
1475 			encoder->update_pipe(state, encoder,
1476 					     crtc_state, conn_state);
1477 	}
1478 }
1479 
1480 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
1481 {
1482 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1483 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
1484 
1485 	plane->disable_arm(plane, crtc_state);
1486 }
1487 
1488 static void ilk_configure_cpu_transcoder(const struct intel_crtc_state *crtc_state)
1489 {
1490 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1491 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1492 
1493 	if (crtc_state->has_pch_encoder) {
1494 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1495 					       &crtc_state->fdi_m_n);
1496 	} else if (intel_crtc_has_dp_encoder(crtc_state)) {
1497 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1498 					       &crtc_state->dp_m_n);
1499 		intel_cpu_transcoder_set_m2_n2(crtc, cpu_transcoder,
1500 					       &crtc_state->dp_m2_n2);
1501 	}
1502 
1503 	intel_set_transcoder_timings(crtc_state);
1504 
1505 	ilk_set_pipeconf(crtc_state);
1506 }
1507 
1508 static void ilk_crtc_enable(struct intel_atomic_state *state,
1509 			    struct intel_crtc *crtc)
1510 {
1511 	const struct intel_crtc_state *new_crtc_state =
1512 		intel_atomic_get_new_crtc_state(state, crtc);
1513 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1514 	enum pipe pipe = crtc->pipe;
1515 
1516 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
1517 		return;
1518 
1519 	/*
1520 	 * Sometimes spurious CPU pipe underruns happen during FDI
1521 	 * training, at least with VGA+HDMI cloning. Suppress them.
1522 	 *
1523 	 * On ILK we get an occasional spurious CPU pipe underruns
1524 	 * between eDP port A enable and vdd enable. Also PCH port
1525 	 * enable seems to result in the occasional CPU pipe underrun.
1526 	 *
1527 	 * Spurious PCH underruns also occur during PCH enabling.
1528 	 */
1529 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
1530 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
1531 
1532 	ilk_configure_cpu_transcoder(new_crtc_state);
1533 
1534 	intel_set_pipe_src_size(new_crtc_state);
1535 
1536 	crtc->active = true;
1537 
1538 	intel_encoders_pre_enable(state, crtc);
1539 
1540 	if (new_crtc_state->has_pch_encoder) {
1541 		ilk_pch_pre_enable(state, crtc);
1542 	} else {
1543 		assert_fdi_tx_disabled(dev_priv, pipe);
1544 		assert_fdi_rx_disabled(dev_priv, pipe);
1545 	}
1546 
1547 	ilk_pfit_enable(new_crtc_state);
1548 
1549 	/*
1550 	 * On ILK+ LUT must be loaded before the pipe is running but with
1551 	 * clocks enabled
1552 	 */
1553 	intel_color_load_luts(new_crtc_state);
1554 	intel_color_commit_noarm(new_crtc_state);
1555 	intel_color_commit_arm(new_crtc_state);
1556 	/* update DSPCNTR to configure gamma for pipe bottom color */
1557 	intel_disable_primary_plane(new_crtc_state);
1558 
1559 	intel_initial_watermarks(state, crtc);
1560 	intel_enable_transcoder(new_crtc_state);
1561 
1562 	if (new_crtc_state->has_pch_encoder)
1563 		ilk_pch_enable(state, crtc);
1564 
1565 	intel_crtc_vblank_on(new_crtc_state);
1566 
1567 	intel_encoders_enable(state, crtc);
1568 
1569 	if (HAS_PCH_CPT(dev_priv))
1570 		intel_wait_for_pipe_scanline_moving(crtc);
1571 
1572 	/*
1573 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
1574 	 * And a second vblank wait is needed at least on ILK with
1575 	 * some interlaced HDMI modes. Let's do the double wait always
1576 	 * in case there are more corner cases we don't know about.
1577 	 */
1578 	if (new_crtc_state->has_pch_encoder) {
1579 		intel_crtc_wait_for_next_vblank(crtc);
1580 		intel_crtc_wait_for_next_vblank(crtc);
1581 	}
1582 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
1583 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
1584 }
1585 
1586 /* Display WA #1180: WaDisableScalarClockGating: glk */
1587 static bool glk_need_scaler_clock_gating_wa(const struct intel_crtc_state *crtc_state)
1588 {
1589 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1590 
1591 	return DISPLAY_VER(i915) == 10 && crtc_state->pch_pfit.enabled;
1592 }
1593 
1594 static void glk_pipe_scaler_clock_gating_wa(struct intel_crtc *crtc, bool enable)
1595 {
1596 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1597 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
1598 
1599 	intel_de_rmw(i915, CLKGATE_DIS_PSL(crtc->pipe),
1600 		     mask, enable ? mask : 0);
1601 }
1602 
1603 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
1604 {
1605 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1606 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1607 
1608 	intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
1609 		       HSW_LINETIME(crtc_state->linetime) |
1610 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
1611 }
1612 
1613 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
1614 {
1615 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1616 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1617 
1618 	intel_de_rmw(i915, hsw_chicken_trans_reg(i915, crtc_state->cpu_transcoder),
1619 		     HSW_FRAME_START_DELAY_MASK,
1620 		     HSW_FRAME_START_DELAY(crtc_state->framestart_delay - 1));
1621 }
1622 
1623 static void hsw_configure_cpu_transcoder(const struct intel_crtc_state *crtc_state)
1624 {
1625 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1626 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1627 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1628 
1629 	if (crtc_state->has_pch_encoder) {
1630 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1631 					       &crtc_state->fdi_m_n);
1632 	} else if (intel_crtc_has_dp_encoder(crtc_state)) {
1633 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
1634 					       &crtc_state->dp_m_n);
1635 		intel_cpu_transcoder_set_m2_n2(crtc, cpu_transcoder,
1636 					       &crtc_state->dp_m2_n2);
1637 	}
1638 
1639 	intel_set_transcoder_timings(crtc_state);
1640 	if (HAS_VRR(dev_priv))
1641 		intel_vrr_set_transcoder_timings(crtc_state);
1642 
1643 	if (cpu_transcoder != TRANSCODER_EDP)
1644 		intel_de_write(dev_priv, TRANS_MULT(cpu_transcoder),
1645 			       crtc_state->pixel_multiplier - 1);
1646 
1647 	hsw_set_frame_start_delay(crtc_state);
1648 
1649 	hsw_set_transconf(crtc_state);
1650 }
1651 
1652 static void hsw_crtc_enable(struct intel_atomic_state *state,
1653 			    struct intel_crtc *crtc)
1654 {
1655 	const struct intel_crtc_state *new_crtc_state =
1656 		intel_atomic_get_new_crtc_state(state, crtc);
1657 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1658 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
1659 	struct intel_crtc *pipe_crtc;
1660 
1661 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
1662 		return;
1663 
1664 	for_each_intel_crtc_in_pipe_mask_reverse(&dev_priv->drm, pipe_crtc,
1665 						 intel_crtc_joined_pipe_mask(new_crtc_state))
1666 		intel_dmc_enable_pipe(dev_priv, pipe_crtc->pipe);
1667 
1668 	intel_encoders_pre_pll_enable(state, crtc);
1669 
1670 	for_each_intel_crtc_in_pipe_mask_reverse(&dev_priv->drm, pipe_crtc,
1671 						 intel_crtc_joined_pipe_mask(new_crtc_state)) {
1672 		const struct intel_crtc_state *pipe_crtc_state =
1673 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1674 
1675 		if (pipe_crtc_state->shared_dpll)
1676 			intel_enable_shared_dpll(pipe_crtc_state);
1677 	}
1678 
1679 	intel_encoders_pre_enable(state, crtc);
1680 
1681 	for_each_intel_crtc_in_pipe_mask_reverse(&dev_priv->drm, pipe_crtc,
1682 						 intel_crtc_joined_pipe_mask(new_crtc_state)) {
1683 		const struct intel_crtc_state *pipe_crtc_state =
1684 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1685 
1686 		intel_dsc_enable(pipe_crtc_state);
1687 
1688 		if (DISPLAY_VER(dev_priv) >= 13)
1689 			intel_uncompressed_joiner_enable(pipe_crtc_state);
1690 
1691 		intel_set_pipe_src_size(pipe_crtc_state);
1692 
1693 		if (DISPLAY_VER(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
1694 			bdw_set_pipe_misc(pipe_crtc_state);
1695 	}
1696 
1697 	if (!transcoder_is_dsi(cpu_transcoder))
1698 		hsw_configure_cpu_transcoder(new_crtc_state);
1699 
1700 	for_each_intel_crtc_in_pipe_mask_reverse(&dev_priv->drm, pipe_crtc,
1701 						 intel_crtc_joined_pipe_mask(new_crtc_state)) {
1702 		const struct intel_crtc_state *pipe_crtc_state =
1703 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1704 
1705 		pipe_crtc->active = true;
1706 
1707 		if (glk_need_scaler_clock_gating_wa(pipe_crtc_state))
1708 			glk_pipe_scaler_clock_gating_wa(pipe_crtc, true);
1709 
1710 		if (DISPLAY_VER(dev_priv) >= 9)
1711 			skl_pfit_enable(pipe_crtc_state);
1712 		else
1713 			ilk_pfit_enable(pipe_crtc_state);
1714 
1715 		/*
1716 		 * On ILK+ LUT must be loaded before the pipe is running but with
1717 		 * clocks enabled
1718 		 */
1719 		intel_color_load_luts(pipe_crtc_state);
1720 		intel_color_commit_noarm(pipe_crtc_state);
1721 		intel_color_commit_arm(pipe_crtc_state);
1722 		/* update DSPCNTR to configure gamma/csc for pipe bottom color */
1723 		if (DISPLAY_VER(dev_priv) < 9)
1724 			intel_disable_primary_plane(pipe_crtc_state);
1725 
1726 		hsw_set_linetime_wm(pipe_crtc_state);
1727 
1728 		if (DISPLAY_VER(dev_priv) >= 11)
1729 			icl_set_pipe_chicken(pipe_crtc_state);
1730 
1731 		intel_initial_watermarks(state, pipe_crtc);
1732 	}
1733 
1734 	intel_encoders_enable(state, crtc);
1735 
1736 	for_each_intel_crtc_in_pipe_mask_reverse(&dev_priv->drm, pipe_crtc,
1737 						 intel_crtc_joined_pipe_mask(new_crtc_state)) {
1738 		const struct intel_crtc_state *pipe_crtc_state =
1739 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
1740 		enum pipe hsw_workaround_pipe;
1741 
1742 		if (glk_need_scaler_clock_gating_wa(pipe_crtc_state)) {
1743 			intel_crtc_wait_for_next_vblank(pipe_crtc);
1744 			glk_pipe_scaler_clock_gating_wa(pipe_crtc, false);
1745 		}
1746 
1747 		/*
1748 		 * If we change the relative order between pipe/planes
1749 		 * enabling, we need to change the workaround.
1750 		 */
1751 		hsw_workaround_pipe = pipe_crtc_state->hsw_workaround_pipe;
1752 		if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
1753 			struct intel_crtc *wa_crtc =
1754 				intel_crtc_for_pipe(dev_priv, hsw_workaround_pipe);
1755 
1756 			intel_crtc_wait_for_next_vblank(wa_crtc);
1757 			intel_crtc_wait_for_next_vblank(wa_crtc);
1758 		}
1759 	}
1760 }
1761 
1762 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
1763 {
1764 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
1765 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1766 	enum pipe pipe = crtc->pipe;
1767 
1768 	/* To avoid upsetting the power well on haswell only disable the pfit if
1769 	 * it's in use. The hw state code will make sure we get this right. */
1770 	if (!old_crtc_state->pch_pfit.enabled)
1771 		return;
1772 
1773 	intel_de_write_fw(dev_priv, PF_CTL(pipe), 0);
1774 	intel_de_write_fw(dev_priv, PF_WIN_POS(pipe), 0);
1775 	intel_de_write_fw(dev_priv, PF_WIN_SZ(pipe), 0);
1776 }
1777 
1778 static void ilk_crtc_disable(struct intel_atomic_state *state,
1779 			     struct intel_crtc *crtc)
1780 {
1781 	const struct intel_crtc_state *old_crtc_state =
1782 		intel_atomic_get_old_crtc_state(state, crtc);
1783 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1784 	enum pipe pipe = crtc->pipe;
1785 
1786 	/*
1787 	 * Sometimes spurious CPU pipe underruns happen when the
1788 	 * pipe is already disabled, but FDI RX/TX is still enabled.
1789 	 * Happens at least with VGA+HDMI cloning. Suppress them.
1790 	 */
1791 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
1792 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
1793 
1794 	intel_encoders_disable(state, crtc);
1795 
1796 	intel_crtc_vblank_off(old_crtc_state);
1797 
1798 	intel_disable_transcoder(old_crtc_state);
1799 
1800 	ilk_pfit_disable(old_crtc_state);
1801 
1802 	if (old_crtc_state->has_pch_encoder)
1803 		ilk_pch_disable(state, crtc);
1804 
1805 	intel_encoders_post_disable(state, crtc);
1806 
1807 	if (old_crtc_state->has_pch_encoder)
1808 		ilk_pch_post_disable(state, crtc);
1809 
1810 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
1811 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
1812 
1813 	intel_disable_shared_dpll(old_crtc_state);
1814 }
1815 
1816 static void hsw_crtc_disable(struct intel_atomic_state *state,
1817 			     struct intel_crtc *crtc)
1818 {
1819 	const struct intel_crtc_state *old_crtc_state =
1820 		intel_atomic_get_old_crtc_state(state, crtc);
1821 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
1822 	struct intel_crtc *pipe_crtc;
1823 
1824 	/*
1825 	 * FIXME collapse everything to one hook.
1826 	 * Need care with mst->ddi interactions.
1827 	 */
1828 	intel_encoders_disable(state, crtc);
1829 	intel_encoders_post_disable(state, crtc);
1830 
1831 	for_each_intel_crtc_in_pipe_mask(&i915->drm, pipe_crtc,
1832 					 intel_crtc_joined_pipe_mask(old_crtc_state)) {
1833 		const struct intel_crtc_state *old_pipe_crtc_state =
1834 			intel_atomic_get_old_crtc_state(state, pipe_crtc);
1835 
1836 		intel_disable_shared_dpll(old_pipe_crtc_state);
1837 	}
1838 
1839 	intel_encoders_post_pll_disable(state, crtc);
1840 
1841 	for_each_intel_crtc_in_pipe_mask(&i915->drm, pipe_crtc,
1842 					 intel_crtc_joined_pipe_mask(old_crtc_state))
1843 		intel_dmc_disable_pipe(i915, pipe_crtc->pipe);
1844 }
1845 
1846 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
1847 {
1848 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1849 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1850 
1851 	if (!crtc_state->gmch_pfit.control)
1852 		return;
1853 
1854 	/*
1855 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
1856 	 * according to register description and PRM.
1857 	 */
1858 	drm_WARN_ON(&dev_priv->drm,
1859 		    intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
1860 	assert_transcoder_disabled(dev_priv, crtc_state->cpu_transcoder);
1861 
1862 	intel_de_write(dev_priv, PFIT_PGM_RATIOS,
1863 		       crtc_state->gmch_pfit.pgm_ratios);
1864 	intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
1865 
1866 	/* Border color in case we don't scale up to the full screen. Black by
1867 	 * default, change to something else for debugging. */
1868 	intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
1869 }
1870 
1871 /* Prefer intel_encoder_is_combo() */
1872 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
1873 {
1874 	if (phy == PHY_NONE)
1875 		return false;
1876 	else if (IS_ALDERLAKE_S(dev_priv))
1877 		return phy <= PHY_E;
1878 	else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv))
1879 		return phy <= PHY_D;
1880 	else if (IS_JASPERLAKE(dev_priv) || IS_ELKHARTLAKE(dev_priv))
1881 		return phy <= PHY_C;
1882 	else if (IS_ALDERLAKE_P(dev_priv) || IS_DISPLAY_VER(dev_priv, 11, 12))
1883 		return phy <= PHY_B;
1884 	else
1885 		/*
1886 		 * DG2 outputs labelled as "combo PHY" in the bspec use
1887 		 * SNPS PHYs with completely different programming,
1888 		 * hence we always return false here.
1889 		 */
1890 		return false;
1891 }
1892 
1893 /* Prefer intel_encoder_is_tc() */
1894 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
1895 {
1896 	/*
1897 	 * DG2's "TC1", although TC-capable output, doesn't share the same flow
1898 	 * as other platforms on the display engine side and rather rely on the
1899 	 * SNPS PHY, that is programmed separately
1900 	 */
1901 	if (IS_DG2(dev_priv))
1902 		return false;
1903 
1904 	if (DISPLAY_VER(dev_priv) >= 13)
1905 		return phy >= PHY_F && phy <= PHY_I;
1906 	else if (IS_TIGERLAKE(dev_priv))
1907 		return phy >= PHY_D && phy <= PHY_I;
1908 	else if (IS_ICELAKE(dev_priv))
1909 		return phy >= PHY_C && phy <= PHY_F;
1910 
1911 	return false;
1912 }
1913 
1914 /* Prefer intel_encoder_is_snps() */
1915 bool intel_phy_is_snps(struct drm_i915_private *dev_priv, enum phy phy)
1916 {
1917 	/*
1918 	 * For DG2, and for DG2 only, all four "combo" ports and the TC1 port
1919 	 * (PHY E) use Synopsis PHYs. See intel_phy_is_tc().
1920 	 */
1921 	return IS_DG2(dev_priv) && phy > PHY_NONE && phy <= PHY_E;
1922 }
1923 
1924 /* Prefer intel_encoder_to_phy() */
1925 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
1926 {
1927 	if (DISPLAY_VER(i915) >= 13 && port >= PORT_D_XELPD)
1928 		return PHY_D + port - PORT_D_XELPD;
1929 	else if (DISPLAY_VER(i915) >= 13 && port >= PORT_TC1)
1930 		return PHY_F + port - PORT_TC1;
1931 	else if (IS_ALDERLAKE_S(i915) && port >= PORT_TC1)
1932 		return PHY_B + port - PORT_TC1;
1933 	else if ((IS_DG1(i915) || IS_ROCKETLAKE(i915)) && port >= PORT_TC1)
1934 		return PHY_C + port - PORT_TC1;
1935 	else if ((IS_JASPERLAKE(i915) || IS_ELKHARTLAKE(i915)) &&
1936 		 port == PORT_D)
1937 		return PHY_A;
1938 
1939 	return PHY_A + port - PORT_A;
1940 }
1941 
1942 /* Prefer intel_encoder_to_tc() */
1943 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
1944 {
1945 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
1946 		return TC_PORT_NONE;
1947 
1948 	if (DISPLAY_VER(dev_priv) >= 12)
1949 		return TC_PORT_1 + port - PORT_TC1;
1950 	else
1951 		return TC_PORT_1 + port - PORT_C;
1952 }
1953 
1954 enum phy intel_encoder_to_phy(struct intel_encoder *encoder)
1955 {
1956 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1957 
1958 	return intel_port_to_phy(i915, encoder->port);
1959 }
1960 
1961 bool intel_encoder_is_combo(struct intel_encoder *encoder)
1962 {
1963 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1964 
1965 	return intel_phy_is_combo(i915, intel_encoder_to_phy(encoder));
1966 }
1967 
1968 bool intel_encoder_is_snps(struct intel_encoder *encoder)
1969 {
1970 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1971 
1972 	return intel_phy_is_snps(i915, intel_encoder_to_phy(encoder));
1973 }
1974 
1975 bool intel_encoder_is_tc(struct intel_encoder *encoder)
1976 {
1977 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1978 
1979 	return intel_phy_is_tc(i915, intel_encoder_to_phy(encoder));
1980 }
1981 
1982 enum tc_port intel_encoder_to_tc(struct intel_encoder *encoder)
1983 {
1984 	struct drm_i915_private *i915 = to_i915(encoder->base.dev);
1985 
1986 	return intel_port_to_tc(i915, encoder->port);
1987 }
1988 
1989 enum intel_display_power_domain
1990 intel_aux_power_domain(struct intel_digital_port *dig_port)
1991 {
1992 	struct drm_i915_private *i915 = to_i915(dig_port->base.base.dev);
1993 
1994 	if (intel_tc_port_in_tbt_alt_mode(dig_port))
1995 		return intel_display_power_tbt_aux_domain(i915, dig_port->aux_ch);
1996 
1997 	return intel_display_power_legacy_aux_domain(i915, dig_port->aux_ch);
1998 }
1999 
2000 static void get_crtc_power_domains(struct intel_crtc_state *crtc_state,
2001 				   struct intel_power_domain_mask *mask)
2002 {
2003 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2004 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2005 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2006 	struct drm_encoder *encoder;
2007 	enum pipe pipe = crtc->pipe;
2008 
2009 	bitmap_zero(mask->bits, POWER_DOMAIN_NUM);
2010 
2011 	if (!crtc_state->hw.active)
2012 		return;
2013 
2014 	set_bit(POWER_DOMAIN_PIPE(pipe), mask->bits);
2015 	set_bit(POWER_DOMAIN_TRANSCODER(cpu_transcoder), mask->bits);
2016 	if (crtc_state->pch_pfit.enabled ||
2017 	    crtc_state->pch_pfit.force_thru)
2018 		set_bit(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe), mask->bits);
2019 
2020 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
2021 				  crtc_state->uapi.encoder_mask) {
2022 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
2023 
2024 		set_bit(intel_encoder->power_domain, mask->bits);
2025 	}
2026 
2027 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
2028 		set_bit(POWER_DOMAIN_AUDIO_MMIO, mask->bits);
2029 
2030 	if (crtc_state->shared_dpll)
2031 		set_bit(POWER_DOMAIN_DISPLAY_CORE, mask->bits);
2032 
2033 	if (crtc_state->dsc.compression_enable)
2034 		set_bit(intel_dsc_power_domain(crtc, cpu_transcoder), mask->bits);
2035 }
2036 
2037 void intel_modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state,
2038 					  struct intel_power_domain_mask *old_domains)
2039 {
2040 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2041 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2042 	enum intel_display_power_domain domain;
2043 	struct intel_power_domain_mask domains, new_domains;
2044 
2045 	get_crtc_power_domains(crtc_state, &domains);
2046 
2047 	bitmap_andnot(new_domains.bits,
2048 		      domains.bits,
2049 		      crtc->enabled_power_domains.mask.bits,
2050 		      POWER_DOMAIN_NUM);
2051 	bitmap_andnot(old_domains->bits,
2052 		      crtc->enabled_power_domains.mask.bits,
2053 		      domains.bits,
2054 		      POWER_DOMAIN_NUM);
2055 
2056 	for_each_power_domain(domain, &new_domains)
2057 		intel_display_power_get_in_set(dev_priv,
2058 					       &crtc->enabled_power_domains,
2059 					       domain);
2060 }
2061 
2062 void intel_modeset_put_crtc_power_domains(struct intel_crtc *crtc,
2063 					  struct intel_power_domain_mask *domains)
2064 {
2065 	intel_display_power_put_mask_in_set(to_i915(crtc->base.dev),
2066 					    &crtc->enabled_power_domains,
2067 					    domains);
2068 }
2069 
2070 static void i9xx_configure_cpu_transcoder(const struct intel_crtc_state *crtc_state)
2071 {
2072 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2073 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2074 
2075 	if (intel_crtc_has_dp_encoder(crtc_state)) {
2076 		intel_cpu_transcoder_set_m1_n1(crtc, cpu_transcoder,
2077 					       &crtc_state->dp_m_n);
2078 		intel_cpu_transcoder_set_m2_n2(crtc, cpu_transcoder,
2079 					       &crtc_state->dp_m2_n2);
2080 	}
2081 
2082 	intel_set_transcoder_timings(crtc_state);
2083 
2084 	i9xx_set_pipeconf(crtc_state);
2085 }
2086 
2087 static void valleyview_crtc_enable(struct intel_atomic_state *state,
2088 				   struct intel_crtc *crtc)
2089 {
2090 	const struct intel_crtc_state *new_crtc_state =
2091 		intel_atomic_get_new_crtc_state(state, crtc);
2092 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2093 	enum pipe pipe = crtc->pipe;
2094 
2095 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
2096 		return;
2097 
2098 	i9xx_configure_cpu_transcoder(new_crtc_state);
2099 
2100 	intel_set_pipe_src_size(new_crtc_state);
2101 
2102 	intel_de_write(dev_priv, VLV_PIPE_MSA_MISC(pipe), 0);
2103 
2104 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
2105 		intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
2106 		intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
2107 	}
2108 
2109 	crtc->active = true;
2110 
2111 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2112 
2113 	intel_encoders_pre_pll_enable(state, crtc);
2114 
2115 	if (IS_CHERRYVIEW(dev_priv))
2116 		chv_enable_pll(new_crtc_state);
2117 	else
2118 		vlv_enable_pll(new_crtc_state);
2119 
2120 	intel_encoders_pre_enable(state, crtc);
2121 
2122 	i9xx_pfit_enable(new_crtc_state);
2123 
2124 	intel_color_load_luts(new_crtc_state);
2125 	intel_color_commit_noarm(new_crtc_state);
2126 	intel_color_commit_arm(new_crtc_state);
2127 	/* update DSPCNTR to configure gamma for pipe bottom color */
2128 	intel_disable_primary_plane(new_crtc_state);
2129 
2130 	intel_initial_watermarks(state, crtc);
2131 	intel_enable_transcoder(new_crtc_state);
2132 
2133 	intel_crtc_vblank_on(new_crtc_state);
2134 
2135 	intel_encoders_enable(state, crtc);
2136 }
2137 
2138 static void i9xx_crtc_enable(struct intel_atomic_state *state,
2139 			     struct intel_crtc *crtc)
2140 {
2141 	const struct intel_crtc_state *new_crtc_state =
2142 		intel_atomic_get_new_crtc_state(state, crtc);
2143 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2144 	enum pipe pipe = crtc->pipe;
2145 
2146 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
2147 		return;
2148 
2149 	i9xx_configure_cpu_transcoder(new_crtc_state);
2150 
2151 	intel_set_pipe_src_size(new_crtc_state);
2152 
2153 	crtc->active = true;
2154 
2155 	if (DISPLAY_VER(dev_priv) != 2)
2156 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2157 
2158 	intel_encoders_pre_enable(state, crtc);
2159 
2160 	i9xx_enable_pll(new_crtc_state);
2161 
2162 	i9xx_pfit_enable(new_crtc_state);
2163 
2164 	intel_color_load_luts(new_crtc_state);
2165 	intel_color_commit_noarm(new_crtc_state);
2166 	intel_color_commit_arm(new_crtc_state);
2167 	/* update DSPCNTR to configure gamma for pipe bottom color */
2168 	intel_disable_primary_plane(new_crtc_state);
2169 
2170 	if (!intel_initial_watermarks(state, crtc))
2171 		intel_update_watermarks(dev_priv);
2172 	intel_enable_transcoder(new_crtc_state);
2173 
2174 	intel_crtc_vblank_on(new_crtc_state);
2175 
2176 	intel_encoders_enable(state, crtc);
2177 
2178 	/* prevents spurious underruns */
2179 	if (DISPLAY_VER(dev_priv) == 2)
2180 		intel_crtc_wait_for_next_vblank(crtc);
2181 }
2182 
2183 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
2184 {
2185 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
2186 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2187 
2188 	if (!old_crtc_state->gmch_pfit.control)
2189 		return;
2190 
2191 	assert_transcoder_disabled(dev_priv, old_crtc_state->cpu_transcoder);
2192 
2193 	drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
2194 		    intel_de_read(dev_priv, PFIT_CONTROL));
2195 	intel_de_write(dev_priv, PFIT_CONTROL, 0);
2196 }
2197 
2198 static void i9xx_crtc_disable(struct intel_atomic_state *state,
2199 			      struct intel_crtc *crtc)
2200 {
2201 	struct intel_crtc_state *old_crtc_state =
2202 		intel_atomic_get_old_crtc_state(state, crtc);
2203 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2204 	enum pipe pipe = crtc->pipe;
2205 
2206 	/*
2207 	 * On gen2 planes are double buffered but the pipe isn't, so we must
2208 	 * wait for planes to fully turn off before disabling the pipe.
2209 	 */
2210 	if (DISPLAY_VER(dev_priv) == 2)
2211 		intel_crtc_wait_for_next_vblank(crtc);
2212 
2213 	intel_encoders_disable(state, crtc);
2214 
2215 	intel_crtc_vblank_off(old_crtc_state);
2216 
2217 	intel_disable_transcoder(old_crtc_state);
2218 
2219 	i9xx_pfit_disable(old_crtc_state);
2220 
2221 	intel_encoders_post_disable(state, crtc);
2222 
2223 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
2224 		if (IS_CHERRYVIEW(dev_priv))
2225 			chv_disable_pll(dev_priv, pipe);
2226 		else if (IS_VALLEYVIEW(dev_priv))
2227 			vlv_disable_pll(dev_priv, pipe);
2228 		else
2229 			i9xx_disable_pll(old_crtc_state);
2230 	}
2231 
2232 	intel_encoders_post_pll_disable(state, crtc);
2233 
2234 	if (DISPLAY_VER(dev_priv) != 2)
2235 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
2236 
2237 	if (!dev_priv->display.funcs.wm->initial_watermarks)
2238 		intel_update_watermarks(dev_priv);
2239 
2240 	/* clock the pipe down to 640x480@60 to potentially save power */
2241 	if (IS_I830(dev_priv))
2242 		i830_enable_pipe(dev_priv, pipe);
2243 }
2244 
2245 void intel_encoder_destroy(struct drm_encoder *encoder)
2246 {
2247 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
2248 
2249 	drm_encoder_cleanup(encoder);
2250 	kfree(intel_encoder);
2251 }
2252 
2253 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
2254 {
2255 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2256 
2257 	/* GDG double wide on either pipe, otherwise pipe A only */
2258 	return DISPLAY_VER(dev_priv) < 4 &&
2259 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
2260 }
2261 
2262 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
2263 {
2264 	u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
2265 	struct drm_rect src;
2266 
2267 	/*
2268 	 * We only use IF-ID interlacing. If we ever use
2269 	 * PF-ID we'll need to adjust the pixel_rate here.
2270 	 */
2271 
2272 	if (!crtc_state->pch_pfit.enabled)
2273 		return pixel_rate;
2274 
2275 	drm_rect_init(&src, 0, 0,
2276 		      drm_rect_width(&crtc_state->pipe_src) << 16,
2277 		      drm_rect_height(&crtc_state->pipe_src) << 16);
2278 
2279 	return intel_adjusted_rate(&src, &crtc_state->pch_pfit.dst,
2280 				   pixel_rate);
2281 }
2282 
2283 static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
2284 					 const struct drm_display_mode *timings)
2285 {
2286 	mode->hdisplay = timings->crtc_hdisplay;
2287 	mode->htotal = timings->crtc_htotal;
2288 	mode->hsync_start = timings->crtc_hsync_start;
2289 	mode->hsync_end = timings->crtc_hsync_end;
2290 
2291 	mode->vdisplay = timings->crtc_vdisplay;
2292 	mode->vtotal = timings->crtc_vtotal;
2293 	mode->vsync_start = timings->crtc_vsync_start;
2294 	mode->vsync_end = timings->crtc_vsync_end;
2295 
2296 	mode->flags = timings->flags;
2297 	mode->type = DRM_MODE_TYPE_DRIVER;
2298 
2299 	mode->clock = timings->crtc_clock;
2300 
2301 	drm_mode_set_name(mode);
2302 }
2303 
2304 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
2305 {
2306 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2307 
2308 	if (HAS_GMCH(dev_priv))
2309 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
2310 		crtc_state->pixel_rate =
2311 			crtc_state->hw.pipe_mode.crtc_clock;
2312 	else
2313 		crtc_state->pixel_rate =
2314 			ilk_pipe_pixel_rate(crtc_state);
2315 }
2316 
2317 static void intel_bigjoiner_adjust_timings(const struct intel_crtc_state *crtc_state,
2318 					   struct drm_display_mode *mode)
2319 {
2320 	int num_pipes = intel_bigjoiner_num_pipes(crtc_state);
2321 
2322 	if (num_pipes < 2)
2323 		return;
2324 
2325 	mode->crtc_clock /= num_pipes;
2326 	mode->crtc_hdisplay /= num_pipes;
2327 	mode->crtc_hblank_start /= num_pipes;
2328 	mode->crtc_hblank_end /= num_pipes;
2329 	mode->crtc_hsync_start /= num_pipes;
2330 	mode->crtc_hsync_end /= num_pipes;
2331 	mode->crtc_htotal /= num_pipes;
2332 }
2333 
2334 static void intel_splitter_adjust_timings(const struct intel_crtc_state *crtc_state,
2335 					  struct drm_display_mode *mode)
2336 {
2337 	int overlap = crtc_state->splitter.pixel_overlap;
2338 	int n = crtc_state->splitter.link_count;
2339 
2340 	if (!crtc_state->splitter.enable)
2341 		return;
2342 
2343 	/*
2344 	 * eDP MSO uses segment timings from EDID for transcoder
2345 	 * timings, but full mode for everything else.
2346 	 *
2347 	 * h_full = (h_segment - pixel_overlap) * link_count
2348 	 */
2349 	mode->crtc_hdisplay = (mode->crtc_hdisplay - overlap) * n;
2350 	mode->crtc_hblank_start = (mode->crtc_hblank_start - overlap) * n;
2351 	mode->crtc_hblank_end = (mode->crtc_hblank_end - overlap) * n;
2352 	mode->crtc_hsync_start = (mode->crtc_hsync_start - overlap) * n;
2353 	mode->crtc_hsync_end = (mode->crtc_hsync_end - overlap) * n;
2354 	mode->crtc_htotal = (mode->crtc_htotal - overlap) * n;
2355 	mode->crtc_clock *= n;
2356 }
2357 
2358 static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
2359 {
2360 	struct drm_display_mode *mode = &crtc_state->hw.mode;
2361 	struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
2362 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2363 
2364 	/*
2365 	 * Start with the adjusted_mode crtc timings, which
2366 	 * have been filled with the transcoder timings.
2367 	 */
2368 	drm_mode_copy(pipe_mode, adjusted_mode);
2369 
2370 	/* Expand MSO per-segment transcoder timings to full */
2371 	intel_splitter_adjust_timings(crtc_state, pipe_mode);
2372 
2373 	/*
2374 	 * We want the full numbers in adjusted_mode normal timings,
2375 	 * adjusted_mode crtc timings are left with the raw transcoder
2376 	 * timings.
2377 	 */
2378 	intel_mode_from_crtc_timings(adjusted_mode, pipe_mode);
2379 
2380 	/* Populate the "user" mode with full numbers */
2381 	drm_mode_copy(mode, pipe_mode);
2382 	intel_mode_from_crtc_timings(mode, mode);
2383 	mode->hdisplay = drm_rect_width(&crtc_state->pipe_src) *
2384 		(intel_bigjoiner_num_pipes(crtc_state) ?: 1);
2385 	mode->vdisplay = drm_rect_height(&crtc_state->pipe_src);
2386 
2387 	/* Derive per-pipe timings in case bigjoiner is used */
2388 	intel_bigjoiner_adjust_timings(crtc_state, pipe_mode);
2389 	intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
2390 
2391 	intel_crtc_compute_pixel_rate(crtc_state);
2392 }
2393 
2394 void intel_encoder_get_config(struct intel_encoder *encoder,
2395 			      struct intel_crtc_state *crtc_state)
2396 {
2397 	encoder->get_config(encoder, crtc_state);
2398 
2399 	intel_crtc_readout_derived_state(crtc_state);
2400 }
2401 
2402 static void intel_bigjoiner_compute_pipe_src(struct intel_crtc_state *crtc_state)
2403 {
2404 	int num_pipes = intel_bigjoiner_num_pipes(crtc_state);
2405 	int width, height;
2406 
2407 	if (num_pipes < 2)
2408 		return;
2409 
2410 	width = drm_rect_width(&crtc_state->pipe_src);
2411 	height = drm_rect_height(&crtc_state->pipe_src);
2412 
2413 	drm_rect_init(&crtc_state->pipe_src, 0, 0,
2414 		      width / num_pipes, height);
2415 }
2416 
2417 static int intel_crtc_compute_pipe_src(struct intel_crtc_state *crtc_state)
2418 {
2419 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2420 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2421 
2422 	intel_bigjoiner_compute_pipe_src(crtc_state);
2423 
2424 	/*
2425 	 * Pipe horizontal size must be even in:
2426 	 * - DVO ganged mode
2427 	 * - LVDS dual channel mode
2428 	 * - Double wide pipe
2429 	 */
2430 	if (drm_rect_width(&crtc_state->pipe_src) & 1) {
2431 		if (crtc_state->double_wide) {
2432 			drm_dbg_kms(&i915->drm,
2433 				    "[CRTC:%d:%s] Odd pipe source width not supported with double wide pipe\n",
2434 				    crtc->base.base.id, crtc->base.name);
2435 			return -EINVAL;
2436 		}
2437 
2438 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_LVDS) &&
2439 		    intel_is_dual_link_lvds(i915)) {
2440 			drm_dbg_kms(&i915->drm,
2441 				    "[CRTC:%d:%s] Odd pipe source width not supported with dual link LVDS\n",
2442 				    crtc->base.base.id, crtc->base.name);
2443 			return -EINVAL;
2444 		}
2445 	}
2446 
2447 	return 0;
2448 }
2449 
2450 static int intel_crtc_compute_pipe_mode(struct intel_crtc_state *crtc_state)
2451 {
2452 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2453 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
2454 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2455 	struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
2456 	int clock_limit = i915->display.cdclk.max_dotclk_freq;
2457 
2458 	/*
2459 	 * Start with the adjusted_mode crtc timings, which
2460 	 * have been filled with the transcoder timings.
2461 	 */
2462 	drm_mode_copy(pipe_mode, adjusted_mode);
2463 
2464 	/* Expand MSO per-segment transcoder timings to full */
2465 	intel_splitter_adjust_timings(crtc_state, pipe_mode);
2466 
2467 	/* Derive per-pipe timings in case bigjoiner is used */
2468 	intel_bigjoiner_adjust_timings(crtc_state, pipe_mode);
2469 	intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
2470 
2471 	if (DISPLAY_VER(i915) < 4) {
2472 		clock_limit = i915->display.cdclk.max_cdclk_freq * 9 / 10;
2473 
2474 		/*
2475 		 * Enable double wide mode when the dot clock
2476 		 * is > 90% of the (display) core speed.
2477 		 */
2478 		if (intel_crtc_supports_double_wide(crtc) &&
2479 		    pipe_mode->crtc_clock > clock_limit) {
2480 			clock_limit = i915->display.cdclk.max_dotclk_freq;
2481 			crtc_state->double_wide = true;
2482 		}
2483 	}
2484 
2485 	if (pipe_mode->crtc_clock > clock_limit) {
2486 		drm_dbg_kms(&i915->drm,
2487 			    "[CRTC:%d:%s] requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
2488 			    crtc->base.base.id, crtc->base.name,
2489 			    pipe_mode->crtc_clock, clock_limit,
2490 			    str_yes_no(crtc_state->double_wide));
2491 		return -EINVAL;
2492 	}
2493 
2494 	return 0;
2495 }
2496 
2497 static int intel_crtc_compute_config(struct intel_atomic_state *state,
2498 				     struct intel_crtc *crtc)
2499 {
2500 	struct intel_crtc_state *crtc_state =
2501 		intel_atomic_get_new_crtc_state(state, crtc);
2502 	int ret;
2503 
2504 	ret = intel_dpll_crtc_compute_clock(state, crtc);
2505 	if (ret)
2506 		return ret;
2507 
2508 	ret = intel_crtc_compute_pipe_src(crtc_state);
2509 	if (ret)
2510 		return ret;
2511 
2512 	ret = intel_crtc_compute_pipe_mode(crtc_state);
2513 	if (ret)
2514 		return ret;
2515 
2516 	intel_crtc_compute_pixel_rate(crtc_state);
2517 
2518 	if (crtc_state->has_pch_encoder)
2519 		return ilk_fdi_compute_config(crtc, crtc_state);
2520 
2521 	return 0;
2522 }
2523 
2524 static void
2525 intel_reduce_m_n_ratio(u32 *num, u32 *den)
2526 {
2527 	while (*num > DATA_LINK_M_N_MASK ||
2528 	       *den > DATA_LINK_M_N_MASK) {
2529 		*num >>= 1;
2530 		*den >>= 1;
2531 	}
2532 }
2533 
2534 static void compute_m_n(u32 *ret_m, u32 *ret_n,
2535 			u32 m, u32 n, u32 constant_n)
2536 {
2537 	if (constant_n)
2538 		*ret_n = constant_n;
2539 	else
2540 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
2541 
2542 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
2543 	intel_reduce_m_n_ratio(ret_m, ret_n);
2544 }
2545 
2546 void
2547 intel_link_compute_m_n(u16 bits_per_pixel_x16, int nlanes,
2548 		       int pixel_clock, int link_clock,
2549 		       int bw_overhead,
2550 		       struct intel_link_m_n *m_n)
2551 {
2552 	u32 link_symbol_clock = intel_dp_link_symbol_clock(link_clock);
2553 	u32 data_m = intel_dp_effective_data_rate(pixel_clock, bits_per_pixel_x16,
2554 						  bw_overhead);
2555 	u32 data_n = drm_dp_max_dprx_data_rate(link_clock, nlanes);
2556 
2557 	/*
2558 	 * Windows/BIOS uses fixed M/N values always. Follow suit.
2559 	 *
2560 	 * Also several DP dongles in particular seem to be fussy
2561 	 * about too large link M/N values. Presumably the 20bit
2562 	 * value used by Windows/BIOS is acceptable to everyone.
2563 	 */
2564 	m_n->tu = 64;
2565 	compute_m_n(&m_n->data_m, &m_n->data_n,
2566 		    data_m, data_n,
2567 		    0x8000000);
2568 
2569 	compute_m_n(&m_n->link_m, &m_n->link_n,
2570 		    pixel_clock, link_symbol_clock,
2571 		    0x80000);
2572 }
2573 
2574 void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
2575 {
2576 	/*
2577 	 * There may be no VBT; and if the BIOS enabled SSC we can
2578 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
2579 	 * BIOS isn't using it, don't assume it will work even if the VBT
2580 	 * indicates as much.
2581 	 */
2582 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
2583 		bool bios_lvds_use_ssc = intel_de_read(dev_priv,
2584 						       PCH_DREF_CONTROL) &
2585 			DREF_SSC1_ENABLE;
2586 
2587 		if (dev_priv->display.vbt.lvds_use_ssc != bios_lvds_use_ssc) {
2588 			drm_dbg_kms(&dev_priv->drm,
2589 				    "SSC %s by BIOS, overriding VBT which says %s\n",
2590 				    str_enabled_disabled(bios_lvds_use_ssc),
2591 				    str_enabled_disabled(dev_priv->display.vbt.lvds_use_ssc));
2592 			dev_priv->display.vbt.lvds_use_ssc = bios_lvds_use_ssc;
2593 		}
2594 	}
2595 }
2596 
2597 void intel_zero_m_n(struct intel_link_m_n *m_n)
2598 {
2599 	/* corresponds to 0 register value */
2600 	memset(m_n, 0, sizeof(*m_n));
2601 	m_n->tu = 1;
2602 }
2603 
2604 void intel_set_m_n(struct drm_i915_private *i915,
2605 		   const struct intel_link_m_n *m_n,
2606 		   i915_reg_t data_m_reg, i915_reg_t data_n_reg,
2607 		   i915_reg_t link_m_reg, i915_reg_t link_n_reg)
2608 {
2609 	intel_de_write(i915, data_m_reg, TU_SIZE(m_n->tu) | m_n->data_m);
2610 	intel_de_write(i915, data_n_reg, m_n->data_n);
2611 	intel_de_write(i915, link_m_reg, m_n->link_m);
2612 	/*
2613 	 * On BDW+ writing LINK_N arms the double buffered update
2614 	 * of all the M/N registers, so it must be written last.
2615 	 */
2616 	intel_de_write(i915, link_n_reg, m_n->link_n);
2617 }
2618 
2619 bool intel_cpu_transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
2620 				    enum transcoder transcoder)
2621 {
2622 	if (IS_HASWELL(dev_priv))
2623 		return transcoder == TRANSCODER_EDP;
2624 
2625 	return IS_DISPLAY_VER(dev_priv, 5, 7) || IS_CHERRYVIEW(dev_priv);
2626 }
2627 
2628 void intel_cpu_transcoder_set_m1_n1(struct intel_crtc *crtc,
2629 				    enum transcoder transcoder,
2630 				    const struct intel_link_m_n *m_n)
2631 {
2632 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2633 	enum pipe pipe = crtc->pipe;
2634 
2635 	if (DISPLAY_VER(dev_priv) >= 5)
2636 		intel_set_m_n(dev_priv, m_n,
2637 			      PIPE_DATA_M1(transcoder), PIPE_DATA_N1(transcoder),
2638 			      PIPE_LINK_M1(transcoder), PIPE_LINK_N1(transcoder));
2639 	else
2640 		intel_set_m_n(dev_priv, m_n,
2641 			      PIPE_DATA_M_G4X(pipe), PIPE_DATA_N_G4X(pipe),
2642 			      PIPE_LINK_M_G4X(pipe), PIPE_LINK_N_G4X(pipe));
2643 }
2644 
2645 void intel_cpu_transcoder_set_m2_n2(struct intel_crtc *crtc,
2646 				    enum transcoder transcoder,
2647 				    const struct intel_link_m_n *m_n)
2648 {
2649 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2650 
2651 	if (!intel_cpu_transcoder_has_m2_n2(dev_priv, transcoder))
2652 		return;
2653 
2654 	intel_set_m_n(dev_priv, m_n,
2655 		      PIPE_DATA_M2(transcoder), PIPE_DATA_N2(transcoder),
2656 		      PIPE_LINK_M2(transcoder), PIPE_LINK_N2(transcoder));
2657 }
2658 
2659 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
2660 {
2661 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2662 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2663 	enum pipe pipe = crtc->pipe;
2664 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2665 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2666 	u32 crtc_vdisplay, crtc_vtotal, crtc_vblank_start, crtc_vblank_end;
2667 	int vsyncshift = 0;
2668 
2669 	/* We need to be careful not to changed the adjusted mode, for otherwise
2670 	 * the hw state checker will get angry at the mismatch. */
2671 	crtc_vdisplay = adjusted_mode->crtc_vdisplay;
2672 	crtc_vtotal = adjusted_mode->crtc_vtotal;
2673 	crtc_vblank_start = adjusted_mode->crtc_vblank_start;
2674 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
2675 
2676 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
2677 		/* the chip adds 2 halflines automatically */
2678 		crtc_vtotal -= 1;
2679 		crtc_vblank_end -= 1;
2680 
2681 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
2682 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
2683 		else
2684 			vsyncshift = adjusted_mode->crtc_hsync_start -
2685 				adjusted_mode->crtc_htotal / 2;
2686 		if (vsyncshift < 0)
2687 			vsyncshift += adjusted_mode->crtc_htotal;
2688 	}
2689 
2690 	/*
2691 	 * VBLANK_START no longer works on ADL+, instead we must use
2692 	 * TRANS_SET_CONTEXT_LATENCY to configure the pipe vblank start.
2693 	 */
2694 	if (DISPLAY_VER(dev_priv) >= 13) {
2695 		intel_de_write(dev_priv, TRANS_SET_CONTEXT_LATENCY(cpu_transcoder),
2696 			       crtc_vblank_start - crtc_vdisplay);
2697 
2698 		/*
2699 		 * VBLANK_START not used by hw, just clear it
2700 		 * to make it stand out in register dumps.
2701 		 */
2702 		crtc_vblank_start = 1;
2703 	}
2704 
2705 	if (DISPLAY_VER(dev_priv) >= 4)
2706 		intel_de_write(dev_priv, TRANS_VSYNCSHIFT(cpu_transcoder),
2707 			       vsyncshift);
2708 
2709 	intel_de_write(dev_priv, TRANS_HTOTAL(cpu_transcoder),
2710 		       HACTIVE(adjusted_mode->crtc_hdisplay - 1) |
2711 		       HTOTAL(adjusted_mode->crtc_htotal - 1));
2712 	intel_de_write(dev_priv, TRANS_HBLANK(cpu_transcoder),
2713 		       HBLANK_START(adjusted_mode->crtc_hblank_start - 1) |
2714 		       HBLANK_END(adjusted_mode->crtc_hblank_end - 1));
2715 	intel_de_write(dev_priv, TRANS_HSYNC(cpu_transcoder),
2716 		       HSYNC_START(adjusted_mode->crtc_hsync_start - 1) |
2717 		       HSYNC_END(adjusted_mode->crtc_hsync_end - 1));
2718 
2719 	intel_de_write(dev_priv, TRANS_VTOTAL(cpu_transcoder),
2720 		       VACTIVE(crtc_vdisplay - 1) |
2721 		       VTOTAL(crtc_vtotal - 1));
2722 	intel_de_write(dev_priv, TRANS_VBLANK(cpu_transcoder),
2723 		       VBLANK_START(crtc_vblank_start - 1) |
2724 		       VBLANK_END(crtc_vblank_end - 1));
2725 	intel_de_write(dev_priv, TRANS_VSYNC(cpu_transcoder),
2726 		       VSYNC_START(adjusted_mode->crtc_vsync_start - 1) |
2727 		       VSYNC_END(adjusted_mode->crtc_vsync_end - 1));
2728 
2729 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
2730 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
2731 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
2732 	 * bits. */
2733 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
2734 	    (pipe == PIPE_B || pipe == PIPE_C))
2735 		intel_de_write(dev_priv, TRANS_VTOTAL(pipe),
2736 			       VACTIVE(crtc_vdisplay - 1) |
2737 			       VTOTAL(crtc_vtotal - 1));
2738 }
2739 
2740 static void intel_set_transcoder_timings_lrr(const struct intel_crtc_state *crtc_state)
2741 {
2742 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2743 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2744 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2745 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
2746 	u32 crtc_vdisplay, crtc_vtotal, crtc_vblank_start, crtc_vblank_end;
2747 
2748 	crtc_vdisplay = adjusted_mode->crtc_vdisplay;
2749 	crtc_vtotal = adjusted_mode->crtc_vtotal;
2750 	crtc_vblank_start = adjusted_mode->crtc_vblank_start;
2751 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
2752 
2753 	drm_WARN_ON(&dev_priv->drm, adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE);
2754 
2755 	/*
2756 	 * The hardware actually ignores TRANS_VBLANK.VBLANK_END in DP mode.
2757 	 * But let's write it anyway to keep the state checker happy.
2758 	 */
2759 	intel_de_write(dev_priv, TRANS_VBLANK(cpu_transcoder),
2760 		       VBLANK_START(crtc_vblank_start - 1) |
2761 		       VBLANK_END(crtc_vblank_end - 1));
2762 	/*
2763 	 * The double buffer latch point for TRANS_VTOTAL
2764 	 * is the transcoder's undelayed vblank.
2765 	 */
2766 	intel_de_write(dev_priv, TRANS_VTOTAL(cpu_transcoder),
2767 		       VACTIVE(crtc_vdisplay - 1) |
2768 		       VTOTAL(crtc_vtotal - 1));
2769 }
2770 
2771 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
2772 {
2773 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2774 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2775 	int width = drm_rect_width(&crtc_state->pipe_src);
2776 	int height = drm_rect_height(&crtc_state->pipe_src);
2777 	enum pipe pipe = crtc->pipe;
2778 
2779 	/* pipesrc controls the size that is scaled from, which should
2780 	 * always be the user's requested size.
2781 	 */
2782 	intel_de_write(dev_priv, PIPESRC(pipe),
2783 		       PIPESRC_WIDTH(width - 1) | PIPESRC_HEIGHT(height - 1));
2784 }
2785 
2786 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
2787 {
2788 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2789 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2790 
2791 	if (DISPLAY_VER(dev_priv) == 2)
2792 		return false;
2793 
2794 	if (DISPLAY_VER(dev_priv) >= 9 ||
2795 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
2796 		return intel_de_read(dev_priv, TRANSCONF(cpu_transcoder)) & TRANSCONF_INTERLACE_MASK_HSW;
2797 	else
2798 		return intel_de_read(dev_priv, TRANSCONF(cpu_transcoder)) & TRANSCONF_INTERLACE_MASK;
2799 }
2800 
2801 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
2802 					 struct intel_crtc_state *pipe_config)
2803 {
2804 	struct drm_device *dev = crtc->base.dev;
2805 	struct drm_i915_private *dev_priv = to_i915(dev);
2806 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
2807 	struct drm_display_mode *adjusted_mode = &pipe_config->hw.adjusted_mode;
2808 	u32 tmp;
2809 
2810 	tmp = intel_de_read(dev_priv, TRANS_HTOTAL(cpu_transcoder));
2811 	adjusted_mode->crtc_hdisplay = REG_FIELD_GET(HACTIVE_MASK, tmp) + 1;
2812 	adjusted_mode->crtc_htotal = REG_FIELD_GET(HTOTAL_MASK, tmp) + 1;
2813 
2814 	if (!transcoder_is_dsi(cpu_transcoder)) {
2815 		tmp = intel_de_read(dev_priv, TRANS_HBLANK(cpu_transcoder));
2816 		adjusted_mode->crtc_hblank_start = REG_FIELD_GET(HBLANK_START_MASK, tmp) + 1;
2817 		adjusted_mode->crtc_hblank_end = REG_FIELD_GET(HBLANK_END_MASK, tmp) + 1;
2818 	}
2819 
2820 	tmp = intel_de_read(dev_priv, TRANS_HSYNC(cpu_transcoder));
2821 	adjusted_mode->crtc_hsync_start = REG_FIELD_GET(HSYNC_START_MASK, tmp) + 1;
2822 	adjusted_mode->crtc_hsync_end = REG_FIELD_GET(HSYNC_END_MASK, tmp) + 1;
2823 
2824 	tmp = intel_de_read(dev_priv, TRANS_VTOTAL(cpu_transcoder));
2825 	adjusted_mode->crtc_vdisplay = REG_FIELD_GET(VACTIVE_MASK, tmp) + 1;
2826 	adjusted_mode->crtc_vtotal = REG_FIELD_GET(VTOTAL_MASK, tmp) + 1;
2827 
2828 	/* FIXME TGL+ DSI transcoders have this! */
2829 	if (!transcoder_is_dsi(cpu_transcoder)) {
2830 		tmp = intel_de_read(dev_priv, TRANS_VBLANK(cpu_transcoder));
2831 		adjusted_mode->crtc_vblank_start = REG_FIELD_GET(VBLANK_START_MASK, tmp) + 1;
2832 		adjusted_mode->crtc_vblank_end = REG_FIELD_GET(VBLANK_END_MASK, tmp) + 1;
2833 	}
2834 	tmp = intel_de_read(dev_priv, TRANS_VSYNC(cpu_transcoder));
2835 	adjusted_mode->crtc_vsync_start = REG_FIELD_GET(VSYNC_START_MASK, tmp) + 1;
2836 	adjusted_mode->crtc_vsync_end = REG_FIELD_GET(VSYNC_END_MASK, tmp) + 1;
2837 
2838 	if (intel_pipe_is_interlaced(pipe_config)) {
2839 		adjusted_mode->flags |= DRM_MODE_FLAG_INTERLACE;
2840 		adjusted_mode->crtc_vtotal += 1;
2841 		adjusted_mode->crtc_vblank_end += 1;
2842 	}
2843 
2844 	if (DISPLAY_VER(dev_priv) >= 13 && !transcoder_is_dsi(cpu_transcoder))
2845 		adjusted_mode->crtc_vblank_start =
2846 			adjusted_mode->crtc_vdisplay +
2847 			intel_de_read(dev_priv, TRANS_SET_CONTEXT_LATENCY(cpu_transcoder));
2848 }
2849 
2850 static void intel_bigjoiner_adjust_pipe_src(struct intel_crtc_state *crtc_state)
2851 {
2852 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2853 	int num_pipes = intel_bigjoiner_num_pipes(crtc_state);
2854 	enum pipe master_pipe, pipe = crtc->pipe;
2855 	int width;
2856 
2857 	if (num_pipes < 2)
2858 		return;
2859 
2860 	master_pipe = bigjoiner_master_pipe(crtc_state);
2861 	width = drm_rect_width(&crtc_state->pipe_src);
2862 
2863 	drm_rect_translate_to(&crtc_state->pipe_src,
2864 			      (pipe - master_pipe) * width, 0);
2865 }
2866 
2867 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
2868 				    struct intel_crtc_state *pipe_config)
2869 {
2870 	struct drm_device *dev = crtc->base.dev;
2871 	struct drm_i915_private *dev_priv = to_i915(dev);
2872 	u32 tmp;
2873 
2874 	tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
2875 
2876 	drm_rect_init(&pipe_config->pipe_src, 0, 0,
2877 		      REG_FIELD_GET(PIPESRC_WIDTH_MASK, tmp) + 1,
2878 		      REG_FIELD_GET(PIPESRC_HEIGHT_MASK, tmp) + 1);
2879 
2880 	intel_bigjoiner_adjust_pipe_src(pipe_config);
2881 }
2882 
2883 void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
2884 {
2885 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2886 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2887 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2888 	u32 val = 0;
2889 
2890 	/*
2891 	 * - We keep both pipes enabled on 830
2892 	 * - During modeset the pipe is still disabled and must remain so
2893 	 * - During fastset the pipe is already enabled and must remain so
2894 	 */
2895 	if (IS_I830(dev_priv) || !intel_crtc_needs_modeset(crtc_state))
2896 		val |= TRANSCONF_ENABLE;
2897 
2898 	if (crtc_state->double_wide)
2899 		val |= TRANSCONF_DOUBLE_WIDE;
2900 
2901 	/* only g4x and later have fancy bpc/dither controls */
2902 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
2903 	    IS_CHERRYVIEW(dev_priv)) {
2904 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
2905 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
2906 			val |= TRANSCONF_DITHER_EN |
2907 				TRANSCONF_DITHER_TYPE_SP;
2908 
2909 		switch (crtc_state->pipe_bpp) {
2910 		default:
2911 			/* Case prevented by intel_choose_pipe_bpp_dither. */
2912 			MISSING_CASE(crtc_state->pipe_bpp);
2913 			fallthrough;
2914 		case 18:
2915 			val |= TRANSCONF_BPC_6;
2916 			break;
2917 		case 24:
2918 			val |= TRANSCONF_BPC_8;
2919 			break;
2920 		case 30:
2921 			val |= TRANSCONF_BPC_10;
2922 			break;
2923 		}
2924 	}
2925 
2926 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
2927 		if (DISPLAY_VER(dev_priv) < 4 ||
2928 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
2929 			val |= TRANSCONF_INTERLACE_W_FIELD_INDICATION;
2930 		else
2931 			val |= TRANSCONF_INTERLACE_W_SYNC_SHIFT;
2932 	} else {
2933 		val |= TRANSCONF_INTERLACE_PROGRESSIVE;
2934 	}
2935 
2936 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
2937 	     crtc_state->limited_color_range)
2938 		val |= TRANSCONF_COLOR_RANGE_SELECT;
2939 
2940 	val |= TRANSCONF_GAMMA_MODE(crtc_state->gamma_mode);
2941 
2942 	if (crtc_state->wgc_enable)
2943 		val |= TRANSCONF_WGC_ENABLE;
2944 
2945 	val |= TRANSCONF_FRAME_START_DELAY(crtc_state->framestart_delay - 1);
2946 
2947 	intel_de_write(dev_priv, TRANSCONF(cpu_transcoder), val);
2948 	intel_de_posting_read(dev_priv, TRANSCONF(cpu_transcoder));
2949 }
2950 
2951 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
2952 {
2953 	if (IS_I830(dev_priv))
2954 		return false;
2955 
2956 	return DISPLAY_VER(dev_priv) >= 4 ||
2957 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
2958 }
2959 
2960 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
2961 {
2962 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2963 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2964 	enum pipe pipe;
2965 	u32 tmp;
2966 
2967 	if (!i9xx_has_pfit(dev_priv))
2968 		return;
2969 
2970 	tmp = intel_de_read(dev_priv, PFIT_CONTROL);
2971 	if (!(tmp & PFIT_ENABLE))
2972 		return;
2973 
2974 	/* Check whether the pfit is attached to our pipe. */
2975 	if (DISPLAY_VER(dev_priv) >= 4)
2976 		pipe = REG_FIELD_GET(PFIT_PIPE_MASK, tmp);
2977 	else
2978 		pipe = PIPE_B;
2979 
2980 	if (pipe != crtc->pipe)
2981 		return;
2982 
2983 	crtc_state->gmch_pfit.control = tmp;
2984 	crtc_state->gmch_pfit.pgm_ratios =
2985 		intel_de_read(dev_priv, PFIT_PGM_RATIOS);
2986 }
2987 
2988 static enum intel_output_format
2989 bdw_get_pipe_misc_output_format(struct intel_crtc *crtc)
2990 {
2991 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2992 	u32 tmp;
2993 
2994 	tmp = intel_de_read(dev_priv, PIPE_MISC(crtc->pipe));
2995 
2996 	if (tmp & PIPE_MISC_YUV420_ENABLE) {
2997 		/* We support 4:2:0 in full blend mode only */
2998 		drm_WARN_ON(&dev_priv->drm,
2999 			    (tmp & PIPE_MISC_YUV420_MODE_FULL_BLEND) == 0);
3000 
3001 		return INTEL_OUTPUT_FORMAT_YCBCR420;
3002 	} else if (tmp & PIPE_MISC_OUTPUT_COLORSPACE_YUV) {
3003 		return INTEL_OUTPUT_FORMAT_YCBCR444;
3004 	} else {
3005 		return INTEL_OUTPUT_FORMAT_RGB;
3006 	}
3007 }
3008 
3009 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
3010 				 struct intel_crtc_state *pipe_config)
3011 {
3012 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3013 	enum intel_display_power_domain power_domain;
3014 	intel_wakeref_t wakeref;
3015 	u32 tmp;
3016 	bool ret;
3017 
3018 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
3019 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
3020 	if (!wakeref)
3021 		return false;
3022 
3023 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
3024 	pipe_config->sink_format = pipe_config->output_format;
3025 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
3026 	pipe_config->shared_dpll = NULL;
3027 
3028 	ret = false;
3029 
3030 	tmp = intel_de_read(dev_priv, TRANSCONF(pipe_config->cpu_transcoder));
3031 	if (!(tmp & TRANSCONF_ENABLE))
3032 		goto out;
3033 
3034 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
3035 	    IS_CHERRYVIEW(dev_priv)) {
3036 		switch (tmp & TRANSCONF_BPC_MASK) {
3037 		case TRANSCONF_BPC_6:
3038 			pipe_config->pipe_bpp = 18;
3039 			break;
3040 		case TRANSCONF_BPC_8:
3041 			pipe_config->pipe_bpp = 24;
3042 			break;
3043 		case TRANSCONF_BPC_10:
3044 			pipe_config->pipe_bpp = 30;
3045 			break;
3046 		default:
3047 			MISSING_CASE(tmp);
3048 			break;
3049 		}
3050 	}
3051 
3052 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
3053 	    (tmp & TRANSCONF_COLOR_RANGE_SELECT))
3054 		pipe_config->limited_color_range = true;
3055 
3056 	pipe_config->gamma_mode = REG_FIELD_GET(TRANSCONF_GAMMA_MODE_MASK_I9XX, tmp);
3057 
3058 	pipe_config->framestart_delay = REG_FIELD_GET(TRANSCONF_FRAME_START_DELAY_MASK, tmp) + 1;
3059 
3060 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
3061 	    (tmp & TRANSCONF_WGC_ENABLE))
3062 		pipe_config->wgc_enable = true;
3063 
3064 	intel_color_get_config(pipe_config);
3065 
3066 	if (DISPLAY_VER(dev_priv) < 4)
3067 		pipe_config->double_wide = tmp & TRANSCONF_DOUBLE_WIDE;
3068 
3069 	intel_get_transcoder_timings(crtc, pipe_config);
3070 	intel_get_pipe_src_size(crtc, pipe_config);
3071 
3072 	i9xx_get_pfit_config(pipe_config);
3073 
3074 	if (DISPLAY_VER(dev_priv) >= 4) {
3075 		/* No way to read it out on pipes B and C */
3076 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
3077 			tmp = dev_priv->display.state.chv_dpll_md[crtc->pipe];
3078 		else
3079 			tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
3080 		pipe_config->pixel_multiplier =
3081 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
3082 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
3083 		pipe_config->dpll_hw_state.dpll_md = tmp;
3084 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
3085 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
3086 		tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
3087 		pipe_config->pixel_multiplier =
3088 			((tmp & SDVO_MULTIPLIER_MASK)
3089 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
3090 	} else {
3091 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
3092 		 * port and will be fixed up in the encoder->get_config
3093 		 * function. */
3094 		pipe_config->pixel_multiplier = 1;
3095 	}
3096 	pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
3097 							DPLL(crtc->pipe));
3098 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
3099 		pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
3100 							       FP0(crtc->pipe));
3101 		pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
3102 							       FP1(crtc->pipe));
3103 	} else {
3104 		/* Mask out read-only status bits. */
3105 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
3106 						     DPLL_PORTC_READY_MASK |
3107 						     DPLL_PORTB_READY_MASK);
3108 	}
3109 
3110 	if (IS_CHERRYVIEW(dev_priv))
3111 		chv_crtc_clock_get(crtc, pipe_config);
3112 	else if (IS_VALLEYVIEW(dev_priv))
3113 		vlv_crtc_clock_get(crtc, pipe_config);
3114 	else
3115 		i9xx_crtc_clock_get(crtc, pipe_config);
3116 
3117 	/*
3118 	 * Normally the dotclock is filled in by the encoder .get_config()
3119 	 * but in case the pipe is enabled w/o any ports we need a sane
3120 	 * default.
3121 	 */
3122 	pipe_config->hw.adjusted_mode.crtc_clock =
3123 		pipe_config->port_clock / pipe_config->pixel_multiplier;
3124 
3125 	ret = true;
3126 
3127 out:
3128 	intel_display_power_put(dev_priv, power_domain, wakeref);
3129 
3130 	return ret;
3131 }
3132 
3133 void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
3134 {
3135 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3136 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3137 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3138 	u32 val = 0;
3139 
3140 	/*
3141 	 * - During modeset the pipe is still disabled and must remain so
3142 	 * - During fastset the pipe is already enabled and must remain so
3143 	 */
3144 	if (!intel_crtc_needs_modeset(crtc_state))
3145 		val |= TRANSCONF_ENABLE;
3146 
3147 	switch (crtc_state->pipe_bpp) {
3148 	default:
3149 		/* Case prevented by intel_choose_pipe_bpp_dither. */
3150 		MISSING_CASE(crtc_state->pipe_bpp);
3151 		fallthrough;
3152 	case 18:
3153 		val |= TRANSCONF_BPC_6;
3154 		break;
3155 	case 24:
3156 		val |= TRANSCONF_BPC_8;
3157 		break;
3158 	case 30:
3159 		val |= TRANSCONF_BPC_10;
3160 		break;
3161 	case 36:
3162 		val |= TRANSCONF_BPC_12;
3163 		break;
3164 	}
3165 
3166 	if (crtc_state->dither)
3167 		val |= TRANSCONF_DITHER_EN | TRANSCONF_DITHER_TYPE_SP;
3168 
3169 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
3170 		val |= TRANSCONF_INTERLACE_IF_ID_ILK;
3171 	else
3172 		val |= TRANSCONF_INTERLACE_PF_PD_ILK;
3173 
3174 	/*
3175 	 * This would end up with an odd purple hue over
3176 	 * the entire display. Make sure we don't do it.
3177 	 */
3178 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
3179 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
3180 
3181 	if (crtc_state->limited_color_range &&
3182 	    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
3183 		val |= TRANSCONF_COLOR_RANGE_SELECT;
3184 
3185 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
3186 		val |= TRANSCONF_OUTPUT_COLORSPACE_YUV709;
3187 
3188 	val |= TRANSCONF_GAMMA_MODE(crtc_state->gamma_mode);
3189 
3190 	val |= TRANSCONF_FRAME_START_DELAY(crtc_state->framestart_delay - 1);
3191 	val |= TRANSCONF_MSA_TIMING_DELAY(crtc_state->msa_timing_delay);
3192 
3193 	intel_de_write(dev_priv, TRANSCONF(cpu_transcoder), val);
3194 	intel_de_posting_read(dev_priv, TRANSCONF(cpu_transcoder));
3195 }
3196 
3197 static void hsw_set_transconf(const struct intel_crtc_state *crtc_state)
3198 {
3199 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3200 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3201 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3202 	u32 val = 0;
3203 
3204 	/*
3205 	 * - During modeset the pipe is still disabled and must remain so
3206 	 * - During fastset the pipe is already enabled and must remain so
3207 	 */
3208 	if (!intel_crtc_needs_modeset(crtc_state))
3209 		val |= TRANSCONF_ENABLE;
3210 
3211 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
3212 		val |= TRANSCONF_DITHER_EN | TRANSCONF_DITHER_TYPE_SP;
3213 
3214 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
3215 		val |= TRANSCONF_INTERLACE_IF_ID_ILK;
3216 	else
3217 		val |= TRANSCONF_INTERLACE_PF_PD_ILK;
3218 
3219 	if (IS_HASWELL(dev_priv) &&
3220 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
3221 		val |= TRANSCONF_OUTPUT_COLORSPACE_YUV_HSW;
3222 
3223 	intel_de_write(dev_priv, TRANSCONF(cpu_transcoder), val);
3224 	intel_de_posting_read(dev_priv, TRANSCONF(cpu_transcoder));
3225 }
3226 
3227 static void bdw_set_pipe_misc(const struct intel_crtc_state *crtc_state)
3228 {
3229 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3230 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3231 	u32 val = 0;
3232 
3233 	switch (crtc_state->pipe_bpp) {
3234 	case 18:
3235 		val |= PIPE_MISC_BPC_6;
3236 		break;
3237 	case 24:
3238 		val |= PIPE_MISC_BPC_8;
3239 		break;
3240 	case 30:
3241 		val |= PIPE_MISC_BPC_10;
3242 		break;
3243 	case 36:
3244 		/* Port output 12BPC defined for ADLP+ */
3245 		if (DISPLAY_VER(dev_priv) >= 13)
3246 			val |= PIPE_MISC_BPC_12_ADLP;
3247 		break;
3248 	default:
3249 		MISSING_CASE(crtc_state->pipe_bpp);
3250 		break;
3251 	}
3252 
3253 	if (crtc_state->dither)
3254 		val |= PIPE_MISC_DITHER_ENABLE | PIPE_MISC_DITHER_TYPE_SP;
3255 
3256 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
3257 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
3258 		val |= PIPE_MISC_OUTPUT_COLORSPACE_YUV;
3259 
3260 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
3261 		val |= PIPE_MISC_YUV420_ENABLE |
3262 			PIPE_MISC_YUV420_MODE_FULL_BLEND;
3263 
3264 	if (DISPLAY_VER(dev_priv) >= 11 && is_hdr_mode(crtc_state))
3265 		val |= PIPE_MISC_HDR_MODE_PRECISION;
3266 
3267 	if (DISPLAY_VER(dev_priv) >= 12)
3268 		val |= PIPE_MISC_PIXEL_ROUNDING_TRUNC;
3269 
3270 	/* allow PSR with sprite enabled */
3271 	if (IS_BROADWELL(dev_priv))
3272 		val |= PIPE_MISC_PSR_MASK_SPRITE_ENABLE;
3273 
3274 	intel_de_write(dev_priv, PIPE_MISC(crtc->pipe), val);
3275 }
3276 
3277 int bdw_get_pipe_misc_bpp(struct intel_crtc *crtc)
3278 {
3279 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3280 	u32 tmp;
3281 
3282 	tmp = intel_de_read(dev_priv, PIPE_MISC(crtc->pipe));
3283 
3284 	switch (tmp & PIPE_MISC_BPC_MASK) {
3285 	case PIPE_MISC_BPC_6:
3286 		return 18;
3287 	case PIPE_MISC_BPC_8:
3288 		return 24;
3289 	case PIPE_MISC_BPC_10:
3290 		return 30;
3291 	/*
3292 	 * PORT OUTPUT 12 BPC defined for ADLP+.
3293 	 *
3294 	 * TODO:
3295 	 * For previous platforms with DSI interface, bits 5:7
3296 	 * are used for storing pipe_bpp irrespective of dithering.
3297 	 * Since the value of 12 BPC is not defined for these bits
3298 	 * on older platforms, need to find a workaround for 12 BPC
3299 	 * MIPI DSI HW readout.
3300 	 */
3301 	case PIPE_MISC_BPC_12_ADLP:
3302 		if (DISPLAY_VER(dev_priv) >= 13)
3303 			return 36;
3304 		fallthrough;
3305 	default:
3306 		MISSING_CASE(tmp);
3307 		return 0;
3308 	}
3309 }
3310 
3311 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
3312 {
3313 	/*
3314 	 * Account for spread spectrum to avoid
3315 	 * oversubscribing the link. Max center spread
3316 	 * is 2.5%; use 5% for safety's sake.
3317 	 */
3318 	u32 bps = target_clock * bpp * 21 / 20;
3319 	return DIV_ROUND_UP(bps, link_bw * 8);
3320 }
3321 
3322 void intel_get_m_n(struct drm_i915_private *i915,
3323 		   struct intel_link_m_n *m_n,
3324 		   i915_reg_t data_m_reg, i915_reg_t data_n_reg,
3325 		   i915_reg_t link_m_reg, i915_reg_t link_n_reg)
3326 {
3327 	m_n->link_m = intel_de_read(i915, link_m_reg) & DATA_LINK_M_N_MASK;
3328 	m_n->link_n = intel_de_read(i915, link_n_reg) & DATA_LINK_M_N_MASK;
3329 	m_n->data_m = intel_de_read(i915, data_m_reg) & DATA_LINK_M_N_MASK;
3330 	m_n->data_n = intel_de_read(i915, data_n_reg) & DATA_LINK_M_N_MASK;
3331 	m_n->tu = REG_FIELD_GET(TU_SIZE_MASK, intel_de_read(i915, data_m_reg)) + 1;
3332 }
3333 
3334 void intel_cpu_transcoder_get_m1_n1(struct intel_crtc *crtc,
3335 				    enum transcoder transcoder,
3336 				    struct intel_link_m_n *m_n)
3337 {
3338 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3339 	enum pipe pipe = crtc->pipe;
3340 
3341 	if (DISPLAY_VER(dev_priv) >= 5)
3342 		intel_get_m_n(dev_priv, m_n,
3343 			      PIPE_DATA_M1(transcoder), PIPE_DATA_N1(transcoder),
3344 			      PIPE_LINK_M1(transcoder), PIPE_LINK_N1(transcoder));
3345 	else
3346 		intel_get_m_n(dev_priv, m_n,
3347 			      PIPE_DATA_M_G4X(pipe), PIPE_DATA_N_G4X(pipe),
3348 			      PIPE_LINK_M_G4X(pipe), PIPE_LINK_N_G4X(pipe));
3349 }
3350 
3351 void intel_cpu_transcoder_get_m2_n2(struct intel_crtc *crtc,
3352 				    enum transcoder transcoder,
3353 				    struct intel_link_m_n *m_n)
3354 {
3355 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3356 
3357 	if (!intel_cpu_transcoder_has_m2_n2(dev_priv, transcoder))
3358 		return;
3359 
3360 	intel_get_m_n(dev_priv, m_n,
3361 		      PIPE_DATA_M2(transcoder), PIPE_DATA_N2(transcoder),
3362 		      PIPE_LINK_M2(transcoder), PIPE_LINK_N2(transcoder));
3363 }
3364 
3365 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
3366 {
3367 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3368 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3369 	u32 ctl, pos, size;
3370 	enum pipe pipe;
3371 
3372 	ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
3373 	if ((ctl & PF_ENABLE) == 0)
3374 		return;
3375 
3376 	if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
3377 		pipe = REG_FIELD_GET(PF_PIPE_SEL_MASK_IVB, ctl);
3378 	else
3379 		pipe = crtc->pipe;
3380 
3381 	crtc_state->pch_pfit.enabled = true;
3382 
3383 	pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
3384 	size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
3385 
3386 	drm_rect_init(&crtc_state->pch_pfit.dst,
3387 		      REG_FIELD_GET(PF_WIN_XPOS_MASK, pos),
3388 		      REG_FIELD_GET(PF_WIN_YPOS_MASK, pos),
3389 		      REG_FIELD_GET(PF_WIN_XSIZE_MASK, size),
3390 		      REG_FIELD_GET(PF_WIN_YSIZE_MASK, size));
3391 
3392 	/*
3393 	 * We currently do not free assignements of panel fitters on
3394 	 * ivb/hsw (since we don't use the higher upscaling modes which
3395 	 * differentiates them) so just WARN about this case for now.
3396 	 */
3397 	drm_WARN_ON(&dev_priv->drm, pipe != crtc->pipe);
3398 }
3399 
3400 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
3401 				struct intel_crtc_state *pipe_config)
3402 {
3403 	struct drm_device *dev = crtc->base.dev;
3404 	struct drm_i915_private *dev_priv = to_i915(dev);
3405 	enum intel_display_power_domain power_domain;
3406 	intel_wakeref_t wakeref;
3407 	u32 tmp;
3408 	bool ret;
3409 
3410 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
3411 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
3412 	if (!wakeref)
3413 		return false;
3414 
3415 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
3416 	pipe_config->shared_dpll = NULL;
3417 
3418 	ret = false;
3419 	tmp = intel_de_read(dev_priv, TRANSCONF(pipe_config->cpu_transcoder));
3420 	if (!(tmp & TRANSCONF_ENABLE))
3421 		goto out;
3422 
3423 	switch (tmp & TRANSCONF_BPC_MASK) {
3424 	case TRANSCONF_BPC_6:
3425 		pipe_config->pipe_bpp = 18;
3426 		break;
3427 	case TRANSCONF_BPC_8:
3428 		pipe_config->pipe_bpp = 24;
3429 		break;
3430 	case TRANSCONF_BPC_10:
3431 		pipe_config->pipe_bpp = 30;
3432 		break;
3433 	case TRANSCONF_BPC_12:
3434 		pipe_config->pipe_bpp = 36;
3435 		break;
3436 	default:
3437 		break;
3438 	}
3439 
3440 	if (tmp & TRANSCONF_COLOR_RANGE_SELECT)
3441 		pipe_config->limited_color_range = true;
3442 
3443 	switch (tmp & TRANSCONF_OUTPUT_COLORSPACE_MASK) {
3444 	case TRANSCONF_OUTPUT_COLORSPACE_YUV601:
3445 	case TRANSCONF_OUTPUT_COLORSPACE_YUV709:
3446 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
3447 		break;
3448 	default:
3449 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
3450 		break;
3451 	}
3452 
3453 	pipe_config->sink_format = pipe_config->output_format;
3454 
3455 	pipe_config->gamma_mode = REG_FIELD_GET(TRANSCONF_GAMMA_MODE_MASK_ILK, tmp);
3456 
3457 	pipe_config->framestart_delay = REG_FIELD_GET(TRANSCONF_FRAME_START_DELAY_MASK, tmp) + 1;
3458 
3459 	pipe_config->msa_timing_delay = REG_FIELD_GET(TRANSCONF_MSA_TIMING_DELAY_MASK, tmp);
3460 
3461 	intel_color_get_config(pipe_config);
3462 
3463 	pipe_config->pixel_multiplier = 1;
3464 
3465 	ilk_pch_get_config(pipe_config);
3466 
3467 	intel_get_transcoder_timings(crtc, pipe_config);
3468 	intel_get_pipe_src_size(crtc, pipe_config);
3469 
3470 	ilk_get_pfit_config(pipe_config);
3471 
3472 	ret = true;
3473 
3474 out:
3475 	intel_display_power_put(dev_priv, power_domain, wakeref);
3476 
3477 	return ret;
3478 }
3479 
3480 static u8 bigjoiner_pipes(struct drm_i915_private *i915)
3481 {
3482 	u8 pipes;
3483 
3484 	if (DISPLAY_VER(i915) >= 12)
3485 		pipes = BIT(PIPE_A) | BIT(PIPE_B) | BIT(PIPE_C) | BIT(PIPE_D);
3486 	else if (DISPLAY_VER(i915) >= 11)
3487 		pipes = BIT(PIPE_B) | BIT(PIPE_C);
3488 	else
3489 		pipes = 0;
3490 
3491 	return pipes & DISPLAY_RUNTIME_INFO(i915)->pipe_mask;
3492 }
3493 
3494 static bool transcoder_ddi_func_is_enabled(struct drm_i915_private *dev_priv,
3495 					   enum transcoder cpu_transcoder)
3496 {
3497 	enum intel_display_power_domain power_domain;
3498 	intel_wakeref_t wakeref;
3499 	u32 tmp = 0;
3500 
3501 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
3502 
3503 	with_intel_display_power_if_enabled(dev_priv, power_domain, wakeref)
3504 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3505 
3506 	return tmp & TRANS_DDI_FUNC_ENABLE;
3507 }
3508 
3509 static void enabled_bigjoiner_pipes(struct drm_i915_private *dev_priv,
3510 				    u8 *master_pipes, u8 *slave_pipes)
3511 {
3512 	struct intel_crtc *crtc;
3513 
3514 	*master_pipes = 0;
3515 	*slave_pipes = 0;
3516 
3517 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, crtc,
3518 					 bigjoiner_pipes(dev_priv)) {
3519 		enum intel_display_power_domain power_domain;
3520 		enum pipe pipe = crtc->pipe;
3521 		intel_wakeref_t wakeref;
3522 
3523 		power_domain = intel_dsc_power_domain(crtc, (enum transcoder) pipe);
3524 		with_intel_display_power_if_enabled(dev_priv, power_domain, wakeref) {
3525 			u32 tmp = intel_de_read(dev_priv, ICL_PIPE_DSS_CTL1(pipe));
3526 
3527 			if (!(tmp & BIG_JOINER_ENABLE))
3528 				continue;
3529 
3530 			if (tmp & MASTER_BIG_JOINER_ENABLE)
3531 				*master_pipes |= BIT(pipe);
3532 			else
3533 				*slave_pipes |= BIT(pipe);
3534 		}
3535 
3536 		if (DISPLAY_VER(dev_priv) < 13)
3537 			continue;
3538 
3539 		power_domain = POWER_DOMAIN_PIPE(pipe);
3540 		with_intel_display_power_if_enabled(dev_priv, power_domain, wakeref) {
3541 			u32 tmp = intel_de_read(dev_priv, ICL_PIPE_DSS_CTL1(pipe));
3542 
3543 			if (tmp & UNCOMPRESSED_JOINER_MASTER)
3544 				*master_pipes |= BIT(pipe);
3545 			if (tmp & UNCOMPRESSED_JOINER_SLAVE)
3546 				*slave_pipes |= BIT(pipe);
3547 		}
3548 	}
3549 
3550 	/* Bigjoiner pipes should always be consecutive master and slave */
3551 	drm_WARN(&dev_priv->drm, *slave_pipes != *master_pipes << 1,
3552 		 "Bigjoiner misconfigured (master pipes 0x%x, slave pipes 0x%x)\n",
3553 		 *master_pipes, *slave_pipes);
3554 }
3555 
3556 static enum pipe get_bigjoiner_master_pipe(enum pipe pipe, u8 master_pipes, u8 slave_pipes)
3557 {
3558 	if ((slave_pipes & BIT(pipe)) == 0)
3559 		return pipe;
3560 
3561 	/* ignore everything above our pipe */
3562 	master_pipes &= ~GENMASK(7, pipe);
3563 
3564 	/* highest remaining bit should be our master pipe */
3565 	return fls(master_pipes) - 1;
3566 }
3567 
3568 static u8 get_bigjoiner_slave_pipes(enum pipe pipe, u8 master_pipes, u8 slave_pipes)
3569 {
3570 	enum pipe master_pipe, next_master_pipe;
3571 
3572 	master_pipe = get_bigjoiner_master_pipe(pipe, master_pipes, slave_pipes);
3573 
3574 	if ((master_pipes & BIT(master_pipe)) == 0)
3575 		return 0;
3576 
3577 	/* ignore our master pipe and everything below it */
3578 	master_pipes &= ~GENMASK(master_pipe, 0);
3579 	/* make sure a high bit is set for the ffs() */
3580 	master_pipes |= BIT(7);
3581 	/* lowest remaining bit should be the next master pipe */
3582 	next_master_pipe = ffs(master_pipes) - 1;
3583 
3584 	return slave_pipes & GENMASK(next_master_pipe - 1, master_pipe);
3585 }
3586 
3587 static u8 hsw_panel_transcoders(struct drm_i915_private *i915)
3588 {
3589 	u8 panel_transcoder_mask = BIT(TRANSCODER_EDP);
3590 
3591 	if (DISPLAY_VER(i915) >= 11)
3592 		panel_transcoder_mask |= BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
3593 
3594 	return panel_transcoder_mask;
3595 }
3596 
3597 static u8 hsw_enabled_transcoders(struct intel_crtc *crtc)
3598 {
3599 	struct drm_device *dev = crtc->base.dev;
3600 	struct drm_i915_private *dev_priv = to_i915(dev);
3601 	u8 panel_transcoder_mask = hsw_panel_transcoders(dev_priv);
3602 	enum transcoder cpu_transcoder;
3603 	u8 master_pipes, slave_pipes;
3604 	u8 enabled_transcoders = 0;
3605 
3606 	/*
3607 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
3608 	 * consistency and less surprising code; it's in always on power).
3609 	 */
3610 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder,
3611 				       panel_transcoder_mask) {
3612 		enum intel_display_power_domain power_domain;
3613 		intel_wakeref_t wakeref;
3614 		enum pipe trans_pipe;
3615 		u32 tmp = 0;
3616 
3617 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
3618 		with_intel_display_power_if_enabled(dev_priv, power_domain, wakeref)
3619 			tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
3620 
3621 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
3622 			continue;
3623 
3624 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
3625 		default:
3626 			drm_WARN(dev, 1,
3627 				 "unknown pipe linked to transcoder %s\n",
3628 				 transcoder_name(cpu_transcoder));
3629 			fallthrough;
3630 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
3631 		case TRANS_DDI_EDP_INPUT_A_ON:
3632 			trans_pipe = PIPE_A;
3633 			break;
3634 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
3635 			trans_pipe = PIPE_B;
3636 			break;
3637 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
3638 			trans_pipe = PIPE_C;
3639 			break;
3640 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
3641 			trans_pipe = PIPE_D;
3642 			break;
3643 		}
3644 
3645 		if (trans_pipe == crtc->pipe)
3646 			enabled_transcoders |= BIT(cpu_transcoder);
3647 	}
3648 
3649 	/* single pipe or bigjoiner master */
3650 	cpu_transcoder = (enum transcoder) crtc->pipe;
3651 	if (transcoder_ddi_func_is_enabled(dev_priv, cpu_transcoder))
3652 		enabled_transcoders |= BIT(cpu_transcoder);
3653 
3654 	/* bigjoiner slave -> consider the master pipe's transcoder as well */
3655 	enabled_bigjoiner_pipes(dev_priv, &master_pipes, &slave_pipes);
3656 	if (slave_pipes & BIT(crtc->pipe)) {
3657 		cpu_transcoder = (enum transcoder)
3658 			get_bigjoiner_master_pipe(crtc->pipe, master_pipes, slave_pipes);
3659 		if (transcoder_ddi_func_is_enabled(dev_priv, cpu_transcoder))
3660 			enabled_transcoders |= BIT(cpu_transcoder);
3661 	}
3662 
3663 	return enabled_transcoders;
3664 }
3665 
3666 static bool has_edp_transcoders(u8 enabled_transcoders)
3667 {
3668 	return enabled_transcoders & BIT(TRANSCODER_EDP);
3669 }
3670 
3671 static bool has_dsi_transcoders(u8 enabled_transcoders)
3672 {
3673 	return enabled_transcoders & (BIT(TRANSCODER_DSI_0) |
3674 				      BIT(TRANSCODER_DSI_1));
3675 }
3676 
3677 static bool has_pipe_transcoders(u8 enabled_transcoders)
3678 {
3679 	return enabled_transcoders & ~(BIT(TRANSCODER_EDP) |
3680 				       BIT(TRANSCODER_DSI_0) |
3681 				       BIT(TRANSCODER_DSI_1));
3682 }
3683 
3684 static void assert_enabled_transcoders(struct drm_i915_private *i915,
3685 				       u8 enabled_transcoders)
3686 {
3687 	/* Only one type of transcoder please */
3688 	drm_WARN_ON(&i915->drm,
3689 		    has_edp_transcoders(enabled_transcoders) +
3690 		    has_dsi_transcoders(enabled_transcoders) +
3691 		    has_pipe_transcoders(enabled_transcoders) > 1);
3692 
3693 	/* Only DSI transcoders can be ganged */
3694 	drm_WARN_ON(&i915->drm,
3695 		    !has_dsi_transcoders(enabled_transcoders) &&
3696 		    !is_power_of_2(enabled_transcoders));
3697 }
3698 
3699 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
3700 				     struct intel_crtc_state *pipe_config,
3701 				     struct intel_display_power_domain_set *power_domain_set)
3702 {
3703 	struct drm_device *dev = crtc->base.dev;
3704 	struct drm_i915_private *dev_priv = to_i915(dev);
3705 	unsigned long enabled_transcoders;
3706 	u32 tmp;
3707 
3708 	enabled_transcoders = hsw_enabled_transcoders(crtc);
3709 	if (!enabled_transcoders)
3710 		return false;
3711 
3712 	assert_enabled_transcoders(dev_priv, enabled_transcoders);
3713 
3714 	/*
3715 	 * With the exception of DSI we should only ever have
3716 	 * a single enabled transcoder. With DSI let's just
3717 	 * pick the first one.
3718 	 */
3719 	pipe_config->cpu_transcoder = ffs(enabled_transcoders) - 1;
3720 
3721 	if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
3722 						       POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
3723 		return false;
3724 
3725 	if (hsw_panel_transcoders(dev_priv) & BIT(pipe_config->cpu_transcoder)) {
3726 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
3727 
3728 		if ((tmp & TRANS_DDI_EDP_INPUT_MASK) == TRANS_DDI_EDP_INPUT_A_ONOFF)
3729 			pipe_config->pch_pfit.force_thru = true;
3730 	}
3731 
3732 	tmp = intel_de_read(dev_priv, TRANSCONF(pipe_config->cpu_transcoder));
3733 
3734 	return tmp & TRANSCONF_ENABLE;
3735 }
3736 
3737 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
3738 					 struct intel_crtc_state *pipe_config,
3739 					 struct intel_display_power_domain_set *power_domain_set)
3740 {
3741 	struct drm_device *dev = crtc->base.dev;
3742 	struct drm_i915_private *dev_priv = to_i915(dev);
3743 	enum transcoder cpu_transcoder;
3744 	enum port port;
3745 	u32 tmp;
3746 
3747 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
3748 		if (port == PORT_A)
3749 			cpu_transcoder = TRANSCODER_DSI_A;
3750 		else
3751 			cpu_transcoder = TRANSCODER_DSI_C;
3752 
3753 		if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
3754 							       POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
3755 			continue;
3756 
3757 		/*
3758 		 * The PLL needs to be enabled with a valid divider
3759 		 * configuration, otherwise accessing DSI registers will hang
3760 		 * the machine. See BSpec North Display Engine
3761 		 * registers/MIPI[BXT]. We can break out here early, since we
3762 		 * need the same DSI PLL to be enabled for both DSI ports.
3763 		 */
3764 		if (!bxt_dsi_pll_is_enabled(dev_priv))
3765 			break;
3766 
3767 		/* XXX: this works for video mode only */
3768 		tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
3769 		if (!(tmp & DPI_ENABLE))
3770 			continue;
3771 
3772 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
3773 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
3774 			continue;
3775 
3776 		pipe_config->cpu_transcoder = cpu_transcoder;
3777 		break;
3778 	}
3779 
3780 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
3781 }
3782 
3783 static void intel_bigjoiner_get_config(struct intel_crtc_state *crtc_state)
3784 {
3785 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3786 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3787 	u8 master_pipes, slave_pipes;
3788 	enum pipe pipe = crtc->pipe;
3789 
3790 	enabled_bigjoiner_pipes(i915, &master_pipes, &slave_pipes);
3791 
3792 	if (((master_pipes | slave_pipes) & BIT(pipe)) == 0)
3793 		return;
3794 
3795 	crtc_state->bigjoiner_pipes =
3796 		BIT(get_bigjoiner_master_pipe(pipe, master_pipes, slave_pipes)) |
3797 		get_bigjoiner_slave_pipes(pipe, master_pipes, slave_pipes);
3798 }
3799 
3800 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
3801 				struct intel_crtc_state *pipe_config)
3802 {
3803 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3804 	bool active;
3805 	u32 tmp;
3806 
3807 	if (!intel_display_power_get_in_set_if_enabled(dev_priv, &crtc->hw_readout_power_domains,
3808 						       POWER_DOMAIN_PIPE(crtc->pipe)))
3809 		return false;
3810 
3811 	pipe_config->shared_dpll = NULL;
3812 
3813 	active = hsw_get_transcoder_state(crtc, pipe_config, &crtc->hw_readout_power_domains);
3814 
3815 	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
3816 	    bxt_get_dsi_transcoder_state(crtc, pipe_config, &crtc->hw_readout_power_domains)) {
3817 		drm_WARN_ON(&dev_priv->drm, active);
3818 		active = true;
3819 	}
3820 
3821 	if (!active)
3822 		goto out;
3823 
3824 	intel_bigjoiner_get_config(pipe_config);
3825 	intel_dsc_get_config(pipe_config);
3826 
3827 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
3828 	    DISPLAY_VER(dev_priv) >= 11)
3829 		intel_get_transcoder_timings(crtc, pipe_config);
3830 
3831 	if (HAS_VRR(dev_priv) && !transcoder_is_dsi(pipe_config->cpu_transcoder))
3832 		intel_vrr_get_config(pipe_config);
3833 
3834 	intel_get_pipe_src_size(crtc, pipe_config);
3835 
3836 	if (IS_HASWELL(dev_priv)) {
3837 		u32 tmp = intel_de_read(dev_priv,
3838 					TRANSCONF(pipe_config->cpu_transcoder));
3839 
3840 		if (tmp & TRANSCONF_OUTPUT_COLORSPACE_YUV_HSW)
3841 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
3842 		else
3843 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
3844 	} else {
3845 		pipe_config->output_format =
3846 			bdw_get_pipe_misc_output_format(crtc);
3847 	}
3848 
3849 	pipe_config->sink_format = pipe_config->output_format;
3850 
3851 	intel_color_get_config(pipe_config);
3852 
3853 	tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
3854 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
3855 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
3856 		pipe_config->ips_linetime =
3857 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
3858 
3859 	if (intel_display_power_get_in_set_if_enabled(dev_priv, &crtc->hw_readout_power_domains,
3860 						      POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe))) {
3861 		if (DISPLAY_VER(dev_priv) >= 9)
3862 			skl_scaler_get_config(pipe_config);
3863 		else
3864 			ilk_get_pfit_config(pipe_config);
3865 	}
3866 
3867 	hsw_ips_get_config(pipe_config);
3868 
3869 	if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
3870 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
3871 		pipe_config->pixel_multiplier =
3872 			intel_de_read(dev_priv,
3873 				      TRANS_MULT(pipe_config->cpu_transcoder)) + 1;
3874 	} else {
3875 		pipe_config->pixel_multiplier = 1;
3876 	}
3877 
3878 	if (!transcoder_is_dsi(pipe_config->cpu_transcoder)) {
3879 		tmp = intel_de_read(dev_priv, hsw_chicken_trans_reg(dev_priv, pipe_config->cpu_transcoder));
3880 
3881 		pipe_config->framestart_delay = REG_FIELD_GET(HSW_FRAME_START_DELAY_MASK, tmp) + 1;
3882 	} else {
3883 		/* no idea if this is correct */
3884 		pipe_config->framestart_delay = 1;
3885 	}
3886 
3887 out:
3888 	intel_display_power_put_all_in_set(dev_priv, &crtc->hw_readout_power_domains);
3889 
3890 	return active;
3891 }
3892 
3893 bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
3894 {
3895 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3896 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
3897 
3898 	if (!i915->display.funcs.display->get_pipe_config(crtc, crtc_state))
3899 		return false;
3900 
3901 	crtc_state->hw.active = true;
3902 
3903 	intel_crtc_readout_derived_state(crtc_state);
3904 
3905 	return true;
3906 }
3907 
3908 int intel_dotclock_calculate(int link_freq,
3909 			     const struct intel_link_m_n *m_n)
3910 {
3911 	/*
3912 	 * The calculation for the data clock -> pixel clock is:
3913 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
3914 	 * But we want to avoid losing precison if possible, so:
3915 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
3916 	 *
3917 	 * and for link freq (10kbs units) -> pixel clock it is:
3918 	 * link_symbol_clock = link_freq * 10 / link_symbol_size
3919 	 * pixel_clock = (m * link_symbol_clock) / n
3920 	 *    or for more precision:
3921 	 * pixel_clock = (m * link_freq * 10) / (n * link_symbol_size)
3922 	 */
3923 
3924 	if (!m_n->link_n)
3925 		return 0;
3926 
3927 	return DIV_ROUND_UP_ULL(mul_u32_u32(m_n->link_m, link_freq * 10),
3928 				m_n->link_n * intel_dp_link_symbol_size(link_freq));
3929 }
3930 
3931 int intel_crtc_dotclock(const struct intel_crtc_state *pipe_config)
3932 {
3933 	int dotclock;
3934 
3935 	if (intel_crtc_has_dp_encoder(pipe_config))
3936 		dotclock = intel_dotclock_calculate(pipe_config->port_clock,
3937 						    &pipe_config->dp_m_n);
3938 	else if (pipe_config->has_hdmi_sink && pipe_config->pipe_bpp > 24)
3939 		dotclock = DIV_ROUND_CLOSEST(pipe_config->port_clock * 24,
3940 					     pipe_config->pipe_bpp);
3941 	else
3942 		dotclock = pipe_config->port_clock;
3943 
3944 	if (pipe_config->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 &&
3945 	    !intel_crtc_has_dp_encoder(pipe_config))
3946 		dotclock *= 2;
3947 
3948 	if (pipe_config->pixel_multiplier)
3949 		dotclock /= pipe_config->pixel_multiplier;
3950 
3951 	return dotclock;
3952 }
3953 
3954 /* Returns the currently programmed mode of the given encoder. */
3955 struct drm_display_mode *
3956 intel_encoder_current_mode(struct intel_encoder *encoder)
3957 {
3958 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
3959 	struct intel_crtc_state *crtc_state;
3960 	struct drm_display_mode *mode;
3961 	struct intel_crtc *crtc;
3962 	enum pipe pipe;
3963 
3964 	if (!encoder->get_hw_state(encoder, &pipe))
3965 		return NULL;
3966 
3967 	crtc = intel_crtc_for_pipe(dev_priv, pipe);
3968 
3969 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
3970 	if (!mode)
3971 		return NULL;
3972 
3973 	crtc_state = intel_crtc_state_alloc(crtc);
3974 	if (!crtc_state) {
3975 		kfree(mode);
3976 		return NULL;
3977 	}
3978 
3979 	if (!intel_crtc_get_pipe_config(crtc_state)) {
3980 		intel_crtc_destroy_state(&crtc->base, &crtc_state->uapi);
3981 		kfree(mode);
3982 		return NULL;
3983 	}
3984 
3985 	intel_encoder_get_config(encoder, crtc_state);
3986 
3987 	intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
3988 
3989 	intel_crtc_destroy_state(&crtc->base, &crtc_state->uapi);
3990 
3991 	return mode;
3992 }
3993 
3994 static bool encoders_cloneable(const struct intel_encoder *a,
3995 			       const struct intel_encoder *b)
3996 {
3997 	/* masks could be asymmetric, so check both ways */
3998 	return a == b || (a->cloneable & BIT(b->type) &&
3999 			  b->cloneable & BIT(a->type));
4000 }
4001 
4002 static bool check_single_encoder_cloning(struct intel_atomic_state *state,
4003 					 struct intel_crtc *crtc,
4004 					 struct intel_encoder *encoder)
4005 {
4006 	struct intel_encoder *source_encoder;
4007 	struct drm_connector *connector;
4008 	struct drm_connector_state *connector_state;
4009 	int i;
4010 
4011 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4012 		if (connector_state->crtc != &crtc->base)
4013 			continue;
4014 
4015 		source_encoder =
4016 			to_intel_encoder(connector_state->best_encoder);
4017 		if (!encoders_cloneable(encoder, source_encoder))
4018 			return false;
4019 	}
4020 
4021 	return true;
4022 }
4023 
4024 static int icl_add_linked_planes(struct intel_atomic_state *state)
4025 {
4026 	struct intel_plane *plane, *linked;
4027 	struct intel_plane_state *plane_state, *linked_plane_state;
4028 	int i;
4029 
4030 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4031 		linked = plane_state->planar_linked_plane;
4032 
4033 		if (!linked)
4034 			continue;
4035 
4036 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
4037 		if (IS_ERR(linked_plane_state))
4038 			return PTR_ERR(linked_plane_state);
4039 
4040 		drm_WARN_ON(state->base.dev,
4041 			    linked_plane_state->planar_linked_plane != plane);
4042 		drm_WARN_ON(state->base.dev,
4043 			    linked_plane_state->planar_slave == plane_state->planar_slave);
4044 	}
4045 
4046 	return 0;
4047 }
4048 
4049 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
4050 {
4051 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4052 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4053 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
4054 	struct intel_plane *plane, *linked;
4055 	struct intel_plane_state *plane_state;
4056 	int i;
4057 
4058 	if (DISPLAY_VER(dev_priv) < 11)
4059 		return 0;
4060 
4061 	/*
4062 	 * Destroy all old plane links and make the slave plane invisible
4063 	 * in the crtc_state->active_planes mask.
4064 	 */
4065 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4066 		if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
4067 			continue;
4068 
4069 		plane_state->planar_linked_plane = NULL;
4070 		if (plane_state->planar_slave && !plane_state->uapi.visible) {
4071 			crtc_state->enabled_planes &= ~BIT(plane->id);
4072 			crtc_state->active_planes &= ~BIT(plane->id);
4073 			crtc_state->update_planes |= BIT(plane->id);
4074 			crtc_state->data_rate[plane->id] = 0;
4075 			crtc_state->rel_data_rate[plane->id] = 0;
4076 		}
4077 
4078 		plane_state->planar_slave = false;
4079 	}
4080 
4081 	if (!crtc_state->nv12_planes)
4082 		return 0;
4083 
4084 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
4085 		struct intel_plane_state *linked_state = NULL;
4086 
4087 		if (plane->pipe != crtc->pipe ||
4088 		    !(crtc_state->nv12_planes & BIT(plane->id)))
4089 			continue;
4090 
4091 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
4092 			if (!icl_is_nv12_y_plane(dev_priv, linked->id))
4093 				continue;
4094 
4095 			if (crtc_state->active_planes & BIT(linked->id))
4096 				continue;
4097 
4098 			linked_state = intel_atomic_get_plane_state(state, linked);
4099 			if (IS_ERR(linked_state))
4100 				return PTR_ERR(linked_state);
4101 
4102 			break;
4103 		}
4104 
4105 		if (!linked_state) {
4106 			drm_dbg_kms(&dev_priv->drm,
4107 				    "Need %d free Y planes for planar YUV\n",
4108 				    hweight8(crtc_state->nv12_planes));
4109 
4110 			return -EINVAL;
4111 		}
4112 
4113 		plane_state->planar_linked_plane = linked;
4114 
4115 		linked_state->planar_slave = true;
4116 		linked_state->planar_linked_plane = plane;
4117 		crtc_state->enabled_planes |= BIT(linked->id);
4118 		crtc_state->active_planes |= BIT(linked->id);
4119 		crtc_state->update_planes |= BIT(linked->id);
4120 		crtc_state->data_rate[linked->id] =
4121 			crtc_state->data_rate_y[plane->id];
4122 		crtc_state->rel_data_rate[linked->id] =
4123 			crtc_state->rel_data_rate_y[plane->id];
4124 		drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
4125 			    linked->base.name, plane->base.name);
4126 
4127 		/* Copy parameters to slave plane */
4128 		linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
4129 		linked_state->color_ctl = plane_state->color_ctl;
4130 		linked_state->view = plane_state->view;
4131 		linked_state->decrypt = plane_state->decrypt;
4132 
4133 		intel_plane_copy_hw_state(linked_state, plane_state);
4134 		linked_state->uapi.src = plane_state->uapi.src;
4135 		linked_state->uapi.dst = plane_state->uapi.dst;
4136 
4137 		if (icl_is_hdr_plane(dev_priv, plane->id)) {
4138 			if (linked->id == PLANE_SPRITE5)
4139 				plane_state->cus_ctl |= PLANE_CUS_Y_PLANE_7_ICL;
4140 			else if (linked->id == PLANE_SPRITE4)
4141 				plane_state->cus_ctl |= PLANE_CUS_Y_PLANE_6_ICL;
4142 			else if (linked->id == PLANE_SPRITE3)
4143 				plane_state->cus_ctl |= PLANE_CUS_Y_PLANE_5_RKL;
4144 			else if (linked->id == PLANE_SPRITE2)
4145 				plane_state->cus_ctl |= PLANE_CUS_Y_PLANE_4_RKL;
4146 			else
4147 				MISSING_CASE(linked->id);
4148 		}
4149 	}
4150 
4151 	return 0;
4152 }
4153 
4154 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
4155 {
4156 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
4157 	struct intel_atomic_state *state =
4158 		to_intel_atomic_state(new_crtc_state->uapi.state);
4159 	const struct intel_crtc_state *old_crtc_state =
4160 		intel_atomic_get_old_crtc_state(state, crtc);
4161 
4162 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
4163 }
4164 
4165 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
4166 {
4167 	const struct drm_display_mode *pipe_mode =
4168 		&crtc_state->hw.pipe_mode;
4169 	int linetime_wm;
4170 
4171 	if (!crtc_state->hw.enable)
4172 		return 0;
4173 
4174 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
4175 					pipe_mode->crtc_clock);
4176 
4177 	return min(linetime_wm, 0x1ff);
4178 }
4179 
4180 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
4181 			       const struct intel_cdclk_state *cdclk_state)
4182 {
4183 	const struct drm_display_mode *pipe_mode =
4184 		&crtc_state->hw.pipe_mode;
4185 	int linetime_wm;
4186 
4187 	if (!crtc_state->hw.enable)
4188 		return 0;
4189 
4190 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
4191 					cdclk_state->logical.cdclk);
4192 
4193 	return min(linetime_wm, 0x1ff);
4194 }
4195 
4196 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
4197 {
4198 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4199 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4200 	const struct drm_display_mode *pipe_mode =
4201 		&crtc_state->hw.pipe_mode;
4202 	int linetime_wm;
4203 
4204 	if (!crtc_state->hw.enable)
4205 		return 0;
4206 
4207 	linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
4208 				   crtc_state->pixel_rate);
4209 
4210 	/* Display WA #1135: BXT:ALL GLK:ALL */
4211 	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
4212 	    skl_watermark_ipc_enabled(dev_priv))
4213 		linetime_wm /= 2;
4214 
4215 	return min(linetime_wm, 0x1ff);
4216 }
4217 
4218 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
4219 				   struct intel_crtc *crtc)
4220 {
4221 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4222 	struct intel_crtc_state *crtc_state =
4223 		intel_atomic_get_new_crtc_state(state, crtc);
4224 	const struct intel_cdclk_state *cdclk_state;
4225 
4226 	if (DISPLAY_VER(dev_priv) >= 9)
4227 		crtc_state->linetime = skl_linetime_wm(crtc_state);
4228 	else
4229 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
4230 
4231 	if (!hsw_crtc_supports_ips(crtc))
4232 		return 0;
4233 
4234 	cdclk_state = intel_atomic_get_cdclk_state(state);
4235 	if (IS_ERR(cdclk_state))
4236 		return PTR_ERR(cdclk_state);
4237 
4238 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
4239 						       cdclk_state);
4240 
4241 	return 0;
4242 }
4243 
4244 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
4245 				   struct intel_crtc *crtc)
4246 {
4247 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4248 	struct intel_crtc_state *crtc_state =
4249 		intel_atomic_get_new_crtc_state(state, crtc);
4250 	int ret;
4251 
4252 	if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv) &&
4253 	    intel_crtc_needs_modeset(crtc_state) &&
4254 	    !crtc_state->hw.active)
4255 		crtc_state->update_wm_post = true;
4256 
4257 	if (intel_crtc_needs_modeset(crtc_state)) {
4258 		ret = intel_dpll_crtc_get_shared_dpll(state, crtc);
4259 		if (ret)
4260 			return ret;
4261 	}
4262 
4263 	/*
4264 	 * May need to update pipe gamma enable bits
4265 	 * when C8 planes are getting enabled/disabled.
4266 	 */
4267 	if (c8_planes_changed(crtc_state))
4268 		crtc_state->uapi.color_mgmt_changed = true;
4269 
4270 	if (intel_crtc_needs_color_update(crtc_state)) {
4271 		ret = intel_color_check(crtc_state);
4272 		if (ret)
4273 			return ret;
4274 	}
4275 
4276 	ret = intel_compute_pipe_wm(state, crtc);
4277 	if (ret) {
4278 		drm_dbg_kms(&dev_priv->drm,
4279 			    "Target pipe watermarks are invalid\n");
4280 		return ret;
4281 	}
4282 
4283 	/*
4284 	 * Calculate 'intermediate' watermarks that satisfy both the
4285 	 * old state and the new state.  We can program these
4286 	 * immediately.
4287 	 */
4288 	ret = intel_compute_intermediate_wm(state, crtc);
4289 	if (ret) {
4290 		drm_dbg_kms(&dev_priv->drm,
4291 			    "No valid intermediate pipe watermarks are possible\n");
4292 		return ret;
4293 	}
4294 
4295 	if (DISPLAY_VER(dev_priv) >= 9) {
4296 		if (intel_crtc_needs_modeset(crtc_state) ||
4297 		    intel_crtc_needs_fastset(crtc_state)) {
4298 			ret = skl_update_scaler_crtc(crtc_state);
4299 			if (ret)
4300 				return ret;
4301 		}
4302 
4303 		ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
4304 		if (ret)
4305 			return ret;
4306 	}
4307 
4308 	if (HAS_IPS(dev_priv)) {
4309 		ret = hsw_ips_compute_config(state, crtc);
4310 		if (ret)
4311 			return ret;
4312 	}
4313 
4314 	if (DISPLAY_VER(dev_priv) >= 9 ||
4315 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
4316 		ret = hsw_compute_linetime_wm(state, crtc);
4317 		if (ret)
4318 			return ret;
4319 
4320 	}
4321 
4322 	ret = intel_psr2_sel_fetch_update(state, crtc);
4323 	if (ret)
4324 		return ret;
4325 
4326 	return 0;
4327 }
4328 
4329 static int
4330 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
4331 		      struct intel_crtc_state *crtc_state)
4332 {
4333 	struct drm_connector *connector = conn_state->connector;
4334 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
4335 	const struct drm_display_info *info = &connector->display_info;
4336 	int bpp;
4337 
4338 	switch (conn_state->max_bpc) {
4339 	case 6 ... 7:
4340 		bpp = 6 * 3;
4341 		break;
4342 	case 8 ... 9:
4343 		bpp = 8 * 3;
4344 		break;
4345 	case 10 ... 11:
4346 		bpp = 10 * 3;
4347 		break;
4348 	case 12 ... 16:
4349 		bpp = 12 * 3;
4350 		break;
4351 	default:
4352 		MISSING_CASE(conn_state->max_bpc);
4353 		return -EINVAL;
4354 	}
4355 
4356 	if (bpp < crtc_state->pipe_bpp) {
4357 		drm_dbg_kms(&i915->drm,
4358 			    "[CONNECTOR:%d:%s] Limiting display bpp to %d "
4359 			    "(EDID bpp %d, max requested bpp %d, max platform bpp %d)\n",
4360 			    connector->base.id, connector->name,
4361 			    bpp, 3 * info->bpc,
4362 			    3 * conn_state->max_requested_bpc,
4363 			    crtc_state->pipe_bpp);
4364 
4365 		crtc_state->pipe_bpp = bpp;
4366 	}
4367 
4368 	return 0;
4369 }
4370 
4371 static int
4372 compute_baseline_pipe_bpp(struct intel_atomic_state *state,
4373 			  struct intel_crtc *crtc)
4374 {
4375 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4376 	struct intel_crtc_state *crtc_state =
4377 		intel_atomic_get_new_crtc_state(state, crtc);
4378 	struct drm_connector *connector;
4379 	struct drm_connector_state *connector_state;
4380 	int bpp, i;
4381 
4382 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
4383 	    IS_CHERRYVIEW(dev_priv)))
4384 		bpp = 10*3;
4385 	else if (DISPLAY_VER(dev_priv) >= 5)
4386 		bpp = 12*3;
4387 	else
4388 		bpp = 8*3;
4389 
4390 	crtc_state->pipe_bpp = bpp;
4391 
4392 	/* Clamp display bpp to connector max bpp */
4393 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4394 		int ret;
4395 
4396 		if (connector_state->crtc != &crtc->base)
4397 			continue;
4398 
4399 		ret = compute_sink_pipe_bpp(connector_state, crtc_state);
4400 		if (ret)
4401 			return ret;
4402 	}
4403 
4404 	return 0;
4405 }
4406 
4407 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
4408 {
4409 	struct drm_device *dev = state->base.dev;
4410 	struct drm_connector *connector;
4411 	struct drm_connector_list_iter conn_iter;
4412 	unsigned int used_ports = 0;
4413 	unsigned int used_mst_ports = 0;
4414 	bool ret = true;
4415 
4416 	/*
4417 	 * We're going to peek into connector->state,
4418 	 * hence connection_mutex must be held.
4419 	 */
4420 	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
4421 
4422 	/*
4423 	 * Walk the connector list instead of the encoder
4424 	 * list to detect the problem on ddi platforms
4425 	 * where there's just one encoder per digital port.
4426 	 */
4427 	drm_connector_list_iter_begin(dev, &conn_iter);
4428 	drm_for_each_connector_iter(connector, &conn_iter) {
4429 		struct drm_connector_state *connector_state;
4430 		struct intel_encoder *encoder;
4431 
4432 		connector_state =
4433 			drm_atomic_get_new_connector_state(&state->base,
4434 							   connector);
4435 		if (!connector_state)
4436 			connector_state = connector->state;
4437 
4438 		if (!connector_state->best_encoder)
4439 			continue;
4440 
4441 		encoder = to_intel_encoder(connector_state->best_encoder);
4442 
4443 		drm_WARN_ON(dev, !connector_state->crtc);
4444 
4445 		switch (encoder->type) {
4446 		case INTEL_OUTPUT_DDI:
4447 			if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
4448 				break;
4449 			fallthrough;
4450 		case INTEL_OUTPUT_DP:
4451 		case INTEL_OUTPUT_HDMI:
4452 		case INTEL_OUTPUT_EDP:
4453 			/* the same port mustn't appear more than once */
4454 			if (used_ports & BIT(encoder->port))
4455 				ret = false;
4456 
4457 			used_ports |= BIT(encoder->port);
4458 			break;
4459 		case INTEL_OUTPUT_DP_MST:
4460 			used_mst_ports |=
4461 				1 << encoder->port;
4462 			break;
4463 		default:
4464 			break;
4465 		}
4466 	}
4467 	drm_connector_list_iter_end(&conn_iter);
4468 
4469 	/* can't mix MST and SST/HDMI on the same port */
4470 	if (used_ports & used_mst_ports)
4471 		return false;
4472 
4473 	return ret;
4474 }
4475 
4476 static void
4477 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
4478 					   struct intel_crtc *crtc)
4479 {
4480 	struct intel_crtc_state *crtc_state =
4481 		intel_atomic_get_new_crtc_state(state, crtc);
4482 
4483 	WARN_ON(intel_crtc_is_bigjoiner_slave(crtc_state));
4484 
4485 	drm_property_replace_blob(&crtc_state->hw.degamma_lut,
4486 				  crtc_state->uapi.degamma_lut);
4487 	drm_property_replace_blob(&crtc_state->hw.gamma_lut,
4488 				  crtc_state->uapi.gamma_lut);
4489 	drm_property_replace_blob(&crtc_state->hw.ctm,
4490 				  crtc_state->uapi.ctm);
4491 }
4492 
4493 static void
4494 intel_crtc_copy_uapi_to_hw_state_modeset(struct intel_atomic_state *state,
4495 					 struct intel_crtc *crtc)
4496 {
4497 	struct intel_crtc_state *crtc_state =
4498 		intel_atomic_get_new_crtc_state(state, crtc);
4499 
4500 	WARN_ON(intel_crtc_is_bigjoiner_slave(crtc_state));
4501 
4502 	crtc_state->hw.enable = crtc_state->uapi.enable;
4503 	crtc_state->hw.active = crtc_state->uapi.active;
4504 	drm_mode_copy(&crtc_state->hw.mode,
4505 		      &crtc_state->uapi.mode);
4506 	drm_mode_copy(&crtc_state->hw.adjusted_mode,
4507 		      &crtc_state->uapi.adjusted_mode);
4508 	crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
4509 
4510 	intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc);
4511 }
4512 
4513 static void
4514 copy_bigjoiner_crtc_state_nomodeset(struct intel_atomic_state *state,
4515 				    struct intel_crtc *slave_crtc)
4516 {
4517 	struct intel_crtc_state *slave_crtc_state =
4518 		intel_atomic_get_new_crtc_state(state, slave_crtc);
4519 	struct intel_crtc *master_crtc = intel_master_crtc(slave_crtc_state);
4520 	const struct intel_crtc_state *master_crtc_state =
4521 		intel_atomic_get_new_crtc_state(state, master_crtc);
4522 
4523 	drm_property_replace_blob(&slave_crtc_state->hw.degamma_lut,
4524 				  master_crtc_state->hw.degamma_lut);
4525 	drm_property_replace_blob(&slave_crtc_state->hw.gamma_lut,
4526 				  master_crtc_state->hw.gamma_lut);
4527 	drm_property_replace_blob(&slave_crtc_state->hw.ctm,
4528 				  master_crtc_state->hw.ctm);
4529 
4530 	slave_crtc_state->uapi.color_mgmt_changed = master_crtc_state->uapi.color_mgmt_changed;
4531 }
4532 
4533 static int
4534 copy_bigjoiner_crtc_state_modeset(struct intel_atomic_state *state,
4535 				  struct intel_crtc *slave_crtc)
4536 {
4537 	struct intel_crtc_state *slave_crtc_state =
4538 		intel_atomic_get_new_crtc_state(state, slave_crtc);
4539 	struct intel_crtc *master_crtc = intel_master_crtc(slave_crtc_state);
4540 	const struct intel_crtc_state *master_crtc_state =
4541 		intel_atomic_get_new_crtc_state(state, master_crtc);
4542 	struct intel_crtc_state *saved_state;
4543 
4544 	WARN_ON(master_crtc_state->bigjoiner_pipes !=
4545 		slave_crtc_state->bigjoiner_pipes);
4546 
4547 	saved_state = kmemdup(master_crtc_state, sizeof(*saved_state), GFP_KERNEL);
4548 	if (!saved_state)
4549 		return -ENOMEM;
4550 
4551 	/* preserve some things from the slave's original crtc state */
4552 	saved_state->uapi = slave_crtc_state->uapi;
4553 	saved_state->scaler_state = slave_crtc_state->scaler_state;
4554 	saved_state->shared_dpll = slave_crtc_state->shared_dpll;
4555 	saved_state->crc_enabled = slave_crtc_state->crc_enabled;
4556 
4557 	intel_crtc_free_hw_state(slave_crtc_state);
4558 	if (slave_crtc_state->dp_tunnel_ref.tunnel)
4559 		drm_dp_tunnel_ref_put(&slave_crtc_state->dp_tunnel_ref);
4560 	memcpy(slave_crtc_state, saved_state, sizeof(*slave_crtc_state));
4561 	kfree(saved_state);
4562 
4563 	/* Re-init hw state */
4564 	memset(&slave_crtc_state->hw, 0, sizeof(slave_crtc_state->hw));
4565 	slave_crtc_state->hw.enable = master_crtc_state->hw.enable;
4566 	slave_crtc_state->hw.active = master_crtc_state->hw.active;
4567 	drm_mode_copy(&slave_crtc_state->hw.mode,
4568 		      &master_crtc_state->hw.mode);
4569 	drm_mode_copy(&slave_crtc_state->hw.pipe_mode,
4570 		      &master_crtc_state->hw.pipe_mode);
4571 	drm_mode_copy(&slave_crtc_state->hw.adjusted_mode,
4572 		      &master_crtc_state->hw.adjusted_mode);
4573 	slave_crtc_state->hw.scaling_filter = master_crtc_state->hw.scaling_filter;
4574 
4575 	if (master_crtc_state->dp_tunnel_ref.tunnel)
4576 		drm_dp_tunnel_ref_get(master_crtc_state->dp_tunnel_ref.tunnel,
4577 				      &slave_crtc_state->dp_tunnel_ref);
4578 
4579 	copy_bigjoiner_crtc_state_nomodeset(state, slave_crtc);
4580 
4581 	slave_crtc_state->uapi.mode_changed = master_crtc_state->uapi.mode_changed;
4582 	slave_crtc_state->uapi.connectors_changed = master_crtc_state->uapi.connectors_changed;
4583 	slave_crtc_state->uapi.active_changed = master_crtc_state->uapi.active_changed;
4584 
4585 	WARN_ON(master_crtc_state->bigjoiner_pipes !=
4586 		slave_crtc_state->bigjoiner_pipes);
4587 
4588 	return 0;
4589 }
4590 
4591 static int
4592 intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
4593 				 struct intel_crtc *crtc)
4594 {
4595 	struct intel_crtc_state *crtc_state =
4596 		intel_atomic_get_new_crtc_state(state, crtc);
4597 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4598 	struct intel_crtc_state *saved_state;
4599 
4600 	saved_state = intel_crtc_state_alloc(crtc);
4601 	if (!saved_state)
4602 		return -ENOMEM;
4603 
4604 	/* free the old crtc_state->hw members */
4605 	intel_crtc_free_hw_state(crtc_state);
4606 
4607 	intel_dp_tunnel_atomic_clear_stream_bw(state, crtc_state);
4608 
4609 	/* FIXME: before the switch to atomic started, a new pipe_config was
4610 	 * kzalloc'd. Code that depends on any field being zero should be
4611 	 * fixed, so that the crtc_state can be safely duplicated. For now,
4612 	 * only fields that are know to not cause problems are preserved. */
4613 
4614 	saved_state->uapi = crtc_state->uapi;
4615 	saved_state->inherited = crtc_state->inherited;
4616 	saved_state->scaler_state = crtc_state->scaler_state;
4617 	saved_state->shared_dpll = crtc_state->shared_dpll;
4618 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
4619 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
4620 	       sizeof(saved_state->icl_port_dplls));
4621 	saved_state->crc_enabled = crtc_state->crc_enabled;
4622 	if (IS_G4X(dev_priv) ||
4623 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
4624 		saved_state->wm = crtc_state->wm;
4625 
4626 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
4627 	kfree(saved_state);
4628 
4629 	intel_crtc_copy_uapi_to_hw_state_modeset(state, crtc);
4630 
4631 	return 0;
4632 }
4633 
4634 static int
4635 intel_modeset_pipe_config(struct intel_atomic_state *state,
4636 			  struct intel_crtc *crtc,
4637 			  const struct intel_link_bw_limits *limits)
4638 {
4639 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
4640 	struct intel_crtc_state *crtc_state =
4641 		intel_atomic_get_new_crtc_state(state, crtc);
4642 	struct drm_connector *connector;
4643 	struct drm_connector_state *connector_state;
4644 	int pipe_src_w, pipe_src_h;
4645 	int base_bpp, ret, i;
4646 
4647 	crtc_state->cpu_transcoder = (enum transcoder) crtc->pipe;
4648 
4649 	crtc_state->framestart_delay = 1;
4650 
4651 	/*
4652 	 * Sanitize sync polarity flags based on requested ones. If neither
4653 	 * positive or negative polarity is requested, treat this as meaning
4654 	 * negative polarity.
4655 	 */
4656 	if (!(crtc_state->hw.adjusted_mode.flags &
4657 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
4658 		crtc_state->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
4659 
4660 	if (!(crtc_state->hw.adjusted_mode.flags &
4661 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
4662 		crtc_state->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
4663 
4664 	ret = compute_baseline_pipe_bpp(state, crtc);
4665 	if (ret)
4666 		return ret;
4667 
4668 	crtc_state->fec_enable = limits->force_fec_pipes & BIT(crtc->pipe);
4669 	crtc_state->max_link_bpp_x16 = limits->max_bpp_x16[crtc->pipe];
4670 
4671 	if (crtc_state->pipe_bpp > to_bpp_int(crtc_state->max_link_bpp_x16)) {
4672 		drm_dbg_kms(&i915->drm,
4673 			    "[CRTC:%d:%s] Link bpp limited to " BPP_X16_FMT "\n",
4674 			    crtc->base.base.id, crtc->base.name,
4675 			    BPP_X16_ARGS(crtc_state->max_link_bpp_x16));
4676 		crtc_state->bw_constrained = true;
4677 	}
4678 
4679 	base_bpp = crtc_state->pipe_bpp;
4680 
4681 	/*
4682 	 * Determine the real pipe dimensions. Note that stereo modes can
4683 	 * increase the actual pipe size due to the frame doubling and
4684 	 * insertion of additional space for blanks between the frame. This
4685 	 * is stored in the crtc timings. We use the requested mode to do this
4686 	 * computation to clearly distinguish it from the adjusted mode, which
4687 	 * can be changed by the connectors in the below retry loop.
4688 	 */
4689 	drm_mode_get_hv_timing(&crtc_state->hw.mode,
4690 			       &pipe_src_w, &pipe_src_h);
4691 	drm_rect_init(&crtc_state->pipe_src, 0, 0,
4692 		      pipe_src_w, pipe_src_h);
4693 
4694 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4695 		struct intel_encoder *encoder =
4696 			to_intel_encoder(connector_state->best_encoder);
4697 
4698 		if (connector_state->crtc != &crtc->base)
4699 			continue;
4700 
4701 		if (!check_single_encoder_cloning(state, crtc, encoder)) {
4702 			drm_dbg_kms(&i915->drm,
4703 				    "[ENCODER:%d:%s] rejecting invalid cloning configuration\n",
4704 				    encoder->base.base.id, encoder->base.name);
4705 			return -EINVAL;
4706 		}
4707 
4708 		/*
4709 		 * Determine output_types before calling the .compute_config()
4710 		 * hooks so that the hooks can use this information safely.
4711 		 */
4712 		if (encoder->compute_output_type)
4713 			crtc_state->output_types |=
4714 				BIT(encoder->compute_output_type(encoder, crtc_state,
4715 								 connector_state));
4716 		else
4717 			crtc_state->output_types |= BIT(encoder->type);
4718 	}
4719 
4720 	/* Ensure the port clock defaults are reset when retrying. */
4721 	crtc_state->port_clock = 0;
4722 	crtc_state->pixel_multiplier = 1;
4723 
4724 	/* Fill in default crtc timings, allow encoders to overwrite them. */
4725 	drm_mode_set_crtcinfo(&crtc_state->hw.adjusted_mode,
4726 			      CRTC_STEREO_DOUBLE);
4727 
4728 	/* Pass our mode to the connectors and the CRTC to give them a chance to
4729 	 * adjust it according to limitations or connector properties, and also
4730 	 * a chance to reject the mode entirely.
4731 	 */
4732 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
4733 		struct intel_encoder *encoder =
4734 			to_intel_encoder(connector_state->best_encoder);
4735 
4736 		if (connector_state->crtc != &crtc->base)
4737 			continue;
4738 
4739 		ret = encoder->compute_config(encoder, crtc_state,
4740 					      connector_state);
4741 		if (ret == -EDEADLK)
4742 			return ret;
4743 		if (ret < 0) {
4744 			drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] config failure: %d\n",
4745 				    encoder->base.base.id, encoder->base.name, ret);
4746 			return ret;
4747 		}
4748 	}
4749 
4750 	/* Set default port clock if not overwritten by the encoder. Needs to be
4751 	 * done afterwards in case the encoder adjusts the mode. */
4752 	if (!crtc_state->port_clock)
4753 		crtc_state->port_clock = crtc_state->hw.adjusted_mode.crtc_clock
4754 			* crtc_state->pixel_multiplier;
4755 
4756 	ret = intel_crtc_compute_config(state, crtc);
4757 	if (ret == -EDEADLK)
4758 		return ret;
4759 	if (ret < 0) {
4760 		drm_dbg_kms(&i915->drm, "[CRTC:%d:%s] config failure: %d\n",
4761 			    crtc->base.base.id, crtc->base.name, ret);
4762 		return ret;
4763 	}
4764 
4765 	/* Dithering seems to not pass-through bits correctly when it should, so
4766 	 * only enable it on 6bpc panels and when its not a compliance
4767 	 * test requesting 6bpc video pattern.
4768 	 */
4769 	crtc_state->dither = (crtc_state->pipe_bpp == 6*3) &&
4770 		!crtc_state->dither_force_disable;
4771 	drm_dbg_kms(&i915->drm,
4772 		    "[CRTC:%d:%s] hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
4773 		    crtc->base.base.id, crtc->base.name,
4774 		    base_bpp, crtc_state->pipe_bpp, crtc_state->dither);
4775 
4776 	return 0;
4777 }
4778 
4779 static int
4780 intel_modeset_pipe_config_late(struct intel_atomic_state *state,
4781 			       struct intel_crtc *crtc)
4782 {
4783 	struct intel_crtc_state *crtc_state =
4784 		intel_atomic_get_new_crtc_state(state, crtc);
4785 	struct drm_connector_state *conn_state;
4786 	struct drm_connector *connector;
4787 	int i;
4788 
4789 	for_each_new_connector_in_state(&state->base, connector,
4790 					conn_state, i) {
4791 		struct intel_encoder *encoder =
4792 			to_intel_encoder(conn_state->best_encoder);
4793 		int ret;
4794 
4795 		if (conn_state->crtc != &crtc->base ||
4796 		    !encoder->compute_config_late)
4797 			continue;
4798 
4799 		ret = encoder->compute_config_late(encoder, crtc_state,
4800 						   conn_state);
4801 		if (ret)
4802 			return ret;
4803 	}
4804 
4805 	return 0;
4806 }
4807 
4808 bool intel_fuzzy_clock_check(int clock1, int clock2)
4809 {
4810 	int diff;
4811 
4812 	if (clock1 == clock2)
4813 		return true;
4814 
4815 	if (!clock1 || !clock2)
4816 		return false;
4817 
4818 	diff = abs(clock1 - clock2);
4819 
4820 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
4821 		return true;
4822 
4823 	return false;
4824 }
4825 
4826 static bool
4827 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
4828 		       const struct intel_link_m_n *m2_n2)
4829 {
4830 	return m_n->tu == m2_n2->tu &&
4831 		m_n->data_m == m2_n2->data_m &&
4832 		m_n->data_n == m2_n2->data_n &&
4833 		m_n->link_m == m2_n2->link_m &&
4834 		m_n->link_n == m2_n2->link_n;
4835 }
4836 
4837 static bool
4838 intel_compare_infoframe(const union hdmi_infoframe *a,
4839 			const union hdmi_infoframe *b)
4840 {
4841 	return memcmp(a, b, sizeof(*a)) == 0;
4842 }
4843 
4844 static bool
4845 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
4846 			 const struct drm_dp_vsc_sdp *b)
4847 {
4848 	return a->pixelformat == b->pixelformat &&
4849 		a->colorimetry == b->colorimetry &&
4850 		a->bpc == b->bpc &&
4851 		a->dynamic_range == b->dynamic_range &&
4852 		a->content_type == b->content_type;
4853 }
4854 
4855 static bool
4856 intel_compare_dp_as_sdp(const struct drm_dp_as_sdp *a,
4857 			const struct drm_dp_as_sdp *b)
4858 {
4859 	return a->vtotal == b->vtotal &&
4860 		a->target_rr == b->target_rr &&
4861 		a->duration_incr_ms == b->duration_incr_ms &&
4862 		a->duration_decr_ms == b->duration_decr_ms &&
4863 		a->mode == b->mode;
4864 }
4865 
4866 static bool
4867 intel_compare_buffer(const u8 *a, const u8 *b, size_t len)
4868 {
4869 	return memcmp(a, b, len) == 0;
4870 }
4871 
4872 static void __printf(5, 6)
4873 pipe_config_mismatch(struct drm_printer *p, bool fastset,
4874 		     const struct intel_crtc *crtc,
4875 		     const char *name, const char *format, ...)
4876 {
4877 	struct va_format vaf;
4878 	va_list args;
4879 
4880 	va_start(args, format);
4881 	vaf.fmt = format;
4882 	vaf.va = &args;
4883 
4884 	if (fastset)
4885 		drm_printf(p, "[CRTC:%d:%s] fastset requirement not met in %s %pV\n",
4886 			   crtc->base.base.id, crtc->base.name, name, &vaf);
4887 	else
4888 		drm_printf(p, "[CRTC:%d:%s] mismatch in %s %pV\n",
4889 			   crtc->base.base.id, crtc->base.name, name, &vaf);
4890 
4891 	va_end(args);
4892 }
4893 
4894 static void
4895 pipe_config_infoframe_mismatch(struct drm_printer *p, bool fastset,
4896 			       const struct intel_crtc *crtc,
4897 			       const char *name,
4898 			       const union hdmi_infoframe *a,
4899 			       const union hdmi_infoframe *b)
4900 {
4901 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
4902 	const char *loglevel;
4903 
4904 	if (fastset) {
4905 		if (!drm_debug_enabled(DRM_UT_KMS))
4906 			return;
4907 
4908 		loglevel = KERN_DEBUG;
4909 	} else {
4910 		loglevel = KERN_ERR;
4911 	}
4912 
4913 	pipe_config_mismatch(p, fastset, crtc, name, "infoframe");
4914 
4915 	drm_printf(p, "expected:\n");
4916 	hdmi_infoframe_log(loglevel, i915->drm.dev, a);
4917 	drm_printf(p, "found:\n");
4918 	hdmi_infoframe_log(loglevel, i915->drm.dev, b);
4919 }
4920 
4921 static void
4922 pipe_config_dp_vsc_sdp_mismatch(struct drm_printer *p, bool fastset,
4923 				const struct intel_crtc *crtc,
4924 				const char *name,
4925 				const struct drm_dp_vsc_sdp *a,
4926 				const struct drm_dp_vsc_sdp *b)
4927 {
4928 	pipe_config_mismatch(p, fastset, crtc, name, "dp sdp");
4929 
4930 	drm_printf(p, "expected:\n");
4931 	drm_dp_vsc_sdp_log(p, a);
4932 	drm_printf(p, "found:\n");
4933 	drm_dp_vsc_sdp_log(p, b);
4934 }
4935 
4936 static void
4937 pipe_config_dp_as_sdp_mismatch(struct drm_i915_private *i915,
4938 			       bool fastset, const char *name,
4939 			       const struct drm_dp_as_sdp *a,
4940 			       const struct drm_dp_as_sdp *b)
4941 {
4942 	struct drm_printer p;
4943 
4944 	if (fastset) {
4945 		p = drm_dbg_printer(&i915->drm, DRM_UT_KMS, NULL);
4946 
4947 		drm_printf(&p, "fastset requirement not met in %s dp sdp\n", name);
4948 	} else {
4949 		p = drm_err_printer(&i915->drm, NULL);
4950 
4951 		drm_printf(&p, "mismatch in %s dp sdp\n", name);
4952 	}
4953 
4954 	drm_printf(&p, "expected:\n");
4955 	drm_dp_as_sdp_log(&p, a);
4956 	drm_printf(&p, "found:\n");
4957 	drm_dp_as_sdp_log(&p, b);
4958 }
4959 
4960 /* Returns the length up to and including the last differing byte */
4961 static size_t
4962 memcmp_diff_len(const u8 *a, const u8 *b, size_t len)
4963 {
4964 	int i;
4965 
4966 	for (i = len - 1; i >= 0; i--) {
4967 		if (a[i] != b[i])
4968 			return i + 1;
4969 	}
4970 
4971 	return 0;
4972 }
4973 
4974 static void
4975 pipe_config_buffer_mismatch(struct drm_printer *p, bool fastset,
4976 			    const struct intel_crtc *crtc,
4977 			    const char *name,
4978 			    const u8 *a, const u8 *b, size_t len)
4979 {
4980 	const char *loglevel;
4981 
4982 	if (fastset) {
4983 		if (!drm_debug_enabled(DRM_UT_KMS))
4984 			return;
4985 
4986 		loglevel = KERN_DEBUG;
4987 	} else {
4988 		loglevel = KERN_ERR;
4989 	}
4990 
4991 	pipe_config_mismatch(p, fastset, crtc, name, "buffer");
4992 
4993 	/* only dump up to the last difference */
4994 	len = memcmp_diff_len(a, b, len);
4995 
4996 	print_hex_dump(loglevel, "expected: ", DUMP_PREFIX_NONE,
4997 		       16, 0, a, len, false);
4998 	print_hex_dump(loglevel, "found: ", DUMP_PREFIX_NONE,
4999 		       16, 0, b, len, false);
5000 }
5001 
5002 static void
5003 pipe_config_pll_mismatch(struct drm_printer *p, bool fastset,
5004 			 const struct intel_crtc *crtc,
5005 			 const char *name,
5006 			 const struct intel_dpll_hw_state *a,
5007 			 const struct intel_dpll_hw_state *b)
5008 {
5009 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
5010 
5011 	pipe_config_mismatch(p, fastset, crtc, name, " "); /* stupid -Werror=format-zero-length */
5012 
5013 	drm_printf(p, "expected:\n");
5014 	intel_dpll_dump_hw_state(i915, p, a);
5015 	drm_printf(p, "found:\n");
5016 	intel_dpll_dump_hw_state(i915, p, b);
5017 }
5018 
5019 bool
5020 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
5021 			  const struct intel_crtc_state *pipe_config,
5022 			  bool fastset)
5023 {
5024 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
5025 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
5026 	struct drm_printer p;
5027 	bool ret = true;
5028 
5029 	if (fastset)
5030 		p = drm_dbg_printer(&dev_priv->drm, DRM_UT_KMS, NULL);
5031 	else
5032 		p = drm_err_printer(&dev_priv->drm, NULL);
5033 
5034 #define PIPE_CONF_CHECK_X(name) do { \
5035 	if (current_config->name != pipe_config->name) { \
5036 		BUILD_BUG_ON_MSG(__same_type(current_config->name, bool), \
5037 				 __stringify(name) " is bool");	\
5038 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5039 				     "(expected 0x%08x, found 0x%08x)", \
5040 				     current_config->name, \
5041 				     pipe_config->name); \
5042 		ret = false; \
5043 	} \
5044 } while (0)
5045 
5046 #define PIPE_CONF_CHECK_X_WITH_MASK(name, mask) do { \
5047 	if ((current_config->name & (mask)) != (pipe_config->name & (mask))) { \
5048 		BUILD_BUG_ON_MSG(__same_type(current_config->name, bool), \
5049 				 __stringify(name) " is bool");	\
5050 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5051 				     "(expected 0x%08x, found 0x%08x)", \
5052 				     current_config->name & (mask), \
5053 				     pipe_config->name & (mask)); \
5054 		ret = false; \
5055 	} \
5056 } while (0)
5057 
5058 #define PIPE_CONF_CHECK_I(name) do { \
5059 	if (current_config->name != pipe_config->name) { \
5060 		BUILD_BUG_ON_MSG(__same_type(current_config->name, bool), \
5061 				 __stringify(name) " is bool");	\
5062 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5063 				     "(expected %i, found %i)", \
5064 				     current_config->name, \
5065 				     pipe_config->name); \
5066 		ret = false; \
5067 	} \
5068 } while (0)
5069 
5070 #define PIPE_CONF_CHECK_BOOL(name) do { \
5071 	if (current_config->name != pipe_config->name) { \
5072 		BUILD_BUG_ON_MSG(!__same_type(current_config->name, bool), \
5073 				 __stringify(name) " is not bool");	\
5074 		pipe_config_mismatch(&p, fastset, crtc,  __stringify(name), \
5075 				     "(expected %s, found %s)", \
5076 				     str_yes_no(current_config->name), \
5077 				     str_yes_no(pipe_config->name)); \
5078 		ret = false; \
5079 	} \
5080 } while (0)
5081 
5082 #define PIPE_CONF_CHECK_P(name) do { \
5083 	if (current_config->name != pipe_config->name) { \
5084 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5085 				     "(expected %p, found %p)", \
5086 				     current_config->name, \
5087 				     pipe_config->name); \
5088 		ret = false; \
5089 	} \
5090 } while (0)
5091 
5092 #define PIPE_CONF_CHECK_M_N(name) do { \
5093 	if (!intel_compare_link_m_n(&current_config->name, \
5094 				    &pipe_config->name)) { \
5095 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5096 				     "(expected tu %i data %i/%i link %i/%i, " \
5097 				     "found tu %i, data %i/%i link %i/%i)", \
5098 				     current_config->name.tu, \
5099 				     current_config->name.data_m, \
5100 				     current_config->name.data_n, \
5101 				     current_config->name.link_m, \
5102 				     current_config->name.link_n, \
5103 				     pipe_config->name.tu, \
5104 				     pipe_config->name.data_m, \
5105 				     pipe_config->name.data_n, \
5106 				     pipe_config->name.link_m, \
5107 				     pipe_config->name.link_n); \
5108 		ret = false; \
5109 	} \
5110 } while (0)
5111 
5112 #define PIPE_CONF_CHECK_PLL(name) do { \
5113 	if (!intel_dpll_compare_hw_state(dev_priv, &current_config->name, \
5114 					 &pipe_config->name)) { \
5115 		pipe_config_pll_mismatch(&p, fastset, crtc, __stringify(name), \
5116 					 &current_config->name, \
5117 					 &pipe_config->name); \
5118 		ret = false; \
5119 	} \
5120 } while (0)
5121 
5122 #define PIPE_CONF_CHECK_TIMINGS(name) do {     \
5123 	PIPE_CONF_CHECK_I(name.crtc_hdisplay); \
5124 	PIPE_CONF_CHECK_I(name.crtc_htotal); \
5125 	PIPE_CONF_CHECK_I(name.crtc_hblank_start); \
5126 	PIPE_CONF_CHECK_I(name.crtc_hblank_end); \
5127 	PIPE_CONF_CHECK_I(name.crtc_hsync_start); \
5128 	PIPE_CONF_CHECK_I(name.crtc_hsync_end); \
5129 	PIPE_CONF_CHECK_I(name.crtc_vdisplay); \
5130 	PIPE_CONF_CHECK_I(name.crtc_vblank_start); \
5131 	PIPE_CONF_CHECK_I(name.crtc_vsync_start); \
5132 	PIPE_CONF_CHECK_I(name.crtc_vsync_end); \
5133 	if (!fastset || !pipe_config->update_lrr) { \
5134 		PIPE_CONF_CHECK_I(name.crtc_vtotal); \
5135 		PIPE_CONF_CHECK_I(name.crtc_vblank_end); \
5136 	} \
5137 } while (0)
5138 
5139 #define PIPE_CONF_CHECK_RECT(name) do { \
5140 	PIPE_CONF_CHECK_I(name.x1); \
5141 	PIPE_CONF_CHECK_I(name.x2); \
5142 	PIPE_CONF_CHECK_I(name.y1); \
5143 	PIPE_CONF_CHECK_I(name.y2); \
5144 } while (0)
5145 
5146 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
5147 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
5148 		pipe_config_mismatch(&p, fastset, crtc, __stringify(name), \
5149 				     "(%x) (expected %i, found %i)", \
5150 				     (mask), \
5151 				     current_config->name & (mask), \
5152 				     pipe_config->name & (mask)); \
5153 		ret = false; \
5154 	} \
5155 } while (0)
5156 
5157 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
5158 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
5159 				     &pipe_config->infoframes.name)) { \
5160 		pipe_config_infoframe_mismatch(&p, fastset, crtc, __stringify(name), \
5161 					       &current_config->infoframes.name, \
5162 					       &pipe_config->infoframes.name); \
5163 		ret = false; \
5164 	} \
5165 } while (0)
5166 
5167 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
5168 	if (!intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
5169 				      &pipe_config->infoframes.name)) { \
5170 		pipe_config_dp_vsc_sdp_mismatch(&p, fastset, crtc, __stringify(name), \
5171 						&current_config->infoframes.name, \
5172 						&pipe_config->infoframes.name); \
5173 		ret = false; \
5174 	} \
5175 } while (0)
5176 
5177 #define PIPE_CONF_CHECK_DP_AS_SDP(name) do { \
5178 	if (!intel_compare_dp_as_sdp(&current_config->infoframes.name, \
5179 				      &pipe_config->infoframes.name)) { \
5180 		pipe_config_dp_as_sdp_mismatch(dev_priv, fastset, __stringify(name), \
5181 						&current_config->infoframes.name, \
5182 						&pipe_config->infoframes.name); \
5183 		ret = false; \
5184 	} \
5185 } while (0)
5186 
5187 #define PIPE_CONF_CHECK_BUFFER(name, len) do { \
5188 	BUILD_BUG_ON(sizeof(current_config->name) != (len)); \
5189 	BUILD_BUG_ON(sizeof(pipe_config->name) != (len)); \
5190 	if (!intel_compare_buffer(current_config->name, pipe_config->name, (len))) { \
5191 		pipe_config_buffer_mismatch(&p, fastset, crtc, __stringify(name), \
5192 					    current_config->name, \
5193 					    pipe_config->name, \
5194 					    (len)); \
5195 		ret = false; \
5196 	} \
5197 } while (0)
5198 
5199 #define PIPE_CONF_CHECK_COLOR_LUT(lut, is_pre_csc_lut) do { \
5200 	if (current_config->gamma_mode == pipe_config->gamma_mode && \
5201 	    !intel_color_lut_equal(current_config, \
5202 				   current_config->lut, pipe_config->lut, \
5203 				   is_pre_csc_lut)) {	\
5204 		pipe_config_mismatch(&p, fastset, crtc, __stringify(lut), \
5205 				     "hw_state doesn't match sw_state"); \
5206 		ret = false; \
5207 	} \
5208 } while (0)
5209 
5210 #define PIPE_CONF_CHECK_CSC(name) do { \
5211 	PIPE_CONF_CHECK_X(name.preoff[0]); \
5212 	PIPE_CONF_CHECK_X(name.preoff[1]); \
5213 	PIPE_CONF_CHECK_X(name.preoff[2]); \
5214 	PIPE_CONF_CHECK_X(name.coeff[0]); \
5215 	PIPE_CONF_CHECK_X(name.coeff[1]); \
5216 	PIPE_CONF_CHECK_X(name.coeff[2]); \
5217 	PIPE_CONF_CHECK_X(name.coeff[3]); \
5218 	PIPE_CONF_CHECK_X(name.coeff[4]); \
5219 	PIPE_CONF_CHECK_X(name.coeff[5]); \
5220 	PIPE_CONF_CHECK_X(name.coeff[6]); \
5221 	PIPE_CONF_CHECK_X(name.coeff[7]); \
5222 	PIPE_CONF_CHECK_X(name.coeff[8]); \
5223 	PIPE_CONF_CHECK_X(name.postoff[0]); \
5224 	PIPE_CONF_CHECK_X(name.postoff[1]); \
5225 	PIPE_CONF_CHECK_X(name.postoff[2]); \
5226 } while (0)
5227 
5228 #define PIPE_CONF_QUIRK(quirk) \
5229 	((current_config->quirks | pipe_config->quirks) & (quirk))
5230 
5231 	PIPE_CONF_CHECK_BOOL(hw.enable);
5232 	PIPE_CONF_CHECK_BOOL(hw.active);
5233 
5234 	PIPE_CONF_CHECK_I(cpu_transcoder);
5235 	PIPE_CONF_CHECK_I(mst_master_transcoder);
5236 
5237 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
5238 	PIPE_CONF_CHECK_I(fdi_lanes);
5239 	PIPE_CONF_CHECK_M_N(fdi_m_n);
5240 
5241 	PIPE_CONF_CHECK_I(lane_count);
5242 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
5243 
5244 	if (HAS_DOUBLE_BUFFERED_M_N(dev_priv)) {
5245 		if (!fastset || !pipe_config->update_m_n)
5246 			PIPE_CONF_CHECK_M_N(dp_m_n);
5247 	} else {
5248 		PIPE_CONF_CHECK_M_N(dp_m_n);
5249 		PIPE_CONF_CHECK_M_N(dp_m2_n2);
5250 	}
5251 
5252 	PIPE_CONF_CHECK_X(output_types);
5253 
5254 	PIPE_CONF_CHECK_I(framestart_delay);
5255 	PIPE_CONF_CHECK_I(msa_timing_delay);
5256 
5257 	PIPE_CONF_CHECK_TIMINGS(hw.pipe_mode);
5258 	PIPE_CONF_CHECK_TIMINGS(hw.adjusted_mode);
5259 
5260 	PIPE_CONF_CHECK_I(pixel_multiplier);
5261 
5262 	PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5263 			      DRM_MODE_FLAG_INTERLACE);
5264 
5265 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
5266 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5267 				      DRM_MODE_FLAG_PHSYNC);
5268 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5269 				      DRM_MODE_FLAG_NHSYNC);
5270 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5271 				      DRM_MODE_FLAG_PVSYNC);
5272 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5273 				      DRM_MODE_FLAG_NVSYNC);
5274 	}
5275 
5276 	PIPE_CONF_CHECK_I(output_format);
5277 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
5278 	if ((DISPLAY_VER(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
5279 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5280 		PIPE_CONF_CHECK_BOOL(limited_color_range);
5281 
5282 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
5283 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
5284 	PIPE_CONF_CHECK_BOOL(has_infoframe);
5285 	PIPE_CONF_CHECK_BOOL(enhanced_framing);
5286 	PIPE_CONF_CHECK_BOOL(fec_enable);
5287 
5288 	if (!fastset) {
5289 		PIPE_CONF_CHECK_BOOL(has_audio);
5290 		PIPE_CONF_CHECK_BUFFER(eld, MAX_ELD_BYTES);
5291 	}
5292 
5293 	PIPE_CONF_CHECK_X(gmch_pfit.control);
5294 	/* pfit ratios are autocomputed by the hw on gen4+ */
5295 	if (DISPLAY_VER(dev_priv) < 4)
5296 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
5297 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
5298 
5299 	/*
5300 	 * Changing the EDP transcoder input mux
5301 	 * (A_ONOFF vs. A_ON) requires a full modeset.
5302 	 */
5303 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
5304 
5305 	if (!fastset) {
5306 		PIPE_CONF_CHECK_RECT(pipe_src);
5307 
5308 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
5309 		PIPE_CONF_CHECK_RECT(pch_pfit.dst);
5310 
5311 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
5312 		PIPE_CONF_CHECK_I(pixel_rate);
5313 
5314 		PIPE_CONF_CHECK_X(gamma_mode);
5315 		if (IS_CHERRYVIEW(dev_priv))
5316 			PIPE_CONF_CHECK_X(cgm_mode);
5317 		else
5318 			PIPE_CONF_CHECK_X(csc_mode);
5319 		PIPE_CONF_CHECK_BOOL(gamma_enable);
5320 		PIPE_CONF_CHECK_BOOL(csc_enable);
5321 		PIPE_CONF_CHECK_BOOL(wgc_enable);
5322 
5323 		PIPE_CONF_CHECK_I(linetime);
5324 		PIPE_CONF_CHECK_I(ips_linetime);
5325 
5326 		PIPE_CONF_CHECK_COLOR_LUT(pre_csc_lut, true);
5327 		PIPE_CONF_CHECK_COLOR_LUT(post_csc_lut, false);
5328 
5329 		PIPE_CONF_CHECK_CSC(csc);
5330 		PIPE_CONF_CHECK_CSC(output_csc);
5331 	}
5332 
5333 	/*
5334 	 * Panel replay has to be enabled before link training. PSR doesn't have
5335 	 * this requirement -> check these only if using panel replay
5336 	 */
5337 	if (current_config->has_panel_replay || pipe_config->has_panel_replay) {
5338 		PIPE_CONF_CHECK_BOOL(has_psr);
5339 		PIPE_CONF_CHECK_BOOL(has_psr2);
5340 		PIPE_CONF_CHECK_BOOL(enable_psr2_sel_fetch);
5341 		PIPE_CONF_CHECK_BOOL(enable_psr2_su_region_et);
5342 		PIPE_CONF_CHECK_BOOL(has_panel_replay);
5343 	}
5344 
5345 	PIPE_CONF_CHECK_BOOL(double_wide);
5346 
5347 	if (dev_priv->display.dpll.mgr)
5348 		PIPE_CONF_CHECK_P(shared_dpll);
5349 
5350 	/* FIXME convert everything over the dpll_mgr */
5351 	if (dev_priv->display.dpll.mgr || HAS_GMCH(dev_priv))
5352 		PIPE_CONF_CHECK_PLL(dpll_hw_state);
5353 
5354 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
5355 	PIPE_CONF_CHECK_X(dsi_pll.div);
5356 
5357 	if (IS_G4X(dev_priv) || DISPLAY_VER(dev_priv) >= 5)
5358 		PIPE_CONF_CHECK_I(pipe_bpp);
5359 
5360 	if (!fastset || !pipe_config->update_m_n) {
5361 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_clock);
5362 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_clock);
5363 	}
5364 	PIPE_CONF_CHECK_I(port_clock);
5365 
5366 	PIPE_CONF_CHECK_I(min_voltage_level);
5367 
5368 	if (current_config->has_psr || pipe_config->has_psr)
5369 		PIPE_CONF_CHECK_X_WITH_MASK(infoframes.enable,
5370 					    ~intel_hdmi_infoframe_enable(DP_SDP_VSC));
5371 	else
5372 		PIPE_CONF_CHECK_X(infoframes.enable);
5373 
5374 	PIPE_CONF_CHECK_X(infoframes.gcp);
5375 	PIPE_CONF_CHECK_INFOFRAME(avi);
5376 	PIPE_CONF_CHECK_INFOFRAME(spd);
5377 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
5378 	PIPE_CONF_CHECK_INFOFRAME(drm);
5379 	PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
5380 	PIPE_CONF_CHECK_DP_AS_SDP(as_sdp);
5381 
5382 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
5383 	PIPE_CONF_CHECK_I(master_transcoder);
5384 	PIPE_CONF_CHECK_X(bigjoiner_pipes);
5385 
5386 	PIPE_CONF_CHECK_BOOL(dsc.config.block_pred_enable);
5387 	PIPE_CONF_CHECK_BOOL(dsc.config.convert_rgb);
5388 	PIPE_CONF_CHECK_BOOL(dsc.config.simple_422);
5389 	PIPE_CONF_CHECK_BOOL(dsc.config.native_422);
5390 	PIPE_CONF_CHECK_BOOL(dsc.config.native_420);
5391 	PIPE_CONF_CHECK_BOOL(dsc.config.vbr_enable);
5392 	PIPE_CONF_CHECK_I(dsc.config.line_buf_depth);
5393 	PIPE_CONF_CHECK_I(dsc.config.bits_per_component);
5394 	PIPE_CONF_CHECK_I(dsc.config.pic_width);
5395 	PIPE_CONF_CHECK_I(dsc.config.pic_height);
5396 	PIPE_CONF_CHECK_I(dsc.config.slice_width);
5397 	PIPE_CONF_CHECK_I(dsc.config.slice_height);
5398 	PIPE_CONF_CHECK_I(dsc.config.initial_dec_delay);
5399 	PIPE_CONF_CHECK_I(dsc.config.initial_xmit_delay);
5400 	PIPE_CONF_CHECK_I(dsc.config.scale_decrement_interval);
5401 	PIPE_CONF_CHECK_I(dsc.config.scale_increment_interval);
5402 	PIPE_CONF_CHECK_I(dsc.config.initial_scale_value);
5403 	PIPE_CONF_CHECK_I(dsc.config.first_line_bpg_offset);
5404 	PIPE_CONF_CHECK_I(dsc.config.flatness_min_qp);
5405 	PIPE_CONF_CHECK_I(dsc.config.flatness_max_qp);
5406 	PIPE_CONF_CHECK_I(dsc.config.slice_bpg_offset);
5407 	PIPE_CONF_CHECK_I(dsc.config.nfl_bpg_offset);
5408 	PIPE_CONF_CHECK_I(dsc.config.initial_offset);
5409 	PIPE_CONF_CHECK_I(dsc.config.final_offset);
5410 	PIPE_CONF_CHECK_I(dsc.config.rc_model_size);
5411 	PIPE_CONF_CHECK_I(dsc.config.rc_quant_incr_limit0);
5412 	PIPE_CONF_CHECK_I(dsc.config.rc_quant_incr_limit1);
5413 	PIPE_CONF_CHECK_I(dsc.config.slice_chunk_size);
5414 	PIPE_CONF_CHECK_I(dsc.config.second_line_bpg_offset);
5415 	PIPE_CONF_CHECK_I(dsc.config.nsl_bpg_offset);
5416 
5417 	PIPE_CONF_CHECK_BOOL(dsc.compression_enable);
5418 	PIPE_CONF_CHECK_BOOL(dsc.dsc_split);
5419 	PIPE_CONF_CHECK_I(dsc.compressed_bpp_x16);
5420 
5421 	PIPE_CONF_CHECK_BOOL(splitter.enable);
5422 	PIPE_CONF_CHECK_I(splitter.link_count);
5423 	PIPE_CONF_CHECK_I(splitter.pixel_overlap);
5424 
5425 	if (!fastset) {
5426 		PIPE_CONF_CHECK_BOOL(vrr.enable);
5427 		PIPE_CONF_CHECK_I(vrr.vmin);
5428 		PIPE_CONF_CHECK_I(vrr.vmax);
5429 		PIPE_CONF_CHECK_I(vrr.flipline);
5430 		PIPE_CONF_CHECK_I(vrr.pipeline_full);
5431 		PIPE_CONF_CHECK_I(vrr.guardband);
5432 		PIPE_CONF_CHECK_I(vrr.vsync_start);
5433 		PIPE_CONF_CHECK_I(vrr.vsync_end);
5434 	}
5435 
5436 #undef PIPE_CONF_CHECK_X
5437 #undef PIPE_CONF_CHECK_I
5438 #undef PIPE_CONF_CHECK_BOOL
5439 #undef PIPE_CONF_CHECK_P
5440 #undef PIPE_CONF_CHECK_FLAGS
5441 #undef PIPE_CONF_CHECK_COLOR_LUT
5442 #undef PIPE_CONF_CHECK_TIMINGS
5443 #undef PIPE_CONF_CHECK_RECT
5444 #undef PIPE_CONF_QUIRK
5445 
5446 	return ret;
5447 }
5448 
5449 static void
5450 intel_verify_planes(struct intel_atomic_state *state)
5451 {
5452 	struct intel_plane *plane;
5453 	const struct intel_plane_state *plane_state;
5454 	int i;
5455 
5456 	for_each_new_intel_plane_in_state(state, plane,
5457 					  plane_state, i)
5458 		assert_plane(plane, plane_state->planar_slave ||
5459 			     plane_state->uapi.visible);
5460 }
5461 
5462 static int intel_modeset_pipe(struct intel_atomic_state *state,
5463 			      struct intel_crtc_state *crtc_state,
5464 			      const char *reason)
5465 {
5466 	struct drm_i915_private *i915 = to_i915(state->base.dev);
5467 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5468 	int ret;
5469 
5470 	drm_dbg_kms(&i915->drm, "[CRTC:%d:%s] Full modeset due to %s\n",
5471 		    crtc->base.base.id, crtc->base.name, reason);
5472 
5473 	ret = drm_atomic_add_affected_connectors(&state->base,
5474 						 &crtc->base);
5475 	if (ret)
5476 		return ret;
5477 
5478 	ret = intel_dp_tunnel_atomic_add_state_for_crtc(state, crtc);
5479 	if (ret)
5480 		return ret;
5481 
5482 	ret = intel_dp_mst_add_topology_state_for_crtc(state, crtc);
5483 	if (ret)
5484 		return ret;
5485 
5486 	ret = intel_atomic_add_affected_planes(state, crtc);
5487 	if (ret)
5488 		return ret;
5489 
5490 	crtc_state->uapi.mode_changed = true;
5491 
5492 	return 0;
5493 }
5494 
5495 /**
5496  * intel_modeset_pipes_in_mask_early - force a full modeset on a set of pipes
5497  * @state: intel atomic state
5498  * @reason: the reason for the full modeset
5499  * @mask: mask of pipes to modeset
5500  *
5501  * Add pipes in @mask to @state and force a full modeset on the enabled ones
5502  * due to the description in @reason.
5503  * This function can be called only before new plane states are computed.
5504  *
5505  * Returns 0 in case of success, negative error code otherwise.
5506  */
5507 int intel_modeset_pipes_in_mask_early(struct intel_atomic_state *state,
5508 				      const char *reason, u8 mask)
5509 {
5510 	struct drm_i915_private *i915 = to_i915(state->base.dev);
5511 	struct intel_crtc *crtc;
5512 
5513 	for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc, mask) {
5514 		struct intel_crtc_state *crtc_state;
5515 		int ret;
5516 
5517 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5518 		if (IS_ERR(crtc_state))
5519 			return PTR_ERR(crtc_state);
5520 
5521 		if (!crtc_state->hw.enable ||
5522 		    intel_crtc_needs_modeset(crtc_state))
5523 			continue;
5524 
5525 		ret = intel_modeset_pipe(state, crtc_state, reason);
5526 		if (ret)
5527 			return ret;
5528 	}
5529 
5530 	return 0;
5531 }
5532 
5533 static void
5534 intel_crtc_flag_modeset(struct intel_crtc_state *crtc_state)
5535 {
5536 	crtc_state->uapi.mode_changed = true;
5537 
5538 	crtc_state->update_pipe = false;
5539 	crtc_state->update_m_n = false;
5540 	crtc_state->update_lrr = false;
5541 }
5542 
5543 /**
5544  * intel_modeset_all_pipes_late - force a full modeset on all pipes
5545  * @state: intel atomic state
5546  * @reason: the reason for the full modeset
5547  *
5548  * Add all pipes to @state and force a full modeset on the active ones due to
5549  * the description in @reason.
5550  * This function can be called only after new plane states are computed already.
5551  *
5552  * Returns 0 in case of success, negative error code otherwise.
5553  */
5554 int intel_modeset_all_pipes_late(struct intel_atomic_state *state,
5555 				 const char *reason)
5556 {
5557 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5558 	struct intel_crtc *crtc;
5559 
5560 	for_each_intel_crtc(&dev_priv->drm, crtc) {
5561 		struct intel_crtc_state *crtc_state;
5562 		int ret;
5563 
5564 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5565 		if (IS_ERR(crtc_state))
5566 			return PTR_ERR(crtc_state);
5567 
5568 		if (!crtc_state->hw.active ||
5569 		    intel_crtc_needs_modeset(crtc_state))
5570 			continue;
5571 
5572 		ret = intel_modeset_pipe(state, crtc_state, reason);
5573 		if (ret)
5574 			return ret;
5575 
5576 		intel_crtc_flag_modeset(crtc_state);
5577 
5578 		crtc_state->update_planes |= crtc_state->active_planes;
5579 		crtc_state->async_flip_planes = 0;
5580 		crtc_state->do_async_flip = false;
5581 	}
5582 
5583 	return 0;
5584 }
5585 
5586 /*
5587  * This implements the workaround described in the "notes" section of the mode
5588  * set sequence documentation. When going from no pipes or single pipe to
5589  * multiple pipes, and planes are enabled after the pipe, we need to wait at
5590  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
5591  */
5592 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
5593 {
5594 	struct intel_crtc_state *crtc_state;
5595 	struct intel_crtc *crtc;
5596 	struct intel_crtc_state *first_crtc_state = NULL;
5597 	struct intel_crtc_state *other_crtc_state = NULL;
5598 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
5599 	int i;
5600 
5601 	/* look at all crtc's that are going to be enabled in during modeset */
5602 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5603 		if (!crtc_state->hw.active ||
5604 		    !intel_crtc_needs_modeset(crtc_state))
5605 			continue;
5606 
5607 		if (first_crtc_state) {
5608 			other_crtc_state = crtc_state;
5609 			break;
5610 		} else {
5611 			first_crtc_state = crtc_state;
5612 			first_pipe = crtc->pipe;
5613 		}
5614 	}
5615 
5616 	/* No workaround needed? */
5617 	if (!first_crtc_state)
5618 		return 0;
5619 
5620 	/* w/a possibly needed, check how many crtc's are already enabled. */
5621 	for_each_intel_crtc(state->base.dev, crtc) {
5622 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5623 		if (IS_ERR(crtc_state))
5624 			return PTR_ERR(crtc_state);
5625 
5626 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
5627 
5628 		if (!crtc_state->hw.active ||
5629 		    intel_crtc_needs_modeset(crtc_state))
5630 			continue;
5631 
5632 		/* 2 or more enabled crtcs means no need for w/a */
5633 		if (enabled_pipe != INVALID_PIPE)
5634 			return 0;
5635 
5636 		enabled_pipe = crtc->pipe;
5637 	}
5638 
5639 	if (enabled_pipe != INVALID_PIPE)
5640 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
5641 	else if (other_crtc_state)
5642 		other_crtc_state->hsw_workaround_pipe = first_pipe;
5643 
5644 	return 0;
5645 }
5646 
5647 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
5648 			   u8 active_pipes)
5649 {
5650 	const struct intel_crtc_state *crtc_state;
5651 	struct intel_crtc *crtc;
5652 	int i;
5653 
5654 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5655 		if (crtc_state->hw.active)
5656 			active_pipes |= BIT(crtc->pipe);
5657 		else
5658 			active_pipes &= ~BIT(crtc->pipe);
5659 	}
5660 
5661 	return active_pipes;
5662 }
5663 
5664 static int intel_modeset_checks(struct intel_atomic_state *state)
5665 {
5666 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5667 
5668 	state->modeset = true;
5669 
5670 	if (IS_HASWELL(dev_priv))
5671 		return hsw_mode_set_planes_workaround(state);
5672 
5673 	return 0;
5674 }
5675 
5676 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
5677 				     struct intel_crtc_state *new_crtc_state)
5678 {
5679 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
5680 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
5681 
5682 	/* only allow LRR when the timings stay within the VRR range */
5683 	if (old_crtc_state->vrr.in_range != new_crtc_state->vrr.in_range)
5684 		new_crtc_state->update_lrr = false;
5685 
5686 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
5687 		drm_dbg_kms(&i915->drm, "[CRTC:%d:%s] fastset requirement not met, forcing full modeset\n",
5688 			    crtc->base.base.id, crtc->base.name);
5689 	else
5690 		new_crtc_state->uapi.mode_changed = false;
5691 
5692 	if (intel_compare_link_m_n(&old_crtc_state->dp_m_n,
5693 				   &new_crtc_state->dp_m_n))
5694 		new_crtc_state->update_m_n = false;
5695 
5696 	if ((old_crtc_state->hw.adjusted_mode.crtc_vtotal == new_crtc_state->hw.adjusted_mode.crtc_vtotal &&
5697 	     old_crtc_state->hw.adjusted_mode.crtc_vblank_end == new_crtc_state->hw.adjusted_mode.crtc_vblank_end))
5698 		new_crtc_state->update_lrr = false;
5699 
5700 	if (intel_crtc_needs_modeset(new_crtc_state))
5701 		intel_crtc_flag_modeset(new_crtc_state);
5702 	else
5703 		new_crtc_state->update_pipe = true;
5704 }
5705 
5706 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
5707 					  struct intel_crtc *crtc,
5708 					  u8 plane_ids_mask)
5709 {
5710 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5711 	struct intel_plane *plane;
5712 
5713 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
5714 		struct intel_plane_state *plane_state;
5715 
5716 		if ((plane_ids_mask & BIT(plane->id)) == 0)
5717 			continue;
5718 
5719 		plane_state = intel_atomic_get_plane_state(state, plane);
5720 		if (IS_ERR(plane_state))
5721 			return PTR_ERR(plane_state);
5722 	}
5723 
5724 	return 0;
5725 }
5726 
5727 int intel_atomic_add_affected_planes(struct intel_atomic_state *state,
5728 				     struct intel_crtc *crtc)
5729 {
5730 	const struct intel_crtc_state *old_crtc_state =
5731 		intel_atomic_get_old_crtc_state(state, crtc);
5732 	const struct intel_crtc_state *new_crtc_state =
5733 		intel_atomic_get_new_crtc_state(state, crtc);
5734 
5735 	return intel_crtc_add_planes_to_state(state, crtc,
5736 					      old_crtc_state->enabled_planes |
5737 					      new_crtc_state->enabled_planes);
5738 }
5739 
5740 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
5741 {
5742 	/* See {hsw,vlv,ivb}_plane_ratio() */
5743 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
5744 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
5745 		IS_IVYBRIDGE(dev_priv);
5746 }
5747 
5748 static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
5749 					   struct intel_crtc *crtc,
5750 					   struct intel_crtc *other)
5751 {
5752 	const struct intel_plane_state __maybe_unused *plane_state;
5753 	struct intel_plane *plane;
5754 	u8 plane_ids = 0;
5755 	int i;
5756 
5757 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5758 		if (plane->pipe == crtc->pipe)
5759 			plane_ids |= BIT(plane->id);
5760 	}
5761 
5762 	return intel_crtc_add_planes_to_state(state, other, plane_ids);
5763 }
5764 
5765 static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
5766 {
5767 	struct drm_i915_private *i915 = to_i915(state->base.dev);
5768 	const struct intel_crtc_state *crtc_state;
5769 	struct intel_crtc *crtc;
5770 	int i;
5771 
5772 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5773 		struct intel_crtc *other;
5774 
5775 		for_each_intel_crtc_in_pipe_mask(&i915->drm, other,
5776 						 crtc_state->bigjoiner_pipes) {
5777 			int ret;
5778 
5779 			if (crtc == other)
5780 				continue;
5781 
5782 			ret = intel_crtc_add_bigjoiner_planes(state, crtc, other);
5783 			if (ret)
5784 				return ret;
5785 		}
5786 	}
5787 
5788 	return 0;
5789 }
5790 
5791 static int intel_atomic_check_planes(struct intel_atomic_state *state)
5792 {
5793 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5794 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
5795 	struct intel_plane_state __maybe_unused *plane_state;
5796 	struct intel_plane *plane;
5797 	struct intel_crtc *crtc;
5798 	int i, ret;
5799 
5800 	ret = icl_add_linked_planes(state);
5801 	if (ret)
5802 		return ret;
5803 
5804 	ret = intel_bigjoiner_add_affected_planes(state);
5805 	if (ret)
5806 		return ret;
5807 
5808 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
5809 		ret = intel_plane_atomic_check(state, plane);
5810 		if (ret) {
5811 			drm_dbg_atomic(&dev_priv->drm,
5812 				       "[PLANE:%d:%s] atomic driver check failed\n",
5813 				       plane->base.base.id, plane->base.name);
5814 			return ret;
5815 		}
5816 	}
5817 
5818 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
5819 					    new_crtc_state, i) {
5820 		u8 old_active_planes, new_active_planes;
5821 
5822 		ret = icl_check_nv12_planes(new_crtc_state);
5823 		if (ret)
5824 			return ret;
5825 
5826 		/*
5827 		 * On some platforms the number of active planes affects
5828 		 * the planes' minimum cdclk calculation. Add such planes
5829 		 * to the state before we compute the minimum cdclk.
5830 		 */
5831 		if (!active_planes_affects_min_cdclk(dev_priv))
5832 			continue;
5833 
5834 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
5835 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
5836 
5837 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
5838 			continue;
5839 
5840 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
5841 		if (ret)
5842 			return ret;
5843 	}
5844 
5845 	return 0;
5846 }
5847 
5848 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
5849 {
5850 	struct intel_crtc_state __maybe_unused *crtc_state;
5851 	struct intel_crtc *crtc;
5852 	int i;
5853 
5854 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
5855 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
5856 		int ret;
5857 
5858 		ret = intel_crtc_atomic_check(state, crtc);
5859 		if (ret) {
5860 			drm_dbg_atomic(&i915->drm,
5861 				       "[CRTC:%d:%s] atomic driver check failed\n",
5862 				       crtc->base.base.id, crtc->base.name);
5863 			return ret;
5864 		}
5865 	}
5866 
5867 	return 0;
5868 }
5869 
5870 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
5871 					       u8 transcoders)
5872 {
5873 	const struct intel_crtc_state *new_crtc_state;
5874 	struct intel_crtc *crtc;
5875 	int i;
5876 
5877 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
5878 		if (new_crtc_state->hw.enable &&
5879 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
5880 		    intel_crtc_needs_modeset(new_crtc_state))
5881 			return true;
5882 	}
5883 
5884 	return false;
5885 }
5886 
5887 static bool intel_pipes_need_modeset(struct intel_atomic_state *state,
5888 				     u8 pipes)
5889 {
5890 	const struct intel_crtc_state *new_crtc_state;
5891 	struct intel_crtc *crtc;
5892 	int i;
5893 
5894 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
5895 		if (new_crtc_state->hw.enable &&
5896 		    pipes & BIT(crtc->pipe) &&
5897 		    intel_crtc_needs_modeset(new_crtc_state))
5898 			return true;
5899 	}
5900 
5901 	return false;
5902 }
5903 
5904 static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
5905 					struct intel_crtc *master_crtc)
5906 {
5907 	struct drm_i915_private *i915 = to_i915(state->base.dev);
5908 	struct intel_crtc_state *master_crtc_state =
5909 		intel_atomic_get_new_crtc_state(state, master_crtc);
5910 	struct intel_crtc *slave_crtc;
5911 
5912 	if (!master_crtc_state->bigjoiner_pipes)
5913 		return 0;
5914 
5915 	/* sanity check */
5916 	if (drm_WARN_ON(&i915->drm,
5917 			master_crtc->pipe != bigjoiner_master_pipe(master_crtc_state)))
5918 		return -EINVAL;
5919 
5920 	if (master_crtc_state->bigjoiner_pipes & ~bigjoiner_pipes(i915)) {
5921 		drm_dbg_kms(&i915->drm,
5922 			    "[CRTC:%d:%s] Cannot act as big joiner master "
5923 			    "(need 0x%x as pipes, only 0x%x possible)\n",
5924 			    master_crtc->base.base.id, master_crtc->base.name,
5925 			    master_crtc_state->bigjoiner_pipes, bigjoiner_pipes(i915));
5926 		return -EINVAL;
5927 	}
5928 
5929 	for_each_intel_crtc_in_pipe_mask(&i915->drm, slave_crtc,
5930 					 intel_crtc_bigjoiner_slave_pipes(master_crtc_state)) {
5931 		struct intel_crtc_state *slave_crtc_state;
5932 		int ret;
5933 
5934 		slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave_crtc);
5935 		if (IS_ERR(slave_crtc_state))
5936 			return PTR_ERR(slave_crtc_state);
5937 
5938 		/* master being enabled, slave was already configured? */
5939 		if (slave_crtc_state->uapi.enable) {
5940 			drm_dbg_kms(&i915->drm,
5941 				    "[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
5942 				    "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
5943 				    slave_crtc->base.base.id, slave_crtc->base.name,
5944 				    master_crtc->base.base.id, master_crtc->base.name);
5945 			return -EINVAL;
5946 		}
5947 
5948 		/*
5949 		 * The state copy logic assumes the master crtc gets processed
5950 		 * before the slave crtc during the main compute_config loop.
5951 		 * This works because the crtcs are created in pipe order,
5952 		 * and the hardware requires master pipe < slave pipe as well.
5953 		 * Should that change we need to rethink the logic.
5954 		 */
5955 		if (WARN_ON(drm_crtc_index(&master_crtc->base) >
5956 			    drm_crtc_index(&slave_crtc->base)))
5957 			return -EINVAL;
5958 
5959 		drm_dbg_kms(&i915->drm,
5960 			    "[CRTC:%d:%s] Used as slave for big joiner master [CRTC:%d:%s]\n",
5961 			    slave_crtc->base.base.id, slave_crtc->base.name,
5962 			    master_crtc->base.base.id, master_crtc->base.name);
5963 
5964 		slave_crtc_state->bigjoiner_pipes =
5965 			master_crtc_state->bigjoiner_pipes;
5966 
5967 		ret = copy_bigjoiner_crtc_state_modeset(state, slave_crtc);
5968 		if (ret)
5969 			return ret;
5970 	}
5971 
5972 	return 0;
5973 }
5974 
5975 static void kill_bigjoiner_slave(struct intel_atomic_state *state,
5976 				 struct intel_crtc *master_crtc)
5977 {
5978 	struct drm_i915_private *i915 = to_i915(state->base.dev);
5979 	struct intel_crtc_state *master_crtc_state =
5980 		intel_atomic_get_new_crtc_state(state, master_crtc);
5981 	struct intel_crtc *slave_crtc;
5982 
5983 	for_each_intel_crtc_in_pipe_mask(&i915->drm, slave_crtc,
5984 					 intel_crtc_bigjoiner_slave_pipes(master_crtc_state)) {
5985 		struct intel_crtc_state *slave_crtc_state =
5986 			intel_atomic_get_new_crtc_state(state, slave_crtc);
5987 
5988 		slave_crtc_state->bigjoiner_pipes = 0;
5989 
5990 		intel_crtc_copy_uapi_to_hw_state_modeset(state, slave_crtc);
5991 	}
5992 
5993 	master_crtc_state->bigjoiner_pipes = 0;
5994 }
5995 
5996 /**
5997  * DOC: asynchronous flip implementation
5998  *
5999  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
6000  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
6001  * Correspondingly, support is currently added for primary plane only.
6002  *
6003  * Async flip can only change the plane surface address, so anything else
6004  * changing is rejected from the intel_async_flip_check_hw() function.
6005  * Once this check is cleared, flip done interrupt is enabled using
6006  * the intel_crtc_enable_flip_done() function.
6007  *
6008  * As soon as the surface address register is written, flip done interrupt is
6009  * generated and the requested events are sent to the usersapce in the interrupt
6010  * handler itself. The timestamp and sequence sent during the flip done event
6011  * correspond to the last vblank and have no relation to the actual time when
6012  * the flip done event was sent.
6013  */
6014 static int intel_async_flip_check_uapi(struct intel_atomic_state *state,
6015 				       struct intel_crtc *crtc)
6016 {
6017 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6018 	const struct intel_crtc_state *new_crtc_state =
6019 		intel_atomic_get_new_crtc_state(state, crtc);
6020 	const struct intel_plane_state *old_plane_state;
6021 	struct intel_plane_state *new_plane_state;
6022 	struct intel_plane *plane;
6023 	int i;
6024 
6025 	if (!new_crtc_state->uapi.async_flip)
6026 		return 0;
6027 
6028 	if (!new_crtc_state->uapi.active) {
6029 		drm_dbg_kms(&i915->drm,
6030 			    "[CRTC:%d:%s] not active\n",
6031 			    crtc->base.base.id, crtc->base.name);
6032 		return -EINVAL;
6033 	}
6034 
6035 	if (intel_crtc_needs_modeset(new_crtc_state)) {
6036 		drm_dbg_kms(&i915->drm,
6037 			    "[CRTC:%d:%s] modeset required\n",
6038 			    crtc->base.base.id, crtc->base.name);
6039 		return -EINVAL;
6040 	}
6041 
6042 	/*
6043 	 * FIXME: Bigjoiner+async flip is busted currently.
6044 	 * Remove this check once the issues are fixed.
6045 	 */
6046 	if (new_crtc_state->bigjoiner_pipes) {
6047 		drm_dbg_kms(&i915->drm,
6048 			    "[CRTC:%d:%s] async flip disallowed with bigjoiner\n",
6049 			    crtc->base.base.id, crtc->base.name);
6050 		return -EINVAL;
6051 	}
6052 
6053 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
6054 					     new_plane_state, i) {
6055 		if (plane->pipe != crtc->pipe)
6056 			continue;
6057 
6058 		/*
6059 		 * TODO: Async flip is only supported through the page flip IOCTL
6060 		 * as of now. So support currently added for primary plane only.
6061 		 * Support for other planes on platforms on which supports
6062 		 * this(vlv/chv and icl+) should be added when async flip is
6063 		 * enabled in the atomic IOCTL path.
6064 		 */
6065 		if (!plane->async_flip) {
6066 			drm_dbg_kms(&i915->drm,
6067 				    "[PLANE:%d:%s] async flip not supported\n",
6068 				    plane->base.base.id, plane->base.name);
6069 			return -EINVAL;
6070 		}
6071 
6072 		if (!old_plane_state->uapi.fb || !new_plane_state->uapi.fb) {
6073 			drm_dbg_kms(&i915->drm,
6074 				    "[PLANE:%d:%s] no old or new framebuffer\n",
6075 				    plane->base.base.id, plane->base.name);
6076 			return -EINVAL;
6077 		}
6078 	}
6079 
6080 	return 0;
6081 }
6082 
6083 static int intel_async_flip_check_hw(struct intel_atomic_state *state, struct intel_crtc *crtc)
6084 {
6085 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6086 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
6087 	const struct intel_plane_state *new_plane_state, *old_plane_state;
6088 	struct intel_plane *plane;
6089 	int i;
6090 
6091 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
6092 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6093 
6094 	if (!new_crtc_state->uapi.async_flip)
6095 		return 0;
6096 
6097 	if (!new_crtc_state->hw.active) {
6098 		drm_dbg_kms(&i915->drm,
6099 			    "[CRTC:%d:%s] not active\n",
6100 			    crtc->base.base.id, crtc->base.name);
6101 		return -EINVAL;
6102 	}
6103 
6104 	if (intel_crtc_needs_modeset(new_crtc_state)) {
6105 		drm_dbg_kms(&i915->drm,
6106 			    "[CRTC:%d:%s] modeset required\n",
6107 			    crtc->base.base.id, crtc->base.name);
6108 		return -EINVAL;
6109 	}
6110 
6111 	if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
6112 		drm_dbg_kms(&i915->drm,
6113 			    "[CRTC:%d:%s] Active planes cannot be in async flip\n",
6114 			    crtc->base.base.id, crtc->base.name);
6115 		return -EINVAL;
6116 	}
6117 
6118 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
6119 					     new_plane_state, i) {
6120 		if (plane->pipe != crtc->pipe)
6121 			continue;
6122 
6123 		/*
6124 		 * Only async flip capable planes should be in the state
6125 		 * if we're really about to ask the hardware to perform
6126 		 * an async flip. We should never get this far otherwise.
6127 		 */
6128 		if (drm_WARN_ON(&i915->drm,
6129 				new_crtc_state->do_async_flip && !plane->async_flip))
6130 			return -EINVAL;
6131 
6132 		/*
6133 		 * Only check async flip capable planes other planes
6134 		 * may be involved in the initial commit due to
6135 		 * the wm0/ddb optimization.
6136 		 *
6137 		 * TODO maybe should track which planes actually
6138 		 * were requested to do the async flip...
6139 		 */
6140 		if (!plane->async_flip)
6141 			continue;
6142 
6143 		/*
6144 		 * FIXME: This check is kept generic for all platforms.
6145 		 * Need to verify this for all gen9 platforms to enable
6146 		 * this selectively if required.
6147 		 */
6148 		switch (new_plane_state->hw.fb->modifier) {
6149 		case DRM_FORMAT_MOD_LINEAR:
6150 			/*
6151 			 * FIXME: Async on Linear buffer is supported on ICL as
6152 			 * but with additional alignment and fbc restrictions
6153 			 * need to be taken care of. These aren't applicable for
6154 			 * gen12+.
6155 			 */
6156 			if (DISPLAY_VER(i915) < 12) {
6157 				drm_dbg_kms(&i915->drm,
6158 					    "[PLANE:%d:%s] Modifier 0x%llx does not support async flip on display ver %d\n",
6159 					    plane->base.base.id, plane->base.name,
6160 					    new_plane_state->hw.fb->modifier, DISPLAY_VER(i915));
6161 				return -EINVAL;
6162 			}
6163 			break;
6164 
6165 		case I915_FORMAT_MOD_X_TILED:
6166 		case I915_FORMAT_MOD_Y_TILED:
6167 		case I915_FORMAT_MOD_Yf_TILED:
6168 		case I915_FORMAT_MOD_4_TILED:
6169 			break;
6170 		default:
6171 			drm_dbg_kms(&i915->drm,
6172 				    "[PLANE:%d:%s] Modifier 0x%llx does not support async flip\n",
6173 				    plane->base.base.id, plane->base.name,
6174 				    new_plane_state->hw.fb->modifier);
6175 			return -EINVAL;
6176 		}
6177 
6178 		if (new_plane_state->hw.fb->format->num_planes > 1) {
6179 			drm_dbg_kms(&i915->drm,
6180 				    "[PLANE:%d:%s] Planar formats do not support async flips\n",
6181 				    plane->base.base.id, plane->base.name);
6182 			return -EINVAL;
6183 		}
6184 
6185 		if (old_plane_state->view.color_plane[0].mapping_stride !=
6186 		    new_plane_state->view.color_plane[0].mapping_stride) {
6187 			drm_dbg_kms(&i915->drm,
6188 				    "[PLANE:%d:%s] Stride cannot be changed in async flip\n",
6189 				    plane->base.base.id, plane->base.name);
6190 			return -EINVAL;
6191 		}
6192 
6193 		if (old_plane_state->hw.fb->modifier !=
6194 		    new_plane_state->hw.fb->modifier) {
6195 			drm_dbg_kms(&i915->drm,
6196 				    "[PLANE:%d:%s] Modifier cannot be changed in async flip\n",
6197 				    plane->base.base.id, plane->base.name);
6198 			return -EINVAL;
6199 		}
6200 
6201 		if (old_plane_state->hw.fb->format !=
6202 		    new_plane_state->hw.fb->format) {
6203 			drm_dbg_kms(&i915->drm,
6204 				    "[PLANE:%d:%s] Pixel format cannot be changed in async flip\n",
6205 				    plane->base.base.id, plane->base.name);
6206 			return -EINVAL;
6207 		}
6208 
6209 		if (old_plane_state->hw.rotation !=
6210 		    new_plane_state->hw.rotation) {
6211 			drm_dbg_kms(&i915->drm,
6212 				    "[PLANE:%d:%s] Rotation cannot be changed in async flip\n",
6213 				    plane->base.base.id, plane->base.name);
6214 			return -EINVAL;
6215 		}
6216 
6217 		if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
6218 		    !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
6219 			drm_dbg_kms(&i915->drm,
6220 				    "[PLANE:%d:%s] Size/co-ordinates cannot be changed in async flip\n",
6221 				    plane->base.base.id, plane->base.name);
6222 			return -EINVAL;
6223 		}
6224 
6225 		if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
6226 			drm_dbg_kms(&i915->drm,
6227 				    "[PLANES:%d:%s] Alpha value cannot be changed in async flip\n",
6228 				    plane->base.base.id, plane->base.name);
6229 			return -EINVAL;
6230 		}
6231 
6232 		if (old_plane_state->hw.pixel_blend_mode !=
6233 		    new_plane_state->hw.pixel_blend_mode) {
6234 			drm_dbg_kms(&i915->drm,
6235 				    "[PLANE:%d:%s] Pixel blend mode cannot be changed in async flip\n",
6236 				    plane->base.base.id, plane->base.name);
6237 			return -EINVAL;
6238 		}
6239 
6240 		if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
6241 			drm_dbg_kms(&i915->drm,
6242 				    "[PLANE:%d:%s] Color encoding cannot be changed in async flip\n",
6243 				    plane->base.base.id, plane->base.name);
6244 			return -EINVAL;
6245 		}
6246 
6247 		if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
6248 			drm_dbg_kms(&i915->drm,
6249 				    "[PLANE:%d:%s] Color range cannot be changed in async flip\n",
6250 				    plane->base.base.id, plane->base.name);
6251 			return -EINVAL;
6252 		}
6253 
6254 		/* plane decryption is allow to change only in synchronous flips */
6255 		if (old_plane_state->decrypt != new_plane_state->decrypt) {
6256 			drm_dbg_kms(&i915->drm,
6257 				    "[PLANE:%d:%s] Decryption cannot be changed in async flip\n",
6258 				    plane->base.base.id, plane->base.name);
6259 			return -EINVAL;
6260 		}
6261 	}
6262 
6263 	return 0;
6264 }
6265 
6266 static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
6267 {
6268 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6269 	struct intel_crtc_state *crtc_state;
6270 	struct intel_crtc *crtc;
6271 	u8 affected_pipes = 0;
6272 	u8 modeset_pipes = 0;
6273 	int i;
6274 
6275 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6276 		affected_pipes |= crtc_state->bigjoiner_pipes;
6277 		if (intel_crtc_needs_modeset(crtc_state))
6278 			modeset_pipes |= crtc_state->bigjoiner_pipes;
6279 	}
6280 
6281 	for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc, affected_pipes) {
6282 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6283 		if (IS_ERR(crtc_state))
6284 			return PTR_ERR(crtc_state);
6285 	}
6286 
6287 	for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc, modeset_pipes) {
6288 		int ret;
6289 
6290 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6291 
6292 		crtc_state->uapi.mode_changed = true;
6293 
6294 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
6295 		if (ret)
6296 			return ret;
6297 
6298 		ret = intel_atomic_add_affected_planes(state, crtc);
6299 		if (ret)
6300 			return ret;
6301 	}
6302 
6303 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6304 		/* Kill old bigjoiner link, we may re-establish afterwards */
6305 		if (intel_crtc_needs_modeset(crtc_state) &&
6306 		    intel_crtc_is_bigjoiner_master(crtc_state))
6307 			kill_bigjoiner_slave(state, crtc);
6308 	}
6309 
6310 	return 0;
6311 }
6312 
6313 static int intel_atomic_check_config(struct intel_atomic_state *state,
6314 				     struct intel_link_bw_limits *limits,
6315 				     enum pipe *failed_pipe)
6316 {
6317 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6318 	struct intel_crtc_state *new_crtc_state;
6319 	struct intel_crtc *crtc;
6320 	int ret;
6321 	int i;
6322 
6323 	*failed_pipe = INVALID_PIPE;
6324 
6325 	ret = intel_bigjoiner_add_affected_crtcs(state);
6326 	if (ret)
6327 		return ret;
6328 
6329 	ret = intel_fdi_add_affected_crtcs(state);
6330 	if (ret)
6331 		return ret;
6332 
6333 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6334 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
6335 			if (intel_crtc_is_bigjoiner_slave(new_crtc_state))
6336 				copy_bigjoiner_crtc_state_nomodeset(state, crtc);
6337 			else
6338 				intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc);
6339 			continue;
6340 		}
6341 
6342 		if (drm_WARN_ON(&i915->drm, intel_crtc_is_bigjoiner_slave(new_crtc_state)))
6343 			continue;
6344 
6345 		ret = intel_crtc_prepare_cleared_state(state, crtc);
6346 		if (ret)
6347 			goto fail;
6348 
6349 		if (!new_crtc_state->hw.enable)
6350 			continue;
6351 
6352 		ret = intel_modeset_pipe_config(state, crtc, limits);
6353 		if (ret)
6354 			goto fail;
6355 	}
6356 
6357 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6358 		if (!intel_crtc_needs_modeset(new_crtc_state))
6359 			continue;
6360 
6361 		if (drm_WARN_ON(&i915->drm, intel_crtc_is_bigjoiner_slave(new_crtc_state)))
6362 			continue;
6363 
6364 		if (!new_crtc_state->hw.enable)
6365 			continue;
6366 
6367 		ret = intel_modeset_pipe_config_late(state, crtc);
6368 		if (ret)
6369 			goto fail;
6370 	}
6371 
6372 fail:
6373 	if (ret)
6374 		*failed_pipe = crtc->pipe;
6375 
6376 	return ret;
6377 }
6378 
6379 static int intel_atomic_check_config_and_link(struct intel_atomic_state *state)
6380 {
6381 	struct intel_link_bw_limits new_limits;
6382 	struct intel_link_bw_limits old_limits;
6383 	int ret;
6384 
6385 	intel_link_bw_init_limits(state, &new_limits);
6386 	old_limits = new_limits;
6387 
6388 	while (true) {
6389 		enum pipe failed_pipe;
6390 
6391 		ret = intel_atomic_check_config(state, &new_limits,
6392 						&failed_pipe);
6393 		if (ret) {
6394 			/*
6395 			 * The bpp limit for a pipe is below the minimum it supports, set the
6396 			 * limit to the minimum and recalculate the config.
6397 			 */
6398 			if (ret == -EINVAL &&
6399 			    intel_link_bw_set_bpp_limit_for_pipe(state,
6400 								 &old_limits,
6401 								 &new_limits,
6402 								 failed_pipe))
6403 				continue;
6404 
6405 			break;
6406 		}
6407 
6408 		old_limits = new_limits;
6409 
6410 		ret = intel_link_bw_atomic_check(state, &new_limits);
6411 		if (ret != -EAGAIN)
6412 			break;
6413 	}
6414 
6415 	return ret;
6416 }
6417 /**
6418  * intel_atomic_check - validate state object
6419  * @dev: drm device
6420  * @_state: state to validate
6421  */
6422 int intel_atomic_check(struct drm_device *dev,
6423 		       struct drm_atomic_state *_state)
6424 {
6425 	struct drm_i915_private *dev_priv = to_i915(dev);
6426 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
6427 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
6428 	struct intel_crtc *crtc;
6429 	int ret, i;
6430 	bool any_ms = false;
6431 
6432 	if (!intel_display_driver_check_access(dev_priv))
6433 		return -ENODEV;
6434 
6435 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6436 					    new_crtc_state, i) {
6437 		/*
6438 		 * crtc's state no longer considered to be inherited
6439 		 * after the first userspace/client initiated commit.
6440 		 */
6441 		if (!state->internal)
6442 			new_crtc_state->inherited = false;
6443 
6444 		if (new_crtc_state->inherited != old_crtc_state->inherited)
6445 			new_crtc_state->uapi.mode_changed = true;
6446 
6447 		if (new_crtc_state->uapi.scaling_filter !=
6448 		    old_crtc_state->uapi.scaling_filter)
6449 			new_crtc_state->uapi.mode_changed = true;
6450 	}
6451 
6452 	intel_vrr_check_modeset(state);
6453 
6454 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
6455 	if (ret)
6456 		goto fail;
6457 
6458 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6459 		ret = intel_async_flip_check_uapi(state, crtc);
6460 		if (ret)
6461 			return ret;
6462 	}
6463 
6464 	ret = intel_atomic_check_config_and_link(state);
6465 	if (ret)
6466 		goto fail;
6467 
6468 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6469 		if (!intel_crtc_needs_modeset(new_crtc_state))
6470 			continue;
6471 
6472 		if (intel_crtc_is_bigjoiner_slave(new_crtc_state)) {
6473 			drm_WARN_ON(&dev_priv->drm, new_crtc_state->uapi.enable);
6474 			continue;
6475 		}
6476 
6477 		ret = intel_atomic_check_bigjoiner(state, crtc);
6478 		if (ret)
6479 			goto fail;
6480 	}
6481 
6482 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6483 					    new_crtc_state, i) {
6484 		if (!intel_crtc_needs_modeset(new_crtc_state))
6485 			continue;
6486 
6487 		intel_bigjoiner_adjust_pipe_src(new_crtc_state);
6488 
6489 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
6490 	}
6491 
6492 	/**
6493 	 * Check if fastset is allowed by external dependencies like other
6494 	 * pipes and transcoders.
6495 	 *
6496 	 * Right now it only forces a fullmodeset when the MST master
6497 	 * transcoder did not changed but the pipe of the master transcoder
6498 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
6499 	 * in case of port synced crtcs, if one of the synced crtcs
6500 	 * needs a full modeset, all other synced crtcs should be
6501 	 * forced a full modeset.
6502 	 */
6503 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6504 		if (!new_crtc_state->hw.enable || intel_crtc_needs_modeset(new_crtc_state))
6505 			continue;
6506 
6507 		if (intel_dp_mst_crtc_needs_modeset(state, crtc))
6508 			intel_crtc_flag_modeset(new_crtc_state);
6509 
6510 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
6511 			enum transcoder master = new_crtc_state->mst_master_transcoder;
6512 
6513 			if (intel_cpu_transcoders_need_modeset(state, BIT(master)))
6514 				intel_crtc_flag_modeset(new_crtc_state);
6515 		}
6516 
6517 		if (is_trans_port_sync_mode(new_crtc_state)) {
6518 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
6519 
6520 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
6521 				trans |= BIT(new_crtc_state->master_transcoder);
6522 
6523 			if (intel_cpu_transcoders_need_modeset(state, trans))
6524 				intel_crtc_flag_modeset(new_crtc_state);
6525 		}
6526 
6527 		if (new_crtc_state->bigjoiner_pipes) {
6528 			if (intel_pipes_need_modeset(state, new_crtc_state->bigjoiner_pipes))
6529 				intel_crtc_flag_modeset(new_crtc_state);
6530 		}
6531 	}
6532 
6533 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6534 					    new_crtc_state, i) {
6535 		if (!intel_crtc_needs_modeset(new_crtc_state))
6536 			continue;
6537 
6538 		any_ms = true;
6539 
6540 		intel_release_shared_dplls(state, crtc);
6541 	}
6542 
6543 	if (any_ms && !check_digital_port_conflicts(state)) {
6544 		drm_dbg_kms(&dev_priv->drm,
6545 			    "rejecting conflicting digital port configuration\n");
6546 		ret = -EINVAL;
6547 		goto fail;
6548 	}
6549 
6550 	ret = intel_atomic_check_planes(state);
6551 	if (ret)
6552 		goto fail;
6553 
6554 	ret = intel_compute_global_watermarks(state);
6555 	if (ret)
6556 		goto fail;
6557 
6558 	ret = intel_bw_atomic_check(state);
6559 	if (ret)
6560 		goto fail;
6561 
6562 	ret = intel_cdclk_atomic_check(state, &any_ms);
6563 	if (ret)
6564 		goto fail;
6565 
6566 	if (intel_any_crtc_needs_modeset(state))
6567 		any_ms = true;
6568 
6569 	if (any_ms) {
6570 		ret = intel_modeset_checks(state);
6571 		if (ret)
6572 			goto fail;
6573 
6574 		ret = intel_modeset_calc_cdclk(state);
6575 		if (ret)
6576 			return ret;
6577 	}
6578 
6579 	ret = intel_pmdemand_atomic_check(state);
6580 	if (ret)
6581 		goto fail;
6582 
6583 	ret = intel_atomic_check_crtcs(state);
6584 	if (ret)
6585 		goto fail;
6586 
6587 	ret = intel_fbc_atomic_check(state);
6588 	if (ret)
6589 		goto fail;
6590 
6591 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6592 					    new_crtc_state, i) {
6593 		intel_color_assert_luts(new_crtc_state);
6594 
6595 		ret = intel_async_flip_check_hw(state, crtc);
6596 		if (ret)
6597 			goto fail;
6598 
6599 		/* Either full modeset or fastset (or neither), never both */
6600 		drm_WARN_ON(&dev_priv->drm,
6601 			    intel_crtc_needs_modeset(new_crtc_state) &&
6602 			    intel_crtc_needs_fastset(new_crtc_state));
6603 
6604 		if (!intel_crtc_needs_modeset(new_crtc_state) &&
6605 		    !intel_crtc_needs_fastset(new_crtc_state))
6606 			continue;
6607 
6608 		intel_crtc_state_dump(new_crtc_state, state,
6609 				      intel_crtc_needs_modeset(new_crtc_state) ?
6610 				      "modeset" : "fastset");
6611 	}
6612 
6613 	return 0;
6614 
6615  fail:
6616 	if (ret == -EDEADLK)
6617 		return ret;
6618 
6619 	/*
6620 	 * FIXME would probably be nice to know which crtc specifically
6621 	 * caused the failure, in cases where we can pinpoint it.
6622 	 */
6623 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6624 					    new_crtc_state, i)
6625 		intel_crtc_state_dump(new_crtc_state, state, "failed");
6626 
6627 	return ret;
6628 }
6629 
6630 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
6631 {
6632 	struct intel_crtc_state *crtc_state;
6633 	struct intel_crtc *crtc;
6634 	int i, ret;
6635 
6636 	ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
6637 	if (ret < 0)
6638 		return ret;
6639 
6640 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6641 		if (intel_crtc_needs_color_update(crtc_state))
6642 			intel_color_prepare_commit(crtc_state);
6643 	}
6644 
6645 	return 0;
6646 }
6647 
6648 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
6649 				  struct intel_crtc_state *crtc_state)
6650 {
6651 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6652 
6653 	if (DISPLAY_VER(dev_priv) != 2 || crtc_state->active_planes)
6654 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
6655 
6656 	if (crtc_state->has_pch_encoder) {
6657 		enum pipe pch_transcoder =
6658 			intel_crtc_pch_transcoder(crtc);
6659 
6660 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
6661 	}
6662 }
6663 
6664 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
6665 			       const struct intel_crtc_state *new_crtc_state)
6666 {
6667 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6668 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6669 
6670 	/*
6671 	 * Update pipe size and adjust fitter if needed: the reason for this is
6672 	 * that in compute_mode_changes we check the native mode (not the pfit
6673 	 * mode) to see if we can flip rather than do a full mode set. In the
6674 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
6675 	 * pfit state, we'll end up with a big fb scanned out into the wrong
6676 	 * sized surface.
6677 	 */
6678 	intel_set_pipe_src_size(new_crtc_state);
6679 
6680 	/* on skylake this is done by detaching scalers */
6681 	if (DISPLAY_VER(dev_priv) >= 9) {
6682 		if (new_crtc_state->pch_pfit.enabled)
6683 			skl_pfit_enable(new_crtc_state);
6684 	} else if (HAS_PCH_SPLIT(dev_priv)) {
6685 		if (new_crtc_state->pch_pfit.enabled)
6686 			ilk_pfit_enable(new_crtc_state);
6687 		else if (old_crtc_state->pch_pfit.enabled)
6688 			ilk_pfit_disable(old_crtc_state);
6689 	}
6690 
6691 	/*
6692 	 * The register is supposedly single buffered so perhaps
6693 	 * not 100% correct to do this here. But SKL+ calculate
6694 	 * this based on the adjust pixel rate so pfit changes do
6695 	 * affect it and so it must be updated for fastsets.
6696 	 * HSW/BDW only really need this here for fastboot, after
6697 	 * that the value should not change without a full modeset.
6698 	 */
6699 	if (DISPLAY_VER(dev_priv) >= 9 ||
6700 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
6701 		hsw_set_linetime_wm(new_crtc_state);
6702 
6703 	if (new_crtc_state->update_m_n)
6704 		intel_cpu_transcoder_set_m1_n1(crtc, new_crtc_state->cpu_transcoder,
6705 					       &new_crtc_state->dp_m_n);
6706 
6707 	if (new_crtc_state->update_lrr)
6708 		intel_set_transcoder_timings_lrr(new_crtc_state);
6709 }
6710 
6711 static void commit_pipe_pre_planes(struct intel_atomic_state *state,
6712 				   struct intel_crtc *crtc)
6713 {
6714 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6715 	const struct intel_crtc_state *old_crtc_state =
6716 		intel_atomic_get_old_crtc_state(state, crtc);
6717 	const struct intel_crtc_state *new_crtc_state =
6718 		intel_atomic_get_new_crtc_state(state, crtc);
6719 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
6720 
6721 	/*
6722 	 * During modesets pipe configuration was programmed as the
6723 	 * CRTC was enabled.
6724 	 */
6725 	if (!modeset) {
6726 		if (intel_crtc_needs_color_update(new_crtc_state))
6727 			intel_color_commit_arm(new_crtc_state);
6728 
6729 		if (DISPLAY_VER(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
6730 			bdw_set_pipe_misc(new_crtc_state);
6731 
6732 		if (intel_crtc_needs_fastset(new_crtc_state))
6733 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
6734 	}
6735 
6736 	intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
6737 
6738 	intel_atomic_update_watermarks(state, crtc);
6739 }
6740 
6741 static void commit_pipe_post_planes(struct intel_atomic_state *state,
6742 				    struct intel_crtc *crtc)
6743 {
6744 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6745 	const struct intel_crtc_state *old_crtc_state =
6746 		intel_atomic_get_old_crtc_state(state, crtc);
6747 	const struct intel_crtc_state *new_crtc_state =
6748 		intel_atomic_get_new_crtc_state(state, crtc);
6749 
6750 	/*
6751 	 * Disable the scaler(s) after the plane(s) so that we don't
6752 	 * get a catastrophic underrun even if the two operations
6753 	 * end up happening in two different frames.
6754 	 */
6755 	if (DISPLAY_VER(dev_priv) >= 9 &&
6756 	    !intel_crtc_needs_modeset(new_crtc_state))
6757 		skl_detach_scalers(new_crtc_state);
6758 
6759 	if (vrr_enabling(old_crtc_state, new_crtc_state))
6760 		intel_vrr_enable(new_crtc_state);
6761 }
6762 
6763 static void intel_enable_crtc(struct intel_atomic_state *state,
6764 			      struct intel_crtc *crtc)
6765 {
6766 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6767 	const struct intel_crtc_state *new_crtc_state =
6768 		intel_atomic_get_new_crtc_state(state, crtc);
6769 	struct intel_crtc *pipe_crtc;
6770 
6771 	if (!intel_crtc_needs_modeset(new_crtc_state))
6772 		return;
6773 
6774 	for_each_intel_crtc_in_pipe_mask_reverse(&dev_priv->drm, pipe_crtc,
6775 						 intel_crtc_joined_pipe_mask(new_crtc_state)) {
6776 		const struct intel_crtc_state *pipe_crtc_state =
6777 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
6778 
6779 		/* VRR will be enable later, if required */
6780 		intel_crtc_update_active_timings(pipe_crtc_state, false);
6781 	}
6782 
6783 	dev_priv->display.funcs.display->crtc_enable(state, crtc);
6784 
6785 	/* vblanks work again, re-enable pipe CRC. */
6786 	intel_crtc_enable_pipe_crc(crtc);
6787 }
6788 
6789 static void intel_pre_update_crtc(struct intel_atomic_state *state,
6790 				  struct intel_crtc *crtc)
6791 {
6792 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6793 	const struct intel_crtc_state *old_crtc_state =
6794 		intel_atomic_get_old_crtc_state(state, crtc);
6795 	struct intel_crtc_state *new_crtc_state =
6796 		intel_atomic_get_new_crtc_state(state, crtc);
6797 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
6798 
6799 	if (old_crtc_state->inherited ||
6800 	    intel_crtc_needs_modeset(new_crtc_state)) {
6801 		if (HAS_DPT(i915))
6802 			intel_dpt_configure(crtc);
6803 	}
6804 
6805 	if (!modeset) {
6806 		if (new_crtc_state->preload_luts &&
6807 		    intel_crtc_needs_color_update(new_crtc_state))
6808 			intel_color_load_luts(new_crtc_state);
6809 
6810 		intel_pre_plane_update(state, crtc);
6811 
6812 		if (intel_crtc_needs_fastset(new_crtc_state))
6813 			intel_encoders_update_pipe(state, crtc);
6814 
6815 		if (DISPLAY_VER(i915) >= 11 &&
6816 		    intel_crtc_needs_fastset(new_crtc_state))
6817 			icl_set_pipe_chicken(new_crtc_state);
6818 
6819 		if (vrr_params_changed(old_crtc_state, new_crtc_state))
6820 			intel_vrr_set_transcoder_timings(new_crtc_state);
6821 	}
6822 
6823 	intel_fbc_update(state, crtc);
6824 
6825 	drm_WARN_ON(&i915->drm, !intel_display_power_is_enabled(i915, POWER_DOMAIN_DC_OFF));
6826 
6827 	if (!modeset &&
6828 	    intel_crtc_needs_color_update(new_crtc_state))
6829 		intel_color_commit_noarm(new_crtc_state);
6830 
6831 	intel_crtc_planes_update_noarm(state, crtc);
6832 }
6833 
6834 static void intel_update_crtc(struct intel_atomic_state *state,
6835 			      struct intel_crtc *crtc)
6836 {
6837 	const struct intel_crtc_state *old_crtc_state =
6838 		intel_atomic_get_old_crtc_state(state, crtc);
6839 	struct intel_crtc_state *new_crtc_state =
6840 		intel_atomic_get_new_crtc_state(state, crtc);
6841 
6842 	/* Perform vblank evasion around commit operation */
6843 	intel_pipe_update_start(state, crtc);
6844 
6845 	commit_pipe_pre_planes(state, crtc);
6846 
6847 	intel_crtc_planes_update_arm(state, crtc);
6848 
6849 	commit_pipe_post_planes(state, crtc);
6850 
6851 	intel_pipe_update_end(state, crtc);
6852 
6853 	/*
6854 	 * VRR/Seamless M/N update may need to update frame timings.
6855 	 *
6856 	 * FIXME Should be synchronized with the start of vblank somehow...
6857 	 */
6858 	if (vrr_enabling(old_crtc_state, new_crtc_state) ||
6859 	    new_crtc_state->update_m_n || new_crtc_state->update_lrr)
6860 		intel_crtc_update_active_timings(new_crtc_state,
6861 						 new_crtc_state->vrr.enable);
6862 
6863 	/*
6864 	 * We usually enable FIFO underrun interrupts as part of the
6865 	 * CRTC enable sequence during modesets.  But when we inherit a
6866 	 * valid pipe configuration from the BIOS we need to take care
6867 	 * of enabling them on the CRTC's first fastset.
6868 	 */
6869 	if (intel_crtc_needs_fastset(new_crtc_state) &&
6870 	    old_crtc_state->inherited)
6871 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
6872 }
6873 
6874 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
6875 					  struct intel_crtc *crtc)
6876 {
6877 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6878 	const struct intel_crtc_state *old_crtc_state =
6879 		intel_atomic_get_old_crtc_state(state, crtc);
6880 	struct intel_crtc *pipe_crtc;
6881 
6882 	/*
6883 	 * We need to disable pipe CRC before disabling the pipe,
6884 	 * or we race against vblank off.
6885 	 */
6886 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, pipe_crtc,
6887 					 intel_crtc_joined_pipe_mask(old_crtc_state))
6888 		intel_crtc_disable_pipe_crc(pipe_crtc);
6889 
6890 	dev_priv->display.funcs.display->crtc_disable(state, crtc);
6891 
6892 	for_each_intel_crtc_in_pipe_mask(&dev_priv->drm, pipe_crtc,
6893 					 intel_crtc_joined_pipe_mask(old_crtc_state)) {
6894 		const struct intel_crtc_state *new_pipe_crtc_state =
6895 			intel_atomic_get_new_crtc_state(state, pipe_crtc);
6896 
6897 		pipe_crtc->active = false;
6898 		intel_fbc_disable(pipe_crtc);
6899 
6900 		if (!new_pipe_crtc_state->hw.active)
6901 			intel_initial_watermarks(state, pipe_crtc);
6902 	}
6903 }
6904 
6905 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
6906 {
6907 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6908 	const struct intel_crtc_state *new_crtc_state, *old_crtc_state;
6909 	struct intel_crtc *crtc;
6910 	u8 disable_pipes = 0;
6911 	int i;
6912 
6913 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6914 					    new_crtc_state, i) {
6915 		if (!intel_crtc_needs_modeset(new_crtc_state))
6916 			continue;
6917 
6918 		/*
6919 		 * Needs to be done even for pipes
6920 		 * that weren't enabled previously.
6921 		 */
6922 		intel_pre_plane_update(state, crtc);
6923 
6924 		if (!old_crtc_state->hw.active)
6925 			continue;
6926 
6927 		disable_pipes |= BIT(crtc->pipe);
6928 	}
6929 
6930 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i) {
6931 		if ((disable_pipes & BIT(crtc->pipe)) == 0)
6932 			continue;
6933 
6934 		intel_crtc_disable_planes(state, crtc);
6935 	}
6936 
6937 	/* Only disable port sync and MST slaves */
6938 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i) {
6939 		if ((disable_pipes & BIT(crtc->pipe)) == 0)
6940 			continue;
6941 
6942 		if (intel_crtc_is_bigjoiner_slave(old_crtc_state))
6943 			continue;
6944 
6945 		/* In case of Transcoder port Sync master slave CRTCs can be
6946 		 * assigned in any order and we need to make sure that
6947 		 * slave CRTCs are disabled first and then master CRTC since
6948 		 * Slave vblanks are masked till Master Vblanks.
6949 		 */
6950 		if (!is_trans_port_sync_slave(old_crtc_state) &&
6951 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
6952 			continue;
6953 
6954 		intel_old_crtc_state_disables(state, crtc);
6955 
6956 		disable_pipes &= ~intel_crtc_joined_pipe_mask(old_crtc_state);
6957 	}
6958 
6959 	/* Disable everything else left on */
6960 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i) {
6961 		if ((disable_pipes & BIT(crtc->pipe)) == 0)
6962 			continue;
6963 
6964 		if (intel_crtc_is_bigjoiner_slave(old_crtc_state))
6965 			continue;
6966 
6967 		intel_old_crtc_state_disables(state, crtc);
6968 
6969 		disable_pipes &= ~intel_crtc_joined_pipe_mask(old_crtc_state);
6970 	}
6971 
6972 	drm_WARN_ON(&i915->drm, disable_pipes);
6973 }
6974 
6975 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
6976 {
6977 	struct intel_crtc_state *new_crtc_state;
6978 	struct intel_crtc *crtc;
6979 	int i;
6980 
6981 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6982 		if (!new_crtc_state->hw.active)
6983 			continue;
6984 
6985 		intel_enable_crtc(state, crtc);
6986 		intel_pre_update_crtc(state, crtc);
6987 	}
6988 
6989 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6990 		if (!new_crtc_state->hw.active)
6991 			continue;
6992 
6993 		intel_update_crtc(state, crtc);
6994 	}
6995 }
6996 
6997 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
6998 {
6999 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7000 	struct intel_crtc *crtc;
7001 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
7002 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
7003 	u8 update_pipes = 0, modeset_pipes = 0;
7004 	int i;
7005 
7006 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7007 		enum pipe pipe = crtc->pipe;
7008 
7009 		if (!new_crtc_state->hw.active)
7010 			continue;
7011 
7012 		/* ignore allocations for crtc's that have been turned off. */
7013 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
7014 			entries[pipe] = old_crtc_state->wm.skl.ddb;
7015 			update_pipes |= BIT(pipe);
7016 		} else {
7017 			modeset_pipes |= BIT(pipe);
7018 		}
7019 	}
7020 
7021 	/*
7022 	 * Whenever the number of active pipes changes, we need to make sure we
7023 	 * update the pipes in the right order so that their ddb allocations
7024 	 * never overlap with each other between CRTC updates. Otherwise we'll
7025 	 * cause pipe underruns and other bad stuff.
7026 	 *
7027 	 * So first lets enable all pipes that do not need a fullmodeset as
7028 	 * those don't have any external dependency.
7029 	 */
7030 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7031 		enum pipe pipe = crtc->pipe;
7032 
7033 		if ((update_pipes & BIT(pipe)) == 0)
7034 			continue;
7035 
7036 		intel_pre_update_crtc(state, crtc);
7037 	}
7038 
7039 	intel_dbuf_mbus_pre_ddb_update(state);
7040 
7041 	while (update_pipes) {
7042 		/*
7043 		 * Commit in reverse order to make bigjoiner master
7044 		 * send the uapi events after slaves are done.
7045 		 */
7046 		for_each_oldnew_intel_crtc_in_state_reverse(state, crtc, old_crtc_state,
7047 							    new_crtc_state, i) {
7048 			enum pipe pipe = crtc->pipe;
7049 
7050 			if ((update_pipes & BIT(pipe)) == 0)
7051 				continue;
7052 
7053 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7054 							entries, I915_MAX_PIPES, pipe))
7055 				continue;
7056 
7057 			entries[pipe] = new_crtc_state->wm.skl.ddb;
7058 			update_pipes &= ~BIT(pipe);
7059 
7060 			intel_update_crtc(state, crtc);
7061 
7062 			/*
7063 			 * If this is an already active pipe, it's DDB changed,
7064 			 * and this isn't the last pipe that needs updating
7065 			 * then we need to wait for a vblank to pass for the
7066 			 * new ddb allocation to take effect.
7067 			 */
7068 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
7069 						 &old_crtc_state->wm.skl.ddb) &&
7070 			    (update_pipes | modeset_pipes))
7071 				intel_crtc_wait_for_next_vblank(crtc);
7072 		}
7073 	}
7074 
7075 	intel_dbuf_mbus_post_ddb_update(state);
7076 
7077 	update_pipes = modeset_pipes;
7078 
7079 	/*
7080 	 * Enable all pipes that needs a modeset and do not depends on other
7081 	 * pipes
7082 	 */
7083 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7084 		enum pipe pipe = crtc->pipe;
7085 
7086 		if ((modeset_pipes & BIT(pipe)) == 0)
7087 			continue;
7088 
7089 		if (intel_crtc_is_bigjoiner_slave(new_crtc_state))
7090 			continue;
7091 
7092 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
7093 		    is_trans_port_sync_master(new_crtc_state))
7094 			continue;
7095 
7096 		modeset_pipes &= ~intel_crtc_joined_pipe_mask(new_crtc_state);
7097 
7098 		intel_enable_crtc(state, crtc);
7099 	}
7100 
7101 	/*
7102 	 * Then we enable all remaining pipes that depend on other
7103 	 * pipes: MST slaves and port sync masters, big joiner master
7104 	 */
7105 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7106 		enum pipe pipe = crtc->pipe;
7107 
7108 		if ((modeset_pipes & BIT(pipe)) == 0)
7109 			continue;
7110 
7111 		if (intel_crtc_is_bigjoiner_slave(new_crtc_state))
7112 			continue;
7113 
7114 		modeset_pipes &= ~intel_crtc_joined_pipe_mask(new_crtc_state);
7115 
7116 		intel_enable_crtc(state, crtc);
7117 	}
7118 
7119 	/*
7120 	 * Finally we do the plane updates/etc. for all pipes that got enabled.
7121 	 */
7122 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7123 		enum pipe pipe = crtc->pipe;
7124 
7125 		if ((update_pipes & BIT(pipe)) == 0)
7126 			continue;
7127 
7128 		intel_pre_update_crtc(state, crtc);
7129 	}
7130 
7131 	/*
7132 	 * Commit in reverse order to make bigjoiner master
7133 	 * send the uapi events after slaves are done.
7134 	 */
7135 	for_each_new_intel_crtc_in_state_reverse(state, crtc, new_crtc_state, i) {
7136 		enum pipe pipe = crtc->pipe;
7137 
7138 		if ((update_pipes & BIT(pipe)) == 0)
7139 			continue;
7140 
7141 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7142 									entries, I915_MAX_PIPES, pipe));
7143 
7144 		entries[pipe] = new_crtc_state->wm.skl.ddb;
7145 		update_pipes &= ~BIT(pipe);
7146 
7147 		intel_update_crtc(state, crtc);
7148 	}
7149 
7150 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
7151 	drm_WARN_ON(&dev_priv->drm, update_pipes);
7152 }
7153 
7154 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
7155 {
7156 	struct drm_i915_private *i915 = to_i915(intel_state->base.dev);
7157 	struct drm_plane *plane;
7158 	struct drm_plane_state *new_plane_state;
7159 	int ret, i;
7160 
7161 	for_each_new_plane_in_state(&intel_state->base, plane, new_plane_state, i) {
7162 		if (new_plane_state->fence) {
7163 			ret = dma_fence_wait_timeout(new_plane_state->fence, false,
7164 						     i915_fence_timeout(i915));
7165 			if (ret <= 0)
7166 				break;
7167 
7168 			dma_fence_put(new_plane_state->fence);
7169 			new_plane_state->fence = NULL;
7170 		}
7171 	}
7172 }
7173 
7174 static void intel_atomic_cleanup_work(struct work_struct *work)
7175 {
7176 	struct intel_atomic_state *state =
7177 		container_of(work, struct intel_atomic_state, base.commit_work);
7178 	struct drm_i915_private *i915 = to_i915(state->base.dev);
7179 	struct intel_crtc_state *old_crtc_state;
7180 	struct intel_crtc *crtc;
7181 	int i;
7182 
7183 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i)
7184 		intel_color_cleanup_commit(old_crtc_state);
7185 
7186 	drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
7187 	drm_atomic_helper_commit_cleanup_done(&state->base);
7188 	drm_atomic_state_put(&state->base);
7189 }
7190 
7191 static void intel_atomic_prepare_plane_clear_colors(struct intel_atomic_state *state)
7192 {
7193 	struct drm_i915_private *i915 = to_i915(state->base.dev);
7194 	struct intel_plane *plane;
7195 	struct intel_plane_state *plane_state;
7196 	int i;
7197 
7198 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7199 		struct drm_framebuffer *fb = plane_state->hw.fb;
7200 		int cc_plane;
7201 		int ret;
7202 
7203 		if (!fb)
7204 			continue;
7205 
7206 		cc_plane = intel_fb_rc_ccs_cc_plane(fb);
7207 		if (cc_plane < 0)
7208 			continue;
7209 
7210 		/*
7211 		 * The layout of the fast clear color value expected by HW
7212 		 * (the DRM ABI requiring this value to be located in fb at
7213 		 * offset 0 of cc plane, plane #2 previous generations or
7214 		 * plane #1 for flat ccs):
7215 		 * - 4 x 4 bytes per-channel value
7216 		 *   (in surface type specific float/int format provided by the fb user)
7217 		 * - 8 bytes native color value used by the display
7218 		 *   (converted/written by GPU during a fast clear operation using the
7219 		 *    above per-channel values)
7220 		 *
7221 		 * The commit's FB prepare hook already ensured that FB obj is pinned and the
7222 		 * caller made sure that the object is synced wrt. the related color clear value
7223 		 * GPU write on it.
7224 		 */
7225 		ret = i915_gem_object_read_from_page(intel_fb_obj(fb),
7226 						     fb->offsets[cc_plane] + 16,
7227 						     &plane_state->ccval,
7228 						     sizeof(plane_state->ccval));
7229 		/* The above could only fail if the FB obj has an unexpected backing store type. */
7230 		drm_WARN_ON(&i915->drm, ret);
7231 	}
7232 }
7233 
7234 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
7235 {
7236 	struct drm_device *dev = state->base.dev;
7237 	struct drm_i915_private *dev_priv = to_i915(dev);
7238 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
7239 	struct intel_crtc *crtc;
7240 	struct intel_power_domain_mask put_domains[I915_MAX_PIPES] = {};
7241 	intel_wakeref_t wakeref = 0;
7242 	int i;
7243 
7244 	intel_atomic_commit_fence_wait(state);
7245 
7246 	drm_atomic_helper_wait_for_dependencies(&state->base);
7247 	drm_dp_mst_atomic_wait_for_dependencies(&state->base);
7248 	intel_atomic_global_state_wait_for_dependencies(state);
7249 
7250 	/*
7251 	 * During full modesets we write a lot of registers, wait
7252 	 * for PLLs, etc. Doing that while DC states are enabled
7253 	 * is not a good idea.
7254 	 *
7255 	 * During fastsets and other updates we also need to
7256 	 * disable DC states due to the following scenario:
7257 	 * 1. DC5 exit and PSR exit happen
7258 	 * 2. Some or all _noarm() registers are written
7259 	 * 3. Due to some long delay PSR is re-entered
7260 	 * 4. DC5 entry -> DMC saves the already written new
7261 	 *    _noarm() registers and the old not yet written
7262 	 *    _arm() registers
7263 	 * 5. DC5 exit -> DMC restores a mixture of old and
7264 	 *    new register values and arms the update
7265 	 * 6. PSR exit -> hardware latches a mixture of old and
7266 	 *    new register values -> corrupted frame, or worse
7267 	 * 7. New _arm() registers are finally written
7268 	 * 8. Hardware finally latches a complete set of new
7269 	 *    register values, and subsequent frames will be OK again
7270 	 *
7271 	 * Also note that due to the pipe CSC hardware issues on
7272 	 * SKL/GLK DC states must remain off until the pipe CSC
7273 	 * state readout has happened. Otherwise we risk corrupting
7274 	 * the CSC latched register values with the readout (see
7275 	 * skl_read_csc() and skl_color_commit_noarm()).
7276 	 */
7277 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_DC_OFF);
7278 
7279 	intel_atomic_prepare_plane_clear_colors(state);
7280 
7281 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7282 					    new_crtc_state, i) {
7283 		if (intel_crtc_needs_modeset(new_crtc_state) ||
7284 		    intel_crtc_needs_fastset(new_crtc_state))
7285 			intel_modeset_get_crtc_power_domains(new_crtc_state, &put_domains[crtc->pipe]);
7286 	}
7287 
7288 	intel_commit_modeset_disables(state);
7289 
7290 	intel_dp_tunnel_atomic_alloc_bw(state);
7291 
7292 	/* FIXME: Eventually get rid of our crtc->config pointer */
7293 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7294 		crtc->config = new_crtc_state;
7295 
7296 	/*
7297 	 * In XE_LPD+ Pmdemand combines many parameters such as voltage index,
7298 	 * plls, cdclk frequency, QGV point selection parameter etc. Voltage
7299 	 * index, cdclk/ddiclk frequencies are supposed to be configured before
7300 	 * the cdclk config is set.
7301 	 */
7302 	intel_pmdemand_pre_plane_update(state);
7303 
7304 	if (state->modeset) {
7305 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
7306 
7307 		intel_set_cdclk_pre_plane_update(state);
7308 
7309 		intel_modeset_verify_disabled(state);
7310 	}
7311 
7312 	intel_sagv_pre_plane_update(state);
7313 
7314 	/* Complete the events for pipes that have now been disabled */
7315 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7316 		bool modeset = intel_crtc_needs_modeset(new_crtc_state);
7317 
7318 		/* Complete events for now disable pipes here. */
7319 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
7320 			spin_lock_irq(&dev->event_lock);
7321 			drm_crtc_send_vblank_event(&crtc->base,
7322 						   new_crtc_state->uapi.event);
7323 			spin_unlock_irq(&dev->event_lock);
7324 
7325 			new_crtc_state->uapi.event = NULL;
7326 		}
7327 	}
7328 
7329 	intel_encoders_update_prepare(state);
7330 
7331 	intel_dbuf_pre_plane_update(state);
7332 
7333 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7334 		if (new_crtc_state->do_async_flip)
7335 			intel_crtc_enable_flip_done(state, crtc);
7336 	}
7337 
7338 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
7339 	dev_priv->display.funcs.display->commit_modeset_enables(state);
7340 
7341 	if (state->modeset)
7342 		intel_set_cdclk_post_plane_update(state);
7343 
7344 	intel_wait_for_vblank_workers(state);
7345 
7346 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
7347 	 * already, but still need the state for the delayed optimization. To
7348 	 * fix this:
7349 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
7350 	 * - schedule that vblank worker _before_ calling hw_done
7351 	 * - at the start of commit_tail, cancel it _synchrously
7352 	 * - switch over to the vblank wait helper in the core after that since
7353 	 *   we don't need out special handling any more.
7354 	 */
7355 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
7356 
7357 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7358 		if (new_crtc_state->do_async_flip)
7359 			intel_crtc_disable_flip_done(state, crtc);
7360 
7361 		intel_color_wait_commit(new_crtc_state);
7362 	}
7363 
7364 	/*
7365 	 * Now that the vblank has passed, we can go ahead and program the
7366 	 * optimal watermarks on platforms that need two-step watermark
7367 	 * programming.
7368 	 *
7369 	 * TODO: Move this (and other cleanup) to an async worker eventually.
7370 	 */
7371 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7372 					    new_crtc_state, i) {
7373 		/*
7374 		 * Gen2 reports pipe underruns whenever all planes are disabled.
7375 		 * So re-enable underrun reporting after some planes get enabled.
7376 		 *
7377 		 * We do this before .optimize_watermarks() so that we have a
7378 		 * chance of catching underruns with the intermediate watermarks
7379 		 * vs. the new plane configuration.
7380 		 */
7381 		if (DISPLAY_VER(dev_priv) == 2 && planes_enabling(old_crtc_state, new_crtc_state))
7382 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
7383 
7384 		intel_optimize_watermarks(state, crtc);
7385 	}
7386 
7387 	intel_dbuf_post_plane_update(state);
7388 
7389 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7390 		intel_post_plane_update(state, crtc);
7391 
7392 		intel_modeset_put_crtc_power_domains(crtc, &put_domains[crtc->pipe]);
7393 
7394 		intel_modeset_verify_crtc(state, crtc);
7395 
7396 		/* Must be done after gamma readout due to HSW split gamma vs. IPS w/a */
7397 		hsw_ips_post_update(state, crtc);
7398 
7399 		/*
7400 		 * Activate DRRS after state readout to avoid
7401 		 * dp_m_n vs. dp_m2_n2 confusion on BDW+.
7402 		 */
7403 		intel_drrs_activate(new_crtc_state);
7404 
7405 		/*
7406 		 * DSB cleanup is done in cleanup_work aligning with framebuffer
7407 		 * cleanup. So copy and reset the dsb structure to sync with
7408 		 * commit_done and later do dsb cleanup in cleanup_work.
7409 		 *
7410 		 * FIXME get rid of this funny new->old swapping
7411 		 */
7412 		old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
7413 	}
7414 
7415 	/* Underruns don't always raise interrupts, so check manually */
7416 	intel_check_cpu_fifo_underruns(dev_priv);
7417 	intel_check_pch_fifo_underruns(dev_priv);
7418 
7419 	if (state->modeset)
7420 		intel_verify_planes(state);
7421 
7422 	intel_sagv_post_plane_update(state);
7423 	intel_pmdemand_post_plane_update(state);
7424 
7425 	drm_atomic_helper_commit_hw_done(&state->base);
7426 	intel_atomic_global_state_commit_done(state);
7427 
7428 	if (state->modeset) {
7429 		/* As one of the primary mmio accessors, KMS has a high
7430 		 * likelihood of triggering bugs in unclaimed access. After we
7431 		 * finish modesetting, see if an error has been flagged, and if
7432 		 * so enable debugging for the next modeset - and hope we catch
7433 		 * the culprit.
7434 		 */
7435 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
7436 	}
7437 	/*
7438 	 * Delay re-enabling DC states by 17 ms to avoid the off->on->off
7439 	 * toggling overhead at and above 60 FPS.
7440 	 */
7441 	intel_display_power_put_async_delay(dev_priv, POWER_DOMAIN_DC_OFF, wakeref, 17);
7442 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
7443 
7444 	/*
7445 	 * Defer the cleanup of the old state to a separate worker to not
7446 	 * impede the current task (userspace for blocking modesets) that
7447 	 * are executed inline. For out-of-line asynchronous modesets/flips,
7448 	 * deferring to a new worker seems overkill, but we would place a
7449 	 * schedule point (cond_resched()) here anyway to keep latencies
7450 	 * down.
7451 	 */
7452 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
7453 	queue_work(system_highpri_wq, &state->base.commit_work);
7454 }
7455 
7456 static void intel_atomic_commit_work(struct work_struct *work)
7457 {
7458 	struct intel_atomic_state *state =
7459 		container_of(work, struct intel_atomic_state, base.commit_work);
7460 
7461 	intel_atomic_commit_tail(state);
7462 }
7463 
7464 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
7465 {
7466 	struct intel_plane_state *old_plane_state, *new_plane_state;
7467 	struct intel_plane *plane;
7468 	int i;
7469 
7470 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
7471 					     new_plane_state, i)
7472 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
7473 					to_intel_frontbuffer(new_plane_state->hw.fb),
7474 					plane->frontbuffer_bit);
7475 }
7476 
7477 static int intel_atomic_setup_commit(struct intel_atomic_state *state, bool nonblock)
7478 {
7479 	int ret;
7480 
7481 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
7482 	if (ret)
7483 		return ret;
7484 
7485 	ret = intel_atomic_global_state_setup_commit(state);
7486 	if (ret)
7487 		return ret;
7488 
7489 	return 0;
7490 }
7491 
7492 static int intel_atomic_swap_state(struct intel_atomic_state *state)
7493 {
7494 	int ret;
7495 
7496 	ret = drm_atomic_helper_swap_state(&state->base, true);
7497 	if (ret)
7498 		return ret;
7499 
7500 	intel_atomic_swap_global_state(state);
7501 
7502 	intel_shared_dpll_swap_state(state);
7503 
7504 	intel_atomic_track_fbs(state);
7505 
7506 	return 0;
7507 }
7508 
7509 int intel_atomic_commit(struct drm_device *dev, struct drm_atomic_state *_state,
7510 			bool nonblock)
7511 {
7512 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
7513 	struct drm_i915_private *dev_priv = to_i915(dev);
7514 	int ret = 0;
7515 
7516 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
7517 
7518 	/*
7519 	 * The intel_legacy_cursor_update() fast path takes care
7520 	 * of avoiding the vblank waits for simple cursor
7521 	 * movement and flips. For cursor on/off and size changes,
7522 	 * we want to perform the vblank waits so that watermark
7523 	 * updates happen during the correct frames. Gen9+ have
7524 	 * double buffered watermarks and so shouldn't need this.
7525 	 *
7526 	 * Unset state->legacy_cursor_update before the call to
7527 	 * drm_atomic_helper_setup_commit() because otherwise
7528 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
7529 	 * we get FIFO underruns because we didn't wait
7530 	 * for vblank.
7531 	 *
7532 	 * FIXME doing watermarks and fb cleanup from a vblank worker
7533 	 * (assuming we had any) would solve these problems.
7534 	 */
7535 	if (DISPLAY_VER(dev_priv) < 9 && state->base.legacy_cursor_update) {
7536 		struct intel_crtc_state *new_crtc_state;
7537 		struct intel_crtc *crtc;
7538 		int i;
7539 
7540 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7541 			if (new_crtc_state->wm.need_postvbl_update ||
7542 			    new_crtc_state->update_wm_post)
7543 				state->base.legacy_cursor_update = false;
7544 	}
7545 
7546 	ret = intel_atomic_prepare_commit(state);
7547 	if (ret) {
7548 		drm_dbg_atomic(&dev_priv->drm,
7549 			       "Preparing state failed with %i\n", ret);
7550 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
7551 		return ret;
7552 	}
7553 
7554 	ret = intel_atomic_setup_commit(state, nonblock);
7555 	if (!ret)
7556 		ret = intel_atomic_swap_state(state);
7557 
7558 	if (ret) {
7559 		struct intel_crtc_state *new_crtc_state;
7560 		struct intel_crtc *crtc;
7561 		int i;
7562 
7563 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7564 			intel_color_cleanup_commit(new_crtc_state);
7565 
7566 		drm_atomic_helper_unprepare_planes(dev, &state->base);
7567 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
7568 		return ret;
7569 	}
7570 
7571 	drm_atomic_state_get(&state->base);
7572 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
7573 
7574 	if (nonblock && state->modeset) {
7575 		queue_work(dev_priv->display.wq.modeset, &state->base.commit_work);
7576 	} else if (nonblock) {
7577 		queue_work(dev_priv->display.wq.flip, &state->base.commit_work);
7578 	} else {
7579 		if (state->modeset)
7580 			flush_workqueue(dev_priv->display.wq.modeset);
7581 		intel_atomic_commit_tail(state);
7582 	}
7583 
7584 	return 0;
7585 }
7586 
7587 /**
7588  * intel_plane_destroy - destroy a plane
7589  * @plane: plane to destroy
7590  *
7591  * Common destruction function for all types of planes (primary, cursor,
7592  * sprite).
7593  */
7594 void intel_plane_destroy(struct drm_plane *plane)
7595 {
7596 	drm_plane_cleanup(plane);
7597 	kfree(to_intel_plane(plane));
7598 }
7599 
7600 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
7601 				      struct drm_file *file)
7602 {
7603 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7604 	struct drm_crtc *drmmode_crtc;
7605 	struct intel_crtc *crtc;
7606 
7607 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
7608 	if (!drmmode_crtc)
7609 		return -ENOENT;
7610 
7611 	crtc = to_intel_crtc(drmmode_crtc);
7612 	pipe_from_crtc_id->pipe = crtc->pipe;
7613 
7614 	return 0;
7615 }
7616 
7617 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
7618 {
7619 	struct drm_device *dev = encoder->base.dev;
7620 	struct intel_encoder *source_encoder;
7621 	u32 possible_clones = 0;
7622 
7623 	for_each_intel_encoder(dev, source_encoder) {
7624 		if (encoders_cloneable(encoder, source_encoder))
7625 			possible_clones |= drm_encoder_mask(&source_encoder->base);
7626 	}
7627 
7628 	return possible_clones;
7629 }
7630 
7631 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
7632 {
7633 	struct drm_device *dev = encoder->base.dev;
7634 	struct intel_crtc *crtc;
7635 	u32 possible_crtcs = 0;
7636 
7637 	for_each_intel_crtc_in_pipe_mask(dev, crtc, encoder->pipe_mask)
7638 		possible_crtcs |= drm_crtc_mask(&crtc->base);
7639 
7640 	return possible_crtcs;
7641 }
7642 
7643 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
7644 {
7645 	if (!IS_MOBILE(dev_priv))
7646 		return false;
7647 
7648 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
7649 		return false;
7650 
7651 	if (IS_IRONLAKE(dev_priv) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
7652 		return false;
7653 
7654 	return true;
7655 }
7656 
7657 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
7658 {
7659 	if (DISPLAY_VER(dev_priv) >= 9)
7660 		return false;
7661 
7662 	if (IS_HASWELL_ULT(dev_priv) || IS_BROADWELL_ULT(dev_priv))
7663 		return false;
7664 
7665 	if (HAS_PCH_LPT_H(dev_priv) &&
7666 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
7667 		return false;
7668 
7669 	/* DDI E can't be used if DDI A requires 4 lanes */
7670 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
7671 		return false;
7672 
7673 	if (!dev_priv->display.vbt.int_crt_support)
7674 		return false;
7675 
7676 	return true;
7677 }
7678 
7679 bool assert_port_valid(struct drm_i915_private *i915, enum port port)
7680 {
7681 	return !drm_WARN(&i915->drm, !(DISPLAY_RUNTIME_INFO(i915)->port_mask & BIT(port)),
7682 			 "Platform does not support port %c\n", port_name(port));
7683 }
7684 
7685 void intel_setup_outputs(struct drm_i915_private *dev_priv)
7686 {
7687 	struct intel_encoder *encoder;
7688 	bool dpd_is_edp = false;
7689 
7690 	intel_pps_unlock_regs_wa(dev_priv);
7691 
7692 	if (!HAS_DISPLAY(dev_priv))
7693 		return;
7694 
7695 	if (HAS_DDI(dev_priv)) {
7696 		if (intel_ddi_crt_present(dev_priv))
7697 			intel_crt_init(dev_priv);
7698 
7699 		intel_bios_for_each_encoder(dev_priv, intel_ddi_init);
7700 
7701 		if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv))
7702 			vlv_dsi_init(dev_priv);
7703 	} else if (HAS_PCH_SPLIT(dev_priv)) {
7704 		int found;
7705 
7706 		/*
7707 		 * intel_edp_init_connector() depends on this completing first,
7708 		 * to prevent the registration of both eDP and LVDS and the
7709 		 * incorrect sharing of the PPS.
7710 		 */
7711 		intel_lvds_init(dev_priv);
7712 		intel_crt_init(dev_priv);
7713 
7714 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
7715 
7716 		if (ilk_has_edp_a(dev_priv))
7717 			g4x_dp_init(dev_priv, DP_A, PORT_A);
7718 
7719 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
7720 			/* PCH SDVOB multiplex with HDMIB */
7721 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
7722 			if (!found)
7723 				g4x_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
7724 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
7725 				g4x_dp_init(dev_priv, PCH_DP_B, PORT_B);
7726 		}
7727 
7728 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
7729 			g4x_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
7730 
7731 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
7732 			g4x_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
7733 
7734 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
7735 			g4x_dp_init(dev_priv, PCH_DP_C, PORT_C);
7736 
7737 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
7738 			g4x_dp_init(dev_priv, PCH_DP_D, PORT_D);
7739 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
7740 		bool has_edp, has_port;
7741 
7742 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->display.vbt.int_crt_support)
7743 			intel_crt_init(dev_priv);
7744 
7745 		/*
7746 		 * The DP_DETECTED bit is the latched state of the DDC
7747 		 * SDA pin at boot. However since eDP doesn't require DDC
7748 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
7749 		 * eDP ports may have been muxed to an alternate function.
7750 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
7751 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
7752 		 * detect eDP ports.
7753 		 *
7754 		 * Sadly the straps seem to be missing sometimes even for HDMI
7755 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
7756 		 * and VBT for the presence of the port. Additionally we can't
7757 		 * trust the port type the VBT declares as we've seen at least
7758 		 * HDMI ports that the VBT claim are DP or eDP.
7759 		 */
7760 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
7761 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
7762 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
7763 			has_edp &= g4x_dp_init(dev_priv, VLV_DP_B, PORT_B);
7764 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
7765 			g4x_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
7766 
7767 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
7768 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
7769 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
7770 			has_edp &= g4x_dp_init(dev_priv, VLV_DP_C, PORT_C);
7771 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
7772 			g4x_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
7773 
7774 		if (IS_CHERRYVIEW(dev_priv)) {
7775 			/*
7776 			 * eDP not supported on port D,
7777 			 * so no need to worry about it
7778 			 */
7779 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
7780 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
7781 				g4x_dp_init(dev_priv, CHV_DP_D, PORT_D);
7782 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
7783 				g4x_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
7784 		}
7785 
7786 		vlv_dsi_init(dev_priv);
7787 	} else if (IS_PINEVIEW(dev_priv)) {
7788 		intel_lvds_init(dev_priv);
7789 		intel_crt_init(dev_priv);
7790 	} else if (IS_DISPLAY_VER(dev_priv, 3, 4)) {
7791 		bool found = false;
7792 
7793 		if (IS_MOBILE(dev_priv))
7794 			intel_lvds_init(dev_priv);
7795 
7796 		intel_crt_init(dev_priv);
7797 
7798 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
7799 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
7800 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
7801 			if (!found && IS_G4X(dev_priv)) {
7802 				drm_dbg_kms(&dev_priv->drm,
7803 					    "probing HDMI on SDVOB\n");
7804 				g4x_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
7805 			}
7806 
7807 			if (!found && IS_G4X(dev_priv))
7808 				g4x_dp_init(dev_priv, DP_B, PORT_B);
7809 		}
7810 
7811 		/* Before G4X SDVOC doesn't have its own detect register */
7812 
7813 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
7814 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
7815 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
7816 		}
7817 
7818 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
7819 
7820 			if (IS_G4X(dev_priv)) {
7821 				drm_dbg_kms(&dev_priv->drm,
7822 					    "probing HDMI on SDVOC\n");
7823 				g4x_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
7824 			}
7825 			if (IS_G4X(dev_priv))
7826 				g4x_dp_init(dev_priv, DP_C, PORT_C);
7827 		}
7828 
7829 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
7830 			g4x_dp_init(dev_priv, DP_D, PORT_D);
7831 
7832 		if (SUPPORTS_TV(dev_priv))
7833 			intel_tv_init(dev_priv);
7834 	} else if (DISPLAY_VER(dev_priv) == 2) {
7835 		if (IS_I85X(dev_priv))
7836 			intel_lvds_init(dev_priv);
7837 
7838 		intel_crt_init(dev_priv);
7839 		intel_dvo_init(dev_priv);
7840 	}
7841 
7842 	for_each_intel_encoder(&dev_priv->drm, encoder) {
7843 		encoder->base.possible_crtcs =
7844 			intel_encoder_possible_crtcs(encoder);
7845 		encoder->base.possible_clones =
7846 			intel_encoder_possible_clones(encoder);
7847 	}
7848 
7849 	intel_init_pch_refclk(dev_priv);
7850 
7851 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
7852 }
7853 
7854 static int max_dotclock(struct drm_i915_private *i915)
7855 {
7856 	int max_dotclock = i915->display.cdclk.max_dotclk_freq;
7857 
7858 	/* icl+ might use bigjoiner */
7859 	if (DISPLAY_VER(i915) >= 11)
7860 		max_dotclock *= 2;
7861 
7862 	return max_dotclock;
7863 }
7864 
7865 enum drm_mode_status intel_mode_valid(struct drm_device *dev,
7866 				      const struct drm_display_mode *mode)
7867 {
7868 	struct drm_i915_private *dev_priv = to_i915(dev);
7869 	int hdisplay_max, htotal_max;
7870 	int vdisplay_max, vtotal_max;
7871 
7872 	/*
7873 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
7874 	 * of DBLSCAN modes to the output's mode list when they detect
7875 	 * the scaling mode property on the connector. And they don't
7876 	 * ask the kernel to validate those modes in any way until
7877 	 * modeset time at which point the client gets a protocol error.
7878 	 * So in order to not upset those clients we silently ignore the
7879 	 * DBLSCAN flag on such connectors. For other connectors we will
7880 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
7881 	 * And we always reject DBLSCAN modes in connector->mode_valid()
7882 	 * as we never want such modes on the connector's mode list.
7883 	 */
7884 
7885 	if (mode->vscan > 1)
7886 		return MODE_NO_VSCAN;
7887 
7888 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
7889 		return MODE_H_ILLEGAL;
7890 
7891 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
7892 			   DRM_MODE_FLAG_NCSYNC |
7893 			   DRM_MODE_FLAG_PCSYNC))
7894 		return MODE_HSYNC;
7895 
7896 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
7897 			   DRM_MODE_FLAG_PIXMUX |
7898 			   DRM_MODE_FLAG_CLKDIV2))
7899 		return MODE_BAD;
7900 
7901 	/*
7902 	 * Reject clearly excessive dotclocks early to
7903 	 * avoid having to worry about huge integers later.
7904 	 */
7905 	if (mode->clock > max_dotclock(dev_priv))
7906 		return MODE_CLOCK_HIGH;
7907 
7908 	/* Transcoder timing limits */
7909 	if (DISPLAY_VER(dev_priv) >= 11) {
7910 		hdisplay_max = 16384;
7911 		vdisplay_max = 8192;
7912 		htotal_max = 16384;
7913 		vtotal_max = 8192;
7914 	} else if (DISPLAY_VER(dev_priv) >= 9 ||
7915 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
7916 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
7917 		vdisplay_max = 4096;
7918 		htotal_max = 8192;
7919 		vtotal_max = 8192;
7920 	} else if (DISPLAY_VER(dev_priv) >= 3) {
7921 		hdisplay_max = 4096;
7922 		vdisplay_max = 4096;
7923 		htotal_max = 8192;
7924 		vtotal_max = 8192;
7925 	} else {
7926 		hdisplay_max = 2048;
7927 		vdisplay_max = 2048;
7928 		htotal_max = 4096;
7929 		vtotal_max = 4096;
7930 	}
7931 
7932 	if (mode->hdisplay > hdisplay_max ||
7933 	    mode->hsync_start > htotal_max ||
7934 	    mode->hsync_end > htotal_max ||
7935 	    mode->htotal > htotal_max)
7936 		return MODE_H_ILLEGAL;
7937 
7938 	if (mode->vdisplay > vdisplay_max ||
7939 	    mode->vsync_start > vtotal_max ||
7940 	    mode->vsync_end > vtotal_max ||
7941 	    mode->vtotal > vtotal_max)
7942 		return MODE_V_ILLEGAL;
7943 
7944 	return MODE_OK;
7945 }
7946 
7947 enum drm_mode_status intel_cpu_transcoder_mode_valid(struct drm_i915_private *dev_priv,
7948 						     const struct drm_display_mode *mode)
7949 {
7950 	/*
7951 	 * Additional transcoder timing limits,
7952 	 * excluding BXT/GLK DSI transcoders.
7953 	 */
7954 	if (DISPLAY_VER(dev_priv) >= 5) {
7955 		if (mode->hdisplay < 64 ||
7956 		    mode->htotal - mode->hdisplay < 32)
7957 			return MODE_H_ILLEGAL;
7958 
7959 		if (mode->vtotal - mode->vdisplay < 5)
7960 			return MODE_V_ILLEGAL;
7961 	} else {
7962 		if (mode->htotal - mode->hdisplay < 32)
7963 			return MODE_H_ILLEGAL;
7964 
7965 		if (mode->vtotal - mode->vdisplay < 3)
7966 			return MODE_V_ILLEGAL;
7967 	}
7968 
7969 	/*
7970 	 * Cantiga+ cannot handle modes with a hsync front porch of 0.
7971 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
7972 	 */
7973 	if ((DISPLAY_VER(dev_priv) >= 5 || IS_G4X(dev_priv)) &&
7974 	    mode->hsync_start == mode->hdisplay)
7975 		return MODE_H_ILLEGAL;
7976 
7977 	return MODE_OK;
7978 }
7979 
7980 enum drm_mode_status
7981 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
7982 				const struct drm_display_mode *mode,
7983 				bool bigjoiner)
7984 {
7985 	int plane_width_max, plane_height_max;
7986 
7987 	/*
7988 	 * intel_mode_valid() should be
7989 	 * sufficient on older platforms.
7990 	 */
7991 	if (DISPLAY_VER(dev_priv) < 9)
7992 		return MODE_OK;
7993 
7994 	/*
7995 	 * Most people will probably want a fullscreen
7996 	 * plane so let's not advertize modes that are
7997 	 * too big for that.
7998 	 */
7999 	if (DISPLAY_VER(dev_priv) >= 11) {
8000 		plane_width_max = 5120 << bigjoiner;
8001 		plane_height_max = 4320;
8002 	} else {
8003 		plane_width_max = 5120;
8004 		plane_height_max = 4096;
8005 	}
8006 
8007 	if (mode->hdisplay > plane_width_max)
8008 		return MODE_H_ILLEGAL;
8009 
8010 	if (mode->vdisplay > plane_height_max)
8011 		return MODE_V_ILLEGAL;
8012 
8013 	return MODE_OK;
8014 }
8015 
8016 static const struct intel_display_funcs skl_display_funcs = {
8017 	.get_pipe_config = hsw_get_pipe_config,
8018 	.crtc_enable = hsw_crtc_enable,
8019 	.crtc_disable = hsw_crtc_disable,
8020 	.commit_modeset_enables = skl_commit_modeset_enables,
8021 	.get_initial_plane_config = skl_get_initial_plane_config,
8022 	.fixup_initial_plane_config = skl_fixup_initial_plane_config,
8023 };
8024 
8025 static const struct intel_display_funcs ddi_display_funcs = {
8026 	.get_pipe_config = hsw_get_pipe_config,
8027 	.crtc_enable = hsw_crtc_enable,
8028 	.crtc_disable = hsw_crtc_disable,
8029 	.commit_modeset_enables = intel_commit_modeset_enables,
8030 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8031 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8032 };
8033 
8034 static const struct intel_display_funcs pch_split_display_funcs = {
8035 	.get_pipe_config = ilk_get_pipe_config,
8036 	.crtc_enable = ilk_crtc_enable,
8037 	.crtc_disable = ilk_crtc_disable,
8038 	.commit_modeset_enables = intel_commit_modeset_enables,
8039 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8040 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8041 };
8042 
8043 static const struct intel_display_funcs vlv_display_funcs = {
8044 	.get_pipe_config = i9xx_get_pipe_config,
8045 	.crtc_enable = valleyview_crtc_enable,
8046 	.crtc_disable = i9xx_crtc_disable,
8047 	.commit_modeset_enables = intel_commit_modeset_enables,
8048 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8049 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8050 };
8051 
8052 static const struct intel_display_funcs i9xx_display_funcs = {
8053 	.get_pipe_config = i9xx_get_pipe_config,
8054 	.crtc_enable = i9xx_crtc_enable,
8055 	.crtc_disable = i9xx_crtc_disable,
8056 	.commit_modeset_enables = intel_commit_modeset_enables,
8057 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8058 	.fixup_initial_plane_config = i9xx_fixup_initial_plane_config,
8059 };
8060 
8061 /**
8062  * intel_init_display_hooks - initialize the display modesetting hooks
8063  * @dev_priv: device private
8064  */
8065 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
8066 {
8067 	if (DISPLAY_VER(dev_priv) >= 9) {
8068 		dev_priv->display.funcs.display = &skl_display_funcs;
8069 	} else if (HAS_DDI(dev_priv)) {
8070 		dev_priv->display.funcs.display = &ddi_display_funcs;
8071 	} else if (HAS_PCH_SPLIT(dev_priv)) {
8072 		dev_priv->display.funcs.display = &pch_split_display_funcs;
8073 	} else if (IS_CHERRYVIEW(dev_priv) ||
8074 		   IS_VALLEYVIEW(dev_priv)) {
8075 		dev_priv->display.funcs.display = &vlv_display_funcs;
8076 	} else {
8077 		dev_priv->display.funcs.display = &i9xx_display_funcs;
8078 	}
8079 }
8080 
8081 int intel_initial_commit(struct drm_device *dev)
8082 {
8083 	struct drm_atomic_state *state = NULL;
8084 	struct drm_modeset_acquire_ctx ctx;
8085 	struct intel_crtc *crtc;
8086 	int ret = 0;
8087 
8088 	state = drm_atomic_state_alloc(dev);
8089 	if (!state)
8090 		return -ENOMEM;
8091 
8092 	drm_modeset_acquire_init(&ctx, 0);
8093 
8094 	state->acquire_ctx = &ctx;
8095 	to_intel_atomic_state(state)->internal = true;
8096 
8097 retry:
8098 	for_each_intel_crtc(dev, crtc) {
8099 		struct intel_crtc_state *crtc_state =
8100 			intel_atomic_get_crtc_state(state, crtc);
8101 
8102 		if (IS_ERR(crtc_state)) {
8103 			ret = PTR_ERR(crtc_state);
8104 			goto out;
8105 		}
8106 
8107 		if (crtc_state->hw.active) {
8108 			struct intel_encoder *encoder;
8109 
8110 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
8111 			if (ret)
8112 				goto out;
8113 
8114 			/*
8115 			 * FIXME hack to force a LUT update to avoid the
8116 			 * plane update forcing the pipe gamma on without
8117 			 * having a proper LUT loaded. Remove once we
8118 			 * have readout for pipe gamma enable.
8119 			 */
8120 			crtc_state->uapi.color_mgmt_changed = true;
8121 
8122 			for_each_intel_encoder_mask(dev, encoder,
8123 						    crtc_state->uapi.encoder_mask) {
8124 				if (encoder->initial_fastset_check &&
8125 				    !encoder->initial_fastset_check(encoder, crtc_state)) {
8126 					ret = drm_atomic_add_affected_connectors(state,
8127 										 &crtc->base);
8128 					if (ret)
8129 						goto out;
8130 				}
8131 			}
8132 		}
8133 	}
8134 
8135 	ret = drm_atomic_commit(state);
8136 
8137 out:
8138 	if (ret == -EDEADLK) {
8139 		drm_atomic_state_clear(state);
8140 		drm_modeset_backoff(&ctx);
8141 		goto retry;
8142 	}
8143 
8144 	drm_atomic_state_put(state);
8145 
8146 	drm_modeset_drop_locks(&ctx);
8147 	drm_modeset_acquire_fini(&ctx);
8148 
8149 	return ret;
8150 }
8151 
8152 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
8153 {
8154 	struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv, pipe);
8155 	enum transcoder cpu_transcoder = (enum transcoder)pipe;
8156 	/* 640x480@60Hz, ~25175 kHz */
8157 	struct dpll clock = {
8158 		.m1 = 18,
8159 		.m2 = 7,
8160 		.p1 = 13,
8161 		.p2 = 4,
8162 		.n = 2,
8163 	};
8164 	u32 dpll, fp;
8165 	int i;
8166 
8167 	drm_WARN_ON(&dev_priv->drm,
8168 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
8169 
8170 	drm_dbg_kms(&dev_priv->drm,
8171 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
8172 		    pipe_name(pipe), clock.vco, clock.dot);
8173 
8174 	fp = i9xx_dpll_compute_fp(&clock);
8175 	dpll = DPLL_DVO_2X_MODE |
8176 		DPLL_VGA_MODE_DIS |
8177 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
8178 		PLL_P2_DIVIDE_BY_4 |
8179 		PLL_REF_INPUT_DREFCLK |
8180 		DPLL_VCO_ENABLE;
8181 
8182 	intel_de_write(dev_priv, TRANS_HTOTAL(cpu_transcoder),
8183 		       HACTIVE(640 - 1) | HTOTAL(800 - 1));
8184 	intel_de_write(dev_priv, TRANS_HBLANK(cpu_transcoder),
8185 		       HBLANK_START(640 - 1) | HBLANK_END(800 - 1));
8186 	intel_de_write(dev_priv, TRANS_HSYNC(cpu_transcoder),
8187 		       HSYNC_START(656 - 1) | HSYNC_END(752 - 1));
8188 	intel_de_write(dev_priv, TRANS_VTOTAL(cpu_transcoder),
8189 		       VACTIVE(480 - 1) | VTOTAL(525 - 1));
8190 	intel_de_write(dev_priv, TRANS_VBLANK(cpu_transcoder),
8191 		       VBLANK_START(480 - 1) | VBLANK_END(525 - 1));
8192 	intel_de_write(dev_priv, TRANS_VSYNC(cpu_transcoder),
8193 		       VSYNC_START(490 - 1) | VSYNC_END(492 - 1));
8194 	intel_de_write(dev_priv, PIPESRC(pipe),
8195 		       PIPESRC_WIDTH(640 - 1) | PIPESRC_HEIGHT(480 - 1));
8196 
8197 	intel_de_write(dev_priv, FP0(pipe), fp);
8198 	intel_de_write(dev_priv, FP1(pipe), fp);
8199 
8200 	/*
8201 	 * Apparently we need to have VGA mode enabled prior to changing
8202 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
8203 	 * dividers, even though the register value does change.
8204 	 */
8205 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
8206 	intel_de_write(dev_priv, DPLL(pipe), dpll);
8207 
8208 	/* Wait for the clocks to stabilize. */
8209 	intel_de_posting_read(dev_priv, DPLL(pipe));
8210 	udelay(150);
8211 
8212 	/* The pixel multiplier can only be updated once the
8213 	 * DPLL is enabled and the clocks are stable.
8214 	 *
8215 	 * So write it again.
8216 	 */
8217 	intel_de_write(dev_priv, DPLL(pipe), dpll);
8218 
8219 	/* We do this three times for luck */
8220 	for (i = 0; i < 3 ; i++) {
8221 		intel_de_write(dev_priv, DPLL(pipe), dpll);
8222 		intel_de_posting_read(dev_priv, DPLL(pipe));
8223 		udelay(150); /* wait for warmup */
8224 	}
8225 
8226 	intel_de_write(dev_priv, TRANSCONF(pipe), TRANSCONF_ENABLE);
8227 	intel_de_posting_read(dev_priv, TRANSCONF(pipe));
8228 
8229 	intel_wait_for_pipe_scanline_moving(crtc);
8230 }
8231 
8232 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
8233 {
8234 	struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv, pipe);
8235 
8236 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
8237 		    pipe_name(pipe));
8238 
8239 	drm_WARN_ON(&dev_priv->drm,
8240 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) & DISP_ENABLE);
8241 	drm_WARN_ON(&dev_priv->drm,
8242 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) & DISP_ENABLE);
8243 	drm_WARN_ON(&dev_priv->drm,
8244 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) & DISP_ENABLE);
8245 	drm_WARN_ON(&dev_priv->drm,
8246 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE_MASK);
8247 	drm_WARN_ON(&dev_priv->drm,
8248 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE_MASK);
8249 
8250 	intel_de_write(dev_priv, TRANSCONF(pipe), 0);
8251 	intel_de_posting_read(dev_priv, TRANSCONF(pipe));
8252 
8253 	intel_wait_for_pipe_scanline_stopped(crtc);
8254 
8255 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
8256 	intel_de_posting_read(dev_priv, DPLL(pipe));
8257 }
8258 
8259 void intel_hpd_poll_fini(struct drm_i915_private *i915)
8260 {
8261 	struct intel_connector *connector;
8262 	struct drm_connector_list_iter conn_iter;
8263 
8264 	/* Kill all the work that may have been queued by hpd. */
8265 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
8266 	for_each_intel_connector_iter(connector, &conn_iter) {
8267 		if (connector->modeset_retry_work.func &&
8268 		    cancel_work_sync(&connector->modeset_retry_work))
8269 			drm_connector_put(&connector->base);
8270 		if (connector->hdcp.shim) {
8271 			cancel_delayed_work_sync(&connector->hdcp.check_work);
8272 			cancel_work_sync(&connector->hdcp.prop_work);
8273 		}
8274 	}
8275 	drm_connector_list_iter_end(&conn_iter);
8276 }
8277 
8278 bool intel_scanout_needs_vtd_wa(struct drm_i915_private *i915)
8279 {
8280 	return DISPLAY_VER(i915) >= 6 && i915_vtd_active(i915);
8281 }
8282