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