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