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