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