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