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