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