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->scaler_state = crtc_state->scaler_state;
5149 	saved_state->shared_dpll = crtc_state->shared_dpll;
5150 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
5151 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
5152 	       sizeof(saved_state->icl_port_dplls));
5153 	saved_state->crc_enabled = crtc_state->crc_enabled;
5154 	if (IS_G4X(dev_priv) ||
5155 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5156 		saved_state->wm = crtc_state->wm;
5157 
5158 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
5159 	kfree(saved_state);
5160 
5161 	intel_crtc_copy_uapi_to_hw_state_modeset(state, crtc);
5162 
5163 	return 0;
5164 }
5165 
5166 static int
5167 intel_modeset_pipe_config(struct intel_atomic_state *state,
5168 			  struct intel_crtc *crtc)
5169 {
5170 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
5171 	struct intel_crtc_state *crtc_state =
5172 		intel_atomic_get_new_crtc_state(state, crtc);
5173 	struct drm_connector *connector;
5174 	struct drm_connector_state *connector_state;
5175 	int pipe_src_w, pipe_src_h;
5176 	int base_bpp, ret, i;
5177 	bool retry = true;
5178 
5179 	crtc_state->cpu_transcoder = (enum transcoder) crtc->pipe;
5180 
5181 	crtc_state->framestart_delay = 1;
5182 
5183 	/*
5184 	 * Sanitize sync polarity flags based on requested ones. If neither
5185 	 * positive or negative polarity is requested, treat this as meaning
5186 	 * negative polarity.
5187 	 */
5188 	if (!(crtc_state->hw.adjusted_mode.flags &
5189 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
5190 		crtc_state->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
5191 
5192 	if (!(crtc_state->hw.adjusted_mode.flags &
5193 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
5194 		crtc_state->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
5195 
5196 	ret = compute_baseline_pipe_bpp(state, crtc);
5197 	if (ret)
5198 		return ret;
5199 
5200 	base_bpp = crtc_state->pipe_bpp;
5201 
5202 	/*
5203 	 * Determine the real pipe dimensions. Note that stereo modes can
5204 	 * increase the actual pipe size due to the frame doubling and
5205 	 * insertion of additional space for blanks between the frame. This
5206 	 * is stored in the crtc timings. We use the requested mode to do this
5207 	 * computation to clearly distinguish it from the adjusted mode, which
5208 	 * can be changed by the connectors in the below retry loop.
5209 	 */
5210 	drm_mode_get_hv_timing(&crtc_state->hw.mode,
5211 			       &pipe_src_w, &pipe_src_h);
5212 	drm_rect_init(&crtc_state->pipe_src, 0, 0,
5213 		      pipe_src_w, pipe_src_h);
5214 
5215 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5216 		struct intel_encoder *encoder =
5217 			to_intel_encoder(connector_state->best_encoder);
5218 
5219 		if (connector_state->crtc != &crtc->base)
5220 			continue;
5221 
5222 		if (!check_single_encoder_cloning(state, crtc, encoder)) {
5223 			drm_dbg_kms(&i915->drm,
5224 				    "[ENCODER:%d:%s] rejecting invalid cloning configuration\n",
5225 				    encoder->base.base.id, encoder->base.name);
5226 			return -EINVAL;
5227 		}
5228 
5229 		/*
5230 		 * Determine output_types before calling the .compute_config()
5231 		 * hooks so that the hooks can use this information safely.
5232 		 */
5233 		if (encoder->compute_output_type)
5234 			crtc_state->output_types |=
5235 				BIT(encoder->compute_output_type(encoder, crtc_state,
5236 								 connector_state));
5237 		else
5238 			crtc_state->output_types |= BIT(encoder->type);
5239 	}
5240 
5241 encoder_retry:
5242 	/* Ensure the port clock defaults are reset when retrying. */
5243 	crtc_state->port_clock = 0;
5244 	crtc_state->pixel_multiplier = 1;
5245 
5246 	/* Fill in default crtc timings, allow encoders to overwrite them. */
5247 	drm_mode_set_crtcinfo(&crtc_state->hw.adjusted_mode,
5248 			      CRTC_STEREO_DOUBLE);
5249 
5250 	/* Pass our mode to the connectors and the CRTC to give them a chance to
5251 	 * adjust it according to limitations or connector properties, and also
5252 	 * a chance to reject the mode entirely.
5253 	 */
5254 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
5255 		struct intel_encoder *encoder =
5256 			to_intel_encoder(connector_state->best_encoder);
5257 
5258 		if (connector_state->crtc != &crtc->base)
5259 			continue;
5260 
5261 		ret = encoder->compute_config(encoder, crtc_state,
5262 					      connector_state);
5263 		if (ret == -EDEADLK)
5264 			return ret;
5265 		if (ret < 0) {
5266 			drm_dbg_kms(&i915->drm, "[ENCODER:%d:%s] config failure: %d\n",
5267 				    encoder->base.base.id, encoder->base.name, ret);
5268 			return ret;
5269 		}
5270 	}
5271 
5272 	/* Set default port clock if not overwritten by the encoder. Needs to be
5273 	 * done afterwards in case the encoder adjusts the mode. */
5274 	if (!crtc_state->port_clock)
5275 		crtc_state->port_clock = crtc_state->hw.adjusted_mode.crtc_clock
5276 			* crtc_state->pixel_multiplier;
5277 
5278 	ret = intel_crtc_compute_config(state, crtc);
5279 	if (ret == -EDEADLK)
5280 		return ret;
5281 	if (ret == -EAGAIN) {
5282 		if (drm_WARN(&i915->drm, !retry,
5283 			     "[CRTC:%d:%s] loop in pipe configuration computation\n",
5284 			     crtc->base.base.id, crtc->base.name))
5285 			return -EINVAL;
5286 
5287 		drm_dbg_kms(&i915->drm, "[CRTC:%d:%s] bw constrained, retrying\n",
5288 			    crtc->base.base.id, crtc->base.name);
5289 		retry = false;
5290 		goto encoder_retry;
5291 	}
5292 	if (ret < 0) {
5293 		drm_dbg_kms(&i915->drm, "[CRTC:%d:%s] config failure: %d\n",
5294 			    crtc->base.base.id, crtc->base.name, ret);
5295 		return ret;
5296 	}
5297 
5298 	/* Dithering seems to not pass-through bits correctly when it should, so
5299 	 * only enable it on 6bpc panels and when its not a compliance
5300 	 * test requesting 6bpc video pattern.
5301 	 */
5302 	crtc_state->dither = (crtc_state->pipe_bpp == 6*3) &&
5303 		!crtc_state->dither_force_disable;
5304 	drm_dbg_kms(&i915->drm,
5305 		    "[CRTC:%d:%s] hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
5306 		    crtc->base.base.id, crtc->base.name,
5307 		    base_bpp, crtc_state->pipe_bpp, crtc_state->dither);
5308 
5309 	return 0;
5310 }
5311 
5312 static int
5313 intel_modeset_pipe_config_late(struct intel_atomic_state *state,
5314 			       struct intel_crtc *crtc)
5315 {
5316 	struct intel_crtc_state *crtc_state =
5317 		intel_atomic_get_new_crtc_state(state, crtc);
5318 	struct drm_connector_state *conn_state;
5319 	struct drm_connector *connector;
5320 	int i;
5321 
5322 	intel_bigjoiner_adjust_pipe_src(crtc_state);
5323 
5324 	for_each_new_connector_in_state(&state->base, connector,
5325 					conn_state, i) {
5326 		struct intel_encoder *encoder =
5327 			to_intel_encoder(conn_state->best_encoder);
5328 		int ret;
5329 
5330 		if (conn_state->crtc != &crtc->base ||
5331 		    !encoder->compute_config_late)
5332 			continue;
5333 
5334 		ret = encoder->compute_config_late(encoder, crtc_state,
5335 						   conn_state);
5336 		if (ret)
5337 			return ret;
5338 	}
5339 
5340 	return 0;
5341 }
5342 
5343 bool intel_fuzzy_clock_check(int clock1, int clock2)
5344 {
5345 	int diff;
5346 
5347 	if (clock1 == clock2)
5348 		return true;
5349 
5350 	if (!clock1 || !clock2)
5351 		return false;
5352 
5353 	diff = abs(clock1 - clock2);
5354 
5355 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
5356 		return true;
5357 
5358 	return false;
5359 }
5360 
5361 static bool
5362 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
5363 		       const struct intel_link_m_n *m2_n2)
5364 {
5365 	return m_n->tu == m2_n2->tu &&
5366 		m_n->data_m == m2_n2->data_m &&
5367 		m_n->data_n == m2_n2->data_n &&
5368 		m_n->link_m == m2_n2->link_m &&
5369 		m_n->link_n == m2_n2->link_n;
5370 }
5371 
5372 static bool
5373 intel_compare_infoframe(const union hdmi_infoframe *a,
5374 			const union hdmi_infoframe *b)
5375 {
5376 	return memcmp(a, b, sizeof(*a)) == 0;
5377 }
5378 
5379 static bool
5380 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
5381 			 const struct drm_dp_vsc_sdp *b)
5382 {
5383 	return memcmp(a, b, sizeof(*a)) == 0;
5384 }
5385 
5386 static bool
5387 intel_compare_buffer(const u8 *a, const u8 *b, size_t len)
5388 {
5389 	return memcmp(a, b, len) == 0;
5390 }
5391 
5392 static void
5393 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
5394 			       bool fastset, const char *name,
5395 			       const union hdmi_infoframe *a,
5396 			       const union hdmi_infoframe *b)
5397 {
5398 	if (fastset) {
5399 		if (!drm_debug_enabled(DRM_UT_KMS))
5400 			return;
5401 
5402 		drm_dbg_kms(&dev_priv->drm,
5403 			    "fastset mismatch in %s infoframe\n", name);
5404 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
5405 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
5406 		drm_dbg_kms(&dev_priv->drm, "found:\n");
5407 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
5408 	} else {
5409 		drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
5410 		drm_err(&dev_priv->drm, "expected:\n");
5411 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
5412 		drm_err(&dev_priv->drm, "found:\n");
5413 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
5414 	}
5415 }
5416 
5417 static void
5418 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
5419 				bool fastset, const char *name,
5420 				const struct drm_dp_vsc_sdp *a,
5421 				const struct drm_dp_vsc_sdp *b)
5422 {
5423 	if (fastset) {
5424 		if (!drm_debug_enabled(DRM_UT_KMS))
5425 			return;
5426 
5427 		drm_dbg_kms(&dev_priv->drm,
5428 			    "fastset mismatch in %s dp sdp\n", name);
5429 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
5430 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
5431 		drm_dbg_kms(&dev_priv->drm, "found:\n");
5432 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
5433 	} else {
5434 		drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
5435 		drm_err(&dev_priv->drm, "expected:\n");
5436 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
5437 		drm_err(&dev_priv->drm, "found:\n");
5438 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
5439 	}
5440 }
5441 
5442 static void
5443 pipe_config_buffer_mismatch(struct drm_i915_private *dev_priv,
5444 			    bool fastset, const char *name,
5445 			    const u8 *a, const u8 *b, size_t len)
5446 {
5447 	if (fastset) {
5448 		if (!drm_debug_enabled(DRM_UT_KMS))
5449 			return;
5450 
5451 		drm_dbg_kms(&dev_priv->drm,
5452 			    "fastset mismatch in %s buffer\n", name);
5453 		print_hex_dump(KERN_DEBUG, "expected: ", DUMP_PREFIX_NONE,
5454 			       16, 0, a, len, false);
5455 		print_hex_dump(KERN_DEBUG, "found: ", DUMP_PREFIX_NONE,
5456 			       16, 0, b, len, false);
5457 	} else {
5458 		drm_err(&dev_priv->drm, "mismatch in %s buffer\n", name);
5459 		print_hex_dump(KERN_ERR, "expected: ", DUMP_PREFIX_NONE,
5460 			       16, 0, a, len, false);
5461 		print_hex_dump(KERN_ERR, "found: ", DUMP_PREFIX_NONE,
5462 			       16, 0, b, len, false);
5463 	}
5464 }
5465 
5466 static void __printf(4, 5)
5467 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
5468 		     const char *name, const char *format, ...)
5469 {
5470 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
5471 	struct va_format vaf;
5472 	va_list args;
5473 
5474 	va_start(args, format);
5475 	vaf.fmt = format;
5476 	vaf.va = &args;
5477 
5478 	if (fastset)
5479 		drm_dbg_kms(&i915->drm,
5480 			    "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
5481 			    crtc->base.base.id, crtc->base.name, name, &vaf);
5482 	else
5483 		drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
5484 			crtc->base.base.id, crtc->base.name, name, &vaf);
5485 
5486 	va_end(args);
5487 }
5488 
5489 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
5490 {
5491 	if (dev_priv->params.fastboot != -1)
5492 		return dev_priv->params.fastboot;
5493 
5494 	/* Enable fastboot by default on Skylake and newer */
5495 	if (DISPLAY_VER(dev_priv) >= 9)
5496 		return true;
5497 
5498 	/* Enable fastboot by default on VLV and CHV */
5499 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5500 		return true;
5501 
5502 	/* Disabled by default on all others */
5503 	return false;
5504 }
5505 
5506 bool
5507 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
5508 			  const struct intel_crtc_state *pipe_config,
5509 			  bool fastset)
5510 {
5511 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
5512 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
5513 	bool ret = true;
5514 	bool fixup_inherited = fastset &&
5515 		current_config->inherited && !pipe_config->inherited;
5516 
5517 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
5518 		drm_dbg_kms(&dev_priv->drm,
5519 			    "initial modeset and fastboot not set\n");
5520 		ret = false;
5521 	}
5522 
5523 #define PIPE_CONF_CHECK_X(name) do { \
5524 	if (current_config->name != pipe_config->name) { \
5525 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
5526 				     "(expected 0x%08x, found 0x%08x)", \
5527 				     current_config->name, \
5528 				     pipe_config->name); \
5529 		ret = false; \
5530 	} \
5531 } while (0)
5532 
5533 #define PIPE_CONF_CHECK_X_WITH_MASK(name, mask) do { \
5534 	if ((current_config->name & (mask)) != (pipe_config->name & (mask))) { \
5535 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
5536 				     "(expected 0x%08x, found 0x%08x)", \
5537 				     current_config->name & (mask), \
5538 				     pipe_config->name & (mask)); \
5539 		ret = false; \
5540 	} \
5541 } while (0)
5542 
5543 #define PIPE_CONF_CHECK_I(name) do { \
5544 	if (current_config->name != pipe_config->name) { \
5545 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
5546 				     "(expected %i, found %i)", \
5547 				     current_config->name, \
5548 				     pipe_config->name); \
5549 		ret = false; \
5550 	} \
5551 } while (0)
5552 
5553 #define PIPE_CONF_CHECK_BOOL(name) do { \
5554 	if (current_config->name != pipe_config->name) { \
5555 		pipe_config_mismatch(fastset, crtc,  __stringify(name), \
5556 				     "(expected %s, found %s)", \
5557 				     str_yes_no(current_config->name), \
5558 				     str_yes_no(pipe_config->name)); \
5559 		ret = false; \
5560 	} \
5561 } while (0)
5562 
5563 /*
5564  * Checks state where we only read out the enabling, but not the entire
5565  * state itself (like full infoframes or ELD for audio). These states
5566  * require a full modeset on bootup to fix up.
5567  */
5568 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
5569 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
5570 		PIPE_CONF_CHECK_BOOL(name); \
5571 	} else { \
5572 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
5573 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
5574 				     str_yes_no(current_config->name), \
5575 				     str_yes_no(pipe_config->name)); \
5576 		ret = false; \
5577 	} \
5578 } while (0)
5579 
5580 #define PIPE_CONF_CHECK_P(name) do { \
5581 	if (current_config->name != pipe_config->name) { \
5582 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
5583 				     "(expected %p, found %p)", \
5584 				     current_config->name, \
5585 				     pipe_config->name); \
5586 		ret = false; \
5587 	} \
5588 } while (0)
5589 
5590 #define PIPE_CONF_CHECK_M_N(name) do { \
5591 	if (!intel_compare_link_m_n(&current_config->name, \
5592 				    &pipe_config->name)) { \
5593 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
5594 				     "(expected tu %i data %i/%i link %i/%i, " \
5595 				     "found tu %i, data %i/%i link %i/%i)", \
5596 				     current_config->name.tu, \
5597 				     current_config->name.data_m, \
5598 				     current_config->name.data_n, \
5599 				     current_config->name.link_m, \
5600 				     current_config->name.link_n, \
5601 				     pipe_config->name.tu, \
5602 				     pipe_config->name.data_m, \
5603 				     pipe_config->name.data_n, \
5604 				     pipe_config->name.link_m, \
5605 				     pipe_config->name.link_n); \
5606 		ret = false; \
5607 	} \
5608 } while (0)
5609 
5610 #define PIPE_CONF_CHECK_TIMINGS(name) do { \
5611 	PIPE_CONF_CHECK_I(name.crtc_hdisplay); \
5612 	PIPE_CONF_CHECK_I(name.crtc_htotal); \
5613 	PIPE_CONF_CHECK_I(name.crtc_hblank_start); \
5614 	PIPE_CONF_CHECK_I(name.crtc_hblank_end); \
5615 	PIPE_CONF_CHECK_I(name.crtc_hsync_start); \
5616 	PIPE_CONF_CHECK_I(name.crtc_hsync_end); \
5617 	PIPE_CONF_CHECK_I(name.crtc_vdisplay); \
5618 	PIPE_CONF_CHECK_I(name.crtc_vtotal); \
5619 	PIPE_CONF_CHECK_I(name.crtc_vblank_start); \
5620 	PIPE_CONF_CHECK_I(name.crtc_vblank_end); \
5621 	PIPE_CONF_CHECK_I(name.crtc_vsync_start); \
5622 	PIPE_CONF_CHECK_I(name.crtc_vsync_end); \
5623 } while (0)
5624 
5625 #define PIPE_CONF_CHECK_RECT(name) do { \
5626 	PIPE_CONF_CHECK_I(name.x1); \
5627 	PIPE_CONF_CHECK_I(name.x2); \
5628 	PIPE_CONF_CHECK_I(name.y1); \
5629 	PIPE_CONF_CHECK_I(name.y2); \
5630 } while (0)
5631 
5632 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
5633 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
5634 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
5635 				     "(%x) (expected %i, found %i)", \
5636 				     (mask), \
5637 				     current_config->name & (mask), \
5638 				     pipe_config->name & (mask)); \
5639 		ret = false; \
5640 	} \
5641 } while (0)
5642 
5643 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
5644 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
5645 				     &pipe_config->infoframes.name)) { \
5646 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
5647 					       &current_config->infoframes.name, \
5648 					       &pipe_config->infoframes.name); \
5649 		ret = false; \
5650 	} \
5651 } while (0)
5652 
5653 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
5654 	if (!current_config->has_psr && !pipe_config->has_psr && \
5655 	    !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
5656 				      &pipe_config->infoframes.name)) { \
5657 		pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
5658 						&current_config->infoframes.name, \
5659 						&pipe_config->infoframes.name); \
5660 		ret = false; \
5661 	} \
5662 } while (0)
5663 
5664 #define PIPE_CONF_CHECK_BUFFER(name, len) do { \
5665 	BUILD_BUG_ON(sizeof(current_config->name) != (len)); \
5666 	BUILD_BUG_ON(sizeof(pipe_config->name) != (len)); \
5667 	if (!intel_compare_buffer(current_config->name, pipe_config->name, (len))) { \
5668 		pipe_config_buffer_mismatch(dev_priv, fastset, __stringify(name), \
5669 					    current_config->name, \
5670 					    pipe_config->name, \
5671 					    (len)); \
5672 		ret = false; \
5673 	} \
5674 } while (0)
5675 
5676 #define PIPE_CONF_CHECK_COLOR_LUT(lut, is_pre_csc_lut) do { \
5677 	if (current_config->gamma_mode == pipe_config->gamma_mode && \
5678 	    !intel_color_lut_equal(current_config, \
5679 				   current_config->lut, pipe_config->lut, \
5680 				   is_pre_csc_lut)) {	\
5681 		pipe_config_mismatch(fastset, crtc, __stringify(lut), \
5682 				     "hw_state doesn't match sw_state"); \
5683 		ret = false; \
5684 	} \
5685 } while (0)
5686 
5687 #define PIPE_CONF_QUIRK(quirk) \
5688 	((current_config->quirks | pipe_config->quirks) & (quirk))
5689 
5690 	PIPE_CONF_CHECK_I(hw.enable);
5691 	PIPE_CONF_CHECK_I(hw.active);
5692 
5693 	PIPE_CONF_CHECK_I(cpu_transcoder);
5694 	PIPE_CONF_CHECK_I(mst_master_transcoder);
5695 
5696 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
5697 	PIPE_CONF_CHECK_I(fdi_lanes);
5698 	PIPE_CONF_CHECK_M_N(fdi_m_n);
5699 
5700 	PIPE_CONF_CHECK_I(lane_count);
5701 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
5702 
5703 	if (HAS_DOUBLE_BUFFERED_M_N(dev_priv)) {
5704 		if (!fastset || !pipe_config->seamless_m_n)
5705 			PIPE_CONF_CHECK_M_N(dp_m_n);
5706 	} else {
5707 		PIPE_CONF_CHECK_M_N(dp_m_n);
5708 		PIPE_CONF_CHECK_M_N(dp_m2_n2);
5709 	}
5710 
5711 	PIPE_CONF_CHECK_X(output_types);
5712 
5713 	PIPE_CONF_CHECK_I(framestart_delay);
5714 	PIPE_CONF_CHECK_I(msa_timing_delay);
5715 
5716 	PIPE_CONF_CHECK_TIMINGS(hw.pipe_mode);
5717 	PIPE_CONF_CHECK_TIMINGS(hw.adjusted_mode);
5718 
5719 	PIPE_CONF_CHECK_I(pixel_multiplier);
5720 
5721 	PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5722 			      DRM_MODE_FLAG_INTERLACE);
5723 
5724 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
5725 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5726 				      DRM_MODE_FLAG_PHSYNC);
5727 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5728 				      DRM_MODE_FLAG_NHSYNC);
5729 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5730 				      DRM_MODE_FLAG_PVSYNC);
5731 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
5732 				      DRM_MODE_FLAG_NVSYNC);
5733 	}
5734 
5735 	PIPE_CONF_CHECK_I(output_format);
5736 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
5737 	if ((DISPLAY_VER(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
5738 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
5739 		PIPE_CONF_CHECK_BOOL(limited_color_range);
5740 
5741 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
5742 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
5743 	PIPE_CONF_CHECK_BOOL(has_infoframe);
5744 	PIPE_CONF_CHECK_BOOL(fec_enable);
5745 
5746 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
5747 	PIPE_CONF_CHECK_BUFFER(eld, MAX_ELD_BYTES);
5748 
5749 	PIPE_CONF_CHECK_X(gmch_pfit.control);
5750 	/* pfit ratios are autocomputed by the hw on gen4+ */
5751 	if (DISPLAY_VER(dev_priv) < 4)
5752 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
5753 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
5754 
5755 	/*
5756 	 * Changing the EDP transcoder input mux
5757 	 * (A_ONOFF vs. A_ON) requires a full modeset.
5758 	 */
5759 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
5760 
5761 	if (!fastset) {
5762 		PIPE_CONF_CHECK_RECT(pipe_src);
5763 
5764 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
5765 		PIPE_CONF_CHECK_RECT(pch_pfit.dst);
5766 
5767 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
5768 		PIPE_CONF_CHECK_I(pixel_rate);
5769 
5770 		PIPE_CONF_CHECK_X(gamma_mode);
5771 		if (IS_CHERRYVIEW(dev_priv))
5772 			PIPE_CONF_CHECK_X(cgm_mode);
5773 		else
5774 			PIPE_CONF_CHECK_X(csc_mode);
5775 		PIPE_CONF_CHECK_BOOL(gamma_enable);
5776 		PIPE_CONF_CHECK_BOOL(csc_enable);
5777 
5778 		PIPE_CONF_CHECK_I(linetime);
5779 		PIPE_CONF_CHECK_I(ips_linetime);
5780 
5781 		PIPE_CONF_CHECK_COLOR_LUT(pre_csc_lut, true);
5782 		PIPE_CONF_CHECK_COLOR_LUT(post_csc_lut, false);
5783 
5784 		if (current_config->active_planes) {
5785 			PIPE_CONF_CHECK_BOOL(has_psr);
5786 			PIPE_CONF_CHECK_BOOL(has_psr2);
5787 			PIPE_CONF_CHECK_BOOL(enable_psr2_sel_fetch);
5788 			PIPE_CONF_CHECK_I(dc3co_exitline);
5789 		}
5790 	}
5791 
5792 	PIPE_CONF_CHECK_BOOL(double_wide);
5793 
5794 	if (dev_priv->display.dpll.mgr) {
5795 		PIPE_CONF_CHECK_P(shared_dpll);
5796 
5797 		PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
5798 		PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
5799 		PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
5800 		PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
5801 		PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
5802 		PIPE_CONF_CHECK_X(dpll_hw_state.spll);
5803 		PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
5804 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
5805 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
5806 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
5807 		PIPE_CONF_CHECK_X(dpll_hw_state.div0);
5808 		PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
5809 		PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
5810 		PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
5811 		PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
5812 		PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
5813 		PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
5814 		PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
5815 		PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
5816 		PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
5817 		PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
5818 		PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
5819 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
5820 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
5821 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
5822 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
5823 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
5824 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
5825 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
5826 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
5827 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
5828 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
5829 	}
5830 
5831 	PIPE_CONF_CHECK_X(dsi_pll.ctrl);
5832 	PIPE_CONF_CHECK_X(dsi_pll.div);
5833 
5834 	if (IS_G4X(dev_priv) || DISPLAY_VER(dev_priv) >= 5)
5835 		PIPE_CONF_CHECK_I(pipe_bpp);
5836 
5837 	if (!fastset || !pipe_config->seamless_m_n) {
5838 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_clock);
5839 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_clock);
5840 	}
5841 	PIPE_CONF_CHECK_I(port_clock);
5842 
5843 	PIPE_CONF_CHECK_I(min_voltage_level);
5844 
5845 	if (current_config->has_psr || pipe_config->has_psr)
5846 		PIPE_CONF_CHECK_X_WITH_MASK(infoframes.enable,
5847 					    ~intel_hdmi_infoframe_enable(DP_SDP_VSC));
5848 	else
5849 		PIPE_CONF_CHECK_X(infoframes.enable);
5850 
5851 	PIPE_CONF_CHECK_X(infoframes.gcp);
5852 	PIPE_CONF_CHECK_INFOFRAME(avi);
5853 	PIPE_CONF_CHECK_INFOFRAME(spd);
5854 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
5855 	PIPE_CONF_CHECK_INFOFRAME(drm);
5856 	PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
5857 
5858 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
5859 	PIPE_CONF_CHECK_I(master_transcoder);
5860 	PIPE_CONF_CHECK_X(bigjoiner_pipes);
5861 
5862 	PIPE_CONF_CHECK_I(dsc.compression_enable);
5863 	PIPE_CONF_CHECK_I(dsc.dsc_split);
5864 	PIPE_CONF_CHECK_I(dsc.compressed_bpp);
5865 
5866 	PIPE_CONF_CHECK_BOOL(splitter.enable);
5867 	PIPE_CONF_CHECK_I(splitter.link_count);
5868 	PIPE_CONF_CHECK_I(splitter.pixel_overlap);
5869 
5870 	PIPE_CONF_CHECK_BOOL(vrr.enable);
5871 	PIPE_CONF_CHECK_I(vrr.vmin);
5872 	PIPE_CONF_CHECK_I(vrr.vmax);
5873 	PIPE_CONF_CHECK_I(vrr.flipline);
5874 	PIPE_CONF_CHECK_I(vrr.pipeline_full);
5875 	PIPE_CONF_CHECK_I(vrr.guardband);
5876 
5877 #undef PIPE_CONF_CHECK_X
5878 #undef PIPE_CONF_CHECK_I
5879 #undef PIPE_CONF_CHECK_BOOL
5880 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
5881 #undef PIPE_CONF_CHECK_P
5882 #undef PIPE_CONF_CHECK_FLAGS
5883 #undef PIPE_CONF_CHECK_COLOR_LUT
5884 #undef PIPE_CONF_CHECK_TIMINGS
5885 #undef PIPE_CONF_CHECK_RECT
5886 #undef PIPE_CONF_QUIRK
5887 
5888 	return ret;
5889 }
5890 
5891 static void
5892 intel_verify_planes(struct intel_atomic_state *state)
5893 {
5894 	struct intel_plane *plane;
5895 	const struct intel_plane_state *plane_state;
5896 	int i;
5897 
5898 	for_each_new_intel_plane_in_state(state, plane,
5899 					  plane_state, i)
5900 		assert_plane(plane, plane_state->planar_slave ||
5901 			     plane_state->uapi.visible);
5902 }
5903 
5904 int intel_modeset_all_pipes(struct intel_atomic_state *state,
5905 			    const char *reason)
5906 {
5907 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
5908 	struct intel_crtc *crtc;
5909 
5910 	/*
5911 	 * Add all pipes to the state, and force
5912 	 * a modeset on all the active ones.
5913 	 */
5914 	for_each_intel_crtc(&dev_priv->drm, crtc) {
5915 		struct intel_crtc_state *crtc_state;
5916 		int ret;
5917 
5918 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
5919 		if (IS_ERR(crtc_state))
5920 			return PTR_ERR(crtc_state);
5921 
5922 		if (!crtc_state->hw.active ||
5923 		    intel_crtc_needs_modeset(crtc_state))
5924 			continue;
5925 
5926 		drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] Full modeset due to %s\n",
5927 			    crtc->base.base.id, crtc->base.name, reason);
5928 
5929 		crtc_state->uapi.mode_changed = true;
5930 		crtc_state->update_pipe = false;
5931 
5932 		ret = drm_atomic_add_affected_connectors(&state->base,
5933 							 &crtc->base);
5934 		if (ret)
5935 			return ret;
5936 
5937 		ret = intel_dp_mst_add_topology_state_for_crtc(state, crtc);
5938 		if (ret)
5939 			return ret;
5940 
5941 		ret = intel_atomic_add_affected_planes(state, crtc);
5942 		if (ret)
5943 			return ret;
5944 
5945 		crtc_state->update_planes |= crtc_state->active_planes;
5946 	}
5947 
5948 	return 0;
5949 }
5950 
5951 void intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
5952 {
5953 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5954 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5955 	struct drm_display_mode adjusted_mode;
5956 
5957 	drm_mode_init(&adjusted_mode, &crtc_state->hw.adjusted_mode);
5958 
5959 	if (crtc_state->vrr.enable) {
5960 		adjusted_mode.crtc_vtotal = crtc_state->vrr.vmax;
5961 		adjusted_mode.crtc_vblank_end = crtc_state->vrr.vmax;
5962 		adjusted_mode.crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
5963 		crtc->vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
5964 	}
5965 
5966 	drm_calc_timestamping_constants(&crtc->base, &adjusted_mode);
5967 
5968 	crtc->mode_flags = crtc_state->mode_flags;
5969 
5970 	/*
5971 	 * The scanline counter increments at the leading edge of hsync.
5972 	 *
5973 	 * On most platforms it starts counting from vtotal-1 on the
5974 	 * first active line. That means the scanline counter value is
5975 	 * always one less than what we would expect. Ie. just after
5976 	 * start of vblank, which also occurs at start of hsync (on the
5977 	 * last active line), the scanline counter will read vblank_start-1.
5978 	 *
5979 	 * On gen2 the scanline counter starts counting from 1 instead
5980 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
5981 	 * to keep the value positive), instead of adding one.
5982 	 *
5983 	 * On HSW+ the behaviour of the scanline counter depends on the output
5984 	 * type. For DP ports it behaves like most other platforms, but on HDMI
5985 	 * there's an extra 1 line difference. So we need to add two instead of
5986 	 * one to the value.
5987 	 *
5988 	 * On VLV/CHV DSI the scanline counter would appear to increment
5989 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
5990 	 * that means we can't tell whether we're in vblank or not while
5991 	 * we're on that particular line. We must still set scanline_offset
5992 	 * to 1 so that the vblank timestamps come out correct when we query
5993 	 * the scanline counter from within the vblank interrupt handler.
5994 	 * However if queried just before the start of vblank we'll get an
5995 	 * answer that's slightly in the future.
5996 	 */
5997 	if (DISPLAY_VER(dev_priv) == 2) {
5998 		int vtotal;
5999 
6000 		vtotal = adjusted_mode.crtc_vtotal;
6001 		if (adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6002 			vtotal /= 2;
6003 
6004 		crtc->scanline_offset = vtotal - 1;
6005 	} else if (HAS_DDI(dev_priv) &&
6006 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
6007 		crtc->scanline_offset = 2;
6008 	} else {
6009 		crtc->scanline_offset = 1;
6010 	}
6011 }
6012 
6013 /*
6014  * This implements the workaround described in the "notes" section of the mode
6015  * set sequence documentation. When going from no pipes or single pipe to
6016  * multiple pipes, and planes are enabled after the pipe, we need to wait at
6017  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
6018  */
6019 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
6020 {
6021 	struct intel_crtc_state *crtc_state;
6022 	struct intel_crtc *crtc;
6023 	struct intel_crtc_state *first_crtc_state = NULL;
6024 	struct intel_crtc_state *other_crtc_state = NULL;
6025 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
6026 	int i;
6027 
6028 	/* look at all crtc's that are going to be enabled in during modeset */
6029 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6030 		if (!crtc_state->hw.active ||
6031 		    !intel_crtc_needs_modeset(crtc_state))
6032 			continue;
6033 
6034 		if (first_crtc_state) {
6035 			other_crtc_state = crtc_state;
6036 			break;
6037 		} else {
6038 			first_crtc_state = crtc_state;
6039 			first_pipe = crtc->pipe;
6040 		}
6041 	}
6042 
6043 	/* No workaround needed? */
6044 	if (!first_crtc_state)
6045 		return 0;
6046 
6047 	/* w/a possibly needed, check how many crtc's are already enabled. */
6048 	for_each_intel_crtc(state->base.dev, crtc) {
6049 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6050 		if (IS_ERR(crtc_state))
6051 			return PTR_ERR(crtc_state);
6052 
6053 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
6054 
6055 		if (!crtc_state->hw.active ||
6056 		    intel_crtc_needs_modeset(crtc_state))
6057 			continue;
6058 
6059 		/* 2 or more enabled crtcs means no need for w/a */
6060 		if (enabled_pipe != INVALID_PIPE)
6061 			return 0;
6062 
6063 		enabled_pipe = crtc->pipe;
6064 	}
6065 
6066 	if (enabled_pipe != INVALID_PIPE)
6067 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
6068 	else if (other_crtc_state)
6069 		other_crtc_state->hsw_workaround_pipe = first_pipe;
6070 
6071 	return 0;
6072 }
6073 
6074 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
6075 			   u8 active_pipes)
6076 {
6077 	const struct intel_crtc_state *crtc_state;
6078 	struct intel_crtc *crtc;
6079 	int i;
6080 
6081 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6082 		if (crtc_state->hw.active)
6083 			active_pipes |= BIT(crtc->pipe);
6084 		else
6085 			active_pipes &= ~BIT(crtc->pipe);
6086 	}
6087 
6088 	return active_pipes;
6089 }
6090 
6091 static int intel_modeset_checks(struct intel_atomic_state *state)
6092 {
6093 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6094 
6095 	state->modeset = true;
6096 
6097 	if (IS_HASWELL(dev_priv))
6098 		return hsw_mode_set_planes_workaround(state);
6099 
6100 	return 0;
6101 }
6102 
6103 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
6104 				     struct intel_crtc_state *new_crtc_state)
6105 {
6106 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
6107 		return;
6108 
6109 	new_crtc_state->uapi.mode_changed = false;
6110 	if (!intel_crtc_needs_modeset(new_crtc_state))
6111 		new_crtc_state->update_pipe = true;
6112 }
6113 
6114 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
6115 					  struct intel_crtc *crtc,
6116 					  u8 plane_ids_mask)
6117 {
6118 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6119 	struct intel_plane *plane;
6120 
6121 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
6122 		struct intel_plane_state *plane_state;
6123 
6124 		if ((plane_ids_mask & BIT(plane->id)) == 0)
6125 			continue;
6126 
6127 		plane_state = intel_atomic_get_plane_state(state, plane);
6128 		if (IS_ERR(plane_state))
6129 			return PTR_ERR(plane_state);
6130 	}
6131 
6132 	return 0;
6133 }
6134 
6135 int intel_atomic_add_affected_planes(struct intel_atomic_state *state,
6136 				     struct intel_crtc *crtc)
6137 {
6138 	const struct intel_crtc_state *old_crtc_state =
6139 		intel_atomic_get_old_crtc_state(state, crtc);
6140 	const struct intel_crtc_state *new_crtc_state =
6141 		intel_atomic_get_new_crtc_state(state, crtc);
6142 
6143 	return intel_crtc_add_planes_to_state(state, crtc,
6144 					      old_crtc_state->enabled_planes |
6145 					      new_crtc_state->enabled_planes);
6146 }
6147 
6148 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
6149 {
6150 	/* See {hsw,vlv,ivb}_plane_ratio() */
6151 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
6152 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
6153 		IS_IVYBRIDGE(dev_priv);
6154 }
6155 
6156 static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
6157 					   struct intel_crtc *crtc,
6158 					   struct intel_crtc *other)
6159 {
6160 	const struct intel_plane_state *plane_state;
6161 	struct intel_plane *plane;
6162 	u8 plane_ids = 0;
6163 	int i;
6164 
6165 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
6166 		if (plane->pipe == crtc->pipe)
6167 			plane_ids |= BIT(plane->id);
6168 	}
6169 
6170 	return intel_crtc_add_planes_to_state(state, other, plane_ids);
6171 }
6172 
6173 static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
6174 {
6175 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6176 	const struct intel_crtc_state *crtc_state;
6177 	struct intel_crtc *crtc;
6178 	int i;
6179 
6180 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6181 		struct intel_crtc *other;
6182 
6183 		for_each_intel_crtc_in_pipe_mask(&i915->drm, other,
6184 						 crtc_state->bigjoiner_pipes) {
6185 			int ret;
6186 
6187 			if (crtc == other)
6188 				continue;
6189 
6190 			ret = intel_crtc_add_bigjoiner_planes(state, crtc, other);
6191 			if (ret)
6192 				return ret;
6193 		}
6194 	}
6195 
6196 	return 0;
6197 }
6198 
6199 static int intel_atomic_check_planes(struct intel_atomic_state *state)
6200 {
6201 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
6202 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
6203 	struct intel_plane_state *plane_state;
6204 	struct intel_plane *plane;
6205 	struct intel_crtc *crtc;
6206 	int i, ret;
6207 
6208 	ret = icl_add_linked_planes(state);
6209 	if (ret)
6210 		return ret;
6211 
6212 	ret = intel_bigjoiner_add_affected_planes(state);
6213 	if (ret)
6214 		return ret;
6215 
6216 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
6217 		ret = intel_plane_atomic_check(state, plane);
6218 		if (ret) {
6219 			drm_dbg_atomic(&dev_priv->drm,
6220 				       "[PLANE:%d:%s] atomic driver check failed\n",
6221 				       plane->base.base.id, plane->base.name);
6222 			return ret;
6223 		}
6224 	}
6225 
6226 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6227 					    new_crtc_state, i) {
6228 		u8 old_active_planes, new_active_planes;
6229 
6230 		ret = icl_check_nv12_planes(new_crtc_state);
6231 		if (ret)
6232 			return ret;
6233 
6234 		/*
6235 		 * On some platforms the number of active planes affects
6236 		 * the planes' minimum cdclk calculation. Add such planes
6237 		 * to the state before we compute the minimum cdclk.
6238 		 */
6239 		if (!active_planes_affects_min_cdclk(dev_priv))
6240 			continue;
6241 
6242 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
6243 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
6244 
6245 		if (hweight8(old_active_planes) == hweight8(new_active_planes))
6246 			continue;
6247 
6248 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
6249 		if (ret)
6250 			return ret;
6251 	}
6252 
6253 	return 0;
6254 }
6255 
6256 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
6257 {
6258 	struct intel_crtc_state *crtc_state;
6259 	struct intel_crtc *crtc;
6260 	int i;
6261 
6262 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6263 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
6264 		int ret;
6265 
6266 		ret = intel_crtc_atomic_check(state, crtc);
6267 		if (ret) {
6268 			drm_dbg_atomic(&i915->drm,
6269 				       "[CRTC:%d:%s] atomic driver check failed\n",
6270 				       crtc->base.base.id, crtc->base.name);
6271 			return ret;
6272 		}
6273 	}
6274 
6275 	return 0;
6276 }
6277 
6278 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
6279 					       u8 transcoders)
6280 {
6281 	const struct intel_crtc_state *new_crtc_state;
6282 	struct intel_crtc *crtc;
6283 	int i;
6284 
6285 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6286 		if (new_crtc_state->hw.enable &&
6287 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
6288 		    intel_crtc_needs_modeset(new_crtc_state))
6289 			return true;
6290 	}
6291 
6292 	return false;
6293 }
6294 
6295 static bool intel_pipes_need_modeset(struct intel_atomic_state *state,
6296 				     u8 pipes)
6297 {
6298 	const struct intel_crtc_state *new_crtc_state;
6299 	struct intel_crtc *crtc;
6300 	int i;
6301 
6302 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6303 		if (new_crtc_state->hw.enable &&
6304 		    pipes & BIT(crtc->pipe) &&
6305 		    intel_crtc_needs_modeset(new_crtc_state))
6306 			return true;
6307 	}
6308 
6309 	return false;
6310 }
6311 
6312 static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
6313 					struct intel_crtc *master_crtc)
6314 {
6315 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6316 	struct intel_crtc_state *master_crtc_state =
6317 		intel_atomic_get_new_crtc_state(state, master_crtc);
6318 	struct intel_crtc *slave_crtc;
6319 
6320 	if (!master_crtc_state->bigjoiner_pipes)
6321 		return 0;
6322 
6323 	/* sanity check */
6324 	if (drm_WARN_ON(&i915->drm,
6325 			master_crtc->pipe != bigjoiner_master_pipe(master_crtc_state)))
6326 		return -EINVAL;
6327 
6328 	if (master_crtc_state->bigjoiner_pipes & ~bigjoiner_pipes(i915)) {
6329 		drm_dbg_kms(&i915->drm,
6330 			    "[CRTC:%d:%s] Cannot act as big joiner master "
6331 			    "(need 0x%x as pipes, only 0x%x possible)\n",
6332 			    master_crtc->base.base.id, master_crtc->base.name,
6333 			    master_crtc_state->bigjoiner_pipes, bigjoiner_pipes(i915));
6334 		return -EINVAL;
6335 	}
6336 
6337 	for_each_intel_crtc_in_pipe_mask(&i915->drm, slave_crtc,
6338 					 intel_crtc_bigjoiner_slave_pipes(master_crtc_state)) {
6339 		struct intel_crtc_state *slave_crtc_state;
6340 		int ret;
6341 
6342 		slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave_crtc);
6343 		if (IS_ERR(slave_crtc_state))
6344 			return PTR_ERR(slave_crtc_state);
6345 
6346 		/* master being enabled, slave was already configured? */
6347 		if (slave_crtc_state->uapi.enable) {
6348 			drm_dbg_kms(&i915->drm,
6349 				    "[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
6350 				    "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
6351 				    slave_crtc->base.base.id, slave_crtc->base.name,
6352 				    master_crtc->base.base.id, master_crtc->base.name);
6353 			return -EINVAL;
6354 		}
6355 
6356 		/*
6357 		 * The state copy logic assumes the master crtc gets processed
6358 		 * before the slave crtc during the main compute_config loop.
6359 		 * This works because the crtcs are created in pipe order,
6360 		 * and the hardware requires master pipe < slave pipe as well.
6361 		 * Should that change we need to rethink the logic.
6362 		 */
6363 		if (WARN_ON(drm_crtc_index(&master_crtc->base) >
6364 			    drm_crtc_index(&slave_crtc->base)))
6365 			return -EINVAL;
6366 
6367 		drm_dbg_kms(&i915->drm,
6368 			    "[CRTC:%d:%s] Used as slave for big joiner master [CRTC:%d:%s]\n",
6369 			    slave_crtc->base.base.id, slave_crtc->base.name,
6370 			    master_crtc->base.base.id, master_crtc->base.name);
6371 
6372 		slave_crtc_state->bigjoiner_pipes =
6373 			master_crtc_state->bigjoiner_pipes;
6374 
6375 		ret = copy_bigjoiner_crtc_state_modeset(state, slave_crtc);
6376 		if (ret)
6377 			return ret;
6378 	}
6379 
6380 	return 0;
6381 }
6382 
6383 static void kill_bigjoiner_slave(struct intel_atomic_state *state,
6384 				 struct intel_crtc *master_crtc)
6385 {
6386 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6387 	struct intel_crtc_state *master_crtc_state =
6388 		intel_atomic_get_new_crtc_state(state, master_crtc);
6389 	struct intel_crtc *slave_crtc;
6390 
6391 	for_each_intel_crtc_in_pipe_mask(&i915->drm, slave_crtc,
6392 					 intel_crtc_bigjoiner_slave_pipes(master_crtc_state)) {
6393 		struct intel_crtc_state *slave_crtc_state =
6394 			intel_atomic_get_new_crtc_state(state, slave_crtc);
6395 
6396 		slave_crtc_state->bigjoiner_pipes = 0;
6397 
6398 		intel_crtc_copy_uapi_to_hw_state_modeset(state, slave_crtc);
6399 	}
6400 
6401 	master_crtc_state->bigjoiner_pipes = 0;
6402 }
6403 
6404 /**
6405  * DOC: asynchronous flip implementation
6406  *
6407  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
6408  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
6409  * Correspondingly, support is currently added for primary plane only.
6410  *
6411  * Async flip can only change the plane surface address, so anything else
6412  * changing is rejected from the intel_async_flip_check_hw() function.
6413  * Once this check is cleared, flip done interrupt is enabled using
6414  * the intel_crtc_enable_flip_done() function.
6415  *
6416  * As soon as the surface address register is written, flip done interrupt is
6417  * generated and the requested events are sent to the usersapce in the interrupt
6418  * handler itself. The timestamp and sequence sent during the flip done event
6419  * correspond to the last vblank and have no relation to the actual time when
6420  * the flip done event was sent.
6421  */
6422 static int intel_async_flip_check_uapi(struct intel_atomic_state *state,
6423 				       struct intel_crtc *crtc)
6424 {
6425 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6426 	const struct intel_crtc_state *new_crtc_state =
6427 		intel_atomic_get_new_crtc_state(state, crtc);
6428 	const struct intel_plane_state *old_plane_state;
6429 	struct intel_plane_state *new_plane_state;
6430 	struct intel_plane *plane;
6431 	int i;
6432 
6433 	if (!new_crtc_state->uapi.async_flip)
6434 		return 0;
6435 
6436 	if (!new_crtc_state->uapi.active) {
6437 		drm_dbg_kms(&i915->drm,
6438 			    "[CRTC:%d:%s] not active\n",
6439 			    crtc->base.base.id, crtc->base.name);
6440 		return -EINVAL;
6441 	}
6442 
6443 	if (intel_crtc_needs_modeset(new_crtc_state)) {
6444 		drm_dbg_kms(&i915->drm,
6445 			    "[CRTC:%d:%s] modeset required\n",
6446 			    crtc->base.base.id, crtc->base.name);
6447 		return -EINVAL;
6448 	}
6449 
6450 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
6451 					     new_plane_state, i) {
6452 		if (plane->pipe != crtc->pipe)
6453 			continue;
6454 
6455 		/*
6456 		 * TODO: Async flip is only supported through the page flip IOCTL
6457 		 * as of now. So support currently added for primary plane only.
6458 		 * Support for other planes on platforms on which supports
6459 		 * this(vlv/chv and icl+) should be added when async flip is
6460 		 * enabled in the atomic IOCTL path.
6461 		 */
6462 		if (!plane->async_flip) {
6463 			drm_dbg_kms(&i915->drm,
6464 				    "[PLANE:%d:%s] async flip not supported\n",
6465 				    plane->base.base.id, plane->base.name);
6466 			return -EINVAL;
6467 		}
6468 
6469 		if (!old_plane_state->uapi.fb || !new_plane_state->uapi.fb) {
6470 			drm_dbg_kms(&i915->drm,
6471 				    "[PLANE:%d:%s] no old or new framebuffer\n",
6472 				    plane->base.base.id, plane->base.name);
6473 			return -EINVAL;
6474 		}
6475 	}
6476 
6477 	return 0;
6478 }
6479 
6480 static int intel_async_flip_check_hw(struct intel_atomic_state *state, struct intel_crtc *crtc)
6481 {
6482 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6483 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
6484 	const struct intel_plane_state *new_plane_state, *old_plane_state;
6485 	struct intel_plane *plane;
6486 	int i;
6487 
6488 	old_crtc_state = intel_atomic_get_old_crtc_state(state, crtc);
6489 	new_crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6490 
6491 	if (!new_crtc_state->uapi.async_flip)
6492 		return 0;
6493 
6494 	if (!new_crtc_state->hw.active) {
6495 		drm_dbg_kms(&i915->drm,
6496 			    "[CRTC:%d:%s] not active\n",
6497 			    crtc->base.base.id, crtc->base.name);
6498 		return -EINVAL;
6499 	}
6500 
6501 	if (intel_crtc_needs_modeset(new_crtc_state)) {
6502 		drm_dbg_kms(&i915->drm,
6503 			    "[CRTC:%d:%s] modeset required\n",
6504 			    crtc->base.base.id, crtc->base.name);
6505 		return -EINVAL;
6506 	}
6507 
6508 	if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
6509 		drm_dbg_kms(&i915->drm,
6510 			    "[CRTC:%d:%s] Active planes cannot be in async flip\n",
6511 			    crtc->base.base.id, crtc->base.name);
6512 		return -EINVAL;
6513 	}
6514 
6515 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
6516 					     new_plane_state, i) {
6517 		if (plane->pipe != crtc->pipe)
6518 			continue;
6519 
6520 		/*
6521 		 * Only async flip capable planes should be in the state
6522 		 * if we're really about to ask the hardware to perform
6523 		 * an async flip. We should never get this far otherwise.
6524 		 */
6525 		if (drm_WARN_ON(&i915->drm,
6526 				new_crtc_state->do_async_flip && !plane->async_flip))
6527 			return -EINVAL;
6528 
6529 		/*
6530 		 * Only check async flip capable planes other planes
6531 		 * may be involved in the initial commit due to
6532 		 * the wm0/ddb optimization.
6533 		 *
6534 		 * TODO maybe should track which planes actually
6535 		 * were requested to do the async flip...
6536 		 */
6537 		if (!plane->async_flip)
6538 			continue;
6539 
6540 		/*
6541 		 * FIXME: This check is kept generic for all platforms.
6542 		 * Need to verify this for all gen9 platforms to enable
6543 		 * this selectively if required.
6544 		 */
6545 		switch (new_plane_state->hw.fb->modifier) {
6546 		case I915_FORMAT_MOD_X_TILED:
6547 		case I915_FORMAT_MOD_Y_TILED:
6548 		case I915_FORMAT_MOD_Yf_TILED:
6549 		case I915_FORMAT_MOD_4_TILED:
6550 			break;
6551 		default:
6552 			drm_dbg_kms(&i915->drm,
6553 				    "[PLANE:%d:%s] Modifier does not support async flips\n",
6554 				    plane->base.base.id, plane->base.name);
6555 			return -EINVAL;
6556 		}
6557 
6558 		if (new_plane_state->hw.fb->format->num_planes > 1) {
6559 			drm_dbg_kms(&i915->drm,
6560 				    "[PLANE:%d:%s] Planar formats do not support async flips\n",
6561 				    plane->base.base.id, plane->base.name);
6562 			return -EINVAL;
6563 		}
6564 
6565 		if (old_plane_state->view.color_plane[0].mapping_stride !=
6566 		    new_plane_state->view.color_plane[0].mapping_stride) {
6567 			drm_dbg_kms(&i915->drm,
6568 				    "[PLANE:%d:%s] Stride cannot be changed in async flip\n",
6569 				    plane->base.base.id, plane->base.name);
6570 			return -EINVAL;
6571 		}
6572 
6573 		if (old_plane_state->hw.fb->modifier !=
6574 		    new_plane_state->hw.fb->modifier) {
6575 			drm_dbg_kms(&i915->drm,
6576 				    "[PLANE:%d:%s] Modifier cannot be changed in async flip\n",
6577 				    plane->base.base.id, plane->base.name);
6578 			return -EINVAL;
6579 		}
6580 
6581 		if (old_plane_state->hw.fb->format !=
6582 		    new_plane_state->hw.fb->format) {
6583 			drm_dbg_kms(&i915->drm,
6584 				    "[PLANE:%d:%s] Pixel format cannot be changed in async flip\n",
6585 				    plane->base.base.id, plane->base.name);
6586 			return -EINVAL;
6587 		}
6588 
6589 		if (old_plane_state->hw.rotation !=
6590 		    new_plane_state->hw.rotation) {
6591 			drm_dbg_kms(&i915->drm,
6592 				    "[PLANE:%d:%s] Rotation cannot be changed in async flip\n",
6593 				    plane->base.base.id, plane->base.name);
6594 			return -EINVAL;
6595 		}
6596 
6597 		if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
6598 		    !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
6599 			drm_dbg_kms(&i915->drm,
6600 				    "[PLANE:%d:%s] Size/co-ordinates cannot be changed in async flip\n",
6601 				    plane->base.base.id, plane->base.name);
6602 			return -EINVAL;
6603 		}
6604 
6605 		if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
6606 			drm_dbg_kms(&i915->drm,
6607 				    "[PLANES:%d:%s] Alpha value cannot be changed in async flip\n",
6608 				    plane->base.base.id, plane->base.name);
6609 			return -EINVAL;
6610 		}
6611 
6612 		if (old_plane_state->hw.pixel_blend_mode !=
6613 		    new_plane_state->hw.pixel_blend_mode) {
6614 			drm_dbg_kms(&i915->drm,
6615 				    "[PLANE:%d:%s] Pixel blend mode cannot be changed in async flip\n",
6616 				    plane->base.base.id, plane->base.name);
6617 			return -EINVAL;
6618 		}
6619 
6620 		if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
6621 			drm_dbg_kms(&i915->drm,
6622 				    "[PLANE:%d:%s] Color encoding cannot be changed in async flip\n",
6623 				    plane->base.base.id, plane->base.name);
6624 			return -EINVAL;
6625 		}
6626 
6627 		if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
6628 			drm_dbg_kms(&i915->drm,
6629 				    "[PLANE:%d:%s] Color range cannot be changed in async flip\n",
6630 				    plane->base.base.id, plane->base.name);
6631 			return -EINVAL;
6632 		}
6633 
6634 		/* plane decryption is allow to change only in synchronous flips */
6635 		if (old_plane_state->decrypt != new_plane_state->decrypt) {
6636 			drm_dbg_kms(&i915->drm,
6637 				    "[PLANE:%d:%s] Decryption cannot be changed in async flip\n",
6638 				    plane->base.base.id, plane->base.name);
6639 			return -EINVAL;
6640 		}
6641 	}
6642 
6643 	return 0;
6644 }
6645 
6646 static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
6647 {
6648 	struct drm_i915_private *i915 = to_i915(state->base.dev);
6649 	struct intel_crtc_state *crtc_state;
6650 	struct intel_crtc *crtc;
6651 	u8 affected_pipes = 0;
6652 	u8 modeset_pipes = 0;
6653 	int i;
6654 
6655 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6656 		affected_pipes |= crtc_state->bigjoiner_pipes;
6657 		if (intel_crtc_needs_modeset(crtc_state))
6658 			modeset_pipes |= crtc_state->bigjoiner_pipes;
6659 	}
6660 
6661 	for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc, affected_pipes) {
6662 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
6663 		if (IS_ERR(crtc_state))
6664 			return PTR_ERR(crtc_state);
6665 	}
6666 
6667 	for_each_intel_crtc_in_pipe_mask(&i915->drm, crtc, modeset_pipes) {
6668 		int ret;
6669 
6670 		crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
6671 
6672 		crtc_state->uapi.mode_changed = true;
6673 
6674 		ret = drm_atomic_add_affected_connectors(&state->base, &crtc->base);
6675 		if (ret)
6676 			return ret;
6677 
6678 		ret = intel_atomic_add_affected_planes(state, crtc);
6679 		if (ret)
6680 			return ret;
6681 	}
6682 
6683 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6684 		/* Kill old bigjoiner link, we may re-establish afterwards */
6685 		if (intel_crtc_needs_modeset(crtc_state) &&
6686 		    intel_crtc_is_bigjoiner_master(crtc_state))
6687 			kill_bigjoiner_slave(state, crtc);
6688 	}
6689 
6690 	return 0;
6691 }
6692 
6693 /**
6694  * intel_atomic_check - validate state object
6695  * @dev: drm device
6696  * @_state: state to validate
6697  */
6698 static int intel_atomic_check(struct drm_device *dev,
6699 			      struct drm_atomic_state *_state)
6700 {
6701 	struct drm_i915_private *dev_priv = to_i915(dev);
6702 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
6703 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
6704 	struct intel_crtc *crtc;
6705 	int ret, i;
6706 	bool any_ms = false;
6707 
6708 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6709 					    new_crtc_state, i) {
6710 		if (new_crtc_state->inherited != old_crtc_state->inherited)
6711 			new_crtc_state->uapi.mode_changed = true;
6712 
6713 		if (new_crtc_state->uapi.scaling_filter !=
6714 		    old_crtc_state->uapi.scaling_filter)
6715 			new_crtc_state->uapi.mode_changed = true;
6716 	}
6717 
6718 	intel_vrr_check_modeset(state);
6719 
6720 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
6721 	if (ret)
6722 		goto fail;
6723 
6724 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6725 		ret = intel_async_flip_check_uapi(state, crtc);
6726 		if (ret)
6727 			return ret;
6728 	}
6729 
6730 	ret = intel_bigjoiner_add_affected_crtcs(state);
6731 	if (ret)
6732 		goto fail;
6733 
6734 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6735 					    new_crtc_state, i) {
6736 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
6737 			if (intel_crtc_is_bigjoiner_slave(new_crtc_state))
6738 				copy_bigjoiner_crtc_state_nomodeset(state, crtc);
6739 			else
6740 				intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc);
6741 			continue;
6742 		}
6743 
6744 		if (intel_crtc_is_bigjoiner_slave(new_crtc_state)) {
6745 			drm_WARN_ON(&dev_priv->drm, new_crtc_state->uapi.enable);
6746 			continue;
6747 		}
6748 
6749 		ret = intel_crtc_prepare_cleared_state(state, crtc);
6750 		if (ret)
6751 			goto fail;
6752 
6753 		if (!new_crtc_state->hw.enable)
6754 			continue;
6755 
6756 		ret = intel_modeset_pipe_config(state, crtc);
6757 		if (ret)
6758 			goto fail;
6759 
6760 		ret = intel_atomic_check_bigjoiner(state, crtc);
6761 		if (ret)
6762 			goto fail;
6763 	}
6764 
6765 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6766 					    new_crtc_state, i) {
6767 		if (!intel_crtc_needs_modeset(new_crtc_state))
6768 			continue;
6769 
6770 		if (new_crtc_state->hw.enable) {
6771 			ret = intel_modeset_pipe_config_late(state, crtc);
6772 			if (ret)
6773 				goto fail;
6774 		}
6775 
6776 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
6777 	}
6778 
6779 	/**
6780 	 * Check if fastset is allowed by external dependencies like other
6781 	 * pipes and transcoders.
6782 	 *
6783 	 * Right now it only forces a fullmodeset when the MST master
6784 	 * transcoder did not changed but the pipe of the master transcoder
6785 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
6786 	 * in case of port synced crtcs, if one of the synced crtcs
6787 	 * needs a full modeset, all other synced crtcs should be
6788 	 * forced a full modeset.
6789 	 */
6790 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
6791 		if (!new_crtc_state->hw.enable || intel_crtc_needs_modeset(new_crtc_state))
6792 			continue;
6793 
6794 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
6795 			enum transcoder master = new_crtc_state->mst_master_transcoder;
6796 
6797 			if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
6798 				new_crtc_state->uapi.mode_changed = true;
6799 				new_crtc_state->update_pipe = false;
6800 			}
6801 		}
6802 
6803 		if (is_trans_port_sync_mode(new_crtc_state)) {
6804 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
6805 
6806 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
6807 				trans |= BIT(new_crtc_state->master_transcoder);
6808 
6809 			if (intel_cpu_transcoders_need_modeset(state, trans)) {
6810 				new_crtc_state->uapi.mode_changed = true;
6811 				new_crtc_state->update_pipe = false;
6812 			}
6813 		}
6814 
6815 		if (new_crtc_state->bigjoiner_pipes) {
6816 			if (intel_pipes_need_modeset(state, new_crtc_state->bigjoiner_pipes)) {
6817 				new_crtc_state->uapi.mode_changed = true;
6818 				new_crtc_state->update_pipe = false;
6819 			}
6820 		}
6821 	}
6822 
6823 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6824 					    new_crtc_state, i) {
6825 		if (!intel_crtc_needs_modeset(new_crtc_state))
6826 			continue;
6827 
6828 		any_ms = true;
6829 
6830 		intel_release_shared_dplls(state, crtc);
6831 	}
6832 
6833 	if (any_ms && !check_digital_port_conflicts(state)) {
6834 		drm_dbg_kms(&dev_priv->drm,
6835 			    "rejecting conflicting digital port configuration\n");
6836 		ret = -EINVAL;
6837 		goto fail;
6838 	}
6839 
6840 	ret = drm_dp_mst_atomic_check(&state->base);
6841 	if (ret)
6842 		goto fail;
6843 
6844 	ret = intel_atomic_check_planes(state);
6845 	if (ret)
6846 		goto fail;
6847 
6848 	ret = intel_compute_global_watermarks(state);
6849 	if (ret)
6850 		goto fail;
6851 
6852 	ret = intel_bw_atomic_check(state);
6853 	if (ret)
6854 		goto fail;
6855 
6856 	ret = intel_cdclk_atomic_check(state, &any_ms);
6857 	if (ret)
6858 		goto fail;
6859 
6860 	if (intel_any_crtc_needs_modeset(state))
6861 		any_ms = true;
6862 
6863 	if (any_ms) {
6864 		ret = intel_modeset_checks(state);
6865 		if (ret)
6866 			goto fail;
6867 
6868 		ret = intel_modeset_calc_cdclk(state);
6869 		if (ret)
6870 			return ret;
6871 	}
6872 
6873 	ret = intel_atomic_check_crtcs(state);
6874 	if (ret)
6875 		goto fail;
6876 
6877 	ret = intel_fbc_atomic_check(state);
6878 	if (ret)
6879 		goto fail;
6880 
6881 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6882 					    new_crtc_state, i) {
6883 		intel_color_assert_luts(new_crtc_state);
6884 
6885 		ret = intel_async_flip_check_hw(state, crtc);
6886 		if (ret)
6887 			goto fail;
6888 
6889 		/* Either full modeset or fastset (or neither), never both */
6890 		drm_WARN_ON(&dev_priv->drm,
6891 			    intel_crtc_needs_modeset(new_crtc_state) &&
6892 			    intel_crtc_needs_fastset(new_crtc_state));
6893 
6894 		if (!intel_crtc_needs_modeset(new_crtc_state) &&
6895 		    !intel_crtc_needs_fastset(new_crtc_state))
6896 			continue;
6897 
6898 		intel_crtc_state_dump(new_crtc_state, state,
6899 				      intel_crtc_needs_modeset(new_crtc_state) ?
6900 				      "modeset" : "fastset");
6901 	}
6902 
6903 	return 0;
6904 
6905  fail:
6906 	if (ret == -EDEADLK)
6907 		return ret;
6908 
6909 	/*
6910 	 * FIXME would probably be nice to know which crtc specifically
6911 	 * caused the failure, in cases where we can pinpoint it.
6912 	 */
6913 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
6914 					    new_crtc_state, i)
6915 		intel_crtc_state_dump(new_crtc_state, state, "failed");
6916 
6917 	return ret;
6918 }
6919 
6920 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
6921 {
6922 	struct intel_crtc_state *crtc_state;
6923 	struct intel_crtc *crtc;
6924 	int i, ret;
6925 
6926 	ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
6927 	if (ret < 0)
6928 		return ret;
6929 
6930 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
6931 		if (intel_crtc_needs_color_update(crtc_state))
6932 			intel_color_prepare_commit(crtc_state);
6933 	}
6934 
6935 	return 0;
6936 }
6937 
6938 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
6939 				  struct intel_crtc_state *crtc_state)
6940 {
6941 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6942 
6943 	if (DISPLAY_VER(dev_priv) != 2 || crtc_state->active_planes)
6944 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
6945 
6946 	if (crtc_state->has_pch_encoder) {
6947 		enum pipe pch_transcoder =
6948 			intel_crtc_pch_transcoder(crtc);
6949 
6950 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
6951 	}
6952 }
6953 
6954 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
6955 			       const struct intel_crtc_state *new_crtc_state)
6956 {
6957 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
6958 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6959 
6960 	/*
6961 	 * Update pipe size and adjust fitter if needed: the reason for this is
6962 	 * that in compute_mode_changes we check the native mode (not the pfit
6963 	 * mode) to see if we can flip rather than do a full mode set. In the
6964 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
6965 	 * pfit state, we'll end up with a big fb scanned out into the wrong
6966 	 * sized surface.
6967 	 */
6968 	intel_set_pipe_src_size(new_crtc_state);
6969 
6970 	/* on skylake this is done by detaching scalers */
6971 	if (DISPLAY_VER(dev_priv) >= 9) {
6972 		if (new_crtc_state->pch_pfit.enabled)
6973 			skl_pfit_enable(new_crtc_state);
6974 	} else if (HAS_PCH_SPLIT(dev_priv)) {
6975 		if (new_crtc_state->pch_pfit.enabled)
6976 			ilk_pfit_enable(new_crtc_state);
6977 		else if (old_crtc_state->pch_pfit.enabled)
6978 			ilk_pfit_disable(old_crtc_state);
6979 	}
6980 
6981 	/*
6982 	 * The register is supposedly single buffered so perhaps
6983 	 * not 100% correct to do this here. But SKL+ calculate
6984 	 * this based on the adjust pixel rate so pfit changes do
6985 	 * affect it and so it must be updated for fastsets.
6986 	 * HSW/BDW only really need this here for fastboot, after
6987 	 * that the value should not change without a full modeset.
6988 	 */
6989 	if (DISPLAY_VER(dev_priv) >= 9 ||
6990 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
6991 		hsw_set_linetime_wm(new_crtc_state);
6992 
6993 	if (new_crtc_state->seamless_m_n)
6994 		intel_cpu_transcoder_set_m1_n1(crtc, new_crtc_state->cpu_transcoder,
6995 					       &new_crtc_state->dp_m_n);
6996 }
6997 
6998 static void commit_pipe_pre_planes(struct intel_atomic_state *state,
6999 				   struct intel_crtc *crtc)
7000 {
7001 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7002 	const struct intel_crtc_state *old_crtc_state =
7003 		intel_atomic_get_old_crtc_state(state, crtc);
7004 	const struct intel_crtc_state *new_crtc_state =
7005 		intel_atomic_get_new_crtc_state(state, crtc);
7006 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
7007 
7008 	/*
7009 	 * During modesets pipe configuration was programmed as the
7010 	 * CRTC was enabled.
7011 	 */
7012 	if (!modeset) {
7013 		if (intel_crtc_needs_color_update(new_crtc_state))
7014 			intel_color_commit_arm(new_crtc_state);
7015 
7016 		if (DISPLAY_VER(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
7017 			bdw_set_pipemisc(new_crtc_state);
7018 
7019 		if (intel_crtc_needs_fastset(new_crtc_state))
7020 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
7021 	}
7022 
7023 	intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
7024 
7025 	intel_atomic_update_watermarks(state, crtc);
7026 }
7027 
7028 static void commit_pipe_post_planes(struct intel_atomic_state *state,
7029 				    struct intel_crtc *crtc)
7030 {
7031 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7032 	const struct intel_crtc_state *new_crtc_state =
7033 		intel_atomic_get_new_crtc_state(state, crtc);
7034 
7035 	/*
7036 	 * Disable the scaler(s) after the plane(s) so that we don't
7037 	 * get a catastrophic underrun even if the two operations
7038 	 * end up happening in two different frames.
7039 	 */
7040 	if (DISPLAY_VER(dev_priv) >= 9 &&
7041 	    !intel_crtc_needs_modeset(new_crtc_state))
7042 		skl_detach_scalers(new_crtc_state);
7043 }
7044 
7045 static void intel_enable_crtc(struct intel_atomic_state *state,
7046 			      struct intel_crtc *crtc)
7047 {
7048 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7049 	const struct intel_crtc_state *new_crtc_state =
7050 		intel_atomic_get_new_crtc_state(state, crtc);
7051 
7052 	if (!intel_crtc_needs_modeset(new_crtc_state))
7053 		return;
7054 
7055 	intel_crtc_update_active_timings(new_crtc_state);
7056 
7057 	dev_priv->display.funcs.display->crtc_enable(state, crtc);
7058 
7059 	if (intel_crtc_is_bigjoiner_slave(new_crtc_state))
7060 		return;
7061 
7062 	/* vblanks work again, re-enable pipe CRC. */
7063 	intel_crtc_enable_pipe_crc(crtc);
7064 }
7065 
7066 static void intel_update_crtc(struct intel_atomic_state *state,
7067 			      struct intel_crtc *crtc)
7068 {
7069 	struct drm_i915_private *i915 = to_i915(state->base.dev);
7070 	const struct intel_crtc_state *old_crtc_state =
7071 		intel_atomic_get_old_crtc_state(state, crtc);
7072 	struct intel_crtc_state *new_crtc_state =
7073 		intel_atomic_get_new_crtc_state(state, crtc);
7074 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
7075 
7076 	if (!modeset) {
7077 		if (new_crtc_state->preload_luts &&
7078 		    intel_crtc_needs_color_update(new_crtc_state))
7079 			intel_color_load_luts(new_crtc_state);
7080 
7081 		intel_pre_plane_update(state, crtc);
7082 
7083 		if (intel_crtc_needs_fastset(new_crtc_state))
7084 			intel_encoders_update_pipe(state, crtc);
7085 
7086 		if (DISPLAY_VER(i915) >= 11 &&
7087 		    intel_crtc_needs_fastset(new_crtc_state))
7088 			icl_set_pipe_chicken(new_crtc_state);
7089 	}
7090 
7091 	intel_fbc_update(state, crtc);
7092 
7093 	if (!modeset &&
7094 	    intel_crtc_needs_color_update(new_crtc_state))
7095 		intel_color_commit_noarm(new_crtc_state);
7096 
7097 	intel_crtc_planes_update_noarm(state, crtc);
7098 
7099 	/* Perform vblank evasion around commit operation */
7100 	intel_pipe_update_start(new_crtc_state);
7101 
7102 	commit_pipe_pre_planes(state, crtc);
7103 
7104 	intel_crtc_planes_update_arm(state, crtc);
7105 
7106 	commit_pipe_post_planes(state, crtc);
7107 
7108 	intel_pipe_update_end(new_crtc_state);
7109 
7110 	/*
7111 	 * We usually enable FIFO underrun interrupts as part of the
7112 	 * CRTC enable sequence during modesets.  But when we inherit a
7113 	 * valid pipe configuration from the BIOS we need to take care
7114 	 * of enabling them on the CRTC's first fastset.
7115 	 */
7116 	if (intel_crtc_needs_fastset(new_crtc_state) && !modeset &&
7117 	    old_crtc_state->inherited)
7118 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
7119 }
7120 
7121 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
7122 					  struct intel_crtc_state *old_crtc_state,
7123 					  struct intel_crtc_state *new_crtc_state,
7124 					  struct intel_crtc *crtc)
7125 {
7126 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7127 
7128 	/*
7129 	 * We need to disable pipe CRC before disabling the pipe,
7130 	 * or we race against vblank off.
7131 	 */
7132 	intel_crtc_disable_pipe_crc(crtc);
7133 
7134 	dev_priv->display.funcs.display->crtc_disable(state, crtc);
7135 	crtc->active = false;
7136 	intel_fbc_disable(crtc);
7137 	intel_disable_shared_dpll(old_crtc_state);
7138 
7139 	if (!new_crtc_state->hw.active)
7140 		intel_initial_watermarks(state, crtc);
7141 }
7142 
7143 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
7144 {
7145 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
7146 	struct intel_crtc *crtc;
7147 	u32 handled = 0;
7148 	int i;
7149 
7150 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7151 					    new_crtc_state, i) {
7152 		if (!intel_crtc_needs_modeset(new_crtc_state))
7153 			continue;
7154 
7155 		if (!old_crtc_state->hw.active)
7156 			continue;
7157 
7158 		intel_pre_plane_update(state, crtc);
7159 		intel_crtc_disable_planes(state, crtc);
7160 	}
7161 
7162 	/* Only disable port sync and MST slaves */
7163 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7164 					    new_crtc_state, i) {
7165 		if (!intel_crtc_needs_modeset(new_crtc_state))
7166 			continue;
7167 
7168 		if (!old_crtc_state->hw.active)
7169 			continue;
7170 
7171 		/* In case of Transcoder port Sync master slave CRTCs can be
7172 		 * assigned in any order and we need to make sure that
7173 		 * slave CRTCs are disabled first and then master CRTC since
7174 		 * Slave vblanks are masked till Master Vblanks.
7175 		 */
7176 		if (!is_trans_port_sync_slave(old_crtc_state) &&
7177 		    !intel_dp_mst_is_slave_trans(old_crtc_state) &&
7178 		    !intel_crtc_is_bigjoiner_slave(old_crtc_state))
7179 			continue;
7180 
7181 		intel_old_crtc_state_disables(state, old_crtc_state,
7182 					      new_crtc_state, crtc);
7183 		handled |= BIT(crtc->pipe);
7184 	}
7185 
7186 	/* Disable everything else left on */
7187 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7188 					    new_crtc_state, i) {
7189 		if (!intel_crtc_needs_modeset(new_crtc_state) ||
7190 		    (handled & BIT(crtc->pipe)))
7191 			continue;
7192 
7193 		if (!old_crtc_state->hw.active)
7194 			continue;
7195 
7196 		intel_old_crtc_state_disables(state, old_crtc_state,
7197 					      new_crtc_state, crtc);
7198 	}
7199 }
7200 
7201 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
7202 {
7203 	struct intel_crtc_state *new_crtc_state;
7204 	struct intel_crtc *crtc;
7205 	int i;
7206 
7207 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7208 		if (!new_crtc_state->hw.active)
7209 			continue;
7210 
7211 		intel_enable_crtc(state, crtc);
7212 		intel_update_crtc(state, crtc);
7213 	}
7214 }
7215 
7216 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
7217 {
7218 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
7219 	struct intel_crtc *crtc;
7220 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
7221 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
7222 	u8 update_pipes = 0, modeset_pipes = 0;
7223 	int i;
7224 
7225 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7226 		enum pipe pipe = crtc->pipe;
7227 
7228 		if (!new_crtc_state->hw.active)
7229 			continue;
7230 
7231 		/* ignore allocations for crtc's that have been turned off. */
7232 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
7233 			entries[pipe] = old_crtc_state->wm.skl.ddb;
7234 			update_pipes |= BIT(pipe);
7235 		} else {
7236 			modeset_pipes |= BIT(pipe);
7237 		}
7238 	}
7239 
7240 	/*
7241 	 * Whenever the number of active pipes changes, we need to make sure we
7242 	 * update the pipes in the right order so that their ddb allocations
7243 	 * never overlap with each other between CRTC updates. Otherwise we'll
7244 	 * cause pipe underruns and other bad stuff.
7245 	 *
7246 	 * So first lets enable all pipes that do not need a fullmodeset as
7247 	 * those don't have any external dependency.
7248 	 */
7249 	while (update_pipes) {
7250 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7251 						    new_crtc_state, i) {
7252 			enum pipe pipe = crtc->pipe;
7253 
7254 			if ((update_pipes & BIT(pipe)) == 0)
7255 				continue;
7256 
7257 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7258 							entries, I915_MAX_PIPES, pipe))
7259 				continue;
7260 
7261 			entries[pipe] = new_crtc_state->wm.skl.ddb;
7262 			update_pipes &= ~BIT(pipe);
7263 
7264 			intel_update_crtc(state, crtc);
7265 
7266 			/*
7267 			 * If this is an already active pipe, it's DDB changed,
7268 			 * and this isn't the last pipe that needs updating
7269 			 * then we need to wait for a vblank to pass for the
7270 			 * new ddb allocation to take effect.
7271 			 */
7272 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
7273 						 &old_crtc_state->wm.skl.ddb) &&
7274 			    (update_pipes | modeset_pipes))
7275 				intel_crtc_wait_for_next_vblank(crtc);
7276 		}
7277 	}
7278 
7279 	update_pipes = modeset_pipes;
7280 
7281 	/*
7282 	 * Enable all pipes that needs a modeset and do not depends on other
7283 	 * pipes
7284 	 */
7285 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7286 		enum pipe pipe = crtc->pipe;
7287 
7288 		if ((modeset_pipes & BIT(pipe)) == 0)
7289 			continue;
7290 
7291 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
7292 		    is_trans_port_sync_master(new_crtc_state) ||
7293 		    intel_crtc_is_bigjoiner_master(new_crtc_state))
7294 			continue;
7295 
7296 		modeset_pipes &= ~BIT(pipe);
7297 
7298 		intel_enable_crtc(state, crtc);
7299 	}
7300 
7301 	/*
7302 	 * Then we enable all remaining pipes that depend on other
7303 	 * pipes: MST slaves and port sync masters, big joiner master
7304 	 */
7305 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7306 		enum pipe pipe = crtc->pipe;
7307 
7308 		if ((modeset_pipes & BIT(pipe)) == 0)
7309 			continue;
7310 
7311 		modeset_pipes &= ~BIT(pipe);
7312 
7313 		intel_enable_crtc(state, crtc);
7314 	}
7315 
7316 	/*
7317 	 * Finally we do the plane updates/etc. for all pipes that got enabled.
7318 	 */
7319 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7320 		enum pipe pipe = crtc->pipe;
7321 
7322 		if ((update_pipes & BIT(pipe)) == 0)
7323 			continue;
7324 
7325 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
7326 									entries, I915_MAX_PIPES, pipe));
7327 
7328 		entries[pipe] = new_crtc_state->wm.skl.ddb;
7329 		update_pipes &= ~BIT(pipe);
7330 
7331 		intel_update_crtc(state, crtc);
7332 	}
7333 
7334 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
7335 	drm_WARN_ON(&dev_priv->drm, update_pipes);
7336 }
7337 
7338 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
7339 {
7340 	struct intel_atomic_state *state, *next;
7341 	struct llist_node *freed;
7342 
7343 	freed = llist_del_all(&dev_priv->display.atomic_helper.free_list);
7344 	llist_for_each_entry_safe(state, next, freed, freed)
7345 		drm_atomic_state_put(&state->base);
7346 }
7347 
7348 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
7349 {
7350 	struct drm_i915_private *dev_priv =
7351 		container_of(work, typeof(*dev_priv), display.atomic_helper.free_work);
7352 
7353 	intel_atomic_helper_free_state(dev_priv);
7354 }
7355 
7356 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
7357 {
7358 	struct wait_queue_entry wait_fence, wait_reset;
7359 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
7360 
7361 	init_wait_entry(&wait_fence, 0);
7362 	init_wait_entry(&wait_reset, 0);
7363 	for (;;) {
7364 		prepare_to_wait(&intel_state->commit_ready.wait,
7365 				&wait_fence, TASK_UNINTERRUPTIBLE);
7366 		prepare_to_wait(bit_waitqueue(&to_gt(dev_priv)->reset.flags,
7367 					      I915_RESET_MODESET),
7368 				&wait_reset, TASK_UNINTERRUPTIBLE);
7369 
7370 
7371 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
7372 		    test_bit(I915_RESET_MODESET, &to_gt(dev_priv)->reset.flags))
7373 			break;
7374 
7375 		schedule();
7376 	}
7377 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
7378 	finish_wait(bit_waitqueue(&to_gt(dev_priv)->reset.flags,
7379 				  I915_RESET_MODESET),
7380 		    &wait_reset);
7381 }
7382 
7383 static void intel_atomic_cleanup_work(struct work_struct *work)
7384 {
7385 	struct intel_atomic_state *state =
7386 		container_of(work, struct intel_atomic_state, base.commit_work);
7387 	struct drm_i915_private *i915 = to_i915(state->base.dev);
7388 	struct intel_crtc_state *old_crtc_state;
7389 	struct intel_crtc *crtc;
7390 	int i;
7391 
7392 	for_each_old_intel_crtc_in_state(state, crtc, old_crtc_state, i)
7393 		intel_color_cleanup_commit(old_crtc_state);
7394 
7395 	drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
7396 	drm_atomic_helper_commit_cleanup_done(&state->base);
7397 	drm_atomic_state_put(&state->base);
7398 
7399 	intel_atomic_helper_free_state(i915);
7400 }
7401 
7402 static void intel_atomic_prepare_plane_clear_colors(struct intel_atomic_state *state)
7403 {
7404 	struct drm_i915_private *i915 = to_i915(state->base.dev);
7405 	struct intel_plane *plane;
7406 	struct intel_plane_state *plane_state;
7407 	int i;
7408 
7409 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7410 		struct drm_framebuffer *fb = plane_state->hw.fb;
7411 		int cc_plane;
7412 		int ret;
7413 
7414 		if (!fb)
7415 			continue;
7416 
7417 		cc_plane = intel_fb_rc_ccs_cc_plane(fb);
7418 		if (cc_plane < 0)
7419 			continue;
7420 
7421 		/*
7422 		 * The layout of the fast clear color value expected by HW
7423 		 * (the DRM ABI requiring this value to be located in fb at
7424 		 * offset 0 of cc plane, plane #2 previous generations or
7425 		 * plane #1 for flat ccs):
7426 		 * - 4 x 4 bytes per-channel value
7427 		 *   (in surface type specific float/int format provided by the fb user)
7428 		 * - 8 bytes native color value used by the display
7429 		 *   (converted/written by GPU during a fast clear operation using the
7430 		 *    above per-channel values)
7431 		 *
7432 		 * The commit's FB prepare hook already ensured that FB obj is pinned and the
7433 		 * caller made sure that the object is synced wrt. the related color clear value
7434 		 * GPU write on it.
7435 		 */
7436 		ret = i915_gem_object_read_from_page(intel_fb_obj(fb),
7437 						     fb->offsets[cc_plane] + 16,
7438 						     &plane_state->ccval,
7439 						     sizeof(plane_state->ccval));
7440 		/* The above could only fail if the FB obj has an unexpected backing store type. */
7441 		drm_WARN_ON(&i915->drm, ret);
7442 	}
7443 }
7444 
7445 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
7446 {
7447 	struct drm_device *dev = state->base.dev;
7448 	struct drm_i915_private *dev_priv = to_i915(dev);
7449 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
7450 	struct intel_crtc *crtc;
7451 	struct intel_power_domain_mask put_domains[I915_MAX_PIPES] = {};
7452 	intel_wakeref_t wakeref = 0;
7453 	int i;
7454 
7455 	intel_atomic_commit_fence_wait(state);
7456 
7457 	drm_atomic_helper_wait_for_dependencies(&state->base);
7458 	drm_dp_mst_atomic_wait_for_dependencies(&state->base);
7459 
7460 	if (state->modeset)
7461 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
7462 
7463 	intel_atomic_prepare_plane_clear_colors(state);
7464 
7465 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7466 					    new_crtc_state, i) {
7467 		if (intel_crtc_needs_modeset(new_crtc_state) ||
7468 		    intel_crtc_needs_fastset(new_crtc_state))
7469 			intel_modeset_get_crtc_power_domains(new_crtc_state, &put_domains[crtc->pipe]);
7470 	}
7471 
7472 	intel_commit_modeset_disables(state);
7473 
7474 	/* FIXME: Eventually get rid of our crtc->config pointer */
7475 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7476 		crtc->config = new_crtc_state;
7477 
7478 	if (state->modeset) {
7479 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
7480 
7481 		intel_set_cdclk_pre_plane_update(state);
7482 
7483 		intel_modeset_verify_disabled(dev_priv, state);
7484 	}
7485 
7486 	intel_sagv_pre_plane_update(state);
7487 
7488 	/* Complete the events for pipes that have now been disabled */
7489 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7490 		bool modeset = intel_crtc_needs_modeset(new_crtc_state);
7491 
7492 		/* Complete events for now disable pipes here. */
7493 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
7494 			spin_lock_irq(&dev->event_lock);
7495 			drm_crtc_send_vblank_event(&crtc->base,
7496 						   new_crtc_state->uapi.event);
7497 			spin_unlock_irq(&dev->event_lock);
7498 
7499 			new_crtc_state->uapi.event = NULL;
7500 		}
7501 	}
7502 
7503 	intel_encoders_update_prepare(state);
7504 
7505 	intel_dbuf_pre_plane_update(state);
7506 	intel_mbus_dbox_update(state);
7507 
7508 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7509 		if (new_crtc_state->do_async_flip)
7510 			intel_crtc_enable_flip_done(state, crtc);
7511 	}
7512 
7513 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
7514 	dev_priv->display.funcs.display->commit_modeset_enables(state);
7515 
7516 	intel_encoders_update_complete(state);
7517 
7518 	if (state->modeset)
7519 		intel_set_cdclk_post_plane_update(state);
7520 
7521 	intel_wait_for_vblank_workers(state);
7522 
7523 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
7524 	 * already, but still need the state for the delayed optimization. To
7525 	 * fix this:
7526 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
7527 	 * - schedule that vblank worker _before_ calling hw_done
7528 	 * - at the start of commit_tail, cancel it _synchrously
7529 	 * - switch over to the vblank wait helper in the core after that since
7530 	 *   we don't need out special handling any more.
7531 	 */
7532 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
7533 
7534 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
7535 		if (new_crtc_state->do_async_flip)
7536 			intel_crtc_disable_flip_done(state, crtc);
7537 	}
7538 
7539 	/*
7540 	 * Now that the vblank has passed, we can go ahead and program the
7541 	 * optimal watermarks on platforms that need two-step watermark
7542 	 * programming.
7543 	 *
7544 	 * TODO: Move this (and other cleanup) to an async worker eventually.
7545 	 */
7546 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
7547 					    new_crtc_state, i) {
7548 		/*
7549 		 * Gen2 reports pipe underruns whenever all planes are disabled.
7550 		 * So re-enable underrun reporting after some planes get enabled.
7551 		 *
7552 		 * We do this before .optimize_watermarks() so that we have a
7553 		 * chance of catching underruns with the intermediate watermarks
7554 		 * vs. the new plane configuration.
7555 		 */
7556 		if (DISPLAY_VER(dev_priv) == 2 && planes_enabling(old_crtc_state, new_crtc_state))
7557 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
7558 
7559 		intel_optimize_watermarks(state, crtc);
7560 	}
7561 
7562 	intel_dbuf_post_plane_update(state);
7563 	intel_psr_post_plane_update(state);
7564 
7565 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
7566 		intel_post_plane_update(state, crtc);
7567 
7568 		intel_modeset_put_crtc_power_domains(crtc, &put_domains[crtc->pipe]);
7569 
7570 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
7571 
7572 		/* Must be done after gamma readout due to HSW split gamma vs. IPS w/a */
7573 		hsw_ips_post_update(state, crtc);
7574 
7575 		/*
7576 		 * Activate DRRS after state readout to avoid
7577 		 * dp_m_n vs. dp_m2_n2 confusion on BDW+.
7578 		 */
7579 		intel_drrs_activate(new_crtc_state);
7580 
7581 		/*
7582 		 * DSB cleanup is done in cleanup_work aligning with framebuffer
7583 		 * cleanup. So copy and reset the dsb structure to sync with
7584 		 * commit_done and later do dsb cleanup in cleanup_work.
7585 		 *
7586 		 * FIXME get rid of this funny new->old swapping
7587 		 */
7588 		old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
7589 	}
7590 
7591 	/* Underruns don't always raise interrupts, so check manually */
7592 	intel_check_cpu_fifo_underruns(dev_priv);
7593 	intel_check_pch_fifo_underruns(dev_priv);
7594 
7595 	if (state->modeset)
7596 		intel_verify_planes(state);
7597 
7598 	intel_sagv_post_plane_update(state);
7599 
7600 	drm_atomic_helper_commit_hw_done(&state->base);
7601 
7602 	if (state->modeset) {
7603 		/* As one of the primary mmio accessors, KMS has a high
7604 		 * likelihood of triggering bugs in unclaimed access. After we
7605 		 * finish modesetting, see if an error has been flagged, and if
7606 		 * so enable debugging for the next modeset - and hope we catch
7607 		 * the culprit.
7608 		 */
7609 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
7610 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
7611 	}
7612 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
7613 
7614 	/*
7615 	 * Defer the cleanup of the old state to a separate worker to not
7616 	 * impede the current task (userspace for blocking modesets) that
7617 	 * are executed inline. For out-of-line asynchronous modesets/flips,
7618 	 * deferring to a new worker seems overkill, but we would place a
7619 	 * schedule point (cond_resched()) here anyway to keep latencies
7620 	 * down.
7621 	 */
7622 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
7623 	queue_work(system_highpri_wq, &state->base.commit_work);
7624 }
7625 
7626 static void intel_atomic_commit_work(struct work_struct *work)
7627 {
7628 	struct intel_atomic_state *state =
7629 		container_of(work, struct intel_atomic_state, base.commit_work);
7630 
7631 	intel_atomic_commit_tail(state);
7632 }
7633 
7634 static int
7635 intel_atomic_commit_ready(struct i915_sw_fence *fence,
7636 			  enum i915_sw_fence_notify notify)
7637 {
7638 	struct intel_atomic_state *state =
7639 		container_of(fence, struct intel_atomic_state, commit_ready);
7640 
7641 	switch (notify) {
7642 	case FENCE_COMPLETE:
7643 		/* we do blocking waits in the worker, nothing to do here */
7644 		break;
7645 	case FENCE_FREE:
7646 		{
7647 			struct intel_atomic_helper *helper =
7648 				&to_i915(state->base.dev)->display.atomic_helper;
7649 
7650 			if (llist_add(&state->freed, &helper->free_list))
7651 				schedule_work(&helper->free_work);
7652 			break;
7653 		}
7654 	}
7655 
7656 	return NOTIFY_DONE;
7657 }
7658 
7659 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
7660 {
7661 	struct intel_plane_state *old_plane_state, *new_plane_state;
7662 	struct intel_plane *plane;
7663 	int i;
7664 
7665 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
7666 					     new_plane_state, i)
7667 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
7668 					to_intel_frontbuffer(new_plane_state->hw.fb),
7669 					plane->frontbuffer_bit);
7670 }
7671 
7672 static int intel_atomic_commit(struct drm_device *dev,
7673 			       struct drm_atomic_state *_state,
7674 			       bool nonblock)
7675 {
7676 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
7677 	struct drm_i915_private *dev_priv = to_i915(dev);
7678 	int ret = 0;
7679 
7680 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
7681 
7682 	drm_atomic_state_get(&state->base);
7683 	i915_sw_fence_init(&state->commit_ready,
7684 			   intel_atomic_commit_ready);
7685 
7686 	/*
7687 	 * The intel_legacy_cursor_update() fast path takes care
7688 	 * of avoiding the vblank waits for simple cursor
7689 	 * movement and flips. For cursor on/off and size changes,
7690 	 * we want to perform the vblank waits so that watermark
7691 	 * updates happen during the correct frames. Gen9+ have
7692 	 * double buffered watermarks and so shouldn't need this.
7693 	 *
7694 	 * Unset state->legacy_cursor_update before the call to
7695 	 * drm_atomic_helper_setup_commit() because otherwise
7696 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
7697 	 * we get FIFO underruns because we didn't wait
7698 	 * for vblank.
7699 	 *
7700 	 * FIXME doing watermarks and fb cleanup from a vblank worker
7701 	 * (assuming we had any) would solve these problems.
7702 	 */
7703 	if (DISPLAY_VER(dev_priv) < 9 && state->base.legacy_cursor_update) {
7704 		struct intel_crtc_state *new_crtc_state;
7705 		struct intel_crtc *crtc;
7706 		int i;
7707 
7708 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7709 			if (new_crtc_state->wm.need_postvbl_update ||
7710 			    new_crtc_state->update_wm_post)
7711 				state->base.legacy_cursor_update = false;
7712 	}
7713 
7714 	ret = intel_atomic_prepare_commit(state);
7715 	if (ret) {
7716 		drm_dbg_atomic(&dev_priv->drm,
7717 			       "Preparing state failed with %i\n", ret);
7718 		i915_sw_fence_commit(&state->commit_ready);
7719 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
7720 		return ret;
7721 	}
7722 
7723 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
7724 	if (!ret)
7725 		ret = drm_atomic_helper_swap_state(&state->base, true);
7726 	if (!ret)
7727 		intel_atomic_swap_global_state(state);
7728 
7729 	if (ret) {
7730 		struct intel_crtc_state *new_crtc_state;
7731 		struct intel_crtc *crtc;
7732 		int i;
7733 
7734 		i915_sw_fence_commit(&state->commit_ready);
7735 
7736 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
7737 			intel_color_cleanup_commit(new_crtc_state);
7738 
7739 		drm_atomic_helper_cleanup_planes(dev, &state->base);
7740 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
7741 		return ret;
7742 	}
7743 	intel_shared_dpll_swap_state(state);
7744 	intel_atomic_track_fbs(state);
7745 
7746 	drm_atomic_state_get(&state->base);
7747 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
7748 
7749 	i915_sw_fence_commit(&state->commit_ready);
7750 	if (nonblock && state->modeset) {
7751 		queue_work(dev_priv->display.wq.modeset, &state->base.commit_work);
7752 	} else if (nonblock) {
7753 		queue_work(dev_priv->display.wq.flip, &state->base.commit_work);
7754 	} else {
7755 		if (state->modeset)
7756 			flush_workqueue(dev_priv->display.wq.modeset);
7757 		intel_atomic_commit_tail(state);
7758 	}
7759 
7760 	return 0;
7761 }
7762 
7763 /**
7764  * intel_plane_destroy - destroy a plane
7765  * @plane: plane to destroy
7766  *
7767  * Common destruction function for all types of planes (primary, cursor,
7768  * sprite).
7769  */
7770 void intel_plane_destroy(struct drm_plane *plane)
7771 {
7772 	drm_plane_cleanup(plane);
7773 	kfree(to_intel_plane(plane));
7774 }
7775 
7776 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
7777 {
7778 	struct intel_plane *plane;
7779 
7780 	for_each_intel_plane(&dev_priv->drm, plane) {
7781 		struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv,
7782 							      plane->pipe);
7783 
7784 		plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
7785 	}
7786 }
7787 
7788 
7789 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
7790 				      struct drm_file *file)
7791 {
7792 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
7793 	struct drm_crtc *drmmode_crtc;
7794 	struct intel_crtc *crtc;
7795 
7796 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
7797 	if (!drmmode_crtc)
7798 		return -ENOENT;
7799 
7800 	crtc = to_intel_crtc(drmmode_crtc);
7801 	pipe_from_crtc_id->pipe = crtc->pipe;
7802 
7803 	return 0;
7804 }
7805 
7806 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
7807 {
7808 	struct drm_device *dev = encoder->base.dev;
7809 	struct intel_encoder *source_encoder;
7810 	u32 possible_clones = 0;
7811 
7812 	for_each_intel_encoder(dev, source_encoder) {
7813 		if (encoders_cloneable(encoder, source_encoder))
7814 			possible_clones |= drm_encoder_mask(&source_encoder->base);
7815 	}
7816 
7817 	return possible_clones;
7818 }
7819 
7820 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
7821 {
7822 	struct drm_device *dev = encoder->base.dev;
7823 	struct intel_crtc *crtc;
7824 	u32 possible_crtcs = 0;
7825 
7826 	for_each_intel_crtc_in_pipe_mask(dev, crtc, encoder->pipe_mask)
7827 		possible_crtcs |= drm_crtc_mask(&crtc->base);
7828 
7829 	return possible_crtcs;
7830 }
7831 
7832 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
7833 {
7834 	if (!IS_MOBILE(dev_priv))
7835 		return false;
7836 
7837 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
7838 		return false;
7839 
7840 	if (IS_IRONLAKE(dev_priv) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
7841 		return false;
7842 
7843 	return true;
7844 }
7845 
7846 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
7847 {
7848 	if (DISPLAY_VER(dev_priv) >= 9)
7849 		return false;
7850 
7851 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
7852 		return false;
7853 
7854 	if (HAS_PCH_LPT_H(dev_priv) &&
7855 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
7856 		return false;
7857 
7858 	/* DDI E can't be used if DDI A requires 4 lanes */
7859 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
7860 		return false;
7861 
7862 	if (!dev_priv->display.vbt.int_crt_support)
7863 		return false;
7864 
7865 	return true;
7866 }
7867 
7868 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
7869 {
7870 	struct intel_encoder *encoder;
7871 	bool dpd_is_edp = false;
7872 
7873 	intel_pps_unlock_regs_wa(dev_priv);
7874 
7875 	if (!HAS_DISPLAY(dev_priv))
7876 		return;
7877 
7878 	if (IS_DG2(dev_priv)) {
7879 		intel_ddi_init(dev_priv, PORT_A);
7880 		intel_ddi_init(dev_priv, PORT_B);
7881 		intel_ddi_init(dev_priv, PORT_C);
7882 		intel_ddi_init(dev_priv, PORT_D_XELPD);
7883 		intel_ddi_init(dev_priv, PORT_TC1);
7884 	} else if (IS_ALDERLAKE_P(dev_priv)) {
7885 		intel_ddi_init(dev_priv, PORT_A);
7886 		intel_ddi_init(dev_priv, PORT_B);
7887 		intel_ddi_init(dev_priv, PORT_TC1);
7888 		intel_ddi_init(dev_priv, PORT_TC2);
7889 		intel_ddi_init(dev_priv, PORT_TC3);
7890 		intel_ddi_init(dev_priv, PORT_TC4);
7891 		icl_dsi_init(dev_priv);
7892 	} else if (IS_ALDERLAKE_S(dev_priv)) {
7893 		intel_ddi_init(dev_priv, PORT_A);
7894 		intel_ddi_init(dev_priv, PORT_TC1);
7895 		intel_ddi_init(dev_priv, PORT_TC2);
7896 		intel_ddi_init(dev_priv, PORT_TC3);
7897 		intel_ddi_init(dev_priv, PORT_TC4);
7898 	} else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv)) {
7899 		intel_ddi_init(dev_priv, PORT_A);
7900 		intel_ddi_init(dev_priv, PORT_B);
7901 		intel_ddi_init(dev_priv, PORT_TC1);
7902 		intel_ddi_init(dev_priv, PORT_TC2);
7903 	} else if (DISPLAY_VER(dev_priv) >= 12) {
7904 		intel_ddi_init(dev_priv, PORT_A);
7905 		intel_ddi_init(dev_priv, PORT_B);
7906 		intel_ddi_init(dev_priv, PORT_TC1);
7907 		intel_ddi_init(dev_priv, PORT_TC2);
7908 		intel_ddi_init(dev_priv, PORT_TC3);
7909 		intel_ddi_init(dev_priv, PORT_TC4);
7910 		intel_ddi_init(dev_priv, PORT_TC5);
7911 		intel_ddi_init(dev_priv, PORT_TC6);
7912 		icl_dsi_init(dev_priv);
7913 	} else if (IS_JSL_EHL(dev_priv)) {
7914 		intel_ddi_init(dev_priv, PORT_A);
7915 		intel_ddi_init(dev_priv, PORT_B);
7916 		intel_ddi_init(dev_priv, PORT_C);
7917 		intel_ddi_init(dev_priv, PORT_D);
7918 		icl_dsi_init(dev_priv);
7919 	} else if (DISPLAY_VER(dev_priv) == 11) {
7920 		intel_ddi_init(dev_priv, PORT_A);
7921 		intel_ddi_init(dev_priv, PORT_B);
7922 		intel_ddi_init(dev_priv, PORT_C);
7923 		intel_ddi_init(dev_priv, PORT_D);
7924 		intel_ddi_init(dev_priv, PORT_E);
7925 		intel_ddi_init(dev_priv, PORT_F);
7926 		icl_dsi_init(dev_priv);
7927 	} else if (IS_GEMINILAKE(dev_priv) || IS_BROXTON(dev_priv)) {
7928 		intel_ddi_init(dev_priv, PORT_A);
7929 		intel_ddi_init(dev_priv, PORT_B);
7930 		intel_ddi_init(dev_priv, PORT_C);
7931 		vlv_dsi_init(dev_priv);
7932 	} else if (DISPLAY_VER(dev_priv) >= 9) {
7933 		intel_ddi_init(dev_priv, PORT_A);
7934 		intel_ddi_init(dev_priv, PORT_B);
7935 		intel_ddi_init(dev_priv, PORT_C);
7936 		intel_ddi_init(dev_priv, PORT_D);
7937 		intel_ddi_init(dev_priv, PORT_E);
7938 	} else if (HAS_DDI(dev_priv)) {
7939 		u32 found;
7940 
7941 		if (intel_ddi_crt_present(dev_priv))
7942 			intel_crt_init(dev_priv);
7943 
7944 		/* Haswell uses DDI functions to detect digital outputs. */
7945 		found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
7946 		if (found)
7947 			intel_ddi_init(dev_priv, PORT_A);
7948 
7949 		found = intel_de_read(dev_priv, SFUSE_STRAP);
7950 		if (found & SFUSE_STRAP_DDIB_DETECTED)
7951 			intel_ddi_init(dev_priv, PORT_B);
7952 		if (found & SFUSE_STRAP_DDIC_DETECTED)
7953 			intel_ddi_init(dev_priv, PORT_C);
7954 		if (found & SFUSE_STRAP_DDID_DETECTED)
7955 			intel_ddi_init(dev_priv, PORT_D);
7956 		if (found & SFUSE_STRAP_DDIF_DETECTED)
7957 			intel_ddi_init(dev_priv, PORT_F);
7958 	} else if (HAS_PCH_SPLIT(dev_priv)) {
7959 		int found;
7960 
7961 		/*
7962 		 * intel_edp_init_connector() depends on this completing first,
7963 		 * to prevent the registration of both eDP and LVDS and the
7964 		 * incorrect sharing of the PPS.
7965 		 */
7966 		intel_lvds_init(dev_priv);
7967 		intel_crt_init(dev_priv);
7968 
7969 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
7970 
7971 		if (ilk_has_edp_a(dev_priv))
7972 			g4x_dp_init(dev_priv, DP_A, PORT_A);
7973 
7974 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
7975 			/* PCH SDVOB multiplex with HDMIB */
7976 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
7977 			if (!found)
7978 				g4x_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
7979 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
7980 				g4x_dp_init(dev_priv, PCH_DP_B, PORT_B);
7981 		}
7982 
7983 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
7984 			g4x_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
7985 
7986 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
7987 			g4x_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
7988 
7989 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
7990 			g4x_dp_init(dev_priv, PCH_DP_C, PORT_C);
7991 
7992 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
7993 			g4x_dp_init(dev_priv, PCH_DP_D, PORT_D);
7994 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
7995 		bool has_edp, has_port;
7996 
7997 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->display.vbt.int_crt_support)
7998 			intel_crt_init(dev_priv);
7999 
8000 		/*
8001 		 * The DP_DETECTED bit is the latched state of the DDC
8002 		 * SDA pin at boot. However since eDP doesn't require DDC
8003 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
8004 		 * eDP ports may have been muxed to an alternate function.
8005 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
8006 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
8007 		 * detect eDP ports.
8008 		 *
8009 		 * Sadly the straps seem to be missing sometimes even for HDMI
8010 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
8011 		 * and VBT for the presence of the port. Additionally we can't
8012 		 * trust the port type the VBT declares as we've seen at least
8013 		 * HDMI ports that the VBT claim are DP or eDP.
8014 		 */
8015 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
8016 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
8017 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
8018 			has_edp &= g4x_dp_init(dev_priv, VLV_DP_B, PORT_B);
8019 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
8020 			g4x_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
8021 
8022 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
8023 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
8024 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
8025 			has_edp &= g4x_dp_init(dev_priv, VLV_DP_C, PORT_C);
8026 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
8027 			g4x_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
8028 
8029 		if (IS_CHERRYVIEW(dev_priv)) {
8030 			/*
8031 			 * eDP not supported on port D,
8032 			 * so no need to worry about it
8033 			 */
8034 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
8035 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
8036 				g4x_dp_init(dev_priv, CHV_DP_D, PORT_D);
8037 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
8038 				g4x_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
8039 		}
8040 
8041 		vlv_dsi_init(dev_priv);
8042 	} else if (IS_PINEVIEW(dev_priv)) {
8043 		intel_lvds_init(dev_priv);
8044 		intel_crt_init(dev_priv);
8045 	} else if (IS_DISPLAY_VER(dev_priv, 3, 4)) {
8046 		bool found = false;
8047 
8048 		if (IS_MOBILE(dev_priv))
8049 			intel_lvds_init(dev_priv);
8050 
8051 		intel_crt_init(dev_priv);
8052 
8053 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
8054 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
8055 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
8056 			if (!found && IS_G4X(dev_priv)) {
8057 				drm_dbg_kms(&dev_priv->drm,
8058 					    "probing HDMI on SDVOB\n");
8059 				g4x_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
8060 			}
8061 
8062 			if (!found && IS_G4X(dev_priv))
8063 				g4x_dp_init(dev_priv, DP_B, PORT_B);
8064 		}
8065 
8066 		/* Before G4X SDVOC doesn't have its own detect register */
8067 
8068 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
8069 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
8070 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
8071 		}
8072 
8073 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
8074 
8075 			if (IS_G4X(dev_priv)) {
8076 				drm_dbg_kms(&dev_priv->drm,
8077 					    "probing HDMI on SDVOC\n");
8078 				g4x_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
8079 			}
8080 			if (IS_G4X(dev_priv))
8081 				g4x_dp_init(dev_priv, DP_C, PORT_C);
8082 		}
8083 
8084 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
8085 			g4x_dp_init(dev_priv, DP_D, PORT_D);
8086 
8087 		if (SUPPORTS_TV(dev_priv))
8088 			intel_tv_init(dev_priv);
8089 	} else if (DISPLAY_VER(dev_priv) == 2) {
8090 		if (IS_I85X(dev_priv))
8091 			intel_lvds_init(dev_priv);
8092 
8093 		intel_crt_init(dev_priv);
8094 		intel_dvo_init(dev_priv);
8095 	}
8096 
8097 	for_each_intel_encoder(&dev_priv->drm, encoder) {
8098 		encoder->base.possible_crtcs =
8099 			intel_encoder_possible_crtcs(encoder);
8100 		encoder->base.possible_clones =
8101 			intel_encoder_possible_clones(encoder);
8102 	}
8103 
8104 	intel_init_pch_refclk(dev_priv);
8105 
8106 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
8107 }
8108 
8109 static int max_dotclock(struct drm_i915_private *i915)
8110 {
8111 	int max_dotclock = i915->max_dotclk_freq;
8112 
8113 	/* icl+ might use bigjoiner */
8114 	if (DISPLAY_VER(i915) >= 11)
8115 		max_dotclock *= 2;
8116 
8117 	return max_dotclock;
8118 }
8119 
8120 static enum drm_mode_status
8121 intel_mode_valid(struct drm_device *dev,
8122 		 const struct drm_display_mode *mode)
8123 {
8124 	struct drm_i915_private *dev_priv = to_i915(dev);
8125 	int hdisplay_max, htotal_max;
8126 	int vdisplay_max, vtotal_max;
8127 
8128 	/*
8129 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
8130 	 * of DBLSCAN modes to the output's mode list when they detect
8131 	 * the scaling mode property on the connector. And they don't
8132 	 * ask the kernel to validate those modes in any way until
8133 	 * modeset time at which point the client gets a protocol error.
8134 	 * So in order to not upset those clients we silently ignore the
8135 	 * DBLSCAN flag on such connectors. For other connectors we will
8136 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
8137 	 * And we always reject DBLSCAN modes in connector->mode_valid()
8138 	 * as we never want such modes on the connector's mode list.
8139 	 */
8140 
8141 	if (mode->vscan > 1)
8142 		return MODE_NO_VSCAN;
8143 
8144 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
8145 		return MODE_H_ILLEGAL;
8146 
8147 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
8148 			   DRM_MODE_FLAG_NCSYNC |
8149 			   DRM_MODE_FLAG_PCSYNC))
8150 		return MODE_HSYNC;
8151 
8152 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
8153 			   DRM_MODE_FLAG_PIXMUX |
8154 			   DRM_MODE_FLAG_CLKDIV2))
8155 		return MODE_BAD;
8156 
8157 	/*
8158 	 * Reject clearly excessive dotclocks early to
8159 	 * avoid having to worry about huge integers later.
8160 	 */
8161 	if (mode->clock > max_dotclock(dev_priv))
8162 		return MODE_CLOCK_HIGH;
8163 
8164 	/* Transcoder timing limits */
8165 	if (DISPLAY_VER(dev_priv) >= 11) {
8166 		hdisplay_max = 16384;
8167 		vdisplay_max = 8192;
8168 		htotal_max = 16384;
8169 		vtotal_max = 8192;
8170 	} else if (DISPLAY_VER(dev_priv) >= 9 ||
8171 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
8172 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
8173 		vdisplay_max = 4096;
8174 		htotal_max = 8192;
8175 		vtotal_max = 8192;
8176 	} else if (DISPLAY_VER(dev_priv) >= 3) {
8177 		hdisplay_max = 4096;
8178 		vdisplay_max = 4096;
8179 		htotal_max = 8192;
8180 		vtotal_max = 8192;
8181 	} else {
8182 		hdisplay_max = 2048;
8183 		vdisplay_max = 2048;
8184 		htotal_max = 4096;
8185 		vtotal_max = 4096;
8186 	}
8187 
8188 	if (mode->hdisplay > hdisplay_max ||
8189 	    mode->hsync_start > htotal_max ||
8190 	    mode->hsync_end > htotal_max ||
8191 	    mode->htotal > htotal_max)
8192 		return MODE_H_ILLEGAL;
8193 
8194 	if (mode->vdisplay > vdisplay_max ||
8195 	    mode->vsync_start > vtotal_max ||
8196 	    mode->vsync_end > vtotal_max ||
8197 	    mode->vtotal > vtotal_max)
8198 		return MODE_V_ILLEGAL;
8199 
8200 	if (DISPLAY_VER(dev_priv) >= 5) {
8201 		if (mode->hdisplay < 64 ||
8202 		    mode->htotal - mode->hdisplay < 32)
8203 			return MODE_H_ILLEGAL;
8204 
8205 		if (mode->vtotal - mode->vdisplay < 5)
8206 			return MODE_V_ILLEGAL;
8207 	} else {
8208 		if (mode->htotal - mode->hdisplay < 32)
8209 			return MODE_H_ILLEGAL;
8210 
8211 		if (mode->vtotal - mode->vdisplay < 3)
8212 			return MODE_V_ILLEGAL;
8213 	}
8214 
8215 	/*
8216 	 * Cantiga+ cannot handle modes with a hsync front porch of 0.
8217 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
8218 	 */
8219 	if ((DISPLAY_VER(dev_priv) > 4 || IS_G4X(dev_priv)) &&
8220 	    mode->hsync_start == mode->hdisplay)
8221 		return MODE_H_ILLEGAL;
8222 
8223 	return MODE_OK;
8224 }
8225 
8226 enum drm_mode_status
8227 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
8228 				const struct drm_display_mode *mode,
8229 				bool bigjoiner)
8230 {
8231 	int plane_width_max, plane_height_max;
8232 
8233 	/*
8234 	 * intel_mode_valid() should be
8235 	 * sufficient on older platforms.
8236 	 */
8237 	if (DISPLAY_VER(dev_priv) < 9)
8238 		return MODE_OK;
8239 
8240 	/*
8241 	 * Most people will probably want a fullscreen
8242 	 * plane so let's not advertize modes that are
8243 	 * too big for that.
8244 	 */
8245 	if (DISPLAY_VER(dev_priv) >= 11) {
8246 		plane_width_max = 5120 << bigjoiner;
8247 		plane_height_max = 4320;
8248 	} else {
8249 		plane_width_max = 5120;
8250 		plane_height_max = 4096;
8251 	}
8252 
8253 	if (mode->hdisplay > plane_width_max)
8254 		return MODE_H_ILLEGAL;
8255 
8256 	if (mode->vdisplay > plane_height_max)
8257 		return MODE_V_ILLEGAL;
8258 
8259 	return MODE_OK;
8260 }
8261 
8262 static const struct drm_mode_config_funcs intel_mode_funcs = {
8263 	.fb_create = intel_user_framebuffer_create,
8264 	.get_format_info = intel_fb_get_format_info,
8265 	.output_poll_changed = intel_fbdev_output_poll_changed,
8266 	.mode_valid = intel_mode_valid,
8267 	.atomic_check = intel_atomic_check,
8268 	.atomic_commit = intel_atomic_commit,
8269 	.atomic_state_alloc = intel_atomic_state_alloc,
8270 	.atomic_state_clear = intel_atomic_state_clear,
8271 	.atomic_state_free = intel_atomic_state_free,
8272 };
8273 
8274 static const struct intel_display_funcs skl_display_funcs = {
8275 	.get_pipe_config = hsw_get_pipe_config,
8276 	.crtc_enable = hsw_crtc_enable,
8277 	.crtc_disable = hsw_crtc_disable,
8278 	.commit_modeset_enables = skl_commit_modeset_enables,
8279 	.get_initial_plane_config = skl_get_initial_plane_config,
8280 };
8281 
8282 static const struct intel_display_funcs ddi_display_funcs = {
8283 	.get_pipe_config = hsw_get_pipe_config,
8284 	.crtc_enable = hsw_crtc_enable,
8285 	.crtc_disable = hsw_crtc_disable,
8286 	.commit_modeset_enables = intel_commit_modeset_enables,
8287 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8288 };
8289 
8290 static const struct intel_display_funcs pch_split_display_funcs = {
8291 	.get_pipe_config = ilk_get_pipe_config,
8292 	.crtc_enable = ilk_crtc_enable,
8293 	.crtc_disable = ilk_crtc_disable,
8294 	.commit_modeset_enables = intel_commit_modeset_enables,
8295 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8296 };
8297 
8298 static const struct intel_display_funcs vlv_display_funcs = {
8299 	.get_pipe_config = i9xx_get_pipe_config,
8300 	.crtc_enable = valleyview_crtc_enable,
8301 	.crtc_disable = i9xx_crtc_disable,
8302 	.commit_modeset_enables = intel_commit_modeset_enables,
8303 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8304 };
8305 
8306 static const struct intel_display_funcs i9xx_display_funcs = {
8307 	.get_pipe_config = i9xx_get_pipe_config,
8308 	.crtc_enable = i9xx_crtc_enable,
8309 	.crtc_disable = i9xx_crtc_disable,
8310 	.commit_modeset_enables = intel_commit_modeset_enables,
8311 	.get_initial_plane_config = i9xx_get_initial_plane_config,
8312 };
8313 
8314 /**
8315  * intel_init_display_hooks - initialize the display modesetting hooks
8316  * @dev_priv: device private
8317  */
8318 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
8319 {
8320 	if (!HAS_DISPLAY(dev_priv))
8321 		return;
8322 
8323 	intel_color_init_hooks(dev_priv);
8324 	intel_init_cdclk_hooks(dev_priv);
8325 	intel_audio_hooks_init(dev_priv);
8326 
8327 	intel_dpll_init_clock_hook(dev_priv);
8328 
8329 	if (DISPLAY_VER(dev_priv) >= 9) {
8330 		dev_priv->display.funcs.display = &skl_display_funcs;
8331 	} else if (HAS_DDI(dev_priv)) {
8332 		dev_priv->display.funcs.display = &ddi_display_funcs;
8333 	} else if (HAS_PCH_SPLIT(dev_priv)) {
8334 		dev_priv->display.funcs.display = &pch_split_display_funcs;
8335 	} else if (IS_CHERRYVIEW(dev_priv) ||
8336 		   IS_VALLEYVIEW(dev_priv)) {
8337 		dev_priv->display.funcs.display = &vlv_display_funcs;
8338 	} else {
8339 		dev_priv->display.funcs.display = &i9xx_display_funcs;
8340 	}
8341 
8342 	intel_fdi_init_hook(dev_priv);
8343 }
8344 
8345 void intel_modeset_init_hw(struct drm_i915_private *i915)
8346 {
8347 	struct intel_cdclk_state *cdclk_state;
8348 
8349 	if (!HAS_DISPLAY(i915))
8350 		return;
8351 
8352 	cdclk_state = to_intel_cdclk_state(i915->display.cdclk.obj.state);
8353 
8354 	intel_update_cdclk(i915);
8355 	intel_cdclk_dump_config(i915, &i915->display.cdclk.hw, "Current CDCLK");
8356 	cdclk_state->logical = cdclk_state->actual = i915->display.cdclk.hw;
8357 }
8358 
8359 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
8360 {
8361 	struct drm_plane *plane;
8362 	struct intel_crtc *crtc;
8363 
8364 	for_each_intel_crtc(state->dev, crtc) {
8365 		struct intel_crtc_state *crtc_state;
8366 
8367 		crtc_state = intel_atomic_get_crtc_state(state, crtc);
8368 		if (IS_ERR(crtc_state))
8369 			return PTR_ERR(crtc_state);
8370 
8371 		if (crtc_state->hw.active) {
8372 			/*
8373 			 * Preserve the inherited flag to avoid
8374 			 * taking the full modeset path.
8375 			 */
8376 			crtc_state->inherited = true;
8377 		}
8378 	}
8379 
8380 	drm_for_each_plane(plane, state->dev) {
8381 		struct drm_plane_state *plane_state;
8382 
8383 		plane_state = drm_atomic_get_plane_state(state, plane);
8384 		if (IS_ERR(plane_state))
8385 			return PTR_ERR(plane_state);
8386 	}
8387 
8388 	return 0;
8389 }
8390 
8391 /*
8392  * Calculate what we think the watermarks should be for the state we've read
8393  * out of the hardware and then immediately program those watermarks so that
8394  * we ensure the hardware settings match our internal state.
8395  *
8396  * We can calculate what we think WM's should be by creating a duplicate of the
8397  * current state (which was constructed during hardware readout) and running it
8398  * through the atomic check code to calculate new watermark values in the
8399  * state object.
8400  */
8401 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
8402 {
8403 	struct drm_atomic_state *state;
8404 	struct intel_atomic_state *intel_state;
8405 	struct intel_crtc *crtc;
8406 	struct intel_crtc_state *crtc_state;
8407 	struct drm_modeset_acquire_ctx ctx;
8408 	int ret;
8409 	int i;
8410 
8411 	/* Only supported on platforms that use atomic watermark design */
8412 	if (!dev_priv->display.funcs.wm->optimize_watermarks)
8413 		return;
8414 
8415 	state = drm_atomic_state_alloc(&dev_priv->drm);
8416 	if (drm_WARN_ON(&dev_priv->drm, !state))
8417 		return;
8418 
8419 	intel_state = to_intel_atomic_state(state);
8420 
8421 	drm_modeset_acquire_init(&ctx, 0);
8422 
8423 retry:
8424 	state->acquire_ctx = &ctx;
8425 
8426 	/*
8427 	 * Hardware readout is the only time we don't want to calculate
8428 	 * intermediate watermarks (since we don't trust the current
8429 	 * watermarks).
8430 	 */
8431 	if (!HAS_GMCH(dev_priv))
8432 		intel_state->skip_intermediate_wm = true;
8433 
8434 	ret = sanitize_watermarks_add_affected(state);
8435 	if (ret)
8436 		goto fail;
8437 
8438 	ret = intel_atomic_check(&dev_priv->drm, state);
8439 	if (ret)
8440 		goto fail;
8441 
8442 	/* Write calculated watermark values back */
8443 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
8444 		crtc_state->wm.need_postvbl_update = true;
8445 		intel_optimize_watermarks(intel_state, crtc);
8446 
8447 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
8448 	}
8449 
8450 fail:
8451 	if (ret == -EDEADLK) {
8452 		drm_atomic_state_clear(state);
8453 		drm_modeset_backoff(&ctx);
8454 		goto retry;
8455 	}
8456 
8457 	/*
8458 	 * If we fail here, it means that the hardware appears to be
8459 	 * programmed in a way that shouldn't be possible, given our
8460 	 * understanding of watermark requirements.  This might mean a
8461 	 * mistake in the hardware readout code or a mistake in the
8462 	 * watermark calculations for a given platform.  Raise a WARN
8463 	 * so that this is noticeable.
8464 	 *
8465 	 * If this actually happens, we'll have to just leave the
8466 	 * BIOS-programmed watermarks untouched and hope for the best.
8467 	 */
8468 	drm_WARN(&dev_priv->drm, ret,
8469 		 "Could not determine valid watermarks for inherited state\n");
8470 
8471 	drm_atomic_state_put(state);
8472 
8473 	drm_modeset_drop_locks(&ctx);
8474 	drm_modeset_acquire_fini(&ctx);
8475 }
8476 
8477 static int intel_initial_commit(struct drm_device *dev)
8478 {
8479 	struct drm_atomic_state *state = NULL;
8480 	struct drm_modeset_acquire_ctx ctx;
8481 	struct intel_crtc *crtc;
8482 	int ret = 0;
8483 
8484 	state = drm_atomic_state_alloc(dev);
8485 	if (!state)
8486 		return -ENOMEM;
8487 
8488 	drm_modeset_acquire_init(&ctx, 0);
8489 
8490 retry:
8491 	state->acquire_ctx = &ctx;
8492 
8493 	for_each_intel_crtc(dev, crtc) {
8494 		struct intel_crtc_state *crtc_state =
8495 			intel_atomic_get_crtc_state(state, crtc);
8496 
8497 		if (IS_ERR(crtc_state)) {
8498 			ret = PTR_ERR(crtc_state);
8499 			goto out;
8500 		}
8501 
8502 		if (crtc_state->hw.active) {
8503 			struct intel_encoder *encoder;
8504 
8505 			/*
8506 			 * We've not yet detected sink capabilities
8507 			 * (audio,infoframes,etc.) and thus we don't want to
8508 			 * force a full state recomputation yet. We want that to
8509 			 * happen only for the first real commit from userspace.
8510 			 * So preserve the inherited flag for the time being.
8511 			 */
8512 			crtc_state->inherited = true;
8513 
8514 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
8515 			if (ret)
8516 				goto out;
8517 
8518 			/*
8519 			 * FIXME hack to force a LUT update to avoid the
8520 			 * plane update forcing the pipe gamma on without
8521 			 * having a proper LUT loaded. Remove once we
8522 			 * have readout for pipe gamma enable.
8523 			 */
8524 			crtc_state->uapi.color_mgmt_changed = true;
8525 
8526 			for_each_intel_encoder_mask(dev, encoder,
8527 						    crtc_state->uapi.encoder_mask) {
8528 				if (encoder->initial_fastset_check &&
8529 				    !encoder->initial_fastset_check(encoder, crtc_state)) {
8530 					ret = drm_atomic_add_affected_connectors(state,
8531 										 &crtc->base);
8532 					if (ret)
8533 						goto out;
8534 				}
8535 			}
8536 		}
8537 	}
8538 
8539 	ret = drm_atomic_commit(state);
8540 
8541 out:
8542 	if (ret == -EDEADLK) {
8543 		drm_atomic_state_clear(state);
8544 		drm_modeset_backoff(&ctx);
8545 		goto retry;
8546 	}
8547 
8548 	drm_atomic_state_put(state);
8549 
8550 	drm_modeset_drop_locks(&ctx);
8551 	drm_modeset_acquire_fini(&ctx);
8552 
8553 	return ret;
8554 }
8555 
8556 static const struct drm_mode_config_helper_funcs intel_mode_config_funcs = {
8557 	.atomic_commit_setup = drm_dp_mst_atomic_setup_commit,
8558 };
8559 
8560 static void intel_mode_config_init(struct drm_i915_private *i915)
8561 {
8562 	struct drm_mode_config *mode_config = &i915->drm.mode_config;
8563 
8564 	drm_mode_config_init(&i915->drm);
8565 	INIT_LIST_HEAD(&i915->display.global.obj_list);
8566 
8567 	mode_config->min_width = 0;
8568 	mode_config->min_height = 0;
8569 
8570 	mode_config->preferred_depth = 24;
8571 	mode_config->prefer_shadow = 1;
8572 
8573 	mode_config->funcs = &intel_mode_funcs;
8574 	mode_config->helper_private = &intel_mode_config_funcs;
8575 
8576 	mode_config->async_page_flip = HAS_ASYNC_FLIPS(i915);
8577 
8578 	/*
8579 	 * Maximum framebuffer dimensions, chosen to match
8580 	 * the maximum render engine surface size on gen4+.
8581 	 */
8582 	if (DISPLAY_VER(i915) >= 7) {
8583 		mode_config->max_width = 16384;
8584 		mode_config->max_height = 16384;
8585 	} else if (DISPLAY_VER(i915) >= 4) {
8586 		mode_config->max_width = 8192;
8587 		mode_config->max_height = 8192;
8588 	} else if (DISPLAY_VER(i915) == 3) {
8589 		mode_config->max_width = 4096;
8590 		mode_config->max_height = 4096;
8591 	} else {
8592 		mode_config->max_width = 2048;
8593 		mode_config->max_height = 2048;
8594 	}
8595 
8596 	if (IS_I845G(i915) || IS_I865G(i915)) {
8597 		mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
8598 		mode_config->cursor_height = 1023;
8599 	} else if (IS_I830(i915) || IS_I85X(i915) ||
8600 		   IS_I915G(i915) || IS_I915GM(i915)) {
8601 		mode_config->cursor_width = 64;
8602 		mode_config->cursor_height = 64;
8603 	} else {
8604 		mode_config->cursor_width = 256;
8605 		mode_config->cursor_height = 256;
8606 	}
8607 }
8608 
8609 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
8610 {
8611 	intel_atomic_global_obj_cleanup(i915);
8612 	drm_mode_config_cleanup(&i915->drm);
8613 }
8614 
8615 /* part #1: call before irq install */
8616 int intel_modeset_init_noirq(struct drm_i915_private *i915)
8617 {
8618 	int ret;
8619 
8620 	if (i915_inject_probe_failure(i915))
8621 		return -ENODEV;
8622 
8623 	if (HAS_DISPLAY(i915)) {
8624 		ret = drm_vblank_init(&i915->drm,
8625 				      INTEL_NUM_PIPES(i915));
8626 		if (ret)
8627 			return ret;
8628 	}
8629 
8630 	intel_bios_init(i915);
8631 
8632 	ret = intel_vga_register(i915);
8633 	if (ret)
8634 		goto cleanup_bios;
8635 
8636 	/* FIXME: completely on the wrong abstraction layer */
8637 	intel_power_domains_init_hw(i915, false);
8638 
8639 	if (!HAS_DISPLAY(i915))
8640 		return 0;
8641 
8642 	intel_dmc_ucode_init(i915);
8643 
8644 	i915->display.wq.modeset = alloc_ordered_workqueue("i915_modeset", 0);
8645 	i915->display.wq.flip = alloc_workqueue("i915_flip", WQ_HIGHPRI |
8646 						WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
8647 
8648 	intel_mode_config_init(i915);
8649 
8650 	ret = intel_cdclk_init(i915);
8651 	if (ret)
8652 		goto cleanup_vga_client_pw_domain_dmc;
8653 
8654 	ret = intel_color_init(i915);
8655 	if (ret)
8656 		goto cleanup_vga_client_pw_domain_dmc;
8657 
8658 	ret = intel_dbuf_init(i915);
8659 	if (ret)
8660 		goto cleanup_vga_client_pw_domain_dmc;
8661 
8662 	ret = intel_bw_init(i915);
8663 	if (ret)
8664 		goto cleanup_vga_client_pw_domain_dmc;
8665 
8666 	init_llist_head(&i915->display.atomic_helper.free_list);
8667 	INIT_WORK(&i915->display.atomic_helper.free_work,
8668 		  intel_atomic_helper_free_state_worker);
8669 
8670 	intel_init_quirks(i915);
8671 
8672 	intel_fbc_init(i915);
8673 
8674 	return 0;
8675 
8676 cleanup_vga_client_pw_domain_dmc:
8677 	intel_dmc_ucode_fini(i915);
8678 	intel_power_domains_driver_remove(i915);
8679 	intel_vga_unregister(i915);
8680 cleanup_bios:
8681 	intel_bios_driver_remove(i915);
8682 
8683 	return ret;
8684 }
8685 
8686 /* part #2: call after irq install, but before gem init */
8687 int intel_modeset_init_nogem(struct drm_i915_private *i915)
8688 {
8689 	struct drm_device *dev = &i915->drm;
8690 	enum pipe pipe;
8691 	struct intel_crtc *crtc;
8692 	int ret;
8693 
8694 	if (!HAS_DISPLAY(i915))
8695 		return 0;
8696 
8697 	intel_init_pm(i915);
8698 
8699 	intel_panel_sanitize_ssc(i915);
8700 
8701 	intel_pps_setup(i915);
8702 
8703 	intel_gmbus_setup(i915);
8704 
8705 	drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
8706 		    INTEL_NUM_PIPES(i915),
8707 		    INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
8708 
8709 	for_each_pipe(i915, pipe) {
8710 		ret = intel_crtc_init(i915, pipe);
8711 		if (ret) {
8712 			intel_mode_config_cleanup(i915);
8713 			return ret;
8714 		}
8715 	}
8716 
8717 	intel_plane_possible_crtcs_init(i915);
8718 	intel_shared_dpll_init(i915);
8719 	intel_fdi_pll_freq_update(i915);
8720 
8721 	intel_update_czclk(i915);
8722 	intel_modeset_init_hw(i915);
8723 	intel_dpll_update_ref_clks(i915);
8724 
8725 	intel_hdcp_component_init(i915);
8726 
8727 	if (i915->display.cdclk.max_cdclk_freq == 0)
8728 		intel_update_max_cdclk(i915);
8729 
8730 	intel_hti_init(i915);
8731 
8732 	/* Just disable it once at startup */
8733 	intel_vga_disable(i915);
8734 	intel_setup_outputs(i915);
8735 
8736 	drm_modeset_lock_all(dev);
8737 	intel_modeset_setup_hw_state(i915, dev->mode_config.acquire_ctx);
8738 	intel_acpi_assign_connector_fwnodes(i915);
8739 	drm_modeset_unlock_all(dev);
8740 
8741 	for_each_intel_crtc(dev, crtc) {
8742 		if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
8743 			continue;
8744 		intel_crtc_initial_plane_config(crtc);
8745 	}
8746 
8747 	/*
8748 	 * Make sure hardware watermarks really match the state we read out.
8749 	 * Note that we need to do this after reconstructing the BIOS fb's
8750 	 * since the watermark calculation done here will use pstate->fb.
8751 	 */
8752 	if (!HAS_GMCH(i915))
8753 		sanitize_watermarks(i915);
8754 
8755 	return 0;
8756 }
8757 
8758 /* part #3: call after gem init */
8759 int intel_modeset_init(struct drm_i915_private *i915)
8760 {
8761 	int ret;
8762 
8763 	if (!HAS_DISPLAY(i915))
8764 		return 0;
8765 
8766 	/*
8767 	 * Force all active planes to recompute their states. So that on
8768 	 * mode_setcrtc after probe, all the intel_plane_state variables
8769 	 * are already calculated and there is no assert_plane warnings
8770 	 * during bootup.
8771 	 */
8772 	ret = intel_initial_commit(&i915->drm);
8773 	if (ret)
8774 		drm_dbg_kms(&i915->drm, "Initial modeset failed, %d\n", ret);
8775 
8776 	intel_overlay_setup(i915);
8777 
8778 	ret = intel_fbdev_init(&i915->drm);
8779 	if (ret)
8780 		return ret;
8781 
8782 	/* Only enable hotplug handling once the fbdev is fully set up. */
8783 	intel_hpd_init(i915);
8784 	intel_hpd_poll_disable(i915);
8785 
8786 	skl_watermark_ipc_init(i915);
8787 
8788 	return 0;
8789 }
8790 
8791 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
8792 {
8793 	struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv, pipe);
8794 	/* 640x480@60Hz, ~25175 kHz */
8795 	struct dpll clock = {
8796 		.m1 = 18,
8797 		.m2 = 7,
8798 		.p1 = 13,
8799 		.p2 = 4,
8800 		.n = 2,
8801 	};
8802 	u32 dpll, fp;
8803 	int i;
8804 
8805 	drm_WARN_ON(&dev_priv->drm,
8806 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
8807 
8808 	drm_dbg_kms(&dev_priv->drm,
8809 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
8810 		    pipe_name(pipe), clock.vco, clock.dot);
8811 
8812 	fp = i9xx_dpll_compute_fp(&clock);
8813 	dpll = DPLL_DVO_2X_MODE |
8814 		DPLL_VGA_MODE_DIS |
8815 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
8816 		PLL_P2_DIVIDE_BY_4 |
8817 		PLL_REF_INPUT_DREFCLK |
8818 		DPLL_VCO_ENABLE;
8819 
8820 	intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
8821 	intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
8822 	intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
8823 	intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
8824 	intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
8825 	intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
8826 	intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
8827 
8828 	intel_de_write(dev_priv, FP0(pipe), fp);
8829 	intel_de_write(dev_priv, FP1(pipe), fp);
8830 
8831 	/*
8832 	 * Apparently we need to have VGA mode enabled prior to changing
8833 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
8834 	 * dividers, even though the register value does change.
8835 	 */
8836 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
8837 	intel_de_write(dev_priv, DPLL(pipe), dpll);
8838 
8839 	/* Wait for the clocks to stabilize. */
8840 	intel_de_posting_read(dev_priv, DPLL(pipe));
8841 	udelay(150);
8842 
8843 	/* The pixel multiplier can only be updated once the
8844 	 * DPLL is enabled and the clocks are stable.
8845 	 *
8846 	 * So write it again.
8847 	 */
8848 	intel_de_write(dev_priv, DPLL(pipe), dpll);
8849 
8850 	/* We do this three times for luck */
8851 	for (i = 0; i < 3 ; i++) {
8852 		intel_de_write(dev_priv, DPLL(pipe), dpll);
8853 		intel_de_posting_read(dev_priv, DPLL(pipe));
8854 		udelay(150); /* wait for warmup */
8855 	}
8856 
8857 	intel_de_write(dev_priv, PIPECONF(pipe), PIPECONF_ENABLE);
8858 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
8859 
8860 	intel_wait_for_pipe_scanline_moving(crtc);
8861 }
8862 
8863 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
8864 {
8865 	struct intel_crtc *crtc = intel_crtc_for_pipe(dev_priv, pipe);
8866 
8867 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
8868 		    pipe_name(pipe));
8869 
8870 	drm_WARN_ON(&dev_priv->drm,
8871 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) & DISP_ENABLE);
8872 	drm_WARN_ON(&dev_priv->drm,
8873 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) & DISP_ENABLE);
8874 	drm_WARN_ON(&dev_priv->drm,
8875 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) & DISP_ENABLE);
8876 	drm_WARN_ON(&dev_priv->drm,
8877 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE_MASK);
8878 	drm_WARN_ON(&dev_priv->drm,
8879 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE_MASK);
8880 
8881 	intel_de_write(dev_priv, PIPECONF(pipe), 0);
8882 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
8883 
8884 	intel_wait_for_pipe_scanline_stopped(crtc);
8885 
8886 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
8887 	intel_de_posting_read(dev_priv, DPLL(pipe));
8888 }
8889 
8890 void intel_display_resume(struct drm_device *dev)
8891 {
8892 	struct drm_i915_private *i915 = to_i915(dev);
8893 	struct drm_atomic_state *state = i915->display.restore.modeset_state;
8894 	struct drm_modeset_acquire_ctx ctx;
8895 	int ret;
8896 
8897 	if (!HAS_DISPLAY(i915))
8898 		return;
8899 
8900 	i915->display.restore.modeset_state = NULL;
8901 	if (state)
8902 		state->acquire_ctx = &ctx;
8903 
8904 	drm_modeset_acquire_init(&ctx, 0);
8905 
8906 	while (1) {
8907 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
8908 		if (ret != -EDEADLK)
8909 			break;
8910 
8911 		drm_modeset_backoff(&ctx);
8912 	}
8913 
8914 	if (!ret)
8915 		ret = __intel_display_resume(i915, state, &ctx);
8916 
8917 	skl_watermark_ipc_update(i915);
8918 	drm_modeset_drop_locks(&ctx);
8919 	drm_modeset_acquire_fini(&ctx);
8920 
8921 	if (ret)
8922 		drm_err(&i915->drm,
8923 			"Restoring old state failed with %i\n", ret);
8924 	if (state)
8925 		drm_atomic_state_put(state);
8926 }
8927 
8928 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
8929 {
8930 	struct intel_connector *connector;
8931 	struct drm_connector_list_iter conn_iter;
8932 
8933 	/* Kill all the work that may have been queued by hpd. */
8934 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
8935 	for_each_intel_connector_iter(connector, &conn_iter) {
8936 		if (connector->modeset_retry_work.func)
8937 			cancel_work_sync(&connector->modeset_retry_work);
8938 		if (connector->hdcp.shim) {
8939 			cancel_delayed_work_sync(&connector->hdcp.check_work);
8940 			cancel_work_sync(&connector->hdcp.prop_work);
8941 		}
8942 	}
8943 	drm_connector_list_iter_end(&conn_iter);
8944 }
8945 
8946 /* part #1: call before irq uninstall */
8947 void intel_modeset_driver_remove(struct drm_i915_private *i915)
8948 {
8949 	if (!HAS_DISPLAY(i915))
8950 		return;
8951 
8952 	flush_workqueue(i915->display.wq.flip);
8953 	flush_workqueue(i915->display.wq.modeset);
8954 
8955 	flush_work(&i915->display.atomic_helper.free_work);
8956 	drm_WARN_ON(&i915->drm, !llist_empty(&i915->display.atomic_helper.free_list));
8957 
8958 	/*
8959 	 * MST topology needs to be suspended so we don't have any calls to
8960 	 * fbdev after it's finalized. MST will be destroyed later as part of
8961 	 * drm_mode_config_cleanup()
8962 	 */
8963 	intel_dp_mst_suspend(i915);
8964 }
8965 
8966 /* part #2: call after irq uninstall */
8967 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
8968 {
8969 	if (!HAS_DISPLAY(i915))
8970 		return;
8971 
8972 	/*
8973 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
8974 	 * poll handlers. Hence disable polling after hpd handling is shut down.
8975 	 */
8976 	intel_hpd_poll_fini(i915);
8977 
8978 	/* poll work can call into fbdev, hence clean that up afterwards */
8979 	intel_fbdev_fini(i915);
8980 
8981 	intel_unregister_dsm_handler();
8982 
8983 	/* flush any delayed tasks or pending work */
8984 	flush_scheduled_work();
8985 
8986 	intel_hdcp_component_fini(i915);
8987 
8988 	intel_mode_config_cleanup(i915);
8989 
8990 	intel_overlay_cleanup(i915);
8991 
8992 	intel_gmbus_teardown(i915);
8993 
8994 	destroy_workqueue(i915->display.wq.flip);
8995 	destroy_workqueue(i915->display.wq.modeset);
8996 
8997 	intel_fbc_cleanup(i915);
8998 }
8999 
9000 /* part #3: call after gem init */
9001 void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
9002 {
9003 	intel_dmc_ucode_fini(i915);
9004 
9005 	intel_power_domains_driver_remove(i915);
9006 
9007 	intel_vga_unregister(i915);
9008 
9009 	intel_bios_driver_remove(i915);
9010 }
9011 
9012 bool intel_modeset_probe_defer(struct pci_dev *pdev)
9013 {
9014 	struct drm_privacy_screen *privacy_screen;
9015 
9016 	/*
9017 	 * apple-gmux is needed on dual GPU MacBook Pro
9018 	 * to probe the panel if we're the inactive GPU.
9019 	 */
9020 	if (vga_switcheroo_client_probe_defer(pdev))
9021 		return true;
9022 
9023 	/* If the LCD panel has a privacy-screen, wait for it */
9024 	privacy_screen = drm_privacy_screen_get(&pdev->dev, NULL);
9025 	if (IS_ERR(privacy_screen) && PTR_ERR(privacy_screen) == -EPROBE_DEFER)
9026 		return true;
9027 
9028 	drm_privacy_screen_put(privacy_screen);
9029 
9030 	return false;
9031 }
9032 
9033 void intel_display_driver_register(struct drm_i915_private *i915)
9034 {
9035 	if (!HAS_DISPLAY(i915))
9036 		return;
9037 
9038 	intel_display_debugfs_register(i915);
9039 
9040 	/* Must be done after probing outputs */
9041 	intel_opregion_register(i915);
9042 	intel_acpi_video_register(i915);
9043 
9044 	intel_audio_init(i915);
9045 
9046 	/*
9047 	 * Some ports require correctly set-up hpd registers for
9048 	 * detection to work properly (leading to ghost connected
9049 	 * connector status), e.g. VGA on gm45.  Hence we can only set
9050 	 * up the initial fbdev config after hpd irqs are fully
9051 	 * enabled. We do it last so that the async config cannot run
9052 	 * before the connectors are registered.
9053 	 */
9054 	intel_fbdev_initial_config_async(&i915->drm);
9055 
9056 	/*
9057 	 * We need to coordinate the hotplugs with the asynchronous
9058 	 * fbdev configuration, for which we use the
9059 	 * fbdev->async_cookie.
9060 	 */
9061 	drm_kms_helper_poll_init(&i915->drm);
9062 }
9063 
9064 void intel_display_driver_unregister(struct drm_i915_private *i915)
9065 {
9066 	if (!HAS_DISPLAY(i915))
9067 		return;
9068 
9069 	intel_fbdev_unregister(i915);
9070 	intel_audio_deinit(i915);
9071 
9072 	/*
9073 	 * After flushing the fbdev (incl. a late async config which
9074 	 * will have delayed queuing of a hotplug event), then flush
9075 	 * the hotplug events.
9076 	 */
9077 	drm_kms_helper_poll_fini(&i915->drm);
9078 	drm_atomic_helper_shutdown(&i915->drm);
9079 
9080 	acpi_video_unregister();
9081 	intel_opregion_unregister(i915);
9082 }
9083 
9084 bool intel_scanout_needs_vtd_wa(struct drm_i915_private *i915)
9085 {
9086 	return DISPLAY_VER(i915) >= 6 && i915_vtd_active(i915);
9087 }
9088