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