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