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 <acpi/video.h>
28 #include <linux/i2c.h>
29 #include <linux/input.h>
30 #include <linux/intel-iommu.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/dma-resv.h>
34 #include <linux/slab.h>
35 
36 #include <drm/drm_atomic.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_atomic_uapi.h>
39 #include <drm/drm_damage_helper.h>
40 #include <drm/drm_dp_helper.h>
41 #include <drm/drm_edid.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_plane_helper.h>
44 #include <drm/drm_probe_helper.h>
45 #include <drm/drm_rect.h>
46 
47 #include "display/intel_audio.h"
48 #include "display/intel_crt.h"
49 #include "display/intel_ddi.h"
50 #include "display/intel_display_debugfs.h"
51 #include "display/intel_dp.h"
52 #include "display/intel_dp_mst.h"
53 #include "display/intel_dpll.h"
54 #include "display/intel_dpll_mgr.h"
55 #include "display/intel_drrs.h"
56 #include "display/intel_dsi.h"
57 #include "display/intel_dvo.h"
58 #include "display/intel_fb.h"
59 #include "display/intel_gmbus.h"
60 #include "display/intel_hdmi.h"
61 #include "display/intel_lvds.h"
62 #include "display/intel_sdvo.h"
63 #include "display/intel_snps_phy.h"
64 #include "display/intel_tv.h"
65 #include "display/intel_vdsc.h"
66 #include "display/intel_vrr.h"
67 
68 #include "gem/i915_gem_lmem.h"
69 #include "gem/i915_gem_object.h"
70 
71 #include "gt/gen8_ppgtt.h"
72 
73 #include "pxp/intel_pxp.h"
74 
75 #include "g4x_dp.h"
76 #include "g4x_hdmi.h"
77 #include "i915_drv.h"
78 #include "intel_acpi.h"
79 #include "intel_atomic.h"
80 #include "intel_atomic_plane.h"
81 #include "intel_bw.h"
82 #include "intel_cdclk.h"
83 #include "intel_color.h"
84 #include "intel_crtc.h"
85 #include "intel_de.h"
86 #include "intel_display_types.h"
87 #include "intel_dmc.h"
88 #include "intel_dp_link_training.h"
89 #include "intel_dpt.h"
90 #include "intel_fbc.h"
91 #include "intel_fbdev.h"
92 #include "intel_fdi.h"
93 #include "intel_fifo_underrun.h"
94 #include "intel_frontbuffer.h"
95 #include "intel_hdcp.h"
96 #include "intel_hotplug.h"
97 #include "intel_overlay.h"
98 #include "intel_panel.h"
99 #include "intel_pcode.h"
100 #include "intel_pipe_crc.h"
101 #include "intel_plane_initial.h"
102 #include "intel_pm.h"
103 #include "intel_pps.h"
104 #include "intel_psr.h"
105 #include "intel_quirks.h"
106 #include "intel_sbi.h"
107 #include "intel_sprite.h"
108 #include "intel_tc.h"
109 #include "intel_vga.h"
110 #include "i9xx_plane.h"
111 #include "skl_scaler.h"
112 #include "skl_universal_plane.h"
113 #include "vlv_sideband.h"
114 
115 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
116 				struct intel_crtc_state *pipe_config);
117 static void ilk_pch_clock_get(struct intel_crtc *crtc,
118 			      struct intel_crtc_state *pipe_config);
119 
120 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
121 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
122 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
123 					 const struct intel_link_m_n *m_n,
124 					 const struct intel_link_m_n *m2_n2);
125 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
126 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
127 static void hsw_set_transconf(const struct intel_crtc_state *crtc_state);
128 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
129 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
130 static void intel_modeset_setup_hw_state(struct drm_device *dev,
131 					 struct drm_modeset_acquire_ctx *ctx);
132 
133 /**
134  * intel_update_watermarks - update FIFO watermark values based on current modes
135  * @dev_priv: i915 device
136  *
137  * Calculate watermark values for the various WM regs based on current mode
138  * and plane configuration.
139  *
140  * There are several cases to deal with here:
141  *   - normal (i.e. non-self-refresh)
142  *   - self-refresh (SR) mode
143  *   - lines are large relative to FIFO size (buffer can hold up to 2)
144  *   - lines are small relative to FIFO size (buffer can hold more than 2
145  *     lines), so need to account for TLB latency
146  *
147  *   The normal calculation is:
148  *     watermark = dotclock * bytes per pixel * latency
149  *   where latency is platform & configuration dependent (we assume pessimal
150  *   values here).
151  *
152  *   The SR calculation is:
153  *     watermark = (trunc(latency/line time)+1) * surface width *
154  *       bytes per pixel
155  *   where
156  *     line time = htotal / dotclock
157  *     surface width = hdisplay for normal plane and 64 for cursor
158  *   and latency is assumed to be high, as above.
159  *
160  * The final value programmed to the register should always be rounded up,
161  * and include an extra 2 entries to account for clock crossings.
162  *
163  * We don't use the sprite, so we can ignore that.  And on Crestline we have
164  * to set the non-SR watermarks to 8.
165  */
166 static void intel_update_watermarks(struct drm_i915_private *dev_priv)
167 {
168 	if (dev_priv->wm_disp->update_wm)
169 		dev_priv->wm_disp->update_wm(dev_priv);
170 }
171 
172 static int intel_compute_pipe_wm(struct intel_atomic_state *state,
173 				 struct intel_crtc *crtc)
174 {
175 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
176 	if (dev_priv->wm_disp->compute_pipe_wm)
177 		return dev_priv->wm_disp->compute_pipe_wm(state, crtc);
178 	return 0;
179 }
180 
181 static int intel_compute_intermediate_wm(struct intel_atomic_state *state,
182 					 struct intel_crtc *crtc)
183 {
184 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
185 	if (!dev_priv->wm_disp->compute_intermediate_wm)
186 		return 0;
187 	if (drm_WARN_ON(&dev_priv->drm,
188 			!dev_priv->wm_disp->compute_pipe_wm))
189 		return 0;
190 	return dev_priv->wm_disp->compute_intermediate_wm(state, crtc);
191 }
192 
193 static bool intel_initial_watermarks(struct intel_atomic_state *state,
194 				     struct intel_crtc *crtc)
195 {
196 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
197 	if (dev_priv->wm_disp->initial_watermarks) {
198 		dev_priv->wm_disp->initial_watermarks(state, crtc);
199 		return true;
200 	}
201 	return false;
202 }
203 
204 static void intel_atomic_update_watermarks(struct intel_atomic_state *state,
205 					   struct intel_crtc *crtc)
206 {
207 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
208 	if (dev_priv->wm_disp->atomic_update_watermarks)
209 		dev_priv->wm_disp->atomic_update_watermarks(state, crtc);
210 }
211 
212 static void intel_optimize_watermarks(struct intel_atomic_state *state,
213 				      struct intel_crtc *crtc)
214 {
215 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
216 	if (dev_priv->wm_disp->optimize_watermarks)
217 		dev_priv->wm_disp->optimize_watermarks(state, crtc);
218 }
219 
220 static int intel_compute_global_watermarks(struct intel_atomic_state *state)
221 {
222 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
223 	if (dev_priv->wm_disp->compute_global_watermarks)
224 		return dev_priv->wm_disp->compute_global_watermarks(state);
225 	return 0;
226 }
227 
228 /* returns HPLL frequency in kHz */
229 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
230 {
231 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
232 
233 	/* Obtain SKU information */
234 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
235 		CCK_FUSE_HPLL_FREQ_MASK;
236 
237 	return vco_freq[hpll_freq] * 1000;
238 }
239 
240 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
241 		      const char *name, u32 reg, int ref_freq)
242 {
243 	u32 val;
244 	int divider;
245 
246 	val = vlv_cck_read(dev_priv, reg);
247 	divider = val & CCK_FREQUENCY_VALUES;
248 
249 	drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
250 		 (divider << CCK_FREQUENCY_STATUS_SHIFT),
251 		 "%s change in progress\n", name);
252 
253 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
254 }
255 
256 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
257 			   const char *name, u32 reg)
258 {
259 	int hpll;
260 
261 	vlv_cck_get(dev_priv);
262 
263 	if (dev_priv->hpll_freq == 0)
264 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
265 
266 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
267 
268 	vlv_cck_put(dev_priv);
269 
270 	return hpll;
271 }
272 
273 static void intel_update_czclk(struct drm_i915_private *dev_priv)
274 {
275 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
276 		return;
277 
278 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
279 						      CCK_CZ_CLOCK_CONTROL);
280 
281 	drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
282 		dev_priv->czclk_freq);
283 }
284 
285 static bool is_hdr_mode(const struct intel_crtc_state *crtc_state)
286 {
287 	return (crtc_state->active_planes &
288 		~(icl_hdr_plane_mask() | BIT(PLANE_CURSOR))) == 0;
289 }
290 
291 /* WA Display #0827: Gen9:all */
292 static void
293 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
294 {
295 	if (enable)
296 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
297 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
298 	else
299 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
300 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
301 }
302 
303 /* Wa_2006604312:icl,ehl */
304 static void
305 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
306 		       bool enable)
307 {
308 	if (enable)
309 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
310 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
311 	else
312 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
313 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
314 }
315 
316 /* Wa_1604331009:icl,jsl,ehl */
317 static void
318 icl_wa_cursorclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
319 		       bool enable)
320 {
321 	intel_de_rmw(dev_priv, CLKGATE_DIS_PSL(pipe), CURSOR_GATING_DIS,
322 		     enable ? CURSOR_GATING_DIS : 0);
323 }
324 
325 static bool
326 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
327 {
328 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
329 }
330 
331 static bool
332 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
333 {
334 	return crtc_state->sync_mode_slaves_mask != 0;
335 }
336 
337 bool
338 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
339 {
340 	return is_trans_port_sync_master(crtc_state) ||
341 		is_trans_port_sync_slave(crtc_state);
342 }
343 
344 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
345 				    enum pipe pipe)
346 {
347 	i915_reg_t reg = PIPEDSL(pipe);
348 	u32 line1, line2;
349 	u32 line_mask;
350 
351 	if (DISPLAY_VER(dev_priv) == 2)
352 		line_mask = DSL_LINEMASK_GEN2;
353 	else
354 		line_mask = DSL_LINEMASK_GEN3;
355 
356 	line1 = intel_de_read(dev_priv, reg) & line_mask;
357 	msleep(5);
358 	line2 = intel_de_read(dev_priv, reg) & line_mask;
359 
360 	return line1 != line2;
361 }
362 
363 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
364 {
365 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
366 	enum pipe pipe = crtc->pipe;
367 
368 	/* Wait for the display line to settle/start moving */
369 	if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
370 		drm_err(&dev_priv->drm,
371 			"pipe %c scanline %s wait timed out\n",
372 			pipe_name(pipe), onoff(state));
373 }
374 
375 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
376 {
377 	wait_for_pipe_scanline_moving(crtc, false);
378 }
379 
380 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
381 {
382 	wait_for_pipe_scanline_moving(crtc, true);
383 }
384 
385 static void
386 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
387 {
388 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
389 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
390 
391 	if (DISPLAY_VER(dev_priv) >= 4) {
392 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
393 		i915_reg_t reg = PIPECONF(cpu_transcoder);
394 
395 		/* Wait for the Pipe State to go off */
396 		if (intel_de_wait_for_clear(dev_priv, reg,
397 					    I965_PIPECONF_ACTIVE, 100))
398 			drm_WARN(&dev_priv->drm, 1,
399 				 "pipe_off wait timed out\n");
400 	} else {
401 		intel_wait_for_pipe_scanline_stopped(crtc);
402 	}
403 }
404 
405 void assert_transcoder(struct drm_i915_private *dev_priv,
406 		       enum transcoder cpu_transcoder, bool state)
407 {
408 	bool cur_state;
409 	enum intel_display_power_domain power_domain;
410 	intel_wakeref_t wakeref;
411 
412 	/* we keep both pipes enabled on 830 */
413 	if (IS_I830(dev_priv))
414 		state = true;
415 
416 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
417 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
418 	if (wakeref) {
419 		u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
420 		cur_state = !!(val & PIPECONF_ENABLE);
421 
422 		intel_display_power_put(dev_priv, power_domain, wakeref);
423 	} else {
424 		cur_state = false;
425 	}
426 
427 	I915_STATE_WARN(cur_state != state,
428 			"transcoder %s assertion failure (expected %s, current %s)\n",
429 			transcoder_name(cpu_transcoder),
430 			onoff(state), onoff(cur_state));
431 }
432 
433 static void assert_plane(struct intel_plane *plane, bool state)
434 {
435 	enum pipe pipe;
436 	bool cur_state;
437 
438 	cur_state = plane->get_hw_state(plane, &pipe);
439 
440 	I915_STATE_WARN(cur_state != state,
441 			"%s assertion failure (expected %s, current %s)\n",
442 			plane->base.name, onoff(state), onoff(cur_state));
443 }
444 
445 #define assert_plane_enabled(p) assert_plane(p, true)
446 #define assert_plane_disabled(p) assert_plane(p, false)
447 
448 static void assert_planes_disabled(struct intel_crtc *crtc)
449 {
450 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
451 	struct intel_plane *plane;
452 
453 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
454 		assert_plane_disabled(plane);
455 }
456 
457 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
458 				    enum pipe pipe)
459 {
460 	u32 val;
461 	bool enabled;
462 
463 	val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
464 	enabled = !!(val & TRANS_ENABLE);
465 	I915_STATE_WARN(enabled,
466 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
467 	     pipe_name(pipe));
468 }
469 
470 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
471 				   enum pipe pipe, enum port port,
472 				   i915_reg_t dp_reg)
473 {
474 	enum pipe port_pipe;
475 	bool state;
476 
477 	state = g4x_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
478 
479 	I915_STATE_WARN(state && port_pipe == pipe,
480 			"PCH DP %c enabled on transcoder %c, should be disabled\n",
481 			port_name(port), pipe_name(pipe));
482 
483 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
484 			"IBX PCH DP %c still using transcoder B\n",
485 			port_name(port));
486 }
487 
488 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
489 				     enum pipe pipe, enum port port,
490 				     i915_reg_t hdmi_reg)
491 {
492 	enum pipe port_pipe;
493 	bool state;
494 
495 	state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
496 
497 	I915_STATE_WARN(state && port_pipe == pipe,
498 			"PCH HDMI %c enabled on transcoder %c, should be disabled\n",
499 			port_name(port), pipe_name(pipe));
500 
501 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
502 			"IBX PCH HDMI %c still using transcoder B\n",
503 			port_name(port));
504 }
505 
506 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
507 				      enum pipe pipe)
508 {
509 	enum pipe port_pipe;
510 
511 	assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
512 	assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
513 	assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
514 
515 	I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
516 			port_pipe == pipe,
517 			"PCH VGA enabled on transcoder %c, should be disabled\n",
518 			pipe_name(pipe));
519 
520 	I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
521 			port_pipe == pipe,
522 			"PCH LVDS enabled on transcoder %c, should be disabled\n",
523 			pipe_name(pipe));
524 
525 	/* PCH SDVOB multiplex with HDMIB */
526 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
527 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
528 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
529 }
530 
531 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
532 			 struct intel_digital_port *dig_port,
533 			 unsigned int expected_mask)
534 {
535 	u32 port_mask;
536 	i915_reg_t dpll_reg;
537 
538 	switch (dig_port->base.port) {
539 	case PORT_B:
540 		port_mask = DPLL_PORTB_READY_MASK;
541 		dpll_reg = DPLL(0);
542 		break;
543 	case PORT_C:
544 		port_mask = DPLL_PORTC_READY_MASK;
545 		dpll_reg = DPLL(0);
546 		expected_mask <<= 4;
547 		break;
548 	case PORT_D:
549 		port_mask = DPLL_PORTD_READY_MASK;
550 		dpll_reg = DPIO_PHY_STATUS;
551 		break;
552 	default:
553 		BUG();
554 	}
555 
556 	if (intel_de_wait_for_register(dev_priv, dpll_reg,
557 				       port_mask, expected_mask, 1000))
558 		drm_WARN(&dev_priv->drm, 1,
559 			 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
560 			 dig_port->base.base.base.id, dig_port->base.base.name,
561 			 intel_de_read(dev_priv, dpll_reg) & port_mask,
562 			 expected_mask);
563 }
564 
565 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
566 {
567 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
568 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
569 	enum pipe pipe = crtc->pipe;
570 	i915_reg_t reg;
571 	u32 val, pipeconf_val;
572 
573 	/* Make sure PCH DPLL is enabled */
574 	assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
575 
576 	/* FDI must be feeding us bits for PCH ports */
577 	assert_fdi_tx_enabled(dev_priv, pipe);
578 	assert_fdi_rx_enabled(dev_priv, pipe);
579 
580 	if (HAS_PCH_CPT(dev_priv)) {
581 		reg = TRANS_CHICKEN2(pipe);
582 		val = intel_de_read(dev_priv, reg);
583 		/*
584 		 * Workaround: Set the timing override bit
585 		 * before enabling the pch transcoder.
586 		 */
587 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
588 		/* Configure frame start delay to match the CPU */
589 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
590 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
591 		intel_de_write(dev_priv, reg, val);
592 	}
593 
594 	reg = PCH_TRANSCONF(pipe);
595 	val = intel_de_read(dev_priv, reg);
596 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
597 
598 	if (HAS_PCH_IBX(dev_priv)) {
599 		/* Configure frame start delay to match the CPU */
600 		val &= ~TRANS_FRAME_START_DELAY_MASK;
601 		val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
602 
603 		/*
604 		 * Make the BPC in transcoder be consistent with
605 		 * that in pipeconf reg. For HDMI we must use 8bpc
606 		 * here for both 8bpc and 12bpc.
607 		 */
608 		val &= ~PIPECONF_BPC_MASK;
609 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
610 			val |= PIPECONF_8BPC;
611 		else
612 			val |= pipeconf_val & PIPECONF_BPC_MASK;
613 	}
614 
615 	val &= ~TRANS_INTERLACE_MASK;
616 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
617 		if (HAS_PCH_IBX(dev_priv) &&
618 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
619 			val |= TRANS_LEGACY_INTERLACED_ILK;
620 		else
621 			val |= TRANS_INTERLACED;
622 	} else {
623 		val |= TRANS_PROGRESSIVE;
624 	}
625 
626 	intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
627 	if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
628 		drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
629 			pipe_name(pipe));
630 }
631 
632 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
633 				      enum transcoder cpu_transcoder)
634 {
635 	u32 val, pipeconf_val;
636 
637 	/* FDI must be feeding us bits for PCH ports */
638 	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
639 	assert_fdi_rx_enabled(dev_priv, PIPE_A);
640 
641 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
642 	/* Workaround: set timing override bit. */
643 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
644 	/* Configure frame start delay to match the CPU */
645 	val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
646 	val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
647 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
648 
649 	val = TRANS_ENABLE;
650 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
651 
652 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
653 	    PIPECONF_INTERLACED_ILK)
654 		val |= TRANS_INTERLACED;
655 	else
656 		val |= TRANS_PROGRESSIVE;
657 
658 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
659 	if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
660 				  TRANS_STATE_ENABLE, 100))
661 		drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
662 }
663 
664 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
665 				       enum pipe pipe)
666 {
667 	i915_reg_t reg;
668 	u32 val;
669 
670 	/* FDI relies on the transcoder */
671 	assert_fdi_tx_disabled(dev_priv, pipe);
672 	assert_fdi_rx_disabled(dev_priv, pipe);
673 
674 	/* Ports must be off as well */
675 	assert_pch_ports_disabled(dev_priv, pipe);
676 
677 	reg = PCH_TRANSCONF(pipe);
678 	val = intel_de_read(dev_priv, reg);
679 	val &= ~TRANS_ENABLE;
680 	intel_de_write(dev_priv, reg, val);
681 	/* wait for PCH transcoder off, transcoder state */
682 	if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
683 		drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
684 			pipe_name(pipe));
685 
686 	if (HAS_PCH_CPT(dev_priv)) {
687 		/* Workaround: Clear the timing override chicken bit again. */
688 		reg = TRANS_CHICKEN2(pipe);
689 		val = intel_de_read(dev_priv, reg);
690 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
691 		intel_de_write(dev_priv, reg, val);
692 	}
693 }
694 
695 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
696 {
697 	u32 val;
698 
699 	val = intel_de_read(dev_priv, LPT_TRANSCONF);
700 	val &= ~TRANS_ENABLE;
701 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
702 	/* wait for PCH transcoder off, transcoder state */
703 	if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
704 				    TRANS_STATE_ENABLE, 50))
705 		drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
706 
707 	/* Workaround: clear timing override bit. */
708 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
709 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
710 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
711 }
712 
713 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
714 {
715 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
716 
717 	if (HAS_PCH_LPT(dev_priv))
718 		return PIPE_A;
719 	else
720 		return crtc->pipe;
721 }
722 
723 void intel_enable_transcoder(const struct intel_crtc_state *new_crtc_state)
724 {
725 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
726 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
727 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
728 	enum pipe pipe = crtc->pipe;
729 	i915_reg_t reg;
730 	u32 val;
731 
732 	drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
733 
734 	assert_planes_disabled(crtc);
735 
736 	/*
737 	 * A pipe without a PLL won't actually be able to drive bits from
738 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
739 	 * need the check.
740 	 */
741 	if (HAS_GMCH(dev_priv)) {
742 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
743 			assert_dsi_pll_enabled(dev_priv);
744 		else
745 			assert_pll_enabled(dev_priv, pipe);
746 	} else {
747 		if (new_crtc_state->has_pch_encoder) {
748 			/* if driving the PCH, we need FDI enabled */
749 			assert_fdi_rx_pll_enabled(dev_priv,
750 						  intel_crtc_pch_transcoder(crtc));
751 			assert_fdi_tx_pll_enabled(dev_priv,
752 						  (enum pipe) cpu_transcoder);
753 		}
754 		/* FIXME: assert CPU port conditions for SNB+ */
755 	}
756 
757 	/* Wa_22012358565:adl-p */
758 	if (DISPLAY_VER(dev_priv) == 13)
759 		intel_de_rmw(dev_priv, PIPE_ARB_CTL(pipe),
760 			     0, PIPE_ARB_USE_PROG_SLOTS);
761 
762 	reg = PIPECONF(cpu_transcoder);
763 	val = intel_de_read(dev_priv, reg);
764 	if (val & PIPECONF_ENABLE) {
765 		/* we keep both pipes enabled on 830 */
766 		drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
767 		return;
768 	}
769 
770 	intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
771 	intel_de_posting_read(dev_priv, reg);
772 
773 	/*
774 	 * Until the pipe starts PIPEDSL reads will return a stale value,
775 	 * which causes an apparent vblank timestamp jump when PIPEDSL
776 	 * resets to its proper value. That also messes up the frame count
777 	 * when it's derived from the timestamps. So let's wait for the
778 	 * pipe to start properly before we call drm_crtc_vblank_on()
779 	 */
780 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
781 		intel_wait_for_pipe_scanline_moving(crtc);
782 }
783 
784 void intel_disable_transcoder(const struct intel_crtc_state *old_crtc_state)
785 {
786 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
787 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
788 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
789 	enum pipe pipe = crtc->pipe;
790 	i915_reg_t reg;
791 	u32 val;
792 
793 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
794 
795 	/*
796 	 * Make sure planes won't keep trying to pump pixels to us,
797 	 * or we might hang the display.
798 	 */
799 	assert_planes_disabled(crtc);
800 
801 	reg = PIPECONF(cpu_transcoder);
802 	val = intel_de_read(dev_priv, reg);
803 	if ((val & PIPECONF_ENABLE) == 0)
804 		return;
805 
806 	/*
807 	 * Double wide has implications for planes
808 	 * so best keep it disabled when not needed.
809 	 */
810 	if (old_crtc_state->double_wide)
811 		val &= ~PIPECONF_DOUBLE_WIDE;
812 
813 	/* Don't disable pipe or pipe PLLs if needed */
814 	if (!IS_I830(dev_priv))
815 		val &= ~PIPECONF_ENABLE;
816 
817 	if (DISPLAY_VER(dev_priv) >= 12)
818 		intel_de_rmw(dev_priv, CHICKEN_TRANS(cpu_transcoder),
819 			     FECSTALL_DIS_DPTSTREAM_DPTTG, 0);
820 
821 	intel_de_write(dev_priv, reg, val);
822 	if ((val & PIPECONF_ENABLE) == 0)
823 		intel_wait_for_pipe_off(old_crtc_state);
824 }
825 
826 bool
827 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
828 				    u64 modifier)
829 {
830 	return info->is_yuv &&
831 	       info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
832 }
833 
834 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
835 {
836 	unsigned int size = 0;
837 	int i;
838 
839 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
840 		size += rot_info->plane[i].dst_stride * rot_info->plane[i].width;
841 
842 	return size;
843 }
844 
845 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
846 {
847 	unsigned int size = 0;
848 	int i;
849 
850 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++) {
851 		if (rem_info->plane_alignment)
852 			size = ALIGN(size, rem_info->plane_alignment);
853 		size += rem_info->plane[i].dst_stride * rem_info->plane[i].height;
854 	}
855 
856 	return size;
857 }
858 
859 bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
860 {
861 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
862 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
863 
864 	return DISPLAY_VER(dev_priv) < 4 ||
865 		(plane->has_fbc &&
866 		 plane_state->view.gtt.type == I915_GGTT_VIEW_NORMAL);
867 }
868 
869 /*
870  * Convert the x/y offsets into a linear offset.
871  * Only valid with 0/180 degree rotation, which is fine since linear
872  * offset is only used with linear buffers on pre-hsw and tiled buffers
873  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
874  */
875 u32 intel_fb_xy_to_linear(int x, int y,
876 			  const struct intel_plane_state *state,
877 			  int color_plane)
878 {
879 	const struct drm_framebuffer *fb = state->hw.fb;
880 	unsigned int cpp = fb->format->cpp[color_plane];
881 	unsigned int pitch = state->view.color_plane[color_plane].stride;
882 
883 	return y * pitch + x * cpp;
884 }
885 
886 /*
887  * Add the x/y offsets derived from fb->offsets[] to the user
888  * specified plane src x/y offsets. The resulting x/y offsets
889  * specify the start of scanout from the beginning of the gtt mapping.
890  */
891 void intel_add_fb_offsets(int *x, int *y,
892 			  const struct intel_plane_state *state,
893 			  int color_plane)
894 
895 {
896 	*x += state->view.color_plane[color_plane].x;
897 	*y += state->view.color_plane[color_plane].y;
898 }
899 
900 /*
901  * From the Sky Lake PRM:
902  * "The Color Control Surface (CCS) contains the compression status of
903  *  the cache-line pairs. The compression state of the cache-line pair
904  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
905  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
906  *  cache-line-pairs. CCS is always Y tiled."
907  *
908  * Since cache line pairs refers to horizontally adjacent cache lines,
909  * each cache line in the CCS corresponds to an area of 32x16 cache
910  * lines on the main surface. Since each pixel is 4 bytes, this gives
911  * us a ratio of one byte in the CCS for each 8x16 pixels in the
912  * main surface.
913  */
914 static const struct drm_format_info skl_ccs_formats[] = {
915 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
916 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
917 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
918 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
919 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
920 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
921 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
922 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
923 };
924 
925 /*
926  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
927  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
928  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
929  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
930  * the main surface.
931  */
932 static const struct drm_format_info gen12_ccs_formats[] = {
933 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
934 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
935 	  .hsub = 1, .vsub = 1, },
936 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
937 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
938 	  .hsub = 1, .vsub = 1, },
939 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
940 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
941 	  .hsub = 1, .vsub = 1, .has_alpha = true },
942 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
943 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
944 	  .hsub = 1, .vsub = 1, .has_alpha = true },
945 	{ .format = DRM_FORMAT_YUYV, .num_planes = 2,
946 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
947 	  .hsub = 2, .vsub = 1, .is_yuv = true },
948 	{ .format = DRM_FORMAT_YVYU, .num_planes = 2,
949 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
950 	  .hsub = 2, .vsub = 1, .is_yuv = true },
951 	{ .format = DRM_FORMAT_UYVY, .num_planes = 2,
952 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
953 	  .hsub = 2, .vsub = 1, .is_yuv = true },
954 	{ .format = DRM_FORMAT_VYUY, .num_planes = 2,
955 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
956 	  .hsub = 2, .vsub = 1, .is_yuv = true },
957 	{ .format = DRM_FORMAT_XYUV8888, .num_planes = 2,
958 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
959 	  .hsub = 1, .vsub = 1, .is_yuv = true },
960 	{ .format = DRM_FORMAT_NV12, .num_planes = 4,
961 	  .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
962 	  .hsub = 2, .vsub = 2, .is_yuv = true },
963 	{ .format = DRM_FORMAT_P010, .num_planes = 4,
964 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
965 	  .hsub = 2, .vsub = 2, .is_yuv = true },
966 	{ .format = DRM_FORMAT_P012, .num_planes = 4,
967 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
968 	  .hsub = 2, .vsub = 2, .is_yuv = true },
969 	{ .format = DRM_FORMAT_P016, .num_planes = 4,
970 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
971 	  .hsub = 2, .vsub = 2, .is_yuv = true },
972 };
973 
974 /*
975  * Same as gen12_ccs_formats[] above, but with additional surface used
976  * to pass Clear Color information in plane 2 with 64 bits of data.
977  */
978 static const struct drm_format_info gen12_ccs_cc_formats[] = {
979 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
980 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
981 	  .hsub = 1, .vsub = 1, },
982 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
983 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
984 	  .hsub = 1, .vsub = 1, },
985 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
986 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
987 	  .hsub = 1, .vsub = 1, .has_alpha = true },
988 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 3,
989 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
990 	  .hsub = 1, .vsub = 1, .has_alpha = true },
991 };
992 
993 static const struct drm_format_info *
994 lookup_format_info(const struct drm_format_info formats[],
995 		   int num_formats, u32 format)
996 {
997 	int i;
998 
999 	for (i = 0; i < num_formats; i++) {
1000 		if (formats[i].format == format)
1001 			return &formats[i];
1002 	}
1003 
1004 	return NULL;
1005 }
1006 
1007 static const struct drm_format_info *
1008 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
1009 {
1010 	switch (cmd->modifier[0]) {
1011 	case I915_FORMAT_MOD_Y_TILED_CCS:
1012 	case I915_FORMAT_MOD_Yf_TILED_CCS:
1013 		return lookup_format_info(skl_ccs_formats,
1014 					  ARRAY_SIZE(skl_ccs_formats),
1015 					  cmd->pixel_format);
1016 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1017 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1018 		return lookup_format_info(gen12_ccs_formats,
1019 					  ARRAY_SIZE(gen12_ccs_formats),
1020 					  cmd->pixel_format);
1021 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1022 		return lookup_format_info(gen12_ccs_cc_formats,
1023 					  ARRAY_SIZE(gen12_ccs_cc_formats),
1024 					  cmd->pixel_format);
1025 	default:
1026 		return NULL;
1027 	}
1028 }
1029 
1030 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
1031 			      u32 pixel_format, u64 modifier)
1032 {
1033 	struct intel_crtc *crtc;
1034 	struct intel_plane *plane;
1035 
1036 	if (!HAS_DISPLAY(dev_priv))
1037 		return 0;
1038 
1039 	/*
1040 	 * We assume the primary plane for pipe A has
1041 	 * the highest stride limits of them all,
1042 	 * if in case pipe A is disabled, use the first pipe from pipe_mask.
1043 	 */
1044 	crtc = intel_get_first_crtc(dev_priv);
1045 	if (!crtc)
1046 		return 0;
1047 
1048 	plane = to_intel_plane(crtc->base.primary);
1049 
1050 	return plane->max_stride(plane, pixel_format, modifier,
1051 				 DRM_MODE_ROTATE_0);
1052 }
1053 
1054 static void
1055 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
1056 			struct intel_plane_state *plane_state,
1057 			bool visible)
1058 {
1059 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1060 
1061 	plane_state->uapi.visible = visible;
1062 
1063 	if (visible)
1064 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
1065 	else
1066 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
1067 }
1068 
1069 static void fixup_plane_bitmasks(struct intel_crtc_state *crtc_state)
1070 {
1071 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1072 	struct drm_plane *plane;
1073 
1074 	/*
1075 	 * Active_planes aliases if multiple "primary" or cursor planes
1076 	 * have been used on the same (or wrong) pipe. plane_mask uses
1077 	 * unique ids, hence we can use that to reconstruct active_planes.
1078 	 */
1079 	crtc_state->enabled_planes = 0;
1080 	crtc_state->active_planes = 0;
1081 
1082 	drm_for_each_plane_mask(plane, &dev_priv->drm,
1083 				crtc_state->uapi.plane_mask) {
1084 		crtc_state->enabled_planes |= BIT(to_intel_plane(plane)->id);
1085 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
1086 	}
1087 }
1088 
1089 void intel_plane_disable_noatomic(struct intel_crtc *crtc,
1090 				  struct intel_plane *plane)
1091 {
1092 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1093 	struct intel_crtc_state *crtc_state =
1094 		to_intel_crtc_state(crtc->base.state);
1095 	struct intel_plane_state *plane_state =
1096 		to_intel_plane_state(plane->base.state);
1097 
1098 	drm_dbg_kms(&dev_priv->drm,
1099 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
1100 		    plane->base.base.id, plane->base.name,
1101 		    crtc->base.base.id, crtc->base.name);
1102 
1103 	intel_set_plane_visible(crtc_state, plane_state, false);
1104 	fixup_plane_bitmasks(crtc_state);
1105 	crtc_state->data_rate[plane->id] = 0;
1106 	crtc_state->min_cdclk[plane->id] = 0;
1107 
1108 	if (plane->id == PLANE_PRIMARY)
1109 		hsw_disable_ips(crtc_state);
1110 
1111 	/*
1112 	 * Vblank time updates from the shadow to live plane control register
1113 	 * are blocked if the memory self-refresh mode is active at that
1114 	 * moment. So to make sure the plane gets truly disabled, disable
1115 	 * first the self-refresh mode. The self-refresh enable bit in turn
1116 	 * will be checked/applied by the HW only at the next frame start
1117 	 * event which is after the vblank start event, so we need to have a
1118 	 * wait-for-vblank between disabling the plane and the pipe.
1119 	 */
1120 	if (HAS_GMCH(dev_priv) &&
1121 	    intel_set_memory_cxsr(dev_priv, false))
1122 		intel_wait_for_vblank(dev_priv, crtc->pipe);
1123 
1124 	/*
1125 	 * Gen2 reports pipe underruns whenever all planes are disabled.
1126 	 * So disable underrun reporting before all the planes get disabled.
1127 	 */
1128 	if (DISPLAY_VER(dev_priv) == 2 && !crtc_state->active_planes)
1129 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
1130 
1131 	intel_disable_plane(plane, crtc_state);
1132 	intel_wait_for_vblank(dev_priv, crtc->pipe);
1133 }
1134 
1135 unsigned int
1136 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
1137 {
1138 	int x = 0, y = 0;
1139 
1140 	intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
1141 					  plane_state->view.color_plane[0].offset, 0);
1142 
1143 	return y;
1144 }
1145 
1146 static int
1147 __intel_display_resume(struct drm_device *dev,
1148 		       struct drm_atomic_state *state,
1149 		       struct drm_modeset_acquire_ctx *ctx)
1150 {
1151 	struct drm_crtc_state *crtc_state;
1152 	struct drm_crtc *crtc;
1153 	int i, ret;
1154 
1155 	intel_modeset_setup_hw_state(dev, ctx);
1156 	intel_vga_redisable(to_i915(dev));
1157 
1158 	if (!state)
1159 		return 0;
1160 
1161 	/*
1162 	 * We've duplicated the state, pointers to the old state are invalid.
1163 	 *
1164 	 * Don't attempt to use the old state until we commit the duplicated state.
1165 	 */
1166 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
1167 		/*
1168 		 * Force recalculation even if we restore
1169 		 * current state. With fast modeset this may not result
1170 		 * in a modeset when the state is compatible.
1171 		 */
1172 		crtc_state->mode_changed = true;
1173 	}
1174 
1175 	/* ignore any reset values/BIOS leftovers in the WM registers */
1176 	if (!HAS_GMCH(to_i915(dev)))
1177 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
1178 
1179 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
1180 
1181 	drm_WARN_ON(dev, ret == -EDEADLK);
1182 	return ret;
1183 }
1184 
1185 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
1186 {
1187 	return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
1188 		intel_has_gpu_reset(&dev_priv->gt));
1189 }
1190 
1191 void intel_display_prepare_reset(struct drm_i915_private *dev_priv)
1192 {
1193 	struct drm_device *dev = &dev_priv->drm;
1194 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
1195 	struct drm_atomic_state *state;
1196 	int ret;
1197 
1198 	if (!HAS_DISPLAY(dev_priv))
1199 		return;
1200 
1201 	/* reset doesn't touch the display */
1202 	if (!dev_priv->params.force_reset_modeset_test &&
1203 	    !gpu_reset_clobbers_display(dev_priv))
1204 		return;
1205 
1206 	/* We have a modeset vs reset deadlock, defensively unbreak it. */
1207 	set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
1208 	smp_mb__after_atomic();
1209 	wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
1210 
1211 	if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
1212 		drm_dbg_kms(&dev_priv->drm,
1213 			    "Modeset potentially stuck, unbreaking through wedging\n");
1214 		intel_gt_set_wedged(&dev_priv->gt);
1215 	}
1216 
1217 	/*
1218 	 * Need mode_config.mutex so that we don't
1219 	 * trample ongoing ->detect() and whatnot.
1220 	 */
1221 	mutex_lock(&dev->mode_config.mutex);
1222 	drm_modeset_acquire_init(ctx, 0);
1223 	while (1) {
1224 		ret = drm_modeset_lock_all_ctx(dev, ctx);
1225 		if (ret != -EDEADLK)
1226 			break;
1227 
1228 		drm_modeset_backoff(ctx);
1229 	}
1230 	/*
1231 	 * Disabling the crtcs gracefully seems nicer. Also the
1232 	 * g33 docs say we should at least disable all the planes.
1233 	 */
1234 	state = drm_atomic_helper_duplicate_state(dev, ctx);
1235 	if (IS_ERR(state)) {
1236 		ret = PTR_ERR(state);
1237 		drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
1238 			ret);
1239 		return;
1240 	}
1241 
1242 	ret = drm_atomic_helper_disable_all(dev, ctx);
1243 	if (ret) {
1244 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
1245 			ret);
1246 		drm_atomic_state_put(state);
1247 		return;
1248 	}
1249 
1250 	dev_priv->modeset_restore_state = state;
1251 	state->acquire_ctx = ctx;
1252 }
1253 
1254 void intel_display_finish_reset(struct drm_i915_private *dev_priv)
1255 {
1256 	struct drm_device *dev = &dev_priv->drm;
1257 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
1258 	struct drm_atomic_state *state;
1259 	int ret;
1260 
1261 	if (!HAS_DISPLAY(dev_priv))
1262 		return;
1263 
1264 	/* reset doesn't touch the display */
1265 	if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
1266 		return;
1267 
1268 	state = fetch_and_zero(&dev_priv->modeset_restore_state);
1269 	if (!state)
1270 		goto unlock;
1271 
1272 	/* reset doesn't touch the display */
1273 	if (!gpu_reset_clobbers_display(dev_priv)) {
1274 		/* for testing only restore the display */
1275 		ret = __intel_display_resume(dev, state, ctx);
1276 		if (ret)
1277 			drm_err(&dev_priv->drm,
1278 				"Restoring old state failed with %i\n", ret);
1279 	} else {
1280 		/*
1281 		 * The display has been reset as well,
1282 		 * so need a full re-initialization.
1283 		 */
1284 		intel_pps_unlock_regs_wa(dev_priv);
1285 		intel_modeset_init_hw(dev_priv);
1286 		intel_init_clock_gating(dev_priv);
1287 		intel_hpd_init(dev_priv);
1288 
1289 		ret = __intel_display_resume(dev, state, ctx);
1290 		if (ret)
1291 			drm_err(&dev_priv->drm,
1292 				"Restoring old state failed with %i\n", ret);
1293 
1294 		intel_hpd_poll_disable(dev_priv);
1295 	}
1296 
1297 	drm_atomic_state_put(state);
1298 unlock:
1299 	drm_modeset_drop_locks(ctx);
1300 	drm_modeset_acquire_fini(ctx);
1301 	mutex_unlock(&dev->mode_config.mutex);
1302 
1303 	clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
1304 }
1305 
1306 static bool underrun_recovery_supported(const struct intel_crtc_state *crtc_state)
1307 {
1308 	if (crtc_state->pch_pfit.enabled &&
1309 	    (crtc_state->pipe_src_w > drm_rect_width(&crtc_state->pch_pfit.dst) ||
1310 	     crtc_state->pipe_src_h > drm_rect_height(&crtc_state->pch_pfit.dst) ||
1311 	     crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420))
1312 		return false;
1313 
1314 	if (crtc_state->dsc.compression_enable)
1315 		return false;
1316 
1317 	if (crtc_state->has_psr2)
1318 		return false;
1319 
1320 	if (crtc_state->splitter.enable)
1321 		return false;
1322 
1323 	return true;
1324 }
1325 
1326 static void icl_set_pipe_chicken(const struct intel_crtc_state *crtc_state)
1327 {
1328 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1329 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1330 	enum pipe pipe = crtc->pipe;
1331 	u32 tmp;
1332 
1333 	tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
1334 
1335 	/*
1336 	 * Display WA #1153: icl
1337 	 * enable hardware to bypass the alpha math
1338 	 * and rounding for per-pixel values 00 and 0xff
1339 	 */
1340 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
1341 	/*
1342 	 * Display WA # 1605353570: icl
1343 	 * Set the pixel rounding bit to 1 for allowing
1344 	 * passthrough of Frame buffer pixels unmodified
1345 	 * across pipe
1346 	 */
1347 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
1348 
1349 	if (IS_DG2(dev_priv)) {
1350 		/*
1351 		 * Underrun recovery must always be disabled on DG2.  However
1352 		 * the chicken bit meaning is inverted compared to other
1353 		 * platforms.
1354 		 */
1355 		tmp &= ~UNDERRUN_RECOVERY_ENABLE_DG2;
1356 	} else if (DISPLAY_VER(dev_priv) >= 13) {
1357 		if (underrun_recovery_supported(crtc_state))
1358 			tmp &= ~UNDERRUN_RECOVERY_DISABLE_ADLP;
1359 		else
1360 			tmp |= UNDERRUN_RECOVERY_DISABLE_ADLP;
1361 	}
1362 
1363 	intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
1364 }
1365 
1366 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
1367 {
1368 	struct drm_crtc *crtc;
1369 	bool cleanup_done;
1370 
1371 	drm_for_each_crtc(crtc, &dev_priv->drm) {
1372 		struct drm_crtc_commit *commit;
1373 		spin_lock(&crtc->commit_lock);
1374 		commit = list_first_entry_or_null(&crtc->commit_list,
1375 						  struct drm_crtc_commit, commit_entry);
1376 		cleanup_done = commit ?
1377 			try_wait_for_completion(&commit->cleanup_done) : true;
1378 		spin_unlock(&crtc->commit_lock);
1379 
1380 		if (cleanup_done)
1381 			continue;
1382 
1383 		drm_crtc_wait_one_vblank(crtc);
1384 
1385 		return true;
1386 	}
1387 
1388 	return false;
1389 }
1390 
1391 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
1392 {
1393 	u32 temp;
1394 
1395 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
1396 
1397 	mutex_lock(&dev_priv->sb_lock);
1398 
1399 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
1400 	temp |= SBI_SSCCTL_DISABLE;
1401 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
1402 
1403 	mutex_unlock(&dev_priv->sb_lock);
1404 }
1405 
1406 /* Program iCLKIP clock to the desired frequency */
1407 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
1408 {
1409 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1410 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1411 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
1412 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
1413 	u32 temp;
1414 
1415 	lpt_disable_iclkip(dev_priv);
1416 
1417 	/* The iCLK virtual clock root frequency is in MHz,
1418 	 * but the adjusted_mode->crtc_clock in in KHz. To get the
1419 	 * divisors, it is necessary to divide one by another, so we
1420 	 * convert the virtual clock precision to KHz here for higher
1421 	 * precision.
1422 	 */
1423 	for (auxdiv = 0; auxdiv < 2; auxdiv++) {
1424 		u32 iclk_virtual_root_freq = 172800 * 1000;
1425 		u32 iclk_pi_range = 64;
1426 		u32 desired_divisor;
1427 
1428 		desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
1429 						    clock << auxdiv);
1430 		divsel = (desired_divisor / iclk_pi_range) - 2;
1431 		phaseinc = desired_divisor % iclk_pi_range;
1432 
1433 		/*
1434 		 * Near 20MHz is a corner case which is
1435 		 * out of range for the 7-bit divisor
1436 		 */
1437 		if (divsel <= 0x7f)
1438 			break;
1439 	}
1440 
1441 	/* This should not happen with any sane values */
1442 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
1443 		    ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
1444 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
1445 		    ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
1446 
1447 	drm_dbg_kms(&dev_priv->drm,
1448 		    "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
1449 		    clock, auxdiv, divsel, phasedir, phaseinc);
1450 
1451 	mutex_lock(&dev_priv->sb_lock);
1452 
1453 	/* Program SSCDIVINTPHASE6 */
1454 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
1455 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
1456 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
1457 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
1458 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
1459 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
1460 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
1461 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
1462 
1463 	/* Program SSCAUXDIV */
1464 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
1465 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
1466 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
1467 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
1468 
1469 	/* Enable modulator and associated divider */
1470 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
1471 	temp &= ~SBI_SSCCTL_DISABLE;
1472 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
1473 
1474 	mutex_unlock(&dev_priv->sb_lock);
1475 
1476 	/* Wait for initialization time */
1477 	udelay(24);
1478 
1479 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
1480 }
1481 
1482 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
1483 {
1484 	u32 divsel, phaseinc, auxdiv;
1485 	u32 iclk_virtual_root_freq = 172800 * 1000;
1486 	u32 iclk_pi_range = 64;
1487 	u32 desired_divisor;
1488 	u32 temp;
1489 
1490 	if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
1491 		return 0;
1492 
1493 	mutex_lock(&dev_priv->sb_lock);
1494 
1495 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
1496 	if (temp & SBI_SSCCTL_DISABLE) {
1497 		mutex_unlock(&dev_priv->sb_lock);
1498 		return 0;
1499 	}
1500 
1501 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
1502 	divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
1503 		SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
1504 	phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
1505 		SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
1506 
1507 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
1508 	auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
1509 		SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
1510 
1511 	mutex_unlock(&dev_priv->sb_lock);
1512 
1513 	desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
1514 
1515 	return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
1516 				 desired_divisor << auxdiv);
1517 }
1518 
1519 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
1520 					   enum pipe pch_transcoder)
1521 {
1522 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1523 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1524 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1525 
1526 	intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
1527 		       intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
1528 	intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
1529 		       intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
1530 	intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
1531 		       intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
1532 
1533 	intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
1534 		       intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
1535 	intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
1536 		       intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
1537 	intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
1538 		       intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
1539 	intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
1540 		       intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
1541 }
1542 
1543 /*
1544  * Finds the encoder associated with the given CRTC. This can only be
1545  * used when we know that the CRTC isn't feeding multiple encoders!
1546  */
1547 struct intel_encoder *
1548 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
1549 			   const struct intel_crtc_state *crtc_state)
1550 {
1551 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1552 	const struct drm_connector_state *connector_state;
1553 	const struct drm_connector *connector;
1554 	struct intel_encoder *encoder = NULL;
1555 	int num_encoders = 0;
1556 	int i;
1557 
1558 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
1559 		if (connector_state->crtc != &crtc->base)
1560 			continue;
1561 
1562 		encoder = to_intel_encoder(connector_state->best_encoder);
1563 		num_encoders++;
1564 	}
1565 
1566 	drm_WARN(encoder->base.dev, num_encoders != 1,
1567 		 "%d encoders for pipe %c\n",
1568 		 num_encoders, pipe_name(crtc->pipe));
1569 
1570 	return encoder;
1571 }
1572 
1573 /*
1574  * Enable PCH resources required for PCH ports:
1575  *   - PCH PLLs
1576  *   - FDI training & RX/TX
1577  *   - update transcoder timings
1578  *   - DP transcoding bits
1579  *   - transcoder
1580  */
1581 static void ilk_pch_enable(const struct intel_atomic_state *state,
1582 			   const struct intel_crtc_state *crtc_state)
1583 {
1584 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1585 	struct drm_device *dev = crtc->base.dev;
1586 	struct drm_i915_private *dev_priv = to_i915(dev);
1587 	enum pipe pipe = crtc->pipe;
1588 	u32 temp;
1589 
1590 	assert_pch_transcoder_disabled(dev_priv, pipe);
1591 
1592 	/* For PCH output, training FDI link */
1593 	intel_fdi_link_train(crtc, crtc_state);
1594 
1595 	/* We need to program the right clock selection before writing the pixel
1596 	 * mutliplier into the DPLL. */
1597 	if (HAS_PCH_CPT(dev_priv)) {
1598 		u32 sel;
1599 
1600 		temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
1601 		temp |= TRANS_DPLL_ENABLE(pipe);
1602 		sel = TRANS_DPLLB_SEL(pipe);
1603 		if (crtc_state->shared_dpll ==
1604 		    intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
1605 			temp |= sel;
1606 		else
1607 			temp &= ~sel;
1608 		intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
1609 	}
1610 
1611 	/* XXX: pch pll's can be enabled any time before we enable the PCH
1612 	 * transcoder, and we actually should do this to not upset any PCH
1613 	 * transcoder that already use the clock when we share it.
1614 	 *
1615 	 * Note that enable_shared_dpll tries to do the right thing, but
1616 	 * get_shared_dpll unconditionally resets the pll - we need that to have
1617 	 * the right LVDS enable sequence. */
1618 	intel_enable_shared_dpll(crtc_state);
1619 
1620 	/* set transcoder timing, panel must allow it */
1621 	assert_pps_unlocked(dev_priv, pipe);
1622 	ilk_pch_transcoder_set_timings(crtc_state, pipe);
1623 
1624 	intel_fdi_normal_train(crtc);
1625 
1626 	/* For PCH DP, enable TRANS_DP_CTL */
1627 	if (HAS_PCH_CPT(dev_priv) &&
1628 	    intel_crtc_has_dp_encoder(crtc_state)) {
1629 		const struct drm_display_mode *adjusted_mode =
1630 			&crtc_state->hw.adjusted_mode;
1631 		u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
1632 		i915_reg_t reg = TRANS_DP_CTL(pipe);
1633 		enum port port;
1634 
1635 		temp = intel_de_read(dev_priv, reg);
1636 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
1637 			  TRANS_DP_SYNC_MASK |
1638 			  TRANS_DP_BPC_MASK);
1639 		temp |= TRANS_DP_OUTPUT_ENABLE;
1640 		temp |= bpc << 9; /* same format but at 11:9 */
1641 
1642 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
1643 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
1644 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
1645 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
1646 
1647 		port = intel_get_crtc_new_encoder(state, crtc_state)->port;
1648 		drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
1649 		temp |= TRANS_DP_PORT_SEL(port);
1650 
1651 		intel_de_write(dev_priv, reg, temp);
1652 	}
1653 
1654 	ilk_enable_pch_transcoder(crtc_state);
1655 }
1656 
1657 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
1658 {
1659 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1660 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1661 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
1662 
1663 	assert_pch_transcoder_disabled(dev_priv, PIPE_A);
1664 
1665 	lpt_program_iclkip(crtc_state);
1666 
1667 	/* Set transcoder timing. */
1668 	ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
1669 
1670 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
1671 }
1672 
1673 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
1674 			       enum pipe pipe)
1675 {
1676 	i915_reg_t dslreg = PIPEDSL(pipe);
1677 	u32 temp;
1678 
1679 	temp = intel_de_read(dev_priv, dslreg);
1680 	udelay(500);
1681 	if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
1682 		if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
1683 			drm_err(&dev_priv->drm,
1684 				"mode set failed: pipe %c stuck\n",
1685 				pipe_name(pipe));
1686 	}
1687 }
1688 
1689 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
1690 {
1691 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1692 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1693 	const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
1694 	enum pipe pipe = crtc->pipe;
1695 	int width = drm_rect_width(dst);
1696 	int height = drm_rect_height(dst);
1697 	int x = dst->x1;
1698 	int y = dst->y1;
1699 
1700 	if (!crtc_state->pch_pfit.enabled)
1701 		return;
1702 
1703 	/* Force use of hard-coded filter coefficients
1704 	 * as some pre-programmed values are broken,
1705 	 * e.g. x201.
1706 	 */
1707 	if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
1708 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
1709 			       PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
1710 	else
1711 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
1712 			       PF_FILTER_MED_3x3);
1713 	intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
1714 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
1715 }
1716 
1717 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
1718 {
1719 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1720 	struct drm_device *dev = crtc->base.dev;
1721 	struct drm_i915_private *dev_priv = to_i915(dev);
1722 
1723 	if (!crtc_state->ips_enabled)
1724 		return;
1725 
1726 	/*
1727 	 * We can only enable IPS after we enable a plane and wait for a vblank
1728 	 * This function is called from post_plane_update, which is run after
1729 	 * a vblank wait.
1730 	 */
1731 	drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
1732 
1733 	if (IS_BROADWELL(dev_priv)) {
1734 		drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
1735 							 IPS_ENABLE | IPS_PCODE_CONTROL));
1736 		/* Quoting Art Runyan: "its not safe to expect any particular
1737 		 * value in IPS_CTL bit 31 after enabling IPS through the
1738 		 * mailbox." Moreover, the mailbox may return a bogus state,
1739 		 * so we need to just enable it and continue on.
1740 		 */
1741 	} else {
1742 		intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
1743 		/* The bit only becomes 1 in the next vblank, so this wait here
1744 		 * is essentially intel_wait_for_vblank. If we don't have this
1745 		 * and don't wait for vblanks until the end of crtc_enable, then
1746 		 * the HW state readout code will complain that the expected
1747 		 * IPS_CTL value is not the one we read. */
1748 		if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
1749 			drm_err(&dev_priv->drm,
1750 				"Timed out waiting for IPS enable\n");
1751 	}
1752 }
1753 
1754 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
1755 {
1756 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
1757 	struct drm_device *dev = crtc->base.dev;
1758 	struct drm_i915_private *dev_priv = to_i915(dev);
1759 
1760 	if (!crtc_state->ips_enabled)
1761 		return;
1762 
1763 	if (IS_BROADWELL(dev_priv)) {
1764 		drm_WARN_ON(dev,
1765 			    sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
1766 		/*
1767 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
1768 		 * 42ms timeout value leads to occasional timeouts so use 100ms
1769 		 * instead.
1770 		 */
1771 		if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
1772 			drm_err(&dev_priv->drm,
1773 				"Timed out waiting for IPS disable\n");
1774 	} else {
1775 		intel_de_write(dev_priv, IPS_CTL, 0);
1776 		intel_de_posting_read(dev_priv, IPS_CTL);
1777 	}
1778 
1779 	/* We need to wait for a vblank before we can disable the plane. */
1780 	intel_wait_for_vblank(dev_priv, crtc->pipe);
1781 }
1782 
1783 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *crtc)
1784 {
1785 	if (crtc->overlay)
1786 		(void) intel_overlay_switch_off(crtc->overlay);
1787 
1788 	/* Let userspace switch the overlay on again. In most cases userspace
1789 	 * has to recompute where to put it anyway.
1790 	 */
1791 }
1792 
1793 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
1794 				       const struct intel_crtc_state *new_crtc_state)
1795 {
1796 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1797 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1798 
1799 	if (!old_crtc_state->ips_enabled)
1800 		return false;
1801 
1802 	if (intel_crtc_needs_modeset(new_crtc_state))
1803 		return true;
1804 
1805 	/*
1806 	 * Workaround : Do not read or write the pipe palette/gamma data while
1807 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
1808 	 *
1809 	 * Disable IPS before we program the LUT.
1810 	 */
1811 	if (IS_HASWELL(dev_priv) &&
1812 	    (new_crtc_state->uapi.color_mgmt_changed ||
1813 	     new_crtc_state->update_pipe) &&
1814 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
1815 		return true;
1816 
1817 	return !new_crtc_state->ips_enabled;
1818 }
1819 
1820 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
1821 				       const struct intel_crtc_state *new_crtc_state)
1822 {
1823 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
1824 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
1825 
1826 	if (!new_crtc_state->ips_enabled)
1827 		return false;
1828 
1829 	if (intel_crtc_needs_modeset(new_crtc_state))
1830 		return true;
1831 
1832 	/*
1833 	 * Workaround : Do not read or write the pipe palette/gamma data while
1834 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
1835 	 *
1836 	 * Re-enable IPS after the LUT has been programmed.
1837 	 */
1838 	if (IS_HASWELL(dev_priv) &&
1839 	    (new_crtc_state->uapi.color_mgmt_changed ||
1840 	     new_crtc_state->update_pipe) &&
1841 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
1842 		return true;
1843 
1844 	/*
1845 	 * We can't read out IPS on broadwell, assume the worst and
1846 	 * forcibly enable IPS on the first fastset.
1847 	 */
1848 	if (new_crtc_state->update_pipe && old_crtc_state->inherited)
1849 		return true;
1850 
1851 	return !old_crtc_state->ips_enabled;
1852 }
1853 
1854 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
1855 {
1856 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1857 
1858 	if (!crtc_state->nv12_planes)
1859 		return false;
1860 
1861 	/* WA Display #0827: Gen9:all */
1862 	if (DISPLAY_VER(dev_priv) == 9)
1863 		return true;
1864 
1865 	return false;
1866 }
1867 
1868 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
1869 {
1870 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1871 
1872 	/* Wa_2006604312:icl,ehl */
1873 	if (crtc_state->scaler_state.scaler_users > 0 && DISPLAY_VER(dev_priv) == 11)
1874 		return true;
1875 
1876 	return false;
1877 }
1878 
1879 static bool needs_cursorclk_wa(const struct intel_crtc_state *crtc_state)
1880 {
1881 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
1882 
1883 	/* Wa_1604331009:icl,jsl,ehl */
1884 	if (is_hdr_mode(crtc_state) &&
1885 	    crtc_state->active_planes & BIT(PLANE_CURSOR) &&
1886 	    DISPLAY_VER(dev_priv) == 11)
1887 		return true;
1888 
1889 	return false;
1890 }
1891 
1892 static void intel_async_flip_vtd_wa(struct drm_i915_private *i915,
1893 				    enum pipe pipe, bool enable)
1894 {
1895 	if (DISPLAY_VER(i915) == 9) {
1896 		/*
1897 		 * "Plane N strech max must be programmed to 11b (x1)
1898 		 *  when Async flips are enabled on that plane."
1899 		 */
1900 		intel_de_rmw(i915, CHICKEN_PIPESL_1(pipe),
1901 			     SKL_PLANE1_STRETCH_MAX_MASK,
1902 			     enable ? SKL_PLANE1_STRETCH_MAX_X1 : SKL_PLANE1_STRETCH_MAX_X8);
1903 	} else {
1904 		/* Also needed on HSW/BDW albeit undocumented */
1905 		intel_de_rmw(i915, CHICKEN_PIPESL_1(pipe),
1906 			     HSW_PRI_STRETCH_MAX_MASK,
1907 			     enable ? HSW_PRI_STRETCH_MAX_X1 : HSW_PRI_STRETCH_MAX_X8);
1908 	}
1909 }
1910 
1911 static bool needs_async_flip_vtd_wa(const struct intel_crtc_state *crtc_state)
1912 {
1913 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
1914 
1915 	return crtc_state->uapi.async_flip && intel_vtd_active() &&
1916 		(DISPLAY_VER(i915) == 9 || IS_BROADWELL(i915) || IS_HASWELL(i915));
1917 }
1918 
1919 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
1920 			    const struct intel_crtc_state *new_crtc_state)
1921 {
1922 	return (!old_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state)) &&
1923 		new_crtc_state->active_planes;
1924 }
1925 
1926 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
1927 			     const struct intel_crtc_state *new_crtc_state)
1928 {
1929 	return old_crtc_state->active_planes &&
1930 		(!new_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state));
1931 }
1932 
1933 static void intel_post_plane_update(struct intel_atomic_state *state,
1934 				    struct intel_crtc *crtc)
1935 {
1936 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
1937 	const struct intel_crtc_state *old_crtc_state =
1938 		intel_atomic_get_old_crtc_state(state, crtc);
1939 	const struct intel_crtc_state *new_crtc_state =
1940 		intel_atomic_get_new_crtc_state(state, crtc);
1941 	enum pipe pipe = crtc->pipe;
1942 
1943 	intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
1944 
1945 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
1946 		intel_update_watermarks(dev_priv);
1947 
1948 	if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
1949 		hsw_enable_ips(new_crtc_state);
1950 
1951 	intel_fbc_post_update(state, crtc);
1952 	intel_drrs_page_flip(state, crtc);
1953 
1954 	if (needs_async_flip_vtd_wa(old_crtc_state) &&
1955 	    !needs_async_flip_vtd_wa(new_crtc_state))
1956 		intel_async_flip_vtd_wa(dev_priv, pipe, false);
1957 
1958 	if (needs_nv12_wa(old_crtc_state) &&
1959 	    !needs_nv12_wa(new_crtc_state))
1960 		skl_wa_827(dev_priv, pipe, false);
1961 
1962 	if (needs_scalerclk_wa(old_crtc_state) &&
1963 	    !needs_scalerclk_wa(new_crtc_state))
1964 		icl_wa_scalerclkgating(dev_priv, pipe, false);
1965 
1966 	if (needs_cursorclk_wa(old_crtc_state) &&
1967 	    !needs_cursorclk_wa(new_crtc_state))
1968 		icl_wa_cursorclkgating(dev_priv, pipe, false);
1969 
1970 }
1971 
1972 static void intel_crtc_enable_flip_done(struct intel_atomic_state *state,
1973 					struct intel_crtc *crtc)
1974 {
1975 	const struct intel_crtc_state *crtc_state =
1976 		intel_atomic_get_new_crtc_state(state, crtc);
1977 	u8 update_planes = crtc_state->update_planes;
1978 	const struct intel_plane_state *plane_state;
1979 	struct intel_plane *plane;
1980 	int i;
1981 
1982 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
1983 		if (plane->enable_flip_done &&
1984 		    plane->pipe == crtc->pipe &&
1985 		    update_planes & BIT(plane->id))
1986 			plane->enable_flip_done(plane);
1987 	}
1988 }
1989 
1990 static void intel_crtc_disable_flip_done(struct intel_atomic_state *state,
1991 					 struct intel_crtc *crtc)
1992 {
1993 	const struct intel_crtc_state *crtc_state =
1994 		intel_atomic_get_new_crtc_state(state, crtc);
1995 	u8 update_planes = crtc_state->update_planes;
1996 	const struct intel_plane_state *plane_state;
1997 	struct intel_plane *plane;
1998 	int i;
1999 
2000 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
2001 		if (plane->disable_flip_done &&
2002 		    plane->pipe == crtc->pipe &&
2003 		    update_planes & BIT(plane->id))
2004 			plane->disable_flip_done(plane);
2005 	}
2006 }
2007 
2008 static void intel_crtc_async_flip_disable_wa(struct intel_atomic_state *state,
2009 					     struct intel_crtc *crtc)
2010 {
2011 	struct drm_i915_private *i915 = to_i915(state->base.dev);
2012 	const struct intel_crtc_state *old_crtc_state =
2013 		intel_atomic_get_old_crtc_state(state, crtc);
2014 	const struct intel_crtc_state *new_crtc_state =
2015 		intel_atomic_get_new_crtc_state(state, crtc);
2016 	u8 update_planes = new_crtc_state->update_planes;
2017 	const struct intel_plane_state *old_plane_state;
2018 	struct intel_plane *plane;
2019 	bool need_vbl_wait = false;
2020 	int i;
2021 
2022 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
2023 		if (plane->need_async_flip_disable_wa &&
2024 		    plane->pipe == crtc->pipe &&
2025 		    update_planes & BIT(plane->id)) {
2026 			/*
2027 			 * Apart from the async flip bit we want to
2028 			 * preserve the old state for the plane.
2029 			 */
2030 			plane->async_flip(plane, old_crtc_state,
2031 					  old_plane_state, false);
2032 			need_vbl_wait = true;
2033 		}
2034 	}
2035 
2036 	if (need_vbl_wait)
2037 		intel_wait_for_vblank(i915, crtc->pipe);
2038 }
2039 
2040 static void intel_pre_plane_update(struct intel_atomic_state *state,
2041 				   struct intel_crtc *crtc)
2042 {
2043 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
2044 	const struct intel_crtc_state *old_crtc_state =
2045 		intel_atomic_get_old_crtc_state(state, crtc);
2046 	const struct intel_crtc_state *new_crtc_state =
2047 		intel_atomic_get_new_crtc_state(state, crtc);
2048 	enum pipe pipe = crtc->pipe;
2049 
2050 	if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
2051 		hsw_disable_ips(old_crtc_state);
2052 
2053 	if (intel_fbc_pre_update(state, crtc))
2054 		intel_wait_for_vblank(dev_priv, pipe);
2055 
2056 	if (!needs_async_flip_vtd_wa(old_crtc_state) &&
2057 	    needs_async_flip_vtd_wa(new_crtc_state))
2058 		intel_async_flip_vtd_wa(dev_priv, pipe, true);
2059 
2060 	/* Display WA 827 */
2061 	if (!needs_nv12_wa(old_crtc_state) &&
2062 	    needs_nv12_wa(new_crtc_state))
2063 		skl_wa_827(dev_priv, pipe, true);
2064 
2065 	/* Wa_2006604312:icl,ehl */
2066 	if (!needs_scalerclk_wa(old_crtc_state) &&
2067 	    needs_scalerclk_wa(new_crtc_state))
2068 		icl_wa_scalerclkgating(dev_priv, pipe, true);
2069 
2070 	/* Wa_1604331009:icl,jsl,ehl */
2071 	if (!needs_cursorclk_wa(old_crtc_state) &&
2072 	    needs_cursorclk_wa(new_crtc_state))
2073 		icl_wa_cursorclkgating(dev_priv, pipe, true);
2074 
2075 	/*
2076 	 * Vblank time updates from the shadow to live plane control register
2077 	 * are blocked if the memory self-refresh mode is active at that
2078 	 * moment. So to make sure the plane gets truly disabled, disable
2079 	 * first the self-refresh mode. The self-refresh enable bit in turn
2080 	 * will be checked/applied by the HW only at the next frame start
2081 	 * event which is after the vblank start event, so we need to have a
2082 	 * wait-for-vblank between disabling the plane and the pipe.
2083 	 */
2084 	if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
2085 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
2086 		intel_wait_for_vblank(dev_priv, pipe);
2087 
2088 	/*
2089 	 * IVB workaround: must disable low power watermarks for at least
2090 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
2091 	 * when scaling is disabled.
2092 	 *
2093 	 * WaCxSRDisabledForSpriteScaling:ivb
2094 	 */
2095 	if (old_crtc_state->hw.active &&
2096 	    new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
2097 		intel_wait_for_vblank(dev_priv, pipe);
2098 
2099 	/*
2100 	 * If we're doing a modeset we don't need to do any
2101 	 * pre-vblank watermark programming here.
2102 	 */
2103 	if (!intel_crtc_needs_modeset(new_crtc_state)) {
2104 		/*
2105 		 * For platforms that support atomic watermarks, program the
2106 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
2107 		 * will be the intermediate values that are safe for both pre- and
2108 		 * post- vblank; when vblank happens, the 'active' values will be set
2109 		 * to the final 'target' values and we'll do this again to get the
2110 		 * optimal watermarks.  For gen9+ platforms, the values we program here
2111 		 * will be the final target values which will get automatically latched
2112 		 * at vblank time; no further programming will be necessary.
2113 		 *
2114 		 * If a platform hasn't been transitioned to atomic watermarks yet,
2115 		 * we'll continue to update watermarks the old way, if flags tell
2116 		 * us to.
2117 		 */
2118 		if (!intel_initial_watermarks(state, crtc))
2119 			if (new_crtc_state->update_wm_pre)
2120 				intel_update_watermarks(dev_priv);
2121 	}
2122 
2123 	/*
2124 	 * Gen2 reports pipe underruns whenever all planes are disabled.
2125 	 * So disable underrun reporting before all the planes get disabled.
2126 	 *
2127 	 * We do this after .initial_watermarks() so that we have a
2128 	 * chance of catching underruns with the intermediate watermarks
2129 	 * vs. the old plane configuration.
2130 	 */
2131 	if (DISPLAY_VER(dev_priv) == 2 && planes_disabling(old_crtc_state, new_crtc_state))
2132 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
2133 
2134 	/*
2135 	 * WA for platforms where async address update enable bit
2136 	 * is double buffered and only latched at start of vblank.
2137 	 */
2138 	if (old_crtc_state->uapi.async_flip && !new_crtc_state->uapi.async_flip)
2139 		intel_crtc_async_flip_disable_wa(state, crtc);
2140 }
2141 
2142 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
2143 				      struct intel_crtc *crtc)
2144 {
2145 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2146 	const struct intel_crtc_state *new_crtc_state =
2147 		intel_atomic_get_new_crtc_state(state, crtc);
2148 	unsigned int update_mask = new_crtc_state->update_planes;
2149 	const struct intel_plane_state *old_plane_state;
2150 	struct intel_plane *plane;
2151 	unsigned fb_bits = 0;
2152 	int i;
2153 
2154 	intel_crtc_dpms_overlay_disable(crtc);
2155 
2156 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
2157 		if (crtc->pipe != plane->pipe ||
2158 		    !(update_mask & BIT(plane->id)))
2159 			continue;
2160 
2161 		intel_disable_plane(plane, new_crtc_state);
2162 
2163 		if (old_plane_state->uapi.visible)
2164 			fb_bits |= plane->frontbuffer_bit;
2165 	}
2166 
2167 	intel_frontbuffer_flip(dev_priv, fb_bits);
2168 }
2169 
2170 /*
2171  * intel_connector_primary_encoder - get the primary encoder for a connector
2172  * @connector: connector for which to return the encoder
2173  *
2174  * Returns the primary encoder for a connector. There is a 1:1 mapping from
2175  * all connectors to their encoder, except for DP-MST connectors which have
2176  * both a virtual and a primary encoder. These DP-MST primary encoders can be
2177  * pointed to by as many DP-MST connectors as there are pipes.
2178  */
2179 static struct intel_encoder *
2180 intel_connector_primary_encoder(struct intel_connector *connector)
2181 {
2182 	struct intel_encoder *encoder;
2183 
2184 	if (connector->mst_port)
2185 		return &dp_to_dig_port(connector->mst_port)->base;
2186 
2187 	encoder = intel_attached_encoder(connector);
2188 	drm_WARN_ON(connector->base.dev, !encoder);
2189 
2190 	return encoder;
2191 }
2192 
2193 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
2194 {
2195 	struct drm_connector_state *new_conn_state;
2196 	struct drm_connector *connector;
2197 	int i;
2198 
2199 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
2200 					i) {
2201 		struct intel_connector *intel_connector;
2202 		struct intel_encoder *encoder;
2203 		struct intel_crtc *crtc;
2204 
2205 		if (!intel_connector_needs_modeset(state, connector))
2206 			continue;
2207 
2208 		intel_connector = to_intel_connector(connector);
2209 		encoder = intel_connector_primary_encoder(intel_connector);
2210 		if (!encoder->update_prepare)
2211 			continue;
2212 
2213 		crtc = new_conn_state->crtc ?
2214 			to_intel_crtc(new_conn_state->crtc) : NULL;
2215 		encoder->update_prepare(state, encoder, crtc);
2216 	}
2217 }
2218 
2219 static void intel_encoders_update_complete(struct intel_atomic_state *state)
2220 {
2221 	struct drm_connector_state *new_conn_state;
2222 	struct drm_connector *connector;
2223 	int i;
2224 
2225 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
2226 					i) {
2227 		struct intel_connector *intel_connector;
2228 		struct intel_encoder *encoder;
2229 		struct intel_crtc *crtc;
2230 
2231 		if (!intel_connector_needs_modeset(state, connector))
2232 			continue;
2233 
2234 		intel_connector = to_intel_connector(connector);
2235 		encoder = intel_connector_primary_encoder(intel_connector);
2236 		if (!encoder->update_complete)
2237 			continue;
2238 
2239 		crtc = new_conn_state->crtc ?
2240 			to_intel_crtc(new_conn_state->crtc) : NULL;
2241 		encoder->update_complete(state, encoder, crtc);
2242 	}
2243 }
2244 
2245 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
2246 					  struct intel_crtc *crtc)
2247 {
2248 	const struct intel_crtc_state *crtc_state =
2249 		intel_atomic_get_new_crtc_state(state, crtc);
2250 	const struct drm_connector_state *conn_state;
2251 	struct drm_connector *conn;
2252 	int i;
2253 
2254 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2255 		struct intel_encoder *encoder =
2256 			to_intel_encoder(conn_state->best_encoder);
2257 
2258 		if (conn_state->crtc != &crtc->base)
2259 			continue;
2260 
2261 		if (encoder->pre_pll_enable)
2262 			encoder->pre_pll_enable(state, encoder,
2263 						crtc_state, conn_state);
2264 	}
2265 }
2266 
2267 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
2268 				      struct intel_crtc *crtc)
2269 {
2270 	const struct intel_crtc_state *crtc_state =
2271 		intel_atomic_get_new_crtc_state(state, crtc);
2272 	const struct drm_connector_state *conn_state;
2273 	struct drm_connector *conn;
2274 	int i;
2275 
2276 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2277 		struct intel_encoder *encoder =
2278 			to_intel_encoder(conn_state->best_encoder);
2279 
2280 		if (conn_state->crtc != &crtc->base)
2281 			continue;
2282 
2283 		if (encoder->pre_enable)
2284 			encoder->pre_enable(state, encoder,
2285 					    crtc_state, conn_state);
2286 	}
2287 }
2288 
2289 static void intel_encoders_enable(struct intel_atomic_state *state,
2290 				  struct intel_crtc *crtc)
2291 {
2292 	const struct intel_crtc_state *crtc_state =
2293 		intel_atomic_get_new_crtc_state(state, crtc);
2294 	const struct drm_connector_state *conn_state;
2295 	struct drm_connector *conn;
2296 	int i;
2297 
2298 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2299 		struct intel_encoder *encoder =
2300 			to_intel_encoder(conn_state->best_encoder);
2301 
2302 		if (conn_state->crtc != &crtc->base)
2303 			continue;
2304 
2305 		if (encoder->enable)
2306 			encoder->enable(state, encoder,
2307 					crtc_state, conn_state);
2308 		intel_opregion_notify_encoder(encoder, true);
2309 	}
2310 }
2311 
2312 static void intel_encoders_pre_disable(struct intel_atomic_state *state,
2313 				       struct intel_crtc *crtc)
2314 {
2315 	const struct intel_crtc_state *old_crtc_state =
2316 		intel_atomic_get_old_crtc_state(state, crtc);
2317 	const struct drm_connector_state *old_conn_state;
2318 	struct drm_connector *conn;
2319 	int i;
2320 
2321 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
2322 		struct intel_encoder *encoder =
2323 			to_intel_encoder(old_conn_state->best_encoder);
2324 
2325 		if (old_conn_state->crtc != &crtc->base)
2326 			continue;
2327 
2328 		if (encoder->pre_disable)
2329 			encoder->pre_disable(state, encoder, old_crtc_state,
2330 					     old_conn_state);
2331 	}
2332 }
2333 
2334 static void intel_encoders_disable(struct intel_atomic_state *state,
2335 				   struct intel_crtc *crtc)
2336 {
2337 	const struct intel_crtc_state *old_crtc_state =
2338 		intel_atomic_get_old_crtc_state(state, crtc);
2339 	const struct drm_connector_state *old_conn_state;
2340 	struct drm_connector *conn;
2341 	int i;
2342 
2343 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
2344 		struct intel_encoder *encoder =
2345 			to_intel_encoder(old_conn_state->best_encoder);
2346 
2347 		if (old_conn_state->crtc != &crtc->base)
2348 			continue;
2349 
2350 		intel_opregion_notify_encoder(encoder, false);
2351 		if (encoder->disable)
2352 			encoder->disable(state, encoder,
2353 					 old_crtc_state, old_conn_state);
2354 	}
2355 }
2356 
2357 static void intel_encoders_post_disable(struct intel_atomic_state *state,
2358 					struct intel_crtc *crtc)
2359 {
2360 	const struct intel_crtc_state *old_crtc_state =
2361 		intel_atomic_get_old_crtc_state(state, crtc);
2362 	const struct drm_connector_state *old_conn_state;
2363 	struct drm_connector *conn;
2364 	int i;
2365 
2366 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
2367 		struct intel_encoder *encoder =
2368 			to_intel_encoder(old_conn_state->best_encoder);
2369 
2370 		if (old_conn_state->crtc != &crtc->base)
2371 			continue;
2372 
2373 		if (encoder->post_disable)
2374 			encoder->post_disable(state, encoder,
2375 					      old_crtc_state, old_conn_state);
2376 	}
2377 }
2378 
2379 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
2380 					    struct intel_crtc *crtc)
2381 {
2382 	const struct intel_crtc_state *old_crtc_state =
2383 		intel_atomic_get_old_crtc_state(state, crtc);
2384 	const struct drm_connector_state *old_conn_state;
2385 	struct drm_connector *conn;
2386 	int i;
2387 
2388 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
2389 		struct intel_encoder *encoder =
2390 			to_intel_encoder(old_conn_state->best_encoder);
2391 
2392 		if (old_conn_state->crtc != &crtc->base)
2393 			continue;
2394 
2395 		if (encoder->post_pll_disable)
2396 			encoder->post_pll_disable(state, encoder,
2397 						  old_crtc_state, old_conn_state);
2398 	}
2399 }
2400 
2401 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
2402 				       struct intel_crtc *crtc)
2403 {
2404 	const struct intel_crtc_state *crtc_state =
2405 		intel_atomic_get_new_crtc_state(state, crtc);
2406 	const struct drm_connector_state *conn_state;
2407 	struct drm_connector *conn;
2408 	int i;
2409 
2410 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2411 		struct intel_encoder *encoder =
2412 			to_intel_encoder(conn_state->best_encoder);
2413 
2414 		if (conn_state->crtc != &crtc->base)
2415 			continue;
2416 
2417 		if (encoder->update_pipe)
2418 			encoder->update_pipe(state, encoder,
2419 					     crtc_state, conn_state);
2420 	}
2421 }
2422 
2423 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
2424 {
2425 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2426 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
2427 
2428 	plane->disable_plane(plane, crtc_state);
2429 }
2430 
2431 static void ilk_crtc_enable(struct intel_atomic_state *state,
2432 			    struct intel_crtc *crtc)
2433 {
2434 	const struct intel_crtc_state *new_crtc_state =
2435 		intel_atomic_get_new_crtc_state(state, crtc);
2436 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2437 	enum pipe pipe = crtc->pipe;
2438 
2439 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
2440 		return;
2441 
2442 	/*
2443 	 * Sometimes spurious CPU pipe underruns happen during FDI
2444 	 * training, at least with VGA+HDMI cloning. Suppress them.
2445 	 *
2446 	 * On ILK we get an occasional spurious CPU pipe underruns
2447 	 * between eDP port A enable and vdd enable. Also PCH port
2448 	 * enable seems to result in the occasional CPU pipe underrun.
2449 	 *
2450 	 * Spurious PCH underruns also occur during PCH enabling.
2451 	 */
2452 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
2453 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
2454 
2455 	if (intel_crtc_has_dp_encoder(new_crtc_state))
2456 		intel_dp_set_m_n(new_crtc_state, M1_N1);
2457 
2458 	intel_set_transcoder_timings(new_crtc_state);
2459 	intel_set_pipe_src_size(new_crtc_state);
2460 
2461 	if (new_crtc_state->has_pch_encoder)
2462 		intel_cpu_transcoder_set_m_n(new_crtc_state,
2463 					     &new_crtc_state->fdi_m_n, NULL);
2464 
2465 	ilk_set_pipeconf(new_crtc_state);
2466 
2467 	crtc->active = true;
2468 
2469 	intel_encoders_pre_enable(state, crtc);
2470 
2471 	if (new_crtc_state->has_pch_encoder) {
2472 		/* Note: FDI PLL enabling _must_ be done before we enable the
2473 		 * cpu pipes, hence this is separate from all the other fdi/pch
2474 		 * enabling. */
2475 		ilk_fdi_pll_enable(new_crtc_state);
2476 	} else {
2477 		assert_fdi_tx_disabled(dev_priv, pipe);
2478 		assert_fdi_rx_disabled(dev_priv, pipe);
2479 	}
2480 
2481 	ilk_pfit_enable(new_crtc_state);
2482 
2483 	/*
2484 	 * On ILK+ LUT must be loaded before the pipe is running but with
2485 	 * clocks enabled
2486 	 */
2487 	intel_color_load_luts(new_crtc_state);
2488 	intel_color_commit(new_crtc_state);
2489 	/* update DSPCNTR to configure gamma for pipe bottom color */
2490 	intel_disable_primary_plane(new_crtc_state);
2491 
2492 	intel_initial_watermarks(state, crtc);
2493 	intel_enable_transcoder(new_crtc_state);
2494 
2495 	if (new_crtc_state->has_pch_encoder)
2496 		ilk_pch_enable(state, new_crtc_state);
2497 
2498 	intel_crtc_vblank_on(new_crtc_state);
2499 
2500 	intel_encoders_enable(state, crtc);
2501 
2502 	if (HAS_PCH_CPT(dev_priv))
2503 		cpt_verify_modeset(dev_priv, pipe);
2504 
2505 	/*
2506 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
2507 	 * And a second vblank wait is needed at least on ILK with
2508 	 * some interlaced HDMI modes. Let's do the double wait always
2509 	 * in case there are more corner cases we don't know about.
2510 	 */
2511 	if (new_crtc_state->has_pch_encoder) {
2512 		intel_wait_for_vblank(dev_priv, pipe);
2513 		intel_wait_for_vblank(dev_priv, pipe);
2514 	}
2515 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2516 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
2517 }
2518 
2519 /* IPS only exists on ULT machines and is tied to pipe A. */
2520 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
2521 {
2522 	return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
2523 }
2524 
2525 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
2526 					    enum pipe pipe, bool apply)
2527 {
2528 	u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
2529 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
2530 
2531 	if (apply)
2532 		val |= mask;
2533 	else
2534 		val &= ~mask;
2535 
2536 	intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
2537 }
2538 
2539 static void icl_pipe_mbus_enable(struct intel_crtc *crtc, bool joined_mbus)
2540 {
2541 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2542 	enum pipe pipe = crtc->pipe;
2543 	u32 val;
2544 
2545 	/* Wa_22010947358:adl-p */
2546 	if (IS_ALDERLAKE_P(dev_priv))
2547 		val = joined_mbus ? MBUS_DBOX_A_CREDIT(6) : MBUS_DBOX_A_CREDIT(4);
2548 	else
2549 		val = MBUS_DBOX_A_CREDIT(2);
2550 
2551 	if (DISPLAY_VER(dev_priv) >= 12) {
2552 		val |= MBUS_DBOX_BW_CREDIT(2);
2553 		val |= MBUS_DBOX_B_CREDIT(12);
2554 	} else {
2555 		val |= MBUS_DBOX_BW_CREDIT(1);
2556 		val |= MBUS_DBOX_B_CREDIT(8);
2557 	}
2558 
2559 	intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
2560 }
2561 
2562 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
2563 {
2564 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2565 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2566 
2567 	intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
2568 		       HSW_LINETIME(crtc_state->linetime) |
2569 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
2570 }
2571 
2572 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
2573 {
2574 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2575 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2576 	i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
2577 	u32 val;
2578 
2579 	val = intel_de_read(dev_priv, reg);
2580 	val &= ~HSW_FRAME_START_DELAY_MASK;
2581 	val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
2582 	intel_de_write(dev_priv, reg, val);
2583 }
2584 
2585 static void icl_ddi_bigjoiner_pre_enable(struct intel_atomic_state *state,
2586 					 const struct intel_crtc_state *crtc_state)
2587 {
2588 	struct intel_crtc *master = to_intel_crtc(crtc_state->uapi.crtc);
2589 	struct drm_i915_private *dev_priv = to_i915(master->base.dev);
2590 	struct intel_crtc_state *master_crtc_state;
2591 	struct drm_connector_state *conn_state;
2592 	struct drm_connector *conn;
2593 	struct intel_encoder *encoder = NULL;
2594 	int i;
2595 
2596 	if (crtc_state->bigjoiner_slave)
2597 		master = crtc_state->bigjoiner_linked_crtc;
2598 
2599 	master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
2600 
2601 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
2602 		if (conn_state->crtc != &master->base)
2603 			continue;
2604 
2605 		encoder = to_intel_encoder(conn_state->best_encoder);
2606 		break;
2607 	}
2608 
2609 	if (!crtc_state->bigjoiner_slave) {
2610 		/* need to enable VDSC, which we skipped in pre-enable */
2611 		intel_dsc_enable(encoder, crtc_state);
2612 	} else {
2613 		/*
2614 		 * Enable sequence steps 1-7 on bigjoiner master
2615 		 */
2616 		intel_encoders_pre_pll_enable(state, master);
2617 		if (master_crtc_state->shared_dpll)
2618 			intel_enable_shared_dpll(master_crtc_state);
2619 		intel_encoders_pre_enable(state, master);
2620 
2621 		/* and DSC on slave */
2622 		intel_dsc_enable(NULL, crtc_state);
2623 	}
2624 
2625 	if (DISPLAY_VER(dev_priv) >= 13)
2626 		intel_uncompressed_joiner_enable(crtc_state);
2627 }
2628 
2629 static void hsw_crtc_enable(struct intel_atomic_state *state,
2630 			    struct intel_crtc *crtc)
2631 {
2632 	const struct intel_crtc_state *new_crtc_state =
2633 		intel_atomic_get_new_crtc_state(state, crtc);
2634 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2635 	enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
2636 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
2637 	bool psl_clkgate_wa;
2638 
2639 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
2640 		return;
2641 
2642 	if (!new_crtc_state->bigjoiner) {
2643 		intel_encoders_pre_pll_enable(state, crtc);
2644 
2645 		if (new_crtc_state->shared_dpll)
2646 			intel_enable_shared_dpll(new_crtc_state);
2647 
2648 		intel_encoders_pre_enable(state, crtc);
2649 	} else {
2650 		icl_ddi_bigjoiner_pre_enable(state, new_crtc_state);
2651 	}
2652 
2653 	intel_set_pipe_src_size(new_crtc_state);
2654 	if (DISPLAY_VER(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
2655 		bdw_set_pipemisc(new_crtc_state);
2656 
2657 	if (!new_crtc_state->bigjoiner_slave && !transcoder_is_dsi(cpu_transcoder)) {
2658 		intel_set_transcoder_timings(new_crtc_state);
2659 
2660 		if (cpu_transcoder != TRANSCODER_EDP)
2661 			intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
2662 				       new_crtc_state->pixel_multiplier - 1);
2663 
2664 		if (new_crtc_state->has_pch_encoder)
2665 			intel_cpu_transcoder_set_m_n(new_crtc_state,
2666 						     &new_crtc_state->fdi_m_n, NULL);
2667 
2668 		hsw_set_frame_start_delay(new_crtc_state);
2669 
2670 		hsw_set_transconf(new_crtc_state);
2671 	}
2672 
2673 	crtc->active = true;
2674 
2675 	/* Display WA #1180: WaDisableScalarClockGating: glk */
2676 	psl_clkgate_wa = DISPLAY_VER(dev_priv) == 10 &&
2677 		new_crtc_state->pch_pfit.enabled;
2678 	if (psl_clkgate_wa)
2679 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
2680 
2681 	if (DISPLAY_VER(dev_priv) >= 9)
2682 		skl_pfit_enable(new_crtc_state);
2683 	else
2684 		ilk_pfit_enable(new_crtc_state);
2685 
2686 	/*
2687 	 * On ILK+ LUT must be loaded before the pipe is running but with
2688 	 * clocks enabled
2689 	 */
2690 	intel_color_load_luts(new_crtc_state);
2691 	intel_color_commit(new_crtc_state);
2692 	/* update DSPCNTR to configure gamma/csc for pipe bottom color */
2693 	if (DISPLAY_VER(dev_priv) < 9)
2694 		intel_disable_primary_plane(new_crtc_state);
2695 
2696 	hsw_set_linetime_wm(new_crtc_state);
2697 
2698 	if (DISPLAY_VER(dev_priv) >= 11)
2699 		icl_set_pipe_chicken(new_crtc_state);
2700 
2701 	intel_initial_watermarks(state, crtc);
2702 
2703 	if (DISPLAY_VER(dev_priv) >= 11) {
2704 		const struct intel_dbuf_state *dbuf_state =
2705 				intel_atomic_get_new_dbuf_state(state);
2706 
2707 		icl_pipe_mbus_enable(crtc, dbuf_state->joined_mbus);
2708 	}
2709 
2710 	if (new_crtc_state->bigjoiner_slave)
2711 		intel_crtc_vblank_on(new_crtc_state);
2712 
2713 	intel_encoders_enable(state, crtc);
2714 
2715 	if (psl_clkgate_wa) {
2716 		intel_wait_for_vblank(dev_priv, pipe);
2717 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
2718 	}
2719 
2720 	/* If we change the relative order between pipe/planes enabling, we need
2721 	 * to change the workaround. */
2722 	hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
2723 	if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
2724 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
2725 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
2726 	}
2727 }
2728 
2729 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
2730 {
2731 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
2732 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2733 	enum pipe pipe = crtc->pipe;
2734 
2735 	/* To avoid upsetting the power well on haswell only disable the pfit if
2736 	 * it's in use. The hw state code will make sure we get this right. */
2737 	if (!old_crtc_state->pch_pfit.enabled)
2738 		return;
2739 
2740 	intel_de_write(dev_priv, PF_CTL(pipe), 0);
2741 	intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
2742 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
2743 }
2744 
2745 static void ilk_crtc_disable(struct intel_atomic_state *state,
2746 			     struct intel_crtc *crtc)
2747 {
2748 	const struct intel_crtc_state *old_crtc_state =
2749 		intel_atomic_get_old_crtc_state(state, crtc);
2750 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2751 	enum pipe pipe = crtc->pipe;
2752 
2753 	/*
2754 	 * Sometimes spurious CPU pipe underruns happen when the
2755 	 * pipe is already disabled, but FDI RX/TX is still enabled.
2756 	 * Happens at least with VGA+HDMI cloning. Suppress them.
2757 	 */
2758 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
2759 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
2760 
2761 	intel_encoders_disable(state, crtc);
2762 
2763 	intel_crtc_vblank_off(old_crtc_state);
2764 
2765 	intel_disable_transcoder(old_crtc_state);
2766 
2767 	ilk_pfit_disable(old_crtc_state);
2768 
2769 	if (old_crtc_state->has_pch_encoder)
2770 		ilk_fdi_disable(crtc);
2771 
2772 	intel_encoders_post_disable(state, crtc);
2773 
2774 	if (old_crtc_state->has_pch_encoder) {
2775 		ilk_disable_pch_transcoder(dev_priv, pipe);
2776 
2777 		if (HAS_PCH_CPT(dev_priv)) {
2778 			i915_reg_t reg;
2779 			u32 temp;
2780 
2781 			/* disable TRANS_DP_CTL */
2782 			reg = TRANS_DP_CTL(pipe);
2783 			temp = intel_de_read(dev_priv, reg);
2784 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
2785 				  TRANS_DP_PORT_SEL_MASK);
2786 			temp |= TRANS_DP_PORT_SEL_NONE;
2787 			intel_de_write(dev_priv, reg, temp);
2788 
2789 			/* disable DPLL_SEL */
2790 			temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
2791 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
2792 			intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
2793 		}
2794 
2795 		ilk_fdi_pll_disable(crtc);
2796 	}
2797 
2798 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
2799 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
2800 }
2801 
2802 static void hsw_crtc_disable(struct intel_atomic_state *state,
2803 			     struct intel_crtc *crtc)
2804 {
2805 	/*
2806 	 * FIXME collapse everything to one hook.
2807 	 * Need care with mst->ddi interactions.
2808 	 */
2809 	intel_encoders_disable(state, crtc);
2810 	intel_encoders_post_disable(state, crtc);
2811 }
2812 
2813 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
2814 {
2815 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2816 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2817 
2818 	if (!crtc_state->gmch_pfit.control)
2819 		return;
2820 
2821 	/*
2822 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
2823 	 * according to register description and PRM.
2824 	 */
2825 	drm_WARN_ON(&dev_priv->drm,
2826 		    intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
2827 	assert_transcoder_disabled(dev_priv, crtc_state->cpu_transcoder);
2828 
2829 	intel_de_write(dev_priv, PFIT_PGM_RATIOS,
2830 		       crtc_state->gmch_pfit.pgm_ratios);
2831 	intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
2832 
2833 	/* Border color in case we don't scale up to the full screen. Black by
2834 	 * default, change to something else for debugging. */
2835 	intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
2836 }
2837 
2838 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
2839 {
2840 	if (phy == PHY_NONE)
2841 		return false;
2842 	else if (IS_DG2(dev_priv))
2843 		/*
2844 		 * DG2 outputs labelled as "combo PHY" in the bspec use
2845 		 * SNPS PHYs with completely different programming,
2846 		 * hence we always return false here.
2847 		 */
2848 		return false;
2849 	else if (IS_ALDERLAKE_S(dev_priv))
2850 		return phy <= PHY_E;
2851 	else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv))
2852 		return phy <= PHY_D;
2853 	else if (IS_JSL_EHL(dev_priv))
2854 		return phy <= PHY_C;
2855 	else if (DISPLAY_VER(dev_priv) >= 11)
2856 		return phy <= PHY_B;
2857 	else
2858 		return false;
2859 }
2860 
2861 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
2862 {
2863 	if (IS_DG2(dev_priv))
2864 		/* DG2's "TC1" output uses a SNPS PHY */
2865 		return false;
2866 	else if (IS_ALDERLAKE_P(dev_priv))
2867 		return phy >= PHY_F && phy <= PHY_I;
2868 	else if (IS_TIGERLAKE(dev_priv))
2869 		return phy >= PHY_D && phy <= PHY_I;
2870 	else if (IS_ICELAKE(dev_priv))
2871 		return phy >= PHY_C && phy <= PHY_F;
2872 	else
2873 		return false;
2874 }
2875 
2876 bool intel_phy_is_snps(struct drm_i915_private *dev_priv, enum phy phy)
2877 {
2878 	if (phy == PHY_NONE)
2879 		return false;
2880 	else if (IS_DG2(dev_priv))
2881 		/*
2882 		 * All four "combo" ports and the TC1 port (PHY E) use
2883 		 * Synopsis PHYs.
2884 		 */
2885 		return phy <= PHY_E;
2886 
2887 	return false;
2888 }
2889 
2890 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
2891 {
2892 	if (DISPLAY_VER(i915) >= 13 && port >= PORT_D_XELPD)
2893 		return PHY_D + port - PORT_D_XELPD;
2894 	else if (DISPLAY_VER(i915) >= 13 && port >= PORT_TC1)
2895 		return PHY_F + port - PORT_TC1;
2896 	else if (IS_ALDERLAKE_S(i915) && port >= PORT_TC1)
2897 		return PHY_B + port - PORT_TC1;
2898 	else if ((IS_DG1(i915) || IS_ROCKETLAKE(i915)) && port >= PORT_TC1)
2899 		return PHY_C + port - PORT_TC1;
2900 	else if (IS_JSL_EHL(i915) && port == PORT_D)
2901 		return PHY_A;
2902 
2903 	return PHY_A + port - PORT_A;
2904 }
2905 
2906 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
2907 {
2908 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
2909 		return TC_PORT_NONE;
2910 
2911 	if (DISPLAY_VER(dev_priv) >= 12)
2912 		return TC_PORT_1 + port - PORT_TC1;
2913 	else
2914 		return TC_PORT_1 + port - PORT_C;
2915 }
2916 
2917 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
2918 {
2919 	switch (port) {
2920 	case PORT_A:
2921 		return POWER_DOMAIN_PORT_DDI_A_LANES;
2922 	case PORT_B:
2923 		return POWER_DOMAIN_PORT_DDI_B_LANES;
2924 	case PORT_C:
2925 		return POWER_DOMAIN_PORT_DDI_C_LANES;
2926 	case PORT_D:
2927 		return POWER_DOMAIN_PORT_DDI_D_LANES;
2928 	case PORT_E:
2929 		return POWER_DOMAIN_PORT_DDI_E_LANES;
2930 	case PORT_F:
2931 		return POWER_DOMAIN_PORT_DDI_F_LANES;
2932 	case PORT_G:
2933 		return POWER_DOMAIN_PORT_DDI_G_LANES;
2934 	case PORT_H:
2935 		return POWER_DOMAIN_PORT_DDI_H_LANES;
2936 	case PORT_I:
2937 		return POWER_DOMAIN_PORT_DDI_I_LANES;
2938 	default:
2939 		MISSING_CASE(port);
2940 		return POWER_DOMAIN_PORT_OTHER;
2941 	}
2942 }
2943 
2944 enum intel_display_power_domain
2945 intel_aux_power_domain(struct intel_digital_port *dig_port)
2946 {
2947 	if (intel_tc_port_in_tbt_alt_mode(dig_port)) {
2948 		switch (dig_port->aux_ch) {
2949 		case AUX_CH_C:
2950 			return POWER_DOMAIN_AUX_C_TBT;
2951 		case AUX_CH_D:
2952 			return POWER_DOMAIN_AUX_D_TBT;
2953 		case AUX_CH_E:
2954 			return POWER_DOMAIN_AUX_E_TBT;
2955 		case AUX_CH_F:
2956 			return POWER_DOMAIN_AUX_F_TBT;
2957 		case AUX_CH_G:
2958 			return POWER_DOMAIN_AUX_G_TBT;
2959 		case AUX_CH_H:
2960 			return POWER_DOMAIN_AUX_H_TBT;
2961 		case AUX_CH_I:
2962 			return POWER_DOMAIN_AUX_I_TBT;
2963 		default:
2964 			MISSING_CASE(dig_port->aux_ch);
2965 			return POWER_DOMAIN_AUX_C_TBT;
2966 		}
2967 	}
2968 
2969 	return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
2970 }
2971 
2972 /*
2973  * Converts aux_ch to power_domain without caring about TBT ports for that use
2974  * intel_aux_power_domain()
2975  */
2976 enum intel_display_power_domain
2977 intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
2978 {
2979 	switch (aux_ch) {
2980 	case AUX_CH_A:
2981 		return POWER_DOMAIN_AUX_A;
2982 	case AUX_CH_B:
2983 		return POWER_DOMAIN_AUX_B;
2984 	case AUX_CH_C:
2985 		return POWER_DOMAIN_AUX_C;
2986 	case AUX_CH_D:
2987 		return POWER_DOMAIN_AUX_D;
2988 	case AUX_CH_E:
2989 		return POWER_DOMAIN_AUX_E;
2990 	case AUX_CH_F:
2991 		return POWER_DOMAIN_AUX_F;
2992 	case AUX_CH_G:
2993 		return POWER_DOMAIN_AUX_G;
2994 	case AUX_CH_H:
2995 		return POWER_DOMAIN_AUX_H;
2996 	case AUX_CH_I:
2997 		return POWER_DOMAIN_AUX_I;
2998 	default:
2999 		MISSING_CASE(aux_ch);
3000 		return POWER_DOMAIN_AUX_A;
3001 	}
3002 }
3003 
3004 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
3005 {
3006 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3007 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3008 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3009 	struct drm_encoder *encoder;
3010 	enum pipe pipe = crtc->pipe;
3011 	u64 mask;
3012 
3013 	if (!crtc_state->hw.active)
3014 		return 0;
3015 
3016 	mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
3017 	mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(cpu_transcoder));
3018 	if (crtc_state->pch_pfit.enabled ||
3019 	    crtc_state->pch_pfit.force_thru)
3020 		mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
3021 
3022 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
3023 				  crtc_state->uapi.encoder_mask) {
3024 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3025 
3026 		mask |= BIT_ULL(intel_encoder->power_domain);
3027 	}
3028 
3029 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
3030 		mask |= BIT_ULL(POWER_DOMAIN_AUDIO_MMIO);
3031 
3032 	if (crtc_state->shared_dpll)
3033 		mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
3034 
3035 	if (crtc_state->dsc.compression_enable)
3036 		mask |= BIT_ULL(intel_dsc_power_domain(crtc, cpu_transcoder));
3037 
3038 	return mask;
3039 }
3040 
3041 static u64
3042 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
3043 {
3044 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3045 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3046 	enum intel_display_power_domain domain;
3047 	u64 domains, new_domains, old_domains;
3048 
3049 	domains = get_crtc_power_domains(crtc_state);
3050 
3051 	new_domains = domains & ~crtc->enabled_power_domains.mask;
3052 	old_domains = crtc->enabled_power_domains.mask & ~domains;
3053 
3054 	for_each_power_domain(domain, new_domains)
3055 		intel_display_power_get_in_set(dev_priv,
3056 					       &crtc->enabled_power_domains,
3057 					       domain);
3058 
3059 	return old_domains;
3060 }
3061 
3062 static void modeset_put_crtc_power_domains(struct intel_crtc *crtc,
3063 					   u64 domains)
3064 {
3065 	intel_display_power_put_mask_in_set(to_i915(crtc->base.dev),
3066 					    &crtc->enabled_power_domains,
3067 					    domains);
3068 }
3069 
3070 static void valleyview_crtc_enable(struct intel_atomic_state *state,
3071 				   struct intel_crtc *crtc)
3072 {
3073 	const struct intel_crtc_state *new_crtc_state =
3074 		intel_atomic_get_new_crtc_state(state, crtc);
3075 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3076 	enum pipe pipe = crtc->pipe;
3077 
3078 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3079 		return;
3080 
3081 	if (intel_crtc_has_dp_encoder(new_crtc_state))
3082 		intel_dp_set_m_n(new_crtc_state, M1_N1);
3083 
3084 	intel_set_transcoder_timings(new_crtc_state);
3085 	intel_set_pipe_src_size(new_crtc_state);
3086 
3087 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
3088 		intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
3089 		intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
3090 	}
3091 
3092 	i9xx_set_pipeconf(new_crtc_state);
3093 
3094 	crtc->active = true;
3095 
3096 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3097 
3098 	intel_encoders_pre_pll_enable(state, crtc);
3099 
3100 	if (IS_CHERRYVIEW(dev_priv))
3101 		chv_enable_pll(new_crtc_state);
3102 	else
3103 		vlv_enable_pll(new_crtc_state);
3104 
3105 	intel_encoders_pre_enable(state, crtc);
3106 
3107 	i9xx_pfit_enable(new_crtc_state);
3108 
3109 	intel_color_load_luts(new_crtc_state);
3110 	intel_color_commit(new_crtc_state);
3111 	/* update DSPCNTR to configure gamma for pipe bottom color */
3112 	intel_disable_primary_plane(new_crtc_state);
3113 
3114 	intel_initial_watermarks(state, crtc);
3115 	intel_enable_transcoder(new_crtc_state);
3116 
3117 	intel_crtc_vblank_on(new_crtc_state);
3118 
3119 	intel_encoders_enable(state, crtc);
3120 }
3121 
3122 static void i9xx_crtc_enable(struct intel_atomic_state *state,
3123 			     struct intel_crtc *crtc)
3124 {
3125 	const struct intel_crtc_state *new_crtc_state =
3126 		intel_atomic_get_new_crtc_state(state, crtc);
3127 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3128 	enum pipe pipe = crtc->pipe;
3129 
3130 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3131 		return;
3132 
3133 	if (intel_crtc_has_dp_encoder(new_crtc_state))
3134 		intel_dp_set_m_n(new_crtc_state, M1_N1);
3135 
3136 	intel_set_transcoder_timings(new_crtc_state);
3137 	intel_set_pipe_src_size(new_crtc_state);
3138 
3139 	i9xx_set_pipeconf(new_crtc_state);
3140 
3141 	crtc->active = true;
3142 
3143 	if (DISPLAY_VER(dev_priv) != 2)
3144 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3145 
3146 	intel_encoders_pre_enable(state, crtc);
3147 
3148 	i9xx_enable_pll(new_crtc_state);
3149 
3150 	i9xx_pfit_enable(new_crtc_state);
3151 
3152 	intel_color_load_luts(new_crtc_state);
3153 	intel_color_commit(new_crtc_state);
3154 	/* update DSPCNTR to configure gamma for pipe bottom color */
3155 	intel_disable_primary_plane(new_crtc_state);
3156 
3157 	if (!intel_initial_watermarks(state, crtc))
3158 		intel_update_watermarks(dev_priv);
3159 	intel_enable_transcoder(new_crtc_state);
3160 
3161 	intel_crtc_vblank_on(new_crtc_state);
3162 
3163 	intel_encoders_enable(state, crtc);
3164 
3165 	/* prevents spurious underruns */
3166 	if (DISPLAY_VER(dev_priv) == 2)
3167 		intel_wait_for_vblank(dev_priv, pipe);
3168 }
3169 
3170 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
3171 {
3172 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
3173 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3174 
3175 	if (!old_crtc_state->gmch_pfit.control)
3176 		return;
3177 
3178 	assert_transcoder_disabled(dev_priv, old_crtc_state->cpu_transcoder);
3179 
3180 	drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
3181 		    intel_de_read(dev_priv, PFIT_CONTROL));
3182 	intel_de_write(dev_priv, PFIT_CONTROL, 0);
3183 }
3184 
3185 static void i9xx_crtc_disable(struct intel_atomic_state *state,
3186 			      struct intel_crtc *crtc)
3187 {
3188 	struct intel_crtc_state *old_crtc_state =
3189 		intel_atomic_get_old_crtc_state(state, crtc);
3190 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3191 	enum pipe pipe = crtc->pipe;
3192 
3193 	/*
3194 	 * On gen2 planes are double buffered but the pipe isn't, so we must
3195 	 * wait for planes to fully turn off before disabling the pipe.
3196 	 */
3197 	if (DISPLAY_VER(dev_priv) == 2)
3198 		intel_wait_for_vblank(dev_priv, pipe);
3199 
3200 	intel_encoders_disable(state, crtc);
3201 
3202 	intel_crtc_vblank_off(old_crtc_state);
3203 
3204 	intel_disable_transcoder(old_crtc_state);
3205 
3206 	i9xx_pfit_disable(old_crtc_state);
3207 
3208 	intel_encoders_post_disable(state, crtc);
3209 
3210 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
3211 		if (IS_CHERRYVIEW(dev_priv))
3212 			chv_disable_pll(dev_priv, pipe);
3213 		else if (IS_VALLEYVIEW(dev_priv))
3214 			vlv_disable_pll(dev_priv, pipe);
3215 		else
3216 			i9xx_disable_pll(old_crtc_state);
3217 	}
3218 
3219 	intel_encoders_post_pll_disable(state, crtc);
3220 
3221 	if (DISPLAY_VER(dev_priv) != 2)
3222 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3223 
3224 	if (!dev_priv->wm_disp->initial_watermarks)
3225 		intel_update_watermarks(dev_priv);
3226 
3227 	/* clock the pipe down to 640x480@60 to potentially save power */
3228 	if (IS_I830(dev_priv))
3229 		i830_enable_pipe(dev_priv, pipe);
3230 }
3231 
3232 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
3233 					struct drm_modeset_acquire_ctx *ctx)
3234 {
3235 	struct intel_encoder *encoder;
3236 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3237 	struct intel_bw_state *bw_state =
3238 		to_intel_bw_state(dev_priv->bw_obj.state);
3239 	struct intel_cdclk_state *cdclk_state =
3240 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
3241 	struct intel_dbuf_state *dbuf_state =
3242 		to_intel_dbuf_state(dev_priv->dbuf.obj.state);
3243 	struct intel_crtc_state *crtc_state =
3244 		to_intel_crtc_state(crtc->base.state);
3245 	struct intel_plane *plane;
3246 	struct drm_atomic_state *state;
3247 	struct intel_crtc_state *temp_crtc_state;
3248 	enum pipe pipe = crtc->pipe;
3249 	int ret;
3250 
3251 	if (!crtc_state->hw.active)
3252 		return;
3253 
3254 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
3255 		const struct intel_plane_state *plane_state =
3256 			to_intel_plane_state(plane->base.state);
3257 
3258 		if (plane_state->uapi.visible)
3259 			intel_plane_disable_noatomic(crtc, plane);
3260 	}
3261 
3262 	state = drm_atomic_state_alloc(&dev_priv->drm);
3263 	if (!state) {
3264 		drm_dbg_kms(&dev_priv->drm,
3265 			    "failed to disable [CRTC:%d:%s], out of memory",
3266 			    crtc->base.base.id, crtc->base.name);
3267 		return;
3268 	}
3269 
3270 	state->acquire_ctx = ctx;
3271 
3272 	/* Everything's already locked, -EDEADLK can't happen. */
3273 	temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
3274 	ret = drm_atomic_add_affected_connectors(state, &crtc->base);
3275 
3276 	drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
3277 
3278 	dev_priv->display->crtc_disable(to_intel_atomic_state(state), crtc);
3279 
3280 	drm_atomic_state_put(state);
3281 
3282 	drm_dbg_kms(&dev_priv->drm,
3283 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
3284 		    crtc->base.base.id, crtc->base.name);
3285 
3286 	crtc->active = false;
3287 	crtc->base.enabled = false;
3288 
3289 	drm_WARN_ON(&dev_priv->drm,
3290 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
3291 	crtc_state->uapi.active = false;
3292 	crtc_state->uapi.connector_mask = 0;
3293 	crtc_state->uapi.encoder_mask = 0;
3294 	intel_crtc_free_hw_state(crtc_state);
3295 	memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
3296 
3297 	for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
3298 		encoder->base.crtc = NULL;
3299 
3300 	intel_fbc_disable(crtc);
3301 	intel_update_watermarks(dev_priv);
3302 	intel_disable_shared_dpll(crtc_state);
3303 
3304 	intel_display_power_put_all_in_set(dev_priv, &crtc->enabled_power_domains);
3305 
3306 	cdclk_state->min_cdclk[pipe] = 0;
3307 	cdclk_state->min_voltage_level[pipe] = 0;
3308 	cdclk_state->active_pipes &= ~BIT(pipe);
3309 
3310 	dbuf_state->active_pipes &= ~BIT(pipe);
3311 
3312 	bw_state->data_rate[pipe] = 0;
3313 	bw_state->num_active_planes[pipe] = 0;
3314 }
3315 
3316 /*
3317  * turn all crtc's off, but do not adjust state
3318  * This has to be paired with a call to intel_modeset_setup_hw_state.
3319  */
3320 int intel_display_suspend(struct drm_device *dev)
3321 {
3322 	struct drm_i915_private *dev_priv = to_i915(dev);
3323 	struct drm_atomic_state *state;
3324 	int ret;
3325 
3326 	if (!HAS_DISPLAY(dev_priv))
3327 		return 0;
3328 
3329 	state = drm_atomic_helper_suspend(dev);
3330 	ret = PTR_ERR_OR_ZERO(state);
3331 	if (ret)
3332 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
3333 			ret);
3334 	else
3335 		dev_priv->modeset_restore_state = state;
3336 	return ret;
3337 }
3338 
3339 void intel_encoder_destroy(struct drm_encoder *encoder)
3340 {
3341 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
3342 
3343 	drm_encoder_cleanup(encoder);
3344 	kfree(intel_encoder);
3345 }
3346 
3347 /* Cross check the actual hw state with our own modeset state tracking (and it's
3348  * internal consistency). */
3349 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
3350 					 struct drm_connector_state *conn_state)
3351 {
3352 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
3353 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
3354 
3355 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
3356 		    connector->base.base.id, connector->base.name);
3357 
3358 	if (connector->get_hw_state(connector)) {
3359 		struct intel_encoder *encoder = intel_attached_encoder(connector);
3360 
3361 		I915_STATE_WARN(!crtc_state,
3362 			 "connector enabled without attached crtc\n");
3363 
3364 		if (!crtc_state)
3365 			return;
3366 
3367 		I915_STATE_WARN(!crtc_state->hw.active,
3368 				"connector is active, but attached crtc isn't\n");
3369 
3370 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
3371 			return;
3372 
3373 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
3374 			"atomic encoder doesn't match attached encoder\n");
3375 
3376 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
3377 			"attached encoder crtc differs from connector crtc\n");
3378 	} else {
3379 		I915_STATE_WARN(crtc_state && crtc_state->hw.active,
3380 				"attached crtc is active, but connector isn't\n");
3381 		I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
3382 			"best encoder set without crtc!\n");
3383 	}
3384 }
3385 
3386 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
3387 {
3388 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3389 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3390 
3391 	/* IPS only exists on ULT machines and is tied to pipe A. */
3392 	if (!hsw_crtc_supports_ips(crtc))
3393 		return false;
3394 
3395 	if (!dev_priv->params.enable_ips)
3396 		return false;
3397 
3398 	if (crtc_state->pipe_bpp > 24)
3399 		return false;
3400 
3401 	/*
3402 	 * We compare against max which means we must take
3403 	 * the increased cdclk requirement into account when
3404 	 * calculating the new cdclk.
3405 	 *
3406 	 * Should measure whether using a lower cdclk w/o IPS
3407 	 */
3408 	if (IS_BROADWELL(dev_priv) &&
3409 	    crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
3410 		return false;
3411 
3412 	return true;
3413 }
3414 
3415 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
3416 {
3417 	struct drm_i915_private *dev_priv =
3418 		to_i915(crtc_state->uapi.crtc->dev);
3419 	struct intel_atomic_state *state =
3420 		to_intel_atomic_state(crtc_state->uapi.state);
3421 
3422 	crtc_state->ips_enabled = false;
3423 
3424 	if (!hsw_crtc_state_ips_capable(crtc_state))
3425 		return 0;
3426 
3427 	/*
3428 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
3429 	 * enabled and disabled dynamically based on package C states,
3430 	 * user space can't make reliable use of the CRCs, so let's just
3431 	 * completely disable it.
3432 	 */
3433 	if (crtc_state->crc_enabled)
3434 		return 0;
3435 
3436 	/* IPS should be fine as long as at least one plane is enabled. */
3437 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
3438 		return 0;
3439 
3440 	if (IS_BROADWELL(dev_priv)) {
3441 		const struct intel_cdclk_state *cdclk_state;
3442 
3443 		cdclk_state = intel_atomic_get_cdclk_state(state);
3444 		if (IS_ERR(cdclk_state))
3445 			return PTR_ERR(cdclk_state);
3446 
3447 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
3448 		if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
3449 			return 0;
3450 	}
3451 
3452 	crtc_state->ips_enabled = true;
3453 
3454 	return 0;
3455 }
3456 
3457 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
3458 {
3459 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3460 
3461 	/* GDG double wide on either pipe, otherwise pipe A only */
3462 	return DISPLAY_VER(dev_priv) < 4 &&
3463 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
3464 }
3465 
3466 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
3467 {
3468 	u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
3469 	struct drm_rect src;
3470 
3471 	/*
3472 	 * We only use IF-ID interlacing. If we ever use
3473 	 * PF-ID we'll need to adjust the pixel_rate here.
3474 	 */
3475 
3476 	if (!crtc_state->pch_pfit.enabled)
3477 		return pixel_rate;
3478 
3479 	drm_rect_init(&src, 0, 0,
3480 		      crtc_state->pipe_src_w << 16,
3481 		      crtc_state->pipe_src_h << 16);
3482 
3483 	return intel_adjusted_rate(&src, &crtc_state->pch_pfit.dst,
3484 				   pixel_rate);
3485 }
3486 
3487 static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
3488 					 const struct drm_display_mode *timings)
3489 {
3490 	mode->hdisplay = timings->crtc_hdisplay;
3491 	mode->htotal = timings->crtc_htotal;
3492 	mode->hsync_start = timings->crtc_hsync_start;
3493 	mode->hsync_end = timings->crtc_hsync_end;
3494 
3495 	mode->vdisplay = timings->crtc_vdisplay;
3496 	mode->vtotal = timings->crtc_vtotal;
3497 	mode->vsync_start = timings->crtc_vsync_start;
3498 	mode->vsync_end = timings->crtc_vsync_end;
3499 
3500 	mode->flags = timings->flags;
3501 	mode->type = DRM_MODE_TYPE_DRIVER;
3502 
3503 	mode->clock = timings->crtc_clock;
3504 
3505 	drm_mode_set_name(mode);
3506 }
3507 
3508 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
3509 {
3510 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3511 
3512 	if (HAS_GMCH(dev_priv))
3513 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
3514 		crtc_state->pixel_rate =
3515 			crtc_state->hw.pipe_mode.crtc_clock;
3516 	else
3517 		crtc_state->pixel_rate =
3518 			ilk_pipe_pixel_rate(crtc_state);
3519 }
3520 
3521 static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
3522 {
3523 	struct drm_display_mode *mode = &crtc_state->hw.mode;
3524 	struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
3525 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
3526 
3527 	drm_mode_copy(pipe_mode, adjusted_mode);
3528 
3529 	if (crtc_state->bigjoiner) {
3530 		/*
3531 		 * transcoder is programmed to the full mode,
3532 		 * but pipe timings are half of the transcoder mode
3533 		 */
3534 		pipe_mode->crtc_hdisplay /= 2;
3535 		pipe_mode->crtc_hblank_start /= 2;
3536 		pipe_mode->crtc_hblank_end /= 2;
3537 		pipe_mode->crtc_hsync_start /= 2;
3538 		pipe_mode->crtc_hsync_end /= 2;
3539 		pipe_mode->crtc_htotal /= 2;
3540 		pipe_mode->crtc_clock /= 2;
3541 	}
3542 
3543 	if (crtc_state->splitter.enable) {
3544 		int n = crtc_state->splitter.link_count;
3545 		int overlap = crtc_state->splitter.pixel_overlap;
3546 
3547 		/*
3548 		 * eDP MSO uses segment timings from EDID for transcoder
3549 		 * timings, but full mode for everything else.
3550 		 *
3551 		 * h_full = (h_segment - pixel_overlap) * link_count
3552 		 */
3553 		pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
3554 		pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
3555 		pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
3556 		pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
3557 		pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
3558 		pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
3559 		pipe_mode->crtc_clock *= n;
3560 
3561 		intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
3562 		intel_mode_from_crtc_timings(adjusted_mode, pipe_mode);
3563 	} else {
3564 		intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
3565 		intel_mode_from_crtc_timings(adjusted_mode, adjusted_mode);
3566 	}
3567 
3568 	intel_crtc_compute_pixel_rate(crtc_state);
3569 
3570 	drm_mode_copy(mode, adjusted_mode);
3571 	mode->hdisplay = crtc_state->pipe_src_w << crtc_state->bigjoiner;
3572 	mode->vdisplay = crtc_state->pipe_src_h;
3573 }
3574 
3575 static void intel_encoder_get_config(struct intel_encoder *encoder,
3576 				     struct intel_crtc_state *crtc_state)
3577 {
3578 	encoder->get_config(encoder, crtc_state);
3579 
3580 	intel_crtc_readout_derived_state(crtc_state);
3581 }
3582 
3583 static int intel_crtc_compute_config(struct intel_crtc *crtc,
3584 				     struct intel_crtc_state *pipe_config)
3585 {
3586 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3587 	struct drm_display_mode *pipe_mode = &pipe_config->hw.pipe_mode;
3588 	int clock_limit = dev_priv->max_dotclk_freq;
3589 
3590 	drm_mode_copy(pipe_mode, &pipe_config->hw.adjusted_mode);
3591 
3592 	/* Adjust pipe_mode for bigjoiner, with half the horizontal mode */
3593 	if (pipe_config->bigjoiner) {
3594 		pipe_mode->crtc_clock /= 2;
3595 		pipe_mode->crtc_hdisplay /= 2;
3596 		pipe_mode->crtc_hblank_start /= 2;
3597 		pipe_mode->crtc_hblank_end /= 2;
3598 		pipe_mode->crtc_hsync_start /= 2;
3599 		pipe_mode->crtc_hsync_end /= 2;
3600 		pipe_mode->crtc_htotal /= 2;
3601 		pipe_config->pipe_src_w /= 2;
3602 	}
3603 
3604 	if (pipe_config->splitter.enable) {
3605 		int n = pipe_config->splitter.link_count;
3606 		int overlap = pipe_config->splitter.pixel_overlap;
3607 
3608 		pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
3609 		pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
3610 		pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
3611 		pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
3612 		pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
3613 		pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
3614 		pipe_mode->crtc_clock *= n;
3615 	}
3616 
3617 	intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
3618 
3619 	if (DISPLAY_VER(dev_priv) < 4) {
3620 		clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
3621 
3622 		/*
3623 		 * Enable double wide mode when the dot clock
3624 		 * is > 90% of the (display) core speed.
3625 		 */
3626 		if (intel_crtc_supports_double_wide(crtc) &&
3627 		    pipe_mode->crtc_clock > clock_limit) {
3628 			clock_limit = dev_priv->max_dotclk_freq;
3629 			pipe_config->double_wide = true;
3630 		}
3631 	}
3632 
3633 	if (pipe_mode->crtc_clock > clock_limit) {
3634 		drm_dbg_kms(&dev_priv->drm,
3635 			    "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
3636 			    pipe_mode->crtc_clock, clock_limit,
3637 			    yesno(pipe_config->double_wide));
3638 		return -EINVAL;
3639 	}
3640 
3641 	/*
3642 	 * Pipe horizontal size must be even in:
3643 	 * - DVO ganged mode
3644 	 * - LVDS dual channel mode
3645 	 * - Double wide pipe
3646 	 */
3647 	if (pipe_config->pipe_src_w & 1) {
3648 		if (pipe_config->double_wide) {
3649 			drm_dbg_kms(&dev_priv->drm,
3650 				    "Odd pipe source width not supported with double wide pipe\n");
3651 			return -EINVAL;
3652 		}
3653 
3654 		if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
3655 		    intel_is_dual_link_lvds(dev_priv)) {
3656 			drm_dbg_kms(&dev_priv->drm,
3657 				    "Odd pipe source width not supported with dual link LVDS\n");
3658 			return -EINVAL;
3659 		}
3660 	}
3661 
3662 	intel_crtc_compute_pixel_rate(pipe_config);
3663 
3664 	if (pipe_config->has_pch_encoder)
3665 		return ilk_fdi_compute_config(crtc, pipe_config);
3666 
3667 	return 0;
3668 }
3669 
3670 static void
3671 intel_reduce_m_n_ratio(u32 *num, u32 *den)
3672 {
3673 	while (*num > DATA_LINK_M_N_MASK ||
3674 	       *den > DATA_LINK_M_N_MASK) {
3675 		*num >>= 1;
3676 		*den >>= 1;
3677 	}
3678 }
3679 
3680 static void compute_m_n(unsigned int m, unsigned int n,
3681 			u32 *ret_m, u32 *ret_n,
3682 			bool constant_n)
3683 {
3684 	/*
3685 	 * Several DP dongles in particular seem to be fussy about
3686 	 * too large link M/N values. Give N value as 0x8000 that
3687 	 * should be acceptable by specific devices. 0x8000 is the
3688 	 * specified fixed N value for asynchronous clock mode,
3689 	 * which the devices expect also in synchronous clock mode.
3690 	 */
3691 	if (constant_n)
3692 		*ret_n = DP_LINK_CONSTANT_N_VALUE;
3693 	else
3694 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
3695 
3696 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
3697 	intel_reduce_m_n_ratio(ret_m, ret_n);
3698 }
3699 
3700 void
3701 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
3702 		       int pixel_clock, int link_clock,
3703 		       struct intel_link_m_n *m_n,
3704 		       bool constant_n, bool fec_enable)
3705 {
3706 	u32 data_clock = bits_per_pixel * pixel_clock;
3707 
3708 	if (fec_enable)
3709 		data_clock = intel_dp_mode_to_fec_clock(data_clock);
3710 
3711 	m_n->tu = 64;
3712 	compute_m_n(data_clock,
3713 		    link_clock * nlanes * 8,
3714 		    &m_n->gmch_m, &m_n->gmch_n,
3715 		    constant_n);
3716 
3717 	compute_m_n(pixel_clock, link_clock,
3718 		    &m_n->link_m, &m_n->link_n,
3719 		    constant_n);
3720 }
3721 
3722 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
3723 {
3724 	/*
3725 	 * There may be no VBT; and if the BIOS enabled SSC we can
3726 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
3727 	 * BIOS isn't using it, don't assume it will work even if the VBT
3728 	 * indicates as much.
3729 	 */
3730 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
3731 		bool bios_lvds_use_ssc = intel_de_read(dev_priv,
3732 						       PCH_DREF_CONTROL) &
3733 			DREF_SSC1_ENABLE;
3734 
3735 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
3736 			drm_dbg_kms(&dev_priv->drm,
3737 				    "SSC %s by BIOS, overriding VBT which says %s\n",
3738 				    enableddisabled(bios_lvds_use_ssc),
3739 				    enableddisabled(dev_priv->vbt.lvds_use_ssc));
3740 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
3741 		}
3742 	}
3743 }
3744 
3745 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
3746 					 const struct intel_link_m_n *m_n)
3747 {
3748 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3749 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3750 	enum pipe pipe = crtc->pipe;
3751 
3752 	intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
3753 		       TU_SIZE(m_n->tu) | m_n->gmch_m);
3754 	intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
3755 	intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
3756 	intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
3757 }
3758 
3759 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
3760 				 enum transcoder transcoder)
3761 {
3762 	if (IS_HASWELL(dev_priv))
3763 		return transcoder == TRANSCODER_EDP;
3764 
3765 	/*
3766 	 * Strictly speaking some registers are available before
3767 	 * gen7, but we only support DRRS on gen7+
3768 	 */
3769 	return DISPLAY_VER(dev_priv) == 7 || IS_CHERRYVIEW(dev_priv);
3770 }
3771 
3772 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
3773 					 const struct intel_link_m_n *m_n,
3774 					 const struct intel_link_m_n *m2_n2)
3775 {
3776 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3777 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3778 	enum pipe pipe = crtc->pipe;
3779 	enum transcoder transcoder = crtc_state->cpu_transcoder;
3780 
3781 	if (DISPLAY_VER(dev_priv) >= 5) {
3782 		intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
3783 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
3784 		intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
3785 			       m_n->gmch_n);
3786 		intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
3787 			       m_n->link_m);
3788 		intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
3789 			       m_n->link_n);
3790 		/*
3791 		 *  M2_N2 registers are set only if DRRS is supported
3792 		 * (to make sure the registers are not unnecessarily accessed).
3793 		 */
3794 		if (m2_n2 && crtc_state->has_drrs &&
3795 		    transcoder_has_m2_n2(dev_priv, transcoder)) {
3796 			intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
3797 				       TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
3798 			intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
3799 				       m2_n2->gmch_n);
3800 			intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
3801 				       m2_n2->link_m);
3802 			intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
3803 				       m2_n2->link_n);
3804 		}
3805 	} else {
3806 		intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
3807 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
3808 		intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
3809 		intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
3810 		intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
3811 	}
3812 }
3813 
3814 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
3815 {
3816 	const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
3817 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
3818 
3819 	if (m_n == M1_N1) {
3820 		dp_m_n = &crtc_state->dp_m_n;
3821 		dp_m2_n2 = &crtc_state->dp_m2_n2;
3822 	} else if (m_n == M2_N2) {
3823 
3824 		/*
3825 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
3826 		 * needs to be programmed into M1_N1.
3827 		 */
3828 		dp_m_n = &crtc_state->dp_m2_n2;
3829 	} else {
3830 		drm_err(&i915->drm, "Unsupported divider value\n");
3831 		return;
3832 	}
3833 
3834 	if (crtc_state->has_pch_encoder)
3835 		intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
3836 	else
3837 		intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
3838 }
3839 
3840 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
3841 {
3842 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3843 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3844 	enum pipe pipe = crtc->pipe;
3845 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3846 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
3847 	u32 crtc_vtotal, crtc_vblank_end;
3848 	int vsyncshift = 0;
3849 
3850 	/* We need to be careful not to changed the adjusted mode, for otherwise
3851 	 * the hw state checker will get angry at the mismatch. */
3852 	crtc_vtotal = adjusted_mode->crtc_vtotal;
3853 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
3854 
3855 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
3856 		/* the chip adds 2 halflines automatically */
3857 		crtc_vtotal -= 1;
3858 		crtc_vblank_end -= 1;
3859 
3860 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
3861 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
3862 		else
3863 			vsyncshift = adjusted_mode->crtc_hsync_start -
3864 				adjusted_mode->crtc_htotal / 2;
3865 		if (vsyncshift < 0)
3866 			vsyncshift += adjusted_mode->crtc_htotal;
3867 	}
3868 
3869 	if (DISPLAY_VER(dev_priv) > 3)
3870 		intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
3871 		               vsyncshift);
3872 
3873 	intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
3874 		       (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
3875 	intel_de_write(dev_priv, HBLANK(cpu_transcoder),
3876 		       (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
3877 	intel_de_write(dev_priv, HSYNC(cpu_transcoder),
3878 		       (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
3879 
3880 	intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
3881 		       (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
3882 	intel_de_write(dev_priv, VBLANK(cpu_transcoder),
3883 		       (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
3884 	intel_de_write(dev_priv, VSYNC(cpu_transcoder),
3885 		       (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
3886 
3887 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
3888 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
3889 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
3890 	 * bits. */
3891 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
3892 	    (pipe == PIPE_B || pipe == PIPE_C))
3893 		intel_de_write(dev_priv, VTOTAL(pipe),
3894 		               intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
3895 
3896 }
3897 
3898 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
3899 {
3900 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3901 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3902 	enum pipe pipe = crtc->pipe;
3903 
3904 	/* pipesrc controls the size that is scaled from, which should
3905 	 * always be the user's requested size.
3906 	 */
3907 	intel_de_write(dev_priv, PIPESRC(pipe),
3908 		       ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
3909 }
3910 
3911 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
3912 {
3913 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3914 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3915 
3916 	if (DISPLAY_VER(dev_priv) == 2)
3917 		return false;
3918 
3919 	if (DISPLAY_VER(dev_priv) >= 9 ||
3920 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
3921 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
3922 	else
3923 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
3924 }
3925 
3926 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
3927 					 struct intel_crtc_state *pipe_config)
3928 {
3929 	struct drm_device *dev = crtc->base.dev;
3930 	struct drm_i915_private *dev_priv = to_i915(dev);
3931 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
3932 	u32 tmp;
3933 
3934 	tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
3935 	pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
3936 	pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
3937 
3938 	if (!transcoder_is_dsi(cpu_transcoder)) {
3939 		tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
3940 		pipe_config->hw.adjusted_mode.crtc_hblank_start =
3941 							(tmp & 0xffff) + 1;
3942 		pipe_config->hw.adjusted_mode.crtc_hblank_end =
3943 						((tmp >> 16) & 0xffff) + 1;
3944 	}
3945 	tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
3946 	pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
3947 	pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
3948 
3949 	tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
3950 	pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
3951 	pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
3952 
3953 	if (!transcoder_is_dsi(cpu_transcoder)) {
3954 		tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
3955 		pipe_config->hw.adjusted_mode.crtc_vblank_start =
3956 							(tmp & 0xffff) + 1;
3957 		pipe_config->hw.adjusted_mode.crtc_vblank_end =
3958 						((tmp >> 16) & 0xffff) + 1;
3959 	}
3960 	tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
3961 	pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
3962 	pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
3963 
3964 	if (intel_pipe_is_interlaced(pipe_config)) {
3965 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
3966 		pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
3967 		pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
3968 	}
3969 }
3970 
3971 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
3972 				    struct intel_crtc_state *pipe_config)
3973 {
3974 	struct drm_device *dev = crtc->base.dev;
3975 	struct drm_i915_private *dev_priv = to_i915(dev);
3976 	u32 tmp;
3977 
3978 	tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
3979 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
3980 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
3981 }
3982 
3983 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
3984 {
3985 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3986 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3987 	u32 pipeconf;
3988 
3989 	pipeconf = 0;
3990 
3991 	/* we keep both pipes enabled on 830 */
3992 	if (IS_I830(dev_priv))
3993 		pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
3994 
3995 	if (crtc_state->double_wide)
3996 		pipeconf |= PIPECONF_DOUBLE_WIDE;
3997 
3998 	/* only g4x and later have fancy bpc/dither controls */
3999 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
4000 	    IS_CHERRYVIEW(dev_priv)) {
4001 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
4002 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
4003 			pipeconf |= PIPECONF_DITHER_EN |
4004 				    PIPECONF_DITHER_TYPE_SP;
4005 
4006 		switch (crtc_state->pipe_bpp) {
4007 		case 18:
4008 			pipeconf |= PIPECONF_6BPC;
4009 			break;
4010 		case 24:
4011 			pipeconf |= PIPECONF_8BPC;
4012 			break;
4013 		case 30:
4014 			pipeconf |= PIPECONF_10BPC;
4015 			break;
4016 		default:
4017 			/* Case prevented by intel_choose_pipe_bpp_dither. */
4018 			BUG();
4019 		}
4020 	}
4021 
4022 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
4023 		if (DISPLAY_VER(dev_priv) < 4 ||
4024 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
4025 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
4026 		else
4027 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
4028 	} else {
4029 		pipeconf |= PIPECONF_PROGRESSIVE;
4030 	}
4031 
4032 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
4033 	     crtc_state->limited_color_range)
4034 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
4035 
4036 	pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
4037 
4038 	pipeconf |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
4039 
4040 	intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
4041 	intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
4042 }
4043 
4044 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
4045 {
4046 	if (IS_I830(dev_priv))
4047 		return false;
4048 
4049 	return DISPLAY_VER(dev_priv) >= 4 ||
4050 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
4051 }
4052 
4053 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
4054 {
4055 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4056 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4057 	u32 tmp;
4058 
4059 	if (!i9xx_has_pfit(dev_priv))
4060 		return;
4061 
4062 	tmp = intel_de_read(dev_priv, PFIT_CONTROL);
4063 	if (!(tmp & PFIT_ENABLE))
4064 		return;
4065 
4066 	/* Check whether the pfit is attached to our pipe. */
4067 	if (DISPLAY_VER(dev_priv) < 4) {
4068 		if (crtc->pipe != PIPE_B)
4069 			return;
4070 	} else {
4071 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
4072 			return;
4073 	}
4074 
4075 	crtc_state->gmch_pfit.control = tmp;
4076 	crtc_state->gmch_pfit.pgm_ratios =
4077 		intel_de_read(dev_priv, PFIT_PGM_RATIOS);
4078 }
4079 
4080 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
4081 			       struct intel_crtc_state *pipe_config)
4082 {
4083 	struct drm_device *dev = crtc->base.dev;
4084 	struct drm_i915_private *dev_priv = to_i915(dev);
4085 	enum pipe pipe = crtc->pipe;
4086 	struct dpll clock;
4087 	u32 mdiv;
4088 	int refclk = 100000;
4089 
4090 	/* In case of DSI, DPLL will not be used */
4091 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
4092 		return;
4093 
4094 	vlv_dpio_get(dev_priv);
4095 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
4096 	vlv_dpio_put(dev_priv);
4097 
4098 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
4099 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
4100 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
4101 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
4102 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
4103 
4104 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
4105 }
4106 
4107 static void chv_crtc_clock_get(struct intel_crtc *crtc,
4108 			       struct intel_crtc_state *pipe_config)
4109 {
4110 	struct drm_device *dev = crtc->base.dev;
4111 	struct drm_i915_private *dev_priv = to_i915(dev);
4112 	enum pipe pipe = crtc->pipe;
4113 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
4114 	struct dpll clock;
4115 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
4116 	int refclk = 100000;
4117 
4118 	/* In case of DSI, DPLL will not be used */
4119 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
4120 		return;
4121 
4122 	vlv_dpio_get(dev_priv);
4123 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
4124 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
4125 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
4126 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
4127 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
4128 	vlv_dpio_put(dev_priv);
4129 
4130 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
4131 	clock.m2 = (pll_dw0 & 0xff) << 22;
4132 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
4133 		clock.m2 |= pll_dw2 & 0x3fffff;
4134 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
4135 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
4136 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
4137 
4138 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
4139 }
4140 
4141 static enum intel_output_format
4142 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
4143 {
4144 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4145 	u32 tmp;
4146 
4147 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
4148 
4149 	if (tmp & PIPEMISC_YUV420_ENABLE) {
4150 		/* We support 4:2:0 in full blend mode only */
4151 		drm_WARN_ON(&dev_priv->drm,
4152 			    (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
4153 
4154 		return INTEL_OUTPUT_FORMAT_YCBCR420;
4155 	} else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
4156 		return INTEL_OUTPUT_FORMAT_YCBCR444;
4157 	} else {
4158 		return INTEL_OUTPUT_FORMAT_RGB;
4159 	}
4160 }
4161 
4162 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
4163 {
4164 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4165 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
4166 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4167 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
4168 	u32 tmp;
4169 
4170 	tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
4171 
4172 	if (tmp & DISPPLANE_GAMMA_ENABLE)
4173 		crtc_state->gamma_enable = true;
4174 
4175 	if (!HAS_GMCH(dev_priv) &&
4176 	    tmp & DISPPLANE_PIPE_CSC_ENABLE)
4177 		crtc_state->csc_enable = true;
4178 }
4179 
4180 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
4181 				 struct intel_crtc_state *pipe_config)
4182 {
4183 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4184 	enum intel_display_power_domain power_domain;
4185 	intel_wakeref_t wakeref;
4186 	u32 tmp;
4187 	bool ret;
4188 
4189 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
4190 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
4191 	if (!wakeref)
4192 		return false;
4193 
4194 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
4195 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
4196 	pipe_config->shared_dpll = NULL;
4197 
4198 	ret = false;
4199 
4200 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
4201 	if (!(tmp & PIPECONF_ENABLE))
4202 		goto out;
4203 
4204 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
4205 	    IS_CHERRYVIEW(dev_priv)) {
4206 		switch (tmp & PIPECONF_BPC_MASK) {
4207 		case PIPECONF_6BPC:
4208 			pipe_config->pipe_bpp = 18;
4209 			break;
4210 		case PIPECONF_8BPC:
4211 			pipe_config->pipe_bpp = 24;
4212 			break;
4213 		case PIPECONF_10BPC:
4214 			pipe_config->pipe_bpp = 30;
4215 			break;
4216 		default:
4217 			break;
4218 		}
4219 	}
4220 
4221 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
4222 	    (tmp & PIPECONF_COLOR_RANGE_SELECT))
4223 		pipe_config->limited_color_range = true;
4224 
4225 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
4226 		PIPECONF_GAMMA_MODE_SHIFT;
4227 
4228 	if (IS_CHERRYVIEW(dev_priv))
4229 		pipe_config->cgm_mode = intel_de_read(dev_priv,
4230 						      CGM_PIPE_MODE(crtc->pipe));
4231 
4232 	i9xx_get_pipe_color_config(pipe_config);
4233 	intel_color_get_config(pipe_config);
4234 
4235 	if (DISPLAY_VER(dev_priv) < 4)
4236 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
4237 
4238 	intel_get_transcoder_timings(crtc, pipe_config);
4239 	intel_get_pipe_src_size(crtc, pipe_config);
4240 
4241 	i9xx_get_pfit_config(pipe_config);
4242 
4243 	if (DISPLAY_VER(dev_priv) >= 4) {
4244 		/* No way to read it out on pipes B and C */
4245 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
4246 			tmp = dev_priv->chv_dpll_md[crtc->pipe];
4247 		else
4248 			tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
4249 		pipe_config->pixel_multiplier =
4250 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
4251 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
4252 		pipe_config->dpll_hw_state.dpll_md = tmp;
4253 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
4254 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
4255 		tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
4256 		pipe_config->pixel_multiplier =
4257 			((tmp & SDVO_MULTIPLIER_MASK)
4258 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
4259 	} else {
4260 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
4261 		 * port and will be fixed up in the encoder->get_config
4262 		 * function. */
4263 		pipe_config->pixel_multiplier = 1;
4264 	}
4265 	pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
4266 							DPLL(crtc->pipe));
4267 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
4268 		pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
4269 							       FP0(crtc->pipe));
4270 		pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
4271 							       FP1(crtc->pipe));
4272 	} else {
4273 		/* Mask out read-only status bits. */
4274 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
4275 						     DPLL_PORTC_READY_MASK |
4276 						     DPLL_PORTB_READY_MASK);
4277 	}
4278 
4279 	if (IS_CHERRYVIEW(dev_priv))
4280 		chv_crtc_clock_get(crtc, pipe_config);
4281 	else if (IS_VALLEYVIEW(dev_priv))
4282 		vlv_crtc_clock_get(crtc, pipe_config);
4283 	else
4284 		i9xx_crtc_clock_get(crtc, pipe_config);
4285 
4286 	/*
4287 	 * Normally the dotclock is filled in by the encoder .get_config()
4288 	 * but in case the pipe is enabled w/o any ports we need a sane
4289 	 * default.
4290 	 */
4291 	pipe_config->hw.adjusted_mode.crtc_clock =
4292 		pipe_config->port_clock / pipe_config->pixel_multiplier;
4293 
4294 	ret = true;
4295 
4296 out:
4297 	intel_display_power_put(dev_priv, power_domain, wakeref);
4298 
4299 	return ret;
4300 }
4301 
4302 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
4303 {
4304 	struct intel_encoder *encoder;
4305 	int i;
4306 	u32 val, final;
4307 	bool has_lvds = false;
4308 	bool has_cpu_edp = false;
4309 	bool has_panel = false;
4310 	bool has_ck505 = false;
4311 	bool can_ssc = false;
4312 	bool using_ssc_source = false;
4313 
4314 	/* We need to take the global config into account */
4315 	for_each_intel_encoder(&dev_priv->drm, encoder) {
4316 		switch (encoder->type) {
4317 		case INTEL_OUTPUT_LVDS:
4318 			has_panel = true;
4319 			has_lvds = true;
4320 			break;
4321 		case INTEL_OUTPUT_EDP:
4322 			has_panel = true;
4323 			if (encoder->port == PORT_A)
4324 				has_cpu_edp = true;
4325 			break;
4326 		default:
4327 			break;
4328 		}
4329 	}
4330 
4331 	if (HAS_PCH_IBX(dev_priv)) {
4332 		has_ck505 = dev_priv->vbt.display_clock_mode;
4333 		can_ssc = has_ck505;
4334 	} else {
4335 		has_ck505 = false;
4336 		can_ssc = true;
4337 	}
4338 
4339 	/* Check if any DPLLs are using the SSC source */
4340 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
4341 		u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
4342 
4343 		if (!(temp & DPLL_VCO_ENABLE))
4344 			continue;
4345 
4346 		if ((temp & PLL_REF_INPUT_MASK) ==
4347 		    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
4348 			using_ssc_source = true;
4349 			break;
4350 		}
4351 	}
4352 
4353 	drm_dbg_kms(&dev_priv->drm,
4354 		    "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
4355 		    has_panel, has_lvds, has_ck505, using_ssc_source);
4356 
4357 	/* Ironlake: try to setup display ref clock before DPLL
4358 	 * enabling. This is only under driver's control after
4359 	 * PCH B stepping, previous chipset stepping should be
4360 	 * ignoring this setting.
4361 	 */
4362 	val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
4363 
4364 	/* As we must carefully and slowly disable/enable each source in turn,
4365 	 * compute the final state we want first and check if we need to
4366 	 * make any changes at all.
4367 	 */
4368 	final = val;
4369 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
4370 	if (has_ck505)
4371 		final |= DREF_NONSPREAD_CK505_ENABLE;
4372 	else
4373 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
4374 
4375 	final &= ~DREF_SSC_SOURCE_MASK;
4376 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4377 	final &= ~DREF_SSC1_ENABLE;
4378 
4379 	if (has_panel) {
4380 		final |= DREF_SSC_SOURCE_ENABLE;
4381 
4382 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
4383 			final |= DREF_SSC1_ENABLE;
4384 
4385 		if (has_cpu_edp) {
4386 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
4387 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4388 			else
4389 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4390 		} else
4391 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4392 	} else if (using_ssc_source) {
4393 		final |= DREF_SSC_SOURCE_ENABLE;
4394 		final |= DREF_SSC1_ENABLE;
4395 	}
4396 
4397 	if (final == val)
4398 		return;
4399 
4400 	/* Always enable nonspread source */
4401 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
4402 
4403 	if (has_ck505)
4404 		val |= DREF_NONSPREAD_CK505_ENABLE;
4405 	else
4406 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
4407 
4408 	if (has_panel) {
4409 		val &= ~DREF_SSC_SOURCE_MASK;
4410 		val |= DREF_SSC_SOURCE_ENABLE;
4411 
4412 		/* SSC must be turned on before enabling the CPU output  */
4413 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4414 			drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
4415 			val |= DREF_SSC1_ENABLE;
4416 		} else
4417 			val &= ~DREF_SSC1_ENABLE;
4418 
4419 		/* Get SSC going before enabling the outputs */
4420 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
4421 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
4422 		udelay(200);
4423 
4424 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4425 
4426 		/* Enable CPU source on CPU attached eDP */
4427 		if (has_cpu_edp) {
4428 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
4429 				drm_dbg_kms(&dev_priv->drm,
4430 					    "Using SSC on eDP\n");
4431 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
4432 			} else
4433 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
4434 		} else
4435 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4436 
4437 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
4438 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
4439 		udelay(200);
4440 	} else {
4441 		drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
4442 
4443 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
4444 
4445 		/* Turn off CPU output */
4446 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
4447 
4448 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
4449 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
4450 		udelay(200);
4451 
4452 		if (!using_ssc_source) {
4453 			drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
4454 
4455 			/* Turn off the SSC source */
4456 			val &= ~DREF_SSC_SOURCE_MASK;
4457 			val |= DREF_SSC_SOURCE_DISABLE;
4458 
4459 			/* Turn off SSC1 */
4460 			val &= ~DREF_SSC1_ENABLE;
4461 
4462 			intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
4463 			intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
4464 			udelay(200);
4465 		}
4466 	}
4467 
4468 	BUG_ON(val != final);
4469 }
4470 
4471 /* Implements 3 different sequences from BSpec chapter "Display iCLK
4472  * Programming" based on the parameters passed:
4473  * - Sequence to enable CLKOUT_DP
4474  * - Sequence to enable CLKOUT_DP without spread
4475  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
4476  */
4477 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
4478 				 bool with_spread, bool with_fdi)
4479 {
4480 	u32 reg, tmp;
4481 
4482 	if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
4483 		     "FDI requires downspread\n"))
4484 		with_spread = true;
4485 	if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
4486 		     with_fdi, "LP PCH doesn't have FDI\n"))
4487 		with_fdi = false;
4488 
4489 	mutex_lock(&dev_priv->sb_lock);
4490 
4491 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4492 	tmp &= ~SBI_SSCCTL_DISABLE;
4493 	tmp |= SBI_SSCCTL_PATHALT;
4494 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4495 
4496 	udelay(24);
4497 
4498 	if (with_spread) {
4499 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4500 		tmp &= ~SBI_SSCCTL_PATHALT;
4501 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4502 
4503 		if (with_fdi)
4504 			lpt_fdi_program_mphy(dev_priv);
4505 	}
4506 
4507 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
4508 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
4509 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
4510 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
4511 
4512 	mutex_unlock(&dev_priv->sb_lock);
4513 }
4514 
4515 /* Sequence to disable CLKOUT_DP */
4516 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
4517 {
4518 	u32 reg, tmp;
4519 
4520 	mutex_lock(&dev_priv->sb_lock);
4521 
4522 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
4523 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
4524 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
4525 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
4526 
4527 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
4528 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
4529 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
4530 			tmp |= SBI_SSCCTL_PATHALT;
4531 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4532 			udelay(32);
4533 		}
4534 		tmp |= SBI_SSCCTL_DISABLE;
4535 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
4536 	}
4537 
4538 	mutex_unlock(&dev_priv->sb_lock);
4539 }
4540 
4541 #define BEND_IDX(steps) ((50 + (steps)) / 5)
4542 
4543 static const u16 sscdivintphase[] = {
4544 	[BEND_IDX( 50)] = 0x3B23,
4545 	[BEND_IDX( 45)] = 0x3B23,
4546 	[BEND_IDX( 40)] = 0x3C23,
4547 	[BEND_IDX( 35)] = 0x3C23,
4548 	[BEND_IDX( 30)] = 0x3D23,
4549 	[BEND_IDX( 25)] = 0x3D23,
4550 	[BEND_IDX( 20)] = 0x3E23,
4551 	[BEND_IDX( 15)] = 0x3E23,
4552 	[BEND_IDX( 10)] = 0x3F23,
4553 	[BEND_IDX(  5)] = 0x3F23,
4554 	[BEND_IDX(  0)] = 0x0025,
4555 	[BEND_IDX( -5)] = 0x0025,
4556 	[BEND_IDX(-10)] = 0x0125,
4557 	[BEND_IDX(-15)] = 0x0125,
4558 	[BEND_IDX(-20)] = 0x0225,
4559 	[BEND_IDX(-25)] = 0x0225,
4560 	[BEND_IDX(-30)] = 0x0325,
4561 	[BEND_IDX(-35)] = 0x0325,
4562 	[BEND_IDX(-40)] = 0x0425,
4563 	[BEND_IDX(-45)] = 0x0425,
4564 	[BEND_IDX(-50)] = 0x0525,
4565 };
4566 
4567 /*
4568  * Bend CLKOUT_DP
4569  * steps -50 to 50 inclusive, in steps of 5
4570  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
4571  * change in clock period = -(steps / 10) * 5.787 ps
4572  */
4573 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
4574 {
4575 	u32 tmp;
4576 	int idx = BEND_IDX(steps);
4577 
4578 	if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
4579 		return;
4580 
4581 	if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
4582 		return;
4583 
4584 	mutex_lock(&dev_priv->sb_lock);
4585 
4586 	if (steps % 10 != 0)
4587 		tmp = 0xAAAAAAAB;
4588 	else
4589 		tmp = 0x00000000;
4590 	intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
4591 
4592 	tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
4593 	tmp &= 0xffff0000;
4594 	tmp |= sscdivintphase[idx];
4595 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
4596 
4597 	mutex_unlock(&dev_priv->sb_lock);
4598 }
4599 
4600 #undef BEND_IDX
4601 
4602 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
4603 {
4604 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
4605 	u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
4606 
4607 	if ((ctl & SPLL_PLL_ENABLE) == 0)
4608 		return false;
4609 
4610 	if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
4611 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
4612 		return true;
4613 
4614 	if (IS_BROADWELL(dev_priv) &&
4615 	    (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
4616 		return true;
4617 
4618 	return false;
4619 }
4620 
4621 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
4622 			       enum intel_dpll_id id)
4623 {
4624 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
4625 	u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
4626 
4627 	if ((ctl & WRPLL_PLL_ENABLE) == 0)
4628 		return false;
4629 
4630 	if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
4631 		return true;
4632 
4633 	if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
4634 	    (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
4635 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
4636 		return true;
4637 
4638 	return false;
4639 }
4640 
4641 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
4642 {
4643 	struct intel_encoder *encoder;
4644 	bool has_fdi = false;
4645 
4646 	for_each_intel_encoder(&dev_priv->drm, encoder) {
4647 		switch (encoder->type) {
4648 		case INTEL_OUTPUT_ANALOG:
4649 			has_fdi = true;
4650 			break;
4651 		default:
4652 			break;
4653 		}
4654 	}
4655 
4656 	/*
4657 	 * The BIOS may have decided to use the PCH SSC
4658 	 * reference so we must not disable it until the
4659 	 * relevant PLLs have stopped relying on it. We'll
4660 	 * just leave the PCH SSC reference enabled in case
4661 	 * any active PLL is using it. It will get disabled
4662 	 * after runtime suspend if we don't have FDI.
4663 	 *
4664 	 * TODO: Move the whole reference clock handling
4665 	 * to the modeset sequence proper so that we can
4666 	 * actually enable/disable/reconfigure these things
4667 	 * safely. To do that we need to introduce a real
4668 	 * clock hierarchy. That would also allow us to do
4669 	 * clock bending finally.
4670 	 */
4671 	dev_priv->pch_ssc_use = 0;
4672 
4673 	if (spll_uses_pch_ssc(dev_priv)) {
4674 		drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
4675 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
4676 	}
4677 
4678 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
4679 		drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
4680 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
4681 	}
4682 
4683 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
4684 		drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
4685 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
4686 	}
4687 
4688 	if (dev_priv->pch_ssc_use)
4689 		return;
4690 
4691 	if (has_fdi) {
4692 		lpt_bend_clkout_dp(dev_priv, 0);
4693 		lpt_enable_clkout_dp(dev_priv, true, true);
4694 	} else {
4695 		lpt_disable_clkout_dp(dev_priv);
4696 	}
4697 }
4698 
4699 /*
4700  * Initialize reference clocks when the driver loads
4701  */
4702 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
4703 {
4704 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
4705 		ilk_init_pch_refclk(dev_priv);
4706 	else if (HAS_PCH_LPT(dev_priv))
4707 		lpt_init_pch_refclk(dev_priv);
4708 }
4709 
4710 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
4711 {
4712 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4713 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4714 	enum pipe pipe = crtc->pipe;
4715 	u32 val;
4716 
4717 	val = 0;
4718 
4719 	switch (crtc_state->pipe_bpp) {
4720 	case 18:
4721 		val |= PIPECONF_6BPC;
4722 		break;
4723 	case 24:
4724 		val |= PIPECONF_8BPC;
4725 		break;
4726 	case 30:
4727 		val |= PIPECONF_10BPC;
4728 		break;
4729 	case 36:
4730 		val |= PIPECONF_12BPC;
4731 		break;
4732 	default:
4733 		/* Case prevented by intel_choose_pipe_bpp_dither. */
4734 		BUG();
4735 	}
4736 
4737 	if (crtc_state->dither)
4738 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4739 
4740 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4741 		val |= PIPECONF_INTERLACED_ILK;
4742 	else
4743 		val |= PIPECONF_PROGRESSIVE;
4744 
4745 	/*
4746 	 * This would end up with an odd purple hue over
4747 	 * the entire display. Make sure we don't do it.
4748 	 */
4749 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
4750 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
4751 
4752 	if (crtc_state->limited_color_range &&
4753 	    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
4754 		val |= PIPECONF_COLOR_RANGE_SELECT;
4755 
4756 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
4757 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
4758 
4759 	val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
4760 
4761 	val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
4762 
4763 	intel_de_write(dev_priv, PIPECONF(pipe), val);
4764 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
4765 }
4766 
4767 static void hsw_set_transconf(const struct intel_crtc_state *crtc_state)
4768 {
4769 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4770 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4771 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
4772 	u32 val = 0;
4773 
4774 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
4775 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
4776 
4777 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
4778 		val |= PIPECONF_INTERLACED_ILK;
4779 	else
4780 		val |= PIPECONF_PROGRESSIVE;
4781 
4782 	if (IS_HASWELL(dev_priv) &&
4783 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
4784 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
4785 
4786 	intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
4787 	intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
4788 }
4789 
4790 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
4791 {
4792 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4793 	const struct intel_crtc_scaler_state *scaler_state =
4794 		&crtc_state->scaler_state;
4795 
4796 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4797 	u32 val = 0;
4798 	int i;
4799 
4800 	switch (crtc_state->pipe_bpp) {
4801 	case 18:
4802 		val |= PIPEMISC_6_BPC;
4803 		break;
4804 	case 24:
4805 		val |= PIPEMISC_8_BPC;
4806 		break;
4807 	case 30:
4808 		val |= PIPEMISC_10_BPC;
4809 		break;
4810 	case 36:
4811 		/* Port output 12BPC defined for ADLP+ */
4812 		if (DISPLAY_VER(dev_priv) > 12)
4813 			val |= PIPEMISC_12_BPC_ADLP;
4814 		break;
4815 	default:
4816 		MISSING_CASE(crtc_state->pipe_bpp);
4817 		break;
4818 	}
4819 
4820 	if (crtc_state->dither)
4821 		val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
4822 
4823 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
4824 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
4825 		val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
4826 
4827 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
4828 		val |= PIPEMISC_YUV420_ENABLE |
4829 			PIPEMISC_YUV420_MODE_FULL_BLEND;
4830 
4831 	if (DISPLAY_VER(dev_priv) >= 11 && is_hdr_mode(crtc_state))
4832 		val |= PIPEMISC_HDR_MODE_PRECISION;
4833 
4834 	if (DISPLAY_VER(dev_priv) >= 12)
4835 		val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
4836 
4837 	if (IS_ALDERLAKE_P(dev_priv)) {
4838 		bool scaler_in_use = false;
4839 
4840 		for (i = 0; i < crtc->num_scalers; i++) {
4841 			if (!scaler_state->scalers[i].in_use)
4842 				continue;
4843 
4844 			scaler_in_use = true;
4845 			break;
4846 		}
4847 
4848 		intel_de_rmw(dev_priv, PIPE_MISC2(crtc->pipe),
4849 			     PIPE_MISC2_UNDERRUN_BUBBLE_COUNTER_MASK,
4850 			     scaler_in_use ? PIPE_MISC2_BUBBLE_COUNTER_SCALER_EN :
4851 			     PIPE_MISC2_BUBBLE_COUNTER_SCALER_DIS);
4852 	}
4853 
4854 	intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
4855 }
4856 
4857 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
4858 {
4859 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4860 	u32 tmp;
4861 
4862 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
4863 
4864 	switch (tmp & PIPEMISC_BPC_MASK) {
4865 	case PIPEMISC_6_BPC:
4866 		return 18;
4867 	case PIPEMISC_8_BPC:
4868 		return 24;
4869 	case PIPEMISC_10_BPC:
4870 		return 30;
4871 	/*
4872 	 * PORT OUTPUT 12 BPC defined for ADLP+.
4873 	 *
4874 	 * TODO:
4875 	 * For previous platforms with DSI interface, bits 5:7
4876 	 * are used for storing pipe_bpp irrespective of dithering.
4877 	 * Since the value of 12 BPC is not defined for these bits
4878 	 * on older platforms, need to find a workaround for 12 BPC
4879 	 * MIPI DSI HW readout.
4880 	 */
4881 	case PIPEMISC_12_BPC_ADLP:
4882 		if (DISPLAY_VER(dev_priv) > 12)
4883 			return 36;
4884 		fallthrough;
4885 	default:
4886 		MISSING_CASE(tmp);
4887 		return 0;
4888 	}
4889 }
4890 
4891 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
4892 {
4893 	/*
4894 	 * Account for spread spectrum to avoid
4895 	 * oversubscribing the link. Max center spread
4896 	 * is 2.5%; use 5% for safety's sake.
4897 	 */
4898 	u32 bps = target_clock * bpp * 21 / 20;
4899 	return DIV_ROUND_UP(bps, link_bw * 8);
4900 }
4901 
4902 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
4903 					 struct intel_link_m_n *m_n)
4904 {
4905 	struct drm_device *dev = crtc->base.dev;
4906 	struct drm_i915_private *dev_priv = to_i915(dev);
4907 	enum pipe pipe = crtc->pipe;
4908 
4909 	m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
4910 	m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
4911 	m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
4912 		& ~TU_SIZE_MASK;
4913 	m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
4914 	m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
4915 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
4916 }
4917 
4918 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
4919 					 enum transcoder transcoder,
4920 					 struct intel_link_m_n *m_n,
4921 					 struct intel_link_m_n *m2_n2)
4922 {
4923 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4924 	enum pipe pipe = crtc->pipe;
4925 
4926 	if (DISPLAY_VER(dev_priv) >= 5) {
4927 		m_n->link_m = intel_de_read(dev_priv,
4928 					    PIPE_LINK_M1(transcoder));
4929 		m_n->link_n = intel_de_read(dev_priv,
4930 					    PIPE_LINK_N1(transcoder));
4931 		m_n->gmch_m = intel_de_read(dev_priv,
4932 					    PIPE_DATA_M1(transcoder))
4933 			& ~TU_SIZE_MASK;
4934 		m_n->gmch_n = intel_de_read(dev_priv,
4935 					    PIPE_DATA_N1(transcoder));
4936 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
4937 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
4938 
4939 		if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
4940 			m2_n2->link_m = intel_de_read(dev_priv,
4941 						      PIPE_LINK_M2(transcoder));
4942 			m2_n2->link_n =	intel_de_read(dev_priv,
4943 							     PIPE_LINK_N2(transcoder));
4944 			m2_n2->gmch_m =	intel_de_read(dev_priv,
4945 							     PIPE_DATA_M2(transcoder))
4946 					& ~TU_SIZE_MASK;
4947 			m2_n2->gmch_n =	intel_de_read(dev_priv,
4948 							     PIPE_DATA_N2(transcoder));
4949 			m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
4950 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
4951 		}
4952 	} else {
4953 		m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
4954 		m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
4955 		m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
4956 			& ~TU_SIZE_MASK;
4957 		m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
4958 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
4959 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
4960 	}
4961 }
4962 
4963 void intel_dp_get_m_n(struct intel_crtc *crtc,
4964 		      struct intel_crtc_state *pipe_config)
4965 {
4966 	if (pipe_config->has_pch_encoder)
4967 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
4968 	else
4969 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
4970 					     &pipe_config->dp_m_n,
4971 					     &pipe_config->dp_m2_n2);
4972 }
4973 
4974 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
4975 				   struct intel_crtc_state *pipe_config)
4976 {
4977 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
4978 				     &pipe_config->fdi_m_n, NULL);
4979 }
4980 
4981 static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
4982 				  u32 pos, u32 size)
4983 {
4984 	drm_rect_init(&crtc_state->pch_pfit.dst,
4985 		      pos >> 16, pos & 0xffff,
4986 		      size >> 16, size & 0xffff);
4987 }
4988 
4989 static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
4990 {
4991 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4992 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4993 	struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
4994 	int id = -1;
4995 	int i;
4996 
4997 	/* find scaler attached to this pipe */
4998 	for (i = 0; i < crtc->num_scalers; i++) {
4999 		u32 ctl, pos, size;
5000 
5001 		ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
5002 		if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
5003 			continue;
5004 
5005 		id = i;
5006 		crtc_state->pch_pfit.enabled = true;
5007 
5008 		pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
5009 		size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
5010 
5011 		ilk_get_pfit_pos_size(crtc_state, pos, size);
5012 
5013 		scaler_state->scalers[i].in_use = true;
5014 		break;
5015 	}
5016 
5017 	scaler_state->scaler_id = id;
5018 	if (id >= 0)
5019 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
5020 	else
5021 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
5022 }
5023 
5024 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
5025 {
5026 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5027 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5028 	u32 ctl, pos, size;
5029 
5030 	ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
5031 	if ((ctl & PF_ENABLE) == 0)
5032 		return;
5033 
5034 	crtc_state->pch_pfit.enabled = true;
5035 
5036 	pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
5037 	size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
5038 
5039 	ilk_get_pfit_pos_size(crtc_state, pos, size);
5040 
5041 	/*
5042 	 * We currently do not free assignements of panel fitters on
5043 	 * ivb/hsw (since we don't use the higher upscaling modes which
5044 	 * differentiates them) so just WARN about this case for now.
5045 	 */
5046 	drm_WARN_ON(&dev_priv->drm, DISPLAY_VER(dev_priv) == 7 &&
5047 		    (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
5048 }
5049 
5050 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
5051 				struct intel_crtc_state *pipe_config)
5052 {
5053 	struct drm_device *dev = crtc->base.dev;
5054 	struct drm_i915_private *dev_priv = to_i915(dev);
5055 	enum intel_display_power_domain power_domain;
5056 	intel_wakeref_t wakeref;
5057 	u32 tmp;
5058 	bool ret;
5059 
5060 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
5061 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
5062 	if (!wakeref)
5063 		return false;
5064 
5065 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5066 	pipe_config->shared_dpll = NULL;
5067 
5068 	ret = false;
5069 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
5070 	if (!(tmp & PIPECONF_ENABLE))
5071 		goto out;
5072 
5073 	switch (tmp & PIPECONF_BPC_MASK) {
5074 	case PIPECONF_6BPC:
5075 		pipe_config->pipe_bpp = 18;
5076 		break;
5077 	case PIPECONF_8BPC:
5078 		pipe_config->pipe_bpp = 24;
5079 		break;
5080 	case PIPECONF_10BPC:
5081 		pipe_config->pipe_bpp = 30;
5082 		break;
5083 	case PIPECONF_12BPC:
5084 		pipe_config->pipe_bpp = 36;
5085 		break;
5086 	default:
5087 		break;
5088 	}
5089 
5090 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
5091 		pipe_config->limited_color_range = true;
5092 
5093 	switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
5094 	case PIPECONF_OUTPUT_COLORSPACE_YUV601:
5095 	case PIPECONF_OUTPUT_COLORSPACE_YUV709:
5096 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
5097 		break;
5098 	default:
5099 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
5100 		break;
5101 	}
5102 
5103 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
5104 		PIPECONF_GAMMA_MODE_SHIFT;
5105 
5106 	pipe_config->csc_mode = intel_de_read(dev_priv,
5107 					      PIPE_CSC_MODE(crtc->pipe));
5108 
5109 	i9xx_get_pipe_color_config(pipe_config);
5110 	intel_color_get_config(pipe_config);
5111 
5112 	if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
5113 		struct intel_shared_dpll *pll;
5114 		enum intel_dpll_id pll_id;
5115 		bool pll_active;
5116 
5117 		pipe_config->has_pch_encoder = true;
5118 
5119 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
5120 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5121 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
5122 
5123 		ilk_get_fdi_m_n_config(crtc, pipe_config);
5124 
5125 		if (HAS_PCH_IBX(dev_priv)) {
5126 			/*
5127 			 * The pipe->pch transcoder and pch transcoder->pll
5128 			 * mapping is fixed.
5129 			 */
5130 			pll_id = (enum intel_dpll_id) crtc->pipe;
5131 		} else {
5132 			tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
5133 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
5134 				pll_id = DPLL_ID_PCH_PLL_B;
5135 			else
5136 				pll_id= DPLL_ID_PCH_PLL_A;
5137 		}
5138 
5139 		pipe_config->shared_dpll =
5140 			intel_get_shared_dpll_by_id(dev_priv, pll_id);
5141 		pll = pipe_config->shared_dpll;
5142 
5143 		pll_active = intel_dpll_get_hw_state(dev_priv, pll,
5144 						     &pipe_config->dpll_hw_state);
5145 		drm_WARN_ON(dev, !pll_active);
5146 
5147 		tmp = pipe_config->dpll_hw_state.dpll;
5148 		pipe_config->pixel_multiplier =
5149 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
5150 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
5151 
5152 		ilk_pch_clock_get(crtc, pipe_config);
5153 	} else {
5154 		pipe_config->pixel_multiplier = 1;
5155 	}
5156 
5157 	intel_get_transcoder_timings(crtc, pipe_config);
5158 	intel_get_pipe_src_size(crtc, pipe_config);
5159 
5160 	ilk_get_pfit_config(pipe_config);
5161 
5162 	ret = true;
5163 
5164 out:
5165 	intel_display_power_put(dev_priv, power_domain, wakeref);
5166 
5167 	return ret;
5168 }
5169 
5170 static bool transcoder_ddi_func_is_enabled(struct drm_i915_private *dev_priv,
5171 					   enum transcoder cpu_transcoder)
5172 {
5173 	enum intel_display_power_domain power_domain;
5174 	intel_wakeref_t wakeref;
5175 	u32 tmp = 0;
5176 
5177 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
5178 
5179 	with_intel_display_power_if_enabled(dev_priv, power_domain, wakeref)
5180 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
5181 
5182 	return tmp & TRANS_DDI_FUNC_ENABLE;
5183 }
5184 
5185 static u8 hsw_panel_transcoders(struct drm_i915_private *i915)
5186 {
5187 	u8 panel_transcoder_mask = BIT(TRANSCODER_EDP);
5188 
5189 	if (DISPLAY_VER(i915) >= 11)
5190 		panel_transcoder_mask |= BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
5191 
5192 	return panel_transcoder_mask;
5193 }
5194 
5195 static u8 hsw_enabled_transcoders(struct intel_crtc *crtc)
5196 {
5197 	struct drm_device *dev = crtc->base.dev;
5198 	struct drm_i915_private *dev_priv = to_i915(dev);
5199 	u8 panel_transcoder_mask = hsw_panel_transcoders(dev_priv);
5200 	enum transcoder cpu_transcoder;
5201 	u8 enabled_transcoders = 0;
5202 
5203 	/*
5204 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
5205 	 * consistency and less surprising code; it's in always on power).
5206 	 */
5207 	for_each_cpu_transcoder_masked(dev_priv, cpu_transcoder,
5208 				       panel_transcoder_mask) {
5209 		enum intel_display_power_domain power_domain;
5210 		intel_wakeref_t wakeref;
5211 		enum pipe trans_pipe;
5212 		u32 tmp = 0;
5213 
5214 		power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
5215 		with_intel_display_power_if_enabled(dev_priv, power_domain, wakeref)
5216 			tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(cpu_transcoder));
5217 
5218 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
5219 			continue;
5220 
5221 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
5222 		default:
5223 			drm_WARN(dev, 1,
5224 				 "unknown pipe linked to transcoder %s\n",
5225 				 transcoder_name(cpu_transcoder));
5226 			fallthrough;
5227 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
5228 		case TRANS_DDI_EDP_INPUT_A_ON:
5229 			trans_pipe = PIPE_A;
5230 			break;
5231 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
5232 			trans_pipe = PIPE_B;
5233 			break;
5234 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
5235 			trans_pipe = PIPE_C;
5236 			break;
5237 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
5238 			trans_pipe = PIPE_D;
5239 			break;
5240 		}
5241 
5242 		if (trans_pipe == crtc->pipe)
5243 			enabled_transcoders |= BIT(cpu_transcoder);
5244 	}
5245 
5246 	cpu_transcoder = (enum transcoder) crtc->pipe;
5247 	if (transcoder_ddi_func_is_enabled(dev_priv, cpu_transcoder))
5248 		enabled_transcoders |= BIT(cpu_transcoder);
5249 
5250 	return enabled_transcoders;
5251 }
5252 
5253 static bool has_edp_transcoders(u8 enabled_transcoders)
5254 {
5255 	return enabled_transcoders & BIT(TRANSCODER_EDP);
5256 }
5257 
5258 static bool has_dsi_transcoders(u8 enabled_transcoders)
5259 {
5260 	return enabled_transcoders & (BIT(TRANSCODER_DSI_0) |
5261 				      BIT(TRANSCODER_DSI_1));
5262 }
5263 
5264 static bool has_pipe_transcoders(u8 enabled_transcoders)
5265 {
5266 	return enabled_transcoders & ~(BIT(TRANSCODER_EDP) |
5267 				       BIT(TRANSCODER_DSI_0) |
5268 				       BIT(TRANSCODER_DSI_1));
5269 }
5270 
5271 static void assert_enabled_transcoders(struct drm_i915_private *i915,
5272 				       u8 enabled_transcoders)
5273 {
5274 	/* Only one type of transcoder please */
5275 	drm_WARN_ON(&i915->drm,
5276 		    has_edp_transcoders(enabled_transcoders) +
5277 		    has_dsi_transcoders(enabled_transcoders) +
5278 		    has_pipe_transcoders(enabled_transcoders) > 1);
5279 
5280 	/* Only DSI transcoders can be ganged */
5281 	drm_WARN_ON(&i915->drm,
5282 		    !has_dsi_transcoders(enabled_transcoders) &&
5283 		    !is_power_of_2(enabled_transcoders));
5284 }
5285 
5286 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
5287 				     struct intel_crtc_state *pipe_config,
5288 				     struct intel_display_power_domain_set *power_domain_set)
5289 {
5290 	struct drm_device *dev = crtc->base.dev;
5291 	struct drm_i915_private *dev_priv = to_i915(dev);
5292 	unsigned long enabled_transcoders;
5293 	u32 tmp;
5294 
5295 	enabled_transcoders = hsw_enabled_transcoders(crtc);
5296 	if (!enabled_transcoders)
5297 		return false;
5298 
5299 	assert_enabled_transcoders(dev_priv, enabled_transcoders);
5300 
5301 	/*
5302 	 * With the exception of DSI we should only ever have
5303 	 * a single enabled transcoder. With DSI let's just
5304 	 * pick the first one.
5305 	 */
5306 	pipe_config->cpu_transcoder = ffs(enabled_transcoders) - 1;
5307 
5308 	if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
5309 						       POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
5310 		return false;
5311 
5312 	if (hsw_panel_transcoders(dev_priv) & BIT(pipe_config->cpu_transcoder)) {
5313 		tmp = intel_de_read(dev_priv, TRANS_DDI_FUNC_CTL(pipe_config->cpu_transcoder));
5314 
5315 		if ((tmp & TRANS_DDI_EDP_INPUT_MASK) == TRANS_DDI_EDP_INPUT_A_ONOFF)
5316 			pipe_config->pch_pfit.force_thru = true;
5317 	}
5318 
5319 	tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
5320 
5321 	return tmp & PIPECONF_ENABLE;
5322 }
5323 
5324 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
5325 					 struct intel_crtc_state *pipe_config,
5326 					 struct intel_display_power_domain_set *power_domain_set)
5327 {
5328 	struct drm_device *dev = crtc->base.dev;
5329 	struct drm_i915_private *dev_priv = to_i915(dev);
5330 	enum transcoder cpu_transcoder;
5331 	enum port port;
5332 	u32 tmp;
5333 
5334 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
5335 		if (port == PORT_A)
5336 			cpu_transcoder = TRANSCODER_DSI_A;
5337 		else
5338 			cpu_transcoder = TRANSCODER_DSI_C;
5339 
5340 		if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
5341 							       POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
5342 			continue;
5343 
5344 		/*
5345 		 * The PLL needs to be enabled with a valid divider
5346 		 * configuration, otherwise accessing DSI registers will hang
5347 		 * the machine. See BSpec North Display Engine
5348 		 * registers/MIPI[BXT]. We can break out here early, since we
5349 		 * need the same DSI PLL to be enabled for both DSI ports.
5350 		 */
5351 		if (!bxt_dsi_pll_is_enabled(dev_priv))
5352 			break;
5353 
5354 		/* XXX: this works for video mode only */
5355 		tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
5356 		if (!(tmp & DPI_ENABLE))
5357 			continue;
5358 
5359 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
5360 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
5361 			continue;
5362 
5363 		pipe_config->cpu_transcoder = cpu_transcoder;
5364 		break;
5365 	}
5366 
5367 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
5368 }
5369 
5370 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
5371 				   struct intel_crtc_state *pipe_config)
5372 {
5373 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5374 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5375 	enum port port;
5376 	u32 tmp;
5377 
5378 	if (transcoder_is_dsi(cpu_transcoder)) {
5379 		port = (cpu_transcoder == TRANSCODER_DSI_A) ?
5380 						PORT_A : PORT_B;
5381 	} else {
5382 		tmp = intel_de_read(dev_priv,
5383 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
5384 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
5385 			return;
5386 		if (DISPLAY_VER(dev_priv) >= 12)
5387 			port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
5388 		else
5389 			port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
5390 	}
5391 
5392 	/*
5393 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
5394 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
5395 	 * the PCH transcoder is on.
5396 	 */
5397 	if (DISPLAY_VER(dev_priv) < 9 &&
5398 	    (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
5399 		pipe_config->has_pch_encoder = true;
5400 
5401 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
5402 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
5403 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
5404 
5405 		ilk_get_fdi_m_n_config(crtc, pipe_config);
5406 	}
5407 }
5408 
5409 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
5410 				struct intel_crtc_state *pipe_config)
5411 {
5412 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5413 	struct intel_display_power_domain_set power_domain_set = { };
5414 	bool active;
5415 	u32 tmp;
5416 
5417 	if (!intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
5418 						       POWER_DOMAIN_PIPE(crtc->pipe)))
5419 		return false;
5420 
5421 	pipe_config->shared_dpll = NULL;
5422 
5423 	active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_set);
5424 
5425 	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
5426 	    bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_set)) {
5427 		drm_WARN_ON(&dev_priv->drm, active);
5428 		active = true;
5429 	}
5430 
5431 	intel_dsc_get_config(pipe_config);
5432 	if (DISPLAY_VER(dev_priv) >= 13 && !pipe_config->dsc.compression_enable)
5433 		intel_uncompressed_joiner_get_config(pipe_config);
5434 
5435 	if (!active) {
5436 		/* bigjoiner slave doesn't enable transcoder */
5437 		if (!pipe_config->bigjoiner_slave)
5438 			goto out;
5439 
5440 		active = true;
5441 		pipe_config->pixel_multiplier = 1;
5442 
5443 		/* we cannot read out most state, so don't bother.. */
5444 		pipe_config->quirks |= PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE;
5445 	} else if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
5446 	    DISPLAY_VER(dev_priv) >= 11) {
5447 		hsw_get_ddi_port_state(crtc, pipe_config);
5448 		intel_get_transcoder_timings(crtc, pipe_config);
5449 	}
5450 
5451 	if (HAS_VRR(dev_priv) && !transcoder_is_dsi(pipe_config->cpu_transcoder))
5452 		intel_vrr_get_config(crtc, pipe_config);
5453 
5454 	intel_get_pipe_src_size(crtc, pipe_config);
5455 
5456 	if (IS_HASWELL(dev_priv)) {
5457 		u32 tmp = intel_de_read(dev_priv,
5458 					PIPECONF(pipe_config->cpu_transcoder));
5459 
5460 		if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
5461 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
5462 		else
5463 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
5464 	} else {
5465 		pipe_config->output_format =
5466 			bdw_get_pipemisc_output_format(crtc);
5467 	}
5468 
5469 	pipe_config->gamma_mode = intel_de_read(dev_priv,
5470 						GAMMA_MODE(crtc->pipe));
5471 
5472 	pipe_config->csc_mode = intel_de_read(dev_priv,
5473 					      PIPE_CSC_MODE(crtc->pipe));
5474 
5475 	if (DISPLAY_VER(dev_priv) >= 9) {
5476 		tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
5477 
5478 		if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
5479 			pipe_config->gamma_enable = true;
5480 
5481 		if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
5482 			pipe_config->csc_enable = true;
5483 	} else {
5484 		i9xx_get_pipe_color_config(pipe_config);
5485 	}
5486 
5487 	intel_color_get_config(pipe_config);
5488 
5489 	tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
5490 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
5491 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
5492 		pipe_config->ips_linetime =
5493 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
5494 
5495 	if (intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
5496 						      POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe))) {
5497 		if (DISPLAY_VER(dev_priv) >= 9)
5498 			skl_get_pfit_config(pipe_config);
5499 		else
5500 			ilk_get_pfit_config(pipe_config);
5501 	}
5502 
5503 	if (hsw_crtc_supports_ips(crtc)) {
5504 		if (IS_HASWELL(dev_priv))
5505 			pipe_config->ips_enabled = intel_de_read(dev_priv,
5506 								 IPS_CTL) & IPS_ENABLE;
5507 		else {
5508 			/*
5509 			 * We cannot readout IPS state on broadwell, set to
5510 			 * true so we can set it to a defined state on first
5511 			 * commit.
5512 			 */
5513 			pipe_config->ips_enabled = true;
5514 		}
5515 	}
5516 
5517 	if (pipe_config->bigjoiner_slave) {
5518 		/* Cannot be read out as a slave, set to 0. */
5519 		pipe_config->pixel_multiplier = 0;
5520 	} else if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
5521 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
5522 		pipe_config->pixel_multiplier =
5523 			intel_de_read(dev_priv,
5524 				      PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
5525 	} else {
5526 		pipe_config->pixel_multiplier = 1;
5527 	}
5528 
5529 out:
5530 	intel_display_power_put_all_in_set(dev_priv, &power_domain_set);
5531 
5532 	return active;
5533 }
5534 
5535 static bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
5536 {
5537 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5538 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
5539 
5540 	if (!i915->display->get_pipe_config(crtc, crtc_state))
5541 		return false;
5542 
5543 	crtc_state->hw.active = true;
5544 
5545 	intel_crtc_readout_derived_state(crtc_state);
5546 
5547 	return true;
5548 }
5549 
5550 /* VESA 640x480x72Hz mode to set on the pipe */
5551 static const struct drm_display_mode load_detect_mode = {
5552 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
5553 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
5554 };
5555 
5556 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
5557 					struct drm_crtc *crtc)
5558 {
5559 	struct drm_plane *plane;
5560 	struct drm_plane_state *plane_state;
5561 	int ret, i;
5562 
5563 	ret = drm_atomic_add_affected_planes(state, crtc);
5564 	if (ret)
5565 		return ret;
5566 
5567 	for_each_new_plane_in_state(state, plane, plane_state, i) {
5568 		if (plane_state->crtc != crtc)
5569 			continue;
5570 
5571 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
5572 		if (ret)
5573 			return ret;
5574 
5575 		drm_atomic_set_fb_for_plane(plane_state, NULL);
5576 	}
5577 
5578 	return 0;
5579 }
5580 
5581 int intel_get_load_detect_pipe(struct drm_connector *connector,
5582 			       struct intel_load_detect_pipe *old,
5583 			       struct drm_modeset_acquire_ctx *ctx)
5584 {
5585 	struct intel_encoder *encoder =
5586 		intel_attached_encoder(to_intel_connector(connector));
5587 	struct intel_crtc *possible_crtc;
5588 	struct intel_crtc *crtc = NULL;
5589 	struct drm_device *dev = encoder->base.dev;
5590 	struct drm_i915_private *dev_priv = to_i915(dev);
5591 	struct drm_mode_config *config = &dev->mode_config;
5592 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
5593 	struct drm_connector_state *connector_state;
5594 	struct intel_crtc_state *crtc_state;
5595 	int ret;
5596 
5597 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5598 		    connector->base.id, connector->name,
5599 		    encoder->base.base.id, encoder->base.name);
5600 
5601 	old->restore_state = NULL;
5602 
5603 	drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
5604 
5605 	/*
5606 	 * Algorithm gets a little messy:
5607 	 *
5608 	 *   - if the connector already has an assigned crtc, use it (but make
5609 	 *     sure it's on first)
5610 	 *
5611 	 *   - try to find the first unused crtc that can drive this connector,
5612 	 *     and use that if we find one
5613 	 */
5614 
5615 	/* See if we already have a CRTC for this connector */
5616 	if (connector->state->crtc) {
5617 		crtc = to_intel_crtc(connector->state->crtc);
5618 
5619 		ret = drm_modeset_lock(&crtc->base.mutex, ctx);
5620 		if (ret)
5621 			goto fail;
5622 
5623 		/* Make sure the crtc and connector are running */
5624 		goto found;
5625 	}
5626 
5627 	/* Find an unused one (if possible) */
5628 	for_each_intel_crtc(dev, possible_crtc) {
5629 		if (!(encoder->base.possible_crtcs &
5630 		      drm_crtc_mask(&possible_crtc->base)))
5631 			continue;
5632 
5633 		ret = drm_modeset_lock(&possible_crtc->base.mutex, ctx);
5634 		if (ret)
5635 			goto fail;
5636 
5637 		if (possible_crtc->base.state->enable) {
5638 			drm_modeset_unlock(&possible_crtc->base.mutex);
5639 			continue;
5640 		}
5641 
5642 		crtc = possible_crtc;
5643 		break;
5644 	}
5645 
5646 	/*
5647 	 * If we didn't find an unused CRTC, don't use any.
5648 	 */
5649 	if (!crtc) {
5650 		drm_dbg_kms(&dev_priv->drm,
5651 			    "no pipe available for load-detect\n");
5652 		ret = -ENODEV;
5653 		goto fail;
5654 	}
5655 
5656 found:
5657 	state = drm_atomic_state_alloc(dev);
5658 	restore_state = drm_atomic_state_alloc(dev);
5659 	if (!state || !restore_state) {
5660 		ret = -ENOMEM;
5661 		goto fail;
5662 	}
5663 
5664 	state->acquire_ctx = ctx;
5665 	restore_state->acquire_ctx = ctx;
5666 
5667 	connector_state = drm_atomic_get_connector_state(state, connector);
5668 	if (IS_ERR(connector_state)) {
5669 		ret = PTR_ERR(connector_state);
5670 		goto fail;
5671 	}
5672 
5673 	ret = drm_atomic_set_crtc_for_connector(connector_state, &crtc->base);
5674 	if (ret)
5675 		goto fail;
5676 
5677 	crtc_state = intel_atomic_get_crtc_state(state, crtc);
5678 	if (IS_ERR(crtc_state)) {
5679 		ret = PTR_ERR(crtc_state);
5680 		goto fail;
5681 	}
5682 
5683 	crtc_state->uapi.active = true;
5684 
5685 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
5686 					   &load_detect_mode);
5687 	if (ret)
5688 		goto fail;
5689 
5690 	ret = intel_modeset_disable_planes(state, &crtc->base);
5691 	if (ret)
5692 		goto fail;
5693 
5694 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
5695 	if (!ret)
5696 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, &crtc->base));
5697 	if (!ret)
5698 		ret = drm_atomic_add_affected_planes(restore_state, &crtc->base);
5699 	if (ret) {
5700 		drm_dbg_kms(&dev_priv->drm,
5701 			    "Failed to create a copy of old state to restore: %i\n",
5702 			    ret);
5703 		goto fail;
5704 	}
5705 
5706 	ret = drm_atomic_commit(state);
5707 	if (ret) {
5708 		drm_dbg_kms(&dev_priv->drm,
5709 			    "failed to set mode on load-detect pipe\n");
5710 		goto fail;
5711 	}
5712 
5713 	old->restore_state = restore_state;
5714 	drm_atomic_state_put(state);
5715 
5716 	/* let the connector get through one full cycle before testing */
5717 	intel_wait_for_vblank(dev_priv, crtc->pipe);
5718 	return true;
5719 
5720 fail:
5721 	if (state) {
5722 		drm_atomic_state_put(state);
5723 		state = NULL;
5724 	}
5725 	if (restore_state) {
5726 		drm_atomic_state_put(restore_state);
5727 		restore_state = NULL;
5728 	}
5729 
5730 	if (ret == -EDEADLK)
5731 		return ret;
5732 
5733 	return false;
5734 }
5735 
5736 void intel_release_load_detect_pipe(struct drm_connector *connector,
5737 				    struct intel_load_detect_pipe *old,
5738 				    struct drm_modeset_acquire_ctx *ctx)
5739 {
5740 	struct intel_encoder *intel_encoder =
5741 		intel_attached_encoder(to_intel_connector(connector));
5742 	struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
5743 	struct drm_encoder *encoder = &intel_encoder->base;
5744 	struct drm_atomic_state *state = old->restore_state;
5745 	int ret;
5746 
5747 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
5748 		    connector->base.id, connector->name,
5749 		    encoder->base.id, encoder->name);
5750 
5751 	if (!state)
5752 		return;
5753 
5754 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
5755 	if (ret)
5756 		drm_dbg_kms(&i915->drm,
5757 			    "Couldn't release load detect pipe: %i\n", ret);
5758 	drm_atomic_state_put(state);
5759 }
5760 
5761 static int i9xx_pll_refclk(struct drm_device *dev,
5762 			   const struct intel_crtc_state *pipe_config)
5763 {
5764 	struct drm_i915_private *dev_priv = to_i915(dev);
5765 	u32 dpll = pipe_config->dpll_hw_state.dpll;
5766 
5767 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
5768 		return dev_priv->vbt.lvds_ssc_freq;
5769 	else if (HAS_PCH_SPLIT(dev_priv))
5770 		return 120000;
5771 	else if (DISPLAY_VER(dev_priv) != 2)
5772 		return 96000;
5773 	else
5774 		return 48000;
5775 }
5776 
5777 /* Returns the clock of the currently programmed mode of the given pipe. */
5778 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
5779 				struct intel_crtc_state *pipe_config)
5780 {
5781 	struct drm_device *dev = crtc->base.dev;
5782 	struct drm_i915_private *dev_priv = to_i915(dev);
5783 	u32 dpll = pipe_config->dpll_hw_state.dpll;
5784 	u32 fp;
5785 	struct dpll clock;
5786 	int port_clock;
5787 	int refclk = i9xx_pll_refclk(dev, pipe_config);
5788 
5789 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
5790 		fp = pipe_config->dpll_hw_state.fp0;
5791 	else
5792 		fp = pipe_config->dpll_hw_state.fp1;
5793 
5794 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
5795 	if (IS_PINEVIEW(dev_priv)) {
5796 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
5797 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
5798 	} else {
5799 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
5800 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
5801 	}
5802 
5803 	if (DISPLAY_VER(dev_priv) != 2) {
5804 		if (IS_PINEVIEW(dev_priv))
5805 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
5806 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
5807 		else
5808 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
5809 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
5810 
5811 		switch (dpll & DPLL_MODE_MASK) {
5812 		case DPLLB_MODE_DAC_SERIAL:
5813 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
5814 				5 : 10;
5815 			break;
5816 		case DPLLB_MODE_LVDS:
5817 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
5818 				7 : 14;
5819 			break;
5820 		default:
5821 			drm_dbg_kms(&dev_priv->drm,
5822 				    "Unknown DPLL mode %08x in programmed "
5823 				    "mode\n", (int)(dpll & DPLL_MODE_MASK));
5824 			return;
5825 		}
5826 
5827 		if (IS_PINEVIEW(dev_priv))
5828 			port_clock = pnv_calc_dpll_params(refclk, &clock);
5829 		else
5830 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
5831 	} else {
5832 		enum pipe lvds_pipe;
5833 
5834 		if (IS_I85X(dev_priv) &&
5835 		    intel_lvds_port_enabled(dev_priv, LVDS, &lvds_pipe) &&
5836 		    lvds_pipe == crtc->pipe) {
5837 			u32 lvds = intel_de_read(dev_priv, LVDS);
5838 
5839 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
5840 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
5841 
5842 			if (lvds & LVDS_CLKB_POWER_UP)
5843 				clock.p2 = 7;
5844 			else
5845 				clock.p2 = 14;
5846 		} else {
5847 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
5848 				clock.p1 = 2;
5849 			else {
5850 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
5851 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
5852 			}
5853 			if (dpll & PLL_P2_DIVIDE_BY_4)
5854 				clock.p2 = 4;
5855 			else
5856 				clock.p2 = 2;
5857 		}
5858 
5859 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
5860 	}
5861 
5862 	/*
5863 	 * This value includes pixel_multiplier. We will use
5864 	 * port_clock to compute adjusted_mode.crtc_clock in the
5865 	 * encoder's get_config() function.
5866 	 */
5867 	pipe_config->port_clock = port_clock;
5868 }
5869 
5870 int intel_dotclock_calculate(int link_freq,
5871 			     const struct intel_link_m_n *m_n)
5872 {
5873 	/*
5874 	 * The calculation for the data clock is:
5875 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
5876 	 * But we want to avoid losing precison if possible, so:
5877 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
5878 	 *
5879 	 * and the link clock is simpler:
5880 	 * link_clock = (m * link_clock) / n
5881 	 */
5882 
5883 	if (!m_n->link_n)
5884 		return 0;
5885 
5886 	return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
5887 }
5888 
5889 static void ilk_pch_clock_get(struct intel_crtc *crtc,
5890 			      struct intel_crtc_state *pipe_config)
5891 {
5892 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5893 
5894 	/* read out port_clock from the DPLL */
5895 	i9xx_crtc_clock_get(crtc, pipe_config);
5896 
5897 	/*
5898 	 * In case there is an active pipe without active ports,
5899 	 * we may need some idea for the dotclock anyway.
5900 	 * Calculate one based on the FDI configuration.
5901 	 */
5902 	pipe_config->hw.adjusted_mode.crtc_clock =
5903 		intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
5904 					 &pipe_config->fdi_m_n);
5905 }
5906 
5907 /* Returns the currently programmed mode of the given encoder. */
5908 struct drm_display_mode *
5909 intel_encoder_current_mode(struct intel_encoder *encoder)
5910 {
5911 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
5912 	struct intel_crtc_state *crtc_state;
5913 	struct drm_display_mode *mode;
5914 	struct intel_crtc *crtc;
5915 	enum pipe pipe;
5916 
5917 	if (!encoder->get_hw_state(encoder, &pipe))
5918 		return NULL;
5919 
5920 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
5921 
5922 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
5923 	if (!mode)
5924 		return NULL;
5925 
5926 	crtc_state = intel_crtc_state_alloc(crtc);
5927 	if (!crtc_state) {
5928 		kfree(mode);
5929 		return NULL;
5930 	}
5931 
5932 	if (!intel_crtc_get_pipe_config(crtc_state)) {
5933 		kfree(crtc_state);
5934 		kfree(mode);
5935 		return NULL;
5936 	}
5937 
5938 	intel_encoder_get_config(encoder, crtc_state);
5939 
5940 	intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
5941 
5942 	kfree(crtc_state);
5943 
5944 	return mode;
5945 }
5946 
5947 /**
5948  * intel_wm_need_update - Check whether watermarks need updating
5949  * @cur: current plane state
5950  * @new: new plane state
5951  *
5952  * Check current plane state versus the new one to determine whether
5953  * watermarks need to be recalculated.
5954  *
5955  * Returns true or false.
5956  */
5957 static bool intel_wm_need_update(const struct intel_plane_state *cur,
5958 				 struct intel_plane_state *new)
5959 {
5960 	/* Update watermarks on tiling or size changes. */
5961 	if (new->uapi.visible != cur->uapi.visible)
5962 		return true;
5963 
5964 	if (!cur->hw.fb || !new->hw.fb)
5965 		return false;
5966 
5967 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
5968 	    cur->hw.rotation != new->hw.rotation ||
5969 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
5970 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
5971 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
5972 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
5973 		return true;
5974 
5975 	return false;
5976 }
5977 
5978 static bool needs_scaling(const struct intel_plane_state *state)
5979 {
5980 	int src_w = drm_rect_width(&state->uapi.src) >> 16;
5981 	int src_h = drm_rect_height(&state->uapi.src) >> 16;
5982 	int dst_w = drm_rect_width(&state->uapi.dst);
5983 	int dst_h = drm_rect_height(&state->uapi.dst);
5984 
5985 	return (src_w != dst_w || src_h != dst_h);
5986 }
5987 
5988 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
5989 				    struct intel_crtc_state *new_crtc_state,
5990 				    const struct intel_plane_state *old_plane_state,
5991 				    struct intel_plane_state *new_plane_state)
5992 {
5993 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
5994 	struct intel_plane *plane = to_intel_plane(new_plane_state->uapi.plane);
5995 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5996 	bool mode_changed = intel_crtc_needs_modeset(new_crtc_state);
5997 	bool was_crtc_enabled = old_crtc_state->hw.active;
5998 	bool is_crtc_enabled = new_crtc_state->hw.active;
5999 	bool turn_off, turn_on, visible, was_visible;
6000 	int ret;
6001 
6002 	if (DISPLAY_VER(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
6003 		ret = skl_update_scaler_plane(new_crtc_state, new_plane_state);
6004 		if (ret)
6005 			return ret;
6006 	}
6007 
6008 	was_visible = old_plane_state->uapi.visible;
6009 	visible = new_plane_state->uapi.visible;
6010 
6011 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
6012 		was_visible = false;
6013 
6014 	/*
6015 	 * Visibility is calculated as if the crtc was on, but
6016 	 * after scaler setup everything depends on it being off
6017 	 * when the crtc isn't active.
6018 	 *
6019 	 * FIXME this is wrong for watermarks. Watermarks should also
6020 	 * be computed as if the pipe would be active. Perhaps move
6021 	 * per-plane wm computation to the .check_plane() hook, and
6022 	 * only combine the results from all planes in the current place?
6023 	 */
6024 	if (!is_crtc_enabled) {
6025 		intel_plane_set_invisible(new_crtc_state, new_plane_state);
6026 		visible = false;
6027 	}
6028 
6029 	if (!was_visible && !visible)
6030 		return 0;
6031 
6032 	turn_off = was_visible && (!visible || mode_changed);
6033 	turn_on = visible && (!was_visible || mode_changed);
6034 
6035 	drm_dbg_atomic(&dev_priv->drm,
6036 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
6037 		       crtc->base.base.id, crtc->base.name,
6038 		       plane->base.base.id, plane->base.name,
6039 		       was_visible, visible,
6040 		       turn_off, turn_on, mode_changed);
6041 
6042 	if (turn_on) {
6043 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
6044 			new_crtc_state->update_wm_pre = true;
6045 
6046 		/* must disable cxsr around plane enable/disable */
6047 		if (plane->id != PLANE_CURSOR)
6048 			new_crtc_state->disable_cxsr = true;
6049 	} else if (turn_off) {
6050 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv))
6051 			new_crtc_state->update_wm_post = true;
6052 
6053 		/* must disable cxsr around plane enable/disable */
6054 		if (plane->id != PLANE_CURSOR)
6055 			new_crtc_state->disable_cxsr = true;
6056 	} else if (intel_wm_need_update(old_plane_state, new_plane_state)) {
6057 		if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv)) {
6058 			/* FIXME bollocks */
6059 			new_crtc_state->update_wm_pre = true;
6060 			new_crtc_state->update_wm_post = true;
6061 		}
6062 	}
6063 
6064 	if (visible || was_visible)
6065 		new_crtc_state->fb_bits |= plane->frontbuffer_bit;
6066 
6067 	/*
6068 	 * ILK/SNB DVSACNTR/Sprite Enable
6069 	 * IVB SPR_CTL/Sprite Enable
6070 	 * "When in Self Refresh Big FIFO mode, a write to enable the
6071 	 *  plane will be internally buffered and delayed while Big FIFO
6072 	 *  mode is exiting."
6073 	 *
6074 	 * Which means that enabling the sprite can take an extra frame
6075 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
6076 	 * down to LP0 and wait for vblank in order to make sure the
6077 	 * sprite gets enabled on the next vblank after the register write.
6078 	 * Doing otherwise would risk enabling the sprite one frame after
6079 	 * we've already signalled flip completion. We can resume LP1+
6080 	 * once the sprite has been enabled.
6081 	 *
6082 	 *
6083 	 * WaCxSRDisabledForSpriteScaling:ivb
6084 	 * IVB SPR_SCALE/Scaling Enable
6085 	 * "Low Power watermarks must be disabled for at least one
6086 	 *  frame before enabling sprite scaling, and kept disabled
6087 	 *  until sprite scaling is disabled."
6088 	 *
6089 	 * ILK/SNB DVSASCALE/Scaling Enable
6090 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
6091 	 *  masked off while Big FIFO mode is exiting."
6092 	 *
6093 	 * Despite the w/a only being listed for IVB we assume that
6094 	 * the ILK/SNB note has similar ramifications, hence we apply
6095 	 * the w/a on all three platforms.
6096 	 *
6097 	 * With experimental results seems this is needed also for primary
6098 	 * plane, not only sprite plane.
6099 	 */
6100 	if (plane->id != PLANE_CURSOR &&
6101 	    (IS_IRONLAKE(dev_priv) || IS_SANDYBRIDGE(dev_priv) ||
6102 	     IS_IVYBRIDGE(dev_priv)) &&
6103 	    (turn_on || (!needs_scaling(old_plane_state) &&
6104 			 needs_scaling(new_plane_state))))
6105 		new_crtc_state->disable_lp_wm = true;
6106 
6107 	return 0;
6108 }
6109 
6110 static bool encoders_cloneable(const struct intel_encoder *a,
6111 			       const struct intel_encoder *b)
6112 {
6113 	/* masks could be asymmetric, so check both ways */
6114 	return a == b || (a->cloneable & (1 << b->type) &&
6115 			  b->cloneable & (1 << a->type));
6116 }
6117 
6118 static bool check_single_encoder_cloning(struct intel_atomic_state *state,
6119 					 struct intel_crtc *crtc,
6120 					 struct intel_encoder *encoder)
6121 {
6122 	struct intel_encoder *source_encoder;
6123 	struct drm_connector *connector;
6124 	struct drm_connector_state *connector_state;
6125 	int i;
6126 
6127 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
6128 		if (connector_state->crtc != &crtc->base)
6129 			continue;
6130 
6131 		source_encoder =
6132 			to_intel_encoder(connector_state->best_encoder);
6133 		if (!encoders_cloneable(encoder, source_encoder))
6134 			return false;
6135 	}
6136 
6137 	return true;
6138 }
6139 
6140 static int icl_add_linked_planes(struct intel_atomic_state *state)
6141 {
6142 	struct intel_plane *plane, *linked;
6143 	struct intel_plane_state *plane_state, *linked_plane_state;
6144 	int i;
6145 
6146 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
6147 		linked = plane_state->planar_linked_plane;
6148 
6149 		if (!linked)
6150 			continue;
6151 
6152 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
6153 		if (IS_ERR(linked_plane_state))
6154 			return PTR_ERR(linked_plane_state);
6155 
6156 		drm_WARN_ON(state->base.dev,
6157 			    linked_plane_state->planar_linked_plane != plane);
6158 		drm_WARN_ON(state->base.dev,
6159 			    linked_plane_state->planar_slave == plane_state->planar_slave);
6160 	}
6161 
6162 	return 0;
6163 }
6164 
6165 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
6166 {
6167 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6168 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6169 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
6170 	struct intel_plane *plane, *linked;
6171 	struct intel_plane_state *plane_state;
6172 	int i;
6173 
6174 	if (DISPLAY_VER(dev_priv) < 11)
6175 		return 0;
6176 
6177 	/*
6178 	 * Destroy all old plane links and make the slave plane invisible
6179 	 * in the crtc_state->active_planes mask.
6180 	 */
6181 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
6182 		if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
6183 			continue;
6184 
6185 		plane_state->planar_linked_plane = NULL;
6186 		if (plane_state->planar_slave && !plane_state->uapi.visible) {
6187 			crtc_state->enabled_planes &= ~BIT(plane->id);
6188 			crtc_state->active_planes &= ~BIT(plane->id);
6189 			crtc_state->update_planes |= BIT(plane->id);
6190 		}
6191 
6192 		plane_state->planar_slave = false;
6193 	}
6194 
6195 	if (!crtc_state->nv12_planes)
6196 		return 0;
6197 
6198 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
6199 		struct intel_plane_state *linked_state = NULL;
6200 
6201 		if (plane->pipe != crtc->pipe ||
6202 		    !(crtc_state->nv12_planes & BIT(plane->id)))
6203 			continue;
6204 
6205 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
6206 			if (!icl_is_nv12_y_plane(dev_priv, linked->id))
6207 				continue;
6208 
6209 			if (crtc_state->active_planes & BIT(linked->id))
6210 				continue;
6211 
6212 			linked_state = intel_atomic_get_plane_state(state, linked);
6213 			if (IS_ERR(linked_state))
6214 				return PTR_ERR(linked_state);
6215 
6216 			break;
6217 		}
6218 
6219 		if (!linked_state) {
6220 			drm_dbg_kms(&dev_priv->drm,
6221 				    "Need %d free Y planes for planar YUV\n",
6222 				    hweight8(crtc_state->nv12_planes));
6223 
6224 			return -EINVAL;
6225 		}
6226 
6227 		plane_state->planar_linked_plane = linked;
6228 
6229 		linked_state->planar_slave = true;
6230 		linked_state->planar_linked_plane = plane;
6231 		crtc_state->enabled_planes |= BIT(linked->id);
6232 		crtc_state->active_planes |= BIT(linked->id);
6233 		crtc_state->update_planes |= BIT(linked->id);
6234 		drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
6235 			    linked->base.name, plane->base.name);
6236 
6237 		/* Copy parameters to slave plane */
6238 		linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
6239 		linked_state->color_ctl = plane_state->color_ctl;
6240 		linked_state->view = plane_state->view;
6241 
6242 		intel_plane_copy_hw_state(linked_state, plane_state);
6243 		linked_state->uapi.src = plane_state->uapi.src;
6244 		linked_state->uapi.dst = plane_state->uapi.dst;
6245 
6246 		if (icl_is_hdr_plane(dev_priv, plane->id)) {
6247 			if (linked->id == PLANE_SPRITE5)
6248 				plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
6249 			else if (linked->id == PLANE_SPRITE4)
6250 				plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
6251 			else if (linked->id == PLANE_SPRITE3)
6252 				plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
6253 			else if (linked->id == PLANE_SPRITE2)
6254 				plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
6255 			else
6256 				MISSING_CASE(linked->id);
6257 		}
6258 	}
6259 
6260 	return 0;
6261 }
6262 
6263 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
6264 {
6265 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6266 	struct intel_atomic_state *state =
6267 		to_intel_atomic_state(new_crtc_state->uapi.state);
6268 	const struct intel_crtc_state *old_crtc_state =
6269 		intel_atomic_get_old_crtc_state(state, crtc);
6270 
6271 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
6272 }
6273 
6274 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
6275 {
6276 	const struct drm_display_mode *pipe_mode =
6277 		&crtc_state->hw.pipe_mode;
6278 	int linetime_wm;
6279 
6280 	if (!crtc_state->hw.enable)
6281 		return 0;
6282 
6283 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
6284 					pipe_mode->crtc_clock);
6285 
6286 	return min(linetime_wm, 0x1ff);
6287 }
6288 
6289 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
6290 			       const struct intel_cdclk_state *cdclk_state)
6291 {
6292 	const struct drm_display_mode *pipe_mode =
6293 		&crtc_state->hw.pipe_mode;
6294 	int linetime_wm;
6295 
6296 	if (!crtc_state->hw.enable)
6297 		return 0;
6298 
6299 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
6300 					cdclk_state->logical.cdclk);
6301 
6302 	return min(linetime_wm, 0x1ff);
6303 }
6304 
6305 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
6306 {
6307 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6308 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6309 	const struct drm_display_mode *pipe_mode =
6310 		&crtc_state->hw.pipe_mode;
6311 	int linetime_wm;
6312 
6313 	if (!crtc_state->hw.enable)
6314 		return 0;
6315 
6316 	linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
6317 				   crtc_state->pixel_rate);
6318 
6319 	/* Display WA #1135: BXT:ALL GLK:ALL */
6320 	if ((IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) &&
6321 	    dev_priv->ipc_enabled)
6322 		linetime_wm /= 2;
6323 
6324 	return min(linetime_wm, 0x1ff);
6325 }
6326 
6327 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
6328 				   struct intel_crtc *crtc)
6329 {
6330 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6331 	struct intel_crtc_state *crtc_state =
6332 		intel_atomic_get_new_crtc_state(state, crtc);
6333 	const struct intel_cdclk_state *cdclk_state;
6334 
6335 	if (DISPLAY_VER(dev_priv) >= 9)
6336 		crtc_state->linetime = skl_linetime_wm(crtc_state);
6337 	else
6338 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
6339 
6340 	if (!hsw_crtc_supports_ips(crtc))
6341 		return 0;
6342 
6343 	cdclk_state = intel_atomic_get_cdclk_state(state);
6344 	if (IS_ERR(cdclk_state))
6345 		return PTR_ERR(cdclk_state);
6346 
6347 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
6348 						       cdclk_state);
6349 
6350 	return 0;
6351 }
6352 
6353 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
6354 				   struct intel_crtc *crtc)
6355 {
6356 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6357 	struct intel_crtc_state *crtc_state =
6358 		intel_atomic_get_new_crtc_state(state, crtc);
6359 	bool mode_changed = intel_crtc_needs_modeset(crtc_state);
6360 	int ret;
6361 
6362 	if (DISPLAY_VER(dev_priv) < 5 && !IS_G4X(dev_priv) &&
6363 	    mode_changed && !crtc_state->hw.active)
6364 		crtc_state->update_wm_post = true;
6365 
6366 	if (mode_changed && crtc_state->hw.enable &&
6367 	    dev_priv->dpll_funcs &&
6368 	    !crtc_state->bigjoiner_slave &&
6369 	    !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
6370 		ret = dev_priv->dpll_funcs->crtc_compute_clock(crtc_state);
6371 		if (ret)
6372 			return ret;
6373 	}
6374 
6375 	/*
6376 	 * May need to update pipe gamma enable bits
6377 	 * when C8 planes are getting enabled/disabled.
6378 	 */
6379 	if (c8_planes_changed(crtc_state))
6380 		crtc_state->uapi.color_mgmt_changed = true;
6381 
6382 	if (mode_changed || crtc_state->update_pipe ||
6383 	    crtc_state->uapi.color_mgmt_changed) {
6384 		ret = intel_color_check(crtc_state);
6385 		if (ret)
6386 			return ret;
6387 	}
6388 
6389 	ret = intel_compute_pipe_wm(state, crtc);
6390 	if (ret) {
6391 		drm_dbg_kms(&dev_priv->drm,
6392 			    "Target pipe watermarks are invalid\n");
6393 		return ret;
6394 	}
6395 
6396 	/*
6397 	 * Calculate 'intermediate' watermarks that satisfy both the
6398 	 * old state and the new state.  We can program these
6399 	 * immediately.
6400 	 */
6401 	ret = intel_compute_intermediate_wm(state, crtc);
6402 	if (ret) {
6403 		drm_dbg_kms(&dev_priv->drm,
6404 			    "No valid intermediate pipe watermarks are possible\n");
6405 		return ret;
6406 	}
6407 
6408 	if (DISPLAY_VER(dev_priv) >= 9) {
6409 		if (mode_changed || crtc_state->update_pipe) {
6410 			ret = skl_update_scaler_crtc(crtc_state);
6411 			if (ret)
6412 				return ret;
6413 		}
6414 
6415 		ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
6416 		if (ret)
6417 			return ret;
6418 	}
6419 
6420 	if (HAS_IPS(dev_priv)) {
6421 		ret = hsw_compute_ips_config(crtc_state);
6422 		if (ret)
6423 			return ret;
6424 	}
6425 
6426 	if (DISPLAY_VER(dev_priv) >= 9 ||
6427 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
6428 		ret = hsw_compute_linetime_wm(state, crtc);
6429 		if (ret)
6430 			return ret;
6431 
6432 	}
6433 
6434 	ret = intel_psr2_sel_fetch_update(state, crtc);
6435 	if (ret)
6436 		return ret;
6437 
6438 	return 0;
6439 }
6440 
6441 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
6442 {
6443 	struct intel_connector *connector;
6444 	struct drm_connector_list_iter conn_iter;
6445 
6446 	drm_connector_list_iter_begin(dev, &conn_iter);
6447 	for_each_intel_connector_iter(connector, &conn_iter) {
6448 		struct drm_connector_state *conn_state = connector->base.state;
6449 		struct intel_encoder *encoder =
6450 			to_intel_encoder(connector->base.encoder);
6451 
6452 		if (conn_state->crtc)
6453 			drm_connector_put(&connector->base);
6454 
6455 		if (encoder) {
6456 			struct intel_crtc *crtc =
6457 				to_intel_crtc(encoder->base.crtc);
6458 			const struct intel_crtc_state *crtc_state =
6459 				to_intel_crtc_state(crtc->base.state);
6460 
6461 			conn_state->best_encoder = &encoder->base;
6462 			conn_state->crtc = &crtc->base;
6463 			conn_state->max_bpc = (crtc_state->pipe_bpp ?: 24) / 3;
6464 
6465 			drm_connector_get(&connector->base);
6466 		} else {
6467 			conn_state->best_encoder = NULL;
6468 			conn_state->crtc = NULL;
6469 		}
6470 	}
6471 	drm_connector_list_iter_end(&conn_iter);
6472 }
6473 
6474 static int
6475 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
6476 		      struct intel_crtc_state *pipe_config)
6477 {
6478 	struct drm_connector *connector = conn_state->connector;
6479 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
6480 	const struct drm_display_info *info = &connector->display_info;
6481 	int bpp;
6482 
6483 	switch (conn_state->max_bpc) {
6484 	case 6 ... 7:
6485 		bpp = 6 * 3;
6486 		break;
6487 	case 8 ... 9:
6488 		bpp = 8 * 3;
6489 		break;
6490 	case 10 ... 11:
6491 		bpp = 10 * 3;
6492 		break;
6493 	case 12 ... 16:
6494 		bpp = 12 * 3;
6495 		break;
6496 	default:
6497 		MISSING_CASE(conn_state->max_bpc);
6498 		return -EINVAL;
6499 	}
6500 
6501 	if (bpp < pipe_config->pipe_bpp) {
6502 		drm_dbg_kms(&i915->drm,
6503 			    "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
6504 			    "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
6505 			    connector->base.id, connector->name,
6506 			    bpp, 3 * info->bpc,
6507 			    3 * conn_state->max_requested_bpc,
6508 			    pipe_config->pipe_bpp);
6509 
6510 		pipe_config->pipe_bpp = bpp;
6511 	}
6512 
6513 	return 0;
6514 }
6515 
6516 static int
6517 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
6518 			  struct intel_crtc_state *pipe_config)
6519 {
6520 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6521 	struct drm_atomic_state *state = pipe_config->uapi.state;
6522 	struct drm_connector *connector;
6523 	struct drm_connector_state *connector_state;
6524 	int bpp, i;
6525 
6526 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
6527 	    IS_CHERRYVIEW(dev_priv)))
6528 		bpp = 10*3;
6529 	else if (DISPLAY_VER(dev_priv) >= 5)
6530 		bpp = 12*3;
6531 	else
6532 		bpp = 8*3;
6533 
6534 	pipe_config->pipe_bpp = bpp;
6535 
6536 	/* Clamp display bpp to connector max bpp */
6537 	for_each_new_connector_in_state(state, connector, connector_state, i) {
6538 		int ret;
6539 
6540 		if (connector_state->crtc != &crtc->base)
6541 			continue;
6542 
6543 		ret = compute_sink_pipe_bpp(connector_state, pipe_config);
6544 		if (ret)
6545 			return ret;
6546 	}
6547 
6548 	return 0;
6549 }
6550 
6551 static void intel_dump_crtc_timings(struct drm_i915_private *i915,
6552 				    const struct drm_display_mode *mode)
6553 {
6554 	drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
6555 		    "type: 0x%x flags: 0x%x\n",
6556 		    mode->crtc_clock,
6557 		    mode->crtc_hdisplay, mode->crtc_hsync_start,
6558 		    mode->crtc_hsync_end, mode->crtc_htotal,
6559 		    mode->crtc_vdisplay, mode->crtc_vsync_start,
6560 		    mode->crtc_vsync_end, mode->crtc_vtotal,
6561 		    mode->type, mode->flags);
6562 }
6563 
6564 static void
6565 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
6566 		      const char *id, unsigned int lane_count,
6567 		      const struct intel_link_m_n *m_n)
6568 {
6569 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
6570 
6571 	drm_dbg_kms(&i915->drm,
6572 		    "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
6573 		    id, lane_count,
6574 		    m_n->gmch_m, m_n->gmch_n,
6575 		    m_n->link_m, m_n->link_n, m_n->tu);
6576 }
6577 
6578 static void
6579 intel_dump_infoframe(struct drm_i915_private *dev_priv,
6580 		     const union hdmi_infoframe *frame)
6581 {
6582 	if (!drm_debug_enabled(DRM_UT_KMS))
6583 		return;
6584 
6585 	hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
6586 }
6587 
6588 static void
6589 intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
6590 		      const struct drm_dp_vsc_sdp *vsc)
6591 {
6592 	if (!drm_debug_enabled(DRM_UT_KMS))
6593 		return;
6594 
6595 	drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
6596 }
6597 
6598 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
6599 
6600 static const char * const output_type_str[] = {
6601 	OUTPUT_TYPE(UNUSED),
6602 	OUTPUT_TYPE(ANALOG),
6603 	OUTPUT_TYPE(DVO),
6604 	OUTPUT_TYPE(SDVO),
6605 	OUTPUT_TYPE(LVDS),
6606 	OUTPUT_TYPE(TVOUT),
6607 	OUTPUT_TYPE(HDMI),
6608 	OUTPUT_TYPE(DP),
6609 	OUTPUT_TYPE(EDP),
6610 	OUTPUT_TYPE(DSI),
6611 	OUTPUT_TYPE(DDI),
6612 	OUTPUT_TYPE(DP_MST),
6613 };
6614 
6615 #undef OUTPUT_TYPE
6616 
6617 static void snprintf_output_types(char *buf, size_t len,
6618 				  unsigned int output_types)
6619 {
6620 	char *str = buf;
6621 	int i;
6622 
6623 	str[0] = '\0';
6624 
6625 	for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
6626 		int r;
6627 
6628 		if ((output_types & BIT(i)) == 0)
6629 			continue;
6630 
6631 		r = snprintf(str, len, "%s%s",
6632 			     str != buf ? "," : "", output_type_str[i]);
6633 		if (r >= len)
6634 			break;
6635 		str += r;
6636 		len -= r;
6637 
6638 		output_types &= ~BIT(i);
6639 	}
6640 
6641 	WARN_ON_ONCE(output_types != 0);
6642 }
6643 
6644 static const char * const output_format_str[] = {
6645 	[INTEL_OUTPUT_FORMAT_RGB] = "RGB",
6646 	[INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
6647 	[INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
6648 };
6649 
6650 static const char *output_formats(enum intel_output_format format)
6651 {
6652 	if (format >= ARRAY_SIZE(output_format_str))
6653 		return "invalid";
6654 	return output_format_str[format];
6655 }
6656 
6657 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
6658 {
6659 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
6660 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
6661 	const struct drm_framebuffer *fb = plane_state->hw.fb;
6662 
6663 	if (!fb) {
6664 		drm_dbg_kms(&i915->drm,
6665 			    "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
6666 			    plane->base.base.id, plane->base.name,
6667 			    yesno(plane_state->uapi.visible));
6668 		return;
6669 	}
6670 
6671 	drm_dbg_kms(&i915->drm,
6672 		    "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %p4cc modifier = 0x%llx, visible: %s\n",
6673 		    plane->base.base.id, plane->base.name,
6674 		    fb->base.id, fb->width, fb->height, &fb->format->format,
6675 		    fb->modifier, yesno(plane_state->uapi.visible));
6676 	drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
6677 		    plane_state->hw.rotation, plane_state->scaler_id);
6678 	if (plane_state->uapi.visible)
6679 		drm_dbg_kms(&i915->drm,
6680 			    "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
6681 			    DRM_RECT_FP_ARG(&plane_state->uapi.src),
6682 			    DRM_RECT_ARG(&plane_state->uapi.dst));
6683 }
6684 
6685 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
6686 				   struct intel_atomic_state *state,
6687 				   const char *context)
6688 {
6689 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
6690 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6691 	const struct intel_plane_state *plane_state;
6692 	struct intel_plane *plane;
6693 	char buf[64];
6694 	int i;
6695 
6696 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
6697 		    crtc->base.base.id, crtc->base.name,
6698 		    yesno(pipe_config->hw.enable), context);
6699 
6700 	if (!pipe_config->hw.enable)
6701 		goto dump_planes;
6702 
6703 	snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
6704 	drm_dbg_kms(&dev_priv->drm,
6705 		    "active: %s, output_types: %s (0x%x), output format: %s\n",
6706 		    yesno(pipe_config->hw.active),
6707 		    buf, pipe_config->output_types,
6708 		    output_formats(pipe_config->output_format));
6709 
6710 	drm_dbg_kms(&dev_priv->drm,
6711 		    "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
6712 		    transcoder_name(pipe_config->cpu_transcoder),
6713 		    pipe_config->pipe_bpp, pipe_config->dither);
6714 
6715 	drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
6716 		    transcoder_name(pipe_config->mst_master_transcoder));
6717 
6718 	drm_dbg_kms(&dev_priv->drm,
6719 		    "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
6720 		    transcoder_name(pipe_config->master_transcoder),
6721 		    pipe_config->sync_mode_slaves_mask);
6722 
6723 	drm_dbg_kms(&dev_priv->drm, "bigjoiner: %s\n",
6724 		    pipe_config->bigjoiner_slave ? "slave" :
6725 		    pipe_config->bigjoiner ? "master" : "no");
6726 
6727 	drm_dbg_kms(&dev_priv->drm, "splitter: %s, link count %d, overlap %d\n",
6728 		    enableddisabled(pipe_config->splitter.enable),
6729 		    pipe_config->splitter.link_count,
6730 		    pipe_config->splitter.pixel_overlap);
6731 
6732 	if (pipe_config->has_pch_encoder)
6733 		intel_dump_m_n_config(pipe_config, "fdi",
6734 				      pipe_config->fdi_lanes,
6735 				      &pipe_config->fdi_m_n);
6736 
6737 	if (intel_crtc_has_dp_encoder(pipe_config)) {
6738 		intel_dump_m_n_config(pipe_config, "dp m_n",
6739 				pipe_config->lane_count, &pipe_config->dp_m_n);
6740 		if (pipe_config->has_drrs)
6741 			intel_dump_m_n_config(pipe_config, "dp m2_n2",
6742 					      pipe_config->lane_count,
6743 					      &pipe_config->dp_m2_n2);
6744 	}
6745 
6746 	drm_dbg_kms(&dev_priv->drm,
6747 		    "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
6748 		    pipe_config->has_audio, pipe_config->has_infoframe,
6749 		    pipe_config->infoframes.enable);
6750 
6751 	if (pipe_config->infoframes.enable &
6752 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
6753 		drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
6754 			    pipe_config->infoframes.gcp);
6755 	if (pipe_config->infoframes.enable &
6756 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
6757 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
6758 	if (pipe_config->infoframes.enable &
6759 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
6760 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
6761 	if (pipe_config->infoframes.enable &
6762 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
6763 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
6764 	if (pipe_config->infoframes.enable &
6765 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
6766 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
6767 	if (pipe_config->infoframes.enable &
6768 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
6769 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
6770 	if (pipe_config->infoframes.enable &
6771 	    intel_hdmi_infoframe_enable(DP_SDP_VSC))
6772 		intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
6773 
6774 	drm_dbg_kms(&dev_priv->drm, "vrr: %s, vmin: %d, vmax: %d, pipeline full: %d, guardband: %d flipline: %d, vmin vblank: %d, vmax vblank: %d\n",
6775 		    yesno(pipe_config->vrr.enable),
6776 		    pipe_config->vrr.vmin, pipe_config->vrr.vmax,
6777 		    pipe_config->vrr.pipeline_full, pipe_config->vrr.guardband,
6778 		    pipe_config->vrr.flipline,
6779 		    intel_vrr_vmin_vblank_start(pipe_config),
6780 		    intel_vrr_vmax_vblank_start(pipe_config));
6781 
6782 	drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
6783 	drm_mode_debug_printmodeline(&pipe_config->hw.mode);
6784 	drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
6785 	drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
6786 	intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
6787 	drm_dbg_kms(&dev_priv->drm, "pipe mode:\n");
6788 	drm_mode_debug_printmodeline(&pipe_config->hw.pipe_mode);
6789 	intel_dump_crtc_timings(dev_priv, &pipe_config->hw.pipe_mode);
6790 	drm_dbg_kms(&dev_priv->drm,
6791 		    "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
6792 		    pipe_config->port_clock,
6793 		    pipe_config->pipe_src_w, pipe_config->pipe_src_h,
6794 		    pipe_config->pixel_rate);
6795 
6796 	drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
6797 		    pipe_config->linetime, pipe_config->ips_linetime);
6798 
6799 	if (DISPLAY_VER(dev_priv) >= 9)
6800 		drm_dbg_kms(&dev_priv->drm,
6801 			    "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
6802 			    crtc->num_scalers,
6803 			    pipe_config->scaler_state.scaler_users,
6804 			    pipe_config->scaler_state.scaler_id);
6805 
6806 	if (HAS_GMCH(dev_priv))
6807 		drm_dbg_kms(&dev_priv->drm,
6808 			    "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
6809 			    pipe_config->gmch_pfit.control,
6810 			    pipe_config->gmch_pfit.pgm_ratios,
6811 			    pipe_config->gmch_pfit.lvds_border_bits);
6812 	else
6813 		drm_dbg_kms(&dev_priv->drm,
6814 			    "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
6815 			    DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
6816 			    enableddisabled(pipe_config->pch_pfit.enabled),
6817 			    yesno(pipe_config->pch_pfit.force_thru));
6818 
6819 	drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
6820 		    pipe_config->ips_enabled, pipe_config->double_wide);
6821 
6822 	intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
6823 
6824 	if (IS_CHERRYVIEW(dev_priv))
6825 		drm_dbg_kms(&dev_priv->drm,
6826 			    "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
6827 			    pipe_config->cgm_mode, pipe_config->gamma_mode,
6828 			    pipe_config->gamma_enable, pipe_config->csc_enable);
6829 	else
6830 		drm_dbg_kms(&dev_priv->drm,
6831 			    "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
6832 			    pipe_config->csc_mode, pipe_config->gamma_mode,
6833 			    pipe_config->gamma_enable, pipe_config->csc_enable);
6834 
6835 	drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
6836 		    pipe_config->hw.degamma_lut ?
6837 		    drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
6838 		    pipe_config->hw.gamma_lut ?
6839 		    drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
6840 
6841 dump_planes:
6842 	if (!state)
6843 		return;
6844 
6845 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
6846 		if (plane->pipe == crtc->pipe)
6847 			intel_dump_plane_state(plane_state);
6848 	}
6849 }
6850 
6851 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
6852 {
6853 	struct drm_device *dev = state->base.dev;
6854 	struct drm_connector *connector;
6855 	struct drm_connector_list_iter conn_iter;
6856 	unsigned int used_ports = 0;
6857 	unsigned int used_mst_ports = 0;
6858 	bool ret = true;
6859 
6860 	/*
6861 	 * We're going to peek into connector->state,
6862 	 * hence connection_mutex must be held.
6863 	 */
6864 	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
6865 
6866 	/*
6867 	 * Walk the connector list instead of the encoder
6868 	 * list to detect the problem on ddi platforms
6869 	 * where there's just one encoder per digital port.
6870 	 */
6871 	drm_connector_list_iter_begin(dev, &conn_iter);
6872 	drm_for_each_connector_iter(connector, &conn_iter) {
6873 		struct drm_connector_state *connector_state;
6874 		struct intel_encoder *encoder;
6875 
6876 		connector_state =
6877 			drm_atomic_get_new_connector_state(&state->base,
6878 							   connector);
6879 		if (!connector_state)
6880 			connector_state = connector->state;
6881 
6882 		if (!connector_state->best_encoder)
6883 			continue;
6884 
6885 		encoder = to_intel_encoder(connector_state->best_encoder);
6886 
6887 		drm_WARN_ON(dev, !connector_state->crtc);
6888 
6889 		switch (encoder->type) {
6890 		case INTEL_OUTPUT_DDI:
6891 			if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
6892 				break;
6893 			fallthrough;
6894 		case INTEL_OUTPUT_DP:
6895 		case INTEL_OUTPUT_HDMI:
6896 		case INTEL_OUTPUT_EDP:
6897 			/* the same port mustn't appear more than once */
6898 			if (used_ports & BIT(encoder->port))
6899 				ret = false;
6900 
6901 			used_ports |= BIT(encoder->port);
6902 			break;
6903 		case INTEL_OUTPUT_DP_MST:
6904 			used_mst_ports |=
6905 				1 << encoder->port;
6906 			break;
6907 		default:
6908 			break;
6909 		}
6910 	}
6911 	drm_connector_list_iter_end(&conn_iter);
6912 
6913 	/* can't mix MST and SST/HDMI on the same port */
6914 	if (used_ports & used_mst_ports)
6915 		return false;
6916 
6917 	return ret;
6918 }
6919 
6920 static void
6921 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
6922 					   struct intel_crtc_state *crtc_state)
6923 {
6924 	const struct intel_crtc_state *from_crtc_state = crtc_state;
6925 
6926 	if (crtc_state->bigjoiner_slave) {
6927 		from_crtc_state = intel_atomic_get_new_crtc_state(state,
6928 								  crtc_state->bigjoiner_linked_crtc);
6929 
6930 		/* No need to copy state if the master state is unchanged */
6931 		if (!from_crtc_state)
6932 			return;
6933 	}
6934 
6935 	intel_crtc_copy_color_blobs(crtc_state, from_crtc_state);
6936 }
6937 
6938 static void
6939 intel_crtc_copy_uapi_to_hw_state(struct intel_atomic_state *state,
6940 				 struct intel_crtc_state *crtc_state)
6941 {
6942 	crtc_state->hw.enable = crtc_state->uapi.enable;
6943 	crtc_state->hw.active = crtc_state->uapi.active;
6944 	crtc_state->hw.mode = crtc_state->uapi.mode;
6945 	crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
6946 	crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
6947 
6948 	intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc_state);
6949 }
6950 
6951 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
6952 {
6953 	if (crtc_state->bigjoiner_slave)
6954 		return;
6955 
6956 	crtc_state->uapi.enable = crtc_state->hw.enable;
6957 	crtc_state->uapi.active = crtc_state->hw.active;
6958 	drm_WARN_ON(crtc_state->uapi.crtc->dev,
6959 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
6960 
6961 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
6962 	crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
6963 
6964 	/* copy color blobs to uapi */
6965 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
6966 				  crtc_state->hw.degamma_lut);
6967 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
6968 				  crtc_state->hw.gamma_lut);
6969 	drm_property_replace_blob(&crtc_state->uapi.ctm,
6970 				  crtc_state->hw.ctm);
6971 }
6972 
6973 static int
6974 copy_bigjoiner_crtc_state(struct intel_crtc_state *crtc_state,
6975 			  const struct intel_crtc_state *from_crtc_state)
6976 {
6977 	struct intel_crtc_state *saved_state;
6978 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6979 
6980 	saved_state = kmemdup(from_crtc_state, sizeof(*saved_state), GFP_KERNEL);
6981 	if (!saved_state)
6982 		return -ENOMEM;
6983 
6984 	saved_state->uapi = crtc_state->uapi;
6985 	saved_state->scaler_state = crtc_state->scaler_state;
6986 	saved_state->shared_dpll = crtc_state->shared_dpll;
6987 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
6988 	saved_state->crc_enabled = crtc_state->crc_enabled;
6989 
6990 	intel_crtc_free_hw_state(crtc_state);
6991 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
6992 	kfree(saved_state);
6993 
6994 	/* Re-init hw state */
6995 	memset(&crtc_state->hw, 0, sizeof(saved_state->hw));
6996 	crtc_state->hw.enable = from_crtc_state->hw.enable;
6997 	crtc_state->hw.active = from_crtc_state->hw.active;
6998 	crtc_state->hw.pipe_mode = from_crtc_state->hw.pipe_mode;
6999 	crtc_state->hw.adjusted_mode = from_crtc_state->hw.adjusted_mode;
7000 
7001 	/* Some fixups */
7002 	crtc_state->uapi.mode_changed = from_crtc_state->uapi.mode_changed;
7003 	crtc_state->uapi.connectors_changed = from_crtc_state->uapi.connectors_changed;
7004 	crtc_state->uapi.active_changed = from_crtc_state->uapi.active_changed;
7005 	crtc_state->nv12_planes = crtc_state->c8_planes = crtc_state->update_planes = 0;
7006 	crtc_state->bigjoiner_linked_crtc = to_intel_crtc(from_crtc_state->uapi.crtc);
7007 	crtc_state->bigjoiner_slave = true;
7008 	crtc_state->cpu_transcoder = (enum transcoder)crtc->pipe;
7009 	crtc_state->has_audio = false;
7010 
7011 	return 0;
7012 }
7013 
7014 static int
7015 intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
7016 				 struct intel_crtc_state *crtc_state)
7017 {
7018 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7019 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7020 	struct intel_crtc_state *saved_state;
7021 
7022 	saved_state = intel_crtc_state_alloc(crtc);
7023 	if (!saved_state)
7024 		return -ENOMEM;
7025 
7026 	/* free the old crtc_state->hw members */
7027 	intel_crtc_free_hw_state(crtc_state);
7028 
7029 	/* FIXME: before the switch to atomic started, a new pipe_config was
7030 	 * kzalloc'd. Code that depends on any field being zero should be
7031 	 * fixed, so that the crtc_state can be safely duplicated. For now,
7032 	 * only fields that are know to not cause problems are preserved. */
7033 
7034 	saved_state->uapi = crtc_state->uapi;
7035 	saved_state->scaler_state = crtc_state->scaler_state;
7036 	saved_state->shared_dpll = crtc_state->shared_dpll;
7037 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
7038 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
7039 	       sizeof(saved_state->icl_port_dplls));
7040 	saved_state->crc_enabled = crtc_state->crc_enabled;
7041 	if (IS_G4X(dev_priv) ||
7042 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
7043 		saved_state->wm = crtc_state->wm;
7044 
7045 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
7046 	kfree(saved_state);
7047 
7048 	intel_crtc_copy_uapi_to_hw_state(state, crtc_state);
7049 
7050 	return 0;
7051 }
7052 
7053 static int
7054 intel_modeset_pipe_config(struct intel_atomic_state *state,
7055 			  struct intel_crtc_state *pipe_config)
7056 {
7057 	struct drm_crtc *crtc = pipe_config->uapi.crtc;
7058 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7059 	struct drm_connector *connector;
7060 	struct drm_connector_state *connector_state;
7061 	int base_bpp, ret, i;
7062 	bool retry = true;
7063 
7064 	pipe_config->cpu_transcoder =
7065 		(enum transcoder) to_intel_crtc(crtc)->pipe;
7066 
7067 	/*
7068 	 * Sanitize sync polarity flags based on requested ones. If neither
7069 	 * positive or negative polarity is requested, treat this as meaning
7070 	 * negative polarity.
7071 	 */
7072 	if (!(pipe_config->hw.adjusted_mode.flags &
7073 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
7074 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
7075 
7076 	if (!(pipe_config->hw.adjusted_mode.flags &
7077 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
7078 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
7079 
7080 	ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
7081 					pipe_config);
7082 	if (ret)
7083 		return ret;
7084 
7085 	base_bpp = pipe_config->pipe_bpp;
7086 
7087 	/*
7088 	 * Determine the real pipe dimensions. Note that stereo modes can
7089 	 * increase the actual pipe size due to the frame doubling and
7090 	 * insertion of additional space for blanks between the frame. This
7091 	 * is stored in the crtc timings. We use the requested mode to do this
7092 	 * computation to clearly distinguish it from the adjusted mode, which
7093 	 * can be changed by the connectors in the below retry loop.
7094 	 */
7095 	drm_mode_get_hv_timing(&pipe_config->hw.mode,
7096 			       &pipe_config->pipe_src_w,
7097 			       &pipe_config->pipe_src_h);
7098 
7099 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
7100 		struct intel_encoder *encoder =
7101 			to_intel_encoder(connector_state->best_encoder);
7102 
7103 		if (connector_state->crtc != crtc)
7104 			continue;
7105 
7106 		if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
7107 			drm_dbg_kms(&i915->drm,
7108 				    "rejecting invalid cloning configuration\n");
7109 			return -EINVAL;
7110 		}
7111 
7112 		/*
7113 		 * Determine output_types before calling the .compute_config()
7114 		 * hooks so that the hooks can use this information safely.
7115 		 */
7116 		if (encoder->compute_output_type)
7117 			pipe_config->output_types |=
7118 				BIT(encoder->compute_output_type(encoder, pipe_config,
7119 								 connector_state));
7120 		else
7121 			pipe_config->output_types |= BIT(encoder->type);
7122 	}
7123 
7124 encoder_retry:
7125 	/* Ensure the port clock defaults are reset when retrying. */
7126 	pipe_config->port_clock = 0;
7127 	pipe_config->pixel_multiplier = 1;
7128 
7129 	/* Fill in default crtc timings, allow encoders to overwrite them. */
7130 	drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
7131 			      CRTC_STEREO_DOUBLE);
7132 
7133 	/* Pass our mode to the connectors and the CRTC to give them a chance to
7134 	 * adjust it according to limitations or connector properties, and also
7135 	 * a chance to reject the mode entirely.
7136 	 */
7137 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
7138 		struct intel_encoder *encoder =
7139 			to_intel_encoder(connector_state->best_encoder);
7140 
7141 		if (connector_state->crtc != crtc)
7142 			continue;
7143 
7144 		ret = encoder->compute_config(encoder, pipe_config,
7145 					      connector_state);
7146 		if (ret == -EDEADLK)
7147 			return ret;
7148 		if (ret < 0) {
7149 			drm_dbg_kms(&i915->drm, "Encoder config failure: %d\n", ret);
7150 			return ret;
7151 		}
7152 	}
7153 
7154 	/* Set default port clock if not overwritten by the encoder. Needs to be
7155 	 * done afterwards in case the encoder adjusts the mode. */
7156 	if (!pipe_config->port_clock)
7157 		pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
7158 			* pipe_config->pixel_multiplier;
7159 
7160 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
7161 	if (ret == -EDEADLK)
7162 		return ret;
7163 	if (ret == -EAGAIN) {
7164 		if (drm_WARN(&i915->drm, !retry,
7165 			     "loop in pipe configuration computation\n"))
7166 			return -EINVAL;
7167 
7168 		drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
7169 		retry = false;
7170 		goto encoder_retry;
7171 	}
7172 	if (ret < 0) {
7173 		drm_dbg_kms(&i915->drm, "CRTC config failure: %d\n", ret);
7174 		return ret;
7175 	}
7176 
7177 	/* Dithering seems to not pass-through bits correctly when it should, so
7178 	 * only enable it on 6bpc panels and when its not a compliance
7179 	 * test requesting 6bpc video pattern.
7180 	 */
7181 	pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
7182 		!pipe_config->dither_force_disable;
7183 	drm_dbg_kms(&i915->drm,
7184 		    "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
7185 		    base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
7186 
7187 	return 0;
7188 }
7189 
7190 static int
7191 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
7192 {
7193 	struct intel_atomic_state *state =
7194 		to_intel_atomic_state(crtc_state->uapi.state);
7195 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7196 	struct drm_connector_state *conn_state;
7197 	struct drm_connector *connector;
7198 	int i;
7199 
7200 	for_each_new_connector_in_state(&state->base, connector,
7201 					conn_state, i) {
7202 		struct intel_encoder *encoder =
7203 			to_intel_encoder(conn_state->best_encoder);
7204 		int ret;
7205 
7206 		if (conn_state->crtc != &crtc->base ||
7207 		    !encoder->compute_config_late)
7208 			continue;
7209 
7210 		ret = encoder->compute_config_late(encoder, crtc_state,
7211 						   conn_state);
7212 		if (ret)
7213 			return ret;
7214 	}
7215 
7216 	return 0;
7217 }
7218 
7219 bool intel_fuzzy_clock_check(int clock1, int clock2)
7220 {
7221 	int diff;
7222 
7223 	if (clock1 == clock2)
7224 		return true;
7225 
7226 	if (!clock1 || !clock2)
7227 		return false;
7228 
7229 	diff = abs(clock1 - clock2);
7230 
7231 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
7232 		return true;
7233 
7234 	return false;
7235 }
7236 
7237 static bool
7238 intel_compare_m_n(unsigned int m, unsigned int n,
7239 		  unsigned int m2, unsigned int n2,
7240 		  bool exact)
7241 {
7242 	if (m == m2 && n == n2)
7243 		return true;
7244 
7245 	if (exact || !m || !n || !m2 || !n2)
7246 		return false;
7247 
7248 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
7249 
7250 	if (n > n2) {
7251 		while (n > n2) {
7252 			m2 <<= 1;
7253 			n2 <<= 1;
7254 		}
7255 	} else if (n < n2) {
7256 		while (n < n2) {
7257 			m <<= 1;
7258 			n <<= 1;
7259 		}
7260 	}
7261 
7262 	if (n != n2)
7263 		return false;
7264 
7265 	return intel_fuzzy_clock_check(m, m2);
7266 }
7267 
7268 static bool
7269 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
7270 		       const struct intel_link_m_n *m2_n2,
7271 		       bool exact)
7272 {
7273 	return m_n->tu == m2_n2->tu &&
7274 		intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
7275 				  m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
7276 		intel_compare_m_n(m_n->link_m, m_n->link_n,
7277 				  m2_n2->link_m, m2_n2->link_n, exact);
7278 }
7279 
7280 static bool
7281 intel_compare_infoframe(const union hdmi_infoframe *a,
7282 			const union hdmi_infoframe *b)
7283 {
7284 	return memcmp(a, b, sizeof(*a)) == 0;
7285 }
7286 
7287 static bool
7288 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
7289 			 const struct drm_dp_vsc_sdp *b)
7290 {
7291 	return memcmp(a, b, sizeof(*a)) == 0;
7292 }
7293 
7294 static void
7295 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
7296 			       bool fastset, const char *name,
7297 			       const union hdmi_infoframe *a,
7298 			       const union hdmi_infoframe *b)
7299 {
7300 	if (fastset) {
7301 		if (!drm_debug_enabled(DRM_UT_KMS))
7302 			return;
7303 
7304 		drm_dbg_kms(&dev_priv->drm,
7305 			    "fastset mismatch in %s infoframe\n", name);
7306 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
7307 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
7308 		drm_dbg_kms(&dev_priv->drm, "found:\n");
7309 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
7310 	} else {
7311 		drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
7312 		drm_err(&dev_priv->drm, "expected:\n");
7313 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
7314 		drm_err(&dev_priv->drm, "found:\n");
7315 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
7316 	}
7317 }
7318 
7319 static void
7320 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
7321 				bool fastset, const char *name,
7322 				const struct drm_dp_vsc_sdp *a,
7323 				const struct drm_dp_vsc_sdp *b)
7324 {
7325 	if (fastset) {
7326 		if (!drm_debug_enabled(DRM_UT_KMS))
7327 			return;
7328 
7329 		drm_dbg_kms(&dev_priv->drm,
7330 			    "fastset mismatch in %s dp sdp\n", name);
7331 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
7332 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
7333 		drm_dbg_kms(&dev_priv->drm, "found:\n");
7334 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
7335 	} else {
7336 		drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
7337 		drm_err(&dev_priv->drm, "expected:\n");
7338 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
7339 		drm_err(&dev_priv->drm, "found:\n");
7340 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
7341 	}
7342 }
7343 
7344 static void __printf(4, 5)
7345 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
7346 		     const char *name, const char *format, ...)
7347 {
7348 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
7349 	struct va_format vaf;
7350 	va_list args;
7351 
7352 	va_start(args, format);
7353 	vaf.fmt = format;
7354 	vaf.va = &args;
7355 
7356 	if (fastset)
7357 		drm_dbg_kms(&i915->drm,
7358 			    "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
7359 			    crtc->base.base.id, crtc->base.name, name, &vaf);
7360 	else
7361 		drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
7362 			crtc->base.base.id, crtc->base.name, name, &vaf);
7363 
7364 	va_end(args);
7365 }
7366 
7367 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
7368 {
7369 	if (dev_priv->params.fastboot != -1)
7370 		return dev_priv->params.fastboot;
7371 
7372 	/* Enable fastboot by default on Skylake and newer */
7373 	if (DISPLAY_VER(dev_priv) >= 9)
7374 		return true;
7375 
7376 	/* Enable fastboot by default on VLV and CHV */
7377 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
7378 		return true;
7379 
7380 	/* Disabled by default on all others */
7381 	return false;
7382 }
7383 
7384 static bool
7385 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
7386 			  const struct intel_crtc_state *pipe_config,
7387 			  bool fastset)
7388 {
7389 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
7390 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
7391 	bool ret = true;
7392 	u32 bp_gamma = 0;
7393 	bool fixup_inherited = fastset &&
7394 		current_config->inherited && !pipe_config->inherited;
7395 
7396 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
7397 		drm_dbg_kms(&dev_priv->drm,
7398 			    "initial modeset and fastboot not set\n");
7399 		ret = false;
7400 	}
7401 
7402 #define PIPE_CONF_CHECK_X(name) do { \
7403 	if (current_config->name != pipe_config->name) { \
7404 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7405 				     "(expected 0x%08x, found 0x%08x)", \
7406 				     current_config->name, \
7407 				     pipe_config->name); \
7408 		ret = false; \
7409 	} \
7410 } while (0)
7411 
7412 #define PIPE_CONF_CHECK_X_WITH_MASK(name, mask) do { \
7413 	if ((current_config->name & (mask)) != (pipe_config->name & (mask))) { \
7414 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7415 				     "(expected 0x%08x, found 0x%08x)", \
7416 				     current_config->name & (mask), \
7417 				     pipe_config->name & (mask)); \
7418 		ret = false; \
7419 	} \
7420 } while (0)
7421 
7422 #define PIPE_CONF_CHECK_I(name) do { \
7423 	if (current_config->name != pipe_config->name) { \
7424 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7425 				     "(expected %i, found %i)", \
7426 				     current_config->name, \
7427 				     pipe_config->name); \
7428 		ret = false; \
7429 	} \
7430 } while (0)
7431 
7432 #define PIPE_CONF_CHECK_BOOL(name) do { \
7433 	if (current_config->name != pipe_config->name) { \
7434 		pipe_config_mismatch(fastset, crtc,  __stringify(name), \
7435 				     "(expected %s, found %s)", \
7436 				     yesno(current_config->name), \
7437 				     yesno(pipe_config->name)); \
7438 		ret = false; \
7439 	} \
7440 } while (0)
7441 
7442 /*
7443  * Checks state where we only read out the enabling, but not the entire
7444  * state itself (like full infoframes or ELD for audio). These states
7445  * require a full modeset on bootup to fix up.
7446  */
7447 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
7448 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
7449 		PIPE_CONF_CHECK_BOOL(name); \
7450 	} else { \
7451 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7452 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
7453 				     yesno(current_config->name), \
7454 				     yesno(pipe_config->name)); \
7455 		ret = false; \
7456 	} \
7457 } while (0)
7458 
7459 #define PIPE_CONF_CHECK_P(name) do { \
7460 	if (current_config->name != pipe_config->name) { \
7461 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7462 				     "(expected %p, found %p)", \
7463 				     current_config->name, \
7464 				     pipe_config->name); \
7465 		ret = false; \
7466 	} \
7467 } while (0)
7468 
7469 #define PIPE_CONF_CHECK_M_N(name) do { \
7470 	if (!intel_compare_link_m_n(&current_config->name, \
7471 				    &pipe_config->name,\
7472 				    !fastset)) { \
7473 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7474 				     "(expected tu %i gmch %i/%i link %i/%i, " \
7475 				     "found tu %i, gmch %i/%i link %i/%i)", \
7476 				     current_config->name.tu, \
7477 				     current_config->name.gmch_m, \
7478 				     current_config->name.gmch_n, \
7479 				     current_config->name.link_m, \
7480 				     current_config->name.link_n, \
7481 				     pipe_config->name.tu, \
7482 				     pipe_config->name.gmch_m, \
7483 				     pipe_config->name.gmch_n, \
7484 				     pipe_config->name.link_m, \
7485 				     pipe_config->name.link_n); \
7486 		ret = false; \
7487 	} \
7488 } while (0)
7489 
7490 /* This is required for BDW+ where there is only one set of registers for
7491  * switching between high and low RR.
7492  * This macro can be used whenever a comparison has to be made between one
7493  * hw state and multiple sw state variables.
7494  */
7495 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
7496 	if (!intel_compare_link_m_n(&current_config->name, \
7497 				    &pipe_config->name, !fastset) && \
7498 	    !intel_compare_link_m_n(&current_config->alt_name, \
7499 				    &pipe_config->name, !fastset)) { \
7500 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7501 				     "(expected tu %i gmch %i/%i link %i/%i, " \
7502 				     "or tu %i gmch %i/%i link %i/%i, " \
7503 				     "found tu %i, gmch %i/%i link %i/%i)", \
7504 				     current_config->name.tu, \
7505 				     current_config->name.gmch_m, \
7506 				     current_config->name.gmch_n, \
7507 				     current_config->name.link_m, \
7508 				     current_config->name.link_n, \
7509 				     current_config->alt_name.tu, \
7510 				     current_config->alt_name.gmch_m, \
7511 				     current_config->alt_name.gmch_n, \
7512 				     current_config->alt_name.link_m, \
7513 				     current_config->alt_name.link_n, \
7514 				     pipe_config->name.tu, \
7515 				     pipe_config->name.gmch_m, \
7516 				     pipe_config->name.gmch_n, \
7517 				     pipe_config->name.link_m, \
7518 				     pipe_config->name.link_n); \
7519 		ret = false; \
7520 	} \
7521 } while (0)
7522 
7523 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
7524 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
7525 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7526 				     "(%x) (expected %i, found %i)", \
7527 				     (mask), \
7528 				     current_config->name & (mask), \
7529 				     pipe_config->name & (mask)); \
7530 		ret = false; \
7531 	} \
7532 } while (0)
7533 
7534 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
7535 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
7536 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
7537 				     "(expected %i, found %i)", \
7538 				     current_config->name, \
7539 				     pipe_config->name); \
7540 		ret = false; \
7541 	} \
7542 } while (0)
7543 
7544 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
7545 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
7546 				     &pipe_config->infoframes.name)) { \
7547 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
7548 					       &current_config->infoframes.name, \
7549 					       &pipe_config->infoframes.name); \
7550 		ret = false; \
7551 	} \
7552 } while (0)
7553 
7554 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
7555 	if (!current_config->has_psr && !pipe_config->has_psr && \
7556 	    !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
7557 				      &pipe_config->infoframes.name)) { \
7558 		pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
7559 						&current_config->infoframes.name, \
7560 						&pipe_config->infoframes.name); \
7561 		ret = false; \
7562 	} \
7563 } while (0)
7564 
7565 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
7566 	if (current_config->name1 != pipe_config->name1) { \
7567 		pipe_config_mismatch(fastset, crtc, __stringify(name1), \
7568 				"(expected %i, found %i, won't compare lut values)", \
7569 				current_config->name1, \
7570 				pipe_config->name1); \
7571 		ret = false;\
7572 	} else { \
7573 		if (!intel_color_lut_equal(current_config->name2, \
7574 					pipe_config->name2, pipe_config->name1, \
7575 					bit_precision)) { \
7576 			pipe_config_mismatch(fastset, crtc, __stringify(name2), \
7577 					"hw_state doesn't match sw_state"); \
7578 			ret = false; \
7579 		} \
7580 	} \
7581 } while (0)
7582 
7583 #define PIPE_CONF_QUIRK(quirk) \
7584 	((current_config->quirks | pipe_config->quirks) & (quirk))
7585 
7586 	PIPE_CONF_CHECK_I(cpu_transcoder);
7587 
7588 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
7589 	PIPE_CONF_CHECK_I(fdi_lanes);
7590 	PIPE_CONF_CHECK_M_N(fdi_m_n);
7591 
7592 	PIPE_CONF_CHECK_I(lane_count);
7593 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
7594 
7595 	if (DISPLAY_VER(dev_priv) < 8) {
7596 		PIPE_CONF_CHECK_M_N(dp_m_n);
7597 
7598 		if (current_config->has_drrs)
7599 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
7600 	} else
7601 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
7602 
7603 	PIPE_CONF_CHECK_X(output_types);
7604 
7605 	/* FIXME do the readout properly and get rid of this quirk */
7606 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
7607 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hdisplay);
7608 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_htotal);
7609 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_start);
7610 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_end);
7611 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_start);
7612 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_end);
7613 
7614 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vdisplay);
7615 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vtotal);
7616 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_start);
7617 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_end);
7618 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_start);
7619 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_end);
7620 
7621 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
7622 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
7623 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
7624 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
7625 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
7626 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
7627 
7628 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
7629 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
7630 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
7631 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
7632 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
7633 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
7634 
7635 		PIPE_CONF_CHECK_I(pixel_multiplier);
7636 
7637 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
7638 				      DRM_MODE_FLAG_INTERLACE);
7639 
7640 		if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
7641 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
7642 					      DRM_MODE_FLAG_PHSYNC);
7643 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
7644 					      DRM_MODE_FLAG_NHSYNC);
7645 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
7646 					      DRM_MODE_FLAG_PVSYNC);
7647 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
7648 					      DRM_MODE_FLAG_NVSYNC);
7649 		}
7650 	}
7651 
7652 	PIPE_CONF_CHECK_I(output_format);
7653 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
7654 	if ((DISPLAY_VER(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
7655 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
7656 		PIPE_CONF_CHECK_BOOL(limited_color_range);
7657 
7658 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
7659 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
7660 	PIPE_CONF_CHECK_BOOL(has_infoframe);
7661 	/* FIXME do the readout properly and get rid of this quirk */
7662 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
7663 		PIPE_CONF_CHECK_BOOL(fec_enable);
7664 
7665 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
7666 
7667 	PIPE_CONF_CHECK_X(gmch_pfit.control);
7668 	/* pfit ratios are autocomputed by the hw on gen4+ */
7669 	if (DISPLAY_VER(dev_priv) < 4)
7670 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
7671 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
7672 
7673 	/*
7674 	 * Changing the EDP transcoder input mux
7675 	 * (A_ONOFF vs. A_ON) requires a full modeset.
7676 	 */
7677 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
7678 
7679 	if (!fastset) {
7680 		PIPE_CONF_CHECK_I(pipe_src_w);
7681 		PIPE_CONF_CHECK_I(pipe_src_h);
7682 
7683 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
7684 		if (current_config->pch_pfit.enabled) {
7685 			PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
7686 			PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
7687 			PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
7688 			PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
7689 		}
7690 
7691 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
7692 		/* FIXME do the readout properly and get rid of this quirk */
7693 		if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
7694 			PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
7695 
7696 		PIPE_CONF_CHECK_X(gamma_mode);
7697 		if (IS_CHERRYVIEW(dev_priv))
7698 			PIPE_CONF_CHECK_X(cgm_mode);
7699 		else
7700 			PIPE_CONF_CHECK_X(csc_mode);
7701 		PIPE_CONF_CHECK_BOOL(gamma_enable);
7702 		PIPE_CONF_CHECK_BOOL(csc_enable);
7703 
7704 		PIPE_CONF_CHECK_I(linetime);
7705 		PIPE_CONF_CHECK_I(ips_linetime);
7706 
7707 		bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
7708 		if (bp_gamma)
7709 			PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
7710 
7711 		if (current_config->active_planes) {
7712 			PIPE_CONF_CHECK_BOOL(has_psr);
7713 			PIPE_CONF_CHECK_BOOL(has_psr2);
7714 			PIPE_CONF_CHECK_BOOL(enable_psr2_sel_fetch);
7715 			PIPE_CONF_CHECK_I(dc3co_exitline);
7716 		}
7717 	}
7718 
7719 	PIPE_CONF_CHECK_BOOL(double_wide);
7720 
7721 	if (dev_priv->dpll.mgr)
7722 		PIPE_CONF_CHECK_P(shared_dpll);
7723 
7724 	/* FIXME do the readout properly and get rid of this quirk */
7725 	if (dev_priv->dpll.mgr && !PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
7726 		PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
7727 		PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
7728 		PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
7729 		PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
7730 		PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
7731 		PIPE_CONF_CHECK_X(dpll_hw_state.spll);
7732 		PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
7733 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
7734 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
7735 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
7736 		PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
7737 		PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
7738 		PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
7739 		PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
7740 		PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
7741 		PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
7742 		PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
7743 		PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
7744 		PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
7745 		PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
7746 		PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
7747 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
7748 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
7749 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
7750 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
7751 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
7752 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
7753 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
7754 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
7755 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
7756 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
7757 	}
7758 
7759 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
7760 		PIPE_CONF_CHECK_X(dsi_pll.ctrl);
7761 		PIPE_CONF_CHECK_X(dsi_pll.div);
7762 
7763 		if (IS_G4X(dev_priv) || DISPLAY_VER(dev_priv) >= 5)
7764 			PIPE_CONF_CHECK_I(pipe_bpp);
7765 
7766 		PIPE_CONF_CHECK_CLOCK_FUZZY(hw.pipe_mode.crtc_clock);
7767 		PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
7768 		PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
7769 
7770 		PIPE_CONF_CHECK_I(min_voltage_level);
7771 	}
7772 
7773 	if (current_config->has_psr || pipe_config->has_psr)
7774 		PIPE_CONF_CHECK_X_WITH_MASK(infoframes.enable,
7775 					    ~intel_hdmi_infoframe_enable(DP_SDP_VSC));
7776 	else
7777 		PIPE_CONF_CHECK_X(infoframes.enable);
7778 
7779 	PIPE_CONF_CHECK_X(infoframes.gcp);
7780 	PIPE_CONF_CHECK_INFOFRAME(avi);
7781 	PIPE_CONF_CHECK_INFOFRAME(spd);
7782 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
7783 	PIPE_CONF_CHECK_INFOFRAME(drm);
7784 	PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
7785 
7786 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
7787 	PIPE_CONF_CHECK_I(master_transcoder);
7788 	PIPE_CONF_CHECK_BOOL(bigjoiner);
7789 	PIPE_CONF_CHECK_BOOL(bigjoiner_slave);
7790 	PIPE_CONF_CHECK_P(bigjoiner_linked_crtc);
7791 
7792 	PIPE_CONF_CHECK_I(dsc.compression_enable);
7793 	PIPE_CONF_CHECK_I(dsc.dsc_split);
7794 	PIPE_CONF_CHECK_I(dsc.compressed_bpp);
7795 
7796 	PIPE_CONF_CHECK_BOOL(splitter.enable);
7797 	PIPE_CONF_CHECK_I(splitter.link_count);
7798 	PIPE_CONF_CHECK_I(splitter.pixel_overlap);
7799 
7800 	PIPE_CONF_CHECK_I(mst_master_transcoder);
7801 
7802 	PIPE_CONF_CHECK_BOOL(vrr.enable);
7803 	PIPE_CONF_CHECK_I(vrr.vmin);
7804 	PIPE_CONF_CHECK_I(vrr.vmax);
7805 	PIPE_CONF_CHECK_I(vrr.flipline);
7806 	PIPE_CONF_CHECK_I(vrr.pipeline_full);
7807 	PIPE_CONF_CHECK_I(vrr.guardband);
7808 
7809 #undef PIPE_CONF_CHECK_X
7810 #undef PIPE_CONF_CHECK_I
7811 #undef PIPE_CONF_CHECK_BOOL
7812 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
7813 #undef PIPE_CONF_CHECK_P
7814 #undef PIPE_CONF_CHECK_FLAGS
7815 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
7816 #undef PIPE_CONF_CHECK_COLOR_LUT
7817 #undef PIPE_CONF_QUIRK
7818 
7819 	return ret;
7820 }
7821 
7822 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
7823 					   const struct intel_crtc_state *pipe_config)
7824 {
7825 	if (pipe_config->has_pch_encoder) {
7826 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
7827 							    &pipe_config->fdi_m_n);
7828 		int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
7829 
7830 		/*
7831 		 * FDI already provided one idea for the dotclock.
7832 		 * Yell if the encoder disagrees.
7833 		 */
7834 		drm_WARN(&dev_priv->drm,
7835 			 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
7836 			 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
7837 			 fdi_dotclock, dotclock);
7838 	}
7839 }
7840 
7841 static void verify_wm_state(struct intel_crtc *crtc,
7842 			    struct intel_crtc_state *new_crtc_state)
7843 {
7844 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7845 	struct skl_hw_state {
7846 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
7847 		struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
7848 		struct skl_pipe_wm wm;
7849 	} *hw;
7850 	const struct skl_pipe_wm *sw_wm = &new_crtc_state->wm.skl.optimal;
7851 	int level, max_level = ilk_wm_max_level(dev_priv);
7852 	struct intel_plane *plane;
7853 	u8 hw_enabled_slices;
7854 
7855 	if (DISPLAY_VER(dev_priv) < 9 || !new_crtc_state->hw.active)
7856 		return;
7857 
7858 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
7859 	if (!hw)
7860 		return;
7861 
7862 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
7863 
7864 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
7865 
7866 	hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
7867 
7868 	if (DISPLAY_VER(dev_priv) >= 11 &&
7869 	    hw_enabled_slices != dev_priv->dbuf.enabled_slices)
7870 		drm_err(&dev_priv->drm,
7871 			"mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
7872 			dev_priv->dbuf.enabled_slices,
7873 			hw_enabled_slices);
7874 
7875 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
7876 		const struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
7877 		const struct skl_wm_level *hw_wm_level, *sw_wm_level;
7878 
7879 		/* Watermarks */
7880 		for (level = 0; level <= max_level; level++) {
7881 			hw_wm_level = &hw->wm.planes[plane->id].wm[level];
7882 			sw_wm_level = skl_plane_wm_level(sw_wm, plane->id, level);
7883 
7884 			if (skl_wm_level_equals(hw_wm_level, sw_wm_level))
7885 				continue;
7886 
7887 			drm_err(&dev_priv->drm,
7888 				"[PLANE:%d:%s] mismatch in WM%d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7889 				plane->base.base.id, plane->base.name, level,
7890 				sw_wm_level->enable,
7891 				sw_wm_level->blocks,
7892 				sw_wm_level->lines,
7893 				hw_wm_level->enable,
7894 				hw_wm_level->blocks,
7895 				hw_wm_level->lines);
7896 		}
7897 
7898 		hw_wm_level = &hw->wm.planes[plane->id].trans_wm;
7899 		sw_wm_level = skl_plane_trans_wm(sw_wm, plane->id);
7900 
7901 		if (!skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
7902 			drm_err(&dev_priv->drm,
7903 				"[PLANE:%d:%s] mismatch in trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7904 				plane->base.base.id, plane->base.name,
7905 				sw_wm_level->enable,
7906 				sw_wm_level->blocks,
7907 				sw_wm_level->lines,
7908 				hw_wm_level->enable,
7909 				hw_wm_level->blocks,
7910 				hw_wm_level->lines);
7911 		}
7912 
7913 		hw_wm_level = &hw->wm.planes[plane->id].sagv.wm0;
7914 		sw_wm_level = &sw_wm->planes[plane->id].sagv.wm0;
7915 
7916 		if (HAS_HW_SAGV_WM(dev_priv) &&
7917 		    !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
7918 			drm_err(&dev_priv->drm,
7919 				"[PLANE:%d:%s] mismatch in SAGV WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7920 				plane->base.base.id, plane->base.name,
7921 				sw_wm_level->enable,
7922 				sw_wm_level->blocks,
7923 				sw_wm_level->lines,
7924 				hw_wm_level->enable,
7925 				hw_wm_level->blocks,
7926 				hw_wm_level->lines);
7927 		}
7928 
7929 		hw_wm_level = &hw->wm.planes[plane->id].sagv.trans_wm;
7930 		sw_wm_level = &sw_wm->planes[plane->id].sagv.trans_wm;
7931 
7932 		if (HAS_HW_SAGV_WM(dev_priv) &&
7933 		    !skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
7934 			drm_err(&dev_priv->drm,
7935 				"[PLANE:%d:%s] mismatch in SAGV trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
7936 				plane->base.base.id, plane->base.name,
7937 				sw_wm_level->enable,
7938 				sw_wm_level->blocks,
7939 				sw_wm_level->lines,
7940 				hw_wm_level->enable,
7941 				hw_wm_level->blocks,
7942 				hw_wm_level->lines);
7943 		}
7944 
7945 		/* DDB */
7946 		hw_ddb_entry = &hw->ddb_y[plane->id];
7947 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane->id];
7948 
7949 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
7950 			drm_err(&dev_priv->drm,
7951 				"[PLANE:%d:%s] mismatch in DDB (expected (%u,%u), found (%u,%u))\n",
7952 				plane->base.base.id, plane->base.name,
7953 				sw_ddb_entry->start, sw_ddb_entry->end,
7954 				hw_ddb_entry->start, hw_ddb_entry->end);
7955 		}
7956 	}
7957 
7958 	kfree(hw);
7959 }
7960 
7961 static void
7962 verify_connector_state(struct intel_atomic_state *state,
7963 		       struct intel_crtc *crtc)
7964 {
7965 	struct drm_connector *connector;
7966 	struct drm_connector_state *new_conn_state;
7967 	int i;
7968 
7969 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
7970 		struct drm_encoder *encoder = connector->encoder;
7971 		struct intel_crtc_state *crtc_state = NULL;
7972 
7973 		if (new_conn_state->crtc != &crtc->base)
7974 			continue;
7975 
7976 		if (crtc)
7977 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
7978 
7979 		intel_connector_verify_state(crtc_state, new_conn_state);
7980 
7981 		I915_STATE_WARN(new_conn_state->best_encoder != encoder,
7982 		     "connector's atomic encoder doesn't match legacy encoder\n");
7983 	}
7984 }
7985 
7986 static void
7987 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
7988 {
7989 	struct intel_encoder *encoder;
7990 	struct drm_connector *connector;
7991 	struct drm_connector_state *old_conn_state, *new_conn_state;
7992 	int i;
7993 
7994 	for_each_intel_encoder(&dev_priv->drm, encoder) {
7995 		bool enabled = false, found = false;
7996 		enum pipe pipe;
7997 
7998 		drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
7999 			    encoder->base.base.id,
8000 			    encoder->base.name);
8001 
8002 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
8003 						   new_conn_state, i) {
8004 			if (old_conn_state->best_encoder == &encoder->base)
8005 				found = true;
8006 
8007 			if (new_conn_state->best_encoder != &encoder->base)
8008 				continue;
8009 			found = enabled = true;
8010 
8011 			I915_STATE_WARN(new_conn_state->crtc !=
8012 					encoder->base.crtc,
8013 			     "connector's crtc doesn't match encoder crtc\n");
8014 		}
8015 
8016 		if (!found)
8017 			continue;
8018 
8019 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
8020 		     "encoder's enabled state mismatch "
8021 		     "(expected %i, found %i)\n",
8022 		     !!encoder->base.crtc, enabled);
8023 
8024 		if (!encoder->base.crtc) {
8025 			bool active;
8026 
8027 			active = encoder->get_hw_state(encoder, &pipe);
8028 			I915_STATE_WARN(active,
8029 			     "encoder detached but still enabled on pipe %c.\n",
8030 			     pipe_name(pipe));
8031 		}
8032 	}
8033 }
8034 
8035 static void
8036 verify_crtc_state(struct intel_crtc *crtc,
8037 		  struct intel_crtc_state *old_crtc_state,
8038 		  struct intel_crtc_state *new_crtc_state)
8039 {
8040 	struct drm_device *dev = crtc->base.dev;
8041 	struct drm_i915_private *dev_priv = to_i915(dev);
8042 	struct intel_encoder *encoder;
8043 	struct intel_crtc_state *pipe_config = old_crtc_state;
8044 	struct drm_atomic_state *state = old_crtc_state->uapi.state;
8045 	struct intel_crtc *master = crtc;
8046 
8047 	__drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
8048 	intel_crtc_free_hw_state(old_crtc_state);
8049 	intel_crtc_state_reset(old_crtc_state, crtc);
8050 	old_crtc_state->uapi.state = state;
8051 
8052 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
8053 		    crtc->base.name);
8054 
8055 	pipe_config->hw.enable = new_crtc_state->hw.enable;
8056 
8057 	intel_crtc_get_pipe_config(pipe_config);
8058 
8059 	/* we keep both pipes enabled on 830 */
8060 	if (IS_I830(dev_priv) && pipe_config->hw.active)
8061 		pipe_config->hw.active = new_crtc_state->hw.active;
8062 
8063 	I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
8064 			"crtc active state doesn't match with hw state "
8065 			"(expected %i, found %i)\n",
8066 			new_crtc_state->hw.active, pipe_config->hw.active);
8067 
8068 	I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
8069 			"transitional active state does not match atomic hw state "
8070 			"(expected %i, found %i)\n",
8071 			new_crtc_state->hw.active, crtc->active);
8072 
8073 	if (new_crtc_state->bigjoiner_slave)
8074 		master = new_crtc_state->bigjoiner_linked_crtc;
8075 
8076 	for_each_encoder_on_crtc(dev, &master->base, encoder) {
8077 		enum pipe pipe;
8078 		bool active;
8079 
8080 		active = encoder->get_hw_state(encoder, &pipe);
8081 		I915_STATE_WARN(active != new_crtc_state->hw.active,
8082 				"[ENCODER:%i] active %i with crtc active %i\n",
8083 				encoder->base.base.id, active,
8084 				new_crtc_state->hw.active);
8085 
8086 		I915_STATE_WARN(active && master->pipe != pipe,
8087 				"Encoder connected to wrong pipe %c\n",
8088 				pipe_name(pipe));
8089 
8090 		if (active)
8091 			intel_encoder_get_config(encoder, pipe_config);
8092 	}
8093 
8094 	if (!new_crtc_state->hw.active)
8095 		return;
8096 
8097 	if (new_crtc_state->bigjoiner_slave)
8098 		/* No PLLs set for slave */
8099 		pipe_config->shared_dpll = NULL;
8100 
8101 	intel_pipe_config_sanity_check(dev_priv, pipe_config);
8102 
8103 	if (!intel_pipe_config_compare(new_crtc_state,
8104 				       pipe_config, false)) {
8105 		I915_STATE_WARN(1, "pipe state doesn't match!\n");
8106 		intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
8107 		intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
8108 	}
8109 }
8110 
8111 static void
8112 intel_verify_planes(struct intel_atomic_state *state)
8113 {
8114 	struct intel_plane *plane;
8115 	const struct intel_plane_state *plane_state;
8116 	int i;
8117 
8118 	for_each_new_intel_plane_in_state(state, plane,
8119 					  plane_state, i)
8120 		assert_plane(plane, plane_state->planar_slave ||
8121 			     plane_state->uapi.visible);
8122 }
8123 
8124 static void
8125 verify_single_dpll_state(struct drm_i915_private *dev_priv,
8126 			 struct intel_shared_dpll *pll,
8127 			 struct intel_crtc *crtc,
8128 			 struct intel_crtc_state *new_crtc_state)
8129 {
8130 	struct intel_dpll_hw_state dpll_hw_state;
8131 	u8 pipe_mask;
8132 	bool active;
8133 
8134 	memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
8135 
8136 	drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
8137 
8138 	active = intel_dpll_get_hw_state(dev_priv, pll, &dpll_hw_state);
8139 
8140 	if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
8141 		I915_STATE_WARN(!pll->on && pll->active_mask,
8142 		     "pll in active use but not on in sw tracking\n");
8143 		I915_STATE_WARN(pll->on && !pll->active_mask,
8144 		     "pll is on but not used by any active pipe\n");
8145 		I915_STATE_WARN(pll->on != active,
8146 		     "pll on state mismatch (expected %i, found %i)\n",
8147 		     pll->on, active);
8148 	}
8149 
8150 	if (!crtc) {
8151 		I915_STATE_WARN(pll->active_mask & ~pll->state.pipe_mask,
8152 				"more active pll users than references: 0x%x vs 0x%x\n",
8153 				pll->active_mask, pll->state.pipe_mask);
8154 
8155 		return;
8156 	}
8157 
8158 	pipe_mask = BIT(crtc->pipe);
8159 
8160 	if (new_crtc_state->hw.active)
8161 		I915_STATE_WARN(!(pll->active_mask & pipe_mask),
8162 				"pll active mismatch (expected pipe %c in active mask 0x%x)\n",
8163 				pipe_name(crtc->pipe), pll->active_mask);
8164 	else
8165 		I915_STATE_WARN(pll->active_mask & pipe_mask,
8166 				"pll active mismatch (didn't expect pipe %c in active mask 0x%x)\n",
8167 				pipe_name(crtc->pipe), pll->active_mask);
8168 
8169 	I915_STATE_WARN(!(pll->state.pipe_mask & pipe_mask),
8170 			"pll enabled crtcs mismatch (expected 0x%x in 0x%x)\n",
8171 			pipe_mask, pll->state.pipe_mask);
8172 
8173 	I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
8174 					  &dpll_hw_state,
8175 					  sizeof(dpll_hw_state)),
8176 			"pll hw state mismatch\n");
8177 }
8178 
8179 static void
8180 verify_shared_dpll_state(struct intel_crtc *crtc,
8181 			 struct intel_crtc_state *old_crtc_state,
8182 			 struct intel_crtc_state *new_crtc_state)
8183 {
8184 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8185 
8186 	if (new_crtc_state->shared_dpll)
8187 		verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
8188 
8189 	if (old_crtc_state->shared_dpll &&
8190 	    old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
8191 		u8 pipe_mask = BIT(crtc->pipe);
8192 		struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
8193 
8194 		I915_STATE_WARN(pll->active_mask & pipe_mask,
8195 				"pll active mismatch (didn't expect pipe %c in active mask (0x%x))\n",
8196 				pipe_name(crtc->pipe), pll->active_mask);
8197 		I915_STATE_WARN(pll->state.pipe_mask & pipe_mask,
8198 				"pll enabled crtcs mismatch (found %x in enabled mask (0x%x))\n",
8199 				pipe_name(crtc->pipe), pll->state.pipe_mask);
8200 	}
8201 }
8202 
8203 static void
8204 verify_mpllb_state(struct intel_atomic_state *state,
8205 		   struct intel_crtc_state *new_crtc_state)
8206 {
8207 	struct drm_i915_private *i915 = to_i915(state->base.dev);
8208 	struct intel_mpllb_state mpllb_hw_state = { 0 };
8209 	struct intel_mpllb_state *mpllb_sw_state = &new_crtc_state->mpllb_state;
8210 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
8211 	struct intel_encoder *encoder;
8212 
8213 	if (!IS_DG2(i915))
8214 		return;
8215 
8216 	if (!new_crtc_state->hw.active)
8217 		return;
8218 
8219 	if (new_crtc_state->bigjoiner_slave)
8220 		return;
8221 
8222 	encoder = intel_get_crtc_new_encoder(state, new_crtc_state);
8223 	intel_mpllb_readout_hw_state(encoder, &mpllb_hw_state);
8224 
8225 #define MPLLB_CHECK(name) do { \
8226 	if (mpllb_sw_state->name != mpllb_hw_state.name) { \
8227 		pipe_config_mismatch(false, crtc, "MPLLB:" __stringify(name), \
8228 				     "(expected 0x%08x, found 0x%08x)", \
8229 				     mpllb_sw_state->name, \
8230 				     mpllb_hw_state.name); \
8231 	} \
8232 } while (0)
8233 
8234 	MPLLB_CHECK(mpllb_cp);
8235 	MPLLB_CHECK(mpllb_div);
8236 	MPLLB_CHECK(mpllb_div2);
8237 	MPLLB_CHECK(mpllb_fracn1);
8238 	MPLLB_CHECK(mpllb_fracn2);
8239 	MPLLB_CHECK(mpllb_sscen);
8240 	MPLLB_CHECK(mpllb_sscstep);
8241 
8242 	/*
8243 	 * ref_control is handled by the hardware/firemware and never
8244 	 * programmed by the software, but the proper values are supplied
8245 	 * in the bspec for verification purposes.
8246 	 */
8247 	MPLLB_CHECK(ref_control);
8248 
8249 #undef MPLLB_CHECK
8250 }
8251 
8252 static void
8253 intel_modeset_verify_crtc(struct intel_crtc *crtc,
8254 			  struct intel_atomic_state *state,
8255 			  struct intel_crtc_state *old_crtc_state,
8256 			  struct intel_crtc_state *new_crtc_state)
8257 {
8258 	if (!intel_crtc_needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
8259 		return;
8260 
8261 	verify_wm_state(crtc, new_crtc_state);
8262 	verify_connector_state(state, crtc);
8263 	verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
8264 	verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
8265 	verify_mpllb_state(state, new_crtc_state);
8266 }
8267 
8268 static void
8269 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
8270 {
8271 	int i;
8272 
8273 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
8274 		verify_single_dpll_state(dev_priv,
8275 					 &dev_priv->dpll.shared_dplls[i],
8276 					 NULL, NULL);
8277 }
8278 
8279 static void
8280 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
8281 			      struct intel_atomic_state *state)
8282 {
8283 	verify_encoder_state(dev_priv, state);
8284 	verify_connector_state(state, NULL);
8285 	verify_disabled_dpll_state(dev_priv);
8286 }
8287 
8288 int intel_modeset_all_pipes(struct intel_atomic_state *state)
8289 {
8290 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8291 	struct intel_crtc *crtc;
8292 
8293 	/*
8294 	 * Add all pipes to the state, and force
8295 	 * a modeset on all the active ones.
8296 	 */
8297 	for_each_intel_crtc(&dev_priv->drm, crtc) {
8298 		struct intel_crtc_state *crtc_state;
8299 		int ret;
8300 
8301 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
8302 		if (IS_ERR(crtc_state))
8303 			return PTR_ERR(crtc_state);
8304 
8305 		if (!crtc_state->hw.active ||
8306 		    drm_atomic_crtc_needs_modeset(&crtc_state->uapi))
8307 			continue;
8308 
8309 		crtc_state->uapi.mode_changed = true;
8310 
8311 		ret = drm_atomic_add_affected_connectors(&state->base,
8312 							 &crtc->base);
8313 		if (ret)
8314 			return ret;
8315 
8316 		ret = intel_atomic_add_affected_planes(state, crtc);
8317 		if (ret)
8318 			return ret;
8319 
8320 		crtc_state->update_planes |= crtc_state->active_planes;
8321 	}
8322 
8323 	return 0;
8324 }
8325 
8326 static void
8327 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
8328 {
8329 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8330 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8331 	struct drm_display_mode adjusted_mode =
8332 		crtc_state->hw.adjusted_mode;
8333 
8334 	if (crtc_state->vrr.enable) {
8335 		adjusted_mode.crtc_vtotal = crtc_state->vrr.vmax;
8336 		adjusted_mode.crtc_vblank_end = crtc_state->vrr.vmax;
8337 		adjusted_mode.crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
8338 		crtc->vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
8339 	}
8340 
8341 	drm_calc_timestamping_constants(&crtc->base, &adjusted_mode);
8342 
8343 	crtc->mode_flags = crtc_state->mode_flags;
8344 
8345 	/*
8346 	 * The scanline counter increments at the leading edge of hsync.
8347 	 *
8348 	 * On most platforms it starts counting from vtotal-1 on the
8349 	 * first active line. That means the scanline counter value is
8350 	 * always one less than what we would expect. Ie. just after
8351 	 * start of vblank, which also occurs at start of hsync (on the
8352 	 * last active line), the scanline counter will read vblank_start-1.
8353 	 *
8354 	 * On gen2 the scanline counter starts counting from 1 instead
8355 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
8356 	 * to keep the value positive), instead of adding one.
8357 	 *
8358 	 * On HSW+ the behaviour of the scanline counter depends on the output
8359 	 * type. For DP ports it behaves like most other platforms, but on HDMI
8360 	 * there's an extra 1 line difference. So we need to add two instead of
8361 	 * one to the value.
8362 	 *
8363 	 * On VLV/CHV DSI the scanline counter would appear to increment
8364 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
8365 	 * that means we can't tell whether we're in vblank or not while
8366 	 * we're on that particular line. We must still set scanline_offset
8367 	 * to 1 so that the vblank timestamps come out correct when we query
8368 	 * the scanline counter from within the vblank interrupt handler.
8369 	 * However if queried just before the start of vblank we'll get an
8370 	 * answer that's slightly in the future.
8371 	 */
8372 	if (DISPLAY_VER(dev_priv) == 2) {
8373 		int vtotal;
8374 
8375 		vtotal = adjusted_mode.crtc_vtotal;
8376 		if (adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
8377 			vtotal /= 2;
8378 
8379 		crtc->scanline_offset = vtotal - 1;
8380 	} else if (HAS_DDI(dev_priv) &&
8381 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
8382 		crtc->scanline_offset = 2;
8383 	} else {
8384 		crtc->scanline_offset = 1;
8385 	}
8386 }
8387 
8388 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
8389 {
8390 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8391 	struct intel_crtc_state *new_crtc_state;
8392 	struct intel_crtc *crtc;
8393 	int i;
8394 
8395 	if (!dev_priv->dpll_funcs)
8396 		return;
8397 
8398 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
8399 		if (!intel_crtc_needs_modeset(new_crtc_state))
8400 			continue;
8401 
8402 		intel_release_shared_dplls(state, crtc);
8403 	}
8404 }
8405 
8406 /*
8407  * This implements the workaround described in the "notes" section of the mode
8408  * set sequence documentation. When going from no pipes or single pipe to
8409  * multiple pipes, and planes are enabled after the pipe, we need to wait at
8410  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
8411  */
8412 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
8413 {
8414 	struct intel_crtc_state *crtc_state;
8415 	struct intel_crtc *crtc;
8416 	struct intel_crtc_state *first_crtc_state = NULL;
8417 	struct intel_crtc_state *other_crtc_state = NULL;
8418 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
8419 	int i;
8420 
8421 	/* look at all crtc's that are going to be enabled in during modeset */
8422 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
8423 		if (!crtc_state->hw.active ||
8424 		    !intel_crtc_needs_modeset(crtc_state))
8425 			continue;
8426 
8427 		if (first_crtc_state) {
8428 			other_crtc_state = crtc_state;
8429 			break;
8430 		} else {
8431 			first_crtc_state = crtc_state;
8432 			first_pipe = crtc->pipe;
8433 		}
8434 	}
8435 
8436 	/* No workaround needed? */
8437 	if (!first_crtc_state)
8438 		return 0;
8439 
8440 	/* w/a possibly needed, check how many crtc's are already enabled. */
8441 	for_each_intel_crtc(state->base.dev, crtc) {
8442 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
8443 		if (IS_ERR(crtc_state))
8444 			return PTR_ERR(crtc_state);
8445 
8446 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
8447 
8448 		if (!crtc_state->hw.active ||
8449 		    intel_crtc_needs_modeset(crtc_state))
8450 			continue;
8451 
8452 		/* 2 or more enabled crtcs means no need for w/a */
8453 		if (enabled_pipe != INVALID_PIPE)
8454 			return 0;
8455 
8456 		enabled_pipe = crtc->pipe;
8457 	}
8458 
8459 	if (enabled_pipe != INVALID_PIPE)
8460 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
8461 	else if (other_crtc_state)
8462 		other_crtc_state->hsw_workaround_pipe = first_pipe;
8463 
8464 	return 0;
8465 }
8466 
8467 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
8468 			   u8 active_pipes)
8469 {
8470 	const struct intel_crtc_state *crtc_state;
8471 	struct intel_crtc *crtc;
8472 	int i;
8473 
8474 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
8475 		if (crtc_state->hw.active)
8476 			active_pipes |= BIT(crtc->pipe);
8477 		else
8478 			active_pipes &= ~BIT(crtc->pipe);
8479 	}
8480 
8481 	return active_pipes;
8482 }
8483 
8484 static int intel_modeset_checks(struct intel_atomic_state *state)
8485 {
8486 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8487 
8488 	state->modeset = true;
8489 
8490 	if (IS_HASWELL(dev_priv))
8491 		return hsw_mode_set_planes_workaround(state);
8492 
8493 	return 0;
8494 }
8495 
8496 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
8497 				     struct intel_crtc_state *new_crtc_state)
8498 {
8499 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
8500 		return;
8501 
8502 	new_crtc_state->uapi.mode_changed = false;
8503 	new_crtc_state->update_pipe = true;
8504 }
8505 
8506 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
8507 				    struct intel_crtc_state *new_crtc_state)
8508 {
8509 	/*
8510 	 * If we're not doing the full modeset we want to
8511 	 * keep the current M/N values as they may be
8512 	 * sufficiently different to the computed values
8513 	 * to cause problems.
8514 	 *
8515 	 * FIXME: should really copy more fuzzy state here
8516 	 */
8517 	new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
8518 	new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
8519 	new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
8520 	new_crtc_state->has_drrs = old_crtc_state->has_drrs;
8521 }
8522 
8523 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
8524 					  struct intel_crtc *crtc,
8525 					  u8 plane_ids_mask)
8526 {
8527 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8528 	struct intel_plane *plane;
8529 
8530 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
8531 		struct intel_plane_state *plane_state;
8532 
8533 		if ((plane_ids_mask & BIT(plane->id)) == 0)
8534 			continue;
8535 
8536 		plane_state = intel_atomic_get_plane_state(state, plane);
8537 		if (IS_ERR(plane_state))
8538 			return PTR_ERR(plane_state);
8539 	}
8540 
8541 	return 0;
8542 }
8543 
8544 int intel_atomic_add_affected_planes(struct intel_atomic_state *state,
8545 				     struct intel_crtc *crtc)
8546 {
8547 	const struct intel_crtc_state *old_crtc_state =
8548 		intel_atomic_get_old_crtc_state(state, crtc);
8549 	const struct intel_crtc_state *new_crtc_state =
8550 		intel_atomic_get_new_crtc_state(state, crtc);
8551 
8552 	return intel_crtc_add_planes_to_state(state, crtc,
8553 					      old_crtc_state->enabled_planes |
8554 					      new_crtc_state->enabled_planes);
8555 }
8556 
8557 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
8558 {
8559 	/* See {hsw,vlv,ivb}_plane_ratio() */
8560 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
8561 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
8562 		IS_IVYBRIDGE(dev_priv);
8563 }
8564 
8565 static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
8566 					   struct intel_crtc *crtc,
8567 					   struct intel_crtc *other)
8568 {
8569 	const struct intel_plane_state *plane_state;
8570 	struct intel_plane *plane;
8571 	u8 plane_ids = 0;
8572 	int i;
8573 
8574 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
8575 		if (plane->pipe == crtc->pipe)
8576 			plane_ids |= BIT(plane->id);
8577 	}
8578 
8579 	return intel_crtc_add_planes_to_state(state, other, plane_ids);
8580 }
8581 
8582 static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
8583 {
8584 	const struct intel_crtc_state *crtc_state;
8585 	struct intel_crtc *crtc;
8586 	int i;
8587 
8588 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
8589 		int ret;
8590 
8591 		if (!crtc_state->bigjoiner)
8592 			continue;
8593 
8594 		ret = intel_crtc_add_bigjoiner_planes(state, crtc,
8595 						      crtc_state->bigjoiner_linked_crtc);
8596 		if (ret)
8597 			return ret;
8598 	}
8599 
8600 	return 0;
8601 }
8602 
8603 static bool bo_has_valid_encryption(struct drm_i915_gem_object *obj)
8604 {
8605 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
8606 
8607 	return intel_pxp_key_check(&i915->gt.pxp, obj, false) == 0;
8608 }
8609 
8610 static bool pxp_is_borked(struct drm_i915_gem_object *obj)
8611 {
8612 	return i915_gem_object_is_protected(obj) && !bo_has_valid_encryption(obj);
8613 }
8614 
8615 static int intel_atomic_check_planes(struct intel_atomic_state *state)
8616 {
8617 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8618 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
8619 	struct intel_plane_state *plane_state;
8620 	struct intel_plane *plane;
8621 	struct intel_plane_state *new_plane_state;
8622 	struct intel_plane_state *old_plane_state;
8623 	struct intel_crtc *crtc;
8624 	const struct drm_framebuffer *fb;
8625 	int i, ret;
8626 
8627 	ret = icl_add_linked_planes(state);
8628 	if (ret)
8629 		return ret;
8630 
8631 	ret = intel_bigjoiner_add_affected_planes(state);
8632 	if (ret)
8633 		return ret;
8634 
8635 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
8636 		ret = intel_plane_atomic_check(state, plane);
8637 		if (ret) {
8638 			drm_dbg_atomic(&dev_priv->drm,
8639 				       "[PLANE:%d:%s] atomic driver check failed\n",
8640 				       plane->base.base.id, plane->base.name);
8641 			return ret;
8642 		}
8643 	}
8644 
8645 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
8646 					    new_crtc_state, i) {
8647 		u8 old_active_planes, new_active_planes;
8648 
8649 		ret = icl_check_nv12_planes(new_crtc_state);
8650 		if (ret)
8651 			return ret;
8652 
8653 		/*
8654 		 * On some platforms the number of active planes affects
8655 		 * the planes' minimum cdclk calculation. Add such planes
8656 		 * to the state before we compute the minimum cdclk.
8657 		 */
8658 		if (!active_planes_affects_min_cdclk(dev_priv))
8659 			continue;
8660 
8661 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
8662 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
8663 
8664 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
8665 			continue;
8666 
8667 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
8668 		if (ret)
8669 			return ret;
8670 	}
8671 
8672 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
8673 		new_plane_state = intel_atomic_get_new_plane_state(state, plane);
8674 		old_plane_state = intel_atomic_get_old_plane_state(state, plane);
8675 		fb = new_plane_state->hw.fb;
8676 		if (fb) {
8677 			new_plane_state->decrypt = bo_has_valid_encryption(intel_fb_obj(fb));
8678 			new_plane_state->force_black = pxp_is_borked(intel_fb_obj(fb));
8679 		} else {
8680 			new_plane_state->decrypt = old_plane_state->decrypt;
8681 			new_plane_state->force_black = old_plane_state->force_black;
8682 		}
8683 	}
8684 
8685 	return 0;
8686 }
8687 
8688 static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
8689 				    bool *need_cdclk_calc)
8690 {
8691 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
8692 	const struct intel_cdclk_state *old_cdclk_state;
8693 	const struct intel_cdclk_state *new_cdclk_state;
8694 	struct intel_plane_state *plane_state;
8695 	struct intel_bw_state *new_bw_state;
8696 	struct intel_plane *plane;
8697 	int min_cdclk = 0;
8698 	enum pipe pipe;
8699 	int ret;
8700 	int i;
8701 	/*
8702 	 * active_planes bitmask has been updated, and potentially
8703 	 * affected planes are part of the state. We can now
8704 	 * compute the minimum cdclk for each plane.
8705 	 */
8706 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
8707 		ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
8708 		if (ret)
8709 			return ret;
8710 	}
8711 
8712 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
8713 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
8714 
8715 	if (new_cdclk_state &&
8716 	    old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
8717 		*need_cdclk_calc = true;
8718 
8719 	ret = intel_cdclk_bw_calc_min_cdclk(state);
8720 	if (ret)
8721 		return ret;
8722 
8723 	new_bw_state = intel_atomic_get_new_bw_state(state);
8724 
8725 	if (!new_cdclk_state || !new_bw_state)
8726 		return 0;
8727 
8728 	for_each_pipe(dev_priv, pipe) {
8729 		min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
8730 
8731 		/*
8732 		 * Currently do this change only if we need to increase
8733 		 */
8734 		if (new_bw_state->min_cdclk > min_cdclk)
8735 			*need_cdclk_calc = true;
8736 	}
8737 
8738 	return 0;
8739 }
8740 
8741 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
8742 {
8743 	struct intel_crtc_state *crtc_state;
8744 	struct intel_crtc *crtc;
8745 	int i;
8746 
8747 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
8748 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
8749 		int ret;
8750 
8751 		ret = intel_crtc_atomic_check(state, crtc);
8752 		if (ret) {
8753 			drm_dbg_atomic(&i915->drm,
8754 				       "[CRTC:%d:%s] atomic driver check failed\n",
8755 				       crtc->base.base.id, crtc->base.name);
8756 			return ret;
8757 		}
8758 	}
8759 
8760 	return 0;
8761 }
8762 
8763 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
8764 					       u8 transcoders)
8765 {
8766 	const struct intel_crtc_state *new_crtc_state;
8767 	struct intel_crtc *crtc;
8768 	int i;
8769 
8770 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
8771 		if (new_crtc_state->hw.enable &&
8772 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
8773 		    intel_crtc_needs_modeset(new_crtc_state))
8774 			return true;
8775 	}
8776 
8777 	return false;
8778 }
8779 
8780 static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
8781 					struct intel_crtc *crtc,
8782 					struct intel_crtc_state *old_crtc_state,
8783 					struct intel_crtc_state *new_crtc_state)
8784 {
8785 	struct intel_crtc_state *slave_crtc_state, *master_crtc_state;
8786 	struct intel_crtc *slave, *master;
8787 
8788 	/* slave being enabled, is master is still claiming this crtc? */
8789 	if (old_crtc_state->bigjoiner_slave) {
8790 		slave = crtc;
8791 		master = old_crtc_state->bigjoiner_linked_crtc;
8792 		master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
8793 		if (!master_crtc_state || !intel_crtc_needs_modeset(master_crtc_state))
8794 			goto claimed;
8795 	}
8796 
8797 	if (!new_crtc_state->bigjoiner)
8798 		return 0;
8799 
8800 	slave = intel_dsc_get_bigjoiner_secondary(crtc);
8801 	if (!slave) {
8802 		DRM_DEBUG_KMS("[CRTC:%d:%s] Big joiner configuration requires "
8803 			      "CRTC + 1 to be used, doesn't exist\n",
8804 			      crtc->base.base.id, crtc->base.name);
8805 		return -EINVAL;
8806 	}
8807 
8808 	new_crtc_state->bigjoiner_linked_crtc = slave;
8809 	slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave);
8810 	master = crtc;
8811 	if (IS_ERR(slave_crtc_state))
8812 		return PTR_ERR(slave_crtc_state);
8813 
8814 	/* master being enabled, slave was already configured? */
8815 	if (slave_crtc_state->uapi.enable)
8816 		goto claimed;
8817 
8818 	DRM_DEBUG_KMS("[CRTC:%d:%s] Used as slave for big joiner\n",
8819 		      slave->base.base.id, slave->base.name);
8820 
8821 	return copy_bigjoiner_crtc_state(slave_crtc_state, new_crtc_state);
8822 
8823 claimed:
8824 	DRM_DEBUG_KMS("[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
8825 		      "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
8826 		      slave->base.base.id, slave->base.name,
8827 		      master->base.base.id, master->base.name);
8828 	return -EINVAL;
8829 }
8830 
8831 static void kill_bigjoiner_slave(struct intel_atomic_state *state,
8832 				 struct intel_crtc_state *master_crtc_state)
8833 {
8834 	struct intel_crtc_state *slave_crtc_state =
8835 		intel_atomic_get_new_crtc_state(state, master_crtc_state->bigjoiner_linked_crtc);
8836 
8837 	slave_crtc_state->bigjoiner = master_crtc_state->bigjoiner = false;
8838 	slave_crtc_state->bigjoiner_slave = master_crtc_state->bigjoiner_slave = false;
8839 	slave_crtc_state->bigjoiner_linked_crtc = master_crtc_state->bigjoiner_linked_crtc = NULL;
8840 	intel_crtc_copy_uapi_to_hw_state(state, slave_crtc_state);
8841 }
8842 
8843 /**
8844  * DOC: asynchronous flip implementation
8845  *
8846  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
8847  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
8848  * Correspondingly, support is currently added for primary plane only.
8849  *
8850  * Async flip can only change the plane surface address, so anything else
8851  * changing is rejected from the intel_atomic_check_async() function.
8852  * Once this check is cleared, flip done interrupt is enabled using
8853  * the intel_crtc_enable_flip_done() function.
8854  *
8855  * As soon as the surface address register is written, flip done interrupt is
8856  * generated and the requested events are sent to the usersapce in the interrupt
8857  * handler itself. The timestamp and sequence sent during the flip done event
8858  * correspond to the last vblank and have no relation to the actual time when
8859  * the flip done event was sent.
8860  */
8861 static int intel_atomic_check_async(struct intel_atomic_state *state)
8862 {
8863 	struct drm_i915_private *i915 = to_i915(state->base.dev);
8864 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
8865 	const struct intel_plane_state *new_plane_state, *old_plane_state;
8866 	struct intel_crtc *crtc;
8867 	struct intel_plane *plane;
8868 	int i;
8869 
8870 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
8871 					    new_crtc_state, i) {
8872 		if (intel_crtc_needs_modeset(new_crtc_state)) {
8873 			drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
8874 			return -EINVAL;
8875 		}
8876 
8877 		if (!new_crtc_state->hw.active) {
8878 			drm_dbg_kms(&i915->drm, "CRTC inactive\n");
8879 			return -EINVAL;
8880 		}
8881 		if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
8882 			drm_dbg_kms(&i915->drm,
8883 				    "Active planes cannot be changed during async flip\n");
8884 			return -EINVAL;
8885 		}
8886 	}
8887 
8888 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
8889 					     new_plane_state, i) {
8890 		/*
8891 		 * TODO: Async flip is only supported through the page flip IOCTL
8892 		 * as of now. So support currently added for primary plane only.
8893 		 * Support for other planes on platforms on which supports
8894 		 * this(vlv/chv and icl+) should be added when async flip is
8895 		 * enabled in the atomic IOCTL path.
8896 		 */
8897 		if (!plane->async_flip)
8898 			return -EINVAL;
8899 
8900 		/*
8901 		 * FIXME: This check is kept generic for all platforms.
8902 		 * Need to verify this for all gen9 platforms to enable
8903 		 * this selectively if required.
8904 		 */
8905 		switch (new_plane_state->hw.fb->modifier) {
8906 		case I915_FORMAT_MOD_X_TILED:
8907 		case I915_FORMAT_MOD_Y_TILED:
8908 		case I915_FORMAT_MOD_Yf_TILED:
8909 			break;
8910 		default:
8911 			drm_dbg_kms(&i915->drm,
8912 				    "Linear memory/CCS does not support async flips\n");
8913 			return -EINVAL;
8914 		}
8915 
8916 		if (old_plane_state->view.color_plane[0].stride !=
8917 		    new_plane_state->view.color_plane[0].stride) {
8918 			drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
8919 			return -EINVAL;
8920 		}
8921 
8922 		if (old_plane_state->hw.fb->modifier !=
8923 		    new_plane_state->hw.fb->modifier) {
8924 			drm_dbg_kms(&i915->drm,
8925 				    "Framebuffer modifiers cannot be changed in async flip\n");
8926 			return -EINVAL;
8927 		}
8928 
8929 		if (old_plane_state->hw.fb->format !=
8930 		    new_plane_state->hw.fb->format) {
8931 			drm_dbg_kms(&i915->drm,
8932 				    "Framebuffer format cannot be changed in async flip\n");
8933 			return -EINVAL;
8934 		}
8935 
8936 		if (old_plane_state->hw.rotation !=
8937 		    new_plane_state->hw.rotation) {
8938 			drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
8939 			return -EINVAL;
8940 		}
8941 
8942 		if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
8943 		    !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
8944 			drm_dbg_kms(&i915->drm,
8945 				    "Plane size/co-ordinates cannot be changed in async flip\n");
8946 			return -EINVAL;
8947 		}
8948 
8949 		if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
8950 			drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
8951 			return -EINVAL;
8952 		}
8953 
8954 		if (old_plane_state->hw.pixel_blend_mode !=
8955 		    new_plane_state->hw.pixel_blend_mode) {
8956 			drm_dbg_kms(&i915->drm,
8957 				    "Pixel blend mode cannot be changed in async flip\n");
8958 			return -EINVAL;
8959 		}
8960 
8961 		if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
8962 			drm_dbg_kms(&i915->drm,
8963 				    "Color encoding cannot be changed in async flip\n");
8964 			return -EINVAL;
8965 		}
8966 
8967 		if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
8968 			drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
8969 			return -EINVAL;
8970 		}
8971 
8972 		/* plane decryption is allow to change only in synchronous flips */
8973 		if (old_plane_state->decrypt != new_plane_state->decrypt)
8974 			return -EINVAL;
8975 	}
8976 
8977 	return 0;
8978 }
8979 
8980 static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
8981 {
8982 	struct intel_crtc_state *crtc_state;
8983 	struct intel_crtc *crtc;
8984 	int i;
8985 
8986 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
8987 		struct intel_crtc_state *linked_crtc_state;
8988 		struct intel_crtc *linked_crtc;
8989 		int ret;
8990 
8991 		if (!crtc_state->bigjoiner)
8992 			continue;
8993 
8994 		linked_crtc = crtc_state->bigjoiner_linked_crtc;
8995 		linked_crtc_state = intel_atomic_get_crtc_state(&state->base, linked_crtc);
8996 		if (IS_ERR(linked_crtc_state))
8997 			return PTR_ERR(linked_crtc_state);
8998 
8999 		if (!intel_crtc_needs_modeset(crtc_state))
9000 			continue;
9001 
9002 		linked_crtc_state->uapi.mode_changed = true;
9003 
9004 		ret = drm_atomic_add_affected_connectors(&state->base,
9005 							 &linked_crtc->base);
9006 		if (ret)
9007 			return ret;
9008 
9009 		ret = intel_atomic_add_affected_planes(state, linked_crtc);
9010 		if (ret)
9011 			return ret;
9012 	}
9013 
9014 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9015 		/* Kill old bigjoiner link, we may re-establish afterwards */
9016 		if (intel_crtc_needs_modeset(crtc_state) &&
9017 		    crtc_state->bigjoiner && !crtc_state->bigjoiner_slave)
9018 			kill_bigjoiner_slave(state, crtc_state);
9019 	}
9020 
9021 	return 0;
9022 }
9023 
9024 /**
9025  * intel_atomic_check - validate state object
9026  * @dev: drm device
9027  * @_state: state to validate
9028  */
9029 static int intel_atomic_check(struct drm_device *dev,
9030 			      struct drm_atomic_state *_state)
9031 {
9032 	struct drm_i915_private *dev_priv = to_i915(dev);
9033 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
9034 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9035 	struct intel_crtc *crtc;
9036 	int ret, i;
9037 	bool any_ms = false;
9038 
9039 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9040 					    new_crtc_state, i) {
9041 		if (new_crtc_state->inherited != old_crtc_state->inherited)
9042 			new_crtc_state->uapi.mode_changed = true;
9043 	}
9044 
9045 	intel_vrr_check_modeset(state);
9046 
9047 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
9048 	if (ret)
9049 		goto fail;
9050 
9051 	ret = intel_bigjoiner_add_affected_crtcs(state);
9052 	if (ret)
9053 		goto fail;
9054 
9055 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9056 					    new_crtc_state, i) {
9057 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
9058 			/* Light copy */
9059 			intel_crtc_copy_uapi_to_hw_state_nomodeset(state, new_crtc_state);
9060 
9061 			continue;
9062 		}
9063 
9064 		if (!new_crtc_state->uapi.enable) {
9065 			if (!new_crtc_state->bigjoiner_slave) {
9066 				intel_crtc_copy_uapi_to_hw_state(state, new_crtc_state);
9067 				any_ms = true;
9068 			}
9069 			continue;
9070 		}
9071 
9072 		ret = intel_crtc_prepare_cleared_state(state, new_crtc_state);
9073 		if (ret)
9074 			goto fail;
9075 
9076 		ret = intel_modeset_pipe_config(state, new_crtc_state);
9077 		if (ret)
9078 			goto fail;
9079 
9080 		ret = intel_atomic_check_bigjoiner(state, crtc, old_crtc_state,
9081 						   new_crtc_state);
9082 		if (ret)
9083 			goto fail;
9084 	}
9085 
9086 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9087 					    new_crtc_state, i) {
9088 		if (!intel_crtc_needs_modeset(new_crtc_state))
9089 			continue;
9090 
9091 		ret = intel_modeset_pipe_config_late(new_crtc_state);
9092 		if (ret)
9093 			goto fail;
9094 
9095 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
9096 	}
9097 
9098 	/**
9099 	 * Check if fastset is allowed by external dependencies like other
9100 	 * pipes and transcoders.
9101 	 *
9102 	 * Right now it only forces a fullmodeset when the MST master
9103 	 * transcoder did not changed but the pipe of the master transcoder
9104 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
9105 	 * in case of port synced crtcs, if one of the synced crtcs
9106 	 * needs a full modeset, all other synced crtcs should be
9107 	 * forced a full modeset.
9108 	 */
9109 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9110 		if (!new_crtc_state->hw.enable || intel_crtc_needs_modeset(new_crtc_state))
9111 			continue;
9112 
9113 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
9114 			enum transcoder master = new_crtc_state->mst_master_transcoder;
9115 
9116 			if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
9117 				new_crtc_state->uapi.mode_changed = true;
9118 				new_crtc_state->update_pipe = false;
9119 			}
9120 		}
9121 
9122 		if (is_trans_port_sync_mode(new_crtc_state)) {
9123 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
9124 
9125 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
9126 				trans |= BIT(new_crtc_state->master_transcoder);
9127 
9128 			if (intel_cpu_transcoders_need_modeset(state, trans)) {
9129 				new_crtc_state->uapi.mode_changed = true;
9130 				new_crtc_state->update_pipe = false;
9131 			}
9132 		}
9133 
9134 		if (new_crtc_state->bigjoiner) {
9135 			struct intel_crtc_state *linked_crtc_state =
9136 				intel_atomic_get_new_crtc_state(state, new_crtc_state->bigjoiner_linked_crtc);
9137 
9138 			if (intel_crtc_needs_modeset(linked_crtc_state)) {
9139 				new_crtc_state->uapi.mode_changed = true;
9140 				new_crtc_state->update_pipe = false;
9141 			}
9142 		}
9143 	}
9144 
9145 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9146 					    new_crtc_state, i) {
9147 		if (intel_crtc_needs_modeset(new_crtc_state)) {
9148 			any_ms = true;
9149 			continue;
9150 		}
9151 
9152 		if (!new_crtc_state->update_pipe)
9153 			continue;
9154 
9155 		intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
9156 	}
9157 
9158 	if (any_ms && !check_digital_port_conflicts(state)) {
9159 		drm_dbg_kms(&dev_priv->drm,
9160 			    "rejecting conflicting digital port configuration\n");
9161 		ret = -EINVAL;
9162 		goto fail;
9163 	}
9164 
9165 	ret = drm_dp_mst_atomic_check(&state->base);
9166 	if (ret)
9167 		goto fail;
9168 
9169 	ret = intel_atomic_check_planes(state);
9170 	if (ret)
9171 		goto fail;
9172 
9173 	intel_fbc_choose_crtc(dev_priv, state);
9174 	ret = intel_compute_global_watermarks(state);
9175 	if (ret)
9176 		goto fail;
9177 
9178 	ret = intel_bw_atomic_check(state);
9179 	if (ret)
9180 		goto fail;
9181 
9182 	ret = intel_atomic_check_cdclk(state, &any_ms);
9183 	if (ret)
9184 		goto fail;
9185 
9186 	if (intel_any_crtc_needs_modeset(state))
9187 		any_ms = true;
9188 
9189 	if (any_ms) {
9190 		ret = intel_modeset_checks(state);
9191 		if (ret)
9192 			goto fail;
9193 
9194 		ret = intel_modeset_calc_cdclk(state);
9195 		if (ret)
9196 			return ret;
9197 
9198 		intel_modeset_clear_plls(state);
9199 	}
9200 
9201 	ret = intel_atomic_check_crtcs(state);
9202 	if (ret)
9203 		goto fail;
9204 
9205 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9206 					    new_crtc_state, i) {
9207 		if (new_crtc_state->uapi.async_flip) {
9208 			ret = intel_atomic_check_async(state);
9209 			if (ret)
9210 				goto fail;
9211 		}
9212 
9213 		if (!intel_crtc_needs_modeset(new_crtc_state) &&
9214 		    !new_crtc_state->update_pipe)
9215 			continue;
9216 
9217 		intel_dump_pipe_config(new_crtc_state, state,
9218 				       intel_crtc_needs_modeset(new_crtc_state) ?
9219 				       "[modeset]" : "[fastset]");
9220 	}
9221 
9222 	return 0;
9223 
9224  fail:
9225 	if (ret == -EDEADLK)
9226 		return ret;
9227 
9228 	/*
9229 	 * FIXME would probably be nice to know which crtc specifically
9230 	 * caused the failure, in cases where we can pinpoint it.
9231 	 */
9232 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9233 					    new_crtc_state, i)
9234 		intel_dump_pipe_config(new_crtc_state, state, "[failed]");
9235 
9236 	return ret;
9237 }
9238 
9239 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
9240 {
9241 	struct intel_crtc_state *crtc_state;
9242 	struct intel_crtc *crtc;
9243 	int i, ret;
9244 
9245 	ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
9246 	if (ret < 0)
9247 		return ret;
9248 
9249 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9250 		bool mode_changed = intel_crtc_needs_modeset(crtc_state);
9251 
9252 		if (mode_changed || crtc_state->update_pipe ||
9253 		    crtc_state->uapi.color_mgmt_changed) {
9254 			intel_dsb_prepare(crtc_state);
9255 		}
9256 	}
9257 
9258 	return 0;
9259 }
9260 
9261 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
9262 				  struct intel_crtc_state *crtc_state)
9263 {
9264 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9265 
9266 	if (DISPLAY_VER(dev_priv) != 2 || crtc_state->active_planes)
9267 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
9268 
9269 	if (crtc_state->has_pch_encoder) {
9270 		enum pipe pch_transcoder =
9271 			intel_crtc_pch_transcoder(crtc);
9272 
9273 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
9274 	}
9275 }
9276 
9277 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
9278 			       const struct intel_crtc_state *new_crtc_state)
9279 {
9280 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
9281 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9282 
9283 	/*
9284 	 * Update pipe size and adjust fitter if needed: the reason for this is
9285 	 * that in compute_mode_changes we check the native mode (not the pfit
9286 	 * mode) to see if we can flip rather than do a full mode set. In the
9287 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
9288 	 * pfit state, we'll end up with a big fb scanned out into the wrong
9289 	 * sized surface.
9290 	 */
9291 	intel_set_pipe_src_size(new_crtc_state);
9292 
9293 	/* on skylake this is done by detaching scalers */
9294 	if (DISPLAY_VER(dev_priv) >= 9) {
9295 		if (new_crtc_state->pch_pfit.enabled)
9296 			skl_pfit_enable(new_crtc_state);
9297 	} else if (HAS_PCH_SPLIT(dev_priv)) {
9298 		if (new_crtc_state->pch_pfit.enabled)
9299 			ilk_pfit_enable(new_crtc_state);
9300 		else if (old_crtc_state->pch_pfit.enabled)
9301 			ilk_pfit_disable(old_crtc_state);
9302 	}
9303 
9304 	/*
9305 	 * The register is supposedly single buffered so perhaps
9306 	 * not 100% correct to do this here. But SKL+ calculate
9307 	 * this based on the adjust pixel rate so pfit changes do
9308 	 * affect it and so it must be updated for fastsets.
9309 	 * HSW/BDW only really need this here for fastboot, after
9310 	 * that the value should not change without a full modeset.
9311 	 */
9312 	if (DISPLAY_VER(dev_priv) >= 9 ||
9313 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
9314 		hsw_set_linetime_wm(new_crtc_state);
9315 
9316 	if (DISPLAY_VER(dev_priv) >= 11)
9317 		icl_set_pipe_chicken(new_crtc_state);
9318 }
9319 
9320 static void commit_pipe_pre_planes(struct intel_atomic_state *state,
9321 				   struct intel_crtc *crtc)
9322 {
9323 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9324 	const struct intel_crtc_state *old_crtc_state =
9325 		intel_atomic_get_old_crtc_state(state, crtc);
9326 	const struct intel_crtc_state *new_crtc_state =
9327 		intel_atomic_get_new_crtc_state(state, crtc);
9328 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
9329 
9330 	/*
9331 	 * During modesets pipe configuration was programmed as the
9332 	 * CRTC was enabled.
9333 	 */
9334 	if (!modeset) {
9335 		if (new_crtc_state->uapi.color_mgmt_changed ||
9336 		    new_crtc_state->update_pipe)
9337 			intel_color_commit(new_crtc_state);
9338 
9339 		if (DISPLAY_VER(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
9340 			bdw_set_pipemisc(new_crtc_state);
9341 
9342 		if (new_crtc_state->update_pipe)
9343 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
9344 	}
9345 
9346 	intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
9347 
9348 	intel_atomic_update_watermarks(state, crtc);
9349 }
9350 
9351 static void commit_pipe_post_planes(struct intel_atomic_state *state,
9352 				    struct intel_crtc *crtc)
9353 {
9354 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9355 	const struct intel_crtc_state *new_crtc_state =
9356 		intel_atomic_get_new_crtc_state(state, crtc);
9357 
9358 	/*
9359 	 * Disable the scaler(s) after the plane(s) so that we don't
9360 	 * get a catastrophic underrun even if the two operations
9361 	 * end up happening in two different frames.
9362 	 */
9363 	if (DISPLAY_VER(dev_priv) >= 9 &&
9364 	    !intel_crtc_needs_modeset(new_crtc_state))
9365 		skl_detach_scalers(new_crtc_state);
9366 }
9367 
9368 static void intel_enable_crtc(struct intel_atomic_state *state,
9369 			      struct intel_crtc *crtc)
9370 {
9371 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9372 	const struct intel_crtc_state *new_crtc_state =
9373 		intel_atomic_get_new_crtc_state(state, crtc);
9374 
9375 	if (!intel_crtc_needs_modeset(new_crtc_state))
9376 		return;
9377 
9378 	intel_crtc_update_active_timings(new_crtc_state);
9379 
9380 	dev_priv->display->crtc_enable(state, crtc);
9381 
9382 	if (new_crtc_state->bigjoiner_slave)
9383 		return;
9384 
9385 	/* vblanks work again, re-enable pipe CRC. */
9386 	intel_crtc_enable_pipe_crc(crtc);
9387 }
9388 
9389 static void intel_update_crtc(struct intel_atomic_state *state,
9390 			      struct intel_crtc *crtc)
9391 {
9392 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9393 	const struct intel_crtc_state *old_crtc_state =
9394 		intel_atomic_get_old_crtc_state(state, crtc);
9395 	struct intel_crtc_state *new_crtc_state =
9396 		intel_atomic_get_new_crtc_state(state, crtc);
9397 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
9398 
9399 	if (!modeset) {
9400 		if (new_crtc_state->preload_luts &&
9401 		    (new_crtc_state->uapi.color_mgmt_changed ||
9402 		     new_crtc_state->update_pipe))
9403 			intel_color_load_luts(new_crtc_state);
9404 
9405 		intel_pre_plane_update(state, crtc);
9406 
9407 		if (new_crtc_state->update_pipe)
9408 			intel_encoders_update_pipe(state, crtc);
9409 	}
9410 
9411 	intel_fbc_update(state, crtc);
9412 
9413 	/* Perform vblank evasion around commit operation */
9414 	intel_pipe_update_start(new_crtc_state);
9415 
9416 	commit_pipe_pre_planes(state, crtc);
9417 
9418 	if (DISPLAY_VER(dev_priv) >= 9)
9419 		skl_update_planes_on_crtc(state, crtc);
9420 	else
9421 		i9xx_update_planes_on_crtc(state, crtc);
9422 
9423 	commit_pipe_post_planes(state, crtc);
9424 
9425 	intel_pipe_update_end(new_crtc_state);
9426 
9427 	/*
9428 	 * We usually enable FIFO underrun interrupts as part of the
9429 	 * CRTC enable sequence during modesets.  But when we inherit a
9430 	 * valid pipe configuration from the BIOS we need to take care
9431 	 * of enabling them on the CRTC's first fastset.
9432 	 */
9433 	if (new_crtc_state->update_pipe && !modeset &&
9434 	    old_crtc_state->inherited)
9435 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
9436 }
9437 
9438 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
9439 					  struct intel_crtc_state *old_crtc_state,
9440 					  struct intel_crtc_state *new_crtc_state,
9441 					  struct intel_crtc *crtc)
9442 {
9443 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9444 
9445 	drm_WARN_ON(&dev_priv->drm, old_crtc_state->bigjoiner_slave);
9446 
9447 	intel_encoders_pre_disable(state, crtc);
9448 
9449 	intel_crtc_disable_planes(state, crtc);
9450 
9451 	/*
9452 	 * We still need special handling for disabling bigjoiner master
9453 	 * and slaves since for slave we do not have encoder or plls
9454 	 * so we dont need to disable those.
9455 	 */
9456 	if (old_crtc_state->bigjoiner) {
9457 		intel_crtc_disable_planes(state,
9458 					  old_crtc_state->bigjoiner_linked_crtc);
9459 		old_crtc_state->bigjoiner_linked_crtc->active = false;
9460 	}
9461 
9462 	/*
9463 	 * We need to disable pipe CRC before disabling the pipe,
9464 	 * or we race against vblank off.
9465 	 */
9466 	intel_crtc_disable_pipe_crc(crtc);
9467 
9468 	dev_priv->display->crtc_disable(state, crtc);
9469 	crtc->active = false;
9470 	intel_fbc_disable(crtc);
9471 	intel_disable_shared_dpll(old_crtc_state);
9472 
9473 	/* FIXME unify this for all platforms */
9474 	if (!new_crtc_state->hw.active &&
9475 	    !HAS_GMCH(dev_priv))
9476 		intel_initial_watermarks(state, crtc);
9477 }
9478 
9479 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
9480 {
9481 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
9482 	struct intel_crtc *crtc;
9483 	u32 handled = 0;
9484 	int i;
9485 
9486 	/* Only disable port sync and MST slaves */
9487 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9488 					    new_crtc_state, i) {
9489 		if (!intel_crtc_needs_modeset(new_crtc_state) || old_crtc_state->bigjoiner)
9490 			continue;
9491 
9492 		if (!old_crtc_state->hw.active)
9493 			continue;
9494 
9495 		/* In case of Transcoder port Sync master slave CRTCs can be
9496 		 * assigned in any order and we need to make sure that
9497 		 * slave CRTCs are disabled first and then master CRTC since
9498 		 * Slave vblanks are masked till Master Vblanks.
9499 		 */
9500 		if (!is_trans_port_sync_slave(old_crtc_state) &&
9501 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
9502 			continue;
9503 
9504 		intel_pre_plane_update(state, crtc);
9505 		intel_old_crtc_state_disables(state, old_crtc_state,
9506 					      new_crtc_state, crtc);
9507 		handled |= BIT(crtc->pipe);
9508 	}
9509 
9510 	/* Disable everything else left on */
9511 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9512 					    new_crtc_state, i) {
9513 		if (!intel_crtc_needs_modeset(new_crtc_state) ||
9514 		    (handled & BIT(crtc->pipe)) ||
9515 		    old_crtc_state->bigjoiner_slave)
9516 			continue;
9517 
9518 		intel_pre_plane_update(state, crtc);
9519 		if (old_crtc_state->bigjoiner) {
9520 			struct intel_crtc *slave =
9521 				old_crtc_state->bigjoiner_linked_crtc;
9522 
9523 			intel_pre_plane_update(state, slave);
9524 		}
9525 
9526 		if (old_crtc_state->hw.active)
9527 			intel_old_crtc_state_disables(state, old_crtc_state,
9528 						      new_crtc_state, crtc);
9529 	}
9530 }
9531 
9532 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
9533 {
9534 	struct intel_crtc_state *new_crtc_state;
9535 	struct intel_crtc *crtc;
9536 	int i;
9537 
9538 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9539 		if (!new_crtc_state->hw.active)
9540 			continue;
9541 
9542 		intel_enable_crtc(state, crtc);
9543 		intel_update_crtc(state, crtc);
9544 	}
9545 }
9546 
9547 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
9548 {
9549 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9550 	struct intel_crtc *crtc;
9551 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9552 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
9553 	u8 update_pipes = 0, modeset_pipes = 0;
9554 	int i;
9555 
9556 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9557 		enum pipe pipe = crtc->pipe;
9558 
9559 		if (!new_crtc_state->hw.active)
9560 			continue;
9561 
9562 		/* ignore allocations for crtc's that have been turned off. */
9563 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
9564 			entries[pipe] = old_crtc_state->wm.skl.ddb;
9565 			update_pipes |= BIT(pipe);
9566 		} else {
9567 			modeset_pipes |= BIT(pipe);
9568 		}
9569 	}
9570 
9571 	/*
9572 	 * Whenever the number of active pipes changes, we need to make sure we
9573 	 * update the pipes in the right order so that their ddb allocations
9574 	 * never overlap with each other between CRTC updates. Otherwise we'll
9575 	 * cause pipe underruns and other bad stuff.
9576 	 *
9577 	 * So first lets enable all pipes that do not need a fullmodeset as
9578 	 * those don't have any external dependency.
9579 	 */
9580 	while (update_pipes) {
9581 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9582 						    new_crtc_state, i) {
9583 			enum pipe pipe = crtc->pipe;
9584 
9585 			if ((update_pipes & BIT(pipe)) == 0)
9586 				continue;
9587 
9588 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
9589 							entries, I915_MAX_PIPES, pipe))
9590 				continue;
9591 
9592 			entries[pipe] = new_crtc_state->wm.skl.ddb;
9593 			update_pipes &= ~BIT(pipe);
9594 
9595 			intel_update_crtc(state, crtc);
9596 
9597 			/*
9598 			 * If this is an already active pipe, it's DDB changed,
9599 			 * and this isn't the last pipe that needs updating
9600 			 * then we need to wait for a vblank to pass for the
9601 			 * new ddb allocation to take effect.
9602 			 */
9603 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
9604 						 &old_crtc_state->wm.skl.ddb) &&
9605 			    (update_pipes | modeset_pipes))
9606 				intel_wait_for_vblank(dev_priv, pipe);
9607 		}
9608 	}
9609 
9610 	update_pipes = modeset_pipes;
9611 
9612 	/*
9613 	 * Enable all pipes that needs a modeset and do not depends on other
9614 	 * pipes
9615 	 */
9616 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9617 		enum pipe pipe = crtc->pipe;
9618 
9619 		if ((modeset_pipes & BIT(pipe)) == 0)
9620 			continue;
9621 
9622 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
9623 		    is_trans_port_sync_master(new_crtc_state) ||
9624 		    (new_crtc_state->bigjoiner && !new_crtc_state->bigjoiner_slave))
9625 			continue;
9626 
9627 		modeset_pipes &= ~BIT(pipe);
9628 
9629 		intel_enable_crtc(state, crtc);
9630 	}
9631 
9632 	/*
9633 	 * Then we enable all remaining pipes that depend on other
9634 	 * pipes: MST slaves and port sync masters, big joiner master
9635 	 */
9636 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9637 		enum pipe pipe = crtc->pipe;
9638 
9639 		if ((modeset_pipes & BIT(pipe)) == 0)
9640 			continue;
9641 
9642 		modeset_pipes &= ~BIT(pipe);
9643 
9644 		intel_enable_crtc(state, crtc);
9645 	}
9646 
9647 	/*
9648 	 * Finally we do the plane updates/etc. for all pipes that got enabled.
9649 	 */
9650 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9651 		enum pipe pipe = crtc->pipe;
9652 
9653 		if ((update_pipes & BIT(pipe)) == 0)
9654 			continue;
9655 
9656 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
9657 									entries, I915_MAX_PIPES, pipe));
9658 
9659 		entries[pipe] = new_crtc_state->wm.skl.ddb;
9660 		update_pipes &= ~BIT(pipe);
9661 
9662 		intel_update_crtc(state, crtc);
9663 	}
9664 
9665 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
9666 	drm_WARN_ON(&dev_priv->drm, update_pipes);
9667 }
9668 
9669 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
9670 {
9671 	struct intel_atomic_state *state, *next;
9672 	struct llist_node *freed;
9673 
9674 	freed = llist_del_all(&dev_priv->atomic_helper.free_list);
9675 	llist_for_each_entry_safe(state, next, freed, freed)
9676 		drm_atomic_state_put(&state->base);
9677 }
9678 
9679 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
9680 {
9681 	struct drm_i915_private *dev_priv =
9682 		container_of(work, typeof(*dev_priv), atomic_helper.free_work);
9683 
9684 	intel_atomic_helper_free_state(dev_priv);
9685 }
9686 
9687 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
9688 {
9689 	struct wait_queue_entry wait_fence, wait_reset;
9690 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
9691 
9692 	init_wait_entry(&wait_fence, 0);
9693 	init_wait_entry(&wait_reset, 0);
9694 	for (;;) {
9695 		prepare_to_wait(&intel_state->commit_ready.wait,
9696 				&wait_fence, TASK_UNINTERRUPTIBLE);
9697 		prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
9698 					      I915_RESET_MODESET),
9699 				&wait_reset, TASK_UNINTERRUPTIBLE);
9700 
9701 
9702 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
9703 		    test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
9704 			break;
9705 
9706 		schedule();
9707 	}
9708 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
9709 	finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
9710 				  I915_RESET_MODESET),
9711 		    &wait_reset);
9712 }
9713 
9714 static void intel_cleanup_dsbs(struct intel_atomic_state *state)
9715 {
9716 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9717 	struct intel_crtc *crtc;
9718 	int i;
9719 
9720 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9721 					    new_crtc_state, i)
9722 		intel_dsb_cleanup(old_crtc_state);
9723 }
9724 
9725 static void intel_atomic_cleanup_work(struct work_struct *work)
9726 {
9727 	struct intel_atomic_state *state =
9728 		container_of(work, struct intel_atomic_state, base.commit_work);
9729 	struct drm_i915_private *i915 = to_i915(state->base.dev);
9730 
9731 	intel_cleanup_dsbs(state);
9732 	drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
9733 	drm_atomic_helper_commit_cleanup_done(&state->base);
9734 	drm_atomic_state_put(&state->base);
9735 
9736 	intel_atomic_helper_free_state(i915);
9737 }
9738 
9739 static void intel_atomic_prepare_plane_clear_colors(struct intel_atomic_state *state)
9740 {
9741 	struct drm_i915_private *i915 = to_i915(state->base.dev);
9742 	struct intel_plane *plane;
9743 	struct intel_plane_state *plane_state;
9744 	int i;
9745 
9746 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9747 		struct drm_framebuffer *fb = plane_state->hw.fb;
9748 		int ret;
9749 
9750 		if (!fb ||
9751 		    fb->modifier != I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC)
9752 			continue;
9753 
9754 		/*
9755 		 * The layout of the fast clear color value expected by HW
9756 		 * (the DRM ABI requiring this value to be located in fb at offset 0 of plane#2):
9757 		 * - 4 x 4 bytes per-channel value
9758 		 *   (in surface type specific float/int format provided by the fb user)
9759 		 * - 8 bytes native color value used by the display
9760 		 *   (converted/written by GPU during a fast clear operation using the
9761 		 *    above per-channel values)
9762 		 *
9763 		 * The commit's FB prepare hook already ensured that FB obj is pinned and the
9764 		 * caller made sure that the object is synced wrt. the related color clear value
9765 		 * GPU write on it.
9766 		 */
9767 		ret = i915_gem_object_read_from_page(intel_fb_obj(fb),
9768 						     fb->offsets[2] + 16,
9769 						     &plane_state->ccval,
9770 						     sizeof(plane_state->ccval));
9771 		/* The above could only fail if the FB obj has an unexpected backing store type. */
9772 		drm_WARN_ON(&i915->drm, ret);
9773 	}
9774 }
9775 
9776 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
9777 {
9778 	struct drm_device *dev = state->base.dev;
9779 	struct drm_i915_private *dev_priv = to_i915(dev);
9780 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
9781 	struct intel_crtc *crtc;
9782 	u64 put_domains[I915_MAX_PIPES] = {};
9783 	intel_wakeref_t wakeref = 0;
9784 	int i;
9785 
9786 	intel_atomic_commit_fence_wait(state);
9787 
9788 	drm_atomic_helper_wait_for_dependencies(&state->base);
9789 
9790 	if (state->modeset)
9791 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
9792 
9793 	intel_atomic_prepare_plane_clear_colors(state);
9794 
9795 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9796 					    new_crtc_state, i) {
9797 		if (intel_crtc_needs_modeset(new_crtc_state) ||
9798 		    new_crtc_state->update_pipe) {
9799 
9800 			put_domains[crtc->pipe] =
9801 				modeset_get_crtc_power_domains(new_crtc_state);
9802 		}
9803 	}
9804 
9805 	intel_commit_modeset_disables(state);
9806 
9807 	/* FIXME: Eventually get rid of our crtc->config pointer */
9808 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
9809 		crtc->config = new_crtc_state;
9810 
9811 	if (state->modeset) {
9812 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
9813 
9814 		intel_set_cdclk_pre_plane_update(state);
9815 
9816 		intel_modeset_verify_disabled(dev_priv, state);
9817 	}
9818 
9819 	intel_sagv_pre_plane_update(state);
9820 
9821 	/* Complete the events for pipes that have now been disabled */
9822 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9823 		bool modeset = intel_crtc_needs_modeset(new_crtc_state);
9824 
9825 		/* Complete events for now disable pipes here. */
9826 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
9827 			spin_lock_irq(&dev->event_lock);
9828 			drm_crtc_send_vblank_event(&crtc->base,
9829 						   new_crtc_state->uapi.event);
9830 			spin_unlock_irq(&dev->event_lock);
9831 
9832 			new_crtc_state->uapi.event = NULL;
9833 		}
9834 	}
9835 
9836 	if (state->modeset)
9837 		intel_encoders_update_prepare(state);
9838 
9839 	intel_dbuf_pre_plane_update(state);
9840 	intel_psr_pre_plane_update(state);
9841 
9842 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9843 		if (new_crtc_state->uapi.async_flip)
9844 			intel_crtc_enable_flip_done(state, crtc);
9845 	}
9846 
9847 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
9848 	dev_priv->display->commit_modeset_enables(state);
9849 
9850 	if (state->modeset) {
9851 		intel_encoders_update_complete(state);
9852 
9853 		intel_set_cdclk_post_plane_update(state);
9854 	}
9855 
9856 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
9857 	 * already, but still need the state for the delayed optimization. To
9858 	 * fix this:
9859 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
9860 	 * - schedule that vblank worker _before_ calling hw_done
9861 	 * - at the start of commit_tail, cancel it _synchrously
9862 	 * - switch over to the vblank wait helper in the core after that since
9863 	 *   we don't need out special handling any more.
9864 	 */
9865 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
9866 
9867 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9868 		if (new_crtc_state->uapi.async_flip)
9869 			intel_crtc_disable_flip_done(state, crtc);
9870 
9871 		if (new_crtc_state->hw.active &&
9872 		    !intel_crtc_needs_modeset(new_crtc_state) &&
9873 		    !new_crtc_state->preload_luts &&
9874 		    (new_crtc_state->uapi.color_mgmt_changed ||
9875 		     new_crtc_state->update_pipe))
9876 			intel_color_load_luts(new_crtc_state);
9877 	}
9878 
9879 	/*
9880 	 * Now that the vblank has passed, we can go ahead and program the
9881 	 * optimal watermarks on platforms that need two-step watermark
9882 	 * programming.
9883 	 *
9884 	 * TODO: Move this (and other cleanup) to an async worker eventually.
9885 	 */
9886 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9887 					    new_crtc_state, i) {
9888 		/*
9889 		 * Gen2 reports pipe underruns whenever all planes are disabled.
9890 		 * So re-enable underrun reporting after some planes get enabled.
9891 		 *
9892 		 * We do this before .optimize_watermarks() so that we have a
9893 		 * chance of catching underruns with the intermediate watermarks
9894 		 * vs. the new plane configuration.
9895 		 */
9896 		if (DISPLAY_VER(dev_priv) == 2 && planes_enabling(old_crtc_state, new_crtc_state))
9897 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
9898 
9899 		intel_optimize_watermarks(state, crtc);
9900 	}
9901 
9902 	intel_dbuf_post_plane_update(state);
9903 	intel_psr_post_plane_update(state);
9904 
9905 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
9906 		intel_post_plane_update(state, crtc);
9907 
9908 		modeset_put_crtc_power_domains(crtc, put_domains[crtc->pipe]);
9909 
9910 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
9911 
9912 		/*
9913 		 * DSB cleanup is done in cleanup_work aligning with framebuffer
9914 		 * cleanup. So copy and reset the dsb structure to sync with
9915 		 * commit_done and later do dsb cleanup in cleanup_work.
9916 		 */
9917 		old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
9918 	}
9919 
9920 	/* Underruns don't always raise interrupts, so check manually */
9921 	intel_check_cpu_fifo_underruns(dev_priv);
9922 	intel_check_pch_fifo_underruns(dev_priv);
9923 
9924 	if (state->modeset)
9925 		intel_verify_planes(state);
9926 
9927 	intel_sagv_post_plane_update(state);
9928 
9929 	drm_atomic_helper_commit_hw_done(&state->base);
9930 
9931 	if (state->modeset) {
9932 		/* As one of the primary mmio accessors, KMS has a high
9933 		 * likelihood of triggering bugs in unclaimed access. After we
9934 		 * finish modesetting, see if an error has been flagged, and if
9935 		 * so enable debugging for the next modeset - and hope we catch
9936 		 * the culprit.
9937 		 */
9938 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
9939 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
9940 	}
9941 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
9942 
9943 	/*
9944 	 * Defer the cleanup of the old state to a separate worker to not
9945 	 * impede the current task (userspace for blocking modesets) that
9946 	 * are executed inline. For out-of-line asynchronous modesets/flips,
9947 	 * deferring to a new worker seems overkill, but we would place a
9948 	 * schedule point (cond_resched()) here anyway to keep latencies
9949 	 * down.
9950 	 */
9951 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
9952 	queue_work(system_highpri_wq, &state->base.commit_work);
9953 }
9954 
9955 static void intel_atomic_commit_work(struct work_struct *work)
9956 {
9957 	struct intel_atomic_state *state =
9958 		container_of(work, struct intel_atomic_state, base.commit_work);
9959 
9960 	intel_atomic_commit_tail(state);
9961 }
9962 
9963 static int __i915_sw_fence_call
9964 intel_atomic_commit_ready(struct i915_sw_fence *fence,
9965 			  enum i915_sw_fence_notify notify)
9966 {
9967 	struct intel_atomic_state *state =
9968 		container_of(fence, struct intel_atomic_state, commit_ready);
9969 
9970 	switch (notify) {
9971 	case FENCE_COMPLETE:
9972 		/* we do blocking waits in the worker, nothing to do here */
9973 		break;
9974 	case FENCE_FREE:
9975 		{
9976 			struct intel_atomic_helper *helper =
9977 				&to_i915(state->base.dev)->atomic_helper;
9978 
9979 			if (llist_add(&state->freed, &helper->free_list))
9980 				schedule_work(&helper->free_work);
9981 			break;
9982 		}
9983 	}
9984 
9985 	return NOTIFY_DONE;
9986 }
9987 
9988 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
9989 {
9990 	struct intel_plane_state *old_plane_state, *new_plane_state;
9991 	struct intel_plane *plane;
9992 	int i;
9993 
9994 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
9995 					     new_plane_state, i)
9996 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
9997 					to_intel_frontbuffer(new_plane_state->hw.fb),
9998 					plane->frontbuffer_bit);
9999 }
10000 
10001 static int intel_atomic_commit(struct drm_device *dev,
10002 			       struct drm_atomic_state *_state,
10003 			       bool nonblock)
10004 {
10005 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
10006 	struct drm_i915_private *dev_priv = to_i915(dev);
10007 	int ret = 0;
10008 
10009 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
10010 
10011 	drm_atomic_state_get(&state->base);
10012 	i915_sw_fence_init(&state->commit_ready,
10013 			   intel_atomic_commit_ready);
10014 
10015 	/*
10016 	 * The intel_legacy_cursor_update() fast path takes care
10017 	 * of avoiding the vblank waits for simple cursor
10018 	 * movement and flips. For cursor on/off and size changes,
10019 	 * we want to perform the vblank waits so that watermark
10020 	 * updates happen during the correct frames. Gen9+ have
10021 	 * double buffered watermarks and so shouldn't need this.
10022 	 *
10023 	 * Unset state->legacy_cursor_update before the call to
10024 	 * drm_atomic_helper_setup_commit() because otherwise
10025 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
10026 	 * we get FIFO underruns because we didn't wait
10027 	 * for vblank.
10028 	 *
10029 	 * FIXME doing watermarks and fb cleanup from a vblank worker
10030 	 * (assuming we had any) would solve these problems.
10031 	 */
10032 	if (DISPLAY_VER(dev_priv) < 9 && state->base.legacy_cursor_update) {
10033 		struct intel_crtc_state *new_crtc_state;
10034 		struct intel_crtc *crtc;
10035 		int i;
10036 
10037 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
10038 			if (new_crtc_state->wm.need_postvbl_update ||
10039 			    new_crtc_state->update_wm_post)
10040 				state->base.legacy_cursor_update = false;
10041 	}
10042 
10043 	ret = intel_atomic_prepare_commit(state);
10044 	if (ret) {
10045 		drm_dbg_atomic(&dev_priv->drm,
10046 			       "Preparing state failed with %i\n", ret);
10047 		i915_sw_fence_commit(&state->commit_ready);
10048 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
10049 		return ret;
10050 	}
10051 
10052 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
10053 	if (!ret)
10054 		ret = drm_atomic_helper_swap_state(&state->base, true);
10055 	if (!ret)
10056 		intel_atomic_swap_global_state(state);
10057 
10058 	if (ret) {
10059 		struct intel_crtc_state *new_crtc_state;
10060 		struct intel_crtc *crtc;
10061 		int i;
10062 
10063 		i915_sw_fence_commit(&state->commit_ready);
10064 
10065 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
10066 			intel_dsb_cleanup(new_crtc_state);
10067 
10068 		drm_atomic_helper_cleanup_planes(dev, &state->base);
10069 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
10070 		return ret;
10071 	}
10072 	intel_shared_dpll_swap_state(state);
10073 	intel_atomic_track_fbs(state);
10074 
10075 	drm_atomic_state_get(&state->base);
10076 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
10077 
10078 	i915_sw_fence_commit(&state->commit_ready);
10079 	if (nonblock && state->modeset) {
10080 		queue_work(dev_priv->modeset_wq, &state->base.commit_work);
10081 	} else if (nonblock) {
10082 		queue_work(dev_priv->flip_wq, &state->base.commit_work);
10083 	} else {
10084 		if (state->modeset)
10085 			flush_workqueue(dev_priv->modeset_wq);
10086 		intel_atomic_commit_tail(state);
10087 	}
10088 
10089 	return 0;
10090 }
10091 
10092 /**
10093  * intel_plane_destroy - destroy a plane
10094  * @plane: plane to destroy
10095  *
10096  * Common destruction function for all types of planes (primary, cursor,
10097  * sprite).
10098  */
10099 void intel_plane_destroy(struct drm_plane *plane)
10100 {
10101 	drm_plane_cleanup(plane);
10102 	kfree(to_intel_plane(plane));
10103 }
10104 
10105 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
10106 {
10107 	struct intel_plane *plane;
10108 
10109 	for_each_intel_plane(&dev_priv->drm, plane) {
10110 		struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
10111 								  plane->pipe);
10112 
10113 		plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
10114 	}
10115 }
10116 
10117 
10118 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
10119 				      struct drm_file *file)
10120 {
10121 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
10122 	struct drm_crtc *drmmode_crtc;
10123 	struct intel_crtc *crtc;
10124 
10125 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
10126 	if (!drmmode_crtc)
10127 		return -ENOENT;
10128 
10129 	crtc = to_intel_crtc(drmmode_crtc);
10130 	pipe_from_crtc_id->pipe = crtc->pipe;
10131 
10132 	return 0;
10133 }
10134 
10135 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
10136 {
10137 	struct drm_device *dev = encoder->base.dev;
10138 	struct intel_encoder *source_encoder;
10139 	u32 possible_clones = 0;
10140 
10141 	for_each_intel_encoder(dev, source_encoder) {
10142 		if (encoders_cloneable(encoder, source_encoder))
10143 			possible_clones |= drm_encoder_mask(&source_encoder->base);
10144 	}
10145 
10146 	return possible_clones;
10147 }
10148 
10149 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
10150 {
10151 	struct drm_device *dev = encoder->base.dev;
10152 	struct intel_crtc *crtc;
10153 	u32 possible_crtcs = 0;
10154 
10155 	for_each_intel_crtc(dev, crtc) {
10156 		if (encoder->pipe_mask & BIT(crtc->pipe))
10157 			possible_crtcs |= drm_crtc_mask(&crtc->base);
10158 	}
10159 
10160 	return possible_crtcs;
10161 }
10162 
10163 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
10164 {
10165 	if (!IS_MOBILE(dev_priv))
10166 		return false;
10167 
10168 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
10169 		return false;
10170 
10171 	if (IS_IRONLAKE(dev_priv) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
10172 		return false;
10173 
10174 	return true;
10175 }
10176 
10177 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
10178 {
10179 	if (DISPLAY_VER(dev_priv) >= 9)
10180 		return false;
10181 
10182 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
10183 		return false;
10184 
10185 	if (HAS_PCH_LPT_H(dev_priv) &&
10186 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
10187 		return false;
10188 
10189 	/* DDI E can't be used if DDI A requires 4 lanes */
10190 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
10191 		return false;
10192 
10193 	if (!dev_priv->vbt.int_crt_support)
10194 		return false;
10195 
10196 	return true;
10197 }
10198 
10199 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
10200 {
10201 	struct intel_encoder *encoder;
10202 	bool dpd_is_edp = false;
10203 
10204 	intel_pps_unlock_regs_wa(dev_priv);
10205 
10206 	if (!HAS_DISPLAY(dev_priv))
10207 		return;
10208 
10209 	if (IS_DG2(dev_priv)) {
10210 		intel_ddi_init(dev_priv, PORT_A);
10211 		intel_ddi_init(dev_priv, PORT_B);
10212 		intel_ddi_init(dev_priv, PORT_C);
10213 		intel_ddi_init(dev_priv, PORT_D_XELPD);
10214 	} else if (IS_ALDERLAKE_P(dev_priv)) {
10215 		intel_ddi_init(dev_priv, PORT_A);
10216 		intel_ddi_init(dev_priv, PORT_B);
10217 		intel_ddi_init(dev_priv, PORT_TC1);
10218 		intel_ddi_init(dev_priv, PORT_TC2);
10219 		intel_ddi_init(dev_priv, PORT_TC3);
10220 		intel_ddi_init(dev_priv, PORT_TC4);
10221 		icl_dsi_init(dev_priv);
10222 	} else if (IS_ALDERLAKE_S(dev_priv)) {
10223 		intel_ddi_init(dev_priv, PORT_A);
10224 		intel_ddi_init(dev_priv, PORT_TC1);
10225 		intel_ddi_init(dev_priv, PORT_TC2);
10226 		intel_ddi_init(dev_priv, PORT_TC3);
10227 		intel_ddi_init(dev_priv, PORT_TC4);
10228 	} else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv)) {
10229 		intel_ddi_init(dev_priv, PORT_A);
10230 		intel_ddi_init(dev_priv, PORT_B);
10231 		intel_ddi_init(dev_priv, PORT_TC1);
10232 		intel_ddi_init(dev_priv, PORT_TC2);
10233 	} else if (DISPLAY_VER(dev_priv) >= 12) {
10234 		intel_ddi_init(dev_priv, PORT_A);
10235 		intel_ddi_init(dev_priv, PORT_B);
10236 		intel_ddi_init(dev_priv, PORT_TC1);
10237 		intel_ddi_init(dev_priv, PORT_TC2);
10238 		intel_ddi_init(dev_priv, PORT_TC3);
10239 		intel_ddi_init(dev_priv, PORT_TC4);
10240 		intel_ddi_init(dev_priv, PORT_TC5);
10241 		intel_ddi_init(dev_priv, PORT_TC6);
10242 		icl_dsi_init(dev_priv);
10243 	} else if (IS_JSL_EHL(dev_priv)) {
10244 		intel_ddi_init(dev_priv, PORT_A);
10245 		intel_ddi_init(dev_priv, PORT_B);
10246 		intel_ddi_init(dev_priv, PORT_C);
10247 		intel_ddi_init(dev_priv, PORT_D);
10248 		icl_dsi_init(dev_priv);
10249 	} else if (DISPLAY_VER(dev_priv) == 11) {
10250 		intel_ddi_init(dev_priv, PORT_A);
10251 		intel_ddi_init(dev_priv, PORT_B);
10252 		intel_ddi_init(dev_priv, PORT_C);
10253 		intel_ddi_init(dev_priv, PORT_D);
10254 		intel_ddi_init(dev_priv, PORT_E);
10255 		intel_ddi_init(dev_priv, PORT_F);
10256 		icl_dsi_init(dev_priv);
10257 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
10258 		intel_ddi_init(dev_priv, PORT_A);
10259 		intel_ddi_init(dev_priv, PORT_B);
10260 		intel_ddi_init(dev_priv, PORT_C);
10261 		vlv_dsi_init(dev_priv);
10262 	} else if (DISPLAY_VER(dev_priv) >= 9) {
10263 		intel_ddi_init(dev_priv, PORT_A);
10264 		intel_ddi_init(dev_priv, PORT_B);
10265 		intel_ddi_init(dev_priv, PORT_C);
10266 		intel_ddi_init(dev_priv, PORT_D);
10267 		intel_ddi_init(dev_priv, PORT_E);
10268 	} else if (HAS_DDI(dev_priv)) {
10269 		u32 found;
10270 
10271 		if (intel_ddi_crt_present(dev_priv))
10272 			intel_crt_init(dev_priv);
10273 
10274 		/* Haswell uses DDI functions to detect digital outputs. */
10275 		found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
10276 		if (found)
10277 			intel_ddi_init(dev_priv, PORT_A);
10278 
10279 		found = intel_de_read(dev_priv, SFUSE_STRAP);
10280 		if (found & SFUSE_STRAP_DDIB_DETECTED)
10281 			intel_ddi_init(dev_priv, PORT_B);
10282 		if (found & SFUSE_STRAP_DDIC_DETECTED)
10283 			intel_ddi_init(dev_priv, PORT_C);
10284 		if (found & SFUSE_STRAP_DDID_DETECTED)
10285 			intel_ddi_init(dev_priv, PORT_D);
10286 		if (found & SFUSE_STRAP_DDIF_DETECTED)
10287 			intel_ddi_init(dev_priv, PORT_F);
10288 	} else if (HAS_PCH_SPLIT(dev_priv)) {
10289 		int found;
10290 
10291 		/*
10292 		 * intel_edp_init_connector() depends on this completing first,
10293 		 * to prevent the registration of both eDP and LVDS and the
10294 		 * incorrect sharing of the PPS.
10295 		 */
10296 		intel_lvds_init(dev_priv);
10297 		intel_crt_init(dev_priv);
10298 
10299 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
10300 
10301 		if (ilk_has_edp_a(dev_priv))
10302 			g4x_dp_init(dev_priv, DP_A, PORT_A);
10303 
10304 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
10305 			/* PCH SDVOB multiplex with HDMIB */
10306 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
10307 			if (!found)
10308 				g4x_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
10309 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
10310 				g4x_dp_init(dev_priv, PCH_DP_B, PORT_B);
10311 		}
10312 
10313 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
10314 			g4x_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
10315 
10316 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
10317 			g4x_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
10318 
10319 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
10320 			g4x_dp_init(dev_priv, PCH_DP_C, PORT_C);
10321 
10322 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
10323 			g4x_dp_init(dev_priv, PCH_DP_D, PORT_D);
10324 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
10325 		bool has_edp, has_port;
10326 
10327 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
10328 			intel_crt_init(dev_priv);
10329 
10330 		/*
10331 		 * The DP_DETECTED bit is the latched state of the DDC
10332 		 * SDA pin at boot. However since eDP doesn't require DDC
10333 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
10334 		 * eDP ports may have been muxed to an alternate function.
10335 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
10336 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
10337 		 * detect eDP ports.
10338 		 *
10339 		 * Sadly the straps seem to be missing sometimes even for HDMI
10340 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
10341 		 * and VBT for the presence of the port. Additionally we can't
10342 		 * trust the port type the VBT declares as we've seen at least
10343 		 * HDMI ports that the VBT claim are DP or eDP.
10344 		 */
10345 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
10346 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
10347 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
10348 			has_edp &= g4x_dp_init(dev_priv, VLV_DP_B, PORT_B);
10349 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
10350 			g4x_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
10351 
10352 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
10353 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
10354 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
10355 			has_edp &= g4x_dp_init(dev_priv, VLV_DP_C, PORT_C);
10356 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
10357 			g4x_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
10358 
10359 		if (IS_CHERRYVIEW(dev_priv)) {
10360 			/*
10361 			 * eDP not supported on port D,
10362 			 * so no need to worry about it
10363 			 */
10364 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
10365 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
10366 				g4x_dp_init(dev_priv, CHV_DP_D, PORT_D);
10367 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
10368 				g4x_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
10369 		}
10370 
10371 		vlv_dsi_init(dev_priv);
10372 	} else if (IS_PINEVIEW(dev_priv)) {
10373 		intel_lvds_init(dev_priv);
10374 		intel_crt_init(dev_priv);
10375 	} else if (IS_DISPLAY_VER(dev_priv, 3, 4)) {
10376 		bool found = false;
10377 
10378 		if (IS_MOBILE(dev_priv))
10379 			intel_lvds_init(dev_priv);
10380 
10381 		intel_crt_init(dev_priv);
10382 
10383 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
10384 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
10385 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
10386 			if (!found && IS_G4X(dev_priv)) {
10387 				drm_dbg_kms(&dev_priv->drm,
10388 					    "probing HDMI on SDVOB\n");
10389 				g4x_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
10390 			}
10391 
10392 			if (!found && IS_G4X(dev_priv))
10393 				g4x_dp_init(dev_priv, DP_B, PORT_B);
10394 		}
10395 
10396 		/* Before G4X SDVOC doesn't have its own detect register */
10397 
10398 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
10399 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
10400 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
10401 		}
10402 
10403 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
10404 
10405 			if (IS_G4X(dev_priv)) {
10406 				drm_dbg_kms(&dev_priv->drm,
10407 					    "probing HDMI on SDVOC\n");
10408 				g4x_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
10409 			}
10410 			if (IS_G4X(dev_priv))
10411 				g4x_dp_init(dev_priv, DP_C, PORT_C);
10412 		}
10413 
10414 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
10415 			g4x_dp_init(dev_priv, DP_D, PORT_D);
10416 
10417 		if (SUPPORTS_TV(dev_priv))
10418 			intel_tv_init(dev_priv);
10419 	} else if (DISPLAY_VER(dev_priv) == 2) {
10420 		if (IS_I85X(dev_priv))
10421 			intel_lvds_init(dev_priv);
10422 
10423 		intel_crt_init(dev_priv);
10424 		intel_dvo_init(dev_priv);
10425 	}
10426 
10427 	for_each_intel_encoder(&dev_priv->drm, encoder) {
10428 		encoder->base.possible_crtcs =
10429 			intel_encoder_possible_crtcs(encoder);
10430 		encoder->base.possible_clones =
10431 			intel_encoder_possible_clones(encoder);
10432 	}
10433 
10434 	intel_init_pch_refclk(dev_priv);
10435 
10436 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
10437 }
10438 
10439 static enum drm_mode_status
10440 intel_mode_valid(struct drm_device *dev,
10441 		 const struct drm_display_mode *mode)
10442 {
10443 	struct drm_i915_private *dev_priv = to_i915(dev);
10444 	int hdisplay_max, htotal_max;
10445 	int vdisplay_max, vtotal_max;
10446 
10447 	/*
10448 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
10449 	 * of DBLSCAN modes to the output's mode list when they detect
10450 	 * the scaling mode property on the connector. And they don't
10451 	 * ask the kernel to validate those modes in any way until
10452 	 * modeset time at which point the client gets a protocol error.
10453 	 * So in order to not upset those clients we silently ignore the
10454 	 * DBLSCAN flag on such connectors. For other connectors we will
10455 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
10456 	 * And we always reject DBLSCAN modes in connector->mode_valid()
10457 	 * as we never want such modes on the connector's mode list.
10458 	 */
10459 
10460 	if (mode->vscan > 1)
10461 		return MODE_NO_VSCAN;
10462 
10463 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
10464 		return MODE_H_ILLEGAL;
10465 
10466 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
10467 			   DRM_MODE_FLAG_NCSYNC |
10468 			   DRM_MODE_FLAG_PCSYNC))
10469 		return MODE_HSYNC;
10470 
10471 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
10472 			   DRM_MODE_FLAG_PIXMUX |
10473 			   DRM_MODE_FLAG_CLKDIV2))
10474 		return MODE_BAD;
10475 
10476 	/* Transcoder timing limits */
10477 	if (DISPLAY_VER(dev_priv) >= 11) {
10478 		hdisplay_max = 16384;
10479 		vdisplay_max = 8192;
10480 		htotal_max = 16384;
10481 		vtotal_max = 8192;
10482 	} else if (DISPLAY_VER(dev_priv) >= 9 ||
10483 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
10484 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
10485 		vdisplay_max = 4096;
10486 		htotal_max = 8192;
10487 		vtotal_max = 8192;
10488 	} else if (DISPLAY_VER(dev_priv) >= 3) {
10489 		hdisplay_max = 4096;
10490 		vdisplay_max = 4096;
10491 		htotal_max = 8192;
10492 		vtotal_max = 8192;
10493 	} else {
10494 		hdisplay_max = 2048;
10495 		vdisplay_max = 2048;
10496 		htotal_max = 4096;
10497 		vtotal_max = 4096;
10498 	}
10499 
10500 	if (mode->hdisplay > hdisplay_max ||
10501 	    mode->hsync_start > htotal_max ||
10502 	    mode->hsync_end > htotal_max ||
10503 	    mode->htotal > htotal_max)
10504 		return MODE_H_ILLEGAL;
10505 
10506 	if (mode->vdisplay > vdisplay_max ||
10507 	    mode->vsync_start > vtotal_max ||
10508 	    mode->vsync_end > vtotal_max ||
10509 	    mode->vtotal > vtotal_max)
10510 		return MODE_V_ILLEGAL;
10511 
10512 	if (DISPLAY_VER(dev_priv) >= 5) {
10513 		if (mode->hdisplay < 64 ||
10514 		    mode->htotal - mode->hdisplay < 32)
10515 			return MODE_H_ILLEGAL;
10516 
10517 		if (mode->vtotal - mode->vdisplay < 5)
10518 			return MODE_V_ILLEGAL;
10519 	} else {
10520 		if (mode->htotal - mode->hdisplay < 32)
10521 			return MODE_H_ILLEGAL;
10522 
10523 		if (mode->vtotal - mode->vdisplay < 3)
10524 			return MODE_V_ILLEGAL;
10525 	}
10526 
10527 	/*
10528 	 * Cantiga+ cannot handle modes with a hsync front porch of 0.
10529 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
10530 	 */
10531 	if ((DISPLAY_VER(dev_priv) > 4 || IS_G4X(dev_priv)) &&
10532 	    mode->hsync_start == mode->hdisplay)
10533 		return MODE_H_ILLEGAL;
10534 
10535 	return MODE_OK;
10536 }
10537 
10538 enum drm_mode_status
10539 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
10540 				const struct drm_display_mode *mode,
10541 				bool bigjoiner)
10542 {
10543 	int plane_width_max, plane_height_max;
10544 
10545 	/*
10546 	 * intel_mode_valid() should be
10547 	 * sufficient on older platforms.
10548 	 */
10549 	if (DISPLAY_VER(dev_priv) < 9)
10550 		return MODE_OK;
10551 
10552 	/*
10553 	 * Most people will probably want a fullscreen
10554 	 * plane so let's not advertize modes that are
10555 	 * too big for that.
10556 	 */
10557 	if (DISPLAY_VER(dev_priv) >= 11) {
10558 		plane_width_max = 5120 << bigjoiner;
10559 		plane_height_max = 4320;
10560 	} else {
10561 		plane_width_max = 5120;
10562 		plane_height_max = 4096;
10563 	}
10564 
10565 	if (mode->hdisplay > plane_width_max)
10566 		return MODE_H_ILLEGAL;
10567 
10568 	if (mode->vdisplay > plane_height_max)
10569 		return MODE_V_ILLEGAL;
10570 
10571 	return MODE_OK;
10572 }
10573 
10574 static const struct drm_mode_config_funcs intel_mode_funcs = {
10575 	.fb_create = intel_user_framebuffer_create,
10576 	.get_format_info = intel_get_format_info,
10577 	.output_poll_changed = intel_fbdev_output_poll_changed,
10578 	.mode_valid = intel_mode_valid,
10579 	.atomic_check = intel_atomic_check,
10580 	.atomic_commit = intel_atomic_commit,
10581 	.atomic_state_alloc = intel_atomic_state_alloc,
10582 	.atomic_state_clear = intel_atomic_state_clear,
10583 	.atomic_state_free = intel_atomic_state_free,
10584 };
10585 
10586 static const struct drm_i915_display_funcs skl_display_funcs = {
10587 	.get_pipe_config = hsw_get_pipe_config,
10588 	.crtc_enable = hsw_crtc_enable,
10589 	.crtc_disable = hsw_crtc_disable,
10590 	.commit_modeset_enables = skl_commit_modeset_enables,
10591 	.get_initial_plane_config = skl_get_initial_plane_config,
10592 };
10593 
10594 static const struct drm_i915_display_funcs ddi_display_funcs = {
10595 	.get_pipe_config = hsw_get_pipe_config,
10596 	.crtc_enable = hsw_crtc_enable,
10597 	.crtc_disable = hsw_crtc_disable,
10598 	.commit_modeset_enables = intel_commit_modeset_enables,
10599 	.get_initial_plane_config = i9xx_get_initial_plane_config,
10600 };
10601 
10602 static const struct drm_i915_display_funcs pch_split_display_funcs = {
10603 	.get_pipe_config = ilk_get_pipe_config,
10604 	.crtc_enable = ilk_crtc_enable,
10605 	.crtc_disable = ilk_crtc_disable,
10606 	.commit_modeset_enables = intel_commit_modeset_enables,
10607 	.get_initial_plane_config = i9xx_get_initial_plane_config,
10608 };
10609 
10610 static const struct drm_i915_display_funcs vlv_display_funcs = {
10611 	.get_pipe_config = i9xx_get_pipe_config,
10612 	.crtc_enable = valleyview_crtc_enable,
10613 	.crtc_disable = i9xx_crtc_disable,
10614 	.commit_modeset_enables = intel_commit_modeset_enables,
10615 	.get_initial_plane_config = i9xx_get_initial_plane_config,
10616 };
10617 
10618 static const struct drm_i915_display_funcs i9xx_display_funcs = {
10619 	.get_pipe_config = i9xx_get_pipe_config,
10620 	.crtc_enable = i9xx_crtc_enable,
10621 	.crtc_disable = i9xx_crtc_disable,
10622 	.commit_modeset_enables = intel_commit_modeset_enables,
10623 	.get_initial_plane_config = i9xx_get_initial_plane_config,
10624 };
10625 
10626 /**
10627  * intel_init_display_hooks - initialize the display modesetting hooks
10628  * @dev_priv: device private
10629  */
10630 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
10631 {
10632 	if (!HAS_DISPLAY(dev_priv))
10633 		return;
10634 
10635 	intel_init_cdclk_hooks(dev_priv);
10636 	intel_init_audio_hooks(dev_priv);
10637 
10638 	intel_dpll_init_clock_hook(dev_priv);
10639 
10640 	if (DISPLAY_VER(dev_priv) >= 9) {
10641 		dev_priv->display = &skl_display_funcs;
10642 	} else if (HAS_DDI(dev_priv)) {
10643 		dev_priv->display = &ddi_display_funcs;
10644 	} else if (HAS_PCH_SPLIT(dev_priv)) {
10645 		dev_priv->display = &pch_split_display_funcs;
10646 	} else if (IS_CHERRYVIEW(dev_priv) ||
10647 		   IS_VALLEYVIEW(dev_priv)) {
10648 		dev_priv->display = &vlv_display_funcs;
10649 	} else {
10650 		dev_priv->display = &i9xx_display_funcs;
10651 	}
10652 
10653 	intel_fdi_init_hook(dev_priv);
10654 }
10655 
10656 void intel_modeset_init_hw(struct drm_i915_private *i915)
10657 {
10658 	struct intel_cdclk_state *cdclk_state;
10659 
10660 	if (!HAS_DISPLAY(i915))
10661 		return;
10662 
10663 	cdclk_state = to_intel_cdclk_state(i915->cdclk.obj.state);
10664 
10665 	intel_update_cdclk(i915);
10666 	intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
10667 	cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
10668 }
10669 
10670 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
10671 {
10672 	struct drm_plane *plane;
10673 	struct intel_crtc *crtc;
10674 
10675 	for_each_intel_crtc(state->dev, crtc) {
10676 		struct intel_crtc_state *crtc_state;
10677 
10678 		crtc_state = intel_atomic_get_crtc_state(state, crtc);
10679 		if (IS_ERR(crtc_state))
10680 			return PTR_ERR(crtc_state);
10681 
10682 		if (crtc_state->hw.active) {
10683 			/*
10684 			 * Preserve the inherited flag to avoid
10685 			 * taking the full modeset path.
10686 			 */
10687 			crtc_state->inherited = true;
10688 		}
10689 	}
10690 
10691 	drm_for_each_plane(plane, state->dev) {
10692 		struct drm_plane_state *plane_state;
10693 
10694 		plane_state = drm_atomic_get_plane_state(state, plane);
10695 		if (IS_ERR(plane_state))
10696 			return PTR_ERR(plane_state);
10697 	}
10698 
10699 	return 0;
10700 }
10701 
10702 /*
10703  * Calculate what we think the watermarks should be for the state we've read
10704  * out of the hardware and then immediately program those watermarks so that
10705  * we ensure the hardware settings match our internal state.
10706  *
10707  * We can calculate what we think WM's should be by creating a duplicate of the
10708  * current state (which was constructed during hardware readout) and running it
10709  * through the atomic check code to calculate new watermark values in the
10710  * state object.
10711  */
10712 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
10713 {
10714 	struct drm_atomic_state *state;
10715 	struct intel_atomic_state *intel_state;
10716 	struct intel_crtc *crtc;
10717 	struct intel_crtc_state *crtc_state;
10718 	struct drm_modeset_acquire_ctx ctx;
10719 	int ret;
10720 	int i;
10721 
10722 	/* Only supported on platforms that use atomic watermark design */
10723 	if (!dev_priv->wm_disp->optimize_watermarks)
10724 		return;
10725 
10726 	state = drm_atomic_state_alloc(&dev_priv->drm);
10727 	if (drm_WARN_ON(&dev_priv->drm, !state))
10728 		return;
10729 
10730 	intel_state = to_intel_atomic_state(state);
10731 
10732 	drm_modeset_acquire_init(&ctx, 0);
10733 
10734 retry:
10735 	state->acquire_ctx = &ctx;
10736 
10737 	/*
10738 	 * Hardware readout is the only time we don't want to calculate
10739 	 * intermediate watermarks (since we don't trust the current
10740 	 * watermarks).
10741 	 */
10742 	if (!HAS_GMCH(dev_priv))
10743 		intel_state->skip_intermediate_wm = true;
10744 
10745 	ret = sanitize_watermarks_add_affected(state);
10746 	if (ret)
10747 		goto fail;
10748 
10749 	ret = intel_atomic_check(&dev_priv->drm, state);
10750 	if (ret)
10751 		goto fail;
10752 
10753 	/* Write calculated watermark values back */
10754 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
10755 		crtc_state->wm.need_postvbl_update = true;
10756 		intel_optimize_watermarks(intel_state, crtc);
10757 
10758 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
10759 	}
10760 
10761 fail:
10762 	if (ret == -EDEADLK) {
10763 		drm_atomic_state_clear(state);
10764 		drm_modeset_backoff(&ctx);
10765 		goto retry;
10766 	}
10767 
10768 	/*
10769 	 * If we fail here, it means that the hardware appears to be
10770 	 * programmed in a way that shouldn't be possible, given our
10771 	 * understanding of watermark requirements.  This might mean a
10772 	 * mistake in the hardware readout code or a mistake in the
10773 	 * watermark calculations for a given platform.  Raise a WARN
10774 	 * so that this is noticeable.
10775 	 *
10776 	 * If this actually happens, we'll have to just leave the
10777 	 * BIOS-programmed watermarks untouched and hope for the best.
10778 	 */
10779 	drm_WARN(&dev_priv->drm, ret,
10780 		 "Could not determine valid watermarks for inherited state\n");
10781 
10782 	drm_atomic_state_put(state);
10783 
10784 	drm_modeset_drop_locks(&ctx);
10785 	drm_modeset_acquire_fini(&ctx);
10786 }
10787 
10788 static int intel_initial_commit(struct drm_device *dev)
10789 {
10790 	struct drm_atomic_state *state = NULL;
10791 	struct drm_modeset_acquire_ctx ctx;
10792 	struct intel_crtc *crtc;
10793 	int ret = 0;
10794 
10795 	state = drm_atomic_state_alloc(dev);
10796 	if (!state)
10797 		return -ENOMEM;
10798 
10799 	drm_modeset_acquire_init(&ctx, 0);
10800 
10801 retry:
10802 	state->acquire_ctx = &ctx;
10803 
10804 	for_each_intel_crtc(dev, crtc) {
10805 		struct intel_crtc_state *crtc_state =
10806 			intel_atomic_get_crtc_state(state, crtc);
10807 
10808 		if (IS_ERR(crtc_state)) {
10809 			ret = PTR_ERR(crtc_state);
10810 			goto out;
10811 		}
10812 
10813 		if (crtc_state->hw.active) {
10814 			struct intel_encoder *encoder;
10815 
10816 			/*
10817 			 * We've not yet detected sink capabilities
10818 			 * (audio,infoframes,etc.) and thus we don't want to
10819 			 * force a full state recomputation yet. We want that to
10820 			 * happen only for the first real commit from userspace.
10821 			 * So preserve the inherited flag for the time being.
10822 			 */
10823 			crtc_state->inherited = true;
10824 
10825 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
10826 			if (ret)
10827 				goto out;
10828 
10829 			/*
10830 			 * FIXME hack to force a LUT update to avoid the
10831 			 * plane update forcing the pipe gamma on without
10832 			 * having a proper LUT loaded. Remove once we
10833 			 * have readout for pipe gamma enable.
10834 			 */
10835 			crtc_state->uapi.color_mgmt_changed = true;
10836 
10837 			for_each_intel_encoder_mask(dev, encoder,
10838 						    crtc_state->uapi.encoder_mask) {
10839 				if (encoder->initial_fastset_check &&
10840 				    !encoder->initial_fastset_check(encoder, crtc_state)) {
10841 					ret = drm_atomic_add_affected_connectors(state,
10842 										 &crtc->base);
10843 					if (ret)
10844 						goto out;
10845 				}
10846 			}
10847 		}
10848 	}
10849 
10850 	ret = drm_atomic_commit(state);
10851 
10852 out:
10853 	if (ret == -EDEADLK) {
10854 		drm_atomic_state_clear(state);
10855 		drm_modeset_backoff(&ctx);
10856 		goto retry;
10857 	}
10858 
10859 	drm_atomic_state_put(state);
10860 
10861 	drm_modeset_drop_locks(&ctx);
10862 	drm_modeset_acquire_fini(&ctx);
10863 
10864 	return ret;
10865 }
10866 
10867 static void intel_mode_config_init(struct drm_i915_private *i915)
10868 {
10869 	struct drm_mode_config *mode_config = &i915->drm.mode_config;
10870 
10871 	drm_mode_config_init(&i915->drm);
10872 	INIT_LIST_HEAD(&i915->global_obj_list);
10873 
10874 	mode_config->min_width = 0;
10875 	mode_config->min_height = 0;
10876 
10877 	mode_config->preferred_depth = 24;
10878 	mode_config->prefer_shadow = 1;
10879 
10880 	mode_config->funcs = &intel_mode_funcs;
10881 
10882 	mode_config->async_page_flip = HAS_ASYNC_FLIPS(i915);
10883 
10884 	/*
10885 	 * Maximum framebuffer dimensions, chosen to match
10886 	 * the maximum render engine surface size on gen4+.
10887 	 */
10888 	if (DISPLAY_VER(i915) >= 7) {
10889 		mode_config->max_width = 16384;
10890 		mode_config->max_height = 16384;
10891 	} else if (DISPLAY_VER(i915) >= 4) {
10892 		mode_config->max_width = 8192;
10893 		mode_config->max_height = 8192;
10894 	} else if (DISPLAY_VER(i915) == 3) {
10895 		mode_config->max_width = 4096;
10896 		mode_config->max_height = 4096;
10897 	} else {
10898 		mode_config->max_width = 2048;
10899 		mode_config->max_height = 2048;
10900 	}
10901 
10902 	if (IS_I845G(i915) || IS_I865G(i915)) {
10903 		mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
10904 		mode_config->cursor_height = 1023;
10905 	} else if (IS_I830(i915) || IS_I85X(i915) ||
10906 		   IS_I915G(i915) || IS_I915GM(i915)) {
10907 		mode_config->cursor_width = 64;
10908 		mode_config->cursor_height = 64;
10909 	} else {
10910 		mode_config->cursor_width = 256;
10911 		mode_config->cursor_height = 256;
10912 	}
10913 }
10914 
10915 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
10916 {
10917 	intel_atomic_global_obj_cleanup(i915);
10918 	drm_mode_config_cleanup(&i915->drm);
10919 }
10920 
10921 /* part #1: call before irq install */
10922 int intel_modeset_init_noirq(struct drm_i915_private *i915)
10923 {
10924 	int ret;
10925 
10926 	if (i915_inject_probe_failure(i915))
10927 		return -ENODEV;
10928 
10929 	if (HAS_DISPLAY(i915)) {
10930 		ret = drm_vblank_init(&i915->drm,
10931 				      INTEL_NUM_PIPES(i915));
10932 		if (ret)
10933 			return ret;
10934 	}
10935 
10936 	intel_bios_init(i915);
10937 
10938 	ret = intel_vga_register(i915);
10939 	if (ret)
10940 		goto cleanup_bios;
10941 
10942 	/* FIXME: completely on the wrong abstraction layer */
10943 	intel_power_domains_init_hw(i915, false);
10944 
10945 	if (!HAS_DISPLAY(i915))
10946 		return 0;
10947 
10948 	intel_dmc_ucode_init(i915);
10949 
10950 	i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
10951 	i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
10952 					WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
10953 
10954 	i915->framestart_delay = 1; /* 1-4 */
10955 
10956 	i915->window2_delay = 0; /* No DSB so no window2 delay */
10957 
10958 	intel_mode_config_init(i915);
10959 
10960 	ret = intel_cdclk_init(i915);
10961 	if (ret)
10962 		goto cleanup_vga_client_pw_domain_dmc;
10963 
10964 	ret = intel_dbuf_init(i915);
10965 	if (ret)
10966 		goto cleanup_vga_client_pw_domain_dmc;
10967 
10968 	ret = intel_bw_init(i915);
10969 	if (ret)
10970 		goto cleanup_vga_client_pw_domain_dmc;
10971 
10972 	init_llist_head(&i915->atomic_helper.free_list);
10973 	INIT_WORK(&i915->atomic_helper.free_work,
10974 		  intel_atomic_helper_free_state_worker);
10975 
10976 	intel_init_quirks(i915);
10977 
10978 	intel_fbc_init(i915);
10979 
10980 	return 0;
10981 
10982 cleanup_vga_client_pw_domain_dmc:
10983 	intel_dmc_ucode_fini(i915);
10984 	intel_power_domains_driver_remove(i915);
10985 	intel_vga_unregister(i915);
10986 cleanup_bios:
10987 	intel_bios_driver_remove(i915);
10988 
10989 	return ret;
10990 }
10991 
10992 /* part #2: call after irq install, but before gem init */
10993 int intel_modeset_init_nogem(struct drm_i915_private *i915)
10994 {
10995 	struct drm_device *dev = &i915->drm;
10996 	enum pipe pipe;
10997 	struct intel_crtc *crtc;
10998 	int ret;
10999 
11000 	if (!HAS_DISPLAY(i915))
11001 		return 0;
11002 
11003 	intel_init_pm(i915);
11004 
11005 	intel_panel_sanitize_ssc(i915);
11006 
11007 	intel_pps_setup(i915);
11008 
11009 	intel_gmbus_setup(i915);
11010 
11011 	drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
11012 		    INTEL_NUM_PIPES(i915),
11013 		    INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
11014 
11015 	for_each_pipe(i915, pipe) {
11016 		ret = intel_crtc_init(i915, pipe);
11017 		if (ret) {
11018 			intel_mode_config_cleanup(i915);
11019 			return ret;
11020 		}
11021 	}
11022 
11023 	intel_plane_possible_crtcs_init(i915);
11024 	intel_shared_dpll_init(dev);
11025 	intel_fdi_pll_freq_update(i915);
11026 
11027 	intel_update_czclk(i915);
11028 	intel_modeset_init_hw(i915);
11029 	intel_dpll_update_ref_clks(i915);
11030 
11031 	intel_hdcp_component_init(i915);
11032 
11033 	if (i915->max_cdclk_freq == 0)
11034 		intel_update_max_cdclk(i915);
11035 
11036 	/*
11037 	 * If the platform has HTI, we need to find out whether it has reserved
11038 	 * any display resources before we create our display outputs.
11039 	 */
11040 	if (INTEL_INFO(i915)->display.has_hti)
11041 		i915->hti_state = intel_de_read(i915, HDPORT_STATE);
11042 
11043 	/* Just disable it once at startup */
11044 	intel_vga_disable(i915);
11045 	intel_setup_outputs(i915);
11046 
11047 	drm_modeset_lock_all(dev);
11048 	intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
11049 	intel_acpi_assign_connector_fwnodes(i915);
11050 	drm_modeset_unlock_all(dev);
11051 
11052 	for_each_intel_crtc(dev, crtc) {
11053 		if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
11054 			continue;
11055 		intel_crtc_initial_plane_config(crtc);
11056 	}
11057 
11058 	/*
11059 	 * Make sure hardware watermarks really match the state we read out.
11060 	 * Note that we need to do this after reconstructing the BIOS fb's
11061 	 * since the watermark calculation done here will use pstate->fb.
11062 	 */
11063 	if (!HAS_GMCH(i915))
11064 		sanitize_watermarks(i915);
11065 
11066 	return 0;
11067 }
11068 
11069 /* part #3: call after gem init */
11070 int intel_modeset_init(struct drm_i915_private *i915)
11071 {
11072 	int ret;
11073 
11074 	if (!HAS_DISPLAY(i915))
11075 		return 0;
11076 
11077 	/*
11078 	 * Force all active planes to recompute their states. So that on
11079 	 * mode_setcrtc after probe, all the intel_plane_state variables
11080 	 * are already calculated and there is no assert_plane warnings
11081 	 * during bootup.
11082 	 */
11083 	ret = intel_initial_commit(&i915->drm);
11084 	if (ret)
11085 		drm_dbg_kms(&i915->drm, "Initial modeset failed, %d\n", ret);
11086 
11087 	intel_overlay_setup(i915);
11088 
11089 	ret = intel_fbdev_init(&i915->drm);
11090 	if (ret)
11091 		return ret;
11092 
11093 	/* Only enable hotplug handling once the fbdev is fully set up. */
11094 	intel_hpd_init(i915);
11095 	intel_hpd_poll_disable(i915);
11096 
11097 	intel_init_ipc(i915);
11098 
11099 	return 0;
11100 }
11101 
11102 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
11103 {
11104 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
11105 	/* 640x480@60Hz, ~25175 kHz */
11106 	struct dpll clock = {
11107 		.m1 = 18,
11108 		.m2 = 7,
11109 		.p1 = 13,
11110 		.p2 = 4,
11111 		.n = 2,
11112 	};
11113 	u32 dpll, fp;
11114 	int i;
11115 
11116 	drm_WARN_ON(&dev_priv->drm,
11117 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
11118 
11119 	drm_dbg_kms(&dev_priv->drm,
11120 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
11121 		    pipe_name(pipe), clock.vco, clock.dot);
11122 
11123 	fp = i9xx_dpll_compute_fp(&clock);
11124 	dpll = DPLL_DVO_2X_MODE |
11125 		DPLL_VGA_MODE_DIS |
11126 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
11127 		PLL_P2_DIVIDE_BY_4 |
11128 		PLL_REF_INPUT_DREFCLK |
11129 		DPLL_VCO_ENABLE;
11130 
11131 	intel_de_write(dev_priv, FP0(pipe), fp);
11132 	intel_de_write(dev_priv, FP1(pipe), fp);
11133 
11134 	intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
11135 	intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
11136 	intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
11137 	intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
11138 	intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
11139 	intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
11140 	intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
11141 
11142 	/*
11143 	 * Apparently we need to have VGA mode enabled prior to changing
11144 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
11145 	 * dividers, even though the register value does change.
11146 	 */
11147 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
11148 	intel_de_write(dev_priv, DPLL(pipe), dpll);
11149 
11150 	/* Wait for the clocks to stabilize. */
11151 	intel_de_posting_read(dev_priv, DPLL(pipe));
11152 	udelay(150);
11153 
11154 	/* The pixel multiplier can only be updated once the
11155 	 * DPLL is enabled and the clocks are stable.
11156 	 *
11157 	 * So write it again.
11158 	 */
11159 	intel_de_write(dev_priv, DPLL(pipe), dpll);
11160 
11161 	/* We do this three times for luck */
11162 	for (i = 0; i < 3 ; i++) {
11163 		intel_de_write(dev_priv, DPLL(pipe), dpll);
11164 		intel_de_posting_read(dev_priv, DPLL(pipe));
11165 		udelay(150); /* wait for warmup */
11166 	}
11167 
11168 	intel_de_write(dev_priv, PIPECONF(pipe),
11169 		       PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
11170 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
11171 
11172 	intel_wait_for_pipe_scanline_moving(crtc);
11173 }
11174 
11175 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
11176 {
11177 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
11178 
11179 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
11180 		    pipe_name(pipe));
11181 
11182 	drm_WARN_ON(&dev_priv->drm,
11183 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
11184 		    DISPLAY_PLANE_ENABLE);
11185 	drm_WARN_ON(&dev_priv->drm,
11186 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
11187 		    DISPLAY_PLANE_ENABLE);
11188 	drm_WARN_ON(&dev_priv->drm,
11189 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
11190 		    DISPLAY_PLANE_ENABLE);
11191 	drm_WARN_ON(&dev_priv->drm,
11192 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
11193 	drm_WARN_ON(&dev_priv->drm,
11194 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
11195 
11196 	intel_de_write(dev_priv, PIPECONF(pipe), 0);
11197 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
11198 
11199 	intel_wait_for_pipe_scanline_stopped(crtc);
11200 
11201 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
11202 	intel_de_posting_read(dev_priv, DPLL(pipe));
11203 }
11204 
11205 static void
11206 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
11207 {
11208 	struct intel_crtc *crtc;
11209 
11210 	if (DISPLAY_VER(dev_priv) >= 4)
11211 		return;
11212 
11213 	for_each_intel_crtc(&dev_priv->drm, crtc) {
11214 		struct intel_plane *plane =
11215 			to_intel_plane(crtc->base.primary);
11216 		struct intel_crtc *plane_crtc;
11217 		enum pipe pipe;
11218 
11219 		if (!plane->get_hw_state(plane, &pipe))
11220 			continue;
11221 
11222 		if (pipe == crtc->pipe)
11223 			continue;
11224 
11225 		drm_dbg_kms(&dev_priv->drm,
11226 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
11227 			    plane->base.base.id, plane->base.name);
11228 
11229 		plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
11230 		intel_plane_disable_noatomic(plane_crtc, plane);
11231 	}
11232 }
11233 
11234 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
11235 {
11236 	struct drm_device *dev = crtc->base.dev;
11237 	struct intel_encoder *encoder;
11238 
11239 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
11240 		return true;
11241 
11242 	return false;
11243 }
11244 
11245 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
11246 {
11247 	struct drm_device *dev = encoder->base.dev;
11248 	struct intel_connector *connector;
11249 
11250 	for_each_connector_on_encoder(dev, &encoder->base, connector)
11251 		return connector;
11252 
11253 	return NULL;
11254 }
11255 
11256 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
11257 			      enum pipe pch_transcoder)
11258 {
11259 	return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
11260 		(HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
11261 }
11262 
11263 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
11264 {
11265 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
11266 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
11267 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
11268 
11269 	if (DISPLAY_VER(dev_priv) >= 9 ||
11270 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
11271 		i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
11272 		u32 val;
11273 
11274 		if (transcoder_is_dsi(cpu_transcoder))
11275 			return;
11276 
11277 		val = intel_de_read(dev_priv, reg);
11278 		val &= ~HSW_FRAME_START_DELAY_MASK;
11279 		val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
11280 		intel_de_write(dev_priv, reg, val);
11281 	} else {
11282 		i915_reg_t reg = PIPECONF(cpu_transcoder);
11283 		u32 val;
11284 
11285 		val = intel_de_read(dev_priv, reg);
11286 		val &= ~PIPECONF_FRAME_START_DELAY_MASK;
11287 		val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
11288 		intel_de_write(dev_priv, reg, val);
11289 	}
11290 
11291 	if (!crtc_state->has_pch_encoder)
11292 		return;
11293 
11294 	if (HAS_PCH_IBX(dev_priv)) {
11295 		i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
11296 		u32 val;
11297 
11298 		val = intel_de_read(dev_priv, reg);
11299 		val &= ~TRANS_FRAME_START_DELAY_MASK;
11300 		val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
11301 		intel_de_write(dev_priv, reg, val);
11302 	} else {
11303 		enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
11304 		i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
11305 		u32 val;
11306 
11307 		val = intel_de_read(dev_priv, reg);
11308 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
11309 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
11310 		intel_de_write(dev_priv, reg, val);
11311 	}
11312 }
11313 
11314 static void intel_sanitize_crtc(struct intel_crtc *crtc,
11315 				struct drm_modeset_acquire_ctx *ctx)
11316 {
11317 	struct drm_device *dev = crtc->base.dev;
11318 	struct drm_i915_private *dev_priv = to_i915(dev);
11319 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
11320 
11321 	if (crtc_state->hw.active) {
11322 		struct intel_plane *plane;
11323 
11324 		/* Clear any frame start delays used for debugging left by the BIOS */
11325 		intel_sanitize_frame_start_delay(crtc_state);
11326 
11327 		/* Disable everything but the primary plane */
11328 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
11329 			const struct intel_plane_state *plane_state =
11330 				to_intel_plane_state(plane->base.state);
11331 
11332 			if (plane_state->uapi.visible &&
11333 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
11334 				intel_plane_disable_noatomic(crtc, plane);
11335 		}
11336 
11337 		/* Disable any background color/etc. set by the BIOS */
11338 		intel_color_commit(crtc_state);
11339 	}
11340 
11341 	/* Adjust the state of the output pipe according to whether we
11342 	 * have active connectors/encoders. */
11343 	if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
11344 	    !crtc_state->bigjoiner_slave)
11345 		intel_crtc_disable_noatomic(crtc, ctx);
11346 
11347 	if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
11348 		/*
11349 		 * We start out with underrun reporting disabled to avoid races.
11350 		 * For correct bookkeeping mark this on active crtcs.
11351 		 *
11352 		 * Also on gmch platforms we dont have any hardware bits to
11353 		 * disable the underrun reporting. Which means we need to start
11354 		 * out with underrun reporting disabled also on inactive pipes,
11355 		 * since otherwise we'll complain about the garbage we read when
11356 		 * e.g. coming up after runtime pm.
11357 		 *
11358 		 * No protection against concurrent access is required - at
11359 		 * worst a fifo underrun happens which also sets this to false.
11360 		 */
11361 		crtc->cpu_fifo_underrun_disabled = true;
11362 		/*
11363 		 * We track the PCH trancoder underrun reporting state
11364 		 * within the crtc. With crtc for pipe A housing the underrun
11365 		 * reporting state for PCH transcoder A, crtc for pipe B housing
11366 		 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
11367 		 * and marking underrun reporting as disabled for the non-existing
11368 		 * PCH transcoders B and C would prevent enabling the south
11369 		 * error interrupt (see cpt_can_enable_serr_int()).
11370 		 */
11371 		if (has_pch_trancoder(dev_priv, crtc->pipe))
11372 			crtc->pch_fifo_underrun_disabled = true;
11373 	}
11374 }
11375 
11376 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
11377 {
11378 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
11379 
11380 	/*
11381 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
11382 	 * the hardware when a high res displays plugged in. DPLL P
11383 	 * divider is zero, and the pipe timings are bonkers. We'll
11384 	 * try to disable everything in that case.
11385 	 *
11386 	 * FIXME would be nice to be able to sanitize this state
11387 	 * without several WARNs, but for now let's take the easy
11388 	 * road.
11389 	 */
11390 	return IS_SANDYBRIDGE(dev_priv) &&
11391 		crtc_state->hw.active &&
11392 		crtc_state->shared_dpll &&
11393 		crtc_state->port_clock == 0;
11394 }
11395 
11396 static void intel_sanitize_encoder(struct intel_encoder *encoder)
11397 {
11398 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
11399 	struct intel_connector *connector;
11400 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
11401 	struct intel_crtc_state *crtc_state = crtc ?
11402 		to_intel_crtc_state(crtc->base.state) : NULL;
11403 
11404 	/* We need to check both for a crtc link (meaning that the
11405 	 * encoder is active and trying to read from a pipe) and the
11406 	 * pipe itself being active. */
11407 	bool has_active_crtc = crtc_state &&
11408 		crtc_state->hw.active;
11409 
11410 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
11411 		drm_dbg_kms(&dev_priv->drm,
11412 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
11413 			    pipe_name(crtc->pipe));
11414 		has_active_crtc = false;
11415 	}
11416 
11417 	connector = intel_encoder_find_connector(encoder);
11418 	if (connector && !has_active_crtc) {
11419 		drm_dbg_kms(&dev_priv->drm,
11420 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
11421 			    encoder->base.base.id,
11422 			    encoder->base.name);
11423 
11424 		/* Connector is active, but has no active pipe. This is
11425 		 * fallout from our resume register restoring. Disable
11426 		 * the encoder manually again. */
11427 		if (crtc_state) {
11428 			struct drm_encoder *best_encoder;
11429 
11430 			drm_dbg_kms(&dev_priv->drm,
11431 				    "[ENCODER:%d:%s] manually disabled\n",
11432 				    encoder->base.base.id,
11433 				    encoder->base.name);
11434 
11435 			/* avoid oopsing in case the hooks consult best_encoder */
11436 			best_encoder = connector->base.state->best_encoder;
11437 			connector->base.state->best_encoder = &encoder->base;
11438 
11439 			/* FIXME NULL atomic state passed! */
11440 			if (encoder->disable)
11441 				encoder->disable(NULL, encoder, crtc_state,
11442 						 connector->base.state);
11443 			if (encoder->post_disable)
11444 				encoder->post_disable(NULL, encoder, crtc_state,
11445 						      connector->base.state);
11446 
11447 			connector->base.state->best_encoder = best_encoder;
11448 		}
11449 		encoder->base.crtc = NULL;
11450 
11451 		/* Inconsistent output/port/pipe state happens presumably due to
11452 		 * a bug in one of the get_hw_state functions. Or someplace else
11453 		 * in our code, like the register restore mess on resume. Clamp
11454 		 * things to off as a safer default. */
11455 
11456 		connector->base.dpms = DRM_MODE_DPMS_OFF;
11457 		connector->base.encoder = NULL;
11458 	}
11459 
11460 	/* notify opregion of the sanitized encoder state */
11461 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
11462 
11463 	if (HAS_DDI(dev_priv))
11464 		intel_ddi_sanitize_encoder_pll_mapping(encoder);
11465 }
11466 
11467 /* FIXME read out full plane state for all planes */
11468 static void readout_plane_state(struct drm_i915_private *dev_priv)
11469 {
11470 	struct intel_plane *plane;
11471 	struct intel_crtc *crtc;
11472 
11473 	for_each_intel_plane(&dev_priv->drm, plane) {
11474 		struct intel_plane_state *plane_state =
11475 			to_intel_plane_state(plane->base.state);
11476 		struct intel_crtc_state *crtc_state;
11477 		enum pipe pipe = PIPE_A;
11478 		bool visible;
11479 
11480 		visible = plane->get_hw_state(plane, &pipe);
11481 
11482 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
11483 		crtc_state = to_intel_crtc_state(crtc->base.state);
11484 
11485 		intel_set_plane_visible(crtc_state, plane_state, visible);
11486 
11487 		drm_dbg_kms(&dev_priv->drm,
11488 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
11489 			    plane->base.base.id, plane->base.name,
11490 			    enableddisabled(visible), pipe_name(pipe));
11491 	}
11492 
11493 	for_each_intel_crtc(&dev_priv->drm, crtc) {
11494 		struct intel_crtc_state *crtc_state =
11495 			to_intel_crtc_state(crtc->base.state);
11496 
11497 		fixup_plane_bitmasks(crtc_state);
11498 	}
11499 }
11500 
11501 static void intel_modeset_readout_hw_state(struct drm_device *dev)
11502 {
11503 	struct drm_i915_private *dev_priv = to_i915(dev);
11504 	struct intel_cdclk_state *cdclk_state =
11505 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
11506 	struct intel_dbuf_state *dbuf_state =
11507 		to_intel_dbuf_state(dev_priv->dbuf.obj.state);
11508 	enum pipe pipe;
11509 	struct intel_crtc *crtc;
11510 	struct intel_encoder *encoder;
11511 	struct intel_connector *connector;
11512 	struct drm_connector_list_iter conn_iter;
11513 	u8 active_pipes = 0;
11514 
11515 	for_each_intel_crtc(dev, crtc) {
11516 		struct intel_crtc_state *crtc_state =
11517 			to_intel_crtc_state(crtc->base.state);
11518 
11519 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
11520 		intel_crtc_free_hw_state(crtc_state);
11521 		intel_crtc_state_reset(crtc_state, crtc);
11522 
11523 		intel_crtc_get_pipe_config(crtc_state);
11524 
11525 		crtc_state->hw.enable = crtc_state->hw.active;
11526 
11527 		crtc->base.enabled = crtc_state->hw.enable;
11528 		crtc->active = crtc_state->hw.active;
11529 
11530 		if (crtc_state->hw.active)
11531 			active_pipes |= BIT(crtc->pipe);
11532 
11533 		drm_dbg_kms(&dev_priv->drm,
11534 			    "[CRTC:%d:%s] hw state readout: %s\n",
11535 			    crtc->base.base.id, crtc->base.name,
11536 			    enableddisabled(crtc_state->hw.active));
11537 	}
11538 
11539 	cdclk_state->active_pipes = dbuf_state->active_pipes = active_pipes;
11540 
11541 	readout_plane_state(dev_priv);
11542 
11543 	for_each_intel_encoder(dev, encoder) {
11544 		struct intel_crtc_state *crtc_state = NULL;
11545 
11546 		pipe = 0;
11547 
11548 		if (encoder->get_hw_state(encoder, &pipe)) {
11549 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
11550 			crtc_state = to_intel_crtc_state(crtc->base.state);
11551 
11552 			encoder->base.crtc = &crtc->base;
11553 			intel_encoder_get_config(encoder, crtc_state);
11554 
11555 			/* read out to slave crtc as well for bigjoiner */
11556 			if (crtc_state->bigjoiner) {
11557 				/* encoder should read be linked to bigjoiner master */
11558 				WARN_ON(crtc_state->bigjoiner_slave);
11559 
11560 				crtc = crtc_state->bigjoiner_linked_crtc;
11561 				crtc_state = to_intel_crtc_state(crtc->base.state);
11562 				intel_encoder_get_config(encoder, crtc_state);
11563 			}
11564 		} else {
11565 			encoder->base.crtc = NULL;
11566 		}
11567 
11568 		if (encoder->sync_state)
11569 			encoder->sync_state(encoder, crtc_state);
11570 
11571 		drm_dbg_kms(&dev_priv->drm,
11572 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
11573 			    encoder->base.base.id, encoder->base.name,
11574 			    enableddisabled(encoder->base.crtc),
11575 			    pipe_name(pipe));
11576 	}
11577 
11578 	intel_dpll_readout_hw_state(dev_priv);
11579 
11580 	drm_connector_list_iter_begin(dev, &conn_iter);
11581 	for_each_intel_connector_iter(connector, &conn_iter) {
11582 		if (connector->get_hw_state(connector)) {
11583 			struct intel_crtc_state *crtc_state;
11584 			struct intel_crtc *crtc;
11585 
11586 			connector->base.dpms = DRM_MODE_DPMS_ON;
11587 
11588 			encoder = intel_attached_encoder(connector);
11589 			connector->base.encoder = &encoder->base;
11590 
11591 			crtc = to_intel_crtc(encoder->base.crtc);
11592 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
11593 
11594 			if (crtc_state && crtc_state->hw.active) {
11595 				/*
11596 				 * This has to be done during hardware readout
11597 				 * because anything calling .crtc_disable may
11598 				 * rely on the connector_mask being accurate.
11599 				 */
11600 				crtc_state->uapi.connector_mask |=
11601 					drm_connector_mask(&connector->base);
11602 				crtc_state->uapi.encoder_mask |=
11603 					drm_encoder_mask(&encoder->base);
11604 			}
11605 		} else {
11606 			connector->base.dpms = DRM_MODE_DPMS_OFF;
11607 			connector->base.encoder = NULL;
11608 		}
11609 		drm_dbg_kms(&dev_priv->drm,
11610 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
11611 			    connector->base.base.id, connector->base.name,
11612 			    enableddisabled(connector->base.encoder));
11613 	}
11614 	drm_connector_list_iter_end(&conn_iter);
11615 
11616 	for_each_intel_crtc(dev, crtc) {
11617 		struct intel_bw_state *bw_state =
11618 			to_intel_bw_state(dev_priv->bw_obj.state);
11619 		struct intel_crtc_state *crtc_state =
11620 			to_intel_crtc_state(crtc->base.state);
11621 		struct intel_plane *plane;
11622 		int min_cdclk = 0;
11623 
11624 		if (crtc_state->bigjoiner_slave)
11625 			continue;
11626 
11627 		if (crtc_state->hw.active) {
11628 			/*
11629 			 * The initial mode needs to be set in order to keep
11630 			 * the atomic core happy. It wants a valid mode if the
11631 			 * crtc's enabled, so we do the above call.
11632 			 *
11633 			 * But we don't set all the derived state fully, hence
11634 			 * set a flag to indicate that a full recalculation is
11635 			 * needed on the next commit.
11636 			 */
11637 			crtc_state->inherited = true;
11638 
11639 			intel_crtc_update_active_timings(crtc_state);
11640 
11641 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
11642 		}
11643 
11644 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
11645 			const struct intel_plane_state *plane_state =
11646 				to_intel_plane_state(plane->base.state);
11647 
11648 			/*
11649 			 * FIXME don't have the fb yet, so can't
11650 			 * use intel_plane_data_rate() :(
11651 			 */
11652 			if (plane_state->uapi.visible)
11653 				crtc_state->data_rate[plane->id] =
11654 					4 * crtc_state->pixel_rate;
11655 			/*
11656 			 * FIXME don't have the fb yet, so can't
11657 			 * use plane->min_cdclk() :(
11658 			 */
11659 			if (plane_state->uapi.visible && plane->min_cdclk) {
11660 				if (crtc_state->double_wide || DISPLAY_VER(dev_priv) >= 10)
11661 					crtc_state->min_cdclk[plane->id] =
11662 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
11663 				else
11664 					crtc_state->min_cdclk[plane->id] =
11665 						crtc_state->pixel_rate;
11666 			}
11667 			drm_dbg_kms(&dev_priv->drm,
11668 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
11669 				    plane->base.base.id, plane->base.name,
11670 				    crtc_state->min_cdclk[plane->id]);
11671 		}
11672 
11673 		if (crtc_state->hw.active) {
11674 			min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
11675 			if (drm_WARN_ON(dev, min_cdclk < 0))
11676 				min_cdclk = 0;
11677 		}
11678 
11679 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
11680 		cdclk_state->min_voltage_level[crtc->pipe] =
11681 			crtc_state->min_voltage_level;
11682 
11683 		intel_bw_crtc_update(bw_state, crtc_state);
11684 
11685 		intel_pipe_config_sanity_check(dev_priv, crtc_state);
11686 
11687 		/* discard our incomplete slave state, copy it from master */
11688 		if (crtc_state->bigjoiner && crtc_state->hw.active) {
11689 			struct intel_crtc *slave = crtc_state->bigjoiner_linked_crtc;
11690 			struct intel_crtc_state *slave_crtc_state =
11691 				to_intel_crtc_state(slave->base.state);
11692 
11693 			copy_bigjoiner_crtc_state(slave_crtc_state, crtc_state);
11694 			slave->base.mode = crtc->base.mode;
11695 
11696 			cdclk_state->min_cdclk[slave->pipe] = min_cdclk;
11697 			cdclk_state->min_voltage_level[slave->pipe] =
11698 				crtc_state->min_voltage_level;
11699 
11700 			for_each_intel_plane_on_crtc(&dev_priv->drm, slave, plane) {
11701 				const struct intel_plane_state *plane_state =
11702 					to_intel_plane_state(plane->base.state);
11703 
11704 				/*
11705 				 * FIXME don't have the fb yet, so can't
11706 				 * use intel_plane_data_rate() :(
11707 				 */
11708 				if (plane_state->uapi.visible)
11709 					crtc_state->data_rate[plane->id] =
11710 						4 * crtc_state->pixel_rate;
11711 				else
11712 					crtc_state->data_rate[plane->id] = 0;
11713 			}
11714 
11715 			intel_bw_crtc_update(bw_state, slave_crtc_state);
11716 			drm_calc_timestamping_constants(&slave->base,
11717 							&slave_crtc_state->hw.adjusted_mode);
11718 		}
11719 	}
11720 }
11721 
11722 static void
11723 get_encoder_power_domains(struct drm_i915_private *dev_priv)
11724 {
11725 	struct intel_encoder *encoder;
11726 
11727 	for_each_intel_encoder(&dev_priv->drm, encoder) {
11728 		struct intel_crtc_state *crtc_state;
11729 
11730 		if (!encoder->get_power_domains)
11731 			continue;
11732 
11733 		/*
11734 		 * MST-primary and inactive encoders don't have a crtc state
11735 		 * and neither of these require any power domain references.
11736 		 */
11737 		if (!encoder->base.crtc)
11738 			continue;
11739 
11740 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
11741 		encoder->get_power_domains(encoder, crtc_state);
11742 	}
11743 }
11744 
11745 static void intel_early_display_was(struct drm_i915_private *dev_priv)
11746 {
11747 	/*
11748 	 * Display WA #1185 WaDisableDARBFClkGating:glk,icl,ehl,tgl
11749 	 * Also known as Wa_14010480278.
11750 	 */
11751 	if (IS_DISPLAY_VER(dev_priv, 10, 12))
11752 		intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
11753 			       intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
11754 
11755 	if (IS_HASWELL(dev_priv)) {
11756 		/*
11757 		 * WaRsPkgCStateDisplayPMReq:hsw
11758 		 * System hang if this isn't done before disabling all planes!
11759 		 */
11760 		intel_de_write(dev_priv, CHICKEN_PAR1_1,
11761 			       intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
11762 	}
11763 
11764 	if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
11765 		/* Display WA #1142:kbl,cfl,cml */
11766 		intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
11767 			     KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
11768 		intel_de_rmw(dev_priv, CHICKEN_MISC_2,
11769 			     KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
11770 			     KBL_ARB_FILL_SPARE_14);
11771 	}
11772 }
11773 
11774 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
11775 				       enum port port, i915_reg_t hdmi_reg)
11776 {
11777 	u32 val = intel_de_read(dev_priv, hdmi_reg);
11778 
11779 	if (val & SDVO_ENABLE ||
11780 	    (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
11781 		return;
11782 
11783 	drm_dbg_kms(&dev_priv->drm,
11784 		    "Sanitizing transcoder select for HDMI %c\n",
11785 		    port_name(port));
11786 
11787 	val &= ~SDVO_PIPE_SEL_MASK;
11788 	val |= SDVO_PIPE_SEL(PIPE_A);
11789 
11790 	intel_de_write(dev_priv, hdmi_reg, val);
11791 }
11792 
11793 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
11794 				     enum port port, i915_reg_t dp_reg)
11795 {
11796 	u32 val = intel_de_read(dev_priv, dp_reg);
11797 
11798 	if (val & DP_PORT_EN ||
11799 	    (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
11800 		return;
11801 
11802 	drm_dbg_kms(&dev_priv->drm,
11803 		    "Sanitizing transcoder select for DP %c\n",
11804 		    port_name(port));
11805 
11806 	val &= ~DP_PIPE_SEL_MASK;
11807 	val |= DP_PIPE_SEL(PIPE_A);
11808 
11809 	intel_de_write(dev_priv, dp_reg, val);
11810 }
11811 
11812 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
11813 {
11814 	/*
11815 	 * The BIOS may select transcoder B on some of the PCH
11816 	 * ports even it doesn't enable the port. This would trip
11817 	 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
11818 	 * Sanitize the transcoder select bits to prevent that. We
11819 	 * assume that the BIOS never actually enabled the port,
11820 	 * because if it did we'd actually have to toggle the port
11821 	 * on and back off to make the transcoder A select stick
11822 	 * (see. intel_dp_link_down(), intel_disable_hdmi(),
11823 	 * intel_disable_sdvo()).
11824 	 */
11825 	ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
11826 	ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
11827 	ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
11828 
11829 	/* PCH SDVOB multiplex with HDMIB */
11830 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
11831 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
11832 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
11833 }
11834 
11835 /* Scan out the current hw modeset state,
11836  * and sanitizes it to the current state
11837  */
11838 static void
11839 intel_modeset_setup_hw_state(struct drm_device *dev,
11840 			     struct drm_modeset_acquire_ctx *ctx)
11841 {
11842 	struct drm_i915_private *dev_priv = to_i915(dev);
11843 	struct intel_encoder *encoder;
11844 	struct intel_crtc *crtc;
11845 	intel_wakeref_t wakeref;
11846 
11847 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
11848 
11849 	intel_early_display_was(dev_priv);
11850 	intel_modeset_readout_hw_state(dev);
11851 
11852 	/* HW state is read out, now we need to sanitize this mess. */
11853 	get_encoder_power_domains(dev_priv);
11854 
11855 	if (HAS_PCH_IBX(dev_priv))
11856 		ibx_sanitize_pch_ports(dev_priv);
11857 
11858 	/*
11859 	 * intel_sanitize_plane_mapping() may need to do vblank
11860 	 * waits, so we need vblank interrupts restored beforehand.
11861 	 */
11862 	for_each_intel_crtc(&dev_priv->drm, crtc) {
11863 		struct intel_crtc_state *crtc_state =
11864 			to_intel_crtc_state(crtc->base.state);
11865 
11866 		drm_crtc_vblank_reset(&crtc->base);
11867 
11868 		if (crtc_state->hw.active)
11869 			intel_crtc_vblank_on(crtc_state);
11870 	}
11871 
11872 	intel_sanitize_plane_mapping(dev_priv);
11873 
11874 	for_each_intel_encoder(dev, encoder)
11875 		intel_sanitize_encoder(encoder);
11876 
11877 	for_each_intel_crtc(&dev_priv->drm, crtc) {
11878 		struct intel_crtc_state *crtc_state =
11879 			to_intel_crtc_state(crtc->base.state);
11880 
11881 		intel_sanitize_crtc(crtc, ctx);
11882 		intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
11883 	}
11884 
11885 	intel_modeset_update_connector_atomic_state(dev);
11886 
11887 	intel_dpll_sanitize_state(dev_priv);
11888 
11889 	if (IS_G4X(dev_priv)) {
11890 		g4x_wm_get_hw_state(dev_priv);
11891 		g4x_wm_sanitize(dev_priv);
11892 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
11893 		vlv_wm_get_hw_state(dev_priv);
11894 		vlv_wm_sanitize(dev_priv);
11895 	} else if (DISPLAY_VER(dev_priv) >= 9) {
11896 		skl_wm_get_hw_state(dev_priv);
11897 	} else if (HAS_PCH_SPLIT(dev_priv)) {
11898 		ilk_wm_get_hw_state(dev_priv);
11899 	}
11900 
11901 	for_each_intel_crtc(dev, crtc) {
11902 		struct intel_crtc_state *crtc_state =
11903 			to_intel_crtc_state(crtc->base.state);
11904 		u64 put_domains;
11905 
11906 		put_domains = modeset_get_crtc_power_domains(crtc_state);
11907 		if (drm_WARN_ON(dev, put_domains))
11908 			modeset_put_crtc_power_domains(crtc, put_domains);
11909 	}
11910 
11911 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
11912 }
11913 
11914 void intel_display_resume(struct drm_device *dev)
11915 {
11916 	struct drm_i915_private *dev_priv = to_i915(dev);
11917 	struct drm_atomic_state *state = dev_priv->modeset_restore_state;
11918 	struct drm_modeset_acquire_ctx ctx;
11919 	int ret;
11920 
11921 	if (!HAS_DISPLAY(dev_priv))
11922 		return;
11923 
11924 	dev_priv->modeset_restore_state = NULL;
11925 	if (state)
11926 		state->acquire_ctx = &ctx;
11927 
11928 	drm_modeset_acquire_init(&ctx, 0);
11929 
11930 	while (1) {
11931 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
11932 		if (ret != -EDEADLK)
11933 			break;
11934 
11935 		drm_modeset_backoff(&ctx);
11936 	}
11937 
11938 	if (!ret)
11939 		ret = __intel_display_resume(dev, state, &ctx);
11940 
11941 	intel_enable_ipc(dev_priv);
11942 	drm_modeset_drop_locks(&ctx);
11943 	drm_modeset_acquire_fini(&ctx);
11944 
11945 	if (ret)
11946 		drm_err(&dev_priv->drm,
11947 			"Restoring old state failed with %i\n", ret);
11948 	if (state)
11949 		drm_atomic_state_put(state);
11950 }
11951 
11952 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
11953 {
11954 	struct intel_connector *connector;
11955 	struct drm_connector_list_iter conn_iter;
11956 
11957 	/* Kill all the work that may have been queued by hpd. */
11958 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
11959 	for_each_intel_connector_iter(connector, &conn_iter) {
11960 		if (connector->modeset_retry_work.func)
11961 			cancel_work_sync(&connector->modeset_retry_work);
11962 		if (connector->hdcp.shim) {
11963 			cancel_delayed_work_sync(&connector->hdcp.check_work);
11964 			cancel_work_sync(&connector->hdcp.prop_work);
11965 		}
11966 	}
11967 	drm_connector_list_iter_end(&conn_iter);
11968 }
11969 
11970 /* part #1: call before irq uninstall */
11971 void intel_modeset_driver_remove(struct drm_i915_private *i915)
11972 {
11973 	if (!HAS_DISPLAY(i915))
11974 		return;
11975 
11976 	flush_workqueue(i915->flip_wq);
11977 	flush_workqueue(i915->modeset_wq);
11978 
11979 	flush_work(&i915->atomic_helper.free_work);
11980 	drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
11981 }
11982 
11983 /* part #2: call after irq uninstall */
11984 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
11985 {
11986 	if (!HAS_DISPLAY(i915))
11987 		return;
11988 
11989 	/*
11990 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
11991 	 * poll handlers. Hence disable polling after hpd handling is shut down.
11992 	 */
11993 	intel_hpd_poll_fini(i915);
11994 
11995 	/*
11996 	 * MST topology needs to be suspended so we don't have any calls to
11997 	 * fbdev after it's finalized. MST will be destroyed later as part of
11998 	 * drm_mode_config_cleanup()
11999 	 */
12000 	intel_dp_mst_suspend(i915);
12001 
12002 	/* poll work can call into fbdev, hence clean that up afterwards */
12003 	intel_fbdev_fini(i915);
12004 
12005 	intel_unregister_dsm_handler();
12006 
12007 	intel_fbc_global_disable(i915);
12008 
12009 	/* flush any delayed tasks or pending work */
12010 	flush_scheduled_work();
12011 
12012 	intel_hdcp_component_fini(i915);
12013 
12014 	intel_mode_config_cleanup(i915);
12015 
12016 	intel_overlay_cleanup(i915);
12017 
12018 	intel_gmbus_teardown(i915);
12019 
12020 	destroy_workqueue(i915->flip_wq);
12021 	destroy_workqueue(i915->modeset_wq);
12022 
12023 	intel_fbc_cleanup_cfb(i915);
12024 }
12025 
12026 /* part #3: call after gem init */
12027 void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
12028 {
12029 	intel_dmc_ucode_fini(i915);
12030 
12031 	intel_power_domains_driver_remove(i915);
12032 
12033 	intel_vga_unregister(i915);
12034 
12035 	intel_bios_driver_remove(i915);
12036 }
12037 
12038 void intel_display_driver_register(struct drm_i915_private *i915)
12039 {
12040 	if (!HAS_DISPLAY(i915))
12041 		return;
12042 
12043 	intel_display_debugfs_register(i915);
12044 
12045 	/* Must be done after probing outputs */
12046 	intel_opregion_register(i915);
12047 	acpi_video_register();
12048 
12049 	intel_audio_init(i915);
12050 
12051 	/*
12052 	 * Some ports require correctly set-up hpd registers for
12053 	 * detection to work properly (leading to ghost connected
12054 	 * connector status), e.g. VGA on gm45.  Hence we can only set
12055 	 * up the initial fbdev config after hpd irqs are fully
12056 	 * enabled. We do it last so that the async config cannot run
12057 	 * before the connectors are registered.
12058 	 */
12059 	intel_fbdev_initial_config_async(&i915->drm);
12060 
12061 	/*
12062 	 * We need to coordinate the hotplugs with the asynchronous
12063 	 * fbdev configuration, for which we use the
12064 	 * fbdev->async_cookie.
12065 	 */
12066 	drm_kms_helper_poll_init(&i915->drm);
12067 }
12068 
12069 void intel_display_driver_unregister(struct drm_i915_private *i915)
12070 {
12071 	if (!HAS_DISPLAY(i915))
12072 		return;
12073 
12074 	intel_fbdev_unregister(i915);
12075 	intel_audio_deinit(i915);
12076 
12077 	/*
12078 	 * After flushing the fbdev (incl. a late async config which
12079 	 * will have delayed queuing of a hotplug event), then flush
12080 	 * the hotplug events.
12081 	 */
12082 	drm_kms_helper_poll_fini(&i915->drm);
12083 	drm_atomic_helper_shutdown(&i915->drm);
12084 
12085 	acpi_video_unregister();
12086 	intel_opregion_unregister(i915);
12087 }
12088