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