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