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