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