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