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