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