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