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