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