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