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