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