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