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/intel-iommu.h>
31 #include <linux/kernel.h>
32 #include <linux/module.h>
33 #include <linux/dma-resv.h>
34 #include <linux/slab.h>
35 
36 #include <drm/drm_atomic.h>
37 #include <drm/drm_atomic_helper.h>
38 #include <drm/drm_atomic_uapi.h>
39 #include <drm/drm_damage_helper.h>
40 #include <drm/drm_dp_helper.h>
41 #include <drm/drm_edid.h>
42 #include <drm/drm_fourcc.h>
43 #include <drm/drm_plane_helper.h>
44 #include <drm/drm_probe_helper.h>
45 #include <drm/drm_rect.h>
46 
47 #include "display/intel_audio.h"
48 #include "display/intel_crt.h"
49 #include "display/intel_ddi.h"
50 #include "display/intel_display_debugfs.h"
51 #include "display/intel_dp.h"
52 #include "display/intel_dp_mst.h"
53 #include "display/intel_dpll.h"
54 #include "display/intel_dpll_mgr.h"
55 #include "display/intel_dsi.h"
56 #include "display/intel_dvo.h"
57 #include "display/intel_gmbus.h"
58 #include "display/intel_hdmi.h"
59 #include "display/intel_lvds.h"
60 #include "display/intel_sdvo.h"
61 #include "display/intel_tv.h"
62 #include "display/intel_vdsc.h"
63 #include "display/intel_vrr.h"
64 
65 #include "gem/i915_gem_object.h"
66 
67 #include "gt/intel_rps.h"
68 
69 #include "i915_drv.h"
70 #include "intel_acpi.h"
71 #include "intel_atomic.h"
72 #include "intel_atomic_plane.h"
73 #include "intel_bw.h"
74 #include "intel_cdclk.h"
75 #include "intel_color.h"
76 #include "intel_crtc.h"
77 #include "intel_csr.h"
78 #include "intel_display_types.h"
79 #include "intel_dp_link_training.h"
80 #include "intel_fbc.h"
81 #include "intel_fdi.h"
82 #include "intel_fbdev.h"
83 #include "intel_fifo_underrun.h"
84 #include "intel_frontbuffer.h"
85 #include "intel_hdcp.h"
86 #include "intel_hotplug.h"
87 #include "intel_overlay.h"
88 #include "intel_pipe_crc.h"
89 #include "intel_pm.h"
90 #include "intel_pps.h"
91 #include "intel_psr.h"
92 #include "intel_quirks.h"
93 #include "intel_sideband.h"
94 #include "intel_sprite.h"
95 #include "intel_tc.h"
96 #include "intel_vga.h"
97 #include "i9xx_plane.h"
98 #include "skl_scaler.h"
99 #include "skl_universal_plane.h"
100 
101 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
102 				struct intel_crtc_state *pipe_config);
103 static void ilk_pch_clock_get(struct intel_crtc *crtc,
104 			      struct intel_crtc_state *pipe_config);
105 
106 static int intel_framebuffer_init(struct intel_framebuffer *ifb,
107 				  struct drm_i915_gem_object *obj,
108 				  struct drm_mode_fb_cmd2 *mode_cmd);
109 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state);
110 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state);
111 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
112 					 const struct intel_link_m_n *m_n,
113 					 const struct intel_link_m_n *m2_n2);
114 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state);
115 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state);
116 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state);
117 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state);
118 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state);
119 static void intel_modeset_setup_hw_state(struct drm_device *dev,
120 					 struct drm_modeset_acquire_ctx *ctx);
121 
122 /* returns HPLL frequency in kHz */
123 int vlv_get_hpll_vco(struct drm_i915_private *dev_priv)
124 {
125 	int hpll_freq, vco_freq[] = { 800, 1600, 2000, 2400 };
126 
127 	/* Obtain SKU information */
128 	hpll_freq = vlv_cck_read(dev_priv, CCK_FUSE_REG) &
129 		CCK_FUSE_HPLL_FREQ_MASK;
130 
131 	return vco_freq[hpll_freq] * 1000;
132 }
133 
134 int vlv_get_cck_clock(struct drm_i915_private *dev_priv,
135 		      const char *name, u32 reg, int ref_freq)
136 {
137 	u32 val;
138 	int divider;
139 
140 	val = vlv_cck_read(dev_priv, reg);
141 	divider = val & CCK_FREQUENCY_VALUES;
142 
143 	drm_WARN(&dev_priv->drm, (val & CCK_FREQUENCY_STATUS) !=
144 		 (divider << CCK_FREQUENCY_STATUS_SHIFT),
145 		 "%s change in progress\n", name);
146 
147 	return DIV_ROUND_CLOSEST(ref_freq << 1, divider + 1);
148 }
149 
150 int vlv_get_cck_clock_hpll(struct drm_i915_private *dev_priv,
151 			   const char *name, u32 reg)
152 {
153 	int hpll;
154 
155 	vlv_cck_get(dev_priv);
156 
157 	if (dev_priv->hpll_freq == 0)
158 		dev_priv->hpll_freq = vlv_get_hpll_vco(dev_priv);
159 
160 	hpll = vlv_get_cck_clock(dev_priv, name, reg, dev_priv->hpll_freq);
161 
162 	vlv_cck_put(dev_priv);
163 
164 	return hpll;
165 }
166 
167 static void intel_update_czclk(struct drm_i915_private *dev_priv)
168 {
169 	if (!(IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)))
170 		return;
171 
172 	dev_priv->czclk_freq = vlv_get_cck_clock_hpll(dev_priv, "czclk",
173 						      CCK_CZ_CLOCK_CONTROL);
174 
175 	drm_dbg(&dev_priv->drm, "CZ clock rate: %d kHz\n",
176 		dev_priv->czclk_freq);
177 }
178 
179 /* WA Display #0827: Gen9:all */
180 static void
181 skl_wa_827(struct drm_i915_private *dev_priv, enum pipe pipe, bool enable)
182 {
183 	if (enable)
184 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
185 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DUPS1_GATING_DIS | DUPS2_GATING_DIS);
186 	else
187 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
188 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~(DUPS1_GATING_DIS | DUPS2_GATING_DIS));
189 }
190 
191 /* Wa_2006604312:icl,ehl */
192 static void
193 icl_wa_scalerclkgating(struct drm_i915_private *dev_priv, enum pipe pipe,
194 		       bool enable)
195 {
196 	if (enable)
197 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
198 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) | DPFR_GATING_DIS);
199 	else
200 		intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe),
201 		               intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe)) & ~DPFR_GATING_DIS);
202 }
203 
204 static bool
205 is_trans_port_sync_slave(const struct intel_crtc_state *crtc_state)
206 {
207 	return crtc_state->master_transcoder != INVALID_TRANSCODER;
208 }
209 
210 static bool
211 is_trans_port_sync_master(const struct intel_crtc_state *crtc_state)
212 {
213 	return crtc_state->sync_mode_slaves_mask != 0;
214 }
215 
216 bool
217 is_trans_port_sync_mode(const struct intel_crtc_state *crtc_state)
218 {
219 	return is_trans_port_sync_master(crtc_state) ||
220 		is_trans_port_sync_slave(crtc_state);
221 }
222 
223 static bool pipe_scanline_is_moving(struct drm_i915_private *dev_priv,
224 				    enum pipe pipe)
225 {
226 	i915_reg_t reg = PIPEDSL(pipe);
227 	u32 line1, line2;
228 	u32 line_mask;
229 
230 	if (IS_GEN(dev_priv, 2))
231 		line_mask = DSL_LINEMASK_GEN2;
232 	else
233 		line_mask = DSL_LINEMASK_GEN3;
234 
235 	line1 = intel_de_read(dev_priv, reg) & line_mask;
236 	msleep(5);
237 	line2 = intel_de_read(dev_priv, reg) & line_mask;
238 
239 	return line1 != line2;
240 }
241 
242 static void wait_for_pipe_scanline_moving(struct intel_crtc *crtc, bool state)
243 {
244 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
245 	enum pipe pipe = crtc->pipe;
246 
247 	/* Wait for the display line to settle/start moving */
248 	if (wait_for(pipe_scanline_is_moving(dev_priv, pipe) == state, 100))
249 		drm_err(&dev_priv->drm,
250 			"pipe %c scanline %s wait timed out\n",
251 			pipe_name(pipe), onoff(state));
252 }
253 
254 static void intel_wait_for_pipe_scanline_stopped(struct intel_crtc *crtc)
255 {
256 	wait_for_pipe_scanline_moving(crtc, false);
257 }
258 
259 static void intel_wait_for_pipe_scanline_moving(struct intel_crtc *crtc)
260 {
261 	wait_for_pipe_scanline_moving(crtc, true);
262 }
263 
264 static void
265 intel_wait_for_pipe_off(const struct intel_crtc_state *old_crtc_state)
266 {
267 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
268 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
269 
270 	if (INTEL_GEN(dev_priv) >= 4) {
271 		enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
272 		i915_reg_t reg = PIPECONF(cpu_transcoder);
273 
274 		/* Wait for the Pipe State to go off */
275 		if (intel_de_wait_for_clear(dev_priv, reg,
276 					    I965_PIPECONF_ACTIVE, 100))
277 			drm_WARN(&dev_priv->drm, 1,
278 				 "pipe_off wait timed out\n");
279 	} else {
280 		intel_wait_for_pipe_scanline_stopped(crtc);
281 	}
282 }
283 
284 /* Only for pre-ILK configs */
285 void assert_pll(struct drm_i915_private *dev_priv,
286 		enum pipe pipe, bool state)
287 {
288 	u32 val;
289 	bool cur_state;
290 
291 	val = intel_de_read(dev_priv, DPLL(pipe));
292 	cur_state = !!(val & DPLL_VCO_ENABLE);
293 	I915_STATE_WARN(cur_state != state,
294 	     "PLL state assertion failure (expected %s, current %s)\n",
295 			onoff(state), onoff(cur_state));
296 }
297 
298 /* XXX: the dsi pll is shared between MIPI DSI ports */
299 void assert_dsi_pll(struct drm_i915_private *dev_priv, bool state)
300 {
301 	u32 val;
302 	bool cur_state;
303 
304 	vlv_cck_get(dev_priv);
305 	val = vlv_cck_read(dev_priv, CCK_REG_DSI_PLL_CONTROL);
306 	vlv_cck_put(dev_priv);
307 
308 	cur_state = val & DSI_PLL_VCO_EN;
309 	I915_STATE_WARN(cur_state != state,
310 	     "DSI PLL state assertion failure (expected %s, current %s)\n",
311 			onoff(state), onoff(cur_state));
312 }
313 
314 static void assert_fdi_tx(struct drm_i915_private *dev_priv,
315 			  enum pipe pipe, bool state)
316 {
317 	bool cur_state;
318 
319 	if (HAS_DDI(dev_priv)) {
320 		/*
321 		 * DDI does not have a specific FDI_TX register.
322 		 *
323 		 * FDI is never fed from EDP transcoder
324 		 * so pipe->transcoder cast is fine here.
325 		 */
326 		enum transcoder cpu_transcoder = (enum transcoder)pipe;
327 		u32 val = intel_de_read(dev_priv,
328 					TRANS_DDI_FUNC_CTL(cpu_transcoder));
329 		cur_state = !!(val & TRANS_DDI_FUNC_ENABLE);
330 	} else {
331 		u32 val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
332 		cur_state = !!(val & FDI_TX_ENABLE);
333 	}
334 	I915_STATE_WARN(cur_state != state,
335 	     "FDI TX state assertion failure (expected %s, current %s)\n",
336 			onoff(state), onoff(cur_state));
337 }
338 #define assert_fdi_tx_enabled(d, p) assert_fdi_tx(d, p, true)
339 #define assert_fdi_tx_disabled(d, p) assert_fdi_tx(d, p, false)
340 
341 static void assert_fdi_rx(struct drm_i915_private *dev_priv,
342 			  enum pipe pipe, bool state)
343 {
344 	u32 val;
345 	bool cur_state;
346 
347 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
348 	cur_state = !!(val & FDI_RX_ENABLE);
349 	I915_STATE_WARN(cur_state != state,
350 	     "FDI RX state assertion failure (expected %s, current %s)\n",
351 			onoff(state), onoff(cur_state));
352 }
353 #define assert_fdi_rx_enabled(d, p) assert_fdi_rx(d, p, true)
354 #define assert_fdi_rx_disabled(d, p) assert_fdi_rx(d, p, false)
355 
356 static void assert_fdi_tx_pll_enabled(struct drm_i915_private *dev_priv,
357 				      enum pipe pipe)
358 {
359 	u32 val;
360 
361 	/* ILK FDI PLL is always enabled */
362 	if (IS_GEN(dev_priv, 5))
363 		return;
364 
365 	/* On Haswell, DDI ports are responsible for the FDI PLL setup */
366 	if (HAS_DDI(dev_priv))
367 		return;
368 
369 	val = intel_de_read(dev_priv, FDI_TX_CTL(pipe));
370 	I915_STATE_WARN(!(val & FDI_TX_PLL_ENABLE), "FDI TX PLL assertion failure, should be active but is disabled\n");
371 }
372 
373 void assert_fdi_rx_pll(struct drm_i915_private *dev_priv,
374 		       enum pipe pipe, bool state)
375 {
376 	u32 val;
377 	bool cur_state;
378 
379 	val = intel_de_read(dev_priv, FDI_RX_CTL(pipe));
380 	cur_state = !!(val & FDI_RX_PLL_ENABLE);
381 	I915_STATE_WARN(cur_state != state,
382 	     "FDI RX PLL assertion failure (expected %s, current %s)\n",
383 			onoff(state), onoff(cur_state));
384 }
385 
386 void assert_panel_unlocked(struct drm_i915_private *dev_priv, enum pipe pipe)
387 {
388 	i915_reg_t pp_reg;
389 	u32 val;
390 	enum pipe panel_pipe = INVALID_PIPE;
391 	bool locked = true;
392 
393 	if (drm_WARN_ON(&dev_priv->drm, HAS_DDI(dev_priv)))
394 		return;
395 
396 	if (HAS_PCH_SPLIT(dev_priv)) {
397 		u32 port_sel;
398 
399 		pp_reg = PP_CONTROL(0);
400 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
401 
402 		switch (port_sel) {
403 		case PANEL_PORT_SELECT_LVDS:
404 			intel_lvds_port_enabled(dev_priv, PCH_LVDS, &panel_pipe);
405 			break;
406 		case PANEL_PORT_SELECT_DPA:
407 			intel_dp_port_enabled(dev_priv, DP_A, PORT_A, &panel_pipe);
408 			break;
409 		case PANEL_PORT_SELECT_DPC:
410 			intel_dp_port_enabled(dev_priv, PCH_DP_C, PORT_C, &panel_pipe);
411 			break;
412 		case PANEL_PORT_SELECT_DPD:
413 			intel_dp_port_enabled(dev_priv, PCH_DP_D, PORT_D, &panel_pipe);
414 			break;
415 		default:
416 			MISSING_CASE(port_sel);
417 			break;
418 		}
419 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
420 		/* presumably write lock depends on pipe, not port select */
421 		pp_reg = PP_CONTROL(pipe);
422 		panel_pipe = pipe;
423 	} else {
424 		u32 port_sel;
425 
426 		pp_reg = PP_CONTROL(0);
427 		port_sel = intel_de_read(dev_priv, PP_ON_DELAYS(0)) & PANEL_PORT_SELECT_MASK;
428 
429 		drm_WARN_ON(&dev_priv->drm,
430 			    port_sel != PANEL_PORT_SELECT_LVDS);
431 		intel_lvds_port_enabled(dev_priv, LVDS, &panel_pipe);
432 	}
433 
434 	val = intel_de_read(dev_priv, pp_reg);
435 	if (!(val & PANEL_POWER_ON) ||
436 	    ((val & PANEL_UNLOCK_MASK) == PANEL_UNLOCK_REGS))
437 		locked = false;
438 
439 	I915_STATE_WARN(panel_pipe == pipe && locked,
440 	     "panel assertion failure, pipe %c regs locked\n",
441 	     pipe_name(pipe));
442 }
443 
444 void assert_pipe(struct drm_i915_private *dev_priv,
445 		 enum transcoder cpu_transcoder, bool state)
446 {
447 	bool cur_state;
448 	enum intel_display_power_domain power_domain;
449 	intel_wakeref_t wakeref;
450 
451 	/* we keep both pipes enabled on 830 */
452 	if (IS_I830(dev_priv))
453 		state = true;
454 
455 	power_domain = POWER_DOMAIN_TRANSCODER(cpu_transcoder);
456 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
457 	if (wakeref) {
458 		u32 val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
459 		cur_state = !!(val & PIPECONF_ENABLE);
460 
461 		intel_display_power_put(dev_priv, power_domain, wakeref);
462 	} else {
463 		cur_state = false;
464 	}
465 
466 	I915_STATE_WARN(cur_state != state,
467 			"transcoder %s assertion failure (expected %s, current %s)\n",
468 			transcoder_name(cpu_transcoder),
469 			onoff(state), onoff(cur_state));
470 }
471 
472 static void assert_plane(struct intel_plane *plane, bool state)
473 {
474 	enum pipe pipe;
475 	bool cur_state;
476 
477 	cur_state = plane->get_hw_state(plane, &pipe);
478 
479 	I915_STATE_WARN(cur_state != state,
480 			"%s assertion failure (expected %s, current %s)\n",
481 			plane->base.name, onoff(state), onoff(cur_state));
482 }
483 
484 #define assert_plane_enabled(p) assert_plane(p, true)
485 #define assert_plane_disabled(p) assert_plane(p, false)
486 
487 static void assert_planes_disabled(struct intel_crtc *crtc)
488 {
489 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
490 	struct intel_plane *plane;
491 
492 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane)
493 		assert_plane_disabled(plane);
494 }
495 
496 void assert_pch_transcoder_disabled(struct drm_i915_private *dev_priv,
497 				    enum pipe pipe)
498 {
499 	u32 val;
500 	bool enabled;
501 
502 	val = intel_de_read(dev_priv, PCH_TRANSCONF(pipe));
503 	enabled = !!(val & TRANS_ENABLE);
504 	I915_STATE_WARN(enabled,
505 	     "transcoder assertion failed, should be off on pipe %c but is still active\n",
506 	     pipe_name(pipe));
507 }
508 
509 static void assert_pch_dp_disabled(struct drm_i915_private *dev_priv,
510 				   enum pipe pipe, enum port port,
511 				   i915_reg_t dp_reg)
512 {
513 	enum pipe port_pipe;
514 	bool state;
515 
516 	state = intel_dp_port_enabled(dev_priv, dp_reg, port, &port_pipe);
517 
518 	I915_STATE_WARN(state && port_pipe == pipe,
519 			"PCH DP %c enabled on transcoder %c, should be disabled\n",
520 			port_name(port), pipe_name(pipe));
521 
522 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
523 			"IBX PCH DP %c still using transcoder B\n",
524 			port_name(port));
525 }
526 
527 static void assert_pch_hdmi_disabled(struct drm_i915_private *dev_priv,
528 				     enum pipe pipe, enum port port,
529 				     i915_reg_t hdmi_reg)
530 {
531 	enum pipe port_pipe;
532 	bool state;
533 
534 	state = intel_sdvo_port_enabled(dev_priv, hdmi_reg, &port_pipe);
535 
536 	I915_STATE_WARN(state && port_pipe == pipe,
537 			"PCH HDMI %c enabled on transcoder %c, should be disabled\n",
538 			port_name(port), pipe_name(pipe));
539 
540 	I915_STATE_WARN(HAS_PCH_IBX(dev_priv) && !state && port_pipe == PIPE_B,
541 			"IBX PCH HDMI %c still using transcoder B\n",
542 			port_name(port));
543 }
544 
545 static void assert_pch_ports_disabled(struct drm_i915_private *dev_priv,
546 				      enum pipe pipe)
547 {
548 	enum pipe port_pipe;
549 
550 	assert_pch_dp_disabled(dev_priv, pipe, PORT_B, PCH_DP_B);
551 	assert_pch_dp_disabled(dev_priv, pipe, PORT_C, PCH_DP_C);
552 	assert_pch_dp_disabled(dev_priv, pipe, PORT_D, PCH_DP_D);
553 
554 	I915_STATE_WARN(intel_crt_port_enabled(dev_priv, PCH_ADPA, &port_pipe) &&
555 			port_pipe == pipe,
556 			"PCH VGA enabled on transcoder %c, should be disabled\n",
557 			pipe_name(pipe));
558 
559 	I915_STATE_WARN(intel_lvds_port_enabled(dev_priv, PCH_LVDS, &port_pipe) &&
560 			port_pipe == pipe,
561 			"PCH LVDS enabled on transcoder %c, should be disabled\n",
562 			pipe_name(pipe));
563 
564 	/* PCH SDVOB multiplex with HDMIB */
565 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_B, PCH_HDMIB);
566 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_C, PCH_HDMIC);
567 	assert_pch_hdmi_disabled(dev_priv, pipe, PORT_D, PCH_HDMID);
568 }
569 
570 void vlv_wait_port_ready(struct drm_i915_private *dev_priv,
571 			 struct intel_digital_port *dig_port,
572 			 unsigned int expected_mask)
573 {
574 	u32 port_mask;
575 	i915_reg_t dpll_reg;
576 
577 	switch (dig_port->base.port) {
578 	case PORT_B:
579 		port_mask = DPLL_PORTB_READY_MASK;
580 		dpll_reg = DPLL(0);
581 		break;
582 	case PORT_C:
583 		port_mask = DPLL_PORTC_READY_MASK;
584 		dpll_reg = DPLL(0);
585 		expected_mask <<= 4;
586 		break;
587 	case PORT_D:
588 		port_mask = DPLL_PORTD_READY_MASK;
589 		dpll_reg = DPIO_PHY_STATUS;
590 		break;
591 	default:
592 		BUG();
593 	}
594 
595 	if (intel_de_wait_for_register(dev_priv, dpll_reg,
596 				       port_mask, expected_mask, 1000))
597 		drm_WARN(&dev_priv->drm, 1,
598 			 "timed out waiting for [ENCODER:%d:%s] port ready: got 0x%x, expected 0x%x\n",
599 			 dig_port->base.base.base.id, dig_port->base.base.name,
600 			 intel_de_read(dev_priv, dpll_reg) & port_mask,
601 			 expected_mask);
602 }
603 
604 static void ilk_enable_pch_transcoder(const struct intel_crtc_state *crtc_state)
605 {
606 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
607 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
608 	enum pipe pipe = crtc->pipe;
609 	i915_reg_t reg;
610 	u32 val, pipeconf_val;
611 
612 	/* Make sure PCH DPLL is enabled */
613 	assert_shared_dpll_enabled(dev_priv, crtc_state->shared_dpll);
614 
615 	/* FDI must be feeding us bits for PCH ports */
616 	assert_fdi_tx_enabled(dev_priv, pipe);
617 	assert_fdi_rx_enabled(dev_priv, pipe);
618 
619 	if (HAS_PCH_CPT(dev_priv)) {
620 		reg = TRANS_CHICKEN2(pipe);
621 		val = intel_de_read(dev_priv, reg);
622 		/*
623 		 * Workaround: Set the timing override bit
624 		 * before enabling the pch transcoder.
625 		 */
626 		val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
627 		/* Configure frame start delay to match the CPU */
628 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
629 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
630 		intel_de_write(dev_priv, reg, val);
631 	}
632 
633 	reg = PCH_TRANSCONF(pipe);
634 	val = intel_de_read(dev_priv, reg);
635 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(pipe));
636 
637 	if (HAS_PCH_IBX(dev_priv)) {
638 		/* Configure frame start delay to match the CPU */
639 		val &= ~TRANS_FRAME_START_DELAY_MASK;
640 		val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
641 
642 		/*
643 		 * Make the BPC in transcoder be consistent with
644 		 * that in pipeconf reg. For HDMI we must use 8bpc
645 		 * here for both 8bpc and 12bpc.
646 		 */
647 		val &= ~PIPECONF_BPC_MASK;
648 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI))
649 			val |= PIPECONF_8BPC;
650 		else
651 			val |= pipeconf_val & PIPECONF_BPC_MASK;
652 	}
653 
654 	val &= ~TRANS_INTERLACE_MASK;
655 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK) == PIPECONF_INTERLACED_ILK) {
656 		if (HAS_PCH_IBX(dev_priv) &&
657 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
658 			val |= TRANS_LEGACY_INTERLACED_ILK;
659 		else
660 			val |= TRANS_INTERLACED;
661 	} else {
662 		val |= TRANS_PROGRESSIVE;
663 	}
664 
665 	intel_de_write(dev_priv, reg, val | TRANS_ENABLE);
666 	if (intel_de_wait_for_set(dev_priv, reg, TRANS_STATE_ENABLE, 100))
667 		drm_err(&dev_priv->drm, "failed to enable transcoder %c\n",
668 			pipe_name(pipe));
669 }
670 
671 static void lpt_enable_pch_transcoder(struct drm_i915_private *dev_priv,
672 				      enum transcoder cpu_transcoder)
673 {
674 	u32 val, pipeconf_val;
675 
676 	/* FDI must be feeding us bits for PCH ports */
677 	assert_fdi_tx_enabled(dev_priv, (enum pipe) cpu_transcoder);
678 	assert_fdi_rx_enabled(dev_priv, PIPE_A);
679 
680 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
681 	/* Workaround: set timing override bit. */
682 	val |= TRANS_CHICKEN2_TIMING_OVERRIDE;
683 	/* Configure frame start delay to match the CPU */
684 	val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
685 	val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
686 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
687 
688 	val = TRANS_ENABLE;
689 	pipeconf_val = intel_de_read(dev_priv, PIPECONF(cpu_transcoder));
690 
691 	if ((pipeconf_val & PIPECONF_INTERLACE_MASK_HSW) ==
692 	    PIPECONF_INTERLACED_ILK)
693 		val |= TRANS_INTERLACED;
694 	else
695 		val |= TRANS_PROGRESSIVE;
696 
697 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
698 	if (intel_de_wait_for_set(dev_priv, LPT_TRANSCONF,
699 				  TRANS_STATE_ENABLE, 100))
700 		drm_err(&dev_priv->drm, "Failed to enable PCH transcoder\n");
701 }
702 
703 static void ilk_disable_pch_transcoder(struct drm_i915_private *dev_priv,
704 				       enum pipe pipe)
705 {
706 	i915_reg_t reg;
707 	u32 val;
708 
709 	/* FDI relies on the transcoder */
710 	assert_fdi_tx_disabled(dev_priv, pipe);
711 	assert_fdi_rx_disabled(dev_priv, pipe);
712 
713 	/* Ports must be off as well */
714 	assert_pch_ports_disabled(dev_priv, pipe);
715 
716 	reg = PCH_TRANSCONF(pipe);
717 	val = intel_de_read(dev_priv, reg);
718 	val &= ~TRANS_ENABLE;
719 	intel_de_write(dev_priv, reg, val);
720 	/* wait for PCH transcoder off, transcoder state */
721 	if (intel_de_wait_for_clear(dev_priv, reg, TRANS_STATE_ENABLE, 50))
722 		drm_err(&dev_priv->drm, "failed to disable transcoder %c\n",
723 			pipe_name(pipe));
724 
725 	if (HAS_PCH_CPT(dev_priv)) {
726 		/* Workaround: Clear the timing override chicken bit again. */
727 		reg = TRANS_CHICKEN2(pipe);
728 		val = intel_de_read(dev_priv, reg);
729 		val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
730 		intel_de_write(dev_priv, reg, val);
731 	}
732 }
733 
734 void lpt_disable_pch_transcoder(struct drm_i915_private *dev_priv)
735 {
736 	u32 val;
737 
738 	val = intel_de_read(dev_priv, LPT_TRANSCONF);
739 	val &= ~TRANS_ENABLE;
740 	intel_de_write(dev_priv, LPT_TRANSCONF, val);
741 	/* wait for PCH transcoder off, transcoder state */
742 	if (intel_de_wait_for_clear(dev_priv, LPT_TRANSCONF,
743 				    TRANS_STATE_ENABLE, 50))
744 		drm_err(&dev_priv->drm, "Failed to disable PCH transcoder\n");
745 
746 	/* Workaround: clear timing override bit. */
747 	val = intel_de_read(dev_priv, TRANS_CHICKEN2(PIPE_A));
748 	val &= ~TRANS_CHICKEN2_TIMING_OVERRIDE;
749 	intel_de_write(dev_priv, TRANS_CHICKEN2(PIPE_A), val);
750 }
751 
752 enum pipe intel_crtc_pch_transcoder(struct intel_crtc *crtc)
753 {
754 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
755 
756 	if (HAS_PCH_LPT(dev_priv))
757 		return PIPE_A;
758 	else
759 		return crtc->pipe;
760 }
761 
762 void intel_enable_pipe(const struct intel_crtc_state *new_crtc_state)
763 {
764 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
765 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
766 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
767 	enum pipe pipe = crtc->pipe;
768 	i915_reg_t reg;
769 	u32 val;
770 
771 	drm_dbg_kms(&dev_priv->drm, "enabling pipe %c\n", pipe_name(pipe));
772 
773 	assert_planes_disabled(crtc);
774 
775 	/*
776 	 * A pipe without a PLL won't actually be able to drive bits from
777 	 * a plane.  On ILK+ the pipe PLLs are integrated, so we don't
778 	 * need the check.
779 	 */
780 	if (HAS_GMCH(dev_priv)) {
781 		if (intel_crtc_has_type(new_crtc_state, INTEL_OUTPUT_DSI))
782 			assert_dsi_pll_enabled(dev_priv);
783 		else
784 			assert_pll_enabled(dev_priv, pipe);
785 	} else {
786 		if (new_crtc_state->has_pch_encoder) {
787 			/* if driving the PCH, we need FDI enabled */
788 			assert_fdi_rx_pll_enabled(dev_priv,
789 						  intel_crtc_pch_transcoder(crtc));
790 			assert_fdi_tx_pll_enabled(dev_priv,
791 						  (enum pipe) cpu_transcoder);
792 		}
793 		/* FIXME: assert CPU port conditions for SNB+ */
794 	}
795 
796 	reg = PIPECONF(cpu_transcoder);
797 	val = intel_de_read(dev_priv, reg);
798 	if (val & PIPECONF_ENABLE) {
799 		/* we keep both pipes enabled on 830 */
800 		drm_WARN_ON(&dev_priv->drm, !IS_I830(dev_priv));
801 		return;
802 	}
803 
804 	intel_de_write(dev_priv, reg, val | PIPECONF_ENABLE);
805 	intel_de_posting_read(dev_priv, reg);
806 
807 	/*
808 	 * Until the pipe starts PIPEDSL reads will return a stale value,
809 	 * which causes an apparent vblank timestamp jump when PIPEDSL
810 	 * resets to its proper value. That also messes up the frame count
811 	 * when it's derived from the timestamps. So let's wait for the
812 	 * pipe to start properly before we call drm_crtc_vblank_on()
813 	 */
814 	if (intel_crtc_max_vblank_count(new_crtc_state) == 0)
815 		intel_wait_for_pipe_scanline_moving(crtc);
816 }
817 
818 void intel_disable_pipe(const struct intel_crtc_state *old_crtc_state)
819 {
820 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
821 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
822 	enum transcoder cpu_transcoder = old_crtc_state->cpu_transcoder;
823 	enum pipe pipe = crtc->pipe;
824 	i915_reg_t reg;
825 	u32 val;
826 
827 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c\n", pipe_name(pipe));
828 
829 	/*
830 	 * Make sure planes won't keep trying to pump pixels to us,
831 	 * or we might hang the display.
832 	 */
833 	assert_planes_disabled(crtc);
834 
835 	reg = PIPECONF(cpu_transcoder);
836 	val = intel_de_read(dev_priv, reg);
837 	if ((val & PIPECONF_ENABLE) == 0)
838 		return;
839 
840 	/*
841 	 * Double wide has implications for planes
842 	 * so best keep it disabled when not needed.
843 	 */
844 	if (old_crtc_state->double_wide)
845 		val &= ~PIPECONF_DOUBLE_WIDE;
846 
847 	/* Don't disable pipe or pipe PLLs if needed */
848 	if (!IS_I830(dev_priv))
849 		val &= ~PIPECONF_ENABLE;
850 
851 	intel_de_write(dev_priv, reg, val);
852 	if ((val & PIPECONF_ENABLE) == 0)
853 		intel_wait_for_pipe_off(old_crtc_state);
854 }
855 
856 static unsigned int intel_tile_size(const struct drm_i915_private *dev_priv)
857 {
858 	return IS_GEN(dev_priv, 2) ? 2048 : 4096;
859 }
860 
861 static bool is_aux_plane(const struct drm_framebuffer *fb, int plane)
862 {
863 	if (is_ccs_modifier(fb->modifier))
864 		return is_ccs_plane(fb, plane);
865 
866 	return plane == 1;
867 }
868 
869 bool
870 intel_format_info_is_yuv_semiplanar(const struct drm_format_info *info,
871 				    u64 modifier)
872 {
873 	return info->is_yuv &&
874 	       info->num_planes == (is_ccs_modifier(modifier) ? 4 : 2);
875 }
876 
877 static bool is_semiplanar_uv_plane(const struct drm_framebuffer *fb,
878 				   int color_plane)
879 {
880 	return intel_format_info_is_yuv_semiplanar(fb->format, fb->modifier) &&
881 	       color_plane == 1;
882 }
883 
884 unsigned int
885 intel_tile_width_bytes(const struct drm_framebuffer *fb, int color_plane)
886 {
887 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
888 	unsigned int cpp = fb->format->cpp[color_plane];
889 
890 	switch (fb->modifier) {
891 	case DRM_FORMAT_MOD_LINEAR:
892 		return intel_tile_size(dev_priv);
893 	case I915_FORMAT_MOD_X_TILED:
894 		if (IS_GEN(dev_priv, 2))
895 			return 128;
896 		else
897 			return 512;
898 	case I915_FORMAT_MOD_Y_TILED_CCS:
899 		if (is_ccs_plane(fb, color_plane))
900 			return 128;
901 		fallthrough;
902 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
903 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
904 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
905 		if (is_ccs_plane(fb, color_plane))
906 			return 64;
907 		fallthrough;
908 	case I915_FORMAT_MOD_Y_TILED:
909 		if (IS_GEN(dev_priv, 2) || HAS_128_BYTE_Y_TILING(dev_priv))
910 			return 128;
911 		else
912 			return 512;
913 	case I915_FORMAT_MOD_Yf_TILED_CCS:
914 		if (is_ccs_plane(fb, color_plane))
915 			return 128;
916 		fallthrough;
917 	case I915_FORMAT_MOD_Yf_TILED:
918 		switch (cpp) {
919 		case 1:
920 			return 64;
921 		case 2:
922 		case 4:
923 			return 128;
924 		case 8:
925 		case 16:
926 			return 256;
927 		default:
928 			MISSING_CASE(cpp);
929 			return cpp;
930 		}
931 		break;
932 	default:
933 		MISSING_CASE(fb->modifier);
934 		return cpp;
935 	}
936 }
937 
938 unsigned int
939 intel_tile_height(const struct drm_framebuffer *fb, int color_plane)
940 {
941 	if (is_gen12_ccs_plane(fb, color_plane))
942 		return 1;
943 
944 	return intel_tile_size(to_i915(fb->dev)) /
945 		intel_tile_width_bytes(fb, color_plane);
946 }
947 
948 /* Return the tile dimensions in pixel units */
949 static void intel_tile_dims(const struct drm_framebuffer *fb, int color_plane,
950 			    unsigned int *tile_width,
951 			    unsigned int *tile_height)
952 {
953 	unsigned int tile_width_bytes = intel_tile_width_bytes(fb, color_plane);
954 	unsigned int cpp = fb->format->cpp[color_plane];
955 
956 	*tile_width = tile_width_bytes / cpp;
957 	*tile_height = intel_tile_height(fb, color_plane);
958 }
959 
960 static unsigned int intel_tile_row_size(const struct drm_framebuffer *fb,
961 					int color_plane)
962 {
963 	unsigned int tile_width, tile_height;
964 
965 	intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
966 
967 	return fb->pitches[color_plane] * tile_height;
968 }
969 
970 unsigned int
971 intel_fb_align_height(const struct drm_framebuffer *fb,
972 		      int color_plane, unsigned int height)
973 {
974 	unsigned int tile_height = intel_tile_height(fb, color_plane);
975 
976 	return ALIGN(height, tile_height);
977 }
978 
979 unsigned int intel_rotation_info_size(const struct intel_rotation_info *rot_info)
980 {
981 	unsigned int size = 0;
982 	int i;
983 
984 	for (i = 0 ; i < ARRAY_SIZE(rot_info->plane); i++)
985 		size += rot_info->plane[i].width * rot_info->plane[i].height;
986 
987 	return size;
988 }
989 
990 unsigned int intel_remapped_info_size(const struct intel_remapped_info *rem_info)
991 {
992 	unsigned int size = 0;
993 	int i;
994 
995 	for (i = 0 ; i < ARRAY_SIZE(rem_info->plane); i++)
996 		size += rem_info->plane[i].width * rem_info->plane[i].height;
997 
998 	return size;
999 }
1000 
1001 static void
1002 intel_fill_fb_ggtt_view(struct i915_ggtt_view *view,
1003 			const struct drm_framebuffer *fb,
1004 			unsigned int rotation)
1005 {
1006 	view->type = I915_GGTT_VIEW_NORMAL;
1007 	if (drm_rotation_90_or_270(rotation)) {
1008 		view->type = I915_GGTT_VIEW_ROTATED;
1009 		view->rotated = to_intel_framebuffer(fb)->rot_info;
1010 	}
1011 }
1012 
1013 static unsigned int intel_cursor_alignment(const struct drm_i915_private *dev_priv)
1014 {
1015 	if (IS_I830(dev_priv))
1016 		return 16 * 1024;
1017 	else if (IS_I85X(dev_priv))
1018 		return 256;
1019 	else if (IS_I845G(dev_priv) || IS_I865G(dev_priv))
1020 		return 32;
1021 	else
1022 		return 4 * 1024;
1023 }
1024 
1025 static unsigned int intel_linear_alignment(const struct drm_i915_private *dev_priv)
1026 {
1027 	if (INTEL_GEN(dev_priv) >= 9)
1028 		return 256 * 1024;
1029 	else if (IS_I965G(dev_priv) || IS_I965GM(dev_priv) ||
1030 		 IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
1031 		return 128 * 1024;
1032 	else if (INTEL_GEN(dev_priv) >= 4)
1033 		return 4 * 1024;
1034 	else
1035 		return 0;
1036 }
1037 
1038 static bool has_async_flips(struct drm_i915_private *i915)
1039 {
1040 	return INTEL_GEN(i915) >= 5;
1041 }
1042 
1043 unsigned int intel_surf_alignment(const struct drm_framebuffer *fb,
1044 				  int color_plane)
1045 {
1046 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
1047 
1048 	/* AUX_DIST needs only 4K alignment */
1049 	if ((INTEL_GEN(dev_priv) < 12 && is_aux_plane(fb, color_plane)) ||
1050 	    is_ccs_plane(fb, color_plane))
1051 		return 4096;
1052 
1053 	switch (fb->modifier) {
1054 	case DRM_FORMAT_MOD_LINEAR:
1055 		return intel_linear_alignment(dev_priv);
1056 	case I915_FORMAT_MOD_X_TILED:
1057 		if (has_async_flips(dev_priv))
1058 			return 256 * 1024;
1059 		return 0;
1060 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1061 		if (is_semiplanar_uv_plane(fb, color_plane))
1062 			return intel_tile_row_size(fb, color_plane);
1063 		fallthrough;
1064 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1065 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1066 		return 16 * 1024;
1067 	case I915_FORMAT_MOD_Y_TILED_CCS:
1068 	case I915_FORMAT_MOD_Yf_TILED_CCS:
1069 	case I915_FORMAT_MOD_Y_TILED:
1070 		if (INTEL_GEN(dev_priv) >= 12 &&
1071 		    is_semiplanar_uv_plane(fb, color_plane))
1072 			return intel_tile_row_size(fb, color_plane);
1073 		fallthrough;
1074 	case I915_FORMAT_MOD_Yf_TILED:
1075 		return 1 * 1024 * 1024;
1076 	default:
1077 		MISSING_CASE(fb->modifier);
1078 		return 0;
1079 	}
1080 }
1081 
1082 static bool intel_plane_uses_fence(const struct intel_plane_state *plane_state)
1083 {
1084 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1085 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1086 
1087 	return INTEL_GEN(dev_priv) < 4 ||
1088 		(plane->has_fbc &&
1089 		 plane_state->view.type == I915_GGTT_VIEW_NORMAL);
1090 }
1091 
1092 struct i915_vma *
1093 intel_pin_and_fence_fb_obj(struct drm_framebuffer *fb,
1094 			   const struct i915_ggtt_view *view,
1095 			   bool uses_fence,
1096 			   unsigned long *out_flags)
1097 {
1098 	struct drm_device *dev = fb->dev;
1099 	struct drm_i915_private *dev_priv = to_i915(dev);
1100 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1101 	intel_wakeref_t wakeref;
1102 	struct i915_vma *vma;
1103 	unsigned int pinctl;
1104 	u32 alignment;
1105 
1106 	if (drm_WARN_ON(dev, !i915_gem_object_is_framebuffer(obj)))
1107 		return ERR_PTR(-EINVAL);
1108 
1109 	alignment = intel_surf_alignment(fb, 0);
1110 	if (drm_WARN_ON(dev, alignment && !is_power_of_2(alignment)))
1111 		return ERR_PTR(-EINVAL);
1112 
1113 	/* Note that the w/a also requires 64 PTE of padding following the
1114 	 * bo. We currently fill all unused PTE with the shadow page and so
1115 	 * we should always have valid PTE following the scanout preventing
1116 	 * the VT-d warning.
1117 	 */
1118 	if (intel_scanout_needs_vtd_wa(dev_priv) && alignment < 256 * 1024)
1119 		alignment = 256 * 1024;
1120 
1121 	/*
1122 	 * Global gtt pte registers are special registers which actually forward
1123 	 * writes to a chunk of system memory. Which means that there is no risk
1124 	 * that the register values disappear as soon as we call
1125 	 * intel_runtime_pm_put(), so it is correct to wrap only the
1126 	 * pin/unpin/fence and not more.
1127 	 */
1128 	wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
1129 
1130 	atomic_inc(&dev_priv->gpu_error.pending_fb_pin);
1131 
1132 	/*
1133 	 * Valleyview is definitely limited to scanning out the first
1134 	 * 512MiB. Lets presume this behaviour was inherited from the
1135 	 * g4x display engine and that all earlier gen are similarly
1136 	 * limited. Testing suggests that it is a little more
1137 	 * complicated than this. For example, Cherryview appears quite
1138 	 * happy to scanout from anywhere within its global aperture.
1139 	 */
1140 	pinctl = 0;
1141 	if (HAS_GMCH(dev_priv))
1142 		pinctl |= PIN_MAPPABLE;
1143 
1144 	vma = i915_gem_object_pin_to_display_plane(obj,
1145 						   alignment, view, pinctl);
1146 	if (IS_ERR(vma))
1147 		goto err;
1148 
1149 	if (uses_fence && i915_vma_is_map_and_fenceable(vma)) {
1150 		int ret;
1151 
1152 		/*
1153 		 * Install a fence for tiled scan-out. Pre-i965 always needs a
1154 		 * fence, whereas 965+ only requires a fence if using
1155 		 * framebuffer compression.  For simplicity, we always, when
1156 		 * possible, install a fence as the cost is not that onerous.
1157 		 *
1158 		 * If we fail to fence the tiled scanout, then either the
1159 		 * modeset will reject the change (which is highly unlikely as
1160 		 * the affected systems, all but one, do not have unmappable
1161 		 * space) or we will not be able to enable full powersaving
1162 		 * techniques (also likely not to apply due to various limits
1163 		 * FBC and the like impose on the size of the buffer, which
1164 		 * presumably we violated anyway with this unmappable buffer).
1165 		 * Anyway, it is presumably better to stumble onwards with
1166 		 * something and try to run the system in a "less than optimal"
1167 		 * mode that matches the user configuration.
1168 		 */
1169 		ret = i915_vma_pin_fence(vma);
1170 		if (ret != 0 && INTEL_GEN(dev_priv) < 4) {
1171 			i915_vma_unpin(vma);
1172 			vma = ERR_PTR(ret);
1173 			goto err;
1174 		}
1175 
1176 		if (ret == 0 && vma->fence)
1177 			*out_flags |= PLANE_HAS_FENCE;
1178 	}
1179 
1180 	i915_vma_get(vma);
1181 err:
1182 	atomic_dec(&dev_priv->gpu_error.pending_fb_pin);
1183 	intel_runtime_pm_put(&dev_priv->runtime_pm, wakeref);
1184 	return vma;
1185 }
1186 
1187 void intel_unpin_fb_vma(struct i915_vma *vma, unsigned long flags)
1188 {
1189 	if (flags & PLANE_HAS_FENCE)
1190 		i915_vma_unpin_fence(vma);
1191 	i915_vma_unpin(vma);
1192 	i915_vma_put(vma);
1193 }
1194 
1195 static int intel_fb_pitch(const struct drm_framebuffer *fb, int color_plane,
1196 			  unsigned int rotation)
1197 {
1198 	if (drm_rotation_90_or_270(rotation))
1199 		return to_intel_framebuffer(fb)->rotated[color_plane].pitch;
1200 	else
1201 		return fb->pitches[color_plane];
1202 }
1203 
1204 /*
1205  * Convert the x/y offsets into a linear offset.
1206  * Only valid with 0/180 degree rotation, which is fine since linear
1207  * offset is only used with linear buffers on pre-hsw and tiled buffers
1208  * with gen2/3, and 90/270 degree rotations isn't supported on any of them.
1209  */
1210 u32 intel_fb_xy_to_linear(int x, int y,
1211 			  const struct intel_plane_state *state,
1212 			  int color_plane)
1213 {
1214 	const struct drm_framebuffer *fb = state->hw.fb;
1215 	unsigned int cpp = fb->format->cpp[color_plane];
1216 	unsigned int pitch = state->color_plane[color_plane].stride;
1217 
1218 	return y * pitch + x * cpp;
1219 }
1220 
1221 /*
1222  * Add the x/y offsets derived from fb->offsets[] to the user
1223  * specified plane src x/y offsets. The resulting x/y offsets
1224  * specify the start of scanout from the beginning of the gtt mapping.
1225  */
1226 void intel_add_fb_offsets(int *x, int *y,
1227 			  const struct intel_plane_state *state,
1228 			  int color_plane)
1229 
1230 {
1231 	*x += state->color_plane[color_plane].x;
1232 	*y += state->color_plane[color_plane].y;
1233 }
1234 
1235 static u32 intel_adjust_tile_offset(int *x, int *y,
1236 				    unsigned int tile_width,
1237 				    unsigned int tile_height,
1238 				    unsigned int tile_size,
1239 				    unsigned int pitch_tiles,
1240 				    u32 old_offset,
1241 				    u32 new_offset)
1242 {
1243 	unsigned int pitch_pixels = pitch_tiles * tile_width;
1244 	unsigned int tiles;
1245 
1246 	WARN_ON(old_offset & (tile_size - 1));
1247 	WARN_ON(new_offset & (tile_size - 1));
1248 	WARN_ON(new_offset > old_offset);
1249 
1250 	tiles = (old_offset - new_offset) / tile_size;
1251 
1252 	*y += tiles / pitch_tiles * tile_height;
1253 	*x += tiles % pitch_tiles * tile_width;
1254 
1255 	/* minimize x in case it got needlessly big */
1256 	*y += *x / pitch_pixels * tile_height;
1257 	*x %= pitch_pixels;
1258 
1259 	return new_offset;
1260 }
1261 
1262 static bool is_surface_linear(const struct drm_framebuffer *fb, int color_plane)
1263 {
1264 	return fb->modifier == DRM_FORMAT_MOD_LINEAR ||
1265 	       is_gen12_ccs_plane(fb, color_plane);
1266 }
1267 
1268 static u32 intel_adjust_aligned_offset(int *x, int *y,
1269 				       const struct drm_framebuffer *fb,
1270 				       int color_plane,
1271 				       unsigned int rotation,
1272 				       unsigned int pitch,
1273 				       u32 old_offset, u32 new_offset)
1274 {
1275 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
1276 	unsigned int cpp = fb->format->cpp[color_plane];
1277 
1278 	drm_WARN_ON(&dev_priv->drm, new_offset > old_offset);
1279 
1280 	if (!is_surface_linear(fb, color_plane)) {
1281 		unsigned int tile_size, tile_width, tile_height;
1282 		unsigned int pitch_tiles;
1283 
1284 		tile_size = intel_tile_size(dev_priv);
1285 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
1286 
1287 		if (drm_rotation_90_or_270(rotation)) {
1288 			pitch_tiles = pitch / tile_height;
1289 			swap(tile_width, tile_height);
1290 		} else {
1291 			pitch_tiles = pitch / (tile_width * cpp);
1292 		}
1293 
1294 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
1295 					 tile_size, pitch_tiles,
1296 					 old_offset, new_offset);
1297 	} else {
1298 		old_offset += *y * pitch + *x * cpp;
1299 
1300 		*y = (old_offset - new_offset) / pitch;
1301 		*x = ((old_offset - new_offset) - *y * pitch) / cpp;
1302 	}
1303 
1304 	return new_offset;
1305 }
1306 
1307 /*
1308  * Adjust the tile offset by moving the difference into
1309  * the x/y offsets.
1310  */
1311 u32 intel_plane_adjust_aligned_offset(int *x, int *y,
1312 				      const struct intel_plane_state *state,
1313 				      int color_plane,
1314 				      u32 old_offset, u32 new_offset)
1315 {
1316 	return intel_adjust_aligned_offset(x, y, state->hw.fb, color_plane,
1317 					   state->hw.rotation,
1318 					   state->color_plane[color_plane].stride,
1319 					   old_offset, new_offset);
1320 }
1321 
1322 /*
1323  * Computes the aligned offset to the base tile and adjusts
1324  * x, y. bytes per pixel is assumed to be a power-of-two.
1325  *
1326  * In the 90/270 rotated case, x and y are assumed
1327  * to be already rotated to match the rotated GTT view, and
1328  * pitch is the tile_height aligned framebuffer height.
1329  *
1330  * This function is used when computing the derived information
1331  * under intel_framebuffer, so using any of that information
1332  * here is not allowed. Anything under drm_framebuffer can be
1333  * used. This is why the user has to pass in the pitch since it
1334  * is specified in the rotated orientation.
1335  */
1336 static u32 intel_compute_aligned_offset(struct drm_i915_private *dev_priv,
1337 					int *x, int *y,
1338 					const struct drm_framebuffer *fb,
1339 					int color_plane,
1340 					unsigned int pitch,
1341 					unsigned int rotation,
1342 					u32 alignment)
1343 {
1344 	unsigned int cpp = fb->format->cpp[color_plane];
1345 	u32 offset, offset_aligned;
1346 
1347 	if (!is_surface_linear(fb, color_plane)) {
1348 		unsigned int tile_size, tile_width, tile_height;
1349 		unsigned int tile_rows, tiles, pitch_tiles;
1350 
1351 		tile_size = intel_tile_size(dev_priv);
1352 		intel_tile_dims(fb, color_plane, &tile_width, &tile_height);
1353 
1354 		if (drm_rotation_90_or_270(rotation)) {
1355 			pitch_tiles = pitch / tile_height;
1356 			swap(tile_width, tile_height);
1357 		} else {
1358 			pitch_tiles = pitch / (tile_width * cpp);
1359 		}
1360 
1361 		tile_rows = *y / tile_height;
1362 		*y %= tile_height;
1363 
1364 		tiles = *x / tile_width;
1365 		*x %= tile_width;
1366 
1367 		offset = (tile_rows * pitch_tiles + tiles) * tile_size;
1368 
1369 		offset_aligned = offset;
1370 		if (alignment)
1371 			offset_aligned = rounddown(offset_aligned, alignment);
1372 
1373 		intel_adjust_tile_offset(x, y, tile_width, tile_height,
1374 					 tile_size, pitch_tiles,
1375 					 offset, offset_aligned);
1376 	} else {
1377 		offset = *y * pitch + *x * cpp;
1378 		offset_aligned = offset;
1379 		if (alignment) {
1380 			offset_aligned = rounddown(offset_aligned, alignment);
1381 			*y = (offset % alignment) / pitch;
1382 			*x = ((offset % alignment) - *y * pitch) / cpp;
1383 		} else {
1384 			*y = *x = 0;
1385 		}
1386 	}
1387 
1388 	return offset_aligned;
1389 }
1390 
1391 u32 intel_plane_compute_aligned_offset(int *x, int *y,
1392 				       const struct intel_plane_state *state,
1393 				       int color_plane)
1394 {
1395 	struct intel_plane *intel_plane = to_intel_plane(state->uapi.plane);
1396 	struct drm_i915_private *dev_priv = to_i915(intel_plane->base.dev);
1397 	const struct drm_framebuffer *fb = state->hw.fb;
1398 	unsigned int rotation = state->hw.rotation;
1399 	int pitch = state->color_plane[color_plane].stride;
1400 	u32 alignment;
1401 
1402 	if (intel_plane->id == PLANE_CURSOR)
1403 		alignment = intel_cursor_alignment(dev_priv);
1404 	else
1405 		alignment = intel_surf_alignment(fb, color_plane);
1406 
1407 	return intel_compute_aligned_offset(dev_priv, x, y, fb, color_plane,
1408 					    pitch, rotation, alignment);
1409 }
1410 
1411 /* Convert the fb->offset[] into x/y offsets */
1412 static int intel_fb_offset_to_xy(int *x, int *y,
1413 				 const struct drm_framebuffer *fb,
1414 				 int color_plane)
1415 {
1416 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
1417 	unsigned int height;
1418 	u32 alignment;
1419 
1420 	if (INTEL_GEN(dev_priv) >= 12 &&
1421 	    is_semiplanar_uv_plane(fb, color_plane))
1422 		alignment = intel_tile_row_size(fb, color_plane);
1423 	else if (fb->modifier != DRM_FORMAT_MOD_LINEAR)
1424 		alignment = intel_tile_size(dev_priv);
1425 	else
1426 		alignment = 0;
1427 
1428 	if (alignment != 0 && fb->offsets[color_plane] % alignment) {
1429 		drm_dbg_kms(&dev_priv->drm,
1430 			    "Misaligned offset 0x%08x for color plane %d\n",
1431 			    fb->offsets[color_plane], color_plane);
1432 		return -EINVAL;
1433 	}
1434 
1435 	height = drm_framebuffer_plane_height(fb->height, fb, color_plane);
1436 	height = ALIGN(height, intel_tile_height(fb, color_plane));
1437 
1438 	/* Catch potential overflows early */
1439 	if (add_overflows_t(u32, mul_u32_u32(height, fb->pitches[color_plane]),
1440 			    fb->offsets[color_plane])) {
1441 		drm_dbg_kms(&dev_priv->drm,
1442 			    "Bad offset 0x%08x or pitch %d for color plane %d\n",
1443 			    fb->offsets[color_plane], fb->pitches[color_plane],
1444 			    color_plane);
1445 		return -ERANGE;
1446 	}
1447 
1448 	*x = 0;
1449 	*y = 0;
1450 
1451 	intel_adjust_aligned_offset(x, y,
1452 				    fb, color_plane, DRM_MODE_ROTATE_0,
1453 				    fb->pitches[color_plane],
1454 				    fb->offsets[color_plane], 0);
1455 
1456 	return 0;
1457 }
1458 
1459 static unsigned int intel_fb_modifier_to_tiling(u64 fb_modifier)
1460 {
1461 	switch (fb_modifier) {
1462 	case I915_FORMAT_MOD_X_TILED:
1463 		return I915_TILING_X;
1464 	case I915_FORMAT_MOD_Y_TILED:
1465 	case I915_FORMAT_MOD_Y_TILED_CCS:
1466 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1467 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1468 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1469 		return I915_TILING_Y;
1470 	default:
1471 		return I915_TILING_NONE;
1472 	}
1473 }
1474 
1475 /*
1476  * From the Sky Lake PRM:
1477  * "The Color Control Surface (CCS) contains the compression status of
1478  *  the cache-line pairs. The compression state of the cache-line pair
1479  *  is specified by 2 bits in the CCS. Each CCS cache-line represents
1480  *  an area on the main surface of 16 x16 sets of 128 byte Y-tiled
1481  *  cache-line-pairs. CCS is always Y tiled."
1482  *
1483  * Since cache line pairs refers to horizontally adjacent cache lines,
1484  * each cache line in the CCS corresponds to an area of 32x16 cache
1485  * lines on the main surface. Since each pixel is 4 bytes, this gives
1486  * us a ratio of one byte in the CCS for each 8x16 pixels in the
1487  * main surface.
1488  */
1489 static const struct drm_format_info skl_ccs_formats[] = {
1490 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
1491 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
1492 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
1493 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, },
1494 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
1495 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
1496 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
1497 	  .cpp = { 4, 1, }, .hsub = 8, .vsub = 16, .has_alpha = true, },
1498 };
1499 
1500 /*
1501  * Gen-12 compression uses 4 bits of CCS data for each cache line pair in the
1502  * main surface. And each 64B CCS cache line represents an area of 4x1 Y-tiles
1503  * in the main surface. With 4 byte pixels and each Y-tile having dimensions of
1504  * 32x32 pixels, the ratio turns out to 1B in the CCS for every 2x32 pixels in
1505  * the main surface.
1506  */
1507 static const struct drm_format_info gen12_ccs_formats[] = {
1508 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 2,
1509 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1510 	  .hsub = 1, .vsub = 1, },
1511 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 2,
1512 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1513 	  .hsub = 1, .vsub = 1, },
1514 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 2,
1515 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1516 	  .hsub = 1, .vsub = 1, .has_alpha = true },
1517 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 2,
1518 	  .char_per_block = { 4, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1519 	  .hsub = 1, .vsub = 1, .has_alpha = true },
1520 	{ .format = DRM_FORMAT_YUYV, .num_planes = 2,
1521 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1522 	  .hsub = 2, .vsub = 1, .is_yuv = true },
1523 	{ .format = DRM_FORMAT_YVYU, .num_planes = 2,
1524 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1525 	  .hsub = 2, .vsub = 1, .is_yuv = true },
1526 	{ .format = DRM_FORMAT_UYVY, .num_planes = 2,
1527 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1528 	  .hsub = 2, .vsub = 1, .is_yuv = true },
1529 	{ .format = DRM_FORMAT_VYUY, .num_planes = 2,
1530 	  .char_per_block = { 2, 1 }, .block_w = { 1, 2 }, .block_h = { 1, 1 },
1531 	  .hsub = 2, .vsub = 1, .is_yuv = true },
1532 	{ .format = DRM_FORMAT_NV12, .num_planes = 4,
1533 	  .char_per_block = { 1, 2, 1, 1 }, .block_w = { 1, 1, 4, 4 }, .block_h = { 1, 1, 1, 1 },
1534 	  .hsub = 2, .vsub = 2, .is_yuv = true },
1535 	{ .format = DRM_FORMAT_P010, .num_planes = 4,
1536 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1537 	  .hsub = 2, .vsub = 2, .is_yuv = true },
1538 	{ .format = DRM_FORMAT_P012, .num_planes = 4,
1539 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1540 	  .hsub = 2, .vsub = 2, .is_yuv = true },
1541 	{ .format = DRM_FORMAT_P016, .num_planes = 4,
1542 	  .char_per_block = { 2, 4, 1, 1 }, .block_w = { 1, 1, 2, 2 }, .block_h = { 1, 1, 1, 1 },
1543 	  .hsub = 2, .vsub = 2, .is_yuv = true },
1544 };
1545 
1546 /*
1547  * Same as gen12_ccs_formats[] above, but with additional surface used
1548  * to pass Clear Color information in plane 2 with 64 bits of data.
1549  */
1550 static const struct drm_format_info gen12_ccs_cc_formats[] = {
1551 	{ .format = DRM_FORMAT_XRGB8888, .depth = 24, .num_planes = 3,
1552 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1553 	  .hsub = 1, .vsub = 1, },
1554 	{ .format = DRM_FORMAT_XBGR8888, .depth = 24, .num_planes = 3,
1555 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1556 	  .hsub = 1, .vsub = 1, },
1557 	{ .format = DRM_FORMAT_ARGB8888, .depth = 32, .num_planes = 3,
1558 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1559 	  .hsub = 1, .vsub = 1, .has_alpha = true },
1560 	{ .format = DRM_FORMAT_ABGR8888, .depth = 32, .num_planes = 3,
1561 	  .char_per_block = { 4, 1, 0 }, .block_w = { 1, 2, 2 }, .block_h = { 1, 1, 1 },
1562 	  .hsub = 1, .vsub = 1, .has_alpha = true },
1563 };
1564 
1565 static const struct drm_format_info *
1566 lookup_format_info(const struct drm_format_info formats[],
1567 		   int num_formats, u32 format)
1568 {
1569 	int i;
1570 
1571 	for (i = 0; i < num_formats; i++) {
1572 		if (formats[i].format == format)
1573 			return &formats[i];
1574 	}
1575 
1576 	return NULL;
1577 }
1578 
1579 static const struct drm_format_info *
1580 intel_get_format_info(const struct drm_mode_fb_cmd2 *cmd)
1581 {
1582 	switch (cmd->modifier[0]) {
1583 	case I915_FORMAT_MOD_Y_TILED_CCS:
1584 	case I915_FORMAT_MOD_Yf_TILED_CCS:
1585 		return lookup_format_info(skl_ccs_formats,
1586 					  ARRAY_SIZE(skl_ccs_formats),
1587 					  cmd->pixel_format);
1588 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS:
1589 	case I915_FORMAT_MOD_Y_TILED_GEN12_MC_CCS:
1590 		return lookup_format_info(gen12_ccs_formats,
1591 					  ARRAY_SIZE(gen12_ccs_formats),
1592 					  cmd->pixel_format);
1593 	case I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC:
1594 		return lookup_format_info(gen12_ccs_cc_formats,
1595 					  ARRAY_SIZE(gen12_ccs_cc_formats),
1596 					  cmd->pixel_format);
1597 	default:
1598 		return NULL;
1599 	}
1600 }
1601 
1602 static int gen12_ccs_aux_stride(struct drm_framebuffer *fb, int ccs_plane)
1603 {
1604 	return DIV_ROUND_UP(fb->pitches[skl_ccs_to_main_plane(fb, ccs_plane)],
1605 			    512) * 64;
1606 }
1607 
1608 u32 intel_plane_fb_max_stride(struct drm_i915_private *dev_priv,
1609 			      u32 pixel_format, u64 modifier)
1610 {
1611 	struct intel_crtc *crtc;
1612 	struct intel_plane *plane;
1613 
1614 	/*
1615 	 * We assume the primary plane for pipe A has
1616 	 * the highest stride limits of them all,
1617 	 * if in case pipe A is disabled, use the first pipe from pipe_mask.
1618 	 */
1619 	crtc = intel_get_first_crtc(dev_priv);
1620 	if (!crtc)
1621 		return 0;
1622 
1623 	plane = to_intel_plane(crtc->base.primary);
1624 
1625 	return plane->max_stride(plane, pixel_format, modifier,
1626 				 DRM_MODE_ROTATE_0);
1627 }
1628 
1629 static
1630 u32 intel_fb_max_stride(struct drm_i915_private *dev_priv,
1631 			u32 pixel_format, u64 modifier)
1632 {
1633 	/*
1634 	 * Arbitrary limit for gen4+ chosen to match the
1635 	 * render engine max stride.
1636 	 *
1637 	 * The new CCS hash mode makes remapping impossible
1638 	 */
1639 	if (!is_ccs_modifier(modifier)) {
1640 		if (INTEL_GEN(dev_priv) >= 7)
1641 			return 256*1024;
1642 		else if (INTEL_GEN(dev_priv) >= 4)
1643 			return 128*1024;
1644 	}
1645 
1646 	return intel_plane_fb_max_stride(dev_priv, pixel_format, modifier);
1647 }
1648 
1649 static u32
1650 intel_fb_stride_alignment(const struct drm_framebuffer *fb, int color_plane)
1651 {
1652 	struct drm_i915_private *dev_priv = to_i915(fb->dev);
1653 	u32 tile_width;
1654 
1655 	if (is_surface_linear(fb, color_plane)) {
1656 		u32 max_stride = intel_plane_fb_max_stride(dev_priv,
1657 							   fb->format->format,
1658 							   fb->modifier);
1659 
1660 		/*
1661 		 * To make remapping with linear generally feasible
1662 		 * we need the stride to be page aligned.
1663 		 */
1664 		if (fb->pitches[color_plane] > max_stride &&
1665 		    !is_ccs_modifier(fb->modifier))
1666 			return intel_tile_size(dev_priv);
1667 		else
1668 			return 64;
1669 	}
1670 
1671 	tile_width = intel_tile_width_bytes(fb, color_plane);
1672 	if (is_ccs_modifier(fb->modifier)) {
1673 		/*
1674 		 * Display WA #0531: skl,bxt,kbl,glk
1675 		 *
1676 		 * Render decompression and plane width > 3840
1677 		 * combined with horizontal panning requires the
1678 		 * plane stride to be a multiple of 4. We'll just
1679 		 * require the entire fb to accommodate that to avoid
1680 		 * potential runtime errors at plane configuration time.
1681 		 */
1682 		if (IS_GEN(dev_priv, 9) && color_plane == 0 && fb->width > 3840)
1683 			tile_width *= 4;
1684 		/*
1685 		 * The main surface pitch must be padded to a multiple of four
1686 		 * tile widths.
1687 		 */
1688 		else if (INTEL_GEN(dev_priv) >= 12)
1689 			tile_width *= 4;
1690 	}
1691 	return tile_width;
1692 }
1693 
1694 bool intel_plane_can_remap(const struct intel_plane_state *plane_state)
1695 {
1696 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1697 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
1698 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1699 	int i;
1700 
1701 	/* We don't want to deal with remapping with cursors */
1702 	if (plane->id == PLANE_CURSOR)
1703 		return false;
1704 
1705 	/*
1706 	 * The display engine limits already match/exceed the
1707 	 * render engine limits, so not much point in remapping.
1708 	 * Would also need to deal with the fence POT alignment
1709 	 * and gen2 2KiB GTT tile size.
1710 	 */
1711 	if (INTEL_GEN(dev_priv) < 4)
1712 		return false;
1713 
1714 	/*
1715 	 * The new CCS hash mode isn't compatible with remapping as
1716 	 * the virtual address of the pages affects the compressed data.
1717 	 */
1718 	if (is_ccs_modifier(fb->modifier))
1719 		return false;
1720 
1721 	/* Linear needs a page aligned stride for remapping */
1722 	if (fb->modifier == DRM_FORMAT_MOD_LINEAR) {
1723 		unsigned int alignment = intel_tile_size(dev_priv) - 1;
1724 
1725 		for (i = 0; i < fb->format->num_planes; i++) {
1726 			if (fb->pitches[i] & alignment)
1727 				return false;
1728 		}
1729 	}
1730 
1731 	return true;
1732 }
1733 
1734 static bool intel_plane_needs_remap(const struct intel_plane_state *plane_state)
1735 {
1736 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
1737 	const struct drm_framebuffer *fb = plane_state->hw.fb;
1738 	unsigned int rotation = plane_state->hw.rotation;
1739 	u32 stride, max_stride;
1740 
1741 	/*
1742 	 * No remapping for invisible planes since we don't have
1743 	 * an actual source viewport to remap.
1744 	 */
1745 	if (!plane_state->uapi.visible)
1746 		return false;
1747 
1748 	if (!intel_plane_can_remap(plane_state))
1749 		return false;
1750 
1751 	/*
1752 	 * FIXME: aux plane limits on gen9+ are
1753 	 * unclear in Bspec, for now no checking.
1754 	 */
1755 	stride = intel_fb_pitch(fb, 0, rotation);
1756 	max_stride = plane->max_stride(plane, fb->format->format,
1757 				       fb->modifier, rotation);
1758 
1759 	return stride > max_stride;
1760 }
1761 
1762 void
1763 intel_fb_plane_get_subsampling(int *hsub, int *vsub,
1764 			       const struct drm_framebuffer *fb,
1765 			       int color_plane)
1766 {
1767 	int main_plane;
1768 
1769 	if (color_plane == 0) {
1770 		*hsub = 1;
1771 		*vsub = 1;
1772 
1773 		return;
1774 	}
1775 
1776 	/*
1777 	 * TODO: Deduct the subsampling from the char block for all CCS
1778 	 * formats and planes.
1779 	 */
1780 	if (!is_gen12_ccs_plane(fb, color_plane)) {
1781 		*hsub = fb->format->hsub;
1782 		*vsub = fb->format->vsub;
1783 
1784 		return;
1785 	}
1786 
1787 	main_plane = skl_ccs_to_main_plane(fb, color_plane);
1788 	*hsub = drm_format_info_block_width(fb->format, color_plane) /
1789 		drm_format_info_block_width(fb->format, main_plane);
1790 
1791 	/*
1792 	 * The min stride check in the core framebuffer_check() function
1793 	 * assumes that format->hsub applies to every plane except for the
1794 	 * first plane. That's incorrect for the CCS AUX plane of the first
1795 	 * plane, but for the above check to pass we must define the block
1796 	 * width with that subsampling applied to it. Adjust the width here
1797 	 * accordingly, so we can calculate the actual subsampling factor.
1798 	 */
1799 	if (main_plane == 0)
1800 		*hsub *= fb->format->hsub;
1801 
1802 	*vsub = 32;
1803 }
1804 static int
1805 intel_fb_check_ccs_xy(struct drm_framebuffer *fb, int ccs_plane, int x, int y)
1806 {
1807 	struct drm_i915_private *i915 = to_i915(fb->dev);
1808 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1809 	int main_plane;
1810 	int hsub, vsub;
1811 	int tile_width, tile_height;
1812 	int ccs_x, ccs_y;
1813 	int main_x, main_y;
1814 
1815 	if (!is_ccs_plane(fb, ccs_plane) || is_gen12_ccs_cc_plane(fb, ccs_plane))
1816 		return 0;
1817 
1818 	intel_tile_dims(fb, ccs_plane, &tile_width, &tile_height);
1819 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, ccs_plane);
1820 
1821 	tile_width *= hsub;
1822 	tile_height *= vsub;
1823 
1824 	ccs_x = (x * hsub) % tile_width;
1825 	ccs_y = (y * vsub) % tile_height;
1826 
1827 	main_plane = skl_ccs_to_main_plane(fb, ccs_plane);
1828 	main_x = intel_fb->normal[main_plane].x % tile_width;
1829 	main_y = intel_fb->normal[main_plane].y % tile_height;
1830 
1831 	/*
1832 	 * CCS doesn't have its own x/y offset register, so the intra CCS tile
1833 	 * x/y offsets must match between CCS and the main surface.
1834 	 */
1835 	if (main_x != ccs_x || main_y != ccs_y) {
1836 		drm_dbg_kms(&i915->drm,
1837 			      "Bad CCS x/y (main %d,%d ccs %d,%d) full (main %d,%d ccs %d,%d)\n",
1838 			      main_x, main_y,
1839 			      ccs_x, ccs_y,
1840 			      intel_fb->normal[main_plane].x,
1841 			      intel_fb->normal[main_plane].y,
1842 			      x, y);
1843 		return -EINVAL;
1844 	}
1845 
1846 	return 0;
1847 }
1848 
1849 static void
1850 intel_fb_plane_dims(int *w, int *h, struct drm_framebuffer *fb, int color_plane)
1851 {
1852 	int main_plane = is_ccs_plane(fb, color_plane) ?
1853 			 skl_ccs_to_main_plane(fb, color_plane) : 0;
1854 	int main_hsub, main_vsub;
1855 	int hsub, vsub;
1856 
1857 	intel_fb_plane_get_subsampling(&main_hsub, &main_vsub, fb, main_plane);
1858 	intel_fb_plane_get_subsampling(&hsub, &vsub, fb, color_plane);
1859 	*w = fb->width / main_hsub / hsub;
1860 	*h = fb->height / main_vsub / vsub;
1861 }
1862 
1863 /*
1864  * Setup the rotated view for an FB plane and return the size the GTT mapping
1865  * requires for this view.
1866  */
1867 static u32
1868 setup_fb_rotation(int plane, const struct intel_remapped_plane_info *plane_info,
1869 		  u32 gtt_offset_rotated, int x, int y,
1870 		  unsigned int width, unsigned int height,
1871 		  unsigned int tile_size,
1872 		  unsigned int tile_width, unsigned int tile_height,
1873 		  struct drm_framebuffer *fb)
1874 {
1875 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1876 	struct intel_rotation_info *rot_info = &intel_fb->rot_info;
1877 	unsigned int pitch_tiles;
1878 	struct drm_rect r;
1879 
1880 	/* Y or Yf modifiers required for 90/270 rotation */
1881 	if (fb->modifier != I915_FORMAT_MOD_Y_TILED &&
1882 	    fb->modifier != I915_FORMAT_MOD_Yf_TILED)
1883 		return 0;
1884 
1885 	if (drm_WARN_ON(fb->dev, plane >= ARRAY_SIZE(rot_info->plane)))
1886 		return 0;
1887 
1888 	rot_info->plane[plane] = *plane_info;
1889 
1890 	intel_fb->rotated[plane].pitch = plane_info->height * tile_height;
1891 
1892 	/* rotate the x/y offsets to match the GTT view */
1893 	drm_rect_init(&r, x, y, width, height);
1894 	drm_rect_rotate(&r,
1895 			plane_info->width * tile_width,
1896 			plane_info->height * tile_height,
1897 			DRM_MODE_ROTATE_270);
1898 	x = r.x1;
1899 	y = r.y1;
1900 
1901 	/* rotate the tile dimensions to match the GTT view */
1902 	pitch_tiles = intel_fb->rotated[plane].pitch / tile_height;
1903 	swap(tile_width, tile_height);
1904 
1905 	/*
1906 	 * We only keep the x/y offsets, so push all of the
1907 	 * gtt offset into the x/y offsets.
1908 	 */
1909 	intel_adjust_tile_offset(&x, &y,
1910 				 tile_width, tile_height,
1911 				 tile_size, pitch_tiles,
1912 				 gtt_offset_rotated * tile_size, 0);
1913 
1914 	/*
1915 	 * First pixel of the framebuffer from
1916 	 * the start of the rotated gtt mapping.
1917 	 */
1918 	intel_fb->rotated[plane].x = x;
1919 	intel_fb->rotated[plane].y = y;
1920 
1921 	return plane_info->width * plane_info->height;
1922 }
1923 
1924 static int
1925 intel_fill_fb_info(struct drm_i915_private *dev_priv,
1926 		   struct drm_framebuffer *fb)
1927 {
1928 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
1929 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
1930 	u32 gtt_offset_rotated = 0;
1931 	unsigned int max_size = 0;
1932 	int i, num_planes = fb->format->num_planes;
1933 	unsigned int tile_size = intel_tile_size(dev_priv);
1934 
1935 	for (i = 0; i < num_planes; i++) {
1936 		unsigned int width, height;
1937 		unsigned int cpp, size;
1938 		u32 offset;
1939 		int x, y;
1940 		int ret;
1941 
1942 		/*
1943 		 * Plane 2 of Render Compression with Clear Color fb modifier
1944 		 * is consumed by the driver and not passed to DE. Skip the
1945 		 * arithmetic related to alignment and offset calculation.
1946 		 */
1947 		if (is_gen12_ccs_cc_plane(fb, i)) {
1948 			if (IS_ALIGNED(fb->offsets[i], PAGE_SIZE))
1949 				continue;
1950 			else
1951 				return -EINVAL;
1952 		}
1953 
1954 		cpp = fb->format->cpp[i];
1955 		intel_fb_plane_dims(&width, &height, fb, i);
1956 
1957 		ret = intel_fb_offset_to_xy(&x, &y, fb, i);
1958 		if (ret) {
1959 			drm_dbg_kms(&dev_priv->drm,
1960 				    "bad fb plane %d offset: 0x%x\n",
1961 				    i, fb->offsets[i]);
1962 			return ret;
1963 		}
1964 
1965 		ret = intel_fb_check_ccs_xy(fb, i, x, y);
1966 		if (ret)
1967 			return ret;
1968 
1969 		/*
1970 		 * The fence (if used) is aligned to the start of the object
1971 		 * so having the framebuffer wrap around across the edge of the
1972 		 * fenced region doesn't really work. We have no API to configure
1973 		 * the fence start offset within the object (nor could we probably
1974 		 * on gen2/3). So it's just easier if we just require that the
1975 		 * fb layout agrees with the fence layout. We already check that the
1976 		 * fb stride matches the fence stride elsewhere.
1977 		 */
1978 		if (i == 0 && i915_gem_object_is_tiled(obj) &&
1979 		    (x + width) * cpp > fb->pitches[i]) {
1980 			drm_dbg_kms(&dev_priv->drm,
1981 				    "bad fb plane %d offset: 0x%x\n",
1982 				     i, fb->offsets[i]);
1983 			return -EINVAL;
1984 		}
1985 
1986 		/*
1987 		 * First pixel of the framebuffer from
1988 		 * the start of the normal gtt mapping.
1989 		 */
1990 		intel_fb->normal[i].x = x;
1991 		intel_fb->normal[i].y = y;
1992 
1993 		offset = intel_compute_aligned_offset(dev_priv, &x, &y, fb, i,
1994 						      fb->pitches[i],
1995 						      DRM_MODE_ROTATE_0,
1996 						      tile_size);
1997 		offset /= tile_size;
1998 
1999 		if (!is_surface_linear(fb, i)) {
2000 			struct intel_remapped_plane_info plane_info;
2001 			unsigned int tile_width, tile_height;
2002 
2003 			intel_tile_dims(fb, i, &tile_width, &tile_height);
2004 
2005 			plane_info.offset = offset;
2006 			plane_info.stride = DIV_ROUND_UP(fb->pitches[i],
2007 							 tile_width * cpp);
2008 			plane_info.width = DIV_ROUND_UP(x + width, tile_width);
2009 			plane_info.height = DIV_ROUND_UP(y + height,
2010 							 tile_height);
2011 
2012 			/* how many tiles does this plane need */
2013 			size = plane_info.stride * plane_info.height;
2014 			/*
2015 			 * If the plane isn't horizontally tile aligned,
2016 			 * we need one more tile.
2017 			 */
2018 			if (x != 0)
2019 				size++;
2020 
2021 			gtt_offset_rotated +=
2022 				setup_fb_rotation(i, &plane_info,
2023 						  gtt_offset_rotated,
2024 						  x, y, width, height,
2025 						  tile_size,
2026 						  tile_width, tile_height,
2027 						  fb);
2028 		} else {
2029 			size = DIV_ROUND_UP((y + height) * fb->pitches[i] +
2030 					    x * cpp, tile_size);
2031 		}
2032 
2033 		/* how many tiles in total needed in the bo */
2034 		max_size = max(max_size, offset + size);
2035 	}
2036 
2037 	if (mul_u32_u32(max_size, tile_size) > obj->base.size) {
2038 		drm_dbg_kms(&dev_priv->drm,
2039 			    "fb too big for bo (need %llu bytes, have %zu bytes)\n",
2040 			    mul_u32_u32(max_size, tile_size), obj->base.size);
2041 		return -EINVAL;
2042 	}
2043 
2044 	return 0;
2045 }
2046 
2047 static void
2048 intel_plane_remap_gtt(struct intel_plane_state *plane_state)
2049 {
2050 	struct drm_i915_private *dev_priv =
2051 		to_i915(plane_state->uapi.plane->dev);
2052 	struct drm_framebuffer *fb = plane_state->hw.fb;
2053 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
2054 	struct intel_rotation_info *info = &plane_state->view.rotated;
2055 	unsigned int rotation = plane_state->hw.rotation;
2056 	int i, num_planes = fb->format->num_planes;
2057 	unsigned int tile_size = intel_tile_size(dev_priv);
2058 	unsigned int src_x, src_y;
2059 	unsigned int src_w, src_h;
2060 	u32 gtt_offset = 0;
2061 
2062 	memset(&plane_state->view, 0, sizeof(plane_state->view));
2063 	plane_state->view.type = drm_rotation_90_or_270(rotation) ?
2064 		I915_GGTT_VIEW_ROTATED : I915_GGTT_VIEW_REMAPPED;
2065 
2066 	src_x = plane_state->uapi.src.x1 >> 16;
2067 	src_y = plane_state->uapi.src.y1 >> 16;
2068 	src_w = drm_rect_width(&plane_state->uapi.src) >> 16;
2069 	src_h = drm_rect_height(&plane_state->uapi.src) >> 16;
2070 
2071 	drm_WARN_ON(&dev_priv->drm, is_ccs_modifier(fb->modifier));
2072 
2073 	/* Make src coordinates relative to the viewport */
2074 	drm_rect_translate(&plane_state->uapi.src,
2075 			   -(src_x << 16), -(src_y << 16));
2076 
2077 	/* Rotate src coordinates to match rotated GTT view */
2078 	if (drm_rotation_90_or_270(rotation))
2079 		drm_rect_rotate(&plane_state->uapi.src,
2080 				src_w << 16, src_h << 16,
2081 				DRM_MODE_ROTATE_270);
2082 
2083 	for (i = 0; i < num_planes; i++) {
2084 		unsigned int hsub = i ? fb->format->hsub : 1;
2085 		unsigned int vsub = i ? fb->format->vsub : 1;
2086 		unsigned int cpp = fb->format->cpp[i];
2087 		unsigned int tile_width, tile_height;
2088 		unsigned int width, height;
2089 		unsigned int pitch_tiles;
2090 		unsigned int x, y;
2091 		u32 offset;
2092 
2093 		intel_tile_dims(fb, i, &tile_width, &tile_height);
2094 
2095 		x = src_x / hsub;
2096 		y = src_y / vsub;
2097 		width = src_w / hsub;
2098 		height = src_h / vsub;
2099 
2100 		/*
2101 		 * First pixel of the src viewport from the
2102 		 * start of the normal gtt mapping.
2103 		 */
2104 		x += intel_fb->normal[i].x;
2105 		y += intel_fb->normal[i].y;
2106 
2107 		offset = intel_compute_aligned_offset(dev_priv, &x, &y,
2108 						      fb, i, fb->pitches[i],
2109 						      DRM_MODE_ROTATE_0, tile_size);
2110 		offset /= tile_size;
2111 
2112 		drm_WARN_ON(&dev_priv->drm, i >= ARRAY_SIZE(info->plane));
2113 		info->plane[i].offset = offset;
2114 		info->plane[i].stride = DIV_ROUND_UP(fb->pitches[i],
2115 						     tile_width * cpp);
2116 		info->plane[i].width = DIV_ROUND_UP(x + width, tile_width);
2117 		info->plane[i].height = DIV_ROUND_UP(y + height, tile_height);
2118 
2119 		if (drm_rotation_90_or_270(rotation)) {
2120 			struct drm_rect r;
2121 
2122 			/* rotate the x/y offsets to match the GTT view */
2123 			drm_rect_init(&r, x, y, width, height);
2124 			drm_rect_rotate(&r,
2125 					info->plane[i].width * tile_width,
2126 					info->plane[i].height * tile_height,
2127 					DRM_MODE_ROTATE_270);
2128 			x = r.x1;
2129 			y = r.y1;
2130 
2131 			pitch_tiles = info->plane[i].height;
2132 			plane_state->color_plane[i].stride = pitch_tiles * tile_height;
2133 
2134 			/* rotate the tile dimensions to match the GTT view */
2135 			swap(tile_width, tile_height);
2136 		} else {
2137 			pitch_tiles = info->plane[i].width;
2138 			plane_state->color_plane[i].stride = pitch_tiles * tile_width * cpp;
2139 		}
2140 
2141 		/*
2142 		 * We only keep the x/y offsets, so push all of the
2143 		 * gtt offset into the x/y offsets.
2144 		 */
2145 		intel_adjust_tile_offset(&x, &y,
2146 					 tile_width, tile_height,
2147 					 tile_size, pitch_tiles,
2148 					 gtt_offset * tile_size, 0);
2149 
2150 		gtt_offset += info->plane[i].width * info->plane[i].height;
2151 
2152 		plane_state->color_plane[i].offset = 0;
2153 		plane_state->color_plane[i].x = x;
2154 		plane_state->color_plane[i].y = y;
2155 	}
2156 }
2157 
2158 int
2159 intel_plane_compute_gtt(struct intel_plane_state *plane_state)
2160 {
2161 	const struct intel_framebuffer *fb =
2162 		to_intel_framebuffer(plane_state->hw.fb);
2163 	unsigned int rotation = plane_state->hw.rotation;
2164 	int i, num_planes;
2165 
2166 	if (!fb)
2167 		return 0;
2168 
2169 	num_planes = fb->base.format->num_planes;
2170 
2171 	if (intel_plane_needs_remap(plane_state)) {
2172 		intel_plane_remap_gtt(plane_state);
2173 
2174 		/*
2175 		 * Sometimes even remapping can't overcome
2176 		 * the stride limitations :( Can happen with
2177 		 * big plane sizes and suitably misaligned
2178 		 * offsets.
2179 		 */
2180 		return intel_plane_check_stride(plane_state);
2181 	}
2182 
2183 	intel_fill_fb_ggtt_view(&plane_state->view, &fb->base, rotation);
2184 
2185 	for (i = 0; i < num_planes; i++) {
2186 		plane_state->color_plane[i].stride = intel_fb_pitch(&fb->base, i, rotation);
2187 		plane_state->color_plane[i].offset = 0;
2188 
2189 		if (drm_rotation_90_or_270(rotation)) {
2190 			plane_state->color_plane[i].x = fb->rotated[i].x;
2191 			plane_state->color_plane[i].y = fb->rotated[i].y;
2192 		} else {
2193 			plane_state->color_plane[i].x = fb->normal[i].x;
2194 			plane_state->color_plane[i].y = fb->normal[i].y;
2195 		}
2196 	}
2197 
2198 	/* Rotate src coordinates to match rotated GTT view */
2199 	if (drm_rotation_90_or_270(rotation))
2200 		drm_rect_rotate(&plane_state->uapi.src,
2201 				fb->base.width << 16, fb->base.height << 16,
2202 				DRM_MODE_ROTATE_270);
2203 
2204 	return intel_plane_check_stride(plane_state);
2205 }
2206 
2207 static struct i915_vma *
2208 initial_plane_vma(struct drm_i915_private *i915,
2209 		  struct intel_initial_plane_config *plane_config)
2210 {
2211 	struct drm_i915_gem_object *obj;
2212 	struct i915_vma *vma;
2213 	u32 base, size;
2214 
2215 	if (plane_config->size == 0)
2216 		return NULL;
2217 
2218 	base = round_down(plane_config->base,
2219 			  I915_GTT_MIN_ALIGNMENT);
2220 	size = round_up(plane_config->base + plane_config->size,
2221 			I915_GTT_MIN_ALIGNMENT);
2222 	size -= base;
2223 
2224 	/*
2225 	 * If the FB is too big, just don't use it since fbdev is not very
2226 	 * important and we should probably use that space with FBC or other
2227 	 * features.
2228 	 */
2229 	if (size * 2 > i915->stolen_usable_size)
2230 		return NULL;
2231 
2232 	obj = i915_gem_object_create_stolen_for_preallocated(i915, base, size);
2233 	if (IS_ERR(obj))
2234 		return NULL;
2235 
2236 	/*
2237 	 * Mark it WT ahead of time to avoid changing the
2238 	 * cache_level during fbdev initialization. The
2239 	 * unbind there would get stuck waiting for rcu.
2240 	 */
2241 	i915_gem_object_set_cache_coherency(obj, HAS_WT(i915) ?
2242 					    I915_CACHE_WT : I915_CACHE_NONE);
2243 
2244 	switch (plane_config->tiling) {
2245 	case I915_TILING_NONE:
2246 		break;
2247 	case I915_TILING_X:
2248 	case I915_TILING_Y:
2249 		obj->tiling_and_stride =
2250 			plane_config->fb->base.pitches[0] |
2251 			plane_config->tiling;
2252 		break;
2253 	default:
2254 		MISSING_CASE(plane_config->tiling);
2255 		goto err_obj;
2256 	}
2257 
2258 	vma = i915_vma_instance(obj, &i915->ggtt.vm, NULL);
2259 	if (IS_ERR(vma))
2260 		goto err_obj;
2261 
2262 	if (i915_ggtt_pin(vma, NULL, 0, PIN_MAPPABLE | PIN_OFFSET_FIXED | base))
2263 		goto err_obj;
2264 
2265 	if (i915_gem_object_is_tiled(obj) &&
2266 	    !i915_vma_is_map_and_fenceable(vma))
2267 		goto err_obj;
2268 
2269 	return vma;
2270 
2271 err_obj:
2272 	i915_gem_object_put(obj);
2273 	return NULL;
2274 }
2275 
2276 static bool
2277 intel_alloc_initial_plane_obj(struct intel_crtc *crtc,
2278 			      struct intel_initial_plane_config *plane_config)
2279 {
2280 	struct drm_device *dev = crtc->base.dev;
2281 	struct drm_i915_private *dev_priv = to_i915(dev);
2282 	struct drm_mode_fb_cmd2 mode_cmd = { 0 };
2283 	struct drm_framebuffer *fb = &plane_config->fb->base;
2284 	struct i915_vma *vma;
2285 
2286 	switch (fb->modifier) {
2287 	case DRM_FORMAT_MOD_LINEAR:
2288 	case I915_FORMAT_MOD_X_TILED:
2289 	case I915_FORMAT_MOD_Y_TILED:
2290 		break;
2291 	default:
2292 		drm_dbg(&dev_priv->drm,
2293 			"Unsupported modifier for initial FB: 0x%llx\n",
2294 			fb->modifier);
2295 		return false;
2296 	}
2297 
2298 	vma = initial_plane_vma(dev_priv, plane_config);
2299 	if (!vma)
2300 		return false;
2301 
2302 	mode_cmd.pixel_format = fb->format->format;
2303 	mode_cmd.width = fb->width;
2304 	mode_cmd.height = fb->height;
2305 	mode_cmd.pitches[0] = fb->pitches[0];
2306 	mode_cmd.modifier[0] = fb->modifier;
2307 	mode_cmd.flags = DRM_MODE_FB_MODIFIERS;
2308 
2309 	if (intel_framebuffer_init(to_intel_framebuffer(fb),
2310 				   vma->obj, &mode_cmd)) {
2311 		drm_dbg_kms(&dev_priv->drm, "intel fb init failed\n");
2312 		goto err_vma;
2313 	}
2314 
2315 	plane_config->vma = vma;
2316 	return true;
2317 
2318 err_vma:
2319 	i915_vma_put(vma);
2320 	return false;
2321 }
2322 
2323 static void
2324 intel_set_plane_visible(struct intel_crtc_state *crtc_state,
2325 			struct intel_plane_state *plane_state,
2326 			bool visible)
2327 {
2328 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
2329 
2330 	plane_state->uapi.visible = visible;
2331 
2332 	if (visible)
2333 		crtc_state->uapi.plane_mask |= drm_plane_mask(&plane->base);
2334 	else
2335 		crtc_state->uapi.plane_mask &= ~drm_plane_mask(&plane->base);
2336 }
2337 
2338 static void fixup_plane_bitmasks(struct intel_crtc_state *crtc_state)
2339 {
2340 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
2341 	struct drm_plane *plane;
2342 
2343 	/*
2344 	 * Active_planes aliases if multiple "primary" or cursor planes
2345 	 * have been used on the same (or wrong) pipe. plane_mask uses
2346 	 * unique ids, hence we can use that to reconstruct active_planes.
2347 	 */
2348 	crtc_state->enabled_planes = 0;
2349 	crtc_state->active_planes = 0;
2350 
2351 	drm_for_each_plane_mask(plane, &dev_priv->drm,
2352 				crtc_state->uapi.plane_mask) {
2353 		crtc_state->enabled_planes |= BIT(to_intel_plane(plane)->id);
2354 		crtc_state->active_planes |= BIT(to_intel_plane(plane)->id);
2355 	}
2356 }
2357 
2358 static void intel_plane_disable_noatomic(struct intel_crtc *crtc,
2359 					 struct intel_plane *plane)
2360 {
2361 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2362 	struct intel_crtc_state *crtc_state =
2363 		to_intel_crtc_state(crtc->base.state);
2364 	struct intel_plane_state *plane_state =
2365 		to_intel_plane_state(plane->base.state);
2366 
2367 	drm_dbg_kms(&dev_priv->drm,
2368 		    "Disabling [PLANE:%d:%s] on [CRTC:%d:%s]\n",
2369 		    plane->base.base.id, plane->base.name,
2370 		    crtc->base.base.id, crtc->base.name);
2371 
2372 	intel_set_plane_visible(crtc_state, plane_state, false);
2373 	fixup_plane_bitmasks(crtc_state);
2374 	crtc_state->data_rate[plane->id] = 0;
2375 	crtc_state->min_cdclk[plane->id] = 0;
2376 
2377 	if (plane->id == PLANE_PRIMARY)
2378 		hsw_disable_ips(crtc_state);
2379 
2380 	/*
2381 	 * Vblank time updates from the shadow to live plane control register
2382 	 * are blocked if the memory self-refresh mode is active at that
2383 	 * moment. So to make sure the plane gets truly disabled, disable
2384 	 * first the self-refresh mode. The self-refresh enable bit in turn
2385 	 * will be checked/applied by the HW only at the next frame start
2386 	 * event which is after the vblank start event, so we need to have a
2387 	 * wait-for-vblank between disabling the plane and the pipe.
2388 	 */
2389 	if (HAS_GMCH(dev_priv) &&
2390 	    intel_set_memory_cxsr(dev_priv, false))
2391 		intel_wait_for_vblank(dev_priv, crtc->pipe);
2392 
2393 	/*
2394 	 * Gen2 reports pipe underruns whenever all planes are disabled.
2395 	 * So disable underrun reporting before all the planes get disabled.
2396 	 */
2397 	if (IS_GEN(dev_priv, 2) && !crtc_state->active_planes)
2398 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, false);
2399 
2400 	intel_disable_plane(plane, crtc_state);
2401 	intel_wait_for_vblank(dev_priv, crtc->pipe);
2402 }
2403 
2404 static void
2405 intel_find_initial_plane_obj(struct intel_crtc *intel_crtc,
2406 			     struct intel_initial_plane_config *plane_config)
2407 {
2408 	struct drm_device *dev = intel_crtc->base.dev;
2409 	struct drm_i915_private *dev_priv = to_i915(dev);
2410 	struct drm_crtc *c;
2411 	struct drm_plane *primary = intel_crtc->base.primary;
2412 	struct drm_plane_state *plane_state = primary->state;
2413 	struct intel_plane *intel_plane = to_intel_plane(primary);
2414 	struct intel_plane_state *intel_state =
2415 		to_intel_plane_state(plane_state);
2416 	struct intel_crtc_state *crtc_state =
2417 		to_intel_crtc_state(intel_crtc->base.state);
2418 	struct drm_framebuffer *fb;
2419 	struct i915_vma *vma;
2420 
2421 	if (!plane_config->fb)
2422 		return;
2423 
2424 	if (intel_alloc_initial_plane_obj(intel_crtc, plane_config)) {
2425 		fb = &plane_config->fb->base;
2426 		vma = plane_config->vma;
2427 		goto valid_fb;
2428 	}
2429 
2430 	/*
2431 	 * Failed to alloc the obj, check to see if we should share
2432 	 * an fb with another CRTC instead
2433 	 */
2434 	for_each_crtc(dev, c) {
2435 		struct intel_plane_state *state;
2436 
2437 		if (c == &intel_crtc->base)
2438 			continue;
2439 
2440 		if (!to_intel_crtc_state(c->state)->uapi.active)
2441 			continue;
2442 
2443 		state = to_intel_plane_state(c->primary->state);
2444 		if (!state->vma)
2445 			continue;
2446 
2447 		if (intel_plane_ggtt_offset(state) == plane_config->base) {
2448 			fb = state->hw.fb;
2449 			vma = state->vma;
2450 			goto valid_fb;
2451 		}
2452 	}
2453 
2454 	/*
2455 	 * We've failed to reconstruct the BIOS FB.  Current display state
2456 	 * indicates that the primary plane is visible, but has a NULL FB,
2457 	 * which will lead to problems later if we don't fix it up.  The
2458 	 * simplest solution is to just disable the primary plane now and
2459 	 * pretend the BIOS never had it enabled.
2460 	 */
2461 	intel_plane_disable_noatomic(intel_crtc, intel_plane);
2462 	if (crtc_state->bigjoiner) {
2463 		struct intel_crtc *slave =
2464 			crtc_state->bigjoiner_linked_crtc;
2465 		intel_plane_disable_noatomic(slave, to_intel_plane(slave->base.primary));
2466 	}
2467 
2468 	return;
2469 
2470 valid_fb:
2471 	intel_state->hw.rotation = plane_config->rotation;
2472 	intel_fill_fb_ggtt_view(&intel_state->view, fb,
2473 				intel_state->hw.rotation);
2474 	intel_state->color_plane[0].stride =
2475 		intel_fb_pitch(fb, 0, intel_state->hw.rotation);
2476 
2477 	__i915_vma_pin(vma);
2478 	intel_state->vma = i915_vma_get(vma);
2479 	if (intel_plane_uses_fence(intel_state) && i915_vma_pin_fence(vma) == 0)
2480 		if (vma->fence)
2481 			intel_state->flags |= PLANE_HAS_FENCE;
2482 
2483 	plane_state->src_x = 0;
2484 	plane_state->src_y = 0;
2485 	plane_state->src_w = fb->width << 16;
2486 	plane_state->src_h = fb->height << 16;
2487 
2488 	plane_state->crtc_x = 0;
2489 	plane_state->crtc_y = 0;
2490 	plane_state->crtc_w = fb->width;
2491 	plane_state->crtc_h = fb->height;
2492 
2493 	intel_state->uapi.src = drm_plane_state_src(plane_state);
2494 	intel_state->uapi.dst = drm_plane_state_dest(plane_state);
2495 
2496 	if (plane_config->tiling)
2497 		dev_priv->preserve_bios_swizzle = true;
2498 
2499 	plane_state->fb = fb;
2500 	drm_framebuffer_get(fb);
2501 
2502 	plane_state->crtc = &intel_crtc->base;
2503 	intel_plane_copy_uapi_to_hw_state(intel_state, intel_state,
2504 					  intel_crtc);
2505 
2506 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
2507 
2508 	atomic_or(to_intel_plane(primary)->frontbuffer_bit,
2509 		  &to_intel_frontbuffer(fb)->bits);
2510 }
2511 
2512 unsigned int
2513 intel_plane_fence_y_offset(const struct intel_plane_state *plane_state)
2514 {
2515 	int x = 0, y = 0;
2516 
2517 	intel_plane_adjust_aligned_offset(&x, &y, plane_state, 0,
2518 					  plane_state->color_plane[0].offset, 0);
2519 
2520 	return y;
2521 }
2522 
2523 static int
2524 __intel_display_resume(struct drm_device *dev,
2525 		       struct drm_atomic_state *state,
2526 		       struct drm_modeset_acquire_ctx *ctx)
2527 {
2528 	struct drm_crtc_state *crtc_state;
2529 	struct drm_crtc *crtc;
2530 	int i, ret;
2531 
2532 	intel_modeset_setup_hw_state(dev, ctx);
2533 	intel_vga_redisable(to_i915(dev));
2534 
2535 	if (!state)
2536 		return 0;
2537 
2538 	/*
2539 	 * We've duplicated the state, pointers to the old state are invalid.
2540 	 *
2541 	 * Don't attempt to use the old state until we commit the duplicated state.
2542 	 */
2543 	for_each_new_crtc_in_state(state, crtc, crtc_state, i) {
2544 		/*
2545 		 * Force recalculation even if we restore
2546 		 * current state. With fast modeset this may not result
2547 		 * in a modeset when the state is compatible.
2548 		 */
2549 		crtc_state->mode_changed = true;
2550 	}
2551 
2552 	/* ignore any reset values/BIOS leftovers in the WM registers */
2553 	if (!HAS_GMCH(to_i915(dev)))
2554 		to_intel_atomic_state(state)->skip_intermediate_wm = true;
2555 
2556 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
2557 
2558 	drm_WARN_ON(dev, ret == -EDEADLK);
2559 	return ret;
2560 }
2561 
2562 static bool gpu_reset_clobbers_display(struct drm_i915_private *dev_priv)
2563 {
2564 	return (INTEL_INFO(dev_priv)->gpu_reset_clobbers_display &&
2565 		intel_has_gpu_reset(&dev_priv->gt));
2566 }
2567 
2568 void intel_display_prepare_reset(struct drm_i915_private *dev_priv)
2569 {
2570 	struct drm_device *dev = &dev_priv->drm;
2571 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
2572 	struct drm_atomic_state *state;
2573 	int ret;
2574 
2575 	if (!HAS_DISPLAY(dev_priv))
2576 		return;
2577 
2578 	/* reset doesn't touch the display */
2579 	if (!dev_priv->params.force_reset_modeset_test &&
2580 	    !gpu_reset_clobbers_display(dev_priv))
2581 		return;
2582 
2583 	/* We have a modeset vs reset deadlock, defensively unbreak it. */
2584 	set_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
2585 	smp_mb__after_atomic();
2586 	wake_up_bit(&dev_priv->gt.reset.flags, I915_RESET_MODESET);
2587 
2588 	if (atomic_read(&dev_priv->gpu_error.pending_fb_pin)) {
2589 		drm_dbg_kms(&dev_priv->drm,
2590 			    "Modeset potentially stuck, unbreaking through wedging\n");
2591 		intel_gt_set_wedged(&dev_priv->gt);
2592 	}
2593 
2594 	/*
2595 	 * Need mode_config.mutex so that we don't
2596 	 * trample ongoing ->detect() and whatnot.
2597 	 */
2598 	mutex_lock(&dev->mode_config.mutex);
2599 	drm_modeset_acquire_init(ctx, 0);
2600 	while (1) {
2601 		ret = drm_modeset_lock_all_ctx(dev, ctx);
2602 		if (ret != -EDEADLK)
2603 			break;
2604 
2605 		drm_modeset_backoff(ctx);
2606 	}
2607 	/*
2608 	 * Disabling the crtcs gracefully seems nicer. Also the
2609 	 * g33 docs say we should at least disable all the planes.
2610 	 */
2611 	state = drm_atomic_helper_duplicate_state(dev, ctx);
2612 	if (IS_ERR(state)) {
2613 		ret = PTR_ERR(state);
2614 		drm_err(&dev_priv->drm, "Duplicating state failed with %i\n",
2615 			ret);
2616 		return;
2617 	}
2618 
2619 	ret = drm_atomic_helper_disable_all(dev, ctx);
2620 	if (ret) {
2621 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
2622 			ret);
2623 		drm_atomic_state_put(state);
2624 		return;
2625 	}
2626 
2627 	dev_priv->modeset_restore_state = state;
2628 	state->acquire_ctx = ctx;
2629 }
2630 
2631 void intel_display_finish_reset(struct drm_i915_private *dev_priv)
2632 {
2633 	struct drm_device *dev = &dev_priv->drm;
2634 	struct drm_modeset_acquire_ctx *ctx = &dev_priv->reset_ctx;
2635 	struct drm_atomic_state *state;
2636 	int ret;
2637 
2638 	if (!HAS_DISPLAY(dev_priv))
2639 		return;
2640 
2641 	/* reset doesn't touch the display */
2642 	if (!test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
2643 		return;
2644 
2645 	state = fetch_and_zero(&dev_priv->modeset_restore_state);
2646 	if (!state)
2647 		goto unlock;
2648 
2649 	/* reset doesn't touch the display */
2650 	if (!gpu_reset_clobbers_display(dev_priv)) {
2651 		/* for testing only restore the display */
2652 		ret = __intel_display_resume(dev, state, ctx);
2653 		if (ret)
2654 			drm_err(&dev_priv->drm,
2655 				"Restoring old state failed with %i\n", ret);
2656 	} else {
2657 		/*
2658 		 * The display has been reset as well,
2659 		 * so need a full re-initialization.
2660 		 */
2661 		intel_pps_unlock_regs_wa(dev_priv);
2662 		intel_modeset_init_hw(dev_priv);
2663 		intel_init_clock_gating(dev_priv);
2664 		intel_hpd_init(dev_priv);
2665 
2666 		ret = __intel_display_resume(dev, state, ctx);
2667 		if (ret)
2668 			drm_err(&dev_priv->drm,
2669 				"Restoring old state failed with %i\n", ret);
2670 
2671 		intel_hpd_poll_disable(dev_priv);
2672 	}
2673 
2674 	drm_atomic_state_put(state);
2675 unlock:
2676 	drm_modeset_drop_locks(ctx);
2677 	drm_modeset_acquire_fini(ctx);
2678 	mutex_unlock(&dev->mode_config.mutex);
2679 
2680 	clear_bit_unlock(I915_RESET_MODESET, &dev_priv->gt.reset.flags);
2681 }
2682 
2683 static void icl_set_pipe_chicken(struct intel_crtc *crtc)
2684 {
2685 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2686 	enum pipe pipe = crtc->pipe;
2687 	u32 tmp;
2688 
2689 	tmp = intel_de_read(dev_priv, PIPE_CHICKEN(pipe));
2690 
2691 	/*
2692 	 * Display WA #1153: icl
2693 	 * enable hardware to bypass the alpha math
2694 	 * and rounding for per-pixel values 00 and 0xff
2695 	 */
2696 	tmp |= PER_PIXEL_ALPHA_BYPASS_EN;
2697 	/*
2698 	 * Display WA # 1605353570: icl
2699 	 * Set the pixel rounding bit to 1 for allowing
2700 	 * passthrough of Frame buffer pixels unmodified
2701 	 * across pipe
2702 	 */
2703 	tmp |= PIXEL_ROUNDING_TRUNC_FB_PASSTHRU;
2704 	intel_de_write(dev_priv, PIPE_CHICKEN(pipe), tmp);
2705 }
2706 
2707 bool intel_has_pending_fb_unpin(struct drm_i915_private *dev_priv)
2708 {
2709 	struct drm_crtc *crtc;
2710 	bool cleanup_done;
2711 
2712 	drm_for_each_crtc(crtc, &dev_priv->drm) {
2713 		struct drm_crtc_commit *commit;
2714 		spin_lock(&crtc->commit_lock);
2715 		commit = list_first_entry_or_null(&crtc->commit_list,
2716 						  struct drm_crtc_commit, commit_entry);
2717 		cleanup_done = commit ?
2718 			try_wait_for_completion(&commit->cleanup_done) : true;
2719 		spin_unlock(&crtc->commit_lock);
2720 
2721 		if (cleanup_done)
2722 			continue;
2723 
2724 		drm_crtc_wait_one_vblank(crtc);
2725 
2726 		return true;
2727 	}
2728 
2729 	return false;
2730 }
2731 
2732 void lpt_disable_iclkip(struct drm_i915_private *dev_priv)
2733 {
2734 	u32 temp;
2735 
2736 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_GATE);
2737 
2738 	mutex_lock(&dev_priv->sb_lock);
2739 
2740 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2741 	temp |= SBI_SSCCTL_DISABLE;
2742 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2743 
2744 	mutex_unlock(&dev_priv->sb_lock);
2745 }
2746 
2747 /* Program iCLKIP clock to the desired frequency */
2748 static void lpt_program_iclkip(const struct intel_crtc_state *crtc_state)
2749 {
2750 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2751 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2752 	int clock = crtc_state->hw.adjusted_mode.crtc_clock;
2753 	u32 divsel, phaseinc, auxdiv, phasedir = 0;
2754 	u32 temp;
2755 
2756 	lpt_disable_iclkip(dev_priv);
2757 
2758 	/* The iCLK virtual clock root frequency is in MHz,
2759 	 * but the adjusted_mode->crtc_clock in in KHz. To get the
2760 	 * divisors, it is necessary to divide one by another, so we
2761 	 * convert the virtual clock precision to KHz here for higher
2762 	 * precision.
2763 	 */
2764 	for (auxdiv = 0; auxdiv < 2; auxdiv++) {
2765 		u32 iclk_virtual_root_freq = 172800 * 1000;
2766 		u32 iclk_pi_range = 64;
2767 		u32 desired_divisor;
2768 
2769 		desired_divisor = DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
2770 						    clock << auxdiv);
2771 		divsel = (desired_divisor / iclk_pi_range) - 2;
2772 		phaseinc = desired_divisor % iclk_pi_range;
2773 
2774 		/*
2775 		 * Near 20MHz is a corner case which is
2776 		 * out of range for the 7-bit divisor
2777 		 */
2778 		if (divsel <= 0x7f)
2779 			break;
2780 	}
2781 
2782 	/* This should not happen with any sane values */
2783 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIVSEL(divsel) &
2784 		    ~SBI_SSCDIVINTPHASE_DIVSEL_MASK);
2785 	drm_WARN_ON(&dev_priv->drm, SBI_SSCDIVINTPHASE_DIR(phasedir) &
2786 		    ~SBI_SSCDIVINTPHASE_INCVAL_MASK);
2787 
2788 	drm_dbg_kms(&dev_priv->drm,
2789 		    "iCLKIP clock: found settings for %dKHz refresh rate: auxdiv=%x, divsel=%x, phasedir=%x, phaseinc=%x\n",
2790 		    clock, auxdiv, divsel, phasedir, phaseinc);
2791 
2792 	mutex_lock(&dev_priv->sb_lock);
2793 
2794 	/* Program SSCDIVINTPHASE6 */
2795 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2796 	temp &= ~SBI_SSCDIVINTPHASE_DIVSEL_MASK;
2797 	temp |= SBI_SSCDIVINTPHASE_DIVSEL(divsel);
2798 	temp &= ~SBI_SSCDIVINTPHASE_INCVAL_MASK;
2799 	temp |= SBI_SSCDIVINTPHASE_INCVAL(phaseinc);
2800 	temp |= SBI_SSCDIVINTPHASE_DIR(phasedir);
2801 	temp |= SBI_SSCDIVINTPHASE_PROPAGATE;
2802 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE6, temp, SBI_ICLK);
2803 
2804 	/* Program SSCAUXDIV */
2805 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2806 	temp &= ~SBI_SSCAUXDIV_FINALDIV2SEL(1);
2807 	temp |= SBI_SSCAUXDIV_FINALDIV2SEL(auxdiv);
2808 	intel_sbi_write(dev_priv, SBI_SSCAUXDIV6, temp, SBI_ICLK);
2809 
2810 	/* Enable modulator and associated divider */
2811 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2812 	temp &= ~SBI_SSCCTL_DISABLE;
2813 	intel_sbi_write(dev_priv, SBI_SSCCTL6, temp, SBI_ICLK);
2814 
2815 	mutex_unlock(&dev_priv->sb_lock);
2816 
2817 	/* Wait for initialization time */
2818 	udelay(24);
2819 
2820 	intel_de_write(dev_priv, PIXCLK_GATE, PIXCLK_GATE_UNGATE);
2821 }
2822 
2823 int lpt_get_iclkip(struct drm_i915_private *dev_priv)
2824 {
2825 	u32 divsel, phaseinc, auxdiv;
2826 	u32 iclk_virtual_root_freq = 172800 * 1000;
2827 	u32 iclk_pi_range = 64;
2828 	u32 desired_divisor;
2829 	u32 temp;
2830 
2831 	if ((intel_de_read(dev_priv, PIXCLK_GATE) & PIXCLK_GATE_UNGATE) == 0)
2832 		return 0;
2833 
2834 	mutex_lock(&dev_priv->sb_lock);
2835 
2836 	temp = intel_sbi_read(dev_priv, SBI_SSCCTL6, SBI_ICLK);
2837 	if (temp & SBI_SSCCTL_DISABLE) {
2838 		mutex_unlock(&dev_priv->sb_lock);
2839 		return 0;
2840 	}
2841 
2842 	temp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE6, SBI_ICLK);
2843 	divsel = (temp & SBI_SSCDIVINTPHASE_DIVSEL_MASK) >>
2844 		SBI_SSCDIVINTPHASE_DIVSEL_SHIFT;
2845 	phaseinc = (temp & SBI_SSCDIVINTPHASE_INCVAL_MASK) >>
2846 		SBI_SSCDIVINTPHASE_INCVAL_SHIFT;
2847 
2848 	temp = intel_sbi_read(dev_priv, SBI_SSCAUXDIV6, SBI_ICLK);
2849 	auxdiv = (temp & SBI_SSCAUXDIV_FINALDIV2SEL_MASK) >>
2850 		SBI_SSCAUXDIV_FINALDIV2SEL_SHIFT;
2851 
2852 	mutex_unlock(&dev_priv->sb_lock);
2853 
2854 	desired_divisor = (divsel + 2) * iclk_pi_range + phaseinc;
2855 
2856 	return DIV_ROUND_CLOSEST(iclk_virtual_root_freq,
2857 				 desired_divisor << auxdiv);
2858 }
2859 
2860 static void ilk_pch_transcoder_set_timings(const struct intel_crtc_state *crtc_state,
2861 					   enum pipe pch_transcoder)
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 transcoder cpu_transcoder = crtc_state->cpu_transcoder;
2866 
2867 	intel_de_write(dev_priv, PCH_TRANS_HTOTAL(pch_transcoder),
2868 		       intel_de_read(dev_priv, HTOTAL(cpu_transcoder)));
2869 	intel_de_write(dev_priv, PCH_TRANS_HBLANK(pch_transcoder),
2870 		       intel_de_read(dev_priv, HBLANK(cpu_transcoder)));
2871 	intel_de_write(dev_priv, PCH_TRANS_HSYNC(pch_transcoder),
2872 		       intel_de_read(dev_priv, HSYNC(cpu_transcoder)));
2873 
2874 	intel_de_write(dev_priv, PCH_TRANS_VTOTAL(pch_transcoder),
2875 		       intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
2876 	intel_de_write(dev_priv, PCH_TRANS_VBLANK(pch_transcoder),
2877 		       intel_de_read(dev_priv, VBLANK(cpu_transcoder)));
2878 	intel_de_write(dev_priv, PCH_TRANS_VSYNC(pch_transcoder),
2879 		       intel_de_read(dev_priv, VSYNC(cpu_transcoder)));
2880 	intel_de_write(dev_priv, PCH_TRANS_VSYNCSHIFT(pch_transcoder),
2881 		       intel_de_read(dev_priv, VSYNCSHIFT(cpu_transcoder)));
2882 }
2883 
2884 static void cpt_set_fdi_bc_bifurcation(struct drm_i915_private *dev_priv, bool enable)
2885 {
2886 	u32 temp;
2887 
2888 	temp = intel_de_read(dev_priv, SOUTH_CHICKEN1);
2889 	if (!!(temp & FDI_BC_BIFURCATION_SELECT) == enable)
2890 		return;
2891 
2892 	drm_WARN_ON(&dev_priv->drm,
2893 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_B)) &
2894 		    FDI_RX_ENABLE);
2895 	drm_WARN_ON(&dev_priv->drm,
2896 		    intel_de_read(dev_priv, FDI_RX_CTL(PIPE_C)) &
2897 		    FDI_RX_ENABLE);
2898 
2899 	temp &= ~FDI_BC_BIFURCATION_SELECT;
2900 	if (enable)
2901 		temp |= FDI_BC_BIFURCATION_SELECT;
2902 
2903 	drm_dbg_kms(&dev_priv->drm, "%sabling fdi C rx\n",
2904 		    enable ? "en" : "dis");
2905 	intel_de_write(dev_priv, SOUTH_CHICKEN1, temp);
2906 	intel_de_posting_read(dev_priv, SOUTH_CHICKEN1);
2907 }
2908 
2909 static void ivb_update_fdi_bc_bifurcation(const struct intel_crtc_state *crtc_state)
2910 {
2911 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2912 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
2913 
2914 	switch (crtc->pipe) {
2915 	case PIPE_A:
2916 		break;
2917 	case PIPE_B:
2918 		if (crtc_state->fdi_lanes > 2)
2919 			cpt_set_fdi_bc_bifurcation(dev_priv, false);
2920 		else
2921 			cpt_set_fdi_bc_bifurcation(dev_priv, true);
2922 
2923 		break;
2924 	case PIPE_C:
2925 		cpt_set_fdi_bc_bifurcation(dev_priv, true);
2926 
2927 		break;
2928 	default:
2929 		BUG();
2930 	}
2931 }
2932 
2933 /*
2934  * Finds the encoder associated with the given CRTC. This can only be
2935  * used when we know that the CRTC isn't feeding multiple encoders!
2936  */
2937 struct intel_encoder *
2938 intel_get_crtc_new_encoder(const struct intel_atomic_state *state,
2939 			   const struct intel_crtc_state *crtc_state)
2940 {
2941 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2942 	const struct drm_connector_state *connector_state;
2943 	const struct drm_connector *connector;
2944 	struct intel_encoder *encoder = NULL;
2945 	int num_encoders = 0;
2946 	int i;
2947 
2948 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
2949 		if (connector_state->crtc != &crtc->base)
2950 			continue;
2951 
2952 		encoder = to_intel_encoder(connector_state->best_encoder);
2953 		num_encoders++;
2954 	}
2955 
2956 	drm_WARN(encoder->base.dev, num_encoders != 1,
2957 		 "%d encoders for pipe %c\n",
2958 		 num_encoders, pipe_name(crtc->pipe));
2959 
2960 	return encoder;
2961 }
2962 
2963 /*
2964  * Enable PCH resources required for PCH ports:
2965  *   - PCH PLLs
2966  *   - FDI training & RX/TX
2967  *   - update transcoder timings
2968  *   - DP transcoding bits
2969  *   - transcoder
2970  */
2971 static void ilk_pch_enable(const struct intel_atomic_state *state,
2972 			   const struct intel_crtc_state *crtc_state)
2973 {
2974 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
2975 	struct drm_device *dev = crtc->base.dev;
2976 	struct drm_i915_private *dev_priv = to_i915(dev);
2977 	enum pipe pipe = crtc->pipe;
2978 	u32 temp;
2979 
2980 	assert_pch_transcoder_disabled(dev_priv, pipe);
2981 
2982 	if (IS_IVYBRIDGE(dev_priv))
2983 		ivb_update_fdi_bc_bifurcation(crtc_state);
2984 
2985 	/* Write the TU size bits before fdi link training, so that error
2986 	 * detection works. */
2987 	intel_de_write(dev_priv, FDI_RX_TUSIZE1(pipe),
2988 		       intel_de_read(dev_priv, PIPE_DATA_M1(pipe)) & TU_SIZE_MASK);
2989 
2990 	/* For PCH output, training FDI link */
2991 	dev_priv->display.fdi_link_train(crtc, crtc_state);
2992 
2993 	/* We need to program the right clock selection before writing the pixel
2994 	 * mutliplier into the DPLL. */
2995 	if (HAS_PCH_CPT(dev_priv)) {
2996 		u32 sel;
2997 
2998 		temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
2999 		temp |= TRANS_DPLL_ENABLE(pipe);
3000 		sel = TRANS_DPLLB_SEL(pipe);
3001 		if (crtc_state->shared_dpll ==
3002 		    intel_get_shared_dpll_by_id(dev_priv, DPLL_ID_PCH_PLL_B))
3003 			temp |= sel;
3004 		else
3005 			temp &= ~sel;
3006 		intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
3007 	}
3008 
3009 	/* XXX: pch pll's can be enabled any time before we enable the PCH
3010 	 * transcoder, and we actually should do this to not upset any PCH
3011 	 * transcoder that already use the clock when we share it.
3012 	 *
3013 	 * Note that enable_shared_dpll tries to do the right thing, but
3014 	 * get_shared_dpll unconditionally resets the pll - we need that to have
3015 	 * the right LVDS enable sequence. */
3016 	intel_enable_shared_dpll(crtc_state);
3017 
3018 	/* set transcoder timing, panel must allow it */
3019 	assert_panel_unlocked(dev_priv, pipe);
3020 	ilk_pch_transcoder_set_timings(crtc_state, pipe);
3021 
3022 	intel_fdi_normal_train(crtc);
3023 
3024 	/* For PCH DP, enable TRANS_DP_CTL */
3025 	if (HAS_PCH_CPT(dev_priv) &&
3026 	    intel_crtc_has_dp_encoder(crtc_state)) {
3027 		const struct drm_display_mode *adjusted_mode =
3028 			&crtc_state->hw.adjusted_mode;
3029 		u32 bpc = (intel_de_read(dev_priv, PIPECONF(pipe)) & PIPECONF_BPC_MASK) >> 5;
3030 		i915_reg_t reg = TRANS_DP_CTL(pipe);
3031 		enum port port;
3032 
3033 		temp = intel_de_read(dev_priv, reg);
3034 		temp &= ~(TRANS_DP_PORT_SEL_MASK |
3035 			  TRANS_DP_SYNC_MASK |
3036 			  TRANS_DP_BPC_MASK);
3037 		temp |= TRANS_DP_OUTPUT_ENABLE;
3038 		temp |= bpc << 9; /* same format but at 11:9 */
3039 
3040 		if (adjusted_mode->flags & DRM_MODE_FLAG_PHSYNC)
3041 			temp |= TRANS_DP_HSYNC_ACTIVE_HIGH;
3042 		if (adjusted_mode->flags & DRM_MODE_FLAG_PVSYNC)
3043 			temp |= TRANS_DP_VSYNC_ACTIVE_HIGH;
3044 
3045 		port = intel_get_crtc_new_encoder(state, crtc_state)->port;
3046 		drm_WARN_ON(dev, port < PORT_B || port > PORT_D);
3047 		temp |= TRANS_DP_PORT_SEL(port);
3048 
3049 		intel_de_write(dev_priv, reg, temp);
3050 	}
3051 
3052 	ilk_enable_pch_transcoder(crtc_state);
3053 }
3054 
3055 void lpt_pch_enable(const struct intel_crtc_state *crtc_state)
3056 {
3057 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3058 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3059 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
3060 
3061 	assert_pch_transcoder_disabled(dev_priv, PIPE_A);
3062 
3063 	lpt_program_iclkip(crtc_state);
3064 
3065 	/* Set transcoder timing. */
3066 	ilk_pch_transcoder_set_timings(crtc_state, PIPE_A);
3067 
3068 	lpt_enable_pch_transcoder(dev_priv, cpu_transcoder);
3069 }
3070 
3071 static void cpt_verify_modeset(struct drm_i915_private *dev_priv,
3072 			       enum pipe pipe)
3073 {
3074 	i915_reg_t dslreg = PIPEDSL(pipe);
3075 	u32 temp;
3076 
3077 	temp = intel_de_read(dev_priv, dslreg);
3078 	udelay(500);
3079 	if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5)) {
3080 		if (wait_for(intel_de_read(dev_priv, dslreg) != temp, 5))
3081 			drm_err(&dev_priv->drm,
3082 				"mode set failed: pipe %c stuck\n",
3083 				pipe_name(pipe));
3084 	}
3085 }
3086 
3087 static void ilk_pfit_enable(const struct intel_crtc_state *crtc_state)
3088 {
3089 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3090 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3091 	const struct drm_rect *dst = &crtc_state->pch_pfit.dst;
3092 	enum pipe pipe = crtc->pipe;
3093 	int width = drm_rect_width(dst);
3094 	int height = drm_rect_height(dst);
3095 	int x = dst->x1;
3096 	int y = dst->y1;
3097 
3098 	if (!crtc_state->pch_pfit.enabled)
3099 		return;
3100 
3101 	/* Force use of hard-coded filter coefficients
3102 	 * as some pre-programmed values are broken,
3103 	 * e.g. x201.
3104 	 */
3105 	if (IS_IVYBRIDGE(dev_priv) || IS_HASWELL(dev_priv))
3106 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
3107 			       PF_FILTER_MED_3x3 | PF_PIPE_SEL_IVB(pipe));
3108 	else
3109 		intel_de_write(dev_priv, PF_CTL(pipe), PF_ENABLE |
3110 			       PF_FILTER_MED_3x3);
3111 	intel_de_write(dev_priv, PF_WIN_POS(pipe), x << 16 | y);
3112 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), width << 16 | height);
3113 }
3114 
3115 void hsw_enable_ips(const struct intel_crtc_state *crtc_state)
3116 {
3117 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3118 	struct drm_device *dev = crtc->base.dev;
3119 	struct drm_i915_private *dev_priv = to_i915(dev);
3120 
3121 	if (!crtc_state->ips_enabled)
3122 		return;
3123 
3124 	/*
3125 	 * We can only enable IPS after we enable a plane and wait for a vblank
3126 	 * This function is called from post_plane_update, which is run after
3127 	 * a vblank wait.
3128 	 */
3129 	drm_WARN_ON(dev, !(crtc_state->active_planes & ~BIT(PLANE_CURSOR)));
3130 
3131 	if (IS_BROADWELL(dev_priv)) {
3132 		drm_WARN_ON(dev, sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL,
3133 							 IPS_ENABLE | IPS_PCODE_CONTROL));
3134 		/* Quoting Art Runyan: "its not safe to expect any particular
3135 		 * value in IPS_CTL bit 31 after enabling IPS through the
3136 		 * mailbox." Moreover, the mailbox may return a bogus state,
3137 		 * so we need to just enable it and continue on.
3138 		 */
3139 	} else {
3140 		intel_de_write(dev_priv, IPS_CTL, IPS_ENABLE);
3141 		/* The bit only becomes 1 in the next vblank, so this wait here
3142 		 * is essentially intel_wait_for_vblank. If we don't have this
3143 		 * and don't wait for vblanks until the end of crtc_enable, then
3144 		 * the HW state readout code will complain that the expected
3145 		 * IPS_CTL value is not the one we read. */
3146 		if (intel_de_wait_for_set(dev_priv, IPS_CTL, IPS_ENABLE, 50))
3147 			drm_err(&dev_priv->drm,
3148 				"Timed out waiting for IPS enable\n");
3149 	}
3150 }
3151 
3152 void hsw_disable_ips(const struct intel_crtc_state *crtc_state)
3153 {
3154 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3155 	struct drm_device *dev = crtc->base.dev;
3156 	struct drm_i915_private *dev_priv = to_i915(dev);
3157 
3158 	if (!crtc_state->ips_enabled)
3159 		return;
3160 
3161 	if (IS_BROADWELL(dev_priv)) {
3162 		drm_WARN_ON(dev,
3163 			    sandybridge_pcode_write(dev_priv, DISPLAY_IPS_CONTROL, 0));
3164 		/*
3165 		 * Wait for PCODE to finish disabling IPS. The BSpec specified
3166 		 * 42ms timeout value leads to occasional timeouts so use 100ms
3167 		 * instead.
3168 		 */
3169 		if (intel_de_wait_for_clear(dev_priv, IPS_CTL, IPS_ENABLE, 100))
3170 			drm_err(&dev_priv->drm,
3171 				"Timed out waiting for IPS disable\n");
3172 	} else {
3173 		intel_de_write(dev_priv, IPS_CTL, 0);
3174 		intel_de_posting_read(dev_priv, IPS_CTL);
3175 	}
3176 
3177 	/* We need to wait for a vblank before we can disable the plane. */
3178 	intel_wait_for_vblank(dev_priv, crtc->pipe);
3179 }
3180 
3181 static void intel_crtc_dpms_overlay_disable(struct intel_crtc *intel_crtc)
3182 {
3183 	if (intel_crtc->overlay)
3184 		(void) intel_overlay_switch_off(intel_crtc->overlay);
3185 
3186 	/* Let userspace switch the overlay on again. In most cases userspace
3187 	 * has to recompute where to put it anyway.
3188 	 */
3189 }
3190 
3191 static bool hsw_pre_update_disable_ips(const struct intel_crtc_state *old_crtc_state,
3192 				       const struct intel_crtc_state *new_crtc_state)
3193 {
3194 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
3195 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3196 
3197 	if (!old_crtc_state->ips_enabled)
3198 		return false;
3199 
3200 	if (intel_crtc_needs_modeset(new_crtc_state))
3201 		return true;
3202 
3203 	/*
3204 	 * Workaround : Do not read or write the pipe palette/gamma data while
3205 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3206 	 *
3207 	 * Disable IPS before we program the LUT.
3208 	 */
3209 	if (IS_HASWELL(dev_priv) &&
3210 	    (new_crtc_state->uapi.color_mgmt_changed ||
3211 	     new_crtc_state->update_pipe) &&
3212 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
3213 		return true;
3214 
3215 	return !new_crtc_state->ips_enabled;
3216 }
3217 
3218 static bool hsw_post_update_enable_ips(const struct intel_crtc_state *old_crtc_state,
3219 				       const struct intel_crtc_state *new_crtc_state)
3220 {
3221 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
3222 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3223 
3224 	if (!new_crtc_state->ips_enabled)
3225 		return false;
3226 
3227 	if (intel_crtc_needs_modeset(new_crtc_state))
3228 		return true;
3229 
3230 	/*
3231 	 * Workaround : Do not read or write the pipe palette/gamma data while
3232 	 * GAMMA_MODE is configured for split gamma and IPS_CTL has IPS enabled.
3233 	 *
3234 	 * Re-enable IPS after the LUT has been programmed.
3235 	 */
3236 	if (IS_HASWELL(dev_priv) &&
3237 	    (new_crtc_state->uapi.color_mgmt_changed ||
3238 	     new_crtc_state->update_pipe) &&
3239 	    new_crtc_state->gamma_mode == GAMMA_MODE_MODE_SPLIT)
3240 		return true;
3241 
3242 	/*
3243 	 * We can't read out IPS on broadwell, assume the worst and
3244 	 * forcibly enable IPS on the first fastset.
3245 	 */
3246 	if (new_crtc_state->update_pipe && old_crtc_state->inherited)
3247 		return true;
3248 
3249 	return !old_crtc_state->ips_enabled;
3250 }
3251 
3252 static bool needs_nv12_wa(const struct intel_crtc_state *crtc_state)
3253 {
3254 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3255 
3256 	if (!crtc_state->nv12_planes)
3257 		return false;
3258 
3259 	/* WA Display #0827: Gen9:all */
3260 	if (IS_GEN(dev_priv, 9) && !IS_GEMINILAKE(dev_priv))
3261 		return true;
3262 
3263 	return false;
3264 }
3265 
3266 static bool needs_scalerclk_wa(const struct intel_crtc_state *crtc_state)
3267 {
3268 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
3269 
3270 	/* Wa_2006604312:icl,ehl */
3271 	if (crtc_state->scaler_state.scaler_users > 0 && IS_GEN(dev_priv, 11))
3272 		return true;
3273 
3274 	return false;
3275 }
3276 
3277 static bool planes_enabling(const struct intel_crtc_state *old_crtc_state,
3278 			    const struct intel_crtc_state *new_crtc_state)
3279 {
3280 	return (!old_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state)) &&
3281 		new_crtc_state->active_planes;
3282 }
3283 
3284 static bool planes_disabling(const struct intel_crtc_state *old_crtc_state,
3285 			     const struct intel_crtc_state *new_crtc_state)
3286 {
3287 	return old_crtc_state->active_planes &&
3288 		(!new_crtc_state->active_planes || intel_crtc_needs_modeset(new_crtc_state));
3289 }
3290 
3291 static void intel_post_plane_update(struct intel_atomic_state *state,
3292 				    struct intel_crtc *crtc)
3293 {
3294 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3295 	const struct intel_crtc_state *old_crtc_state =
3296 		intel_atomic_get_old_crtc_state(state, crtc);
3297 	const struct intel_crtc_state *new_crtc_state =
3298 		intel_atomic_get_new_crtc_state(state, crtc);
3299 	enum pipe pipe = crtc->pipe;
3300 
3301 	intel_frontbuffer_flip(dev_priv, new_crtc_state->fb_bits);
3302 
3303 	if (new_crtc_state->update_wm_post && new_crtc_state->hw.active)
3304 		intel_update_watermarks(crtc);
3305 
3306 	if (hsw_post_update_enable_ips(old_crtc_state, new_crtc_state))
3307 		hsw_enable_ips(new_crtc_state);
3308 
3309 	intel_fbc_post_update(state, crtc);
3310 
3311 	if (needs_nv12_wa(old_crtc_state) &&
3312 	    !needs_nv12_wa(new_crtc_state))
3313 		skl_wa_827(dev_priv, pipe, false);
3314 
3315 	if (needs_scalerclk_wa(old_crtc_state) &&
3316 	    !needs_scalerclk_wa(new_crtc_state))
3317 		icl_wa_scalerclkgating(dev_priv, pipe, false);
3318 }
3319 
3320 static void intel_crtc_enable_flip_done(struct intel_atomic_state *state,
3321 					struct intel_crtc *crtc)
3322 {
3323 	const struct intel_crtc_state *crtc_state =
3324 		intel_atomic_get_new_crtc_state(state, crtc);
3325 	u8 update_planes = crtc_state->update_planes;
3326 	const struct intel_plane_state *plane_state;
3327 	struct intel_plane *plane;
3328 	int i;
3329 
3330 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
3331 		if (plane->enable_flip_done &&
3332 		    plane->pipe == crtc->pipe &&
3333 		    update_planes & BIT(plane->id))
3334 			plane->enable_flip_done(plane);
3335 	}
3336 }
3337 
3338 static void intel_crtc_disable_flip_done(struct intel_atomic_state *state,
3339 					 struct intel_crtc *crtc)
3340 {
3341 	const struct intel_crtc_state *crtc_state =
3342 		intel_atomic_get_new_crtc_state(state, crtc);
3343 	u8 update_planes = crtc_state->update_planes;
3344 	const struct intel_plane_state *plane_state;
3345 	struct intel_plane *plane;
3346 	int i;
3347 
3348 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
3349 		if (plane->disable_flip_done &&
3350 		    plane->pipe == crtc->pipe &&
3351 		    update_planes & BIT(plane->id))
3352 			plane->disable_flip_done(plane);
3353 	}
3354 }
3355 
3356 static void intel_crtc_async_flip_disable_wa(struct intel_atomic_state *state,
3357 					     struct intel_crtc *crtc)
3358 {
3359 	struct drm_i915_private *i915 = to_i915(state->base.dev);
3360 	const struct intel_crtc_state *old_crtc_state =
3361 		intel_atomic_get_old_crtc_state(state, crtc);
3362 	const struct intel_crtc_state *new_crtc_state =
3363 		intel_atomic_get_new_crtc_state(state, crtc);
3364 	u8 update_planes = new_crtc_state->update_planes;
3365 	const struct intel_plane_state *old_plane_state;
3366 	struct intel_plane *plane;
3367 	bool need_vbl_wait = false;
3368 	int i;
3369 
3370 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
3371 		if (plane->need_async_flip_disable_wa &&
3372 		    plane->pipe == crtc->pipe &&
3373 		    update_planes & BIT(plane->id)) {
3374 			/*
3375 			 * Apart from the async flip bit we want to
3376 			 * preserve the old state for the plane.
3377 			 */
3378 			plane->async_flip(plane, old_crtc_state,
3379 					  old_plane_state, false);
3380 			need_vbl_wait = true;
3381 		}
3382 	}
3383 
3384 	if (need_vbl_wait)
3385 		intel_wait_for_vblank(i915, crtc->pipe);
3386 }
3387 
3388 static void intel_pre_plane_update(struct intel_atomic_state *state,
3389 				   struct intel_crtc *crtc)
3390 {
3391 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
3392 	const struct intel_crtc_state *old_crtc_state =
3393 		intel_atomic_get_old_crtc_state(state, crtc);
3394 	const struct intel_crtc_state *new_crtc_state =
3395 		intel_atomic_get_new_crtc_state(state, crtc);
3396 	enum pipe pipe = crtc->pipe;
3397 
3398 	if (hsw_pre_update_disable_ips(old_crtc_state, new_crtc_state))
3399 		hsw_disable_ips(old_crtc_state);
3400 
3401 	if (intel_fbc_pre_update(state, crtc))
3402 		intel_wait_for_vblank(dev_priv, pipe);
3403 
3404 	/* Display WA 827 */
3405 	if (!needs_nv12_wa(old_crtc_state) &&
3406 	    needs_nv12_wa(new_crtc_state))
3407 		skl_wa_827(dev_priv, pipe, true);
3408 
3409 	/* Wa_2006604312:icl,ehl */
3410 	if (!needs_scalerclk_wa(old_crtc_state) &&
3411 	    needs_scalerclk_wa(new_crtc_state))
3412 		icl_wa_scalerclkgating(dev_priv, pipe, true);
3413 
3414 	/*
3415 	 * Vblank time updates from the shadow to live plane control register
3416 	 * are blocked if the memory self-refresh mode is active at that
3417 	 * moment. So to make sure the plane gets truly disabled, disable
3418 	 * first the self-refresh mode. The self-refresh enable bit in turn
3419 	 * will be checked/applied by the HW only at the next frame start
3420 	 * event which is after the vblank start event, so we need to have a
3421 	 * wait-for-vblank between disabling the plane and the pipe.
3422 	 */
3423 	if (HAS_GMCH(dev_priv) && old_crtc_state->hw.active &&
3424 	    new_crtc_state->disable_cxsr && intel_set_memory_cxsr(dev_priv, false))
3425 		intel_wait_for_vblank(dev_priv, pipe);
3426 
3427 	/*
3428 	 * IVB workaround: must disable low power watermarks for at least
3429 	 * one frame before enabling scaling.  LP watermarks can be re-enabled
3430 	 * when scaling is disabled.
3431 	 *
3432 	 * WaCxSRDisabledForSpriteScaling:ivb
3433 	 */
3434 	if (old_crtc_state->hw.active &&
3435 	    new_crtc_state->disable_lp_wm && ilk_disable_lp_wm(dev_priv))
3436 		intel_wait_for_vblank(dev_priv, pipe);
3437 
3438 	/*
3439 	 * If we're doing a modeset we don't need to do any
3440 	 * pre-vblank watermark programming here.
3441 	 */
3442 	if (!intel_crtc_needs_modeset(new_crtc_state)) {
3443 		/*
3444 		 * For platforms that support atomic watermarks, program the
3445 		 * 'intermediate' watermarks immediately.  On pre-gen9 platforms, these
3446 		 * will be the intermediate values that are safe for both pre- and
3447 		 * post- vblank; when vblank happens, the 'active' values will be set
3448 		 * to the final 'target' values and we'll do this again to get the
3449 		 * optimal watermarks.  For gen9+ platforms, the values we program here
3450 		 * will be the final target values which will get automatically latched
3451 		 * at vblank time; no further programming will be necessary.
3452 		 *
3453 		 * If a platform hasn't been transitioned to atomic watermarks yet,
3454 		 * we'll continue to update watermarks the old way, if flags tell
3455 		 * us to.
3456 		 */
3457 		if (dev_priv->display.initial_watermarks)
3458 			dev_priv->display.initial_watermarks(state, crtc);
3459 		else if (new_crtc_state->update_wm_pre)
3460 			intel_update_watermarks(crtc);
3461 	}
3462 
3463 	/*
3464 	 * Gen2 reports pipe underruns whenever all planes are disabled.
3465 	 * So disable underrun reporting before all the planes get disabled.
3466 	 *
3467 	 * We do this after .initial_watermarks() so that we have a
3468 	 * chance of catching underruns with the intermediate watermarks
3469 	 * vs. the old plane configuration.
3470 	 */
3471 	if (IS_GEN(dev_priv, 2) && planes_disabling(old_crtc_state, new_crtc_state))
3472 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3473 
3474 	/*
3475 	 * WA for platforms where async address update enable bit
3476 	 * is double buffered and only latched at start of vblank.
3477 	 */
3478 	if (old_crtc_state->uapi.async_flip && !new_crtc_state->uapi.async_flip)
3479 		intel_crtc_async_flip_disable_wa(state, crtc);
3480 }
3481 
3482 static void intel_crtc_disable_planes(struct intel_atomic_state *state,
3483 				      struct intel_crtc *crtc)
3484 {
3485 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3486 	const struct intel_crtc_state *new_crtc_state =
3487 		intel_atomic_get_new_crtc_state(state, crtc);
3488 	unsigned int update_mask = new_crtc_state->update_planes;
3489 	const struct intel_plane_state *old_plane_state;
3490 	struct intel_plane *plane;
3491 	unsigned fb_bits = 0;
3492 	int i;
3493 
3494 	intel_crtc_dpms_overlay_disable(crtc);
3495 
3496 	for_each_old_intel_plane_in_state(state, plane, old_plane_state, i) {
3497 		if (crtc->pipe != plane->pipe ||
3498 		    !(update_mask & BIT(plane->id)))
3499 			continue;
3500 
3501 		intel_disable_plane(plane, new_crtc_state);
3502 
3503 		if (old_plane_state->uapi.visible)
3504 			fb_bits |= plane->frontbuffer_bit;
3505 	}
3506 
3507 	intel_frontbuffer_flip(dev_priv, fb_bits);
3508 }
3509 
3510 /*
3511  * intel_connector_primary_encoder - get the primary encoder for a connector
3512  * @connector: connector for which to return the encoder
3513  *
3514  * Returns the primary encoder for a connector. There is a 1:1 mapping from
3515  * all connectors to their encoder, except for DP-MST connectors which have
3516  * both a virtual and a primary encoder. These DP-MST primary encoders can be
3517  * pointed to by as many DP-MST connectors as there are pipes.
3518  */
3519 static struct intel_encoder *
3520 intel_connector_primary_encoder(struct intel_connector *connector)
3521 {
3522 	struct intel_encoder *encoder;
3523 
3524 	if (connector->mst_port)
3525 		return &dp_to_dig_port(connector->mst_port)->base;
3526 
3527 	encoder = intel_attached_encoder(connector);
3528 	drm_WARN_ON(connector->base.dev, !encoder);
3529 
3530 	return encoder;
3531 }
3532 
3533 static void intel_encoders_update_prepare(struct intel_atomic_state *state)
3534 {
3535 	struct drm_connector_state *new_conn_state;
3536 	struct drm_connector *connector;
3537 	int i;
3538 
3539 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
3540 					i) {
3541 		struct intel_connector *intel_connector;
3542 		struct intel_encoder *encoder;
3543 		struct intel_crtc *crtc;
3544 
3545 		if (!intel_connector_needs_modeset(state, connector))
3546 			continue;
3547 
3548 		intel_connector = to_intel_connector(connector);
3549 		encoder = intel_connector_primary_encoder(intel_connector);
3550 		if (!encoder->update_prepare)
3551 			continue;
3552 
3553 		crtc = new_conn_state->crtc ?
3554 			to_intel_crtc(new_conn_state->crtc) : NULL;
3555 		encoder->update_prepare(state, encoder, crtc);
3556 	}
3557 }
3558 
3559 static void intel_encoders_update_complete(struct intel_atomic_state *state)
3560 {
3561 	struct drm_connector_state *new_conn_state;
3562 	struct drm_connector *connector;
3563 	int i;
3564 
3565 	for_each_new_connector_in_state(&state->base, connector, new_conn_state,
3566 					i) {
3567 		struct intel_connector *intel_connector;
3568 		struct intel_encoder *encoder;
3569 		struct intel_crtc *crtc;
3570 
3571 		if (!intel_connector_needs_modeset(state, connector))
3572 			continue;
3573 
3574 		intel_connector = to_intel_connector(connector);
3575 		encoder = intel_connector_primary_encoder(intel_connector);
3576 		if (!encoder->update_complete)
3577 			continue;
3578 
3579 		crtc = new_conn_state->crtc ?
3580 			to_intel_crtc(new_conn_state->crtc) : NULL;
3581 		encoder->update_complete(state, encoder, crtc);
3582 	}
3583 }
3584 
3585 static void intel_encoders_pre_pll_enable(struct intel_atomic_state *state,
3586 					  struct intel_crtc *crtc)
3587 {
3588 	const struct intel_crtc_state *crtc_state =
3589 		intel_atomic_get_new_crtc_state(state, crtc);
3590 	const struct drm_connector_state *conn_state;
3591 	struct drm_connector *conn;
3592 	int i;
3593 
3594 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3595 		struct intel_encoder *encoder =
3596 			to_intel_encoder(conn_state->best_encoder);
3597 
3598 		if (conn_state->crtc != &crtc->base)
3599 			continue;
3600 
3601 		if (encoder->pre_pll_enable)
3602 			encoder->pre_pll_enable(state, encoder,
3603 						crtc_state, conn_state);
3604 	}
3605 }
3606 
3607 static void intel_encoders_pre_enable(struct intel_atomic_state *state,
3608 				      struct intel_crtc *crtc)
3609 {
3610 	const struct intel_crtc_state *crtc_state =
3611 		intel_atomic_get_new_crtc_state(state, crtc);
3612 	const struct drm_connector_state *conn_state;
3613 	struct drm_connector *conn;
3614 	int i;
3615 
3616 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3617 		struct intel_encoder *encoder =
3618 			to_intel_encoder(conn_state->best_encoder);
3619 
3620 		if (conn_state->crtc != &crtc->base)
3621 			continue;
3622 
3623 		if (encoder->pre_enable)
3624 			encoder->pre_enable(state, encoder,
3625 					    crtc_state, conn_state);
3626 	}
3627 }
3628 
3629 static void intel_encoders_enable(struct intel_atomic_state *state,
3630 				  struct intel_crtc *crtc)
3631 {
3632 	const struct intel_crtc_state *crtc_state =
3633 		intel_atomic_get_new_crtc_state(state, crtc);
3634 	const struct drm_connector_state *conn_state;
3635 	struct drm_connector *conn;
3636 	int i;
3637 
3638 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3639 		struct intel_encoder *encoder =
3640 			to_intel_encoder(conn_state->best_encoder);
3641 
3642 		if (conn_state->crtc != &crtc->base)
3643 			continue;
3644 
3645 		if (encoder->enable)
3646 			encoder->enable(state, encoder,
3647 					crtc_state, conn_state);
3648 		intel_opregion_notify_encoder(encoder, true);
3649 	}
3650 }
3651 
3652 static void intel_encoders_disable(struct intel_atomic_state *state,
3653 				   struct intel_crtc *crtc)
3654 {
3655 	const struct intel_crtc_state *old_crtc_state =
3656 		intel_atomic_get_old_crtc_state(state, crtc);
3657 	const struct drm_connector_state *old_conn_state;
3658 	struct drm_connector *conn;
3659 	int i;
3660 
3661 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3662 		struct intel_encoder *encoder =
3663 			to_intel_encoder(old_conn_state->best_encoder);
3664 
3665 		if (old_conn_state->crtc != &crtc->base)
3666 			continue;
3667 
3668 		intel_opregion_notify_encoder(encoder, false);
3669 		if (encoder->disable)
3670 			encoder->disable(state, encoder,
3671 					 old_crtc_state, old_conn_state);
3672 	}
3673 }
3674 
3675 static void intel_encoders_post_disable(struct intel_atomic_state *state,
3676 					struct intel_crtc *crtc)
3677 {
3678 	const struct intel_crtc_state *old_crtc_state =
3679 		intel_atomic_get_old_crtc_state(state, crtc);
3680 	const struct drm_connector_state *old_conn_state;
3681 	struct drm_connector *conn;
3682 	int i;
3683 
3684 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3685 		struct intel_encoder *encoder =
3686 			to_intel_encoder(old_conn_state->best_encoder);
3687 
3688 		if (old_conn_state->crtc != &crtc->base)
3689 			continue;
3690 
3691 		if (encoder->post_disable)
3692 			encoder->post_disable(state, encoder,
3693 					      old_crtc_state, old_conn_state);
3694 	}
3695 }
3696 
3697 static void intel_encoders_post_pll_disable(struct intel_atomic_state *state,
3698 					    struct intel_crtc *crtc)
3699 {
3700 	const struct intel_crtc_state *old_crtc_state =
3701 		intel_atomic_get_old_crtc_state(state, crtc);
3702 	const struct drm_connector_state *old_conn_state;
3703 	struct drm_connector *conn;
3704 	int i;
3705 
3706 	for_each_old_connector_in_state(&state->base, conn, old_conn_state, i) {
3707 		struct intel_encoder *encoder =
3708 			to_intel_encoder(old_conn_state->best_encoder);
3709 
3710 		if (old_conn_state->crtc != &crtc->base)
3711 			continue;
3712 
3713 		if (encoder->post_pll_disable)
3714 			encoder->post_pll_disable(state, encoder,
3715 						  old_crtc_state, old_conn_state);
3716 	}
3717 }
3718 
3719 static void intel_encoders_update_pipe(struct intel_atomic_state *state,
3720 				       struct intel_crtc *crtc)
3721 {
3722 	const struct intel_crtc_state *crtc_state =
3723 		intel_atomic_get_new_crtc_state(state, crtc);
3724 	const struct drm_connector_state *conn_state;
3725 	struct drm_connector *conn;
3726 	int i;
3727 
3728 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3729 		struct intel_encoder *encoder =
3730 			to_intel_encoder(conn_state->best_encoder);
3731 
3732 		if (conn_state->crtc != &crtc->base)
3733 			continue;
3734 
3735 		if (encoder->update_pipe)
3736 			encoder->update_pipe(state, encoder,
3737 					     crtc_state, conn_state);
3738 	}
3739 }
3740 
3741 static void intel_disable_primary_plane(const struct intel_crtc_state *crtc_state)
3742 {
3743 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3744 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
3745 
3746 	plane->disable_plane(plane, crtc_state);
3747 }
3748 
3749 static void ilk_crtc_enable(struct intel_atomic_state *state,
3750 			    struct intel_crtc *crtc)
3751 {
3752 	const struct intel_crtc_state *new_crtc_state =
3753 		intel_atomic_get_new_crtc_state(state, crtc);
3754 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3755 	enum pipe pipe = crtc->pipe;
3756 
3757 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3758 		return;
3759 
3760 	/*
3761 	 * Sometimes spurious CPU pipe underruns happen during FDI
3762 	 * training, at least with VGA+HDMI cloning. Suppress them.
3763 	 *
3764 	 * On ILK we get an occasional spurious CPU pipe underruns
3765 	 * between eDP port A enable and vdd enable. Also PCH port
3766 	 * enable seems to result in the occasional CPU pipe underrun.
3767 	 *
3768 	 * Spurious PCH underruns also occur during PCH enabling.
3769 	 */
3770 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
3771 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
3772 
3773 	if (new_crtc_state->has_pch_encoder)
3774 		intel_prepare_shared_dpll(new_crtc_state);
3775 
3776 	if (intel_crtc_has_dp_encoder(new_crtc_state))
3777 		intel_dp_set_m_n(new_crtc_state, M1_N1);
3778 
3779 	intel_set_transcoder_timings(new_crtc_state);
3780 	intel_set_pipe_src_size(new_crtc_state);
3781 
3782 	if (new_crtc_state->has_pch_encoder)
3783 		intel_cpu_transcoder_set_m_n(new_crtc_state,
3784 					     &new_crtc_state->fdi_m_n, NULL);
3785 
3786 	ilk_set_pipeconf(new_crtc_state);
3787 
3788 	crtc->active = true;
3789 
3790 	intel_encoders_pre_enable(state, crtc);
3791 
3792 	if (new_crtc_state->has_pch_encoder) {
3793 		/* Note: FDI PLL enabling _must_ be done before we enable the
3794 		 * cpu pipes, hence this is separate from all the other fdi/pch
3795 		 * enabling. */
3796 		ilk_fdi_pll_enable(new_crtc_state);
3797 	} else {
3798 		assert_fdi_tx_disabled(dev_priv, pipe);
3799 		assert_fdi_rx_disabled(dev_priv, pipe);
3800 	}
3801 
3802 	ilk_pfit_enable(new_crtc_state);
3803 
3804 	/*
3805 	 * On ILK+ LUT must be loaded before the pipe is running but with
3806 	 * clocks enabled
3807 	 */
3808 	intel_color_load_luts(new_crtc_state);
3809 	intel_color_commit(new_crtc_state);
3810 	/* update DSPCNTR to configure gamma for pipe bottom color */
3811 	intel_disable_primary_plane(new_crtc_state);
3812 
3813 	if (dev_priv->display.initial_watermarks)
3814 		dev_priv->display.initial_watermarks(state, crtc);
3815 	intel_enable_pipe(new_crtc_state);
3816 
3817 	if (new_crtc_state->has_pch_encoder)
3818 		ilk_pch_enable(state, new_crtc_state);
3819 
3820 	intel_crtc_vblank_on(new_crtc_state);
3821 
3822 	intel_encoders_enable(state, crtc);
3823 
3824 	if (HAS_PCH_CPT(dev_priv))
3825 		cpt_verify_modeset(dev_priv, pipe);
3826 
3827 	/*
3828 	 * Must wait for vblank to avoid spurious PCH FIFO underruns.
3829 	 * And a second vblank wait is needed at least on ILK with
3830 	 * some interlaced HDMI modes. Let's do the double wait always
3831 	 * in case there are more corner cases we don't know about.
3832 	 */
3833 	if (new_crtc_state->has_pch_encoder) {
3834 		intel_wait_for_vblank(dev_priv, pipe);
3835 		intel_wait_for_vblank(dev_priv, pipe);
3836 	}
3837 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
3838 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
3839 }
3840 
3841 /* IPS only exists on ULT machines and is tied to pipe A. */
3842 static bool hsw_crtc_supports_ips(struct intel_crtc *crtc)
3843 {
3844 	return HAS_IPS(to_i915(crtc->base.dev)) && crtc->pipe == PIPE_A;
3845 }
3846 
3847 static void glk_pipe_scaler_clock_gating_wa(struct drm_i915_private *dev_priv,
3848 					    enum pipe pipe, bool apply)
3849 {
3850 	u32 val = intel_de_read(dev_priv, CLKGATE_DIS_PSL(pipe));
3851 	u32 mask = DPF_GATING_DIS | DPF_RAM_GATING_DIS | DPFR_GATING_DIS;
3852 
3853 	if (apply)
3854 		val |= mask;
3855 	else
3856 		val &= ~mask;
3857 
3858 	intel_de_write(dev_priv, CLKGATE_DIS_PSL(pipe), val);
3859 }
3860 
3861 static void icl_pipe_mbus_enable(struct intel_crtc *crtc)
3862 {
3863 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3864 	enum pipe pipe = crtc->pipe;
3865 	u32 val;
3866 
3867 	val = MBUS_DBOX_A_CREDIT(2);
3868 
3869 	if (INTEL_GEN(dev_priv) >= 12) {
3870 		val |= MBUS_DBOX_BW_CREDIT(2);
3871 		val |= MBUS_DBOX_B_CREDIT(12);
3872 	} else {
3873 		val |= MBUS_DBOX_BW_CREDIT(1);
3874 		val |= MBUS_DBOX_B_CREDIT(8);
3875 	}
3876 
3877 	intel_de_write(dev_priv, PIPE_MBUS_DBOX_CTL(pipe), val);
3878 }
3879 
3880 static void hsw_set_linetime_wm(const struct intel_crtc_state *crtc_state)
3881 {
3882 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3883 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3884 
3885 	intel_de_write(dev_priv, WM_LINETIME(crtc->pipe),
3886 		       HSW_LINETIME(crtc_state->linetime) |
3887 		       HSW_IPS_LINETIME(crtc_state->ips_linetime));
3888 }
3889 
3890 static void hsw_set_frame_start_delay(const struct intel_crtc_state *crtc_state)
3891 {
3892 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
3893 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3894 	i915_reg_t reg = CHICKEN_TRANS(crtc_state->cpu_transcoder);
3895 	u32 val;
3896 
3897 	val = intel_de_read(dev_priv, reg);
3898 	val &= ~HSW_FRAME_START_DELAY_MASK;
3899 	val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
3900 	intel_de_write(dev_priv, reg, val);
3901 }
3902 
3903 static void icl_ddi_bigjoiner_pre_enable(struct intel_atomic_state *state,
3904 					 const struct intel_crtc_state *crtc_state)
3905 {
3906 	struct intel_crtc *master = to_intel_crtc(crtc_state->uapi.crtc);
3907 	struct intel_crtc_state *master_crtc_state;
3908 	struct drm_connector_state *conn_state;
3909 	struct drm_connector *conn;
3910 	struct intel_encoder *encoder = NULL;
3911 	int i;
3912 
3913 	if (crtc_state->bigjoiner_slave)
3914 		master = crtc_state->bigjoiner_linked_crtc;
3915 
3916 	master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
3917 
3918 	for_each_new_connector_in_state(&state->base, conn, conn_state, i) {
3919 		if (conn_state->crtc != &master->base)
3920 			continue;
3921 
3922 		encoder = to_intel_encoder(conn_state->best_encoder);
3923 		break;
3924 	}
3925 
3926 	if (!crtc_state->bigjoiner_slave) {
3927 		/* need to enable VDSC, which we skipped in pre-enable */
3928 		intel_dsc_enable(encoder, crtc_state);
3929 	} else {
3930 		/*
3931 		 * Enable sequence steps 1-7 on bigjoiner master
3932 		 */
3933 		intel_encoders_pre_pll_enable(state, master);
3934 		intel_enable_shared_dpll(master_crtc_state);
3935 		intel_encoders_pre_enable(state, master);
3936 
3937 		/* and DSC on slave */
3938 		intel_dsc_enable(NULL, crtc_state);
3939 	}
3940 }
3941 
3942 static void hsw_crtc_enable(struct intel_atomic_state *state,
3943 			    struct intel_crtc *crtc)
3944 {
3945 	const struct intel_crtc_state *new_crtc_state =
3946 		intel_atomic_get_new_crtc_state(state, crtc);
3947 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
3948 	enum pipe pipe = crtc->pipe, hsw_workaround_pipe;
3949 	enum transcoder cpu_transcoder = new_crtc_state->cpu_transcoder;
3950 	bool psl_clkgate_wa;
3951 
3952 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
3953 		return;
3954 
3955 	if (!new_crtc_state->bigjoiner) {
3956 		intel_encoders_pre_pll_enable(state, crtc);
3957 
3958 		if (new_crtc_state->shared_dpll)
3959 			intel_enable_shared_dpll(new_crtc_state);
3960 
3961 		intel_encoders_pre_enable(state, crtc);
3962 	} else {
3963 		icl_ddi_bigjoiner_pre_enable(state, new_crtc_state);
3964 	}
3965 
3966 	intel_set_pipe_src_size(new_crtc_state);
3967 	if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
3968 		bdw_set_pipemisc(new_crtc_state);
3969 
3970 	if (!new_crtc_state->bigjoiner_slave && !transcoder_is_dsi(cpu_transcoder)) {
3971 		intel_set_transcoder_timings(new_crtc_state);
3972 
3973 		if (cpu_transcoder != TRANSCODER_EDP)
3974 			intel_de_write(dev_priv, PIPE_MULT(cpu_transcoder),
3975 				       new_crtc_state->pixel_multiplier - 1);
3976 
3977 		if (new_crtc_state->has_pch_encoder)
3978 			intel_cpu_transcoder_set_m_n(new_crtc_state,
3979 						     &new_crtc_state->fdi_m_n, NULL);
3980 
3981 		hsw_set_frame_start_delay(new_crtc_state);
3982 	}
3983 
3984 	if (!transcoder_is_dsi(cpu_transcoder))
3985 		hsw_set_pipeconf(new_crtc_state);
3986 
3987 	crtc->active = true;
3988 
3989 	/* Display WA #1180: WaDisableScalarClockGating: glk, cnl */
3990 	psl_clkgate_wa = (IS_GEMINILAKE(dev_priv) || IS_CANNONLAKE(dev_priv)) &&
3991 		new_crtc_state->pch_pfit.enabled;
3992 	if (psl_clkgate_wa)
3993 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, true);
3994 
3995 	if (INTEL_GEN(dev_priv) >= 9)
3996 		skl_pfit_enable(new_crtc_state);
3997 	else
3998 		ilk_pfit_enable(new_crtc_state);
3999 
4000 	/*
4001 	 * On ILK+ LUT must be loaded before the pipe is running but with
4002 	 * clocks enabled
4003 	 */
4004 	intel_color_load_luts(new_crtc_state);
4005 	intel_color_commit(new_crtc_state);
4006 	/* update DSPCNTR to configure gamma/csc for pipe bottom color */
4007 	if (INTEL_GEN(dev_priv) < 9)
4008 		intel_disable_primary_plane(new_crtc_state);
4009 
4010 	hsw_set_linetime_wm(new_crtc_state);
4011 
4012 	if (INTEL_GEN(dev_priv) >= 11)
4013 		icl_set_pipe_chicken(crtc);
4014 
4015 	if (dev_priv->display.initial_watermarks)
4016 		dev_priv->display.initial_watermarks(state, crtc);
4017 
4018 	if (INTEL_GEN(dev_priv) >= 11)
4019 		icl_pipe_mbus_enable(crtc);
4020 
4021 	if (new_crtc_state->bigjoiner_slave)
4022 		intel_crtc_vblank_on(new_crtc_state);
4023 
4024 	intel_encoders_enable(state, crtc);
4025 
4026 	if (psl_clkgate_wa) {
4027 		intel_wait_for_vblank(dev_priv, pipe);
4028 		glk_pipe_scaler_clock_gating_wa(dev_priv, pipe, false);
4029 	}
4030 
4031 	/* If we change the relative order between pipe/planes enabling, we need
4032 	 * to change the workaround. */
4033 	hsw_workaround_pipe = new_crtc_state->hsw_workaround_pipe;
4034 	if (IS_HASWELL(dev_priv) && hsw_workaround_pipe != INVALID_PIPE) {
4035 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
4036 		intel_wait_for_vblank(dev_priv, hsw_workaround_pipe);
4037 	}
4038 }
4039 
4040 void ilk_pfit_disable(const struct intel_crtc_state *old_crtc_state)
4041 {
4042 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
4043 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4044 	enum pipe pipe = crtc->pipe;
4045 
4046 	/* To avoid upsetting the power well on haswell only disable the pfit if
4047 	 * it's in use. The hw state code will make sure we get this right. */
4048 	if (!old_crtc_state->pch_pfit.enabled)
4049 		return;
4050 
4051 	intel_de_write(dev_priv, PF_CTL(pipe), 0);
4052 	intel_de_write(dev_priv, PF_WIN_POS(pipe), 0);
4053 	intel_de_write(dev_priv, PF_WIN_SZ(pipe), 0);
4054 }
4055 
4056 static void ilk_crtc_disable(struct intel_atomic_state *state,
4057 			     struct intel_crtc *crtc)
4058 {
4059 	const struct intel_crtc_state *old_crtc_state =
4060 		intel_atomic_get_old_crtc_state(state, crtc);
4061 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4062 	enum pipe pipe = crtc->pipe;
4063 
4064 	/*
4065 	 * Sometimes spurious CPU pipe underruns happen when the
4066 	 * pipe is already disabled, but FDI RX/TX is still enabled.
4067 	 * Happens at least with VGA+HDMI cloning. Suppress them.
4068 	 */
4069 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4070 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, false);
4071 
4072 	intel_encoders_disable(state, crtc);
4073 
4074 	intel_crtc_vblank_off(old_crtc_state);
4075 
4076 	intel_disable_pipe(old_crtc_state);
4077 
4078 	ilk_pfit_disable(old_crtc_state);
4079 
4080 	if (old_crtc_state->has_pch_encoder)
4081 		ilk_fdi_disable(crtc);
4082 
4083 	intel_encoders_post_disable(state, crtc);
4084 
4085 	if (old_crtc_state->has_pch_encoder) {
4086 		ilk_disable_pch_transcoder(dev_priv, pipe);
4087 
4088 		if (HAS_PCH_CPT(dev_priv)) {
4089 			i915_reg_t reg;
4090 			u32 temp;
4091 
4092 			/* disable TRANS_DP_CTL */
4093 			reg = TRANS_DP_CTL(pipe);
4094 			temp = intel_de_read(dev_priv, reg);
4095 			temp &= ~(TRANS_DP_OUTPUT_ENABLE |
4096 				  TRANS_DP_PORT_SEL_MASK);
4097 			temp |= TRANS_DP_PORT_SEL_NONE;
4098 			intel_de_write(dev_priv, reg, temp);
4099 
4100 			/* disable DPLL_SEL */
4101 			temp = intel_de_read(dev_priv, PCH_DPLL_SEL);
4102 			temp &= ~(TRANS_DPLL_ENABLE(pipe) | TRANS_DPLLB_SEL(pipe));
4103 			intel_de_write(dev_priv, PCH_DPLL_SEL, temp);
4104 		}
4105 
4106 		ilk_fdi_pll_disable(crtc);
4107 	}
4108 
4109 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4110 	intel_set_pch_fifo_underrun_reporting(dev_priv, pipe, true);
4111 }
4112 
4113 static void hsw_crtc_disable(struct intel_atomic_state *state,
4114 			     struct intel_crtc *crtc)
4115 {
4116 	/*
4117 	 * FIXME collapse everything to one hook.
4118 	 * Need care with mst->ddi interactions.
4119 	 */
4120 	intel_encoders_disable(state, crtc);
4121 	intel_encoders_post_disable(state, crtc);
4122 }
4123 
4124 static void i9xx_pfit_enable(const struct intel_crtc_state *crtc_state)
4125 {
4126 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4127 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4128 
4129 	if (!crtc_state->gmch_pfit.control)
4130 		return;
4131 
4132 	/*
4133 	 * The panel fitter should only be adjusted whilst the pipe is disabled,
4134 	 * according to register description and PRM.
4135 	 */
4136 	drm_WARN_ON(&dev_priv->drm,
4137 		    intel_de_read(dev_priv, PFIT_CONTROL) & PFIT_ENABLE);
4138 	assert_pipe_disabled(dev_priv, crtc_state->cpu_transcoder);
4139 
4140 	intel_de_write(dev_priv, PFIT_PGM_RATIOS,
4141 		       crtc_state->gmch_pfit.pgm_ratios);
4142 	intel_de_write(dev_priv, PFIT_CONTROL, crtc_state->gmch_pfit.control);
4143 
4144 	/* Border color in case we don't scale up to the full screen. Black by
4145 	 * default, change to something else for debugging. */
4146 	intel_de_write(dev_priv, BCLRPAT(crtc->pipe), 0);
4147 }
4148 
4149 bool intel_phy_is_combo(struct drm_i915_private *dev_priv, enum phy phy)
4150 {
4151 	if (phy == PHY_NONE)
4152 		return false;
4153 	else if (IS_ALDERLAKE_S(dev_priv))
4154 		return phy <= PHY_E;
4155 	else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv))
4156 		return phy <= PHY_D;
4157 	else if (IS_JSL_EHL(dev_priv))
4158 		return phy <= PHY_C;
4159 	else if (INTEL_GEN(dev_priv) >= 11)
4160 		return phy <= PHY_B;
4161 	else
4162 		return false;
4163 }
4164 
4165 bool intel_phy_is_tc(struct drm_i915_private *dev_priv, enum phy phy)
4166 {
4167 	if (IS_TIGERLAKE(dev_priv))
4168 		return phy >= PHY_D && phy <= PHY_I;
4169 	else if (IS_ICELAKE(dev_priv))
4170 		return phy >= PHY_C && phy <= PHY_F;
4171 	else
4172 		return false;
4173 }
4174 
4175 enum phy intel_port_to_phy(struct drm_i915_private *i915, enum port port)
4176 {
4177 	if (IS_ALDERLAKE_S(i915) && port >= PORT_TC1)
4178 		return PHY_B + port - PORT_TC1;
4179 	else if ((IS_DG1(i915) || IS_ROCKETLAKE(i915)) && port >= PORT_TC1)
4180 		return PHY_C + port - PORT_TC1;
4181 	else if (IS_JSL_EHL(i915) && port == PORT_D)
4182 		return PHY_A;
4183 
4184 	return PHY_A + port - PORT_A;
4185 }
4186 
4187 enum tc_port intel_port_to_tc(struct drm_i915_private *dev_priv, enum port port)
4188 {
4189 	if (!intel_phy_is_tc(dev_priv, intel_port_to_phy(dev_priv, port)))
4190 		return TC_PORT_NONE;
4191 
4192 	if (INTEL_GEN(dev_priv) >= 12)
4193 		return TC_PORT_1 + port - PORT_TC1;
4194 	else
4195 		return TC_PORT_1 + port - PORT_C;
4196 }
4197 
4198 enum intel_display_power_domain intel_port_to_power_domain(enum port port)
4199 {
4200 	switch (port) {
4201 	case PORT_A:
4202 		return POWER_DOMAIN_PORT_DDI_A_LANES;
4203 	case PORT_B:
4204 		return POWER_DOMAIN_PORT_DDI_B_LANES;
4205 	case PORT_C:
4206 		return POWER_DOMAIN_PORT_DDI_C_LANES;
4207 	case PORT_D:
4208 		return POWER_DOMAIN_PORT_DDI_D_LANES;
4209 	case PORT_E:
4210 		return POWER_DOMAIN_PORT_DDI_E_LANES;
4211 	case PORT_F:
4212 		return POWER_DOMAIN_PORT_DDI_F_LANES;
4213 	case PORT_G:
4214 		return POWER_DOMAIN_PORT_DDI_G_LANES;
4215 	case PORT_H:
4216 		return POWER_DOMAIN_PORT_DDI_H_LANES;
4217 	case PORT_I:
4218 		return POWER_DOMAIN_PORT_DDI_I_LANES;
4219 	default:
4220 		MISSING_CASE(port);
4221 		return POWER_DOMAIN_PORT_OTHER;
4222 	}
4223 }
4224 
4225 enum intel_display_power_domain
4226 intel_aux_power_domain(struct intel_digital_port *dig_port)
4227 {
4228 	struct drm_i915_private *dev_priv = to_i915(dig_port->base.base.dev);
4229 	enum phy phy = intel_port_to_phy(dev_priv, dig_port->base.port);
4230 
4231 	if (intel_phy_is_tc(dev_priv, phy) &&
4232 	    dig_port->tc_mode == TC_PORT_TBT_ALT) {
4233 		switch (dig_port->aux_ch) {
4234 		case AUX_CH_C:
4235 			return POWER_DOMAIN_AUX_C_TBT;
4236 		case AUX_CH_D:
4237 			return POWER_DOMAIN_AUX_D_TBT;
4238 		case AUX_CH_E:
4239 			return POWER_DOMAIN_AUX_E_TBT;
4240 		case AUX_CH_F:
4241 			return POWER_DOMAIN_AUX_F_TBT;
4242 		case AUX_CH_G:
4243 			return POWER_DOMAIN_AUX_G_TBT;
4244 		case AUX_CH_H:
4245 			return POWER_DOMAIN_AUX_H_TBT;
4246 		case AUX_CH_I:
4247 			return POWER_DOMAIN_AUX_I_TBT;
4248 		default:
4249 			MISSING_CASE(dig_port->aux_ch);
4250 			return POWER_DOMAIN_AUX_C_TBT;
4251 		}
4252 	}
4253 
4254 	return intel_legacy_aux_to_power_domain(dig_port->aux_ch);
4255 }
4256 
4257 /*
4258  * Converts aux_ch to power_domain without caring about TBT ports for that use
4259  * intel_aux_power_domain()
4260  */
4261 enum intel_display_power_domain
4262 intel_legacy_aux_to_power_domain(enum aux_ch aux_ch)
4263 {
4264 	switch (aux_ch) {
4265 	case AUX_CH_A:
4266 		return POWER_DOMAIN_AUX_A;
4267 	case AUX_CH_B:
4268 		return POWER_DOMAIN_AUX_B;
4269 	case AUX_CH_C:
4270 		return POWER_DOMAIN_AUX_C;
4271 	case AUX_CH_D:
4272 		return POWER_DOMAIN_AUX_D;
4273 	case AUX_CH_E:
4274 		return POWER_DOMAIN_AUX_E;
4275 	case AUX_CH_F:
4276 		return POWER_DOMAIN_AUX_F;
4277 	case AUX_CH_G:
4278 		return POWER_DOMAIN_AUX_G;
4279 	case AUX_CH_H:
4280 		return POWER_DOMAIN_AUX_H;
4281 	case AUX_CH_I:
4282 		return POWER_DOMAIN_AUX_I;
4283 	default:
4284 		MISSING_CASE(aux_ch);
4285 		return POWER_DOMAIN_AUX_A;
4286 	}
4287 }
4288 
4289 static u64 get_crtc_power_domains(struct intel_crtc_state *crtc_state)
4290 {
4291 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4292 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4293 	struct drm_encoder *encoder;
4294 	enum pipe pipe = crtc->pipe;
4295 	u64 mask;
4296 	enum transcoder transcoder = crtc_state->cpu_transcoder;
4297 
4298 	if (!crtc_state->hw.active)
4299 		return 0;
4300 
4301 	mask = BIT_ULL(POWER_DOMAIN_PIPE(pipe));
4302 	mask |= BIT_ULL(POWER_DOMAIN_TRANSCODER(transcoder));
4303 	if (crtc_state->pch_pfit.enabled ||
4304 	    crtc_state->pch_pfit.force_thru)
4305 		mask |= BIT_ULL(POWER_DOMAIN_PIPE_PANEL_FITTER(pipe));
4306 
4307 	drm_for_each_encoder_mask(encoder, &dev_priv->drm,
4308 				  crtc_state->uapi.encoder_mask) {
4309 		struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4310 
4311 		mask |= BIT_ULL(intel_encoder->power_domain);
4312 	}
4313 
4314 	if (HAS_DDI(dev_priv) && crtc_state->has_audio)
4315 		mask |= BIT_ULL(POWER_DOMAIN_AUDIO);
4316 
4317 	if (crtc_state->shared_dpll)
4318 		mask |= BIT_ULL(POWER_DOMAIN_DISPLAY_CORE);
4319 
4320 	if (crtc_state->dsc.compression_enable)
4321 		mask |= BIT_ULL(intel_dsc_power_domain(crtc_state));
4322 
4323 	return mask;
4324 }
4325 
4326 static u64
4327 modeset_get_crtc_power_domains(struct intel_crtc_state *crtc_state)
4328 {
4329 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4330 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4331 	enum intel_display_power_domain domain;
4332 	u64 domains, new_domains, old_domains;
4333 
4334 	domains = get_crtc_power_domains(crtc_state);
4335 
4336 	new_domains = domains & ~crtc->enabled_power_domains.mask;
4337 	old_domains = crtc->enabled_power_domains.mask & ~domains;
4338 
4339 	for_each_power_domain(domain, new_domains)
4340 		intel_display_power_get_in_set(dev_priv,
4341 					       &crtc->enabled_power_domains,
4342 					       domain);
4343 
4344 	return old_domains;
4345 }
4346 
4347 static void modeset_put_crtc_power_domains(struct intel_crtc *crtc,
4348 					   u64 domains)
4349 {
4350 	intel_display_power_put_mask_in_set(to_i915(crtc->base.dev),
4351 					    &crtc->enabled_power_domains,
4352 					    domains);
4353 }
4354 
4355 static void valleyview_crtc_enable(struct intel_atomic_state *state,
4356 				   struct intel_crtc *crtc)
4357 {
4358 	const struct intel_crtc_state *new_crtc_state =
4359 		intel_atomic_get_new_crtc_state(state, crtc);
4360 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4361 	enum pipe pipe = crtc->pipe;
4362 
4363 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
4364 		return;
4365 
4366 	if (intel_crtc_has_dp_encoder(new_crtc_state))
4367 		intel_dp_set_m_n(new_crtc_state, M1_N1);
4368 
4369 	intel_set_transcoder_timings(new_crtc_state);
4370 	intel_set_pipe_src_size(new_crtc_state);
4371 
4372 	if (IS_CHERRYVIEW(dev_priv) && pipe == PIPE_B) {
4373 		intel_de_write(dev_priv, CHV_BLEND(pipe), CHV_BLEND_LEGACY);
4374 		intel_de_write(dev_priv, CHV_CANVAS(pipe), 0);
4375 	}
4376 
4377 	i9xx_set_pipeconf(new_crtc_state);
4378 
4379 	crtc->active = true;
4380 
4381 	intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4382 
4383 	intel_encoders_pre_pll_enable(state, crtc);
4384 
4385 	if (IS_CHERRYVIEW(dev_priv)) {
4386 		chv_prepare_pll(crtc, new_crtc_state);
4387 		chv_enable_pll(crtc, new_crtc_state);
4388 	} else {
4389 		vlv_prepare_pll(crtc, new_crtc_state);
4390 		vlv_enable_pll(crtc, new_crtc_state);
4391 	}
4392 
4393 	intel_encoders_pre_enable(state, crtc);
4394 
4395 	i9xx_pfit_enable(new_crtc_state);
4396 
4397 	intel_color_load_luts(new_crtc_state);
4398 	intel_color_commit(new_crtc_state);
4399 	/* update DSPCNTR to configure gamma for pipe bottom color */
4400 	intel_disable_primary_plane(new_crtc_state);
4401 
4402 	dev_priv->display.initial_watermarks(state, crtc);
4403 	intel_enable_pipe(new_crtc_state);
4404 
4405 	intel_crtc_vblank_on(new_crtc_state);
4406 
4407 	intel_encoders_enable(state, crtc);
4408 }
4409 
4410 static void i9xx_set_pll_dividers(const struct intel_crtc_state *crtc_state)
4411 {
4412 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4413 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4414 
4415 	intel_de_write(dev_priv, FP0(crtc->pipe),
4416 		       crtc_state->dpll_hw_state.fp0);
4417 	intel_de_write(dev_priv, FP1(crtc->pipe),
4418 		       crtc_state->dpll_hw_state.fp1);
4419 }
4420 
4421 static void i9xx_crtc_enable(struct intel_atomic_state *state,
4422 			     struct intel_crtc *crtc)
4423 {
4424 	const struct intel_crtc_state *new_crtc_state =
4425 		intel_atomic_get_new_crtc_state(state, crtc);
4426 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4427 	enum pipe pipe = crtc->pipe;
4428 
4429 	if (drm_WARN_ON(&dev_priv->drm, crtc->active))
4430 		return;
4431 
4432 	i9xx_set_pll_dividers(new_crtc_state);
4433 
4434 	if (intel_crtc_has_dp_encoder(new_crtc_state))
4435 		intel_dp_set_m_n(new_crtc_state, M1_N1);
4436 
4437 	intel_set_transcoder_timings(new_crtc_state);
4438 	intel_set_pipe_src_size(new_crtc_state);
4439 
4440 	i9xx_set_pipeconf(new_crtc_state);
4441 
4442 	crtc->active = true;
4443 
4444 	if (!IS_GEN(dev_priv, 2))
4445 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, true);
4446 
4447 	intel_encoders_pre_enable(state, crtc);
4448 
4449 	i9xx_enable_pll(crtc, new_crtc_state);
4450 
4451 	i9xx_pfit_enable(new_crtc_state);
4452 
4453 	intel_color_load_luts(new_crtc_state);
4454 	intel_color_commit(new_crtc_state);
4455 	/* update DSPCNTR to configure gamma for pipe bottom color */
4456 	intel_disable_primary_plane(new_crtc_state);
4457 
4458 	if (dev_priv->display.initial_watermarks)
4459 		dev_priv->display.initial_watermarks(state, crtc);
4460 	else
4461 		intel_update_watermarks(crtc);
4462 	intel_enable_pipe(new_crtc_state);
4463 
4464 	intel_crtc_vblank_on(new_crtc_state);
4465 
4466 	intel_encoders_enable(state, crtc);
4467 
4468 	/* prevents spurious underruns */
4469 	if (IS_GEN(dev_priv, 2))
4470 		intel_wait_for_vblank(dev_priv, pipe);
4471 }
4472 
4473 static void i9xx_pfit_disable(const struct intel_crtc_state *old_crtc_state)
4474 {
4475 	struct intel_crtc *crtc = to_intel_crtc(old_crtc_state->uapi.crtc);
4476 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4477 
4478 	if (!old_crtc_state->gmch_pfit.control)
4479 		return;
4480 
4481 	assert_pipe_disabled(dev_priv, old_crtc_state->cpu_transcoder);
4482 
4483 	drm_dbg_kms(&dev_priv->drm, "disabling pfit, current: 0x%08x\n",
4484 		    intel_de_read(dev_priv, PFIT_CONTROL));
4485 	intel_de_write(dev_priv, PFIT_CONTROL, 0);
4486 }
4487 
4488 static void i9xx_crtc_disable(struct intel_atomic_state *state,
4489 			      struct intel_crtc *crtc)
4490 {
4491 	struct intel_crtc_state *old_crtc_state =
4492 		intel_atomic_get_old_crtc_state(state, crtc);
4493 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4494 	enum pipe pipe = crtc->pipe;
4495 
4496 	/*
4497 	 * On gen2 planes are double buffered but the pipe isn't, so we must
4498 	 * wait for planes to fully turn off before disabling the pipe.
4499 	 */
4500 	if (IS_GEN(dev_priv, 2))
4501 		intel_wait_for_vblank(dev_priv, pipe);
4502 
4503 	intel_encoders_disable(state, crtc);
4504 
4505 	intel_crtc_vblank_off(old_crtc_state);
4506 
4507 	intel_disable_pipe(old_crtc_state);
4508 
4509 	i9xx_pfit_disable(old_crtc_state);
4510 
4511 	intel_encoders_post_disable(state, crtc);
4512 
4513 	if (!intel_crtc_has_type(old_crtc_state, INTEL_OUTPUT_DSI)) {
4514 		if (IS_CHERRYVIEW(dev_priv))
4515 			chv_disable_pll(dev_priv, pipe);
4516 		else if (IS_VALLEYVIEW(dev_priv))
4517 			vlv_disable_pll(dev_priv, pipe);
4518 		else
4519 			i9xx_disable_pll(old_crtc_state);
4520 	}
4521 
4522 	intel_encoders_post_pll_disable(state, crtc);
4523 
4524 	if (!IS_GEN(dev_priv, 2))
4525 		intel_set_cpu_fifo_underrun_reporting(dev_priv, pipe, false);
4526 
4527 	if (!dev_priv->display.initial_watermarks)
4528 		intel_update_watermarks(crtc);
4529 
4530 	/* clock the pipe down to 640x480@60 to potentially save power */
4531 	if (IS_I830(dev_priv))
4532 		i830_enable_pipe(dev_priv, pipe);
4533 }
4534 
4535 static void intel_crtc_disable_noatomic(struct intel_crtc *crtc,
4536 					struct drm_modeset_acquire_ctx *ctx)
4537 {
4538 	struct intel_encoder *encoder;
4539 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4540 	struct intel_bw_state *bw_state =
4541 		to_intel_bw_state(dev_priv->bw_obj.state);
4542 	struct intel_cdclk_state *cdclk_state =
4543 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
4544 	struct intel_dbuf_state *dbuf_state =
4545 		to_intel_dbuf_state(dev_priv->dbuf.obj.state);
4546 	struct intel_crtc_state *crtc_state =
4547 		to_intel_crtc_state(crtc->base.state);
4548 	struct intel_plane *plane;
4549 	struct drm_atomic_state *state;
4550 	struct intel_crtc_state *temp_crtc_state;
4551 	enum pipe pipe = crtc->pipe;
4552 	int ret;
4553 
4554 	if (!crtc_state->hw.active)
4555 		return;
4556 
4557 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
4558 		const struct intel_plane_state *plane_state =
4559 			to_intel_plane_state(plane->base.state);
4560 
4561 		if (plane_state->uapi.visible)
4562 			intel_plane_disable_noatomic(crtc, plane);
4563 	}
4564 
4565 	state = drm_atomic_state_alloc(&dev_priv->drm);
4566 	if (!state) {
4567 		drm_dbg_kms(&dev_priv->drm,
4568 			    "failed to disable [CRTC:%d:%s], out of memory",
4569 			    crtc->base.base.id, crtc->base.name);
4570 		return;
4571 	}
4572 
4573 	state->acquire_ctx = ctx;
4574 
4575 	/* Everything's already locked, -EDEADLK can't happen. */
4576 	temp_crtc_state = intel_atomic_get_crtc_state(state, crtc);
4577 	ret = drm_atomic_add_affected_connectors(state, &crtc->base);
4578 
4579 	drm_WARN_ON(&dev_priv->drm, IS_ERR(temp_crtc_state) || ret);
4580 
4581 	dev_priv->display.crtc_disable(to_intel_atomic_state(state), crtc);
4582 
4583 	drm_atomic_state_put(state);
4584 
4585 	drm_dbg_kms(&dev_priv->drm,
4586 		    "[CRTC:%d:%s] hw state adjusted, was enabled, now disabled\n",
4587 		    crtc->base.base.id, crtc->base.name);
4588 
4589 	crtc->active = false;
4590 	crtc->base.enabled = false;
4591 
4592 	drm_WARN_ON(&dev_priv->drm,
4593 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, NULL) < 0);
4594 	crtc_state->uapi.active = false;
4595 	crtc_state->uapi.connector_mask = 0;
4596 	crtc_state->uapi.encoder_mask = 0;
4597 	intel_crtc_free_hw_state(crtc_state);
4598 	memset(&crtc_state->hw, 0, sizeof(crtc_state->hw));
4599 
4600 	for_each_encoder_on_crtc(&dev_priv->drm, &crtc->base, encoder)
4601 		encoder->base.crtc = NULL;
4602 
4603 	intel_fbc_disable(crtc);
4604 	intel_update_watermarks(crtc);
4605 	intel_disable_shared_dpll(crtc_state);
4606 
4607 	intel_display_power_put_all_in_set(dev_priv, &crtc->enabled_power_domains);
4608 
4609 	dev_priv->active_pipes &= ~BIT(pipe);
4610 	cdclk_state->min_cdclk[pipe] = 0;
4611 	cdclk_state->min_voltage_level[pipe] = 0;
4612 	cdclk_state->active_pipes &= ~BIT(pipe);
4613 
4614 	dbuf_state->active_pipes &= ~BIT(pipe);
4615 
4616 	bw_state->data_rate[pipe] = 0;
4617 	bw_state->num_active_planes[pipe] = 0;
4618 }
4619 
4620 /*
4621  * turn all crtc's off, but do not adjust state
4622  * This has to be paired with a call to intel_modeset_setup_hw_state.
4623  */
4624 int intel_display_suspend(struct drm_device *dev)
4625 {
4626 	struct drm_i915_private *dev_priv = to_i915(dev);
4627 	struct drm_atomic_state *state;
4628 	int ret;
4629 
4630 	state = drm_atomic_helper_suspend(dev);
4631 	ret = PTR_ERR_OR_ZERO(state);
4632 	if (ret)
4633 		drm_err(&dev_priv->drm, "Suspending crtc's failed with %i\n",
4634 			ret);
4635 	else
4636 		dev_priv->modeset_restore_state = state;
4637 	return ret;
4638 }
4639 
4640 void intel_encoder_destroy(struct drm_encoder *encoder)
4641 {
4642 	struct intel_encoder *intel_encoder = to_intel_encoder(encoder);
4643 
4644 	drm_encoder_cleanup(encoder);
4645 	kfree(intel_encoder);
4646 }
4647 
4648 /* Cross check the actual hw state with our own modeset state tracking (and it's
4649  * internal consistency). */
4650 static void intel_connector_verify_state(struct intel_crtc_state *crtc_state,
4651 					 struct drm_connector_state *conn_state)
4652 {
4653 	struct intel_connector *connector = to_intel_connector(conn_state->connector);
4654 	struct drm_i915_private *i915 = to_i915(connector->base.dev);
4655 
4656 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s]\n",
4657 		    connector->base.base.id, connector->base.name);
4658 
4659 	if (connector->get_hw_state(connector)) {
4660 		struct intel_encoder *encoder = intel_attached_encoder(connector);
4661 
4662 		I915_STATE_WARN(!crtc_state,
4663 			 "connector enabled without attached crtc\n");
4664 
4665 		if (!crtc_state)
4666 			return;
4667 
4668 		I915_STATE_WARN(!crtc_state->hw.active,
4669 				"connector is active, but attached crtc isn't\n");
4670 
4671 		if (!encoder || encoder->type == INTEL_OUTPUT_DP_MST)
4672 			return;
4673 
4674 		I915_STATE_WARN(conn_state->best_encoder != &encoder->base,
4675 			"atomic encoder doesn't match attached encoder\n");
4676 
4677 		I915_STATE_WARN(conn_state->crtc != encoder->base.crtc,
4678 			"attached encoder crtc differs from connector crtc\n");
4679 	} else {
4680 		I915_STATE_WARN(crtc_state && crtc_state->hw.active,
4681 				"attached crtc is active, but connector isn't\n");
4682 		I915_STATE_WARN(!crtc_state && conn_state->best_encoder,
4683 			"best encoder set without crtc!\n");
4684 	}
4685 }
4686 
4687 bool hsw_crtc_state_ips_capable(const struct intel_crtc_state *crtc_state)
4688 {
4689 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
4690 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4691 
4692 	/* IPS only exists on ULT machines and is tied to pipe A. */
4693 	if (!hsw_crtc_supports_ips(crtc))
4694 		return false;
4695 
4696 	if (!dev_priv->params.enable_ips)
4697 		return false;
4698 
4699 	if (crtc_state->pipe_bpp > 24)
4700 		return false;
4701 
4702 	/*
4703 	 * We compare against max which means we must take
4704 	 * the increased cdclk requirement into account when
4705 	 * calculating the new cdclk.
4706 	 *
4707 	 * Should measure whether using a lower cdclk w/o IPS
4708 	 */
4709 	if (IS_BROADWELL(dev_priv) &&
4710 	    crtc_state->pixel_rate > dev_priv->max_cdclk_freq * 95 / 100)
4711 		return false;
4712 
4713 	return true;
4714 }
4715 
4716 static int hsw_compute_ips_config(struct intel_crtc_state *crtc_state)
4717 {
4718 	struct drm_i915_private *dev_priv =
4719 		to_i915(crtc_state->uapi.crtc->dev);
4720 	struct intel_atomic_state *state =
4721 		to_intel_atomic_state(crtc_state->uapi.state);
4722 
4723 	crtc_state->ips_enabled = false;
4724 
4725 	if (!hsw_crtc_state_ips_capable(crtc_state))
4726 		return 0;
4727 
4728 	/*
4729 	 * When IPS gets enabled, the pipe CRC changes. Since IPS gets
4730 	 * enabled and disabled dynamically based on package C states,
4731 	 * user space can't make reliable use of the CRCs, so let's just
4732 	 * completely disable it.
4733 	 */
4734 	if (crtc_state->crc_enabled)
4735 		return 0;
4736 
4737 	/* IPS should be fine as long as at least one plane is enabled. */
4738 	if (!(crtc_state->active_planes & ~BIT(PLANE_CURSOR)))
4739 		return 0;
4740 
4741 	if (IS_BROADWELL(dev_priv)) {
4742 		const struct intel_cdclk_state *cdclk_state;
4743 
4744 		cdclk_state = intel_atomic_get_cdclk_state(state);
4745 		if (IS_ERR(cdclk_state))
4746 			return PTR_ERR(cdclk_state);
4747 
4748 		/* pixel rate mustn't exceed 95% of cdclk with IPS on BDW */
4749 		if (crtc_state->pixel_rate > cdclk_state->logical.cdclk * 95 / 100)
4750 			return 0;
4751 	}
4752 
4753 	crtc_state->ips_enabled = true;
4754 
4755 	return 0;
4756 }
4757 
4758 static bool intel_crtc_supports_double_wide(const struct intel_crtc *crtc)
4759 {
4760 	const struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4761 
4762 	/* GDG double wide on either pipe, otherwise pipe A only */
4763 	return INTEL_GEN(dev_priv) < 4 &&
4764 		(crtc->pipe == PIPE_A || IS_I915G(dev_priv));
4765 }
4766 
4767 static u32 ilk_pipe_pixel_rate(const struct intel_crtc_state *crtc_state)
4768 {
4769 	u32 pixel_rate = crtc_state->hw.pipe_mode.crtc_clock;
4770 	unsigned int pipe_w, pipe_h, pfit_w, pfit_h;
4771 
4772 	/*
4773 	 * We only use IF-ID interlacing. If we ever use
4774 	 * PF-ID we'll need to adjust the pixel_rate here.
4775 	 */
4776 
4777 	if (!crtc_state->pch_pfit.enabled)
4778 		return pixel_rate;
4779 
4780 	pipe_w = crtc_state->pipe_src_w;
4781 	pipe_h = crtc_state->pipe_src_h;
4782 
4783 	pfit_w = drm_rect_width(&crtc_state->pch_pfit.dst);
4784 	pfit_h = drm_rect_height(&crtc_state->pch_pfit.dst);
4785 
4786 	if (pipe_w < pfit_w)
4787 		pipe_w = pfit_w;
4788 	if (pipe_h < pfit_h)
4789 		pipe_h = pfit_h;
4790 
4791 	if (drm_WARN_ON(crtc_state->uapi.crtc->dev,
4792 			!pfit_w || !pfit_h))
4793 		return pixel_rate;
4794 
4795 	return div_u64(mul_u32_u32(pixel_rate, pipe_w * pipe_h),
4796 		       pfit_w * pfit_h);
4797 }
4798 
4799 static void intel_mode_from_crtc_timings(struct drm_display_mode *mode,
4800 					 const struct drm_display_mode *timings)
4801 {
4802 	mode->hdisplay = timings->crtc_hdisplay;
4803 	mode->htotal = timings->crtc_htotal;
4804 	mode->hsync_start = timings->crtc_hsync_start;
4805 	mode->hsync_end = timings->crtc_hsync_end;
4806 
4807 	mode->vdisplay = timings->crtc_vdisplay;
4808 	mode->vtotal = timings->crtc_vtotal;
4809 	mode->vsync_start = timings->crtc_vsync_start;
4810 	mode->vsync_end = timings->crtc_vsync_end;
4811 
4812 	mode->flags = timings->flags;
4813 	mode->type = DRM_MODE_TYPE_DRIVER;
4814 
4815 	mode->clock = timings->crtc_clock;
4816 
4817 	drm_mode_set_name(mode);
4818 }
4819 
4820 static void intel_crtc_compute_pixel_rate(struct intel_crtc_state *crtc_state)
4821 {
4822 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
4823 
4824 	if (HAS_GMCH(dev_priv))
4825 		/* FIXME calculate proper pipe pixel rate for GMCH pfit */
4826 		crtc_state->pixel_rate =
4827 			crtc_state->hw.pipe_mode.crtc_clock;
4828 	else
4829 		crtc_state->pixel_rate =
4830 			ilk_pipe_pixel_rate(crtc_state);
4831 }
4832 
4833 static void intel_crtc_readout_derived_state(struct intel_crtc_state *crtc_state)
4834 {
4835 	struct drm_display_mode *mode = &crtc_state->hw.mode;
4836 	struct drm_display_mode *pipe_mode = &crtc_state->hw.pipe_mode;
4837 	struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
4838 
4839 	drm_mode_copy(pipe_mode, adjusted_mode);
4840 
4841 	if (crtc_state->bigjoiner) {
4842 		/*
4843 		 * transcoder is programmed to the full mode,
4844 		 * but pipe timings are half of the transcoder mode
4845 		 */
4846 		pipe_mode->crtc_hdisplay /= 2;
4847 		pipe_mode->crtc_hblank_start /= 2;
4848 		pipe_mode->crtc_hblank_end /= 2;
4849 		pipe_mode->crtc_hsync_start /= 2;
4850 		pipe_mode->crtc_hsync_end /= 2;
4851 		pipe_mode->crtc_htotal /= 2;
4852 		pipe_mode->crtc_clock /= 2;
4853 	}
4854 
4855 	if (crtc_state->splitter.enable) {
4856 		int n = crtc_state->splitter.link_count;
4857 		int overlap = crtc_state->splitter.pixel_overlap;
4858 
4859 		/*
4860 		 * eDP MSO uses segment timings from EDID for transcoder
4861 		 * timings, but full mode for everything else.
4862 		 *
4863 		 * h_full = (h_segment - pixel_overlap) * link_count
4864 		 */
4865 		pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
4866 		pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
4867 		pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
4868 		pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
4869 		pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
4870 		pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
4871 		pipe_mode->crtc_clock *= n;
4872 
4873 		intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4874 		intel_mode_from_crtc_timings(adjusted_mode, pipe_mode);
4875 	} else {
4876 		intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4877 		intel_mode_from_crtc_timings(adjusted_mode, adjusted_mode);
4878 	}
4879 
4880 	intel_crtc_compute_pixel_rate(crtc_state);
4881 
4882 	drm_mode_copy(mode, adjusted_mode);
4883 	mode->hdisplay = crtc_state->pipe_src_w << crtc_state->bigjoiner;
4884 	mode->vdisplay = crtc_state->pipe_src_h;
4885 }
4886 
4887 static void intel_encoder_get_config(struct intel_encoder *encoder,
4888 				     struct intel_crtc_state *crtc_state)
4889 {
4890 	encoder->get_config(encoder, crtc_state);
4891 
4892 	intel_crtc_readout_derived_state(crtc_state);
4893 }
4894 
4895 static int intel_crtc_compute_config(struct intel_crtc *crtc,
4896 				     struct intel_crtc_state *pipe_config)
4897 {
4898 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
4899 	struct drm_display_mode *pipe_mode = &pipe_config->hw.pipe_mode;
4900 	int clock_limit = dev_priv->max_dotclk_freq;
4901 
4902 	drm_mode_copy(pipe_mode, &pipe_config->hw.adjusted_mode);
4903 
4904 	/* Adjust pipe_mode for bigjoiner, with half the horizontal mode */
4905 	if (pipe_config->bigjoiner) {
4906 		pipe_mode->crtc_clock /= 2;
4907 		pipe_mode->crtc_hdisplay /= 2;
4908 		pipe_mode->crtc_hblank_start /= 2;
4909 		pipe_mode->crtc_hblank_end /= 2;
4910 		pipe_mode->crtc_hsync_start /= 2;
4911 		pipe_mode->crtc_hsync_end /= 2;
4912 		pipe_mode->crtc_htotal /= 2;
4913 		pipe_config->pipe_src_w /= 2;
4914 	}
4915 
4916 	if (pipe_config->splitter.enable) {
4917 		int n = pipe_config->splitter.link_count;
4918 		int overlap = pipe_config->splitter.pixel_overlap;
4919 
4920 		pipe_mode->crtc_hdisplay = (pipe_mode->crtc_hdisplay - overlap) * n;
4921 		pipe_mode->crtc_hblank_start = (pipe_mode->crtc_hblank_start - overlap) * n;
4922 		pipe_mode->crtc_hblank_end = (pipe_mode->crtc_hblank_end - overlap) * n;
4923 		pipe_mode->crtc_hsync_start = (pipe_mode->crtc_hsync_start - overlap) * n;
4924 		pipe_mode->crtc_hsync_end = (pipe_mode->crtc_hsync_end - overlap) * n;
4925 		pipe_mode->crtc_htotal = (pipe_mode->crtc_htotal - overlap) * n;
4926 		pipe_mode->crtc_clock *= n;
4927 	}
4928 
4929 	intel_mode_from_crtc_timings(pipe_mode, pipe_mode);
4930 
4931 	if (INTEL_GEN(dev_priv) < 4) {
4932 		clock_limit = dev_priv->max_cdclk_freq * 9 / 10;
4933 
4934 		/*
4935 		 * Enable double wide mode when the dot clock
4936 		 * is > 90% of the (display) core speed.
4937 		 */
4938 		if (intel_crtc_supports_double_wide(crtc) &&
4939 		    pipe_mode->crtc_clock > clock_limit) {
4940 			clock_limit = dev_priv->max_dotclk_freq;
4941 			pipe_config->double_wide = true;
4942 		}
4943 	}
4944 
4945 	if (pipe_mode->crtc_clock > clock_limit) {
4946 		drm_dbg_kms(&dev_priv->drm,
4947 			    "requested pixel clock (%d kHz) too high (max: %d kHz, double wide: %s)\n",
4948 			    pipe_mode->crtc_clock, clock_limit,
4949 			    yesno(pipe_config->double_wide));
4950 		return -EINVAL;
4951 	}
4952 
4953 	/*
4954 	 * Pipe horizontal size must be even in:
4955 	 * - DVO ganged mode
4956 	 * - LVDS dual channel mode
4957 	 * - Double wide pipe
4958 	 */
4959 	if (pipe_config->pipe_src_w & 1) {
4960 		if (pipe_config->double_wide) {
4961 			drm_dbg_kms(&dev_priv->drm,
4962 				    "Odd pipe source width not supported with double wide pipe\n");
4963 			return -EINVAL;
4964 		}
4965 
4966 		if (intel_crtc_has_type(pipe_config, INTEL_OUTPUT_LVDS) &&
4967 		    intel_is_dual_link_lvds(dev_priv)) {
4968 			drm_dbg_kms(&dev_priv->drm,
4969 				    "Odd pipe source width not supported with dual link LVDS\n");
4970 			return -EINVAL;
4971 		}
4972 	}
4973 
4974 	/* Cantiga+ cannot handle modes with a hsync front porch of 0.
4975 	 * WaPruneModeWithIncorrectHsyncOffset:ctg,elk,ilk,snb,ivb,vlv,hsw.
4976 	 */
4977 	if ((INTEL_GEN(dev_priv) > 4 || IS_G4X(dev_priv)) &&
4978 	    pipe_mode->crtc_hsync_start == pipe_mode->crtc_hdisplay)
4979 		return -EINVAL;
4980 
4981 	intel_crtc_compute_pixel_rate(pipe_config);
4982 
4983 	if (pipe_config->has_pch_encoder)
4984 		return ilk_fdi_compute_config(crtc, pipe_config);
4985 
4986 	return 0;
4987 }
4988 
4989 static void
4990 intel_reduce_m_n_ratio(u32 *num, u32 *den)
4991 {
4992 	while (*num > DATA_LINK_M_N_MASK ||
4993 	       *den > DATA_LINK_M_N_MASK) {
4994 		*num >>= 1;
4995 		*den >>= 1;
4996 	}
4997 }
4998 
4999 static void compute_m_n(unsigned int m, unsigned int n,
5000 			u32 *ret_m, u32 *ret_n,
5001 			bool constant_n)
5002 {
5003 	/*
5004 	 * Several DP dongles in particular seem to be fussy about
5005 	 * too large link M/N values. Give N value as 0x8000 that
5006 	 * should be acceptable by specific devices. 0x8000 is the
5007 	 * specified fixed N value for asynchronous clock mode,
5008 	 * which the devices expect also in synchronous clock mode.
5009 	 */
5010 	if (constant_n)
5011 		*ret_n = DP_LINK_CONSTANT_N_VALUE;
5012 	else
5013 		*ret_n = min_t(unsigned int, roundup_pow_of_two(n), DATA_LINK_N_MAX);
5014 
5015 	*ret_m = div_u64(mul_u32_u32(m, *ret_n), n);
5016 	intel_reduce_m_n_ratio(ret_m, ret_n);
5017 }
5018 
5019 void
5020 intel_link_compute_m_n(u16 bits_per_pixel, int nlanes,
5021 		       int pixel_clock, int link_clock,
5022 		       struct intel_link_m_n *m_n,
5023 		       bool constant_n, bool fec_enable)
5024 {
5025 	u32 data_clock = bits_per_pixel * pixel_clock;
5026 
5027 	if (fec_enable)
5028 		data_clock = intel_dp_mode_to_fec_clock(data_clock);
5029 
5030 	m_n->tu = 64;
5031 	compute_m_n(data_clock,
5032 		    link_clock * nlanes * 8,
5033 		    &m_n->gmch_m, &m_n->gmch_n,
5034 		    constant_n);
5035 
5036 	compute_m_n(pixel_clock, link_clock,
5037 		    &m_n->link_m, &m_n->link_n,
5038 		    constant_n);
5039 }
5040 
5041 static void intel_panel_sanitize_ssc(struct drm_i915_private *dev_priv)
5042 {
5043 	/*
5044 	 * There may be no VBT; and if the BIOS enabled SSC we can
5045 	 * just keep using it to avoid unnecessary flicker.  Whereas if the
5046 	 * BIOS isn't using it, don't assume it will work even if the VBT
5047 	 * indicates as much.
5048 	 */
5049 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv)) {
5050 		bool bios_lvds_use_ssc = intel_de_read(dev_priv,
5051 						       PCH_DREF_CONTROL) &
5052 			DREF_SSC1_ENABLE;
5053 
5054 		if (dev_priv->vbt.lvds_use_ssc != bios_lvds_use_ssc) {
5055 			drm_dbg_kms(&dev_priv->drm,
5056 				    "SSC %s by BIOS, overriding VBT which says %s\n",
5057 				    enableddisabled(bios_lvds_use_ssc),
5058 				    enableddisabled(dev_priv->vbt.lvds_use_ssc));
5059 			dev_priv->vbt.lvds_use_ssc = bios_lvds_use_ssc;
5060 		}
5061 	}
5062 }
5063 
5064 static void intel_pch_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
5065 					 const struct intel_link_m_n *m_n)
5066 {
5067 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5068 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5069 	enum pipe pipe = crtc->pipe;
5070 
5071 	intel_de_write(dev_priv, PCH_TRANS_DATA_M1(pipe),
5072 		       TU_SIZE(m_n->tu) | m_n->gmch_m);
5073 	intel_de_write(dev_priv, PCH_TRANS_DATA_N1(pipe), m_n->gmch_n);
5074 	intel_de_write(dev_priv, PCH_TRANS_LINK_M1(pipe), m_n->link_m);
5075 	intel_de_write(dev_priv, PCH_TRANS_LINK_N1(pipe), m_n->link_n);
5076 }
5077 
5078 static bool transcoder_has_m2_n2(struct drm_i915_private *dev_priv,
5079 				 enum transcoder transcoder)
5080 {
5081 	if (IS_HASWELL(dev_priv))
5082 		return transcoder == TRANSCODER_EDP;
5083 
5084 	/*
5085 	 * Strictly speaking some registers are available before
5086 	 * gen7, but we only support DRRS on gen7+
5087 	 */
5088 	return IS_GEN(dev_priv, 7) || IS_CHERRYVIEW(dev_priv);
5089 }
5090 
5091 static void intel_cpu_transcoder_set_m_n(const struct intel_crtc_state *crtc_state,
5092 					 const struct intel_link_m_n *m_n,
5093 					 const struct intel_link_m_n *m2_n2)
5094 {
5095 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5096 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5097 	enum pipe pipe = crtc->pipe;
5098 	enum transcoder transcoder = crtc_state->cpu_transcoder;
5099 
5100 	if (INTEL_GEN(dev_priv) >= 5) {
5101 		intel_de_write(dev_priv, PIPE_DATA_M1(transcoder),
5102 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
5103 		intel_de_write(dev_priv, PIPE_DATA_N1(transcoder),
5104 			       m_n->gmch_n);
5105 		intel_de_write(dev_priv, PIPE_LINK_M1(transcoder),
5106 			       m_n->link_m);
5107 		intel_de_write(dev_priv, PIPE_LINK_N1(transcoder),
5108 			       m_n->link_n);
5109 		/*
5110 		 *  M2_N2 registers are set only if DRRS is supported
5111 		 * (to make sure the registers are not unnecessarily accessed).
5112 		 */
5113 		if (m2_n2 && crtc_state->has_drrs &&
5114 		    transcoder_has_m2_n2(dev_priv, transcoder)) {
5115 			intel_de_write(dev_priv, PIPE_DATA_M2(transcoder),
5116 				       TU_SIZE(m2_n2->tu) | m2_n2->gmch_m);
5117 			intel_de_write(dev_priv, PIPE_DATA_N2(transcoder),
5118 				       m2_n2->gmch_n);
5119 			intel_de_write(dev_priv, PIPE_LINK_M2(transcoder),
5120 				       m2_n2->link_m);
5121 			intel_de_write(dev_priv, PIPE_LINK_N2(transcoder),
5122 				       m2_n2->link_n);
5123 		}
5124 	} else {
5125 		intel_de_write(dev_priv, PIPE_DATA_M_G4X(pipe),
5126 			       TU_SIZE(m_n->tu) | m_n->gmch_m);
5127 		intel_de_write(dev_priv, PIPE_DATA_N_G4X(pipe), m_n->gmch_n);
5128 		intel_de_write(dev_priv, PIPE_LINK_M_G4X(pipe), m_n->link_m);
5129 		intel_de_write(dev_priv, PIPE_LINK_N_G4X(pipe), m_n->link_n);
5130 	}
5131 }
5132 
5133 void intel_dp_set_m_n(const struct intel_crtc_state *crtc_state, enum link_m_n_set m_n)
5134 {
5135 	const struct intel_link_m_n *dp_m_n, *dp_m2_n2 = NULL;
5136 	struct drm_i915_private *i915 = to_i915(crtc_state->uapi.crtc->dev);
5137 
5138 	if (m_n == M1_N1) {
5139 		dp_m_n = &crtc_state->dp_m_n;
5140 		dp_m2_n2 = &crtc_state->dp_m2_n2;
5141 	} else if (m_n == M2_N2) {
5142 
5143 		/*
5144 		 * M2_N2 registers are not supported. Hence m2_n2 divider value
5145 		 * needs to be programmed into M1_N1.
5146 		 */
5147 		dp_m_n = &crtc_state->dp_m2_n2;
5148 	} else {
5149 		drm_err(&i915->drm, "Unsupported divider value\n");
5150 		return;
5151 	}
5152 
5153 	if (crtc_state->has_pch_encoder)
5154 		intel_pch_transcoder_set_m_n(crtc_state, &crtc_state->dp_m_n);
5155 	else
5156 		intel_cpu_transcoder_set_m_n(crtc_state, dp_m_n, dp_m2_n2);
5157 }
5158 
5159 static void intel_set_transcoder_timings(const struct intel_crtc_state *crtc_state)
5160 {
5161 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5162 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5163 	enum pipe pipe = crtc->pipe;
5164 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5165 	const struct drm_display_mode *adjusted_mode = &crtc_state->hw.adjusted_mode;
5166 	u32 crtc_vtotal, crtc_vblank_end;
5167 	int vsyncshift = 0;
5168 
5169 	/* We need to be careful not to changed the adjusted mode, for otherwise
5170 	 * the hw state checker will get angry at the mismatch. */
5171 	crtc_vtotal = adjusted_mode->crtc_vtotal;
5172 	crtc_vblank_end = adjusted_mode->crtc_vblank_end;
5173 
5174 	if (adjusted_mode->flags & DRM_MODE_FLAG_INTERLACE) {
5175 		/* the chip adds 2 halflines automatically */
5176 		crtc_vtotal -= 1;
5177 		crtc_vblank_end -= 1;
5178 
5179 		if (intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
5180 			vsyncshift = (adjusted_mode->crtc_htotal - 1) / 2;
5181 		else
5182 			vsyncshift = adjusted_mode->crtc_hsync_start -
5183 				adjusted_mode->crtc_htotal / 2;
5184 		if (vsyncshift < 0)
5185 			vsyncshift += adjusted_mode->crtc_htotal;
5186 	}
5187 
5188 	if (INTEL_GEN(dev_priv) > 3)
5189 		intel_de_write(dev_priv, VSYNCSHIFT(cpu_transcoder),
5190 		               vsyncshift);
5191 
5192 	intel_de_write(dev_priv, HTOTAL(cpu_transcoder),
5193 		       (adjusted_mode->crtc_hdisplay - 1) | ((adjusted_mode->crtc_htotal - 1) << 16));
5194 	intel_de_write(dev_priv, HBLANK(cpu_transcoder),
5195 		       (adjusted_mode->crtc_hblank_start - 1) | ((adjusted_mode->crtc_hblank_end - 1) << 16));
5196 	intel_de_write(dev_priv, HSYNC(cpu_transcoder),
5197 		       (adjusted_mode->crtc_hsync_start - 1) | ((adjusted_mode->crtc_hsync_end - 1) << 16));
5198 
5199 	intel_de_write(dev_priv, VTOTAL(cpu_transcoder),
5200 		       (adjusted_mode->crtc_vdisplay - 1) | ((crtc_vtotal - 1) << 16));
5201 	intel_de_write(dev_priv, VBLANK(cpu_transcoder),
5202 		       (adjusted_mode->crtc_vblank_start - 1) | ((crtc_vblank_end - 1) << 16));
5203 	intel_de_write(dev_priv, VSYNC(cpu_transcoder),
5204 		       (adjusted_mode->crtc_vsync_start - 1) | ((adjusted_mode->crtc_vsync_end - 1) << 16));
5205 
5206 	/* Workaround: when the EDP input selection is B, the VTOTAL_B must be
5207 	 * programmed with the VTOTAL_EDP value. Same for VTOTAL_C. This is
5208 	 * documented on the DDI_FUNC_CTL register description, EDP Input Select
5209 	 * bits. */
5210 	if (IS_HASWELL(dev_priv) && cpu_transcoder == TRANSCODER_EDP &&
5211 	    (pipe == PIPE_B || pipe == PIPE_C))
5212 		intel_de_write(dev_priv, VTOTAL(pipe),
5213 		               intel_de_read(dev_priv, VTOTAL(cpu_transcoder)));
5214 
5215 }
5216 
5217 static void intel_set_pipe_src_size(const struct intel_crtc_state *crtc_state)
5218 {
5219 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5220 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5221 	enum pipe pipe = crtc->pipe;
5222 
5223 	/* pipesrc controls the size that is scaled from, which should
5224 	 * always be the user's requested size.
5225 	 */
5226 	intel_de_write(dev_priv, PIPESRC(pipe),
5227 		       ((crtc_state->pipe_src_w - 1) << 16) | (crtc_state->pipe_src_h - 1));
5228 }
5229 
5230 static bool intel_pipe_is_interlaced(const struct intel_crtc_state *crtc_state)
5231 {
5232 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
5233 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
5234 
5235 	if (IS_GEN(dev_priv, 2))
5236 		return false;
5237 
5238 	if (INTEL_GEN(dev_priv) >= 9 ||
5239 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
5240 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK_HSW;
5241 	else
5242 		return intel_de_read(dev_priv, PIPECONF(cpu_transcoder)) & PIPECONF_INTERLACE_MASK;
5243 }
5244 
5245 static void intel_get_transcoder_timings(struct intel_crtc *crtc,
5246 					 struct intel_crtc_state *pipe_config)
5247 {
5248 	struct drm_device *dev = crtc->base.dev;
5249 	struct drm_i915_private *dev_priv = to_i915(dev);
5250 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
5251 	u32 tmp;
5252 
5253 	tmp = intel_de_read(dev_priv, HTOTAL(cpu_transcoder));
5254 	pipe_config->hw.adjusted_mode.crtc_hdisplay = (tmp & 0xffff) + 1;
5255 	pipe_config->hw.adjusted_mode.crtc_htotal = ((tmp >> 16) & 0xffff) + 1;
5256 
5257 	if (!transcoder_is_dsi(cpu_transcoder)) {
5258 		tmp = intel_de_read(dev_priv, HBLANK(cpu_transcoder));
5259 		pipe_config->hw.adjusted_mode.crtc_hblank_start =
5260 							(tmp & 0xffff) + 1;
5261 		pipe_config->hw.adjusted_mode.crtc_hblank_end =
5262 						((tmp >> 16) & 0xffff) + 1;
5263 	}
5264 	tmp = intel_de_read(dev_priv, HSYNC(cpu_transcoder));
5265 	pipe_config->hw.adjusted_mode.crtc_hsync_start = (tmp & 0xffff) + 1;
5266 	pipe_config->hw.adjusted_mode.crtc_hsync_end = ((tmp >> 16) & 0xffff) + 1;
5267 
5268 	tmp = intel_de_read(dev_priv, VTOTAL(cpu_transcoder));
5269 	pipe_config->hw.adjusted_mode.crtc_vdisplay = (tmp & 0xffff) + 1;
5270 	pipe_config->hw.adjusted_mode.crtc_vtotal = ((tmp >> 16) & 0xffff) + 1;
5271 
5272 	if (!transcoder_is_dsi(cpu_transcoder)) {
5273 		tmp = intel_de_read(dev_priv, VBLANK(cpu_transcoder));
5274 		pipe_config->hw.adjusted_mode.crtc_vblank_start =
5275 							(tmp & 0xffff) + 1;
5276 		pipe_config->hw.adjusted_mode.crtc_vblank_end =
5277 						((tmp >> 16) & 0xffff) + 1;
5278 	}
5279 	tmp = intel_de_read(dev_priv, VSYNC(cpu_transcoder));
5280 	pipe_config->hw.adjusted_mode.crtc_vsync_start = (tmp & 0xffff) + 1;
5281 	pipe_config->hw.adjusted_mode.crtc_vsync_end = ((tmp >> 16) & 0xffff) + 1;
5282 
5283 	if (intel_pipe_is_interlaced(pipe_config)) {
5284 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_INTERLACE;
5285 		pipe_config->hw.adjusted_mode.crtc_vtotal += 1;
5286 		pipe_config->hw.adjusted_mode.crtc_vblank_end += 1;
5287 	}
5288 }
5289 
5290 static void intel_get_pipe_src_size(struct intel_crtc *crtc,
5291 				    struct intel_crtc_state *pipe_config)
5292 {
5293 	struct drm_device *dev = crtc->base.dev;
5294 	struct drm_i915_private *dev_priv = to_i915(dev);
5295 	u32 tmp;
5296 
5297 	tmp = intel_de_read(dev_priv, PIPESRC(crtc->pipe));
5298 	pipe_config->pipe_src_h = (tmp & 0xffff) + 1;
5299 	pipe_config->pipe_src_w = ((tmp >> 16) & 0xffff) + 1;
5300 }
5301 
5302 static void i9xx_set_pipeconf(const struct intel_crtc_state *crtc_state)
5303 {
5304 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5305 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5306 	u32 pipeconf;
5307 
5308 	pipeconf = 0;
5309 
5310 	/* we keep both pipes enabled on 830 */
5311 	if (IS_I830(dev_priv))
5312 		pipeconf |= intel_de_read(dev_priv, PIPECONF(crtc->pipe)) & PIPECONF_ENABLE;
5313 
5314 	if (crtc_state->double_wide)
5315 		pipeconf |= PIPECONF_DOUBLE_WIDE;
5316 
5317 	/* only g4x and later have fancy bpc/dither controls */
5318 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
5319 	    IS_CHERRYVIEW(dev_priv)) {
5320 		/* Bspec claims that we can't use dithering for 30bpp pipes. */
5321 		if (crtc_state->dither && crtc_state->pipe_bpp != 30)
5322 			pipeconf |= PIPECONF_DITHER_EN |
5323 				    PIPECONF_DITHER_TYPE_SP;
5324 
5325 		switch (crtc_state->pipe_bpp) {
5326 		case 18:
5327 			pipeconf |= PIPECONF_6BPC;
5328 			break;
5329 		case 24:
5330 			pipeconf |= PIPECONF_8BPC;
5331 			break;
5332 		case 30:
5333 			pipeconf |= PIPECONF_10BPC;
5334 			break;
5335 		default:
5336 			/* Case prevented by intel_choose_pipe_bpp_dither. */
5337 			BUG();
5338 		}
5339 	}
5340 
5341 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE) {
5342 		if (INTEL_GEN(dev_priv) < 4 ||
5343 		    intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
5344 			pipeconf |= PIPECONF_INTERLACE_W_FIELD_INDICATION;
5345 		else
5346 			pipeconf |= PIPECONF_INTERLACE_W_SYNC_SHIFT;
5347 	} else {
5348 		pipeconf |= PIPECONF_PROGRESSIVE;
5349 	}
5350 
5351 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
5352 	     crtc_state->limited_color_range)
5353 		pipeconf |= PIPECONF_COLOR_RANGE_SELECT;
5354 
5355 	pipeconf |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
5356 
5357 	pipeconf |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
5358 
5359 	intel_de_write(dev_priv, PIPECONF(crtc->pipe), pipeconf);
5360 	intel_de_posting_read(dev_priv, PIPECONF(crtc->pipe));
5361 }
5362 
5363 static bool i9xx_has_pfit(struct drm_i915_private *dev_priv)
5364 {
5365 	if (IS_I830(dev_priv))
5366 		return false;
5367 
5368 	return INTEL_GEN(dev_priv) >= 4 ||
5369 		IS_PINEVIEW(dev_priv) || IS_MOBILE(dev_priv);
5370 }
5371 
5372 static void i9xx_get_pfit_config(struct intel_crtc_state *crtc_state)
5373 {
5374 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5375 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5376 	u32 tmp;
5377 
5378 	if (!i9xx_has_pfit(dev_priv))
5379 		return;
5380 
5381 	tmp = intel_de_read(dev_priv, PFIT_CONTROL);
5382 	if (!(tmp & PFIT_ENABLE))
5383 		return;
5384 
5385 	/* Check whether the pfit is attached to our pipe. */
5386 	if (INTEL_GEN(dev_priv) < 4) {
5387 		if (crtc->pipe != PIPE_B)
5388 			return;
5389 	} else {
5390 		if ((tmp & PFIT_PIPE_MASK) != (crtc->pipe << PFIT_PIPE_SHIFT))
5391 			return;
5392 	}
5393 
5394 	crtc_state->gmch_pfit.control = tmp;
5395 	crtc_state->gmch_pfit.pgm_ratios =
5396 		intel_de_read(dev_priv, PFIT_PGM_RATIOS);
5397 }
5398 
5399 static void vlv_crtc_clock_get(struct intel_crtc *crtc,
5400 			       struct intel_crtc_state *pipe_config)
5401 {
5402 	struct drm_device *dev = crtc->base.dev;
5403 	struct drm_i915_private *dev_priv = to_i915(dev);
5404 	enum pipe pipe = crtc->pipe;
5405 	struct dpll clock;
5406 	u32 mdiv;
5407 	int refclk = 100000;
5408 
5409 	/* In case of DSI, DPLL will not be used */
5410 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
5411 		return;
5412 
5413 	vlv_dpio_get(dev_priv);
5414 	mdiv = vlv_dpio_read(dev_priv, pipe, VLV_PLL_DW3(pipe));
5415 	vlv_dpio_put(dev_priv);
5416 
5417 	clock.m1 = (mdiv >> DPIO_M1DIV_SHIFT) & 7;
5418 	clock.m2 = mdiv & DPIO_M2DIV_MASK;
5419 	clock.n = (mdiv >> DPIO_N_SHIFT) & 0xf;
5420 	clock.p1 = (mdiv >> DPIO_P1_SHIFT) & 7;
5421 	clock.p2 = (mdiv >> DPIO_P2_SHIFT) & 0x1f;
5422 
5423 	pipe_config->port_clock = vlv_calc_dpll_params(refclk, &clock);
5424 }
5425 
5426 static void chv_crtc_clock_get(struct intel_crtc *crtc,
5427 			       struct intel_crtc_state *pipe_config)
5428 {
5429 	struct drm_device *dev = crtc->base.dev;
5430 	struct drm_i915_private *dev_priv = to_i915(dev);
5431 	enum pipe pipe = crtc->pipe;
5432 	enum dpio_channel port = vlv_pipe_to_channel(pipe);
5433 	struct dpll clock;
5434 	u32 cmn_dw13, pll_dw0, pll_dw1, pll_dw2, pll_dw3;
5435 	int refclk = 100000;
5436 
5437 	/* In case of DSI, DPLL will not be used */
5438 	if ((pipe_config->dpll_hw_state.dpll & DPLL_VCO_ENABLE) == 0)
5439 		return;
5440 
5441 	vlv_dpio_get(dev_priv);
5442 	cmn_dw13 = vlv_dpio_read(dev_priv, pipe, CHV_CMN_DW13(port));
5443 	pll_dw0 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW0(port));
5444 	pll_dw1 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW1(port));
5445 	pll_dw2 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW2(port));
5446 	pll_dw3 = vlv_dpio_read(dev_priv, pipe, CHV_PLL_DW3(port));
5447 	vlv_dpio_put(dev_priv);
5448 
5449 	clock.m1 = (pll_dw1 & 0x7) == DPIO_CHV_M1_DIV_BY_2 ? 2 : 0;
5450 	clock.m2 = (pll_dw0 & 0xff) << 22;
5451 	if (pll_dw3 & DPIO_CHV_FRAC_DIV_EN)
5452 		clock.m2 |= pll_dw2 & 0x3fffff;
5453 	clock.n = (pll_dw1 >> DPIO_CHV_N_DIV_SHIFT) & 0xf;
5454 	clock.p1 = (cmn_dw13 >> DPIO_CHV_P1_DIV_SHIFT) & 0x7;
5455 	clock.p2 = (cmn_dw13 >> DPIO_CHV_P2_DIV_SHIFT) & 0x1f;
5456 
5457 	pipe_config->port_clock = chv_calc_dpll_params(refclk, &clock);
5458 }
5459 
5460 static enum intel_output_format
5461 bdw_get_pipemisc_output_format(struct intel_crtc *crtc)
5462 {
5463 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5464 	u32 tmp;
5465 
5466 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
5467 
5468 	if (tmp & PIPEMISC_YUV420_ENABLE) {
5469 		/* We support 4:2:0 in full blend mode only */
5470 		drm_WARN_ON(&dev_priv->drm,
5471 			    (tmp & PIPEMISC_YUV420_MODE_FULL_BLEND) == 0);
5472 
5473 		return INTEL_OUTPUT_FORMAT_YCBCR420;
5474 	} else if (tmp & PIPEMISC_OUTPUT_COLORSPACE_YUV) {
5475 		return INTEL_OUTPUT_FORMAT_YCBCR444;
5476 	} else {
5477 		return INTEL_OUTPUT_FORMAT_RGB;
5478 	}
5479 }
5480 
5481 static void i9xx_get_pipe_color_config(struct intel_crtc_state *crtc_state)
5482 {
5483 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
5484 	struct intel_plane *plane = to_intel_plane(crtc->base.primary);
5485 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5486 	enum i9xx_plane_id i9xx_plane = plane->i9xx_plane;
5487 	u32 tmp;
5488 
5489 	tmp = intel_de_read(dev_priv, DSPCNTR(i9xx_plane));
5490 
5491 	if (tmp & DISPPLANE_GAMMA_ENABLE)
5492 		crtc_state->gamma_enable = true;
5493 
5494 	if (!HAS_GMCH(dev_priv) &&
5495 	    tmp & DISPPLANE_PIPE_CSC_ENABLE)
5496 		crtc_state->csc_enable = true;
5497 }
5498 
5499 static bool i9xx_get_pipe_config(struct intel_crtc *crtc,
5500 				 struct intel_crtc_state *pipe_config)
5501 {
5502 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
5503 	enum intel_display_power_domain power_domain;
5504 	intel_wakeref_t wakeref;
5505 	u32 tmp;
5506 	bool ret;
5507 
5508 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
5509 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
5510 	if (!wakeref)
5511 		return false;
5512 
5513 	pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
5514 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
5515 	pipe_config->shared_dpll = NULL;
5516 
5517 	ret = false;
5518 
5519 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
5520 	if (!(tmp & PIPECONF_ENABLE))
5521 		goto out;
5522 
5523 	if (IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
5524 	    IS_CHERRYVIEW(dev_priv)) {
5525 		switch (tmp & PIPECONF_BPC_MASK) {
5526 		case PIPECONF_6BPC:
5527 			pipe_config->pipe_bpp = 18;
5528 			break;
5529 		case PIPECONF_8BPC:
5530 			pipe_config->pipe_bpp = 24;
5531 			break;
5532 		case PIPECONF_10BPC:
5533 			pipe_config->pipe_bpp = 30;
5534 			break;
5535 		default:
5536 			break;
5537 		}
5538 	}
5539 
5540 	if ((IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) &&
5541 	    (tmp & PIPECONF_COLOR_RANGE_SELECT))
5542 		pipe_config->limited_color_range = true;
5543 
5544 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_I9XX) >>
5545 		PIPECONF_GAMMA_MODE_SHIFT;
5546 
5547 	if (IS_CHERRYVIEW(dev_priv))
5548 		pipe_config->cgm_mode = intel_de_read(dev_priv,
5549 						      CGM_PIPE_MODE(crtc->pipe));
5550 
5551 	i9xx_get_pipe_color_config(pipe_config);
5552 	intel_color_get_config(pipe_config);
5553 
5554 	if (INTEL_GEN(dev_priv) < 4)
5555 		pipe_config->double_wide = tmp & PIPECONF_DOUBLE_WIDE;
5556 
5557 	intel_get_transcoder_timings(crtc, pipe_config);
5558 	intel_get_pipe_src_size(crtc, pipe_config);
5559 
5560 	i9xx_get_pfit_config(pipe_config);
5561 
5562 	if (INTEL_GEN(dev_priv) >= 4) {
5563 		/* No way to read it out on pipes B and C */
5564 		if (IS_CHERRYVIEW(dev_priv) && crtc->pipe != PIPE_A)
5565 			tmp = dev_priv->chv_dpll_md[crtc->pipe];
5566 		else
5567 			tmp = intel_de_read(dev_priv, DPLL_MD(crtc->pipe));
5568 		pipe_config->pixel_multiplier =
5569 			((tmp & DPLL_MD_UDI_MULTIPLIER_MASK)
5570 			 >> DPLL_MD_UDI_MULTIPLIER_SHIFT) + 1;
5571 		pipe_config->dpll_hw_state.dpll_md = tmp;
5572 	} else if (IS_I945G(dev_priv) || IS_I945GM(dev_priv) ||
5573 		   IS_G33(dev_priv) || IS_PINEVIEW(dev_priv)) {
5574 		tmp = intel_de_read(dev_priv, DPLL(crtc->pipe));
5575 		pipe_config->pixel_multiplier =
5576 			((tmp & SDVO_MULTIPLIER_MASK)
5577 			 >> SDVO_MULTIPLIER_SHIFT_HIRES) + 1;
5578 	} else {
5579 		/* Note that on i915G/GM the pixel multiplier is in the sdvo
5580 		 * port and will be fixed up in the encoder->get_config
5581 		 * function. */
5582 		pipe_config->pixel_multiplier = 1;
5583 	}
5584 	pipe_config->dpll_hw_state.dpll = intel_de_read(dev_priv,
5585 							DPLL(crtc->pipe));
5586 	if (!IS_VALLEYVIEW(dev_priv) && !IS_CHERRYVIEW(dev_priv)) {
5587 		pipe_config->dpll_hw_state.fp0 = intel_de_read(dev_priv,
5588 							       FP0(crtc->pipe));
5589 		pipe_config->dpll_hw_state.fp1 = intel_de_read(dev_priv,
5590 							       FP1(crtc->pipe));
5591 	} else {
5592 		/* Mask out read-only status bits. */
5593 		pipe_config->dpll_hw_state.dpll &= ~(DPLL_LOCK_VLV |
5594 						     DPLL_PORTC_READY_MASK |
5595 						     DPLL_PORTB_READY_MASK);
5596 	}
5597 
5598 	if (IS_CHERRYVIEW(dev_priv))
5599 		chv_crtc_clock_get(crtc, pipe_config);
5600 	else if (IS_VALLEYVIEW(dev_priv))
5601 		vlv_crtc_clock_get(crtc, pipe_config);
5602 	else
5603 		i9xx_crtc_clock_get(crtc, pipe_config);
5604 
5605 	/*
5606 	 * Normally the dotclock is filled in by the encoder .get_config()
5607 	 * but in case the pipe is enabled w/o any ports we need a sane
5608 	 * default.
5609 	 */
5610 	pipe_config->hw.adjusted_mode.crtc_clock =
5611 		pipe_config->port_clock / pipe_config->pixel_multiplier;
5612 
5613 	ret = true;
5614 
5615 out:
5616 	intel_display_power_put(dev_priv, power_domain, wakeref);
5617 
5618 	return ret;
5619 }
5620 
5621 static void ilk_init_pch_refclk(struct drm_i915_private *dev_priv)
5622 {
5623 	struct intel_encoder *encoder;
5624 	int i;
5625 	u32 val, final;
5626 	bool has_lvds = false;
5627 	bool has_cpu_edp = false;
5628 	bool has_panel = false;
5629 	bool has_ck505 = false;
5630 	bool can_ssc = false;
5631 	bool using_ssc_source = false;
5632 
5633 	/* We need to take the global config into account */
5634 	for_each_intel_encoder(&dev_priv->drm, encoder) {
5635 		switch (encoder->type) {
5636 		case INTEL_OUTPUT_LVDS:
5637 			has_panel = true;
5638 			has_lvds = true;
5639 			break;
5640 		case INTEL_OUTPUT_EDP:
5641 			has_panel = true;
5642 			if (encoder->port == PORT_A)
5643 				has_cpu_edp = true;
5644 			break;
5645 		default:
5646 			break;
5647 		}
5648 	}
5649 
5650 	if (HAS_PCH_IBX(dev_priv)) {
5651 		has_ck505 = dev_priv->vbt.display_clock_mode;
5652 		can_ssc = has_ck505;
5653 	} else {
5654 		has_ck505 = false;
5655 		can_ssc = true;
5656 	}
5657 
5658 	/* Check if any DPLLs are using the SSC source */
5659 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++) {
5660 		u32 temp = intel_de_read(dev_priv, PCH_DPLL(i));
5661 
5662 		if (!(temp & DPLL_VCO_ENABLE))
5663 			continue;
5664 
5665 		if ((temp & PLL_REF_INPUT_MASK) ==
5666 		    PLLB_REF_INPUT_SPREADSPECTRUMIN) {
5667 			using_ssc_source = true;
5668 			break;
5669 		}
5670 	}
5671 
5672 	drm_dbg_kms(&dev_priv->drm,
5673 		    "has_panel %d has_lvds %d has_ck505 %d using_ssc_source %d\n",
5674 		    has_panel, has_lvds, has_ck505, using_ssc_source);
5675 
5676 	/* Ironlake: try to setup display ref clock before DPLL
5677 	 * enabling. This is only under driver's control after
5678 	 * PCH B stepping, previous chipset stepping should be
5679 	 * ignoring this setting.
5680 	 */
5681 	val = intel_de_read(dev_priv, PCH_DREF_CONTROL);
5682 
5683 	/* As we must carefully and slowly disable/enable each source in turn,
5684 	 * compute the final state we want first and check if we need to
5685 	 * make any changes at all.
5686 	 */
5687 	final = val;
5688 	final &= ~DREF_NONSPREAD_SOURCE_MASK;
5689 	if (has_ck505)
5690 		final |= DREF_NONSPREAD_CK505_ENABLE;
5691 	else
5692 		final |= DREF_NONSPREAD_SOURCE_ENABLE;
5693 
5694 	final &= ~DREF_SSC_SOURCE_MASK;
5695 	final &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5696 	final &= ~DREF_SSC1_ENABLE;
5697 
5698 	if (has_panel) {
5699 		final |= DREF_SSC_SOURCE_ENABLE;
5700 
5701 		if (intel_panel_use_ssc(dev_priv) && can_ssc)
5702 			final |= DREF_SSC1_ENABLE;
5703 
5704 		if (has_cpu_edp) {
5705 			if (intel_panel_use_ssc(dev_priv) && can_ssc)
5706 				final |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5707 			else
5708 				final |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5709 		} else
5710 			final |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5711 	} else if (using_ssc_source) {
5712 		final |= DREF_SSC_SOURCE_ENABLE;
5713 		final |= DREF_SSC1_ENABLE;
5714 	}
5715 
5716 	if (final == val)
5717 		return;
5718 
5719 	/* Always enable nonspread source */
5720 	val &= ~DREF_NONSPREAD_SOURCE_MASK;
5721 
5722 	if (has_ck505)
5723 		val |= DREF_NONSPREAD_CK505_ENABLE;
5724 	else
5725 		val |= DREF_NONSPREAD_SOURCE_ENABLE;
5726 
5727 	if (has_panel) {
5728 		val &= ~DREF_SSC_SOURCE_MASK;
5729 		val |= DREF_SSC_SOURCE_ENABLE;
5730 
5731 		/* SSC must be turned on before enabling the CPU output  */
5732 		if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5733 			drm_dbg_kms(&dev_priv->drm, "Using SSC on panel\n");
5734 			val |= DREF_SSC1_ENABLE;
5735 		} else
5736 			val &= ~DREF_SSC1_ENABLE;
5737 
5738 		/* Get SSC going before enabling the outputs */
5739 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5740 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5741 		udelay(200);
5742 
5743 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5744 
5745 		/* Enable CPU source on CPU attached eDP */
5746 		if (has_cpu_edp) {
5747 			if (intel_panel_use_ssc(dev_priv) && can_ssc) {
5748 				drm_dbg_kms(&dev_priv->drm,
5749 					    "Using SSC on eDP\n");
5750 				val |= DREF_CPU_SOURCE_OUTPUT_DOWNSPREAD;
5751 			} else
5752 				val |= DREF_CPU_SOURCE_OUTPUT_NONSPREAD;
5753 		} else
5754 			val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5755 
5756 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5757 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5758 		udelay(200);
5759 	} else {
5760 		drm_dbg_kms(&dev_priv->drm, "Disabling CPU source output\n");
5761 
5762 		val &= ~DREF_CPU_SOURCE_OUTPUT_MASK;
5763 
5764 		/* Turn off CPU output */
5765 		val |= DREF_CPU_SOURCE_OUTPUT_DISABLE;
5766 
5767 		intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5768 		intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5769 		udelay(200);
5770 
5771 		if (!using_ssc_source) {
5772 			drm_dbg_kms(&dev_priv->drm, "Disabling SSC source\n");
5773 
5774 			/* Turn off the SSC source */
5775 			val &= ~DREF_SSC_SOURCE_MASK;
5776 			val |= DREF_SSC_SOURCE_DISABLE;
5777 
5778 			/* Turn off SSC1 */
5779 			val &= ~DREF_SSC1_ENABLE;
5780 
5781 			intel_de_write(dev_priv, PCH_DREF_CONTROL, val);
5782 			intel_de_posting_read(dev_priv, PCH_DREF_CONTROL);
5783 			udelay(200);
5784 		}
5785 	}
5786 
5787 	BUG_ON(val != final);
5788 }
5789 
5790 static void lpt_reset_fdi_mphy(struct drm_i915_private *dev_priv)
5791 {
5792 	u32 tmp;
5793 
5794 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
5795 	tmp |= FDI_MPHY_IOSFSB_RESET_CTL;
5796 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
5797 
5798 	if (wait_for_us(intel_de_read(dev_priv, SOUTH_CHICKEN2) &
5799 			FDI_MPHY_IOSFSB_RESET_STATUS, 100))
5800 		drm_err(&dev_priv->drm, "FDI mPHY reset assert timeout\n");
5801 
5802 	tmp = intel_de_read(dev_priv, SOUTH_CHICKEN2);
5803 	tmp &= ~FDI_MPHY_IOSFSB_RESET_CTL;
5804 	intel_de_write(dev_priv, SOUTH_CHICKEN2, tmp);
5805 
5806 	if (wait_for_us((intel_de_read(dev_priv, SOUTH_CHICKEN2) &
5807 			 FDI_MPHY_IOSFSB_RESET_STATUS) == 0, 100))
5808 		drm_err(&dev_priv->drm, "FDI mPHY reset de-assert timeout\n");
5809 }
5810 
5811 /* WaMPhyProgramming:hsw */
5812 static void lpt_program_fdi_mphy(struct drm_i915_private *dev_priv)
5813 {
5814 	u32 tmp;
5815 
5816 	tmp = intel_sbi_read(dev_priv, 0x8008, SBI_MPHY);
5817 	tmp &= ~(0xFF << 24);
5818 	tmp |= (0x12 << 24);
5819 	intel_sbi_write(dev_priv, 0x8008, tmp, SBI_MPHY);
5820 
5821 	tmp = intel_sbi_read(dev_priv, 0x2008, SBI_MPHY);
5822 	tmp |= (1 << 11);
5823 	intel_sbi_write(dev_priv, 0x2008, tmp, SBI_MPHY);
5824 
5825 	tmp = intel_sbi_read(dev_priv, 0x2108, SBI_MPHY);
5826 	tmp |= (1 << 11);
5827 	intel_sbi_write(dev_priv, 0x2108, tmp, SBI_MPHY);
5828 
5829 	tmp = intel_sbi_read(dev_priv, 0x206C, SBI_MPHY);
5830 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5831 	intel_sbi_write(dev_priv, 0x206C, tmp, SBI_MPHY);
5832 
5833 	tmp = intel_sbi_read(dev_priv, 0x216C, SBI_MPHY);
5834 	tmp |= (1 << 24) | (1 << 21) | (1 << 18);
5835 	intel_sbi_write(dev_priv, 0x216C, tmp, SBI_MPHY);
5836 
5837 	tmp = intel_sbi_read(dev_priv, 0x2080, SBI_MPHY);
5838 	tmp &= ~(7 << 13);
5839 	tmp |= (5 << 13);
5840 	intel_sbi_write(dev_priv, 0x2080, tmp, SBI_MPHY);
5841 
5842 	tmp = intel_sbi_read(dev_priv, 0x2180, SBI_MPHY);
5843 	tmp &= ~(7 << 13);
5844 	tmp |= (5 << 13);
5845 	intel_sbi_write(dev_priv, 0x2180, tmp, SBI_MPHY);
5846 
5847 	tmp = intel_sbi_read(dev_priv, 0x208C, SBI_MPHY);
5848 	tmp &= ~0xFF;
5849 	tmp |= 0x1C;
5850 	intel_sbi_write(dev_priv, 0x208C, tmp, SBI_MPHY);
5851 
5852 	tmp = intel_sbi_read(dev_priv, 0x218C, SBI_MPHY);
5853 	tmp &= ~0xFF;
5854 	tmp |= 0x1C;
5855 	intel_sbi_write(dev_priv, 0x218C, tmp, SBI_MPHY);
5856 
5857 	tmp = intel_sbi_read(dev_priv, 0x2098, SBI_MPHY);
5858 	tmp &= ~(0xFF << 16);
5859 	tmp |= (0x1C << 16);
5860 	intel_sbi_write(dev_priv, 0x2098, tmp, SBI_MPHY);
5861 
5862 	tmp = intel_sbi_read(dev_priv, 0x2198, SBI_MPHY);
5863 	tmp &= ~(0xFF << 16);
5864 	tmp |= (0x1C << 16);
5865 	intel_sbi_write(dev_priv, 0x2198, tmp, SBI_MPHY);
5866 
5867 	tmp = intel_sbi_read(dev_priv, 0x20C4, SBI_MPHY);
5868 	tmp |= (1 << 27);
5869 	intel_sbi_write(dev_priv, 0x20C4, tmp, SBI_MPHY);
5870 
5871 	tmp = intel_sbi_read(dev_priv, 0x21C4, SBI_MPHY);
5872 	tmp |= (1 << 27);
5873 	intel_sbi_write(dev_priv, 0x21C4, tmp, SBI_MPHY);
5874 
5875 	tmp = intel_sbi_read(dev_priv, 0x20EC, SBI_MPHY);
5876 	tmp &= ~(0xF << 28);
5877 	tmp |= (4 << 28);
5878 	intel_sbi_write(dev_priv, 0x20EC, tmp, SBI_MPHY);
5879 
5880 	tmp = intel_sbi_read(dev_priv, 0x21EC, SBI_MPHY);
5881 	tmp &= ~(0xF << 28);
5882 	tmp |= (4 << 28);
5883 	intel_sbi_write(dev_priv, 0x21EC, tmp, SBI_MPHY);
5884 }
5885 
5886 /* Implements 3 different sequences from BSpec chapter "Display iCLK
5887  * Programming" based on the parameters passed:
5888  * - Sequence to enable CLKOUT_DP
5889  * - Sequence to enable CLKOUT_DP without spread
5890  * - Sequence to enable CLKOUT_DP for FDI usage and configure PCH FDI I/O
5891  */
5892 static void lpt_enable_clkout_dp(struct drm_i915_private *dev_priv,
5893 				 bool with_spread, bool with_fdi)
5894 {
5895 	u32 reg, tmp;
5896 
5897 	if (drm_WARN(&dev_priv->drm, with_fdi && !with_spread,
5898 		     "FDI requires downspread\n"))
5899 		with_spread = true;
5900 	if (drm_WARN(&dev_priv->drm, HAS_PCH_LPT_LP(dev_priv) &&
5901 		     with_fdi, "LP PCH doesn't have FDI\n"))
5902 		with_fdi = false;
5903 
5904 	mutex_lock(&dev_priv->sb_lock);
5905 
5906 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5907 	tmp &= ~SBI_SSCCTL_DISABLE;
5908 	tmp |= SBI_SSCCTL_PATHALT;
5909 	intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5910 
5911 	udelay(24);
5912 
5913 	if (with_spread) {
5914 		tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5915 		tmp &= ~SBI_SSCCTL_PATHALT;
5916 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5917 
5918 		if (with_fdi) {
5919 			lpt_reset_fdi_mphy(dev_priv);
5920 			lpt_program_fdi_mphy(dev_priv);
5921 		}
5922 	}
5923 
5924 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
5925 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5926 	tmp |= SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5927 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5928 
5929 	mutex_unlock(&dev_priv->sb_lock);
5930 }
5931 
5932 /* Sequence to disable CLKOUT_DP */
5933 void lpt_disable_clkout_dp(struct drm_i915_private *dev_priv)
5934 {
5935 	u32 reg, tmp;
5936 
5937 	mutex_lock(&dev_priv->sb_lock);
5938 
5939 	reg = HAS_PCH_LPT_LP(dev_priv) ? SBI_GEN0 : SBI_DBUFF0;
5940 	tmp = intel_sbi_read(dev_priv, reg, SBI_ICLK);
5941 	tmp &= ~SBI_GEN0_CFG_BUFFENABLE_DISABLE;
5942 	intel_sbi_write(dev_priv, reg, tmp, SBI_ICLK);
5943 
5944 	tmp = intel_sbi_read(dev_priv, SBI_SSCCTL, SBI_ICLK);
5945 	if (!(tmp & SBI_SSCCTL_DISABLE)) {
5946 		if (!(tmp & SBI_SSCCTL_PATHALT)) {
5947 			tmp |= SBI_SSCCTL_PATHALT;
5948 			intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5949 			udelay(32);
5950 		}
5951 		tmp |= SBI_SSCCTL_DISABLE;
5952 		intel_sbi_write(dev_priv, SBI_SSCCTL, tmp, SBI_ICLK);
5953 	}
5954 
5955 	mutex_unlock(&dev_priv->sb_lock);
5956 }
5957 
5958 #define BEND_IDX(steps) ((50 + (steps)) / 5)
5959 
5960 static const u16 sscdivintphase[] = {
5961 	[BEND_IDX( 50)] = 0x3B23,
5962 	[BEND_IDX( 45)] = 0x3B23,
5963 	[BEND_IDX( 40)] = 0x3C23,
5964 	[BEND_IDX( 35)] = 0x3C23,
5965 	[BEND_IDX( 30)] = 0x3D23,
5966 	[BEND_IDX( 25)] = 0x3D23,
5967 	[BEND_IDX( 20)] = 0x3E23,
5968 	[BEND_IDX( 15)] = 0x3E23,
5969 	[BEND_IDX( 10)] = 0x3F23,
5970 	[BEND_IDX(  5)] = 0x3F23,
5971 	[BEND_IDX(  0)] = 0x0025,
5972 	[BEND_IDX( -5)] = 0x0025,
5973 	[BEND_IDX(-10)] = 0x0125,
5974 	[BEND_IDX(-15)] = 0x0125,
5975 	[BEND_IDX(-20)] = 0x0225,
5976 	[BEND_IDX(-25)] = 0x0225,
5977 	[BEND_IDX(-30)] = 0x0325,
5978 	[BEND_IDX(-35)] = 0x0325,
5979 	[BEND_IDX(-40)] = 0x0425,
5980 	[BEND_IDX(-45)] = 0x0425,
5981 	[BEND_IDX(-50)] = 0x0525,
5982 };
5983 
5984 /*
5985  * Bend CLKOUT_DP
5986  * steps -50 to 50 inclusive, in steps of 5
5987  * < 0 slow down the clock, > 0 speed up the clock, 0 == no bend (135MHz)
5988  * change in clock period = -(steps / 10) * 5.787 ps
5989  */
5990 static void lpt_bend_clkout_dp(struct drm_i915_private *dev_priv, int steps)
5991 {
5992 	u32 tmp;
5993 	int idx = BEND_IDX(steps);
5994 
5995 	if (drm_WARN_ON(&dev_priv->drm, steps % 5 != 0))
5996 		return;
5997 
5998 	if (drm_WARN_ON(&dev_priv->drm, idx >= ARRAY_SIZE(sscdivintphase)))
5999 		return;
6000 
6001 	mutex_lock(&dev_priv->sb_lock);
6002 
6003 	if (steps % 10 != 0)
6004 		tmp = 0xAAAAAAAB;
6005 	else
6006 		tmp = 0x00000000;
6007 	intel_sbi_write(dev_priv, SBI_SSCDITHPHASE, tmp, SBI_ICLK);
6008 
6009 	tmp = intel_sbi_read(dev_priv, SBI_SSCDIVINTPHASE, SBI_ICLK);
6010 	tmp &= 0xffff0000;
6011 	tmp |= sscdivintphase[idx];
6012 	intel_sbi_write(dev_priv, SBI_SSCDIVINTPHASE, tmp, SBI_ICLK);
6013 
6014 	mutex_unlock(&dev_priv->sb_lock);
6015 }
6016 
6017 #undef BEND_IDX
6018 
6019 static bool spll_uses_pch_ssc(struct drm_i915_private *dev_priv)
6020 {
6021 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
6022 	u32 ctl = intel_de_read(dev_priv, SPLL_CTL);
6023 
6024 	if ((ctl & SPLL_PLL_ENABLE) == 0)
6025 		return false;
6026 
6027 	if ((ctl & SPLL_REF_MASK) == SPLL_REF_MUXED_SSC &&
6028 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
6029 		return true;
6030 
6031 	if (IS_BROADWELL(dev_priv) &&
6032 	    (ctl & SPLL_REF_MASK) == SPLL_REF_PCH_SSC_BDW)
6033 		return true;
6034 
6035 	return false;
6036 }
6037 
6038 static bool wrpll_uses_pch_ssc(struct drm_i915_private *dev_priv,
6039 			       enum intel_dpll_id id)
6040 {
6041 	u32 fuse_strap = intel_de_read(dev_priv, FUSE_STRAP);
6042 	u32 ctl = intel_de_read(dev_priv, WRPLL_CTL(id));
6043 
6044 	if ((ctl & WRPLL_PLL_ENABLE) == 0)
6045 		return false;
6046 
6047 	if ((ctl & WRPLL_REF_MASK) == WRPLL_REF_PCH_SSC)
6048 		return true;
6049 
6050 	if ((IS_BROADWELL(dev_priv) || IS_HSW_ULT(dev_priv)) &&
6051 	    (ctl & WRPLL_REF_MASK) == WRPLL_REF_MUXED_SSC_BDW &&
6052 	    (fuse_strap & HSW_CPU_SSC_ENABLE) == 0)
6053 		return true;
6054 
6055 	return false;
6056 }
6057 
6058 static void lpt_init_pch_refclk(struct drm_i915_private *dev_priv)
6059 {
6060 	struct intel_encoder *encoder;
6061 	bool has_fdi = false;
6062 
6063 	for_each_intel_encoder(&dev_priv->drm, encoder) {
6064 		switch (encoder->type) {
6065 		case INTEL_OUTPUT_ANALOG:
6066 			has_fdi = true;
6067 			break;
6068 		default:
6069 			break;
6070 		}
6071 	}
6072 
6073 	/*
6074 	 * The BIOS may have decided to use the PCH SSC
6075 	 * reference so we must not disable it until the
6076 	 * relevant PLLs have stopped relying on it. We'll
6077 	 * just leave the PCH SSC reference enabled in case
6078 	 * any active PLL is using it. It will get disabled
6079 	 * after runtime suspend if we don't have FDI.
6080 	 *
6081 	 * TODO: Move the whole reference clock handling
6082 	 * to the modeset sequence proper so that we can
6083 	 * actually enable/disable/reconfigure these things
6084 	 * safely. To do that we need to introduce a real
6085 	 * clock hierarchy. That would also allow us to do
6086 	 * clock bending finally.
6087 	 */
6088 	dev_priv->pch_ssc_use = 0;
6089 
6090 	if (spll_uses_pch_ssc(dev_priv)) {
6091 		drm_dbg_kms(&dev_priv->drm, "SPLL using PCH SSC\n");
6092 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_SPLL);
6093 	}
6094 
6095 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL1)) {
6096 		drm_dbg_kms(&dev_priv->drm, "WRPLL1 using PCH SSC\n");
6097 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL1);
6098 	}
6099 
6100 	if (wrpll_uses_pch_ssc(dev_priv, DPLL_ID_WRPLL2)) {
6101 		drm_dbg_kms(&dev_priv->drm, "WRPLL2 using PCH SSC\n");
6102 		dev_priv->pch_ssc_use |= BIT(DPLL_ID_WRPLL2);
6103 	}
6104 
6105 	if (dev_priv->pch_ssc_use)
6106 		return;
6107 
6108 	if (has_fdi) {
6109 		lpt_bend_clkout_dp(dev_priv, 0);
6110 		lpt_enable_clkout_dp(dev_priv, true, true);
6111 	} else {
6112 		lpt_disable_clkout_dp(dev_priv);
6113 	}
6114 }
6115 
6116 /*
6117  * Initialize reference clocks when the driver loads
6118  */
6119 void intel_init_pch_refclk(struct drm_i915_private *dev_priv)
6120 {
6121 	if (HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv))
6122 		ilk_init_pch_refclk(dev_priv);
6123 	else if (HAS_PCH_LPT(dev_priv))
6124 		lpt_init_pch_refclk(dev_priv);
6125 }
6126 
6127 static void ilk_set_pipeconf(const struct intel_crtc_state *crtc_state)
6128 {
6129 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6130 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6131 	enum pipe pipe = crtc->pipe;
6132 	u32 val;
6133 
6134 	val = 0;
6135 
6136 	switch (crtc_state->pipe_bpp) {
6137 	case 18:
6138 		val |= PIPECONF_6BPC;
6139 		break;
6140 	case 24:
6141 		val |= PIPECONF_8BPC;
6142 		break;
6143 	case 30:
6144 		val |= PIPECONF_10BPC;
6145 		break;
6146 	case 36:
6147 		val |= PIPECONF_12BPC;
6148 		break;
6149 	default:
6150 		/* Case prevented by intel_choose_pipe_bpp_dither. */
6151 		BUG();
6152 	}
6153 
6154 	if (crtc_state->dither)
6155 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6156 
6157 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6158 		val |= PIPECONF_INTERLACED_ILK;
6159 	else
6160 		val |= PIPECONF_PROGRESSIVE;
6161 
6162 	/*
6163 	 * This would end up with an odd purple hue over
6164 	 * the entire display. Make sure we don't do it.
6165 	 */
6166 	drm_WARN_ON(&dev_priv->drm, crtc_state->limited_color_range &&
6167 		    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB);
6168 
6169 	if (crtc_state->limited_color_range &&
6170 	    !intel_crtc_has_type(crtc_state, INTEL_OUTPUT_SDVO))
6171 		val |= PIPECONF_COLOR_RANGE_SELECT;
6172 
6173 	if (crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
6174 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV709;
6175 
6176 	val |= PIPECONF_GAMMA_MODE(crtc_state->gamma_mode);
6177 
6178 	val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
6179 
6180 	intel_de_write(dev_priv, PIPECONF(pipe), val);
6181 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
6182 }
6183 
6184 static void hsw_set_pipeconf(const struct intel_crtc_state *crtc_state)
6185 {
6186 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6187 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6188 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
6189 	u32 val = 0;
6190 
6191 	if (IS_HASWELL(dev_priv) && crtc_state->dither)
6192 		val |= (PIPECONF_DITHER_EN | PIPECONF_DITHER_TYPE_SP);
6193 
6194 	if (crtc_state->hw.adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
6195 		val |= PIPECONF_INTERLACED_ILK;
6196 	else
6197 		val |= PIPECONF_PROGRESSIVE;
6198 
6199 	if (IS_HASWELL(dev_priv) &&
6200 	    crtc_state->output_format != INTEL_OUTPUT_FORMAT_RGB)
6201 		val |= PIPECONF_OUTPUT_COLORSPACE_YUV_HSW;
6202 
6203 	intel_de_write(dev_priv, PIPECONF(cpu_transcoder), val);
6204 	intel_de_posting_read(dev_priv, PIPECONF(cpu_transcoder));
6205 }
6206 
6207 static void bdw_set_pipemisc(const struct intel_crtc_state *crtc_state)
6208 {
6209 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6210 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6211 	u32 val = 0;
6212 
6213 	switch (crtc_state->pipe_bpp) {
6214 	case 18:
6215 		val |= PIPEMISC_DITHER_6_BPC;
6216 		break;
6217 	case 24:
6218 		val |= PIPEMISC_DITHER_8_BPC;
6219 		break;
6220 	case 30:
6221 		val |= PIPEMISC_DITHER_10_BPC;
6222 		break;
6223 	case 36:
6224 		val |= PIPEMISC_DITHER_12_BPC;
6225 		break;
6226 	default:
6227 		MISSING_CASE(crtc_state->pipe_bpp);
6228 		break;
6229 	}
6230 
6231 	if (crtc_state->dither)
6232 		val |= PIPEMISC_DITHER_ENABLE | PIPEMISC_DITHER_TYPE_SP;
6233 
6234 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420 ||
6235 	    crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR444)
6236 		val |= PIPEMISC_OUTPUT_COLORSPACE_YUV;
6237 
6238 	if (crtc_state->output_format == INTEL_OUTPUT_FORMAT_YCBCR420)
6239 		val |= PIPEMISC_YUV420_ENABLE |
6240 			PIPEMISC_YUV420_MODE_FULL_BLEND;
6241 
6242 	if (INTEL_GEN(dev_priv) >= 11 &&
6243 	    (crtc_state->active_planes & ~(icl_hdr_plane_mask() |
6244 					   BIT(PLANE_CURSOR))) == 0)
6245 		val |= PIPEMISC_HDR_MODE_PRECISION;
6246 
6247 	if (INTEL_GEN(dev_priv) >= 12)
6248 		val |= PIPEMISC_PIXEL_ROUNDING_TRUNC;
6249 
6250 	intel_de_write(dev_priv, PIPEMISC(crtc->pipe), val);
6251 }
6252 
6253 int bdw_get_pipemisc_bpp(struct intel_crtc *crtc)
6254 {
6255 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6256 	u32 tmp;
6257 
6258 	tmp = intel_de_read(dev_priv, PIPEMISC(crtc->pipe));
6259 
6260 	switch (tmp & PIPEMISC_DITHER_BPC_MASK) {
6261 	case PIPEMISC_DITHER_6_BPC:
6262 		return 18;
6263 	case PIPEMISC_DITHER_8_BPC:
6264 		return 24;
6265 	case PIPEMISC_DITHER_10_BPC:
6266 		return 30;
6267 	case PIPEMISC_DITHER_12_BPC:
6268 		return 36;
6269 	default:
6270 		MISSING_CASE(tmp);
6271 		return 0;
6272 	}
6273 }
6274 
6275 int ilk_get_lanes_required(int target_clock, int link_bw, int bpp)
6276 {
6277 	/*
6278 	 * Account for spread spectrum to avoid
6279 	 * oversubscribing the link. Max center spread
6280 	 * is 2.5%; use 5% for safety's sake.
6281 	 */
6282 	u32 bps = target_clock * bpp * 21 / 20;
6283 	return DIV_ROUND_UP(bps, link_bw * 8);
6284 }
6285 
6286 static void intel_pch_transcoder_get_m_n(struct intel_crtc *crtc,
6287 					 struct intel_link_m_n *m_n)
6288 {
6289 	struct drm_device *dev = crtc->base.dev;
6290 	struct drm_i915_private *dev_priv = to_i915(dev);
6291 	enum pipe pipe = crtc->pipe;
6292 
6293 	m_n->link_m = intel_de_read(dev_priv, PCH_TRANS_LINK_M1(pipe));
6294 	m_n->link_n = intel_de_read(dev_priv, PCH_TRANS_LINK_N1(pipe));
6295 	m_n->gmch_m = intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
6296 		& ~TU_SIZE_MASK;
6297 	m_n->gmch_n = intel_de_read(dev_priv, PCH_TRANS_DATA_N1(pipe));
6298 	m_n->tu = ((intel_de_read(dev_priv, PCH_TRANS_DATA_M1(pipe))
6299 		    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6300 }
6301 
6302 static void intel_cpu_transcoder_get_m_n(struct intel_crtc *crtc,
6303 					 enum transcoder transcoder,
6304 					 struct intel_link_m_n *m_n,
6305 					 struct intel_link_m_n *m2_n2)
6306 {
6307 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6308 	enum pipe pipe = crtc->pipe;
6309 
6310 	if (INTEL_GEN(dev_priv) >= 5) {
6311 		m_n->link_m = intel_de_read(dev_priv,
6312 					    PIPE_LINK_M1(transcoder));
6313 		m_n->link_n = intel_de_read(dev_priv,
6314 					    PIPE_LINK_N1(transcoder));
6315 		m_n->gmch_m = intel_de_read(dev_priv,
6316 					    PIPE_DATA_M1(transcoder))
6317 			& ~TU_SIZE_MASK;
6318 		m_n->gmch_n = intel_de_read(dev_priv,
6319 					    PIPE_DATA_N1(transcoder));
6320 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M1(transcoder))
6321 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6322 
6323 		if (m2_n2 && transcoder_has_m2_n2(dev_priv, transcoder)) {
6324 			m2_n2->link_m = intel_de_read(dev_priv,
6325 						      PIPE_LINK_M2(transcoder));
6326 			m2_n2->link_n =	intel_de_read(dev_priv,
6327 							     PIPE_LINK_N2(transcoder));
6328 			m2_n2->gmch_m =	intel_de_read(dev_priv,
6329 							     PIPE_DATA_M2(transcoder))
6330 					& ~TU_SIZE_MASK;
6331 			m2_n2->gmch_n =	intel_de_read(dev_priv,
6332 							     PIPE_DATA_N2(transcoder));
6333 			m2_n2->tu = ((intel_de_read(dev_priv, PIPE_DATA_M2(transcoder))
6334 					& TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6335 		}
6336 	} else {
6337 		m_n->link_m = intel_de_read(dev_priv, PIPE_LINK_M_G4X(pipe));
6338 		m_n->link_n = intel_de_read(dev_priv, PIPE_LINK_N_G4X(pipe));
6339 		m_n->gmch_m = intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
6340 			& ~TU_SIZE_MASK;
6341 		m_n->gmch_n = intel_de_read(dev_priv, PIPE_DATA_N_G4X(pipe));
6342 		m_n->tu = ((intel_de_read(dev_priv, PIPE_DATA_M_G4X(pipe))
6343 			    & TU_SIZE_MASK) >> TU_SIZE_SHIFT) + 1;
6344 	}
6345 }
6346 
6347 void intel_dp_get_m_n(struct intel_crtc *crtc,
6348 		      struct intel_crtc_state *pipe_config)
6349 {
6350 	if (pipe_config->has_pch_encoder)
6351 		intel_pch_transcoder_get_m_n(crtc, &pipe_config->dp_m_n);
6352 	else
6353 		intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6354 					     &pipe_config->dp_m_n,
6355 					     &pipe_config->dp_m2_n2);
6356 }
6357 
6358 static void ilk_get_fdi_m_n_config(struct intel_crtc *crtc,
6359 				   struct intel_crtc_state *pipe_config)
6360 {
6361 	intel_cpu_transcoder_get_m_n(crtc, pipe_config->cpu_transcoder,
6362 				     &pipe_config->fdi_m_n, NULL);
6363 }
6364 
6365 static void ilk_get_pfit_pos_size(struct intel_crtc_state *crtc_state,
6366 				  u32 pos, u32 size)
6367 {
6368 	drm_rect_init(&crtc_state->pch_pfit.dst,
6369 		      pos >> 16, pos & 0xffff,
6370 		      size >> 16, size & 0xffff);
6371 }
6372 
6373 static void skl_get_pfit_config(struct intel_crtc_state *crtc_state)
6374 {
6375 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6376 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6377 	struct intel_crtc_scaler_state *scaler_state = &crtc_state->scaler_state;
6378 	int id = -1;
6379 	int i;
6380 
6381 	/* find scaler attached to this pipe */
6382 	for (i = 0; i < crtc->num_scalers; i++) {
6383 		u32 ctl, pos, size;
6384 
6385 		ctl = intel_de_read(dev_priv, SKL_PS_CTRL(crtc->pipe, i));
6386 		if ((ctl & (PS_SCALER_EN | PS_PLANE_SEL_MASK)) != PS_SCALER_EN)
6387 			continue;
6388 
6389 		id = i;
6390 		crtc_state->pch_pfit.enabled = true;
6391 
6392 		pos = intel_de_read(dev_priv, SKL_PS_WIN_POS(crtc->pipe, i));
6393 		size = intel_de_read(dev_priv, SKL_PS_WIN_SZ(crtc->pipe, i));
6394 
6395 		ilk_get_pfit_pos_size(crtc_state, pos, size);
6396 
6397 		scaler_state->scalers[i].in_use = true;
6398 		break;
6399 	}
6400 
6401 	scaler_state->scaler_id = id;
6402 	if (id >= 0)
6403 		scaler_state->scaler_users |= (1 << SKL_CRTC_INDEX);
6404 	else
6405 		scaler_state->scaler_users &= ~(1 << SKL_CRTC_INDEX);
6406 }
6407 
6408 static void ilk_get_pfit_config(struct intel_crtc_state *crtc_state)
6409 {
6410 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6411 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6412 	u32 ctl, pos, size;
6413 
6414 	ctl = intel_de_read(dev_priv, PF_CTL(crtc->pipe));
6415 	if ((ctl & PF_ENABLE) == 0)
6416 		return;
6417 
6418 	crtc_state->pch_pfit.enabled = true;
6419 
6420 	pos = intel_de_read(dev_priv, PF_WIN_POS(crtc->pipe));
6421 	size = intel_de_read(dev_priv, PF_WIN_SZ(crtc->pipe));
6422 
6423 	ilk_get_pfit_pos_size(crtc_state, pos, size);
6424 
6425 	/*
6426 	 * We currently do not free assignements of panel fitters on
6427 	 * ivb/hsw (since we don't use the higher upscaling modes which
6428 	 * differentiates them) so just WARN about this case for now.
6429 	 */
6430 	drm_WARN_ON(&dev_priv->drm, IS_GEN(dev_priv, 7) &&
6431 		    (ctl & PF_PIPE_SEL_MASK_IVB) != PF_PIPE_SEL_IVB(crtc->pipe));
6432 }
6433 
6434 static bool ilk_get_pipe_config(struct intel_crtc *crtc,
6435 				struct intel_crtc_state *pipe_config)
6436 {
6437 	struct drm_device *dev = crtc->base.dev;
6438 	struct drm_i915_private *dev_priv = to_i915(dev);
6439 	enum intel_display_power_domain power_domain;
6440 	intel_wakeref_t wakeref;
6441 	u32 tmp;
6442 	bool ret;
6443 
6444 	power_domain = POWER_DOMAIN_PIPE(crtc->pipe);
6445 	wakeref = intel_display_power_get_if_enabled(dev_priv, power_domain);
6446 	if (!wakeref)
6447 		return false;
6448 
6449 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6450 	pipe_config->shared_dpll = NULL;
6451 
6452 	ret = false;
6453 	tmp = intel_de_read(dev_priv, PIPECONF(crtc->pipe));
6454 	if (!(tmp & PIPECONF_ENABLE))
6455 		goto out;
6456 
6457 	switch (tmp & PIPECONF_BPC_MASK) {
6458 	case PIPECONF_6BPC:
6459 		pipe_config->pipe_bpp = 18;
6460 		break;
6461 	case PIPECONF_8BPC:
6462 		pipe_config->pipe_bpp = 24;
6463 		break;
6464 	case PIPECONF_10BPC:
6465 		pipe_config->pipe_bpp = 30;
6466 		break;
6467 	case PIPECONF_12BPC:
6468 		pipe_config->pipe_bpp = 36;
6469 		break;
6470 	default:
6471 		break;
6472 	}
6473 
6474 	if (tmp & PIPECONF_COLOR_RANGE_SELECT)
6475 		pipe_config->limited_color_range = true;
6476 
6477 	switch (tmp & PIPECONF_OUTPUT_COLORSPACE_MASK) {
6478 	case PIPECONF_OUTPUT_COLORSPACE_YUV601:
6479 	case PIPECONF_OUTPUT_COLORSPACE_YUV709:
6480 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
6481 		break;
6482 	default:
6483 		pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
6484 		break;
6485 	}
6486 
6487 	pipe_config->gamma_mode = (tmp & PIPECONF_GAMMA_MODE_MASK_ILK) >>
6488 		PIPECONF_GAMMA_MODE_SHIFT;
6489 
6490 	pipe_config->csc_mode = intel_de_read(dev_priv,
6491 					      PIPE_CSC_MODE(crtc->pipe));
6492 
6493 	i9xx_get_pipe_color_config(pipe_config);
6494 	intel_color_get_config(pipe_config);
6495 
6496 	if (intel_de_read(dev_priv, PCH_TRANSCONF(crtc->pipe)) & TRANS_ENABLE) {
6497 		struct intel_shared_dpll *pll;
6498 		enum intel_dpll_id pll_id;
6499 		bool pll_active;
6500 
6501 		pipe_config->has_pch_encoder = true;
6502 
6503 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(crtc->pipe));
6504 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6505 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
6506 
6507 		ilk_get_fdi_m_n_config(crtc, pipe_config);
6508 
6509 		if (HAS_PCH_IBX(dev_priv)) {
6510 			/*
6511 			 * The pipe->pch transcoder and pch transcoder->pll
6512 			 * mapping is fixed.
6513 			 */
6514 			pll_id = (enum intel_dpll_id) crtc->pipe;
6515 		} else {
6516 			tmp = intel_de_read(dev_priv, PCH_DPLL_SEL);
6517 			if (tmp & TRANS_DPLLB_SEL(crtc->pipe))
6518 				pll_id = DPLL_ID_PCH_PLL_B;
6519 			else
6520 				pll_id= DPLL_ID_PCH_PLL_A;
6521 		}
6522 
6523 		pipe_config->shared_dpll =
6524 			intel_get_shared_dpll_by_id(dev_priv, pll_id);
6525 		pll = pipe_config->shared_dpll;
6526 
6527 		pll_active = intel_dpll_get_hw_state(dev_priv, pll,
6528 						     &pipe_config->dpll_hw_state);
6529 		drm_WARN_ON(dev, !pll_active);
6530 
6531 		tmp = pipe_config->dpll_hw_state.dpll;
6532 		pipe_config->pixel_multiplier =
6533 			((tmp & PLL_REF_SDVO_HDMI_MULTIPLIER_MASK)
6534 			 >> PLL_REF_SDVO_HDMI_MULTIPLIER_SHIFT) + 1;
6535 
6536 		ilk_pch_clock_get(crtc, pipe_config);
6537 	} else {
6538 		pipe_config->pixel_multiplier = 1;
6539 	}
6540 
6541 	intel_get_transcoder_timings(crtc, pipe_config);
6542 	intel_get_pipe_src_size(crtc, pipe_config);
6543 
6544 	ilk_get_pfit_config(pipe_config);
6545 
6546 	ret = true;
6547 
6548 out:
6549 	intel_display_power_put(dev_priv, power_domain, wakeref);
6550 
6551 	return ret;
6552 }
6553 
6554 static bool hsw_get_transcoder_state(struct intel_crtc *crtc,
6555 				     struct intel_crtc_state *pipe_config,
6556 				     struct intel_display_power_domain_set *power_domain_set)
6557 {
6558 	struct drm_device *dev = crtc->base.dev;
6559 	struct drm_i915_private *dev_priv = to_i915(dev);
6560 	unsigned long panel_transcoder_mask = BIT(TRANSCODER_EDP);
6561 	unsigned long enabled_panel_transcoders = 0;
6562 	enum transcoder panel_transcoder;
6563 	u32 tmp;
6564 
6565 	if (INTEL_GEN(dev_priv) >= 11)
6566 		panel_transcoder_mask |=
6567 			BIT(TRANSCODER_DSI_0) | BIT(TRANSCODER_DSI_1);
6568 
6569 	/*
6570 	 * The pipe->transcoder mapping is fixed with the exception of the eDP
6571 	 * and DSI transcoders handled below.
6572 	 */
6573 	pipe_config->cpu_transcoder = (enum transcoder) crtc->pipe;
6574 
6575 	/*
6576 	 * XXX: Do intel_display_power_get_if_enabled before reading this (for
6577 	 * consistency and less surprising code; it's in always on power).
6578 	 */
6579 	for_each_cpu_transcoder_masked(dev_priv, panel_transcoder,
6580 				       panel_transcoder_mask) {
6581 		bool force_thru = false;
6582 		enum pipe trans_pipe;
6583 
6584 		tmp = intel_de_read(dev_priv,
6585 				    TRANS_DDI_FUNC_CTL(panel_transcoder));
6586 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
6587 			continue;
6588 
6589 		/*
6590 		 * Log all enabled ones, only use the first one.
6591 		 *
6592 		 * FIXME: This won't work for two separate DSI displays.
6593 		 */
6594 		enabled_panel_transcoders |= BIT(panel_transcoder);
6595 		if (enabled_panel_transcoders != BIT(panel_transcoder))
6596 			continue;
6597 
6598 		switch (tmp & TRANS_DDI_EDP_INPUT_MASK) {
6599 		default:
6600 			drm_WARN(dev, 1,
6601 				 "unknown pipe linked to transcoder %s\n",
6602 				 transcoder_name(panel_transcoder));
6603 			fallthrough;
6604 		case TRANS_DDI_EDP_INPUT_A_ONOFF:
6605 			force_thru = true;
6606 			fallthrough;
6607 		case TRANS_DDI_EDP_INPUT_A_ON:
6608 			trans_pipe = PIPE_A;
6609 			break;
6610 		case TRANS_DDI_EDP_INPUT_B_ONOFF:
6611 			trans_pipe = PIPE_B;
6612 			break;
6613 		case TRANS_DDI_EDP_INPUT_C_ONOFF:
6614 			trans_pipe = PIPE_C;
6615 			break;
6616 		case TRANS_DDI_EDP_INPUT_D_ONOFF:
6617 			trans_pipe = PIPE_D;
6618 			break;
6619 		}
6620 
6621 		if (trans_pipe == crtc->pipe) {
6622 			pipe_config->cpu_transcoder = panel_transcoder;
6623 			pipe_config->pch_pfit.force_thru = force_thru;
6624 		}
6625 	}
6626 
6627 	/*
6628 	 * Valid combos: none, eDP, DSI0, DSI1, DSI0+DSI1
6629 	 */
6630 	drm_WARN_ON(dev, (enabled_panel_transcoders & BIT(TRANSCODER_EDP)) &&
6631 		    enabled_panel_transcoders != BIT(TRANSCODER_EDP));
6632 
6633 	if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
6634 						       POWER_DOMAIN_TRANSCODER(pipe_config->cpu_transcoder)))
6635 		return false;
6636 
6637 	tmp = intel_de_read(dev_priv, PIPECONF(pipe_config->cpu_transcoder));
6638 
6639 	return tmp & PIPECONF_ENABLE;
6640 }
6641 
6642 static bool bxt_get_dsi_transcoder_state(struct intel_crtc *crtc,
6643 					 struct intel_crtc_state *pipe_config,
6644 					 struct intel_display_power_domain_set *power_domain_set)
6645 {
6646 	struct drm_device *dev = crtc->base.dev;
6647 	struct drm_i915_private *dev_priv = to_i915(dev);
6648 	enum transcoder cpu_transcoder;
6649 	enum port port;
6650 	u32 tmp;
6651 
6652 	for_each_port_masked(port, BIT(PORT_A) | BIT(PORT_C)) {
6653 		if (port == PORT_A)
6654 			cpu_transcoder = TRANSCODER_DSI_A;
6655 		else
6656 			cpu_transcoder = TRANSCODER_DSI_C;
6657 
6658 		if (!intel_display_power_get_in_set_if_enabled(dev_priv, power_domain_set,
6659 							       POWER_DOMAIN_TRANSCODER(cpu_transcoder)))
6660 			continue;
6661 
6662 		/*
6663 		 * The PLL needs to be enabled with a valid divider
6664 		 * configuration, otherwise accessing DSI registers will hang
6665 		 * the machine. See BSpec North Display Engine
6666 		 * registers/MIPI[BXT]. We can break out here early, since we
6667 		 * need the same DSI PLL to be enabled for both DSI ports.
6668 		 */
6669 		if (!bxt_dsi_pll_is_enabled(dev_priv))
6670 			break;
6671 
6672 		/* XXX: this works for video mode only */
6673 		tmp = intel_de_read(dev_priv, BXT_MIPI_PORT_CTRL(port));
6674 		if (!(tmp & DPI_ENABLE))
6675 			continue;
6676 
6677 		tmp = intel_de_read(dev_priv, MIPI_CTRL(port));
6678 		if ((tmp & BXT_PIPE_SELECT_MASK) != BXT_PIPE_SELECT(crtc->pipe))
6679 			continue;
6680 
6681 		pipe_config->cpu_transcoder = cpu_transcoder;
6682 		break;
6683 	}
6684 
6685 	return transcoder_is_dsi(pipe_config->cpu_transcoder);
6686 }
6687 
6688 static void hsw_get_ddi_port_state(struct intel_crtc *crtc,
6689 				   struct intel_crtc_state *pipe_config)
6690 {
6691 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6692 	enum transcoder cpu_transcoder = pipe_config->cpu_transcoder;
6693 	enum port port;
6694 	u32 tmp;
6695 
6696 	if (transcoder_is_dsi(cpu_transcoder)) {
6697 		port = (cpu_transcoder == TRANSCODER_DSI_A) ?
6698 						PORT_A : PORT_B;
6699 	} else {
6700 		tmp = intel_de_read(dev_priv,
6701 				    TRANS_DDI_FUNC_CTL(cpu_transcoder));
6702 		if (!(tmp & TRANS_DDI_FUNC_ENABLE))
6703 			return;
6704 		if (INTEL_GEN(dev_priv) >= 12)
6705 			port = TGL_TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
6706 		else
6707 			port = TRANS_DDI_FUNC_CTL_VAL_TO_PORT(tmp);
6708 	}
6709 
6710 	/*
6711 	 * Haswell has only FDI/PCH transcoder A. It is which is connected to
6712 	 * DDI E. So just check whether this pipe is wired to DDI E and whether
6713 	 * the PCH transcoder is on.
6714 	 */
6715 	if (INTEL_GEN(dev_priv) < 9 &&
6716 	    (port == PORT_E) && intel_de_read(dev_priv, LPT_TRANSCONF) & TRANS_ENABLE) {
6717 		pipe_config->has_pch_encoder = true;
6718 
6719 		tmp = intel_de_read(dev_priv, FDI_RX_CTL(PIPE_A));
6720 		pipe_config->fdi_lanes = ((FDI_DP_PORT_WIDTH_MASK & tmp) >>
6721 					  FDI_DP_PORT_WIDTH_SHIFT) + 1;
6722 
6723 		ilk_get_fdi_m_n_config(crtc, pipe_config);
6724 	}
6725 }
6726 
6727 static bool hsw_get_pipe_config(struct intel_crtc *crtc,
6728 				struct intel_crtc_state *pipe_config)
6729 {
6730 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
6731 	struct intel_display_power_domain_set power_domain_set = { };
6732 	bool active;
6733 	u32 tmp;
6734 
6735 	if (!intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
6736 						       POWER_DOMAIN_PIPE(crtc->pipe)))
6737 		return false;
6738 
6739 	pipe_config->shared_dpll = NULL;
6740 
6741 	active = hsw_get_transcoder_state(crtc, pipe_config, &power_domain_set);
6742 
6743 	if (IS_GEN9_LP(dev_priv) &&
6744 	    bxt_get_dsi_transcoder_state(crtc, pipe_config, &power_domain_set)) {
6745 		drm_WARN_ON(&dev_priv->drm, active);
6746 		active = true;
6747 	}
6748 
6749 	intel_dsc_get_config(pipe_config);
6750 
6751 	if (!active) {
6752 		/* bigjoiner slave doesn't enable transcoder */
6753 		if (!pipe_config->bigjoiner_slave)
6754 			goto out;
6755 
6756 		active = true;
6757 		pipe_config->pixel_multiplier = 1;
6758 
6759 		/* we cannot read out most state, so don't bother.. */
6760 		pipe_config->quirks |= PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE;
6761 	} else if (!transcoder_is_dsi(pipe_config->cpu_transcoder) ||
6762 	    INTEL_GEN(dev_priv) >= 11) {
6763 		hsw_get_ddi_port_state(crtc, pipe_config);
6764 		intel_get_transcoder_timings(crtc, pipe_config);
6765 	}
6766 
6767 	if (HAS_VRR(dev_priv) && !transcoder_is_dsi(pipe_config->cpu_transcoder))
6768 		intel_vrr_get_config(crtc, pipe_config);
6769 
6770 	intel_get_pipe_src_size(crtc, pipe_config);
6771 
6772 	if (IS_HASWELL(dev_priv)) {
6773 		u32 tmp = intel_de_read(dev_priv,
6774 					PIPECONF(pipe_config->cpu_transcoder));
6775 
6776 		if (tmp & PIPECONF_OUTPUT_COLORSPACE_YUV_HSW)
6777 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_YCBCR444;
6778 		else
6779 			pipe_config->output_format = INTEL_OUTPUT_FORMAT_RGB;
6780 	} else {
6781 		pipe_config->output_format =
6782 			bdw_get_pipemisc_output_format(crtc);
6783 	}
6784 
6785 	pipe_config->gamma_mode = intel_de_read(dev_priv,
6786 						GAMMA_MODE(crtc->pipe));
6787 
6788 	pipe_config->csc_mode = intel_de_read(dev_priv,
6789 					      PIPE_CSC_MODE(crtc->pipe));
6790 
6791 	if (INTEL_GEN(dev_priv) >= 9) {
6792 		tmp = intel_de_read(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe));
6793 
6794 		if (tmp & SKL_BOTTOM_COLOR_GAMMA_ENABLE)
6795 			pipe_config->gamma_enable = true;
6796 
6797 		if (tmp & SKL_BOTTOM_COLOR_CSC_ENABLE)
6798 			pipe_config->csc_enable = true;
6799 	} else {
6800 		i9xx_get_pipe_color_config(pipe_config);
6801 	}
6802 
6803 	intel_color_get_config(pipe_config);
6804 
6805 	tmp = intel_de_read(dev_priv, WM_LINETIME(crtc->pipe));
6806 	pipe_config->linetime = REG_FIELD_GET(HSW_LINETIME_MASK, tmp);
6807 	if (IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
6808 		pipe_config->ips_linetime =
6809 			REG_FIELD_GET(HSW_IPS_LINETIME_MASK, tmp);
6810 
6811 	if (intel_display_power_get_in_set_if_enabled(dev_priv, &power_domain_set,
6812 						      POWER_DOMAIN_PIPE_PANEL_FITTER(crtc->pipe))) {
6813 		if (INTEL_GEN(dev_priv) >= 9)
6814 			skl_get_pfit_config(pipe_config);
6815 		else
6816 			ilk_get_pfit_config(pipe_config);
6817 	}
6818 
6819 	if (hsw_crtc_supports_ips(crtc)) {
6820 		if (IS_HASWELL(dev_priv))
6821 			pipe_config->ips_enabled = intel_de_read(dev_priv,
6822 								 IPS_CTL) & IPS_ENABLE;
6823 		else {
6824 			/*
6825 			 * We cannot readout IPS state on broadwell, set to
6826 			 * true so we can set it to a defined state on first
6827 			 * commit.
6828 			 */
6829 			pipe_config->ips_enabled = true;
6830 		}
6831 	}
6832 
6833 	if (pipe_config->bigjoiner_slave) {
6834 		/* Cannot be read out as a slave, set to 0. */
6835 		pipe_config->pixel_multiplier = 0;
6836 	} else if (pipe_config->cpu_transcoder != TRANSCODER_EDP &&
6837 	    !transcoder_is_dsi(pipe_config->cpu_transcoder)) {
6838 		pipe_config->pixel_multiplier =
6839 			intel_de_read(dev_priv,
6840 				      PIPE_MULT(pipe_config->cpu_transcoder)) + 1;
6841 	} else {
6842 		pipe_config->pixel_multiplier = 1;
6843 	}
6844 
6845 out:
6846 	intel_display_power_put_all_in_set(dev_priv, &power_domain_set);
6847 
6848 	return active;
6849 }
6850 
6851 static bool intel_crtc_get_pipe_config(struct intel_crtc_state *crtc_state)
6852 {
6853 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
6854 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
6855 
6856 	if (!i915->display.get_pipe_config(crtc, crtc_state))
6857 		return false;
6858 
6859 	crtc_state->hw.active = true;
6860 
6861 	intel_crtc_readout_derived_state(crtc_state);
6862 
6863 	return true;
6864 }
6865 
6866 /* VESA 640x480x72Hz mode to set on the pipe */
6867 static const struct drm_display_mode load_detect_mode = {
6868 	DRM_MODE("640x480", DRM_MODE_TYPE_DEFAULT, 31500, 640, 664,
6869 		 704, 832, 0, 480, 489, 491, 520, 0, DRM_MODE_FLAG_NHSYNC | DRM_MODE_FLAG_NVSYNC),
6870 };
6871 
6872 struct drm_framebuffer *
6873 intel_framebuffer_create(struct drm_i915_gem_object *obj,
6874 			 struct drm_mode_fb_cmd2 *mode_cmd)
6875 {
6876 	struct intel_framebuffer *intel_fb;
6877 	int ret;
6878 
6879 	intel_fb = kzalloc(sizeof(*intel_fb), GFP_KERNEL);
6880 	if (!intel_fb)
6881 		return ERR_PTR(-ENOMEM);
6882 
6883 	ret = intel_framebuffer_init(intel_fb, obj, mode_cmd);
6884 	if (ret)
6885 		goto err;
6886 
6887 	return &intel_fb->base;
6888 
6889 err:
6890 	kfree(intel_fb);
6891 	return ERR_PTR(ret);
6892 }
6893 
6894 static int intel_modeset_disable_planes(struct drm_atomic_state *state,
6895 					struct drm_crtc *crtc)
6896 {
6897 	struct drm_plane *plane;
6898 	struct drm_plane_state *plane_state;
6899 	int ret, i;
6900 
6901 	ret = drm_atomic_add_affected_planes(state, crtc);
6902 	if (ret)
6903 		return ret;
6904 
6905 	for_each_new_plane_in_state(state, plane, plane_state, i) {
6906 		if (plane_state->crtc != crtc)
6907 			continue;
6908 
6909 		ret = drm_atomic_set_crtc_for_plane(plane_state, NULL);
6910 		if (ret)
6911 			return ret;
6912 
6913 		drm_atomic_set_fb_for_plane(plane_state, NULL);
6914 	}
6915 
6916 	return 0;
6917 }
6918 
6919 int intel_get_load_detect_pipe(struct drm_connector *connector,
6920 			       struct intel_load_detect_pipe *old,
6921 			       struct drm_modeset_acquire_ctx *ctx)
6922 {
6923 	struct intel_crtc *intel_crtc;
6924 	struct intel_encoder *intel_encoder =
6925 		intel_attached_encoder(to_intel_connector(connector));
6926 	struct drm_crtc *possible_crtc;
6927 	struct drm_encoder *encoder = &intel_encoder->base;
6928 	struct drm_crtc *crtc = NULL;
6929 	struct drm_device *dev = encoder->dev;
6930 	struct drm_i915_private *dev_priv = to_i915(dev);
6931 	struct drm_mode_config *config = &dev->mode_config;
6932 	struct drm_atomic_state *state = NULL, *restore_state = NULL;
6933 	struct drm_connector_state *connector_state;
6934 	struct intel_crtc_state *crtc_state;
6935 	int ret, i = -1;
6936 
6937 	drm_dbg_kms(&dev_priv->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
6938 		    connector->base.id, connector->name,
6939 		    encoder->base.id, encoder->name);
6940 
6941 	old->restore_state = NULL;
6942 
6943 	drm_WARN_ON(dev, !drm_modeset_is_locked(&config->connection_mutex));
6944 
6945 	/*
6946 	 * Algorithm gets a little messy:
6947 	 *
6948 	 *   - if the connector already has an assigned crtc, use it (but make
6949 	 *     sure it's on first)
6950 	 *
6951 	 *   - try to find the first unused crtc that can drive this connector,
6952 	 *     and use that if we find one
6953 	 */
6954 
6955 	/* See if we already have a CRTC for this connector */
6956 	if (connector->state->crtc) {
6957 		crtc = connector->state->crtc;
6958 
6959 		ret = drm_modeset_lock(&crtc->mutex, ctx);
6960 		if (ret)
6961 			goto fail;
6962 
6963 		/* Make sure the crtc and connector are running */
6964 		goto found;
6965 	}
6966 
6967 	/* Find an unused one (if possible) */
6968 	for_each_crtc(dev, possible_crtc) {
6969 		i++;
6970 		if (!(encoder->possible_crtcs & (1 << i)))
6971 			continue;
6972 
6973 		ret = drm_modeset_lock(&possible_crtc->mutex, ctx);
6974 		if (ret)
6975 			goto fail;
6976 
6977 		if (possible_crtc->state->enable) {
6978 			drm_modeset_unlock(&possible_crtc->mutex);
6979 			continue;
6980 		}
6981 
6982 		crtc = possible_crtc;
6983 		break;
6984 	}
6985 
6986 	/*
6987 	 * If we didn't find an unused CRTC, don't use any.
6988 	 */
6989 	if (!crtc) {
6990 		drm_dbg_kms(&dev_priv->drm,
6991 			    "no pipe available for load-detect\n");
6992 		ret = -ENODEV;
6993 		goto fail;
6994 	}
6995 
6996 found:
6997 	intel_crtc = to_intel_crtc(crtc);
6998 
6999 	state = drm_atomic_state_alloc(dev);
7000 	restore_state = drm_atomic_state_alloc(dev);
7001 	if (!state || !restore_state) {
7002 		ret = -ENOMEM;
7003 		goto fail;
7004 	}
7005 
7006 	state->acquire_ctx = ctx;
7007 	restore_state->acquire_ctx = ctx;
7008 
7009 	connector_state = drm_atomic_get_connector_state(state, connector);
7010 	if (IS_ERR(connector_state)) {
7011 		ret = PTR_ERR(connector_state);
7012 		goto fail;
7013 	}
7014 
7015 	ret = drm_atomic_set_crtc_for_connector(connector_state, crtc);
7016 	if (ret)
7017 		goto fail;
7018 
7019 	crtc_state = intel_atomic_get_crtc_state(state, intel_crtc);
7020 	if (IS_ERR(crtc_state)) {
7021 		ret = PTR_ERR(crtc_state);
7022 		goto fail;
7023 	}
7024 
7025 	crtc_state->uapi.active = true;
7026 
7027 	ret = drm_atomic_set_mode_for_crtc(&crtc_state->uapi,
7028 					   &load_detect_mode);
7029 	if (ret)
7030 		goto fail;
7031 
7032 	ret = intel_modeset_disable_planes(state, crtc);
7033 	if (ret)
7034 		goto fail;
7035 
7036 	ret = PTR_ERR_OR_ZERO(drm_atomic_get_connector_state(restore_state, connector));
7037 	if (!ret)
7038 		ret = PTR_ERR_OR_ZERO(drm_atomic_get_crtc_state(restore_state, crtc));
7039 	if (!ret)
7040 		ret = drm_atomic_add_affected_planes(restore_state, crtc);
7041 	if (ret) {
7042 		drm_dbg_kms(&dev_priv->drm,
7043 			    "Failed to create a copy of old state to restore: %i\n",
7044 			    ret);
7045 		goto fail;
7046 	}
7047 
7048 	ret = drm_atomic_commit(state);
7049 	if (ret) {
7050 		drm_dbg_kms(&dev_priv->drm,
7051 			    "failed to set mode on load-detect pipe\n");
7052 		goto fail;
7053 	}
7054 
7055 	old->restore_state = restore_state;
7056 	drm_atomic_state_put(state);
7057 
7058 	/* let the connector get through one full cycle before testing */
7059 	intel_wait_for_vblank(dev_priv, intel_crtc->pipe);
7060 	return true;
7061 
7062 fail:
7063 	if (state) {
7064 		drm_atomic_state_put(state);
7065 		state = NULL;
7066 	}
7067 	if (restore_state) {
7068 		drm_atomic_state_put(restore_state);
7069 		restore_state = NULL;
7070 	}
7071 
7072 	if (ret == -EDEADLK)
7073 		return ret;
7074 
7075 	return false;
7076 }
7077 
7078 void intel_release_load_detect_pipe(struct drm_connector *connector,
7079 				    struct intel_load_detect_pipe *old,
7080 				    struct drm_modeset_acquire_ctx *ctx)
7081 {
7082 	struct intel_encoder *intel_encoder =
7083 		intel_attached_encoder(to_intel_connector(connector));
7084 	struct drm_i915_private *i915 = to_i915(intel_encoder->base.dev);
7085 	struct drm_encoder *encoder = &intel_encoder->base;
7086 	struct drm_atomic_state *state = old->restore_state;
7087 	int ret;
7088 
7089 	drm_dbg_kms(&i915->drm, "[CONNECTOR:%d:%s], [ENCODER:%d:%s]\n",
7090 		    connector->base.id, connector->name,
7091 		    encoder->base.id, encoder->name);
7092 
7093 	if (!state)
7094 		return;
7095 
7096 	ret = drm_atomic_helper_commit_duplicated_state(state, ctx);
7097 	if (ret)
7098 		drm_dbg_kms(&i915->drm,
7099 			    "Couldn't release load detect pipe: %i\n", ret);
7100 	drm_atomic_state_put(state);
7101 }
7102 
7103 static int i9xx_pll_refclk(struct drm_device *dev,
7104 			   const struct intel_crtc_state *pipe_config)
7105 {
7106 	struct drm_i915_private *dev_priv = to_i915(dev);
7107 	u32 dpll = pipe_config->dpll_hw_state.dpll;
7108 
7109 	if ((dpll & PLL_REF_INPUT_MASK) == PLLB_REF_INPUT_SPREADSPECTRUMIN)
7110 		return dev_priv->vbt.lvds_ssc_freq;
7111 	else if (HAS_PCH_SPLIT(dev_priv))
7112 		return 120000;
7113 	else if (!IS_GEN(dev_priv, 2))
7114 		return 96000;
7115 	else
7116 		return 48000;
7117 }
7118 
7119 /* Returns the clock of the currently programmed mode of the given pipe. */
7120 static void i9xx_crtc_clock_get(struct intel_crtc *crtc,
7121 				struct intel_crtc_state *pipe_config)
7122 {
7123 	struct drm_device *dev = crtc->base.dev;
7124 	struct drm_i915_private *dev_priv = to_i915(dev);
7125 	enum pipe pipe = crtc->pipe;
7126 	u32 dpll = pipe_config->dpll_hw_state.dpll;
7127 	u32 fp;
7128 	struct dpll clock;
7129 	int port_clock;
7130 	int refclk = i9xx_pll_refclk(dev, pipe_config);
7131 
7132 	if ((dpll & DISPLAY_RATE_SELECT_FPA1) == 0)
7133 		fp = pipe_config->dpll_hw_state.fp0;
7134 	else
7135 		fp = pipe_config->dpll_hw_state.fp1;
7136 
7137 	clock.m1 = (fp & FP_M1_DIV_MASK) >> FP_M1_DIV_SHIFT;
7138 	if (IS_PINEVIEW(dev_priv)) {
7139 		clock.n = ffs((fp & FP_N_PINEVIEW_DIV_MASK) >> FP_N_DIV_SHIFT) - 1;
7140 		clock.m2 = (fp & FP_M2_PINEVIEW_DIV_MASK) >> FP_M2_DIV_SHIFT;
7141 	} else {
7142 		clock.n = (fp & FP_N_DIV_MASK) >> FP_N_DIV_SHIFT;
7143 		clock.m2 = (fp & FP_M2_DIV_MASK) >> FP_M2_DIV_SHIFT;
7144 	}
7145 
7146 	if (!IS_GEN(dev_priv, 2)) {
7147 		if (IS_PINEVIEW(dev_priv))
7148 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_PINEVIEW) >>
7149 				DPLL_FPA01_P1_POST_DIV_SHIFT_PINEVIEW);
7150 		else
7151 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK) >>
7152 			       DPLL_FPA01_P1_POST_DIV_SHIFT);
7153 
7154 		switch (dpll & DPLL_MODE_MASK) {
7155 		case DPLLB_MODE_DAC_SERIAL:
7156 			clock.p2 = dpll & DPLL_DAC_SERIAL_P2_CLOCK_DIV_5 ?
7157 				5 : 10;
7158 			break;
7159 		case DPLLB_MODE_LVDS:
7160 			clock.p2 = dpll & DPLLB_LVDS_P2_CLOCK_DIV_7 ?
7161 				7 : 14;
7162 			break;
7163 		default:
7164 			drm_dbg_kms(&dev_priv->drm,
7165 				    "Unknown DPLL mode %08x in programmed "
7166 				    "mode\n", (int)(dpll & DPLL_MODE_MASK));
7167 			return;
7168 		}
7169 
7170 		if (IS_PINEVIEW(dev_priv))
7171 			port_clock = pnv_calc_dpll_params(refclk, &clock);
7172 		else
7173 			port_clock = i9xx_calc_dpll_params(refclk, &clock);
7174 	} else {
7175 		u32 lvds = IS_I830(dev_priv) ? 0 : intel_de_read(dev_priv,
7176 								 LVDS);
7177 		bool is_lvds = (pipe == 1) && (lvds & LVDS_PORT_EN);
7178 
7179 		if (is_lvds) {
7180 			clock.p1 = ffs((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830_LVDS) >>
7181 				       DPLL_FPA01_P1_POST_DIV_SHIFT);
7182 
7183 			if (lvds & LVDS_CLKB_POWER_UP)
7184 				clock.p2 = 7;
7185 			else
7186 				clock.p2 = 14;
7187 		} else {
7188 			if (dpll & PLL_P1_DIVIDE_BY_TWO)
7189 				clock.p1 = 2;
7190 			else {
7191 				clock.p1 = ((dpll & DPLL_FPA01_P1_POST_DIV_MASK_I830) >>
7192 					    DPLL_FPA01_P1_POST_DIV_SHIFT) + 2;
7193 			}
7194 			if (dpll & PLL_P2_DIVIDE_BY_4)
7195 				clock.p2 = 4;
7196 			else
7197 				clock.p2 = 2;
7198 		}
7199 
7200 		port_clock = i9xx_calc_dpll_params(refclk, &clock);
7201 	}
7202 
7203 	/*
7204 	 * This value includes pixel_multiplier. We will use
7205 	 * port_clock to compute adjusted_mode.crtc_clock in the
7206 	 * encoder's get_config() function.
7207 	 */
7208 	pipe_config->port_clock = port_clock;
7209 }
7210 
7211 int intel_dotclock_calculate(int link_freq,
7212 			     const struct intel_link_m_n *m_n)
7213 {
7214 	/*
7215 	 * The calculation for the data clock is:
7216 	 * pixel_clock = ((m/n)*(link_clock * nr_lanes))/bpp
7217 	 * But we want to avoid losing precison if possible, so:
7218 	 * pixel_clock = ((m * link_clock * nr_lanes)/(n*bpp))
7219 	 *
7220 	 * and the link clock is simpler:
7221 	 * link_clock = (m * link_clock) / n
7222 	 */
7223 
7224 	if (!m_n->link_n)
7225 		return 0;
7226 
7227 	return div_u64(mul_u32_u32(m_n->link_m, link_freq), m_n->link_n);
7228 }
7229 
7230 static void ilk_pch_clock_get(struct intel_crtc *crtc,
7231 			      struct intel_crtc_state *pipe_config)
7232 {
7233 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7234 
7235 	/* read out port_clock from the DPLL */
7236 	i9xx_crtc_clock_get(crtc, pipe_config);
7237 
7238 	/*
7239 	 * In case there is an active pipe without active ports,
7240 	 * we may need some idea for the dotclock anyway.
7241 	 * Calculate one based on the FDI configuration.
7242 	 */
7243 	pipe_config->hw.adjusted_mode.crtc_clock =
7244 		intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
7245 					 &pipe_config->fdi_m_n);
7246 }
7247 
7248 /* Returns the currently programmed mode of the given encoder. */
7249 struct drm_display_mode *
7250 intel_encoder_current_mode(struct intel_encoder *encoder)
7251 {
7252 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
7253 	struct intel_crtc_state *crtc_state;
7254 	struct drm_display_mode *mode;
7255 	struct intel_crtc *crtc;
7256 	enum pipe pipe;
7257 
7258 	if (!encoder->get_hw_state(encoder, &pipe))
7259 		return NULL;
7260 
7261 	crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
7262 
7263 	mode = kzalloc(sizeof(*mode), GFP_KERNEL);
7264 	if (!mode)
7265 		return NULL;
7266 
7267 	crtc_state = intel_crtc_state_alloc(crtc);
7268 	if (!crtc_state) {
7269 		kfree(mode);
7270 		return NULL;
7271 	}
7272 
7273 	if (!intel_crtc_get_pipe_config(crtc_state)) {
7274 		kfree(crtc_state);
7275 		kfree(mode);
7276 		return NULL;
7277 	}
7278 
7279 	intel_encoder_get_config(encoder, crtc_state);
7280 
7281 	intel_mode_from_crtc_timings(mode, &crtc_state->hw.adjusted_mode);
7282 
7283 	kfree(crtc_state);
7284 
7285 	return mode;
7286 }
7287 
7288 /**
7289  * intel_wm_need_update - Check whether watermarks need updating
7290  * @cur: current plane state
7291  * @new: new plane state
7292  *
7293  * Check current plane state versus the new one to determine whether
7294  * watermarks need to be recalculated.
7295  *
7296  * Returns true or false.
7297  */
7298 static bool intel_wm_need_update(const struct intel_plane_state *cur,
7299 				 struct intel_plane_state *new)
7300 {
7301 	/* Update watermarks on tiling or size changes. */
7302 	if (new->uapi.visible != cur->uapi.visible)
7303 		return true;
7304 
7305 	if (!cur->hw.fb || !new->hw.fb)
7306 		return false;
7307 
7308 	if (cur->hw.fb->modifier != new->hw.fb->modifier ||
7309 	    cur->hw.rotation != new->hw.rotation ||
7310 	    drm_rect_width(&new->uapi.src) != drm_rect_width(&cur->uapi.src) ||
7311 	    drm_rect_height(&new->uapi.src) != drm_rect_height(&cur->uapi.src) ||
7312 	    drm_rect_width(&new->uapi.dst) != drm_rect_width(&cur->uapi.dst) ||
7313 	    drm_rect_height(&new->uapi.dst) != drm_rect_height(&cur->uapi.dst))
7314 		return true;
7315 
7316 	return false;
7317 }
7318 
7319 static bool needs_scaling(const struct intel_plane_state *state)
7320 {
7321 	int src_w = drm_rect_width(&state->uapi.src) >> 16;
7322 	int src_h = drm_rect_height(&state->uapi.src) >> 16;
7323 	int dst_w = drm_rect_width(&state->uapi.dst);
7324 	int dst_h = drm_rect_height(&state->uapi.dst);
7325 
7326 	return (src_w != dst_w || src_h != dst_h);
7327 }
7328 
7329 int intel_plane_atomic_calc_changes(const struct intel_crtc_state *old_crtc_state,
7330 				    struct intel_crtc_state *crtc_state,
7331 				    const struct intel_plane_state *old_plane_state,
7332 				    struct intel_plane_state *plane_state)
7333 {
7334 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7335 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
7336 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7337 	bool mode_changed = intel_crtc_needs_modeset(crtc_state);
7338 	bool was_crtc_enabled = old_crtc_state->hw.active;
7339 	bool is_crtc_enabled = crtc_state->hw.active;
7340 	bool turn_off, turn_on, visible, was_visible;
7341 	int ret;
7342 
7343 	if (INTEL_GEN(dev_priv) >= 9 && plane->id != PLANE_CURSOR) {
7344 		ret = skl_update_scaler_plane(crtc_state, plane_state);
7345 		if (ret)
7346 			return ret;
7347 	}
7348 
7349 	was_visible = old_plane_state->uapi.visible;
7350 	visible = plane_state->uapi.visible;
7351 
7352 	if (!was_crtc_enabled && drm_WARN_ON(&dev_priv->drm, was_visible))
7353 		was_visible = false;
7354 
7355 	/*
7356 	 * Visibility is calculated as if the crtc was on, but
7357 	 * after scaler setup everything depends on it being off
7358 	 * when the crtc isn't active.
7359 	 *
7360 	 * FIXME this is wrong for watermarks. Watermarks should also
7361 	 * be computed as if the pipe would be active. Perhaps move
7362 	 * per-plane wm computation to the .check_plane() hook, and
7363 	 * only combine the results from all planes in the current place?
7364 	 */
7365 	if (!is_crtc_enabled) {
7366 		intel_plane_set_invisible(crtc_state, plane_state);
7367 		visible = false;
7368 	}
7369 
7370 	if (!was_visible && !visible)
7371 		return 0;
7372 
7373 	turn_off = was_visible && (!visible || mode_changed);
7374 	turn_on = visible && (!was_visible || mode_changed);
7375 
7376 	drm_dbg_atomic(&dev_priv->drm,
7377 		       "[CRTC:%d:%s] with [PLANE:%d:%s] visible %i -> %i, off %i, on %i, ms %i\n",
7378 		       crtc->base.base.id, crtc->base.name,
7379 		       plane->base.base.id, plane->base.name,
7380 		       was_visible, visible,
7381 		       turn_off, turn_on, mode_changed);
7382 
7383 	if (turn_on) {
7384 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
7385 			crtc_state->update_wm_pre = true;
7386 
7387 		/* must disable cxsr around plane enable/disable */
7388 		if (plane->id != PLANE_CURSOR)
7389 			crtc_state->disable_cxsr = true;
7390 	} else if (turn_off) {
7391 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv))
7392 			crtc_state->update_wm_post = true;
7393 
7394 		/* must disable cxsr around plane enable/disable */
7395 		if (plane->id != PLANE_CURSOR)
7396 			crtc_state->disable_cxsr = true;
7397 	} else if (intel_wm_need_update(old_plane_state, plane_state)) {
7398 		if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv)) {
7399 			/* FIXME bollocks */
7400 			crtc_state->update_wm_pre = true;
7401 			crtc_state->update_wm_post = true;
7402 		}
7403 	}
7404 
7405 	if (visible || was_visible)
7406 		crtc_state->fb_bits |= plane->frontbuffer_bit;
7407 
7408 	/*
7409 	 * ILK/SNB DVSACNTR/Sprite Enable
7410 	 * IVB SPR_CTL/Sprite Enable
7411 	 * "When in Self Refresh Big FIFO mode, a write to enable the
7412 	 *  plane will be internally buffered and delayed while Big FIFO
7413 	 *  mode is exiting."
7414 	 *
7415 	 * Which means that enabling the sprite can take an extra frame
7416 	 * when we start in big FIFO mode (LP1+). Thus we need to drop
7417 	 * down to LP0 and wait for vblank in order to make sure the
7418 	 * sprite gets enabled on the next vblank after the register write.
7419 	 * Doing otherwise would risk enabling the sprite one frame after
7420 	 * we've already signalled flip completion. We can resume LP1+
7421 	 * once the sprite has been enabled.
7422 	 *
7423 	 *
7424 	 * WaCxSRDisabledForSpriteScaling:ivb
7425 	 * IVB SPR_SCALE/Scaling Enable
7426 	 * "Low Power watermarks must be disabled for at least one
7427 	 *  frame before enabling sprite scaling, and kept disabled
7428 	 *  until sprite scaling is disabled."
7429 	 *
7430 	 * ILK/SNB DVSASCALE/Scaling Enable
7431 	 * "When in Self Refresh Big FIFO mode, scaling enable will be
7432 	 *  masked off while Big FIFO mode is exiting."
7433 	 *
7434 	 * Despite the w/a only being listed for IVB we assume that
7435 	 * the ILK/SNB note has similar ramifications, hence we apply
7436 	 * the w/a on all three platforms.
7437 	 *
7438 	 * With experimental results seems this is needed also for primary
7439 	 * plane, not only sprite plane.
7440 	 */
7441 	if (plane->id != PLANE_CURSOR &&
7442 	    (IS_GEN_RANGE(dev_priv, 5, 6) ||
7443 	     IS_IVYBRIDGE(dev_priv)) &&
7444 	    (turn_on || (!needs_scaling(old_plane_state) &&
7445 			 needs_scaling(plane_state))))
7446 		crtc_state->disable_lp_wm = true;
7447 
7448 	return 0;
7449 }
7450 
7451 static bool encoders_cloneable(const struct intel_encoder *a,
7452 			       const struct intel_encoder *b)
7453 {
7454 	/* masks could be asymmetric, so check both ways */
7455 	return a == b || (a->cloneable & (1 << b->type) &&
7456 			  b->cloneable & (1 << a->type));
7457 }
7458 
7459 static bool check_single_encoder_cloning(struct intel_atomic_state *state,
7460 					 struct intel_crtc *crtc,
7461 					 struct intel_encoder *encoder)
7462 {
7463 	struct intel_encoder *source_encoder;
7464 	struct drm_connector *connector;
7465 	struct drm_connector_state *connector_state;
7466 	int i;
7467 
7468 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
7469 		if (connector_state->crtc != &crtc->base)
7470 			continue;
7471 
7472 		source_encoder =
7473 			to_intel_encoder(connector_state->best_encoder);
7474 		if (!encoders_cloneable(encoder, source_encoder))
7475 			return false;
7476 	}
7477 
7478 	return true;
7479 }
7480 
7481 static int icl_add_linked_planes(struct intel_atomic_state *state)
7482 {
7483 	struct intel_plane *plane, *linked;
7484 	struct intel_plane_state *plane_state, *linked_plane_state;
7485 	int i;
7486 
7487 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7488 		linked = plane_state->planar_linked_plane;
7489 
7490 		if (!linked)
7491 			continue;
7492 
7493 		linked_plane_state = intel_atomic_get_plane_state(state, linked);
7494 		if (IS_ERR(linked_plane_state))
7495 			return PTR_ERR(linked_plane_state);
7496 
7497 		drm_WARN_ON(state->base.dev,
7498 			    linked_plane_state->planar_linked_plane != plane);
7499 		drm_WARN_ON(state->base.dev,
7500 			    linked_plane_state->planar_slave == plane_state->planar_slave);
7501 	}
7502 
7503 	return 0;
7504 }
7505 
7506 static int icl_check_nv12_planes(struct intel_crtc_state *crtc_state)
7507 {
7508 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7509 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7510 	struct intel_atomic_state *state = to_intel_atomic_state(crtc_state->uapi.state);
7511 	struct intel_plane *plane, *linked;
7512 	struct intel_plane_state *plane_state;
7513 	int i;
7514 
7515 	if (INTEL_GEN(dev_priv) < 11)
7516 		return 0;
7517 
7518 	/*
7519 	 * Destroy all old plane links and make the slave plane invisible
7520 	 * in the crtc_state->active_planes mask.
7521 	 */
7522 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7523 		if (plane->pipe != crtc->pipe || !plane_state->planar_linked_plane)
7524 			continue;
7525 
7526 		plane_state->planar_linked_plane = NULL;
7527 		if (plane_state->planar_slave && !plane_state->uapi.visible) {
7528 			crtc_state->enabled_planes &= ~BIT(plane->id);
7529 			crtc_state->active_planes &= ~BIT(plane->id);
7530 			crtc_state->update_planes |= BIT(plane->id);
7531 		}
7532 
7533 		plane_state->planar_slave = false;
7534 	}
7535 
7536 	if (!crtc_state->nv12_planes)
7537 		return 0;
7538 
7539 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
7540 		struct intel_plane_state *linked_state = NULL;
7541 
7542 		if (plane->pipe != crtc->pipe ||
7543 		    !(crtc_state->nv12_planes & BIT(plane->id)))
7544 			continue;
7545 
7546 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, linked) {
7547 			if (!icl_is_nv12_y_plane(dev_priv, linked->id))
7548 				continue;
7549 
7550 			if (crtc_state->active_planes & BIT(linked->id))
7551 				continue;
7552 
7553 			linked_state = intel_atomic_get_plane_state(state, linked);
7554 			if (IS_ERR(linked_state))
7555 				return PTR_ERR(linked_state);
7556 
7557 			break;
7558 		}
7559 
7560 		if (!linked_state) {
7561 			drm_dbg_kms(&dev_priv->drm,
7562 				    "Need %d free Y planes for planar YUV\n",
7563 				    hweight8(crtc_state->nv12_planes));
7564 
7565 			return -EINVAL;
7566 		}
7567 
7568 		plane_state->planar_linked_plane = linked;
7569 
7570 		linked_state->planar_slave = true;
7571 		linked_state->planar_linked_plane = plane;
7572 		crtc_state->enabled_planes |= BIT(linked->id);
7573 		crtc_state->active_planes |= BIT(linked->id);
7574 		crtc_state->update_planes |= BIT(linked->id);
7575 		drm_dbg_kms(&dev_priv->drm, "Using %s as Y plane for %s\n",
7576 			    linked->base.name, plane->base.name);
7577 
7578 		/* Copy parameters to slave plane */
7579 		linked_state->ctl = plane_state->ctl | PLANE_CTL_YUV420_Y_PLANE;
7580 		linked_state->color_ctl = plane_state->color_ctl;
7581 		linked_state->view = plane_state->view;
7582 		memcpy(linked_state->color_plane, plane_state->color_plane,
7583 		       sizeof(linked_state->color_plane));
7584 
7585 		intel_plane_copy_hw_state(linked_state, plane_state);
7586 		linked_state->uapi.src = plane_state->uapi.src;
7587 		linked_state->uapi.dst = plane_state->uapi.dst;
7588 
7589 		if (icl_is_hdr_plane(dev_priv, plane->id)) {
7590 			if (linked->id == PLANE_SPRITE5)
7591 				plane_state->cus_ctl |= PLANE_CUS_PLANE_7;
7592 			else if (linked->id == PLANE_SPRITE4)
7593 				plane_state->cus_ctl |= PLANE_CUS_PLANE_6;
7594 			else if (linked->id == PLANE_SPRITE3)
7595 				plane_state->cus_ctl |= PLANE_CUS_PLANE_5_RKL;
7596 			else if (linked->id == PLANE_SPRITE2)
7597 				plane_state->cus_ctl |= PLANE_CUS_PLANE_4_RKL;
7598 			else
7599 				MISSING_CASE(linked->id);
7600 		}
7601 	}
7602 
7603 	return 0;
7604 }
7605 
7606 static bool c8_planes_changed(const struct intel_crtc_state *new_crtc_state)
7607 {
7608 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
7609 	struct intel_atomic_state *state =
7610 		to_intel_atomic_state(new_crtc_state->uapi.state);
7611 	const struct intel_crtc_state *old_crtc_state =
7612 		intel_atomic_get_old_crtc_state(state, crtc);
7613 
7614 	return !old_crtc_state->c8_planes != !new_crtc_state->c8_planes;
7615 }
7616 
7617 static u16 hsw_linetime_wm(const struct intel_crtc_state *crtc_state)
7618 {
7619 	const struct drm_display_mode *pipe_mode =
7620 		&crtc_state->hw.pipe_mode;
7621 	int linetime_wm;
7622 
7623 	if (!crtc_state->hw.enable)
7624 		return 0;
7625 
7626 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
7627 					pipe_mode->crtc_clock);
7628 
7629 	return min(linetime_wm, 0x1ff);
7630 }
7631 
7632 static u16 hsw_ips_linetime_wm(const struct intel_crtc_state *crtc_state,
7633 			       const struct intel_cdclk_state *cdclk_state)
7634 {
7635 	const struct drm_display_mode *pipe_mode =
7636 		&crtc_state->hw.pipe_mode;
7637 	int linetime_wm;
7638 
7639 	if (!crtc_state->hw.enable)
7640 		return 0;
7641 
7642 	linetime_wm = DIV_ROUND_CLOSEST(pipe_mode->crtc_htotal * 1000 * 8,
7643 					cdclk_state->logical.cdclk);
7644 
7645 	return min(linetime_wm, 0x1ff);
7646 }
7647 
7648 static u16 skl_linetime_wm(const struct intel_crtc_state *crtc_state)
7649 {
7650 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
7651 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7652 	const struct drm_display_mode *pipe_mode =
7653 		&crtc_state->hw.pipe_mode;
7654 	int linetime_wm;
7655 
7656 	if (!crtc_state->hw.enable)
7657 		return 0;
7658 
7659 	linetime_wm = DIV_ROUND_UP(pipe_mode->crtc_htotal * 1000 * 8,
7660 				   crtc_state->pixel_rate);
7661 
7662 	/* Display WA #1135: BXT:ALL GLK:ALL */
7663 	if (IS_GEN9_LP(dev_priv) && dev_priv->ipc_enabled)
7664 		linetime_wm /= 2;
7665 
7666 	return min(linetime_wm, 0x1ff);
7667 }
7668 
7669 static int hsw_compute_linetime_wm(struct intel_atomic_state *state,
7670 				   struct intel_crtc *crtc)
7671 {
7672 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7673 	struct intel_crtc_state *crtc_state =
7674 		intel_atomic_get_new_crtc_state(state, crtc);
7675 	const struct intel_cdclk_state *cdclk_state;
7676 
7677 	if (INTEL_GEN(dev_priv) >= 9)
7678 		crtc_state->linetime = skl_linetime_wm(crtc_state);
7679 	else
7680 		crtc_state->linetime = hsw_linetime_wm(crtc_state);
7681 
7682 	if (!hsw_crtc_supports_ips(crtc))
7683 		return 0;
7684 
7685 	cdclk_state = intel_atomic_get_cdclk_state(state);
7686 	if (IS_ERR(cdclk_state))
7687 		return PTR_ERR(cdclk_state);
7688 
7689 	crtc_state->ips_linetime = hsw_ips_linetime_wm(crtc_state,
7690 						       cdclk_state);
7691 
7692 	return 0;
7693 }
7694 
7695 static int intel_crtc_atomic_check(struct intel_atomic_state *state,
7696 				   struct intel_crtc *crtc)
7697 {
7698 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7699 	struct intel_crtc_state *crtc_state =
7700 		intel_atomic_get_new_crtc_state(state, crtc);
7701 	bool mode_changed = intel_crtc_needs_modeset(crtc_state);
7702 	int ret;
7703 
7704 	if (INTEL_GEN(dev_priv) < 5 && !IS_G4X(dev_priv) &&
7705 	    mode_changed && !crtc_state->hw.active)
7706 		crtc_state->update_wm_post = true;
7707 
7708 	if (mode_changed && crtc_state->hw.enable &&
7709 	    dev_priv->display.crtc_compute_clock &&
7710 	    !crtc_state->bigjoiner_slave &&
7711 	    !drm_WARN_ON(&dev_priv->drm, crtc_state->shared_dpll)) {
7712 		ret = dev_priv->display.crtc_compute_clock(crtc, crtc_state);
7713 		if (ret)
7714 			return ret;
7715 	}
7716 
7717 	/*
7718 	 * May need to update pipe gamma enable bits
7719 	 * when C8 planes are getting enabled/disabled.
7720 	 */
7721 	if (c8_planes_changed(crtc_state))
7722 		crtc_state->uapi.color_mgmt_changed = true;
7723 
7724 	if (mode_changed || crtc_state->update_pipe ||
7725 	    crtc_state->uapi.color_mgmt_changed) {
7726 		ret = intel_color_check(crtc_state);
7727 		if (ret)
7728 			return ret;
7729 	}
7730 
7731 	if (dev_priv->display.compute_pipe_wm) {
7732 		ret = dev_priv->display.compute_pipe_wm(crtc_state);
7733 		if (ret) {
7734 			drm_dbg_kms(&dev_priv->drm,
7735 				    "Target pipe watermarks are invalid\n");
7736 			return ret;
7737 		}
7738 	}
7739 
7740 	if (dev_priv->display.compute_intermediate_wm) {
7741 		if (drm_WARN_ON(&dev_priv->drm,
7742 				!dev_priv->display.compute_pipe_wm))
7743 			return 0;
7744 
7745 		/*
7746 		 * Calculate 'intermediate' watermarks that satisfy both the
7747 		 * old state and the new state.  We can program these
7748 		 * immediately.
7749 		 */
7750 		ret = dev_priv->display.compute_intermediate_wm(crtc_state);
7751 		if (ret) {
7752 			drm_dbg_kms(&dev_priv->drm,
7753 				    "No valid intermediate pipe watermarks are possible\n");
7754 			return ret;
7755 		}
7756 	}
7757 
7758 	if (INTEL_GEN(dev_priv) >= 9) {
7759 		if (mode_changed || crtc_state->update_pipe) {
7760 			ret = skl_update_scaler_crtc(crtc_state);
7761 			if (ret)
7762 				return ret;
7763 		}
7764 
7765 		ret = intel_atomic_setup_scalers(dev_priv, crtc, crtc_state);
7766 		if (ret)
7767 			return ret;
7768 	}
7769 
7770 	if (HAS_IPS(dev_priv)) {
7771 		ret = hsw_compute_ips_config(crtc_state);
7772 		if (ret)
7773 			return ret;
7774 	}
7775 
7776 	if (INTEL_GEN(dev_priv) >= 9 ||
7777 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
7778 		ret = hsw_compute_linetime_wm(state, crtc);
7779 		if (ret)
7780 			return ret;
7781 
7782 	}
7783 
7784 	if (!mode_changed) {
7785 		ret = intel_psr2_sel_fetch_update(state, crtc);
7786 		if (ret)
7787 			return ret;
7788 	}
7789 
7790 	return 0;
7791 }
7792 
7793 static void intel_modeset_update_connector_atomic_state(struct drm_device *dev)
7794 {
7795 	struct intel_connector *connector;
7796 	struct drm_connector_list_iter conn_iter;
7797 
7798 	drm_connector_list_iter_begin(dev, &conn_iter);
7799 	for_each_intel_connector_iter(connector, &conn_iter) {
7800 		struct drm_connector_state *conn_state = connector->base.state;
7801 		struct intel_encoder *encoder =
7802 			to_intel_encoder(connector->base.encoder);
7803 
7804 		if (conn_state->crtc)
7805 			drm_connector_put(&connector->base);
7806 
7807 		if (encoder) {
7808 			struct intel_crtc *crtc =
7809 				to_intel_crtc(encoder->base.crtc);
7810 			const struct intel_crtc_state *crtc_state =
7811 				to_intel_crtc_state(crtc->base.state);
7812 
7813 			conn_state->best_encoder = &encoder->base;
7814 			conn_state->crtc = &crtc->base;
7815 			conn_state->max_bpc = (crtc_state->pipe_bpp ?: 24) / 3;
7816 
7817 			drm_connector_get(&connector->base);
7818 		} else {
7819 			conn_state->best_encoder = NULL;
7820 			conn_state->crtc = NULL;
7821 		}
7822 	}
7823 	drm_connector_list_iter_end(&conn_iter);
7824 }
7825 
7826 static int
7827 compute_sink_pipe_bpp(const struct drm_connector_state *conn_state,
7828 		      struct intel_crtc_state *pipe_config)
7829 {
7830 	struct drm_connector *connector = conn_state->connector;
7831 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7832 	const struct drm_display_info *info = &connector->display_info;
7833 	int bpp;
7834 
7835 	switch (conn_state->max_bpc) {
7836 	case 6 ... 7:
7837 		bpp = 6 * 3;
7838 		break;
7839 	case 8 ... 9:
7840 		bpp = 8 * 3;
7841 		break;
7842 	case 10 ... 11:
7843 		bpp = 10 * 3;
7844 		break;
7845 	case 12 ... 16:
7846 		bpp = 12 * 3;
7847 		break;
7848 	default:
7849 		MISSING_CASE(conn_state->max_bpc);
7850 		return -EINVAL;
7851 	}
7852 
7853 	if (bpp < pipe_config->pipe_bpp) {
7854 		drm_dbg_kms(&i915->drm,
7855 			    "[CONNECTOR:%d:%s] Limiting display bpp to %d instead of "
7856 			    "EDID bpp %d, requested bpp %d, max platform bpp %d\n",
7857 			    connector->base.id, connector->name,
7858 			    bpp, 3 * info->bpc,
7859 			    3 * conn_state->max_requested_bpc,
7860 			    pipe_config->pipe_bpp);
7861 
7862 		pipe_config->pipe_bpp = bpp;
7863 	}
7864 
7865 	return 0;
7866 }
7867 
7868 static int
7869 compute_baseline_pipe_bpp(struct intel_crtc *crtc,
7870 			  struct intel_crtc_state *pipe_config)
7871 {
7872 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
7873 	struct drm_atomic_state *state = pipe_config->uapi.state;
7874 	struct drm_connector *connector;
7875 	struct drm_connector_state *connector_state;
7876 	int bpp, i;
7877 
7878 	if ((IS_G4X(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
7879 	    IS_CHERRYVIEW(dev_priv)))
7880 		bpp = 10*3;
7881 	else if (INTEL_GEN(dev_priv) >= 5)
7882 		bpp = 12*3;
7883 	else
7884 		bpp = 8*3;
7885 
7886 	pipe_config->pipe_bpp = bpp;
7887 
7888 	/* Clamp display bpp to connector max bpp */
7889 	for_each_new_connector_in_state(state, connector, connector_state, i) {
7890 		int ret;
7891 
7892 		if (connector_state->crtc != &crtc->base)
7893 			continue;
7894 
7895 		ret = compute_sink_pipe_bpp(connector_state, pipe_config);
7896 		if (ret)
7897 			return ret;
7898 	}
7899 
7900 	return 0;
7901 }
7902 
7903 static void intel_dump_crtc_timings(struct drm_i915_private *i915,
7904 				    const struct drm_display_mode *mode)
7905 {
7906 	drm_dbg_kms(&i915->drm, "crtc timings: %d %d %d %d %d %d %d %d %d, "
7907 		    "type: 0x%x flags: 0x%x\n",
7908 		    mode->crtc_clock,
7909 		    mode->crtc_hdisplay, mode->crtc_hsync_start,
7910 		    mode->crtc_hsync_end, mode->crtc_htotal,
7911 		    mode->crtc_vdisplay, mode->crtc_vsync_start,
7912 		    mode->crtc_vsync_end, mode->crtc_vtotal,
7913 		    mode->type, mode->flags);
7914 }
7915 
7916 static void
7917 intel_dump_m_n_config(const struct intel_crtc_state *pipe_config,
7918 		      const char *id, unsigned int lane_count,
7919 		      const struct intel_link_m_n *m_n)
7920 {
7921 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
7922 
7923 	drm_dbg_kms(&i915->drm,
7924 		    "%s: lanes: %i; gmch_m: %u, gmch_n: %u, link_m: %u, link_n: %u, tu: %u\n",
7925 		    id, lane_count,
7926 		    m_n->gmch_m, m_n->gmch_n,
7927 		    m_n->link_m, m_n->link_n, m_n->tu);
7928 }
7929 
7930 static void
7931 intel_dump_infoframe(struct drm_i915_private *dev_priv,
7932 		     const union hdmi_infoframe *frame)
7933 {
7934 	if (!drm_debug_enabled(DRM_UT_KMS))
7935 		return;
7936 
7937 	hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, frame);
7938 }
7939 
7940 static void
7941 intel_dump_dp_vsc_sdp(struct drm_i915_private *dev_priv,
7942 		      const struct drm_dp_vsc_sdp *vsc)
7943 {
7944 	if (!drm_debug_enabled(DRM_UT_KMS))
7945 		return;
7946 
7947 	drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, vsc);
7948 }
7949 
7950 #define OUTPUT_TYPE(x) [INTEL_OUTPUT_ ## x] = #x
7951 
7952 static const char * const output_type_str[] = {
7953 	OUTPUT_TYPE(UNUSED),
7954 	OUTPUT_TYPE(ANALOG),
7955 	OUTPUT_TYPE(DVO),
7956 	OUTPUT_TYPE(SDVO),
7957 	OUTPUT_TYPE(LVDS),
7958 	OUTPUT_TYPE(TVOUT),
7959 	OUTPUT_TYPE(HDMI),
7960 	OUTPUT_TYPE(DP),
7961 	OUTPUT_TYPE(EDP),
7962 	OUTPUT_TYPE(DSI),
7963 	OUTPUT_TYPE(DDI),
7964 	OUTPUT_TYPE(DP_MST),
7965 };
7966 
7967 #undef OUTPUT_TYPE
7968 
7969 static void snprintf_output_types(char *buf, size_t len,
7970 				  unsigned int output_types)
7971 {
7972 	char *str = buf;
7973 	int i;
7974 
7975 	str[0] = '\0';
7976 
7977 	for (i = 0; i < ARRAY_SIZE(output_type_str); i++) {
7978 		int r;
7979 
7980 		if ((output_types & BIT(i)) == 0)
7981 			continue;
7982 
7983 		r = snprintf(str, len, "%s%s",
7984 			     str != buf ? "," : "", output_type_str[i]);
7985 		if (r >= len)
7986 			break;
7987 		str += r;
7988 		len -= r;
7989 
7990 		output_types &= ~BIT(i);
7991 	}
7992 
7993 	WARN_ON_ONCE(output_types != 0);
7994 }
7995 
7996 static const char * const output_format_str[] = {
7997 	[INTEL_OUTPUT_FORMAT_RGB] = "RGB",
7998 	[INTEL_OUTPUT_FORMAT_YCBCR420] = "YCBCR4:2:0",
7999 	[INTEL_OUTPUT_FORMAT_YCBCR444] = "YCBCR4:4:4",
8000 };
8001 
8002 static const char *output_formats(enum intel_output_format format)
8003 {
8004 	if (format >= ARRAY_SIZE(output_format_str))
8005 		return "invalid";
8006 	return output_format_str[format];
8007 }
8008 
8009 static void intel_dump_plane_state(const struct intel_plane_state *plane_state)
8010 {
8011 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
8012 	struct drm_i915_private *i915 = to_i915(plane->base.dev);
8013 	const struct drm_framebuffer *fb = plane_state->hw.fb;
8014 
8015 	if (!fb) {
8016 		drm_dbg_kms(&i915->drm,
8017 			    "[PLANE:%d:%s] fb: [NOFB], visible: %s\n",
8018 			    plane->base.base.id, plane->base.name,
8019 			    yesno(plane_state->uapi.visible));
8020 		return;
8021 	}
8022 
8023 	drm_dbg_kms(&i915->drm,
8024 		    "[PLANE:%d:%s] fb: [FB:%d] %ux%u format = %p4cc modifier = 0x%llx, visible: %s\n",
8025 		    plane->base.base.id, plane->base.name,
8026 		    fb->base.id, fb->width, fb->height, &fb->format->format,
8027 		    fb->modifier, yesno(plane_state->uapi.visible));
8028 	drm_dbg_kms(&i915->drm, "\trotation: 0x%x, scaler: %d\n",
8029 		    plane_state->hw.rotation, plane_state->scaler_id);
8030 	if (plane_state->uapi.visible)
8031 		drm_dbg_kms(&i915->drm,
8032 			    "\tsrc: " DRM_RECT_FP_FMT " dst: " DRM_RECT_FMT "\n",
8033 			    DRM_RECT_FP_ARG(&plane_state->uapi.src),
8034 			    DRM_RECT_ARG(&plane_state->uapi.dst));
8035 }
8036 
8037 static void intel_dump_pipe_config(const struct intel_crtc_state *pipe_config,
8038 				   struct intel_atomic_state *state,
8039 				   const char *context)
8040 {
8041 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
8042 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8043 	const struct intel_plane_state *plane_state;
8044 	struct intel_plane *plane;
8045 	char buf[64];
8046 	int i;
8047 
8048 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s] enable: %s %s\n",
8049 		    crtc->base.base.id, crtc->base.name,
8050 		    yesno(pipe_config->hw.enable), context);
8051 
8052 	if (!pipe_config->hw.enable)
8053 		goto dump_planes;
8054 
8055 	snprintf_output_types(buf, sizeof(buf), pipe_config->output_types);
8056 	drm_dbg_kms(&dev_priv->drm,
8057 		    "active: %s, output_types: %s (0x%x), output format: %s\n",
8058 		    yesno(pipe_config->hw.active),
8059 		    buf, pipe_config->output_types,
8060 		    output_formats(pipe_config->output_format));
8061 
8062 	drm_dbg_kms(&dev_priv->drm,
8063 		    "cpu_transcoder: %s, pipe bpp: %i, dithering: %i\n",
8064 		    transcoder_name(pipe_config->cpu_transcoder),
8065 		    pipe_config->pipe_bpp, pipe_config->dither);
8066 
8067 	drm_dbg_kms(&dev_priv->drm, "MST master transcoder: %s\n",
8068 		    transcoder_name(pipe_config->mst_master_transcoder));
8069 
8070 	drm_dbg_kms(&dev_priv->drm,
8071 		    "port sync: master transcoder: %s, slave transcoder bitmask = 0x%x\n",
8072 		    transcoder_name(pipe_config->master_transcoder),
8073 		    pipe_config->sync_mode_slaves_mask);
8074 
8075 	drm_dbg_kms(&dev_priv->drm, "bigjoiner: %s\n",
8076 		    pipe_config->bigjoiner_slave ? "slave" :
8077 		    pipe_config->bigjoiner ? "master" : "no");
8078 
8079 	drm_dbg_kms(&dev_priv->drm, "splitter: %s, link count %d, overlap %d\n",
8080 		    enableddisabled(pipe_config->splitter.enable),
8081 		    pipe_config->splitter.link_count,
8082 		    pipe_config->splitter.pixel_overlap);
8083 
8084 	if (pipe_config->has_pch_encoder)
8085 		intel_dump_m_n_config(pipe_config, "fdi",
8086 				      pipe_config->fdi_lanes,
8087 				      &pipe_config->fdi_m_n);
8088 
8089 	if (intel_crtc_has_dp_encoder(pipe_config)) {
8090 		intel_dump_m_n_config(pipe_config, "dp m_n",
8091 				pipe_config->lane_count, &pipe_config->dp_m_n);
8092 		if (pipe_config->has_drrs)
8093 			intel_dump_m_n_config(pipe_config, "dp m2_n2",
8094 					      pipe_config->lane_count,
8095 					      &pipe_config->dp_m2_n2);
8096 	}
8097 
8098 	drm_dbg_kms(&dev_priv->drm,
8099 		    "audio: %i, infoframes: %i, infoframes enabled: 0x%x\n",
8100 		    pipe_config->has_audio, pipe_config->has_infoframe,
8101 		    pipe_config->infoframes.enable);
8102 
8103 	if (pipe_config->infoframes.enable &
8104 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GENERAL_CONTROL))
8105 		drm_dbg_kms(&dev_priv->drm, "GCP: 0x%x\n",
8106 			    pipe_config->infoframes.gcp);
8107 	if (pipe_config->infoframes.enable &
8108 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_AVI))
8109 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.avi);
8110 	if (pipe_config->infoframes.enable &
8111 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_SPD))
8112 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.spd);
8113 	if (pipe_config->infoframes.enable &
8114 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_VENDOR))
8115 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.hdmi);
8116 	if (pipe_config->infoframes.enable &
8117 	    intel_hdmi_infoframe_enable(HDMI_INFOFRAME_TYPE_DRM))
8118 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
8119 	if (pipe_config->infoframes.enable &
8120 	    intel_hdmi_infoframe_enable(HDMI_PACKET_TYPE_GAMUT_METADATA))
8121 		intel_dump_infoframe(dev_priv, &pipe_config->infoframes.drm);
8122 	if (pipe_config->infoframes.enable &
8123 	    intel_hdmi_infoframe_enable(DP_SDP_VSC))
8124 		intel_dump_dp_vsc_sdp(dev_priv, &pipe_config->infoframes.vsc);
8125 
8126 	drm_dbg_kms(&dev_priv->drm, "vrr: %s, vmin: %d, vmax: %d, pipeline full: %d, flipline: %d, vmin vblank: %d, vmax vblank: %d\n",
8127 		    yesno(pipe_config->vrr.enable),
8128 		    pipe_config->vrr.vmin, pipe_config->vrr.vmax,
8129 		    pipe_config->vrr.pipeline_full, pipe_config->vrr.flipline,
8130 		    intel_vrr_vmin_vblank_start(pipe_config),
8131 		    intel_vrr_vmax_vblank_start(pipe_config));
8132 
8133 	drm_dbg_kms(&dev_priv->drm, "requested mode:\n");
8134 	drm_mode_debug_printmodeline(&pipe_config->hw.mode);
8135 	drm_dbg_kms(&dev_priv->drm, "adjusted mode:\n");
8136 	drm_mode_debug_printmodeline(&pipe_config->hw.adjusted_mode);
8137 	intel_dump_crtc_timings(dev_priv, &pipe_config->hw.adjusted_mode);
8138 	drm_dbg_kms(&dev_priv->drm, "pipe mode:\n");
8139 	drm_mode_debug_printmodeline(&pipe_config->hw.pipe_mode);
8140 	intel_dump_crtc_timings(dev_priv, &pipe_config->hw.pipe_mode);
8141 	drm_dbg_kms(&dev_priv->drm,
8142 		    "port clock: %d, pipe src size: %dx%d, pixel rate %d\n",
8143 		    pipe_config->port_clock,
8144 		    pipe_config->pipe_src_w, pipe_config->pipe_src_h,
8145 		    pipe_config->pixel_rate);
8146 
8147 	drm_dbg_kms(&dev_priv->drm, "linetime: %d, ips linetime: %d\n",
8148 		    pipe_config->linetime, pipe_config->ips_linetime);
8149 
8150 	if (INTEL_GEN(dev_priv) >= 9)
8151 		drm_dbg_kms(&dev_priv->drm,
8152 			    "num_scalers: %d, scaler_users: 0x%x, scaler_id: %d\n",
8153 			    crtc->num_scalers,
8154 			    pipe_config->scaler_state.scaler_users,
8155 			    pipe_config->scaler_state.scaler_id);
8156 
8157 	if (HAS_GMCH(dev_priv))
8158 		drm_dbg_kms(&dev_priv->drm,
8159 			    "gmch pfit: control: 0x%08x, ratios: 0x%08x, lvds border: 0x%08x\n",
8160 			    pipe_config->gmch_pfit.control,
8161 			    pipe_config->gmch_pfit.pgm_ratios,
8162 			    pipe_config->gmch_pfit.lvds_border_bits);
8163 	else
8164 		drm_dbg_kms(&dev_priv->drm,
8165 			    "pch pfit: " DRM_RECT_FMT ", %s, force thru: %s\n",
8166 			    DRM_RECT_ARG(&pipe_config->pch_pfit.dst),
8167 			    enableddisabled(pipe_config->pch_pfit.enabled),
8168 			    yesno(pipe_config->pch_pfit.force_thru));
8169 
8170 	drm_dbg_kms(&dev_priv->drm, "ips: %i, double wide: %i\n",
8171 		    pipe_config->ips_enabled, pipe_config->double_wide);
8172 
8173 	intel_dpll_dump_hw_state(dev_priv, &pipe_config->dpll_hw_state);
8174 
8175 	if (IS_CHERRYVIEW(dev_priv))
8176 		drm_dbg_kms(&dev_priv->drm,
8177 			    "cgm_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
8178 			    pipe_config->cgm_mode, pipe_config->gamma_mode,
8179 			    pipe_config->gamma_enable, pipe_config->csc_enable);
8180 	else
8181 		drm_dbg_kms(&dev_priv->drm,
8182 			    "csc_mode: 0x%x gamma_mode: 0x%x gamma_enable: %d csc_enable: %d\n",
8183 			    pipe_config->csc_mode, pipe_config->gamma_mode,
8184 			    pipe_config->gamma_enable, pipe_config->csc_enable);
8185 
8186 	drm_dbg_kms(&dev_priv->drm, "degamma lut: %d entries, gamma lut: %d entries\n",
8187 		    pipe_config->hw.degamma_lut ?
8188 		    drm_color_lut_size(pipe_config->hw.degamma_lut) : 0,
8189 		    pipe_config->hw.gamma_lut ?
8190 		    drm_color_lut_size(pipe_config->hw.gamma_lut) : 0);
8191 
8192 dump_planes:
8193 	if (!state)
8194 		return;
8195 
8196 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
8197 		if (plane->pipe == crtc->pipe)
8198 			intel_dump_plane_state(plane_state);
8199 	}
8200 }
8201 
8202 static bool check_digital_port_conflicts(struct intel_atomic_state *state)
8203 {
8204 	struct drm_device *dev = state->base.dev;
8205 	struct drm_connector *connector;
8206 	struct drm_connector_list_iter conn_iter;
8207 	unsigned int used_ports = 0;
8208 	unsigned int used_mst_ports = 0;
8209 	bool ret = true;
8210 
8211 	/*
8212 	 * We're going to peek into connector->state,
8213 	 * hence connection_mutex must be held.
8214 	 */
8215 	drm_modeset_lock_assert_held(&dev->mode_config.connection_mutex);
8216 
8217 	/*
8218 	 * Walk the connector list instead of the encoder
8219 	 * list to detect the problem on ddi platforms
8220 	 * where there's just one encoder per digital port.
8221 	 */
8222 	drm_connector_list_iter_begin(dev, &conn_iter);
8223 	drm_for_each_connector_iter(connector, &conn_iter) {
8224 		struct drm_connector_state *connector_state;
8225 		struct intel_encoder *encoder;
8226 
8227 		connector_state =
8228 			drm_atomic_get_new_connector_state(&state->base,
8229 							   connector);
8230 		if (!connector_state)
8231 			connector_state = connector->state;
8232 
8233 		if (!connector_state->best_encoder)
8234 			continue;
8235 
8236 		encoder = to_intel_encoder(connector_state->best_encoder);
8237 
8238 		drm_WARN_ON(dev, !connector_state->crtc);
8239 
8240 		switch (encoder->type) {
8241 		case INTEL_OUTPUT_DDI:
8242 			if (drm_WARN_ON(dev, !HAS_DDI(to_i915(dev))))
8243 				break;
8244 			fallthrough;
8245 		case INTEL_OUTPUT_DP:
8246 		case INTEL_OUTPUT_HDMI:
8247 		case INTEL_OUTPUT_EDP:
8248 			/* the same port mustn't appear more than once */
8249 			if (used_ports & BIT(encoder->port))
8250 				ret = false;
8251 
8252 			used_ports |= BIT(encoder->port);
8253 			break;
8254 		case INTEL_OUTPUT_DP_MST:
8255 			used_mst_ports |=
8256 				1 << encoder->port;
8257 			break;
8258 		default:
8259 			break;
8260 		}
8261 	}
8262 	drm_connector_list_iter_end(&conn_iter);
8263 
8264 	/* can't mix MST and SST/HDMI on the same port */
8265 	if (used_ports & used_mst_ports)
8266 		return false;
8267 
8268 	return ret;
8269 }
8270 
8271 static void
8272 intel_crtc_copy_uapi_to_hw_state_nomodeset(struct intel_atomic_state *state,
8273 					   struct intel_crtc_state *crtc_state)
8274 {
8275 	const struct intel_crtc_state *from_crtc_state = crtc_state;
8276 
8277 	if (crtc_state->bigjoiner_slave) {
8278 		from_crtc_state = intel_atomic_get_new_crtc_state(state,
8279 								  crtc_state->bigjoiner_linked_crtc);
8280 
8281 		/* No need to copy state if the master state is unchanged */
8282 		if (!from_crtc_state)
8283 			return;
8284 	}
8285 
8286 	intel_crtc_copy_color_blobs(crtc_state, from_crtc_state);
8287 }
8288 
8289 static void
8290 intel_crtc_copy_uapi_to_hw_state(struct intel_atomic_state *state,
8291 				 struct intel_crtc_state *crtc_state)
8292 {
8293 	crtc_state->hw.enable = crtc_state->uapi.enable;
8294 	crtc_state->hw.active = crtc_state->uapi.active;
8295 	crtc_state->hw.mode = crtc_state->uapi.mode;
8296 	crtc_state->hw.adjusted_mode = crtc_state->uapi.adjusted_mode;
8297 	crtc_state->hw.scaling_filter = crtc_state->uapi.scaling_filter;
8298 
8299 	intel_crtc_copy_uapi_to_hw_state_nomodeset(state, crtc_state);
8300 }
8301 
8302 static void intel_crtc_copy_hw_to_uapi_state(struct intel_crtc_state *crtc_state)
8303 {
8304 	if (crtc_state->bigjoiner_slave)
8305 		return;
8306 
8307 	crtc_state->uapi.enable = crtc_state->hw.enable;
8308 	crtc_state->uapi.active = crtc_state->hw.active;
8309 	drm_WARN_ON(crtc_state->uapi.crtc->dev,
8310 		    drm_atomic_set_mode_for_crtc(&crtc_state->uapi, &crtc_state->hw.mode) < 0);
8311 
8312 	crtc_state->uapi.adjusted_mode = crtc_state->hw.adjusted_mode;
8313 	crtc_state->uapi.scaling_filter = crtc_state->hw.scaling_filter;
8314 
8315 	/* copy color blobs to uapi */
8316 	drm_property_replace_blob(&crtc_state->uapi.degamma_lut,
8317 				  crtc_state->hw.degamma_lut);
8318 	drm_property_replace_blob(&crtc_state->uapi.gamma_lut,
8319 				  crtc_state->hw.gamma_lut);
8320 	drm_property_replace_blob(&crtc_state->uapi.ctm,
8321 				  crtc_state->hw.ctm);
8322 }
8323 
8324 static int
8325 copy_bigjoiner_crtc_state(struct intel_crtc_state *crtc_state,
8326 			  const struct intel_crtc_state *from_crtc_state)
8327 {
8328 	struct intel_crtc_state *saved_state;
8329 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8330 
8331 	saved_state = kmemdup(from_crtc_state, sizeof(*saved_state), GFP_KERNEL);
8332 	if (!saved_state)
8333 		return -ENOMEM;
8334 
8335 	saved_state->uapi = crtc_state->uapi;
8336 	saved_state->scaler_state = crtc_state->scaler_state;
8337 	saved_state->shared_dpll = crtc_state->shared_dpll;
8338 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
8339 	saved_state->crc_enabled = crtc_state->crc_enabled;
8340 
8341 	intel_crtc_free_hw_state(crtc_state);
8342 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
8343 	kfree(saved_state);
8344 
8345 	/* Re-init hw state */
8346 	memset(&crtc_state->hw, 0, sizeof(saved_state->hw));
8347 	crtc_state->hw.enable = from_crtc_state->hw.enable;
8348 	crtc_state->hw.active = from_crtc_state->hw.active;
8349 	crtc_state->hw.pipe_mode = from_crtc_state->hw.pipe_mode;
8350 	crtc_state->hw.adjusted_mode = from_crtc_state->hw.adjusted_mode;
8351 
8352 	/* Some fixups */
8353 	crtc_state->uapi.mode_changed = from_crtc_state->uapi.mode_changed;
8354 	crtc_state->uapi.connectors_changed = from_crtc_state->uapi.connectors_changed;
8355 	crtc_state->uapi.active_changed = from_crtc_state->uapi.active_changed;
8356 	crtc_state->nv12_planes = crtc_state->c8_planes = crtc_state->update_planes = 0;
8357 	crtc_state->bigjoiner_linked_crtc = to_intel_crtc(from_crtc_state->uapi.crtc);
8358 	crtc_state->bigjoiner_slave = true;
8359 	crtc_state->cpu_transcoder = (enum transcoder)crtc->pipe;
8360 	crtc_state->has_audio = false;
8361 
8362 	return 0;
8363 }
8364 
8365 static int
8366 intel_crtc_prepare_cleared_state(struct intel_atomic_state *state,
8367 				 struct intel_crtc_state *crtc_state)
8368 {
8369 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8370 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
8371 	struct intel_crtc_state *saved_state;
8372 
8373 	saved_state = intel_crtc_state_alloc(crtc);
8374 	if (!saved_state)
8375 		return -ENOMEM;
8376 
8377 	/* free the old crtc_state->hw members */
8378 	intel_crtc_free_hw_state(crtc_state);
8379 
8380 	/* FIXME: before the switch to atomic started, a new pipe_config was
8381 	 * kzalloc'd. Code that depends on any field being zero should be
8382 	 * fixed, so that the crtc_state can be safely duplicated. For now,
8383 	 * only fields that are know to not cause problems are preserved. */
8384 
8385 	saved_state->uapi = crtc_state->uapi;
8386 	saved_state->scaler_state = crtc_state->scaler_state;
8387 	saved_state->shared_dpll = crtc_state->shared_dpll;
8388 	saved_state->dpll_hw_state = crtc_state->dpll_hw_state;
8389 	memcpy(saved_state->icl_port_dplls, crtc_state->icl_port_dplls,
8390 	       sizeof(saved_state->icl_port_dplls));
8391 	saved_state->crc_enabled = crtc_state->crc_enabled;
8392 	if (IS_G4X(dev_priv) ||
8393 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
8394 		saved_state->wm = crtc_state->wm;
8395 
8396 	memcpy(crtc_state, saved_state, sizeof(*crtc_state));
8397 	kfree(saved_state);
8398 
8399 	intel_crtc_copy_uapi_to_hw_state(state, crtc_state);
8400 
8401 	return 0;
8402 }
8403 
8404 static int
8405 intel_modeset_pipe_config(struct intel_atomic_state *state,
8406 			  struct intel_crtc_state *pipe_config)
8407 {
8408 	struct drm_crtc *crtc = pipe_config->uapi.crtc;
8409 	struct drm_i915_private *i915 = to_i915(pipe_config->uapi.crtc->dev);
8410 	struct drm_connector *connector;
8411 	struct drm_connector_state *connector_state;
8412 	int base_bpp, ret, i;
8413 	bool retry = true;
8414 
8415 	pipe_config->cpu_transcoder =
8416 		(enum transcoder) to_intel_crtc(crtc)->pipe;
8417 
8418 	/*
8419 	 * Sanitize sync polarity flags based on requested ones. If neither
8420 	 * positive or negative polarity is requested, treat this as meaning
8421 	 * negative polarity.
8422 	 */
8423 	if (!(pipe_config->hw.adjusted_mode.flags &
8424 	      (DRM_MODE_FLAG_PHSYNC | DRM_MODE_FLAG_NHSYNC)))
8425 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NHSYNC;
8426 
8427 	if (!(pipe_config->hw.adjusted_mode.flags &
8428 	      (DRM_MODE_FLAG_PVSYNC | DRM_MODE_FLAG_NVSYNC)))
8429 		pipe_config->hw.adjusted_mode.flags |= DRM_MODE_FLAG_NVSYNC;
8430 
8431 	ret = compute_baseline_pipe_bpp(to_intel_crtc(crtc),
8432 					pipe_config);
8433 	if (ret)
8434 		return ret;
8435 
8436 	base_bpp = pipe_config->pipe_bpp;
8437 
8438 	/*
8439 	 * Determine the real pipe dimensions. Note that stereo modes can
8440 	 * increase the actual pipe size due to the frame doubling and
8441 	 * insertion of additional space for blanks between the frame. This
8442 	 * is stored in the crtc timings. We use the requested mode to do this
8443 	 * computation to clearly distinguish it from the adjusted mode, which
8444 	 * can be changed by the connectors in the below retry loop.
8445 	 */
8446 	drm_mode_get_hv_timing(&pipe_config->hw.mode,
8447 			       &pipe_config->pipe_src_w,
8448 			       &pipe_config->pipe_src_h);
8449 
8450 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
8451 		struct intel_encoder *encoder =
8452 			to_intel_encoder(connector_state->best_encoder);
8453 
8454 		if (connector_state->crtc != crtc)
8455 			continue;
8456 
8457 		if (!check_single_encoder_cloning(state, to_intel_crtc(crtc), encoder)) {
8458 			drm_dbg_kms(&i915->drm,
8459 				    "rejecting invalid cloning configuration\n");
8460 			return -EINVAL;
8461 		}
8462 
8463 		/*
8464 		 * Determine output_types before calling the .compute_config()
8465 		 * hooks so that the hooks can use this information safely.
8466 		 */
8467 		if (encoder->compute_output_type)
8468 			pipe_config->output_types |=
8469 				BIT(encoder->compute_output_type(encoder, pipe_config,
8470 								 connector_state));
8471 		else
8472 			pipe_config->output_types |= BIT(encoder->type);
8473 	}
8474 
8475 encoder_retry:
8476 	/* Ensure the port clock defaults are reset when retrying. */
8477 	pipe_config->port_clock = 0;
8478 	pipe_config->pixel_multiplier = 1;
8479 
8480 	/* Fill in default crtc timings, allow encoders to overwrite them. */
8481 	drm_mode_set_crtcinfo(&pipe_config->hw.adjusted_mode,
8482 			      CRTC_STEREO_DOUBLE);
8483 
8484 	/* Pass our mode to the connectors and the CRTC to give them a chance to
8485 	 * adjust it according to limitations or connector properties, and also
8486 	 * a chance to reject the mode entirely.
8487 	 */
8488 	for_each_new_connector_in_state(&state->base, connector, connector_state, i) {
8489 		struct intel_encoder *encoder =
8490 			to_intel_encoder(connector_state->best_encoder);
8491 
8492 		if (connector_state->crtc != crtc)
8493 			continue;
8494 
8495 		ret = encoder->compute_config(encoder, pipe_config,
8496 					      connector_state);
8497 		if (ret < 0) {
8498 			if (ret != -EDEADLK)
8499 				drm_dbg_kms(&i915->drm,
8500 					    "Encoder config failure: %d\n",
8501 					    ret);
8502 			return ret;
8503 		}
8504 	}
8505 
8506 	/* Set default port clock if not overwritten by the encoder. Needs to be
8507 	 * done afterwards in case the encoder adjusts the mode. */
8508 	if (!pipe_config->port_clock)
8509 		pipe_config->port_clock = pipe_config->hw.adjusted_mode.crtc_clock
8510 			* pipe_config->pixel_multiplier;
8511 
8512 	ret = intel_crtc_compute_config(to_intel_crtc(crtc), pipe_config);
8513 	if (ret == -EDEADLK)
8514 		return ret;
8515 	if (ret < 0) {
8516 		drm_dbg_kms(&i915->drm, "CRTC fixup failed\n");
8517 		return ret;
8518 	}
8519 
8520 	if (ret == I915_DISPLAY_CONFIG_RETRY) {
8521 		if (drm_WARN(&i915->drm, !retry,
8522 			     "loop in pipe configuration computation\n"))
8523 			return -EINVAL;
8524 
8525 		drm_dbg_kms(&i915->drm, "CRTC bw constrained, retrying\n");
8526 		retry = false;
8527 		goto encoder_retry;
8528 	}
8529 
8530 	/* Dithering seems to not pass-through bits correctly when it should, so
8531 	 * only enable it on 6bpc panels and when its not a compliance
8532 	 * test requesting 6bpc video pattern.
8533 	 */
8534 	pipe_config->dither = (pipe_config->pipe_bpp == 6*3) &&
8535 		!pipe_config->dither_force_disable;
8536 	drm_dbg_kms(&i915->drm,
8537 		    "hw max bpp: %i, pipe bpp: %i, dithering: %i\n",
8538 		    base_bpp, pipe_config->pipe_bpp, pipe_config->dither);
8539 
8540 	return 0;
8541 }
8542 
8543 static int
8544 intel_modeset_pipe_config_late(struct intel_crtc_state *crtc_state)
8545 {
8546 	struct intel_atomic_state *state =
8547 		to_intel_atomic_state(crtc_state->uapi.state);
8548 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
8549 	struct drm_connector_state *conn_state;
8550 	struct drm_connector *connector;
8551 	int i;
8552 
8553 	for_each_new_connector_in_state(&state->base, connector,
8554 					conn_state, i) {
8555 		struct intel_encoder *encoder =
8556 			to_intel_encoder(conn_state->best_encoder);
8557 		int ret;
8558 
8559 		if (conn_state->crtc != &crtc->base ||
8560 		    !encoder->compute_config_late)
8561 			continue;
8562 
8563 		ret = encoder->compute_config_late(encoder, crtc_state,
8564 						   conn_state);
8565 		if (ret)
8566 			return ret;
8567 	}
8568 
8569 	return 0;
8570 }
8571 
8572 bool intel_fuzzy_clock_check(int clock1, int clock2)
8573 {
8574 	int diff;
8575 
8576 	if (clock1 == clock2)
8577 		return true;
8578 
8579 	if (!clock1 || !clock2)
8580 		return false;
8581 
8582 	diff = abs(clock1 - clock2);
8583 
8584 	if (((((diff + clock1 + clock2) * 100)) / (clock1 + clock2)) < 105)
8585 		return true;
8586 
8587 	return false;
8588 }
8589 
8590 static bool
8591 intel_compare_m_n(unsigned int m, unsigned int n,
8592 		  unsigned int m2, unsigned int n2,
8593 		  bool exact)
8594 {
8595 	if (m == m2 && n == n2)
8596 		return true;
8597 
8598 	if (exact || !m || !n || !m2 || !n2)
8599 		return false;
8600 
8601 	BUILD_BUG_ON(DATA_LINK_M_N_MASK > INT_MAX);
8602 
8603 	if (n > n2) {
8604 		while (n > n2) {
8605 			m2 <<= 1;
8606 			n2 <<= 1;
8607 		}
8608 	} else if (n < n2) {
8609 		while (n < n2) {
8610 			m <<= 1;
8611 			n <<= 1;
8612 		}
8613 	}
8614 
8615 	if (n != n2)
8616 		return false;
8617 
8618 	return intel_fuzzy_clock_check(m, m2);
8619 }
8620 
8621 static bool
8622 intel_compare_link_m_n(const struct intel_link_m_n *m_n,
8623 		       const struct intel_link_m_n *m2_n2,
8624 		       bool exact)
8625 {
8626 	return m_n->tu == m2_n2->tu &&
8627 		intel_compare_m_n(m_n->gmch_m, m_n->gmch_n,
8628 				  m2_n2->gmch_m, m2_n2->gmch_n, exact) &&
8629 		intel_compare_m_n(m_n->link_m, m_n->link_n,
8630 				  m2_n2->link_m, m2_n2->link_n, exact);
8631 }
8632 
8633 static bool
8634 intel_compare_infoframe(const union hdmi_infoframe *a,
8635 			const union hdmi_infoframe *b)
8636 {
8637 	return memcmp(a, b, sizeof(*a)) == 0;
8638 }
8639 
8640 static bool
8641 intel_compare_dp_vsc_sdp(const struct drm_dp_vsc_sdp *a,
8642 			 const struct drm_dp_vsc_sdp *b)
8643 {
8644 	return memcmp(a, b, sizeof(*a)) == 0;
8645 }
8646 
8647 static void
8648 pipe_config_infoframe_mismatch(struct drm_i915_private *dev_priv,
8649 			       bool fastset, const char *name,
8650 			       const union hdmi_infoframe *a,
8651 			       const union hdmi_infoframe *b)
8652 {
8653 	if (fastset) {
8654 		if (!drm_debug_enabled(DRM_UT_KMS))
8655 			return;
8656 
8657 		drm_dbg_kms(&dev_priv->drm,
8658 			    "fastset mismatch in %s infoframe\n", name);
8659 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
8660 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, a);
8661 		drm_dbg_kms(&dev_priv->drm, "found:\n");
8662 		hdmi_infoframe_log(KERN_DEBUG, dev_priv->drm.dev, b);
8663 	} else {
8664 		drm_err(&dev_priv->drm, "mismatch in %s infoframe\n", name);
8665 		drm_err(&dev_priv->drm, "expected:\n");
8666 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, a);
8667 		drm_err(&dev_priv->drm, "found:\n");
8668 		hdmi_infoframe_log(KERN_ERR, dev_priv->drm.dev, b);
8669 	}
8670 }
8671 
8672 static void
8673 pipe_config_dp_vsc_sdp_mismatch(struct drm_i915_private *dev_priv,
8674 				bool fastset, const char *name,
8675 				const struct drm_dp_vsc_sdp *a,
8676 				const struct drm_dp_vsc_sdp *b)
8677 {
8678 	if (fastset) {
8679 		if (!drm_debug_enabled(DRM_UT_KMS))
8680 			return;
8681 
8682 		drm_dbg_kms(&dev_priv->drm,
8683 			    "fastset mismatch in %s dp sdp\n", name);
8684 		drm_dbg_kms(&dev_priv->drm, "expected:\n");
8685 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, a);
8686 		drm_dbg_kms(&dev_priv->drm, "found:\n");
8687 		drm_dp_vsc_sdp_log(KERN_DEBUG, dev_priv->drm.dev, b);
8688 	} else {
8689 		drm_err(&dev_priv->drm, "mismatch in %s dp sdp\n", name);
8690 		drm_err(&dev_priv->drm, "expected:\n");
8691 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, a);
8692 		drm_err(&dev_priv->drm, "found:\n");
8693 		drm_dp_vsc_sdp_log(KERN_ERR, dev_priv->drm.dev, b);
8694 	}
8695 }
8696 
8697 static void __printf(4, 5)
8698 pipe_config_mismatch(bool fastset, const struct intel_crtc *crtc,
8699 		     const char *name, const char *format, ...)
8700 {
8701 	struct drm_i915_private *i915 = to_i915(crtc->base.dev);
8702 	struct va_format vaf;
8703 	va_list args;
8704 
8705 	va_start(args, format);
8706 	vaf.fmt = format;
8707 	vaf.va = &args;
8708 
8709 	if (fastset)
8710 		drm_dbg_kms(&i915->drm,
8711 			    "[CRTC:%d:%s] fastset mismatch in %s %pV\n",
8712 			    crtc->base.base.id, crtc->base.name, name, &vaf);
8713 	else
8714 		drm_err(&i915->drm, "[CRTC:%d:%s] mismatch in %s %pV\n",
8715 			crtc->base.base.id, crtc->base.name, name, &vaf);
8716 
8717 	va_end(args);
8718 }
8719 
8720 static bool fastboot_enabled(struct drm_i915_private *dev_priv)
8721 {
8722 	if (dev_priv->params.fastboot != -1)
8723 		return dev_priv->params.fastboot;
8724 
8725 	/* Enable fastboot by default on Skylake and newer */
8726 	if (INTEL_GEN(dev_priv) >= 9)
8727 		return true;
8728 
8729 	/* Enable fastboot by default on VLV and CHV */
8730 	if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
8731 		return true;
8732 
8733 	/* Disabled by default on all others */
8734 	return false;
8735 }
8736 
8737 static bool
8738 intel_pipe_config_compare(const struct intel_crtc_state *current_config,
8739 			  const struct intel_crtc_state *pipe_config,
8740 			  bool fastset)
8741 {
8742 	struct drm_i915_private *dev_priv = to_i915(current_config->uapi.crtc->dev);
8743 	struct intel_crtc *crtc = to_intel_crtc(pipe_config->uapi.crtc);
8744 	bool ret = true;
8745 	u32 bp_gamma = 0;
8746 	bool fixup_inherited = fastset &&
8747 		current_config->inherited && !pipe_config->inherited;
8748 
8749 	if (fixup_inherited && !fastboot_enabled(dev_priv)) {
8750 		drm_dbg_kms(&dev_priv->drm,
8751 			    "initial modeset and fastboot not set\n");
8752 		ret = false;
8753 	}
8754 
8755 #define PIPE_CONF_CHECK_X(name) do { \
8756 	if (current_config->name != pipe_config->name) { \
8757 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8758 				     "(expected 0x%08x, found 0x%08x)", \
8759 				     current_config->name, \
8760 				     pipe_config->name); \
8761 		ret = false; \
8762 	} \
8763 } while (0)
8764 
8765 #define PIPE_CONF_CHECK_I(name) do { \
8766 	if (current_config->name != pipe_config->name) { \
8767 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8768 				     "(expected %i, found %i)", \
8769 				     current_config->name, \
8770 				     pipe_config->name); \
8771 		ret = false; \
8772 	} \
8773 } while (0)
8774 
8775 #define PIPE_CONF_CHECK_BOOL(name) do { \
8776 	if (current_config->name != pipe_config->name) { \
8777 		pipe_config_mismatch(fastset, crtc,  __stringify(name), \
8778 				     "(expected %s, found %s)", \
8779 				     yesno(current_config->name), \
8780 				     yesno(pipe_config->name)); \
8781 		ret = false; \
8782 	} \
8783 } while (0)
8784 
8785 /*
8786  * Checks state where we only read out the enabling, but not the entire
8787  * state itself (like full infoframes or ELD for audio). These states
8788  * require a full modeset on bootup to fix up.
8789  */
8790 #define PIPE_CONF_CHECK_BOOL_INCOMPLETE(name) do { \
8791 	if (!fixup_inherited || (!current_config->name && !pipe_config->name)) { \
8792 		PIPE_CONF_CHECK_BOOL(name); \
8793 	} else { \
8794 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8795 				     "unable to verify whether state matches exactly, forcing modeset (expected %s, found %s)", \
8796 				     yesno(current_config->name), \
8797 				     yesno(pipe_config->name)); \
8798 		ret = false; \
8799 	} \
8800 } while (0)
8801 
8802 #define PIPE_CONF_CHECK_P(name) do { \
8803 	if (current_config->name != pipe_config->name) { \
8804 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8805 				     "(expected %p, found %p)", \
8806 				     current_config->name, \
8807 				     pipe_config->name); \
8808 		ret = false; \
8809 	} \
8810 } while (0)
8811 
8812 #define PIPE_CONF_CHECK_M_N(name) do { \
8813 	if (!intel_compare_link_m_n(&current_config->name, \
8814 				    &pipe_config->name,\
8815 				    !fastset)) { \
8816 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8817 				     "(expected tu %i gmch %i/%i link %i/%i, " \
8818 				     "found tu %i, gmch %i/%i link %i/%i)", \
8819 				     current_config->name.tu, \
8820 				     current_config->name.gmch_m, \
8821 				     current_config->name.gmch_n, \
8822 				     current_config->name.link_m, \
8823 				     current_config->name.link_n, \
8824 				     pipe_config->name.tu, \
8825 				     pipe_config->name.gmch_m, \
8826 				     pipe_config->name.gmch_n, \
8827 				     pipe_config->name.link_m, \
8828 				     pipe_config->name.link_n); \
8829 		ret = false; \
8830 	} \
8831 } while (0)
8832 
8833 /* This is required for BDW+ where there is only one set of registers for
8834  * switching between high and low RR.
8835  * This macro can be used whenever a comparison has to be made between one
8836  * hw state and multiple sw state variables.
8837  */
8838 #define PIPE_CONF_CHECK_M_N_ALT(name, alt_name) do { \
8839 	if (!intel_compare_link_m_n(&current_config->name, \
8840 				    &pipe_config->name, !fastset) && \
8841 	    !intel_compare_link_m_n(&current_config->alt_name, \
8842 				    &pipe_config->name, !fastset)) { \
8843 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8844 				     "(expected tu %i gmch %i/%i link %i/%i, " \
8845 				     "or tu %i gmch %i/%i link %i/%i, " \
8846 				     "found tu %i, gmch %i/%i link %i/%i)", \
8847 				     current_config->name.tu, \
8848 				     current_config->name.gmch_m, \
8849 				     current_config->name.gmch_n, \
8850 				     current_config->name.link_m, \
8851 				     current_config->name.link_n, \
8852 				     current_config->alt_name.tu, \
8853 				     current_config->alt_name.gmch_m, \
8854 				     current_config->alt_name.gmch_n, \
8855 				     current_config->alt_name.link_m, \
8856 				     current_config->alt_name.link_n, \
8857 				     pipe_config->name.tu, \
8858 				     pipe_config->name.gmch_m, \
8859 				     pipe_config->name.gmch_n, \
8860 				     pipe_config->name.link_m, \
8861 				     pipe_config->name.link_n); \
8862 		ret = false; \
8863 	} \
8864 } while (0)
8865 
8866 #define PIPE_CONF_CHECK_FLAGS(name, mask) do { \
8867 	if ((current_config->name ^ pipe_config->name) & (mask)) { \
8868 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8869 				     "(%x) (expected %i, found %i)", \
8870 				     (mask), \
8871 				     current_config->name & (mask), \
8872 				     pipe_config->name & (mask)); \
8873 		ret = false; \
8874 	} \
8875 } while (0)
8876 
8877 #define PIPE_CONF_CHECK_CLOCK_FUZZY(name) do { \
8878 	if (!intel_fuzzy_clock_check(current_config->name, pipe_config->name)) { \
8879 		pipe_config_mismatch(fastset, crtc, __stringify(name), \
8880 				     "(expected %i, found %i)", \
8881 				     current_config->name, \
8882 				     pipe_config->name); \
8883 		ret = false; \
8884 	} \
8885 } while (0)
8886 
8887 #define PIPE_CONF_CHECK_INFOFRAME(name) do { \
8888 	if (!intel_compare_infoframe(&current_config->infoframes.name, \
8889 				     &pipe_config->infoframes.name)) { \
8890 		pipe_config_infoframe_mismatch(dev_priv, fastset, __stringify(name), \
8891 					       &current_config->infoframes.name, \
8892 					       &pipe_config->infoframes.name); \
8893 		ret = false; \
8894 	} \
8895 } while (0)
8896 
8897 #define PIPE_CONF_CHECK_DP_VSC_SDP(name) do { \
8898 	if (!current_config->has_psr && !pipe_config->has_psr && \
8899 	    !intel_compare_dp_vsc_sdp(&current_config->infoframes.name, \
8900 				      &pipe_config->infoframes.name)) { \
8901 		pipe_config_dp_vsc_sdp_mismatch(dev_priv, fastset, __stringify(name), \
8902 						&current_config->infoframes.name, \
8903 						&pipe_config->infoframes.name); \
8904 		ret = false; \
8905 	} \
8906 } while (0)
8907 
8908 #define PIPE_CONF_CHECK_COLOR_LUT(name1, name2, bit_precision) do { \
8909 	if (current_config->name1 != pipe_config->name1) { \
8910 		pipe_config_mismatch(fastset, crtc, __stringify(name1), \
8911 				"(expected %i, found %i, won't compare lut values)", \
8912 				current_config->name1, \
8913 				pipe_config->name1); \
8914 		ret = false;\
8915 	} else { \
8916 		if (!intel_color_lut_equal(current_config->name2, \
8917 					pipe_config->name2, pipe_config->name1, \
8918 					bit_precision)) { \
8919 			pipe_config_mismatch(fastset, crtc, __stringify(name2), \
8920 					"hw_state doesn't match sw_state"); \
8921 			ret = false; \
8922 		} \
8923 	} \
8924 } while (0)
8925 
8926 #define PIPE_CONF_QUIRK(quirk) \
8927 	((current_config->quirks | pipe_config->quirks) & (quirk))
8928 
8929 	PIPE_CONF_CHECK_I(cpu_transcoder);
8930 
8931 	PIPE_CONF_CHECK_BOOL(has_pch_encoder);
8932 	PIPE_CONF_CHECK_I(fdi_lanes);
8933 	PIPE_CONF_CHECK_M_N(fdi_m_n);
8934 
8935 	PIPE_CONF_CHECK_I(lane_count);
8936 	PIPE_CONF_CHECK_X(lane_lat_optim_mask);
8937 
8938 	if (INTEL_GEN(dev_priv) < 8) {
8939 		PIPE_CONF_CHECK_M_N(dp_m_n);
8940 
8941 		if (current_config->has_drrs)
8942 			PIPE_CONF_CHECK_M_N(dp_m2_n2);
8943 	} else
8944 		PIPE_CONF_CHECK_M_N_ALT(dp_m_n, dp_m2_n2);
8945 
8946 	PIPE_CONF_CHECK_X(output_types);
8947 
8948 	/* FIXME do the readout properly and get rid of this quirk */
8949 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
8950 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hdisplay);
8951 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_htotal);
8952 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_start);
8953 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hblank_end);
8954 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_start);
8955 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_hsync_end);
8956 
8957 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vdisplay);
8958 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vtotal);
8959 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_start);
8960 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vblank_end);
8961 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_start);
8962 		PIPE_CONF_CHECK_I(hw.pipe_mode.crtc_vsync_end);
8963 
8964 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hdisplay);
8965 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_htotal);
8966 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_start);
8967 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hblank_end);
8968 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_start);
8969 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_hsync_end);
8970 
8971 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vdisplay);
8972 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vtotal);
8973 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_start);
8974 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vblank_end);
8975 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_start);
8976 		PIPE_CONF_CHECK_I(hw.adjusted_mode.crtc_vsync_end);
8977 
8978 		PIPE_CONF_CHECK_I(pixel_multiplier);
8979 
8980 		PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8981 				      DRM_MODE_FLAG_INTERLACE);
8982 
8983 		if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_MODE_SYNC_FLAGS)) {
8984 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8985 					      DRM_MODE_FLAG_PHSYNC);
8986 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8987 					      DRM_MODE_FLAG_NHSYNC);
8988 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8989 					      DRM_MODE_FLAG_PVSYNC);
8990 			PIPE_CONF_CHECK_FLAGS(hw.adjusted_mode.flags,
8991 					      DRM_MODE_FLAG_NVSYNC);
8992 		}
8993 	}
8994 
8995 	PIPE_CONF_CHECK_I(output_format);
8996 	PIPE_CONF_CHECK_BOOL(has_hdmi_sink);
8997 	if ((INTEL_GEN(dev_priv) < 8 && !IS_HASWELL(dev_priv)) ||
8998 	    IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv))
8999 		PIPE_CONF_CHECK_BOOL(limited_color_range);
9000 
9001 	PIPE_CONF_CHECK_BOOL(hdmi_scrambling);
9002 	PIPE_CONF_CHECK_BOOL(hdmi_high_tmds_clock_ratio);
9003 	PIPE_CONF_CHECK_BOOL(has_infoframe);
9004 	/* FIXME do the readout properly and get rid of this quirk */
9005 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
9006 		PIPE_CONF_CHECK_BOOL(fec_enable);
9007 
9008 	PIPE_CONF_CHECK_BOOL_INCOMPLETE(has_audio);
9009 
9010 	PIPE_CONF_CHECK_X(gmch_pfit.control);
9011 	/* pfit ratios are autocomputed by the hw on gen4+ */
9012 	if (INTEL_GEN(dev_priv) < 4)
9013 		PIPE_CONF_CHECK_X(gmch_pfit.pgm_ratios);
9014 	PIPE_CONF_CHECK_X(gmch_pfit.lvds_border_bits);
9015 
9016 	/*
9017 	 * Changing the EDP transcoder input mux
9018 	 * (A_ONOFF vs. A_ON) requires a full modeset.
9019 	 */
9020 	PIPE_CONF_CHECK_BOOL(pch_pfit.force_thru);
9021 
9022 	if (!fastset) {
9023 		PIPE_CONF_CHECK_I(pipe_src_w);
9024 		PIPE_CONF_CHECK_I(pipe_src_h);
9025 
9026 		PIPE_CONF_CHECK_BOOL(pch_pfit.enabled);
9027 		if (current_config->pch_pfit.enabled) {
9028 			PIPE_CONF_CHECK_I(pch_pfit.dst.x1);
9029 			PIPE_CONF_CHECK_I(pch_pfit.dst.y1);
9030 			PIPE_CONF_CHECK_I(pch_pfit.dst.x2);
9031 			PIPE_CONF_CHECK_I(pch_pfit.dst.y2);
9032 		}
9033 
9034 		PIPE_CONF_CHECK_I(scaler_state.scaler_id);
9035 		/* FIXME do the readout properly and get rid of this quirk */
9036 		if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE))
9037 			PIPE_CONF_CHECK_CLOCK_FUZZY(pixel_rate);
9038 
9039 		PIPE_CONF_CHECK_X(gamma_mode);
9040 		if (IS_CHERRYVIEW(dev_priv))
9041 			PIPE_CONF_CHECK_X(cgm_mode);
9042 		else
9043 			PIPE_CONF_CHECK_X(csc_mode);
9044 		PIPE_CONF_CHECK_BOOL(gamma_enable);
9045 		PIPE_CONF_CHECK_BOOL(csc_enable);
9046 
9047 		PIPE_CONF_CHECK_I(linetime);
9048 		PIPE_CONF_CHECK_I(ips_linetime);
9049 
9050 		bp_gamma = intel_color_get_gamma_bit_precision(pipe_config);
9051 		if (bp_gamma)
9052 			PIPE_CONF_CHECK_COLOR_LUT(gamma_mode, hw.gamma_lut, bp_gamma);
9053 	}
9054 
9055 	PIPE_CONF_CHECK_BOOL(double_wide);
9056 
9057 	PIPE_CONF_CHECK_P(shared_dpll);
9058 
9059 	/* FIXME do the readout properly and get rid of this quirk */
9060 	if (!PIPE_CONF_QUIRK(PIPE_CONFIG_QUIRK_BIGJOINER_SLAVE)) {
9061 		PIPE_CONF_CHECK_X(dpll_hw_state.dpll);
9062 		PIPE_CONF_CHECK_X(dpll_hw_state.dpll_md);
9063 		PIPE_CONF_CHECK_X(dpll_hw_state.fp0);
9064 		PIPE_CONF_CHECK_X(dpll_hw_state.fp1);
9065 		PIPE_CONF_CHECK_X(dpll_hw_state.wrpll);
9066 		PIPE_CONF_CHECK_X(dpll_hw_state.spll);
9067 		PIPE_CONF_CHECK_X(dpll_hw_state.ctrl1);
9068 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr1);
9069 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr2);
9070 		PIPE_CONF_CHECK_X(dpll_hw_state.cfgcr0);
9071 		PIPE_CONF_CHECK_X(dpll_hw_state.ebb0);
9072 		PIPE_CONF_CHECK_X(dpll_hw_state.ebb4);
9073 		PIPE_CONF_CHECK_X(dpll_hw_state.pll0);
9074 		PIPE_CONF_CHECK_X(dpll_hw_state.pll1);
9075 		PIPE_CONF_CHECK_X(dpll_hw_state.pll2);
9076 		PIPE_CONF_CHECK_X(dpll_hw_state.pll3);
9077 		PIPE_CONF_CHECK_X(dpll_hw_state.pll6);
9078 		PIPE_CONF_CHECK_X(dpll_hw_state.pll8);
9079 		PIPE_CONF_CHECK_X(dpll_hw_state.pll9);
9080 		PIPE_CONF_CHECK_X(dpll_hw_state.pll10);
9081 		PIPE_CONF_CHECK_X(dpll_hw_state.pcsdw12);
9082 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_refclkin_ctl);
9083 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_coreclkctl1);
9084 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_clktop2_hsclkctl);
9085 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div0);
9086 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_div1);
9087 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_lf);
9088 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_frac_lock);
9089 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_ssc);
9090 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_bias);
9091 		PIPE_CONF_CHECK_X(dpll_hw_state.mg_pll_tdc_coldst_bias);
9092 
9093 		PIPE_CONF_CHECK_X(dsi_pll.ctrl);
9094 		PIPE_CONF_CHECK_X(dsi_pll.div);
9095 
9096 		if (IS_G4X(dev_priv) || INTEL_GEN(dev_priv) >= 5)
9097 			PIPE_CONF_CHECK_I(pipe_bpp);
9098 
9099 		PIPE_CONF_CHECK_CLOCK_FUZZY(hw.pipe_mode.crtc_clock);
9100 		PIPE_CONF_CHECK_CLOCK_FUZZY(hw.adjusted_mode.crtc_clock);
9101 		PIPE_CONF_CHECK_CLOCK_FUZZY(port_clock);
9102 
9103 		PIPE_CONF_CHECK_I(min_voltage_level);
9104 	}
9105 
9106 	PIPE_CONF_CHECK_X(infoframes.enable);
9107 	PIPE_CONF_CHECK_X(infoframes.gcp);
9108 	PIPE_CONF_CHECK_INFOFRAME(avi);
9109 	PIPE_CONF_CHECK_INFOFRAME(spd);
9110 	PIPE_CONF_CHECK_INFOFRAME(hdmi);
9111 	PIPE_CONF_CHECK_INFOFRAME(drm);
9112 	PIPE_CONF_CHECK_DP_VSC_SDP(vsc);
9113 
9114 	PIPE_CONF_CHECK_X(sync_mode_slaves_mask);
9115 	PIPE_CONF_CHECK_I(master_transcoder);
9116 	PIPE_CONF_CHECK_BOOL(bigjoiner);
9117 	PIPE_CONF_CHECK_BOOL(bigjoiner_slave);
9118 	PIPE_CONF_CHECK_P(bigjoiner_linked_crtc);
9119 
9120 	PIPE_CONF_CHECK_I(dsc.compression_enable);
9121 	PIPE_CONF_CHECK_I(dsc.dsc_split);
9122 	PIPE_CONF_CHECK_I(dsc.compressed_bpp);
9123 
9124 	PIPE_CONF_CHECK_BOOL(splitter.enable);
9125 	PIPE_CONF_CHECK_I(splitter.link_count);
9126 	PIPE_CONF_CHECK_I(splitter.pixel_overlap);
9127 
9128 	PIPE_CONF_CHECK_I(mst_master_transcoder);
9129 
9130 	PIPE_CONF_CHECK_BOOL(vrr.enable);
9131 	PIPE_CONF_CHECK_I(vrr.vmin);
9132 	PIPE_CONF_CHECK_I(vrr.vmax);
9133 	PIPE_CONF_CHECK_I(vrr.flipline);
9134 	PIPE_CONF_CHECK_I(vrr.pipeline_full);
9135 
9136 #undef PIPE_CONF_CHECK_X
9137 #undef PIPE_CONF_CHECK_I
9138 #undef PIPE_CONF_CHECK_BOOL
9139 #undef PIPE_CONF_CHECK_BOOL_INCOMPLETE
9140 #undef PIPE_CONF_CHECK_P
9141 #undef PIPE_CONF_CHECK_FLAGS
9142 #undef PIPE_CONF_CHECK_CLOCK_FUZZY
9143 #undef PIPE_CONF_CHECK_COLOR_LUT
9144 #undef PIPE_CONF_QUIRK
9145 
9146 	return ret;
9147 }
9148 
9149 static void intel_pipe_config_sanity_check(struct drm_i915_private *dev_priv,
9150 					   const struct intel_crtc_state *pipe_config)
9151 {
9152 	if (pipe_config->has_pch_encoder) {
9153 		int fdi_dotclock = intel_dotclock_calculate(intel_fdi_link_freq(dev_priv, pipe_config),
9154 							    &pipe_config->fdi_m_n);
9155 		int dotclock = pipe_config->hw.adjusted_mode.crtc_clock;
9156 
9157 		/*
9158 		 * FDI already provided one idea for the dotclock.
9159 		 * Yell if the encoder disagrees.
9160 		 */
9161 		drm_WARN(&dev_priv->drm,
9162 			 !intel_fuzzy_clock_check(fdi_dotclock, dotclock),
9163 			 "FDI dotclock and encoder dotclock mismatch, fdi: %i, encoder: %i\n",
9164 			 fdi_dotclock, dotclock);
9165 	}
9166 }
9167 
9168 static void verify_wm_state(struct intel_crtc *crtc,
9169 			    struct intel_crtc_state *new_crtc_state)
9170 {
9171 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9172 	struct skl_hw_state {
9173 		struct skl_ddb_entry ddb_y[I915_MAX_PLANES];
9174 		struct skl_ddb_entry ddb_uv[I915_MAX_PLANES];
9175 		struct skl_pipe_wm wm;
9176 	} *hw;
9177 	const struct skl_pipe_wm *sw_wm = &new_crtc_state->wm.skl.optimal;
9178 	int level, max_level = ilk_wm_max_level(dev_priv);
9179 	struct intel_plane *plane;
9180 	u8 hw_enabled_slices;
9181 
9182 	if (INTEL_GEN(dev_priv) < 9 || !new_crtc_state->hw.active)
9183 		return;
9184 
9185 	hw = kzalloc(sizeof(*hw), GFP_KERNEL);
9186 	if (!hw)
9187 		return;
9188 
9189 	skl_pipe_wm_get_hw_state(crtc, &hw->wm);
9190 
9191 	skl_pipe_ddb_get_hw_state(crtc, hw->ddb_y, hw->ddb_uv);
9192 
9193 	hw_enabled_slices = intel_enabled_dbuf_slices_mask(dev_priv);
9194 
9195 	if (INTEL_GEN(dev_priv) >= 11 &&
9196 	    hw_enabled_slices != dev_priv->dbuf.enabled_slices)
9197 		drm_err(&dev_priv->drm,
9198 			"mismatch in DBUF Slices (expected 0x%x, got 0x%x)\n",
9199 			dev_priv->dbuf.enabled_slices,
9200 			hw_enabled_slices);
9201 
9202 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
9203 		const struct skl_ddb_entry *hw_ddb_entry, *sw_ddb_entry;
9204 		const struct skl_wm_level *hw_wm_level, *sw_wm_level;
9205 
9206 		/* Watermarks */
9207 		for (level = 0; level <= max_level; level++) {
9208 			hw_wm_level = &hw->wm.planes[plane->id].wm[level];
9209 			sw_wm_level = skl_plane_wm_level(sw_wm, plane->id, level);
9210 
9211 			if (skl_wm_level_equals(hw_wm_level, sw_wm_level))
9212 				continue;
9213 
9214 			drm_err(&dev_priv->drm,
9215 				"[PLANE:%d:%s] mismatch in WM%d (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
9216 				plane->base.base.id, plane->base.name, level,
9217 				sw_wm_level->enable,
9218 				sw_wm_level->blocks,
9219 				sw_wm_level->lines,
9220 				hw_wm_level->enable,
9221 				hw_wm_level->blocks,
9222 				hw_wm_level->lines);
9223 		}
9224 
9225 		hw_wm_level = &hw->wm.planes[plane->id].trans_wm;
9226 		sw_wm_level = skl_plane_trans_wm(sw_wm, plane->id);
9227 
9228 		if (!skl_wm_level_equals(hw_wm_level, sw_wm_level)) {
9229 			drm_err(&dev_priv->drm,
9230 				"[PLANE:%d:%s] mismatch in trans WM (expected e=%d b=%u l=%u, got e=%d b=%u l=%u)\n",
9231 				plane->base.base.id, plane->base.name,
9232 				sw_wm_level->enable,
9233 				sw_wm_level->blocks,
9234 				sw_wm_level->lines,
9235 				hw_wm_level->enable,
9236 				hw_wm_level->blocks,
9237 				hw_wm_level->lines);
9238 		}
9239 
9240 		/* DDB */
9241 		hw_ddb_entry = &hw->ddb_y[plane->id];
9242 		sw_ddb_entry = &new_crtc_state->wm.skl.plane_ddb_y[plane->id];
9243 
9244 		if (!skl_ddb_entry_equal(hw_ddb_entry, sw_ddb_entry)) {
9245 			drm_err(&dev_priv->drm,
9246 				"[PLANE:%d:%s] mismatch in DDB (expected (%u,%u), found (%u,%u))\n",
9247 				plane->base.base.id, plane->base.name,
9248 				sw_ddb_entry->start, sw_ddb_entry->end,
9249 				hw_ddb_entry->start, hw_ddb_entry->end);
9250 		}
9251 	}
9252 
9253 	kfree(hw);
9254 }
9255 
9256 static void
9257 verify_connector_state(struct intel_atomic_state *state,
9258 		       struct intel_crtc *crtc)
9259 {
9260 	struct drm_connector *connector;
9261 	struct drm_connector_state *new_conn_state;
9262 	int i;
9263 
9264 	for_each_new_connector_in_state(&state->base, connector, new_conn_state, i) {
9265 		struct drm_encoder *encoder = connector->encoder;
9266 		struct intel_crtc_state *crtc_state = NULL;
9267 
9268 		if (new_conn_state->crtc != &crtc->base)
9269 			continue;
9270 
9271 		if (crtc)
9272 			crtc_state = intel_atomic_get_new_crtc_state(state, crtc);
9273 
9274 		intel_connector_verify_state(crtc_state, new_conn_state);
9275 
9276 		I915_STATE_WARN(new_conn_state->best_encoder != encoder,
9277 		     "connector's atomic encoder doesn't match legacy encoder\n");
9278 	}
9279 }
9280 
9281 static void
9282 verify_encoder_state(struct drm_i915_private *dev_priv, struct intel_atomic_state *state)
9283 {
9284 	struct intel_encoder *encoder;
9285 	struct drm_connector *connector;
9286 	struct drm_connector_state *old_conn_state, *new_conn_state;
9287 	int i;
9288 
9289 	for_each_intel_encoder(&dev_priv->drm, encoder) {
9290 		bool enabled = false, found = false;
9291 		enum pipe pipe;
9292 
9293 		drm_dbg_kms(&dev_priv->drm, "[ENCODER:%d:%s]\n",
9294 			    encoder->base.base.id,
9295 			    encoder->base.name);
9296 
9297 		for_each_oldnew_connector_in_state(&state->base, connector, old_conn_state,
9298 						   new_conn_state, i) {
9299 			if (old_conn_state->best_encoder == &encoder->base)
9300 				found = true;
9301 
9302 			if (new_conn_state->best_encoder != &encoder->base)
9303 				continue;
9304 			found = enabled = true;
9305 
9306 			I915_STATE_WARN(new_conn_state->crtc !=
9307 					encoder->base.crtc,
9308 			     "connector's crtc doesn't match encoder crtc\n");
9309 		}
9310 
9311 		if (!found)
9312 			continue;
9313 
9314 		I915_STATE_WARN(!!encoder->base.crtc != enabled,
9315 		     "encoder's enabled state mismatch "
9316 		     "(expected %i, found %i)\n",
9317 		     !!encoder->base.crtc, enabled);
9318 
9319 		if (!encoder->base.crtc) {
9320 			bool active;
9321 
9322 			active = encoder->get_hw_state(encoder, &pipe);
9323 			I915_STATE_WARN(active,
9324 			     "encoder detached but still enabled on pipe %c.\n",
9325 			     pipe_name(pipe));
9326 		}
9327 	}
9328 }
9329 
9330 static void
9331 verify_crtc_state(struct intel_crtc *crtc,
9332 		  struct intel_crtc_state *old_crtc_state,
9333 		  struct intel_crtc_state *new_crtc_state)
9334 {
9335 	struct drm_device *dev = crtc->base.dev;
9336 	struct drm_i915_private *dev_priv = to_i915(dev);
9337 	struct intel_encoder *encoder;
9338 	struct intel_crtc_state *pipe_config = old_crtc_state;
9339 	struct drm_atomic_state *state = old_crtc_state->uapi.state;
9340 	struct intel_crtc *master = crtc;
9341 
9342 	__drm_atomic_helper_crtc_destroy_state(&old_crtc_state->uapi);
9343 	intel_crtc_free_hw_state(old_crtc_state);
9344 	intel_crtc_state_reset(old_crtc_state, crtc);
9345 	old_crtc_state->uapi.state = state;
9346 
9347 	drm_dbg_kms(&dev_priv->drm, "[CRTC:%d:%s]\n", crtc->base.base.id,
9348 		    crtc->base.name);
9349 
9350 	pipe_config->hw.enable = new_crtc_state->hw.enable;
9351 
9352 	intel_crtc_get_pipe_config(pipe_config);
9353 
9354 	/* we keep both pipes enabled on 830 */
9355 	if (IS_I830(dev_priv) && pipe_config->hw.active)
9356 		pipe_config->hw.active = new_crtc_state->hw.active;
9357 
9358 	I915_STATE_WARN(new_crtc_state->hw.active != pipe_config->hw.active,
9359 			"crtc active state doesn't match with hw state "
9360 			"(expected %i, found %i)\n",
9361 			new_crtc_state->hw.active, pipe_config->hw.active);
9362 
9363 	I915_STATE_WARN(crtc->active != new_crtc_state->hw.active,
9364 			"transitional active state does not match atomic hw state "
9365 			"(expected %i, found %i)\n",
9366 			new_crtc_state->hw.active, crtc->active);
9367 
9368 	if (new_crtc_state->bigjoiner_slave)
9369 		master = new_crtc_state->bigjoiner_linked_crtc;
9370 
9371 	for_each_encoder_on_crtc(dev, &master->base, encoder) {
9372 		enum pipe pipe;
9373 		bool active;
9374 
9375 		active = encoder->get_hw_state(encoder, &pipe);
9376 		I915_STATE_WARN(active != new_crtc_state->hw.active,
9377 				"[ENCODER:%i] active %i with crtc active %i\n",
9378 				encoder->base.base.id, active,
9379 				new_crtc_state->hw.active);
9380 
9381 		I915_STATE_WARN(active && master->pipe != pipe,
9382 				"Encoder connected to wrong pipe %c\n",
9383 				pipe_name(pipe));
9384 
9385 		if (active)
9386 			intel_encoder_get_config(encoder, pipe_config);
9387 	}
9388 
9389 	if (!new_crtc_state->hw.active)
9390 		return;
9391 
9392 	intel_pipe_config_sanity_check(dev_priv, pipe_config);
9393 
9394 	if (!intel_pipe_config_compare(new_crtc_state,
9395 				       pipe_config, false)) {
9396 		I915_STATE_WARN(1, "pipe state doesn't match!\n");
9397 		intel_dump_pipe_config(pipe_config, NULL, "[hw state]");
9398 		intel_dump_pipe_config(new_crtc_state, NULL, "[sw state]");
9399 	}
9400 }
9401 
9402 static void
9403 intel_verify_planes(struct intel_atomic_state *state)
9404 {
9405 	struct intel_plane *plane;
9406 	const struct intel_plane_state *plane_state;
9407 	int i;
9408 
9409 	for_each_new_intel_plane_in_state(state, plane,
9410 					  plane_state, i)
9411 		assert_plane(plane, plane_state->planar_slave ||
9412 			     plane_state->uapi.visible);
9413 }
9414 
9415 static void
9416 verify_single_dpll_state(struct drm_i915_private *dev_priv,
9417 			 struct intel_shared_dpll *pll,
9418 			 struct intel_crtc *crtc,
9419 			 struct intel_crtc_state *new_crtc_state)
9420 {
9421 	struct intel_dpll_hw_state dpll_hw_state;
9422 	u8 pipe_mask;
9423 	bool active;
9424 
9425 	memset(&dpll_hw_state, 0, sizeof(dpll_hw_state));
9426 
9427 	drm_dbg_kms(&dev_priv->drm, "%s\n", pll->info->name);
9428 
9429 	active = intel_dpll_get_hw_state(dev_priv, pll, &dpll_hw_state);
9430 
9431 	if (!(pll->info->flags & INTEL_DPLL_ALWAYS_ON)) {
9432 		I915_STATE_WARN(!pll->on && pll->active_mask,
9433 		     "pll in active use but not on in sw tracking\n");
9434 		I915_STATE_WARN(pll->on && !pll->active_mask,
9435 		     "pll is on but not used by any active pipe\n");
9436 		I915_STATE_WARN(pll->on != active,
9437 		     "pll on state mismatch (expected %i, found %i)\n",
9438 		     pll->on, active);
9439 	}
9440 
9441 	if (!crtc) {
9442 		I915_STATE_WARN(pll->active_mask & ~pll->state.pipe_mask,
9443 				"more active pll users than references: 0x%x vs 0x%x\n",
9444 				pll->active_mask, pll->state.pipe_mask);
9445 
9446 		return;
9447 	}
9448 
9449 	pipe_mask = BIT(crtc->pipe);
9450 
9451 	if (new_crtc_state->hw.active)
9452 		I915_STATE_WARN(!(pll->active_mask & pipe_mask),
9453 				"pll active mismatch (expected pipe %c in active mask 0x%x)\n",
9454 				pipe_name(crtc->pipe), pll->active_mask);
9455 	else
9456 		I915_STATE_WARN(pll->active_mask & pipe_mask,
9457 				"pll active mismatch (didn't expect pipe %c in active mask 0x%x)\n",
9458 				pipe_name(crtc->pipe), pll->active_mask);
9459 
9460 	I915_STATE_WARN(!(pll->state.pipe_mask & pipe_mask),
9461 			"pll enabled crtcs mismatch (expected 0x%x in 0x%x)\n",
9462 			pipe_mask, pll->state.pipe_mask);
9463 
9464 	I915_STATE_WARN(pll->on && memcmp(&pll->state.hw_state,
9465 					  &dpll_hw_state,
9466 					  sizeof(dpll_hw_state)),
9467 			"pll hw state mismatch\n");
9468 }
9469 
9470 static void
9471 verify_shared_dpll_state(struct intel_crtc *crtc,
9472 			 struct intel_crtc_state *old_crtc_state,
9473 			 struct intel_crtc_state *new_crtc_state)
9474 {
9475 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9476 
9477 	if (new_crtc_state->shared_dpll)
9478 		verify_single_dpll_state(dev_priv, new_crtc_state->shared_dpll, crtc, new_crtc_state);
9479 
9480 	if (old_crtc_state->shared_dpll &&
9481 	    old_crtc_state->shared_dpll != new_crtc_state->shared_dpll) {
9482 		u8 pipe_mask = BIT(crtc->pipe);
9483 		struct intel_shared_dpll *pll = old_crtc_state->shared_dpll;
9484 
9485 		I915_STATE_WARN(pll->active_mask & pipe_mask,
9486 				"pll active mismatch (didn't expect pipe %c in active mask (0x%x))\n",
9487 				pipe_name(crtc->pipe), pll->active_mask);
9488 		I915_STATE_WARN(pll->state.pipe_mask & pipe_mask,
9489 				"pll enabled crtcs mismatch (found %x in enabled mask (0x%x))\n",
9490 				pipe_name(crtc->pipe), pll->state.pipe_mask);
9491 	}
9492 }
9493 
9494 static void
9495 intel_modeset_verify_crtc(struct intel_crtc *crtc,
9496 			  struct intel_atomic_state *state,
9497 			  struct intel_crtc_state *old_crtc_state,
9498 			  struct intel_crtc_state *new_crtc_state)
9499 {
9500 	if (!intel_crtc_needs_modeset(new_crtc_state) && !new_crtc_state->update_pipe)
9501 		return;
9502 
9503 	verify_wm_state(crtc, new_crtc_state);
9504 	verify_connector_state(state, crtc);
9505 	verify_crtc_state(crtc, old_crtc_state, new_crtc_state);
9506 	verify_shared_dpll_state(crtc, old_crtc_state, new_crtc_state);
9507 }
9508 
9509 static void
9510 verify_disabled_dpll_state(struct drm_i915_private *dev_priv)
9511 {
9512 	int i;
9513 
9514 	for (i = 0; i < dev_priv->dpll.num_shared_dpll; i++)
9515 		verify_single_dpll_state(dev_priv,
9516 					 &dev_priv->dpll.shared_dplls[i],
9517 					 NULL, NULL);
9518 }
9519 
9520 static void
9521 intel_modeset_verify_disabled(struct drm_i915_private *dev_priv,
9522 			      struct intel_atomic_state *state)
9523 {
9524 	verify_encoder_state(dev_priv, state);
9525 	verify_connector_state(state, NULL);
9526 	verify_disabled_dpll_state(dev_priv);
9527 }
9528 
9529 static void
9530 intel_crtc_update_active_timings(const struct intel_crtc_state *crtc_state)
9531 {
9532 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
9533 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
9534 	struct drm_display_mode adjusted_mode =
9535 		crtc_state->hw.adjusted_mode;
9536 
9537 	if (crtc_state->vrr.enable) {
9538 		adjusted_mode.crtc_vtotal = crtc_state->vrr.vmax;
9539 		adjusted_mode.crtc_vblank_end = crtc_state->vrr.vmax;
9540 		adjusted_mode.crtc_vblank_start = intel_vrr_vmin_vblank_start(crtc_state);
9541 		crtc->vmax_vblank_start = intel_vrr_vmax_vblank_start(crtc_state);
9542 	}
9543 
9544 	drm_calc_timestamping_constants(&crtc->base, &adjusted_mode);
9545 
9546 	crtc->mode_flags = crtc_state->mode_flags;
9547 
9548 	/*
9549 	 * The scanline counter increments at the leading edge of hsync.
9550 	 *
9551 	 * On most platforms it starts counting from vtotal-1 on the
9552 	 * first active line. That means the scanline counter value is
9553 	 * always one less than what we would expect. Ie. just after
9554 	 * start of vblank, which also occurs at start of hsync (on the
9555 	 * last active line), the scanline counter will read vblank_start-1.
9556 	 *
9557 	 * On gen2 the scanline counter starts counting from 1 instead
9558 	 * of vtotal-1, so we have to subtract one (or rather add vtotal-1
9559 	 * to keep the value positive), instead of adding one.
9560 	 *
9561 	 * On HSW+ the behaviour of the scanline counter depends on the output
9562 	 * type. For DP ports it behaves like most other platforms, but on HDMI
9563 	 * there's an extra 1 line difference. So we need to add two instead of
9564 	 * one to the value.
9565 	 *
9566 	 * On VLV/CHV DSI the scanline counter would appear to increment
9567 	 * approx. 1/3 of a scanline before start of vblank. Unfortunately
9568 	 * that means we can't tell whether we're in vblank or not while
9569 	 * we're on that particular line. We must still set scanline_offset
9570 	 * to 1 so that the vblank timestamps come out correct when we query
9571 	 * the scanline counter from within the vblank interrupt handler.
9572 	 * However if queried just before the start of vblank we'll get an
9573 	 * answer that's slightly in the future.
9574 	 */
9575 	if (IS_GEN(dev_priv, 2)) {
9576 		int vtotal;
9577 
9578 		vtotal = adjusted_mode.crtc_vtotal;
9579 		if (adjusted_mode.flags & DRM_MODE_FLAG_INTERLACE)
9580 			vtotal /= 2;
9581 
9582 		crtc->scanline_offset = vtotal - 1;
9583 	} else if (HAS_DDI(dev_priv) &&
9584 		   intel_crtc_has_type(crtc_state, INTEL_OUTPUT_HDMI)) {
9585 		crtc->scanline_offset = 2;
9586 	} else {
9587 		crtc->scanline_offset = 1;
9588 	}
9589 }
9590 
9591 static void intel_modeset_clear_plls(struct intel_atomic_state *state)
9592 {
9593 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9594 	struct intel_crtc_state *new_crtc_state;
9595 	struct intel_crtc *crtc;
9596 	int i;
9597 
9598 	if (!dev_priv->display.crtc_compute_clock)
9599 		return;
9600 
9601 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9602 		if (!intel_crtc_needs_modeset(new_crtc_state))
9603 			continue;
9604 
9605 		intel_release_shared_dplls(state, crtc);
9606 	}
9607 }
9608 
9609 /*
9610  * This implements the workaround described in the "notes" section of the mode
9611  * set sequence documentation. When going from no pipes or single pipe to
9612  * multiple pipes, and planes are enabled after the pipe, we need to wait at
9613  * least 2 vblanks on the first pipe before enabling planes on the second pipe.
9614  */
9615 static int hsw_mode_set_planes_workaround(struct intel_atomic_state *state)
9616 {
9617 	struct intel_crtc_state *crtc_state;
9618 	struct intel_crtc *crtc;
9619 	struct intel_crtc_state *first_crtc_state = NULL;
9620 	struct intel_crtc_state *other_crtc_state = NULL;
9621 	enum pipe first_pipe = INVALID_PIPE, enabled_pipe = INVALID_PIPE;
9622 	int i;
9623 
9624 	/* look at all crtc's that are going to be enabled in during modeset */
9625 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9626 		if (!crtc_state->hw.active ||
9627 		    !intel_crtc_needs_modeset(crtc_state))
9628 			continue;
9629 
9630 		if (first_crtc_state) {
9631 			other_crtc_state = crtc_state;
9632 			break;
9633 		} else {
9634 			first_crtc_state = crtc_state;
9635 			first_pipe = crtc->pipe;
9636 		}
9637 	}
9638 
9639 	/* No workaround needed? */
9640 	if (!first_crtc_state)
9641 		return 0;
9642 
9643 	/* w/a possibly needed, check how many crtc's are already enabled. */
9644 	for_each_intel_crtc(state->base.dev, crtc) {
9645 		crtc_state = intel_atomic_get_crtc_state(&state->base, crtc);
9646 		if (IS_ERR(crtc_state))
9647 			return PTR_ERR(crtc_state);
9648 
9649 		crtc_state->hsw_workaround_pipe = INVALID_PIPE;
9650 
9651 		if (!crtc_state->hw.active ||
9652 		    intel_crtc_needs_modeset(crtc_state))
9653 			continue;
9654 
9655 		/* 2 or more enabled crtcs means no need for w/a */
9656 		if (enabled_pipe != INVALID_PIPE)
9657 			return 0;
9658 
9659 		enabled_pipe = crtc->pipe;
9660 	}
9661 
9662 	if (enabled_pipe != INVALID_PIPE)
9663 		first_crtc_state->hsw_workaround_pipe = enabled_pipe;
9664 	else if (other_crtc_state)
9665 		other_crtc_state->hsw_workaround_pipe = first_pipe;
9666 
9667 	return 0;
9668 }
9669 
9670 u8 intel_calc_active_pipes(struct intel_atomic_state *state,
9671 			   u8 active_pipes)
9672 {
9673 	const struct intel_crtc_state *crtc_state;
9674 	struct intel_crtc *crtc;
9675 	int i;
9676 
9677 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9678 		if (crtc_state->hw.active)
9679 			active_pipes |= BIT(crtc->pipe);
9680 		else
9681 			active_pipes &= ~BIT(crtc->pipe);
9682 	}
9683 
9684 	return active_pipes;
9685 }
9686 
9687 static int intel_modeset_checks(struct intel_atomic_state *state)
9688 {
9689 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9690 
9691 	state->modeset = true;
9692 
9693 	if (IS_HASWELL(dev_priv))
9694 		return hsw_mode_set_planes_workaround(state);
9695 
9696 	return 0;
9697 }
9698 
9699 /*
9700  * Handle calculation of various watermark data at the end of the atomic check
9701  * phase.  The code here should be run after the per-crtc and per-plane 'check'
9702  * handlers to ensure that all derived state has been updated.
9703  */
9704 static int calc_watermark_data(struct intel_atomic_state *state)
9705 {
9706 	struct drm_device *dev = state->base.dev;
9707 	struct drm_i915_private *dev_priv = to_i915(dev);
9708 
9709 	/* Is there platform-specific watermark information to calculate? */
9710 	if (dev_priv->display.compute_global_watermarks)
9711 		return dev_priv->display.compute_global_watermarks(state);
9712 
9713 	return 0;
9714 }
9715 
9716 static void intel_crtc_check_fastset(const struct intel_crtc_state *old_crtc_state,
9717 				     struct intel_crtc_state *new_crtc_state)
9718 {
9719 	if (!intel_pipe_config_compare(old_crtc_state, new_crtc_state, true))
9720 		return;
9721 
9722 	new_crtc_state->uapi.mode_changed = false;
9723 	new_crtc_state->update_pipe = true;
9724 }
9725 
9726 static void intel_crtc_copy_fastset(const struct intel_crtc_state *old_crtc_state,
9727 				    struct intel_crtc_state *new_crtc_state)
9728 {
9729 	/*
9730 	 * If we're not doing the full modeset we want to
9731 	 * keep the current M/N values as they may be
9732 	 * sufficiently different to the computed values
9733 	 * to cause problems.
9734 	 *
9735 	 * FIXME: should really copy more fuzzy state here
9736 	 */
9737 	new_crtc_state->fdi_m_n = old_crtc_state->fdi_m_n;
9738 	new_crtc_state->dp_m_n = old_crtc_state->dp_m_n;
9739 	new_crtc_state->dp_m2_n2 = old_crtc_state->dp_m2_n2;
9740 	new_crtc_state->has_drrs = old_crtc_state->has_drrs;
9741 }
9742 
9743 static int intel_crtc_add_planes_to_state(struct intel_atomic_state *state,
9744 					  struct intel_crtc *crtc,
9745 					  u8 plane_ids_mask)
9746 {
9747 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9748 	struct intel_plane *plane;
9749 
9750 	for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
9751 		struct intel_plane_state *plane_state;
9752 
9753 		if ((plane_ids_mask & BIT(plane->id)) == 0)
9754 			continue;
9755 
9756 		plane_state = intel_atomic_get_plane_state(state, plane);
9757 		if (IS_ERR(plane_state))
9758 			return PTR_ERR(plane_state);
9759 	}
9760 
9761 	return 0;
9762 }
9763 
9764 int intel_atomic_add_affected_planes(struct intel_atomic_state *state,
9765 				     struct intel_crtc *crtc)
9766 {
9767 	const struct intel_crtc_state *old_crtc_state =
9768 		intel_atomic_get_old_crtc_state(state, crtc);
9769 	const struct intel_crtc_state *new_crtc_state =
9770 		intel_atomic_get_new_crtc_state(state, crtc);
9771 
9772 	return intel_crtc_add_planes_to_state(state, crtc,
9773 					      old_crtc_state->enabled_planes |
9774 					      new_crtc_state->enabled_planes);
9775 }
9776 
9777 static bool active_planes_affects_min_cdclk(struct drm_i915_private *dev_priv)
9778 {
9779 	/* See {hsw,vlv,ivb}_plane_ratio() */
9780 	return IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv) ||
9781 		IS_CHERRYVIEW(dev_priv) || IS_VALLEYVIEW(dev_priv) ||
9782 		IS_IVYBRIDGE(dev_priv) || (INTEL_GEN(dev_priv) >= 11);
9783 }
9784 
9785 static int intel_crtc_add_bigjoiner_planes(struct intel_atomic_state *state,
9786 					   struct intel_crtc *crtc,
9787 					   struct intel_crtc *other)
9788 {
9789 	const struct intel_plane_state *plane_state;
9790 	struct intel_plane *plane;
9791 	u8 plane_ids = 0;
9792 	int i;
9793 
9794 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9795 		if (plane->pipe == crtc->pipe)
9796 			plane_ids |= BIT(plane->id);
9797 	}
9798 
9799 	return intel_crtc_add_planes_to_state(state, other, plane_ids);
9800 }
9801 
9802 static int intel_bigjoiner_add_affected_planes(struct intel_atomic_state *state)
9803 {
9804 	const struct intel_crtc_state *crtc_state;
9805 	struct intel_crtc *crtc;
9806 	int i;
9807 
9808 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9809 		int ret;
9810 
9811 		if (!crtc_state->bigjoiner)
9812 			continue;
9813 
9814 		ret = intel_crtc_add_bigjoiner_planes(state, crtc,
9815 						      crtc_state->bigjoiner_linked_crtc);
9816 		if (ret)
9817 			return ret;
9818 	}
9819 
9820 	return 0;
9821 }
9822 
9823 static int intel_atomic_check_planes(struct intel_atomic_state *state)
9824 {
9825 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9826 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
9827 	struct intel_plane_state *plane_state;
9828 	struct intel_plane *plane;
9829 	struct intel_crtc *crtc;
9830 	int i, ret;
9831 
9832 	ret = icl_add_linked_planes(state);
9833 	if (ret)
9834 		return ret;
9835 
9836 	ret = intel_bigjoiner_add_affected_planes(state);
9837 	if (ret)
9838 		return ret;
9839 
9840 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9841 		ret = intel_plane_atomic_check(state, plane);
9842 		if (ret) {
9843 			drm_dbg_atomic(&dev_priv->drm,
9844 				       "[PLANE:%d:%s] atomic driver check failed\n",
9845 				       plane->base.base.id, plane->base.name);
9846 			return ret;
9847 		}
9848 	}
9849 
9850 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
9851 					    new_crtc_state, i) {
9852 		u8 old_active_planes, new_active_planes;
9853 
9854 		ret = icl_check_nv12_planes(new_crtc_state);
9855 		if (ret)
9856 			return ret;
9857 
9858 		/*
9859 		 * On some platforms the number of active planes affects
9860 		 * the planes' minimum cdclk calculation. Add such planes
9861 		 * to the state before we compute the minimum cdclk.
9862 		 */
9863 		if (!active_planes_affects_min_cdclk(dev_priv))
9864 			continue;
9865 
9866 		old_active_planes = old_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
9867 		new_active_planes = new_crtc_state->active_planes & ~BIT(PLANE_CURSOR);
9868 
9869 		/*
9870 		 * Not only the number of planes, but if the plane configuration had
9871 		 * changed might already mean we need to recompute min CDCLK,
9872 		 * because different planes might consume different amount of Dbuf bandwidth
9873 		 * according to formula: Bw per plane = Pixel rate * bpp * pipe/plane scale factor
9874 		 */
9875 		if (old_active_planes == new_active_planes)
9876 			continue;
9877 
9878 		ret = intel_crtc_add_planes_to_state(state, crtc, new_active_planes);
9879 		if (ret)
9880 			return ret;
9881 	}
9882 
9883 	return 0;
9884 }
9885 
9886 static int intel_atomic_check_cdclk(struct intel_atomic_state *state,
9887 				    bool *need_cdclk_calc)
9888 {
9889 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9890 	const struct intel_cdclk_state *old_cdclk_state;
9891 	const struct intel_cdclk_state *new_cdclk_state;
9892 	struct intel_plane_state *plane_state;
9893 	struct intel_bw_state *new_bw_state;
9894 	struct intel_plane *plane;
9895 	int min_cdclk = 0;
9896 	enum pipe pipe;
9897 	int ret;
9898 	int i;
9899 	/*
9900 	 * active_planes bitmask has been updated, and potentially
9901 	 * affected planes are part of the state. We can now
9902 	 * compute the minimum cdclk for each plane.
9903 	 */
9904 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
9905 		ret = intel_plane_calc_min_cdclk(state, plane, need_cdclk_calc);
9906 		if (ret)
9907 			return ret;
9908 	}
9909 
9910 	old_cdclk_state = intel_atomic_get_old_cdclk_state(state);
9911 	new_cdclk_state = intel_atomic_get_new_cdclk_state(state);
9912 
9913 	if (new_cdclk_state &&
9914 	    old_cdclk_state->force_min_cdclk != new_cdclk_state->force_min_cdclk)
9915 		*need_cdclk_calc = true;
9916 
9917 	ret = dev_priv->display.bw_calc_min_cdclk(state);
9918 	if (ret)
9919 		return ret;
9920 
9921 	new_bw_state = intel_atomic_get_new_bw_state(state);
9922 
9923 	if (!new_cdclk_state || !new_bw_state)
9924 		return 0;
9925 
9926 	for_each_pipe(dev_priv, pipe) {
9927 		min_cdclk = max(new_cdclk_state->min_cdclk[pipe], min_cdclk);
9928 
9929 		/*
9930 		 * Currently do this change only if we need to increase
9931 		 */
9932 		if (new_bw_state->min_cdclk > min_cdclk)
9933 			*need_cdclk_calc = true;
9934 	}
9935 
9936 	return 0;
9937 }
9938 
9939 static int intel_atomic_check_crtcs(struct intel_atomic_state *state)
9940 {
9941 	struct intel_crtc_state *crtc_state;
9942 	struct intel_crtc *crtc;
9943 	int i;
9944 
9945 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
9946 		struct drm_i915_private *i915 = to_i915(crtc->base.dev);
9947 		int ret;
9948 
9949 		ret = intel_crtc_atomic_check(state, crtc);
9950 		if (ret) {
9951 			drm_dbg_atomic(&i915->drm,
9952 				       "[CRTC:%d:%s] atomic driver check failed\n",
9953 				       crtc->base.base.id, crtc->base.name);
9954 			return ret;
9955 		}
9956 	}
9957 
9958 	return 0;
9959 }
9960 
9961 static bool intel_cpu_transcoders_need_modeset(struct intel_atomic_state *state,
9962 					       u8 transcoders)
9963 {
9964 	const struct intel_crtc_state *new_crtc_state;
9965 	struct intel_crtc *crtc;
9966 	int i;
9967 
9968 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
9969 		if (new_crtc_state->hw.enable &&
9970 		    transcoders & BIT(new_crtc_state->cpu_transcoder) &&
9971 		    intel_crtc_needs_modeset(new_crtc_state))
9972 			return true;
9973 	}
9974 
9975 	return false;
9976 }
9977 
9978 static int intel_atomic_check_bigjoiner(struct intel_atomic_state *state,
9979 					struct intel_crtc *crtc,
9980 					struct intel_crtc_state *old_crtc_state,
9981 					struct intel_crtc_state *new_crtc_state)
9982 {
9983 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
9984 	struct intel_crtc_state *slave_crtc_state, *master_crtc_state;
9985 	struct intel_crtc *slave, *master;
9986 
9987 	/* slave being enabled, is master is still claiming this crtc? */
9988 	if (old_crtc_state->bigjoiner_slave) {
9989 		slave = crtc;
9990 		master = old_crtc_state->bigjoiner_linked_crtc;
9991 		master_crtc_state = intel_atomic_get_new_crtc_state(state, master);
9992 		if (!master_crtc_state || !intel_crtc_needs_modeset(master_crtc_state))
9993 			goto claimed;
9994 	}
9995 
9996 	if (!new_crtc_state->bigjoiner)
9997 		return 0;
9998 
9999 	if (1 + crtc->pipe >= INTEL_NUM_PIPES(dev_priv)) {
10000 		DRM_DEBUG_KMS("[CRTC:%d:%s] Big joiner configuration requires "
10001 			      "CRTC + 1 to be used, doesn't exist\n",
10002 			      crtc->base.base.id, crtc->base.name);
10003 		return -EINVAL;
10004 	}
10005 
10006 	slave = new_crtc_state->bigjoiner_linked_crtc =
10007 		intel_get_crtc_for_pipe(dev_priv, crtc->pipe + 1);
10008 	slave_crtc_state = intel_atomic_get_crtc_state(&state->base, slave);
10009 	master = crtc;
10010 	if (IS_ERR(slave_crtc_state))
10011 		return PTR_ERR(slave_crtc_state);
10012 
10013 	/* master being enabled, slave was already configured? */
10014 	if (slave_crtc_state->uapi.enable)
10015 		goto claimed;
10016 
10017 	DRM_DEBUG_KMS("[CRTC:%d:%s] Used as slave for big joiner\n",
10018 		      slave->base.base.id, slave->base.name);
10019 
10020 	return copy_bigjoiner_crtc_state(slave_crtc_state, new_crtc_state);
10021 
10022 claimed:
10023 	DRM_DEBUG_KMS("[CRTC:%d:%s] Slave is enabled as normal CRTC, but "
10024 		      "[CRTC:%d:%s] claiming this CRTC for bigjoiner.\n",
10025 		      slave->base.base.id, slave->base.name,
10026 		      master->base.base.id, master->base.name);
10027 	return -EINVAL;
10028 }
10029 
10030 static void kill_bigjoiner_slave(struct intel_atomic_state *state,
10031 				 struct intel_crtc_state *master_crtc_state)
10032 {
10033 	struct intel_crtc_state *slave_crtc_state =
10034 		intel_atomic_get_new_crtc_state(state, master_crtc_state->bigjoiner_linked_crtc);
10035 
10036 	slave_crtc_state->bigjoiner = master_crtc_state->bigjoiner = false;
10037 	slave_crtc_state->bigjoiner_slave = master_crtc_state->bigjoiner_slave = false;
10038 	slave_crtc_state->bigjoiner_linked_crtc = master_crtc_state->bigjoiner_linked_crtc = NULL;
10039 	intel_crtc_copy_uapi_to_hw_state(state, slave_crtc_state);
10040 }
10041 
10042 /**
10043  * DOC: asynchronous flip implementation
10044  *
10045  * Asynchronous page flip is the implementation for the DRM_MODE_PAGE_FLIP_ASYNC
10046  * flag. Currently async flip is only supported via the drmModePageFlip IOCTL.
10047  * Correspondingly, support is currently added for primary plane only.
10048  *
10049  * Async flip can only change the plane surface address, so anything else
10050  * changing is rejected from the intel_atomic_check_async() function.
10051  * Once this check is cleared, flip done interrupt is enabled using
10052  * the intel_crtc_enable_flip_done() function.
10053  *
10054  * As soon as the surface address register is written, flip done interrupt is
10055  * generated and the requested events are sent to the usersapce in the interrupt
10056  * handler itself. The timestamp and sequence sent during the flip done event
10057  * correspond to the last vblank and have no relation to the actual time when
10058  * the flip done event was sent.
10059  */
10060 static int intel_atomic_check_async(struct intel_atomic_state *state)
10061 {
10062 	struct drm_i915_private *i915 = to_i915(state->base.dev);
10063 	const struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10064 	const struct intel_plane_state *new_plane_state, *old_plane_state;
10065 	struct intel_crtc *crtc;
10066 	struct intel_plane *plane;
10067 	int i;
10068 
10069 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10070 					    new_crtc_state, i) {
10071 		if (intel_crtc_needs_modeset(new_crtc_state)) {
10072 			drm_dbg_kms(&i915->drm, "Modeset Required. Async flip not supported\n");
10073 			return -EINVAL;
10074 		}
10075 
10076 		if (!new_crtc_state->hw.active) {
10077 			drm_dbg_kms(&i915->drm, "CRTC inactive\n");
10078 			return -EINVAL;
10079 		}
10080 		if (old_crtc_state->active_planes != new_crtc_state->active_planes) {
10081 			drm_dbg_kms(&i915->drm,
10082 				    "Active planes cannot be changed during async flip\n");
10083 			return -EINVAL;
10084 		}
10085 	}
10086 
10087 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
10088 					     new_plane_state, i) {
10089 		/*
10090 		 * TODO: Async flip is only supported through the page flip IOCTL
10091 		 * as of now. So support currently added for primary plane only.
10092 		 * Support for other planes on platforms on which supports
10093 		 * this(vlv/chv and icl+) should be added when async flip is
10094 		 * enabled in the atomic IOCTL path.
10095 		 */
10096 		if (!plane->async_flip)
10097 			return -EINVAL;
10098 
10099 		/*
10100 		 * FIXME: This check is kept generic for all platforms.
10101 		 * Need to verify this for all gen9 and gen10 platforms to enable
10102 		 * this selectively if required.
10103 		 */
10104 		switch (new_plane_state->hw.fb->modifier) {
10105 		case I915_FORMAT_MOD_X_TILED:
10106 		case I915_FORMAT_MOD_Y_TILED:
10107 		case I915_FORMAT_MOD_Yf_TILED:
10108 			break;
10109 		default:
10110 			drm_dbg_kms(&i915->drm,
10111 				    "Linear memory/CCS does not support async flips\n");
10112 			return -EINVAL;
10113 		}
10114 
10115 		if (old_plane_state->color_plane[0].stride !=
10116 		    new_plane_state->color_plane[0].stride) {
10117 			drm_dbg_kms(&i915->drm, "Stride cannot be changed in async flip\n");
10118 			return -EINVAL;
10119 		}
10120 
10121 		if (old_plane_state->hw.fb->modifier !=
10122 		    new_plane_state->hw.fb->modifier) {
10123 			drm_dbg_kms(&i915->drm,
10124 				    "Framebuffer modifiers cannot be changed in async flip\n");
10125 			return -EINVAL;
10126 		}
10127 
10128 		if (old_plane_state->hw.fb->format !=
10129 		    new_plane_state->hw.fb->format) {
10130 			drm_dbg_kms(&i915->drm,
10131 				    "Framebuffer format cannot be changed in async flip\n");
10132 			return -EINVAL;
10133 		}
10134 
10135 		if (old_plane_state->hw.rotation !=
10136 		    new_plane_state->hw.rotation) {
10137 			drm_dbg_kms(&i915->drm, "Rotation cannot be changed in async flip\n");
10138 			return -EINVAL;
10139 		}
10140 
10141 		if (!drm_rect_equals(&old_plane_state->uapi.src, &new_plane_state->uapi.src) ||
10142 		    !drm_rect_equals(&old_plane_state->uapi.dst, &new_plane_state->uapi.dst)) {
10143 			drm_dbg_kms(&i915->drm,
10144 				    "Plane size/co-ordinates cannot be changed in async flip\n");
10145 			return -EINVAL;
10146 		}
10147 
10148 		if (old_plane_state->hw.alpha != new_plane_state->hw.alpha) {
10149 			drm_dbg_kms(&i915->drm, "Alpha value cannot be changed in async flip\n");
10150 			return -EINVAL;
10151 		}
10152 
10153 		if (old_plane_state->hw.pixel_blend_mode !=
10154 		    new_plane_state->hw.pixel_blend_mode) {
10155 			drm_dbg_kms(&i915->drm,
10156 				    "Pixel blend mode cannot be changed in async flip\n");
10157 			return -EINVAL;
10158 		}
10159 
10160 		if (old_plane_state->hw.color_encoding != new_plane_state->hw.color_encoding) {
10161 			drm_dbg_kms(&i915->drm,
10162 				    "Color encoding cannot be changed in async flip\n");
10163 			return -EINVAL;
10164 		}
10165 
10166 		if (old_plane_state->hw.color_range != new_plane_state->hw.color_range) {
10167 			drm_dbg_kms(&i915->drm, "Color range cannot be changed in async flip\n");
10168 			return -EINVAL;
10169 		}
10170 	}
10171 
10172 	return 0;
10173 }
10174 
10175 static int intel_bigjoiner_add_affected_crtcs(struct intel_atomic_state *state)
10176 {
10177 	struct intel_crtc_state *crtc_state;
10178 	struct intel_crtc *crtc;
10179 	int i;
10180 
10181 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
10182 		struct intel_crtc_state *linked_crtc_state;
10183 		struct intel_crtc *linked_crtc;
10184 		int ret;
10185 
10186 		if (!crtc_state->bigjoiner)
10187 			continue;
10188 
10189 		linked_crtc = crtc_state->bigjoiner_linked_crtc;
10190 		linked_crtc_state = intel_atomic_get_crtc_state(&state->base, linked_crtc);
10191 		if (IS_ERR(linked_crtc_state))
10192 			return PTR_ERR(linked_crtc_state);
10193 
10194 		if (!intel_crtc_needs_modeset(crtc_state))
10195 			continue;
10196 
10197 		linked_crtc_state->uapi.mode_changed = true;
10198 
10199 		ret = drm_atomic_add_affected_connectors(&state->base,
10200 							 &linked_crtc->base);
10201 		if (ret)
10202 			return ret;
10203 
10204 		ret = intel_atomic_add_affected_planes(state, linked_crtc);
10205 		if (ret)
10206 			return ret;
10207 	}
10208 
10209 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
10210 		/* Kill old bigjoiner link, we may re-establish afterwards */
10211 		if (intel_crtc_needs_modeset(crtc_state) &&
10212 		    crtc_state->bigjoiner && !crtc_state->bigjoiner_slave)
10213 			kill_bigjoiner_slave(state, crtc_state);
10214 	}
10215 
10216 	return 0;
10217 }
10218 
10219 /**
10220  * intel_atomic_check - validate state object
10221  * @dev: drm device
10222  * @_state: state to validate
10223  */
10224 static int intel_atomic_check(struct drm_device *dev,
10225 			      struct drm_atomic_state *_state)
10226 {
10227 	struct drm_i915_private *dev_priv = to_i915(dev);
10228 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
10229 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10230 	struct intel_crtc *crtc;
10231 	int ret, i;
10232 	bool any_ms = false;
10233 
10234 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10235 					    new_crtc_state, i) {
10236 		if (new_crtc_state->inherited != old_crtc_state->inherited)
10237 			new_crtc_state->uapi.mode_changed = true;
10238 	}
10239 
10240 	intel_vrr_check_modeset(state);
10241 
10242 	ret = drm_atomic_helper_check_modeset(dev, &state->base);
10243 	if (ret)
10244 		goto fail;
10245 
10246 	ret = intel_bigjoiner_add_affected_crtcs(state);
10247 	if (ret)
10248 		goto fail;
10249 
10250 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10251 					    new_crtc_state, i) {
10252 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
10253 			/* Light copy */
10254 			intel_crtc_copy_uapi_to_hw_state_nomodeset(state, new_crtc_state);
10255 
10256 			continue;
10257 		}
10258 
10259 		if (!new_crtc_state->uapi.enable) {
10260 			if (!new_crtc_state->bigjoiner_slave) {
10261 				intel_crtc_copy_uapi_to_hw_state(state, new_crtc_state);
10262 				any_ms = true;
10263 			}
10264 			continue;
10265 		}
10266 
10267 		ret = intel_crtc_prepare_cleared_state(state, new_crtc_state);
10268 		if (ret)
10269 			goto fail;
10270 
10271 		ret = intel_modeset_pipe_config(state, new_crtc_state);
10272 		if (ret)
10273 			goto fail;
10274 
10275 		ret = intel_atomic_check_bigjoiner(state, crtc, old_crtc_state,
10276 						   new_crtc_state);
10277 		if (ret)
10278 			goto fail;
10279 	}
10280 
10281 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10282 					    new_crtc_state, i) {
10283 		if (!intel_crtc_needs_modeset(new_crtc_state))
10284 			continue;
10285 
10286 		ret = intel_modeset_pipe_config_late(new_crtc_state);
10287 		if (ret)
10288 			goto fail;
10289 
10290 		intel_crtc_check_fastset(old_crtc_state, new_crtc_state);
10291 	}
10292 
10293 	/**
10294 	 * Check if fastset is allowed by external dependencies like other
10295 	 * pipes and transcoders.
10296 	 *
10297 	 * Right now it only forces a fullmodeset when the MST master
10298 	 * transcoder did not changed but the pipe of the master transcoder
10299 	 * needs a fullmodeset so all slaves also needs to do a fullmodeset or
10300 	 * in case of port synced crtcs, if one of the synced crtcs
10301 	 * needs a full modeset, all other synced crtcs should be
10302 	 * forced a full modeset.
10303 	 */
10304 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10305 		if (!new_crtc_state->hw.enable || intel_crtc_needs_modeset(new_crtc_state))
10306 			continue;
10307 
10308 		if (intel_dp_mst_is_slave_trans(new_crtc_state)) {
10309 			enum transcoder master = new_crtc_state->mst_master_transcoder;
10310 
10311 			if (intel_cpu_transcoders_need_modeset(state, BIT(master))) {
10312 				new_crtc_state->uapi.mode_changed = true;
10313 				new_crtc_state->update_pipe = false;
10314 			}
10315 		}
10316 
10317 		if (is_trans_port_sync_mode(new_crtc_state)) {
10318 			u8 trans = new_crtc_state->sync_mode_slaves_mask;
10319 
10320 			if (new_crtc_state->master_transcoder != INVALID_TRANSCODER)
10321 				trans |= BIT(new_crtc_state->master_transcoder);
10322 
10323 			if (intel_cpu_transcoders_need_modeset(state, trans)) {
10324 				new_crtc_state->uapi.mode_changed = true;
10325 				new_crtc_state->update_pipe = false;
10326 			}
10327 		}
10328 
10329 		if (new_crtc_state->bigjoiner) {
10330 			struct intel_crtc_state *linked_crtc_state =
10331 				intel_atomic_get_new_crtc_state(state, new_crtc_state->bigjoiner_linked_crtc);
10332 
10333 			if (intel_crtc_needs_modeset(linked_crtc_state)) {
10334 				new_crtc_state->uapi.mode_changed = true;
10335 				new_crtc_state->update_pipe = false;
10336 			}
10337 		}
10338 	}
10339 
10340 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10341 					    new_crtc_state, i) {
10342 		if (intel_crtc_needs_modeset(new_crtc_state)) {
10343 			any_ms = true;
10344 			continue;
10345 		}
10346 
10347 		if (!new_crtc_state->update_pipe)
10348 			continue;
10349 
10350 		intel_crtc_copy_fastset(old_crtc_state, new_crtc_state);
10351 	}
10352 
10353 	if (any_ms && !check_digital_port_conflicts(state)) {
10354 		drm_dbg_kms(&dev_priv->drm,
10355 			    "rejecting conflicting digital port configuration\n");
10356 		ret = -EINVAL;
10357 		goto fail;
10358 	}
10359 
10360 	ret = drm_dp_mst_atomic_check(&state->base);
10361 	if (ret)
10362 		goto fail;
10363 
10364 	ret = intel_atomic_check_planes(state);
10365 	if (ret)
10366 		goto fail;
10367 
10368 	intel_fbc_choose_crtc(dev_priv, state);
10369 	ret = calc_watermark_data(state);
10370 	if (ret)
10371 		goto fail;
10372 
10373 	ret = intel_bw_atomic_check(state);
10374 	if (ret)
10375 		goto fail;
10376 
10377 	ret = intel_atomic_check_cdclk(state, &any_ms);
10378 	if (ret)
10379 		goto fail;
10380 
10381 	if (any_ms) {
10382 		ret = intel_modeset_checks(state);
10383 		if (ret)
10384 			goto fail;
10385 
10386 		ret = intel_modeset_calc_cdclk(state);
10387 		if (ret)
10388 			return ret;
10389 
10390 		intel_modeset_clear_plls(state);
10391 	}
10392 
10393 	ret = intel_atomic_check_crtcs(state);
10394 	if (ret)
10395 		goto fail;
10396 
10397 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10398 					    new_crtc_state, i) {
10399 		if (new_crtc_state->uapi.async_flip) {
10400 			ret = intel_atomic_check_async(state);
10401 			if (ret)
10402 				goto fail;
10403 		}
10404 
10405 		if (!intel_crtc_needs_modeset(new_crtc_state) &&
10406 		    !new_crtc_state->update_pipe)
10407 			continue;
10408 
10409 		intel_dump_pipe_config(new_crtc_state, state,
10410 				       intel_crtc_needs_modeset(new_crtc_state) ?
10411 				       "[modeset]" : "[fastset]");
10412 	}
10413 
10414 	return 0;
10415 
10416  fail:
10417 	if (ret == -EDEADLK)
10418 		return ret;
10419 
10420 	/*
10421 	 * FIXME would probably be nice to know which crtc specifically
10422 	 * caused the failure, in cases where we can pinpoint it.
10423 	 */
10424 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10425 					    new_crtc_state, i)
10426 		intel_dump_pipe_config(new_crtc_state, state, "[failed]");
10427 
10428 	return ret;
10429 }
10430 
10431 static int intel_atomic_prepare_commit(struct intel_atomic_state *state)
10432 {
10433 	struct intel_crtc_state *crtc_state;
10434 	struct intel_crtc *crtc;
10435 	int i, ret;
10436 
10437 	ret = drm_atomic_helper_prepare_planes(state->base.dev, &state->base);
10438 	if (ret < 0)
10439 		return ret;
10440 
10441 	for_each_new_intel_crtc_in_state(state, crtc, crtc_state, i) {
10442 		bool mode_changed = intel_crtc_needs_modeset(crtc_state);
10443 
10444 		if (mode_changed || crtc_state->update_pipe ||
10445 		    crtc_state->uapi.color_mgmt_changed) {
10446 			intel_dsb_prepare(crtc_state);
10447 		}
10448 	}
10449 
10450 	return 0;
10451 }
10452 
10453 void intel_crtc_arm_fifo_underrun(struct intel_crtc *crtc,
10454 				  struct intel_crtc_state *crtc_state)
10455 {
10456 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10457 
10458 	if (!IS_GEN(dev_priv, 2) || crtc_state->active_planes)
10459 		intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
10460 
10461 	if (crtc_state->has_pch_encoder) {
10462 		enum pipe pch_transcoder =
10463 			intel_crtc_pch_transcoder(crtc);
10464 
10465 		intel_set_pch_fifo_underrun_reporting(dev_priv, pch_transcoder, true);
10466 	}
10467 }
10468 
10469 static void intel_pipe_fastset(const struct intel_crtc_state *old_crtc_state,
10470 			       const struct intel_crtc_state *new_crtc_state)
10471 {
10472 	struct intel_crtc *crtc = to_intel_crtc(new_crtc_state->uapi.crtc);
10473 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
10474 
10475 	/*
10476 	 * Update pipe size and adjust fitter if needed: the reason for this is
10477 	 * that in compute_mode_changes we check the native mode (not the pfit
10478 	 * mode) to see if we can flip rather than do a full mode set. In the
10479 	 * fastboot case, we'll flip, but if we don't update the pipesrc and
10480 	 * pfit state, we'll end up with a big fb scanned out into the wrong
10481 	 * sized surface.
10482 	 */
10483 	intel_set_pipe_src_size(new_crtc_state);
10484 
10485 	/* on skylake this is done by detaching scalers */
10486 	if (INTEL_GEN(dev_priv) >= 9) {
10487 		skl_detach_scalers(new_crtc_state);
10488 
10489 		if (new_crtc_state->pch_pfit.enabled)
10490 			skl_pfit_enable(new_crtc_state);
10491 	} else if (HAS_PCH_SPLIT(dev_priv)) {
10492 		if (new_crtc_state->pch_pfit.enabled)
10493 			ilk_pfit_enable(new_crtc_state);
10494 		else if (old_crtc_state->pch_pfit.enabled)
10495 			ilk_pfit_disable(old_crtc_state);
10496 	}
10497 
10498 	/*
10499 	 * The register is supposedly single buffered so perhaps
10500 	 * not 100% correct to do this here. But SKL+ calculate
10501 	 * this based on the adjust pixel rate so pfit changes do
10502 	 * affect it and so it must be updated for fastsets.
10503 	 * HSW/BDW only really need this here for fastboot, after
10504 	 * that the value should not change without a full modeset.
10505 	 */
10506 	if (INTEL_GEN(dev_priv) >= 9 ||
10507 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv))
10508 		hsw_set_linetime_wm(new_crtc_state);
10509 
10510 	if (INTEL_GEN(dev_priv) >= 11)
10511 		icl_set_pipe_chicken(crtc);
10512 }
10513 
10514 static void commit_pipe_config(struct intel_atomic_state *state,
10515 			       struct intel_crtc *crtc)
10516 {
10517 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10518 	const struct intel_crtc_state *old_crtc_state =
10519 		intel_atomic_get_old_crtc_state(state, crtc);
10520 	const struct intel_crtc_state *new_crtc_state =
10521 		intel_atomic_get_new_crtc_state(state, crtc);
10522 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
10523 
10524 	/*
10525 	 * During modesets pipe configuration was programmed as the
10526 	 * CRTC was enabled.
10527 	 */
10528 	if (!modeset) {
10529 		if (new_crtc_state->uapi.color_mgmt_changed ||
10530 		    new_crtc_state->update_pipe)
10531 			intel_color_commit(new_crtc_state);
10532 
10533 		if (INTEL_GEN(dev_priv) >= 9)
10534 			skl_detach_scalers(new_crtc_state);
10535 
10536 		if (INTEL_GEN(dev_priv) >= 9 || IS_BROADWELL(dev_priv))
10537 			bdw_set_pipemisc(new_crtc_state);
10538 
10539 		if (new_crtc_state->update_pipe)
10540 			intel_pipe_fastset(old_crtc_state, new_crtc_state);
10541 
10542 		intel_psr2_program_trans_man_trk_ctl(new_crtc_state);
10543 	}
10544 
10545 	if (dev_priv->display.atomic_update_watermarks)
10546 		dev_priv->display.atomic_update_watermarks(state, crtc);
10547 }
10548 
10549 static void intel_enable_crtc(struct intel_atomic_state *state,
10550 			      struct intel_crtc *crtc)
10551 {
10552 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10553 	const struct intel_crtc_state *new_crtc_state =
10554 		intel_atomic_get_new_crtc_state(state, crtc);
10555 
10556 	if (!intel_crtc_needs_modeset(new_crtc_state))
10557 		return;
10558 
10559 	intel_crtc_update_active_timings(new_crtc_state);
10560 
10561 	dev_priv->display.crtc_enable(state, crtc);
10562 
10563 	if (new_crtc_state->bigjoiner_slave)
10564 		return;
10565 
10566 	/* vblanks work again, re-enable pipe CRC. */
10567 	intel_crtc_enable_pipe_crc(crtc);
10568 }
10569 
10570 static void intel_update_crtc(struct intel_atomic_state *state,
10571 			      struct intel_crtc *crtc)
10572 {
10573 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10574 	const struct intel_crtc_state *old_crtc_state =
10575 		intel_atomic_get_old_crtc_state(state, crtc);
10576 	struct intel_crtc_state *new_crtc_state =
10577 		intel_atomic_get_new_crtc_state(state, crtc);
10578 	bool modeset = intel_crtc_needs_modeset(new_crtc_state);
10579 
10580 	if (!modeset) {
10581 		if (new_crtc_state->preload_luts &&
10582 		    (new_crtc_state->uapi.color_mgmt_changed ||
10583 		     new_crtc_state->update_pipe))
10584 			intel_color_load_luts(new_crtc_state);
10585 
10586 		intel_pre_plane_update(state, crtc);
10587 
10588 		if (new_crtc_state->update_pipe)
10589 			intel_encoders_update_pipe(state, crtc);
10590 	}
10591 
10592 	if (new_crtc_state->update_pipe && !new_crtc_state->enable_fbc)
10593 		intel_fbc_disable(crtc);
10594 	else
10595 		intel_fbc_enable(state, crtc);
10596 
10597 	/* Perform vblank evasion around commit operation */
10598 	intel_pipe_update_start(new_crtc_state);
10599 
10600 	commit_pipe_config(state, crtc);
10601 
10602 	if (INTEL_GEN(dev_priv) >= 9)
10603 		skl_update_planes_on_crtc(state, crtc);
10604 	else
10605 		i9xx_update_planes_on_crtc(state, crtc);
10606 
10607 	intel_pipe_update_end(new_crtc_state);
10608 
10609 	/*
10610 	 * We usually enable FIFO underrun interrupts as part of the
10611 	 * CRTC enable sequence during modesets.  But when we inherit a
10612 	 * valid pipe configuration from the BIOS we need to take care
10613 	 * of enabling them on the CRTC's first fastset.
10614 	 */
10615 	if (new_crtc_state->update_pipe && !modeset &&
10616 	    old_crtc_state->inherited)
10617 		intel_crtc_arm_fifo_underrun(crtc, new_crtc_state);
10618 }
10619 
10620 static void intel_old_crtc_state_disables(struct intel_atomic_state *state,
10621 					  struct intel_crtc_state *old_crtc_state,
10622 					  struct intel_crtc_state *new_crtc_state,
10623 					  struct intel_crtc *crtc)
10624 {
10625 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10626 
10627 	drm_WARN_ON(&dev_priv->drm, old_crtc_state->bigjoiner_slave);
10628 
10629 	intel_crtc_disable_planes(state, crtc);
10630 
10631 	/*
10632 	 * We still need special handling for disabling bigjoiner master
10633 	 * and slaves since for slave we do not have encoder or plls
10634 	 * so we dont need to disable those.
10635 	 */
10636 	if (old_crtc_state->bigjoiner) {
10637 		intel_crtc_disable_planes(state,
10638 					  old_crtc_state->bigjoiner_linked_crtc);
10639 		old_crtc_state->bigjoiner_linked_crtc->active = false;
10640 	}
10641 
10642 	/*
10643 	 * We need to disable pipe CRC before disabling the pipe,
10644 	 * or we race against vblank off.
10645 	 */
10646 	intel_crtc_disable_pipe_crc(crtc);
10647 
10648 	dev_priv->display.crtc_disable(state, crtc);
10649 	crtc->active = false;
10650 	intel_fbc_disable(crtc);
10651 	intel_disable_shared_dpll(old_crtc_state);
10652 
10653 	/* FIXME unify this for all platforms */
10654 	if (!new_crtc_state->hw.active &&
10655 	    !HAS_GMCH(dev_priv) &&
10656 	    dev_priv->display.initial_watermarks)
10657 		dev_priv->display.initial_watermarks(state, crtc);
10658 }
10659 
10660 static void intel_commit_modeset_disables(struct intel_atomic_state *state)
10661 {
10662 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
10663 	struct intel_crtc *crtc;
10664 	u32 handled = 0;
10665 	int i;
10666 
10667 	/* Only disable port sync and MST slaves */
10668 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10669 					    new_crtc_state, i) {
10670 		if (!intel_crtc_needs_modeset(new_crtc_state) || old_crtc_state->bigjoiner)
10671 			continue;
10672 
10673 		if (!old_crtc_state->hw.active)
10674 			continue;
10675 
10676 		/* In case of Transcoder port Sync master slave CRTCs can be
10677 		 * assigned in any order and we need to make sure that
10678 		 * slave CRTCs are disabled first and then master CRTC since
10679 		 * Slave vblanks are masked till Master Vblanks.
10680 		 */
10681 		if (!is_trans_port_sync_slave(old_crtc_state) &&
10682 		    !intel_dp_mst_is_slave_trans(old_crtc_state))
10683 			continue;
10684 
10685 		intel_pre_plane_update(state, crtc);
10686 		intel_old_crtc_state_disables(state, old_crtc_state,
10687 					      new_crtc_state, crtc);
10688 		handled |= BIT(crtc->pipe);
10689 	}
10690 
10691 	/* Disable everything else left on */
10692 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10693 					    new_crtc_state, i) {
10694 		if (!intel_crtc_needs_modeset(new_crtc_state) ||
10695 		    (handled & BIT(crtc->pipe)) ||
10696 		    old_crtc_state->bigjoiner_slave)
10697 			continue;
10698 
10699 		intel_pre_plane_update(state, crtc);
10700 		if (old_crtc_state->bigjoiner) {
10701 			struct intel_crtc *slave =
10702 				old_crtc_state->bigjoiner_linked_crtc;
10703 
10704 			intel_pre_plane_update(state, slave);
10705 		}
10706 
10707 		if (old_crtc_state->hw.active)
10708 			intel_old_crtc_state_disables(state, old_crtc_state,
10709 						      new_crtc_state, crtc);
10710 	}
10711 }
10712 
10713 static void intel_commit_modeset_enables(struct intel_atomic_state *state)
10714 {
10715 	struct intel_crtc_state *new_crtc_state;
10716 	struct intel_crtc *crtc;
10717 	int i;
10718 
10719 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10720 		if (!new_crtc_state->hw.active)
10721 			continue;
10722 
10723 		intel_enable_crtc(state, crtc);
10724 		intel_update_crtc(state, crtc);
10725 	}
10726 }
10727 
10728 static void skl_commit_modeset_enables(struct intel_atomic_state *state)
10729 {
10730 	struct drm_i915_private *dev_priv = to_i915(state->base.dev);
10731 	struct intel_crtc *crtc;
10732 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10733 	struct skl_ddb_entry entries[I915_MAX_PIPES] = {};
10734 	u8 update_pipes = 0, modeset_pipes = 0;
10735 	int i;
10736 
10737 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
10738 		enum pipe pipe = crtc->pipe;
10739 
10740 		if (!new_crtc_state->hw.active)
10741 			continue;
10742 
10743 		/* ignore allocations for crtc's that have been turned off. */
10744 		if (!intel_crtc_needs_modeset(new_crtc_state)) {
10745 			entries[pipe] = old_crtc_state->wm.skl.ddb;
10746 			update_pipes |= BIT(pipe);
10747 		} else {
10748 			modeset_pipes |= BIT(pipe);
10749 		}
10750 	}
10751 
10752 	/*
10753 	 * Whenever the number of active pipes changes, we need to make sure we
10754 	 * update the pipes in the right order so that their ddb allocations
10755 	 * never overlap with each other between CRTC updates. Otherwise we'll
10756 	 * cause pipe underruns and other bad stuff.
10757 	 *
10758 	 * So first lets enable all pipes that do not need a fullmodeset as
10759 	 * those don't have any external dependency.
10760 	 */
10761 	while (update_pipes) {
10762 		for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10763 						    new_crtc_state, i) {
10764 			enum pipe pipe = crtc->pipe;
10765 
10766 			if ((update_pipes & BIT(pipe)) == 0)
10767 				continue;
10768 
10769 			if (skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
10770 							entries, I915_MAX_PIPES, pipe))
10771 				continue;
10772 
10773 			entries[pipe] = new_crtc_state->wm.skl.ddb;
10774 			update_pipes &= ~BIT(pipe);
10775 
10776 			intel_update_crtc(state, crtc);
10777 
10778 			/*
10779 			 * If this is an already active pipe, it's DDB changed,
10780 			 * and this isn't the last pipe that needs updating
10781 			 * then we need to wait for a vblank to pass for the
10782 			 * new ddb allocation to take effect.
10783 			 */
10784 			if (!skl_ddb_entry_equal(&new_crtc_state->wm.skl.ddb,
10785 						 &old_crtc_state->wm.skl.ddb) &&
10786 			    (update_pipes | modeset_pipes))
10787 				intel_wait_for_vblank(dev_priv, pipe);
10788 		}
10789 	}
10790 
10791 	update_pipes = modeset_pipes;
10792 
10793 	/*
10794 	 * Enable all pipes that needs a modeset and do not depends on other
10795 	 * pipes
10796 	 */
10797 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10798 		enum pipe pipe = crtc->pipe;
10799 
10800 		if ((modeset_pipes & BIT(pipe)) == 0)
10801 			continue;
10802 
10803 		if (intel_dp_mst_is_slave_trans(new_crtc_state) ||
10804 		    is_trans_port_sync_master(new_crtc_state) ||
10805 		    (new_crtc_state->bigjoiner && !new_crtc_state->bigjoiner_slave))
10806 			continue;
10807 
10808 		modeset_pipes &= ~BIT(pipe);
10809 
10810 		intel_enable_crtc(state, crtc);
10811 	}
10812 
10813 	/*
10814 	 * Then we enable all remaining pipes that depend on other
10815 	 * pipes: MST slaves and port sync masters, big joiner master
10816 	 */
10817 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10818 		enum pipe pipe = crtc->pipe;
10819 
10820 		if ((modeset_pipes & BIT(pipe)) == 0)
10821 			continue;
10822 
10823 		modeset_pipes &= ~BIT(pipe);
10824 
10825 		intel_enable_crtc(state, crtc);
10826 	}
10827 
10828 	/*
10829 	 * Finally we do the plane updates/etc. for all pipes that got enabled.
10830 	 */
10831 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
10832 		enum pipe pipe = crtc->pipe;
10833 
10834 		if ((update_pipes & BIT(pipe)) == 0)
10835 			continue;
10836 
10837 		drm_WARN_ON(&dev_priv->drm, skl_ddb_allocation_overlaps(&new_crtc_state->wm.skl.ddb,
10838 									entries, I915_MAX_PIPES, pipe));
10839 
10840 		entries[pipe] = new_crtc_state->wm.skl.ddb;
10841 		update_pipes &= ~BIT(pipe);
10842 
10843 		intel_update_crtc(state, crtc);
10844 	}
10845 
10846 	drm_WARN_ON(&dev_priv->drm, modeset_pipes);
10847 	drm_WARN_ON(&dev_priv->drm, update_pipes);
10848 }
10849 
10850 static void intel_atomic_helper_free_state(struct drm_i915_private *dev_priv)
10851 {
10852 	struct intel_atomic_state *state, *next;
10853 	struct llist_node *freed;
10854 
10855 	freed = llist_del_all(&dev_priv->atomic_helper.free_list);
10856 	llist_for_each_entry_safe(state, next, freed, freed)
10857 		drm_atomic_state_put(&state->base);
10858 }
10859 
10860 static void intel_atomic_helper_free_state_worker(struct work_struct *work)
10861 {
10862 	struct drm_i915_private *dev_priv =
10863 		container_of(work, typeof(*dev_priv), atomic_helper.free_work);
10864 
10865 	intel_atomic_helper_free_state(dev_priv);
10866 }
10867 
10868 static void intel_atomic_commit_fence_wait(struct intel_atomic_state *intel_state)
10869 {
10870 	struct wait_queue_entry wait_fence, wait_reset;
10871 	struct drm_i915_private *dev_priv = to_i915(intel_state->base.dev);
10872 
10873 	init_wait_entry(&wait_fence, 0);
10874 	init_wait_entry(&wait_reset, 0);
10875 	for (;;) {
10876 		prepare_to_wait(&intel_state->commit_ready.wait,
10877 				&wait_fence, TASK_UNINTERRUPTIBLE);
10878 		prepare_to_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
10879 					      I915_RESET_MODESET),
10880 				&wait_reset, TASK_UNINTERRUPTIBLE);
10881 
10882 
10883 		if (i915_sw_fence_done(&intel_state->commit_ready) ||
10884 		    test_bit(I915_RESET_MODESET, &dev_priv->gt.reset.flags))
10885 			break;
10886 
10887 		schedule();
10888 	}
10889 	finish_wait(&intel_state->commit_ready.wait, &wait_fence);
10890 	finish_wait(bit_waitqueue(&dev_priv->gt.reset.flags,
10891 				  I915_RESET_MODESET),
10892 		    &wait_reset);
10893 }
10894 
10895 static void intel_cleanup_dsbs(struct intel_atomic_state *state)
10896 {
10897 	struct intel_crtc_state *old_crtc_state, *new_crtc_state;
10898 	struct intel_crtc *crtc;
10899 	int i;
10900 
10901 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10902 					    new_crtc_state, i)
10903 		intel_dsb_cleanup(old_crtc_state);
10904 }
10905 
10906 static void intel_atomic_cleanup_work(struct work_struct *work)
10907 {
10908 	struct intel_atomic_state *state =
10909 		container_of(work, struct intel_atomic_state, base.commit_work);
10910 	struct drm_i915_private *i915 = to_i915(state->base.dev);
10911 
10912 	intel_cleanup_dsbs(state);
10913 	drm_atomic_helper_cleanup_planes(&i915->drm, &state->base);
10914 	drm_atomic_helper_commit_cleanup_done(&state->base);
10915 	drm_atomic_state_put(&state->base);
10916 
10917 	intel_atomic_helper_free_state(i915);
10918 }
10919 
10920 static void intel_atomic_prepare_plane_clear_colors(struct intel_atomic_state *state)
10921 {
10922 	struct drm_i915_private *i915 = to_i915(state->base.dev);
10923 	struct intel_plane *plane;
10924 	struct intel_plane_state *plane_state;
10925 	int i;
10926 
10927 	for_each_new_intel_plane_in_state(state, plane, plane_state, i) {
10928 		struct drm_framebuffer *fb = plane_state->hw.fb;
10929 		int ret;
10930 
10931 		if (!fb ||
10932 		    fb->modifier != I915_FORMAT_MOD_Y_TILED_GEN12_RC_CCS_CC)
10933 			continue;
10934 
10935 		/*
10936 		 * The layout of the fast clear color value expected by HW
10937 		 * (the DRM ABI requiring this value to be located in fb at offset 0 of plane#2):
10938 		 * - 4 x 4 bytes per-channel value
10939 		 *   (in surface type specific float/int format provided by the fb user)
10940 		 * - 8 bytes native color value used by the display
10941 		 *   (converted/written by GPU during a fast clear operation using the
10942 		 *    above per-channel values)
10943 		 *
10944 		 * The commit's FB prepare hook already ensured that FB obj is pinned and the
10945 		 * caller made sure that the object is synced wrt. the related color clear value
10946 		 * GPU write on it.
10947 		 */
10948 		ret = i915_gem_object_read_from_page(intel_fb_obj(fb),
10949 						     fb->offsets[2] + 16,
10950 						     &plane_state->ccval,
10951 						     sizeof(plane_state->ccval));
10952 		/* The above could only fail if the FB obj has an unexpected backing store type. */
10953 		drm_WARN_ON(&i915->drm, ret);
10954 	}
10955 }
10956 
10957 static void intel_atomic_commit_tail(struct intel_atomic_state *state)
10958 {
10959 	struct drm_device *dev = state->base.dev;
10960 	struct drm_i915_private *dev_priv = to_i915(dev);
10961 	struct intel_crtc_state *new_crtc_state, *old_crtc_state;
10962 	struct intel_crtc *crtc;
10963 	u64 put_domains[I915_MAX_PIPES] = {};
10964 	intel_wakeref_t wakeref = 0;
10965 	int i;
10966 
10967 	intel_atomic_commit_fence_wait(state);
10968 
10969 	drm_atomic_helper_wait_for_dependencies(&state->base);
10970 
10971 	if (state->modeset)
10972 		wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_MODESET);
10973 
10974 	intel_atomic_prepare_plane_clear_colors(state);
10975 
10976 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
10977 					    new_crtc_state, i) {
10978 		if (intel_crtc_needs_modeset(new_crtc_state) ||
10979 		    new_crtc_state->update_pipe) {
10980 
10981 			put_domains[crtc->pipe] =
10982 				modeset_get_crtc_power_domains(new_crtc_state);
10983 		}
10984 	}
10985 
10986 	intel_commit_modeset_disables(state);
10987 
10988 	/* FIXME: Eventually get rid of our crtc->config pointer */
10989 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
10990 		crtc->config = new_crtc_state;
10991 
10992 	if (state->modeset) {
10993 		drm_atomic_helper_update_legacy_modeset_state(dev, &state->base);
10994 
10995 		intel_set_cdclk_pre_plane_update(state);
10996 
10997 		intel_modeset_verify_disabled(dev_priv, state);
10998 	}
10999 
11000 	intel_sagv_pre_plane_update(state);
11001 
11002 	/* Complete the events for pipes that have now been disabled */
11003 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
11004 		bool modeset = intel_crtc_needs_modeset(new_crtc_state);
11005 
11006 		/* Complete events for now disable pipes here. */
11007 		if (modeset && !new_crtc_state->hw.active && new_crtc_state->uapi.event) {
11008 			spin_lock_irq(&dev->event_lock);
11009 			drm_crtc_send_vblank_event(&crtc->base,
11010 						   new_crtc_state->uapi.event);
11011 			spin_unlock_irq(&dev->event_lock);
11012 
11013 			new_crtc_state->uapi.event = NULL;
11014 		}
11015 	}
11016 
11017 	if (state->modeset)
11018 		intel_encoders_update_prepare(state);
11019 
11020 	intel_dbuf_pre_plane_update(state);
11021 
11022 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
11023 		if (new_crtc_state->uapi.async_flip)
11024 			intel_crtc_enable_flip_done(state, crtc);
11025 	}
11026 
11027 	/* Now enable the clocks, plane, pipe, and connectors that we set up. */
11028 	dev_priv->display.commit_modeset_enables(state);
11029 
11030 	if (state->modeset) {
11031 		intel_encoders_update_complete(state);
11032 
11033 		intel_set_cdclk_post_plane_update(state);
11034 	}
11035 
11036 	/* FIXME: We should call drm_atomic_helper_commit_hw_done() here
11037 	 * already, but still need the state for the delayed optimization. To
11038 	 * fix this:
11039 	 * - wrap the optimization/post_plane_update stuff into a per-crtc work.
11040 	 * - schedule that vblank worker _before_ calling hw_done
11041 	 * - at the start of commit_tail, cancel it _synchrously
11042 	 * - switch over to the vblank wait helper in the core after that since
11043 	 *   we don't need out special handling any more.
11044 	 */
11045 	drm_atomic_helper_wait_for_flip_done(dev, &state->base);
11046 
11047 	for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i) {
11048 		if (new_crtc_state->uapi.async_flip)
11049 			intel_crtc_disable_flip_done(state, crtc);
11050 
11051 		if (new_crtc_state->hw.active &&
11052 		    !intel_crtc_needs_modeset(new_crtc_state) &&
11053 		    !new_crtc_state->preload_luts &&
11054 		    (new_crtc_state->uapi.color_mgmt_changed ||
11055 		     new_crtc_state->update_pipe))
11056 			intel_color_load_luts(new_crtc_state);
11057 	}
11058 
11059 	/*
11060 	 * Now that the vblank has passed, we can go ahead and program the
11061 	 * optimal watermarks on platforms that need two-step watermark
11062 	 * programming.
11063 	 *
11064 	 * TODO: Move this (and other cleanup) to an async worker eventually.
11065 	 */
11066 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state,
11067 					    new_crtc_state, i) {
11068 		/*
11069 		 * Gen2 reports pipe underruns whenever all planes are disabled.
11070 		 * So re-enable underrun reporting after some planes get enabled.
11071 		 *
11072 		 * We do this before .optimize_watermarks() so that we have a
11073 		 * chance of catching underruns with the intermediate watermarks
11074 		 * vs. the new plane configuration.
11075 		 */
11076 		if (IS_GEN(dev_priv, 2) && planes_enabling(old_crtc_state, new_crtc_state))
11077 			intel_set_cpu_fifo_underrun_reporting(dev_priv, crtc->pipe, true);
11078 
11079 		if (dev_priv->display.optimize_watermarks)
11080 			dev_priv->display.optimize_watermarks(state, crtc);
11081 	}
11082 
11083 	intel_dbuf_post_plane_update(state);
11084 
11085 	for_each_oldnew_intel_crtc_in_state(state, crtc, old_crtc_state, new_crtc_state, i) {
11086 		intel_post_plane_update(state, crtc);
11087 
11088 		modeset_put_crtc_power_domains(crtc, put_domains[crtc->pipe]);
11089 
11090 		intel_modeset_verify_crtc(crtc, state, old_crtc_state, new_crtc_state);
11091 
11092 		/*
11093 		 * DSB cleanup is done in cleanup_work aligning with framebuffer
11094 		 * cleanup. So copy and reset the dsb structure to sync with
11095 		 * commit_done and later do dsb cleanup in cleanup_work.
11096 		 */
11097 		old_crtc_state->dsb = fetch_and_zero(&new_crtc_state->dsb);
11098 	}
11099 
11100 	/* Underruns don't always raise interrupts, so check manually */
11101 	intel_check_cpu_fifo_underruns(dev_priv);
11102 	intel_check_pch_fifo_underruns(dev_priv);
11103 
11104 	if (state->modeset)
11105 		intel_verify_planes(state);
11106 
11107 	intel_sagv_post_plane_update(state);
11108 
11109 	drm_atomic_helper_commit_hw_done(&state->base);
11110 
11111 	if (state->modeset) {
11112 		/* As one of the primary mmio accessors, KMS has a high
11113 		 * likelihood of triggering bugs in unclaimed access. After we
11114 		 * finish modesetting, see if an error has been flagged, and if
11115 		 * so enable debugging for the next modeset - and hope we catch
11116 		 * the culprit.
11117 		 */
11118 		intel_uncore_arm_unclaimed_mmio_detection(&dev_priv->uncore);
11119 		intel_display_power_put(dev_priv, POWER_DOMAIN_MODESET, wakeref);
11120 	}
11121 	intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
11122 
11123 	/*
11124 	 * Defer the cleanup of the old state to a separate worker to not
11125 	 * impede the current task (userspace for blocking modesets) that
11126 	 * are executed inline. For out-of-line asynchronous modesets/flips,
11127 	 * deferring to a new worker seems overkill, but we would place a
11128 	 * schedule point (cond_resched()) here anyway to keep latencies
11129 	 * down.
11130 	 */
11131 	INIT_WORK(&state->base.commit_work, intel_atomic_cleanup_work);
11132 	queue_work(system_highpri_wq, &state->base.commit_work);
11133 }
11134 
11135 static void intel_atomic_commit_work(struct work_struct *work)
11136 {
11137 	struct intel_atomic_state *state =
11138 		container_of(work, struct intel_atomic_state, base.commit_work);
11139 
11140 	intel_atomic_commit_tail(state);
11141 }
11142 
11143 static int __i915_sw_fence_call
11144 intel_atomic_commit_ready(struct i915_sw_fence *fence,
11145 			  enum i915_sw_fence_notify notify)
11146 {
11147 	struct intel_atomic_state *state =
11148 		container_of(fence, struct intel_atomic_state, commit_ready);
11149 
11150 	switch (notify) {
11151 	case FENCE_COMPLETE:
11152 		/* we do blocking waits in the worker, nothing to do here */
11153 		break;
11154 	case FENCE_FREE:
11155 		{
11156 			struct intel_atomic_helper *helper =
11157 				&to_i915(state->base.dev)->atomic_helper;
11158 
11159 			if (llist_add(&state->freed, &helper->free_list))
11160 				schedule_work(&helper->free_work);
11161 			break;
11162 		}
11163 	}
11164 
11165 	return NOTIFY_DONE;
11166 }
11167 
11168 static void intel_atomic_track_fbs(struct intel_atomic_state *state)
11169 {
11170 	struct intel_plane_state *old_plane_state, *new_plane_state;
11171 	struct intel_plane *plane;
11172 	int i;
11173 
11174 	for_each_oldnew_intel_plane_in_state(state, plane, old_plane_state,
11175 					     new_plane_state, i)
11176 		intel_frontbuffer_track(to_intel_frontbuffer(old_plane_state->hw.fb),
11177 					to_intel_frontbuffer(new_plane_state->hw.fb),
11178 					plane->frontbuffer_bit);
11179 }
11180 
11181 static int intel_atomic_commit(struct drm_device *dev,
11182 			       struct drm_atomic_state *_state,
11183 			       bool nonblock)
11184 {
11185 	struct intel_atomic_state *state = to_intel_atomic_state(_state);
11186 	struct drm_i915_private *dev_priv = to_i915(dev);
11187 	int ret = 0;
11188 
11189 	state->wakeref = intel_runtime_pm_get(&dev_priv->runtime_pm);
11190 
11191 	drm_atomic_state_get(&state->base);
11192 	i915_sw_fence_init(&state->commit_ready,
11193 			   intel_atomic_commit_ready);
11194 
11195 	/*
11196 	 * The intel_legacy_cursor_update() fast path takes care
11197 	 * of avoiding the vblank waits for simple cursor
11198 	 * movement and flips. For cursor on/off and size changes,
11199 	 * we want to perform the vblank waits so that watermark
11200 	 * updates happen during the correct frames. Gen9+ have
11201 	 * double buffered watermarks and so shouldn't need this.
11202 	 *
11203 	 * Unset state->legacy_cursor_update before the call to
11204 	 * drm_atomic_helper_setup_commit() because otherwise
11205 	 * drm_atomic_helper_wait_for_flip_done() is a noop and
11206 	 * we get FIFO underruns because we didn't wait
11207 	 * for vblank.
11208 	 *
11209 	 * FIXME doing watermarks and fb cleanup from a vblank worker
11210 	 * (assuming we had any) would solve these problems.
11211 	 */
11212 	if (INTEL_GEN(dev_priv) < 9 && state->base.legacy_cursor_update) {
11213 		struct intel_crtc_state *new_crtc_state;
11214 		struct intel_crtc *crtc;
11215 		int i;
11216 
11217 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
11218 			if (new_crtc_state->wm.need_postvbl_update ||
11219 			    new_crtc_state->update_wm_post)
11220 				state->base.legacy_cursor_update = false;
11221 	}
11222 
11223 	ret = intel_atomic_prepare_commit(state);
11224 	if (ret) {
11225 		drm_dbg_atomic(&dev_priv->drm,
11226 			       "Preparing state failed with %i\n", ret);
11227 		i915_sw_fence_commit(&state->commit_ready);
11228 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
11229 		return ret;
11230 	}
11231 
11232 	ret = drm_atomic_helper_setup_commit(&state->base, nonblock);
11233 	if (!ret)
11234 		ret = drm_atomic_helper_swap_state(&state->base, true);
11235 	if (!ret)
11236 		intel_atomic_swap_global_state(state);
11237 
11238 	if (ret) {
11239 		struct intel_crtc_state *new_crtc_state;
11240 		struct intel_crtc *crtc;
11241 		int i;
11242 
11243 		i915_sw_fence_commit(&state->commit_ready);
11244 
11245 		for_each_new_intel_crtc_in_state(state, crtc, new_crtc_state, i)
11246 			intel_dsb_cleanup(new_crtc_state);
11247 
11248 		drm_atomic_helper_cleanup_planes(dev, &state->base);
11249 		intel_runtime_pm_put(&dev_priv->runtime_pm, state->wakeref);
11250 		return ret;
11251 	}
11252 	intel_shared_dpll_swap_state(state);
11253 	intel_atomic_track_fbs(state);
11254 
11255 	drm_atomic_state_get(&state->base);
11256 	INIT_WORK(&state->base.commit_work, intel_atomic_commit_work);
11257 
11258 	i915_sw_fence_commit(&state->commit_ready);
11259 	if (nonblock && state->modeset) {
11260 		queue_work(dev_priv->modeset_wq, &state->base.commit_work);
11261 	} else if (nonblock) {
11262 		queue_work(dev_priv->flip_wq, &state->base.commit_work);
11263 	} else {
11264 		if (state->modeset)
11265 			flush_workqueue(dev_priv->modeset_wq);
11266 		intel_atomic_commit_tail(state);
11267 	}
11268 
11269 	return 0;
11270 }
11271 
11272 struct wait_rps_boost {
11273 	struct wait_queue_entry wait;
11274 
11275 	struct drm_crtc *crtc;
11276 	struct i915_request *request;
11277 };
11278 
11279 static int do_rps_boost(struct wait_queue_entry *_wait,
11280 			unsigned mode, int sync, void *key)
11281 {
11282 	struct wait_rps_boost *wait = container_of(_wait, typeof(*wait), wait);
11283 	struct i915_request *rq = wait->request;
11284 
11285 	/*
11286 	 * If we missed the vblank, but the request is already running it
11287 	 * is reasonable to assume that it will complete before the next
11288 	 * vblank without our intervention, so leave RPS alone.
11289 	 */
11290 	if (!i915_request_started(rq))
11291 		intel_rps_boost(rq);
11292 	i915_request_put(rq);
11293 
11294 	drm_crtc_vblank_put(wait->crtc);
11295 
11296 	list_del(&wait->wait.entry);
11297 	kfree(wait);
11298 	return 1;
11299 }
11300 
11301 static void add_rps_boost_after_vblank(struct drm_crtc *crtc,
11302 				       struct dma_fence *fence)
11303 {
11304 	struct wait_rps_boost *wait;
11305 
11306 	if (!dma_fence_is_i915(fence))
11307 		return;
11308 
11309 	if (INTEL_GEN(to_i915(crtc->dev)) < 6)
11310 		return;
11311 
11312 	if (drm_crtc_vblank_get(crtc))
11313 		return;
11314 
11315 	wait = kmalloc(sizeof(*wait), GFP_KERNEL);
11316 	if (!wait) {
11317 		drm_crtc_vblank_put(crtc);
11318 		return;
11319 	}
11320 
11321 	wait->request = to_request(dma_fence_get(fence));
11322 	wait->crtc = crtc;
11323 
11324 	wait->wait.func = do_rps_boost;
11325 	wait->wait.flags = 0;
11326 
11327 	add_wait_queue(drm_crtc_vblank_waitqueue(crtc), &wait->wait);
11328 }
11329 
11330 int intel_plane_pin_fb(struct intel_plane_state *plane_state)
11331 {
11332 	struct intel_plane *plane = to_intel_plane(plane_state->uapi.plane);
11333 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11334 	struct drm_framebuffer *fb = plane_state->hw.fb;
11335 	struct i915_vma *vma;
11336 
11337 	if (plane->id == PLANE_CURSOR &&
11338 	    INTEL_INFO(dev_priv)->display.cursor_needs_physical) {
11339 		struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11340 		const int align = intel_cursor_alignment(dev_priv);
11341 		int err;
11342 
11343 		err = i915_gem_object_attach_phys(obj, align);
11344 		if (err)
11345 			return err;
11346 	}
11347 
11348 	vma = intel_pin_and_fence_fb_obj(fb,
11349 					 &plane_state->view,
11350 					 intel_plane_uses_fence(plane_state),
11351 					 &plane_state->flags);
11352 	if (IS_ERR(vma))
11353 		return PTR_ERR(vma);
11354 
11355 	plane_state->vma = vma;
11356 
11357 	return 0;
11358 }
11359 
11360 void intel_plane_unpin_fb(struct intel_plane_state *old_plane_state)
11361 {
11362 	struct i915_vma *vma;
11363 
11364 	vma = fetch_and_zero(&old_plane_state->vma);
11365 	if (vma)
11366 		intel_unpin_fb_vma(vma, old_plane_state->flags);
11367 }
11368 
11369 /**
11370  * intel_prepare_plane_fb - Prepare fb for usage on plane
11371  * @_plane: drm plane to prepare for
11372  * @_new_plane_state: the plane state being prepared
11373  *
11374  * Prepares a framebuffer for usage on a display plane.  Generally this
11375  * involves pinning the underlying object and updating the frontbuffer tracking
11376  * bits.  Some older platforms need special physical address handling for
11377  * cursor planes.
11378  *
11379  * Returns 0 on success, negative error code on failure.
11380  */
11381 int
11382 intel_prepare_plane_fb(struct drm_plane *_plane,
11383 		       struct drm_plane_state *_new_plane_state)
11384 {
11385 	struct i915_sched_attr attr = {
11386 		.priority = I915_USER_PRIORITY(I915_PRIORITY_DISPLAY),
11387 	};
11388 	struct intel_plane *plane = to_intel_plane(_plane);
11389 	struct intel_plane_state *new_plane_state =
11390 		to_intel_plane_state(_new_plane_state);
11391 	struct intel_atomic_state *state =
11392 		to_intel_atomic_state(new_plane_state->uapi.state);
11393 	struct drm_i915_private *dev_priv = to_i915(plane->base.dev);
11394 	const struct intel_plane_state *old_plane_state =
11395 		intel_atomic_get_old_plane_state(state, plane);
11396 	struct drm_i915_gem_object *obj = intel_fb_obj(new_plane_state->hw.fb);
11397 	struct drm_i915_gem_object *old_obj = intel_fb_obj(old_plane_state->hw.fb);
11398 	int ret;
11399 
11400 	if (old_obj) {
11401 		const struct intel_crtc_state *crtc_state =
11402 			intel_atomic_get_new_crtc_state(state,
11403 							to_intel_crtc(old_plane_state->hw.crtc));
11404 
11405 		/* Big Hammer, we also need to ensure that any pending
11406 		 * MI_WAIT_FOR_EVENT inside a user batch buffer on the
11407 		 * current scanout is retired before unpinning the old
11408 		 * framebuffer. Note that we rely on userspace rendering
11409 		 * into the buffer attached to the pipe they are waiting
11410 		 * on. If not, userspace generates a GPU hang with IPEHR
11411 		 * point to the MI_WAIT_FOR_EVENT.
11412 		 *
11413 		 * This should only fail upon a hung GPU, in which case we
11414 		 * can safely continue.
11415 		 */
11416 		if (intel_crtc_needs_modeset(crtc_state)) {
11417 			ret = i915_sw_fence_await_reservation(&state->commit_ready,
11418 							      old_obj->base.resv, NULL,
11419 							      false, 0,
11420 							      GFP_KERNEL);
11421 			if (ret < 0)
11422 				return ret;
11423 		}
11424 	}
11425 
11426 	if (new_plane_state->uapi.fence) { /* explicit fencing */
11427 		i915_gem_fence_wait_priority(new_plane_state->uapi.fence,
11428 					     &attr);
11429 		ret = i915_sw_fence_await_dma_fence(&state->commit_ready,
11430 						    new_plane_state->uapi.fence,
11431 						    i915_fence_timeout(dev_priv),
11432 						    GFP_KERNEL);
11433 		if (ret < 0)
11434 			return ret;
11435 	}
11436 
11437 	if (!obj)
11438 		return 0;
11439 
11440 	ret = i915_gem_object_pin_pages(obj);
11441 	if (ret)
11442 		return ret;
11443 
11444 	ret = intel_plane_pin_fb(new_plane_state);
11445 
11446 	i915_gem_object_unpin_pages(obj);
11447 	if (ret)
11448 		return ret;
11449 
11450 	i915_gem_object_wait_priority(obj, 0, &attr);
11451 	i915_gem_object_flush_frontbuffer(obj, ORIGIN_DIRTYFB);
11452 
11453 	if (!new_plane_state->uapi.fence) { /* implicit fencing */
11454 		struct dma_fence *fence;
11455 
11456 		ret = i915_sw_fence_await_reservation(&state->commit_ready,
11457 						      obj->base.resv, NULL,
11458 						      false,
11459 						      i915_fence_timeout(dev_priv),
11460 						      GFP_KERNEL);
11461 		if (ret < 0)
11462 			goto unpin_fb;
11463 
11464 		fence = dma_resv_get_excl_rcu(obj->base.resv);
11465 		if (fence) {
11466 			add_rps_boost_after_vblank(new_plane_state->hw.crtc,
11467 						   fence);
11468 			dma_fence_put(fence);
11469 		}
11470 	} else {
11471 		add_rps_boost_after_vblank(new_plane_state->hw.crtc,
11472 					   new_plane_state->uapi.fence);
11473 	}
11474 
11475 	/*
11476 	 * We declare pageflips to be interactive and so merit a small bias
11477 	 * towards upclocking to deliver the frame on time. By only changing
11478 	 * the RPS thresholds to sample more regularly and aim for higher
11479 	 * clocks we can hopefully deliver low power workloads (like kodi)
11480 	 * that are not quite steady state without resorting to forcing
11481 	 * maximum clocks following a vblank miss (see do_rps_boost()).
11482 	 */
11483 	if (!state->rps_interactive) {
11484 		intel_rps_mark_interactive(&dev_priv->gt.rps, true);
11485 		state->rps_interactive = true;
11486 	}
11487 
11488 	return 0;
11489 
11490 unpin_fb:
11491 	intel_plane_unpin_fb(new_plane_state);
11492 
11493 	return ret;
11494 }
11495 
11496 /**
11497  * intel_cleanup_plane_fb - Cleans up an fb after plane use
11498  * @plane: drm plane to clean up for
11499  * @_old_plane_state: the state from the previous modeset
11500  *
11501  * Cleans up a framebuffer that has just been removed from a plane.
11502  */
11503 void
11504 intel_cleanup_plane_fb(struct drm_plane *plane,
11505 		       struct drm_plane_state *_old_plane_state)
11506 {
11507 	struct intel_plane_state *old_plane_state =
11508 		to_intel_plane_state(_old_plane_state);
11509 	struct intel_atomic_state *state =
11510 		to_intel_atomic_state(old_plane_state->uapi.state);
11511 	struct drm_i915_private *dev_priv = to_i915(plane->dev);
11512 	struct drm_i915_gem_object *obj = intel_fb_obj(old_plane_state->hw.fb);
11513 
11514 	if (!obj)
11515 		return;
11516 
11517 	if (state->rps_interactive) {
11518 		intel_rps_mark_interactive(&dev_priv->gt.rps, false);
11519 		state->rps_interactive = false;
11520 	}
11521 
11522 	/* Should only be called after a successful intel_prepare_plane_fb()! */
11523 	intel_plane_unpin_fb(old_plane_state);
11524 }
11525 
11526 /**
11527  * intel_plane_destroy - destroy a plane
11528  * @plane: plane to destroy
11529  *
11530  * Common destruction function for all types of planes (primary, cursor,
11531  * sprite).
11532  */
11533 void intel_plane_destroy(struct drm_plane *plane)
11534 {
11535 	drm_plane_cleanup(plane);
11536 	kfree(to_intel_plane(plane));
11537 }
11538 
11539 static void intel_plane_possible_crtcs_init(struct drm_i915_private *dev_priv)
11540 {
11541 	struct intel_plane *plane;
11542 
11543 	for_each_intel_plane(&dev_priv->drm, plane) {
11544 		struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv,
11545 								  plane->pipe);
11546 
11547 		plane->base.possible_crtcs = drm_crtc_mask(&crtc->base);
11548 	}
11549 }
11550 
11551 
11552 int intel_get_pipe_from_crtc_id_ioctl(struct drm_device *dev, void *data,
11553 				      struct drm_file *file)
11554 {
11555 	struct drm_i915_get_pipe_from_crtc_id *pipe_from_crtc_id = data;
11556 	struct drm_crtc *drmmode_crtc;
11557 	struct intel_crtc *crtc;
11558 
11559 	drmmode_crtc = drm_crtc_find(dev, file, pipe_from_crtc_id->crtc_id);
11560 	if (!drmmode_crtc)
11561 		return -ENOENT;
11562 
11563 	crtc = to_intel_crtc(drmmode_crtc);
11564 	pipe_from_crtc_id->pipe = crtc->pipe;
11565 
11566 	return 0;
11567 }
11568 
11569 static u32 intel_encoder_possible_clones(struct intel_encoder *encoder)
11570 {
11571 	struct drm_device *dev = encoder->base.dev;
11572 	struct intel_encoder *source_encoder;
11573 	u32 possible_clones = 0;
11574 
11575 	for_each_intel_encoder(dev, source_encoder) {
11576 		if (encoders_cloneable(encoder, source_encoder))
11577 			possible_clones |= drm_encoder_mask(&source_encoder->base);
11578 	}
11579 
11580 	return possible_clones;
11581 }
11582 
11583 static u32 intel_encoder_possible_crtcs(struct intel_encoder *encoder)
11584 {
11585 	struct drm_device *dev = encoder->base.dev;
11586 	struct intel_crtc *crtc;
11587 	u32 possible_crtcs = 0;
11588 
11589 	for_each_intel_crtc(dev, crtc) {
11590 		if (encoder->pipe_mask & BIT(crtc->pipe))
11591 			possible_crtcs |= drm_crtc_mask(&crtc->base);
11592 	}
11593 
11594 	return possible_crtcs;
11595 }
11596 
11597 static bool ilk_has_edp_a(struct drm_i915_private *dev_priv)
11598 {
11599 	if (!IS_MOBILE(dev_priv))
11600 		return false;
11601 
11602 	if ((intel_de_read(dev_priv, DP_A) & DP_DETECTED) == 0)
11603 		return false;
11604 
11605 	if (IS_GEN(dev_priv, 5) && (intel_de_read(dev_priv, FUSE_STRAP) & ILK_eDP_A_DISABLE))
11606 		return false;
11607 
11608 	return true;
11609 }
11610 
11611 static bool intel_ddi_crt_present(struct drm_i915_private *dev_priv)
11612 {
11613 	if (INTEL_GEN(dev_priv) >= 9)
11614 		return false;
11615 
11616 	if (IS_HSW_ULT(dev_priv) || IS_BDW_ULT(dev_priv))
11617 		return false;
11618 
11619 	if (HAS_PCH_LPT_H(dev_priv) &&
11620 	    intel_de_read(dev_priv, SFUSE_STRAP) & SFUSE_STRAP_CRT_DISABLED)
11621 		return false;
11622 
11623 	/* DDI E can't be used if DDI A requires 4 lanes */
11624 	if (intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_A_4_LANES)
11625 		return false;
11626 
11627 	if (!dev_priv->vbt.int_crt_support)
11628 		return false;
11629 
11630 	return true;
11631 }
11632 
11633 static void intel_setup_outputs(struct drm_i915_private *dev_priv)
11634 {
11635 	struct intel_encoder *encoder;
11636 	bool dpd_is_edp = false;
11637 
11638 	intel_pps_unlock_regs_wa(dev_priv);
11639 
11640 	if (!HAS_DISPLAY(dev_priv))
11641 		return;
11642 
11643 	if (IS_ALDERLAKE_S(dev_priv)) {
11644 		intel_ddi_init(dev_priv, PORT_A);
11645 		intel_ddi_init(dev_priv, PORT_TC1);
11646 		intel_ddi_init(dev_priv, PORT_TC2);
11647 		intel_ddi_init(dev_priv, PORT_TC3);
11648 		intel_ddi_init(dev_priv, PORT_TC4);
11649 	} else if (IS_DG1(dev_priv) || IS_ROCKETLAKE(dev_priv)) {
11650 		intel_ddi_init(dev_priv, PORT_A);
11651 		intel_ddi_init(dev_priv, PORT_B);
11652 		intel_ddi_init(dev_priv, PORT_TC1);
11653 		intel_ddi_init(dev_priv, PORT_TC2);
11654 	} else if (INTEL_GEN(dev_priv) >= 12) {
11655 		intel_ddi_init(dev_priv, PORT_A);
11656 		intel_ddi_init(dev_priv, PORT_B);
11657 		intel_ddi_init(dev_priv, PORT_TC1);
11658 		intel_ddi_init(dev_priv, PORT_TC2);
11659 		intel_ddi_init(dev_priv, PORT_TC3);
11660 		intel_ddi_init(dev_priv, PORT_TC4);
11661 		intel_ddi_init(dev_priv, PORT_TC5);
11662 		intel_ddi_init(dev_priv, PORT_TC6);
11663 		icl_dsi_init(dev_priv);
11664 	} else if (IS_JSL_EHL(dev_priv)) {
11665 		intel_ddi_init(dev_priv, PORT_A);
11666 		intel_ddi_init(dev_priv, PORT_B);
11667 		intel_ddi_init(dev_priv, PORT_C);
11668 		intel_ddi_init(dev_priv, PORT_D);
11669 		icl_dsi_init(dev_priv);
11670 	} else if (IS_GEN(dev_priv, 11)) {
11671 		intel_ddi_init(dev_priv, PORT_A);
11672 		intel_ddi_init(dev_priv, PORT_B);
11673 		intel_ddi_init(dev_priv, PORT_C);
11674 		intel_ddi_init(dev_priv, PORT_D);
11675 		intel_ddi_init(dev_priv, PORT_E);
11676 		/*
11677 		 * On some ICL SKUs port F is not present. No strap bits for
11678 		 * this, so rely on VBT.
11679 		 * Work around broken VBTs on SKUs known to have no port F.
11680 		 */
11681 		if (IS_ICL_WITH_PORT_F(dev_priv) &&
11682 		    intel_bios_is_port_present(dev_priv, PORT_F))
11683 			intel_ddi_init(dev_priv, PORT_F);
11684 
11685 		icl_dsi_init(dev_priv);
11686 	} else if (IS_GEN9_LP(dev_priv)) {
11687 		/*
11688 		 * FIXME: Broxton doesn't support port detection via the
11689 		 * DDI_BUF_CTL_A or SFUSE_STRAP registers, find another way to
11690 		 * detect the ports.
11691 		 */
11692 		intel_ddi_init(dev_priv, PORT_A);
11693 		intel_ddi_init(dev_priv, PORT_B);
11694 		intel_ddi_init(dev_priv, PORT_C);
11695 
11696 		vlv_dsi_init(dev_priv);
11697 	} else if (HAS_DDI(dev_priv)) {
11698 		int found;
11699 
11700 		if (intel_ddi_crt_present(dev_priv))
11701 			intel_crt_init(dev_priv);
11702 
11703 		/*
11704 		 * Haswell uses DDI functions to detect digital outputs.
11705 		 * On SKL pre-D0 the strap isn't connected. Later SKUs may or
11706 		 * may not have it - it was supposed to be fixed by the same
11707 		 * time we stopped using straps. Assume it's there.
11708 		 */
11709 		found = intel_de_read(dev_priv, DDI_BUF_CTL(PORT_A)) & DDI_INIT_DISPLAY_DETECTED;
11710 		/* WaIgnoreDDIAStrap: skl */
11711 		if (found || IS_GEN9_BC(dev_priv))
11712 			intel_ddi_init(dev_priv, PORT_A);
11713 
11714 		/* DDI B, C, D, and F detection is indicated by the SFUSE_STRAP
11715 		 * register */
11716 		if (HAS_PCH_TGP(dev_priv)) {
11717 			/* W/A due to lack of STRAP config on TGP PCH*/
11718 			found = (SFUSE_STRAP_DDIB_DETECTED |
11719 				 SFUSE_STRAP_DDIC_DETECTED |
11720 				 SFUSE_STRAP_DDID_DETECTED);
11721 		} else {
11722 			found = intel_de_read(dev_priv, SFUSE_STRAP);
11723 		}
11724 
11725 		if (found & SFUSE_STRAP_DDIB_DETECTED)
11726 			intel_ddi_init(dev_priv, PORT_B);
11727 		if (found & SFUSE_STRAP_DDIC_DETECTED)
11728 			intel_ddi_init(dev_priv, PORT_C);
11729 		if (found & SFUSE_STRAP_DDID_DETECTED)
11730 			intel_ddi_init(dev_priv, PORT_D);
11731 		if (found & SFUSE_STRAP_DDIF_DETECTED)
11732 			intel_ddi_init(dev_priv, PORT_F);
11733 		/*
11734 		 * On SKL we don't have a way to detect DDI-E so we rely on VBT.
11735 		 */
11736 		if (IS_GEN9_BC(dev_priv) &&
11737 		    intel_bios_is_port_present(dev_priv, PORT_E))
11738 			intel_ddi_init(dev_priv, PORT_E);
11739 
11740 	} else if (HAS_PCH_SPLIT(dev_priv)) {
11741 		int found;
11742 
11743 		/*
11744 		 * intel_edp_init_connector() depends on this completing first,
11745 		 * to prevent the registration of both eDP and LVDS and the
11746 		 * incorrect sharing of the PPS.
11747 		 */
11748 		intel_lvds_init(dev_priv);
11749 		intel_crt_init(dev_priv);
11750 
11751 		dpd_is_edp = intel_dp_is_port_edp(dev_priv, PORT_D);
11752 
11753 		if (ilk_has_edp_a(dev_priv))
11754 			intel_dp_init(dev_priv, DP_A, PORT_A);
11755 
11756 		if (intel_de_read(dev_priv, PCH_HDMIB) & SDVO_DETECTED) {
11757 			/* PCH SDVOB multiplex with HDMIB */
11758 			found = intel_sdvo_init(dev_priv, PCH_SDVOB, PORT_B);
11759 			if (!found)
11760 				intel_hdmi_init(dev_priv, PCH_HDMIB, PORT_B);
11761 			if (!found && (intel_de_read(dev_priv, PCH_DP_B) & DP_DETECTED))
11762 				intel_dp_init(dev_priv, PCH_DP_B, PORT_B);
11763 		}
11764 
11765 		if (intel_de_read(dev_priv, PCH_HDMIC) & SDVO_DETECTED)
11766 			intel_hdmi_init(dev_priv, PCH_HDMIC, PORT_C);
11767 
11768 		if (!dpd_is_edp && intel_de_read(dev_priv, PCH_HDMID) & SDVO_DETECTED)
11769 			intel_hdmi_init(dev_priv, PCH_HDMID, PORT_D);
11770 
11771 		if (intel_de_read(dev_priv, PCH_DP_C) & DP_DETECTED)
11772 			intel_dp_init(dev_priv, PCH_DP_C, PORT_C);
11773 
11774 		if (intel_de_read(dev_priv, PCH_DP_D) & DP_DETECTED)
11775 			intel_dp_init(dev_priv, PCH_DP_D, PORT_D);
11776 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
11777 		bool has_edp, has_port;
11778 
11779 		if (IS_VALLEYVIEW(dev_priv) && dev_priv->vbt.int_crt_support)
11780 			intel_crt_init(dev_priv);
11781 
11782 		/*
11783 		 * The DP_DETECTED bit is the latched state of the DDC
11784 		 * SDA pin at boot. However since eDP doesn't require DDC
11785 		 * (no way to plug in a DP->HDMI dongle) the DDC pins for
11786 		 * eDP ports may have been muxed to an alternate function.
11787 		 * Thus we can't rely on the DP_DETECTED bit alone to detect
11788 		 * eDP ports. Consult the VBT as well as DP_DETECTED to
11789 		 * detect eDP ports.
11790 		 *
11791 		 * Sadly the straps seem to be missing sometimes even for HDMI
11792 		 * ports (eg. on Voyo V3 - CHT x7-Z8700), so check both strap
11793 		 * and VBT for the presence of the port. Additionally we can't
11794 		 * trust the port type the VBT declares as we've seen at least
11795 		 * HDMI ports that the VBT claim are DP or eDP.
11796 		 */
11797 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_B);
11798 		has_port = intel_bios_is_port_present(dev_priv, PORT_B);
11799 		if (intel_de_read(dev_priv, VLV_DP_B) & DP_DETECTED || has_port)
11800 			has_edp &= intel_dp_init(dev_priv, VLV_DP_B, PORT_B);
11801 		if ((intel_de_read(dev_priv, VLV_HDMIB) & SDVO_DETECTED || has_port) && !has_edp)
11802 			intel_hdmi_init(dev_priv, VLV_HDMIB, PORT_B);
11803 
11804 		has_edp = intel_dp_is_port_edp(dev_priv, PORT_C);
11805 		has_port = intel_bios_is_port_present(dev_priv, PORT_C);
11806 		if (intel_de_read(dev_priv, VLV_DP_C) & DP_DETECTED || has_port)
11807 			has_edp &= intel_dp_init(dev_priv, VLV_DP_C, PORT_C);
11808 		if ((intel_de_read(dev_priv, VLV_HDMIC) & SDVO_DETECTED || has_port) && !has_edp)
11809 			intel_hdmi_init(dev_priv, VLV_HDMIC, PORT_C);
11810 
11811 		if (IS_CHERRYVIEW(dev_priv)) {
11812 			/*
11813 			 * eDP not supported on port D,
11814 			 * so no need to worry about it
11815 			 */
11816 			has_port = intel_bios_is_port_present(dev_priv, PORT_D);
11817 			if (intel_de_read(dev_priv, CHV_DP_D) & DP_DETECTED || has_port)
11818 				intel_dp_init(dev_priv, CHV_DP_D, PORT_D);
11819 			if (intel_de_read(dev_priv, CHV_HDMID) & SDVO_DETECTED || has_port)
11820 				intel_hdmi_init(dev_priv, CHV_HDMID, PORT_D);
11821 		}
11822 
11823 		vlv_dsi_init(dev_priv);
11824 	} else if (IS_PINEVIEW(dev_priv)) {
11825 		intel_lvds_init(dev_priv);
11826 		intel_crt_init(dev_priv);
11827 	} else if (IS_GEN_RANGE(dev_priv, 3, 4)) {
11828 		bool found = false;
11829 
11830 		if (IS_MOBILE(dev_priv))
11831 			intel_lvds_init(dev_priv);
11832 
11833 		intel_crt_init(dev_priv);
11834 
11835 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
11836 			drm_dbg_kms(&dev_priv->drm, "probing SDVOB\n");
11837 			found = intel_sdvo_init(dev_priv, GEN3_SDVOB, PORT_B);
11838 			if (!found && IS_G4X(dev_priv)) {
11839 				drm_dbg_kms(&dev_priv->drm,
11840 					    "probing HDMI on SDVOB\n");
11841 				intel_hdmi_init(dev_priv, GEN4_HDMIB, PORT_B);
11842 			}
11843 
11844 			if (!found && IS_G4X(dev_priv))
11845 				intel_dp_init(dev_priv, DP_B, PORT_B);
11846 		}
11847 
11848 		/* Before G4X SDVOC doesn't have its own detect register */
11849 
11850 		if (intel_de_read(dev_priv, GEN3_SDVOB) & SDVO_DETECTED) {
11851 			drm_dbg_kms(&dev_priv->drm, "probing SDVOC\n");
11852 			found = intel_sdvo_init(dev_priv, GEN3_SDVOC, PORT_C);
11853 		}
11854 
11855 		if (!found && (intel_de_read(dev_priv, GEN3_SDVOC) & SDVO_DETECTED)) {
11856 
11857 			if (IS_G4X(dev_priv)) {
11858 				drm_dbg_kms(&dev_priv->drm,
11859 					    "probing HDMI on SDVOC\n");
11860 				intel_hdmi_init(dev_priv, GEN4_HDMIC, PORT_C);
11861 			}
11862 			if (IS_G4X(dev_priv))
11863 				intel_dp_init(dev_priv, DP_C, PORT_C);
11864 		}
11865 
11866 		if (IS_G4X(dev_priv) && (intel_de_read(dev_priv, DP_D) & DP_DETECTED))
11867 			intel_dp_init(dev_priv, DP_D, PORT_D);
11868 
11869 		if (SUPPORTS_TV(dev_priv))
11870 			intel_tv_init(dev_priv);
11871 	} else if (IS_GEN(dev_priv, 2)) {
11872 		if (IS_I85X(dev_priv))
11873 			intel_lvds_init(dev_priv);
11874 
11875 		intel_crt_init(dev_priv);
11876 		intel_dvo_init(dev_priv);
11877 	}
11878 
11879 	for_each_intel_encoder(&dev_priv->drm, encoder) {
11880 		encoder->base.possible_crtcs =
11881 			intel_encoder_possible_crtcs(encoder);
11882 		encoder->base.possible_clones =
11883 			intel_encoder_possible_clones(encoder);
11884 	}
11885 
11886 	intel_init_pch_refclk(dev_priv);
11887 
11888 	drm_helper_move_panel_connectors_to_head(&dev_priv->drm);
11889 }
11890 
11891 static void intel_user_framebuffer_destroy(struct drm_framebuffer *fb)
11892 {
11893 	struct intel_framebuffer *intel_fb = to_intel_framebuffer(fb);
11894 
11895 	drm_framebuffer_cleanup(fb);
11896 	intel_frontbuffer_put(intel_fb->frontbuffer);
11897 
11898 	kfree(intel_fb);
11899 }
11900 
11901 static int intel_user_framebuffer_create_handle(struct drm_framebuffer *fb,
11902 						struct drm_file *file,
11903 						unsigned int *handle)
11904 {
11905 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11906 	struct drm_i915_private *i915 = to_i915(obj->base.dev);
11907 
11908 	if (obj->userptr.mm) {
11909 		drm_dbg(&i915->drm,
11910 			"attempting to use a userptr for a framebuffer, denied\n");
11911 		return -EINVAL;
11912 	}
11913 
11914 	return drm_gem_handle_create(file, &obj->base, handle);
11915 }
11916 
11917 static int intel_user_framebuffer_dirty(struct drm_framebuffer *fb,
11918 					struct drm_file *file,
11919 					unsigned flags, unsigned color,
11920 					struct drm_clip_rect *clips,
11921 					unsigned num_clips)
11922 {
11923 	struct drm_i915_gem_object *obj = intel_fb_obj(fb);
11924 
11925 	i915_gem_object_flush_if_display(obj);
11926 	intel_frontbuffer_flush(to_intel_frontbuffer(fb), ORIGIN_DIRTYFB);
11927 
11928 	return 0;
11929 }
11930 
11931 static const struct drm_framebuffer_funcs intel_fb_funcs = {
11932 	.destroy = intel_user_framebuffer_destroy,
11933 	.create_handle = intel_user_framebuffer_create_handle,
11934 	.dirty = intel_user_framebuffer_dirty,
11935 };
11936 
11937 static int intel_framebuffer_init(struct intel_framebuffer *intel_fb,
11938 				  struct drm_i915_gem_object *obj,
11939 				  struct drm_mode_fb_cmd2 *mode_cmd)
11940 {
11941 	struct drm_i915_private *dev_priv = to_i915(obj->base.dev);
11942 	struct drm_framebuffer *fb = &intel_fb->base;
11943 	u32 max_stride;
11944 	unsigned int tiling, stride;
11945 	int ret = -EINVAL;
11946 	int i;
11947 
11948 	intel_fb->frontbuffer = intel_frontbuffer_get(obj);
11949 	if (!intel_fb->frontbuffer)
11950 		return -ENOMEM;
11951 
11952 	i915_gem_object_lock(obj, NULL);
11953 	tiling = i915_gem_object_get_tiling(obj);
11954 	stride = i915_gem_object_get_stride(obj);
11955 	i915_gem_object_unlock(obj);
11956 
11957 	if (mode_cmd->flags & DRM_MODE_FB_MODIFIERS) {
11958 		/*
11959 		 * If there's a fence, enforce that
11960 		 * the fb modifier and tiling mode match.
11961 		 */
11962 		if (tiling != I915_TILING_NONE &&
11963 		    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
11964 			drm_dbg_kms(&dev_priv->drm,
11965 				    "tiling_mode doesn't match fb modifier\n");
11966 			goto err;
11967 		}
11968 	} else {
11969 		if (tiling == I915_TILING_X) {
11970 			mode_cmd->modifier[0] = I915_FORMAT_MOD_X_TILED;
11971 		} else if (tiling == I915_TILING_Y) {
11972 			drm_dbg_kms(&dev_priv->drm,
11973 				    "No Y tiling for legacy addfb\n");
11974 			goto err;
11975 		}
11976 	}
11977 
11978 	if (!drm_any_plane_has_format(&dev_priv->drm,
11979 				      mode_cmd->pixel_format,
11980 				      mode_cmd->modifier[0])) {
11981 		drm_dbg_kms(&dev_priv->drm,
11982 			    "unsupported pixel format %p4cc / modifier 0x%llx\n",
11983 			    &mode_cmd->pixel_format, mode_cmd->modifier[0]);
11984 		goto err;
11985 	}
11986 
11987 	/*
11988 	 * gen2/3 display engine uses the fence if present,
11989 	 * so the tiling mode must match the fb modifier exactly.
11990 	 */
11991 	if (INTEL_GEN(dev_priv) < 4 &&
11992 	    tiling != intel_fb_modifier_to_tiling(mode_cmd->modifier[0])) {
11993 		drm_dbg_kms(&dev_priv->drm,
11994 			    "tiling_mode must match fb modifier exactly on gen2/3\n");
11995 		goto err;
11996 	}
11997 
11998 	max_stride = intel_fb_max_stride(dev_priv, mode_cmd->pixel_format,
11999 					 mode_cmd->modifier[0]);
12000 	if (mode_cmd->pitches[0] > max_stride) {
12001 		drm_dbg_kms(&dev_priv->drm,
12002 			    "%s pitch (%u) must be at most %d\n",
12003 			    mode_cmd->modifier[0] != DRM_FORMAT_MOD_LINEAR ?
12004 			    "tiled" : "linear",
12005 			    mode_cmd->pitches[0], max_stride);
12006 		goto err;
12007 	}
12008 
12009 	/*
12010 	 * If there's a fence, enforce that
12011 	 * the fb pitch and fence stride match.
12012 	 */
12013 	if (tiling != I915_TILING_NONE && mode_cmd->pitches[0] != stride) {
12014 		drm_dbg_kms(&dev_priv->drm,
12015 			    "pitch (%d) must match tiling stride (%d)\n",
12016 			    mode_cmd->pitches[0], stride);
12017 		goto err;
12018 	}
12019 
12020 	/* FIXME need to adjust LINOFF/TILEOFF accordingly. */
12021 	if (mode_cmd->offsets[0] != 0) {
12022 		drm_dbg_kms(&dev_priv->drm,
12023 			    "plane 0 offset (0x%08x) must be 0\n",
12024 			    mode_cmd->offsets[0]);
12025 		goto err;
12026 	}
12027 
12028 	drm_helper_mode_fill_fb_struct(&dev_priv->drm, fb, mode_cmd);
12029 
12030 	for (i = 0; i < fb->format->num_planes; i++) {
12031 		u32 stride_alignment;
12032 
12033 		if (mode_cmd->handles[i] != mode_cmd->handles[0]) {
12034 			drm_dbg_kms(&dev_priv->drm, "bad plane %d handle\n",
12035 				    i);
12036 			goto err;
12037 		}
12038 
12039 		stride_alignment = intel_fb_stride_alignment(fb, i);
12040 		if (fb->pitches[i] & (stride_alignment - 1)) {
12041 			drm_dbg_kms(&dev_priv->drm,
12042 				    "plane %d pitch (%d) must be at least %u byte aligned\n",
12043 				    i, fb->pitches[i], stride_alignment);
12044 			goto err;
12045 		}
12046 
12047 		if (is_gen12_ccs_plane(fb, i) && !is_gen12_ccs_cc_plane(fb, i)) {
12048 			int ccs_aux_stride = gen12_ccs_aux_stride(fb, i);
12049 
12050 			if (fb->pitches[i] != ccs_aux_stride) {
12051 				drm_dbg_kms(&dev_priv->drm,
12052 					    "ccs aux plane %d pitch (%d) must be %d\n",
12053 					    i,
12054 					    fb->pitches[i], ccs_aux_stride);
12055 				goto err;
12056 			}
12057 		}
12058 
12059 		fb->obj[i] = &obj->base;
12060 	}
12061 
12062 	ret = intel_fill_fb_info(dev_priv, fb);
12063 	if (ret)
12064 		goto err;
12065 
12066 	ret = drm_framebuffer_init(&dev_priv->drm, fb, &intel_fb_funcs);
12067 	if (ret) {
12068 		drm_err(&dev_priv->drm, "framebuffer init failed %d\n", ret);
12069 		goto err;
12070 	}
12071 
12072 	return 0;
12073 
12074 err:
12075 	intel_frontbuffer_put(intel_fb->frontbuffer);
12076 	return ret;
12077 }
12078 
12079 static struct drm_framebuffer *
12080 intel_user_framebuffer_create(struct drm_device *dev,
12081 			      struct drm_file *filp,
12082 			      const struct drm_mode_fb_cmd2 *user_mode_cmd)
12083 {
12084 	struct drm_framebuffer *fb;
12085 	struct drm_i915_gem_object *obj;
12086 	struct drm_mode_fb_cmd2 mode_cmd = *user_mode_cmd;
12087 
12088 	obj = i915_gem_object_lookup(filp, mode_cmd.handles[0]);
12089 	if (!obj)
12090 		return ERR_PTR(-ENOENT);
12091 
12092 	fb = intel_framebuffer_create(obj, &mode_cmd);
12093 	i915_gem_object_put(obj);
12094 
12095 	return fb;
12096 }
12097 
12098 static enum drm_mode_status
12099 intel_mode_valid(struct drm_device *dev,
12100 		 const struct drm_display_mode *mode)
12101 {
12102 	struct drm_i915_private *dev_priv = to_i915(dev);
12103 	int hdisplay_max, htotal_max;
12104 	int vdisplay_max, vtotal_max;
12105 
12106 	/*
12107 	 * Can't reject DBLSCAN here because Xorg ddxen can add piles
12108 	 * of DBLSCAN modes to the output's mode list when they detect
12109 	 * the scaling mode property on the connector. And they don't
12110 	 * ask the kernel to validate those modes in any way until
12111 	 * modeset time at which point the client gets a protocol error.
12112 	 * So in order to not upset those clients we silently ignore the
12113 	 * DBLSCAN flag on such connectors. For other connectors we will
12114 	 * reject modes with the DBLSCAN flag in encoder->compute_config().
12115 	 * And we always reject DBLSCAN modes in connector->mode_valid()
12116 	 * as we never want such modes on the connector's mode list.
12117 	 */
12118 
12119 	if (mode->vscan > 1)
12120 		return MODE_NO_VSCAN;
12121 
12122 	if (mode->flags & DRM_MODE_FLAG_HSKEW)
12123 		return MODE_H_ILLEGAL;
12124 
12125 	if (mode->flags & (DRM_MODE_FLAG_CSYNC |
12126 			   DRM_MODE_FLAG_NCSYNC |
12127 			   DRM_MODE_FLAG_PCSYNC))
12128 		return MODE_HSYNC;
12129 
12130 	if (mode->flags & (DRM_MODE_FLAG_BCAST |
12131 			   DRM_MODE_FLAG_PIXMUX |
12132 			   DRM_MODE_FLAG_CLKDIV2))
12133 		return MODE_BAD;
12134 
12135 	/* Transcoder timing limits */
12136 	if (INTEL_GEN(dev_priv) >= 11) {
12137 		hdisplay_max = 16384;
12138 		vdisplay_max = 8192;
12139 		htotal_max = 16384;
12140 		vtotal_max = 8192;
12141 	} else if (INTEL_GEN(dev_priv) >= 9 ||
12142 		   IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12143 		hdisplay_max = 8192; /* FDI max 4096 handled elsewhere */
12144 		vdisplay_max = 4096;
12145 		htotal_max = 8192;
12146 		vtotal_max = 8192;
12147 	} else if (INTEL_GEN(dev_priv) >= 3) {
12148 		hdisplay_max = 4096;
12149 		vdisplay_max = 4096;
12150 		htotal_max = 8192;
12151 		vtotal_max = 8192;
12152 	} else {
12153 		hdisplay_max = 2048;
12154 		vdisplay_max = 2048;
12155 		htotal_max = 4096;
12156 		vtotal_max = 4096;
12157 	}
12158 
12159 	if (mode->hdisplay > hdisplay_max ||
12160 	    mode->hsync_start > htotal_max ||
12161 	    mode->hsync_end > htotal_max ||
12162 	    mode->htotal > htotal_max)
12163 		return MODE_H_ILLEGAL;
12164 
12165 	if (mode->vdisplay > vdisplay_max ||
12166 	    mode->vsync_start > vtotal_max ||
12167 	    mode->vsync_end > vtotal_max ||
12168 	    mode->vtotal > vtotal_max)
12169 		return MODE_V_ILLEGAL;
12170 
12171 	if (INTEL_GEN(dev_priv) >= 5) {
12172 		if (mode->hdisplay < 64 ||
12173 		    mode->htotal - mode->hdisplay < 32)
12174 			return MODE_H_ILLEGAL;
12175 
12176 		if (mode->vtotal - mode->vdisplay < 5)
12177 			return MODE_V_ILLEGAL;
12178 	} else {
12179 		if (mode->htotal - mode->hdisplay < 32)
12180 			return MODE_H_ILLEGAL;
12181 
12182 		if (mode->vtotal - mode->vdisplay < 3)
12183 			return MODE_V_ILLEGAL;
12184 	}
12185 
12186 	return MODE_OK;
12187 }
12188 
12189 enum drm_mode_status
12190 intel_mode_valid_max_plane_size(struct drm_i915_private *dev_priv,
12191 				const struct drm_display_mode *mode,
12192 				bool bigjoiner)
12193 {
12194 	int plane_width_max, plane_height_max;
12195 
12196 	/*
12197 	 * intel_mode_valid() should be
12198 	 * sufficient on older platforms.
12199 	 */
12200 	if (INTEL_GEN(dev_priv) < 9)
12201 		return MODE_OK;
12202 
12203 	/*
12204 	 * Most people will probably want a fullscreen
12205 	 * plane so let's not advertize modes that are
12206 	 * too big for that.
12207 	 */
12208 	if (INTEL_GEN(dev_priv) >= 11) {
12209 		plane_width_max = 5120 << bigjoiner;
12210 		plane_height_max = 4320;
12211 	} else {
12212 		plane_width_max = 5120;
12213 		plane_height_max = 4096;
12214 	}
12215 
12216 	if (mode->hdisplay > plane_width_max)
12217 		return MODE_H_ILLEGAL;
12218 
12219 	if (mode->vdisplay > plane_height_max)
12220 		return MODE_V_ILLEGAL;
12221 
12222 	return MODE_OK;
12223 }
12224 
12225 static const struct drm_mode_config_funcs intel_mode_funcs = {
12226 	.fb_create = intel_user_framebuffer_create,
12227 	.get_format_info = intel_get_format_info,
12228 	.output_poll_changed = intel_fbdev_output_poll_changed,
12229 	.mode_valid = intel_mode_valid,
12230 	.atomic_check = intel_atomic_check,
12231 	.atomic_commit = intel_atomic_commit,
12232 	.atomic_state_alloc = intel_atomic_state_alloc,
12233 	.atomic_state_clear = intel_atomic_state_clear,
12234 	.atomic_state_free = intel_atomic_state_free,
12235 };
12236 
12237 /**
12238  * intel_init_display_hooks - initialize the display modesetting hooks
12239  * @dev_priv: device private
12240  */
12241 void intel_init_display_hooks(struct drm_i915_private *dev_priv)
12242 {
12243 	intel_init_cdclk_hooks(dev_priv);
12244 	intel_init_audio_hooks(dev_priv);
12245 
12246 	intel_dpll_init_clock_hook(dev_priv);
12247 
12248 	if (INTEL_GEN(dev_priv) >= 9) {
12249 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
12250 		dev_priv->display.crtc_enable = hsw_crtc_enable;
12251 		dev_priv->display.crtc_disable = hsw_crtc_disable;
12252 	} else if (HAS_DDI(dev_priv)) {
12253 		dev_priv->display.get_pipe_config = hsw_get_pipe_config;
12254 		dev_priv->display.crtc_enable = hsw_crtc_enable;
12255 		dev_priv->display.crtc_disable = hsw_crtc_disable;
12256 	} else if (HAS_PCH_SPLIT(dev_priv)) {
12257 		dev_priv->display.get_pipe_config = ilk_get_pipe_config;
12258 		dev_priv->display.crtc_enable = ilk_crtc_enable;
12259 		dev_priv->display.crtc_disable = ilk_crtc_disable;
12260 	} else if (IS_CHERRYVIEW(dev_priv) ||
12261 		   IS_VALLEYVIEW(dev_priv)) {
12262 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
12263 		dev_priv->display.crtc_enable = valleyview_crtc_enable;
12264 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
12265 	} else {
12266 		dev_priv->display.get_pipe_config = i9xx_get_pipe_config;
12267 		dev_priv->display.crtc_enable = i9xx_crtc_enable;
12268 		dev_priv->display.crtc_disable = i9xx_crtc_disable;
12269 	}
12270 
12271 	intel_fdi_init_hook(dev_priv);
12272 
12273 	if (INTEL_GEN(dev_priv) >= 9) {
12274 		dev_priv->display.commit_modeset_enables = skl_commit_modeset_enables;
12275 		dev_priv->display.get_initial_plane_config = skl_get_initial_plane_config;
12276 	} else {
12277 		dev_priv->display.commit_modeset_enables = intel_commit_modeset_enables;
12278 		dev_priv->display.get_initial_plane_config = i9xx_get_initial_plane_config;
12279 	}
12280 
12281 }
12282 
12283 void intel_modeset_init_hw(struct drm_i915_private *i915)
12284 {
12285 	struct intel_cdclk_state *cdclk_state =
12286 		to_intel_cdclk_state(i915->cdclk.obj.state);
12287 
12288 	intel_update_cdclk(i915);
12289 	intel_dump_cdclk_config(&i915->cdclk.hw, "Current CDCLK");
12290 	cdclk_state->logical = cdclk_state->actual = i915->cdclk.hw;
12291 }
12292 
12293 static int sanitize_watermarks_add_affected(struct drm_atomic_state *state)
12294 {
12295 	struct drm_plane *plane;
12296 	struct intel_crtc *crtc;
12297 
12298 	for_each_intel_crtc(state->dev, crtc) {
12299 		struct intel_crtc_state *crtc_state;
12300 
12301 		crtc_state = intel_atomic_get_crtc_state(state, crtc);
12302 		if (IS_ERR(crtc_state))
12303 			return PTR_ERR(crtc_state);
12304 
12305 		if (crtc_state->hw.active) {
12306 			/*
12307 			 * Preserve the inherited flag to avoid
12308 			 * taking the full modeset path.
12309 			 */
12310 			crtc_state->inherited = true;
12311 		}
12312 	}
12313 
12314 	drm_for_each_plane(plane, state->dev) {
12315 		struct drm_plane_state *plane_state;
12316 
12317 		plane_state = drm_atomic_get_plane_state(state, plane);
12318 		if (IS_ERR(plane_state))
12319 			return PTR_ERR(plane_state);
12320 	}
12321 
12322 	return 0;
12323 }
12324 
12325 /*
12326  * Calculate what we think the watermarks should be for the state we've read
12327  * out of the hardware and then immediately program those watermarks so that
12328  * we ensure the hardware settings match our internal state.
12329  *
12330  * We can calculate what we think WM's should be by creating a duplicate of the
12331  * current state (which was constructed during hardware readout) and running it
12332  * through the atomic check code to calculate new watermark values in the
12333  * state object.
12334  */
12335 static void sanitize_watermarks(struct drm_i915_private *dev_priv)
12336 {
12337 	struct drm_atomic_state *state;
12338 	struct intel_atomic_state *intel_state;
12339 	struct intel_crtc *crtc;
12340 	struct intel_crtc_state *crtc_state;
12341 	struct drm_modeset_acquire_ctx ctx;
12342 	int ret;
12343 	int i;
12344 
12345 	/* Only supported on platforms that use atomic watermark design */
12346 	if (!dev_priv->display.optimize_watermarks)
12347 		return;
12348 
12349 	state = drm_atomic_state_alloc(&dev_priv->drm);
12350 	if (drm_WARN_ON(&dev_priv->drm, !state))
12351 		return;
12352 
12353 	intel_state = to_intel_atomic_state(state);
12354 
12355 	drm_modeset_acquire_init(&ctx, 0);
12356 
12357 retry:
12358 	state->acquire_ctx = &ctx;
12359 
12360 	/*
12361 	 * Hardware readout is the only time we don't want to calculate
12362 	 * intermediate watermarks (since we don't trust the current
12363 	 * watermarks).
12364 	 */
12365 	if (!HAS_GMCH(dev_priv))
12366 		intel_state->skip_intermediate_wm = true;
12367 
12368 	ret = sanitize_watermarks_add_affected(state);
12369 	if (ret)
12370 		goto fail;
12371 
12372 	ret = intel_atomic_check(&dev_priv->drm, state);
12373 	if (ret)
12374 		goto fail;
12375 
12376 	/* Write calculated watermark values back */
12377 	for_each_new_intel_crtc_in_state(intel_state, crtc, crtc_state, i) {
12378 		crtc_state->wm.need_postvbl_update = true;
12379 		dev_priv->display.optimize_watermarks(intel_state, crtc);
12380 
12381 		to_intel_crtc_state(crtc->base.state)->wm = crtc_state->wm;
12382 	}
12383 
12384 fail:
12385 	if (ret == -EDEADLK) {
12386 		drm_atomic_state_clear(state);
12387 		drm_modeset_backoff(&ctx);
12388 		goto retry;
12389 	}
12390 
12391 	/*
12392 	 * If we fail here, it means that the hardware appears to be
12393 	 * programmed in a way that shouldn't be possible, given our
12394 	 * understanding of watermark requirements.  This might mean a
12395 	 * mistake in the hardware readout code or a mistake in the
12396 	 * watermark calculations for a given platform.  Raise a WARN
12397 	 * so that this is noticeable.
12398 	 *
12399 	 * If this actually happens, we'll have to just leave the
12400 	 * BIOS-programmed watermarks untouched and hope for the best.
12401 	 */
12402 	drm_WARN(&dev_priv->drm, ret,
12403 		 "Could not determine valid watermarks for inherited state\n");
12404 
12405 	drm_atomic_state_put(state);
12406 
12407 	drm_modeset_drop_locks(&ctx);
12408 	drm_modeset_acquire_fini(&ctx);
12409 }
12410 
12411 static void intel_update_fdi_pll_freq(struct drm_i915_private *dev_priv)
12412 {
12413 	if (IS_GEN(dev_priv, 5)) {
12414 		u32 fdi_pll_clk =
12415 			intel_de_read(dev_priv, FDI_PLL_BIOS_0) & FDI_PLL_FB_CLOCK_MASK;
12416 
12417 		dev_priv->fdi_pll_freq = (fdi_pll_clk + 2) * 10000;
12418 	} else if (IS_GEN(dev_priv, 6) || IS_IVYBRIDGE(dev_priv)) {
12419 		dev_priv->fdi_pll_freq = 270000;
12420 	} else {
12421 		return;
12422 	}
12423 
12424 	drm_dbg(&dev_priv->drm, "FDI PLL freq=%d\n", dev_priv->fdi_pll_freq);
12425 }
12426 
12427 static int intel_initial_commit(struct drm_device *dev)
12428 {
12429 	struct drm_atomic_state *state = NULL;
12430 	struct drm_modeset_acquire_ctx ctx;
12431 	struct intel_crtc *crtc;
12432 	int ret = 0;
12433 
12434 	state = drm_atomic_state_alloc(dev);
12435 	if (!state)
12436 		return -ENOMEM;
12437 
12438 	drm_modeset_acquire_init(&ctx, 0);
12439 
12440 retry:
12441 	state->acquire_ctx = &ctx;
12442 
12443 	for_each_intel_crtc(dev, crtc) {
12444 		struct intel_crtc_state *crtc_state =
12445 			intel_atomic_get_crtc_state(state, crtc);
12446 
12447 		if (IS_ERR(crtc_state)) {
12448 			ret = PTR_ERR(crtc_state);
12449 			goto out;
12450 		}
12451 
12452 		if (crtc_state->hw.active) {
12453 			struct intel_encoder *encoder;
12454 
12455 			/*
12456 			 * We've not yet detected sink capabilities
12457 			 * (audio,infoframes,etc.) and thus we don't want to
12458 			 * force a full state recomputation yet. We want that to
12459 			 * happen only for the first real commit from userspace.
12460 			 * So preserve the inherited flag for the time being.
12461 			 */
12462 			crtc_state->inherited = true;
12463 
12464 			ret = drm_atomic_add_affected_planes(state, &crtc->base);
12465 			if (ret)
12466 				goto out;
12467 
12468 			/*
12469 			 * FIXME hack to force a LUT update to avoid the
12470 			 * plane update forcing the pipe gamma on without
12471 			 * having a proper LUT loaded. Remove once we
12472 			 * have readout for pipe gamma enable.
12473 			 */
12474 			crtc_state->uapi.color_mgmt_changed = true;
12475 
12476 			for_each_intel_encoder_mask(dev, encoder,
12477 						    crtc_state->uapi.encoder_mask) {
12478 				if (encoder->initial_fastset_check &&
12479 				    !encoder->initial_fastset_check(encoder, crtc_state)) {
12480 					ret = drm_atomic_add_affected_connectors(state,
12481 										 &crtc->base);
12482 					if (ret)
12483 						goto out;
12484 				}
12485 			}
12486 		}
12487 	}
12488 
12489 	ret = drm_atomic_commit(state);
12490 
12491 out:
12492 	if (ret == -EDEADLK) {
12493 		drm_atomic_state_clear(state);
12494 		drm_modeset_backoff(&ctx);
12495 		goto retry;
12496 	}
12497 
12498 	drm_atomic_state_put(state);
12499 
12500 	drm_modeset_drop_locks(&ctx);
12501 	drm_modeset_acquire_fini(&ctx);
12502 
12503 	return ret;
12504 }
12505 
12506 static void intel_mode_config_init(struct drm_i915_private *i915)
12507 {
12508 	struct drm_mode_config *mode_config = &i915->drm.mode_config;
12509 
12510 	drm_mode_config_init(&i915->drm);
12511 	INIT_LIST_HEAD(&i915->global_obj_list);
12512 
12513 	mode_config->min_width = 0;
12514 	mode_config->min_height = 0;
12515 
12516 	mode_config->preferred_depth = 24;
12517 	mode_config->prefer_shadow = 1;
12518 
12519 	mode_config->allow_fb_modifiers = true;
12520 
12521 	mode_config->funcs = &intel_mode_funcs;
12522 
12523 	mode_config->async_page_flip = has_async_flips(i915);
12524 
12525 	/*
12526 	 * Maximum framebuffer dimensions, chosen to match
12527 	 * the maximum render engine surface size on gen4+.
12528 	 */
12529 	if (INTEL_GEN(i915) >= 7) {
12530 		mode_config->max_width = 16384;
12531 		mode_config->max_height = 16384;
12532 	} else if (INTEL_GEN(i915) >= 4) {
12533 		mode_config->max_width = 8192;
12534 		mode_config->max_height = 8192;
12535 	} else if (IS_GEN(i915, 3)) {
12536 		mode_config->max_width = 4096;
12537 		mode_config->max_height = 4096;
12538 	} else {
12539 		mode_config->max_width = 2048;
12540 		mode_config->max_height = 2048;
12541 	}
12542 
12543 	if (IS_I845G(i915) || IS_I865G(i915)) {
12544 		mode_config->cursor_width = IS_I845G(i915) ? 64 : 512;
12545 		mode_config->cursor_height = 1023;
12546 	} else if (IS_I830(i915) || IS_I85X(i915) ||
12547 		   IS_I915G(i915) || IS_I915GM(i915)) {
12548 		mode_config->cursor_width = 64;
12549 		mode_config->cursor_height = 64;
12550 	} else {
12551 		mode_config->cursor_width = 256;
12552 		mode_config->cursor_height = 256;
12553 	}
12554 }
12555 
12556 static void intel_mode_config_cleanup(struct drm_i915_private *i915)
12557 {
12558 	intel_atomic_global_obj_cleanup(i915);
12559 	drm_mode_config_cleanup(&i915->drm);
12560 }
12561 
12562 static void plane_config_fini(struct intel_initial_plane_config *plane_config)
12563 {
12564 	if (plane_config->fb) {
12565 		struct drm_framebuffer *fb = &plane_config->fb->base;
12566 
12567 		/* We may only have the stub and not a full framebuffer */
12568 		if (drm_framebuffer_read_refcount(fb))
12569 			drm_framebuffer_put(fb);
12570 		else
12571 			kfree(fb);
12572 	}
12573 
12574 	if (plane_config->vma)
12575 		i915_vma_put(plane_config->vma);
12576 }
12577 
12578 /* part #1: call before irq install */
12579 int intel_modeset_init_noirq(struct drm_i915_private *i915)
12580 {
12581 	int ret;
12582 
12583 	if (i915_inject_probe_failure(i915))
12584 		return -ENODEV;
12585 
12586 	if (HAS_DISPLAY(i915)) {
12587 		ret = drm_vblank_init(&i915->drm,
12588 				      INTEL_NUM_PIPES(i915));
12589 		if (ret)
12590 			return ret;
12591 	}
12592 
12593 	intel_bios_init(i915);
12594 
12595 	ret = intel_vga_register(i915);
12596 	if (ret)
12597 		goto cleanup_bios;
12598 
12599 	/* FIXME: completely on the wrong abstraction layer */
12600 	intel_power_domains_init_hw(i915, false);
12601 
12602 	intel_csr_ucode_init(i915);
12603 
12604 	i915->modeset_wq = alloc_ordered_workqueue("i915_modeset", 0);
12605 	i915->flip_wq = alloc_workqueue("i915_flip", WQ_HIGHPRI |
12606 					WQ_UNBOUND, WQ_UNBOUND_MAX_ACTIVE);
12607 
12608 	i915->framestart_delay = 1; /* 1-4 */
12609 
12610 	intel_mode_config_init(i915);
12611 
12612 	ret = intel_cdclk_init(i915);
12613 	if (ret)
12614 		goto cleanup_vga_client_pw_domain_csr;
12615 
12616 	ret = intel_dbuf_init(i915);
12617 	if (ret)
12618 		goto cleanup_vga_client_pw_domain_csr;
12619 
12620 	ret = intel_bw_init(i915);
12621 	if (ret)
12622 		goto cleanup_vga_client_pw_domain_csr;
12623 
12624 	init_llist_head(&i915->atomic_helper.free_list);
12625 	INIT_WORK(&i915->atomic_helper.free_work,
12626 		  intel_atomic_helper_free_state_worker);
12627 
12628 	intel_init_quirks(i915);
12629 
12630 	intel_fbc_init(i915);
12631 
12632 	return 0;
12633 
12634 cleanup_vga_client_pw_domain_csr:
12635 	intel_csr_ucode_fini(i915);
12636 	intel_power_domains_driver_remove(i915);
12637 	intel_vga_unregister(i915);
12638 cleanup_bios:
12639 	intel_bios_driver_remove(i915);
12640 
12641 	return ret;
12642 }
12643 
12644 /* part #2: call after irq install, but before gem init */
12645 int intel_modeset_init_nogem(struct drm_i915_private *i915)
12646 {
12647 	struct drm_device *dev = &i915->drm;
12648 	enum pipe pipe;
12649 	struct intel_crtc *crtc;
12650 	int ret;
12651 
12652 	intel_init_pm(i915);
12653 
12654 	intel_panel_sanitize_ssc(i915);
12655 
12656 	intel_pps_setup(i915);
12657 
12658 	intel_gmbus_setup(i915);
12659 
12660 	drm_dbg_kms(&i915->drm, "%d display pipe%s available.\n",
12661 		    INTEL_NUM_PIPES(i915),
12662 		    INTEL_NUM_PIPES(i915) > 1 ? "s" : "");
12663 
12664 	if (HAS_DISPLAY(i915)) {
12665 		for_each_pipe(i915, pipe) {
12666 			ret = intel_crtc_init(i915, pipe);
12667 			if (ret) {
12668 				intel_mode_config_cleanup(i915);
12669 				return ret;
12670 			}
12671 		}
12672 	}
12673 
12674 	intel_plane_possible_crtcs_init(i915);
12675 	intel_shared_dpll_init(dev);
12676 	intel_update_fdi_pll_freq(i915);
12677 
12678 	intel_update_czclk(i915);
12679 	intel_modeset_init_hw(i915);
12680 	intel_dpll_update_ref_clks(i915);
12681 
12682 	intel_hdcp_component_init(i915);
12683 
12684 	if (i915->max_cdclk_freq == 0)
12685 		intel_update_max_cdclk(i915);
12686 
12687 	/*
12688 	 * If the platform has HTI, we need to find out whether it has reserved
12689 	 * any display resources before we create our display outputs.
12690 	 */
12691 	if (INTEL_INFO(i915)->display.has_hti)
12692 		i915->hti_state = intel_de_read(i915, HDPORT_STATE);
12693 
12694 	/* Just disable it once at startup */
12695 	intel_vga_disable(i915);
12696 	intel_setup_outputs(i915);
12697 
12698 	drm_modeset_lock_all(dev);
12699 	intel_modeset_setup_hw_state(dev, dev->mode_config.acquire_ctx);
12700 	drm_modeset_unlock_all(dev);
12701 
12702 	for_each_intel_crtc(dev, crtc) {
12703 		struct intel_initial_plane_config plane_config = {};
12704 
12705 		if (!to_intel_crtc_state(crtc->base.state)->uapi.active)
12706 			continue;
12707 
12708 		/*
12709 		 * Note that reserving the BIOS fb up front prevents us
12710 		 * from stuffing other stolen allocations like the ring
12711 		 * on top.  This prevents some ugliness at boot time, and
12712 		 * can even allow for smooth boot transitions if the BIOS
12713 		 * fb is large enough for the active pipe configuration.
12714 		 */
12715 		i915->display.get_initial_plane_config(crtc, &plane_config);
12716 
12717 		/*
12718 		 * If the fb is shared between multiple heads, we'll
12719 		 * just get the first one.
12720 		 */
12721 		intel_find_initial_plane_obj(crtc, &plane_config);
12722 
12723 		plane_config_fini(&plane_config);
12724 	}
12725 
12726 	/*
12727 	 * Make sure hardware watermarks really match the state we read out.
12728 	 * Note that we need to do this after reconstructing the BIOS fb's
12729 	 * since the watermark calculation done here will use pstate->fb.
12730 	 */
12731 	if (!HAS_GMCH(i915))
12732 		sanitize_watermarks(i915);
12733 
12734 	return 0;
12735 }
12736 
12737 /* part #3: call after gem init */
12738 int intel_modeset_init(struct drm_i915_private *i915)
12739 {
12740 	int ret;
12741 
12742 	if (!HAS_DISPLAY(i915))
12743 		return 0;
12744 
12745 	/*
12746 	 * Force all active planes to recompute their states. So that on
12747 	 * mode_setcrtc after probe, all the intel_plane_state variables
12748 	 * are already calculated and there is no assert_plane warnings
12749 	 * during bootup.
12750 	 */
12751 	ret = intel_initial_commit(&i915->drm);
12752 	if (ret)
12753 		drm_dbg_kms(&i915->drm, "Initial modeset failed, %d\n", ret);
12754 
12755 	intel_overlay_setup(i915);
12756 
12757 	ret = intel_fbdev_init(&i915->drm);
12758 	if (ret)
12759 		return ret;
12760 
12761 	/* Only enable hotplug handling once the fbdev is fully set up. */
12762 	intel_hpd_init(i915);
12763 	intel_hpd_poll_disable(i915);
12764 
12765 	intel_init_ipc(i915);
12766 
12767 	return 0;
12768 }
12769 
12770 void i830_enable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
12771 {
12772 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12773 	/* 640x480@60Hz, ~25175 kHz */
12774 	struct dpll clock = {
12775 		.m1 = 18,
12776 		.m2 = 7,
12777 		.p1 = 13,
12778 		.p2 = 4,
12779 		.n = 2,
12780 	};
12781 	u32 dpll, fp;
12782 	int i;
12783 
12784 	drm_WARN_ON(&dev_priv->drm,
12785 		    i9xx_calc_dpll_params(48000, &clock) != 25154);
12786 
12787 	drm_dbg_kms(&dev_priv->drm,
12788 		    "enabling pipe %c due to force quirk (vco=%d dot=%d)\n",
12789 		    pipe_name(pipe), clock.vco, clock.dot);
12790 
12791 	fp = i9xx_dpll_compute_fp(&clock);
12792 	dpll = DPLL_DVO_2X_MODE |
12793 		DPLL_VGA_MODE_DIS |
12794 		((clock.p1 - 2) << DPLL_FPA01_P1_POST_DIV_SHIFT) |
12795 		PLL_P2_DIVIDE_BY_4 |
12796 		PLL_REF_INPUT_DREFCLK |
12797 		DPLL_VCO_ENABLE;
12798 
12799 	intel_de_write(dev_priv, FP0(pipe), fp);
12800 	intel_de_write(dev_priv, FP1(pipe), fp);
12801 
12802 	intel_de_write(dev_priv, HTOTAL(pipe), (640 - 1) | ((800 - 1) << 16));
12803 	intel_de_write(dev_priv, HBLANK(pipe), (640 - 1) | ((800 - 1) << 16));
12804 	intel_de_write(dev_priv, HSYNC(pipe), (656 - 1) | ((752 - 1) << 16));
12805 	intel_de_write(dev_priv, VTOTAL(pipe), (480 - 1) | ((525 - 1) << 16));
12806 	intel_de_write(dev_priv, VBLANK(pipe), (480 - 1) | ((525 - 1) << 16));
12807 	intel_de_write(dev_priv, VSYNC(pipe), (490 - 1) | ((492 - 1) << 16));
12808 	intel_de_write(dev_priv, PIPESRC(pipe), ((640 - 1) << 16) | (480 - 1));
12809 
12810 	/*
12811 	 * Apparently we need to have VGA mode enabled prior to changing
12812 	 * the P1/P2 dividers. Otherwise the DPLL will keep using the old
12813 	 * dividers, even though the register value does change.
12814 	 */
12815 	intel_de_write(dev_priv, DPLL(pipe), dpll & ~DPLL_VGA_MODE_DIS);
12816 	intel_de_write(dev_priv, DPLL(pipe), dpll);
12817 
12818 	/* Wait for the clocks to stabilize. */
12819 	intel_de_posting_read(dev_priv, DPLL(pipe));
12820 	udelay(150);
12821 
12822 	/* The pixel multiplier can only be updated once the
12823 	 * DPLL is enabled and the clocks are stable.
12824 	 *
12825 	 * So write it again.
12826 	 */
12827 	intel_de_write(dev_priv, DPLL(pipe), dpll);
12828 
12829 	/* We do this three times for luck */
12830 	for (i = 0; i < 3 ; i++) {
12831 		intel_de_write(dev_priv, DPLL(pipe), dpll);
12832 		intel_de_posting_read(dev_priv, DPLL(pipe));
12833 		udelay(150); /* wait for warmup */
12834 	}
12835 
12836 	intel_de_write(dev_priv, PIPECONF(pipe),
12837 		       PIPECONF_ENABLE | PIPECONF_PROGRESSIVE);
12838 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
12839 
12840 	intel_wait_for_pipe_scanline_moving(crtc);
12841 }
12842 
12843 void i830_disable_pipe(struct drm_i915_private *dev_priv, enum pipe pipe)
12844 {
12845 	struct intel_crtc *crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12846 
12847 	drm_dbg_kms(&dev_priv->drm, "disabling pipe %c due to force quirk\n",
12848 		    pipe_name(pipe));
12849 
12850 	drm_WARN_ON(&dev_priv->drm,
12851 		    intel_de_read(dev_priv, DSPCNTR(PLANE_A)) &
12852 		    DISPLAY_PLANE_ENABLE);
12853 	drm_WARN_ON(&dev_priv->drm,
12854 		    intel_de_read(dev_priv, DSPCNTR(PLANE_B)) &
12855 		    DISPLAY_PLANE_ENABLE);
12856 	drm_WARN_ON(&dev_priv->drm,
12857 		    intel_de_read(dev_priv, DSPCNTR(PLANE_C)) &
12858 		    DISPLAY_PLANE_ENABLE);
12859 	drm_WARN_ON(&dev_priv->drm,
12860 		    intel_de_read(dev_priv, CURCNTR(PIPE_A)) & MCURSOR_MODE);
12861 	drm_WARN_ON(&dev_priv->drm,
12862 		    intel_de_read(dev_priv, CURCNTR(PIPE_B)) & MCURSOR_MODE);
12863 
12864 	intel_de_write(dev_priv, PIPECONF(pipe), 0);
12865 	intel_de_posting_read(dev_priv, PIPECONF(pipe));
12866 
12867 	intel_wait_for_pipe_scanline_stopped(crtc);
12868 
12869 	intel_de_write(dev_priv, DPLL(pipe), DPLL_VGA_MODE_DIS);
12870 	intel_de_posting_read(dev_priv, DPLL(pipe));
12871 }
12872 
12873 static void
12874 intel_sanitize_plane_mapping(struct drm_i915_private *dev_priv)
12875 {
12876 	struct intel_crtc *crtc;
12877 
12878 	if (INTEL_GEN(dev_priv) >= 4)
12879 		return;
12880 
12881 	for_each_intel_crtc(&dev_priv->drm, crtc) {
12882 		struct intel_plane *plane =
12883 			to_intel_plane(crtc->base.primary);
12884 		struct intel_crtc *plane_crtc;
12885 		enum pipe pipe;
12886 
12887 		if (!plane->get_hw_state(plane, &pipe))
12888 			continue;
12889 
12890 		if (pipe == crtc->pipe)
12891 			continue;
12892 
12893 		drm_dbg_kms(&dev_priv->drm,
12894 			    "[PLANE:%d:%s] attached to the wrong pipe, disabling plane\n",
12895 			    plane->base.base.id, plane->base.name);
12896 
12897 		plane_crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
12898 		intel_plane_disable_noatomic(plane_crtc, plane);
12899 	}
12900 }
12901 
12902 static bool intel_crtc_has_encoders(struct intel_crtc *crtc)
12903 {
12904 	struct drm_device *dev = crtc->base.dev;
12905 	struct intel_encoder *encoder;
12906 
12907 	for_each_encoder_on_crtc(dev, &crtc->base, encoder)
12908 		return true;
12909 
12910 	return false;
12911 }
12912 
12913 static struct intel_connector *intel_encoder_find_connector(struct intel_encoder *encoder)
12914 {
12915 	struct drm_device *dev = encoder->base.dev;
12916 	struct intel_connector *connector;
12917 
12918 	for_each_connector_on_encoder(dev, &encoder->base, connector)
12919 		return connector;
12920 
12921 	return NULL;
12922 }
12923 
12924 static bool has_pch_trancoder(struct drm_i915_private *dev_priv,
12925 			      enum pipe pch_transcoder)
12926 {
12927 	return HAS_PCH_IBX(dev_priv) || HAS_PCH_CPT(dev_priv) ||
12928 		(HAS_PCH_LPT_H(dev_priv) && pch_transcoder == PIPE_A);
12929 }
12930 
12931 static void intel_sanitize_frame_start_delay(const struct intel_crtc_state *crtc_state)
12932 {
12933 	struct intel_crtc *crtc = to_intel_crtc(crtc_state->uapi.crtc);
12934 	struct drm_i915_private *dev_priv = to_i915(crtc->base.dev);
12935 	enum transcoder cpu_transcoder = crtc_state->cpu_transcoder;
12936 
12937 	if (INTEL_GEN(dev_priv) >= 9 ||
12938 	    IS_BROADWELL(dev_priv) || IS_HASWELL(dev_priv)) {
12939 		i915_reg_t reg = CHICKEN_TRANS(cpu_transcoder);
12940 		u32 val;
12941 
12942 		if (transcoder_is_dsi(cpu_transcoder))
12943 			return;
12944 
12945 		val = intel_de_read(dev_priv, reg);
12946 		val &= ~HSW_FRAME_START_DELAY_MASK;
12947 		val |= HSW_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12948 		intel_de_write(dev_priv, reg, val);
12949 	} else {
12950 		i915_reg_t reg = PIPECONF(cpu_transcoder);
12951 		u32 val;
12952 
12953 		val = intel_de_read(dev_priv, reg);
12954 		val &= ~PIPECONF_FRAME_START_DELAY_MASK;
12955 		val |= PIPECONF_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12956 		intel_de_write(dev_priv, reg, val);
12957 	}
12958 
12959 	if (!crtc_state->has_pch_encoder)
12960 		return;
12961 
12962 	if (HAS_PCH_IBX(dev_priv)) {
12963 		i915_reg_t reg = PCH_TRANSCONF(crtc->pipe);
12964 		u32 val;
12965 
12966 		val = intel_de_read(dev_priv, reg);
12967 		val &= ~TRANS_FRAME_START_DELAY_MASK;
12968 		val |= TRANS_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12969 		intel_de_write(dev_priv, reg, val);
12970 	} else {
12971 		enum pipe pch_transcoder = intel_crtc_pch_transcoder(crtc);
12972 		i915_reg_t reg = TRANS_CHICKEN2(pch_transcoder);
12973 		u32 val;
12974 
12975 		val = intel_de_read(dev_priv, reg);
12976 		val &= ~TRANS_CHICKEN2_FRAME_START_DELAY_MASK;
12977 		val |= TRANS_CHICKEN2_FRAME_START_DELAY(dev_priv->framestart_delay - 1);
12978 		intel_de_write(dev_priv, reg, val);
12979 	}
12980 }
12981 
12982 static void intel_sanitize_crtc(struct intel_crtc *crtc,
12983 				struct drm_modeset_acquire_ctx *ctx)
12984 {
12985 	struct drm_device *dev = crtc->base.dev;
12986 	struct drm_i915_private *dev_priv = to_i915(dev);
12987 	struct intel_crtc_state *crtc_state = to_intel_crtc_state(crtc->base.state);
12988 
12989 	if (crtc_state->hw.active) {
12990 		struct intel_plane *plane;
12991 
12992 		/* Clear any frame start delays used for debugging left by the BIOS */
12993 		intel_sanitize_frame_start_delay(crtc_state);
12994 
12995 		/* Disable everything but the primary plane */
12996 		for_each_intel_plane_on_crtc(dev, crtc, plane) {
12997 			const struct intel_plane_state *plane_state =
12998 				to_intel_plane_state(plane->base.state);
12999 
13000 			if (plane_state->uapi.visible &&
13001 			    plane->base.type != DRM_PLANE_TYPE_PRIMARY)
13002 				intel_plane_disable_noatomic(crtc, plane);
13003 		}
13004 
13005 		/*
13006 		 * Disable any background color set by the BIOS, but enable the
13007 		 * gamma and CSC to match how we program our planes.
13008 		 */
13009 		if (INTEL_GEN(dev_priv) >= 9)
13010 			intel_de_write(dev_priv, SKL_BOTTOM_COLOR(crtc->pipe),
13011 				       SKL_BOTTOM_COLOR_GAMMA_ENABLE | SKL_BOTTOM_COLOR_CSC_ENABLE);
13012 	}
13013 
13014 	/* Adjust the state of the output pipe according to whether we
13015 	 * have active connectors/encoders. */
13016 	if (crtc_state->hw.active && !intel_crtc_has_encoders(crtc) &&
13017 	    !crtc_state->bigjoiner_slave)
13018 		intel_crtc_disable_noatomic(crtc, ctx);
13019 
13020 	if (crtc_state->hw.active || HAS_GMCH(dev_priv)) {
13021 		/*
13022 		 * We start out with underrun reporting disabled to avoid races.
13023 		 * For correct bookkeeping mark this on active crtcs.
13024 		 *
13025 		 * Also on gmch platforms we dont have any hardware bits to
13026 		 * disable the underrun reporting. Which means we need to start
13027 		 * out with underrun reporting disabled also on inactive pipes,
13028 		 * since otherwise we'll complain about the garbage we read when
13029 		 * e.g. coming up after runtime pm.
13030 		 *
13031 		 * No protection against concurrent access is required - at
13032 		 * worst a fifo underrun happens which also sets this to false.
13033 		 */
13034 		crtc->cpu_fifo_underrun_disabled = true;
13035 		/*
13036 		 * We track the PCH trancoder underrun reporting state
13037 		 * within the crtc. With crtc for pipe A housing the underrun
13038 		 * reporting state for PCH transcoder A, crtc for pipe B housing
13039 		 * it for PCH transcoder B, etc. LPT-H has only PCH transcoder A,
13040 		 * and marking underrun reporting as disabled for the non-existing
13041 		 * PCH transcoders B and C would prevent enabling the south
13042 		 * error interrupt (see cpt_can_enable_serr_int()).
13043 		 */
13044 		if (has_pch_trancoder(dev_priv, crtc->pipe))
13045 			crtc->pch_fifo_underrun_disabled = true;
13046 	}
13047 }
13048 
13049 static bool has_bogus_dpll_config(const struct intel_crtc_state *crtc_state)
13050 {
13051 	struct drm_i915_private *dev_priv = to_i915(crtc_state->uapi.crtc->dev);
13052 
13053 	/*
13054 	 * Some SNB BIOSen (eg. ASUS K53SV) are known to misprogram
13055 	 * the hardware when a high res displays plugged in. DPLL P
13056 	 * divider is zero, and the pipe timings are bonkers. We'll
13057 	 * try to disable everything in that case.
13058 	 *
13059 	 * FIXME would be nice to be able to sanitize this state
13060 	 * without several WARNs, but for now let's take the easy
13061 	 * road.
13062 	 */
13063 	return IS_GEN(dev_priv, 6) &&
13064 		crtc_state->hw.active &&
13065 		crtc_state->shared_dpll &&
13066 		crtc_state->port_clock == 0;
13067 }
13068 
13069 static void intel_sanitize_encoder(struct intel_encoder *encoder)
13070 {
13071 	struct drm_i915_private *dev_priv = to_i915(encoder->base.dev);
13072 	struct intel_connector *connector;
13073 	struct intel_crtc *crtc = to_intel_crtc(encoder->base.crtc);
13074 	struct intel_crtc_state *crtc_state = crtc ?
13075 		to_intel_crtc_state(crtc->base.state) : NULL;
13076 
13077 	/* We need to check both for a crtc link (meaning that the
13078 	 * encoder is active and trying to read from a pipe) and the
13079 	 * pipe itself being active. */
13080 	bool has_active_crtc = crtc_state &&
13081 		crtc_state->hw.active;
13082 
13083 	if (crtc_state && has_bogus_dpll_config(crtc_state)) {
13084 		drm_dbg_kms(&dev_priv->drm,
13085 			    "BIOS has misprogrammed the hardware. Disabling pipe %c\n",
13086 			    pipe_name(crtc->pipe));
13087 		has_active_crtc = false;
13088 	}
13089 
13090 	connector = intel_encoder_find_connector(encoder);
13091 	if (connector && !has_active_crtc) {
13092 		drm_dbg_kms(&dev_priv->drm,
13093 			    "[ENCODER:%d:%s] has active connectors but no active pipe!\n",
13094 			    encoder->base.base.id,
13095 			    encoder->base.name);
13096 
13097 		/* Connector is active, but has no active pipe. This is
13098 		 * fallout from our resume register restoring. Disable
13099 		 * the encoder manually again. */
13100 		if (crtc_state) {
13101 			struct drm_encoder *best_encoder;
13102 
13103 			drm_dbg_kms(&dev_priv->drm,
13104 				    "[ENCODER:%d:%s] manually disabled\n",
13105 				    encoder->base.base.id,
13106 				    encoder->base.name);
13107 
13108 			/* avoid oopsing in case the hooks consult best_encoder */
13109 			best_encoder = connector->base.state->best_encoder;
13110 			connector->base.state->best_encoder = &encoder->base;
13111 
13112 			/* FIXME NULL atomic state passed! */
13113 			if (encoder->disable)
13114 				encoder->disable(NULL, encoder, crtc_state,
13115 						 connector->base.state);
13116 			if (encoder->post_disable)
13117 				encoder->post_disable(NULL, encoder, crtc_state,
13118 						      connector->base.state);
13119 
13120 			connector->base.state->best_encoder = best_encoder;
13121 		}
13122 		encoder->base.crtc = NULL;
13123 
13124 		/* Inconsistent output/port/pipe state happens presumably due to
13125 		 * a bug in one of the get_hw_state functions. Or someplace else
13126 		 * in our code, like the register restore mess on resume. Clamp
13127 		 * things to off as a safer default. */
13128 
13129 		connector->base.dpms = DRM_MODE_DPMS_OFF;
13130 		connector->base.encoder = NULL;
13131 	}
13132 
13133 	/* notify opregion of the sanitized encoder state */
13134 	intel_opregion_notify_encoder(encoder, connector && has_active_crtc);
13135 
13136 	if (HAS_DDI(dev_priv))
13137 		intel_ddi_sanitize_encoder_pll_mapping(encoder);
13138 }
13139 
13140 /* FIXME read out full plane state for all planes */
13141 static void readout_plane_state(struct drm_i915_private *dev_priv)
13142 {
13143 	struct intel_plane *plane;
13144 	struct intel_crtc *crtc;
13145 
13146 	for_each_intel_plane(&dev_priv->drm, plane) {
13147 		struct intel_plane_state *plane_state =
13148 			to_intel_plane_state(plane->base.state);
13149 		struct intel_crtc_state *crtc_state;
13150 		enum pipe pipe = PIPE_A;
13151 		bool visible;
13152 
13153 		visible = plane->get_hw_state(plane, &pipe);
13154 
13155 		crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
13156 		crtc_state = to_intel_crtc_state(crtc->base.state);
13157 
13158 		intel_set_plane_visible(crtc_state, plane_state, visible);
13159 
13160 		drm_dbg_kms(&dev_priv->drm,
13161 			    "[PLANE:%d:%s] hw state readout: %s, pipe %c\n",
13162 			    plane->base.base.id, plane->base.name,
13163 			    enableddisabled(visible), pipe_name(pipe));
13164 	}
13165 
13166 	for_each_intel_crtc(&dev_priv->drm, crtc) {
13167 		struct intel_crtc_state *crtc_state =
13168 			to_intel_crtc_state(crtc->base.state);
13169 
13170 		fixup_plane_bitmasks(crtc_state);
13171 	}
13172 }
13173 
13174 static void intel_modeset_readout_hw_state(struct drm_device *dev)
13175 {
13176 	struct drm_i915_private *dev_priv = to_i915(dev);
13177 	struct intel_cdclk_state *cdclk_state =
13178 		to_intel_cdclk_state(dev_priv->cdclk.obj.state);
13179 	struct intel_dbuf_state *dbuf_state =
13180 		to_intel_dbuf_state(dev_priv->dbuf.obj.state);
13181 	enum pipe pipe;
13182 	struct intel_crtc *crtc;
13183 	struct intel_encoder *encoder;
13184 	struct intel_connector *connector;
13185 	struct drm_connector_list_iter conn_iter;
13186 	u8 active_pipes = 0;
13187 
13188 	for_each_intel_crtc(dev, crtc) {
13189 		struct intel_crtc_state *crtc_state =
13190 			to_intel_crtc_state(crtc->base.state);
13191 
13192 		__drm_atomic_helper_crtc_destroy_state(&crtc_state->uapi);
13193 		intel_crtc_free_hw_state(crtc_state);
13194 		intel_crtc_state_reset(crtc_state, crtc);
13195 
13196 		intel_crtc_get_pipe_config(crtc_state);
13197 
13198 		crtc_state->hw.enable = crtc_state->hw.active;
13199 
13200 		crtc->base.enabled = crtc_state->hw.enable;
13201 		crtc->active = crtc_state->hw.active;
13202 
13203 		if (crtc_state->hw.active)
13204 			active_pipes |= BIT(crtc->pipe);
13205 
13206 		drm_dbg_kms(&dev_priv->drm,
13207 			    "[CRTC:%d:%s] hw state readout: %s\n",
13208 			    crtc->base.base.id, crtc->base.name,
13209 			    enableddisabled(crtc_state->hw.active));
13210 	}
13211 
13212 	dev_priv->active_pipes = cdclk_state->active_pipes =
13213 		dbuf_state->active_pipes = active_pipes;
13214 
13215 	readout_plane_state(dev_priv);
13216 
13217 	for_each_intel_encoder(dev, encoder) {
13218 		pipe = 0;
13219 
13220 		if (encoder->get_hw_state(encoder, &pipe)) {
13221 			struct intel_crtc_state *crtc_state;
13222 
13223 			crtc = intel_get_crtc_for_pipe(dev_priv, pipe);
13224 			crtc_state = to_intel_crtc_state(crtc->base.state);
13225 
13226 			encoder->base.crtc = &crtc->base;
13227 			intel_encoder_get_config(encoder, crtc_state);
13228 			if (encoder->sync_state)
13229 				encoder->sync_state(encoder, crtc_state);
13230 
13231 			/* read out to slave crtc as well for bigjoiner */
13232 			if (crtc_state->bigjoiner) {
13233 				/* encoder should read be linked to bigjoiner master */
13234 				WARN_ON(crtc_state->bigjoiner_slave);
13235 
13236 				crtc = crtc_state->bigjoiner_linked_crtc;
13237 				crtc_state = to_intel_crtc_state(crtc->base.state);
13238 				intel_encoder_get_config(encoder, crtc_state);
13239 			}
13240 		} else {
13241 			encoder->base.crtc = NULL;
13242 		}
13243 
13244 		drm_dbg_kms(&dev_priv->drm,
13245 			    "[ENCODER:%d:%s] hw state readout: %s, pipe %c\n",
13246 			    encoder->base.base.id, encoder->base.name,
13247 			    enableddisabled(encoder->base.crtc),
13248 			    pipe_name(pipe));
13249 	}
13250 
13251 	intel_dpll_readout_hw_state(dev_priv);
13252 
13253 	drm_connector_list_iter_begin(dev, &conn_iter);
13254 	for_each_intel_connector_iter(connector, &conn_iter) {
13255 		if (connector->get_hw_state(connector)) {
13256 			struct intel_crtc_state *crtc_state;
13257 			struct intel_crtc *crtc;
13258 
13259 			connector->base.dpms = DRM_MODE_DPMS_ON;
13260 
13261 			encoder = intel_attached_encoder(connector);
13262 			connector->base.encoder = &encoder->base;
13263 
13264 			crtc = to_intel_crtc(encoder->base.crtc);
13265 			crtc_state = crtc ? to_intel_crtc_state(crtc->base.state) : NULL;
13266 
13267 			if (crtc_state && crtc_state->hw.active) {
13268 				/*
13269 				 * This has to be done during hardware readout
13270 				 * because anything calling .crtc_disable may
13271 				 * rely on the connector_mask being accurate.
13272 				 */
13273 				crtc_state->uapi.connector_mask |=
13274 					drm_connector_mask(&connector->base);
13275 				crtc_state->uapi.encoder_mask |=
13276 					drm_encoder_mask(&encoder->base);
13277 			}
13278 		} else {
13279 			connector->base.dpms = DRM_MODE_DPMS_OFF;
13280 			connector->base.encoder = NULL;
13281 		}
13282 		drm_dbg_kms(&dev_priv->drm,
13283 			    "[CONNECTOR:%d:%s] hw state readout: %s\n",
13284 			    connector->base.base.id, connector->base.name,
13285 			    enableddisabled(connector->base.encoder));
13286 	}
13287 	drm_connector_list_iter_end(&conn_iter);
13288 
13289 	for_each_intel_crtc(dev, crtc) {
13290 		struct intel_bw_state *bw_state =
13291 			to_intel_bw_state(dev_priv->bw_obj.state);
13292 		struct intel_crtc_state *crtc_state =
13293 			to_intel_crtc_state(crtc->base.state);
13294 		struct intel_plane *plane;
13295 		int min_cdclk = 0;
13296 
13297 		if (crtc_state->bigjoiner_slave)
13298 			continue;
13299 
13300 		if (crtc_state->hw.active) {
13301 			/*
13302 			 * The initial mode needs to be set in order to keep
13303 			 * the atomic core happy. It wants a valid mode if the
13304 			 * crtc's enabled, so we do the above call.
13305 			 *
13306 			 * But we don't set all the derived state fully, hence
13307 			 * set a flag to indicate that a full recalculation is
13308 			 * needed on the next commit.
13309 			 */
13310 			crtc_state->inherited = true;
13311 
13312 			intel_crtc_update_active_timings(crtc_state);
13313 
13314 			intel_crtc_copy_hw_to_uapi_state(crtc_state);
13315 		}
13316 
13317 		for_each_intel_plane_on_crtc(&dev_priv->drm, crtc, plane) {
13318 			const struct intel_plane_state *plane_state =
13319 				to_intel_plane_state(plane->base.state);
13320 
13321 			/*
13322 			 * FIXME don't have the fb yet, so can't
13323 			 * use intel_plane_data_rate() :(
13324 			 */
13325 			if (plane_state->uapi.visible)
13326 				crtc_state->data_rate[plane->id] =
13327 					4 * crtc_state->pixel_rate;
13328 			/*
13329 			 * FIXME don't have the fb yet, so can't
13330 			 * use plane->min_cdclk() :(
13331 			 */
13332 			if (plane_state->uapi.visible && plane->min_cdclk) {
13333 				if (crtc_state->double_wide ||
13334 				    INTEL_GEN(dev_priv) >= 10 || IS_GEMINILAKE(dev_priv))
13335 					crtc_state->min_cdclk[plane->id] =
13336 						DIV_ROUND_UP(crtc_state->pixel_rate, 2);
13337 				else
13338 					crtc_state->min_cdclk[plane->id] =
13339 						crtc_state->pixel_rate;
13340 			}
13341 			drm_dbg_kms(&dev_priv->drm,
13342 				    "[PLANE:%d:%s] min_cdclk %d kHz\n",
13343 				    plane->base.base.id, plane->base.name,
13344 				    crtc_state->min_cdclk[plane->id]);
13345 		}
13346 
13347 		if (crtc_state->hw.active) {
13348 			min_cdclk = intel_crtc_compute_min_cdclk(crtc_state);
13349 			if (drm_WARN_ON(dev, min_cdclk < 0))
13350 				min_cdclk = 0;
13351 		}
13352 
13353 		cdclk_state->min_cdclk[crtc->pipe] = min_cdclk;
13354 		cdclk_state->min_voltage_level[crtc->pipe] =
13355 			crtc_state->min_voltage_level;
13356 
13357 		intel_bw_crtc_update(bw_state, crtc_state);
13358 
13359 		intel_pipe_config_sanity_check(dev_priv, crtc_state);
13360 
13361 		/* discard our incomplete slave state, copy it from master */
13362 		if (crtc_state->bigjoiner && crtc_state->hw.active) {
13363 			struct intel_crtc *slave = crtc_state->bigjoiner_linked_crtc;
13364 			struct intel_crtc_state *slave_crtc_state =
13365 				to_intel_crtc_state(slave->base.state);
13366 
13367 			copy_bigjoiner_crtc_state(slave_crtc_state, crtc_state);
13368 			slave->base.mode = crtc->base.mode;
13369 
13370 			cdclk_state->min_cdclk[slave->pipe] = min_cdclk;
13371 			cdclk_state->min_voltage_level[slave->pipe] =
13372 				crtc_state->min_voltage_level;
13373 
13374 			for_each_intel_plane_on_crtc(&dev_priv->drm, slave, plane) {
13375 				const struct intel_plane_state *plane_state =
13376 					to_intel_plane_state(plane->base.state);
13377 
13378 				/*
13379 				 * FIXME don't have the fb yet, so can't
13380 				 * use intel_plane_data_rate() :(
13381 				 */
13382 				if (plane_state->uapi.visible)
13383 					crtc_state->data_rate[plane->id] =
13384 						4 * crtc_state->pixel_rate;
13385 				else
13386 					crtc_state->data_rate[plane->id] = 0;
13387 			}
13388 
13389 			intel_bw_crtc_update(bw_state, slave_crtc_state);
13390 			drm_calc_timestamping_constants(&slave->base,
13391 							&slave_crtc_state->hw.adjusted_mode);
13392 		}
13393 	}
13394 }
13395 
13396 static void
13397 get_encoder_power_domains(struct drm_i915_private *dev_priv)
13398 {
13399 	struct intel_encoder *encoder;
13400 
13401 	for_each_intel_encoder(&dev_priv->drm, encoder) {
13402 		struct intel_crtc_state *crtc_state;
13403 
13404 		if (!encoder->get_power_domains)
13405 			continue;
13406 
13407 		/*
13408 		 * MST-primary and inactive encoders don't have a crtc state
13409 		 * and neither of these require any power domain references.
13410 		 */
13411 		if (!encoder->base.crtc)
13412 			continue;
13413 
13414 		crtc_state = to_intel_crtc_state(encoder->base.crtc->state);
13415 		encoder->get_power_domains(encoder, crtc_state);
13416 	}
13417 }
13418 
13419 static void intel_early_display_was(struct drm_i915_private *dev_priv)
13420 {
13421 	/*
13422 	 * Display WA #1185 WaDisableDARBFClkGating:cnl,glk,icl,ehl,tgl
13423 	 * Also known as Wa_14010480278.
13424 	 */
13425 	if (IS_GEN_RANGE(dev_priv, 10, 12) || IS_GEMINILAKE(dev_priv))
13426 		intel_de_write(dev_priv, GEN9_CLKGATE_DIS_0,
13427 			       intel_de_read(dev_priv, GEN9_CLKGATE_DIS_0) | DARBF_GATING_DIS);
13428 
13429 	if (IS_HASWELL(dev_priv)) {
13430 		/*
13431 		 * WaRsPkgCStateDisplayPMReq:hsw
13432 		 * System hang if this isn't done before disabling all planes!
13433 		 */
13434 		intel_de_write(dev_priv, CHICKEN_PAR1_1,
13435 			       intel_de_read(dev_priv, CHICKEN_PAR1_1) | FORCE_ARB_IDLE_PLANES);
13436 	}
13437 
13438 	if (IS_KABYLAKE(dev_priv) || IS_COFFEELAKE(dev_priv) || IS_COMETLAKE(dev_priv)) {
13439 		/* Display WA #1142:kbl,cfl,cml */
13440 		intel_de_rmw(dev_priv, CHICKEN_PAR1_1,
13441 			     KBL_ARB_FILL_SPARE_22, KBL_ARB_FILL_SPARE_22);
13442 		intel_de_rmw(dev_priv, CHICKEN_MISC_2,
13443 			     KBL_ARB_FILL_SPARE_13 | KBL_ARB_FILL_SPARE_14,
13444 			     KBL_ARB_FILL_SPARE_14);
13445 	}
13446 }
13447 
13448 static void ibx_sanitize_pch_hdmi_port(struct drm_i915_private *dev_priv,
13449 				       enum port port, i915_reg_t hdmi_reg)
13450 {
13451 	u32 val = intel_de_read(dev_priv, hdmi_reg);
13452 
13453 	if (val & SDVO_ENABLE ||
13454 	    (val & SDVO_PIPE_SEL_MASK) == SDVO_PIPE_SEL(PIPE_A))
13455 		return;
13456 
13457 	drm_dbg_kms(&dev_priv->drm,
13458 		    "Sanitizing transcoder select for HDMI %c\n",
13459 		    port_name(port));
13460 
13461 	val &= ~SDVO_PIPE_SEL_MASK;
13462 	val |= SDVO_PIPE_SEL(PIPE_A);
13463 
13464 	intel_de_write(dev_priv, hdmi_reg, val);
13465 }
13466 
13467 static void ibx_sanitize_pch_dp_port(struct drm_i915_private *dev_priv,
13468 				     enum port port, i915_reg_t dp_reg)
13469 {
13470 	u32 val = intel_de_read(dev_priv, dp_reg);
13471 
13472 	if (val & DP_PORT_EN ||
13473 	    (val & DP_PIPE_SEL_MASK) == DP_PIPE_SEL(PIPE_A))
13474 		return;
13475 
13476 	drm_dbg_kms(&dev_priv->drm,
13477 		    "Sanitizing transcoder select for DP %c\n",
13478 		    port_name(port));
13479 
13480 	val &= ~DP_PIPE_SEL_MASK;
13481 	val |= DP_PIPE_SEL(PIPE_A);
13482 
13483 	intel_de_write(dev_priv, dp_reg, val);
13484 }
13485 
13486 static void ibx_sanitize_pch_ports(struct drm_i915_private *dev_priv)
13487 {
13488 	/*
13489 	 * The BIOS may select transcoder B on some of the PCH
13490 	 * ports even it doesn't enable the port. This would trip
13491 	 * assert_pch_dp_disabled() and assert_pch_hdmi_disabled().
13492 	 * Sanitize the transcoder select bits to prevent that. We
13493 	 * assume that the BIOS never actually enabled the port,
13494 	 * because if it did we'd actually have to toggle the port
13495 	 * on and back off to make the transcoder A select stick
13496 	 * (see. intel_dp_link_down(), intel_disable_hdmi(),
13497 	 * intel_disable_sdvo()).
13498 	 */
13499 	ibx_sanitize_pch_dp_port(dev_priv, PORT_B, PCH_DP_B);
13500 	ibx_sanitize_pch_dp_port(dev_priv, PORT_C, PCH_DP_C);
13501 	ibx_sanitize_pch_dp_port(dev_priv, PORT_D, PCH_DP_D);
13502 
13503 	/* PCH SDVOB multiplex with HDMIB */
13504 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_B, PCH_HDMIB);
13505 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_C, PCH_HDMIC);
13506 	ibx_sanitize_pch_hdmi_port(dev_priv, PORT_D, PCH_HDMID);
13507 }
13508 
13509 /* Scan out the current hw modeset state,
13510  * and sanitizes it to the current state
13511  */
13512 static void
13513 intel_modeset_setup_hw_state(struct drm_device *dev,
13514 			     struct drm_modeset_acquire_ctx *ctx)
13515 {
13516 	struct drm_i915_private *dev_priv = to_i915(dev);
13517 	struct intel_encoder *encoder;
13518 	struct intel_crtc *crtc;
13519 	intel_wakeref_t wakeref;
13520 
13521 	wakeref = intel_display_power_get(dev_priv, POWER_DOMAIN_INIT);
13522 
13523 	intel_early_display_was(dev_priv);
13524 	intel_modeset_readout_hw_state(dev);
13525 
13526 	/* HW state is read out, now we need to sanitize this mess. */
13527 
13528 	/* Sanitize the TypeC port mode upfront, encoders depend on this */
13529 	for_each_intel_encoder(dev, encoder) {
13530 		enum phy phy = intel_port_to_phy(dev_priv, encoder->port);
13531 
13532 		/* We need to sanitize only the MST primary port. */
13533 		if (encoder->type != INTEL_OUTPUT_DP_MST &&
13534 		    intel_phy_is_tc(dev_priv, phy))
13535 			intel_tc_port_sanitize(enc_to_dig_port(encoder));
13536 	}
13537 
13538 	get_encoder_power_domains(dev_priv);
13539 
13540 	if (HAS_PCH_IBX(dev_priv))
13541 		ibx_sanitize_pch_ports(dev_priv);
13542 
13543 	/*
13544 	 * intel_sanitize_plane_mapping() may need to do vblank
13545 	 * waits, so we need vblank interrupts restored beforehand.
13546 	 */
13547 	for_each_intel_crtc(&dev_priv->drm, crtc) {
13548 		struct intel_crtc_state *crtc_state =
13549 			to_intel_crtc_state(crtc->base.state);
13550 
13551 		drm_crtc_vblank_reset(&crtc->base);
13552 
13553 		if (crtc_state->hw.active)
13554 			intel_crtc_vblank_on(crtc_state);
13555 	}
13556 
13557 	intel_sanitize_plane_mapping(dev_priv);
13558 
13559 	for_each_intel_encoder(dev, encoder)
13560 		intel_sanitize_encoder(encoder);
13561 
13562 	for_each_intel_crtc(&dev_priv->drm, crtc) {
13563 		struct intel_crtc_state *crtc_state =
13564 			to_intel_crtc_state(crtc->base.state);
13565 
13566 		intel_sanitize_crtc(crtc, ctx);
13567 		intel_dump_pipe_config(crtc_state, NULL, "[setup_hw_state]");
13568 	}
13569 
13570 	intel_modeset_update_connector_atomic_state(dev);
13571 
13572 	intel_dpll_sanitize_state(dev_priv);
13573 
13574 	if (IS_G4X(dev_priv)) {
13575 		g4x_wm_get_hw_state(dev_priv);
13576 		g4x_wm_sanitize(dev_priv);
13577 	} else if (IS_VALLEYVIEW(dev_priv) || IS_CHERRYVIEW(dev_priv)) {
13578 		vlv_wm_get_hw_state(dev_priv);
13579 		vlv_wm_sanitize(dev_priv);
13580 	} else if (INTEL_GEN(dev_priv) >= 9) {
13581 		skl_wm_get_hw_state(dev_priv);
13582 	} else if (HAS_PCH_SPLIT(dev_priv)) {
13583 		ilk_wm_get_hw_state(dev_priv);
13584 	}
13585 
13586 	for_each_intel_crtc(dev, crtc) {
13587 		struct intel_crtc_state *crtc_state =
13588 			to_intel_crtc_state(crtc->base.state);
13589 		u64 put_domains;
13590 
13591 		put_domains = modeset_get_crtc_power_domains(crtc_state);
13592 		if (drm_WARN_ON(dev, put_domains))
13593 			modeset_put_crtc_power_domains(crtc, put_domains);
13594 	}
13595 
13596 	intel_display_power_put(dev_priv, POWER_DOMAIN_INIT, wakeref);
13597 }
13598 
13599 void intel_display_resume(struct drm_device *dev)
13600 {
13601 	struct drm_i915_private *dev_priv = to_i915(dev);
13602 	struct drm_atomic_state *state = dev_priv->modeset_restore_state;
13603 	struct drm_modeset_acquire_ctx ctx;
13604 	int ret;
13605 
13606 	dev_priv->modeset_restore_state = NULL;
13607 	if (state)
13608 		state->acquire_ctx = &ctx;
13609 
13610 	drm_modeset_acquire_init(&ctx, 0);
13611 
13612 	while (1) {
13613 		ret = drm_modeset_lock_all_ctx(dev, &ctx);
13614 		if (ret != -EDEADLK)
13615 			break;
13616 
13617 		drm_modeset_backoff(&ctx);
13618 	}
13619 
13620 	if (!ret)
13621 		ret = __intel_display_resume(dev, state, &ctx);
13622 
13623 	intel_enable_ipc(dev_priv);
13624 	drm_modeset_drop_locks(&ctx);
13625 	drm_modeset_acquire_fini(&ctx);
13626 
13627 	if (ret)
13628 		drm_err(&dev_priv->drm,
13629 			"Restoring old state failed with %i\n", ret);
13630 	if (state)
13631 		drm_atomic_state_put(state);
13632 }
13633 
13634 static void intel_hpd_poll_fini(struct drm_i915_private *i915)
13635 {
13636 	struct intel_connector *connector;
13637 	struct drm_connector_list_iter conn_iter;
13638 
13639 	/* Kill all the work that may have been queued by hpd. */
13640 	drm_connector_list_iter_begin(&i915->drm, &conn_iter);
13641 	for_each_intel_connector_iter(connector, &conn_iter) {
13642 		if (connector->modeset_retry_work.func)
13643 			cancel_work_sync(&connector->modeset_retry_work);
13644 		if (connector->hdcp.shim) {
13645 			cancel_delayed_work_sync(&connector->hdcp.check_work);
13646 			cancel_work_sync(&connector->hdcp.prop_work);
13647 		}
13648 	}
13649 	drm_connector_list_iter_end(&conn_iter);
13650 }
13651 
13652 /* part #1: call before irq uninstall */
13653 void intel_modeset_driver_remove(struct drm_i915_private *i915)
13654 {
13655 	flush_workqueue(i915->flip_wq);
13656 	flush_workqueue(i915->modeset_wq);
13657 
13658 	flush_work(&i915->atomic_helper.free_work);
13659 	drm_WARN_ON(&i915->drm, !llist_empty(&i915->atomic_helper.free_list));
13660 }
13661 
13662 /* part #2: call after irq uninstall */
13663 void intel_modeset_driver_remove_noirq(struct drm_i915_private *i915)
13664 {
13665 	/*
13666 	 * Due to the hpd irq storm handling the hotplug work can re-arm the
13667 	 * poll handlers. Hence disable polling after hpd handling is shut down.
13668 	 */
13669 	intel_hpd_poll_fini(i915);
13670 
13671 	/*
13672 	 * MST topology needs to be suspended so we don't have any calls to
13673 	 * fbdev after it's finalized. MST will be destroyed later as part of
13674 	 * drm_mode_config_cleanup()
13675 	 */
13676 	intel_dp_mst_suspend(i915);
13677 
13678 	/* poll work can call into fbdev, hence clean that up afterwards */
13679 	intel_fbdev_fini(i915);
13680 
13681 	intel_unregister_dsm_handler();
13682 
13683 	intel_fbc_global_disable(i915);
13684 
13685 	/* flush any delayed tasks or pending work */
13686 	flush_scheduled_work();
13687 
13688 	intel_hdcp_component_fini(i915);
13689 
13690 	intel_mode_config_cleanup(i915);
13691 
13692 	intel_overlay_cleanup(i915);
13693 
13694 	intel_gmbus_teardown(i915);
13695 
13696 	destroy_workqueue(i915->flip_wq);
13697 	destroy_workqueue(i915->modeset_wq);
13698 
13699 	intel_fbc_cleanup_cfb(i915);
13700 }
13701 
13702 /* part #3: call after gem init */
13703 void intel_modeset_driver_remove_nogem(struct drm_i915_private *i915)
13704 {
13705 	intel_csr_ucode_fini(i915);
13706 
13707 	intel_power_domains_driver_remove(i915);
13708 
13709 	intel_vga_unregister(i915);
13710 
13711 	intel_bios_driver_remove(i915);
13712 }
13713 
13714 void intel_display_driver_register(struct drm_i915_private *i915)
13715 {
13716 	if (!HAS_DISPLAY(i915))
13717 		return;
13718 
13719 	intel_display_debugfs_register(i915);
13720 
13721 	/* Must be done after probing outputs */
13722 	intel_opregion_register(i915);
13723 	acpi_video_register();
13724 
13725 	intel_audio_init(i915);
13726 
13727 	/*
13728 	 * Some ports require correctly set-up hpd registers for
13729 	 * detection to work properly (leading to ghost connected
13730 	 * connector status), e.g. VGA on gm45.  Hence we can only set
13731 	 * up the initial fbdev config after hpd irqs are fully
13732 	 * enabled. We do it last so that the async config cannot run
13733 	 * before the connectors are registered.
13734 	 */
13735 	intel_fbdev_initial_config_async(&i915->drm);
13736 
13737 	/*
13738 	 * We need to coordinate the hotplugs with the asynchronous
13739 	 * fbdev configuration, for which we use the
13740 	 * fbdev->async_cookie.
13741 	 */
13742 	drm_kms_helper_poll_init(&i915->drm);
13743 }
13744 
13745 void intel_display_driver_unregister(struct drm_i915_private *i915)
13746 {
13747 	if (!HAS_DISPLAY(i915))
13748 		return;
13749 
13750 	intel_fbdev_unregister(i915);
13751 	intel_audio_deinit(i915);
13752 
13753 	/*
13754 	 * After flushing the fbdev (incl. a late async config which
13755 	 * will have delayed queuing of a hotplug event), then flush
13756 	 * the hotplug events.
13757 	 */
13758 	drm_kms_helper_poll_fini(&i915->drm);
13759 	drm_atomic_helper_shutdown(&i915->drm);
13760 
13761 	acpi_video_unregister();
13762 	intel_opregion_unregister(i915);
13763 }
13764 
13765 #if IS_ENABLED(CONFIG_DRM_I915_CAPTURE_ERROR)
13766 
13767 struct intel_display_error_state {
13768 
13769 	u32 power_well_driver;
13770 
13771 	struct intel_cursor_error_state {
13772 		u32 control;
13773 		u32 position;
13774 		u32 base;
13775 		u32 size;
13776 	} cursor[I915_MAX_PIPES];
13777 
13778 	struct intel_pipe_error_state {
13779 		bool power_domain_on;
13780 		u32 source;
13781 		u32 stat;
13782 	} pipe[I915_MAX_PIPES];
13783 
13784 	struct intel_plane_error_state {
13785 		u32 control;
13786 		u32 stride;
13787 		u32 size;
13788 		u32 pos;
13789 		u32 addr;
13790 		u32 surface;
13791 		u32 tile_offset;
13792 	} plane[I915_MAX_PIPES];
13793 
13794 	struct intel_transcoder_error_state {
13795 		bool available;
13796 		bool power_domain_on;
13797 		enum transcoder cpu_transcoder;
13798 
13799 		u32 conf;
13800 
13801 		u32 htotal;
13802 		u32 hblank;
13803 		u32 hsync;
13804 		u32 vtotal;
13805 		u32 vblank;
13806 		u32 vsync;
13807 	} transcoder[5];
13808 };
13809 
13810 struct intel_display_error_state *
13811 intel_display_capture_error_state(struct drm_i915_private *dev_priv)
13812 {
13813 	struct intel_display_error_state *error;
13814 	int transcoders[] = {
13815 		TRANSCODER_A,
13816 		TRANSCODER_B,
13817 		TRANSCODER_C,
13818 		TRANSCODER_D,
13819 		TRANSCODER_EDP,
13820 	};
13821 	int i;
13822 
13823 	BUILD_BUG_ON(ARRAY_SIZE(transcoders) != ARRAY_SIZE(error->transcoder));
13824 
13825 	if (!HAS_DISPLAY(dev_priv))
13826 		return NULL;
13827 
13828 	error = kzalloc(sizeof(*error), GFP_ATOMIC);
13829 	if (error == NULL)
13830 		return NULL;
13831 
13832 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
13833 		error->power_well_driver = intel_de_read(dev_priv,
13834 							 HSW_PWR_WELL_CTL2);
13835 
13836 	for_each_pipe(dev_priv, i) {
13837 		error->pipe[i].power_domain_on =
13838 			__intel_display_power_is_enabled(dev_priv,
13839 							 POWER_DOMAIN_PIPE(i));
13840 		if (!error->pipe[i].power_domain_on)
13841 			continue;
13842 
13843 		error->cursor[i].control = intel_de_read(dev_priv, CURCNTR(i));
13844 		error->cursor[i].position = intel_de_read(dev_priv, CURPOS(i));
13845 		error->cursor[i].base = intel_de_read(dev_priv, CURBASE(i));
13846 
13847 		error->plane[i].control = intel_de_read(dev_priv, DSPCNTR(i));
13848 		error->plane[i].stride = intel_de_read(dev_priv, DSPSTRIDE(i));
13849 		if (INTEL_GEN(dev_priv) <= 3) {
13850 			error->plane[i].size = intel_de_read(dev_priv,
13851 							     DSPSIZE(i));
13852 			error->plane[i].pos = intel_de_read(dev_priv,
13853 							    DSPPOS(i));
13854 		}
13855 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
13856 			error->plane[i].addr = intel_de_read(dev_priv,
13857 							     DSPADDR(i));
13858 		if (INTEL_GEN(dev_priv) >= 4) {
13859 			error->plane[i].surface = intel_de_read(dev_priv,
13860 								DSPSURF(i));
13861 			error->plane[i].tile_offset = intel_de_read(dev_priv,
13862 								    DSPTILEOFF(i));
13863 		}
13864 
13865 		error->pipe[i].source = intel_de_read(dev_priv, PIPESRC(i));
13866 
13867 		if (HAS_GMCH(dev_priv))
13868 			error->pipe[i].stat = intel_de_read(dev_priv,
13869 							    PIPESTAT(i));
13870 	}
13871 
13872 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
13873 		enum transcoder cpu_transcoder = transcoders[i];
13874 
13875 		if (!HAS_TRANSCODER(dev_priv, cpu_transcoder))
13876 			continue;
13877 
13878 		error->transcoder[i].available = true;
13879 		error->transcoder[i].power_domain_on =
13880 			__intel_display_power_is_enabled(dev_priv,
13881 				POWER_DOMAIN_TRANSCODER(cpu_transcoder));
13882 		if (!error->transcoder[i].power_domain_on)
13883 			continue;
13884 
13885 		error->transcoder[i].cpu_transcoder = cpu_transcoder;
13886 
13887 		error->transcoder[i].conf = intel_de_read(dev_priv,
13888 							  PIPECONF(cpu_transcoder));
13889 		error->transcoder[i].htotal = intel_de_read(dev_priv,
13890 							    HTOTAL(cpu_transcoder));
13891 		error->transcoder[i].hblank = intel_de_read(dev_priv,
13892 							    HBLANK(cpu_transcoder));
13893 		error->transcoder[i].hsync = intel_de_read(dev_priv,
13894 							   HSYNC(cpu_transcoder));
13895 		error->transcoder[i].vtotal = intel_de_read(dev_priv,
13896 							    VTOTAL(cpu_transcoder));
13897 		error->transcoder[i].vblank = intel_de_read(dev_priv,
13898 							    VBLANK(cpu_transcoder));
13899 		error->transcoder[i].vsync = intel_de_read(dev_priv,
13900 							   VSYNC(cpu_transcoder));
13901 	}
13902 
13903 	return error;
13904 }
13905 
13906 #define err_printf(e, ...) i915_error_printf(e, __VA_ARGS__)
13907 
13908 void
13909 intel_display_print_error_state(struct drm_i915_error_state_buf *m,
13910 				struct intel_display_error_state *error)
13911 {
13912 	struct drm_i915_private *dev_priv = m->i915;
13913 	int i;
13914 
13915 	if (!error)
13916 		return;
13917 
13918 	err_printf(m, "Num Pipes: %d\n", INTEL_NUM_PIPES(dev_priv));
13919 	if (IS_HASWELL(dev_priv) || IS_BROADWELL(dev_priv))
13920 		err_printf(m, "PWR_WELL_CTL2: %08x\n",
13921 			   error->power_well_driver);
13922 	for_each_pipe(dev_priv, i) {
13923 		err_printf(m, "Pipe [%d]:\n", i);
13924 		err_printf(m, "  Power: %s\n",
13925 			   onoff(error->pipe[i].power_domain_on));
13926 		err_printf(m, "  SRC: %08x\n", error->pipe[i].source);
13927 		err_printf(m, "  STAT: %08x\n", error->pipe[i].stat);
13928 
13929 		err_printf(m, "Plane [%d]:\n", i);
13930 		err_printf(m, "  CNTR: %08x\n", error->plane[i].control);
13931 		err_printf(m, "  STRIDE: %08x\n", error->plane[i].stride);
13932 		if (INTEL_GEN(dev_priv) <= 3) {
13933 			err_printf(m, "  SIZE: %08x\n", error->plane[i].size);
13934 			err_printf(m, "  POS: %08x\n", error->plane[i].pos);
13935 		}
13936 		if (INTEL_GEN(dev_priv) <= 7 && !IS_HASWELL(dev_priv))
13937 			err_printf(m, "  ADDR: %08x\n", error->plane[i].addr);
13938 		if (INTEL_GEN(dev_priv) >= 4) {
13939 			err_printf(m, "  SURF: %08x\n", error->plane[i].surface);
13940 			err_printf(m, "  TILEOFF: %08x\n", error->plane[i].tile_offset);
13941 		}
13942 
13943 		err_printf(m, "Cursor [%d]:\n", i);
13944 		err_printf(m, "  CNTR: %08x\n", error->cursor[i].control);
13945 		err_printf(m, "  POS: %08x\n", error->cursor[i].position);
13946 		err_printf(m, "  BASE: %08x\n", error->cursor[i].base);
13947 	}
13948 
13949 	for (i = 0; i < ARRAY_SIZE(error->transcoder); i++) {
13950 		if (!error->transcoder[i].available)
13951 			continue;
13952 
13953 		err_printf(m, "CPU transcoder: %s\n",
13954 			   transcoder_name(error->transcoder[i].cpu_transcoder));
13955 		err_printf(m, "  Power: %s\n",
13956 			   onoff(error->transcoder[i].power_domain_on));
13957 		err_printf(m, "  CONF: %08x\n", error->transcoder[i].conf);
13958 		err_printf(m, "  HTOTAL: %08x\n", error->transcoder[i].htotal);
13959 		err_printf(m, "  HBLANK: %08x\n", error->transcoder[i].hblank);
13960 		err_printf(m, "  HSYNC: %08x\n", error->transcoder[i].hsync);
13961 		err_printf(m, "  VTOTAL: %08x\n", error->transcoder[i].vtotal);
13962 		err_printf(m, "  VBLANK: %08x\n", error->transcoder[i].vblank);
13963 		err_printf(m, "  VSYNC: %08x\n", error->transcoder[i].vsync);
13964 	}
13965 }
13966 
13967 #endif
13968