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